chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / sysv / linux / if_index.c
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.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #include <alloca.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <net/if.h>
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>
32
33 #include "netlinkaccess.h"
34
35
36 /* Variable to signal whether SIOCGIFCONF is not available.  */
37 # if __ASSUME_SIOCGIFNAME == 0
38 static int old_siocgifconf;
39 #else
40 # define old_siocgifconf 0
41 #endif
42
43
44 unsigned int
45 if_nametoindex (const char *ifname)
46 {
47 #ifndef SIOCGIFINDEX
48   __set_errno (ENOSYS);
49   return 0;
50 #else
51   struct ifreq ifr;
52   int fd = __opensock ();
53
54   if (fd < 0)
55     return 0;
56
57   strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
58   if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
59     {
60       int saved_errno = errno;
61       close_not_cancel_no_status (fd);
62       if (saved_errno == EINVAL)
63         __set_errno (ENOSYS);
64       return 0;
65     }
66   close_not_cancel_no_status (fd);
67   return ifr.ifr_ifindex;
68 #endif
69 }
70 libc_hidden_def (if_nametoindex)
71
72
73 void
74 if_freenameindex (struct if_nameindex *ifn)
75 {
76   struct if_nameindex *ptr = ifn;
77   while (ptr->if_name || ptr->if_index)
78     {
79       free (ptr->if_name);
80       ++ptr;
81     }
82   free (ifn);
83 }
84 libc_hidden_def (if_freenameindex)
85
86
87 #if __ASSUME_NETLINK_SUPPORT == 0
88 static struct if_nameindex *
89 if_nameindex_ioctl (void)
90 {
91   int fd = __opensock ();
92   struct ifconf ifc;
93   unsigned int nifs, i;
94   int rq_len;
95   struct if_nameindex *idx = NULL;
96 # define RQ_IFS 4
97
98   if (fd < 0)
99     return NULL;
100
101   ifc.ifc_buf = NULL;
102
103   /* We may be able to get the needed buffer size directly, rather than
104      guessing.  */
105   if (! old_siocgifconf)
106     {
107       ifc.ifc_buf = NULL;
108       ifc.ifc_len = 0;
109       if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
110         {
111 # if __ASSUME_SIOCGIFNAME == 0
112           old_siocgifconf = 1;
113 # endif
114           rq_len = RQ_IFS * sizeof (struct ifreq);
115         }
116       else
117         rq_len = ifc.ifc_len;
118     }
119   else
120     rq_len = RQ_IFS * sizeof (struct ifreq);
121
122   /* Read all the interfaces out of the kernel.  */
123   ifc.ifc_buf = alloca (rq_len);
124   ifc.ifc_len = rq_len;
125   while (1)
126     {
127         if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
128         {
129           close_not_cancel_no_status (fd);
130           return NULL;
131         }
132       if (ifc.ifc_len < rq_len || ! old_siocgifconf)
133         break;
134
135       ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
136       ifc.ifc_len = rq_len;
137     }
138
139   nifs = ifc.ifc_len / sizeof (struct ifreq);
140
141   idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
142   if (idx == NULL)
143     {
144       close_not_cancel_no_status (fd);
145       __set_errno (ENOBUFS);
146       return NULL;
147     }
148
149   for (i = 0; i < nifs; ++i)
150     {
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)
155         {
156           int saved_errno = errno;
157           unsigned int j;
158
159           for (j =  0; j < i; ++j)
160             free (idx[j].if_name);
161           free (idx);
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);
168           return NULL;
169         }
170       idx[i].if_index = ifr->ifr_ifindex;
171     }
172
173   idx[i].if_index = 0;
174   idx[i].if_name = NULL;
175
176   close_not_cancel_no_status (fd);
177   return idx;
178 }
179 #endif
180
181
182 static struct if_nameindex *
183 if_nameindex_netlink (void)
184 {
185   struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
186   struct if_nameindex *idx = NULL;
187
188   if (__no_netlink_support || __netlink_open (&nh) < 0)
189     return NULL;
190
191
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)
195     goto exit_free;
196
197   /* Count the interfaces.  */
198   unsigned int nifs = 0;
199   for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
200     {
201       struct nlmsghdr *nlh;
202       size_t size = nlp->size;
203
204       if (nlp->nlh == NULL)
205         continue;
206
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))
210         {
211           /* Check if the message is what we want.  */
212           if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
213             continue;
214
215           if (nlh->nlmsg_type == NLMSG_DONE)
216             break;              /* ok */
217
218           if (nlh->nlmsg_type == RTM_NEWLINK)
219             ++nifs;
220         }
221     }
222
223   idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
224   if (idx == NULL)
225     {
226     nomem:
227       __set_errno (ENOBUFS);
228       goto exit_free;
229     }
230
231   /* Add the interfaces.  */
232   nifs = 0;
233   for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
234     {
235       struct nlmsghdr *nlh;
236       size_t size = nlp->size;
237
238       if (nlp->nlh == NULL)
239         continue;
240
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))
244         {
245           /* Check if the message is what we want.  */
246           if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
247             continue;
248
249           if (nlh->nlmsg_type == NLMSG_DONE)
250             break;              /* ok */
251
252           if (nlh->nlmsg_type == RTM_NEWLINK)
253             {
254               struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
255               struct rtattr *rta = IFLA_RTA (ifim);
256               size_t rtasize = IFLA_PAYLOAD (nlh);
257
258               idx[nifs].if_index = ifim->ifi_index;
259
260               while (RTA_OK (rta, rtasize))
261                 {
262                   char *rta_data = RTA_DATA (rta);
263                   size_t rta_payload = RTA_PAYLOAD (rta);
264
265                   if (rta->rta_type == IFLA_IFNAME)
266                     {
267                       idx[nifs].if_name = __strndup (rta_data, rta_payload);
268                       if (idx[nifs].if_name == NULL)
269                         {
270                           idx[nifs].if_index = 0;
271                           if_freenameindex (idx);
272                           idx = NULL;
273                           goto nomem;
274                         }
275                       break;
276                     }
277
278                   rta = RTA_NEXT (rta, rtasize);
279                 }
280
281               ++nifs;
282             }
283         }
284     }
285
286   idx[nifs].if_index = 0;
287   idx[nifs].if_name = NULL;
288
289  exit_free:
290   __netlink_free_handle (&nh);
291   __netlink_close (&nh);
292
293   return idx;
294 }
295
296
297 struct if_nameindex *
298 if_nameindex (void)
299 {
300 #ifndef SIOCGIFINDEX
301   __set_errno (ENOSYS);
302   return NULL;
303 #else
304   struct if_nameindex *result = if_nameindex_netlink ();
305 # if __ASSUME_NETLINK_SUPPORT == 0
306   if (__no_netlink_support)
307     result = if_nameindex_ioctl ();
308 # endif
309   return result;
310 #endif
311 }
312 libc_hidden_def (if_nameindex)
313
314
315 char *
316 if_indextoname (unsigned int ifindex, char *ifname)
317 {
318 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
319   __set_errno (ENOSYS);
320   return NULL;
321 #else
322 # if __ASSUME_SIOCGIFNAME == 0
323   struct if_nameindex *idx;
324   struct if_nameindex *p;
325   char *result = NULL;
326 # endif
327
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.  */
331   struct ifreq ifr;
332   int fd;
333 #  if __ASSUME_SIOCGIFNAME == 0
334   static int siocgifname_works_not;
335
336   if (!siocgifname_works_not)
337 #  endif
338     {
339 #  if __ASSUME_SIOCGIFNAME == 0
340       int serrno = errno;
341 #  endif
342       int status;
343
344       fd = __opensock ();
345
346       if (fd < 0)
347         return NULL;
348
349       ifr.ifr_ifindex = ifindex;
350       status = __ioctl (fd, SIOCGIFNAME, &ifr);
351
352       close_not_cancel_no_status (fd);
353
354       if (status  < 0)
355         {
356 #  if __ASSUME_SIOCGIFNAME == 0
357           if (errno == EINVAL)
358             siocgifname_works_not = 1; /* Don't make the same mistake twice. */
359           else
360 #  endif
361             {
362               if (errno == ENODEV)
363                 /* POSIX requires ENXIO.  */
364                 __set_errno (ENXIO);
365
366               return NULL;
367             }
368         }
369       else
370         return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
371
372 #  if __ASSUME_SIOCGIFNAME == 0
373       __set_errno (serrno);
374 #  endif
375     }
376 # endif
377
378 # if __ASSUME_SIOCGIFNAME == 0
379   idx = if_nameindex ();
380
381   if (idx != NULL)
382     {
383       for (p = idx; p->if_index || p->if_name; ++p)
384         if (p->if_index == ifindex)
385           {
386             result = strncpy (ifname, p->if_name, IFNAMSIZ);
387             break;
388           }
389
390       if_freenameindex (idx);
391
392       if (result == NULL)
393         __set_errno (ENXIO);
394     }
395   return result;
396 # endif
397 #endif
398 }
399 libc_hidden_def (if_indextoname)
400
401
402 #if 0
403 void
404 internal_function
405 __protocol_available (int *have_inet, int *have_inet6)
406 {
407   int fd = __opensock ();
408   unsigned int nifs;
409   int rq_len;
410   struct ifconf ifc;
411 # define RQ_IFS 4
412
413   /* Wirst case assumption.  */
414   *have_inet = 0;
415   *have_inet6 = 0;
416
417   if (fd < 0)
418     /* We cannot open the socket.  No networking at all?  */
419     return;
420
421   /* We may be able to get the needed buffer size directly, rather than
422      guessing.  */
423   if (! old_siocgifconf)
424     {
425       ifc.ifc_buf = NULL;
426       ifc.ifc_len = 0;
427       if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
428         {
429 # if __ASSUME_SIOCGIFNAME == 0
430           old_siocgifconf = 1;
431 # endif
432           rq_len = RQ_IFS * sizeof (struct ifreq);
433         }
434       else
435         rq_len = ifc.ifc_len;
436     }
437   else
438     rq_len = RQ_IFS * sizeof (struct ifreq);
439
440   /* Read all the interfaces out of the kernel.  */
441   do
442     {
443       ifc.ifc_buf = alloca (ifc.ifc_len = rq_len);
444       if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
445         {
446           close_not_cancel_no_status (fd);
447           return;
448         }
449       rq_len *= 2;
450     }
451   while (ifc.ifc_len == rq_len && old_siocgifconf);
452
453   nifs = ifc.ifc_len / sizeof (struct ifreq);
454
455   /* Go through all the interfaces and get the address.  */
456   while (nifs-- > 0)
457     if (__ioctl (fd, SIOCGIFADDR, &ifc.ifc_req[nifs]) >= 0)
458       {
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)
462           *have_inet = 1;
463         else if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET6)
464           *have_inet6 = 1;
465
466         /* Note, this is & not &&.  It works since the values are always
467            0 or 1.  */
468         if (*have_inet & *have_inet6)
469           /* We can stop early.  */
470           break;
471       }
472
473   close_not_cancel_no_status (fd);
474 }
475 #endif