1 /* Copyright (C) 1997,98,99,2000,2002,2003,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <bits/libc-lock.h>
29 #include <not-cancel.h>
31 #include "netlinkaccess.h"
34 /* Variable to signal whether SIOCGIFCONF is not available. */
35 # if __ASSUME_SIOCGIFNAME == 0
36 static int old_siocgifconf
;
38 # define old_siocgifconf 0
43 if_nametoindex (const char *ifname
)
50 int fd
= __opensock ();
55 strncpy (ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
56 if (__ioctl (fd
, SIOCGIFINDEX
, &ifr
) < 0)
58 int saved_errno
= errno
;
59 close_not_cancel_no_status (fd
);
60 if (saved_errno
== EINVAL
)
64 close_not_cancel_no_status (fd
);
65 return ifr
.ifr_ifindex
;
68 libc_hidden_def (if_nametoindex
)
72 if_freenameindex (struct if_nameindex
*ifn
)
74 struct if_nameindex
*ptr
= ifn
;
75 while (ptr
->if_name
|| ptr
->if_index
)
82 libc_hidden_def (if_freenameindex
)
85 #if __ASSUME_NETLINK_SUPPORT == 0
86 static struct if_nameindex
*
87 if_nameindex_ioctl (void)
89 int fd
= __opensock ();
93 struct if_nameindex
*idx
= NULL
;
101 /* We may be able to get the needed buffer size directly, rather than
103 if (! old_siocgifconf
)
107 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0 || ifc
.ifc_len
== 0)
109 # if __ASSUME_SIOCGIFNAME == 0
112 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
115 rq_len
= ifc
.ifc_len
;
118 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
120 /* Read all the interfaces out of the kernel. */
121 ifc
.ifc_buf
= alloca (rq_len
);
122 ifc
.ifc_len
= rq_len
;
125 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0)
127 close_not_cancel_no_status (fd
);
130 if (ifc
.ifc_len
< rq_len
|| ! old_siocgifconf
)
133 ifc
.ifc_buf
= extend_alloca (ifc
.ifc_buf
, rq_len
, 2 * rq_len
);
134 ifc
.ifc_len
= rq_len
;
137 nifs
= ifc
.ifc_len
/ sizeof (struct ifreq
);
139 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
142 close_not_cancel_no_status (fd
);
143 __set_errno (ENOBUFS
);
147 for (i
= 0; i
< nifs
; ++i
)
149 struct ifreq
*ifr
= &ifc
.ifc_req
[i
];
150 idx
[i
].if_name
= __strdup (ifr
->ifr_name
);
151 if (idx
[i
].if_name
== NULL
152 || __ioctl (fd
, SIOCGIFINDEX
, ifr
) < 0)
154 int saved_errno
= errno
;
157 for (j
= 0; j
< i
; ++j
)
158 free (idx
[j
].if_name
);
160 close_not_cancel_no_status (fd
);
161 if (saved_errno
== EINVAL
)
162 saved_errno
= ENOSYS
;
163 else if (saved_errno
== ENOMEM
)
164 saved_errno
= ENOBUFS
;
165 __set_errno (saved_errno
);
168 idx
[i
].if_index
= ifr
->ifr_ifindex
;
172 idx
[i
].if_name
= NULL
;
174 close_not_cancel_no_status (fd
);
180 static struct if_nameindex
*
181 if_nameindex_netlink (void)
183 struct netlink_handle nh
= { 0, 0, 0, NULL
, NULL
};
184 struct if_nameindex
*idx
= NULL
;
186 if (__no_netlink_support
|| __netlink_open (&nh
) < 0)
190 /* Tell the kernel that we wish to get a list of all
191 active interfaces. */
192 if (__netlink_sendreq (&nh
, RTM_GETLINK
) < 0)
195 /* Collect all data for every interface. */
196 if (__netlink_receive (&nh
) < 0)
199 /* Count the interfaces. */
200 unsigned int nifs
= 0;
201 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
203 struct nlmsghdr
*nlh
;
204 size_t size
= nlp
->size
;
206 if (nlp
->nlh
== NULL
)
209 /* Walk through all entries we got from the kernel and look, which
210 message type they contain. */
211 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
213 /* Check if the message is what we want. */
214 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
217 if (nlh
->nlmsg_type
== NLMSG_DONE
)
220 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
225 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
229 __set_errno (ENOBUFS
);
233 /* Add the interfaces. */
235 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
237 struct nlmsghdr
*nlh
;
238 size_t size
= nlp
->size
;
240 if (nlp
->nlh
== NULL
)
243 /* Walk through all entries we got from the kernel and look, which
244 message type they contain. */
245 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
247 /* Check if the message is what we want. */
248 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
251 if (nlh
->nlmsg_type
== NLMSG_DONE
)
254 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
256 struct ifinfomsg
*ifim
= (struct ifinfomsg
*) NLMSG_DATA (nlh
);
257 struct rtattr
*rta
= IFLA_RTA (ifim
);
258 size_t rtasize
= IFLA_PAYLOAD (nlh
);
260 idx
[nifs
].if_index
= ifim
->ifi_index
;
262 while (RTA_OK (rta
, rtasize
))
264 char *rta_data
= RTA_DATA (rta
);
265 size_t rta_payload
= RTA_PAYLOAD (rta
);
267 if (rta
->rta_type
== IFLA_IFNAME
)
269 idx
[nifs
].if_name
= __strndup (rta_data
, rta_payload
);
270 if (idx
[nifs
].if_name
== NULL
)
272 idx
[nifs
].if_index
= 0;
273 if_freenameindex (idx
);
280 rta
= RTA_NEXT (rta
, rtasize
);
288 idx
[nifs
].if_index
= 0;
289 idx
[nifs
].if_name
= NULL
;
292 __netlink_free_handle (&nh
);
294 __netlink_close (&nh
);
300 struct if_nameindex
*
304 __set_errno (ENOSYS
);
307 struct if_nameindex
*result
= if_nameindex_netlink ();
308 # if __ASSUME_NETLINK_SUPPORT == 0
309 if (__no_netlink_support
)
310 result
= if_nameindex_ioctl ();
315 libc_hidden_def (if_nameindex
)
319 if_indextoname (unsigned int ifindex
, char *ifname
)
321 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
322 __set_errno (ENOSYS
);
325 # if __ASSUME_SIOCGIFNAME == 0
326 struct if_nameindex
*idx
;
327 struct if_nameindex
*p
;
331 # if defined SIOCGIFNAME || __ASSUME_SIOCGIFNAME > 0
332 /* We may be able to do the conversion directly, rather than searching a
333 list. This ioctl is not present in kernels before version 2.1.50. */
336 # if __ASSUME_SIOCGIFNAME == 0
337 static int siocgifname_works_not
;
339 if (!siocgifname_works_not
)
342 # if __ASSUME_SIOCGIFNAME == 0
352 ifr
.ifr_ifindex
= ifindex
;
353 status
= __ioctl (fd
, SIOCGIFNAME
, &ifr
);
355 close_not_cancel_no_status (fd
);
359 # if __ASSUME_SIOCGIFNAME == 0
361 siocgifname_works_not
= 1; /* Don't make the same mistake twice. */
366 /* POSIX requires ENXIO. */
373 return strncpy (ifname
, ifr
.ifr_name
, IFNAMSIZ
);
375 # if __ASSUME_SIOCGIFNAME == 0
376 __set_errno (serrno
);
381 # if __ASSUME_SIOCGIFNAME == 0
382 idx
= if_nameindex ();
386 for (p
= idx
; p
->if_index
|| p
->if_name
; ++p
)
387 if (p
->if_index
== ifindex
)
389 result
= strncpy (ifname
, p
->if_name
, IFNAMSIZ
);
393 if_freenameindex (idx
);
402 libc_hidden_def (if_indextoname
)
408 __protocol_available (int *have_inet
, int *have_inet6
)
410 int fd
= __opensock ();
416 /* Wirst case assumption. */
421 /* We cannot open the socket. No networking at all? */
424 /* We may be able to get the needed buffer size directly, rather than
426 if (! old_siocgifconf
)
430 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0 || ifc
.ifc_len
== 0)
432 # if __ASSUME_SIOCGIFNAME == 0
435 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
438 rq_len
= ifc
.ifc_len
;
441 rq_len
= RQ_IFS
* sizeof (struct ifreq
);
443 /* Read all the interfaces out of the kernel. */
446 ifc
.ifc_buf
= alloca (ifc
.ifc_len
= rq_len
);
447 if (__ioctl (fd
, SIOCGIFCONF
, &ifc
) < 0)
449 close_not_cancel_no_status (fd
);
454 while (ifc
.ifc_len
== rq_len
&& old_siocgifconf
);
456 nifs
= ifc
.ifc_len
/ sizeof (struct ifreq
);
458 /* Go through all the interfaces and get the address. */
460 if (__ioctl (fd
, SIOCGIFADDR
, &ifc
.ifc_req
[nifs
]) >= 0)
462 /* We successfully got information about this interface. Now
463 test whether it is an IPv4 or IPv6 address. */
464 if (ifc
.ifc_req
[nifs
].ifr_addr
.sa_family
== AF_INET
)
466 else if (ifc
.ifc_req
[nifs
].ifr_addr
.sa_family
== AF_INET6
)
469 /* Note, this is & not &&. It works since the values are always
471 if (*have_inet
& *have_inet6
)
472 /* We can stop early. */
476 close_not_cancel_no_status (fd
);