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