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 static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield
)
820 /* This shouldn't be possible */
821 assert(frr_protodown_r_bit
< 32);
822 return (rc_bitfield
== (((uint32_t)1) << frr_protodown_r_bit
));
826 * Process interface protodown dplane update.
828 * If the interface is an es bond member then it must follow EVPN's
831 static void netlink_proc_dplane_if_protodown(struct zebra_if
*zif
,
836 uint32_t rc_bitfield
= 0;
837 struct rtattr
*pd_reason_info
[IFLA_MAX
+ 1];
839 protodown
= !!*(uint8_t *)RTA_DATA(tb
[IFLA_PROTO_DOWN
]);
841 if (tb
[IFLA_PROTO_DOWN_REASON
]) {
842 netlink_parse_rtattr_nested(pd_reason_info
, IFLA_INFO_MAX
,
843 tb
[IFLA_PROTO_DOWN_REASON
]);
845 if (pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
])
846 rc_bitfield
= *(uint32_t *)RTA_DATA(
847 pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
]);
851 * Set our reason code to note it wasn't us.
852 * If the reason we got from the kernel is ONLY frr though, don't
855 COND_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_EXTERNAL
,
856 protodown
&& rc_bitfield
&&
857 !is_if_protodown_reason_only_frr(rc_bitfield
));
860 old_protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
861 if (protodown
== old_protodown
)
864 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
865 zlog_debug("interface %s dplane change, protdown %s",
866 zif
->ifp
->name
, protodown
? "on" : "off");
868 /* Set protodown, respectively */
869 COND_FLAG(zif
->flags
, ZIF_FLAG_PROTODOWN
, protodown
);
871 if (zebra_evpn_is_es_bond_member(zif
->ifp
)) {
872 /* Check it's not already being sent to the dplane first */
874 CHECK_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
)) {
875 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
877 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
883 CHECK_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
)) {
884 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
886 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
891 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
893 "bond mbr %s reinstate protodown %s in the dplane",
894 zif
->ifp
->name
, old_protodown
? "on" : "off");
897 SET_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
);
899 SET_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
);
901 dplane_intf_update(zif
->ifp
);
905 static uint8_t netlink_parse_lacp_bypass(struct rtattr
**linkinfo
)
908 struct rtattr
*mbrinfo
[IFLA_BOND_SLAVE_MAX
+ 1];
910 netlink_parse_rtattr_nested(mbrinfo
, IFLA_BOND_SLAVE_MAX
,
911 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
912 if (mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
])
913 bypass
= *(uint8_t *)RTA_DATA(
914 mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
]);
920 * Only called at startup to cleanup leftover protodown reasons we may
921 * have not cleaned up. We leave protodown set though.
923 static void if_sweep_protodown(struct zebra_if
*zif
)
927 protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
932 if (IS_ZEBRA_DEBUG_KERNEL
)
933 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
934 zif
->ifp
->name
, protodown
? "on" : "off",
937 /* Only clear our reason codes, leave external if it was set */
938 UNSET_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_ALL
);
939 dplane_intf_update(zif
->ifp
);
943 * Called from interface_lookup_netlink(). This function is only used
946 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
949 struct ifinfomsg
*ifi
;
950 struct rtattr
*tb
[IFLA_MAX
+ 1];
951 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
952 struct interface
*ifp
;
956 char *slave_kind
= NULL
;
957 struct zebra_ns
*zns
= NULL
;
958 vrf_id_t vrf_id
= VRF_DEFAULT
;
959 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
960 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
961 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
962 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
963 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
964 struct zebra_if
*zif
;
965 ns_id_t link_nsid
= ns_id
;
968 zns
= zebra_ns_lookup(ns_id
);
971 if (h
->nlmsg_type
!= RTM_NEWLINK
)
974 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
977 "%s: Message received from netlink is of a broken size: %d %zu",
978 __func__
, h
->nlmsg_len
,
979 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
983 /* We are interested in some AF_BRIDGE notifications. */
984 if (ifi
->ifi_family
== AF_BRIDGE
)
985 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
987 /* Looking up interface name. */
988 memset(linkinfo
, 0, sizeof(linkinfo
));
989 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
992 /* check for wireless messages to ignore */
993 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
994 if (IS_ZEBRA_DEBUG_KERNEL
)
995 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1000 if (tb
[IFLA_IFNAME
] == NULL
)
1002 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1004 if (tb
[IFLA_IFALIAS
])
1005 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1007 if (tb
[IFLA_LINKINFO
]) {
1008 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1011 if (linkinfo
[IFLA_INFO_KIND
])
1012 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1014 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1015 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1017 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
1018 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
1020 netlink_determine_zebra_iftype(kind
, &zif_type
);
1023 /* If VRF, create the VRF structure itself. */
1024 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1025 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1026 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1029 if (tb
[IFLA_MASTER
]) {
1030 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1031 && !vrf_is_backend_netns()) {
1032 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1033 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1034 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
1035 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1037 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1038 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
1039 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1040 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1041 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1043 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1045 if (vrf_is_backend_netns())
1046 vrf_id
= (vrf_id_t
)ns_id
;
1048 /* If linking to another interface, note it. */
1050 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1052 if (tb
[IFLA_LINK_NETNSID
]) {
1053 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1054 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1057 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1058 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
1060 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1061 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
1063 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
1064 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1066 /* Set zebra interface type */
1067 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1068 if (IS_ZEBRA_IF_VRF(ifp
))
1069 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1072 * Just set the @link/lower-device ifindex. During nldump interfaces are
1073 * not ordered in any fashion so we may end up getting upper devices
1074 * before lower devices. We will setup the real linkage once the dump
1077 zif
= (struct zebra_if
*)ifp
->info
;
1078 zif
->link_ifindex
= link_ifindex
;
1081 XFREE(MTYPE_TMP
, zif
->desc
);
1082 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1085 /* Hardware type and address. */
1086 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
1088 netlink_interface_update_hw_addr(tb
, ifp
);
1092 /* Extract and save L2 interface information, take additional actions.
1094 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
1096 if (IS_ZEBRA_IF_BOND(ifp
))
1097 zebra_l2if_update_bond(ifp
, true);
1098 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1099 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
,
1100 ZEBRA_BRIDGE_NO_ACTION
);
1101 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1102 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, !!bypass
);
1104 if (tb
[IFLA_PROTO_DOWN
]) {
1105 netlink_proc_dplane_if_protodown(zif
, tb
);
1106 if_sweep_protodown(zif
);
1112 /* Request for specific interface or address information from the kernel */
1113 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
1114 int type
, uint32_t filter_mask
)
1118 struct ifinfomsg ifm
;
1122 /* Form the request, specifying filter (rtattr) if needed. */
1123 memset(&req
, 0, sizeof(req
));
1124 req
.n
.nlmsg_type
= type
;
1125 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1126 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1127 req
.ifm
.ifi_family
= family
;
1129 /* Include filter, if specified. */
1131 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
1133 return netlink_request(netlink_cmd
, &req
);
1136 enum netlink_msg_status
1137 netlink_put_gre_set_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1139 enum dplane_op_e op
;
1140 enum netlink_msg_status ret
;
1142 op
= dplane_ctx_get_op(ctx
);
1143 assert(op
== DPLANE_OP_GRE_SET
);
1145 ret
= netlink_batch_add_msg(bth
, ctx
, netlink_gre_set_msg_encoder
, false);
1150 /* Interface lookup by netlink socket. */
1151 int interface_lookup_netlink(struct zebra_ns
*zns
)
1154 struct zebra_dplane_info dp_info
;
1155 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1157 /* Capture key info from ns struct */
1158 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1160 /* Get interface information. */
1161 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
1164 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1169 /* Get interface information - for bridge interfaces. */
1170 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
1171 RTEXT_FILTER_BRVLAN
);
1174 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1179 /* fixup linkages */
1180 zebra_if_update_all_links(zns
);
1185 * interface_addr_lookup_netlink() - Look up interface addresses
1187 * @zns: Zebra netlink socket
1188 * Return: Result status
1190 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
1193 struct zebra_dplane_info dp_info
;
1194 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1196 /* Capture key info from ns struct */
1197 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1199 /* Get IPv4 address of the interfaces. */
1200 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
1203 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1208 /* Get IPv6 address of the interfaces. */
1209 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
1212 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1220 int kernel_interface_set_master(struct interface
*master
,
1221 struct interface
*slave
)
1223 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1227 struct ifinfomsg ifa
;
1228 char buf
[NL_PKT_BUF_SIZE
];
1231 memset(&req
, 0, sizeof(req
));
1233 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1234 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1235 req
.n
.nlmsg_type
= RTM_SETLINK
;
1236 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1238 req
.ifa
.ifi_index
= slave
->ifindex
;
1240 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
1241 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
1243 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1247 /* Interface address modification. */
1248 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
1249 void *buf
, size_t buflen
)
1252 const struct prefix
*p
;
1258 struct ifaddrmsg ifa
;
1262 if (buflen
< sizeof(*req
))
1265 p
= dplane_ctx_get_intf_addr(ctx
);
1266 memset(req
, 0, sizeof(*req
));
1268 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1270 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1271 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1273 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1278 req
->n
.nlmsg_type
= cmd
;
1279 req
->ifa
.ifa_family
= p
->family
;
1281 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1283 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1286 if (p
->family
== AF_INET
) {
1287 if (dplane_ctx_intf_is_connected(ctx
)) {
1288 p
= dplane_ctx_get_intf_dest(ctx
);
1289 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1290 &p
->u
.prefix
, bytelen
))
1292 } else if (cmd
== RTM_NEWADDR
) {
1293 struct in_addr broad
= {
1294 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1297 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1303 /* p is now either address or destination/bcast addr */
1304 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1306 if (dplane_ctx_intf_is_secondary(ctx
))
1307 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1309 if (dplane_ctx_intf_has_label(ctx
)) {
1310 label
= dplane_ctx_get_intf_label(ctx
);
1311 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1316 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1319 enum netlink_msg_status
1320 netlink_put_address_update_msg(struct nl_batch
*bth
,
1321 struct zebra_dplane_ctx
*ctx
)
1323 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1327 static ssize_t
netlink_intf_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
1330 enum dplane_op_e op
;
1333 op
= dplane_ctx_get_op(ctx
);
1336 case DPLANE_OP_INTF_UPDATE
:
1339 case DPLANE_OP_INTF_INSTALL
:
1342 case DPLANE_OP_INTF_DELETE
:
1347 EC_ZEBRA_NHG_FIB_UPDATE
,
1348 "Context received for kernel interface update with incorrect OP code (%u)",
1353 return netlink_intf_msg_encode(cmd
, ctx
, buf
, buflen
);
1356 enum netlink_msg_status
1357 netlink_put_intf_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1359 return netlink_batch_add_msg(bth
, ctx
, netlink_intf_msg_encoder
, false);
1362 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1365 struct ifaddrmsg
*ifa
;
1366 struct rtattr
*tb
[IFA_MAX
+ 1];
1367 struct interface
*ifp
;
1372 struct zebra_ns
*zns
;
1373 uint32_t metric
= METRIC_MAX
;
1374 uint32_t kernel_flags
= 0;
1376 zns
= zebra_ns_lookup(ns_id
);
1377 ifa
= NLMSG_DATA(h
);
1379 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1381 EC_ZEBRA_UNKNOWN_FAMILY
,
1382 "Invalid address family: %u received from kernel interface addr change: %s",
1383 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1387 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1390 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1393 "%s: Message received from netlink is of a broken size: %d %zu",
1394 __func__
, h
->nlmsg_len
,
1395 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1399 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1401 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1404 /* During startup, failure to lookup the referenced
1405 * interface should not be an error, so we have
1406 * downgraded this condition to warning, and we permit
1407 * the startup interface state retrieval to continue.
1409 flog_warn(EC_LIB_INTERFACE
,
1410 "%s: can't find interface by index %d",
1411 __func__
, ifa
->ifa_index
);
1414 flog_err(EC_LIB_INTERFACE
,
1415 "%s: can't find interface by index %d",
1416 __func__
, ifa
->ifa_index
);
1421 /* Flags passed through */
1423 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1425 kernel_flags
= ifa
->ifa_flags
;
1427 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1430 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1431 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1434 zlog_debug(" IFA_LOCAL %s/%d",
1435 inet_ntop(ifa
->ifa_family
,
1436 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1438 ifa
->ifa_prefixlen
);
1439 if (tb
[IFA_ADDRESS
])
1440 zlog_debug(" IFA_ADDRESS %s/%d",
1441 inet_ntop(ifa
->ifa_family
,
1442 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1444 ifa
->ifa_prefixlen
);
1445 if (tb
[IFA_BROADCAST
])
1446 zlog_debug(" IFA_BROADCAST %s/%d",
1447 inet_ntop(ifa
->ifa_family
,
1448 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1450 ifa
->ifa_prefixlen
);
1451 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1452 zlog_debug(" IFA_LABEL %s",
1453 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1455 if (tb
[IFA_CACHEINFO
]) {
1456 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1457 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1458 ci
->ifa_prefered
, ci
->ifa_valid
);
1462 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1463 if (tb
[IFA_LOCAL
] == NULL
)
1464 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1465 if (tb
[IFA_ADDRESS
] == NULL
)
1466 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1468 /* local interface address */
1469 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1471 /* is there a peer address? */
1473 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1474 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1475 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1476 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1478 /* seeking a broadcast address */
1479 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1482 /* addr is primary key, SOL if we don't have one */
1484 zlog_debug("%s: Local Interface Address is NULL for %s",
1485 __func__
, ifp
->name
);
1490 if (kernel_flags
& IFA_F_SECONDARY
)
1491 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1495 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1497 if (label
&& strcmp(ifp
->name
, label
) == 0)
1500 if (tb
[IFA_RT_PRIORITY
])
1501 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1503 /* Register interface address to the interface. */
1504 if (ifa
->ifa_family
== AF_INET
) {
1505 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1507 "Invalid prefix length: %u received from kernel interface addr change: %s",
1509 nl_msg_type_to_str(h
->nlmsg_type
));
1513 if (h
->nlmsg_type
== RTM_NEWADDR
)
1514 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1516 (struct in_addr
*)broad
, label
,
1518 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1519 /* Delete with a peer address */
1520 connected_delete_ipv4(
1521 ifp
, flags
, (struct in_addr
*)addr
,
1522 ifa
->ifa_prefixlen
, broad
);
1524 connected_delete_ipv4(
1525 ifp
, flags
, (struct in_addr
*)addr
,
1526 ifa
->ifa_prefixlen
, NULL
);
1529 if (ifa
->ifa_family
== AF_INET6
) {
1530 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1532 "Invalid prefix length: %u received from kernel interface addr change: %s",
1534 nl_msg_type_to_str(h
->nlmsg_type
));
1537 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1538 /* Only consider valid addresses; we'll not get a
1540 * the kernel till IPv6 DAD has completed, but at init
1542 * does query for and will receive all addresses.
1545 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1546 connected_add_ipv6(ifp
, flags
,
1547 (struct in6_addr
*)addr
,
1548 (struct in6_addr
*)broad
,
1549 ifa
->ifa_prefixlen
, label
,
1552 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1553 NULL
, ifa
->ifa_prefixlen
);
1557 * Linux kernel does not send route delete on interface down/addr del
1558 * so we have to re-process routes it owns (i.e. kernel routes)
1560 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1561 rib_update(RIB_UPDATE_KERNEL
);
1567 * Parse and validate an incoming interface address change message,
1568 * generating a dplane context object.
1569 * This runs in the dplane pthread; the context is enqueued to the
1570 * main pthread for processing.
1572 int netlink_interface_addr_dplane(struct nlmsghdr
*h
, ns_id_t ns_id
,
1573 int startup
/*ignored*/)
1576 struct ifaddrmsg
*ifa
;
1577 struct rtattr
*tb
[IFA_MAX
+ 1];
1581 uint32_t metric
= METRIC_MAX
;
1582 uint32_t kernel_flags
= 0;
1583 struct zebra_dplane_ctx
*ctx
;
1586 ifa
= NLMSG_DATA(h
);
1588 /* Validate message types */
1589 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1592 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1593 if (IS_ZEBRA_DEBUG_KERNEL
)
1594 zlog_debug("%s: %s: Invalid address family: %u",
1595 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1600 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1602 if (IS_ZEBRA_DEBUG_KERNEL
)
1603 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1604 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1606 (size_t)NLMSG_LENGTH(
1607 sizeof(struct ifaddrmsg
)));
1611 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1613 /* Flags passed through */
1615 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1617 kernel_flags
= ifa
->ifa_flags
;
1619 if (IS_ZEBRA_DEBUG_KERNEL
) { /* remove this line to see initial ifcfg */
1620 char buf
[PREFIX_STRLEN
];
1622 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__
,
1623 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
,
1624 ifa
->ifa_index
, kernel_flags
);
1626 zlog_debug(" IFA_LOCAL %s/%d",
1627 inet_ntop(ifa
->ifa_family
,
1628 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1630 ifa
->ifa_prefixlen
);
1631 if (tb
[IFA_ADDRESS
])
1632 zlog_debug(" IFA_ADDRESS %s/%d",
1633 inet_ntop(ifa
->ifa_family
,
1634 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1636 ifa
->ifa_prefixlen
);
1637 if (tb
[IFA_BROADCAST
])
1638 zlog_debug(" IFA_BROADCAST %s/%d",
1639 inet_ntop(ifa
->ifa_family
,
1640 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1642 ifa
->ifa_prefixlen
);
1644 zlog_debug(" IFA_LABEL %s",
1645 (const char *)RTA_DATA(tb
[IFA_LABEL
]));
1647 if (tb
[IFA_CACHEINFO
]) {
1648 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1650 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1651 ci
->ifa_prefered
, ci
->ifa_valid
);
1655 /* Validate prefix length */
1657 if (ifa
->ifa_family
== AF_INET
1658 && ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1659 if (IS_ZEBRA_DEBUG_KERNEL
)
1660 zlog_debug("%s: %s: Invalid prefix length: %u",
1661 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1662 ifa
->ifa_prefixlen
);
1666 if (ifa
->ifa_family
== AF_INET6
) {
1667 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1668 if (IS_ZEBRA_DEBUG_KERNEL
)
1669 zlog_debug("%s: %s: Invalid prefix length: %u",
1671 nl_msg_type_to_str(h
->nlmsg_type
),
1672 ifa
->ifa_prefixlen
);
1676 /* Only consider valid addresses; we'll not get a kernel
1677 * notification till IPv6 DAD has completed, but at init
1678 * time, FRR does query for and will receive all addresses.
1680 if (h
->nlmsg_type
== RTM_NEWADDR
1681 && (kernel_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))) {
1682 if (IS_ZEBRA_DEBUG_KERNEL
)
1683 zlog_debug("%s: %s: Invalid/tentative addr",
1685 nl_msg_type_to_str(h
->nlmsg_type
));
1690 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1691 if (tb
[IFA_LOCAL
] == NULL
)
1692 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1693 if (tb
[IFA_ADDRESS
] == NULL
)
1694 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1696 /* local interface address */
1697 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1699 /* addr is primary key, SOL if we don't have one */
1701 if (IS_ZEBRA_DEBUG_KERNEL
)
1702 zlog_debug("%s: %s: No local interface address",
1703 __func__
, nl_msg_type_to_str(h
->nlmsg_type
));
1707 /* Allocate a context object, now that validation is done. */
1708 ctx
= dplane_ctx_alloc();
1709 if (h
->nlmsg_type
== RTM_NEWADDR
)
1710 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_ADD
);
1712 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_DEL
);
1714 dplane_ctx_set_ifindex(ctx
, ifa
->ifa_index
);
1715 dplane_ctx_set_ns_id(ctx
, ns_id
);
1717 /* Convert addr to prefix */
1718 memset(&p
, 0, sizeof(p
));
1719 p
.family
= ifa
->ifa_family
;
1720 p
.prefixlen
= ifa
->ifa_prefixlen
;
1721 if (p
.family
== AF_INET
)
1722 p
.u
.prefix4
= *(struct in_addr
*)addr
;
1724 p
.u
.prefix6
= *(struct in6_addr
*)addr
;
1726 dplane_ctx_set_intf_addr(ctx
, &p
);
1728 /* is there a peer address? */
1730 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1731 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1732 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1733 dplane_ctx_intf_set_connected(ctx
);
1734 } else if (tb
[IFA_BROADCAST
]) {
1735 /* seeking a broadcast address */
1736 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
1737 dplane_ctx_intf_set_broadcast(ctx
);
1742 /* Convert addr to prefix */
1743 memset(&p
, 0, sizeof(p
));
1744 p
.family
= ifa
->ifa_family
;
1745 p
.prefixlen
= ifa
->ifa_prefixlen
;
1746 if (p
.family
== AF_INET
)
1747 p
.u
.prefix4
= *(struct in_addr
*)broad
;
1749 p
.u
.prefix6
= *(struct in6_addr
*)broad
;
1751 dplane_ctx_set_intf_dest(ctx
, &p
);
1755 if (kernel_flags
& IFA_F_SECONDARY
)
1756 dplane_ctx_intf_set_secondary(ctx
);
1759 if (tb
[IFA_LABEL
]) {
1760 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1761 dplane_ctx_set_intf_label(ctx
, label
);
1764 if (tb
[IFA_RT_PRIORITY
])
1765 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1767 dplane_ctx_set_intf_metric(ctx
, metric
);
1769 /* Enqueue ctx for main pthread to process */
1770 dplane_provider_enqueue_to_zebra(ctx
);
1775 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1778 struct ifinfomsg
*ifi
;
1779 struct rtattr
*tb
[IFLA_MAX
+ 1];
1780 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1781 struct interface
*ifp
;
1785 char *slave_kind
= NULL
;
1786 struct zebra_ns
*zns
;
1787 vrf_id_t vrf_id
= VRF_DEFAULT
;
1788 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1789 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1790 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1791 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1792 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1793 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1794 struct zebra_if
*zif
;
1795 ns_id_t link_nsid
= ns_id
;
1796 ifindex_t master_infindex
= IFINDEX_INTERNAL
;
1799 zns
= zebra_ns_lookup(ns_id
);
1800 ifi
= NLMSG_DATA(h
);
1802 /* assume if not default zns, then new VRF */
1803 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1804 /* If this is not link add/delete message so print warning. */
1805 zlog_debug("netlink_link_change: wrong kernel message %s",
1806 nl_msg_type_to_str(h
->nlmsg_type
));
1810 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1811 || ifi
->ifi_family
== AF_INET6
)) {
1813 EC_ZEBRA_UNKNOWN_FAMILY
,
1814 "Invalid address family: %u received from kernel link change: %s",
1815 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1819 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1822 "%s: Message received from netlink is of a broken size %d %zu",
1823 __func__
, h
->nlmsg_len
,
1824 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1828 /* We are interested in some AF_BRIDGE notifications. */
1829 if (ifi
->ifi_family
== AF_BRIDGE
)
1830 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1832 /* Looking up interface name. */
1833 memset(linkinfo
, 0, sizeof(linkinfo
));
1834 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
1837 /* check for wireless messages to ignore */
1838 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1839 if (IS_ZEBRA_DEBUG_KERNEL
)
1840 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1845 if (tb
[IFLA_IFNAME
] == NULL
)
1847 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1849 /* Must be valid string. */
1850 len
= RTA_PAYLOAD(tb
[IFLA_IFNAME
]);
1851 if (len
< 2 || name
[len
- 1] != '\0') {
1852 if (IS_ZEBRA_DEBUG_KERNEL
)
1853 zlog_debug("%s: invalid intf name", __func__
);
1857 if (tb
[IFLA_LINKINFO
]) {
1858 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1861 if (linkinfo
[IFLA_INFO_KIND
])
1862 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1864 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1865 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1867 netlink_determine_zebra_iftype(kind
, &zif_type
);
1870 /* If linking to another interface, note it. */
1872 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1874 if (tb
[IFLA_LINK_NETNSID
]) {
1875 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1876 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1878 if (tb
[IFLA_IFALIAS
]) {
1879 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1882 /* See if interface is present. */
1883 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1885 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1886 /* If VRF, create or update the VRF structure itself. */
1887 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1888 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1889 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1892 if (tb
[IFLA_MASTER
]) {
1893 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1894 && !vrf_is_backend_netns()) {
1895 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1896 master_infindex
= vrf_id
=
1897 *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1898 } else if (slave_kind
1899 && (strcmp(slave_kind
, "bridge") == 0)) {
1900 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1901 master_infindex
= bridge_ifindex
=
1902 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1903 } else if (slave_kind
1904 && (strcmp(slave_kind
, "bond") == 0)) {
1905 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1906 master_infindex
= bond_ifindex
=
1907 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1908 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1910 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1912 if (vrf_is_backend_netns())
1913 vrf_id
= (vrf_id_t
)ns_id
;
1915 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1916 /* Add interface notification from kernel */
1917 if (IS_ZEBRA_DEBUG_KERNEL
)
1919 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1920 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1921 zif_slave_type
, master_infindex
,
1925 /* unknown interface */
1926 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1928 /* pre-configured interface, learnt now */
1929 if (ifp
->vrf
->vrf_id
!= vrf_id
)
1930 if_update_to_new_vrf(ifp
, vrf_id
);
1933 /* Update interface information. */
1934 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1935 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1936 if (!tb
[IFLA_MTU
]) {
1938 "RTM_NEWLINK for interface %s(%u) without MTU set",
1939 name
, ifi
->ifi_index
);
1942 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1944 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1946 /* Set interface type */
1947 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1948 if (IS_ZEBRA_IF_VRF(ifp
))
1949 SET_FLAG(ifp
->status
,
1950 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1953 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
1956 netlink_to_zebra_link_type(ifi
->ifi_type
);
1957 netlink_interface_update_hw_addr(tb
, ifp
);
1959 /* Inform clients, install any configured addresses. */
1962 /* Extract and save L2 interface information, take
1963 * additional actions. */
1964 netlink_interface_update_l2info(
1965 ifp
, linkinfo
[IFLA_INFO_DATA
],
1967 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1968 zebra_l2if_update_bridge_slave(
1969 ifp
, bridge_ifindex
, ns_id
,
1970 ZEBRA_BRIDGE_NO_ACTION
);
1971 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1972 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
1975 if (tb
[IFLA_PROTO_DOWN
])
1976 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
1978 } else if (ifp
->vrf
->vrf_id
!= vrf_id
) {
1979 /* VRF change for an interface. */
1980 if (IS_ZEBRA_DEBUG_KERNEL
)
1982 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1983 name
, ifp
->ifindex
, ifp
->vrf
->vrf_id
,
1984 vrf_id
, ifi
->ifi_flags
);
1986 if_handle_vrf_change(ifp
, vrf_id
);
1988 bool was_bridge_slave
, was_bond_slave
;
1989 uint8_t chgflags
= ZEBRA_BRIDGE_NO_ACTION
;
1992 /* Interface update. */
1993 if (IS_ZEBRA_DEBUG_KERNEL
)
1995 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1996 name
, ifp
->ifindex
, zif_slave_type
,
1997 master_infindex
, ifi
->ifi_flags
);
1999 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2000 if (!tb
[IFLA_MTU
]) {
2002 "RTM_NEWLINK for interface %s(%u) without MTU set",
2003 name
, ifi
->ifi_index
);
2006 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2009 /* Update interface type - NOTE: Only slave_type can
2011 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
2012 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
2013 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2015 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
2018 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2021 netlink_to_zebra_link_type(ifi
->ifi_type
);
2022 netlink_interface_update_hw_addr(tb
, ifp
);
2024 if (tb
[IFLA_PROTO_DOWN
])
2025 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2027 if (if_is_no_ptm_operative(ifp
)) {
2028 bool is_up
= if_is_operative(ifp
);
2029 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2030 if (!if_is_no_ptm_operative(ifp
) ||
2031 CHECK_FLAG(zif
->flags
,
2032 ZIF_FLAG_PROTODOWN
)) {
2033 if (IS_ZEBRA_DEBUG_KERNEL
)
2035 "Intf %s(%u) has gone DOWN",
2036 name
, ifp
->ifindex
);
2038 rib_update(RIB_UPDATE_KERNEL
);
2039 } else if (if_is_operative(ifp
)) {
2040 bool mac_updated
= false;
2042 /* Must notify client daemons of new
2043 * interface status. */
2044 if (IS_ZEBRA_DEBUG_KERNEL
)
2046 "Intf %s(%u) PTM up, notifying clients",
2047 name
, ifp
->ifindex
);
2050 /* Update EVPN VNI when SVI MAC change
2052 if (memcmp(old_hw_addr
, ifp
->hw_addr
,
2053 INTERFACE_HWADDR_MAX
))
2055 if (IS_ZEBRA_IF_VLAN(ifp
)
2057 struct interface
*link_if
;
2060 if_lookup_by_index_per_ns(
2061 zebra_ns_lookup(NS_DEFAULT
),
2064 zebra_vxlan_svi_up(ifp
,
2066 } else if (mac_updated
2067 && IS_ZEBRA_IF_BRIDGE(ifp
)) {
2069 "Intf %s(%u) bridge changed MAC address",
2070 name
, ifp
->ifindex
);
2072 ZEBRA_BRIDGE_MASTER_MAC_CHANGE
;
2076 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2077 if (if_is_operative(ifp
) &&
2078 !CHECK_FLAG(zif
->flags
,
2079 ZIF_FLAG_PROTODOWN
)) {
2080 if (IS_ZEBRA_DEBUG_KERNEL
)
2082 "Intf %s(%u) has come UP",
2083 name
, ifp
->ifindex
);
2085 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2087 ZEBRA_BRIDGE_MASTER_UP
;
2089 if (IS_ZEBRA_DEBUG_KERNEL
)
2091 "Intf %s(%u) has gone DOWN",
2092 name
, ifp
->ifindex
);
2094 rib_update(RIB_UPDATE_KERNEL
);
2098 /* Extract and save L2 interface information, take
2099 * additional actions. */
2100 netlink_interface_update_l2info(
2101 ifp
, linkinfo
[IFLA_INFO_DATA
],
2103 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2104 zebra_l2if_update_bridge(ifp
, chgflags
);
2105 if (IS_ZEBRA_IF_BOND(ifp
))
2106 zebra_l2if_update_bond(ifp
, true);
2107 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
2108 zebra_l2if_update_bridge_slave(
2109 ifp
, bridge_ifindex
, ns_id
, chgflags
);
2110 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
2111 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2117 XFREE(MTYPE_TMP
, zif
->desc
);
2119 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
2122 /* Delete interface notification from kernel */
2124 if (IS_ZEBRA_DEBUG_KERNEL
)
2126 "RTM_DELLINK for unknown interface %s(%u)",
2127 name
, ifi
->ifi_index
);
2131 if (IS_ZEBRA_DEBUG_KERNEL
)
2132 zlog_debug("RTM_DELLINK for %s(%u)", name
,
2135 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
2137 if (IS_ZEBRA_IF_BOND(ifp
))
2138 zebra_l2if_update_bond(ifp
, false);
2139 if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2140 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, false);
2141 /* Special handling for bridge or VxLAN interfaces. */
2142 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2143 zebra_l2_bridge_del(ifp
);
2144 else if (IS_ZEBRA_IF_VXLAN(ifp
))
2145 zebra_l2_vxlanif_del(ifp
);
2147 if_delete_update(&ifp
);
2149 /* If VRF, delete the VRF structure itself. */
2150 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns())
2151 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2158 * Interface encoding helper function.
2160 * \param[in] cmd netlink command.
2161 * \param[in] ctx dataplane context (information snapshot).
2162 * \param[out] buf buffer to hold the packet.
2163 * \param[in] buflen amount of buffer bytes.
2166 ssize_t
netlink_intf_msg_encode(uint16_t cmd
,
2167 const struct zebra_dplane_ctx
*ctx
, void *buf
,
2172 struct ifinfomsg ifa
;
2176 struct rtattr
*nest_protodown_reason
;
2177 ifindex_t ifindex
= dplane_ctx_get_ifindex(ctx
);
2178 bool down
= dplane_ctx_intf_is_protodown(ctx
);
2179 bool pd_reason_val
= dplane_ctx_get_intf_pd_reason_val(ctx
);
2181 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2183 if (buflen
< sizeof(*req
))
2186 memset(req
, 0, sizeof(*req
));
2188 if (cmd
!= RTM_SETLINK
)
2190 EC_ZEBRA_INTF_UPDATE_FAILURE
,
2191 "Only RTM_SETLINK message type currently supported in dplane pthread");
2193 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2194 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2195 req
->n
.nlmsg_type
= cmd
;
2196 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2198 req
->ifa
.ifi_index
= ifindex
;
2200 nl_attr_put8(&req
->n
, buflen
, IFLA_PROTO_DOWN
, down
);
2201 nl_attr_put32(&req
->n
, buflen
, IFLA_LINK
, ifindex
);
2203 /* Reason info nest */
2204 nest_protodown_reason
=
2205 nl_attr_nest(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON
);
2207 if (!nest_protodown_reason
)
2210 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_MASK
,
2211 (1 << frr_protodown_r_bit
));
2212 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_VALUE
,
2213 ((int)pd_reason_val
) << frr_protodown_r_bit
);
2215 nl_attr_nest_end(&req
->n
, nest_protodown_reason
);
2217 if (IS_ZEBRA_DEBUG_KERNEL
)
2218 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2219 __func__
, nl_msg_type_to_str(cmd
), down
,
2220 pd_reason_val
, ifindex
);
2222 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2225 /* Interface information read by netlink. */
2226 void interface_list(struct zebra_ns
*zns
)
2228 interface_lookup_netlink(zns
);
2229 /* We add routes for interface address,
2230 * so we need to get the nexthop info
2231 * from the kernel before we can do that
2233 netlink_nexthop_read(zns
);
2235 interface_addr_lookup_netlink(zns
);
2238 void if_netlink_set_frr_protodown_r_bit(uint8_t bit
)
2240 if (IS_ZEBRA_DEBUG_KERNEL
)
2242 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2243 frr_protodown_r_bit
, bit
);
2245 frr_protodown_r_bit
= bit
;
2248 void if_netlink_unset_frr_protodown_r_bit(void)
2250 if (IS_ZEBRA_DEBUG_KERNEL
)
2252 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2253 frr_protodown_r_bit
, FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2255 frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
2259 bool if_netlink_frr_protodown_r_bit_is_set(void)
2261 return (frr_protodown_r_bit
!= FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2264 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2266 return frr_protodown_r_bit
;
2269 #endif /* GNU_LINUX */