1 /* Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005
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>
32 #include "netlinkaccess.h"
35 /* Variable to signal whether SIOCGIFCONF is not available. */
36 # if __ASSUME_SIOCGIFNAME == 0
37 static int old_siocgifconf
;
39 # define old_siocgifconf 0
44 if_nametoindex (const char *ifname
)
51 int fd
= __opensock ();
56 strncpy (ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
57 if (__ioctl (fd
, SIOCGIFINDEX
, &ifr
) < 0)
59 int saved_errno
= errno
;
60 close_not_cancel_no_status (fd
);
61 if (saved_errno
== EINVAL
)
65 close_not_cancel_no_status (fd
);
66 return ifr
.ifr_ifindex
;
69 libc_hidden_def (if_nametoindex
)
73 if_freenameindex (struct if_nameindex
*ifn
)
75 struct if_nameindex
*ptr
= ifn
;
76 while (ptr
->if_name
|| ptr
->if_index
)
83 libc_hidden_def (if_freenameindex
)
86 #if __ASSUME_NETLINK_SUPPORT == 0
87 static struct if_nameindex
*
88 if_nameindex_ioctl (void)
90 int fd
= __opensock ();
94 struct if_nameindex
*idx
= NULL
;
102 /* We may be able to get the needed buffer size directly, rather than
104 if (! old_siocgifconf
)
108 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0 || ifc
.ifc_len
== 0)
110 # if __ASSUME_SIOCGIFNAME == 0
113 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
116 rq_len
= ifc
.ifc_len
;
119 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
121 /* Read all the interfaces out of the kernel. */
122 ifc
.ifc_buf
= alloca (rq_len
);
123 ifc
.ifc_len
= rq_len
;
126 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0)
128 close_not_cancel_no_status (fd
);
131 if (ifc
.ifc_len
< rq_len
|| ! old_siocgifconf
)
134 ifc
.ifc_buf
= extend_alloca (ifc
.ifc_buf
, rq_len
, 2 * rq_len
);
135 ifc
.ifc_len
= rq_len
;
138 nifs
= ifc
.ifc_len
/ sizeof (struct ifreq
);
140 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
143 close_not_cancel_no_status (fd
);
144 __set_errno (ENOBUFS
);
148 for (i
= 0; i
< nifs
; ++i
)
150 struct ifreq
*ifr
= &ifc
.ifc_req
[i
];
151 idx
[i
].if_name
= __strdup (ifr
->ifr_name
);
152 if (idx
[i
].if_name
== NULL
153 || __ioctl (fd
, SIOCGIFINDEX
, ifr
) < 0)
155 int saved_errno
= errno
;
158 for (j
= 0; j
< i
; ++j
)
159 free (idx
[j
].if_name
);
161 close_not_cancel_no_status (fd
);
162 if (saved_errno
== EINVAL
)
163 saved_errno
= ENOSYS
;
164 else if (saved_errno
== ENOMEM
)
165 saved_errno
= ENOBUFS
;
166 __set_errno (saved_errno
);
169 idx
[i
].if_index
= ifr
->ifr_ifindex
;
173 idx
[i
].if_name
= NULL
;
175 close_not_cancel_no_status (fd
);
181 static struct if_nameindex
*
182 if_nameindex_netlink (void)
184 struct netlink_handle nh
= { 0, 0, 0, NULL
, NULL
};
185 struct if_nameindex
*idx
= NULL
;
187 if (__no_netlink_support
|| __netlink_open (&nh
) < 0)
191 /* Tell the kernel that we wish to get a list of all
192 active interfaces. Collect all data for every interface. */
193 if (__netlink_request (&nh
, RTM_GETLINK
) < 0)
196 /* Count the interfaces. */
197 unsigned int nifs
= 0;
198 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
200 struct nlmsghdr
*nlh
;
201 size_t size
= nlp
->size
;
203 if (nlp
->nlh
== NULL
)
206 /* Walk through all entries we got from the kernel and look, which
207 message type they contain. */
208 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
210 /* Check if the message is what we want. */
211 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
214 if (nlh
->nlmsg_type
== NLMSG_DONE
)
217 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
222 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
226 __set_errno (ENOBUFS
);
230 /* Add the interfaces. */
232 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
234 struct nlmsghdr
*nlh
;
235 size_t size
= nlp
->size
;
237 if (nlp
->nlh
== NULL
)
240 /* Walk through all entries we got from the kernel and look, which
241 message type they contain. */
242 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
244 /* Check if the message is what we want. */
245 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
248 if (nlh
->nlmsg_type
== NLMSG_DONE
)
251 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
253 struct ifinfomsg
*ifim
= (struct ifinfomsg
*) NLMSG_DATA (nlh
);
254 struct rtattr
*rta
= IFLA_RTA (ifim
);
255 size_t rtasize
= IFLA_PAYLOAD (nlh
);
257 idx
[nifs
].if_index
= ifim
->ifi_index
;
259 while (RTA_OK (rta
, rtasize
))
261 char *rta_data
= RTA_DATA (rta
);
262 size_t rta_payload
= RTA_PAYLOAD (rta
);
264 if (rta
->rta_type
== IFLA_IFNAME
)
266 idx
[nifs
].if_name
= __strndup (rta_data
, rta_payload
);
267 if (idx
[nifs
].if_name
== NULL
)
269 idx
[nifs
].if_index
= 0;
270 if_freenameindex (idx
);
277 rta
= RTA_NEXT (rta
, rtasize
);
285 idx
[nifs
].if_index
= 0;
286 idx
[nifs
].if_name
= NULL
;
289 __netlink_free_handle (&nh
);
290 __netlink_close (&nh
);
296 struct if_nameindex
*
300 __set_errno (ENOSYS
);
303 struct if_nameindex
*result
= if_nameindex_netlink ();
304 # if __ASSUME_NETLINK_SUPPORT == 0
305 if (__no_netlink_support
)
306 result
= if_nameindex_ioctl ();
311 libc_hidden_def (if_nameindex
)
315 if_indextoname (unsigned int ifindex
, char *ifname
)
317 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
318 __set_errno (ENOSYS
);
321 # if __ASSUME_SIOCGIFNAME == 0
322 struct if_nameindex
*idx
;
323 struct if_nameindex
*p
;
327 # if defined SIOCGIFNAME || __ASSUME_SIOCGIFNAME > 0
328 /* We may be able to do the conversion directly, rather than searching a
329 list. This ioctl is not present in kernels before version 2.1.50. */
332 # if __ASSUME_SIOCGIFNAME == 0
333 static int siocgifname_works_not
;
335 if (!siocgifname_works_not
)
338 # if __ASSUME_SIOCGIFNAME == 0
348 ifr
.ifr_ifindex
= ifindex
;
349 status
= __ioctl (fd
, SIOCGIFNAME
, &ifr
);
351 close_not_cancel_no_status (fd
);
355 # if __ASSUME_SIOCGIFNAME == 0
357 siocgifname_works_not
= 1; /* Don't make the same mistake twice. */
362 /* POSIX requires ENXIO. */
369 return strncpy (ifname
, ifr
.ifr_name
, IFNAMSIZ
);
371 # if __ASSUME_SIOCGIFNAME == 0
372 __set_errno (serrno
);
377 # if __ASSUME_SIOCGIFNAME == 0
378 idx
= if_nameindex ();
382 for (p
= idx
; p
->if_index
|| p
->if_name
; ++p
)
383 if (p
->if_index
== ifindex
)
385 result
= strncpy (ifname
, p
->if_name
, IFNAMSIZ
);
389 if_freenameindex (idx
);
398 libc_hidden_def (if_indextoname
)
404 __protocol_available (int *have_inet
, int *have_inet6
)
406 int fd
= __opensock ();
412 /* Wirst case assumption. */
417 /* We cannot open the socket. No networking at all? */
420 /* We may be able to get the needed buffer size directly, rather than
422 if (! old_siocgifconf
)
426 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0 || ifc
.ifc_len
== 0)
428 # if __ASSUME_SIOCGIFNAME == 0
431 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
434 rq_len
= ifc
.ifc_len
;
437 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
439 /* Read all the interfaces out of the kernel. */
442 ifc
.ifc_buf
= alloca (ifc
.ifc_len
= rq_len
);
443 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0)
445 close_not_cancel_no_status (fd
);
450 while (ifc
.ifc_len
== rq_len
&& old_siocgifconf
);
452 nifs
= ifc
.ifc_len
/ sizeof (struct ifreq
);
454 /* Go through all the interfaces and get the address. */
456 if (__ioctl (fd
, SIOCGIFADDR
, &ifc
.ifc_req
[nifs
]) >= 0)
458 /* We successfully got information about this interface. Now
459 test whether it is an IPv4 or IPv6 address. */
460 if (ifc
.ifc_req
[nifs
].ifr_addr
.sa_family
== AF_INET
)
462 else if (ifc
.ifc_req
[nifs
].ifr_addr
.sa_family
== AF_INET6
)
465 /* Note, this is & not &&. It works since the values are always
467 if (*have_inet
& *have_inet6
)
468 /* We can stop early. */
472 close_not_cancel_no_status (fd
);