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"
74 extern struct zebra_privs_t zserv_privs
;
76 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
77 names and ifindex values. */
78 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
81 struct interface
*oifp
;
83 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
85 if (ifi_index
== IFINDEX_INTERNAL
)
88 "Netlink is setting interface %s ifindex to reserved internal value %u",
89 ifp
->name
, ifi_index
);
91 if (IS_ZEBRA_DEBUG_KERNEL
)
93 "interface index %d was renamed from %s to %s",
94 ifi_index
, oifp
->name
, ifp
->name
);
98 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
99 ifi_index
, oifp
->name
, ifp
->name
);
100 if_delete_update(oifp
);
103 if_set_index(ifp
, ifi_index
);
106 /* Utility function to parse hardware link-layer address and update ifp */
107 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
108 struct interface
*ifp
)
112 if (tb
[IFLA_ADDRESS
]) {
115 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
117 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
118 zlog_debug("Hardware address is too large: %d",
121 ifp
->hw_addr_len
= hw_addr_len
;
122 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
125 for (i
= 0; i
< hw_addr_len
; i
++)
126 if (ifp
->hw_addr
[i
] != 0)
129 if (i
== hw_addr_len
)
130 ifp
->hw_addr_len
= 0;
132 ifp
->hw_addr_len
= hw_addr_len
;
137 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
141 return ZEBRA_LLT_ETHER
;
143 return ZEBRA_LLT_EETHER
;
145 return ZEBRA_LLT_AX25
;
147 return ZEBRA_LLT_PRONET
;
149 return ZEBRA_LLT_IEEE802
;
151 return ZEBRA_LLT_ARCNET
;
152 case ARPHRD_APPLETLK
:
153 return ZEBRA_LLT_APPLETLK
;
155 return ZEBRA_LLT_DLCI
;
157 return ZEBRA_LLT_ATM
;
158 case ARPHRD_METRICOM
:
159 return ZEBRA_LLT_METRICOM
;
160 case ARPHRD_IEEE1394
:
161 return ZEBRA_LLT_IEEE1394
;
163 return ZEBRA_LLT_EUI64
;
164 case ARPHRD_INFINIBAND
:
165 return ZEBRA_LLT_INFINIBAND
;
167 return ZEBRA_LLT_SLIP
;
169 return ZEBRA_LLT_CSLIP
;
171 return ZEBRA_LLT_SLIP6
;
173 return ZEBRA_LLT_CSLIP6
;
175 return ZEBRA_LLT_RSRVD
;
177 return ZEBRA_LLT_ADAPT
;
179 return ZEBRA_LLT_ROSE
;
181 return ZEBRA_LLT_X25
;
183 return ZEBRA_LLT_PPP
;
185 return ZEBRA_LLT_CHDLC
;
187 return ZEBRA_LLT_LAPB
;
189 return ZEBRA_LLT_RAWHDLC
;
191 return ZEBRA_LLT_IPIP
;
193 return ZEBRA_LLT_IPIP6
;
195 return ZEBRA_LLT_FRAD
;
197 return ZEBRA_LLT_SKIP
;
198 case ARPHRD_LOOPBACK
:
199 return ZEBRA_LLT_LOOPBACK
;
200 case ARPHRD_LOCALTLK
:
201 return ZEBRA_LLT_LOCALTLK
;
203 return ZEBRA_LLT_FDDI
;
205 return ZEBRA_LLT_SIT
;
207 return ZEBRA_LLT_IPDDP
;
209 return ZEBRA_LLT_IPGRE
;
211 return ZEBRA_LLT_PIMREG
;
213 return ZEBRA_LLT_HIPPI
;
215 return ZEBRA_LLT_ECONET
;
217 return ZEBRA_LLT_IRDA
;
219 return ZEBRA_LLT_FCPP
;
221 return ZEBRA_LLT_FCAL
;
223 return ZEBRA_LLT_FCPL
;
224 case ARPHRD_FCFABRIC
:
225 return ZEBRA_LLT_FCFABRIC
;
226 case ARPHRD_IEEE802_TR
:
227 return ZEBRA_LLT_IEEE802_TR
;
228 case ARPHRD_IEEE80211
:
229 return ZEBRA_LLT_IEEE80211
;
230 #ifdef ARPHRD_IEEE802154
231 case ARPHRD_IEEE802154
:
232 return ZEBRA_LLT_IEEE802154
;
236 return ZEBRA_LLT_IP6GRE
;
238 #ifdef ARPHRD_IEEE802154_PHY
239 case ARPHRD_IEEE802154_PHY
:
240 return ZEBRA_LLT_IEEE802154_PHY
;
244 return ZEBRA_LLT_UNKNOWN
;
248 static void netlink_determine_zebra_iftype(const char *kind
,
249 zebra_iftype_t
*zif_type
)
251 *zif_type
= ZEBRA_IF_OTHER
;
256 if (strcmp(kind
, "vrf") == 0)
257 *zif_type
= ZEBRA_IF_VRF
;
258 else if (strcmp(kind
, "bridge") == 0)
259 *zif_type
= ZEBRA_IF_BRIDGE
;
260 else if (strcmp(kind
, "vlan") == 0)
261 *zif_type
= ZEBRA_IF_VLAN
;
262 else if (strcmp(kind
, "vxlan") == 0)
263 *zif_type
= ZEBRA_IF_VXLAN
;
264 else if (strcmp(kind
, "macvlan") == 0)
265 *zif_type
= ZEBRA_IF_MACVLAN
;
266 else if (strcmp(kind
, "veth") == 0)
267 *zif_type
= ZEBRA_IF_VETH
;
268 else if (strcmp(kind
, "bond") == 0)
269 *zif_type
= ZEBRA_IF_BOND
;
270 else if (strcmp(kind
, "bond_slave") == 0)
271 *zif_type
= ZEBRA_IF_BOND_SLAVE
;
274 #define parse_rtattr_nested(tb, max, rta) \
275 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
277 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
278 uint32_t ns_id
, const char *name
)
280 struct ifinfomsg
*ifi
;
281 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
282 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
284 struct zebra_vrf
*zvrf
;
285 uint32_t nl_table_id
;
289 memset(linkinfo
, 0, sizeof(linkinfo
));
290 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
292 if (!linkinfo
[IFLA_INFO_DATA
]) {
293 if (IS_ZEBRA_DEBUG_KERNEL
)
295 "%s: IFLA_INFO_DATA missing from VRF message: %s",
300 memset(attr
, 0, sizeof(attr
));
301 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
302 if (!attr
[IFLA_VRF_TABLE
]) {
303 if (IS_ZEBRA_DEBUG_KERNEL
)
305 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
310 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
312 if (h
->nlmsg_type
== RTM_NEWLINK
) {
313 if (IS_ZEBRA_DEBUG_KERNEL
)
314 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
315 ifi
->ifi_index
, nl_table_id
);
317 if (!vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
)) {
320 exist_id
= vrf_lookup_by_table(nl_table_id
, ns_id
);
321 if (exist_id
!= VRF_DEFAULT
) {
322 vrf
= vrf_lookup_by_id(exist_id
);
325 EC_ZEBRA_VRF_MISCONFIGURED
,
326 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
327 name
, ifi
->ifi_index
, vrf
->name
);
333 * vrf_get is implied creation if it does not exist
335 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
336 name
); // It would create vrf
338 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
339 name
, ifi
->ifi_index
);
344 * This is the only place that we get the actual kernel table_id
345 * being used. We need it to set the table_id of the routes
346 * we are passing to the kernel.... And to throw some totally
347 * awesome parties. that too.
349 * At this point we *must* have a zvrf because the vrf_create
350 * callback creates one. We *must* set the table id
351 * before the vrf_enable because of( at the very least )
352 * static routes being delayed for installation until
353 * during the vrf_enable callbacks.
355 zvrf
= (struct zebra_vrf
*)vrf
->info
;
356 zvrf
->table_id
= nl_table_id
;
358 /* Enable the created VRF. */
359 if (!vrf_enable(vrf
)) {
360 flog_err(EC_LIB_INTERFACE
,
361 "Failed to enable VRF %s id %u", name
,
366 } else // h->nlmsg_type == RTM_DELLINK
368 if (IS_ZEBRA_DEBUG_KERNEL
)
369 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
372 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
375 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
384 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
387 struct ethtool_cmd ecmd
;
390 const char *ifname
= interface
->name
;
394 /* initialize struct */
395 memset(&ifdata
, 0, sizeof(ifdata
));
397 /* set interface name */
398 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
400 /* initialize ethtool interface */
401 memset(&ecmd
, 0, sizeof(ecmd
));
402 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
403 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
405 /* use ioctl to get IP address of an interface */
406 frr_with_privs(&zserv_privs
) {
407 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
411 if (IS_ZEBRA_DEBUG_KERNEL
)
412 zlog_debug("Failure to read interface %s speed: %d %s",
413 ifname
, errno
, safe_strerror(errno
));
414 /* no vrf socket creation may probably mean vrf issue */
419 /* Get the current link state for the interface */
420 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
,
424 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
426 "IOCTL failure to read interface %s speed: %d %s",
427 ifname
, errno
, safe_strerror(errno
));
428 /* no device means interface unreachable */
429 if (errno
== ENODEV
&& error
)
437 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
440 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
442 return get_iflink_speed(ifp
, error
);
445 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
446 struct zebra_l2info_bridge
*bridge_info
)
448 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
450 memset(bridge_info
, 0, sizeof(*bridge_info
));
451 memset(attr
, 0, sizeof(attr
));
452 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
453 if (attr
[IFLA_BR_VLAN_FILTERING
])
454 bridge_info
->vlan_aware
=
455 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
459 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
460 struct zebra_l2info_vlan
*vlan_info
)
462 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
465 memset(vlan_info
, 0, sizeof(*vlan_info
));
466 memset(attr
, 0, sizeof(attr
));
467 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
468 if (!attr
[IFLA_VLAN_ID
]) {
469 if (IS_ZEBRA_DEBUG_KERNEL
)
470 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
474 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
475 vlan_info
->vid
= vid_in_msg
;
479 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
480 struct zebra_l2info_vxlan
*vxl_info
)
482 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
484 struct in_addr vtep_ip_in_msg
;
485 ifindex_t ifindex_link
;
487 memset(vxl_info
, 0, sizeof(*vxl_info
));
488 memset(attr
, 0, sizeof(attr
));
489 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
490 if (!attr
[IFLA_VXLAN_ID
]) {
491 if (IS_ZEBRA_DEBUG_KERNEL
)
493 "IFLA_VXLAN_ID missing from VXLAN IF message");
497 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
498 vxl_info
->vni
= vni_in_msg
;
499 if (!attr
[IFLA_VXLAN_LOCAL
]) {
500 if (IS_ZEBRA_DEBUG_KERNEL
)
502 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
505 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
506 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
509 if (attr
[IFLA_VXLAN_GROUP
]) {
510 vxl_info
->mcast_grp
=
511 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_GROUP
]);
514 if (!attr
[IFLA_VXLAN_LINK
]) {
515 if (IS_ZEBRA_DEBUG_KERNEL
)
516 zlog_debug("IFLA_VXLAN_LINK missing "
517 "from VXLAN IF message");
520 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
521 vxl_info
->ifindex_link
= ifindex_link
;
527 * Extract and save L2 params (of interest) for an interface. When a
528 * bridge interface is added or updated, take further actions to map
529 * its members. Likewise, for VxLAN interface.
531 static void netlink_interface_update_l2info(struct interface
*ifp
,
532 struct rtattr
*link_data
, int add
,
538 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
539 struct zebra_l2info_bridge bridge_info
;
541 netlink_extract_bridge_info(link_data
, &bridge_info
);
542 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
543 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
544 struct zebra_l2info_vlan vlan_info
;
546 netlink_extract_vlan_info(link_data
, &vlan_info
);
547 zebra_l2_vlanif_update(ifp
, &vlan_info
);
548 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
549 struct zebra_l2info_vxlan vxlan_info
;
551 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
552 vxlan_info
.link_nsid
= link_nsid
;
553 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
554 if (link_nsid
!= NS_UNKNOWN
&&
555 vxlan_info
.ifindex_link
)
556 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
561 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
565 struct ifinfomsg
*ifi
;
566 struct rtattr
*tb
[IFLA_MAX
+ 1];
567 struct interface
*ifp
;
568 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
573 vlanid_t access_vlan
;
575 /* Fetch name and ifindex */
577 memset(tb
, 0, sizeof(tb
));
578 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
580 if (tb
[IFLA_IFNAME
] == NULL
)
582 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
584 /* The interface should already be known, if not discard. */
585 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
587 zlog_debug("Cannot find bridge IF %s(%u)", name
,
591 if (!IS_ZEBRA_IF_VXLAN(ifp
))
594 /* We are only interested in the access VLAN i.e., AF_SPEC */
595 if (!tb
[IFLA_AF_SPEC
])
598 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
599 * only 1 access VLAN is accepted.
601 memset(aftb
, 0, sizeof(aftb
));
602 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
603 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
606 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
607 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
610 access_vlan
= (vlanid_t
)vinfo
->vid
;
611 if (IS_ZEBRA_DEBUG_KERNEL
)
612 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
613 name
, ifi
->ifi_index
);
614 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
619 * Called from interface_lookup_netlink(). This function is only used
622 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
625 struct ifinfomsg
*ifi
;
626 struct rtattr
*tb
[IFLA_MAX
+ 1];
627 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
628 struct interface
*ifp
;
632 char *slave_kind
= NULL
;
633 struct zebra_ns
*zns
= NULL
;
634 vrf_id_t vrf_id
= VRF_DEFAULT
;
635 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
636 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
637 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
638 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
639 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
640 struct zebra_if
*zif
;
641 ns_id_t link_nsid
= ns_id
;
643 zns
= zebra_ns_lookup(ns_id
);
646 if (h
->nlmsg_type
!= RTM_NEWLINK
)
649 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
652 "%s: Message received from netlink is of a broken size: %d %zu",
653 __func__
, h
->nlmsg_len
,
654 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
658 /* We are interested in some AF_BRIDGE notifications. */
659 if (ifi
->ifi_family
== AF_BRIDGE
)
660 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
662 /* Looking up interface name. */
663 memset(tb
, 0, sizeof(tb
));
664 memset(linkinfo
, 0, sizeof(linkinfo
));
665 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
667 /* check for wireless messages to ignore */
668 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
669 if (IS_ZEBRA_DEBUG_KERNEL
)
670 zlog_debug("%s: ignoring IFLA_WIRELESS message",
675 if (tb
[IFLA_IFNAME
] == NULL
)
677 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
679 if (tb
[IFLA_IFALIAS
])
680 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
682 if (tb
[IFLA_LINKINFO
]) {
683 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
685 if (linkinfo
[IFLA_INFO_KIND
])
686 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
688 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
689 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
691 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
692 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
694 netlink_determine_zebra_iftype(kind
, &zif_type
);
697 /* If VRF, create the VRF structure itself. */
698 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
699 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
700 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
703 if (tb
[IFLA_MASTER
]) {
704 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
705 && !vrf_is_backend_netns()) {
706 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
707 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
708 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
709 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
711 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
712 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
713 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
714 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
716 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
718 if (vrf_is_backend_netns())
719 vrf_id
= (vrf_id_t
)ns_id
;
721 /* If linking to another interface, note it. */
723 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
725 if (tb
[IFLA_LINK_NETNSID
]) {
726 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
727 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
731 * We add by index first because in some cases such as the master
732 * interface, we have the index before we have the name. Fixing
733 * back references on the slave interfaces is painful if not done
734 * this way, i.e. by creating by ifindex.
736 ifp
= if_get_by_ifindex(ifi
->ifi_index
, vrf_id
, name
);
737 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
739 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
740 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
742 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
743 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
745 /* Set zebra interface type */
746 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
747 if (IS_ZEBRA_IF_VRF(ifp
))
748 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
751 * Just set the @link/lower-device ifindex. During nldump interfaces are
752 * not ordered in any fashion so we may end up getting upper devices
753 * before lower devices. We will setup the real linkage once the dump
756 zif
= (struct zebra_if
*)ifp
->info
;
757 zif
->link_ifindex
= link_ifindex
;
760 XFREE(MTYPE_TMP
, zif
->desc
);
761 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
764 /* Hardware type and address. */
765 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
766 netlink_interface_update_hw_addr(tb
, ifp
);
770 /* Extract and save L2 interface information, take additional actions.
772 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
774 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
775 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
);
776 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
777 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
782 /* Request for specific interface or address information from the kernel */
783 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
784 int type
, uint32_t filter_mask
)
788 struct ifinfomsg ifm
;
792 /* Form the request, specifying filter (rtattr) if needed. */
793 memset(&req
, 0, sizeof(req
));
794 req
.n
.nlmsg_type
= type
;
795 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
796 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
797 req
.ifm
.ifi_family
= family
;
799 /* Include filter, if specified. */
801 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
803 return netlink_request(netlink_cmd
, &req
);
806 /* Interface lookup by netlink socket. */
807 int interface_lookup_netlink(struct zebra_ns
*zns
)
810 struct zebra_dplane_info dp_info
;
811 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
813 /* Capture key info from ns struct */
814 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
816 /* Get interface information. */
817 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
820 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
825 /* Get interface information - for bridge interfaces. */
826 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
827 RTEXT_FILTER_BRVLAN
);
830 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
835 /* Get interface information - for bridge interfaces. */
836 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
837 RTEXT_FILTER_BRVLAN
);
840 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
846 zebra_if_update_all_links();
851 * interface_addr_lookup_netlink() - Look up interface addresses
853 * @zns: Zebra netlink socket
854 * Return: Result status
856 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
859 struct zebra_dplane_info dp_info
;
860 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
862 /* Capture key info from ns struct */
863 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
865 /* Get IPv4 address of the interfaces. */
866 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
869 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
874 /* Get IPv6 address of the interfaces. */
875 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
878 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
886 int kernel_interface_set_master(struct interface
*master
,
887 struct interface
*slave
)
889 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
893 struct ifinfomsg ifa
;
894 char buf
[NL_PKT_BUF_SIZE
];
897 memset(&req
, 0, sizeof(req
));
899 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
900 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
901 req
.n
.nlmsg_type
= RTM_SETLINK
;
902 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
904 req
.ifa
.ifi_index
= slave
->ifindex
;
906 addattr_l(&req
.n
, sizeof(req
), IFLA_MASTER
, &master
->ifindex
, 4);
907 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &slave
->ifindex
, 4);
909 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
913 /* Interface address modification. */
914 static int netlink_address_ctx(const struct zebra_dplane_ctx
*ctx
)
917 const struct prefix
*p
;
923 struct ifaddrmsg ifa
;
924 char buf
[NL_PKT_BUF_SIZE
];
927 p
= dplane_ctx_get_intf_addr(ctx
);
928 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
930 bytelen
= (p
->family
== AF_INET
? 4 : 16);
932 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
933 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
935 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
940 req
.n
.nlmsg_type
= cmd
;
941 req
.ifa
.ifa_family
= p
->family
;
943 req
.ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
945 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
947 if (p
->family
== AF_INET
) {
948 if (dplane_ctx_intf_is_connected(ctx
)) {
949 p
= dplane_ctx_get_intf_dest(ctx
);
950 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
,
951 &p
->u
.prefix
, bytelen
);
952 } else if (cmd
== RTM_NEWADDR
) {
953 struct in_addr broad
= {
954 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
957 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
,
962 /* p is now either address or destination/bcast addr */
963 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
965 if (dplane_ctx_intf_is_secondary(ctx
))
966 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
968 if (dplane_ctx_intf_has_label(ctx
)) {
969 label
= dplane_ctx_get_intf_label(ctx
);
970 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, label
,
974 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
975 dplane_ctx_get_ns(ctx
), 0);
978 enum zebra_dplane_result
kernel_address_update_ctx(struct zebra_dplane_ctx
*ctx
)
980 return (netlink_address_ctx(ctx
) == 0 ?
981 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
984 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
987 struct ifaddrmsg
*ifa
;
988 struct rtattr
*tb
[IFA_MAX
+ 1];
989 struct interface
*ifp
;
994 struct zebra_ns
*zns
;
995 uint32_t metric
= METRIC_MAX
;
996 uint32_t kernel_flags
= 0;
998 zns
= zebra_ns_lookup(ns_id
);
1001 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1003 EC_ZEBRA_UNKNOWN_FAMILY
,
1004 "Invalid address family: %u received from kernel interface addr change: %s",
1005 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1009 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1012 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1015 "%s: Message received from netlink is of a broken size: %d %zu",
1016 __func__
, h
->nlmsg_len
,
1017 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1021 memset(tb
, 0, sizeof(tb
));
1022 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1024 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1028 "netlink_interface_addr can't find interface by index %d",
1033 /* Flags passed through */
1035 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1037 kernel_flags
= ifa
->ifa_flags
;
1039 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1042 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1043 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1046 zlog_debug(" IFA_LOCAL %s/%d",
1047 inet_ntop(ifa
->ifa_family
,
1048 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1050 ifa
->ifa_prefixlen
);
1051 if (tb
[IFA_ADDRESS
])
1052 zlog_debug(" IFA_ADDRESS %s/%d",
1053 inet_ntop(ifa
->ifa_family
,
1054 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1056 ifa
->ifa_prefixlen
);
1057 if (tb
[IFA_BROADCAST
])
1058 zlog_debug(" IFA_BROADCAST %s/%d",
1059 inet_ntop(ifa
->ifa_family
,
1060 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1062 ifa
->ifa_prefixlen
);
1063 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1064 zlog_debug(" IFA_LABEL %s",
1065 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1067 if (tb
[IFA_CACHEINFO
]) {
1068 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1069 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1070 ci
->ifa_prefered
, ci
->ifa_valid
);
1074 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1075 if (tb
[IFA_LOCAL
] == NULL
)
1076 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1077 if (tb
[IFA_ADDRESS
] == NULL
)
1078 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1080 /* local interface address */
1081 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1083 /* is there a peer address? */
1085 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1086 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1087 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1088 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1090 /* seeking a broadcast address */
1091 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1094 /* addr is primary key, SOL if we don't have one */
1096 zlog_debug("%s: Local Interface Address is NULL for %s",
1097 __func__
, ifp
->name
);
1102 if (kernel_flags
& IFA_F_SECONDARY
)
1103 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1107 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1109 if (label
&& strcmp(ifp
->name
, label
) == 0)
1112 if (tb
[IFA_RT_PRIORITY
])
1113 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1115 /* Register interface address to the interface. */
1116 if (ifa
->ifa_family
== AF_INET
) {
1117 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1119 "Invalid prefix length: %u received from kernel interface addr change: %s",
1121 nl_msg_type_to_str(h
->nlmsg_type
));
1125 if (h
->nlmsg_type
== RTM_NEWADDR
)
1126 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1128 (struct in_addr
*)broad
, label
,
1130 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1131 /* Delete with a peer address */
1132 connected_delete_ipv4(
1133 ifp
, flags
, (struct in_addr
*)addr
,
1134 ifa
->ifa_prefixlen
, broad
);
1136 connected_delete_ipv4(
1137 ifp
, flags
, (struct in_addr
*)addr
,
1138 ifa
->ifa_prefixlen
, NULL
);
1141 if (ifa
->ifa_family
== AF_INET6
) {
1142 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1144 "Invalid prefix length: %u received from kernel interface addr change: %s",
1146 nl_msg_type_to_str(h
->nlmsg_type
));
1149 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1150 /* Only consider valid addresses; we'll not get a
1152 * the kernel till IPv6 DAD has completed, but at init
1154 * does query for and will receive all addresses.
1157 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1158 connected_add_ipv6(ifp
, flags
,
1159 (struct in6_addr
*)addr
,
1160 (struct in6_addr
*)broad
,
1161 ifa
->ifa_prefixlen
, label
,
1164 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1165 NULL
, ifa
->ifa_prefixlen
);
1170 * Linux kernel does not send route delete on interface down/addr del
1171 * so we have to re-process routes it owns (i.e. kernel routes)
1173 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1174 rib_update(RIB_UPDATE_KERNEL
);
1179 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1182 struct ifinfomsg
*ifi
;
1183 struct rtattr
*tb
[IFLA_MAX
+ 1];
1184 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1185 struct interface
*ifp
;
1189 char *slave_kind
= NULL
;
1190 struct zebra_ns
*zns
;
1191 vrf_id_t vrf_id
= VRF_DEFAULT
;
1192 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1193 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1194 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1195 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1196 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1197 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1198 struct zebra_if
*zif
;
1199 ns_id_t link_nsid
= ns_id
;
1201 zns
= zebra_ns_lookup(ns_id
);
1202 ifi
= NLMSG_DATA(h
);
1204 /* assume if not default zns, then new VRF */
1205 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1206 /* If this is not link add/delete message so print warning. */
1207 zlog_debug("netlink_link_change: wrong kernel message %s",
1208 nl_msg_type_to_str(h
->nlmsg_type
));
1212 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1213 || ifi
->ifi_family
== AF_INET6
)) {
1215 EC_ZEBRA_UNKNOWN_FAMILY
,
1216 "Invalid address family: %u received from kernel link change: %s",
1217 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1221 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1224 "%s: Message received from netlink is of a broken size %d %zu",
1225 __func__
, h
->nlmsg_len
,
1226 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1230 /* We are interested in some AF_BRIDGE notifications. */
1231 if (ifi
->ifi_family
== AF_BRIDGE
)
1232 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1234 /* Looking up interface name. */
1235 memset(tb
, 0, sizeof(tb
));
1236 memset(linkinfo
, 0, sizeof(linkinfo
));
1237 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1239 /* check for wireless messages to ignore */
1240 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1241 if (IS_ZEBRA_DEBUG_KERNEL
)
1242 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1247 if (tb
[IFLA_IFNAME
] == NULL
)
1249 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1251 if (tb
[IFLA_LINKINFO
]) {
1252 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1254 if (linkinfo
[IFLA_INFO_KIND
])
1255 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1257 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1258 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1260 netlink_determine_zebra_iftype(kind
, &zif_type
);
1263 /* If linking to another interface, note it. */
1265 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1267 if (tb
[IFLA_LINK_NETNSID
]) {
1268 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1269 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1271 if (tb
[IFLA_IFALIAS
]) {
1272 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1275 /* If VRF, create or update the VRF structure itself. */
1276 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1277 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1278 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1281 /* See if interface is present. */
1282 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1284 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1285 if (tb
[IFLA_MASTER
]) {
1286 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1287 && !vrf_is_backend_netns()) {
1288 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1289 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1290 } else if (slave_kind
1291 && (strcmp(slave_kind
, "bridge") == 0)) {
1292 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1294 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1295 } else if (slave_kind
1296 && (strcmp(slave_kind
, "bond") == 0)) {
1297 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1299 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1301 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1303 if (vrf_is_backend_netns())
1304 vrf_id
= (vrf_id_t
)ns_id
;
1306 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1307 /* Add interface notification from kernel */
1308 if (IS_ZEBRA_DEBUG_KERNEL
)
1310 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1311 "sl_type %d master %u flags 0x%x",
1312 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1313 zif_slave_type
, bridge_ifindex
,
1317 /* unknown interface */
1318 ifp
= if_get_by_name(name
, vrf_id
);
1320 /* pre-configured interface, learnt now */
1321 if (ifp
->vrf_id
!= vrf_id
)
1322 if_update_to_new_vrf(ifp
, vrf_id
);
1325 /* Update interface information. */
1326 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1327 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1328 if (!tb
[IFLA_MTU
]) {
1330 "RTM_NEWLINK for interface %s(%u) without MTU set",
1331 name
, ifi
->ifi_index
);
1334 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1336 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1338 /* Set interface type */
1339 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1340 if (IS_ZEBRA_IF_VRF(ifp
))
1341 SET_FLAG(ifp
->status
,
1342 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1345 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1347 netlink_interface_update_hw_addr(tb
, ifp
);
1349 /* Inform clients, install any configured addresses. */
1352 /* Extract and save L2 interface information, take
1353 * additional actions. */
1354 netlink_interface_update_l2info(
1355 ifp
, linkinfo
[IFLA_INFO_DATA
],
1357 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1358 zebra_l2if_update_bridge_slave(ifp
,
1361 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1362 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1363 } else if (ifp
->vrf_id
!= vrf_id
) {
1364 /* VRF change for an interface. */
1365 if (IS_ZEBRA_DEBUG_KERNEL
)
1367 "RTM_NEWLINK vrf-change for %s(%u) "
1368 "vrf_id %u -> %u flags 0x%x",
1369 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1372 if_handle_vrf_change(ifp
, vrf_id
);
1374 bool was_bridge_slave
, was_bond_slave
;
1376 /* Interface update. */
1377 if (IS_ZEBRA_DEBUG_KERNEL
)
1379 "RTM_NEWLINK update for %s(%u) "
1380 "sl_type %d master %u flags 0x%x",
1381 name
, ifp
->ifindex
, zif_slave_type
,
1382 bridge_ifindex
, ifi
->ifi_flags
);
1384 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1385 if (!tb
[IFLA_MTU
]) {
1387 "RTM_NEWLINK for interface %s(%u) without MTU set",
1388 name
, ifi
->ifi_index
);
1391 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1394 /* Update interface type - NOTE: Only slave_type can
1396 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1397 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
1398 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1400 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
1402 netlink_interface_update_hw_addr(tb
, ifp
);
1404 if (if_is_no_ptm_operative(ifp
)) {
1405 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1406 if (!if_is_no_ptm_operative(ifp
)) {
1407 if (IS_ZEBRA_DEBUG_KERNEL
)
1409 "Intf %s(%u) has gone DOWN",
1410 name
, ifp
->ifindex
);
1412 rib_update(RIB_UPDATE_KERNEL
);
1413 } else if (if_is_operative(ifp
)) {
1414 /* Must notify client daemons of new
1415 * interface status. */
1416 if (IS_ZEBRA_DEBUG_KERNEL
)
1418 "Intf %s(%u) PTM up, notifying clients",
1419 name
, ifp
->ifindex
);
1420 zebra_interface_up_update(ifp
);
1422 /* Update EVPN VNI when SVI MAC change
1424 if (IS_ZEBRA_IF_VLAN(ifp
) &&
1425 memcmp(old_hw_addr
, ifp
->hw_addr
,
1426 INTERFACE_HWADDR_MAX
)) {
1427 struct interface
*link_if
;
1430 if_lookup_by_index_per_ns(
1431 zebra_ns_lookup(NS_DEFAULT
),
1434 zebra_vxlan_svi_up(ifp
,
1439 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1440 if (if_is_operative(ifp
)) {
1441 if (IS_ZEBRA_DEBUG_KERNEL
)
1443 "Intf %s(%u) has come UP",
1444 name
, ifp
->ifindex
);
1447 if (IS_ZEBRA_DEBUG_KERNEL
)
1449 "Intf %s(%u) has gone DOWN",
1450 name
, ifp
->ifindex
);
1452 rib_update(RIB_UPDATE_KERNEL
);
1456 /* Extract and save L2 interface information, take
1457 * additional actions. */
1458 netlink_interface_update_l2info(
1459 ifp
, linkinfo
[IFLA_INFO_DATA
],
1461 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1462 zebra_l2if_update_bridge_slave(ifp
,
1465 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
1466 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1471 XFREE(MTYPE_TMP
, zif
->desc
);
1473 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1476 /* Delete interface notification from kernel */
1478 if (IS_ZEBRA_DEBUG_KERNEL
)
1480 "RTM_DELLINK for unknown interface %s(%u)",
1481 name
, ifi
->ifi_index
);
1485 if (IS_ZEBRA_DEBUG_KERNEL
)
1486 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1489 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1491 /* Special handling for bridge or VxLAN interfaces. */
1492 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1493 zebra_l2_bridge_del(ifp
);
1494 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1495 zebra_l2_vxlanif_del(ifp
);
1497 if (!IS_ZEBRA_IF_VRF(ifp
))
1498 if_delete_update(ifp
);
1504 int netlink_protodown(struct interface
*ifp
, bool down
)
1506 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1510 struct ifinfomsg ifa
;
1511 char buf
[NL_PKT_BUF_SIZE
];
1514 memset(&req
, 0, sizeof(req
));
1516 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1517 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1518 req
.n
.nlmsg_type
= RTM_SETLINK
;
1519 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1521 req
.ifa
.ifi_index
= ifp
->ifindex
;
1523 addattr_l(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN
, &down
, sizeof(down
));
1524 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &ifp
->ifindex
, 4);
1526 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1530 /* Interface information read by netlink. */
1531 void interface_list(struct zebra_ns
*zns
)
1533 interface_lookup_netlink(zns
);
1534 /* We add routes for interface address,
1535 * so we need to get the nexthop info
1536 * from the kernel before we can do that
1538 netlink_nexthop_read(zns
);
1540 interface_addr_lookup_netlink(zns
);
1543 #endif /* GNU_LINUX */