Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / linux / netlink / netlink.c @ a2697f02

History | View | Annotate | Download (19 KB)

1
/*
2
 *        BIRD -- Linux Netlink Interface
3
 *
4
 *        (c) 1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <string.h>
10
#include <stdio.h>
11
#include <fcntl.h>
12
#include <net/if.h>
13
#include <sys/socket.h>
14
#include <sys/uio.h>
15
#include <errno.h>
16

    
17
#define LOCAL_DEBUG
18

    
19
#include "nest/bird.h"
20
#include "nest/route.h"
21
#include "nest/protocol.h"
22
#include "nest/iface.h"
23
#include "lib/timer.h"
24
#include "lib/unix.h"
25
#include "lib/krt.h"
26
#include "lib/socket.h"
27

    
28
#include <asm/types.h>
29
#include <linux/netlink.h>
30
#include <linux/rtnetlink.h>
31

    
32
#ifndef MSG_TRUNC                        /* FIXME: Hack to circumvent omissions in glibc includes */
33
#define MSG_TRUNC 0x20
34
#endif
35

    
36
/*
37
 *        Synchronous Netlink interface
38
 */
39

    
40
static int nl_sync_fd = -1;                /* Unix socket for synchronous netlink actions */
41
static u32 nl_sync_seq;                        /* Sequence number of last request sent */
42

    
43
static byte *nl_rx_buffer;                /* Receive buffer */
44
#define NL_RX_SIZE 2048
45

    
46
static struct nlmsghdr *nl_last_hdr;        /* Recently received packet */
47
static unsigned int nl_last_size;
48

    
49
static void
50
nl_open(void)
51
{
52
  if (nl_sync_fd < 0)
53
    {
54
      nl_sync_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
55
      if (nl_sync_fd < 0)
56
        die("Unable to open rtnetlink socket: %m");
57
      nl_sync_seq = now;
58
      nl_rx_buffer = xmalloc(NL_RX_SIZE);
59
    }
60
}
61

    
62
static void
63
nl_send(struct nlmsghdr *nh)
64
{
65
  struct sockaddr_nl sa;
66

    
67
  memset(&sa, 0, sizeof(sa));
68
  sa.nl_family = AF_NETLINK;
69
  nh->nlmsg_pid = 0;
70
  nh->nlmsg_seq = ++nl_sync_seq;
71
  if (sendto(nl_sync_fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0)
72
    die("rtnetlink sendto: %m");
73
  nl_last_hdr = NULL;
74
}
75

    
76
static void
77
nl_request_dump(int cmd)
78
{
79
  struct {
80
    struct nlmsghdr nh;
81
    struct rtgenmsg g;
82
  } req;
83
  req.nh.nlmsg_type = cmd;
84
  req.nh.nlmsg_len = sizeof(req);
85
  req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
86
  req.g.rtgen_family = PF_INET;
87
  nl_send(&req.nh);
88
}
89

    
90
static struct nlmsghdr *
91
nl_get_reply(void)
92
{
93
  for(;;)
94
    {
95
      if (!nl_last_hdr)
96
        {
97
          struct iovec iov = { nl_rx_buffer, NL_RX_SIZE };
98
          struct sockaddr_nl sa;
99
          struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
100
          int x = recvmsg(nl_sync_fd, &m, 0);
101
          if (x < 0)
102
            die("nl_get_reply: %m");
103
          if (sa.nl_pid)                /* It isn't from the kernel */
104
            {
105
              DBG("Non-kernel packet\n");
106
              continue;
107
            }
108
          nl_last_size = x;
109
          nl_last_hdr = (void *) nl_rx_buffer;
110
          if (m.msg_flags & MSG_TRUNC)
111
            bug("nl_get_reply: got truncated reply which should be impossible");
112
        }
113
      if (NLMSG_OK(nl_last_hdr, nl_last_size))
114
        {
115
          struct nlmsghdr *h = nl_last_hdr;
116
          if (h->nlmsg_seq != nl_sync_seq)
117
            {
118
              log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)",
119
                  h->nlmsg_seq, nl_sync_seq);
120
              continue;
121
            }
122
          nl_last_hdr = NLMSG_NEXT(h, nl_last_size);
123
          return h;
124
        }
125
      if (nl_last_size)
126
        log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl_last_size);
