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