Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / linux / netlink.c @ 153f02da

History | View | Annotate | Download (47.6 KB)

1 95616c82 Ondrej Zajicek
/*
2
 *        BIRD -- Linux Netlink Interface
3
 *
4
 *        (c) 1999--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 a8caff32 Jan Moskyto Matejka
#include <alloca.h>
10 95616c82 Ondrej Zajicek
#include <stdio.h>
11 f83ce94d Ondrej Zajicek
#include <unistd.h>
12 95616c82 Ondrej Zajicek
#include <fcntl.h>
13
#include <sys/socket.h>
14
#include <sys/uio.h>
15
#include <errno.h>
16
17
#undef LOCAL_DEBUG
18
19
#include "nest/bird.h"
20
#include "nest/route.h"
21
#include "nest/protocol.h"
22
#include "nest/iface.h"
23 4e276a89 Jan Moskyto Matejka
#include "lib/alloca.h"
24 7152e5ef Jan Moskyto Matejka
#include "sysdep/unix/timer.h"
25
#include "sysdep/unix/unix.h"
26
#include "sysdep/unix/krt.h"
27 95616c82 Ondrej Zajicek
#include "lib/socket.h"
28
#include "lib/string.h"
29 9ddbfbdd Jan Moskyto Matejka
#include "lib/hash.h"
30 95616c82 Ondrej Zajicek
#include "conf/conf.h"
31
32
#include <asm/types.h>
33
#include <linux/if.h>
34 54635f43 Jan Moskyto Matejka
#ifdef HAVE_LWTUNNEL
35 d14f8c3c Jan Moskyto Matejka
#include <linux/lwtunnel.h>
36 54635f43 Jan Moskyto Matejka
#else
37
#include "sysdep/linux/lwtunnel.h"
38
#endif
39 95616c82 Ondrej Zajicek
#include <linux/netlink.h>
40
#include <linux/rtnetlink.h>
41
42 9ddbfbdd Jan Moskyto Matejka
43 95616c82 Ondrej Zajicek
#ifndef MSG_TRUNC                        /* Hack: Several versions of glibc miss this one :( */
44
#define MSG_TRUNC 0x20
45
#endif
46
47 a08a81c6 Ondrej Zajicek (work)
#ifndef IFA_FLAGS
48
#define IFA_FLAGS 8
49
#endif
50
51 95616c82 Ondrej Zajicek
#ifndef IFF_LOWER_UP
52
#define IFF_LOWER_UP 0x10000
53
#endif
54
55 9ddbfbdd Jan Moskyto Matejka
#ifndef RTA_TABLE
56
#define RTA_TABLE  15
57
#endif
58
59 d14f8c3c Jan Moskyto Matejka
#ifndef RTA_VIA
60
#define RTA_VIA         18
61
#endif
62
63 97e48b6a Jan Moskyto Matejka
#ifndef HAVE_STRUCT_RTVIA
64
struct rtvia {
65 734e9fb8 Ondrej Zajicek (work)
        unsigned short        rtvia_family;
66
        u8                rtvia_addr[0];
67 97e48b6a Jan Moskyto Matejka
};
68
#endif
69
70 d14f8c3c Jan Moskyto Matejka
#ifndef RTA_NEWDST
71
#define RTA_NEWDST  19
72
#endif
73
74
#ifndef RTA_ENCAP_TYPE
75
#define RTA_ENCAP_TYPE        21
76
#endif
77
78
#ifndef RTA_ENCAP
79
#define RTA_ENCAP  22
80
#endif
81 9ddbfbdd Jan Moskyto Matejka
82 cc5b93f7 Ondrej Zajicek (work)
#define krt_ecmp6(p) ((p)->af == AF_INET6)
83 2feaa693 Ondrej Zajicek (work)
84
/*
85
 * Structure nl_parse_state keeps state of received route processing. Ideally,
86
 * we could just independently parse received Netlink messages and immediately
87
 * propagate received routes to the rest of BIRD, but Linux kernel represents
88
 * and announces IPv6 ECMP routes not as one route with multiple next hops (like
89
 * RTA_MULTIPATH in IPv4 ECMP), but as a set of routes with the same prefix.
90
 *
91
 * Therefore, BIRD keeps currently processed route in nl_parse_state structure
92
 * and postpones its propagation until we expect it to be final; i.e., when
93
 * non-matching route is received or when the scan ends. When another matching
94
 * route is received, it is merged with the already processed route to form an
95
 * ECMP route. Note that merging is done only for IPv6 (merge == 1), but the
96
 * postponing is done in both cases (for simplicity). All IPv4 routes are just
97
 * considered non-matching.
98
 *
99
 * This is ignored for asynchronous notifications (every notification is handled
100
 * as a separate route). It is not an issue for our routes, as we ignore such
101
 * notifications anyways. But importing alien IPv6 ECMP routes does not work
102
 * properly.
103
 */
104
105
struct nl_parse_state
106
{
107
  struct linpool *pool;
108
  int scan;
109
  int merge;
110
111
  net *net;
112
  rta *attrs;
113
  struct krt_proto *proto;
114
  s8 new;
115
  s8 krt_src;
116
  u8 krt_type;
117
  u8 krt_proto;
118
  u32 krt_metric;
119
};
120
121 95616c82 Ondrej Zajicek
/*
122
 *        Synchronous Netlink interface
123
 */
124
125
struct nl_sock
126
{
127
  int fd;
128
  u32 seq;
129
  byte *rx_buffer;                        /* Receive buffer */
130
  struct nlmsghdr *last_hdr;                /* Recently received packet */
131 ae80a2de Pavel Tvrdík
  uint last_size;
132 95616c82 Ondrej Zajicek
};
133
134
#define NL_RX_SIZE 8192
135
136 2feaa693 Ondrej Zajicek (work)
#define NL_OP_DELETE        0
137
#define NL_OP_ADD        (NLM_F_CREATE|NLM_F_EXCL)
138
#define NL_OP_REPLACE        (NLM_F_CREATE|NLM_F_REPLACE)
139
#define NL_OP_APPEND        (NLM_F_CREATE|NLM_F_APPEND)
140
141
static linpool *nl_linpool;
142
143 95616c82 Ondrej Zajicek
static struct nl_sock nl_scan = {.fd = -1};        /* Netlink socket for synchronous scan */
144
static struct nl_sock nl_req  = {.fd = -1};        /* Netlink socket for requests */
145
146
static void
147
nl_open_sock(struct nl_sock *nl)
148
{
149
  if (nl->fd < 0)
150
    {
151
      nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
152
      if (nl->fd < 0)
153
        die("Unable to open rtnetlink socket: %m");
154
      nl->seq = now;
155
      nl->rx_buffer = xmalloc(NL_RX_SIZE);
156
      nl->last_hdr = NULL;
157
      nl->last_size = 0;
158
    }
159
}
160
161
static void
162
nl_open(void)
163
{
164
  nl_open_sock(&nl_scan);
165
  nl_open_sock(&nl_req);
166
}
167
168
static void
169
nl_send(struct nl_sock *nl, struct nlmsghdr *nh)
170
{
171
  struct sockaddr_nl sa;
172
173
  memset(&sa, 0, sizeof(sa));
174
  sa.nl_family = AF_NETLINK;
175
  nh->nlmsg_pid = 0;
176
  nh->nlmsg_seq = ++(nl->seq);
177
  if (sendto(nl->fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0)
178
    die("rtnetlink sendto: %m");
179
  nl->last_hdr = NULL;
180
}
181
182
static void
183 86c3eea0 Ondrej Zajicek
nl_request_dump(int af, int cmd)
184 95616c82 Ondrej Zajicek
{
185
  struct {
186
    struct nlmsghdr nh;
187
    struct rtgenmsg g;
188 641172c6 Ondrej Zajicek
  } req = {
189
    .nh.nlmsg_type = cmd,
190
    .nh.nlmsg_len = sizeof(req),
191
    .nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
192
    .g.rtgen_family = af
193
  };
194 95616c82 Ondrej Zajicek
  nl_send(&nl_scan, &req.nh);
195
}
196
197
static struct nlmsghdr *
198
nl_get_reply(struct nl_sock *nl)
199
{
200
  for(;;)
201
    {
202
      if (!nl->last_hdr)
203
        {
204
          struct iovec iov = { nl->rx_buffer, NL_RX_SIZE };
205
          struct sockaddr_nl sa;
206 31e9e101 Stijn Tintel
          struct msghdr m = {
207
            .msg_name = &sa,
208
            .msg_namelen = sizeof(sa),
209
            .msg_iov = &iov,
210
            .msg_iovlen = 1,
211
          };
212 95616c82 Ondrej Zajicek
          int x = recvmsg(nl->fd, &m, 0);
213
          if (x < 0)
214
            die("nl_get_reply: %m");
215
          if (sa.nl_pid)                /* It isn't from the kernel */
216
            {
217
              DBG("Non-kernel packet\n");
218
              continue;
219
            }
220
          nl->last_size = x;
221
          nl->last_hdr = (void *) nl->rx_buffer;
222
          if (m.msg_flags & MSG_TRUNC)
223
            bug("nl_get_reply: got truncated reply which should be impossible");
224
        }
225
      if (NLMSG_OK(nl->last_hdr, nl->last_size))
226
        {
227
          struct nlmsghdr *h = nl->last_hdr;
228
          nl->last_hdr = NLMSG_NEXT(h, nl->last_size);
229
          if (h->nlmsg_seq != nl->seq)
230
            {
231
              log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)",
232
                  h->nlmsg_seq, nl->seq);
233
              continue;
234
            }
235
          return h;
236
        }
237
      if (nl->last_size)
238
        log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl->last_size);
239
      nl->last_hdr = NULL;
240
    }
241
}
242
243 1123e707 Ondrej Zajicek
static struct tbf rl_netlink_err = TBF_DEFAULT_LOG_LIMITS;
244 95616c82 Ondrej Zajicek
245
static int
246 2feaa693 Ondrej Zajicek (work)
nl_error(struct nlmsghdr *h, int ignore_esrch)
247 95616c82 Ondrej Zajicek
{
248
  struct nlmsgerr *e;
249
  int ec;
250
251
  if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
252
    {
253
      log(L_WARN "Netlink: Truncated error message received");
254
      return ENOBUFS;
255
    }
256
  e = (struct nlmsgerr *) NLMSG_DATA(h);
257
  ec = -e->error;
258 2feaa693 Ondrej Zajicek (work)
  if (ec && !(ignore_esrch && (ec == ESRCH)))
259 95616c82 Ondrej Zajicek
    log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec));
260
  return ec;
261
}
262
263
static struct nlmsghdr *
264
nl_get_scan(void)
265
{
266
  struct nlmsghdr *h = nl_get_reply(&nl_scan);
267
268
  if (h->nlmsg_type == NLMSG_DONE)
269
    return NULL;
270
  if (h->nlmsg_type == NLMSG_ERROR)
271
    {
272 2feaa693 Ondrej Zajicek (work)
      nl_error(h, 0);
273 95616c82 Ondrej Zajicek
      return NULL;
274
    }
275
  return h;
276
}
277
278
static int
279 2feaa693 Ondrej Zajicek (work)
nl_exchange(struct nlmsghdr *pkt, int ignore_esrch)
280 95616c82 Ondrej Zajicek
{
281
  struct nlmsghdr *h;
282
283
  nl_send(&nl_req, pkt);
284
  for(;;)
285
    {
286
      h = nl_get_reply(&nl_req);
287
      if (h->nlmsg_type == NLMSG_ERROR)
288
        break;
289
      log(L_WARN "nl_exchange: Unexpected reply received");
290
    }
291 2feaa693 Ondrej Zajicek (work)
  return nl_error(h, ignore_esrch) ? -1 : 0;
292 95616c82 Ondrej Zajicek
}
293
294
/*
295
 *        Netlink attributes
296
 */
297
298
static int nl_attr_len;
299
300
static void *
301
nl_checkin(struct nlmsghdr *h, int lsize)
302
{
303
  nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize);
304
  if (nl_attr_len < 0)
305
    {
306
      log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len);
307
      return NULL;
308
    }
309
  return NLMSG_DATA(h);
