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/if_netlink.h"
70 #include "zebra/zebra_errors.h"
72 extern struct zebra_privs_t zserv_privs
;
74 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
75 names and ifindex values. */
76 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
79 struct interface
*oifp
;
81 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
83 if (ifi_index
== IFINDEX_INTERNAL
)
86 "Netlink is setting interface %s ifindex to reserved internal value %u",
87 ifp
->name
, ifi_index
);
89 if (IS_ZEBRA_DEBUG_KERNEL
)
91 "interface index %d was renamed from %s to %s",
92 ifi_index
, oifp
->name
, ifp
->name
);
96 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
97 ifi_index
, oifp
->name
, ifp
->name
);
98 if_delete_update(oifp
);
101 if_set_index(ifp
, ifi_index
);
104 /* Utility function to parse hardware link-layer address and update ifp */
105 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
106 struct interface
*ifp
)
110 if (tb
[IFLA_ADDRESS
]) {
113 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
115 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
116 zlog_debug("Hardware address is too large: %d",
119 ifp
->hw_addr_len
= hw_addr_len
;
120 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
123 for (i
= 0; i
< hw_addr_len
; i
++)
124 if (ifp
->hw_addr
[i
] != 0)
127 if (i
== hw_addr_len
)
128 ifp
->hw_addr_len
= 0;
130 ifp
->hw_addr_len
= hw_addr_len
;
135 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
139 return ZEBRA_LLT_ETHER
;
141 return ZEBRA_LLT_EETHER
;
143 return ZEBRA_LLT_AX25
;
145 return ZEBRA_LLT_PRONET
;
147 return ZEBRA_LLT_IEEE802
;
149 return ZEBRA_LLT_ARCNET
;
150 case ARPHRD_APPLETLK
:
151 return ZEBRA_LLT_APPLETLK
;
153 return ZEBRA_LLT_DLCI
;
155 return ZEBRA_LLT_ATM
;
156 case ARPHRD_METRICOM
:
157 return ZEBRA_LLT_METRICOM
;
158 case ARPHRD_IEEE1394
:
159 return ZEBRA_LLT_IEEE1394
;
161 return ZEBRA_LLT_EUI64
;
162 case ARPHRD_INFINIBAND
:
163 return ZEBRA_LLT_INFINIBAND
;
165 return ZEBRA_LLT_SLIP
;
167 return ZEBRA_LLT_CSLIP
;
169 return ZEBRA_LLT_SLIP6
;
171 return ZEBRA_LLT_CSLIP6
;
173 return ZEBRA_LLT_RSRVD
;
175 return ZEBRA_LLT_ADAPT
;
177 return ZEBRA_LLT_ROSE
;
179 return ZEBRA_LLT_X25
;
181 return ZEBRA_LLT_PPP
;
183 return ZEBRA_LLT_CHDLC
;
185 return ZEBRA_LLT_LAPB
;
187 return ZEBRA_LLT_RAWHDLC
;
189 return ZEBRA_LLT_IPIP
;
191 return ZEBRA_LLT_IPIP6
;
193 return ZEBRA_LLT_FRAD
;
195 return ZEBRA_LLT_SKIP
;
196 case ARPHRD_LOOPBACK
:
197 return ZEBRA_LLT_LOOPBACK
;
198 case ARPHRD_LOCALTLK
:
199 return ZEBRA_LLT_LOCALTLK
;
201 return ZEBRA_LLT_FDDI
;
203 return ZEBRA_LLT_SIT
;
205 return ZEBRA_LLT_IPDDP
;
207 return ZEBRA_LLT_IPGRE
;
209 return ZEBRA_LLT_PIMREG
;
211 return ZEBRA_LLT_HIPPI
;
213 return ZEBRA_LLT_ECONET
;
215 return ZEBRA_LLT_IRDA
;
217 return ZEBRA_LLT_FCPP
;
219 return ZEBRA_LLT_FCAL
;
221 return ZEBRA_LLT_FCPL
;
222 case ARPHRD_FCFABRIC
:
223 return ZEBRA_LLT_FCFABRIC
;
224 case ARPHRD_IEEE802_TR
:
225 return ZEBRA_LLT_IEEE802_TR
;
226 case ARPHRD_IEEE80211
:
227 return ZEBRA_LLT_IEEE80211
;
228 #ifdef ARPHRD_IEEE802154
229 case ARPHRD_IEEE802154
:
230 return ZEBRA_LLT_IEEE802154
;
234 return ZEBRA_LLT_IP6GRE
;
236 #ifdef ARPHRD_IEEE802154_PHY
237 case ARPHRD_IEEE802154_PHY
:
238 return ZEBRA_LLT_IEEE802154_PHY
;
242 return ZEBRA_LLT_UNKNOWN
;
246 static void netlink_determine_zebra_iftype(char *kind
, zebra_iftype_t
*zif_type
)
248 *zif_type
= ZEBRA_IF_OTHER
;
253 if (strcmp(kind
, "vrf") == 0)
254 *zif_type
= ZEBRA_IF_VRF
;
255 else if (strcmp(kind
, "bridge") == 0)
256 *zif_type
= ZEBRA_IF_BRIDGE
;
257 else if (strcmp(kind
, "vlan") == 0)
258 *zif_type
= ZEBRA_IF_VLAN
;
259 else if (strcmp(kind
, "vxlan") == 0)
260 *zif_type
= ZEBRA_IF_VXLAN
;
261 else if (strcmp(kind
, "macvlan") == 0)
262 *zif_type
= ZEBRA_IF_MACVLAN
;
263 else if (strcmp(kind
, "veth") == 0)
264 *zif_type
= ZEBRA_IF_VETH
;
267 #define parse_rtattr_nested(tb, max, rta) \
268 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
270 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
273 struct ifinfomsg
*ifi
;
274 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
275 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
277 struct zebra_vrf
*zvrf
;
278 uint32_t nl_table_id
;
282 memset(linkinfo
, 0, sizeof linkinfo
);
283 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
285 if (!linkinfo
[IFLA_INFO_DATA
]) {
286 if (IS_ZEBRA_DEBUG_KERNEL
)
288 "%s: IFLA_INFO_DATA missing from VRF message: %s",
293 memset(attr
, 0, sizeof attr
);
294 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
295 if (!attr
[IFLA_VRF_TABLE
]) {
296 if (IS_ZEBRA_DEBUG_KERNEL
)
298 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
303 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
305 if (h
->nlmsg_type
== RTM_NEWLINK
) {
306 if (IS_ZEBRA_DEBUG_KERNEL
)
307 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
308 ifi
->ifi_index
, nl_table_id
);
311 * vrf_get is implied creation if it does not exist
313 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
314 name
); // It would create vrf
316 flog_err(EC_LIB_INTERFACE
, "VRF %s id %u not created",
317 name
, ifi
->ifi_index
);
322 * This is the only place that we get the actual kernel table_id
323 * being used. We need it to set the table_id of the routes
324 * we are passing to the kernel.... And to throw some totally
325 * awesome parties. that too.
327 * At this point we *must* have a zvrf because the vrf_create
328 * callback creates one. We *must* set the table id
329 * before the vrf_enable because of( at the very least )
330 * static routes being delayed for installation until
331 * during the vrf_enable callbacks.
333 zvrf
= (struct zebra_vrf
*)vrf
->info
;
334 zvrf
->table_id
= nl_table_id
;
336 /* Enable the created VRF. */
337 if (!vrf_enable(vrf
)) {
338 flog_err(EC_LIB_INTERFACE
,
339 "Failed to enable VRF %s id %u", name
,
344 } else // h->nlmsg_type == RTM_DELLINK
346 if (IS_ZEBRA_DEBUG_KERNEL
)
347 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
350 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
353 flog_warn(EC_ZEBRA_VRF_NOT_FOUND
, "%s: vrf not found",
362 static int get_iflink_speed(struct interface
*interface
)
365 struct ethtool_cmd ecmd
;
368 const char *ifname
= interface
->name
;
370 /* initialize struct */
371 memset(&ifdata
, 0, sizeof(ifdata
));
373 /* set interface name */
374 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
376 /* initialize ethtool interface */
377 memset(&ecmd
, 0, sizeof(ecmd
));
378 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
379 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
381 /* use ioctl to get IP address of an interface */
382 frr_elevate_privs(&zserv_privs
) {
383 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
,
387 if (IS_ZEBRA_DEBUG_KERNEL
)
388 zlog_debug("Failure to read interface %s speed: %d %s",
389 ifname
, errno
, safe_strerror(errno
));
392 /* Get the current link state for the interface */
393 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
,
397 if (IS_ZEBRA_DEBUG_KERNEL
)
399 "IOCTL failure to read interface %s speed: %d %s",
400 ifname
, errno
, safe_strerror(errno
));
407 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
410 uint32_t kernel_get_speed(struct interface
*ifp
)
412 return get_iflink_speed(ifp
);
415 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
416 struct zebra_l2info_bridge
*bridge_info
)
418 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
420 memset(bridge_info
, 0, sizeof(*bridge_info
));
421 memset(attr
, 0, sizeof attr
);
422 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
423 if (attr
[IFLA_BR_VLAN_FILTERING
])
424 bridge_info
->vlan_aware
=
425 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
429 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
430 struct zebra_l2info_vlan
*vlan_info
)
432 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
435 memset(vlan_info
, 0, sizeof(*vlan_info
));
436 memset(attr
, 0, sizeof attr
);
437 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
438 if (!attr
[IFLA_VLAN_ID
]) {
439 if (IS_ZEBRA_DEBUG_KERNEL
)
440 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
444 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
445 vlan_info
->vid
= vid_in_msg
;
449 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
450 struct zebra_l2info_vxlan
*vxl_info
)
452 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
454 struct in_addr vtep_ip_in_msg
;
456 memset(vxl_info
, 0, sizeof(*vxl_info
));
457 memset(attr
, 0, sizeof attr
);
458 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
459 if (!attr
[IFLA_VXLAN_ID
]) {
460 if (IS_ZEBRA_DEBUG_KERNEL
)
462 "IFLA_VXLAN_ID missing from VXLAN IF message");
466 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
467 vxl_info
->vni
= vni_in_msg
;
468 if (!attr
[IFLA_VXLAN_LOCAL
]) {
469 if (IS_ZEBRA_DEBUG_KERNEL
)
471 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
474 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
475 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
482 * Extract and save L2 params (of interest) for an interface. When a
483 * bridge interface is added or updated, take further actions to map
484 * its members. Likewise, for VxLAN interface.
486 static void netlink_interface_update_l2info(struct interface
*ifp
,
487 struct rtattr
*link_data
, int add
)
492 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
493 struct zebra_l2info_bridge bridge_info
;
495 netlink_extract_bridge_info(link_data
, &bridge_info
);
496 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
497 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
498 struct zebra_l2info_vlan vlan_info
;
500 netlink_extract_vlan_info(link_data
, &vlan_info
);
501 zebra_l2_vlanif_update(ifp
, &vlan_info
);
502 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
503 struct zebra_l2info_vxlan vxlan_info
;
505 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
506 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
510 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
514 struct ifinfomsg
*ifi
;
515 struct rtattr
*tb
[IFLA_MAX
+ 1];
516 struct interface
*ifp
;
517 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
522 vlanid_t access_vlan
;
524 /* Fetch name and ifindex */
526 memset(tb
, 0, sizeof tb
);
527 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
529 if (tb
[IFLA_IFNAME
] == NULL
)
531 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
533 /* The interface should already be known, if not discard. */
534 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
536 zlog_debug("Cannot find bridge IF %s(%u)", name
,
540 if (!IS_ZEBRA_IF_VXLAN(ifp
))
543 /* We are only interested in the access VLAN i.e., AF_SPEC */
544 if (!tb
[IFLA_AF_SPEC
])
547 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
548 * only 1 access VLAN is accepted.
550 memset(aftb
, 0, sizeof aftb
);
551 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
552 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
555 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
556 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
559 access_vlan
= (vlanid_t
)vinfo
->vid
;
560 if (IS_ZEBRA_DEBUG_KERNEL
)
561 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
562 name
, ifi
->ifi_index
);
563 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
568 * Called from interface_lookup_netlink(). This function is only used
571 static int netlink_interface(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
574 struct ifinfomsg
*ifi
;
575 struct rtattr
*tb
[IFLA_MAX
+ 1];
576 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
577 struct interface
*ifp
;
581 char *slave_kind
= NULL
;
582 struct zebra_ns
*zns
;
583 vrf_id_t vrf_id
= VRF_DEFAULT
;
584 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
585 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
586 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
587 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
588 struct zebra_if
*zif
;
590 zns
= zebra_ns_lookup(ns_id
);
593 if (h
->nlmsg_type
!= RTM_NEWLINK
)
596 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
598 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
601 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
605 /* We are interested in some AF_BRIDGE notifications. */
606 if (ifi
->ifi_family
== AF_BRIDGE
)
607 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
609 /* Looking up interface name. */
610 memset(tb
, 0, sizeof tb
);
611 memset(linkinfo
, 0, sizeof linkinfo
);
612 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
614 /* check for wireless messages to ignore */
615 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
616 if (IS_ZEBRA_DEBUG_KERNEL
)
617 zlog_debug("%s: ignoring IFLA_WIRELESS message",
622 if (tb
[IFLA_IFNAME
] == NULL
)
624 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
626 if (tb
[IFLA_IFALIAS
])
627 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
629 if (tb
[IFLA_LINKINFO
]) {
630 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
632 if (linkinfo
[IFLA_INFO_KIND
])
633 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
635 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
636 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
638 netlink_determine_zebra_iftype(kind
, &zif_type
);
641 /* If VRF, create the VRF structure itself. */
642 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
643 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
644 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
647 if (tb
[IFLA_MASTER
]) {
648 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
649 && !vrf_is_backend_netns()) {
650 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
651 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
652 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
653 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
655 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
657 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
659 if (vrf_is_backend_netns())
660 vrf_id
= (vrf_id_t
)ns_id
;
662 /* If linking to another interface, note it. */
664 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
667 ifp
= if_get_by_name(name
, vrf_id
, 0);
668 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
669 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
670 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
672 ifp
->speed
= get_iflink_speed(ifp
);
673 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
676 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
678 /* Set zebra interface type */
679 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
680 if (IS_ZEBRA_IF_VRF(ifp
))
681 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
684 * Just set the @link/lower-device ifindex. During nldump interfaces are
685 * not ordered in any fashion so we may end up getting upper devices
686 * before lower devices. We will setup the real linkage once the dump
689 zif
= (struct zebra_if
*)ifp
->info
;
690 zif
->link_ifindex
= link_ifindex
;
692 /* Hardware type and address. */
693 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
694 netlink_interface_update_hw_addr(tb
, ifp
);
698 /* Extract and save L2 interface information, take additional actions.
700 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
701 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
702 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
707 /* Request for specific interface or address information from the kernel */
708 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
709 uint32_t filter_mask
)
713 struct ifinfomsg ifm
;
717 /* Form the request, specifying filter (rtattr) if needed. */
718 memset(&req
, 0, sizeof(req
));
719 req
.n
.nlmsg_type
= type
;
720 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
721 req
.ifm
.ifi_family
= family
;
723 /* Include filter, if specified. */
725 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
727 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
730 /* Interface lookup by netlink socket. */
731 int interface_lookup_netlink(struct zebra_ns
*zns
)
735 /* Get interface information. */
736 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
739 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
744 /* Get interface information - for bridge interfaces. */
745 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
746 RTEXT_FILTER_BRVLAN
);
749 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
754 /* Get interface information - for bridge interfaces. */
755 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
756 RTEXT_FILTER_BRVLAN
);
759 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
765 zebra_if_update_all_links();
767 /* Get IPv4 address of the interfaces. */
768 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
771 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
776 /* Get IPv6 address of the interfaces. */
777 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
780 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
788 int kernel_interface_set_master(struct interface
*master
,
789 struct interface
*slave
)
791 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
795 struct ifinfomsg ifa
;
796 char buf
[NL_PKT_BUF_SIZE
];
799 memset(&req
, 0, sizeof req
);
801 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
802 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
803 req
.n
.nlmsg_type
= RTM_SETLINK
;
804 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
806 req
.ifa
.ifi_index
= slave
->ifindex
;
808 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
809 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
811 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
815 /* Interface address modification. */
816 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
817 struct connected
*ifc
)
824 struct ifaddrmsg ifa
;
825 char buf
[NL_PKT_BUF_SIZE
];
828 struct zebra_ns
*zns
;
830 if (vrf_is_backend_netns())
831 zns
= zebra_ns_lookup((ns_id_t
)ifp
->vrf_id
);
833 zns
= zebra_ns_lookup(NS_DEFAULT
);
835 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
837 bytelen
= (family
== AF_INET
? 4 : 16);
839 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
840 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
841 req
.n
.nlmsg_type
= cmd
;
842 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
844 req
.ifa
.ifa_family
= family
;
846 req
.ifa
.ifa_index
= ifp
->ifindex
;
848 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
850 if (family
== AF_INET
) {
851 if (CONNECTED_PEER(ifc
)) {
852 p
= ifc
->destination
;
853 addattr_l(&req
.n
, sizeof req
, IFA_ADDRESS
, &p
->u
.prefix
,
855 } else if (cmd
== RTM_NEWADDR
&& ifc
->destination
) {
856 p
= ifc
->destination
;
857 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
858 &p
->u
.prefix
, bytelen
);
862 /* p is now either ifc->address or ifc->destination */
863 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
865 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
866 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
869 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
870 strlen(ifc
->label
) + 1);
872 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
876 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
878 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
881 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
883 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
886 int kernel_address_add_ipv6(struct interface
*ifp
, struct connected
*ifc
)
888 return netlink_address(RTM_NEWADDR
, AF_INET6
, ifp
, ifc
);
891 int kernel_address_delete_ipv6(struct interface
*ifp
, struct connected
*ifc
)
893 return netlink_address(RTM_DELADDR
, AF_INET6
, ifp
, ifc
);
896 int netlink_interface_addr(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
899 struct ifaddrmsg
*ifa
;
900 struct rtattr
*tb
[IFA_MAX
+ 1];
901 struct interface
*ifp
;
906 struct zebra_ns
*zns
;
908 zns
= zebra_ns_lookup(ns_id
);
911 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
) {
913 EC_ZEBRA_UNKNOWN_FAMILY
,
914 "Invalid address family: %u received from kernel interface addr change: %s",
915 ifa
->ifa_family
, nl_msg_type_to_str(h
->nlmsg_type
));
919 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
922 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
924 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
927 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg
)));
931 memset(tb
, 0, sizeof tb
);
932 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
934 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
938 "netlink_interface_addr can't find interface by index %d",
943 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
946 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
947 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
950 zlog_debug(" IFA_LOCAL %s/%d",
951 inet_ntop(ifa
->ifa_family
,
952 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
956 zlog_debug(" IFA_ADDRESS %s/%d",
957 inet_ntop(ifa
->ifa_family
,
958 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
961 if (tb
[IFA_BROADCAST
])
962 zlog_debug(" IFA_BROADCAST %s/%d",
963 inet_ntop(ifa
->ifa_family
,
964 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
967 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
968 zlog_debug(" IFA_LABEL %s",
969 (char *)RTA_DATA(tb
[IFA_LABEL
]));
971 if (tb
[IFA_CACHEINFO
]) {
972 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
973 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
974 ci
->ifa_prefered
, ci
->ifa_valid
);
978 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
979 if (tb
[IFA_LOCAL
] == NULL
)
980 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
981 if (tb
[IFA_ADDRESS
] == NULL
)
982 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
984 /* local interface address */
985 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
987 /* is there a peer address? */
989 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
990 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
991 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
992 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
994 /* seeking a broadcast address */
995 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
998 /* addr is primary key, SOL if we don't have one */
1000 zlog_debug("%s: NULL address", __func__
);
1005 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
1006 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
1010 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
1012 if (label
&& strcmp(ifp
->name
, label
) == 0)
1015 /* Register interface address to the interface. */
1016 if (ifa
->ifa_family
== AF_INET
) {
1017 if (ifa
->ifa_prefixlen
> IPV4_MAX_BITLEN
) {
1019 "Invalid prefix length: %u received from kernel interface addr change: %s",
1021 nl_msg_type_to_str(h
->nlmsg_type
));
1024 if (h
->nlmsg_type
== RTM_NEWADDR
)
1025 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
1027 (struct in_addr
*)broad
, label
);
1029 connected_delete_ipv4(
1030 ifp
, flags
, (struct in_addr
*)addr
,
1031 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
1033 if (ifa
->ifa_family
== AF_INET6
) {
1034 if (ifa
->ifa_prefixlen
> IPV6_MAX_BITLEN
) {
1036 "Invalid prefix length: %u received from kernel interface addr change: %s",
1038 nl_msg_type_to_str(h
->nlmsg_type
));
1041 if (h
->nlmsg_type
== RTM_NEWADDR
) {
1042 /* Only consider valid addresses; we'll not get a
1044 * the kernel till IPv6 DAD has completed, but at init
1046 * does query for and will receive all addresses.
1048 if (!(ifa
->ifa_flags
1049 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1050 connected_add_ipv6(ifp
, flags
,
1051 (struct in6_addr
*)addr
,
1052 (struct in6_addr
*)broad
,
1053 ifa
->ifa_prefixlen
, label
);
1055 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1056 (struct in6_addr
*)broad
,
1057 ifa
->ifa_prefixlen
);
1063 int netlink_link_change(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
1066 struct ifinfomsg
*ifi
;
1067 struct rtattr
*tb
[IFLA_MAX
+ 1];
1068 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1069 struct interface
*ifp
;
1073 char *slave_kind
= NULL
;
1074 struct zebra_ns
*zns
;
1075 vrf_id_t vrf_id
= VRF_DEFAULT
;
1076 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1077 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1078 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1079 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1082 zns
= zebra_ns_lookup(ns_id
);
1083 ifi
= NLMSG_DATA(h
);
1085 /* assume if not default zns, then new VRF */
1086 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1087 /* If this is not link add/delete message so print warning. */
1088 zlog_debug("netlink_link_change: wrong kernel message %s",
1089 nl_msg_type_to_str(h
->nlmsg_type
));
1093 if (!(ifi
->ifi_family
== AF_UNSPEC
|| ifi
->ifi_family
== AF_BRIDGE
1094 || ifi
->ifi_family
== AF_INET6
)) {
1096 EC_ZEBRA_UNKNOWN_FAMILY
,
1097 "Invalid address family: %u received from kernel link change: %s",
1098 ifi
->ifi_family
, nl_msg_type_to_str(h
->nlmsg_type
));
1102 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1104 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
1105 __PRETTY_FUNCTION__
, h
->nlmsg_len
,
1106 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg
)));
1110 /* We are interested in some AF_BRIDGE notifications. */
1111 if (ifi
->ifi_family
== AF_BRIDGE
)
1112 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1114 /* Looking up interface name. */
1115 memset(tb
, 0, sizeof tb
);
1116 memset(linkinfo
, 0, sizeof linkinfo
);
1117 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1119 /* check for wireless messages to ignore */
1120 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1121 if (IS_ZEBRA_DEBUG_KERNEL
)
1122 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1127 if (tb
[IFLA_IFNAME
] == NULL
)
1129 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1131 if (tb
[IFLA_LINKINFO
]) {
1132 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1134 if (linkinfo
[IFLA_INFO_KIND
])
1135 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1137 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1138 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1140 netlink_determine_zebra_iftype(kind
, &zif_type
);
1143 /* If linking to another interface, note it. */
1145 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1147 if (tb
[IFLA_IFALIAS
]) {
1148 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1151 /* If VRF, create or update the VRF structure itself. */
1152 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1153 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1154 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1157 /* See if interface is present. */
1158 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1162 XFREE(MTYPE_TMP
, ifp
->desc
);
1164 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1167 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1168 if (tb
[IFLA_MASTER
]) {
1169 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1170 && !vrf_is_backend_netns()) {
1171 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1172 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1173 } else if (slave_kind
1174 && (strcmp(slave_kind
, "bridge") == 0)) {
1175 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1177 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1179 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1181 if (vrf_is_backend_netns())
1182 vrf_id
= (vrf_id_t
)ns_id
;
1184 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1185 /* Add interface notification from kernel */
1186 if (IS_ZEBRA_DEBUG_KERNEL
)
1188 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1189 "sl_type %d master %u flags 0x%x",
1190 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1191 zif_slave_type
, bridge_ifindex
,
1195 /* unknown interface */
1196 ifp
= if_get_by_name(name
, vrf_id
, 0);
1198 /* pre-configured interface, learnt now */
1199 if (ifp
->vrf_id
!= vrf_id
)
1200 if_update_to_new_vrf(ifp
, vrf_id
);
1203 /* Update interface information. */
1204 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1205 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1206 if (!tb
[IFLA_MTU
]) {
1208 "RTM_NEWLINK for interface %s(%u) without MTU set",
1209 name
, ifi
->ifi_index
);
1212 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1214 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1216 /* Set interface type */
1217 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1218 if (IS_ZEBRA_IF_VRF(ifp
))
1219 SET_FLAG(ifp
->status
,
1220 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1223 zebra_if_update_link(ifp
, link_ifindex
, ns_id
);
1225 netlink_interface_update_hw_addr(tb
, ifp
);
1227 /* Inform clients, install any configured addresses. */
1230 /* Extract and save L2 interface information, take
1231 * additional actions. */
1232 netlink_interface_update_l2info(
1233 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1234 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1235 zebra_l2if_update_bridge_slave(ifp
,
1237 } else if (ifp
->vrf_id
!= vrf_id
) {
1238 /* VRF change for an interface. */
1239 if (IS_ZEBRA_DEBUG_KERNEL
)
1241 "RTM_NEWLINK vrf-change for %s(%u) "
1242 "vrf_id %u -> %u flags 0x%x",
1243 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1246 if_handle_vrf_change(ifp
, vrf_id
);
1248 int was_bridge_slave
;
1250 /* Interface update. */
1251 if (IS_ZEBRA_DEBUG_KERNEL
)
1253 "RTM_NEWLINK update for %s(%u) "
1254 "sl_type %d master %u flags 0x%x",
1255 name
, ifp
->ifindex
, zif_slave_type
,
1256 bridge_ifindex
, ifi
->ifi_flags
);
1258 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1259 if (!tb
[IFLA_MTU
]) {
1261 "RTM_NEWLINK for interface %s(%u) without MTU set",
1262 name
, ifi
->ifi_index
);
1265 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1268 /* Update interface type - NOTE: Only slave_type can
1270 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1271 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1273 netlink_interface_update_hw_addr(tb
, ifp
);
1275 if (if_is_no_ptm_operative(ifp
)) {
1276 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1277 if (!if_is_no_ptm_operative(ifp
)) {
1278 if (IS_ZEBRA_DEBUG_KERNEL
)
1280 "Intf %s(%u) has gone DOWN",
1281 name
, ifp
->ifindex
);
1283 } else if (if_is_operative(ifp
)) {
1284 /* Must notify client daemons of new
1285 * interface status. */
1286 if (IS_ZEBRA_DEBUG_KERNEL
)
1288 "Intf %s(%u) PTM up, notifying clients",
1289 name
, ifp
->ifindex
);
1290 zebra_interface_up_update(ifp
);
1293 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1294 if (if_is_operative(ifp
)) {
1295 if (IS_ZEBRA_DEBUG_KERNEL
)
1297 "Intf %s(%u) has come UP",
1298 name
, ifp
->ifindex
);
1303 /* Extract and save L2 interface information, take
1304 * additional actions. */
1305 netlink_interface_update_l2info(
1306 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1307 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1308 zebra_l2if_update_bridge_slave(ifp
,
1312 /* Delete interface notification from kernel */
1314 if (IS_ZEBRA_DEBUG_KERNEL
)
1316 "RTM_DELLINK for unknown interface %s(%u)",
1317 name
, ifi
->ifi_index
);
1321 if (IS_ZEBRA_DEBUG_KERNEL
)
1322 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1325 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1327 /* Special handling for bridge or VxLAN interfaces. */
1328 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1329 zebra_l2_bridge_del(ifp
);
1330 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1331 zebra_l2_vxlanif_del(ifp
);
1333 if (!IS_ZEBRA_IF_VRF(ifp
))
1334 if_delete_update(ifp
);
1340 /* Interface information read by netlink. */
1341 void interface_list(struct zebra_ns
*zns
)
1343 interface_lookup_netlink(zns
);
1346 #endif /* GNU_LINUX */