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