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"
57 #include "zebra/zserv.h"
58 #include "zebra/zebra_ns.h"
59 #include "zebra/zebra_vrf.h"
61 #include "zebra/redistribute.h"
62 #include "zebra/interface.h"
63 #include "zebra/debug.h"
64 #include "zebra/rtadv.h"
65 #include "zebra/zebra_ptm.h"
66 #include "zebra/zebra_mpls.h"
67 #include "zebra/kernel_netlink.h"
68 #include "zebra/if_netlink.h"
70 extern struct zebra_privs_t zserv_privs
;
72 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
73 names and ifindex values. */
74 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
77 struct interface
*oifp
;
79 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
81 if (ifi_index
== IFINDEX_INTERNAL
)
83 "Netlink is setting interface %s ifindex to reserved "
85 ifp
->name
, ifi_index
);
87 if (IS_ZEBRA_DEBUG_KERNEL
)
89 "interface index %d was renamed from %s to %s",
90 ifi_index
, oifp
->name
, ifp
->name
);
93 "interface rename detected on up interface: index %d "
94 "was renamed from %s to %s, results are uncertain!",
95 ifi_index
, oifp
->name
, ifp
->name
);
96 if_delete_update(oifp
);
99 if_set_index(ifp
, ifi_index
);
102 /* Utility function to parse hardware link-layer address and update ifp */
103 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
104 struct interface
*ifp
)
108 if (tb
[IFLA_ADDRESS
]) {
111 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
113 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
114 zlog_warn("Hardware address is too large: %d",
117 ifp
->hw_addr_len
= hw_addr_len
;
118 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
121 for (i
= 0; i
< hw_addr_len
; i
++)
122 if (ifp
->hw_addr
[i
] != 0)
125 if (i
== hw_addr_len
)
126 ifp
->hw_addr_len
= 0;
128 ifp
->hw_addr_len
= hw_addr_len
;
133 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
137 return ZEBRA_LLT_ETHER
;
139 return ZEBRA_LLT_EETHER
;
141 return ZEBRA_LLT_AX25
;
143 return ZEBRA_LLT_PRONET
;
145 return ZEBRA_LLT_IEEE802
;
147 return ZEBRA_LLT_ARCNET
;
148 case ARPHRD_APPLETLK
:
149 return ZEBRA_LLT_APPLETLK
;
151 return ZEBRA_LLT_DLCI
;
153 return ZEBRA_LLT_ATM
;
154 case ARPHRD_METRICOM
:
155 return ZEBRA_LLT_METRICOM
;
156 case ARPHRD_IEEE1394
:
157 return ZEBRA_LLT_IEEE1394
;
159 return ZEBRA_LLT_EUI64
;
160 case ARPHRD_INFINIBAND
:
161 return ZEBRA_LLT_INFINIBAND
;
163 return ZEBRA_LLT_SLIP
;
165 return ZEBRA_LLT_CSLIP
;
167 return ZEBRA_LLT_SLIP6
;
169 return ZEBRA_LLT_CSLIP6
;
171 return ZEBRA_LLT_RSRVD
;
173 return ZEBRA_LLT_ADAPT
;
175 return ZEBRA_LLT_ROSE
;
177 return ZEBRA_LLT_X25
;
179 return ZEBRA_LLT_PPP
;
181 return ZEBRA_LLT_CHDLC
;
183 return ZEBRA_LLT_LAPB
;
185 return ZEBRA_LLT_RAWHDLC
;
187 return ZEBRA_LLT_IPIP
;
189 return ZEBRA_LLT_IPIP6
;
191 return ZEBRA_LLT_FRAD
;
193 return ZEBRA_LLT_SKIP
;
194 case ARPHRD_LOOPBACK
:
195 return ZEBRA_LLT_LOOPBACK
;
196 case ARPHRD_LOCALTLK
:
197 return ZEBRA_LLT_LOCALTLK
;
199 return ZEBRA_LLT_FDDI
;
201 return ZEBRA_LLT_SIT
;
203 return ZEBRA_LLT_IPDDP
;
205 return ZEBRA_LLT_IPGRE
;
207 return ZEBRA_LLT_PIMREG
;
209 return ZEBRA_LLT_HIPPI
;
211 return ZEBRA_LLT_ECONET
;
213 return ZEBRA_LLT_IRDA
;
215 return ZEBRA_LLT_FCPP
;
217 return ZEBRA_LLT_FCAL
;
219 return ZEBRA_LLT_FCPL
;
220 case ARPHRD_FCFABRIC
:
221 return ZEBRA_LLT_FCFABRIC
;
222 case ARPHRD_IEEE802_TR
:
223 return ZEBRA_LLT_IEEE802_TR
;
224 case ARPHRD_IEEE80211
:
225 return ZEBRA_LLT_IEEE80211
;
226 #ifdef ARPHRD_IEEE802154
227 case ARPHRD_IEEE802154
:
228 return ZEBRA_LLT_IEEE802154
;
232 return ZEBRA_LLT_IP6GRE
;
234 #ifdef ARPHRD_IEEE802154_PHY
235 case ARPHRD_IEEE802154_PHY
:
236 return ZEBRA_LLT_IEEE802154_PHY
;
240 return ZEBRA_LLT_UNKNOWN
;
244 static void netlink_determine_zebra_iftype(char *kind
, zebra_iftype_t
*zif_type
)
246 *zif_type
= ZEBRA_IF_OTHER
;
251 if (strcmp(kind
, "vrf") == 0)
252 *zif_type
= ZEBRA_IF_VRF
;
253 else if (strcmp(kind
, "bridge") == 0)
254 *zif_type
= ZEBRA_IF_BRIDGE
;
255 else if (strcmp(kind
, "vlan") == 0)
256 *zif_type
= ZEBRA_IF_VLAN
;
257 else if (strcmp(kind
, "vxlan") == 0)
258 *zif_type
= ZEBRA_IF_VXLAN
;
259 else if (strcmp(kind
, "macvlan") == 0)
260 *zif_type
= ZEBRA_IF_MACVLAN
;
263 #define parse_rtattr_nested(tb, max, rta) \
264 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
266 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
269 struct ifinfomsg
*ifi
;
270 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
271 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
273 struct zebra_vrf
*zvrf
;
274 uint32_t nl_table_id
;
278 memset(linkinfo
, 0, sizeof linkinfo
);
279 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
281 if (!linkinfo
[IFLA_INFO_DATA
]) {
282 if (IS_ZEBRA_DEBUG_KERNEL
)
284 "%s: IFLA_INFO_DATA missing from VRF message: %s",
289 memset(attr
, 0, sizeof attr
);
290 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
291 if (!attr
[IFLA_VRF_TABLE
]) {
292 if (IS_ZEBRA_DEBUG_KERNEL
)
294 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
299 nl_table_id
= *(uint32_t *)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
301 if (h
->nlmsg_type
== RTM_NEWLINK
) {
302 if (IS_ZEBRA_DEBUG_KERNEL
)
303 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
304 ifi
->ifi_index
, nl_table_id
);
307 * vrf_get is implied creation if it does not exist
309 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
310 name
); // It would create vrf
312 zlog_err("VRF %s id %u not created", name
,
318 * This is the only place that we get the actual kernel table_id
319 * being used. We need it to set the table_id of the routes
320 * we are passing to the kernel.... And to throw some totally
321 * awesome parties. that too.
323 * At this point we *must* have a zvrf because the vrf_create
324 * callback creates one. We *must* set the table id
325 * before the vrf_enable because of( at the very least )
326 * static routes being delayed for installation until
327 * during the vrf_enable callbacks.
329 zvrf
= (struct zebra_vrf
*)vrf
->info
;
330 zvrf
->table_id
= nl_table_id
;
332 /* Enable the created VRF. */
333 if (!vrf_enable(vrf
)) {
334 zlog_err("Failed to enable VRF %s id %u", name
,
339 } else // h->nlmsg_type == RTM_DELLINK
341 if (IS_ZEBRA_DEBUG_KERNEL
)
342 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
345 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
348 zlog_warn("%s: vrf not found", __func__
);
356 static int get_iflink_speed(struct interface
*interface
)
359 struct ethtool_cmd ecmd
;
362 const char *ifname
= interface
->name
;
364 /* initialize struct */
365 memset(&ifdata
, 0, sizeof(ifdata
));
367 /* set interface name */
368 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
370 /* initialize ethtool interface */
371 memset(&ecmd
, 0, sizeof(ecmd
));
372 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
373 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
375 /* use ioctl to get IP address of an interface */
376 if (zserv_privs
.change(ZPRIVS_RAISE
))
377 zlog_err("Can't raise privileges");
378 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
, interface
->vrf_id
,
381 if (IS_ZEBRA_DEBUG_KERNEL
)
382 zlog_debug("Failure to read interface %s speed: %d %s",
383 ifname
, errno
, safe_strerror(errno
));
386 /* Get the current link state for the interface */
387 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
, (char *)&ifdata
);
388 if (zserv_privs
.change(ZPRIVS_LOWER
))
389 zlog_err("Can't lower privileges");
391 if (IS_ZEBRA_DEBUG_KERNEL
)
393 "IOCTL failure to read interface %s speed: %d %s",
394 ifname
, errno
, safe_strerror(errno
));
401 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
404 uint32_t kernel_get_speed(struct interface
*ifp
)
406 return get_iflink_speed(ifp
);
409 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
410 struct zebra_l2info_bridge
*bridge_info
)
412 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
414 memset(bridge_info
, 0, sizeof(*bridge_info
));
415 memset(attr
, 0, sizeof attr
);
416 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
417 if (attr
[IFLA_BR_VLAN_FILTERING
])
418 bridge_info
->vlan_aware
=
419 *(uint8_t *)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
423 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
424 struct zebra_l2info_vlan
*vlan_info
)
426 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
429 memset(vlan_info
, 0, sizeof(*vlan_info
));
430 memset(attr
, 0, sizeof attr
);
431 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
432 if (!attr
[IFLA_VLAN_ID
]) {
433 if (IS_ZEBRA_DEBUG_KERNEL
)
434 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
438 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
439 vlan_info
->vid
= vid_in_msg
;
443 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
444 struct zebra_l2info_vxlan
*vxl_info
)
446 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
448 struct in_addr vtep_ip_in_msg
;
450 memset(vxl_info
, 0, sizeof(*vxl_info
));
451 memset(attr
, 0, sizeof attr
);
452 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
453 if (!attr
[IFLA_VXLAN_ID
]) {
454 if (IS_ZEBRA_DEBUG_KERNEL
)
456 "IFLA_VXLAN_ID missing from VXLAN IF message");
460 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
461 vxl_info
->vni
= vni_in_msg
;
462 if (!attr
[IFLA_VXLAN_LOCAL
]) {
463 if (IS_ZEBRA_DEBUG_KERNEL
)
465 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
468 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
469 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
476 * Extract and save L2 params (of interest) for an interface. When a
477 * bridge interface is added or updated, take further actions to map
478 * its members. Likewise, for VxLAN interface.
480 static void netlink_interface_update_l2info(struct interface
*ifp
,
481 struct rtattr
*link_data
, int add
)
486 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
487 struct zebra_l2info_bridge bridge_info
;
489 netlink_extract_bridge_info(link_data
, &bridge_info
);
490 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
491 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
492 struct zebra_l2info_vlan vlan_info
;
494 netlink_extract_vlan_info(link_data
, &vlan_info
);
495 zebra_l2_vlanif_update(ifp
, &vlan_info
);
496 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
497 struct zebra_l2info_vxlan vxlan_info
;
499 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
500 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
504 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
508 struct ifinfomsg
*ifi
;
509 struct rtattr
*tb
[IFLA_MAX
+ 1];
510 struct interface
*ifp
;
511 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
516 vlanid_t access_vlan
;
518 /* Fetch name and ifindex */
520 memset(tb
, 0, sizeof tb
);
521 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
523 if (tb
[IFLA_IFNAME
] == NULL
)
525 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
527 /* The interface should already be known, if not discard. */
528 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
530 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
533 if (!IS_ZEBRA_IF_VXLAN(ifp
))
536 /* We are only interested in the access VLAN i.e., AF_SPEC */
537 if (!tb
[IFLA_AF_SPEC
])
540 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
541 * only 1 access VLAN is accepted.
543 memset(aftb
, 0, sizeof aftb
);
544 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
545 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
548 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
549 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
552 access_vlan
= (vlanid_t
)vinfo
->vid
;
553 if (IS_ZEBRA_DEBUG_KERNEL
)
554 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
555 name
, ifi
->ifi_index
);
556 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
560 /* Called from interface_lookup_netlink(). This function is only used
562 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
563 ns_id_t ns_id
, int startup
)
566 struct ifinfomsg
*ifi
;
567 struct rtattr
*tb
[IFLA_MAX
+ 1];
568 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
569 struct interface
*ifp
;
573 char *slave_kind
= NULL
;
574 struct zebra_ns
*zns
;
575 vrf_id_t vrf_id
= VRF_DEFAULT
;
576 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
577 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
578 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
579 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
581 zns
= zebra_ns_lookup(ns_id
);
584 if (h
->nlmsg_type
!= RTM_NEWLINK
)
587 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
591 /* We are interested in some AF_BRIDGE notifications. */
592 if (ifi
->ifi_family
== AF_BRIDGE
)
593 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
595 /* Looking up interface name. */
596 memset(tb
, 0, sizeof tb
);
597 memset(linkinfo
, 0, sizeof linkinfo
);
598 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
600 /* check for wireless messages to ignore */
601 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
602 if (IS_ZEBRA_DEBUG_KERNEL
)
603 zlog_debug("%s: ignoring IFLA_WIRELESS message",
608 if (tb
[IFLA_IFNAME
] == NULL
)
610 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
612 if (tb
[IFLA_IFALIAS
])
613 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
615 if (tb
[IFLA_LINKINFO
]) {
616 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
618 if (linkinfo
[IFLA_INFO_KIND
])
619 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
621 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
622 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
623 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
626 netlink_determine_zebra_iftype(kind
, &zif_type
);
629 /* If VRF, create the VRF structure itself. */
630 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
631 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
632 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
635 if (tb
[IFLA_MASTER
]) {
636 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
637 && !vrf_is_backend_netns()) {
638 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
639 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
640 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
641 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
643 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
645 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
647 if (vrf_is_backend_netns())
648 vrf_id
= (vrf_id_t
)ns_id
;
650 /* If linking to another interface, note it. */
652 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
655 ifp
= if_get_by_name(name
, vrf_id
, 0);
656 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
657 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
658 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
660 ifp
->speed
= get_iflink_speed(ifp
);
661 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
664 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
666 /* Set zebra interface type */
667 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
668 if (IS_ZEBRA_IF_VRF(ifp
))
669 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
672 zebra_if_update_link(ifp
, link_ifindex
);
674 /* Hardware type and address. */
675 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
676 netlink_interface_update_hw_addr(tb
, ifp
);
680 /* Extract and save L2 interface information, take additional actions.
682 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
683 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
684 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
689 /* Request for specific interface or address information from the kernel */
690 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
691 uint32_t filter_mask
)
695 struct ifinfomsg ifm
;
699 /* Form the request, specifying filter (rtattr) if needed. */
700 memset(&req
, 0, sizeof(req
));
701 req
.n
.nlmsg_type
= type
;
702 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
703 req
.ifm
.ifi_family
= family
;
705 /* Include filter, if specified. */
707 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
709 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
712 /* Interface lookup by netlink socket. */
713 int interface_lookup_netlink(struct zebra_ns
*zns
)
717 /* Get interface information. */
718 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
721 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
726 /* Get interface information - for bridge interfaces. */
727 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
728 RTEXT_FILTER_BRVLAN
);
731 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
736 /* Get interface information - for bridge interfaces. */
737 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
738 RTEXT_FILTER_BRVLAN
);
741 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
746 /* Get IPv4 address of the interfaces. */
747 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
750 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
755 /* Get IPv6 address of the interfaces. */
756 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
759 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
767 int kernel_interface_set_master(struct interface
*master
,
768 struct interface
*slave
)
770 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
774 struct ifinfomsg ifa
;
775 char buf
[NL_PKT_BUF_SIZE
];
778 memset(&req
, 0, sizeof req
);
780 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
781 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
782 req
.n
.nlmsg_type
= RTM_SETLINK
;
783 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
785 req
.ifa
.ifi_index
= slave
->ifindex
;
787 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
788 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
790 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
794 /* Interface address modification. */
795 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
796 struct connected
*ifc
)
803 struct ifaddrmsg ifa
;
804 char buf
[NL_PKT_BUF_SIZE
];
807 struct zebra_ns
*zns
;
809 if (vrf_is_backend_netns())
810 zns
= zebra_ns_lookup((ns_id_t
)ifp
->vrf_id
);
812 zns
= zebra_ns_lookup(NS_DEFAULT
);
814 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
816 bytelen
= (family
== AF_INET
? 4 : 16);
818 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
819 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
820 req
.n
.nlmsg_type
= cmd
;
821 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
823 req
.ifa
.ifa_family
= family
;
825 req
.ifa
.ifa_index
= ifp
->ifindex
;
827 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
829 if (family
== AF_INET
) {
830 if (CONNECTED_PEER(ifc
)) {
831 p
= ifc
->destination
;
832 addattr_l(&req
.n
, sizeof req
, IFA_ADDRESS
, &p
->u
.prefix
,
834 } else if (cmd
== RTM_NEWADDR
&& ifc
->destination
) {
835 p
= ifc
->destination
;
836 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
837 &p
->u
.prefix
, bytelen
);
841 /* p is now either ifc->address or ifc->destination */
842 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
844 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
845 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
848 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
849 strlen(ifc
->label
) + 1);
851 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
855 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
857 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
860 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
862 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
865 int kernel_address_add_ipv6(struct interface
*ifp
, struct connected
*ifc
)
867 return netlink_address(RTM_NEWADDR
, AF_INET6
, ifp
, ifc
);
870 int kernel_address_delete_ipv6(struct interface
*ifp
, struct connected
*ifc
)
872 return netlink_address(RTM_DELADDR
, AF_INET6
, ifp
, ifc
);
875 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
876 ns_id_t ns_id
, int startup
)
879 struct ifaddrmsg
*ifa
;
880 struct rtattr
*tb
[IFA_MAX
+ 1];
881 struct interface
*ifp
;
886 struct zebra_ns
*zns
;
888 zns
= zebra_ns_lookup(ns_id
);
891 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
894 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
897 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
901 memset(tb
, 0, sizeof tb
);
902 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
904 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
907 "netlink_interface_addr can't find interface by index %d",
912 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
915 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
916 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
919 zlog_debug(" IFA_LOCAL %s/%d",
920 inet_ntop(ifa
->ifa_family
,
921 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
925 zlog_debug(" IFA_ADDRESS %s/%d",
926 inet_ntop(ifa
->ifa_family
,
927 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
930 if (tb
[IFA_BROADCAST
])
931 zlog_debug(" IFA_BROADCAST %s/%d",
932 inet_ntop(ifa
->ifa_family
,
933 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
936 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
937 zlog_debug(" IFA_LABEL %s",
938 (char *)RTA_DATA(tb
[IFA_LABEL
]));
940 if (tb
[IFA_CACHEINFO
]) {
941 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
942 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
943 ci
->ifa_prefered
, ci
->ifa_valid
);
947 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
948 if (tb
[IFA_LOCAL
] == NULL
)
949 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
950 if (tb
[IFA_ADDRESS
] == NULL
)
951 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
953 /* local interface address */
954 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
956 /* is there a peer address? */
958 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
959 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
960 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
961 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
963 /* seeking a broadcast address */
964 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
967 /* addr is primary key, SOL if we don't have one */
969 zlog_debug("%s: NULL address", __func__
);
974 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
975 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
979 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
981 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
984 /* Register interface address to the interface. */
985 if (ifa
->ifa_family
== AF_INET
) {
986 if (h
->nlmsg_type
== RTM_NEWADDR
)
987 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
989 (struct in_addr
*)broad
, label
);
991 connected_delete_ipv4(
992 ifp
, flags
, (struct in_addr
*)addr
,
993 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
995 if (ifa
->ifa_family
== AF_INET6
) {
996 if (h
->nlmsg_type
== RTM_NEWADDR
) {
997 /* Only consider valid addresses; we'll not get a
999 * the kernel till IPv6 DAD has completed, but at init
1001 * does query for and will receive all addresses.
1003 if (!(ifa
->ifa_flags
1004 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1005 connected_add_ipv6(ifp
, flags
,
1006 (struct in6_addr
*)addr
,
1007 (struct in6_addr
*)broad
,
1008 ifa
->ifa_prefixlen
, label
);
1010 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1011 (struct in6_addr
*)broad
,
1012 ifa
->ifa_prefixlen
);
1018 /* helper function called by if_netlink_change
1019 * to delete interfaces in case the interface moved
1022 static void if_netlink_check_ifp_instance_consistency(uint16_t cmd
,
1023 struct interface
*ifp
,
1026 struct interface
*other_ifp
;
1029 * look if interface name is also found on other netns
1030 * - only if vrf backend is netns
1031 * - do not concern lo interface
1032 * - then remove previous one
1033 * - for new link case, check found interface is not active
1035 if (!vrf_is_backend_netns() ||
1036 !strcmp(ifp
->name
, "lo"))
1038 other_ifp
= if_lookup_by_name_not_ns(ns_id
, ifp
->name
);
1041 /* because previous interface may be inactive,
1042 * interface is moved back to default vrf
1043 * then one may find the same pointer; ignore
1045 if (other_ifp
== ifp
)
1047 if ((cmd
== RTM_NEWLINK
)
1048 && (CHECK_FLAG(other_ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)))
1050 if (IS_ZEBRA_DEBUG_KERNEL
&& cmd
== RTM_NEWLINK
) {
1051 zlog_debug("RTM_NEWLINK %s(%u, VRF %u) replaces %s(%u, VRF %u)\n",
1058 } else if (IS_ZEBRA_DEBUG_KERNEL
&& cmd
== RTM_DELLINK
) {
1059 zlog_debug("RTM_DELLINK %s(%u, VRF %u) is replaced by %s(%u, VRF %u)\n",
1067 /* the found interface replaces the current one
1070 if (cmd
== RTM_DELLINK
)
1073 if_delete(other_ifp
);
1074 /* the found interface is replaced by the current one
1079 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1080 ns_id_t ns_id
, int startup
)
1083 struct ifinfomsg
*ifi
;
1084 struct rtattr
*tb
[IFLA_MAX
+ 1];
1085 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1086 struct interface
*ifp
;
1090 char *slave_kind
= NULL
;
1091 struct zebra_ns
*zns
;
1092 vrf_id_t vrf_id
= VRF_DEFAULT
;
1093 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1094 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1095 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1096 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1099 zns
= zebra_ns_lookup(ns_id
);
1100 ifi
= NLMSG_DATA(h
);
1102 /* assume if not default zns, then new VRF */
1103 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1104 /* If this is not link add/delete message so print warning. */
1105 zlog_warn("netlink_link_change: wrong kernel message %d",
1110 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1114 /* We are interested in some AF_BRIDGE notifications. */
1115 if (ifi
->ifi_family
== AF_BRIDGE
)
1116 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1118 /* Looking up interface name. */
1119 memset(tb
, 0, sizeof tb
);
1120 memset(linkinfo
, 0, sizeof linkinfo
);
1121 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1123 /* check for wireless messages to ignore */
1124 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1125 if (IS_ZEBRA_DEBUG_KERNEL
)
1126 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1131 if (tb
[IFLA_IFNAME
] == NULL
)
1133 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1135 if (tb
[IFLA_LINKINFO
]) {
1136 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1138 if (linkinfo
[IFLA_INFO_KIND
])
1139 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1141 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1142 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1143 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1146 netlink_determine_zebra_iftype(kind
, &zif_type
);
1149 /* If linking to another interface, note it. */
1151 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1153 if (tb
[IFLA_IFALIAS
]) {
1154 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1157 /* If VRF, create or update the VRF structure itself. */
1158 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1159 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1160 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1163 /* See if interface is present. */
1164 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1168 XFREE(MTYPE_TMP
, ifp
->desc
);
1170 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1173 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1174 if (tb
[IFLA_MASTER
]) {
1175 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1176 && !vrf_is_backend_netns()) {
1177 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1178 vrf_id
= *(uint32_t *)RTA_DATA(tb
[IFLA_MASTER
]);
1179 } else if (slave_kind
1180 && (strcmp(slave_kind
, "bridge") == 0)) {
1181 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1183 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1185 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1187 if (vrf_is_backend_netns())
1188 vrf_id
= (vrf_id_t
)ns_id
;
1190 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1191 /* Add interface notification from kernel */
1192 if (IS_ZEBRA_DEBUG_KERNEL
)
1194 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1195 "sl_type %d master %u flags 0x%x",
1196 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1197 zif_slave_type
, bridge_ifindex
,
1201 /* unknown interface */
1202 ifp
= if_get_by_name(name
, vrf_id
, 0);
1204 /* pre-configured interface, learnt now */
1205 if (ifp
->vrf_id
!= vrf_id
)
1206 if_update_to_new_vrf(ifp
, vrf_id
);
1209 /* Update interface information. */
1210 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1211 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
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
);
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 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK
,
1239 } else if (ifp
->vrf_id
!= vrf_id
) {
1240 /* VRF change for an interface. */
1241 if (IS_ZEBRA_DEBUG_KERNEL
)
1243 "RTM_NEWLINK vrf-change for %s(%u) "
1244 "vrf_id %u -> %u flags 0x%x",
1245 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1248 if_handle_vrf_change(ifp
, vrf_id
);
1250 int was_bridge_slave
;
1252 /* Interface update. */
1253 if (IS_ZEBRA_DEBUG_KERNEL
)
1255 "RTM_NEWLINK update for %s(%u) "
1256 "sl_type %d master %u flags 0x%x",
1257 name
, ifp
->ifindex
, zif_slave_type
,
1258 bridge_ifindex
, ifi
->ifi_flags
);
1260 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1261 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1264 /* Update interface type - NOTE: Only slave_type can
1266 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1267 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1269 netlink_interface_update_hw_addr(tb
, ifp
);
1271 if (if_is_no_ptm_operative(ifp
)) {
1272 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1273 if (!if_is_no_ptm_operative(ifp
)) {
1274 if (IS_ZEBRA_DEBUG_KERNEL
)
1276 "Intf %s(%u) has gone DOWN",
1277 name
, ifp
->ifindex
);
1279 } else if (if_is_operative(ifp
)) {
1280 /* Must notify client daemons of new
1281 * interface status. */
1282 if (IS_ZEBRA_DEBUG_KERNEL
)
1284 "Intf %s(%u) PTM up, notifying clients",
1285 name
, ifp
->ifindex
);
1286 zebra_interface_up_update(ifp
);
1289 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1290 if (if_is_operative(ifp
)) {
1291 if (IS_ZEBRA_DEBUG_KERNEL
)
1293 "Intf %s(%u) has come UP",
1294 name
, ifp
->ifindex
);
1299 /* Extract and save L2 interface information, take
1300 * additional actions. */
1301 netlink_interface_update_l2info(
1302 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1303 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1304 zebra_l2if_update_bridge_slave(ifp
,
1306 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK
,
1310 /* Delete interface notification from kernel */
1312 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1313 name
, ifi
->ifi_index
);
1317 if (IS_ZEBRA_DEBUG_KERNEL
)
1318 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1321 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1323 /* Special handling for bridge or VxLAN interfaces. */
1324 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1325 zebra_l2_bridge_del(ifp
);
1326 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1327 zebra_l2_vxlanif_del(ifp
);
1329 if (!IS_ZEBRA_IF_VRF(ifp
))
1330 if_delete_update(ifp
);
1331 if_netlink_check_ifp_instance_consistency(RTM_DELLINK
,
1338 /* Interface information read by netlink. */
1339 void interface_list(struct zebra_ns
*zns
)
1341 interface_lookup_netlink(zns
);
1344 #endif /* GNU_LINUX */