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/netconf_netlink.h"
51 #include "zebra/zebra_errors.h"
53 #ifndef SO_RCVBUFFORCE
54 #define SO_RCVBUFFORCE (33)
57 /* Hack for GNU libc version 2. */
59 #define MSG_TRUNC 0x20
60 #endif /* MSG_TRUNC */
63 #define NLMSG_TAIL(nmsg) \
64 ((struct rtattr *)(((uint8_t *)(nmsg)) \
65 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
69 #define RTA_TAIL(rta) \
70 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
73 #ifndef RTNL_FAMILY_IP6MR
74 #define RTNL_FAMILY_IP6MR 129
77 #ifndef RTPROT_MROUTED
78 #define RTPROT_MROUTED 17
81 #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
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
91 #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
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"},
116 static const struct message rtproto_str
[] = {
117 {RTPROT_REDIRECT
, "redirect"},
118 {RTPROT_KERNEL
, "kernel"},
119 {RTPROT_BOOT
, "boot"},
120 {RTPROT_STATIC
, "static"},
121 {RTPROT_GATED
, "GateD"},
122 {RTPROT_RA
, "router advertisement"},
124 {RTPROT_ZEBRA
, "Zebra"},
126 {RTPROT_BIRD
, "BIRD"},
127 #endif /* RTPROT_BIRD */
128 {RTPROT_MROUTED
, "mroute"},
130 {RTPROT_OSPF
, "OSPF"},
131 {RTPROT_ISIS
, "IS-IS"},
133 {RTPROT_RIPNG
, "RIPNG"},
134 {RTPROT_ZSTATIC
, "static"},
137 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
139 {AF_BRIDGE
, "bridge"},
140 {RTNL_FAMILY_IPMR
, "ipv4MR"},
141 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
144 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
145 {RTN_UNICAST
, "unicast"},
146 {RTN_LOCAL
, "local"},
147 {RTN_BROADCAST
, "broadcast"},
148 {RTN_ANYCAST
, "anycast"},
149 {RTN_MULTICAST
, "multicast"},
150 {RTN_BLACKHOLE
, "blackhole"},
151 {RTN_UNREACHABLE
, "unreachable"},
152 {RTN_PROHIBIT
, "prohibited"},
153 {RTN_THROW
, "throw"},
155 {RTN_XRESOLVE
, "resolver"},
158 extern struct thread_master
*master
;
160 extern struct zebra_privs_t zserv_privs
;
162 DEFINE_MTYPE_STATIC(ZEBRA
, NL_BUF
, "Zebra Netlink buffers");
164 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
165 * We have both the main and dplane pthreads using these structs, so we have
166 * to protect the hash with a lock.
168 static struct hash
*nlsock_hash
;
169 pthread_mutex_t nlsock_mutex
;
171 /* Lock and unlock wrappers for nlsock hash */
172 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
173 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
175 size_t nl_batch_tx_bufsize
;
176 char *nl_batch_tx_buf
;
178 _Atomic
uint32_t nl_batch_bufsize
= NL_DEFAULT_BATCH_BUFSIZE
;
179 _Atomic
uint32_t nl_batch_send_threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
190 const struct zebra_dplane_info
*zns
;
192 struct dplane_ctx_q ctx_list
;
195 * Pointer to the queue of completed contexts outbound back
196 * towards the dataplane module.
198 struct dplane_ctx_q
*ctx_out_q
;
201 int netlink_config_write_helper(struct vty
*vty
)
204 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
205 uint32_t threshold
= atomic_load_explicit(&nl_batch_send_threshold
,
206 memory_order_relaxed
);
208 if (size
!= NL_DEFAULT_BATCH_BUFSIZE
209 || threshold
!= NL_DEFAULT_BATCH_SEND_THRESHOLD
)
210 vty_out(vty
, "zebra kernel netlink batch-tx-buf %u %u\n", size
,
213 if (if_netlink_frr_protodown_r_bit_is_set())
214 vty_out(vty
, "zebra protodown reason-bit %u\n",
215 if_netlink_get_frr_protodown_r_bit());
220 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
223 size
= NL_DEFAULT_BATCH_BUFSIZE
;
224 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
227 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
228 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
229 memory_order_relaxed
);
232 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
235 * This is an error condition that must be handled during
238 * The netlink_talk_filter function is used for communication
239 * down the netlink_cmd pipe and we are expecting
240 * an ack being received. So if we get here
241 * then we did not receive the ack and instead
242 * received some other message in an unexpected
245 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
246 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
250 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
253 socklen_t newlen
= sizeof(newsize
);
254 socklen_t oldlen
= sizeof(oldsize
);
257 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
259 flog_err_sys(EC_LIB_SOCKET
,
260 "Can't get %s receive buffer size: %s", nl
->name
,
261 safe_strerror(errno
));
265 /* Try force option (linux >= 2.6.14) and fall back to normal set */
266 frr_with_privs(&zserv_privs
) {
267 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
268 &rcvbufsize
, sizeof(rcvbufsize
));
271 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
274 flog_err_sys(EC_LIB_SOCKET
,
275 "Can't set %s receive buffer size: %s", nl
->name
,
276 safe_strerror(errno
));
280 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
282 flog_err_sys(EC_LIB_SOCKET
,
283 "Can't get %s receive buffer size: %s", nl
->name
,
284 safe_strerror(errno
));
290 /* Make socket for Linux netlink interface. */
291 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
292 unsigned long ext_groups
, ns_id_t ns_id
)
295 struct sockaddr_nl snl
;
299 frr_with_privs(&zserv_privs
) {
300 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
302 zlog_err("Can't open %s socket: %s", nl
->name
,
303 safe_strerror(errno
));
307 memset(&snl
, 0, sizeof(snl
));
308 snl
.nl_family
= AF_NETLINK
;
309 snl
.nl_groups
= groups
;
311 #if defined SOL_NETLINK
313 ret
= setsockopt(sock
, SOL_NETLINK
,
314 NETLINK_ADD_MEMBERSHIP
, &ext_groups
,
318 "can't setsockopt NETLINK_ADD_MEMBERSHIP: %s(%d)",
319 safe_strerror(errno
), errno
);
324 /* Bind the socket to the netlink structure for anything. */
325 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
329 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
330 snl
.nl_groups
, safe_strerror(errno
));
335 /* multiple netlink sockets will have different nl_pid */
336 namelen
= sizeof(snl
);
337 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
338 if (ret
< 0 || namelen
!= sizeof(snl
)) {
339 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
340 nl
->name
, safe_strerror(errno
));
347 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
348 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
354 * Dispatch an incoming netlink message; used by the zebra main pthread's
355 * netlink event reader.
357 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
361 * When we handle new message types here
362 * because we are starting to install them
363 * then lets check the netlink_install_filter
364 * and see if we should add the corresponding
365 * allow through entry there.
366 * Probably not needed to do but please
369 switch (h
->nlmsg_type
) {
371 return netlink_route_change(h
, ns_id
, startup
);
373 return netlink_route_change(h
, ns_id
, startup
);
375 return netlink_link_change(h
, ns_id
, startup
);
377 return netlink_link_change(h
, ns_id
, startup
);
381 return netlink_neigh_change(h
, ns_id
);
383 return netlink_rule_change(h
, ns_id
, startup
);
385 return netlink_rule_change(h
, ns_id
, startup
);
387 return netlink_nexthop_change(h
, ns_id
, startup
);
389 return netlink_nexthop_change(h
, ns_id
, startup
);
391 /* Messages handled in the dplane thread */
400 * If we have received this message then
401 * we have made a mistake during development
402 * and we need to write some code to handle
403 * this message type or not ask for
404 * it to be sent up to us
406 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
407 "Unknown netlink nlmsg_type %s(%d) vrf %u",
408 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
416 * Dispatch an incoming netlink message; used by the dataplane pthread's
417 * netlink event reader code.
419 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
423 * Dispatch the incoming messages that the dplane pthread handles
425 switch (h
->nlmsg_type
) {
428 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
432 return netlink_netconf_change(h
, ns_id
, startup
);
434 /* TODO -- other messages for the dplane socket and pthread */
446 static void kernel_read(struct thread
*thread
)
448 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
449 struct zebra_dplane_info dp_info
;
451 /* Capture key info from ns struct */
452 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
454 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
457 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
462 * Called by the dplane pthread to read incoming OS messages and dispatch them.
464 int kernel_dplane_read(struct zebra_dplane_info
*info
)
466 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
468 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
475 * Filter out messages from self that occur on listener socket,
476 * caused by our actions on the command socket(s)
478 * When we add new Netlink message types we probably
479 * do not need to add them here as that we are filtering
480 * on the routes we actually care to receive( which is rarer
481 * then the normal course of operations). We are intentionally
482 * allowing some messages from ourselves through
483 * ( I'm looking at you Interface based netlink messages )
484 * so that we only have to write one way to handle incoming
485 * address add/delete and xxxNETCONF changes.
487 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
490 * BPF_JUMP instructions and where you jump to are based upon
491 * 0 as being the next statement. So count from 0. Writing
492 * this down because every time I look at this I have to
495 struct sock_filter filter
[] = {
498 * if (nlmsg_pid == pid ||
499 * nlmsg_pid == dplane_pid) {
500 * if (the incoming nlmsg_type ==
501 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
507 * keep this netlink message
510 * 0: Load the nlmsg_pid into the BPF register
512 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
513 offsetof(struct nlmsghdr
, nlmsg_pid
)),
517 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
519 * 2: Compare to dplane pid
521 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
523 * 3: Load the nlmsg_type into BPF register
525 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
526 offsetof(struct nlmsghdr
, nlmsg_type
)),
528 * 4: Compare to RTM_NEWADDR
530 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
532 * 5: Compare to RTM_DELADDR
534 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
536 * 6: Compare to RTM_NEWNETCONF
538 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
541 * 7: Compare to RTM_DELNETCONF
543 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
546 * 8: This is the end state of we want to skip the
549 BPF_STMT(BPF_RET
| BPF_K
, 0),
550 /* 9: This is the end state of we want to keep
553 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
556 struct sock_fprog prog
= {
557 .len
= array_size(filter
), .filter
= filter
,
560 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
562 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
563 safe_strerror(errno
));
566 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
567 int len
, unsigned short flags
)
571 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
572 while (RTA_OK(rta
, len
)) {
573 type
= rta
->rta_type
& ~flags
;
574 if ((type
<= max
) && (!tb
[type
]))
576 rta
= RTA_NEXT(rta
, len
);
580 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
583 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
584 while (RTA_OK(rta
, len
)) {
585 if (rta
->rta_type
<= max
)
586 tb
[rta
->rta_type
] = rta
;
587 rta
= RTA_NEXT(rta
, len
);
592 * netlink_parse_rtattr_nested() - Parses a nested route attribute
593 * @tb: Pointer to array for storing rtattr in.
594 * @max: Max number to store.
595 * @rta: Pointer to rtattr to look for nested items in.
597 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
600 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
603 bool nl_addraw_l(struct nlmsghdr
*n
, unsigned int maxlen
, const void *data
,
606 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
607 zlog_err("ERROR message exceeded bound of %d", maxlen
);
611 memcpy(NLMSG_TAIL(n
), data
, len
);
612 memset((uint8_t *)NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
613 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
618 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
619 const void *data
, unsigned int alen
)
624 len
= RTA_LENGTH(alen
);
626 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
629 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
630 rta
->rta_type
= type
;
634 memcpy(RTA_DATA(rta
), data
, alen
);
638 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
643 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
646 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
649 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
652 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
655 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
658 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
661 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
663 struct rtattr
*nest
= NLMSG_TAIL(n
);
665 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
668 nest
->rta_type
|= NLA_F_NESTED
;
672 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
674 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
678 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
680 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
682 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
686 memset(rtnh
, 0, sizeof(struct rtnexthop
));
688 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
693 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
695 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
698 bool nl_rta_put(struct rtattr
*rta
, unsigned int maxlen
, int type
,
699 const void *data
, int alen
)
701 struct rtattr
*subrta
;
702 int len
= RTA_LENGTH(alen
);
704 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
705 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
709 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
710 subrta
->rta_type
= type
;
711 subrta
->rta_len
= len
;
713 memcpy(RTA_DATA(subrta
), data
, alen
);
714 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
719 bool nl_rta_put16(struct rtattr
*rta
, unsigned int maxlen
, int type
,
722 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint16_t));
725 bool nl_rta_put64(struct rtattr
*rta
, unsigned int maxlen
, int type
,
728 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint64_t));
731 struct rtattr
*nl_rta_nest(struct rtattr
*rta
, unsigned int maxlen
, int type
)
733 struct rtattr
*nest
= RTA_TAIL(rta
);
735 if (nl_rta_put(rta
, maxlen
, type
, NULL
, 0))
738 nest
->rta_type
|= NLA_F_NESTED
;
743 int nl_rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
745 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
750 const char *nl_msg_type_to_str(uint16_t msg_type
)
752 return lookup_msg(nlmsg_str
, msg_type
, "");
755 const char *nl_rtproto_to_str(uint8_t rtproto
)
757 return lookup_msg(rtproto_str
, rtproto
, "");
760 const char *nl_family_to_str(uint8_t family
)
762 return lookup_msg(family_str
, family
, "");
765 const char *nl_rttype_to_str(uint8_t rttype
)
767 return lookup_msg(rttype_str
, rttype
, "");
770 #define NLA_OK(nla, len) \
771 ((len) >= (int)sizeof(struct nlattr) \
772 && (nla)->nla_len >= sizeof(struct nlattr) \
773 && (nla)->nla_len <= (len))
774 #define NLA_NEXT(nla, attrlen) \
775 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
776 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
777 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
778 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
780 #define ERR_NLA(err, inner_len) \
781 ((struct nlattr *)(((char *)(err)) \
782 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
783 + NLMSG_ALIGN((inner_len))))
785 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
786 struct nlattr
*nla
, int len
)
788 while (NLA_OK(nla
, len
)) {
789 if (nla
->nla_type
<= max
)
790 tb
[nla
->nla_type
] = nla
;
791 nla
= NLA_NEXT(nla
, len
);
795 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
797 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
798 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
799 const struct nlmsghdr
*err_nlh
= NULL
;
800 /* Length not including nlmsghdr */
802 /* Inner error netlink message length */
803 uint32_t inner_len
= 0;
804 const char *msg
= NULL
;
807 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
808 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
810 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
812 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
815 if (tb
[NLMSGERR_ATTR_MSG
])
816 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
818 if (tb
[NLMSGERR_ATTR_OFFS
]) {
819 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
821 if (off
> h
->nlmsg_len
) {
822 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
823 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
825 * Header of failed message
826 * we are not doing anything currently with it
827 * but noticing it for later.
830 zlog_debug("%s: Received %s extended Ack", __func__
,
831 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
835 if (msg
&& *msg
!= '\0') {
836 bool is_err
= !!err
->error
;
839 zlog_err("Extended Error: %s", msg
);
841 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
842 "Extended Warning: %s", msg
);
847 * netlink_send_msg - send a netlink message of a certain size.
849 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
851 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
854 struct sockaddr_nl snl
= {};
855 struct iovec iov
= {};
856 struct msghdr msg
= {};
861 iov
.iov_len
= buflen
;
863 msg
.msg_namelen
= sizeof(snl
);
867 snl
.nl_family
= AF_NETLINK
;
869 /* Send message to netlink interface. */
870 frr_with_privs(&zserv_privs
) {
871 status
= sendmsg(nl
->sock
, &msg
, 0);
875 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
876 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
878 nl_dump(buf
, buflen
);
880 zlog_hexdump(buf
, buflen
);
881 #endif /* NETLINK_DEBUG */
885 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
886 safe_strerror(save_errno
));
894 * netlink_recv_msg - receive a netlink message.
896 * Returns -1 on error, 0 if read would block or the number of bytes received.
898 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
903 iov
.iov_base
= nl
->buf
;
904 iov
.iov_len
= nl
->buflen
;
911 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
913 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
914 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
916 iov
.iov_base
= nl
->buf
;
917 iov
.iov_len
= nl
->buflen
;
920 status
= recvmsg(nl
->sock
, msg
, 0);
921 } while (status
== -1 && errno
== EINTR
);
924 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
926 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
927 nl
->name
, safe_strerror(errno
));
929 * In this case we are screwed. There is no good way to recover
930 * zebra at this point.
936 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
940 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
941 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
942 "%s sender address length error: length %d", nl
->name
,
947 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
948 zlog_debug("%s: << netlink message dump [recv]", __func__
);
950 nl_dump(nl
->buf
, status
);
952 zlog_hexdump(nl
->buf
, status
);
953 #endif /* NETLINK_DEBUG */
960 * netlink_parse_error - parse a netlink error message
962 * Returns 1 if this message is acknowledgement, 0 if this error should be
963 * ignored, -1 otherwise.
965 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
966 bool is_cmd
, bool startup
)
968 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
969 int errnum
= err
->error
;
970 int msg_type
= err
->msg
.nlmsg_type
;
972 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
973 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
974 "%s error: message truncated", nl
->name
);
979 * Parse the extended information before we actually handle it. At this
980 * point in time we do not do anything other than report the issue.
982 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
983 netlink_parse_extended_ack(h
);
985 /* If the error field is zero, then this is an ACK. */
986 if (err
->error
== 0) {
987 if (IS_ZEBRA_DEBUG_KERNEL
) {
988 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
990 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
991 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
998 /* Deal with errors that occur because of races in link handling. */
1000 && ((msg_type
== RTM_DELROUTE
1001 && (-errnum
== ENODEV
|| -errnum
== ESRCH
))
1002 || (msg_type
== RTM_NEWROUTE
1003 && (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)))) {
1004 if (IS_ZEBRA_DEBUG_KERNEL
)
1005 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1006 nl
->name
, safe_strerror(-errnum
),
1007 nl_msg_type_to_str(msg_type
), msg_type
,
1008 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1013 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1014 * link-local. The kernel should have already deleted the neighbor so
1015 * do not log these as an error.
1017 if (msg_type
== RTM_DELNEIGH
1018 || (is_cmd
&& msg_type
== RTM_NEWROUTE
1019 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
1021 * This is known to happen in some situations, don't log as
1024 if (IS_ZEBRA_DEBUG_KERNEL
)
1025 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1026 nl
->name
, safe_strerror(-errnum
),
1027 nl_msg_type_to_str(msg_type
), msg_type
,
1028 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1030 if ((msg_type
!= RTM_GETNEXTHOP
) || !startup
)
1031 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
1032 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1033 nl
->name
, safe_strerror(-errnum
),
1034 nl_msg_type_to_str(msg_type
), msg_type
,
1035 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1042 * netlink_parse_info
1044 * Receive message from netlink interface and pass those information
1045 * to the given function.
1047 * filter -> Function to call to read the results
1048 * nl -> netlink socket information
1049 * zns -> The zebra namespace data
1050 * count -> How many we should read in, 0 means as much as possible
1051 * startup -> Are we reading in under startup conditions? passed to
1054 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
1055 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
1056 int count
, bool startup
)
1064 struct sockaddr_nl snl
;
1065 struct msghdr msg
= {.msg_name
= (void *)&snl
,
1066 .msg_namelen
= sizeof(snl
)};
1069 if (count
&& read_in
>= count
)
1072 status
= netlink_recv_msg(nl
, &msg
);
1075 else if (status
== 0)
1079 for (h
= (struct nlmsghdr
*)nl
->buf
;
1080 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
1081 h
= NLMSG_NEXT(h
, status
)) {
1082 /* Finish of reading. */
1083 if (h
->nlmsg_type
== NLMSG_DONE
)
1086 /* Error handling. */
1087 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1088 int err
= netlink_parse_error(
1089 nl
, h
, zns
->is_cmd
, startup
);
1092 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1100 * What is the right thing to do? The kernel
1101 * is telling us that the dump request was interrupted
1102 * and we more than likely are out of luck and have
1103 * missed data from the kernel. At this point in time
1104 * lets just note that this is happening.
1106 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
1108 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
1109 "netlink recvmsg: The Dump request was interrupted");
1111 /* OK we got netlink message. */
1112 if (IS_ZEBRA_DEBUG_KERNEL
)
1114 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1116 nl_msg_type_to_str(h
->nlmsg_type
),
1117 h
->nlmsg_type
, h
->nlmsg_len
,
1118 h
->nlmsg_seq
, h
->nlmsg_pid
);
1122 * Ignore messages that maybe sent from
1123 * other actors besides the kernel
1125 if (snl
.nl_pid
!= 0) {
1126 zlog_debug("Ignoring message from pid %u",
1131 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1133 zlog_debug("%s filter function error",
1139 /* After error care. */
1140 if (msg
.msg_flags
& MSG_TRUNC
) {
1141 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1142 "%s error: message truncated", nl
->name
);
1146 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1147 "%s error: data remnant size %d", nl
->name
,
1158 * sendmsg() to netlink socket then recvmsg().
1159 * Calls netlink_parse_info to parse returned data
1161 * filter -> The filter to read final results from kernel
1162 * nlmsghdr -> The data to send to the kernel
1163 * dp_info -> The dataplane and netlink socket information
1164 * startup -> Are we reading in under startup conditions
1165 * This is passed through eventually to filter.
1167 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1170 struct zebra_dplane_info
*dp_info
, bool startup
)
1174 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1175 n
->nlmsg_seq
= dp_info
->seq
;
1176 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1178 if (IS_ZEBRA_DEBUG_KERNEL
)
1180 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1181 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1182 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1185 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1189 * Get reply from netlink socket.
1190 * The reply should either be an acknowlegement or an error.
1192 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1196 * Synchronous version of netlink_talk_info. Converts args to suit the
1197 * common version, which is suitable for both sync and async use.
1199 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1200 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1203 struct zebra_dplane_info dp_info
;
1205 /* Increment sequence number before capturing snapshot of ns socket
1210 /* Capture info in intermediate info struct */
1211 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1213 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1216 /* Issue request message to kernel via netlink socket. GET messages
1217 * are issued through this interface.
1219 int netlink_request(struct nlsock
*nl
, void *req
)
1221 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1223 /* Check netlink socket. */
1225 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1230 /* Fill common fields for all requests. */
1231 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1232 n
->nlmsg_seq
= ++nl
->seq
;
1234 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1240 static int nl_batch_read_resp(struct nl_batch
*bth
)
1243 struct sockaddr_nl snl
;
1244 struct msghdr msg
= {};
1247 struct zebra_dplane_ctx
*ctx
;
1250 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1252 msg
.msg_name
= (void *)&snl
;
1253 msg
.msg_namelen
= sizeof(snl
);
1256 * The responses are not batched, so we need to read and process one
1257 * message at a time.
1260 status
= netlink_recv_msg(nl
, &msg
);
1262 * status == -1 is a full on failure somewhere
1263 * since we don't know where the problem happened
1264 * we must mark all as failed
1266 * Else we mark everything as worked
1269 if (status
== -1 || status
== 0) {
1270 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1273 dplane_ctx_set_status(
1275 ZEBRA_DPLANE_REQUEST_FAILURE
);
1276 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1281 h
= (struct nlmsghdr
*)nl
->buf
;
1285 * Find the corresponding context object. Received responses are
1286 * in the same order as requests we sent, so we can simply
1287 * iterate over the context list and match responses with
1288 * requests at same time.
1291 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1294 * This is a situation where we have gotten
1295 * into a bad spot. We need to know that
1296 * this happens( does it? )
1299 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1305 * 'update' context objects take two consecutive
1308 if (dplane_ctx_is_update(ctx
) &&
1309 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1311 * This is the situation where we get a response
1312 * to a message that should be ignored.
1318 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1319 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1321 /* We have found corresponding context object. */
1322 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1325 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1327 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1329 dplane_ctx_get_ns(ctx
)->seq
);
1334 * If we ignore the message due to an update
1335 * above we should still fricking decode the
1336 * message for our operator to understand
1339 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1342 zlog_debug("%s: netlink error message seq=%d %d",
1343 __func__
, h
->nlmsg_seq
, err
);
1348 * We received a message with the sequence number that isn't
1349 * associated with any dplane context object.
1352 if (IS_ZEBRA_DEBUG_KERNEL
)
1354 "%s: skipping unassociated response, seq number %d NS %u",
1355 __func__
, h
->nlmsg_seq
,
1360 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1361 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1365 dplane_ctx_set_status(
1366 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1368 if (IS_ZEBRA_DEBUG_KERNEL
)
1369 zlog_debug("%s: netlink error message seq=%d ",
1370 __func__
, h
->nlmsg_seq
);
1375 * If we get here then we did not receive neither the ack nor
1376 * the error and instead received some other message in an
1379 if (IS_ZEBRA_DEBUG_KERNEL
)
1380 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1381 __func__
, h
->nlmsg_type
,
1382 nl_msg_type_to_str(h
->nlmsg_type
),
1389 static void nl_batch_reset(struct nl_batch
*bth
)
1391 bth
->buf_head
= bth
->buf
;
1396 TAILQ_INIT(&(bth
->ctx_list
));
1399 static void nl_batch_init(struct nl_batch
*bth
, struct dplane_ctx_q
*ctx_out_q
)
1402 * If the size of the buffer has changed, free and then allocate a new
1406 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1407 if (bufsize
!= nl_batch_tx_bufsize
) {
1408 if (nl_batch_tx_buf
)
1409 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1411 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1412 nl_batch_tx_bufsize
= bufsize
;
1415 bth
->buf
= nl_batch_tx_buf
;
1416 bth
->bufsiz
= bufsize
;
1417 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1418 memory_order_relaxed
);
1420 bth
->ctx_out_q
= ctx_out_q
;
1422 nl_batch_reset(bth
);
1425 static void nl_batch_send(struct nl_batch
*bth
)
1427 struct zebra_dplane_ctx
*ctx
;
1430 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1432 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1434 if (IS_ZEBRA_DEBUG_KERNEL
)
1435 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1436 __func__
, nl
->name
, bth
->curlen
,
1439 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1443 if (nl_batch_read_resp(bth
) == -1)
1448 /* Move remaining contexts to the outbound queue. */
1450 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1455 dplane_ctx_set_status(ctx
,
1456 ZEBRA_DPLANE_REQUEST_FAILURE
);
1458 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1461 nl_batch_reset(bth
);
1464 enum netlink_msg_status
netlink_batch_add_msg(
1465 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1466 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1471 struct nlmsghdr
*msgh
;
1474 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1477 * If there was an error while encoding the message (other than buffer
1478 * overflow) then return an error.
1481 return FRR_NETLINK_ERROR
;
1484 * If the message doesn't fit entirely in the buffer then send the batch
1489 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1490 bth
->bufsiz
- bth
->curlen
);
1492 * If the message doesn't fit in the empty buffer then just
1496 return FRR_NETLINK_ERROR
;
1499 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1500 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1505 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1506 msgh
->nlmsg_seq
= seq
;
1507 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1509 bth
->zns
= dplane_ctx_get_ns(ctx
);
1510 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1511 bth
->curlen
+= size
;
1514 return FRR_NETLINK_QUEUED
;
1517 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1518 struct zebra_dplane_ctx
*ctx
)
1520 if (dplane_ctx_is_skip_kernel(ctx
))
1521 return FRR_NETLINK_SUCCESS
;
1523 switch (dplane_ctx_get_op(ctx
)) {
1525 case DPLANE_OP_ROUTE_INSTALL
:
1526 case DPLANE_OP_ROUTE_UPDATE
:
1527 case DPLANE_OP_ROUTE_DELETE
:
1528 return netlink_put_route_update_msg(bth
, ctx
);
1530 case DPLANE_OP_NH_INSTALL
:
1531 case DPLANE_OP_NH_UPDATE
:
1532 case DPLANE_OP_NH_DELETE
:
1533 return netlink_put_nexthop_update_msg(bth
, ctx
);
1535 case DPLANE_OP_LSP_INSTALL
:
1536 case DPLANE_OP_LSP_UPDATE
:
1537 case DPLANE_OP_LSP_DELETE
:
1538 return netlink_put_lsp_update_msg(bth
, ctx
);
1540 case DPLANE_OP_PW_INSTALL
:
1541 case DPLANE_OP_PW_UNINSTALL
:
1542 return netlink_put_pw_update_msg(bth
, ctx
);
1544 case DPLANE_OP_ADDR_INSTALL
:
1545 case DPLANE_OP_ADDR_UNINSTALL
:
1546 return netlink_put_address_update_msg(bth
, ctx
);
1548 case DPLANE_OP_MAC_INSTALL
:
1549 case DPLANE_OP_MAC_DELETE
:
1550 return netlink_put_mac_update_msg(bth
, ctx
);
1552 case DPLANE_OP_NEIGH_INSTALL
:
1553 case DPLANE_OP_NEIGH_UPDATE
:
1554 case DPLANE_OP_NEIGH_DELETE
:
1555 case DPLANE_OP_VTEP_ADD
:
1556 case DPLANE_OP_VTEP_DELETE
:
1557 case DPLANE_OP_NEIGH_DISCOVER
:
1558 case DPLANE_OP_NEIGH_IP_INSTALL
:
1559 case DPLANE_OP_NEIGH_IP_DELETE
:
1560 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1561 return netlink_put_neigh_update_msg(bth
, ctx
);
1563 case DPLANE_OP_RULE_ADD
:
1564 case DPLANE_OP_RULE_DELETE
:
1565 case DPLANE_OP_RULE_UPDATE
:
1566 return netlink_put_rule_update_msg(bth
, ctx
);
1568 case DPLANE_OP_SYS_ROUTE_ADD
:
1569 case DPLANE_OP_SYS_ROUTE_DELETE
:
1570 case DPLANE_OP_ROUTE_NOTIFY
:
1571 case DPLANE_OP_LSP_NOTIFY
:
1572 case DPLANE_OP_BR_PORT_UPDATE
:
1573 return FRR_NETLINK_SUCCESS
;
1575 case DPLANE_OP_IPTABLE_ADD
:
1576 case DPLANE_OP_IPTABLE_DELETE
:
1577 case DPLANE_OP_IPSET_ADD
:
1578 case DPLANE_OP_IPSET_DELETE
:
1579 case DPLANE_OP_IPSET_ENTRY_ADD
:
1580 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1581 return FRR_NETLINK_ERROR
;
1583 case DPLANE_OP_GRE_SET
:
1584 return netlink_put_gre_set_msg(bth
, ctx
);
1586 case DPLANE_OP_INTF_ADDR_ADD
:
1587 case DPLANE_OP_INTF_ADDR_DEL
:
1588 case DPLANE_OP_INTF_NETCONFIG
:
1589 case DPLANE_OP_NONE
:
1590 return FRR_NETLINK_ERROR
;
1592 case DPLANE_OP_INTF_INSTALL
:
1593 case DPLANE_OP_INTF_UPDATE
:
1594 case DPLANE_OP_INTF_DELETE
:
1595 return netlink_put_intf_update_msg(bth
, ctx
);
1598 return FRR_NETLINK_ERROR
;
1601 void kernel_update_multi(struct dplane_ctx_q
*ctx_list
)
1603 struct nl_batch batch
;
1604 struct zebra_dplane_ctx
*ctx
;
1605 struct dplane_ctx_q handled_list
;
1606 enum netlink_msg_status res
;
1608 TAILQ_INIT(&handled_list
);
1609 nl_batch_init(&batch
, &handled_list
);
1612 ctx
= dplane_ctx_dequeue(ctx_list
);
1616 if (batch
.zns
!= NULL
1617 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1618 nl_batch_send(&batch
);
1621 * Assume all messages will succeed and then mark only the ones
1624 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1626 res
= nl_put_msg(&batch
, ctx
);
1628 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1629 if (res
== FRR_NETLINK_ERROR
)
1630 dplane_ctx_set_status(ctx
,
1631 ZEBRA_DPLANE_REQUEST_FAILURE
);
1633 if (batch
.curlen
> batch
.limit
)
1634 nl_batch_send(&batch
);
1637 nl_batch_send(&batch
);
1639 TAILQ_INIT(ctx_list
);
1640 dplane_ctx_list_append(ctx_list
, &handled_list
);
1643 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1645 struct nlsock lookup
, *retval
;
1650 retval
= hash_lookup(nlsock_hash
, &lookup
);
1656 /* Insert nlsock entry into hash */
1657 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1660 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1664 /* Remove nlsock entry from hash */
1665 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1668 (void)hash_release(nlsock_hash
, nls
);
1672 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1674 const struct nlsock
*nl
= arg
;
1679 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1681 const struct nlsock
*nl1
= arg1
;
1682 const struct nlsock
*nl2
= arg2
;
1684 if (nl1
->sock
== nl2
->sock
)
1690 /* Exported interface function. This function simply calls
1691 netlink_socket (). */
1692 void kernel_init(struct zebra_ns
*zns
)
1694 uint32_t groups
, dplane_groups
, ext_groups
;
1695 #if defined SOL_NETLINK
1700 * Initialize netlink sockets
1702 * If RTMGRP_XXX exists use that, but at some point
1703 * I think the kernel developers realized that
1704 * keeping track of all the different values would
1705 * lead to confusion, so we need to convert the
1706 * RTNLGRP_XXX to a bit position for ourself
1708 groups
= RTMGRP_LINK
|
1710 RTMGRP_IPV4_IFADDR
|
1712 RTMGRP_IPV6_IFADDR
|
1713 RTMGRP_IPV4_MROUTE
|
1715 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1716 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1717 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1));
1719 dplane_groups
= (RTMGRP_LINK
|
1720 RTMGRP_IPV4_IFADDR
|
1721 RTMGRP_IPV6_IFADDR
|
1722 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1723 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1724 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1726 /* Use setsockopt for > 31 group */
1727 ext_groups
= RTNLGRP_TUNNEL
;
1729 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1730 "netlink-listen (NS %u)", zns
->ns_id
);
1731 zns
->netlink
.sock
= -1;
1732 if (netlink_socket(&zns
->netlink
, groups
, ext_groups
, zns
->ns_id
) < 0) {
1733 zlog_err("Failure to create %s socket",
1738 kernel_netlink_nlsock_insert(&zns
->netlink
);
1740 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1741 "netlink-cmd (NS %u)", zns
->ns_id
);
1742 zns
->netlink_cmd
.sock
= -1;
1743 if (netlink_socket(&zns
->netlink_cmd
, 0, 0, zns
->ns_id
) < 0) {
1744 zlog_err("Failure to create %s socket",
1745 zns
->netlink_cmd
.name
);
1749 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1751 /* Outbound socket for dplane programming of the host OS. */
1752 snprintf(zns
->netlink_dplane_out
.name
,
1753 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1755 zns
->netlink_dplane_out
.sock
= -1;
1756 if (netlink_socket(&zns
->netlink_dplane_out
, 0, 0, zns
->ns_id
) < 0) {
1757 zlog_err("Failure to create %s socket",
1758 zns
->netlink_dplane_out
.name
);
1762 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1764 /* Inbound socket for OS events coming to the dplane. */
1765 snprintf(zns
->netlink_dplane_in
.name
,
1766 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1768 zns
->netlink_dplane_in
.sock
= -1;
1769 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
, 0,
1771 zlog_err("Failure to create %s socket",
1772 zns
->netlink_dplane_in
.name
);
1776 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1779 * SOL_NETLINK is not available on all platforms yet
1780 * apparently. It's in bits/socket.h which I am not
1781 * sure that we want to pull into our build system.
1783 #if defined SOL_NETLINK
1785 * Let's tell the kernel that we want to receive extended
1786 * ACKS over our command socket(s)
1789 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1793 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1794 errno
, safe_strerror(errno
));
1797 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1798 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1801 zlog_notice("Registration for extended dp ACK failed : %d %s",
1802 errno
, safe_strerror(errno
));
1805 * Trim off the payload of the original netlink message in the
1806 * acknowledgment. This option is available since Linux 4.2, so if
1807 * setsockopt fails, ignore the error.
1810 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1811 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1814 "Registration for reduced ACK packet size failed, probably running an early kernel");
1817 /* Register kernel socket. */
1818 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1819 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1820 zns
->netlink
.name
, safe_strerror(errno
));
1822 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1823 zlog_err("Can't set %s socket error: %s(%d)",
1824 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1826 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1827 zlog_err("Can't set %s socket error: %s(%d)",
1828 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1831 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1832 zlog_err("Can't set %s socket error: %s(%d)",
1833 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1836 /* Set receive buffer size if it's set from command line */
1838 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1839 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1840 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1841 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1844 /* Set filter for inbound sockets, to exclude events we've generated
1847 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1848 zns
->netlink_dplane_out
.snl
.nl_pid
);
1850 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1851 zns
->netlink_cmd
.snl
.nl_pid
,
1852 zns
->netlink_dplane_out
.snl
.nl_pid
);
1854 zns
->t_netlink
= NULL
;
1856 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1857 zns
->netlink
.sock
, &zns
->t_netlink
);
1862 /* Helper to clean up an nlsock */
1863 static void kernel_nlsock_fini(struct nlsock
*nls
)
1865 if (nls
&& nls
->sock
>= 0) {
1866 kernel_netlink_nlsock_remove(nls
);
1869 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1874 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1876 thread_cancel(&zns
->t_netlink
);
1878 kernel_nlsock_fini(&zns
->netlink
);
1880 kernel_nlsock_fini(&zns
->netlink_cmd
);
1882 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1884 /* During zebra shutdown, we need to leave the dataplane socket
1885 * around until all work is done.
1888 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1892 * Global init for platform-/OS-specific things
1894 void kernel_router_init(void)
1896 /* Init nlsock hash and lock */
1897 pthread_mutex_init(&nlsock_mutex
, NULL
);
1898 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
1899 kernel_netlink_nlsock_hash_equal
,
1900 "Netlink Socket Hash");
1904 * Global deinit for platform-/OS-specific things
1906 void kernel_router_terminate(void)
1908 pthread_mutex_destroy(&nlsock_mutex
);
1910 hash_free(nlsock_hash
);
1914 #endif /* HAVE_NETLINK */