Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blobdd333b4a94d4c94346fb5bf0e5bcdec0140000d6
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/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <ifaddrs.h>
22 #include <netdb.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <stdint.h>
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>
37 #include <atomic.h>
38 #include <nscd/nscd-client.h>
41 #ifndef IFA_F_HOMEADDRESS
42 # define IFA_F_HOMEADDRESS 0
43 #endif
44 #ifndef IFA_F_OPTIMISTIC
45 # define IFA_F_OPTIMISTIC 0
46 #endif
49 struct cached_data
51 uint32_t timestamp;
52 uint32_t usecnt;
53 bool seen_ipv4;
54 bool seen_ipv6;
55 size_t in6ailen;
56 struct in6addrinfo in6ai[0];
59 static struct cached_data noai6ai_cached =
61 .usecnt = 1, /* Make sure we never try to delete this entry. */
62 .in6ailen = 0
65 libc_freeres_ptr (static struct cached_data *cache);
66 __libc_lock_define_initialized (static, lock);
69 #ifdef IS_IN_nscd
70 static uint32_t nl_timestamp;
72 uint32_t
73 __bump_nl_timestamp (void)
75 if (atomic_increment_val (&nl_timestamp) == 0)
76 atomic_increment (&nl_timestamp);
78 return nl_timestamp;
80 #endif
82 static inline uint32_t
83 get_nl_timestamp (void)
85 #ifdef IS_IN_nscd
86 return nl_timestamp;
87 #elif defined USE_NSCD
88 return __nscd_get_nl_timestamp ();
89 #else
90 return 0;
91 #endif
94 static inline bool
95 cache_valid_p (void)
97 if (cache != NULL)
99 uint32_t timestamp = get_nl_timestamp ();
100 return timestamp != 0 && cache->timestamp == timestamp;
102 return false;
106 static struct cached_data *
107 make_request (int fd, pid_t pid)
109 struct req
111 struct nlmsghdr nlh;
112 struct rtgenmsg g;
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. */
116 char pad[3];
117 } req;
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;
133 #ifdef PAGE_SIZE
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;
137 #else
138 const size_t buf_size = __getpagesize ();
139 #endif
140 bool use_malloc = false;
141 char *buf;
143 if (__libc_use_alloca (buf_size))
144 buf = alloca (buf_size);
145 else
147 buf = malloc (buf_size);
148 if (buf != NULL)
149 use_malloc = true;
150 else
151 goto out_fail;
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)
159 goto out_fail;
161 bool done = false;
162 struct in6ailist
164 struct in6addrinfo info;
165 struct in6ailist *next;
166 } *in6ailist = NULL;
167 size_t in6ailistlen = 0;
168 bool seen_ipv4 = false;
169 bool seen_ipv6 = false;
173 struct msghdr msg =
175 (void *) &nladdr, sizeof (nladdr),
176 &iov, 1,
177 NULL, 0,
181 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
182 if (read_len < 0)
183 goto out_fail;
185 if (msg.msg_flags & MSG_TRUNC)
186 goto out_fail;
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)
195 continue;
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)
205 continue;
207 const void *local = NULL;
208 const void *address = NULL;
209 while (RTA_OK (rta, len))
211 switch (rta->rta_type)
213 case IFA_LOCAL:
214 local = RTA_DATA (rta);
215 break;
217 case IFA_ADDRESS:
218 address = RTA_DATA (rta);
219 goto out;
222 rta = RTA_NEXT (rta, len);
225 if (local != NULL)
227 address = local;
228 out:
229 if (ifam->ifa_family == AF_INET)
231 if (*(const in_addr_t *) address
232 != htonl (INADDR_LOOPBACK))
233 seen_ipv4 = true;
235 else
237 if (!IN6_IS_ADDR_LOOPBACK (address))
238 seen_ipv6 = true;
242 struct in6ailist *newp = alloca (sizeof (*newp));
243 newp->info.flags = (((ifam->ifa_flags
244 & (IFA_F_DEPRECATED
245 | IFA_F_OPTIMISTIC))
246 ? in6ai_deprecated : 0)
247 | ((ifam->ifa_flags
248 & IFA_F_HOMEADDRESS)
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;
259 else
260 memcpy (newp->info.addr, address, sizeof (newp->info.addr));
261 newp->next = in6ailist;
262 in6ailist = newp;
263 ++in6ailistlen;
265 else if (nlmh->nlmsg_type == NLMSG_DONE)
266 /* We found the end, leave the loop. */
267 done = true;
270 while (! done);
272 struct cached_data *result;
273 if (seen_ipv6 && in6ailist != NULL)
275 result = malloc (sizeof (*result)
276 + in6ailistlen * sizeof (struct in6addrinfo));
277 if (result == NULL)
278 goto out_fail;
280 result->timestamp = get_nl_timestamp ();
281 result->usecnt = 2;
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);
293 else
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;
301 if (use_malloc)
302 free (buf);
303 return result;
305 out_fail:
306 if (use_malloc)
307 free (buf);
308 return NULL;
312 void
313 attribute_hidden
314 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
315 struct in6addrinfo **in6ai, size_t *in6ailen)
317 *in6ai = NULL;
318 *in6ailen = 0;
320 struct cached_data *olddata = NULL;
321 struct cached_data *data = NULL;
323 __libc_lock_lock (lock);
325 if (cache_valid_p ())
327 data = cache;
328 atomic_increment (&cache->usecnt);
330 else
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,
344 &addr_len) == 0)
345 data = make_request (fd, nladdr.nl_pid);
347 close_not_cancel_no_status (fd);
350 if (data != NULL)
352 olddata = cache;
353 cache = data;
357 __libc_lock_unlock (lock);
359 if (data != NULL)
361 /* It worked. */
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))
369 free (olddata);
371 return;
374 /* We cannot determine what interfaces are available. Be
375 pessimistic. */
376 *seen_ipv4 = true;
377 *seen_ipv6 = true;
381 void
382 __free_in6ai (struct in6addrinfo *ai)
384 if (ai != NULL)
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)
395 /* Still unused. */
396 free (data);
398 __libc_lock_unlock (lock);