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