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 <linux/if_bridge.h>
34 #include <linux/if_link.h>
35 #include <net/if_arp.h>
36 #include <linux/sockios.h>
37 #include <linux/ethtool.h>
43 #include "connected.h"
46 #include "zebra_memory.h"
56 #include "zebra/zserv.h"
57 #include "zebra/zebra_ns.h"
58 #include "zebra/zebra_vrf.h"
60 #include "zebra/redistribute.h"
61 #include "zebra/interface.h"
62 #include "zebra/debug.h"
63 #include "zebra/rtadv.h"
64 #include "zebra/zebra_ptm.h"
65 #include "zebra/zebra_mpls.h"
66 #include "zebra/kernel_netlink.h"
67 #include "zebra/if_netlink.h"
70 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
71 names and ifindex values. */
72 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
75 struct interface
*oifp
;
77 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
79 if (ifi_index
== IFINDEX_INTERNAL
)
81 "Netlink is setting interface %s ifindex to reserved "
83 ifp
->name
, ifi_index
);
85 if (IS_ZEBRA_DEBUG_KERNEL
)
87 "interface index %d was renamed from %s to %s",
88 ifi_index
, oifp
->name
, ifp
->name
);
91 "interface rename detected on up interface: index %d "
92 "was renamed from %s to %s, results are uncertain!",
93 ifi_index
, oifp
->name
, ifp
->name
);
94 if_delete_update(oifp
);
97 if_set_index(ifp
, ifi_index
);
100 /* Utility function to parse hardware link-layer address and update ifp */
101 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
102 struct interface
*ifp
)
106 if (tb
[IFLA_ADDRESS
]) {
109 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
111 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
112 zlog_warn("Hardware address is too large: %d",
115 ifp
->hw_addr_len
= hw_addr_len
;
116 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
119 for (i
= 0; i
< hw_addr_len
; i
++)
120 if (ifp
->hw_addr
[i
] != 0)
123 if (i
== hw_addr_len
)
124 ifp
->hw_addr_len
= 0;
126 ifp
->hw_addr_len
= hw_addr_len
;
131 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
135 return ZEBRA_LLT_ETHER
;
137 return ZEBRA_LLT_EETHER
;
139 return ZEBRA_LLT_AX25
;
141 return ZEBRA_LLT_PRONET
;
143 return ZEBRA_LLT_IEEE802
;
145 return ZEBRA_LLT_ARCNET
;
146 case ARPHRD_APPLETLK
:
147 return ZEBRA_LLT_APPLETLK
;
149 return ZEBRA_LLT_DLCI
;
151 return ZEBRA_LLT_ATM
;
152 case ARPHRD_METRICOM
:
153 return ZEBRA_LLT_METRICOM
;
154 case ARPHRD_IEEE1394
:
155 return ZEBRA_LLT_IEEE1394
;
157 return ZEBRA_LLT_EUI64
;
158 case ARPHRD_INFINIBAND
:
159 return ZEBRA_LLT_INFINIBAND
;
161 return ZEBRA_LLT_SLIP
;
163 return ZEBRA_LLT_CSLIP
;
165 return ZEBRA_LLT_SLIP6
;
167 return ZEBRA_LLT_CSLIP6
;
169 return ZEBRA_LLT_RSRVD
;
171 return ZEBRA_LLT_ADAPT
;
173 return ZEBRA_LLT_ROSE
;
175 return ZEBRA_LLT_X25
;
177 return ZEBRA_LLT_PPP
;
179 return ZEBRA_LLT_CHDLC
;
181 return ZEBRA_LLT_LAPB
;
183 return ZEBRA_LLT_RAWHDLC
;
185 return ZEBRA_LLT_IPIP
;
187 return ZEBRA_LLT_IPIP6
;
189 return ZEBRA_LLT_FRAD
;
191 return ZEBRA_LLT_SKIP
;
192 case ARPHRD_LOOPBACK
:
193 return ZEBRA_LLT_LOOPBACK
;
194 case ARPHRD_LOCALTLK
:
195 return ZEBRA_LLT_LOCALTLK
;
197 return ZEBRA_LLT_FDDI
;
199 return ZEBRA_LLT_SIT
;
201 return ZEBRA_LLT_IPDDP
;
203 return ZEBRA_LLT_IPGRE
;
205 return ZEBRA_LLT_PIMREG
;
207 return ZEBRA_LLT_HIPPI
;
209 return ZEBRA_LLT_ECONET
;
211 return ZEBRA_LLT_IRDA
;
213 return ZEBRA_LLT_FCPP
;
215 return ZEBRA_LLT_FCAL
;
217 return ZEBRA_LLT_FCPL
;
218 case ARPHRD_FCFABRIC
:
219 return ZEBRA_LLT_FCFABRIC
;
220 case ARPHRD_IEEE802_TR
:
221 return ZEBRA_LLT_IEEE802_TR
;
222 case ARPHRD_IEEE80211
:
223 return ZEBRA_LLT_IEEE80211
;
224 #ifdef ARPHRD_IEEE802154
225 case ARPHRD_IEEE802154
:
226 return ZEBRA_LLT_IEEE802154
;
230 return ZEBRA_LLT_IP6GRE
;
232 #ifdef ARPHRD_IEEE802154_PHY
233 case ARPHRD_IEEE802154_PHY
:
234 return ZEBRA_LLT_IEEE802154_PHY
;
238 return ZEBRA_LLT_UNKNOWN
;
242 static void netlink_determine_zebra_iftype(char *kind
, zebra_iftype_t
*zif_type
)
244 *zif_type
= ZEBRA_IF_OTHER
;
249 if (strcmp(kind
, "vrf") == 0)
250 *zif_type
= ZEBRA_IF_VRF
;
251 else if (strcmp(kind
, "bridge") == 0)
252 *zif_type
= ZEBRA_IF_BRIDGE
;
253 else if (strcmp(kind
, "vlan") == 0)
254 *zif_type
= ZEBRA_IF_VLAN
;
255 else if (strcmp(kind
, "vxlan") == 0)
256 *zif_type
= ZEBRA_IF_VXLAN
;
257 else if (strcmp(kind
, "macvlan") == 0)
258 *zif_type
= ZEBRA_IF_MACVLAN
;
261 #define parse_rtattr_nested(tb, max, rta) \
262 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
264 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
267 struct ifinfomsg
*ifi
;
268 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
269 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
271 struct zebra_vrf
*zvrf
;
272 u_int32_t nl_table_id
;
276 memset(linkinfo
, 0, sizeof linkinfo
);
277 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
279 if (!linkinfo
[IFLA_INFO_DATA
]) {
280 if (IS_ZEBRA_DEBUG_KERNEL
)
282 "%s: IFLA_INFO_DATA missing from VRF message: %s",
287 memset(attr
, 0, sizeof attr
);
288 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
289 if (!attr
[IFLA_VRF_TABLE
]) {
290 if (IS_ZEBRA_DEBUG_KERNEL
)
292 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
297 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
299 if (h
->nlmsg_type
== RTM_NEWLINK
) {
300 if (IS_ZEBRA_DEBUG_KERNEL
)
301 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
302 ifi
->ifi_index
, nl_table_id
);
305 * vrf_get is implied creation if it does not exist
307 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
308 name
); // It would create vrf
310 zlog_err("VRF %s id %u not created", name
,
315 /* Enable the created VRF. */
316 if (!vrf_enable(vrf
)) {
317 zlog_err("Failed to enable VRF %s id %u", name
,
323 * This is the only place that we get the actual kernel table_id
324 * being used. We need it to set the table_id of the routes
325 * we are passing to the kernel.... And to throw some totally
326 * awesome parties. that too.
328 zvrf
= (struct zebra_vrf
*)vrf
->info
;
329 zvrf
->table_id
= nl_table_id
;
330 } else // h->nlmsg_type == RTM_DELLINK
332 if (IS_ZEBRA_DEBUG_KERNEL
)
333 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
336 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
339 zlog_warn("%s: vrf not found", __func__
);
347 static int get_iflink_speed(const char *ifname
)
350 struct ethtool_cmd ecmd
;
354 /* initialize struct */
355 memset(&ifdata
, 0, sizeof(ifdata
));
357 /* set interface name */
358 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
360 /* initialize ethtool interface */
361 memset(&ecmd
, 0, sizeof(ecmd
));
362 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
363 ifdata
.ifr_data
= (__caddr_t
)&ecmd
;
365 /* use ioctl to get IP address of an interface */
366 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
368 if (IS_ZEBRA_DEBUG_KERNEL
)
369 zlog_debug("Failure to read interface %s speed: %d %s",
370 ifname
, errno
, safe_strerror(errno
));
374 /* Get the current link state for the interface */
375 rc
= ioctl(sd
, SIOCETHTOOL
, (char *)&ifdata
);
377 if (IS_ZEBRA_DEBUG_KERNEL
)
379 "IOCTL failure to read interface %s speed: %d %s",
380 ifname
, errno
, safe_strerror(errno
));
387 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
390 uint32_t kernel_get_speed(struct interface
*ifp
)
392 return get_iflink_speed(ifp
->name
);
395 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
396 struct zebra_l2info_bridge
*bridge_info
)
398 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
400 memset(bridge_info
, 0, sizeof(*bridge_info
));
401 memset(attr
, 0, sizeof attr
);
402 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
403 if (attr
[IFLA_BR_VLAN_FILTERING
])
404 bridge_info
->vlan_aware
=
405 *(u_char
*)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
409 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
410 struct zebra_l2info_vlan
*vlan_info
)
412 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
415 memset(vlan_info
, 0, sizeof(*vlan_info
));
416 memset(attr
, 0, sizeof attr
);
417 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
418 if (!attr
[IFLA_VLAN_ID
]) {
419 if (IS_ZEBRA_DEBUG_KERNEL
)
420 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
424 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
425 vlan_info
->vid
= vid_in_msg
;
429 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
430 struct zebra_l2info_vxlan
*vxl_info
)
432 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
434 struct in_addr vtep_ip_in_msg
;
436 memset(vxl_info
, 0, sizeof(*vxl_info
));
437 memset(attr
, 0, sizeof attr
);
438 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
439 if (!attr
[IFLA_VXLAN_ID
]) {
440 if (IS_ZEBRA_DEBUG_KERNEL
)
442 "IFLA_VXLAN_ID missing from VXLAN IF message");
446 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
447 vxl_info
->vni
= vni_in_msg
;
448 if (!attr
[IFLA_VXLAN_LOCAL
]) {
449 if (IS_ZEBRA_DEBUG_KERNEL
)
451 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
454 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
455 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
462 * Extract and save L2 params (of interest) for an interface. When a
463 * bridge interface is added or updated, take further actions to map
464 * its members. Likewise, for VxLAN interface.
466 static void netlink_interface_update_l2info(struct interface
*ifp
,
467 struct rtattr
*link_data
, int add
)
472 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
473 struct zebra_l2info_bridge bridge_info
;
475 netlink_extract_bridge_info(link_data
, &bridge_info
);
476 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
477 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
478 struct zebra_l2info_vlan vlan_info
;
480 netlink_extract_vlan_info(link_data
, &vlan_info
);
481 zebra_l2_vlanif_update(ifp
, &vlan_info
);
482 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
483 struct zebra_l2info_vxlan vxlan_info
;
485 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
486 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
490 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
494 struct ifinfomsg
*ifi
;
495 struct rtattr
*tb
[IFLA_MAX
+ 1];
496 struct interface
*ifp
;
497 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
502 vlanid_t access_vlan
;
504 /* Fetch name and ifindex */
506 memset(tb
, 0, sizeof tb
);
507 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
509 if (tb
[IFLA_IFNAME
] == NULL
)
511 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
513 /* The interface should already be known, if not discard. */
514 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
516 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
519 if (!IS_ZEBRA_IF_VXLAN(ifp
))
522 /* We are only interested in the access VLAN i.e., AF_SPEC */
523 if (!tb
[IFLA_AF_SPEC
])
526 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
527 * only 1 access VLAN is accepted.
529 memset(aftb
, 0, sizeof aftb
);
530 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
531 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
534 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
535 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
538 access_vlan
= (vlanid_t
)vinfo
->vid
;
539 if (IS_ZEBRA_DEBUG_KERNEL
)
540 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
541 name
, ifi
->ifi_index
);
542 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
546 /* Called from interface_lookup_netlink(). This function is only used
548 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
549 ns_id_t ns_id
, int startup
)
552 struct ifinfomsg
*ifi
;
553 struct rtattr
*tb
[IFLA_MAX
+ 1];
554 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
555 struct interface
*ifp
;
559 char *slave_kind
= NULL
;
560 struct zebra_ns
*zns
;
561 vrf_id_t vrf_id
= VRF_DEFAULT
;
562 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
563 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
564 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
565 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
567 zns
= zebra_ns_lookup(ns_id
);
570 if (h
->nlmsg_type
!= RTM_NEWLINK
)
573 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
577 /* We are interested in some AF_BRIDGE notifications. */
578 if (ifi
->ifi_family
== AF_BRIDGE
)
579 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
581 /* Looking up interface name. */
582 memset(tb
, 0, sizeof tb
);
583 memset(linkinfo
, 0, sizeof linkinfo
);
584 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
587 /* check for wireless messages to ignore */
588 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
589 if (IS_ZEBRA_DEBUG_KERNEL
)
590 zlog_debug("%s: ignoring IFLA_WIRELESS message",
594 #endif /* IFLA_WIRELESS */
596 if (tb
[IFLA_IFNAME
] == NULL
)
598 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
600 if (tb
[IFLA_IFALIAS
])
601 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
603 if (tb
[IFLA_LINKINFO
]) {
604 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
606 if (linkinfo
[IFLA_INFO_KIND
])
607 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
609 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
610 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
611 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
614 netlink_determine_zebra_iftype(kind
, &zif_type
);
617 /* If VRF, create the VRF structure itself. */
618 if (zif_type
== ZEBRA_IF_VRF
) {
619 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
620 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
623 if (tb
[IFLA_MASTER
]) {
624 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
625 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
626 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
627 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
628 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
630 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
632 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
635 /* If linking to another interface, note it. */
637 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
640 ifp
= if_get_by_name(name
, vrf_id
, 0);
641 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
642 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
643 if (IS_ZEBRA_IF_VRF(ifp
))
644 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
645 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
647 ifp
->speed
= get_iflink_speed(name
);
648 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
651 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
653 /* Set zebra interface type */
654 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
657 zebra_if_update_link(ifp
, link_ifindex
);
659 /* Hardware type and address. */
660 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
661 netlink_interface_update_hw_addr(tb
, ifp
);
665 /* Extract and save L2 interface information, take additional actions.
667 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
668 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
669 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
674 /* Request for specific interface or address information from the kernel */
675 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
676 u_int32_t filter_mask
)
680 struct ifinfomsg ifm
;
684 /* Form the request, specifying filter (rtattr) if needed. */
685 memset(&req
, 0, sizeof(req
));
686 req
.n
.nlmsg_type
= type
;
687 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
688 req
.ifm
.ifi_family
= family
;
690 /* Include filter, if specified. */
692 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
694 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
697 /* Interface lookup by netlink socket. */
698 int interface_lookup_netlink(struct zebra_ns
*zns
)
702 /* Get interface information. */
703 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
706 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
711 /* Get interface information - for bridge interfaces. */
712 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
713 RTEXT_FILTER_BRVLAN
);
716 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
721 /* Get interface information - for bridge interfaces. */
722 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
723 RTEXT_FILTER_BRVLAN
);
726 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
731 /* Get IPv4 address of the interfaces. */
732 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
735 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
740 /* Get IPv6 address of the interfaces. */
741 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
744 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
752 int kernel_interface_set_master(struct interface
*master
,
753 struct interface
*slave
)
755 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
759 struct ifinfomsg ifa
;
760 char buf
[NL_PKT_BUF_SIZE
];
763 memset(&req
, 0, sizeof req
);
765 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
766 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
767 req
.n
.nlmsg_type
= RTM_SETLINK
;
768 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
770 req
.ifa
.ifi_index
= slave
->ifindex
;
772 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
773 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
775 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
779 /* Interface address modification. */
780 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
781 struct connected
*ifc
)
788 struct ifaddrmsg ifa
;
789 char buf
[NL_PKT_BUF_SIZE
];
792 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
795 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
797 bytelen
= (family
== AF_INET
? 4 : 16);
799 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
800 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
801 req
.n
.nlmsg_type
= cmd
;
802 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
804 req
.ifa
.ifa_family
= family
;
806 req
.ifa
.ifa_index
= ifp
->ifindex
;
808 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
810 if (family
== AF_INET
) {
811 if (CONNECTED_PEER(ifc
)) {
812 p
= ifc
->destination
;
813 addattr_l(&req
.n
, sizeof req
, IFA_ADDRESS
, &p
->u
.prefix
,
815 } else if (cmd
== RTM_NEWADDR
&& ifc
->destination
) {
816 p
= ifc
->destination
;
817 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
818 &p
->u
.prefix
, bytelen
);
822 /* p is now either ifc->address or ifc->destination */
823 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
825 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
826 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
829 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
830 strlen(ifc
->label
) + 1);
832 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
836 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
838 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
841 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
843 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
846 int kernel_address_add_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
848 return netlink_address (RTM_NEWADDR
, AF_INET6
, ifp
, ifc
);
851 int kernel_address_delete_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
853 return netlink_address (RTM_DELADDR
, AF_INET6
, ifp
, ifc
);
856 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
857 ns_id_t ns_id
, int startup
)
860 struct ifaddrmsg
*ifa
;
861 struct rtattr
*tb
[IFA_MAX
+ 1];
862 struct interface
*ifp
;
867 struct zebra_ns
*zns
;
869 zns
= zebra_ns_lookup(ns_id
);
872 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
875 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
878 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
882 memset(tb
, 0, sizeof tb
);
883 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
885 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
888 "netlink_interface_addr can't find interface by index %d",
893 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
896 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
897 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
900 zlog_debug(" IFA_LOCAL %s/%d",
901 inet_ntop(ifa
->ifa_family
,
902 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
906 zlog_debug(" IFA_ADDRESS %s/%d",
907 inet_ntop(ifa
->ifa_family
,
908 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
911 if (tb
[IFA_BROADCAST
])
912 zlog_debug(" IFA_BROADCAST %s/%d",
913 inet_ntop(ifa
->ifa_family
,
914 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
917 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
918 zlog_debug(" IFA_LABEL %s",
919 (char *)RTA_DATA(tb
[IFA_LABEL
]));
921 if (tb
[IFA_CACHEINFO
]) {
922 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
923 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
924 ci
->ifa_prefered
, ci
->ifa_valid
);
928 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
929 if (tb
[IFA_LOCAL
] == NULL
)
930 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
931 if (tb
[IFA_ADDRESS
] == NULL
)
932 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
934 /* local interface address */
935 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
937 /* is there a peer address? */
939 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
940 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
941 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
942 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
944 /* seeking a broadcast address */
945 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
948 /* addr is primary key, SOL if we don't have one */
950 zlog_debug("%s: NULL address", __func__
);
955 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
956 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
960 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
962 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
965 /* Register interface address to the interface. */
966 if (ifa
->ifa_family
== AF_INET
) {
967 if (h
->nlmsg_type
== RTM_NEWADDR
)
968 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
970 (struct in_addr
*)broad
, label
);
972 connected_delete_ipv4(
973 ifp
, flags
, (struct in_addr
*)addr
,
974 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
976 if (ifa
->ifa_family
== AF_INET6
) {
977 if (h
->nlmsg_type
== RTM_NEWADDR
) {
978 /* Only consider valid addresses; we'll not get a
980 * the kernel till IPv6 DAD has completed, but at init
982 * does query for and will receive all addresses.
985 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
986 connected_add_ipv6(ifp
, flags
,
987 (struct in6_addr
*)addr
,
988 ifa
->ifa_prefixlen
, label
);
990 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
997 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
998 ns_id_t ns_id
, int startup
)
1001 struct ifinfomsg
*ifi
;
1002 struct rtattr
*tb
[IFLA_MAX
+ 1];
1003 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1004 struct interface
*ifp
;
1008 char *slave_kind
= NULL
;
1009 struct zebra_ns
*zns
;
1010 vrf_id_t vrf_id
= VRF_DEFAULT
;
1011 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1012 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1013 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1014 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1017 zns
= zebra_ns_lookup(ns_id
);
1018 ifi
= NLMSG_DATA(h
);
1020 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1021 /* If this is not link add/delete message so print warning. */
1022 zlog_warn("netlink_link_change: wrong kernel message %d",
1027 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1031 /* We are interested in some AF_BRIDGE notifications. */
1032 if (ifi
->ifi_family
== AF_BRIDGE
)
1033 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1035 /* Looking up interface name. */
1036 memset(tb
, 0, sizeof tb
);
1037 memset(linkinfo
, 0, sizeof linkinfo
);
1038 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1040 #ifdef IFLA_WIRELESS
1041 /* check for wireless messages to ignore */
1042 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1043 if (IS_ZEBRA_DEBUG_KERNEL
)
1044 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1048 #endif /* IFLA_WIRELESS */
1050 if (tb
[IFLA_IFNAME
] == NULL
)
1052 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1054 if (tb
[IFLA_LINKINFO
]) {
1055 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1057 if (linkinfo
[IFLA_INFO_KIND
])
1058 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1060 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1061 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1062 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1065 netlink_determine_zebra_iftype(kind
, &zif_type
);
1068 /* If linking to another interface, note it. */
1070 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1072 if (tb
[IFLA_IFALIAS
]) {
1073 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1076 /* If VRF, create or update the VRF structure itself. */
1077 if (zif_type
== ZEBRA_IF_VRF
) {
1078 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1079 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1082 /* See if interface is present. */
1083 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1087 XFREE(MTYPE_TMP
, ifp
->desc
);
1089 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1092 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1093 if (tb
[IFLA_MASTER
]) {
1094 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
1095 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1097 *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1098 } else if (slave_kind
1099 && (strcmp(slave_kind
, "bridge") == 0)) {
1100 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1102 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1104 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1108 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1109 /* Add interface notification from kernel */
1110 if (IS_ZEBRA_DEBUG_KERNEL
)
1112 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1113 "sl_type %d master %u flags 0x%x",
1114 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1115 zif_slave_type
, bridge_ifindex
,
1119 /* unknown interface */
1120 ifp
= if_get_by_name(name
, vrf_id
, 0);
1122 /* pre-configured interface, learnt now */
1123 if (ifp
->vrf_id
!= vrf_id
)
1124 if_update_to_new_vrf(ifp
, vrf_id
);
1127 /* Update interface information. */
1128 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1129 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1130 if (IS_ZEBRA_IF_VRF(ifp
))
1131 SET_FLAG(ifp
->status
,
1132 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1133 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1135 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1137 /* Set interface type */
1138 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1141 zebra_if_update_link(ifp
, link_ifindex
);
1143 netlink_interface_update_hw_addr(tb
, ifp
);
1145 /* Inform clients, install any configured addresses. */
1148 /* Extract and save L2 interface information, take
1149 * additional actions. */
1150 netlink_interface_update_l2info(
1151 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1152 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1153 zebra_l2if_update_bridge_slave(ifp
,
1155 } else if (ifp
->vrf_id
!= vrf_id
) {
1156 /* VRF change for an interface. */
1157 if (IS_ZEBRA_DEBUG_KERNEL
)
1159 "RTM_NEWLINK vrf-change for %s(%u) "
1160 "vrf_id %u -> %u flags 0x%x",
1161 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1164 if_handle_vrf_change(ifp
, vrf_id
);
1166 int was_bridge_slave
;
1168 /* Interface update. */
1169 if (IS_ZEBRA_DEBUG_KERNEL
)
1171 "RTM_NEWLINK update for %s(%u) "
1172 "sl_type %d master %u flags 0x%x",
1173 name
, ifp
->ifindex
, zif_slave_type
,
1174 bridge_ifindex
, ifi
->ifi_flags
);
1176 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1177 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1180 /* Update interface type - NOTE: Only slave_type can
1182 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1183 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1185 netlink_interface_update_hw_addr(tb
, ifp
);
1187 if (if_is_no_ptm_operative(ifp
)) {
1188 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1189 if (!if_is_no_ptm_operative(ifp
)) {
1190 if (IS_ZEBRA_DEBUG_KERNEL
)
1192 "Intf %s(%u) has gone DOWN",
1193 name
, ifp
->ifindex
);
1195 } else if (if_is_operative(ifp
)) {
1196 /* Must notify client daemons of new
1197 * interface status. */
1198 if (IS_ZEBRA_DEBUG_KERNEL
)
1200 "Intf %s(%u) PTM up, notifying clients",
1201 name
, ifp
->ifindex
);
1202 zebra_interface_up_update(ifp
);
1205 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1206 if (if_is_operative(ifp
)) {
1207 if (IS_ZEBRA_DEBUG_KERNEL
)
1209 "Intf %s(%u) has come UP",
1210 name
, ifp
->ifindex
);
1215 /* Extract and save L2 interface information, take
1216 * additional actions. */
1217 netlink_interface_update_l2info(
1218 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1219 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1220 zebra_l2if_update_bridge_slave(ifp
,
1224 /* Delete interface notification from kernel */
1226 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1227 name
, ifi
->ifi_index
);
1231 if (IS_ZEBRA_DEBUG_KERNEL
)
1232 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1235 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1237 /* Special handling for bridge or VxLAN interfaces. */
1238 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1239 zebra_l2_bridge_del(ifp
);
1240 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1241 zebra_l2_vxlanif_del(ifp
);
1243 if (!IS_ZEBRA_IF_VRF(ifp
))
1244 if_delete_update(ifp
);
1250 /* Interface information read by netlink. */
1251 void interface_list(struct zebra_ns
*zns
)
1253 interface_lookup_netlink(zns
);
1256 #endif /* GNU_LINUX */