]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_netlink.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[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 "rib.h"
33 #include "thread.h"
34 #include "privs.h"
35 #include "nexthop.h"
36 #include "vrf.h"
37 #include "mpls.h"
38 #include "lib_errors.h"
39 #include "hash.h"
40
41 #include "zebra/zebra_router.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/rt.h"
45 #include "zebra/debug.h"
46 #include "zebra/kernel_netlink.h"
47 #include "zebra/rt_netlink.h"
48 #include "zebra/if_netlink.h"
49 #include "zebra/rule_netlink.h"
50 #include "zebra/tc_netlink.h"
51 #include "zebra/netconf_netlink.h"
52 #include "zebra/zebra_errors.h"
53
54 #ifndef SO_RCVBUFFORCE
55 #define SO_RCVBUFFORCE (33)
56 #endif
57
58 /* Hack for GNU libc version 2. */
59 #ifndef MSG_TRUNC
60 #define MSG_TRUNC 0x20
61 #endif /* MSG_TRUNC */
62
63 #ifndef NLMSG_TAIL
64 #define NLMSG_TAIL(nmsg) \
65 ((struct rtattr *)(((uint8_t *)(nmsg)) \
66 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
67 #endif
68
69 #ifndef RTA_TAIL
70 #define RTA_TAIL(rta) \
71 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
72 #endif
73
74 #ifndef RTNL_FAMILY_IP6MR
75 #define RTNL_FAMILY_IP6MR 129
76 #endif
77
78 #ifndef RTPROT_MROUTED
79 #define RTPROT_MROUTED 17
80 #endif
81
82 #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
83
84 /*
85 * We limit the batch's size to a number smaller than the length of the
86 * underlying buffer since the last message that wouldn't fit the batch would go
87 * over the upper boundary and then it would have to be encoded again into a new
88 * buffer. If the difference between the limit and the length of the buffer is
89 * big enough (bigger than the biggest Netlink message) then this situation
90 * won't occur.
91 */
92 #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
93
94 static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"},
95 {RTM_DELROUTE, "RTM_DELROUTE"},
96 {RTM_GETROUTE, "RTM_GETROUTE"},
97 {RTM_NEWLINK, "RTM_NEWLINK"},
98 {RTM_SETLINK, "RTM_SETLINK"},
99 {RTM_DELLINK, "RTM_DELLINK"},
100 {RTM_GETLINK, "RTM_GETLINK"},
101 {RTM_NEWADDR, "RTM_NEWADDR"},
102 {RTM_DELADDR, "RTM_DELADDR"},
103 {RTM_GETADDR, "RTM_GETADDR"},
104 {RTM_NEWNEIGH, "RTM_NEWNEIGH"},
105 {RTM_DELNEIGH, "RTM_DELNEIGH"},
106 {RTM_GETNEIGH, "RTM_GETNEIGH"},
107 {RTM_NEWRULE, "RTM_NEWRULE"},
108 {RTM_DELRULE, "RTM_DELRULE"},
109 {RTM_GETRULE, "RTM_GETRULE"},
110 {RTM_NEWNEXTHOP, "RTM_NEWNEXTHOP"},
111 {RTM_DELNEXTHOP, "RTM_DELNEXTHOP"},
112 {RTM_GETNEXTHOP, "RTM_GETNEXTHOP"},
113 {RTM_NEWNETCONF, "RTM_NEWNETCONF"},
114 {RTM_DELNETCONF, "RTM_DELNETCONF"},
115 {RTM_NEWTUNNEL, "RTM_NEWTUNNEL"},
116 {RTM_DELTUNNEL, "RTM_DELTUNNEL"},
117 {RTM_GETTUNNEL, "RTM_GETTUNNEL"},
118 {RTM_NEWQDISC, "RTM_NEWQDISC"},
119 {RTM_DELQDISC, "RTM_DELQDISC"},
120 {RTM_GETQDISC, "RTM_GETQDISC"},
121 {RTM_NEWTCLASS, "RTM_NEWTCLASS"},
122 {RTM_DELTCLASS, "RTM_DELTCLASS"},
123 {RTM_GETTCLASS, "RTM_GETTCLASS"},
124 {RTM_NEWTFILTER, "RTM_NEWTFILTER"},
125 {RTM_DELTFILTER, "RTM_DELTFILTER"},
126 {RTM_GETTFILTER, "RTM_GETTFILTER"},
127 {RTM_NEWVLAN, "RTM_NEWVLAN"},
128 {RTM_DELVLAN, "RTM_DELVLAN"},
129 {RTM_GETVLAN, "RTM_GETVLAN"},
130 {0}};
131
132 static const struct message rtproto_str[] = {
133 {RTPROT_REDIRECT, "redirect"},
134 {RTPROT_KERNEL, "kernel"},
135 {RTPROT_BOOT, "boot"},
136 {RTPROT_STATIC, "static"},
137 {RTPROT_GATED, "GateD"},
138 {RTPROT_RA, "router advertisement"},
139 {RTPROT_MRT, "MRT"},
140 {RTPROT_ZEBRA, "Zebra"},
141 #ifdef RTPROT_BIRD
142 {RTPROT_BIRD, "BIRD"},
143 #endif /* RTPROT_BIRD */
144 {RTPROT_MROUTED, "mroute"},
145 {RTPROT_BGP, "BGP"},
146 {RTPROT_OSPF, "OSPF"},
147 {RTPROT_ISIS, "IS-IS"},
148 {RTPROT_RIP, "RIP"},
149 {RTPROT_RIPNG, "RIPNG"},
150 {RTPROT_ZSTATIC, "static"},
151 {0}};
152
153 static const struct message family_str[] = {{AF_INET, "ipv4"},
154 {AF_INET6, "ipv6"},
155 {AF_BRIDGE, "bridge"},
156 {RTNL_FAMILY_IPMR, "ipv4MR"},
157 {RTNL_FAMILY_IP6MR, "ipv6MR"},
158 {0}};
159
160 static const struct message rttype_str[] = {{RTN_UNSPEC, "none"},
161 {RTN_UNICAST, "unicast"},
162 {RTN_LOCAL, "local"},
163 {RTN_BROADCAST, "broadcast"},
164 {RTN_ANYCAST, "anycast"},
165 {RTN_MULTICAST, "multicast"},
166 {RTN_BLACKHOLE, "blackhole"},
167 {RTN_UNREACHABLE, "unreachable"},
168 {RTN_PROHIBIT, "prohibited"},
169 {RTN_THROW, "throw"},
170 {RTN_NAT, "nat"},
171 {RTN_XRESOLVE, "resolver"},
172 {0}};
173
174 extern struct thread_master *master;
175
176 extern struct zebra_privs_t zserv_privs;
177
178 DEFINE_MTYPE_STATIC(ZEBRA, NL_BUF, "Zebra Netlink buffers");
179
180 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
181 * We have both the main and dplane pthreads using these structs, so we have
182 * to protect the hash with a lock.
183 */
184 static struct hash *nlsock_hash;
185 pthread_mutex_t nlsock_mutex;
186
187 /* Lock and unlock wrappers for nlsock hash */
188 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
189 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
190
191 size_t nl_batch_tx_bufsize;
192 char *nl_batch_tx_buf;
193
194 _Atomic uint32_t nl_batch_bufsize = NL_DEFAULT_BATCH_BUFSIZE;
195 _Atomic uint32_t nl_batch_send_threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD;
196
197 struct nl_batch {
198 void *buf;
199 size_t bufsiz;
200 size_t limit;
201
202 void *buf_head;
203 size_t curlen;
204 size_t msgcnt;
205
206 const struct zebra_dplane_info *zns;
207
208 struct dplane_ctx_list_head ctx_list;
209
210 /*
211 * Pointer to the queue of completed contexts outbound back
212 * towards the dataplane module.
213 */
214 struct dplane_ctx_list_head *ctx_out_q;
215 };
216
217 int netlink_config_write_helper(struct vty *vty)
218 {
219 uint32_t size =
220 atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed);
221 uint32_t threshold = atomic_load_explicit(&nl_batch_send_threshold,
222 memory_order_relaxed);
223
224 if (size != NL_DEFAULT_BATCH_BUFSIZE
225 || threshold != NL_DEFAULT_BATCH_SEND_THRESHOLD)
226 vty_out(vty, "zebra kernel netlink batch-tx-buf %u %u\n", size,
227 threshold);
228
229 if (if_netlink_frr_protodown_r_bit_is_set())
230 vty_out(vty, "zebra protodown reason-bit %u\n",
231 if_netlink_get_frr_protodown_r_bit());
232
233 return 0;
234 }
235
236 void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold, bool set)
237 {
238 if (!set) {
239 size = NL_DEFAULT_BATCH_BUFSIZE;
240 threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD;
241 }
242
243 atomic_store_explicit(&nl_batch_bufsize, size, memory_order_relaxed);
244 atomic_store_explicit(&nl_batch_send_threshold, threshold,
245 memory_order_relaxed);
246 }
247
248 int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup)
249 {
250 /*
251 * This is an error condition that must be handled during
252 * development.
253 *
254 * The netlink_talk_filter function is used for communication
255 * down the netlink_cmd pipe and we are expecting
256 * an ack being received. So if we get here
257 * then we did not receive the ack and instead
258 * received some other message in an unexpected
259 * way.
260 */
261 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__,
262 h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id);
263 return 0;
264 }
265
266 static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
267 {
268 uint32_t oldsize;
269 socklen_t newlen = sizeof(newsize);
270 socklen_t oldlen = sizeof(oldsize);
271 int ret;
272
273 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
274 if (ret < 0) {
275 flog_err_sys(EC_LIB_SOCKET,
276 "Can't get %s receive buffer size: %s", nl->name,
277 safe_strerror(errno));
278 return -1;
279 }
280
281 /* Try force option (linux >= 2.6.14) and fall back to normal set */
282 frr_with_privs(&zserv_privs) {
283 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE,
284 &rcvbufsize, sizeof(rcvbufsize));
285 }
286 if (ret < 0)
287 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
288 sizeof(rcvbufsize));
289 if (ret < 0) {
290 flog_err_sys(EC_LIB_SOCKET,
291 "Can't set %s receive buffer size: %s", nl->name,
292 safe_strerror(errno));
293 return -1;
294 }
295
296 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
297 if (ret < 0) {
298 flog_err_sys(EC_LIB_SOCKET,
299 "Can't get %s receive buffer size: %s", nl->name,
300 safe_strerror(errno));
301 return -1;
302 }
303 return 0;
304 }
305
306 static const char *group2str(uint32_t group)
307 {
308 switch (group) {
309 case RTNLGRP_TUNNEL:
310 return "RTNLGRP_TUNNEL";
311 default:
312 return "UNKNOWN";
313 }
314 }
315
316 /* Make socket for Linux netlink interface. */
317 static int netlink_socket(struct nlsock *nl, unsigned long groups,
318 uint32_t ext_groups[], uint8_t ext_group_size,
319 ns_id_t ns_id)
320 {
321 int ret;
322 struct sockaddr_nl snl;
323 int sock;
324 int namelen;
325
326 frr_with_privs(&zserv_privs) {
327 sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id);
328 if (sock < 0) {
329 zlog_err("Can't open %s socket: %s", nl->name,
330 safe_strerror(errno));
331 return -1;
332 }
333
334 memset(&snl, 0, sizeof(snl));
335 snl.nl_family = AF_NETLINK;
336 snl.nl_groups = groups;
337
338 if (ext_group_size) {
339 uint8_t i;
340
341 for (i = 0; i < ext_group_size; i++) {
342 #if defined SOL_NETLINK
343 ret = setsockopt(sock, SOL_NETLINK,
344 NETLINK_ADD_MEMBERSHIP,
345 &ext_groups[i],
346 sizeof(ext_groups[i]));
347 if (ret < 0) {
348 zlog_notice(
349 "can't setsockopt NETLINK_ADD_MEMBERSHIP for group %s(%u), this linux kernel does not support it: %s(%d)",
350 group2str(ext_groups[i]),
351 ext_groups[i],
352 safe_strerror(errno), errno);
353 }
354 #else
355 zlog_notice(
356 "Unable to use NETLINK_ADD_MEMBERSHIP via SOL_NETLINK for %s(%u) since the linux kernel does not support the socket option",
357 group2str(ext_groups[i]),
358 ext_groups[i]);
359 #endif
360 }
361 }
362
363 /* Bind the socket to the netlink structure for anything. */
364 ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl));
365 }
366
367 if (ret < 0) {
368 zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name,
369 snl.nl_groups, safe_strerror(errno));
370 close(sock);
371 return -1;
372 }
373
374 /* multiple netlink sockets will have different nl_pid */
375 namelen = sizeof(snl);
376 ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen);
377 if (ret < 0 || namelen != sizeof(snl)) {
378 flog_err_sys(EC_LIB_SOCKET, "Can't get %s socket name: %s",
379 nl->name, safe_strerror(errno));
380 close(sock);
381 return -1;
382 }
383
384 nl->snl = snl;
385 nl->sock = sock;
386 nl->buflen = NL_RCV_PKT_BUF_SIZE;
387 nl->buf = XMALLOC(MTYPE_NL_BUF, nl->buflen);
388
389 return ret;
390 }
391
392 /*
393 * Dispatch an incoming netlink message; used by the zebra main pthread's
394 * netlink event reader.
395 */
396 static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
397 int startup)
398 {
399 /*
400 * When we handle new message types here
401 * because we are starting to install them
402 * then lets check the netlink_install_filter
403 * and see if we should add the corresponding
404 * allow through entry there.
405 * Probably not needed to do but please
406 * think about it.
407 */
408 switch (h->nlmsg_type) {
409 case RTM_NEWROUTE:
410 return netlink_route_change(h, ns_id, startup);
411 case RTM_DELROUTE:
412 return netlink_route_change(h, ns_id, startup);
413 case RTM_NEWLINK:
414 return netlink_link_change(h, ns_id, startup);
415 case RTM_DELLINK:
416 return netlink_link_change(h, ns_id, startup);
417 case RTM_NEWNEIGH:
418 case RTM_DELNEIGH:
419 case RTM_GETNEIGH:
420 return netlink_neigh_change(h, ns_id);
421 case RTM_NEWRULE:
422 return netlink_rule_change(h, ns_id, startup);
423 case RTM_DELRULE:
424 return netlink_rule_change(h, ns_id, startup);
425 case RTM_NEWNEXTHOP:
426 return netlink_nexthop_change(h, ns_id, startup);
427 case RTM_DELNEXTHOP:
428 return netlink_nexthop_change(h, ns_id, startup);
429 case RTM_NEWQDISC:
430 case RTM_DELQDISC:
431 return netlink_qdisc_change(h, ns_id, startup);
432 case RTM_NEWTCLASS:
433 case RTM_DELTCLASS:
434 return netlink_tclass_change(h, ns_id, startup);
435 case RTM_NEWTFILTER:
436 case RTM_DELTFILTER:
437 return netlink_tfilter_change(h, ns_id, startup);
438 case RTM_NEWVLAN:
439 return netlink_vlan_change(h, ns_id, startup);
440 case RTM_DELVLAN:
441 return netlink_vlan_change(h, ns_id, startup);
442
443 /* Messages handled in the dplane thread */
444 case RTM_NEWADDR:
445 case RTM_DELADDR:
446 case RTM_NEWNETCONF:
447 case RTM_DELNETCONF:
448 case RTM_NEWTUNNEL:
449 case RTM_DELTUNNEL:
450 case RTM_GETTUNNEL:
451 return 0;
452 default:
453 /*
454 * If we have received this message then
455 * we have made a mistake during development
456 * and we need to write some code to handle
457 * this message type or not ask for
458 * it to be sent up to us
459 */
460 flog_err(EC_ZEBRA_UNKNOWN_NLMSG,
461 "Unknown netlink nlmsg_type %s(%d) vrf %u",
462 nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type,
463 ns_id);
464 break;
465 }
466 return 0;
467 }
468
469 /*
470 * Dispatch an incoming netlink message; used by the dataplane pthread's
471 * netlink event reader code.
472 */
473 static int dplane_netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
474 int startup)
475 {
476 /*
477 * Dispatch the incoming messages that the dplane pthread handles
478 */
479 switch (h->nlmsg_type) {
480 case RTM_NEWADDR:
481 case RTM_DELADDR:
482 return netlink_interface_addr_dplane(h, ns_id, startup);
483
484 case RTM_NEWNETCONF:
485 case RTM_DELNETCONF:
486 return netlink_netconf_change(h, ns_id, startup);
487
488 /* TODO -- other messages for the dplane socket and pthread */
489
490 case RTM_NEWLINK:
491 case RTM_DELLINK:
492
493 default:
494 break;
495 }
496
497 return 0;
498 }
499
500 static void kernel_read(struct thread *thread)
501 {
502 struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread);
503 struct zebra_dplane_info dp_info;
504
505 /* Capture key info from ns struct */
506 zebra_dplane_info_from_zns(&dp_info, zns, false);
507
508 netlink_parse_info(netlink_information_fetch, &zns->netlink, &dp_info,
509 5, false);
510
511 thread_add_read(zrouter.master, kernel_read, zns, zns->netlink.sock,
512 &zns->t_netlink);
513 }
514
515 /*
516 * Called by the dplane pthread to read incoming OS messages and dispatch them.
517 */
518 int kernel_dplane_read(struct zebra_dplane_info *info)
519 {
520 struct nlsock *nl = kernel_netlink_nlsock_lookup(info->sock);
521
522 netlink_parse_info(dplane_netlink_information_fetch, nl, info, 5,
523 false);
524
525 return 0;
526 }
527
528 /*
529 * Filter out messages from self that occur on listener socket,
530 * caused by our actions on the command socket(s)
531 *
532 * When we add new Netlink message types we probably
533 * do not need to add them here as that we are filtering
534 * on the routes we actually care to receive( which is rarer
535 * then the normal course of operations). We are intentionally
536 * allowing some messages from ourselves through
537 * ( I'm looking at you Interface based netlink messages )
538 * so that we only have to write one way to handle incoming
539 * address add/delete and xxxNETCONF changes.
540 */
541 static void netlink_install_filter(int sock, uint32_t pid, uint32_t dplane_pid)
542 {
543 /*
544 * BPF_JUMP instructions and where you jump to are based upon
545 * 0 as being the next statement. So count from 0. Writing
546 * this down because every time I look at this I have to
547 * re-remember it.
548 */
549 struct sock_filter filter[] = {
550 /*
551 * Logic:
552 * if (nlmsg_pid == pid ||
553 * nlmsg_pid == dplane_pid) {
554 * if (the incoming nlmsg_type ==
555 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
556 * RTM_DELNETCONF)
557 * keep this message
558 * else
559 * skip this message
560 * } else
561 * keep this netlink message
562 */
563 /*
564 * 0: Load the nlmsg_pid into the BPF register
565 */
566 BPF_STMT(BPF_LD | BPF_ABS | BPF_W,
567 offsetof(struct nlmsghdr, nlmsg_pid)),
568 /*
569 * 1: Compare to pid
570 */
571 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 1, 0),
572 /*
573 * 2: Compare to dplane pid
574 */
575 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(dplane_pid), 0, 6),
576 /*
577 * 3: Load the nlmsg_type into BPF register
578 */
579 BPF_STMT(BPF_LD | BPF_ABS | BPF_H,
580 offsetof(struct nlmsghdr, nlmsg_type)),
581 /*
582 * 4: Compare to RTM_NEWADDR
583 */
584 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 4, 0),
585 /*
586 * 5: Compare to RTM_DELADDR
587 */
588 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 3, 0),
589 /*
590 * 6: Compare to RTM_NEWNETCONF
591 */
592 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWNETCONF), 2,
593 0),
594 /*
595 * 7: Compare to RTM_DELNETCONF
596 */
597 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELNETCONF), 1,
598 0),
599 /*
600 * 8: This is the end state of we want to skip the
601 * message
602 */
603 BPF_STMT(BPF_RET | BPF_K, 0),
604 /* 9: This is the end state of we want to keep
605 * the message
606 */
607 BPF_STMT(BPF_RET | BPF_K, 0xffff),
608 };
609
610 struct sock_fprog prog = {
611 .len = array_size(filter), .filter = filter,
612 };
613
614 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))
615 < 0)
616 flog_err_sys(EC_LIB_SOCKET, "Can't install socket filter: %s",
617 safe_strerror(errno));
618 }
619
620 void netlink_parse_rtattr_flags(struct rtattr **tb, int max, struct rtattr *rta,
621 int len, unsigned short flags)
622 {
623 unsigned short type;
624
625 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
626 while (RTA_OK(rta, len)) {
627 type = rta->rta_type & ~flags;
628 if ((type <= max) && (!tb[type]))
629 tb[type] = rta;
630 rta = RTA_NEXT(rta, len);
631 }
632 }
633
634 void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
635 int len)
636 {
637 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
638 while (RTA_OK(rta, len)) {
639 if (rta->rta_type <= max)
640 tb[rta->rta_type] = rta;
641 rta = RTA_NEXT(rta, len);
642 }
643 }
644
645 /**
646 * netlink_parse_rtattr_nested() - Parses a nested route attribute
647 * @tb: Pointer to array for storing rtattr in.
648 * @max: Max number to store.
649 * @rta: Pointer to rtattr to look for nested items in.
650 */
651 void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
652 struct rtattr *rta)
653 {
654 netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta));
655 }
656
657 bool nl_addraw_l(struct nlmsghdr *n, unsigned int maxlen, const void *data,
658 unsigned int len)
659 {
660 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
661 zlog_err("ERROR message exceeded bound of %d", maxlen);
662 return false;
663 }
664
665 memcpy(NLMSG_TAIL(n), data, len);
666 memset((uint8_t *)NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
667 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
668
669 return true;
670 }
671
672 bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type,
673 const void *data, unsigned int alen)
674 {
675 int len;
676 struct rtattr *rta;
677
678 len = RTA_LENGTH(alen);
679
680 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
681 return false;
682
683 rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
684 rta->rta_type = type;
685 rta->rta_len = len;
686
687 if (data)
688 memcpy(RTA_DATA(rta), data, alen);
689 else
690 assert(alen == 0);
691
692 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
693
694 return true;
695 }
696
697 bool nl_attr_put8(struct nlmsghdr *n, unsigned int maxlen, int type,
698 uint8_t data)
699 {
700 return nl_attr_put(n, maxlen, type, &data, sizeof(uint8_t));
701 }
702
703 bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type,
704 uint16_t data)
705 {
706 return nl_attr_put(n, maxlen, type, &data, sizeof(uint16_t));
707 }
708
709 bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type,
710 uint32_t data)
711 {
712 return nl_attr_put(n, maxlen, type, &data, sizeof(uint32_t));
713 }
714
715 bool nl_attr_put64(struct nlmsghdr *n, unsigned int maxlen, int type,
716 uint64_t data)
717 {
718 return nl_attr_put(n, maxlen, type, &data, sizeof(uint64_t));
719 }
720
721 struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen, int type)
722 {
723 struct rtattr *nest = NLMSG_TAIL(n);
724
725 if (!nl_attr_put(n, maxlen, type, NULL, 0))
726 return NULL;
727
728 nest->rta_type |= NLA_F_NESTED;
729 return nest;
730 }
731
732 int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
733 {
734 nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest;
735 return n->nlmsg_len;
736 }
737
738 struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen)
739 {
740 struct rtnexthop *rtnh = (struct rtnexthop *)NLMSG_TAIL(n);
741
742 if (NLMSG_ALIGN(n->nlmsg_len) + RTNH_ALIGN(sizeof(struct rtnexthop))
743 > maxlen)
744 return NULL;
745
746 memset(rtnh, 0, sizeof(struct rtnexthop));
747 n->nlmsg_len =
748 NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(sizeof(struct rtnexthop));
749
750 return rtnh;
751 }
752
753 void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh)
754 {
755 rtnh->rtnh_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)rtnh;
756 }
757
758 bool nl_rta_put(struct rtattr *rta, unsigned int maxlen, int type,
759 const void *data, int alen)
760 {
761 struct rtattr *subrta;
762 int len = RTA_LENGTH(alen);
763
764 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
765 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
766 maxlen);
767 return false;
768 }
769 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
770 subrta->rta_type = type;
771 subrta->rta_len = len;
772 if (alen)
773 memcpy(RTA_DATA(subrta), data, alen);
774 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
775
776 return true;
777 }
778
779 bool nl_rta_put16(struct rtattr *rta, unsigned int maxlen, int type,
780 uint16_t data)
781 {
782 return nl_rta_put(rta, maxlen, type, &data, sizeof(uint16_t));
783 }
784
785 bool nl_rta_put64(struct rtattr *rta, unsigned int maxlen, int type,
786 uint64_t data)
787 {
788 return nl_rta_put(rta, maxlen, type, &data, sizeof(uint64_t));
789 }
790
791 struct rtattr *nl_rta_nest(struct rtattr *rta, unsigned int maxlen, int type)
792 {
793 struct rtattr *nest = RTA_TAIL(rta);
794
795 if (nl_rta_put(rta, maxlen, type, NULL, 0))
796 return NULL;
797
798 nest->rta_type |= NLA_F_NESTED;
799
800 return nest;
801 }
802
803 int nl_rta_nest_end(struct rtattr *rta, struct rtattr *nest)
804 {
805 nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest;
806
807 return rta->rta_len;
808 }
809
810 const char *nl_msg_type_to_str(uint16_t msg_type)
811 {
812 return lookup_msg(nlmsg_str, msg_type, "");
813 }
814
815 const char *nl_rtproto_to_str(uint8_t rtproto)
816 {
817 return lookup_msg(rtproto_str, rtproto, "");
818 }
819
820 const char *nl_family_to_str(uint8_t family)
821 {
822 return lookup_msg(family_str, family, "");
823 }
824
825 const char *nl_rttype_to_str(uint8_t rttype)
826 {
827 return lookup_msg(rttype_str, rttype, "");
828 }
829
830 #define NLA_OK(nla, len) \
831 ((len) >= (int)sizeof(struct nlattr) \
832 && (nla)->nla_len >= sizeof(struct nlattr) \
833 && (nla)->nla_len <= (len))
834 #define NLA_NEXT(nla, attrlen) \
835 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
836 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
837 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
838 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
839
840 #define ERR_NLA(err, inner_len) \
841 ((struct nlattr *)(((char *)(err)) \
842 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
843 + NLMSG_ALIGN((inner_len))))
844
845 static void netlink_parse_nlattr(struct nlattr **tb, int max,
846 struct nlattr *nla, int len)
847 {
848 while (NLA_OK(nla, len)) {
849 if (nla->nla_type <= max)
850 tb[nla->nla_type] = nla;
851 nla = NLA_NEXT(nla, len);
852 }
853 }
854
855 static void netlink_parse_extended_ack(struct nlmsghdr *h)
856 {
857 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
858 const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(h);
859 const struct nlmsghdr *err_nlh = NULL;
860 /* Length not including nlmsghdr */
861 uint32_t len = 0;
862 /* Inner error netlink message length */
863 uint32_t inner_len = 0;
864 const char *msg = NULL;
865 uint32_t off = 0;
866
867 if (!(h->nlmsg_flags & NLM_F_CAPPED))
868 inner_len = (uint32_t)NLMSG_PAYLOAD(&err->msg, 0);
869
870 len = (uint32_t)(NLMSG_PAYLOAD(h, sizeof(struct nlmsgerr)) - inner_len);
871
872 netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, ERR_NLA(err, inner_len),
873 len);
874
875 if (tb[NLMSGERR_ATTR_MSG])
876 msg = (const char *)NLA_DATA(tb[NLMSGERR_ATTR_MSG]);
877
878 if (tb[NLMSGERR_ATTR_OFFS]) {
879 off = *(uint32_t *)NLA_DATA(tb[NLMSGERR_ATTR_OFFS]);
880
881 if (off > h->nlmsg_len) {
882 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
883 } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) {
884 /*
885 * Header of failed message
886 * we are not doing anything currently with it
887 * but noticing it for later.
888 */
889 err_nlh = &err->msg;
890 zlog_debug("%s: Received %s extended Ack", __func__,
891 nl_msg_type_to_str(err_nlh->nlmsg_type));
892 }
893 }
894
895 if (msg && *msg != '\0') {
896 bool is_err = !!err->error;
897
898 if (is_err)
899 zlog_err("Extended Error: %s", msg);
900 else
901 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING,
902 "Extended Warning: %s", msg);
903 }
904 }
905
906 /*
907 * netlink_send_msg - send a netlink message of a certain size.
908 *
909 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
910 */
911 static ssize_t netlink_send_msg(const struct nlsock *nl, void *buf,
912 size_t buflen)
913 {
914 struct sockaddr_nl snl = {};
915 struct iovec iov = {};
916 struct msghdr msg = {};
917 ssize_t status;
918 int save_errno = 0;
919
920 iov.iov_base = buf;
921 iov.iov_len = buflen;
922 msg.msg_name = &snl;
923 msg.msg_namelen = sizeof(snl);
924 msg.msg_iov = &iov;
925 msg.msg_iovlen = 1;
926
927 snl.nl_family = AF_NETLINK;
928
929 /* Send message to netlink interface. */
930 frr_with_privs(&zserv_privs) {
931 status = sendmsg(nl->sock, &msg, 0);
932 save_errno = errno;
933 }
934
935 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
936 zlog_debug("%s: >> netlink message dump [sent]", __func__);
937 #ifdef NETLINK_DEBUG
938 nl_dump(buf, buflen);
939 #else
940 zlog_hexdump(buf, buflen);
941 #endif /* NETLINK_DEBUG */
942 }
943
944 if (status == -1) {
945 flog_err_sys(EC_LIB_SOCKET, "%s error: %s", __func__,
946 safe_strerror(save_errno));
947 return -1;
948 }
949
950 return status;
951 }
952
953 /*
954 * netlink_recv_msg - receive a netlink message.
955 *
956 * Returns -1 on error, 0 if read would block or the number of bytes received.
957 */
958 static int netlink_recv_msg(struct nlsock *nl, struct msghdr *msg)
959 {
960 struct iovec iov;
961 int status;
962
963 iov.iov_base = nl->buf;
964 iov.iov_len = nl->buflen;
965 msg->msg_iov = &iov;
966 msg->msg_iovlen = 1;
967
968 do {
969 int bytes;
970
971 bytes = recv(nl->sock, NULL, 0, MSG_PEEK | MSG_TRUNC);
972
973 if (bytes >= 0 && (size_t)bytes > nl->buflen) {
974 nl->buf = XREALLOC(MTYPE_NL_BUF, nl->buf, bytes);
975 nl->buflen = bytes;
976 iov.iov_base = nl->buf;
977 iov.iov_len = nl->buflen;
978 }
979
980 status = recvmsg(nl->sock, msg, 0);
981 } while (status == -1 && errno == EINTR);
982
983 if (status == -1) {
984 if (errno == EWOULDBLOCK || errno == EAGAIN)
985 return 0;
986 flog_err(EC_ZEBRA_RECVMSG_OVERRUN, "%s recvmsg overrun: %s",
987 nl->name, safe_strerror(errno));
988 /*
989 * In this case we are screwed. There is no good way to recover
990 * zebra at this point.
991 */
992 exit(-1);
993 }
994
995 if (status == 0) {
996 flog_err_sys(EC_LIB_SOCKET, "%s EOF", nl->name);
997 return -1;
998 }
999
1000 if (msg->msg_namelen != sizeof(struct sockaddr_nl)) {
1001 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1002 "%s sender address length error: length %d", nl->name,
1003 msg->msg_namelen);
1004 return -1;
1005 }
1006
1007 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
1008 zlog_debug("%s: << netlink message dump [recv]", __func__);
1009 #ifdef NETLINK_DEBUG
1010 nl_dump(nl->buf, status);
1011 #else
1012 zlog_hexdump(nl->buf, status);
1013 #endif /* NETLINK_DEBUG */
1014 }
1015
1016 return status;
1017 }
1018
1019 /*
1020 * netlink_parse_error - parse a netlink error message
1021 *
1022 * Returns 1 if this message is acknowledgement, 0 if this error should be
1023 * ignored, -1 otherwise.
1024 */
1025 static int netlink_parse_error(const struct nlsock *nl, struct nlmsghdr *h,
1026 bool is_cmd, bool startup)
1027 {
1028 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
1029 int errnum = err->error;
1030 int msg_type = err->msg.nlmsg_type;
1031
1032 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1033 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1034 "%s error: message truncated", nl->name);
1035 return -1;
1036 }
1037
1038 /*
1039 * Parse the extended information before we actually handle it. At this
1040 * point in time we do not do anything other than report the issue.
1041 */
1042 if (h->nlmsg_flags & NLM_F_ACK_TLVS)
1043 netlink_parse_extended_ack(h);
1044
1045 /* If the error field is zero, then this is an ACK. */
1046 if (err->error == 0) {
1047 if (IS_ZEBRA_DEBUG_KERNEL) {
1048 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
1049 __func__, nl->name,
1050 nl_msg_type_to_str(err->msg.nlmsg_type),
1051 err->msg.nlmsg_type, err->msg.nlmsg_seq,
1052 err->msg.nlmsg_pid);
1053 }
1054
1055 return 1;
1056 }
1057
1058 /*
1059 * Deal with errors that occur because of races in link handling
1060 * or types are not supported in kernel.
1061 */
1062 if (is_cmd &&
1063 ((msg_type == RTM_DELROUTE &&
1064 (-errnum == ENODEV || -errnum == ESRCH)) ||
1065 (msg_type == RTM_NEWROUTE &&
1066 (-errnum == ENETDOWN || -errnum == EEXIST)) ||
1067 ((msg_type == RTM_NEWTUNNEL || msg_type == RTM_DELTUNNEL ||
1068 msg_type == RTM_GETTUNNEL) &&
1069 (-errnum == EOPNOTSUPP)))) {
1070 if (IS_ZEBRA_DEBUG_KERNEL)
1071 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1072 nl->name, safe_strerror(-errnum),
1073 nl_msg_type_to_str(msg_type), msg_type,
1074 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1075 return 0;
1076 }
1077
1078 /*
1079 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1080 * link-local. The kernel should have already deleted the neighbor so
1081 * do not log these as an error.
1082 */
1083 if (msg_type == RTM_DELNEIGH
1084 || (is_cmd && msg_type == RTM_NEWROUTE
1085 && (-errnum == ESRCH || -errnum == ENETUNREACH))) {
1086 /*
1087 * This is known to happen in some situations, don't log as
1088 * error.
1089 */
1090 if (IS_ZEBRA_DEBUG_KERNEL)
1091 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1092 nl->name, safe_strerror(-errnum),
1093 nl_msg_type_to_str(msg_type), msg_type,
1094 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1095 } else {
1096 if ((msg_type != RTM_GETNEXTHOP && msg_type != RTM_GETVLAN) ||
1097 !startup)
1098 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE,
1099 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1100 nl->name, safe_strerror(-errnum),
1101 nl_msg_type_to_str(msg_type), msg_type,
1102 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1103 }
1104
1105 return -1;
1106 }
1107
1108 /*
1109 * netlink_parse_info
1110 *
1111 * Receive message from netlink interface and pass those information
1112 * to the given function.
1113 *
1114 * filter -> Function to call to read the results
1115 * nl -> netlink socket information
1116 * zns -> The zebra namespace data
1117 * count -> How many we should read in, 0 means as much as possible
1118 * startup -> Are we reading in under startup conditions? passed to
1119 * the filter.
1120 */
1121 int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
1122 struct nlsock *nl, const struct zebra_dplane_info *zns,
1123 int count, bool startup)
1124 {
1125 int status;
1126 int ret = 0;
1127 int error;
1128 int read_in = 0;
1129
1130 while (1) {
1131 struct sockaddr_nl snl;
1132 struct msghdr msg = {.msg_name = (void *)&snl,
1133 .msg_namelen = sizeof(snl)};
1134 struct nlmsghdr *h;
1135
1136 if (count && read_in >= count)
1137 return 0;
1138
1139 status = netlink_recv_msg(nl, &msg);
1140 if (status == -1)
1141 return -1;
1142 else if (status == 0)
1143 break;
1144
1145 read_in++;
1146 for (h = (struct nlmsghdr *)nl->buf;
1147 (status >= 0 && NLMSG_OK(h, (unsigned int)status));
1148 h = NLMSG_NEXT(h, status)) {
1149 /* Finish of reading. */
1150 if (h->nlmsg_type == NLMSG_DONE)
1151 return ret;
1152
1153 /* Error handling. */
1154 if (h->nlmsg_type == NLMSG_ERROR) {
1155 int err = netlink_parse_error(
1156 nl, h, zns->is_cmd, startup);
1157
1158 if (err == 1) {
1159 if (!(h->nlmsg_flags & NLM_F_MULTI))
1160 return 0;
1161 continue;
1162 } else
1163 return err;
1164 }
1165
1166 /*
1167 * What is the right thing to do? The kernel
1168 * is telling us that the dump request was interrupted
1169 * and we more than likely are out of luck and have
1170 * missed data from the kernel. At this point in time
1171 * lets just note that this is happening.
1172 */
1173 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
1174 flog_err(
1175 EC_ZEBRA_NETLINK_BAD_SEQUENCE,
1176 "netlink recvmsg: The Dump request was interrupted");
1177
1178 /* OK we got netlink message. */
1179 if (IS_ZEBRA_DEBUG_KERNEL)
1180 zlog_debug(
1181 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1182 __func__, nl->name,
1183 nl_msg_type_to_str(h->nlmsg_type),
1184 h->nlmsg_type, h->nlmsg_len,
1185 h->nlmsg_seq, h->nlmsg_pid);
1186
1187
1188 /*
1189 * Ignore messages that maybe sent from
1190 * other actors besides the kernel
1191 */
1192 if (snl.nl_pid != 0) {
1193 zlog_debug("Ignoring message from pid %u",
1194 snl.nl_pid);
1195 continue;
1196 }
1197
1198 error = (*filter)(h, zns->ns_id, startup);
1199 if (error < 0) {
1200 zlog_debug("%s filter function error",
1201 nl->name);
1202 ret = error;
1203 }
1204 }
1205
1206 /* After error care. */
1207 if (msg.msg_flags & MSG_TRUNC) {
1208 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1209 "%s error: message truncated", nl->name);
1210 continue;
1211 }
1212 if (status) {
1213 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1214 "%s error: data remnant size %d", nl->name,
1215 status);
1216 return -1;
1217 }
1218 }
1219 return ret;
1220 }
1221
1222 /*
1223 * netlink_talk_info
1224 *
1225 * sendmsg() to netlink socket then recvmsg().
1226 * Calls netlink_parse_info to parse returned data
1227 *
1228 * filter -> The filter to read final results from kernel
1229 * nlmsghdr -> The data to send to the kernel
1230 * dp_info -> The dataplane and netlink socket information
1231 * startup -> Are we reading in under startup conditions
1232 * This is passed through eventually to filter.
1233 */
1234 static int netlink_talk_info(int (*filter)(struct nlmsghdr *, ns_id_t,
1235 int startup),
1236 struct nlmsghdr *n,
1237 struct zebra_dplane_info *dp_info, bool startup)
1238 {
1239 struct nlsock *nl;
1240
1241 nl = kernel_netlink_nlsock_lookup(dp_info->sock);
1242 n->nlmsg_seq = dp_info->seq;
1243 n->nlmsg_pid = nl->snl.nl_pid;
1244
1245 if (IS_ZEBRA_DEBUG_KERNEL)
1246 zlog_debug(
1247 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1248 nl->name, nl_msg_type_to_str(n->nlmsg_type),
1249 n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq,
1250 n->nlmsg_flags);
1251
1252 if (netlink_send_msg(nl, n, n->nlmsg_len) == -1)
1253 return -1;
1254
1255 /*
1256 * Get reply from netlink socket.
1257 * The reply should either be an acknowlegement or an error.
1258 */
1259 return netlink_parse_info(filter, nl, dp_info, 0, startup);
1260 }
1261
1262 /*
1263 * Synchronous version of netlink_talk_info. Converts args to suit the
1264 * common version, which is suitable for both sync and async use.
1265 */
1266 int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
1267 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
1268 bool startup)
1269 {
1270 struct zebra_dplane_info dp_info;
1271
1272 /* Increment sequence number before capturing snapshot of ns socket
1273 * info.
1274 */
1275 nl->seq++;
1276
1277 /* Capture info in intermediate info struct */
1278 zebra_dplane_info_from_zns(&dp_info, zns, (nl == &(zns->netlink_cmd)));
1279
1280 return netlink_talk_info(filter, n, &dp_info, startup);
1281 }
1282
1283 /* Issue request message to kernel via netlink socket. GET messages
1284 * are issued through this interface.
1285 */
1286 int netlink_request(struct nlsock *nl, void *req)
1287 {
1288 struct nlmsghdr *n = (struct nlmsghdr *)req;
1289
1290 /* Check netlink socket. */
1291 if (nl->sock < 0) {
1292 flog_err_sys(EC_LIB_SOCKET, "%s socket isn't active.",
1293 nl->name);
1294 return -1;
1295 }
1296
1297 /* Fill common fields for all requests. */
1298 n->nlmsg_pid = nl->snl.nl_pid;
1299 n->nlmsg_seq = ++nl->seq;
1300
1301 if (netlink_send_msg(nl, req, n->nlmsg_len) == -1)
1302 return -1;
1303
1304 return 0;
1305 }
1306
1307 static int nl_batch_read_resp(struct nl_batch *bth)
1308 {
1309 struct nlmsghdr *h;
1310 struct sockaddr_nl snl;
1311 struct msghdr msg = {};
1312 int status, seq;
1313 struct nlsock *nl;
1314 struct zebra_dplane_ctx *ctx;
1315 bool ignore_msg;
1316
1317 nl = kernel_netlink_nlsock_lookup(bth->zns->sock);
1318
1319 msg.msg_name = (void *)&snl;
1320 msg.msg_namelen = sizeof(snl);
1321
1322 /*
1323 * The responses are not batched, so we need to read and process one
1324 * message at a time.
1325 */
1326 while (true) {
1327 status = netlink_recv_msg(nl, &msg);
1328 /*
1329 * status == -1 is a full on failure somewhere
1330 * since we don't know where the problem happened
1331 * we must mark all as failed
1332 *
1333 * Else we mark everything as worked
1334 *
1335 */
1336 if (status == -1 || status == 0) {
1337 while ((ctx = dplane_ctx_dequeue(&(bth->ctx_list))) !=
1338 NULL) {
1339 if (status == -1)
1340 dplane_ctx_set_status(
1341 ctx,
1342 ZEBRA_DPLANE_REQUEST_FAILURE);
1343 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
1344 }
1345 return status;
1346 }
1347
1348 h = (struct nlmsghdr *)nl->buf;
1349 ignore_msg = false;
1350 seq = h->nlmsg_seq;
1351 /*
1352 * Find the corresponding context object. Received responses are
1353 * in the same order as requests we sent, so we can simply
1354 * iterate over the context list and match responses with
1355 * requests at same time.
1356 */
1357 while (true) {
1358 ctx = dplane_ctx_get_head(&(bth->ctx_list));
1359 if (ctx == NULL) {
1360 /*
1361 * This is a situation where we have gotten
1362 * into a bad spot. We need to know that
1363 * this happens( does it? )
1364 */
1365 zlog_err(
1366 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1367 __func__);
1368 break;
1369 }
1370
1371 /*
1372 * 'update' context objects take two consecutive
1373 * sequence numbers.
1374 */
1375 if (dplane_ctx_is_update(ctx) &&
1376 dplane_ctx_get_ns(ctx)->seq + 1 == seq) {
1377 /*
1378 * This is the situation where we get a response
1379 * to a message that should be ignored.
1380 */
1381 ignore_msg = true;
1382 break;
1383 }
1384
1385 ctx = dplane_ctx_dequeue(&(bth->ctx_list));
1386 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
1387
1388 /* We have found corresponding context object. */
1389 if (dplane_ctx_get_ns(ctx)->seq == seq)
1390 break;
1391
1392 if (dplane_ctx_get_ns(ctx)->seq > seq)
1393 zlog_warn(
1394 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1395 __func__, seq,
1396 dplane_ctx_get_ns(ctx)->seq);
1397 }
1398
1399 if (ignore_msg) {
1400 /*
1401 * If we ignore the message due to an update
1402 * above we should still fricking decode the
1403 * message for our operator to understand
1404 * what is going on
1405 */
1406 int err = netlink_parse_error(nl, h, bth->zns->is_cmd,
1407 false);
1408
1409 zlog_debug("%s: netlink error message seq=%d %d",
1410 __func__, h->nlmsg_seq, err);
1411 continue;
1412 }
1413
1414 /*
1415 * We received a message with the sequence number that isn't
1416 * associated with any dplane context object.
1417 */
1418 if (ctx == NULL) {
1419 if (IS_ZEBRA_DEBUG_KERNEL)
1420 zlog_debug(
1421 "%s: skipping unassociated response, seq number %d NS %u",
1422 __func__, h->nlmsg_seq,
1423 bth->zns->ns_id);
1424 continue;
1425 }
1426
1427 if (h->nlmsg_type == NLMSG_ERROR) {
1428 int err = netlink_parse_error(nl, h, bth->zns->is_cmd,
1429 false);
1430
1431 if (err == -1)
1432 dplane_ctx_set_status(
1433 ctx, ZEBRA_DPLANE_REQUEST_FAILURE);
1434
1435 if (IS_ZEBRA_DEBUG_KERNEL)
1436 zlog_debug("%s: netlink error message seq=%d ",
1437 __func__, h->nlmsg_seq);
1438 continue;
1439 }
1440
1441 /*
1442 * If we get here then we did not receive neither the ack nor
1443 * the error and instead received some other message in an
1444 * unexpected way.
1445 */
1446 if (IS_ZEBRA_DEBUG_KERNEL)
1447 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1448 __func__, h->nlmsg_type,
1449 nl_msg_type_to_str(h->nlmsg_type),
1450 bth->zns->ns_id);
1451 }
1452
1453 return 0;
1454 }
1455
1456 static void nl_batch_reset(struct nl_batch *bth)
1457 {
1458 bth->buf_head = bth->buf;
1459 bth->curlen = 0;
1460 bth->msgcnt = 0;
1461 bth->zns = NULL;
1462
1463 dplane_ctx_q_init(&(bth->ctx_list));
1464 }
1465
1466 static void nl_batch_init(struct nl_batch *bth,
1467 struct dplane_ctx_list_head *ctx_out_q)
1468 {
1469 /*
1470 * If the size of the buffer has changed, free and then allocate a new
1471 * one.
1472 */
1473 size_t bufsize =
1474 atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed);
1475 if (bufsize != nl_batch_tx_bufsize) {
1476 if (nl_batch_tx_buf)
1477 XFREE(MTYPE_NL_BUF, nl_batch_tx_buf);
1478
1479 nl_batch_tx_buf = XCALLOC(MTYPE_NL_BUF, bufsize);
1480 nl_batch_tx_bufsize = bufsize;
1481 }
1482
1483 bth->buf = nl_batch_tx_buf;
1484 bth->bufsiz = bufsize;
1485 bth->limit = atomic_load_explicit(&nl_batch_send_threshold,
1486 memory_order_relaxed);
1487
1488 bth->ctx_out_q = ctx_out_q;
1489
1490 nl_batch_reset(bth);
1491 }
1492
1493 static void nl_batch_send(struct nl_batch *bth)
1494 {
1495 struct zebra_dplane_ctx *ctx;
1496 bool err = false;
1497
1498 if (bth->curlen != 0 && bth->zns != NULL) {
1499 struct nlsock *nl =
1500 kernel_netlink_nlsock_lookup(bth->zns->sock);
1501
1502 if (IS_ZEBRA_DEBUG_KERNEL)
1503 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1504 __func__, nl->name, bth->curlen,
1505 bth->msgcnt);
1506
1507 if (netlink_send_msg(nl, bth->buf, bth->curlen) == -1)
1508 err = true;
1509
1510 if (!err) {
1511 if (nl_batch_read_resp(bth) == -1)
1512 err = true;
1513 }
1514 }
1515
1516 /* Move remaining contexts to the outbound queue. */
1517 while (true) {
1518 ctx = dplane_ctx_dequeue(&(bth->ctx_list));
1519 if (ctx == NULL)
1520 break;
1521
1522 if (err)
1523 dplane_ctx_set_status(ctx,
1524 ZEBRA_DPLANE_REQUEST_FAILURE);
1525
1526 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
1527 }
1528
1529 nl_batch_reset(bth);
1530 }
1531
1532 enum netlink_msg_status netlink_batch_add_msg(
1533 struct nl_batch *bth, struct zebra_dplane_ctx *ctx,
1534 ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t),
1535 bool ignore_res)
1536 {
1537 int seq;
1538 ssize_t size;
1539 struct nlmsghdr *msgh;
1540 struct nlsock *nl;
1541
1542 size = (*msg_encoder)(ctx, bth->buf_head, bth->bufsiz - bth->curlen);
1543
1544 /*
1545 * If there was an error while encoding the message (other than buffer
1546 * overflow) then return an error.
1547 */
1548 if (size < 0)
1549 return FRR_NETLINK_ERROR;
1550
1551 /*
1552 * If the message doesn't fit entirely in the buffer then send the batch
1553 * and retry.
1554 */
1555 if (size == 0) {
1556 nl_batch_send(bth);
1557 size = (*msg_encoder)(ctx, bth->buf_head,
1558 bth->bufsiz - bth->curlen);
1559 /*
1560 * If the message doesn't fit in the empty buffer then just
1561 * return an error.
1562 */
1563 if (size <= 0)
1564 return FRR_NETLINK_ERROR;
1565 }
1566
1567 seq = dplane_ctx_get_ns(ctx)->seq;
1568 nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
1569
1570 if (ignore_res)
1571 seq++;
1572
1573 msgh = (struct nlmsghdr *)bth->buf_head;
1574 msgh->nlmsg_seq = seq;
1575 msgh->nlmsg_pid = nl->snl.nl_pid;
1576
1577 bth->zns = dplane_ctx_get_ns(ctx);
1578 bth->buf_head = ((char *)bth->buf_head) + size;
1579 bth->curlen += size;
1580 bth->msgcnt++;
1581
1582 return FRR_NETLINK_QUEUED;
1583 }
1584
1585 static enum netlink_msg_status nl_put_msg(struct nl_batch *bth,
1586 struct zebra_dplane_ctx *ctx)
1587 {
1588 if (dplane_ctx_is_skip_kernel(ctx))
1589 return FRR_NETLINK_SUCCESS;
1590
1591 switch (dplane_ctx_get_op(ctx)) {
1592
1593 case DPLANE_OP_ROUTE_INSTALL:
1594 case DPLANE_OP_ROUTE_UPDATE:
1595 case DPLANE_OP_ROUTE_DELETE:
1596 return netlink_put_route_update_msg(bth, ctx);
1597
1598 case DPLANE_OP_NH_INSTALL:
1599 case DPLANE_OP_NH_UPDATE:
1600 case DPLANE_OP_NH_DELETE:
1601 return netlink_put_nexthop_update_msg(bth, ctx);
1602
1603 case DPLANE_OP_LSP_INSTALL:
1604 case DPLANE_OP_LSP_UPDATE:
1605 case DPLANE_OP_LSP_DELETE:
1606 return netlink_put_lsp_update_msg(bth, ctx);
1607
1608 case DPLANE_OP_PW_INSTALL:
1609 case DPLANE_OP_PW_UNINSTALL:
1610 return netlink_put_pw_update_msg(bth, ctx);
1611
1612 case DPLANE_OP_ADDR_INSTALL:
1613 case DPLANE_OP_ADDR_UNINSTALL:
1614 return netlink_put_address_update_msg(bth, ctx);
1615
1616 case DPLANE_OP_MAC_INSTALL:
1617 case DPLANE_OP_MAC_DELETE:
1618 return netlink_put_mac_update_msg(bth, ctx);
1619
1620 case DPLANE_OP_NEIGH_INSTALL:
1621 case DPLANE_OP_NEIGH_UPDATE:
1622 case DPLANE_OP_NEIGH_DELETE:
1623 case DPLANE_OP_VTEP_ADD:
1624 case DPLANE_OP_VTEP_DELETE:
1625 case DPLANE_OP_NEIGH_DISCOVER:
1626 case DPLANE_OP_NEIGH_IP_INSTALL:
1627 case DPLANE_OP_NEIGH_IP_DELETE:
1628 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1629 return netlink_put_neigh_update_msg(bth, ctx);
1630
1631 case DPLANE_OP_RULE_ADD:
1632 case DPLANE_OP_RULE_DELETE:
1633 case DPLANE_OP_RULE_UPDATE:
1634 return netlink_put_rule_update_msg(bth, ctx);
1635
1636 case DPLANE_OP_SYS_ROUTE_ADD:
1637 case DPLANE_OP_SYS_ROUTE_DELETE:
1638 case DPLANE_OP_ROUTE_NOTIFY:
1639 case DPLANE_OP_LSP_NOTIFY:
1640 case DPLANE_OP_BR_PORT_UPDATE:
1641 return FRR_NETLINK_SUCCESS;
1642
1643 case DPLANE_OP_IPTABLE_ADD:
1644 case DPLANE_OP_IPTABLE_DELETE:
1645 case DPLANE_OP_IPSET_ADD:
1646 case DPLANE_OP_IPSET_DELETE:
1647 case DPLANE_OP_IPSET_ENTRY_ADD:
1648 case DPLANE_OP_IPSET_ENTRY_DELETE:
1649 return FRR_NETLINK_ERROR;
1650
1651 case DPLANE_OP_GRE_SET:
1652 return netlink_put_gre_set_msg(bth, ctx);
1653
1654 case DPLANE_OP_INTF_ADDR_ADD:
1655 case DPLANE_OP_INTF_ADDR_DEL:
1656 case DPLANE_OP_NONE:
1657 return FRR_NETLINK_ERROR;
1658
1659 case DPLANE_OP_INTF_NETCONFIG:
1660 return netlink_put_intf_netconfig(bth, ctx);
1661
1662 case DPLANE_OP_INTF_INSTALL:
1663 case DPLANE_OP_INTF_UPDATE:
1664 case DPLANE_OP_INTF_DELETE:
1665 return netlink_put_intf_update_msg(bth, ctx);
1666
1667 case DPLANE_OP_TC_QDISC_INSTALL:
1668 case DPLANE_OP_TC_QDISC_UNINSTALL:
1669 return netlink_put_tc_qdisc_update_msg(bth, ctx);
1670 case DPLANE_OP_TC_CLASS_ADD:
1671 case DPLANE_OP_TC_CLASS_DELETE:
1672 case DPLANE_OP_TC_CLASS_UPDATE:
1673 return netlink_put_tc_class_update_msg(bth, ctx);
1674 case DPLANE_OP_TC_FILTER_ADD:
1675 case DPLANE_OP_TC_FILTER_DELETE:
1676 case DPLANE_OP_TC_FILTER_UPDATE:
1677 return netlink_put_tc_filter_update_msg(bth, ctx);
1678 }
1679
1680 return FRR_NETLINK_ERROR;
1681 }
1682
1683 void kernel_update_multi(struct dplane_ctx_list_head *ctx_list)
1684 {
1685 struct nl_batch batch;
1686 struct zebra_dplane_ctx *ctx;
1687 struct dplane_ctx_list_head handled_list;
1688 enum netlink_msg_status res;
1689
1690 dplane_ctx_q_init(&handled_list);
1691 nl_batch_init(&batch, &handled_list);
1692
1693 while (true) {
1694 ctx = dplane_ctx_dequeue(ctx_list);
1695 if (ctx == NULL)
1696 break;
1697
1698 if (batch.zns != NULL
1699 && batch.zns->ns_id != dplane_ctx_get_ns(ctx)->ns_id)
1700 nl_batch_send(&batch);
1701
1702 /*
1703 * Assume all messages will succeed and then mark only the ones
1704 * that failed.
1705 */
1706 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
1707
1708 res = nl_put_msg(&batch, ctx);
1709
1710 dplane_ctx_enqueue_tail(&(batch.ctx_list), ctx);
1711 if (res == FRR_NETLINK_ERROR)
1712 dplane_ctx_set_status(ctx,
1713 ZEBRA_DPLANE_REQUEST_FAILURE);
1714
1715 if (batch.curlen > batch.limit)
1716 nl_batch_send(&batch);
1717 }
1718
1719 nl_batch_send(&batch);
1720
1721 dplane_ctx_q_init(ctx_list);
1722 dplane_ctx_list_append(ctx_list, &handled_list);
1723 }
1724
1725 struct nlsock *kernel_netlink_nlsock_lookup(int sock)
1726 {
1727 struct nlsock lookup, *retval;
1728
1729 lookup.sock = sock;
1730
1731 NLSOCK_LOCK();
1732 retval = hash_lookup(nlsock_hash, &lookup);
1733 NLSOCK_UNLOCK();
1734
1735 return retval;
1736 }
1737
1738 /* Insert nlsock entry into hash */
1739 static void kernel_netlink_nlsock_insert(struct nlsock *nls)
1740 {
1741 NLSOCK_LOCK();
1742 (void)hash_get(nlsock_hash, nls, hash_alloc_intern);
1743 NLSOCK_UNLOCK();
1744 }
1745
1746 /* Remove nlsock entry from hash */
1747 static void kernel_netlink_nlsock_remove(struct nlsock *nls)
1748 {
1749 NLSOCK_LOCK();
1750 (void)hash_release(nlsock_hash, nls);
1751 NLSOCK_UNLOCK();
1752 }
1753
1754 static uint32_t kernel_netlink_nlsock_key(const void *arg)
1755 {
1756 const struct nlsock *nl = arg;
1757
1758 return nl->sock;
1759 }
1760
1761 static bool kernel_netlink_nlsock_hash_equal(const void *arg1, const void *arg2)
1762 {
1763 const struct nlsock *nl1 = arg1;
1764 const struct nlsock *nl2 = arg2;
1765
1766 if (nl1->sock == nl2->sock)
1767 return true;
1768
1769 return false;
1770 }
1771
1772 /* Exported interface function. This function simply calls
1773 netlink_socket (). */
1774 void kernel_init(struct zebra_ns *zns)
1775 {
1776 uint32_t groups, dplane_groups, ext_groups;
1777 #if defined SOL_NETLINK
1778 int one, ret, grp;
1779 #endif
1780
1781 /*
1782 * Initialize netlink sockets
1783 *
1784 * If RTMGRP_XXX exists use that, but at some point
1785 * I think the kernel developers realized that
1786 * keeping track of all the different values would
1787 * lead to confusion, so we need to convert the
1788 * RTNLGRP_XXX to a bit position for ourself
1789 *
1790 *
1791 * NOTE: If the bit is >= 32, you must use setsockopt(). Those
1792 * groups are added further below after SOL_NETLINK is verified to
1793 * exist.
1794 */
1795 groups = RTMGRP_LINK |
1796 RTMGRP_IPV4_ROUTE |
1797 RTMGRP_IPV4_IFADDR |
1798 RTMGRP_IPV6_ROUTE |
1799 RTMGRP_IPV6_IFADDR |
1800 RTMGRP_IPV4_MROUTE |
1801 RTMGRP_NEIGH |
1802 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE - 1)) |
1803 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE - 1)) |
1804 ((uint32_t) 1 << (RTNLGRP_NEXTHOP - 1)) |
1805 ((uint32_t) 1 << (RTNLGRP_TC - 1));
1806
1807 dplane_groups = (RTMGRP_LINK |
1808 RTMGRP_IPV4_IFADDR |
1809 RTMGRP_IPV6_IFADDR |
1810 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF - 1)) |
1811 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF - 1)) |
1812 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF - 1)));
1813
1814 /* Use setsockopt for > 31 group */
1815 ext_groups = RTNLGRP_TUNNEL;
1816
1817 snprintf(zns->netlink.name, sizeof(zns->netlink.name),
1818 "netlink-listen (NS %u)", zns->ns_id);
1819 zns->netlink.sock = -1;
1820 if (netlink_socket(&zns->netlink, groups, &ext_groups, 1, zns->ns_id) <
1821 0) {
1822 zlog_err("Failure to create %s socket",
1823 zns->netlink.name);
1824 exit(-1);
1825 }
1826
1827 kernel_netlink_nlsock_insert(&zns->netlink);
1828
1829 snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name),
1830 "netlink-cmd (NS %u)", zns->ns_id);
1831 zns->netlink_cmd.sock = -1;
1832 if (netlink_socket(&zns->netlink_cmd, 0, 0, 0, zns->ns_id) < 0) {
1833 zlog_err("Failure to create %s socket",
1834 zns->netlink_cmd.name);
1835 exit(-1);
1836 }
1837
1838 kernel_netlink_nlsock_insert(&zns->netlink_cmd);
1839
1840 /* Outbound socket for dplane programming of the host OS. */
1841 snprintf(zns->netlink_dplane_out.name,
1842 sizeof(zns->netlink_dplane_out.name), "netlink-dp (NS %u)",
1843 zns->ns_id);
1844 zns->netlink_dplane_out.sock = -1;
1845 if (netlink_socket(&zns->netlink_dplane_out, 0, 0, 0, zns->ns_id) < 0) {
1846 zlog_err("Failure to create %s socket",
1847 zns->netlink_dplane_out.name);
1848 exit(-1);
1849 }
1850
1851 kernel_netlink_nlsock_insert(&zns->netlink_dplane_out);
1852
1853 /* Inbound socket for OS events coming to the dplane. */
1854 snprintf(zns->netlink_dplane_in.name,
1855 sizeof(zns->netlink_dplane_in.name), "netlink-dp-in (NS %u)",
1856 zns->ns_id);
1857 zns->netlink_dplane_in.sock = -1;
1858 if (netlink_socket(&zns->netlink_dplane_in, dplane_groups, 0, 0,
1859 zns->ns_id) < 0) {
1860 zlog_err("Failure to create %s socket",
1861 zns->netlink_dplane_in.name);
1862 exit(-1);
1863 }
1864
1865 kernel_netlink_nlsock_insert(&zns->netlink_dplane_in);
1866
1867 /*
1868 * SOL_NETLINK is not available on all platforms yet
1869 * apparently. It's in bits/socket.h which I am not
1870 * sure that we want to pull into our build system.
1871 */
1872 #if defined SOL_NETLINK
1873
1874 /*
1875 * setsockopt multicast group subscriptions that don't fit in nl_groups
1876 */
1877 grp = RTNLGRP_BRVLAN;
1878 ret = setsockopt(zns->netlink.sock, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
1879 &grp, sizeof(grp));
1880
1881 if (ret < 0)
1882 zlog_notice(
1883 "Registration for RTNLGRP_BRVLAN Membership failed : %d %s",
1884 errno, safe_strerror(errno));
1885 /*
1886 * Let's tell the kernel that we want to receive extended
1887 * ACKS over our command socket(s)
1888 */
1889 one = 1;
1890 ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK,
1891 &one, sizeof(one));
1892
1893 if (ret < 0)
1894 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1895 errno, safe_strerror(errno));
1896
1897 one = 1;
1898 ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK,
1899 NETLINK_EXT_ACK, &one, sizeof(one));
1900
1901 if (ret < 0)
1902 zlog_notice("Registration for extended dp ACK failed : %d %s",
1903 errno, safe_strerror(errno));
1904
1905 /*
1906 * Trim off the payload of the original netlink message in the
1907 * acknowledgment. This option is available since Linux 4.2, so if
1908 * setsockopt fails, ignore the error.
1909 */
1910 one = 1;
1911 ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK,
1912 NETLINK_CAP_ACK, &one, sizeof(one));
1913 if (ret < 0)
1914 zlog_notice(
1915 "Registration for reduced ACK packet size failed, probably running an early kernel");
1916 #endif
1917
1918 /* Register kernel socket. */
1919 if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
1920 flog_err_sys(EC_LIB_SOCKET, "Can't set %s socket flags: %s",
1921 zns->netlink.name, safe_strerror(errno));
1922
1923 if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0)
1924 zlog_err("Can't set %s socket error: %s(%d)",
1925 zns->netlink_cmd.name, safe_strerror(errno), errno);
1926
1927 if (fcntl(zns->netlink_dplane_out.sock, F_SETFL, O_NONBLOCK) < 0)
1928 zlog_err("Can't set %s socket error: %s(%d)",
1929 zns->netlink_dplane_out.name, safe_strerror(errno),
1930 errno);
1931
1932 if (fcntl(zns->netlink_dplane_in.sock, F_SETFL, O_NONBLOCK) < 0)
1933 zlog_err("Can't set %s socket error: %s(%d)",
1934 zns->netlink_dplane_in.name, safe_strerror(errno),
1935 errno);
1936
1937 /* Set receive buffer size if it's set from command line */
1938 if (rcvbufsize) {
1939 netlink_recvbuf(&zns->netlink, rcvbufsize);
1940 netlink_recvbuf(&zns->netlink_cmd, rcvbufsize);
1941 netlink_recvbuf(&zns->netlink_dplane_out, rcvbufsize);
1942 netlink_recvbuf(&zns->netlink_dplane_in, rcvbufsize);
1943 }
1944
1945 /* Set filter for inbound sockets, to exclude events we've generated
1946 * ourselves.
1947 */
1948 netlink_install_filter(zns->netlink.sock, zns->netlink_cmd.snl.nl_pid,
1949 zns->netlink_dplane_out.snl.nl_pid);
1950
1951 netlink_install_filter(zns->netlink_dplane_in.sock,
1952 zns->netlink_cmd.snl.nl_pid,
1953 zns->netlink_dplane_out.snl.nl_pid);
1954
1955 zns->t_netlink = NULL;
1956
1957 thread_add_read(zrouter.master, kernel_read, zns,
1958 zns->netlink.sock, &zns->t_netlink);
1959
1960 rt_netlink_init();
1961 }
1962
1963 /* Helper to clean up an nlsock */
1964 static void kernel_nlsock_fini(struct nlsock *nls)
1965 {
1966 if (nls && nls->sock >= 0) {
1967 kernel_netlink_nlsock_remove(nls);
1968 close(nls->sock);
1969 nls->sock = -1;
1970 XFREE(MTYPE_NL_BUF, nls->buf);
1971 nls->buflen = 0;
1972 }
1973 }
1974
1975 void kernel_terminate(struct zebra_ns *zns, bool complete)
1976 {
1977 THREAD_OFF(zns->t_netlink);
1978
1979 kernel_nlsock_fini(&zns->netlink);
1980
1981 kernel_nlsock_fini(&zns->netlink_cmd);
1982
1983 kernel_nlsock_fini(&zns->netlink_dplane_in);
1984
1985 /* During zebra shutdown, we need to leave the dataplane socket
1986 * around until all work is done.
1987 */
1988 if (complete)
1989 kernel_nlsock_fini(&zns->netlink_dplane_out);
1990 }
1991
1992 /*
1993 * Global init for platform-/OS-specific things
1994 */
1995 void kernel_router_init(void)
1996 {
1997 /* Init nlsock hash and lock */
1998 pthread_mutex_init(&nlsock_mutex, NULL);
1999 nlsock_hash = hash_create_size(8, kernel_netlink_nlsock_key,
2000 kernel_netlink_nlsock_hash_equal,
2001 "Netlink Socket Hash");
2002 }
2003
2004 /*
2005 * Global deinit for platform-/OS-specific things
2006 */
2007 void kernel_router_terminate(void)
2008 {
2009 pthread_mutex_destroy(&nlsock_mutex);
2010
2011 hash_free(nlsock_hash);
2012 nlsock_hash = NULL;
2013 }
2014
2015 #endif /* HAVE_NETLINK */