1 /* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connected.h"
38 #include "lib_errors.h"
41 #include "zebra/zebra_router.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
45 #include "zebra/debug.h"
46 #include "zebra/kernel_netlink.h"
47 #include "zebra/rt_netlink.h"
48 #include "zebra/if_netlink.h"
49 #include "zebra/rule_netlink.h"
50 #include "zebra/tc_netlink.h"
51 #include "zebra/netconf_netlink.h"
52 #include "zebra/zebra_errors.h"
54 #ifndef SO_RCVBUFFORCE
55 #define SO_RCVBUFFORCE (33)
58 /* Hack for GNU libc version 2. */
60 #define MSG_TRUNC 0x20
61 #endif /* MSG_TRUNC */
64 #define NLMSG_TAIL(nmsg) \
65 ((struct rtattr *)(((uint8_t *)(nmsg)) \
66 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
70 #define RTA_TAIL(rta) \
71 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
74 #ifndef RTNL_FAMILY_IP6MR
75 #define RTNL_FAMILY_IP6MR 129
78 #ifndef RTPROT_MROUTED
79 #define RTPROT_MROUTED 17
82 #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
85 * We limit the batch's size to a number smaller than the length of the
86 * underlying buffer since the last message that wouldn't fit the batch would go
87 * over the upper boundary and then it would have to be encoded again into a new
88 * buffer. If the difference between the limit and the length of the buffer is
89 * big enough (bigger than the biggest Netlink message) then this situation
92 #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
94 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
95 {RTM_DELROUTE
, "RTM_DELROUTE"},
96 {RTM_GETROUTE
, "RTM_GETROUTE"},
97 {RTM_NEWLINK
, "RTM_NEWLINK"},
98 {RTM_SETLINK
, "RTM_SETLINK"},
99 {RTM_DELLINK
, "RTM_DELLINK"},
100 {RTM_GETLINK
, "RTM_GETLINK"},
101 {RTM_NEWADDR
, "RTM_NEWADDR"},
102 {RTM_DELADDR
, "RTM_DELADDR"},
103 {RTM_GETADDR
, "RTM_GETADDR"},
104 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
105 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
106 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
107 {RTM_NEWRULE
, "RTM_NEWRULE"},
108 {RTM_DELRULE
, "RTM_DELRULE"},
109 {RTM_GETRULE
, "RTM_GETRULE"},
110 {RTM_NEWNEXTHOP
, "RTM_NEWNEXTHOP"},
111 {RTM_DELNEXTHOP
, "RTM_DELNEXTHOP"},
112 {RTM_GETNEXTHOP
, "RTM_GETNEXTHOP"},
113 {RTM_NEWNETCONF
, "RTM_NEWNETCONF"},
114 {RTM_DELNETCONF
, "RTM_DELNETCONF"},
115 {RTM_NEWTUNNEL
, "RTM_NEWTUNNEL"},
116 {RTM_DELTUNNEL
, "RTM_DELTUNNEL"},
117 {RTM_GETTUNNEL
, "RTM_GETTUNNEL"},
118 {RTM_NEWQDISC
, "RTM_NEWQDISC"},
119 {RTM_DELQDISC
, "RTM_DELQDISC"},
120 {RTM_GETQDISC
, "RTM_GETQDISC"},
121 {RTM_NEWTCLASS
, "RTM_NEWTCLASS"},
122 {RTM_DELTCLASS
, "RTM_DELTCLASS"},
123 {RTM_GETTCLASS
, "RTM_GETTCLASS"},
124 {RTM_NEWTFILTER
, "RTM_NEWTFILTER"},
125 {RTM_DELTFILTER
, "RTM_DELTFILTER"},
126 {RTM_GETTFILTER
, "RTM_GETTFILTER"},
127 {RTM_NEWVLAN
, "RTM_NEWVLAN"},
128 {RTM_DELVLAN
, "RTM_DELVLAN"},
129 {RTM_GETVLAN
, "RTM_GETVLAN"},
132 static const struct message rtproto_str
[] = {
133 {RTPROT_REDIRECT
, "redirect"},
134 {RTPROT_KERNEL
, "kernel"},
135 {RTPROT_BOOT
, "boot"},
136 {RTPROT_STATIC
, "static"},
137 {RTPROT_GATED
, "GateD"},
138 {RTPROT_RA
, "router advertisement"},
140 {RTPROT_ZEBRA
, "Zebra"},
142 {RTPROT_BIRD
, "BIRD"},
143 #endif /* RTPROT_BIRD */
144 {RTPROT_MROUTED
, "mroute"},
146 {RTPROT_OSPF
, "OSPF"},
147 {RTPROT_ISIS
, "IS-IS"},
149 {RTPROT_RIPNG
, "RIPNG"},
150 {RTPROT_ZSTATIC
, "static"},
153 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
155 {AF_BRIDGE
, "bridge"},
156 {RTNL_FAMILY_IPMR
, "ipv4MR"},
157 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
160 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
161 {RTN_UNICAST
, "unicast"},
162 {RTN_LOCAL
, "local"},
163 {RTN_BROADCAST
, "broadcast"},
164 {RTN_ANYCAST
, "anycast"},
165 {RTN_MULTICAST
, "multicast"},
166 {RTN_BLACKHOLE
, "blackhole"},
167 {RTN_UNREACHABLE
, "unreachable"},
168 {RTN_PROHIBIT
, "prohibited"},
169 {RTN_THROW
, "throw"},
171 {RTN_XRESOLVE
, "resolver"},
174 extern struct thread_master
*master
;
176 extern struct zebra_privs_t zserv_privs
;
178 DEFINE_MTYPE_STATIC(ZEBRA
, NL_BUF
, "Zebra Netlink buffers");
180 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
181 * We have both the main and dplane pthreads using these structs, so we have
182 * to protect the hash with a lock.
184 static struct hash
*nlsock_hash
;
185 pthread_mutex_t nlsock_mutex
;
187 /* Lock and unlock wrappers for nlsock hash */
188 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
189 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
191 size_t nl_batch_tx_bufsize
;
192 char *nl_batch_tx_buf
;
194 _Atomic
uint32_t nl_batch_bufsize
= NL_DEFAULT_BATCH_BUFSIZE
;
195 _Atomic
uint32_t nl_batch_send_threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
206 const struct zebra_dplane_info
*zns
;
208 struct dplane_ctx_list_head ctx_list
;
211 * Pointer to the queue of completed contexts outbound back
212 * towards the dataplane module.
214 struct dplane_ctx_list_head
*ctx_out_q
;
217 int netlink_config_write_helper(struct vty
*vty
)
220 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
221 uint32_t threshold
= atomic_load_explicit(&nl_batch_send_threshold
,
222 memory_order_relaxed
);
224 if (size
!= NL_DEFAULT_BATCH_BUFSIZE
225 || threshold
!= NL_DEFAULT_BATCH_SEND_THRESHOLD
)
226 vty_out(vty
, "zebra kernel netlink batch-tx-buf %u %u\n", size
,
229 if (if_netlink_frr_protodown_r_bit_is_set())
230 vty_out(vty
, "zebra protodown reason-bit %u\n",
231 if_netlink_get_frr_protodown_r_bit());
236 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
239 size
= NL_DEFAULT_BATCH_BUFSIZE
;
240 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
243 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
244 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
245 memory_order_relaxed
);
248 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
251 * This is an error condition that must be handled during
254 * The netlink_talk_filter function is used for communication
255 * down the netlink_cmd pipe and we are expecting
256 * an ack being received. So if we get here
257 * then we did not receive the ack and instead
258 * received some other message in an unexpected
261 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
262 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
266 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
269 socklen_t newlen
= sizeof(newsize
);
270 socklen_t oldlen
= sizeof(oldsize
);
273 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
275 flog_err_sys(EC_LIB_SOCKET
,
276 "Can't get %s receive buffer size: %s", nl
->name
,
277 safe_strerror(errno
));
281 /* Try force option (linux >= 2.6.14) and fall back to normal set */
282 frr_with_privs(&zserv_privs
) {
283 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
284 &rcvbufsize
, sizeof(rcvbufsize
));
287 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
290 flog_err_sys(EC_LIB_SOCKET
,
291 "Can't set %s receive buffer size: %s", nl
->name
,
292 safe_strerror(errno
));
296 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
298 flog_err_sys(EC_LIB_SOCKET
,
299 "Can't get %s receive buffer size: %s", nl
->name
,
300 safe_strerror(errno
));
306 static const char *group2str(uint32_t group
)
310 return "RTNLGRP_TUNNEL";
316 /* Make socket for Linux netlink interface. */
317 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
318 uint32_t ext_groups
[], uint8_t ext_group_size
,
322 struct sockaddr_nl snl
;
326 frr_with_privs(&zserv_privs
) {
327 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
329 zlog_err("Can't open %s socket: %s", nl
->name
,
330 safe_strerror(errno
));
334 memset(&snl
, 0, sizeof(snl
));
335 snl
.nl_family
= AF_NETLINK
;
336 snl
.nl_groups
= groups
;
338 if (ext_group_size
) {
341 for (i
= 0; i
< ext_group_size
; i
++) {
342 #if defined SOL_NETLINK
343 ret
= setsockopt(sock
, SOL_NETLINK
,
344 NETLINK_ADD_MEMBERSHIP
,
346 sizeof(ext_groups
[i
]));
349 "can't setsockopt NETLINK_ADD_MEMBERSHIP for group %s(%u), this linux kernel does not support it: %s(%d)",
350 group2str(ext_groups
[i
]),
352 safe_strerror(errno
), errno
);
356 "Unable to use NETLINK_ADD_MEMBERSHIP via SOL_NETLINK for %s(%u) since the linux kernel does not support the socket option",
357 group2str(ext_groups
[i
]),
363 /* Bind the socket to the netlink structure for anything. */
364 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
368 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
369 snl
.nl_groups
, safe_strerror(errno
));
374 /* multiple netlink sockets will have different nl_pid */
375 namelen
= sizeof(snl
);
376 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
377 if (ret
< 0 || namelen
!= sizeof(snl
)) {
378 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
379 nl
->name
, safe_strerror(errno
));
386 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
387 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
393 * Dispatch an incoming netlink message; used by the zebra main pthread's
394 * netlink event reader.
396 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
400 * When we handle new message types here
401 * because we are starting to install them
402 * then lets check the netlink_install_filter
403 * and see if we should add the corresponding
404 * allow through entry there.
405 * Probably not needed to do but please
408 switch (h
->nlmsg_type
) {
410 return netlink_route_change(h
, ns_id
, startup
);
412 return netlink_route_change(h
, ns_id
, startup
);
414 return netlink_link_change(h
, ns_id
, startup
);
416 return netlink_link_change(h
, ns_id
, startup
);
420 return netlink_neigh_change(h
, ns_id
);
422 return netlink_rule_change(h
, ns_id
, startup
);
424 return netlink_rule_change(h
, ns_id
, startup
);
426 return netlink_nexthop_change(h
, ns_id
, startup
);
428 return netlink_nexthop_change(h
, ns_id
, startup
);
431 return netlink_qdisc_change(h
, ns_id
, startup
);
434 return netlink_tclass_change(h
, ns_id
, startup
);
437 return netlink_tfilter_change(h
, ns_id
, startup
);
439 return netlink_vlan_change(h
, ns_id
, startup
);
441 return netlink_vlan_change(h
, ns_id
, startup
);
443 /* Messages handled in the dplane thread */
454 * If we have received this message then
455 * we have made a mistake during development
456 * and we need to write some code to handle
457 * this message type or not ask for
458 * it to be sent up to us
460 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
461 "Unknown netlink nlmsg_type %s(%d) vrf %u",
462 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
470 * Dispatch an incoming netlink message; used by the dataplane pthread's
471 * netlink event reader code.
473 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
477 * Dispatch the incoming messages that the dplane pthread handles
479 switch (h
->nlmsg_type
) {
482 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
486 return netlink_netconf_change(h
, ns_id
, startup
);
488 /* TODO -- other messages for the dplane socket and pthread */
500 static void kernel_read(struct thread
*thread
)
502 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
503 struct zebra_dplane_info dp_info
;
505 /* Capture key info from ns struct */
506 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
508 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
511 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
516 * Called by the dplane pthread to read incoming OS messages and dispatch them.
518 int kernel_dplane_read(struct zebra_dplane_info
*info
)
520 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
522 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
529 * Filter out messages from self that occur on listener socket,
530 * caused by our actions on the command socket(s)
532 * When we add new Netlink message types we probably
533 * do not need to add them here as that we are filtering
534 * on the routes we actually care to receive( which is rarer
535 * then the normal course of operations). We are intentionally
536 * allowing some messages from ourselves through
537 * ( I'm looking at you Interface based netlink messages )
538 * so that we only have to write one way to handle incoming
539 * address add/delete and xxxNETCONF changes.
541 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
544 * BPF_JUMP instructions and where you jump to are based upon
545 * 0 as being the next statement. So count from 0. Writing
546 * this down because every time I look at this I have to
549 struct sock_filter filter
[] = {
552 * if (nlmsg_pid == pid ||
553 * nlmsg_pid == dplane_pid) {
554 * if (the incoming nlmsg_type ==
555 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
561 * keep this netlink message
564 * 0: Load the nlmsg_pid into the BPF register
566 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
567 offsetof(struct nlmsghdr
, nlmsg_pid
)),
571 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
573 * 2: Compare to dplane pid
575 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
577 * 3: Load the nlmsg_type into BPF register
579 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
580 offsetof(struct nlmsghdr
, nlmsg_type
)),
582 * 4: Compare to RTM_NEWADDR
584 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
586 * 5: Compare to RTM_DELADDR
588 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
590 * 6: Compare to RTM_NEWNETCONF
592 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
595 * 7: Compare to RTM_DELNETCONF
597 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
600 * 8: This is the end state of we want to skip the
603 BPF_STMT(BPF_RET
| BPF_K
, 0),
604 /* 9: This is the end state of we want to keep
607 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
610 struct sock_fprog prog
= {
611 .len
= array_size(filter
), .filter
= filter
,
614 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
616 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
617 safe_strerror(errno
));
620 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
621 int len
, unsigned short flags
)
625 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
626 while (RTA_OK(rta
, len
)) {
627 type
= rta
->rta_type
& ~flags
;
628 if ((type
<= max
) && (!tb
[type
]))
630 rta
= RTA_NEXT(rta
, len
);
634 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
637 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
638 while (RTA_OK(rta
, len
)) {
639 if (rta
->rta_type
<= max
)
640 tb
[rta
->rta_type
] = rta
;
641 rta
= RTA_NEXT(rta
, len
);
646 * netlink_parse_rtattr_nested() - Parses a nested route attribute
647 * @tb: Pointer to array for storing rtattr in.
648 * @max: Max number to store.
649 * @rta: Pointer to rtattr to look for nested items in.
651 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
654 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
657 bool nl_addraw_l(struct nlmsghdr
*n
, unsigned int maxlen
, const void *data
,
660 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
661 zlog_err("ERROR message exceeded bound of %d", maxlen
);
665 memcpy(NLMSG_TAIL(n
), data
, len
);
666 memset((uint8_t *)NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
667 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
672 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
673 const void *data
, unsigned int alen
)
678 len
= RTA_LENGTH(alen
);
680 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
683 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
684 rta
->rta_type
= type
;
688 memcpy(RTA_DATA(rta
), data
, alen
);
692 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
697 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
700 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
703 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
706 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
709 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
712 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
715 bool nl_attr_put64(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
718 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint64_t));
721 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
723 struct rtattr
*nest
= NLMSG_TAIL(n
);
725 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
728 nest
->rta_type
|= NLA_F_NESTED
;
732 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
734 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
738 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
740 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
742 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
746 memset(rtnh
, 0, sizeof(struct rtnexthop
));
748 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
753 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
755 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
758 bool nl_rta_put(struct rtattr
*rta
, unsigned int maxlen
, int type
,
759 const void *data
, int alen
)
761 struct rtattr
*subrta
;
762 int len
= RTA_LENGTH(alen
);
764 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
765 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
769 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
770 subrta
->rta_type
= type
;
771 subrta
->rta_len
= len
;
773 memcpy(RTA_DATA(subrta
), data
, alen
);
774 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
779 bool nl_rta_put16(struct rtattr
*rta
, unsigned int maxlen
, int type
,
782 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint16_t));
785 bool nl_rta_put64(struct rtattr
*rta
, unsigned int maxlen
, int type
,
788 return nl_rta_put(rta
, maxlen
, type
, &data
, sizeof(uint64_t));
791 struct rtattr
*nl_rta_nest(struct rtattr
*rta
, unsigned int maxlen
, int type
)
793 struct rtattr
*nest
= RTA_TAIL(rta
);
795 if (nl_rta_put(rta
, maxlen
, type
, NULL
, 0))
798 nest
->rta_type
|= NLA_F_NESTED
;
803 int nl_rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
805 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
810 const char *nl_msg_type_to_str(uint16_t msg_type
)
812 return lookup_msg(nlmsg_str
, msg_type
, "");
815 const char *nl_rtproto_to_str(uint8_t rtproto
)
817 return lookup_msg(rtproto_str
, rtproto
, "");
820 const char *nl_family_to_str(uint8_t family
)
822 return lookup_msg(family_str
, family
, "");
825 const char *nl_rttype_to_str(uint8_t rttype
)
827 return lookup_msg(rttype_str
, rttype
, "");
830 #define NLA_OK(nla, len) \
831 ((len) >= (int)sizeof(struct nlattr) \
832 && (nla)->nla_len >= sizeof(struct nlattr) \
833 && (nla)->nla_len <= (len))
834 #define NLA_NEXT(nla, attrlen) \
835 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
836 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
837 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
838 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
840 #define ERR_NLA(err, inner_len) \
841 ((struct nlattr *)(((char *)(err)) \
842 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
843 + NLMSG_ALIGN((inner_len))))
845 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
846 struct nlattr
*nla
, int len
)
848 while (NLA_OK(nla
, len
)) {
849 if (nla
->nla_type
<= max
)
850 tb
[nla
->nla_type
] = nla
;
851 nla
= NLA_NEXT(nla
, len
);
855 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
857 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
858 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
859 const struct nlmsghdr
*err_nlh
= NULL
;
860 /* Length not including nlmsghdr */
862 /* Inner error netlink message length */
863 uint32_t inner_len
= 0;
864 const char *msg
= NULL
;
867 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
868 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
870 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
872 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
875 if (tb
[NLMSGERR_ATTR_MSG
])
876 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
878 if (tb
[NLMSGERR_ATTR_OFFS
]) {
879 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
881 if (off
> h
->nlmsg_len
) {
882 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
883 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
885 * Header of failed message
886 * we are not doing anything currently with it
887 * but noticing it for later.
890 zlog_debug("%s: Received %s extended Ack", __func__
,
891 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
895 if (msg
&& *msg
!= '\0') {
896 bool is_err
= !!err
->error
;
899 zlog_err("Extended Error: %s", msg
);
901 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
902 "Extended Warning: %s", msg
);
907 * netlink_send_msg - send a netlink message of a certain size.
909 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
911 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
914 struct sockaddr_nl snl
= {};
915 struct iovec iov
= {};
916 struct msghdr msg
= {};
921 iov
.iov_len
= buflen
;
923 msg
.msg_namelen
= sizeof(snl
);
927 snl
.nl_family
= AF_NETLINK
;
929 /* Send message to netlink interface. */
930 frr_with_privs(&zserv_privs
) {
931 status
= sendmsg(nl
->sock
, &msg
, 0);
935 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
936 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
938 nl_dump(buf
, buflen
);
940 zlog_hexdump(buf
, buflen
);
941 #endif /* NETLINK_DEBUG */
945 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
946 safe_strerror(save_errno
));
954 * netlink_recv_msg - receive a netlink message.
956 * Returns -1 on error, 0 if read would block or the number of bytes received.
958 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
963 iov
.iov_base
= nl
->buf
;
964 iov
.iov_len
= nl
->buflen
;
971 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
973 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
974 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
976 iov
.iov_base
= nl
->buf
;
977 iov
.iov_len
= nl
->buflen
;
980 status
= recvmsg(nl
->sock
, msg
, 0);
981 } while (status
== -1 && errno
== EINTR
);
984 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
986 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
987 nl
->name
, safe_strerror(errno
));
989 * In this case we are screwed. There is no good way to recover
990 * zebra at this point.
996 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
1000 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
1001 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1002 "%s sender address length error: length %d", nl
->name
,
1007 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
1008 zlog_debug("%s: << netlink message dump [recv]", __func__
);
1009 #ifdef NETLINK_DEBUG
1010 nl_dump(nl
->buf
, status
);
1012 zlog_hexdump(nl
->buf
, status
);
1013 #endif /* NETLINK_DEBUG */
1020 * netlink_parse_error - parse a netlink error message
1022 * Returns 1 if this message is acknowledgement, 0 if this error should be
1023 * ignored, -1 otherwise.
1025 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
1026 bool is_cmd
, bool startup
)
1028 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
1029 int errnum
= err
->error
;
1030 int msg_type
= err
->msg
.nlmsg_type
;
1032 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
1033 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1034 "%s error: message truncated", nl
->name
);
1039 * Parse the extended information before we actually handle it. At this
1040 * point in time we do not do anything other than report the issue.
1042 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
1043 netlink_parse_extended_ack(h
);
1045 /* If the error field is zero, then this is an ACK. */
1046 if (err
->error
== 0) {
1047 if (IS_ZEBRA_DEBUG_KERNEL
) {
1048 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
1050 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
1051 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
1052 err
->msg
.nlmsg_pid
);
1059 * Deal with errors that occur because of races in link handling
1060 * or types are not supported in kernel.
1063 ((msg_type
== RTM_DELROUTE
&&
1064 (-errnum
== ENODEV
|| -errnum
== ESRCH
)) ||
1065 (msg_type
== RTM_NEWROUTE
&&
1066 (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)) ||
1067 ((msg_type
== RTM_NEWTUNNEL
|| msg_type
== RTM_DELTUNNEL
||
1068 msg_type
== RTM_GETTUNNEL
) &&
1069 (-errnum
== EOPNOTSUPP
)))) {
1070 if (IS_ZEBRA_DEBUG_KERNEL
)
1071 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1072 nl
->name
, safe_strerror(-errnum
),
1073 nl_msg_type_to_str(msg_type
), msg_type
,
1074 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1079 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1080 * link-local. The kernel should have already deleted the neighbor so
1081 * do not log these as an error.
1083 if (msg_type
== RTM_DELNEIGH
1084 || (is_cmd
&& msg_type
== RTM_NEWROUTE
1085 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
1087 * This is known to happen in some situations, don't log as
1090 if (IS_ZEBRA_DEBUG_KERNEL
)
1091 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1092 nl
->name
, safe_strerror(-errnum
),
1093 nl_msg_type_to_str(msg_type
), msg_type
,
1094 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1096 if ((msg_type
!= RTM_GETNEXTHOP
&& msg_type
!= RTM_GETVLAN
) ||
1098 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
1099 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1100 nl
->name
, safe_strerror(-errnum
),
1101 nl_msg_type_to_str(msg_type
), msg_type
,
1102 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
1109 * netlink_parse_info
1111 * Receive message from netlink interface and pass those information
1112 * to the given function.
1114 * filter -> Function to call to read the results
1115 * nl -> netlink socket information
1116 * zns -> The zebra namespace data
1117 * count -> How many we should read in, 0 means as much as possible
1118 * startup -> Are we reading in under startup conditions? passed to
1121 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
1122 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
1123 int count
, bool startup
)
1131 struct sockaddr_nl snl
;
1132 struct msghdr msg
= {.msg_name
= (void *)&snl
,
1133 .msg_namelen
= sizeof(snl
)};
1136 if (count
&& read_in
>= count
)
1139 status
= netlink_recv_msg(nl
, &msg
);
1142 else if (status
== 0)
1146 for (h
= (struct nlmsghdr
*)nl
->buf
;
1147 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
1148 h
= NLMSG_NEXT(h
, status
)) {
1149 /* Finish of reading. */
1150 if (h
->nlmsg_type
== NLMSG_DONE
)
1153 /* Error handling. */
1154 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1155 int err
= netlink_parse_error(
1156 nl
, h
, zns
->is_cmd
, startup
);
1159 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1167 * What is the right thing to do? The kernel
1168 * is telling us that the dump request was interrupted
1169 * and we more than likely are out of luck and have
1170 * missed data from the kernel. At this point in time
1171 * lets just note that this is happening.
1173 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
1175 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
1176 "netlink recvmsg: The Dump request was interrupted");
1178 /* OK we got netlink message. */
1179 if (IS_ZEBRA_DEBUG_KERNEL
)
1181 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1183 nl_msg_type_to_str(h
->nlmsg_type
),
1184 h
->nlmsg_type
, h
->nlmsg_len
,
1185 h
->nlmsg_seq
, h
->nlmsg_pid
);
1189 * Ignore messages that maybe sent from
1190 * other actors besides the kernel
1192 if (snl
.nl_pid
!= 0) {
1193 zlog_debug("Ignoring message from pid %u",
1198 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1200 zlog_debug("%s filter function error",
1206 /* After error care. */
1207 if (msg
.msg_flags
& MSG_TRUNC
) {
1208 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1209 "%s error: message truncated", nl
->name
);
1213 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1214 "%s error: data remnant size %d", nl
->name
,
1225 * sendmsg() to netlink socket then recvmsg().
1226 * Calls netlink_parse_info to parse returned data
1228 * filter -> The filter to read final results from kernel
1229 * nlmsghdr -> The data to send to the kernel
1230 * dp_info -> The dataplane and netlink socket information
1231 * startup -> Are we reading in under startup conditions
1232 * This is passed through eventually to filter.
1234 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1237 struct zebra_dplane_info
*dp_info
, bool startup
)
1241 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1242 n
->nlmsg_seq
= dp_info
->seq
;
1243 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1245 if (IS_ZEBRA_DEBUG_KERNEL
)
1247 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1248 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1249 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1252 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1256 * Get reply from netlink socket.
1257 * The reply should either be an acknowlegement or an error.
1259 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1263 * Synchronous version of netlink_talk_info. Converts args to suit the
1264 * common version, which is suitable for both sync and async use.
1266 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1267 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1270 struct zebra_dplane_info dp_info
;
1272 /* Increment sequence number before capturing snapshot of ns socket
1277 /* Capture info in intermediate info struct */
1278 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1280 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1283 /* Issue request message to kernel via netlink socket. GET messages
1284 * are issued through this interface.
1286 int netlink_request(struct nlsock
*nl
, void *req
)
1288 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1290 /* Check netlink socket. */
1292 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1297 /* Fill common fields for all requests. */
1298 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1299 n
->nlmsg_seq
= ++nl
->seq
;
1301 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1307 static int nl_batch_read_resp(struct nl_batch
*bth
)
1310 struct sockaddr_nl snl
;
1311 struct msghdr msg
= {};
1314 struct zebra_dplane_ctx
*ctx
;
1317 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1319 msg
.msg_name
= (void *)&snl
;
1320 msg
.msg_namelen
= sizeof(snl
);
1323 * The responses are not batched, so we need to read and process one
1324 * message at a time.
1327 status
= netlink_recv_msg(nl
, &msg
);
1329 * status == -1 is a full on failure somewhere
1330 * since we don't know where the problem happened
1331 * we must mark all as failed
1333 * Else we mark everything as worked
1336 if (status
== -1 || status
== 0) {
1337 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1340 dplane_ctx_set_status(
1342 ZEBRA_DPLANE_REQUEST_FAILURE
);
1343 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1348 h
= (struct nlmsghdr
*)nl
->buf
;
1352 * Find the corresponding context object. Received responses are
1353 * in the same order as requests we sent, so we can simply
1354 * iterate over the context list and match responses with
1355 * requests at same time.
1358 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1361 * This is a situation where we have gotten
1362 * into a bad spot. We need to know that
1363 * this happens( does it? )
1366 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1372 * 'update' context objects take two consecutive
1375 if (dplane_ctx_is_update(ctx
) &&
1376 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1378 * This is the situation where we get a response
1379 * to a message that should be ignored.
1385 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1386 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1388 /* We have found corresponding context object. */
1389 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1392 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1394 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1396 dplane_ctx_get_ns(ctx
)->seq
);
1401 * If we ignore the message due to an update
1402 * above we should still fricking decode the
1403 * message for our operator to understand
1406 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1409 zlog_debug("%s: netlink error message seq=%d %d",
1410 __func__
, h
->nlmsg_seq
, err
);
1415 * We received a message with the sequence number that isn't
1416 * associated with any dplane context object.
1419 if (IS_ZEBRA_DEBUG_KERNEL
)
1421 "%s: skipping unassociated response, seq number %d NS %u",
1422 __func__
, h
->nlmsg_seq
,
1427 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1428 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1432 dplane_ctx_set_status(
1433 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1435 if (IS_ZEBRA_DEBUG_KERNEL
)
1436 zlog_debug("%s: netlink error message seq=%d ",
1437 __func__
, h
->nlmsg_seq
);
1442 * If we get here then we did not receive neither the ack nor
1443 * the error and instead received some other message in an
1446 if (IS_ZEBRA_DEBUG_KERNEL
)
1447 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1448 __func__
, h
->nlmsg_type
,
1449 nl_msg_type_to_str(h
->nlmsg_type
),
1456 static void nl_batch_reset(struct nl_batch
*bth
)
1458 bth
->buf_head
= bth
->buf
;
1463 dplane_ctx_q_init(&(bth
->ctx_list
));
1466 static void nl_batch_init(struct nl_batch
*bth
,
1467 struct dplane_ctx_list_head
*ctx_out_q
)
1470 * If the size of the buffer has changed, free and then allocate a new
1474 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1475 if (bufsize
!= nl_batch_tx_bufsize
) {
1476 if (nl_batch_tx_buf
)
1477 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1479 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1480 nl_batch_tx_bufsize
= bufsize
;
1483 bth
->buf
= nl_batch_tx_buf
;
1484 bth
->bufsiz
= bufsize
;
1485 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1486 memory_order_relaxed
);
1488 bth
->ctx_out_q
= ctx_out_q
;
1490 nl_batch_reset(bth
);
1493 static void nl_batch_send(struct nl_batch
*bth
)
1495 struct zebra_dplane_ctx
*ctx
;
1498 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1500 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1502 if (IS_ZEBRA_DEBUG_KERNEL
)
1503 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1504 __func__
, nl
->name
, bth
->curlen
,
1507 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1511 if (nl_batch_read_resp(bth
) == -1)
1516 /* Move remaining contexts to the outbound queue. */
1518 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1523 dplane_ctx_set_status(ctx
,
1524 ZEBRA_DPLANE_REQUEST_FAILURE
);
1526 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1529 nl_batch_reset(bth
);
1532 enum netlink_msg_status
netlink_batch_add_msg(
1533 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1534 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1539 struct nlmsghdr
*msgh
;
1542 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1545 * If there was an error while encoding the message (other than buffer
1546 * overflow) then return an error.
1549 return FRR_NETLINK_ERROR
;
1552 * If the message doesn't fit entirely in the buffer then send the batch
1557 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1558 bth
->bufsiz
- bth
->curlen
);
1560 * If the message doesn't fit in the empty buffer then just
1564 return FRR_NETLINK_ERROR
;
1567 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1568 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1573 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1574 msgh
->nlmsg_seq
= seq
;
1575 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1577 bth
->zns
= dplane_ctx_get_ns(ctx
);
1578 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1579 bth
->curlen
+= size
;
1582 return FRR_NETLINK_QUEUED
;
1585 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1586 struct zebra_dplane_ctx
*ctx
)
1588 if (dplane_ctx_is_skip_kernel(ctx
))
1589 return FRR_NETLINK_SUCCESS
;
1591 switch (dplane_ctx_get_op(ctx
)) {
1593 case DPLANE_OP_ROUTE_INSTALL
:
1594 case DPLANE_OP_ROUTE_UPDATE
:
1595 case DPLANE_OP_ROUTE_DELETE
:
1596 return netlink_put_route_update_msg(bth
, ctx
);
1598 case DPLANE_OP_NH_INSTALL
:
1599 case DPLANE_OP_NH_UPDATE
:
1600 case DPLANE_OP_NH_DELETE
:
1601 return netlink_put_nexthop_update_msg(bth
, ctx
);
1603 case DPLANE_OP_LSP_INSTALL
:
1604 case DPLANE_OP_LSP_UPDATE
:
1605 case DPLANE_OP_LSP_DELETE
:
1606 return netlink_put_lsp_update_msg(bth
, ctx
);
1608 case DPLANE_OP_PW_INSTALL
:
1609 case DPLANE_OP_PW_UNINSTALL
:
1610 return netlink_put_pw_update_msg(bth
, ctx
);
1612 case DPLANE_OP_ADDR_INSTALL
:
1613 case DPLANE_OP_ADDR_UNINSTALL
:
1614 return netlink_put_address_update_msg(bth
, ctx
);
1616 case DPLANE_OP_MAC_INSTALL
:
1617 case DPLANE_OP_MAC_DELETE
:
1618 return netlink_put_mac_update_msg(bth
, ctx
);
1620 case DPLANE_OP_NEIGH_INSTALL
:
1621 case DPLANE_OP_NEIGH_UPDATE
:
1622 case DPLANE_OP_NEIGH_DELETE
:
1623 case DPLANE_OP_VTEP_ADD
:
1624 case DPLANE_OP_VTEP_DELETE
:
1625 case DPLANE_OP_NEIGH_DISCOVER
:
1626 case DPLANE_OP_NEIGH_IP_INSTALL
:
1627 case DPLANE_OP_NEIGH_IP_DELETE
:
1628 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1629 return netlink_put_neigh_update_msg(bth
, ctx
);
1631 case DPLANE_OP_RULE_ADD
:
1632 case DPLANE_OP_RULE_DELETE
:
1633 case DPLANE_OP_RULE_UPDATE
:
1634 return netlink_put_rule_update_msg(bth
, ctx
);
1636 case DPLANE_OP_SYS_ROUTE_ADD
:
1637 case DPLANE_OP_SYS_ROUTE_DELETE
:
1638 case DPLANE_OP_ROUTE_NOTIFY
:
1639 case DPLANE_OP_LSP_NOTIFY
:
1640 case DPLANE_OP_BR_PORT_UPDATE
:
1641 return FRR_NETLINK_SUCCESS
;
1643 case DPLANE_OP_IPTABLE_ADD
:
1644 case DPLANE_OP_IPTABLE_DELETE
:
1645 case DPLANE_OP_IPSET_ADD
:
1646 case DPLANE_OP_IPSET_DELETE
:
1647 case DPLANE_OP_IPSET_ENTRY_ADD
:
1648 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1649 return FRR_NETLINK_ERROR
;
1651 case DPLANE_OP_GRE_SET
:
1652 return netlink_put_gre_set_msg(bth
, ctx
);
1654 case DPLANE_OP_INTF_ADDR_ADD
:
1655 case DPLANE_OP_INTF_ADDR_DEL
:
1656 case DPLANE_OP_NONE
:
1657 return FRR_NETLINK_ERROR
;
1659 case DPLANE_OP_INTF_NETCONFIG
:
1660 return netlink_put_intf_netconfig(bth
, ctx
);
1662 case DPLANE_OP_INTF_INSTALL
:
1663 case DPLANE_OP_INTF_UPDATE
:
1664 case DPLANE_OP_INTF_DELETE
:
1665 return netlink_put_intf_update_msg(bth
, ctx
);
1667 case DPLANE_OP_TC_QDISC_INSTALL
:
1668 case DPLANE_OP_TC_QDISC_UNINSTALL
:
1669 return netlink_put_tc_qdisc_update_msg(bth
, ctx
);
1670 case DPLANE_OP_TC_CLASS_ADD
:
1671 case DPLANE_OP_TC_CLASS_DELETE
:
1672 case DPLANE_OP_TC_CLASS_UPDATE
:
1673 return netlink_put_tc_class_update_msg(bth
, ctx
);
1674 case DPLANE_OP_TC_FILTER_ADD
:
1675 case DPLANE_OP_TC_FILTER_DELETE
:
1676 case DPLANE_OP_TC_FILTER_UPDATE
:
1677 return netlink_put_tc_filter_update_msg(bth
, ctx
);
1680 return FRR_NETLINK_ERROR
;
1683 void kernel_update_multi(struct dplane_ctx_list_head
*ctx_list
)
1685 struct nl_batch batch
;
1686 struct zebra_dplane_ctx
*ctx
;
1687 struct dplane_ctx_list_head handled_list
;
1688 enum netlink_msg_status res
;
1690 dplane_ctx_q_init(&handled_list
);
1691 nl_batch_init(&batch
, &handled_list
);
1694 ctx
= dplane_ctx_dequeue(ctx_list
);
1698 if (batch
.zns
!= NULL
1699 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1700 nl_batch_send(&batch
);
1703 * Assume all messages will succeed and then mark only the ones
1706 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1708 res
= nl_put_msg(&batch
, ctx
);
1710 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1711 if (res
== FRR_NETLINK_ERROR
)
1712 dplane_ctx_set_status(ctx
,
1713 ZEBRA_DPLANE_REQUEST_FAILURE
);
1715 if (batch
.curlen
> batch
.limit
)
1716 nl_batch_send(&batch
);
1719 nl_batch_send(&batch
);
1721 dplane_ctx_q_init(ctx_list
);
1722 dplane_ctx_list_append(ctx_list
, &handled_list
);
1725 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1727 struct nlsock lookup
, *retval
;
1732 retval
= hash_lookup(nlsock_hash
, &lookup
);
1738 /* Insert nlsock entry into hash */
1739 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1742 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1746 /* Remove nlsock entry from hash */
1747 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1750 (void)hash_release(nlsock_hash
, nls
);
1754 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1756 const struct nlsock
*nl
= arg
;
1761 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1763 const struct nlsock
*nl1
= arg1
;
1764 const struct nlsock
*nl2
= arg2
;
1766 if (nl1
->sock
== nl2
->sock
)
1772 /* Exported interface function. This function simply calls
1773 netlink_socket (). */
1774 void kernel_init(struct zebra_ns
*zns
)
1776 uint32_t groups
, dplane_groups
, ext_groups
;
1777 #if defined SOL_NETLINK
1782 * Initialize netlink sockets
1784 * If RTMGRP_XXX exists use that, but at some point
1785 * I think the kernel developers realized that
1786 * keeping track of all the different values would
1787 * lead to confusion, so we need to convert the
1788 * RTNLGRP_XXX to a bit position for ourself
1791 * NOTE: If the bit is >= 32, you must use setsockopt(). Those
1792 * groups are added further below after SOL_NETLINK is verified to
1795 groups
= RTMGRP_LINK
|
1797 RTMGRP_IPV4_IFADDR
|
1799 RTMGRP_IPV6_IFADDR
|
1800 RTMGRP_IPV4_MROUTE
|
1802 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1803 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1804 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1)) |
1805 ((uint32_t) 1 << (RTNLGRP_TC
- 1));
1807 dplane_groups
= (RTMGRP_LINK
|
1808 RTMGRP_IPV4_IFADDR
|
1809 RTMGRP_IPV6_IFADDR
|
1810 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1811 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1812 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1814 /* Use setsockopt for > 31 group */
1815 ext_groups
= RTNLGRP_TUNNEL
;
1817 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1818 "netlink-listen (NS %u)", zns
->ns_id
);
1819 zns
->netlink
.sock
= -1;
1820 if (netlink_socket(&zns
->netlink
, groups
, &ext_groups
, 1, zns
->ns_id
) <
1822 zlog_err("Failure to create %s socket",
1827 kernel_netlink_nlsock_insert(&zns
->netlink
);
1829 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1830 "netlink-cmd (NS %u)", zns
->ns_id
);
1831 zns
->netlink_cmd
.sock
= -1;
1832 if (netlink_socket(&zns
->netlink_cmd
, 0, 0, 0, zns
->ns_id
) < 0) {
1833 zlog_err("Failure to create %s socket",
1834 zns
->netlink_cmd
.name
);
1838 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1840 /* Outbound socket for dplane programming of the host OS. */
1841 snprintf(zns
->netlink_dplane_out
.name
,
1842 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1844 zns
->netlink_dplane_out
.sock
= -1;
1845 if (netlink_socket(&zns
->netlink_dplane_out
, 0, 0, 0, zns
->ns_id
) < 0) {
1846 zlog_err("Failure to create %s socket",
1847 zns
->netlink_dplane_out
.name
);
1851 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1853 /* Inbound socket for OS events coming to the dplane. */
1854 snprintf(zns
->netlink_dplane_in
.name
,
1855 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1857 zns
->netlink_dplane_in
.sock
= -1;
1858 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
, 0, 0,
1860 zlog_err("Failure to create %s socket",
1861 zns
->netlink_dplane_in
.name
);
1865 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1868 * SOL_NETLINK is not available on all platforms yet
1869 * apparently. It's in bits/socket.h which I am not
1870 * sure that we want to pull into our build system.
1872 #if defined SOL_NETLINK
1875 * setsockopt multicast group subscriptions that don't fit in nl_groups
1877 grp
= RTNLGRP_BRVLAN
;
1878 ret
= setsockopt(zns
->netlink
.sock
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
,
1883 "Registration for RTNLGRP_BRVLAN Membership failed : %d %s",
1884 errno
, safe_strerror(errno
));
1886 * Let's tell the kernel that we want to receive extended
1887 * ACKS over our command socket(s)
1890 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1894 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1895 errno
, safe_strerror(errno
));
1898 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1899 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1902 zlog_notice("Registration for extended dp ACK failed : %d %s",
1903 errno
, safe_strerror(errno
));
1906 * Trim off the payload of the original netlink message in the
1907 * acknowledgment. This option is available since Linux 4.2, so if
1908 * setsockopt fails, ignore the error.
1911 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1912 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1915 "Registration for reduced ACK packet size failed, probably running an early kernel");
1918 /* Register kernel socket. */
1919 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1920 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1921 zns
->netlink
.name
, safe_strerror(errno
));
1923 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1924 zlog_err("Can't set %s socket error: %s(%d)",
1925 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1927 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1928 zlog_err("Can't set %s socket error: %s(%d)",
1929 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1932 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1933 zlog_err("Can't set %s socket error: %s(%d)",
1934 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1937 /* Set receive buffer size if it's set from command line */
1939 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1940 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1941 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1942 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1945 /* Set filter for inbound sockets, to exclude events we've generated
1948 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1949 zns
->netlink_dplane_out
.snl
.nl_pid
);
1951 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1952 zns
->netlink_cmd
.snl
.nl_pid
,
1953 zns
->netlink_dplane_out
.snl
.nl_pid
);
1955 zns
->t_netlink
= NULL
;
1957 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1958 zns
->netlink
.sock
, &zns
->t_netlink
);
1963 /* Helper to clean up an nlsock */
1964 static void kernel_nlsock_fini(struct nlsock
*nls
)
1966 if (nls
&& nls
->sock
>= 0) {
1967 kernel_netlink_nlsock_remove(nls
);
1970 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1975 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1977 THREAD_OFF(zns
->t_netlink
);
1979 kernel_nlsock_fini(&zns
->netlink
);
1981 kernel_nlsock_fini(&zns
->netlink_cmd
);
1983 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1985 /* During zebra shutdown, we need to leave the dataplane socket
1986 * around until all work is done.
1989 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1993 * Global init for platform-/OS-specific things
1995 void kernel_router_init(void)
1997 /* Init nlsock hash and lock */
1998 pthread_mutex_init(&nlsock_mutex
, NULL
);
1999 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
2000 kernel_netlink_nlsock_hash_equal
,
2001 "Netlink Socket Hash");
2005 * Global deinit for platform-/OS-specific things
2007 void kernel_router_terminate(void)
2009 pthread_mutex_destroy(&nlsock_mutex
);
2011 hash_free(nlsock_hash
);
2015 #endif /* HAVE_NETLINK */