310
}
311
312 ad276157 Jan Moskyto Matejka
struct nl_want_attrs {
313
  u8 defined:1;
314
  u8 checksize:1;
315
  u8 size;
316
};
317
318
319
#define BIRD_IFLA_MAX (IFLA_WIRELESS+1)
320
321
static struct nl_want_attrs ifla_attr_want[BIRD_IFLA_MAX] = {
322
  [IFLA_IFNAME]          = { 1, 0, 0 },
323
  [IFLA_MTU]          = { 1, 1, sizeof(u32) },
324
  [IFLA_WIRELESS] = { 1, 0, 0 },
325
};
326
327 29a64162 Ondrej Zajicek (work)
328 e37d2e3e Ondrej Zajicek (work)
#define BIRD_IFA_MAX  (IFA_FLAGS+1)
329 ad276157 Jan Moskyto Matejka
330
static struct nl_want_attrs ifa_attr_want4[BIRD_IFA_MAX] = {
331
  [IFA_ADDRESS]          = { 1, 1, sizeof(ip4_addr) },
332
  [IFA_LOCAL]          = { 1, 1, sizeof(ip4_addr) },
333
  [IFA_BROADCAST] = { 1, 1, sizeof(ip4_addr) },
334 62e64905 Ondrej Zajicek (work)
  [IFA_FLAGS]     = { 1, 1, sizeof(u32) },
335 ad276157 Jan Moskyto Matejka
};
336 29a64162 Ondrej Zajicek (work)
337 ad276157 Jan Moskyto Matejka
static struct nl_want_attrs ifa_attr_want6[BIRD_IFA_MAX] = {
338
  [IFA_ADDRESS]          = { 1, 1, sizeof(ip6_addr) },
339
  [IFA_LOCAL]          = { 1, 1, sizeof(ip6_addr) },
340 e37d2e3e Ondrej Zajicek (work)
  [IFA_FLAGS]          = { 1, 1, sizeof(u32) },
341 ad276157 Jan Moskyto Matejka
};
342 29a64162 Ondrej Zajicek (work)
343 ad276157 Jan Moskyto Matejka
344 d14f8c3c Jan Moskyto Matejka
#define BIRD_RTA_MAX  (RTA_ENCAP+1)
345 ad276157 Jan Moskyto Matejka
346 4e276a89 Jan Moskyto Matejka
static struct nl_want_attrs nexthop_attr_want4[BIRD_RTA_MAX] = {
347 ad276157 Jan Moskyto Matejka
  [RTA_GATEWAY]          = { 1, 1, sizeof(ip4_addr) },
348 d14f8c3c Jan Moskyto Matejka
  [RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) },
349
  [RTA_ENCAP]          = { 1, 0, 0 },
350
};
351
352
static struct nl_want_attrs encap_mpls_want[BIRD_RTA_MAX] = {
353
  [RTA_DST]       = { 1, 0, 0 },
354 ad276157 Jan Moskyto Matejka
};
355
356
static struct nl_want_attrs rtm_attr_want4[BIRD_RTA_MAX] = {
357
  [RTA_DST]          = { 1, 1, sizeof(ip4_addr) },
358
  [RTA_OIF]          = { 1, 1, sizeof(u32) },
359
  [RTA_GATEWAY]          = { 1, 1, sizeof(ip4_addr) },
360
  [RTA_PRIORITY]  = { 1, 1, sizeof(u32) },
361
  [RTA_PREFSRC]          = { 1, 1, sizeof(ip4_addr) },
362
  [RTA_METRICS]          = { 1, 0, 0 },
363
  [RTA_MULTIPATH] = { 1, 0, 0 },
364
  [RTA_FLOW]          = { 1, 1, sizeof(u32) },
365
  [RTA_TABLE]          = { 1, 1, sizeof(u32) },
366 d14f8c3c Jan Moskyto Matejka
  [RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) },
367
  [RTA_ENCAP]          = { 1, 0, 0 },
368 ad276157 Jan Moskyto Matejka
};
369 29a64162 Ondrej Zajicek (work)
370 ad276157 Jan Moskyto Matejka
static struct nl_want_attrs rtm_attr_want6[BIRD_RTA_MAX] = {
371
  [RTA_DST]          = { 1, 1, sizeof(ip6_addr) },
372
  [RTA_IIF]          = { 1, 1, sizeof(u32) },
373
  [RTA_OIF]          = { 1, 1, sizeof(u32) },
374
  [RTA_GATEWAY]          = { 1, 1, sizeof(ip6_addr) },
375
  [RTA_PRIORITY]  = { 1, 1, sizeof(u32) },
376
  [RTA_PREFSRC]          = { 1, 1, sizeof(ip6_addr) },
377
  [RTA_METRICS]          = { 1, 0, 0 },
378
  [RTA_FLOW]          = { 1, 1, sizeof(u32) },
379
  [RTA_TABLE]          = { 1, 1, sizeof(u32) },
380 d14f8c3c Jan Moskyto Matejka
  [RTA_ENCAP_TYPE]= { 1, 1, sizeof(u16) },
381
  [RTA_ENCAP]          = { 1, 0, 0 },
382
};
383
384
static struct nl_want_attrs rtm_attr_want_mpls[BIRD_RTA_MAX] = {
385
  [RTA_DST]          = { 1, 1, sizeof(u32) },
386
  [RTA_IIF]          = { 1, 1, sizeof(u32) },
387
  [RTA_OIF]          = { 1, 1, sizeof(u32) },
388
  [RTA_PRIORITY]  = { 1, 1, sizeof(u32) },
389
  [RTA_METRICS]          = { 1, 0, 0 },
390
  [RTA_FLOW]          = { 1, 1, sizeof(u32) },
391
  [RTA_TABLE]          = { 1, 1, sizeof(u32) },
392
  [RTA_VIA]          = { 1, 0, 0 },
393
  [RTA_NEWDST]          = { 1, 0, 0 },
394 ad276157 Jan Moskyto Matejka
};
395
396
397 95616c82 Ondrej Zajicek
static int
398 ad276157 Jan Moskyto Matejka
nl_parse_attrs(struct rtattr *a, struct nl_want_attrs *want, struct rtattr **k, int ksize)
399 95616c82 Ondrej Zajicek
{
400
  int max = ksize / sizeof(struct rtattr *);
401
  bzero(k, ksize);
402 ad276157 Jan Moskyto Matejka
403
  for ( ; RTA_OK(a, nl_attr_len); a = RTA_NEXT(a, nl_attr_len))
404 95616c82 Ondrej Zajicek
    {
405 ad276157 Jan Moskyto Matejka
      if ((a->rta_type >= max) || !want[a->rta_type].defined)
406
        continue;
407
408
      if (want[a->rta_type].checksize && (RTA_PAYLOAD(a) != want[a->rta_type].size))
409
        {
410 9b136840 Jan Moskyto Matejka
          log(L_ERR "nl_parse_attrs: Malformed attribute received");
411 ad276157 Jan Moskyto Matejka
          return 0;
412
        }
413
414
      k[a->rta_type] = a;
415 95616c82 Ondrej Zajicek
    }
416 ad276157 Jan Moskyto Matejka
417 95616c82 Ondrej Zajicek
  if (nl_attr_len)
418
    {
419
      log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len);
420
      return 0;
421
    }
422 ad276157 Jan Moskyto Matejka
423
  return 1;
