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"
43 #include "zebra/debug.h"
44 #include "zebra/kernel_netlink.h"
45 #include "zebra/rt_netlink.h"
46 #include "zebra/if_netlink.h"
48 #ifndef SO_RCVBUFFORCE
49 #define SO_RCVBUFFORCE (33)
52 /* Hack for GNU libc version 2. */
54 #define MSG_TRUNC 0x20
55 #endif /* MSG_TRUNC */
58 #define NLMSG_TAIL(nmsg) \
59 ((struct rtattr *)(((u_char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
63 #define RTA_TAIL(rta) \
64 ((struct rtattr *)(((u_char *)(rta)) + RTA_ALIGN((rta)->rta_len)))
67 #ifndef RTNL_FAMILY_IP6MR
68 #define RTNL_FAMILY_IP6MR 129
71 #ifndef RTPROT_MROUTED
72 #define RTPROT_MROUTED 17
75 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
76 {RTM_DELROUTE
, "RTM_DELROUTE"},
77 {RTM_GETROUTE
, "RTM_GETROUTE"},
78 {RTM_NEWLINK
, "RTM_NEWLINK"},
79 {RTM_DELLINK
, "RTM_DELLINK"},
80 {RTM_GETLINK
, "RTM_GETLINK"},
81 {RTM_NEWADDR
, "RTM_NEWADDR"},
82 {RTM_DELADDR
, "RTM_DELADDR"},
83 {RTM_GETADDR
, "RTM_GETADDR"},
84 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
85 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
86 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
89 static const struct message rtproto_str
[] = {
90 {RTPROT_REDIRECT
, "redirect"},
91 {RTPROT_KERNEL
, "kernel"},
92 {RTPROT_BOOT
, "boot"},
93 {RTPROT_STATIC
, "static"},
94 {RTPROT_GATED
, "GateD"},
95 {RTPROT_RA
, "router advertisement"},
97 {RTPROT_ZEBRA
, "Zebra"},
99 {RTPROT_BIRD
, "BIRD"},
100 #endif /* RTPROT_BIRD */
101 {RTPROT_MROUTED
, "mroute"},
103 {RTPROT_OSPF
, "OSPF"},
104 {RTPROT_ISIS
, "IS-IS"},
106 {RTPROT_RIPNG
, "RIPNG"},
109 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
111 {AF_BRIDGE
, "bridge"},
112 {RTNL_FAMILY_IPMR
, "ipv4MR"},
113 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
116 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
117 {RTN_MULTICAST
, "multicast"},
120 extern struct thread_master
*master
;
121 extern u_int32_t nl_rcvbufsize
;
123 extern struct zebra_privs_t zserv_privs
;
125 int netlink_talk_filter(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
126 ns_id_t ns_id
, int startup
)
128 zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u",
129 h
->nlmsg_type
, ns_id
);
133 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
136 socklen_t newlen
= sizeof(newsize
);
137 socklen_t oldlen
= sizeof(oldsize
);
140 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
142 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
143 safe_strerror(errno
));
147 /* Try force option (linux >= 2.6.14) and fall back to normal set */
148 if (zserv_privs
.change(ZPRIVS_RAISE
))
149 zlog_err("routing_socket: Can't raise privileges");
150 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
151 sizeof(nl_rcvbufsize
));
152 if (zserv_privs
.change(ZPRIVS_LOWER
))
153 zlog_err("routing_socket: Can't lower privileges");
155 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
156 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
158 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
159 safe_strerror(errno
));
163 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
165 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
166 safe_strerror(errno
));
170 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
175 /* Make socket for Linux netlink interface. */
176 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
180 struct sockaddr_nl snl
;
185 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
186 zlog_err("Can't raise privileges");
190 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
192 zlog_err("Can't open %s socket: %s", nl
->name
,
193 safe_strerror(errno
));
197 memset(&snl
, 0, sizeof snl
);
198 snl
.nl_family
= AF_NETLINK
;
199 snl
.nl_groups
= groups
;
201 /* Bind the socket to the netlink structure for anything. */
202 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
204 if (zserv_privs
.change(ZPRIVS_LOWER
))
205 zlog_err("Can't lower privileges");
208 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
209 snl
.nl_groups
, safe_strerror(save_errno
));
214 /* multiple netlink sockets will have different nl_pid */
215 namelen
= sizeof snl
;
216 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
217 if (ret
< 0 || namelen
!= sizeof snl
) {
218 zlog_err("Can't get %s socket name: %s", nl
->name
,
219 safe_strerror(errno
));
229 static int netlink_information_fetch(struct sockaddr_nl
*snl
,
230 struct nlmsghdr
*h
, ns_id_t ns_id
,
233 /* JF: Ignore messages that aren't from the kernel */
234 if (snl
->nl_pid
!= 0) {
235 zlog_err("Ignoring message from pid %u", snl
->nl_pid
);
239 switch (h
->nlmsg_type
) {
241 return netlink_route_change(snl
, h
, ns_id
, startup
);
244 return netlink_route_change(snl
, h
, ns_id
, startup
);
247 return netlink_link_change(snl
, h
, ns_id
, startup
);
250 return netlink_link_change(snl
, h
, ns_id
, startup
);
253 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
256 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
259 return netlink_neigh_change(snl
, h
, ns_id
);
262 return netlink_neigh_change(snl
, h
, ns_id
);
265 if (IS_ZEBRA_DEBUG_KERNEL
)
266 zlog_debug("Unknown netlink nlmsg_type %d vrf %u\n",
267 h
->nlmsg_type
, ns_id
);
273 static int kernel_read(struct thread
*thread
)
275 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
276 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
277 zns
->t_netlink
= NULL
;
278 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
284 /* Filter out messages from self that occur on listener socket,
285 * caused by our actions on the command socket
287 static void netlink_install_filter(int sock
, __u32 pid
)
289 struct sock_filter filter
[] = {
291 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
292 offsetof(struct nlmsghdr
, nlmsg_type
)),
293 /* 1: jeq 0x18 jt 5 jf next */
294 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWROUTE
), 3, 0),
295 /* 2: jeq 0x19 jt 5 jf next */
296 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELROUTE
), 2, 0),
297 /* 3: jeq 0x19 jt 5 jf next */
298 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNEIGH
), 1, 0),
299 /* 4: jeq 0x19 jt 5 jf 8 */
300 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNEIGH
), 0, 3),
302 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
303 offsetof(struct nlmsghdr
, nlmsg_pid
)),
304 /* 6: jeq XX jt 7 jf 8 */
305 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 1),
306 /* 7: ret 0 (skip) */
307 BPF_STMT(BPF_RET
| BPF_K
, 0),
308 /* 8: ret 0xffff (keep) */
309 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
312 struct sock_fprog prog
= {
313 .len
= array_size(filter
), .filter
= filter
,
316 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
318 zlog_warn("Can't install socket filter: %s\n",
319 safe_strerror(errno
));
322 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
325 while (RTA_OK(rta
, len
)) {
326 if (rta
->rta_type
<= max
)
327 tb
[rta
->rta_type
] = rta
;
328 rta
= RTA_NEXT(rta
, len
);
332 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
338 len
= RTA_LENGTH(alen
);
340 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
343 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
344 rta
->rta_type
= type
;
348 memcpy(RTA_DATA(rta
), data
, alen
);
352 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
357 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
361 struct rtattr
*subrta
;
363 len
= RTA_LENGTH(alen
);
365 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
368 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
369 subrta
->rta_type
= type
;
370 subrta
->rta_len
= len
;
373 memcpy(RTA_DATA(subrta
), data
, alen
);
377 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
382 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, u_int16_t data
)
384 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int16_t
));
387 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
389 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int32_t
));
392 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
394 struct rtattr
*nest
= NLMSG_TAIL(n
);
396 addattr_l(n
, maxlen
, type
, NULL
, 0);
400 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
402 nest
->rta_len
= (u_char
*)NLMSG_TAIL(n
) - (u_char
*)nest
;
406 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
408 struct rtattr
*nest
= RTA_TAIL(rta
);
410 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
414 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
416 nest
->rta_len
= (u_char
*)RTA_TAIL(rta
) - (u_char
*)nest
;
420 const char *nl_msg_type_to_str(uint16_t msg_type
)
422 return lookup_msg(nlmsg_str
, msg_type
, "");
425 const char *nl_rtproto_to_str(u_char rtproto
)
427 return lookup_msg(rtproto_str
, rtproto
, "");
430 const char *nl_family_to_str(u_char family
)
432 return lookup_msg(family_str
, family
, "");
435 const char *nl_rttype_to_str(u_char rttype
)
437 return lookup_msg(rttype_str
, rttype
, "");
443 * Receive message from netlink interface and pass those information
444 * to the given function.
446 * filter -> Function to call to read the results
447 * nl -> netlink socket information
448 * zns -> The zebra namespace data
449 * count -> How many we should read in, 0 means as much as possible
450 * startup -> Are we reading in under startup conditions? passed to
453 int netlink_parse_info(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*,
455 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
464 char buf
[NL_PKT_BUF_SIZE
];
465 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
466 struct sockaddr_nl snl
;
467 struct msghdr msg
= {.msg_name
= (void *)&snl
,
468 .msg_namelen
= sizeof snl
,
473 if (count
&& read_in
>= count
)
476 status
= recvmsg(nl
->sock
, &msg
, 0);
480 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
482 zlog_err("%s recvmsg overrun: %s", nl
->name
,
483 safe_strerror(errno
));
485 * In this case we are screwed.
486 * There is no good way to
487 * recover zebra at this point.
494 zlog_err("%s EOF", nl
->name
);
498 if (msg
.msg_namelen
!= sizeof snl
) {
499 zlog_err("%s sender address length error: length %d",
500 nl
->name
, msg
.msg_namelen
);
504 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
505 zlog_debug("%s: << netlink message dump [recv]",
507 zlog_hexdump(buf
, status
);
511 for (h
= (struct nlmsghdr
*)buf
;
512 NLMSG_OK(h
, (unsigned int)status
);
513 h
= NLMSG_NEXT(h
, status
)) {
514 /* Finish of reading. */
515 if (h
->nlmsg_type
== NLMSG_DONE
)
518 /* Error handling. */
519 if (h
->nlmsg_type
== NLMSG_ERROR
) {
520 struct nlmsgerr
*err
=
521 (struct nlmsgerr
*)NLMSG_DATA(h
);
522 int errnum
= err
->error
;
523 int msg_type
= err
->msg
.nlmsg_type
;
525 /* If the error field is zero, then this is an
527 if (err
->error
== 0) {
528 if (IS_ZEBRA_DEBUG_KERNEL
) {
530 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
531 __FUNCTION__
, nl
->name
,
533 err
->msg
.nlmsg_type
),
539 /* return if not a multipart message,
540 * otherwise continue */
541 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
547 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
548 zlog_err("%s error: message truncated",
553 /* Deal with errors that occur because of races
554 * in link handling */
555 if (nl
== &zns
->netlink_cmd
556 && ((msg_type
== RTM_DELROUTE
557 && (-errnum
== ENODEV
558 || -errnum
== ESRCH
))
559 || (msg_type
== RTM_NEWROUTE
560 && (-errnum
== ENETDOWN
561 || -errnum
== EEXIST
)))) {
562 if (IS_ZEBRA_DEBUG_KERNEL
)
564 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
566 safe_strerror(-errnum
),
575 /* We see RTM_DELNEIGH when shutting down an
576 * interface with an IPv4
577 * link-local. The kernel should have already
578 * deleted the neighbor
579 * so do not log these as an error.
581 if (msg_type
== RTM_DELNEIGH
582 || (nl
== &zns
->netlink_cmd
583 && msg_type
== RTM_NEWROUTE
585 || -errnum
== ENETUNREACH
))) {
586 /* This is known to happen in some
587 * situations, don't log
590 if (IS_ZEBRA_DEBUG_KERNEL
)
592 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
594 safe_strerror(-errnum
),
602 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
604 safe_strerror(-errnum
),
605 nl_msg_type_to_str(msg_type
),
606 msg_type
, err
->msg
.nlmsg_seq
,
612 /* OK we got netlink message. */
613 if (IS_ZEBRA_DEBUG_KERNEL
)
615 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
617 nl_msg_type_to_str(h
->nlmsg_type
),
618 h
->nlmsg_type
, h
->nlmsg_len
,
619 h
->nlmsg_seq
, h
->nlmsg_pid
);
621 /* skip unsolicited messages originating from command
623 * linux sets the originators port-id for {NEW|DEL}ADDR
625 * so this has to be checked here. */
626 if (nl
!= &zns
->netlink_cmd
627 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
628 && (h
->nlmsg_type
!= RTM_NEWADDR
629 && h
->nlmsg_type
!= RTM_DELADDR
)) {
630 if (IS_ZEBRA_DEBUG_KERNEL
)
632 "netlink_parse_info: %s packet comes from %s",
633 zns
->netlink_cmd
.name
,
638 error
= (*filter
)(&snl
, h
, zns
->ns_id
, startup
);
640 zlog_err("%s filter function error", nl
->name
);
645 /* After error care. */
646 if (msg
.msg_flags
& MSG_TRUNC
) {
647 zlog_err("%s error: message truncated", nl
->name
);
651 zlog_err("%s error: data remnant size %d", nl
->name
,
662 * sendmsg() to netlink socket then recvmsg().
663 * Calls netlink_parse_info to parse returned data
665 * filter -> The filter to read final results from kernel
666 * nlmsghdr -> The data to send to the kernel
667 * nl -> The netlink socket information
668 * zns -> The zebra namespace information
669 * startup -> Are we reading in under startup conditions
670 * This is passed through eventually to filter.
672 int netlink_talk(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*, ns_id_t
,
674 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
678 struct sockaddr_nl snl
;
683 memset(&snl
, 0, sizeof snl
);
684 memset(&iov
, 0, sizeof iov
);
685 memset(&msg
, 0, sizeof msg
);
688 iov
.iov_len
= n
->nlmsg_len
;
689 msg
.msg_name
= (void *)&snl
;
690 msg
.msg_namelen
= sizeof snl
;
694 snl
.nl_family
= AF_NETLINK
;
696 n
->nlmsg_seq
= ++nl
->seq
;
697 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
699 /* Request an acknowledgement by setting NLM_F_ACK */
700 n
->nlmsg_flags
|= NLM_F_ACK
;
702 if (IS_ZEBRA_DEBUG_KERNEL
)
704 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
705 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
706 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
709 /* Send message to netlink interface. */
710 if (zserv_privs
.change(ZPRIVS_RAISE
))
711 zlog_err("Can't raise privileges");
712 status
= sendmsg(nl
->sock
, &msg
, 0);
714 if (zserv_privs
.change(ZPRIVS_LOWER
))
715 zlog_err("Can't lower privileges");
717 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
718 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
719 zlog_hexdump(n
, n
->nlmsg_len
);
723 zlog_err("netlink_talk sendmsg() error: %s",
724 safe_strerror(save_errno
));
730 * Get reply from netlink socket.
731 * The reply should either be an acknowlegement or an error.
733 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
736 /* Issue request message to kernel via netlink socket. GET messages
737 * are issued through this interface.
739 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
742 struct sockaddr_nl snl
;
745 /* Check netlink socket. */
747 zlog_err("%s socket isn't active.", nl
->name
);
751 /* Fill common fields for all requests. */
752 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
753 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
754 n
->nlmsg_seq
= ++nl
->seq
;
756 memset(&snl
, 0, sizeof snl
);
757 snl
.nl_family
= AF_NETLINK
;
759 /* Raise capabilities and send message, then lower capabilities. */
760 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
761 zlog_err("Can't raise privileges");
765 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
766 (struct sockaddr
*)&snl
, sizeof snl
);
769 if (zserv_privs
.change(ZPRIVS_LOWER
))
770 zlog_err("Can't lower privileges");
773 zlog_err("%s sendto failed: %s", nl
->name
,
774 safe_strerror(save_errno
));
781 /* Exported interface function. This function simply calls
782 netlink_socket (). */
783 void kernel_init(struct zebra_ns
*zns
)
785 unsigned long groups
;
787 /* Initialize netlink sockets */
788 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
789 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV4_MROUTE
792 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
793 "netlink-listen (NS %u)", zns
->ns_id
);
794 zns
->netlink
.sock
= -1;
795 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
797 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
798 "netlink-cmd (NS %u)", zns
->ns_id
);
799 zns
->netlink_cmd
.sock
= -1;
800 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
802 /* Register kernel socket. */
803 if (zns
->netlink
.sock
> 0) {
804 /* Only want non-blocking on the netlink event socket */
805 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
806 zlog_err("Can't set %s socket flags: %s",
807 zns
->netlink
.name
, safe_strerror(errno
));
809 /* Set receive buffer size if it's set from command line */
811 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
813 netlink_install_filter(zns
->netlink
.sock
,
814 zns
->netlink_cmd
.snl
.nl_pid
);
815 zns
->t_netlink
= NULL
;
816 thread_add_read(zebrad
.master
, kernel_read
, zns
,
817 zns
->netlink
.sock
, &zns
->t_netlink
);
823 void kernel_terminate(struct zebra_ns
*zns
)
825 THREAD_READ_OFF(zns
->t_netlink
);
827 if (zns
->netlink
.sock
>= 0) {
828 close(zns
->netlink
.sock
);
829 zns
->netlink
.sock
= -1;
832 if (zns
->netlink_cmd
.sock
>= 0) {
833 close(zns
->netlink_cmd
.sock
);
834 zns
->netlink_cmd
.sock
= -1;
838 #endif /* HAVE_NETLINK */