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
24 /* The following definition is to workaround an issue in the Linux kernel
25 * header files with redefinition of 'struct in6_addr' in both
26 * netinet/in.h and linux/in6.h.
27 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
31 #include <linux/if_bridge.h>
32 #include <net/if_arp.h>
33 #include <linux/sockios.h>
34 #include <linux/ethtool.h>
40 #include "connected.h"
43 #include "zebra_memory.h"
53 #include "zebra/zserv.h"
54 #include "zebra/zebra_ns.h"
55 #include "zebra/zebra_vrf.h"
57 #include "zebra/redistribute.h"
58 #include "zebra/interface.h"
59 #include "zebra/debug.h"
60 #include "zebra/rtadv.h"
61 #include "zebra/zebra_ptm.h"
62 #include "zebra/zebra_mpls.h"
63 #include "zebra/kernel_netlink.h"
64 #include "zebra/if_netlink.h"
67 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
68 names and ifindex values. */
69 static void set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
,
72 struct interface
*oifp
;
74 if (((oifp
= if_lookup_by_index_per_ns(zns
, ifi_index
)) != NULL
)
76 if (ifi_index
== IFINDEX_INTERNAL
)
78 "Netlink is setting interface %s ifindex to reserved "
80 ifp
->name
, ifi_index
);
82 if (IS_ZEBRA_DEBUG_KERNEL
)
84 "interface index %d was renamed from %s to %s",
85 ifi_index
, oifp
->name
, ifp
->name
);
88 "interface rename detected on up interface: index %d "
89 "was renamed from %s to %s, results are uncertain!",
90 ifi_index
, oifp
->name
, ifp
->name
);
91 if_delete_update(oifp
);
94 ifp
->ifindex
= ifi_index
;
97 /* Utility function to parse hardware link-layer address and update ifp */
98 static void netlink_interface_update_hw_addr(struct rtattr
**tb
,
99 struct interface
*ifp
)
103 if (tb
[IFLA_ADDRESS
]) {
106 hw_addr_len
= RTA_PAYLOAD(tb
[IFLA_ADDRESS
]);
108 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
109 zlog_warn("Hardware address is too large: %d",
112 ifp
->hw_addr_len
= hw_addr_len
;
113 memcpy(ifp
->hw_addr
, RTA_DATA(tb
[IFLA_ADDRESS
]),
116 for (i
= 0; i
< hw_addr_len
; i
++)
117 if (ifp
->hw_addr
[i
] != 0)
120 if (i
== hw_addr_len
)
121 ifp
->hw_addr_len
= 0;
123 ifp
->hw_addr_len
= hw_addr_len
;
128 static enum zebra_link_type
netlink_to_zebra_link_type(unsigned int hwt
)
132 return ZEBRA_LLT_ETHER
;
134 return ZEBRA_LLT_EETHER
;
136 return ZEBRA_LLT_AX25
;
138 return ZEBRA_LLT_PRONET
;
140 return ZEBRA_LLT_IEEE802
;
142 return ZEBRA_LLT_ARCNET
;
143 case ARPHRD_APPLETLK
:
144 return ZEBRA_LLT_APPLETLK
;
146 return ZEBRA_LLT_DLCI
;
148 return ZEBRA_LLT_ATM
;
149 case ARPHRD_METRICOM
:
150 return ZEBRA_LLT_METRICOM
;
151 case ARPHRD_IEEE1394
:
152 return ZEBRA_LLT_IEEE1394
;
154 return ZEBRA_LLT_EUI64
;
155 case ARPHRD_INFINIBAND
:
156 return ZEBRA_LLT_INFINIBAND
;
158 return ZEBRA_LLT_SLIP
;
160 return ZEBRA_LLT_CSLIP
;
162 return ZEBRA_LLT_SLIP6
;
164 return ZEBRA_LLT_CSLIP6
;
166 return ZEBRA_LLT_RSRVD
;
168 return ZEBRA_LLT_ADAPT
;
170 return ZEBRA_LLT_ROSE
;
172 return ZEBRA_LLT_X25
;
174 return ZEBRA_LLT_PPP
;
176 return ZEBRA_LLT_CHDLC
;
178 return ZEBRA_LLT_LAPB
;
180 return ZEBRA_LLT_RAWHDLC
;
182 return ZEBRA_LLT_IPIP
;
184 return ZEBRA_LLT_IPIP6
;
186 return ZEBRA_LLT_FRAD
;
188 return ZEBRA_LLT_SKIP
;
189 case ARPHRD_LOOPBACK
:
190 return ZEBRA_LLT_LOOPBACK
;
191 case ARPHRD_LOCALTLK
:
192 return ZEBRA_LLT_LOCALTLK
;
194 return ZEBRA_LLT_FDDI
;
196 return ZEBRA_LLT_SIT
;
198 return ZEBRA_LLT_IPDDP
;
200 return ZEBRA_LLT_IPGRE
;
202 return ZEBRA_LLT_PIMREG
;
204 return ZEBRA_LLT_HIPPI
;
206 return ZEBRA_LLT_ECONET
;
208 return ZEBRA_LLT_IRDA
;
210 return ZEBRA_LLT_FCPP
;
212 return ZEBRA_LLT_FCAL
;
214 return ZEBRA_LLT_FCPL
;
215 case ARPHRD_FCFABRIC
:
216 return ZEBRA_LLT_FCFABRIC
;
217 case ARPHRD_IEEE802_TR
:
218 return ZEBRA_LLT_IEEE802_TR
;
219 case ARPHRD_IEEE80211
:
220 return ZEBRA_LLT_IEEE80211
;
221 case ARPHRD_IEEE802154
:
222 return ZEBRA_LLT_IEEE802154
;
225 return ZEBRA_LLT_IP6GRE
;
227 #ifdef ARPHRD_IEEE802154_PHY
228 case ARPHRD_IEEE802154_PHY
:
229 return ZEBRA_LLT_IEEE802154_PHY
;
233 return ZEBRA_LLT_UNKNOWN
;
237 static void netlink_determine_zebra_iftype(char *kind
, zebra_iftype_t
*zif_type
)
239 *zif_type
= ZEBRA_IF_OTHER
;
244 if (strcmp(kind
, "vrf") == 0)
245 *zif_type
= ZEBRA_IF_VRF
;
246 else if (strcmp(kind
, "bridge") == 0)
247 *zif_type
= ZEBRA_IF_BRIDGE
;
248 else if (strcmp(kind
, "vlan") == 0)
249 *zif_type
= ZEBRA_IF_VLAN
;
250 else if (strcmp(kind
, "vxlan") == 0)
251 *zif_type
= ZEBRA_IF_VXLAN
;
254 // Temporary Assignments to compile on older platforms.
256 #define IFLA_BR_MAX 39
259 #ifndef IFLA_VXLAN_ID
260 #define IFLA_VXLAN_ID 1
263 #ifndef IFLA_VXLAN_LOCAL
264 #define IFLA_VXLAN_LOCAL 4
267 #ifndef IFLA_VXLAN_MAX
268 #define IFLA_VXLAN_MAX 26
271 #ifndef IFLA_BRIDGE_MAX
272 #define IFLA_BRIDGE_MAX 2
275 #ifndef IFLA_BRIDGE_VLAN_INFO
276 #define IFLA_BRIDGE_VLAN_INFO 2
279 #ifndef BRIDGE_VLAN_INFO_PVID
280 #define BRIDGE_VLAN_INFO_PVID (1<<1)
283 #ifndef RTEXT_FILTER_BRVLAN
284 #define RTEXT_FILTER_BRVLAN (1<<1)
288 #define NTF_SELF 0x02
291 #ifndef IFLA_BR_VLAN_FILTERING
292 #define IFLA_BR_VLAN_FILTERING 7
295 #define parse_rtattr_nested(tb, max, rta) \
296 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
298 static void netlink_vrf_change(struct nlmsghdr
*h
, struct rtattr
*tb
,
301 struct ifinfomsg
*ifi
;
302 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+ 1];
303 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
305 struct zebra_vrf
*zvrf
;
306 u_int32_t nl_table_id
;
310 memset(linkinfo
, 0, sizeof linkinfo
);
311 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
313 if (!linkinfo
[IFLA_INFO_DATA
]) {
314 if (IS_ZEBRA_DEBUG_KERNEL
)
316 "%s: IFLA_INFO_DATA missing from VRF message: %s",
321 memset(attr
, 0, sizeof attr
);
322 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
323 if (!attr
[IFLA_VRF_TABLE
]) {
324 if (IS_ZEBRA_DEBUG_KERNEL
)
326 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
331 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
333 if (h
->nlmsg_type
== RTM_NEWLINK
) {
334 if (IS_ZEBRA_DEBUG_KERNEL
)
335 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name
,
336 ifi
->ifi_index
, nl_table_id
);
339 * vrf_get is implied creation if it does not exist
341 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
,
342 name
); // It would create vrf
344 zlog_err("VRF %s id %u not created", name
,
349 /* Enable the created VRF. */
350 if (!vrf_enable(vrf
)) {
351 zlog_err("Failed to enable VRF %s id %u", name
,
357 * This is the only place that we get the actual kernel table_id
358 * being used. We need it to set the table_id of the routes
359 * we are passing to the kernel.... And to throw some totally
360 * awesome parties. that too.
362 zvrf
= (struct zebra_vrf
*)vrf
->info
;
363 zvrf
->table_id
= nl_table_id
;
364 } else // h->nlmsg_type == RTM_DELLINK
366 if (IS_ZEBRA_DEBUG_KERNEL
)
367 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
370 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
373 zlog_warn("%s: vrf not found", __func__
);
381 static int get_iflink_speed(const char *ifname
)
384 struct ethtool_cmd ecmd
;
388 /* initialize struct */
389 memset(&ifdata
, 0, sizeof(ifdata
));
391 /* set interface name */
392 strcpy(ifdata
.ifr_name
, ifname
);
394 /* initialize ethtool interface */
395 memset(&ecmd
, 0, sizeof(ecmd
));
396 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
397 ifdata
.ifr_data
= (__caddr_t
)&ecmd
;
399 /* use ioctl to get IP address of an interface */
400 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
402 zlog_debug("Failure to read interface %s speed: %d %s", ifname
,
403 errno
, safe_strerror(errno
));
407 /* Get the current link state for the interface */
408 rc
= ioctl(sd
, SIOCETHTOOL
, (char *)&ifdata
);
410 zlog_debug("IOCTL failure to read interface %s speed: %d %s",
411 ifname
, errno
, safe_strerror(errno
));
418 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
421 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
422 struct zebra_l2info_bridge
*bridge_info
)
424 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
426 memset(bridge_info
, 0, sizeof(*bridge_info
));
427 memset(attr
, 0, sizeof attr
);
428 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
429 if (attr
[IFLA_BR_VLAN_FILTERING
])
430 bridge_info
->vlan_aware
=
431 *(u_char
*)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
435 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
436 struct zebra_l2info_vlan
*vlan_info
)
438 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
441 memset(vlan_info
, 0, sizeof(*vlan_info
));
442 memset(attr
, 0, sizeof attr
);
443 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
444 if (!attr
[IFLA_VLAN_ID
]) {
445 if (IS_ZEBRA_DEBUG_KERNEL
)
446 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
450 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
451 vlan_info
->vid
= vid_in_msg
;
455 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
456 struct zebra_l2info_vxlan
*vxl_info
)
458 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
460 struct in_addr vtep_ip_in_msg
;
462 memset(vxl_info
, 0, sizeof(*vxl_info
));
463 memset(attr
, 0, sizeof attr
);
464 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
465 if (!attr
[IFLA_VXLAN_ID
]) {
466 if (IS_ZEBRA_DEBUG_KERNEL
)
468 "IFLA_VXLAN_ID missing from VXLAN IF message");
472 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
473 vxl_info
->vni
= vni_in_msg
;
474 if (!attr
[IFLA_VXLAN_LOCAL
]) {
475 if (IS_ZEBRA_DEBUG_KERNEL
)
477 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
480 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
481 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
488 * Extract and save L2 params (of interest) for an interface. When a
489 * bridge interface is added or updated, take further actions to map
490 * its members. Likewise, for VxLAN interface.
492 static void netlink_interface_update_l2info(struct interface
*ifp
,
493 struct rtattr
*link_data
, int add
)
498 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
499 struct zebra_l2info_bridge bridge_info
;
501 netlink_extract_bridge_info(link_data
, &bridge_info
);
502 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
503 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
504 struct zebra_l2info_vlan vlan_info
;
506 netlink_extract_vlan_info(link_data
, &vlan_info
);
507 zebra_l2_vlanif_update(ifp
, &vlan_info
);
508 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
509 struct zebra_l2info_vxlan vxlan_info
;
511 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
512 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
516 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
520 struct ifinfomsg
*ifi
;
521 struct rtattr
*tb
[IFLA_MAX
+ 1];
522 struct interface
*ifp
;
523 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
528 vlanid_t access_vlan
;
530 /* Fetch name and ifindex */
532 memset(tb
, 0, sizeof tb
);
533 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
535 if (tb
[IFLA_IFNAME
] == NULL
)
537 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
539 /* The interface should already be known, if not discard. */
540 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
542 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
545 if (!IS_ZEBRA_IF_VXLAN(ifp
))
548 /* We are only interested in the access VLAN i.e., AF_SPEC */
549 if (!tb
[IFLA_AF_SPEC
])
552 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
553 * only 1 access VLAN is accepted.
555 memset(aftb
, 0, sizeof aftb
);
556 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
557 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
560 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
561 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
564 access_vlan
= (vlanid_t
)vinfo
->vid
;
565 if (IS_ZEBRA_DEBUG_KERNEL
)
566 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
567 name
, ifi
->ifi_index
);
568 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
572 /* Called from interface_lookup_netlink(). This function is only used
574 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
575 ns_id_t ns_id
, int startup
)
578 struct ifinfomsg
*ifi
;
579 struct rtattr
*tb
[IFLA_MAX
+ 1];
580 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
581 struct interface
*ifp
;
584 char *slave_kind
= NULL
;
585 struct zebra_ns
*zns
;
586 vrf_id_t vrf_id
= VRF_DEFAULT
;
587 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
588 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
589 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
590 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
592 zns
= zebra_ns_lookup(ns_id
);
595 if (h
->nlmsg_type
!= RTM_NEWLINK
)
598 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
602 /* We are interested in some AF_BRIDGE notifications. */
603 if (ifi
->ifi_family
== AF_BRIDGE
)
604 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
606 /* Looking up interface name. */
607 memset(tb
, 0, sizeof tb
);
608 memset(linkinfo
, 0, sizeof linkinfo
);
609 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
612 /* check for wireless messages to ignore */
613 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
614 if (IS_ZEBRA_DEBUG_KERNEL
)
615 zlog_debug("%s: ignoring IFLA_WIRELESS message",
619 #endif /* IFLA_WIRELESS */
621 if (tb
[IFLA_IFNAME
] == NULL
)
623 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
625 if (tb
[IFLA_LINKINFO
]) {
626 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
628 if (linkinfo
[IFLA_INFO_KIND
])
629 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
631 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
632 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
633 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
636 netlink_determine_zebra_iftype(kind
, &zif_type
);
639 /* If VRF, create the VRF structure itself. */
640 if (zif_type
== ZEBRA_IF_VRF
) {
641 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
642 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
645 if (tb
[IFLA_MASTER
]) {
646 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
647 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
648 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
649 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
650 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
652 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
654 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
657 /* If linking to another interface, note it. */
659 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
662 ifp
= if_get_by_name(name
, vrf_id
);
663 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
664 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
665 if (IS_ZEBRA_IF_VRF(ifp
))
666 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
667 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
669 ifp
->speed
= get_iflink_speed(name
);
670 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
672 /* Set zebra interface type */
673 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
676 zebra_if_update_link(ifp
, link_ifindex
);
678 /* Hardware type and address. */
679 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
680 netlink_interface_update_hw_addr(tb
, ifp
);
684 /* Extract and save L2 interface information, take additional actions.
686 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
687 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
688 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
693 /* Request for specific interface or address information from the kernel */
694 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
695 u_int32_t filter_mask
)
699 struct ifinfomsg ifm
;
703 /* Form the request, specifying filter (rtattr) if needed. */
704 memset(&req
, 0, sizeof(req
));
705 req
.n
.nlmsg_type
= type
;
706 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
707 req
.ifm
.ifi_family
= family
;
709 /* Include filter, if specified. */
711 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
713 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
716 /* Interface lookup by netlink socket. */
717 int interface_lookup_netlink(struct zebra_ns
*zns
)
721 /* Get interface information. */
722 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
725 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
730 /* Get interface information - for bridge interfaces. */
731 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
732 RTEXT_FILTER_BRVLAN
);
735 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
740 /* Get interface information - for bridge interfaces. */
741 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
742 RTEXT_FILTER_BRVLAN
);
745 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
750 /* Get IPv4 address of the interfaces. */
751 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
754 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
759 /* Get IPv6 address of the interfaces. */
760 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
763 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
771 /* Interface address modification. */
772 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
773 struct connected
*ifc
)
780 struct ifaddrmsg ifa
;
781 char buf
[NL_PKT_BUF_SIZE
];
784 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
787 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
789 bytelen
= (family
== AF_INET
? 4 : 16);
791 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
792 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
793 req
.n
.nlmsg_type
= cmd
;
794 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
796 req
.ifa
.ifa_family
= family
;
798 req
.ifa
.ifa_index
= ifp
->ifindex
;
799 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
801 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
803 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
) {
804 if (!CONNECTED_PEER(ifc
) && ifc
->destination
) {
805 p
= ifc
->destination
;
806 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
807 &p
->u
.prefix
, bytelen
);
811 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
812 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
815 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
816 strlen(ifc
->label
) + 1);
818 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
822 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
824 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
827 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
829 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
832 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
833 ns_id_t ns_id
, int startup
)
836 struct ifaddrmsg
*ifa
;
837 struct rtattr
*tb
[IFA_MAX
+ 1];
838 struct interface
*ifp
;
843 struct zebra_ns
*zns
;
845 zns
= zebra_ns_lookup(ns_id
);
848 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
851 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
854 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
858 memset(tb
, 0, sizeof tb
);
859 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
861 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
864 "netlink_interface_addr can't find interface by index %d",
869 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
872 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
873 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
876 zlog_debug(" IFA_LOCAL %s/%d",
877 inet_ntop(ifa
->ifa_family
,
878 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
882 zlog_debug(" IFA_ADDRESS %s/%d",
883 inet_ntop(ifa
->ifa_family
,
884 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
887 if (tb
[IFA_BROADCAST
])
888 zlog_debug(" IFA_BROADCAST %s/%d",
889 inet_ntop(ifa
->ifa_family
,
890 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
893 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
894 zlog_debug(" IFA_LABEL %s",
895 (char *)RTA_DATA(tb
[IFA_LABEL
]));
897 if (tb
[IFA_CACHEINFO
]) {
898 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
899 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
900 ci
->ifa_prefered
, ci
->ifa_valid
);
904 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
905 if (tb
[IFA_LOCAL
] == NULL
)
906 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
907 if (tb
[IFA_ADDRESS
] == NULL
)
908 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
910 /* local interface address */
911 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
913 /* is there a peer address? */
915 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
916 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
917 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
918 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
920 /* seeking a broadcast address */
921 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
924 /* addr is primary key, SOL if we don't have one */
926 zlog_debug("%s: NULL address", __func__
);
931 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
932 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
936 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
938 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
941 /* Register interface address to the interface. */
942 if (ifa
->ifa_family
== AF_INET
) {
943 if (h
->nlmsg_type
== RTM_NEWADDR
)
944 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
946 (struct in_addr
*)broad
, label
);
948 connected_delete_ipv4(
949 ifp
, flags
, (struct in_addr
*)addr
,
950 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
952 if (ifa
->ifa_family
== AF_INET6
) {
953 if (h
->nlmsg_type
== RTM_NEWADDR
) {
954 /* Only consider valid addresses; we'll not get a
956 * the kernel till IPv6 DAD has completed, but at init
958 * does query for and will receive all addresses.
961 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
963 ifp
, flags
, (struct in6_addr
*)addr
,
965 (struct in6_addr
*)broad
, label
);
967 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
969 (struct in6_addr
*)broad
);
975 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
976 ns_id_t ns_id
, int startup
)
979 struct ifinfomsg
*ifi
;
980 struct rtattr
*tb
[IFLA_MAX
+ 1];
981 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
982 struct interface
*ifp
;
985 char *slave_kind
= NULL
;
986 struct zebra_ns
*zns
;
987 vrf_id_t vrf_id
= VRF_DEFAULT
;
988 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
989 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
990 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
991 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
994 zns
= zebra_ns_lookup(ns_id
);
997 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
998 /* If this is not link add/delete message so print warning. */
999 zlog_warn("netlink_link_change: wrong kernel message %d",
1004 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1008 /* We are interested in some AF_BRIDGE notifications. */
1009 if (ifi
->ifi_family
== AF_BRIDGE
)
1010 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1012 /* Looking up interface name. */
1013 memset(tb
, 0, sizeof tb
);
1014 memset(linkinfo
, 0, sizeof linkinfo
);
1015 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1017 #ifdef IFLA_WIRELESS
1018 /* check for wireless messages to ignore */
1019 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1020 if (IS_ZEBRA_DEBUG_KERNEL
)
1021 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1025 #endif /* IFLA_WIRELESS */
1027 if (tb
[IFLA_IFNAME
] == NULL
)
1029 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1031 if (tb
[IFLA_LINKINFO
]) {
1032 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1034 if (linkinfo
[IFLA_INFO_KIND
])
1035 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1037 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1038 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1039 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1042 netlink_determine_zebra_iftype(kind
, &zif_type
);
1045 /* If linking to another interface, note it. */
1047 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1049 /* If VRF, create or update the VRF structure itself. */
1050 if (zif_type
== ZEBRA_IF_VRF
) {
1051 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1052 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1055 /* See if interface is present. */
1056 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1058 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1059 if (tb
[IFLA_MASTER
]) {
1060 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)) {
1061 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1063 *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1064 } else if (slave_kind
1065 && (strcmp(slave_kind
, "bridge") == 0)) {
1066 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1068 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1070 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1074 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1075 /* Add interface notification from kernel */
1076 if (IS_ZEBRA_DEBUG_KERNEL
)
1078 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1079 "sl_type %d master %u flags 0x%x",
1080 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1081 zif_slave_type
, bridge_ifindex
,
1085 /* unknown interface */
1086 ifp
= if_get_by_name(name
, vrf_id
);
1088 /* pre-configured interface, learnt now */
1089 if (ifp
->vrf_id
!= vrf_id
)
1090 if_update_to_new_vrf(ifp
, vrf_id
);
1093 /* Update interface information. */
1094 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1095 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1096 if (IS_ZEBRA_IF_VRF(ifp
))
1097 SET_FLAG(ifp
->status
,
1098 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1099 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1101 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1103 /* Set interface type */
1104 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1107 zebra_if_update_link(ifp
, link_ifindex
);
1109 netlink_interface_update_hw_addr(tb
, ifp
);
1111 /* Inform clients, install any configured addresses. */
1114 /* Extract and save L2 interface information, take
1115 * additional actions. */
1116 netlink_interface_update_l2info(
1117 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1118 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1119 zebra_l2if_update_bridge_slave(ifp
,
1121 } else if (ifp
->vrf_id
!= vrf_id
) {
1122 /* VRF change for an interface. */
1123 if (IS_ZEBRA_DEBUG_KERNEL
)
1125 "RTM_NEWLINK vrf-change for %s(%u) "
1126 "vrf_id %u -> %u flags 0x%x",
1127 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1130 if_handle_vrf_change(ifp
, vrf_id
);
1132 int was_bridge_slave
;
1134 /* Interface update. */
1135 if (IS_ZEBRA_DEBUG_KERNEL
)
1137 "RTM_NEWLINK update for %s(%u) "
1138 "sl_type %d master %u flags 0x%x",
1139 name
, ifp
->ifindex
, zif_slave_type
,
1140 bridge_ifindex
, ifi
->ifi_flags
);
1142 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1143 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1146 /* Update interface type - NOTE: Only slave_type can
1148 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1149 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1151 netlink_interface_update_hw_addr(tb
, ifp
);
1153 if (if_is_no_ptm_operative(ifp
)) {
1154 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1155 if (!if_is_no_ptm_operative(ifp
)) {
1156 if (IS_ZEBRA_DEBUG_KERNEL
)
1158 "Intf %s(%u) has gone DOWN",
1159 name
, ifp
->ifindex
);
1161 } else if (if_is_operative(ifp
)) {
1162 /* Must notify client daemons of new
1163 * interface status. */
1164 if (IS_ZEBRA_DEBUG_KERNEL
)
1166 "Intf %s(%u) PTM up, notifying clients",
1167 name
, ifp
->ifindex
);
1168 zebra_interface_up_update(ifp
);
1171 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1172 if (if_is_operative(ifp
)) {
1173 if (IS_ZEBRA_DEBUG_KERNEL
)
1175 "Intf %s(%u) has come UP",
1176 name
, ifp
->ifindex
);
1181 /* Extract and save L2 interface information, take
1182 * additional actions. */
1183 netlink_interface_update_l2info(
1184 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1185 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1186 zebra_l2if_update_bridge_slave(ifp
,
1190 /* Delete interface notification from kernel */
1192 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1193 name
, ifi
->ifi_index
);
1197 if (IS_ZEBRA_DEBUG_KERNEL
)
1198 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1201 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1203 /* Special handling for bridge or VxLAN interfaces. */
1204 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1205 zebra_l2_bridge_del(ifp
);
1206 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1207 zebra_l2_vxlanif_del(ifp
);
1209 if (!IS_ZEBRA_IF_VRF(ifp
))
1210 if_delete_update(ifp
);
1216 /* Interface information read by netlink. */
1217 void interface_list(struct zebra_ns
*zns
)
1219 interface_lookup_netlink(zns
);