]> git.ipfire.org Git - thirdparty/bird.git/blob - sysdep/linux/netlink.c
Makes krt.c much more readable.
[thirdparty/bird.git] / sysdep / linux / netlink.c
1 /*
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 #include <stdio.h>
10 #include <fcntl.h>
11 #include <sys/socket.h>
12 #include <sys/uio.h>
13 #include <errno.h>
14
15 #undef LOCAL_DEBUG
16
17 #include "nest/bird.h"
18 #include "nest/route.h"
19 #include "nest/protocol.h"
20 #include "nest/iface.h"
21 #include "lib/alloca.h"
22 #include "lib/timer.h"
23 #include "lib/unix.h"
24 #include "lib/krt.h"
25 #include "lib/socket.h"
26 #include "lib/string.h"
27 #include "conf/conf.h"
28
29 #include <asm/types.h>
30 #include <linux/if.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
33
34 #ifndef MSG_TRUNC /* Hack: Several versions of glibc miss this one :( */
35 #define MSG_TRUNC 0x20
36 #endif
37
38 #ifndef IFF_LOWER_UP
39 #define IFF_LOWER_UP 0x10000
40 #endif
41
42 /*
43 * Synchronous Netlink interface
44 */
45
46 struct nl_sock
47 {
48 int fd;
49 u32 seq;
50 byte *rx_buffer; /* Receive buffer */
51 struct nlmsghdr *last_hdr; /* Recently received packet */
52 unsigned int last_size;
53 };
54
55 #define NL_RX_SIZE 8192
56
57 static struct nl_sock nl_scan = {.fd = -1}; /* Netlink socket for synchronous scan */
58 static struct nl_sock nl_req = {.fd = -1}; /* Netlink socket for requests */
59
60 static void
61 nl_open_sock(struct nl_sock *nl)
62 {
63 if (nl->fd < 0)
64 {
65 nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
66 if (nl->fd < 0)
67 die("Unable to open rtnetlink socket: %m");
68 nl->seq = now;
69 nl->rx_buffer = xmalloc(NL_RX_SIZE);
70 nl->last_hdr = NULL;
71 nl->last_size = 0;
72 }
73 }
74
75 static void
76 nl_open(void)
77 {
78 nl_open_sock(&nl_scan);
79 nl_open_sock(&nl_req);
80 }
81
82 static void
83 nl_send(struct nl_sock *nl, struct nlmsghdr *nh)
84 {
85 struct sockaddr_nl sa;
86
87 memset(&sa, 0, sizeof(sa));
88 sa.nl_family = AF_NETLINK;
89 nh->nlmsg_pid = 0;
90 nh->nlmsg_seq = ++(nl->seq);
91 if (sendto(nl->fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0)
92 die("rtnetlink sendto: %m");
93 nl->last_hdr = NULL;
94 }
95
96 static void
97 nl_request_dump(int cmd)
98 {
99 struct {
100 struct nlmsghdr nh;
101 struct rtgenmsg g;
102 } req;
103 req.nh.nlmsg_type = cmd;
104 req.nh.nlmsg_len = sizeof(req);
105 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
106 /* Is it important which PF_* is used for link-level interface scan?
107 It seems that some information is available only when PF_INET is used. */
108 req.g.rtgen_family = (cmd == RTM_GETLINK) ? PF_INET : BIRD_PF;
109 nl_send(&nl_scan, &req.nh);
110 }
111
112 static struct nlmsghdr *
113 nl_get_reply(struct nl_sock *nl)
114 {
115 for(;;)
116 {
117 if (!nl->last_hdr)
118 {
119 struct iovec iov = { nl->rx_buffer, NL_RX_SIZE };
120 struct sockaddr_nl sa;
121 struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
122 int x = recvmsg(nl->fd, &m, 0);
123 if (x < 0)
124 die("nl_get_reply: %m");
125 if (sa.nl_pid) /* It isn't from the kernel */
126 {
127 DBG("Non-kernel packet\n");
128 continue;
129 }
130 nl->last_size = x;
131 nl->last_hdr = (void *) nl->rx_buffer;
132 if (m.msg_flags & MSG_TRUNC)
133 bug("nl_get_reply: got truncated reply which should be impossible");
134 }
135 if (NLMSG_OK(nl->last_hdr, nl->last_size))
136 {
137 struct nlmsghdr *h = nl->last_hdr;
138 nl->last_hdr = NLMSG_NEXT(h, nl->last_size);
139 if (h->nlmsg_seq != nl->seq)
140 {
141 log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)",
142 h->nlmsg_seq, nl->seq);
143 continue;
144 }
145 return h;
146 }
147 if (nl->last_size)
148 log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl->last_size);
149 nl->last_hdr = NULL;
150 }
151 }
152
153 static struct rate_limit rl_netlink_err;
154
155 static int
156 nl_error(struct nlmsghdr *h)
157 {
158 struct nlmsgerr *e;
159 int ec;
160
161 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
162 {
163 log(L_WARN "Netlink: Truncated error message received");
164 return ENOBUFS;
165 }
166 e = (struct nlmsgerr *) NLMSG_DATA(h);
167 ec = -e->error;
168 if (ec)
169 log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec));
170 return ec;
171 }
172
173 static struct nlmsghdr *
174 nl_get_scan(void)
175 {
176 struct nlmsghdr *h = nl_get_reply(&nl_scan);
177
178 if (h->nlmsg_type == NLMSG_DONE)
179 return NULL;
180 if (h->nlmsg_type == NLMSG_ERROR)
181 {
182 nl_error(h);
183 return NULL;
184 }
185 return h;
186 }
187
188 static int
189 nl_exchange(struct nlmsghdr *pkt)
190 {
191 struct nlmsghdr *h;
192
193 nl_send(&nl_req, pkt);
194 for(;;)
195 {
196 h = nl_get_reply(&nl_req);
197 if (h->nlmsg_type == NLMSG_ERROR)
198 break;
199 log(L_WARN "nl_exchange: Unexpected reply received");
200 }
201 return nl_error(h) ? -1 : 0;
202 }
203
204 /*
205 * Netlink attributes
206 */
207
208 static int nl_attr_len;
209
210 static void *
211 nl_checkin(struct nlmsghdr *h, int lsize)
212 {
213 nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize);
214 if (nl_attr_len < 0)
215 {
216 log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len);
217 return NULL;
218 }
219 return NLMSG_DATA(h);
220 }
221
222 static int
223 nl_parse_attrs(struct rtattr *a, struct rtattr **k, int ksize)
224 {
225 int max = ksize / sizeof(struct rtattr *);
226 bzero(k, ksize);
227 while (RTA_OK(a, nl_attr_len))
228 {
229 if (a->rta_type < max)
230 k[a->rta_type] = a;
231 a = RTA_NEXT(a, nl_attr_len);
232 }
233 if (nl_attr_len)
234 {
235 log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len);
236 return 0;
237 }
238 else
239 return 1;
240 }
241
242 void
243 nl_add_attr(struct nlmsghdr *h, unsigned bufsize, unsigned code,
244 void *data, unsigned dlen)
245 {
246 unsigned len = RTA_LENGTH(dlen);
247 unsigned pos = NLMSG_ALIGN(h->nlmsg_len);
248 struct rtattr *a;
249
250 if (pos + len > bufsize)
251 bug("nl_add_attr: packet buffer overflow");
252 a = (struct rtattr *)((char *)h + pos);
253 a->rta_type = code;
254 a->rta_len = len;
255 h->nlmsg_len = pos + len;
256 memcpy(RTA_DATA(a), data, dlen);
257 }
258
259 static inline void
260 nl_add_attr_u32(struct nlmsghdr *h, unsigned bufsize, int code, u32 data)
261 {
262 nl_add_attr(h, bufsize, code, &data, 4);
263 }
264
265 static inline void
266 nl_add_attr_ipa(struct nlmsghdr *h, unsigned bufsize, int code, ip_addr ipa)
267 {
268 ipa_hton(ipa);
269 nl_add_attr(h, bufsize, code, &ipa, sizeof(ipa));
270 }
271
272 #define RTNH_SIZE (sizeof(struct rtnexthop) + sizeof(struct rtattr) + sizeof(ip_addr))
273
274 static inline void
275 add_mpnexthop(char *buf, ip_addr ipa, unsigned iface, unsigned char weight)
276 {
277 struct rtnexthop *nh = (void *) buf;
278 struct rtattr *rt = (void *) (buf + sizeof(*nh));
279 nh->rtnh_len = RTNH_SIZE;
280 nh->rtnh_flags = 0;
281 nh->rtnh_hops = weight;
282 nh->rtnh_ifindex = iface;
283 rt->rta_len = sizeof(*rt) + sizeof(ipa);
284 rt->rta_type = RTA_GATEWAY;
285 ipa_hton(ipa);
286 memcpy(buf + sizeof(*nh) + sizeof(*rt), &ipa, sizeof(ipa));
287 }
288
289
290 static void
291 nl_add_multipath(struct nlmsghdr *h, unsigned bufsize, struct mpnh *nh)
292 {
293 unsigned len = sizeof(struct rtattr);
294 unsigned pos = NLMSG_ALIGN(h->nlmsg_len);
295 char *buf = (char *)h + pos;
296 struct rtattr *rt = (void *) buf;
297 buf += len;
298
299 for (; nh; nh = nh->next)
300 {
301 len += RTNH_SIZE;
302 if (pos + len > bufsize)
303 bug("nl_add_multipath: packet buffer overflow");
304
305 add_mpnexthop(buf, nh->gw, nh->iface->index, nh->weight);
306 buf += RTNH_SIZE;
307 }
308
309 rt->rta_type = RTA_MULTIPATH;
310 rt->rta_len = len;
311 h->nlmsg_len = pos + len;
312 }
313
314
315 static struct mpnh *
316 nl_parse_multipath(struct krt_proto *p, struct rtattr *ra)
317 {
318 /* Temporary buffer for multicast nexthops */
319 static struct mpnh *nh_buffer;
320 static int nh_buf_size; /* in number of structures */
321 static int nh_buf_used;
322
323 struct rtattr *a[RTA_CACHEINFO+1];
324 struct rtnexthop *nh = RTA_DATA(ra);
325 struct mpnh *rv, *first, **last;
326 int len = RTA_PAYLOAD(ra);
327
328 first = NULL;
329 last = &first;
330 nh_buf_used = 0;
331
332 while (len)
333 {
334 /* Use RTNH_OK(nh,len) ?? */
335 if ((len < sizeof(*nh)) || (len < nh->rtnh_len))
336 return NULL;
337
338 if (nh_buf_used == nh_buf_size)
339 {
340 nh_buf_size = nh_buf_size ? (nh_buf_size * 2) : 4;
341 nh_buffer = xrealloc(nh_buffer, nh_buf_size * sizeof(struct mpnh));
342 }
343 *last = rv = nh_buffer + nh_buf_used++;
344 rv->next = NULL;
345 last = &(rv->next);
346
347 rv->weight = nh->rtnh_hops;
348 rv->iface = if_find_by_index(nh->rtnh_ifindex);
349 if (!rv->iface)
350 return NULL;
351
352 /* Nonexistent RTNH_PAYLOAD ?? */
353 nl_attr_len = nh->rtnh_len - RTNH_LENGTH(0);
354 nl_parse_attrs(RTNH_DATA(nh), a, sizeof(a));
355 if (a[RTA_GATEWAY])
356 {
357 if (RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr))
358 return NULL;
359
360 memcpy(&rv->gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ip_addr));
361 ipa_ntoh(rv->gw);
362
363 neighbor *ng = neigh_find2(&p->p, &rv->gw, rv->iface,
364 (nh->rtnh_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0);
365 if (!ng || (ng->scope == SCOPE_HOST))
366 return NULL;
367 }
368 else
369 return NULL;
370
371 len -= NLMSG_ALIGN(nh->rtnh_len);
372 nh = RTNH_NEXT(nh);
373 }
374
375 return first;
376 }
377
378
379 /*
380 * Scanning of interfaces
381 */
382
383 static void
384 nl_parse_link(struct nlmsghdr *h, int scan)
385 {
386 struct ifinfomsg *i;
387 struct rtattr *a[IFLA_WIRELESS+1];
388 int new = h->nlmsg_type == RTM_NEWLINK;
389 struct iface f = {};
390 struct iface *ifi;
391 char *name;
392 u32 mtu;
393 unsigned int fl;
394
395 if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), a, sizeof(a)))
396 return;
397 if (!a[IFLA_IFNAME] || RTA_PAYLOAD(a[IFLA_IFNAME]) < 2 ||
398 !a[IFLA_MTU] || RTA_PAYLOAD(a[IFLA_MTU]) != 4)
399 {
400 if (scan || !a[IFLA_WIRELESS])
401 log(L_ERR "nl_parse_link: Malformed message received");
402 return;
403 }
404 name = RTA_DATA(a[IFLA_IFNAME]);
405 memcpy(&mtu, RTA_DATA(a[IFLA_MTU]), sizeof(u32));
406
407 ifi = if_find_by_index(i->ifi_index);
408 if (!new)
409 {
410 DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name);
411 if (!ifi)
412 return;
413
414 if_delete(ifi);
415 }
416 else
417 {
418 DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags);
419 if (ifi && strncmp(ifi->name, name, sizeof(ifi->name)-1))
420 if_delete(ifi);
421
422 strncpy(f.name, name, sizeof(f.name)-1);
423 f.index = i->ifi_index;
424 f.mtu = mtu;
425
426 fl = i->ifi_flags;
427 if (fl & IFF_UP)
428 f.flags |= IF_ADMIN_UP;
429 if (fl & IFF_LOWER_UP)
430 f.flags |= IF_LINK_UP;
431 if (fl & IFF_LOOPBACK) /* Loopback */
432 f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE;
433 else if (fl & IFF_POINTOPOINT) /* PtP */
434 f.flags |= IF_MULTICAST;
435 else if (fl & IFF_BROADCAST) /* Broadcast */
436 f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST;
437 else
438 f.flags |= IF_MULTIACCESS; /* NBMA */
439 if_update(&f);
440 }
441 }
442
443 static void
444 nl_parse_addr(struct nlmsghdr *h)
445 {
446 struct ifaddrmsg *i;
447 struct rtattr *a[IFA_ANYCAST+1];
448 int new = h->nlmsg_type == RTM_NEWADDR;
449 struct ifa ifa;
450 struct iface *ifi;
451 int scope;
452
453 if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFA_RTA(i), a, sizeof(a)))
454 return;
455 if (i->ifa_family != BIRD_AF)
456 return;
457 if (!a[IFA_ADDRESS] || RTA_PAYLOAD(a[IFA_ADDRESS]) != sizeof(ip_addr)
458 #ifdef IPV6
459 || a[IFA_LOCAL] && RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr)
460 #else
461 || !a[IFA_LOCAL] || RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr)
462 || (a[IFA_BROADCAST] && RTA_PAYLOAD(a[IFA_BROADCAST]) != sizeof(ip_addr))
463 #endif
464 )
465 {
466 log(L_ERR "nl_parse_addr: Malformed message received");
467 return;
468 }
469
470 ifi = if_find_by_index(i->ifa_index);
471 if (!ifi)
472 {
473 log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index);
474 return;
475 }
476
477 bzero(&ifa, sizeof(ifa));
478 ifa.iface = ifi;
479 if (i->ifa_flags & IFA_F_SECONDARY)
480 ifa.flags |= IA_SECONDARY;
481
482 /* IFA_LOCAL can be unset for IPv6 interfaces */
483 memcpy(&ifa.ip, RTA_DATA(a[IFA_LOCAL] ? : a[IFA_ADDRESS]), sizeof(ifa.ip));
484 ipa_ntoh(ifa.ip);
485 ifa.pxlen = i->ifa_prefixlen;
486 if (i->ifa_prefixlen > BITS_PER_IP_ADDRESS)
487 {
488 log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen);
489 new = 0;
490 }
491 if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS)
492 {
493 ip_addr addr;
494 memcpy(&addr, RTA_DATA(a[IFA_ADDRESS]), sizeof(addr));
495 ipa_ntoh(addr);
496 ifa.prefix = ifa.brd = addr;
497
498 /* It is either a host address or a peer address */
499 if (ipa_equal(ifa.ip, addr))
500 ifa.flags |= IA_HOST;
501 else
502 {
503 ifa.flags |= IA_PEER;
504 ifa.opposite = addr;
505 }
506 }
507 else
508 {
509 ip_addr netmask = ipa_mkmask(ifa.pxlen);
510 ifa.prefix = ipa_and(ifa.ip, netmask);
511 ifa.brd = ipa_or(ifa.ip, ipa_not(netmask));
512 if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 1)
513 ifa.opposite = ipa_opposite_m1(ifa.ip);
514
515 #ifndef IPV6
516 if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 2)
517 ifa.opposite = ipa_opposite_m2(ifa.ip);
518
519 if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST])
520 {
521 ip_addr xbrd;
522 memcpy(&xbrd, RTA_DATA(a[IFA_BROADCAST]), sizeof(xbrd));
523 ipa_ntoh(xbrd);
524 if (ipa_equal(xbrd, ifa.prefix) || ipa_equal(xbrd, ifa.brd))
525 ifa.brd = xbrd;
526 else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */
527 log(L_ERR "KIF: Invalid broadcast address %I for %s", xbrd, ifi->name);
528 }
529 #endif
530 }
531
532 scope = ipa_classify(ifa.ip);
533 if (scope < 0)
534 {
535 log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name);
536 return;
537 }
538 ifa.scope = scope & IADDR_SCOPE_MASK;
539
540 DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %I/%d, brd %I, opp %I\n",
541 ifi->index, ifi->name,
542 new ? "added" : "removed",
543 ifa.ip, ifa.flags, ifa.prefix, ifa.pxlen, ifa.brd, ifa.opposite);
544 if (new)
545 ifa_update(&ifa);
546 else
547 ifa_delete(&ifa);
548 }
549
550 void
551 kif_do_scan(struct kif_proto *p UNUSED)
552 {
553 struct nlmsghdr *h;
554
555 if_start_update();
556
557 nl_request_dump(RTM_GETLINK);
558 while (h = nl_get_scan())
559 if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)
560 nl_parse_link(h, 1);
561 else
562 log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
563
564 nl_request_dump(RTM_GETADDR);
565 while (h = nl_get_scan())
566 if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR)
567 nl_parse_addr(h);
568 else
569 log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type);
570
571 if_end_update();
572 }
573
574 /*
575 * Routes
576 */
577
578 static struct krt_proto *nl_table_map[NL_NUM_TABLES];
579
580 int
581 krt_capable(rte *e)
582 {
583 rta *a = e->attrs;
584
585 if (a->cast != RTC_UNICAST)
586 return 0;
587
588 switch (a->dest)
589 {
590 case RTD_ROUTER:
591 case RTD_DEVICE:
592 if (a->iface == NULL)
593 return 0;
594 case RTD_BLACKHOLE:
595 case RTD_UNREACHABLE:
596 case RTD_PROHIBIT:
597 case RTD_MULTIPATH:
598 break;
599 default:
600 return 0;
601 }
602 return 1;
603 }
604
605 static inline int
606 nh_bufsize(struct mpnh *nh)
607 {
608 int rv = 0;
609 for (; nh != NULL; nh = nh->next)
610 rv += RTNH_SIZE;
611 return rv;
612 }
613
614 static int
615 nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new)
616 {
617 eattr *ea;
618 net *net = e->net;
619 rta *a = e->attrs;
620 struct {
621 struct nlmsghdr h;
622 struct rtmsg r;
623 char buf[128 + nh_bufsize(a->nexthops)];
624 } r;
625
626 DBG("nl_send_route(%I/%d,new=%d)\n", net->n.prefix, net->n.pxlen, new);
627
628 bzero(&r.h, sizeof(r.h));
629 bzero(&r.r, sizeof(r.r));
630 r.h.nlmsg_type = new ? RTM_NEWROUTE : RTM_DELROUTE;
631 r.h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
632 r.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | (new ? NLM_F_CREATE|NLM_F_EXCL : 0);
633
634 r.r.rtm_family = BIRD_AF;
635 r.r.rtm_dst_len = net->n.pxlen;
636 r.r.rtm_tos = 0;
637 r.r.rtm_table = KRT_CF->sys.table_id;
638 r.r.rtm_protocol = RTPROT_BIRD;
639 r.r.rtm_scope = RT_SCOPE_UNIVERSE;
640 nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix);
641
642 u32 metric = 0;
643 if (new && e->attrs->source == RTS_INHERIT)
644 metric = e->u.krt.metric;
645 if (ea = ea_find(eattrs, EA_KRT_METRIC))
646 metric = ea->u.data;
647 if (metric != 0)
648 nl_add_attr_u32(&r.h, sizeof(r), RTA_PRIORITY, metric);
649
650 if (ea = ea_find(eattrs, EA_KRT_PREFSRC))
651 nl_add_attr_ipa(&r.h, sizeof(r), RTA_PREFSRC, *(ip_addr *)ea->u.ptr->data);
652
653 if (ea = ea_find(eattrs, EA_KRT_REALM))
654 nl_add_attr_u32(&r.h, sizeof(r), RTA_FLOW, ea->u.data);
655
656 /* a->iface != NULL checked in krt_capable() for router and device routes */
657
658 switch (a->dest)
659 {
660 case RTD_ROUTER:
661 r.r.rtm_type = RTN_UNICAST;
662 nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index);
663 nl_add_attr_ipa(&r.h, sizeof(r), RTA_GATEWAY, a->gw);
664 break;
665 case RTD_DEVICE:
666 r.r.rtm_type = RTN_UNICAST;
667 nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index);
668 break;
669 case RTD_BLACKHOLE:
670 r.r.rtm_type = RTN_BLACKHOLE;
671 break;
672 case RTD_UNREACHABLE:
673 r.r.rtm_type = RTN_UNREACHABLE;
674 break;
675 case RTD_PROHIBIT:
676 r.r.rtm_type = RTN_PROHIBIT;
677 break;
678 case RTD_MULTIPATH:
679 r.r.rtm_type = RTN_UNICAST;
680 nl_add_multipath(&r.h, sizeof(r), a->nexthops);
681 break;
682 default:
683 bug("krt_capable inconsistent with nl_send_route");
684 }
685
686 return nl_exchange(&r.h);
687 }
688
689 void
690 krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs)
691 {
692 int err = 0;
693
694 /*
695 * NULL for eattr of the old route is a little hack, but we don't
696 * get proper eattrs for old in rt_notify() anyway. NULL means no
697 * extended route attributes and therefore matches if the kernel
698 * route has any of them.
699 */
700
701 if (old)
702 nl_send_route(p, old, NULL, 0);
703
704 if (new)
705 err = nl_send_route(p, new, eattrs, 1);
706
707 if (err < 0)
708 n->n.flags |= KRF_SYNC_ERROR;
709 else
710 n->n.flags &= ~KRF_SYNC_ERROR;
711 }
712
713
714 #define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
715
716 static void
717 nl_parse_route(struct nlmsghdr *h, int scan)
718 {
719 struct krt_proto *p;
720 struct rtmsg *i;
721 struct rtattr *a[RTA_CACHEINFO+1];
722 int new = h->nlmsg_type == RTM_NEWROUTE;
723
724 ip_addr dst = IPA_NONE;
725 u32 oif = ~0;
726 int src;
727
728 if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(RTM_RTA(i), a, sizeof(a)))
729 return;
730 if (i->rtm_family != BIRD_AF)
731 return;
732 if ((a[RTA_DST] && RTA_PAYLOAD(a[RTA_DST]) != sizeof(ip_addr)) ||
733 #ifdef IPV6
734 (a[RTA_IIF] && RTA_PAYLOAD(a[RTA_IIF]) != 4) ||
735 #endif
736 (a[RTA_OIF] && RTA_PAYLOAD(a[RTA_OIF]) != 4) ||
737 (a[RTA_GATEWAY] && RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)) ||
738 (a[RTA_PRIORITY] && RTA_PAYLOAD(a[RTA_PRIORITY]) != 4) ||
739 (a[RTA_PREFSRC] && RTA_PAYLOAD(a[RTA_PREFSRC]) != sizeof(ip_addr)) ||
740 (a[RTA_FLOW] && RTA_PAYLOAD(a[RTA_FLOW]) != 4))
741 {
742 log(L_ERR "KRT: Malformed message received");
743 return;
744 }
745
746 if (a[RTA_DST])
747 {
748 memcpy(&dst, RTA_DATA(a[RTA_DST]), sizeof(dst));
749 ipa_ntoh(dst);
750 }
751
752 if (a[RTA_OIF])
753 memcpy(&oif, RTA_DATA(a[RTA_OIF]), sizeof(oif));
754
755 p = nl_table_map[i->rtm_table]; /* Do we know this table? */
756 DBG("KRT: Got %I/%d, type=%d, oif=%d, table=%d, prid=%d, proto=%s\n", dst, i->rtm_dst_len, i->rtm_type, oif, i->rtm_table, i->rtm_protocol, p ? p->p.name : "(none)");
757 if (!p)
758 SKIP("unknown table %d\n", i->rtm_table);
759
760
761 #ifdef IPV6
762 if (a[RTA_IIF])
763 SKIP("IIF set\n");
764 #else
765 if (i->rtm_tos != 0) /* We don't support TOS */
766 SKIP("TOS %02x\n", i->rtm_tos);
767 #endif
768
769 if (scan && !new)
770 SKIP("RTM_DELROUTE in scan\n");
771
772 int c = ipa_classify_net(dst);
773 if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK))
774 SKIP("strange class/scope\n");
775
776 // ignore rtm_scope, it is not a real scope
777 // if (i->rtm_scope != RT_SCOPE_UNIVERSE)
778 // SKIP("scope %u\n", i->rtm_scope);
779
780 switch (i->rtm_protocol)
781 {
782 case RTPROT_UNSPEC:
783 SKIP("proto unspec\n");
784
785 case RTPROT_REDIRECT:
786 src = KRT_SRC_REDIRECT;
787 break;
788
789 case RTPROT_KERNEL:
790 src = KRT_SRC_KERNEL;
791 return;
792
793 case RTPROT_BIRD:
794 if (!scan)
795 SKIP("echo\n");
796 src = KRT_SRC_BIRD;
797 break;
798
799 case RTPROT_BOOT:
800 default:
801 src = KRT_SRC_ALIEN;
802 }
803
804 net *net = net_get(p->p.table, dst, i->rtm_dst_len);
805
806 rta ra = {
807 .proto = &p->p,
808 .source = RTS_INHERIT,
809 .scope = SCOPE_UNIVERSE,
810 .cast = RTC_UNICAST
811 };
812
813 switch (i->rtm_type)
814 {
815 case RTN_UNICAST:
816
817 if (a[RTA_MULTIPATH])
818 {
819 ra.dest = RTD_MULTIPATH;
820 ra.nexthops = nl_parse_multipath(p, a[RTA_MULTIPATH]);
821 if (!ra.nexthops)
822 {
823 log(L_ERR "KRT: Received strange multipath route %I/%d",
824 net->n.prefix, net->n.pxlen);
825 return;
826 }
827
828 break;
829 }
830
831 ra.iface = if_find_by_index(oif);
832 if (!ra.iface)
833 {
834 log(L_ERR "KRT: Received route %I/%d with unknown ifindex %u",
835 net->n.prefix, net->n.pxlen, oif);
836 return;
837 }
838
839 if (a[RTA_GATEWAY])
840 {
841 neighbor *ng;
842 ra.dest = RTD_ROUTER;
843 memcpy(&ra.gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ra.gw));
844 ipa_ntoh(ra.gw);
845
846 #ifdef IPV6
847 /* Silently skip strange 6to4 routes */
848 if (ipa_in_net(ra.gw, IPA_NONE, 96))
849 return;
850 #endif
851
852 ng = neigh_find2(&p->p, &ra.gw, ra.iface,
853 (i->rtm_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0);
854 if (!ng || (ng->scope == SCOPE_HOST))
855 {
856 log(L_ERR "KRT: Received route %I/%d with strange next-hop %I",
857 net->n.prefix, net->n.pxlen, ra.gw);
858 return;
859 }
860 }
861 else
862 {
863 ra.dest = RTD_DEVICE;
864
865 /*
866 * In Linux IPv6, 'native' device routes have proto
867 * RTPROT_BOOT and not RTPROT_KERNEL (which they have in
868 * IPv4 and which is expected). We cannot distinguish
869 * 'native' and user defined device routes, so we ignore all
870 * such device routes and for consistency, we have the same
871 * behavior in IPv4. Anyway, users should use RTPROT_STATIC
872 * for their 'alien' routes.
873 */
874
875 if (i->rtm_protocol == RTPROT_BOOT)
876 src = KRT_SRC_KERNEL;
877 }
878
879 break;
880 case RTN_BLACKHOLE:
881 ra.dest = RTD_BLACKHOLE;
882 break;
883 case RTN_UNREACHABLE:
884 ra.dest = RTD_UNREACHABLE;
885 break;
886 case RTN_PROHIBIT:
887 ra.dest = RTD_PROHIBIT;
888 break;
889 /* FIXME: What about RTN_THROW? */
890 default:
891 SKIP("type %d\n", i->rtm_type);
892 return;
893 }
894
895 rte *e = rte_get_temp(&ra);
896 e->net = net;
897 e->u.krt.src = src;
898 e->u.krt.proto = i->rtm_protocol;
899 e->u.krt.type = i->rtm_type;
900
901 if (a[RTA_PRIORITY])
902 memcpy(&e->u.krt.metric, RTA_DATA(a[RTA_PRIORITY]), sizeof(e->u.krt.metric));
903 else
904 e->u.krt.metric = 0;
905
906 if (a[RTA_PREFSRC])
907 {
908 ip_addr ps;
909 memcpy(&ps, RTA_DATA(a[RTA_PREFSRC]), sizeof(ps));
910 ipa_ntoh(ps);
911
912 ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr));
913 ea->next = ra.eattrs;
914 ra.eattrs = ea;
915 ea->flags = EALF_SORTED;
916 ea->count = 1;
917 ea->attrs[0].id = EA_KRT_PREFSRC;
918 ea->attrs[0].flags = 0;
919 ea->attrs[0].type = EAF_TYPE_IP_ADDRESS;
920 ea->attrs[0].u.ptr = alloca(sizeof(struct adata) + sizeof(ps));
921 ea->attrs[0].u.ptr->length = sizeof(ps);
922 memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps));
923 }
924
925 if (a[RTA_FLOW])
926 {
927 ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr));
928 ea->next = ra.eattrs;
929 ra.eattrs = ea;
930 ea->flags = EALF_SORTED;
931 ea->count = 1;
932 ea->attrs[0].id = EA_KRT_REALM;
933 ea->attrs[0].flags = 0;
934 ea->attrs[0].type = EAF_TYPE_INT;
935 memcpy(&ea->attrs[0].u.data, RTA_DATA(a[RTA_FLOW]), 4);
936 }
937
938 if (scan)
939 krt_got_route(p, e);
940 else
941 krt_got_route_async(p, e, new);
942 }
943
944 void
945 krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */
946 {
947 struct nlmsghdr *h;
948
949 nl_request_dump(RTM_GETROUTE);
950 while (h = nl_get_scan())
951 if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)
952 nl_parse_route(h, 1);
953 else
954 log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type);
955 }
956
957 /*
958 * Asynchronous Netlink interface
959 */
960
961 static sock *nl_async_sk; /* BIRD socket for asynchronous notifications */
962 static byte *nl_async_rx_buffer; /* Receive buffer */
963
964 static void
965 nl_async_msg(struct nlmsghdr *h)
966 {
967 switch (h->nlmsg_type)
968 {
969 case RTM_NEWROUTE:
970 case RTM_DELROUTE:
971 DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type);
972 nl_parse_route(h, 0);
973 break;
974 case RTM_NEWLINK:
975 case RTM_DELLINK:
976 DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type);
977 nl_parse_link(h, 0);
978 break;
979 case RTM_NEWADDR:
980 case RTM_DELADDR:
981 DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type);
982 nl_parse_addr(h);
983 break;
984 default:
985 DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type);
986 }
987 }
988
989 static int
990 nl_async_hook(sock *sk, int size UNUSED)
991 {
992 struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE };
993 struct sockaddr_nl sa;
994 struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
995 struct nlmsghdr *h;
996 int x;
997 unsigned int len;
998
999 x = recvmsg(sk->fd, &m, 0);
1000 if (x < 0)
1001 {
1002 if (errno == ENOBUFS)
1003 {
1004 /*
1005 * Netlink reports some packets have been thrown away.
1006 * One day we might react to it by asking for route table
1007 * scan in near future.
1008 */
1009 return 1; /* More data are likely to be ready */
1010 }
1011 else if (errno != EWOULDBLOCK)
1012 log(L_ERR "Netlink recvmsg: %m");
1013 return 0;
1014 }
1015 if (sa.nl_pid) /* It isn't from the kernel */
1016 {
1017 DBG("Non-kernel packet\n");
1018 return 1;
1019 }
1020 h = (void *) nl_async_rx_buffer;
1021 len = x;
1022 if (m.msg_flags & MSG_TRUNC)
1023 {
1024 log(L_WARN "Netlink got truncated asynchronous message");
1025 return 1;
1026 }
1027 while (NLMSG_OK(h, len))
1028 {
1029 nl_async_msg(h);
1030 h = NLMSG_NEXT(h, len);
1031 }
1032 if (len)
1033 log(L_WARN "nl_async_hook: Found packet remnant of size %d", len);
1034 return 1;
1035 }
1036
1037 static void
1038 nl_open_async(void)
1039 {
1040 sock *sk;
1041 struct sockaddr_nl sa;
1042 int fd;
1043 static int nl_open_tried = 0;
1044
1045 if (nl_open_tried)
1046 return;
1047 nl_open_tried = 1;
1048
1049 DBG("KRT: Opening async netlink socket\n");
1050
1051 fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1052 if (fd < 0)
1053 {
1054 log(L_ERR "Unable to open asynchronous rtnetlink socket: %m");
1055 return;
1056 }
1057
1058 bzero(&sa, sizeof(sa));
1059 sa.nl_family = AF_NETLINK;
1060 #ifdef IPV6
1061 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
1062 #else
1063 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
1064 #endif
1065 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
1066 {
1067 log(L_ERR "Unable to bind asynchronous rtnetlink socket: %m");
1068 return;
1069 }
1070
1071 sk = nl_async_sk = sk_new(krt_pool);
1072 sk->type = SK_MAGIC;
1073 sk->rx_hook = nl_async_hook;
1074 sk->fd = fd;
1075 if (sk_open(sk))
1076 bug("Netlink: sk_open failed");
1077
1078 if (!nl_async_rx_buffer)
1079 nl_async_rx_buffer = xmalloc(NL_RX_SIZE);
1080 }
1081
1082 /*
1083 * Interface to the UNIX krt module
1084 */
1085
1086 static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8];
1087
1088 void
1089 krt_sys_start(struct krt_proto *p)
1090 {
1091 nl_table_map[KRT_CF->sys.table_id] = p;
1092
1093 nl_open();
1094 nl_open_async();
1095 }
1096
1097 void
1098 krt_sys_shutdown(struct krt_proto *p UNUSED)
1099 {
1100 }
1101
1102 int
1103 krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
1104 {
1105 return n->sys.table_id == o->sys.table_id;
1106 }
1107
1108
1109 void
1110 krt_sys_preconfig(struct config *c UNUSED)
1111 {
1112 bzero(&nl_cf_table, sizeof(nl_cf_table));
1113 }
1114
1115 void
1116 krt_sys_postconfig(struct krt_config *x)
1117 {
1118 int id = x->sys.table_id;
1119
1120 if (nl_cf_table[id/8] & (1 << (id%8)))
1121 cf_error("Multiple kernel syncers defined for table #%d", id);
1122 nl_cf_table[id/8] |= (1 << (id%8));
1123 }
1124
1125 void
1126 krt_sys_init_config(struct krt_config *cf)
1127 {
1128 cf->sys.table_id = RT_TABLE_MAIN;
1129 }
1130
1131 void
1132 krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
1133 {
1134 d->sys.table_id = s->sys.table_id;
1135 }
1136
1137
1138
1139 void
1140 kif_sys_start(struct kif_proto *p UNUSED)
1141 {
1142 nl_open();
1143 nl_open_async();
1144 }
1145
1146 void
1147 kif_sys_shutdown(struct kif_proto *p UNUSED)
1148 {
1149 }