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