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"
79 extern struct zebra_privs_t zserv_privs
;
80 uint8_t frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
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
,
87 struct interface
*oifp
;
89 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
91 if (ifi_index
== IFINDEX_INTERNAL
)
94 "Netlink is setting interface %s ifindex to reserved internal value %u",
95 ifp
->name
, ifi_index
);
97 if (IS_ZEBRA_DEBUG_KERNEL
)
99 "interface index %d was renamed from %s to %s",
100 ifi_index
, oifp
->name
, ifp
->name
);
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
);
109 if_set_index(ifp
, ifi_index
);
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
)
118 if (tb
[IFLA_ADDRESS
]) {
121 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
123 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
124 zlog_debug("Hardware address is too large: %d",
127 ifp
->hw_addr_len
= hw_addr_len
;
128 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
131 for (i
= 0; i
< hw_addr_len
; i
++)
132 if (ifp
->hw_addr
[i
] != 0)
135 if (i
== hw_addr_len
)
136 ifp
->hw_addr_len
= 0;
138 ifp
->hw_addr_len
= hw_addr_len
;
143 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
147 return ZEBRA_LLT_ETHER
;
149 return ZEBRA_LLT_EETHER
;
151 return ZEBRA_LLT_AX25
;
153 return ZEBRA_LLT_PRONET
;
155 return ZEBRA_LLT_IEEE802
;
157 return ZEBRA_LLT_ARCNET
;
158 case ARPHRD_APPLETLK
:
159 return ZEBRA_LLT_APPLETLK
;
161 return ZEBRA_LLT_DLCI
;
163 return ZEBRA_LLT_ATM
;
164 case ARPHRD_METRICOM
:
165 return ZEBRA_LLT_METRICOM
;
166 case ARPHRD_IEEE1394
:
167 return ZEBRA_LLT_IEEE1394
;
169 return ZEBRA_LLT_EUI64
;
170 case ARPHRD_INFINIBAND
:
171 return ZEBRA_LLT_INFINIBAND
;
173 return ZEBRA_LLT_SLIP
;
175 return ZEBRA_LLT_CSLIP
;
177 return ZEBRA_LLT_SLIP6
;
179 return ZEBRA_LLT_CSLIP6
;
181 return ZEBRA_LLT_RSRVD
;
183 return ZEBRA_LLT_ADAPT
;
185 return ZEBRA_LLT_ROSE
;
187 return ZEBRA_LLT_X25
;
189 return ZEBRA_LLT_PPP
;
191 return ZEBRA_LLT_CHDLC
;
193 return ZEBRA_LLT_LAPB
;
195 return ZEBRA_LLT_RAWHDLC
;
197 return ZEBRA_LLT_IPIP
;
199 return ZEBRA_LLT_IPIP6
;
201 return ZEBRA_LLT_FRAD
;
203 return ZEBRA_LLT_SKIP
;
204 case ARPHRD_LOOPBACK
:
205 return ZEBRA_LLT_LOOPBACK
;
206 case ARPHRD_LOCALTLK
:
207 return ZEBRA_LLT_LOCALTLK
;
209 return ZEBRA_LLT_FDDI
;
211 return ZEBRA_LLT_SIT
;
213 return ZEBRA_LLT_IPDDP
;
215 return ZEBRA_LLT_IPGRE
;
217 return ZEBRA_LLT_PIMREG
;
219 return ZEBRA_LLT_HIPPI
;
221 return ZEBRA_LLT_ECONET
;
223 return ZEBRA_LLT_IRDA
;
225 return ZEBRA_LLT_FCPP
;
227 return ZEBRA_LLT_FCAL
;
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
;
242 return ZEBRA_LLT_IP6GRE
;
244 #ifdef ARPHRD_IEEE802154_PHY
245 case ARPHRD_IEEE802154_PHY
:
246 return ZEBRA_LLT_IEEE802154_PHY
;
250 return ZEBRA_LLT_UNKNOWN
;
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
)
258 struct zebra_if
*zif
;
260 zif
= (struct zebra_if
*)ifp
->info
;
261 zif
->zif_slave_type
= zif_slave_type
;
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.
270 zebra_evpn_if_init(zif
);
274 static void netlink_determine_zebra_iftype(const char *kind
,
275 enum zebra_iftype
*zif_type
)
277 *zif_type
= ZEBRA_IF_OTHER
;
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
;
302 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
303 uint32_t ns_id
, const char *name
)
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
;
314 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
316 if (!linkinfo
[IFLA_INFO_DATA
]) {
317 if (IS_ZEBRA_DEBUG_KERNEL
)
319 "%s: IFLA_INFO_DATA missing from VRF message: %s",
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
)
329 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
334 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
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
);
341 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
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
);
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
);
356 vrf
= vrf_update((vrf_id_t
)ifi
->ifi_index
, name
);
358 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
359 name
, ifi
->ifi_index
);
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.
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.
375 zvrf
= (struct zebra_vrf
*)vrf
->info
;
376 zvrf
->table_id
= nl_table_id
;
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
,
386 } else // h->nlmsg_type == RTM_DELLINK
388 if (IS_ZEBRA_DEBUG_KERNEL
)
389 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
392 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
395 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
404 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
407 struct ethtool_cmd ecmd
;
410 const char *ifname
= interface
->name
;
414 /* initialize struct */
415 memset(&ifdata
, 0, sizeof(ifdata
));
417 /* set interface name */
418 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
420 /* initialize ethtool interface */
421 memset(&ecmd
, 0, sizeof(ecmd
));
422 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
423 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
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
);
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 */
438 /* Get the current link state for the interface */
439 rc
= vrf_ioctl(interface
->vrf
->vrf_id
, sd
, SIOCETHTOOL
,
443 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
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
)
456 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
459 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
461 return get_iflink_speed(ifp
, error
);
465 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
470 struct ifinfomsg ifi
;
475 struct rtattr
*rta_info
, *rta_data
;
476 const struct zebra_l2info_gre
*gre_info
;
478 if (buflen
< sizeof(*req
))
480 memset(req
, 0, sizeof(*req
));
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
;
486 req
->ifi
.ifi_index
= dplane_ctx_get_ifindex(ctx
);
488 gre_info
= dplane_ctx_gre_get_info(ctx
);
492 req
->ifi
.ifi_change
= 0xFFFFFFFF;
493 link_idx
= dplane_ctx_gre_get_link_ifindex(ctx
);
494 mtu
= dplane_ctx_gre_get_mtu(ctx
);
496 if (mtu
&& !nl_attr_put32(&req
->n
, buflen
, IFLA_MTU
, mtu
))
499 rta_info
= nl_attr_nest(&req
->n
, buflen
, IFLA_LINKINFO
);
503 if (!nl_attr_put(&req
->n
, buflen
, IFLA_INFO_KIND
, "gre", 3))
506 rta_data
= nl_attr_nest(&req
->n
, buflen
, IFLA_INFO_DATA
);
510 if (!nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LINK
, link_idx
))
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
))
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
))
523 if (gre_info
->ikey
&&
524 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
527 if (gre_info
->okey
&&
528 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
532 nl_attr_nest_end(&req
->n
, rta_data
);
533 nl_attr_nest_end(&req
->n
, rta_info
);
535 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
538 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
539 struct zebra_l2info_bridge
*bridge_info
)
541 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
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
]);
551 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
552 struct zebra_l2info_vlan
*vlan_info
)
554 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
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");
565 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
566 vlan_info
->vid
= vid_in_msg
;
570 static int netlink_extract_gre_info(struct rtattr
*link_data
,
571 struct zebra_l2info_gre
*gre_info
)
573 struct rtattr
*attr
[IFLA_GRE_MAX
+ 1];
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
);
579 if (!attr
[IFLA_GRE_LOCAL
]) {
580 if (IS_ZEBRA_DEBUG_KERNEL
)
582 "IFLA_GRE_LOCAL missing from GRE IF message");
585 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_LOCAL
]);
586 if (!attr
[IFLA_GRE_REMOTE
]) {
587 if (IS_ZEBRA_DEBUG_KERNEL
)
589 "IFLA_GRE_REMOTE missing from GRE IF message");
591 gre_info
->vtep_ip_remote
=
592 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_REMOTE
]);
594 if (!attr
[IFLA_GRE_LINK
]) {
595 if (IS_ZEBRA_DEBUG_KERNEL
)
596 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
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
);
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
]);
611 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
612 struct zebra_l2info_vxlan
*vxl_info
)
614 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
616 struct in_addr vtep_ip_in_msg
;
617 ifindex_t ifindex_link
;
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
)
624 "IFLA_VXLAN_ID missing from VXLAN IF message");
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
)
633 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
636 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
637 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
640 if (attr
[IFLA_VXLAN_GROUP
]) {
641 vxl_info
->mcast_grp
=
642 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_GROUP
]);
645 if (!attr
[IFLA_VXLAN_LINK
]) {
646 if (IS_ZEBRA_DEBUG_KERNEL
)
647 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
650 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
651 vxl_info
->ifindex_link
= ifindex_link
;
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.
661 static void netlink_interface_update_l2info(struct interface
*ifp
,
662 struct rtattr
*link_data
, int add
,
668 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
669 struct zebra_l2info_bridge bridge_info
;
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
;
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
;
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
,
690 } else if (IS_ZEBRA_IF_GRE(ifp
)) {
691 struct zebra_l2info_gre gre_info
;
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
,
703 static int netlink_bridge_vxlan_update(struct interface
*ifp
,
704 struct rtattr
*af_spec
)
706 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
707 struct bridge_vlan_info
*vinfo
;
708 vlanid_t access_vlan
;
713 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
714 * only 1 access VLAN is accepted.
716 netlink_parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, af_spec
);
717 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
720 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
721 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
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
);
732 static void netlink_bridge_vlan_update(struct interface
*ifp
,
733 struct rtattr
*af_spec
)
737 uint16_t vid_range_start
= 0;
738 struct zebra_if
*zif
;
739 bitfield_t old_vlan_bitmap
;
740 struct bridge_vlan_info
*vinfo
;
742 zif
= (struct zebra_if
*)ifp
->info
;
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
);
750 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
);
751 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
753 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
758 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
759 vid_range_start
= vinfo
->vid
;
763 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
764 vid_range_start
= vinfo
->vid
;
766 zebra_vlan_bitmap_compute(ifp
, vid_range_start
,
771 zebra_vlan_mbr_re_eval(ifp
, old_vlan_bitmap
);
773 bf_free(old_vlan_bitmap
);
776 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
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
;
786 /* Fetch name and ifindex */
788 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
790 if (tb
[IFLA_IFNAME
] == NULL
)
792 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
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
);
797 zlog_debug("Cannot find bridge IF %s(%u)", name
,
802 /* We are only interested in the access VLAN i.e., AF_SPEC */
803 af_spec
= tb
[IFLA_AF_SPEC
];
805 if (IS_ZEBRA_IF_VXLAN(ifp
))
806 return netlink_bridge_vxlan_update(ifp
, af_spec
);
808 /* build vlan bitmap associated with this interface if that
809 * device type is interested in the vlans
811 zif
= (struct zebra_if
*)ifp
->info
;
812 if (bf_is_inited(zif
->vlan_bitmap
))
813 netlink_bridge_vlan_update(ifp
, af_spec
);
818 /* If the interface is an es bond member then it must follow EVPN's
821 static void netlink_proc_dplane_if_protodown(struct zebra_if
*zif
,
826 /* Set our reason code to note it wasn't us */
828 zif
->protodown_rc
|= ZEBRA_PROTODOWN_EXTERNAL
;
830 zif
->protodown_rc
&= ~ZEBRA_PROTODOWN_EXTERNAL
;
832 zif_protodown
= !!(zif
->flags
& ZIF_FLAG_PROTODOWN
);
833 if (protodown
== zif_protodown
)
836 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
837 zlog_debug("interface %s dplane change, protdown %s",
838 zif
->ifp
->name
, protodown
? "on" : "off");
840 if (zebra_evpn_is_es_bond_member(zif
->ifp
)) {
841 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
843 "bond mbr %s re-instate protdown %s in the dplane",
844 zif
->ifp
->name
, zif_protodown
? "on" : "off");
845 netlink_protodown(zif
->ifp
, zif_protodown
, zif
->protodown_rc
);
848 zif
->flags
|= ZIF_FLAG_PROTODOWN
;
850 zif
->flags
&= ~ZIF_FLAG_PROTODOWN
;
854 static uint8_t netlink_parse_lacp_bypass(struct rtattr
**linkinfo
)
857 struct rtattr
*mbrinfo
[IFLA_BOND_SLAVE_MAX
+ 1];
859 netlink_parse_rtattr_nested(mbrinfo
, IFLA_BOND_SLAVE_MAX
,
860 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
861 if (mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
])
862 bypass
= *(uint8_t *)RTA_DATA(
863 mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
]);
869 * Called from interface_lookup_netlink(). This function is only used
872 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
875 struct ifinfomsg
*ifi
;
876 struct rtattr
*tb
[IFLA_MAX
+ 1];
877 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
878 struct interface
*ifp
;
882 char *slave_kind
= NULL
;
883 struct zebra_ns
*zns
= NULL
;
884 vrf_id_t vrf_id
= VRF_DEFAULT
;
885 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
886 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
887 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
888 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
889 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
890 struct zebra_if
*zif
;
891 ns_id_t link_nsid
= ns_id
;
894 zns
= zebra_ns_lookup(ns_id
);
897 if (h
->nlmsg_type
!= RTM_NEWLINK
)
900 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
903 "%s: Message received from netlink is of a broken size: %d %zu",
904 __func__
, h
->nlmsg_len
,
905 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
909 /* We are interested in some AF_BRIDGE notifications. */
910 if (ifi
->ifi_family
== AF_BRIDGE
)
911 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
913 /* Looking up interface name. */
914 memset(linkinfo
, 0, sizeof(linkinfo
));
915 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
917 /* check for wireless messages to ignore */
918 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
919 if (IS_ZEBRA_DEBUG_KERNEL
)
920 zlog_debug("%s: ignoring IFLA_WIRELESS message",
925 if (tb
[IFLA_IFNAME
] == NULL
)
927 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
929 if (tb
[IFLA_IFALIAS
])
930 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
932 if (tb
[IFLA_LINKINFO
]) {
933 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
936 if (linkinfo
[IFLA_INFO_KIND
])
937 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
939 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
940 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
942 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
943 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
945 netlink_determine_zebra_iftype(kind
, &zif_type
);
948 /* If VRF, create the VRF structure itself. */
949 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
950 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
951 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
954 if (tb
[IFLA_MASTER
]) {
955 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
956 && !vrf_is_backend_netns()) {
957 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
958 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
959 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
960 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
962 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
963 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
964 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
965 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
966 bypass
= netlink_parse_lacp_bypass(linkinfo
);
968 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
970 if (vrf_is_backend_netns())
971 vrf_id
= (vrf_id_t
)ns_id
;
973 /* If linking to another interface, note it. */
975 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
977 if (tb
[IFLA_LINK_NETNSID
]) {
978 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
979 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
982 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
983 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
985 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
986 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
988 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
989 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
991 /* Set zebra interface type */
992 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
993 if (IS_ZEBRA_IF_VRF(ifp
))
994 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
997 * Just set the @link/lower-device ifindex. During nldump interfaces are
998 * not ordered in any fashion so we may end up getting upper devices
999 * before lower devices. We will setup the real linkage once the dump
1002 zif
= (struct zebra_if
*)ifp
->info
;
1003 zif
->link_ifindex
= link_ifindex
;
1006 XFREE(MTYPE_TMP
, zif
->desc
);
1007 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1010 /* Hardware type and address. */
1011 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
1013 netlink_interface_update_hw_addr(tb
, ifp
);
1017 /* Extract and save L2 interface information, take additional actions.
1019 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
1021 if (IS_ZEBRA_IF_BOND(ifp
))
1022 zebra_l2if_update_bond(ifp
, true);
1023 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1024 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
,
1025 ZEBRA_BRIDGE_NO_ACTION
);
1026 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1027 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, !!bypass
);
1029 if (tb
[IFLA_PROTO_DOWN
]) {
1032 protodown
= *(uint8_t *)RTA_DATA(tb
[IFLA_PROTO_DOWN
]);
1033 netlink_proc_dplane_if_protodown(zif
, !!protodown
);
1039 /* Request for specific interface or address information from the kernel */
1040 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
1041 int type
, uint32_t filter_mask
)
1045 struct ifinfomsg ifm
;
1049 /* Form the request, specifying filter (rtattr) if needed. */
1050 memset(&req
, 0, sizeof(req
));
1051 req
.n
.nlmsg_type
= type
;
1052 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1053 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1054 req
.ifm
.ifi_family
= family
;
1056 /* Include filter, if specified. */
1058 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
1060 return netlink_request(netlink_cmd
, &req
);
1063 enum netlink_msg_status
1064 netlink_put_gre_set_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1066 enum dplane_op_e op
;
1067 enum netlink_msg_status ret
;
1069 op
= dplane_ctx_get_op(ctx
);
1070 assert(op
== DPLANE_OP_GRE_SET
);
1072 ret
= netlink_batch_add_msg(bth
, ctx
, netlink_gre_set_msg_encoder
, false);
1077 /* Interface lookup by netlink socket. */
1078 int interface_lookup_netlink(struct zebra_ns
*zns
)
1081 struct zebra_dplane_info dp_info
;
1082 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1084 /* Capture key info from ns struct */
1085 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1087 /* Get interface information. */
1088 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
1091 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1096 /* Get interface information - for bridge interfaces. */
1097 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
1098 RTEXT_FILTER_BRVLAN
);
1101 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1106 /* fixup linkages */
1107 zebra_if_update_all_links(zns
);
1112 * interface_addr_lookup_netlink() - Look up interface addresses
1114 * @zns: Zebra netlink socket
1115 * Return: Result status
1117 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
1120 struct zebra_dplane_info dp_info
;
1121 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1123 /* Capture key info from ns struct */
1124 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1126 /* Get IPv4 address of the interfaces. */
1127 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
1130 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1135 /* Get IPv6 address of the interfaces. */
1136 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
1139 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1147 int kernel_interface_set_master(struct interface
*master
,
1148 struct interface
*slave
)
1150 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1154 struct ifinfomsg ifa
;
1155 char buf
[NL_PKT_BUF_SIZE
];
1158 memset(&req
, 0, sizeof(req
));
1160 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1161 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1162 req
.n
.nlmsg_type
= RTM_SETLINK
;
1163 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1165 req
.ifa
.ifi_index
= slave
->ifindex
;
1167 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
1168 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
1170 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1174 /* Interface address modification. */
1175 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
1176 void *buf
, size_t buflen
)
1179 const struct prefix
*p
;
1185 struct ifaddrmsg ifa
;
1189 if (buflen
< sizeof(*req
))
1192 p
= dplane_ctx_get_intf_addr(ctx
);
1193 memset(req
, 0, sizeof(*req
));
1195 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1197 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1198 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1200 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1205 req
->n
.nlmsg_type
= cmd
;
1206 req
->ifa
.ifa_family
= p
->family
;
1208 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1210 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1213 if (p
->family
== AF_INET
) {
1214 if (dplane_ctx_intf_is_connected(ctx
)) {
1215 p
= dplane_ctx_get_intf_dest(ctx
);
1216 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1217 &p
->u
.prefix
, bytelen
))
1219 } else if (cmd
== RTM_NEWADDR
) {
1220 struct in_addr broad
= {
1221 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1224 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1230 /* p is now either address or destination/bcast addr */
1231 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1233 if (dplane_ctx_intf_is_secondary(ctx
))
1234 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1236 if (dplane_ctx_intf_has_label(ctx
)) {
1237 label
= dplane_ctx_get_intf_label(ctx
);
1238 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1243 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1246 enum netlink_msg_status
1247 netlink_put_address_update_msg(struct nl_batch
*bth
,
1248 struct zebra_dplane_ctx
*ctx
)
1250 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1254 static ssize_t
netlink_intf_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
1257 enum dplane_op_e op
;
1260 op
= dplane_ctx_get_op(ctx
);
1263 case DPLANE_OP_INTF_UPDATE
:
1266 case DPLANE_OP_INTF_INSTALL
:
1269 case DPLANE_OP_INTF_DELETE
:
1274 EC_ZEBRA_NHG_FIB_UPDATE
,
1275 "Context received for kernel interface update with incorrect OP code (%u)",
1280 return netlink_intf_msg_encode(cmd
, ctx
, buf
, buflen
);
1283 enum netlink_msg_status
1284 netlink_put_intf_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1286 return netlink_batch_add_msg(bth
, ctx
, netlink_intf_msg_encoder
, false);
1289 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1292 struct ifaddrmsg
*ifa
;
1293 struct rtattr
*tb
[IFA_MAX
+ 1];
1294 struct interface
*ifp
;
1299 struct zebra_ns
*zns
;
1300 uint32_t metric
= METRIC_MAX
;
1301 uint32_t kernel_flags
= 0;
1303 zns
= zebra_ns_lookup(ns_id
);
1304 ifa
= NLMSG_DATA(h
);
1306 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1308 EC_ZEBRA_UNKNOWN_FAMILY
,
1309 "Invalid address family: %u received from kernel interface addr change: %s",
1310 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1314 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1317 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1320 "%s: Message received from netlink is of a broken size: %d %zu",
1321 __func__
, h
->nlmsg_len
,
1322 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1326 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1328 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1331 /* During startup, failure to lookup the referenced
1332 * interface should not be an error, so we have
1333 * downgraded this condition to warning, and we permit
1334 * the startup interface state retrieval to continue.
1336 flog_warn(EC_LIB_INTERFACE
,
1337 "%s: can't find interface by index %d",
1338 __func__
, ifa
->ifa_index
);
1341 flog_err(EC_LIB_INTERFACE
,
1342 "%s: can't find interface by index %d",
1343 __func__
, ifa
->ifa_index
);
1348 /* Flags passed through */
1350 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1352 kernel_flags
= ifa
->ifa_flags
;
1354 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1357 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1358 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1361 zlog_debug(" IFA_LOCAL %s/%d",
1362 inet_ntop(ifa
->ifa_family
,
1363 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1365 ifa
->ifa_prefixlen
);
1366 if (tb
[IFA_ADDRESS
])
1367 zlog_debug(" IFA_ADDRESS %s/%d",
1368 inet_ntop(ifa
->ifa_family
,
1369 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1371 ifa
->ifa_prefixlen
);
1372 if (tb
[IFA_BROADCAST
])
1373 zlog_debug(" IFA_BROADCAST %s/%d",
1374 inet_ntop(ifa
->ifa_family
,
1375 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1377 ifa
->ifa_prefixlen
);
1378 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1379 zlog_debug(" IFA_LABEL %s",
1380 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1382 if (tb
[IFA_CACHEINFO
]) {
1383 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1384 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1385 ci
->ifa_prefered
, ci
->ifa_valid
);
1389 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1390 if (tb
[IFA_LOCAL
] == NULL
)
1391 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1392 if (tb
[IFA_ADDRESS
] == NULL
)
1393 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1395 /* local interface address */
1396 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1398 /* is there a peer address? */
1400 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1401 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1402 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1403 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1405 /* seeking a broadcast address */
1406 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1409 /* addr is primary key, SOL if we don't have one */
1411 zlog_debug("%s: Local Interface Address is NULL for %s",
1412 __func__
, ifp
->name
);
1417 if (kernel_flags
& IFA_F_SECONDARY
)
1418 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1422 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1424 if (label
&& strcmp(ifp
->name
, label
) == 0)
1427 if (tb
[IFA_RT_PRIORITY
])
1428 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1430 /* Register interface address to the interface. */
1431 if (ifa
->ifa_family
== AF_INET
) {
1432 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1434 "Invalid prefix length: %u received from kernel interface addr change: %s",
1436 nl_msg_type_to_str(h
->nlmsg_type
));
1440 if (h
->nlmsg_type
== RTM_NEWADDR
)
1441 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1443 (struct in_addr
*)broad
, label
,
1445 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1446 /* Delete with a peer address */
1447 connected_delete_ipv4(
1448 ifp
, flags
, (struct in_addr
*)addr
,
1449 ifa
->ifa_prefixlen
, broad
);
1451 connected_delete_ipv4(
1452 ifp
, flags
, (struct in_addr
*)addr
,
1453 ifa
->ifa_prefixlen
, NULL
);
1456 if (ifa
->ifa_family
== AF_INET6
) {
1457 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1459 "Invalid prefix length: %u received from kernel interface addr change: %s",
1461 nl_msg_type_to_str(h
->nlmsg_type
));
1464 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1465 /* Only consider valid addresses; we'll not get a
1467 * the kernel till IPv6 DAD has completed, but at init
1469 * does query for and will receive all addresses.
1472 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1473 connected_add_ipv6(ifp
, flags
,
1474 (struct in6_addr
*)addr
,
1475 (struct in6_addr
*)broad
,
1476 ifa
->ifa_prefixlen
, label
,
1479 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1480 NULL
, ifa
->ifa_prefixlen
);
1484 * Linux kernel does not send route delete on interface down/addr del
1485 * so we have to re-process routes it owns (i.e. kernel routes)
1487 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1488 rib_update(RIB_UPDATE_KERNEL
);
1494 * Parse and validate an incoming interface address change message,
1495 * generating a dplane context object.
1496 * This runs in the dplane pthread; the context is enqueued to the
1497 * main pthread for processing.
1499 int netlink_interface_addr_dplane(struct nlmsghdr
*h
, ns_id_t ns_id
,
1500 int startup
/*ignored*/)
1503 struct ifaddrmsg
*ifa
;
1504 struct rtattr
*tb
[IFA_MAX
+ 1];
1508 uint32_t metric
= METRIC_MAX
;
1509 uint32_t kernel_flags
= 0;
1510 struct zebra_dplane_ctx
*ctx
;
1513 ifa
= NLMSG_DATA(h
);
1515 /* Validate message types */
1516 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1519 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1520 if (IS_ZEBRA_DEBUG_KERNEL
)
1521 zlog_debug("%s: %s: Invalid address family: %u",
1522 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1527 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1529 if (IS_ZEBRA_DEBUG_KERNEL
)
1530 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1531 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1533 (size_t)NLMSG_LENGTH(
1534 sizeof(struct ifaddrmsg
)));
1538 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1540 /* Flags passed through */
1542 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1544 kernel_flags
= ifa
->ifa_flags
;
1546 if (IS_ZEBRA_DEBUG_KERNEL
) { /* remove this line to see initial ifcfg */
1547 char buf
[PREFIX_STRLEN
];
1549 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__
,
1550 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
,
1551 ifa
->ifa_index
, kernel_flags
);
1553 zlog_debug(" IFA_LOCAL %s/%d",
1554 inet_ntop(ifa
->ifa_family
,
1555 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1557 ifa
->ifa_prefixlen
);
1558 if (tb
[IFA_ADDRESS
])
1559 zlog_debug(" IFA_ADDRESS %s/%d",
1560 inet_ntop(ifa
->ifa_family
,
1561 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1563 ifa
->ifa_prefixlen
);
1564 if (tb
[IFA_BROADCAST
])
1565 zlog_debug(" IFA_BROADCAST %s/%d",
1566 inet_ntop(ifa
->ifa_family
,
1567 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1569 ifa
->ifa_prefixlen
);
1571 zlog_debug(" IFA_LABEL %s",
1572 (const char *)RTA_DATA(tb
[IFA_LABEL
]));
1574 if (tb
[IFA_CACHEINFO
]) {
1575 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1577 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1578 ci
->ifa_prefered
, ci
->ifa_valid
);
1582 /* Validate prefix length */
1584 if (ifa
->ifa_family
== AF_INET
1585 && ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1586 if (IS_ZEBRA_DEBUG_KERNEL
)
1587 zlog_debug("%s: %s: Invalid prefix length: %u",
1588 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1589 ifa
->ifa_prefixlen
);
1593 if (ifa
->ifa_family
== AF_INET6
) {
1594 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1595 if (IS_ZEBRA_DEBUG_KERNEL
)
1596 zlog_debug("%s: %s: Invalid prefix length: %u",
1598 nl_msg_type_to_str(h
->nlmsg_type
),
1599 ifa
->ifa_prefixlen
);
1603 /* Only consider valid addresses; we'll not get a kernel
1604 * notification till IPv6 DAD has completed, but at init
1605 * time, FRR does query for and will receive all addresses.
1607 if (h
->nlmsg_type
== RTM_NEWADDR
1608 && (kernel_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))) {
1609 if (IS_ZEBRA_DEBUG_KERNEL
)
1610 zlog_debug("%s: %s: Invalid/tentative addr",
1612 nl_msg_type_to_str(h
->nlmsg_type
));
1617 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1618 if (tb
[IFA_LOCAL
] == NULL
)
1619 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1620 if (tb
[IFA_ADDRESS
] == NULL
)
1621 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1623 /* local interface address */
1624 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1626 /* addr is primary key, SOL if we don't have one */
1628 if (IS_ZEBRA_DEBUG_KERNEL
)
1629 zlog_debug("%s: %s: No local interface address",
1630 __func__
, nl_msg_type_to_str(h
->nlmsg_type
));
1634 /* Allocate a context object, now that validation is done. */
1635 ctx
= dplane_ctx_alloc();
1636 if (h
->nlmsg_type
== RTM_NEWADDR
)
1637 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_ADD
);
1639 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_DEL
);
1641 dplane_ctx_set_ifindex(ctx
, ifa
->ifa_index
);
1642 dplane_ctx_set_ns_id(ctx
, ns_id
);
1644 /* Convert addr to prefix */
1645 memset(&p
, 0, sizeof(p
));
1646 p
.family
= ifa
->ifa_family
;
1647 p
.prefixlen
= ifa
->ifa_prefixlen
;
1648 if (p
.family
== AF_INET
)
1649 p
.u
.prefix4
= *(struct in_addr
*)addr
;
1651 p
.u
.prefix6
= *(struct in6_addr
*)addr
;
1653 dplane_ctx_set_intf_addr(ctx
, &p
);
1655 /* is there a peer address? */
1657 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1658 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1659 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1660 dplane_ctx_intf_set_connected(ctx
);
1661 } else if (tb
[IFA_BROADCAST
]) {
1662 /* seeking a broadcast address */
1663 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
1664 dplane_ctx_intf_set_broadcast(ctx
);
1669 /* Convert addr to prefix */
1670 memset(&p
, 0, sizeof(p
));
1671 p
.family
= ifa
->ifa_family
;
1672 p
.prefixlen
= ifa
->ifa_prefixlen
;
1673 if (p
.family
== AF_INET
)
1674 p
.u
.prefix4
= *(struct in_addr
*)broad
;
1676 p
.u
.prefix6
= *(struct in6_addr
*)broad
;
1678 dplane_ctx_set_intf_dest(ctx
, &p
);
1682 if (kernel_flags
& IFA_F_SECONDARY
)
1683 dplane_ctx_intf_set_secondary(ctx
);
1686 if (tb
[IFA_LABEL
]) {
1687 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1688 dplane_ctx_set_intf_label(ctx
, label
);
1691 if (tb
[IFA_RT_PRIORITY
])
1692 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1694 dplane_ctx_set_intf_metric(ctx
, metric
);
1696 /* Enqueue ctx for main pthread to process */
1697 dplane_provider_enqueue_to_zebra(ctx
);
1702 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1705 struct ifinfomsg
*ifi
;
1706 struct rtattr
*tb
[IFLA_MAX
+ 1];
1707 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1708 struct interface
*ifp
;
1712 char *slave_kind
= NULL
;
1713 struct zebra_ns
*zns
;
1714 vrf_id_t vrf_id
= VRF_DEFAULT
;
1715 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1716 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1717 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1718 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1719 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1720 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1721 struct zebra_if
*zif
;
1722 ns_id_t link_nsid
= ns_id
;
1723 ifindex_t master_infindex
= IFINDEX_INTERNAL
;
1726 zns
= zebra_ns_lookup(ns_id
);
1727 ifi
= NLMSG_DATA(h
);
1729 /* assume if not default zns, then new VRF */
1730 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1731 /* If this is not link add/delete message so print warning. */
1732 zlog_debug("netlink_link_change: wrong kernel message %s",
1733 nl_msg_type_to_str(h
->nlmsg_type
));
1737 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1738 || ifi
->ifi_family
== AF_INET6
)) {
1740 EC_ZEBRA_UNKNOWN_FAMILY
,
1741 "Invalid address family: %u received from kernel link change: %s",
1742 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1746 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1749 "%s: Message received from netlink is of a broken size %d %zu",
1750 __func__
, h
->nlmsg_len
,
1751 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1755 /* We are interested in some AF_BRIDGE notifications. */
1756 if (ifi
->ifi_family
== AF_BRIDGE
)
1757 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1759 /* Looking up interface name. */
1760 memset(linkinfo
, 0, sizeof(linkinfo
));
1761 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1763 /* check for wireless messages to ignore */
1764 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1765 if (IS_ZEBRA_DEBUG_KERNEL
)
1766 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1771 if (tb
[IFLA_IFNAME
] == NULL
)
1773 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1775 /* Must be valid string. */
1776 len
= RTA_PAYLOAD(tb
[IFLA_IFNAME
]);
1777 if (len
< 2 || name
[len
- 1] != '\0') {
1778 if (IS_ZEBRA_DEBUG_KERNEL
)
1779 zlog_debug("%s: invalid intf name", __func__
);
1783 if (tb
[IFLA_LINKINFO
]) {
1784 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1787 if (linkinfo
[IFLA_INFO_KIND
])
1788 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1790 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1791 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1793 netlink_determine_zebra_iftype(kind
, &zif_type
);
1796 /* If linking to another interface, note it. */
1798 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1800 if (tb
[IFLA_LINK_NETNSID
]) {
1801 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1802 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1804 if (tb
[IFLA_IFALIAS
]) {
1805 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1808 /* See if interface is present. */
1809 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1811 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1812 /* If VRF, create or update the VRF structure itself. */
1813 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1814 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1815 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1818 if (tb
[IFLA_MASTER
]) {
1819 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1820 && !vrf_is_backend_netns()) {
1821 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1822 master_infindex
= vrf_id
=
1823 *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1824 } else if (slave_kind
1825 && (strcmp(slave_kind
, "bridge") == 0)) {
1826 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1827 master_infindex
= bridge_ifindex
=
1828 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1829 } else if (slave_kind
1830 && (strcmp(slave_kind
, "bond") == 0)) {
1831 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1832 master_infindex
= bond_ifindex
=
1833 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1834 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1836 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1838 if (vrf_is_backend_netns())
1839 vrf_id
= (vrf_id_t
)ns_id
;
1841 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1842 /* Add interface notification from kernel */
1843 if (IS_ZEBRA_DEBUG_KERNEL
)
1845 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1846 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1847 zif_slave_type
, master_infindex
,
1851 /* unknown interface */
1852 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1854 /* pre-configured interface, learnt now */
1855 if (ifp
->vrf
->vrf_id
!= vrf_id
)
1856 if_update_to_new_vrf(ifp
, vrf_id
);
1859 /* Update interface information. */
1860 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1861 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1862 if (!tb
[IFLA_MTU
]) {
1864 "RTM_NEWLINK for interface %s(%u) without MTU set",
1865 name
, ifi
->ifi_index
);
1868 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1870 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1872 /* Set interface type */
1873 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1874 if (IS_ZEBRA_IF_VRF(ifp
))
1875 SET_FLAG(ifp
->status
,
1876 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1879 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1882 netlink_to_zebra_link_type(ifi
->ifi_type
);
1883 netlink_interface_update_hw_addr(tb
, ifp
);
1885 /* Inform clients, install any configured addresses. */
1888 /* Extract and save L2 interface information, take
1889 * additional actions. */
1890 netlink_interface_update_l2info(
1891 ifp
, linkinfo
[IFLA_INFO_DATA
],
1893 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1894 zebra_l2if_update_bridge_slave(
1895 ifp
, bridge_ifindex
, ns_id
,
1896 ZEBRA_BRIDGE_NO_ACTION
);
1897 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1898 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
1901 if (tb
[IFLA_PROTO_DOWN
]) {
1904 protodown
= *(uint8_t *)RTA_DATA(
1905 tb
[IFLA_PROTO_DOWN
]);
1906 netlink_proc_dplane_if_protodown(ifp
->info
,
1909 } else if (ifp
->vrf
->vrf_id
!= vrf_id
) {
1910 /* VRF change for an interface. */
1911 if (IS_ZEBRA_DEBUG_KERNEL
)
1913 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1914 name
, ifp
->ifindex
, ifp
->vrf
->vrf_id
,
1915 vrf_id
, ifi
->ifi_flags
);
1917 if_handle_vrf_change(ifp
, vrf_id
);
1919 bool was_bridge_slave
, was_bond_slave
;
1920 uint8_t chgflags
= ZEBRA_BRIDGE_NO_ACTION
;
1923 /* Interface update. */
1924 if (IS_ZEBRA_DEBUG_KERNEL
)
1926 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1927 name
, ifp
->ifindex
, zif_slave_type
,
1928 master_infindex
, ifi
->ifi_flags
);
1930 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1931 if (!tb
[IFLA_MTU
]) {
1933 "RTM_NEWLINK for interface %s(%u) without MTU set",
1934 name
, ifi
->ifi_index
);
1937 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1940 /* Update interface type - NOTE: Only slave_type can
1942 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1943 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
1944 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1946 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
1949 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1952 netlink_to_zebra_link_type(ifi
->ifi_type
);
1953 netlink_interface_update_hw_addr(tb
, ifp
);
1955 if (tb
[IFLA_PROTO_DOWN
]) {
1958 protodown
= *(uint8_t *)RTA_DATA(
1959 tb
[IFLA_PROTO_DOWN
]);
1960 netlink_proc_dplane_if_protodown(zif
,
1964 if (if_is_no_ptm_operative(ifp
)) {
1965 bool is_up
= if_is_operative(ifp
);
1966 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1967 if (!if_is_no_ptm_operative(ifp
) ||
1968 CHECK_FLAG(zif
->flags
,
1969 ZIF_FLAG_PROTODOWN
)) {
1970 if (IS_ZEBRA_DEBUG_KERNEL
)
1972 "Intf %s(%u) has gone DOWN",
1973 name
, ifp
->ifindex
);
1975 rib_update(RIB_UPDATE_KERNEL
);
1976 } else if (if_is_operative(ifp
)) {
1977 bool mac_updated
= false;
1979 /* Must notify client daemons of new
1980 * interface status. */
1981 if (IS_ZEBRA_DEBUG_KERNEL
)
1983 "Intf %s(%u) PTM up, notifying clients",
1984 name
, ifp
->ifindex
);
1987 /* Update EVPN VNI when SVI MAC change
1989 if (memcmp(old_hw_addr
, ifp
->hw_addr
,
1990 INTERFACE_HWADDR_MAX
))
1992 if (IS_ZEBRA_IF_VLAN(ifp
)
1994 struct interface
*link_if
;
1997 if_lookup_by_index_per_ns(
1998 zebra_ns_lookup(NS_DEFAULT
),
2001 zebra_vxlan_svi_up(ifp
,
2003 } else if (mac_updated
2004 && IS_ZEBRA_IF_BRIDGE(ifp
)) {
2006 "Intf %s(%u) bridge changed MAC address",
2007 name
, ifp
->ifindex
);
2009 ZEBRA_BRIDGE_MASTER_MAC_CHANGE
;
2013 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2014 if (if_is_operative(ifp
) &&
2015 !CHECK_FLAG(zif
->flags
,
2016 ZIF_FLAG_PROTODOWN
)) {
2017 if (IS_ZEBRA_DEBUG_KERNEL
)
2019 "Intf %s(%u) has come UP",
2020 name
, ifp
->ifindex
);
2022 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2024 ZEBRA_BRIDGE_MASTER_UP
;
2026 if (IS_ZEBRA_DEBUG_KERNEL
)
2028 "Intf %s(%u) has gone DOWN",
2029 name
, ifp
->ifindex
);
2031 rib_update(RIB_UPDATE_KERNEL
);
2035 /* Extract and save L2 interface information, take
2036 * additional actions. */
2037 netlink_interface_update_l2info(
2038 ifp
, linkinfo
[IFLA_INFO_DATA
],
2040 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2041 zebra_l2if_update_bridge(ifp
, chgflags
);
2042 if (IS_ZEBRA_IF_BOND(ifp
))
2043 zebra_l2if_update_bond(ifp
, true);
2044 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
2045 zebra_l2if_update_bridge_slave(
2046 ifp
, bridge_ifindex
, ns_id
, chgflags
);
2047 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
2048 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2054 XFREE(MTYPE_TMP
, zif
->desc
);
2056 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
2059 /* Delete interface notification from kernel */
2061 if (IS_ZEBRA_DEBUG_KERNEL
)
2063 "RTM_DELLINK for unknown interface %s(%u)",
2064 name
, ifi
->ifi_index
);
2068 if (IS_ZEBRA_DEBUG_KERNEL
)
2069 zlog_debug("RTM_DELLINK for %s(%u)", name
,
2072 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
2074 if (IS_ZEBRA_IF_BOND(ifp
))
2075 zebra_l2if_update_bond(ifp
, false);
2076 if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2077 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, false);
2078 /* Special handling for bridge or VxLAN interfaces. */
2079 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2080 zebra_l2_bridge_del(ifp
);
2081 else if (IS_ZEBRA_IF_VXLAN(ifp
))
2082 zebra_l2_vxlanif_del(ifp
);
2084 if_delete_update(ifp
);
2086 /* If VRF, delete the VRF structure itself. */
2087 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns())
2088 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2095 * Interface encoding helper function.
2097 * \param[in] cmd netlink command.
2098 * \param[in] ctx dataplane context (information snapshot).
2099 * \param[out] buf buffer to hold the packet.
2100 * \param[in] buflen amount of buffer bytes.
2103 ssize_t
netlink_intf_msg_encode(uint16_t cmd
,
2104 const struct zebra_dplane_ctx
*ctx
, void *buf
,
2109 struct ifinfomsg ifa
;
2113 struct rtattr
*nest_protodown_reason
;
2114 ifindex_t ifindex
= dplane_ctx_get_ifindex(ctx
);
2115 uint32_t r_bitfield
= dplane_ctx_get_intf_r_bitfield(ctx
);
2116 bool down
= dplane_ctx_intf_is_protodown(ctx
);
2118 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2120 if (buflen
< sizeof(*req
))
2123 memset(req
, 0, sizeof(*req
));
2125 if (cmd
!= RTM_SETLINK
)
2127 EC_ZEBRA_INTF_UPDATE_FAILURE
,
2128 "Only RTM_SETLINK message type currently supported in dplane pthread");
2130 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2131 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2132 req
->n
.nlmsg_type
= cmd
;
2133 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2135 req
->ifa
.ifi_index
= ifindex
;
2137 nl_attr_put8(&req
->n
, buflen
, IFLA_PROTO_DOWN
, down
);
2138 nl_attr_put32(&req
->n
, buflen
, IFLA_LINK
, ifindex
);
2141 nest_protodown_reason
=
2142 nl_attr_nest(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON
);
2144 if (!nest_protodown_reason
)
2147 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_MASK
,
2148 (1 << frr_protodown_r_bit
));
2149 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_VALUE
,
2150 ((int)down
) << frr_protodown_r_bit
);
2152 nl_attr_nest_end(&req
->n
, nest_protodown_reason
);
2155 if (IS_ZEBRA_DEBUG_KERNEL
)
2156 zlog_debug("%s: %s, protodown=%d ifindex=%u", __func__
,
2157 nl_msg_type_to_str(cmd
), down
, ifindex
);
2159 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2162 int netlink_protodown(struct interface
*ifp
, bool down
, uint32_t r_bitfield
)
2164 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
2165 struct rtattr
*nest_protodown_reason
;
2169 struct ifinfomsg ifa
;
2170 char buf
[NL_PKT_BUF_SIZE
];
2173 memset(&req
, 0, sizeof(req
));
2175 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2176 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2177 req
.n
.nlmsg_type
= RTM_SETLINK
;
2178 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
2180 req
.ifa
.ifi_index
= ifp
->ifindex
;
2182 nl_attr_put8(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN
, down
);
2183 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, ifp
->ifindex
);
2186 nest_protodown_reason
= nl_attr_nest(&req
.n
, sizeof(req
),
2187 IFLA_PROTO_DOWN_REASON
);
2189 if (!nest_protodown_reason
)
2192 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN_REASON_MASK
,
2193 (1 << frr_protodown_r_bit
));
2194 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN_REASON_VALUE
,
2195 ((int)down
) << frr_protodown_r_bit
);
2197 nl_attr_nest_end(&req
.n
, nest_protodown_reason
);
2200 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
2204 /* Interface information read by netlink. */
2205 void interface_list(struct zebra_ns
*zns
)
2207 interface_lookup_netlink(zns
);
2208 /* We add routes for interface address,
2209 * so we need to get the nexthop info
2210 * from the kernel before we can do that
2212 netlink_nexthop_read(zns
);
2214 interface_addr_lookup_netlink(zns
);
2217 void if_netlink_set_frr_protodown_r_bit(uint8_t bit
)
2219 if (IS_ZEBRA_DEBUG_KERNEL
)
2220 zlog_debug("FRR protodown reason bit change %u -> %u",
2221 frr_protodown_r_bit
, bit
);
2223 frr_protodown_r_bit
= bit
;
2226 void if_netlink_unset_frr_protodown_r_bit(void)
2228 if (IS_ZEBRA_DEBUG_KERNEL
)
2229 zlog_debug("FRR protodown reason bit change %u -> %u",
2230 frr_protodown_r_bit
,
2231 FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2233 frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
2237 bool if_netlink_frr_protodown_r_bit_is_set(void)
2239 return (frr_protodown_r_bit
!= FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2242 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2244 return frr_protodown_r_bit
;
2247 #endif /* GNU_LINUX */