Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / bsd / krt-sock.c @ 05476c4d

History | View | Annotate | Download (22.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 "lib/timer.h"
32
#include "lib/unix.h"
33
#include "lib/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

    
76
/* Dynamic max number of tables */
77

    
78
int krt_max_tables;
79

    
80
#ifdef KRT_USE_SYSCTL_NET_FIBS
81

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

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

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

    
97
#else
98

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

    
105
#endif /* KRT_USE_SYSCTL_NET_FIBS */
106

    
107

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

    
110
#ifdef KRT_USE_SETFIB_SCAN
111

    
112
/*
113
static int krt_default_fib;
114

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

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

127
  return fib;
128
}
129
*/
130

    
131
extern int setfib(int fib);
132

    
133
#endif /* KRT_USE_SETFIB_SCAN */
134

    
135

    
136
/* table_id -> krt_proto map */
137

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

    
142

    
143
/* Route socket message processing */
144

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

    
150
  return
151
    a->cast == RTC_UNICAST &&
152
    (a->dest == RTD_ROUTER
153
     || a->dest == RTD_DEVICE
154
#ifdef RTF_REJECT
155
     || a->dest == RTD_UNREACHABLE
156
#endif
157
#ifdef RTF_BLACKHOLE
158
     || a->dest == RTD_BLACKHOLE
159
#endif
160
     );
161
}
162

    
163
#ifndef RTAX_MAX
164
#define RTAX_MAX 8
165
#endif
166

    
167
struct ks_msg
168
{
169
  struct rt_msghdr rtm;
170
  struct sockaddr_storage buf[RTAX_MAX];
171
};
172

    
173
#define ROUNDUP(a) \
174
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
175

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

    
181
#define GETADDR(p, F) \
182
  bzero(p, sizeof(*p));\
183
  if ((addrs & (F)) && ((struct sockaddr *)body)->sa_len) {\
184
    unsigned int l = ROUNDUP(((struct sockaddr *)body)->sa_len);\
185
    memcpy(p, body, (l > sizeof(*p) ? sizeof(*p) : l));\
186
    body += l;}
187

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

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

    
203
  bzero(&msg,sizeof (struct rt_msghdr));
204
  msg.rtm.rtm_version = RTM_VERSION;
205
  msg.rtm.rtm_type = cmd;
206
  msg.rtm.rtm_seq = msg_seq++;
207
  msg.rtm.rtm_addrs = RTA_DST;
208
  msg.rtm.rtm_flags = RTF_UP | RTF_PROTO1;
209

    
210
  if (net->n.pxlen == MAX_PREFIX_LENGTH)
211
    msg.rtm.rtm_flags |= RTF_HOST;
212
  else
213
    msg.rtm.rtm_addrs |= RTA_NETMASK;
214

    
215
#ifdef KRT_SHARED_SOCKET
216
  msg.rtm.rtm_tableid = KRT_CF->sys.table_id;
217
#endif
218

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

    
228
  /* This is really very nasty, but I'm not able
229
   * to add "(reject|blackhole)" route without
230
   * gateway set
231
   */
232
  if(!i)
233
  {
234
    i = HEAD(iface_list);
235

    
236
    WALK_LIST(j, iface_list)
237
    {
238
      if (j->flags & IF_LOOPBACK)
239
      {
240
        i = j;
241
        break;
242
      }
243
    }
244
  }
245

    
246
  gw = a->gw;
247

    
248
#ifdef IPV6
249
  /* Embed interface ID to link-local address */
250
  if (ipa_has_link_scope(gw))
251
    _I0(gw) = 0xfe800000 | (i->index & 0x0000ffff);
252
#endif
253

    
254
  sockaddr_fill(&dst,  BIRD_AF, net->n.prefix, NULL, 0);
255
  sockaddr_fill(&mask, BIRD_AF, ipa_mkmask(net->n.pxlen), NULL, 0);
256
  sockaddr_fill(&gate, BIRD_AF, gw, NULL, 0);
257

    
258
  switch (a->dest)
