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
,
394 const void *data
, unsigned int alen
)
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
,
419 const void *data
, unsigned int alen
)
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");
548 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
550 * Header of failed message
551 * we are not doing anything currently with it
552 * but noticing it for later.
555 zlog_warn("%s: Received %d extended Ack",
556 __PRETTY_FUNCTION__
, err_nlh
->nlmsg_type
);
560 if (msg
&& *msg
!= '\0') {
561 bool is_err
= !!err
->error
;
564 zlog_err("Extended Error: %s", msg
);
566 zlog_warn("Extended Warning: %s", msg
);
573 * Receive message from netlink interface and pass those information
574 * to the given function.
576 * filter -> Function to call to read the results
577 * nl -> netlink socket information
578 * zns -> The zebra namespace data
579 * count -> How many we should read in, 0 means as much as possible
580 * startup -> Are we reading in under startup conditions? passed to
583 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
584 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
,
593 char buf
[NL_RCV_PKT_BUF_SIZE
];
594 struct iovec iov
= {.iov_base
= buf
, .iov_len
= sizeof buf
};
595 struct sockaddr_nl snl
;
596 struct msghdr msg
= {.msg_name
= (void *)&snl
,
597 .msg_namelen
= sizeof snl
,
602 if (count
&& read_in
>= count
)
605 status
= recvmsg(nl
->sock
, &msg
, 0);
609 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
611 zlog_err("%s recvmsg overrun: %s", nl
->name
,
612 safe_strerror(errno
));
614 * In this case we are screwed.
615 * There is no good way to
616 * recover zebra at this point.
623 zlog_err("%s EOF", nl
->name
);
627 if (msg
.msg_namelen
!= sizeof snl
) {
628 zlog_err("%s sender address length error: length %d",
629 nl
->name
, msg
.msg_namelen
);
633 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
634 zlog_debug("%s: << netlink message dump [recv]",
636 zlog_hexdump(buf
, status
);
640 for (h
= (struct nlmsghdr
*)buf
;
641 NLMSG_OK(h
, (unsigned int)status
);
642 h
= NLMSG_NEXT(h
, status
)) {
643 /* Finish of reading. */
644 if (h
->nlmsg_type
== NLMSG_DONE
)
647 /* Error handling. */
648 if (h
->nlmsg_type
== NLMSG_ERROR
) {
649 struct nlmsgerr
*err
=
650 (struct nlmsgerr
*)NLMSG_DATA(h
);
651 int errnum
= err
->error
;
652 int msg_type
= err
->msg
.nlmsg_type
;
655 < NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
656 zlog_err("%s error: message truncated",
662 * Parse the extended information before
663 * we actually handle it.
664 * At this point in time we do not
665 * do anything other than report the
668 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
669 netlink_parse_extended_ack(h
);
671 /* If the error field is zero, then this is an
673 if (err
->error
== 0) {
674 if (IS_ZEBRA_DEBUG_KERNEL
) {
676 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
677 __FUNCTION__
, nl
->name
,
679 err
->msg
.nlmsg_type
),
685 /* return if not a multipart message,
686 * otherwise continue */
687 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
692 /* Deal with errors that occur because of races
693 * in link handling */
694 if (nl
== &zns
->netlink_cmd
695 && ((msg_type
== RTM_DELROUTE
696 && (-errnum
== ENODEV
697 || -errnum
== ESRCH
))
698 || (msg_type
== RTM_NEWROUTE
699 && (-errnum
== ENETDOWN
700 || -errnum
== EEXIST
)))) {
701 if (IS_ZEBRA_DEBUG_KERNEL
)
703 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
705 safe_strerror(-errnum
),
714 /* We see RTM_DELNEIGH when shutting down an
715 * interface with an IPv4
716 * link-local. The kernel should have already
717 * deleted the neighbor
718 * so do not log these as an error.
720 if (msg_type
== RTM_DELNEIGH
721 || (nl
== &zns
->netlink_cmd
722 && msg_type
== RTM_NEWROUTE
724 || -errnum
== ENETUNREACH
))) {
725 /* This is known to happen in some
726 * situations, don't log
729 if (IS_ZEBRA_DEBUG_KERNEL
)
731 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
733 safe_strerror(-errnum
),
741 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
743 safe_strerror(-errnum
),
744 nl_msg_type_to_str(msg_type
),
745 msg_type
, err
->msg
.nlmsg_seq
,
751 /* OK we got netlink message. */
752 if (IS_ZEBRA_DEBUG_KERNEL
)
754 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
756 nl_msg_type_to_str(h
->nlmsg_type
),
757 h
->nlmsg_type
, h
->nlmsg_len
,
758 h
->nlmsg_seq
, h
->nlmsg_pid
);
762 * Ignore messages that maybe sent from
763 * other actors besides the kernel
765 if (snl
.nl_pid
!= 0) {
766 zlog_err("Ignoring message from pid %u",
771 error
= (*filter
)(h
, zns
->ns_id
, startup
);
773 zlog_err("%s filter function error", nl
->name
);
774 zlog_backtrace(LOG_ERR
);
779 /* After error care. */
780 if (msg
.msg_flags
& MSG_TRUNC
) {
781 zlog_err("%s error: message truncated", nl
->name
);
785 zlog_err("%s error: data remnant size %d", nl
->name
,
796 * sendmsg() to netlink socket then recvmsg().
797 * Calls netlink_parse_info to parse returned data
799 * filter -> The filter to read final results from kernel
800 * nlmsghdr -> The data to send to the kernel
801 * nl -> The netlink socket information
802 * zns -> The zebra namespace information
803 * startup -> Are we reading in under startup conditions
804 * This is passed through eventually to filter.
806 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
807 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
811 struct sockaddr_nl snl
;
816 memset(&snl
, 0, sizeof snl
);
817 memset(&iov
, 0, sizeof iov
);
818 memset(&msg
, 0, sizeof msg
);
821 iov
.iov_len
= n
->nlmsg_len
;
822 msg
.msg_name
= (void *)&snl
;
823 msg
.msg_namelen
= sizeof snl
;
827 snl
.nl_family
= AF_NETLINK
;
829 n
->nlmsg_seq
= ++nl
->seq
;
830 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
832 if (IS_ZEBRA_DEBUG_KERNEL
)
834 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
835 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
836 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
839 /* Send message to netlink interface. */
840 if (zserv_privs
.change(ZPRIVS_RAISE
))
841 zlog_err("Can't raise privileges");
842 status
= sendmsg(nl
->sock
, &msg
, 0);
844 if (zserv_privs
.change(ZPRIVS_LOWER
))
845 zlog_err("Can't lower privileges");
847 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
848 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
849 zlog_hexdump(n
, n
->nlmsg_len
);
853 zlog_err("netlink_talk sendmsg() error: %s",
854 safe_strerror(save_errno
));
860 * Get reply from netlink socket.
861 * The reply should either be an acknowlegement or an error.
863 return netlink_parse_info(filter
, nl
, zns
, 0, startup
);
866 /* Issue request message to kernel via netlink socket. GET messages
867 * are issued through this interface.
869 int netlink_request(struct nlsock
*nl
, struct nlmsghdr
*n
)
872 struct sockaddr_nl snl
;
875 /* Check netlink socket. */
877 zlog_err("%s socket isn't active.", nl
->name
);
881 /* Fill common fields for all requests. */
882 n
->nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
883 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
884 n
->nlmsg_seq
= ++nl
->seq
;
886 memset(&snl
, 0, sizeof snl
);
887 snl
.nl_family
= AF_NETLINK
;
889 /* Raise capabilities and send message, then lower capabilities. */
890 if (zserv_privs
.change(ZPRIVS_RAISE
)) {
891 zlog_err("Can't raise privileges");
895 ret
= sendto(nl
->sock
, (void *)n
, n
->nlmsg_len
, 0,
896 (struct sockaddr
*)&snl
, sizeof snl
);
899 if (zserv_privs
.change(ZPRIVS_LOWER
))
900 zlog_err("Can't lower privileges");
903 zlog_err("%s sendto failed: %s", nl
->name
,
904 safe_strerror(save_errno
));
911 /* Exported interface function. This function simply calls
912 netlink_socket (). */
913 void kernel_init(struct zebra_ns
*zns
)
915 unsigned long groups
;
916 #if defined SOL_NETLINK
921 * Initialize netlink sockets
923 * If RTMGRP_XXX exists use that, but at some point
924 * I think the kernel developers realized that
925 * keeping track of all the different values would
926 * lead to confusion, so we need to convert the
927 * RTNLGRP_XXX to a bit position for ourself
929 groups
= RTMGRP_LINK
|
936 (1 << (RTNLGRP_IPV4_RULE
- 1)) |
937 (1 << (RTNLGRP_IPV6_RULE
- 1));
939 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
940 "netlink-listen (NS %u)", zns
->ns_id
);
941 zns
->netlink
.sock
= -1;
942 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
943 zlog_err("Failure to create %s socket",
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 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
952 zlog_err("Failure to create %s socket",
953 zns
->netlink_cmd
.name
);
958 * SOL_NETLINK is not available on all platforms yet
959 * apparently. It's in bits/socket.h which I am not
960 * sure that we want to pull into our build system.
962 #if defined SOL_NETLINK
964 * Let's tell the kernel that we want to receive extended
965 * ACKS over our command socket
968 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
972 zlog_notice("Registration for extended ACK failed : %d %s",
973 errno
, safe_strerror(errno
));
976 /* Register kernel socket. */
977 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
978 zlog_err("Can't set %s socket error: %s(%d)",
979 zns
->netlink
.name
, safe_strerror(errno
), errno
);
981 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
982 zlog_err("Can't set %s socket error: %s(%d)",
983 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
985 /* Set receive buffer size if it's set from command line */
987 netlink_recvbuf(&zns
->netlink
, nl_rcvbufsize
);
989 netlink_install_filter(zns
->netlink
.sock
,
990 zns
->netlink_cmd
.snl
.nl_pid
);
991 zns
->t_netlink
= NULL
;
993 thread_add_read(zebrad
.master
, kernel_read
, zns
,
994 zns
->netlink
.sock
, &zns
->t_netlink
);
999 void kernel_terminate(struct zebra_ns
*zns
)
1001 THREAD_READ_OFF(zns
->t_netlink
);
1003 if (zns
->netlink
.sock
>= 0) {
1004 close(zns
->netlink
.sock
);
1005 zns
->netlink
.sock
= -1;
1008 if (zns
->netlink_cmd
.sock
>= 0) {
1009 close(zns
->netlink_cmd
.sock
);
1010 zns
->netlink_cmd
.sock
= -1;
1014 #endif /* HAVE_NETLINK */