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"},
114 {RTM_NEWTUNNEL
, "RTM_NEWTUNNEL"},
115 {RTM_DELTUNNEL
, "RTM_DELTUNNEL"},
116 {RTM_GETTUNNEL
, "RTM_GETTUNNEL"},
119 static const struct message rtproto_str
[] = {
120 {RTPROT_REDIRECT
, "redirect"},
121 {RTPROT_KERNEL
, "kernel"},
122 {RTPROT_BOOT
, "boot"},
123 {RTPROT_STATIC
, "static"},
124 {RTPROT_GATED
, "GateD"},
125 {RTPROT_RA
, "router advertisement"},
127 {RTPROT_ZEBRA
, "Zebra"},
129 {RTPROT_BIRD
, "BIRD"},
130 #endif /* RTPROT_BIRD */
131 {RTPROT_MROUTED
, "mroute"},
133 {RTPROT_OSPF
, "OSPF"},
134 {RTPROT_ISIS
, "IS-IS"},
136 {RTPROT_RIPNG
, "RIPNG"},
137 {RTPROT_ZSTATIC
, "static"},
140 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
142 {AF_BRIDGE
, "bridge"},
143 {RTNL_FAMILY_IPMR
, "ipv4MR"},
144 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
147 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
148 {RTN_UNICAST
, "unicast"},
149 {RTN_LOCAL
, "local"},
150 {RTN_BROADCAST
, "broadcast"},
151 {RTN_ANYCAST
, "anycast"},
152 {RTN_MULTICAST
, "multicast"},
153 {RTN_BLACKHOLE
, "blackhole"},
154 {RTN_UNREACHABLE
, "unreachable"},
155 {RTN_PROHIBIT
, "prohibited"},
156 {RTN_THROW
, "throw"},
158 {RTN_XRESOLVE
, "resolver"},
161 extern struct thread_master
*master
;
163 extern struct zebra_privs_t zserv_privs
;
165 DEFINE_MTYPE_STATIC(ZEBRA
, NL_BUF
, "Zebra Netlink buffers");
167 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
168 * We have both the main and dplane pthreads using these structs, so we have
169 * to protect the hash with a lock.
171 static struct hash
*nlsock_hash
;
172 pthread_mutex_t nlsock_mutex
;
174 /* Lock and unlock wrappers for nlsock hash */
175 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
176 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
178 size_t nl_batch_tx_bufsize
;
179 char *nl_batch_tx_buf
;
181 _Atomic
uint32_t nl_batch_bufsize
= NL_DEFAULT_BATCH_BUFSIZE
;
182 _Atomic
uint32_t nl_batch_send_threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
193 const struct zebra_dplane_info
*zns
;
195 struct dplane_ctx_q ctx_list
;
198 * Pointer to the queue of completed contexts outbound back
199 * towards the dataplane module.
201 struct dplane_ctx_q
*ctx_out_q
;
204 int netlink_config_write_helper(struct vty
*vty
)
207 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
208 uint32_t threshold
= atomic_load_explicit(&nl_batch_send_threshold
,
209 memory_order_relaxed
);
211 if (size
!= NL_DEFAULT_BATCH_BUFSIZE
212 || threshold
!= NL_DEFAULT_BATCH_SEND_THRESHOLD
)
213 vty_out(vty
, "zebra kernel netlink batch-tx-buf %u %u\n", size
,
216 if (if_netlink_frr_protodown_r_bit_is_set())
217 vty_out(vty
, "zebra protodown reason-bit %u\n",
218 if_netlink_get_frr_protodown_r_bit());
223 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
226 size
= NL_DEFAULT_BATCH_BUFSIZE
;
227 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
230 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
231 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
232 memory_order_relaxed
);
235 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
238 * This is an error condition that must be handled during
241 * The netlink_talk_filter function is used for communication
242 * down the netlink_cmd pipe and we are expecting
243 * an ack being received. So if we get here
244 * then we did not receive the ack and instead
245 * received some other message in an unexpected
248 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
249 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
253 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
256 socklen_t newlen
= sizeof(newsize
);
257 socklen_t oldlen
= sizeof(oldsize
);
260 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
262 flog_err_sys(EC_LIB_SOCKET
,
263 "Can't get %s receive buffer size: %s", nl
->name
,
264 safe_strerror(errno
));
268 /* Try force option (linux >= 2.6.14) and fall back to normal set */
269 frr_with_privs(&zserv_privs
) {
270 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
271 &rcvbufsize
, sizeof(rcvbufsize
));
274 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
277 flog_err_sys(EC_LIB_SOCKET
,
278 "Can't set %s receive buffer size: %s", nl
->name
,
279 safe_strerror(errno
));
283 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
285 flog_err_sys(EC_LIB_SOCKET
,
286 "Can't get %s receive buffer size: %s", nl
->name
,
287 safe_strerror(errno
));
293 /* Make socket for Linux netlink interface. */
294 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
295 unsigned long ext_groups
, ns_id_t ns_id
)
298 struct sockaddr_nl snl
;
302 frr_with_privs(&zserv_privs
) {
303 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
305 zlog_err("Can't open %s socket: %s", nl
->name
,
306 safe_strerror(errno
));
310 memset(&snl
, 0, sizeof(snl
));
311 snl
.nl_family
= AF_NETLINK
;
312 snl
.nl_groups
= groups
;
314 #if defined SOL_NETLINK
316 ret
= setsockopt(sock
, SOL_NETLINK
,
317 NETLINK_ADD_MEMBERSHIP
, &ext_groups
,
321 "can't setsockopt NETLINK_ADD_MEMBERSHIP: %s(%d)",
322 safe_strerror(errno
), errno
);
327 /* Bind the socket to the netlink structure for anything. */
328 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
332 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
333 snl
.nl_groups
, safe_strerror(errno
));
338 /* multiple netlink sockets will have different nl_pid */
339 namelen
= sizeof(snl
);
340 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
341 if (ret
< 0 || namelen
!= sizeof(snl
)) {
342 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
343 nl
->name
, safe_strerror(errno
));
350 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
351 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
357 * Dispatch an incoming netlink message; used by the zebra main pthread's
358 * netlink event reader.
360 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
364 * When we handle new message types here
365 * because we are starting to install them
366 * then lets check the netlink_install_filter
367 * and see if we should add the corresponding
368 * allow through entry there.
369 * Probably not needed to do but please
372 switch (h
->nlmsg_type
) {
374 return netlink_route_change(h
, ns_id
, startup
);
376 return netlink_route_change(h
, ns_id
, startup
);
378 return netlink_link_change(h
, ns_id
, startup
);
380 return netlink_link_change(h
, ns_id
, startup
);
384 return netlink_neigh_change(h
, ns_id
);
386 return netlink_rule_change(h
, ns_id
, startup
);
388 return netlink_rule_change(h
, ns_id
, startup
);
390 return netlink_nexthop_change(h
, ns_id
, startup
);
392 return netlink_nexthop_change(h
, ns_id
, startup
);
394 /* Messages handled in the dplane thread */
405 * If we have received this message then
406 * we have made a mistake during development
407 * and we need to write some code to handle
408 * this message type or not ask for
409 * it to be sent up to us
411 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
412 "Unknown netlink nlmsg_type %s(%d) vrf %u",
413 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
421 * Dispatch an incoming netlink message; used by the dataplane pthread's
422 * netlink event reader code.
424 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
428 * Dispatch the incoming messages that the dplane pthread handles
430 switch (h
->nlmsg_type
) {
433 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
437 return netlink_netconf_change(h
, ns_id
, startup
);
439 /* TODO -- other messages for the dplane socket and pthread */
451 static void kernel_read(struct thread
*thread
)
453 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
454 struct zebra_dplane_info dp_info
;
456 /* Capture key info from ns struct */
457 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
459 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
462 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
467 * Called by the dplane pthread to read incoming OS messages and dispatch them.
469 int kernel_dplane_read(struct zebra_dplane_info
*info
)
471 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
473 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
480 * Filter out messages from self that occur on listener socket,
481 * caused by our actions on the command socket(s)
483 * When we add new Netlink message types we probably
484 * do not need to add them here as that we are filtering
485 * on the routes we actually care to receive( which is rarer
486 * then the normal course of operations). We are intentionally
487 * allowing some messages from ourselves through
488 * ( I'm looking at you Interface based netlink messages )
489 * so that we only have to write one way to handle incoming
490 * address add/delete and xxxNETCONF changes.
492 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
495 * BPF_JUMP instructions and where you jump to are based upon
496 * 0 as being the next statement. So count from 0. Writing
497 * this down because every time I look at this I have to
500 struct sock_filter filter
[] = {
503 * if (nlmsg_pid == pid ||
504 * nlmsg_pid == dplane_pid) {
505 * if (the incoming nlmsg_type ==
506 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
512 * keep this netlink message
515 * 0: Load the nlmsg_pid into the BPF register
517 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
518 offsetof(struct nlmsghdr
, nlmsg_pid
)),
522 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
524 * 2: Compare to dplane pid
526 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
528 * 3: Load the nlmsg_type into BPF register
530 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
531 offsetof(struct nlmsghdr
, nlmsg_type
)),
533 * 4: Compare to RTM_NEWADDR
535 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
537 * 5: Compare to RTM_DELADDR
539 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
541 * 6: Compare to RTM_NEWNETCONF
543 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
546 * 7: Compare to RTM_DELNETCONF
548 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
551 * 8: This is the end state of we want to skip the
554 BPF_STMT(BPF_RET
| BPF_K
, 0),
555 /* 9: This is the end state of we want to keep
558 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
561 struct sock_fprog prog
= {
562 .len
= array_size(filter
), .filter
= filter
,
565 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
567 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
568 safe_strerror(errno
));
571 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
572 int len
, unsigned short flags
)
576 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
577 while (RTA_OK(rta
, len
)) {
578 type
= rta
->rta_type
& ~flags
;
579 if ((type
<= max
) && (!tb
[type
]))
581 rta
= RTA_NEXT(rta
, len
);
585 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
588 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
589 while (RTA_OK(rta
, len
)) {
590 if (rta
->rta_type
<= max
)
591 tb
[rta
->rta_type
] = rta
;
592 rta
= RTA_NEXT(rta
, len
);
597 * netlink_parse_rtattr_nested() - Parses a nested route attribute
598 * @tb: Pointer to array for storing rtattr in.
599 * @max: Max number to store.
600 * @rta: Pointer to rtattr to look for nested items in.
602 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
605 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
608 bool nl_addraw_l(struct nlmsghdr
*n
, unsigned int maxlen
, const void *data
,
611 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
612 zlog_err("ERROR message exceeded bound of %d", maxlen
);
616 memcpy(NLMSG_TAIL(n
), data
, len
);
617 memset((uint8_t *)NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
618 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
623 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
624 const void *data
, unsigned int alen
)
629 len
= RTA_LENGTH(alen
);
631 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
634 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
635 rta
->rta_type
= type
;
639 memcpy(RTA_DATA(rta
), data
, alen
);
643 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
648 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
651 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
654 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
657 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
660 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
663 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
666 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
668 struct rtattr
*nest
= NLMSG_TAIL(n
);
670 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
673 nest
->rta_type
|= NLA_F_NESTED
;
677 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
679 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
683 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
685 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
687 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
691 memset(rtnh
, 0, sizeof(struct rtnexthop
));
693 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
698 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
700 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
703 bool nl_rta_put(struct rtattr
*rta
, unsigned int maxlen
, int type
,
704 const void *data
, int alen
)
706 struct rtattr
*subrta
;
707 int len
= RTA_LENGTH(alen
);
709 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
710 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
714 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
715 subrta
->rta_type
= type
;
716 subrta
->rta_len
= len
;
718 memcpy(RTA_DATA(subrta
), data
, alen
);
719 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
724 bool nl_rta_put16(struct rtattr
*rta
, unsigned int maxlen
, int type
,
727 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint16_t));
730 bool nl_rta_put64(struct rtattr
*rta
, unsigned int maxlen
, int type
,
733 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint64_t));
736 struct rtattr
*nl_rta_nest(struct rtattr
*rta
, unsigned int maxlen
, int type
)
738 struct rtattr
*nest
= RTA_TAIL(rta
);
740 if (nl_rta_put(rta
, maxlen
, type
, NULL
, 0))
743 nest
->rta_type
|= NLA_F_NESTED
;
748 int nl_rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
750 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
755 const char *nl_msg_type_to_str(uint16_t msg_type
)
757 return lookup_msg(nlmsg_str
, msg_type
, "");
760 const char *nl_rtproto_to_str(uint8_t rtproto
)
762 return lookup_msg(rtproto_str
, rtproto
, "");
765 const char *nl_family_to_str(uint8_t family
)
767 return lookup_msg(family_str
, family
, "");
770 const char *nl_rttype_to_str(uint8_t rttype
)
772 return lookup_msg(rttype_str
, rttype
, "");
775 #define NLA_OK(nla, len) \
776 ((len) >= (int)sizeof(struct nlattr) \
777 && (nla)->nla_len >= sizeof(struct nlattr) \
778 && (nla)->nla_len <= (len))
779 #define NLA_NEXT(nla, attrlen) \
780 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
781 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
782 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
783 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
785 #define ERR_NLA(err, inner_len) \
786 ((struct nlattr *)(((char *)(err)) \
787 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
788 + NLMSG_ALIGN((inner_len))))
790 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
791 struct nlattr
*nla
, int len
)
793 while (NLA_OK(nla
, len
)) {
794 if (nla
->nla_type
<= max
)
795 tb
[nla
->nla_type
] = nla
;
796 nla
= NLA_NEXT(nla
, len
);
800 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
802 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
803 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
804 const struct nlmsghdr
*err_nlh
= NULL
;
805 /* Length not including nlmsghdr */
807 /* Inner error netlink message length */
808 uint32_t inner_len
= 0;
809 const char *msg
= NULL
;
812 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
813 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
815 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
817 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
820 if (tb
[NLMSGERR_ATTR_MSG
])
821 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
823 if (tb
[NLMSGERR_ATTR_OFFS
]) {
824 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
826 if (off
> h
->nlmsg_len
) {
827 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
828 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
830 * Header of failed message
831 * we are not doing anything currently with it
832 * but noticing it for later.
835 zlog_debug("%s: Received %s extended Ack", __func__
,
836 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
840 if (msg
&& *msg
!= '\0') {
841 bool is_err
= !!err
->error
;
844 zlog_err("Extended Error: %s", msg
);
846 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
847 "Extended Warning: %s", msg
);
852 * netlink_send_msg - send a netlink message of a certain size.
854 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
856 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
859 struct sockaddr_nl snl
= {};
860 struct iovec iov
= {};
861 struct msghdr msg
= {};
866 iov
.iov_len
= buflen
;
868 msg
.msg_namelen
= sizeof(snl
);
872 snl
.nl_family
= AF_NETLINK
;
874 /* Send message to netlink interface. */
875 frr_with_privs(&zserv_privs
) {
876 status
= sendmsg(nl
->sock
, &msg
, 0);
880 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
881 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
883 nl_dump(buf
, buflen
);
885 zlog_hexdump(buf
, buflen
);
886 #endif /* NETLINK_DEBUG */
890 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
891 safe_strerror(save_errno
));
899 * netlink_recv_msg - receive a netlink message.
901 * Returns -1 on error, 0 if read would block or the number of bytes received.
903 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
908 iov
.iov_base
= nl
->buf
;
909 iov
.iov_len
= nl
->buflen
;
916 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
918 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
919 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
921 iov
.iov_base
= nl
->buf
;
922 iov
.iov_len
= nl
->buflen
;
925 status
= recvmsg(nl
->sock
, msg
, 0);
926 } while (status
== -1 && errno
== EINTR
);
929 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
931 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
932 nl
->name
, safe_strerror(errno
));
934 * In this case we are screwed. There is no good way to recover
935 * zebra at this point.
941 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
945 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
946 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
947 "%s sender address length error: length %d", nl
->name
,
952 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
953 zlog_debug("%s: << netlink message dump [recv]", __func__
);
955 nl_dump(nl
->buf
, status
);
957 zlog_hexdump(nl
->buf
, status
);
958 #endif /* NETLINK_DEBUG */
965 * netlink_parse_error - parse a netlink error message
967 * Returns 1 if this message is acknowledgement, 0 if this error should be
968 * ignored, -1 otherwise.
970 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
971 bool is_cmd
, bool startup
)
973 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
974 int errnum
= err
->error
;
975 int msg_type
= err
->msg
.nlmsg_type
;
977 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
978 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
979 "%s error: message truncated", nl
->name
);
984 * Parse the extended information before we actually handle it. At this
985 * point in time we do not do anything other than report the issue.
987 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
988 netlink_parse_extended_ack(h
);
990 /* If the error field is zero, then this is an ACK. */
991 if (err
->error
== 0) {
992 if (IS_ZEBRA_DEBUG_KERNEL
) {
993 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
995 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
996 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
1003 /* Deal with errors that occur because of races in link handling. */
1005 && ((msg_type
== RTM_DELROUTE
1006 && (-errnum
== ENODEV
|| -errnum
== ESRCH
))
1007 || (msg_type
== RTM_NEWROUTE
1008 && (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)))) {
1009 if (IS_ZEBRA_DEBUG_KERNEL
)
1010 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1011 nl
->name
, safe_strerror(-errnum
),
1012 nl_msg_type_to_str(msg_type
), msg_type
,
1013 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1018 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1019 * link-local. The kernel should have already deleted the neighbor so
1020 * do not log these as an error.
1022 if (msg_type
== RTM_DELNEIGH
1023 || (is_cmd
&& msg_type
== RTM_NEWROUTE
1024 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
1026 * This is known to happen in some situations, don't log as
1029 if (IS_ZEBRA_DEBUG_KERNEL
)
1030 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1031 nl
->name
, safe_strerror(-errnum
),
1032 nl_msg_type_to_str(msg_type
), msg_type
,
1033 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1035 if ((msg_type
!= RTM_GETNEXTHOP
) || !startup
)
1036 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
1037 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1038 nl
->name
, safe_strerror(-errnum
),
1039 nl_msg_type_to_str(msg_type
), msg_type
,
1040 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1047 * netlink_parse_info
1049 * Receive message from netlink interface and pass those information
1050 * to the given function.
1052 * filter -> Function to call to read the results
1053 * nl -> netlink socket information
1054 * zns -> The zebra namespace data
1055 * count -> How many we should read in, 0 means as much as possible
1056 * startup -> Are we reading in under startup conditions? passed to
1059 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
1060 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
1061 int count
, bool startup
)
1069 struct sockaddr_nl snl
;
1070 struct msghdr msg
= {.msg_name
= (void *)&snl
,
1071 .msg_namelen
= sizeof(snl
)};
1074 if (count
&& read_in
>= count
)
1077 status
= netlink_recv_msg(nl
, &msg
);
1080 else if (status
== 0)
1084 for (h
= (struct nlmsghdr
*)nl
->buf
;
1085 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
1086 h
= NLMSG_NEXT(h
, status
)) {
1087 /* Finish of reading. */
1088 if (h
->nlmsg_type
== NLMSG_DONE
)
1091 /* Error handling. */
1092 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1093 int err
= netlink_parse_error(
1094 nl
, h
, zns
->is_cmd
, startup
);
1097 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1105 * What is the right thing to do? The kernel
1106 * is telling us that the dump request was interrupted
1107 * and we more than likely are out of luck and have
1108 * missed data from the kernel. At this point in time
1109 * lets just note that this is happening.
1111 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
1113 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
1114 "netlink recvmsg: The Dump request was interrupted");
1116 /* OK we got netlink message. */
1117 if (IS_ZEBRA_DEBUG_KERNEL
)
1119 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1121 nl_msg_type_to_str(h
->nlmsg_type
),
1122 h
->nlmsg_type
, h
->nlmsg_len
,
1123 h
->nlmsg_seq
, h
->nlmsg_pid
);
1127 * Ignore messages that maybe sent from
1128 * other actors besides the kernel
1130 if (snl
.nl_pid
!= 0) {
1131 zlog_debug("Ignoring message from pid %u",
1136 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1138 zlog_debug("%s filter function error",
1144 /* After error care. */
1145 if (msg
.msg_flags
& MSG_TRUNC
) {
1146 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1147 "%s error: message truncated", nl
->name
);
1151 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1152 "%s error: data remnant size %d", nl
->name
,
1163 * sendmsg() to netlink socket then recvmsg().
1164 * Calls netlink_parse_info to parse returned data
1166 * filter -> The filter to read final results from kernel
1167 * nlmsghdr -> The data to send to the kernel
1168 * dp_info -> The dataplane and netlink socket information
1169 * startup -> Are we reading in under startup conditions
1170 * This is passed through eventually to filter.
1172 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1175 struct zebra_dplane_info
*dp_info
, bool startup
)
1179 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1180 n
->nlmsg_seq
= dp_info
->seq
;
1181 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1183 if (IS_ZEBRA_DEBUG_KERNEL
)
1185 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1186 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1187 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1190 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1194 * Get reply from netlink socket.
1195 * The reply should either be an acknowlegement or an error.
1197 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1201 * Synchronous version of netlink_talk_info. Converts args to suit the
1202 * common version, which is suitable for both sync and async use.
1204 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1205 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1208 struct zebra_dplane_info dp_info
;
1210 /* Increment sequence number before capturing snapshot of ns socket
1215 /* Capture info in intermediate info struct */
1216 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1218 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1221 /* Issue request message to kernel via netlink socket. GET messages
1222 * are issued through this interface.
1224 int netlink_request(struct nlsock
*nl
, void *req
)
1226 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1228 /* Check netlink socket. */
1230 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1235 /* Fill common fields for all requests. */
1236 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1237 n
->nlmsg_seq
= ++nl
->seq
;
1239 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1245 static int nl_batch_read_resp(struct nl_batch
*bth
)
1248 struct sockaddr_nl snl
;
1249 struct msghdr msg
= {};
1252 struct zebra_dplane_ctx
*ctx
;
1255 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1257 msg
.msg_name
= (void *)&snl
;
1258 msg
.msg_namelen
= sizeof(snl
);
1261 * The responses are not batched, so we need to read and process one
1262 * message at a time.
1265 status
= netlink_recv_msg(nl
, &msg
);
1267 * status == -1 is a full on failure somewhere
1268 * since we don't know where the problem happened
1269 * we must mark all as failed
1271 * Else we mark everything as worked
1274 if (status
== -1 || status
== 0) {
1275 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1278 dplane_ctx_set_status(
1280 ZEBRA_DPLANE_REQUEST_FAILURE
);
1281 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1286 h
= (struct nlmsghdr
*)nl
->buf
;
1290 * Find the corresponding context object. Received responses are
1291 * in the same order as requests we sent, so we can simply
1292 * iterate over the context list and match responses with
1293 * requests at same time.
1296 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1299 * This is a situation where we have gotten
1300 * into a bad spot. We need to know that
1301 * this happens( does it? )
1304 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1310 * 'update' context objects take two consecutive
1313 if (dplane_ctx_is_update(ctx
) &&
1314 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1316 * This is the situation where we get a response
1317 * to a message that should be ignored.
1323 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1324 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1326 /* We have found corresponding context object. */
1327 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1330 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1332 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1334 dplane_ctx_get_ns(ctx
)->seq
);
1339 * If we ignore the message due to an update
1340 * above we should still fricking decode the
1341 * message for our operator to understand
1344 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1347 zlog_debug("%s: netlink error message seq=%d %d",
1348 __func__
, h
->nlmsg_seq
, err
);
1353 * We received a message with the sequence number that isn't
1354 * associated with any dplane context object.
1357 if (IS_ZEBRA_DEBUG_KERNEL
)
1359 "%s: skipping unassociated response, seq number %d NS %u",
1360 __func__
, h
->nlmsg_seq
,
1365 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1366 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1370 dplane_ctx_set_status(
1371 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1373 if (IS_ZEBRA_DEBUG_KERNEL
)
1374 zlog_debug("%s: netlink error message seq=%d ",
1375 __func__
, h
->nlmsg_seq
);
1380 * If we get here then we did not receive neither the ack nor
1381 * the error and instead received some other message in an
1384 if (IS_ZEBRA_DEBUG_KERNEL
)
1385 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1386 __func__
, h
->nlmsg_type
,
1387 nl_msg_type_to_str(h
->nlmsg_type
),
1394 static void nl_batch_reset(struct nl_batch
*bth
)
1396 bth
->buf_head
= bth
->buf
;
1401 TAILQ_INIT(&(bth
->ctx_list
));
1404 static void nl_batch_init(struct nl_batch
*bth
, struct dplane_ctx_q
*ctx_out_q
)
1407 * If the size of the buffer has changed, free and then allocate a new
1411 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1412 if (bufsize
!= nl_batch_tx_bufsize
) {
1413 if (nl_batch_tx_buf
)
1414 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1416 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1417 nl_batch_tx_bufsize
= bufsize
;
1420 bth
->buf
= nl_batch_tx_buf
;
1421 bth
->bufsiz
= bufsize
;
1422 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1423 memory_order_relaxed
);
1425 bth
->ctx_out_q
= ctx_out_q
;
1427 nl_batch_reset(bth
);
1430 static void nl_batch_send(struct nl_batch
*bth
)
1432 struct zebra_dplane_ctx
*ctx
;
1435 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1437 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1439 if (IS_ZEBRA_DEBUG_KERNEL
)
1440 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1441 __func__
, nl
->name
, bth
->curlen
,
1444 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1448 if (nl_batch_read_resp(bth
) == -1)
1453 /* Move remaining contexts to the outbound queue. */
1455 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1460 dplane_ctx_set_status(ctx
,
1461 ZEBRA_DPLANE_REQUEST_FAILURE
);
1463 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1466 nl_batch_reset(bth
);
1469 enum netlink_msg_status
netlink_batch_add_msg(
1470 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1471 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1476 struct nlmsghdr
*msgh
;
1479 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1482 * If there was an error while encoding the message (other than buffer
1483 * overflow) then return an error.
1486 return FRR_NETLINK_ERROR
;
1489 * If the message doesn't fit entirely in the buffer then send the batch
1494 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1495 bth
->bufsiz
- bth
->curlen
);
1497 * If the message doesn't fit in the empty buffer then just
1501 return FRR_NETLINK_ERROR
;
1504 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1505 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1510 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1511 msgh
->nlmsg_seq
= seq
;
1512 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1514 bth
->zns
= dplane_ctx_get_ns(ctx
);
1515 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1516 bth
->curlen
+= size
;
1519 return FRR_NETLINK_QUEUED
;
1522 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1523 struct zebra_dplane_ctx
*ctx
)
1525 if (dplane_ctx_is_skip_kernel(ctx
))
1526 return FRR_NETLINK_SUCCESS
;
1528 switch (dplane_ctx_get_op(ctx
)) {
1530 case DPLANE_OP_ROUTE_INSTALL
:
1531 case DPLANE_OP_ROUTE_UPDATE
:
1532 case DPLANE_OP_ROUTE_DELETE
:
1533 return netlink_put_route_update_msg(bth
, ctx
);
1535 case DPLANE_OP_NH_INSTALL
:
1536 case DPLANE_OP_NH_UPDATE
:
1537 case DPLANE_OP_NH_DELETE
:
1538 return netlink_put_nexthop_update_msg(bth
, ctx
);
1540 case DPLANE_OP_LSP_INSTALL
:
1541 case DPLANE_OP_LSP_UPDATE
:
1542 case DPLANE_OP_LSP_DELETE
:
1543 return netlink_put_lsp_update_msg(bth
, ctx
);
1545 case DPLANE_OP_PW_INSTALL
:
1546 case DPLANE_OP_PW_UNINSTALL
:
1547 return netlink_put_pw_update_msg(bth
, ctx
);
1549 case DPLANE_OP_ADDR_INSTALL
:
1550 case DPLANE_OP_ADDR_UNINSTALL
:
1551 return netlink_put_address_update_msg(bth
, ctx
);
1553 case DPLANE_OP_MAC_INSTALL
:
1554 case DPLANE_OP_MAC_DELETE
:
1555 return netlink_put_mac_update_msg(bth
, ctx
);
1557 case DPLANE_OP_NEIGH_INSTALL
:
1558 case DPLANE_OP_NEIGH_UPDATE
:
1559 case DPLANE_OP_NEIGH_DELETE
:
1560 case DPLANE_OP_VTEP_ADD
:
1561 case DPLANE_OP_VTEP_DELETE
:
1562 case DPLANE_OP_NEIGH_DISCOVER
:
1563 case DPLANE_OP_NEIGH_IP_INSTALL
:
1564 case DPLANE_OP_NEIGH_IP_DELETE
:
1565 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1566 return netlink_put_neigh_update_msg(bth
, ctx
);
1568 case DPLANE_OP_RULE_ADD
:
1569 case DPLANE_OP_RULE_DELETE
:
1570 case DPLANE_OP_RULE_UPDATE
:
1571 return netlink_put_rule_update_msg(bth
, ctx
);
1573 case DPLANE_OP_SYS_ROUTE_ADD
:
1574 case DPLANE_OP_SYS_ROUTE_DELETE
:
1575 case DPLANE_OP_ROUTE_NOTIFY
:
1576 case DPLANE_OP_LSP_NOTIFY
:
1577 case DPLANE_OP_BR_PORT_UPDATE
:
1578 return FRR_NETLINK_SUCCESS
;
1580 case DPLANE_OP_IPTABLE_ADD
:
1581 case DPLANE_OP_IPTABLE_DELETE
:
1582 case DPLANE_OP_IPSET_ADD
:
1583 case DPLANE_OP_IPSET_DELETE
:
1584 case DPLANE_OP_IPSET_ENTRY_ADD
:
1585 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1586 return FRR_NETLINK_ERROR
;
1588 case DPLANE_OP_GRE_SET
:
1589 return netlink_put_gre_set_msg(bth
, ctx
);
1591 case DPLANE_OP_INTF_ADDR_ADD
:
1592 case DPLANE_OP_INTF_ADDR_DEL
:
1593 case DPLANE_OP_INTF_NETCONFIG
:
1594 case DPLANE_OP_NONE
:
1595 return FRR_NETLINK_ERROR
;
1597 case DPLANE_OP_INTF_INSTALL
:
1598 case DPLANE_OP_INTF_UPDATE
:
1599 case DPLANE_OP_INTF_DELETE
:
1600 return netlink_put_intf_update_msg(bth
, ctx
);
1603 return FRR_NETLINK_ERROR
;
1606 void kernel_update_multi(struct dplane_ctx_q
*ctx_list
)
1608 struct nl_batch batch
;
1609 struct zebra_dplane_ctx
*ctx
;
1610 struct dplane_ctx_q handled_list
;
1611 enum netlink_msg_status res
;
1613 TAILQ_INIT(&handled_list
);
1614 nl_batch_init(&batch
, &handled_list
);
1617 ctx
= dplane_ctx_dequeue(ctx_list
);
1621 if (batch
.zns
!= NULL
1622 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1623 nl_batch_send(&batch
);
1626 * Assume all messages will succeed and then mark only the ones
1629 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1631 res
= nl_put_msg(&batch
, ctx
);
1633 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1634 if (res
== FRR_NETLINK_ERROR
)
1635 dplane_ctx_set_status(ctx
,
1636 ZEBRA_DPLANE_REQUEST_FAILURE
);
1638 if (batch
.curlen
> batch
.limit
)
1639 nl_batch_send(&batch
);
1642 nl_batch_send(&batch
);
1644 TAILQ_INIT(ctx_list
);
1645 dplane_ctx_list_append(ctx_list
, &handled_list
);
1648 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1650 struct nlsock lookup
, *retval
;
1655 retval
= hash_lookup(nlsock_hash
, &lookup
);
1661 /* Insert nlsock entry into hash */
1662 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1665 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1669 /* Remove nlsock entry from hash */
1670 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1673 (void)hash_release(nlsock_hash
, nls
);
1677 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1679 const struct nlsock
*nl
= arg
;
1684 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1686 const struct nlsock
*nl1
= arg1
;
1687 const struct nlsock
*nl2
= arg2
;
1689 if (nl1
->sock
== nl2
->sock
)
1695 /* Exported interface function. This function simply calls
1696 netlink_socket (). */
1697 void kernel_init(struct zebra_ns
*zns
)
1699 uint32_t groups
, dplane_groups
, ext_groups
;
1700 #if defined SOL_NETLINK
1705 * Initialize netlink sockets
1707 * If RTMGRP_XXX exists use that, but at some point
1708 * I think the kernel developers realized that
1709 * keeping track of all the different values would
1710 * lead to confusion, so we need to convert the
1711 * RTNLGRP_XXX to a bit position for ourself
1713 groups
= RTMGRP_LINK
|
1715 RTMGRP_IPV4_IFADDR
|
1717 RTMGRP_IPV6_IFADDR
|
1718 RTMGRP_IPV4_MROUTE
|
1720 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1721 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1722 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1));
1724 dplane_groups
= (RTMGRP_LINK
|
1725 RTMGRP_IPV4_IFADDR
|
1726 RTMGRP_IPV6_IFADDR
|
1727 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1728 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1729 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1731 /* Use setsockopt for > 31 group */
1732 ext_groups
= RTNLGRP_TUNNEL
;
1734 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1735 "netlink-listen (NS %u)", zns
->ns_id
);
1736 zns
->netlink
.sock
= -1;
1737 if (netlink_socket(&zns
->netlink
, groups
, ext_groups
, zns
->ns_id
) < 0) {
1738 zlog_err("Failure to create %s socket",
1743 kernel_netlink_nlsock_insert(&zns
->netlink
);
1745 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1746 "netlink-cmd (NS %u)", zns
->ns_id
);
1747 zns
->netlink_cmd
.sock
= -1;
1748 if (netlink_socket(&zns
->netlink_cmd
, 0, 0, zns
->ns_id
) < 0) {
1749 zlog_err("Failure to create %s socket",
1750 zns
->netlink_cmd
.name
);
1754 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1756 /* Outbound socket for dplane programming of the host OS. */
1757 snprintf(zns
->netlink_dplane_out
.name
,
1758 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1760 zns
->netlink_dplane_out
.sock
= -1;
1761 if (netlink_socket(&zns
->netlink_dplane_out
, 0, 0, zns
->ns_id
) < 0) {
1762 zlog_err("Failure to create %s socket",
1763 zns
->netlink_dplane_out
.name
);
1767 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1769 /* Inbound socket for OS events coming to the dplane. */
1770 snprintf(zns
->netlink_dplane_in
.name
,
1771 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1773 zns
->netlink_dplane_in
.sock
= -1;
1774 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
, 0,
1776 zlog_err("Failure to create %s socket",
1777 zns
->netlink_dplane_in
.name
);
1781 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1784 * SOL_NETLINK is not available on all platforms yet
1785 * apparently. It's in bits/socket.h which I am not
1786 * sure that we want to pull into our build system.
1788 #if defined SOL_NETLINK
1790 * Let's tell the kernel that we want to receive extended
1791 * ACKS over our command socket(s)
1794 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1798 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1799 errno
, safe_strerror(errno
));
1802 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1803 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1806 zlog_notice("Registration for extended dp ACK failed : %d %s",
1807 errno
, safe_strerror(errno
));
1810 * Trim off the payload of the original netlink message in the
1811 * acknowledgment. This option is available since Linux 4.2, so if
1812 * setsockopt fails, ignore the error.
1815 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1816 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1819 "Registration for reduced ACK packet size failed, probably running an early kernel");
1822 /* Register kernel socket. */
1823 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1824 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1825 zns
->netlink
.name
, safe_strerror(errno
));
1827 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1828 zlog_err("Can't set %s socket error: %s(%d)",
1829 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1831 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1832 zlog_err("Can't set %s socket error: %s(%d)",
1833 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1836 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1837 zlog_err("Can't set %s socket error: %s(%d)",
1838 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1841 /* Set receive buffer size if it's set from command line */
1843 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1844 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1845 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1846 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1849 /* Set filter for inbound sockets, to exclude events we've generated
1852 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1853 zns
->netlink_dplane_out
.snl
.nl_pid
);
1855 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1856 zns
->netlink_cmd
.snl
.nl_pid
,
1857 zns
->netlink_dplane_out
.snl
.nl_pid
);
1859 zns
->t_netlink
= NULL
;
1861 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1862 zns
->netlink
.sock
, &zns
->t_netlink
);
1867 /* Helper to clean up an nlsock */
1868 static void kernel_nlsock_fini(struct nlsock
*nls
)
1870 if (nls
&& nls
->sock
>= 0) {
1871 kernel_netlink_nlsock_remove(nls
);
1874 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1879 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1881 thread_cancel(&zns
->t_netlink
);
1883 kernel_nlsock_fini(&zns
->netlink
);
1885 kernel_nlsock_fini(&zns
->netlink_cmd
);
1887 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1889 /* During zebra shutdown, we need to leave the dataplane socket
1890 * around until all work is done.
1893 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1897 * Global init for platform-/OS-specific things
1899 void kernel_router_init(void)
1901 /* Init nlsock hash and lock */
1902 pthread_mutex_init(&nlsock_mutex
, NULL
);
1903 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
1904 kernel_netlink_nlsock_hash_equal
,
1905 "Netlink Socket Hash");
1909 * Global deinit for platform-/OS-specific things
1911 void kernel_router_terminate(void)
1913 pthread_mutex_destroy(&nlsock_mutex
);
1915 hash_free(nlsock_hash
);
1919 #endif /* HAVE_NETLINK */