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
,
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
);
318 * vrf_get is implied creation if it does not exist
320 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
321 name
); // It would create vrf
323 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
324 name
, ifi
->ifi_index
);
329 * This is the only place that we get the actual kernel table_id
330 * being used. We need it to set the table_id of the routes
331 * we are passing to the kernel.... And to throw some totally
332 * awesome parties. that too.
334 * At this point we *must* have a zvrf because the vrf_create
335 * callback creates one. We *must* set the table id
336 * before the vrf_enable because of( at the very least )
337 * static routes being delayed for installation until
338 * during the vrf_enable callbacks.
340 zvrf
= (struct zebra_vrf
*)vrf
->info
;
341 zvrf
->table_id
= nl_table_id
;
343 /* Enable the created VRF. */
344 if (!vrf_enable(vrf
)) {
345 flog_err(EC_LIB_INTERFACE
,
346 "Failed to enable VRF %s id %u", name
,
351 } else // h->nlmsg_type == RTM_DELLINK
353 if (IS_ZEBRA_DEBUG_KERNEL
)
354 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
357 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
360 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
369 static uint32_t get_iflink_speed(struct interface
*interface
, int *error
)
372 struct ethtool_cmd ecmd
;
375 const char *ifname
= interface
->name
;
379 /* initialize struct */
380 memset(&ifdata
, 0, sizeof(ifdata
));
382 /* set interface name */
383 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
385 /* initialize ethtool interface */
386 memset(&ecmd
, 0, sizeof(ecmd
));
387 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
388 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
390 /* use ioctl to get IP address of an interface */
391 frr_with_privs(&zserv_privs
) {
392 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
396 if (IS_ZEBRA_DEBUG_KERNEL
)
397 zlog_debug("Failure to read interface %s speed: %d %s",
398 ifname
, errno
, safe_strerror(errno
));
399 /* no vrf socket creation may probably mean vrf issue */
404 /* Get the current link state for the interface */
405 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
,
409 if (errno
!= EOPNOTSUPP
&& IS_ZEBRA_DEBUG_KERNEL
)
411 "IOCTL failure to read interface %s speed: %d %s",
412 ifname
, errno
, safe_strerror(errno
));
413 /* no device means interface unreachable */
414 if (errno
== ENODEV
&& error
)
422 return ((uint32_t)ecmd
.speed_hi
<< 16) | ecmd
.speed
;
425 uint32_t kernel_get_speed(struct interface
*ifp
, int *error
)
427 return get_iflink_speed(ifp
, error
);
430 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
431 struct zebra_l2info_bridge
*bridge_info
)
433 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
435 memset(bridge_info
, 0, sizeof(*bridge_info
));
436 memset(attr
, 0, sizeof(attr
));
437 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
438 if (attr
[IFLA_BR_VLAN_FILTERING
])
439 bridge_info
->vlan_aware
=
440 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
444 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
445 struct zebra_l2info_vlan
*vlan_info
)
447 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
450 memset(vlan_info
, 0, sizeof(*vlan_info
));
451 memset(attr
, 0, sizeof(attr
));
452 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
453 if (!attr
[IFLA_VLAN_ID
]) {
454 if (IS_ZEBRA_DEBUG_KERNEL
)
455 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
459 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
460 vlan_info
->vid
= vid_in_msg
;
464 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
465 struct zebra_l2info_vxlan
*vxl_info
)
467 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
469 struct in_addr vtep_ip_in_msg
;
471 memset(vxl_info
, 0, sizeof(*vxl_info
));
472 memset(attr
, 0, sizeof(attr
));
473 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
474 if (!attr
[IFLA_VXLAN_ID
]) {
475 if (IS_ZEBRA_DEBUG_KERNEL
)
477 "IFLA_VXLAN_ID missing from VXLAN IF message");
481 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
482 vxl_info
->vni
= vni_in_msg
;
483 if (!attr
[IFLA_VXLAN_LOCAL
]) {
484 if (IS_ZEBRA_DEBUG_KERNEL
)
486 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
489 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
490 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
493 if (attr
[IFLA_VXLAN_GROUP
]) {
494 vxl_info
->mcast_grp
=
495 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_GROUP
]);
502 * Extract and save L2 params (of interest) for an interface. When a
503 * bridge interface is added or updated, take further actions to map
504 * its members. Likewise, for VxLAN interface.
506 static void netlink_interface_update_l2info(struct interface
*ifp
,
507 struct rtattr
*link_data
, int add
)
512 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
513 struct zebra_l2info_bridge bridge_info
;
515 netlink_extract_bridge_info(link_data
, &bridge_info
);
516 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
517 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
518 struct zebra_l2info_vlan vlan_info
;
520 netlink_extract_vlan_info(link_data
, &vlan_info
);
521 zebra_l2_vlanif_update(ifp
, &vlan_info
);
522 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
523 struct zebra_l2info_vxlan vxlan_info
;
525 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
526 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
530 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
534 struct ifinfomsg
*ifi
;
535 struct rtattr
*tb
[IFLA_MAX
+ 1];
536 struct interface
*ifp
;
537 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
542 vlanid_t access_vlan
;
544 /* Fetch name and ifindex */
546 memset(tb
, 0, sizeof(tb
));
547 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
549 if (tb
[IFLA_IFNAME
] == NULL
)
551 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
553 /* The interface should already be known, if not discard. */
554 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
556 zlog_debug("Cannot find bridge IF %s(%u)", name
,
560 if (!IS_ZEBRA_IF_VXLAN(ifp
))
563 /* We are only interested in the access VLAN i.e., AF_SPEC */
564 if (!tb
[IFLA_AF_SPEC
])
567 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
568 * only 1 access VLAN is accepted.
570 memset(aftb
, 0, sizeof(aftb
));
571 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
572 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
575 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
576 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
579 access_vlan
= (vlanid_t
)vinfo
->vid
;
580 if (IS_ZEBRA_DEBUG_KERNEL
)
581 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
582 name
, ifi
->ifi_index
);
583 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
588 * Called from interface_lookup_netlink(). This function is only used
591 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
594 struct ifinfomsg
*ifi
;
595 struct rtattr
*tb
[IFLA_MAX
+ 1];
596 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
597 struct interface
*ifp
;
601 char *slave_kind
= NULL
;
602 struct zebra_ns
*zns
= NULL
;
603 vrf_id_t vrf_id
= VRF_DEFAULT
;
604 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
605 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
606 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
607 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
608 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
609 struct zebra_if
*zif
;
611 zns
= zebra_ns_lookup(ns_id
);
614 if (h
->nlmsg_type
!= RTM_NEWLINK
)
617 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
620 "%s: Message received from netlink is of a broken size: %d %zu",
621 __func__
, h
->nlmsg_len
,
622 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
626 /* We are interested in some AF_BRIDGE notifications. */
627 if (ifi
->ifi_family
== AF_BRIDGE
)
628 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
630 /* Looking up interface name. */
631 memset(tb
, 0, sizeof(tb
));
632 memset(linkinfo
, 0, sizeof(linkinfo
));
633 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
635 /* check for wireless messages to ignore */
636 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
637 if (IS_ZEBRA_DEBUG_KERNEL
)
638 zlog_debug("%s: ignoring IFLA_WIRELESS message",
643 if (tb
[IFLA_IFNAME
] == NULL
)
645 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
647 if (tb
[IFLA_IFALIAS
])
648 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
650 if (tb
[IFLA_LINKINFO
]) {
651 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
653 if (linkinfo
[IFLA_INFO_KIND
])
654 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
656 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
657 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
659 if ((slave_kind
!= NULL
) && strcmp(slave_kind
, "bond") == 0)
660 netlink_determine_zebra_iftype("bond_slave", &zif_type
);
662 netlink_determine_zebra_iftype(kind
, &zif_type
);
665 /* If VRF, create the VRF structure itself. */
666 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
667 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
668 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
671 if (tb
[IFLA_MASTER
]) {
672 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
673 && !vrf_is_backend_netns()) {
674 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
675 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
676 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
677 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
679 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
680 } else if (slave_kind
&& (strcmp(slave_kind
, "bond") == 0)) {
681 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
682 bond_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
684 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
686 if (vrf_is_backend_netns())
687 vrf_id
= (vrf_id_t
)ns_id
;
689 /* If linking to another interface, note it. */
691 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
694 * We add by index first because in some cases such as the master
695 * interface, we have the index before we have the name. Fixing
696 * back references on the slave interfaces is painful if not done
697 * this way, i.e. by creating by ifindex.
699 ifp
= if_get_by_ifindex(ifi
->ifi_index
, vrf_id
);
700 set_ifindex(ifp
, ifi
->ifi_index
, zns
); /* add it to ns struct */
702 if_set_name(ifp
, name
);
704 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
705 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
707 ifp
->speed
= get_iflink_speed(ifp
, NULL
);
708 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
710 /* Set zebra interface type */
711 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
712 if (IS_ZEBRA_IF_VRF(ifp
))
713 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
716 * Just set the @link/lower-device ifindex. During nldump interfaces are
717 * not ordered in any fashion so we may end up getting upper devices
718 * before lower devices. We will setup the real linkage once the dump
721 zif
= (struct zebra_if
*)ifp
->info
;
722 zif
->link_ifindex
= link_ifindex
;
725 XFREE(MTYPE_TMP
, zif
->desc
);
726 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
729 /* Hardware type and address. */
730 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
731 netlink_interface_update_hw_addr(tb
, ifp
);
735 /* Extract and save L2 interface information, take additional actions.
737 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
738 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
739 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
740 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
741 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
746 /* Request for specific interface or address information from the kernel */
747 static int netlink_request_intf_addr(struct nlsock
*netlink_cmd
, int family
,
748 int type
, uint32_t filter_mask
)
752 struct ifinfomsg ifm
;
756 /* Form the request, specifying filter (rtattr) if needed. */
757 memset(&req
, 0, sizeof(req
));
758 req
.n
.nlmsg_type
= type
;
759 req
.n
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
760 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
761 req
.ifm
.ifi_family
= family
;
763 /* Include filter, if specified. */
765 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
767 return netlink_request(netlink_cmd
, &req
.n
);
770 /* Interface lookup by netlink socket. */
771 int interface_lookup_netlink(struct zebra_ns
*zns
)
774 struct zebra_dplane_info dp_info
;
775 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
777 /* Capture key info from ns struct */
778 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
780 /* Get interface information. */
781 ret
= netlink_request_intf_addr(netlink_cmd
, AF_PACKET
, RTM_GETLINK
, 0);
784 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
789 /* Get interface information - for bridge interfaces. */
790 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
791 RTEXT_FILTER_BRVLAN
);
794 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
799 /* Get interface information - for bridge interfaces. */
800 ret
= netlink_request_intf_addr(netlink_cmd
, AF_BRIDGE
, RTM_GETLINK
,
801 RTEXT_FILTER_BRVLAN
);
804 ret
= netlink_parse_info(netlink_interface
, netlink_cmd
, &dp_info
, 0,
810 zebra_if_update_all_links();
815 * interface_addr_lookup_netlink() - Look up interface addresses
817 * @zns: Zebra netlink socket
818 * Return: Result status
820 static int interface_addr_lookup_netlink(struct zebra_ns
*zns
)
823 struct zebra_dplane_info dp_info
;
824 struct nlsock
*netlink_cmd
= &zns
->netlink_cmd
;
826 /* Capture key info from ns struct */
827 zebra_dplane_info_from_zns(&dp_info
, zns
, true /*is_cmd*/);
829 /* Get IPv4 address of the interfaces. */
830 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET
, RTM_GETADDR
, 0);
833 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
838 /* Get IPv6 address of the interfaces. */
839 ret
= netlink_request_intf_addr(netlink_cmd
, AF_INET6
, RTM_GETADDR
, 0);
842 ret
= netlink_parse_info(netlink_interface_addr
, netlink_cmd
, &dp_info
,
850 int kernel_interface_set_master(struct interface
*master
,
851 struct interface
*slave
)
853 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
857 struct ifinfomsg ifa
;
858 char buf
[NL_PKT_BUF_SIZE
];
861 memset(&req
, 0, sizeof(req
));
863 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
864 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
865 req
.n
.nlmsg_type
= RTM_SETLINK
;
866 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
868 req
.ifa
.ifi_index
= slave
->ifindex
;
870 addattr_l(&req
.n
, sizeof(req
), IFLA_MASTER
, &master
->ifindex
, 4);
871 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &slave
->ifindex
, 4);
873 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
877 /* Interface address modification. */
878 static int netlink_address_ctx(const struct zebra_dplane_ctx
*ctx
)
881 const struct prefix
*p
;
887 struct ifaddrmsg ifa
;
888 char buf
[NL_PKT_BUF_SIZE
];
891 p
= dplane_ctx_get_intf_addr(ctx
);
892 memset(&req
, 0, sizeof(req
) - NL_PKT_BUF_SIZE
);
894 bytelen
= (p
->family
== AF_INET
? 4 : 16);
896 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
897 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
899 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ADDR_INSTALL
)
904 req
.n
.nlmsg_type
= cmd
;
905 req
.ifa
.ifa_family
= p
->family
;
907 req
.ifa
.ifa_index
= dplane_ctx_get_ifindex(ctx
);
909 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
911 if (p
->family
== AF_INET
) {
912 if (dplane_ctx_intf_is_connected(ctx
)) {
913 p
= dplane_ctx_get_intf_dest(ctx
);
914 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
,
915 &p
->u
.prefix
, bytelen
);
916 } else if (cmd
== RTM_NEWADDR
) {
917 struct in_addr broad
= {
918 .s_addr
= ipv4_broadcast_addr(p
->u
.prefix4
.s_addr
,
921 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
,
926 /* p is now either address or destination/bcast addr */
927 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
929 if (dplane_ctx_intf_is_secondary(ctx
))
930 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
932 if (dplane_ctx_intf_has_label(ctx
)) {
933 label
= dplane_ctx_get_intf_label(ctx
);
934 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, label
,
938 return netlink_talk_info(netlink_talk_filter
, &req
.n
,
939 dplane_ctx_get_ns(ctx
), 0);
942 enum zebra_dplane_result
kernel_address_update_ctx(struct zebra_dplane_ctx
*ctx
)
944 return (netlink_address_ctx(ctx
) == 0 ?
945 ZEBRA_DPLANE_REQUEST_SUCCESS
: ZEBRA_DPLANE_REQUEST_FAILURE
);
948 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
951 struct ifaddrmsg
*ifa
;
952 struct rtattr
*tb
[IFA_MAX
+ 1];
953 struct interface
*ifp
;
958 struct zebra_ns
*zns
;
959 uint32_t metric
= METRIC_MAX
;
960 uint32_t kernel_flags
= 0;
962 zns
= zebra_ns_lookup(ns_id
);
965 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
967 EC_ZEBRA_UNKNOWN_FAMILY
,
968 "Invalid address family: %u received from kernel interface addr change: %s",
969 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
973 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
976 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
979 "%s: Message received from netlink is of a broken size: %d %zu",
980 __func__
, h
->nlmsg_len
,
981 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
985 memset(tb
, 0, sizeof(tb
));
986 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
988 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
992 "netlink_interface_addr can't find interface by index %d",
997 /* Flags passed through */
999 kernel_flags
= *(int *)RTA_DATA(tb
[IFA_FLAGS
]);
1001 kernel_flags
= ifa
->ifa_flags
;
1003 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
1006 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1007 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
1010 zlog_debug(" IFA_LOCAL %s/%d",
1011 inet_ntop(ifa
->ifa_family
,
1012 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
1014 ifa
->ifa_prefixlen
);
1015 if (tb
[IFA_ADDRESS
])
1016 zlog_debug(" IFA_ADDRESS %s/%d",
1017 inet_ntop(ifa
->ifa_family
,
1018 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
1020 ifa
->ifa_prefixlen
);
1021 if (tb
[IFA_BROADCAST
])
1022 zlog_debug(" IFA_BROADCAST %s/%d",
1023 inet_ntop(ifa
->ifa_family
,
1024 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
1026 ifa
->ifa_prefixlen
);
1027 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
1028 zlog_debug(" IFA_LABEL %s",
1029 (char *)RTA_DATA(tb
[IFA_LABEL
]));
1031 if (tb
[IFA_CACHEINFO
]) {
1032 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
1033 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1034 ci
->ifa_prefered
, ci
->ifa_valid
);
1038 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1039 if (tb
[IFA_LOCAL
] == NULL
)
1040 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1041 if (tb
[IFA_ADDRESS
] == NULL
)
1042 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
1044 /* local interface address */
1045 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
1047 /* is there a peer address? */
1049 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
1050 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
1051 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
1052 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
1054 /* seeking a broadcast address */
1055 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
1058 /* addr is primary key, SOL if we don't have one */
1060 zlog_debug("%s: Local Interface Address is NULL for %s",
1061 __func__
, ifp
->name
);
1066 if (kernel_flags
& IFA_F_SECONDARY
)
1067 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1071 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1073 if (label
&& strcmp(ifp
->name
, label
) == 0)
1076 if (tb
[IFA_RT_PRIORITY
])
1077 metric
= *(uint32_t *)RTA_DATA(tb
[IFA_RT_PRIORITY
]);
1079 /* Register interface address to the interface. */
1080 if (ifa
->ifa_family
== AF_INET
) {
1081 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1083 "Invalid prefix length: %u received from kernel interface addr change: %s",
1085 nl_msg_type_to_str(h
->nlmsg_type
));
1088 if (h
->nlmsg_type
== RTM_NEWADDR
)
1089 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1091 (struct in_addr
*)broad
, label
,
1094 connected_delete_ipv4(
1095 ifp
, flags
, (struct in_addr
*)addr
,
1096 ifa
->ifa_prefixlen
, NULL
);
1098 if (ifa
->ifa_family
== AF_INET6
) {
1099 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1101 "Invalid prefix length: %u received from kernel interface addr change: %s",
1103 nl_msg_type_to_str(h
->nlmsg_type
));
1106 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1107 /* Only consider valid addresses; we'll not get a
1109 * the kernel till IPv6 DAD has completed, but at init
1111 * does query for and will receive all addresses.
1114 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1115 connected_add_ipv6(ifp
, flags
,
1116 (struct in6_addr
*)addr
,
1117 (struct in6_addr
*)broad
,
1118 ifa
->ifa_prefixlen
, label
,
1121 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1122 NULL
, ifa
->ifa_prefixlen
);
1127 * Linux kernel does not send route delete on interface down/addr del
1128 * so we have to re-process routes it owns (i.e. kernel routes)
1130 if (h
->nlmsg_type
!= RTM_NEWADDR
)
1131 rib_update(RIB_UPDATE_KERNEL
);
1136 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1139 struct ifinfomsg
*ifi
;
1140 struct rtattr
*tb
[IFLA_MAX
+ 1];
1141 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1142 struct interface
*ifp
;
1146 char *slave_kind
= NULL
;
1147 struct zebra_ns
*zns
;
1148 vrf_id_t vrf_id
= VRF_DEFAULT
;
1149 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1150 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1151 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1152 ifindex_t bond_ifindex
= IFINDEX_INTERNAL
;
1153 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1154 uint8_t old_hw_addr
[INTERFACE_HWADDR_MAX
];
1155 struct zebra_if
*zif
;
1157 zns
= zebra_ns_lookup(ns_id
);
1158 ifi
= NLMSG_DATA(h
);
1160 /* assume if not default zns, then new VRF */
1161 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1162 /* If this is not link add/delete message so print warning. */
1163 zlog_debug("netlink_link_change: wrong kernel message %s",
1164 nl_msg_type_to_str(h
->nlmsg_type
));
1168 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1169 || ifi
->ifi_family
== AF_INET6
)) {
1171 EC_ZEBRA_UNKNOWN_FAMILY
,
1172 "Invalid address family: %u received from kernel link change: %s",
1173 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1177 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1180 "%s: Message received from netlink is of a broken size %d %zu",
1181 __func__
, h
->nlmsg_len
,
1182 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1186 /* We are interested in some AF_BRIDGE notifications. */
1187 if (ifi
->ifi_family
== AF_BRIDGE
)
1188 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1190 /* Looking up interface name. */
1191 memset(tb
, 0, sizeof(tb
));
1192 memset(linkinfo
, 0, sizeof(linkinfo
));
1193 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1195 /* check for wireless messages to ignore */
1196 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1197 if (IS_ZEBRA_DEBUG_KERNEL
)
1198 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1203 if (tb
[IFLA_IFNAME
] == NULL
)
1205 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1207 if (tb
[IFLA_LINKINFO
]) {
1208 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1210 if (linkinfo
[IFLA_INFO_KIND
])
1211 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1213 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1214 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1216 netlink_determine_zebra_iftype(kind
, &zif_type
);
1219 /* If linking to another interface, note it. */
1221 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1223 if (tb
[IFLA_IFALIAS
]) {
1224 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1227 /* If VRF, create or update the VRF structure itself. */
1228 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1229 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1230 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1233 /* See if interface is present. */
1234 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1236 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1237 if (tb
[IFLA_MASTER
]) {
1238 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1239 && !vrf_is_backend_netns()) {
1240 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1241 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1242 } else if (slave_kind
1243 && (strcmp(slave_kind
, "bridge") == 0)) {
1244 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1246 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1247 } else if (slave_kind
1248 && (strcmp(slave_kind
, "bond") == 0)) {
1249 zif_slave_type
= ZEBRA_IF_SLAVE_BOND
;
1251 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1253 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1255 if (vrf_is_backend_netns())
1256 vrf_id
= (vrf_id_t
)ns_id
;
1258 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1259 /* Add interface notification from kernel */
1260 if (IS_ZEBRA_DEBUG_KERNEL
)
1262 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1263 "sl_type %d master %u flags 0x%x",
1264 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1265 zif_slave_type
, bridge_ifindex
,
1269 /* unknown interface */
1270 ifp
= if_get_by_name(name
, vrf_id
);
1272 /* pre-configured interface, learnt now */
1273 if (ifp
->vrf_id
!= vrf_id
)
1274 if_update_to_new_vrf(ifp
, vrf_id
);
1277 /* Update interface information. */
1278 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1279 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1280 if (!tb
[IFLA_MTU
]) {
1282 "RTM_NEWLINK for interface %s(%u) without MTU set",
1283 name
, ifi
->ifi_index
);
1286 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1288 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1290 /* Set interface type */
1291 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1292 if (IS_ZEBRA_IF_VRF(ifp
))
1293 SET_FLAG(ifp
->status
,
1294 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1297 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1299 netlink_interface_update_hw_addr(tb
, ifp
);
1301 /* Inform clients, install any configured addresses. */
1304 /* Extract and save L2 interface information, take
1305 * additional actions. */
1306 netlink_interface_update_l2info(
1307 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1308 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1309 zebra_l2if_update_bridge_slave(ifp
,
1311 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
))
1312 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1313 } else if (ifp
->vrf_id
!= vrf_id
) {
1314 /* VRF change for an interface. */
1315 if (IS_ZEBRA_DEBUG_KERNEL
)
1317 "RTM_NEWLINK vrf-change for %s(%u) "
1318 "vrf_id %u -> %u flags 0x%x",
1319 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1322 if_handle_vrf_change(ifp
, vrf_id
);
1324 bool was_bridge_slave
, was_bond_slave
;
1326 /* Interface update. */
1327 if (IS_ZEBRA_DEBUG_KERNEL
)
1329 "RTM_NEWLINK update for %s(%u) "
1330 "sl_type %d master %u flags 0x%x",
1331 name
, ifp
->ifindex
, zif_slave_type
,
1332 bridge_ifindex
, ifi
->ifi_flags
);
1334 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1335 if (!tb
[IFLA_MTU
]) {
1337 "RTM_NEWLINK for interface %s(%u) without MTU set",
1338 name
, ifi
->ifi_index
);
1341 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1344 /* Update interface type - NOTE: Only slave_type can
1346 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1347 was_bond_slave
= IS_ZEBRA_IF_BOND_SLAVE(ifp
);
1348 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1350 memcpy(old_hw_addr
, ifp
->hw_addr
, INTERFACE_HWADDR_MAX
);
1352 netlink_interface_update_hw_addr(tb
, ifp
);
1354 if (if_is_no_ptm_operative(ifp
)) {
1355 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1356 if (!if_is_no_ptm_operative(ifp
)) {
1357 if (IS_ZEBRA_DEBUG_KERNEL
)
1359 "Intf %s(%u) has gone DOWN",
1360 name
, ifp
->ifindex
);
1362 rib_update(RIB_UPDATE_KERNEL
);
1363 } else if (if_is_operative(ifp
)) {
1364 /* Must notify client daemons of new
1365 * interface status. */
1366 if (IS_ZEBRA_DEBUG_KERNEL
)
1368 "Intf %s(%u) PTM up, notifying clients",
1369 name
, ifp
->ifindex
);
1370 zebra_interface_up_update(ifp
);
1372 /* Update EVPN VNI when SVI MAC change
1374 if (IS_ZEBRA_IF_VLAN(ifp
) &&
1375 memcmp(old_hw_addr
, ifp
->hw_addr
,
1376 INTERFACE_HWADDR_MAX
)) {
1377 struct interface
*link_if
;
1380 if_lookup_by_index_per_ns(
1381 zebra_ns_lookup(NS_DEFAULT
),
1384 zebra_vxlan_svi_up(ifp
,
1389 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1390 if (if_is_operative(ifp
)) {
1391 if (IS_ZEBRA_DEBUG_KERNEL
)
1393 "Intf %s(%u) has come UP",
1394 name
, ifp
->ifindex
);
1397 if (IS_ZEBRA_DEBUG_KERNEL
)
1399 "Intf %s(%u) has gone DOWN",
1400 name
, ifp
->ifindex
);
1402 rib_update(RIB_UPDATE_KERNEL
);
1406 /* Extract and save L2 interface information, take
1407 * additional actions. */
1408 netlink_interface_update_l2info(
1409 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1410 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1411 zebra_l2if_update_bridge_slave(ifp
,
1413 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp
) || was_bond_slave
)
1414 zebra_l2if_update_bond_slave(ifp
, bond_ifindex
);
1419 XFREE(MTYPE_TMP
, zif
->desc
);
1421 zif
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1424 /* Delete interface notification from kernel */
1426 if (IS_ZEBRA_DEBUG_KERNEL
)
1428 "RTM_DELLINK for unknown interface %s(%u)",
1429 name
, ifi
->ifi_index
);
1433 if (IS_ZEBRA_DEBUG_KERNEL
)
1434 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1437 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1439 /* Special handling for bridge or VxLAN interfaces. */
1440 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1441 zebra_l2_bridge_del(ifp
);
1442 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1443 zebra_l2_vxlanif_del(ifp
);
1445 if (!IS_ZEBRA_IF_VRF(ifp
))
1446 if_delete_update(ifp
);
1452 int netlink_protodown(struct interface
*ifp
, bool down
)
1454 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
1458 struct ifinfomsg ifa
;
1459 char buf
[NL_PKT_BUF_SIZE
];
1462 memset(&req
, 0, sizeof(req
));
1464 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1465 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1466 req
.n
.nlmsg_type
= RTM_SETLINK
;
1467 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
1469 req
.ifa
.ifi_index
= ifp
->ifindex
;
1471 addattr_l(&req
.n
, sizeof(req
), IFLA_PROTO_DOWN
, &down
, sizeof(down
));
1472 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &ifp
->ifindex
, 4);
1474 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
1478 /* Interface information read by netlink. */
1479 void interface_list(struct zebra_ns
*zns
)
1481 interface_lookup_netlink(zns
);
1482 /* We add routes for interface address,
1483 * so we need to get the nexthop info
1484 * from the kernel before we can do that
1486 netlink_nexthop_read(zns
);
1488 interface_addr_lookup_netlink(zns
);
1491 #endif /* GNU_LINUX */