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