blob: c97979960ef51101bcddf6771a3b22e719eaca91 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Interface looking up by ioctl ().
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "if.h"
26#include "sockunion.h"
27#include "prefix.h"
28#include "ioctl.h"
29#include "connected.h"
30#include "memory.h"
31#include "log.h"
32
33#include "zebra/interface.h"
34
35/* Interface looking up using infamous SIOCGIFCONF. */
36int
37interface_list_ioctl ()
38{
39 int ret;
40 int sock;
41#define IFNUM_BASE 32
42 int ifnum;
43 struct ifreq *ifreq;
44 struct ifconf ifconf;
45 struct interface *ifp;
46 int n;
47 int lastlen;
48
49 /* Normally SIOCGIFCONF works with AF_INET socket. */
50 sock = socket (AF_INET, SOCK_DGRAM, 0);
51 if (sock < 0)
52 {
53 zlog_warn ("Can't make AF_INET socket stream: %s", strerror (errno));
54 return -1;
55 }
56
57 /* Set initial ifreq count. This will be double when SIOCGIFCONF
58 fail. Solaris has SIOCGIFNUM. */
59#ifdef SIOCGIFNUM
60 ret = ioctl (sock, SIOCGIFNUM, &ifnum);
61 if (ret < 0)
62 ifnum = IFNUM_BASE;
63 else
64 ifnum++;
65#else
66 ifnum = IFNUM_BASE;
67#endif /* SIOCGIFNUM */
68
69 ifconf.ifc_buf = NULL;
70
71 lastlen = 0;
72 /* Loop until SIOCGIFCONF success. */
73 for (;;)
74 {
75 ifconf.ifc_len = sizeof (struct ifreq) * ifnum;
76 ifconf.ifc_buf = XREALLOC(MTYPE_TMP, ifconf.ifc_buf, ifconf.ifc_len);
77
78 ret = ioctl(sock, SIOCGIFCONF, &ifconf);
79
80 if (ret < 0)
81 {
82 zlog_warn ("SIOCGIFCONF: %s", strerror(errno));
83 goto end;
84 }
85 /* Repeatedly get info til buffer fails to grow. */
86 if (ifconf.ifc_len > lastlen)
87 {
88 lastlen = ifconf.ifc_len;
89 ifnum += 10;
90 continue;
91 }
92 /* Success. */
93 break;
94 }
95
96 /* Allocate interface. */
97 ifreq = ifconf.ifc_req;
98
99#ifdef OPEN_BSD
100 for (n = 0; n < ifconf.ifc_len; )
101 {
102 int size;
103
104 ifreq = (struct ifreq *)((caddr_t) ifconf.ifc_req + n);
105 ifp = if_get_by_name (ifreq->ifr_name);
106 if_add_update (ifp);
107 size = ifreq->ifr_addr.sa_len;
108 if (size < sizeof (ifreq->ifr_addr))
109 size = sizeof (ifreq->ifr_addr);
110 size += sizeof (ifreq->ifr_name);
111 n += size;
112 }
113#else
114 for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq))
115 {
116 ifp = if_get_by_name (ifreq->ifr_name);
117 if_add_update (ifp);
118 ifreq++;
119 }
120#endif /* OPEN_BSD */
121
122 end:
123 close (sock);
124 XFREE (MTYPE_TMP, ifconf.ifc_buf);
125
126 return ret;
127}
128
129/* Get interface's index by ioctl. */
130int
131if_get_index (struct interface *ifp)
132{
paulab836aa2002-12-13 21:19:02 +0000133#if defined(HAVE_IF_NAMETOINDEX)
134 /* Modern systems should have if_nametoindex(3). */
135 ifp->ifindex = if_nametoindex(ifp->name);
136#elif defined(SIOCGIFINDEX) && !defined(HAVE_BROKEN_ALIASES)
137 /* Fall-back for older linuxes. */
paul718e3742002-12-13 20:15:29 +0000138 int ret;
139 struct ifreq ifreq;
140
141 ifreq_set_name (&ifreq, ifp);
142
143 ret = if_ioctl (SIOCGIFINDEX, (caddr_t) &ifreq);
144 if (ret < 0)
145 {
146 /* Linux 2.0.X does not have interface index. */
147 ifp->ifindex = if_fake_index++;
148 return ifp->ifindex;
149 }
150
151 /* OK we got interface index. */
152#ifdef ifr_ifindex
153 ifp->ifindex = ifreq.ifr_ifindex;
154#else
155 ifp->ifindex = ifreq.ifr_index;
156#endif
paul718e3742002-12-13 20:15:29 +0000157
158#else
paulab836aa2002-12-13 21:19:02 +0000159/* Linux 2.2.X does not provide individual interface index
160 for aliases and we know it. For others issue a warning. */
161#if !defined(HAVE_BROKEN_ALIASES)
162#warning "Using if_fake_index. You may want to add appropriate"
163#warning "mapping from ifname to ifindex for your system..."
164#endif
165 /* This branch probably won't provide usable results, but anyway... */
166 static int if_fake_index = 1;
paul718e3742002-12-13 20:15:29 +0000167 ifp->ifindex = if_fake_index++;
paulab836aa2002-12-13 21:19:02 +0000168#endif
169
paul718e3742002-12-13 20:15:29 +0000170 return ifp->ifindex;
paul718e3742002-12-13 20:15:29 +0000171}
172
173#ifdef SIOCGIFHWADDR
174int
175if_get_hwaddr (struct interface *ifp)
176{
177 int ret;
178 struct ifreq ifreq;
179 int i;
180
181 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
182 ifreq.ifr_addr.sa_family = AF_INET;
183
184 /* Fetch Hardware address if available. */
185 ret = if_ioctl (SIOCGIFHWADDR, (caddr_t) &ifreq);
186 if (ret < 0)
187 ifp->hw_addr_len = 0;
188 else
189 {
190 memcpy (ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6);
191
192 for (i = 0; i < 6; i++)
193 if (ifp->hw_addr[i] != 0)
194 break;
195
196 if (i == 6)
197 ifp->hw_addr_len = 0;
198 else
199 ifp->hw_addr_len = 6;
200 }
201 return 0;
202}
203#endif /* SIOCGIFHWADDR */
204
205#ifdef HAVE_GETIFADDRS
206#include <ifaddrs.h>
207
208int
209if_getaddrs ()
210{
211 int ret;
212 struct ifaddrs *ifap;
213 struct ifaddrs *ifapfree;
214 struct interface *ifp;
215 int prefixlen;
216
217 ret = getifaddrs (&ifap);
218 if (ret != 0)
219 {
220 zlog_err ("getifaddrs(): %s", strerror (errno));
221 return -1;
222 }
223
224 for (ifapfree = ifap; ifap; ifap = ifap->ifa_next)
225 {
226 ifp = if_lookup_by_name (ifap->ifa_name);
227 if (ifp == NULL)
228 {
229 zlog_err ("if_getaddrs(): Can't lookup interface %s\n",
230 ifap->ifa_name);
231 continue;
232 }
233
234 if (ifap->ifa_addr->sa_family == AF_INET)
235 {
236 struct sockaddr_in *addr;
237 struct sockaddr_in *mask;
238 struct sockaddr_in *dest;
239 struct in_addr *dest_pnt;
240
241 addr = (struct sockaddr_in *) ifap->ifa_addr;
242 mask = (struct sockaddr_in *) ifap->ifa_netmask;
243 prefixlen = ip_masklen (mask->sin_addr);
244
245 dest_pnt = NULL;
246
247 if (ifap->ifa_flags & IFF_POINTOPOINT)
248 {
249 dest = (struct sockaddr_in *) ifap->ifa_dstaddr;
250 dest_pnt = &dest->sin_addr;
251 }
252
253 if (ifap->ifa_flags & IFF_BROADCAST)
254 {
255 dest = (struct sockaddr_in *) ifap->ifa_broadaddr;
256 dest_pnt = &dest->sin_addr;
257 }
258
259 connected_add_ipv4 (ifp, 0, &addr->sin_addr,
260 prefixlen, dest_pnt, NULL);
261 }
262#ifdef HAVE_IPV6
263 if (ifap->ifa_addr->sa_family == AF_INET6)
264 {
265 struct sockaddr_in6 *addr;
266 struct sockaddr_in6 *mask;
267 struct sockaddr_in6 *dest;
268 struct in6_addr *dest_pnt;
269
270 addr = (struct sockaddr_in6 *) ifap->ifa_addr;
271 mask = (struct sockaddr_in6 *) ifap->ifa_netmask;
272 prefixlen = ip6_masklen (mask->sin6_addr);
273
274 dest_pnt = NULL;
275
276 if (ifap->ifa_flags & IFF_POINTOPOINT)
277 {
278 if (ifap->ifa_dstaddr)
279 {
280 dest = (struct sockaddr_in6 *) ifap->ifa_dstaddr;
281 dest_pnt = &dest->sin6_addr;
282 }
283 }
284
285 if (ifap->ifa_flags & IFF_BROADCAST)
286 {
287 if (ifap->ifa_broadaddr)
288 {
289 dest = (struct sockaddr_in6 *) ifap->ifa_broadaddr;
290 dest_pnt = &dest->sin6_addr;
291 }
292 }
293
paulab836aa2002-12-13 21:19:02 +0000294#if defined(KAME)
295 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
296 {
297 addr->sin6_scope_id =
298 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]);
299 addr->sin6_addr.s6_addr[2] = addr->sin6_addr.s6_addr[3] = 0;
300 }
301#endif
302
paul718e3742002-12-13 20:15:29 +0000303 connected_add_ipv6 (ifp, &addr->sin6_addr, prefixlen, dest_pnt);
304 }
305#endif /* HAVE_IPV6 */
306 }
307
308 freeifaddrs (ifapfree);
309
310 return 0;
311}
312#else /* HAVE_GETIFADDRS */
313/* Interface address lookup by ioctl. This function only looks up
314 IPv4 address. */
315int
316if_get_addr (struct interface *ifp)
317{
318 int ret;
319 struct ifreq ifreq;
320 struct sockaddr_in addr;
321 struct sockaddr_in mask;
322 struct sockaddr_in dest;
323 struct in_addr *dest_pnt;
324 u_char prefixlen;
325
326 /* Interface's name and address family. */
327 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
328 ifreq.ifr_addr.sa_family = AF_INET;
329
330 /* Interface's address. */
331 ret = if_ioctl (SIOCGIFADDR, (caddr_t) &ifreq);
332 if (ret < 0)
333 {
334 if (errno != EADDRNOTAVAIL)
335 {
336 zlog_warn ("SIOCGIFADDR fail: %s", strerror (errno));
337 return ret;
338 }
339 return 0;
340 }
341 memcpy (&addr, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
342
343 /* Interface's network mask. */
344 ret = if_ioctl (SIOCGIFNETMASK, (caddr_t) &ifreq);
345 if (ret < 0)
346 {
347 if (errno != EADDRNOTAVAIL)
348 {
349 zlog_warn ("SIOCGIFNETMASK fail: %s", strerror (errno));
350 return ret;
351 }
352 return 0;
353 }
354#ifdef ifr_netmask
355 memcpy (&mask, &ifreq.ifr_netmask, sizeof (struct sockaddr_in));
356#else
357 memcpy (&mask, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
358#endif /* ifr_netmask */
359 prefixlen = ip_masklen (mask.sin_addr);
360
361 /* Point to point or borad cast address pointer init. */
362 dest_pnt = NULL;
363
364 if (ifp->flags & IFF_POINTOPOINT)
365 {
366 ret = if_ioctl (SIOCGIFDSTADDR, (caddr_t) &ifreq);
367 if (ret < 0)
368 {
369 if (errno != EADDRNOTAVAIL)
370 {
371 zlog_warn ("SIOCGIFDSTADDR fail: %s", strerror (errno));
372 return ret;
373 }
374 return 0;
375 }
376 memcpy (&dest, &ifreq.ifr_dstaddr, sizeof (struct sockaddr_in));
377 dest_pnt = &dest.sin_addr;
378 }
379 if (ifp->flags & IFF_BROADCAST)
380 {
381 ret = if_ioctl (SIOCGIFBRDADDR, (caddr_t) &ifreq);
382 if (ret < 0)
383 {
384 if (errno != EADDRNOTAVAIL)
385 {
386 zlog_warn ("SIOCGIFBRDADDR fail: %s", strerror (errno));
387 return ret;
388 }
389 return 0;
390 }
391 memcpy (&dest, &ifreq.ifr_broadaddr, sizeof (struct sockaddr_in));
392 dest_pnt = &dest.sin_addr;
393 }
394
395
396 /* Set address to the interface. */
397 connected_add_ipv4 (ifp, 0, &addr.sin_addr, prefixlen, dest_pnt, NULL);
398
399 return 0;
400}
401#endif /* HAVE_GETIFADDRS */
402
403/* Fetch interface information via ioctl(). */
404static void
405interface_info_ioctl ()
406{
407 listnode node;
408 struct interface *ifp;
409
410 for (node = listhead (iflist); node; node = nextnode (node))
411 {
412 ifp = getdata (node);
413
414 if_get_index (ifp);
415#ifdef SIOCGIFHWADDR
416 if_get_hwaddr (ifp);
417#endif /* SIOCGIFHWADDR */
418 if_get_flags (ifp);
419#ifndef HAVE_GETIFADDRS
420 if_get_addr (ifp);
421#endif /* ! HAVE_GETIFADDRS */
422 if_get_mtu (ifp);
423 if_get_metric (ifp);
424 }
425}
426
427/* Lookup all interface information. */
428void
429interface_list ()
430{
431 /* Linux can do both proc & ioctl, ioctl is the only way to get
432 interface aliases in 2.2 series kernels. */
433#ifdef HAVE_PROC_NET_DEV
434 interface_list_proc ();
435#endif /* HAVE_PROC_NET_DEV */
436 interface_list_ioctl ();
437
438 /* After listing is done, get index, address, flags and other
439 interface's information. */
440 interface_info_ioctl ();
441
442#ifdef HAVE_GETIFADDRS
443 if_getaddrs ();
444#endif /* HAVE_GETIFADDRS */
445
446#if defined(HAVE_IPV6) && defined(HAVE_PROC_NET_IF_INET6)
447 /* Linux provides interface's IPv6 address via
448 /proc/net/if_inet6. */
449 ifaddr_proc_ipv6 ();
450#endif /* HAVE_IPV6 && HAVE_PROC_NET_IF_INET6 */
451}