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