]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_netlink.c
lib, vrrpd: Use THREAD_ARG
[mirror_frr.git] / zebra / kernel_netlink.c
CommitLineData
718e3744 1/* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
1fdc9eae 20
21#include <zebra.h>
22
ddfeb486
DL
23#ifdef HAVE_NETLINK
24
1fdc9eae 25#include "linklist.h"
26#include "if.h"
27#include "log.h"
28#include "prefix.h"
29#include "connected.h"
30#include "table.h"
31#include "memory.h"
1fdc9eae 32#include "rib.h"
33#include "thread.h"
34#include "privs.h"
35#include "nexthop.h"
36#include "vrf.h"
37#include "mpls.h"
174482ef 38#include "lib_errors.h"
d4000d7b 39#include "hash.h"
1fdc9eae 40
3801e764 41#include "zebra/zebra_router.h"
1fdc9eae 42#include "zebra/zebra_ns.h"
43#include "zebra/zebra_vrf.h"
05f7f5db 44#include "zebra/rt.h"
1fdc9eae 45#include "zebra/debug.h"
46#include "zebra/kernel_netlink.h"
47#include "zebra/rt_netlink.h"
48#include "zebra/if_netlink.h"
942bf97b 49#include "zebra/rule_netlink.h"
ebb61fca 50#include "zebra/netconf_netlink.h"
43e52561 51#include "zebra/zebra_errors.h"
1fdc9eae 52
53#ifndef SO_RCVBUFFORCE
54#define SO_RCVBUFFORCE (33)
55#endif
56
57/* Hack for GNU libc version 2. */
58#ifndef MSG_TRUNC
59#define MSG_TRUNC 0x20
60#endif /* MSG_TRUNC */
61
62#ifndef NLMSG_TAIL
d62a17ae 63#define NLMSG_TAIL(nmsg) \
d7c0a89a
QY
64 ((struct rtattr *)(((uint8_t *)(nmsg)) \
65 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
1fdc9eae 66#endif
67
68#ifndef RTA_TAIL
d62a17ae 69#define RTA_TAIL(rta) \
d7c0a89a 70 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
1fdc9eae 71#endif
72
f909c673
DS
73#ifndef RTNL_FAMILY_IP6MR
74#define RTNL_FAMILY_IP6MR 129
75#endif
76
77#ifndef RTPROT_MROUTED
78#define RTPROT_MROUTED 17
79#endif
80
531c92b8 81#define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
e63c7622
JU
82
83/*
84 * We limit the batch's size to a number smaller than the length of the
85 * underlying buffer since the last message that wouldn't fit the batch would go
86 * over the upper boundary and then it would have to be encoded again into a new
87 * buffer. If the difference between the limit and the length of the buffer is
88 * big enough (bigger than the biggest Netlink message) then this situation
89 * won't occur.
90 */
531c92b8
JU
91#define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
92
d62a17ae 93static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"},
94 {RTM_DELROUTE, "RTM_DELROUTE"},
95 {RTM_GETROUTE, "RTM_GETROUTE"},
96 {RTM_NEWLINK, "RTM_NEWLINK"},
5d414138 97 {RTM_SETLINK, "RTM_SETLINK"},
d62a17ae 98 {RTM_DELLINK, "RTM_DELLINK"},
99 {RTM_GETLINK, "RTM_GETLINK"},
100 {RTM_NEWADDR, "RTM_NEWADDR"},
101 {RTM_DELADDR, "RTM_DELADDR"},
102 {RTM_GETADDR, "RTM_GETADDR"},
103 {RTM_NEWNEIGH, "RTM_NEWNEIGH"},
104 {RTM_DELNEIGH, "RTM_DELNEIGH"},
105 {RTM_GETNEIGH, "RTM_GETNEIGH"},
942bf97b 106 {RTM_NEWRULE, "RTM_NEWRULE"},
107 {RTM_DELRULE, "RTM_DELRULE"},
108 {RTM_GETRULE, "RTM_GETRULE"},
79580b5a
SW
109 {RTM_NEWNEXTHOP, "RTM_NEWNEXTHOP"},
110 {RTM_DELNEXTHOP, "RTM_DELNEXTHOP"},
111 {RTM_GETNEXTHOP, "RTM_GETNEXTHOP"},
ebb61fca
DS
112 {RTM_NEWNETCONF, "RTM_NEWNETCONF"},
113 {RTM_DELNETCONF, "RTM_DELNETCONF"},
acc8e687
CS
114 {RTM_NEWTUNNEL, "RTM_NEWTUNNEL"},
115 {RTM_DELTUNNEL, "RTM_DELTUNNEL"},
116 {RTM_GETTUNNEL, "RTM_GETTUNNEL"},
d62a17ae 117 {0}};
1fdc9eae 118
119static const struct message rtproto_str[] = {
d62a17ae 120 {RTPROT_REDIRECT, "redirect"},
121 {RTPROT_KERNEL, "kernel"},
122 {RTPROT_BOOT, "boot"},
123 {RTPROT_STATIC, "static"},
124 {RTPROT_GATED, "GateD"},
125 {RTPROT_RA, "router advertisement"},
126 {RTPROT_MRT, "MRT"},
127 {RTPROT_ZEBRA, "Zebra"},
1fdc9eae 128#ifdef RTPROT_BIRD
d62a17ae 129 {RTPROT_BIRD, "BIRD"},
1fdc9eae 130#endif /* RTPROT_BIRD */
d62a17ae 131 {RTPROT_MROUTED, "mroute"},
132 {RTPROT_BGP, "BGP"},
133 {RTPROT_OSPF, "OSPF"},
134 {RTPROT_ISIS, "IS-IS"},
135 {RTPROT_RIP, "RIP"},
136 {RTPROT_RIPNG, "RIPNG"},
d4d71f11 137 {RTPROT_ZSTATIC, "static"},
d62a17ae 138 {0}};
139
140static const struct message family_str[] = {{AF_INET, "ipv4"},
141 {AF_INET6, "ipv6"},
142 {AF_BRIDGE, "bridge"},
143 {RTNL_FAMILY_IPMR, "ipv4MR"},
144 {RTNL_FAMILY_IP6MR, "ipv6MR"},
145 {0}};
146
8c8f250b
DS
147static const struct message rttype_str[] = {{RTN_UNSPEC, "none"},
148 {RTN_UNICAST, "unicast"},
149 {RTN_LOCAL, "local"},
150 {RTN_BROADCAST, "broadcast"},
151 {RTN_ANYCAST, "anycast"},
d62a17ae 152 {RTN_MULTICAST, "multicast"},
8c8f250b
DS
153 {RTN_BLACKHOLE, "blackhole"},
154 {RTN_UNREACHABLE, "unreachable"},
155 {RTN_PROHIBIT, "prohibited"},
156 {RTN_THROW, "throw"},
157 {RTN_NAT, "nat"},
158 {RTN_XRESOLVE, "resolver"},
d62a17ae 159 {0}};
b339bde7 160
1fdc9eae 161extern struct thread_master *master;
1fdc9eae 162
163extern struct zebra_privs_t zserv_privs;
164
bf8d3d6a 165DEFINE_MTYPE_STATIC(ZEBRA, NL_BUF, "Zebra Netlink buffers");
531c92b8 166
34869809
MS
167/* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
168 * We have both the main and dplane pthreads using these structs, so we have
169 * to protect the hash with a lock.
170 */
171static struct hash *nlsock_hash;
172pthread_mutex_t nlsock_mutex;
173
174/* Lock and unlock wrappers for nlsock hash */
175#define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
176#define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
177
531c92b8
JU
178size_t nl_batch_tx_bufsize;
179char *nl_batch_tx_buf;
180
531c92b8
JU
181_Atomic uint32_t nl_batch_bufsize = NL_DEFAULT_BATCH_BUFSIZE;
182_Atomic uint32_t nl_batch_send_threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD;
183
e63c7622
JU
184struct nl_batch {
185 void *buf;
186 size_t bufsiz;
187 size_t limit;
188
189 void *buf_head;
190 size_t curlen;
191 size_t msgcnt;
192
193 const struct zebra_dplane_info *zns;
e63c7622 194
f6feb48b 195 struct dplane_ctx_q ctx_list;
e63c7622 196
f6feb48b
JU
197 /*
198 * Pointer to the queue of completed contexts outbound back
199 * towards the dataplane module.
200 */
201 struct dplane_ctx_q *ctx_out_q;
e63c7622
JU
202};
203
531c92b8
JU
204int netlink_config_write_helper(struct vty *vty)
205{
206 uint32_t size =
207 atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed);
208 uint32_t threshold = atomic_load_explicit(&nl_batch_send_threshold,
209 memory_order_relaxed);
210
211 if (size != NL_DEFAULT_BATCH_BUFSIZE
212 || threshold != NL_DEFAULT_BATCH_SEND_THRESHOLD)
213 vty_out(vty, "zebra kernel netlink batch-tx-buf %u %u\n", size,
214 threshold);
215
c40e1b1c
SW
216 if (if_netlink_frr_protodown_r_bit_is_set())
217 vty_out(vty, "zebra protodown reason-bit %u\n",
218 if_netlink_get_frr_protodown_r_bit());
219
531c92b8
JU
220 return 0;
221}
222
223void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold, bool set)
224{
225 if (!set) {
226 size = NL_DEFAULT_BATCH_BUFSIZE;
227 threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD;
228 }
229
230 atomic_store_explicit(&nl_batch_bufsize, size, memory_order_relaxed);
231 atomic_store_explicit(&nl_batch_send_threshold, threshold,
232 memory_order_relaxed);
233}
234
2414abd3 235int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 236{
3575d9e8
DS
237 /*
238 * This is an error condition that must be handled during
239 * development.
240 *
241 * The netlink_talk_filter function is used for communication
242 * down the netlink_cmd pipe and we are expecting
243 * an ack being received. So if we get here
244 * then we did not receive the ack and instead
245 * received some other message in an unexpected
246 * way.
247 */
43e52561
QY
248 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__,
249 h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id);
d62a17ae 250 return 0;
1fdc9eae 251}
252
d62a17ae 253static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
1fdc9eae 254{
d7c0a89a 255 uint32_t oldsize;
d62a17ae 256 socklen_t newlen = sizeof(newsize);
257 socklen_t oldlen = sizeof(oldsize);
258 int ret;
259
260 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
261 if (ret < 0) {
450971aa 262 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
263 "Can't get %s receive buffer size: %s", nl->name,
264 safe_strerror(errno));
d62a17ae 265 return -1;
266 }
267
268 /* Try force option (linux >= 2.6.14) and fall back to normal set */
0cf6db21 269 frr_with_privs(&zserv_privs) {
01b9e3fd 270 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE,
9fb83b55 271 &rcvbufsize, sizeof(rcvbufsize));
01b9e3fd 272 }
d62a17ae 273 if (ret < 0)
9fb83b55
DS
274 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize,
275 sizeof(rcvbufsize));
d62a17ae 276 if (ret < 0) {
450971aa 277 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
278 "Can't set %s receive buffer size: %s", nl->name,
279 safe_strerror(errno));
d62a17ae 280 return -1;
281 }
282
283 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
284 if (ret < 0) {
450971aa 285 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
286 "Can't get %s receive buffer size: %s", nl->name,
287 safe_strerror(errno));
d62a17ae 288 return -1;
289 }
d62a17ae 290 return 0;
1fdc9eae 291}
292
fe953d7c
DS
293static const char *group2str(uint32_t group)
294{
295 switch (group) {
296 case RTNLGRP_TUNNEL:
297 return "RTNLGRP_TUNNEL";
298 default:
299 return "UNKNOWN";
300 }
301}
302
1fdc9eae 303/* Make socket for Linux netlink interface. */
d62a17ae 304static int netlink_socket(struct nlsock *nl, unsigned long groups,
fe953d7c
DS
305 uint32_t ext_groups[], uint8_t ext_group_size,
306 ns_id_t ns_id)
1fdc9eae 307{
d62a17ae 308 int ret;
309 struct sockaddr_nl snl;
310 int sock;
311 int namelen;
d62a17ae 312
0cf6db21 313 frr_with_privs(&zserv_privs) {
6bb30c2c
DL
314 sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id);
315 if (sock < 0) {
316 zlog_err("Can't open %s socket: %s", nl->name,
317 safe_strerror(errno));
318 return -1;
319 }
d62a17ae 320
0d6f7fd6 321 memset(&snl, 0, sizeof(snl));
6bb30c2c
DL
322 snl.nl_family = AF_NETLINK;
323 snl.nl_groups = groups;
d62a17ae 324
fe953d7c
DS
325 if (ext_group_size) {
326 uint8_t i;
327
328 for (i = 0; i < ext_group_size; i++) {
cc408c06 329#if defined SOL_NETLINK
fe953d7c
DS
330 ret = setsockopt(sock, SOL_NETLINK,
331 NETLINK_ADD_MEMBERSHIP,
332 &ext_groups[i],
333 sizeof(ext_groups[i]));
334 if (ret < 0) {
335 zlog_notice(
336 "can't setsockopt NETLINK_ADD_MEMBERSHIP for group %s(%u), this linux kernel does not support it: %s(%d)",
337 group2str(ext_groups[i]),
338 ext_groups[i],
339 safe_strerror(errno), errno);
340 }
cc408c06
DS
341#else
342 zlog_notice(
343 "Unable to use NETLINK_ADD_MEMBERSHIP via SOL_NETLINK for %s(%u) since the linux kernel does not support the socket option",
344 group2str(ext_groups[i]),
345 ext_groups[i]);
346#endif
e5b1de8a
CS
347 }
348 }
47e2eb27 349
6bb30c2c 350 /* Bind the socket to the netlink structure for anything. */
0d6f7fd6 351 ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl));
6bb30c2c 352 }
d62a17ae 353
354 if (ret < 0) {
6bb30c2c
DL
355 zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name,
356 snl.nl_groups, safe_strerror(errno));
d62a17ae 357 close(sock);
358 return -1;
359 }
360
361 /* multiple netlink sockets will have different nl_pid */
0d6f7fd6 362 namelen = sizeof(snl);
d62a17ae 363 ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen);
0d6f7fd6 364 if (ret < 0 || namelen != sizeof(snl)) {
450971aa 365 flog_err_sys(EC_LIB_SOCKET, "Can't get %s socket name: %s",
09c866e3 366 nl->name, safe_strerror(errno));
d62a17ae 367 close(sock);
368 return -1;
369 }
370
371 nl->snl = snl;
372 nl->sock = sock;
2cf7651f
DS
373 nl->buflen = NL_RCV_PKT_BUF_SIZE;
374 nl->buf = XMALLOC(MTYPE_NL_BUF, nl->buflen);
375
d62a17ae 376 return ret;
1fdc9eae 377}
378
d166308b
MS
379/*
380 * Dispatch an incoming netlink message; used by the zebra main pthread's
381 * netlink event reader.
382 */
2414abd3 383static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
d62a17ae 384 int startup)
1fdc9eae 385{
3575d9e8
DS
386 /*
387 * When we handle new message types here
388 * because we are starting to install them
389 * then lets check the netlink_install_filter
390 * and see if we should add the corresponding
391 * allow through entry there.
392 * Probably not needed to do but please
393 * think about it.
394 */
d62a17ae 395 switch (h->nlmsg_type) {
396 case RTM_NEWROUTE:
2414abd3 397 return netlink_route_change(h, ns_id, startup);
d62a17ae 398 case RTM_DELROUTE:
2414abd3 399 return netlink_route_change(h, ns_id, startup);
d62a17ae 400 case RTM_NEWLINK:
2414abd3 401 return netlink_link_change(h, ns_id, startup);
d62a17ae 402 case RTM_DELLINK:
2414abd3 403 return netlink_link_change(h, ns_id, startup);
d62a17ae 404 case RTM_NEWNEIGH:
d62a17ae 405 case RTM_DELNEIGH:
951f8bcb 406 case RTM_GETNEIGH:
7a52f27e 407 return netlink_neigh_change(h, ns_id);
942bf97b 408 case RTM_NEWRULE:
2414abd3 409 return netlink_rule_change(h, ns_id, startup);
942bf97b 410 case RTM_DELRULE:
2414abd3 411 return netlink_rule_change(h, ns_id, startup);
79580b5a 412 case RTM_NEWNEXTHOP:
d9f5b2f5 413 return netlink_nexthop_change(h, ns_id, startup);
79580b5a 414 case RTM_DELNEXTHOP:
d9f5b2f5 415 return netlink_nexthop_change(h, ns_id, startup);
d166308b
MS
416
417 /* Messages handled in the dplane thread */
418 case RTM_NEWADDR:
419 case RTM_DELADDR:
cd787a8a
MS
420 case RTM_NEWNETCONF:
421 case RTM_DELNETCONF:
acc8e687
CS
422 case RTM_NEWTUNNEL:
423 case RTM_DELTUNNEL:
424 case RTM_GETTUNNEL:
d166308b 425 return 0;
d62a17ae 426 default:
3575d9e8
DS
427 /*
428 * If we have received this message then
429 * we have made a mistake during development
430 * and we need to write some code to handle
431 * this message type or not ask for
432 * it to be sent up to us
433 */
e914ccbe 434 flog_err(EC_ZEBRA_UNKNOWN_NLMSG,
1d5453d6 435 "Unknown netlink nlmsg_type %s(%d) vrf %u",
1c50c1c0
QY
436 nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type,
437 ns_id);
d62a17ae 438 break;
439 }
440 return 0;
1fdc9eae 441}
442
d166308b
MS
443/*
444 * Dispatch an incoming netlink message; used by the dataplane pthread's
445 * netlink event reader code.
446 */
447static int dplane_netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id,
448 int startup)
449{
450 /*
451 * Dispatch the incoming messages that the dplane pthread handles
452 */
453 switch (h->nlmsg_type) {
454 case RTM_NEWADDR:
455 case RTM_DELADDR:
456 return netlink_interface_addr_dplane(h, ns_id, startup);
457
9f3f1486
MS
458 case RTM_NEWNETCONF:
459 case RTM_DELNETCONF:
460 return netlink_netconf_change(h, ns_id, startup);
461
462 /* TODO -- other messages for the dplane socket and pthread */
463
d166308b
MS
464 case RTM_NEWLINK:
465 case RTM_DELLINK:
466
467 default:
468 break;
469 }
470
471 return 0;
472}
473
cc9f21da 474static void kernel_read(struct thread *thread)
1fdc9eae 475{
d62a17ae 476 struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread);
85a75f1e
MS
477 struct zebra_dplane_info dp_info;
478
479 /* Capture key info from ns struct */
480 zebra_dplane_info_from_zns(&dp_info, zns, false);
481
482 netlink_parse_info(netlink_information_fetch, &zns->netlink, &dp_info,
9bfadae8 483 5, false);
d166308b 484
3801e764 485 thread_add_read(zrouter.master, kernel_read, zns, zns->netlink.sock,
d62a17ae 486 &zns->t_netlink);
1fdc9eae 487}
488
d166308b
MS
489/*
490 * Called by the dplane pthread to read incoming OS messages and dispatch them.
491 */
492int kernel_dplane_read(struct zebra_dplane_info *info)
493{
d4000d7b
DS
494 struct nlsock *nl = kernel_netlink_nlsock_lookup(info->sock);
495
496 netlink_parse_info(dplane_netlink_information_fetch, nl, info, 5,
497 false);
d166308b
MS
498
499 return 0;
500}
501
3575d9e8
DS
502/*
503 * Filter out messages from self that occur on listener socket,
62b8bb7a 504 * caused by our actions on the command socket(s)
3575d9e8
DS
505 *
506 * When we add new Netlink message types we probably
507 * do not need to add them here as that we are filtering
508 * on the routes we actually care to receive( which is rarer
509 * then the normal course of operations). We are intentionally
510 * allowing some messages from ourselves through
511 * ( I'm looking at you Interface based netlink messages )
9f3f1486
MS
512 * so that we only have to write one way to handle incoming
513 * address add/delete and xxxNETCONF changes.
1fdc9eae 514 */
ff45112c 515static void netlink_install_filter(int sock, uint32_t pid, uint32_t dplane_pid)
1fdc9eae 516{
3575d9e8
DS
517 /*
518 * BPF_JUMP instructions and where you jump to are based upon
519 * 0 as being the next statement. So count from 0. Writing
520 * this down because every time I look at this I have to
521 * re-remember it.
522 */
d62a17ae 523 struct sock_filter filter[] = {
3575d9e8
DS
524 /*
525 * Logic:
62b8bb7a
MS
526 * if (nlmsg_pid == pid ||
527 * nlmsg_pid == dplane_pid) {
3575d9e8 528 * if (the incoming nlmsg_type ==
9f3f1486
MS
529 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
530 * RTM_DELNETCONF)
3575d9e8
DS
531 * keep this message
532 * else
533 * skip this message
534 * } else
535 * keep this netlink message
536 */
537 /*
538 * 0: Load the nlmsg_pid into the BPF register
539 */
d62a17ae 540 BPF_STMT(BPF_LD | BPF_ABS | BPF_W,
541 offsetof(struct nlmsghdr, nlmsg_pid)),
3575d9e8
DS
542 /*
543 * 1: Compare to pid
544 */
62b8bb7a 545 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 1, 0),
3575d9e8 546 /*
62b8bb7a
MS
547 * 2: Compare to dplane pid
548 */
9f3f1486 549 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(dplane_pid), 0, 6),
62b8bb7a
MS
550 /*
551 * 3: Load the nlmsg_type into BPF register
3575d9e8
DS
552 */
553 BPF_STMT(BPF_LD | BPF_ABS | BPF_H,
554 offsetof(struct nlmsghdr, nlmsg_type)),
555 /*
62b8bb7a 556 * 4: Compare to RTM_NEWADDR
3575d9e8 557 */
9f3f1486 558 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 4, 0),
3575d9e8 559 /*
62b8bb7a 560 * 5: Compare to RTM_DELADDR
3575d9e8 561 */
9f3f1486
MS
562 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 3, 0),
563 /*
564 * 6: Compare to RTM_NEWNETCONF
565 */
566 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWNETCONF), 2,
567 0),
568 /*
569 * 7: Compare to RTM_DELNETCONF
570 */
571 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELNETCONF), 1,
572 0),
3575d9e8 573 /*
9f3f1486 574 * 8: This is the end state of we want to skip the
3575d9e8
DS
575 * message
576 */
d62a17ae 577 BPF_STMT(BPF_RET | BPF_K, 0),
9f3f1486 578 /* 9: This is the end state of we want to keep
3575d9e8
DS
579 * the message
580 */
d62a17ae 581 BPF_STMT(BPF_RET | BPF_K, 0xffff),
582 };
583
584 struct sock_fprog prog = {
9d303b37 585 .len = array_size(filter), .filter = filter,
d62a17ae 586 };
587
588 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))
589 < 0)
1d5453d6 590 flog_err_sys(EC_LIB_SOCKET, "Can't install socket filter: %s",
9df414fe 591 safe_strerror(errno));
1fdc9eae 592}
593
d166308b
MS
594void netlink_parse_rtattr_flags(struct rtattr **tb, int max, struct rtattr *rta,
595 int len, unsigned short flags)
4bcdb608
NA
596{
597 unsigned short type;
598
269b69d7 599 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
4bcdb608
NA
600 while (RTA_OK(rta, len)) {
601 type = rta->rta_type & ~flags;
602 if ((type <= max) && (!tb[type]))
603 tb[type] = rta;
604 rta = RTA_NEXT(rta, len);
605 }
606}
607
d62a17ae 608void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta,
609 int len)
1fdc9eae 610{
269b69d7 611 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
d62a17ae 612 while (RTA_OK(rta, len)) {
613 if (rta->rta_type <= max)
614 tb[rta->rta_type] = rta;
615 rta = RTA_NEXT(rta, len);
616 }
1fdc9eae 617}
618
87da6a60
SW
619/**
620 * netlink_parse_rtattr_nested() - Parses a nested route attribute
621 * @tb: Pointer to array for storing rtattr in.
622 * @max: Max number to store.
623 * @rta: Pointer to rtattr to look for nested items in.
624 */
625void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
626 struct rtattr *rta)
627{
628 netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta));
629}
630
f8f3e484
CS
631bool nl_addraw_l(struct nlmsghdr *n, unsigned int maxlen, const void *data,
632 unsigned int len)
633{
634 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
1b3cf91b 635 zlog_err("ERROR message exceeded bound of %d", maxlen);
f8f3e484
CS
636 return false;
637 }
638
639 memcpy(NLMSG_TAIL(n), data, len);
640 memset((uint8_t *)NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
641 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
642
643 return true;
644}
645
312a6bee
JU
646bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type,
647 const void *data, unsigned int alen)
1fdc9eae 648{
d62a17ae 649 int len;
650 struct rtattr *rta;
1fdc9eae 651
d62a17ae 652 len = RTA_LENGTH(alen);
1fdc9eae 653
d62a17ae 654 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
312a6bee 655 return false;
1fdc9eae 656
d62a17ae 657 rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
658 rta->rta_type = type;
659 rta->rta_len = len;
4b2792b5 660
d62a17ae 661 if (data)
662 memcpy(RTA_DATA(rta), data, alen);
663 else
664 assert(alen == 0);
4b2792b5 665
d62a17ae 666 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
1fdc9eae 667
312a6bee 668 return true;
1fdc9eae 669}
670
94d70a65
DS
671bool nl_attr_put8(struct nlmsghdr *n, unsigned int maxlen, int type,
672 uint8_t data)
673{
674 return nl_attr_put(n, maxlen, type, &data, sizeof(uint8_t));
675}
676
312a6bee
JU
677bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type,
678 uint16_t data)
bbc16902 679{
312a6bee 680 return nl_attr_put(n, maxlen, type, &data, sizeof(uint16_t));
bbc16902 681}
682
312a6bee
JU
683bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type,
684 uint32_t data)
1fdc9eae 685{
312a6bee 686 return nl_attr_put(n, maxlen, type, &data, sizeof(uint32_t));
1fdc9eae 687}
688
312a6bee 689struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen, int type)
1fdc9eae 690{
d62a17ae 691 struct rtattr *nest = NLMSG_TAIL(n);
1fdc9eae 692
312a6bee
JU
693 if (!nl_attr_put(n, maxlen, type, NULL, 0))
694 return NULL;
695
40d86eba 696 nest->rta_type |= NLA_F_NESTED;
d62a17ae 697 return nest;
1fdc9eae 698}
699
312a6bee 700int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
1fdc9eae 701{
d7c0a89a 702 nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest;
d62a17ae 703 return n->nlmsg_len;
1fdc9eae 704}
705
312a6bee 706struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen)
1fdc9eae 707{
312a6bee 708 struct rtnexthop *rtnh = (struct rtnexthop *)NLMSG_TAIL(n);
1fdc9eae 709
312a6bee
JU
710 if (NLMSG_ALIGN(n->nlmsg_len) + RTNH_ALIGN(sizeof(struct rtnexthop))
711 > maxlen)
712 return NULL;
713
714 memset(rtnh, 0, sizeof(struct rtnexthop));
715 n->nlmsg_len =
716 NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(sizeof(struct rtnexthop));
717
718 return rtnh;
1fdc9eae 719}
720
312a6bee 721void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh)
1fdc9eae 722{
312a6bee 723 rtnh->rtnh_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)rtnh;
1fdc9eae 724}
725
f8f3e484
CS
726bool nl_rta_put(struct rtattr *rta, unsigned int maxlen, int type,
727 const void *data, int alen)
728{
729 struct rtattr *subrta;
730 int len = RTA_LENGTH(alen);
731
732 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
733 zlog_err("ERROR max allowed bound %d exceeded for rtattr",
734 maxlen);
735 return false;
736 }
737 subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
738 subrta->rta_type = type;
739 subrta->rta_len = len;
740 if (alen)
741 memcpy(RTA_DATA(subrta), data, alen);
742 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
743
744 return true;
745}
746
747bool nl_rta_put16(struct rtattr *rta, unsigned int maxlen, int type,
748 uint16_t data)
749{
750 return nl_rta_put(rta, maxlen, type, &data, sizeof(uint16_t));
751}
752
753bool nl_rta_put64(struct rtattr *rta, unsigned int maxlen, int type,
754 uint64_t data)
755{
756 return nl_rta_put(rta, maxlen, type, &data, sizeof(uint64_t));
757}
758
759struct rtattr *nl_rta_nest(struct rtattr *rta, unsigned int maxlen, int type)
760{
761 struct rtattr *nest = RTA_TAIL(rta);
762
763 if (nl_rta_put(rta, maxlen, type, NULL, 0))
764 return NULL;
765
766 nest->rta_type |= NLA_F_NESTED;
767
768 return nest;
769}
770
771int nl_rta_nest_end(struct rtattr *rta, struct rtattr *nest)
772{
773 nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest;
774
775 return rta->rta_len;
776}
777
d62a17ae 778const char *nl_msg_type_to_str(uint16_t msg_type)
1fdc9eae 779{
d62a17ae 780 return lookup_msg(nlmsg_str, msg_type, "");
1fdc9eae 781}
782
d7c0a89a 783const char *nl_rtproto_to_str(uint8_t rtproto)
1fdc9eae 784{
d62a17ae 785 return lookup_msg(rtproto_str, rtproto, "");
1fdc9eae 786}
b339bde7 787
d7c0a89a 788const char *nl_family_to_str(uint8_t family)
b339bde7 789{
d62a17ae 790 return lookup_msg(family_str, family, "");
b339bde7
DS
791}
792
d7c0a89a 793const char *nl_rttype_to_str(uint8_t rttype)
b339bde7 794{
d62a17ae 795 return lookup_msg(rttype_str, rttype, "");
b339bde7
DS
796}
797
4cebb2b6 798#define NLA_OK(nla, len) \
5d307d5d
DS
799 ((len) >= (int)sizeof(struct nlattr) \
800 && (nla)->nla_len >= sizeof(struct nlattr) \
801 && (nla)->nla_len <= (len))
4cebb2b6
SW
802#define NLA_NEXT(nla, attrlen) \
803 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
804 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
805#define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
806#define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
807
808#define ERR_NLA(err, inner_len) \
809 ((struct nlattr *)(((char *)(err)) \
810 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
811 + NLMSG_ALIGN((inner_len))))
5d307d5d
DS
812
813static void netlink_parse_nlattr(struct nlattr **tb, int max,
814 struct nlattr *nla, int len)
815{
4cebb2b6 816 while (NLA_OK(nla, len)) {
5d307d5d
DS
817 if (nla->nla_type <= max)
818 tb[nla->nla_type] = nla;
4cebb2b6 819 nla = NLA_NEXT(nla, len);
5d307d5d
DS
820 }
821}
822
823static void netlink_parse_extended_ack(struct nlmsghdr *h)
824{
4cebb2b6
SW
825 struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {};
826 const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(h);
5d307d5d 827 const struct nlmsghdr *err_nlh = NULL;
4cebb2b6
SW
828 /* Length not including nlmsghdr */
829 uint32_t len = 0;
830 /* Inner error netlink message length */
831 uint32_t inner_len = 0;
5d307d5d
DS
832 const char *msg = NULL;
833 uint32_t off = 0;
834
835 if (!(h->nlmsg_flags & NLM_F_CAPPED))
4cebb2b6
SW
836 inner_len = (uint32_t)NLMSG_PAYLOAD(&err->msg, 0);
837
838 len = (uint32_t)(NLMSG_PAYLOAD(h, sizeof(struct nlmsgerr)) - inner_len);
5d307d5d 839
4cebb2b6
SW
840 netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, ERR_NLA(err, inner_len),
841 len);
5d307d5d
DS
842
843 if (tb[NLMSGERR_ATTR_MSG])
4cebb2b6 844 msg = (const char *)NLA_DATA(tb[NLMSGERR_ATTR_MSG]);
5d307d5d
DS
845
846 if (tb[NLMSGERR_ATTR_OFFS]) {
4cebb2b6 847 off = *(uint32_t *)NLA_DATA(tb[NLMSGERR_ATTR_OFFS]);
5d307d5d
DS
848
849 if (off > h->nlmsg_len) {
9165c5f5 850 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
5d307d5d
DS
851 } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) {
852 /*
853 * Header of failed message
854 * we are not doing anything currently with it
855 * but noticing it for later.
856 */
857 err_nlh = &err->msg;
15569c58 858 zlog_debug("%s: Received %s extended Ack", __func__,
87b5d1b0 859 nl_msg_type_to_str(err_nlh->nlmsg_type));
5d307d5d
DS
860 }
861 }
862
863 if (msg && *msg != '\0') {
864 bool is_err = !!err->error;
865
866 if (is_err)
867 zlog_err("Extended Error: %s", msg);
868 else
e914ccbe 869 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING,
9df414fe 870 "Extended Warning: %s", msg);
5d307d5d
DS
871 }
872}
873
ae6138bf
JU
874/*
875 * netlink_send_msg - send a netlink message of a certain size.
876 *
877 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
878 */
f8653393
JU
879static ssize_t netlink_send_msg(const struct nlsock *nl, void *buf,
880 size_t buflen)
ae6138bf 881{
f8653393
JU
882 struct sockaddr_nl snl = {};
883 struct iovec iov = {};
884 struct msghdr msg = {};
885 ssize_t status;
886 int save_errno = 0;
ae6138bf
JU
887
888 iov.iov_base = buf;
889 iov.iov_len = buflen;
f8653393 890 msg.msg_name = &snl;
ae6138bf
JU
891 msg.msg_namelen = sizeof(snl);
892 msg.msg_iov = &iov;
893 msg.msg_iovlen = 1;
894
895 snl.nl_family = AF_NETLINK;
896
897 /* Send message to netlink interface. */
898 frr_with_privs(&zserv_privs) {
899 status = sendmsg(nl->sock, &msg, 0);
900 save_errno = errno;
901 }
902
903 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
904 zlog_debug("%s: >> netlink message dump [sent]", __func__);
eead0bc4
RZ
905#ifdef NETLINK_DEBUG
906 nl_dump(buf, buflen);
907#else
ae6138bf 908 zlog_hexdump(buf, buflen);
eead0bc4 909#endif /* NETLINK_DEBUG */
ae6138bf
JU
910 }
911
f8653393 912 if (status == -1) {
ae6138bf
JU
913 flog_err_sys(EC_LIB_SOCKET, "%s error: %s", __func__,
914 safe_strerror(save_errno));
915 return -1;
916 }
917
918 return status;
919}
920
921/*
922 * netlink_recv_msg - receive a netlink message.
923 *
924 * Returns -1 on error, 0 if read would block or the number of bytes received.
925 */
2cf7651f 926static int netlink_recv_msg(struct nlsock *nl, struct msghdr *msg)
ae6138bf
JU
927{
928 struct iovec iov;
929 int status;
930
2cf7651f
DS
931 iov.iov_base = nl->buf;
932 iov.iov_len = nl->buflen;
933 msg->msg_iov = &iov;
934 msg->msg_iovlen = 1;
ae6138bf
JU
935
936 do {
2cf7651f
DS
937 int bytes;
938
939 bytes = recv(nl->sock, NULL, 0, MSG_PEEK | MSG_TRUNC);
940
941 if (bytes >= 0 && (size_t)bytes > nl->buflen) {
942 nl->buf = XREALLOC(MTYPE_NL_BUF, nl->buf, bytes);
943 nl->buflen = bytes;
944 iov.iov_base = nl->buf;
945 iov.iov_len = nl->buflen;
946 }
947
948 status = recvmsg(nl->sock, msg, 0);
f8653393 949 } while (status == -1 && errno == EINTR);
ae6138bf 950
f8653393 951 if (status == -1) {
ae6138bf
JU
952 if (errno == EWOULDBLOCK || errno == EAGAIN)
953 return 0;
954 flog_err(EC_ZEBRA_RECVMSG_OVERRUN, "%s recvmsg overrun: %s",
955 nl->name, safe_strerror(errno));
956 /*
957 * In this case we are screwed. There is no good way to recover
958 * zebra at this point.
959 */
960 exit(-1);
961 }
962
963 if (status == 0) {
964 flog_err_sys(EC_LIB_SOCKET, "%s EOF", nl->name);
965 return -1;
966 }
967
2cf7651f 968 if (msg->msg_namelen != sizeof(struct sockaddr_nl)) {
ae6138bf
JU
969 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
970 "%s sender address length error: length %d", nl->name,
2cf7651f 971 msg->msg_namelen);
ae6138bf
JU
972 return -1;
973 }
974
975 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) {
976 zlog_debug("%s: << netlink message dump [recv]", __func__);
eead0bc4 977#ifdef NETLINK_DEBUG
2cf7651f 978 nl_dump(nl->buf, status);
eead0bc4 979#else
2cf7651f 980 zlog_hexdump(nl->buf, status);
eead0bc4 981#endif /* NETLINK_DEBUG */
ae6138bf
JU
982 }
983
ae6138bf
JU
984 return status;
985}
986
987/*
988 * netlink_parse_error - parse a netlink error message
989 *
990 * Returns 1 if this message is acknowledgement, 0 if this error should be
991 * ignored, -1 otherwise.
992 */
993static int netlink_parse_error(const struct nlsock *nl, struct nlmsghdr *h,
d166308b 994 bool is_cmd, bool startup)
ae6138bf
JU
995{
996 struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
997 int errnum = err->error;
998 int msg_type = err->msg.nlmsg_type;
999
1000 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1001 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1002 "%s error: message truncated", nl->name);
1003 return -1;
1004 }
1005
1006 /*
1007 * Parse the extended information before we actually handle it. At this
1008 * point in time we do not do anything other than report the issue.
1009 */
1010 if (h->nlmsg_flags & NLM_F_ACK_TLVS)
1011 netlink_parse_extended_ack(h);
1012
1013 /* If the error field is zero, then this is an ACK. */
1014 if (err->error == 0) {
1015 if (IS_ZEBRA_DEBUG_KERNEL) {
1016 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
1017 __func__, nl->name,
1018 nl_msg_type_to_str(err->msg.nlmsg_type),
1019 err->msg.nlmsg_type, err->msg.nlmsg_seq,
1020 err->msg.nlmsg_pid);
1021 }
1022
1023 return 1;
1024 }
1025
1026 /* Deal with errors that occur because of races in link handling. */
d166308b 1027 if (is_cmd
ae6138bf
JU
1028 && ((msg_type == RTM_DELROUTE
1029 && (-errnum == ENODEV || -errnum == ESRCH))
1030 || (msg_type == RTM_NEWROUTE
1031 && (-errnum == ENETDOWN || -errnum == EEXIST)))) {
1032 if (IS_ZEBRA_DEBUG_KERNEL)
1033 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
1034 nl->name, safe_strerror(-errnum),
1035 nl_msg_type_to_str(msg_type), msg_type,
1036 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1037 return 0;
1038 }
1039
1040 /*
1041 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
1042 * link-local. The kernel should have already deleted the neighbor so
1043 * do not log these as an error.
1044 */
1045 if (msg_type == RTM_DELNEIGH
d166308b 1046 || (is_cmd && msg_type == RTM_NEWROUTE
ae6138bf
JU
1047 && (-errnum == ESRCH || -errnum == ENETUNREACH))) {
1048 /*
1049 * This is known to happen in some situations, don't log as
1050 * error.
1051 */
1052 if (IS_ZEBRA_DEBUG_KERNEL)
1053 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
1054 nl->name, safe_strerror(-errnum),
1055 nl_msg_type_to_str(msg_type), msg_type,
1056 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1057 } else {
1058 if ((msg_type != RTM_GETNEXTHOP) || !startup)
1059 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE,
1060 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
1061 nl->name, safe_strerror(-errnum),
1062 nl_msg_type_to_str(msg_type), msg_type,
1063 err->msg.nlmsg_seq, err->msg.nlmsg_pid);
1064 }
1065
1066 return -1;
1067}
1068
936ebf0a
DS
1069/*
1070 * netlink_parse_info
1071 *
1072 * Receive message from netlink interface and pass those information
1073 * to the given function.
1074 *
1075 * filter -> Function to call to read the results
1076 * nl -> netlink socket information
1077 * zns -> The zebra namespace data
1078 * count -> How many we should read in, 0 means as much as possible
1079 * startup -> Are we reading in under startup conditions? passed to
1080 * the filter.
1081 */
2414abd3 1082int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
2cf7651f 1083 struct nlsock *nl, const struct zebra_dplane_info *zns,
9bfadae8 1084 int count, bool startup)
1fdc9eae 1085{
d62a17ae 1086 int status;
1087 int ret = 0;
1088 int error;
1089 int read_in = 0;
1090
1091 while (1) {
d62a17ae 1092 struct sockaddr_nl snl;
1093 struct msghdr msg = {.msg_name = (void *)&snl,
ae6138bf 1094 .msg_namelen = sizeof(snl)};
d62a17ae 1095 struct nlmsghdr *h;
1096
1097 if (count && read_in >= count)
1098 return 0;
1099
2cf7651f 1100 status = netlink_recv_msg(nl, &msg);
ae6138bf 1101 if (status == -1)
d62a17ae 1102 return -1;
ae6138bf
JU
1103 else if (status == 0)
1104 break;
81a2f870 1105
d62a17ae 1106 read_in++;
2cf7651f 1107 for (h = (struct nlmsghdr *)nl->buf;
e6a0e0d1 1108 (status >= 0 && NLMSG_OK(h, (unsigned int)status));
d62a17ae 1109 h = NLMSG_NEXT(h, status)) {
1110 /* Finish of reading. */
1111 if (h->nlmsg_type == NLMSG_DONE)
1112 return ret;
1113
1114 /* Error handling. */
1115 if (h->nlmsg_type == NLMSG_ERROR) {
d166308b
MS
1116 int err = netlink_parse_error(
1117 nl, h, zns->is_cmd, startup);
1118
ae6138bf 1119 if (err == 1) {
d62a17ae 1120 if (!(h->nlmsg_flags & NLM_F_MULTI))
1121 return 0;
1122 continue;
ae6138bf
JU
1123 } else
1124 return err;
d62a17ae 1125 }
1126
2f71996a
DS
1127 /*
1128 * What is the right thing to do? The kernel
1129 * is telling us that the dump request was interrupted
1130 * and we more than likely are out of luck and have
1131 * missed data from the kernel. At this point in time
1132 * lets just note that this is happening.
1133 */
1134 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
1135 flog_err(
1136 EC_ZEBRA_NETLINK_BAD_SEQUENCE,
1137 "netlink recvmsg: The Dump request was interrupted");
1138
d62a17ae 1139 /* OK we got netlink message. */
1140 if (IS_ZEBRA_DEBUG_KERNEL)
1141 zlog_debug(
d166308b
MS
1142 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1143 __func__, nl->name,
d62a17ae 1144 nl_msg_type_to_str(h->nlmsg_type),
1145 h->nlmsg_type, h->nlmsg_len,
1146 h->nlmsg_seq, h->nlmsg_pid);
1147
783827ae
DS
1148
1149 /*
1150 * Ignore messages that maybe sent from
1151 * other actors besides the kernel
1152 */
1153 if (snl.nl_pid != 0) {
43e52561
QY
1154 zlog_debug("Ignoring message from pid %u",
1155 snl.nl_pid);
d62a17ae 1156 continue;
1157 }
1158
2414abd3 1159 error = (*filter)(h, zns->ns_id, startup);
d62a17ae 1160 if (error < 0) {
9df414fe
QY
1161 zlog_debug("%s filter function error",
1162 nl->name);
d62a17ae 1163 ret = error;
1164 }
1165 }
1166
1167 /* After error care. */
1168 if (msg.msg_flags & MSG_TRUNC) {
e914ccbe 1169 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0 1170 "%s error: message truncated", nl->name);
d62a17ae 1171 continue;
1172 }
1173 if (status) {
e914ccbe 1174 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0
QY
1175 "%s error: data remnant size %d", nl->name,
1176 status);
d62a17ae 1177 return -1;
1178 }
1179 }
1180 return ret;
1fdc9eae 1181}
1182
936ebf0a 1183/*
7cdb1a84 1184 * netlink_talk_info
936ebf0a
DS
1185 *
1186 * sendmsg() to netlink socket then recvmsg().
1187 * Calls netlink_parse_info to parse returned data
1188 *
1189 * filter -> The filter to read final results from kernel
1190 * nlmsghdr -> The data to send to the kernel
8b962e77 1191 * dp_info -> The dataplane and netlink socket information
936ebf0a
DS
1192 * startup -> Are we reading in under startup conditions
1193 * This is passed through eventually to filter.
1194 */
2cf7651f
DS
1195static int netlink_talk_info(int (*filter)(struct nlmsghdr *, ns_id_t,
1196 int startup),
1197 struct nlmsghdr *n,
1198 struct zebra_dplane_info *dp_info, bool startup)
1fdc9eae 1199{
d4000d7b 1200 struct nlsock *nl;
d62a17ae 1201
d4000d7b 1202 nl = kernel_netlink_nlsock_lookup(dp_info->sock);
3670f504 1203 n->nlmsg_seq = dp_info->seq;
d62a17ae 1204 n->nlmsg_pid = nl->snl.nl_pid;
1205
d62a17ae 1206 if (IS_ZEBRA_DEBUG_KERNEL)
1207 zlog_debug(
1208 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1209 nl->name, nl_msg_type_to_str(n->nlmsg_type),
1210 n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq,
1211 n->nlmsg_flags);
1212
f8653393 1213 if (netlink_send_msg(nl, n, n->nlmsg_len) == -1)
d62a17ae 1214 return -1;
d62a17ae 1215
d62a17ae 1216 /*
1217 * Get reply from netlink socket.
1218 * The reply should either be an acknowlegement or an error.
1219 */
7cdb1a84
MS
1220 return netlink_parse_info(filter, nl, dp_info, 0, startup);
1221}
1222
1223/*
1224 * Synchronous version of netlink_talk_info. Converts args to suit the
1225 * common version, which is suitable for both sync and async use.
7cdb1a84
MS
1226 */
1227int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
1228 struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns,
9bfadae8 1229 bool startup)
7cdb1a84
MS
1230{
1231 struct zebra_dplane_info dp_info;
1232
1233 /* Increment sequence number before capturing snapshot of ns socket
1234 * info.
1235 */
1236 nl->seq++;
1237
1238 /* Capture info in intermediate info struct */
85a75f1e 1239 zebra_dplane_info_from_zns(&dp_info, zns, (nl == &(zns->netlink_cmd)));
7cdb1a84 1240
5709131c 1241 return netlink_talk_info(filter, n, &dp_info, startup);
1fdc9eae 1242}
1243
289602d7 1244/* Issue request message to kernel via netlink socket. GET messages
1245 * are issued through this interface.
1246 */
fd3f8e52 1247int netlink_request(struct nlsock *nl, void *req)
1fdc9eae 1248{
fd3f8e52 1249 struct nlmsghdr *n = (struct nlmsghdr *)req;
d62a17ae 1250
1251 /* Check netlink socket. */
1252 if (nl->sock < 0) {
450971aa 1253 flog_err_sys(EC_LIB_SOCKET, "%s socket isn't active.",
09c866e3 1254 nl->name);
d62a17ae 1255 return -1;
1256 }
1257
1258 /* Fill common fields for all requests. */
d62a17ae 1259 n->nlmsg_pid = nl->snl.nl_pid;
1260 n->nlmsg_seq = ++nl->seq;
1261
f8653393 1262 if (netlink_send_msg(nl, req, n->nlmsg_len) == -1)
d62a17ae 1263 return -1;
d62a17ae 1264
1265 return 0;
1fdc9eae 1266}
1267
e63c7622
JU
1268static int nl_batch_read_resp(struct nl_batch *bth)
1269{
1270 struct nlmsghdr *h;
1271 struct sockaddr_nl snl;
9d06e121 1272 struct msghdr msg = {};
f6feb48b 1273 int status, seq;
d4000d7b 1274 struct nlsock *nl;
f6feb48b
JU
1275 struct zebra_dplane_ctx *ctx;
1276 bool ignore_msg;
e63c7622 1277
d4000d7b 1278 nl = kernel_netlink_nlsock_lookup(bth->zns->sock);
e63c7622
JU
1279
1280 msg.msg_name = (void *)&snl;
1281 msg.msg_namelen = sizeof(snl);
1282
2f9dbd3a
JU
1283 /*
1284 * The responses are not batched, so we need to read and process one
1285 * message at a time.
1286 */
1287 while (true) {
2cf7651f 1288 status = netlink_recv_msg(nl, &msg);
00249e25
DS
1289 /*
1290 * status == -1 is a full on failure somewhere
1291 * since we don't know where the problem happened
1292 * we must mark all as failed
1293 *
1294 * Else we mark everything as worked
1295 *
1296 */
1297 if (status == -1 || status == 0) {
1298 while ((ctx = dplane_ctx_dequeue(&(bth->ctx_list))) !=
1299 NULL) {
1300 if (status == -1)
1301 dplane_ctx_set_status(
1302 ctx,
1303 ZEBRA_DPLANE_REQUEST_FAILURE);
1304 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
1305 }
2f9dbd3a 1306 return status;
00249e25 1307 }
e63c7622 1308
2cf7651f 1309 h = (struct nlmsghdr *)nl->buf;
f6feb48b
JU
1310 ignore_msg = false;
1311 seq = h->nlmsg_seq;
e63c7622 1312 /*
f6feb48b
JU
1313 * Find the corresponding context object. Received responses are
1314 * in the same order as requests we sent, so we can simply
1315 * iterate over the context list and match responses with
1316 * requests at same time.
e63c7622 1317 */
f6feb48b 1318 while (true) {
c8453cd7
DS
1319 ctx = dplane_ctx_get_head(&(bth->ctx_list));
1320 if (ctx == NULL) {
1321 /*
1322 * This is a situation where we have gotten
1323 * into a bad spot. We need to know that
1324 * this happens( does it? )
1325 */
1326 zlog_err(
1327 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1328 __func__);
e63c7622 1329 break;
c8453cd7 1330 }
f6feb48b
JU
1331
1332 /*
1333 * 'update' context objects take two consecutive
1334 * sequence numbers.
1335 */
3670f504
DS
1336 if (dplane_ctx_is_update(ctx) &&
1337 dplane_ctx_get_ns(ctx)->seq + 1 == seq) {
f6feb48b
JU
1338 /*
1339 * This is the situation where we get a response
1340 * to a message that should be ignored.
1341 */
1342 ignore_msg = true;
1343 break;
1344 }
c8453cd7
DS
1345
1346 ctx = dplane_ctx_dequeue(&(bth->ctx_list));
1347 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
1348
1349 /* We have found corresponding context object. */
3670f504 1350 if (dplane_ctx_get_ns(ctx)->seq == seq)
c8453cd7
DS
1351 break;
1352
3670f504 1353 if (dplane_ctx_get_ns(ctx)->seq > seq)
c8453cd7 1354 zlog_warn(
b9d95135 1355 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
c8453cd7 1356 __func__, seq,
3670f504 1357 dplane_ctx_get_ns(ctx)->seq);
e63c7622
JU
1358 }
1359
c8453cd7
DS
1360 if (ignore_msg) {
1361 /*
1362 * If we ignore the message due to an update
1363 * above we should still fricking decode the
1364 * message for our operator to understand
1365 * what is going on
1366 */
1367 int err = netlink_parse_error(nl, h, bth->zns->is_cmd,
1368 false);
1369
1370 zlog_debug("%s: netlink error message seq=%d %d",
1371 __func__, h->nlmsg_seq, err);
f6feb48b 1372 continue;
c8453cd7 1373 }
f6feb48b 1374
e63c7622
JU
1375 /*
1376 * We received a message with the sequence number that isn't
1377 * associated with any dplane context object.
1378 */
f6feb48b 1379 if (ctx == NULL) {
4c99d413
MS
1380 if (IS_ZEBRA_DEBUG_KERNEL)
1381 zlog_debug(
1382 "%s: skipping unassociated response, seq number %d NS %u",
1383 __func__, h->nlmsg_seq,
1384 bth->zns->ns_id);
e63c7622
JU
1385 continue;
1386 }
1387
1388 if (h->nlmsg_type == NLMSG_ERROR) {
d166308b
MS
1389 int err = netlink_parse_error(nl, h, bth->zns->is_cmd,
1390 false);
e63c7622
JU
1391
1392 if (err == -1)
f6feb48b
JU
1393 dplane_ctx_set_status(
1394 ctx, ZEBRA_DPLANE_REQUEST_FAILURE);
e63c7622 1395
4c99d413
MS
1396 if (IS_ZEBRA_DEBUG_KERNEL)
1397 zlog_debug("%s: netlink error message seq=%d ",
1398 __func__, h->nlmsg_seq);
e63c7622
JU
1399 continue;
1400 }
1401
1402 /*
1403 * If we get here then we did not receive neither the ack nor
1404 * the error and instead received some other message in an
1405 * unexpected way.
1406 */
4c99d413
MS
1407 if (IS_ZEBRA_DEBUG_KERNEL)
1408 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1409 __func__, h->nlmsg_type,
1410 nl_msg_type_to_str(h->nlmsg_type),
1411 bth->zns->ns_id);
e63c7622
JU
1412 }
1413
1414 return 0;
1415}
1416
1417static void nl_batch_reset(struct nl_batch *bth)
1418{
e63c7622
JU
1419 bth->buf_head = bth->buf;
1420 bth->curlen = 0;
1421 bth->msgcnt = 0;
1422 bth->zns = NULL;
1423
f6feb48b 1424 TAILQ_INIT(&(bth->ctx_list));
e63c7622
JU
1425}
1426
f6feb48b 1427static void nl_batch_init(struct nl_batch *bth, struct dplane_ctx_q *ctx_out_q)
e63c7622 1428{
531c92b8
JU
1429 /*
1430 * If the size of the buffer has changed, free and then allocate a new
1431 * one.
1432 */
1433 size_t bufsize =
1434 atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed);
1435 if (bufsize != nl_batch_tx_bufsize) {
1436 if (nl_batch_tx_buf)
1437 XFREE(MTYPE_NL_BUF, nl_batch_tx_buf);
1438
1439 nl_batch_tx_buf = XCALLOC(MTYPE_NL_BUF, bufsize);
1440 nl_batch_tx_bufsize = bufsize;
1441 }
1442
f6feb48b 1443 bth->buf = nl_batch_tx_buf;
531c92b8
JU
1444 bth->bufsiz = bufsize;
1445 bth->limit = atomic_load_explicit(&nl_batch_send_threshold,
1446 memory_order_relaxed);
e63c7622 1447
f6feb48b 1448 bth->ctx_out_q = ctx_out_q;
e63c7622 1449
f6feb48b
JU
1450 nl_batch_reset(bth);
1451}
1452
1453static void nl_batch_send(struct nl_batch *bth)
1454{
1455 struct zebra_dplane_ctx *ctx;
1456 bool err = false;
e63c7622 1457
f6feb48b 1458 if (bth->curlen != 0 && bth->zns != NULL) {
d4000d7b
DS
1459 struct nlsock *nl =
1460 kernel_netlink_nlsock_lookup(bth->zns->sock);
1461
f6feb48b
JU
1462 if (IS_ZEBRA_DEBUG_KERNEL)
1463 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
d4000d7b 1464 __func__, nl->name, bth->curlen,
f6feb48b 1465 bth->msgcnt);
e63c7622 1466
d4000d7b 1467 if (netlink_send_msg(nl, bth->buf, bth->curlen) == -1)
e63c7622 1468 err = true;
e63c7622 1469
f6feb48b
JU
1470 if (!err) {
1471 if (nl_batch_read_resp(bth) == -1)
1472 err = true;
1473 }
1474 }
e63c7622 1475
f6feb48b
JU
1476 /* Move remaining contexts to the outbound queue. */
1477 while (true) {
1478 ctx = dplane_ctx_dequeue(&(bth->ctx_list));
1479 if (ctx == NULL)
1480 break;
e63c7622 1481
f6feb48b
JU
1482 if (err)
1483 dplane_ctx_set_status(ctx,
1484 ZEBRA_DPLANE_REQUEST_FAILURE);
e63c7622 1485
f6feb48b 1486 dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx);
e63c7622
JU
1487 }
1488
1489 nl_batch_reset(bth);
1490}
1491
e63c7622
JU
1492enum netlink_msg_status netlink_batch_add_msg(
1493 struct nl_batch *bth, struct zebra_dplane_ctx *ctx,
1494 ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t),
f6feb48b 1495 bool ignore_res)
e63c7622
JU
1496{
1497 int seq;
1498 ssize_t size;
1499 struct nlmsghdr *msgh;
d4000d7b 1500 struct nlsock *nl;
e63c7622 1501
e63c7622
JU
1502 size = (*msg_encoder)(ctx, bth->buf_head, bth->bufsiz - bth->curlen);
1503
1504 /*
1505 * If there was an error while encoding the message (other than buffer
1506 * overflow) then return an error.
1507 */
1508 if (size < 0)
1509 return FRR_NETLINK_ERROR;
1510
1511 /*
1512 * If the message doesn't fit entirely in the buffer then send the batch
1513 * and retry.
1514 */
1515 if (size == 0) {
1516 nl_batch_send(bth);
1517 size = (*msg_encoder)(ctx, bth->buf_head,
1518 bth->bufsiz - bth->curlen);
1519 /*
1520 * If the message doesn't fit in the empty buffer then just
1521 * return an error.
1522 */
1523 if (size <= 0)
1524 return FRR_NETLINK_ERROR;
1525 }
1526
3670f504 1527 seq = dplane_ctx_get_ns(ctx)->seq;
d4000d7b
DS
1528 nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
1529
f6feb48b 1530 if (ignore_res)
e63c7622
JU
1531 seq++;
1532
1533 msgh = (struct nlmsghdr *)bth->buf_head;
1534 msgh->nlmsg_seq = seq;
d4000d7b 1535 msgh->nlmsg_pid = nl->snl.nl_pid;
e63c7622 1536
e63c7622
JU
1537 bth->zns = dplane_ctx_get_ns(ctx);
1538 bth->buf_head = ((char *)bth->buf_head) + size;
1539 bth->curlen += size;
1540 bth->msgcnt++;
1541
e63c7622
JU
1542 return FRR_NETLINK_QUEUED;
1543}
1544
67e3369e
JU
1545static enum netlink_msg_status nl_put_msg(struct nl_batch *bth,
1546 struct zebra_dplane_ctx *ctx)
1547{
1548 if (dplane_ctx_is_skip_kernel(ctx))
1549 return FRR_NETLINK_SUCCESS;
1550
1551 switch (dplane_ctx_get_op(ctx)) {
1552
1553 case DPLANE_OP_ROUTE_INSTALL:
1554 case DPLANE_OP_ROUTE_UPDATE:
1555 case DPLANE_OP_ROUTE_DELETE:
1556 return netlink_put_route_update_msg(bth, ctx);
1557
1558 case DPLANE_OP_NH_INSTALL:
1559 case DPLANE_OP_NH_UPDATE:
1560 case DPLANE_OP_NH_DELETE:
1561 return netlink_put_nexthop_update_msg(bth, ctx);
1562
1563 case DPLANE_OP_LSP_INSTALL:
1564 case DPLANE_OP_LSP_UPDATE:
1565 case DPLANE_OP_LSP_DELETE:
1566 return netlink_put_lsp_update_msg(bth, ctx);
1567
1568 case DPLANE_OP_PW_INSTALL:
1569 case DPLANE_OP_PW_UNINSTALL:
1570 return netlink_put_pw_update_msg(bth, ctx);
1571
1572 case DPLANE_OP_ADDR_INSTALL:
1573 case DPLANE_OP_ADDR_UNINSTALL:
1574 return netlink_put_address_update_msg(bth, ctx);
1575
1576 case DPLANE_OP_MAC_INSTALL:
1577 case DPLANE_OP_MAC_DELETE:
1578 return netlink_put_mac_update_msg(bth, ctx);
1579
1580 case DPLANE_OP_NEIGH_INSTALL:
1581 case DPLANE_OP_NEIGH_UPDATE:
1582 case DPLANE_OP_NEIGH_DELETE:
1583 case DPLANE_OP_VTEP_ADD:
1584 case DPLANE_OP_VTEP_DELETE:
d68e74b4 1585 case DPLANE_OP_NEIGH_DISCOVER:
0a27a2fe
PG
1586 case DPLANE_OP_NEIGH_IP_INSTALL:
1587 case DPLANE_OP_NEIGH_IP_DELETE:
e18747a9 1588 case DPLANE_OP_NEIGH_TABLE_UPDATE:
67e3369e
JU
1589 return netlink_put_neigh_update_msg(bth, ctx);
1590
1591 case DPLANE_OP_RULE_ADD:
1592 case DPLANE_OP_RULE_DELETE:
1593 case DPLANE_OP_RULE_UPDATE:
1594 return netlink_put_rule_update_msg(bth, ctx);
1595
1596 case DPLANE_OP_SYS_ROUTE_ADD:
1597 case DPLANE_OP_SYS_ROUTE_DELETE:
1598 case DPLANE_OP_ROUTE_NOTIFY:
1599 case DPLANE_OP_LSP_NOTIFY:
c60522f7 1600 case DPLANE_OP_BR_PORT_UPDATE:
67e3369e
JU
1601 return FRR_NETLINK_SUCCESS;
1602
5162e000
PG
1603 case DPLANE_OP_IPTABLE_ADD:
1604 case DPLANE_OP_IPTABLE_DELETE:
ef524230
PG
1605 case DPLANE_OP_IPSET_ADD:
1606 case DPLANE_OP_IPSET_DELETE:
1607 case DPLANE_OP_IPSET_ENTRY_ADD:
1608 case DPLANE_OP_IPSET_ENTRY_DELETE:
5162e000
PG
1609 return FRR_NETLINK_ERROR;
1610
62b4b7e4
PG
1611 case DPLANE_OP_GRE_SET:
1612 return netlink_put_gre_set_msg(bth, ctx);
1613
9d59df63
MS
1614 case DPLANE_OP_INTF_ADDR_ADD:
1615 case DPLANE_OP_INTF_ADDR_DEL:
728f2017 1616 case DPLANE_OP_INTF_NETCONFIG:
67e3369e
JU
1617 case DPLANE_OP_NONE:
1618 return FRR_NETLINK_ERROR;
5d414138
SW
1619
1620 case DPLANE_OP_INTF_INSTALL:
1621 case DPLANE_OP_INTF_UPDATE:
1622 case DPLANE_OP_INTF_DELETE:
1623 return netlink_put_intf_update_msg(bth, ctx);
67e3369e
JU
1624 }
1625
1626 return FRR_NETLINK_ERROR;
1627}
1628
fef24b03
JU
1629void kernel_update_multi(struct dplane_ctx_q *ctx_list)
1630{
67e3369e
JU
1631 struct nl_batch batch;
1632 struct zebra_dplane_ctx *ctx;
1633 struct dplane_ctx_q handled_list;
1634 enum netlink_msg_status res;
1635
67e3369e 1636 TAILQ_INIT(&handled_list);
f6feb48b 1637 nl_batch_init(&batch, &handled_list);
67e3369e
JU
1638
1639 while (true) {
1640 ctx = dplane_ctx_dequeue(ctx_list);
1641 if (ctx == NULL)
1642 break;
1643
f6feb48b
JU
1644 if (batch.zns != NULL
1645 && batch.zns->ns_id != dplane_ctx_get_ns(ctx)->ns_id)
1646 nl_batch_send(&batch);
67e3369e
JU
1647
1648 /*
f6feb48b
JU
1649 * Assume all messages will succeed and then mark only the ones
1650 * that failed.
67e3369e 1651 */
f6feb48b
JU
1652 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
1653
1654 res = nl_put_msg(&batch, ctx);
1655
1656 dplane_ctx_enqueue_tail(&(batch.ctx_list), ctx);
1657 if (res == FRR_NETLINK_ERROR)
67e3369e
JU
1658 dplane_ctx_set_status(ctx,
1659 ZEBRA_DPLANE_REQUEST_FAILURE);
1660
f6feb48b
JU
1661 if (batch.curlen > batch.limit)
1662 nl_batch_send(&batch);
67e3369e
JU
1663 }
1664
1665 nl_batch_send(&batch);
1666
1667 TAILQ_INIT(ctx_list);
1668 dplane_ctx_list_append(ctx_list, &handled_list);
fef24b03
JU
1669}
1670
d4000d7b
DS
1671struct nlsock *kernel_netlink_nlsock_lookup(int sock)
1672{
34869809 1673 struct nlsock lookup, *retval;
d4000d7b
DS
1674
1675 lookup.sock = sock;
1676
34869809
MS
1677 NLSOCK_LOCK();
1678 retval = hash_lookup(nlsock_hash, &lookup);
1679 NLSOCK_UNLOCK();
1680
1681 return retval;
1682}
1683
1684/* Insert nlsock entry into hash */
1685static void kernel_netlink_nlsock_insert(struct nlsock *nls)
1686{
1687 NLSOCK_LOCK();
1688 (void)hash_get(nlsock_hash, nls, hash_alloc_intern);
1689 NLSOCK_UNLOCK();
1690}
1691
1692/* Remove nlsock entry from hash */
1693static void kernel_netlink_nlsock_remove(struct nlsock *nls)
1694{
1695 NLSOCK_LOCK();
1696 (void)hash_release(nlsock_hash, nls);
1697 NLSOCK_UNLOCK();
d4000d7b
DS
1698}
1699
1700static uint32_t kernel_netlink_nlsock_key(const void *arg)
1701{
1702 const struct nlsock *nl = arg;
1703
1704 return nl->sock;
1705}
1706
1707static bool kernel_netlink_nlsock_hash_equal(const void *arg1, const void *arg2)
1708{
1709 const struct nlsock *nl1 = arg1;
1710 const struct nlsock *nl2 = arg2;
1711
1712 if (nl1->sock == nl2->sock)
1713 return true;
1714
1715 return false;
1716}
1717
1fdc9eae 1718/* Exported interface function. This function simply calls
1719 netlink_socket (). */
d62a17ae 1720void kernel_init(struct zebra_ns *zns)
1fdc9eae 1721{
47e2eb27 1722 uint32_t groups, dplane_groups, ext_groups;
5d307d5d
DS
1723#if defined SOL_NETLINK
1724 int one, ret;
1725#endif
d62a17ae 1726
026a316f
DS
1727 /*
1728 * Initialize netlink sockets
1729 *
1730 * If RTMGRP_XXX exists use that, but at some point
1731 * I think the kernel developers realized that
1732 * keeping track of all the different values would
1733 * lead to confusion, so we need to convert the
1734 * RTNLGRP_XXX to a bit position for ourself
1735 */
1736 groups = RTMGRP_LINK |
1737 RTMGRP_IPV4_ROUTE |
1738 RTMGRP_IPV4_IFADDR |
1739 RTMGRP_IPV6_ROUTE |
1740 RTMGRP_IPV6_IFADDR |
1741 RTMGRP_IPV4_MROUTE |
1742 RTMGRP_NEIGH |
67188ca2
QY
1743 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE - 1)) |
1744 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE - 1)) |
cd787a8a 1745 ((uint32_t) 1 << (RTNLGRP_NEXTHOP - 1));
d62a17ae 1746
ceab66b7
MS
1747 dplane_groups = (RTMGRP_LINK |
1748 RTMGRP_IPV4_IFADDR |
cd787a8a
MS
1749 RTMGRP_IPV6_IFADDR |
1750 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF - 1)) |
1751 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF - 1)) |
1752 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF - 1)));
1753
47e2eb27
CS
1754 /* Use setsockopt for > 31 group */
1755 ext_groups = RTNLGRP_TUNNEL;
ceab66b7 1756
d62a17ae 1757 snprintf(zns->netlink.name, sizeof(zns->netlink.name),
1758 "netlink-listen (NS %u)", zns->ns_id);
1759 zns->netlink.sock = -1;
fe953d7c
DS
1760 if (netlink_socket(&zns->netlink, groups, &ext_groups, 1, zns->ns_id) <
1761 0) {
19d5a4fe
DS
1762 zlog_err("Failure to create %s socket",
1763 zns->netlink.name);
1764 exit(-1);
1765 }
34869809
MS
1766
1767 kernel_netlink_nlsock_insert(&zns->netlink);
d62a17ae 1768
1769 snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name),
1770 "netlink-cmd (NS %u)", zns->ns_id);
1771 zns->netlink_cmd.sock = -1;
fe953d7c 1772 if (netlink_socket(&zns->netlink_cmd, 0, 0, 0, zns->ns_id) < 0) {
19d5a4fe
DS
1773 zlog_err("Failure to create %s socket",
1774 zns->netlink_cmd.name);
1775 exit(-1);
1776 }
34869809
MS
1777
1778 kernel_netlink_nlsock_insert(&zns->netlink_cmd);
d62a17ae 1779
80dcc388
MS
1780 /* Outbound socket for dplane programming of the host OS. */
1781 snprintf(zns->netlink_dplane_out.name,
1782 sizeof(zns->netlink_dplane_out.name), "netlink-dp (NS %u)",
1783 zns->ns_id);
1784 zns->netlink_dplane_out.sock = -1;
fe953d7c 1785 if (netlink_socket(&zns->netlink_dplane_out, 0, 0, 0, zns->ns_id) < 0) {
62b8bb7a 1786 zlog_err("Failure to create %s socket",
80dcc388
MS
1787 zns->netlink_dplane_out.name);
1788 exit(-1);
1789 }
34869809
MS
1790
1791 kernel_netlink_nlsock_insert(&zns->netlink_dplane_out);
80dcc388
MS
1792
1793 /* Inbound socket for OS events coming to the dplane. */
1794 snprintf(zns->netlink_dplane_in.name,
1795 sizeof(zns->netlink_dplane_in.name), "netlink-dp-in (NS %u)",
1796 zns->ns_id);
1797 zns->netlink_dplane_in.sock = -1;
fe953d7c 1798 if (netlink_socket(&zns->netlink_dplane_in, dplane_groups, 0, 0,
ceab66b7 1799 zns->ns_id) < 0) {
80dcc388
MS
1800 zlog_err("Failure to create %s socket",
1801 zns->netlink_dplane_in.name);
62b8bb7a
MS
1802 exit(-1);
1803 }
34869809
MS
1804
1805 kernel_netlink_nlsock_insert(&zns->netlink_dplane_in);
62b8bb7a 1806
5d307d5d
DS
1807 /*
1808 * SOL_NETLINK is not available on all platforms yet
1809 * apparently. It's in bits/socket.h which I am not
1810 * sure that we want to pull into our build system.
1811 */
1812#if defined SOL_NETLINK
1813 /*
1814 * Let's tell the kernel that we want to receive extended
62b8bb7a 1815 * ACKS over our command socket(s)
5d307d5d
DS
1816 */
1817 one = 1;
1818 ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK,
1819 &one, sizeof(one));
1820
1821 if (ret < 0)
62b8bb7a
MS
1822 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1823 errno, safe_strerror(errno));
1824
1825 one = 1;
80dcc388
MS
1826 ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK,
1827 NETLINK_EXT_ACK, &one, sizeof(one));
62b8bb7a
MS
1828
1829 if (ret < 0)
1830 zlog_notice("Registration for extended dp ACK failed : %d %s",
5d307d5d 1831 errno, safe_strerror(errno));
97f85144
JU
1832
1833 /*
1834 * Trim off the payload of the original netlink message in the
1835 * acknowledgment. This option is available since Linux 4.2, so if
1836 * setsockopt fails, ignore the error.
1837 */
1838 one = 1;
80dcc388
MS
1839 ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK,
1840 NETLINK_CAP_ACK, &one, sizeof(one));
9781e6a0
DS
1841 if (ret < 0)
1842 zlog_notice(
1843 "Registration for reduced ACK packet size failed, probably running an early kernel");
5d307d5d
DS
1844#endif
1845
d62a17ae 1846 /* Register kernel socket. */
19d5a4fe 1847 if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
450971aa 1848 flog_err_sys(EC_LIB_SOCKET, "Can't set %s socket flags: %s",
09c866e3 1849 zns->netlink.name, safe_strerror(errno));
8c85e8ea
DS
1850
1851 if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0)
1852 zlog_err("Can't set %s socket error: %s(%d)",
1853 zns->netlink_cmd.name, safe_strerror(errno), errno);
19d5a4fe 1854
80dcc388 1855 if (fcntl(zns->netlink_dplane_out.sock, F_SETFL, O_NONBLOCK) < 0)
62b8bb7a 1856 zlog_err("Can't set %s socket error: %s(%d)",
80dcc388
MS
1857 zns->netlink_dplane_out.name, safe_strerror(errno),
1858 errno);
1859
1860 if (fcntl(zns->netlink_dplane_in.sock, F_SETFL, O_NONBLOCK) < 0)
1861 zlog_err("Can't set %s socket error: %s(%d)",
1862 zns->netlink_dplane_in.name, safe_strerror(errno),
1863 errno);
62b8bb7a 1864
19d5a4fe 1865 /* Set receive buffer size if it's set from command line */
9fb83b55
DS
1866 if (rcvbufsize) {
1867 netlink_recvbuf(&zns->netlink, rcvbufsize);
1868 netlink_recvbuf(&zns->netlink_cmd, rcvbufsize);
1869 netlink_recvbuf(&zns->netlink_dplane_out, rcvbufsize);
1870 netlink_recvbuf(&zns->netlink_dplane_in, rcvbufsize);
97f85144 1871 }
19d5a4fe 1872
80dcc388
MS
1873 /* Set filter for inbound sockets, to exclude events we've generated
1874 * ourselves.
1875 */
1876 netlink_install_filter(zns->netlink.sock, zns->netlink_cmd.snl.nl_pid,
1877 zns->netlink_dplane_out.snl.nl_pid);
1878
1879 netlink_install_filter(zns->netlink_dplane_in.sock,
62b8bb7a 1880 zns->netlink_cmd.snl.nl_pid,
80dcc388 1881 zns->netlink_dplane_out.snl.nl_pid);
62b8bb7a 1882
19d5a4fe
DS
1883 zns->t_netlink = NULL;
1884
3801e764 1885 thread_add_read(zrouter.master, kernel_read, zns,
19d5a4fe 1886 zns->netlink.sock, &zns->t_netlink);
d62a17ae 1887
1888 rt_netlink_init();
1fdc9eae 1889}
1890
34869809
MS
1891/* Helper to clean up an nlsock */
1892static void kernel_nlsock_fini(struct nlsock *nls)
1893{
1894 if (nls && nls->sock >= 0) {
1895 kernel_netlink_nlsock_remove(nls);
1896 close(nls->sock);
1897 nls->sock = -1;
1898 XFREE(MTYPE_NL_BUF, nls->buf);
1899 nls->buflen = 0;
1900 }
1901}
1902
62b8bb7a 1903void kernel_terminate(struct zebra_ns *zns, bool complete)
1fdc9eae 1904{
50478845 1905 thread_cancel(&zns->t_netlink);
d62a17ae 1906
34869809 1907 kernel_nlsock_fini(&zns->netlink);
d62a17ae 1908
34869809 1909 kernel_nlsock_fini(&zns->netlink_cmd);
ddfeb486 1910
34869809 1911 kernel_nlsock_fini(&zns->netlink_dplane_in);
80dcc388 1912
62b8bb7a
MS
1913 /* During zebra shutdown, we need to leave the dataplane socket
1914 * around until all work is done.
1915 */
34869809
MS
1916 if (complete)
1917 kernel_nlsock_fini(&zns->netlink_dplane_out);
1918}
d4000d7b 1919
34869809
MS
1920/*
1921 * Global init for platform-/OS-specific things
1922 */
1923void kernel_router_init(void)
1924{
1925 /* Init nlsock hash and lock */
1926 pthread_mutex_init(&nlsock_mutex, NULL);
1927 nlsock_hash = hash_create_size(8, kernel_netlink_nlsock_key,
1928 kernel_netlink_nlsock_hash_equal,
1929 "Netlink Socket Hash");
1930}
1931
1932/*
1933 * Global deinit for platform-/OS-specific things
1934 */
1935void kernel_router_terminate(void)
1936{
1937 pthread_mutex_destroy(&nlsock_mutex);
1938
1939 hash_free(nlsock_hash);
1940 nlsock_hash = NULL;
62b8bb7a 1941}
34869809 1942
ddfeb486 1943#endif /* HAVE_NETLINK */