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 <net/if_arp.h>
35 #include <linux/sockios.h>
36 #include <linux/ethtool.h>
42 #include "connected.h"
45 #include "zebra_memory.h"
55 #include "zebra/zserv.h"
56 #include "zebra/zebra_ns.h"
57 #include "zebra/zebra_vrf.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/interface.h"
61 #include "zebra/debug.h"
62 #include "zebra/rtadv.h"
63 #include "zebra/zebra_ptm.h"
64 #include "zebra/zebra_mpls.h"
65 #include "zebra/kernel_netlink.h"
66 #include "zebra/if_netlink.h"
69 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
70 names and ifindex values. */
71 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
74 struct interface
*oifp
;
76 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
78 if (ifi_index
== IFINDEX_INTERNAL
)
80 "Netlink is setting interface %s ifindex to reserved "
82 ifp
->name
, ifi_index
);
84 if (IS_ZEBRA_DEBUG_KERNEL
)
86 "interface index %d was renamed from %s to %s",
87 ifi_index
, oifp
->name
, ifp
->name
);
90 "interface rename detected on up interface: index %d "
91 "was renamed from %s to %s, results are uncertain!",
92 ifi_index
, oifp
->name
, ifp
->name
);
93 if_delete_update(oifp
);
96 ifp
->ifindex
= ifi_index
;
99 /* Utility function to parse hardware link-layer address and update ifp */
100 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
101 struct interface
*ifp
)
105 if (tb
[IFLA_ADDRESS
]) {
108 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
110 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
111 zlog_warn("Hardware address is too large: %d",
114 ifp
->hw_addr_len
= hw_addr_len
;
115 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
118 for (i
= 0; i
< hw_addr_len
; i
++)
119 if (ifp
->hw_addr
[i
] != 0)
122 if (i
== hw_addr_len
)
123 ifp
->hw_addr_len
= 0;
125 ifp
->hw_addr_len
= hw_addr_len
;
130 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
134 return ZEBRA_LLT_ETHER
;
136 return ZEBRA_LLT_EETHER
;
138 return ZEBRA_LLT_AX25
;
140 return ZEBRA_LLT_PRONET
;
142 return ZEBRA_LLT_IEEE802
;
144 return ZEBRA_LLT_ARCNET
;
145 case ARPHRD_APPLETLK
:
146 return ZEBRA_LLT_APPLETLK
;
148 return ZEBRA_LLT_DLCI
;
150 return ZEBRA_LLT_ATM
;
151 case ARPHRD_METRICOM
:
152 return ZEBRA_LLT_METRICOM
;
153 case ARPHRD_IEEE1394
:
154 return ZEBRA_LLT_IEEE1394
;
156 return ZEBRA_LLT_EUI64
;
157 case ARPHRD_INFINIBAND
:
158 return ZEBRA_LLT_INFINIBAND
;
160 return ZEBRA_LLT_SLIP
;
162 return ZEBRA_LLT_CSLIP
;
164 return ZEBRA_LLT_SLIP6
;
166 return ZEBRA_LLT_CSLIP6
;
168 return ZEBRA_LLT_RSRVD
;
170 return ZEBRA_LLT_ADAPT
;
172 return ZEBRA_LLT_ROSE
;
174 return ZEBRA_LLT_X25
;
176 return ZEBRA_LLT_PPP
;
178 return ZEBRA_LLT_CHDLC
;
180 return ZEBRA_LLT_LAPB
;
182 return ZEBRA_LLT_RAWHDLC
;
184 return ZEBRA_LLT_IPIP
;
186 return ZEBRA_LLT_IPIP6
;
188 return ZEBRA_LLT_FRAD
;
190 return ZEBRA_LLT_SKIP
;
191 case ARPHRD_LOOPBACK
:
192 return ZEBRA_LLT_LOOPBACK
;
193 case ARPHRD_LOCALTLK
:
194 return ZEBRA_LLT_LOCALTLK
;
196 return ZEBRA_LLT_FDDI
;
198 return ZEBRA_LLT_SIT
;
200 return ZEBRA_LLT_IPDDP
;
202 return ZEBRA_LLT_IPGRE
;
204 return ZEBRA_LLT_PIMREG
;
206 return ZEBRA_LLT_HIPPI
;
208 return ZEBRA_LLT_ECONET
;
210 return ZEBRA_LLT_IRDA
;
212 return ZEBRA_LLT_FCPP
;
214 return ZEBRA_LLT_FCAL
;
216 return ZEBRA_LLT_FCPL
;
217 case ARPHRD_FCFABRIC
:
218 return ZEBRA_LLT_FCFABRIC
;
219 case ARPHRD_IEEE802_TR
:
220 return ZEBRA_LLT_IEEE802_TR
;
221 case ARPHRD_IEEE80211
:
222 return ZEBRA_LLT_IEEE80211
;
223 case ARPHRD_IEEE802154
:
224 return ZEBRA_LLT_IEEE802154
;
227 return ZEBRA_LLT_IP6GRE
;
229 #ifdef ARPHRD_IEEE802154_PHY
230 case ARPHRD_IEEE802154_PHY
:
231 return ZEBRA_LLT_IEEE802154_PHY
;
235 return ZEBRA_LLT_UNKNOWN
;
239 static void netlink_determine_zebra_iftype(char *kind
, zebra_iftype_t
*zif_type
)
241 *zif_type
= ZEBRA_IF_OTHER
;
246 if (strcmp(kind
, "vrf") == 0)
247 *zif_type
= ZEBRA_IF_VRF
;
248 else if (strcmp(kind
, "bridge") == 0)
249 *zif_type
= ZEBRA_IF_BRIDGE
;
250 else if (strcmp(kind
, "vlan") == 0)
251 *zif_type
= ZEBRA_IF_VLAN
;
252 else if (strcmp(kind
, "vxlan") == 0)
253 *zif_type
= ZEBRA_IF_VXLAN
;
254 else if (strcmp(kind
, "macvlan") == 0)
255 *zif_type
= ZEBRA_IF_MACVLAN
;
258 // Temporary Assignments to compile on older platforms.
260 #define IFLA_BR_MAX 39
263 #ifndef IFLA_VXLAN_ID
264 #define IFLA_VXLAN_ID 1
267 #ifndef IFLA_VXLAN_LOCAL
268 #define IFLA_VXLAN_LOCAL 4
271 #ifndef IFLA_VXLAN_MAX
272 #define IFLA_VXLAN_MAX 26
275 #ifndef IFLA_BRIDGE_MAX
276 #define IFLA_BRIDGE_MAX 2
279 #ifndef IFLA_BRIDGE_VLAN_INFO
280 #define IFLA_BRIDGE_VLAN_INFO 2
283 #ifndef BRIDGE_VLAN_INFO_PVID
284 #define BRIDGE_VLAN_INFO_PVID (1<<1)
287 #ifndef RTEXT_FILTER_BRVLAN
288 #define RTEXT_FILTER_BRVLAN (1<<1)
292 #define NTF_SELF 0x02
295 #ifndef IFLA_BR_VLAN_FILTERING
296 #define IFLA_BR_VLAN_FILTERING 7
299 #define parse_rtattr_nested(tb, max, rta) \
300 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
302 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
305 struct ifinfomsg
*ifi
;
306 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
307 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
309 struct zebra_vrf
*zvrf
;
310 u_int32_t nl_table_id
;
314 memset(linkinfo
, 0, sizeof linkinfo
);
315 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
317 if (!linkinfo
[IFLA_INFO_DATA
]) {
318 if (IS_ZEBRA_DEBUG_KERNEL
)
320 "%s: IFLA_INFO_DATA missing from VRF message: %s",
325 memset(attr
, 0, sizeof attr
);
326 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
327 if (!attr
[IFLA_VRF_TABLE
]) {
328 if (IS_ZEBRA_DEBUG_KERNEL
)
330 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
335 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
337 if (h
->nlmsg_type
== RTM_NEWLINK
) {
338 if (IS_ZEBRA_DEBUG_KERNEL
)
339 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
340 ifi
->ifi_index
, nl_table_id
);
343 * vrf_get is implied creation if it does not exist
345 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
346 name
); // It would create vrf
348 zlog_err("VRF %s id %u not created", name
,
353 /* Enable the created VRF. */
354 if (!vrf_enable(vrf
)) {
355 zlog_err("Failed to enable VRF %s id %u", name
,
361 * This is the only place that we get the actual kernel table_id
362 * being used. We need it to set the table_id of the routes
363 * we are passing to the kernel.... And to throw some totally
364 * awesome parties. that too.
366 zvrf
= (struct zebra_vrf
*)vrf
->info
;
367 zvrf
->table_id
= nl_table_id
;
368 } else // h->nlmsg_type == RTM_DELLINK
370 if (IS_ZEBRA_DEBUG_KERNEL
)
371 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
374 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
377 zlog_warn("%s: vrf not found", __func__
);
385 static int get_iflink_speed(const char *ifname
)
388 struct ethtool_cmd ecmd
;
392 /* initialize struct */
393 memset(&ifdata
, 0, sizeof(ifdata
));
395 /* set interface name */
396 strcpy(ifdata
.ifr_name
, ifname
);
398 /* initialize ethtool interface */
399 memset(&ecmd
, 0, sizeof(ecmd
));
400 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
401 ifdata
.ifr_data
= (__caddr_t
)&ecmd
;
403 /* use ioctl to get IP address of an interface */
404 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
406 if (IS_ZEBRA_DEBUG_KERNEL
)
407 zlog_debug("Failure to read interface %s speed: %d %s",
408 ifname
, errno
, safe_strerror(errno
));
412 /* Get the current link state for the interface */
413 rc
= ioctl(sd
, SIOCETHTOOL
, (char *)&ifdata
);
415 if (IS_ZEBRA_DEBUG_KERNEL
)
417 "IOCTL failure to read interface %s speed: %d %s",
418 ifname
, errno
, safe_strerror(errno
));
425 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
428 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
429 struct zebra_l2info_bridge
*bridge_info
)
431 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
433 memset(bridge_info
, 0, sizeof(*bridge_info
));
434 memset(attr
, 0, sizeof attr
);
435 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
436 if (attr
[IFLA_BR_VLAN_FILTERING
])
437 bridge_info
->vlan_aware
=
438 *(u_char
*)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
442 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
443 struct zebra_l2info_vlan
*vlan_info
)
445 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
448 memset(vlan_info
, 0, sizeof(*vlan_info
));
449 memset(attr
, 0, sizeof attr
);
450 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
451 if (!attr
[IFLA_VLAN_ID
]) {
452 if (IS_ZEBRA_DEBUG_KERNEL
)
453 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
457 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
458 vlan_info
->vid
= vid_in_msg
;
462 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
463 struct zebra_l2info_vxlan
*vxl_info
)
465 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
467 struct in_addr vtep_ip_in_msg
;
469 memset(vxl_info
, 0, sizeof(*vxl_info
));
470 memset(attr
, 0, sizeof attr
);
471 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
472 if (!attr
[IFLA_VXLAN_ID
]) {
473 if (IS_ZEBRA_DEBUG_KERNEL
)
475 "IFLA_VXLAN_ID missing from VXLAN IF message");
479 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
480 vxl_info
->vni
= vni_in_msg
;
481 if (!attr
[IFLA_VXLAN_LOCAL
]) {
482 if (IS_ZEBRA_DEBUG_KERNEL
)
484 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
487 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
488 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
495 * Extract and save L2 params (of interest) for an interface. When a
496 * bridge interface is added or updated, take further actions to map
497 * its members. Likewise, for VxLAN interface.
499 static void netlink_interface_update_l2info(struct interface
*ifp
,
500 struct rtattr
*link_data
, int add
)
505 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
506 struct zebra_l2info_bridge bridge_info
;
508 netlink_extract_bridge_info(link_data
, &bridge_info
);
509 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
510 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
511 struct zebra_l2info_vlan vlan_info
;
513 netlink_extract_vlan_info(link_data
, &vlan_info
);
514 zebra_l2_vlanif_update(ifp
, &vlan_info
);
515 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
516 struct zebra_l2info_vxlan vxlan_info
;
518 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
519 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
523 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
527 struct ifinfomsg
*ifi
;
528 struct rtattr
*tb
[IFLA_MAX
+ 1];
529 struct interface
*ifp
;
530 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
535 vlanid_t access_vlan
;
537 /* Fetch name and ifindex */
539 memset(tb
, 0, sizeof tb
);
540 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
542 if (tb
[IFLA_IFNAME
] == NULL
)
544 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
546 /* The interface should already be known, if not discard. */
547 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
549 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
552 if (!IS_ZEBRA_IF_VXLAN(ifp
))
555 /* We are only interested in the access VLAN i.e., AF_SPEC */
556 if (!tb
[IFLA_AF_SPEC
])
559 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
560 * only 1 access VLAN is accepted.
562 memset(aftb
, 0, sizeof aftb
);
563 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
564 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
567 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
568 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
571 access_vlan
= (vlanid_t
)vinfo
->vid
;
572 if (IS_ZEBRA_DEBUG_KERNEL
)
573 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
574 name
, ifi
->ifi_index
);
575 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
579 /* Called from interface_lookup_netlink(). This function is only used
581 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
582 ns_id_t ns_id
, int startup
)
585 struct ifinfomsg
*ifi
;
586 struct rtattr
*tb
[IFLA_MAX
+ 1];
587 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
588 struct interface
*ifp
;
591 char *slave_kind
= NULL
;
592 struct zebra_ns
*zns
;
593 vrf_id_t vrf_id
= VRF_DEFAULT
;
594 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
595 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
596 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
597 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
599 zns
= zebra_ns_lookup(ns_id
);
602 if (h
->nlmsg_type
!= RTM_NEWLINK
)
605 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
609 /* We are interested in some AF_BRIDGE notifications. */
610 if (ifi
->ifi_family
== AF_BRIDGE
)
611 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
613 /* Looking up interface name. */
614 memset(tb
, 0, sizeof tb
);
615 memset(linkinfo
, 0, sizeof linkinfo
);
616 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
619 /* check for wireless messages to ignore */
620 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
621 if (IS_ZEBRA_DEBUG_KERNEL
)
622 zlog_debug("%s: ignoring IFLA_WIRELESS message",
626 #endif /* IFLA_WIRELESS */
628 if (tb
[IFLA_IFNAME
] == NULL
)
630 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
632 if (tb
[IFLA_LINKINFO
]) {
633 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
635 if (linkinfo
[IFLA_INFO_KIND
])
636 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
638 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
639 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
640 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
643 netlink_determine_zebra_iftype(kind
, &zif_type
);
646 /* If VRF, create the VRF structure itself. */
647 if (zif_type
== ZEBRA_IF_VRF
) {
648 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
649 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
652 if (tb
[IFLA_MASTER
]) {
653 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
654 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
655 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
656 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
657 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
659 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
661 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
664 /* If linking to another interface, note it. */
666 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
669 ifp
= if_get_by_name(name
, vrf_id
);
670 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
671 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
672 if (IS_ZEBRA_IF_VRF(ifp
))
673 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
674 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
676 ifp
->speed
= get_iflink_speed(name
);
677 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
679 /* Set zebra interface type */
680 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
683 zebra_if_update_link(ifp
, link_ifindex
);
685 /* Hardware type and address. */
686 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
687 netlink_interface_update_hw_addr(tb
, ifp
);
691 /* Extract and save L2 interface information, take additional actions.
693 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
694 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
695 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
700 /* Request for specific interface or address information from the kernel */
701 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
702 u_int32_t filter_mask
)
706 struct ifinfomsg ifm
;
710 /* Form the request, specifying filter (rtattr) if needed. */
711 memset(&req
, 0, sizeof(req
));
712 req
.n
.nlmsg_type
= type
;
713 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
714 req
.ifm
.ifi_family
= family
;
716 /* Include filter, if specified. */
718 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
720 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
723 /* Interface lookup by netlink socket. */
724 int interface_lookup_netlink(struct zebra_ns
*zns
)
728 /* Get interface information. */
729 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
732 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
737 /* Get interface information - for bridge interfaces. */
738 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
739 RTEXT_FILTER_BRVLAN
);
742 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
747 /* Get interface information - for bridge interfaces. */
748 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
749 RTEXT_FILTER_BRVLAN
);
752 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
757 /* Get IPv4 address of the interfaces. */
758 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
761 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
766 /* Get IPv6 address of the interfaces. */
767 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
770 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
778 int kernel_interface_set_master(struct interface
*master
,
779 struct interface
*slave
)
781 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
785 struct ifinfomsg ifa
;
786 char buf
[NL_PKT_BUF_SIZE
];
789 memset(&req
, 0, sizeof req
);
791 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
792 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
793 req
.n
.nlmsg_type
= RTM_SETLINK
;
794 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
796 req
.ifa
.ifi_index
= slave
->ifindex
;
798 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
799 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
801 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
805 /* Interface address modification. */
806 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
807 struct connected
*ifc
)
814 struct ifaddrmsg ifa
;
815 char buf
[NL_PKT_BUF_SIZE
];
818 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
821 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
823 bytelen
= (family
== AF_INET
? 4 : 16);
825 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
826 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
827 req
.n
.nlmsg_type
= cmd
;
828 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
830 req
.ifa
.ifa_family
= family
;
832 req
.ifa
.ifa_index
= ifp
->ifindex
;
833 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
835 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
837 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
) {
838 if (!CONNECTED_PEER(ifc
) && ifc
->destination
) {
839 p
= ifc
->destination
;
840 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
841 &p
->u
.prefix
, bytelen
);
845 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
846 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
849 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
850 strlen(ifc
->label
) + 1);
852 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
856 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
858 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
861 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
863 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
866 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
867 ns_id_t ns_id
, int startup
)
870 struct ifaddrmsg
*ifa
;
871 struct rtattr
*tb
[IFA_MAX
+ 1];
872 struct interface
*ifp
;
877 struct zebra_ns
*zns
;
879 zns
= zebra_ns_lookup(ns_id
);
882 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
885 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
888 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
892 memset(tb
, 0, sizeof tb
);
893 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
895 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
898 "netlink_interface_addr can't find interface by index %d",
903 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
906 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
907 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
910 zlog_debug(" IFA_LOCAL %s/%d",
911 inet_ntop(ifa
->ifa_family
,
912 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
916 zlog_debug(" IFA_ADDRESS %s/%d",
917 inet_ntop(ifa
->ifa_family
,
918 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
921 if (tb
[IFA_BROADCAST
])
922 zlog_debug(" IFA_BROADCAST %s/%d",
923 inet_ntop(ifa
->ifa_family
,
924 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
927 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
928 zlog_debug(" IFA_LABEL %s",
929 (char *)RTA_DATA(tb
[IFA_LABEL
]));
931 if (tb
[IFA_CACHEINFO
]) {
932 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
933 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
934 ci
->ifa_prefered
, ci
->ifa_valid
);
938 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
939 if (tb
[IFA_LOCAL
] == NULL
)
940 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
941 if (tb
[IFA_ADDRESS
] == NULL
)
942 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
944 /* local interface address */
945 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
947 /* is there a peer address? */
949 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
950 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
951 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
952 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
954 /* seeking a broadcast address */
955 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
958 /* addr is primary key, SOL if we don't have one */
960 zlog_debug("%s: NULL address", __func__
);
965 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
966 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
970 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
972 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
975 /* Register interface address to the interface. */
976 if (ifa
->ifa_family
== AF_INET
) {
977 if (h
->nlmsg_type
== RTM_NEWADDR
)
978 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
980 (struct in_addr
*)broad
, label
);
982 connected_delete_ipv4(
983 ifp
, flags
, (struct in_addr
*)addr
,
984 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
986 if (ifa
->ifa_family
== AF_INET6
) {
987 if (h
->nlmsg_type
== RTM_NEWADDR
) {
988 /* Only consider valid addresses; we'll not get a
990 * the kernel till IPv6 DAD has completed, but at init
992 * does query for and will receive all addresses.
995 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
997 ifp
, flags
, (struct in6_addr
*)addr
,
999 (struct in6_addr
*)broad
, label
);
1001 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1003 (struct in6_addr
*)broad
);
1009 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1010 ns_id_t ns_id
, int startup
)
1013 struct ifinfomsg
*ifi
;
1014 struct rtattr
*tb
[IFLA_MAX
+ 1];
1015 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1016 struct interface
*ifp
;
1019 char *slave_kind
= NULL
;
1020 struct zebra_ns
*zns
;
1021 vrf_id_t vrf_id
= VRF_DEFAULT
;
1022 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1023 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1024 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1025 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1028 zns
= zebra_ns_lookup(ns_id
);
1029 ifi
= NLMSG_DATA(h
);
1031 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1032 /* If this is not link add/delete message so print warning. */
1033 zlog_warn("netlink_link_change: wrong kernel message %d",
1038 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1042 /* We are interested in some AF_BRIDGE notifications. */
1043 if (ifi
->ifi_family
== AF_BRIDGE
)
1044 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1046 /* Looking up interface name. */
1047 memset(tb
, 0, sizeof tb
);
1048 memset(linkinfo
, 0, sizeof linkinfo
);
1049 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1051 #ifdef IFLA_WIRELESS
1052 /* check for wireless messages to ignore */
1053 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1054 if (IS_ZEBRA_DEBUG_KERNEL
)
1055 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1059 #endif /* IFLA_WIRELESS */
1061 if (tb
[IFLA_IFNAME
] == NULL
)
1063 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1065 if (tb
[IFLA_LINKINFO
]) {
1066 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1068 if (linkinfo
[IFLA_INFO_KIND
])
1069 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1071 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1072 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1073 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1076 netlink_determine_zebra_iftype(kind
, &zif_type
);
1079 /* If linking to another interface, note it. */
1081 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1083 /* If VRF, create or update the VRF structure itself. */
1084 if (zif_type
== ZEBRA_IF_VRF
) {
1085 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1086 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1089 /* See if interface is present. */
1090 ifp
= if_lookup_by_name_per_ns(zns
, name
);
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
);
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 */