424 95616c82 Ondrej Zajicek
}
425
426 d14f8c3c Jan Moskyto Matejka
static inline u16 rta_get_u16(struct rtattr *a)
427
{ return *(u16 *) RTA_DATA(a); }
428
429 fce764f9 Pavel Tvrdík
static inline u32 rta_get_u32(struct rtattr *a)
430 acb04cfd Ondrej Zajicek (work)
{ return *(u32 *) RTA_DATA(a); }
431
432
static inline ip4_addr rta_get_ip4(struct rtattr *a)
433
{ return ip4_ntoh(*(ip4_addr *) RTA_DATA(a)); }
434
435
static inline ip6_addr rta_get_ip6(struct rtattr *a)
436
{ return ip6_ntoh(*(ip6_addr *) RTA_DATA(a)); }
437
438 9b136840 Jan Moskyto Matejka
static inline ip_addr rta_get_ipa(struct rtattr *a)
439
{
440
  if (RTA_PAYLOAD(a) == sizeof(ip4_addr))
441
    return ipa_from_ip4(rta_get_ip4(a));
442
  else
443
    return ipa_from_ip6(rta_get_ip6(a));
444
}
445 acb04cfd Ondrej Zajicek (work)
446 d14f8c3c Jan Moskyto Matejka
static inline ip_addr rta_get_via(struct rtattr *a)
447
{
448
  struct rtvia *v = RTA_DATA(a);
449
  switch(v->rtvia_family) {
450
    case AF_INET:  return ipa_from_ip4(ip4_ntoh(*(ip4_addr *) v->rtvia_addr));
451
    case AF_INET6: return ipa_from_ip6(ip6_ntoh(*(ip6_addr *) v->rtvia_addr));
452
  }
453
  return IPA_NONE;
454
}
455
456
static u32 rta_mpls_stack[MPLS_MAX_LABEL_STACK];
457
static inline int rta_get_mpls(struct rtattr *a, u32 *stack)
458
{
459
  if (RTA_PAYLOAD(a) % 4)
460
    log(L_WARN "KRT: Strange length of received MPLS stack: %u", RTA_PAYLOAD(a));
461
462
  return mpls_get(RTA_DATA(a), RTA_PAYLOAD(a) & ~0x3, stack);
463
}
464
465 9fdf9d29 Ondrej Zajicek
struct rtattr *
466
nl_add_attr(struct nlmsghdr *h, uint bufsize, uint code, const void *data, uint dlen)
467 95616c82 Ondrej Zajicek
{
468 9fdf9d29 Ondrej Zajicek
  uint pos = NLMSG_ALIGN(h->nlmsg_len);
469
  uint len = RTA_LENGTH(dlen);
470 95616c82 Ondrej Zajicek
471
  if (pos + len > bufsize)
472
    bug("nl_add_attr: packet buffer overflow");
473 9fdf9d29 Ondrej Zajicek
474
  struct rtattr *a = (struct rtattr *)((char *)h + pos);
475 95616c82 Ondrej Zajicek
  a->rta_type = code;
476
  a->rta_len = len;
477
  h->nlmsg_len = pos + len;
478 9fdf9d29 Ondrej Zajicek
479
  if (dlen > 0)
480
    memcpy(RTA_DATA(a), data, dlen);
481
482
  return a;
483 95616c82 Ondrej Zajicek
}
484
485 d14f8c3c Jan Moskyto Matejka
static inline struct rtattr *
486
nl_open_attr(struct nlmsghdr *h, uint bufsize, uint code)
487
{
488
  return nl_add_attr(h, bufsize, code, NULL, 0);
489
}
490
491
static inline void
492
nl_close_attr(struct nlmsghdr *h, struct rtattr *a)
493
{
494
  a->rta_len = (void *)h + NLMSG_ALIGN(h->nlmsg_len) - (void *)a;
495
}
496
497
static inline void
498
nl_add_attr_u16(struct nlmsghdr *h, uint bufsize, int code, u16 data)
499
{
500
  nl_add_attr(h, bufsize, code, &data, 2);
501
}
502
503 95616c82 Ondrej Zajicek
static inline void
504 29a64162 Ondrej Zajicek (work)
nl_add_attr_u32(struct nlmsghdr *h, uint bufsize, int code, u32 data)
505 95616c82 Ondrej Zajicek
{
506
  nl_add_attr(h, bufsize, code, &data, 4);
507
}
508
509
static inline void
510 29a64162 Ondrej Zajicek (work)
nl_add_attr_ip4(struct nlmsghdr *h, uint bufsize, int code, ip4_addr ip4)
511 95616c82 Ondrej Zajicek
{
512 29a64162 Ondrej Zajicek (work)
  ip4 = ip4_hton(ip4);
513
  nl_add_attr(h, bufsize, code, &ip4, sizeof(ip4));
514
}
515
516
static inline void
517
nl_add_attr_ip6(struct nlmsghdr *h, uint bufsize, int code, ip6_addr ip6)
518
{
519
  ip6 = ip6_hton(ip6);
520
  nl_add_attr(h, bufsize, code, &ip6, sizeof(ip6));
521
}
522
523
static inline void
524
nl_add_attr_ipa(struct nlmsghdr *h, uint bufsize, int code, ip_addr ipa)
525
{
526
  if (ipa_is_ip4(ipa))
527
    nl_add_attr_ip4(h, bufsize, code, ipa_to_ip4(ipa));
528 9b136840 Jan Moskyto Matejka
  else
529 29a64162 Ondrej Zajicek (work)
    nl_add_attr_ip6(h, bufsize, code, ipa_to_ip6(ipa));
530 95616c82 Ondrej Zajicek
}
531
532 d14f8c3c Jan Moskyto Matejka
static inline void
533
nl_add_attr_mpls(struct nlmsghdr *h, uint bufsize, int code, int len, u32 *stack)
534 9fdf9d29 Ondrej Zajicek
{
535 d14f8c3c Jan Moskyto Matejka
  char buf[len*4];
536
  mpls_put(buf, len, stack);
537
  nl_add_attr(h, bufsize, code, buf, len*4);
538 9fdf9d29 Ondrej Zajicek
}
539 95616c82 Ondrej Zajicek
540
static inline void
541 d14f8c3c Jan Moskyto Matejka
nl_add_attr_mpls_encap(struct nlmsghdr *h, uint bufsize, int len, u32 *stack)
542 95616c82 Ondrej Zajicek
{
543 d14f8c3c Jan Moskyto Matejka
  nl_add_attr_u16(h, bufsize, RTA_ENCAP_TYPE, LWTUNNEL_ENCAP_MPLS);
544
545
  struct rtattr *nest = nl_open_attr(h, bufsize, RTA_ENCAP);
546
  nl_add_attr_mpls(h, bufsize, RTA_DST, len, stack);
547
  nl_close_attr(h, nest);
548
}
549
550
static inline void
551
nl_add_attr_via(struct nlmsghdr *h, uint bufsize, ip_addr ipa)
552
{
553
  struct rtattr *nest = nl_open_attr(h, bufsize, RTA_VIA);
554
  struct rtvia *via = RTA_DATA(nest);
555
556
  h->nlmsg_len += sizeof(*via);
557
558 62e64905 Ondrej Zajicek (work)
  if (ipa_is_ip4(ipa))
559
  {
560 d14f8c3c Jan Moskyto Matejka
    via->rtvia_family = AF_INET;
561 62e64905 Ondrej Zajicek (work)
    put_ip4(via->rtvia_addr, ipa_to_ip4(ipa));
562
    h->nlmsg_len += sizeof(ip4_addr);
563
  }
564
  else
565
  {
566 d14f8c3c Jan Moskyto Matejka
    via->rtvia_family = AF_INET6;
567 62e64905 Ondrej Zajicek (work)
    put_ip6(via->rtvia_addr, ipa_to_ip6(ipa));
568
    h->nlmsg_len += sizeof(ip6_addr);
569 d14f8c3c Jan Moskyto Matejka
  }
570
571
  nl_close_attr(h, nest);
572 95616c82 Ondrej Zajicek
}
573
574 9fdf9d29 Ondrej Zajicek
static inline struct rtnexthop *
575
nl_open_nexthop(struct nlmsghdr *h, uint bufsize)
576
{
577
  uint pos = NLMSG_ALIGN(h->nlmsg_len);
578
  uint len = RTNH_LENGTH(0);
579
580
  if (pos + len > bufsize)
581
    bug("nl_open_nexthop: packet buffer overflow");
582
583
  h->nlmsg_len = pos + len;
584
585
  return (void *)h + pos;
586
}
587
588
static inline void
589
nl_close_nexthop(struct nlmsghdr *h, struct rtnexthop *nh)
590
{
591
  nh->rtnh_len = (void *)h + NLMSG_ALIGN(h->nlmsg_len) - (void *)nh;
592
}
593 95616c82 Ondrej Zajicek
594 d14f8c3c Jan Moskyto Matejka
static inline void
595
nl_add_nexthop(struct nlmsghdr *h, uint bufsize, struct nexthop *nh, int af)
596
{
597
  if (nh->labels > 0)
598
    if (af == AF_MPLS)
599
      nl_add_attr_mpls(h, bufsize, RTA_NEWDST, nh->labels, nh->label);
600
    else
601
      nl_add_attr_mpls_encap(h, bufsize, nh->labels, nh->label);
602
603
  if (ipa_nonzero(nh->gw))
604
    if (af == AF_MPLS)
605
      nl_add_attr_via(h, bufsize, nh->gw);
606
    else
607
      nl_add_attr_ipa(h, bufsize, RTA_GATEWAY, nh->gw);
608
}
609
610 95616c82 Ondrej Zajicek
static void
611 d14f8c3c Jan Moskyto Matejka
nl_add_multipath(struct nlmsghdr *h, uint bufsize, struct nexthop *nh, int af)
612 95616c82 Ondrej Zajicek
{
613 9fdf9d29 Ondrej Zajicek
  struct rtattr *a = nl_open_attr(h, bufsize, RTA_MULTIPATH);
614
615 95616c82 Ondrej Zajicek
  for (; nh; nh = nh->next)
616 9fdf9d29 Ondrej Zajicek
  {
617
    struct rtnexthop *rtnh = nl_open_nexthop(h, bufsize);
618 95616c82 Ondrej Zajicek
619 9fdf9d29 Ondrej Zajicek
    rtnh->rtnh_flags = 0;
620
    rtnh->rtnh_hops = nh->weight;
621
    rtnh->rtnh_ifindex = nh->iface->index;
622 95616c82 Ondrej Zajicek
623 d14f8c3c Jan Moskyto Matejka
    nl_add_nexthop(h, bufsize, nh, af);
624 95616c82 Ondrej Zajicek
625 a1f5e514 Ondrej Zajicek (work)
    if (nh->flags & RNF_ONLINK)
626
      rtnh->rtnh_flags |= RTNH_F_ONLINK;
627
628 9fdf9d29 Ondrej Zajicek
    nl_close_nexthop(h, rtnh);
629
  }
630
631
  nl_close_attr(h, a);
632
}
633 95616c82 Ondrej Zajicek
634 4e276a89 Jan Moskyto Matejka
static struct nexthop *
635 95616c82 Ondrej Zajicek
nl_parse_multipath(struct krt_proto *p, struct rtattr *ra)
636
{
637
  /* Temporary buffer for multicast nexthops */
638 4e276a89 Jan Moskyto Matejka
  static struct nexthop *nh_buffer;
639 95616c82 Ondrej Zajicek
  static int nh_buf_size;        /* in number of structures */
640
  static int nh_buf_used;
641
642 ad276157 Jan Moskyto Matejka
  struct rtattr *a[BIRD_RTA_MAX];
643 95616c82 Ondrej Zajicek
  struct rtnexthop *nh = RTA_DATA(ra);
644 4e276a89 Jan Moskyto Matejka
  struct nexthop *rv, *first, **last;
645 3e236955 Jan Moskyto Matejka
  unsigned len = RTA_PAYLOAD(ra);
646 95616c82 Ondrej Zajicek
647
  first = NULL;
648
  last = &first;
649
  nh_buf_used = 0;
650
651
  while (len)
652
    {
653
      /* Use RTNH_OK(nh,len) ?? */
654
      if ((len < sizeof(*nh)) || (len < nh->rtnh_len))
655
        return NULL;
656
657
      if (nh_buf_used == nh_buf_size)
658
      {
659
        nh_buf_size = nh_buf_size ? (nh_buf_size * 2) : 4;
660 d14f8c3c Jan Moskyto Matejka
        nh_buffer = xrealloc(nh_buffer, nh_buf_size * NEXTHOP_MAX_SIZE);
661 95616c82 Ondrej Zajicek
      }
662
      *last = rv = nh_buffer + nh_buf_used++;
663
      rv->next = NULL;
664
      last = &(rv->next);
665
666 a1f5e514 Ondrej Zajicek (work)
      rv->flags = 0;
667 95616c82 Ondrej Zajicek
      rv->weight = nh->rtnh_hops;
668
      rv->iface = if_find_by_index(nh->rtnh_ifindex);
669
      if (!rv->iface)
670
        return NULL;
671
672
      /* Nonexistent RTNH_PAYLOAD ?? */
673
      nl_attr_len = nh->rtnh_len - RTNH_LENGTH(0);
674 4e276a89 Jan Moskyto Matejka
      nl_parse_attrs(RTNH_DATA(nh), nexthop_attr_want4, a, sizeof(a));
675 95616c82 Ondrej Zajicek
      if (a[RTA_GATEWAY])
676
        {
677 23c212e7 Ondrej Zajicek (work)
          rv->gw = rta_get_ipa(a[RTA_GATEWAY]);
678 95616c82 Ondrej Zajicek
679 a1f5e514 Ondrej Zajicek (work)
          if (nh->rtnh_flags & RTNH_F_ONLINK)
680
            rv->flags |= RNF_ONLINK;
681
682 23c212e7 Ondrej Zajicek (work)
          neighbor *nbr;
683
          nbr = neigh_find2(&p->p, &rv->gw, rv->iface,
684 a1f5e514 Ondrej Zajicek (work)
                            (rv->flags & RNF_ONLINK) ? NEF_ONLINK : 0);
685 23c212e7 Ondrej Zajicek (work)
          if (!nbr || (nbr->scope == SCOPE_HOST))
686 95616c82 Ondrej Zajicek
            return NULL;
687
        }
688
      else
689 d14f8c3c Jan Moskyto Matejka
        rv->gw = IPA_NONE;
690 62e64905 Ondrej Zajicek (work)
691 d14f8c3c Jan Moskyto Matejka
      if (a[RTA_ENCAP_TYPE])
692
        {
693
          if (rta_get_u16(a[RTA_ENCAP_TYPE]) != LWTUNNEL_ENCAP_MPLS) {
694
            log(L_WARN "KRT: Unknown encapsulation method %d in multipath", rta_get_u16(a[RTA_ENCAP_TYPE]));
695
            return NULL;
696
          }
697
698
          struct rtattr *enca[BIRD_RTA_MAX];
699
          nl_attr_len = RTA_PAYLOAD(a[RTA_ENCAP]);
700
          nl_parse_attrs(RTA_DATA(a[RTA_ENCAP]), encap_mpls_want, enca, sizeof(enca));
701
          rv->labels = rta_get_mpls(enca[RTA_DST], rv->label);
702
          break;
703
        }
704
705 95616c82 Ondrej Zajicek
706
      len -= NLMSG_ALIGN(nh->rtnh_len);
707
      nh = RTNH_NEXT(nh);
708
    }
709
710
  return first;
711
}
712
713 9fdf9d29 Ondrej Zajicek
static void
714
nl_add_metrics(struct nlmsghdr *h, uint bufsize, u32 *metrics, int max)
715
{
716
  struct rtattr *a = nl_open_attr(h, bufsize, RTA_METRICS);
717
  int t;
718
719
  for (t = 1; t < max; t++)
720
    if (metrics[0] & (1 << t))
721
      nl_add_attr_u32(h, bufsize, t, metrics[t]);
722
723
  nl_close_attr(h, a);
724
}
725
726
static int
727
nl_parse_metrics(struct rtattr *hdr, u32 *metrics, int max)
728
{
729
  struct rtattr *a = RTA_DATA(hdr);
730
  int len = RTA_PAYLOAD(hdr);
731
732
  metrics[0] = 0;
733
  for (; RTA_OK(a, len); a = RTA_NEXT(a, len))
734
  {
735
    if (a->rta_type == RTA_UNSPEC)
736
      continue;
737
738
    if (a->rta_type >= max)
739
      continue;
740
741
    if (RTA_PAYLOAD(a) != 4)
742
      return -1;
743
744
    metrics[0] |= 1 << a->rta_type;
745 acb04cfd Ondrej Zajicek (work)
    metrics[a->rta_type] = rta_get_u32(a);
746 9fdf9d29 Ondrej Zajicek
  }
747
748
  if (len > 0)
749
    return -1;
750
751
  return 0;
752
}
753
754 95616c82 Ondrej Zajicek
755
/*
756
 *        Scanning of interfaces
757
 */
758
759
static void
760
nl_parse_link(struct nlmsghdr *h, int scan)
761
{
762
  struct ifinfomsg *i;
763 ad276157 Jan Moskyto Matejka
  struct rtattr *a[BIRD_IFLA_MAX];
764 95616c82 Ondrej Zajicek
  int new = h->nlmsg_type == RTM_NEWLINK;
765
  struct iface f = {};
766
  struct iface *ifi;
767
  char *name;
768
  u32 mtu;
769 ae80a2de Pavel Tvrdík
  uint fl;
770 95616c82 Ondrej Zajicek
771 ad276157 Jan Moskyto Matejka
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), ifla_attr_want, a, sizeof(a)))
772 95616c82 Ondrej Zajicek
    return;
773 ad276157 Jan Moskyto Matejka
  if (!a[IFLA_IFNAME] || (RTA_PAYLOAD(a[IFLA_IFNAME]) < 2) || !a[IFLA_MTU])
774 95616c82 Ondrej Zajicek
    {
775 ad276157 Jan Moskyto Matejka
      /*
776
       * IFLA_IFNAME and IFLA_MTU are required, in fact, but there may also come
777
       * a message with IFLA_WIRELESS set, where (e.g.) no IFLA_IFNAME exists.
778
       * We simply ignore all such messages with IFLA_WIRELESS without notice.
779
       */
780
781
      if (a[IFLA_WIRELESS])
782
        return;
783
784
      log(L_ERR "KIF: Malformed message received");
785 95616c82 Ondrej Zajicek
      return;
786
    }