259
  {
260
    case RTD_ROUTER:
261
      msg.rtm.rtm_flags |= RTF_GATEWAY;
262
      msg.rtm.rtm_addrs |= RTA_GATEWAY;
263
      break;
264
#ifdef RTF_REJECT
265
    case RTD_UNREACHABLE:
266
#endif
267
#ifdef RTF_BLACKHOLE
268
    case RTD_BLACKHOLE:
269
#endif
270
    case RTD_DEVICE:
271
      if(i)
272
      {
273
#ifdef RTF_CLONING
274
        if (cmd == RTM_ADD && (i->flags & IF_MULTIACCESS) != IF_MULTIACCESS)        /* PTP */
275
          msg.rtm.rtm_flags |= RTF_CLONING;
276
#endif
277

    
278
        if(!i->addr) {
279
          log(L_ERR "KRT: interface %s has no IP addess", i->name);
280
          return -1;
281
        }
282

    
283
        sockaddr_fill(&dst, BIRD_AF, i->addr->ip, NULL, 0);
284
        msg.rtm.rtm_addrs |= RTA_GATEWAY;
285
      }
286
      break;
287
    default:
288
      bug("krt-sock: unknown flags, but not filtered");
289
  }
290

    
291
  msg.rtm.rtm_index = i->index;
292

    
293
  NEXTADDR(RTA_DST, dst);
294
  NEXTADDR(RTA_GATEWAY, gate);
295
  NEXTADDR(RTA_NETMASK, mask);
296

    
297
  l = body - (char *)&msg;
298
  msg.rtm.rtm_msglen = l;
299

    
300
  if ((l = write(p->sys.sk->fd, (char *)&msg, l)) < 0) {
301
    log(L_ERR "KRT: Error sending route %I/%d to kernel: %m", net->n.prefix, net->n.pxlen);
302
    return -1;
303
  }
304

    
305
  return 0;
306
}
307

    
308
void
309
krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old,
310
                struct ea_list *eattrs UNUSED)
