]>
Commit | Line | Data |
---|---|---|
f7a9f785 | 1 | /* Copyright (C) 1997-2016 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
1f205a47 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
1f205a47 | 8 | |
478b92f0 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
1f205a47 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
1f205a47 | 17 | |
de7b50f4 | 18 | #include <alloca.h> |
92f1da4d | 19 | #include <errno.h> |
1f205a47 UD |
20 | #include <string.h> |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
6591c335 | 23 | #include <unistd.h> |
1f205a47 | 24 | #include <net/if.h> |
8f2ece69 UD |
25 | #include <sys/socket.h> |
26 | #include <sys/ioctl.h> | |
ec999b8e | 27 | #include <libc-lock.h> |
57375460 | 28 | #include <not-cancel.h> |
1f205a47 | 29 | |
f5164429 UD |
30 | #include "netlinkaccess.h" |
31 | ||
9a8fcca0 | 32 | |
8f2ece69 | 33 | unsigned int |
9a44d530 | 34 | __if_nametoindex (const char *ifname) |
1f205a47 | 35 | { |
263456bd | 36 | #ifndef SIOCGIFINDEX |
55c14926 | 37 | __set_errno (ENOSYS); |
ca34d7a7 | 38 | return 0; |
55c14926 | 39 | #else |
8f2ece69 | 40 | struct ifreq ifr; |
f720d3d2 | 41 | int fd = __opensock (); |
8f2ece69 UD |
42 | |
43 | if (fd < 0) | |
44 | return 0; | |
45 | ||
46 | strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); | |
263456bd | 47 | if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0) |
8f2ece69 | 48 | { |
ca34d7a7 | 49 | int saved_errno = errno; |
57375460 | 50 | close_not_cancel_no_status (fd); |
ca34d7a7 UD |
51 | if (saved_errno == EINVAL) |
52 | __set_errno (ENOSYS); | |
8f2ece69 | 53 | return 0; |
1f205a47 | 54 | } |
57375460 | 55 | close_not_cancel_no_status (fd); |
8f2ece69 | 56 | return ifr.ifr_ifindex; |
55c14926 | 57 | #endif |
1f205a47 | 58 | } |
9a44d530 JM |
59 | libc_hidden_def (__if_nametoindex) |
60 | weak_alias (__if_nametoindex, if_nametoindex) | |
61 | libc_hidden_weak (if_nametoindex) | |
faea9de6 | 62 | |
1f205a47 | 63 | |
8f2ece69 | 64 | void |
9a44d530 | 65 | __if_freenameindex (struct if_nameindex *ifn) |
1f205a47 UD |
66 | { |
67 | struct if_nameindex *ptr = ifn; | |
478b92f0 | 68 | while (ptr->if_name || ptr->if_index) |
1f205a47 | 69 | { |
f5164429 | 70 | free (ptr->if_name); |
8f2ece69 | 71 | ++ptr; |
1f205a47 | 72 | } |
8f2ece69 | 73 | free (ifn); |
1f205a47 | 74 | } |
9a44d530 JM |
75 | libc_hidden_def (__if_freenameindex) |
76 | weak_alias (__if_freenameindex, if_freenameindex) | |
77 | libc_hidden_weak (if_freenameindex) | |
1f205a47 | 78 | |
f5164429 | 79 | |
f5164429 UD |
80 | static struct if_nameindex * |
81 | if_nameindex_netlink (void) | |
82 | { | |
83 | struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; | |
84 | struct if_nameindex *idx = NULL; | |
85 | ||
89b4b02f | 86 | if (__netlink_open (&nh) < 0) |
f5164429 UD |
87 | return NULL; |
88 | ||
89 | ||
90 | /* Tell the kernel that we wish to get a list of all | |
c63d8f80 UD |
91 | active interfaces. Collect all data for every interface. */ |
92 | if (__netlink_request (&nh, RTM_GETLINK) < 0) | |
f5164429 UD |
93 | goto exit_free; |
94 | ||
95 | /* Count the interfaces. */ | |
96 | unsigned int nifs = 0; | |
97 | for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) | |
98 | { | |
99 | struct nlmsghdr *nlh; | |
100 | size_t size = nlp->size; | |
101 | ||
102 | if (nlp->nlh == NULL) | |
103 | continue; | |
104 | ||
105 | /* Walk through all entries we got from the kernel and look, which | |
106 | message type they contain. */ | |
107 | for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) | |
108 | { | |
109 | /* Check if the message is what we want. */ | |
110 | if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) | |
111 | continue; | |
112 | ||
113 | if (nlh->nlmsg_type == NLMSG_DONE) | |
114 | break; /* ok */ | |
115 | ||
116 | if (nlh->nlmsg_type == RTM_NEWLINK) | |
117 | ++nifs; | |
118 | } | |
119 | } | |
120 | ||
121 | idx = malloc ((nifs + 1) * sizeof (struct if_nameindex)); | |
122 | if (idx == NULL) | |
123 | { | |
124 | nomem: | |
125 | __set_errno (ENOBUFS); | |
126 | goto exit_free; | |
127 | } | |
128 | ||
129 | /* Add the interfaces. */ | |
130 | nifs = 0; | |
131 | for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) | |
132 | { | |
133 | struct nlmsghdr *nlh; | |
134 | size_t size = nlp->size; | |
135 | ||
136 | if (nlp->nlh == NULL) | |
137 | continue; | |
138 | ||
139 | /* Walk through all entries we got from the kernel and look, which | |
140 | message type they contain. */ | |
141 | for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) | |
142 | { | |
143 | /* Check if the message is what we want. */ | |
144 | if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) | |
145 | continue; | |
146 | ||
147 | if (nlh->nlmsg_type == NLMSG_DONE) | |
148 | break; /* ok */ | |
149 | ||
150 | if (nlh->nlmsg_type == RTM_NEWLINK) | |
151 | { | |
152 | struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); | |
153 | struct rtattr *rta = IFLA_RTA (ifim); | |
154 | size_t rtasize = IFLA_PAYLOAD (nlh); | |
155 | ||
156 | idx[nifs].if_index = ifim->ifi_index; | |
157 | ||
158 | while (RTA_OK (rta, rtasize)) | |
159 | { | |
160 | char *rta_data = RTA_DATA (rta); | |
161 | size_t rta_payload = RTA_PAYLOAD (rta); | |
162 | ||
163 | if (rta->rta_type == IFLA_IFNAME) | |
164 | { | |
165 | idx[nifs].if_name = __strndup (rta_data, rta_payload); | |
166 | if (idx[nifs].if_name == NULL) | |
167 | { | |
168 | idx[nifs].if_index = 0; | |
9a44d530 | 169 | __if_freenameindex (idx); |
f5164429 UD |
170 | idx = NULL; |
171 | goto nomem; | |
172 | } | |
173 | break; | |
174 | } | |
175 | ||
176 | rta = RTA_NEXT (rta, rtasize); | |
177 | } | |
178 | ||
179 | ++nifs; | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | idx[nifs].if_index = 0; | |
185 | idx[nifs].if_name = NULL; | |
186 | ||
187 | exit_free: | |
188 | __netlink_free_handle (&nh); | |
f5164429 UD |
189 | __netlink_close (&nh); |
190 | ||
191 | return idx; | |
192 | } | |
193 | ||
194 | ||
195 | struct if_nameindex * | |
9a44d530 | 196 | __if_nameindex (void) |
f5164429 UD |
197 | { |
198 | #ifndef SIOCGIFINDEX | |
199 | __set_errno (ENOSYS); | |
200 | return NULL; | |
201 | #else | |
202 | struct if_nameindex *result = if_nameindex_netlink (); | |
f5164429 | 203 | return result; |
55c14926 | 204 | #endif |
8f2ece69 | 205 | } |
9a44d530 JM |
206 | weak_alias (__if_nameindex, if_nameindex) |
207 | libc_hidden_weak (if_nameindex) | |
f5164429 | 208 | |
8f2ece69 UD |
209 | |
210 | char * | |
9a44d530 | 211 | __if_indextoname (unsigned int ifindex, char *ifname) |
8f2ece69 | 212 | { |
40a55d20 UD |
213 | /* We may be able to do the conversion directly, rather than searching a |
214 | list. This ioctl is not present in kernels before version 2.1.50. */ | |
215 | struct ifreq ifr; | |
216 | int fd; | |
ffb7875d | 217 | int status; |
9a8fcca0 | 218 | |
ffb7875d | 219 | fd = __opensock (); |
c7be5c15 | 220 | |
ffb7875d JM |
221 | if (fd < 0) |
222 | return NULL; | |
40a55d20 | 223 | |
ffb7875d JM |
224 | ifr.ifr_ifindex = ifindex; |
225 | status = __ioctl (fd, SIOCGIFNAME, &ifr); | |
40a55d20 | 226 | |
ffb7875d | 227 | close_not_cancel_no_status (fd); |
40a55d20 | 228 | |
ffb7875d | 229 | if (status < 0) |
55c14926 | 230 | { |
ffb7875d JM |
231 | if (errno == ENODEV) |
232 | /* POSIX requires ENXIO. */ | |
c7be5c15 | 233 | __set_errno (ENXIO); |
71d3bda9 | 234 | |
ffb7875d | 235 | return NULL; |
71d3bda9 UD |
236 | } |
237 | else | |
ffb7875d | 238 | return strncpy (ifname, ifr.ifr_name, IFNAMSIZ); |
71d3bda9 | 239 | } |
9a44d530 JM |
240 | weak_alias (__if_indextoname, if_indextoname) |
241 | libc_hidden_weak (if_indextoname) |