787 ad276157 Jan Moskyto Matejka
788 95616c82 Ondrej Zajicek
  name = RTA_DATA(a[IFLA_IFNAME]);
789 acb04cfd Ondrej Zajicek (work)
  mtu = rta_get_u32(a[IFLA_MTU]);
790 95616c82 Ondrej Zajicek
791
  ifi = if_find_by_index(i->ifi_index);
792
  if (!new)
793
    {
794
      DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name);
795
      if (!ifi)
796
        return;
797
798
      if_delete(ifi);
799
    }
800
  else
801
    {
802
      DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags);
803
      if (ifi && strncmp(ifi->name, name, sizeof(ifi->name)-1))
804
        if_delete(ifi);
805
806
      strncpy(f.name, name, sizeof(f.name)-1);
807
      f.index = i->ifi_index;
808
      f.mtu = mtu;
809
810
      fl = i->ifi_flags;
811
      if (fl & IFF_UP)
812
        f.flags |= IF_ADMIN_UP;
813
      if (fl & IFF_LOWER_UP)
814
        f.flags |= IF_LINK_UP;
815
      if (fl & IFF_LOOPBACK)                /* Loopback */
816
        f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE;
817
      else if (fl & IFF_POINTOPOINT)        /* PtP */
818
        f.flags |= IF_MULTICAST;
819
      else if (fl & IFF_BROADCAST)        /* Broadcast */
820
        f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST;
821
      else
822
        f.flags |= IF_MULTIACCESS;        /* NBMA */
823 3216eb03 Ondrej Zajicek
824 16a3254c Ondrej Zajicek
      if (fl & IFF_MULTICAST)
825
        f.flags |= IF_MULTICAST;
826
827 3216eb03 Ondrej Zajicek
      ifi = if_update(&f);
828
829
      if (!scan)
830
        if_end_partial_update(ifi);
831 95616c82 Ondrej Zajicek
    }
832
}
833
834
static void
835 9b136840 Jan Moskyto Matejka
nl_parse_addr4(struct ifaddrmsg *i, int scan, int new)
836 95616c82 Ondrej Zajicek
{
837 ad276157 Jan Moskyto Matejka
  struct rtattr *a[BIRD_IFA_MAX];
838 95616c82 Ondrej Zajicek
  struct iface *ifi;
839 e37d2e3e Ondrej Zajicek (work)
  u32 ifa_flags;
840 95616c82 Ondrej Zajicek
  int scope;
841
842 9b136840 Jan Moskyto Matejka
  if (!nl_parse_attrs(IFA_RTA(i), ifa_attr_want4, a, sizeof(a)))
843 95616c82 Ondrej Zajicek
    return;
844 ad276157 Jan Moskyto Matejka
845 9b136840 Jan Moskyto Matejka
  if (!a[IFA_LOCAL])
846 ad276157 Jan Moskyto Matejka
    {
847 9b136840 Jan Moskyto Matejka
      log(L_ERR "KIF: Malformed message received (missing IFA_LOCAL)");
848
      return;
849 ad276157 Jan Moskyto Matejka
    }
850
  if (!a[IFA_ADDRESS])
851 95616c82 Ondrej Zajicek
    {
852 ad276157 Jan Moskyto Matejka
      log(L_ERR "KIF: Malformed message received (missing IFA_ADDRESS)");
853 95616c82 Ondrej Zajicek
      return;
854
    }
855
856
  ifi = if_find_by_index(i->ifa_index);
857
  if (!ifi)
858
    {
859
      log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index);
860
      return;
861
    }
862
863 e37d2e3e Ondrej Zajicek (work)
  if (a[IFA_FLAGS])
864
    ifa_flags = rta_get_u32(a[IFA_FLAGS]);
865
  else
866
    ifa_flags = i->ifa_flags;
867
868 9b136840 Jan Moskyto Matejka
  struct ifa ifa;
869 95616c82 Ondrej Zajicek
  bzero(&ifa, sizeof(ifa));
870
  ifa.iface = ifi;
871 cc5b93f7 Ondrej Zajicek (work)
  if (ifa_flags & IFA_F_SECONDARY)
872 95616c82 Ondrej Zajicek
    ifa.flags |= IA_SECONDARY;
873
874 9b136840 Jan Moskyto Matejka
  ifa.ip = rta_get_ipa(a[IFA_LOCAL]);
875
876 d7661fbe Jan Moskyto Matejka
  if (i->ifa_prefixlen > IP4_MAX_PREFIX_LENGTH)
877 95616c82 Ondrej Zajicek
    {
878
      log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen);
879
      new = 0;
880
    }
881 d7661fbe Jan Moskyto Matejka
  if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH)
882 95616c82 Ondrej Zajicek
    {
883 9b136840 Jan Moskyto Matejka
      ifa.brd = rta_get_ipa(a[IFA_ADDRESS]);
884
      net_fill_ip4(&ifa.prefix, rta_get_ip4(a[IFA_ADDRESS]), i->ifa_prefixlen);
885 95616c82 Ondrej Zajicek
886
      /* It is either a host address or a peer address */
887 9b136840 Jan Moskyto Matejka
      if (ipa_equal(ifa.ip, ifa.brd))
888 95616c82 Ondrej Zajicek
        ifa.flags |= IA_HOST;
889
      else
890
        {
891
          ifa.flags |= IA_PEER;
892 9b136840 Jan Moskyto Matejka
          ifa.opposite = ifa.brd;
893 95616c82 Ondrej Zajicek
        }
894
    }
895
  else
896
    {
897 9b136840 Jan Moskyto Matejka
      net_fill_ip4(&ifa.prefix, ipa_to_ip4(ifa.ip), i->ifa_prefixlen);
898
      net_normalize(&ifa.prefix);
899
900 d7661fbe Jan Moskyto Matejka
      if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH - 1)
901 95616c82 Ondrej Zajicek
        ifa.opposite = ipa_opposite_m1(ifa.ip);
902
903 d7661fbe Jan Moskyto Matejka
      if (i->ifa_prefixlen == IP4_MAX_PREFIX_LENGTH - 2)
904 95616c82 Ondrej Zajicek
        ifa.opposite = ipa_opposite_m2(ifa.ip);
905
906
      if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST])
907
        {
908 9b136840 Jan Moskyto Matejka
          ip4_addr xbrd = rta_get_ip4(a[IFA_BROADCAST]);
909
          ip4_addr ybrd = ip4_or(ipa_to_ip4(ifa.ip), ip4_not(ip4_mkmask(i->ifa_prefixlen)));
910
911
          if (ip4_equal(xbrd, net4_prefix(&ifa.prefix)) || ip4_equal(xbrd, ybrd))
912
            ifa.brd = ipa_from_ip4(xbrd);
913 95616c82 Ondrej Zajicek
          else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */
914 9b136840 Jan Moskyto Matejka
            {
915 e691d16a Ondrej Zajicek (work)
              log(L_ERR "KIF: Invalid broadcast address %I4 for %s", xbrd, ifi->name);
916 9b136840 Jan Moskyto Matejka
              ifa.brd = ipa_from_ip4(ybrd);
917
            }
918
        }
919
    }
920
921
  scope = ipa_classify(ifa.ip);
922
  if (scope < 0)
923
    {
924
      log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name);
925
      return;
926
    }
927
  ifa.scope = scope & IADDR_SCOPE_MASK;
928
929
  DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %N, brd %I, opp %I\n",
930
      ifi->index, ifi->name,
931
      new ? "added" : "removed",
932
      ifa.ip, ifa.flags, ifa.prefix, ifa.brd, ifa.opposite);
933
934
  if (new)
935
    ifa_update(&ifa);
936
  else
937
    ifa_delete(&ifa);
938
939
  if (!scan)
940
    if_end_partial_update(ifi);
941
}
942
943
static void
944
nl_parse_addr6(struct ifaddrmsg *i, int scan, int new)
945
{
946
  struct rtattr *a[BIRD_IFA_MAX];
947
  struct iface *ifi;
948 cc5b93f7 Ondrej Zajicek (work)
  u32 ifa_flags;
949 9b136840 Jan Moskyto Matejka
  int scope;
950
951
  if (!nl_parse_attrs(IFA_RTA(i), ifa_attr_want6, a, sizeof(a)))
952
    return;
953
954
  if (!a[IFA_ADDRESS])
955
    {
956
      log(L_ERR "KIF: Malformed message received (missing IFA_ADDRESS)");
957
      return;
958
    }
959
960
  ifi = if_find_by_index(i->ifa_index);
961
  if (!ifi)
962
    {
963
      log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index);
964
      return;
965
    }
966
967 cc5b93f7 Ondrej Zajicek (work)
  if (a[IFA_FLAGS])
968
    ifa_flags = rta_get_u32(a[IFA_FLAGS]);
969
  else
970
    ifa_flags = i->ifa_flags;
971
972 9b136840 Jan Moskyto Matejka
  struct ifa ifa;
973
  bzero(&ifa, sizeof(ifa));
974
  ifa.iface = ifi;
975 e37d2e3e Ondrej Zajicek (work)
  if (ifa_flags & IFA_F_SECONDARY)
976 9b136840 Jan Moskyto Matejka
    ifa.flags |= IA_SECONDARY;
977
978 e37d2e3e Ondrej Zajicek (work)
  /* Ignore tentative addresses silently */
979
  if (ifa_flags & IFA_F_TENTATIVE)
980
    return;
981 9b136840 Jan Moskyto Matejka
982 95616c82 Ondrej Zajicek
  /* IFA_LOCAL can be unset for IPv6 interfaces */
983 9b136840 Jan Moskyto Matejka
  ifa.ip = rta_get_ipa(a[IFA_LOCAL] ? : a[IFA_ADDRESS]);
984
985 d7661fbe Jan Moskyto Matejka
  if (i->ifa_prefixlen > IP6_MAX_PREFIX_LENGTH)
986 9b136840 Jan Moskyto Matejka
    {
987
      log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen);
988
      new = 0;
989
    }
990 d7661fbe Jan Moskyto Matejka
  if (i->ifa_prefixlen == IP6_MAX_PREFIX_LENGTH)
991 9b136840 Jan Moskyto Matejka
    {
992
      ifa.brd = rta_get_ipa(a[IFA_ADDRESS]);
993
      net_fill_ip6(&ifa.prefix, rta_get_ip6(a[IFA_ADDRESS]), i->ifa_prefixlen);
994
995
      /* It is either a host address or a peer address */
996
      if (ipa_equal(ifa.ip, ifa.brd))
997
        ifa.flags |= IA_HOST;
998
      else
999
        {
1000
          ifa.flags |= IA_PEER;
1001
          ifa.opposite = ifa.brd;
1002 95616c82 Ondrej Zajicek
        }
1003 9b136840 Jan Moskyto Matejka
    }
1004
  else
1005
    {
1006
      net_fill_ip6(&ifa.prefix, ipa_to_ip6(ifa.ip), i->ifa_prefixlen);
1007
      net_normalize(&ifa.prefix);
1008
1009 d7661fbe Jan Moskyto Matejka
      if (i->ifa_prefixlen == IP6_MAX_PREFIX_LENGTH - 1)
1010 9b136840 Jan Moskyto Matejka
        ifa.opposite = ipa_opposite_m1(ifa.ip);
1011 95616c82 Ondrej Zajicek
    }
1012
1013
  scope = ipa_classify(ifa.ip);
1014
  if (scope < 0)
1015
    {
1016
      log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name);
1017
      return;
1018
    }
1019
  ifa.scope = scope & IADDR_SCOPE_MASK;
1020
1021 9b136840 Jan Moskyto Matejka
  DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %N, brd %I, opp %I\n",
1022 95616c82 Ondrej Zajicek
      ifi->index, ifi->name,
1023
      new ? "added" : "removed",
1024 9b136840 Jan Moskyto Matejka
      ifa.ip, ifa.flags, ifa.prefix, ifa.brd, ifa.opposite);
1025 3216eb03 Ondrej Zajicek
1026 95616c82 Ondrej Zajicek
  if (new)
1027
    ifa_update(&ifa);
1028
  else
1029
    ifa_delete(&ifa);
1030 3216eb03 Ondrej Zajicek
1031
  if (!scan)
1032
    if_end_partial_update(ifi);
