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