]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/if_index.c
5d137598f0eb46a80834ee1ad687fc209a831b95
1 /* Copyright (C) 1997-2025 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 <https://www.gnu.org/licenses/>. */
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <libc-lock.h>
27 #include <not-cancel.h>
29 #include "netlinkaccess.h"
33 __if_nametoindex (const char *ifname
)
35 if (strlen (ifname
) >= IFNAMSIZ
)
41 int fd
= __opensock ();
46 strncpy (ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
48 int status
= __ioctl (fd
, SIOCGIFINDEX
, &ifr
);
49 __close_nocancel_nostatus (fd
);
51 return status
< 0 ? 0 : ifr
.ifr_ifindex
;
53 libc_hidden_def (__if_nametoindex
)
54 weak_alias (__if_nametoindex
, if_nametoindex
)
55 libc_hidden_weak (if_nametoindex
)
59 __if_freenameindex (struct if_nameindex
*ifn
)
61 struct if_nameindex
*ptr
= ifn
;
62 while (ptr
->if_name
|| ptr
->if_index
)
69 libc_hidden_def (__if_freenameindex
)
70 weak_alias (__if_freenameindex
, if_freenameindex
)
71 libc_hidden_weak (if_freenameindex
)
77 struct netlink_handle nh
= { 0, 0, 0, NULL
, NULL
};
78 struct if_nameindex
*idx
= NULL
;
80 if (__netlink_open (&nh
) < 0)
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)
89 /* Count the interfaces. */
90 unsigned int nifs
= 0;
91 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
94 size_t size
= nlp
->size
;
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
))
103 /* Check if the message is what we want. */
104 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
107 if (nlh
->nlmsg_type
== NLMSG_DONE
)
110 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
115 idx
= malloc ((nifs
+ 1) * sizeof (struct if_nameindex
));
119 __set_errno (ENOBUFS
);
123 /* Add the interfaces. */
125 for (struct netlink_res
*nlp
= nh
.nlm_list
; nlp
; nlp
= nlp
->next
)
127 struct nlmsghdr
*nlh
;
128 size_t size
= nlp
->size
;
130 if (nlp
->nlh
== NULL
)
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
))
137 /* Check if the message is what we want. */
138 if ((pid_t
) nlh
->nlmsg_pid
!= nh
.pid
|| nlh
->nlmsg_seq
!= nlp
->seq
)
141 if (nlh
->nlmsg_type
== NLMSG_DONE
)
144 if (nlh
->nlmsg_type
== RTM_NEWLINK
)
146 struct ifinfomsg
*ifim
= (struct ifinfomsg
*) NLMSG_DATA (nlh
);
147 struct rtattr
*rta
= IFLA_RTA (ifim
);
148 size_t rtasize
= IFLA_PAYLOAD (nlh
);
150 idx
[nifs
].if_index
= ifim
->ifi_index
;
152 while (RTA_OK (rta
, rtasize
))
154 char *rta_data
= RTA_DATA (rta
);
155 size_t rta_payload
= RTA_PAYLOAD (rta
);
157 if (rta
->rta_type
== IFLA_IFNAME
)
159 idx
[nifs
].if_name
= __strndup (rta_data
, rta_payload
);
160 if (idx
[nifs
].if_name
== NULL
)
162 idx
[nifs
].if_index
= 0;
163 __if_freenameindex (idx
);
170 rta
= RTA_NEXT (rta
, rtasize
);
178 idx
[nifs
].if_index
= 0;
179 idx
[nifs
].if_name
= NULL
;
182 __netlink_free_handle (&nh
);
183 __netlink_close (&nh
);
187 weak_alias (__if_nameindex
, if_nameindex
)
188 libc_hidden_weak (if_nameindex
)
192 __if_indextoname (unsigned int ifindex
, char ifname
[IF_NAMESIZE
])
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. */
205 ifr
.ifr_ifindex
= ifindex
;
206 status
= __ioctl (fd
, SIOCGIFNAME
, &ifr
);
208 __close_nocancel_nostatus (fd
);
213 /* POSIX requires ENXIO. */
219 return strncpy (ifname
, ifr
.ifr_name
, IFNAMSIZ
);
221 weak_alias (__if_indextoname
, if_indextoname
)
222 libc_hidden_weak (if_indextoname
)