127
      nl_last_hdr = NULL;
128
    }
129
}
130

    
131
static int
132
nl_error(struct nlmsghdr *h)
133
{
134
  struct nlmsgerr *e;
135
  int ec;
136

    
137
  if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
138
    {
139
      log(L_WARN "Netlink: Truncated error message received");
140
      return ENOBUFS;
141
    }
142
  e = (struct nlmsgerr *) NLMSG_DATA(h);
143
  ec = -e->error;
144
  if (ec)
145
    log(L_WARN "Netlink: %s", strerror(ec)); /* FIXME: Shut up? */
146
  return ec;
147
}
148

    
149
static struct nlmsghdr *
150
nl_get_scan(void)
151
{
152
  struct nlmsghdr *h = nl_get_reply();
153

    
154
  if (h->nlmsg_type == NLMSG_DONE)
155
    return NULL;
156
  if (h->nlmsg_type == NLMSG_ERROR)
157
    {
158
      nl_error(h);
159
      return NULL;
160
    }
161
  return h;
162
}
163

    
164
static int
165
nl_exchange(struct nlmsghdr *pkt)
166
{
167
  struct nlmsghdr *h;
168

    
169
  nl_send(pkt);
170
  for(;;)
171
    {
172
      h = nl_get_reply();
173
      if (h->nlmsg_type == NLMSG_ERROR)
174
        break;
175
      log(L_WARN "nl_exchange: Unexpected reply received");
176
    }
177
  return nl_error(h);
178
}
179

    
180
/*
181
 *        Netlink attributes
182
 */
183

    
184
static int nl_attr_len;
185

    
186
static void *
187
nl_checkin(struct nlmsghdr *h, int lsize)
188
{
189
  nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize);
190
  if (nl_attr_len < 0)
191
    {
192
      log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len);
193
      return NULL;
194
    }
195
  return NLMSG_DATA(h);
196
}
197

    
198
static int
199
nl_parse_attrs(struct rtattr *a, struct rtattr **k, int ksize)
200
{
201
  int max = ksize / sizeof(struct rtattr *);
202
  bzero(k, ksize);
203
  while (RTA_OK(a, nl_attr_len))
204
    {
205
      if (a->rta_type < max)
206
        k[a->rta_type] = a;
207
      a = RTA_NEXT(a, nl_attr_len);
208
    }
209
  if (nl_attr_len)
210
    {
211
      log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len);
212
      return 0;
213
    }
214
  else
215
    return 1;
216
}
217

    
218
static void
219
nl_add_attr_u32(struct nlmsghdr *h, unsigned maxsize, int code, u32 data)
220
{
221
  unsigned len = RTA_LENGTH(4);
222
  struct rtattr *a;
223

    
224
  if (NLMSG_ALIGN(h->nlmsg_len) + len > maxsize)
225
    bug("nl_add_attr32: packet buffer overflow");
226
  a = (struct rtattr *)((char *)h + NLMSG_ALIGN(h->nlmsg_len));
227
  a->rta_type = code;
228
  a->rta_len = len;
229
  memcpy(RTA_DATA(a), &data, 4);
230
  h->nlmsg_len = NLMSG_ALIGN(h->nlmsg_len) + len;
231
}
232

    
233
static void
234
nl_add_attr_ipa(struct nlmsghdr *h, unsigned maxsize, int code, ip_addr ipa)
235
{
236
  unsigned len = RTA_LENGTH(sizeof(ipa));
237
  struct rtattr *a;
238

    
239
  if (NLMSG_ALIGN(h->nlmsg_len) + len > maxsize)
240
    bug("nl_add_attr_ipa: packet buffer overflow");
241
  a = (struct rtattr *)((char *)h + NLMSG_ALIGN(h->nlmsg_len));
242
  a->rta_type = code;
243
  a->rta_len = len;
244
  ipa = ipa_hton(ipa);
245
  memcpy(RTA_DATA(a), &ipa, sizeof(ipa));
246
  h->nlmsg_len = NLMSG_ALIGN(h->nlmsg_len) + len;
247
}
248

    
249
/*
250
 *        Scanning of interfaces
251
 */
