Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / bsd / krt-sock.c @ 62e64905

History | View | Annotate | Download (24.5 KB)

1
/*
2
 *        BIRD -- BSD Routing Table Syncing
3
 *
4
 *        (c) 2004 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <ctype.h>
12
#include <fcntl.h>
13
#include <unistd.h>
14
#include <sys/param.h>
15
#include <sys/types.h>
16
#include <sys/socket.h>
17
#include <sys/sysctl.h>
18
#include <sys/ioctl.h>
19
#include <netinet/in.h>
20
#include <net/route.h>
21
#include <net/if.h>
22
#include <net/if_dl.h>
23

    
24
#undef LOCAL_DEBUG
25

    
26
#include "nest/bird.h"
27
#include "nest/iface.h"
28
#include "nest/route.h"
29
#include "nest/protocol.h"
30
#include "nest/iface.h"
31
#include "sysdep/unix/timer.h"
32
#include "sysdep/unix/unix.h"
33
#include "sysdep/unix/krt.h"
34
#include "lib/string.h"
35
#include "lib/socket.h"
36

    
37

    
38
/*
39
 * There are significant differences in multiple tables support between BSD variants.
40
 *
41
 * OpenBSD has table_id field for routes in route socket protocol, therefore all
42
 * tables could be managed by one kernel socket. FreeBSD lacks such field,
43
 * therefore multiple sockets (locked to specific table using SO_SETFIB socket
44
 * option) must be used.
45
 *
46
 * Both FreeBSD and OpenBSD uses separate scans for each table. In OpenBSD,
47
 * table_id is specified explicitly as sysctl scan argument, while in FreeBSD it
48
 * is handled implicitly by changing default table using setfib() syscall.
49
 *
50
 * KRT_SHARED_SOCKET        - use shared kernel socked instead of one for each krt_proto
51
 * KRT_USE_SETFIB_SCAN        - use setfib() for sysctl() route scan
52
 * KRT_USE_SETFIB_SOCK        - use SO_SETFIB socket option for kernel sockets
53
 * KRT_USE_SYSCTL_7        - use 7-th arg of sysctl() as table id for route scans
54
 * KRT_USE_SYSCTL_NET_FIBS - use net.fibs sysctl() for dynamic max number of fibs
55
 */
56

    
57
#ifdef __FreeBSD__
58
#define KRT_MAX_TABLES 256
59
#define KRT_USE_SETFIB_SCAN
60
#define KRT_USE_SETFIB_SOCK
61
#define KRT_USE_SYSCTL_NET_FIBS
62
#endif
63

    
64
#ifdef __OpenBSD__
65
#define KRT_MAX_TABLES (RT_TABLEID_MAX+1)
66
#define KRT_SHARED_SOCKET
67
#define KRT_USE_SYSCTL_7
68
#endif
69

    
70
#ifndef KRT_MAX_TABLES
71
#define KRT_MAX_TABLES 1
72
#endif
73

    
74

    
75
/* Dynamic max number of tables */
76

    
77
int krt_max_tables;
78

    
79
#ifdef KRT_USE_SYSCTL_NET_FIBS
80

    
81
static int
82
krt_get_max_tables(void)
83
{
84
  int fibs;
85
  size_t fibs_len = sizeof(fibs);
86

    
87
  if (sysctlbyname("net.fibs", &fibs, &fibs_len, NULL, 0) < 0)
88
  {
89
    log(L_WARN "KRT: unable to get max number of fib tables: %m");
90
    return 1;
91
  }
92

    
93
  return MIN(fibs, KRT_MAX_TABLES);
94
}
95

    
96
#else
97

    
98
static int
99
krt_get_max_tables(void)
100
{
101
  return KRT_MAX_TABLES;
102
}
103

    
104
#endif /* KRT_USE_SYSCTL_NET_FIBS */
105

    
106

    
107
/* setfib() syscall for FreeBSD scans */
108

    
109
#ifdef KRT_USE_SETFIB_SCAN
110

    
111
/*
112
static int krt_default_fib;
113

114
static int
115
krt_get_active_fib(void)
116
{
117
  int fib;
118
  size_t fib_len = sizeof(fib);
119

120
  if (sysctlbyname("net.my_fibnum", &fib, &fib_len, NULL, 0) < 0)
121
  {
122
    log(L_WARN "KRT: unable to get active fib number: %m");
123
    return 0;
124
  }
125

126
  return fib;
127
}
128
*/
129

    
130
extern int setfib(int fib);
131

    
132
#endif /* KRT_USE_SETFIB_SCAN */
133

    
134

    
135
/* table_id -> krt_proto map */
136

    
137
#ifdef KRT_SHARED_SOCKET
138
static struct krt_proto *krt_table_map[KRT_MAX_TABLES][2];
139
#endif
140

    
141

    
142
/* Route socket message processing */
143

    
144
int
145
krt_capable(rte *e)
146
{
147
  rta *a = e->attrs;
148

    
149
  return
150
    ((a->dest == RTD_UNICAST && !a->nh.next) /* No multipath support */
151
#ifdef RTF_REJECT
152
     || a->dest == RTD_UNREACHABLE
153
#endif
154
#ifdef RTF_BLACKHOLE
155
     || a->dest == RTD_BLACKHOLE
156
#endif
157
     );
158
}
159

    
160
#ifndef RTAX_MAX
161
#define RTAX_MAX 8
162
#endif
163

    
164
struct ks_msg
165
{
166
  struct rt_msghdr rtm;
167
  struct sockaddr_storage buf[RTAX_MAX];
168
};
169

    
170
#define ROUNDUP(a) \
171
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
172

    
173
#define NEXTADDR(w, u) \
174
        if (msg.rtm.rtm_addrs & (w)) {\
175
          l = ROUNDUP(((struct sockaddr *)&(u))->sa_len);\
176
          memmove(body, &(u), l); body += l;}
