1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003-2017 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 <http://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
);
70 static uint32_t nl_timestamp
;
73 __bump_nl_timestamp (void)
75 if (atomic_increment_val (&nl_timestamp
) == 0)
76 atomic_increment (&nl_timestamp
);
82 static inline uint32_t
83 get_nl_timestamp (void)
87 #elif defined USE_NSCD
88 return __nscd_get_nl_timestamp ();
99 uint32_t timestamp
= get_nl_timestamp ();
100 return timestamp
!= 0 && cache
->timestamp
== timestamp
;
106 static struct cached_data
*
107 make_request (int fd
, pid_t pid
)
109 struct cached_data
*result
= NULL
;
111 size_t result_len
= 0;
112 size_t result_cap
= 32;
118 /* struct rtgenmsg consists of a single byte. This means there
119 are three bytes of padding included in the REQ definition.
120 We make them explicit here. */
123 struct sockaddr_nl nladdr
;
125 req
.nlh
.nlmsg_len
= sizeof (req
);
126 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
127 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
128 req
.nlh
.nlmsg_pid
= 0;
129 req
.nlh
.nlmsg_seq
= time (NULL
);
130 req
.g
.rtgen_family
= AF_UNSPEC
;
132 assert (sizeof (req
) - offsetof (struct req
, pad
) == 3);
133 memset (req
.pad
, '\0', sizeof (req
.pad
));
135 memset (&nladdr
, '\0', sizeof (nladdr
));
136 nladdr
.nl_family
= AF_NETLINK
;
139 const size_t buf_size
= PAGE_SIZE
;
141 const size_t buf_size
= 4096;
145 struct iovec iov
= { buf
, buf_size
};
147 if (TEMP_FAILURE_RETRY (__sendto (fd
, (void *) &req
, sizeof (req
), 0,
148 (struct sockaddr
*) &nladdr
,
149 sizeof (nladdr
))) < 0)
154 bool seen_ipv4
= false;
155 bool seen_ipv6
= false;
161 .msg_name
= (void *) &nladdr
,
162 .msg_namelen
= sizeof (nladdr
),
170 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
171 __netlink_assert_response (fd
, read_len
);
175 if (msg
.msg_flags
& MSG_TRUNC
)
178 struct nlmsghdr
*nlmh
;
179 for (nlmh
= (struct nlmsghdr
*) buf
;
180 NLMSG_OK (nlmh
, (size_t) read_len
);
181 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
183 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
184 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
187 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
189 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
190 struct rtattr
*rta
= IFA_RTA (ifam
);
191 size_t len
= nlmh
->nlmsg_len
- NLMSG_LENGTH (sizeof (*ifam
));
193 if (ifam
->ifa_family
!= AF_INET
194 && ifam
->ifa_family
!= AF_INET6
)
197 const void *local
= NULL
;
198 const void *address
= NULL
;
199 while (RTA_OK (rta
, len
))
201 switch (rta
->rta_type
)
204 local
= RTA_DATA (rta
);
208 address
= RTA_DATA (rta
);
212 rta
= RTA_NEXT (rta
, len
);
219 if (ifam
->ifa_family
== AF_INET
)
221 if (*(const in_addr_t
*) address
222 != htonl (INADDR_LOOPBACK
))
227 if (!IN6_IS_ADDR_LOOPBACK (address
))
232 if (result_len
== 0 || result_len
== result_cap
)
234 result_cap
= 2 * result_cap
;
235 result
= realloc (result
, sizeof (*result
)
237 * sizeof (struct in6addrinfo
));
243 struct in6addrinfo
*info
= &result
->in6ai
[result_len
++];
245 info
->flags
= (((ifam
->ifa_flags
246 & (IFA_F_DEPRECATED
| IFA_F_OPTIMISTIC
))
247 ? in6ai_deprecated
: 0)
248 | ((ifam
->ifa_flags
& IFA_F_HOMEADDRESS
)
249 ? in6ai_homeaddress
: 0));
250 info
->prefixlen
= ifam
->ifa_prefixlen
;
251 info
->index
= ifam
->ifa_index
;
252 if (ifam
->ifa_family
== AF_INET
)
256 info
->addr
[2] = htonl (0xffff);
257 info
->addr
[3] = *(const in_addr_t
*) address
;
260 memcpy (info
->addr
, address
, sizeof (info
->addr
));
262 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
263 /* We found the end, leave the loop. */
269 if (seen_ipv6
&& result
!= NULL
)
271 result
->timestamp
= get_nl_timestamp ();
273 result
->seen_ipv4
= seen_ipv4
;
274 result
->seen_ipv6
= true;
275 result
->in6ailen
= result_len
;
281 atomic_add (&noai6ai_cached
.usecnt
, 2);
282 noai6ai_cached
.seen_ipv4
= seen_ipv4
;
283 noai6ai_cached
.seen_ipv6
= seen_ipv6
;
284 result
= &noai6ai_cached
;
298 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
,
299 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
304 struct cached_data
*olddata
= NULL
;
305 struct cached_data
*data
= NULL
;
307 __libc_lock_lock (lock
);
309 if (cache_valid_p ())
312 atomic_increment (&cache
->usecnt
);
316 int fd
= __socket (PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
318 if (__glibc_likely (fd
>= 0))
320 struct sockaddr_nl nladdr
;
321 memset (&nladdr
, '\0', sizeof (nladdr
));
322 nladdr
.nl_family
= AF_NETLINK
;
324 socklen_t addr_len
= sizeof (nladdr
);
326 if (__bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
327 && __getsockname (fd
, (struct sockaddr
*) &nladdr
,
329 data
= make_request (fd
, nladdr
.nl_pid
);
331 __close_nocancel_nostatus (fd
);
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_add_zero (&olddata
->usecnt
, -1))
358 /* We cannot determine what interfaces are available. Be
364 /* Free the cache if it has been allocated. */
365 libc_freeres_fn (freecache
)
368 __free_in6ai (cache
->in6ai
);
372 __free_in6ai (struct in6addrinfo
*ai
)
376 struct cached_data
*data
=
377 (struct cached_data
*) ((char *) ai
378 - offsetof (struct cached_data
, in6ai
));
380 if (atomic_add_zero (&data
->usecnt
, -1))
382 __libc_lock_lock (lock
);
384 if (data
->usecnt
== 0)
388 __libc_lock_unlock (lock
);