252

    
253
static void
254
nl_parse_link(struct nlmsghdr *h, int scan)
255
{
256
  struct ifinfomsg *i;
257
  struct rtattr *a[IFLA_STATS+1];
258
  int new = h->nlmsg_type == RTM_NEWLINK;
259
  struct iface f;
260
  struct iface *ifi;
261
  char *name;
262
  u32 mtu;
263
  unsigned int fl;
264

    
265
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), a, sizeof(a)))
266
    return;
267
  if (!a[IFLA_IFNAME] || RTA_PAYLOAD(a[IFLA_IFNAME]) < 2 ||
268
      !a[IFLA_MTU] || RTA_PAYLOAD(a[IFLA_MTU]) != 4)
269
    {
270
      log(L_ERR "nl_parse_link: Malformed message received");
271
      return;
272
    }
273
  name = RTA_DATA(a[IFLA_IFNAME]);
274
  memcpy(&mtu, RTA_DATA(a[IFLA_MTU]), sizeof(u32));
275

    
276
  ifi = if_find_by_index(i->ifi_index);
277
  if (!new)
278
    {
279
      DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name);
280
      if (ifi && !scan)
281
        {
282
          memcpy(&f, ifi, sizeof(struct iface));
283
          f.flags |= IF_ADMIN_DOWN;
284
          if_update(&f);
285
        }
286
    }
287
  else
288
    {
289
      DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags);
290
      if (ifi)
291
        memcpy(&f, ifi, sizeof(f));
292
      else
293
        {
294
          bzero(&f, sizeof(f));
295
          f.index = i->ifi_index;
296
        }
297
      strncpy(f.name, RTA_DATA(a[IFLA_IFNAME]), sizeof(f.name)-1);
298
      f.mtu = mtu;
299
      f.flags = 0;
300
      fl = i->ifi_flags;
301
      if (fl & IFF_UP)
302
        f.flags |= IF_LINK_UP;
303
      if (fl & IFF_POINTOPOINT)
304
        f.flags |= IF_UNNUMBERED | IF_MULTICAST;
305
      if (fl & IFF_LOOPBACK)
306
        f.flags |= IF_LOOPBACK | IF_IGNORE;
307
      if (fl & IFF_BROADCAST)
308
        f.flags |= IF_BROADCAST | IF_MULTICAST;
309
      if_update(&f);
310
    }
311
}
312

    
313
static void
314
nl_parse_addr(struct nlmsghdr *h)
315
{
316
  struct ifaddrmsg *i;
317
  struct rtattr *a[IFA_ANYCAST+1];
318
  int new = h->nlmsg_type == RTM_NEWADDR;
319
  struct ifa ifa;
320
  struct iface *ifi;
321

    
322
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFA_RTA(i), a, sizeof(a)))
323
    return;
324
  if (i->ifa_family != AF_INET)
325
    return;
326
  if (!a[IFA_ADDRESS] || RTA_PAYLOAD(a[IFA_ADDRESS]) != sizeof(ip_addr) ||
327
      !a[IFA_LOCAL] || RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr) ||
328
      (a[IFA_BROADCAST] && RTA_PAYLOAD(a[IFA_BROADCAST]) != sizeof(ip_addr)))
329
    {
330
      log(L_ERR "nl_parse_addr: Malformed message received");
331
      return;
332
    }
333

    
334
  ifi = if_find_by_index(i->ifa_index);
335
  if (!ifi)
336
    {
337
      log(L_ERR "KIF: Received address message for unknown interface %d\n", i->ifa_index);
338
      return;
339
    }
340

    
341
  if (i->ifa_prefixlen > 32 || i->ifa_prefixlen == 31 ||
342
      (ifi->flags & IF_UNNUMBERED) && i->ifa_prefixlen != 32)
343
    {
344
      log(L_ERR "KIF: Invalid prefix length for interface %s: %d\n", ifi->name, i->ifa_prefixlen);
345
      new = 0;
346
    }
347

    
348
  bzero(&ifa, sizeof(ifa));
349
  ifa.iface = ifi;
350
  if (i->ifa_flags & IFA_F_SECONDARY)
