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