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
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
26 #include "sockunion.h"
29 #include "connected.h"
35 #include "zebra/interface.h"
36 #include "zebra/rib.h"
38 /* Interface looking up using infamous SIOCGIFCONF. */
40 interface_list_ioctl (void)
48 struct interface *ifp;
52 /* Normally SIOCGIFCONF works with AF_INET socket. */
53 sock = socket (AF_INET, SOCK_DGRAM, 0);
56 zlog_warn ("Can't make AF_INET socket stream: %s", safe_strerror (errno));
60 /* Set initial ifreq count. This will be double when SIOCGIFCONF
61 fail. Solaris has SIOCGIFNUM. */
63 ret = ioctl (sock, SIOCGIFNUM, &ifnum);
70 #endif /* SIOCGIFNUM */
72 ifconf.ifc_buf = NULL;
75 /* Loop until SIOCGIFCONF success. */
78 ifconf.ifc_len = sizeof (struct ifreq) * ifnum;
79 ifconf.ifc_buf = XREALLOC(MTYPE_TMP, ifconf.ifc_buf, ifconf.ifc_len);
81 ret = ioctl(sock, SIOCGIFCONF, &ifconf);
85 zlog_warn ("SIOCGIFCONF: %s", safe_strerror(errno));
88 /* Repeatedly get info til buffer fails to grow. */
89 if (ifconf.ifc_len > lastlen)
91 lastlen = ifconf.ifc_len;
99 /* Allocate interface. */
100 ifreq = ifconf.ifc_req;
103 for (n = 0; n < ifconf.ifc_len; )
107 ifreq = (struct ifreq *)((caddr_t) ifconf.ifc_req + n);
108 ifp = if_get_by_name_len(ifreq->ifr_name,
109 strnlen(ifreq->ifr_name,
110 sizeof(ifreq->ifr_name)));
112 size = ifreq->ifr_addr.sa_len;
113 if (size < sizeof (ifreq->ifr_addr))
114 size = sizeof (ifreq->ifr_addr);
115 size += sizeof (ifreq->ifr_name);
119 for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq))
121 ifp = if_get_by_name_len(ifreq->ifr_name,
122 strnlen(ifreq->ifr_name,
123 sizeof(ifreq->ifr_name)));
127 #endif /* OPEN_BSD */
131 XFREE (MTYPE_TMP, ifconf.ifc_buf);
136 /* Get interface's index by ioctl. */
138 if_get_index (struct interface *ifp)
140 #if defined(HAVE_IF_NAMETOINDEX)
141 /* Modern systems should have if_nametoindex(3). */
142 ifp->ifindex = if_nametoindex(ifp->name);
143 #elif defined(SIOCGIFINDEX) && !defined(HAVE_BROKEN_ALIASES)
144 /* Fall-back for older linuxes. */
147 static int if_fake_index;
149 ifreq_set_name (&ifreq, ifp);
151 ret = if_ioctl (SIOCGIFINDEX, (caddr_t) &ifreq);
154 /* Linux 2.0.X does not have interface index. */
155 ifp->ifindex = if_fake_index++;
159 /* OK we got interface index. */
161 ifp->ifindex = ifreq.ifr_ifindex;
163 ifp->ifindex = ifreq.ifr_index;
167 /* Linux 2.2.X does not provide individual interface index
168 for aliases and we know it. For others issue a warning. */
169 #if !defined(HAVE_BROKEN_ALIASES)
170 #warning "Using if_fake_index. You may want to add appropriate"
171 #warning "mapping from ifname to ifindex for your system..."
173 /* This branch probably won't provide usable results, but anyway... */
174 static int if_fake_index = 1;
175 ifp->ifindex = if_fake_index++;
183 if_get_hwaddr (struct interface *ifp)
189 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
190 ifreq.ifr_addr.sa_family = AF_INET;
192 /* Fetch Hardware address if available. */
193 ret = if_ioctl (SIOCGIFHWADDR, (caddr_t) &ifreq);
195 ifp->hw_addr_len = 0;
198 memcpy (ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6);
200 for (i = 0; i < 6; i++)
201 if (ifp->hw_addr[i] != 0)
205 ifp->hw_addr_len = 0;
207 ifp->hw_addr_len = 6;
211 #endif /* SIOCGIFHWADDR */
213 #ifdef HAVE_GETIFADDRS
220 struct ifaddrs *ifap;
221 struct ifaddrs *ifapfree;
222 struct interface *ifp;
225 ret = getifaddrs (&ifap);
228 zlog_err ("getifaddrs(): %s", safe_strerror (errno));
232 for (ifapfree = ifap; ifap; ifap = ifap->ifa_next)
234 if (ifap->ifa_addr == NULL)
236 zlog_err ("%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
237 __func__, (ifap->ifa_name ? ifap->ifa_name : "(null)"));
241 ifp = if_lookup_by_name (ifap->ifa_name);
244 zlog_err ("if_getaddrs(): Can't lookup interface %s\n",
249 if (ifap->ifa_addr->sa_family == AF_INET)
251 struct sockaddr_in *addr;
252 struct sockaddr_in *mask;
253 struct sockaddr_in *dest;
254 struct in_addr *dest_pnt;
257 addr = (struct sockaddr_in *) ifap->ifa_addr;
258 mask = (struct sockaddr_in *) ifap->ifa_netmask;
259 prefixlen = ip_masklen (mask->sin_addr);
263 if (ifap->ifa_dstaddr &&
264 !IPV4_ADDR_SAME(&addr->sin_addr,
265 &((struct sockaddr_in *)
266 ifap->ifa_dstaddr)->sin_addr))
268 dest = (struct sockaddr_in *) ifap->ifa_dstaddr;
269 dest_pnt = &dest->sin_addr;
270 flags = ZEBRA_IFA_PEER;
272 else if (ifap->ifa_broadaddr &&
273 !IPV4_ADDR_SAME(&addr->sin_addr,
274 &((struct sockaddr_in *)
275 ifap->ifa_broadaddr)->sin_addr))
277 dest = (struct sockaddr_in *) ifap->ifa_broadaddr;
278 dest_pnt = &dest->sin_addr;
281 connected_add_ipv4 (ifp, flags, &addr->sin_addr,
282 prefixlen, dest_pnt, NULL);
285 if (ifap->ifa_addr->sa_family == AF_INET6)
287 struct sockaddr_in6 *addr;
288 struct sockaddr_in6 *mask;
289 struct sockaddr_in6 *dest;
290 struct in6_addr *dest_pnt;
293 addr = (struct sockaddr_in6 *) ifap->ifa_addr;
294 mask = (struct sockaddr_in6 *) ifap->ifa_netmask;
295 prefixlen = ip6_masklen (mask->sin6_addr);
299 if (ifap->ifa_dstaddr &&
300 !IPV6_ADDR_SAME(&addr->sin6_addr,
301 &((struct sockaddr_in6 *)
302 ifap->ifa_dstaddr)->sin6_addr))
304 dest = (struct sockaddr_in6 *) ifap->ifa_dstaddr;
305 dest_pnt = &dest->sin6_addr;
306 flags = ZEBRA_IFA_PEER;
308 else if (ifap->ifa_broadaddr &&
309 !IPV6_ADDR_SAME(&addr->sin6_addr,
310 &((struct sockaddr_in6 *)
311 ifap->ifa_broadaddr)->sin6_addr))
313 dest = (struct sockaddr_in6 *) ifap->ifa_broadaddr;
314 dest_pnt = &dest->sin6_addr;
318 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
320 addr->sin6_scope_id =
321 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]);
322 addr->sin6_addr.s6_addr[2] = addr->sin6_addr.s6_addr[3] = 0;
326 connected_add_ipv6 (ifp, flags, &addr->sin6_addr, prefixlen,
329 #endif /* HAVE_IPV6 */
332 freeifaddrs (ifapfree);
336 #else /* HAVE_GETIFADDRS */
337 /* Interface address lookup by ioctl. This function only looks up
340 if_get_addr (struct interface *ifp)
344 struct sockaddr_in addr;
345 struct sockaddr_in mask;
346 struct sockaddr_in dest;
347 struct in_addr *dest_pnt;
351 /* Interface's name and address family. */
352 strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
353 ifreq.ifr_addr.sa_family = AF_INET;
355 /* Interface's address. */
356 ret = if_ioctl (SIOCGIFADDR, (caddr_t) &ifreq);
359 if (errno != EADDRNOTAVAIL)
361 zlog_warn ("SIOCGIFADDR fail: %s", safe_strerror (errno));
366 memcpy (&addr, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
368 /* Interface's network mask. */
369 ret = if_ioctl (SIOCGIFNETMASK, (caddr_t) &ifreq);
372 if (errno != EADDRNOTAVAIL)
374 zlog_warn ("SIOCGIFNETMASK fail: %s", safe_strerror (errno));
380 memcpy (&mask, &ifreq.ifr_netmask, sizeof (struct sockaddr_in));
382 memcpy (&mask, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
383 #endif /* ifr_netmask */
384 prefixlen = ip_masklen (mask.sin_addr);
386 /* Point to point or borad cast address pointer init. */
389 ret = if_ioctl (SIOCGIFDSTADDR, (caddr_t) &ifreq);
392 if (errno != EADDRNOTAVAIL)
393 zlog_warn ("SIOCGIFDSTADDR fail: %s", safe_strerror (errno));
395 else if (!IPV4_ADDR_SAME(&addr.sin_addr, &ifreq.ifr_dstaddr.sin_addr))
397 memcpy (&dest, &ifreq.ifr_dstaddr, sizeof (struct sockaddr_in));
398 dest_pnt = &dest.sin_addr;
399 flags = ZEBRA_IFA_PEER;
403 ret = if_ioctl (SIOCGIFBRDADDR, (caddr_t) &ifreq);
406 if (errno != EADDRNOTAVAIL)
407 zlog_warn ("SIOCGIFBRDADDR fail: %s", safe_strerror (errno));
409 else if (!IPV4_ADDR_SAME(&addr.sin_addr, &ifreq.ifr_broadaddr.sin_addr))
411 memcpy (&dest, &ifreq.ifr_broadaddr, sizeof (struct sockaddr_in));
412 dest_pnt = &dest.sin_addr;
417 /* Set address to the interface. */
418 connected_add_ipv4 (ifp, flags, &addr.sin_addr, prefixlen, dest_pnt, NULL);
422 #endif /* HAVE_GETIFADDRS */
424 /* Fetch interface information via ioctl(). */
426 interface_info_ioctl ()
428 struct listnode *node, *nnode;
429 struct interface *ifp;
431 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
436 #endif /* SIOCGIFHWADDR */
438 #ifndef HAVE_GETIFADDRS
440 #endif /* ! HAVE_GETIFADDRS */
446 /* Lookup all interface information. */
448 interface_list (struct zebra_vrf *zvrf)
450 if (zvrf->vrf_id != VRF_DEFAULT)
452 zlog_warn ("interface_list: ignore VRF %u", zvrf->vrf_id);
455 /* Linux can do both proc & ioctl, ioctl is the only way to get
456 interface aliases in 2.2 series kernels. */
457 #ifdef HAVE_PROC_NET_DEV
458 interface_list_proc ();
459 #endif /* HAVE_PROC_NET_DEV */
460 interface_list_ioctl ();
462 /* After listing is done, get index, address, flags and other
463 interface's information. */
464 interface_info_ioctl ();
466 #ifdef HAVE_GETIFADDRS
468 #endif /* HAVE_GETIFADDRS */
470 #if defined(HAVE_IPV6) && defined(HAVE_PROC_NET_IF_INET6)
471 /* Linux provides interface's IPv6 address via
472 /proc/net/if_inet6. */
474 #endif /* HAVE_IPV6 && HAVE_PROC_NET_IF_INET6 */