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