1 /* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "connected.h"
32 #include "zebra_memory.h"
40 #include "zebra/zserv.h"
41 #include "zebra/zebra_ns.h"
42 #include "zebra/zebra_vrf.h"
44 #include "zebra/debug.h"
45 #include "zebra/kernel_netlink.h"
46 #include "zebra/rt_netlink.h"
47 #include "zebra/if_netlink.h"
48 #include "zebra/rule_netlink.h"
50 #ifndef SO_RCVBUFFORCE
51 #define SO_RCVBUFFORCE (33)
54 /* Hack for GNU libc version 2. */
56 #define MSG_TRUNC 0x20
57 #endif /* MSG_TRUNC */
60 #define NLMSG_TAIL(nmsg) \
61 ((struct rtattr *)(((uint8_t *)(nmsg)) \
62 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
66 #define RTA_TAIL(rta) \
67 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
70 #ifndef RTNL_FAMILY_IP6MR
71 #define RTNL_FAMILY_IP6MR 129
74 #ifndef RTPROT_MROUTED
75 #define RTPROT_MROUTED 17
78 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
79 {RTM_DELROUTE
, "RTM_DELROUTE"},
80 {RTM_GETROUTE
, "RTM_GETROUTE"},
81 {RTM_NEWLINK
, "RTM_NEWLINK"},
82 {RTM_DELLINK
, "RTM_DELLINK"},
83 {RTM_GETLINK
, "RTM_GETLINK"},
84 {RTM_NEWADDR
, "RTM_NEWADDR"},
85 {RTM_DELADDR
, "RTM_DELADDR"},
86 {RTM_GETADDR
, "RTM_GETADDR"},
87 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
88 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
89 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
90 {RTM_NEWRULE
, "RTM_NEWRULE"},
91 {RTM_DELRULE
, "RTM_DELRULE"},
92 {RTM_GETRULE
, "RTM_GETRULE"},
95 static const struct message rtproto_str
[] = {
96 {RTPROT_REDIRECT
, "redirect"},
97 {RTPROT_KERNEL
, "kernel"},
98 {RTPROT_BOOT
, "boot"},
99 {RTPROT_STATIC
, "static"},
100 {RTPROT_GATED
, "GateD"},
101 {RTPROT_RA
, "router advertisement"},
103 {RTPROT_ZEBRA
, "Zebra"},
105 {RTPROT_BIRD
, "BIRD"},
106 #endif /* RTPROT_BIRD */
107 {RTPROT_MROUTED
, "mroute"},
109 {RTPROT_OSPF
, "OSPF"},
110 {RTPROT_ISIS
, "IS-IS"},
112 {RTPROT_RIPNG
, "RIPNG"},
115 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
117 {AF_BRIDGE
, "bridge"},
118 {RTNL_FAMILY_IPMR
, "ipv4MR"},
119 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
122 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
123 {RTN_MULTICAST
, "multicast"},
126 extern struct thread_master
*master
;
127 extern uint32_t nl_rcvbufsize
;
129 extern struct zebra_privs_t zserv_privs
;
131 int netlink_talk_filter(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
132 ns_id_t ns_id
, int startup
)
134 zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u",
135 h
->nlmsg_type
, ns_id
);
139 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
142 socklen_t newlen
= sizeof(newsize
);
143 socklen_t oldlen
= sizeof(oldsize
);
146 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
148 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
149 safe_strerror(errno
));
153 /* Try force option (linux >= 2.6.14) and fall back to normal set */
154 if (zserv_privs
.change(ZPRIVS_RAISE
))
155 zlog_err("routing_socket: Can't raise privileges");
156 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
157 sizeof(nl_rcvbufsize
));
158 if (zserv_privs
.change(ZPRIVS_LOWER
))
159 zlog_err("routing_socket: Can't lower privileges");
161 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
162 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
164 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
165 safe_strerror(errno
));
169 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
171 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
172 safe_strerror(errno
));
176 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
181 /* Make socket for Linux netlink interface. */
182 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
186 struct sockaddr_nl snl
;
191 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
192 zlog_err("Can't raise privileges");
196 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
198 zlog_err("Can't open %s socket: %s", nl
->name
,
199 safe_strerror(errno
));
203 memset(&snl
, 0, sizeof snl
);
204 snl
.nl_family
= AF_NETLINK
;
205 snl
.nl_groups
= groups
;
207 /* Bind the socket to the netlink structure for anything. */
208 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
210 if (zserv_privs
.change(ZPRIVS_LOWER
))
211 zlog_err("Can't lower privileges");
214 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
215 snl
.nl_groups
, safe_strerror(save_errno
));
220 /* multiple netlink sockets will have different nl_pid */
221 namelen
= sizeof snl
;
222 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
223 if (ret
< 0 || namelen
!= sizeof snl
) {
224 zlog_err("Can't get %s socket name: %s", nl
->name
,
225 safe_strerror(errno
));
235 static int netlink_information_fetch(struct sockaddr_nl
*snl
,
236 struct nlmsghdr
*h
, ns_id_t ns_id
,
239 /* JF: Ignore messages that aren't from the kernel */
240 if (snl
->nl_pid
!= 0) {
241 zlog_err("Ignoring message from pid %u", snl
->nl_pid
);
245 switch (h
->nlmsg_type
) {
247 return netlink_route_change(snl
, h
, ns_id
, startup
);
249 return netlink_route_change(snl
, h
, ns_id
, startup
);
251 return netlink_link_change(snl
, h
, ns_id
, startup
);
253 return netlink_link_change(snl
, h
, ns_id
, startup
);
255 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
257 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
259 return netlink_neigh_change(snl
, h
, ns_id
);
261 return netlink_neigh_change(snl
, h
, ns_id
);
263 return netlink_rule_change(snl
, h
, ns_id
, startup
);
265 return netlink_rule_change(snl
, h
, ns_id
, startup
);
267 if (IS_ZEBRA_DEBUG_KERNEL
)
268 zlog_debug("Unknown netlink nlmsg_type %d vrf %u\n",
269 h
->nlmsg_type
, ns_id
);
275 static int kernel_read(struct thread
*thread
)
277 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
278 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
279 zns
->t_netlink
= NULL
;
280 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
286 /* Filter out messages from self that occur on listener socket,
287 * caused by our actions on the command socket
289 static void netlink_install_filter(int sock
, __u32 pid
)
291 struct sock_filter filter
[] = {
293 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
294 offsetof(struct nlmsghdr
, nlmsg_type
)),
295 /* 1: jeq 0x18 jt 5 jf next */
296 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWROUTE
), 3, 0),
297 /* 2: jeq 0x19 jt 5 jf next */
298 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELROUTE
), 2, 0),
299 /* 3: jeq 0x19 jt 5 jf next */
300 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNEIGH
), 1, 0),
301 /* 4: jeq 0x19 jt 5 jf 8 */
302 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNEIGH
), 0, 3),
304 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
305 offsetof(struct nlmsghdr
, nlmsg_pid
)),
306 /* 6: jeq XX jt 7 jf 8 */
307 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 1),
308 /* 7: ret 0 (skip) */
309 BPF_STMT(BPF_RET
| BPF_K
, 0),
310 /* 8: ret 0xffff (keep) */
311 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
314 struct sock_fprog prog
= {
315 .len
= array_size(filter
), .filter
= filter
,
318 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
320 zlog_warn("Can't install socket filter: %s\n",
321 safe_strerror(errno
));
324 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
327 while (RTA_OK(rta
, len
)) {
328 if (rta
->rta_type
<= max
)
329 tb
[rta
->rta_type
] = rta
;
330 rta
= RTA_NEXT(rta
, len
);
334 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
340 len
= RTA_LENGTH(alen
);
342 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
345 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
346 rta
->rta_type
= type
;
350 memcpy(RTA_DATA(rta
), data
, alen
);
354 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
359 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
363 struct rtattr
*subrta
;
365 len
= RTA_LENGTH(alen
);
367 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
370 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
371 subrta
->rta_type
= type
;
372 subrta
->rta_len
= len
;
375 memcpy(RTA_DATA(subrta
), data
, alen
);
379 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
384 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
386 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
389 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
391 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
394 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
396 struct rtattr
*nest
= NLMSG_TAIL(n
);
398 addattr_l(n
, maxlen
, type
, NULL
, 0);
402 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
404 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
408 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
410 struct rtattr
*nest
= RTA_TAIL(rta
);
412 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
416 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
418 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
422 const char *nl_msg_type_to_str(uint16_t msg_type
)
424 return lookup_msg(nlmsg_str
, msg_type
, "");
427 const char *nl_rtproto_to_str(uint8_t rtproto
)
429 return lookup_msg(rtproto_str
, rtproto
, "");
432 const char *nl_family_to_str(uint8_t family
)
434 return lookup_msg(family_str
, family
, "");
437 const char *nl_rttype_to_str(uint8_t rttype
)
439 return lookup_msg(rttype_str
, rttype
, "");
445 * Receive message from netlink interface and pass those information
446 * to the given function.
448 * filter -> Function to call to read the results
449 * nl -> netlink socket information
450 * zns -> The zebra namespace data
451 * count -> How many we should read in, 0 means as much as possible
452 * startup -> Are we reading in under startup conditions? passed to
455 int netlink_parse_info(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*,
457 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
466 char buf
[NL_PKT_BUF_SIZE
];
467 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
468 struct sockaddr_nl snl
;
469 struct msghdr msg
= {.msg_name
= (void *)&snl
,
470 .msg_namelen
= sizeof snl
,
475 if (count
&& read_in
>= count
)
478 status
= recvmsg(nl
->sock
, &msg
, 0);
482 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
484 zlog_err("%s recvmsg overrun: %s", nl
->name
,
485 safe_strerror(errno
));
487 * In this case we are screwed.
488 * There is no good way to
489 * recover zebra at this point.
496 zlog_err("%s EOF", nl
->name
);
500 if (msg
.msg_namelen
!= sizeof snl
) {
501 zlog_err("%s sender address length error: length %d",
502 nl
->name
, msg
.msg_namelen
);
506 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
507 zlog_debug("%s: << netlink message dump [recv]",
509 zlog_hexdump(buf
, status
);
513 for (h
= (struct nlmsghdr
*)buf
;
514 NLMSG_OK(h
, (unsigned int)status
);
515 h
= NLMSG_NEXT(h
, status
)) {
516 /* Finish of reading. */
517 if (h
->nlmsg_type
== NLMSG_DONE
)
520 /* Error handling. */
521 if (h
->nlmsg_type
== NLMSG_ERROR
) {
522 struct nlmsgerr
*err
=
523 (struct nlmsgerr
*)NLMSG_DATA(h
);
524 int errnum
= err
->error
;
525 int msg_type
= err
->msg
.nlmsg_type
;
527 /* If the error field is zero, then this is an
529 if (err
->error
== 0) {
530 if (IS_ZEBRA_DEBUG_KERNEL
) {
532 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
533 __FUNCTION__
, nl
->name
,
535 err
->msg
.nlmsg_type
),
541 /* return if not a multipart message,
542 * otherwise continue */
543 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
549 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
550 zlog_err("%s error: message truncated",
555 /* Deal with errors that occur because of races
556 * in link handling */
557 if (nl
== &zns
->netlink_cmd
558 && ((msg_type
== RTM_DELROUTE
559 && (-errnum
== ENODEV
560 || -errnum
== ESRCH
))
561 || (msg_type
== RTM_NEWROUTE
562 && (-errnum
== ENETDOWN
563 || -errnum
== EEXIST
)))) {
564 if (IS_ZEBRA_DEBUG_KERNEL
)
566 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
568 safe_strerror(-errnum
),
577 /* We see RTM_DELNEIGH when shutting down an
578 * interface with an IPv4
579 * link-local. The kernel should have already
580 * deleted the neighbor
581 * so do not log these as an error.
583 if (msg_type
== RTM_DELNEIGH
584 || (nl
== &zns
->netlink_cmd
585 && msg_type
== RTM_NEWROUTE
587 || -errnum
== ENETUNREACH
))) {
588 /* This is known to happen in some
589 * situations, don't log
592 if (IS_ZEBRA_DEBUG_KERNEL
)
594 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
596 safe_strerror(-errnum
),
604 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
606 safe_strerror(-errnum
),
607 nl_msg_type_to_str(msg_type
),
608 msg_type
, err
->msg
.nlmsg_seq
,
614 /* OK we got netlink message. */
615 if (IS_ZEBRA_DEBUG_KERNEL
)
617 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
619 nl_msg_type_to_str(h
->nlmsg_type
),
620 h
->nlmsg_type
, h
->nlmsg_len
,
621 h
->nlmsg_seq
, h
->nlmsg_pid
);
623 /* skip unsolicited messages originating from command
625 * linux sets the originators port-id for {NEW|DEL}ADDR
627 * so this has to be checked here. */
628 if (nl
!= &zns
->netlink_cmd
629 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
630 && (h
->nlmsg_type
!= RTM_NEWADDR
631 && h
->nlmsg_type
!= RTM_DELADDR
)) {
632 if (IS_ZEBRA_DEBUG_KERNEL
)
634 "netlink_parse_info: %s packet comes from %s",
635 zns
->netlink_cmd
.name
,
640 error
= (*filter
)(&snl
, h
, zns
->ns_id
, startup
);
642 zlog_err("%s filter function error", nl
->name
);
647 /* After error care. */
648 if (msg
.msg_flags
& MSG_TRUNC
) {
649 zlog_err("%s error: message truncated", nl
->name
);
653 zlog_err("%s error: data remnant size %d", nl
->name
,
664 * sendmsg() to netlink socket then recvmsg().
665 * Calls netlink_parse_info to parse returned data
667 * filter -> The filter to read final results from kernel
668 * nlmsghdr -> The data to send to the kernel
669 * nl -> The netlink socket information
670 * zns -> The zebra namespace information
671 * startup -> Are we reading in under startup conditions
672 * This is passed through eventually to filter.
674 int netlink_talk(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*, ns_id_t
,
676 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
680 struct sockaddr_nl snl
;
685 memset(&snl
, 0, sizeof snl
);
686 memset(&iov
, 0, sizeof iov
);
687 memset(&msg
, 0, sizeof msg
);
690 iov
.iov_len
= n
->nlmsg_len
;
691 msg
.msg_name
= (void *)&snl
;
692 msg
.msg_namelen
= sizeof snl
;
696 snl
.nl_family
= AF_NETLINK
;
698 n
->nlmsg_seq
= ++nl
->seq
;
699 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
701 /* Request an acknowledgement by setting NLM_F_ACK */
702 n
->nlmsg_flags
|= NLM_F_ACK
;
704 if (IS_ZEBRA_DEBUG_KERNEL
)
706 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
707 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
708 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
711 /* Send message to netlink interface. */
712 if (zserv_privs
.change(ZPRIVS_RAISE
))
713 zlog_err("Can't raise privileges");
714 status
= sendmsg(nl
->sock
, &msg
, 0);
716 if (zserv_privs
.change(ZPRIVS_LOWER
))
717 zlog_err("Can't lower privileges");
719 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
720 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
721 zlog_hexdump(n
, n
->nlmsg_len
);
725 zlog_err("netlink_talk sendmsg() error: %s",
726 safe_strerror(save_errno
));
732 * Get reply from netlink socket.
733 * The reply should either be an acknowlegement or an error.
735 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
738 /* Issue request message to kernel via netlink socket. GET messages
739 * are issued through this interface.
741 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
744 struct sockaddr_nl snl
;
747 /* Check netlink socket. */
749 zlog_err("%s socket isn't active.", nl
->name
);
753 /* Fill common fields for all requests. */
754 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
755 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
756 n
->nlmsg_seq
= ++nl
->seq
;
758 memset(&snl
, 0, sizeof snl
);
759 snl
.nl_family
= AF_NETLINK
;
761 /* Raise capabilities and send message, then lower capabilities. */
762 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
763 zlog_err("Can't raise privileges");
767 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
768 (struct sockaddr
*)&snl
, sizeof snl
);
771 if (zserv_privs
.change(ZPRIVS_LOWER
))
772 zlog_err("Can't lower privileges");
775 zlog_err("%s sendto failed: %s", nl
->name
,
776 safe_strerror(save_errno
));
783 /* Exported interface function. This function simply calls
784 netlink_socket (). */
785 void kernel_init(struct zebra_ns
*zns
)
787 unsigned long groups
;
789 /* Initialize netlink sockets */
790 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
791 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV4_MROUTE
793 | RTNLGRP_IPV4_RULE
| RTNLGRP_IPV6_RULE
;
795 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
796 "netlink-listen (NS %u)", zns
->ns_id
);
797 zns
->netlink
.sock
= -1;
798 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
800 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
801 "netlink-cmd (NS %u)", zns
->ns_id
);
802 zns
->netlink_cmd
.sock
= -1;
803 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
805 /* Register kernel socket. */
806 if (zns
->netlink
.sock
> 0) {
807 /* Only want non-blocking on the netlink event socket */
808 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
809 zlog_err("Can't set %s socket flags: %s",
810 zns
->netlink
.name
, safe_strerror(errno
));
812 /* Set receive buffer size if it's set from command line */
814 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
816 netlink_install_filter(zns
->netlink
.sock
,
817 zns
->netlink_cmd
.snl
.nl_pid
);
818 zns
->t_netlink
= NULL
;
819 thread_add_read(zebrad
.master
, kernel_read
, zns
,
820 zns
->netlink
.sock
, &zns
->t_netlink
);
826 void kernel_terminate(struct zebra_ns
*zns
)
828 THREAD_READ_OFF(zns
->t_netlink
);
830 if (zns
->netlink
.sock
>= 0) {
831 close(zns
->netlink
.sock
);
832 zns
->netlink
.sock
= -1;
835 if (zns
->netlink_cmd
.sock
>= 0) {
836 close(zns
->netlink_cmd
.sock
);
837 zns
->netlink_cmd
.sock
= -1;
841 #endif /* HAVE_NETLINK */