1033 95616c82 Ondrej Zajicek
}
1034
1035 9b136840 Jan Moskyto Matejka
static void
1036
nl_parse_addr(struct nlmsghdr *h, int scan)
1037
{
1038
  struct ifaddrmsg *i;
1039
1040
  if (!(i = nl_checkin(h, sizeof(*i))))
1041
    return;
1042
1043
  int new = (h->nlmsg_type == RTM_NEWADDR);
1044
1045
  switch (i->ifa_family)
1046
    {
1047
      case AF_INET:
1048
        return nl_parse_addr4(i, scan, new);
1049 29a64162 Ondrej Zajicek (work)
1050 9b136840 Jan Moskyto Matejka
      case AF_INET6:
1051
        return nl_parse_addr6(i, scan, new);
1052
    }
1053
}
1054
1055 95616c82 Ondrej Zajicek
void
1056
kif_do_scan(struct kif_proto *p UNUSED)
1057
{
1058
  struct nlmsghdr *h;
1059
1060
  if_start_update();
1061
1062 86c3eea0 Ondrej Zajicek
  nl_request_dump(AF_UNSPEC, RTM_GETLINK);
1063 95616c82 Ondrej Zajicek
  while (h = nl_get_scan())
1064
    if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)
1065
      nl_parse_link(h, 1);
1066
    else
1067
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
1068 29a64162 Ondrej Zajicek (work)
1069 d7661fbe Jan Moskyto Matejka
  nl_request_dump(AF_INET, RTM_GETADDR);
1070 95616c82 Ondrej Zajicek
  while (h = nl_get_scan())
1071
    if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
1072 3216eb03 Ondrej Zajicek
      nl_parse_addr(h, 1);
1073 95616c82 Ondrej Zajicek
    else
1074
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
1075 29a64162 Ondrej Zajicek (work)
1076 d7661fbe Jan Moskyto Matejka
  nl_request_dump(AF_INET6, RTM_GETADDR);
1077
  while (h = nl_get_scan())
1078
    if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
1079
      nl_parse_addr(h, 1);
1080
    else
1081
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
1082 29a64162 Ondrej Zajicek (work)
1083 95616c82 Ondrej Zajicek
  if_end_update();
1084
}
1085
1086
/*
1087
 *        Routes
1088
 */
1089
1090 9ddbfbdd Jan Moskyto Matejka
static inline u32
1091
krt_table_id(struct krt_proto *p)
1092
{
1093
  return KRT_CF->sys.table_id;
1094
}
1095
1096
static HASH(struct krt_proto) nl_table_map;
1097
1098 29a64162 Ondrej Zajicek (work)
#define RTH_KEY(p)                p->af, krt_table_id(p)
1099
#define RTH_NEXT(p)                p->sys.hash_next
1100
#define RTH_EQ(a1,i1,a2,i2)        a1 == a2 && i1 == i2
1101
#define RTH_FN(a,i)                a ^ u32_hash(i)
1102 9ddbfbdd Jan Moskyto Matejka
1103
#define RTH_REHASH                rth_rehash
1104
#define RTH_PARAMS                /8, *2, 2, 2, 6, 20
1105
1106
HASH_DEFINE_REHASH_FN(RTH, struct krt_proto)
1107 95616c82 Ondrej Zajicek
1108
int
1109
krt_capable(rte *e)
1110
{
1111
  rta *a = e->attrs;
1112
1113
  switch (a->dest)
1114 62e64905 Ondrej Zajicek (work)
  {
1115 4e276a89 Jan Moskyto Matejka
    case RTD_UNICAST:
1116 95616c82 Ondrej Zajicek
    case RTD_BLACKHOLE:
1117
    case RTD_UNREACHABLE:
1118
    case RTD_PROHIBIT:
1119 62e64905 Ondrej Zajicek (work)
      return 1;
1120
1121 95616c82 Ondrej Zajicek
    default:
1122
      return 0;
1123 62e64905 Ondrej Zajicek (work)
  }
1124 95616c82 Ondrej Zajicek
}
1125
1126
static inline int
1127 4e276a89 Jan Moskyto Matejka
nh_bufsize(struct nexthop *nh)
1128 95616c82 Ondrej Zajicek
{
1129
  int rv = 0;
1130
  for (; nh != NULL; nh = nh->next)
1131 9fdf9d29 Ondrej Zajicek
    rv += RTNH_LENGTH(RTA_LENGTH(sizeof(ip_addr)));
1132 95616c82 Ondrej Zajicek
  return rv;
1133
}
1134
1135
static int
1136 4e276a89 Jan Moskyto Matejka
nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int op, int dest, struct nexthop *nh)
1137 95616c82 Ondrej Zajicek
{
1138
  eattr *ea;
1139
  net *net = e->net;
1140
  rta *a = e->attrs;
1141 4e276a89 Jan Moskyto Matejka
  int bufsize = 128 + KRT_METRICS_MAX*8 + nh_bufsize(&(a->nh));
1142 4adcb9df Ondrej Zajicek (work)
  u32 priority = 0;
1143 a8caff32 Jan Moskyto Matejka
1144 95616c82 Ondrej Zajicek
  struct {
1145
    struct nlmsghdr h;
1146
    struct rtmsg r;
1147 a8caff32 Jan Moskyto Matejka
    char buf[0];
1148
  } *r;
1149
1150
  int rsize = sizeof(*r) + bufsize;
1151
  r = alloca(rsize);
1152 95616c82 Ondrej Zajicek
1153 cc5b93f7 Ondrej Zajicek (work)
  DBG("nl_send_route(%N,op=%x)\n", net->n.addr, op);
1154 95616c82 Ondrej Zajicek
1155 a8caff32 Jan Moskyto Matejka
  bzero(&r->h, sizeof(r->h));
1156
  bzero(&r->r, sizeof(r->r));
1157 cc5b93f7 Ondrej Zajicek (work)
  r->h.nlmsg_type = op ? RTM_NEWROUTE : RTM_DELROUTE;
1158 a8caff32 Jan Moskyto Matejka
  r->h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1159 cc5b93f7 Ondrej Zajicek (work)
  r->h.nlmsg_flags = op | NLM_F_REQUEST | NLM_F_ACK;
1160 95616c82 Ondrej Zajicek
1161 a8caff32 Jan Moskyto Matejka
  r->r.rtm_family = p->af;
1162
  r->r.rtm_dst_len = net_pxlen(net->n.addr);
1163
  r->r.rtm_protocol = RTPROT_BIRD;
1164 7074be22 Ondrej Zajicek (work)
  r->r.rtm_scope = RT_SCOPE_NOWHERE;
1165 d14f8c3c Jan Moskyto Matejka
  if (p->af == AF_MPLS)
1166
  {
1167
    u32 label = net_mpls(net->n.addr);
1168
    nl_add_attr_mpls(&r->h, rsize, RTA_DST, 1, &label);
1169
  }
1170
  else
1171
    nl_add_attr_ipa(&r->h, rsize, RTA_DST, net_prefix(net->n.addr));
1172 95616c82 Ondrej Zajicek
1173 2feaa693 Ondrej Zajicek (work)
  /*
1174
   * Strange behavior for RTM_DELROUTE:
1175
   * 1) rtm_family is ignored in IPv6, works for IPv4
1176
   * 2) not setting RTA_PRIORITY is different from setting default value (on IPv6)
1177
   * 3) not setting RTA_PRIORITY is equivalent to setting 0, which is wildcard
1178
   */
1179
1180 9ddbfbdd Jan Moskyto Matejka
  if (krt_table_id(p) < 256)
1181 a8caff32 Jan Moskyto Matejka
    r->r.rtm_table = krt_table_id(p);
1182 9ddbfbdd Jan Moskyto Matejka
  else
1183 a8caff32 Jan Moskyto Matejka
    nl_add_attr_u32(&r->h, rsize, RTA_TABLE, krt_table_id(p));
1184 9ddbfbdd Jan Moskyto Matejka
1185 4adcb9df Ondrej Zajicek (work)
  if (a->source == RTS_DUMMY)
1186
    priority = e->u.krt.metric;
1187
  else if (KRT_CF->sys.metric)
1188
    priority = KRT_CF->sys.metric;
1189
  else if ((op != NL_OP_DELETE) && (ea = ea_find(eattrs, EA_KRT_METRIC)))
1190
    priority = ea->u.data;
1191 78a2cc28 Ondrej Zajicek
1192 4adcb9df Ondrej Zajicek (work)
  if (priority)
1193 d1b8fe93 Ondrej Zajicek (work)
    nl_add_attr_u32(&r->h, rsize, RTA_PRIORITY, priority);
1194 78a2cc28 Ondrej Zajicek
1195 2feaa693 Ondrej Zajicek (work)
  /* For route delete, we do not specify remaining route attributes */
1196
  if (op == NL_OP_DELETE)
1197
    goto dest;
1198 78a2cc28 Ondrej Zajicek
1199 6e75d0d2 Ondrej Zajicek (work)
  /* Default scope is LINK for device routes, UNIVERSE otherwise */
1200
  if (ea = ea_find(eattrs, EA_KRT_SCOPE))
1201 cc5b93f7 Ondrej Zajicek (work)
    r->r.rtm_scope = ea->u.data;
1202 6e75d0d2 Ondrej Zajicek (work)
  else
1203 4e276a89 Jan Moskyto Matejka
    r->r.rtm_scope = (dest == RTD_UNICAST && ipa_zero(nh->gw)) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
1204 95616c82 Ondrej Zajicek
1205
  if (ea = ea_find(eattrs, EA_KRT_PREFSRC))
1206 a8caff32 Jan Moskyto Matejka
    nl_add_attr_ipa(&r->h, rsize, RTA_PREFSRC, *(ip_addr *)ea->u.ptr->data);
1207 95616c82 Ondrej Zajicek
1208
  if (ea = ea_find(eattrs, EA_KRT_REALM))
1209 a8caff32 Jan Moskyto Matejka
    nl_add_attr_u32(&r->h, rsize, RTA_FLOW, ea->u.data);
1210 95616c82 Ondrej Zajicek
1211 9fdf9d29 Ondrej Zajicek
1212
  u32 metrics[KRT_METRICS_MAX];
1213
  metrics[0] = 0;
1214
1215
  struct ea_walk_state ews = { .eattrs = eattrs };
1216
  while (ea = ea_walk(&ews, EA_KRT_METRICS, KRT_METRICS_MAX))
1217
  {
1218
    int id = ea->id - EA_KRT_METRICS;
1219
    metrics[0] |= 1 << id;
1220
    metrics[id] = ea->u.data;
1221
  }
1222
1223
  if (metrics[0])
1224 a8caff32 Jan Moskyto Matejka
    nl_add_metrics(&r->h, rsize, metrics, KRT_METRICS_MAX);
1225 9fdf9d29 Ondrej Zajicek
1226
1227 2feaa693 Ondrej Zajicek (work)
dest:
1228
  switch (dest)
1229 95616c82 Ondrej Zajicek
    {
1230 4e276a89 Jan Moskyto Matejka
    case RTD_UNICAST:
1231 a8caff32 Jan Moskyto Matejka
      r->r.rtm_type = RTN_UNICAST;
1232 4e276a89 Jan Moskyto Matejka
      if (nh->next && !krt_ecmp6(p))
1233 d14f8c3c Jan Moskyto Matejka
        nl_add_multipath(&r->h, rsize, nh, p->af);
1234 4e276a89 Jan Moskyto Matejka
      else
1235
      {
1236
        nl_add_attr_u32(&r->h, rsize, RTA_OIF, nh->iface->index);
1237 d14f8c3c Jan Moskyto Matejka
        nl_add_nexthop(&r->h, rsize, nh, p->af);
1238 a1f5e514 Ondrej Zajicek (work)
1239
        if (nh->flags & RNF_ONLINK)
1240
          r->r.rtm_flags |= RTNH_F_ONLINK;
1241 4e276a89 Jan Moskyto Matejka
      }
1242 95616c82 Ondrej Zajicek
      break;
1243
    case RTD_BLACKHOLE:
1244 a8caff32 Jan Moskyto Matejka
      r->r.rtm_type = RTN_BLACKHOLE;
1245 95616c82 Ondrej Zajicek
      break;
1246
    case RTD_UNREACHABLE:
1247 a8caff32 Jan Moskyto Matejka
      r->r.rtm_type = RTN_UNREACHABLE;
1248 95616c82 Ondrej Zajicek
      break;
1249
    case RTD_PROHIBIT:
1250 a8caff32 Jan Moskyto Matejka
      r->r.rtm_type = RTN_PROHIBIT;
1251 95616c82 Ondrej Zajicek
      break;
1252 2feaa693 Ondrej Zajicek (work)
    case RTD_NONE:
1253
      break;
1254 95616c82 Ondrej Zajicek
    default:
1255
      bug("krt_capable inconsistent with nl_send_route");
1256
    }
1257
1258 2feaa693 Ondrej Zajicek (work)
  /* Ignore missing for DELETE */
1259 cc5b93f7 Ondrej Zajicek (work)
  return nl_exchange(&r->h, (op == NL_OP_DELETE));
1260 2feaa693 Ondrej Zajicek (work)
}
1261
1262
static inline int
1263
nl_add_rte(struct krt_proto *p, rte *e, struct ea_list *eattrs)
1264
{
1265
  rta *a = e->attrs;
1266
  int err = 0;
1267
1268 4e276a89 Jan Moskyto Matejka
  if (krt_ecmp6(p) && a->nh.next)
1269 2feaa693 Ondrej Zajicek (work)
  {
1270 4e276a89 Jan Moskyto Matejka
    struct nexthop *nh = &(a->nh);
1271 2feaa693 Ondrej Zajicek (work)
1272 4e276a89 Jan Moskyto Matejka
    err = nl_send_route(p, e, eattrs, NL_OP_ADD, RTD_UNICAST, nh);
1273 2feaa693 Ondrej Zajicek (work)
    if (err < 0)
1274
      return err;
1275
1276
    for (nh = nh->next; nh; nh = nh->next)
1277 4e276a89 Jan Moskyto Matejka
      err += nl_send_route(p, e, eattrs, NL_OP_APPEND, RTD_UNICAST, nh);
1278 2feaa693 Ondrej Zajicek (work)
1279
    return err;
1280
  }
1281
1282 4e276a89 Jan Moskyto Matejka
  return nl_send_route(p, e, eattrs, NL_OP_ADD, a->dest, &(a->nh));
1283 2feaa693 Ondrej Zajicek (work)
}
1284
1285
static inline int
1286
nl_delete_rte(struct krt_proto *p, rte *e, struct ea_list *eattrs)
1287
{
1288
  int err = 0;
1289
1290
  /* For IPv6, we just repeatedly request DELETE until we get error */
1291
  do
1292 4e276a89 Jan Moskyto Matejka
    err = nl_send_route(p, e, eattrs, NL_OP_DELETE, RTD_NONE, NULL);
1293 2feaa693 Ondrej Zajicek (work)
  while (krt_ecmp6(p) && !err);
1294
1295
  return err;
1296 95616c82 Ondrej Zajicek
}
1297
1298
void
1299
krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs)
1300
{
1301
  int err = 0;
1302
1303
  /*
1304 2feaa693 Ondrej Zajicek (work)
   * We could use NL_OP_REPLACE, but route replace on Linux has some problems:
1305
   *
1306
   * 1) Does not check for matching rtm_protocol
1307
   * 2) Has broken semantics for IPv6 ECMP
1308
   * 3) Crashes some kernel version when used for IPv6 ECMP
1309
   *
1310
   * So we use NL_OP_DELETE and then NL_OP_ADD. We also do not trust the old
1311
   * route value, so we do not try to optimize IPv6 ECMP reconfigurations.
1312 95616c82 Ondrej Zajicek
   */
1313
1314
  if (old)
1315 2feaa693 Ondrej Zajicek (work)
    nl_delete_rte(p, old, eattrs);
1316 95616c82 Ondrej Zajicek
1317
  if (new)
1318 2feaa693 Ondrej Zajicek (work)
    err = nl_add_rte(p, new, eattrs);
1319 95616c82 Ondrej Zajicek
1320
  if (err < 0)
1321
    n->n.flags |= KRF_SYNC_ERROR;
1322
  else
1323
    n->n.flags &= ~KRF_SYNC_ERROR;
1324
}
1325
1326
1327 4e276a89 Jan Moskyto Matejka
static inline struct nexthop *
1328
nl_alloc_nexthop(struct nl_parse_state *s, ip_addr gw, struct iface *iface, byte weight)
1329 2feaa693 Ondrej Zajicek (work)
{
1330 4e276a89 Jan Moskyto Matejka
  struct nexthop *nh = lp_alloc(s->pool, sizeof(struct nexthop));
1331 2feaa693 Ondrej Zajicek (work)
1332
  nh->gw = gw;
1333
  nh->iface = iface;
1334
  nh->next = NULL;
1335
  nh->weight = weight;
1336
1337
  return nh;
1338
}
1339
1340
static int
1341
nl_mergable_route(struct nl_parse_state *s, net *net, struct krt_proto *p, uint priority, uint krt_type)
1342
{
1343
  /* Route merging must be active */
1344
  if (!s->merge)
1345
    return 0;
1346
1347
  /* Saved and new route must have same network, proto/table, and priority */
1348
  if ((s->net != net) || (s->proto != p) || (s->krt_metric != priority))
1349
    return 0;
1350
1351
  /* Both must be regular unicast routes */
1352
  if ((s->krt_type != RTN_UNICAST) || (krt_type != RTN_UNICAST))
1353
    return 0;
1354
1355
  return 1;
1356
}
1357
1358
static void
1359
nl_announce_route(struct nl_parse_state *s)
1360
{
1361
  rte *e = rte_get_temp(s->attrs);
1362
  e->net = s->net;
1363
  e->u.krt.src = s->krt_src;
1364
  e->u.krt.proto = s->krt_proto;
1365
  e->u.krt.seen = 0;
1366
  e->u.krt.best = 0;
1367
  e->u.krt.metric = s->krt_metric;
1368
1369
  if (s->scan)
1370
    krt_got_route(s->proto, e);
1371
  else
1372
    krt_got_route_async(s->proto, e, s->new);
1373
1374
  s->net = NULL;
1375
  s->attrs = NULL;
1376
  s->proto = NULL;
1377
  lp_flush(s->pool);
1378
}
1379
1380
static inline void
1381
nl_parse_begin(struct nl_parse_state *s, int scan, int merge)
1382
{
1383
  memset(s, 0, sizeof (struct nl_parse_state));
1384
  s->pool = nl_linpool;
1385
  s->scan = scan;
1386
  s->merge = merge;
1387
}
1388
1389
static inline void
1390
nl_parse_end(struct nl_parse_state *s)
1391
{
1392
  if (s->net)
1393
    nl_announce_route(s);
1394
}
1395
1396
1397 95616c82 Ondrej Zajicek
#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
1398
1399
static void
1400 2feaa693 Ondrej Zajicek (work)
nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h)
1401 95616c82 Ondrej Zajicek
{
1402
  struct krt_proto *p;
1403
  struct rtmsg *i;
1404 ad276157 Jan Moskyto Matejka
  struct rtattr *a[BIRD_RTA_MAX];
1405 95616c82 Ondrej Zajicek
  int new = h->nlmsg_type == RTM_NEWROUTE;
1406
1407 29a64162 Ondrej Zajicek (work)
  net_addr dst;
1408 95616c82 Ondrej Zajicek
  u32 oif = ~0;
1409 29a64162 Ondrej Zajicek (work)
  u32 table_id;
1410 2feaa693 Ondrej Zajicek (work)
  u32 priority = 0;
1411 6e75d0d2 Ondrej Zajicek (work)
  u32 def_scope = RT_SCOPE_UNIVERSE;
1412 95616c82 Ondrej Zajicek
  int src;
1413
1414 ad276157 Jan Moskyto Matejka
  if (!(i = nl_checkin(h, sizeof(*i))))
1415 95616c82 Ondrej Zajicek
    return;
1416 ad276157 Jan Moskyto Matejka
1417
  switch (i->rtm_family)
1418 95616c82 Ondrej Zajicek
    {
1419 29a64162 Ondrej Zajicek (work)
    case AF_INET:
1420
      if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want4, a, sizeof(a)))
