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
33 #include <netinet/if_ether.h>
34 #include <linux/if_bridge.h>
35 #include <linux/if_link.h>
36 #include <net/if_arp.h>
37 #include <linux/sockios.h>
38 #include <linux/ethtool.h>
44 #include "connected.h"
47 #include "zebra_memory.h"
55 #include "lib_errors.h"
58 #include "zebra/zserv.h"
59 #include "zebra/zebra_ns.h"
60 #include "zebra/zebra_vrf.h"
62 #include "zebra/redistribute.h"
63 #include "zebra/interface.h"
64 #include "zebra/debug.h"
65 #include "zebra/rtadv.h"
66 #include "zebra/zebra_ptm.h"
67 #include "zebra/zebra_mpls.h"
68 #include "zebra/kernel_netlink.h"
69 #include "zebra/rt_netlink.h"
70 #include "zebra/if_netlink.h"
71 #include "zebra/zebra_errors.h"
72 #include "zebra/zebra_vxlan.h"
73 #include "zebra/zebra_evpn_mh.h"
75 extern struct zebra_privs_t zserv_privs
;
77 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
78 names and ifindex values. */
79 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
82 struct interface
*oifp
;
84 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
86 if (ifi_index
== IFINDEX_INTERNAL
)
89 "Netlink is setting interface %s ifindex to reserved internal value %u",
90 ifp
->name
, ifi_index
);
92 if (IS_ZEBRA_DEBUG_KERNEL
)
94 "interface index %d was renamed from %s to %s",
95 ifi_index
, oifp
->name
, ifp
->name
);
99 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
100 ifi_index
, oifp
->name
, ifp
->name
);
101 if_delete_update(oifp
);
104 if_set_index(ifp
, ifi_index
);
107 /* Utility function to parse hardware link-layer address and update ifp */
108 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
109 struct interface
*ifp
)
113 if (tb
[IFLA_ADDRESS
]) {
116 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
118 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
119 zlog_debug("Hardware address is too large: %d",
122 ifp
->hw_addr_len
= hw_addr_len
;
123 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
126 for (i
= 0; i
< hw_addr_len
; i
++)
127 if (ifp
->hw_addr
[i
] != 0)
130 if (i
== hw_addr_len
)
131 ifp
->hw_addr_len
= 0;
133 ifp
->hw_addr_len
= hw_addr_len
;
138 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
142 return ZEBRA_LLT_ETHER
;
144 return ZEBRA_LLT_EETHER
;
146 return ZEBRA_LLT_AX25
;
148 return ZEBRA_LLT_PRONET
;
150 return ZEBRA_LLT_IEEE802
;
152 return ZEBRA_LLT_ARCNET
;
153 case ARPHRD_APPLETLK
:
154 return ZEBRA_LLT_APPLETLK
;
156 return ZEBRA_LLT_DLCI
;
158 return ZEBRA_LLT_ATM
;
159 case ARPHRD_METRICOM
:
160 return ZEBRA_LLT_METRICOM
;
161 case ARPHRD_IEEE1394
:
162 return ZEBRA_LLT_IEEE1394
;
164 return ZEBRA_LLT_EUI64
;
165 case ARPHRD_INFINIBAND
:
166 return ZEBRA_LLT_INFINIBAND
;
168 return ZEBRA_LLT_SLIP
;
170 return ZEBRA_LLT_CSLIP
;
172 return ZEBRA_LLT_SLIP6
;
174 return ZEBRA_LLT_CSLIP6
;
176 return ZEBRA_LLT_RSRVD
;
178 return ZEBRA_LLT_ADAPT
;
180 return ZEBRA_LLT_ROSE
;
182 return ZEBRA_LLT_X25
;
184 return ZEBRA_LLT_PPP
;
186 return ZEBRA_LLT_CHDLC
;
188 return ZEBRA_LLT_LAPB
;
190 return ZEBRA_LLT_RAWHDLC
;
192 return ZEBRA_LLT_IPIP
;
194 return ZEBRA_LLT_IPIP6
;
196 return ZEBRA_LLT_FRAD
;
198 return ZEBRA_LLT_SKIP
;
199 case ARPHRD_LOOPBACK
:
200 return ZEBRA_LLT_LOOPBACK
;
201 case ARPHRD_LOCALTLK
:
202 return ZEBRA_LLT_LOCALTLK
;
204 return ZEBRA_LLT_FDDI
;
206 return ZEBRA_LLT_SIT
;
208 return ZEBRA_LLT_IPDDP
;
210 return ZEBRA_LLT_IPGRE
;
212 return ZEBRA_LLT_PIMREG
;
214 return ZEBRA_LLT_HIPPI
;
216 return ZEBRA_LLT_ECONET
;
218 return ZEBRA_LLT_IRDA
;
220 return ZEBRA_LLT_FCPP
;
222 return ZEBRA_LLT_FCAL
;
224 return ZEBRA_LLT_FCPL
;
225 case ARPHRD_FCFABRIC
:
226 return ZEBRA_LLT_FCFABRIC
;
227 case ARPHRD_IEEE802_TR
:
228 return ZEBRA_LLT_IEEE802_TR
;
229 case ARPHRD_IEEE80211
:
230 return ZEBRA_LLT_IEEE80211
;
231 #ifdef ARPHRD_IEEE802154
232 case ARPHRD_IEEE802154
:
233 return ZEBRA_LLT_IEEE802154
;
237 return ZEBRA_LLT_IP6GRE
;
239 #ifdef ARPHRD_IEEE802154_PHY
240 case ARPHRD_IEEE802154_PHY
:
241 return ZEBRA_LLT_IEEE802154_PHY
;
245 return ZEBRA_LLT_UNKNOWN
;
249 static inline void zebra_if_set_ziftype(struct interface
*ifp
,
250 zebra_iftype_t zif_type
,
251 zebra_slave_iftype_t zif_slave_type
)
253 struct zebra_if
*zif
;
255 zif
= (struct zebra_if
*)ifp
->info
;
256 zif
->zif_slave_type
= zif_slave_type
;
258 if (zif
->zif_type
!= zif_type
) {
259 zif
->zif_type
= zif_type
;
260 /* If the if_type has been set to bond initialize ES info
261 * against it. XXX - note that we don't handle the case where
262 * a zif changes from bond to non-bond; it is really
263 * an unexpected/error condition.
265 zebra_evpn_if_init(zif
);
269 static void netlink_determine_zebra_iftype(const char *kind
,
270 zebra_iftype_t
*zif_type
)
272 *zif_type
= ZEBRA_IF_OTHER
;
277 if (strcmp(kind
, "vrf") == 0)
278 *zif_type
= ZEBRA_IF_VRF
;
279 else if (strcmp(kind
, "bridge") == 0)
280 *zif_type
= ZEBRA_IF_BRIDGE
;
281 else if (strcmp(kind
, "vlan") == 0)
282 *zif_type
= ZEBRA_IF_VLAN
;
283 else if (strcmp(kind
, "vxlan") == 0)
284 *zif_type
= ZEBRA_IF_VXLAN
;
285 else if (strcmp(kind
, "macvlan") == 0)
286 *zif_type
= ZEBRA_IF_MACVLAN
;
287 else if (strcmp(kind
, "veth") == 0)
288 *zif_type
= ZEBRA_IF_VETH
;
289 else if (strcmp(kind
, "bond") == 0)
290 *zif_type
= ZEBRA_IF_BOND
;
291 else if (strcmp(kind
, "bond_slave") == 0)
292 *zif_type
= ZEBRA_IF_BOND_SLAVE
;
295 #define parse_rtattr_nested(tb, max, rta) \
296 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
298 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
299 uint32_t ns_id
, const char *name
)
301 struct ifinfomsg
*ifi
;
302 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
303 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
305 struct zebra_vrf
*zvrf
;
306 uint32_t nl_table_id
;
310 memset(linkinfo
, 0, sizeof(linkinfo
));
311 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
313 if (!linkinfo
[IFLA_INFO_DATA
]) {
314 if (IS_ZEBRA_DEBUG_KERNEL
)
316 "%s: IFLA_INFO_DATA missing from VRF message: %s",
321 memset(attr
, 0, sizeof(attr
));
322 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
323 if (!attr
[IFLA_VRF_TABLE
]) {
324 if (IS_ZEBRA_DEBUG_KERNEL
)
326 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
331 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
333 if (h
->nlmsg_type
== RTM_NEWLINK
) {
334 if (IS_ZEBRA_DEBUG_KERNEL
)
335 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
336 ifi
->ifi_index
, nl_table_id
);
338 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
341 exist_id
= vrf_lookup_by_table(nl_table_id
, ns_id
);
342 if (exist_id
!= VRF_DEFAULT
) {
343 vrf
= vrf_lookup_by_id(exist_id
);
346 EC_ZEBRA_VRF_MISCONFIGURED
,
347 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
348 name
, ifi
->ifi_index
, vrf
->name
);
354 * vrf_get is implied creation if it does not exist
356 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
357 name
); // It would create vrf
359 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
360 name
, ifi
->ifi_index
);
365 * This is the only place that we get the actual kernel table_id
366 * being used. We need it to set the table_id of the routes
367 * we are passing to the kernel.... And to throw some totally
368 * awesome parties. that too.
370 * At this point we *must* have a zvrf because the vrf_create
371 * callback creates one. We *must* set the table id
372 * before the vrf_enable because of( at the very least )
373 * static routes being delayed for installation until
374 * during the vrf_enable callbacks.
376 zvrf
= (struct zebra_vrf
*)vrf
->info
;
377 zvrf
->table_id
= nl_table_id
;
379 /* Enable the created VRF. */
380 if (!vrf_enable(vrf
)) {
381 flog_err(EC_LIB_INTERFACE
,
382 "Failed to enable VRF %s id %u", name
,
387 } else // h->nlmsg_type == RTM_DELLINK
389 if (IS_ZEBRA_DEBUG_KERNEL
)
390 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
393 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
396 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
405 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
408 struct ethtool_cmd ecmd
;
411 const char *ifname
= interface
->name
;
415 /* initialize struct */
416 memset(&ifdata
, 0, sizeof(ifdata
));
418 /* set interface name */
419 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
421 /* initialize ethtool interface */
422 memset(&ecmd
, 0, sizeof(ecmd
));
423 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
424 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
426 /* use ioctl to get IP address of an interface */
427 frr_with_privs(&zserv_privs
) {
428 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
432 if (IS_ZEBRA_DEBUG_KERNEL
)
433 zlog_debug("Failure to read interface %s speed: %d %s",
434 ifname
, errno
, safe_strerror(errno
));
435 /* no vrf socket creation may probably mean vrf issue */
440 /* Get the current link state for the interface */
441 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
,
445 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
447 "IOCTL failure to read interface %s speed: %d %s",
448 ifname
, errno
, safe_strerror(errno
));
449 /* no device means interface unreachable */
450 if (errno
== ENODEV
&& error
)
458 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
461 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
463 return get_iflink_speed(ifp
, error
);
466 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
467 struct zebra_l2info_bridge
*bridge_info
)
469 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
471 memset(bridge_info
, 0, sizeof(*bridge_info
));
472 memset(attr
, 0, sizeof(attr
));
473 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
474 if (attr
[IFLA_BR_VLAN_FILTERING
])
475 bridge_info
->vlan_aware
=
476 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
480 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
481 struct zebra_l2info_vlan
*vlan_info
)
483 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
486 memset(vlan_info
, 0, sizeof(*vlan_info
));
487 memset(attr
, 0, sizeof(attr
));
488 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
489 if (!attr
[IFLA_VLAN_ID
]) {
490 if (IS_ZEBRA_DEBUG_KERNEL
)
491 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
495 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
496 vlan_info
->vid
= vid_in_msg
;
500 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
501 struct zebra_l2info_vxlan
*vxl_info
)
503 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
505 struct in_addr vtep_ip_in_msg
;
506 ifindex_t ifindex_link
;
508 memset(vxl_info
, 0, sizeof(*vxl_info
));
509 memset(attr
, 0, sizeof(attr
));
510 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
511 if (!attr
[IFLA_VXLAN_ID
]) {
512 if (IS_ZEBRA_DEBUG_KERNEL
)
514 "IFLA_VXLAN_ID missing from VXLAN IF message");
518 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
519 vxl_info
->vni
= vni_in_msg
;
520 if (!attr
[IFLA_VXLAN_LOCAL
]) {
521 if (IS_ZEBRA_DEBUG_KERNEL
)
523 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
526 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
527 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
530 if (attr
[IFLA_VXLAN_GROUP
]) {
531 vxl_info
->mcast_grp
=
532 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_GROUP
]);
535 if (!attr
[IFLA_VXLAN_LINK
]) {
536 if (IS_ZEBRA_DEBUG_KERNEL
)
537 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
540 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
541 vxl_info
->ifindex_link
= ifindex_link
;
547 * Extract and save L2 params (of interest) for an interface. When a
548 * bridge interface is added or updated, take further actions to map
549 * its members. Likewise, for VxLAN interface.
551 static void netlink_interface_update_l2info(struct interface
*ifp
,
552 struct rtattr
*link_data
, int add
,
558 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
559 struct zebra_l2info_bridge bridge_info
;
561 netlink_extract_bridge_info(link_data
, &bridge_info
);
562 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
563 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
564 struct zebra_l2info_vlan vlan_info
;
566 netlink_extract_vlan_info(link_data
, &vlan_info
);
567 zebra_l2_vlanif_update(ifp
, &vlan_info
);
568 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
569 struct zebra_l2info_vxlan vxlan_info
;
571 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
572 vxlan_info
.link_nsid
= link_nsid
;
573 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
574 if (link_nsid
!= NS_UNKNOWN
&&
575 vxlan_info
.ifindex_link
)
576 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
581 static int netlink_bridge_vxlan_update(struct interface
*ifp
,
582 struct rtattr
*af_spec
)
584 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
585 struct bridge_vlan_info
*vinfo
;
586 vlanid_t access_vlan
;
588 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
589 * only 1 access VLAN is accepted.
591 memset(aftb
, 0, sizeof(aftb
));
592 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, af_spec
);
593 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
596 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
597 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
600 access_vlan
= (vlanid_t
)vinfo
->vid
;
601 if (IS_ZEBRA_DEBUG_KERNEL
)
602 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
603 ifp
->name
, ifp
->ifindex
);
604 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
608 static void netlink_bridge_vlan_update(struct interface
*ifp
,
609 struct rtattr
*af_spec
)
613 uint16_t vid_range_start
= 0;
614 struct zebra_if
*zif
;
615 bitfield_t old_vlan_bitmap
;
616 struct bridge_vlan_info
*vinfo
;
618 zif
= (struct zebra_if
*)ifp
->info
;
620 /* cache the old bitmap addrs */
621 old_vlan_bitmap
= zif
->vlan_bitmap
;
622 /* create a new bitmap space for re-eval */
623 bf_init(zif
->vlan_bitmap
, IF_VLAN_BITMAP_MAX
);
625 for (i
= RTA_DATA(af_spec
), rem
= RTA_PAYLOAD(af_spec
);
626 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
628 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
633 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
634 vid_range_start
= vinfo
->vid
;
638 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
639 vid_range_start
= vinfo
->vid
;
641 zebra_vlan_bitmap_compute(ifp
, vid_range_start
, vinfo
->vid
);
644 zebra_vlan_mbr_re_eval(ifp
, old_vlan_bitmap
);
646 bf_free(old_vlan_bitmap
);
649 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
653 struct ifinfomsg
*ifi
;
654 struct rtattr
*tb
[IFLA_MAX
+ 1];
655 struct interface
*ifp
;
656 struct zebra_if
*zif
;
657 struct rtattr
*af_spec
;
659 /* Fetch name and ifindex */
661 memset(tb
, 0, sizeof(tb
));
662 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
664 if (tb
[IFLA_IFNAME
] == NULL
)
666 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
668 /* The interface should already be known, if not discard. */
669 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
671 zlog_debug("Cannot find bridge IF %s(%u)", name
,
676 /* We are only interested in the access VLAN i.e., AF_SPEC */
677 af_spec
= tb
[IFLA_AF_SPEC
];
681 if (IS_ZEBRA_IF_VXLAN(ifp
))
682 return netlink_bridge_vxlan_update(ifp
, af_spec
);
684 /* build vlan bitmap associated with this interface if that
685 * device type is interested in the vlans
687 zif
= (struct zebra_if
*)ifp
->info
;
688 if (bf_is_inited(zif
->vlan_bitmap
))
689 netlink_bridge_vlan_update(ifp
, af_spec
);
695 * Called from interface_lookup_netlink(). This function is only used
698 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
701 struct ifinfomsg
*ifi
;
702 struct rtattr
*tb
[IFLA_MAX
+ 1];
703 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
704 struct interface
*ifp
;
708 char *slave_kind
= NULL
;
709 struct zebra_ns
*zns
= NULL
;
710 vrf_id_t vrf_id
= VRF_DEFAULT
;
711 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
712 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
713 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
714 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
715 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
716 struct zebra_if
*zif
;
717 ns_id_t link_nsid
= ns_id
;
719 zns
= zebra_ns_lookup(ns_id
);
722 if (h
->nlmsg_type
!= RTM_NEWLINK
)
725 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
728 "%s: Message received from netlink is of a broken size: %d %zu",
729 __func__
, h
->nlmsg_len
,
730 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
734 /* We are interested in some AF_BRIDGE notifications. */
735 if (ifi
->ifi_family
== AF_BRIDGE
)
736 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
738 /* Looking up interface name. */
739 memset(tb
, 0, sizeof(tb
));
740 memset(linkinfo
, 0, sizeof(linkinfo
));
741 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
743 /* check for wireless messages to ignore */
744 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
745 if (IS_ZEBRA_DEBUG_KERNEL
)
746 zlog_debug("%s: ignoring IFLA_WIRELESS message",
751 if (tb
[IFLA_IFNAME
] == NULL
)
753 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
755 if (tb
[IFLA_IFALIAS
])
756 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
758 if (tb
[IFLA_LINKINFO
]) {
759 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
761 if (linkinfo
[IFLA_INFO_KIND
])
762 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
764 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
765 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
767 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
768 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
770 netlink_determine_zebra_iftype(kind
, &zif_type
);
773 /* If VRF, create the VRF structure itself. */
774 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
775 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
776 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
779 if (tb
[IFLA_MASTER
]) {
780 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
781 && !vrf_is_backend_netns()) {
782 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
783 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
784 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
785 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
787 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
788 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
789 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
790 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
792 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
794 if (vrf_is_backend_netns())
795 vrf_id
= (vrf_id_t
)ns_id
;
797 /* If linking to another interface, note it. */
799 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
801 if (tb
[IFLA_LINK_NETNSID
])
802 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
805 * We add by index first because in some cases such as the master
806 * interface, we have the index before we have the name. Fixing
807 * back references on the slave interfaces is painful if not done
808 * this way, i.e. by creating by ifindex.
810 ifp
= if_get_by_ifindex(ifi
->ifi_index
, vrf_id
);
811 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
813 if_set_name(ifp
, name
);
815 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
816 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
818 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
819 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
821 /* Set zebra interface type */
822 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
823 if (IS_ZEBRA_IF_VRF(ifp
))
824 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
827 * Just set the @link/lower-device ifindex. During nldump interfaces are
828 * not ordered in any fashion so we may end up getting upper devices
829 * before lower devices. We will setup the real linkage once the dump
832 zif
= (struct zebra_if
*)ifp
->info
;
833 zif
->link_ifindex
= link_ifindex
;
836 XFREE(MTYPE_TMP
, zif
->desc
);
837 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
840 /* Hardware type and address. */
841 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
842 netlink_interface_update_hw_addr(tb
, ifp
);
846 /* Extract and save L2 interface information, take additional actions.
848 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
850 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
851 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
852 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
853 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
858 /* Request for specific interface or address information from the kernel */
859 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
860 int type
, uint32_t filter_mask
)
864 struct ifinfomsg ifm
;
868 /* Form the request, specifying filter (rtattr) if needed. */
869 memset(&req
, 0, sizeof(req
));
870 req
.n
.nlmsg_type
= type
;
871 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
872 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
873 req
.ifm
.ifi_family
= family
;
875 /* Include filter, if specified. */
877 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
879 return netlink_request(netlink_cmd
, &req
);
882 /* Interface lookup by netlink socket. */
883 int interface_lookup_netlink(struct zebra_ns
*zns
)
886 struct zebra_dplane_info dp_info
;
887 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
889 /* Capture key info from ns struct */
890 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
892 /* Get interface information. */
893 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
896 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
901 /* Get interface information - for bridge interfaces. */
902 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
903 RTEXT_FILTER_BRVLAN
);
906 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
911 /* Get interface information - for bridge interfaces. */
912 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
913 RTEXT_FILTER_BRVLAN
);
916 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
922 zebra_if_update_all_links();
927 * interface_addr_lookup_netlink() - Look up interface addresses
929 * @zns: Zebra netlink socket
930 * Return: Result status
932 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
935 struct zebra_dplane_info dp_info
;
936 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
938 /* Capture key info from ns struct */
939 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
941 /* Get IPv4 address of the interfaces. */
942 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
945 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
950 /* Get IPv6 address of the interfaces. */
951 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
954 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
962 int kernel_interface_set_master(struct interface
*master
,
963 struct interface
*slave
)
965 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
969 struct ifinfomsg ifa
;
970 char buf
[NL_PKT_BUF_SIZE
];
973 memset(&req
, 0, sizeof(req
));
975 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
976 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
977 req
.n
.nlmsg_type
= RTM_SETLINK
;
978 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
980 req
.ifa
.ifi_index
= slave
->ifindex
;
982 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
983 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
985 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
989 /* Interface address modification. */
990 static ssize_t
netlink_address_msg_encoder(struct zebra_dplane_ctx
*ctx
,
991 void *buf
, size_t buflen
)
994 const struct prefix
*p
;
1000 struct ifaddrmsg ifa
;
1004 if (buflen
< sizeof(*req
))
1007 p
= dplane_ctx_get_intf_addr(ctx
);
1008 memset(req
, 0, sizeof(*req
));
1010 bytelen
= (p
->family
== AF_INET
? 4 : 16);
1012 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1013 req
->n
.nlmsg_flags
= NLM_F_REQUEST
;
1015 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
1020 req
->n
.nlmsg_type
= cmd
;
1021 req
->ifa
.ifa_family
= p
->family
;
1023 req
->ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
1025 if (!nl_attr_put(&req
->n
, buflen
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
))
1028 if (p
->family
== AF_INET
) {
1029 if (dplane_ctx_intf_is_connected(ctx
)) {
1030 p
= dplane_ctx_get_intf_dest(ctx
);
1031 if (!nl_attr_put(&req
->n
, buflen
, IFA_ADDRESS
,
1032 &p
->u
.prefix
, bytelen
))
1034 } else if (cmd
== RTM_NEWADDR
) {
1035 struct in_addr broad
= {
1036 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
1039 if (!nl_attr_put(&req
->n
, buflen
, IFA_BROADCAST
, &broad
,
1045 /* p is now either address or destination/bcast addr */
1046 req
->ifa
.ifa_prefixlen
= p
->prefixlen
;
1048 if (dplane_ctx_intf_is_secondary(ctx
))
1049 SET_FLAG(req
->ifa
.ifa_flags
, IFA_F_SECONDARY
);
1051 if (dplane_ctx_intf_has_label(ctx
)) {
1052 label
= dplane_ctx_get_intf_label(ctx
);
1053 if (!nl_attr_put(&req
->n
, buflen
, IFA_LABEL
, label
,
1058 return NLMSG_ALIGN(req
->n
.nlmsg_len
);
1062 * The communication with the kernel is done using the message batching
1063 * interface, so return a failure.
1065 enum zebra_dplane_result
kernel_address_update_ctx(struct zebra_dplane_ctx
*ctx
)
1067 return ZEBRA_DPLANE_REQUEST_FAILURE
;
1070 enum netlink_msg_status
1071 netlink_put_address_update_msg(struct nl_batch
*bth
,
1072 struct zebra_dplane_ctx
*ctx
)
1074 return netlink_batch_add_msg(bth
, ctx
, netlink_address_msg_encoder
,
1078 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1081 struct ifaddrmsg
*ifa
;
1082 struct rtattr
*tb
[IFA_MAX
+ 1];
1083 struct interface
*ifp
;
1088 struct zebra_ns
*zns
;
1089 uint32_t metric
= METRIC_MAX
;
1090 uint32_t kernel_flags
= 0;
1092 zns
= zebra_ns_lookup(ns_id
);
1093 ifa
= NLMSG_DATA(h
);
1095 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1097 EC_ZEBRA_UNKNOWN_FAMILY
,
1098 "Invalid address family: %u received from kernel interface addr change: %s",
1099 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1103 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1106 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1109 "%s: Message received from netlink is of a broken size: %d %zu",
1110 __func__
, h
->nlmsg_len
,
1111 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1115 memset(tb
, 0, sizeof(tb
));
1116 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1118 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1122 "netlink_interface_addr can't find interface by index %d",
1127 /* Flags passed through */
1129 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1131 kernel_flags
= ifa
->ifa_flags
;
1133 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1136 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1137 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1140 zlog_debug(" IFA_LOCAL %s/%d",
1141 inet_ntop(ifa
->ifa_family
,
1142 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1144 ifa
->ifa_prefixlen
);
1145 if (tb
[IFA_ADDRESS
])
1146 zlog_debug(" IFA_ADDRESS %s/%d",
1147 inet_ntop(ifa
->ifa_family
,
1148 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1150 ifa
->ifa_prefixlen
);
1151 if (tb
[IFA_BROADCAST
])
1152 zlog_debug(" IFA_BROADCAST %s/%d",
1153 inet_ntop(ifa
->ifa_family
,
1154 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1156 ifa
->ifa_prefixlen
);
1157 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1158 zlog_debug(" IFA_LABEL %s",
1159 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1161 if (tb
[IFA_CACHEINFO
]) {
1162 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1163 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1164 ci
->ifa_prefered
, ci
->ifa_valid
);
1168 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1169 if (tb
[IFA_LOCAL
] == NULL
)
1170 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1171 if (tb
[IFA_ADDRESS
] == NULL
)
1172 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1174 /* local interface address */
1175 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1177 /* is there a peer address? */
1179 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1180 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1181 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1182 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1184 /* seeking a broadcast address */
1185 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1188 /* addr is primary key, SOL if we don't have one */
1190 zlog_debug("%s: Local Interface Address is NULL for %s",
1191 __func__
, ifp
->name
);
1196 if (kernel_flags
& IFA_F_SECONDARY
)
1197 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1201 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1203 if (label
&& strcmp(ifp
->name
, label
) == 0)
1206 if (tb
[IFA_RT_PRIORITY
])
1207 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1209 /* Register interface address to the interface. */
1210 if (ifa
->ifa_family
== AF_INET
) {
1211 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1213 "Invalid prefix length: %u received from kernel interface addr change: %s",
1215 nl_msg_type_to_str(h
->nlmsg_type
));
1219 if (h
->nlmsg_type
== RTM_NEWADDR
)
1220 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1222 (struct in_addr
*)broad
, label
,
1224 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1225 /* Delete with a peer address */
1226 connected_delete_ipv4(
1227 ifp
, flags
, (struct in_addr
*)addr
,
1228 ifa
->ifa_prefixlen
, broad
);
1230 connected_delete_ipv4(
1231 ifp
, flags
, (struct in_addr
*)addr
,
1232 ifa
->ifa_prefixlen
, NULL
);
1235 if (ifa
->ifa_family
== AF_INET6
) {
1236 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1238 "Invalid prefix length: %u received from kernel interface addr change: %s",
1240 nl_msg_type_to_str(h
->nlmsg_type
));
1243 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1244 /* Only consider valid addresses; we'll not get a
1246 * the kernel till IPv6 DAD has completed, but at init
1248 * does query for and will receive all addresses.
1251 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1252 connected_add_ipv6(ifp
, flags
,
1253 (struct in6_addr
*)addr
,
1254 (struct in6_addr
*)broad
,
1255 ifa
->ifa_prefixlen
, label
,
1258 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1259 NULL
, ifa
->ifa_prefixlen
);
1264 * Linux kernel does not send route delete on interface down/addr del
1265 * so we have to re-process routes it owns (i.e. kernel routes)
1267 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1268 rib_update(RIB_UPDATE_KERNEL
);
1273 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1276 struct ifinfomsg
*ifi
;
1277 struct rtattr
*tb
[IFLA_MAX
+ 1];
1278 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1279 struct interface
*ifp
;
1283 char *slave_kind
= NULL
;
1284 struct zebra_ns
*zns
;
1285 vrf_id_t vrf_id
= VRF_DEFAULT
;
1286 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1287 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1288 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1289 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1290 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1291 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1292 struct zebra_if
*zif
;
1293 ns_id_t link_nsid
= ns_id
;
1295 zns
= zebra_ns_lookup(ns_id
);
1296 ifi
= NLMSG_DATA(h
);
1298 /* assume if not default zns, then new VRF */
1299 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1300 /* If this is not link add/delete message so print warning. */
1301 zlog_debug("netlink_link_change: wrong kernel message %s",
1302 nl_msg_type_to_str(h
->nlmsg_type
));
1306 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1307 || ifi
->ifi_family
== AF_INET6
)) {
1309 EC_ZEBRA_UNKNOWN_FAMILY
,
1310 "Invalid address family: %u received from kernel link change: %s",
1311 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1315 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1318 "%s: Message received from netlink is of a broken size %d %zu",
1319 __func__
, h
->nlmsg_len
,
1320 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1324 /* We are interested in some AF_BRIDGE notifications. */
1325 if (ifi
->ifi_family
== AF_BRIDGE
)
1326 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1328 /* Looking up interface name. */
1329 memset(tb
, 0, sizeof(tb
));
1330 memset(linkinfo
, 0, sizeof(linkinfo
));
1331 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1333 /* check for wireless messages to ignore */
1334 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1335 if (IS_ZEBRA_DEBUG_KERNEL
)
1336 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1341 if (tb
[IFLA_IFNAME
] == NULL
)
1343 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1345 if (tb
[IFLA_LINKINFO
]) {
1346 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1348 if (linkinfo
[IFLA_INFO_KIND
])
1349 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1351 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1352 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1354 netlink_determine_zebra_iftype(kind
, &zif_type
);
1357 /* If linking to another interface, note it. */
1359 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1361 if (tb
[IFLA_LINK_NETNSID
])
1362 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1364 if (tb
[IFLA_IFALIAS
]) {
1365 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1368 /* If VRF, create or update the VRF structure itself. */
1369 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1370 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1371 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1374 /* See if interface is present. */
1375 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1377 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1378 if (tb
[IFLA_MASTER
]) {
1379 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1380 && !vrf_is_backend_netns()) {
1381 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1382 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1383 } else if (slave_kind
1384 && (strcmp(slave_kind
, "bridge") == 0)) {
1385 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1387 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1388 } else if (slave_kind
1389 && (strcmp(slave_kind
, "bond") == 0)) {
1390 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1392 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1394 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1396 if (vrf_is_backend_netns())
1397 vrf_id
= (vrf_id_t
)ns_id
;
1399 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1400 /* Add interface notification from kernel */
1401 if (IS_ZEBRA_DEBUG_KERNEL
)
1403 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1404 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1405 zif_slave_type
, bridge_ifindex
,
1409 /* unknown interface */
1410 ifp
= if_get_by_name(name
, vrf_id
);
1412 /* pre-configured interface, learnt now */
1413 if (ifp
->vrf_id
!= vrf_id
)
1414 if_update_to_new_vrf(ifp
, vrf_id
);
1417 /* Update interface information. */
1418 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1419 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1420 if (!tb
[IFLA_MTU
]) {
1422 "RTM_NEWLINK for interface %s(%u) without MTU set",
1423 name
, ifi
->ifi_index
);
1426 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1428 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1430 /* Set interface type */
1431 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1432 if (IS_ZEBRA_IF_VRF(ifp
))
1433 SET_FLAG(ifp
->status
,
1434 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1437 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1439 netlink_interface_update_hw_addr(tb
, ifp
);
1441 /* Inform clients, install any configured addresses. */
1444 /* Extract and save L2 interface information, take
1445 * additional actions. */
1446 netlink_interface_update_l2info(
1447 ifp
, linkinfo
[IFLA_INFO_DATA
],
1449 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1450 zebra_l2if_update_bridge_slave(ifp
,
1452 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1453 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1454 } else if (ifp
->vrf_id
!= vrf_id
) {
1455 /* VRF change for an interface. */
1456 if (IS_ZEBRA_DEBUG_KERNEL
)
1458 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1459 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1462 if_handle_vrf_change(ifp
, vrf_id
);
1464 bool was_bridge_slave
, was_bond_slave
;
1466 /* Interface update. */
1467 if (IS_ZEBRA_DEBUG_KERNEL
)
1469 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1470 name
, ifp
->ifindex
, zif_slave_type
,
1471 bridge_ifindex
, ifi
->ifi_flags
);
1473 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1474 if (!tb
[IFLA_MTU
]) {
1476 "RTM_NEWLINK for interface %s(%u) without MTU set",
1477 name
, ifi
->ifi_index
);
1480 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1483 /* Update interface type - NOTE: Only slave_type can
1485 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1486 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
1487 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1489 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
1491 netlink_interface_update_hw_addr(tb
, ifp
);
1493 if (if_is_no_ptm_operative(ifp
)) {
1494 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1495 if (!if_is_no_ptm_operative(ifp
)) {
1496 if (IS_ZEBRA_DEBUG_KERNEL
)
1498 "Intf %s(%u) has gone DOWN",
1499 name
, ifp
->ifindex
);
1501 rib_update(RIB_UPDATE_KERNEL
);
1502 } else if (if_is_operative(ifp
)) {
1503 /* Must notify client daemons of new
1504 * interface status. */
1505 if (IS_ZEBRA_DEBUG_KERNEL
)
1507 "Intf %s(%u) PTM up, notifying clients",
1508 name
, ifp
->ifindex
);
1509 zebra_interface_up_update(ifp
);
1511 /* Update EVPN VNI when SVI MAC change
1513 if (IS_ZEBRA_IF_VLAN(ifp
) &&
1514 memcmp(old_hw_addr
, ifp
->hw_addr
,
1515 INTERFACE_HWADDR_MAX
)) {
1516 struct interface
*link_if
;
1519 if_lookup_by_index_per_ns(
1520 zebra_ns_lookup(NS_DEFAULT
),
1523 zebra_vxlan_svi_up(ifp
,
1528 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1529 if (if_is_operative(ifp
)) {
1530 if (IS_ZEBRA_DEBUG_KERNEL
)
1532 "Intf %s(%u) has come UP",
1533 name
, ifp
->ifindex
);
1536 if (IS_ZEBRA_DEBUG_KERNEL
)
1538 "Intf %s(%u) has gone DOWN",
1539 name
, ifp
->ifindex
);
1541 rib_update(RIB_UPDATE_KERNEL
);
1545 /* Extract and save L2 interface information, take
1546 * additional actions. */
1547 netlink_interface_update_l2info(
1548 ifp
, linkinfo
[IFLA_INFO_DATA
],
1550 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1551 zebra_l2if_update_bridge_slave(ifp
,
1553 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
1554 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1559 XFREE(MTYPE_TMP
, zif
->desc
);
1561 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1564 /* Delete interface notification from kernel */
1566 if (IS_ZEBRA_DEBUG_KERNEL
)
1568 "RTM_DELLINK for unknown interface %s(%u)",
1569 name
, ifi
->ifi_index
);
1573 if (IS_ZEBRA_DEBUG_KERNEL
)
1574 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1577 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1579 /* Special handling for bridge or VxLAN interfaces. */
1580 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1581 zebra_l2_bridge_del(ifp
);
1582 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1583 zebra_l2_vxlanif_del(ifp
);
1585 if (!IS_ZEBRA_IF_VRF(ifp
))
1586 if_delete_update(ifp
);
1592 int netlink_protodown(struct interface
*ifp
, bool down
)
1594 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1598 struct ifinfomsg ifa
;
1599 char buf
[NL_PKT_BUF_SIZE
];
1602 memset(&req
, 0, sizeof(req
));
1604 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1605 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1606 req
.n
.nlmsg_type
= RTM_SETLINK
;
1607 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1609 req
.ifa
.ifi_index
= ifp
->ifindex
;
1611 nl_attr_put(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN
, &down
, sizeof(down
));
1612 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, ifp
->ifindex
);
1614 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1618 /* Interface information read by netlink. */
1619 void interface_list(struct zebra_ns
*zns
)
1621 interface_lookup_netlink(zns
);
1622 /* We add routes for interface address,
1623 * so we need to get the nexthop info
1624 * from the kernel before we can do that
1626 netlink_nexthop_read(zns
);
1628 interface_addr_lookup_netlink(zns
);
1631 #endif /* GNU_LINUX */