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"
56 #ifndef SO_RCVBUFFORCE
57 #define SO_RCVBUFFORCE (33)
60 /* Hack for GNU libc version 2. */
62 #define MSG_TRUNC 0x20
63 #endif /* MSG_TRUNC */
66 #define NLMSG_TAIL(nmsg) \
67 ((struct rtattr *)(((uint8_t *)(nmsg)) \
68 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
72 #define RTA_TAIL(rta) \
73 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
76 #ifndef RTNL_FAMILY_IP6MR
77 #define RTNL_FAMILY_IP6MR 129
80 #ifndef RTPROT_MROUTED
81 #define RTPROT_MROUTED 17
84 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
85 {RTM_DELROUTE
, "RTM_DELROUTE"},
86 {RTM_GETROUTE
, "RTM_GETROUTE"},
87 {RTM_NEWLINK
, "RTM_NEWLINK"},
88 {RTM_DELLINK
, "RTM_DELLINK"},
89 {RTM_GETLINK
, "RTM_GETLINK"},
90 {RTM_NEWADDR
, "RTM_NEWADDR"},
91 {RTM_DELADDR
, "RTM_DELADDR"},
92 {RTM_GETADDR
, "RTM_GETADDR"},
93 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
94 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
95 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
96 {RTM_NEWRULE
, "RTM_NEWRULE"},
97 {RTM_DELRULE
, "RTM_DELRULE"},
98 {RTM_GETRULE
, "RTM_GETRULE"},
101 static const struct message rtproto_str
[] = {
102 {RTPROT_REDIRECT
, "redirect"},
103 {RTPROT_KERNEL
, "kernel"},
104 {RTPROT_BOOT
, "boot"},
105 {RTPROT_STATIC
, "static"},
106 {RTPROT_GATED
, "GateD"},
107 {RTPROT_RA
, "router advertisement"},
109 {RTPROT_ZEBRA
, "Zebra"},
111 {RTPROT_BIRD
, "BIRD"},
112 #endif /* RTPROT_BIRD */
113 {RTPROT_MROUTED
, "mroute"},
115 {RTPROT_OSPF
, "OSPF"},
116 {RTPROT_ISIS
, "IS-IS"},
118 {RTPROT_RIPNG
, "RIPNG"},
119 {RTPROT_ZSTATIC
, "static"},
122 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
124 {AF_BRIDGE
, "bridge"},
125 {RTNL_FAMILY_IPMR
, "ipv4MR"},
126 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
129 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
130 {RTN_MULTICAST
, "multicast"},
133 extern struct thread_master
*master
;
134 extern uint32_t nl_rcvbufsize
;
136 extern struct zebra_privs_t zserv_privs
;
138 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
141 * This is an error condition that must be handled during
144 * The netlink_talk_filter function is used for communication
145 * down the netlink_cmd pipe and we are expecting
146 * an ack being received. So if we get here
147 * then we did not receive the ack and instead
148 * received some other message in an unexpected
151 zlog_err("%s: ignoring message type 0x%04x(%s) NS %u",
152 __PRETTY_FUNCTION__
, h
->nlmsg_type
,
153 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 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
167 safe_strerror(errno
));
171 /* Try force option (linux >= 2.6.14) and fall back to normal set */
172 if (zserv_privs
.change(ZPRIVS_RAISE
))
173 zlog_ferr(LIB_ERR_PRIVILEGES
,
174 "routing_socket: Can't raise privileges");
175 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
176 sizeof(nl_rcvbufsize
));
177 if (zserv_privs
.change(ZPRIVS_LOWER
))
178 zlog_ferr(LIB_ERR_PRIVILEGES
,
179 "routing_socket: Can't lower privileges");
181 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
182 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
184 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
185 safe_strerror(errno
));
189 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
191 zlog_err("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
;
211 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
212 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges");
216 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
218 zlog_err("Can't open %s socket: %s", nl
->name
,
219 safe_strerror(errno
));
223 memset(&snl
, 0, sizeof snl
);
224 snl
.nl_family
= AF_NETLINK
;
225 snl
.nl_groups
= groups
;
227 /* Bind the socket to the netlink structure for anything. */
228 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
230 if (zserv_privs
.change(ZPRIVS_LOWER
))
231 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges");
234 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
235 snl
.nl_groups
, safe_strerror(save_errno
));
240 /* multiple netlink sockets will have different nl_pid */
241 namelen
= sizeof snl
;
242 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
243 if (ret
< 0 || namelen
!= sizeof snl
) {
244 zlog_err("Can't get %s socket name: %s", nl
->name
,
245 safe_strerror(errno
));
255 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
259 * When we handle new message types here
260 * because we are starting to install them
261 * then lets check the netlink_install_filter
262 * and see if we should add the corresponding
263 * allow through entry there.
264 * Probably not needed to do but please
267 switch (h
->nlmsg_type
) {
269 return netlink_route_change(h
, ns_id
, startup
);
271 return netlink_route_change(h
, ns_id
, startup
);
273 return netlink_link_change(h
, ns_id
, startup
);
275 return netlink_link_change(h
, ns_id
, startup
);
277 return netlink_interface_addr(h
, ns_id
, startup
);
279 return netlink_interface_addr(h
, ns_id
, startup
);
281 return netlink_neigh_change(h
, ns_id
);
283 return netlink_neigh_change(h
, ns_id
);
285 return netlink_rule_change(h
, ns_id
, startup
);
287 return netlink_rule_change(h
, ns_id
, startup
);
290 * If we have received this message then
291 * we have made a mistake during development
292 * and we need to write some code to handle
293 * this message type or not ask for
294 * it to be sent up to us
296 zlog_err("Unknown netlink nlmsg_type %s(%d) vrf %u\n",
297 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
304 #if defined(HANDLE_NETLINK_FUZZING)
305 /* Using globals here to avoid adding function parameters */
307 /* Keep distinct filenames for netlink fuzzy collection */
308 static unsigned int netlink_file_counter
= 1;
310 /* File name to read fuzzed netlink from */
311 static char netlink_fuzz_file
[MAXPATHLEN
] = "";
313 /* Flag for whether to read from file or not */
317 * netlink_read_init() - Starts the message parser
318 * @fname: Filename to read.
320 void netlink_read_init(const char *fname
)
322 snprintf(netlink_fuzz_file
, MAXPATHLEN
, "%s", fname
);
323 /* Creating this fake socket for testing purposes */
324 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
326 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 1, 0);
330 * netlink_write_incoming() - Writes all data received from netlink to a file
331 * @buf: Data from netlink.
332 * @size: Size of data.
333 * @counter: Counter for keeping filenames distinct.
335 static void netlink_write_incoming(const char *buf
, const unsigned int size
,
336 unsigned int counter
)
338 char fname
[MAXPATHLEN
];
341 zserv_privs
.change(ZPRIVS_RAISE
);
342 snprintf(fname
, MAXPATHLEN
, "%s/%s_%u", DAEMON_VTY_DIR
, "netlink",
344 f
= fopen(fname
, "w");
346 fwrite(buf
, 1, size
, f
);
349 zserv_privs
.change(ZPRIVS_LOWER
);
353 * netlink_read_file() - Reads netlink data from file
354 * @buf: Netlink buffer being overwritten.
355 * @fname: File name to read from.
357 * Return: Size of file.
359 static long netlink_read_file(char *buf
, const char *fname
)
362 long file_bytes
= -1;
364 zserv_privs
.change(ZPRIVS_RAISE
);
365 f
= fopen(fname
, "r");
367 fseek(f
, 0, SEEK_END
);
368 file_bytes
= ftell(f
);
370 fread(buf
, NL_RCV_PKT_BUF_SIZE
, 1, f
);
373 zserv_privs
.change(ZPRIVS_LOWER
);
377 #endif /* HANDLE_NETLINK_FUZZING */
379 static int kernel_read(struct thread
*thread
)
381 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
382 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
383 zns
->t_netlink
= NULL
;
384 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
391 * Filter out messages from self that occur on listener socket,
392 * caused by our actions on the command socket
394 * When we add new Netlink message types we probably
395 * do not need to add them here as that we are filtering
396 * on the routes we actually care to receive( which is rarer
397 * then the normal course of operations). We are intentionally
398 * allowing some messages from ourselves through
399 * ( I'm looking at you Interface based netlink messages )
400 * so that we only had to write one way to handle incoming
401 * address add/delete changes.
403 static void netlink_install_filter(int sock
, __u32 pid
)
406 * BPF_JUMP instructions and where you jump to are based upon
407 * 0 as being the next statement. So count from 0. Writing
408 * this down because every time I look at this I have to
411 struct sock_filter filter
[] = {
414 * if (nlmsg_pid == pid) {
415 * if (the incoming nlmsg_type ==
416 * RTM_NEWADDR | RTM_DELADDR)
421 * keep this netlink message
424 * 0: Load the nlmsg_pid into the BPF register
426 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
427 offsetof(struct nlmsghdr
, nlmsg_pid
)),
431 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 4),
433 * 2: Load the nlmsg_type into BPF register
435 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
436 offsetof(struct nlmsghdr
, nlmsg_type
)),
438 * 3: Compare to RTM_NEWADDR
440 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 2, 0),
442 * 4: Compare to RTM_DELADDR
444 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 1, 0),
446 * 5: This is the end state of we want to skip the
449 BPF_STMT(BPF_RET
| BPF_K
, 0),
450 /* 6: This is the end state of we want to keep
453 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
456 struct sock_fprog prog
= {
457 .len
= array_size(filter
), .filter
= filter
,
460 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
462 zlog_warn("Can't install socket filter: %s\n",
463 safe_strerror(errno
));
466 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
469 while (RTA_OK(rta
, len
)) {
470 if (rta
->rta_type
<= max
)
471 tb
[rta
->rta_type
] = rta
;
472 rta
= RTA_NEXT(rta
, len
);
476 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
477 const void *data
, unsigned int alen
)
482 len
= RTA_LENGTH(alen
);
484 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
487 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
488 rta
->rta_type
= type
;
492 memcpy(RTA_DATA(rta
), data
, alen
);
496 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
501 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
,
502 const void *data
, unsigned int alen
)
505 struct rtattr
*subrta
;
507 len
= RTA_LENGTH(alen
);
509 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
512 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
513 subrta
->rta_type
= type
;
514 subrta
->rta_len
= len
;
517 memcpy(RTA_DATA(subrta
), data
, alen
);
521 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
526 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
528 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
531 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
533 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
536 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
538 struct rtattr
*nest
= NLMSG_TAIL(n
);
540 addattr_l(n
, maxlen
, type
, NULL
, 0);
544 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
546 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
550 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
552 struct rtattr
*nest
= RTA_TAIL(rta
);
554 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
558 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
560 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
564 const char *nl_msg_type_to_str(uint16_t msg_type
)
566 return lookup_msg(nlmsg_str
, msg_type
, "");
569 const char *nl_rtproto_to_str(uint8_t rtproto
)
571 return lookup_msg(rtproto_str
, rtproto
, "");
574 const char *nl_family_to_str(uint8_t family
)
576 return lookup_msg(family_str
, family
, "");
579 const char *nl_rttype_to_str(uint8_t rttype
)
581 return lookup_msg(rttype_str
, rttype
, "");
584 #define NL_OK(nla, len) \
585 ((len) >= (int)sizeof(struct nlattr) \
586 && (nla)->nla_len >= sizeof(struct nlattr) \
587 && (nla)->nla_len <= (len))
588 #define NL_NEXT(nla, attrlen) \
589 ((attrlen) -= RTA_ALIGN((nla)->nla_len), \
590 (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len)))
592 ((struct nlattr *)(((char *)(r)) \
593 + NLMSG_ALIGN(sizeof(struct nlmsgerr))))
595 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
596 struct nlattr
*nla
, int len
)
598 while (NL_OK(nla
, len
)) {
599 if (nla
->nla_type
<= max
)
600 tb
[nla
->nla_type
] = nla
;
601 nla
= NL_NEXT(nla
, len
);
605 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
607 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
608 const struct nlmsgerr
*err
=
609 (const struct nlmsgerr
*)((uint8_t *)h
611 sizeof(struct nlmsghdr
)));
612 const struct nlmsghdr
*err_nlh
= NULL
;
613 uint32_t hlen
= sizeof(*err
);
614 const char *msg
= NULL
;
617 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
618 hlen
+= h
->nlmsg_len
- NLMSG_ALIGN(sizeof(struct nlmsghdr
));
620 memset(tb
, 0, sizeof(tb
));
621 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, NL_RTA(h
), hlen
);
623 if (tb
[NLMSGERR_ATTR_MSG
])
624 msg
= (const char *)RTA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
626 if (tb
[NLMSGERR_ATTR_OFFS
]) {
627 off
= *(uint32_t *)RTA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
629 if (off
> h
->nlmsg_len
) {
630 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n");
631 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
633 * Header of failed message
634 * we are not doing anything currently with it
635 * but noticing it for later.
638 zlog_warn("%s: Received %d extended Ack",
639 __PRETTY_FUNCTION__
, err_nlh
->nlmsg_type
);
643 if (msg
&& *msg
!= '\0') {
644 bool is_err
= !!err
->error
;
647 zlog_err("Extended Error: %s", msg
);
649 zlog_warn("Extended Warning: %s", msg
);
656 * Receive message from netlink interface and pass those information
657 * to the given function.
659 * filter -> Function to call to read the results
660 * nl -> netlink socket information
661 * zns -> The zebra namespace data
662 * count -> How many we should read in, 0 means as much as possible
663 * startup -> Are we reading in under startup conditions? passed to
666 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
667 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
676 char buf
[NL_RCV_PKT_BUF_SIZE
];
677 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
678 struct sockaddr_nl snl
;
679 struct msghdr msg
= {.msg_name
= (void *)&snl
,
680 .msg_namelen
= sizeof snl
,
685 if (count
&& read_in
>= count
)
688 #if defined(HANDLE_NETLINK_FUZZING)
689 /* Check if reading and filename is set */
690 if (netlink_read
&& '\0' != netlink_fuzz_file
[0]) {
691 zlog_debug("Reading netlink fuzz file");
692 status
= netlink_read_file(buf
, netlink_fuzz_file
);
695 status
= recvmsg(nl
->sock
, &msg
, 0);
698 status
= recvmsg(nl
->sock
, &msg
, 0);
699 #endif /* HANDLE_NETLINK_FUZZING */
703 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
705 zlog_err("%s recvmsg overrun: %s", nl
->name
,
706 safe_strerror(errno
));
708 * In this case we are screwed.
709 * There is no good way to
710 * recover zebra at this point.
717 zlog_err("%s EOF", nl
->name
);
721 if (msg
.msg_namelen
!= sizeof snl
) {
722 zlog_err("%s sender address length error: length %d",
723 nl
->name
, msg
.msg_namelen
);
727 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
728 zlog_debug("%s: << netlink message dump [recv]",
730 zlog_hexdump(buf
, status
);
733 #if defined(HANDLE_NETLINK_FUZZING)
735 zlog_debug("Writing incoming netlink message");
736 netlink_write_incoming(buf
, status
,
737 netlink_file_counter
++);
739 #endif /* HANDLE_NETLINK_FUZZING */
742 for (h
= (struct nlmsghdr
*)buf
;
743 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
744 h
= NLMSG_NEXT(h
, status
)) {
745 /* Finish of reading. */
746 if (h
->nlmsg_type
== NLMSG_DONE
)
749 /* Error handling. */
750 if (h
->nlmsg_type
== NLMSG_ERROR
) {
751 struct nlmsgerr
*err
=
752 (struct nlmsgerr
*)NLMSG_DATA(h
);
753 int errnum
= err
->error
;
754 int msg_type
= err
->msg
.nlmsg_type
;
757 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
758 zlog_err("%s error: message truncated",
764 * Parse the extended information before
765 * we actually handle it.
766 * At this point in time we do not
767 * do anything other than report the
770 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
771 netlink_parse_extended_ack(h
);
773 /* If the error field is zero, then this is an
775 if (err
->error
== 0) {
776 if (IS_ZEBRA_DEBUG_KERNEL
) {
778 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
779 __FUNCTION__
, nl
->name
,
781 err
->msg
.nlmsg_type
),
787 /* return if not a multipart message,
788 * otherwise continue */
789 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
794 /* Deal with errors that occur because of races
795 * in link handling */
796 if (nl
== &zns
->netlink_cmd
797 && ((msg_type
== RTM_DELROUTE
798 && (-errnum
== ENODEV
799 || -errnum
== ESRCH
))
800 || (msg_type
== RTM_NEWROUTE
801 && (-errnum
== ENETDOWN
802 || -errnum
== EEXIST
)))) {
803 if (IS_ZEBRA_DEBUG_KERNEL
)
805 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
807 safe_strerror(-errnum
),
816 /* We see RTM_DELNEIGH when shutting down an
817 * interface with an IPv4
818 * link-local. The kernel should have already
819 * deleted the neighbor
820 * so do not log these as an error.
822 if (msg_type
== RTM_DELNEIGH
823 || (nl
== &zns
->netlink_cmd
824 && msg_type
== RTM_NEWROUTE
826 || -errnum
== ENETUNREACH
))) {
827 /* This is known to happen in some
828 * situations, don't log
831 if (IS_ZEBRA_DEBUG_KERNEL
)
833 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
835 safe_strerror(-errnum
),
843 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
845 safe_strerror(-errnum
),
846 nl_msg_type_to_str(msg_type
),
847 msg_type
, err
->msg
.nlmsg_seq
,
853 /* OK we got netlink message. */
854 if (IS_ZEBRA_DEBUG_KERNEL
)
856 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
858 nl_msg_type_to_str(h
->nlmsg_type
),
859 h
->nlmsg_type
, h
->nlmsg_len
,
860 h
->nlmsg_seq
, h
->nlmsg_pid
);
864 * Ignore messages that maybe sent from
865 * other actors besides the kernel
867 if (snl
.nl_pid
!= 0) {
868 zlog_err("Ignoring message from pid %u",
873 error
= (*filter
)(h
, zns
->ns_id
, startup
);
875 zlog_err("%s filter function error", nl
->name
);
876 zlog_backtrace(LOG_ERR
);
881 /* After error care. */
882 if (msg
.msg_flags
& MSG_TRUNC
) {
883 zlog_err("%s error: message truncated", nl
->name
);
887 zlog_err("%s error: data remnant size %d", nl
->name
,
898 * sendmsg() to netlink socket then recvmsg().
899 * Calls netlink_parse_info to parse returned data
901 * filter -> The filter to read final results from kernel
902 * nlmsghdr -> The data to send to the kernel
903 * nl -> The netlink socket information
904 * zns -> The zebra namespace information
905 * startup -> Are we reading in under startup conditions
906 * This is passed through eventually to filter.
908 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
909 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
913 struct sockaddr_nl snl
;
918 memset(&snl
, 0, sizeof snl
);
919 memset(&iov
, 0, sizeof iov
);
920 memset(&msg
, 0, sizeof msg
);
923 iov
.iov_len
= n
->nlmsg_len
;
924 msg
.msg_name
= (void *)&snl
;
925 msg
.msg_namelen
= sizeof snl
;
929 snl
.nl_family
= AF_NETLINK
;
931 n
->nlmsg_seq
= ++nl
->seq
;
932 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
934 if (IS_ZEBRA_DEBUG_KERNEL
)
936 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
937 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
938 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
941 /* Send message to netlink interface. */
942 if (zserv_privs
.change(ZPRIVS_RAISE
))
943 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges");
944 status
= sendmsg(nl
->sock
, &msg
, 0);
946 if (zserv_privs
.change(ZPRIVS_LOWER
))
947 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges");
949 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
950 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
951 zlog_hexdump(n
, n
->nlmsg_len
);
955 zlog_err("netlink_talk sendmsg() error: %s",
956 safe_strerror(save_errno
));
962 * Get reply from netlink socket.
963 * The reply should either be an acknowlegement or an error.
965 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
968 /* Issue request message to kernel via netlink socket. GET messages
969 * are issued through this interface.
971 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
974 struct sockaddr_nl snl
;
977 /* Check netlink socket. */
979 zlog_err("%s socket isn't active.", nl
->name
);
983 /* Fill common fields for all requests. */
984 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
985 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
986 n
->nlmsg_seq
= ++nl
->seq
;
988 memset(&snl
, 0, sizeof snl
);
989 snl
.nl_family
= AF_NETLINK
;
991 /* Raise capabilities and send message, then lower capabilities. */
992 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
993 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't raise privileges");
997 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
998 (struct sockaddr
*)&snl
, sizeof snl
);
1001 if (zserv_privs
.change(ZPRIVS_LOWER
))
1002 zlog_ferr(LIB_ERR_PRIVILEGES
, "Can't lower privileges");
1005 zlog_err("%s sendto failed: %s", nl
->name
,
1006 safe_strerror(save_errno
));
1013 /* Exported interface function. This function simply calls
1014 netlink_socket (). */
1015 void kernel_init(struct zebra_ns
*zns
)
1017 unsigned long groups
;
1018 #if defined SOL_NETLINK
1023 * Initialize netlink sockets
1025 * If RTMGRP_XXX exists use that, but at some point
1026 * I think the kernel developers realized that
1027 * keeping track of all the different values would
1028 * lead to confusion, so we need to convert the
1029 * RTNLGRP_XXX to a bit position for ourself
1031 groups
= RTMGRP_LINK
|
1033 RTMGRP_IPV4_IFADDR
|
1035 RTMGRP_IPV6_IFADDR
|
1036 RTMGRP_IPV4_MROUTE
|
1038 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
1039 (1 << (RTNLGRP_IPV6_RULE
- 1));
1041 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1042 "netlink-listen (NS %u)", zns
->ns_id
);
1043 zns
->netlink
.sock
= -1;
1044 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1045 zlog_err("Failure to create %s socket",
1050 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1051 "netlink-cmd (NS %u)", zns
->ns_id
);
1052 zns
->netlink_cmd
.sock
= -1;
1053 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1054 zlog_err("Failure to create %s socket",
1055 zns
->netlink_cmd
.name
);
1060 * SOL_NETLINK is not available on all platforms yet
1061 * apparently. It's in bits/socket.h which I am not
1062 * sure that we want to pull into our build system.
1064 #if defined SOL_NETLINK
1066 * Let's tell the kernel that we want to receive extended
1067 * ACKS over our command socket
1070 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1074 zlog_notice("Registration for extended ACK failed : %d %s",
1075 errno
, safe_strerror(errno
));
1078 /* Register kernel socket. */
1079 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1080 zlog_err("Can't set %s socket error: %s(%d)",
1081 zns
->netlink
.name
, safe_strerror(errno
), errno
);
1083 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1084 zlog_err("Can't set %s socket error: %s(%d)",
1085 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1087 /* Set receive buffer size if it's set from command line */
1089 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
1091 assert(zns
->netlink
.sock
>= 0);
1092 netlink_install_filter(zns
->netlink
.sock
,
1093 zns
->netlink_cmd
.snl
.nl_pid
);
1094 zns
->t_netlink
= NULL
;
1096 thread_add_read(zebrad
.master
, kernel_read
, zns
,
1097 zns
->netlink
.sock
, &zns
->t_netlink
);
1102 void kernel_terminate(struct zebra_ns
*zns
)
1104 THREAD_READ_OFF(zns
->t_netlink
);
1106 if (zns
->netlink
.sock
>= 0) {
1107 close(zns
->netlink
.sock
);
1108 zns
->netlink
.sock
= -1;
1111 if (zns
->netlink_cmd
.sock
>= 0) {
1112 close(zns
->netlink_cmd
.sock
);
1113 zns
->netlink_cmd
.sock
= -1;
1117 #endif /* HAVE_NETLINK */