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