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