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 <kernel-features.h>
36 #include <bits/libc-lock.h>
38 #include <nscd/nscd-client.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 libc_freeres_ptr (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
)
113 /* struct rtgenmsg consists of a single byte. This means there
114 are three bytes of padding included in the REQ definition.
115 We make them explicit here. */
118 struct sockaddr_nl nladdr
;
120 req
.nlh
.nlmsg_len
= sizeof (req
);
121 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
122 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
123 req
.nlh
.nlmsg_pid
= 0;
124 req
.nlh
.nlmsg_seq
= time (NULL
);
125 req
.g
.rtgen_family
= AF_UNSPEC
;
127 assert (sizeof (req
) - offsetof (struct req
, pad
) == 3);
128 memset (req
.pad
, '\0', sizeof (req
.pad
));
130 memset (&nladdr
, '\0', sizeof (nladdr
));
131 nladdr
.nl_family
= AF_NETLINK
;
134 /* Help the compiler optimize out the malloc call if PAGE_SIZE
135 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
136 const size_t buf_size
= PAGE_SIZE
;
138 const size_t buf_size
= __getpagesize ();
140 bool use_malloc
= false;
143 if (__libc_use_alloca (buf_size
))
144 buf
= alloca (buf_size
);
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
;
167 size_t in6ailistlen
= 0;
168 bool seen_ipv4
= false;
169 bool seen_ipv6
= false;
175 (void *) &nladdr
, sizeof (nladdr
),
181 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
185 if (msg
.msg_flags
& MSG_TRUNC
)
188 struct nlmsghdr
*nlmh
;
189 for (nlmh
= (struct nlmsghdr
*) buf
;
190 NLMSG_OK (nlmh
, (size_t) read_len
);
191 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
193 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
194 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
197 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
199 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
200 struct rtattr
*rta
= IFA_RTA (ifam
);
201 size_t len
= nlmh
->nlmsg_len
- NLMSG_LENGTH (sizeof (*ifam
));
203 if (ifam
->ifa_family
!= AF_INET
204 && ifam
->ifa_family
!= AF_INET6
)
207 const void *local
= NULL
;
208 const void *address
= NULL
;
209 while (RTA_OK (rta
, len
))
211 switch (rta
->rta_type
)
214 local
= RTA_DATA (rta
);
218 address
= RTA_DATA (rta
);
222 rta
= RTA_NEXT (rta
, len
);
229 if (ifam
->ifa_family
== AF_INET
)
231 if (*(const in_addr_t
*) address
232 != htonl (INADDR_LOOPBACK
))
237 if (!IN6_IS_ADDR_LOOPBACK (address
))
242 struct in6ailist
*newp
= alloca (sizeof (*newp
));
243 newp
->info
.flags
= (((ifam
->ifa_flags
246 ? in6ai_deprecated
: 0)
249 ? in6ai_homeaddress
: 0));
250 newp
->info
.prefixlen
= ifam
->ifa_prefixlen
;
251 newp
->info
.index
= ifam
->ifa_index
;
252 if (ifam
->ifa_family
== AF_INET
)
254 newp
->info
.addr
[0] = 0;
255 newp
->info
.addr
[1] = 0;
256 newp
->info
.addr
[2] = htonl (0xffff);
257 newp
->info
.addr
[3] = *(const in_addr_t
*) address
;
260 memcpy (newp
->info
.addr
, address
, sizeof (newp
->info
.addr
));
261 newp
->next
= in6ailist
;
265 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
266 /* We found the end, leave the loop. */
272 struct cached_data
*result
;
273 if (seen_ipv6
&& in6ailist
!= NULL
)
275 result
= malloc (sizeof (*result
)
276 + in6ailistlen
* sizeof (struct in6addrinfo
));
280 result
->timestamp
= get_nl_timestamp ();
282 result
->seen_ipv4
= seen_ipv4
;
283 result
->seen_ipv6
= true;
284 result
->in6ailen
= in6ailistlen
;
288 result
->in6ai
[--in6ailistlen
] = in6ailist
->info
;
289 in6ailist
= in6ailist
->next
;
291 while (in6ailist
!= NULL
);
295 atomic_add (&noai6ai_cached
.usecnt
, 2);
296 noai6ai_cached
.seen_ipv4
= seen_ipv4
;
297 noai6ai_cached
.seen_ipv6
= seen_ipv6
;
298 result
= &noai6ai_cached
;
314 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
,
315 struct in6addrinfo
**in6ai
, size_t *in6ailen
)
320 struct cached_data
*olddata
= NULL
;
321 struct cached_data
*data
= NULL
;
323 __libc_lock_lock (lock
);
325 if (cache_valid_p ())
328 atomic_increment (&cache
->usecnt
);
332 int fd
= __socket (PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
334 if (__builtin_expect (fd
>= 0, 1))
336 struct sockaddr_nl nladdr
;
337 memset (&nladdr
, '\0', sizeof (nladdr
));
338 nladdr
.nl_family
= AF_NETLINK
;
340 socklen_t addr_len
= sizeof (nladdr
);
342 if (__bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
343 && __getsockname (fd
, (struct sockaddr
*) &nladdr
,
345 data
= make_request (fd
, nladdr
.nl_pid
);
347 close_not_cancel_no_status (fd
);
357 __libc_lock_unlock (lock
);
362 *seen_ipv4
= data
->seen_ipv4
;
363 *seen_ipv6
= data
->seen_ipv6
;
364 *in6ailen
= data
->in6ailen
;
365 *in6ai
= data
->in6ai
;
367 if (olddata
!= NULL
&& olddata
->usecnt
> 0
368 && atomic_add_zero (&olddata
->usecnt
, -1))
374 /* We cannot determine what interfaces are available. Be
382 __free_in6ai (struct in6addrinfo
*ai
)
386 struct cached_data
*data
=
387 (struct cached_data
*) ((char *) ai
388 - offsetof (struct cached_data
, in6ai
));
390 if (atomic_add_zero (&data
->usecnt
, -1))
392 __libc_lock_lock (lock
);
394 if (data
->usecnt
== 0)
398 __libc_lock_unlock (lock
);