]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_netlink.c
Merge pull request #2561 from pacovn/Coverity_1399231_Logically_dead_code
[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, void *data,
394 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, void *data,
419 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 off = 0;
549 } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) {
550 /*
551 * Header of failed message
552 * we are not doing anything currently with it
553 * but noticing it for later.
554 */
555 err_nlh = &err->msg;
556 zlog_warn("%s: Received %d extended Ack",
557 __PRETTY_FUNCTION__, err_nlh->nlmsg_type);
558 }
559 }
560
561 if (msg && *msg != '\0') {
562 bool is_err = !!err->error;
563
564 if (is_err)
565 zlog_err("Extended Error: %s", msg);
566 else
567 zlog_warn("Extended Warning: %s", msg);
568 }
569 }
570
571 /*
572 * netlink_parse_info
573 *
574 * Receive message from netlink interface and pass those information
575 * to the given function.
576 *
577 * filter -> Function to call to read the results
578 * nl -> netlink socket information
579 * zns -> The zebra namespace data
580 * count -> How many we should read in, 0 means as much as possible
581 * startup -> Are we reading in under startup conditions? passed to
582 * the filter.
583 */
584 int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
585 struct nlsock *nl, struct zebra_ns *zns, int count,
586 int startup)
587 {
588 int status;
589 int ret = 0;
590 int error;
591 int read_in = 0;
592
593 while (1) {
594 char buf[NL_RCV_PKT_BUF_SIZE];
595 struct iovec iov = {.iov_base = buf, .iov_len = sizeof buf};
596 struct sockaddr_nl snl;
597 struct msghdr msg = {.msg_name = (void *)&snl,
598 .msg_namelen = sizeof snl,
599 .msg_iov = &iov,
600 .msg_iovlen = 1};
601 struct nlmsghdr *h;
602
603 if (count && read_in >= count)
604 return 0;
605
606 status = recvmsg(nl->sock, &msg, 0);
607 if (status < 0) {
608 if (errno == EINTR)
609 continue;
610 if (errno == EWOULDBLOCK || errno == EAGAIN)
611 break;
612 zlog_err("%s recvmsg overrun: %s", nl->name,
613 safe_strerror(errno));
614 /*
615 * In this case we are screwed.
616 * There is no good way to
617 * recover zebra at this point.
618 */
619 exit(-1);
620 continue;
621 }
622
623 if (status == 0) {
624 zlog_err("%s EOF", nl->name);
625 return -1;
626 }
627
628 if (msg.msg_namelen != sizeof snl) {
629 zlog_err("%s sender address length error: length %d",
630 nl->name, msg.msg_namelen);
631 return -1;
632 }
633
634 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
635 zlog_debug("%s: << netlink message dump [recv]",
636 __func__);
637 zlog_hexdump(buf, status);
638 }
639
640 read_in++;
641 for (h = (struct nlmsghdr *)buf;
642 NLMSG_OK(h, (unsigned int)status);
643 h = NLMSG_NEXT(h, status)) {
644 /* Finish of reading. */
645 if (h->nlmsg_type == NLMSG_DONE)
646 return ret;
647
648 /* Error handling. */
649 if (h->nlmsg_type == NLMSG_ERROR) {
650 struct nlmsgerr *err =
651 (struct nlmsgerr *)NLMSG_DATA(h);
652 int errnum = err->error;
653 int msg_type = err->msg.nlmsg_type;
654
655 if (h->nlmsg_len
656 < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
657 zlog_err("%s error: message truncated",
658 nl->name);
659 return -1;
660 }
661
662 /*
663 * Parse the extended information before
664 * we actually handle it.
665 * At this point in time we do not
666 * do anything other than report the
667 * issue.
668 */
669 if (h->nlmsg_flags & NLM_F_ACK_TLVS)
670 netlink_parse_extended_ack(h);
671
672 /* If the error field is zero, then this is an
673 * ACK */
674 if (err->error == 0) {
675 if (IS_ZEBRA_DEBUG_KERNEL) {
676 zlog_debug(
677 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
678 __FUNCTION__, nl->name,
679 nl_msg_type_to_str(
680 err->msg.nlmsg_type),
681 err->msg.nlmsg_type,
682 err->msg.nlmsg_seq,
683 err->msg.nlmsg_pid);
684 }
685
686 /* return if not a multipart message,
687 * otherwise continue */
688 if (!(h->nlmsg_flags & NLM_F_MULTI))
689 return 0;
690 continue;
691 }
692
693 /* Deal with errors that occur because of races
694 * in link handling */
695 if (nl == &zns->netlink_cmd
696 && ((msg_type == RTM_DELROUTE
697 && (-errnum == ENODEV
698 || -errnum == ESRCH))
699 || (msg_type == RTM_NEWROUTE
700 && (-errnum == ENETDOWN
701 || -errnum == EEXIST)))) {
702 if (IS_ZEBRA_DEBUG_KERNEL)
703 zlog_debug(
704 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
705 nl->name,
706 safe_strerror(-errnum),
707 nl_msg_type_to_str(
708 msg_type),
709 msg_type,
710 err->msg.nlmsg_seq,
711 err->msg.nlmsg_pid);
712 return 0;
713 }
714
715 /* We see RTM_DELNEIGH when shutting down an
716 * interface with an IPv4
717 * link-local. The kernel should have already
718 * deleted the neighbor
719 * so do not log these as an error.
720 */
721 if (msg_type == RTM_DELNEIGH
722 || (nl == &zns->netlink_cmd
723 && msg_type == RTM_NEWROUTE
724 && (-errnum == ESRCH
725 || -errnum == ENETUNREACH))) {
726 /* This is known to happen in some
727 * situations, don't log
728 * as error.
729 */
730 if (IS_ZEBRA_DEBUG_KERNEL)
731 zlog_debug(
732 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
733 nl->name,
734 safe_strerror(-errnum),
735 nl_msg_type_to_str(
736 msg_type),
737 msg_type,
738 err->msg.nlmsg_seq,
739 err->msg.nlmsg_pid);
740 } else
741 zlog_err(
742 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
743 nl->name,
744 safe_strerror(-errnum),
745 nl_msg_type_to_str(msg_type),
746 msg_type, err->msg.nlmsg_seq,
747 err->msg.nlmsg_pid);
748
749 return -1;
750 }
751
752 /* OK we got netlink message. */
753 if (IS_ZEBRA_DEBUG_KERNEL)
754 zlog_debug(
755 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
756 nl->name,
757 nl_msg_type_to_str(h->nlmsg_type),
758 h->nlmsg_type, h->nlmsg_len,
759 h->nlmsg_seq, h->nlmsg_pid);
760
761
762 /*
763 * Ignore messages that maybe sent from
764 * other actors besides the kernel
765 */
766 if (snl.nl_pid != 0) {
767 zlog_err("Ignoring message from pid %u",
768 snl.nl_pid);
769 continue;
770 }
771
772 error = (*filter)(h, zns->ns_id, startup);
773 if (error < 0) {
774 zlog_err("%s filter function error", nl->name);
775 zlog_backtrace(LOG_ERR);
776 ret = error;
777 }
778 }
779
780 /* After error care. */
781 if (msg.msg_flags & MSG_TRUNC) {
782 zlog_err("%s error: message truncated", nl->name);
783 continue;
784 }
785 if (status) {
786 zlog_err("%s error: data remnant size %d", nl->name,
787 status);
788 return -1;
789 }
790 }
791 return ret;
792 }
793
794 /*
795 * netlink_talk
796 *
797 * sendmsg() to netlink socket then recvmsg().
798 * Calls netlink_parse_info to parse returned data
799 *
800 * filter -> The filter to read final results from kernel
801 * nlmsghdr -> The data to send to the kernel
802 * nl -> The netlink socket information
803 * zns -> The zebra namespace information
804 * startup -> Are we reading in under startup conditions
805 * This is passed through eventually to filter.
806 */
807 int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
808 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
809 int startup)
810 {
811 int status;
812 struct sockaddr_nl snl;
813 struct iovec iov;
814 struct msghdr msg;
815 int save_errno;
816
817 memset(&snl, 0, sizeof snl);
818 memset(&iov, 0, sizeof iov);
819 memset(&msg, 0, sizeof msg);
820
821 iov.iov_base = n;
822 iov.iov_len = n->nlmsg_len;
823 msg.msg_name = (void *)&snl;
824 msg.msg_namelen = sizeof snl;
825 msg.msg_iov = &iov;
826 msg.msg_iovlen = 1;
827
828 snl.nl_family = AF_NETLINK;
829
830 n->nlmsg_seq = ++nl->seq;
831 n->nlmsg_pid = nl->snl.nl_pid;
832
833 /* Request an acknowledgement by setting NLM_F_ACK */
834 n->nlmsg_flags |= NLM_F_ACK;
835
836 if (IS_ZEBRA_DEBUG_KERNEL)
837 zlog_debug(
838 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
839 nl->name, nl_msg_type_to_str(n->nlmsg_type),
840 n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq,
841 n->nlmsg_flags);
842
843 /* Send message to netlink interface. */
844 if (zserv_privs.change(ZPRIVS_RAISE))
845 zlog_err("Can't raise privileges");
846 status = sendmsg(nl->sock, &msg, 0);
847 save_errno = errno;
848 if (zserv_privs.change(ZPRIVS_LOWER))
849 zlog_err("Can't lower privileges");
850
851 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
852 zlog_debug("%s: >> netlink message dump [sent]", __func__);
853 zlog_hexdump(n, n->nlmsg_len);
854 }
855
856 if (status < 0) {
857 zlog_err("netlink_talk sendmsg() error: %s",
858 safe_strerror(save_errno));
859 return -1;
860 }
861
862
863 /*
864 * Get reply from netlink socket.
865 * The reply should either be an acknowlegement or an error.
866 */
867 return netlink_parse_info(filter, nl, zns, 0, startup);
868 }
869
870 /* Issue request message to kernel via netlink socket. GET messages
871 * are issued through this interface.
872 */
873 int netlink_request(struct nlsock *nl, struct nlmsghdr *n)
874 {
875 int ret;
876 struct sockaddr_nl snl;
877 int save_errno;
878
879 /* Check netlink socket. */
880 if (nl->sock < 0) {
881 zlog_err("%s socket isn't active.", nl->name);
882 return -1;
883 }
884
885 /* Fill common fields for all requests. */
886 n->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
887 n->nlmsg_pid = nl->snl.nl_pid;
888 n->nlmsg_seq = ++nl->seq;
889
890 memset(&snl, 0, sizeof snl);
891 snl.nl_family = AF_NETLINK;
892
893 /* Raise capabilities and send message, then lower capabilities. */
894 if (zserv_privs.change(ZPRIVS_RAISE)) {
895 zlog_err("Can't raise privileges");
896 return -1;
897 }
898
899 ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0,
900 (struct sockaddr *)&snl, sizeof snl);
901 save_errno = errno;
902
903 if (zserv_privs.change(ZPRIVS_LOWER))
904 zlog_err("Can't lower privileges");
905
906 if (ret < 0) {
907 zlog_err("%s sendto failed: %s", nl->name,
908 safe_strerror(save_errno));
909 return -1;
910 }
911
912 return 0;
913 }
914
915 /* Exported interface function. This function simply calls
916 netlink_socket (). */
917 void kernel_init(struct zebra_ns *zns)
918 {
919 unsigned long groups;
920 #if defined SOL_NETLINK
921 int one, ret;
922 #endif
923
924 /*
925 * Initialize netlink sockets
926 *
927 * If RTMGRP_XXX exists use that, but at some point
928 * I think the kernel developers realized that
929 * keeping track of all the different values would
930 * lead to confusion, so we need to convert the
931 * RTNLGRP_XXX to a bit position for ourself
932 */
933 groups = RTMGRP_LINK |
934 RTMGRP_IPV4_ROUTE |
935 RTMGRP_IPV4_IFADDR |
936 RTMGRP_IPV6_ROUTE |
937 RTMGRP_IPV6_IFADDR |
938 RTMGRP_IPV4_MROUTE |
939 RTMGRP_NEIGH |
940 (1 << (RTNLGRP_IPV4_RULE - 1)) |
941 (1 << (RTNLGRP_IPV6_RULE - 1));
942
943 snprintf(zns->netlink.name, sizeof(zns->netlink.name),
944 "netlink-listen (NS %u)", zns->ns_id);
945 zns->netlink.sock = -1;
946 netlink_socket(&zns->netlink, groups, zns->ns_id);
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 netlink_socket(&zns->netlink_cmd, 0, zns->ns_id);
952
953 /*
954 * SOL_NETLINK is not available on all platforms yet
955 * apparently. It's in bits/socket.h which I am not
956 * sure that we want to pull into our build system.
957 */
958 #if defined SOL_NETLINK
959 /*
960 * Let's tell the kernel that we want to receive extended
961 * ACKS over our command socket
962 */
963 one = 1;
964 ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK,
965 &one, sizeof(one));
966
967 if (ret < 0)
968 zlog_notice("Registration for extended ACK failed : %d %s",
969 errno, safe_strerror(errno));
970 #endif
971
972 /* Register kernel socket. */
973 if (zns->netlink.sock > 0) {
974 /* Only want non-blocking on the netlink event socket */
975 if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
976 zlog_err("Can't set %s socket flags: %s",
977 zns->netlink.name, safe_strerror(errno));
978
979 /* Set receive buffer size if it's set from command line */
980 if (nl_rcvbufsize)
981 netlink_recvbuf(&zns->netlink, nl_rcvbufsize);
982
983 netlink_install_filter(zns->netlink.sock,
984 zns->netlink_cmd.snl.nl_pid);
985 zns->t_netlink = NULL;
986
987 thread_add_read(zebrad.master, kernel_read, zns,
988 zns->netlink.sock, &zns->t_netlink);
989 }
990
991 rt_netlink_init();
992 }
993
994 void kernel_terminate(struct zebra_ns *zns)
995 {
996 THREAD_READ_OFF(zns->t_netlink);
997
998 if (zns->netlink.sock >= 0) {
999 close(zns->netlink.sock);
1000 zns->netlink.sock = -1;
1001 }
1002
1003 if (zns->netlink_cmd.sock >= 0) {
1004 close(zns->netlink_cmd.sock);
1005 zns->netlink_cmd.sock = -1;
1006 }
1007 }
1008
1009 #endif /* HAVE_NETLINK */