1421
        return;
1422
1423
      if (a[RTA_DST])
1424
        net_fill_ip4(&dst, rta_get_ip4(a[RTA_DST]), i->rtm_dst_len);
1425
      else
1426
        net_fill_ip4(&dst, IP4_NONE, 0);
1427
      break;
1428
1429 cc5b93f7 Ondrej Zajicek (work)
    case AF_INET6:
1430
      if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want6, a, sizeof(a)))
1431
        return;
1432 29a64162 Ondrej Zajicek (work)
1433
      if (a[RTA_DST])
1434
        net_fill_ip6(&dst, rta_get_ip6(a[RTA_DST]), i->rtm_dst_len);
1435
      else
1436
        net_fill_ip6(&dst, IP6_NONE, 0);
1437
      break;
1438
1439 d14f8c3c Jan Moskyto Matejka
    case AF_MPLS:
1440
      if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want_mpls, a, sizeof(a)))
1441
        return;
1442
1443 ed610044 Ondrej Zajicek (work)
      if (!a[RTA_DST])
1444
        SKIP("MPLS route without RTA_DST");
1445
1446
      if (rta_get_mpls(a[RTA_DST], rta_mpls_stack) != 1)
1447
        SKIP("MPLS route with multi-label RTA_DST");
1448
1449
      net_fill_mpls(&dst, rta_mpls_stack[0]);
1450 d14f8c3c Jan Moskyto Matejka
      break;
1451
1452 29a64162 Ondrej Zajicek (work)
    default:
1453
      return;
1454 95616c82 Ondrej Zajicek
    }
1455
1456
  if (a[RTA_OIF])
1457 acb04cfd Ondrej Zajicek (work)
    oif = rta_get_u32(a[RTA_OIF]);
1458 95616c82 Ondrej Zajicek
1459 9ddbfbdd Jan Moskyto Matejka
  if (a[RTA_TABLE])
1460 29a64162 Ondrej Zajicek (work)
    table_id = rta_get_u32(a[RTA_TABLE]);
1461 9ddbfbdd Jan Moskyto Matejka
  else
1462 29a64162 Ondrej Zajicek (work)
    table_id = i->rtm_table;
1463 9ddbfbdd Jan Moskyto Matejka
1464 29a64162 Ondrej Zajicek (work)
  /* Do we know this table? */
1465
  p = HASH_FIND(nl_table_map, RTH, i->rtm_family, table_id);
1466 95616c82 Ondrej Zajicek
  if (!p)
1467 9ddbfbdd Jan Moskyto Matejka
    SKIP("unknown table %d\n", table);
1468 95616c82 Ondrej Zajicek
1469
  if (a[RTA_IIF])
1470
    SKIP("IIF set\n");
1471 29a64162 Ondrej Zajicek (work)
1472 95616c82 Ondrej Zajicek
  if (i->rtm_tos != 0)                        /* We don't support TOS */
1473
    SKIP("TOS %02x\n", i->rtm_tos);
1474
1475 2feaa693 Ondrej Zajicek (work)
  if (s->scan && !new)
1476 95616c82 Ondrej Zajicek
    SKIP("RTM_DELROUTE in scan\n");
1477
1478 2feaa693 Ondrej Zajicek (work)
  if (a[RTA_PRIORITY])
1479
    priority = rta_get_u32(a[RTA_PRIORITY]);
1480
1481 9b136840 Jan Moskyto Matejka
  int c = net_classify(&dst);
1482 95616c82 Ondrej Zajicek
  if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
1483
    SKIP("strange class/scope\n");
1484
1485
  switch (i->rtm_protocol)
1486
    {
1487
    case RTPROT_UNSPEC:
1488
      SKIP("proto unspec\n");
1489
1490
    case RTPROT_REDIRECT:
1491
      src = KRT_SRC_REDIRECT;
1492
      break;
1493
1494
    case RTPROT_KERNEL:
1495
      src = KRT_SRC_KERNEL;
1496
      return;
1497
1498
    case RTPROT_BIRD:
1499 2feaa693 Ondrej Zajicek (work)
      if (!s->scan)
1500 95616c82 Ondrej Zajicek
        SKIP("echo\n");
1501
      src = KRT_SRC_BIRD;
1502
      break;
1503
1504
    case RTPROT_BOOT:
1505
    default:
1506
      src = KRT_SRC_ALIEN;
1507
    }
1508
1509 f4a60a9b Ondrej Zajicek (work)
  net *net = net_get(p->p.main_channel->table, &dst);
1510 95616c82 Ondrej Zajicek
1511 2feaa693 Ondrej Zajicek (work)
  if (s->net && !nl_mergable_route(s, net, p, priority, i->rtm_type))
1512
    nl_announce_route(s);
1513
1514 d14f8c3c Jan Moskyto Matejka
  rta *ra = lp_allocz(s->pool, RTA_MAX_SIZE);
1515 2feaa693 Ondrej Zajicek (work)
  ra->src = p->p.main_source;
1516
  ra->source = RTS_INHERIT;
1517
  ra->scope = SCOPE_UNIVERSE;
1518 95616c82 Ondrej Zajicek
1519
  switch (i->rtm_type)