177

    
178
#define GETADDR(p, F) \
179
  bzero(p, sizeof(*p));\
180
  if ((addrs & (F)) && ((struct sockaddr *)body)->sa_len) {\
181
    uint l = ROUNDUP(((struct sockaddr *)body)->sa_len);\
182
    memcpy(p, body, (l > sizeof(*p) ? sizeof(*p) : l));\
183
    body += l;}
184

    
185
static int
186
krt_send_route(struct krt_proto *p, int cmd, rte *e)
187
{
188
  net *net = e->net;
189
  rta *a = e->attrs;
190
  static int msg_seq;
191
  struct iface *j, *i = a->nh.iface;
192
  int l;
193
  struct ks_msg msg;
194
  char *body = (char *)msg.buf;
195
  sockaddr gate, mask, dst;
196

    
197
  DBG("krt-sock: send %I/%d via %I\n", net->n.prefix, net->n.pxlen, a->gw);
198

    
199
  bzero(&msg,sizeof (struct rt_msghdr));
200
  msg.rtm.rtm_version = RTM_VERSION;
201
  msg.rtm.rtm_type = cmd;
202
  msg.rtm.rtm_seq = msg_seq++;
203
  msg.rtm.rtm_addrs = RTA_DST;
204
  msg.rtm.rtm_flags = RTF_UP | RTF_PROTO1;
205

    
206
  /* XXXX */
207
  if (net_pxlen(net->n.addr) == net_max_prefix_length[net->n.addr->type])
208
    msg.rtm.rtm_flags |= RTF_HOST;
209
  else
210
    msg.rtm.rtm_addrs |= RTA_NETMASK;
211

    
212
#ifdef KRT_SHARED_SOCKET
213
  msg.rtm.rtm_tableid = KRT_CF->sys.table_id;
214
#endif
215

    
216
#ifdef RTF_REJECT
217
  if(a->dest == RTD_UNREACHABLE)
218
    msg.rtm.rtm_flags |= RTF_REJECT;
219
#endif
220
#ifdef RTF_BLACKHOLE
221
  if(a->dest == RTD_BLACKHOLE)
222
    msg.rtm.rtm_flags |= RTF_BLACKHOLE;
223
#endif
224

    
225
  /*
226
   * This is really very nasty, but I'm not able to add reject/blackhole route
227
   * without gateway address.
228
   */
229
  if (!i)
230
  {
231
    WALK_LIST(j, iface_list)
232
    {
233
      if (j->flags & IF_LOOPBACK)
234
      {
235
        i = j;
236
        break;
237
      }
238
    }
239

    
240
    if (!i)
241
    {
242
      log(L_ERR "KRT: Cannot find loopback iface");
243
      return -1;
244
    }
245
  }
246

    
247
  int af = AF_UNSPEC;
248

    
249
  switch (net->n.addr->type) {
250
    case NET_IP4:
251
      af = AF_INET;
252
      break;
253
    case NET_IP6:
254
      af = AF_INET6;
255
      break;
256
    default:
257
      log(L_ERR "KRT: Not sending VPN route %N to kernel", net->n.addr);
258
      return -1;
259
  }
260

    
261
  sockaddr_fill(&dst,  af, net_prefix(net->n.addr), NULL, 0);
262
  sockaddr_fill(&mask, af, net_pxmask(net->n.addr), NULL, 0);
263

    
264
  switch (a->dest)
265
  {
266
  case RTD_UNICAST:
267
    if (ipa_nonzero(a->nh.gw))
268
    {
269
      ip_addr gw = a->nh.gw;
270

    
271
      /* Embed interface ID to link-local address */
272
      if (ipa_is_link_local(gw))
273
        _I0(gw) = 0xfe800000 | (i->index & 0x0000ffff);
274

    
275
      sockaddr_fill(&gate, af, gw, NULL, 0);
276
      msg.rtm.rtm_flags |= RTF_GATEWAY;
277
      msg.rtm.rtm_addrs |= RTA_GATEWAY;
278
      break;
279
    }
280

    
281
#ifdef RTF_REJECT
282
  case RTD_UNREACHABLE:
283
#endif
284
#ifdef RTF_BLACKHOLE
285
  case RTD_BLACKHOLE:
286
#endif
287
  {
288
    /* Fallback for all other valid cases */
289
    if (!i->addr)
290
    {
291
      log(L_ERR "KRT: interface %s has no IP addess", i->name);
292
      return -1;
293
    }
294

    
295
#ifdef RTF_CLONING
296
    if (cmd == RTM_ADD && (i->flags & IF_MULTIACCESS) != IF_MULTIACCESS)        /* PTP */
297
      msg.rtm.rtm_flags |= RTF_CLONING;
298
#endif
299

    
300
    sockaddr_fill(&gate, ipa_is_ip4(i->addr->ip) ? AF_INET : AF_INET6, i->addr->ip, NULL, 0);
301
    msg.rtm.rtm_addrs |= RTA_GATEWAY;
302
  }
303

    
304
  default:
305
    bug("krt-sock: unknown flags, but not filtered");
306
  }
307

    
308
  msg.rtm.rtm_index = i->index;
309

    
310
  NEXTADDR(RTA_DST, dst);
311
  NEXTADDR(RTA_GATEWAY, gate);
312
  NEXTADDR(RTA_NETMASK, mask);
313

    
314
  l = body - (char *)&msg;
315
  msg.rtm.rtm_msglen = l;
316

    
317
  if ((l = write(p->sys.sk->fd, (char *)&msg, l)) < 0) {
318
    log(L_ERR "KRT: Error sending route %N to kernel: %m", net->n.addr);
319
    return -1;
320
  }
321

    
322
  return 0;
323
}
324

    
325
void
326
krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old,
327
                struct ea_list *eattrs UNUSED)
