]>
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"
32 #include "zebra_memory.h"
37 #include "zebra/interface.h"
38 #include "zebra/rib.h"
42 /* Interface looking up using infamous SIOCGIFCONF. */
43 static int interface_list_ioctl(void)
51 struct interface
*ifp
;
55 /* Normally SIOCGIFCONF works with AF_INET socket. */
56 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
58 zlog_warn("Can't make AF_INET socket stream: %s",
59 safe_strerror(errno
));
63 /* Set initial ifreq count. This will be double when SIOCGIFCONF
64 fail. Solaris has SIOCGIFNUM. */
66 ret
= ioctl(sock
, SIOCGIFNUM
, &ifnum
);
73 #endif /* SIOCGIFNUM */
75 ifconf
.ifc_buf
= NULL
;
78 /* Loop until SIOCGIFCONF success. */
80 ifconf
.ifc_len
= sizeof(struct ifreq
) * ifnum
;
82 XREALLOC(MTYPE_TMP
, ifconf
.ifc_buf
, ifconf
.ifc_len
);
84 ret
= ioctl(sock
, SIOCGIFCONF
, &ifconf
);
87 zlog_warn("SIOCGIFCONF: %s", safe_strerror(errno
));
90 /* Repeatedly get info til buffer fails to grow. */
91 if (ifconf
.ifc_len
> lastlen
) {
92 lastlen
= ifconf
.ifc_len
;
100 /* Allocate interface. */
101 ifreq
= ifconf
.ifc_req
;
104 for (n
= 0; n
< ifconf
.ifc_len
;) {
107 ifreq
= (struct ifreq
*)((caddr_t
)ifconf
.ifc_req
+ n
);
108 ifp
= if_get_by_name(ifreq
->ifr_name
, VRF_DEFAULT
, 0);
110 size
= ifreq
->ifr_addr
.sa_len
;
111 if (size
< sizeof(ifreq
->ifr_addr
))
112 size
= sizeof(ifreq
->ifr_addr
);
113 size
+= sizeof(ifreq
->ifr_name
);
117 for (n
= 0; n
< ifconf
.ifc_len
; n
+= sizeof(struct ifreq
)) {
118 ifp
= if_get_by_name(ifreq
->ifr_name
, VRF_DEFAULT
, 0);
122 #endif /* OPEN_BSD */
126 XFREE(MTYPE_TMP
, ifconf
.ifc_buf
);
131 /* Get interface's index by ioctl. */
132 static int if_get_index(struct interface
*ifp
)
134 if_set_index(ifp
, if_nametoindex(ifp
->name
));
139 static int if_get_hwaddr(struct interface
*ifp
)
145 strncpy(ifreq
.ifr_name
, ifp
->name
, IFNAMSIZ
);
146 ifreq
.ifr_addr
.sa_family
= AF_INET
;
148 /* Fetch Hardware address if available. */
149 ret
= vrf_if_ioctl(SIOCGIFHWADDR
, (caddr_t
)&ifreq
, ifp
->vrf_id
);
151 ifp
->hw_addr_len
= 0;
153 memcpy(ifp
->hw_addr
, ifreq
.ifr_hwaddr
.sa_data
, 6);
155 for (i
= 0; i
< 6; i
++)
156 if (ifp
->hw_addr
[i
] != 0)
160 ifp
->hw_addr_len
= 0;
162 ifp
->hw_addr_len
= 6;
166 #endif /* SIOCGIFHWADDR */
168 static int if_getaddrs(void)
171 struct ifaddrs
*ifap
;
172 struct ifaddrs
*ifapfree
;
173 struct interface
*ifp
;
176 ret
= getifaddrs(&ifap
);
178 zlog_err("getifaddrs(): %s", safe_strerror(errno
));
182 for (ifapfree
= ifap
; ifap
; ifap
= ifap
->ifa_next
) {
183 if (ifap
->ifa_addr
== NULL
) {
185 "%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
187 (ifap
->ifa_name
? ifap
->ifa_name
: "(null)"));
191 ifp
= if_lookup_by_name(ifap
->ifa_name
, VRF_DEFAULT
);
193 zlog_err("if_getaddrs(): Can't lookup interface %s\n",
198 if (ifap
->ifa_addr
->sa_family
== AF_INET
) {
199 struct sockaddr_in
*addr
;
200 struct sockaddr_in
*mask
;
201 struct sockaddr_in
*dest
;
202 struct in_addr
*dest_pnt
;
205 addr
= (struct sockaddr_in
*)ifap
->ifa_addr
;
206 mask
= (struct sockaddr_in
*)ifap
->ifa_netmask
;
207 prefixlen
= ip_masklen(mask
->sin_addr
);
211 if (if_is_pointopoint(ifp
) && ifap
->ifa_dstaddr
212 && !IPV4_ADDR_SAME(&addr
->sin_addr
,
213 &((struct sockaddr_in
*)
216 dest
= (struct sockaddr_in
*)ifap
->ifa_dstaddr
;
217 dest_pnt
= &dest
->sin_addr
;
218 flags
= ZEBRA_IFA_PEER
;
219 } else if (ifap
->ifa_broadaddr
222 &((struct sockaddr_in
*)
225 dest
= (struct sockaddr_in
*)
227 dest_pnt
= &dest
->sin_addr
;
230 connected_add_ipv4(ifp
, flags
, &addr
->sin_addr
,
231 prefixlen
, dest_pnt
, NULL
);
233 if (ifap
->ifa_addr
->sa_family
== AF_INET6
) {
234 struct sockaddr_in6
*addr
;
235 struct sockaddr_in6
*mask
;
238 addr
= (struct sockaddr_in6
*)ifap
->ifa_addr
;
239 mask
= (struct sockaddr_in6
*)ifap
->ifa_netmask
;
240 prefixlen
= ip6_masklen(mask
->sin6_addr
);
243 if (IN6_IS_ADDR_LINKLOCAL(&addr
->sin6_addr
)) {
244 addr
->sin6_scope_id
=
245 ntohs(*(u_int16_t
*)&addr
->sin6_addr
247 addr
->sin6_addr
.s6_addr
[2] =
248 addr
->sin6_addr
.s6_addr
[3] = 0;
252 connected_add_ipv6(ifp
, flags
, &addr
->sin6_addr
,
257 freeifaddrs(ifapfree
);
262 /* Fetch interface information via ioctl(). */
263 static void interface_info_ioctl()
265 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
266 struct interface
*ifp
;
268 FOR_ALL_INTERFACES (vrf
, ifp
) {
272 #endif /* SIOCGIFHWADDR */
279 /* Lookup all interface information. */
280 void interface_list(struct zebra_ns
*zns
)
283 zlog_info("interface_list: NS %u", zns
->ns_id
);
285 /* Linux can do both proc & ioctl, ioctl is the only way to get
286 interface aliases in 2.2 series kernels. */
287 #ifdef HAVE_PROC_NET_DEV
288 interface_list_proc();
289 #endif /* HAVE_PROC_NET_DEV */
290 interface_list_ioctl();
292 /* After listing is done, get index, address, flags and other
293 interface's information. */
294 interface_info_ioctl();
298 #if defined(HAVE_PROC_NET_IF_INET6)
299 /* Linux provides interface's IPv6 address via
300 /proc/net/if_inet6. */
302 #endif /* HAVE_PROC_NET_IF_INET6 */
305 #endif /* OPEN_BSD */