1520
    {
1521
    case RTN_UNICAST:
1522 62e64905 Ondrej Zajicek (work)
      ra->dest = RTD_UNICAST;
1523 95616c82 Ondrej Zajicek
1524 ad276157 Jan Moskyto Matejka
      if (a[RTA_MULTIPATH] && (i->rtm_family == AF_INET))
1525 95616c82 Ondrej Zajicek
        {
1526 4e276a89 Jan Moskyto Matejka
          struct nexthop *nh = nl_parse_multipath(p, a[RTA_MULTIPATH]);
1527
          if (!nh)
1528 95616c82 Ondrej Zajicek
            {
1529 fe9f1a6d Ondrej Zajicek (work)
              log(L_ERR "KRT: Received strange multipath route %N", net->n.addr);
1530 95616c82 Ondrej Zajicek
              return;
1531
            }
1532 9fdf9d29 Ondrej Zajicek
1533 62e64905 Ondrej Zajicek (work)
          ra->nh = *nh;
1534 95616c82 Ondrej Zajicek
          break;
1535
        }
1536
1537 4e276a89 Jan Moskyto Matejka
      ra->nh.iface = if_find_by_index(oif);
1538
      if (!ra->nh.iface)
1539 95616c82 Ondrej Zajicek
        {
1540 fe9f1a6d Ondrej Zajicek (work)
          log(L_ERR "KRT: Received route %N with unknown ifindex %u", net->n.addr, oif);
1541 95616c82 Ondrej Zajicek
          return;
1542
        }
1543
1544 d14f8c3c Jan Moskyto Matejka
      if ((i->rtm_family != AF_MPLS) && a[RTA_GATEWAY] || (i->rtm_family == AF_MPLS) && a[RTA_VIA])
1545 95616c82 Ondrej Zajicek
        {
1546 d14f8c3c Jan Moskyto Matejka
          if (i->rtm_family == AF_MPLS)
1547
            ra->nh.gw = rta_get_via(a[RTA_VIA]);
1548
          else
1549
            ra->nh.gw = rta_get_ipa(a[RTA_GATEWAY]);
1550 95616c82 Ondrej Zajicek
1551
          /* Silently skip strange 6to4 routes */
1552 0bf95f99 Ondrej Zajicek (work)
          const net_addr_ip6 sit = NET_ADDR_IP6(IP6_NONE, 96);
1553 4e276a89 Jan Moskyto Matejka
          if ((i->rtm_family == AF_INET6) && ipa_in_netX(ra->nh.gw, (net_addr *) &sit))
1554 95616c82 Ondrej Zajicek
            return;
1555
1556 a1f5e514 Ondrej Zajicek (work)
          if (i->rtm_flags & RTNH_F_ONLINK)
1557
            ra->nh.flags |= RNF_ONLINK;
1558
1559 23c212e7 Ondrej Zajicek (work)
          neighbor *nbr;
1560 d14f8c3c Jan Moskyto Matejka
          nbr = neigh_find2(&p->p, &(ra->nh.gw), ra->nh.iface,
1561 a1f5e514 Ondrej Zajicek (work)
                            (ra->nh.flags & RNF_ONLINK) ? NEF_ONLINK : 0);
1562 23c212e7 Ondrej Zajicek (work)
          if (!nbr || (nbr->scope == SCOPE_HOST))
1563 95616c82 Ondrej Zajicek
            {
1564 4e276a89 Jan Moskyto Matejka
              log(L_ERR "KRT: Received route %N with strange next-hop %I", net->n.addr,
1565
                  ra->nh.gw);
1566 95616c82 Ondrej Zajicek
              return;
1567
            }
1568
        }
1569
1570
      break;
1571
    case RTN_BLACKHOLE:
1572 2feaa693 Ondrej Zajicek (work)
      ra->dest = RTD_BLACKHOLE;
1573 95616c82 Ondrej Zajicek
      break;
1574
    case RTN_UNREACHABLE:
1575 2feaa693 Ondrej Zajicek (work)
      ra->dest = RTD_UNREACHABLE;
1576 95616c82 Ondrej Zajicek
      break;
1577
    case RTN_PROHIBIT:
1578 2feaa693 Ondrej Zajicek (work)
      ra->dest = RTD_PROHIBIT;
1579 95616c82 Ondrej Zajicek
      break;
1580
    /* FIXME: What about RTN_THROW? */
1581
    default:
1582
      SKIP("type %d\n", i->rtm_type);
1583
      return;
1584
    }
1585
1586 d14f8c3c Jan Moskyto Matejka
  int labels = 0;
1587
  if ((i->rtm_family == AF_MPLS) && a[RTA_NEWDST] && !ra->nh.next)
1588
    labels = rta_get_mpls(a[RTA_NEWDST], ra->nh.label);
1589
1590
  if (a[RTA_ENCAP] && a[RTA_ENCAP_TYPE] && !ra->nh.next)
1591
    {
1592
      switch (rta_get_u16(a[RTA_ENCAP_TYPE]))
1593
        {
1594
          case LWTUNNEL_ENCAP_MPLS:
1595
            {
1596
              struct rtattr *enca[BIRD_RTA_MAX];
1597
              nl_attr_len = RTA_PAYLOAD(a[RTA_ENCAP]);
1598
              nl_parse_attrs(RTA_DATA(a[RTA_ENCAP]), encap_mpls_want, enca, sizeof(enca));
1599
              labels = rta_get_mpls(enca[RTA_DST], ra->nh.label);
1600
              break;
1601
            }
1602
          default:
1603
            SKIP("unknown encapsulation method %d\n", rta_get_u16(a[RTA_ENCAP_TYPE]));
1604
            break;
1605
        }
1606
    }
1607
1608
  if (labels < 0)
1609
  {
1610
    log(L_WARN "KRT: Too long MPLS stack received, ignoring.");
1611
    ra->nh.labels = 0;
1612
  }
1613
  else
1614
    ra->nh.labels = labels;
1615
1616
  rte *e = rte_get_temp(ra);
1617
  e->net = net;
1618
  e->u.krt.src = src;
1619
  e->u.krt.proto = i->rtm_protocol;
1620
  e->u.krt.seen = 0;
1621
  e->u.krt.best = 0;
1622
  e->u.krt.metric = 0;
1623
1624 6e75d0d2 Ondrej Zajicek (work)
  if (i->rtm_scope != def_scope)
1625
    {
1626
      ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr));
1627
      ea->next = ra->eattrs;
1628
      ra->eattrs = ea;
1629
      ea->flags = EALF_SORTED;
1630
      ea->count = 1;
1631
      ea->attrs[0].id = EA_KRT_SCOPE;
1632
      ea->attrs[0].flags = 0;
1633
      ea->attrs[0].type = EAF_TYPE_INT;
1634
      ea->attrs[0].u.data = i->rtm_scope;
1635
    }
1636 95616c82 Ondrej Zajicek
1637 d14f8c3c Jan Moskyto Matejka
  if (a[RTA_PRIORITY])
1638
    e->u.krt.metric = rta_get_u32(a[RTA_PRIORITY]);
1639
1640 95616c82 Ondrej Zajicek
  if (a[RTA_PREFSRC])
1641
    {
1642 9b136840 Jan Moskyto Matejka
      ip_addr ps = rta_get_ipa(a[RTA_PREFSRC]);
1643 95616c82 Ondrej Zajicek
1644 2feaa693 Ondrej Zajicek (work)
      ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr));
1645
      ea->next = ra->eattrs;
1646
      ra->eattrs = ea;
1647 95616c82 Ondrej Zajicek
      ea->flags = EALF_SORTED;
1648
      ea->count = 1;
1649
      ea->attrs[0].id = EA_KRT_PREFSRC;
1650
      ea->attrs[0].flags = 0;
1651
      ea->attrs[0].type = EAF_TYPE_IP_ADDRESS;
1652 2feaa693 Ondrej Zajicek (work)
      ea->attrs[0].u.ptr = lp_alloc(s->pool, sizeof(struct adata) + sizeof(ps));
1653 95616c82 Ondrej Zajicek
      ea->attrs[0].u.ptr->length = sizeof(ps);
1654
      memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps));
1655
    }
1656
1657
  if (a[RTA_FLOW])
1658
    {
1659 2feaa693 Ondrej Zajicek (work)
      ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + sizeof(eattr));
1660
      ea->next = ra->eattrs;
1661
      ra->eattrs = ea;
1662 95616c82 Ondrej Zajicek
      ea->flags = EALF_SORTED;
1663
      ea->count = 1;
1664
      ea->attrs[0].id = EA_KRT_REALM;
1665
      ea->attrs[0].flags = 0;
1666
      ea->attrs[0].type = EAF_TYPE_INT;
1667 acb04cfd Ondrej Zajicek (work)
      ea->attrs[0].u.data = rta_get_u32(a[RTA_FLOW]);
1668 95616c82 Ondrej Zajicek
    }
1669
1670 9fdf9d29 Ondrej Zajicek
  if (a[RTA_METRICS])
1671
    {
1672
      u32 metrics[KRT_METRICS_MAX];
1673 2feaa693 Ondrej Zajicek (work)
      ea_list *ea = lp_alloc(s->pool, sizeof(ea_list) + KRT_METRICS_MAX * sizeof(eattr));
1674 9fdf9d29 Ondrej Zajicek
      int t, n = 0;
1675
1676
      if (nl_parse_metrics(a[RTA_METRICS], metrics, ARRAY_SIZE(metrics)) < 0)
1677
        {
1678 fe9f1a6d Ondrej Zajicek (work)
          log(L_ERR "KRT: Received route %N with strange RTA_METRICS attribute", net->n.addr);
1679 9fdf9d29 Ondrej Zajicek
          return;
1680
        }
1681
1682
      for (t = 1; t < KRT_METRICS_MAX; t++)
1683
        if (metrics[0] & (1 << t))
1684
          {
1685
            ea->attrs[n].id = EA_CODE(EAP_KRT, KRT_METRICS_OFFSET + t);
1686
            ea->attrs[n].flags = 0;
1687
            ea->attrs[n].type = EAF_TYPE_INT; /* FIXME: Some are EAF_TYPE_BITFIELD */
1688
            ea->attrs[n].u.data = metrics[t];
1689
            n++;
1690
          }
1691
1692
      if (n > 0)
1693
        {
1694 2feaa693 Ondrej Zajicek (work)
          ea->next = ra->eattrs;
1695 9fdf9d29 Ondrej Zajicek
          ea->flags = EALF_SORTED;
1696
          ea->count = n;
1697 2feaa693 Ondrej Zajicek (work)
          ra->eattrs = ea;
1698 9fdf9d29 Ondrej Zajicek
        }
1699
    }
1700
1701 2feaa693 Ondrej Zajicek (work)
  /*
1702
   * Ideally, now we would send the received route to the rest of kernel code.
1703
   * But IPv6 ECMP routes are sent as a sequence of routes, so we postpone it
1704
   * and merge next hops until the end of the sequence.
1705
   */
1706
1707
  if (!s->net)
1708
  {
1709
    /* Store the new route */
1710
    s->net = net;
1711
    s->attrs = ra;
1712
    s->proto = p;
1713
    s->new = new;
1714
    s->krt_src = src;
1715
    s->krt_type = i->rtm_type;
1716
    s->krt_proto = i->rtm_protocol;
1717
    s->krt_metric = priority;
1718
  }
1719 95616c82 Ondrej Zajicek
  else
1720 2feaa693 Ondrej Zajicek (work)
  {
1721
    /* Merge next hops with the stored route */
1722 62e64905 Ondrej Zajicek (work)
    rta *oa = s->attrs;
1723 2feaa693 Ondrej Zajicek (work)
1724 62e64905 Ondrej Zajicek (work)
    struct nexthop *nhs = &oa->nh;
1725
    nexthop_insert(&nhs, &ra->nh);
1726
1727
    /* Perhaps new nexthop is inserted at the first position */
1728
    if (nhs == &ra->nh)
1729
    {
1730
      /* Swap rtas */
1731
      s->attrs = ra;
1732
1733
      /* Keep old eattrs */
1734
      ra->eattrs = oa->eattrs;
1735
    }
1736 2feaa693 Ondrej Zajicek (work)
  }
