]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/if_index.c
Linux: Fix typo in comment in termios_internals.h
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / if_index.c
CommitLineData
26420023 1/* Copyright (C) 1997-2025 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
92f1da4d 18#include <errno.h>
1f205a47
UD
19#include <string.h>
20#include <stdio.h>
21#include <stdlib.h>
6591c335 22#include <unistd.h>
1f205a47 23#include <net/if.h>
8f2ece69
UD
24#include <sys/socket.h>
25#include <sys/ioctl.h>
ec999b8e 26#include <libc-lock.h>
57375460 27#include <not-cancel.h>
1f205a47 28
f5164429
UD
29#include "netlinkaccess.h"
30
9a8fcca0 31
8f2ece69 32unsigned int
9a44d530 33__if_nametoindex (const char *ifname)
1f205a47 34{
2180fee1
SE
35 if (strlen (ifname) >= IFNAMSIZ)
36 {
37 __set_errno (ENODEV);
38 return 0;
39 }
40
d527c860 41 int fd = __opensock ();
d527c860
FW
42 if (fd < 0)
43 return 0;
44
7a751ce3
MR
45 struct ifreq ifr;
46 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
47
48 int status = __ioctl (fd, SIOCGIFINDEX, &ifr);
c181840c 49 __close_nocancel_nostatus (fd);
7a751ce3
MR
50
51 return status < 0 ? 0 : ifr.ifr_ifindex;
1f205a47 52}
9a44d530
JM
53libc_hidden_def (__if_nametoindex)
54weak_alias (__if_nametoindex, if_nametoindex)
55libc_hidden_weak (if_nametoindex)
faea9de6 56
1f205a47 57
8f2ece69 58void
9a44d530 59__if_freenameindex (struct if_nameindex *ifn)
1f205a47
UD
60{
61 struct if_nameindex *ptr = ifn;
478b92f0 62 while (ptr->if_name || ptr->if_index)
1f205a47 63 {
f5164429 64 free (ptr->if_name);
8f2ece69 65 ++ptr;
1f205a47 66 }
8f2ece69 67 free (ifn);
1f205a47 68}
9a44d530
JM
69libc_hidden_def (__if_freenameindex)
70weak_alias (__if_freenameindex, if_freenameindex)
71libc_hidden_weak (if_freenameindex)
1f205a47 72
f5164429 73
7a751ce3
MR
74struct if_nameindex *
75__if_nameindex (void)
f5164429
UD
76{
77 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
78 struct if_nameindex *idx = NULL;
79
89b4b02f 80 if (__netlink_open (&nh) < 0)
f5164429
UD
81 return NULL;
82
83
84 /* Tell the kernel that we wish to get a list of all
c63d8f80
UD
85 active interfaces. Collect all data for every interface. */
86 if (__netlink_request (&nh, RTM_GETLINK) < 0)
f5164429
UD
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;
9a44d530 163 __if_freenameindex (idx);
f5164429
UD
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);
f5164429
UD
183 __netlink_close (&nh);
184
185 return idx;
186}
9a44d530
JM
187weak_alias (__if_nameindex, if_nameindex)
188libc_hidden_weak (if_nameindex)
f5164429 189
8f2ece69
UD
190
191char *
26492c0a 192__if_indextoname (unsigned int ifindex, char ifname[IF_NAMESIZE])
8f2ece69 193{
40a55d20
UD
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;
ffb7875d 198 int status;
9a8fcca0 199
ffb7875d 200 fd = __opensock ();
c7be5c15 201
ffb7875d
JM
202 if (fd < 0)
203 return NULL;
40a55d20 204
ffb7875d
JM
205 ifr.ifr_ifindex = ifindex;
206 status = __ioctl (fd, SIOCGIFNAME, &ifr);
40a55d20 207
c181840c 208 __close_nocancel_nostatus (fd);
40a55d20 209
ffb7875d 210 if (status < 0)
55c14926 211 {
ffb7875d
JM
212 if (errno == ENODEV)
213 /* POSIX requires ENXIO. */
c7be5c15 214 __set_errno (ENXIO);
71d3bda9 215
ffb7875d 216 return NULL;
71d3bda9
UD
217 }
218 else
ffb7875d 219 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
71d3bda9 220}
9a44d530
JM
221weak_alias (__if_indextoname, if_indextoname)
222libc_hidden_weak (if_indextoname)