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