1737 95616c82 Ondrej Zajicek
}
1738
1739
void
1740
krt_do_scan(struct krt_proto *p UNUSED)        /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */
1741
{
1742
  struct nlmsghdr *h;
1743 2feaa693 Ondrej Zajicek (work)
  struct nl_parse_state s;
1744 95616c82 Ondrej Zajicek
1745 cc5b93f7 Ondrej Zajicek (work)
  nl_parse_begin(&s, 1, 0);
1746 d7661fbe Jan Moskyto Matejka
  nl_request_dump(AF_INET, RTM_GETROUTE);
1747 95616c82 Ondrej Zajicek
  while (h = nl_get_scan())
1748
    if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
1749 2feaa693 Ondrej Zajicek (work)
      nl_parse_route(&s, h);
1750 95616c82 Ondrej Zajicek
    else
1751
      log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
1752 cc5b93f7 Ondrej Zajicek (work)
  nl_parse_end(&s);
1753 29a64162 Ondrej Zajicek (work)
1754 cc5b93f7 Ondrej Zajicek (work)
  nl_parse_begin(&s, 1, 1);
1755 d7661fbe Jan Moskyto Matejka
  nl_request_dump(AF_INET6, RTM_GETROUTE);
1756
  while (h = nl_get_scan())
1757
    if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
1758 cc5b93f7 Ondrej Zajicek (work)
      nl_parse_route(&s, h);
1759 d7661fbe Jan Moskyto Matejka
    else
1760
      log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
1761 2feaa693 Ondrej Zajicek (work)
  nl_parse_end(&s);
1762 d14f8c3c Jan Moskyto Matejka
1763
  nl_parse_begin(&s, 1, 1);
1764
  nl_request_dump(AF_MPLS, RTM_GETROUTE);
1765
  while (h = nl_get_scan())
1766
    if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
1767
      nl_parse_route(&s, h);
1768
    else
1769
      log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
1770
  nl_parse_end(&s);
1771 95616c82 Ondrej Zajicek
}
1772
1773
/*
1774
 *        Asynchronous Netlink interface
1775
 */
1776
1777
static sock *nl_async_sk;                /* BIRD socket for asynchronous notifications */
1778
static byte *nl_async_rx_buffer;        /* Receive buffer */
1779
1780
static void
1781
nl_async_msg(struct nlmsghdr *h)
1782
{
1783 2feaa693 Ondrej Zajicek (work)
  struct nl_parse_state s;
1784
1785 95616c82 Ondrej Zajicek
  switch (h->nlmsg_type)
1786
    {
1787
    case RTM_NEWROUTE:
1788
    case RTM_DELROUTE:
1789
      DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type);
1790 2feaa693 Ondrej Zajicek (work)
      nl_parse_begin(&s, 0, 0);
1791
      nl_parse_route(&s, h);
1792
      nl_parse_end(&s);
1793 95616c82 Ondrej Zajicek
      break;
1794
    case RTM_NEWLINK:
1795
    case RTM_DELLINK:
1796
      DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type);
1797 1e4891e4 Ondrej Zajicek (work)
      if (kif_proto)
1798
        nl_parse_link(h, 0);
1799 95616c82 Ondrej Zajicek
      break;
1800
    case RTM_NEWADDR:
1801
    case RTM_DELADDR:
1802
      DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type);
1803 1e4891e4 Ondrej Zajicek (work)
      if (kif_proto)
1804
        nl_parse_addr(h, 0);
1805 95616c82 Ondrej Zajicek
      break;
1806
    default:
1807
      DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type);
1808
    }
1809
}
1810
1811
static int
1812 3e236955 Jan Moskyto Matejka
nl_async_hook(sock *sk, uint size UNUSED)
1813 95616c82 Ondrej Zajicek
{
1814
  struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE };
1815
  struct sockaddr_nl sa;
1816 31e9e101 Stijn Tintel
  struct msghdr m = {
1817
    .msg_name = &sa,
1818
    .msg_namelen = sizeof(sa),
1819
    .msg_iov = &iov,
1820
    .msg_iovlen = 1,
1821
  };
1822 95616c82 Ondrej Zajicek
  struct nlmsghdr *h;
1823
  int x;
1824 ae80a2de Pavel Tvrdík
  uint len;
1825 95616c82 Ondrej Zajicek
1826
  x = recvmsg(sk->fd, &m, 0);
1827
  if (x < 0)
1828
    {
1829
      if (errno == ENOBUFS)
1830
        {
1831
          /*
1832
           *  Netlink reports some packets have been thrown away.
1833
           *  One day we might react to it by asking for route table
1834
           *  scan in near future.
1835
           */
1836 2c33da50 Jan Moskyto Matejka
          log(L_WARN "Kernel dropped some netlink messages, will resync on next scan.");
1837 95616c82 Ondrej Zajicek
          return 1;        /* More data are likely to be ready */
1838
        }
1839
      else if (errno != EWOULDBLOCK)
1840
        log(L_ERR "Netlink recvmsg: %m");
1841
      return 0;
1842
    }
1843
  if (sa.nl_pid)                /* It isn't from the kernel */
1844
    {
1845
      DBG("Non-kernel packet\n");
1846
      return 1;
1847
    }
1848
  h = (void *) nl_async_rx_buffer;
1849
  len = x;
1850
  if (m.msg_flags & MSG_TRUNC)
1851
    {
1852
      log(L_WARN "Netlink got truncated asynchronous message");
1853
      return 1;
1854
    }
1855
  while (NLMSG_OK(h, len))
1856
    {
1857
      nl_async_msg(h);
1858
      h = NLMSG_NEXT(h, len);
1859
    }
1860
  if (len)
1861
    log(L_WARN "nl_async_hook: Found packet remnant of size %d", len);
1862
  return 1;
1863
}
1864
1865
static void
1866 ccd2a3ed Jan Moskyto Matejka
nl_async_err_hook(sock *sk, int e UNUSED)
1867
{
1868
  nl_async_hook(sk, 0);
1869
}
1870
1871
static void
1872 95616c82 Ondrej Zajicek
nl_open_async(void)
1873
{
1874
  sock *sk;
1875
  struct sockaddr_nl sa;
1876
  int fd;
1877
1878 f83ce94d Ondrej Zajicek
  if (nl_async_sk)
1879 95616c82 Ondrej Zajicek
    return;
1880
1881
  DBG("KRT: Opening async netlink socket\n");
1882
1883
  fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1884
  if (fd < 0)
1885
    {
1886
      log(L_ERR "Unable to open asynchronous rtnetlink socket: %m");
1887
      return;
1888
    }
1889
1890
  bzero(&sa, sizeof(sa));
1891
  sa.nl_family = AF_NETLINK;
1892 29a64162 Ondrej Zajicek (work)
  sa.nl_groups = RTMGRP_LINK |
1893
    RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
1894
    RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
1895
1896 95616c82 Ondrej Zajicek
  if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
1897
    {
1898
      log(L_ERR "Unable to bind asynchronous rtnetlink socket: %m");
1899 f83ce94d Ondrej Zajicek
      close(fd);
1900 95616c82 Ondrej Zajicek
      return;
1901
    }
1902
1903 f83ce94d Ondrej Zajicek
  nl_async_rx_buffer = xmalloc(NL_RX_SIZE);
1904
1905 95616c82 Ondrej Zajicek
  sk = nl_async_sk = sk_new(krt_pool);
1906
  sk->type = SK_MAGIC;
1907
  sk->rx_hook = nl_async_hook;
1908 ccd2a3ed Jan Moskyto Matejka
  sk->err_hook = nl_async_err_hook;
1909 95616c82 Ondrej Zajicek
  sk->fd = fd;
1910 05476c4d Ondrej Zajicek
  if (sk_open(sk) < 0)
1911 95616c82 Ondrej Zajicek
    bug("Netlink: sk_open failed");
1912
}
1913
1914 9ddbfbdd Jan Moskyto Matejka
1915 95616c82 Ondrej Zajicek
/*
1916
 *        Interface to the UNIX krt module
1917
 */
1918
1919
void
1920 9ddbfbdd Jan Moskyto Matejka
krt_sys_io_init(void)
1921
{
1922 05d47bd5 Jan Moskyto Matejka
  nl_linpool = lp_new_default(krt_pool);
1923 9ddbfbdd Jan Moskyto Matejka
  HASH_INIT(nl_table_map, krt_pool, 6);
1924
}
1925
1926
int
1927 c6964c30 Ondrej Zajicek
krt_sys_start(struct krt_proto *p)
1928 95616c82 Ondrej Zajicek
{
1929 29a64162 Ondrej Zajicek (work)
  struct krt_proto *old = HASH_FIND(nl_table_map, RTH, p->af, krt_table_id(p));
1930 9ddbfbdd Jan Moskyto Matejka
1931
  if (old)
1932
    {
1933
      log(L_ERR "%s: Kernel table %u already registered by %s",
1934
          p->p.name, krt_table_id(p), old->p.name);
1935
      return 0;
1936
    }
1937
1938
  HASH_INSERT2(nl_table_map, RTH, krt_pool, p);
1939 c6964c30 Ondrej Zajicek
1940
  nl_open();
1941
  nl_open_async();
1942 9ddbfbdd Jan Moskyto Matejka
1943
  return 1;
1944 95616c82 Ondrej Zajicek
}
1945
1946
void
1947 9ddbfbdd Jan Moskyto Matejka
krt_sys_shutdown(struct krt_proto *p)
1948 95616c82 Ondrej Zajicek
{
1949 9ddbfbdd Jan Moskyto Matejka
  HASH_REMOVE2(nl_table_map, RTH, krt_pool, p);
1950 95616c82 Ondrej Zajicek
}
1951
1952
int
1953
krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
1954
{
1955 4adcb9df Ondrej Zajicek (work)
  return (n->sys.table_id == o->sys.table_id) && (n->sys.metric == o->sys.metric);
1956 95616c82 Ondrej Zajicek
}
1957
1958
void
1959
krt_sys_init_config(struct krt_config *cf)
1960
{
1961
  cf->sys.table_id = RT_TABLE_MAIN;
1962 bff21441 Ondrej Zajicek (work)
  cf->sys.metric = 32;
1963 95616c82 Ondrej Zajicek
}
1964
1965
void
1966
krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
1967
{
1968
  d->sys.table_id = s->sys.table_id;
1969 4adcb9df Ondrej Zajicek (work)
  d->sys.metric = s->sys.metric;
1970 95616c82 Ondrej Zajicek
}
1971
1972 9fdf9d29 Ondrej Zajicek
static const char *krt_metrics_names[KRT_METRICS_MAX] = {
1973
  NULL, "lock", "mtu", "window", "rtt", "rttvar", "sstresh", "cwnd", "advmss",
1974
  "reordering", "hoplimit", "initcwnd", "features", "rto_min", "initrwnd", "quickack"
1975
};
1976
1977
static const char *krt_features_names[KRT_FEATURES_MAX] = {
1978
  "ecn", NULL, NULL, "allfrag"
1979
};
1980
1981
int
1982
krt_sys_get_attr(eattr *a, byte *buf, int buflen UNUSED)
1983
{
1984
  switch (a->id)
1985
  {
1986
  case EA_KRT_PREFSRC:
1987
    bsprintf(buf, "prefsrc");
1988
    return GA_NAME;
1989
1990
  case EA_KRT_REALM:
1991
    bsprintf(buf, "realm");
1992
    return GA_NAME;
1993
1994 6e75d0d2 Ondrej Zajicek (work)
  case EA_KRT_SCOPE:
1995
    bsprintf(buf, "scope");
1996
    return GA_NAME;
1997
1998 9fdf9d29 Ondrej Zajicek
  case EA_KRT_LOCK:
1999
    buf += bsprintf(buf, "lock:");
2000
    ea_format_bitfield(a, buf, buflen, krt_metrics_names, 2, KRT_METRICS_MAX);
2001
    return GA_FULL;
2002
2003
  case EA_KRT_FEATURES:
2004
    buf += bsprintf(buf, "features:");
2005
    ea_format_bitfield(a, buf, buflen, krt_features_names, 0, KRT_FEATURES_MAX);
2006
    return GA_FULL;
2007
2008
  default:;
2009
    int id = (int)EA_ID(a->id) - KRT_METRICS_OFFSET;
2010
    if (id > 0 && id < KRT_METRICS_MAX)
2011
    {
2012
      bsprintf(buf, "%s", krt_metrics_names[id]);
2013
      return GA_NAME;
2014
    }
2015
2016
    return GA_UNKNOWN;
2017
  }
2018
}
2019
2020 95616c82 Ondrej Zajicek
2021
2022
void
2023
kif_sys_start(struct kif_proto *p UNUSED)
2024
{
2025
  nl_open();
2026
  nl_open_async();
2027
}
2028
2029
void
2030
kif_sys_shutdown(struct kif_proto *p UNUSED)
2031
{
2032
}
2033 153f02da Ondrej Zajicek (work)
2034
int
2035
kif_update_sysdep_addr(struct iface *i UNUSED)
2036
{
2037
  return 0;
2038
}