]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_netlink.c
Merge pull request #2640 from pguibert6WIND/doc_ldp
[mirror_frr.git] / zebra / kernel_netlink.c
1 /* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #ifdef HAVE_NETLINK
24
25 #include "linklist.h"
26 #include "if.h"
27 #include "log.h"
28 #include "prefix.h"
29 #include "connected.h"
30 #include "table.h"
31 #include "memory.h"
32 #include "zebra_memory.h"
33 #include "rib.h"
34 #include "thread.h"
35 #include "privs.h"
36 #include "nexthop.h"
37 #include "vrf.h"
38 #include "mpls.h"
39
40 #include "zebra/zserv.h"
41 #include "zebra/zebra_ns.h"
42 #include "zebra/zebra_vrf.h"
43 #include "zebra/rt.h"
44 #include "zebra/debug.h"
45 #include "zebra/kernel_netlink.h"
46 #include "zebra/rt_netlink.h"
47 #include "zebra/if_netlink.h"
48 #include "zebra/rule_netlink.h"
49
50 #ifndef SO_RCVBUFFORCE
51 #define SO_RCVBUFFORCE (33)
52 #endif
53
54 /* Hack for GNU libc version 2. */
55 #ifndef MSG_TRUNC
56 #define MSG_TRUNC 0x20
57 #endif /* MSG_TRUNC */
58
59 #ifndef NLMSG_TAIL
60 #define NLMSG_TAIL(nmsg) \
61 ((struct rtattr *)(((uint8_t *)(nmsg)) \
62 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
63 #endif
64
65 #ifndef RTA_TAIL
66 #define RTA_TAIL(rta) \
67 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
68 #endif
69
70 #ifndef RTNL_FAMILY_IP6MR
71 #define RTNL_FAMILY_IP6MR 129
72 #endif
73
74 #ifndef RTPROT_MROUTED
75 #define RTPROT_MROUTED 17
76 #endif
77
78 static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"},
79 {RTM_DELROUTE, "RTM_DELROUTE"},
80 {RTM_GETROUTE, "RTM_GETROUTE"},
81 {RTM_NEWLINK, "RTM_NEWLINK"},
82 {RTM_DELLINK, "RTM_DELLINK"},
83 {RTM_GETLINK, "RTM_GETLINK"},
84 {RTM_NEWADDR, "RTM_NEWADDR"},
85 {RTM_DELADDR, "RTM_DELADDR"},
86 {RTM_GETADDR, "RTM_GETADDR"},
87 {RTM_NEWNEIGH, "RTM_NEWNEIGH"},
88 {RTM_DELNEIGH, "RTM_DELNEIGH"},
89 {RTM_GETNEIGH, "RTM_GETNEIGH"},
90 {RTM_NEWRULE, "RTM_NEWRULE"},
91 {RTM_DELRULE, "RTM_DELRULE"},
92 {RTM_GETRULE, "RTM_GETRULE"},
93 {0}};
94
95 static const struct message rtproto_str[] = {
96 {RTPROT_REDIRECT, "redirect"},
97 {RTPROT_KERNEL, "kernel"},
98 {RTPROT_BOOT, "boot"},
99 {RTPROT_STATIC, "static"},
100 {RTPROT_GATED, "GateD"},
101 {RTPROT_RA, "router advertisement"},
102 {RTPROT_MRT, "MRT"},
103 {RTPROT_ZEBRA, "Zebra"},
104 #ifdef RTPROT_BIRD
105 {RTPROT_BIRD, "BIRD"},
106 #endif /* RTPROT_BIRD */
107 {RTPROT_MROUTED, "mroute"},
108 {RTPROT_BGP, "BGP"},
109 {RTPROT_OSPF, "OSPF"},
110 {RTPROT_ISIS, "IS-IS"},
111 {RTPROT_RIP, "RIP"},
112 {RTPROT_RIPNG, "RIPNG"},
113 {RTPROT_ZSTATIC, "static"},
114 {0}};
115
116 static const struct message family_str[] = {{AF_INET, "ipv4"},
117 {AF_INET6, "ipv6"},
118 {AF_BRIDGE, "bridge"},
119 {RTNL_FAMILY_IPMR, "ipv4MR"},
120 {RTNL_FAMILY_IP6MR, "ipv6MR"},
121 {0}};
122
123 static const struct message rttype_str[] = {{RTN_UNICAST, "unicast"},
124 {RTN_MULTICAST, "multicast"},
125 {0}};
126
127 extern struct thread_master *master;
128 extern uint32_t nl_rcvbufsize;
129
130 extern struct zebra_privs_t zserv_privs;
131
132 int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup)
133 {
134 /*
135 * This is an error condition that must be handled during
136 * development.
137 *
138 * The netlink_talk_filter function is used for communication
139 * down the netlink_cmd pipe and we are expecting
140 * an ack being received. So if we get here
141 * then we did not receive the ack and instead
142 * received some other message in an unexpected
143 * way.
144 */
145 zlog_err("%s: ignoring message type 0x%04x(%s) NS %u",
146 __PRETTY_FUNCTION__, h->nlmsg_type,
147 nl_msg_type_to_str(h->nlmsg_type), ns_id);
148 return 0;
149 }
150
151 static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
152 {
153 uint32_t oldsize;
154 socklen_t newlen = sizeof(newsize);
155 socklen_t oldlen = sizeof(oldsize);
156 int ret;
157
158 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
159 if (ret < 0) {
160 zlog_err("Can't get %s receive buffer size: %s", nl->name,
161 safe_strerror(errno));
162 return -1;
163 }
164
165 /* Try force option (linux >= 2.6.14) and fall back to normal set */
166 if (zserv_privs.change(ZPRIVS_RAISE))
167 zlog_err("routing_socket: Can't raise privileges");
168 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize,
169 sizeof(nl_rcvbufsize));
170 if (zserv_privs.change(ZPRIVS_LOWER))
171 zlog_err("routing_socket: Can't lower privileges");
172 if (ret < 0)
173 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF,
174 &nl_rcvbufsize, sizeof(nl_rcvbufsize));
175 if (ret < 0) {
176 zlog_err("Can't set %s receive buffer size: %s", nl->name,
177 safe_strerror(errno));
178 return -1;
179 }
180
181 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
182 if (ret < 0) {
183 zlog_err("Can't get %s receive buffer size: %s", nl->name,
184 safe_strerror(errno));
185 return -1;
186 }
187
188 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
189 oldsize, newsize);
190 return 0;
191 }
192
193 /* Make socket for Linux netlink interface. */
194 static int netlink_socket(struct nlsock *nl, unsigned long groups,
195 ns_id_t ns_id)
196 {
197 int ret;
198 struct sockaddr_nl snl;
199 int sock;
200 int namelen;
201 int save_errno;
202
203 if (zserv_privs.change(ZPRIVS_RAISE)) {
204 zlog_err("Can't raise privileges");
205 return -1;
206 }
207
208 sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id);
209 if (sock < 0) {
210 zlog_err("Can't open %s socket: %s", nl->name,
211 safe_strerror(errno));
212 return -1;
213 }
214
215 memset(&snl, 0, sizeof snl);
216 snl.nl_family = AF_NETLINK;
217 snl.nl_groups = groups;
218
219 /* Bind the socket to the netlink structure for anything. */
220 ret = bind(sock, (struct sockaddr *)&snl, sizeof snl);
221 save_errno = errno;
222 if (zserv_privs.change(ZPRIVS_LOWER))
223 zlog_err("Can't lower privileges");
224
225 if (ret < 0) {
226 zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name,
227 snl.nl_groups, safe_strerror(save_errno));
228 close(sock);
229 return -1;
230 }
231
232 /* multiple netlink sockets will have different nl_pid */
233 namelen = sizeof snl;
234 ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen);
235 if (ret < 0 || namelen != sizeof snl) {
236 zlog_err("Can't get %s socket name: %s", nl->name,
237 safe_strerror(errno));
238 close(sock);
239 return -1;
240 }
241
242 nl->snl = snl;
243 nl->sock = sock;
244 return ret;
245 }
246
247 static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
248 int startup)
249 {
250 /*
251 * When we handle new message types here
252 * because we are starting to install them
253 * then lets check the netlink_install_filter
254 * and see if we should add the corresponding
255 * allow through entry there.
256 * Probably not needed to do but please
257 * think about it.
258 */
259 switch (h->nlmsg_type) {
260 case RTM_NEWROUTE:
261 return netlink_route_change(h, ns_id, startup);
262 case RTM_DELROUTE:
263 return netlink_route_change(h, ns_id, startup);
264 case RTM_NEWLINK:
265 return netlink_link_change(h, ns_id, startup);
266 case RTM_DELLINK:
267 return netlink_link_change(h, ns_id, startup);
268 case RTM_NEWADDR:
269 return netlink_interface_addr(h, ns_id, startup);
270 case RTM_DELADDR:
271 return netlink_interface_addr(h, ns_id, startup);
272 case RTM_NEWNEIGH:
273 return netlink_neigh_change(h, ns_id);
274 case RTM_DELNEIGH:
275 return netlink_neigh_change(h, ns_id);
276 case RTM_NEWRULE:
277 return netlink_rule_change(h, ns_id, startup);
278 case RTM_DELRULE:
279 return netlink_rule_change(h, ns_id, startup);
280 default:
281 /*
282 * If we have received this message then
283 * we have made a mistake during development
284 * and we need to write some code to handle
285 * this message type or not ask for
286 * it to be sent up to us
287 */
288 zlog_err("Unknown netlink nlmsg_type %s(%d) vrf %u\n",
289 nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type,
290 ns_id);
291 break;
292 }
293 return 0;
294 }
295
296 static int kernel_read(struct thread *thread)
297 {
298 struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread);
299 netlink_parse_info(netlink_information_fetch, &zns->netlink, zns, 5, 0);
300 zns->t_netlink = NULL;
301 thread_add_read(zebrad.master, kernel_read, zns, zns->netlink.sock,
302 &zns->t_netlink);
303
304 return 0;
305 }
306
307 /*
308 * Filter out messages from self that occur on listener socket,
309 * caused by our actions on the command socket
310 *
311 * When we add new Netlink message types we probably
312 * do not need to add them here as that we are filtering
313 * on the routes we actually care to receive( which is rarer
314 * then the normal course of operations). We are intentionally
315 * allowing some messages from ourselves through
316 * ( I'm looking at you Interface based netlink messages )
317 * so that we only had to write one way to handle incoming
318 * address add/delete changes.
319 */
320 static void netlink_install_filter(int sock, __u32 pid)
321 {
322 /*
323 * BPF_JUMP instructions and where you jump to are based upon
324 * 0 as being the next statement. So count from 0. Writing
325 * this down because every time I look at this I have to
326 * re-remember it.
327 */
328 struct sock_filter filter[] = {
329 /*
330 * Logic:
331 * if (nlmsg_pid == pid) {
332 * if (the incoming nlmsg_type ==
333 * RTM_NEWADDR | RTM_DELADDR)
334 * keep this message
335 * else
336 * skip this message
337 * } else
338 * keep this netlink message
339 */
340 /*
341 * 0: Load the nlmsg_pid into the BPF register
342 */
343 BPF_STMT(BPF_LD | BPF_ABS | BPF_W,
344 offsetof(struct nlmsghdr, nlmsg_pid)),
345 /*
346 * 1: Compare to pid
347 */
348 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 0, 4),
349 /*
350 * 2: Load the nlmsg_type into BPF register
351 */
352 BPF_STMT(BPF_LD | BPF_ABS | BPF_H,
353 offsetof(struct nlmsghdr, nlmsg_type)),
354 /*
355 * 3: Compare to RTM_NEWADDR
356 */
357 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 2, 0),
358 /*
359 * 4: Compare to RTM_DELADDR
360 */
361 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 1, 0),
362 /*
363 * 5: This is the end state of we want to skip the
364 * message
365 */
366 BPF_STMT(BPF_RET | BPF_K, 0),
367 /* 6: This is the end state of we want to keep
368 * the message
369 */
370 BPF_STMT(BPF_RET | BPF_K, 0xffff),
371 };
372
373 struct sock_fprog prog = {
374 .len = array_size(filter), .filter = filter,
375 };
376
377 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))
378 < 0)
379 zlog_warn("Can't install socket filter: %s\n",
380 safe_strerror(errno));
381 }
382
383 void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
384 int len)
385 {
386 while (RTA_OK(rta, len)) {
387 if (rta->rta_type <= max)
388 tb[rta->rta_type] = rta;
389 rta = RTA_NEXT(rta, len);
390 }
391 }
392
393 int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
394 const void *data, unsigned int alen)
395 {
396 int len;
397 struct rtattr *rta;
398
399 len = RTA_LENGTH(alen);
400
401 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
402 return -1;
403
404 rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
405 rta->rta_type = type;
406 rta->rta_len = len;
407
408 if (data)
409 memcpy(RTA_DATA(rta), data, alen);
410 else
411 assert(alen == 0);
412
413 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
414
415 return 0;
416 }
417
418 int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type,
419 const void *data, unsigned int alen)
420 {
421 unsigned int len;
422 struct rtattr *subrta;
423
424 len = RTA_LENGTH(alen);
425
426 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen)
427 return -1;
428
429 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
430 subrta->rta_type = type;
431 subrta->rta_len = len;
432
433 if (data)
434 memcpy(RTA_DATA(subrta), data, alen);
435 else
436 assert(alen == 0);
437
438 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
439
440 return 0;
441 }
442
443 int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, uint16_t data)
444 {
445 return addattr_l(n, maxlen, type, &data, sizeof(uint16_t));
446 }
447
448 int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data)
449 {
450 return addattr_l(n, maxlen, type, &data, sizeof(uint32_t));
451 }
452
453 struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
454 {
455 struct rtattr *nest = NLMSG_TAIL(n);
456
457 addattr_l(n, maxlen, type, NULL, 0);
458 return nest;
459 }
460
461 int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
462 {
463 nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest;
464 return n->nlmsg_len;
465 }
466
467 struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
468 {
469 struct rtattr *nest = RTA_TAIL(rta);
470
471 rta_addattr_l(rta, maxlen, type, NULL, 0);
472 return nest;
473 }
474
475 int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
476 {
477 nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest;
478 return rta->rta_len;
479 }
480
481 const char *nl_msg_type_to_str(uint16_t msg_type)
482 {
483 return lookup_msg(nlmsg_str, msg_type, "");
484 }
485
486 const char *nl_rtproto_to_str(uint8_t rtproto)
487 {
488 return lookup_msg(rtproto_str, rtproto, "");
489 }
490
491 const char *nl_family_to_str(uint8_t family)
492 {
493 return lookup_msg(family_str, family, "");
494 }
495
496 const char *nl_rttype_to_str(uint8_t rttype)
497 {
498 return lookup_msg(rttype_str, rttype, "");
499 }
500
501 #define NL_OK(nla, len) \
502 ((len) >= (int)sizeof(struct nlattr) \
503 && (nla)->nla_len >= sizeof(struct nlattr) \
504 && (nla)->nla_len <= (len))
505 #define NL_NEXT(nla, attrlen) \
506 ((attrlen) -= RTA_ALIGN((nla)->nla_len), \
507 (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len)))
508 #define NL_RTA(r) \
509 ((struct nlattr *)(((char *)(r)) \
510 + NLMSG_ALIGN(sizeof(struct nlmsgerr))))
511
512 static void netlink_parse_nlattr(struct nlattr **tb, int max,
513 struct nlattr *nla, int len)
514 {
515 while (NL_OK(nla, len)) {
516 if (nla->nla_type <= max)
517 tb[nla->nla_type] = nla;
518 nla = NL_NEXT(nla, len);
519 }
520 }
521
522 static void netlink_parse_extended_ack(struct nlmsghdr *h)
523 {
524 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
525 const struct nlmsgerr *err =
526 (const struct nlmsgerr *)((uint8_t *)h
527 + NLMSG_ALIGN(
528 sizeof(struct nlmsghdr)));
529 const struct nlmsghdr *err_nlh = NULL;
530 uint32_t hlen = sizeof(*err);
531 const char *msg = NULL;
532 uint32_t off = 0;
533
534 if (!(h->nlmsg_flags & NLM_F_CAPPED))
535 hlen += h->nlmsg_len - NLMSG_ALIGN(sizeof(struct nlmsghdr));
536
537 memset(tb, 0, sizeof(tb));
538 netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, NL_RTA(h), hlen);
539
540 if (tb[NLMSGERR_ATTR_MSG])
541 msg = (const char *)RTA_DATA(tb[NLMSGERR_ATTR_MSG]);
542
543 if (tb[NLMSGERR_ATTR_OFFS]) {
544 off = *(uint32_t *)RTA_DATA(tb[NLMSGERR_ATTR_OFFS]);
545
546 if (off > h->nlmsg_len) {
547 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n");
548 } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) {
549 /*
550 * Header of failed message
551 * we are not doing anything currently with it
552 * but noticing it for later.
553 */
554 err_nlh = &err->msg;
555 zlog_warn("%s: Received %d extended Ack",
556 __PRETTY_FUNCTION__, err_nlh->nlmsg_type);
557 }
558 }
559
560 if (msg && *msg != '\0') {
561 bool is_err = !!err->error;
562
563 if (is_err)
564 zlog_err("Extended Error: %s", msg);
565 else
566 zlog_warn("Extended Warning: %s", msg);
567 }
568 }
569
570 /*
571 * netlink_parse_info
572 *
573 * Receive message from netlink interface and pass those information
574 * to the given function.
575 *
576 * filter -> Function to call to read the results
577 * nl -> netlink socket information
578 * zns -> The zebra namespace data
579 * count -> How many we should read in, 0 means as much as possible
580 * startup -> Are we reading in under startup conditions? passed to
581 * the filter.
582 */
583 int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
584 struct nlsock *nl, struct zebra_ns *zns, int count,
585 int startup)
586 {
587 int status;
588 int ret = 0;
589 int error;
590 int read_in = 0;
591
592 while (1) {
593 char buf[NL_RCV_PKT_BUF_SIZE];
594 struct iovec iov = {.iov_base = buf, .iov_len = sizeof buf};
595 struct sockaddr_nl snl;
596 struct msghdr msg = {.msg_name = (void *)&snl,
597 .msg_namelen = sizeof snl,
598 .msg_iov = &iov,
599 .msg_iovlen = 1};
600 struct nlmsghdr *h;
601
602 if (count && read_in >= count)
603 return 0;
604
605 status = recvmsg(nl->sock, &msg, 0);
606 if (status < 0) {
607 if (errno == EINTR)
608 continue;
609 if (errno == EWOULDBLOCK || errno == EAGAIN)
610 break;
611 zlog_err("%s recvmsg overrun: %s", nl->name,
612 safe_strerror(errno));
613 /*
614 * In this case we are screwed.
615 * There is no good way to
616 * recover zebra at this point.
617 */
618 exit(-1);
619 continue;
620 }
621
622 if (status == 0) {
623 zlog_err("%s EOF", nl->name);
624 return -1;
625 }
626
627 if (msg.msg_namelen != sizeof snl) {
628 zlog_err("%s sender address length error: length %d",
629 nl->name, msg.msg_namelen);
630 return -1;
631 }
632
633 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
634 zlog_debug("%s: << netlink message dump [recv]",
635 __func__);
636 zlog_hexdump(buf, status);
637 }
638
639 read_in++;
640 for (h = (struct nlmsghdr *)buf;
641 NLMSG_OK(h, (unsigned int)status);
642 h = NLMSG_NEXT(h, status)) {
643 /* Finish of reading. */
644 if (h->nlmsg_type == NLMSG_DONE)
645 return ret;
646
647 /* Error handling. */
648 if (h->nlmsg_type == NLMSG_ERROR) {
649 struct nlmsgerr *err =
650 (struct nlmsgerr *)NLMSG_DATA(h);
651 int errnum = err->error;
652 int msg_type = err->msg.nlmsg_type;
653
654 if (h->nlmsg_len
655 < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
656 zlog_err("%s error: message truncated",
657 nl->name);
658 return -1;
659 }
660
661 /*
662 * Parse the extended information before
663 * we actually handle it.
664 * At this point in time we do not
665 * do anything other than report the
666 * issue.
667 */
668 if (h->nlmsg_flags & NLM_F_ACK_TLVS)
669 netlink_parse_extended_ack(h);
670
671 /* If the error field is zero, then this is an
672 * ACK */
673 if (err->error == 0) {
674 if (IS_ZEBRA_DEBUG_KERNEL) {
675 zlog_debug(
676 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
677 __FUNCTION__, nl->name,
678 nl_msg_type_to_str(
679 err->msg.nlmsg_type),
680 err->msg.nlmsg_type,
681 err->msg.nlmsg_seq,
682 err->msg.nlmsg_pid);
683 }
684
685 /* return if not a multipart message,
686 * otherwise continue */
687 if (!(h->nlmsg_flags & NLM_F_MULTI))
688 return 0;
689 continue;
690 }
691
692 /* Deal with errors that occur because of races
693 * in link handling */
694 if (nl == &zns->netlink_cmd
695 && ((msg_type == RTM_DELROUTE
696 && (-errnum == ENODEV
697 || -errnum == ESRCH))
698 || (msg_type == RTM_NEWROUTE
699 && (-errnum == ENETDOWN
700 || -errnum == EEXIST)))) {
701 if (IS_ZEBRA_DEBUG_KERNEL)
702 zlog_debug(
703 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
704 nl->name,
705 safe_strerror(-errnum),
706 nl_msg_type_to_str(
707 msg_type),
708 msg_type,
709 err->msg.nlmsg_seq,
710 err->msg.nlmsg_pid);
711 return 0;
712 }
713
714 /* We see RTM_DELNEIGH when shutting down an
715 * interface with an IPv4
716 * link-local. The kernel should have already
717 * deleted the neighbor
718 * so do not log these as an error.
719 */
720 if (msg_type == RTM_DELNEIGH
721 || (nl == &zns->netlink_cmd
722 && msg_type == RTM_NEWROUTE
723 && (-errnum == ESRCH
724 || -errnum == ENETUNREACH))) {
725 /* This is known to happen in some
726 * situations, don't log
727 * as error.
728 */
729 if (IS_ZEBRA_DEBUG_KERNEL)
730 zlog_debug(
731 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
732 nl->name,
733 safe_strerror(-errnum),
734 nl_msg_type_to_str(
735 msg_type),
736 msg_type,
737 err->msg.nlmsg_seq,
738 err->msg.nlmsg_pid);
739 } else
740 zlog_err(
741 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
742 nl->name,
743 safe_strerror(-errnum),
744 nl_msg_type_to_str(msg_type),
745 msg_type, err->msg.nlmsg_seq,
746 err->msg.nlmsg_pid);
747
748 return -1;
749 }
750
751 /* OK we got netlink message. */
752 if (IS_ZEBRA_DEBUG_KERNEL)
753 zlog_debug(
754 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
755 nl->name,
756 nl_msg_type_to_str(h->nlmsg_type),
757 h->nlmsg_type, h->nlmsg_len,
758 h->nlmsg_seq, h->nlmsg_pid);
759
760
761 /*
762 * Ignore messages that maybe sent from
763 * other actors besides the kernel
764 */
765 if (snl.nl_pid != 0) {
766 zlog_err("Ignoring message from pid %u",
767 snl.nl_pid);
768 continue;
769 }
770
771 error = (*filter)(h, zns->ns_id, startup);
772 if (error < 0) {
773 zlog_err("%s filter function error", nl->name);
774 zlog_backtrace(LOG_ERR);
775 ret = error;
776 }
777 }
778
779 /* After error care. */
780 if (msg.msg_flags & MSG_TRUNC) {
781 zlog_err("%s error: message truncated", nl->name);
782 continue;
783 }
784 if (status) {
785 zlog_err("%s error: data remnant size %d", nl->name,
786 status);
787 return -1;
788 }
789 }
790 return ret;
791 }
792
793 /*
794 * netlink_talk
795 *
796 * sendmsg() to netlink socket then recvmsg().
797 * Calls netlink_parse_info to parse returned data
798 *
799 * filter -> The filter to read final results from kernel
800 * nlmsghdr -> The data to send to the kernel
801 * nl -> The netlink socket information
802 * zns -> The zebra namespace information
803 * startup -> Are we reading in under startup conditions
804 * This is passed through eventually to filter.
805 */
806 int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
807 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
808 int startup)
809 {
810 int status;
811 struct sockaddr_nl snl;
812 struct iovec iov;
813 struct msghdr msg;
814 int save_errno;
815
816 memset(&snl, 0, sizeof snl);
817 memset(&iov, 0, sizeof iov);
818 memset(&msg, 0, sizeof msg);
819
820 iov.iov_base = n;
821 iov.iov_len = n->nlmsg_len;
822 msg.msg_name = (void *)&snl;
823 msg.msg_namelen = sizeof snl;
824 msg.msg_iov = &iov;
825 msg.msg_iovlen = 1;
826
827 snl.nl_family = AF_NETLINK;
828
829 n->nlmsg_seq = ++nl->seq;
830 n->nlmsg_pid = nl->snl.nl_pid;
831
832 if (IS_ZEBRA_DEBUG_KERNEL)
833 zlog_debug(
834 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
835 nl->name, nl_msg_type_to_str(n->nlmsg_type),
836 n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq,
837 n->nlmsg_flags);
838
839 /* Send message to netlink interface. */
840 if (zserv_privs.change(ZPRIVS_RAISE))
841 zlog_err("Can't raise privileges");
842 status = sendmsg(nl->sock, &msg, 0);
843 save_errno = errno;
844 if (zserv_privs.change(ZPRIVS_LOWER))
845 zlog_err("Can't lower privileges");
846
847 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
848 zlog_debug("%s: >> netlink message dump [sent]", __func__);
849 zlog_hexdump(n, n->nlmsg_len);
850 }
851
852 if (status < 0) {
853 zlog_err("netlink_talk sendmsg() error: %s",
854 safe_strerror(save_errno));
855 return -1;
856 }
857
858
859 /*
860 * Get reply from netlink socket.
861 * The reply should either be an acknowlegement or an error.
862 */
863 return netlink_parse_info(filter, nl, zns, 0, startup);
864 }
865
866 /* Issue request message to kernel via netlink socket. GET messages
867 * are issued through this interface.
868 */
869 int netlink_request(struct nlsock *nl, struct nlmsghdr *n)
870 {
871 int ret;
872 struct sockaddr_nl snl;
873 int save_errno;
874
875 /* Check netlink socket. */
876 if (nl->sock < 0) {
877 zlog_err("%s socket isn't active.", nl->name);
878 return -1;
879 }
880
881 /* Fill common fields for all requests. */
882 n->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
883 n->nlmsg_pid = nl->snl.nl_pid;
884 n->nlmsg_seq = ++nl->seq;
885
886 memset(&snl, 0, sizeof snl);
887 snl.nl_family = AF_NETLINK;
888
889 /* Raise capabilities and send message, then lower capabilities. */
890 if (zserv_privs.change(ZPRIVS_RAISE)) {
891 zlog_err("Can't raise privileges");
892 return -1;
893 }
894
895 ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0,
896 (struct sockaddr *)&snl, sizeof snl);
897 save_errno = errno;
898
899 if (zserv_privs.change(ZPRIVS_LOWER))
900 zlog_err("Can't lower privileges");
901
902 if (ret < 0) {
903 zlog_err("%s sendto failed: %s", nl->name,
904 safe_strerror(save_errno));
905 return -1;
906 }
907
908 return 0;
909 }
910
911 /* Exported interface function. This function simply calls
912 netlink_socket (). */
913 void kernel_init(struct zebra_ns *zns)
914 {
915 unsigned long groups;
916 #if defined SOL_NETLINK
917 int one, ret;
918 #endif
919
920 /*
921 * Initialize netlink sockets
922 *
923 * If RTMGRP_XXX exists use that, but at some point
924 * I think the kernel developers realized that
925 * keeping track of all the different values would
926 * lead to confusion, so we need to convert the
927 * RTNLGRP_XXX to a bit position for ourself
928 */
929 groups = RTMGRP_LINK |
930 RTMGRP_IPV4_ROUTE |
931 RTMGRP_IPV4_IFADDR |
932 RTMGRP_IPV6_ROUTE |
933 RTMGRP_IPV6_IFADDR |
934 RTMGRP_IPV4_MROUTE |
935 RTMGRP_NEIGH |
936 (1 << (RTNLGRP_IPV4_RULE - 1)) |
937 (1 << (RTNLGRP_IPV6_RULE - 1));
938
939 snprintf(zns->netlink.name, sizeof(zns->netlink.name),
940 "netlink-listen (NS %u)", zns->ns_id);
941 zns->netlink.sock = -1;
942 if (netlink_socket(&zns->netlink, groups, zns->ns_id) < 0) {
943 zlog_err("Failure to create %s socket",
944 zns->netlink.name);
945 exit(-1);
946 }
947
948 snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name),
949 "netlink-cmd (NS %u)", zns->ns_id);
950 zns->netlink_cmd.sock = -1;
951 if (netlink_socket(&zns->netlink_cmd, 0, zns->ns_id) < 0) {
952 zlog_err("Failure to create %s socket",
953 zns->netlink_cmd.name);
954 exit(-1);
955 }
956
957 /*
958 * SOL_NETLINK is not available on all platforms yet
959 * apparently. It's in bits/socket.h which I am not
960 * sure that we want to pull into our build system.
961 */
962 #if defined SOL_NETLINK
963 /*
964 * Let's tell the kernel that we want to receive extended
965 * ACKS over our command socket
966 */
967 one = 1;
968 ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK,
969 &one, sizeof(one));
970
971 if (ret < 0)
972 zlog_notice("Registration for extended ACK failed : %d %s",
973 errno, safe_strerror(errno));
974 #endif
975
976 /* Register kernel socket. */
977 if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
978 zlog_err("Can't set %s socket error: %s(%d)",
979 zns->netlink.name, safe_strerror(errno), errno);
980
981 if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0)
982 zlog_err("Can't set %s socket error: %s(%d)",
983 zns->netlink_cmd.name, safe_strerror(errno), errno);
984
985 /* Set receive buffer size if it's set from command line */
986 if (nl_rcvbufsize)
987 netlink_recvbuf(&zns->netlink, nl_rcvbufsize);
988
989 netlink_install_filter(zns->netlink.sock,
990 zns->netlink_cmd.snl.nl_pid);
991 zns->t_netlink = NULL;
992
993 thread_add_read(zebrad.master, kernel_read, zns,
994 zns->netlink.sock, &zns->t_netlink);
995
996 rt_netlink_init();
997 }
998
999 void kernel_terminate(struct zebra_ns *zns)
1000 {
1001 THREAD_READ_OFF(zns->t_netlink);
1002
1003 if (zns->netlink.sock >= 0) {
1004 close(zns->netlink.sock);
1005 zns->netlink.sock = -1;
1006 }
1007
1008 if (zns->netlink_cmd.sock >= 0) {
1009 close(zns->netlink_cmd.sock);
1010 zns->netlink_cmd.sock = -1;
1011 }
1012 }
1013
1014 #endif /* HAVE_NETLINK */