311
{
312
  int err = 0;
313

    
314
  if (old)
315
    krt_send_route(p, RTM_DELETE, old);
316

    
317
  if (new)
318
    err = krt_send_route(p, RTM_ADD, new);
319

    
320
  if (err < 0)
321
    n->n.flags |= KRF_SYNC_ERROR;
322
  else
323
    n->n.flags &= ~KRF_SYNC_ERROR;
324
}
325

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

    
328
static void
329
krt_read_route(struct ks_msg *msg, struct krt_proto *p, int scan)
330
{
331
  /* p is NULL iff KRT_SHARED_SOCKET and !scan */
332

    
333
  rte *e;
334
  net *net;
335
  sockaddr dst, gate, mask;
336
  ip_addr idst, igate, imask;
337
  void *body = (char *)msg->buf;
338
  int new = (msg->rtm.rtm_type == RTM_ADD);
339
  char *errmsg = "KRT: Invalid route received";
340
  int flags = msg->rtm.rtm_flags;
341
  int addrs = msg->rtm.rtm_addrs;
342
  int src;
343
  byte src2;
344

    
345
  if (!(flags & RTF_UP) && scan)
346
    SKIP("not up in scan\n");
347

    
348
  if (!(flags & RTF_DONE) && !scan)
349
    SKIP("not done in async\n");
350

    
351
  if (flags & RTF_LLINFO)
352
    SKIP("link-local\n");
353

    
354
#ifdef KRT_SHARED_SOCKET
355
  if (!scan)
356
  {
357
    int table_id = msg->rtm.rtm_tableid;
358
    p = (table_id < KRT_MAX_TABLES) ? krt_table_map[table_id] : NULL;
359

    
360
    if (!p)
361
      SKIP("unknown table id %d\n", table_id);
362
  }
363
#endif
364

    
365
  GETADDR(&dst, RTA_DST);
366
  GETADDR(&gate, RTA_GATEWAY);
367
  GETADDR(&mask, RTA_NETMASK);
368

    
369
  if (dst.sa.sa_family != BIRD_AF)
370
    SKIP("invalid DST");
371

    
372
  idst  = ipa_from_sa(&dst);
373
  imask = ipa_from_sa(&mask);
374
  igate = (gate.sa.sa_family == BIRD_AF) ? ipa_from_sa(&gate) : IPA_NONE;
375

    
376
  /* We do not test family for RTA_NETMASK, because BSD sends us
377
     some strange values, but interpreting them as IPv4/IPv6 works */
378

    
379

    
380
  int c = ipa_classify_net(idst);
381
  if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
382
    SKIP("strange class/scope\n");
383

    
384
  int pxlen = (flags & RTF_HOST) ? MAX_PREFIX_LENGTH : ipa_mklen(imask);
385
  if (pxlen < 0)
386
    { log(L_ERR "%s (%I) - netmask %I", errmsg, idst, imask); return; }
387

    
388
  if ((flags & RTF_GATEWAY) && ipa_zero(igate))
389
    { log(L_ERR "%s (%I/%d) - missing gateway", errmsg, idst, pxlen); return; }
390

    
391
  u32 self_mask = RTF_PROTO1;
392
  u32 alien_mask = RTF_STATIC | RTF_PROTO1 | RTF_GATEWAY;
393

    
394
  src2 = (flags & RTF_STATIC) ? 1 : 0;
395
  src2 |= (flags & RTF_PROTO1) ? 2 : 0;
396

    
397
#ifdef RTF_PROTO2
398
  alien_mask |= RTF_PROTO2;
399
  src2 |= (flags & RTF_PROTO2) ? 4 : 0;
400
#endif
401

    
402
#ifdef RTF_PROTO3
403
  alien_mask |= RTF_PROTO3;
404
  src2 |= (flags & RTF_PROTO3) ? 8 : 0;
405
#endif
406

    
407
#ifdef RTF_REJECT
408
  alien_mask |= RTF_REJECT;
409
#endif
410

    
411
#ifdef RTF_BLACKHOLE
412
  alien_mask |= RTF_BLACKHOLE;
413
#endif
414

    
415
  if (flags & (RTF_DYNAMIC | RTF_MODIFIED))
416
    src = KRT_SRC_REDIRECT;
417
  else if (flags & self_mask)
418
    {
419
      if (!scan)
420
        SKIP("echo\n");
421
      src = KRT_SRC_BIRD;
422
    }
423
  else if (flags & alien_mask)
424
    src = KRT_SRC_ALIEN;
425
  else
426
    src = KRT_SRC_KERNEL;
427

    
428
  net = net_get(p->p.table, idst, pxlen);
429

    
430
  rta a = {
431
    .src = p->p.main_source,
432
    .source = RTS_INHERIT,
433
    .scope = SCOPE_UNIVERSE,
434
    .cast = RTC_UNICAST
435
  };
436

    
437
  /* reject/blackhole routes have also set RTF_GATEWAY,
438
     we wil check them first. */
439

    
440
#ifdef RTF_REJECT
441
  if(flags & RTF_REJECT) {
442
    a.dest = RTD_UNREACHABLE;
443
    goto done;
444
  }
445
#endif
446

    
447
#ifdef RTF_BLACKHOLE
448
  if(flags & RTF_BLACKHOLE) {
449
    a.dest = RTD_BLACKHOLE;
450
    goto done;
451
  }
452
#endif
453

    
454
  a.iface = if_find_by_index(msg->rtm.rtm_index);
455
  if (!a.iface)
456
    {
457
      log(L_ERR "KRT: Received route %I/%d with unknown ifindex %u",
458
          net->n.prefix, net->n.pxlen, msg->rtm.rtm_index);
459
      return;
460
    }
461

    
462
  if (flags & RTF_GATEWAY)
463
  {
464
    neighbor *ng;
465
    a.dest = RTD_ROUTER;
466
    a.gw = igate;
467

    
468
#ifdef IPV6
469
    /* Clean up embedded interface ID returned in link-local address */
470
    if (ipa_has_link_scope(a.gw))
471
      _I0(a.gw) = 0xfe800000;
472
#endif
473

    
474
    ng = neigh_find2(&p->p, &a.gw, a.iface, 0);
475
    if (!ng || (ng->scope == SCOPE_HOST))
476
      {
477
        /* Ignore routes with next-hop 127.0.0.1, host routes with such
478
           next-hop appear on OpenBSD for address aliases. */
479
        if (ipa_classify(a.gw) == (IADDR_HOST | SCOPE_HOST))
480
          return;
481

    
482
        log(L_ERR "KRT: Received route %I/%d with strange next-hop %I",
483
            net->n.prefix, net->n.pxlen, a.gw);
484
        return;
485
      }
486
  }
487
  else
488
    a.dest = RTD_DEVICE;
489

    
490
 done:
491
  e = rte_get_temp(&a);
492
  e->net = net;
493
  e->u.krt.src = src;
494
  e->u.krt.proto = src2;
495

    
496
  /* These are probably too Linux-specific */
497
  e->u.krt.type = 0;
498
  e->u.krt.metric = 0;
499

    
500
  if (scan)
501
    krt_got_route(p, e);
502
  else
503
    krt_got_route_async(p, e, new);
504
}
505

    
506
static void
507
krt_read_ifannounce(struct ks_msg *msg)
508
{
509
  struct if_announcemsghdr *ifam = (struct if_announcemsghdr *)&msg->rtm;
510

    
511
  if (ifam->ifan_what == IFAN_ARRIVAL)
512
  {
513
    /* Not enough info to create the iface, so we just trigger iface scan */
514
    kif_request_scan();
515
  }
516
  else if (ifam->ifan_what == IFAN_DEPARTURE)
517
  {
518
    struct iface *iface = if_find_by_index(ifam->ifan_index);
519

    
520
    /* Interface is destroyed */
521
    if (!iface)
522
    {
523
      DBG("KRT: unknown interface (%s, #%d) going down. Ignoring\n", ifam->ifan_name, ifam->ifan_index);
524
      return;
525
    }
526

    
527
    if_delete(iface);
528
  }
529

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

    
533
static void
534
krt_read_ifinfo(struct ks_msg *msg, int scan)
535
{
536
  struct if_msghdr *ifm = (struct if_msghdr *)&msg->rtm;
537
  void *body = (void *)(ifm + 1);
538
  struct sockaddr_dl *dl = NULL;
539
  unsigned int i;
540
  struct iface *iface = NULL, f = {};
541
  int fl = ifm->ifm_flags;
542
  int nlen = 0;
543

    
544
  for (i = 1; i<=RTA_IFP; i <<= 1)
545
  {
546
    if (i & ifm->ifm_addrs)
547
    {
548
      if (i == RTA_IFP)
549
      {
550
        dl = (struct sockaddr_dl *)body;
551
        break;
552
      }
553
      body += ROUNDUP(((struct sockaddr *)&(body))->sa_len);
554
    }
555
  }
556

    
557
  if (dl && (dl->sdl_family != AF_LINK))
558
  {
559
    log(L_WARN "Ignoring strange IFINFO");
560
    return;
561
  }
562

    
563
  if (dl)
564
    nlen = MIN(sizeof(f.name)-1, dl->sdl_nlen);
565

    
566
  /* Note that asynchronous IFINFO messages do not contain iface
567
     name, so we have to found an existing iface by iface index */
568

    
569
  iface = if_find_by_index(ifm->ifm_index);
570
  if (!iface)
571
  {
572
    /* New interface */
573
    if (!dl)
574
      return;        /* No interface name, ignoring */
575

    
576
    memcpy(f.name, dl->sdl_data, nlen);
577
    DBG("New interface '%s' found\n", f.name);
578
  }
579
  else if (dl && memcmp(iface->name, dl->sdl_data, nlen))
580
  {
581
    /* Interface renamed */
582
    if_delete(iface);
583
    memcpy(f.name, dl->sdl_data, nlen);
584
  }
585
  else
586
  {
587
    /* Old interface */
588
    memcpy(f.name, iface->name, sizeof(f.name));
589
  }
590

    
591
  f.index = ifm->ifm_index;
592
  f.mtu = ifm->ifm_data.ifi_mtu;
593

    
594
  if (fl & IFF_UP)
595
    f.flags |= IF_ADMIN_UP;
596
  if (ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
597
    f.flags |= IF_LINK_UP;          /* up or unknown */
598
  if (fl & IFF_LOOPBACK)            /* Loopback */
599
    f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE;
600
  else if (fl & IFF_POINTOPOINT)    /* PtP */
601
    f.flags |= IF_MULTICAST;
602
  else if (fl & IFF_BROADCAST)      /* Broadcast */
603
    f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST;
604
  else
605
    f.flags |= IF_MULTIACCESS;      /* NBMA */
606

    
607
  iface = if_update(&f);
608

    
609
  if (!scan)
610
    if_end_partial_update(iface);
611
}
612

    
613
static void
614
krt_read_addr(struct ks_msg *msg, int scan)
615
{
616
  struct ifa_msghdr *ifam = (struct ifa_msghdr *)&msg->rtm;
617
  void *body = (void *)(ifam + 1);
618
  sockaddr addr, mask, brd;
619
  struct iface *iface = NULL;
620
  struct ifa ifa;
621
  struct sockaddr null;
622
  ip_addr iaddr, imask, ibrd;
623
  int addrs = ifam->ifam_addrs;
624
  int scope, masklen = -1;
625
  int new = (ifam->ifam_type == RTM_NEWADDR);
626

    
627
  /* Strange messages with zero (invalid) ifindex appear on OpenBSD */
628
  if (ifam->ifam_index == 0)
629
    return;
630

    
631
  if(!(iface = if_find_by_index(ifam->ifam_index)))
632
  {
633
    log(L_ERR "KIF: Received address message for unknown interface %d", ifam->ifam_index);
634
    return;
635
  }
636

    
637
  GETADDR (&null, RTA_DST);
638
  GETADDR (&null, RTA_GATEWAY);
639
  GETADDR (&mask, RTA_NETMASK);
640
  GETADDR (&null, RTA_GENMASK);
641
  GETADDR (&null, RTA_IFP);
642
  GETADDR (&addr, RTA_IFA);
643
  GETADDR (&null, RTA_AUTHOR);
644
  GETADDR (&brd, RTA_BRD);
645

    
646
  /* Some other family address */
647
  if (addr.sa.sa_family != BIRD_AF)
648
    return;
649

    
650
  iaddr = ipa_from_sa(&addr);
651
  imask = ipa_from_sa(&mask);
652
  ibrd  = ipa_from_sa(&brd);
653

    
654

    
655
  if ((masklen = ipa_mklen(imask)) < 0)
656
  {
657
    log(L_ERR "KIF: Invalid masklen %I for %s", imask, iface->name);
658
    return;
659
  }
660

    
661
#ifdef IPV6
662
  /* Clean up embedded interface ID returned in link-local address */
663

    
664
  if (ipa_has_link_scope(iaddr))
665
    _I0(iaddr) = 0xfe800000;
666

    
667
  if (ipa_has_link_scope(ibrd))
668
    _I0(ibrd) = 0xfe800000;
669
#endif
670

    
671

    
672
  bzero(&ifa, sizeof(ifa));
673
  ifa.iface = iface;
674
  ifa.ip = iaddr;
675
  ifa.pxlen = masklen;
676

    
677
  scope = ipa_classify(ifa.ip);
678
  if (scope < 0)
679
  {
680
    log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, iface->name);
681
    return;
682
  }
683
  ifa.scope = scope & IADDR_SCOPE_MASK;
684

    
685
  if (masklen < BITS_PER_IP_ADDRESS)
686
  {
687
    ifa.prefix = ipa_and(ifa.ip, ipa_mkmask(masklen));
688

    
689
    if (masklen == (BITS_PER_IP_ADDRESS - 1))
690
      ifa.opposite = ipa_opposite_m1(ifa.ip);
691

    
692
#ifndef IPV6
693
    if (masklen == (BITS_PER_IP_ADDRESS - 2))
694
      ifa.opposite = ipa_opposite_m2(ifa.ip);
695
#endif
696

    
697
    if (iface->flags & IF_BROADCAST)
698
      ifa.brd = ibrd;
699

    
700
    if (!(iface->flags & IF_MULTIACCESS))
701
      ifa.opposite = ibrd;
702
  }
703
  else if (!(iface->flags & IF_MULTIACCESS) && ipa_nonzero(ibrd))
704
  {
705
    ifa.prefix = ifa.opposite = ibrd;
706
    ifa.flags |= IA_PEER;
707
  }
708
  else
709
  {
710
    ifa.prefix = ifa.ip;
711
    ifa.flags |= IA_HOST;
712
  }
713

    
714
  if (new)
715
    ifa_update(&ifa);
716
  else
717
    ifa_delete(&ifa);
718

    
719
  if (!scan)
720
    if_end_partial_update(iface);
721
}
722

    
723
static void
724
krt_read_msg(struct proto *p, struct ks_msg *msg, int scan)
725
{
726
  /* p is NULL iff KRT_SHARED_SOCKET and !scan */
727

    
728
  switch (msg->rtm.rtm_type)
729
  {
730
    case RTM_GET:
731
      if(!scan) return;
732
    case RTM_ADD:
733
    case RTM_DELETE:
734
      krt_read_route(msg, (struct krt_proto *)p, scan);
735
      break;
736
    case RTM_IFANNOUNCE:
737
      krt_read_ifannounce(msg);
738
      break;
739
    case RTM_IFINFO:
740
      krt_read_ifinfo(msg, scan);
741
      break;
742
    case RTM_NEWADDR:
743
    case RTM_DELADDR:
744
      krt_read_addr(msg, scan);
745
      break;
746
    default:
747
      break;
748
  }
749
}
750

    
751

    
752
/* Sysctl based scans */
753

    
754
static byte *krt_buffer;
755
static size_t krt_buflen, krt_bufmin;
756
static struct proto *krt_buffer_owner;
757

    
758
static byte *
759
krt_buffer_update(struct proto *p, size_t *needed)
760
{
761
  size_t req = *needed;
762

    
763
  if ((req > krt_buflen) ||
764
      ((p == krt_buffer_owner) && (req < krt_bufmin)))
765
  {
766
    /* min buflen is 32 kB, step is 8 kB, or 128 kB if > 1 MB */
767
    size_t step = (req < 0x100000) ? 0x2000 : 0x20000;
768
    krt_buflen = (req < 0x6000) ? 0x8000 : (req + step);
769
    krt_bufmin = (req < 0x8000) ? 0 : (req - 2*step);
770

    
771
    if (krt_buffer) 
772
      mb_free(krt_buffer);
773
    krt_buffer = mb_alloc(krt_pool, krt_buflen);
774
    krt_buffer_owner = p;
775
  }
776

    
777
  *needed = krt_buflen;
778
  return krt_buffer;
779
}
780

    
781
static void
782
krt_buffer_release(struct proto *p)
783
{
784
  if (p == krt_buffer_owner)
785
  {
786
    mb_free(krt_buffer);
787
    krt_buffer = NULL;
788
    krt_buflen = 0;
789
    krt_buffer_owner = 0;
790
  }
791
}
792

    
793
static void
794
krt_sysctl_scan(struct proto *p, int cmd, int table_id)
795
{
796
  byte *buf, *next;
797
  int mib[7], mcnt;
798
  size_t needed;
799
  struct ks_msg *m;
800
  int retries = 3;
801
  int rv;
802

    
803
  mib[0] = CTL_NET;
804
  mib[1] = PF_ROUTE;
805
  mib[2] = 0;
806
  mib[3] = BIRD_AF;
807
  mib[4] = cmd;
808
  mib[5] = 0;
809
  mcnt = 6;
810

    
811
#ifdef KRT_USE_SYSCTL_7
812
  if (table_id >= 0)
813
  {
814
    mib[6] = table_id;
815
    mcnt = 7;
816
  }
817
#endif
818

    
819
#ifdef KRT_USE_SETFIB_SCAN
820
  if (table_id > 0)
821
    if (setfib(table_id) < 0)
822
    {
823
      log(L_ERR "KRT: setfib(%d) failed: %m", table_id);
824
      return;
825
    }
826
#endif
827

    
828
 try:
829
  rv = sysctl(mib, mcnt, NULL, &needed, NULL, 0);
830
  if (rv < 0)
831
  {
832
    /* OpenBSD returns EINVAL for not yet used tables */
833
    if ((errno == EINVAL) && (table_id > 0))
834
      goto exit;
835

    
836
    log(L_ERR "KRT: Route scan estimate failed: %m");
837
    goto exit;
838
  }
839

    
840
  /* The table is empty */
841
  if (needed == 0)
842
    goto exit;
843

    
844
  buf = krt_buffer_update(p, &needed);
845

    
846
  rv = sysctl(mib, mcnt, buf, &needed, NULL, 0);
847
  if (rv < 0)
848
  {
849
    /* The buffer size changed since last sysctl ('needed' is not changed) */
850
    if ((errno == ENOMEM) && retries--)
851
      goto try;
852

    
853
    log(L_ERR "KRT: Route scan failed: %m");
854
    goto exit;
855
  }
856

    
857
#ifdef KRT_USE_SETFIB_SCAN
858
  if (table_id > 0)
859
    if (setfib(0) < 0)
860
      die("KRT: setfib(%d) failed: %m", 0);
861
#endif
862

    
863
  /* Process received messages */
864
  for (next = buf; next < (buf + needed); next += m->rtm.rtm_msglen)
865
  {
866
    m = (struct ks_msg *)next;
867
    krt_read_msg(p, m, 1);
868
  }
869

    
870
  return;
871

    
872
 exit:
873
  krt_buffer_release(p);
874

    
875
#ifdef KRT_USE_SETFIB_SCAN
876
  if (table_id > 0)
877
    if (setfib(0) < 0)
878
      die("KRT: setfib(%d) failed: %m", 0);
879
#endif
880
}
881

    
882
void
883
krt_do_scan(struct krt_proto *p)
884
{
885
  krt_sysctl_scan(&p->p, NET_RT_DUMP, KRT_CF->sys.table_id);
886
}
887

    
888
void
889
kif_do_scan(struct kif_proto *p)
890
{
891
  if_start_update();
892
  krt_sysctl_scan(&p->p, NET_RT_IFLIST, -1);
893
  if_end_update();
894
}
895

    
896

    
897
/* Kernel sockets */
898

    
899
static int
900
krt_sock_hook(sock *sk, int size UNUSED)
901
{
902
  struct ks_msg msg;
903
  int l = read(sk->fd, (char *)&msg, sizeof(msg));
904

    
905
  if (l <= 0)
906
    log(L_ERR "krt-sock: read failed");
907
  else
908
    krt_read_msg((struct proto *) sk->data, &msg, 0);
909

    
910
  return 0;
911
}
912

    
913
static sock *
914
krt_sock_open(pool *pool, void *data, int table_id)
915
{
916
  sock *sk;
917
  int fd;
918

    
919
  fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
920
  if (fd < 0)
921
    die("Cannot open kernel socket for routes");
922

    
923
#ifdef KRT_USE_SETFIB_SOCK
924
  if (table_id > 0)
925
  {
926
    if (setsockopt(fd, SOL_SOCKET, SO_SETFIB, &table_id, sizeof(table_id)) < 0)
927
      die("Cannot set FIB %d for kernel socket: %m", table_id);
928
  }
929
#endif
930

    
931
  sk = sk_new(pool);
932
  sk->type = SK_MAGIC;
933
  sk->rx_hook = krt_sock_hook;
934
  sk->fd = fd;
935
  sk->data = data;
936

    
937
  if (sk_open(sk) < 0)
938
    bug("krt-sock: sk_open failed");
939

    
940
  return sk;
941
}
942

    
943

    
944
#ifdef KRT_SHARED_SOCKET
945

    
946
static sock *krt_sock;
947
static int krt_sock_count;
948

    
949

    
950
static void
951
krt_sock_open_shared(void)
952
{
953
  if (!krt_sock_count)
954
    krt_sock = krt_sock_open(krt_pool, NULL, -1);
955
  
956
  krt_sock_count++;
957
}
958

    
959
static void
960
krt_sock_close_shared(void)
961
{
962
  krt_sock_count--;
963

    
964
  if (!krt_sock_count)
965
  {
966
    rfree(krt_sock);
967
    krt_sock = NULL;
968
  }
969
}
970

    
971
void
972
krt_sys_start(struct krt_proto *p)
973
{
974
  krt_table_map[KRT_CF->sys.table_id] = p;
975

    
976
  krt_sock_open_shared();
977
  p->sys.sk = krt_sock;
978
}
979

    
980
void
981
krt_sys_shutdown(struct krt_proto *p)
982
{
983
  krt_sock_close_shared();
984
  p->sys.sk = NULL;
985

    
986
  krt_table_map[KRT_CF->sys.table_id] = NULL;
987

    
988
  krt_buffer_release(&p->p);
989
}
990

    
991
#else
992

    
993
void
994
krt_sys_start(struct krt_proto *p)
995
{
996
  p->sys.sk = krt_sock_open(p->p.pool, p, KRT_CF->sys.table_id);
997
}
998

    
999
void
1000
krt_sys_shutdown(struct krt_proto *p)
1001
{
1002
  rfree(p->sys.sk);
1003
  p->sys.sk = NULL;
1004

    
1005
  krt_buffer_release(&p->p);
1006
}
1007

    
1008
#endif /* KRT_SHARED_SOCKET */
1009

    
1010

    
1011
/* KRT configuration callbacks */
1012

    
1013
static u32 krt_table_cf[(KRT_MAX_TABLES+31) / 32];
1014

    
1015
int
1016
krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
1017
{
1018
  return n->sys.table_id == o->sys.table_id;
1019
}
1020

    
1021
void
1022
krt_sys_preconfig(struct config *c UNUSED)
1023
{
1024
  krt_max_tables = krt_get_max_tables();
1025
  bzero(&krt_table_cf, sizeof(krt_table_cf));
1026
}
1027

    
1028
void
1029
krt_sys_postconfig(struct krt_config *x)
1030
{
1031
  u32 *tbl = krt_table_cf;
1032
  int id = x->sys.table_id;
1033

    
1034
  if (tbl[id/32] & (1 << (id%32)))
1035
    cf_error("Multiple kernel syncers defined for table #%d", id);
1036

    
1037
  tbl[id/32] |= (1 << (id%32));
1038
}
1039

    
1040
void krt_sys_init_config(struct krt_config *c)
1041
{
1042
  c->sys.table_id = 0; /* Default table */
1043
}
1044

    
1045
void krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
1046
{
1047
  d->sys.table_id = s->sys.table_id;
1048
}
1049

    
1050

    
1051
/* KIF misc code */
1052

    
1053
void
1054
kif_sys_start(struct kif_proto *p UNUSED)
1055
{
1056
}
1057

    
1058
void
1059
kif_sys_shutdown(struct kif_proto *p)
1060
{
1061
  krt_buffer_release(&p->p);
1062
}
1063

    
1064

    
1065
struct ifa *
1066
kif_get_primary_ip(struct iface *i)
1067
{
1068
#ifndef IPV6
1069
  static int fd = -1;
1070
  
1071
  if (fd < 0)
1072
    fd = socket(AF_INET, SOCK_DGRAM, 0);
1073

    
1074
  struct ifreq ifr;
1075
  memset(&ifr, 0, sizeof(ifr));
1076
  strncpy(ifr.ifr_name, i->name, IFNAMSIZ);
1077

    
1078
  int rv = ioctl(fd, SIOCGIFADDR, (char *) &ifr);
1079
  if (rv < 0)
1080
    return NULL;
1081

    
1082
  ip_addr addr;
1083
  struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
1084
  memcpy(&addr, &sin->sin_addr.s_addr, sizeof(ip_addr));
1085
  ipa_ntoh(addr);
1086

    
1087
  struct ifa *a;
1088
  WALK_LIST(a, i->addrs)
1089
  {
1090
    if (ipa_equal(a->ip, addr))
1091
      return a;
1092
  }
1093
#endif
1094

    
1095
  return NULL;
1096
}