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