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_ns.h"
48 #include "zebra/zebra_vrf.h"
50 #include "zebra/debug.h"
51 #include "zebra/kernel_netlink.h"
52 #include "zebra/rt_netlink.h"
53 #include "zebra/if_netlink.h"
54 #include "zebra/rule_netlink.h"
55 #include "zebra/zebra_errors.h"
57 #ifndef SO_RCVBUFFORCE
58 #define SO_RCVBUFFORCE (33)
61 /* Hack for GNU libc version 2. */
63 #define MSG_TRUNC 0x20
64 #endif /* MSG_TRUNC */
67 #define NLMSG_TAIL(nmsg) \
68 ((struct rtattr *)(((uint8_t *)(nmsg)) \
69 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
73 #define RTA_TAIL(rta) \
74 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
77 #ifndef RTNL_FAMILY_IP6MR
78 #define RTNL_FAMILY_IP6MR 129
81 #ifndef RTPROT_MROUTED
82 #define RTPROT_MROUTED 17
85 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
86 {RTM_DELROUTE
, "RTM_DELROUTE"},
87 {RTM_GETROUTE
, "RTM_GETROUTE"},
88 {RTM_NEWLINK
, "RTM_NEWLINK"},
89 {RTM_DELLINK
, "RTM_DELLINK"},
90 {RTM_GETLINK
, "RTM_GETLINK"},
91 {RTM_NEWADDR
, "RTM_NEWADDR"},
92 {RTM_DELADDR
, "RTM_DELADDR"},
93 {RTM_GETADDR
, "RTM_GETADDR"},
94 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
95 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
96 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
97 {RTM_NEWRULE
, "RTM_NEWRULE"},
98 {RTM_DELRULE
, "RTM_DELRULE"},
99 {RTM_GETRULE
, "RTM_GETRULE"},
102 static const struct message rtproto_str
[] = {
103 {RTPROT_REDIRECT
, "redirect"},
104 {RTPROT_KERNEL
, "kernel"},
105 {RTPROT_BOOT
, "boot"},
106 {RTPROT_STATIC
, "static"},
107 {RTPROT_GATED
, "GateD"},
108 {RTPROT_RA
, "router advertisement"},
110 {RTPROT_ZEBRA
, "Zebra"},
112 {RTPROT_BIRD
, "BIRD"},
113 #endif /* RTPROT_BIRD */
114 {RTPROT_MROUTED
, "mroute"},
116 {RTPROT_OSPF
, "OSPF"},
117 {RTPROT_ISIS
, "IS-IS"},
119 {RTPROT_RIPNG
, "RIPNG"},
120 {RTPROT_ZSTATIC
, "static"},
123 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
125 {AF_BRIDGE
, "bridge"},
126 {RTNL_FAMILY_IPMR
, "ipv4MR"},
127 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
130 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
131 {RTN_MULTICAST
, "multicast"},
134 extern struct thread_master
*master
;
135 extern uint32_t nl_rcvbufsize
;
137 extern struct zebra_privs_t zserv_privs
;
139 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
142 * This is an error condition that must be handled during
145 * The netlink_talk_filter function is used for communication
146 * down the netlink_cmd pipe and we are expecting
147 * an ack being received. So if we get here
148 * then we did not receive the ack and instead
149 * received some other message in an unexpected
152 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
153 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
157 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
160 socklen_t newlen
= sizeof(newsize
);
161 socklen_t oldlen
= sizeof(oldsize
);
164 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
166 flog_err_sys(LIB_ERR_SOCKET
,
167 "Can't get %s receive buffer size: %s", nl
->name
,
168 safe_strerror(errno
));
172 /* Try force option (linux >= 2.6.14) and fall back to normal set */
173 frr_elevate_privs(&zserv_privs
) {
174 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
176 sizeof(nl_rcvbufsize
));
179 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
180 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
182 flog_err_sys(LIB_ERR_SOCKET
,
183 "Can't set %s receive buffer size: %s", nl
->name
,
184 safe_strerror(errno
));
188 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
190 flog_err_sys(LIB_ERR_SOCKET
,
191 "Can't get %s receive buffer size: %s", nl
->name
,
192 safe_strerror(errno
));
196 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
201 /* Make socket for Linux netlink interface. */
202 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
206 struct sockaddr_nl snl
;
210 frr_elevate_privs(&zserv_privs
) {
211 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
213 zlog_err("Can't open %s socket: %s", nl
->name
,
214 safe_strerror(errno
));
218 memset(&snl
, 0, sizeof snl
);
219 snl
.nl_family
= AF_NETLINK
;
220 snl
.nl_groups
= groups
;
222 /* Bind the socket to the netlink structure for anything. */
223 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
227 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
228 snl
.nl_groups
, safe_strerror(errno
));
233 /* multiple netlink sockets will have different nl_pid */
234 namelen
= sizeof snl
;
235 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
236 if (ret
< 0 || namelen
!= sizeof snl
) {
237 flog_err_sys(LIB_ERR_SOCKET
, "Can't get %s socket name: %s",
238 nl
->name
, safe_strerror(errno
));
248 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
252 * When we handle new message types here
253 * because we are starting to install them
254 * then lets check the netlink_install_filter
255 * and see if we should add the corresponding
256 * allow through entry there.
257 * Probably not needed to do but please
260 switch (h
->nlmsg_type
) {
262 return netlink_route_change(h
, ns_id
, startup
);
264 return netlink_route_change(h
, ns_id
, startup
);
266 return netlink_link_change(h
, ns_id
, startup
);
268 return netlink_link_change(h
, ns_id
, startup
);
270 return netlink_interface_addr(h
, ns_id
, startup
);
272 return netlink_interface_addr(h
, ns_id
, startup
);
274 return netlink_neigh_change(h
, ns_id
);
276 return netlink_neigh_change(h
, ns_id
);
278 return netlink_rule_change(h
, ns_id
, startup
);
280 return netlink_rule_change(h
, ns_id
, startup
);
283 * If we have received this message then
284 * we have made a mistake during development
285 * and we need to write some code to handle
286 * this message type or not ask for
287 * it to be sent up to us
289 flog_err(ZEBRA_ERR_UNKNOWN_NLMSG
,
290 "Unknown netlink nlmsg_type %s(%d) vrf %u\n",
291 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
298 #if defined(HANDLE_NETLINK_FUZZING)
299 /* Using globals here to avoid adding function parameters */
301 /* Keep distinct filenames for netlink fuzzy collection */
302 static unsigned int netlink_file_counter
= 1;
304 /* File name to read fuzzed netlink from */
305 static char netlink_fuzz_file
[MAXPATHLEN
] = "";
307 /* Flag for whether to read from file or not */
311 * netlink_read_init() - Starts the message parser
312 * @fname: Filename to read.
314 void netlink_read_init(const char *fname
)
316 snprintf(netlink_fuzz_file
, MAXPATHLEN
, "%s", fname
);
317 /* Creating this fake socket for testing purposes */
318 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
320 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 1, 0);
324 * netlink_write_incoming() - Writes all data received from netlink to a file
325 * @buf: Data from netlink.
326 * @size: Size of data.
327 * @counter: Counter for keeping filenames distinct.
329 static void netlink_write_incoming(const char *buf
, const unsigned int size
,
330 unsigned int counter
)
332 char fname
[MAXPATHLEN
];
335 snprintf(fname
, MAXPATHLEN
, "%s/%s_%u", DAEMON_VTY_DIR
, "netlink",
337 frr_elevate_privs(&zserv_privs
) {
338 f
= fopen(fname
, "w");
341 fwrite(buf
, 1, size
, f
);
347 * netlink_read_file() - Reads netlink data from file
348 * @buf: Netlink buffer being overwritten.
349 * @fname: File name to read from.
351 * Return: Size of file.
353 static long netlink_read_file(char *buf
, const char *fname
)
356 long file_bytes
= -1;
358 frr_elevate_privs(&zserv_privs
) {
359 f
= fopen(fname
, "r");
362 fseek(f
, 0, SEEK_END
);
363 file_bytes
= ftell(f
);
365 fread(buf
, NL_RCV_PKT_BUF_SIZE
, 1, f
);
371 #endif /* HANDLE_NETLINK_FUZZING */
373 static int kernel_read(struct thread
*thread
)
375 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
376 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
377 zns
->t_netlink
= NULL
;
378 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
385 * Filter out messages from self that occur on listener socket,
386 * caused by our actions on the command socket
388 * When we add new Netlink message types we probably
389 * do not need to add them here as that we are filtering
390 * on the routes we actually care to receive( which is rarer
391 * then the normal course of operations). We are intentionally
392 * allowing some messages from ourselves through
393 * ( I'm looking at you Interface based netlink messages )
394 * so that we only had to write one way to handle incoming
395 * address add/delete changes.
397 static void netlink_install_filter(int sock
, __u32 pid
)
400 * BPF_JUMP instructions and where you jump to are based upon
401 * 0 as being the next statement. So count from 0. Writing
402 * this down because every time I look at this I have to
405 struct sock_filter filter
[] = {
408 * if (nlmsg_pid == pid) {
409 * if (the incoming nlmsg_type ==
410 * RTM_NEWADDR | RTM_DELADDR)
415 * keep this netlink message
418 * 0: Load the nlmsg_pid into the BPF register
420 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
421 offsetof(struct nlmsghdr
, nlmsg_pid
)),
425 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 4),
427 * 2: Load the nlmsg_type into BPF register
429 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
430 offsetof(struct nlmsghdr
, nlmsg_type
)),
432 * 3: Compare to RTM_NEWADDR
434 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 2, 0),
436 * 4: Compare to RTM_DELADDR
438 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 1, 0),
440 * 5: This is the end state of we want to skip the
443 BPF_STMT(BPF_RET
| BPF_K
, 0),
444 /* 6: This is the end state of we want to keep
447 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
450 struct sock_fprog prog
= {
451 .len
= array_size(filter
), .filter
= filter
,
454 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
456 zlog_warn("Can't install socket filter: %s\n",
457 safe_strerror(errno
));
460 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
463 while (RTA_OK(rta
, len
)) {
464 if (rta
->rta_type
<= max
)
465 tb
[rta
->rta_type
] = rta
;
466 rta
= RTA_NEXT(rta
, len
);
471 * netlink_parse_rtattr_nested() - Parses a nested route attribute
472 * @tb: Pointer to array for storing rtattr in.
473 * @max: Max number to store.
474 * @rta: Pointer to rtattr to look for nested items in.
476 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
479 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
482 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
483 const void *data
, unsigned int alen
)
488 len
= RTA_LENGTH(alen
);
490 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
493 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
494 rta
->rta_type
= type
;
498 memcpy(RTA_DATA(rta
), data
, alen
);
502 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
507 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
,
508 const void *data
, unsigned int alen
)
511 struct rtattr
*subrta
;
513 len
= RTA_LENGTH(alen
);
515 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
518 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
519 subrta
->rta_type
= type
;
520 subrta
->rta_len
= len
;
523 memcpy(RTA_DATA(subrta
), data
, alen
);
527 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
532 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
534 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
537 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
539 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
542 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
544 struct rtattr
*nest
= NLMSG_TAIL(n
);
546 addattr_l(n
, maxlen
, type
, NULL
, 0);
550 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
552 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
556 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
558 struct rtattr
*nest
= RTA_TAIL(rta
);
560 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
564 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
566 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
570 const char *nl_msg_type_to_str(uint16_t msg_type
)
572 return lookup_msg(nlmsg_str
, msg_type
, "");
575 const char *nl_rtproto_to_str(uint8_t rtproto
)
577 return lookup_msg(rtproto_str
, rtproto
, "");
580 const char *nl_family_to_str(uint8_t family
)
582 return lookup_msg(family_str
, family
, "");
585 const char *nl_rttype_to_str(uint8_t rttype
)
587 return lookup_msg(rttype_str
, rttype
, "");
590 #define NL_OK(nla, len) \
591 ((len) >= (int)sizeof(struct nlattr) \
592 && (nla)->nla_len >= sizeof(struct nlattr) \
593 && (nla)->nla_len <= (len))
594 #define NL_NEXT(nla, attrlen) \
595 ((attrlen) -= RTA_ALIGN((nla)->nla_len), \
596 (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len)))
598 ((struct nlattr *)(((char *)(r)) \
599 + NLMSG_ALIGN(sizeof(struct nlmsgerr))))
601 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
602 struct nlattr
*nla
, int len
)
604 while (NL_OK(nla
, len
)) {
605 if (nla
->nla_type
<= max
)
606 tb
[nla
->nla_type
] = nla
;
607 nla
= NL_NEXT(nla
, len
);
611 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
613 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
614 const struct nlmsgerr
*err
=
615 (const struct nlmsgerr
*)((uint8_t *)h
617 sizeof(struct nlmsghdr
)));
618 const struct nlmsghdr
*err_nlh
= NULL
;
619 uint32_t hlen
= sizeof(*err
);
620 const char *msg
= NULL
;
623 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
624 hlen
+= h
->nlmsg_len
- NLMSG_ALIGN(sizeof(struct nlmsghdr
));
626 memset(tb
, 0, sizeof(tb
));
627 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, NL_RTA(h
), hlen
);
629 if (tb
[NLMSGERR_ATTR_MSG
])
630 msg
= (const char *)RTA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
632 if (tb
[NLMSGERR_ATTR_OFFS
]) {
633 off
= *(uint32_t *)RTA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
635 if (off
> h
->nlmsg_len
) {
636 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n");
637 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
639 * Header of failed message
640 * we are not doing anything currently with it
641 * but noticing it for later.
644 zlog_warn("%s: Received %d extended Ack",
645 __PRETTY_FUNCTION__
, err_nlh
->nlmsg_type
);
649 if (msg
&& *msg
!= '\0') {
650 bool is_err
= !!err
->error
;
653 zlog_err("Extended Error: %s", msg
);
655 zlog_warn("Extended Warning: %s", msg
);
662 * Receive message from netlink interface and pass those information
663 * to the given function.
665 * filter -> Function to call to read the results
666 * nl -> netlink socket information
667 * zns -> The zebra namespace data
668 * count -> How many we should read in, 0 means as much as possible
669 * startup -> Are we reading in under startup conditions? passed to
672 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
673 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
682 char buf
[NL_RCV_PKT_BUF_SIZE
];
683 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
684 struct sockaddr_nl snl
;
685 struct msghdr msg
= {.msg_name
= (void *)&snl
,
686 .msg_namelen
= sizeof snl
,
691 if (count
&& read_in
>= count
)
694 #if defined(HANDLE_NETLINK_FUZZING)
695 /* Check if reading and filename is set */
696 if (netlink_read
&& '\0' != netlink_fuzz_file
[0]) {
697 zlog_debug("Reading netlink fuzz file");
698 status
= netlink_read_file(buf
, netlink_fuzz_file
);
701 status
= recvmsg(nl
->sock
, &msg
, 0);
704 status
= recvmsg(nl
->sock
, &msg
, 0);
705 #endif /* HANDLE_NETLINK_FUZZING */
709 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
711 flog_err(ZEBRA_ERR_RECVMSG_OVERRUN
,
712 "%s recvmsg overrun: %s", nl
->name
,
713 safe_strerror(errno
));
715 * In this case we are screwed.
716 * There is no good way to
717 * recover zebra at this point.
724 flog_err_sys(LIB_ERR_SOCKET
, "%s EOF", nl
->name
);
728 if (msg
.msg_namelen
!= sizeof snl
) {
729 flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR
,
730 "%s sender address length error: length %d",
731 nl
->name
, msg
.msg_namelen
);
735 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
736 zlog_debug("%s: << netlink message dump [recv]",
738 zlog_hexdump(buf
, status
);
741 #if defined(HANDLE_NETLINK_FUZZING)
743 zlog_debug("Writing incoming netlink message");
744 netlink_write_incoming(buf
, status
,
745 netlink_file_counter
++);
747 #endif /* HANDLE_NETLINK_FUZZING */
750 for (h
= (struct nlmsghdr
*)buf
;
751 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
752 h
= NLMSG_NEXT(h
, status
)) {
753 /* Finish of reading. */
754 if (h
->nlmsg_type
== NLMSG_DONE
)
757 /* Error handling. */
758 if (h
->nlmsg_type
== NLMSG_ERROR
) {
759 struct nlmsgerr
*err
=
760 (struct nlmsgerr
*)NLMSG_DATA(h
);
761 int errnum
= err
->error
;
762 int msg_type
= err
->msg
.nlmsg_type
;
765 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
766 zlog_err("%s error: message truncated",
772 * Parse the extended information before
773 * we actually handle it.
774 * At this point in time we do not
775 * do anything other than report the
778 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
779 netlink_parse_extended_ack(h
);
781 /* If the error field is zero, then this is an
783 if (err
->error
== 0) {
784 if (IS_ZEBRA_DEBUG_KERNEL
) {
786 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
787 __FUNCTION__
, nl
->name
,
789 err
->msg
.nlmsg_type
),
795 /* return if not a multipart message,
796 * otherwise continue */
797 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
803 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
805 ZEBRA_ERR_NETLINK_LENGTH_ERROR
,
806 "%s error: message truncated",
811 /* Deal with errors that occur because of races
812 * in link handling */
813 if (nl
== &zns
->netlink_cmd
814 && ((msg_type
== RTM_DELROUTE
815 && (-errnum
== ENODEV
816 || -errnum
== ESRCH
))
817 || (msg_type
== RTM_NEWROUTE
818 && (-errnum
== ENETDOWN
819 || -errnum
== EEXIST
)))) {
820 if (IS_ZEBRA_DEBUG_KERNEL
)
822 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
824 safe_strerror(-errnum
),
833 /* We see RTM_DELNEIGH when shutting down an
834 * interface with an IPv4
835 * link-local. The kernel should have already
836 * deleted the neighbor
837 * so do not log these as an error.
839 if (msg_type
== RTM_DELNEIGH
840 || (nl
== &zns
->netlink_cmd
841 && msg_type
== RTM_NEWROUTE
843 || -errnum
== ENETUNREACH
))) {
844 /* This is known to happen in some
845 * situations, don't log
848 if (IS_ZEBRA_DEBUG_KERNEL
)
850 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
852 safe_strerror(-errnum
),
860 ZEBRA_ERR_UNEXPECTED_MESSAGE
,
861 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
863 safe_strerror(-errnum
),
864 nl_msg_type_to_str(msg_type
),
865 msg_type
, err
->msg
.nlmsg_seq
,
871 /* OK we got netlink message. */
872 if (IS_ZEBRA_DEBUG_KERNEL
)
874 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
876 nl_msg_type_to_str(h
->nlmsg_type
),
877 h
->nlmsg_type
, h
->nlmsg_len
,
878 h
->nlmsg_seq
, h
->nlmsg_pid
);
882 * Ignore messages that maybe sent from
883 * other actors besides the kernel
885 if (snl
.nl_pid
!= 0) {
886 zlog_debug("Ignoring message from pid %u",
891 error
= (*filter
)(h
, zns
->ns_id
, startup
);
893 zlog_warn("%s filter function error", nl
->name
);
898 /* After error care. */
899 if (msg
.msg_flags
& MSG_TRUNC
) {
900 flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR
,
901 "%s error: message truncated", nl
->name
);
905 flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR
,
906 "%s error: data remnant size %d", nl
->name
,
917 * sendmsg() to netlink socket then recvmsg().
918 * Calls netlink_parse_info to parse returned data
920 * filter -> The filter to read final results from kernel
921 * nlmsghdr -> The data to send to the kernel
922 * nl -> The netlink socket information
923 * zns -> The zebra namespace information
924 * startup -> Are we reading in under startup conditions
925 * This is passed through eventually to filter.
927 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
928 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
932 struct sockaddr_nl snl
;
937 memset(&snl
, 0, sizeof snl
);
938 memset(&iov
, 0, sizeof iov
);
939 memset(&msg
, 0, sizeof msg
);
942 iov
.iov_len
= n
->nlmsg_len
;
943 msg
.msg_name
= (void *)&snl
;
944 msg
.msg_namelen
= sizeof snl
;
948 snl
.nl_family
= AF_NETLINK
;
950 n
->nlmsg_seq
= ++nl
->seq
;
951 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
953 if (IS_ZEBRA_DEBUG_KERNEL
)
955 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
956 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
957 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
960 /* Send message to netlink interface. */
961 frr_elevate_privs(&zserv_privs
) {
962 status
= sendmsg(nl
->sock
, &msg
, 0);
966 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
967 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
968 zlog_hexdump(n
, n
->nlmsg_len
);
972 flog_err_sys(LIB_ERR_SOCKET
, "netlink_talk sendmsg() error: %s",
973 safe_strerror(save_errno
));
979 * Get reply from netlink socket.
980 * The reply should either be an acknowlegement or an error.
982 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
985 /* Issue request message to kernel via netlink socket. GET messages
986 * are issued through this interface.
988 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
991 struct sockaddr_nl snl
;
993 /* Check netlink socket. */
995 flog_err_sys(LIB_ERR_SOCKET
, "%s socket isn't active.",
1000 /* Fill common fields for all requests. */
1001 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1002 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1003 n
->nlmsg_seq
= ++nl
->seq
;
1005 memset(&snl
, 0, sizeof snl
);
1006 snl
.nl_family
= AF_NETLINK
;
1008 /* Raise capabilities and send message, then lower capabilities. */
1009 frr_elevate_privs(&zserv_privs
) {
1010 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
1011 (struct sockaddr
*)&snl
, sizeof snl
);
1015 zlog_err("%s sendto failed: %s", nl
->name
,
1016 safe_strerror(errno
));
1023 /* Exported interface function. This function simply calls
1024 netlink_socket (). */
1025 void kernel_init(struct zebra_ns
*zns
)
1027 unsigned long groups
;
1028 #if defined SOL_NETLINK
1033 * Initialize netlink sockets
1035 * If RTMGRP_XXX exists use that, but at some point
1036 * I think the kernel developers realized that
1037 * keeping track of all the different values would
1038 * lead to confusion, so we need to convert the
1039 * RTNLGRP_XXX to a bit position for ourself
1041 groups
= RTMGRP_LINK
|
1043 RTMGRP_IPV4_IFADDR
|
1045 RTMGRP_IPV6_IFADDR
|
1046 RTMGRP_IPV4_MROUTE
|
1048 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
1049 (1 << (RTNLGRP_IPV6_RULE
- 1));
1051 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1052 "netlink-listen (NS %u)", zns
->ns_id
);
1053 zns
->netlink
.sock
= -1;
1054 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1055 zlog_err("Failure to create %s socket",
1060 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1061 "netlink-cmd (NS %u)", zns
->ns_id
);
1062 zns
->netlink_cmd
.sock
= -1;
1063 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1064 zlog_err("Failure to create %s socket",
1065 zns
->netlink_cmd
.name
);
1070 * SOL_NETLINK is not available on all platforms yet
1071 * apparently. It's in bits/socket.h which I am not
1072 * sure that we want to pull into our build system.
1074 #if defined SOL_NETLINK
1076 * Let's tell the kernel that we want to receive extended
1077 * ACKS over our command socket
1080 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1084 zlog_notice("Registration for extended ACK failed : %d %s",
1085 errno
, safe_strerror(errno
));
1088 /* Register kernel socket. */
1089 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1090 flog_err_sys(LIB_ERR_SOCKET
, "Can't set %s socket flags: %s",
1091 zns
->netlink
.name
, safe_strerror(errno
));
1093 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1094 zlog_err("Can't set %s socket error: %s(%d)",
1095 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1097 /* Set receive buffer size if it's set from command line */
1099 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
1101 assert(zns
->netlink
.sock
>= 0);
1102 netlink_install_filter(zns
->netlink
.sock
,
1103 zns
->netlink_cmd
.snl
.nl_pid
);
1104 zns
->t_netlink
= NULL
;
1106 thread_add_read(zebrad
.master
, kernel_read
, zns
,
1107 zns
->netlink
.sock
, &zns
->t_netlink
);
1112 void kernel_terminate(struct zebra_ns
*zns
)
1114 THREAD_READ_OFF(zns
->t_netlink
);
1116 if (zns
->netlink
.sock
>= 0) {
1117 close(zns
->netlink
.sock
);
1118 zns
->netlink
.sock
= -1;
1121 if (zns
->netlink_cmd
.sock
>= 0) {
1122 close(zns
->netlink_cmd
.sock
);
1123 zns
->netlink_cmd
.sock
= -1;
1127 #endif /* HAVE_NETLINK */