]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_netlink.c
zebra: Add a conditional guard if zvrf lookup fail
[mirror_frr.git] / zebra / rt_netlink.c
CommitLineData
718e3744 1/* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 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 */
20
21#include <zebra.h>
ddfeb486
DL
22
23#ifdef HAVE_NETLINK
24
8ccc7e80 25#include <net/if_arp.h>
ba777396
RW
26#include <linux/lwtunnel.h>
27#include <linux/mpls_iptunnel.h>
28#include <linux/neighbour.h>
29#include <linux/rtnetlink.h>
718e3744 30
31/* Hack for GNU libc version 2. */
32#ifndef MSG_TRUNC
33#define MSG_TRUNC 0x20
34#endif /* MSG_TRUNC */
35
36#include "linklist.h"
37#include "if.h"
38#include "log.h"
39#include "prefix.h"
40#include "connected.h"
41#include "table.h"
26e2ae36 42#include "memory.h"
4a1ab8e4 43#include "zebra_memory.h"
718e3744 44#include "rib.h"
e04ab74d 45#include "thread.h"
edd7c245 46#include "privs.h"
fb018d25 47#include "nexthop.h"
78104b9b 48#include "vrf.h"
5e6a74d8 49#include "vty.h"
40c7bdb0 50#include "mpls.h"
13d60d35 51#include "vxlan.h"
718e3744 52
bf094f69 53#include "zebra/zapi_msg.h"
fe18ee2d 54#include "zebra/zebra_ns.h"
7c551956 55#include "zebra/zebra_vrf.h"
6621ca86 56#include "zebra/rt.h"
718e3744 57#include "zebra/redistribute.h"
58#include "zebra/interface.h"
59#include "zebra/debug.h"
12f6fb97 60#include "zebra/rtadv.h"
567b877d 61#include "zebra/zebra_ptm.h"
40c7bdb0 62#include "zebra/zebra_mpls.h"
1fdc9eae 63#include "zebra/kernel_netlink.h"
64#include "zebra/rt_netlink.h"
e3be0432 65#include "zebra/zebra_mroute.h"
2232a77c 66#include "zebra/zebra_vxlan.h"
364fed6b 67#include "zebra/zebra_errors.h"
e3be0432 68
40c7bdb0 69#ifndef AF_MPLS
70#define AF_MPLS 28
71#endif
72
2232a77c 73static vlanid_t filter_vlan = 0;
74
d62a17ae 75struct gw_family_t {
d7c0a89a
QY
76 uint16_t filler;
77 uint16_t family;
d62a17ae 78 union g_addr gate;
40c7bdb0 79};
80
8755598a
DS
81char ipv4_ll_buf[16] = "169.254.0.1";
82struct in_addr ipv4_ll;
83
84/*
85 * The ipv4_ll data structure is used for all 5549
86 * additions to the kernel. Let's figure out the
87 * correct value one time instead for every
88 * install/remove of a 5549 type route
89 */
d62a17ae 90void rt_netlink_init(void)
8755598a 91{
d62a17ae 92 inet_pton(AF_INET, ipv4_ll_buf, &ipv4_ll);
8755598a
DS
93}
94
23b1f334
DD
95static inline int is_selfroute(int proto)
96{
d62a17ae 97 if ((proto == RTPROT_BGP) || (proto == RTPROT_OSPF)
d4d71f11 98 || (proto == RTPROT_ZSTATIC) || (proto == RTPROT_ZEBRA)
d62a17ae 99 || (proto == RTPROT_ISIS) || (proto == RTPROT_RIPNG)
100 || (proto == RTPROT_NHRP) || (proto == RTPROT_EIGRP)
915902cb 101 || (proto == RTPROT_LDP) || (proto == RTPROT_BABEL)
0761368a 102 || (proto == RTPROT_RIP) || (proto == RTPROT_SHARP)
da82f6b4 103 || (proto == RTPROT_PBR) || (proto == RTPROT_OPENFABRIC)) {
d62a17ae 104 return 1;
105 }
106
107 return 0;
23b1f334
DD
108}
109
915902cb 110static inline int zebra2proto(int proto)
23b1f334 111{
d62a17ae 112 switch (proto) {
113 case ZEBRA_ROUTE_BABEL:
114 proto = RTPROT_BABEL;
115 break;
116 case ZEBRA_ROUTE_BGP:
117 proto = RTPROT_BGP;
118 break;
119 case ZEBRA_ROUTE_OSPF:
120 case ZEBRA_ROUTE_OSPF6:
121 proto = RTPROT_OSPF;
122 break;
123 case ZEBRA_ROUTE_STATIC:
d4d71f11 124 proto = RTPROT_ZSTATIC;
d62a17ae 125 break;
126 case ZEBRA_ROUTE_ISIS:
127 proto = RTPROT_ISIS;
128 break;
129 case ZEBRA_ROUTE_RIP:
130 proto = RTPROT_RIP;
131 break;
132 case ZEBRA_ROUTE_RIPNG:
133 proto = RTPROT_RIPNG;
134 break;
135 case ZEBRA_ROUTE_NHRP:
136 proto = RTPROT_NHRP;
137 break;
138 case ZEBRA_ROUTE_EIGRP:
139 proto = RTPROT_EIGRP;
140 break;
141 case ZEBRA_ROUTE_LDP:
142 proto = RTPROT_LDP;
143 break;
8a71d93d
DS
144 case ZEBRA_ROUTE_SHARP:
145 proto = RTPROT_SHARP;
146 break;
0761368a
DS
147 case ZEBRA_ROUTE_PBR:
148 proto = RTPROT_PBR;
149 break;
da82f6b4
CF
150 case ZEBRA_ROUTE_OPENFABRIC:
151 proto = RTPROT_OPENFABRIC;
152 break;
a56ec5c0
DS
153 case ZEBRA_ROUTE_TABLE:
154 proto = RTPROT_ZEBRA;
155 break;
d62a17ae 156 default:
0761368a
DS
157 /*
158 * When a user adds a new protocol this will show up
159 * to let them know to do something about it. This
160 * is intentionally a warn because we should see
161 * this as part of development of a new protocol
162 */
9df414fe
QY
163 zlog_debug(
164 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
165 __PRETTY_FUNCTION__, proto);
d62a17ae 166 proto = RTPROT_ZEBRA;
167 break;
168 }
169
170 return proto;
23b1f334
DD
171}
172
915902cb
DS
173static inline int proto2zebra(int proto, int family)
174{
175 switch (proto) {
176 case RTPROT_BABEL:
177 proto = ZEBRA_ROUTE_BABEL;
178 break;
179 case RTPROT_BGP:
180 proto = ZEBRA_ROUTE_BGP;
181 break;
182 case RTPROT_OSPF:
996c9314
LB
183 proto = (family == AFI_IP) ? ZEBRA_ROUTE_OSPF
184 : ZEBRA_ROUTE_OSPF6;
915902cb
DS
185 break;
186 case RTPROT_ISIS:
187 proto = ZEBRA_ROUTE_ISIS;
188 break;
189 case RTPROT_RIP:
190 proto = ZEBRA_ROUTE_RIP;
191 break;
192 case RTPROT_RIPNG:
193 proto = ZEBRA_ROUTE_RIPNG;
194 break;
195 case RTPROT_NHRP:
196 proto = ZEBRA_ROUTE_NHRP;
197 break;
198 case RTPROT_EIGRP:
199 proto = ZEBRA_ROUTE_EIGRP;
200 break;
201 case RTPROT_LDP:
202 proto = ZEBRA_ROUTE_LDP;
203 break;
204 case RTPROT_STATIC:
d4d71f11 205 case RTPROT_ZSTATIC:
915902cb
DS
206 proto = ZEBRA_ROUTE_STATIC;
207 break;
0761368a
DS
208 case RTPROT_SHARP:
209 proto = ZEBRA_ROUTE_SHARP;
210 break;
211 case RTPROT_PBR:
212 proto = ZEBRA_ROUTE_PBR;
213 break;
da82f6b4
CF
214 case RTPROT_OPENFABRIC:
215 proto = ZEBRA_ROUTE_OPENFABRIC;
216 break;
915902cb 217 default:
0761368a
DS
218 /*
219 * When a user adds a new protocol this will show up
220 * to let them know to do something about it. This
221 * is intentionally a warn because we should see
222 * this as part of development of a new protocol
223 */
9df414fe
QY
224 zlog_debug(
225 "%s: Please add this protocol(%d) to proper rt_netlink.c handling",
226 __PRETTY_FUNCTION__, proto);
915902cb
DS
227 proto = ZEBRA_ROUTE_KERNEL;
228 break;
229 }
230 return proto;
231}
232
12f6fb97
DS
233/*
234Pending: create an efficient table_id (in a tree/hash) based lookup)
235 */
d7c0a89a 236static vrf_id_t vrf_lookup_by_table(uint32_t table_id, ns_id_t ns_id)
12f6fb97 237{
d62a17ae 238 struct vrf *vrf;
239 struct zebra_vrf *zvrf;
12f6fb97 240
a2addae8 241 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
78dd30b2
PG
242 zvrf = vrf->info;
243 if (zvrf == NULL)
d62a17ae 244 continue;
78dd30b2
PG
245 /* case vrf with netns : match the netnsid */
246 if (vrf_is_backend_netns()) {
247 if (ns_id == zvrf_id(zvrf))
248 return zvrf_id(zvrf);
249 } else {
250 /* VRF is VRF_BACKEND_VRF_LITE */
251 if (zvrf->table_id != table_id)
252 continue;
253 return zvrf_id(zvrf);
254 }
d62a17ae 255 }
12f6fb97 256
d62a17ae 257 return VRF_DEFAULT;
12f6fb97
DS
258}
259
87da6a60
SW
260/**
261 * @parse_encap_mpls() - Parses encapsulated mpls attributes
262 * @tb: Pointer to rtattr to look for nested items in.
263 * @labels: Pointer to store labels in.
264 *
265 * Return: Number of mpls labels found.
266 */
267static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels)
268{
269 struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0};
270 mpls_lse_t *lses = NULL;
271 int num_labels = 0;
272 uint32_t ttl = 0;
273 uint32_t bos = 0;
274 uint32_t exp = 0;
275 mpls_label_t label = 0;
276
277 netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb);
278 lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]);
279 while (!bos && num_labels < MPLS_MAX_LABELS) {
280 mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos);
281 labels[num_labels++] = label;
282 }
283
284 return num_labels;
285}
286
718e3744 287/* Looking up routing table by netlink interface. */
2414abd3 288static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
d62a17ae 289 int startup)
718e3744 290{
d62a17ae 291 int len;
292 struct rtmsg *rtm;
293 struct rtattr *tb[RTA_MAX + 1];
d7c0a89a 294 uint8_t flags = 0;
d62a17ae 295 struct prefix p;
792fa92e 296 struct prefix_ipv6 src_p = {};
78dd30b2 297 vrf_id_t vrf_id;
d62a17ae 298
299 char anyaddr[16] = {0};
300
915902cb 301 int proto = ZEBRA_ROUTE_KERNEL;
d62a17ae 302 int index = 0;
303 int table;
304 int metric = 0;
d7c0a89a 305 uint32_t mtu = 0;
25715c7e 306 uint8_t distance = 0;
4e40b6d6 307 route_tag_t tag = 0;
d62a17ae 308
309 void *dest = NULL;
310 void *gate = NULL;
311 void *prefsrc = NULL; /* IPv4 preferred source host address */
312 void *src = NULL; /* IPv6 srcdest source prefix */
e655a03c 313 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 314
87da6a60
SW
315 /* MPLS labels */
316 mpls_label_t labels[MPLS_MAX_LABELS] = {0};
317 int num_labels = 0;
318
d62a17ae 319 rtm = NLMSG_DATA(h);
320
321 if (startup && h->nlmsg_type != RTM_NEWROUTE)
322 return 0;
e655a03c
DL
323 switch (rtm->rtm_type) {
324 case RTN_UNICAST:
325 break;
326 case RTN_BLACKHOLE:
327 bh_type = BLACKHOLE_NULL;
328 break;
329 case RTN_UNREACHABLE:
330 bh_type = BLACKHOLE_REJECT;
331 break;
332 case RTN_PROHIBIT:
333 bh_type = BLACKHOLE_ADMINPROHIB;
334 break;
335 default:
8c8f250b
DS
336 if (IS_ZEBRA_DEBUG_KERNEL)
337 zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
338 nl_rttype_to_str(rtm->rtm_type),
339 rtm->rtm_type);
d62a17ae 340 return 0;
e655a03c 341 }
d62a17ae 342
343 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
9bdf8618
DS
344 if (len < 0) {
345 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
346 __PRETTY_FUNCTION__, h->nlmsg_len,
347 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
d62a17ae 348 return -1;
9bdf8618 349 }
d62a17ae 350
351 memset(tb, 0, sizeof tb);
352 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
353
354 if (rtm->rtm_flags & RTM_F_CLONED)
355 return 0;
356 if (rtm->rtm_protocol == RTPROT_REDIRECT)
357 return 0;
358 if (rtm->rtm_protocol == RTPROT_KERNEL)
359 return 0;
360
361 if (!startup && is_selfroute(rtm->rtm_protocol)
6ab5222f
DS
362 && h->nlmsg_type == RTM_NEWROUTE) {
363 if (IS_ZEBRA_DEBUG_KERNEL)
364 zlog_debug("Route type: %d Received that we think we have originated, ignoring",
365 rtm->rtm_protocol);
d62a17ae 366 return 0;
6ab5222f 367 }
d62a17ae 368
369 /* We don't care about change notifications for the MPLS table. */
370 /* TODO: Revisit this. */
371 if (rtm->rtm_family == AF_MPLS)
372 return 0;
373
374 /* Table corresponding to route. */
375 if (tb[RTA_TABLE])
376 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
377 else
378 table = rtm->rtm_table;
379
380 /* Map to VRF */
78dd30b2 381 vrf_id = vrf_lookup_by_table(table, ns_id);
d62a17ae 382 if (vrf_id == VRF_DEFAULT) {
383 if (!is_zebra_valid_kernel_table(table)
384 && !is_zebra_main_routing_table(table))
385 return 0;
386 }
387
388 /* Route which inserted by Zebra. */
915902cb 389 if (is_selfroute(rtm->rtm_protocol)) {
d62a17ae 390 flags |= ZEBRA_FLAG_SELFROUTE;
915902cb
DS
391 proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family);
392 }
d62a17ae 393 if (tb[RTA_OIF])
394 index = *(int *)RTA_DATA(tb[RTA_OIF]);
395
396 if (tb[RTA_DST])
397 dest = RTA_DATA(tb[RTA_DST]);
398 else
399 dest = anyaddr;
400
401 if (tb[RTA_SRC])
402 src = RTA_DATA(tb[RTA_SRC]);
403 else
404 src = anyaddr;
405
406 if (tb[RTA_PREFSRC])
407 prefsrc = RTA_DATA(tb[RTA_PREFSRC]);
408
409 if (tb[RTA_GATEWAY])
410 gate = RTA_DATA(tb[RTA_GATEWAY]);
411
f19435a8
DS
412 if (tb[RTA_PRIORITY])
413 metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]);
d62a17ae 414
4e40b6d6
KK
415#if defined(SUPPORT_REALMS)
416 if (tb[RTA_FLOW])
417 tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]);
418#endif
419
f19435a8
DS
420 if (tb[RTA_METRICS]) {
421 struct rtattr *mxrta[RTAX_MAX + 1];
d62a17ae 422
f19435a8 423 memset(mxrta, 0, sizeof mxrta);
996c9314 424 netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
f19435a8 425 RTA_PAYLOAD(tb[RTA_METRICS]));
d62a17ae 426
f19435a8 427 if (mxrta[RTAX_MTU])
d7c0a89a 428 mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]);
d62a17ae 429 }
430
431 if (rtm->rtm_family == AF_INET) {
432 p.family = AF_INET;
930571d2 433 if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) {
e17d9b2d 434 zlog_err(
75829703 435 "Invalid destination prefix length: %u received from kernel route change",
930571d2 436 rtm->rtm_dst_len);
e17d9b2d 437 return -1;
930571d2 438 }
d62a17ae 439 memcpy(&p.u.prefix4, dest, 4);
440 p.prefixlen = rtm->rtm_dst_len;
441
1f610a1f
CF
442 if (rtm->rtm_src_len != 0) {
443 char buf[PREFIX_STRLEN];
9df414fe 444 flog_warn(
e914ccbe 445 EC_ZEBRA_UNSUPPORTED_V4_SRCDEST,
9df414fe
QY
446 "unsupported IPv4 sourcedest route (dest %s vrf %u)",
447 prefix2str(&p, buf, sizeof(buf)), vrf_id);
1f610a1f
CF
448 return 0;
449 }
930571d2 450
1f610a1f
CF
451 /* Force debug below to not display anything for source */
452 src_p.prefixlen = 0;
d62a17ae 453 } else if (rtm->rtm_family == AF_INET6) {
454 p.family = AF_INET6;
930571d2 455 if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) {
e17d9b2d 456 zlog_err(
75829703 457 "Invalid destination prefix length: %u received from kernel route change",
930571d2 458 rtm->rtm_dst_len);
e17d9b2d 459 return -1;
930571d2 460 }
d62a17ae 461 memcpy(&p.u.prefix6, dest, 16);
462 p.prefixlen = rtm->rtm_dst_len;
463
464 src_p.family = AF_INET6;
930571d2 465 if (rtm->rtm_src_len > IPV6_MAX_BITLEN) {
e17d9b2d 466 zlog_err(
75829703 467 "Invalid source prefix length: %u received from kernel route change",
930571d2 468 rtm->rtm_src_len);
e17d9b2d 469 return -1;
930571d2 470 }
d62a17ae 471 memcpy(&src_p.prefix, src, 16);
472 src_p.prefixlen = rtm->rtm_src_len;
473 }
474
25715c7e
DS
475 /*
476 * For ZEBRA_ROUTE_KERNEL types:
477 *
478 * The metric/priority of the route received from the kernel
479 * is a 32 bit number. We are going to interpret the high
480 * order byte as the Admin Distance and the low order 3 bytes
481 * as the metric.
482 *
483 * This will allow us to do two things:
484 * 1) Allow the creation of kernel routes that can be
485 * overridden by zebra.
486 * 2) Allow the old behavior for 'most' kernel route types
487 * if a user enters 'ip route ...' v4 routes get a metric
488 * of 0 and v6 routes get a metric of 1024. Both of these
489 * values will end up with a admin distance of 0, which
490 * will cause them to win for the purposes of zebra.
491 */
492 if (proto == ZEBRA_ROUTE_KERNEL) {
493 distance = (metric >> 24) & 0xFF;
996c9314 494 metric = (metric & 0x00FFFFFF);
25715c7e
DS
495 }
496
d62a17ae 497 if (IS_ZEBRA_DEBUG_KERNEL) {
498 char buf[PREFIX_STRLEN];
499 char buf2[PREFIX_STRLEN];
45df4e96 500 zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d",
996c9314
LB
501 nl_msg_type_to_str(h->nlmsg_type),
502 prefix2str(&p, buf, sizeof(buf)),
503 src_p.prefixlen ? " from " : "",
504 src_p.prefixlen
505 ? prefix2str(&src_p, buf2, sizeof(buf2))
506 : "",
45df4e96 507 vrf_id, table, metric, distance);
d62a17ae 508 }
509
510 afi_t afi = AFI_IP;
511 if (rtm->rtm_family == AF_INET6)
512 afi = AFI_IP6;
513
514 if (h->nlmsg_type == RTM_NEWROUTE) {
8795f904
DS
515 struct interface *ifp;
516 vrf_id_t nh_vrf_id = vrf_id;
517
fd36be7e
DL
518 if (!tb[RTA_MULTIPATH]) {
519 struct nexthop nh;
520 size_t sz = (afi == AFI_IP) ? 4 : 16;
521
522 memset(&nh, 0, sizeof(nh));
af760ec1
DS
523
524 if (bh_type == BLACKHOLE_UNSPEC) {
525 if (index && !gate)
526 nh.type = NEXTHOP_TYPE_IFINDEX;
527 else if (index && gate)
996c9314
LB
528 nh.type =
529 (afi == AFI_IP)
530 ? NEXTHOP_TYPE_IPV4_IFINDEX
531 : NEXTHOP_TYPE_IPV6_IFINDEX;
af760ec1 532 else if (!index && gate)
1dca2eaa
RW
533 nh.type = (afi == AFI_IP)
534 ? NEXTHOP_TYPE_IPV4
535 : NEXTHOP_TYPE_IPV6;
af760ec1
DS
536 else {
537 nh.type = NEXTHOP_TYPE_BLACKHOLE;
538 nh.bh_type = bh_type;
539 }
540 } else {
fd36be7e 541 nh.type = NEXTHOP_TYPE_BLACKHOLE;
e655a03c
DL
542 nh.bh_type = bh_type;
543 }
fd36be7e
DL
544 nh.ifindex = index;
545 if (prefsrc)
546 memcpy(&nh.src, prefsrc, sz);
547 if (gate)
548 memcpy(&nh.gate, gate, sz);
915902cb 549
8795f904 550 if (index) {
fac4d51e
PG
551 ifp = if_lookup_by_index_per_ns(
552 zebra_ns_lookup(ns_id),
553 index);
8795f904
DS
554 if (ifp)
555 nh_vrf_id = ifp->vrf_id;
556 }
4a7371e9 557 nh.vrf_id = nh_vrf_id;
8795f904 558
87da6a60
SW
559 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
560 && *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
561 == LWTUNNEL_ENCAP_MPLS) {
562 num_labels =
563 parse_encap_mpls(tb[RTA_ENCAP], labels);
564 }
565
6cae47ed
DS
566 if (rtm->rtm_flags & RTNH_F_ONLINK)
567 SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
568
87da6a60
SW
569 if (num_labels)
570 nexthop_add_labels(&nh, ZEBRA_LSP_STATIC,
571 num_labels, labels);
572
4a7371e9 573 rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p,
1f610a1f 574 &src_p, &nh, table, metric, mtu, distance, tag);
fd36be7e 575 } else {
d62a17ae 576 /* This is a multipath route */
577
578 struct route_entry *re;
579 struct rtnexthop *rtnh =
580 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
581
582 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
583
584 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
915902cb 585 re->type = proto;
25715c7e 586 re->distance = distance;
d62a17ae 587 re->flags = flags;
588 re->metric = metric;
589 re->mtu = mtu;
590 re->vrf_id = vrf_id;
591 re->table = table;
592 re->nexthop_num = 0;
98572489 593 re->uptime = monotime(NULL);
4e40b6d6 594 re->tag = tag;
d62a17ae 595
596 for (;;) {
87da6a60 597 struct nexthop *nh = NULL;
c683bd44 598
d62a17ae 599 if (len < (int)sizeof(*rtnh)
600 || rtnh->rtnh_len > len)
601 break;
602
603 index = rtnh->rtnh_ifindex;
8795f904
DS
604 if (index) {
605 /*
606 * Yes we are looking this up
607 * for every nexthop and just
608 * using the last one looked
609 * up right now
610 */
fac4d51e
PG
611 ifp = if_lookup_by_index_per_ns(
612 zebra_ns_lookup(ns_id),
613 index);
8795f904 614 if (ifp)
4a7371e9
DS
615 nh_vrf_id = ifp->vrf_id;
616 else {
9df414fe 617 flog_warn(
e914ccbe 618 EC_ZEBRA_UNKNOWN_INTERFACE,
4a7371e9
DS
619 "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
620 __PRETTY_FUNCTION__,
621 index);
622 nh_vrf_id = VRF_DEFAULT;
623 }
624 } else
625 nh_vrf_id = vrf_id;
626
d62a17ae 627 gate = 0;
628 if (rtnh->rtnh_len > sizeof(*rtnh)) {
629 memset(tb, 0, sizeof(tb));
630 netlink_parse_rtattr(
631 tb, RTA_MAX, RTNH_DATA(rtnh),
632 rtnh->rtnh_len - sizeof(*rtnh));
633 if (tb[RTA_GATEWAY])
634 gate = RTA_DATA(
635 tb[RTA_GATEWAY]);
87da6a60
SW
636 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
637 && *(uint16_t *)RTA_DATA(
638 tb[RTA_ENCAP_TYPE])
639 == LWTUNNEL_ENCAP_MPLS) {
640 num_labels = parse_encap_mpls(
641 tb[RTA_ENCAP], labels);
642 }
d62a17ae 643 }
644
645 if (gate) {
646 if (rtm->rtm_family == AF_INET) {
647 if (index)
87da6a60 648 nh = route_entry_nexthop_ipv4_ifindex_add(
d62a17ae 649 re, gate,
4a7371e9
DS
650 prefsrc, index,
651 nh_vrf_id);
d62a17ae 652 else
87da6a60 653 nh = route_entry_nexthop_ipv4_add(
d62a17ae 654 re, gate,
4a7371e9
DS
655 prefsrc,
656 nh_vrf_id);
d62a17ae 657 } else if (rtm->rtm_family
658 == AF_INET6) {
659 if (index)
87da6a60 660 nh = route_entry_nexthop_ipv6_ifindex_add(
4a7371e9
DS
661 re, gate, index,
662 nh_vrf_id);
d62a17ae 663 else
87da6a60 664 nh = route_entry_nexthop_ipv6_add(
4a7371e9
DS
665 re, gate,
666 nh_vrf_id);
d62a17ae 667 }
668 } else
87da6a60 669 nh = route_entry_nexthop_ifindex_add(
4a7371e9 670 re, index, nh_vrf_id);
d62a17ae 671
87da6a60
SW
672 if (nh && num_labels)
673 nexthop_add_labels(nh, ZEBRA_LSP_STATIC,
674 num_labels, labels);
675
6cae47ed
DS
676 if (nh && (rtnh->rtnh_flags & RTNH_F_ONLINK))
677 SET_FLAG(nh->flags,
678 NEXTHOP_FLAG_ONLINK);
679
3c04071d
SW
680 if (rtnh->rtnh_len == 0)
681 break;
682
d62a17ae 683 len -= NLMSG_ALIGN(rtnh->rtnh_len);
684 rtnh = RTNH_NEXT(rtnh);
685 }
686
687 zserv_nexthop_num_warn(__func__,
688 (const struct prefix *)&p,
689 re->nexthop_num);
690 if (re->nexthop_num == 0)
691 XFREE(MTYPE_RE, re);
692 else
1f610a1f
CF
693 rib_add_multipath(afi, SAFI_UNICAST, &p,
694 &src_p, re);
d62a17ae 695 }
696 } else {
fd36be7e
DL
697 if (!tb[RTA_MULTIPATH]) {
698 struct nexthop nh;
699 size_t sz = (afi == AFI_IP) ? 4 : 16;
700
701 memset(&nh, 0, sizeof(nh));
8ba5bd58
RW
702 if (bh_type == BLACKHOLE_UNSPEC) {
703 if (index && !gate)
704 nh.type = NEXTHOP_TYPE_IFINDEX;
705 else if (index && gate)
706 nh.type =
707 (afi == AFI_IP)
708 ? NEXTHOP_TYPE_IPV4_IFINDEX
709 : NEXTHOP_TYPE_IPV6_IFINDEX;
710 else if (!index && gate)
711 nh.type = (afi == AFI_IP)
712 ? NEXTHOP_TYPE_IPV4
60466a63 713 : NEXTHOP_TYPE_IPV6;
8ba5bd58
RW
714 else {
715 nh.type = NEXTHOP_TYPE_BLACKHOLE;
716 nh.bh_type = BLACKHOLE_UNSPEC;
717 }
718 } else {
fd36be7e 719 nh.type = NEXTHOP_TYPE_BLACKHOLE;
8ba5bd58
RW
720 nh.bh_type = bh_type;
721 }
fd36be7e
DL
722 nh.ifindex = index;
723 if (gate)
724 memcpy(&nh.gate, gate, sz);
996c9314 725 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
51c4ed0a
DS
726 &p, &src_p, &nh, table, metric, distance,
727 true);
fd36be7e
DL
728 } else {
729 /* XXX: need to compare the entire list of nexthops
730 * here for NLM_F_APPEND stupidity */
996c9314 731 rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
51c4ed0a
DS
732 &p, &src_p, NULL, table, metric, distance,
733 true);
d62a17ae 734 }
735 }
736
737 return 0;
718e3744 738}
739
e3be0432
DS
740static struct mcast_route_data *mroute = NULL;
741
2414abd3 742static int netlink_route_change_read_multicast(struct nlmsghdr *h,
d62a17ae 743 ns_id_t ns_id, int startup)
565fdc75 744{
d62a17ae 745 int len;
746 struct rtmsg *rtm;
747 struct rtattr *tb[RTA_MAX + 1];
748 struct mcast_route_data *m;
749 struct mcast_route_data mr;
750 int iif = 0;
751 int count;
752 int oif[256];
753 int oif_count = 0;
754 char sbuf[40];
755 char gbuf[40];
756 char oif_list[256] = "\0";
78dd30b2 757 vrf_id_t vrf;
43b5cc5e 758 int table;
d62a17ae 759
760 if (mroute)
761 m = mroute;
762 else {
763 memset(&mr, 0, sizeof(mr));
764 m = &mr;
765 }
766
767 rtm = NLMSG_DATA(h);
768
769 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
770
771 memset(tb, 0, sizeof tb);
772 netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
90d82769 773
43b5cc5e
DS
774 if (tb[RTA_TABLE])
775 table = *(int *)RTA_DATA(tb[RTA_TABLE]);
776 else
777 table = rtm->rtm_table;
778
78dd30b2 779 vrf = vrf_lookup_by_table(table, ns_id);
43b5cc5e 780
d62a17ae 781 if (tb[RTA_IIF])
782 iif = *(int *)RTA_DATA(tb[RTA_IIF]);
783
784 if (tb[RTA_SRC])
bd8b9272 785 m->sg.src = *(struct in_addr *)RTA_DATA(tb[RTA_SRC]);
d62a17ae 786
787 if (tb[RTA_DST])
bd8b9272 788 m->sg.grp = *(struct in_addr *)RTA_DATA(tb[RTA_DST]);
d62a17ae 789
62819462 790 if (tb[RTA_EXPIRES])
d62a17ae 791 m->lastused = *(unsigned long long *)RTA_DATA(tb[RTA_EXPIRES]);
792
793 if (tb[RTA_MULTIPATH]) {
794 struct rtnexthop *rtnh =
795 (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
796
797 len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
798 for (;;) {
799 if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len)
800 break;
801
802 oif[oif_count] = rtnh->rtnh_ifindex;
803 oif_count++;
804
3c04071d
SW
805 if (rtnh->rtnh_len == 0)
806 break;
807
d62a17ae 808 len -= NLMSG_ALIGN(rtnh->rtnh_len);
809 rtnh = RTNH_NEXT(rtnh);
810 }
811 }
812
813 if (IS_ZEBRA_DEBUG_KERNEL) {
822c9af2
SW
814 struct interface *ifp = NULL;
815 struct zebra_vrf *zvrf = NULL;
816
0af35d90
RW
817 strlcpy(sbuf, inet_ntoa(m->sg.src), sizeof(sbuf));
818 strlcpy(gbuf, inet_ntoa(m->sg.grp), sizeof(gbuf));
d62a17ae 819 for (count = 0; count < oif_count; count++) {
820 ifp = if_lookup_by_index(oif[count], vrf);
821 char temp[256];
822
5b4256ca
DS
823 sprintf(temp, "%s(%d) ", ifp ? ifp->name : "Unknown",
824 oif[count]);
eab4a5c2 825 strlcat(oif_list, temp, sizeof(oif_list));
d62a17ae 826 }
822c9af2 827 zvrf = zebra_vrf_lookup_by_id(vrf);
d62a17ae 828 ifp = if_lookup_by_index(iif, vrf);
822c9af2
SW
829 zlog_debug(
830 "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
831 (zvrf ? zvrf->vrf->name : "Unknown"), vrf,
832 nl_msg_type_to_str(h->nlmsg_type), sbuf, gbuf,
833 ifp ? ifp->name : "Unknown", iif, oif_list,
834 m->lastused);
90d82769 835 }
d62a17ae 836 return 0;
565fdc75
DS
837}
838
2414abd3 839int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
565fdc75 840{
d62a17ae 841 int len;
d62a17ae 842 struct rtmsg *rtm;
843
844 rtm = NLMSG_DATA(h);
845
846 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) {
847 /* If this is not route add/delete message print warning. */
9165c5f5 848 zlog_debug("Kernel message: %s NS %u",
87b5d1b0 849 nl_msg_type_to_str(h->nlmsg_type), ns_id);
d62a17ae 850 return 0;
851 }
852
c25e2f1a
DS
853 if (!(rtm->rtm_family == AF_INET ||
854 rtm->rtm_family == AF_INET6 ||
855 rtm->rtm_family == RTNL_FAMILY_IPMR )) {
9df414fe 856 flog_warn(
e914ccbe 857 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
858 "Invalid address family: %u received from kernel route change: %s",
859 rtm->rtm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
860 return 0;
861 }
862
d62a17ae 863 /* Connected route. */
864 if (IS_ZEBRA_DEBUG_KERNEL)
78dd30b2 865 zlog_debug("%s %s %s proto %s NS %u",
d62a17ae 866 nl_msg_type_to_str(h->nlmsg_type),
867 nl_family_to_str(rtm->rtm_family),
868 nl_rttype_to_str(rtm->rtm_type),
78dd30b2 869 nl_rtproto_to_str(rtm->rtm_protocol), ns_id);
d62a17ae 870
d62a17ae 871
872 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
9bdf8618
DS
873 if (len < 0) {
874 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
875 __PRETTY_FUNCTION__,
876 h->nlmsg_len,
877 (size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
d62a17ae 878 return -1;
9bdf8618 879 }
d62a17ae 880
e655a03c 881 if (rtm->rtm_type == RTN_MULTICAST)
2414abd3 882 netlink_route_change_read_multicast(h, ns_id, startup);
e655a03c 883 else
2414abd3 884 netlink_route_change_read_unicast(h, ns_id, startup);
d62a17ae 885 return 0;
565fdc75
DS
886}
887
289602d7 888/* Request for specific route information from the kernel */
d62a17ae 889static int netlink_request_route(struct zebra_ns *zns, int family, int type)
289602d7 890{
d62a17ae 891 struct {
892 struct nlmsghdr n;
893 struct rtmsg rtm;
894 } req;
895
896 /* Form the request, specifying filter (rtattr) if needed. */
897 memset(&req, 0, sizeof(req));
898 req.n.nlmsg_type = type;
718f9b0f 899 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 900 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
901 req.rtm.rtm_family = family;
902
903 return netlink_request(&zns->netlink_cmd, &req.n);
289602d7 904}
905
718e3744 906/* Routing table read function using netlink interface. Only called
907 bootstrap time. */
d62a17ae 908int netlink_route_read(struct zebra_ns *zns)
718e3744 909{
d62a17ae 910 int ret;
85a75f1e
MS
911 struct zebra_dplane_info dp_info;
912
913 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 914
915 /* Get IPv4 routing table. */
916 ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE);
917 if (ret < 0)
918 return ret;
919 ret = netlink_parse_info(netlink_route_change_read_unicast,
85a75f1e 920 &zns->netlink_cmd, &dp_info, 0, 1);
d62a17ae 921 if (ret < 0)
922 return ret;
923
924 /* Get IPv6 routing table. */
925 ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE);
926 if (ret < 0)
927 return ret;
928 ret = netlink_parse_info(netlink_route_change_read_unicast,
85a75f1e 929 &zns->netlink_cmd, &dp_info, 0, 1);
d62a17ae 930 if (ret < 0)
931 return ret;
932
933 return 0;
718e3744 934}
935
d7c0a89a
QY
936static void _netlink_route_nl_add_gateway_info(uint8_t route_family,
937 uint8_t gw_family,
d62a17ae 938 struct nlmsghdr *nlmsg,
939 size_t req_size, int bytelen,
81793ac1 940 const struct nexthop *nexthop)
40c7bdb0 941{
d62a17ae 942 if (route_family == AF_MPLS) {
943 struct gw_family_t gw_fam;
944
945 gw_fam.family = gw_family;
946 if (gw_family == AF_INET)
947 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
948 else
949 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
950 addattr_l(nlmsg, req_size, RTA_VIA, &gw_fam.family,
951 bytelen + 2);
952 } else {
953 if (gw_family == AF_INET)
954 addattr_l(nlmsg, req_size, RTA_GATEWAY,
955 &nexthop->gate.ipv4, bytelen);
956 else
957 addattr_l(nlmsg, req_size, RTA_GATEWAY,
958 &nexthop->gate.ipv6, bytelen);
959 }
40c7bdb0 960}
961
d7c0a89a
QY
962static void _netlink_route_rta_add_gateway_info(uint8_t route_family,
963 uint8_t gw_family,
d62a17ae 964 struct rtattr *rta,
965 struct rtnexthop *rtnh,
966 size_t req_size, int bytelen,
81793ac1 967 const struct nexthop *nexthop)
40c7bdb0 968{
d62a17ae 969 if (route_family == AF_MPLS) {
970 struct gw_family_t gw_fam;
971
972 gw_fam.family = gw_family;
973 if (gw_family == AF_INET)
974 memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen);
975 else
976 memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen);
977 rta_addattr_l(rta, req_size, RTA_VIA, &gw_fam.family,
978 bytelen + 2);
979 rtnh->rtnh_len += RTA_LENGTH(bytelen + 2);
980 } else {
981 if (gw_family == AF_INET)
982 rta_addattr_l(rta, req_size, RTA_GATEWAY,
983 &nexthop->gate.ipv4, bytelen);
984 else
985 rta_addattr_l(rta, req_size, RTA_GATEWAY,
986 &nexthop->gate.ipv6, bytelen);
987 rtnh->rtnh_len += sizeof(struct rtattr) + bytelen;
988 }
40c7bdb0 989}
990
fa713d9e
CF
991/* This function takes a nexthop as argument and adds
992 * the appropriate netlink attributes to an existing
993 * netlink message.
994 *
995 * @param routedesc: Human readable description of route type
996 * (direct/recursive, single-/multipath)
997 * @param bytelen: Length of addresses in bytes.
998 * @param nexthop: Nexthop information
999 * @param nlmsg: nlmsghdr structure to fill in.
1000 * @param req_size: The size allocated for the message.
1001 */
d62a17ae 1002static void _netlink_route_build_singlepath(const char *routedesc, int bytelen,
81793ac1 1003 const struct nexthop *nexthop,
d62a17ae 1004 struct nlmsghdr *nlmsg,
1005 struct rtmsg *rtmsg,
1006 size_t req_size, int cmd)
fa713d9e 1007{
8ecdb26e 1008 struct mpls_label_stack *nh_label;
d62a17ae 1009 mpls_lse_t out_lse[MPLS_MAX_LABELS];
fa712963 1010 int num_labels = 0;
9a62e84b 1011 char label_buf[256];
d62a17ae 1012
1013 /*
1014 * label_buf is *only* currently used within debugging.
1015 * As such when we assign it we are guarding it inside
1016 * a debug test. If you want to change this make sure
1017 * you fix this assumption
1018 */
1019 label_buf[0] = '\0';
d62a17ae 1020
fa712963 1021 assert(nexthop);
81793ac1 1022 for (const struct nexthop *nh = nexthop; nh; nh = nh->rparent) {
d62a17ae 1023 char label_buf1[20];
1024
fa712963
RW
1025 nh_label = nh->nh_label;
1026 if (!nh_label || !nh_label->num_labels)
1027 continue;
1028
1029 for (int i = 0; i < nh_label->num_labels; i++) {
1030 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1031 continue;
1032
1033 if (IS_ZEBRA_DEBUG_KERNEL) {
1034 if (!num_labels)
1035 sprintf(label_buf, "label %u",
1036 nh_label->label[i]);
1037 else {
1038 sprintf(label_buf1, "/%u",
1039 nh_label->label[i]);
1040 strlcat(label_buf, label_buf1,
1041 sizeof(label_buf));
d62a17ae 1042 }
d62a17ae 1043 }
fa712963
RW
1044
1045 out_lse[num_labels] =
1046 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1047 num_labels++;
d62a17ae 1048 }
fa712963
RW
1049 }
1050
1051 if (num_labels) {
1052 /* Set the BoS bit */
1053 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1054
1055 if (rtmsg->rtm_family == AF_MPLS)
1056 addattr_l(nlmsg, req_size, RTA_NEWDST, &out_lse,
1057 num_labels * sizeof(mpls_lse_t));
1058 else {
1059 struct rtattr *nest;
d7c0a89a 1060 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
fa712963
RW
1061
1062 addattr_l(nlmsg, req_size, RTA_ENCAP_TYPE, &encap,
d7c0a89a 1063 sizeof(uint16_t));
fa712963
RW
1064 nest = addattr_nest(nlmsg, req_size, RTA_ENCAP);
1065 addattr_l(nlmsg, req_size, MPLS_IPTUNNEL_DST, &out_lse,
1066 num_labels * sizeof(mpls_lse_t));
1067 addattr_nest_end(nlmsg, nest);
66d42727 1068 }
0aabccc0 1069 }
fa713d9e 1070
d62a17ae 1071 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1072 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1073
1074 if (rtmsg->rtm_family == AF_INET
1075 && (nexthop->type == NEXTHOP_TYPE_IPV6
1076 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) {
1077 rtmsg->rtm_flags |= RTNH_F_ONLINK;
1078 addattr_l(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4);
1079 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1080
1081 if (nexthop->rmap_src.ipv4.s_addr && (cmd == RTM_NEWROUTE))
1082 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1083 &nexthop->rmap_src.ipv4, bytelen);
1084 else if (nexthop->src.ipv4.s_addr && (cmd == RTM_NEWROUTE))
1085 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1086 &nexthop->src.ipv4, bytelen);
1087
1088 if (IS_ZEBRA_DEBUG_KERNEL)
1089 zlog_debug(
1090 " 5549: _netlink_route_build_singlepath() (%s): "
7556c3fd 1091 "nexthop via %s %s if %u(%u)",
d62a17ae 1092 routedesc, ipv4_ll_buf, label_buf,
7556c3fd 1093 nexthop->ifindex, nexthop->vrf_id);
d62a17ae 1094 return;
0aabccc0
DD
1095 }
1096
d62a17ae 1097 if (nexthop->type == NEXTHOP_TYPE_IPV4
1098 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1099 /* Send deletes to the kernel without specifying the next-hop */
1100 if (cmd != RTM_DELROUTE)
1101 _netlink_route_nl_add_gateway_info(
1102 rtmsg->rtm_family, AF_INET, nlmsg, req_size,
1103 bytelen, nexthop);
1104
1105 if (cmd == RTM_NEWROUTE) {
1106 if (nexthop->rmap_src.ipv4.s_addr)
1107 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1108 &nexthop->rmap_src.ipv4, bytelen);
1109 else if (nexthop->src.ipv4.s_addr)
1110 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1111 &nexthop->src.ipv4, bytelen);
1112 }
1113
1114 if (IS_ZEBRA_DEBUG_KERNEL)
1115 zlog_debug(
1116 "netlink_route_multipath() (%s): "
7556c3fd 1117 "nexthop via %s %s if %u(%u)",
d62a17ae 1118 routedesc, inet_ntoa(nexthop->gate.ipv4),
7556c3fd 1119 label_buf, nexthop->ifindex, nexthop->vrf_id);
0aabccc0 1120 }
fa713d9e 1121
d62a17ae 1122 if (nexthop->type == NEXTHOP_TYPE_IPV6
1123 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1124 _netlink_route_nl_add_gateway_info(rtmsg->rtm_family, AF_INET6,
1125 nlmsg, req_size, bytelen,
1126 nexthop);
1127
1128 if (cmd == RTM_NEWROUTE) {
1129 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1130 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1131 &nexthop->rmap_src.ipv6, bytelen);
1132 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1133 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1134 &nexthop->src.ipv6, bytelen);
1135 }
fa713d9e 1136
d62a17ae 1137 if (IS_ZEBRA_DEBUG_KERNEL)
1138 zlog_debug(
1139 "netlink_route_multipath() (%s): "
7556c3fd 1140 "nexthop via %s %s if %u(%u)",
d62a17ae 1141 routedesc, inet6_ntoa(nexthop->gate.ipv6),
7556c3fd 1142 label_buf, nexthop->ifindex, nexthop->vrf_id);
d62a17ae 1143 }
5e210522
DS
1144
1145 /*
1146 * We have the ifindex so we should always send it
1147 * This is especially useful if we are doing route
1148 * leaking.
1149 */
1150 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
d62a17ae 1151 addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1152
275565fb 1153 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
d62a17ae 1154 if (cmd == RTM_NEWROUTE) {
1155 if (nexthop->rmap_src.ipv4.s_addr)
1156 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1157 &nexthop->rmap_src.ipv4, bytelen);
1158 else if (nexthop->src.ipv4.s_addr)
1159 addattr_l(nlmsg, req_size, RTA_PREFSRC,
1160 &nexthop->src.ipv4, bytelen);
1161 }
fa713d9e 1162
d62a17ae 1163 if (IS_ZEBRA_DEBUG_KERNEL)
1164 zlog_debug(
1165 "netlink_route_multipath() (%s): "
7556c3fd
DS
1166 "nexthop via if %u(%u)",
1167 routedesc, nexthop->ifindex, nexthop->vrf_id);
0aabccc0 1168 }
fa713d9e
CF
1169}
1170
1171/* This function takes a nexthop as argument and
1172 * appends to the given rtattr/rtnexthop pair the
1173 * representation of the nexthop. If the nexthop
1174 * defines a preferred source, the src parameter
1175 * will be modified to point to that src, otherwise
1176 * it will be kept unmodified.
1177 *
1178 * @param routedesc: Human readable description of route type
1179 * (direct/recursive, single-/multipath)
1180 * @param bytelen: Length of addresses in bytes.
1181 * @param nexthop: Nexthop information
1182 * @param rta: rtnetlink attribute structure
1183 * @param rtnh: pointer to an rtnetlink nexthop structure
1184 * @param src: pointer pointing to a location where
1185 * the prefsrc should be stored.
1186 */
d62a17ae 1187static void _netlink_route_build_multipath(const char *routedesc, int bytelen,
81793ac1 1188 const struct nexthop *nexthop,
d62a17ae 1189 struct rtattr *rta,
1190 struct rtnexthop *rtnh,
1191 struct rtmsg *rtmsg,
81793ac1 1192 const union g_addr **src)
fa713d9e 1193{
8ecdb26e 1194 struct mpls_label_stack *nh_label;
d62a17ae 1195 mpls_lse_t out_lse[MPLS_MAX_LABELS];
fa712963 1196 int num_labels = 0;
9a62e84b 1197 char label_buf[256];
d62a17ae 1198
1199 rtnh->rtnh_len = sizeof(*rtnh);
1200 rtnh->rtnh_flags = 0;
1201 rtnh->rtnh_hops = 0;
1202 rta->rta_len += rtnh->rtnh_len;
1203
1204 /*
1205 * label_buf is *only* currently used within debugging.
1206 * As such when we assign it we are guarding it inside
1207 * a debug test. If you want to change this make sure
1208 * you fix this assumption
1209 */
1210 label_buf[0] = '\0';
d62a17ae 1211
fa712963 1212 assert(nexthop);
81793ac1 1213 for (const struct nexthop *nh = nexthop; nh; nh = nh->rparent) {
d62a17ae 1214 char label_buf1[20];
1215
fa712963
RW
1216 nh_label = nh->nh_label;
1217 if (!nh_label || !nh_label->num_labels)
1218 continue;
1219
1220 for (int i = 0; i < nh_label->num_labels; i++) {
1221 if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL)
1222 continue;
1223
1224 if (IS_ZEBRA_DEBUG_KERNEL) {
1225 if (!num_labels)
1226 sprintf(label_buf, "label %u",
1227 nh_label->label[i]);
1228 else {
1229 sprintf(label_buf1, "/%u",
1230 nh_label->label[i]);
1231 strlcat(label_buf, label_buf1,
1232 sizeof(label_buf));
d62a17ae 1233 }
d62a17ae 1234 }
fa712963
RW
1235
1236 out_lse[num_labels] =
1237 mpls_lse_encode(nh_label->label[i], 0, 0, 0);
1238 num_labels++;
d62a17ae 1239 }
fa712963
RW
1240 }
1241
1242 if (num_labels) {
1243 /* Set the BoS bit */
1244 out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT);
1245
1246 if (rtmsg->rtm_family == AF_MPLS) {
1247 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_NEWDST,
1248 &out_lse,
1249 num_labels * sizeof(mpls_lse_t));
1250 rtnh->rtnh_len +=
1251 RTA_LENGTH(num_labels * sizeof(mpls_lse_t));
1252 } else {
1253 struct rtattr *nest;
d7c0a89a 1254 uint16_t encap = LWTUNNEL_ENCAP_MPLS;
fa712963
RW
1255 int len = rta->rta_len;
1256
1257 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_ENCAP_TYPE,
d7c0a89a 1258 &encap, sizeof(uint16_t));
fa712963
RW
1259 nest = rta_nest(rta, NL_PKT_BUF_SIZE, RTA_ENCAP);
1260 rta_addattr_l(rta, NL_PKT_BUF_SIZE, MPLS_IPTUNNEL_DST,
1261 &out_lse,
1262 num_labels * sizeof(mpls_lse_t));
1263 rta_nest_end(rta, nest);
1264 rtnh->rtnh_len += rta->rta_len - len;
66d42727 1265 }
d62a17ae 1266 }
1267
1268 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
1269 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1270
1271 if (rtmsg->rtm_family == AF_INET
1272 && (nexthop->type == NEXTHOP_TYPE_IPV6
1273 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) {
1274 bytelen = 4;
1275 rtnh->rtnh_flags |= RTNH_F_ONLINK;
1276 rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, &ipv4_ll,
1277 bytelen);
1278 rtnh->rtnh_len += sizeof(struct rtattr) + bytelen;
1279 rtnh->rtnh_ifindex = nexthop->ifindex;
1280
1281 if (nexthop->rmap_src.ipv4.s_addr)
1282 *src = &nexthop->rmap_src;
1283 else if (nexthop->src.ipv4.s_addr)
1284 *src = &nexthop->src;
1285
1286 if (IS_ZEBRA_DEBUG_KERNEL)
1287 zlog_debug(
1288 " 5549: netlink_route_build_multipath() (%s): "
1289 "nexthop via %s %s if %u",
1290 routedesc, ipv4_ll_buf, label_buf,
1291 nexthop->ifindex);
1292 return;
1293 }
1294
1295 if (nexthop->type == NEXTHOP_TYPE_IPV4
1296 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
1297 _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET,
1298 rta, rtnh, NL_PKT_BUF_SIZE,
1299 bytelen, nexthop);
1300 if (nexthop->rmap_src.ipv4.s_addr)
1301 *src = &nexthop->rmap_src;
1302 else if (nexthop->src.ipv4.s_addr)
1303 *src = &nexthop->src;
1304
1305 if (IS_ZEBRA_DEBUG_KERNEL)
1306 zlog_debug(
1307 "netlink_route_multipath() (%s): "
1308 "nexthop via %s %s if %u",
1309 routedesc, inet_ntoa(nexthop->gate.ipv4),
1310 label_buf, nexthop->ifindex);
1311 }
1312 if (nexthop->type == NEXTHOP_TYPE_IPV6
1313 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
1314 _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET6,
1315 rta, rtnh, NL_PKT_BUF_SIZE,
1316 bytelen, nexthop);
1317
1318 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6))
1319 *src = &nexthop->rmap_src;
1320 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6))
1321 *src = &nexthop->src;
1322
1323 if (IS_ZEBRA_DEBUG_KERNEL)
1324 zlog_debug(
1325 "netlink_route_multipath() (%s): "
1326 "nexthop via %s %s if %u",
1327 routedesc, inet6_ntoa(nexthop->gate.ipv6),
1328 label_buf, nexthop->ifindex);
1329 }
5e210522
DS
1330
1331 /*
1332 * We have figured out the ifindex so we should always send it
1333 * This is especially useful if we are doing route
1334 * leaking.
1335 */
1336 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)
1337 rtnh->rtnh_ifindex = nexthop->ifindex;
1338
d62a17ae 1339 /* ifindex */
275565fb 1340 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
d62a17ae 1341 if (nexthop->rmap_src.ipv4.s_addr)
1342 *src = &nexthop->rmap_src;
1343 else if (nexthop->src.ipv4.s_addr)
1344 *src = &nexthop->src;
1345
1346 if (IS_ZEBRA_DEBUG_KERNEL)
1347 zlog_debug(
1348 "netlink_route_multipath() (%s): "
1349 "nexthop via if %u",
1350 routedesc, nexthop->ifindex);
d62a17ae 1351 }
fa713d9e
CF
1352}
1353
d62a17ae 1354static inline void _netlink_mpls_build_singlepath(const char *routedesc,
81793ac1 1355 const zebra_nhlfe_t *nhlfe,
d62a17ae 1356 struct nlmsghdr *nlmsg,
1357 struct rtmsg *rtmsg,
1358 size_t req_size, int cmd)
40c7bdb0 1359{
d62a17ae 1360 int bytelen;
d7c0a89a 1361 uint8_t family;
40c7bdb0 1362
d62a17ae 1363 family = NHLFE_FAMILY(nhlfe);
1364 bytelen = (family == AF_INET ? 4 : 16);
1365 _netlink_route_build_singlepath(routedesc, bytelen, nhlfe->nexthop,
1366 nlmsg, rtmsg, req_size, cmd);
40c7bdb0 1367}
1368
1369
1370static inline void
81793ac1 1371_netlink_mpls_build_multipath(const char *routedesc, const zebra_nhlfe_t *nhlfe,
d62a17ae 1372 struct rtattr *rta, struct rtnexthop *rtnh,
81793ac1 1373 struct rtmsg *rtmsg, const union g_addr **src)
40c7bdb0 1374{
d62a17ae 1375 int bytelen;
d7c0a89a 1376 uint8_t family;
40c7bdb0 1377
d62a17ae 1378 family = NHLFE_FAMILY(nhlfe);
1379 bytelen = (family == AF_INET ? 4 : 16);
1380 _netlink_route_build_multipath(routedesc, bytelen, nhlfe->nexthop, rta,
1381 rtnh, rtmsg, src);
40c7bdb0 1382}
1383
1384
fa713d9e
CF
1385/* Log debug information for netlink_route_multipath
1386 * if debug logging is enabled.
1387 *
1388 * @param cmd: Netlink command which is to be processed
1389 * @param p: Prefix for which the change is due
fa713d9e 1390 * @param family: Address family which the change concerns
45df4e96
DS
1391 * @param zvrf: The vrf we are in
1392 * @param tableid: The table we are working on
fa713d9e 1393 */
86391e56
MS
1394static void _netlink_route_debug(int cmd, const struct prefix *p,
1395 int family, vrf_id_t vrfid,
7556c3fd 1396 uint32_t tableid)
fa713d9e 1397{
d62a17ae 1398 if (IS_ZEBRA_DEBUG_KERNEL) {
1399 char buf[PREFIX_STRLEN];
1400 zlog_debug(
45df4e96
DS
1401 "netlink_route_multipath(): %s %s vrf %u(%u)",
1402 nl_msg_type_to_str(cmd),
1403 prefix2str(p, buf, sizeof(buf)),
86391e56 1404 vrfid, tableid);
d62a17ae 1405 }
1406}
1407
d7c0a89a 1408static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc)
40c7bdb0 1409{
d62a17ae 1410 if (IS_ZEBRA_DEBUG_KERNEL)
1411 zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc,
1412 nl_msg_type_to_str(cmd), label);
fa713d9e
CF
1413}
1414
d62a17ae 1415static int netlink_neigh_update(int cmd, int ifindex, uint32_t addr, char *lla,
5895d33f 1416 int llalen, ns_id_t ns_id)
5c610faf 1417{
d62a17ae 1418 struct {
1419 struct nlmsghdr n;
1420 struct ndmsg ndm;
1421 char buf[256];
1422 } req;
5c610faf 1423
5895d33f 1424 struct zebra_ns *zns = zebra_ns_lookup(ns_id);
8f7d9fc0 1425
5605ecfc 1426 memset(&req, 0, sizeof(req));
5c610faf 1427
d62a17ae 1428 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1429 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1430 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
1431 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
a55ba23f 1432
d62a17ae 1433 req.ndm.ndm_family = AF_INET;
1434 req.ndm.ndm_state = NUD_PERMANENT;
1435 req.ndm.ndm_ifindex = ifindex;
1436 req.ndm.ndm_type = RTN_UNICAST;
5c610faf 1437
d62a17ae 1438 addattr_l(&req.n, sizeof(req), NDA_DST, &addr, 4);
1439 addattr_l(&req.n, sizeof(req), NDA_LLADDR, lla, llalen);
5c610faf 1440
d62a17ae 1441 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1442 0);
5c610faf
DS
1443}
1444
7cdb1a84
MS
1445/*
1446 * Routing table change via netlink interface, using a dataplane context object
1447 */
25779064 1448static int netlink_route_multipath(int cmd, struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1449{
1450 int bytelen;
7cdb1a84
MS
1451 struct nexthop *nexthop = NULL;
1452 unsigned int nexthop_num;
1453 int family;
1454 const char *routedesc;
1455 int setsrc = 0;
1456 union g_addr src;
1457 const struct prefix *p, *src_p;
1458 uint32_t table_id;
1459
1460 struct {
1461 struct nlmsghdr n;
1462 struct rtmsg r;
1463 char buf[NL_PKT_BUF_SIZE];
1464 } req;
1465
1466 p = dplane_ctx_get_dest(ctx);
1467 src_p = dplane_ctx_get_src(ctx);
1468
1469 family = PREFIX_FAMILY(p);
1470
5709131c 1471 memset(&req, 0, sizeof(req) - NL_PKT_BUF_SIZE);
7cdb1a84
MS
1472
1473 bytelen = (family == AF_INET ? 4 : 16);
1474
1475 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1476 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1477
334734a8
DS
1478 if ((cmd == RTM_NEWROUTE) &&
1479 ((p->family == AF_INET) || v6_rr_semantics))
1480 req.n.nlmsg_flags |= NLM_F_REPLACE;
7cdb1a84
MS
1481
1482 req.n.nlmsg_type = cmd;
1483
1484 req.n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
1485
1486 req.r.rtm_family = family;
1487 req.r.rtm_dst_len = p->prefixlen;
1488 req.r.rtm_src_len = src_p ? src_p->prefixlen : 0;
1489 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1490
5709131c 1491 if (cmd == RTM_DELROUTE)
7cdb1a84 1492 req.r.rtm_protocol = zebra2proto(dplane_ctx_get_old_type(ctx));
5709131c 1493 else
7cdb1a84 1494 req.r.rtm_protocol = zebra2proto(dplane_ctx_get_type(ctx));
7cdb1a84
MS
1495
1496 /*
1497 * blackhole routes are not RTN_UNICAST, they are
1498 * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT
1499 * so setting this value as a RTN_UNICAST would
1500 * cause the route lookup of just the prefix
1501 * to fail. So no need to specify this for
1502 * the RTM_DELROUTE case
1503 */
1504 if (cmd != RTM_DELROUTE)
1505 req.r.rtm_type = RTN_UNICAST;
1506
5709131c 1507 addattr_l(&req.n, sizeof(req), RTA_DST, &p->u.prefix, bytelen);
7cdb1a84 1508 if (src_p)
5709131c 1509 addattr_l(&req.n, sizeof(req), RTA_SRC, &src_p->u.prefix,
7cdb1a84
MS
1510 bytelen);
1511
1512 /* Metric. */
1513 /* Hardcode the metric for all routes coming from zebra. Metric isn't
1514 * used
1515 * either by the kernel or by zebra. Its purely for calculating best
1516 * path(s)
1517 * by the routing protocol and for communicating with protocol peers.
1518 */
5709131c 1519 addattr32(&req.n, sizeof(req), RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC);
7cdb1a84
MS
1520
1521#if defined(SUPPORT_REALMS)
1522 {
1523 route_tag_t tag;
1524
5709131c 1525 if (cmd == RTM_DELROUTE)
7cdb1a84 1526 tag = dplane_ctx_get_old_tag(ctx);
5709131c 1527 else
7cdb1a84 1528 tag = dplane_ctx_get_tag(ctx);
7cdb1a84
MS
1529
1530 if (tag > 0 && tag <= 255)
5709131c 1531 addattr32(&req.n, sizeof(req), RTA_FLOW, tag);
7cdb1a84
MS
1532 }
1533#endif
1534 /* Table corresponding to this route. */
1535 table_id = dplane_ctx_get_table(ctx);
1536 if (table_id < 256)
1537 req.r.rtm_table = table_id;
1538 else {
1539 req.r.rtm_table = RT_TABLE_UNSPEC;
5709131c 1540 addattr32(&req.n, sizeof(req), RTA_TABLE, table_id);
7cdb1a84
MS
1541 }
1542
1543 _netlink_route_debug(cmd, p, family, dplane_ctx_get_vrf(ctx), table_id);
1544
1545 /*
1546 * If we are not updating the route and we have received
1547 * a route delete, then all we need to fill in is the
1548 * prefix information to tell the kernel to schwack
1549 * it.
1550 */
1551 if (cmd == RTM_DELROUTE)
1552 goto skip;
1553
1554 if (dplane_ctx_get_mtu(ctx) || dplane_ctx_get_nh_mtu(ctx)) {
1555 char buf[NL_PKT_BUF_SIZE];
1556 struct rtattr *rta = (void *)buf;
1557 uint32_t mtu = dplane_ctx_get_mtu(ctx);
1558 uint32_t nexthop_mtu = dplane_ctx_get_nh_mtu(ctx);
5709131c 1559
7cdb1a84
MS
1560 if (!mtu || (nexthop_mtu && nexthop_mtu < mtu))
1561 mtu = nexthop_mtu;
1562 rta->rta_type = RTA_METRICS;
1563 rta->rta_len = RTA_LENGTH(0);
5709131c
MS
1564 rta_addattr_l(rta, NL_PKT_BUF_SIZE,
1565 RTAX_MTU, &mtu, sizeof(mtu));
7cdb1a84
MS
1566 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA(rta),
1567 RTA_PAYLOAD(rta));
1568 }
1569
1570 /* Count overall nexthops so we can decide whether to use singlepath
5709131c
MS
1571 * or multipath case.
1572 */
7cdb1a84
MS
1573 nexthop_num = 0;
1574 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1575 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1576 continue;
1577 if (cmd == RTM_NEWROUTE && !NEXTHOP_IS_ACTIVE(nexthop->flags))
1578 continue;
1579
1580 nexthop_num++;
1581 }
1582
1583 /* Singlepath case. */
220f0f42 1584 if (nexthop_num == 1) {
7cdb1a84
MS
1585 nexthop_num = 0;
1586 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1587 /*
1588 * So we want to cover 2 types of blackhole
1589 * routes here:
1590 * 1) A normal blackhole route( ala from a static
1591 * install.
1592 * 2) A recursively resolved blackhole route
1593 */
1594 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1595 switch (nexthop->bh_type) {
1596 case BLACKHOLE_ADMINPROHIB:
1597 req.r.rtm_type = RTN_PROHIBIT;
1598 break;
1599 case BLACKHOLE_REJECT:
1600 req.r.rtm_type = RTN_UNREACHABLE;
1601 break;
1602 default:
1603 req.r.rtm_type = RTN_BLACKHOLE;
1604 break;
1605 }
1606 goto skip;
1607 }
1608 if (CHECK_FLAG(nexthop->flags,
1609 NEXTHOP_FLAG_RECURSIVE)) {
5709131c
MS
1610
1611 if (setsrc)
1612 continue;
1613
1614 if (family == AF_INET) {
1615 if (nexthop->rmap_src.ipv4.s_addr
1616 != 0) {
1617 src.ipv4 =
1618 nexthop->rmap_src.ipv4;
1619 setsrc = 1;
1620 } else if (nexthop->src.ipv4.s_addr
1621 != 0) {
1622 src.ipv4 =
1623 nexthop->src.ipv4;
1624 setsrc = 1;
1625 }
1626 } else if (family == AF_INET6) {
1627 if (!IN6_IS_ADDR_UNSPECIFIED(
1628 &nexthop->rmap_src.ipv6)) {
1629 src.ipv6 =
1630 nexthop->rmap_src.ipv6;
1631 setsrc = 1;
1632 } else if (
1633 !IN6_IS_ADDR_UNSPECIFIED(
1634 &nexthop->src.ipv6)) {
1635 src.ipv6 =
1636 nexthop->src.ipv6;
1637 setsrc = 1;
7cdb1a84
MS
1638 }
1639 }
f183e380 1640 continue;
7cdb1a84
MS
1641 }
1642
1643 if ((cmd == RTM_NEWROUTE
1644 && NEXTHOP_IS_ACTIVE(nexthop->flags))) {
1645 routedesc = nexthop->rparent
1646 ? "recursive, single-path"
1647 : "single-path";
1648
1649 _netlink_route_build_singlepath(
1650 routedesc, bytelen, nexthop, &req.n,
5709131c 1651 &req.r, sizeof(req), cmd);
7cdb1a84
MS
1652 nexthop_num++;
1653 break;
1654 }
1655 }
1656 if (setsrc && (cmd == RTM_NEWROUTE)) {
1657 if (family == AF_INET)
5709131c 1658 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1659 &src.ipv4, bytelen);
1660 else if (family == AF_INET6)
5709131c 1661 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1662 &src.ipv6, bytelen);
1663 }
1664 } else { /* Multipath case */
1665 char buf[NL_PKT_BUF_SIZE];
1666 struct rtattr *rta = (void *)buf;
1667 struct rtnexthop *rtnh;
81793ac1 1668 const union g_addr *src1 = NULL;
7cdb1a84
MS
1669
1670 rta->rta_type = RTA_MULTIPATH;
1671 rta->rta_len = RTA_LENGTH(0);
1672 rtnh = RTA_DATA(rta);
1673
1674 nexthop_num = 0;
1675 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
7cdb1a84
MS
1676 if (CHECK_FLAG(nexthop->flags,
1677 NEXTHOP_FLAG_RECURSIVE)) {
1678 /* This only works for IPv4 now */
5709131c
MS
1679 if (setsrc)
1680 continue;
1681
1682 if (family == AF_INET) {
1683 if (nexthop->rmap_src.ipv4.s_addr
1684 != 0) {
1685 src.ipv4 =
1686 nexthop->rmap_src.ipv4;
1687 setsrc = 1;
1688 } else if (nexthop->src.ipv4.s_addr
1689 != 0) {
1690 src.ipv4 =
1691 nexthop->src.ipv4;
1692 setsrc = 1;
1693 }
1694 } else if (family == AF_INET6) {
1695 if (!IN6_IS_ADDR_UNSPECIFIED(
1696 &nexthop->rmap_src.ipv6)) {
1697 src.ipv6 =
1698 nexthop->rmap_src.ipv6;
1699 setsrc = 1;
1700 } else if (
1701 !IN6_IS_ADDR_UNSPECIFIED(
1702 &nexthop->src.ipv6)) {
1703 src.ipv6 =
1704 nexthop->src.ipv6;
1705 setsrc = 1;
7cdb1a84
MS
1706 }
1707 }
78e54ded
MS
1708
1709 continue;
7cdb1a84
MS
1710 }
1711
1712 if ((cmd == RTM_NEWROUTE
1713 && NEXTHOP_IS_ACTIVE(nexthop->flags))) {
1714 routedesc = nexthop->rparent
1715 ? "recursive, multipath"
1716 : "multipath";
1717 nexthop_num++;
1718
1719 _netlink_route_build_multipath(
1720 routedesc, bytelen, nexthop, rta, rtnh,
1721 &req.r, &src1);
1722 rtnh = RTNH_NEXT(rtnh);
1723
1724 if (!setsrc && src1) {
1725 if (family == AF_INET)
1726 src.ipv4 = src1->ipv4;
1727 else if (family == AF_INET6)
1728 src.ipv6 = src1->ipv6;
1729
1730 setsrc = 1;
1731 }
1732 }
1733 }
1734 if (setsrc && (cmd == RTM_NEWROUTE)) {
1735 if (family == AF_INET)
5709131c 1736 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1737 &src.ipv4, bytelen);
1738 else if (family == AF_INET6)
5709131c 1739 addattr_l(&req.n, sizeof(req), RTA_PREFSRC,
7cdb1a84
MS
1740 &src.ipv6, bytelen);
1741 if (IS_ZEBRA_DEBUG_KERNEL)
1742 zlog_debug("Setting source");
1743 }
1744
1745 if (rta->rta_len > RTA_LENGTH(0))
1746 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
1747 RTA_DATA(rta), RTA_PAYLOAD(rta));
1748 }
1749
1750 /* If there is no useful nexthop then return. */
1751 if (nexthop_num == 0) {
1752 if (IS_ZEBRA_DEBUG_KERNEL)
1753 zlog_debug(
1754 "netlink_route_multipath(): No useful nexthop.");
1755 return 0;
1756 }
1757
1758skip:
7cdb1a84
MS
1759 /* Talk to netlink socket. */
1760 return netlink_talk_info(netlink_talk_filter, &req.n,
1761 dplane_ctx_get_ns(ctx), 0);
1762}
1763
43b5cc5e 1764int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in)
e3be0432 1765{
5523c156 1766 uint32_t actual_table;
d62a17ae 1767 int suc = 0;
1768 struct mcast_route_data *mr = (struct mcast_route_data *)in;
bd8b9272
DS
1769 struct {
1770 struct nlmsghdr n;
1771 struct ndmsg ndm;
1772 char buf[256];
1773 } req;
e3be0432 1774
d62a17ae 1775 mroute = mr;
5895d33f 1776 struct zebra_ns *zns;
bd8b9272 1777
009f8ad5 1778 zns = zvrf->zns;
5605ecfc 1779 memset(&req, 0, sizeof(req));
bd8b9272
DS
1780
1781 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1782 req.n.nlmsg_flags = NLM_F_REQUEST;
1783 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1784
1785 req.ndm.ndm_family = RTNL_FAMILY_IPMR;
1786 req.n.nlmsg_type = RTM_GETROUTE;
1787
1788 addattr_l(&req.n, sizeof(req), RTA_IIF, &mroute->ifindex, 4);
1789 addattr_l(&req.n, sizeof(req), RTA_OIF, &mroute->ifindex, 4);
1790 addattr_l(&req.n, sizeof(req), RTA_SRC, &mroute->sg.src.s_addr, 4);
1791 addattr_l(&req.n, sizeof(req), RTA_DST, &mroute->sg.grp.s_addr, 4);
5523c156
DS
1792 /*
1793 * What?
1794 *
1795 * So during the namespace cleanup we started storing
1796 * the zvrf table_id for the default table as RT_TABLE_MAIN
1797 * which is what the normal routing table for ip routing is.
1798 * This change caused this to break our lookups of sg data
1799 * because prior to this change the zvrf->table_id was 0
1800 * and when the pim multicast kernel code saw a 0,
1801 * it was auto-translated to RT_TABLE_DEFAULT. But since
1802 * we are now passing in RT_TABLE_MAIN there is no auto-translation
1803 * and the kernel goes screw you and the delicious cookies you
1804 * are trying to give me. So now we have this little hack.
1805 */
1806 actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT :
1807 zvrf->table_id;
1808 addattr_l(&req.n, sizeof(req), RTA_TABLE, &actual_table, 4);
e3be0432 1809
bd8b9272
DS
1810 suc = netlink_talk(netlink_route_change_read_multicast, &req.n,
1811 &zns->netlink_cmd, zns, 0);
e3be0432 1812
bd8b9272 1813 mroute = NULL;
d62a17ae 1814 return suc;
e3be0432
DS
1815}
1816
7cdb1a84
MS
1817/*
1818 * Update or delete a prefix from the kernel,
1819 * using info from a dataplane context.
1820 */
25779064 1821enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1822{
1823 int cmd, ret;
1824 const struct prefix *p = dplane_ctx_get_dest(ctx);
f183e380 1825 struct nexthop *nexthop;
7cdb1a84
MS
1826
1827 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
1828 cmd = RTM_DELROUTE;
1829 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
1830 cmd = RTM_NEWROUTE;
1831 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
1832
1833 if (p->family == AF_INET || v6_rr_semantics) {
1834 /* Single 'replace' operation */
1835 cmd = RTM_NEWROUTE;
1836 } else {
1837 /*
1838 * So v6 route replace semantics are not in
1839 * the kernel at this point as I understand it.
1840 * so let's do a delete then an add.
1841 * In the future once v6 route replace semantics
1842 * are in we can figure out what to do here to
1843 * allow working with old and new kernels.
1844 *
1845 * I'm also intentionally ignoring the failure case
1846 * of the route delete. If that happens yeah we're
1847 * screwed.
1848 */
3cdba47a
DS
1849 if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx)))
1850 (void)netlink_route_multipath(RTM_DELROUTE,
1851 ctx);
7cdb1a84
MS
1852 cmd = RTM_NEWROUTE;
1853 }
1854
1855 } else {
1856 return ZEBRA_DPLANE_REQUEST_FAILURE;
1857 }
1858
3cdba47a
DS
1859 if (!RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)))
1860 ret = netlink_route_multipath(cmd, ctx);
1861 else
1862 ret = 0;
f183e380
MS
1863 if ((cmd == RTM_NEWROUTE) && (ret == 0)) {
1864 /* Update installed nexthops to signal which have been
1865 * installed.
1866 */
1867 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
1868 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1869 continue;
1870
1871 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
1872 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380
MS
1873 }
1874 }
1875 }
7cdb1a84
MS
1876
1877 return (ret == 0 ?
1878 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
1879}
1880
d62a17ae 1881int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 1882 int llalen, ns_id_t ns_id)
6b8a5694 1883{
d62a17ae 1884 return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex,
5895d33f 1885 addr, lla, llalen, ns_id);
6b8a5694 1886}
718e3744 1887
13d60d35 1888/*
1889 * Add remote VTEP to the flood list for this VxLAN interface (VNI). This
1890 * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00.
1891 */
d62a17ae 1892static int netlink_vxlan_flood_list_update(struct interface *ifp,
1893 struct in_addr *vtep_ip, int cmd)
13d60d35 1894{
5895d33f 1895 struct zebra_ns *zns;
d62a17ae 1896 struct {
1897 struct nlmsghdr n;
1898 struct ndmsg ndm;
1899 char buf[256];
1900 } req;
d7c0a89a 1901 uint8_t dst_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
009f8ad5 1902 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
d62a17ae 1903
009f8ad5 1904 zns = zvrf->zns;
5605ecfc 1905 memset(&req, 0, sizeof(req));
d62a17ae 1906
1907 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1908 req.n.nlmsg_flags = NLM_F_REQUEST;
1909 if (cmd == RTM_NEWNEIGH)
1910 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_APPEND);
1911 req.n.nlmsg_type = cmd;
1912 req.ndm.ndm_family = PF_BRIDGE;
1913 req.ndm.ndm_state = NUD_NOARP | NUD_PERMANENT;
1914 req.ndm.ndm_flags |= NTF_SELF; // Handle by "self", not "master"
1915
1916
1917 addattr_l(&req.n, sizeof(req), NDA_LLADDR, &dst_mac, 6);
1918 req.ndm.ndm_ifindex = ifp->ifindex;
1919 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip->s_addr, 4);
1920
1921 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1922 0);
13d60d35 1923}
1924
1925/*
d62a17ae 1926 * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1927 * adding
13d60d35 1928 * a "flood" MAC FDB entry.
1929 */
d62a17ae 1930int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 1931{
d62a17ae 1932 if (IS_ZEBRA_DEBUG_VXLAN)
1933 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
1934 inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex);
13d60d35 1935
d62a17ae 1936 return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_NEWNEIGH);
13d60d35 1937}
1938
1939/*
1940 * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves
1941 * deleting the "flood" MAC FDB entry.
1942 */
d62a17ae 1943int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 1944{
d62a17ae 1945 if (IS_ZEBRA_DEBUG_VXLAN)
1946 zlog_debug(
1947 "Uninstall %s from flood list for VNI %u intf %s(%u)",
1948 inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex);
13d60d35 1949
d62a17ae 1950 return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_DELNEIGH);
13d60d35 1951}
1952
2232a77c 1953#ifndef NDA_RTA
d62a17ae 1954#define NDA_RTA(r) \
1955 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
2232a77c 1956#endif
1957
2414abd3 1958static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 1959{
d62a17ae 1960 struct ndmsg *ndm;
1961 struct interface *ifp;
1962 struct zebra_if *zif;
d62a17ae 1963 struct rtattr *tb[NDA_MAX + 1];
1964 struct interface *br_if;
1965 struct ethaddr mac;
1966 vlanid_t vid = 0;
1967 struct prefix vtep_ip;
1968 int vid_present = 0, dst_present = 0;
1969 char buf[ETHER_ADDR_STRLEN];
1970 char vid_buf[20];
1971 char dst_buf[30];
a37f4598 1972 bool sticky;
d62a17ae 1973
1974 ndm = NLMSG_DATA(h);
1975
2853fed6 1976 /* We only process macfdb notifications if EVPN is enabled */
1977 if (!is_evpn_enabled())
1978 return 0;
1979
d62a17ae 1980 /* The interface should exist. */
5895d33f 1981 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 1982 ndm->ndm_ifindex);
28bd0652
DS
1983 if (!ifp || !ifp->info) {
1984 if (IS_ZEBRA_DEBUG_KERNEL)
1985 zlog_debug("\t%s without associated interface: %u",
1986 __PRETTY_FUNCTION__, ndm->ndm_ifindex);
d62a17ae 1987 return 0;
28bd0652 1988 }
d62a17ae 1989
1990 /* The interface should be something we're interested in. */
28bd0652
DS
1991 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
1992 if (IS_ZEBRA_DEBUG_KERNEL)
1993 zlog_debug("\t%s Not interested in %s, not a slave",
1994 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 1995 return 0;
28bd0652 1996 }
d62a17ae 1997
1998 /* Drop "permanent" entries. */
28bd0652
DS
1999 if (ndm->ndm_state & NUD_PERMANENT) {
2000 if (IS_ZEBRA_DEBUG_KERNEL)
2001 zlog_debug("\t%s Entry is PERMANENT, dropping",
2002 __PRETTY_FUNCTION__);
d62a17ae 2003 return 0;
28bd0652 2004 }
d62a17ae 2005
2006 zif = (struct zebra_if *)ifp->info;
2007 if ((br_if = zif->brslave_info.br_if) == NULL) {
28bd0652
DS
2008 if (IS_ZEBRA_DEBUG_KERNEL)
2009 zlog_debug(
2010 "%s family %s IF %s(%u) brIF %u - no bridge master",
2011 nl_msg_type_to_str(h->nlmsg_type),
2012 nl_family_to_str(ndm->ndm_family), ifp->name,
2013 ndm->ndm_ifindex,
2014 zif->brslave_info.bridge_ifindex);
d62a17ae 2015 return 0;
2016 }
2017
2018 /* Parse attributes and extract fields of interest. */
2019 memset(tb, 0, sizeof tb);
2020 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
2021
2022 if (!tb[NDA_LLADDR]) {
28bd0652
DS
2023 if (IS_ZEBRA_DEBUG_KERNEL)
2024 zlog_debug("%s family %s IF %s(%u) brIF %u - no LLADDR",
2025 nl_msg_type_to_str(h->nlmsg_type),
2026 nl_family_to_str(ndm->ndm_family), ifp->name,
2027 ndm->ndm_ifindex,
2028 zif->brslave_info.bridge_ifindex);
d62a17ae 2029 return 0;
2030 }
2031
ff8b7eb8 2032 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
2033 if (IS_ZEBRA_DEBUG_KERNEL)
2034 zlog_debug(
2035 "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu",
2036 nl_msg_type_to_str(h->nlmsg_type),
2037 nl_family_to_str(ndm->ndm_family), ifp->name,
2038 ndm->ndm_ifindex,
2039 zif->brslave_info.bridge_ifindex,
2040 (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR]));
d62a17ae 2041 return 0;
2042 }
2043
ff8b7eb8 2044 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 2045
2046 if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) {
2047 vid_present = 1;
d7c0a89a 2048 vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]);
d62a17ae 2049 sprintf(vid_buf, " VLAN %u", vid);
2050 }
2051
2052 if (tb[NDA_DST]) {
2053 /* TODO: Only IPv4 supported now. */
2054 dst_present = 1;
2055 vtep_ip.family = AF_INET;
2056 vtep_ip.prefixlen = IPV4_MAX_BITLEN;
2057 memcpy(&(vtep_ip.u.prefix4.s_addr), RTA_DATA(tb[NDA_DST]),
2058 IPV4_MAX_BYTELEN);
2059 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip.u.prefix4));
2060 }
2061
a37f4598 2062 sticky = !!(ndm->ndm_state & NUD_NOARP);
d62a17ae 2063
2064 if (IS_ZEBRA_DEBUG_KERNEL)
2065 zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s",
2066 nl_msg_type_to_str(h->nlmsg_type),
2067 nl_family_to_str(ndm->ndm_family), ifp->name,
2068 ndm->ndm_ifindex, vid_present ? vid_buf : "",
2069 sticky ? "sticky " : "",
2070 prefix_mac2str(&mac, buf, sizeof(buf)),
2071 dst_present ? dst_buf : "");
2072
28bd0652
DS
2073 if (filter_vlan && vid != filter_vlan) {
2074 if (IS_ZEBRA_DEBUG_KERNEL)
2075 zlog_debug("\tFiltered due to filter vlan: %d",
2076 filter_vlan);
d62a17ae 2077 return 0;
28bd0652 2078 }
d62a17ae 2079
2080 /* If add or update, do accordingly if learnt on a "local" interface; if
2081 * the notification is over VxLAN, this has to be related to
2082 * multi-homing,
2083 * so perform an implicit delete of any local entry (if it exists).
2084 */
2085 if (h->nlmsg_type == RTM_NEWNEIGH) {
d62a17ae 2086 if (IS_ZEBRA_IF_VXLAN(ifp))
2087 return zebra_vxlan_check_del_local_mac(ifp, br_if, &mac,
2088 vid);
2089
2090 return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid,
2091 sticky);
2092 }
2093
2094 /* This is a delete notification.
2095 * 1. For a MAC over VxLan, check if it needs to be refreshed(readded)
2096 * 2. For a MAC over "local" interface, delete the mac
2097 * Note: We will get notifications from both bridge driver and VxLAN
2098 * driver.
2099 * Ignore the notification from VxLan driver as it is also generated
2100 * when mac moves from remote to local.
2101 */
28bd0652
DS
2102 if (dst_present) {
2103 if (IS_ZEBRA_DEBUG_KERNEL)
2104 zlog_debug("\tNo Destination Present");
d62a17ae 2105 return 0;
28bd0652 2106 }
d62a17ae 2107
2108 if (IS_ZEBRA_IF_VXLAN(ifp))
2109 return zebra_vxlan_check_readd_remote_mac(ifp, br_if, &mac,
2110 vid);
2111
2112 return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid);
2232a77c 2113}
2114
2414abd3 2115static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 2116{
d62a17ae 2117 int len;
2118 struct ndmsg *ndm;
2232a77c 2119
d62a17ae 2120 if (h->nlmsg_type != RTM_NEWNEIGH)
2121 return 0;
2232a77c 2122
d62a17ae 2123 /* Length validity. */
2124 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2125 if (len < 0)
2126 return -1;
2232a77c 2127
d62a17ae 2128 /* We are interested only in AF_BRIDGE notifications. */
2129 ndm = NLMSG_DATA(h);
2130 if (ndm->ndm_family != AF_BRIDGE)
2131 return 0;
2232a77c 2132
2414abd3 2133 return netlink_macfdb_change(h, len, ns_id);
2232a77c 2134}
2135
2136/* Request for MAC FDB information from the kernel */
85a75f1e
MS
2137static int netlink_request_macs(struct nlsock *netlink_cmd, int family,
2138 int type, ifindex_t master_ifindex)
2232a77c 2139{
d62a17ae 2140 struct {
2141 struct nlmsghdr n;
2142 struct ifinfomsg ifm;
2143 char buf[256];
2144 } req;
2145
2146 /* Form the request, specifying filter (rtattr) if needed. */
2147 memset(&req, 0, sizeof(req));
2148 req.n.nlmsg_type = type;
718f9b0f 2149 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 2150 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2151 req.ifm.ifi_family = family;
2152 if (master_ifindex)
2153 addattr32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex);
2154
85a75f1e 2155 return netlink_request(netlink_cmd, &req.n);
2232a77c 2156}
2157
2158/*
2159 * MAC forwarding database read using netlink interface. This is invoked
2160 * at startup.
2161 */
d62a17ae 2162int netlink_macfdb_read(struct zebra_ns *zns)
2232a77c 2163{
d62a17ae 2164 int ret;
85a75f1e
MS
2165 struct zebra_dplane_info dp_info;
2166
2167 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2168
2169 /* Get bridge FDB table. */
85a75f1e
MS
2170 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
2171 0);
d62a17ae 2172 if (ret < 0)
2173 return ret;
2174 /* We are reading entire table. */
2175 filter_vlan = 0;
85a75f1e
MS
2176 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2177 &dp_info, 0, 1);
d62a17ae 2178
2179 return ret;
2232a77c 2180}
2181
2182/*
2183 * MAC forwarding database read using netlink interface. This is for a
2184 * specific bridge and matching specific access VLAN (if VLAN-aware bridge).
2185 */
d62a17ae 2186int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
2187 struct interface *br_if)
2232a77c 2188{
d62a17ae 2189 struct zebra_if *br_zif;
2190 struct zebra_if *zif;
2191 struct zebra_l2info_vxlan *vxl;
85a75f1e 2192 struct zebra_dplane_info dp_info;
d62a17ae 2193 int ret = 0;
2194
85a75f1e 2195 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 2196
2197 /* Save VLAN we're filtering on, if needed. */
2198 br_zif = (struct zebra_if *)br_if->info;
2199 zif = (struct zebra_if *)ifp->info;
2200 vxl = &zif->l2info.vxl;
2201 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif))
2202 filter_vlan = vxl->access_vlan;
2203
2204 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2205 */
85a75f1e 2206 ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH,
d62a17ae 2207 br_if->ifindex);
2208 if (ret < 0)
2209 return ret;
85a75f1e
MS
2210 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2211 &dp_info, 0, 0);
d62a17ae 2212
2213 /* Reset VLAN filter. */
2214 filter_vlan = 0;
2215 return ret;
2232a77c 2216}
2217
67fb9374
CS
2218
2219/* Request for MAC FDB for a specific MAC address in VLAN from the kernel */
2220static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns,
2221 int family,
2222 int type,
2223 struct interface *br_if,
2224 struct ethaddr *mac,
2225 vlanid_t vid)
2226{
2227 struct {
2228 struct nlmsghdr n;
2229 struct ndmsg ndm;
2230 char buf[256];
2231 } req;
2232 struct zebra_if *br_zif;
2233 char buf[ETHER_ADDR_STRLEN];
2234
2235 memset(&req, 0, sizeof(req));
2236 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2237 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
2238 req.n.nlmsg_flags = NLM_F_REQUEST;
2239 req.ndm.ndm_family = family; /* AF_BRIDGE */
2240 /* req.ndm.ndm_state = NUD_REACHABLE; */
2241
2242 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2243
2244 br_zif = (struct zebra_if *)br_if->info;
2245 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0)
2246 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2247
2248 addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
2249
2250 if (IS_ZEBRA_DEBUG_KERNEL)
2251 zlog_debug("%s: Tx family %s IF %s(%u) MAC %s vid %u",
2252 __PRETTY_FUNCTION__,
2253 nl_family_to_str(req.ndm.ndm_family), br_if->name,
2254 br_if->ifindex,
2255 prefix_mac2str(mac, buf, sizeof(buf)), vid);
2256
2257 return netlink_request(&zns->netlink_cmd, &req.n);
2258}
2259
2260int netlink_macfdb_read_specific_mac(struct zebra_ns *zns,
2261 struct interface *br_if,
2262 struct ethaddr *mac, vlanid_t vid)
2263{
2264 int ret = 0;
2265 struct zebra_dplane_info dp_info;
2266
2267 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2268
2269 /* Get bridge FDB table for specific bridge - we do the VLAN filtering.
2270 */
2271 ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE,
2272 RTM_GETNEIGH,
2273 br_if, mac, vid);
2274 if (ret < 0)
2275 return ret;
2276
2277 ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd,
2278 &dp_info, 1, 0);
2279
2280 return ret;
2281}
d62a17ae 2282static int netlink_macfdb_update(struct interface *ifp, vlanid_t vid,
2283 struct ethaddr *mac, struct in_addr vtep_ip,
a37f4598 2284 int cmd, bool sticky)
2232a77c 2285{
5895d33f 2286 struct zebra_ns *zns;
d62a17ae 2287 struct {
2288 struct nlmsghdr n;
2289 struct ndmsg ndm;
2290 char buf[256];
2291 } req;
2292 int dst_alen;
2293 struct zebra_if *zif;
2294 struct interface *br_if;
2295 struct zebra_if *br_zif;
2296 char buf[ETHER_ADDR_STRLEN];
36590076 2297 int vid_present = 0;
d62a17ae 2298 char vid_buf[20];
2299 char dst_buf[30];
009f8ad5 2300 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
d62a17ae 2301
009f8ad5 2302 zns = zvrf->zns;
d62a17ae 2303 zif = ifp->info;
2304 if ((br_if = zif->brslave_info.br_if) == NULL) {
9df414fe
QY
2305 zlog_debug("MAC %s on IF %s(%u) - no mapping to bridge",
2306 (cmd == RTM_NEWNEIGH) ? "add" : "del", ifp->name,
2307 ifp->ifindex);
d62a17ae 2308 return -1;
2309 }
2310
5605ecfc 2311 memset(&req, 0, sizeof(req));
d62a17ae 2312
2313 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2314 req.n.nlmsg_flags = NLM_F_REQUEST;
2315 if (cmd == RTM_NEWNEIGH)
2316 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2317 req.n.nlmsg_type = cmd;
2318 req.ndm.ndm_family = AF_BRIDGE;
2319 req.ndm.ndm_flags |= NTF_SELF | NTF_MASTER;
2320 req.ndm.ndm_state = NUD_REACHABLE;
2321
2322 if (sticky)
2323 req.ndm.ndm_state |= NUD_NOARP;
2324 else
2325 req.ndm.ndm_flags |= NTF_EXT_LEARNED;
2326
2327 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2328 req.ndm.ndm_ifindex = ifp->ifindex;
d63c1b18 2329 dst_alen = 4; // TODO: hardcoded
2330 addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip, dst_alen);
d63c1b18 2331 sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip));
d62a17ae 2332 br_zif = (struct zebra_if *)br_if->info;
2333 if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0) {
2334 addattr16(&req.n, sizeof(req), NDA_VLAN, vid);
2335 vid_present = 1;
2336 sprintf(vid_buf, " VLAN %u", vid);
2337 }
2338 addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex);
2339
2340 if (IS_ZEBRA_DEBUG_KERNEL)
2341 zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s",
2342 nl_msg_type_to_str(cmd),
2343 nl_family_to_str(req.ndm.ndm_family), ifp->name,
2344 ifp->ifindex, vid_present ? vid_buf : "",
2345 sticky ? "sticky " : "",
36590076 2346 prefix_mac2str(mac, buf, sizeof(buf)), dst_buf);
d62a17ae 2347
2348 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2349 0);
2232a77c 2350}
2351
f17b99ed
DS
2352/*
2353 * In the event the kernel deletes ipv4 link-local neighbor entries created for
2354 * 5549 support, re-install them.
2355 */
2356static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif,
9b036974
DS
2357 struct interface *ifp, struct ipaddr *ip,
2358 bool handle_failed)
f17b99ed
DS
2359{
2360 if (ndm->ndm_family != AF_INET)
2361 return;
2362
2363 if (!zif->v6_2_v4_ll_neigh_entry)
2364 return;
2365
2366 if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr)
2367 return;
2368
9b036974
DS
2369 if (handle_failed && ndm->ndm_state & NUD_FAILED) {
2370 zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling",
2371 ifp->name);
2372 return;
2373 }
2374
f17b99ed
DS
2375 if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true);
2376}
2377
d62a17ae 2378#define NUD_VALID \
2379 (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \
2380 | NUD_DELAY)
2232a77c 2381
2414abd3 2382static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
2232a77c 2383{
d62a17ae 2384 struct ndmsg *ndm;
2385 struct interface *ifp;
2386 struct zebra_if *zif;
d62a17ae 2387 struct rtattr *tb[NDA_MAX + 1];
2388 struct interface *link_if;
2389 struct ethaddr mac;
2390 struct ipaddr ip;
2391 char buf[ETHER_ADDR_STRLEN];
2392 char buf2[INET6_ADDRSTRLEN];
2393 int mac_present = 0;
a37f4598 2394 bool is_ext;
2395 bool is_router;
d62a17ae 2396
2397 ndm = NLMSG_DATA(h);
2398
2399 /* The interface should exist. */
5895d33f 2400 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
d62a17ae 2401 ndm->ndm_ifindex);
2853fed6 2402 if (!ifp || !ifp->info)
d62a17ae 2403 return 0;
2404
20089ae2
DS
2405 zif = (struct zebra_if *)ifp->info;
2406
2407 /* Parse attributes and extract fields of interest. */
2408 memset(tb, 0, sizeof tb);
2409 netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
2410
2411 if (!tb[NDA_DST]) {
9df414fe
QY
2412 zlog_debug("%s family %s IF %s(%u) - no DST",
2413 nl_msg_type_to_str(h->nlmsg_type),
2414 nl_family_to_str(ndm->ndm_family), ifp->name,
2415 ndm->ndm_ifindex);
d62a17ae 2416 return 0;
20089ae2
DS
2417 }
2418
2419 memset(&ip, 0, sizeof(struct ipaddr));
2420 ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6;
2421 memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
2422
f17b99ed
DS
2423 /* if kernel deletes our rfc5549 neighbor entry, re-install it */
2424 if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) {
9b036974 2425 netlink_handle_5549(ndm, zif, ifp, &ip, false);
28bd0652
DS
2426 if (IS_ZEBRA_DEBUG_KERNEL)
2427 zlog_debug(
2428 "\tNeighbor Entry Received is a 5549 entry, finished");
20089ae2
DS
2429 return 0;
2430 }
d62a17ae 2431
f17b99ed 2432 /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */
9b036974
DS
2433 if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID))
2434 netlink_handle_5549(ndm, zif, ifp, &ip, true);
f17b99ed 2435
d62a17ae 2436 /* The neighbor is present on an SVI. From this, we locate the
2437 * underlying
2438 * bridge because we're only interested in neighbors on a VxLAN bridge.
2439 * The bridge is located based on the nature of the SVI:
2440 * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN
2441 * interface
2442 * and is linked to the bridge
2443 * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge
2444 * inteface
2445 * itself
2446 */
2447 if (IS_ZEBRA_IF_VLAN(ifp)) {
5895d33f 2448 link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
71349e03 2449 zif->link_ifindex);
d62a17ae 2450 if (!link_if)
2451 return 0;
2452 } else if (IS_ZEBRA_IF_BRIDGE(ifp))
2453 link_if = ifp;
28bd0652
DS
2454 else {
2455 if (IS_ZEBRA_DEBUG_KERNEL)
2456 zlog_debug(
2457 "\tNeighbor Entry received is not on a VLAN or a BRIDGE, ignoring");
d62a17ae 2458 return 0;
28bd0652 2459 }
d62a17ae 2460
d62a17ae 2461 memset(&mac, 0, sizeof(struct ethaddr));
d62a17ae 2462 if (h->nlmsg_type == RTM_NEWNEIGH) {
2463 if (tb[NDA_LLADDR]) {
ff8b7eb8 2464 if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) {
28bd0652
DS
2465 if (IS_ZEBRA_DEBUG_KERNEL)
2466 zlog_debug(
2467 "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu",
2468 nl_msg_type_to_str(
2469 h->nlmsg_type),
2470 nl_family_to_str(
2471 ndm->ndm_family),
2472 ifp->name, ndm->ndm_ifindex,
2473 (unsigned long)RTA_PAYLOAD(
2474 tb[NDA_LLADDR]));
d62a17ae 2475 return 0;
2476 }
2477
2478 mac_present = 1;
ff8b7eb8 2479 memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN);
d62a17ae 2480 }
2481
a37f4598 2482 is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED);
2483 is_router = !!(ndm->ndm_flags & NTF_ROUTER);
d62a17ae 2484
2485 if (IS_ZEBRA_DEBUG_KERNEL)
2486 zlog_debug(
2487 "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x",
2488 nl_msg_type_to_str(h->nlmsg_type),
2489 nl_family_to_str(ndm->ndm_family), ifp->name,
2490 ndm->ndm_ifindex,
2491 ipaddr2str(&ip, buf2, sizeof(buf2)),
2492 mac_present
2493 ? prefix_mac2str(&mac, buf, sizeof(buf))
2494 : "",
2495 ndm->ndm_state, ndm->ndm_flags);
2496
2497 /* If the neighbor state is valid for use, process as an add or
2498 * update
2499 * else process as a delete. Note that the delete handling may
2500 * result
2501 * in re-adding the neighbor if it is a valid "remote" neighbor.
2502 */
2503 if (ndm->ndm_state & NUD_VALID)
ee69da27 2504 return zebra_vxlan_handle_kernel_neigh_update(
d62a17ae 2505 ifp, link_if, &ip, &mac, ndm->ndm_state,
a37f4598 2506 is_ext, is_router);
d62a17ae 2507
ee69da27 2508 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
d62a17ae 2509 }
2510
2511 if (IS_ZEBRA_DEBUG_KERNEL)
2512 zlog_debug("Rx %s family %s IF %s(%u) IP %s",
2513 nl_msg_type_to_str(h->nlmsg_type),
2514 nl_family_to_str(ndm->ndm_family), ifp->name,
2515 ndm->ndm_ifindex,
2516 ipaddr2str(&ip, buf2, sizeof(buf2)));
2517
2518 /* Process the delete - it may result in re-adding the neighbor if it is
2519 * a valid "remote" neighbor.
2520 */
ee69da27 2521 return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
2232a77c 2522}
2523
2414abd3 2524static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2232a77c 2525{
d62a17ae 2526 int len;
2527 struct ndmsg *ndm;
2232a77c 2528
d62a17ae 2529 if (h->nlmsg_type != RTM_NEWNEIGH)
2530 return 0;
2232a77c 2531
d62a17ae 2532 /* Length validity. */
2533 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
2534 if (len < 0)
2535 return -1;
2232a77c 2536
d62a17ae 2537 /* We are interested only in AF_INET or AF_INET6 notifications. */
2538 ndm = NLMSG_DATA(h);
2539 if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6)
2540 return 0;
2232a77c 2541
2414abd3 2542 return netlink_neigh_change(h, len);
2232a77c 2543}
2544
2545/* Request for IP neighbor information from the kernel */
85a75f1e
MS
2546static int netlink_request_neigh(struct nlsock *netlink_cmd, int family,
2547 int type, ifindex_t ifindex)
2232a77c 2548{
d62a17ae 2549 struct {
2550 struct nlmsghdr n;
2551 struct ndmsg ndm;
2552 char buf[256];
2553 } req;
2554
2555 /* Form the request, specifying filter (rtattr) if needed. */
2556 memset(&req, 0, sizeof(req));
2557 req.n.nlmsg_type = type;
718f9b0f 2558 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 2559 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2560 req.ndm.ndm_family = family;
2561 if (ifindex)
2562 addattr32(&req.n, sizeof(req), NDA_IFINDEX, ifindex);
2563
85a75f1e 2564 return netlink_request(netlink_cmd, &req.n);
2232a77c 2565}
2566
2567/*
2568 * IP Neighbor table read using netlink interface. This is invoked
2569 * at startup.
2570 */
d62a17ae 2571int netlink_neigh_read(struct zebra_ns *zns)
2232a77c 2572{
d62a17ae 2573 int ret;
85a75f1e
MS
2574 struct zebra_dplane_info dp_info;
2575
2576 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 2577
d62a17ae 2578 /* Get IP neighbor table. */
85a75f1e
MS
2579 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
2580 0);
d62a17ae 2581 if (ret < 0)
2582 return ret;
85a75f1e
MS
2583 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
2584 &dp_info, 0, 1);
2232a77c 2585
d62a17ae 2586 return ret;
2232a77c 2587}
2588
2589/*
2590 * IP Neighbor table read using netlink interface. This is for a specific
2591 * VLAN device.
2592 */
d62a17ae 2593int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 2594{
d62a17ae 2595 int ret = 0;
85a75f1e
MS
2596 struct zebra_dplane_info dp_info;
2597
2598 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2232a77c 2599
85a75f1e 2600 ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH,
d62a17ae 2601 vlan_if->ifindex);
2602 if (ret < 0)
2603 return ret;
85a75f1e
MS
2604 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
2605 &dp_info, 0, 0);
2232a77c 2606
d62a17ae 2607 return ret;
2232a77c 2608}
2609
67fb9374
CS
2610/*
2611 * Request for a specific IP in VLAN (SVI) device from IP Neighbor table,
2612 * read using netlink interface.
2613 */
2614static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns,
2615 int type, struct ipaddr *ip,
2616 ifindex_t ifindex)
2617{
2618 struct {
2619 struct nlmsghdr n;
2620 struct ndmsg ndm;
2621 char buf[256];
2622 } req;
2623 int ipa_len;
2624
2625 /* Form the request, specifying filter (rtattr) if needed. */
2626 memset(&req, 0, sizeof(req));
2627 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2628 req.n.nlmsg_flags = NLM_F_REQUEST;
2629 req.n.nlmsg_type = type; /* RTM_GETNEIGH */
2630 req.ndm.ndm_ifindex = ifindex;
2631
2632 if (IS_IPADDR_V4(ip)) {
2633 ipa_len = IPV4_MAX_BYTELEN;
2634 req.ndm.ndm_family = AF_INET;
2635
2636 } else {
2637 ipa_len = IPV6_MAX_BYTELEN;
2638 req.ndm.ndm_family = AF_INET6;
2639 }
2640
2641 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
2642
2643 return netlink_request(&zns->netlink_cmd, &req.n);
2644}
2645
2646int netlink_neigh_read_specific_ip(struct ipaddr *ip,
2647 struct interface *vlan_if)
2648{
2649 int ret = 0;
2650 struct zebra_ns *zns;
2651 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vlan_if->vrf_id);
2652 char buf[INET6_ADDRSTRLEN];
2653 struct zebra_dplane_info dp_info;
2654
2655 zns = zvrf->zns;
2656
2657 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2658
2659 if (IS_ZEBRA_DEBUG_KERNEL)
2660 zlog_debug("%s: neigh request IF %s(%u) IP %s vrf_id %u",
2661 __PRETTY_FUNCTION__, vlan_if->name,
2662 vlan_if->ifindex,
2663 ipaddr2str(ip, buf, sizeof(buf)),
2664 vlan_if->vrf_id);
2665
2666 ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip,
2667 vlan_if->ifindex);
2668 if (ret < 0)
2669 return ret;
2670
2671 ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd,
2672 &dp_info, 1, 0);
2673
2674 return ret;
2675}
2676
2414abd3 2677int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
2232a77c 2678{
d62a17ae 2679 int len;
2680 struct ndmsg *ndm;
2232a77c 2681
d62a17ae 2682 if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH))
2683 return 0;
2232a77c 2684
d62a17ae 2685 /* Length validity. */
2686 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
9bdf8618
DS
2687 if (len < 0) {
2688 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
2689 __PRETTY_FUNCTION__, h->nlmsg_len,
2690 (size_t)NLMSG_LENGTH(sizeof(struct ndmsg)));
d62a17ae 2691 return -1;
9bdf8618 2692 }
2232a77c 2693
d62a17ae 2694 /* Is this a notification for the MAC FDB or IP neighbor table? */
2695 ndm = NLMSG_DATA(h);
2696 if (ndm->ndm_family == AF_BRIDGE)
2414abd3 2697 return netlink_macfdb_change(h, len, ns_id);
2232a77c 2698
d62a17ae 2699 if (ndm->ndm_type != RTN_UNICAST)
2700 return 0;
2232a77c 2701
d62a17ae 2702 if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6)
2414abd3 2703 return netlink_ipneigh_change(h, len, ns_id);
8a1b681c 2704 else {
9df414fe 2705 flog_warn(
e914ccbe 2706 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
2707 "Invalid address family: %u received from kernel neighbor change: %s",
2708 ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
2709 return 0;
2710 }
2232a77c 2711
d62a17ae 2712 return 0;
2232a77c 2713}
2714
d62a17ae 2715static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip,
68e33151
CS
2716 struct ethaddr *mac, uint8_t flags,
2717 uint16_t state, int cmd)
2232a77c 2718{
d62a17ae 2719 struct {
2720 struct nlmsghdr n;
2721 struct ndmsg ndm;
2722 char buf[256];
2723 } req;
2724 int ipa_len;
2725
5895d33f 2726 struct zebra_ns *zns;
d62a17ae 2727 char buf[INET6_ADDRSTRLEN];
2728 char buf2[ETHER_ADDR_STRLEN];
009f8ad5 2729 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id);
d62a17ae 2730
009f8ad5 2731 zns = zvrf->zns;
5605ecfc 2732 memset(&req, 0, sizeof(req));
d62a17ae 2733
2734 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
2735 req.n.nlmsg_flags = NLM_F_REQUEST;
2736 if (cmd == RTM_NEWNEIGH)
2737 req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
2738 req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
2739 req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
68e33151 2740 req.ndm.ndm_state = state;
d62a17ae 2741 req.ndm.ndm_ifindex = ifp->ifindex;
2742 req.ndm.ndm_type = RTN_UNICAST;
68e33151 2743 req.ndm.ndm_flags = flags;
d62a17ae 2744
2745 ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
2746 addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
2747 if (mac)
2748 addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
2749
2750 if (IS_ZEBRA_DEBUG_KERNEL)
6fe2b0e6 2751 zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x state 0x%x",
d62a17ae 2752 nl_msg_type_to_str(cmd),
2753 nl_family_to_str(req.ndm.ndm_family), ifp->name,
2754 ifp->ifindex, ipaddr2str(ip, buf, sizeof(buf)),
2755 mac ? prefix_mac2str(mac, buf2, sizeof(buf2))
6fe2b0e6 2756 : "null", flags, state);
d62a17ae 2757
2758 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2759 0);
2232a77c 2760}
2761
d62a17ae 2762int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
a37f4598 2763 struct in_addr vtep_ip, bool sticky)
2232a77c 2764{
d63c1b18 2765 return netlink_macfdb_update(ifp, vid, mac, vtep_ip, RTM_NEWNEIGH,
d62a17ae 2766 sticky);
2232a77c 2767}
2768
d62a17ae 2769int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d63c1b18 2770 struct in_addr vtep_ip)
2232a77c 2771{
d63c1b18 2772 return netlink_macfdb_update(ifp, vid, mac, vtep_ip, RTM_DELNEIGH, 0);
2232a77c 2773}
2774
d62a17ae 2775int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
68e33151 2776 struct ethaddr *mac, uint8_t flags)
2232a77c 2777{
68e33151
CS
2778 return netlink_neigh_update2(ifp, ip, mac, flags,
2779 NUD_NOARP, RTM_NEWNEIGH);
2232a77c 2780}
2781
d62a17ae 2782int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 2783{
68e33151 2784 return netlink_neigh_update2(ifp, ip, NULL, 0, 0, RTM_DELNEIGH);
2232a77c 2785}
2786
6fe2b0e6
CS
2787int kernel_upd_neigh(struct interface *ifp, struct ipaddr *ip,
2788 struct ethaddr *mac, uint8_t flags, uint16_t state)
2789{
2790 return netlink_neigh_update2(ifp, ip, mac, flags,
2791 state, RTM_NEWNEIGH);
2792}
2793
16c628de
MS
2794/*
2795 * MPLS label forwarding table change via netlink interface, using dataplane
2796 * context information.
2797 */
fc608372 2798int netlink_mpls_multipath(int cmd, struct zebra_dplane_ctx *ctx)
16c628de
MS
2799{
2800 mpls_lse_t lse;
81793ac1 2801 const zebra_nhlfe_t *nhlfe;
16c628de
MS
2802 struct nexthop *nexthop = NULL;
2803 unsigned int nexthop_num;
2804 const char *routedesc;
2805 int route_type;
2806
2807 struct {
2808 struct nlmsghdr n;
2809 struct rtmsg r;
2810 char buf[NL_PKT_BUF_SIZE];
2811 } req;
2812
2813 memset(&req, 0, sizeof(req) - NL_PKT_BUF_SIZE);
2814
2815 /*
2816 * Count # nexthops so we can decide whether to use singlepath
2817 * or multipath case.
2818 */
2819 nexthop_num = 0;
2820 for (nhlfe = dplane_ctx_get_nhlfe(ctx); nhlfe; nhlfe = nhlfe->next) {
2821 nexthop = nhlfe->nexthop;
2822 if (!nexthop)
2823 continue;
2824 if (cmd == RTM_NEWROUTE) {
2825 /* Count all selected NHLFEs */
2826 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2827 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2828 nexthop_num++;
2829 } else { /* DEL */
2830 /* Count all installed NHLFEs */
2831 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
2832 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2833 nexthop_num++;
2834 }
2835 }
2836
2837 if ((nexthop_num == 0) ||
2838 (!dplane_ctx_get_best_nhlfe(ctx) && (cmd != RTM_DELROUTE)))
2839 return 0;
2840
2841 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2842 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
2843 req.n.nlmsg_type = cmd;
2844 req.n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid;
2845
2846 req.r.rtm_family = AF_MPLS;
2847 req.r.rtm_table = RT_TABLE_MAIN;
2848 req.r.rtm_dst_len = MPLS_LABEL_LEN_BITS;
2849 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
2850 req.r.rtm_type = RTN_UNICAST;
2851
2852 if (cmd == RTM_NEWROUTE) {
2853 /* We do a replace to handle update. */
2854 req.n.nlmsg_flags |= NLM_F_REPLACE;
2855
2856 /* set the protocol value if installing */
2857 route_type = re_type_from_lsp_type(
2858 dplane_ctx_get_best_nhlfe(ctx)->type);
2859 req.r.rtm_protocol = zebra2proto(route_type);
2860 }
2861
2862 /* Fill destination */
2863 lse = mpls_lse_encode(dplane_ctx_get_in_label(ctx), 0, 0, 1);
2864 addattr_l(&req.n, sizeof(req), RTA_DST, &lse, sizeof(mpls_lse_t));
2865
2866 /* Fill nexthops (paths) based on single-path or multipath. The paths
2867 * chosen depend on the operation.
2868 */
fc608372 2869 if (nexthop_num == 1) {
16c628de
MS
2870 routedesc = "single-path";
2871 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
2872 routedesc);
2873
2874 nexthop_num = 0;
2875 for (nhlfe = dplane_ctx_get_nhlfe(ctx);
2876 nhlfe; nhlfe = nhlfe->next) {
2877 nexthop = nhlfe->nexthop;
2878 if (!nexthop)
2879 continue;
2880
2881 if ((cmd == RTM_NEWROUTE
2882 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2883 && CHECK_FLAG(nexthop->flags,
2884 NEXTHOP_FLAG_ACTIVE)))
2885 || (cmd == RTM_DELROUTE
2886 && (CHECK_FLAG(nhlfe->flags,
2887 NHLFE_FLAG_INSTALLED)
2888 && CHECK_FLAG(nexthop->flags,
2889 NEXTHOP_FLAG_FIB)))) {
2890 /* Add the gateway */
2891 _netlink_mpls_build_singlepath(
2892 routedesc, nhlfe,
2893 &req.n, &req.r,
2894 sizeof(req), cmd);
2895
2896 nexthop_num++;
2897 break;
2898 }
2899 }
2900 } else { /* Multipath case */
2901 char buf[NL_PKT_BUF_SIZE];
2902 struct rtattr *rta = (void *)buf;
2903 struct rtnexthop *rtnh;
81793ac1 2904 const union g_addr *src1 = NULL;
16c628de
MS
2905
2906 rta->rta_type = RTA_MULTIPATH;
2907 rta->rta_len = RTA_LENGTH(0);
2908 rtnh = RTA_DATA(rta);
2909
2910 routedesc = "multipath";
2911 _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx),
2912 routedesc);
2913
2914 nexthop_num = 0;
2915 for (nhlfe = dplane_ctx_get_nhlfe(ctx);
2916 nhlfe; nhlfe = nhlfe->next) {
2917 nexthop = nhlfe->nexthop;
2918 if (!nexthop)
2919 continue;
2920
16c628de
MS
2921 if ((cmd == RTM_NEWROUTE
2922 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
2923 && CHECK_FLAG(nexthop->flags,
2924 NEXTHOP_FLAG_ACTIVE)))
2925 || (cmd == RTM_DELROUTE
2926 && (CHECK_FLAG(nhlfe->flags,
2927 NHLFE_FLAG_INSTALLED)
2928 && CHECK_FLAG(nexthop->flags,
2929 NEXTHOP_FLAG_FIB)))) {
2930 nexthop_num++;
2931
2932 /* Build the multipath */
2933 _netlink_mpls_build_multipath(routedesc, nhlfe,
2934 rta, rtnh, &req.r,
2935 &src1);
2936 rtnh = RTNH_NEXT(rtnh);
2937 }
2938 }
2939
2940 /* Add the multipath */
2941 if (rta->rta_len > RTA_LENGTH(0))
2942 addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH,
2943 RTA_DATA(rta), RTA_PAYLOAD(rta));
2944 }
2945
2946 /* Talk to netlink socket. */
2947 return netlink_talk_info(netlink_talk_filter, &req.n,
2948 dplane_ctx_get_ns(ctx), 0);
2949}
ddfeb486 2950#endif /* HAVE_NETLINK */