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 from VXLAN IF message");
519 *(ifindex_t
*)RTA_DATA(attr
[IFLA_VXLAN_LINK
]);
520 vxl_info
->ifindex_link
= ifindex_link
;
526 * Extract and save L2 params (of interest) for an interface. When a
527 * bridge interface is added or updated, take further actions to map
528 * its members. Likewise, for VxLAN interface.
530 static void netlink_interface_update_l2info(struct interface
*ifp
,
531 struct rtattr
*link_data
, int add
,
537 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
538 struct zebra_l2info_bridge bridge_info
;
540 netlink_extract_bridge_info(link_data
, &bridge_info
);
541 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
542 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
543 struct zebra_l2info_vlan vlan_info
;
545 netlink_extract_vlan_info(link_data
, &vlan_info
);
546 zebra_l2_vlanif_update(ifp
, &vlan_info
);
547 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
548 struct zebra_l2info_vxlan vxlan_info
;
550 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
551 vxlan_info
.link_nsid
= link_nsid
;
552 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
553 if (link_nsid
!= NS_UNKNOWN
&&
554 vxlan_info
.ifindex_link
)
555 zebra_if_update_link(ifp
, vxlan_info
.ifindex_link
,
560 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
564 struct ifinfomsg
*ifi
;
565 struct rtattr
*tb
[IFLA_MAX
+ 1];
566 struct interface
*ifp
;
567 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
572 vlanid_t access_vlan
;
574 /* Fetch name and ifindex */
576 memset(tb
, 0, sizeof(tb
));
577 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
579 if (tb
[IFLA_IFNAME
] == NULL
)
581 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
583 /* The interface should already be known, if not discard. */
584 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
586 zlog_debug("Cannot find bridge IF %s(%u)", name
,
590 if (!IS_ZEBRA_IF_VXLAN(ifp
))
593 /* We are only interested in the access VLAN i.e., AF_SPEC */
594 if (!tb
[IFLA_AF_SPEC
])
597 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
598 * only 1 access VLAN is accepted.
600 memset(aftb
, 0, sizeof(aftb
));
601 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
602 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
605 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
606 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
609 access_vlan
= (vlanid_t
)vinfo
->vid
;
610 if (IS_ZEBRA_DEBUG_KERNEL
)
611 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
612 name
, ifi
->ifi_index
);
613 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
618 * Called from interface_lookup_netlink(). This function is only used
621 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
624 struct ifinfomsg
*ifi
;
625 struct rtattr
*tb
[IFLA_MAX
+ 1];
626 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
627 struct interface
*ifp
;
631 char *slave_kind
= NULL
;
632 struct zebra_ns
*zns
= NULL
;
633 vrf_id_t vrf_id
= VRF_DEFAULT
;
634 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
635 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
636 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
637 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
638 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
639 struct zebra_if
*zif
;
640 ns_id_t link_nsid
= ns_id
;
642 zns
= zebra_ns_lookup(ns_id
);
645 if (h
->nlmsg_type
!= RTM_NEWLINK
)
648 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
651 "%s: Message received from netlink is of a broken size: %d %zu",
652 __func__
, h
->nlmsg_len
,
653 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
657 /* We are interested in some AF_BRIDGE notifications. */
658 if (ifi
->ifi_family
== AF_BRIDGE
)
659 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
661 /* Looking up interface name. */
662 memset(tb
, 0, sizeof(tb
));
663 memset(linkinfo
, 0, sizeof(linkinfo
));
664 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
666 /* check for wireless messages to ignore */
667 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
668 if (IS_ZEBRA_DEBUG_KERNEL
)
669 zlog_debug("%s: ignoring IFLA_WIRELESS message",
674 if (tb
[IFLA_IFNAME
] == NULL
)
676 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
678 if (tb
[IFLA_IFALIAS
])
679 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
681 if (tb
[IFLA_LINKINFO
]) {
682 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
684 if (linkinfo
[IFLA_INFO_KIND
])
685 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
687 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
688 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
690 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
691 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
693 netlink_determine_zebra_iftype(kind
, &zif_type
);
696 /* If VRF, create the VRF structure itself. */
697 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
698 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
699 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
702 if (tb
[IFLA_MASTER
]) {
703 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
704 && !vrf_is_backend_netns()) {
705 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
706 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
707 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
708 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
710 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
711 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
712 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
713 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
715 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
717 if (vrf_is_backend_netns())
718 vrf_id
= (vrf_id_t
)ns_id
;
720 /* If linking to another interface, note it. */
722 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
724 if (tb
[IFLA_LINK_NETNSID
]) {
725 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
726 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
730 * We add by index first because in some cases such as the master
731 * interface, we have the index before we have the name. Fixing
732 * back references on the slave interfaces is painful if not done
733 * this way, i.e. by creating by ifindex.
735 ifp
= if_get_by_ifindex(ifi
->ifi_index
, vrf_id
, name
);
736 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
738 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
739 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
741 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
742 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
744 /* Set zebra interface type */
745 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
746 if (IS_ZEBRA_IF_VRF(ifp
))
747 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
750 * Just set the @link/lower-device ifindex. During nldump interfaces are
751 * not ordered in any fashion so we may end up getting upper devices
752 * before lower devices. We will setup the real linkage once the dump
755 zif
= (struct zebra_if
*)ifp
->info
;
756 zif
->link_ifindex
= link_ifindex
;
759 XFREE(MTYPE_TMP
, zif
->desc
);
760 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
763 /* Hardware type and address. */
764 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
765 netlink_interface_update_hw_addr(tb
, ifp
);
769 /* Extract and save L2 interface information, take additional actions.
771 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
],
773 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
774 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
, ns_id
);
775 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
776 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
781 /* Request for specific interface or address information from the kernel */
782 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
783 int type
, uint32_t filter_mask
)
787 struct ifinfomsg ifm
;
791 /* Form the request, specifying filter (rtattr) if needed. */
792 memset(&req
, 0, sizeof(req
));
793 req
.n
.nlmsg_type
= type
;
794 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
795 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
796 req
.ifm
.ifi_family
= family
;
798 /* Include filter, if specified. */
800 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
802 return netlink_request(netlink_cmd
, &req
);
805 /* Interface lookup by netlink socket. */
806 int interface_lookup_netlink(struct zebra_ns
*zns
)
809 struct zebra_dplane_info dp_info
;
810 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
812 /* Capture key info from ns struct */
813 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
815 /* Get interface information. */
816 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
819 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
824 /* Get interface information - for bridge interfaces. */
825 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
826 RTEXT_FILTER_BRVLAN
);
829 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
834 /* Get interface information - for bridge interfaces. */
835 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
836 RTEXT_FILTER_BRVLAN
);
839 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
845 zebra_if_update_all_links();
850 * interface_addr_lookup_netlink() - Look up interface addresses
852 * @zns: Zebra netlink socket
853 * Return: Result status
855 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
858 struct zebra_dplane_info dp_info
;
859 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
861 /* Capture key info from ns struct */
862 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
864 /* Get IPv4 address of the interfaces. */
865 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
868 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
873 /* Get IPv6 address of the interfaces. */
874 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
877 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
885 int kernel_interface_set_master(struct interface
*master
,
886 struct interface
*slave
)
888 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
892 struct ifinfomsg ifa
;
893 char buf
[NL_PKT_BUF_SIZE
];
896 memset(&req
, 0, sizeof(req
));
898 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
899 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
900 req
.n
.nlmsg_type
= RTM_SETLINK
;
901 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
903 req
.ifa
.ifi_index
= slave
->ifindex
;
905 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_MASTER
, master
->ifindex
);
906 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, slave
->ifindex
);
908 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
912 /* Interface address modification. */
913 static int netlink_address_ctx(const struct zebra_dplane_ctx
*ctx
)
916 const struct prefix
*p
;
922 struct ifaddrmsg ifa
;
923 char buf
[NL_PKT_BUF_SIZE
];
926 p
= dplane_ctx_get_intf_addr(ctx
);
927 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
929 bytelen
= (p
->family
== AF_INET
? 4 : 16);
931 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
932 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
934 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
939 req
.n
.nlmsg_type
= cmd
;
940 req
.ifa
.ifa_family
= p
->family
;
942 req
.ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
944 nl_attr_put(&req
.n
, sizeof(req
), IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
946 if (p
->family
== AF_INET
) {
947 if (dplane_ctx_intf_is_connected(ctx
)) {
948 p
= dplane_ctx_get_intf_dest(ctx
);
949 nl_attr_put(&req
.n
, sizeof(req
), IFA_ADDRESS
,
950 &p
->u
.prefix
, bytelen
);
951 } else if (cmd
== RTM_NEWADDR
) {
952 struct in_addr broad
= {
953 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
956 nl_attr_put(&req
.n
, sizeof(req
), IFA_BROADCAST
, &broad
,
961 /* p is now either address or destination/bcast addr */
962 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
964 if (dplane_ctx_intf_is_secondary(ctx
))
965 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
967 if (dplane_ctx_intf_has_label(ctx
)) {
968 label
= dplane_ctx_get_intf_label(ctx
);
969 nl_attr_put(&req
.n
, sizeof(req
), IFA_LABEL
, label
,
973 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
974 dplane_ctx_get_ns(ctx
), 0);
977 enum zebra_dplane_result
kernel_address_update_ctx(struct zebra_dplane_ctx
*ctx
)
979 return (netlink_address_ctx(ctx
) == 0 ?
980 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
983 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
986 struct ifaddrmsg
*ifa
;
987 struct rtattr
*tb
[IFA_MAX
+ 1];
988 struct interface
*ifp
;
993 struct zebra_ns
*zns
;
994 uint32_t metric
= METRIC_MAX
;
995 uint32_t kernel_flags
= 0;
997 zns
= zebra_ns_lookup(ns_id
);
1000 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
1002 EC_ZEBRA_UNKNOWN_FAMILY
,
1003 "Invalid address family: %u received from kernel interface addr change: %s",
1004 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1008 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
1011 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1014 "%s: Message received from netlink is of a broken size: %d %zu",
1015 __func__
, h
->nlmsg_len
,
1016 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
1020 memset(tb
, 0, sizeof(tb
));
1021 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
1023 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
1027 "netlink_interface_addr can't find interface by index %d",
1032 /* Flags passed through */
1034 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1036 kernel_flags
= ifa
->ifa_flags
;
1038 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1041 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1042 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1045 zlog_debug(" IFA_LOCAL %s/%d",
1046 inet_ntop(ifa
->ifa_family
,
1047 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1049 ifa
->ifa_prefixlen
);
1050 if (tb
[IFA_ADDRESS
])
1051 zlog_debug(" IFA_ADDRESS %s/%d",
1052 inet_ntop(ifa
->ifa_family
,
1053 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1055 ifa
->ifa_prefixlen
);
1056 if (tb
[IFA_BROADCAST
])
1057 zlog_debug(" IFA_BROADCAST %s/%d",
1058 inet_ntop(ifa
->ifa_family
,
1059 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1061 ifa
->ifa_prefixlen
);
1062 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1063 zlog_debug(" IFA_LABEL %s",
1064 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1066 if (tb
[IFA_CACHEINFO
]) {
1067 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1068 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1069 ci
->ifa_prefered
, ci
->ifa_valid
);
1073 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1074 if (tb
[IFA_LOCAL
] == NULL
)
1075 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1076 if (tb
[IFA_ADDRESS
] == NULL
)
1077 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1079 /* local interface address */
1080 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1082 /* is there a peer address? */
1084 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1085 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1086 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1087 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1089 /* seeking a broadcast address */
1090 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1093 /* addr is primary key, SOL if we don't have one */
1095 zlog_debug("%s: Local Interface Address is NULL for %s",
1096 __func__
, ifp
->name
);
1101 if (kernel_flags
& IFA_F_SECONDARY
)
1102 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1106 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1108 if (label
&& strcmp(ifp
->name
, label
) == 0)
1111 if (tb
[IFA_RT_PRIORITY
])
1112 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1114 /* Register interface address to the interface. */
1115 if (ifa
->ifa_family
== AF_INET
) {
1116 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1118 "Invalid prefix length: %u received from kernel interface addr change: %s",
1120 nl_msg_type_to_str(h
->nlmsg_type
));
1124 if (h
->nlmsg_type
== RTM_NEWADDR
)
1125 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1127 (struct in_addr
*)broad
, label
,
1129 else if (CHECK_FLAG(flags
, ZEBRA_IFA_PEER
)) {
1130 /* Delete with a peer address */
1131 connected_delete_ipv4(
1132 ifp
, flags
, (struct in_addr
*)addr
,
1133 ifa
->ifa_prefixlen
, broad
);
1135 connected_delete_ipv4(
1136 ifp
, flags
, (struct in_addr
*)addr
,
1137 ifa
->ifa_prefixlen
, NULL
);
1140 if (ifa
->ifa_family
== AF_INET6
) {
1141 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1143 "Invalid prefix length: %u received from kernel interface addr change: %s",
1145 nl_msg_type_to_str(h
->nlmsg_type
));
1148 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1149 /* Only consider valid addresses; we'll not get a
1151 * the kernel till IPv6 DAD has completed, but at init
1153 * does query for and will receive all addresses.
1156 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1157 connected_add_ipv6(ifp
, flags
,
1158 (struct in6_addr
*)addr
,
1159 (struct in6_addr
*)broad
,
1160 ifa
->ifa_prefixlen
, label
,
1163 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1164 NULL
, ifa
->ifa_prefixlen
);
1169 * Linux kernel does not send route delete on interface down/addr del
1170 * so we have to re-process routes it owns (i.e. kernel routes)
1172 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1173 rib_update(RIB_UPDATE_KERNEL
);
1178 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1181 struct ifinfomsg
*ifi
;
1182 struct rtattr
*tb
[IFLA_MAX
+ 1];
1183 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1184 struct interface
*ifp
;
1188 char *slave_kind
= NULL
;
1189 struct zebra_ns
*zns
;
1190 vrf_id_t vrf_id
= VRF_DEFAULT
;
1191 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1192 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1193 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1194 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1195 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1196 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1197 struct zebra_if
*zif
;
1198 ns_id_t link_nsid
= ns_id
;
1200 zns
= zebra_ns_lookup(ns_id
);
1201 ifi
= NLMSG_DATA(h
);
1203 /* assume if not default zns, then new VRF */
1204 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1205 /* If this is not link add/delete message so print warning. */
1206 zlog_debug("netlink_link_change: wrong kernel message %s",
1207 nl_msg_type_to_str(h
->nlmsg_type
));
1211 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1212 || ifi
->ifi_family
== AF_INET6
)) {
1214 EC_ZEBRA_UNKNOWN_FAMILY
,
1215 "Invalid address family: %u received from kernel link change: %s",
1216 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1220 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1223 "%s: Message received from netlink is of a broken size %d %zu",
1224 __func__
, h
->nlmsg_len
,
1225 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1229 /* We are interested in some AF_BRIDGE notifications. */
1230 if (ifi
->ifi_family
== AF_BRIDGE
)
1231 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1233 /* Looking up interface name. */
1234 memset(tb
, 0, sizeof(tb
));
1235 memset(linkinfo
, 0, sizeof(linkinfo
));
1236 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1238 /* check for wireless messages to ignore */
1239 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1240 if (IS_ZEBRA_DEBUG_KERNEL
)
1241 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1246 if (tb
[IFLA_IFNAME
] == NULL
)
1248 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1250 if (tb
[IFLA_LINKINFO
]) {
1251 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1253 if (linkinfo
[IFLA_INFO_KIND
])
1254 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1256 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1257 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1259 netlink_determine_zebra_iftype(kind
, &zif_type
);
1262 /* If linking to another interface, note it. */
1264 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1266 if (tb
[IFLA_LINK_NETNSID
]) {
1267 link_nsid
= *(ns_id_t
*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
1268 link_nsid
= ns_id_get_absolute(ns_id
, link_nsid
);
1270 if (tb
[IFLA_IFALIAS
]) {
1271 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1274 /* If VRF, create or update the VRF structure itself. */
1275 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1276 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], ns_id
, name
);
1277 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1280 /* See if interface is present. */
1281 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1283 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1284 if (tb
[IFLA_MASTER
]) {
1285 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1286 && !vrf_is_backend_netns()) {
1287 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1288 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1289 } else if (slave_kind
1290 && (strcmp(slave_kind
, "bridge") == 0)) {
1291 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1293 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1294 } else if (slave_kind
1295 && (strcmp(slave_kind
, "bond") == 0)) {
1296 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1298 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1300 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1302 if (vrf_is_backend_netns())
1303 vrf_id
= (vrf_id_t
)ns_id
;
1305 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1306 /* Add interface notification from kernel */
1307 if (IS_ZEBRA_DEBUG_KERNEL
)
1309 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1310 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1311 zif_slave_type
, bridge_ifindex
,
1315 /* unknown interface */
1316 ifp
= if_get_by_name(name
, vrf_id
);
1318 /* pre-configured interface, learnt now */
1319 if (ifp
->vrf_id
!= vrf_id
)
1320 if_update_to_new_vrf(ifp
, vrf_id
);
1323 /* Update interface information. */
1324 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1325 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1326 if (!tb
[IFLA_MTU
]) {
1328 "RTM_NEWLINK for interface %s(%u) without MTU set",
1329 name
, ifi
->ifi_index
);
1332 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1334 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1336 /* Set interface type */
1337 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1338 if (IS_ZEBRA_IF_VRF(ifp
))
1339 SET_FLAG(ifp
->status
,
1340 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1343 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1345 netlink_interface_update_hw_addr(tb
, ifp
);
1347 /* Inform clients, install any configured addresses. */
1350 /* Extract and save L2 interface information, take
1351 * additional actions. */
1352 netlink_interface_update_l2info(
1353 ifp
, linkinfo
[IFLA_INFO_DATA
],
1355 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1356 zebra_l2if_update_bridge_slave(ifp
,
1359 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1360 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1361 } else if (ifp
->vrf_id
!= vrf_id
) {
1362 /* VRF change for an interface. */
1363 if (IS_ZEBRA_DEBUG_KERNEL
)
1365 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1366 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1369 if_handle_vrf_change(ifp
, vrf_id
);
1371 bool was_bridge_slave
, was_bond_slave
;
1373 /* Interface update. */
1374 if (IS_ZEBRA_DEBUG_KERNEL
)
1376 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1377 name
, ifp
->ifindex
, zif_slave_type
,
1378 bridge_ifindex
, ifi
->ifi_flags
);
1380 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1381 if (!tb
[IFLA_MTU
]) {
1383 "RTM_NEWLINK for interface %s(%u) without MTU set",
1384 name
, ifi
->ifi_index
);
1387 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1390 /* Update interface type - NOTE: Only slave_type can
1392 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1393 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
1394 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1396 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
1398 netlink_interface_update_hw_addr(tb
, ifp
);
1400 if (if_is_no_ptm_operative(ifp
)) {
1401 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1402 if (!if_is_no_ptm_operative(ifp
)) {
1403 if (IS_ZEBRA_DEBUG_KERNEL
)
1405 "Intf %s(%u) has gone DOWN",
1406 name
, ifp
->ifindex
);
1408 rib_update(RIB_UPDATE_KERNEL
);
1409 } else if (if_is_operative(ifp
)) {
1410 /* Must notify client daemons of new
1411 * interface status. */
1412 if (IS_ZEBRA_DEBUG_KERNEL
)
1414 "Intf %s(%u) PTM up, notifying clients",
1415 name
, ifp
->ifindex
);
1416 zebra_interface_up_update(ifp
);
1418 /* Update EVPN VNI when SVI MAC change
1420 if (IS_ZEBRA_IF_VLAN(ifp
) &&
1421 memcmp(old_hw_addr
, ifp
->hw_addr
,
1422 INTERFACE_HWADDR_MAX
)) {
1423 struct interface
*link_if
;
1426 if_lookup_by_index_per_ns(
1427 zebra_ns_lookup(NS_DEFAULT
),
1430 zebra_vxlan_svi_up(ifp
,
1435 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1436 if (if_is_operative(ifp
)) {
1437 if (IS_ZEBRA_DEBUG_KERNEL
)
1439 "Intf %s(%u) has come UP",
1440 name
, ifp
->ifindex
);
1443 if (IS_ZEBRA_DEBUG_KERNEL
)
1445 "Intf %s(%u) has gone DOWN",
1446 name
, ifp
->ifindex
);
1448 rib_update(RIB_UPDATE_KERNEL
);
1452 /* Extract and save L2 interface information, take
1453 * additional actions. */
1454 netlink_interface_update_l2info(
1455 ifp
, linkinfo
[IFLA_INFO_DATA
],
1457 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1458 zebra_l2if_update_bridge_slave(ifp
,
1461 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
1462 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1467 XFREE(MTYPE_TMP
, zif
->desc
);
1469 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1472 /* Delete interface notification from kernel */
1474 if (IS_ZEBRA_DEBUG_KERNEL
)
1476 "RTM_DELLINK for unknown interface %s(%u)",
1477 name
, ifi
->ifi_index
);
1481 if (IS_ZEBRA_DEBUG_KERNEL
)
1482 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1485 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1487 /* Special handling for bridge or VxLAN interfaces. */
1488 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1489 zebra_l2_bridge_del(ifp
);
1490 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1491 zebra_l2_vxlanif_del(ifp
);
1493 if (!IS_ZEBRA_IF_VRF(ifp
))
1494 if_delete_update(ifp
);
1500 int netlink_protodown(struct interface
*ifp
, bool down
)
1502 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1506 struct ifinfomsg ifa
;
1507 char buf
[NL_PKT_BUF_SIZE
];
1510 memset(&req
, 0, sizeof(req
));
1512 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1513 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1514 req
.n
.nlmsg_type
= RTM_SETLINK
;
1515 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1517 req
.ifa
.ifi_index
= ifp
->ifindex
;
1519 nl_attr_put(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN
, &down
, sizeof(down
));
1520 nl_attr_put32(&req
.n
, sizeof(req
), IFLA_LINK
, ifp
->ifindex
);
1522 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1526 /* Interface information read by netlink. */
1527 void interface_list(struct zebra_ns
*zns
)
1529 interface_lookup_netlink(zns
);
1530 /* We add routes for interface address,
1531 * so we need to get the nexthop info
1532 * from the kernel before we can do that
1534 netlink_nexthop_read(zns
);
1536 interface_addr_lookup_netlink(zns
);
1539 #endif /* GNU_LINUX */