1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Interface looking up by netlink.
4 * Copyright (C) 1998 Kunihiro Ishiguro
11 /* The following definition is to workaround an issue in the Linux kernel
12 * header files with redefinition of 'struct in6_addr' in both
13 * netinet/in.h and linux/in6.h.
14 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
20 #include <netinet/if_ether.h>
21 #include <linux/if_bridge.h>
22 #include <linux/if_link.h>
23 #include <linux/if_tunnel.h>
24 #include <net/if_arp.h>
25 #include <linux/sockios.h>
26 #include <linux/ethtool.h>
32 #include "connected.h"
42 #include "lib_errors.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/zebra_vrf.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/interface.h"
51 #include "zebra/debug.h"
52 #include "zebra/rtadv.h"
53 #include "zebra/zebra_ptm.h"
54 #include "zebra/zebra_mpls.h"
55 #include "zebra/kernel_netlink.h"
56 #include "zebra/rt_netlink.h"
57 #include "zebra/if_netlink.h"
58 #include "zebra/zebra_errors.h"
59 #include "zebra/zebra_vxlan.h"
60 #include "zebra/zebra_evpn_mh.h"
61 #include "zebra/zebra_l2.h"
62 #include "zebra/netconf_netlink.h"
63 #include "zebra/zebra_trace.h"
65 extern struct zebra_privs_t zserv_privs
;
66 uint8_t frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
68 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
69 names and ifindex values. */
70 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
73 struct interface
*oifp
;
75 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
77 if (ifi_index
== IFINDEX_INTERNAL
)
80 "Netlink is setting interface %s ifindex to reserved internal value %u",
81 ifp
->name
, ifi_index
);
83 if (IS_ZEBRA_DEBUG_KERNEL
)
85 "interface index %d was renamed from %s to %s",
86 ifi_index
, oifp
->name
, ifp
->name
);
90 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
91 ifi_index
, oifp
->name
, ifp
->name
);
92 if_delete_update(&oifp
);
95 if_set_index(ifp
, ifi_index
);
98 /* Utility function to parse hardware link-layer address and update ifp */
99 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
100 struct interface
*ifp
)
104 if (tb
[IFLA_ADDRESS
]) {
107 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
109 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
110 zlog_debug("Hardware address is too large: %d",
113 ifp
->hw_addr_len
= hw_addr_len
;
114 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
117 for (i
= 0; i
< hw_addr_len
; i
++)
118 if (ifp
->hw_addr
[i
] != 0)
121 if (i
== hw_addr_len
)
122 ifp
->hw_addr_len
= 0;
124 ifp
->hw_addr_len
= hw_addr_len
;
129 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
133 return ZEBRA_LLT_ETHER
;
135 return ZEBRA_LLT_EETHER
;
137 return ZEBRA_LLT_AX25
;
139 return ZEBRA_LLT_PRONET
;
141 return ZEBRA_LLT_IEEE802
;
143 return ZEBRA_LLT_ARCNET
;
144 case ARPHRD_APPLETLK
:
145 return ZEBRA_LLT_APPLETLK
;
147 return ZEBRA_LLT_DLCI
;
149 return ZEBRA_LLT_ATM
;
150 case ARPHRD_METRICOM
:
151 return ZEBRA_LLT_METRICOM
;
152 case ARPHRD_IEEE1394
:
153 return ZEBRA_LLT_IEEE1394
;
155 return ZEBRA_LLT_EUI64
;
156 case ARPHRD_INFINIBAND
:
157 return ZEBRA_LLT_INFINIBAND
;
159 return ZEBRA_LLT_SLIP
;
161 return ZEBRA_LLT_CSLIP
;
163 return ZEBRA_LLT_SLIP6
;
165 return ZEBRA_LLT_CSLIP6
;
167 return ZEBRA_LLT_RSRVD
;
169 return ZEBRA_LLT_ADAPT
;
171 return ZEBRA_LLT_ROSE
;
173 return ZEBRA_LLT_X25
;
175 return ZEBRA_LLT_PPP
;
177 return ZEBRA_LLT_CHDLC
;
179 return ZEBRA_LLT_LAPB
;
181 return ZEBRA_LLT_RAWHDLC
;
183 return ZEBRA_LLT_IPIP
;
185 return ZEBRA_LLT_IPIP6
;
187 return ZEBRA_LLT_FRAD
;
189 return ZEBRA_LLT_SKIP
;
190 case ARPHRD_LOOPBACK
:
191 return ZEBRA_LLT_LOOPBACK
;
192 case ARPHRD_LOCALTLK
:
193 return ZEBRA_LLT_LOCALTLK
;
195 return ZEBRA_LLT_FDDI
;
197 return ZEBRA_LLT_SIT
;
199 return ZEBRA_LLT_IPDDP
;
201 return ZEBRA_LLT_IPGRE
;
203 return ZEBRA_LLT_PIMREG
;
205 return ZEBRA_LLT_HIPPI
;
207 return ZEBRA_LLT_ECONET
;
209 return ZEBRA_LLT_IRDA
;
211 return ZEBRA_LLT_FCPP
;
213 return ZEBRA_LLT_FCAL
;
215 return ZEBRA_LLT_FCPL
;
216 case ARPHRD_FCFABRIC
:
217 return ZEBRA_LLT_FCFABRIC
;
218 case ARPHRD_IEEE802_TR
:
219 return ZEBRA_LLT_IEEE802_TR
;
220 case ARPHRD_IEEE80211
:
221 return ZEBRA_LLT_IEEE80211
;
222 #ifdef ARPHRD_IEEE802154
223 case ARPHRD_IEEE802154
:
224 return ZEBRA_LLT_IEEE802154
;
228 return ZEBRA_LLT_IP6GRE
;
230 #ifdef ARPHRD_IEEE802154_PHY
231 case ARPHRD_IEEE802154_PHY
:
232 return ZEBRA_LLT_IEEE802154_PHY
;
236 return ZEBRA_LLT_UNKNOWN
;
240 static inline void zebra_if_set_ziftype(struct interface
*ifp
,
241 enum zebra_iftype zif_type
,
242 enum zebra_slave_iftype zif_slave_type
)
244 struct zebra_if
*zif
;
246 zif
= (struct zebra_if
*)ifp
->info
;
247 zif
->zif_slave_type
= zif_slave_type
;
249 if (zif
->zif_type
!= zif_type
) {
250 zif
->zif_type
= zif_type
;
251 /* If the if_type has been set to bond initialize ES info
252 * against it. XXX - note that we don't handle the case where
253 * a zif changes from bond to non-bond; it is really
254 * an unexpected/error condition.
256 zebra_evpn_if_init(zif
);
260 static void netlink_determine_zebra_iftype(const char *kind
,
261 enum zebra_iftype
*zif_type
)
263 *zif_type
= ZEBRA_IF_OTHER
;
268 if (strcmp(kind
, "vrf") == 0)
269 *zif_type
= ZEBRA_IF_VRF
;
270 else if (strcmp(kind
, "bridge") == 0)
271 *zif_type
= ZEBRA_IF_BRIDGE
;
272 else if (strcmp(kind
, "vlan") == 0)
273 *zif_type
= ZEBRA_IF_VLAN
;
274 else if (strcmp(kind
, "vxlan") == 0)
275 *zif_type
= ZEBRA_IF_VXLAN
;
276 else if (strcmp(kind
, "macvlan") == 0)
277 *zif_type
= ZEBRA_IF_MACVLAN
;
278 else if (strcmp(kind
, "veth") == 0)
279 *zif_type
= ZEBRA_IF_VETH
;
280 else if (strcmp(kind
, "bond") == 0)
281 *zif_type
= ZEBRA_IF_BOND
;
282 else if (strcmp(kind
, "bond_slave") == 0)
283 *zif_type
= ZEBRA_IF_BOND_SLAVE
;
284 else if (strcmp(kind
, "gre") == 0)
285 *zif_type
= ZEBRA_IF_GRE
;
288 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
289 uint32_t ns_id
, const char *name
)
291 struct ifinfomsg
*ifi
;
292 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
293 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
294 struct vrf
*vrf
= NULL
;
295 struct zebra_vrf
*zvrf
;
296 uint32_t nl_table_id
;
300 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
302 if (!linkinfo
[IFLA_INFO_DATA
]) {
303 if (IS_ZEBRA_DEBUG_KERNEL
)
305 "%s: IFLA_INFO_DATA missing from VRF message: %s",
310 netlink_parse_rtattr_nested(attr
, IFLA_VRF_MAX
,
311 linkinfo
[IFLA_INFO_DATA
]);
312 if (!attr
[IFLA_VRF_TABLE
]) {
313 if (IS_ZEBRA_DEBUG_KERNEL
)
315 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
320 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
322 if (h
->nlmsg_type
== RTM_NEWLINK
) {
323 if (IS_ZEBRA_DEBUG_KERNEL
)
324 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
325 ifi
->ifi_index
, nl_table_id
);
327 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
330 exist_id
= vrf_lookup_by_table(nl_table_id
, ns_id
);
331 if (exist_id
!= VRF_DEFAULT
) {
332 vrf
= vrf_lookup_by_id(exist_id
);
335 EC_ZEBRA_VRF_MISCONFIGURED
,
336 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
337 name
, ifi
->ifi_index
, vrf
->name
);
342 vrf
= vrf_update((vrf_id_t
)ifi
->ifi_index
, name
);
344 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
345 name
, ifi
->ifi_index
);
350 * This is the only place that we get the actual kernel table_id
351 * being used. We need it to set the table_id of the routes
352 * we are passing to the kernel.... And to throw some totally
353 * awesome parties. that too.
355 * At this point we *must* have a zvrf because the vrf_create
356 * callback creates one. We *must* set the table id
357 * before the vrf_enable because of( at the very least )
358 * static routes being delayed for installation until
359 * during the vrf_enable callbacks.
361 zvrf
= (struct zebra_vrf
*)vrf
->info
;
362 zvrf
->table_id
= nl_table_id
;
364 /* Enable the created VRF. */
365 if (!vrf_enable(vrf
)) {
366 flog_err(EC_LIB_INTERFACE
,
367 "Failed to enable VRF %s id %u", name
,
372 } else // h->nlmsg_type == RTM_DELLINK
374 if (IS_ZEBRA_DEBUG_KERNEL
)
375 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
378 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
381 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
390 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
393 struct ethtool_cmd ecmd
;
396 const char *ifname
= interface
->name
;
400 /* initialize struct */
401 memset(&ifdata
, 0, sizeof(ifdata
));
403 /* set interface name */
404 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
406 /* initialize ethtool interface */
407 memset(&ecmd
, 0, sizeof(ecmd
));
408 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
409 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
411 /* use ioctl to get speed of an interface */
412 frr_with_privs(&zserv_privs
) {
413 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
414 interface
->vrf
->vrf_id
, NULL
);
416 if (IS_ZEBRA_DEBUG_KERNEL
)
417 zlog_debug("Failure to read interface %s speed: %d %s",
418 ifname
, errno
, safe_strerror(errno
));
419 /* no vrf socket creation may probably mean vrf issue */
424 /* Get the current link state for the interface */
425 rc
= vrf_ioctl(interface
->vrf
->vrf_id
, sd
, SIOCETHTOOL
,
429 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
431 "IOCTL failure to read interface %s speed: %d %s",
432 ifname
, errno
, safe_strerror(errno
));
433 /* no device means interface unreachable */
434 if (errno
== ENODEV
&& error
)
442 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
445 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
447 return get_iflink_speed(ifp
, error
);
451 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
456 struct ifinfomsg ifi
;
461 struct rtattr
*rta_info
, *rta_data
;
462 const struct zebra_l2info_gre
*gre_info
;
464 if (buflen
< sizeof(*req
))
466 memset(req
, 0, sizeof(*req
));
468 req
->n
.nlmsg_type
= RTM_NEWLINK
;
469 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
470 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
472 req
->ifi
.ifi_index
= dplane_ctx_get_ifindex(ctx
);
474 gre_info
= dplane_ctx_gre_get_info(ctx
);
478 req
->ifi
.ifi_change
= 0xFFFFFFFF;
479 link_idx
= dplane_ctx_gre_get_link_ifindex(ctx
);
480 mtu
= dplane_ctx_gre_get_mtu(ctx
);
482 if (mtu
&& !nl_attr_put32(&req
->n
, buflen
, IFLA_MTU
, mtu
))
485 rta_info
= nl_attr_nest(&req
->n
, buflen
, IFLA_LINKINFO
);
489 if (!nl_attr_put(&req
->n
, buflen
, IFLA_INFO_KIND
, "gre", 3))
492 rta_data
= nl_attr_nest(&req
->n
, buflen
, IFLA_INFO_DATA
);
496 if (!nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LINK
, link_idx
))
499 if (gre_info
->vtep_ip
.s_addr
&&
500 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_LOCAL
,
501 gre_info
->vtep_ip
.s_addr
))
504 if (gre_info
->vtep_ip_remote
.s_addr
&&
505 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_REMOTE
,
506 gre_info
->vtep_ip_remote
.s_addr
))
509 if (gre_info
->ikey
&&
510 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
513 if (gre_info
->okey
&&
514 !nl_attr_put32(&req
->n
, buflen
, IFLA_GRE_IKEY
,
518 nl_attr_nest_end(&req
->n
, rta_data
);
519 nl_attr_nest_end(&req
->n
, rta_info
);
521 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
524 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
525 struct zebra_l2info_bridge
*bridge_info
)
527 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
529 memset(bridge_info
, 0, sizeof(*bridge_info
));
530 netlink_parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
531 if (attr
[IFLA_BR_VLAN_FILTERING
])
532 bridge_info
->bridge
.vlan_aware
=
533 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
537 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
538 struct zebra_l2info_vlan
*vlan_info
)
540 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
543 memset(vlan_info
, 0, sizeof(*vlan_info
));
544 netlink_parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
545 if (!attr
[IFLA_VLAN_ID
]) {
546 if (IS_ZEBRA_DEBUG_KERNEL
)
547 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
551 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
552 vlan_info
->vid
= vid_in_msg
;
556 static int netlink_extract_gre_info(struct rtattr
*link_data
,
557 struct zebra_l2info_gre
*gre_info
)
559 struct rtattr
*attr
[IFLA_GRE_MAX
+ 1];
561 memset(gre_info
, 0, sizeof(*gre_info
));
562 memset(attr
, 0, sizeof(attr
));
563 netlink_parse_rtattr_nested(attr
, IFLA_GRE_MAX
, link_data
);
565 if (!attr
[IFLA_GRE_LOCAL
]) {
566 if (IS_ZEBRA_DEBUG_KERNEL
)
568 "IFLA_GRE_LOCAL missing from GRE IF message");
571 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_LOCAL
]);
572 if (!attr
[IFLA_GRE_REMOTE
]) {
573 if (IS_ZEBRA_DEBUG_KERNEL
)
575 "IFLA_GRE_REMOTE missing from GRE IF message");
577 gre_info
->vtep_ip_remote
=
578 *(struct in_addr
*)RTA_DATA(attr
[IFLA_GRE_REMOTE
]);
580 if (!attr
[IFLA_GRE_LINK
]) {
581 if (IS_ZEBRA_DEBUG_KERNEL
)
582 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
584 gre_info
->ifindex_link
=
585 *(ifindex_t
*)RTA_DATA(attr
[IFLA_GRE_LINK
]);
586 if (IS_ZEBRA_DEBUG_KERNEL
)
587 zlog_debug("IFLA_GRE_LINK obtained is %u",
588 gre_info
->ifindex_link
);
590 if (attr
[IFLA_GRE_IKEY
])
591 gre_info
->ikey
= *(uint32_t *)RTA_DATA(attr
[IFLA_GRE_IKEY
]);
592 if (attr
[IFLA_GRE_OKEY
])
593 gre_info
->okey
= *(uint32_t *)RTA_DATA(attr
[IFLA_GRE_OKEY
]);
597 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
598 struct zebra_l2info_vxlan
*vxl_info
)
601 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
603 struct in_addr vtep_ip_in_msg
;
604 ifindex_t ifindex_link
;
606 memset(vxl_info
, 0, sizeof(*vxl_info
));
607 netlink_parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
608 if (attr
[IFLA_VXLAN_COLLECT_METADATA
]) {
609 svd
= *(uint8_t *)RTA_DATA(attr
[IFLA_VXLAN_COLLECT_METADATA
]);
610 if (IS_ZEBRA_DEBUG_KERNEL
)
612 "IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
618 * In case of svd we will not get vni info directly from the
621 if (!attr
[IFLA_VXLAN_ID
]) {
622 if (IS_ZEBRA_DEBUG_KERNEL
)
624 "IFLA_VXLAN_ID missing from VXLAN IF message");
628 vxl_info
->vni_info
.iftype
= ZEBRA_VXLAN_IF_VNI
;
629 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
630 vxl_info
->vni_info
.vni
.vni
= vni_in_msg
;
632 vxl_info
->vni_info
.iftype
= ZEBRA_VXLAN_IF_SVD
;
635 if (!attr
[IFLA_VXLAN_LOCAL
]) {
636 if (IS_ZEBRA_DEBUG_KERNEL
)
638 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
641 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
642 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
645 if (attr
[IFLA_VXLAN_GROUP
]) {
647 vxl_info
->vni_info
.vni
.mcast_grp
=
648 *(struct in_addr
*)RTA_DATA(
649 attr
[IFLA_VXLAN_GROUP
]);
652 if (!attr
[IFLA_VXLAN_LINK
]) {
653 if (IS_ZEBRA_DEBUG_KERNEL
)
654 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
657 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
658 vxl_info
->ifindex_link
= ifindex_link
;
664 * Extract and save L2 params (of interest) for an interface. When a
665 * bridge interface is added or updated, take further actions to map
666 * its members. Likewise, for VxLAN interface.
668 static void netlink_interface_update_l2info(struct interface
*ifp
,
669 struct rtattr
*link_data
, int add
,
675 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
676 struct zebra_l2info_bridge bridge_info
;
678 netlink_extract_bridge_info(link_data
, &bridge_info
);
679 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
680 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
681 struct zebra_l2info_vlan vlan_info
;
683 netlink_extract_vlan_info(link_data
, &vlan_info
);
684 zebra_l2_vlanif_update(ifp
, &vlan_info
);
685 zebra_evpn_acc_bd_svi_set(ifp
->info
, NULL
,
686 !!if_is_operative(ifp
));
687 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
688 struct zebra_l2info_vxlan vxlan_info
;
690 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
691 vxlan_info
.link_nsid
= link_nsid
;
692 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
693 if (link_nsid
!= NS_UNKNOWN
&&
694 vxlan_info
.ifindex_link
)
695 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
697 } else if (IS_ZEBRA_IF_GRE(ifp
)) {
698 struct zebra_l2info_gre gre_info
;
700 netlink_extract_gre_info(link_data
, &gre_info
);
701 gre_info
.link_nsid
= link_nsid
;
702 zebra_l2_greif_add_update(ifp
, &gre_info
, add
);
703 if (link_nsid
!= NS_UNKNOWN
&&
704 gre_info
.ifindex_link
)
705 zebra_if_update_link(ifp
, gre_info
.ifindex_link
,
710 static int netlink_bridge_vxlan_vlan_vni_map_update(struct interface
*ifp
,
711 struct rtattr
*af_spec
)
718 struct zebra_vxlan_vni vni
;
719 struct zebra_vxlan_vni
*vnip
;
720 struct hash
*vni_table
= NULL
;
721 struct zebra_vxlan_vni vni_end
;
722 struct zebra_vxlan_vni vni_start
;
723 struct rtattr
*aftb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+ 1];
725 memset(&vni_start
, 0, sizeof(vni_start
));
726 memset(&vni_end
, 0, sizeof(vni_end
));
728 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
); RTA_OK(i
, rem
);
729 i
= RTA_NEXT(i
, rem
)) {
731 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_TUNNEL_INFO
)
734 memset(aftb
, 0, sizeof(aftb
));
735 netlink_parse_rtattr_nested(aftb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
,
737 if (!aftb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
] ||
738 !aftb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
739 /* vlan-vni info missing */
743 memset(&vni
, 0, sizeof(vni
));
745 vni
.vni
= *(vni_t
*)RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
746 vni
.access_vlan
= *(vlanid_t
*)RTA_DATA(
747 aftb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
749 if (aftb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
750 flags
= *(uint16_t *)RTA_DATA(
751 aftb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
753 if (flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
758 if (flags
& BRIDGE_VLAN_INFO_RANGE_END
)
761 if (!(flags
& BRIDGE_VLAN_INFO_RANGE_END
)) {
766 if (IS_ZEBRA_DEBUG_KERNEL
)
768 "Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
769 vni_start
.access_vlan
, vni_end
.access_vlan
,
770 vni_start
.vni
, vni_end
.vni
, ifp
->name
,
774 vni_table
= zebra_vxlan_vni_table_create();
779 for (vid
= vni_start
.access_vlan
, vni_id
= vni_start
.vni
;
780 vid
<= vni_end
.access_vlan
; vid
++, vni_id
++) {
782 memset(&vni
, 0, sizeof(vni
));
784 vni
.access_vlan
= vid
;
785 vnip
= hash_get(vni_table
, &vni
, zebra_vxlan_vni_alloc
);
790 memset(&vni_start
, 0, sizeof(vni_start
));
791 memset(&vni_end
, 0, sizeof(vni_end
));
795 zebra_vxlan_if_vni_table_add_update(ifp
, vni_table
);
800 static int netlink_bridge_vxlan_update(struct interface
*ifp
,
801 struct rtattr
*af_spec
)
803 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
804 struct bridge_vlan_info
*vinfo
;
805 struct zebra_if
*zif
;
806 vlanid_t access_vlan
;
811 zif
= (struct zebra_if
*)ifp
->info
;
813 /* Single vxlan devices has vni-vlan range to update */
814 if (IS_ZEBRA_VXLAN_IF_SVD(zif
))
815 return netlink_bridge_vxlan_vlan_vni_map_update(ifp
, af_spec
);
817 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
818 * only 1 access VLAN is accepted.
820 netlink_parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, af_spec
);
821 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
824 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
825 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
828 access_vlan
= (vlanid_t
)vinfo
->vid
;
829 if (IS_ZEBRA_DEBUG_KERNEL
)
830 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
831 ifp
->name
, ifp
->ifindex
);
832 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
836 static void netlink_bridge_vlan_update(struct interface
*ifp
,
837 struct rtattr
*af_spec
)
841 uint16_t vid_range_start
= 0;
842 struct zebra_if
*zif
;
843 bitfield_t old_vlan_bitmap
;
844 struct bridge_vlan_info
*vinfo
;
846 zif
= (struct zebra_if
*)ifp
->info
;
848 /* cache the old bitmap addrs */
849 old_vlan_bitmap
= zif
->vlan_bitmap
;
850 /* create a new bitmap space for re-eval */
851 bf_init(zif
->vlan_bitmap
, IF_VLAN_BITMAP_MAX
);
854 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
);
855 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
857 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
862 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
863 vid_range_start
= vinfo
->vid
;
867 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
868 vid_range_start
= vinfo
->vid
;
870 zebra_vlan_bitmap_compute(ifp
, vid_range_start
,
875 zebra_vlan_mbr_re_eval(ifp
, old_vlan_bitmap
);
877 bf_free(old_vlan_bitmap
);
880 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
884 struct ifinfomsg
*ifi
;
885 struct rtattr
*tb
[IFLA_MAX
+ 1];
886 struct interface
*ifp
;
887 struct zebra_if
*zif
;
888 struct rtattr
*af_spec
;
890 /* Fetch name and ifindex */
892 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
894 if (tb
[IFLA_IFNAME
] == NULL
)
896 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
898 /* The interface should already be known, if not discard. */
899 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
901 zlog_debug("Cannot find bridge IF %s(%u)", name
,
906 /* We are only interested in the access VLAN i.e., AF_SPEC */
907 af_spec
= tb
[IFLA_AF_SPEC
];
909 if (IS_ZEBRA_IF_VXLAN(ifp
))
910 return netlink_bridge_vxlan_update(ifp
, af_spec
);
912 /* build vlan bitmap associated with this interface if that
913 * device type is interested in the vlans
915 zif
= (struct zebra_if
*)ifp
->info
;
916 if (bf_is_inited(zif
->vlan_bitmap
))
917 netlink_bridge_vlan_update(ifp
, af_spec
);
922 static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield
)
924 /* This shouldn't be possible */
925 assert(frr_protodown_r_bit
< 32);
926 return (rc_bitfield
== (((uint32_t)1) << frr_protodown_r_bit
));
930 * Process interface protodown dplane update.
932 * If the interface is an es bond member then it must follow EVPN's
935 static void netlink_proc_dplane_if_protodown(struct zebra_if
*zif
,
940 uint32_t rc_bitfield
= 0;
941 struct rtattr
*pd_reason_info
[IFLA_MAX
+ 1];
943 protodown
= !!*(uint8_t *)RTA_DATA(tb
[IFLA_PROTO_DOWN
]);
945 if (tb
[IFLA_PROTO_DOWN_REASON
]) {
946 netlink_parse_rtattr_nested(pd_reason_info
, IFLA_INFO_MAX
,
947 tb
[IFLA_PROTO_DOWN_REASON
]);
949 if (pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
])
950 rc_bitfield
= *(uint32_t *)RTA_DATA(
951 pd_reason_info
[IFLA_PROTO_DOWN_REASON_VALUE
]);
955 * Set our reason code to note it wasn't us.
956 * If the reason we got from the kernel is ONLY frr though, don't
959 COND_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_EXTERNAL
,
960 protodown
&& rc_bitfield
&&
961 !is_if_protodown_reason_only_frr(rc_bitfield
));
964 old_protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
965 if (protodown
== old_protodown
)
968 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
969 zlog_debug("interface %s dplane change, protdown %s",
970 zif
->ifp
->name
, protodown
? "on" : "off");
972 /* Set protodown, respectively */
973 COND_FLAG(zif
->flags
, ZIF_FLAG_PROTODOWN
, protodown
);
975 if (zebra_evpn_is_es_bond_member(zif
->ifp
)) {
976 /* Check it's not already being sent to the dplane first */
978 CHECK_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
)) {
979 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
981 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
987 CHECK_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
)) {
988 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
990 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
995 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_KERNEL
)
997 "bond mbr %s reinstate protodown %s in the dplane",
998 zif
->ifp
->name
, old_protodown
? "on" : "off");
1001 SET_FLAG(zif
->flags
, ZIF_FLAG_SET_PROTODOWN
);
1003 SET_FLAG(zif
->flags
, ZIF_FLAG_UNSET_PROTODOWN
);
1005 dplane_intf_update(zif
->ifp
);
1009 static uint8_t netlink_parse_lacp_bypass(struct rtattr
**linkinfo
)
1012 struct rtattr
*mbrinfo
[IFLA_BOND_SLAVE_MAX
+ 1];
1014 netlink_parse_rtattr_nested(mbrinfo
, IFLA_BOND_SLAVE_MAX
,
1015 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
1016 if (mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
])
1017 bypass
= *(uint8_t *)RTA_DATA(
1018 mbrinfo
[IFLA_BOND_SLAVE_AD_RX_BYPASS
]);
1024 * Only called at startup to cleanup leftover protodown reasons we may
1025 * have not cleaned up. We leave protodown set though.
1027 static void if_sweep_protodown(struct zebra_if
*zif
)
1031 protodown
= !!ZEBRA_IF_IS_PROTODOWN(zif
);
1036 if (IS_ZEBRA_DEBUG_KERNEL
)
1037 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
1038 zif
->ifp
->name
, protodown
? "on" : "off",
1041 /* Only clear our reason codes, leave external if it was set */
1042 UNSET_FLAG(zif
->protodown_rc
, ZEBRA_PROTODOWN_ALL
);
1043 dplane_intf_update(zif
->ifp
);
1047 * Called from interface_lookup_netlink(). This function is only used
1050 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1053 struct ifinfomsg
*ifi
;
1054 struct rtattr
*tb
[IFLA_MAX
+ 1];
1055 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1056 struct interface
*ifp
;
1060 char *slave_kind
= NULL
;
1061 struct zebra_ns
*zns
= NULL
;
1062 vrf_id_t vrf_id
= VRF_DEFAULT
;
1063 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1064 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1065 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1066 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1067 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1068 struct zebra_if
*zif
;
1069 ns_id_t link_nsid
= ns_id
;
1072 frrtrace(3, frr_zebra
, netlink_interface
, h
, ns_id
, startup
);
1074 zns
= zebra_ns_lookup(ns_id
);
1075 ifi
= NLMSG_DATA(h
);
1077 if (h
->nlmsg_type
!= RTM_NEWLINK
)
1080 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1083 "%s: Message received from netlink is of a broken size: %d %zu",
1084 __func__
, h
->nlmsg_len
,
1085 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1089 /* We are interested in some AF_BRIDGE notifications. */
1090 if (ifi
->ifi_family
== AF_BRIDGE
)
1091 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1093 /* Looking up interface name. */
1094 memset(linkinfo
, 0, sizeof(linkinfo
));
1095 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
1098 /* check for wireless messages to ignore */
1099 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1100 if (IS_ZEBRA_DEBUG_KERNEL
)
1101 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1106 if (tb
[IFLA_IFNAME
] == NULL
)
1108 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1110 if (tb
[IFLA_IFALIAS
])
1111 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1113 if (tb
[IFLA_LINKINFO
]) {
1114 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
1117 if (linkinfo
[IFLA_INFO_KIND
])
1118 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1120 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1121 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1123 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
1124 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
1126 netlink_determine_zebra_iftype(kind
, &zif_type
);
1129 /* If VRF, create the VRF structure itself. */
1130 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1131 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1132 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1135 if (tb
[IFLA_MASTER
]) {
1136 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1137 && !vrf_is_backend_netns()) {
1138 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1139 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1140 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
1141 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1143 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1144 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
1145 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1146 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1147 bypass
= netlink_parse_lacp_bypass(linkinfo
);
1149 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1151 if (vrf_is_backend_netns())
1152 vrf_id
= (vrf_id_t
)ns_id
;
1154 /* If linking to another interface, note it. */
1156 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1158 if (tb
[IFLA_LINK_NETNSID
]) {
1159 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1160 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1163 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
1164 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
1166 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1167 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
1169 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
1170 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1172 /* Set zebra interface type */
1173 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1174 if (IS_ZEBRA_IF_VRF(ifp
))
1175 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1178 * Just set the @link/lower-device ifindex. During nldump interfaces are
1179 * not ordered in any fashion so we may end up getting upper devices
1180 * before lower devices. We will setup the real linkage once the dump
1183 zif
= (struct zebra_if
*)ifp
->info
;
1184 zif
->link_ifindex
= link_ifindex
;
1187 XFREE(MTYPE_ZIF_DESC
, zif
->desc
);
1188 zif
->desc
= XSTRDUP(MTYPE_ZIF_DESC
, desc
);
1191 /* Hardware type and address. */
1192 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
1194 netlink_interface_update_hw_addr(tb
, ifp
);
1198 /* Extract and save L2 interface information, take additional actions.
1200 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
1202 if (IS_ZEBRA_IF_BOND(ifp
))
1203 zebra_l2if_update_bond(ifp
, true);
1204 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1205 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
,
1206 ZEBRA_BRIDGE_NO_ACTION
);
1207 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1208 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, !!bypass
);
1210 if (tb
[IFLA_PROTO_DOWN
]) {
1211 netlink_proc_dplane_if_protodown(zif
, tb
);
1212 if_sweep_protodown(zif
);
1218 /* Request for specific interface or address information from the kernel */
1219 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
1220 int type
, uint32_t filter_mask
)
1224 struct ifinfomsg ifm
;
1228 frrtrace(4, frr_zebra
, netlink_request_intf_addr
, netlink_cmd
, family
,
1231 /* Form the request, specifying filter (rtattr) if needed. */
1232 memset(&req
, 0, sizeof(req
));
1233 req
.n
.nlmsg_type
= type
;
1234 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
1235 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1236 req
.ifm
.ifi_family
= family
;
1238 /* Include filter, if specified. */
1240 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
1242 return netlink_request(netlink_cmd
, &req
);
1245 enum netlink_msg_status
1246 netlink_put_gre_set_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1248 enum dplane_op_e op
;
1249 enum netlink_msg_status ret
;
1251 op
= dplane_ctx_get_op(ctx
);
1252 assert(op
== DPLANE_OP_GRE_SET
);
1254 ret
= netlink_batch_add_msg(bth
, ctx
, netlink_gre_set_msg_encoder
, false);
1259 /* Interface lookup by netlink socket. */
1260 int interface_lookup_netlink(struct zebra_ns
*zns
)
1263 struct zebra_dplane_info dp_info
;
1264 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1266 /* Capture key info from ns struct */
1267 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1269 /* Get interface information. */
1270 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
1273 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1278 /* Get interface information - for bridge interfaces. */
1279 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
1280 RTEXT_FILTER_BRVLAN
);
1283 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
1289 * So netlink_tunneldump_read will initiate a request
1290 * per tunnel to get data. If we are on a kernel that
1291 * does not support this then we will get X error messages
1292 * (one per tunnel request )back which netlink_parse_info will
1293 * stop after the first one. So we need to read equivalent
1294 * error messages per tunnel then we can continue.
1295 * if we do not gather all the read failures then
1296 * later requests will not work right.
1298 ret
= netlink_tunneldump_read(zns
);
1302 /* fixup linkages */
1303 zebra_if_update_all_links(zns
);
1308 * interface_addr_lookup_netlink() - Look up interface addresses
1310 * @zns: Zebra netlink socket
1311 * Return: Result status
1313 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
1316 struct zebra_dplane_info dp_info
;
1317 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
1319 /* Capture key info from ns struct */
1320 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
1322 /* Get IPv4 address of the interfaces. */
1323 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
1326 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1331 /* Get IPv6 address of the interfaces. */
1332 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
1335 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
1343 int kernel_interface_set_master(struct interface
*master
,
1344 struct interface
*slave
)
1346 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1350 struct ifinfomsg ifa
;
1351 char buf
[NL_PKT_BUF_SIZE
];
1354 memset(&req
, 0, sizeof(req
));
1356 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1357 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1358 req
.n
.nlmsg_type
= RTM_SETLINK
;
1359 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1361 req
.ifa
.ifi_index
= slave
->ifindex
;
1363 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
1364 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
1366 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1370 /* Interface address modification. */
1371 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
1372 void *buf
, size_t buflen
)
1375 const struct prefix
*p
;
1381 struct ifaddrmsg ifa
;
1385 if (buflen
< sizeof(*req
))
1388 p
= dplane_ctx_get_intf_addr(ctx
);
1389 memset(req
, 0, sizeof(*req
));
1391 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1393 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1394 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1396 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1401 req
->n
.nlmsg_type
= cmd
;
1402 req
->ifa
.ifa_family
= p
->family
;
1404 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1406 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1409 if (p
->family
== AF_INET
) {
1410 if (dplane_ctx_intf_is_connected(ctx
)) {
1411 p
= dplane_ctx_get_intf_dest(ctx
);
1412 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1413 &p
->u
.prefix
, bytelen
))
1415 } else if (cmd
== RTM_NEWADDR
) {
1416 struct in_addr broad
= {
1417 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1420 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1426 /* p is now either address or destination/bcast addr */
1427 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1429 if (dplane_ctx_intf_is_secondary(ctx
))
1430 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1432 if (dplane_ctx_intf_has_label(ctx
)) {
1433 label
= dplane_ctx_get_intf_label(ctx
);
1434 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1439 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1442 enum netlink_msg_status
1443 netlink_put_address_update_msg(struct nl_batch
*bth
,
1444 struct zebra_dplane_ctx
*ctx
)
1446 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1450 static ssize_t
netlink_intf_msg_encoder(struct zebra_dplane_ctx
*ctx
, void *buf
,
1453 enum dplane_op_e op
;
1456 op
= dplane_ctx_get_op(ctx
);
1459 case DPLANE_OP_INTF_UPDATE
:
1462 case DPLANE_OP_INTF_INSTALL
:
1465 case DPLANE_OP_INTF_DELETE
:
1468 case DPLANE_OP_NONE
:
1469 case DPLANE_OP_ROUTE_INSTALL
:
1470 case DPLANE_OP_ROUTE_UPDATE
:
1471 case DPLANE_OP_ROUTE_DELETE
:
1472 case DPLANE_OP_ROUTE_NOTIFY
:
1473 case DPLANE_OP_NH_INSTALL
:
1474 case DPLANE_OP_NH_UPDATE
:
1475 case DPLANE_OP_NH_DELETE
:
1476 case DPLANE_OP_LSP_INSTALL
:
1477 case DPLANE_OP_LSP_DELETE
:
1478 case DPLANE_OP_LSP_NOTIFY
:
1479 case DPLANE_OP_LSP_UPDATE
:
1480 case DPLANE_OP_PW_INSTALL
:
1481 case DPLANE_OP_PW_UNINSTALL
:
1482 case DPLANE_OP_SYS_ROUTE_ADD
:
1483 case DPLANE_OP_SYS_ROUTE_DELETE
:
1484 case DPLANE_OP_ADDR_INSTALL
:
1485 case DPLANE_OP_ADDR_UNINSTALL
:
1486 case DPLANE_OP_MAC_INSTALL
:
1487 case DPLANE_OP_MAC_DELETE
:
1488 case DPLANE_OP_NEIGH_INSTALL
:
1489 case DPLANE_OP_NEIGH_UPDATE
:
1490 case DPLANE_OP_NEIGH_DELETE
:
1491 case DPLANE_OP_NEIGH_DISCOVER
:
1492 case DPLANE_OP_VTEP_ADD
:
1493 case DPLANE_OP_VTEP_DELETE
:
1494 case DPLANE_OP_RULE_ADD
:
1495 case DPLANE_OP_RULE_DELETE
:
1496 case DPLANE_OP_RULE_UPDATE
:
1497 case DPLANE_OP_BR_PORT_UPDATE
:
1498 case DPLANE_OP_IPTABLE_ADD
:
1499 case DPLANE_OP_IPTABLE_DELETE
:
1500 case DPLANE_OP_IPSET_ADD
:
1501 case DPLANE_OP_IPSET_ENTRY_ADD
:
1502 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1503 case DPLANE_OP_IPSET_DELETE
:
1504 case DPLANE_OP_NEIGH_IP_INSTALL
:
1505 case DPLANE_OP_NEIGH_IP_DELETE
:
1506 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1507 case DPLANE_OP_GRE_SET
:
1508 case DPLANE_OP_INTF_ADDR_ADD
:
1509 case DPLANE_OP_INTF_ADDR_DEL
:
1510 case DPLANE_OP_INTF_NETCONFIG
:
1511 case DPLANE_OP_TC_QDISC_INSTALL
:
1512 case DPLANE_OP_TC_QDISC_UNINSTALL
:
1513 case DPLANE_OP_TC_CLASS_ADD
:
1514 case DPLANE_OP_TC_CLASS_DELETE
:
1515 case DPLANE_OP_TC_CLASS_UPDATE
:
1516 case DPLANE_OP_TC_FILTER_ADD
:
1517 case DPLANE_OP_TC_FILTER_DELETE
:
1518 case DPLANE_OP_TC_FILTER_UPDATE
:
1520 EC_ZEBRA_NHG_FIB_UPDATE
,
1521 "Context received for kernel interface update with incorrect OP code (%u)",
1526 return netlink_intf_msg_encode(cmd
, ctx
, buf
, buflen
);
1529 enum netlink_msg_status
1530 netlink_put_intf_update_msg(struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
)
1532 return netlink_batch_add_msg(bth
, ctx
, netlink_intf_msg_encoder
, false);
1535 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1538 struct ifaddrmsg
*ifa
;
1539 struct rtattr
*tb
[IFA_MAX
+ 1];
1540 struct interface
*ifp
;
1545 struct zebra_ns
*zns
;
1546 uint32_t metric
= METRIC_MAX
;
1547 uint32_t kernel_flags
= 0;
1549 frrtrace(3, frr_zebra
, netlink_interface_addr
, h
, ns_id
, startup
);
1551 zns
= zebra_ns_lookup(ns_id
);
1552 ifa
= NLMSG_DATA(h
);
1554 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1556 EC_ZEBRA_UNKNOWN_FAMILY
,
1557 "Invalid address family: %u received from kernel interface addr change: %s",
1558 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1562 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1565 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1568 "%s: Message received from netlink is of a broken size: %d %zu",
1569 __func__
, h
->nlmsg_len
,
1570 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1574 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1576 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1579 /* During startup, failure to lookup the referenced
1580 * interface should not be an error, so we have
1581 * downgraded this condition to warning, and we permit
1582 * the startup interface state retrieval to continue.
1584 flog_warn(EC_LIB_INTERFACE
,
1585 "%s: can't find interface by index %d",
1586 __func__
, ifa
->ifa_index
);
1589 flog_err(EC_LIB_INTERFACE
,
1590 "%s: can't find interface by index %d",
1591 __func__
, ifa
->ifa_index
);
1596 /* Flags passed through */
1598 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1600 kernel_flags
= ifa
->ifa_flags
;
1602 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1605 zlog_debug("%s %s %s flags 0x%x:", __func__
,
1606 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1609 zlog_debug(" IFA_LOCAL %s/%d",
1610 inet_ntop(ifa
->ifa_family
,
1611 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1613 ifa
->ifa_prefixlen
);
1614 if (tb
[IFA_ADDRESS
])
1615 zlog_debug(" IFA_ADDRESS %s/%d",
1616 inet_ntop(ifa
->ifa_family
,
1617 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1619 ifa
->ifa_prefixlen
);
1620 if (tb
[IFA_BROADCAST
])
1621 zlog_debug(" IFA_BROADCAST %s/%d",
1622 inet_ntop(ifa
->ifa_family
,
1623 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1625 ifa
->ifa_prefixlen
);
1626 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1627 zlog_debug(" IFA_LABEL %s",
1628 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1630 if (tb
[IFA_CACHEINFO
]) {
1631 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1632 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1633 ci
->ifa_prefered
, ci
->ifa_valid
);
1637 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1638 if (tb
[IFA_LOCAL
] == NULL
)
1639 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1640 if (tb
[IFA_ADDRESS
] == NULL
)
1641 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1643 /* local interface address */
1644 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1646 /* is there a peer address? */
1648 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1649 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1650 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1651 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1653 /* seeking a broadcast address */
1654 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1657 /* addr is primary key, SOL if we don't have one */
1659 zlog_debug("%s: Local Interface Address is NULL for %s",
1660 __func__
, ifp
->name
);
1665 if (kernel_flags
& IFA_F_SECONDARY
)
1666 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1670 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1672 if (label
&& strcmp(ifp
->name
, label
) == 0)
1675 if (tb
[IFA_RT_PRIORITY
])
1676 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1678 /* Register interface address to the interface. */
1679 if (ifa
->ifa_family
== AF_INET
) {
1680 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1682 "Invalid prefix length: %u received from kernel interface addr change: %s",
1684 nl_msg_type_to_str(h
->nlmsg_type
));
1688 if (h
->nlmsg_type
== RTM_NEWADDR
)
1689 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1691 (struct in_addr
*)broad
, label
,
1693 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1694 /* Delete with a peer address */
1695 connected_delete_ipv4(
1696 ifp
, flags
, (struct in_addr
*)addr
,
1697 ifa
->ifa_prefixlen
, broad
);
1699 connected_delete_ipv4(
1700 ifp
, flags
, (struct in_addr
*)addr
,
1701 ifa
->ifa_prefixlen
, NULL
);
1704 if (ifa
->ifa_family
== AF_INET6
) {
1705 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1707 "Invalid prefix length: %u received from kernel interface addr change: %s",
1709 nl_msg_type_to_str(h
->nlmsg_type
));
1712 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1713 /* Only consider valid addresses; we'll not get a
1715 * the kernel till IPv6 DAD has completed, but at init
1717 * does query for and will receive all addresses.
1720 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1721 connected_add_ipv6(ifp
, flags
,
1722 (struct in6_addr
*)addr
,
1723 (struct in6_addr
*)broad
,
1724 ifa
->ifa_prefixlen
, label
,
1727 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1728 NULL
, ifa
->ifa_prefixlen
);
1732 * Linux kernel does not send route delete on interface down/addr del
1733 * so we have to re-process routes it owns (i.e. kernel routes)
1735 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1736 rib_update(RIB_UPDATE_KERNEL
);
1742 * Parse and validate an incoming interface address change message,
1743 * generating a dplane context object.
1744 * This runs in the dplane pthread; the context is enqueued to the
1745 * main pthread for processing.
1747 int netlink_interface_addr_dplane(struct nlmsghdr
*h
, ns_id_t ns_id
,
1748 int startup
/*ignored*/)
1751 struct ifaddrmsg
*ifa
;
1752 struct rtattr
*tb
[IFA_MAX
+ 1];
1756 uint32_t metric
= METRIC_MAX
;
1757 uint32_t kernel_flags
= 0;
1758 struct zebra_dplane_ctx
*ctx
;
1761 ifa
= NLMSG_DATA(h
);
1763 /* Validate message types */
1764 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1767 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1768 if (IS_ZEBRA_DEBUG_KERNEL
)
1769 zlog_debug("%s: %s: Invalid address family: %u",
1770 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1775 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1777 if (IS_ZEBRA_DEBUG_KERNEL
)
1778 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1779 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1781 (size_t)NLMSG_LENGTH(
1782 sizeof(struct ifaddrmsg
)));
1786 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1788 /* Flags passed through */
1790 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1792 kernel_flags
= ifa
->ifa_flags
;
1794 if (IS_ZEBRA_DEBUG_KERNEL
) { /* remove this line to see initial ifcfg */
1795 char buf
[PREFIX_STRLEN
];
1797 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__
,
1798 nl_msg_type_to_str(h
->nlmsg_type
), ns_id
,
1799 ifa
->ifa_index
, kernel_flags
);
1801 zlog_debug(" IFA_LOCAL %s/%d",
1802 inet_ntop(ifa
->ifa_family
,
1803 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1805 ifa
->ifa_prefixlen
);
1806 if (tb
[IFA_ADDRESS
])
1807 zlog_debug(" IFA_ADDRESS %s/%d",
1808 inet_ntop(ifa
->ifa_family
,
1809 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1811 ifa
->ifa_prefixlen
);
1812 if (tb
[IFA_BROADCAST
])
1813 zlog_debug(" IFA_BROADCAST %s/%d",
1814 inet_ntop(ifa
->ifa_family
,
1815 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1817 ifa
->ifa_prefixlen
);
1819 zlog_debug(" IFA_LABEL %s",
1820 (const char *)RTA_DATA(tb
[IFA_LABEL
]));
1822 if (tb
[IFA_CACHEINFO
]) {
1823 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1825 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1826 ci
->ifa_prefered
, ci
->ifa_valid
);
1830 /* Validate prefix length */
1832 if (ifa
->ifa_family
== AF_INET
1833 && ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1834 if (IS_ZEBRA_DEBUG_KERNEL
)
1835 zlog_debug("%s: %s: Invalid prefix length: %u",
1836 __func__
, nl_msg_type_to_str(h
->nlmsg_type
),
1837 ifa
->ifa_prefixlen
);
1841 if (ifa
->ifa_family
== AF_INET6
) {
1842 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1843 if (IS_ZEBRA_DEBUG_KERNEL
)
1844 zlog_debug("%s: %s: Invalid prefix length: %u",
1846 nl_msg_type_to_str(h
->nlmsg_type
),
1847 ifa
->ifa_prefixlen
);
1851 /* Only consider valid addresses; we'll not get a kernel
1852 * notification till IPv6 DAD has completed, but at init
1853 * time, FRR does query for and will receive all addresses.
1855 if (h
->nlmsg_type
== RTM_NEWADDR
1856 && (kernel_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
))) {
1857 if (IS_ZEBRA_DEBUG_KERNEL
)
1858 zlog_debug("%s: %s: Invalid/tentative addr",
1860 nl_msg_type_to_str(h
->nlmsg_type
));
1865 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1866 if (tb
[IFA_LOCAL
] == NULL
)
1867 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1868 if (tb
[IFA_ADDRESS
] == NULL
)
1869 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1871 /* local interface address */
1872 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1874 /* addr is primary key, SOL if we don't have one */
1876 if (IS_ZEBRA_DEBUG_KERNEL
)
1877 zlog_debug("%s: %s: No local interface address",
1878 __func__
, nl_msg_type_to_str(h
->nlmsg_type
));
1882 /* Allocate a context object, now that validation is done. */
1883 ctx
= dplane_ctx_alloc();
1884 if (h
->nlmsg_type
== RTM_NEWADDR
)
1885 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_ADD
);
1887 dplane_ctx_set_op(ctx
, DPLANE_OP_INTF_ADDR_DEL
);
1889 dplane_ctx_set_ifindex(ctx
, ifa
->ifa_index
);
1890 dplane_ctx_set_ns_id(ctx
, ns_id
);
1892 /* Convert addr to prefix */
1893 memset(&p
, 0, sizeof(p
));
1894 p
.family
= ifa
->ifa_family
;
1895 p
.prefixlen
= ifa
->ifa_prefixlen
;
1896 if (p
.family
== AF_INET
)
1897 p
.u
.prefix4
= *(struct in_addr
*)addr
;
1899 p
.u
.prefix6
= *(struct in6_addr
*)addr
;
1901 dplane_ctx_set_intf_addr(ctx
, &p
);
1903 /* is there a peer address? */
1905 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1906 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1907 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1908 dplane_ctx_intf_set_connected(ctx
);
1909 } else if (tb
[IFA_BROADCAST
]) {
1910 /* seeking a broadcast address */
1911 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
1912 dplane_ctx_intf_set_broadcast(ctx
);
1917 /* Convert addr to prefix */
1918 memset(&p
, 0, sizeof(p
));
1919 p
.family
= ifa
->ifa_family
;
1920 p
.prefixlen
= ifa
->ifa_prefixlen
;
1921 if (p
.family
== AF_INET
)
1922 p
.u
.prefix4
= *(struct in_addr
*)broad
;
1924 p
.u
.prefix6
= *(struct in6_addr
*)broad
;
1926 dplane_ctx_set_intf_dest(ctx
, &p
);
1930 if (kernel_flags
& IFA_F_SECONDARY
)
1931 dplane_ctx_intf_set_secondary(ctx
);
1934 if (tb
[IFA_LABEL
]) {
1935 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1936 dplane_ctx_set_intf_label(ctx
, label
);
1939 if (tb
[IFA_RT_PRIORITY
])
1940 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1942 dplane_ctx_set_intf_metric(ctx
, metric
);
1944 /* Enqueue ctx for main pthread to process */
1945 dplane_provider_enqueue_to_zebra(ctx
);
1950 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1953 struct ifinfomsg
*ifi
;
1954 struct rtattr
*tb
[IFLA_MAX
+ 1];
1955 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1956 struct interface
*ifp
;
1960 char *slave_kind
= NULL
;
1961 struct zebra_ns
*zns
;
1962 vrf_id_t vrf_id
= VRF_DEFAULT
;
1963 enum zebra_iftype zif_type
= ZEBRA_IF_OTHER
;
1964 enum zebra_slave_iftype zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1965 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1966 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1967 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1968 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1969 struct zebra_if
*zif
;
1970 ns_id_t link_nsid
= ns_id
;
1971 ifindex_t master_infindex
= IFINDEX_INTERNAL
;
1974 zns
= zebra_ns_lookup(ns_id
);
1975 ifi
= NLMSG_DATA(h
);
1977 /* assume if not default zns, then new VRF */
1978 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1979 /* If this is not link add/delete message so print warning. */
1980 zlog_debug("%s: wrong kernel message %s", __func__
,
1981 nl_msg_type_to_str(h
->nlmsg_type
));
1985 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1986 || ifi
->ifi_family
== AF_INET6
)) {
1988 EC_ZEBRA_UNKNOWN_FAMILY
,
1989 "Invalid address family: %u received from kernel link change: %s",
1990 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1994 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1997 "%s: Message received from netlink is of a broken size %d %zu",
1998 __func__
, h
->nlmsg_len
,
1999 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
2003 /* We are interested in some AF_BRIDGE notifications. */
2004 if (ifi
->ifi_family
== AF_BRIDGE
)
2005 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
2007 /* Looking up interface name. */
2008 memset(linkinfo
, 0, sizeof(linkinfo
));
2009 netlink_parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
,
2012 /* check for wireless messages to ignore */
2013 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
2014 if (IS_ZEBRA_DEBUG_KERNEL
)
2015 zlog_debug("%s: ignoring IFLA_WIRELESS message",
2020 if (tb
[IFLA_IFNAME
] == NULL
)
2022 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
2024 /* Must be valid string. */
2025 len
= RTA_PAYLOAD(tb
[IFLA_IFNAME
]);
2026 if (len
< 2 || name
[len
- 1] != '\0') {
2027 if (IS_ZEBRA_DEBUG_KERNEL
)
2028 zlog_debug("%s: invalid intf name", __func__
);
2032 if (tb
[IFLA_LINKINFO
]) {
2033 netlink_parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
,
2036 if (linkinfo
[IFLA_INFO_KIND
])
2037 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
2039 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
2040 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
2042 netlink_determine_zebra_iftype(kind
, &zif_type
);
2045 /* If linking to another interface, note it. */
2047 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
2049 if (tb
[IFLA_LINK_NETNSID
]) {
2050 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
2051 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
2053 if (tb
[IFLA_IFALIAS
]) {
2054 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
2057 /* See if interface is present. */
2058 ifp
= if_lookup_by_name_per_ns(zns
, name
);
2060 if (h
->nlmsg_type
== RTM_NEWLINK
) {
2061 /* If VRF, create or update the VRF structure itself. */
2062 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
2063 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2064 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
2067 if (tb
[IFLA_MASTER
]) {
2068 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
2069 && !vrf_is_backend_netns()) {
2070 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
2071 master_infindex
= vrf_id
=
2072 *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
2073 } else if (slave_kind
2074 && (strcmp(slave_kind
, "bridge") == 0)) {
2075 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
2076 master_infindex
= bridge_ifindex
=
2077 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
2078 } else if (slave_kind
2079 && (strcmp(slave_kind
, "bond") == 0)) {
2080 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
2081 master_infindex
= bond_ifindex
=
2082 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
2083 bypass
= netlink_parse_lacp_bypass(linkinfo
);
2085 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
2087 if (vrf_is_backend_netns())
2088 vrf_id
= (vrf_id_t
)ns_id
;
2090 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
2091 /* Add interface notification from kernel */
2092 if (IS_ZEBRA_DEBUG_KERNEL
)
2094 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
2095 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
2096 zif_slave_type
, master_infindex
,
2100 /* unknown interface */
2101 ifp
= if_get_by_name(name
, vrf_id
, NULL
);
2103 /* pre-configured interface, learnt now */
2104 if (ifp
->vrf
->vrf_id
!= vrf_id
)
2105 if_update_to_new_vrf(ifp
, vrf_id
);
2108 /* Update interface information. */
2109 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2110 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2111 if (!tb
[IFLA_MTU
]) {
2113 "RTM_NEWLINK for interface %s(%u) without MTU set",
2114 name
, ifi
->ifi_index
);
2117 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2119 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
2121 /* Set interface type */
2122 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2123 if (IS_ZEBRA_IF_VRF(ifp
))
2124 SET_FLAG(ifp
->status
,
2125 ZEBRA_INTERFACE_VRF_LOOPBACK
);
2128 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2131 netlink_to_zebra_link_type(ifi
->ifi_type
);
2132 netlink_interface_update_hw_addr(tb
, ifp
);
2134 /* Inform clients, install any configured addresses. */
2137 /* Extract and save L2 interface information, take
2138 * additional actions. */
2139 netlink_interface_update_l2info(
2140 ifp
, linkinfo
[IFLA_INFO_DATA
],
2142 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
2143 zebra_l2if_update_bridge_slave(
2144 ifp
, bridge_ifindex
, ns_id
,
2145 ZEBRA_BRIDGE_NO_ACTION
);
2146 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2147 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2150 if (tb
[IFLA_PROTO_DOWN
])
2151 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2152 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
2154 if (IS_ZEBRA_DEBUG_KERNEL
)
2156 "RTM_NEWLINK ADD for %s(%u), vlan-aware %d",
2158 IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2161 } else if (ifp
->vrf
->vrf_id
!= vrf_id
) {
2162 /* VRF change for an interface. */
2163 if (IS_ZEBRA_DEBUG_KERNEL
)
2165 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
2166 name
, ifp
->ifindex
, ifp
->vrf
->vrf_id
,
2167 vrf_id
, ifi
->ifi_flags
);
2169 if_handle_vrf_change(ifp
, vrf_id
);
2171 bool was_bridge_slave
, was_bond_slave
;
2172 uint8_t chgflags
= ZEBRA_BRIDGE_NO_ACTION
;
2175 /* Interface update. */
2176 if (IS_ZEBRA_DEBUG_KERNEL
)
2178 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
2179 name
, ifp
->ifindex
, zif_slave_type
,
2180 master_infindex
, ifi
->ifi_flags
);
2182 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
2183 if (!tb
[IFLA_MTU
]) {
2185 "RTM_NEWLINK for interface %s(%u) without MTU set",
2186 name
, ifi
->ifi_index
);
2189 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
2192 /* Update interface type - NOTE: Only slave_type can
2194 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
2195 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
2196 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
2198 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
2201 zebra_if_update_link(ifp
, link_ifindex
, link_nsid
);
2204 netlink_to_zebra_link_type(ifi
->ifi_type
);
2205 netlink_interface_update_hw_addr(tb
, ifp
);
2207 if (tb
[IFLA_PROTO_DOWN
])
2208 netlink_proc_dplane_if_protodown(ifp
->info
, tb
);
2210 if (if_is_no_ptm_operative(ifp
)) {
2211 bool is_up
= if_is_operative(ifp
);
2212 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2213 if (!if_is_no_ptm_operative(ifp
) ||
2214 CHECK_FLAG(zif
->flags
,
2215 ZIF_FLAG_PROTODOWN
)) {
2216 if (IS_ZEBRA_DEBUG_KERNEL
)
2218 "Intf %s(%u) has gone DOWN",
2219 name
, ifp
->ifindex
);
2221 rib_update(RIB_UPDATE_KERNEL
);
2222 } else if (if_is_operative(ifp
)) {
2223 bool mac_updated
= false;
2225 /* Must notify client daemons of new
2226 * interface status. */
2227 if (IS_ZEBRA_DEBUG_KERNEL
)
2229 "Intf %s(%u) PTM up, notifying clients",
2230 name
, ifp
->ifindex
);
2233 /* Update EVPN VNI when SVI MAC change
2235 if (memcmp(old_hw_addr
, ifp
->hw_addr
,
2236 INTERFACE_HWADDR_MAX
))
2238 if (IS_ZEBRA_IF_VLAN(ifp
)
2240 struct interface
*link_if
;
2243 if_lookup_by_index_per_ns(
2244 zebra_ns_lookup(NS_DEFAULT
),
2247 zebra_vxlan_svi_up(ifp
,
2249 } else if (mac_updated
2250 && IS_ZEBRA_IF_BRIDGE(ifp
)) {
2252 "Intf %s(%u) bridge changed MAC address",
2253 name
, ifp
->ifindex
);
2255 ZEBRA_BRIDGE_MASTER_MAC_CHANGE
;
2259 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
2260 if (if_is_operative(ifp
) &&
2261 !CHECK_FLAG(zif
->flags
,
2262 ZIF_FLAG_PROTODOWN
)) {
2263 if (IS_ZEBRA_DEBUG_KERNEL
)
2265 "Intf %s(%u) has come UP",
2266 name
, ifp
->ifindex
);
2268 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2270 ZEBRA_BRIDGE_MASTER_UP
;
2272 if (IS_ZEBRA_DEBUG_KERNEL
)
2274 "Intf %s(%u) has gone DOWN",
2275 name
, ifp
->ifindex
);
2277 rib_update(RIB_UPDATE_KERNEL
);
2281 /* Extract and save L2 interface information, take
2282 * additional actions. */
2283 netlink_interface_update_l2info(
2284 ifp
, linkinfo
[IFLA_INFO_DATA
],
2286 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2287 zebra_l2if_update_bridge(ifp
, chgflags
);
2288 if (IS_ZEBRA_IF_BOND(ifp
))
2289 zebra_l2if_update_bond(ifp
, true);
2290 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
2291 zebra_l2if_update_bridge_slave(
2292 ifp
, bridge_ifindex
, ns_id
, chgflags
);
2293 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
2294 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
,
2296 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
2297 if (IS_ZEBRA_DEBUG_KERNEL
)
2299 "RTM_NEWLINK update for %s(%u), vlan-aware %d",
2301 IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(
2308 XFREE(MTYPE_ZIF_DESC
, zif
->desc
);
2310 zif
->desc
= XSTRDUP(MTYPE_ZIF_DESC
, desc
);
2313 /* Delete interface notification from kernel */
2315 if (IS_ZEBRA_DEBUG_KERNEL
)
2317 "RTM_DELLINK for unknown interface %s(%u)",
2318 name
, ifi
->ifi_index
);
2322 if (IS_ZEBRA_DEBUG_KERNEL
)
2323 zlog_debug("RTM_DELLINK for %s(%u)", name
,
2326 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
2328 if (IS_ZEBRA_IF_BOND(ifp
))
2329 zebra_l2if_update_bond(ifp
, false);
2330 if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
2331 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
, false);
2332 /* Special handling for bridge or VxLAN interfaces. */
2333 if (IS_ZEBRA_IF_BRIDGE(ifp
))
2334 zebra_l2_bridge_del(ifp
);
2335 else if (IS_ZEBRA_IF_VXLAN(ifp
))
2336 zebra_l2_vxlanif_del(ifp
);
2338 if_delete_update(&ifp
);
2340 /* If VRF, delete the VRF structure itself. */
2341 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns())
2342 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
2349 * Interface encoding helper function.
2351 * \param[in] cmd netlink command.
2352 * \param[in] ctx dataplane context (information snapshot).
2353 * \param[out] buf buffer to hold the packet.
2354 * \param[in] buflen amount of buffer bytes.
2357 ssize_t
netlink_intf_msg_encode(uint16_t cmd
,
2358 const struct zebra_dplane_ctx
*ctx
, void *buf
,
2363 struct ifinfomsg ifa
;
2367 struct rtattr
*nest_protodown_reason
;
2368 ifindex_t ifindex
= dplane_ctx_get_ifindex(ctx
);
2369 bool down
= dplane_ctx_intf_is_protodown(ctx
);
2370 bool pd_reason_val
= dplane_ctx_get_intf_pd_reason_val(ctx
);
2372 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
2374 if (buflen
< sizeof(*req
))
2377 memset(req
, 0, sizeof(*req
));
2379 if (cmd
!= RTM_SETLINK
)
2381 EC_ZEBRA_INTF_UPDATE_FAILURE
,
2382 "Only RTM_SETLINK message type currently supported in dplane pthread");
2384 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
2385 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
2386 req
->n
.nlmsg_type
= cmd
;
2387 req
->n
.nlmsg_pid
= nl
->snl
.nl_pid
;
2389 req
->ifa
.ifi_index
= ifindex
;
2391 nl_attr_put8(&req
->n
, buflen
, IFLA_PROTO_DOWN
, down
);
2392 nl_attr_put32(&req
->n
, buflen
, IFLA_LINK
, ifindex
);
2394 /* Reason info nest */
2395 nest_protodown_reason
=
2396 nl_attr_nest(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON
);
2398 if (!nest_protodown_reason
)
2401 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_MASK
,
2402 (1 << frr_protodown_r_bit
));
2403 nl_attr_put32(&req
->n
, buflen
, IFLA_PROTO_DOWN_REASON_VALUE
,
2404 ((int)pd_reason_val
) << frr_protodown_r_bit
);
2406 nl_attr_nest_end(&req
->n
, nest_protodown_reason
);
2408 if (IS_ZEBRA_DEBUG_KERNEL
)
2409 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2410 __func__
, nl_msg_type_to_str(cmd
), down
,
2411 pd_reason_val
, ifindex
);
2413 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
2416 /* Interface information read by netlink. */
2417 void interface_list(struct zebra_ns
*zns
)
2419 interface_lookup_netlink(zns
);
2420 /* We add routes for interface address,
2421 * so we need to get the nexthop info
2422 * from the kernel before we can do that
2424 netlink_nexthop_read(zns
);
2426 interface_addr_lookup_netlink(zns
);
2429 void if_netlink_set_frr_protodown_r_bit(uint8_t bit
)
2431 if (IS_ZEBRA_DEBUG_KERNEL
)
2433 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2434 frr_protodown_r_bit
, bit
);
2436 frr_protodown_r_bit
= bit
;
2439 void if_netlink_unset_frr_protodown_r_bit(void)
2441 if (IS_ZEBRA_DEBUG_KERNEL
)
2443 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2444 frr_protodown_r_bit
, FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2446 frr_protodown_r_bit
= FRR_PROTODOWN_REASON_DEFAULT_BIT
;
2450 bool if_netlink_frr_protodown_r_bit_is_set(void)
2452 return (frr_protodown_r_bit
!= FRR_PROTODOWN_REASON_DEFAULT_BIT
);
2455 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2457 return frr_protodown_r_bit
;
2461 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2463 * @zns: Zebra namespace
2464 * @family: AF_* netlink family
2465 * @type: RTM_* (RTM_GETTUNNEL) route type
2467 * Return: Result status
2469 static int netlink_request_tunneldump(struct zebra_ns
*zns
, int family
,
2474 struct tunnel_msg tmsg
;
2478 /* Form the request */
2479 memset(&req
, 0, sizeof(req
));
2480 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tunnel_msg
));
2481 req
.n
.nlmsg_type
= RTM_GETTUNNEL
;
2482 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2483 req
.tmsg
.family
= family
;
2484 req
.tmsg
.ifindex
= ifindex
;
2486 return netlink_request(&zns
->netlink_cmd
, &req
);
2490 * Currently we only ask for vxlan l3svd vni information.
2491 * In the future this can be expanded.
2493 int netlink_tunneldump_read(struct zebra_ns
*zns
)
2496 struct zebra_dplane_info dp_info
;
2497 struct route_node
*rn
;
2498 struct interface
*tmp_if
= NULL
;
2499 struct zebra_if
*zif
;
2500 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
2502 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2504 for (rn
= route_top(zns
->if_table
); rn
; rn
= route_next(rn
)) {
2505 tmp_if
= (struct interface
*)rn
->info
;
2509 if (!zif
|| zif
->zif_type
!= ZEBRA_IF_VXLAN
)
2512 ret
= netlink_request_tunneldump(zns
, PF_BRIDGE
,
2517 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
,
2527 static const char *port_state2str(uint8_t state
)
2530 case BR_STATE_DISABLED
:
2532 case BR_STATE_LISTENING
:
2534 case BR_STATE_LEARNING
:
2536 case BR_STATE_FORWARDING
:
2537 return "FORWARDING";
2538 case BR_STATE_BLOCKING
:
2545 static void vxlan_vni_state_change(struct zebra_if
*zif
, uint16_t id
,
2548 struct zebra_vxlan_vni
*vnip
;
2550 vnip
= zebra_vxlan_if_vlanid_vni_find(zif
, id
);
2553 if (IS_ZEBRA_DEBUG_VXLAN
)
2555 "Cannot find VNI for VID (%u) IF %s for vlan state update",
2556 id
, zif
->ifp
->name
);
2562 case BR_STATE_FORWARDING
:
2563 zebra_vxlan_if_vni_up(zif
->ifp
, vnip
);
2565 case BR_STATE_BLOCKING
:
2566 zebra_vxlan_if_vni_down(zif
->ifp
, vnip
);
2568 case BR_STATE_DISABLED
:
2569 case BR_STATE_LISTENING
:
2570 case BR_STATE_LEARNING
:
2572 /* Not used for anything at the moment */
2577 static void vlan_id_range_state_change(struct interface
*ifp
, uint16_t id_start
,
2578 uint16_t id_end
, uint8_t state
)
2580 struct zebra_if
*zif
;
2582 zif
= (struct zebra_if
*)ifp
->info
;
2587 for (uint16_t i
= id_start
; i
<= id_end
; i
++)
2588 vxlan_vni_state_change(zif
, i
, state
);
2592 * netlink_vlan_change() - Read in change about vlans from the kernel
2594 * @h: Netlink message header
2595 * @ns_id: Namspace id
2596 * @startup: Are we reading under startup conditions?
2598 * Return: Result status
2600 int netlink_vlan_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
2603 struct interface
*ifp
;
2604 struct br_vlan_msg
*bvm
;
2605 struct bridge_vlan_info
*vinfo
;
2606 struct rtattr
*vtb
[BRIDGE_VLANDB_ENTRY_MAX
+ 1] = {};
2607 struct rtattr
*attr
;
2612 /* We only care about state changes for now */
2613 if (!(h
->nlmsg_type
== RTM_NEWVLAN
))
2616 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct br_vlan_msg
));
2619 "%s: Message received from netlink is of a broken size %d %zu",
2620 __func__
, h
->nlmsg_len
,
2621 (size_t)NLMSG_LENGTH(sizeof(struct br_vlan_msg
)));
2625 bvm
= NLMSG_DATA(h
);
2627 if (bvm
->family
!= AF_BRIDGE
)
2630 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), bvm
->ifindex
);
2632 zlog_debug("Cannot find bridge-vlan IF (%u) for vlan update",
2637 if (!IS_ZEBRA_IF_VXLAN(ifp
)) {
2638 if (IS_ZEBRA_DEBUG_KERNEL
)
2639 zlog_debug("Ignoring non-vxlan IF (%s) for vlan update",
2645 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_VXLAN
)
2646 zlog_debug("%s %s IF %s NS %u",
2647 nl_msg_type_to_str(h
->nlmsg_type
),
2648 nl_family_to_str(bvm
->family
), ifp
->name
, ns_id
);
2650 /* Loop over "ALL" BRIDGE_VLANDB_ENTRY */
2652 for (attr
= BRVLAN_RTA(bvm
); RTA_OK(attr
, rem
);
2653 attr
= RTA_NEXT(attr
, rem
)) {
2657 type
= attr
->rta_type
& NLA_TYPE_MASK
;
2659 if (type
!= BRIDGE_VLANDB_ENTRY
)
2662 /* Parse nested entry data */
2663 netlink_parse_rtattr_nested(vtb
, BRIDGE_VLANDB_ENTRY_MAX
, attr
);
2665 /* It must have info for the ID */
2666 if (!vtb
[BRIDGE_VLANDB_ENTRY_INFO
])
2669 vinfo
= (struct bridge_vlan_info
*)RTA_DATA(
2670 vtb
[BRIDGE_VLANDB_ENTRY_INFO
]);
2673 * We only care about state info, if there is none, just ignore
2676 if (!vtb
[BRIDGE_VLANDB_ENTRY_STATE
])
2679 state
= *(uint8_t *)RTA_DATA(vtb
[BRIDGE_VLANDB_ENTRY_STATE
]);
2681 if (vtb
[BRIDGE_VLANDB_ENTRY_RANGE
])
2682 vrange
= *(uint32_t *)RTA_DATA(
2683 vtb
[BRIDGE_VLANDB_ENTRY_RANGE
]);
2685 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_VXLAN
) {
2687 zlog_debug("VLANDB_ENTRY: VID (%u-%u) state=%s",
2689 port_state2str(state
));
2691 zlog_debug("VLANDB_ENTRY: VID (%u) state=%s",
2692 vinfo
->vid
, port_state2str(state
));
2695 vlan_id_range_state_change(
2696 ifp
, vinfo
->vid
, (vrange
? vrange
: vinfo
->vid
), state
);
2703 * netlink_request_vlan() - Request vlan information from the kernel
2704 * @zns: Zebra namespace
2705 * @family: AF_* netlink family
2708 * Return: Result status
2710 static int netlink_request_vlan(struct zebra_ns
*zns
, int family
, int type
)
2714 struct br_vlan_msg bvm
;
2718 /* Form the request, specifying filter (rtattr) if needed. */
2719 memset(&req
, 0, sizeof(req
));
2720 req
.n
.nlmsg_type
= type
;
2721 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
2722 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_vlan_msg
));
2723 req
.bvm
.family
= family
;
2725 nl_attr_put32(&req
.n
, sizeof(req
), BRIDGE_VLANDB_DUMP_FLAGS
,
2726 BRIDGE_VLANDB_DUMPF_STATS
);
2728 return netlink_request(&zns
->netlink_cmd
, &req
);
2732 * netlink_vlan_read() - Vlan read function using netlink interface
2734 * @zns: Zebra name space
2736 * Return: Result status
2737 * Only called at bootstrap time.
2739 int netlink_vlan_read(struct zebra_ns
*zns
)
2742 struct zebra_dplane_info dp_info
;
2744 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
2746 /* Get bridg vlan info */
2747 ret
= netlink_request_vlan(zns
, PF_BRIDGE
, RTM_GETVLAN
);
2751 ret
= netlink_parse_info(netlink_vlan_change
, &zns
->netlink_cmd
,
2757 #endif /* GNU_LINUX */