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
20 Reworked Dec 2002 by Erik Andersen <andersen@codepoet.org>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <libc-internal.h>
36 #include "netlinkaccess.h"
38 /* Experimentally off - libc_hidden_proto(strncpy) */
39 /* Experimentally off - libc_hidden_proto(strdup) */
40 libc_hidden_proto(ioctl)
41 libc_hidden_proto(close)
42 #if __ASSUME_NETLINK_SUPPORT
43 /* Experimentally off - libc_hidden_proto(strndup) */
46 extern int __opensock(void) attribute_hidden;
48 libc_hidden_proto(if_nametoindex)
50 if_nametoindex(const char* ifname)
57 int fd = __opensock();
62 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
63 if (ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
65 int saved_errno = errno;
67 if (saved_errno == EINVAL)
73 return ifr.ifr_ifindex;
76 libc_hidden_def(if_nametoindex)
78 libc_hidden_proto(if_freenameindex)
80 if_freenameindex (struct if_nameindex *ifn)
82 struct if_nameindex *ptr = ifn;
83 while (ptr->if_name || ptr->if_index)
90 libc_hidden_def(if_freenameindex)
92 libc_hidden_proto(if_nameindex)
93 #if !__ASSUME_NETLINK_SUPPORT
101 int fd = __opensock ();
103 unsigned int nifs, i;
105 struct if_nameindex *idx = NULL;
113 /* Guess on the correct buffer size... */
114 rq_len = RQ_IFS * sizeof (struct ifreq);
116 /* Read all the interfaces out of the kernel. */
117 /* Note: alloca's in this loop are diff from glibc because it's smaller */
120 ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
121 ifc.ifc_len = rq_len;
123 if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
129 while (ifc.ifc_len == rq_len);
131 nifs = ifc.ifc_len / sizeof(struct ifreq);
133 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
137 __set_errno(ENOBUFS);
141 for (i = 0; i < nifs; ++i)
143 struct ifreq *ifr = &ifc.ifc_req[i];
144 idx[i].if_name = strdup (ifr->ifr_name);
145 if (idx[i].if_name == NULL
146 || ioctl (fd, SIOCGIFINDEX, ifr) < 0)
148 int saved_errno = errno;
151 for (j = 0; j < i; ++j)
152 free (idx[j].if_name);
155 if (saved_errno == EINVAL)
156 saved_errno = ENOSYS;
157 else if (saved_errno == ENOMEM)
158 saved_errno = ENOBUFS;
159 __set_errno (saved_errno);
162 idx[i].if_index = ifr->ifr_ifindex;
166 idx[i].if_name = NULL;
173 struct if_nameindex *
176 unsigned int nifs = 0;
177 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
178 struct if_nameindex *idx = NULL;
179 struct netlink_res *nlp;
181 if (__netlink_open (&nh) < 0)
185 /* Tell the kernel that we wish to get a list of all
186 active interfaces. Collect all data for every interface. */
187 if (__netlink_request (&nh, RTM_GETLINK) < 0)
190 /* Count the interfaces. */
191 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
193 struct nlmsghdr *nlh;
194 size_t size = nlp->size;
196 if (nlp->nlh == NULL)
199 /* Walk through all entries we got from the kernel and look, which
200 message type they contain. */
201 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
203 /* Check if the message is what we want. */
204 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
207 if (nlh->nlmsg_type == NLMSG_DONE)
210 if (nlh->nlmsg_type == RTM_NEWLINK)
215 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
219 __set_errno (ENOBUFS);
223 /* Add the interfaces. */
225 for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
227 struct nlmsghdr *nlh;
228 size_t size = nlp->size;
230 if (nlp->nlh == NULL)
233 /* Walk through all entries we got from the kernel and look, which
234 message type they contain. */
235 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
237 /* Check if the message is what we want. */
238 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
241 if (nlh->nlmsg_type == NLMSG_DONE)
244 if (nlh->nlmsg_type == RTM_NEWLINK)
246 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
247 struct rtattr *rta = IFLA_RTA (ifim);
248 size_t rtasize = IFLA_PAYLOAD (nlh);
250 idx[nifs].if_index = ifim->ifi_index;
252 while (RTA_OK (rta, rtasize))
254 char *rta_data = RTA_DATA (rta);
255 size_t rta_payload = RTA_PAYLOAD (rta);
257 if (rta->rta_type == IFLA_IFNAME)
259 idx[nifs].if_name = strndup (rta_data, rta_payload);
260 if (idx[nifs].if_name == NULL)
262 idx[nifs].if_index = 0;
263 if_freenameindex (idx);
270 rta = RTA_NEXT (rta, rtasize);
278 idx[nifs].if_index = 0;
279 idx[nifs].if_name = NULL;
282 __netlink_free_handle (&nh);
283 __netlink_close (&nh);
288 libc_hidden_def(if_nameindex)
291 if_indextoname (unsigned int ifindex, char *ifname)
293 #if !defined SIOCGIFINDEX
294 __set_errno (ENOSYS);
298 /* Use ioctl to avoid searching the list. */
307 ifr.ifr_ifindex = ifindex;
308 if (ioctl (fd, SIOCGIFNAME, &ifr) < 0)
312 if (serrno == ENODEV)
313 /* POSIX requires ENXIO. */
315 __set_errno (serrno);
320 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
322 struct if_nameindex *idx;
323 struct if_nameindex *p;
326 idx = if_nameindex();
330 for (p = idx; p->if_index || p->if_name; ++p)
331 if (p->if_index == ifindex)
333 result = strncpy (ifname, p->if_name, IFNAMSIZ);
337 if_freenameindex (idx);