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"
49 #ifndef SO_RCVBUFFORCE
50 #define SO_RCVBUFFORCE (33)
53 /* Hack for GNU libc version 2. */
55 #define MSG_TRUNC 0x20
56 #endif /* MSG_TRUNC */
59 #define NLMSG_TAIL(nmsg) \
60 ((struct rtattr *)(((u_char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
64 #define RTA_TAIL(rta) \
65 ((struct rtattr *)(((u_char *)(rta)) + RTA_ALIGN((rta)->rta_len)))
68 #ifndef RTNL_FAMILY_IP6MR
69 #define RTNL_FAMILY_IP6MR 129
72 #ifndef RTPROT_MROUTED
73 #define RTPROT_MROUTED 17
76 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
77 {RTM_DELROUTE
, "RTM_DELROUTE"},
78 {RTM_GETROUTE
, "RTM_GETROUTE"},
79 {RTM_NEWLINK
, "RTM_NEWLINK"},
80 {RTM_DELLINK
, "RTM_DELLINK"},
81 {RTM_GETLINK
, "RTM_GETLINK"},
82 {RTM_NEWADDR
, "RTM_NEWADDR"},
83 {RTM_DELADDR
, "RTM_DELADDR"},
84 {RTM_GETADDR
, "RTM_GETADDR"},
85 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
86 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
87 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
90 static const struct message rtproto_str
[] = {
91 {RTPROT_REDIRECT
, "redirect"},
92 {RTPROT_KERNEL
, "kernel"},
93 {RTPROT_BOOT
, "boot"},
94 {RTPROT_STATIC
, "static"},
95 {RTPROT_GATED
, "GateD"},
96 {RTPROT_RA
, "router advertisement"},
98 {RTPROT_ZEBRA
, "Zebra"},
100 {RTPROT_BIRD
, "BIRD"},
101 #endif /* RTPROT_BIRD */
102 {RTPROT_MROUTED
, "mroute"},
104 {RTPROT_OSPF
, "OSPF"},
105 {RTPROT_ISIS
, "IS-IS"},
107 {RTPROT_RIPNG
, "RIPNG"},
110 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
112 {AF_BRIDGE
, "bridge"},
113 {RTNL_FAMILY_IPMR
, "ipv4MR"},
114 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
117 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
118 {RTN_MULTICAST
, "multicast"},
121 extern struct thread_master
*master
;
122 extern u_int32_t nl_rcvbufsize
;
124 extern struct zebra_privs_t zserv_privs
;
126 int netlink_talk_filter(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
127 ns_id_t ns_id
, int startup
)
129 zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u",
130 h
->nlmsg_type
, ns_id
);
134 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
137 socklen_t newlen
= sizeof(newsize
);
138 socklen_t oldlen
= sizeof(oldsize
);
141 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
143 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
144 safe_strerror(errno
));
148 /* Try force option (linux >= 2.6.14) and fall back to normal set */
149 if (zserv_privs
.change(ZPRIVS_RAISE
))
150 zlog_err("routing_socket: Can't raise privileges");
151 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
152 sizeof(nl_rcvbufsize
));
153 if (zserv_privs
.change(ZPRIVS_LOWER
))
154 zlog_err("routing_socket: Can't lower privileges");
156 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
157 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
159 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
160 safe_strerror(errno
));
164 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
166 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
167 safe_strerror(errno
));
171 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
176 /* Make socket for Linux netlink interface. */
177 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
181 struct sockaddr_nl snl
;
186 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
187 zlog_err("Can't raise privileges");
191 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
193 zlog_err("Can't open %s socket: %s", nl
->name
,
194 safe_strerror(errno
));
198 memset(&snl
, 0, sizeof snl
);
199 snl
.nl_family
= AF_NETLINK
;
200 snl
.nl_groups
= groups
;
202 /* Bind the socket to the netlink structure for anything. */
203 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
205 if (zserv_privs
.change(ZPRIVS_LOWER
))
206 zlog_err("Can't lower privileges");
209 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
210 snl
.nl_groups
, safe_strerror(save_errno
));
215 /* multiple netlink sockets will have different nl_pid */
216 namelen
= sizeof snl
;
217 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
218 if (ret
< 0 || namelen
!= sizeof snl
) {
219 zlog_err("Can't get %s socket name: %s", nl
->name
,
220 safe_strerror(errno
));
230 static int netlink_information_fetch(struct sockaddr_nl
*snl
,
231 struct nlmsghdr
*h
, ns_id_t ns_id
,
234 /* JF: Ignore messages that aren't from the kernel */
235 if (snl
->nl_pid
!= 0) {
236 zlog_err("Ignoring message from pid %u", snl
->nl_pid
);
240 switch (h
->nlmsg_type
) {
242 return netlink_route_change(snl
, h
, ns_id
, startup
);
245 return netlink_route_change(snl
, h
, ns_id
, startup
);
248 return netlink_link_change(snl
, h
, ns_id
, startup
);
251 return netlink_link_change(snl
, h
, ns_id
, startup
);
254 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
257 return netlink_interface_addr(snl
, h
, ns_id
, startup
);
260 return netlink_neigh_change(snl
, h
, ns_id
);
263 return netlink_neigh_change(snl
, h
, ns_id
);
266 if (IS_ZEBRA_DEBUG_KERNEL
)
267 zlog_debug("Unknown netlink nlmsg_type %d vrf %u\n",
268 h
->nlmsg_type
, ns_id
);
274 static int kernel_read(struct thread
*thread
)
276 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
277 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
278 zns
->t_netlink
= NULL
;
279 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
285 /* Filter out messages from self that occur on listener socket,
286 * caused by our actions on the command socket
288 static void netlink_install_filter(int sock
, __u32 pid
)
290 struct sock_filter filter
[] = {
292 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
293 offsetof(struct nlmsghdr
, nlmsg_type
)),
294 /* 1: jeq 0x18 jt 5 jf next */
295 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWROUTE
), 3, 0),
296 /* 2: jeq 0x19 jt 5 jf next */
297 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELROUTE
), 2, 0),
298 /* 3: jeq 0x19 jt 5 jf next */
299 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNEIGH
), 1, 0),
300 /* 4: jeq 0x19 jt 5 jf 8 */
301 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNEIGH
), 0, 3),
303 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
304 offsetof(struct nlmsghdr
, nlmsg_pid
)),
305 /* 6: jeq XX jt 7 jf 8 */
306 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 1),
307 /* 7: ret 0 (skip) */
308 BPF_STMT(BPF_RET
| BPF_K
, 0),
309 /* 8: ret 0xffff (keep) */
310 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
313 struct sock_fprog prog
= {
314 .len
= array_size(filter
), .filter
= filter
,
317 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
319 zlog_warn("Can't install socket filter: %s\n",
320 safe_strerror(errno
));
323 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
326 while (RTA_OK(rta
, len
)) {
327 if (rta
->rta_type
<= max
)
328 tb
[rta
->rta_type
] = rta
;
329 rta
= RTA_NEXT(rta
, len
);
333 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
339 len
= RTA_LENGTH(alen
);
341 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
344 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
345 rta
->rta_type
= type
;
349 memcpy(RTA_DATA(rta
), data
, alen
);
353 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
358 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
362 struct rtattr
*subrta
;
364 len
= RTA_LENGTH(alen
);
366 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
369 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
370 subrta
->rta_type
= type
;
371 subrta
->rta_len
= len
;
374 memcpy(RTA_DATA(subrta
), data
, alen
);
378 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
383 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, u_int16_t data
)
385 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int16_t
));
388 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
390 return addattr_l(n
, maxlen
, type
, &data
, sizeof(u_int32_t
));
393 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
395 struct rtattr
*nest
= NLMSG_TAIL(n
);
397 addattr_l(n
, maxlen
, type
, NULL
, 0);
401 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
403 nest
->rta_len
= (u_char
*)NLMSG_TAIL(n
) - (u_char
*)nest
;
407 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
409 struct rtattr
*nest
= RTA_TAIL(rta
);
411 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
415 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
417 nest
->rta_len
= (u_char
*)RTA_TAIL(rta
) - (u_char
*)nest
;
421 const char *nl_msg_type_to_str(uint16_t msg_type
)
423 return lookup_msg(nlmsg_str
, msg_type
, "");
426 const char *nl_rtproto_to_str(u_char rtproto
)
428 return lookup_msg(rtproto_str
, rtproto
, "");
431 const char *nl_family_to_str(u_char family
)
433 return lookup_msg(family_str
, family
, "");
436 const char *nl_rttype_to_str(u_char rttype
)
438 return lookup_msg(rttype_str
, rttype
, "");
444 * Receive message from netlink interface and pass those information
445 * to the given function.
447 * filter -> Function to call to read the results
448 * nl -> netlink socket information
449 * zns -> The zebra namespace data
450 * count -> How many we should read in, 0 means as much as possible
451 * startup -> Are we reading in under startup conditions? passed to
454 int netlink_parse_info(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*,
456 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
465 char buf
[NL_PKT_BUF_SIZE
];
466 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
467 struct sockaddr_nl snl
;
468 struct msghdr msg
= {.msg_name
= (void *)&snl
,
469 .msg_namelen
= sizeof snl
,
474 if (count
&& read_in
>= count
)
477 status
= recvmsg(nl
->sock
, &msg
, 0);
481 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
483 zlog_err("%s recvmsg overrun: %s", nl
->name
,
484 safe_strerror(errno
));
486 * In this case we are screwed.
487 * There is no good way to
488 * recover zebra at this point.
495 zlog_err("%s EOF", nl
->name
);
499 if (msg
.msg_namelen
!= sizeof snl
) {
500 zlog_err("%s sender address length error: length %d",
501 nl
->name
, msg
.msg_namelen
);
505 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
506 zlog_debug("%s: << netlink message dump [recv]",
508 zlog_hexdump(buf
, status
);
512 for (h
= (struct nlmsghdr
*)buf
;
513 NLMSG_OK(h
, (unsigned int)status
);
514 h
= NLMSG_NEXT(h
, status
)) {
515 /* Finish of reading. */
516 if (h
->nlmsg_type
== NLMSG_DONE
)
519 /* Error handling. */
520 if (h
->nlmsg_type
== NLMSG_ERROR
) {
521 struct nlmsgerr
*err
=
522 (struct nlmsgerr
*)NLMSG_DATA(h
);
523 int errnum
= err
->error
;
524 int msg_type
= err
->msg
.nlmsg_type
;
526 /* If the error field is zero, then this is an
528 if (err
->error
== 0) {
529 if (IS_ZEBRA_DEBUG_KERNEL
) {
531 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
532 __FUNCTION__
, nl
->name
,
534 err
->msg
.nlmsg_type
),
540 /* return if not a multipart message,
541 * otherwise continue */
542 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
548 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
549 zlog_err("%s error: message truncated",
554 /* Deal with errors that occur because of races
555 * in link handling */
556 if (nl
== &zns
->netlink_cmd
557 && ((msg_type
== RTM_DELROUTE
558 && (-errnum
== ENODEV
559 || -errnum
== ESRCH
))
560 || (msg_type
== RTM_NEWROUTE
561 && (-errnum
== ENETDOWN
562 || -errnum
== EEXIST
)))) {
563 if (IS_ZEBRA_DEBUG_KERNEL
)
565 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
567 safe_strerror(-errnum
),
576 /* We see RTM_DELNEIGH when shutting down an
577 * interface with an IPv4
578 * link-local. The kernel should have already
579 * deleted the neighbor
580 * so do not log these as an error.
582 if (msg_type
== RTM_DELNEIGH
583 || (nl
== &zns
->netlink_cmd
584 && msg_type
== RTM_NEWROUTE
586 || -errnum
== ENETUNREACH
))) {
587 /* This is known to happen in some
588 * situations, don't log
591 if (IS_ZEBRA_DEBUG_KERNEL
)
593 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
595 safe_strerror(-errnum
),
603 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
605 safe_strerror(-errnum
),
606 nl_msg_type_to_str(msg_type
),
607 msg_type
, err
->msg
.nlmsg_seq
,
613 /* OK we got netlink message. */
614 if (IS_ZEBRA_DEBUG_KERNEL
)
616 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
618 nl_msg_type_to_str(h
->nlmsg_type
),
619 h
->nlmsg_type
, h
->nlmsg_len
,
620 h
->nlmsg_seq
, h
->nlmsg_pid
);
622 /* skip unsolicited messages originating from command
624 * linux sets the originators port-id for {NEW|DEL}ADDR
626 * so this has to be checked here. */
627 if (nl
!= &zns
->netlink_cmd
628 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
629 && (h
->nlmsg_type
!= RTM_NEWADDR
630 && h
->nlmsg_type
!= RTM_DELADDR
)) {
631 if (IS_ZEBRA_DEBUG_KERNEL
)
633 "netlink_parse_info: %s packet comes from %s",
634 zns
->netlink_cmd
.name
,
639 error
= (*filter
)(&snl
, h
, zns
->ns_id
, startup
);
641 zlog_err("%s filter function error", nl
->name
);
646 /* After error care. */
647 if (msg
.msg_flags
& MSG_TRUNC
) {
648 zlog_err("%s error: message truncated", nl
->name
);
652 zlog_err("%s error: data remnant size %d", nl
->name
,
663 * sendmsg() to netlink socket then recvmsg().
664 * Calls netlink_parse_info to parse returned data
666 * filter -> The filter to read final results from kernel
667 * nlmsghdr -> The data to send to the kernel
668 * nl -> The netlink socket information
669 * zns -> The zebra namespace information
670 * startup -> Are we reading in under startup conditions
671 * This is passed through eventually to filter.
673 int netlink_talk(int (*filter
)(struct sockaddr_nl
*, struct nlmsghdr
*, ns_id_t
,
675 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
679 struct sockaddr_nl snl
;
684 memset(&snl
, 0, sizeof snl
);
685 memset(&iov
, 0, sizeof iov
);
686 memset(&msg
, 0, sizeof msg
);
689 iov
.iov_len
= n
->nlmsg_len
;
690 msg
.msg_name
= (void *)&snl
;
691 msg
.msg_namelen
= sizeof snl
;
695 snl
.nl_family
= AF_NETLINK
;
697 n
->nlmsg_seq
= ++nl
->seq
;
698 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
700 /* Request an acknowledgement by setting NLM_F_ACK */
701 n
->nlmsg_flags
|= NLM_F_ACK
;
703 if (IS_ZEBRA_DEBUG_KERNEL
)
705 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
706 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
707 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
710 /* Send message to netlink interface. */
711 if (zserv_privs
.change(ZPRIVS_RAISE
))
712 zlog_err("Can't raise privileges");
713 status
= sendmsg(nl
->sock
, &msg
, 0);
715 if (zserv_privs
.change(ZPRIVS_LOWER
))
716 zlog_err("Can't lower privileges");
718 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
719 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
720 zlog_hexdump(n
, n
->nlmsg_len
);
724 zlog_err("netlink_talk sendmsg() error: %s",
725 safe_strerror(save_errno
));
731 * Get reply from netlink socket.
732 * The reply should either be an acknowlegement or an error.
734 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
737 /* Issue request message to kernel via netlink socket. GET messages
738 * are issued through this interface.
740 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
743 struct sockaddr_nl snl
;
746 /* Check netlink socket. */
748 zlog_err("%s socket isn't active.", nl
->name
);
752 /* Fill common fields for all requests. */
753 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
754 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
755 n
->nlmsg_seq
= ++nl
->seq
;
757 memset(&snl
, 0, sizeof snl
);
758 snl
.nl_family
= AF_NETLINK
;
760 /* Raise capabilities and send message, then lower capabilities. */
761 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
762 zlog_err("Can't raise privileges");
766 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
767 (struct sockaddr
*)&snl
, sizeof snl
);
770 if (zserv_privs
.change(ZPRIVS_LOWER
))
771 zlog_err("Can't lower privileges");
774 zlog_err("%s sendto failed: %s", nl
->name
,
775 safe_strerror(save_errno
));
782 /* Exported interface function. This function simply calls
783 netlink_socket (). */
784 void kernel_init(struct zebra_ns
*zns
)
786 unsigned long groups
;
788 /* Initialize netlink sockets */
789 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
790 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
| RTMGRP_IPV4_MROUTE
793 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
794 "netlink-listen (NS %u)", zns
->ns_id
);
795 zns
->netlink
.sock
= -1;
796 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
798 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
799 "netlink-cmd (NS %u)", zns
->ns_id
);
800 zns
->netlink_cmd
.sock
= -1;
801 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
803 /* Register kernel socket. */
804 if (zns
->netlink
.sock
> 0) {
805 /* Only want non-blocking on the netlink event socket */
806 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
807 zlog_err("Can't set %s socket flags: %s",
808 zns
->netlink
.name
, safe_strerror(errno
));
810 /* Set receive buffer size if it's set from command line */
812 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
814 netlink_install_filter(zns
->netlink
.sock
,
815 zns
->netlink_cmd
.snl
.nl_pid
);
816 zns
->t_netlink
= NULL
;
817 thread_add_read(zebrad
.master
, kernel_read
, zns
,
818 zns
->netlink
.sock
, &zns
->t_netlink
);
824 void kernel_terminate(struct zebra_ns
*zns
)
826 THREAD_READ_OFF(zns
->t_netlink
);
828 if (zns
->netlink
.sock
>= 0) {
829 close(zns
->netlink
.sock
);
830 zns
->netlink
.sock
= -1;
833 if (zns
->netlink_cmd
.sock
>= 0) {
834 close(zns
->netlink_cmd
.sock
);
835 zns
->netlink_cmd
.sock
= -1;
839 #endif /* HAVE_NETLINK */