2 * Interface looking up by netlink.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
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
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>
47 #include "connected.h"
57 #include "lib_errors.h"
60 #include "zebra/zserv.h"
61 #include "zebra/zebra_ns.h"
62 #include "zebra/zebra_vrf.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"
80 extern struct zebra_privs_t zserv_privs
;
81 uint8_t frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
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
,
88 struct interface
*oifp
;
90 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
92 if (ifi_index
== IFINDEX_INTERNAL
)
95 "Netlink is setting interface %s ifindex to reserved internal value %u",
96 ifp
->name
, ifi_index
);
98 if (IS_ZEBRA_DEBUG_KERNEL
)
100 "interface index %d was renamed from %s to %s",
101 ifi_index
, oifp
->name
, ifp
->name
);
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
);
110 if_set_index(ifp
, ifi_index
);
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
)
119 if (tb
[IFLA_ADDRESS
]) {
122 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
124 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
125 zlog_debug("Hardware address is too large: %d",
128 ifp
->hw_addr_len
= hw_addr_len
;
129 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
132 for (i
= 0; i
< hw_addr_len
; i
++)
133 if (ifp
->hw_addr
[i
] != 0)
136 if (i
== hw_addr_len
)
137 ifp
->hw_addr_len
= 0;
139 ifp
->hw_addr_len
= hw_addr_len
;
144 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
148 return ZEBRA_LLT_ETHER
;
150 return ZEBRA_LLT_EETHER
;
152 return ZEBRA_LLT_AX25
;
154 return ZEBRA_LLT_PRONET
;
156 return ZEBRA_LLT_IEEE802
;
158 return ZEBRA_LLT_ARCNET
;
159 case ARPHRD_APPLETLK
:
160 return ZEBRA_LLT_APPLETLK
;
162 return ZEBRA_LLT_DLCI
;
164 return ZEBRA_LLT_ATM
;
165 case ARPHRD_METRICOM
:
166 return ZEBRA_LLT_METRICOM
;
167 case ARPHRD_IEEE1394
:
168 return ZEBRA_LLT_IEEE1394
;
170 return ZEBRA_LLT_EUI64
;
171 case ARPHRD_INFINIBAND
:
172 return ZEBRA_LLT_INFINIBAND
;
174 return ZEBRA_LLT_SLIP
;
176 return ZEBRA_LLT_CSLIP
;
178 return ZEBRA_LLT_SLIP6
;
180 return ZEBRA_LLT_CSLIP6
;
182 return ZEBRA_LLT_RSRVD
;
184 return ZEBRA_LLT_ADAPT
;
186 return ZEBRA_LLT_ROSE
;
188 return ZEBRA_LLT_X25
;
190 return ZEBRA_LLT_PPP
;
192 return ZEBRA_LLT_CHDLC
;
194 return ZEBRA_LLT_LAPB
;
196 return ZEBRA_LLT_RAWHDLC
;
198 return ZEBRA_LLT_IPIP
;
200 return ZEBRA_LLT_IPIP6
;
202 return ZEBRA_LLT_FRAD
;
204 return ZEBRA_LLT_SKIP
;
205 case ARPHRD_LOOPBACK
:
206 return ZEBRA_LLT_LOOPBACK
;
207 case ARPHRD_LOCALTLK
:
208 return ZEBRA_LLT_LOCALTLK
;
210 return ZEBRA_LLT_FDDI
;
212 return ZEBRA_LLT_SIT
;
214 return ZEBRA_LLT_IPDDP
;
216 return ZEBRA_LLT_IPGRE
;
218 return ZEBRA_LLT_PIMREG
;
220 return ZEBRA_LLT_HIPPI
;
222 return ZEBRA_LLT_ECONET
;
224 return ZEBRA_LLT_IRDA
;
226 return ZEBRA_LLT_FCPP
;
228 return ZEBRA_LLT_FCAL
;
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
;
243 return ZEBRA_LLT_IP6GRE
;
245 #ifdef ARPHRD_IEEE802154_PHY
246 case ARPHRD_IEEE802154_PHY
:
247 return ZEBRA_LLT_IEEE802154_PHY
;
251 return ZEBRA_LLT_UNKNOWN
;
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
)
259 struct zebra_if
*zif
;
261 zif
= (struct zebra_if
*)ifp
->info
;
262 zif
->zif_slave_type
= zif_slave_type
;
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.
271 zebra_evpn_if_init(zif
);
275 static void netlink_determine_zebra_iftype(const char *kind
,
276 enum zebra_iftype
*zif_type
)
278 *zif_type
= ZEBRA_IF_OTHER
;
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
;
303 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
304 uint32_t ns_id
, const char *name
)
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
;
315 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
317 if (!linkinfo
[IFLA_INFO_DATA
]) {
318 if (IS_ZEBRA_DEBUG_KERNEL
)
320 "%s: IFLA_INFO_DATA missing from VRF message: %s",
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
)
330 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
335 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
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
);
342 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
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
);
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
);
357 vrf
= vrf_update((vrf_id_t
)ifi
->ifi_index
, name
);
359 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
360 name
, ifi
->ifi_index
);
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.
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.
376 zvrf
= (struct zebra_vrf
*)vrf
->info
;
377 zvrf
->table_id
= nl_table_id
;
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
,
387 } else // h->nlmsg_type == RTM_DELLINK
389 if (IS_ZEBRA_DEBUG_KERNEL
)
390 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
393 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
396 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
405 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
408 struct ethtool_cmd ecmd
;
411 const char *ifname
= interface
->name
;
415 /* initialize struct */
416 memset(&ifdata
, 0, sizeof(ifdata
));
418 /* set interface name */
419 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
421 /* initialize ethtool interface */
422 memset(&ecmd
, 0, sizeof(ecmd
));
423 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
424 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
426 /* use ioctl to get IP address 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
);
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 */
439 /* Get the current link state for the interface */
440 rc
= vrf_ioctl(interface
->vrf
->vrf_id
, sd
, SIOCETHTOOL
,
444 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
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
)
457 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
460 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
462 return get_iflink_speed(ifp
, error
);
466 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
471 struct ifinfomsg ifi
;
476 struct rtattr
*rta_info
, *rta_data
;
477 const struct zebra_l2info_gre
*gre_info
;
479 if (buflen
< sizeof(*req
))
481 memset(req
, 0, sizeof(*req
));
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
;
487 req
->ifi
.ifi_index
= dplane_ctx_get_ifindex(ctx
);
489 gre_info
= dplane_ctx_gre_get_info(ctx
);
493 req
->ifi
.ifi_change
= 0xFFFFFFFF;
494 link_idx
= dplane_ctx_gre_get_link_ifindex(ctx
);
495 mtu
= dplane_ctx_gre_get_mtu(ctx
);
497 if (mtu
&& !nl_attr_put32(&req
->n
, buflen
, IFLA_MTU
, mtu
))
500 rta_info
= nl_attr_nest(&req
->n
, buflen
, IFLA_LINKINFO
);
504 if (!nl_attr_put(&req
->n
, buflen
, IFLA_INFO_KIND
, "gre", 3))
507 rta_data
= nl_attr_nest(&req
->n
, buflen
, IFLA_INFO_DATA
);
511 if (!nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LINK
, link_idx
))
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
))
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
))
524 if (gre_info
->ikey
&&
525 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
528 if (gre_info
->okey
&&
529 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
533 nl_attr_nest_end(&req
->n
, rta_data
);
534 nl_attr_nest_end(&req
->n
, rta_info
);
536 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
539 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
540 struct zebra_l2info_bridge
*bridge_info
)
542 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
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
->vlan_aware
=
548 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
552 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
553 struct zebra_l2info_vlan
*vlan_info
)
555 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
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");
566 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
567 vlan_info
->vid
= vid_in_msg
;
571 static int netlink_extract_gre_info(struct rtattr
*link_data
,
572 struct zebra_l2info_gre
*gre_info
)
574 struct rtattr
*attr
[IFLA_GRE_MAX
+ 1];
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
);
580 if (!attr
[IFLA_GRE_LOCAL
]) {
581 if (IS_ZEBRA_DEBUG_KERNEL
)
583 "IFLA_GRE_LOCAL missing from GRE IF message");
586 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_LOCAL
]);
587 if (!attr
[IFLA_GRE_REMOTE
]) {
588 if (IS_ZEBRA_DEBUG_KERNEL
)
590 "IFLA_GRE_REMOTE missing from GRE IF message");
592 gre_info
->vtep_ip_remote
=
593 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_REMOTE
]);
595 if (!attr
[IFLA_GRE_LINK
]) {
596 if (IS_ZEBRA_DEBUG_KERNEL
)
597 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
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
);
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
]);
612 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
613 struct zebra_l2info_vxlan
*vxl_info
)
615 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
617 struct in_addr vtep_ip_in_msg
;
618 ifindex_t ifindex_link
;
620 memset(vxl_info
, 0, sizeof(*vxl_info
));
621 netlink_parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
622 if (!attr
[IFLA_VXLAN_ID
]) {
623 if (IS_ZEBRA_DEBUG_KERNEL
)
625 "IFLA_VXLAN_ID missing from VXLAN IF message");
629 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
630 vxl_info
->vni
= vni_in_msg
;
631 if (!attr
[IFLA_VXLAN_LOCAL
]) {
632 if (IS_ZEBRA_DEBUG_KERNEL
)
634 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
637 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
638 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
641 if (attr
[IFLA_VXLAN_GROUP
]) {
642 vxl_info
->mcast_grp
=
643 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_GROUP
]);
646 if (!attr
[IFLA_VXLAN_LINK
]) {
647 if (IS_ZEBRA_DEBUG_KERNEL
)
648 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
651 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
652 vxl_info
->ifindex_link
= ifindex_link
;
658 * Extract and save L2 params (of interest) for an interface. When a
659 * bridge interface is added or updated, take further actions to map
660 * its members. Likewise, for VxLAN interface.
662 static void netlink_interface_update_l2info(struct interface
*ifp
,
663 struct rtattr
*link_data
, int add
,
669 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
670 struct zebra_l2info_bridge bridge_info
;
672 netlink_extract_bridge_info(link_data
, &bridge_info
);
673 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
674 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
675 struct zebra_l2info_vlan vlan_info
;
677 netlink_extract_vlan_info(link_data
, &vlan_info
);
678 zebra_l2_vlanif_update(ifp
, &vlan_info
);
679 zebra_evpn_acc_bd_svi_set(ifp
->info
, NULL
,
680 !!if_is_operative(ifp
));
681 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
682 struct zebra_l2info_vxlan vxlan_info
;
684 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
685 vxlan_info
.link_nsid
= link_nsid
;
686 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
687 if (link_nsid
!= NS_UNKNOWN
&&
688 vxlan_info
.ifindex_link
)
689 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
691 } else if (IS_ZEBRA_IF_GRE(ifp
)) {
692 struct zebra_l2info_gre gre_info
;
694 netlink_extract_gre_info(link_data
, &gre_info
);
695 gre_info
.link_nsid
= link_nsid
;
696 zebra_l2_greif_add_update(ifp
, &gre_info
, add
);
697 if (link_nsid
!= NS_UNKNOWN
&&
698 gre_info
.ifindex_link
)
699 zebra_if_update_link(ifp
, gre_info
.ifindex_link
,
704 static int netlink_bridge_vxlan_update(struct interface
*ifp
,
705 struct rtattr
*af_spec
)
707 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
708 struct bridge_vlan_info
*vinfo
;
709 vlanid_t access_vlan
;
714 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
715 * only 1 access VLAN is accepted.
717 netlink_parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, af_spec
);
718 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
721 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
722 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
725 access_vlan
= (vlanid_t
)vinfo
->vid
;
726 if (IS_ZEBRA_DEBUG_KERNEL
)
727 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
728 ifp
->name
, ifp
->ifindex
);
729 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
733 static void netlink_bridge_vlan_update(struct interface
*ifp
,
734 struct rtattr
*af_spec
)
738 uint16_t vid_range_start
= 0;
739 struct zebra_if
*zif
;
740 bitfield_t old_vlan_bitmap
;
741 struct bridge_vlan_info
*vinfo
;
743 zif
= (struct zebra_if
*)ifp
->info
;
745 /* cache the old bitmap addrs */
746 old_vlan_bitmap
= zif
->vlan_bitmap
;
747 /* create a new bitmap space for re-eval */
748 bf_init(zif
->vlan_bitmap
, IF_VLAN_BITMAP_MAX
);
751 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
);
752 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
754 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
759 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
760 vid_range_start
= vinfo
->vid
;
764 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
765 vid_range_start
= vinfo
->vid
;
767 zebra_vlan_bitmap_compute(ifp
, vid_range_start
,
772 zebra_vlan_mbr_re_eval(ifp
, old_vlan_bitmap
);
774 bf_free(old_vlan_bitmap
);
777 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
781 struct ifinfomsg
*ifi
;
782 struct rtattr
*tb
[IFLA_MAX
+ 1];
783 struct interface
*ifp
;
784 struct zebra_if
*zif
;
785 struct rtattr
*af_spec
;
787 /* Fetch name and ifindex */
789 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
791 if (tb
[IFLA_IFNAME
] == NULL
)
793 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
795 /* The interface should already be known, if not discard. */
796 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
798 zlog_debug("Cannot find bridge IF %s(%u)", name
,
803 /* We are only interested in the access VLAN i.e., AF_SPEC */
804 af_spec
= tb
[IFLA_AF_SPEC
];
806 if (IS_ZEBRA_IF_VXLAN(ifp
))
807 return netlink_bridge_vxlan_update(ifp
, af_spec
);
809 /* build vlan bitmap associated with this interface if that
810 * device type is interested in the vlans
812 zif
= (struct zebra_if
*)ifp
->info
;
813 if (bf_is_inited(zif
->vlan_bitmap
))
814 netlink_bridge_vlan_update(ifp
, af_spec
);
819 static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield
)
821 /* This shouldn't be possible */
822 assert(frr_protodown_r_bit
< 32);
823 return (rc_bitfield
== (((uint32_t)1) << frr_protodown_r_bit
));
827 * Process interface protodown dplane update.
829 * If the interface is an es bond member then it must follow EVPN's
832 static void netlink_proc_dplane_if_protodown(struct zebra_if
*zif
,
837 uint32_t rc_bitfield
= 0;
838 struct rtattr
*pd_reason_info
[IFLA_MAX
+ 1];
840 protodown
= !!*(uint8_t *)RTA_DATA(tb
[IFLA_PROTO_DOWN
]);
842 if (tb
[IFLA_PROTO_DOWN_REASON
]) {
843 netlink_parse_rtattr_nested(pd_reason_info
, IFLA_INFO_MAX
,
844 tb
[IFLA_PROTO_DOWN_REASON
]);
846 if (pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
])
847 rc_bitfield
= *(uint32_t *)RTA_DATA(
848 pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
]);
852 * Set our reason code to note it wasn't us.
853 * If the reason we got from the kernel is ONLY frr though, don't
856 COND_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_EXTERNAL
,
857 protodown
&& rc_bitfield
&&
858 !is_if_protodown_reason_only_frr(rc_bitfield
));
861 old_protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
862 if (protodown
== old_protodown
)
865 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
866 zlog_debug("interface %s dplane change, protdown %s",
867 zif
->ifp
->name
, protodown
? "on" : "off");
869 /* Set protodown, respectively */
870 COND_FLAG(zif
->flags
, ZIF_FLAG_PROTODOWN
, protodown
);
872 if (zebra_evpn_is_es_bond_member(zif
->ifp
)) {
873 /* Check it's not already being sent to the dplane first */
875 CHECK_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
)) {
876 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
878 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
884 CHECK_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
)) {
885 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
887 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
892 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
894 "bond mbr %s reinstate protodown %s in the dplane",
895 zif
->ifp
->name
, old_protodown
? "on" : "off");
898 SET_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
);
900 SET_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
);
902 dplane_intf_update(zif
->ifp
);
906 static uint8_t netlink_parse_lacp_bypass(struct rtattr
**linkinfo
)
909 struct rtattr
*mbrinfo
[IFLA_BOND_SLAVE_MAX
+ 1];
911 netlink_parse_rtattr_nested(mbrinfo
, IFLA_BOND_SLAVE_MAX
,
912 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
913 if (mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
])
914 bypass
= *(uint8_t *)RTA_DATA(
915 mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
]);
921 * Only called at startup to cleanup leftover protodown reasons we may
922 * have not cleaned up. We leave protodown set though.
924 static void if_sweep_protodown(struct zebra_if
*zif
)
928 protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
933 if (IS_ZEBRA_DEBUG_KERNEL
)
934 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
935 zif
->ifp
->name
, protodown
? "on" : "off",
938 /* Only clear our reason codes, leave external if it was set */
939 UNSET_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_ALL
);
940 dplane_intf_update(zif
->ifp
);
944 * Called from interface_lookup_netlink(). This function is only used
947 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
950 struct ifinfomsg
*ifi
;
951 struct rtattr
*tb
[IFLA_MAX
+ 1];
952 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
953 struct interface
*ifp
;
957 char *slave_kind
= NULL
;
958 struct zebra_ns
*zns
= NULL
;
959 vrf_id_t vrf_id
= VRF_DEFAULT
;
960 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
961 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
962 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
963 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
964 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
965 struct zebra_if
*zif
;
966 ns_id_t link_nsid
= ns_id
;
969 frrtrace(3, frr_zebra
, netlink_interface
, h
, ns_id
, startup
);
971 zns
= zebra_ns_lookup(ns_id
);
974 if (h
->nlmsg_type
!= RTM_NEWLINK
)
977 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
980 "%s: Message received from netlink is of a broken size: %d %zu",
981 __func__
, h
->nlmsg_len
,
982 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
986 /* We are interested in some AF_BRIDGE notifications. */
987 if (ifi
->ifi_family
== AF_BRIDGE
)
988 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
990 /* Looking up interface name. */
991 memset(linkinfo
, 0, sizeof(linkinfo
));
992 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
995 /* check for wireless messages to ignore */
996 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
997 if (IS_ZEBRA_DEBUG_KERNEL
)
998 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1003 if (tb
[IFLA_IFNAME
] == NULL
)
1005 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1007 if (tb
[IFLA_IFALIAS
])
1008 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1010 if (tb
[IFLA_LINKINFO
]) {
1011 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1014 if (linkinfo
[IFLA_INFO_KIND
])
1015 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1017 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1018 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1020 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
1021 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
1023 netlink_determine_zebra_iftype(kind
, &zif_type
);
1026 /* If VRF, create the VRF structure itself. */
1027 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1028 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1029 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1032 if (tb
[IFLA_MASTER
]) {
1033 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1034 && !vrf_is_backend_netns()) {
1035 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1036 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1037 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
1038 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1040 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1041 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
1042 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1043 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1044 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1046 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1048 if (vrf_is_backend_netns())
1049 vrf_id
= (vrf_id_t
)ns_id
;
1051 /* If linking to another interface, note it. */
1053 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1055 if (tb
[IFLA_LINK_NETNSID
]) {
1056 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1057 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1060 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1061 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
1063 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1064 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
1066 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
1067 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1069 /* Set zebra interface type */
1070 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1071 if (IS_ZEBRA_IF_VRF(ifp
))
1072 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1075 * Just set the @link/lower-device ifindex. During nldump interfaces are
1076 * not ordered in any fashion so we may end up getting upper devices
1077 * before lower devices. We will setup the real linkage once the dump
1080 zif
= (struct zebra_if
*)ifp
->info
;
1081 zif
->link_ifindex
= link_ifindex
;
1084 XFREE(MTYPE_TMP
, zif
->desc
);
1085 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1088 /* Hardware type and address. */
1089 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
1091 netlink_interface_update_hw_addr(tb
, ifp
);
1095 /* Extract and save L2 interface information, take additional actions.
1097 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
1099 if (IS_ZEBRA_IF_BOND(ifp
))
1100 zebra_l2if_update_bond(ifp
, true);
1101 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1102 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
,
1103 ZEBRA_BRIDGE_NO_ACTION
);
1104 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1105 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, !!bypass
);
1107 if (tb
[IFLA_PROTO_DOWN
]) {
1108 netlink_proc_dplane_if_protodown(zif
, tb
);
1109 if_sweep_protodown(zif
);
1115 /* Request for specific interface or address information from the kernel */
1116 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
1117 int type
, uint32_t filter_mask
)
1121 struct ifinfomsg ifm
;
1125 frrtrace(4, frr_zebra
, netlink_request_intf_addr
, netlink_cmd
, family
,
1128 /* Form the request, specifying filter (rtattr) if needed. */
1129 memset(&req
, 0, sizeof(req
));
1130 req
.n
.nlmsg_type
= type
;
1131 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1132 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1133 req
.ifm
.ifi_family
= family
;
1135 /* Include filter, if specified. */
1137 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
1139 return netlink_request(netlink_cmd
, &req
);
1142 enum netlink_msg_status
1143 netlink_put_gre_set_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1145 enum dplane_op_e op
;
1146 enum netlink_msg_status ret
;
1148 op
= dplane_ctx_get_op(ctx
);
1149 assert(op
== DPLANE_OP_GRE_SET
);
1151 ret
= netlink_batch_add_msg(bth
, ctx
, netlink_gre_set_msg_encoder
, false);
1156 /* Interface lookup by netlink socket. */
1157 int interface_lookup_netlink(struct zebra_ns
*zns
)
1160 struct zebra_dplane_info dp_info
;
1161 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1163 /* Capture key info from ns struct */
1164 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1166 /* Get interface information. */
1167 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
1170 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1175 /* Get interface information - for bridge interfaces. */
1176 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
1177 RTEXT_FILTER_BRVLAN
);
1180 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1185 /* fixup linkages */
1186 zebra_if_update_all_links(zns
);
1191 * interface_addr_lookup_netlink() - Look up interface addresses
1193 * @zns: Zebra netlink socket
1194 * Return: Result status
1196 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
1199 struct zebra_dplane_info dp_info
;
1200 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1202 /* Capture key info from ns struct */
1203 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1205 /* Get IPv4 address of the interfaces. */
1206 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
1209 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1214 /* Get IPv6 address of the interfaces. */
1215 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
1218 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1226 int kernel_interface_set_master(struct interface
*master
,
1227 struct interface
*slave
)
1229 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1233 struct ifinfomsg ifa
;
1234 char buf
[NL_PKT_BUF_SIZE
];
1237 memset(&req
, 0, sizeof(req
));
1239 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1240 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1241 req
.n
.nlmsg_type
= RTM_SETLINK
;
1242 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1244 req
.ifa
.ifi_index
= slave
->ifindex
;
1246 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
1247 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
1249 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1253 /* Interface address modification. */
1254 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
1255 void *buf
, size_t buflen
)
1258 const struct prefix
*p
;
1264 struct ifaddrmsg ifa
;
1268 if (buflen
< sizeof(*req
))
1271 p
= dplane_ctx_get_intf_addr(ctx
);
1272 memset(req
, 0, sizeof(*req
));
1274 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1276 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1277 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1279 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1284 req
->n
.nlmsg_type
= cmd
;
1285 req
->ifa
.ifa_family
= p
->family
;
1287 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1289 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1292 if (p
->family
== AF_INET
) {
1293 if (dplane_ctx_intf_is_connected(ctx
)) {
1294 p
= dplane_ctx_get_intf_dest(ctx
);
1295 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1296 &p
->u
.prefix
, bytelen
))
1298 } else if (cmd
== RTM_NEWADDR
) {
1299 struct in_addr broad
= {
1300 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1303 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1309 /* p is now either address or destination/bcast addr */
1310 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1312 if (dplane_ctx_intf_is_secondary(ctx
))
1313 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1315 if (dplane_ctx_intf_has_label(ctx
)) {
1316 label
= dplane_ctx_get_intf_label(ctx
);
1317 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1322 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1325 enum netlink_msg_status
1326 netlink_put_address_update_msg(struct nl_batch
*bth
,
1327 struct zebra_dplane_ctx
*ctx
)
1329 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1333 static ssize_t
netlink_intf_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
1336 enum dplane_op_e op
;
1339 op
= dplane_ctx_get_op(ctx
);
1342 case DPLANE_OP_INTF_UPDATE
:
1345 case DPLANE_OP_INTF_INSTALL
:
1348 case DPLANE_OP_INTF_DELETE
:
1353 EC_ZEBRA_NHG_FIB_UPDATE
,
1354 "Context received for kernel interface update with incorrect OP code (%u)",
1359 return netlink_intf_msg_encode(cmd
, ctx
, buf
, buflen
);
1362 enum netlink_msg_status
1363 netlink_put_intf_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1365 return netlink_batch_add_msg(bth
, ctx
, netlink_intf_msg_encoder
, false);
1368 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1371 struct ifaddrmsg
*ifa
;
1372 struct rtattr
*tb
[IFA_MAX
+ 1];
1373 struct interface
*ifp
;
1378 struct zebra_ns
*zns
;
1379 uint32_t metric
= METRIC_MAX
;
1380 uint32_t kernel_flags
= 0;
1382 frrtrace(3, frr_zebra
, netlink_interface_addr
, h
, ns_id
, startup
);
1384 zns
= zebra_ns_lookup(ns_id
);
1385 ifa
= NLMSG_DATA(h
);
1387 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1389 EC_ZEBRA_UNKNOWN_FAMILY
,
1390 "Invalid address family: %u received from kernel interface addr change: %s",
1391 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1395 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1398 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1401 "%s: Message received from netlink is of a broken size: %d %zu",
1402 __func__
, h
->nlmsg_len
,
1403 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1407 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1409 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1412 /* During startup, failure to lookup the referenced
1413 * interface should not be an error, so we have
1414 * downgraded this condition to warning, and we permit
1415 * the startup interface state retrieval to continue.
1417 flog_warn(EC_LIB_INTERFACE
,
1418 "%s: can't find interface by index %d",
1419 __func__
, ifa
->ifa_index
);
1422 flog_err(EC_LIB_INTERFACE
,
1423 "%s: can't find interface by index %d",
1424 __func__
, ifa
->ifa_index
);
1429 /* Flags passed through */
1431 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1433 kernel_flags
= ifa
->ifa_flags
;
1435 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1438 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1439 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1442 zlog_debug(" IFA_LOCAL %s/%d",
1443 inet_ntop(ifa
->ifa_family
,
1444 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1446 ifa
->ifa_prefixlen
);
1447 if (tb
[IFA_ADDRESS
])
1448 zlog_debug(" IFA_ADDRESS %s/%d",
1449 inet_ntop(ifa
->ifa_family
,
1450 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1452 ifa
->ifa_prefixlen
);
1453 if (tb
[IFA_BROADCAST
])
1454 zlog_debug(" IFA_BROADCAST %s/%d",
1455 inet_ntop(ifa
->ifa_family
,
1456 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1458 ifa
->ifa_prefixlen
);
1459 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1460 zlog_debug(" IFA_LABEL %s",
1461 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1463 if (tb
[IFA_CACHEINFO
]) {
1464 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1465 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1466 ci
->ifa_prefered
, ci
->ifa_valid
);
1470 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1471 if (tb
[IFA_LOCAL
] == NULL
)
1472 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1473 if (tb
[IFA_ADDRESS
] == NULL
)
1474 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1476 /* local interface address */
1477 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1479 /* is there a peer address? */
1481 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1482 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1483 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1484 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1486 /* seeking a broadcast address */
1487 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1490 /* addr is primary key, SOL if we don't have one */
1492 zlog_debug("%s: Local Interface Address is NULL for %s",
1493 __func__
, ifp
->name
);
1498 if (kernel_flags
& IFA_F_SECONDARY
)
1499 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1503 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1505 if (label
&& strcmp(ifp
->name
, label
) == 0)
1508 if (tb
[IFA_RT_PRIORITY
])
1509 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1511 /* Register interface address to the interface. */
1512 if (ifa
->ifa_family
== AF_INET
) {
1513 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1515 "Invalid prefix length: %u received from kernel interface addr change: %s",
1517 nl_msg_type_to_str(h
->nlmsg_type
));
1521 if (h
->nlmsg_type
== RTM_NEWADDR
)
1522 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1524 (struct in_addr
*)broad
, label
,
1526 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1527 /* Delete with a peer address */
1528 connected_delete_ipv4(
1529 ifp
, flags
, (struct in_addr
*)addr
,
1530 ifa
->ifa_prefixlen
, broad
);
1532 connected_delete_ipv4(
1533 ifp
, flags
, (struct in_addr
*)addr
,
1534 ifa
->ifa_prefixlen
, NULL
);
1537 if (ifa
->ifa_family
== AF_INET6
) {
1538 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1540 "Invalid prefix length: %u received from kernel interface addr change: %s",
1542 nl_msg_type_to_str(h
->nlmsg_type
));
1545 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1546 /* Only consider valid addresses; we'll not get a
1548 * the kernel till IPv6 DAD has completed, but at init
1550 * does query for and will receive all addresses.
1553 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1554 connected_add_ipv6(ifp
, flags
,
1555 (struct in6_addr
*)addr
,
1556 (struct in6_addr
*)broad
,
1557 ifa
->ifa_prefixlen
, label
,
1560 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1561 NULL
, ifa
->ifa_prefixlen
);
1565 * Linux kernel does not send route delete on interface down/addr del
1566 * so we have to re-process routes it owns (i.e. kernel routes)
1568 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1569 rib_update(RIB_UPDATE_KERNEL
);
1575 * Parse and validate an incoming interface address change message,
1576 * generating a dplane context object.
1577 * This runs in the dplane pthread; the context is enqueued to the
1578 * main pthread for processing.
1580 int netlink_interface_addr_dplane(struct nlmsghdr
*h
, ns_id_t ns_id
,
1581 int startup
/*ignored*/)
1584 struct ifaddrmsg
*ifa
;
1585 struct rtattr
*tb
[IFA_MAX
+ 1];
1589 uint32_t metric
= METRIC_MAX
;
1590 uint32_t kernel_flags
= 0;
1591 struct zebra_dplane_ctx
*ctx
;
1594 ifa
= NLMSG_DATA(h
);
1596 /* Validate message types */
1597 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1600 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1601 if (IS_ZEBRA_DEBUG_KERNEL
)
1602 zlog_debug("%s: %s: Invalid address family: %u",
1603 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1608 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1610 if (IS_ZEBRA_DEBUG_KERNEL
)
1611 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1612 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1614 (size_t)NLMSG_LENGTH(
1615 sizeof(struct ifaddrmsg
)));
1619 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1621 /* Flags passed through */
1623 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1625 kernel_flags
= ifa
->ifa_flags
;
1627 if (IS_ZEBRA_DEBUG_KERNEL
) { /* remove this line to see initial ifcfg */
1628 char buf
[PREFIX_STRLEN
];
1630 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__
,
1631 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
,
1632 ifa
->ifa_index
, kernel_flags
);
1634 zlog_debug(" IFA_LOCAL %s/%d",
1635 inet_ntop(ifa
->ifa_family
,
1636 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1638 ifa
->ifa_prefixlen
);
1639 if (tb
[IFA_ADDRESS
])
1640 zlog_debug(" IFA_ADDRESS %s/%d",
1641 inet_ntop(ifa
->ifa_family
,
1642 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1644 ifa
->ifa_prefixlen
);
1645 if (tb
[IFA_BROADCAST
])
1646 zlog_debug(" IFA_BROADCAST %s/%d",
1647 inet_ntop(ifa
->ifa_family
,
1648 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1650 ifa
->ifa_prefixlen
);
1652 zlog_debug(" IFA_LABEL %s",
1653 (const char *)RTA_DATA(tb
[IFA_LABEL
]));
1655 if (tb
[IFA_CACHEINFO
]) {
1656 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1658 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1659 ci
->ifa_prefered
, ci
->ifa_valid
);
1663 /* Validate prefix length */
1665 if (ifa
->ifa_family
== AF_INET
1666 && ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1667 if (IS_ZEBRA_DEBUG_KERNEL
)
1668 zlog_debug("%s: %s: Invalid prefix length: %u",
1669 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1670 ifa
->ifa_prefixlen
);
1674 if (ifa
->ifa_family
== AF_INET6
) {
1675 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1676 if (IS_ZEBRA_DEBUG_KERNEL
)
1677 zlog_debug("%s: %s: Invalid prefix length: %u",
1679 nl_msg_type_to_str(h
->nlmsg_type
),
1680 ifa
->ifa_prefixlen
);
1684 /* Only consider valid addresses; we'll not get a kernel
1685 * notification till IPv6 DAD has completed, but at init
1686 * time, FRR does query for and will receive all addresses.
1688 if (h
->nlmsg_type
== RTM_NEWADDR
1689 && (kernel_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))) {
1690 if (IS_ZEBRA_DEBUG_KERNEL
)
1691 zlog_debug("%s: %s: Invalid/tentative addr",
1693 nl_msg_type_to_str(h
->nlmsg_type
));
1698 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1699 if (tb
[IFA_LOCAL
] == NULL
)
1700 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1701 if (tb
[IFA_ADDRESS
] == NULL
)
1702 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1704 /* local interface address */
1705 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1707 /* addr is primary key, SOL if we don't have one */
1709 if (IS_ZEBRA_DEBUG_KERNEL
)
1710 zlog_debug("%s: %s: No local interface address",
1711 __func__
, nl_msg_type_to_str(h
->nlmsg_type
));
1715 /* Allocate a context object, now that validation is done. */
1716 ctx
= dplane_ctx_alloc();
1717 if (h
->nlmsg_type
== RTM_NEWADDR
)
1718 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_ADD
);
1720 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_DEL
);
1722 dplane_ctx_set_ifindex(ctx
, ifa
->ifa_index
);
1723 dplane_ctx_set_ns_id(ctx
, ns_id
);
1725 /* Convert addr to prefix */
1726 memset(&p
, 0, sizeof(p
));
1727 p
.family
= ifa
->ifa_family
;
1728 p
.prefixlen
= ifa
->ifa_prefixlen
;
1729 if (p
.family
== AF_INET
)
1730 p
.u
.prefix4
= *(struct in_addr
*)addr
;
1732 p
.u
.prefix6
= *(struct in6_addr
*)addr
;
1734 dplane_ctx_set_intf_addr(ctx
, &p
);
1736 /* is there a peer address? */
1738 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1739 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1740 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1741 dplane_ctx_intf_set_connected(ctx
);
1742 } else if (tb
[IFA_BROADCAST
]) {
1743 /* seeking a broadcast address */
1744 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
1745 dplane_ctx_intf_set_broadcast(ctx
);
1750 /* Convert addr to prefix */
1751 memset(&p
, 0, sizeof(p
));
1752 p
.family
= ifa
->ifa_family
;
1753 p
.prefixlen
= ifa
->ifa_prefixlen
;
1754 if (p
.family
== AF_INET
)
1755 p
.u
.prefix4
= *(struct in_addr
*)broad
;
1757 p
.u
.prefix6
= *(struct in6_addr
*)broad
;
1759 dplane_ctx_set_intf_dest(ctx
, &p
);
1763 if (kernel_flags
& IFA_F_SECONDARY
)
1764 dplane_ctx_intf_set_secondary(ctx
);
1767 if (tb
[IFA_LABEL
]) {
1768 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1769 dplane_ctx_set_intf_label(ctx
, label
);
1772 if (tb
[IFA_RT_PRIORITY
])
1773 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1775 dplane_ctx_set_intf_metric(ctx
, metric
);
1777 /* Enqueue ctx for main pthread to process */
1778 dplane_provider_enqueue_to_zebra(ctx
);
1783 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1786 struct ifinfomsg
*ifi
;
1787 struct rtattr
*tb
[IFLA_MAX
+ 1];
1788 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1789 struct interface
*ifp
;
1793 char *slave_kind
= NULL
;
1794 struct zebra_ns
*zns
;
1795 vrf_id_t vrf_id
= VRF_DEFAULT
;
1796 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1797 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1798 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1799 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1800 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1801 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1802 struct zebra_if
*zif
;
1803 ns_id_t link_nsid
= ns_id
;
1804 ifindex_t master_infindex
= IFINDEX_INTERNAL
;
1807 zns
= zebra_ns_lookup(ns_id
);
1808 ifi
= NLMSG_DATA(h
);
1810 /* assume if not default zns, then new VRF */
1811 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1812 /* If this is not link add/delete message so print warning. */
1813 zlog_debug("netlink_link_change: wrong kernel message %s",
1814 nl_msg_type_to_str(h
->nlmsg_type
));
1818 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1819 || ifi
->ifi_family
== AF_INET6
)) {
1821 EC_ZEBRA_UNKNOWN_FAMILY
,
1822 "Invalid address family: %u received from kernel link change: %s",
1823 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1827 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1830 "%s: Message received from netlink is of a broken size %d %zu",
1831 __func__
, h
->nlmsg_len
,
1832 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1836 /* We are interested in some AF_BRIDGE notifications. */
1837 if (ifi
->ifi_family
== AF_BRIDGE
)
1838 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1840 /* Looking up interface name. */
1841 memset(linkinfo
, 0, sizeof(linkinfo
));
1842 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
1845 /* check for wireless messages to ignore */
1846 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1847 if (IS_ZEBRA_DEBUG_KERNEL
)
1848 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1853 if (tb
[IFLA_IFNAME
] == NULL
)
1855 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1857 /* Must be valid string. */
1858 len
= RTA_PAYLOAD(tb
[IFLA_IFNAME
]);
1859 if (len
< 2 || name
[len
- 1] != '\0') {
1860 if (IS_ZEBRA_DEBUG_KERNEL
)
1861 zlog_debug("%s: invalid intf name", __func__
);
1865 if (tb
[IFLA_LINKINFO
]) {
1866 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1869 if (linkinfo
[IFLA_INFO_KIND
])
1870 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1872 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1873 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1875 netlink_determine_zebra_iftype(kind
, &zif_type
);
1878 /* If linking to another interface, note it. */
1880 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1882 if (tb
[IFLA_LINK_NETNSID
]) {
1883 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1884 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1886 if (tb
[IFLA_IFALIAS
]) {
1887 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1890 /* See if interface is present. */
1891 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1893 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1894 /* If VRF, create or update the VRF structure itself. */
1895 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1896 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1897 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1900 if (tb
[IFLA_MASTER
]) {
1901 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1902 && !vrf_is_backend_netns()) {
1903 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1904 master_infindex
= vrf_id
=
1905 *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1906 } else if (slave_kind
1907 && (strcmp(slave_kind
, "bridge") == 0)) {
1908 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1909 master_infindex
= bridge_ifindex
=
1910 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1911 } else if (slave_kind
1912 && (strcmp(slave_kind
, "bond") == 0)) {
1913 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1914 master_infindex
= bond_ifindex
=
1915 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1916 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1918 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1920 if (vrf_is_backend_netns())
1921 vrf_id
= (vrf_id_t
)ns_id
;
1923 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1924 /* Add interface notification from kernel */
1925 if (IS_ZEBRA_DEBUG_KERNEL
)
1927 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1928 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1929 zif_slave_type
, master_infindex
,
1933 /* unknown interface */
1934 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1936 /* pre-configured interface, learnt now */
1937 if (ifp
->vrf
->vrf_id
!= vrf_id
)
1938 if_update_to_new_vrf(ifp
, vrf_id
);
1941 /* Update interface information. */
1942 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1943 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1944 if (!tb
[IFLA_MTU
]) {
1946 "RTM_NEWLINK for interface %s(%u) without MTU set",
1947 name
, ifi
->ifi_index
);
1950 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1952 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1954 /* Set interface type */
1955 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1956 if (IS_ZEBRA_IF_VRF(ifp
))
1957 SET_FLAG(ifp
->status
,
1958 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1961 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
1964 netlink_to_zebra_link_type(ifi
->ifi_type
);
1965 netlink_interface_update_hw_addr(tb
, ifp
);
1967 /* Inform clients, install any configured addresses. */
1970 /* Extract and save L2 interface information, take
1971 * additional actions. */
1972 netlink_interface_update_l2info(
1973 ifp
, linkinfo
[IFLA_INFO_DATA
],
1975 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1976 zebra_l2if_update_bridge_slave(
1977 ifp
, bridge_ifindex
, ns_id
,
1978 ZEBRA_BRIDGE_NO_ACTION
);
1979 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1980 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
1983 if (tb
[IFLA_PROTO_DOWN
])
1984 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
1986 } else if (ifp
->vrf
->vrf_id
!= vrf_id
) {
1987 /* VRF change for an interface. */
1988 if (IS_ZEBRA_DEBUG_KERNEL
)
1990 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1991 name
, ifp
->ifindex
, ifp
->vrf
->vrf_id
,
1992 vrf_id
, ifi
->ifi_flags
);
1994 if_handle_vrf_change(ifp
, vrf_id
);
1996 bool was_bridge_slave
, was_bond_slave
;
1997 uint8_t chgflags
= ZEBRA_BRIDGE_NO_ACTION
;
2000 /* Interface update. */
2001 if (IS_ZEBRA_DEBUG_KERNEL
)
2003 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
2004 name
, ifp
->ifindex
, zif_slave_type
,
2005 master_infindex
, ifi
->ifi_flags
);
2007 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2008 if (!tb
[IFLA_MTU
]) {
2010 "RTM_NEWLINK for interface %s(%u) without MTU set",
2011 name
, ifi
->ifi_index
);
2014 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2017 /* Update interface type - NOTE: Only slave_type can
2019 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
2020 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
2021 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2023 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
2026 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2029 netlink_to_zebra_link_type(ifi
->ifi_type
);
2030 netlink_interface_update_hw_addr(tb
, ifp
);
2032 if (tb
[IFLA_PROTO_DOWN
])
2033 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2035 if (if_is_no_ptm_operative(ifp
)) {
2036 bool is_up
= if_is_operative(ifp
);
2037 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2038 if (!if_is_no_ptm_operative(ifp
) ||
2039 CHECK_FLAG(zif
->flags
,
2040 ZIF_FLAG_PROTODOWN
)) {
2041 if (IS_ZEBRA_DEBUG_KERNEL
)
2043 "Intf %s(%u) has gone DOWN",
2044 name
, ifp
->ifindex
);
2046 rib_update(RIB_UPDATE_KERNEL
);
2047 } else if (if_is_operative(ifp
)) {
2048 bool mac_updated
= false;
2050 /* Must notify client daemons of new
2051 * interface status. */
2052 if (IS_ZEBRA_DEBUG_KERNEL
)
2054 "Intf %s(%u) PTM up, notifying clients",
2055 name
, ifp
->ifindex
);
2058 /* Update EVPN VNI when SVI MAC change
2060 if (memcmp(old_hw_addr
, ifp
->hw_addr
,
2061 INTERFACE_HWADDR_MAX
))
2063 if (IS_ZEBRA_IF_VLAN(ifp
)
2065 struct interface
*link_if
;
2068 if_lookup_by_index_per_ns(
2069 zebra_ns_lookup(NS_DEFAULT
),
2072 zebra_vxlan_svi_up(ifp
,
2074 } else if (mac_updated
2075 && IS_ZEBRA_IF_BRIDGE(ifp
)) {
2077 "Intf %s(%u) bridge changed MAC address",
2078 name
, ifp
->ifindex
);
2080 ZEBRA_BRIDGE_MASTER_MAC_CHANGE
;
2084 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2085 if (if_is_operative(ifp
) &&
2086 !CHECK_FLAG(zif
->flags
,
2087 ZIF_FLAG_PROTODOWN
)) {
2088 if (IS_ZEBRA_DEBUG_KERNEL
)
2090 "Intf %s(%u) has come UP",
2091 name
, ifp
->ifindex
);
2093 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2095 ZEBRA_BRIDGE_MASTER_UP
;
2097 if (IS_ZEBRA_DEBUG_KERNEL
)
2099 "Intf %s(%u) has gone DOWN",
2100 name
, ifp
->ifindex
);
2102 rib_update(RIB_UPDATE_KERNEL
);
2106 /* Extract and save L2 interface information, take
2107 * additional actions. */
2108 netlink_interface_update_l2info(
2109 ifp
, linkinfo
[IFLA_INFO_DATA
],
2111 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2112 zebra_l2if_update_bridge(ifp
, chgflags
);
2113 if (IS_ZEBRA_IF_BOND(ifp
))
2114 zebra_l2if_update_bond(ifp
, true);
2115 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
2116 zebra_l2if_update_bridge_slave(
2117 ifp
, bridge_ifindex
, ns_id
, chgflags
);
2118 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
2119 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2125 XFREE(MTYPE_TMP
, zif
->desc
);
2127 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
2130 /* Delete interface notification from kernel */
2132 if (IS_ZEBRA_DEBUG_KERNEL
)
2134 "RTM_DELLINK for unknown interface %s(%u)",
2135 name
, ifi
->ifi_index
);
2139 if (IS_ZEBRA_DEBUG_KERNEL
)
2140 zlog_debug("RTM_DELLINK for %s(%u)", name
,
2143 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
2145 if (IS_ZEBRA_IF_BOND(ifp
))
2146 zebra_l2if_update_bond(ifp
, false);
2147 if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2148 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, false);
2149 /* Special handling for bridge or VxLAN interfaces. */
2150 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2151 zebra_l2_bridge_del(ifp
);
2152 else if (IS_ZEBRA_IF_VXLAN(ifp
))
2153 zebra_l2_vxlanif_del(ifp
);
2155 if_delete_update(&ifp
);
2157 /* If VRF, delete the VRF structure itself. */
2158 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns())
2159 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2166 * Interface encoding helper function.
2168 * \param[in] cmd netlink command.
2169 * \param[in] ctx dataplane context (information snapshot).
2170 * \param[out] buf buffer to hold the packet.
2171 * \param[in] buflen amount of buffer bytes.
2174 ssize_t
netlink_intf_msg_encode(uint16_t cmd
,
2175 const struct zebra_dplane_ctx
*ctx
, void *buf
,
2180 struct ifinfomsg ifa
;
2184 struct rtattr
*nest_protodown_reason
;
2185 ifindex_t ifindex
= dplane_ctx_get_ifindex(ctx
);
2186 bool down
= dplane_ctx_intf_is_protodown(ctx
);
2187 bool pd_reason_val
= dplane_ctx_get_intf_pd_reason_val(ctx
);
2189 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2191 if (buflen
< sizeof(*req
))
2194 memset(req
, 0, sizeof(*req
));
2196 if (cmd
!= RTM_SETLINK
)
2198 EC_ZEBRA_INTF_UPDATE_FAILURE
,
2199 "Only RTM_SETLINK message type currently supported in dplane pthread");
2201 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2202 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2203 req
->n
.nlmsg_type
= cmd
;
2204 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2206 req
->ifa
.ifi_index
= ifindex
;
2208 nl_attr_put8(&req
->n
, buflen
, IFLA_PROTO_DOWN
, down
);
2209 nl_attr_put32(&req
->n
, buflen
, IFLA_LINK
, ifindex
);
2211 /* Reason info nest */
2212 nest_protodown_reason
=
2213 nl_attr_nest(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON
);
2215 if (!nest_protodown_reason
)
2218 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_MASK
,
2219 (1 << frr_protodown_r_bit
));
2220 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_VALUE
,
2221 ((int)pd_reason_val
) << frr_protodown_r_bit
);
2223 nl_attr_nest_end(&req
->n
, nest_protodown_reason
);
2225 if (IS_ZEBRA_DEBUG_KERNEL
)
2226 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2227 __func__
, nl_msg_type_to_str(cmd
), down
,
2228 pd_reason_val
, ifindex
);
2230 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2233 /* Interface information read by netlink. */
2234 void interface_list(struct zebra_ns
*zns
)
2236 interface_lookup_netlink(zns
);
2237 /* We add routes for interface address,
2238 * so we need to get the nexthop info
2239 * from the kernel before we can do that
2241 netlink_nexthop_read(zns
);
2243 interface_addr_lookup_netlink(zns
);
2246 void if_netlink_set_frr_protodown_r_bit(uint8_t bit
)
2248 if (IS_ZEBRA_DEBUG_KERNEL
)
2250 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2251 frr_protodown_r_bit
, bit
);
2253 frr_protodown_r_bit
= bit
;
2256 void if_netlink_unset_frr_protodown_r_bit(void)
2258 if (IS_ZEBRA_DEBUG_KERNEL
)
2260 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2261 frr_protodown_r_bit
, FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2263 frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
2267 bool if_netlink_frr_protodown_r_bit_is_set(void)
2269 return (frr_protodown_r_bit
!= FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2272 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2274 return frr_protodown_r_bit
;
2277 #endif /* GNU_LINUX */