1 /* Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <bits/libc-lock.h>
30 #include <not-cancel.h>
31 #include <kernel-features.h>
33 #include "netlinkaccess.h"
36 /* Variable to signal whether SIOCGIFCONF is not available. */
37 # if __ASSUME_SIOCGIFNAME == 0
38 static int old_siocgifconf;
40 # define old_siocgifconf 0
45 if_nametoindex (const char *ifname)
52 int fd = __opensock ();
57 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
58 if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
60 int saved_errno = errno;
61 close_not_cancel_no_status (fd);
62 if (saved_errno == EINVAL)
66 close_not_cancel_no_status (fd);
67 return ifr.ifr_ifindex;
70 libc_hidden_def (if_nametoindex)
74 if_freenameindex (struct if_nameindex *ifn)
76 struct if_nameindex *ptr = ifn;
77 while (ptr->if_name || ptr->if_index)
84 libc_hidden_def (if_freenameindex)
87 #if __ASSUME_NETLINK_SUPPORT == 0
88 static struct if_nameindex *
89 if_nameindex_ioctl (void)
91 int fd = __opensock ();
95 struct if_nameindex *idx = NULL;
103 /* We may be able to get the needed buffer size directly, rather than
105 if (! old_siocgifconf)
109 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
111 # if __ASSUME_SIOCGIFNAME == 0
114 rq_len = RQ_IFS * sizeof (struct ifreq);
117 rq_len = ifc.ifc_len;
120 rq_len = RQ_IFS * sizeof (struct ifreq);
122 /* Read all the interfaces out of the kernel. */
123 ifc.ifc_buf = alloca (rq_len);
124 ifc.ifc_len = rq_len;
127 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
129 close_not_cancel_no_status (fd);
132 if (ifc.ifc_len < rq_len || ! old_siocgifconf)
135 ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
136 ifc.ifc_len = rq_len;
139 nifs = ifc.ifc_len / sizeof (struct ifreq);
141 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
144 close_not_cancel_no_status (fd);
145 __set_errno (ENOBUFS);
149 for (i = 0; i < nifs; ++i)
151 struct ifreq *ifr = &ifc.ifc_req[i];
152 idx[i].if_name = __strdup (ifr->ifr_name);
153 if (idx[i].if_name == NULL
154 || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
156 int saved_errno = errno;
159 for (j = 0; j < i; ++j)
160 free (idx[j].if_name);
162 close_not_cancel_no_status (fd);
163 if (saved_errno == EINVAL)
164 saved_errno = ENOSYS;
165 else if (saved_errno == ENOMEM)
166 saved_errno = ENOBUFS;
167 __set_errno (saved_errno);
170 idx[i].if_index = ifr->ifr_ifindex;
174 idx[i].if_name = NULL;
176 close_not_cancel_no_status (fd);
182 static struct if_nameindex *
183 if_nameindex_netlink (void)
185 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
186 struct if_nameindex *idx = NULL;
188 if (__no_netlink_support || __netlink_open (&nh) < 0)
192 /* Tell the kernel that we wish to get a list of all
193 active interfaces. Collect all data for every interface. */
194 if (__netlink_request (&nh, RTM_GETLINK) < 0)
197 /* Count the interfaces. */
198 unsigned int nifs = 0;
199 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
201 struct nlmsghdr *nlh;
202 size_t size = nlp->size;
204 if (nlp->nlh == NULL)
207 /* Walk through all entries we got from the kernel and look, which
208 message type they contain. */
209 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
211 /* Check if the message is what we want. */
212 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
215 if (nlh->nlmsg_type == NLMSG_DONE)
218 if (nlh->nlmsg_type == RTM_NEWLINK)
223 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
227 __set_errno (ENOBUFS);
231 /* Add the interfaces. */
233 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
235 struct nlmsghdr *nlh;
236 size_t size = nlp->size;
238 if (nlp->nlh == NULL)
241 /* Walk through all entries we got from the kernel and look, which
242 message type they contain. */
243 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
245 /* Check if the message is what we want. */
246 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
249 if (nlh->nlmsg_type == NLMSG_DONE)
252 if (nlh->nlmsg_type == RTM_NEWLINK)
254 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
255 struct rtattr *rta = IFLA_RTA (ifim);
256 size_t rtasize = IFLA_PAYLOAD (nlh);
258 idx[nifs].if_index = ifim->ifi_index;
260 while (RTA_OK (rta, rtasize))
262 char *rta_data = RTA_DATA (rta);
263 size_t rta_payload = RTA_PAYLOAD (rta);
265 if (rta->rta_type == IFLA_IFNAME)
267 idx[nifs].if_name = __strndup (rta_data, rta_payload);
268 if (idx[nifs].if_name == NULL)
270 idx[nifs].if_index = 0;
271 if_freenameindex (idx);
278 rta = RTA_NEXT (rta, rtasize);
286 idx[nifs].if_index = 0;
287 idx[nifs].if_name = NULL;
290 __netlink_free_handle (&nh);
291 __netlink_close (&nh);
297 struct if_nameindex *
301 __set_errno (ENOSYS);
304 struct if_nameindex *result = if_nameindex_netlink ();
305 # if __ASSUME_NETLINK_SUPPORT == 0
306 if (__no_netlink_support)
307 result = if_nameindex_ioctl ();
312 libc_hidden_def (if_nameindex)
316 if_indextoname (unsigned int ifindex, char *ifname)
318 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
319 __set_errno (ENOSYS);
322 # if __ASSUME_SIOCGIFNAME == 0
323 struct if_nameindex *idx;
324 struct if_nameindex *p;
328 # if defined SIOCGIFNAME || __ASSUME_SIOCGIFNAME > 0
329 /* We may be able to do the conversion directly, rather than searching a
330 list. This ioctl is not present in kernels before version 2.1.50. */
333 # if __ASSUME_SIOCGIFNAME == 0
334 static int siocgifname_works_not;
336 if (!siocgifname_works_not)
339 # if __ASSUME_SIOCGIFNAME == 0
349 ifr.ifr_ifindex = ifindex;
350 status = __ioctl (fd, SIOCGIFNAME, &ifr);
352 close_not_cancel_no_status (fd);
356 # if __ASSUME_SIOCGIFNAME == 0
358 siocgifname_works_not = 1; /* Don't make the same mistake twice. */
363 /* POSIX requires ENXIO. */
370 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
372 # if __ASSUME_SIOCGIFNAME == 0
373 __set_errno (serrno);
378 # if __ASSUME_SIOCGIFNAME == 0
379 idx = if_nameindex ();
383 for (p = idx; p->if_index || p->if_name; ++p)
384 if (p->if_index == ifindex)
386 result = strncpy (ifname, p->if_name, IFNAMSIZ);
390 if_freenameindex (idx);
399 libc_hidden_def (if_indextoname)
405 __protocol_available (int *have_inet, int *have_inet6)
407 int fd = __opensock ();
413 /* Wirst case assumption. */
418 /* We cannot open the socket. No networking at all? */
421 /* We may be able to get the needed buffer size directly, rather than
423 if (! old_siocgifconf)
427 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
429 # if __ASSUME_SIOCGIFNAME == 0
432 rq_len = RQ_IFS * sizeof (struct ifreq);
435 rq_len = ifc.ifc_len;
438 rq_len = RQ_IFS * sizeof (struct ifreq);
440 /* Read all the interfaces out of the kernel. */
443 ifc.ifc_buf = alloca (ifc.ifc_len = rq_len);
444 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
446 close_not_cancel_no_status (fd);
451 while (ifc.ifc_len == rq_len && old_siocgifconf);
453 nifs = ifc.ifc_len / sizeof (struct ifreq);
455 /* Go through all the interfaces and get the address. */
457 if (__ioctl (fd, SIOCGIFADDR, &ifc.ifc_req[nifs]) >= 0)
459 /* We successfully got information about this interface. Now
460 test whether it is an IPv4 or IPv6 address. */
461 if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET)
463 else if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET6)
466 /* Note, this is & not &&. It works since the values are always
468 if (*have_inet & *have_inet6)
469 /* We can stop early. */
473 close_not_cancel_no_status (fd);