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