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