2 * Interface looking up by netlink.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 /* The following definition is to workaround an issue in the Linux kernel
27 * header files with redefinition of 'struct in6_addr' in both
28 * netinet/in.h and linux/in6.h.
29 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
35 #include <netinet/if_ether.h>
36 #include <linux/if_bridge.h>
37 #include <linux/if_link.h>
38 #include <linux/if_tunnel.h>
39 #include <net/if_arp.h>
40 #include <linux/sockios.h>
41 #include <linux/ethtool.h>
47 #include "connected.h"
57 #include "lib_errors.h"
60 #include "zebra/zserv.h"
61 #include "zebra/zebra_ns.h"
62 #include "zebra/zebra_vrf.h"
64 #include "zebra/redistribute.h"
65 #include "zebra/interface.h"
66 #include "zebra/debug.h"
67 #include "zebra/rtadv.h"
68 #include "zebra/zebra_ptm.h"
69 #include "zebra/zebra_mpls.h"
70 #include "zebra/kernel_netlink.h"
71 #include "zebra/rt_netlink.h"
72 #include "zebra/if_netlink.h"
73 #include "zebra/zebra_errors.h"
74 #include "zebra/zebra_vxlan.h"
75 #include "zebra/zebra_evpn_mh.h"
76 #include "zebra/zebra_l2.h"
77 #include "zebra/netconf_netlink.h"
78 #include "zebra/zebra_trace.h"
80 extern struct zebra_privs_t zserv_privs
;
81 uint8_t frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
83 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
84 names and ifindex values. */
85 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
88 struct interface
*oifp
;
90 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
92 if (ifi_index
== IFINDEX_INTERNAL
)
95 "Netlink is setting interface %s ifindex to reserved internal value %u",
96 ifp
->name
, ifi_index
);
98 if (IS_ZEBRA_DEBUG_KERNEL
)
100 "interface index %d was renamed from %s to %s",
101 ifi_index
, oifp
->name
, ifp
->name
);
105 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
106 ifi_index
, oifp
->name
, ifp
->name
);
107 if_delete_update(&oifp
);
110 if_set_index(ifp
, ifi_index
);
113 /* Utility function to parse hardware link-layer address and update ifp */
114 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
115 struct interface
*ifp
)
119 if (tb
[IFLA_ADDRESS
]) {
122 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
124 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
125 zlog_debug("Hardware address is too large: %d",
128 ifp
->hw_addr_len
= hw_addr_len
;
129 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
132 for (i
= 0; i
< hw_addr_len
; i
++)
133 if (ifp
->hw_addr
[i
] != 0)
136 if (i
== hw_addr_len
)
137 ifp
->hw_addr_len
= 0;
139 ifp
->hw_addr_len
= hw_addr_len
;
144 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
148 return ZEBRA_LLT_ETHER
;
150 return ZEBRA_LLT_EETHER
;
152 return ZEBRA_LLT_AX25
;
154 return ZEBRA_LLT_PRONET
;
156 return ZEBRA_LLT_IEEE802
;
158 return ZEBRA_LLT_ARCNET
;
159 case ARPHRD_APPLETLK
:
160 return ZEBRA_LLT_APPLETLK
;
162 return ZEBRA_LLT_DLCI
;
164 return ZEBRA_LLT_ATM
;
165 case ARPHRD_METRICOM
:
166 return ZEBRA_LLT_METRICOM
;
167 case ARPHRD_IEEE1394
:
168 return ZEBRA_LLT_IEEE1394
;
170 return ZEBRA_LLT_EUI64
;
171 case ARPHRD_INFINIBAND
:
172 return ZEBRA_LLT_INFINIBAND
;
174 return ZEBRA_LLT_SLIP
;
176 return ZEBRA_LLT_CSLIP
;
178 return ZEBRA_LLT_SLIP6
;
180 return ZEBRA_LLT_CSLIP6
;
182 return ZEBRA_LLT_RSRVD
;
184 return ZEBRA_LLT_ADAPT
;
186 return ZEBRA_LLT_ROSE
;
188 return ZEBRA_LLT_X25
;
190 return ZEBRA_LLT_PPP
;
192 return ZEBRA_LLT_CHDLC
;
194 return ZEBRA_LLT_LAPB
;
196 return ZEBRA_LLT_RAWHDLC
;
198 return ZEBRA_LLT_IPIP
;
200 return ZEBRA_LLT_IPIP6
;
202 return ZEBRA_LLT_FRAD
;
204 return ZEBRA_LLT_SKIP
;
205 case ARPHRD_LOOPBACK
:
206 return ZEBRA_LLT_LOOPBACK
;
207 case ARPHRD_LOCALTLK
:
208 return ZEBRA_LLT_LOCALTLK
;
210 return ZEBRA_LLT_FDDI
;
212 return ZEBRA_LLT_SIT
;
214 return ZEBRA_LLT_IPDDP
;
216 return ZEBRA_LLT_IPGRE
;
218 return ZEBRA_LLT_PIMREG
;
220 return ZEBRA_LLT_HIPPI
;
222 return ZEBRA_LLT_ECONET
;
224 return ZEBRA_LLT_IRDA
;
226 return ZEBRA_LLT_FCPP
;
228 return ZEBRA_LLT_FCAL
;
230 return ZEBRA_LLT_FCPL
;
231 case ARPHRD_FCFABRIC
:
232 return ZEBRA_LLT_FCFABRIC
;
233 case ARPHRD_IEEE802_TR
:
234 return ZEBRA_LLT_IEEE802_TR
;
235 case ARPHRD_IEEE80211
:
236 return ZEBRA_LLT_IEEE80211
;
237 #ifdef ARPHRD_IEEE802154
238 case ARPHRD_IEEE802154
:
239 return ZEBRA_LLT_IEEE802154
;
243 return ZEBRA_LLT_IP6GRE
;
245 #ifdef ARPHRD_IEEE802154_PHY
246 case ARPHRD_IEEE802154_PHY
:
247 return ZEBRA_LLT_IEEE802154_PHY
;
251 return ZEBRA_LLT_UNKNOWN
;
255 static inline void zebra_if_set_ziftype(struct interface
*ifp
,
256 enum zebra_iftype zif_type
,
257 enum zebra_slave_iftype zif_slave_type
)
259 struct zebra_if
*zif
;
261 zif
= (struct zebra_if
*)ifp
->info
;
262 zif
->zif_slave_type
= zif_slave_type
;
264 if (zif
->zif_type
!= zif_type
) {
265 zif
->zif_type
= zif_type
;
266 /* If the if_type has been set to bond initialize ES info
267 * against it. XXX - note that we don't handle the case where
268 * a zif changes from bond to non-bond; it is really
269 * an unexpected/error condition.
271 zebra_evpn_if_init(zif
);
275 static void netlink_determine_zebra_iftype(const char *kind
,
276 enum zebra_iftype
*zif_type
)
278 *zif_type
= ZEBRA_IF_OTHER
;
283 if (strcmp(kind
, "vrf") == 0)
284 *zif_type
= ZEBRA_IF_VRF
;
285 else if (strcmp(kind
, "bridge") == 0)
286 *zif_type
= ZEBRA_IF_BRIDGE
;
287 else if (strcmp(kind
, "vlan") == 0)
288 *zif_type
= ZEBRA_IF_VLAN
;
289 else if (strcmp(kind
, "vxlan") == 0)
290 *zif_type
= ZEBRA_IF_VXLAN
;
291 else if (strcmp(kind
, "macvlan") == 0)
292 *zif_type
= ZEBRA_IF_MACVLAN
;
293 else if (strcmp(kind
, "veth") == 0)
294 *zif_type
= ZEBRA_IF_VETH
;
295 else if (strcmp(kind
, "bond") == 0)
296 *zif_type
= ZEBRA_IF_BOND
;
297 else if (strcmp(kind
, "bond_slave") == 0)
298 *zif_type
= ZEBRA_IF_BOND_SLAVE
;
299 else if (strcmp(kind
, "gre") == 0)
300 *zif_type
= ZEBRA_IF_GRE
;
303 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
304 uint32_t ns_id
, const char *name
)
306 struct ifinfomsg
*ifi
;
307 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
308 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
309 struct vrf
*vrf
= NULL
;
310 struct zebra_vrf
*zvrf
;
311 uint32_t nl_table_id
;
315 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
317 if (!linkinfo
[IFLA_INFO_DATA
]) {
318 if (IS_ZEBRA_DEBUG_KERNEL
)
320 "%s: IFLA_INFO_DATA missing from VRF message: %s",
325 netlink_parse_rtattr_nested(attr
, IFLA_VRF_MAX
,
326 linkinfo
[IFLA_INFO_DATA
]);
327 if (!attr
[IFLA_VRF_TABLE
]) {
328 if (IS_ZEBRA_DEBUG_KERNEL
)
330 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
335 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
337 if (h
->nlmsg_type
== RTM_NEWLINK
) {
338 if (IS_ZEBRA_DEBUG_KERNEL
)
339 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
340 ifi
->ifi_index
, nl_table_id
);
342 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
345 exist_id
= vrf_lookup_by_table(nl_table_id
, ns_id
);
346 if (exist_id
!= VRF_DEFAULT
) {
347 vrf
= vrf_lookup_by_id(exist_id
);
350 EC_ZEBRA_VRF_MISCONFIGURED
,
351 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
352 name
, ifi
->ifi_index
, vrf
->name
);
357 vrf
= vrf_update((vrf_id_t
)ifi
->ifi_index
, name
);
359 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
360 name
, ifi
->ifi_index
);
365 * This is the only place that we get the actual kernel table_id
366 * being used. We need it to set the table_id of the routes
367 * we are passing to the kernel.... And to throw some totally
368 * awesome parties. that too.
370 * At this point we *must* have a zvrf because the vrf_create
371 * callback creates one. We *must* set the table id
372 * before the vrf_enable because of( at the very least )
373 * static routes being delayed for installation until
374 * during the vrf_enable callbacks.
376 zvrf
= (struct zebra_vrf
*)vrf
->info
;
377 zvrf
->table_id
= nl_table_id
;
379 /* Enable the created VRF. */
380 if (!vrf_enable(vrf
)) {
381 flog_err(EC_LIB_INTERFACE
,
382 "Failed to enable VRF %s id %u", name
,
387 } else // h->nlmsg_type == RTM_DELLINK
389 if (IS_ZEBRA_DEBUG_KERNEL
)
390 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
393 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
396 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
405 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
408 struct ethtool_cmd ecmd
;
411 const char *ifname
= interface
->name
;
415 /* initialize struct */
416 memset(&ifdata
, 0, sizeof(ifdata
));
418 /* set interface name */
419 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
421 /* initialize ethtool interface */
422 memset(&ecmd
, 0, sizeof(ecmd
));
423 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
424 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
426 /* use ioctl to get speed of an interface */
427 frr_with_privs(&zserv_privs
) {
428 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
429 interface
->vrf
->vrf_id
, NULL
);
431 if (IS_ZEBRA_DEBUG_KERNEL
)
432 zlog_debug("Failure to read interface %s speed: %d %s",
433 ifname
, errno
, safe_strerror(errno
));
434 /* no vrf socket creation may probably mean vrf issue */
439 /* Get the current link state for the interface */
440 rc
= vrf_ioctl(interface
->vrf
->vrf_id
, sd
, SIOCETHTOOL
,
444 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
446 "IOCTL failure to read interface %s speed: %d %s",
447 ifname
, errno
, safe_strerror(errno
));
448 /* no device means interface unreachable */
449 if (errno
== ENODEV
&& error
)
457 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
460 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
462 return get_iflink_speed(ifp
, error
);
466 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
471 struct ifinfomsg ifi
;
476 struct rtattr
*rta_info
, *rta_data
;
477 const struct zebra_l2info_gre
*gre_info
;
479 if (buflen
< sizeof(*req
))
481 memset(req
, 0, sizeof(*req
));
483 req
->n
.nlmsg_type
= RTM_NEWLINK
;
484 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
485 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
487 req
->ifi
.ifi_index
= dplane_ctx_get_ifindex(ctx
);
489 gre_info
= dplane_ctx_gre_get_info(ctx
);
493 req
->ifi
.ifi_change
= 0xFFFFFFFF;
494 link_idx
= dplane_ctx_gre_get_link_ifindex(ctx
);
495 mtu
= dplane_ctx_gre_get_mtu(ctx
);
497 if (mtu
&& !nl_attr_put32(&req
->n
, buflen
, IFLA_MTU
, mtu
))
500 rta_info
= nl_attr_nest(&req
->n
, buflen
, IFLA_LINKINFO
);
504 if (!nl_attr_put(&req
->n
, buflen
, IFLA_INFO_KIND
, "gre", 3))
507 rta_data
= nl_attr_nest(&req
->n
, buflen
, IFLA_INFO_DATA
);
511 if (!nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LINK
, link_idx
))
514 if (gre_info
->vtep_ip
.s_addr
&&
515 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LOCAL
,
516 gre_info
->vtep_ip
.s_addr
))
519 if (gre_info
->vtep_ip_remote
.s_addr
&&
520 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_REMOTE
,
521 gre_info
->vtep_ip_remote
.s_addr
))
524 if (gre_info
->ikey
&&
525 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
528 if (gre_info
->okey
&&
529 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
533 nl_attr_nest_end(&req
->n
, rta_data
);
534 nl_attr_nest_end(&req
->n
, rta_info
);
536 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
539 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
540 struct zebra_l2info_bridge
*bridge_info
)
542 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
544 memset(bridge_info
, 0, sizeof(*bridge_info
));
545 netlink_parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
546 if (attr
[IFLA_BR_VLAN_FILTERING
])
547 bridge_info
->bridge
.vlan_aware
=
548 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
552 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
553 struct zebra_l2info_vlan
*vlan_info
)
555 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
558 memset(vlan_info
, 0, sizeof(*vlan_info
));
559 netlink_parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
560 if (!attr
[IFLA_VLAN_ID
]) {
561 if (IS_ZEBRA_DEBUG_KERNEL
)
562 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
566 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
567 vlan_info
->vid
= vid_in_msg
;
571 static int netlink_extract_gre_info(struct rtattr
*link_data
,
572 struct zebra_l2info_gre
*gre_info
)
574 struct rtattr
*attr
[IFLA_GRE_MAX
+ 1];
576 memset(gre_info
, 0, sizeof(*gre_info
));
577 memset(attr
, 0, sizeof(attr
));
578 netlink_parse_rtattr_nested(attr
, IFLA_GRE_MAX
, link_data
);
580 if (!attr
[IFLA_GRE_LOCAL
]) {
581 if (IS_ZEBRA_DEBUG_KERNEL
)
583 "IFLA_GRE_LOCAL missing from GRE IF message");
586 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_LOCAL
]);
587 if (!attr
[IFLA_GRE_REMOTE
]) {
588 if (IS_ZEBRA_DEBUG_KERNEL
)
590 "IFLA_GRE_REMOTE missing from GRE IF message");
592 gre_info
->vtep_ip_remote
=
593 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_REMOTE
]);
595 if (!attr
[IFLA_GRE_LINK
]) {
596 if (IS_ZEBRA_DEBUG_KERNEL
)
597 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
599 gre_info
->ifindex_link
=
600 *(ifindex_t
*)RTA_DATA(attr
[IFLA_GRE_LINK
]);
601 if (IS_ZEBRA_DEBUG_KERNEL
)
602 zlog_debug("IFLA_GRE_LINK obtained is %u",
603 gre_info
->ifindex_link
);
605 if (attr
[IFLA_GRE_IKEY
])
606 gre_info
->ikey
= *(uint32_t *)RTA_DATA(attr
[IFLA_GRE_IKEY
]);
607 if (attr
[IFLA_GRE_OKEY
])
608 gre_info
->okey
= *(uint32_t *)RTA_DATA(attr
[IFLA_GRE_OKEY
]);
612 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
613 struct zebra_l2info_vxlan
*vxl_info
)
616 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
618 struct in_addr vtep_ip_in_msg
;
619 ifindex_t ifindex_link
;
621 memset(vxl_info
, 0, sizeof(*vxl_info
));
622 netlink_parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
623 if (attr
[IFLA_VXLAN_COLLECT_METADATA
]) {
624 svd
= *(uint8_t *)RTA_DATA(attr
[IFLA_VXLAN_COLLECT_METADATA
]);
625 if (IS_ZEBRA_DEBUG_KERNEL
)
627 "IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
632 /* in case of svd we will not get vni info directly from the
634 if (!attr
[IFLA_VXLAN_ID
]) {
635 if (IS_ZEBRA_DEBUG_KERNEL
)
637 "IFLA_VXLAN_ID missing from VXLAN IF message");
641 vxl_info
->vni_info
.iftype
= ZEBRA_VXLAN_IF_VNI
;
642 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
643 vxl_info
->vni_info
.vni
.vni
= vni_in_msg
;
645 vxl_info
->vni_info
.iftype
= ZEBRA_VXLAN_IF_SVD
;
648 if (!attr
[IFLA_VXLAN_LOCAL
]) {
649 if (IS_ZEBRA_DEBUG_KERNEL
)
651 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
654 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
655 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
658 if (attr
[IFLA_VXLAN_GROUP
]) {
660 vxl_info
->vni_info
.vni
.mcast_grp
=
661 *(struct in_addr
*)RTA_DATA(
662 attr
[IFLA_VXLAN_GROUP
]);
665 if (!attr
[IFLA_VXLAN_LINK
]) {
666 if (IS_ZEBRA_DEBUG_KERNEL
)
667 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
670 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
671 vxl_info
->ifindex_link
= ifindex_link
;
677 * Extract and save L2 params (of interest) for an interface. When a
678 * bridge interface is added or updated, take further actions to map
679 * its members. Likewise, for VxLAN interface.
681 static void netlink_interface_update_l2info(struct interface
*ifp
,
682 struct rtattr
*link_data
, int add
,
688 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
689 struct zebra_l2info_bridge bridge_info
;
691 netlink_extract_bridge_info(link_data
, &bridge_info
);
692 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
693 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
694 struct zebra_l2info_vlan vlan_info
;
696 netlink_extract_vlan_info(link_data
, &vlan_info
);
697 zebra_l2_vlanif_update(ifp
, &vlan_info
);
698 zebra_evpn_acc_bd_svi_set(ifp
->info
, NULL
,
699 !!if_is_operative(ifp
));
700 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
701 struct zebra_l2info_vxlan vxlan_info
;
703 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
704 vxlan_info
.link_nsid
= link_nsid
;
705 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
706 if (link_nsid
!= NS_UNKNOWN
&&
707 vxlan_info
.ifindex_link
)
708 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
710 } else if (IS_ZEBRA_IF_GRE(ifp
)) {
711 struct zebra_l2info_gre gre_info
;
713 netlink_extract_gre_info(link_data
, &gre_info
);
714 gre_info
.link_nsid
= link_nsid
;
715 zebra_l2_greif_add_update(ifp
, &gre_info
, add
);
716 if (link_nsid
!= NS_UNKNOWN
&&
717 gre_info
.ifindex_link
)
718 zebra_if_update_link(ifp
, gre_info
.ifindex_link
,
723 static int netlink_bridge_vxlan_update(struct interface
*ifp
,
724 struct rtattr
*af_spec
)
726 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
727 struct bridge_vlan_info
*vinfo
;
728 vlanid_t access_vlan
;
733 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
734 * only 1 access VLAN is accepted.
736 netlink_parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, af_spec
);
737 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
740 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
741 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
744 access_vlan
= (vlanid_t
)vinfo
->vid
;
745 if (IS_ZEBRA_DEBUG_KERNEL
)
746 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
747 ifp
->name
, ifp
->ifindex
);
748 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
752 static void netlink_bridge_vlan_update(struct interface
*ifp
,
753 struct rtattr
*af_spec
)
757 uint16_t vid_range_start
= 0;
758 struct zebra_if
*zif
;
759 bitfield_t old_vlan_bitmap
;
760 struct bridge_vlan_info
*vinfo
;
762 zif
= (struct zebra_if
*)ifp
->info
;
764 /* cache the old bitmap addrs */
765 old_vlan_bitmap
= zif
->vlan_bitmap
;
766 /* create a new bitmap space for re-eval */
767 bf_init(zif
->vlan_bitmap
, IF_VLAN_BITMAP_MAX
);
770 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
);
771 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
773 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
778 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
779 vid_range_start
= vinfo
->vid
;
783 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
784 vid_range_start
= vinfo
->vid
;
786 zebra_vlan_bitmap_compute(ifp
, vid_range_start
,
791 zebra_vlan_mbr_re_eval(ifp
, old_vlan_bitmap
);
793 bf_free(old_vlan_bitmap
);
796 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
800 struct ifinfomsg
*ifi
;
801 struct rtattr
*tb
[IFLA_MAX
+ 1];
802 struct interface
*ifp
;
803 struct zebra_if
*zif
;
804 struct rtattr
*af_spec
;
806 /* Fetch name and ifindex */
808 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
810 if (tb
[IFLA_IFNAME
] == NULL
)
812 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
814 /* The interface should already be known, if not discard. */
815 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
817 zlog_debug("Cannot find bridge IF %s(%u)", name
,
822 /* We are only interested in the access VLAN i.e., AF_SPEC */
823 af_spec
= tb
[IFLA_AF_SPEC
];
825 if (IS_ZEBRA_IF_VXLAN(ifp
))
826 return netlink_bridge_vxlan_update(ifp
, af_spec
);
828 /* build vlan bitmap associated with this interface if that
829 * device type is interested in the vlans
831 zif
= (struct zebra_if
*)ifp
->info
;
832 if (bf_is_inited(zif
->vlan_bitmap
))
833 netlink_bridge_vlan_update(ifp
, af_spec
);
838 static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield
)
840 /* This shouldn't be possible */
841 assert(frr_protodown_r_bit
< 32);
842 return (rc_bitfield
== (((uint32_t)1) << frr_protodown_r_bit
));
846 * Process interface protodown dplane update.
848 * If the interface is an es bond member then it must follow EVPN's
851 static void netlink_proc_dplane_if_protodown(struct zebra_if
*zif
,
856 uint32_t rc_bitfield
= 0;
857 struct rtattr
*pd_reason_info
[IFLA_MAX
+ 1];
859 protodown
= !!*(uint8_t *)RTA_DATA(tb
[IFLA_PROTO_DOWN
]);
861 if (tb
[IFLA_PROTO_DOWN_REASON
]) {
862 netlink_parse_rtattr_nested(pd_reason_info
, IFLA_INFO_MAX
,
863 tb
[IFLA_PROTO_DOWN_REASON
]);
865 if (pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
])
866 rc_bitfield
= *(uint32_t *)RTA_DATA(
867 pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
]);
871 * Set our reason code to note it wasn't us.
872 * If the reason we got from the kernel is ONLY frr though, don't
875 COND_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_EXTERNAL
,
876 protodown
&& rc_bitfield
&&
877 !is_if_protodown_reason_only_frr(rc_bitfield
));
880 old_protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
881 if (protodown
== old_protodown
)
884 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
885 zlog_debug("interface %s dplane change, protdown %s",
886 zif
->ifp
->name
, protodown
? "on" : "off");
888 /* Set protodown, respectively */
889 COND_FLAG(zif
->flags
, ZIF_FLAG_PROTODOWN
, protodown
);
891 if (zebra_evpn_is_es_bond_member(zif
->ifp
)) {
892 /* Check it's not already being sent to the dplane first */
894 CHECK_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
)) {
895 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
897 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
903 CHECK_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
)) {
904 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
906 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
911 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
913 "bond mbr %s reinstate protodown %s in the dplane",
914 zif
->ifp
->name
, old_protodown
? "on" : "off");
917 SET_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
);
919 SET_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
);
921 dplane_intf_update(zif
->ifp
);
925 static uint8_t netlink_parse_lacp_bypass(struct rtattr
**linkinfo
)
928 struct rtattr
*mbrinfo
[IFLA_BOND_SLAVE_MAX
+ 1];
930 netlink_parse_rtattr_nested(mbrinfo
, IFLA_BOND_SLAVE_MAX
,
931 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
932 if (mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
])
933 bypass
= *(uint8_t *)RTA_DATA(
934 mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
]);
940 * Only called at startup to cleanup leftover protodown reasons we may
941 * have not cleaned up. We leave protodown set though.
943 static void if_sweep_protodown(struct zebra_if
*zif
)
947 protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
952 if (IS_ZEBRA_DEBUG_KERNEL
)
953 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
954 zif
->ifp
->name
, protodown
? "on" : "off",
957 /* Only clear our reason codes, leave external if it was set */
958 UNSET_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_ALL
);
959 dplane_intf_update(zif
->ifp
);
963 * Called from interface_lookup_netlink(). This function is only used
966 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
969 struct ifinfomsg
*ifi
;
970 struct rtattr
*tb
[IFLA_MAX
+ 1];
971 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
972 struct interface
*ifp
;
976 char *slave_kind
= NULL
;
977 struct zebra_ns
*zns
= NULL
;
978 vrf_id_t vrf_id
= VRF_DEFAULT
;
979 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
980 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
981 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
982 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
983 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
984 struct zebra_if
*zif
;
985 ns_id_t link_nsid
= ns_id
;
988 frrtrace(3, frr_zebra
, netlink_interface
, h
, ns_id
, startup
);
990 zns
= zebra_ns_lookup(ns_id
);
993 if (h
->nlmsg_type
!= RTM_NEWLINK
)
996 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
999 "%s: Message received from netlink is of a broken size: %d %zu",
1000 __func__
, h
->nlmsg_len
,
1001 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1005 /* We are interested in some AF_BRIDGE notifications. */
1006 if (ifi
->ifi_family
== AF_BRIDGE
)
1007 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1009 /* Looking up interface name. */
1010 memset(linkinfo
, 0, sizeof(linkinfo
));
1011 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
1014 /* check for wireless messages to ignore */
1015 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1016 if (IS_ZEBRA_DEBUG_KERNEL
)
1017 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1022 if (tb
[IFLA_IFNAME
] == NULL
)
1024 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1026 if (tb
[IFLA_IFALIAS
])
1027 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1029 if (tb
[IFLA_LINKINFO
]) {
1030 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1033 if (linkinfo
[IFLA_INFO_KIND
])
1034 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1036 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1037 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1039 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
1040 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
1042 netlink_determine_zebra_iftype(kind
, &zif_type
);
1045 /* If VRF, create the VRF structure itself. */
1046 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1047 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1048 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1051 if (tb
[IFLA_MASTER
]) {
1052 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1053 && !vrf_is_backend_netns()) {
1054 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1055 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1056 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
1057 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1059 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1060 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
1061 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1062 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1063 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1065 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1067 if (vrf_is_backend_netns())
1068 vrf_id
= (vrf_id_t
)ns_id
;
1070 /* If linking to another interface, note it. */
1072 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1074 if (tb
[IFLA_LINK_NETNSID
]) {
1075 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1076 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1079 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1080 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
1082 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1083 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
1085 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
1086 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1088 /* Set zebra interface type */
1089 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1090 if (IS_ZEBRA_IF_VRF(ifp
))
1091 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1094 * Just set the @link/lower-device ifindex. During nldump interfaces are
1095 * not ordered in any fashion so we may end up getting upper devices
1096 * before lower devices. We will setup the real linkage once the dump
1099 zif
= (struct zebra_if
*)ifp
->info
;
1100 zif
->link_ifindex
= link_ifindex
;
1103 XFREE(MTYPE_ZIF_DESC
, zif
->desc
);
1104 zif
->desc
= XSTRDUP(MTYPE_ZIF_DESC
, desc
);
1107 /* Hardware type and address. */
1108 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
1110 netlink_interface_update_hw_addr(tb
, ifp
);
1114 /* Extract and save L2 interface information, take additional actions.
1116 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
1118 if (IS_ZEBRA_IF_BOND(ifp
))
1119 zebra_l2if_update_bond(ifp
, true);
1120 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1121 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
,
1122 ZEBRA_BRIDGE_NO_ACTION
);
1123 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1124 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, !!bypass
);
1126 if (tb
[IFLA_PROTO_DOWN
]) {
1127 netlink_proc_dplane_if_protodown(zif
, tb
);
1128 if_sweep_protodown(zif
);
1134 /* Request for specific interface or address information from the kernel */
1135 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
1136 int type
, uint32_t filter_mask
)
1140 struct ifinfomsg ifm
;
1144 frrtrace(4, frr_zebra
, netlink_request_intf_addr
, netlink_cmd
, family
,
1147 /* Form the request, specifying filter (rtattr) if needed. */
1148 memset(&req
, 0, sizeof(req
));
1149 req
.n
.nlmsg_type
= type
;
1150 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1151 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1152 req
.ifm
.ifi_family
= family
;
1154 /* Include filter, if specified. */
1156 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
1158 return netlink_request(netlink_cmd
, &req
);
1161 enum netlink_msg_status
1162 netlink_put_gre_set_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1164 enum dplane_op_e op
;
1165 enum netlink_msg_status ret
;
1167 op
= dplane_ctx_get_op(ctx
);
1168 assert(op
== DPLANE_OP_GRE_SET
);
1170 ret
= netlink_batch_add_msg(bth
, ctx
, netlink_gre_set_msg_encoder
, false);
1175 /* Interface lookup by netlink socket. */
1176 int interface_lookup_netlink(struct zebra_ns
*zns
)
1179 struct zebra_dplane_info dp_info
;
1180 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1182 /* Capture key info from ns struct */
1183 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1185 /* Get interface information. */
1186 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
1189 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1194 /* Get interface information - for bridge interfaces. */
1195 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
1196 RTEXT_FILTER_BRVLAN
);
1199 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1205 * So netlink_tunneldump_read will initiate a request
1206 * per tunnel to get data. If we are on a kernel that
1207 * does not support this then we will get X error messages
1208 * (one per tunnel request )back which netlink_parse_info will
1209 * stop after the first one. So we need to read equivalent
1210 * error messages per tunnel then we can continue.
1211 * if we do not gather all the read failures then
1212 * later requests will not work right.
1214 ret
= netlink_tunneldump_read(zns
);
1218 /* fixup linkages */
1219 zebra_if_update_all_links(zns
);
1224 * interface_addr_lookup_netlink() - Look up interface addresses
1226 * @zns: Zebra netlink socket
1227 * Return: Result status
1229 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
1232 struct zebra_dplane_info dp_info
;
1233 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1235 /* Capture key info from ns struct */
1236 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1238 /* Get IPv4 address of the interfaces. */
1239 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
1242 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1247 /* Get IPv6 address of the interfaces. */
1248 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
1251 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1259 int kernel_interface_set_master(struct interface
*master
,
1260 struct interface
*slave
)
1262 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1266 struct ifinfomsg ifa
;
1267 char buf
[NL_PKT_BUF_SIZE
];
1270 memset(&req
, 0, sizeof(req
));
1272 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1273 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1274 req
.n
.nlmsg_type
= RTM_SETLINK
;
1275 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1277 req
.ifa
.ifi_index
= slave
->ifindex
;
1279 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
1280 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
1282 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1286 /* Interface address modification. */
1287 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
1288 void *buf
, size_t buflen
)
1291 const struct prefix
*p
;
1297 struct ifaddrmsg ifa
;
1301 if (buflen
< sizeof(*req
))
1304 p
= dplane_ctx_get_intf_addr(ctx
);
1305 memset(req
, 0, sizeof(*req
));
1307 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1309 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1310 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1312 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1317 req
->n
.nlmsg_type
= cmd
;
1318 req
->ifa
.ifa_family
= p
->family
;
1320 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1322 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1325 if (p
->family
== AF_INET
) {
1326 if (dplane_ctx_intf_is_connected(ctx
)) {
1327 p
= dplane_ctx_get_intf_dest(ctx
);
1328 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1329 &p
->u
.prefix
, bytelen
))
1331 } else if (cmd
== RTM_NEWADDR
) {
1332 struct in_addr broad
= {
1333 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1336 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1342 /* p is now either address or destination/bcast addr */
1343 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1345 if (dplane_ctx_intf_is_secondary(ctx
))
1346 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1348 if (dplane_ctx_intf_has_label(ctx
)) {
1349 label
= dplane_ctx_get_intf_label(ctx
);
1350 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1355 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1358 enum netlink_msg_status
1359 netlink_put_address_update_msg(struct nl_batch
*bth
,
1360 struct zebra_dplane_ctx
*ctx
)
1362 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1366 static ssize_t
netlink_intf_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
1369 enum dplane_op_e op
;
1372 op
= dplane_ctx_get_op(ctx
);
1375 case DPLANE_OP_INTF_UPDATE
:
1378 case DPLANE_OP_INTF_INSTALL
:
1381 case DPLANE_OP_INTF_DELETE
:
1384 case DPLANE_OP_NONE
:
1385 case DPLANE_OP_ROUTE_INSTALL
:
1386 case DPLANE_OP_ROUTE_UPDATE
:
1387 case DPLANE_OP_ROUTE_DELETE
:
1388 case DPLANE_OP_ROUTE_NOTIFY
:
1389 case DPLANE_OP_NH_INSTALL
:
1390 case DPLANE_OP_NH_UPDATE
:
1391 case DPLANE_OP_NH_DELETE
:
1392 case DPLANE_OP_LSP_INSTALL
:
1393 case DPLANE_OP_LSP_DELETE
:
1394 case DPLANE_OP_LSP_NOTIFY
:
1395 case DPLANE_OP_LSP_UPDATE
:
1396 case DPLANE_OP_PW_INSTALL
:
1397 case DPLANE_OP_PW_UNINSTALL
:
1398 case DPLANE_OP_SYS_ROUTE_ADD
:
1399 case DPLANE_OP_SYS_ROUTE_DELETE
:
1400 case DPLANE_OP_ADDR_INSTALL
:
1401 case DPLANE_OP_ADDR_UNINSTALL
:
1402 case DPLANE_OP_MAC_INSTALL
:
1403 case DPLANE_OP_MAC_DELETE
:
1404 case DPLANE_OP_NEIGH_INSTALL
:
1405 case DPLANE_OP_NEIGH_UPDATE
:
1406 case DPLANE_OP_NEIGH_DELETE
:
1407 case DPLANE_OP_NEIGH_DISCOVER
:
1408 case DPLANE_OP_VTEP_ADD
:
1409 case DPLANE_OP_VTEP_DELETE
:
1410 case DPLANE_OP_RULE_ADD
:
1411 case DPLANE_OP_RULE_DELETE
:
1412 case DPLANE_OP_RULE_UPDATE
:
1413 case DPLANE_OP_BR_PORT_UPDATE
:
1414 case DPLANE_OP_IPTABLE_ADD
:
1415 case DPLANE_OP_IPTABLE_DELETE
:
1416 case DPLANE_OP_IPSET_ADD
:
1417 case DPLANE_OP_IPSET_ENTRY_ADD
:
1418 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1419 case DPLANE_OP_IPSET_DELETE
:
1420 case DPLANE_OP_NEIGH_IP_INSTALL
:
1421 case DPLANE_OP_NEIGH_IP_DELETE
:
1422 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1423 case DPLANE_OP_GRE_SET
:
1424 case DPLANE_OP_INTF_ADDR_ADD
:
1425 case DPLANE_OP_INTF_ADDR_DEL
:
1426 case DPLANE_OP_INTF_NETCONFIG
:
1427 case DPLANE_OP_TC_QDISC_INSTALL
:
1428 case DPLANE_OP_TC_QDISC_UNINSTALL
:
1429 case DPLANE_OP_TC_CLASS_ADD
:
1430 case DPLANE_OP_TC_CLASS_DELETE
:
1431 case DPLANE_OP_TC_CLASS_UPDATE
:
1432 case DPLANE_OP_TC_FILTER_ADD
:
1433 case DPLANE_OP_TC_FILTER_DELETE
:
1434 case DPLANE_OP_TC_FILTER_UPDATE
:
1436 EC_ZEBRA_NHG_FIB_UPDATE
,
1437 "Context received for kernel interface update with incorrect OP code (%u)",
1442 return netlink_intf_msg_encode(cmd
, ctx
, buf
, buflen
);
1445 enum netlink_msg_status
1446 netlink_put_intf_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1448 return netlink_batch_add_msg(bth
, ctx
, netlink_intf_msg_encoder
, false);
1451 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1454 struct ifaddrmsg
*ifa
;
1455 struct rtattr
*tb
[IFA_MAX
+ 1];
1456 struct interface
*ifp
;
1461 struct zebra_ns
*zns
;
1462 uint32_t metric
= METRIC_MAX
;
1463 uint32_t kernel_flags
= 0;
1465 frrtrace(3, frr_zebra
, netlink_interface_addr
, h
, ns_id
, startup
);
1467 zns
= zebra_ns_lookup(ns_id
);
1468 ifa
= NLMSG_DATA(h
);
1470 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1472 EC_ZEBRA_UNKNOWN_FAMILY
,
1473 "Invalid address family: %u received from kernel interface addr change: %s",
1474 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1478 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1481 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1484 "%s: Message received from netlink is of a broken size: %d %zu",
1485 __func__
, h
->nlmsg_len
,
1486 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1490 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1492 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1495 /* During startup, failure to lookup the referenced
1496 * interface should not be an error, so we have
1497 * downgraded this condition to warning, and we permit
1498 * the startup interface state retrieval to continue.
1500 flog_warn(EC_LIB_INTERFACE
,
1501 "%s: can't find interface by index %d",
1502 __func__
, ifa
->ifa_index
);
1505 flog_err(EC_LIB_INTERFACE
,
1506 "%s: can't find interface by index %d",
1507 __func__
, ifa
->ifa_index
);
1512 /* Flags passed through */
1514 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1516 kernel_flags
= ifa
->ifa_flags
;
1518 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1521 zlog_debug("%s %s %s flags 0x%x:", __func__
,
1522 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1525 zlog_debug(" IFA_LOCAL %s/%d",
1526 inet_ntop(ifa
->ifa_family
,
1527 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1529 ifa
->ifa_prefixlen
);
1530 if (tb
[IFA_ADDRESS
])
1531 zlog_debug(" IFA_ADDRESS %s/%d",
1532 inet_ntop(ifa
->ifa_family
,
1533 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1535 ifa
->ifa_prefixlen
);
1536 if (tb
[IFA_BROADCAST
])
1537 zlog_debug(" IFA_BROADCAST %s/%d",
1538 inet_ntop(ifa
->ifa_family
,
1539 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1541 ifa
->ifa_prefixlen
);
1542 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1543 zlog_debug(" IFA_LABEL %s",
1544 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1546 if (tb
[IFA_CACHEINFO
]) {
1547 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1548 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1549 ci
->ifa_prefered
, ci
->ifa_valid
);
1553 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1554 if (tb
[IFA_LOCAL
] == NULL
)
1555 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1556 if (tb
[IFA_ADDRESS
] == NULL
)
1557 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1559 /* local interface address */
1560 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1562 /* is there a peer address? */
1564 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1565 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1566 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1567 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1569 /* seeking a broadcast address */
1570 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1573 /* addr is primary key, SOL if we don't have one */
1575 zlog_debug("%s: Local Interface Address is NULL for %s",
1576 __func__
, ifp
->name
);
1581 if (kernel_flags
& IFA_F_SECONDARY
)
1582 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1586 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1588 if (label
&& strcmp(ifp
->name
, label
) == 0)
1591 if (tb
[IFA_RT_PRIORITY
])
1592 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1594 /* Register interface address to the interface. */
1595 if (ifa
->ifa_family
== AF_INET
) {
1596 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1598 "Invalid prefix length: %u received from kernel interface addr change: %s",
1600 nl_msg_type_to_str(h
->nlmsg_type
));
1604 if (h
->nlmsg_type
== RTM_NEWADDR
)
1605 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1607 (struct in_addr
*)broad
, label
,
1609 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1610 /* Delete with a peer address */
1611 connected_delete_ipv4(
1612 ifp
, flags
, (struct in_addr
*)addr
,
1613 ifa
->ifa_prefixlen
, broad
);
1615 connected_delete_ipv4(
1616 ifp
, flags
, (struct in_addr
*)addr
,
1617 ifa
->ifa_prefixlen
, NULL
);
1620 if (ifa
->ifa_family
== AF_INET6
) {
1621 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1623 "Invalid prefix length: %u received from kernel interface addr change: %s",
1625 nl_msg_type_to_str(h
->nlmsg_type
));
1628 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1629 /* Only consider valid addresses; we'll not get a
1631 * the kernel till IPv6 DAD has completed, but at init
1633 * does query for and will receive all addresses.
1636 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1637 connected_add_ipv6(ifp
, flags
,
1638 (struct in6_addr
*)addr
,
1639 (struct in6_addr
*)broad
,
1640 ifa
->ifa_prefixlen
, label
,
1643 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1644 NULL
, ifa
->ifa_prefixlen
);
1648 * Linux kernel does not send route delete on interface down/addr del
1649 * so we have to re-process routes it owns (i.e. kernel routes)
1651 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1652 rib_update(RIB_UPDATE_KERNEL
);
1658 * Parse and validate an incoming interface address change message,
1659 * generating a dplane context object.
1660 * This runs in the dplane pthread; the context is enqueued to the
1661 * main pthread for processing.
1663 int netlink_interface_addr_dplane(struct nlmsghdr
*h
, ns_id_t ns_id
,
1664 int startup
/*ignored*/)
1667 struct ifaddrmsg
*ifa
;
1668 struct rtattr
*tb
[IFA_MAX
+ 1];
1672 uint32_t metric
= METRIC_MAX
;
1673 uint32_t kernel_flags
= 0;
1674 struct zebra_dplane_ctx
*ctx
;
1677 ifa
= NLMSG_DATA(h
);
1679 /* Validate message types */
1680 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1683 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1684 if (IS_ZEBRA_DEBUG_KERNEL
)
1685 zlog_debug("%s: %s: Invalid address family: %u",
1686 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1691 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1693 if (IS_ZEBRA_DEBUG_KERNEL
)
1694 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1695 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1697 (size_t)NLMSG_LENGTH(
1698 sizeof(struct ifaddrmsg
)));
1702 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1704 /* Flags passed through */
1706 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1708 kernel_flags
= ifa
->ifa_flags
;
1710 if (IS_ZEBRA_DEBUG_KERNEL
) { /* remove this line to see initial ifcfg */
1711 char buf
[PREFIX_STRLEN
];
1713 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__
,
1714 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
,
1715 ifa
->ifa_index
, kernel_flags
);
1717 zlog_debug(" IFA_LOCAL %s/%d",
1718 inet_ntop(ifa
->ifa_family
,
1719 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1721 ifa
->ifa_prefixlen
);
1722 if (tb
[IFA_ADDRESS
])
1723 zlog_debug(" IFA_ADDRESS %s/%d",
1724 inet_ntop(ifa
->ifa_family
,
1725 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1727 ifa
->ifa_prefixlen
);
1728 if (tb
[IFA_BROADCAST
])
1729 zlog_debug(" IFA_BROADCAST %s/%d",
1730 inet_ntop(ifa
->ifa_family
,
1731 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1733 ifa
->ifa_prefixlen
);
1735 zlog_debug(" IFA_LABEL %s",
1736 (const char *)RTA_DATA(tb
[IFA_LABEL
]));
1738 if (tb
[IFA_CACHEINFO
]) {
1739 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1741 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1742 ci
->ifa_prefered
, ci
->ifa_valid
);
1746 /* Validate prefix length */
1748 if (ifa
->ifa_family
== AF_INET
1749 && ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1750 if (IS_ZEBRA_DEBUG_KERNEL
)
1751 zlog_debug("%s: %s: Invalid prefix length: %u",
1752 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1753 ifa
->ifa_prefixlen
);
1757 if (ifa
->ifa_family
== AF_INET6
) {
1758 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1759 if (IS_ZEBRA_DEBUG_KERNEL
)
1760 zlog_debug("%s: %s: Invalid prefix length: %u",
1762 nl_msg_type_to_str(h
->nlmsg_type
),
1763 ifa
->ifa_prefixlen
);
1767 /* Only consider valid addresses; we'll not get a kernel
1768 * notification till IPv6 DAD has completed, but at init
1769 * time, FRR does query for and will receive all addresses.
1771 if (h
->nlmsg_type
== RTM_NEWADDR
1772 && (kernel_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))) {
1773 if (IS_ZEBRA_DEBUG_KERNEL
)
1774 zlog_debug("%s: %s: Invalid/tentative addr",
1776 nl_msg_type_to_str(h
->nlmsg_type
));
1781 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1782 if (tb
[IFA_LOCAL
] == NULL
)
1783 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1784 if (tb
[IFA_ADDRESS
] == NULL
)
1785 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1787 /* local interface address */
1788 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1790 /* addr is primary key, SOL if we don't have one */
1792 if (IS_ZEBRA_DEBUG_KERNEL
)
1793 zlog_debug("%s: %s: No local interface address",
1794 __func__
, nl_msg_type_to_str(h
->nlmsg_type
));
1798 /* Allocate a context object, now that validation is done. */
1799 ctx
= dplane_ctx_alloc();
1800 if (h
->nlmsg_type
== RTM_NEWADDR
)
1801 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_ADD
);
1803 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_DEL
);
1805 dplane_ctx_set_ifindex(ctx
, ifa
->ifa_index
);
1806 dplane_ctx_set_ns_id(ctx
, ns_id
);
1808 /* Convert addr to prefix */
1809 memset(&p
, 0, sizeof(p
));
1810 p
.family
= ifa
->ifa_family
;
1811 p
.prefixlen
= ifa
->ifa_prefixlen
;
1812 if (p
.family
== AF_INET
)
1813 p
.u
.prefix4
= *(struct in_addr
*)addr
;
1815 p
.u
.prefix6
= *(struct in6_addr
*)addr
;
1817 dplane_ctx_set_intf_addr(ctx
, &p
);
1819 /* is there a peer address? */
1821 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1822 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1823 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1824 dplane_ctx_intf_set_connected(ctx
);
1825 } else if (tb
[IFA_BROADCAST
]) {
1826 /* seeking a broadcast address */
1827 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
1828 dplane_ctx_intf_set_broadcast(ctx
);
1833 /* Convert addr to prefix */
1834 memset(&p
, 0, sizeof(p
));
1835 p
.family
= ifa
->ifa_family
;
1836 p
.prefixlen
= ifa
->ifa_prefixlen
;
1837 if (p
.family
== AF_INET
)
1838 p
.u
.prefix4
= *(struct in_addr
*)broad
;
1840 p
.u
.prefix6
= *(struct in6_addr
*)broad
;
1842 dplane_ctx_set_intf_dest(ctx
, &p
);
1846 if (kernel_flags
& IFA_F_SECONDARY
)
1847 dplane_ctx_intf_set_secondary(ctx
);
1850 if (tb
[IFA_LABEL
]) {
1851 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1852 dplane_ctx_set_intf_label(ctx
, label
);
1855 if (tb
[IFA_RT_PRIORITY
])
1856 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1858 dplane_ctx_set_intf_metric(ctx
, metric
);
1860 /* Enqueue ctx for main pthread to process */
1861 dplane_provider_enqueue_to_zebra(ctx
);
1866 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1869 struct ifinfomsg
*ifi
;
1870 struct rtattr
*tb
[IFLA_MAX
+ 1];
1871 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1872 struct interface
*ifp
;
1876 char *slave_kind
= NULL
;
1877 struct zebra_ns
*zns
;
1878 vrf_id_t vrf_id
= VRF_DEFAULT
;
1879 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1880 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1881 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1882 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1883 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1884 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1885 struct zebra_if
*zif
;
1886 ns_id_t link_nsid
= ns_id
;
1887 ifindex_t master_infindex
= IFINDEX_INTERNAL
;
1890 zns
= zebra_ns_lookup(ns_id
);
1891 ifi
= NLMSG_DATA(h
);
1893 /* assume if not default zns, then new VRF */
1894 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1895 /* If this is not link add/delete message so print warning. */
1896 zlog_debug("%s: wrong kernel message %s", __func__
,
1897 nl_msg_type_to_str(h
->nlmsg_type
));
1901 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1902 || ifi
->ifi_family
== AF_INET6
)) {
1904 EC_ZEBRA_UNKNOWN_FAMILY
,
1905 "Invalid address family: %u received from kernel link change: %s",
1906 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1910 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1913 "%s: Message received from netlink is of a broken size %d %zu",
1914 __func__
, h
->nlmsg_len
,
1915 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1919 /* We are interested in some AF_BRIDGE notifications. */
1920 if (ifi
->ifi_family
== AF_BRIDGE
)
1921 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1923 /* Looking up interface name. */
1924 memset(linkinfo
, 0, sizeof(linkinfo
));
1925 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
1928 /* check for wireless messages to ignore */
1929 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1930 if (IS_ZEBRA_DEBUG_KERNEL
)
1931 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1936 if (tb
[IFLA_IFNAME
] == NULL
)
1938 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1940 /* Must be valid string. */
1941 len
= RTA_PAYLOAD(tb
[IFLA_IFNAME
]);
1942 if (len
< 2 || name
[len
- 1] != '\0') {
1943 if (IS_ZEBRA_DEBUG_KERNEL
)
1944 zlog_debug("%s: invalid intf name", __func__
);
1948 if (tb
[IFLA_LINKINFO
]) {
1949 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1952 if (linkinfo
[IFLA_INFO_KIND
])
1953 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1955 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1956 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1958 netlink_determine_zebra_iftype(kind
, &zif_type
);
1961 /* If linking to another interface, note it. */
1963 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1965 if (tb
[IFLA_LINK_NETNSID
]) {
1966 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1967 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1969 if (tb
[IFLA_IFALIAS
]) {
1970 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1973 /* See if interface is present. */
1974 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1976 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1977 /* If VRF, create or update the VRF structure itself. */
1978 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1979 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1980 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1983 if (tb
[IFLA_MASTER
]) {
1984 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1985 && !vrf_is_backend_netns()) {
1986 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1987 master_infindex
= vrf_id
=
1988 *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1989 } else if (slave_kind
1990 && (strcmp(slave_kind
, "bridge") == 0)) {
1991 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1992 master_infindex
= bridge_ifindex
=
1993 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1994 } else if (slave_kind
1995 && (strcmp(slave_kind
, "bond") == 0)) {
1996 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1997 master_infindex
= bond_ifindex
=
1998 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1999 bypass
= netlink_parse_lacp_bypass(linkinfo
);
2001 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
2003 if (vrf_is_backend_netns())
2004 vrf_id
= (vrf_id_t
)ns_id
;
2006 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
2007 /* Add interface notification from kernel */
2008 if (IS_ZEBRA_DEBUG_KERNEL
)
2010 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
2011 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
2012 zif_slave_type
, master_infindex
,
2016 /* unknown interface */
2017 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
2019 /* pre-configured interface, learnt now */
2020 if (ifp
->vrf
->vrf_id
!= vrf_id
)
2021 if_update_to_new_vrf(ifp
, vrf_id
);
2024 /* Update interface information. */
2025 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2026 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2027 if (!tb
[IFLA_MTU
]) {
2029 "RTM_NEWLINK for interface %s(%u) without MTU set",
2030 name
, ifi
->ifi_index
);
2033 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2035 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
2037 /* Set interface type */
2038 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2039 if (IS_ZEBRA_IF_VRF(ifp
))
2040 SET_FLAG(ifp
->status
,
2041 ZEBRA_INTERFACE_VRF_LOOPBACK
);
2044 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2047 netlink_to_zebra_link_type(ifi
->ifi_type
);
2048 netlink_interface_update_hw_addr(tb
, ifp
);
2050 /* Inform clients, install any configured addresses. */
2053 /* Extract and save L2 interface information, take
2054 * additional actions. */
2055 netlink_interface_update_l2info(
2056 ifp
, linkinfo
[IFLA_INFO_DATA
],
2058 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2059 zebra_l2if_update_bridge_slave(
2060 ifp
, bridge_ifindex
, ns_id
,
2061 ZEBRA_BRIDGE_NO_ACTION
);
2062 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2063 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2066 if (tb
[IFLA_PROTO_DOWN
])
2067 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2069 } else if (ifp
->vrf
->vrf_id
!= vrf_id
) {
2070 /* VRF change for an interface. */
2071 if (IS_ZEBRA_DEBUG_KERNEL
)
2073 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
2074 name
, ifp
->ifindex
, ifp
->vrf
->vrf_id
,
2075 vrf_id
, ifi
->ifi_flags
);
2077 if_handle_vrf_change(ifp
, vrf_id
);
2079 bool was_bridge_slave
, was_bond_slave
;
2080 uint8_t chgflags
= ZEBRA_BRIDGE_NO_ACTION
;
2083 /* Interface update. */
2084 if (IS_ZEBRA_DEBUG_KERNEL
)
2086 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
2087 name
, ifp
->ifindex
, zif_slave_type
,
2088 master_infindex
, ifi
->ifi_flags
);
2090 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2091 if (!tb
[IFLA_MTU
]) {
2093 "RTM_NEWLINK for interface %s(%u) without MTU set",
2094 name
, ifi
->ifi_index
);
2097 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2100 /* Update interface type - NOTE: Only slave_type can
2102 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
2103 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
2104 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2106 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
2109 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2112 netlink_to_zebra_link_type(ifi
->ifi_type
);
2113 netlink_interface_update_hw_addr(tb
, ifp
);
2115 if (tb
[IFLA_PROTO_DOWN
])
2116 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2118 if (if_is_no_ptm_operative(ifp
)) {
2119 bool is_up
= if_is_operative(ifp
);
2120 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2121 if (!if_is_no_ptm_operative(ifp
) ||
2122 CHECK_FLAG(zif
->flags
,
2123 ZIF_FLAG_PROTODOWN
)) {
2124 if (IS_ZEBRA_DEBUG_KERNEL
)
2126 "Intf %s(%u) has gone DOWN",
2127 name
, ifp
->ifindex
);
2129 rib_update(RIB_UPDATE_KERNEL
);
2130 } else if (if_is_operative(ifp
)) {
2131 bool mac_updated
= false;
2133 /* Must notify client daemons of new
2134 * interface status. */
2135 if (IS_ZEBRA_DEBUG_KERNEL
)
2137 "Intf %s(%u) PTM up, notifying clients",
2138 name
, ifp
->ifindex
);
2141 /* Update EVPN VNI when SVI MAC change
2143 if (memcmp(old_hw_addr
, ifp
->hw_addr
,
2144 INTERFACE_HWADDR_MAX
))
2146 if (IS_ZEBRA_IF_VLAN(ifp
)
2148 struct interface
*link_if
;
2151 if_lookup_by_index_per_ns(
2152 zebra_ns_lookup(NS_DEFAULT
),
2155 zebra_vxlan_svi_up(ifp
,
2157 } else if (mac_updated
2158 && IS_ZEBRA_IF_BRIDGE(ifp
)) {
2160 "Intf %s(%u) bridge changed MAC address",
2161 name
, ifp
->ifindex
);
2163 ZEBRA_BRIDGE_MASTER_MAC_CHANGE
;
2167 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2168 if (if_is_operative(ifp
) &&
2169 !CHECK_FLAG(zif
->flags
,
2170 ZIF_FLAG_PROTODOWN
)) {
2171 if (IS_ZEBRA_DEBUG_KERNEL
)
2173 "Intf %s(%u) has come UP",
2174 name
, ifp
->ifindex
);
2176 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2178 ZEBRA_BRIDGE_MASTER_UP
;
2180 if (IS_ZEBRA_DEBUG_KERNEL
)
2182 "Intf %s(%u) has gone DOWN",
2183 name
, ifp
->ifindex
);
2185 rib_update(RIB_UPDATE_KERNEL
);
2189 /* Extract and save L2 interface information, take
2190 * additional actions. */
2191 netlink_interface_update_l2info(
2192 ifp
, linkinfo
[IFLA_INFO_DATA
],
2194 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2195 zebra_l2if_update_bridge(ifp
, chgflags
);
2196 if (IS_ZEBRA_IF_BOND(ifp
))
2197 zebra_l2if_update_bond(ifp
, true);
2198 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
2199 zebra_l2if_update_bridge_slave(
2200 ifp
, bridge_ifindex
, ns_id
, chgflags
);
2201 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
2202 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2208 XFREE(MTYPE_ZIF_DESC
, zif
->desc
);
2210 zif
->desc
= XSTRDUP(MTYPE_ZIF_DESC
, desc
);
2213 /* Delete interface notification from kernel */
2215 if (IS_ZEBRA_DEBUG_KERNEL
)
2217 "RTM_DELLINK for unknown interface %s(%u)",
2218 name
, ifi
->ifi_index
);
2222 if (IS_ZEBRA_DEBUG_KERNEL
)
2223 zlog_debug("RTM_DELLINK for %s(%u)", name
,
2226 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
2228 if (IS_ZEBRA_IF_BOND(ifp
))
2229 zebra_l2if_update_bond(ifp
, false);
2230 if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2231 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, false);
2232 /* Special handling for bridge or VxLAN interfaces. */
2233 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2234 zebra_l2_bridge_del(ifp
);
2235 else if (IS_ZEBRA_IF_VXLAN(ifp
))
2236 zebra_l2_vxlanif_del(ifp
);
2238 if_delete_update(&ifp
);
2240 /* If VRF, delete the VRF structure itself. */
2241 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns())
2242 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2249 * Interface encoding helper function.
2251 * \param[in] cmd netlink command.
2252 * \param[in] ctx dataplane context (information snapshot).
2253 * \param[out] buf buffer to hold the packet.
2254 * \param[in] buflen amount of buffer bytes.
2257 ssize_t
netlink_intf_msg_encode(uint16_t cmd
,
2258 const struct zebra_dplane_ctx
*ctx
, void *buf
,
2263 struct ifinfomsg ifa
;
2267 struct rtattr
*nest_protodown_reason
;
2268 ifindex_t ifindex
= dplane_ctx_get_ifindex(ctx
);
2269 bool down
= dplane_ctx_intf_is_protodown(ctx
);
2270 bool pd_reason_val
= dplane_ctx_get_intf_pd_reason_val(ctx
);
2272 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2274 if (buflen
< sizeof(*req
))
2277 memset(req
, 0, sizeof(*req
));
2279 if (cmd
!= RTM_SETLINK
)
2281 EC_ZEBRA_INTF_UPDATE_FAILURE
,
2282 "Only RTM_SETLINK message type currently supported in dplane pthread");
2284 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2285 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2286 req
->n
.nlmsg_type
= cmd
;
2287 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2289 req
->ifa
.ifi_index
= ifindex
;
2291 nl_attr_put8(&req
->n
, buflen
, IFLA_PROTO_DOWN
, down
);
2292 nl_attr_put32(&req
->n
, buflen
, IFLA_LINK
, ifindex
);
2294 /* Reason info nest */
2295 nest_protodown_reason
=
2296 nl_attr_nest(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON
);
2298 if (!nest_protodown_reason
)
2301 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_MASK
,
2302 (1 << frr_protodown_r_bit
));
2303 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_VALUE
,
2304 ((int)pd_reason_val
) << frr_protodown_r_bit
);
2306 nl_attr_nest_end(&req
->n
, nest_protodown_reason
);
2308 if (IS_ZEBRA_DEBUG_KERNEL
)
2309 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2310 __func__
, nl_msg_type_to_str(cmd
), down
,
2311 pd_reason_val
, ifindex
);
2313 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2316 /* Interface information read by netlink. */
2317 void interface_list(struct zebra_ns
*zns
)
2319 interface_lookup_netlink(zns
);
2320 /* We add routes for interface address,
2321 * so we need to get the nexthop info
2322 * from the kernel before we can do that
2324 netlink_nexthop_read(zns
);
2326 interface_addr_lookup_netlink(zns
);
2329 void if_netlink_set_frr_protodown_r_bit(uint8_t bit
)
2331 if (IS_ZEBRA_DEBUG_KERNEL
)
2333 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2334 frr_protodown_r_bit
, bit
);
2336 frr_protodown_r_bit
= bit
;
2339 void if_netlink_unset_frr_protodown_r_bit(void)
2341 if (IS_ZEBRA_DEBUG_KERNEL
)
2343 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2344 frr_protodown_r_bit
, FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2346 frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
2350 bool if_netlink_frr_protodown_r_bit_is_set(void)
2352 return (frr_protodown_r_bit
!= FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2355 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2357 return frr_protodown_r_bit
;
2361 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2363 * @zns: Zebra namespace
2364 * @family: AF_* netlink family
2365 * @type: RTM_* (RTM_GETTUNNEL) route type
2367 * Return: Result status
2369 static int netlink_request_tunneldump(struct zebra_ns
*zns
, int family
,
2374 struct tunnel_msg tmsg
;
2378 /* Form the request */
2379 memset(&req
, 0, sizeof(req
));
2380 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tunnel_msg
));
2381 req
.n
.nlmsg_type
= RTM_GETTUNNEL
;
2382 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2383 req
.tmsg
.family
= family
;
2384 req
.tmsg
.ifindex
= ifindex
;
2386 return netlink_request(&zns
->netlink_cmd
, &req
);
2390 * Currently we only ask for vxlan l3svd vni information.
2391 * In the future this can be expanded.
2393 int netlink_tunneldump_read(struct zebra_ns
*zns
)
2396 struct zebra_dplane_info dp_info
;
2397 struct route_node
*rn
;
2398 struct interface
*tmp_if
= NULL
;
2399 struct zebra_if
*zif
;
2400 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
2402 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2404 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
2405 tmp_if
= (struct interface
*)rn
->info
;
2409 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VXLAN
)
2412 ret
= netlink_request_tunneldump(zns
, PF_BRIDGE
,
2417 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
,
2426 #endif /* GNU_LINUX */