1 /* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
29 #include "connected.h"
38 #include "lib_errors.h"
41 #include "zebra/zebra_router.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
45 #include "zebra/debug.h"
46 #include "zebra/kernel_netlink.h"
47 #include "zebra/rt_netlink.h"
48 #include "zebra/if_netlink.h"
49 #include "zebra/rule_netlink.h"
50 #include "zebra/tc_netlink.h"
51 #include "zebra/netconf_netlink.h"
52 #include "zebra/zebra_errors.h"
54 #ifndef SO_RCVBUFFORCE
55 #define SO_RCVBUFFORCE (33)
58 /* Hack for GNU libc version 2. */
60 #define MSG_TRUNC 0x20
61 #endif /* MSG_TRUNC */
64 #define NLMSG_TAIL(nmsg) \
65 ((struct rtattr *)(((uint8_t *)(nmsg)) \
66 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
70 #define RTA_TAIL(rta) \
71 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
74 #ifndef RTNL_FAMILY_IP6MR
75 #define RTNL_FAMILY_IP6MR 129
78 #ifndef RTPROT_MROUTED
79 #define RTPROT_MROUTED 17
82 #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
85 * We limit the batch's size to a number smaller than the length of the
86 * underlying buffer since the last message that wouldn't fit the batch would go
87 * over the upper boundary and then it would have to be encoded again into a new
88 * buffer. If the difference between the limit and the length of the buffer is
89 * big enough (bigger than the biggest Netlink message) then this situation
92 #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
94 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
95 {RTM_DELROUTE
, "RTM_DELROUTE"},
96 {RTM_GETROUTE
, "RTM_GETROUTE"},
97 {RTM_NEWLINK
, "RTM_NEWLINK"},
98 {RTM_SETLINK
, "RTM_SETLINK"},
99 {RTM_DELLINK
, "RTM_DELLINK"},
100 {RTM_GETLINK
, "RTM_GETLINK"},
101 {RTM_NEWADDR
, "RTM_NEWADDR"},
102 {RTM_DELADDR
, "RTM_DELADDR"},
103 {RTM_GETADDR
, "RTM_GETADDR"},
104 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
105 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
106 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
107 {RTM_NEWRULE
, "RTM_NEWRULE"},
108 {RTM_DELRULE
, "RTM_DELRULE"},
109 {RTM_GETRULE
, "RTM_GETRULE"},
110 {RTM_NEWNEXTHOP
, "RTM_NEWNEXTHOP"},
111 {RTM_DELNEXTHOP
, "RTM_DELNEXTHOP"},
112 {RTM_GETNEXTHOP
, "RTM_GETNEXTHOP"},
113 {RTM_NEWNETCONF
, "RTM_NEWNETCONF"},
114 {RTM_DELNETCONF
, "RTM_DELNETCONF"},
115 {RTM_NEWTUNNEL
, "RTM_NEWTUNNEL"},
116 {RTM_DELTUNNEL
, "RTM_DELTUNNEL"},
117 {RTM_GETTUNNEL
, "RTM_GETTUNNEL"},
118 {RTM_NEWQDISC
, "RTM_NEWQDISC"},
119 {RTM_DELQDISC
, "RTM_DELQDISC"},
120 {RTM_GETQDISC
, "RTM_GETQDISC"},
121 {RTM_NEWTCLASS
, "RTM_NEWTCLASS"},
122 {RTM_DELTCLASS
, "RTM_DELTCLASS"},
123 {RTM_GETTCLASS
, "RTM_GETTCLASS"},
124 {RTM_NEWTFILTER
, "RTM_NEWTFILTER"},
125 {RTM_DELTFILTER
, "RTM_DELTFILTER"},
126 {RTM_GETTFILTER
, "RTM_GETTFILTER"},
129 static const struct message rtproto_str
[] = {
130 {RTPROT_REDIRECT
, "redirect"},
131 {RTPROT_KERNEL
, "kernel"},
132 {RTPROT_BOOT
, "boot"},
133 {RTPROT_STATIC
, "static"},
134 {RTPROT_GATED
, "GateD"},
135 {RTPROT_RA
, "router advertisement"},
137 {RTPROT_ZEBRA
, "Zebra"},
139 {RTPROT_BIRD
, "BIRD"},
140 #endif /* RTPROT_BIRD */
141 {RTPROT_MROUTED
, "mroute"},
143 {RTPROT_OSPF
, "OSPF"},
144 {RTPROT_ISIS
, "IS-IS"},
146 {RTPROT_RIPNG
, "RIPNG"},
147 {RTPROT_ZSTATIC
, "static"},
150 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
152 {AF_BRIDGE
, "bridge"},
153 {RTNL_FAMILY_IPMR
, "ipv4MR"},
154 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
157 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
158 {RTN_UNICAST
, "unicast"},
159 {RTN_LOCAL
, "local"},
160 {RTN_BROADCAST
, "broadcast"},
161 {RTN_ANYCAST
, "anycast"},
162 {RTN_MULTICAST
, "multicast"},
163 {RTN_BLACKHOLE
, "blackhole"},
164 {RTN_UNREACHABLE
, "unreachable"},
165 {RTN_PROHIBIT
, "prohibited"},
166 {RTN_THROW
, "throw"},
168 {RTN_XRESOLVE
, "resolver"},
171 extern struct thread_master
*master
;
173 extern struct zebra_privs_t zserv_privs
;
175 DEFINE_MTYPE_STATIC(ZEBRA
, NL_BUF
, "Zebra Netlink buffers");
177 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
178 * We have both the main and dplane pthreads using these structs, so we have
179 * to protect the hash with a lock.
181 static struct hash
*nlsock_hash
;
182 pthread_mutex_t nlsock_mutex
;
184 /* Lock and unlock wrappers for nlsock hash */
185 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
186 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
188 size_t nl_batch_tx_bufsize
;
189 char *nl_batch_tx_buf
;
191 _Atomic
uint32_t nl_batch_bufsize
= NL_DEFAULT_BATCH_BUFSIZE
;
192 _Atomic
uint32_t nl_batch_send_threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
203 const struct zebra_dplane_info
*zns
;
205 struct dplane_ctx_q ctx_list
;
208 * Pointer to the queue of completed contexts outbound back
209 * towards the dataplane module.
211 struct dplane_ctx_q
*ctx_out_q
;
214 int netlink_config_write_helper(struct vty
*vty
)
217 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
218 uint32_t threshold
= atomic_load_explicit(&nl_batch_send_threshold
,
219 memory_order_relaxed
);
221 if (size
!= NL_DEFAULT_BATCH_BUFSIZE
222 || threshold
!= NL_DEFAULT_BATCH_SEND_THRESHOLD
)
223 vty_out(vty
, "zebra kernel netlink batch-tx-buf %u %u\n", size
,
226 if (if_netlink_frr_protodown_r_bit_is_set())
227 vty_out(vty
, "zebra protodown reason-bit %u\n",
228 if_netlink_get_frr_protodown_r_bit());
233 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
236 size
= NL_DEFAULT_BATCH_BUFSIZE
;
237 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
240 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
241 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
242 memory_order_relaxed
);
245 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
248 * This is an error condition that must be handled during
251 * The netlink_talk_filter function is used for communication
252 * down the netlink_cmd pipe and we are expecting
253 * an ack being received. So if we get here
254 * then we did not receive the ack and instead
255 * received some other message in an unexpected
258 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
259 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
263 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
266 socklen_t newlen
= sizeof(newsize
);
267 socklen_t oldlen
= sizeof(oldsize
);
270 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
272 flog_err_sys(EC_LIB_SOCKET
,
273 "Can't get %s receive buffer size: %s", nl
->name
,
274 safe_strerror(errno
));
278 /* Try force option (linux >= 2.6.14) and fall back to normal set */
279 frr_with_privs(&zserv_privs
) {
280 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
281 &rcvbufsize
, sizeof(rcvbufsize
));
284 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
287 flog_err_sys(EC_LIB_SOCKET
,
288 "Can't set %s receive buffer size: %s", nl
->name
,
289 safe_strerror(errno
));
293 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
295 flog_err_sys(EC_LIB_SOCKET
,
296 "Can't get %s receive buffer size: %s", nl
->name
,
297 safe_strerror(errno
));
303 static const char *group2str(uint32_t group
)
307 return "RTNLGRP_TUNNEL";
313 /* Make socket for Linux netlink interface. */
314 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
315 uint32_t ext_groups
[], uint8_t ext_group_size
,
319 struct sockaddr_nl snl
;
323 frr_with_privs(&zserv_privs
) {
324 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
326 zlog_err("Can't open %s socket: %s", nl
->name
,
327 safe_strerror(errno
));
331 memset(&snl
, 0, sizeof(snl
));
332 snl
.nl_family
= AF_NETLINK
;
333 snl
.nl_groups
= groups
;
335 if (ext_group_size
) {
338 for (i
= 0; i
< ext_group_size
; i
++) {
339 #if defined SOL_NETLINK
340 ret
= setsockopt(sock
, SOL_NETLINK
,
341 NETLINK_ADD_MEMBERSHIP
,
343 sizeof(ext_groups
[i
]));
346 "can't setsockopt NETLINK_ADD_MEMBERSHIP for group %s(%u), this linux kernel does not support it: %s(%d)",
347 group2str(ext_groups
[i
]),
349 safe_strerror(errno
), errno
);
353 "Unable to use NETLINK_ADD_MEMBERSHIP via SOL_NETLINK for %s(%u) since the linux kernel does not support the socket option",
354 group2str(ext_groups
[i
]),
360 /* Bind the socket to the netlink structure for anything. */
361 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
365 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
366 snl
.nl_groups
, safe_strerror(errno
));
371 /* multiple netlink sockets will have different nl_pid */
372 namelen
= sizeof(snl
);
373 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
374 if (ret
< 0 || namelen
!= sizeof(snl
)) {
375 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
376 nl
->name
, safe_strerror(errno
));
383 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
384 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
390 * Dispatch an incoming netlink message; used by the zebra main pthread's
391 * netlink event reader.
393 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
397 * When we handle new message types here
398 * because we are starting to install them
399 * then lets check the netlink_install_filter
400 * and see if we should add the corresponding
401 * allow through entry there.
402 * Probably not needed to do but please
405 switch (h
->nlmsg_type
) {
407 return netlink_route_change(h
, ns_id
, startup
);
409 return netlink_route_change(h
, ns_id
, startup
);
411 return netlink_link_change(h
, ns_id
, startup
);
413 return netlink_link_change(h
, ns_id
, startup
);
417 return netlink_neigh_change(h
, ns_id
);
419 return netlink_rule_change(h
, ns_id
, startup
);
421 return netlink_rule_change(h
, ns_id
, startup
);
423 return netlink_nexthop_change(h
, ns_id
, startup
);
425 return netlink_nexthop_change(h
, ns_id
, startup
);
428 return netlink_qdisc_change(h
, ns_id
, startup
);
431 return netlink_tclass_change(h
, ns_id
, startup
);
434 return netlink_tfilter_change(h
, ns_id
, startup
);
436 /* Messages handled in the dplane thread */
447 * If we have received this message then
448 * we have made a mistake during development
449 * and we need to write some code to handle
450 * this message type or not ask for
451 * it to be sent up to us
453 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
454 "Unknown netlink nlmsg_type %s(%d) vrf %u",
455 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
463 * Dispatch an incoming netlink message; used by the dataplane pthread's
464 * netlink event reader code.
466 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
470 * Dispatch the incoming messages that the dplane pthread handles
472 switch (h
->nlmsg_type
) {
475 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
479 return netlink_netconf_change(h
, ns_id
, startup
);
481 /* TODO -- other messages for the dplane socket and pthread */
493 static void kernel_read(struct thread
*thread
)
495 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
496 struct zebra_dplane_info dp_info
;
498 /* Capture key info from ns struct */
499 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
501 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
504 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
509 * Called by the dplane pthread to read incoming OS messages and dispatch them.
511 int kernel_dplane_read(struct zebra_dplane_info
*info
)
513 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
515 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
522 * Filter out messages from self that occur on listener socket,
523 * caused by our actions on the command socket(s)
525 * When we add new Netlink message types we probably
526 * do not need to add them here as that we are filtering
527 * on the routes we actually care to receive( which is rarer
528 * then the normal course of operations). We are intentionally
529 * allowing some messages from ourselves through
530 * ( I'm looking at you Interface based netlink messages )
531 * so that we only have to write one way to handle incoming
532 * address add/delete and xxxNETCONF changes.
534 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
537 * BPF_JUMP instructions and where you jump to are based upon
538 * 0 as being the next statement. So count from 0. Writing
539 * this down because every time I look at this I have to
542 struct sock_filter filter
[] = {
545 * if (nlmsg_pid == pid ||
546 * nlmsg_pid == dplane_pid) {
547 * if (the incoming nlmsg_type ==
548 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
554 * keep this netlink message
557 * 0: Load the nlmsg_pid into the BPF register
559 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
560 offsetof(struct nlmsghdr
, nlmsg_pid
)),
564 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
566 * 2: Compare to dplane pid
568 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
570 * 3: Load the nlmsg_type into BPF register
572 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
573 offsetof(struct nlmsghdr
, nlmsg_type
)),
575 * 4: Compare to RTM_NEWADDR
577 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
579 * 5: Compare to RTM_DELADDR
581 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
583 * 6: Compare to RTM_NEWNETCONF
585 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
588 * 7: Compare to RTM_DELNETCONF
590 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
593 * 8: This is the end state of we want to skip the
596 BPF_STMT(BPF_RET
| BPF_K
, 0),
597 /* 9: This is the end state of we want to keep
600 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
603 struct sock_fprog prog
= {
604 .len
= array_size(filter
), .filter
= filter
,
607 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
609 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
610 safe_strerror(errno
));
613 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
614 int len
, unsigned short flags
)
618 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
619 while (RTA_OK(rta
, len
)) {
620 type
= rta
->rta_type
& ~flags
;
621 if ((type
<= max
) && (!tb
[type
]))
623 rta
= RTA_NEXT(rta
, len
);
627 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
630 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
631 while (RTA_OK(rta
, len
)) {
632 if (rta
->rta_type
<= max
)
633 tb
[rta
->rta_type
] = rta
;
634 rta
= RTA_NEXT(rta
, len
);
639 * netlink_parse_rtattr_nested() - Parses a nested route attribute
640 * @tb: Pointer to array for storing rtattr in.
641 * @max: Max number to store.
642 * @rta: Pointer to rtattr to look for nested items in.
644 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
647 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
650 bool nl_addraw_l(struct nlmsghdr
*n
, unsigned int maxlen
, const void *data
,
653 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
654 zlog_err("ERROR message exceeded bound of %d", maxlen
);
658 memcpy(NLMSG_TAIL(n
), data
, len
);
659 memset((uint8_t *)NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
660 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
665 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
666 const void *data
, unsigned int alen
)
671 len
= RTA_LENGTH(alen
);
673 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
676 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
677 rta
->rta_type
= type
;
681 memcpy(RTA_DATA(rta
), data
, alen
);
685 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
690 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
693 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
696 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
699 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
702 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
705 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
708 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
710 struct rtattr
*nest
= NLMSG_TAIL(n
);
712 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
715 nest
->rta_type
|= NLA_F_NESTED
;
719 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
721 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
725 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
727 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
729 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
733 memset(rtnh
, 0, sizeof(struct rtnexthop
));
735 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
740 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
742 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
745 bool nl_rta_put(struct rtattr
*rta
, unsigned int maxlen
, int type
,
746 const void *data
, int alen
)
748 struct rtattr
*subrta
;
749 int len
= RTA_LENGTH(alen
);
751 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
752 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
756 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
757 subrta
->rta_type
= type
;
758 subrta
->rta_len
= len
;
760 memcpy(RTA_DATA(subrta
), data
, alen
);
761 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
766 bool nl_rta_put16(struct rtattr
*rta
, unsigned int maxlen
, int type
,
769 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint16_t));
772 bool nl_rta_put64(struct rtattr
*rta
, unsigned int maxlen
, int type
,
775 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint64_t));
778 struct rtattr
*nl_rta_nest(struct rtattr
*rta
, unsigned int maxlen
, int type
)
780 struct rtattr
*nest
= RTA_TAIL(rta
);
782 if (nl_rta_put(rta
, maxlen
, type
, NULL
, 0))
785 nest
->rta_type
|= NLA_F_NESTED
;
790 int nl_rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
792 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
797 const char *nl_msg_type_to_str(uint16_t msg_type
)
799 return lookup_msg(nlmsg_str
, msg_type
, "");
802 const char *nl_rtproto_to_str(uint8_t rtproto
)
804 return lookup_msg(rtproto_str
, rtproto
, "");
807 const char *nl_family_to_str(uint8_t family
)
809 return lookup_msg(family_str
, family
, "");
812 const char *nl_rttype_to_str(uint8_t rttype
)
814 return lookup_msg(rttype_str
, rttype
, "");
817 #define NLA_OK(nla, len) \
818 ((len) >= (int)sizeof(struct nlattr) \
819 && (nla)->nla_len >= sizeof(struct nlattr) \
820 && (nla)->nla_len <= (len))
821 #define NLA_NEXT(nla, attrlen) \
822 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
823 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
824 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
825 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
827 #define ERR_NLA(err, inner_len) \
828 ((struct nlattr *)(((char *)(err)) \
829 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
830 + NLMSG_ALIGN((inner_len))))
832 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
833 struct nlattr
*nla
, int len
)
835 while (NLA_OK(nla
, len
)) {
836 if (nla
->nla_type
<= max
)
837 tb
[nla
->nla_type
] = nla
;
838 nla
= NLA_NEXT(nla
, len
);
842 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
844 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
845 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
846 const struct nlmsghdr
*err_nlh
= NULL
;
847 /* Length not including nlmsghdr */
849 /* Inner error netlink message length */
850 uint32_t inner_len
= 0;
851 const char *msg
= NULL
;
854 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
855 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
857 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
859 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
862 if (tb
[NLMSGERR_ATTR_MSG
])
863 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
865 if (tb
[NLMSGERR_ATTR_OFFS
]) {
866 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
868 if (off
> h
->nlmsg_len
) {
869 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
870 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
872 * Header of failed message
873 * we are not doing anything currently with it
874 * but noticing it for later.
877 zlog_debug("%s: Received %s extended Ack", __func__
,
878 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
882 if (msg
&& *msg
!= '\0') {
883 bool is_err
= !!err
->error
;
886 zlog_err("Extended Error: %s", msg
);
888 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
889 "Extended Warning: %s", msg
);
894 * netlink_send_msg - send a netlink message of a certain size.
896 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
898 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
901 struct sockaddr_nl snl
= {};
902 struct iovec iov
= {};
903 struct msghdr msg
= {};
908 iov
.iov_len
= buflen
;
910 msg
.msg_namelen
= sizeof(snl
);
914 snl
.nl_family
= AF_NETLINK
;
916 /* Send message to netlink interface. */
917 frr_with_privs(&zserv_privs
) {
918 status
= sendmsg(nl
->sock
, &msg
, 0);
922 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
923 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
925 nl_dump(buf
, buflen
);
927 zlog_hexdump(buf
, buflen
);
928 #endif /* NETLINK_DEBUG */
932 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
933 safe_strerror(save_errno
));
941 * netlink_recv_msg - receive a netlink message.
943 * Returns -1 on error, 0 if read would block or the number of bytes received.
945 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
950 iov
.iov_base
= nl
->buf
;
951 iov
.iov_len
= nl
->buflen
;
958 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
960 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
961 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
963 iov
.iov_base
= nl
->buf
;
964 iov
.iov_len
= nl
->buflen
;
967 status
= recvmsg(nl
->sock
, msg
, 0);
968 } while (status
== -1 && errno
== EINTR
);
971 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
973 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
974 nl
->name
, safe_strerror(errno
));
976 * In this case we are screwed. There is no good way to recover
977 * zebra at this point.
983 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
987 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
988 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
989 "%s sender address length error: length %d", nl
->name
,
994 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
995 zlog_debug("%s: << netlink message dump [recv]", __func__
);
997 nl_dump(nl
->buf
, status
);
999 zlog_hexdump(nl
->buf
, status
);
1000 #endif /* NETLINK_DEBUG */
1007 * netlink_parse_error - parse a netlink error message
1009 * Returns 1 if this message is acknowledgement, 0 if this error should be
1010 * ignored, -1 otherwise.
1012 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
1013 bool is_cmd
, bool startup
)
1015 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
1016 int errnum
= err
->error
;
1017 int msg_type
= err
->msg
.nlmsg_type
;
1019 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
1020 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1021 "%s error: message truncated", nl
->name
);
1026 * Parse the extended information before we actually handle it. At this
1027 * point in time we do not do anything other than report the issue.
1029 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
1030 netlink_parse_extended_ack(h
);
1032 /* If the error field is zero, then this is an ACK. */
1033 if (err
->error
== 0) {
1034 if (IS_ZEBRA_DEBUG_KERNEL
) {
1035 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
1037 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
1038 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
1039 err
->msg
.nlmsg_pid
);
1046 * Deal with errors that occur because of races in link handling
1047 * or types are not supported in kernel.
1050 ((msg_type
== RTM_DELROUTE
&&
1051 (-errnum
== ENODEV
|| -errnum
== ESRCH
)) ||
1052 (msg_type
== RTM_NEWROUTE
&&
1053 (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)) ||
1054 ((msg_type
== RTM_NEWTUNNEL
|| msg_type
== RTM_DELTUNNEL
||
1055 msg_type
== RTM_GETTUNNEL
) &&
1056 (-errnum
== EOPNOTSUPP
)))) {
1057 if (IS_ZEBRA_DEBUG_KERNEL
)
1058 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1059 nl
->name
, safe_strerror(-errnum
),
1060 nl_msg_type_to_str(msg_type
), msg_type
,
1061 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1066 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1067 * link-local. The kernel should have already deleted the neighbor so
1068 * do not log these as an error.
1070 if (msg_type
== RTM_DELNEIGH
1071 || (is_cmd
&& msg_type
== RTM_NEWROUTE
1072 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
1074 * This is known to happen in some situations, don't log as
1077 if (IS_ZEBRA_DEBUG_KERNEL
)
1078 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1079 nl
->name
, safe_strerror(-errnum
),
1080 nl_msg_type_to_str(msg_type
), msg_type
,
1081 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1083 if ((msg_type
!= RTM_GETNEXTHOP
) || !startup
)
1084 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
1085 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1086 nl
->name
, safe_strerror(-errnum
),
1087 nl_msg_type_to_str(msg_type
), msg_type
,
1088 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1095 * netlink_parse_info
1097 * Receive message from netlink interface and pass those information
1098 * to the given function.
1100 * filter -> Function to call to read the results
1101 * nl -> netlink socket information
1102 * zns -> The zebra namespace data
1103 * count -> How many we should read in, 0 means as much as possible
1104 * startup -> Are we reading in under startup conditions? passed to
1107 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
1108 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
1109 int count
, bool startup
)
1117 struct sockaddr_nl snl
;
1118 struct msghdr msg
= {.msg_name
= (void *)&snl
,
1119 .msg_namelen
= sizeof(snl
)};
1122 if (count
&& read_in
>= count
)
1125 status
= netlink_recv_msg(nl
, &msg
);
1128 else if (status
== 0)
1132 for (h
= (struct nlmsghdr
*)nl
->buf
;
1133 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
1134 h
= NLMSG_NEXT(h
, status
)) {
1135 /* Finish of reading. */
1136 if (h
->nlmsg_type
== NLMSG_DONE
)
1139 /* Error handling. */
1140 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1141 int err
= netlink_parse_error(
1142 nl
, h
, zns
->is_cmd
, startup
);
1145 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1153 * What is the right thing to do? The kernel
1154 * is telling us that the dump request was interrupted
1155 * and we more than likely are out of luck and have
1156 * missed data from the kernel. At this point in time
1157 * lets just note that this is happening.
1159 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
1161 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
1162 "netlink recvmsg: The Dump request was interrupted");
1164 /* OK we got netlink message. */
1165 if (IS_ZEBRA_DEBUG_KERNEL
)
1167 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1169 nl_msg_type_to_str(h
->nlmsg_type
),
1170 h
->nlmsg_type
, h
->nlmsg_len
,
1171 h
->nlmsg_seq
, h
->nlmsg_pid
);
1175 * Ignore messages that maybe sent from
1176 * other actors besides the kernel
1178 if (snl
.nl_pid
!= 0) {
1179 zlog_debug("Ignoring message from pid %u",
1184 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1186 zlog_debug("%s filter function error",
1192 /* After error care. */
1193 if (msg
.msg_flags
& MSG_TRUNC
) {
1194 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1195 "%s error: message truncated", nl
->name
);
1199 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1200 "%s error: data remnant size %d", nl
->name
,
1211 * sendmsg() to netlink socket then recvmsg().
1212 * Calls netlink_parse_info to parse returned data
1214 * filter -> The filter to read final results from kernel
1215 * nlmsghdr -> The data to send to the kernel
1216 * dp_info -> The dataplane and netlink socket information
1217 * startup -> Are we reading in under startup conditions
1218 * This is passed through eventually to filter.
1220 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1223 struct zebra_dplane_info
*dp_info
, bool startup
)
1227 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1228 n
->nlmsg_seq
= dp_info
->seq
;
1229 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1231 if (IS_ZEBRA_DEBUG_KERNEL
)
1233 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1234 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1235 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1238 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1242 * Get reply from netlink socket.
1243 * The reply should either be an acknowlegement or an error.
1245 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1249 * Synchronous version of netlink_talk_info. Converts args to suit the
1250 * common version, which is suitable for both sync and async use.
1252 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1253 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1256 struct zebra_dplane_info dp_info
;
1258 /* Increment sequence number before capturing snapshot of ns socket
1263 /* Capture info in intermediate info struct */
1264 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1266 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1269 /* Issue request message to kernel via netlink socket. GET messages
1270 * are issued through this interface.
1272 int netlink_request(struct nlsock
*nl
, void *req
)
1274 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1276 /* Check netlink socket. */
1278 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1283 /* Fill common fields for all requests. */
1284 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1285 n
->nlmsg_seq
= ++nl
->seq
;
1287 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1293 static int nl_batch_read_resp(struct nl_batch
*bth
)
1296 struct sockaddr_nl snl
;
1297 struct msghdr msg
= {};
1300 struct zebra_dplane_ctx
*ctx
;
1303 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1305 msg
.msg_name
= (void *)&snl
;
1306 msg
.msg_namelen
= sizeof(snl
);
1309 * The responses are not batched, so we need to read and process one
1310 * message at a time.
1313 status
= netlink_recv_msg(nl
, &msg
);
1315 * status == -1 is a full on failure somewhere
1316 * since we don't know where the problem happened
1317 * we must mark all as failed
1319 * Else we mark everything as worked
1322 if (status
== -1 || status
== 0) {
1323 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1326 dplane_ctx_set_status(
1328 ZEBRA_DPLANE_REQUEST_FAILURE
);
1329 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1334 h
= (struct nlmsghdr
*)nl
->buf
;
1338 * Find the corresponding context object. Received responses are
1339 * in the same order as requests we sent, so we can simply
1340 * iterate over the context list and match responses with
1341 * requests at same time.
1344 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1347 * This is a situation where we have gotten
1348 * into a bad spot. We need to know that
1349 * this happens( does it? )
1352 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1358 * 'update' context objects take two consecutive
1361 if (dplane_ctx_is_update(ctx
) &&
1362 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1364 * This is the situation where we get a response
1365 * to a message that should be ignored.
1371 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1372 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1374 /* We have found corresponding context object. */
1375 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1378 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1380 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1382 dplane_ctx_get_ns(ctx
)->seq
);
1387 * If we ignore the message due to an update
1388 * above we should still fricking decode the
1389 * message for our operator to understand
1392 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1395 zlog_debug("%s: netlink error message seq=%d %d",
1396 __func__
, h
->nlmsg_seq
, err
);
1401 * We received a message with the sequence number that isn't
1402 * associated with any dplane context object.
1405 if (IS_ZEBRA_DEBUG_KERNEL
)
1407 "%s: skipping unassociated response, seq number %d NS %u",
1408 __func__
, h
->nlmsg_seq
,
1413 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1414 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1418 dplane_ctx_set_status(
1419 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1421 if (IS_ZEBRA_DEBUG_KERNEL
)
1422 zlog_debug("%s: netlink error message seq=%d ",
1423 __func__
, h
->nlmsg_seq
);
1428 * If we get here then we did not receive neither the ack nor
1429 * the error and instead received some other message in an
1432 if (IS_ZEBRA_DEBUG_KERNEL
)
1433 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1434 __func__
, h
->nlmsg_type
,
1435 nl_msg_type_to_str(h
->nlmsg_type
),
1442 static void nl_batch_reset(struct nl_batch
*bth
)
1444 bth
->buf_head
= bth
->buf
;
1449 TAILQ_INIT(&(bth
->ctx_list
));
1452 static void nl_batch_init(struct nl_batch
*bth
, struct dplane_ctx_q
*ctx_out_q
)
1455 * If the size of the buffer has changed, free and then allocate a new
1459 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1460 if (bufsize
!= nl_batch_tx_bufsize
) {
1461 if (nl_batch_tx_buf
)
1462 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1464 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1465 nl_batch_tx_bufsize
= bufsize
;
1468 bth
->buf
= nl_batch_tx_buf
;
1469 bth
->bufsiz
= bufsize
;
1470 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1471 memory_order_relaxed
);
1473 bth
->ctx_out_q
= ctx_out_q
;
1475 nl_batch_reset(bth
);
1478 static void nl_batch_send(struct nl_batch
*bth
)
1480 struct zebra_dplane_ctx
*ctx
;
1483 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1485 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1487 if (IS_ZEBRA_DEBUG_KERNEL
)
1488 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1489 __func__
, nl
->name
, bth
->curlen
,
1492 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1496 if (nl_batch_read_resp(bth
) == -1)
1501 /* Move remaining contexts to the outbound queue. */
1503 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1508 dplane_ctx_set_status(ctx
,
1509 ZEBRA_DPLANE_REQUEST_FAILURE
);
1511 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1514 nl_batch_reset(bth
);
1517 enum netlink_msg_status
netlink_batch_add_msg(
1518 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1519 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1524 struct nlmsghdr
*msgh
;
1527 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1530 * If there was an error while encoding the message (other than buffer
1531 * overflow) then return an error.
1534 return FRR_NETLINK_ERROR
;
1537 * If the message doesn't fit entirely in the buffer then send the batch
1542 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1543 bth
->bufsiz
- bth
->curlen
);
1545 * If the message doesn't fit in the empty buffer then just
1549 return FRR_NETLINK_ERROR
;
1552 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1553 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1558 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1559 msgh
->nlmsg_seq
= seq
;
1560 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1562 bth
->zns
= dplane_ctx_get_ns(ctx
);
1563 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1564 bth
->curlen
+= size
;
1567 return FRR_NETLINK_QUEUED
;
1570 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1571 struct zebra_dplane_ctx
*ctx
)
1573 if (dplane_ctx_is_skip_kernel(ctx
))
1574 return FRR_NETLINK_SUCCESS
;
1576 switch (dplane_ctx_get_op(ctx
)) {
1578 case DPLANE_OP_ROUTE_INSTALL
:
1579 case DPLANE_OP_ROUTE_UPDATE
:
1580 case DPLANE_OP_ROUTE_DELETE
:
1581 return netlink_put_route_update_msg(bth
, ctx
);
1583 case DPLANE_OP_NH_INSTALL
:
1584 case DPLANE_OP_NH_UPDATE
:
1585 case DPLANE_OP_NH_DELETE
:
1586 return netlink_put_nexthop_update_msg(bth
, ctx
);
1588 case DPLANE_OP_LSP_INSTALL
:
1589 case DPLANE_OP_LSP_UPDATE
:
1590 case DPLANE_OP_LSP_DELETE
:
1591 return netlink_put_lsp_update_msg(bth
, ctx
);
1593 case DPLANE_OP_PW_INSTALL
:
1594 case DPLANE_OP_PW_UNINSTALL
:
1595 return netlink_put_pw_update_msg(bth
, ctx
);
1597 case DPLANE_OP_ADDR_INSTALL
:
1598 case DPLANE_OP_ADDR_UNINSTALL
:
1599 return netlink_put_address_update_msg(bth
, ctx
);
1601 case DPLANE_OP_MAC_INSTALL
:
1602 case DPLANE_OP_MAC_DELETE
:
1603 return netlink_put_mac_update_msg(bth
, ctx
);
1605 case DPLANE_OP_NEIGH_INSTALL
:
1606 case DPLANE_OP_NEIGH_UPDATE
:
1607 case DPLANE_OP_NEIGH_DELETE
:
1608 case DPLANE_OP_VTEP_ADD
:
1609 case DPLANE_OP_VTEP_DELETE
:
1610 case DPLANE_OP_NEIGH_DISCOVER
:
1611 case DPLANE_OP_NEIGH_IP_INSTALL
:
1612 case DPLANE_OP_NEIGH_IP_DELETE
:
1613 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1614 return netlink_put_neigh_update_msg(bth
, ctx
);
1616 case DPLANE_OP_RULE_ADD
:
1617 case DPLANE_OP_RULE_DELETE
:
1618 case DPLANE_OP_RULE_UPDATE
:
1619 return netlink_put_rule_update_msg(bth
, ctx
);
1621 case DPLANE_OP_SYS_ROUTE_ADD
:
1622 case DPLANE_OP_SYS_ROUTE_DELETE
:
1623 case DPLANE_OP_ROUTE_NOTIFY
:
1624 case DPLANE_OP_LSP_NOTIFY
:
1625 case DPLANE_OP_BR_PORT_UPDATE
:
1626 return FRR_NETLINK_SUCCESS
;
1628 case DPLANE_OP_IPTABLE_ADD
:
1629 case DPLANE_OP_IPTABLE_DELETE
:
1630 case DPLANE_OP_IPSET_ADD
:
1631 case DPLANE_OP_IPSET_DELETE
:
1632 case DPLANE_OP_IPSET_ENTRY_ADD
:
1633 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1634 return FRR_NETLINK_ERROR
;
1636 case DPLANE_OP_GRE_SET
:
1637 return netlink_put_gre_set_msg(bth
, ctx
);
1639 case DPLANE_OP_INTF_ADDR_ADD
:
1640 case DPLANE_OP_INTF_ADDR_DEL
:
1641 case DPLANE_OP_NONE
:
1642 return FRR_NETLINK_ERROR
;
1644 case DPLANE_OP_INTF_NETCONFIG
:
1645 return netlink_put_intf_netconfig(bth
, ctx
);
1647 case DPLANE_OP_INTF_INSTALL
:
1648 case DPLANE_OP_INTF_UPDATE
:
1649 case DPLANE_OP_INTF_DELETE
:
1650 return netlink_put_intf_update_msg(bth
, ctx
);
1652 case DPLANE_OP_TC_QDISC_INSTALL
:
1653 case DPLANE_OP_TC_QDISC_UNINSTALL
:
1654 return netlink_put_tc_qdisc_update_msg(bth
, ctx
);
1655 case DPLANE_OP_TC_CLASS_ADD
:
1656 case DPLANE_OP_TC_CLASS_DELETE
:
1657 case DPLANE_OP_TC_CLASS_UPDATE
:
1658 return netlink_put_tc_class_update_msg(bth
, ctx
);
1659 case DPLANE_OP_TC_FILTER_ADD
:
1660 case DPLANE_OP_TC_FILTER_DELETE
:
1661 case DPLANE_OP_TC_FILTER_UPDATE
:
1662 return netlink_put_tc_filter_update_msg(bth
, ctx
);
1665 return FRR_NETLINK_ERROR
;
1668 void kernel_update_multi(struct dplane_ctx_q
*ctx_list
)
1670 struct nl_batch batch
;
1671 struct zebra_dplane_ctx
*ctx
;
1672 struct dplane_ctx_q handled_list
;
1673 enum netlink_msg_status res
;
1675 TAILQ_INIT(&handled_list
);
1676 nl_batch_init(&batch
, &handled_list
);
1679 ctx
= dplane_ctx_dequeue(ctx_list
);
1683 if (batch
.zns
!= NULL
1684 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1685 nl_batch_send(&batch
);
1688 * Assume all messages will succeed and then mark only the ones
1691 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1693 res
= nl_put_msg(&batch
, ctx
);
1695 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1696 if (res
== FRR_NETLINK_ERROR
)
1697 dplane_ctx_set_status(ctx
,
1698 ZEBRA_DPLANE_REQUEST_FAILURE
);
1700 if (batch
.curlen
> batch
.limit
)
1701 nl_batch_send(&batch
);
1704 nl_batch_send(&batch
);
1706 TAILQ_INIT(ctx_list
);
1707 dplane_ctx_list_append(ctx_list
, &handled_list
);
1710 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1712 struct nlsock lookup
, *retval
;
1717 retval
= hash_lookup(nlsock_hash
, &lookup
);
1723 /* Insert nlsock entry into hash */
1724 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1727 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1731 /* Remove nlsock entry from hash */
1732 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1735 (void)hash_release(nlsock_hash
, nls
);
1739 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1741 const struct nlsock
*nl
= arg
;
1746 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1748 const struct nlsock
*nl1
= arg1
;
1749 const struct nlsock
*nl2
= arg2
;
1751 if (nl1
->sock
== nl2
->sock
)
1757 /* Exported interface function. This function simply calls
1758 netlink_socket (). */
1759 void kernel_init(struct zebra_ns
*zns
)
1761 uint32_t groups
, dplane_groups
, ext_groups
;
1762 #if defined SOL_NETLINK
1767 * Initialize netlink sockets
1769 * If RTMGRP_XXX exists use that, but at some point
1770 * I think the kernel developers realized that
1771 * keeping track of all the different values would
1772 * lead to confusion, so we need to convert the
1773 * RTNLGRP_XXX to a bit position for ourself
1775 groups
= RTMGRP_LINK
|
1777 RTMGRP_IPV4_IFADDR
|
1779 RTMGRP_IPV6_IFADDR
|
1780 RTMGRP_IPV4_MROUTE
|
1782 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1783 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1784 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1)) |
1785 ((uint32_t) 1 << (RTNLGRP_TC
- 1));
1787 dplane_groups
= (RTMGRP_LINK
|
1788 RTMGRP_IPV4_IFADDR
|
1789 RTMGRP_IPV6_IFADDR
|
1790 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1791 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1792 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1794 /* Use setsockopt for > 31 group */
1795 ext_groups
= RTNLGRP_TUNNEL
;
1797 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1798 "netlink-listen (NS %u)", zns
->ns_id
);
1799 zns
->netlink
.sock
= -1;
1800 if (netlink_socket(&zns
->netlink
, groups
, &ext_groups
, 1, zns
->ns_id
) <
1802 zlog_err("Failure to create %s socket",
1807 kernel_netlink_nlsock_insert(&zns
->netlink
);
1809 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1810 "netlink-cmd (NS %u)", zns
->ns_id
);
1811 zns
->netlink_cmd
.sock
= -1;
1812 if (netlink_socket(&zns
->netlink_cmd
, 0, 0, 0, zns
->ns_id
) < 0) {
1813 zlog_err("Failure to create %s socket",
1814 zns
->netlink_cmd
.name
);
1818 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1820 /* Outbound socket for dplane programming of the host OS. */
1821 snprintf(zns
->netlink_dplane_out
.name
,
1822 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1824 zns
->netlink_dplane_out
.sock
= -1;
1825 if (netlink_socket(&zns
->netlink_dplane_out
, 0, 0, 0, zns
->ns_id
) < 0) {
1826 zlog_err("Failure to create %s socket",
1827 zns
->netlink_dplane_out
.name
);
1831 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1833 /* Inbound socket for OS events coming to the dplane. */
1834 snprintf(zns
->netlink_dplane_in
.name
,
1835 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1837 zns
->netlink_dplane_in
.sock
= -1;
1838 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
, 0, 0,
1840 zlog_err("Failure to create %s socket",
1841 zns
->netlink_dplane_in
.name
);
1845 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1848 * SOL_NETLINK is not available on all platforms yet
1849 * apparently. It's in bits/socket.h which I am not
1850 * sure that we want to pull into our build system.
1852 #if defined SOL_NETLINK
1854 * Let's tell the kernel that we want to receive extended
1855 * ACKS over our command socket(s)
1858 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1862 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1863 errno
, safe_strerror(errno
));
1866 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1867 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1870 zlog_notice("Registration for extended dp ACK failed : %d %s",
1871 errno
, safe_strerror(errno
));
1874 * Trim off the payload of the original netlink message in the
1875 * acknowledgment. This option is available since Linux 4.2, so if
1876 * setsockopt fails, ignore the error.
1879 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1880 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1883 "Registration for reduced ACK packet size failed, probably running an early kernel");
1886 /* Register kernel socket. */
1887 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1888 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1889 zns
->netlink
.name
, safe_strerror(errno
));
1891 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1892 zlog_err("Can't set %s socket error: %s(%d)",
1893 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1895 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1896 zlog_err("Can't set %s socket error: %s(%d)",
1897 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1900 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1901 zlog_err("Can't set %s socket error: %s(%d)",
1902 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1905 /* Set receive buffer size if it's set from command line */
1907 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1908 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1909 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1910 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1913 /* Set filter for inbound sockets, to exclude events we've generated
1916 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1917 zns
->netlink_dplane_out
.snl
.nl_pid
);
1919 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1920 zns
->netlink_cmd
.snl
.nl_pid
,
1921 zns
->netlink_dplane_out
.snl
.nl_pid
);
1923 zns
->t_netlink
= NULL
;
1925 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1926 zns
->netlink
.sock
, &zns
->t_netlink
);
1931 /* Helper to clean up an nlsock */
1932 static void kernel_nlsock_fini(struct nlsock
*nls
)
1934 if (nls
&& nls
->sock
>= 0) {
1935 kernel_netlink_nlsock_remove(nls
);
1938 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1943 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1945 THREAD_OFF(zns
->t_netlink
);
1947 kernel_nlsock_fini(&zns
->netlink
);
1949 kernel_nlsock_fini(&zns
->netlink_cmd
);
1951 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1953 /* During zebra shutdown, we need to leave the dataplane socket
1954 * around until all work is done.
1957 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1961 * Global init for platform-/OS-specific things
1963 void kernel_router_init(void)
1965 /* Init nlsock hash and lock */
1966 pthread_mutex_init(&nlsock_mutex
, NULL
);
1967 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
1968 kernel_netlink_nlsock_hash_equal
,
1969 "Netlink Socket Hash");
1973 * Global deinit for platform-/OS-specific things
1975 void kernel_router_terminate(void)
1977 pthread_mutex_destroy(&nlsock_mutex
);
1979 hash_free(nlsock_hash
);
1983 #endif /* HAVE_NETLINK */