351
    ifa.flags |= IA_SECONDARY;
352
  memcpy(&ifa.ip, RTA_DATA(a[IFA_LOCAL]), sizeof(ifa.ip));
353
  ifa.ip = ipa_ntoh(ifa.ip);
354
  ifa.pxlen = i->ifa_prefixlen;
355
  if (ifi->flags & IF_UNNUMBERED)
356
    {
357
      memcpy(&ifa.opposite, RTA_DATA(a[IFA_ADDRESS]), sizeof(ifa.opposite));
358
      ifa.opposite = ifa.brd = ipa_ntoh(ifa.opposite);
359
    }
360
  else if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST])
361
    {
362
      memcpy(&ifa.brd, RTA_DATA(a[IFA_BROADCAST]), sizeof(ifa.brd));
363
      ifa.brd = ipa_ntoh(ifa.brd);
364
    }
365
  /* else a NBMA link */
366
  ifa.prefix = ipa_and(ifa.ip, ipa_mkmask(ifa.pxlen));
367

    
368
  DBG("KIF: IF%d(%s): %s IPA %I, net %I/%d, brd %I, opp %I\n",
369
      ifi->index, ifi->name,
370
      new ? "added" : "removed",
371
      ifa.ip, ifa.prefix, ifa.pxlen, ifa.brd, ifa.opposite);
372
  if (new)
373
    ifa_update(&ifa);
374
  else
375
    ifa_delete(&ifa);
376
}
377

    
378
void
379
krt_if_scan(struct kif_proto *p)
380
{
381
  struct nlmsghdr *h;
382

    
383
  if_start_update();
384

    
385
  nl_request_dump(RTM_GETLINK);
386
  while (h = nl_get_scan())
387
    if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)
388
      nl_parse_link(h, 1);
389
    else
390
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
391

    
392
  nl_request_dump(RTM_GETADDR);
393
  while (h = nl_get_scan())
394
    if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
395
      nl_parse_addr(h);
396
    else
397
      log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
398

    
399
  if_end_update();
400
}
401

    
402
/*
403
 *        Routes
404
 */
405

    
406
int
407
krt_capable(rte *e)
408
{
409
  rta *a = e->attrs;
410

    
411
  if (a->cast != RTC_UNICAST
412
#ifdef IPV6
413
      && a->cast != RTC_ANYCAST
414
#endif
415
      )
416
    return 0;
417
  if (a->source == RTS_DEVICE)        /* Kernel takes care of device routes itself */
418
    return 0;
419
  switch (a->dest)
420
    {
421
    case RTD_ROUTER:
422
    case RTD_DEVICE:
423
    case RTD_BLACKHOLE:
424
    case RTD_UNREACHABLE:
425
    case RTD_PROHIBIT:
426
      break;
427
    default:
428
      return 0;
429
    }
430
  return 1;
431
}
432

    
433
static void
434
nl_send_route(rte *e, int new)
435
{
436
  net *net = e->net;
437
  rta *a = e->attrs;
438
  struct {
439
    struct nlmsghdr h;
440
    struct rtmsg r;
441
    char buf[128];
442
  } r;
443
  struct nlmsghdr *reply;
444

    
445
  DBG("nl_send_route(%I/%d,new=%d)\n", net->n.prefix, net->n.pxlen, new);
446

    
447
  bzero(&r.h, sizeof(r.h));
448
  bzero(&r.r, sizeof(r.r));
449
  r.h.nlmsg_type = new ? RTM_NEWROUTE : RTM_DELROUTE;
450
  r.h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
451
  r.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | (new ? NLM_F_CREATE|NLM_F_REPLACE : 0);
452
  /* FIXME: Do we really need to process ACKs? */
453

    
454
  r.r.rtm_family = AF_INET;
455
  r.r.rtm_dst_len = net->n.pxlen;
456
  r.r.rtm_tos = 0;        /* FIXME: Non-zero TOS? */
457
  r.r.rtm_table = RT_TABLE_MAIN;        /* FIXME: Other tables? */
458
  r.r.rtm_protocol = RTPROT_BIRD;
459
  r.r.rtm_scope = RT_SCOPE_UNIVERSE;        /* FIXME: Other scopes? */
460
  nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix);
