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