]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
zebra: Fix missing VRF flag
[mirror_frr.git] / zebra / if_netlink.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Interface looking up by netlink.
4 * Copyright (C) 1998 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #ifdef GNU_LINUX
10
11 /* The following definition is to workaround an issue in the Linux kernel
12 * header files with redefinition of 'struct in6_addr' in both
13 * netinet/in.h and linux/in6.h.
14 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
15 */
16 #define _LINUX_IN6_H
17 #define _LINUX_IF_H
18 #define _LINUX_IP_H
19
20 #include <netinet/if_ether.h>
21 #include <linux/if_bridge.h>
22 #include <linux/if_link.h>
23 #include <linux/if_tunnel.h>
24 #include <net/if_arp.h>
25 #include <linux/sockios.h>
26 #include <linux/ethtool.h>
27
28 #include "linklist.h"
29 #include "if.h"
30 #include "log.h"
31 #include "prefix.h"
32 #include "connected.h"
33 #include "table.h"
34 #include "memory.h"
35 #include "rib.h"
36 #include "frrevent.h"
37 #include "privs.h"
38 #include "nexthop.h"
39 #include "vrf.h"
40 #include "vrf_int.h"
41 #include "mpls.h"
42 #include "lib_errors.h"
43
44 #include "vty.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/zebra_vrf.h"
48 #include "zebra/rt.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/interface.h"
51 #include "zebra/debug.h"
52 #include "zebra/rtadv.h"
53 #include "zebra/zebra_ptm.h"
54 #include "zebra/zebra_mpls.h"
55 #include "zebra/kernel_netlink.h"
56 #include "zebra/rt_netlink.h"
57 #include "zebra/if_netlink.h"
58 #include "zebra/zebra_errors.h"
59 #include "zebra/zebra_vxlan.h"
60 #include "zebra/zebra_evpn_mh.h"
61 #include "zebra/zebra_l2.h"
62 #include "zebra/netconf_netlink.h"
63 #include "zebra/zebra_trace.h"
64
65 extern struct zebra_privs_t zserv_privs;
66 uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
67
68 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
69 names and ifindex values. */
70 static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
71 struct zebra_ns *zns)
72 {
73 struct interface *oifp;
74
75 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
76 && (oifp != ifp)) {
77 if (ifi_index == IFINDEX_INTERNAL)
78 flog_err(
79 EC_LIB_INTERFACE,
80 "Netlink is setting interface %s ifindex to reserved internal value %u",
81 ifp->name, ifi_index);
82 else {
83 if (IS_ZEBRA_DEBUG_KERNEL)
84 zlog_debug(
85 "interface index %d was renamed from %s to %s",
86 ifi_index, oifp->name, ifp->name);
87 if (if_is_up(oifp))
88 flog_err(
89 EC_LIB_INTERFACE,
90 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
91 ifi_index, oifp->name, ifp->name);
92 if_delete_update(&oifp);
93 }
94 }
95 if_set_index(ifp, ifi_index);
96 }
97
98 /* Utility function to parse hardware link-layer address and update ifp */
99 static void netlink_interface_update_hw_addr(struct rtattr **tb,
100 struct interface *ifp)
101 {
102 int i;
103
104 if (tb[IFLA_ADDRESS]) {
105 int hw_addr_len;
106
107 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
108
109 if (hw_addr_len > INTERFACE_HWADDR_MAX)
110 zlog_debug("Hardware address is too large: %d",
111 hw_addr_len);
112 else {
113 ifp->hw_addr_len = hw_addr_len;
114 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
115 hw_addr_len);
116
117 for (i = 0; i < hw_addr_len; i++)
118 if (ifp->hw_addr[i] != 0)
119 break;
120
121 if (i == hw_addr_len)
122 ifp->hw_addr_len = 0;
123 else
124 ifp->hw_addr_len = hw_addr_len;
125 }
126 }
127 }
128
129 static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
130 {
131 switch (hwt) {
132 case ARPHRD_ETHER:
133 return ZEBRA_LLT_ETHER;
134 case ARPHRD_EETHER:
135 return ZEBRA_LLT_EETHER;
136 case ARPHRD_AX25:
137 return ZEBRA_LLT_AX25;
138 case ARPHRD_PRONET:
139 return ZEBRA_LLT_PRONET;
140 case ARPHRD_IEEE802:
141 return ZEBRA_LLT_IEEE802;
142 case ARPHRD_ARCNET:
143 return ZEBRA_LLT_ARCNET;
144 case ARPHRD_APPLETLK:
145 return ZEBRA_LLT_APPLETLK;
146 case ARPHRD_DLCI:
147 return ZEBRA_LLT_DLCI;
148 case ARPHRD_ATM:
149 return ZEBRA_LLT_ATM;
150 case ARPHRD_METRICOM:
151 return ZEBRA_LLT_METRICOM;
152 case ARPHRD_IEEE1394:
153 return ZEBRA_LLT_IEEE1394;
154 case ARPHRD_EUI64:
155 return ZEBRA_LLT_EUI64;
156 case ARPHRD_INFINIBAND:
157 return ZEBRA_LLT_INFINIBAND;
158 case ARPHRD_SLIP:
159 return ZEBRA_LLT_SLIP;
160 case ARPHRD_CSLIP:
161 return ZEBRA_LLT_CSLIP;
162 case ARPHRD_SLIP6:
163 return ZEBRA_LLT_SLIP6;
164 case ARPHRD_CSLIP6:
165 return ZEBRA_LLT_CSLIP6;
166 case ARPHRD_RSRVD:
167 return ZEBRA_LLT_RSRVD;
168 case ARPHRD_ADAPT:
169 return ZEBRA_LLT_ADAPT;
170 case ARPHRD_ROSE:
171 return ZEBRA_LLT_ROSE;
172 case ARPHRD_X25:
173 return ZEBRA_LLT_X25;
174 case ARPHRD_PPP:
175 return ZEBRA_LLT_PPP;
176 case ARPHRD_CISCO:
177 return ZEBRA_LLT_CHDLC;
178 case ARPHRD_LAPB:
179 return ZEBRA_LLT_LAPB;
180 case ARPHRD_RAWHDLC:
181 return ZEBRA_LLT_RAWHDLC;
182 case ARPHRD_TUNNEL:
183 return ZEBRA_LLT_IPIP;
184 case ARPHRD_TUNNEL6:
185 return ZEBRA_LLT_IPIP6;
186 case ARPHRD_FRAD:
187 return ZEBRA_LLT_FRAD;
188 case ARPHRD_SKIP:
189 return ZEBRA_LLT_SKIP;
190 case ARPHRD_LOOPBACK:
191 return ZEBRA_LLT_LOOPBACK;
192 case ARPHRD_LOCALTLK:
193 return ZEBRA_LLT_LOCALTLK;
194 case ARPHRD_FDDI:
195 return ZEBRA_LLT_FDDI;
196 case ARPHRD_SIT:
197 return ZEBRA_LLT_SIT;
198 case ARPHRD_IPDDP:
199 return ZEBRA_LLT_IPDDP;
200 case ARPHRD_IPGRE:
201 return ZEBRA_LLT_IPGRE;
202 case ARPHRD_PIMREG:
203 return ZEBRA_LLT_PIMREG;
204 case ARPHRD_HIPPI:
205 return ZEBRA_LLT_HIPPI;
206 case ARPHRD_ECONET:
207 return ZEBRA_LLT_ECONET;
208 case ARPHRD_IRDA:
209 return ZEBRA_LLT_IRDA;
210 case ARPHRD_FCPP:
211 return ZEBRA_LLT_FCPP;
212 case ARPHRD_FCAL:
213 return ZEBRA_LLT_FCAL;
214 case ARPHRD_FCPL:
215 return ZEBRA_LLT_FCPL;
216 case ARPHRD_FCFABRIC:
217 return ZEBRA_LLT_FCFABRIC;
218 case ARPHRD_IEEE802_TR:
219 return ZEBRA_LLT_IEEE802_TR;
220 case ARPHRD_IEEE80211:
221 return ZEBRA_LLT_IEEE80211;
222 #ifdef ARPHRD_IEEE802154
223 case ARPHRD_IEEE802154:
224 return ZEBRA_LLT_IEEE802154;
225 #endif
226 #ifdef ARPHRD_IP6GRE
227 case ARPHRD_IP6GRE:
228 return ZEBRA_LLT_IP6GRE;
229 #endif
230 #ifdef ARPHRD_IEEE802154_PHY
231 case ARPHRD_IEEE802154_PHY:
232 return ZEBRA_LLT_IEEE802154_PHY;
233 #endif
234
235 default:
236 return ZEBRA_LLT_UNKNOWN;
237 }
238 }
239
240 static inline void zebra_if_set_ziftype(struct interface *ifp,
241 enum zebra_iftype zif_type,
242 enum zebra_slave_iftype zif_slave_type)
243 {
244 struct zebra_if *zif;
245
246 zif = (struct zebra_if *)ifp->info;
247 zif->zif_slave_type = zif_slave_type;
248
249 if (zif->zif_type != zif_type) {
250 zif->zif_type = zif_type;
251 /* If the if_type has been set to bond initialize ES info
252 * against it. XXX - note that we don't handle the case where
253 * a zif changes from bond to non-bond; it is really
254 * an unexpected/error condition.
255 */
256 zebra_evpn_if_init(zif);
257 }
258 }
259
260 static void netlink_determine_zebra_iftype(const char *kind,
261 enum zebra_iftype *zif_type)
262 {
263 *zif_type = ZEBRA_IF_OTHER;
264
265 if (!kind)
266 return;
267
268 if (strcmp(kind, "vrf") == 0)
269 *zif_type = ZEBRA_IF_VRF;
270 else if (strcmp(kind, "bridge") == 0)
271 *zif_type = ZEBRA_IF_BRIDGE;
272 else if (strcmp(kind, "vlan") == 0)
273 *zif_type = ZEBRA_IF_VLAN;
274 else if (strcmp(kind, "vxlan") == 0)
275 *zif_type = ZEBRA_IF_VXLAN;
276 else if (strcmp(kind, "macvlan") == 0)
277 *zif_type = ZEBRA_IF_MACVLAN;
278 else if (strcmp(kind, "veth") == 0)
279 *zif_type = ZEBRA_IF_VETH;
280 else if (strcmp(kind, "bond") == 0)
281 *zif_type = ZEBRA_IF_BOND;
282 else if (strcmp(kind, "bond_slave") == 0)
283 *zif_type = ZEBRA_IF_BOND_SLAVE;
284 else if (strcmp(kind, "gre") == 0)
285 *zif_type = ZEBRA_IF_GRE;
286 }
287
288 static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
289 uint32_t ns_id, const char *name)
290 {
291 struct ifinfomsg *ifi;
292 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
293 struct rtattr *attr[IFLA_VRF_MAX + 1];
294 struct vrf *vrf = NULL;
295 struct zebra_vrf *zvrf;
296 uint32_t nl_table_id;
297
298 ifi = NLMSG_DATA(h);
299
300 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
301
302 if (!linkinfo[IFLA_INFO_DATA]) {
303 if (IS_ZEBRA_DEBUG_KERNEL)
304 zlog_debug(
305 "%s: IFLA_INFO_DATA missing from VRF message: %s",
306 __func__, name);
307 return;
308 }
309
310 netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
311 linkinfo[IFLA_INFO_DATA]);
312 if (!attr[IFLA_VRF_TABLE]) {
313 if (IS_ZEBRA_DEBUG_KERNEL)
314 zlog_debug(
315 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
316 __func__, name);
317 return;
318 }
319
320 nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
321
322 if (h->nlmsg_type == RTM_NEWLINK) {
323 if (IS_ZEBRA_DEBUG_KERNEL)
324 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
325 ifi->ifi_index, nl_table_id);
326
327 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
328 vrf_id_t exist_id;
329
330 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
331 if (exist_id != VRF_DEFAULT) {
332 vrf = vrf_lookup_by_id(exist_id);
333
334 flog_err(
335 EC_ZEBRA_VRF_MISCONFIGURED,
336 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
337 name, ifi->ifi_index, vrf->name);
338 exit(-1);
339 }
340 }
341
342 vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
343 if (!vrf) {
344 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
345 name, ifi->ifi_index);
346 return;
347 }
348
349 /*
350 * This is the only place that we get the actual kernel table_id
351 * being used. We need it to set the table_id of the routes
352 * we are passing to the kernel.... And to throw some totally
353 * awesome parties. that too.
354 *
355 * At this point we *must* have a zvrf because the vrf_create
356 * callback creates one. We *must* set the table id
357 * before the vrf_enable because of( at the very least )
358 * static routes being delayed for installation until
359 * during the vrf_enable callbacks.
360 */
361 zvrf = (struct zebra_vrf *)vrf->info;
362 zvrf->table_id = nl_table_id;
363
364 /* Enable the created VRF. */
365 if (!vrf_enable(vrf)) {
366 flog_err(EC_LIB_INTERFACE,
367 "Failed to enable VRF %s id %u", name,
368 ifi->ifi_index);
369 return;
370 }
371
372 } else // h->nlmsg_type == RTM_DELLINK
373 {
374 if (IS_ZEBRA_DEBUG_KERNEL)
375 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
376 ifi->ifi_index);
377
378 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
379
380 if (!vrf) {
381 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
382 __func__);
383 return;
384 }
385
386 vrf_delete(vrf);
387 }
388 }
389
390 static uint32_t get_iflink_speed(struct interface *interface, int *error)
391 {
392 struct ifreq ifdata;
393 struct ethtool_cmd ecmd;
394 int sd;
395 int rc;
396 const char *ifname = interface->name;
397
398 if (error)
399 *error = 0;
400 /* initialize struct */
401 memset(&ifdata, 0, sizeof(ifdata));
402
403 /* set interface name */
404 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
405
406 /* initialize ethtool interface */
407 memset(&ecmd, 0, sizeof(ecmd));
408 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
409 ifdata.ifr_data = (caddr_t)&ecmd;
410
411 /* use ioctl to get speed of an interface */
412 frr_with_privs(&zserv_privs) {
413 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
414 interface->vrf->vrf_id, NULL);
415 if (sd < 0) {
416 if (IS_ZEBRA_DEBUG_KERNEL)
417 zlog_debug("Failure to read interface %s speed: %d %s",
418 ifname, errno, safe_strerror(errno));
419 /* no vrf socket creation may probably mean vrf issue */
420 if (error)
421 *error = -1;
422 return 0;
423 }
424 /* Get the current link state for the interface */
425 rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
426 (char *)&ifdata);
427 }
428 if (rc < 0) {
429 if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
430 zlog_debug(
431 "IOCTL failure to read interface %s speed: %d %s",
432 ifname, errno, safe_strerror(errno));
433 /* no device means interface unreachable */
434 if (errno == ENODEV && error)
435 *error = -1;
436 ecmd.speed_hi = 0;
437 ecmd.speed = 0;
438 }
439
440 close(sd);
441
442 return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
443 }
444
445 uint32_t kernel_get_speed(struct interface *ifp, int *error)
446 {
447 return get_iflink_speed(ifp, error);
448 }
449
450 static ssize_t
451 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
452 size_t buflen)
453 {
454 struct {
455 struct nlmsghdr n;
456 struct ifinfomsg ifi;
457 char buf[];
458 } *req = buf;
459 uint32_t link_idx;
460 unsigned int mtu;
461 struct rtattr *rta_info, *rta_data;
462 const struct zebra_l2info_gre *gre_info;
463
464 if (buflen < sizeof(*req))
465 return 0;
466 memset(req, 0, sizeof(*req));
467
468 req->n.nlmsg_type = RTM_NEWLINK;
469 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
470 req->n.nlmsg_flags = NLM_F_REQUEST;
471
472 req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
473
474 gre_info = dplane_ctx_gre_get_info(ctx);
475 if (!gre_info)
476 return 0;
477
478 req->ifi.ifi_change = 0xFFFFFFFF;
479 link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
480 mtu = dplane_ctx_gre_get_mtu(ctx);
481
482 if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
483 return 0;
484
485 rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
486 if (!rta_info)
487 return 0;
488
489 if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
490 return 0;
491
492 rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
493 if (!rta_data)
494 return 0;
495
496 if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
497 return 0;
498
499 if (gre_info->vtep_ip.s_addr &&
500 !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
501 gre_info->vtep_ip.s_addr))
502 return 0;
503
504 if (gre_info->vtep_ip_remote.s_addr &&
505 !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
506 gre_info->vtep_ip_remote.s_addr))
507 return 0;
508
509 if (gre_info->ikey &&
510 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
511 gre_info->ikey))
512 return 0;
513 if (gre_info->okey &&
514 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
515 gre_info->okey))
516 return 0;
517
518 nl_attr_nest_end(&req->n, rta_data);
519 nl_attr_nest_end(&req->n, rta_info);
520
521 return NLMSG_ALIGN(req->n.nlmsg_len);
522 }
523
524 static int netlink_extract_bridge_info(struct rtattr *link_data,
525 struct zebra_l2info_bridge *bridge_info)
526 {
527 struct rtattr *attr[IFLA_BR_MAX + 1];
528
529 memset(bridge_info, 0, sizeof(*bridge_info));
530 netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
531 if (attr[IFLA_BR_VLAN_FILTERING])
532 bridge_info->bridge.vlan_aware =
533 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
534 return 0;
535 }
536
537 static int netlink_extract_vlan_info(struct rtattr *link_data,
538 struct zebra_l2info_vlan *vlan_info)
539 {
540 struct rtattr *attr[IFLA_VLAN_MAX + 1];
541 vlanid_t vid_in_msg;
542
543 memset(vlan_info, 0, sizeof(*vlan_info));
544 netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
545 if (!attr[IFLA_VLAN_ID]) {
546 if (IS_ZEBRA_DEBUG_KERNEL)
547 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
548 return -1;
549 }
550
551 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
552 vlan_info->vid = vid_in_msg;
553 return 0;
554 }
555
556 static int netlink_extract_gre_info(struct rtattr *link_data,
557 struct zebra_l2info_gre *gre_info)
558 {
559 struct rtattr *attr[IFLA_GRE_MAX + 1];
560
561 memset(gre_info, 0, sizeof(*gre_info));
562 memset(attr, 0, sizeof(attr));
563 netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
564
565 if (!attr[IFLA_GRE_LOCAL]) {
566 if (IS_ZEBRA_DEBUG_KERNEL)
567 zlog_debug(
568 "IFLA_GRE_LOCAL missing from GRE IF message");
569 } else
570 gre_info->vtep_ip =
571 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
572 if (!attr[IFLA_GRE_REMOTE]) {
573 if (IS_ZEBRA_DEBUG_KERNEL)
574 zlog_debug(
575 "IFLA_GRE_REMOTE missing from GRE IF message");
576 } else
577 gre_info->vtep_ip_remote =
578 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
579
580 if (!attr[IFLA_GRE_LINK]) {
581 if (IS_ZEBRA_DEBUG_KERNEL)
582 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
583 } else {
584 gre_info->ifindex_link =
585 *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
586 if (IS_ZEBRA_DEBUG_KERNEL)
587 zlog_debug("IFLA_GRE_LINK obtained is %u",
588 gre_info->ifindex_link);
589 }
590 if (attr[IFLA_GRE_IKEY])
591 gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
592 if (attr[IFLA_GRE_OKEY])
593 gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
594 return 0;
595 }
596
597 static int netlink_extract_vxlan_info(struct rtattr *link_data,
598 struct zebra_l2info_vxlan *vxl_info)
599 {
600 uint8_t svd = 0;
601 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
602 vni_t vni_in_msg;
603 struct in_addr vtep_ip_in_msg;
604 ifindex_t ifindex_link;
605
606 memset(vxl_info, 0, sizeof(*vxl_info));
607 netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
608 if (attr[IFLA_VXLAN_COLLECT_METADATA]) {
609 svd = *(uint8_t *)RTA_DATA(attr[IFLA_VXLAN_COLLECT_METADATA]);
610 if (IS_ZEBRA_DEBUG_KERNEL)
611 zlog_debug(
612 "IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
613 svd);
614 }
615
616 if (!svd) {
617 /*
618 * In case of svd we will not get vni info directly from the
619 * device
620 */
621 if (!attr[IFLA_VXLAN_ID]) {
622 if (IS_ZEBRA_DEBUG_KERNEL)
623 zlog_debug(
624 "IFLA_VXLAN_ID missing from VXLAN IF message");
625 return -1;
626 }
627
628 vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_VNI;
629 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
630 vxl_info->vni_info.vni.vni = vni_in_msg;
631 } else {
632 vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_SVD;
633 }
634
635 if (!attr[IFLA_VXLAN_LOCAL]) {
636 if (IS_ZEBRA_DEBUG_KERNEL)
637 zlog_debug(
638 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
639 } else {
640 vtep_ip_in_msg =
641 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
642 vxl_info->vtep_ip = vtep_ip_in_msg;
643 }
644
645 if (attr[IFLA_VXLAN_GROUP]) {
646 if (!svd)
647 vxl_info->vni_info.vni.mcast_grp =
648 *(struct in_addr *)RTA_DATA(
649 attr[IFLA_VXLAN_GROUP]);
650 }
651
652 if (!attr[IFLA_VXLAN_LINK]) {
653 if (IS_ZEBRA_DEBUG_KERNEL)
654 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
655 } else {
656 ifindex_link =
657 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
658 vxl_info->ifindex_link = ifindex_link;
659 }
660 return 0;
661 }
662
663 /*
664 * Extract and save L2 params (of interest) for an interface. When a
665 * bridge interface is added or updated, take further actions to map
666 * its members. Likewise, for VxLAN interface.
667 */
668 static void netlink_interface_update_l2info(struct interface *ifp,
669 struct rtattr *link_data, int add,
670 ns_id_t link_nsid)
671 {
672 if (!link_data)
673 return;
674
675 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
676 struct zebra_l2info_bridge bridge_info;
677
678 netlink_extract_bridge_info(link_data, &bridge_info);
679 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
680 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
681 struct zebra_l2info_vlan vlan_info;
682
683 netlink_extract_vlan_info(link_data, &vlan_info);
684 zebra_l2_vlanif_update(ifp, &vlan_info);
685 zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
686 !!if_is_operative(ifp));
687 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
688 struct zebra_l2info_vxlan vxlan_info;
689
690 netlink_extract_vxlan_info(link_data, &vxlan_info);
691 vxlan_info.link_nsid = link_nsid;
692 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
693 if (link_nsid != NS_UNKNOWN &&
694 vxlan_info.ifindex_link)
695 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
696 link_nsid);
697 } else if (IS_ZEBRA_IF_GRE(ifp)) {
698 struct zebra_l2info_gre gre_info;
699
700 netlink_extract_gre_info(link_data, &gre_info);
701 gre_info.link_nsid = link_nsid;
702 zebra_l2_greif_add_update(ifp, &gre_info, add);
703 if (link_nsid != NS_UNKNOWN &&
704 gre_info.ifindex_link)
705 zebra_if_update_link(ifp, gre_info.ifindex_link,
706 link_nsid);
707 }
708 }
709
710 static int netlink_bridge_vxlan_vlan_vni_map_update(struct interface *ifp,
711 struct rtattr *af_spec)
712 {
713 int rem;
714 vni_t vni_id;
715 vlanid_t vid;
716 uint16_t flags;
717 struct rtattr *i;
718 struct zebra_vxlan_vni vni;
719 struct zebra_vxlan_vni *vnip;
720 struct hash *vni_table = NULL;
721 struct zebra_vxlan_vni vni_end;
722 struct zebra_vxlan_vni vni_start;
723 struct rtattr *aftb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
724
725 memset(&vni_start, 0, sizeof(vni_start));
726 memset(&vni_end, 0, sizeof(vni_end));
727
728 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); RTA_OK(i, rem);
729 i = RTA_NEXT(i, rem)) {
730
731 if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO)
732 continue;
733
734 memset(aftb, 0, sizeof(aftb));
735 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
736 i);
737 if (!aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
738 !aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
739 /* vlan-vni info missing */
740 return 0;
741
742 flags = 0;
743 memset(&vni, 0, sizeof(vni));
744
745 vni.vni = *(vni_t *)RTA_DATA(aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
746 vni.access_vlan = *(vlanid_t *)RTA_DATA(
747 aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
748
749 if (aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
750 flags = *(uint16_t *)RTA_DATA(
751 aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
752
753 if (flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
754 vni_start = vni;
755 continue;
756 }
757
758 if (flags & BRIDGE_VLAN_INFO_RANGE_END)
759 vni_end = vni;
760
761 if (!(flags & BRIDGE_VLAN_INFO_RANGE_END)) {
762 vni_start = vni;
763 vni_end = vni;
764 }
765
766 if (IS_ZEBRA_DEBUG_KERNEL)
767 zlog_debug(
768 "Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
769 vni_start.access_vlan, vni_end.access_vlan,
770 vni_start.vni, vni_end.vni, ifp->name,
771 ifp->ifindex);
772
773 if (!vni_table) {
774 vni_table = zebra_vxlan_vni_table_create();
775 if (!vni_table)
776 return 0;
777 }
778
779 for (vid = vni_start.access_vlan, vni_id = vni_start.vni;
780 vid <= vni_end.access_vlan; vid++, vni_id++) {
781
782 memset(&vni, 0, sizeof(vni));
783 vni.vni = vni_id;
784 vni.access_vlan = vid;
785 vnip = hash_get(vni_table, &vni, zebra_vxlan_vni_alloc);
786 if (!vnip)
787 return 0;
788 }
789
790 memset(&vni_start, 0, sizeof(vni_start));
791 memset(&vni_end, 0, sizeof(vni_end));
792 }
793
794 if (vni_table)
795 zebra_vxlan_if_vni_table_add_update(ifp, vni_table);
796
797 return 0;
798 }
799
800 static int netlink_bridge_vxlan_update(struct interface *ifp,
801 struct rtattr *af_spec)
802 {
803 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
804 struct bridge_vlan_info *vinfo;
805 struct zebra_if *zif;
806 vlanid_t access_vlan;
807
808 if (!af_spec)
809 return 0;
810
811 zif = (struct zebra_if *)ifp->info;
812
813 /* Single vxlan devices has vni-vlan range to update */
814 if (IS_ZEBRA_VXLAN_IF_SVD(zif))
815 return netlink_bridge_vxlan_vlan_vni_map_update(ifp, af_spec);
816
817 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
818 * only 1 access VLAN is accepted.
819 */
820 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
821 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
822 return 0;
823
824 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
825 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
826 return 0;
827
828 access_vlan = (vlanid_t)vinfo->vid;
829 if (IS_ZEBRA_DEBUG_KERNEL)
830 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
831 ifp->name, ifp->ifindex);
832 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
833 return 0;
834 }
835
836 static void netlink_bridge_vlan_update(struct interface *ifp,
837 struct rtattr *af_spec)
838 {
839 struct rtattr *i;
840 int rem;
841 uint16_t vid_range_start = 0;
842 struct zebra_if *zif;
843 bitfield_t old_vlan_bitmap;
844 struct bridge_vlan_info *vinfo;
845
846 zif = (struct zebra_if *)ifp->info;
847
848 /* cache the old bitmap addrs */
849 old_vlan_bitmap = zif->vlan_bitmap;
850 /* create a new bitmap space for re-eval */
851 bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
852
853 if (af_spec) {
854 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
855 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
856
857 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
858 continue;
859
860 vinfo = RTA_DATA(i);
861
862 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
863 vid_range_start = vinfo->vid;
864 continue;
865 }
866
867 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
868 vid_range_start = vinfo->vid;
869
870 zebra_vlan_bitmap_compute(ifp, vid_range_start,
871 vinfo->vid);
872 }
873 }
874
875 zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
876
877 bf_free(old_vlan_bitmap);
878 }
879
880 static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
881 int startup)
882 {
883 char *name = NULL;
884 struct ifinfomsg *ifi;
885 struct rtattr *tb[IFLA_MAX + 1];
886 struct interface *ifp;
887 struct zebra_if *zif;
888 struct rtattr *af_spec;
889
890 /* Fetch name and ifindex */
891 ifi = NLMSG_DATA(h);
892 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
893
894 if (tb[IFLA_IFNAME] == NULL)
895 return -1;
896 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
897
898 /* The interface should already be known, if not discard. */
899 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
900 if (!ifp) {
901 zlog_debug("Cannot find bridge IF %s(%u)", name,
902 ifi->ifi_index);
903 return 0;
904 }
905
906 /* We are only interested in the access VLAN i.e., AF_SPEC */
907 af_spec = tb[IFLA_AF_SPEC];
908
909 if (IS_ZEBRA_IF_VXLAN(ifp))
910 return netlink_bridge_vxlan_update(ifp, af_spec);
911
912 /* build vlan bitmap associated with this interface if that
913 * device type is interested in the vlans
914 */
915 zif = (struct zebra_if *)ifp->info;
916 if (bf_is_inited(zif->vlan_bitmap))
917 netlink_bridge_vlan_update(ifp, af_spec);
918
919 return 0;
920 }
921
922 static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
923 {
924 /* This shouldn't be possible */
925 assert(frr_protodown_r_bit < 32);
926 return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
927 }
928
929 /*
930 * Process interface protodown dplane update.
931 *
932 * If the interface is an es bond member then it must follow EVPN's
933 * protodown setting.
934 */
935 static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
936 struct rtattr **tb)
937 {
938 bool protodown;
939 bool old_protodown;
940 uint32_t rc_bitfield = 0;
941 struct rtattr *pd_reason_info[IFLA_MAX + 1];
942
943 protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
944
945 if (tb[IFLA_PROTO_DOWN_REASON]) {
946 netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
947 tb[IFLA_PROTO_DOWN_REASON]);
948
949 if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
950 rc_bitfield = *(uint32_t *)RTA_DATA(
951 pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
952 }
953
954 /*
955 * Set our reason code to note it wasn't us.
956 * If the reason we got from the kernel is ONLY frr though, don't
957 * set it.
958 */
959 COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
960 protodown && rc_bitfield &&
961 !is_if_protodown_reason_only_frr(rc_bitfield));
962
963
964 old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
965 if (protodown == old_protodown)
966 return;
967
968 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
969 zlog_debug("interface %s dplane change, protdown %s",
970 zif->ifp->name, protodown ? "on" : "off");
971
972 /* Set protodown, respectively */
973 COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);
974
975 if (zebra_evpn_is_es_bond_member(zif->ifp)) {
976 /* Check it's not already being sent to the dplane first */
977 if (protodown &&
978 CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
979 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
980 zlog_debug(
981 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
982 zif->ifp->name);
983 return;
984 }
985
986 if (!protodown &&
987 CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
988 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
989 zlog_debug(
990 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
991 zif->ifp->name);
992 return;
993 }
994
995 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
996 zlog_debug(
997 "bond mbr %s reinstate protodown %s in the dplane",
998 zif->ifp->name, old_protodown ? "on" : "off");
999
1000 if (old_protodown)
1001 SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
1002 else
1003 SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
1004
1005 dplane_intf_update(zif->ifp);
1006 }
1007 }
1008
1009 static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
1010 {
1011 uint8_t bypass = 0;
1012 struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
1013
1014 netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
1015 linkinfo[IFLA_INFO_SLAVE_DATA]);
1016 if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
1017 bypass = *(uint8_t *)RTA_DATA(
1018 mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
1019
1020 return bypass;
1021 }
1022
1023 /*
1024 * Only called at startup to cleanup leftover protodown reasons we may
1025 * have not cleaned up. We leave protodown set though.
1026 */
1027 static void if_sweep_protodown(struct zebra_if *zif)
1028 {
1029 bool protodown;
1030
1031 protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
1032
1033 if (!protodown)
1034 return;
1035
1036 if (IS_ZEBRA_DEBUG_KERNEL)
1037 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
1038 zif->ifp->name, protodown ? "on" : "off",
1039 zif->protodown_rc);
1040
1041 /* Only clear our reason codes, leave external if it was set */
1042 UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
1043 dplane_intf_update(zif->ifp);
1044 }
1045
1046 /*
1047 * Called from interface_lookup_netlink(). This function is only used
1048 * during bootstrap.
1049 */
1050 static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1051 {
1052 int len;
1053 struct ifinfomsg *ifi;
1054 struct rtattr *tb[IFLA_MAX + 1];
1055 struct rtattr *linkinfo[IFLA_MAX + 1];
1056 struct interface *ifp;
1057 char *name = NULL;
1058 char *kind = NULL;
1059 char *desc = NULL;
1060 char *slave_kind = NULL;
1061 struct zebra_ns *zns = NULL;
1062 vrf_id_t vrf_id = VRF_DEFAULT;
1063 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1064 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1065 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1066 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1067 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1068 struct zebra_if *zif;
1069 ns_id_t link_nsid = ns_id;
1070 uint8_t bypass = 0;
1071
1072 frrtrace(3, frr_zebra, netlink_interface, h, ns_id, startup);
1073
1074 zns = zebra_ns_lookup(ns_id);
1075 ifi = NLMSG_DATA(h);
1076
1077 if (h->nlmsg_type != RTM_NEWLINK)
1078 return 0;
1079
1080 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1081 if (len < 0) {
1082 zlog_err(
1083 "%s: Message received from netlink is of a broken size: %d %zu",
1084 __func__, h->nlmsg_len,
1085 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
1086 return -1;
1087 }
1088
1089 /* We are interested in some AF_BRIDGE notifications. */
1090 if (ifi->ifi_family == AF_BRIDGE)
1091 return netlink_bridge_interface(h, len, ns_id, startup);
1092
1093 /* Looking up interface name. */
1094 memset(linkinfo, 0, sizeof(linkinfo));
1095 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
1096 NLA_F_NESTED);
1097
1098 /* check for wireless messages to ignore */
1099 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1100 if (IS_ZEBRA_DEBUG_KERNEL)
1101 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1102 __func__);
1103 return 0;
1104 }
1105
1106 if (tb[IFLA_IFNAME] == NULL)
1107 return -1;
1108 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1109
1110 if (tb[IFLA_IFALIAS])
1111 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1112
1113 if (tb[IFLA_LINKINFO]) {
1114 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1115 tb[IFLA_LINKINFO]);
1116
1117 if (linkinfo[IFLA_INFO_KIND])
1118 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1119
1120 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1121 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1122
1123 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
1124 netlink_determine_zebra_iftype("bond_slave", &zif_type);
1125 else
1126 netlink_determine_zebra_iftype(kind, &zif_type);
1127 }
1128
1129 /* If VRF, create the VRF structure itself. */
1130 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1131 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1132 vrf_id = (vrf_id_t)ifi->ifi_index;
1133 }
1134
1135 if (tb[IFLA_MASTER]) {
1136 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1137 && !vrf_is_backend_netns()) {
1138 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
1139 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
1140 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
1141 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1142 bridge_ifindex =
1143 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1144 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
1145 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1146 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1147 bypass = netlink_parse_lacp_bypass(linkinfo);
1148 } else
1149 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1150 }
1151 if (vrf_is_backend_netns())
1152 vrf_id = (vrf_id_t)ns_id;
1153
1154 /* If linking to another interface, note it. */
1155 if (tb[IFLA_LINK])
1156 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1157
1158 if (tb[IFLA_LINK_NETNSID]) {
1159 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
1160 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1161 }
1162
1163 ifp = if_get_by_name(name, vrf_id, NULL);
1164 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
1165
1166 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1167 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
1168 ifp->metric = 0;
1169 ifp->speed = get_iflink_speed(ifp, NULL);
1170 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1171
1172 /* Set zebra interface type */
1173 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1174 if (IS_ZEBRA_IF_VRF(ifp))
1175 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1176
1177 /*
1178 * Just set the @link/lower-device ifindex. During nldump interfaces are
1179 * not ordered in any fashion so we may end up getting upper devices
1180 * before lower devices. We will setup the real linkage once the dump
1181 * is complete.
1182 */
1183 zif = (struct zebra_if *)ifp->info;
1184 zif->link_ifindex = link_ifindex;
1185
1186 if (desc) {
1187 XFREE(MTYPE_ZIF_DESC, zif->desc);
1188 zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
1189 }
1190
1191 /* Hardware type and address. */
1192 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
1193
1194 netlink_interface_update_hw_addr(tb, ifp);
1195
1196 if_add_update(ifp);
1197
1198 /* Extract and save L2 interface information, take additional actions.
1199 */
1200 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1201 1, link_nsid);
1202 if (IS_ZEBRA_IF_BOND(ifp))
1203 zebra_l2if_update_bond(ifp, true);
1204 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1205 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1206 ZEBRA_BRIDGE_NO_ACTION);
1207 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1208 zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
1209
1210 if (tb[IFLA_PROTO_DOWN]) {
1211 netlink_proc_dplane_if_protodown(zif, tb);
1212 if_sweep_protodown(zif);
1213 }
1214
1215 return 0;
1216 }
1217
1218 /* Request for specific interface or address information from the kernel */
1219 static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1220 int type, uint32_t filter_mask)
1221 {
1222 struct {
1223 struct nlmsghdr n;
1224 struct ifinfomsg ifm;
1225 char buf[256];
1226 } req;
1227
1228 frrtrace(4, frr_zebra, netlink_request_intf_addr, netlink_cmd, family,
1229 type, filter_mask);
1230
1231 /* Form the request, specifying filter (rtattr) if needed. */
1232 memset(&req, 0, sizeof(req));
1233 req.n.nlmsg_type = type;
1234 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
1235 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1236 req.ifm.ifi_family = family;
1237
1238 /* Include filter, if specified. */
1239 if (filter_mask)
1240 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
1241
1242 return netlink_request(netlink_cmd, &req);
1243 }
1244
1245 enum netlink_msg_status
1246 netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1247 {
1248 enum dplane_op_e op;
1249 enum netlink_msg_status ret;
1250
1251 op = dplane_ctx_get_op(ctx);
1252 assert(op == DPLANE_OP_GRE_SET);
1253
1254 ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1255
1256 return ret;
1257 }
1258
1259 /* Interface lookup by netlink socket. */
1260 int interface_lookup_netlink(struct zebra_ns *zns)
1261 {
1262 int ret;
1263 struct zebra_dplane_info dp_info;
1264 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1265
1266 /* Capture key info from ns struct */
1267 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1268
1269 /* Get interface information. */
1270 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
1271 if (ret < 0)
1272 return ret;
1273 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1274 true);
1275 if (ret < 0)
1276 return ret;
1277
1278 /* Get interface information - for bridge interfaces. */
1279 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
1280 RTEXT_FILTER_BRVLAN);
1281 if (ret < 0)
1282 return ret;
1283 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1284 true);
1285 if (ret < 0)
1286 return ret;
1287
1288 /*
1289 * So netlink_tunneldump_read will initiate a request
1290 * per tunnel to get data. If we are on a kernel that
1291 * does not support this then we will get X error messages
1292 * (one per tunnel request )back which netlink_parse_info will
1293 * stop after the first one. So we need to read equivalent
1294 * error messages per tunnel then we can continue.
1295 * if we do not gather all the read failures then
1296 * later requests will not work right.
1297 */
1298 ret = netlink_tunneldump_read(zns);
1299 if (ret < 0)
1300 return ret;
1301
1302 /* fixup linkages */
1303 zebra_if_update_all_links(zns);
1304 return 0;
1305 }
1306
1307 /**
1308 * interface_addr_lookup_netlink() - Look up interface addresses
1309 *
1310 * @zns: Zebra netlink socket
1311 * Return: Result status
1312 */
1313 static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1314 {
1315 int ret;
1316 struct zebra_dplane_info dp_info;
1317 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1318
1319 /* Capture key info from ns struct */
1320 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1321
1322 /* Get IPv4 address of the interfaces. */
1323 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
1324 if (ret < 0)
1325 return ret;
1326 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1327 0, true);
1328 if (ret < 0)
1329 return ret;
1330
1331 /* Get IPv6 address of the interfaces. */
1332 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
1333 if (ret < 0)
1334 return ret;
1335 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1336 0, true);
1337 if (ret < 0)
1338 return ret;
1339
1340 return 0;
1341 }
1342
1343 int kernel_interface_set_master(struct interface *master,
1344 struct interface *slave)
1345 {
1346 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1347
1348 struct {
1349 struct nlmsghdr n;
1350 struct ifinfomsg ifa;
1351 char buf[NL_PKT_BUF_SIZE];
1352 } req;
1353
1354 memset(&req, 0, sizeof(req));
1355
1356 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1357 req.n.nlmsg_flags = NLM_F_REQUEST;
1358 req.n.nlmsg_type = RTM_SETLINK;
1359 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1360
1361 req.ifa.ifi_index = slave->ifindex;
1362
1363 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1364 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
1365
1366 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1367 false);
1368 }
1369
1370 /* Interface address modification. */
1371 static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1372 void *buf, size_t buflen)
1373 {
1374 int bytelen;
1375 const struct prefix *p;
1376 int cmd;
1377 const char *label;
1378
1379 struct {
1380 struct nlmsghdr n;
1381 struct ifaddrmsg ifa;
1382 char buf[0];
1383 } *req = buf;
1384
1385 if (buflen < sizeof(*req))
1386 return 0;
1387
1388 p = dplane_ctx_get_intf_addr(ctx);
1389 memset(req, 0, sizeof(*req));
1390
1391 bytelen = (p->family == AF_INET ? 4 : 16);
1392
1393 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1394 req->n.nlmsg_flags = NLM_F_REQUEST;
1395
1396 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1397 cmd = RTM_NEWADDR;
1398 else
1399 cmd = RTM_DELADDR;
1400
1401 req->n.nlmsg_type = cmd;
1402 req->ifa.ifa_family = p->family;
1403
1404 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1405
1406 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1407 return 0;
1408
1409 if (p->family == AF_INET) {
1410 if (dplane_ctx_intf_is_connected(ctx)) {
1411 p = dplane_ctx_get_intf_dest(ctx);
1412 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1413 &p->u.prefix, bytelen))
1414 return 0;
1415 } else if (cmd == RTM_NEWADDR) {
1416 struct in_addr broad = {
1417 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1418 p->prefixlen)
1419 };
1420 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1421 bytelen))
1422 return 0;
1423 }
1424 }
1425
1426 /* p is now either address or destination/bcast addr */
1427 req->ifa.ifa_prefixlen = p->prefixlen;
1428
1429 if (dplane_ctx_intf_is_secondary(ctx))
1430 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1431
1432 if (dplane_ctx_intf_has_label(ctx)) {
1433 label = dplane_ctx_get_intf_label(ctx);
1434 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1435 strlen(label) + 1))
1436 return 0;
1437 }
1438
1439 return NLMSG_ALIGN(req->n.nlmsg_len);
1440 }
1441
1442 enum netlink_msg_status
1443 netlink_put_address_update_msg(struct nl_batch *bth,
1444 struct zebra_dplane_ctx *ctx)
1445 {
1446 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1447 false);
1448 }
1449
1450 static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1451 size_t buflen)
1452 {
1453 enum dplane_op_e op;
1454 int cmd = 0;
1455
1456 op = dplane_ctx_get_op(ctx);
1457
1458 switch (op) {
1459 case DPLANE_OP_INTF_UPDATE:
1460 cmd = RTM_SETLINK;
1461 break;
1462 case DPLANE_OP_INTF_INSTALL:
1463 cmd = RTM_NEWLINK;
1464 break;
1465 case DPLANE_OP_INTF_DELETE:
1466 cmd = RTM_DELLINK;
1467 break;
1468 case DPLANE_OP_NONE:
1469 case DPLANE_OP_ROUTE_INSTALL:
1470 case DPLANE_OP_ROUTE_UPDATE:
1471 case DPLANE_OP_ROUTE_DELETE:
1472 case DPLANE_OP_ROUTE_NOTIFY:
1473 case DPLANE_OP_NH_INSTALL:
1474 case DPLANE_OP_NH_UPDATE:
1475 case DPLANE_OP_NH_DELETE:
1476 case DPLANE_OP_LSP_INSTALL:
1477 case DPLANE_OP_LSP_DELETE:
1478 case DPLANE_OP_LSP_NOTIFY:
1479 case DPLANE_OP_LSP_UPDATE:
1480 case DPLANE_OP_PW_INSTALL:
1481 case DPLANE_OP_PW_UNINSTALL:
1482 case DPLANE_OP_SYS_ROUTE_ADD:
1483 case DPLANE_OP_SYS_ROUTE_DELETE:
1484 case DPLANE_OP_ADDR_INSTALL:
1485 case DPLANE_OP_ADDR_UNINSTALL:
1486 case DPLANE_OP_MAC_INSTALL:
1487 case DPLANE_OP_MAC_DELETE:
1488 case DPLANE_OP_NEIGH_INSTALL:
1489 case DPLANE_OP_NEIGH_UPDATE:
1490 case DPLANE_OP_NEIGH_DELETE:
1491 case DPLANE_OP_NEIGH_DISCOVER:
1492 case DPLANE_OP_VTEP_ADD:
1493 case DPLANE_OP_VTEP_DELETE:
1494 case DPLANE_OP_RULE_ADD:
1495 case DPLANE_OP_RULE_DELETE:
1496 case DPLANE_OP_RULE_UPDATE:
1497 case DPLANE_OP_BR_PORT_UPDATE:
1498 case DPLANE_OP_IPTABLE_ADD:
1499 case DPLANE_OP_IPTABLE_DELETE:
1500 case DPLANE_OP_IPSET_ADD:
1501 case DPLANE_OP_IPSET_ENTRY_ADD:
1502 case DPLANE_OP_IPSET_ENTRY_DELETE:
1503 case DPLANE_OP_IPSET_DELETE:
1504 case DPLANE_OP_NEIGH_IP_INSTALL:
1505 case DPLANE_OP_NEIGH_IP_DELETE:
1506 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1507 case DPLANE_OP_GRE_SET:
1508 case DPLANE_OP_INTF_ADDR_ADD:
1509 case DPLANE_OP_INTF_ADDR_DEL:
1510 case DPLANE_OP_INTF_NETCONFIG:
1511 case DPLANE_OP_TC_QDISC_INSTALL:
1512 case DPLANE_OP_TC_QDISC_UNINSTALL:
1513 case DPLANE_OP_TC_CLASS_ADD:
1514 case DPLANE_OP_TC_CLASS_DELETE:
1515 case DPLANE_OP_TC_CLASS_UPDATE:
1516 case DPLANE_OP_TC_FILTER_ADD:
1517 case DPLANE_OP_TC_FILTER_DELETE:
1518 case DPLANE_OP_TC_FILTER_UPDATE:
1519 flog_err(
1520 EC_ZEBRA_NHG_FIB_UPDATE,
1521 "Context received for kernel interface update with incorrect OP code (%u)",
1522 op);
1523 return -1;
1524 }
1525
1526 return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1527 }
1528
1529 enum netlink_msg_status
1530 netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1531 {
1532 return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1533 }
1534
1535 int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1536 {
1537 int len;
1538 struct ifaddrmsg *ifa;
1539 struct rtattr *tb[IFA_MAX + 1];
1540 struct interface *ifp;
1541 void *addr;
1542 void *broad;
1543 uint8_t flags = 0;
1544 char *label = NULL;
1545 struct zebra_ns *zns;
1546 uint32_t metric = METRIC_MAX;
1547 uint32_t kernel_flags = 0;
1548
1549 frrtrace(3, frr_zebra, netlink_interface_addr, h, ns_id, startup);
1550
1551 zns = zebra_ns_lookup(ns_id);
1552 ifa = NLMSG_DATA(h);
1553
1554 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1555 flog_warn(
1556 EC_ZEBRA_UNKNOWN_FAMILY,
1557 "Invalid address family: %u received from kernel interface addr change: %s",
1558 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
1559 return 0;
1560 }
1561
1562 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1563 return 0;
1564
1565 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1566 if (len < 0) {
1567 zlog_err(
1568 "%s: Message received from netlink is of a broken size: %d %zu",
1569 __func__, h->nlmsg_len,
1570 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
1571 return -1;
1572 }
1573
1574 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1575
1576 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1577 if (ifp == NULL) {
1578 if (startup) {
1579 /* During startup, failure to lookup the referenced
1580 * interface should not be an error, so we have
1581 * downgraded this condition to warning, and we permit
1582 * the startup interface state retrieval to continue.
1583 */
1584 flog_warn(EC_LIB_INTERFACE,
1585 "%s: can't find interface by index %d",
1586 __func__, ifa->ifa_index);
1587 return 0;
1588 } else {
1589 flog_err(EC_LIB_INTERFACE,
1590 "%s: can't find interface by index %d",
1591 __func__, ifa->ifa_index);
1592 return -1;
1593 }
1594 }
1595
1596 /* Flags passed through */
1597 if (tb[IFA_FLAGS])
1598 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1599 else
1600 kernel_flags = ifa->ifa_flags;
1601
1602 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1603 {
1604 char buf[BUFSIZ];
1605 zlog_debug("%s %s %s flags 0x%x:", __func__,
1606 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
1607 kernel_flags);
1608 if (tb[IFA_LOCAL])
1609 zlog_debug(" IFA_LOCAL %s/%d",
1610 inet_ntop(ifa->ifa_family,
1611 RTA_DATA(tb[IFA_LOCAL]), buf,
1612 BUFSIZ),
1613 ifa->ifa_prefixlen);
1614 if (tb[IFA_ADDRESS])
1615 zlog_debug(" IFA_ADDRESS %s/%d",
1616 inet_ntop(ifa->ifa_family,
1617 RTA_DATA(tb[IFA_ADDRESS]), buf,
1618 BUFSIZ),
1619 ifa->ifa_prefixlen);
1620 if (tb[IFA_BROADCAST])
1621 zlog_debug(" IFA_BROADCAST %s/%d",
1622 inet_ntop(ifa->ifa_family,
1623 RTA_DATA(tb[IFA_BROADCAST]), buf,
1624 BUFSIZ),
1625 ifa->ifa_prefixlen);
1626 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1627 zlog_debug(" IFA_LABEL %s",
1628 (char *)RTA_DATA(tb[IFA_LABEL]));
1629
1630 if (tb[IFA_CACHEINFO]) {
1631 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1632 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1633 ci->ifa_prefered, ci->ifa_valid);
1634 }
1635 }
1636
1637 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1638 if (tb[IFA_LOCAL] == NULL)
1639 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1640 if (tb[IFA_ADDRESS] == NULL)
1641 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1642
1643 /* local interface address */
1644 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1645
1646 /* is there a peer address? */
1647 if (tb[IFA_ADDRESS]
1648 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1649 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1650 broad = RTA_DATA(tb[IFA_ADDRESS]);
1651 SET_FLAG(flags, ZEBRA_IFA_PEER);
1652 } else
1653 /* seeking a broadcast address */
1654 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1655 : NULL);
1656
1657 /* addr is primary key, SOL if we don't have one */
1658 if (addr == NULL) {
1659 zlog_debug("%s: Local Interface Address is NULL for %s",
1660 __func__, ifp->name);
1661 return -1;
1662 }
1663
1664 /* Flags. */
1665 if (kernel_flags & IFA_F_SECONDARY)
1666 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1667
1668 /* Label */
1669 if (tb[IFA_LABEL])
1670 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1671
1672 if (label && strcmp(ifp->name, label) == 0)
1673 label = NULL;
1674
1675 if (tb[IFA_RT_PRIORITY])
1676 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1677
1678 /* Register interface address to the interface. */
1679 if (ifa->ifa_family == AF_INET) {
1680 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1681 zlog_err(
1682 "Invalid prefix length: %u received from kernel interface addr change: %s",
1683 ifa->ifa_prefixlen,
1684 nl_msg_type_to_str(h->nlmsg_type));
1685 return -1;
1686 }
1687
1688 if (h->nlmsg_type == RTM_NEWADDR)
1689 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1690 ifa->ifa_prefixlen,
1691 (struct in_addr *)broad, label,
1692 metric);
1693 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1694 /* Delete with a peer address */
1695 connected_delete_ipv4(
1696 ifp, flags, (struct in_addr *)addr,
1697 ifa->ifa_prefixlen, broad);
1698 } else
1699 connected_delete_ipv4(
1700 ifp, flags, (struct in_addr *)addr,
1701 ifa->ifa_prefixlen, NULL);
1702 }
1703
1704 if (ifa->ifa_family == AF_INET6) {
1705 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1706 zlog_err(
1707 "Invalid prefix length: %u received from kernel interface addr change: %s",
1708 ifa->ifa_prefixlen,
1709 nl_msg_type_to_str(h->nlmsg_type));
1710 return -1;
1711 }
1712 if (h->nlmsg_type == RTM_NEWADDR) {
1713 /* Only consider valid addresses; we'll not get a
1714 * notification from
1715 * the kernel till IPv6 DAD has completed, but at init
1716 * time, Quagga
1717 * does query for and will receive all addresses.
1718 */
1719 if (!(kernel_flags
1720 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
1721 connected_add_ipv6(ifp, flags,
1722 (struct in6_addr *)addr,
1723 (struct in6_addr *)broad,
1724 ifa->ifa_prefixlen, label,
1725 metric);
1726 } else
1727 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
1728 NULL, ifa->ifa_prefixlen);
1729 }
1730
1731 /*
1732 * Linux kernel does not send route delete on interface down/addr del
1733 * so we have to re-process routes it owns (i.e. kernel routes)
1734 */
1735 if (h->nlmsg_type != RTM_NEWADDR)
1736 rib_update(RIB_UPDATE_KERNEL);
1737
1738 return 0;
1739 }
1740
1741 /*
1742 * Parse and validate an incoming interface address change message,
1743 * generating a dplane context object.
1744 * This runs in the dplane pthread; the context is enqueued to the
1745 * main pthread for processing.
1746 */
1747 int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1748 int startup /*ignored*/)
1749 {
1750 int len;
1751 struct ifaddrmsg *ifa;
1752 struct rtattr *tb[IFA_MAX + 1];
1753 void *addr;
1754 void *broad;
1755 char *label = NULL;
1756 uint32_t metric = METRIC_MAX;
1757 uint32_t kernel_flags = 0;
1758 struct zebra_dplane_ctx *ctx;
1759 struct prefix p;
1760
1761 ifa = NLMSG_DATA(h);
1762
1763 /* Validate message types */
1764 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1765 return 0;
1766
1767 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1768 if (IS_ZEBRA_DEBUG_KERNEL)
1769 zlog_debug("%s: %s: Invalid address family: %u",
1770 __func__, nl_msg_type_to_str(h->nlmsg_type),
1771 ifa->ifa_family);
1772 return 0;
1773 }
1774
1775 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1776 if (len < 0) {
1777 if (IS_ZEBRA_DEBUG_KERNEL)
1778 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1779 __func__, nl_msg_type_to_str(h->nlmsg_type),
1780 h->nlmsg_len,
1781 (size_t)NLMSG_LENGTH(
1782 sizeof(struct ifaddrmsg)));
1783 return -1;
1784 }
1785
1786 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1787
1788 /* Flags passed through */
1789 if (tb[IFA_FLAGS])
1790 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1791 else
1792 kernel_flags = ifa->ifa_flags;
1793
1794 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1795 char buf[PREFIX_STRLEN];
1796
1797 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1798 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1799 ifa->ifa_index, kernel_flags);
1800 if (tb[IFA_LOCAL])
1801 zlog_debug(" IFA_LOCAL %s/%d",
1802 inet_ntop(ifa->ifa_family,
1803 RTA_DATA(tb[IFA_LOCAL]), buf,
1804 sizeof(buf)),
1805 ifa->ifa_prefixlen);
1806 if (tb[IFA_ADDRESS])
1807 zlog_debug(" IFA_ADDRESS %s/%d",
1808 inet_ntop(ifa->ifa_family,
1809 RTA_DATA(tb[IFA_ADDRESS]), buf,
1810 sizeof(buf)),
1811 ifa->ifa_prefixlen);
1812 if (tb[IFA_BROADCAST])
1813 zlog_debug(" IFA_BROADCAST %s/%d",
1814 inet_ntop(ifa->ifa_family,
1815 RTA_DATA(tb[IFA_BROADCAST]), buf,
1816 sizeof(buf)),
1817 ifa->ifa_prefixlen);
1818 if (tb[IFA_LABEL])
1819 zlog_debug(" IFA_LABEL %s",
1820 (const char *)RTA_DATA(tb[IFA_LABEL]));
1821
1822 if (tb[IFA_CACHEINFO]) {
1823 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1824
1825 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1826 ci->ifa_prefered, ci->ifa_valid);
1827 }
1828 }
1829
1830 /* Validate prefix length */
1831
1832 if (ifa->ifa_family == AF_INET
1833 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1834 if (IS_ZEBRA_DEBUG_KERNEL)
1835 zlog_debug("%s: %s: Invalid prefix length: %u",
1836 __func__, nl_msg_type_to_str(h->nlmsg_type),
1837 ifa->ifa_prefixlen);
1838 return -1;
1839 }
1840
1841 if (ifa->ifa_family == AF_INET6) {
1842 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1843 if (IS_ZEBRA_DEBUG_KERNEL)
1844 zlog_debug("%s: %s: Invalid prefix length: %u",
1845 __func__,
1846 nl_msg_type_to_str(h->nlmsg_type),
1847 ifa->ifa_prefixlen);
1848 return -1;
1849 }
1850
1851 /* Only consider valid addresses; we'll not get a kernel
1852 * notification till IPv6 DAD has completed, but at init
1853 * time, FRR does query for and will receive all addresses.
1854 */
1855 if (h->nlmsg_type == RTM_NEWADDR
1856 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1857 if (IS_ZEBRA_DEBUG_KERNEL)
1858 zlog_debug("%s: %s: Invalid/tentative addr",
1859 __func__,
1860 nl_msg_type_to_str(h->nlmsg_type));
1861 return 0;
1862 }
1863 }
1864
1865 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1866 if (tb[IFA_LOCAL] == NULL)
1867 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1868 if (tb[IFA_ADDRESS] == NULL)
1869 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1870
1871 /* local interface address */
1872 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1873
1874 /* addr is primary key, SOL if we don't have one */
1875 if (addr == NULL) {
1876 if (IS_ZEBRA_DEBUG_KERNEL)
1877 zlog_debug("%s: %s: No local interface address",
1878 __func__, nl_msg_type_to_str(h->nlmsg_type));
1879 return -1;
1880 }
1881
1882 /* Allocate a context object, now that validation is done. */
1883 ctx = dplane_ctx_alloc();
1884 if (h->nlmsg_type == RTM_NEWADDR)
1885 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1886 else
1887 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1888
1889 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1890 dplane_ctx_set_ns_id(ctx, ns_id);
1891
1892 /* Convert addr to prefix */
1893 memset(&p, 0, sizeof(p));
1894 p.family = ifa->ifa_family;
1895 p.prefixlen = ifa->ifa_prefixlen;
1896 if (p.family == AF_INET)
1897 p.u.prefix4 = *(struct in_addr *)addr;
1898 else
1899 p.u.prefix6 = *(struct in6_addr *)addr;
1900
1901 dplane_ctx_set_intf_addr(ctx, &p);
1902
1903 /* is there a peer address? */
1904 if (tb[IFA_ADDRESS]
1905 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1906 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1907 broad = RTA_DATA(tb[IFA_ADDRESS]);
1908 dplane_ctx_intf_set_connected(ctx);
1909 } else if (tb[IFA_BROADCAST]) {
1910 /* seeking a broadcast address */
1911 broad = RTA_DATA(tb[IFA_BROADCAST]);
1912 dplane_ctx_intf_set_broadcast(ctx);
1913 } else
1914 broad = NULL;
1915
1916 if (broad) {
1917 /* Convert addr to prefix */
1918 memset(&p, 0, sizeof(p));
1919 p.family = ifa->ifa_family;
1920 p.prefixlen = ifa->ifa_prefixlen;
1921 if (p.family == AF_INET)
1922 p.u.prefix4 = *(struct in_addr *)broad;
1923 else
1924 p.u.prefix6 = *(struct in6_addr *)broad;
1925
1926 dplane_ctx_set_intf_dest(ctx, &p);
1927 }
1928
1929 /* Flags. */
1930 if (kernel_flags & IFA_F_SECONDARY)
1931 dplane_ctx_intf_set_secondary(ctx);
1932
1933 /* Label */
1934 if (tb[IFA_LABEL]) {
1935 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1936 dplane_ctx_set_intf_label(ctx, label);
1937 }
1938
1939 if (tb[IFA_RT_PRIORITY])
1940 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1941
1942 dplane_ctx_set_intf_metric(ctx, metric);
1943
1944 /* Enqueue ctx for main pthread to process */
1945 dplane_provider_enqueue_to_zebra(ctx);
1946
1947 return 0;
1948 }
1949
1950 int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1951 {
1952 int len;
1953 struct ifinfomsg *ifi;
1954 struct rtattr *tb[IFLA_MAX + 1];
1955 struct rtattr *linkinfo[IFLA_MAX + 1];
1956 struct interface *ifp;
1957 char *name = NULL;
1958 char *kind = NULL;
1959 char *desc = NULL;
1960 char *slave_kind = NULL;
1961 struct zebra_ns *zns;
1962 vrf_id_t vrf_id = VRF_DEFAULT;
1963 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1964 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1965 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1966 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1967 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1968 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
1969 struct zebra_if *zif;
1970 ns_id_t link_nsid = ns_id;
1971 ifindex_t master_infindex = IFINDEX_INTERNAL;
1972 uint8_t bypass = 0;
1973
1974 zns = zebra_ns_lookup(ns_id);
1975 ifi = NLMSG_DATA(h);
1976
1977 /* assume if not default zns, then new VRF */
1978 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1979 /* If this is not link add/delete message so print warning. */
1980 zlog_debug("%s: wrong kernel message %s", __func__,
1981 nl_msg_type_to_str(h->nlmsg_type));
1982 return 0;
1983 }
1984
1985 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1986 || ifi->ifi_family == AF_INET6)) {
1987 flog_warn(
1988 EC_ZEBRA_UNKNOWN_FAMILY,
1989 "Invalid address family: %u received from kernel link change: %s",
1990 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
1991 return 0;
1992 }
1993
1994 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1995 if (len < 0) {
1996 zlog_err(
1997 "%s: Message received from netlink is of a broken size %d %zu",
1998 __func__, h->nlmsg_len,
1999 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
2000 return -1;
2001 }
2002
2003 /* We are interested in some AF_BRIDGE notifications. */
2004 if (ifi->ifi_family == AF_BRIDGE)
2005 return netlink_bridge_interface(h, len, ns_id, startup);
2006
2007 /* Looking up interface name. */
2008 memset(linkinfo, 0, sizeof(linkinfo));
2009 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
2010 NLA_F_NESTED);
2011
2012 /* check for wireless messages to ignore */
2013 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
2014 if (IS_ZEBRA_DEBUG_KERNEL)
2015 zlog_debug("%s: ignoring IFLA_WIRELESS message",
2016 __func__);
2017 return 0;
2018 }
2019
2020 if (tb[IFLA_IFNAME] == NULL)
2021 return -1;
2022 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
2023
2024 /* Must be valid string. */
2025 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
2026 if (len < 2 || name[len - 1] != '\0') {
2027 if (IS_ZEBRA_DEBUG_KERNEL)
2028 zlog_debug("%s: invalid intf name", __func__);
2029 return -1;
2030 }
2031
2032 if (tb[IFLA_LINKINFO]) {
2033 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
2034 tb[IFLA_LINKINFO]);
2035
2036 if (linkinfo[IFLA_INFO_KIND])
2037 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
2038
2039 if (linkinfo[IFLA_INFO_SLAVE_KIND])
2040 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
2041
2042 netlink_determine_zebra_iftype(kind, &zif_type);
2043 }
2044
2045 /* If linking to another interface, note it. */
2046 if (tb[IFLA_LINK])
2047 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
2048
2049 if (tb[IFLA_LINK_NETNSID]) {
2050 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
2051 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
2052 }
2053 if (tb[IFLA_IFALIAS]) {
2054 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
2055 }
2056
2057 /* See if interface is present. */
2058 ifp = if_lookup_by_name_per_ns(zns, name);
2059
2060 if (h->nlmsg_type == RTM_NEWLINK) {
2061 /* If VRF, create or update the VRF structure itself. */
2062 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
2063 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2064 vrf_id = (vrf_id_t)ifi->ifi_index;
2065 }
2066
2067 if (tb[IFLA_MASTER]) {
2068 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
2069 && !vrf_is_backend_netns()) {
2070 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
2071 master_infindex = vrf_id =
2072 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
2073 } else if (slave_kind
2074 && (strcmp(slave_kind, "bridge") == 0)) {
2075 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
2076 master_infindex = bridge_ifindex =
2077 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
2078 } else if (slave_kind
2079 && (strcmp(slave_kind, "bond") == 0)) {
2080 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
2081 master_infindex = bond_ifindex =
2082 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
2083 bypass = netlink_parse_lacp_bypass(linkinfo);
2084 } else
2085 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
2086 }
2087 if (vrf_is_backend_netns())
2088 vrf_id = (vrf_id_t)ns_id;
2089 if (ifp == NULL
2090 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2091 /* Add interface notification from kernel */
2092 if (IS_ZEBRA_DEBUG_KERNEL)
2093 zlog_debug(
2094 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
2095 name, ifi->ifi_index, vrf_id, zif_type,
2096 zif_slave_type, master_infindex,
2097 ifi->ifi_flags);
2098
2099 if (ifp == NULL) {
2100 /* unknown interface */
2101 ifp = if_get_by_name(name, vrf_id, NULL);
2102 } else {
2103 /* pre-configured interface, learnt now */
2104 if (ifp->vrf->vrf_id != vrf_id)
2105 if_update_to_new_vrf(ifp, vrf_id);
2106 }
2107
2108 /* Update interface information. */
2109 set_ifindex(ifp, ifi->ifi_index, zns);
2110 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2111 if (!tb[IFLA_MTU]) {
2112 zlog_debug(
2113 "RTM_NEWLINK for interface %s(%u) without MTU set",
2114 name, ifi->ifi_index);
2115 return 0;
2116 }
2117 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2118 ifp->metric = 0;
2119 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
2120
2121 /* Set interface type */
2122 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2123 if (IS_ZEBRA_IF_VRF(ifp))
2124 SET_FLAG(ifp->status,
2125 ZEBRA_INTERFACE_VRF_LOOPBACK);
2126
2127 /* Update link. */
2128 zebra_if_update_link(ifp, link_ifindex, link_nsid);
2129
2130 ifp->ll_type =
2131 netlink_to_zebra_link_type(ifi->ifi_type);
2132 netlink_interface_update_hw_addr(tb, ifp);
2133
2134 /* Inform clients, install any configured addresses. */
2135 if_add_update(ifp);
2136
2137 /* Extract and save L2 interface information, take
2138 * additional actions. */
2139 netlink_interface_update_l2info(
2140 ifp, linkinfo[IFLA_INFO_DATA],
2141 1, link_nsid);
2142 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
2143 zebra_l2if_update_bridge_slave(
2144 ifp, bridge_ifindex, ns_id,
2145 ZEBRA_BRIDGE_NO_ACTION);
2146 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2147 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2148 !!bypass);
2149
2150 if (tb[IFLA_PROTO_DOWN])
2151 netlink_proc_dplane_if_protodown(ifp->info, tb);
2152 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
2153 zif = ifp->info;
2154 if (IS_ZEBRA_DEBUG_KERNEL)
2155 zlog_debug(
2156 "RTM_NEWLINK ADD for %s(%u), vlan-aware %d",
2157 name, ifp->ifindex,
2158 IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2159 zif));
2160 }
2161 } else if (ifp->vrf->vrf_id != vrf_id) {
2162 /* VRF change for an interface. */
2163 if (IS_ZEBRA_DEBUG_KERNEL)
2164 zlog_debug(
2165 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
2166 name, ifp->ifindex, ifp->vrf->vrf_id,
2167 vrf_id, ifi->ifi_flags);
2168
2169 if_handle_vrf_change(ifp, vrf_id);
2170 } else {
2171 bool was_bridge_slave, was_bond_slave;
2172 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
2173 zif = ifp->info;
2174
2175 /* Interface update. */
2176 if (IS_ZEBRA_DEBUG_KERNEL)
2177 zlog_debug(
2178 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
2179 name, ifp->ifindex, zif_slave_type,
2180 master_infindex, ifi->ifi_flags);
2181
2182 set_ifindex(ifp, ifi->ifi_index, zns);
2183 if (!tb[IFLA_MTU]) {
2184 zlog_debug(
2185 "RTM_NEWLINK for interface %s(%u) without MTU set",
2186 name, ifi->ifi_index);
2187 return 0;
2188 }
2189 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2190 ifp->metric = 0;
2191
2192 /* Update interface type - NOTE: Only slave_type can
2193 * change. */
2194 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
2195 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
2196 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2197
2198 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
2199
2200 /* Update link. */
2201 zebra_if_update_link(ifp, link_ifindex, link_nsid);
2202
2203 ifp->ll_type =
2204 netlink_to_zebra_link_type(ifi->ifi_type);
2205 netlink_interface_update_hw_addr(tb, ifp);
2206
2207 if (tb[IFLA_PROTO_DOWN])
2208 netlink_proc_dplane_if_protodown(ifp->info, tb);
2209
2210 if (if_is_no_ptm_operative(ifp)) {
2211 bool is_up = if_is_operative(ifp);
2212 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2213 if (!if_is_no_ptm_operative(ifp) ||
2214 CHECK_FLAG(zif->flags,
2215 ZIF_FLAG_PROTODOWN)) {
2216 if (IS_ZEBRA_DEBUG_KERNEL)
2217 zlog_debug(
2218 "Intf %s(%u) has gone DOWN",
2219 name, ifp->ifindex);
2220 if_down(ifp);
2221 rib_update(RIB_UPDATE_KERNEL);
2222 } else if (if_is_operative(ifp)) {
2223 bool mac_updated = false;
2224
2225 /* Must notify client daemons of new
2226 * interface status. */
2227 if (IS_ZEBRA_DEBUG_KERNEL)
2228 zlog_debug(
2229 "Intf %s(%u) PTM up, notifying clients",
2230 name, ifp->ifindex);
2231 if_up(ifp, !is_up);
2232
2233 /* Update EVPN VNI when SVI MAC change
2234 */
2235 if (memcmp(old_hw_addr, ifp->hw_addr,
2236 INTERFACE_HWADDR_MAX))
2237 mac_updated = true;
2238 if (IS_ZEBRA_IF_VLAN(ifp)
2239 && mac_updated) {
2240 struct interface *link_if;
2241
2242 link_if =
2243 if_lookup_by_index_per_ns(
2244 zebra_ns_lookup(NS_DEFAULT),
2245 link_ifindex);
2246 if (link_if)
2247 zebra_vxlan_svi_up(ifp,
2248 link_if);
2249 } else if (mac_updated
2250 && IS_ZEBRA_IF_BRIDGE(ifp)) {
2251 zlog_debug(
2252 "Intf %s(%u) bridge changed MAC address",
2253 name, ifp->ifindex);
2254 chgflags =
2255 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
2256 }
2257 }
2258 } else {
2259 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2260 if (if_is_operative(ifp) &&
2261 !CHECK_FLAG(zif->flags,
2262 ZIF_FLAG_PROTODOWN)) {
2263 if (IS_ZEBRA_DEBUG_KERNEL)
2264 zlog_debug(
2265 "Intf %s(%u) has come UP",
2266 name, ifp->ifindex);
2267 if_up(ifp, true);
2268 if (IS_ZEBRA_IF_BRIDGE(ifp))
2269 chgflags =
2270 ZEBRA_BRIDGE_MASTER_UP;
2271 } else {
2272 if (IS_ZEBRA_DEBUG_KERNEL)
2273 zlog_debug(
2274 "Intf %s(%u) has gone DOWN",
2275 name, ifp->ifindex);
2276 if_down(ifp);
2277 rib_update(RIB_UPDATE_KERNEL);
2278 }
2279 }
2280
2281 /* Extract and save L2 interface information, take
2282 * additional actions. */
2283 netlink_interface_update_l2info(
2284 ifp, linkinfo[IFLA_INFO_DATA],
2285 0, link_nsid);
2286 if (IS_ZEBRA_IF_BRIDGE(ifp))
2287 zebra_l2if_update_bridge(ifp, chgflags);
2288 if (IS_ZEBRA_IF_BOND(ifp))
2289 zebra_l2if_update_bond(ifp, true);
2290 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
2291 zebra_l2if_update_bridge_slave(
2292 ifp, bridge_ifindex, ns_id, chgflags);
2293 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
2294 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2295 !!bypass);
2296 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
2297 if (IS_ZEBRA_DEBUG_KERNEL)
2298 zlog_debug(
2299 "RTM_NEWLINK update for %s(%u), vlan-aware %d",
2300 name, ifp->ifindex,
2301 IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2302 zif));
2303 }
2304 }
2305
2306 zif = ifp->info;
2307 if (zif) {
2308 XFREE(MTYPE_ZIF_DESC, zif->desc);
2309 if (desc)
2310 zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
2311 }
2312 } else {
2313 /* Delete interface notification from kernel */
2314 if (ifp == NULL) {
2315 if (IS_ZEBRA_DEBUG_KERNEL)
2316 zlog_debug(
2317 "RTM_DELLINK for unknown interface %s(%u)",
2318 name, ifi->ifi_index);
2319 return 0;
2320 }
2321
2322 if (IS_ZEBRA_DEBUG_KERNEL)
2323 zlog_debug("RTM_DELLINK for %s(%u)", name,
2324 ifp->ifindex);
2325
2326 if (IS_ZEBRA_IF_BOND(ifp))
2327 zebra_l2if_update_bond(ifp, false);
2328 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2329 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
2330 /* Special handling for bridge or VxLAN interfaces. */
2331 if (IS_ZEBRA_IF_BRIDGE(ifp))
2332 zebra_l2_bridge_del(ifp);
2333 else if (IS_ZEBRA_IF_VXLAN(ifp))
2334 zebra_l2_vxlanif_del(ifp);
2335
2336 if_delete_update(&ifp);
2337
2338 /* If VRF, delete the VRF structure itself. */
2339 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2340 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2341 }
2342
2343 return 0;
2344 }
2345
2346 /**
2347 * Interface encoding helper function.
2348 *
2349 * \param[in] cmd netlink command.
2350 * \param[in] ctx dataplane context (information snapshot).
2351 * \param[out] buf buffer to hold the packet.
2352 * \param[in] buflen amount of buffer bytes.
2353 */
2354
2355 ssize_t netlink_intf_msg_encode(uint16_t cmd,
2356 const struct zebra_dplane_ctx *ctx, void *buf,
2357 size_t buflen)
2358 {
2359 struct {
2360 struct nlmsghdr n;
2361 struct ifinfomsg ifa;
2362 char buf[];
2363 } *req = buf;
2364
2365 struct rtattr *nest_protodown_reason;
2366 ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
2367 bool down = dplane_ctx_intf_is_protodown(ctx);
2368 bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
2369 struct nlsock *nl =
2370 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
2371
2372 if (buflen < sizeof(*req))
2373 return 0;
2374
2375 memset(req, 0, sizeof(*req));
2376
2377 if (cmd != RTM_SETLINK)
2378 flog_err(
2379 EC_ZEBRA_INTF_UPDATE_FAILURE,
2380 "Only RTM_SETLINK message type currently supported in dplane pthread");
2381
2382 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2383 req->n.nlmsg_flags = NLM_F_REQUEST;
2384 req->n.nlmsg_type = cmd;
2385 req->n.nlmsg_pid = nl->snl.nl_pid;
2386
2387 req->ifa.ifi_index = ifindex;
2388
2389 nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2390 nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2391
2392 /* Reason info nest */
2393 nest_protodown_reason =
2394 nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
2395
2396 if (!nest_protodown_reason)
2397 return -1;
2398
2399 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2400 (1 << frr_protodown_r_bit));
2401 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
2402 ((int)pd_reason_val) << frr_protodown_r_bit);
2403
2404 nl_attr_nest_end(&req->n, nest_protodown_reason);
2405
2406 if (IS_ZEBRA_DEBUG_KERNEL)
2407 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2408 __func__, nl_msg_type_to_str(cmd), down,
2409 pd_reason_val, ifindex);
2410
2411 return NLMSG_ALIGN(req->n.nlmsg_len);
2412 }
2413
2414 /* Interface information read by netlink. */
2415 void interface_list(struct zebra_ns *zns)
2416 {
2417 interface_lookup_netlink(zns);
2418 /* We add routes for interface address,
2419 * so we need to get the nexthop info
2420 * from the kernel before we can do that
2421 */
2422 netlink_nexthop_read(zns);
2423
2424 interface_addr_lookup_netlink(zns);
2425 }
2426
2427 void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2428 {
2429 if (IS_ZEBRA_DEBUG_KERNEL)
2430 zlog_debug(
2431 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2432 frr_protodown_r_bit, bit);
2433
2434 frr_protodown_r_bit = bit;
2435 }
2436
2437 void if_netlink_unset_frr_protodown_r_bit(void)
2438 {
2439 if (IS_ZEBRA_DEBUG_KERNEL)
2440 zlog_debug(
2441 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2442 frr_protodown_r_bit, FRR_PROTODOWN_REASON_DEFAULT_BIT);
2443
2444 frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2445 }
2446
2447
2448 bool if_netlink_frr_protodown_r_bit_is_set(void)
2449 {
2450 return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2451 }
2452
2453 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2454 {
2455 return frr_protodown_r_bit;
2456 }
2457
2458 /**
2459 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2460 *
2461 * @zns: Zebra namespace
2462 * @family: AF_* netlink family
2463 * @type: RTM_* (RTM_GETTUNNEL) route type
2464 *
2465 * Return: Result status
2466 */
2467 static int netlink_request_tunneldump(struct zebra_ns *zns, int family,
2468 int ifindex)
2469 {
2470 struct {
2471 struct nlmsghdr n;
2472 struct tunnel_msg tmsg;
2473 char buf[256];
2474 } req;
2475
2476 /* Form the request */
2477 memset(&req, 0, sizeof(req));
2478 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg));
2479 req.n.nlmsg_type = RTM_GETTUNNEL;
2480 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2481 req.tmsg.family = family;
2482 req.tmsg.ifindex = ifindex;
2483
2484 return netlink_request(&zns->netlink_cmd, &req);
2485 }
2486
2487 /*
2488 * Currently we only ask for vxlan l3svd vni information.
2489 * In the future this can be expanded.
2490 */
2491 int netlink_tunneldump_read(struct zebra_ns *zns)
2492 {
2493 int ret = 0;
2494 struct zebra_dplane_info dp_info;
2495 struct route_node *rn;
2496 struct interface *tmp_if = NULL;
2497 struct zebra_if *zif;
2498 struct nlsock *netlink_cmd = &zns->netlink_cmd;
2499
2500 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2501
2502 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2503 tmp_if = (struct interface *)rn->info;
2504 if (!tmp_if)
2505 continue;
2506 zif = tmp_if->info;
2507 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2508 continue;
2509
2510 ret = netlink_request_tunneldump(zns, PF_BRIDGE,
2511 tmp_if->ifindex);
2512 if (ret < 0)
2513 return ret;
2514
2515 ret = netlink_parse_info(netlink_interface, netlink_cmd,
2516 &dp_info, 0, true);
2517
2518 if (ret < 0)
2519 return ret;
2520 }
2521
2522 return 0;
2523 }
2524
2525 static const char *port_state2str(uint8_t state)
2526 {
2527 switch (state) {
2528 case BR_STATE_DISABLED:
2529 return "DISABLED";
2530 case BR_STATE_LISTENING:
2531 return "LISTENING";
2532 case BR_STATE_LEARNING:
2533 return "LEARNING";
2534 case BR_STATE_FORWARDING:
2535 return "FORWARDING";
2536 case BR_STATE_BLOCKING:
2537 return "BLOCKING";
2538 }
2539
2540 return "UNKNOWN";
2541 }
2542
2543 static void vxlan_vni_state_change(struct zebra_if *zif, uint16_t id,
2544 uint8_t state)
2545 {
2546 struct zebra_vxlan_vni *vnip;
2547
2548 vnip = zebra_vxlan_if_vlanid_vni_find(zif, id);
2549
2550 if (!vnip) {
2551 if (IS_ZEBRA_DEBUG_VXLAN)
2552 zlog_debug(
2553 "Cannot find VNI for VID (%u) IF %s for vlan state update",
2554 id, zif->ifp->name);
2555
2556 return;
2557 }
2558
2559 switch (state) {
2560 case BR_STATE_FORWARDING:
2561 zebra_vxlan_if_vni_up(zif->ifp, vnip);
2562 break;
2563 case BR_STATE_BLOCKING:
2564 zebra_vxlan_if_vni_down(zif->ifp, vnip);
2565 break;
2566 case BR_STATE_DISABLED:
2567 case BR_STATE_LISTENING:
2568 case BR_STATE_LEARNING:
2569 default:
2570 /* Not used for anything at the moment */
2571 break;
2572 }
2573 }
2574
2575 static void vlan_id_range_state_change(struct interface *ifp, uint16_t id_start,
2576 uint16_t id_end, uint8_t state)
2577 {
2578 struct zebra_if *zif;
2579
2580 zif = (struct zebra_if *)ifp->info;
2581
2582 if (!zif)
2583 return;
2584
2585 for (uint16_t i = id_start; i <= id_end; i++)
2586 vxlan_vni_state_change(zif, i, state);
2587 }
2588
2589 /**
2590 * netlink_vlan_change() - Read in change about vlans from the kernel
2591 *
2592 * @h: Netlink message header
2593 * @ns_id: Namspace id
2594 * @startup: Are we reading under startup conditions?
2595 *
2596 * Return: Result status
2597 */
2598 int netlink_vlan_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2599 {
2600 int len, rem;
2601 struct interface *ifp;
2602 struct br_vlan_msg *bvm;
2603 struct bridge_vlan_info *vinfo;
2604 struct rtattr *vtb[BRIDGE_VLANDB_ENTRY_MAX + 1] = {};
2605 struct rtattr *attr;
2606 uint8_t state;
2607 uint32_t vrange;
2608 int type;
2609
2610 /* We only care about state changes for now */
2611 if (!(h->nlmsg_type == RTM_NEWVLAN))
2612 return 0;
2613
2614 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2615 if (len < 0) {
2616 zlog_warn(
2617 "%s: Message received from netlink is of a broken size %d %zu",
2618 __func__, h->nlmsg_len,
2619 (size_t)NLMSG_LENGTH(sizeof(struct br_vlan_msg)));
2620 return -1;
2621 }
2622
2623 bvm = NLMSG_DATA(h);
2624
2625 if (bvm->family != AF_BRIDGE)
2626 return 0;
2627
2628 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), bvm->ifindex);
2629 if (!ifp) {
2630 zlog_debug("Cannot find bridge-vlan IF (%u) for vlan update",
2631 bvm->ifindex);
2632 return 0;
2633 }
2634
2635 if (!IS_ZEBRA_IF_VXLAN(ifp)) {
2636 if (IS_ZEBRA_DEBUG_KERNEL)
2637 zlog_debug("Ignoring non-vxlan IF (%s) for vlan update",
2638 ifp->name);
2639
2640 return 0;
2641 }
2642
2643 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN)
2644 zlog_debug("%s %s IF %s NS %u",
2645 nl_msg_type_to_str(h->nlmsg_type),
2646 nl_family_to_str(bvm->family), ifp->name, ns_id);
2647
2648 /* Loop over "ALL" BRIDGE_VLANDB_ENTRY */
2649 rem = len;
2650 for (attr = BRVLAN_RTA(bvm); RTA_OK(attr, rem);
2651 attr = RTA_NEXT(attr, rem)) {
2652 vinfo = NULL;
2653 vrange = 0;
2654
2655 type = attr->rta_type & NLA_TYPE_MASK;
2656
2657 if (type != BRIDGE_VLANDB_ENTRY)
2658 continue;
2659
2660 /* Parse nested entry data */
2661 netlink_parse_rtattr_nested(vtb, BRIDGE_VLANDB_ENTRY_MAX, attr);
2662
2663 /* It must have info for the ID */
2664 if (!vtb[BRIDGE_VLANDB_ENTRY_INFO])
2665 continue;
2666
2667 vinfo = (struct bridge_vlan_info *)RTA_DATA(
2668 vtb[BRIDGE_VLANDB_ENTRY_INFO]);
2669
2670 /*
2671 * We only care about state info, if there is none, just ignore
2672 * it.
2673 */
2674 if (!vtb[BRIDGE_VLANDB_ENTRY_STATE])
2675 continue;
2676
2677 state = *(uint8_t *)RTA_DATA(vtb[BRIDGE_VLANDB_ENTRY_STATE]);
2678
2679 if (vtb[BRIDGE_VLANDB_ENTRY_RANGE])
2680 vrange = *(uint32_t *)RTA_DATA(
2681 vtb[BRIDGE_VLANDB_ENTRY_RANGE]);
2682
2683 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN) {
2684 if (vrange)
2685 zlog_debug("VLANDB_ENTRY: VID (%u-%u) state=%s",
2686 vinfo->vid, vrange,
2687 port_state2str(state));
2688 else
2689 zlog_debug("VLANDB_ENTRY: VID (%u) state=%s",
2690 vinfo->vid, port_state2str(state));
2691 }
2692
2693 vlan_id_range_state_change(
2694 ifp, vinfo->vid, (vrange ? vrange : vinfo->vid), state);
2695 }
2696
2697 return 0;
2698 }
2699
2700 /**
2701 * netlink_request_vlan() - Request vlan information from the kernel
2702 * @zns: Zebra namespace
2703 * @family: AF_* netlink family
2704 * @type: RTM_* type
2705 *
2706 * Return: Result status
2707 */
2708 static int netlink_request_vlan(struct zebra_ns *zns, int family, int type)
2709 {
2710 struct {
2711 struct nlmsghdr n;
2712 struct br_vlan_msg bvm;
2713 char buf[256];
2714 } req;
2715
2716 /* Form the request, specifying filter (rtattr) if needed. */
2717 memset(&req, 0, sizeof(req));
2718 req.n.nlmsg_type = type;
2719 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2720 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2721 req.bvm.family = family;
2722
2723 nl_attr_put32(&req.n, sizeof(req), BRIDGE_VLANDB_DUMP_FLAGS,
2724 BRIDGE_VLANDB_DUMPF_STATS);
2725
2726 return netlink_request(&zns->netlink_cmd, &req);
2727 }
2728
2729 /**
2730 * netlink_vlan_read() - Vlan read function using netlink interface
2731 *
2732 * @zns: Zebra name space
2733 *
2734 * Return: Result status
2735 * Only called at bootstrap time.
2736 */
2737 int netlink_vlan_read(struct zebra_ns *zns)
2738 {
2739 int ret;
2740 struct zebra_dplane_info dp_info;
2741
2742 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2743
2744 /* Get bridg vlan info */
2745 ret = netlink_request_vlan(zns, PF_BRIDGE, RTM_GETVLAN);
2746 if (ret < 0)
2747 return ret;
2748
2749 ret = netlink_parse_info(netlink_vlan_change, &zns->netlink_cmd,
2750 &dp_info, 0, 1);
2751
2752 return ret;
2753 }
2754
2755 #endif /* GNU_LINUX */