461
  switch (a->dest)
462
    {
463
    case RTD_ROUTER:
464
      r.r.rtm_type = RTN_UNICAST;
465
      nl_add_attr_ipa(&r.h, sizeof(r), RTA_GATEWAY, a->gw);
466
      break;
467
    case RTD_DEVICE:
468
      r.r.rtm_type = RTN_UNICAST;
469
      nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index);
470
      break;
471
    case RTD_BLACKHOLE:
472
      r.r.rtm_type = RTN_BLACKHOLE;
473
      break;
474
    case RTD_UNREACHABLE:
475
      r.r.rtm_type = RTN_UNREACHABLE;
476
      break;
477
    case RTD_PROHIBIT:
478
      r.r.rtm_type = RTN_PROHIBIT;
479
      break;
480
    default:
481
      bug("krt_capable inconsistent with nl_send_route");
482
    }
483

    
484
  nl_exchange(&r.h);
485
}
486

    
487
void
488
krt_set_notify(struct krt_proto *p, net *n, rte *new, rte *old)
489
{
490
  if (old && new)
491
    {
492
      /* FIXME: Priorities and TOS should be identical as well, but we don't use them yet. */
493
      nl_send_route(new, 1);
494
    }
495
  else
496
    {
497
      if (old)
498
        {
499
          if (!old->attrs->iface || (old->attrs->iface->flags & IF_UP))
500
            nl_send_route(old, 0);
501
          /* else the kernel has already flushed it */
502
        }
503
      if (new)
504
        nl_send_route(new, 1);
505
    }
506
}
507

    
508
struct iface *
509
krt_temp_iface(struct krt_proto *p, unsigned index)
510
{
511
  struct iface *i, *j;
512

    
513
  WALK_LIST(i, p->scan.temp_ifs)
514
    if (i->index == index)
515
      return i;
516
  i = mb_allocz(p->p.pool, sizeof(struct iface));
517
  if (j = if_find_by_index(index))
518
    strcpy(i->name, j->name);
519
  else
520
    strcpy(i->name, "?");
521
  i->index = index;
522
  add_tail(&p->scan.temp_ifs, &i->n);
523
  return i;
524
}
525

    
526
static void
527
nl_parse_route(struct krt_proto *p, struct nlmsghdr *h, int scan)
528
{
529
  struct rtmsg *i;
530
  struct rtattr *a[RTA_CACHEINFO+1];
531
  int new = h->nlmsg_type == RTM_NEWROUTE;
532
  ip_addr dst;
533
  rta ra;
534
  rte *e;
535
  net *net;
536
  u32 oif;
537
  int src;
538

    
539
  if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(RTM_RTA(i), a, sizeof(a)))
540
    return;
541
  if (i->rtm_family != AF_INET)
542
    return;
543
  if ((a[RTA_DST] && RTA_PAYLOAD(a[RTA_DST]) != sizeof(ip_addr)) ||
544
      (a[RTA_OIF] && RTA_PAYLOAD(a[RTA_OIF]) != 4) ||
545
      (a[RTA_PRIORITY] && RTA_PAYLOAD(a[RTA_PRIORITY]) != 4) ||
546
      (a[RTA_GATEWAY] && RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)))
547
    {
548
      log(L_ERR "nl_parse_route: Malformed message received");
549
      return;
550
    }
551

    
552
  if (i->rtm_table != RT_TABLE_MAIN)        /* FIXME: What about other tables? */
553
    return;
554
  if (i->rtm_tos != 0)                        /* FIXME: What about TOS? */
555
    return;
556

    
557
  if (scan && !new)
558
    {
559
      DBG("KRT: Ignoring route deletion\n");
560
      return;
561
    }
562

    
563
  if (a[RTA_DST])
564
    {
565
      memcpy(&dst, RTA_DATA(a[RTA_DST]), sizeof(dst));
566
      dst = ipa_ntoh(dst);
567
    }
568
  else
569
    dst = IPA_NONE;
570
  if (a[RTA_OIF])
571
    memcpy(&oif, RTA_DATA(a[RTA_OIF]), sizeof(oif));
572
  else
573
    oif = ~0;
