]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_netlink.c
*: Convert to using frr_vtydir instead of DAEMON_VTY_DIR
[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
acfa8927 23#if defined(HANDLE_NETLINK_FUZZING)
81a2f870
SW
24#include <stdio.h>
25#include <string.h>
acfa8927 26#endif /* HANDLE_NETLINK_FUZZING */
81a2f870 27
ddfeb486
DL
28#ifdef HAVE_NETLINK
29
1fdc9eae 30#include "linklist.h"
31#include "if.h"
32#include "log.h"
33#include "prefix.h"
34#include "connected.h"
35#include "table.h"
36#include "memory.h"
37#include "zebra_memory.h"
38#include "rib.h"
39#include "thread.h"
40#include "privs.h"
41#include "nexthop.h"
42#include "vrf.h"
43#include "mpls.h"
174482ef 44#include "lib_errors.h"
1fdc9eae 45
3801e764
DS
46//#include "zebra/zserv.h"
47#include "zebra/zebra_router.h"
1fdc9eae 48#include "zebra/zebra_ns.h"
49#include "zebra/zebra_vrf.h"
05f7f5db 50#include "zebra/rt.h"
1fdc9eae 51#include "zebra/debug.h"
52#include "zebra/kernel_netlink.h"
53#include "zebra/rt_netlink.h"
54#include "zebra/if_netlink.h"
942bf97b 55#include "zebra/rule_netlink.h"
43e52561 56#include "zebra/zebra_errors.h"
1fdc9eae 57
58#ifndef SO_RCVBUFFORCE
59#define SO_RCVBUFFORCE (33)
60#endif
61
62/* Hack for GNU libc version 2. */
63#ifndef MSG_TRUNC
64#define MSG_TRUNC 0x20
65#endif /* MSG_TRUNC */
66
67#ifndef NLMSG_TAIL
d62a17ae 68#define NLMSG_TAIL(nmsg) \
d7c0a89a
QY
69 ((struct rtattr *)(((uint8_t *)(nmsg)) \
70 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
1fdc9eae 71#endif
72
73#ifndef RTA_TAIL
d62a17ae 74#define RTA_TAIL(rta) \
d7c0a89a 75 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
1fdc9eae 76#endif
77
f909c673
DS
78#ifndef RTNL_FAMILY_IP6MR
79#define RTNL_FAMILY_IP6MR 129
80#endif
81
82#ifndef RTPROT_MROUTED
83#define RTPROT_MROUTED 17
84#endif
85
d62a17ae 86static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"},
87 {RTM_DELROUTE, "RTM_DELROUTE"},
88 {RTM_GETROUTE, "RTM_GETROUTE"},
89 {RTM_NEWLINK, "RTM_NEWLINK"},
90 {RTM_DELLINK, "RTM_DELLINK"},
91 {RTM_GETLINK, "RTM_GETLINK"},
92 {RTM_NEWADDR, "RTM_NEWADDR"},
93 {RTM_DELADDR, "RTM_DELADDR"},
94 {RTM_GETADDR, "RTM_GETADDR"},
95 {RTM_NEWNEIGH, "RTM_NEWNEIGH"},
96 {RTM_DELNEIGH, "RTM_DELNEIGH"},
97 {RTM_GETNEIGH, "RTM_GETNEIGH"},
942bf97b 98 {RTM_NEWRULE, "RTM_NEWRULE"},
99 {RTM_DELRULE, "RTM_DELRULE"},
100 {RTM_GETRULE, "RTM_GETRULE"},
d62a17ae 101 {0}};
1fdc9eae 102
103static const struct message rtproto_str[] = {
d62a17ae 104 {RTPROT_REDIRECT, "redirect"},
105 {RTPROT_KERNEL, "kernel"},
106 {RTPROT_BOOT, "boot"},
107 {RTPROT_STATIC, "static"},
108 {RTPROT_GATED, "GateD"},
109 {RTPROT_RA, "router advertisement"},
110 {RTPROT_MRT, "MRT"},
111 {RTPROT_ZEBRA, "Zebra"},
1fdc9eae 112#ifdef RTPROT_BIRD
d62a17ae 113 {RTPROT_BIRD, "BIRD"},
1fdc9eae 114#endif /* RTPROT_BIRD */
d62a17ae 115 {RTPROT_MROUTED, "mroute"},
116 {RTPROT_BGP, "BGP"},
117 {RTPROT_OSPF, "OSPF"},
118 {RTPROT_ISIS, "IS-IS"},
119 {RTPROT_RIP, "RIP"},
120 {RTPROT_RIPNG, "RIPNG"},
d4d71f11 121 {RTPROT_ZSTATIC, "static"},
d62a17ae 122 {0}};
123
124static const struct message family_str[] = {{AF_INET, "ipv4"},
125 {AF_INET6, "ipv6"},
126 {AF_BRIDGE, "bridge"},
127 {RTNL_FAMILY_IPMR, "ipv4MR"},
128 {RTNL_FAMILY_IP6MR, "ipv6MR"},
129 {0}};
130
131static const struct message rttype_str[] = {{RTN_UNICAST, "unicast"},
132 {RTN_MULTICAST, "multicast"},
133 {0}};
b339bde7 134
1fdc9eae 135extern struct thread_master *master;
d7c0a89a 136extern uint32_t nl_rcvbufsize;
1fdc9eae 137
138extern struct zebra_privs_t zserv_privs;
139
7cdb1a84 140
2414abd3 141int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 142{
3575d9e8
DS
143 /*
144 * This is an error condition that must be handled during
145 * development.
146 *
147 * The netlink_talk_filter function is used for communication
148 * down the netlink_cmd pipe and we are expecting
149 * an ack being received. So if we get here
150 * then we did not receive the ack and instead
151 * received some other message in an unexpected
152 * way.
153 */
43e52561
QY
154 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__,
155 h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id);
d62a17ae 156 return 0;
1fdc9eae 157}
158
d62a17ae 159static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
1fdc9eae 160{
d7c0a89a 161 uint32_t oldsize;
d62a17ae 162 socklen_t newlen = sizeof(newsize);
163 socklen_t oldlen = sizeof(oldsize);
164 int ret;
165
166 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
167 if (ret < 0) {
450971aa 168 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
169 "Can't get %s receive buffer size: %s", nl->name,
170 safe_strerror(errno));
d62a17ae 171 return -1;
172 }
173
174 /* Try force option (linux >= 2.6.14) and fall back to normal set */
01b9e3fd
DL
175 frr_elevate_privs(&zserv_privs) {
176 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE,
177 &nl_rcvbufsize,
178 sizeof(nl_rcvbufsize));
179 }
d62a17ae 180 if (ret < 0)
181 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF,
182 &nl_rcvbufsize, sizeof(nl_rcvbufsize));
183 if (ret < 0) {
450971aa 184 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
185 "Can't set %s receive buffer size: %s", nl->name,
186 safe_strerror(errno));
d62a17ae 187 return -1;
188 }
189
190 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
191 if (ret < 0) {
450971aa 192 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
193 "Can't get %s receive buffer size: %s", nl->name,
194 safe_strerror(errno));
d62a17ae 195 return -1;
196 }
197
198 zlog_info("Setting netlink socket receive buffer size: %u -> %u",
199 oldsize, newsize);
200 return 0;
1fdc9eae 201}
202
203/* Make socket for Linux netlink interface. */
d62a17ae 204static int netlink_socket(struct nlsock *nl, unsigned long groups,
205 ns_id_t ns_id)
1fdc9eae 206{
d62a17ae 207 int ret;
208 struct sockaddr_nl snl;
209 int sock;
210 int namelen;
d62a17ae 211
6bb30c2c
DL
212 frr_elevate_privs(&zserv_privs) {
213 sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id);
214 if (sock < 0) {
215 zlog_err("Can't open %s socket: %s", nl->name,
216 safe_strerror(errno));
217 return -1;
218 }
d62a17ae 219
6bb30c2c
DL
220 memset(&snl, 0, sizeof snl);
221 snl.nl_family = AF_NETLINK;
222 snl.nl_groups = groups;
d62a17ae 223
6bb30c2c
DL
224 /* Bind the socket to the netlink structure for anything. */
225 ret = bind(sock, (struct sockaddr *)&snl, sizeof snl);
226 }
d62a17ae 227
228 if (ret < 0) {
6bb30c2c
DL
229 zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name,
230 snl.nl_groups, safe_strerror(errno));
d62a17ae 231 close(sock);
232 return -1;
233 }
234
235 /* multiple netlink sockets will have different nl_pid */
236 namelen = sizeof snl;
237 ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen);
238 if (ret < 0 || namelen != sizeof snl) {
450971aa 239 flog_err_sys(EC_LIB_SOCKET, "Can't get %s socket name: %s",
09c866e3 240 nl->name, safe_strerror(errno));
d62a17ae 241 close(sock);
242 return -1;
243 }
244
245 nl->snl = snl;
246 nl->sock = sock;
247 return ret;
1fdc9eae 248}
249
2414abd3 250static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
d62a17ae 251 int startup)
1fdc9eae 252{
3575d9e8
DS
253 /*
254 * When we handle new message types here
255 * because we are starting to install them
256 * then lets check the netlink_install_filter
257 * and see if we should add the corresponding
258 * allow through entry there.
259 * Probably not needed to do but please
260 * think about it.
261 */
d62a17ae 262 switch (h->nlmsg_type) {
263 case RTM_NEWROUTE:
2414abd3 264 return netlink_route_change(h, ns_id, startup);
d62a17ae 265 case RTM_DELROUTE:
2414abd3 266 return netlink_route_change(h, ns_id, startup);
d62a17ae 267 case RTM_NEWLINK:
2414abd3 268 return netlink_link_change(h, ns_id, startup);
d62a17ae 269 case RTM_DELLINK:
2414abd3 270 return netlink_link_change(h, ns_id, startup);
d62a17ae 271 case RTM_NEWADDR:
2414abd3 272 return netlink_interface_addr(h, ns_id, startup);
d62a17ae 273 case RTM_DELADDR:
2414abd3 274 return netlink_interface_addr(h, ns_id, startup);
d62a17ae 275 case RTM_NEWNEIGH:
2414abd3 276 return netlink_neigh_change(h, ns_id);
d62a17ae 277 case RTM_DELNEIGH:
2414abd3 278 return netlink_neigh_change(h, ns_id);
942bf97b 279 case RTM_NEWRULE:
2414abd3 280 return netlink_rule_change(h, ns_id, startup);
942bf97b 281 case RTM_DELRULE:
2414abd3 282 return netlink_rule_change(h, ns_id, startup);
d62a17ae 283 default:
3575d9e8
DS
284 /*
285 * If we have received this message then
286 * we have made a mistake during development
287 * and we need to write some code to handle
288 * this message type or not ask for
289 * it to be sent up to us
290 */
e914ccbe 291 flog_err(EC_ZEBRA_UNKNOWN_NLMSG,
1c50c1c0
QY
292 "Unknown netlink nlmsg_type %s(%d) vrf %u\n",
293 nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type,
294 ns_id);
d62a17ae 295 break;
296 }
297 return 0;
1fdc9eae 298}
299
acfa8927 300#if defined(HANDLE_NETLINK_FUZZING)
81a2f870
SW
301/* Using globals here to avoid adding function parameters */
302
303/* Keep distinct filenames for netlink fuzzy collection */
304static unsigned int netlink_file_counter = 1;
305
306/* File name to read fuzzed netlink from */
307static char netlink_fuzz_file[MAXPATHLEN] = "";
308
309/* Flag for whether to read from file or not */
29bf7b0b 310bool netlink_read;
81a2f870
SW
311
312/**
313 * netlink_read_init() - Starts the message parser
314 * @fname: Filename to read.
315 */
316void netlink_read_init(const char *fname)
317{
1bcea841
MS
318 struct zebra_dplane_info dp_info;
319
81a2f870
SW
320 snprintf(netlink_fuzz_file, MAXPATHLEN, "%s", fname);
321 /* Creating this fake socket for testing purposes */
ef593eff
SW
322 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
323
1bcea841
MS
324 /* Capture key info from zns struct */
325 zebra_dplane_info_from_zns(&dp_info, zns, false);
326
327 netlink_parse_info(netlink_information_fetch, &zns->netlink,
328 &dp_info, 1, 0);
81a2f870
SW
329}
330
331/**
332 * netlink_write_incoming() - Writes all data received from netlink to a file
333 * @buf: Data from netlink.
334 * @size: Size of data.
335 * @counter: Counter for keeping filenames distinct.
336 */
337static void netlink_write_incoming(const char *buf, const unsigned int size,
338 unsigned int counter)
339{
340 char fname[MAXPATHLEN];
341 FILE *f;
342
3c649c71 343 snprintf(fname, MAXPATHLEN, "%s/%s_%u", frr_vtydir, "netlink", counter);
6bb30c2c
DL
344 frr_elevate_privs(&zserv_privs) {
345 f = fopen(fname, "w");
346 }
81a2f870
SW
347 if (f) {
348 fwrite(buf, 1, size, f);
349 fclose(f);
350 }
81a2f870
SW
351}
352
353/**
354 * netlink_read_file() - Reads netlink data from file
355 * @buf: Netlink buffer being overwritten.
356 * @fname: File name to read from.
357 *
358 * Return: Size of file.
359 */
360static long netlink_read_file(char *buf, const char *fname)
361{
362 FILE *f;
363 long file_bytes = -1;
bd7891fd 364
6bb30c2c
DL
365 frr_elevate_privs(&zserv_privs) {
366 f = fopen(fname, "r");
367 }
81a2f870
SW
368 if (f) {
369 fseek(f, 0, SEEK_END);
370 file_bytes = ftell(f);
371 rewind(f);
ef593eff 372 fread(buf, NL_RCV_PKT_BUF_SIZE, 1, f);
81a2f870
SW
373 fclose(f);
374 }
81a2f870
SW
375 return file_bytes;
376}
377
acfa8927 378#endif /* HANDLE_NETLINK_FUZZING */
81a2f870 379
d62a17ae 380static int kernel_read(struct thread *thread)
1fdc9eae 381{
d62a17ae 382 struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread);
85a75f1e
MS
383 struct zebra_dplane_info dp_info;
384
385 /* Capture key info from ns struct */
386 zebra_dplane_info_from_zns(&dp_info, zns, false);
387
388 netlink_parse_info(netlink_information_fetch, &zns->netlink, &dp_info,
389 5, 0);
d62a17ae 390 zns->t_netlink = NULL;
3801e764 391 thread_add_read(zrouter.master, kernel_read, zns, zns->netlink.sock,
d62a17ae 392 &zns->t_netlink);
1fdc9eae 393
d62a17ae 394 return 0;
1fdc9eae 395}
396
3575d9e8
DS
397/*
398 * Filter out messages from self that occur on listener socket,
62b8bb7a 399 * caused by our actions on the command socket(s)
3575d9e8
DS
400 *
401 * When we add new Netlink message types we probably
402 * do not need to add them here as that we are filtering
403 * on the routes we actually care to receive( which is rarer
404 * then the normal course of operations). We are intentionally
405 * allowing some messages from ourselves through
406 * ( I'm looking at you Interface based netlink messages )
407 * so that we only had to write one way to handle incoming
408 * address add/delete changes.
1fdc9eae 409 */
62b8bb7a 410static void netlink_install_filter(int sock, __u32 pid, __u32 dplane_pid)
1fdc9eae 411{
3575d9e8
DS
412 /*
413 * BPF_JUMP instructions and where you jump to are based upon
414 * 0 as being the next statement. So count from 0. Writing
415 * this down because every time I look at this I have to
416 * re-remember it.
417 */
d62a17ae 418 struct sock_filter filter[] = {
3575d9e8
DS
419 /*
420 * Logic:
62b8bb7a
MS
421 * if (nlmsg_pid == pid ||
422 * nlmsg_pid == dplane_pid) {
3575d9e8
DS
423 * if (the incoming nlmsg_type ==
424 * RTM_NEWADDR | RTM_DELADDR)
425 * keep this message
426 * else
427 * skip this message
428 * } else
429 * keep this netlink message
430 */
431 /*
432 * 0: Load the nlmsg_pid into the BPF register
433 */
d62a17ae 434 BPF_STMT(BPF_LD | BPF_ABS | BPF_W,
435 offsetof(struct nlmsghdr, nlmsg_pid)),
3575d9e8
DS
436 /*
437 * 1: Compare to pid
438 */
62b8bb7a 439 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 1, 0),
3575d9e8 440 /*
62b8bb7a
MS
441 * 2: Compare to dplane pid
442 */
443 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(dplane_pid), 0, 4),
444 /*
445 * 3: Load the nlmsg_type into BPF register
3575d9e8
DS
446 */
447 BPF_STMT(BPF_LD | BPF_ABS | BPF_H,
448 offsetof(struct nlmsghdr, nlmsg_type)),
449 /*
62b8bb7a 450 * 4: Compare to RTM_NEWADDR
3575d9e8
DS
451 */
452 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 2, 0),
453 /*
62b8bb7a 454 * 5: Compare to RTM_DELADDR
3575d9e8
DS
455 */
456 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 1, 0),
457 /*
62b8bb7a 458 * 6: This is the end state of we want to skip the
3575d9e8
DS
459 * message
460 */
d62a17ae 461 BPF_STMT(BPF_RET | BPF_K, 0),
62b8bb7a 462 /* 7: This is the end state of we want to keep
3575d9e8
DS
463 * the message
464 */
d62a17ae 465 BPF_STMT(BPF_RET | BPF_K, 0xffff),
466 };
467
468 struct sock_fprog prog = {
9d303b37 469 .len = array_size(filter), .filter = filter,
d62a17ae 470 };
471
472 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))
473 < 0)
1c50c1c0 474 flog_err_sys(EC_LIB_SOCKET, "Can't install socket filter: %s\n",
9df414fe 475 safe_strerror(errno));
1fdc9eae 476}
477
d62a17ae 478void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
479 int len)
1fdc9eae 480{
d62a17ae 481 while (RTA_OK(rta, len)) {
482 if (rta->rta_type <= max)
483 tb[rta->rta_type] = rta;
484 rta = RTA_NEXT(rta, len);
485 }
1fdc9eae 486}
487
87da6a60
SW
488/**
489 * netlink_parse_rtattr_nested() - Parses a nested route attribute
490 * @tb: Pointer to array for storing rtattr in.
491 * @max: Max number to store.
492 * @rta: Pointer to rtattr to look for nested items in.
493 */
494void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
495 struct rtattr *rta)
496{
497 netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta));
498}
499
86391e56
MS
500int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
501 const void *data, unsigned int alen)
1fdc9eae 502{
d62a17ae 503 int len;
504 struct rtattr *rta;
1fdc9eae 505
d62a17ae 506 len = RTA_LENGTH(alen);
1fdc9eae 507
d62a17ae 508 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
509 return -1;
1fdc9eae 510
d62a17ae 511 rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
512 rta->rta_type = type;
513 rta->rta_len = len;
4b2792b5 514
d62a17ae 515 if (data)
516 memcpy(RTA_DATA(rta), data, alen);
517 else
518 assert(alen == 0);
4b2792b5 519
d62a17ae 520 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1fdc9eae 521
d62a17ae 522 return 0;
1fdc9eae 523}
524
86391e56
MS
525int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type,
526 const void *data, unsigned int alen)
1fdc9eae 527{
d62a17ae 528 unsigned int len;
529 struct rtattr *subrta;
1fdc9eae 530
d62a17ae 531 len = RTA_LENGTH(alen);
1fdc9eae 532
d62a17ae 533 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen)
534 return -1;
1fdc9eae 535
d62a17ae 536 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
537 subrta->rta_type = type;
538 subrta->rta_len = len;
4b2792b5 539
d62a17ae 540 if (data)
541 memcpy(RTA_DATA(subrta), data, alen);
542 else
543 assert(alen == 0);
4b2792b5 544
d62a17ae 545 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
1fdc9eae 546
d62a17ae 547 return 0;
1fdc9eae 548}
549
d7c0a89a 550int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, uint16_t data)
bbc16902 551{
d7c0a89a 552 return addattr_l(n, maxlen, type, &data, sizeof(uint16_t));
bbc16902 553}
554
d62a17ae 555int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data)
1fdc9eae 556{
d7c0a89a 557 return addattr_l(n, maxlen, type, &data, sizeof(uint32_t));
1fdc9eae 558}
559
d62a17ae 560struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
1fdc9eae 561{
d62a17ae 562 struct rtattr *nest = NLMSG_TAIL(n);
1fdc9eae 563
d62a17ae 564 addattr_l(n, maxlen, type, NULL, 0);
565 return nest;
1fdc9eae 566}
567
d62a17ae 568int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1fdc9eae 569{
d7c0a89a 570 nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest;
d62a17ae 571 return n->nlmsg_len;
1fdc9eae 572}
573
d62a17ae 574struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
1fdc9eae 575{
d62a17ae 576 struct rtattr *nest = RTA_TAIL(rta);
1fdc9eae 577
d62a17ae 578 rta_addattr_l(rta, maxlen, type, NULL, 0);
579 return nest;
1fdc9eae 580}
581
d62a17ae 582int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
1fdc9eae 583{
d7c0a89a 584 nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest;
d62a17ae 585 return rta->rta_len;
1fdc9eae 586}
587
d62a17ae 588const char *nl_msg_type_to_str(uint16_t msg_type)
1fdc9eae 589{
d62a17ae 590 return lookup_msg(nlmsg_str, msg_type, "");
1fdc9eae 591}
592
d7c0a89a 593const char *nl_rtproto_to_str(uint8_t rtproto)
1fdc9eae 594{
d62a17ae 595 return lookup_msg(rtproto_str, rtproto, "");
1fdc9eae 596}
b339bde7 597
d7c0a89a 598const char *nl_family_to_str(uint8_t family)
b339bde7 599{
d62a17ae 600 return lookup_msg(family_str, family, "");
b339bde7
DS
601}
602
d7c0a89a 603const char *nl_rttype_to_str(uint8_t rttype)
b339bde7 604{
d62a17ae 605 return lookup_msg(rttype_str, rttype, "");
b339bde7
DS
606}
607
4cebb2b6 608#define NLA_OK(nla, len) \
5d307d5d
DS
609 ((len) >= (int)sizeof(struct nlattr) \
610 && (nla)->nla_len >= sizeof(struct nlattr) \
611 && (nla)->nla_len <= (len))
4cebb2b6
SW
612#define NLA_NEXT(nla, attrlen) \
613 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
614 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
615#define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
616#define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
617
618#define ERR_NLA(err, inner_len) \
619 ((struct nlattr *)(((char *)(err)) \
620 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
621 + NLMSG_ALIGN((inner_len))))
5d307d5d
DS
622
623static void netlink_parse_nlattr(struct nlattr **tb, int max,
624 struct nlattr *nla, int len)
625{
4cebb2b6 626 while (NLA_OK(nla, len)) {
5d307d5d
DS
627 if (nla->nla_type <= max)
628 tb[nla->nla_type] = nla;
4cebb2b6 629 nla = NLA_NEXT(nla, len);
5d307d5d
DS
630 }
631}
632
633static void netlink_parse_extended_ack(struct nlmsghdr *h)
634{
4cebb2b6
SW
635 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
636 const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(h);
5d307d5d 637 const struct nlmsghdr *err_nlh = NULL;
4cebb2b6
SW
638 /* Length not including nlmsghdr */
639 uint32_t len = 0;
640 /* Inner error netlink message length */
641 uint32_t inner_len = 0;
5d307d5d
DS
642 const char *msg = NULL;
643 uint32_t off = 0;
644
645 if (!(h->nlmsg_flags & NLM_F_CAPPED))
4cebb2b6
SW
646 inner_len = (uint32_t)NLMSG_PAYLOAD(&err->msg, 0);
647
648 len = (uint32_t)(NLMSG_PAYLOAD(h, sizeof(struct nlmsgerr)) - inner_len);
5d307d5d 649
4cebb2b6
SW
650 netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, ERR_NLA(err, inner_len),
651 len);
5d307d5d
DS
652
653 if (tb[NLMSGERR_ATTR_MSG])
4cebb2b6 654 msg = (const char *)NLA_DATA(tb[NLMSGERR_ATTR_MSG]);
5d307d5d
DS
655
656 if (tb[NLMSGERR_ATTR_OFFS]) {
4cebb2b6 657 off = *(uint32_t *)NLA_DATA(tb[NLMSGERR_ATTR_OFFS]);
5d307d5d
DS
658
659 if (off > h->nlmsg_len) {
9165c5f5 660 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
5d307d5d
DS
661 } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) {
662 /*
663 * Header of failed message
664 * we are not doing anything currently with it
665 * but noticing it for later.
666 */
667 err_nlh = &err->msg;
87b5d1b0
DS
668 zlog_debug("%s: Received %s extended Ack",
669 __PRETTY_FUNCTION__,
670 nl_msg_type_to_str(err_nlh->nlmsg_type));
5d307d5d
DS
671 }
672 }
673
674 if (msg && *msg != '\0') {
675 bool is_err = !!err->error;
676
677 if (is_err)
678 zlog_err("Extended Error: %s", msg);
679 else
e914ccbe 680 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING,
9df414fe 681 "Extended Warning: %s", msg);
5d307d5d
DS
682 }
683}
684
936ebf0a
DS
685/*
686 * netlink_parse_info
687 *
688 * Receive message from netlink interface and pass those information
689 * to the given function.
690 *
691 * filter -> Function to call to read the results
692 * nl -> netlink socket information
693 * zns -> The zebra namespace data
694 * count -> How many we should read in, 0 means as much as possible
695 * startup -> Are we reading in under startup conditions? passed to
696 * the filter.
697 */
2414abd3 698int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
7cdb1a84
MS
699 const struct nlsock *nl,
700 const struct zebra_dplane_info *zns,
85a75f1e 701 int count, int startup)
1fdc9eae 702{
d62a17ae 703 int status;
704 int ret = 0;
705 int error;
706 int read_in = 0;
707
708 while (1) {
9ed7517b 709 char buf[NL_RCV_PKT_BUF_SIZE];
d62a17ae 710 struct iovec iov = {.iov_base = buf, .iov_len = sizeof buf};
711 struct sockaddr_nl snl;
712 struct msghdr msg = {.msg_name = (void *)&snl,
713 .msg_namelen = sizeof snl,
714 .msg_iov = &iov,
715 .msg_iovlen = 1};
716 struct nlmsghdr *h;
717
718 if (count && read_in >= count)
719 return 0;
720
acfa8927 721#if defined(HANDLE_NETLINK_FUZZING)
81a2f870
SW
722 /* Check if reading and filename is set */
723 if (netlink_read && '\0' != netlink_fuzz_file[0]) {
724 zlog_debug("Reading netlink fuzz file");
725 status = netlink_read_file(buf, netlink_fuzz_file);
726 snl.nl_pid = 0;
727 } else {
728 status = recvmsg(nl->sock, &msg, 0);
729 }
730#else
d62a17ae 731 status = recvmsg(nl->sock, &msg, 0);
acfa8927 732#endif /* HANDLE_NETLINK_FUZZING */
d62a17ae 733 if (status < 0) {
734 if (errno == EINTR)
735 continue;
736 if (errno == EWOULDBLOCK || errno == EAGAIN)
737 break;
e914ccbe 738 flog_err(EC_ZEBRA_RECVMSG_OVERRUN,
1c50c1c0
QY
739 "%s recvmsg overrun: %s", nl->name,
740 safe_strerror(errno));
d62a17ae 741 /*
742 * In this case we are screwed.
743 * There is no good way to
744 * recover zebra at this point.
745 */
746 exit(-1);
747 continue;
1fdc9eae 748 }
749
d62a17ae 750 if (status == 0) {
450971aa 751 flog_err_sys(EC_LIB_SOCKET, "%s EOF", nl->name);
d62a17ae 752 return -1;
753 }
754
755 if (msg.msg_namelen != sizeof snl) {
e914ccbe 756 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0
QY
757 "%s sender address length error: length %d",
758 nl->name, msg.msg_namelen);
d62a17ae 759 return -1;
760 }
761
762 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
763 zlog_debug("%s: << netlink message dump [recv]",
764 __func__);
765 zlog_hexdump(buf, status);
766 }
767
acfa8927 768#if defined(HANDLE_NETLINK_FUZZING)
81a2f870
SW
769 if (!netlink_read) {
770 zlog_debug("Writing incoming netlink message");
771 netlink_write_incoming(buf, status,
772 netlink_file_counter++);
773 }
acfa8927 774#endif /* HANDLE_NETLINK_FUZZING */
81a2f870 775
d62a17ae 776 read_in++;
777 for (h = (struct nlmsghdr *)buf;
e6a0e0d1 778 (status >= 0 && NLMSG_OK(h, (unsigned int)status));
d62a17ae 779 h = NLMSG_NEXT(h, status)) {
780 /* Finish of reading. */
781 if (h->nlmsg_type == NLMSG_DONE)
782 return ret;
783
784 /* Error handling. */
785 if (h->nlmsg_type == NLMSG_ERROR) {
786 struct nlmsgerr *err =
787 (struct nlmsgerr *)NLMSG_DATA(h);
788 int errnum = err->error;
789 int msg_type = err->msg.nlmsg_type;
790
5d307d5d
DS
791 if (h->nlmsg_len
792 < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
13ec4c1d
RZ
793 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
794 "%s error: message truncated",
5d307d5d
DS
795 nl->name);
796 return -1;
797 }
798
799 /*
800 * Parse the extended information before
801 * we actually handle it.
802 * At this point in time we do not
803 * do anything other than report the
804 * issue.
805 */
806 if (h->nlmsg_flags & NLM_F_ACK_TLVS)
807 netlink_parse_extended_ack(h);
808
d62a17ae 809 /* If the error field is zero, then this is an
810 * ACK */
811 if (err->error == 0) {
812 if (IS_ZEBRA_DEBUG_KERNEL) {
813 zlog_debug(
814 "%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
815 __FUNCTION__, nl->name,
816 nl_msg_type_to_str(
817 err->msg.nlmsg_type),
818 err->msg.nlmsg_type,
819 err->msg.nlmsg_seq,
820 err->msg.nlmsg_pid);
821 }
822
823 /* return if not a multipart message,
824 * otherwise continue */
825 if (!(h->nlmsg_flags & NLM_F_MULTI))
826 return 0;
827 continue;
828 }
829
d62a17ae 830 /* Deal with errors that occur because of races
831 * in link handling */
85a75f1e 832 if (zns->is_cmd
d62a17ae 833 && ((msg_type == RTM_DELROUTE
834 && (-errnum == ENODEV
835 || -errnum == ESRCH))
836 || (msg_type == RTM_NEWROUTE
837 && (-errnum == ENETDOWN
838 || -errnum == EEXIST)))) {
839 if (IS_ZEBRA_DEBUG_KERNEL)
840 zlog_debug(
841 "%s: error: %s type=%s(%u), seq=%u, pid=%u",
842 nl->name,
843 safe_strerror(-errnum),
844 nl_msg_type_to_str(
845 msg_type),
846 msg_type,
847 err->msg.nlmsg_seq,
848 err->msg.nlmsg_pid);
849 return 0;
850 }
851
852 /* We see RTM_DELNEIGH when shutting down an
853 * interface with an IPv4
854 * link-local. The kernel should have already
855 * deleted the neighbor
856 * so do not log these as an error.
857 */
858 if (msg_type == RTM_DELNEIGH
85a75f1e 859 || (zns->is_cmd && msg_type == RTM_NEWROUTE
d62a17ae 860 && (-errnum == ESRCH
861 || -errnum == ENETUNREACH))) {
862 /* This is known to happen in some
863 * situations, don't log
864 * as error.
865 */
866 if (IS_ZEBRA_DEBUG_KERNEL)
867 zlog_debug(
868 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
869 nl->name,
870 safe_strerror(-errnum),
871 nl_msg_type_to_str(
872 msg_type),
873 msg_type,
874 err->msg.nlmsg_seq,
875 err->msg.nlmsg_pid);
876 } else
af4c2728 877 flog_err(
e914ccbe 878 EC_ZEBRA_UNEXPECTED_MESSAGE,
d62a17ae 879 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
880 nl->name,
881 safe_strerror(-errnum),
882 nl_msg_type_to_str(msg_type),
883 msg_type, err->msg.nlmsg_seq,
884 err->msg.nlmsg_pid);
885
886 return -1;
887 }
888
889 /* OK we got netlink message. */
890 if (IS_ZEBRA_DEBUG_KERNEL)
891 zlog_debug(
892 "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u",
893 nl->name,
894 nl_msg_type_to_str(h->nlmsg_type),
895 h->nlmsg_type, h->nlmsg_len,
896 h->nlmsg_seq, h->nlmsg_pid);
897
783827ae
DS
898
899 /*
900 * Ignore messages that maybe sent from
901 * other actors besides the kernel
902 */
903 if (snl.nl_pid != 0) {
43e52561
QY
904 zlog_debug("Ignoring message from pid %u",
905 snl.nl_pid);
d62a17ae 906 continue;
907 }
908
2414abd3 909 error = (*filter)(h, zns->ns_id, startup);
d62a17ae 910 if (error < 0) {
9df414fe
QY
911 zlog_debug("%s filter function error",
912 nl->name);
d62a17ae 913 ret = error;
914 }
915 }
916
917 /* After error care. */
918 if (msg.msg_flags & MSG_TRUNC) {
e914ccbe 919 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0 920 "%s error: message truncated", nl->name);
d62a17ae 921 continue;
922 }
923 if (status) {
e914ccbe 924 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0
QY
925 "%s error: data remnant size %d", nl->name,
926 status);
d62a17ae 927 return -1;
928 }
929 }
930 return ret;
1fdc9eae 931}
932
936ebf0a 933/*
7cdb1a84 934 * netlink_talk_info
936ebf0a
DS
935 *
936 * sendmsg() to netlink socket then recvmsg().
937 * Calls netlink_parse_info to parse returned data
938 *
939 * filter -> The filter to read final results from kernel
940 * nlmsghdr -> The data to send to the kernel
8b962e77 941 * dp_info -> The dataplane and netlink socket information
936ebf0a
DS
942 * startup -> Are we reading in under startup conditions
943 * This is passed through eventually to filter.
944 */
7cdb1a84
MS
945int netlink_talk_info(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
946 struct nlmsghdr *n,
947 const struct zebra_dplane_info *dp_info, int startup)
1fdc9eae 948{
8d2dcc85 949 int status = 0;
d62a17ae 950 struct sockaddr_nl snl;
cbaca6a1
DS
951 struct iovec iov;
952 struct msghdr msg;
8d2dcc85 953 int save_errno = 0;
7cdb1a84 954 const struct nlsock *nl;
d62a17ae 955
956 memset(&snl, 0, sizeof snl);
cbaca6a1
DS
957 memset(&iov, 0, sizeof iov);
958 memset(&msg, 0, sizeof msg);
959
960 iov.iov_base = n;
961 iov.iov_len = n->nlmsg_len;
962 msg.msg_name = (void *)&snl;
963 msg.msg_namelen = sizeof snl;
964 msg.msg_iov = &iov;
965 msg.msg_iovlen = 1;
966
d62a17ae 967 snl.nl_family = AF_NETLINK;
968
7cdb1a84
MS
969 nl = &(dp_info->nls);
970 n->nlmsg_seq = nl->seq;
d62a17ae 971 n->nlmsg_pid = nl->snl.nl_pid;
972
d62a17ae 973 if (IS_ZEBRA_DEBUG_KERNEL)
974 zlog_debug(
975 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
976 nl->name, nl_msg_type_to_str(n->nlmsg_type),
977 n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq,
978 n->nlmsg_flags);
979
980 /* Send message to netlink interface. */
01b9e3fd
DL
981 frr_elevate_privs(&zserv_privs) {
982 status = sendmsg(nl->sock, &msg, 0);
983 save_errno = errno;
984 }
d62a17ae 985
986 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
987 zlog_debug("%s: >> netlink message dump [sent]", __func__);
988 zlog_hexdump(n, n->nlmsg_len);
989 }
990
991 if (status < 0) {
450971aa 992 flog_err_sys(EC_LIB_SOCKET, "netlink_talk sendmsg() error: %s",
09c866e3 993 safe_strerror(save_errno));
d62a17ae 994 return -1;
995 }
996
d62a17ae 997 /*
998 * Get reply from netlink socket.
999 * The reply should either be an acknowlegement or an error.
1000 */
7cdb1a84
MS
1001 return netlink_parse_info(filter, nl, dp_info, 0, startup);
1002}
1003
1004/*
1005 * Synchronous version of netlink_talk_info. Converts args to suit the
1006 * common version, which is suitable for both sync and async use.
7cdb1a84
MS
1007 */
1008int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
1009 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
1010 int startup)
1011{
1012 struct zebra_dplane_info dp_info;
1013
1014 /* Increment sequence number before capturing snapshot of ns socket
1015 * info.
1016 */
1017 nl->seq++;
1018
1019 /* Capture info in intermediate info struct */
85a75f1e 1020 zebra_dplane_info_from_zns(&dp_info, zns, (nl == &(zns->netlink_cmd)));
7cdb1a84 1021
5709131c 1022 return netlink_talk_info(filter, n, &dp_info, startup);
1fdc9eae 1023}
1024
289602d7 1025/* Issue request message to kernel via netlink socket. GET messages
1026 * are issued through this interface.
1027 */
d62a17ae 1028int netlink_request(struct nlsock *nl, struct nlmsghdr *n)
1fdc9eae 1029{
d62a17ae 1030 int ret;
1031 struct sockaddr_nl snl;
d62a17ae 1032
1033 /* Check netlink socket. */
1034 if (nl->sock < 0) {
450971aa 1035 flog_err_sys(EC_LIB_SOCKET, "%s socket isn't active.",
09c866e3 1036 nl->name);
d62a17ae 1037 return -1;
1038 }
1039
1040 /* Fill common fields for all requests. */
d62a17ae 1041 n->nlmsg_pid = nl->snl.nl_pid;
1042 n->nlmsg_seq = ++nl->seq;
1043
1044 memset(&snl, 0, sizeof snl);
1045 snl.nl_family = AF_NETLINK;
1046
1047 /* Raise capabilities and send message, then lower capabilities. */
6bb30c2c
DL
1048 frr_elevate_privs(&zserv_privs) {
1049 ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0,
1050 (struct sockaddr *)&snl, sizeof snl);
d62a17ae 1051 }
1052
d62a17ae 1053 if (ret < 0) {
6bb30c2c
DL
1054 zlog_err("%s sendto failed: %s", nl->name,
1055 safe_strerror(errno));
d62a17ae 1056 return -1;
1057 }
1058
1059 return 0;
1fdc9eae 1060}
1061
1062/* Exported interface function. This function simply calls
1063 netlink_socket (). */
d62a17ae 1064void kernel_init(struct zebra_ns *zns)
1fdc9eae 1065{
d62a17ae 1066 unsigned long groups;
5d307d5d
DS
1067#if defined SOL_NETLINK
1068 int one, ret;
1069#endif
d62a17ae 1070
026a316f
DS
1071 /*
1072 * Initialize netlink sockets
1073 *
1074 * If RTMGRP_XXX exists use that, but at some point
1075 * I think the kernel developers realized that
1076 * keeping track of all the different values would
1077 * lead to confusion, so we need to convert the
1078 * RTNLGRP_XXX to a bit position for ourself
1079 */
1080 groups = RTMGRP_LINK |
1081 RTMGRP_IPV4_ROUTE |
1082 RTMGRP_IPV4_IFADDR |
1083 RTMGRP_IPV6_ROUTE |
1084 RTMGRP_IPV6_IFADDR |
1085 RTMGRP_IPV4_MROUTE |
1086 RTMGRP_NEIGH |
1087 (1 << (RTNLGRP_IPV4_RULE - 1)) |
1088 (1 << (RTNLGRP_IPV6_RULE - 1));
d62a17ae 1089
1090 snprintf(zns->netlink.name, sizeof(zns->netlink.name),
1091 "netlink-listen (NS %u)", zns->ns_id);
1092 zns->netlink.sock = -1;
19d5a4fe
DS
1093 if (netlink_socket(&zns->netlink, groups, zns->ns_id) < 0) {
1094 zlog_err("Failure to create %s socket",
1095 zns->netlink.name);
1096 exit(-1);
1097 }
d62a17ae 1098
1099 snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name),
1100 "netlink-cmd (NS %u)", zns->ns_id);
1101 zns->netlink_cmd.sock = -1;
19d5a4fe
DS
1102 if (netlink_socket(&zns->netlink_cmd, 0, zns->ns_id) < 0) {
1103 zlog_err("Failure to create %s socket",
1104 zns->netlink_cmd.name);
1105 exit(-1);
1106 }
d62a17ae 1107
62b8bb7a
MS
1108 snprintf(zns->netlink_dplane.name, sizeof(zns->netlink_dplane.name),
1109 "netlink-dp (NS %u)", zns->ns_id);
1110 zns->netlink_dplane.sock = -1;
1111 if (netlink_socket(&zns->netlink_dplane, 0, zns->ns_id) < 0) {
1112 zlog_err("Failure to create %s socket",
1113 zns->netlink_dplane.name);
1114 exit(-1);
1115 }
1116
5d307d5d
DS
1117 /*
1118 * SOL_NETLINK is not available on all platforms yet
1119 * apparently. It's in bits/socket.h which I am not
1120 * sure that we want to pull into our build system.
1121 */
1122#if defined SOL_NETLINK
1123 /*
1124 * Let's tell the kernel that we want to receive extended
62b8bb7a 1125 * ACKS over our command socket(s)
5d307d5d
DS
1126 */
1127 one = 1;
1128 ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK,
1129 &one, sizeof(one));
1130
1131 if (ret < 0)
62b8bb7a
MS
1132 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1133 errno, safe_strerror(errno));
1134
1135 one = 1;
1136 ret = setsockopt(zns->netlink_dplane.sock, SOL_NETLINK, NETLINK_EXT_ACK,
1137 &one, sizeof(one));
1138
1139 if (ret < 0)
1140 zlog_notice("Registration for extended dp ACK failed : %d %s",
5d307d5d
DS
1141 errno, safe_strerror(errno));
1142#endif
1143
d62a17ae 1144 /* Register kernel socket. */
19d5a4fe 1145 if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
450971aa 1146 flog_err_sys(EC_LIB_SOCKET, "Can't set %s socket flags: %s",
09c866e3 1147 zns->netlink.name, safe_strerror(errno));
8c85e8ea
DS
1148
1149 if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0)
1150 zlog_err("Can't set %s socket error: %s(%d)",
1151 zns->netlink_cmd.name, safe_strerror(errno), errno);
19d5a4fe 1152
62b8bb7a
MS
1153 if (fcntl(zns->netlink_dplane.sock, F_SETFL, O_NONBLOCK) < 0)
1154 zlog_err("Can't set %s socket error: %s(%d)",
1155 zns->netlink_dplane.name, safe_strerror(errno), errno);
1156
19d5a4fe
DS
1157 /* Set receive buffer size if it's set from command line */
1158 if (nl_rcvbufsize)
1159 netlink_recvbuf(&zns->netlink, nl_rcvbufsize);
1160
1161 netlink_install_filter(zns->netlink.sock,
62b8bb7a
MS
1162 zns->netlink_cmd.snl.nl_pid,
1163 zns->netlink_dplane.snl.nl_pid);
1164
19d5a4fe
DS
1165 zns->t_netlink = NULL;
1166
3801e764 1167 thread_add_read(zrouter.master, kernel_read, zns,
19d5a4fe 1168 zns->netlink.sock, &zns->t_netlink);
d62a17ae 1169
1170 rt_netlink_init();
1fdc9eae 1171}
1172
62b8bb7a 1173void kernel_terminate(struct zebra_ns *zns, bool complete)
1fdc9eae 1174{
d62a17ae 1175 THREAD_READ_OFF(zns->t_netlink);
1176
1177 if (zns->netlink.sock >= 0) {
1178 close(zns->netlink.sock);
1179 zns->netlink.sock = -1;
1180 }
1181
1182 if (zns->netlink_cmd.sock >= 0) {
1183 close(zns->netlink_cmd.sock);
1184 zns->netlink_cmd.sock = -1;
1185 }
ddfeb486 1186
62b8bb7a
MS
1187 /* During zebra shutdown, we need to leave the dataplane socket
1188 * around until all work is done.
1189 */
1190 if (complete) {
1191 if (zns->netlink_dplane.sock >= 0) {
1192 close(zns->netlink_dplane.sock);
1193 zns->netlink_dplane.sock = -1;
1194 }
1195 }
1196}
ddfeb486 1197#endif /* HAVE_NETLINK */