]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_netlink.c
zebra: do not hide distance and metric for kernel routes
[mirror_frr.git] / zebra / kernel_netlink.c
CommitLineData
718e3744 1/* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
896014f4
DL
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
718e3744 19 */
1fdc9eae 20
21#include <zebra.h>
22
ddfeb486
DL
23#ifdef HAVE_NETLINK
24
1fdc9eae 25#include "linklist.h"
26#include "if.h"
27#include "log.h"
28#include "prefix.h"
29#include "connected.h"
30#include "table.h"
31#include "memory.h"
32#include "zebra_memory.h"
33#include "rib.h"
34#include "thread.h"
35#include "privs.h"
36#include "nexthop.h"
37#include "vrf.h"
38#include "mpls.h"
39
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"
47
48#ifndef SO_RCVBUFFORCE
49#define SO_RCVBUFFORCE (33)
50#endif
51
52/* Hack for GNU libc version 2. */
53#ifndef MSG_TRUNC
54#define MSG_TRUNC 0x20
55#endif /* MSG_TRUNC */
56
57#ifndef NLMSG_TAIL
d62a17ae 58#define NLMSG_TAIL(nmsg) \
59 ((struct rtattr *)(((u_char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
1fdc9eae 60#endif
61
62#ifndef RTA_TAIL
d62a17ae 63#define RTA_TAIL(rta) \
64 ((struct rtattr *)(((u_char *)(rta)) + RTA_ALIGN((rta)->rta_len)))
1fdc9eae 65#endif
66
f909c673
DS
67#ifndef RTNL_FAMILY_IP6MR
68#define RTNL_FAMILY_IP6MR 129
69#endif
70
71#ifndef RTPROT_MROUTED
72#define RTPROT_MROUTED 17
73#endif
74
d62a17ae 75static 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"},
87 {0}};
1fdc9eae 88
89static const struct message rtproto_str[] = {
d62a17ae 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"},
96 {RTPROT_MRT, "MRT"},
97 {RTPROT_ZEBRA, "Zebra"},
1fdc9eae 98#ifdef RTPROT_BIRD
d62a17ae 99 {RTPROT_BIRD, "BIRD"},
1fdc9eae 100#endif /* RTPROT_BIRD */
d62a17ae 101 {RTPROT_MROUTED, "mroute"},
102 {RTPROT_BGP, "BGP"},
103 {RTPROT_OSPF, "OSPF"},
104 {RTPROT_ISIS, "IS-IS"},
105 {RTPROT_RIP, "RIP"},
106 {RTPROT_RIPNG, "RIPNG"},
107 {0}};
108
109static const struct message family_str[] = {{AF_INET, "ipv4"},
110 {AF_INET6, "ipv6"},
111 {AF_BRIDGE, "bridge"},
112 {RTNL_FAMILY_IPMR, "ipv4MR"},
113 {RTNL_FAMILY_IP6MR, "ipv6MR"},
114 {0}};
115
116static const struct message rttype_str[] = {{RTN_UNICAST, "unicast"},
117 {RTN_MULTICAST, "multicast"},
118 {0}};
b339bde7 119
1fdc9eae 120extern struct thread_master *master;
121extern u_int32_t nl_rcvbufsize;
122
123extern struct zebra_privs_t zserv_privs;
124
d62a17ae 125int netlink_talk_filter(struct sockaddr_nl *snl, struct nlmsghdr *h,
126 ns_id_t ns_id, int startup)
1fdc9eae 127{
d62a17ae 128 zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u",
129 h->nlmsg_type, ns_id);
130 return 0;
1fdc9eae 131}
132
d62a17ae 133static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
1fdc9eae 134{
d62a17ae 135 u_int32_t oldsize;
136 socklen_t newlen = sizeof(newsize);
137 socklen_t oldlen = sizeof(oldsize);
138 int ret;
139
140 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
141 if (ret < 0) {
142 zlog_err("Can't get %s receive buffer size: %s", nl->name,
143 safe_strerror(errno));
144 return -1;
145 }
146
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");
154 if (ret < 0)
155 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF,
156 &nl_rcvbufsize, sizeof(nl_rcvbufsize));
157 if (ret < 0) {
158 zlog_err("Can't set %s receive buffer size: %s", nl->name,
159 safe_strerror(errno));
160 return -1;
161 }
162
163 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
164 if (ret < 0) {
165 zlog_err("Can't get %s receive buffer size: %s", nl->name,
166 safe_strerror(errno));
167 return -1;
168 }
169
170 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
171 oldsize, newsize);
172 return 0;
1fdc9eae 173}
174
175/* Make socket for Linux netlink interface. */
d62a17ae 176static int netlink_socket(struct nlsock *nl, unsigned long groups,
177 ns_id_t ns_id)
1fdc9eae 178{
d62a17ae 179 int ret;
180 struct sockaddr_nl snl;
181 int sock;
182 int namelen;
183 int save_errno;
184
185 if (zserv_privs.change(ZPRIVS_RAISE)) {
186 zlog_err("Can't raise privileges");
187 return -1;
188 }
189
190 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
191 if (sock < 0) {
192 zlog_err("Can't open %s socket: %s", nl->name,
193 safe_strerror(errno));
194 return -1;
195 }
196
197 memset(&snl, 0, sizeof snl);
198 snl.nl_family = AF_NETLINK;
199 snl.nl_groups = groups;
200
201 /* Bind the socket to the netlink structure for anything. */
202 ret = bind(sock, (struct sockaddr *)&snl, sizeof snl);
203 save_errno = errno;
204 if (zserv_privs.change(ZPRIVS_LOWER))
205 zlog_err("Can't lower privileges");
206
207 if (ret < 0) {
208 zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name,
209 snl.nl_groups, safe_strerror(save_errno));
210 close(sock);
211 return -1;
212 }
213
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));
220 close(sock);
221 return -1;
222 }
223
224 nl->snl = snl;
225 nl->sock = sock;
226 return ret;
1fdc9eae 227}
228
d62a17ae 229static int netlink_information_fetch(struct sockaddr_nl *snl,
230 struct nlmsghdr *h, ns_id_t ns_id,
231 int startup)
1fdc9eae 232{
d62a17ae 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);
236 return 0;
237 }
238
239 switch (h->nlmsg_type) {
240 case RTM_NEWROUTE:
241 return netlink_route_change(snl, h, ns_id, startup);
242 break;
243 case RTM_DELROUTE:
244 return netlink_route_change(snl, h, ns_id, startup);
245 break;
246 case RTM_NEWLINK:
247 return netlink_link_change(snl, h, ns_id, startup);
248 break;
249 case RTM_DELLINK:
250 return netlink_link_change(snl, h, ns_id, startup);
251 break;
252 case RTM_NEWADDR:
253 return netlink_interface_addr(snl, h, ns_id, startup);
254 break;
255 case RTM_DELADDR:
256 return netlink_interface_addr(snl, h, ns_id, startup);
257 break;
258 case RTM_NEWNEIGH:
259 return netlink_neigh_change(snl, h, ns_id);
260 break;
261 case RTM_DELNEIGH:
262 return netlink_neigh_change(snl, h, ns_id);
263 break;
264 default:
265 zlog_warn("Unknown netlink nlmsg_type %d vrf %u\n",
266 h->nlmsg_type, ns_id);
267 break;
268 }
269 return 0;
1fdc9eae 270}
271
d62a17ae 272static int kernel_read(struct thread *thread)
1fdc9eae 273{
d62a17ae 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,
278 &zns->t_netlink);
1fdc9eae 279
d62a17ae 280 return 0;
1fdc9eae 281}
282
283/* Filter out messages from self that occur on listener socket,
284 * caused by our actions on the command socket
285 */
d62a17ae 286static void netlink_install_filter(int sock, __u32 pid)
1fdc9eae 287{
d62a17ae 288 struct sock_filter filter[] = {
289 /* 0: ldh [4] */
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),
300 /* 5: ldw [12] */
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),
309 };
310
311 struct sock_fprog prog = {
9d303b37 312 .len = array_size(filter), .filter = filter,
d62a17ae 313 };
314
315 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))
316 < 0)
317 zlog_warn("Can't install socket filter: %s\n",
318 safe_strerror(errno));
1fdc9eae 319}
320
d62a17ae 321void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
322 int len)
1fdc9eae 323{
d62a17ae 324 while (RTA_OK(rta, len)) {
325 if (rta->rta_type <= max)
326 tb[rta->rta_type] = rta;
327 rta = RTA_NEXT(rta, len);
328 }
1fdc9eae 329}
330
d62a17ae 331int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type, void *data,
332 unsigned int alen)
1fdc9eae 333{
d62a17ae 334 int len;
335 struct rtattr *rta;
1fdc9eae 336
d62a17ae 337 len = RTA_LENGTH(alen);
1fdc9eae 338
d62a17ae 339 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
340 return -1;
1fdc9eae 341
d62a17ae 342 rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
343 rta->rta_type = type;
344 rta->rta_len = len;
4b2792b5 345
d62a17ae 346 if (data)
347 memcpy(RTA_DATA(rta), data, alen);
348 else
349 assert(alen == 0);
4b2792b5 350
d62a17ae 351 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1fdc9eae 352
d62a17ae 353 return 0;
1fdc9eae 354}
355
d62a17ae 356int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type, void *data,
357 unsigned int alen)
1fdc9eae 358{
d62a17ae 359 unsigned int len;
360 struct rtattr *subrta;
1fdc9eae 361
d62a17ae 362 len = RTA_LENGTH(alen);
1fdc9eae 363
d62a17ae 364 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen)
365 return -1;
1fdc9eae 366
d62a17ae 367 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
368 subrta->rta_type = type;
369 subrta->rta_len = len;
4b2792b5 370
d62a17ae 371 if (data)
372 memcpy(RTA_DATA(subrta), data, alen);
373 else
374 assert(alen == 0);
4b2792b5 375
d62a17ae 376 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1fdc9eae 377
d62a17ae 378 return 0;
1fdc9eae 379}
380
d62a17ae 381int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, u_int16_t data)
bbc16902 382{
d62a17ae 383 return addattr_l(n, maxlen, type, &data, sizeof(u_int16_t));
bbc16902 384}
385
d62a17ae 386int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data)
1fdc9eae 387{
d62a17ae 388 return addattr_l(n, maxlen, type, &data, sizeof(u_int32_t));
1fdc9eae 389}
390
d62a17ae 391struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1fdc9eae 392{
d62a17ae 393 struct rtattr *nest = NLMSG_TAIL(n);
1fdc9eae 394
d62a17ae 395 addattr_l(n, maxlen, type, NULL, 0);
396 return nest;
1fdc9eae 397}
398
d62a17ae 399int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1fdc9eae 400{
d62a17ae 401 nest->rta_len = (u_char *)NLMSG_TAIL(n) - (u_char *)nest;
402 return n->nlmsg_len;
1fdc9eae 403}
404
d62a17ae 405struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1fdc9eae 406{
d62a17ae 407 struct rtattr *nest = RTA_TAIL(rta);
1fdc9eae 408
d62a17ae 409 rta_addattr_l(rta, maxlen, type, NULL, 0);
410 return nest;
1fdc9eae 411}
412
d62a17ae 413int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1fdc9eae 414{
d62a17ae 415 nest->rta_len = (u_char *)RTA_TAIL(rta) - (u_char *)nest;
416 return rta->rta_len;
1fdc9eae 417}
418
d62a17ae 419const char *nl_msg_type_to_str(uint16_t msg_type)
1fdc9eae 420{
d62a17ae 421 return lookup_msg(nlmsg_str, msg_type, "");
1fdc9eae 422}
423
d62a17ae 424const char *nl_rtproto_to_str(u_char rtproto)
1fdc9eae 425{
d62a17ae 426 return lookup_msg(rtproto_str, rtproto, "");
1fdc9eae 427}
b339bde7 428
d62a17ae 429const char *nl_family_to_str(u_char family)
b339bde7 430{
d62a17ae 431 return lookup_msg(family_str, family, "");
b339bde7
DS
432}
433
d62a17ae 434const char *nl_rttype_to_str(u_char rttype)
b339bde7 435{
d62a17ae 436 return lookup_msg(rttype_str, rttype, "");
b339bde7
DS
437}
438
936ebf0a
DS
439/*
440 * netlink_parse_info
441 *
442 * Receive message from netlink interface and pass those information
443 * to the given function.
444 *
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
450 * the filter.
451 */
d62a17ae 452int netlink_parse_info(int (*filter)(struct sockaddr_nl *, struct nlmsghdr *,
453 ns_id_t, int),
454 struct nlsock *nl, struct zebra_ns *zns, int count,
455 int startup)
1fdc9eae 456{
d62a17ae 457 int status;
458 int ret = 0;
459 int error;
460 int read_in = 0;
461
462 while (1) {
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,
468 .msg_iov = &iov,
469 .msg_iovlen = 1};
470 struct nlmsghdr *h;
471
472 if (count && read_in >= count)
473 return 0;
474
475 status = recvmsg(nl->sock, &msg, 0);
476 if (status < 0) {
477 if (errno == EINTR)
478 continue;
479 if (errno == EWOULDBLOCK || errno == EAGAIN)
480 break;
481 zlog_err("%s recvmsg overrun: %s", nl->name,
482 safe_strerror(errno));
483 /*
484 * In this case we are screwed.
485 * There is no good way to
486 * recover zebra at this point.
487 */
488 exit(-1);
489 continue;
1fdc9eae 490 }
491
d62a17ae 492 if (status == 0) {
493 zlog_err("%s EOF", nl->name);
494 return -1;
495 }
496
497 if (msg.msg_namelen != sizeof snl) {
498 zlog_err("%s sender address length error: length %d",
499 nl->name, msg.msg_namelen);
500 return -1;
501 }
502
503 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
504 zlog_debug("%s: << netlink message dump [recv]",
505 __func__);
506 zlog_hexdump(buf, status);
507 }
508
509 read_in++;
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)
515 return ret;
516
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;
523
524 /* If the error field is zero, then this is an
525 * ACK */
526 if (err->error == 0) {
527 if (IS_ZEBRA_DEBUG_KERNEL) {
528 zlog_debug(
529 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
530 __FUNCTION__, nl->name,
531 nl_msg_type_to_str(
532 err->msg.nlmsg_type),
533 err->msg.nlmsg_type,
534 err->msg.nlmsg_seq,
535 err->msg.nlmsg_pid);
536 }
537
538 /* return if not a multipart message,
539 * otherwise continue */
540 if (!(h->nlmsg_flags & NLM_F_MULTI))
541 return 0;
542 continue;
543 }
544
545 if (h->nlmsg_len
546 < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
547 zlog_err("%s error: message truncated",
548 nl->name);
549 return -1;
550 }
551
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)
562 zlog_debug(
563 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
564 nl->name,
565 safe_strerror(-errnum),
566 nl_msg_type_to_str(
567 msg_type),
568 msg_type,
569 err->msg.nlmsg_seq,
570 err->msg.nlmsg_pid);
571 return 0;
572 }
573
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.
579 */
580 if (msg_type == RTM_DELNEIGH
581 || (nl == &zns->netlink_cmd
582 && msg_type == RTM_NEWROUTE
583 && (-errnum == ESRCH
584 || -errnum == ENETUNREACH))) {
585 /* This is known to happen in some
586 * situations, don't log
587 * as error.
588 */
589 if (IS_ZEBRA_DEBUG_KERNEL)
590 zlog_debug(
591 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
592 nl->name,
593 safe_strerror(-errnum),
594 nl_msg_type_to_str(
595 msg_type),
596 msg_type,
597 err->msg.nlmsg_seq,
598 err->msg.nlmsg_pid);
599 } else
600 zlog_err(
601 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
602 nl->name,
603 safe_strerror(-errnum),
604 nl_msg_type_to_str(msg_type),
605 msg_type, err->msg.nlmsg_seq,
606 err->msg.nlmsg_pid);
607
608 return -1;
609 }
610
611 /* OK we got netlink message. */
612 if (IS_ZEBRA_DEBUG_KERNEL)
613 zlog_debug(
614 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
615 nl->name,
616 nl_msg_type_to_str(h->nlmsg_type),
617 h->nlmsg_type, h->nlmsg_len,
618 h->nlmsg_seq, h->nlmsg_pid);
619
620 /* skip unsolicited messages originating from command
621 * socket
622 * linux sets the originators port-id for {NEW|DEL}ADDR
623 * messages,
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)
630 zlog_debug(
631 "netlink_parse_info: %s packet comes from %s",
632 zns->netlink_cmd.name,
633 nl->name);
634 continue;
635 }
636
637 error = (*filter)(&snl, h, zns->ns_id, startup);
638 if (error < 0) {
639 zlog_err("%s filter function error", nl->name);
640 ret = error;
641 }
642 }
643
644 /* After error care. */
645 if (msg.msg_flags & MSG_TRUNC) {
646 zlog_err("%s error: message truncated", nl->name);
647 continue;
648 }
649 if (status) {
650 zlog_err("%s error: data remnant size %d", nl->name,
651 status);
652 return -1;
653 }
654 }
655 return ret;
1fdc9eae 656}
657
936ebf0a
DS
658/*
659 * netlink_talk
660 *
661 * sendmsg() to netlink socket then recvmsg().
662 * Calls netlink_parse_info to parse returned data
663 *
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.
670 */
d62a17ae 671int netlink_talk(int (*filter)(struct sockaddr_nl *, struct nlmsghdr *, ns_id_t,
672 int startup),
673 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
674 int startup)
1fdc9eae 675{
d62a17ae 676 int status;
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,
682 .msg_iov = &iov,
683 .msg_iovlen = 1,
684 };
685 int save_errno;
686
687 memset(&snl, 0, sizeof snl);
688 snl.nl_family = AF_NETLINK;
689
690 n->nlmsg_seq = ++nl->seq;
691 n->nlmsg_pid = nl->snl.nl_pid;
692
693 /* Request an acknowledgement by setting NLM_F_ACK */
694 n->nlmsg_flags |= NLM_F_ACK;
695
696 if (IS_ZEBRA_DEBUG_KERNEL)
697 zlog_debug(
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,
701 n->nlmsg_flags);
702
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);
707 save_errno = errno;
708 if (zserv_privs.change(ZPRIVS_LOWER))
709 zlog_err("Can't lower privileges");
710
711 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
712 zlog_debug("%s: >> netlink message dump [sent]", __func__);
713 zlog_hexdump(n, n->nlmsg_len);
714 }
715
716 if (status < 0) {
717 zlog_err("netlink_talk sendmsg() error: %s",
718 safe_strerror(save_errno));
719 return -1;
720 }
721
722
723 /*
724 * Get reply from netlink socket.
725 * The reply should either be an acknowlegement or an error.
726 */
727 return netlink_parse_info(filter, nl, zns, 0, startup);
1fdc9eae 728}
729
289602d7 730/* Issue request message to kernel via netlink socket. GET messages
731 * are issued through this interface.
732 */
d62a17ae 733int netlink_request(struct nlsock *nl, struct nlmsghdr *n)
1fdc9eae 734{
d62a17ae 735 int ret;
736 struct sockaddr_nl snl;
737 int save_errno;
738
739 /* Check netlink socket. */
740 if (nl->sock < 0) {
741 zlog_err("%s socket isn't active.", nl->name);
742 return -1;
743 }
744
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;
749
750 memset(&snl, 0, sizeof snl);
751 snl.nl_family = AF_NETLINK;
752
753 /* Raise capabilities and send message, then lower capabilities. */
754 if (zserv_privs.change(ZPRIVS_RAISE)) {
755 zlog_err("Can't raise privileges");
756 return -1;
757 }
758
759 ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0,
760 (struct sockaddr *)&snl, sizeof snl);
761 save_errno = errno;
762
763 if (zserv_privs.change(ZPRIVS_LOWER))
764 zlog_err("Can't lower privileges");
765
766 if (ret < 0) {
767 zlog_err("%s sendto failed: %s", nl->name,
768 safe_strerror(save_errno));
769 return -1;
770 }
771
772 return 0;
1fdc9eae 773}
774
775/* Exported interface function. This function simply calls
776 netlink_socket (). */
d62a17ae 777void kernel_init(struct zebra_ns *zns)
1fdc9eae 778{
d62a17ae 779 unsigned long groups;
780
781 /* Initialize netlink sockets */
782 groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR
783 | RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_MROUTE
784 | RTMGRP_NEIGH;
785
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);
790
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);
795
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));
802
803 /* Set receive buffer size if it's set from command line */
804 if (nl_rcvbufsize)
805 netlink_recvbuf(&zns->netlink, nl_rcvbufsize);
806
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);
812 }
813
814 rt_netlink_init();
1fdc9eae 815}
816
d62a17ae 817void kernel_terminate(struct zebra_ns *zns)
1fdc9eae 818{
d62a17ae 819 THREAD_READ_OFF(zns->t_netlink);
820
821 if (zns->netlink.sock >= 0) {
822 close(zns->netlink.sock);
823 zns->netlink.sock = -1;
824 }
825
826 if (zns->netlink_cmd.sock >= 0) {
827 close(zns->netlink_cmd.sock);
828 zns->netlink_cmd.sock = -1;
829 }
1fdc9eae 830}
ddfeb486
DL
831
832#endif /* HAVE_NETLINK */