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)
26 #endif /* HANDLE_NETLINK_FUZZING */
34 #include "connected.h"
37 #include "zebra_memory.h"
44 #include "lib_errors.h"
46 //#include "zebra/zserv.h"
47 #include "zebra/zebra_router.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/zebra_vrf.h"
51 #include "zebra/debug.h"
52 #include "zebra/kernel_netlink.h"
53 #include "zebra/rt_netlink.h"
54 #include "zebra/if_netlink.h"
55 #include "zebra/rule_netlink.h"
56 #include "zebra/zebra_errors.h"
58 #ifndef SO_RCVBUFFORCE
59 #define SO_RCVBUFFORCE (33)
62 /* Hack for GNU libc version 2. */
64 #define MSG_TRUNC 0x20
65 #endif /* MSG_TRUNC */
68 #define NLMSG_TAIL(nmsg) \
69 ((struct rtattr *)(((uint8_t *)(nmsg)) \
70 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
74 #define RTA_TAIL(rta) \
75 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
78 #ifndef RTNL_FAMILY_IP6MR
79 #define RTNL_FAMILY_IP6MR 129
82 #ifndef RTPROT_MROUTED
83 #define RTPROT_MROUTED 17
86 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
87 {RTM_DELROUTE
, "RTM_DELROUTE"},
88 {RTM_GETROUTE
, "RTM_GETROUTE"},
89 {RTM_NEWLINK
, "RTM_NEWLINK"},
90 {RTM_DELLINK
, "RTM_DELLINK"},
91 {RTM_GETLINK
, "RTM_GETLINK"},
92 {RTM_NEWADDR
, "RTM_NEWADDR"},
93 {RTM_DELADDR
, "RTM_DELADDR"},
94 {RTM_GETADDR
, "RTM_GETADDR"},
95 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
96 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
97 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
98 {RTM_NEWRULE
, "RTM_NEWRULE"},
99 {RTM_DELRULE
, "RTM_DELRULE"},
100 {RTM_GETRULE
, "RTM_GETRULE"},
103 static const struct message rtproto_str
[] = {
104 {RTPROT_REDIRECT
, "redirect"},
105 {RTPROT_KERNEL
, "kernel"},
106 {RTPROT_BOOT
, "boot"},
107 {RTPROT_STATIC
, "static"},
108 {RTPROT_GATED
, "GateD"},
109 {RTPROT_RA
, "router advertisement"},
111 {RTPROT_ZEBRA
, "Zebra"},
113 {RTPROT_BIRD
, "BIRD"},
114 #endif /* RTPROT_BIRD */
115 {RTPROT_MROUTED
, "mroute"},
117 {RTPROT_OSPF
, "OSPF"},
118 {RTPROT_ISIS
, "IS-IS"},
120 {RTPROT_RIPNG
, "RIPNG"},
121 {RTPROT_ZSTATIC
, "static"},
124 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
126 {AF_BRIDGE
, "bridge"},
127 {RTNL_FAMILY_IPMR
, "ipv4MR"},
128 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
131 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
132 {RTN_MULTICAST
, "multicast"},
135 extern struct thread_master
*master
;
136 extern uint32_t nl_rcvbufsize
;
138 extern struct zebra_privs_t zserv_privs
;
141 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
144 * This is an error condition that must be handled during
147 * The netlink_talk_filter function is used for communication
148 * down the netlink_cmd pipe and we are expecting
149 * an ack being received. So if we get here
150 * then we did not receive the ack and instead
151 * received some other message in an unexpected
154 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
155 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
159 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
162 socklen_t newlen
= sizeof(newsize
);
163 socklen_t oldlen
= sizeof(oldsize
);
166 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
168 flog_err_sys(EC_LIB_SOCKET
,
169 "Can't get %s receive buffer size: %s", nl
->name
,
170 safe_strerror(errno
));
174 /* Try force option (linux >= 2.6.14) and fall back to normal set */
175 frr_elevate_privs(&zserv_privs
) {
176 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
178 sizeof(nl_rcvbufsize
));
181 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
182 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
184 flog_err_sys(EC_LIB_SOCKET
,
185 "Can't set %s receive buffer size: %s", nl
->name
,
186 safe_strerror(errno
));
190 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
192 flog_err_sys(EC_LIB_SOCKET
,
193 "Can't get %s receive buffer size: %s", nl
->name
,
194 safe_strerror(errno
));
198 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
203 /* Make socket for Linux netlink interface. */
204 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
208 struct sockaddr_nl snl
;
212 frr_elevate_privs(&zserv_privs
) {
213 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
215 zlog_err("Can't open %s socket: %s", nl
->name
,
216 safe_strerror(errno
));
220 memset(&snl
, 0, sizeof snl
);
221 snl
.nl_family
= AF_NETLINK
;
222 snl
.nl_groups
= groups
;
224 /* Bind the socket to the netlink structure for anything. */
225 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
229 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
230 snl
.nl_groups
, safe_strerror(errno
));
235 /* multiple netlink sockets will have different nl_pid */
236 namelen
= sizeof snl
;
237 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
238 if (ret
< 0 || namelen
!= sizeof snl
) {
239 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
240 nl
->name
, safe_strerror(errno
));
250 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
254 * When we handle new message types here
255 * because we are starting to install them
256 * then lets check the netlink_install_filter
257 * and see if we should add the corresponding
258 * allow through entry there.
259 * Probably not needed to do but please
262 switch (h
->nlmsg_type
) {
264 return netlink_route_change(h
, ns_id
, startup
);
266 return netlink_route_change(h
, ns_id
, startup
);
268 return netlink_link_change(h
, ns_id
, startup
);
270 return netlink_link_change(h
, ns_id
, startup
);
272 return netlink_interface_addr(h
, ns_id
, startup
);
274 return netlink_interface_addr(h
, ns_id
, startup
);
276 return netlink_neigh_change(h
, ns_id
);
278 return netlink_neigh_change(h
, ns_id
);
280 return netlink_rule_change(h
, ns_id
, startup
);
282 return netlink_rule_change(h
, ns_id
, startup
);
285 * If we have received this message then
286 * we have made a mistake during development
287 * and we need to write some code to handle
288 * this message type or not ask for
289 * it to be sent up to us
291 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
292 "Unknown netlink nlmsg_type %s(%d) vrf %u\n",
293 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
300 #if defined(HANDLE_NETLINK_FUZZING)
301 /* Using globals here to avoid adding function parameters */
303 /* Keep distinct filenames for netlink fuzzy collection */
304 static unsigned int netlink_file_counter
= 1;
306 /* File name to read fuzzed netlink from */
307 static char netlink_fuzz_file
[MAXPATHLEN
] = "";
309 /* Flag for whether to read from file or not */
313 * netlink_read_init() - Starts the message parser
314 * @fname: Filename to read.
316 void netlink_read_init(const char *fname
)
318 struct zebra_dplane_info dp_info
;
320 snprintf(netlink_fuzz_file
, MAXPATHLEN
, "%s", fname
);
321 /* Creating this fake socket for testing purposes */
322 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
324 /* Capture key info from zns struct */
325 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
327 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
,
332 * netlink_write_incoming() - Writes all data received from netlink to a file
333 * @buf: Data from netlink.
334 * @size: Size of data.
335 * @counter: Counter for keeping filenames distinct.
337 static void netlink_write_incoming(const char *buf
, const unsigned int size
,
338 unsigned int counter
)
340 char fname
[MAXPATHLEN
];
343 snprintf(fname
, MAXPATHLEN
, "%s/%s_%u", DAEMON_VTY_DIR
, "netlink",
345 frr_elevate_privs(&zserv_privs
) {
346 f
= fopen(fname
, "w");
349 fwrite(buf
, 1, size
, f
);
355 * netlink_read_file() - Reads netlink data from file
356 * @buf: Netlink buffer being overwritten.
357 * @fname: File name to read from.
359 * Return: Size of file.
361 static long netlink_read_file(char *buf
, const char *fname
)
364 long file_bytes
= -1;
366 frr_elevate_privs(&zserv_privs
) {
367 f
= fopen(fname
, "r");
370 fseek(f
, 0, SEEK_END
);
371 file_bytes
= ftell(f
);
373 fread(buf
, NL_RCV_PKT_BUF_SIZE
, 1, f
);
379 #endif /* HANDLE_NETLINK_FUZZING */
381 static int kernel_read(struct thread
*thread
)
383 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
384 struct zebra_dplane_info dp_info
;
386 /* Capture key info from ns struct */
387 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
389 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
391 zns
->t_netlink
= NULL
;
392 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
399 * Filter out messages from self that occur on listener socket,
400 * caused by our actions on the command socket(s)
402 * When we add new Netlink message types we probably
403 * do not need to add them here as that we are filtering
404 * on the routes we actually care to receive( which is rarer
405 * then the normal course of operations). We are intentionally
406 * allowing some messages from ourselves through
407 * ( I'm looking at you Interface based netlink messages )
408 * so that we only had to write one way to handle incoming
409 * address add/delete changes.
411 static void netlink_install_filter(int sock
, __u32 pid
, __u32 dplane_pid
)
414 * BPF_JUMP instructions and where you jump to are based upon
415 * 0 as being the next statement. So count from 0. Writing
416 * this down because every time I look at this I have to
419 struct sock_filter filter
[] = {
422 * if (nlmsg_pid == pid ||
423 * nlmsg_pid == dplane_pid) {
424 * if (the incoming nlmsg_type ==
425 * RTM_NEWADDR | RTM_DELADDR)
430 * keep this netlink message
433 * 0: Load the nlmsg_pid into the BPF register
435 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
436 offsetof(struct nlmsghdr
, nlmsg_pid
)),
440 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
442 * 2: Compare to dplane pid
444 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 4),
446 * 3: Load the nlmsg_type into BPF register
448 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
449 offsetof(struct nlmsghdr
, nlmsg_type
)),
451 * 4: Compare to RTM_NEWADDR
453 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 2, 0),
455 * 5: Compare to RTM_DELADDR
457 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 1, 0),
459 * 6: This is the end state of we want to skip the
462 BPF_STMT(BPF_RET
| BPF_K
, 0),
463 /* 7: This is the end state of we want to keep
466 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
469 struct sock_fprog prog
= {
470 .len
= array_size(filter
), .filter
= filter
,
473 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
475 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s\n",
476 safe_strerror(errno
));
479 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
482 while (RTA_OK(rta
, len
)) {
483 if (rta
->rta_type
<= max
)
484 tb
[rta
->rta_type
] = rta
;
485 rta
= RTA_NEXT(rta
, len
);
490 * netlink_parse_rtattr_nested() - Parses a nested route attribute
491 * @tb: Pointer to array for storing rtattr in.
492 * @max: Max number to store.
493 * @rta: Pointer to rtattr to look for nested items in.
495 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
498 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
501 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
502 const void *data
, unsigned int alen
)
507 len
= RTA_LENGTH(alen
);
509 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
512 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
513 rta
->rta_type
= type
;
517 memcpy(RTA_DATA(rta
), data
, alen
);
521 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
526 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
,
527 const void *data
, unsigned int alen
)
530 struct rtattr
*subrta
;
532 len
= RTA_LENGTH(alen
);
534 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
537 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
538 subrta
->rta_type
= type
;
539 subrta
->rta_len
= len
;
542 memcpy(RTA_DATA(subrta
), data
, alen
);
546 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
551 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
553 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
556 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
558 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
561 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
563 struct rtattr
*nest
= NLMSG_TAIL(n
);
565 addattr_l(n
, maxlen
, type
, NULL
, 0);
569 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
571 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
575 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
577 struct rtattr
*nest
= RTA_TAIL(rta
);
579 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
583 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
585 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
589 const char *nl_msg_type_to_str(uint16_t msg_type
)
591 return lookup_msg(nlmsg_str
, msg_type
, "");
594 const char *nl_rtproto_to_str(uint8_t rtproto
)
596 return lookup_msg(rtproto_str
, rtproto
, "");
599 const char *nl_family_to_str(uint8_t family
)
601 return lookup_msg(family_str
, family
, "");
604 const char *nl_rttype_to_str(uint8_t rttype
)
606 return lookup_msg(rttype_str
, rttype
, "");
609 #define NL_OK(nla, len) \
610 ((len) >= (int)sizeof(struct nlattr) \
611 && (nla)->nla_len >= sizeof(struct nlattr) \
612 && (nla)->nla_len <= (len))
613 #define NL_NEXT(nla, attrlen) \
614 ((attrlen) -= RTA_ALIGN((nla)->nla_len), \
615 (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len)))
617 ((struct nlattr *)(((char *)(r)) \
618 + NLMSG_ALIGN(sizeof(struct nlmsgerr))))
620 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
621 struct nlattr
*nla
, int len
)
623 while (NL_OK(nla
, len
)) {
624 if (nla
->nla_type
<= max
)
625 tb
[nla
->nla_type
] = nla
;
626 nla
= NL_NEXT(nla
, len
);
630 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
632 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
633 const struct nlmsgerr
*err
=
634 (const struct nlmsgerr
*)((uint8_t *)h
636 sizeof(struct nlmsghdr
)));
637 const struct nlmsghdr
*err_nlh
= NULL
;
638 uint32_t hlen
= sizeof(*err
);
639 const char *msg
= NULL
;
642 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
643 hlen
+= h
->nlmsg_len
- NLMSG_ALIGN(sizeof(struct nlmsghdr
));
645 memset(tb
, 0, sizeof(tb
));
646 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, NL_RTA(h
), hlen
);
648 if (tb
[NLMSGERR_ATTR_MSG
])
649 msg
= (const char *)RTA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
651 if (tb
[NLMSGERR_ATTR_OFFS
]) {
652 off
= *(uint32_t *)RTA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
654 if (off
> h
->nlmsg_len
) {
655 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n");
656 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
658 * Header of failed message
659 * we are not doing anything currently with it
660 * but noticing it for later.
663 zlog_debug("%s: Received %s extended Ack",
665 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
669 if (msg
&& *msg
!= '\0') {
670 bool is_err
= !!err
->error
;
673 zlog_err("Extended Error: %s", msg
);
675 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
676 "Extended Warning: %s", msg
);
683 * Receive message from netlink interface and pass those information
684 * to the given function.
686 * filter -> Function to call to read the results
687 * nl -> netlink socket information
688 * zns -> The zebra namespace data
689 * count -> How many we should read in, 0 means as much as possible
690 * startup -> Are we reading in under startup conditions? passed to
693 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
694 const struct nlsock
*nl
,
695 const struct zebra_dplane_info
*zns
,
696 int count
, int startup
)
704 char buf
[NL_RCV_PKT_BUF_SIZE
];
705 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
706 struct sockaddr_nl snl
;
707 struct msghdr msg
= {.msg_name
= (void *)&snl
,
708 .msg_namelen
= sizeof snl
,
713 if (count
&& read_in
>= count
)
716 #if defined(HANDLE_NETLINK_FUZZING)
717 /* Check if reading and filename is set */
718 if (netlink_read
&& '\0' != netlink_fuzz_file
[0]) {
719 zlog_debug("Reading netlink fuzz file");
720 status
= netlink_read_file(buf
, netlink_fuzz_file
);
723 status
= recvmsg(nl
->sock
, &msg
, 0);
726 status
= recvmsg(nl
->sock
, &msg
, 0);
727 #endif /* HANDLE_NETLINK_FUZZING */
731 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
733 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
,
734 "%s recvmsg overrun: %s", nl
->name
,
735 safe_strerror(errno
));
737 * In this case we are screwed.
738 * There is no good way to
739 * recover zebra at this point.
746 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
750 if (msg
.msg_namelen
!= sizeof snl
) {
751 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
752 "%s sender address length error: length %d",
753 nl
->name
, msg
.msg_namelen
);
757 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
758 zlog_debug("%s: << netlink message dump [recv]",
760 zlog_hexdump(buf
, status
);
763 #if defined(HANDLE_NETLINK_FUZZING)
765 zlog_debug("Writing incoming netlink message");
766 netlink_write_incoming(buf
, status
,
767 netlink_file_counter
++);
769 #endif /* HANDLE_NETLINK_FUZZING */
772 for (h
= (struct nlmsghdr
*)buf
;
773 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
774 h
= NLMSG_NEXT(h
, status
)) {
775 /* Finish of reading. */
776 if (h
->nlmsg_type
== NLMSG_DONE
)
779 /* Error handling. */
780 if (h
->nlmsg_type
== NLMSG_ERROR
) {
781 struct nlmsgerr
*err
=
782 (struct nlmsgerr
*)NLMSG_DATA(h
);
783 int errnum
= err
->error
;
784 int msg_type
= err
->msg
.nlmsg_type
;
787 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
788 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
789 "%s error: message truncated",
795 * Parse the extended information before
796 * we actually handle it.
797 * At this point in time we do not
798 * do anything other than report the
801 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
802 netlink_parse_extended_ack(h
);
804 /* If the error field is zero, then this is an
806 if (err
->error
== 0) {
807 if (IS_ZEBRA_DEBUG_KERNEL
) {
809 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
810 __FUNCTION__
, nl
->name
,
812 err
->msg
.nlmsg_type
),
818 /* return if not a multipart message,
819 * otherwise continue */
820 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
825 /* Deal with errors that occur because of races
826 * in link handling */
828 && ((msg_type
== RTM_DELROUTE
829 && (-errnum
== ENODEV
830 || -errnum
== ESRCH
))
831 || (msg_type
== RTM_NEWROUTE
832 && (-errnum
== ENETDOWN
833 || -errnum
== EEXIST
)))) {
834 if (IS_ZEBRA_DEBUG_KERNEL
)
836 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
838 safe_strerror(-errnum
),
847 /* We see RTM_DELNEIGH when shutting down an
848 * interface with an IPv4
849 * link-local. The kernel should have already
850 * deleted the neighbor
851 * so do not log these as an error.
853 if (msg_type
== RTM_DELNEIGH
854 || (zns
->is_cmd
&& msg_type
== RTM_NEWROUTE
856 || -errnum
== ENETUNREACH
))) {
857 /* This is known to happen in some
858 * situations, don't log
861 if (IS_ZEBRA_DEBUG_KERNEL
)
863 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
865 safe_strerror(-errnum
),
873 EC_ZEBRA_UNEXPECTED_MESSAGE
,
874 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
876 safe_strerror(-errnum
),
877 nl_msg_type_to_str(msg_type
),
878 msg_type
, err
->msg
.nlmsg_seq
,
884 /* OK we got netlink message. */
885 if (IS_ZEBRA_DEBUG_KERNEL
)
887 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
889 nl_msg_type_to_str(h
->nlmsg_type
),
890 h
->nlmsg_type
, h
->nlmsg_len
,
891 h
->nlmsg_seq
, h
->nlmsg_pid
);
895 * Ignore messages that maybe sent from
896 * other actors besides the kernel
898 if (snl
.nl_pid
!= 0) {
899 zlog_debug("Ignoring message from pid %u",
904 error
= (*filter
)(h
, zns
->ns_id
, startup
);
906 zlog_debug("%s filter function error",
912 /* After error care. */
913 if (msg
.msg_flags
& MSG_TRUNC
) {
914 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
915 "%s error: message truncated", nl
->name
);
919 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
920 "%s error: data remnant size %d", nl
->name
,
931 * sendmsg() to netlink socket then recvmsg().
932 * Calls netlink_parse_info to parse returned data
934 * filter -> The filter to read final results from kernel
935 * nlmsghdr -> The data to send to the kernel
936 * dp_info -> The dataplane and netlink socket information
937 * startup -> Are we reading in under startup conditions
938 * This is passed through eventually to filter.
940 int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
942 const struct zebra_dplane_info
*dp_info
, int startup
)
945 struct sockaddr_nl snl
;
949 const struct nlsock
*nl
;
951 memset(&snl
, 0, sizeof snl
);
952 memset(&iov
, 0, sizeof iov
);
953 memset(&msg
, 0, sizeof msg
);
956 iov
.iov_len
= n
->nlmsg_len
;
957 msg
.msg_name
= (void *)&snl
;
958 msg
.msg_namelen
= sizeof snl
;
962 snl
.nl_family
= AF_NETLINK
;
964 nl
= &(dp_info
->nls
);
965 n
->nlmsg_seq
= nl
->seq
;
966 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
968 if (IS_ZEBRA_DEBUG_KERNEL
)
970 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
971 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
972 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
975 /* Send message to netlink interface. */
976 frr_elevate_privs(&zserv_privs
) {
977 status
= sendmsg(nl
->sock
, &msg
, 0);
981 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
982 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
983 zlog_hexdump(n
, n
->nlmsg_len
);
987 flog_err_sys(EC_LIB_SOCKET
, "netlink_talk sendmsg() error: %s",
988 safe_strerror(save_errno
));
993 * Get reply from netlink socket.
994 * The reply should either be an acknowlegement or an error.
996 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1000 * Synchronous version of netlink_talk_info. Converts args to suit the
1001 * common version, which is suitable for both sync and async use.
1003 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1004 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1007 struct zebra_dplane_info dp_info
;
1009 /* Increment sequence number before capturing snapshot of ns socket
1014 /* Capture info in intermediate info struct */
1015 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1017 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1020 /* Issue request message to kernel via netlink socket. GET messages
1021 * are issued through this interface.
1023 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
1026 struct sockaddr_nl snl
;
1028 /* Check netlink socket. */
1030 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1035 /* Fill common fields for all requests. */
1036 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1037 n
->nlmsg_seq
= ++nl
->seq
;
1039 memset(&snl
, 0, sizeof snl
);
1040 snl
.nl_family
= AF_NETLINK
;
1042 /* Raise capabilities and send message, then lower capabilities. */
1043 frr_elevate_privs(&zserv_privs
) {
1044 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
1045 (struct sockaddr
*)&snl
, sizeof snl
);
1049 zlog_err("%s sendto failed: %s", nl
->name
,
1050 safe_strerror(errno
));
1057 /* Exported interface function. This function simply calls
1058 netlink_socket (). */
1059 void kernel_init(struct zebra_ns
*zns
)
1061 unsigned long groups
;
1062 #if defined SOL_NETLINK
1067 * Initialize netlink sockets
1069 * If RTMGRP_XXX exists use that, but at some point
1070 * I think the kernel developers realized that
1071 * keeping track of all the different values would
1072 * lead to confusion, so we need to convert the
1073 * RTNLGRP_XXX to a bit position for ourself
1075 groups
= RTMGRP_LINK
|
1077 RTMGRP_IPV4_IFADDR
|
1079 RTMGRP_IPV6_IFADDR
|
1080 RTMGRP_IPV4_MROUTE
|
1082 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
1083 (1 << (RTNLGRP_IPV6_RULE
- 1));
1085 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1086 "netlink-listen (NS %u)", zns
->ns_id
);
1087 zns
->netlink
.sock
= -1;
1088 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1089 zlog_err("Failure to create %s socket",
1094 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1095 "netlink-cmd (NS %u)", zns
->ns_id
);
1096 zns
->netlink_cmd
.sock
= -1;
1097 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1098 zlog_err("Failure to create %s socket",
1099 zns
->netlink_cmd
.name
);
1103 snprintf(zns
->netlink_dplane
.name
, sizeof(zns
->netlink_dplane
.name
),
1104 "netlink-dp (NS %u)", zns
->ns_id
);
1105 zns
->netlink_dplane
.sock
= -1;
1106 if (netlink_socket(&zns
->netlink_dplane
, 0, zns
->ns_id
) < 0) {
1107 zlog_err("Failure to create %s socket",
1108 zns
->netlink_dplane
.name
);
1113 * SOL_NETLINK is not available on all platforms yet
1114 * apparently. It's in bits/socket.h which I am not
1115 * sure that we want to pull into our build system.
1117 #if defined SOL_NETLINK
1119 * Let's tell the kernel that we want to receive extended
1120 * ACKS over our command socket(s)
1123 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1127 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1128 errno
, safe_strerror(errno
));
1131 ret
= setsockopt(zns
->netlink_dplane
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1135 zlog_notice("Registration for extended dp ACK failed : %d %s",
1136 errno
, safe_strerror(errno
));
1139 /* Register kernel socket. */
1140 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1141 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1142 zns
->netlink
.name
, safe_strerror(errno
));
1144 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1145 zlog_err("Can't set %s socket error: %s(%d)",
1146 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1148 if (fcntl(zns
->netlink_dplane
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1149 zlog_err("Can't set %s socket error: %s(%d)",
1150 zns
->netlink_dplane
.name
, safe_strerror(errno
), errno
);
1152 /* Set receive buffer size if it's set from command line */
1154 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
1156 netlink_install_filter(zns
->netlink
.sock
,
1157 zns
->netlink_cmd
.snl
.nl_pid
,
1158 zns
->netlink_dplane
.snl
.nl_pid
);
1160 zns
->t_netlink
= NULL
;
1162 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1163 zns
->netlink
.sock
, &zns
->t_netlink
);
1168 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1170 THREAD_READ_OFF(zns
->t_netlink
);
1172 if (zns
->netlink
.sock
>= 0) {
1173 close(zns
->netlink
.sock
);
1174 zns
->netlink
.sock
= -1;
1177 if (zns
->netlink_cmd
.sock
>= 0) {
1178 close(zns
->netlink_cmd
.sock
);
1179 zns
->netlink_cmd
.sock
= -1;
1182 /* During zebra shutdown, we need to leave the dataplane socket
1183 * around until all work is done.
1186 if (zns
->netlink_dplane
.sock
>= 0) {
1187 close(zns
->netlink_dplane
.sock
);
1188 zns
->netlink_dplane
.sock
= -1;
1192 #endif /* HAVE_NETLINK */