]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/if_ioctl.c
2 * Interface looking up by ioctl ().
3 * Copyright (C) 1997, 98 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
27 #include "sockunion.h"
30 #include "connected.h"
35 #include "lib_errors.h"
37 #include "zebra/interface.h"
38 #include "zebra/rib.h"
40 #include "zebra/zebra_errors.h"
44 /* Interface looking up using infamous SIOCGIFCONF. */
45 static int interface_list_ioctl(void)
53 struct interface
*ifp
;
57 /* Normally SIOCGIFCONF works with AF_INET socket. */
58 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
60 flog_err_sys(EC_LIB_SOCKET
,
61 "Can't make AF_INET socket stream: %s",
62 safe_strerror(errno
));
66 /* Set initial ifreq count. This will be double when SIOCGIFCONF
67 fail. Solaris has SIOCGIFNUM. */
69 ret
= ioctl(sock
, SIOCGIFNUM
, &ifnum
);
76 #endif /* SIOCGIFNUM */
78 ifconf
.ifc_buf
= NULL
;
81 /* Loop until SIOCGIFCONF success. */
83 ifconf
.ifc_len
= sizeof(struct ifreq
) * ifnum
;
85 XREALLOC(MTYPE_TMP
, ifconf
.ifc_buf
, ifconf
.ifc_len
);
87 ret
= ioctl(sock
, SIOCGIFCONF
, &ifconf
);
90 flog_err_sys(EC_LIB_SYSTEM_CALL
, "SIOCGIFCONF: %s",
91 safe_strerror(errno
));
94 /* Repeatedly get info til buffer fails to grow. */
95 if (ifconf
.ifc_len
> lastlen
) {
96 lastlen
= ifconf
.ifc_len
;
104 /* Allocate interface. */
105 ifreq
= ifconf
.ifc_req
;
108 for (n
= 0; n
< ifconf
.ifc_len
;) {
111 ifreq
= (struct ifreq
*)((caddr_t
)ifconf
.ifc_req
+ n
);
112 ifp
= if_get_by_name(ifreq
->ifr_name
, VRF_DEFAULT
,
115 size
= ifreq
->ifr_addr
.sa_len
;
116 if (size
< sizeof(ifreq
->ifr_addr
))
117 size
= sizeof(ifreq
->ifr_addr
);
118 size
+= sizeof(ifreq
->ifr_name
);
122 for (n
= 0; n
< ifconf
.ifc_len
; n
+= sizeof(struct ifreq
)) {
123 ifp
= if_get_by_name(ifreq
->ifr_name
, VRF_DEFAULT
,
128 #endif /* OPEN_BSD */
132 XFREE(MTYPE_TMP
, ifconf
.ifc_buf
);
137 /* Get interface's index by ioctl. */
138 static int if_get_index(struct interface
*ifp
)
140 if_set_index(ifp
, if_nametoindex(ifp
->name
));
145 static int if_get_hwaddr(struct interface
*ifp
)
151 strlcpy(ifreq
.ifr_name
, ifp
->name
, sizeof(ifreq
.ifr_name
));
152 ifreq
.ifr_addr
.sa_family
= AF_INET
;
154 /* Fetch Hardware address if available. */
155 ret
= vrf_if_ioctl(SIOCGIFHWADDR
, (caddr_t
)&ifreq
, ifp
->vrf
->vrf_id
);
157 ifp
->hw_addr_len
= 0;
159 memcpy(ifp
->hw_addr
, ifreq
.ifr_hwaddr
.sa_data
, 6);
161 for (i
= 0; i
< 6; i
++)
162 if (ifp
->hw_addr
[i
] != 0)
166 ifp
->hw_addr_len
= 0;
168 ifp
->hw_addr_len
= 6;
172 #endif /* SIOCGIFHWADDR */
174 static int if_getaddrs(void)
177 struct ifaddrs
*ifap
;
178 struct ifaddrs
*ifapfree
;
179 struct interface
*ifp
;
182 ret
= getifaddrs(&ifap
);
184 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getifaddrs(): %s",
185 safe_strerror(errno
));
189 for (ifapfree
= ifap
; ifap
; ifap
= ifap
->ifa_next
) {
190 if (ifap
->ifa_addr
== NULL
) {
193 "%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
195 (ifap
->ifa_name
? ifap
->ifa_name
: "(null)"));
199 ifp
= if_lookup_by_name(ifap
->ifa_name
, VRF_DEFAULT
);
201 flog_err(EC_LIB_INTERFACE
,
202 "if_getaddrs(): Can't lookup interface %s\n",
207 if (ifap
->ifa_addr
->sa_family
== AF_INET
) {
208 struct sockaddr_in
*addr
;
209 struct sockaddr_in
*mask
;
210 struct sockaddr_in
*dest
;
211 struct in_addr
*dest_pnt
;
214 addr
= (struct sockaddr_in
*)ifap
->ifa_addr
;
215 mask
= (struct sockaddr_in
*)ifap
->ifa_netmask
;
216 prefixlen
= ip_masklen(mask
->sin_addr
);
220 if (if_is_pointopoint(ifp
) && ifap
->ifa_dstaddr
221 && !IPV4_ADDR_SAME(&addr
->sin_addr
,
222 &((struct sockaddr_in
*)
225 dest
= (struct sockaddr_in
*)ifap
->ifa_dstaddr
;
226 dest_pnt
= &dest
->sin_addr
;
227 flags
= ZEBRA_IFA_PEER
;
228 } else if (ifap
->ifa_broadaddr
231 &((struct sockaddr_in
*)
234 dest
= (struct sockaddr_in
*)
236 dest_pnt
= &dest
->sin_addr
;
239 connected_add_ipv4(ifp
, flags
, &addr
->sin_addr
,
240 prefixlen
, dest_pnt
, NULL
,
243 if (ifap
->ifa_addr
->sa_family
== AF_INET6
) {
244 struct sockaddr_in6
*addr
;
245 struct sockaddr_in6
*mask
;
248 addr
= (struct sockaddr_in6
*)ifap
->ifa_addr
;
249 mask
= (struct sockaddr_in6
*)ifap
->ifa_netmask
;
250 prefixlen
= ip6_masklen(mask
->sin6_addr
);
253 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
254 addr
->sin6_scope_id
=
255 ntohs(*(uint16_t *)&addr
->sin6_addr
257 addr
->sin6_addr
.s6_addr
[2] =
258 addr
->sin6_addr
.s6_addr
[3] = 0;
262 connected_add_ipv6(ifp
, flags
, &addr
->sin6_addr
, NULL
,
263 prefixlen
, NULL
, METRIC_MAX
);
267 freeifaddrs(ifapfree
);
272 /* Fetch interface information via ioctl(). */
273 static void interface_info_ioctl()
275 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
276 struct interface
*ifp
;
278 FOR_ALL_INTERFACES (vrf
, ifp
) {
282 #endif /* SIOCGIFHWADDR */
289 /* Lookup all interface information. */
290 void interface_list(struct zebra_ns
*zns
)
293 zlog_info("interface_list: NS %u", zns
->ns_id
);
295 /* Linux can do both proc & ioctl, ioctl is the only way to get
296 interface aliases in 2.2 series kernels. */
297 #ifdef HAVE_PROC_NET_DEV
298 interface_list_proc();
299 #endif /* HAVE_PROC_NET_DEV */
300 interface_list_ioctl();
302 /* After listing is done, get index, address, flags and other
303 interface's information. */
304 interface_info_ioctl();
308 #if defined(HAVE_PROC_NET_IF_INET6)
309 /* Linux provides interface's IPv6 address via
310 /proc/net/if_inet6. */
312 #endif /* HAVE_PROC_NET_IF_INET6 */
315 #endif /* OPEN_BSD */