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 zlog_warn("Unknown netlink nlmsg_type %d vrf %u\n",
266 h
->nlmsg_type
, ns_id
);
272 static int kernel_read(struct thread
*thread
)
274 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
275 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
276 zns
->t_netlink
= NULL
;
277 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
283 /* Filter out messages from self that occur on listener socket,
284 * caused by our actions on the command socket
286 static void netlink_install_filter(int sock
, __u32 pid
)
288 struct sock_filter filter
[] = {
290 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
291 offsetof(struct nlmsghdr
, nlmsg_type
)),
292 /* 1: jeq 0x18 jt 5 jf next */
293 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWROUTE
), 3, 0),
294 /* 2: jeq 0x19 jt 5 jf next */
295 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELROUTE
), 2, 0),
296 /* 3: jeq 0x19 jt 5 jf next */
297 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNEIGH
), 1, 0),
298 /* 4: jeq 0x19 jt 5 jf 8 */
299 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNEIGH
), 0, 3),
301 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
302 offsetof(struct nlmsghdr
, nlmsg_pid
)),
303 /* 6: jeq XX jt 7 jf 8 */
304 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 1),
305 /* 7: ret 0 (skip) */
306 BPF_STMT(BPF_RET
| BPF_K
, 0),
307 /* 8: ret 0xffff (keep) */
308 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
311 struct sock_fprog prog
= {
312 .len
= array_size(filter
), .filter
= filter
,
315 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
317 zlog_warn("Can't install socket filter: %s\n",
318 safe_strerror(errno
));
321 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
324 while (RTA_OK(rta
, len
)) {
325 if (rta
->rta_type
<= max
)
326 tb
[rta
->rta_type
] = rta
;
327 rta
= RTA_NEXT(rta
, len
);
331 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
337 len
= RTA_LENGTH(alen
);
339 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
342 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
343 rta
->rta_type
= type
;
347 memcpy(RTA_DATA(rta
), data
, alen
);
351 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
356 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
360 struct rtattr
*subrta
;
362 len
= RTA_LENGTH(alen
);
364 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
367 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
368 subrta
->rta_type
= type
;
369 subrta
->rta_len
= len
;
372 memcpy(RTA_DATA(subrta
), data
, alen
);
376 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
381 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, u_int16_t data
)
383 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int16_t
));
386 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
388 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int32_t
));
391 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
393 struct rtattr
*nest
= NLMSG_TAIL(n
);
395 addattr_l(n
, maxlen
, type
, NULL
, 0);
399 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
401 nest
->rta_len
= (u_char
*)NLMSG_TAIL(n
) - (u_char
*)nest
;
405 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
407 struct rtattr
*nest
= RTA_TAIL(rta
);
409 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
413 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
415 nest
->rta_len
= (u_char
*)RTA_TAIL(rta
) - (u_char
*)nest
;
419 const char *nl_msg_type_to_str(uint16_t msg_type
)
421 return lookup_msg(nlmsg_str
, msg_type
, "");
424 const char *nl_rtproto_to_str(u_char rtproto
)
426 return lookup_msg(rtproto_str
, rtproto
, "");
429 const char *nl_family_to_str(u_char family
)
431 return lookup_msg(family_str
, family
, "");
434 const char *nl_rttype_to_str(u_char rttype
)
436 return lookup_msg(rttype_str
, rttype
, "");
442 * Receive message from netlink interface and pass those information
443 * to the given function.
445 * filter -> Function to call to read the results
446 * nl -> netlink socket information
447 * zns -> The zebra namespace data
448 * count -> How many we should read in, 0 means as much as possible
449 * startup -> Are we reading in under startup conditions? passed to
452 int netlink_parse_info(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*,
454 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
463 char buf
[NL_PKT_BUF_SIZE
];
464 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
465 struct sockaddr_nl snl
;
466 struct msghdr msg
= {.msg_name
= (void *)&snl
,
467 .msg_namelen
= sizeof snl
,
472 if (count
&& read_in
>= count
)
475 status
= recvmsg(nl
->sock
, &msg
, 0);
479 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
481 zlog_err("%s recvmsg overrun: %s", nl
->name
,
482 safe_strerror(errno
));
484 * In this case we are screwed.
485 * There is no good way to
486 * recover zebra at this point.
493 zlog_err("%s EOF", nl
->name
);
497 if (msg
.msg_namelen
!= sizeof snl
) {
498 zlog_err("%s sender address length error: length %d",
499 nl
->name
, msg
.msg_namelen
);
503 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
504 zlog_debug("%s: << netlink message dump [recv]",
506 zlog_hexdump(buf
, status
);
510 for (h
= (struct nlmsghdr
*)buf
;
511 NLMSG_OK(h
, (unsigned int)status
);
512 h
= NLMSG_NEXT(h
, status
)) {
513 /* Finish of reading. */
514 if (h
->nlmsg_type
== NLMSG_DONE
)
517 /* Error handling. */
518 if (h
->nlmsg_type
== NLMSG_ERROR
) {
519 struct nlmsgerr
*err
=
520 (struct nlmsgerr
*)NLMSG_DATA(h
);
521 int errnum
= err
->error
;
522 int msg_type
= err
->msg
.nlmsg_type
;
524 /* If the error field is zero, then this is an
526 if (err
->error
== 0) {
527 if (IS_ZEBRA_DEBUG_KERNEL
) {
529 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
530 __FUNCTION__
, nl
->name
,
532 err
->msg
.nlmsg_type
),
538 /* return if not a multipart message,
539 * otherwise continue */
540 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
546 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
547 zlog_err("%s error: message truncated",
552 /* Deal with errors that occur because of races
553 * in link handling */
554 if (nl
== &zns
->netlink_cmd
555 && ((msg_type
== RTM_DELROUTE
556 && (-errnum
== ENODEV
557 || -errnum
== ESRCH
))
558 || (msg_type
== RTM_NEWROUTE
559 && (-errnum
== ENETDOWN
560 || -errnum
== EEXIST
)))) {
561 if (IS_ZEBRA_DEBUG_KERNEL
)
563 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
565 safe_strerror(-errnum
),
574 /* We see RTM_DELNEIGH when shutting down an
575 * interface with an IPv4
576 * link-local. The kernel should have already
577 * deleted the neighbor
578 * so do not log these as an error.
580 if (msg_type
== RTM_DELNEIGH
581 || (nl
== &zns
->netlink_cmd
582 && msg_type
== RTM_NEWROUTE
584 || -errnum
== ENETUNREACH
))) {
585 /* This is known to happen in some
586 * situations, don't log
589 if (IS_ZEBRA_DEBUG_KERNEL
)
591 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
593 safe_strerror(-errnum
),
601 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
603 safe_strerror(-errnum
),
604 nl_msg_type_to_str(msg_type
),
605 msg_type
, err
->msg
.nlmsg_seq
,
611 /* OK we got netlink message. */
612 if (IS_ZEBRA_DEBUG_KERNEL
)
614 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
616 nl_msg_type_to_str(h
->nlmsg_type
),
617 h
->nlmsg_type
, h
->nlmsg_len
,
618 h
->nlmsg_seq
, h
->nlmsg_pid
);
620 /* skip unsolicited messages originating from command
622 * linux sets the originators port-id for {NEW|DEL}ADDR
624 * so this has to be checked here. */
625 if (nl
!= &zns
->netlink_cmd
626 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
627 && (h
->nlmsg_type
!= RTM_NEWADDR
628 && h
->nlmsg_type
!= RTM_DELADDR
)) {
629 if (IS_ZEBRA_DEBUG_KERNEL
)
631 "netlink_parse_info: %s packet comes from %s",
632 zns
->netlink_cmd
.name
,
637 error
= (*filter
)(&snl
, h
, zns
->ns_id
, startup
);
639 zlog_err("%s filter function error", nl
->name
);
644 /* After error care. */
645 if (msg
.msg_flags
& MSG_TRUNC
) {
646 zlog_err("%s error: message truncated", nl
->name
);
650 zlog_err("%s error: data remnant size %d", nl
->name
,
661 * sendmsg() to netlink socket then recvmsg().
662 * Calls netlink_parse_info to parse returned data
664 * filter -> The filter to read final results from kernel
665 * nlmsghdr -> The data to send to the kernel
666 * nl -> The netlink socket information
667 * zns -> The zebra namespace information
668 * startup -> Are we reading in under startup conditions
669 * This is passed through eventually to filter.
671 int netlink_talk(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*, ns_id_t
,
673 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
677 struct sockaddr_nl snl
;
678 struct iovec iov
= {.iov_base
= (void *)n
, .iov_len
= n
->nlmsg_len
};
679 struct msghdr msg
= {
680 .msg_name
= (void *)&snl
,
681 .msg_namelen
= sizeof snl
,
687 memset(&snl
, 0, sizeof snl
);
688 snl
.nl_family
= AF_NETLINK
;
690 n
->nlmsg_seq
= ++nl
->seq
;
691 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
693 /* Request an acknowledgement by setting NLM_F_ACK */
694 n
->nlmsg_flags
|= NLM_F_ACK
;
696 if (IS_ZEBRA_DEBUG_KERNEL
)
698 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
699 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
700 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
703 /* Send message to netlink interface. */
704 if (zserv_privs
.change(ZPRIVS_RAISE
))
705 zlog_err("Can't raise privileges");
706 status
= sendmsg(nl
->sock
, &msg
, 0);
708 if (zserv_privs
.change(ZPRIVS_LOWER
))
709 zlog_err("Can't lower privileges");
711 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
712 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
713 zlog_hexdump(n
, n
->nlmsg_len
);
717 zlog_err("netlink_talk sendmsg() error: %s",
718 safe_strerror(save_errno
));
724 * Get reply from netlink socket.
725 * The reply should either be an acknowlegement or an error.
727 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
730 /* Issue request message to kernel via netlink socket. GET messages
731 * are issued through this interface.
733 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
736 struct sockaddr_nl snl
;
739 /* Check netlink socket. */
741 zlog_err("%s socket isn't active.", nl
->name
);
745 /* Fill common fields for all requests. */
746 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
747 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
748 n
->nlmsg_seq
= ++nl
->seq
;
750 memset(&snl
, 0, sizeof snl
);
751 snl
.nl_family
= AF_NETLINK
;
753 /* Raise capabilities and send message, then lower capabilities. */
754 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
755 zlog_err("Can't raise privileges");
759 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
760 (struct sockaddr
*)&snl
, sizeof snl
);
763 if (zserv_privs
.change(ZPRIVS_LOWER
))
764 zlog_err("Can't lower privileges");
767 zlog_err("%s sendto failed: %s", nl
->name
,
768 safe_strerror(save_errno
));
775 /* Exported interface function. This function simply calls
776 netlink_socket (). */
777 void kernel_init(struct zebra_ns
*zns
)
779 unsigned long groups
;
781 /* Initialize netlink sockets */
782 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
783 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV4_MROUTE
786 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
787 "netlink-listen (NS %u)", zns
->ns_id
);
788 zns
->netlink
.sock
= -1;
789 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
791 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
792 "netlink-cmd (NS %u)", zns
->ns_id
);
793 zns
->netlink_cmd
.sock
= -1;
794 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
796 /* Register kernel socket. */
797 if (zns
->netlink
.sock
> 0) {
798 /* Only want non-blocking on the netlink event socket */
799 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
800 zlog_err("Can't set %s socket flags: %s",
801 zns
->netlink
.name
, safe_strerror(errno
));
803 /* Set receive buffer size if it's set from command line */
805 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
807 netlink_install_filter(zns
->netlink
.sock
,
808 zns
->netlink_cmd
.snl
.nl_pid
);
809 zns
->t_netlink
= NULL
;
810 thread_add_read(zebrad
.master
, kernel_read
, zns
,
811 zns
->netlink
.sock
, &zns
->t_netlink
);
817 void kernel_terminate(struct zebra_ns
*zns
)
819 THREAD_READ_OFF(zns
->t_netlink
);
821 if (zns
->netlink
.sock
>= 0) {
822 close(zns
->netlink
.sock
);
823 zns
->netlink
.sock
= -1;
826 if (zns
->netlink_cmd
.sock
>= 0) {
827 close(zns
->netlink_cmd
.sock
);
828 zns
->netlink_cmd
.sock
= -1;
832 #endif /* HAVE_NETLINK */