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