1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003-2014 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 <bits/libc-lock.h>
37 #include <nscd/nscd-client.h>
40 #ifndef IFA_F_HOMEADDRESS
41 # define IFA_F_HOMEADDRESS 0
43 #ifndef IFA_F_OPTIMISTIC
44 # define IFA_F_OPTIMISTIC 0
55 struct in6addrinfo in6ai
[0];
58 static struct cached_data noai6ai_cached
=
60 .usecnt
= 1, /* Make sure we never try to delete this entry. */
64 static struct cached_data
*cache
;
65 __libc_lock_define_initialized (static, lock
);
69 static uint32_t nl_timestamp
;
72 __bump_nl_timestamp (void)
74 if (atomic_increment_val (&nl_timestamp
) == 0)
75 atomic_increment (&nl_timestamp
);
81 static inline uint32_t
82 get_nl_timestamp (void)
86 #elif defined USE_NSCD
87 return __nscd_get_nl_timestamp ();
98 uint32_t timestamp
= get_nl_timestamp ();
99 return timestamp
!= 0 && cache
->timestamp
== timestamp
;
105 static struct cached_data
*
106 make_request (int fd
, pid_t pid
)
112 /* struct rtgenmsg consists of a single byte. This means there
113 are three bytes of padding included in the REQ definition.
114 We make them explicit here. */
117 struct sockaddr_nl nladdr
;
119 req
.nlh
.nlmsg_len
= sizeof (req
);
120 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
121 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
122 req
.nlh
.nlmsg_pid
= 0;
123 req
.nlh
.nlmsg_seq
= time (NULL
);
124 req
.g
.rtgen_family
= AF_UNSPEC
;
126 assert (sizeof (req
) - offsetof (struct req
, pad
) == 3);
127 memset (req
.pad
, '\0', sizeof (req
.pad
));
129 memset (&nladdr
, '\0', sizeof (nladdr
));
130 nladdr
.nl_family
= AF_NETLINK
;
133 /* Help the compiler optimize out the malloc call if PAGE_SIZE
134 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
135 const size_t buf_size
= PAGE_SIZE
;
137 const size_t buf_size
= __getpagesize ();
139 bool use_malloc
= false;
141 size_t alloca_used
= 0;
143 if (__libc_use_alloca (buf_size
))
144 buf
= alloca_account (buf_size
, alloca_used
);
147 buf
= malloc (buf_size
);
154 struct iovec iov
= { buf
, buf_size
};
156 if (TEMP_FAILURE_RETRY (__sendto (fd
, (void *) &req
, sizeof (req
), 0,
157 (struct sockaddr
*) &nladdr
,
158 sizeof (nladdr
))) < 0)
164 struct in6addrinfo info
;
165 struct in6ailist
*next
;
168 size_t in6ailistlen
= 0;
169 bool seen_ipv4
= false;
170 bool seen_ipv6
= false;
176 (void *) &nladdr
, sizeof (nladdr
),
182 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
186 if (msg
.msg_flags
& MSG_TRUNC
)
189 struct nlmsghdr
*nlmh
;
190 for (nlmh
= (struct nlmsghdr
*) buf
;
191 NLMSG_OK (nlmh
, (size_t) read_len
);
192 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
194 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
195 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
198 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
200 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
201 struct rtattr
*rta
= IFA_RTA (ifam
);
202 size_t len
= nlmh
->nlmsg_len
- NLMSG_LENGTH (sizeof (*ifam
));
204 if (ifam
->ifa_family
!= AF_INET
205 && ifam
->ifa_family
!= AF_INET6
)
208 const void *local
= NULL
;
209 const void *address
= NULL
;
210 while (RTA_OK (rta
, len
))
212 switch (rta
->rta_type
)
215 local
= RTA_DATA (rta
);
219 address
= RTA_DATA (rta
);
223 rta
= RTA_NEXT (rta
, len
);
230 if (ifam
->ifa_family
== AF_INET
)
232 if (*(const in_addr_t
*) address
233 != htonl (INADDR_LOOPBACK
))
238 if (!IN6_IS_ADDR_LOOPBACK (address
))
243 struct in6ailist
*newp
;
244 if (__libc_use_alloca (alloca_used
+ sizeof (*newp
)))
246 newp
= alloca_account (sizeof (*newp
), alloca_used
);
247 newp
->use_malloc
= false;
251 newp
= malloc (sizeof (*newp
));
254 newp
->use_malloc
= true;
256 newp
->info
.flags
= (((ifam
->ifa_flags
259 ? in6ai_deprecated
: 0)
262 ? in6ai_homeaddress
: 0));
263 newp
->info
.prefixlen
= ifam
->ifa_prefixlen
;
264 newp
->info
.index
= ifam
->ifa_index
;
265 if (ifam
->ifa_family
== AF_INET
)
267 newp
->info
.addr
[0] = 0;
268 newp
->info
.addr
[1] = 0;
269 newp
->info
.addr
[2] = htonl (0xffff);
270 newp
->info
.addr
[3] = *(const in_addr_t
*) address
;
273 memcpy (newp
->info
.addr
, address
, sizeof (newp
->info
.addr
));
274 newp
->next
= in6ailist
;
278 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
279 /* We found the end, leave the loop. */
285 struct cached_data
*result
;
286 if (seen_ipv6
&& in6ailist
!= NULL
)
288 result
= malloc (sizeof (*result
)
289 + in6ailistlen
* sizeof (struct in6addrinfo
));
293 result
->timestamp
= get_nl_timestamp ();
295 result
->seen_ipv4
= seen_ipv4
;
296 result
->seen_ipv6
= true;
297 result
->in6ailen
= in6ailistlen
;
301 result
->in6ai
[--in6ailistlen
] = in6ailist
->info
;
302 struct in6ailist
*next
= in6ailist
->next
;
303 if (in6ailist
->use_malloc
)
307 while (in6ailist
!= NULL
);
311 atomic_add (&noai6ai_cached
.usecnt
, 2);
312 noai6ai_cached
.seen_ipv4
= seen_ipv4
;
313 noai6ai_cached
.seen_ipv6
= seen_ipv6
;
314 result
= &noai6ai_cached
;
322 while (in6ailist
!= NULL
)
324 struct in6ailist
*next
= in6ailist
->next
;
325 if (in6ailist
->use_malloc
)
338 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
,
339 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
344 struct cached_data
*olddata
= NULL
;
345 struct cached_data
*data
= NULL
;
347 __libc_lock_lock (lock
);
349 if (cache_valid_p ())
352 atomic_increment (&cache
->usecnt
);
356 int fd
= __socket (PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
358 if (__glibc_likely (fd
>= 0))
360 struct sockaddr_nl nladdr
;
361 memset (&nladdr
, '\0', sizeof (nladdr
));
362 nladdr
.nl_family
= AF_NETLINK
;
364 socklen_t addr_len
= sizeof (nladdr
);
366 if (__bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
367 && __getsockname (fd
, (struct sockaddr
*) &nladdr
,
369 data
= make_request (fd
, nladdr
.nl_pid
);
371 close_not_cancel_no_status (fd
);
381 __libc_lock_unlock (lock
);
386 *seen_ipv4
= data
->seen_ipv4
;
387 *seen_ipv6
= data
->seen_ipv6
;
388 *in6ailen
= data
->in6ailen
;
389 *in6ai
= data
->in6ai
;
391 if (olddata
!= NULL
&& olddata
->usecnt
> 0
392 && atomic_add_zero (&olddata
->usecnt
, -1))
398 /* We cannot determine what interfaces are available. Be
404 /* Free the cache if it has been allocated. */
405 libc_freeres_fn (freecache
)
408 __free_in6ai (cache
->in6ai
);
412 __free_in6ai (struct in6addrinfo
*ai
)
416 struct cached_data
*data
=
417 (struct cached_data
*) ((char *) ai
418 - offsetof (struct cached_data
, in6ai
));
420 if (atomic_add_zero (&data
->usecnt
, -1))
422 __libc_lock_lock (lock
);
424 if (data
->usecnt
== 0)
428 __libc_lock_unlock (lock
);