328
{
329
  int err = 0;
330

    
331
  if (old)
332
    krt_send_route(p, RTM_DELETE, old);
333

    
334
  if (new)
335
    err = krt_send_route(p, RTM_ADD, new);
336

    
337
  if (err < 0)
338
    n->n.flags |= KRF_SYNC_ERROR;
339
  else
340
    n->n.flags &= ~KRF_SYNC_ERROR;
341
}
342

    
343
#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
344

    
345
static void
346
krt_read_route(struct ks_msg *msg, struct krt_proto *p, int scan)
347
{
348
  /* p is NULL iff KRT_SHARED_SOCKET and !scan */
349

    
350
  int ipv6;
351
  rte *e;
352
  net *net;
353
  sockaddr dst, gate, mask;
354
  ip_addr idst, igate, imask;
355
  net_addr ndst;
356
  void *body = (char *)msg->buf;
357
  int new = (msg->rtm.rtm_type != RTM_DELETE);
358
  char *errmsg = "KRT: Invalid route received";
359
  int flags = msg->rtm.rtm_flags;
360
  int addrs = msg->rtm.rtm_addrs;
361
  int src;
362
  byte src2;
363

    
364
  if (!(flags & RTF_UP) && scan)
365
    SKIP("not up in scan\n");
366

    
367
  if (!(flags & RTF_DONE) && !scan)
368
    SKIP("not done in async\n");
369

    
370
  if (flags & RTF_LLINFO)
371
    SKIP("link-local\n");
372

    
373
  GETADDR(&dst, RTA_DST);
374
  GETADDR(&gate, RTA_GATEWAY);
375
  GETADDR(&mask, RTA_NETMASK);
376

    
377
  switch (dst.sa.sa_family) {
378
    case AF_INET:
379
      ipv6 = 0;
380
      break;
381
    case AF_INET6:
382
      ipv6 = 1;
383
      break;
384
    default:
385
      SKIP("invalid DST");
386
  }
387

    
388
  /* We do not test family for RTA_NETMASK, because BSD sends us
389
     some strange values, but interpreting them as IPv4/IPv6 works */
390
  mask.sa.sa_family = dst.sa.sa_family;
391

    
392
  idst  = ipa_from_sa(&dst);
393
  imask = ipa_from_sa(&mask);
394
  igate = (gate.sa.sa_family == dst.sa.sa_family) ? ipa_from_sa(&gate) : IPA_NONE;
395

    
396
#ifdef KRT_SHARED_SOCKET
397
  if (!scan)
398
  {
399
    int table_id = msg->rtm.rtm_tableid;
400
    p = (table_id < KRT_MAX_TABLES) ? krt_table_map[table_id][ipv6] : NULL;
401

    
402
    if (!p)
403
      SKIP("unknown table id %d\n", table_id);
404
  }
405
#endif
406
  if ((!ipv6) && (p->p.main_channel->table->addr_type != NET_IP4))
407
    SKIP("reading only IPv4 routes");
408
  if (  ipv6  && (p->p.main_channel->table->addr_type != NET_IP6))
409
    SKIP("reading only IPv6 routes");
410

    
411
  int c = ipa_classify_net(idst);
412
  if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
413
    SKIP("strange class/scope\n");
414

    
415
  int pxlen;
416
  if (ipv6)
417
    pxlen = (flags & RTF_HOST) ? IP6_MAX_PREFIX_LENGTH : ip6_masklen(&ipa_to_ip6(imask));
418
  else
419
    pxlen = (flags & RTF_HOST) ? IP4_MAX_PREFIX_LENGTH : ip4_masklen(ipa_to_ip4(imask));
420

    
421
  if (pxlen < 0)
422
    { log(L_ERR "%s (%I) - netmask %I", errmsg, idst, imask); return; }
423

    
424
  if (ipv6)
425
    net_fill_ip6(&ndst, ipa_to_ip6(idst), pxlen);
426
  else
427
    net_fill_ip4(&ndst, ipa_to_ip4(idst), pxlen);
428

    
429
  if ((flags & RTF_GATEWAY) && ipa_zero(igate))
430
    { log(L_ERR "%s (%N) - missing gateway", errmsg, ndst); return; }
431

    
432
  u32 self_mask = RTF_PROTO1;
433
  u32 alien_mask = RTF_STATIC | RTF_PROTO1 | RTF_GATEWAY;
434

    
435
  src2 = (flags & RTF_STATIC) ? 1 : 0;
436
  src2 |= (flags & RTF_PROTO1) ? 2 : 0;
437

    
438
#ifdef RTF_PROTO2
439
  alien_mask |= RTF_PROTO2;
440
  src2 |= (flags & RTF_PROTO2) ? 4 : 0;
441
#endif
442

    
443
#ifdef RTF_PROTO3
444
  alien_mask |= RTF_PROTO3;
445
  src2 |= (flags & RTF_PROTO3) ? 8 : 0;
446
#endif
447

    
448
#ifdef RTF_REJECT
449
  alien_mask |= RTF_REJECT;
450
#endif
451

    
452
#ifdef RTF_BLACKHOLE
453
  alien_mask |= RTF_BLACKHOLE;
454
#endif
455

    
456
  if (flags & (RTF_DYNAMIC | RTF_MODIFIED))
457
    src = KRT_SRC_REDIRECT;
458
  else if (flags & self_mask)
459
    {
460
      if (!scan)
461
        SKIP("echo\n");
462
      src = KRT_SRC_BIRD;
463
    }
464
  else if (flags & alien_mask)
465
    src = KRT_SRC_ALIEN;
466
  else
467
    src = KRT_SRC_KERNEL;
468

    
469
  net = net_get(p->p.main_channel->table, &ndst);
470

    
471
  rta a = {
472
    .src = p->p.main_source,
473
    .source = RTS_INHERIT,
474
    .scope = SCOPE_UNIVERSE,
475
  };
476

    
477
  /* reject/blackhole routes have also set RTF_GATEWAY,
478
     we wil check them first. */
479

    
480
#ifdef RTF_REJECT
481
  if(flags & RTF_REJECT) {
482
    a.dest = RTD_UNREACHABLE;
483
    goto done;
484
  }
485
#endif
486

    
487
#ifdef RTF_BLACKHOLE
488
  if(flags & RTF_BLACKHOLE) {
489
    a.dest = RTD_BLACKHOLE;
490
    goto done;
491
  }
492
#endif
493

    
494
  a.nh.iface = if_find_by_index(msg->rtm.rtm_index);
495
  if (!a.nh.iface)
496
    {
497
      log(L_ERR "KRT: Received route %N with unknown ifindex %u",
498
          net->n.addr, msg->rtm.rtm_index);
499
      return;
500
    }
501

    
502
  a.dest = RTD_UNICAST;
503
  if (flags & RTF_GATEWAY)
504
  {
505
    neighbor *ng;
506
    a.nh.gw = igate;
507

    
508
    /* Clean up embedded interface ID returned in link-local address */
509
    if (ipa_is_link_local(a.nh.gw))
510
      _I0(a.nh.gw) = 0xfe800000;
511

    
512
    ng = neigh_find2(&p->p, &a.nh.gw, a.nh.iface, 0);
513
    if (!ng || (ng->scope == SCOPE_HOST))
514
      {
515
        /* Ignore routes with next-hop 127.0.0.1, host routes with such
516
           next-hop appear on OpenBSD for address aliases. */
517
        if (ipa_classify(a.nh.gw) == (IADDR_HOST | SCOPE_HOST))
518
          return;
519

    
520
        log(L_ERR "KRT: Received route %N with strange next-hop %I",
521
            net->n.addr, a.nh.gw);
522
        return;
523
      }
524
  }
525

    
526
 done:
527
  e = rte_get_temp(&a);
528
  e->net = net;
529
  e->u.krt.src = src;
530
  e->u.krt.proto = src2;
531
  e->u.krt.seen = 0;
532
  e->u.krt.best = 0;
533
  e->u.krt.metric = 0;
534

    
535
  if (scan)
536
    krt_got_route(p, e);
537
  else
538
    krt_got_route_async(p, e, new);
539
}
540

    
541
static void
542
krt_read_ifannounce(struct ks_msg *msg)
543
{
544
  struct if_announcemsghdr *ifam = (struct if_announcemsghdr *)&msg->rtm;
545

    
546
  if (ifam->ifan_what == IFAN_ARRIVAL)
547
  {
548
    /* Not enough info to create the iface, so we just trigger iface scan */
549
    kif_request_scan();
550
  }
551
  else if (ifam->ifan_what == IFAN_DEPARTURE)
552
  {
553
    struct iface *iface = if_find_by_index(ifam->ifan_index);
554

    
555
    /* Interface is destroyed */
556
    if (!iface)
557
    {
558
      DBG("KRT: unknown interface (%s, #%d) going down. Ignoring\n", ifam->ifan_name, ifam->ifan_index);
559
      return;
560
    }
561

    
562
    if_delete(iface);
563
  }
564

    
565
  DBG("KRT: IFANNOUNCE what: %d index %d name %s\n", ifam->ifan_what, ifam->ifan_index, ifam->ifan_name);
566
}
567

    
568
static void
569
krt_read_ifinfo(struct ks_msg *msg, int scan)
570
{
571
  struct if_msghdr *ifm = (struct if_msghdr *)&msg->rtm;
572
  void *body = (void *)(ifm + 1);
573
  struct sockaddr_dl *dl = NULL;
574
  uint i;
575
  struct iface *iface = NULL, f = {};
576
  int fl = ifm->ifm_flags;
577
  int nlen = 0;
578

    
579
  for (i = 1; i<=RTA_IFP; i <<= 1)
580
  {
581
    if (i & ifm->ifm_addrs)
582
    {
583
      if (i == RTA_IFP)
584
      {
585
        dl = (struct sockaddr_dl *)body;
586
        break;
587
      }
588
      body += ROUNDUP(((struct sockaddr *)&(body))->sa_len);
589
    }
590
  }
591

    
592
  if (dl && (dl->sdl_family != AF_LINK))
593
  {
594
    log(L_WARN "Ignoring strange IFINFO");
595
    return;
596
  }
597

    
598
  if (dl)
599
    nlen = MIN(sizeof(f.name)-1, dl->sdl_nlen);
600

    
601
  /* Note that asynchronous IFINFO messages do not contain iface
602
     name, so we have to found an existing iface by iface index */
603

    
604
  iface = if_find_by_index(ifm->ifm_index);
605
  if (!iface)
606
  {
607
    /* New interface */
608
    if (!dl)
609
      return;        /* No interface name, ignoring */
610

    
611
    memcpy(f.name, dl->sdl_data, nlen);
612
    DBG("New interface '%s' found\n", f.name);
613
  }
614
  else if (dl && memcmp(iface->name, dl->sdl_data, nlen))
615
  {
616
    /* Interface renamed */
617
    if_delete(iface);
618
    memcpy(f.name, dl->sdl_data, nlen);
619
  }
620
  else
621
  {
622
    /* Old interface */
623
    memcpy(f.name, iface->name, sizeof(f.name));
624
  }
625

    
626
  f.index = ifm->ifm_index;
627
  f.mtu = ifm->ifm_data.ifi_mtu;
628

    
629
  if (fl & IFF_UP)
630
    f.flags |= IF_ADMIN_UP;
631
  if (ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
632
    f.flags |= IF_LINK_UP;          /* up or unknown */
633
  if (fl & IFF_LOOPBACK)            /* Loopback */
634
    f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE;
635
  else if (fl & IFF_POINTOPOINT)    /* PtP */
636
    f.flags |= IF_MULTICAST;
637
  else if (fl & IFF_BROADCAST)      /* Broadcast */
638
    f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST;
639
  else
640
    f.flags |= IF_MULTIACCESS;      /* NBMA */
641

    
642
  iface = if_update(&f);
643

    
644
  if (!scan)
645
    if_end_partial_update(iface);
646
}
647

    
648
static void
649
krt_read_addr(struct ks_msg *msg, int scan)
650
{
651
  struct ifa_msghdr *ifam = (struct ifa_msghdr *)&msg->rtm;
652
  void *body = (void *)(ifam + 1);
653
  sockaddr addr, mask, brd;
654
  struct iface *iface = NULL;
655
  struct ifa ifa;
656
  struct sockaddr null;
657
  ip_addr iaddr, imask, ibrd;
658
  int addrs = ifam->ifam_addrs;
659
  int scope, masklen = -1;
660
  int new = (ifam->ifam_type == RTM_NEWADDR);
661

    
662
  /* Strange messages with zero (invalid) ifindex appear on OpenBSD */
663
  if (ifam->ifam_index == 0)
664
    return;
665

    
666
  if(!(iface = if_find_by_index(ifam->ifam_index)))
667
  {
668
    log(L_ERR "KIF: Received address message for unknown interface %d", ifam->ifam_index);
669
    return;
670
  }
671

    
672
  GETADDR (&null, RTA_DST);
673
  GETADDR (&null, RTA_GATEWAY);
674
  GETADDR (&mask, RTA_NETMASK);
675
  GETADDR (&null, RTA_GENMASK);
676
  GETADDR (&null, RTA_IFP);
677
  GETADDR (&addr, RTA_IFA);
678
  GETADDR (&null, RTA_AUTHOR);
679
  GETADDR (&brd, RTA_BRD);
680

    
681
  /* Is addr family IP4 or IP6? */
682
  int ipv6;
683
  switch (addr.sa.sa_family) {
684
    case AF_INET: ipv6 = 0; break;
685
    case AF_INET6: ipv6 = 1; break;
686
    default: return;
687
  }
688

    
689
  iaddr = ipa_from_sa(&addr);
690
  imask = ipa_from_sa(&mask);
691
  ibrd  = ipa_from_sa(&brd);
692

    
693
  if ((ipv6 ? (masklen = ip6_masklen(&ipa_to_ip6(imask))) : (masklen = ip4_masklen(ipa_to_ip4(imask)))) < 0)
694
  {
695
    log(L_ERR "KIF: Invalid mask %I for %s", imask, iface->name);
696
    return;
697
  }
698

    
699
  /* Clean up embedded interface ID returned in link-local address */
700

    
701
  if (ipa_is_link_local(iaddr))
702
    _I0(iaddr) = 0xfe800000;
703

    
704
  if (ipa_is_link_local(ibrd))
705
    _I0(ibrd) = 0xfe800000;
706

    
707

    
708
  bzero(&ifa, sizeof(ifa));
709
  ifa.iface = iface;
710
  ifa.ip = iaddr;
711

    
712
  scope = ipa_classify(ifa.ip);
713
  if (scope < 0)
714
  {
715
    log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, iface->name);
716
    return;
717
  }
718
  ifa.scope = scope & IADDR_SCOPE_MASK;
719

    
720
  if (masklen < (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH))
721
  {
722
    net_fill_ipa(&ifa.prefix, ifa.ip, masklen);
723
    net_normalize(&ifa.prefix);
724

    
725
    if (masklen == ((ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH) - 1))
726
      ifa.opposite = ipa_opposite_m1(ifa.ip);
727

    
728
    if ((!ipv6) && (masklen == IP4_MAX_PREFIX_LENGTH - 2))
729
      ifa.opposite = ipa_opposite_m2(ifa.ip);
730

    
731
    if (iface->flags & IF_BROADCAST)
732
      ifa.brd = ibrd;
733

    
734
    if (!(iface->flags & IF_MULTIACCESS))
735
      ifa.opposite = ibrd;
736
  }
737
  else if (!(iface->flags & IF_MULTIACCESS) && ipa_nonzero(ibrd))
738
  {
739
    net_fill_ipa(&ifa.prefix, ibrd, (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH));
740
    ifa.opposite = ibrd;
741
    ifa.flags |= IA_PEER;
742
  }
743
  else
744
  {
745
    net_fill_ipa(&ifa.prefix, ifa.ip, (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH));
746
    ifa.flags |= IA_HOST;
747
  }
748

    
749
  if (new)
750
    ifa_update(&ifa);
751
  else
752
    ifa_delete(&ifa);
753

    
754
  if (!scan)
755
    if_end_partial_update(iface);
756
}
757

    
758
static void
759
krt_read_msg(struct proto *p, struct ks_msg *msg, int scan)
760
{
761
  /* p is NULL iff KRT_SHARED_SOCKET and !scan */
762

    
763
  switch (msg->rtm.rtm_type)
764
  {
765
    case RTM_GET:
766
      if(!scan) return;
767
    case RTM_ADD:
768
    case RTM_DELETE:
769
    case RTM_CHANGE:
770
      krt_read_route(msg, (struct krt_proto *)p, scan);
771
      break;
772
    case RTM_IFANNOUNCE:
773
      krt_read_ifannounce(msg);
774
      break;
775
    case RTM_IFINFO:
776
      krt_read_ifinfo(msg, scan);
777
      break;
778
    case RTM_NEWADDR:
779
    case RTM_DELADDR:
780
      krt_read_addr(msg, scan);
781
      break;
782
    default:
783
      break;
784
  }
785
}
786

    
787

    
788
/* Sysctl based scans */
789

    
790
static byte *krt_buffer;
791
static size_t krt_buflen, krt_bufmin;
792
static struct proto *krt_buffer_owner;
793

    
794
static byte *
795
krt_buffer_update(struct proto *p, size_t *needed)
796
{
797
  size_t req = *needed;
798

    
799
  if ((req > krt_buflen) ||
800
      ((p == krt_buffer_owner) && (req < krt_bufmin)))
801
  {
802
    /* min buflen is 32 kB, step is 8 kB, or 128 kB if > 1 MB */
803
    size_t step = (req < 0x100000) ? 0x2000 : 0x20000;
804
    krt_buflen = (req < 0x6000) ? 0x8000 : (req + step);
805
    krt_bufmin = (req < 0x8000) ? 0 : (req - 2*step);
806

    
807
    if (krt_buffer) 
808
      mb_free(krt_buffer);
809
    krt_buffer = mb_alloc(krt_pool, krt_buflen);
810
    krt_buffer_owner = p;
811
  }
812

    
813
  *needed = krt_buflen;
814
  return krt_buffer;
815
}
816

    
817
static void
818
krt_buffer_release(struct proto *p)
819
{
820
  if (p == krt_buffer_owner)
821
  {
822
    mb_free(krt_buffer);
823
    krt_buffer = NULL;
824
    krt_buflen = 0;
825
    krt_buffer_owner = 0;
826
  }
827
}
828

    
829
static void
830
krt_sysctl_scan(struct proto *p, int cmd, int table_id)
831
{
832
  byte *buf, *next;
833
  int mib[7], mcnt;
834
  size_t needed;
835
  struct ks_msg *m;
836
  int retries = 3;
837
  int rv;
838

    
839
  mib[0] = CTL_NET;
840
  mib[1] = PF_ROUTE;
841
  mib[2] = 0;
842
  mib[3] = 0; // Set AF to 0 for all available families
843
  mib[4] = cmd;
844
  mib[5] = 0;
845
  mcnt = 6;
846

    
847
#ifdef KRT_USE_SYSCTL_7
848
  if (table_id >= 0)
849
  {
850
    mib[6] = table_id;
851
    mcnt = 7;
852
  }
853
#endif
854

    
855
#ifdef KRT_USE_SETFIB_SCAN
856
  if (table_id > 0)
857
    if (setfib(table_id) < 0)
858
    {
859
      log(L_ERR "KRT: setfib(%d) failed: %m", table_id);
860
      return;
861
    }
862
#endif
863

    
864
 try:
865
  rv = sysctl(mib, mcnt, NULL, &needed, NULL, 0);
866
  if (rv < 0)
867
  {
868
    /* OpenBSD returns EINVAL for not yet used tables */
869
    if ((errno == EINVAL) && (table_id > 0))
870
      goto exit;
871

    
872
    log(L_ERR "KRT: Route scan estimate failed: %m");
873
    goto exit;
874
  }
875

    
876
  /* The table is empty */
877
  if (needed == 0)
878
    goto exit;
879

    
880
  buf = krt_buffer_update(p, &needed);
881

    
882
  rv = sysctl(mib, mcnt, buf, &needed, NULL, 0);
883
  if (rv < 0)
884
  {
885
    /* The buffer size changed since last sysctl ('needed' is not changed) */
886
    if ((errno == ENOMEM) && retries--)
887
      goto try;
888

    
889
    log(L_ERR "KRT: Route scan failed: %m");
890
    goto exit;
891
  }
892

    
893
#ifdef KRT_USE_SETFIB_SCAN
894
  if (table_id > 0)
895
    if (setfib(0) < 0)
896
      die("KRT: setfib(%d) failed: %m", 0);
897
#endif
898

    
899
  /* Process received messages */
900
  for (next = buf; next < (buf + needed); next += m->rtm.rtm_msglen)
901
  {
902
    m = (struct ks_msg *)next;
903
    krt_read_msg(p, m, 1);
904
  }
905

    
906
  return;
907

    
908
 exit:
909
  krt_buffer_release(p);
910

    
911
#ifdef KRT_USE_SETFIB_SCAN
912
  if (table_id > 0)
913
    if (setfib(0) < 0)
914
      die("KRT: setfib(%d) failed: %m", 0);
915
#endif
916
}
917

    
918
void
919
krt_do_scan(struct krt_proto *p)
920
{
921
  krt_sysctl_scan(&p->p, NET_RT_DUMP, KRT_CF->sys.table_id);
922
}
923

    
924
void
925
kif_do_scan(struct kif_proto *p)
926
{
927
  if_start_update();
928
  krt_sysctl_scan(&p->p, NET_RT_IFLIST, -1);
929
  if_end_update();
930
}
931

    
932

    
933
/* Kernel sockets */
934

    
935
static int
936
krt_sock_hook(sock *sk, uint size UNUSED)
937
{
938
  struct ks_msg msg;
939
  int l = read(sk->fd, (char *)&msg, sizeof(msg));
940

    
941
  if (l <= 0)
942
    log(L_ERR "krt-sock: read failed");
943
  else
944
    krt_read_msg((struct proto *) sk->data, &msg, 0);
945

    
946
  return 0;
947
}
948

    
949
static void
950
krt_sock_err_hook(sock *sk, int e UNUSED)
951
{
952
  krt_sock_hook(sk, 0);
953
}
954

    
955
static sock *
956
krt_sock_open(pool *pool, void *data, int table_id UNUSED)
957
{
958
  sock *sk;
959
  int fd;
960

    
961
  fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
962
  if (fd < 0)
963
    die("Cannot open kernel socket for routes");
964

    
965
#ifdef KRT_USE_SETFIB_SOCK
966
  if (table_id > 0)
967
  {
968
    if (setsockopt(fd, SOL_SOCKET, SO_SETFIB, &table_id, sizeof(table_id)) < 0)
969
      die("Cannot set FIB %d for kernel socket: %m", table_id);
970
  }
971
#endif
972

    
973
  sk = sk_new(pool);
974
  sk->type = SK_MAGIC;
975
  sk->rx_hook = krt_sock_hook;
976
  sk->err_hook = krt_sock_err_hook;
977
  sk->fd = fd;
978
  sk->data = data;
979

    
980
  if (sk_open(sk) < 0)
981
    bug("krt-sock: sk_open failed");
982

    
983
  return sk;
984
}
985

    
986
static u32 krt_table_cf[(KRT_MAX_TABLES+31) / 32][2];
987

    
988
#ifdef KRT_SHARED_SOCKET
989

    
990
static sock *krt_sock;
991
static int krt_sock_count;
992

    
993

    
994
static void
995
krt_sock_open_shared(void)
996
{
997
  if (!krt_sock_count)
998
    krt_sock = krt_sock_open(krt_pool, NULL, -1);
999
  
1000
  krt_sock_count++;
1001
}
1002

    
1003
static void
1004
krt_sock_close_shared(void)
1005
{
1006
  krt_sock_count--;
1007

    
1008
  if (!krt_sock_count)
1009
  {
1010
    rfree(krt_sock);
1011
    krt_sock = NULL;
1012
  }
1013
}
1014

    
1015
int
1016
krt_sys_start(struct krt_proto *p)
1017
{
1018
  int id = KRT_CF->sys.table_id;
1019

    
1020
  if (krt_table_cf[id/32][!!(p->af == AF_INET6)] & (1 << (id%32)))
1021
    {
1022
      log(L_ERR "%s: Multiple kernel syncers defined for table #%d", p->p.name, id);
1023
      return 0;
1024
    }
1025

    
1026
  krt_table_cf[id/32][!!(p->af == AF_INET6)] |= (1 << (id%32));
1027

    
1028
  krt_table_map[KRT_CF->sys.table_id][!!(p->af == AF_INET6)] = p;
1029

    
1030
  krt_sock_open_shared();
1031
  p->sys.sk = krt_sock;
1032

    
1033
  return 1;
1034
}
1035

    
1036
void
1037
krt_sys_shutdown(struct krt_proto *p)
1038
{
1039
  krt_table_cf[(KRT_CF->sys.table_id)/32][!!(p->af == AF_INET6)] &= ~(1 << ((KRT_CF->sys.table_id)%32));
1040

    
1041
  krt_sock_close_shared();
1042
  p->sys.sk = NULL;
1043

    
1044
  krt_table_map[KRT_CF->sys.table_id][!!(p->af == AF_INET6)] = NULL;
1045

    
1046
  krt_buffer_release(&p->p);
1047
}
1048

    
1049
#else
1050

    
1051
int
1052
krt_sys_start(struct krt_proto *p)
1053
{
1054
  int id = KRT_CF->sys.table_id;
1055

    
1056
  if (krt_table_cf[id/32][!!(p->af == AF_INET6)] & (1 << (id%32)))
1057
    {
1058
      log(L_ERR "%s: Multiple kernel syncers defined for table #%d", p->p.name, id);
1059
      return 0;
1060
    }
1061

    
1062
  krt_table_cf[id/32][!!(p->af == AF_INET6)] |= (1 << (id%32));
1063

    
1064
  p->sys.sk = krt_sock_open(p->p.pool, p, KRT_CF->sys.table_id);
1065
  return 1;
1066
}
1067

    
1068
void
1069
krt_sys_shutdown(struct krt_proto *p)
1070
{
1071
  krt_table_cf[(KRT_CF->sys.table_id)/32][!!(p->af == AF_INET6)] &= ~(1 << ((KRT_CF->sys.table_id)%32));
1072

    
1073
  rfree(p->sys.sk);
1074
  p->sys.sk = NULL;
1075

    
1076
  krt_buffer_release(&p->p);
1077
}
1078

    
1079
#endif /* KRT_SHARED_SOCKET */
1080

    
1081

    
1082
/* KRT configuration callbacks */
1083

    
1084
int
1085
krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
1086
{
1087
  return n->sys.table_id == o->sys.table_id;
1088
}
1089

    
1090
void
1091
krt_sys_preconfig(struct config *c UNUSED)
1092
{
1093
  krt_max_tables = krt_get_max_tables();
1094
  bzero(&krt_table_cf, sizeof(krt_table_cf));
1095
}
1096

    
1097
void krt_sys_init_config(struct krt_config *c)
1098
{
1099
  c->sys.table_id = 0; /* Default table */
1100
}
1101

    
1102
void krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
1103
{
1104
  d->sys.table_id = s->sys.table_id;
1105
}
1106

    
1107

    
1108
/* KIF misc code */
1109

    
1110
void
1111
kif_sys_start(struct kif_proto *p UNUSED)
1112
{
1113
}
1114

    
1115
void
1116
kif_sys_shutdown(struct kif_proto *p)
1117
{
1118
  krt_buffer_release(&p->p);
1119
}
1120

    
1121

    
1122
struct ifa *
1123
kif_get_primary_ip(struct iface *i UNUSED)
1124
{
1125
#if 0
1126
  static int fd = -1;
1127
  
1128
  if (fd < 0)
1129
    fd = socket(AF_INET, SOCK_DGRAM, 0);
1130

1131
  struct ifreq ifr;
1132
  memset(&ifr, 0, sizeof(ifr));
1133
  strncpy(ifr.ifr_name, i->name, IFNAMSIZ);
1134

1135
  int rv = ioctl(fd, SIOCGIFADDR, (char *) &ifr);
1136
  if (rv < 0)
1137
    return NULL;
1138

1139
  ip_addr addr;
1140
  struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
1141
  memcpy(&addr, &sin->sin_addr.s_addr, sizeof(ip_addr));
1142
  ipa_ntoh(addr);
1143

1144
  struct ifa *a;
1145
  WALK_LIST(a, i->addrs)
1146
  {
1147
    if (ipa_equal(a->ip, addr))
1148
      return a;
1149
  }
1150
#endif
1151

    
1152
  return NULL;
1153
}