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