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