1 /* Copyright (C) 1997-2014 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, see
16 <http://www.gnu.org/licenses/>. */
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <bits/libc-lock.h>
28 #include <not-cancel.h>
29 #include <kernel-features.h>
31 #include "netlinkaccess.h"
35 if_nametoindex (const char *ifname
)
42 int fd
= __opensock ();
47 strncpy (ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
48 if (__ioctl (fd
, SIOCGIFINDEX
, &ifr
) < 0)
50 int saved_errno
= errno
;
51 close_not_cancel_no_status (fd
);
52 if (saved_errno
== EINVAL
)
56 close_not_cancel_no_status (fd
);
57 return ifr
.ifr_ifindex
;
60 libc_hidden_def (if_nametoindex
)
64 if_freenameindex (struct if_nameindex
*ifn
)
66 struct if_nameindex
*ptr
= ifn
;
67 while (ptr
->if_name
|| ptr
->if_index
)
74 libc_hidden_def (if_freenameindex
)
77 static struct if_nameindex
*
78 if_nameindex_netlink (void)
80 struct netlink_handle nh
= { 0, 0, 0, NULL
, NULL
};
81 struct if_nameindex
*idx
= NULL
;
83 if (__netlink_open (&nh
) < 0)
87 /* Tell the kernel that we wish to get a list of all
88 active interfaces. Collect all data for every interface. */
89 if (__netlink_request (&nh
, RTM_GETLINK
) < 0)
92 /* Count the interfaces. */
93 unsigned int nifs
= 0;
94 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
97 size_t size
= nlp
->size
;
102 /* Walk through all entries we got from the kernel and look, which
103 message type they contain. */
104 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
106 /* Check if the message is what we want. */
107 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
110 if (nlh
->nlmsg_type
== NLMSG_DONE
)
113 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
118 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
122 __set_errno (ENOBUFS
);
126 /* Add the interfaces. */
128 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
130 struct nlmsghdr
*nlh
;
131 size_t size
= nlp
->size
;
133 if (nlp
->nlh
== NULL
)
136 /* Walk through all entries we got from the kernel and look, which
137 message type they contain. */
138 for (nlh
= nlp
->nlh
; NLMSG_OK (nlh
, size
); nlh
= NLMSG_NEXT (nlh
, size
))
140 /* Check if the message is what we want. */
141 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
144 if (nlh
->nlmsg_type
== NLMSG_DONE
)
147 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
149 struct ifinfomsg
*ifim
= (struct ifinfomsg
*) NLMSG_DATA (nlh
);
150 struct rtattr
*rta
= IFLA_RTA (ifim
);
151 size_t rtasize
= IFLA_PAYLOAD (nlh
);
153 idx
[nifs
].if_index
= ifim
->ifi_index
;
155 while (RTA_OK (rta
, rtasize
))
157 char *rta_data
= RTA_DATA (rta
);
158 size_t rta_payload
= RTA_PAYLOAD (rta
);
160 if (rta
->rta_type
== IFLA_IFNAME
)
162 idx
[nifs
].if_name
= __strndup (rta_data
, rta_payload
);
163 if (idx
[nifs
].if_name
== NULL
)
165 idx
[nifs
].if_index
= 0;
166 if_freenameindex (idx
);
173 rta
= RTA_NEXT (rta
, rtasize
);
181 idx
[nifs
].if_index
= 0;
182 idx
[nifs
].if_name
= NULL
;
185 __netlink_free_handle (&nh
);
186 __netlink_close (&nh
);
192 struct if_nameindex
*
196 __set_errno (ENOSYS
);
199 struct if_nameindex
*result
= if_nameindex_netlink ();
203 libc_hidden_def (if_nameindex
)
207 if_indextoname (unsigned int ifindex
, char *ifname
)
209 /* We may be able to do the conversion directly, rather than searching a
210 list. This ioctl is not present in kernels before version 2.1.50. */
220 ifr
.ifr_ifindex
= ifindex
;
221 status
= __ioctl (fd
, SIOCGIFNAME
, &ifr
);
223 close_not_cancel_no_status (fd
);
228 /* POSIX requires ENXIO. */
234 return strncpy (ifname
, ifr
.ifr_name
, IFNAMSIZ
);
236 libc_hidden_def (if_indextoname
)