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
23 #if defined(HANDLE_NETLINK_FUZZING)
27 #endif /* HANDLE_NETLINK_FUZZING */
35 #include "connected.h"
38 #include "zebra_memory.h"
45 #include "lib_errors.h"
47 //#include "zebra/zserv.h"
48 #include "zebra/zebra_router.h"
49 #include "zebra/zebra_ns.h"
50 #include "zebra/zebra_vrf.h"
52 #include "zebra/debug.h"
53 #include "zebra/kernel_netlink.h"
54 #include "zebra/rt_netlink.h"
55 #include "zebra/if_netlink.h"
56 #include "zebra/rule_netlink.h"
57 #include "zebra/zebra_errors.h"
59 #ifndef SO_RCVBUFFORCE
60 #define SO_RCVBUFFORCE (33)
63 /* Hack for GNU libc version 2. */
65 #define MSG_TRUNC 0x20
66 #endif /* MSG_TRUNC */
69 #define NLMSG_TAIL(nmsg) \
70 ((struct rtattr *)(((uint8_t *)(nmsg)) \
71 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
75 #define RTA_TAIL(rta) \
76 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
79 #ifndef RTNL_FAMILY_IP6MR
80 #define RTNL_FAMILY_IP6MR 129
83 #ifndef RTPROT_MROUTED
84 #define RTPROT_MROUTED 17
87 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
88 {RTM_DELROUTE
, "RTM_DELROUTE"},
89 {RTM_GETROUTE
, "RTM_GETROUTE"},
90 {RTM_NEWLINK
, "RTM_NEWLINK"},
91 {RTM_DELLINK
, "RTM_DELLINK"},
92 {RTM_GETLINK
, "RTM_GETLINK"},
93 {RTM_NEWADDR
, "RTM_NEWADDR"},
94 {RTM_DELADDR
, "RTM_DELADDR"},
95 {RTM_GETADDR
, "RTM_GETADDR"},
96 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
97 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
98 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
99 {RTM_NEWRULE
, "RTM_NEWRULE"},
100 {RTM_DELRULE
, "RTM_DELRULE"},
101 {RTM_GETRULE
, "RTM_GETRULE"},
102 {RTM_NEWNEXTHOP
, "RTM_NEWNEXTHOP"},
103 {RTM_DELNEXTHOP
, "RTM_DELNEXTHOP"},
104 {RTM_GETNEXTHOP
, "RTM_GETNEXTHOP"},
107 static const struct message rtproto_str
[] = {
108 {RTPROT_REDIRECT
, "redirect"},
109 {RTPROT_KERNEL
, "kernel"},
110 {RTPROT_BOOT
, "boot"},
111 {RTPROT_STATIC
, "static"},
112 {RTPROT_GATED
, "GateD"},
113 {RTPROT_RA
, "router advertisement"},
115 {RTPROT_ZEBRA
, "Zebra"},
117 {RTPROT_BIRD
, "BIRD"},
118 #endif /* RTPROT_BIRD */
119 {RTPROT_MROUTED
, "mroute"},
121 {RTPROT_OSPF
, "OSPF"},
122 {RTPROT_ISIS
, "IS-IS"},
124 {RTPROT_RIPNG
, "RIPNG"},
125 {RTPROT_ZSTATIC
, "static"},
128 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
130 {AF_BRIDGE
, "bridge"},
131 {RTNL_FAMILY_IPMR
, "ipv4MR"},
132 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
135 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
136 {RTN_UNICAST
, "unicast"},
137 {RTN_LOCAL
, "local"},
138 {RTN_BROADCAST
, "broadcast"},
139 {RTN_ANYCAST
, "anycast"},
140 {RTN_MULTICAST
, "multicast"},
141 {RTN_BLACKHOLE
, "blackhole"},
142 {RTN_UNREACHABLE
, "unreachable"},
143 {RTN_PROHIBIT
, "prohibited"},
144 {RTN_THROW
, "throw"},
146 {RTN_XRESOLVE
, "resolver"},
149 extern struct thread_master
*master
;
150 extern uint32_t nl_rcvbufsize
;
152 extern struct zebra_privs_t zserv_privs
;
155 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
158 * This is an error condition that must be handled during
161 * The netlink_talk_filter function is used for communication
162 * down the netlink_cmd pipe and we are expecting
163 * an ack being received. So if we get here
164 * then we did not receive the ack and instead
165 * received some other message in an unexpected
168 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
169 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
173 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
176 socklen_t newlen
= sizeof(newsize
);
177 socklen_t oldlen
= sizeof(oldsize
);
180 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
182 flog_err_sys(EC_LIB_SOCKET
,
183 "Can't get %s receive buffer size: %s", nl
->name
,
184 safe_strerror(errno
));
188 /* Try force option (linux >= 2.6.14) and fall back to normal set */
189 frr_with_privs(&zserv_privs
) {
190 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
192 sizeof(nl_rcvbufsize
));
195 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
196 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
198 flog_err_sys(EC_LIB_SOCKET
,
199 "Can't set %s receive buffer size: %s", nl
->name
,
200 safe_strerror(errno
));
204 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
206 flog_err_sys(EC_LIB_SOCKET
,
207 "Can't get %s receive buffer size: %s", nl
->name
,
208 safe_strerror(errno
));
212 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
217 /* Make socket for Linux netlink interface. */
218 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
222 struct sockaddr_nl snl
;
226 frr_with_privs(&zserv_privs
) {
227 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
229 zlog_err("Can't open %s socket: %s", nl
->name
,
230 safe_strerror(errno
));
234 memset(&snl
, 0, sizeof snl
);
235 snl
.nl_family
= AF_NETLINK
;
236 snl
.nl_groups
= groups
;
238 /* Bind the socket to the netlink structure for anything. */
239 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
243 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
244 snl
.nl_groups
, safe_strerror(errno
));
249 /* multiple netlink sockets will have different nl_pid */
250 namelen
= sizeof snl
;
251 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
252 if (ret
< 0 || namelen
!= sizeof snl
) {
253 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
254 nl
->name
, safe_strerror(errno
));
264 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
268 * When we handle new message types here
269 * because we are starting to install them
270 * then lets check the netlink_install_filter
271 * and see if we should add the corresponding
272 * allow through entry there.
273 * Probably not needed to do but please
276 switch (h
->nlmsg_type
) {
278 return netlink_route_change(h
, ns_id
, startup
);
280 return netlink_route_change(h
, ns_id
, startup
);
282 return netlink_link_change(h
, ns_id
, startup
);
284 return netlink_link_change(h
, ns_id
, startup
);
286 return netlink_interface_addr(h
, ns_id
, startup
);
288 return netlink_interface_addr(h
, ns_id
, startup
);
290 return netlink_neigh_change(h
, ns_id
);
292 return netlink_neigh_change(h
, ns_id
);
294 return netlink_rule_change(h
, ns_id
, startup
);
296 return netlink_rule_change(h
, ns_id
, startup
);
298 return netlink_nexthop_change(h
, ns_id
, startup
);
300 return netlink_nexthop_change(h
, ns_id
, startup
);
303 * If we have received this message then
304 * we have made a mistake during development
305 * and we need to write some code to handle
306 * this message type or not ask for
307 * it to be sent up to us
309 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
310 "Unknown netlink nlmsg_type %s(%d) vrf %u\n",
311 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
318 #if defined(HANDLE_NETLINK_FUZZING)
319 /* Using globals here to avoid adding function parameters */
321 /* Keep distinct filenames for netlink fuzzy collection */
322 static unsigned int netlink_file_counter
= 1;
324 /* File name to read fuzzed netlink from */
325 static char netlink_fuzz_file
[MAXPATHLEN
] = "";
327 /* Flag for whether to read from file or not */
331 * netlink_read_init() - Starts the message parser
332 * @fname: Filename to read.
334 void netlink_read_init(const char *fname
)
336 struct zebra_dplane_info dp_info
;
338 snprintf(netlink_fuzz_file
, MAXPATHLEN
, "%s", fname
);
339 /* Creating this fake socket for testing purposes */
340 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
342 /* Capture key info from zns struct */
343 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
345 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
,
350 * netlink_write_incoming() - Writes all data received from netlink to a file
351 * @buf: Data from netlink.
352 * @size: Size of data.
353 * @counter: Counter for keeping filenames distinct.
355 static void netlink_write_incoming(const char *buf
, const unsigned int size
,
356 unsigned int counter
)
358 char fname
[MAXPATHLEN
];
361 snprintf(fname
, MAXPATHLEN
, "%s/%s_%u", frr_vtydir
, "netlink", counter
);
362 frr_with_privs(&zserv_privs
) {
363 f
= fopen(fname
, "w");
366 fwrite(buf
, 1, size
, f
);
372 * netlink_read_file() - Reads netlink data from file
373 * @buf: Netlink buffer being overwritten.
374 * @fname: File name to read from.
376 * Return: Size of file.
378 static long netlink_read_file(char *buf
, const char *fname
)
381 long file_bytes
= -1;
383 frr_with_privs(&zserv_privs
) {
384 f
= fopen(fname
, "r");
387 fseek(f
, 0, SEEK_END
);
388 file_bytes
= ftell(f
);
390 fread(buf
, NL_RCV_PKT_BUF_SIZE
, 1, f
);
396 #endif /* HANDLE_NETLINK_FUZZING */
398 static int kernel_read(struct thread
*thread
)
400 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
401 struct zebra_dplane_info dp_info
;
403 /* Capture key info from ns struct */
404 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
406 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
408 zns
->t_netlink
= NULL
;
409 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
416 * Filter out messages from self that occur on listener socket,
417 * caused by our actions on the command socket(s)
419 * When we add new Netlink message types we probably
420 * do not need to add them here as that we are filtering
421 * on the routes we actually care to receive( which is rarer
422 * then the normal course of operations). We are intentionally
423 * allowing some messages from ourselves through
424 * ( I'm looking at you Interface based netlink messages )
425 * so that we only had to write one way to handle incoming
426 * address add/delete changes.
428 static void netlink_install_filter(int sock
, __u32 pid
, __u32 dplane_pid
)
431 * BPF_JUMP instructions and where you jump to are based upon
432 * 0 as being the next statement. So count from 0. Writing
433 * this down because every time I look at this I have to
436 struct sock_filter filter
[] = {
439 * if (nlmsg_pid == pid ||
440 * nlmsg_pid == dplane_pid) {
441 * if (the incoming nlmsg_type ==
442 * RTM_NEWADDR | RTM_DELADDR)
447 * keep this netlink message
450 * 0: Load the nlmsg_pid into the BPF register
452 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
453 offsetof(struct nlmsghdr
, nlmsg_pid
)),
457 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
459 * 2: Compare to dplane pid
461 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 4),
463 * 3: Load the nlmsg_type into BPF register
465 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
466 offsetof(struct nlmsghdr
, nlmsg_type
)),
468 * 4: Compare to RTM_NEWADDR
470 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 2, 0),
472 * 5: Compare to RTM_DELADDR
474 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 1, 0),
476 * 6: This is the end state of we want to skip the
479 BPF_STMT(BPF_RET
| BPF_K
, 0),
480 /* 7: This is the end state of we want to keep
483 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
486 struct sock_fprog prog
= {
487 .len
= array_size(filter
), .filter
= filter
,
490 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
492 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s\n",
493 safe_strerror(errno
));
496 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
499 while (RTA_OK(rta
, len
)) {
500 if (rta
->rta_type
<= max
)
501 tb
[rta
->rta_type
] = rta
;
502 rta
= RTA_NEXT(rta
, len
);
507 * netlink_parse_rtattr_nested() - Parses a nested route attribute
508 * @tb: Pointer to array for storing rtattr in.
509 * @max: Max number to store.
510 * @rta: Pointer to rtattr to look for nested items in.
512 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
515 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
518 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
519 const void *data
, unsigned int alen
)
524 len
= RTA_LENGTH(alen
);
526 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
529 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
530 rta
->rta_type
= type
;
534 memcpy(RTA_DATA(rta
), data
, alen
);
538 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
543 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
,
544 const void *data
, unsigned int alen
)
547 struct rtattr
*subrta
;
549 len
= RTA_LENGTH(alen
);
551 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
554 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
555 subrta
->rta_type
= type
;
556 subrta
->rta_len
= len
;
559 memcpy(RTA_DATA(subrta
), data
, alen
);
563 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
568 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
570 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
573 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
575 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
578 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
580 struct rtattr
*nest
= NLMSG_TAIL(n
);
582 addattr_l(n
, maxlen
, type
, NULL
, 0);
586 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
588 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
592 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
594 struct rtattr
*nest
= RTA_TAIL(rta
);
596 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
600 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
602 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
606 const char *nl_msg_type_to_str(uint16_t msg_type
)
608 return lookup_msg(nlmsg_str
, msg_type
, "");
611 const char *nl_rtproto_to_str(uint8_t rtproto
)
613 return lookup_msg(rtproto_str
, rtproto
, "");
616 const char *nl_family_to_str(uint8_t family
)
618 return lookup_msg(family_str
, family
, "");
621 const char *nl_rttype_to_str(uint8_t rttype
)
623 return lookup_msg(rttype_str
, rttype
, "");
626 #define NLA_OK(nla, len) \
627 ((len) >= (int)sizeof(struct nlattr) \
628 && (nla)->nla_len >= sizeof(struct nlattr) \
629 && (nla)->nla_len <= (len))
630 #define NLA_NEXT(nla, attrlen) \
631 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
632 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
633 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
634 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
636 #define ERR_NLA(err, inner_len) \
637 ((struct nlattr *)(((char *)(err)) \
638 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
639 + NLMSG_ALIGN((inner_len))))
641 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
642 struct nlattr
*nla
, int len
)
644 while (NLA_OK(nla
, len
)) {
645 if (nla
->nla_type
<= max
)
646 tb
[nla
->nla_type
] = nla
;
647 nla
= NLA_NEXT(nla
, len
);
651 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
653 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
654 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
655 const struct nlmsghdr
*err_nlh
= NULL
;
656 /* Length not including nlmsghdr */
658 /* Inner error netlink message length */
659 uint32_t inner_len
= 0;
660 const char *msg
= NULL
;
663 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
664 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
666 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
668 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
671 if (tb
[NLMSGERR_ATTR_MSG
])
672 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
674 if (tb
[NLMSGERR_ATTR_OFFS
]) {
675 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
677 if (off
> h
->nlmsg_len
) {
678 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
679 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
681 * Header of failed message
682 * we are not doing anything currently with it
683 * but noticing it for later.
686 zlog_debug("%s: Received %s extended Ack",
688 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
692 if (msg
&& *msg
!= '\0') {
693 bool is_err
= !!err
->error
;
696 zlog_err("Extended Error: %s", msg
);
698 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
699 "Extended Warning: %s", msg
);
706 * Receive message from netlink interface and pass those information
707 * to the given function.
709 * filter -> Function to call to read the results
710 * nl -> netlink socket information
711 * zns -> The zebra namespace data
712 * count -> How many we should read in, 0 means as much as possible
713 * startup -> Are we reading in under startup conditions? passed to
716 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
717 const struct nlsock
*nl
,
718 const struct zebra_dplane_info
*zns
,
719 int count
, int startup
)
727 char buf
[NL_RCV_PKT_BUF_SIZE
];
728 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
729 struct sockaddr_nl snl
;
730 struct msghdr msg
= {.msg_name
= (void *)&snl
,
731 .msg_namelen
= sizeof snl
,
736 if (count
&& read_in
>= count
)
739 #if defined(HANDLE_NETLINK_FUZZING)
740 /* Check if reading and filename is set */
741 if (netlink_read
&& '\0' != netlink_fuzz_file
[0]) {
742 zlog_debug("Reading netlink fuzz file");
743 status
= netlink_read_file(buf
, netlink_fuzz_file
);
746 status
= recvmsg(nl
->sock
, &msg
, 0);
749 status
= recvmsg(nl
->sock
, &msg
, 0);
750 #endif /* HANDLE_NETLINK_FUZZING */
754 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
756 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
,
757 "%s recvmsg overrun: %s", nl
->name
,
758 safe_strerror(errno
));
760 * In this case we are screwed.
761 * There is no good way to
762 * recover zebra at this point.
769 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
773 if (msg
.msg_namelen
!= sizeof snl
) {
774 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
775 "%s sender address length error: length %d",
776 nl
->name
, msg
.msg_namelen
);
780 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
781 zlog_debug("%s: << netlink message dump [recv]",
783 zlog_hexdump(buf
, status
);
786 #if defined(HANDLE_NETLINK_FUZZING)
788 zlog_debug("Writing incoming netlink message");
789 netlink_write_incoming(buf
, status
,
790 netlink_file_counter
++);
792 #endif /* HANDLE_NETLINK_FUZZING */
795 for (h
= (struct nlmsghdr
*)buf
;
796 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
797 h
= NLMSG_NEXT(h
, status
)) {
798 /* Finish of reading. */
799 if (h
->nlmsg_type
== NLMSG_DONE
)
802 /* Error handling. */
803 if (h
->nlmsg_type
== NLMSG_ERROR
) {
804 struct nlmsgerr
*err
=
805 (struct nlmsgerr
*)NLMSG_DATA(h
);
806 int errnum
= err
->error
;
807 int msg_type
= err
->msg
.nlmsg_type
;
810 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
811 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
812 "%s error: message truncated",
818 * Parse the extended information before
819 * we actually handle it.
820 * At this point in time we do not
821 * do anything other than report the
824 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
825 netlink_parse_extended_ack(h
);
827 /* If the error field is zero, then this is an
829 if (err
->error
== 0) {
830 if (IS_ZEBRA_DEBUG_KERNEL
) {
832 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
833 __FUNCTION__
, nl
->name
,
835 err
->msg
.nlmsg_type
),
841 /* return if not a multipart message,
842 * otherwise continue */
843 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
848 /* Deal with errors that occur because of races
849 * in link handling */
851 && ((msg_type
== RTM_DELROUTE
852 && (-errnum
== ENODEV
853 || -errnum
== ESRCH
))
854 || (msg_type
== RTM_NEWROUTE
855 && (-errnum
== ENETDOWN
856 || -errnum
== EEXIST
)))) {
857 if (IS_ZEBRA_DEBUG_KERNEL
)
859 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
861 safe_strerror(-errnum
),
870 /* We see RTM_DELNEIGH when shutting down an
871 * interface with an IPv4
872 * link-local. The kernel should have already
873 * deleted the neighbor
874 * so do not log these as an error.
876 if (msg_type
== RTM_DELNEIGH
877 || (zns
->is_cmd
&& msg_type
== RTM_NEWROUTE
879 || -errnum
== ENETUNREACH
))) {
880 /* This is known to happen in some
881 * situations, don't log
884 if (IS_ZEBRA_DEBUG_KERNEL
)
886 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
888 safe_strerror(-errnum
),
895 if ((msg_type
!= RTM_GETNEXTHOP
)
898 EC_ZEBRA_UNEXPECTED_MESSAGE
,
899 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
901 safe_strerror(-errnum
),
912 /* OK we got netlink message. */
913 if (IS_ZEBRA_DEBUG_KERNEL
)
915 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
917 nl_msg_type_to_str(h
->nlmsg_type
),
918 h
->nlmsg_type
, h
->nlmsg_len
,
919 h
->nlmsg_seq
, h
->nlmsg_pid
);
923 * Ignore messages that maybe sent from
924 * other actors besides the kernel
926 if (snl
.nl_pid
!= 0) {
927 zlog_debug("Ignoring message from pid %u",
932 error
= (*filter
)(h
, zns
->ns_id
, startup
);
934 zlog_debug("%s filter function error",
940 /* After error care. */
941 if (msg
.msg_flags
& MSG_TRUNC
) {
942 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
943 "%s error: message truncated", nl
->name
);
947 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
948 "%s error: data remnant size %d", nl
->name
,
959 * sendmsg() to netlink socket then recvmsg().
960 * Calls netlink_parse_info to parse returned data
962 * filter -> The filter to read final results from kernel
963 * nlmsghdr -> The data to send to the kernel
964 * dp_info -> The dataplane and netlink socket information
965 * startup -> Are we reading in under startup conditions
966 * This is passed through eventually to filter.
968 int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
970 const struct zebra_dplane_info
*dp_info
, int startup
)
973 struct sockaddr_nl snl
;
977 const struct nlsock
*nl
;
979 memset(&snl
, 0, sizeof snl
);
980 memset(&iov
, 0, sizeof iov
);
981 memset(&msg
, 0, sizeof msg
);
984 iov
.iov_len
= n
->nlmsg_len
;
985 msg
.msg_name
= (void *)&snl
;
986 msg
.msg_namelen
= sizeof snl
;
990 snl
.nl_family
= AF_NETLINK
;
992 nl
= &(dp_info
->nls
);
993 n
->nlmsg_seq
= nl
->seq
;
994 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
996 if (IS_ZEBRA_DEBUG_KERNEL
)
998 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
999 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1000 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1003 /* Send message to netlink interface. */
1004 frr_with_privs(&zserv_privs
) {
1005 status
= sendmsg(nl
->sock
, &msg
, 0);
1009 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
1010 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
1011 zlog_hexdump(n
, n
->nlmsg_len
);
1015 flog_err_sys(EC_LIB_SOCKET
, "netlink_talk sendmsg() error: %s",
1016 safe_strerror(save_errno
));
1021 * Get reply from netlink socket.
1022 * The reply should either be an acknowlegement or an error.
1024 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1028 * Synchronous version of netlink_talk_info. Converts args to suit the
1029 * common version, which is suitable for both sync and async use.
1031 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1032 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1035 struct zebra_dplane_info dp_info
;
1037 /* Increment sequence number before capturing snapshot of ns socket
1042 /* Capture info in intermediate info struct */
1043 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1045 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1048 /* Issue request message to kernel via netlink socket. GET messages
1049 * are issued through this interface.
1051 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
1054 struct sockaddr_nl snl
;
1056 /* Check netlink socket. */
1058 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1063 /* Fill common fields for all requests. */
1064 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1065 n
->nlmsg_seq
= ++nl
->seq
;
1067 memset(&snl
, 0, sizeof snl
);
1068 snl
.nl_family
= AF_NETLINK
;
1070 /* Raise capabilities and send message, then lower capabilities. */
1071 frr_with_privs(&zserv_privs
) {
1072 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
1073 (struct sockaddr
*)&snl
, sizeof snl
);
1077 zlog_err("%s sendto failed: %s", nl
->name
,
1078 safe_strerror(errno
));
1085 /* Exported interface function. This function simply calls
1086 netlink_socket (). */
1087 void kernel_init(struct zebra_ns
*zns
)
1089 unsigned long groups
;
1090 #if defined SOL_NETLINK
1095 * Initialize netlink sockets
1097 * If RTMGRP_XXX exists use that, but at some point
1098 * I think the kernel developers realized that
1099 * keeping track of all the different values would
1100 * lead to confusion, so we need to convert the
1101 * RTNLGRP_XXX to a bit position for ourself
1103 groups
= RTMGRP_LINK
|
1105 RTMGRP_IPV4_IFADDR
|
1107 RTMGRP_IPV6_IFADDR
|
1108 RTMGRP_IPV4_MROUTE
|
1110 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
1111 (1 << (RTNLGRP_IPV6_RULE
- 1)) |
1112 (1 << (RTNLGRP_NEXTHOP
- 1));
1114 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1115 "netlink-listen (NS %u)", zns
->ns_id
);
1116 zns
->netlink
.sock
= -1;
1117 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1118 zlog_err("Failure to create %s socket",
1123 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1124 "netlink-cmd (NS %u)", zns
->ns_id
);
1125 zns
->netlink_cmd
.sock
= -1;
1126 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1127 zlog_err("Failure to create %s socket",
1128 zns
->netlink_cmd
.name
);
1132 snprintf(zns
->netlink_dplane
.name
, sizeof(zns
->netlink_dplane
.name
),
1133 "netlink-dp (NS %u)", zns
->ns_id
);
1134 zns
->netlink_dplane
.sock
= -1;
1135 if (netlink_socket(&zns
->netlink_dplane
, 0, zns
->ns_id
) < 0) {
1136 zlog_err("Failure to create %s socket",
1137 zns
->netlink_dplane
.name
);
1142 * SOL_NETLINK is not available on all platforms yet
1143 * apparently. It's in bits/socket.h which I am not
1144 * sure that we want to pull into our build system.
1146 #if defined SOL_NETLINK
1148 * Let's tell the kernel that we want to receive extended
1149 * ACKS over our command socket(s)
1152 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1156 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1157 errno
, safe_strerror(errno
));
1160 ret
= setsockopt(zns
->netlink_dplane
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1164 zlog_notice("Registration for extended dp ACK failed : %d %s",
1165 errno
, safe_strerror(errno
));
1168 /* Register kernel socket. */
1169 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1170 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1171 zns
->netlink
.name
, safe_strerror(errno
));
1173 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1174 zlog_err("Can't set %s socket error: %s(%d)",
1175 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1177 if (fcntl(zns
->netlink_dplane
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1178 zlog_err("Can't set %s socket error: %s(%d)",
1179 zns
->netlink_dplane
.name
, safe_strerror(errno
), errno
);
1181 /* Set receive buffer size if it's set from command line */
1183 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
1185 netlink_install_filter(zns
->netlink
.sock
,
1186 zns
->netlink_cmd
.snl
.nl_pid
,
1187 zns
->netlink_dplane
.snl
.nl_pid
);
1189 zns
->t_netlink
= NULL
;
1191 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1192 zns
->netlink
.sock
, &zns
->t_netlink
);
1197 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1199 THREAD_READ_OFF(zns
->t_netlink
);
1201 if (zns
->netlink
.sock
>= 0) {
1202 close(zns
->netlink
.sock
);
1203 zns
->netlink
.sock
= -1;
1206 if (zns
->netlink_cmd
.sock
>= 0) {
1207 close(zns
->netlink_cmd
.sock
);
1208 zns
->netlink_cmd
.sock
= -1;
1211 /* During zebra shutdown, we need to leave the dataplane socket
1212 * around until all work is done.
1215 if (zns
->netlink_dplane
.sock
>= 0) {
1216 close(zns
->netlink_dplane
.sock
);
1217 zns
->netlink_dplane
.sock
= -1;
1221 #endif /* HAVE_NETLINK */