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