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"},
113 {RTPROT_ZSTATIC
, "static"},
116 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
118 {AF_BRIDGE
, "bridge"},
119 {RTNL_FAMILY_IPMR
, "ipv4MR"},
120 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
123 static const struct message rttype_str
[] = {{RTN_UNICAST
, "unicast"},
124 {RTN_MULTICAST
, "multicast"},
127 extern struct thread_master
*master
;
128 extern uint32_t nl_rcvbufsize
;
130 extern struct zebra_privs_t zserv_privs
;
132 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
135 * This is an error condition that must be handled during
138 * The netlink_talk_filter function is used for communication
139 * down the netlink_cmd pipe and we are expecting
140 * an ack being received. So if we get here
141 * then we did not receive the ack and instead
142 * received some other message in an unexpected
145 zlog_err("%s: ignoring message type 0x%04x(%s) NS %u",
146 __PRETTY_FUNCTION__
, h
->nlmsg_type
,
147 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
151 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
154 socklen_t newlen
= sizeof(newsize
);
155 socklen_t oldlen
= sizeof(oldsize
);
158 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
160 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
161 safe_strerror(errno
));
165 /* Try force option (linux >= 2.6.14) and fall back to normal set */
166 if (zserv_privs
.change(ZPRIVS_RAISE
))
167 zlog_err("routing_socket: Can't raise privileges");
168 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
169 sizeof(nl_rcvbufsize
));
170 if (zserv_privs
.change(ZPRIVS_LOWER
))
171 zlog_err("routing_socket: Can't lower privileges");
173 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
,
174 &nl_rcvbufsize
, sizeof(nl_rcvbufsize
));
176 zlog_err("Can't set %s receive buffer size: %s", nl
->name
,
177 safe_strerror(errno
));
181 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
183 zlog_err("Can't get %s receive buffer size: %s", nl
->name
,
184 safe_strerror(errno
));
188 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
193 /* Make socket for Linux netlink interface. */
194 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
198 struct sockaddr_nl snl
;
203 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
204 zlog_err("Can't raise privileges");
208 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
210 zlog_err("Can't open %s socket: %s", nl
->name
,
211 safe_strerror(errno
));
215 memset(&snl
, 0, sizeof snl
);
216 snl
.nl_family
= AF_NETLINK
;
217 snl
.nl_groups
= groups
;
219 /* Bind the socket to the netlink structure for anything. */
220 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof snl
);
222 if (zserv_privs
.change(ZPRIVS_LOWER
))
223 zlog_err("Can't lower privileges");
226 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
227 snl
.nl_groups
, safe_strerror(save_errno
));
232 /* multiple netlink sockets will have different nl_pid */
233 namelen
= sizeof snl
;
234 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
235 if (ret
< 0 || namelen
!= sizeof snl
) {
236 zlog_err("Can't get %s socket name: %s", nl
->name
,
237 safe_strerror(errno
));
247 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
251 * When we handle new message types here
252 * because we are starting to install them
253 * then lets check the netlink_install_filter
254 * and see if we should add the corresponding
255 * allow through entry there.
256 * Probably not needed to do but please
259 switch (h
->nlmsg_type
) {
261 return netlink_route_change(h
, ns_id
, startup
);
263 return netlink_route_change(h
, ns_id
, startup
);
265 return netlink_link_change(h
, ns_id
, startup
);
267 return netlink_link_change(h
, ns_id
, startup
);
269 return netlink_interface_addr(h
, ns_id
, startup
);
271 return netlink_interface_addr(h
, ns_id
, startup
);
273 return netlink_neigh_change(h
, ns_id
);
275 return netlink_neigh_change(h
, ns_id
);
277 return netlink_rule_change(h
, ns_id
, startup
);
279 return netlink_rule_change(h
, ns_id
, startup
);
282 * If we have received this message then
283 * we have made a mistake during development
284 * and we need to write some code to handle
285 * this message type or not ask for
286 * it to be sent up to us
288 zlog_err("Unknown netlink nlmsg_type %s(%d) vrf %u\n",
289 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
296 static int kernel_read(struct thread
*thread
)
298 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
299 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, zns
, 5, 0);
300 zns
->t_netlink
= NULL
;
301 thread_add_read(zebrad
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
308 * Filter out messages from self that occur on listener socket,
309 * caused by our actions on the command socket
311 * When we add new Netlink message types we probably
312 * do not need to add them here as that we are filtering
313 * on the routes we actually care to receive( which is rarer
314 * then the normal course of operations). We are intentionally
315 * allowing some messages from ourselves through
316 * ( I'm looking at you Interface based netlink messages )
317 * so that we only had to write one way to handle incoming
318 * address add/delete changes.
320 static void netlink_install_filter(int sock
, __u32 pid
)
323 * BPF_JUMP instructions and where you jump to are based upon
324 * 0 as being the next statement. So count from 0. Writing
325 * this down because every time I look at this I have to
328 struct sock_filter filter
[] = {
331 * if (nlmsg_pid == pid) {
332 * if (the incoming nlmsg_type ==
333 * RTM_NEWADDR | RTM_DELADDR)
338 * keep this netlink message
341 * 0: Load the nlmsg_pid into the BPF register
343 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
344 offsetof(struct nlmsghdr
, nlmsg_pid
)),
348 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 0, 4),
350 * 2: Load the nlmsg_type into BPF register
352 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
353 offsetof(struct nlmsghdr
, nlmsg_type
)),
355 * 3: Compare to RTM_NEWADDR
357 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 2, 0),
359 * 4: Compare to RTM_DELADDR
361 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 1, 0),
363 * 5: This is the end state of we want to skip the
366 BPF_STMT(BPF_RET
| BPF_K
, 0),
367 /* 6: This is the end state of we want to keep
370 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
373 struct sock_fprog prog
= {
374 .len
= array_size(filter
), .filter
= filter
,
377 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
379 zlog_warn("Can't install socket filter: %s\n",
380 safe_strerror(errno
));
383 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
386 while (RTA_OK(rta
, len
)) {
387 if (rta
->rta_type
<= max
)
388 tb
[rta
->rta_type
] = rta
;
389 rta
= RTA_NEXT(rta
, len
);
393 int addattr_l(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
,
399 len
= RTA_LENGTH(alen
);
401 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
404 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
405 rta
->rta_type
= type
;
409 memcpy(RTA_DATA(rta
), data
, alen
);
413 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
418 int rta_addattr_l(struct rtattr
*rta
, unsigned int maxlen
, int type
, void *data
,
422 struct rtattr
*subrta
;
424 len
= RTA_LENGTH(alen
);
426 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
)
429 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
430 subrta
->rta_type
= type
;
431 subrta
->rta_len
= len
;
434 memcpy(RTA_DATA(subrta
), data
, alen
);
438 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
443 int addattr16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, uint16_t data
)
445 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint16_t));
448 int addattr32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
450 return addattr_l(n
, maxlen
, type
, &data
, sizeof(uint32_t));
453 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
455 struct rtattr
*nest
= NLMSG_TAIL(n
);
457 addattr_l(n
, maxlen
, type
, NULL
, 0);
461 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
463 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
467 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
469 struct rtattr
*nest
= RTA_TAIL(rta
);
471 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
475 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
477 nest
->rta_len
= (uint8_t *)RTA_TAIL(rta
) - (uint8_t *)nest
;
481 const char *nl_msg_type_to_str(uint16_t msg_type
)
483 return lookup_msg(nlmsg_str
, msg_type
, "");
486 const char *nl_rtproto_to_str(uint8_t rtproto
)
488 return lookup_msg(rtproto_str
, rtproto
, "");
491 const char *nl_family_to_str(uint8_t family
)
493 return lookup_msg(family_str
, family
, "");
496 const char *nl_rttype_to_str(uint8_t rttype
)
498 return lookup_msg(rttype_str
, rttype
, "");
501 #define NL_OK(nla, len) \
502 ((len) >= (int)sizeof(struct nlattr) \
503 && (nla)->nla_len >= sizeof(struct nlattr) \
504 && (nla)->nla_len <= (len))
505 #define NL_NEXT(nla, attrlen) \
506 ((attrlen) -= RTA_ALIGN((nla)->nla_len), \
507 (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len)))
509 ((struct nlattr *)(((char *)(r)) \
510 + NLMSG_ALIGN(sizeof(struct nlmsgerr))))
512 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
513 struct nlattr
*nla
, int len
)
515 while (NL_OK(nla
, len
)) {
516 if (nla
->nla_type
<= max
)
517 tb
[nla
->nla_type
] = nla
;
518 nla
= NL_NEXT(nla
, len
);
522 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
524 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1];
525 const struct nlmsgerr
*err
=
526 (const struct nlmsgerr
*)((uint8_t *)h
528 sizeof(struct nlmsghdr
)));
529 const struct nlmsghdr
*err_nlh
= NULL
;
530 uint32_t hlen
= sizeof(*err
);
531 const char *msg
= NULL
;
534 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
535 hlen
+= h
->nlmsg_len
- NLMSG_ALIGN(sizeof(struct nlmsghdr
));
537 memset(tb
, 0, sizeof(tb
));
538 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, NL_RTA(h
), hlen
);
540 if (tb
[NLMSGERR_ATTR_MSG
])
541 msg
= (const char *)RTA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
543 if (tb
[NLMSGERR_ATTR_OFFS
]) {
544 off
= *(uint32_t *)RTA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
546 if (off
> h
->nlmsg_len
) {
547 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n");
549 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
551 * Header of failed message
552 * we are not doing anything currently with it
553 * but noticing it for later.
556 zlog_warn("%s: Received %d extended Ack",
557 __PRETTY_FUNCTION__
, err_nlh
->nlmsg_type
);
561 if (msg
&& *msg
!= '\0') {
562 bool is_err
= !!err
->error
;
565 zlog_err("Extended Error: %s", msg
);
567 zlog_warn("Extended Warning: %s", msg
);
574 * Receive message from netlink interface and pass those information
575 * to the given function.
577 * filter -> Function to call to read the results
578 * nl -> netlink socket information
579 * zns -> The zebra namespace data
580 * count -> How many we should read in, 0 means as much as possible
581 * startup -> Are we reading in under startup conditions? passed to
584 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
585 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
594 char buf
[NL_RCV_PKT_BUF_SIZE
];
595 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
596 struct sockaddr_nl snl
;
597 struct msghdr msg
= {.msg_name
= (void *)&snl
,
598 .msg_namelen
= sizeof snl
,
603 if (count
&& read_in
>= count
)
606 status
= recvmsg(nl
->sock
, &msg
, 0);
610 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
612 zlog_err("%s recvmsg overrun: %s", nl
->name
,
613 safe_strerror(errno
));
615 * In this case we are screwed.
616 * There is no good way to
617 * recover zebra at this point.
624 zlog_err("%s EOF", nl
->name
);
628 if (msg
.msg_namelen
!= sizeof snl
) {
629 zlog_err("%s sender address length error: length %d",
630 nl
->name
, msg
.msg_namelen
);
634 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
635 zlog_debug("%s: << netlink message dump [recv]",
637 zlog_hexdump(buf
, status
);
641 for (h
= (struct nlmsghdr
*)buf
;
642 NLMSG_OK(h
, (unsigned int)status
);
643 h
= NLMSG_NEXT(h
, status
)) {
644 /* Finish of reading. */
645 if (h
->nlmsg_type
== NLMSG_DONE
)
648 /* Error handling. */
649 if (h
->nlmsg_type
== NLMSG_ERROR
) {
650 struct nlmsgerr
*err
=
651 (struct nlmsgerr
*)NLMSG_DATA(h
);
652 int errnum
= err
->error
;
653 int msg_type
= err
->msg
.nlmsg_type
;
656 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
657 zlog_err("%s error: message truncated",
663 * Parse the extended information before
664 * we actually handle it.
665 * At this point in time we do not
666 * do anything other than report the
669 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
670 netlink_parse_extended_ack(h
);
672 /* If the error field is zero, then this is an
674 if (err
->error
== 0) {
675 if (IS_ZEBRA_DEBUG_KERNEL
) {
677 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
678 __FUNCTION__
, nl
->name
,
680 err
->msg
.nlmsg_type
),
686 /* return if not a multipart message,
687 * otherwise continue */
688 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
693 /* Deal with errors that occur because of races
694 * in link handling */
695 if (nl
== &zns
->netlink_cmd
696 && ((msg_type
== RTM_DELROUTE
697 && (-errnum
== ENODEV
698 || -errnum
== ESRCH
))
699 || (msg_type
== RTM_NEWROUTE
700 && (-errnum
== ENETDOWN
701 || -errnum
== EEXIST
)))) {
702 if (IS_ZEBRA_DEBUG_KERNEL
)
704 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
706 safe_strerror(-errnum
),
715 /* We see RTM_DELNEIGH when shutting down an
716 * interface with an IPv4
717 * link-local. The kernel should have already
718 * deleted the neighbor
719 * so do not log these as an error.
721 if (msg_type
== RTM_DELNEIGH
722 || (nl
== &zns
->netlink_cmd
723 && msg_type
== RTM_NEWROUTE
725 || -errnum
== ENETUNREACH
))) {
726 /* This is known to happen in some
727 * situations, don't log
730 if (IS_ZEBRA_DEBUG_KERNEL
)
732 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
734 safe_strerror(-errnum
),
742 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
744 safe_strerror(-errnum
),
745 nl_msg_type_to_str(msg_type
),
746 msg_type
, err
->msg
.nlmsg_seq
,
752 /* OK we got netlink message. */
753 if (IS_ZEBRA_DEBUG_KERNEL
)
755 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
757 nl_msg_type_to_str(h
->nlmsg_type
),
758 h
->nlmsg_type
, h
->nlmsg_len
,
759 h
->nlmsg_seq
, h
->nlmsg_pid
);
763 * Ignore messages that maybe sent from
764 * other actors besides the kernel
766 if (snl
.nl_pid
!= 0) {
767 zlog_err("Ignoring message from pid %u",
772 error
= (*filter
)(h
, zns
->ns_id
, startup
);
774 zlog_err("%s filter function error", nl
->name
);
775 zlog_backtrace(LOG_ERR
);
780 /* After error care. */
781 if (msg
.msg_flags
& MSG_TRUNC
) {
782 zlog_err("%s error: message truncated", nl
->name
);
786 zlog_err("%s error: data remnant size %d", nl
->name
,
797 * sendmsg() to netlink socket then recvmsg().
798 * Calls netlink_parse_info to parse returned data
800 * filter -> The filter to read final results from kernel
801 * nlmsghdr -> The data to send to the kernel
802 * nl -> The netlink socket information
803 * zns -> The zebra namespace information
804 * startup -> Are we reading in under startup conditions
805 * This is passed through eventually to filter.
807 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
808 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
812 struct sockaddr_nl snl
;
817 memset(&snl
, 0, sizeof snl
);
818 memset(&iov
, 0, sizeof iov
);
819 memset(&msg
, 0, sizeof msg
);
822 iov
.iov_len
= n
->nlmsg_len
;
823 msg
.msg_name
= (void *)&snl
;
824 msg
.msg_namelen
= sizeof snl
;
828 snl
.nl_family
= AF_NETLINK
;
830 n
->nlmsg_seq
= ++nl
->seq
;
831 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
833 /* Request an acknowledgement by setting NLM_F_ACK */
834 n
->nlmsg_flags
|= NLM_F_ACK
;
836 if (IS_ZEBRA_DEBUG_KERNEL
)
838 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
839 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
840 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
843 /* Send message to netlink interface. */
844 if (zserv_privs
.change(ZPRIVS_RAISE
))
845 zlog_err("Can't raise privileges");
846 status
= sendmsg(nl
->sock
, &msg
, 0);
848 if (zserv_privs
.change(ZPRIVS_LOWER
))
849 zlog_err("Can't lower privileges");
851 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
852 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
853 zlog_hexdump(n
, n
->nlmsg_len
);
857 zlog_err("netlink_talk sendmsg() error: %s",
858 safe_strerror(save_errno
));
864 * Get reply from netlink socket.
865 * The reply should either be an acknowlegement or an error.
867 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
870 /* Issue request message to kernel via netlink socket. GET messages
871 * are issued through this interface.
873 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
876 struct sockaddr_nl snl
;
879 /* Check netlink socket. */
881 zlog_err("%s socket isn't active.", nl
->name
);
885 /* Fill common fields for all requests. */
886 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
887 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
888 n
->nlmsg_seq
= ++nl
->seq
;
890 memset(&snl
, 0, sizeof snl
);
891 snl
.nl_family
= AF_NETLINK
;
893 /* Raise capabilities and send message, then lower capabilities. */
894 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
895 zlog_err("Can't raise privileges");
899 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
900 (struct sockaddr
*)&snl
, sizeof snl
);
903 if (zserv_privs
.change(ZPRIVS_LOWER
))
904 zlog_err("Can't lower privileges");
907 zlog_err("%s sendto failed: %s", nl
->name
,
908 safe_strerror(save_errno
));
915 /* Exported interface function. This function simply calls
916 netlink_socket (). */
917 void kernel_init(struct zebra_ns
*zns
)
919 unsigned long groups
;
920 #if defined SOL_NETLINK
925 * Initialize netlink sockets
927 * If RTMGRP_XXX exists use that, but at some point
928 * I think the kernel developers realized that
929 * keeping track of all the different values would
930 * lead to confusion, so we need to convert the
931 * RTNLGRP_XXX to a bit position for ourself
933 groups
= RTMGRP_LINK
|
940 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
941 (1 << (RTNLGRP_IPV6_RULE
- 1));
943 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
944 "netlink-listen (NS %u)", zns
->ns_id
);
945 zns
->netlink
.sock
= -1;
946 netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
);
948 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
949 "netlink-cmd (NS %u)", zns
->ns_id
);
950 zns
->netlink_cmd
.sock
= -1;
951 netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
);
954 * SOL_NETLINK is not available on all platforms yet
955 * apparently. It's in bits/socket.h which I am not
956 * sure that we want to pull into our build system.
958 #if defined SOL_NETLINK
960 * Let's tell the kernel that we want to receive extended
961 * ACKS over our command socket
964 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
968 zlog_notice("Registration for extended ACK failed : %d %s",
969 errno
, safe_strerror(errno
));
972 /* Register kernel socket. */
973 if (zns
->netlink
.sock
> 0) {
974 /* Only want non-blocking on the netlink event socket */
975 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
976 zlog_err("Can't set %s socket flags: %s",
977 zns
->netlink
.name
, safe_strerror(errno
));
979 /* Set receive buffer size if it's set from command line */
981 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
983 netlink_install_filter(zns
->netlink
.sock
,
984 zns
->netlink_cmd
.snl
.nl_pid
);
985 zns
->t_netlink
= NULL
;
987 thread_add_read(zebrad
.master
, kernel_read
, zns
,
988 zns
->netlink
.sock
, &zns
->t_netlink
);
994 void kernel_terminate(struct zebra_ns
*zns
)
996 THREAD_READ_OFF(zns
->t_netlink
);
998 if (zns
->netlink
.sock
>= 0) {
999 close(zns
->netlink
.sock
);
1000 zns
->netlink
.sock
= -1;
1003 if (zns
->netlink_cmd
.sock
>= 0) {
1004 close(zns
->netlink_cmd
.sock
);
1005 zns
->netlink_cmd
.sock
= -1;
1009 #endif /* HAVE_NETLINK */