574

    
575
  DBG("Got %I/%d, type=%d, oif=%d\n", dst, i->rtm_dst_len, i->rtm_type, oif);
576

    
577
  switch (i->rtm_protocol)
578
    {
579
    case RTPROT_REDIRECT:
580
      src = KRT_SRC_REDIRECT;
581
      break;
582
    case RTPROT_KERNEL:
583
      DBG("Route originated in kernel, ignoring\n");
584
      return;
585
    case RTPROT_BIRD:
586
      if (!scan)
587
        {
588
          DBG("Echo of our own route, ignoring\n");
589
          return;
590
        }
591
      src = KRT_SRC_BIRD;
592
      break;
593
    default:
594
      src = KRT_SRC_ALIEN;
595
    }
596

    
597
  net = net_get(&master_table, dst, i->rtm_dst_len);
598
  ra.proto = &p->p;
599
  ra.source = RTS_INHERIT;
600
  ra.scope = SCOPE_UNIVERSE;
601
  ra.cast = RTC_UNICAST;
602
  ra.flags = ra.aflags = 0;
603
  ra.from = IPA_NONE;
604
  ra.gw = IPA_NONE;
605
  ra.iface = NULL;
606
  ra.attrs = NULL;
607

    
608
  switch (i->rtm_type)
609
    {
610
    case RTN_UNICAST:
611
      if (oif == ~0U)
612
        {
613
          log(L_ERR "KRT: Mysterious route with no OIF (%I/%d)", net->n.prefix, net->n.pxlen);
614
          return;
615
        }
616
      if (a[RTA_GATEWAY])
617
        {
618
          neighbor *ng;
619
          ra.dest = RTD_ROUTER;
620
          memcpy(&ra.gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ra.gw));
621
          ra.gw = ipa_ntoh(ra.gw);
622
          ng = neigh_find(&p->p, &ra.gw, 0);
623
          if (ng)
624
            ra.iface = ng->iface;
625
          else
626
            /* FIXME: Remove this warning? */
627
            log(L_WARN "Kernel told us to use non-neighbor %I for %I/%d", ra.gw, net->n.prefix, net->n.pxlen);
628
        }
629
      else
630
        {
631
          ra.dest = RTD_DEVICE;
632
          ra.iface = krt_temp_iface(p, oif);
633
        }
634
      break;
635
    case RTN_BLACKHOLE:
636
      ra.dest = RTD_BLACKHOLE;
637
      break;
638
    case RTN_UNREACHABLE:
639
      ra.dest = RTD_UNREACHABLE;
640
      break;
641
    case RTN_PROHIBIT:
642
      ra.dest = RTD_PROHIBIT;
643
      break;
644
    /* FIXME: What about RTN_THROW? */
645
    default:
646
      DBG("KRT: Ignoring route with type=%d\n", i->rtm_type);
647
      return;
648
    }
649

    
650
  if (i->rtm_scope != RT_SCOPE_UNIVERSE)        /* FIXME: Other scopes? */
651
    {
652
      DBG("KRT: Ignoring route with scope=%d\n", i->rtm_scope);
653
      return;
654
    }
655

    
656
  e = rte_get_temp(&ra);
657
  e->net = net;
658
  e->u.krt.src = src;
659
  e->u.krt.proto = i->rtm_protocol;
660
  e->u.krt.type = i->rtm_type;
661
  if (a[RTA_PRIORITY])
662
    memcpy(&e->u.krt.metric, RTA_DATA(a[RTA_PRIORITY]), sizeof(e->u.krt.metric));
663
  else
664
    e->u.krt.metric = 0;
665
  if (scan)
666
    krt_got_route(p, e);
667
  else
668
    krt_got_route_async(p, e, new);
669
}
670

    
671
void
672
krt_scan_fire(struct krt_proto *p)
673
{
674
  struct nlmsghdr *h;
675

    
676
  nl_request_dump(RTM_GETROUTE);
677
  while (h = nl_get_scan())
678
    if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
679
      nl_parse_route(p, h, 1);
680
    else
681
      log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
682
}
683

    
684
/*
685
 *        Asynchronous Netlink interface
686
 */
