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