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
27 #include "connected.h"
30 #include "zebra_memory.h"
38 #include "zebra/zserv.h"
39 #include "zebra/zebra_ns.h"
40 #include "zebra/zebra_vrf.h"
41 #include "zebra/debug.h"
42 #include "zebra/kernel_netlink.h"
43 #include "zebra/rt_netlink.h"
44 #include "zebra/if_netlink.h"
46 #ifndef SO_RCVBUFFORCE
47 #define SO_RCVBUFFORCE (33)
50 /* Hack for GNU libc version 2. */
52 #define MSG_TRUNC 0x20
53 #endif /* MSG_TRUNC */
56 #define NLMSG_TAIL(nmsg) \
57 ((struct rtattr *)(((u_char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
61 #define RTA_TAIL(rta) \
62 ((struct rtattr *)(((u_char *)(rta)) + RTA_ALIGN((rta)->rta_len)))
65 #ifndef RTNL_FAMILY_IP6MR
66 #define RTNL_FAMILY_IP6MR 129
69 #ifndef RTPROT_MROUTED
70 #define RTPROT_MROUTED 17
73 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
74 {RTM_DELROUTE
, "RTM_DELROUTE"},
75 {RTM_GETROUTE
, "RTM_GETROUTE"},
76 {RTM_NEWLINK
, "RTM_NEWLINK"},
77 {RTM_DELLINK
, "RTM_DELLINK"},
78 {RTM_GETLINK
, "RTM_GETLINK"},
79 {RTM_NEWADDR
, "RTM_NEWADDR"},
80 {RTM_DELADDR
, "RTM_DELADDR"},
81 {RTM_GETADDR
, "RTM_GETADDR"},
82 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
83 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
84 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
87 static const struct message rtproto_str
[] = {
88 {RTPROT_REDIRECT
, "redirect"},
89 {RTPROT_KERNEL
, "kernel"},
90 {RTPROT_BOOT
, "boot"},
91 {RTPROT_STATIC
, "static"},
92 {RTPROT_GATED
, "GateD"},
93 {RTPROT_RA
, "router advertisement"},
95 {RTPROT_ZEBRA
, "Zebra"},
97 {RTPROT_BIRD
, "BIRD"},
98 #endif /* RTPROT_BIRD */
99 {RTPROT_MROUTED
, "mroute"},
101 {RTPROT_OSPF
, "OSPF"},
102 {RTPROT_ISIS
, "IS-IS"},
104 {RTPROT_RIPNG
, "RIPNG"},
107 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
109 {AF_BRIDGE
, "bridge"},
110 {RTNL_FAMILY_IPMR
, "ipv4MR"},
111 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
114 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
115 {RTN_MULTICAST
, "multicast"},
118 extern struct thread_master
*master
;
119 extern u_int32_t nl_rcvbufsize
;
121 extern struct zebra_privs_t zserv_privs
;
123 int netlink_talk_filter(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
124 ns_id_t ns_id
, int startup
)
126 zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u",
127 h
->nlmsg_type
, ns_id
);
131 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
134 socklen_t newlen
= sizeof(newsize
);
135 socklen_t oldlen
= sizeof(oldsize
);
138 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
140 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
141 safe_strerror(errno
));
145 /* Try force option (linux >= 2.6.14) and fall back to normal set */
146 if (zserv_privs
.change(ZPRIVS_RAISE
))
147 zlog_err("routing_socket: Can't raise privileges");
148 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
149 sizeof(nl_rcvbufsize
));
150 if (zserv_privs
.change(ZPRIVS_LOWER
))
151 zlog_err("routing_socket: Can't lower privileges");
153 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
154 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
156 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
157 safe_strerror(errno
));
161 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
163 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
164 safe_strerror(errno
));
168 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
173 /* Make socket for Linux netlink interface. */
174 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
178 struct sockaddr_nl snl
;
183 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
184 zlog_err("Can't raise privileges");
188 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
190 zlog_err("Can't open %s socket: %s", nl
->name
,
191 safe_strerror(errno
));
195 memset(&snl
, 0, sizeof snl
);
196 snl
.nl_family
= AF_NETLINK
;
197 snl
.nl_groups
= groups
;
199 /* Bind the socket to the netlink structure for anything. */
200 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
202 if (zserv_privs
.change(ZPRIVS_LOWER
))
203 zlog_err("Can't lower privileges");
206 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
207 snl
.nl_groups
, safe_strerror(save_errno
));
212 /* multiple netlink sockets will have different nl_pid */
213 namelen
= sizeof snl
;
214 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
215 if (ret
< 0 || namelen
!= sizeof snl
) {
216 zlog_err("Can't get %s socket name: %s", nl
->name
,
217 safe_strerror(errno
));
227 static int netlink_information_fetch(struct sockaddr_nl
*snl
,
228 struct nlmsghdr
*h
, ns_id_t ns_id
,
231 /* JF: Ignore messages that aren't from the kernel */
232 if (snl
->nl_pid
!= 0) {
233 zlog_err("Ignoring message from pid %u", snl
->nl_pid
);
237 switch (h
->nlmsg_type
) {
239 return netlink_route_change(snl
, h
, ns_id
, startup
);
242 return netlink_route_change(snl
, h
, ns_id
, startup
);
245 return netlink_link_change(snl
, h
, ns_id
, startup
);
248 return netlink_link_change(snl
, h
, ns_id
, startup
);
251 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
254 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
257 return netlink_neigh_change(snl
, h
, ns_id
);
260 return netlink_neigh_change(snl
, h
, ns_id
);
263 zlog_warn("Unknown netlink nlmsg_type %d vrf %u\n",
264 h
->nlmsg_type
, ns_id
);
270 static int kernel_read(struct thread
*thread
)
272 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
273 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
274 zns
->t_netlink
= NULL
;
275 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
281 /* Filter out messages from self that occur on listener socket,
282 * caused by our actions on the command socket
284 static void netlink_install_filter(int sock
, __u32 pid
)
286 struct sock_filter filter
[] = {
288 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
289 offsetof(struct nlmsghdr
, nlmsg_type
)),
290 /* 1: jeq 0x18 jt 5 jf next */
291 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWROUTE
), 3, 0),
292 /* 2: jeq 0x19 jt 5 jf next */
293 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELROUTE
), 2, 0),
294 /* 3: jeq 0x19 jt 5 jf next */
295 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNEIGH
), 1, 0),
296 /* 4: jeq 0x19 jt 5 jf 8 */
297 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNEIGH
), 0, 3),
299 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
300 offsetof(struct nlmsghdr
, nlmsg_pid
)),
301 /* 6: jeq XX jt 7 jf 8 */
302 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 1),
303 /* 7: ret 0 (skip) */
304 BPF_STMT(BPF_RET
| BPF_K
, 0),
305 /* 8: ret 0xffff (keep) */
306 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
309 struct sock_fprog prog
= {
310 .len
= array_size(filter
), .filter
= filter
,
313 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
315 zlog_warn("Can't install socket filter: %s\n",
316 safe_strerror(errno
));
319 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
322 while (RTA_OK(rta
, len
)) {
323 if (rta
->rta_type
<= max
)
324 tb
[rta
->rta_type
] = rta
;
325 rta
= RTA_NEXT(rta
, len
);
329 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
335 len
= RTA_LENGTH(alen
);
337 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
340 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
341 rta
->rta_type
= type
;
345 memcpy(RTA_DATA(rta
), data
, alen
);
349 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
354 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
358 struct rtattr
*subrta
;
360 len
= RTA_LENGTH(alen
);
362 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
365 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
366 subrta
->rta_type
= type
;
367 subrta
->rta_len
= len
;
370 memcpy(RTA_DATA(subrta
), data
, alen
);
374 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
379 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, u_int16_t data
)
381 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int16_t
));
384 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
386 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int32_t
));
389 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
391 struct rtattr
*nest
= NLMSG_TAIL(n
);
393 addattr_l(n
, maxlen
, type
, NULL
, 0);
397 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
399 nest
->rta_len
= (u_char
*)NLMSG_TAIL(n
) - (u_char
*)nest
;
403 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
405 struct rtattr
*nest
= RTA_TAIL(rta
);
407 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
411 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
413 nest
->rta_len
= (u_char
*)RTA_TAIL(rta
) - (u_char
*)nest
;
417 const char *nl_msg_type_to_str(uint16_t msg_type
)
419 return lookup_msg(nlmsg_str
, msg_type
, "");
422 const char *nl_rtproto_to_str(u_char rtproto
)
424 return lookup_msg(rtproto_str
, rtproto
, "");
427 const char *nl_family_to_str(u_char family
)
429 return lookup_msg(family_str
, family
, "");
432 const char *nl_rttype_to_str(u_char rttype
)
434 return lookup_msg(rttype_str
, rttype
, "");
440 * Receive message from netlink interface and pass those information
441 * to the given function.
443 * filter -> Function to call to read the results
444 * nl -> netlink socket information
445 * zns -> The zebra namespace data
446 * count -> How many we should read in, 0 means as much as possible
447 * startup -> Are we reading in under startup conditions? passed to
450 int netlink_parse_info(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*,
452 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
461 char buf
[NL_PKT_BUF_SIZE
];
462 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
463 struct sockaddr_nl snl
;
464 struct msghdr msg
= {.msg_name
= (void *)&snl
,
465 .msg_namelen
= sizeof snl
,
470 if (count
&& read_in
>= count
)
473 status
= recvmsg(nl
->sock
, &msg
, 0);
477 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
479 zlog_err("%s recvmsg overrun: %s", nl
->name
,
480 safe_strerror(errno
));
482 * In this case we are screwed.
483 * There is no good way to
484 * recover zebra at this point.
491 zlog_err("%s EOF", nl
->name
);
495 if (msg
.msg_namelen
!= sizeof snl
) {
496 zlog_err("%s sender address length error: length %d",
497 nl
->name
, msg
.msg_namelen
);
501 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
502 zlog_debug("%s: << netlink message dump [recv]",
504 zlog_hexdump(buf
, status
);
508 for (h
= (struct nlmsghdr
*)buf
;
509 NLMSG_OK(h
, (unsigned int)status
);
510 h
= NLMSG_NEXT(h
, status
)) {
511 /* Finish of reading. */
512 if (h
->nlmsg_type
== NLMSG_DONE
)
515 /* Error handling. */
516 if (h
->nlmsg_type
== NLMSG_ERROR
) {
517 struct nlmsgerr
*err
=
518 (struct nlmsgerr
*)NLMSG_DATA(h
);
519 int errnum
= err
->error
;
520 int msg_type
= err
->msg
.nlmsg_type
;
522 /* If the error field is zero, then this is an
524 if (err
->error
== 0) {
525 if (IS_ZEBRA_DEBUG_KERNEL
) {
527 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
528 __FUNCTION__
, nl
->name
,
530 err
->msg
.nlmsg_type
),
536 /* return if not a multipart message,
537 * otherwise continue */
538 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
544 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
545 zlog_err("%s error: message truncated",
550 /* Deal with errors that occur because of races
551 * in link handling */
552 if (nl
== &zns
->netlink_cmd
553 && ((msg_type
== RTM_DELROUTE
554 && (-errnum
== ENODEV
555 || -errnum
== ESRCH
))
556 || (msg_type
== RTM_NEWROUTE
557 && (-errnum
== ENETDOWN
558 || -errnum
== EEXIST
)))) {
559 if (IS_ZEBRA_DEBUG_KERNEL
)
561 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
563 safe_strerror(-errnum
),
572 /* We see RTM_DELNEIGH when shutting down an
573 * interface with an IPv4
574 * link-local. The kernel should have already
575 * deleted the neighbor
576 * so do not log these as an error.
578 if (msg_type
== RTM_DELNEIGH
579 || (nl
== &zns
->netlink_cmd
580 && msg_type
== RTM_NEWROUTE
582 || -errnum
== ENETUNREACH
))) {
583 /* This is known to happen in some
584 * situations, don't log
587 if (IS_ZEBRA_DEBUG_KERNEL
)
589 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
591 safe_strerror(-errnum
),
599 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
601 safe_strerror(-errnum
),
602 nl_msg_type_to_str(msg_type
),
603 msg_type
, err
->msg
.nlmsg_seq
,
609 /* OK we got netlink message. */
610 if (IS_ZEBRA_DEBUG_KERNEL
)
612 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
614 nl_msg_type_to_str(h
->nlmsg_type
),
615 h
->nlmsg_type
, h
->nlmsg_len
,
616 h
->nlmsg_seq
, h
->nlmsg_pid
);
618 /* skip unsolicited messages originating from command
620 * linux sets the originators port-id for {NEW|DEL}ADDR
622 * so this has to be checked here. */
623 if (nl
!= &zns
->netlink_cmd
624 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
625 && (h
->nlmsg_type
!= RTM_NEWADDR
626 && h
->nlmsg_type
!= RTM_DELADDR
)) {
627 if (IS_ZEBRA_DEBUG_KERNEL
)
629 "netlink_parse_info: %s packet comes from %s",
630 zns
->netlink_cmd
.name
,
635 error
= (*filter
)(&snl
, h
, zns
->ns_id
, startup
);
637 zlog_err("%s filter function error", nl
->name
);
642 /* After error care. */
643 if (msg
.msg_flags
& MSG_TRUNC
) {
644 zlog_err("%s error: message truncated", nl
->name
);
648 zlog_err("%s error: data remnant size %d", nl
->name
,
659 * sendmsg() to netlink socket then recvmsg().
660 * Calls netlink_parse_info to parse returned data
662 * filter -> The filter to read final results from kernel
663 * nlmsghdr -> The data to send to the kernel
664 * nl -> The netlink socket information
665 * zns -> The zebra namespace information
666 * startup -> Are we reading in under startup conditions
667 * This is passed through eventually to filter.
669 int netlink_talk(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*, ns_id_t
,
671 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
675 struct sockaddr_nl snl
;
676 struct iovec iov
= {.iov_base
= (void *)n
, .iov_len
= n
->nlmsg_len
};
677 struct msghdr msg
= {
678 .msg_name
= (void *)&snl
,
679 .msg_namelen
= sizeof snl
,
685 memset(&snl
, 0, sizeof snl
);
686 snl
.nl_family
= AF_NETLINK
;
688 n
->nlmsg_seq
= ++nl
->seq
;
689 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
691 /* Request an acknowledgement by setting NLM_F_ACK */
692 n
->nlmsg_flags
|= NLM_F_ACK
;
694 if (IS_ZEBRA_DEBUG_KERNEL
)
696 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
697 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
698 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
701 /* Send message to netlink interface. */
702 if (zserv_privs
.change(ZPRIVS_RAISE
))
703 zlog_err("Can't raise privileges");
704 status
= sendmsg(nl
->sock
, &msg
, 0);
706 if (zserv_privs
.change(ZPRIVS_LOWER
))
707 zlog_err("Can't lower privileges");
709 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
710 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
711 zlog_hexdump(n
, n
->nlmsg_len
);
715 zlog_err("netlink_talk sendmsg() error: %s",
716 safe_strerror(save_errno
));
722 * Get reply from netlink socket.
723 * The reply should either be an acknowlegement or an error.
725 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
728 /* Issue request message to kernel via netlink socket. GET messages
729 * are issued through this interface.
731 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
734 struct sockaddr_nl snl
;
737 /* Check netlink socket. */
739 zlog_err("%s socket isn't active.", nl
->name
);
743 /* Fill common fields for all requests. */
744 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
745 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
746 n
->nlmsg_seq
= ++nl
->seq
;
748 memset(&snl
, 0, sizeof snl
);
749 snl
.nl_family
= AF_NETLINK
;
751 /* Raise capabilities and send message, then lower capabilities. */
752 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
753 zlog_err("Can't raise privileges");
757 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
758 (struct sockaddr
*)&snl
, sizeof snl
);
761 if (zserv_privs
.change(ZPRIVS_LOWER
))
762 zlog_err("Can't lower privileges");
765 zlog_err("%s sendto failed: %s", nl
->name
,
766 safe_strerror(save_errno
));
773 /* Exported interface function. This function simply calls
774 netlink_socket (). */
775 void kernel_init(struct zebra_ns
*zns
)
777 unsigned long groups
;
779 /* Initialize netlink sockets */
780 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
781 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV4_MROUTE
784 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
785 "netlink-listen (NS %u)", zns
->ns_id
);
786 zns
->netlink
.sock
= -1;
787 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
789 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
790 "netlink-cmd (NS %u)", zns
->ns_id
);
791 zns
->netlink_cmd
.sock
= -1;
792 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
794 /* Register kernel socket. */
795 if (zns
->netlink
.sock
> 0) {
796 /* Only want non-blocking on the netlink event socket */
797 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
798 zlog_err("Can't set %s socket flags: %s",
799 zns
->netlink
.name
, safe_strerror(errno
));
801 /* Set receive buffer size if it's set from command line */
803 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
805 netlink_install_filter(zns
->netlink
.sock
,
806 zns
->netlink_cmd
.snl
.nl_pid
);
807 zns
->t_netlink
= NULL
;
808 thread_add_read(zebrad
.master
, kernel_read
, zns
,
809 zns
->netlink
.sock
, &zns
->t_netlink
);
815 void kernel_terminate(struct zebra_ns
*zns
)
817 THREAD_READ_OFF(zns
->t_netlink
);
819 if (zns
->netlink
.sock
>= 0) {
820 close(zns
->netlink
.sock
);
821 zns
->netlink
.sock
= -1;
824 if (zns
->netlink_cmd
.sock
>= 0) {
825 close(zns
->netlink_cmd
.sock
);
826 zns
->netlink_cmd
.sock
= -1;