687

    
688
static sock *nl_async_sk;                /* BIRD socket for asynchronous notifications */
689
static byte *nl_async_rx_buffer;        /* Receive buffer */
690

    
691
static void
692
nl_async_msg(struct krt_proto *p, struct nlmsghdr *h)
693
{
694
  switch (h->nlmsg_type)
695
    {
696
    case RTM_NEWROUTE:
697
    case RTM_DELROUTE:
698
      DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type);
699
      nl_parse_route(p, h, 0);
700
      break;
701
    case RTM_NEWLINK:
702
    case RTM_DELLINK:
703
      DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type);
704
      nl_parse_link(h, 0);
705
      break;
706
    case RTM_NEWADDR:
707
    case RTM_DELADDR:
708
      DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type);
709
      nl_parse_addr(h);
710
      break;
711
    default:
712
      DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type);
713
    }
714
}
715

    
716
static int
717
nl_async_hook(sock *sk, int size)
718
{
719
  struct krt_proto *p = sk->data;
720
  struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE };
721
  struct sockaddr_nl sa;
722
  struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
723
  struct nlmsghdr *h;
724
  int x;
725
  unsigned int len;
726

    
727
  nl_last_hdr = NULL;                /* Discard packets accidentally remaining in the rxbuf */
728
  x = recvmsg(sk->fd, &m, 0);
729
  if (x < 0)
730
    {
731
      if (errno != EWOULDBLOCK)
732
        log(L_ERR "Netlink recvmsg: %m");
733
      return 0;
734
    }
735
  if (sa.nl_pid)                /* It isn't from the kernel */
736
    {
737
      DBG("Non-kernel packet\n");
738
      return 1;
739
    }
740
  h = (void *) nl_async_rx_buffer;
741
  len = x;
742
  if (m.msg_flags & MSG_TRUNC)
743
    {
744
      log(L_WARN "Netlink got truncated asynchronous message");
745
      return 1;
746
    }
747
  while (NLMSG_OK(h, len))
748
    {
749
      nl_async_msg(p, h);
750
      h = NLMSG_NEXT(h, len);
751
    }
752
  if (len)
753
    log(L_WARN "nl_async_hook: Found packet remnant of size %d", len);
754
  return 1;
755
}
756

    
757
static void
758
nl_open_async(struct krt_proto *p)
759
{
760
  sock *sk;
761
  struct sockaddr_nl sa;
762
  int fd;
763

    
764
  DBG("KRT: Opening async netlink socket\n");
765

    
766
  fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
767
  if (fd < 0)
768
    {
769
      log(L_ERR "Unable to open secondary rtnetlink socket: %m");
770
      return;
771
    }
772

    
773
  bzero(&sa, sizeof(sa));
774
  sa.nl_family = AF_NETLINK;
775
  sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
776
  if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
777
    {
778
      log(L_ERR "Unable to bind secondary rtnetlink socket: %m");
779
      return;
780
    }
781

    
782
  sk = nl_async_sk = sk_new(p->p.pool);
783
  sk->type = SK_MAGIC;
784
  sk->data = p;
785
  sk->rx_hook = nl_async_hook;
786
  sk->fd = fd;
787
  if (sk_open(sk))
788
    bug("Netlink: sk_open failed");
789

    
790
  if (!nl_async_rx_buffer)
791
    nl_async_rx_buffer = xmalloc(NL_RX_SIZE);
792
}
793

    
794
/*
795
 *        Interface to the UNIX krt module
796
 */
797

    
798
void
799
krt_scan_preconfig(struct krt_config *x)
800
{
801
  x->scan.async = 1;
802
  /* FIXME: Use larger defaults for scanning times? */
803
}
804

    
805
void
806
krt_scan_start(struct krt_proto *p)
807
{
808
  init_list(&p->scan.temp_ifs);
809
  nl_open();
810
  if (KRT_CF->scan.async)        /* FIXME: Async is for debugging only. Get rid of it some day. */
811
    nl_open_async(p);
812
}
813

    
814
void
815
krt_scan_shutdown(struct krt_proto *p)
816
{
817
}
818

    
819
void
820
krt_if_start(struct kif_proto *p)
821
{
822
  nl_open();
823
  /* FIXME: nl_open_async() after scan.async is gone */
824
}