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 u_int32_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
= *(u_int32_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
,
317 /* Enable the created VRF. */
318 if (!vrf_enable(vrf
)) {
319 zlog_err("Failed to enable VRF %s id %u", name
,
325 * This is the only place that we get the actual kernel table_id
326 * being used. We need it to set the table_id of the routes
327 * we are passing to the kernel.... And to throw some totally
328 * awesome parties. that too.
330 zvrf
= (struct zebra_vrf
*)vrf
->info
;
331 zvrf
->table_id
= nl_table_id
;
332 } else // h->nlmsg_type == RTM_DELLINK
334 if (IS_ZEBRA_DEBUG_KERNEL
)
335 zlog_debug("RTM_DELLINK for VRF %s(%u)", name
,
338 vrf
= vrf_lookup_by_id((vrf_id_t
)ifi
->ifi_index
);
341 zlog_warn("%s: vrf not found", __func__
);
349 static int get_iflink_speed(struct interface
*interface
)
352 struct ethtool_cmd ecmd
;
355 const char *ifname
= interface
->name
;
357 /* initialize struct */
358 memset(&ifdata
, 0, sizeof(ifdata
));
360 /* set interface name */
361 strlcpy(ifdata
.ifr_name
, ifname
, sizeof(ifdata
.ifr_name
));
363 /* initialize ethtool interface */
364 memset(&ecmd
, 0, sizeof(ecmd
));
365 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
366 ifdata
.ifr_data
= (caddr_t
)&ecmd
;
368 /* use ioctl to get IP address of an interface */
369 if (zserv_privs
.change(ZPRIVS_RAISE
))
370 zlog_err("Can't raise privileges");
371 sd
= vrf_socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
, interface
->vrf_id
,
374 if (IS_ZEBRA_DEBUG_KERNEL
)
375 zlog_debug("Failure to read interface %s speed: %d %s",
376 ifname
, errno
, safe_strerror(errno
));
379 /* Get the current link state for the interface */
380 rc
= vrf_ioctl(interface
->vrf_id
, sd
, SIOCETHTOOL
, (char *)&ifdata
);
381 if (zserv_privs
.change(ZPRIVS_LOWER
))
382 zlog_err("Can't lower privileges");
384 if (IS_ZEBRA_DEBUG_KERNEL
)
386 "IOCTL failure to read interface %s speed: %d %s",
387 ifname
, errno
, safe_strerror(errno
));
394 return (ecmd
.speed_hi
<< 16) | ecmd
.speed
;
397 uint32_t kernel_get_speed(struct interface
*ifp
)
399 return get_iflink_speed(ifp
);
402 static int netlink_extract_bridge_info(struct rtattr
*link_data
,
403 struct zebra_l2info_bridge
*bridge_info
)
405 struct rtattr
*attr
[IFLA_BR_MAX
+ 1];
407 memset(bridge_info
, 0, sizeof(*bridge_info
));
408 memset(attr
, 0, sizeof attr
);
409 parse_rtattr_nested(attr
, IFLA_BR_MAX
, link_data
);
410 if (attr
[IFLA_BR_VLAN_FILTERING
])
411 bridge_info
->vlan_aware
=
412 *(u_char
*)RTA_DATA(attr
[IFLA_BR_VLAN_FILTERING
]);
416 static int netlink_extract_vlan_info(struct rtattr
*link_data
,
417 struct zebra_l2info_vlan
*vlan_info
)
419 struct rtattr
*attr
[IFLA_VLAN_MAX
+ 1];
422 memset(vlan_info
, 0, sizeof(*vlan_info
));
423 memset(attr
, 0, sizeof attr
);
424 parse_rtattr_nested(attr
, IFLA_VLAN_MAX
, link_data
);
425 if (!attr
[IFLA_VLAN_ID
]) {
426 if (IS_ZEBRA_DEBUG_KERNEL
)
427 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
431 vid_in_msg
= *(vlanid_t
*)RTA_DATA(attr
[IFLA_VLAN_ID
]);
432 vlan_info
->vid
= vid_in_msg
;
436 static int netlink_extract_vxlan_info(struct rtattr
*link_data
,
437 struct zebra_l2info_vxlan
*vxl_info
)
439 struct rtattr
*attr
[IFLA_VXLAN_MAX
+ 1];
441 struct in_addr vtep_ip_in_msg
;
443 memset(vxl_info
, 0, sizeof(*vxl_info
));
444 memset(attr
, 0, sizeof attr
);
445 parse_rtattr_nested(attr
, IFLA_VXLAN_MAX
, link_data
);
446 if (!attr
[IFLA_VXLAN_ID
]) {
447 if (IS_ZEBRA_DEBUG_KERNEL
)
449 "IFLA_VXLAN_ID missing from VXLAN IF message");
453 vni_in_msg
= *(vni_t
*)RTA_DATA(attr
[IFLA_VXLAN_ID
]);
454 vxl_info
->vni
= vni_in_msg
;
455 if (!attr
[IFLA_VXLAN_LOCAL
]) {
456 if (IS_ZEBRA_DEBUG_KERNEL
)
458 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
461 *(struct in_addr
*)RTA_DATA(attr
[IFLA_VXLAN_LOCAL
]);
462 vxl_info
->vtep_ip
= vtep_ip_in_msg
;
469 * Extract and save L2 params (of interest) for an interface. When a
470 * bridge interface is added or updated, take further actions to map
471 * its members. Likewise, for VxLAN interface.
473 static void netlink_interface_update_l2info(struct interface
*ifp
,
474 struct rtattr
*link_data
, int add
)
479 if (IS_ZEBRA_IF_BRIDGE(ifp
)) {
480 struct zebra_l2info_bridge bridge_info
;
482 netlink_extract_bridge_info(link_data
, &bridge_info
);
483 zebra_l2_bridge_add_update(ifp
, &bridge_info
, add
);
484 } else if (IS_ZEBRA_IF_VLAN(ifp
)) {
485 struct zebra_l2info_vlan vlan_info
;
487 netlink_extract_vlan_info(link_data
, &vlan_info
);
488 zebra_l2_vlanif_update(ifp
, &vlan_info
);
489 } else if (IS_ZEBRA_IF_VXLAN(ifp
)) {
490 struct zebra_l2info_vxlan vxlan_info
;
492 netlink_extract_vxlan_info(link_data
, &vxlan_info
);
493 zebra_l2_vxlanif_add_update(ifp
, &vxlan_info
, add
);
497 static int netlink_bridge_interface(struct nlmsghdr
*h
, int len
, ns_id_t ns_id
,
501 struct ifinfomsg
*ifi
;
502 struct rtattr
*tb
[IFLA_MAX
+ 1];
503 struct interface
*ifp
;
504 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+ 1];
509 vlanid_t access_vlan
;
511 /* Fetch name and ifindex */
513 memset(tb
, 0, sizeof tb
);
514 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
516 if (tb
[IFLA_IFNAME
] == NULL
)
518 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
520 /* The interface should already be known, if not discard. */
521 ifp
= if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id
), ifi
->ifi_index
);
523 zlog_warn("Cannot find bridge IF %s(%u)", name
, ifi
->ifi_index
);
526 if (!IS_ZEBRA_IF_VXLAN(ifp
))
529 /* We are only interested in the access VLAN i.e., AF_SPEC */
530 if (!tb
[IFLA_AF_SPEC
])
533 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
534 * only 1 access VLAN is accepted.
536 memset(aftb
, 0, sizeof aftb
);
537 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, tb
[IFLA_AF_SPEC
]);
538 if (!aftb
[IFLA_BRIDGE_VLAN_INFO
])
541 vinfo
= RTA_DATA(aftb
[IFLA_BRIDGE_VLAN_INFO
]);
542 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
))
545 access_vlan
= (vlanid_t
)vinfo
->vid
;
546 if (IS_ZEBRA_DEBUG_KERNEL
)
547 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan
,
548 name
, ifi
->ifi_index
);
549 zebra_l2_vxlanif_update_access_vlan(ifp
, access_vlan
);
553 /* Called from interface_lookup_netlink(). This function is only used
555 static int netlink_interface(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
556 ns_id_t ns_id
, int startup
)
559 struct ifinfomsg
*ifi
;
560 struct rtattr
*tb
[IFLA_MAX
+ 1];
561 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
562 struct interface
*ifp
;
566 char *slave_kind
= NULL
;
567 struct zebra_ns
*zns
;
568 vrf_id_t vrf_id
= VRF_DEFAULT
;
569 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
570 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
571 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
572 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
574 zns
= zebra_ns_lookup(ns_id
);
577 if (h
->nlmsg_type
!= RTM_NEWLINK
)
580 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
584 /* We are interested in some AF_BRIDGE notifications. */
585 if (ifi
->ifi_family
== AF_BRIDGE
)
586 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
588 /* Looking up interface name. */
589 memset(tb
, 0, sizeof tb
);
590 memset(linkinfo
, 0, sizeof linkinfo
);
591 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
594 /* check for wireless messages to ignore */
595 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
596 if (IS_ZEBRA_DEBUG_KERNEL
)
597 zlog_debug("%s: ignoring IFLA_WIRELESS message",
601 #endif /* IFLA_WIRELESS */
603 if (tb
[IFLA_IFNAME
] == NULL
)
605 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
607 if (tb
[IFLA_IFALIAS
])
608 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
610 if (tb
[IFLA_LINKINFO
]) {
611 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
613 if (linkinfo
[IFLA_INFO_KIND
])
614 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
616 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
617 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
618 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
621 netlink_determine_zebra_iftype(kind
, &zif_type
);
624 /* If VRF, create the VRF structure itself. */
625 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
626 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
627 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
630 if (tb
[IFLA_MASTER
]) {
631 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
632 && !vrf_is_backend_netns()) {
633 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
634 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
635 } else if (slave_kind
&& (strcmp(slave_kind
, "bridge") == 0)) {
636 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
638 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
640 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
642 if (vrf_is_backend_netns())
643 vrf_id
= (vrf_id_t
)ns_id
;
645 /* If linking to another interface, note it. */
647 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
650 ifp
= if_get_by_name(name
, vrf_id
, 0);
651 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
652 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
653 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *)RTA_DATA(tb
[IFLA_MTU
]);
655 ifp
->speed
= get_iflink_speed(ifp
);
656 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
659 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
661 /* Set zebra interface type */
662 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
663 if (IS_ZEBRA_IF_VRF(ifp
))
664 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
667 zebra_if_update_link(ifp
, link_ifindex
);
669 /* Hardware type and address. */
670 ifp
->ll_type
= netlink_to_zebra_link_type(ifi
->ifi_type
);
671 netlink_interface_update_hw_addr(tb
, ifp
);
675 /* Extract and save L2 interface information, take additional actions.
677 netlink_interface_update_l2info(ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
678 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
679 zebra_l2if_update_bridge_slave(ifp
, bridge_ifindex
);
684 /* Request for specific interface or address information from the kernel */
685 static int netlink_request_intf_addr(struct zebra_ns
*zns
, int family
, int type
,
686 u_int32_t filter_mask
)
690 struct ifinfomsg ifm
;
694 /* Form the request, specifying filter (rtattr) if needed. */
695 memset(&req
, 0, sizeof(req
));
696 req
.n
.nlmsg_type
= type
;
697 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
698 req
.ifm
.ifi_family
= family
;
700 /* Include filter, if specified. */
702 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filter_mask
);
704 return netlink_request(&zns
->netlink_cmd
, &req
.n
);
707 /* Interface lookup by netlink socket. */
708 int interface_lookup_netlink(struct zebra_ns
*zns
)
712 /* Get interface information. */
713 ret
= netlink_request_intf_addr(zns
, AF_PACKET
, RTM_GETLINK
, 0);
716 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
721 /* Get interface information - for bridge interfaces. */
722 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
723 RTEXT_FILTER_BRVLAN
);
726 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
731 /* Get interface information - for bridge interfaces. */
732 ret
= netlink_request_intf_addr(zns
, AF_BRIDGE
, RTM_GETLINK
,
733 RTEXT_FILTER_BRVLAN
);
736 ret
= netlink_parse_info(netlink_interface
, &zns
->netlink_cmd
, zns
, 0,
741 /* Get IPv4 address of the interfaces. */
742 ret
= netlink_request_intf_addr(zns
, AF_INET
, RTM_GETADDR
, 0);
745 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
750 /* Get IPv6 address of the interfaces. */
751 ret
= netlink_request_intf_addr(zns
, AF_INET6
, RTM_GETADDR
, 0);
754 ret
= netlink_parse_info(netlink_interface_addr
, &zns
->netlink_cmd
, zns
,
762 int kernel_interface_set_master(struct interface
*master
,
763 struct interface
*slave
)
765 struct zebra_ns
*zns
= zebra_ns_lookup(NS_DEFAULT
);
769 struct ifinfomsg ifa
;
770 char buf
[NL_PKT_BUF_SIZE
];
773 memset(&req
, 0, sizeof req
);
775 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
776 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
777 req
.n
.nlmsg_type
= RTM_SETLINK
;
778 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
780 req
.ifa
.ifi_index
= slave
->ifindex
;
782 addattr_l(&req
.n
, sizeof req
, IFLA_MASTER
, &master
->ifindex
, 4);
783 addattr_l(&req
.n
, sizeof req
, IFLA_LINK
, &slave
->ifindex
, 4);
785 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
789 /* Interface address modification. */
790 static int netlink_address(int cmd
, int family
, struct interface
*ifp
,
791 struct connected
*ifc
)
798 struct ifaddrmsg ifa
;
799 char buf
[NL_PKT_BUF_SIZE
];
802 struct zebra_ns
*zns
;
804 if (vrf_is_backend_netns())
805 zns
= zebra_ns_lookup((ns_id_t
)ifp
->vrf_id
);
807 zns
= zebra_ns_lookup(NS_DEFAULT
);
809 memset(&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
811 bytelen
= (family
== AF_INET
? 4 : 16);
813 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
814 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
815 req
.n
.nlmsg_type
= cmd
;
816 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
818 req
.ifa
.ifa_family
= family
;
820 req
.ifa
.ifa_index
= ifp
->ifindex
;
822 addattr_l(&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
824 if (family
== AF_INET
) {
825 if (CONNECTED_PEER(ifc
)) {
826 p
= ifc
->destination
;
827 addattr_l(&req
.n
, sizeof req
, IFA_ADDRESS
, &p
->u
.prefix
,
829 } else if (cmd
== RTM_NEWADDR
&& ifc
->destination
) {
830 p
= ifc
->destination
;
831 addattr_l(&req
.n
, sizeof req
, IFA_BROADCAST
,
832 &p
->u
.prefix
, bytelen
);
836 /* p is now either ifc->address or ifc->destination */
837 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
839 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
840 SET_FLAG(req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
843 addattr_l(&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
844 strlen(ifc
->label
) + 1);
846 return netlink_talk(netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
,
850 int kernel_address_add_ipv4(struct interface
*ifp
, struct connected
*ifc
)
852 return netlink_address(RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
855 int kernel_address_delete_ipv4(struct interface
*ifp
, struct connected
*ifc
)
857 return netlink_address(RTM_DELADDR
, AF_INET
, ifp
, ifc
);
860 int kernel_address_add_ipv6(struct interface
*ifp
, struct connected
*ifc
)
862 return netlink_address(RTM_NEWADDR
, AF_INET6
, ifp
, ifc
);
865 int kernel_address_delete_ipv6(struct interface
*ifp
, struct connected
*ifc
)
867 return netlink_address(RTM_DELADDR
, AF_INET6
, ifp
, ifc
);
870 int netlink_interface_addr(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
871 ns_id_t ns_id
, int startup
)
874 struct ifaddrmsg
*ifa
;
875 struct rtattr
*tb
[IFA_MAX
+ 1];
876 struct interface
*ifp
;
881 struct zebra_ns
*zns
;
883 zns
= zebra_ns_lookup(ns_id
);
886 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
889 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
892 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
896 memset(tb
, 0, sizeof tb
);
897 netlink_parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), len
);
899 ifp
= if_lookup_by_index_per_ns(zns
, ifa
->ifa_index
);
902 "netlink_interface_addr can't find interface by index %d",
907 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
910 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
911 nl_msg_type_to_str(h
->nlmsg_type
), ifp
->name
,
914 zlog_debug(" IFA_LOCAL %s/%d",
915 inet_ntop(ifa
->ifa_family
,
916 RTA_DATA(tb
[IFA_LOCAL
]), buf
,
920 zlog_debug(" IFA_ADDRESS %s/%d",
921 inet_ntop(ifa
->ifa_family
,
922 RTA_DATA(tb
[IFA_ADDRESS
]), buf
,
925 if (tb
[IFA_BROADCAST
])
926 zlog_debug(" IFA_BROADCAST %s/%d",
927 inet_ntop(ifa
->ifa_family
,
928 RTA_DATA(tb
[IFA_BROADCAST
]), buf
,
931 if (tb
[IFA_LABEL
] && strcmp(ifp
->name
, RTA_DATA(tb
[IFA_LABEL
])))
932 zlog_debug(" IFA_LABEL %s",
933 (char *)RTA_DATA(tb
[IFA_LABEL
]));
935 if (tb
[IFA_CACHEINFO
]) {
936 struct ifa_cacheinfo
*ci
= RTA_DATA(tb
[IFA_CACHEINFO
]);
937 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
938 ci
->ifa_prefered
, ci
->ifa_valid
);
942 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
943 if (tb
[IFA_LOCAL
] == NULL
)
944 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
945 if (tb
[IFA_ADDRESS
] == NULL
)
946 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
948 /* local interface address */
949 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
951 /* is there a peer address? */
953 && memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]),
954 RTA_PAYLOAD(tb
[IFA_ADDRESS
]))) {
955 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
956 SET_FLAG(flags
, ZEBRA_IFA_PEER
);
958 /* seeking a broadcast address */
959 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
])
962 /* addr is primary key, SOL if we don't have one */
964 zlog_debug("%s: NULL address", __func__
);
969 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
970 SET_FLAG(flags
, ZEBRA_IFA_SECONDARY
);
974 label
= (char *)RTA_DATA(tb
[IFA_LABEL
]);
976 if (ifp
&& label
&& strcmp(ifp
->name
, label
) == 0)
979 /* Register interface address to the interface. */
980 if (ifa
->ifa_family
== AF_INET
) {
981 if (h
->nlmsg_type
== RTM_NEWADDR
)
982 connected_add_ipv4(ifp
, flags
, (struct in_addr
*)addr
,
984 (struct in_addr
*)broad
, label
);
986 connected_delete_ipv4(
987 ifp
, flags
, (struct in_addr
*)addr
,
988 ifa
->ifa_prefixlen
, (struct in_addr
*)broad
);
990 if (ifa
->ifa_family
== AF_INET6
) {
991 if (h
->nlmsg_type
== RTM_NEWADDR
) {
992 /* Only consider valid addresses; we'll not get a
994 * the kernel till IPv6 DAD has completed, but at init
996 * does query for and will receive all addresses.
999 & (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
1000 connected_add_ipv6(ifp
, flags
,
1001 (struct in6_addr
*)addr
,
1002 ifa
->ifa_prefixlen
, label
);
1004 connected_delete_ipv6(ifp
, (struct in6_addr
*)addr
,
1005 ifa
->ifa_prefixlen
);
1011 int netlink_link_change(struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1012 ns_id_t ns_id
, int startup
)
1015 struct ifinfomsg
*ifi
;
1016 struct rtattr
*tb
[IFLA_MAX
+ 1];
1017 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1018 struct interface
*ifp
;
1022 char *slave_kind
= NULL
;
1023 struct zebra_ns
*zns
;
1024 vrf_id_t vrf_id
= VRF_DEFAULT
;
1025 zebra_iftype_t zif_type
= ZEBRA_IF_OTHER
;
1026 zebra_slave_iftype_t zif_slave_type
= ZEBRA_IF_SLAVE_NONE
;
1027 ifindex_t bridge_ifindex
= IFINDEX_INTERNAL
;
1028 ifindex_t link_ifindex
= IFINDEX_INTERNAL
;
1031 zns
= zebra_ns_lookup(ns_id
);
1032 ifi
= NLMSG_DATA(h
);
1034 /* assume if not default zns, then new VRF */
1035 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
)) {
1036 /* If this is not link add/delete message so print warning. */
1037 zlog_warn("netlink_link_change: wrong kernel message %d",
1042 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(struct ifinfomsg
));
1046 /* We are interested in some AF_BRIDGE notifications. */
1047 if (ifi
->ifi_family
== AF_BRIDGE
)
1048 return netlink_bridge_interface(h
, len
, ns_id
, startup
);
1050 /* Looking up interface name. */
1051 memset(tb
, 0, sizeof tb
);
1052 memset(linkinfo
, 0, sizeof linkinfo
);
1053 netlink_parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1055 #ifdef IFLA_WIRELESS
1056 /* check for wireless messages to ignore */
1057 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0)) {
1058 if (IS_ZEBRA_DEBUG_KERNEL
)
1059 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1063 #endif /* IFLA_WIRELESS */
1065 if (tb
[IFLA_IFNAME
] == NULL
)
1067 name
= (char *)RTA_DATA(tb
[IFLA_IFNAME
]);
1069 if (tb
[IFLA_LINKINFO
]) {
1070 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1072 if (linkinfo
[IFLA_INFO_KIND
])
1073 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1075 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1076 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1077 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1080 netlink_determine_zebra_iftype(kind
, &zif_type
);
1083 /* If linking to another interface, note it. */
1085 link_ifindex
= *(ifindex_t
*)RTA_DATA(tb
[IFLA_LINK
]);
1087 if (tb
[IFLA_IFALIAS
]) {
1088 desc
= (char *)RTA_DATA(tb
[IFLA_IFALIAS
]);
1091 /* If VRF, create or update the VRF structure itself. */
1092 if (zif_type
== ZEBRA_IF_VRF
&& !vrf_is_backend_netns()) {
1093 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1094 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1097 /* See if interface is present. */
1098 ifp
= if_lookup_by_name_per_ns(zns
, name
);
1102 XFREE(MTYPE_TMP
, ifp
->desc
);
1104 ifp
->desc
= XSTRDUP(MTYPE_TMP
, desc
);
1107 if (h
->nlmsg_type
== RTM_NEWLINK
) {
1108 if (tb
[IFLA_MASTER
]) {
1109 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0)
1110 && !vrf_is_backend_netns()) {
1111 zif_slave_type
= ZEBRA_IF_SLAVE_VRF
;
1113 *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1114 } else if (slave_kind
1115 && (strcmp(slave_kind
, "bridge") == 0)) {
1116 zif_slave_type
= ZEBRA_IF_SLAVE_BRIDGE
;
1118 *(ifindex_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1120 zif_slave_type
= ZEBRA_IF_SLAVE_OTHER
;
1122 if (vrf_is_backend_netns())
1123 vrf_id
= (vrf_id_t
)ns_id
;
1125 || !CHECK_FLAG(ifp
->status
, ZEBRA_INTERFACE_ACTIVE
)) {
1126 /* Add interface notification from kernel */
1127 if (IS_ZEBRA_DEBUG_KERNEL
)
1129 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1130 "sl_type %d master %u flags 0x%x",
1131 name
, ifi
->ifi_index
, vrf_id
, zif_type
,
1132 zif_slave_type
, bridge_ifindex
,
1136 /* unknown interface */
1137 ifp
= if_get_by_name(name
, vrf_id
, 0);
1139 /* pre-configured interface, learnt now */
1140 if (ifp
->vrf_id
!= vrf_id
)
1141 if_update_to_new_vrf(ifp
, vrf_id
);
1144 /* Update interface information. */
1145 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1146 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1147 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1149 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1151 /* Set interface type */
1152 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1153 if (IS_ZEBRA_IF_VRF(ifp
))
1154 SET_FLAG(ifp
->status
,
1155 ZEBRA_INTERFACE_VRF_LOOPBACK
);
1158 zebra_if_update_link(ifp
, link_ifindex
);
1160 netlink_interface_update_hw_addr(tb
, ifp
);
1162 /* Inform clients, install any configured addresses. */
1165 /* Extract and save L2 interface information, take
1166 * additional actions. */
1167 netlink_interface_update_l2info(
1168 ifp
, linkinfo
[IFLA_INFO_DATA
], 1);
1169 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
1170 zebra_l2if_update_bridge_slave(ifp
,
1172 } else if (ifp
->vrf_id
!= vrf_id
) {
1173 /* VRF change for an interface. */
1174 if (IS_ZEBRA_DEBUG_KERNEL
)
1176 "RTM_NEWLINK vrf-change for %s(%u) "
1177 "vrf_id %u -> %u flags 0x%x",
1178 name
, ifp
->ifindex
, ifp
->vrf_id
, vrf_id
,
1181 if_handle_vrf_change(ifp
, vrf_id
);
1183 int was_bridge_slave
;
1185 /* Interface update. */
1186 if (IS_ZEBRA_DEBUG_KERNEL
)
1188 "RTM_NEWLINK update for %s(%u) "
1189 "sl_type %d master %u flags 0x%x",
1190 name
, ifp
->ifindex
, zif_slave_type
,
1191 bridge_ifindex
, ifi
->ifi_flags
);
1193 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
1194 ifp
->mtu6
= ifp
->mtu
= *(int *)RTA_DATA(tb
[IFLA_MTU
]);
1197 /* Update interface type - NOTE: Only slave_type can
1199 was_bridge_slave
= IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
);
1200 zebra_if_set_ziftype(ifp
, zif_type
, zif_slave_type
);
1202 netlink_interface_update_hw_addr(tb
, ifp
);
1204 if (if_is_no_ptm_operative(ifp
)) {
1205 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1206 if (!if_is_no_ptm_operative(ifp
)) {
1207 if (IS_ZEBRA_DEBUG_KERNEL
)
1209 "Intf %s(%u) has gone DOWN",
1210 name
, ifp
->ifindex
);
1212 } else if (if_is_operative(ifp
)) {
1213 /* Must notify client daemons of new
1214 * interface status. */
1215 if (IS_ZEBRA_DEBUG_KERNEL
)
1217 "Intf %s(%u) PTM up, notifying clients",
1218 name
, ifp
->ifindex
);
1219 zebra_interface_up_update(ifp
);
1222 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1223 if (if_is_operative(ifp
)) {
1224 if (IS_ZEBRA_DEBUG_KERNEL
)
1226 "Intf %s(%u) has come UP",
1227 name
, ifp
->ifindex
);
1232 /* Extract and save L2 interface information, take
1233 * additional actions. */
1234 netlink_interface_update_l2info(
1235 ifp
, linkinfo
[IFLA_INFO_DATA
], 0);
1236 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
) || was_bridge_slave
)
1237 zebra_l2if_update_bridge_slave(ifp
,
1241 /* Delete interface notification from kernel */
1243 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1244 name
, ifi
->ifi_index
);
1248 if (IS_ZEBRA_DEBUG_KERNEL
)
1249 zlog_debug("RTM_DELLINK for %s(%u)", name
,
1252 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1254 /* Special handling for bridge or VxLAN interfaces. */
1255 if (IS_ZEBRA_IF_BRIDGE(ifp
))
1256 zebra_l2_bridge_del(ifp
);
1257 else if (IS_ZEBRA_IF_VXLAN(ifp
))
1258 zebra_l2_vxlanif_del(ifp
);
1260 if (!IS_ZEBRA_IF_VRF(ifp
))
1261 if_delete_update(ifp
);
1267 /* Interface information read by netlink. */
1268 void interface_list(struct zebra_ns
*zns
)
1270 interface_lookup_netlink(zns
);
1273 #endif /* GNU_LINUX */