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
;
256 // Temporary Assignments to compile on older platforms.
258 #define IFLA_BR_MAX 39
261 #ifndef IFLA_VXLAN_ID
262 #define IFLA_VXLAN_ID 1
265 #ifndef IFLA_VXLAN_LOCAL
266 #define IFLA_VXLAN_LOCAL 4
269 #ifndef IFLA_VXLAN_MAX
270 #define IFLA_VXLAN_MAX 26
273 #ifndef IFLA_BRIDGE_MAX
274 #define IFLA_BRIDGE_MAX 2
277 #ifndef IFLA_BRIDGE_VLAN_INFO
278 #define IFLA_BRIDGE_VLAN_INFO 2
281 #ifndef BRIDGE_VLAN_INFO_PVID
282 #define BRIDGE_VLAN_INFO_PVID (1<<1)
285 #ifndef RTEXT_FILTER_BRVLAN
286 #define RTEXT_FILTER_BRVLAN (1<<1)
290 #define NTF_SELF 0x02
293 #ifndef IFLA_BR_VLAN_FILTERING
294 #define IFLA_BR_VLAN_FILTERING 7
297 #define parse_rtattr_nested(tb, max, rta) \
298 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
300 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
303 struct ifinfomsg
*ifi
;
304 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
305 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
307 struct zebra_vrf
*zvrf
;
308 u_int32_t nl_table_id
;
312 memset(linkinfo
, 0, sizeof linkinfo
);
313 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
315 if (!linkinfo
[IFLA_INFO_DATA
]) {
316 if (IS_ZEBRA_DEBUG_KERNEL
)
318 "%s: IFLA_INFO_DATA missing from VRF message: %s",
323 memset(attr
, 0, sizeof attr
);
324 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
325 if (!attr
[IFLA_VRF_TABLE
]) {
326 if (IS_ZEBRA_DEBUG_KERNEL
)
328 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
333 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
335 if (h
->nlmsg_type
== RTM_NEWLINK
) {
336 if (IS_ZEBRA_DEBUG_KERNEL
)
337 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
338 ifi
->ifi_index
, nl_table_id
);
341 * vrf_get is implied creation if it does not exist
343 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
344 name
); // It would create vrf
346 zlog_err("VRF %s id %u not created", name
,
351 /* Enable the created VRF. */
352 if (!vrf_enable(vrf
)) {
353 zlog_err("Failed to enable VRF %s id %u", name
,
359 * This is the only place that we get the actual kernel table_id
360 * being used. We need it to set the table_id of the routes
361 * we are passing to the kernel.... And to throw some totally
362 * awesome parties. that too.
364 zvrf
= (struct zebra_vrf
*)vrf
->info
;
365 zvrf
->table_id
= nl_table_id
;
366 } else // h->nlmsg_type == RTM_DELLINK
368 if (IS_ZEBRA_DEBUG_KERNEL
)
369 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
372 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
375 zlog_warn("%s: vrf not found", __func__
);
383 static int get_iflink_speed(const char *ifname
)
386 struct ethtool_cmd ecmd
;
390 /* initialize struct */
391 memset(&ifdata
, 0, sizeof(ifdata
));
393 /* set interface name */
394 strcpy(ifdata
.ifr_name
, ifname
);
396 /* initialize ethtool interface */
397 memset(&ecmd
, 0, sizeof(ecmd
));
398 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
399 ifdata
.ifr_data
= (__caddr_t
)&ecmd
;
401 /* use ioctl to get IP address of an interface */
402 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
404 zlog_debug("Failure to read interface %s speed: %d %s", ifname
,
405 errno
, safe_strerror(errno
));
409 /* Get the current link state for the interface */
410 rc
= ioctl(sd
, SIOCETHTOOL
, (char *)&ifdata
);
412 zlog_debug("IOCTL failure to read interface %s speed: %d %s",
413 ifname
, errno
, safe_strerror(errno
));
420 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
423 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
424 struct zebra_l2info_bridge
*bridge_info
)
426 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
428 memset(bridge_info
, 0, sizeof(*bridge_info
));
429 memset(attr
, 0, sizeof attr
);
430 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
431 if (attr
[IFLA_BR_VLAN_FILTERING
])
432 bridge_info
->vlan_aware
=
433 *(u_char
*)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
437 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
438 struct zebra_l2info_vlan
*vlan_info
)
440 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
443 memset(vlan_info
, 0, sizeof(*vlan_info
));
444 memset(attr
, 0, sizeof attr
);
445 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
446 if (!attr
[IFLA_VLAN_ID
]) {
447 if (IS_ZEBRA_DEBUG_KERNEL
)
448 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
452 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
453 vlan_info
->vid
= vid_in_msg
;
457 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
458 struct zebra_l2info_vxlan
*vxl_info
)
460 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
462 struct in_addr vtep_ip_in_msg
;
464 memset(vxl_info
, 0, sizeof(*vxl_info
));
465 memset(attr
, 0, sizeof attr
);
466 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
467 if (!attr
[IFLA_VXLAN_ID
]) {
468 if (IS_ZEBRA_DEBUG_KERNEL
)
470 "IFLA_VXLAN_ID missing from VXLAN IF message");
474 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
475 vxl_info
->vni
= vni_in_msg
;
476 if (!attr
[IFLA_VXLAN_LOCAL
]) {
477 if (IS_ZEBRA_DEBUG_KERNEL
)
479 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
482 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
483 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
490 * Extract and save L2 params (of interest) for an interface. When a
491 * bridge interface is added or updated, take further actions to map
492 * its members. Likewise, for VxLAN interface.
494 static void netlink_interface_update_l2info(struct interface
*ifp
,
495 struct rtattr
*link_data
, int add
)
500 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
501 struct zebra_l2info_bridge bridge_info
;
503 netlink_extract_bridge_info(link_data
, &bridge_info
);
504 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
505 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
506 struct zebra_l2info_vlan vlan_info
;
508 netlink_extract_vlan_info(link_data
, &vlan_info
);
509 zebra_l2_vlanif_update(ifp
, &vlan_info
);
510 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
511 struct zebra_l2info_vxlan vxlan_info
;
513 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
514 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
518 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
522 struct ifinfomsg
*ifi
;
523 struct rtattr
*tb
[IFLA_MAX
+ 1];
524 struct interface
*ifp
;
525 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
530 vlanid_t access_vlan
;
532 /* Fetch name and ifindex */
534 memset(tb
, 0, sizeof tb
);
535 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
537 if (tb
[IFLA_IFNAME
] == NULL
)
539 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
541 /* The interface should already be known, if not discard. */
542 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
544 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
547 if (!IS_ZEBRA_IF_VXLAN(ifp
))
550 /* We are only interested in the access VLAN i.e., AF_SPEC */
551 if (!tb
[IFLA_AF_SPEC
])
554 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
555 * only 1 access VLAN is accepted.
557 memset(aftb
, 0, sizeof aftb
);
558 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
559 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
562 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
563 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
566 access_vlan
= (vlanid_t
)vinfo
->vid
;
567 if (IS_ZEBRA_DEBUG_KERNEL
)
568 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
569 name
, ifi
->ifi_index
);
570 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
574 /* Called from interface_lookup_netlink(). This function is only used
576 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
577 ns_id_t ns_id
, int startup
)
580 struct ifinfomsg
*ifi
;
581 struct rtattr
*tb
[IFLA_MAX
+ 1];
582 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
583 struct interface
*ifp
;
586 char *slave_kind
= NULL
;
587 struct zebra_ns
*zns
;
588 vrf_id_t vrf_id
= VRF_DEFAULT
;
589 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
590 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
591 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
592 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
594 zns
= zebra_ns_lookup(ns_id
);
597 if (h
->nlmsg_type
!= RTM_NEWLINK
)
600 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
604 /* We are interested in some AF_BRIDGE notifications. */
605 if (ifi
->ifi_family
== AF_BRIDGE
)
606 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
608 /* Looking up interface name. */
609 memset(tb
, 0, sizeof tb
);
610 memset(linkinfo
, 0, sizeof linkinfo
);
611 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
614 /* check for wireless messages to ignore */
615 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
616 if (IS_ZEBRA_DEBUG_KERNEL
)
617 zlog_debug("%s: ignoring IFLA_WIRELESS message",
621 #endif /* IFLA_WIRELESS */
623 if (tb
[IFLA_IFNAME
] == NULL
)
625 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
627 if (tb
[IFLA_LINKINFO
]) {
628 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
630 if (linkinfo
[IFLA_INFO_KIND
])
631 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
633 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
634 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
635 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
638 netlink_determine_zebra_iftype(kind
, &zif_type
);
641 /* If VRF, create the VRF structure itself. */
642 if (zif_type
== ZEBRA_IF_VRF
) {
643 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
644 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
647 if (tb
[IFLA_MASTER
]) {
648 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
649 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
650 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
651 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
652 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
654 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
656 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
659 /* If linking to another interface, note it. */
661 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
664 ifp
= if_get_by_name(name
, vrf_id
);
665 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
666 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
667 if (IS_ZEBRA_IF_VRF(ifp
))
668 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
669 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
671 ifp
->speed
= get_iflink_speed(name
);
672 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
674 /* Set zebra interface type */
675 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
678 zebra_if_update_link(ifp
, link_ifindex
);
680 /* Hardware type and address. */
681 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
682 netlink_interface_update_hw_addr(tb
, ifp
);
686 /* Extract and save L2 interface information, take additional actions.
688 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
689 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
690 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
695 /* Request for specific interface or address information from the kernel */
696 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
697 u_int32_t filter_mask
)
701 struct ifinfomsg ifm
;
705 /* Form the request, specifying filter (rtattr) if needed. */
706 memset(&req
, 0, sizeof(req
));
707 req
.n
.nlmsg_type
= type
;
708 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
709 req
.ifm
.ifi_family
= family
;
711 /* Include filter, if specified. */
713 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
715 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
718 /* Interface lookup by netlink socket. */
719 int interface_lookup_netlink(struct zebra_ns
*zns
)
723 /* Get interface information. */
724 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
727 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
732 /* Get interface information - for bridge interfaces. */
733 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
734 RTEXT_FILTER_BRVLAN
);
737 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
742 /* Get interface information - for bridge interfaces. */
743 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
744 RTEXT_FILTER_BRVLAN
);
747 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
752 /* Get IPv4 address of the interfaces. */
753 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
756 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
761 /* Get IPv6 address of the interfaces. */
762 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
765 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
773 int kernel_interface_set_master(struct interface
*master
,
774 struct interface
*slave
)
776 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
780 struct ifinfomsg ifa
;
781 char buf
[NL_PKT_BUF_SIZE
];
784 memset(&req
, 0, sizeof req
);
786 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
787 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
788 req
.n
.nlmsg_type
= RTM_SETLINK
;
789 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
791 req
.ifa
.ifi_index
= slave
->ifindex
;
793 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
794 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
796 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
800 /* Interface address modification. */
801 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
802 struct connected
*ifc
)
809 struct ifaddrmsg ifa
;
810 char buf
[NL_PKT_BUF_SIZE
];
813 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
816 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
818 bytelen
= (family
== AF_INET
? 4 : 16);
820 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
821 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
822 req
.n
.nlmsg_type
= cmd
;
823 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
825 req
.ifa
.ifa_family
= family
;
827 req
.ifa
.ifa_index
= ifp
->ifindex
;
828 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
830 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
832 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
) {
833 if (!CONNECTED_PEER(ifc
) && ifc
->destination
) {
834 p
= ifc
->destination
;
835 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
836 &p
->u
.prefix
, bytelen
);
840 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
841 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
844 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
845 strlen(ifc
->label
) + 1);
847 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
851 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
853 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
856 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
858 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
861 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
862 ns_id_t ns_id
, int startup
)
865 struct ifaddrmsg
*ifa
;
866 struct rtattr
*tb
[IFA_MAX
+ 1];
867 struct interface
*ifp
;
872 struct zebra_ns
*zns
;
874 zns
= zebra_ns_lookup(ns_id
);
877 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
880 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
883 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
887 memset(tb
, 0, sizeof tb
);
888 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
890 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
893 "netlink_interface_addr can't find interface by index %d",
898 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
901 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
902 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
905 zlog_debug(" IFA_LOCAL %s/%d",
906 inet_ntop(ifa
->ifa_family
,
907 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
911 zlog_debug(" IFA_ADDRESS %s/%d",
912 inet_ntop(ifa
->ifa_family
,
913 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
916 if (tb
[IFA_BROADCAST
])
917 zlog_debug(" IFA_BROADCAST %s/%d",
918 inet_ntop(ifa
->ifa_family
,
919 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
922 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
923 zlog_debug(" IFA_LABEL %s",
924 (char *)RTA_DATA(tb
[IFA_LABEL
]));
926 if (tb
[IFA_CACHEINFO
]) {
927 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
928 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
929 ci
->ifa_prefered
, ci
->ifa_valid
);
933 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
934 if (tb
[IFA_LOCAL
] == NULL
)
935 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
936 if (tb
[IFA_ADDRESS
] == NULL
)
937 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
939 /* local interface address */
940 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
942 /* is there a peer address? */
944 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
945 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
946 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
947 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
949 /* seeking a broadcast address */
950 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
953 /* addr is primary key, SOL if we don't have one */
955 zlog_debug("%s: NULL address", __func__
);
960 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
961 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
965 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
967 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
970 /* Register interface address to the interface. */
971 if (ifa
->ifa_family
== AF_INET
) {
972 if (h
->nlmsg_type
== RTM_NEWADDR
)
973 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
975 (struct in_addr
*)broad
, label
);
977 connected_delete_ipv4(
978 ifp
, flags
, (struct in_addr
*)addr
,
979 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
981 if (ifa
->ifa_family
== AF_INET6
) {
982 if (h
->nlmsg_type
== RTM_NEWADDR
) {
983 /* Only consider valid addresses; we'll not get a
985 * the kernel till IPv6 DAD has completed, but at init
987 * does query for and will receive all addresses.
990 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
992 ifp
, flags
, (struct in6_addr
*)addr
,
994 (struct in6_addr
*)broad
, label
);
996 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
998 (struct in6_addr
*)broad
);
1004 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1005 ns_id_t ns_id
, int startup
)
1008 struct ifinfomsg
*ifi
;
1009 struct rtattr
*tb
[IFLA_MAX
+ 1];
1010 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1011 struct interface
*ifp
;
1014 char *slave_kind
= NULL
;
1015 struct zebra_ns
*zns
;
1016 vrf_id_t vrf_id
= VRF_DEFAULT
;
1017 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1018 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1019 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1020 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1023 zns
= zebra_ns_lookup(ns_id
);
1024 ifi
= NLMSG_DATA(h
);
1026 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1027 /* If this is not link add/delete message so print warning. */
1028 zlog_warn("netlink_link_change: wrong kernel message %d",
1033 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1037 /* We are interested in some AF_BRIDGE notifications. */
1038 if (ifi
->ifi_family
== AF_BRIDGE
)
1039 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1041 /* Looking up interface name. */
1042 memset(tb
, 0, sizeof tb
);
1043 memset(linkinfo
, 0, sizeof linkinfo
);
1044 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1046 #ifdef IFLA_WIRELESS
1047 /* check for wireless messages to ignore */
1048 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1049 if (IS_ZEBRA_DEBUG_KERNEL
)
1050 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1054 #endif /* IFLA_WIRELESS */
1056 if (tb
[IFLA_IFNAME
] == NULL
)
1058 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1060 if (tb
[IFLA_LINKINFO
]) {
1061 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1063 if (linkinfo
[IFLA_INFO_KIND
])
1064 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1066 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1067 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1068 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1071 netlink_determine_zebra_iftype(kind
, &zif_type
);
1074 /* If linking to another interface, note it. */
1076 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1078 /* If VRF, create or update the VRF structure itself. */
1079 if (zif_type
== ZEBRA_IF_VRF
) {
1080 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1081 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1084 /* See if interface is present. */
1085 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1087 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1088 if (tb
[IFLA_MASTER
]) {
1089 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
1090 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1092 *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1093 } else if (slave_kind
1094 && (strcmp(slave_kind
, "bridge") == 0)) {
1095 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1097 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1099 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1103 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1104 /* Add interface notification from kernel */
1105 if (IS_ZEBRA_DEBUG_KERNEL
)
1107 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1108 "sl_type %d master %u flags 0x%x",
1109 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1110 zif_slave_type
, bridge_ifindex
,
1114 /* unknown interface */
1115 ifp
= if_get_by_name(name
, vrf_id
);
1117 /* pre-configured interface, learnt now */
1118 if (ifp
->vrf_id
!= vrf_id
)
1119 if_update_to_new_vrf(ifp
, vrf_id
);
1122 /* Update interface information. */
1123 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1124 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1125 if (IS_ZEBRA_IF_VRF(ifp
))
1126 SET_FLAG(ifp
->status
,
1127 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1128 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1130 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1132 /* Set interface type */
1133 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1136 zebra_if_update_link(ifp
, link_ifindex
);
1138 netlink_interface_update_hw_addr(tb
, ifp
);
1140 /* Inform clients, install any configured addresses. */
1143 /* Extract and save L2 interface information, take
1144 * additional actions. */
1145 netlink_interface_update_l2info(
1146 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1147 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1148 zebra_l2if_update_bridge_slave(ifp
,
1150 } else if (ifp
->vrf_id
!= vrf_id
) {
1151 /* VRF change for an interface. */
1152 if (IS_ZEBRA_DEBUG_KERNEL
)
1154 "RTM_NEWLINK vrf-change for %s(%u) "
1155 "vrf_id %u -> %u flags 0x%x",
1156 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1159 if_handle_vrf_change(ifp
, vrf_id
);
1161 int was_bridge_slave
;
1163 /* Interface update. */
1164 if (IS_ZEBRA_DEBUG_KERNEL
)
1166 "RTM_NEWLINK update for %s(%u) "
1167 "sl_type %d master %u flags 0x%x",
1168 name
, ifp
->ifindex
, zif_slave_type
,
1169 bridge_ifindex
, ifi
->ifi_flags
);
1171 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1172 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1175 /* Update interface type - NOTE: Only slave_type can
1177 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1178 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1180 netlink_interface_update_hw_addr(tb
, ifp
);
1182 if (if_is_no_ptm_operative(ifp
)) {
1183 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1184 if (!if_is_no_ptm_operative(ifp
)) {
1185 if (IS_ZEBRA_DEBUG_KERNEL
)
1187 "Intf %s(%u) has gone DOWN",
1188 name
, ifp
->ifindex
);
1190 } else if (if_is_operative(ifp
)) {
1191 /* Must notify client daemons of new
1192 * interface status. */
1193 if (IS_ZEBRA_DEBUG_KERNEL
)
1195 "Intf %s(%u) PTM up, notifying clients",
1196 name
, ifp
->ifindex
);
1197 zebra_interface_up_update(ifp
);
1200 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1201 if (if_is_operative(ifp
)) {
1202 if (IS_ZEBRA_DEBUG_KERNEL
)
1204 "Intf %s(%u) has come UP",
1205 name
, ifp
->ifindex
);
1210 /* Extract and save L2 interface information, take
1211 * additional actions. */
1212 netlink_interface_update_l2info(
1213 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1214 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1215 zebra_l2if_update_bridge_slave(ifp
,
1219 /* Delete interface notification from kernel */
1221 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1222 name
, ifi
->ifi_index
);
1226 if (IS_ZEBRA_DEBUG_KERNEL
)
1227 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1230 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1232 /* Special handling for bridge or VxLAN interfaces. */
1233 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1234 zebra_l2_bridge_del(ifp
);
1235 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1236 zebra_l2_vxlanif_del(ifp
);
1238 if (!IS_ZEBRA_IF_VRF(ifp
))
1239 if_delete_update(ifp
);
1245 /* Interface information read by netlink. */
1246 void interface_list(struct zebra_ns
*zns
)
1248 interface_lookup_netlink(zns
);
1251 #endif /* GNU_LINUX */