1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
28 #include <sys/socket.h>
30 #include <asm/types.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
34 #include <not-cancel.h>
35 #include <libc-lock.h>
37 #include <nscd/nscd-client.h>
39 #include "netlinkaccess.h"
41 #ifndef IFA_F_HOMEADDRESS
42 # define IFA_F_HOMEADDRESS 0
44 #ifndef IFA_F_OPTIMISTIC
45 # define IFA_F_OPTIMISTIC 0
56 struct in6addrinfo in6ai
[0];
59 static struct cached_data noai6ai_cached
=
61 .usecnt
= 1, /* Make sure we never try to delete this entry. */
65 static struct cached_data
*cache
;
66 __libc_lock_define_initialized (static, lock
);
69 static inline uint32_t
70 get_nl_timestamp (void)
73 return __nscd_get_nl_timestamp ();
84 uint32_t timestamp
= get_nl_timestamp ();
85 return timestamp
!= 0 && cache
->timestamp
== timestamp
;
91 static struct cached_data
*
92 make_request (int fd
, pid_t pid
)
94 struct cached_data
*result
= NULL
;
96 size_t result_len
= 0;
97 size_t result_cap
= 32;
103 /* struct rtgenmsg consists of a single byte. This means there
104 are three bytes of padding included in the REQ definition.
105 We make them explicit here. */
108 struct sockaddr_nl nladdr
;
110 req
.nlh
.nlmsg_len
= sizeof (req
);
111 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
112 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
113 req
.nlh
.nlmsg_pid
= 0;
114 req
.nlh
.nlmsg_seq
= time_now ();
115 req
.g
.rtgen_family
= AF_UNSPEC
;
117 assert (sizeof (req
) - offsetof (struct req
, pad
) == 3);
118 memset (req
.pad
, '\0', sizeof (req
.pad
));
120 memset (&nladdr
, '\0', sizeof (nladdr
));
121 nladdr
.nl_family
= AF_NETLINK
;
124 const size_t buf_size
= PAGE_SIZE
;
126 const size_t buf_size
= 4096;
130 struct iovec iov
= { buf
, buf_size
};
132 if (TEMP_FAILURE_RETRY (__sendto (fd
, (void *) &req
, sizeof (req
), 0,
133 (struct sockaddr
*) &nladdr
,
134 sizeof (nladdr
))) < 0)
139 bool seen_ipv4
= false;
140 bool seen_ipv6
= false;
146 .msg_name
= (void *) &nladdr
,
147 .msg_namelen
= sizeof (nladdr
),
155 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
156 __netlink_assert_response (fd
, read_len
);
160 if (msg
.msg_flags
& MSG_TRUNC
)
163 struct nlmsghdr
*nlmh
;
164 for (nlmh
= (struct nlmsghdr
*) buf
;
165 NLMSG_OK (nlmh
, (size_t) read_len
);
166 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
168 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
169 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
172 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
174 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
175 struct rtattr
*rta
= IFA_RTA (ifam
);
176 size_t len
= nlmh
->nlmsg_len
- NLMSG_LENGTH (sizeof (*ifam
));
178 if (ifam
->ifa_family
!= AF_INET
179 && ifam
->ifa_family
!= AF_INET6
)
182 const void *local
= NULL
;
183 const void *address
= NULL
;
184 while (RTA_OK (rta
, len
))
186 switch (rta
->rta_type
)
189 local
= RTA_DATA (rta
);
193 address
= RTA_DATA (rta
);
197 rta
= RTA_NEXT (rta
, len
);
204 if (ifam
->ifa_family
== AF_INET
)
206 if (*(const in_addr_t
*) address
207 != htonl (INADDR_LOOPBACK
))
212 if (!IN6_IS_ADDR_LOOPBACK (address
))
217 if (result_len
== 0 || result_len
== result_cap
)
219 result_cap
= 2 * result_cap
;
220 result
= realloc (result
, sizeof (*result
)
222 * sizeof (struct in6addrinfo
));
228 struct in6addrinfo
*info
= &result
->in6ai
[result_len
++];
230 info
->flags
= (((ifam
->ifa_flags
231 & (IFA_F_DEPRECATED
| IFA_F_OPTIMISTIC
))
232 ? in6ai_deprecated
: 0)
233 | ((ifam
->ifa_flags
& IFA_F_HOMEADDRESS
)
234 ? in6ai_homeaddress
: 0));
235 info
->prefixlen
= ifam
->ifa_prefixlen
;
236 info
->index
= ifam
->ifa_index
;
237 if (ifam
->ifa_family
== AF_INET
)
241 info
->addr
[2] = htonl (0xffff);
242 info
->addr
[3] = *(const in_addr_t
*) address
;
245 memcpy (info
->addr
, address
, sizeof (info
->addr
));
247 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
248 /* We found the end, leave the loop. */
254 if (seen_ipv6
&& result
!= NULL
)
256 result
->timestamp
= get_nl_timestamp ();
258 result
->seen_ipv4
= seen_ipv4
;
259 result
->seen_ipv6
= true;
260 result
->in6ailen
= result_len
;
266 atomic_fetch_add_relaxed (&noai6ai_cached
.usecnt
, 2);
267 noai6ai_cached
.seen_ipv4
= seen_ipv4
;
268 noai6ai_cached
.seen_ipv6
= seen_ipv6
;
269 result
= &noai6ai_cached
;
282 cancel_handler (void *arg
__attribute__((unused
)))
284 /* Release the lock. */
285 __libc_lock_unlock (lock
);
291 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
,
292 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
297 struct cached_data
*olddata
= NULL
;
298 struct cached_data
*data
= NULL
;
301 /* Make sure that lock is released when the thread is cancelled. */
302 __libc_cleanup_push (cancel_handler
, NULL
);
304 __libc_lock_lock (lock
);
306 if (cache_valid_p ())
309 atomic_fetch_add_relaxed (&cache
->usecnt
, 1);
313 int fd
= __socket (PF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, NETLINK_ROUTE
);
315 if (__glibc_likely (fd
>= 0))
317 struct sockaddr_nl nladdr
;
318 memset (&nladdr
, '\0', sizeof (nladdr
));
319 nladdr
.nl_family
= AF_NETLINK
;
321 socklen_t addr_len
= sizeof (nladdr
);
323 if (__bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
324 && __getsockname (fd
, (struct sockaddr
*) &nladdr
,
326 data
= make_request (fd
, nladdr
.nl_pid
);
328 __close_nocancel_nostatus (fd
);
339 __libc_cleanup_pop (0);
341 __libc_lock_unlock (lock
);
346 *seen_ipv4
= data
->seen_ipv4
;
347 *seen_ipv6
= data
->seen_ipv6
;
348 *in6ailen
= data
->in6ailen
;
349 *in6ai
= data
->in6ai
;
351 if (olddata
!= NULL
&& olddata
->usecnt
> 0
352 && atomic_fetch_add_relaxed (&olddata
->usecnt
, -1) == 1)
358 /* We cannot determine what interfaces are available. Be
364 /* Free the cache if it has been allocated. */
366 __check_pf_freemem (void)
369 __free_in6ai (cache
->in6ai
);
373 __free_in6ai (struct in6addrinfo
*ai
)
377 struct cached_data
*data
=
378 (struct cached_data
*) ((char *) ai
379 - offsetof (struct cached_data
, in6ai
));
381 if (atomic_fetch_add_relaxed (&data
->usecnt
, -1) == 1)
383 __libc_lock_lock (lock
);
385 if (data
->usecnt
== 0)
389 __libc_lock_unlock (lock
);