Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blob8bc025b04129d29fddcdf619a38141e43350b348
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003-2013 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 <sys/socket.h>
29 #include <asm/types.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
33 #include <not-cancel.h>
34 #include <kernel-features.h>
35 #include <bits/libc-lock.h>
36 #include <atomic.h>
37 #include <nscd/nscd-client.h>
40 #ifndef IFA_F_HOMEADDRESS
41 # define IFA_F_HOMEADDRESS 0
42 #endif
43 #ifndef IFA_F_OPTIMISTIC
44 # define IFA_F_OPTIMISTIC 0
45 #endif
48 struct cached_data
50 uint32_t timestamp;
51 uint32_t usecnt;
52 bool seen_ipv4;
53 bool seen_ipv6;
54 size_t in6ailen;
55 struct in6addrinfo in6ai[0];
58 static struct cached_data noai6ai_cached =
60 .usecnt = 1, /* Make sure we never try to delete this entry. */
61 .in6ailen = 0
64 libc_freeres_ptr (static struct cached_data *cache);
65 __libc_lock_define_initialized (static, lock);
68 #ifdef IS_IN_nscd
69 static uint32_t nl_timestamp;
71 uint32_t
72 __bump_nl_timestamp (void)
74 if (atomic_increment_val (&nl_timestamp) == 0)
75 atomic_increment (&nl_timestamp);
77 return nl_timestamp;
79 #endif
81 static inline uint32_t
82 get_nl_timestamp (void)
84 #ifdef IS_IN_nscd
85 return nl_timestamp;
86 #elif defined USE_NSCD
87 return __nscd_get_nl_timestamp ();
88 #else
89 return 0;
90 #endif
93 static inline bool
94 cache_valid_p (void)
96 if (cache != NULL)
98 uint32_t timestamp = get_nl_timestamp ();
99 return timestamp != 0 && cache->timestamp == timestamp;
101 return false;
105 static struct cached_data *
106 make_request (int fd, pid_t pid)
108 struct req
110 struct nlmsghdr nlh;
111 struct rtgenmsg g;
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. */
115 char pad[3];
116 } req;
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;
132 #ifdef PAGE_SIZE
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;
136 #else
137 const size_t buf_size = __getpagesize ();
138 #endif
139 bool use_malloc = false;
140 char *buf;
142 if (__libc_use_alloca (buf_size))
143 buf = alloca (buf_size);
144 else
146 buf = malloc (buf_size);
147 if (buf != NULL)
148 use_malloc = true;
149 else
150 goto out_fail;
153 struct iovec iov = { buf, buf_size };
155 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
156 (struct sockaddr *) &nladdr,
157 sizeof (nladdr))) < 0)
158 goto out_fail;
160 bool done = false;
161 struct in6ailist
163 struct in6addrinfo info;
164 struct in6ailist *next;
165 } *in6ailist = NULL;
166 size_t in6ailistlen = 0;
167 bool seen_ipv4 = false;
168 bool seen_ipv6 = false;
172 struct msghdr msg =
174 (void *) &nladdr, sizeof (nladdr),
175 &iov, 1,
176 NULL, 0,
180 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
181 if (read_len < 0)
182 goto out_fail;
184 if (msg.msg_flags & MSG_TRUNC)
185 goto out_fail;
187 struct nlmsghdr *nlmh;
188 for (nlmh = (struct nlmsghdr *) buf;
189 NLMSG_OK (nlmh, (size_t) read_len);
190 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
192 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
193 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
194 continue;
196 if (nlmh->nlmsg_type == RTM_NEWADDR)
198 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
199 struct rtattr *rta = IFA_RTA (ifam);
200 size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
202 if (ifam->ifa_family != AF_INET
203 && ifam->ifa_family != AF_INET6)
204 continue;
206 const void *local = NULL;
207 const void *address = NULL;
208 while (RTA_OK (rta, len))
210 switch (rta->rta_type)
212 case IFA_LOCAL:
213 local = RTA_DATA (rta);
214 break;
216 case IFA_ADDRESS:
217 address = RTA_DATA (rta);
218 goto out;
221 rta = RTA_NEXT (rta, len);
224 if (local != NULL)
226 address = local;
227 out:
228 if (ifam->ifa_family == AF_INET)
230 if (*(const in_addr_t *) address
231 != htonl (INADDR_LOOPBACK))
232 seen_ipv4 = true;
234 else
236 if (!IN6_IS_ADDR_LOOPBACK (address))
237 seen_ipv6 = true;
241 struct in6ailist *newp = alloca (sizeof (*newp));
242 newp->info.flags = (((ifam->ifa_flags
243 & (IFA_F_DEPRECATED
244 | IFA_F_OPTIMISTIC))
245 ? in6ai_deprecated : 0)
246 | ((ifam->ifa_flags
247 & IFA_F_HOMEADDRESS)
248 ? in6ai_homeaddress : 0));
249 newp->info.prefixlen = ifam->ifa_prefixlen;
250 newp->info.index = ifam->ifa_index;
251 if (ifam->ifa_family == AF_INET)
253 newp->info.addr[0] = 0;
254 newp->info.addr[1] = 0;
255 newp->info.addr[2] = htonl (0xffff);
256 newp->info.addr[3] = *(const in_addr_t *) address;
258 else
259 memcpy (newp->info.addr, address, sizeof (newp->info.addr));
260 newp->next = in6ailist;
261 in6ailist = newp;
262 ++in6ailistlen;
264 else if (nlmh->nlmsg_type == NLMSG_DONE)
265 /* We found the end, leave the loop. */
266 done = true;
269 while (! done);
271 struct cached_data *result;
272 if (seen_ipv6 && in6ailist != NULL)
274 result = malloc (sizeof (*result)
275 + in6ailistlen * sizeof (struct in6addrinfo));
276 if (result == NULL)
277 goto out_fail;
279 result->timestamp = get_nl_timestamp ();
280 result->usecnt = 2;
281 result->seen_ipv4 = seen_ipv4;
282 result->seen_ipv6 = true;
283 result->in6ailen = in6ailistlen;
287 result->in6ai[--in6ailistlen] = in6ailist->info;
288 in6ailist = in6ailist->next;
290 while (in6ailist != NULL);
292 else
294 atomic_add (&noai6ai_cached.usecnt, 2);
295 noai6ai_cached.seen_ipv4 = seen_ipv4;
296 noai6ai_cached.seen_ipv6 = seen_ipv6;
297 result = &noai6ai_cached;
300 if (use_malloc)
301 free (buf);
302 return result;
304 out_fail:
305 if (use_malloc)
306 free (buf);
307 return NULL;
311 void
312 attribute_hidden
313 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
314 struct in6addrinfo **in6ai, size_t *in6ailen)
316 *in6ai = NULL;
317 *in6ailen = 0;
319 struct cached_data *olddata = NULL;
320 struct cached_data *data = NULL;
322 __libc_lock_lock (lock);
324 if (cache_valid_p ())
326 data = cache;
327 atomic_increment (&cache->usecnt);
329 else
331 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
333 if (__builtin_expect (fd >= 0, 1))
335 struct sockaddr_nl nladdr;
336 memset (&nladdr, '\0', sizeof (nladdr));
337 nladdr.nl_family = AF_NETLINK;
339 socklen_t addr_len = sizeof (nladdr);
341 if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
342 && __getsockname (fd, (struct sockaddr *) &nladdr,
343 &addr_len) == 0)
344 data = make_request (fd, nladdr.nl_pid);
346 close_not_cancel_no_status (fd);
349 if (data != NULL)
351 olddata = cache;
352 cache = data;
356 __libc_lock_unlock (lock);
358 if (data != NULL)
360 /* It worked. */
361 *seen_ipv4 = data->seen_ipv4;
362 *seen_ipv6 = data->seen_ipv6;
363 *in6ailen = data->in6ailen;
364 *in6ai = data->in6ai;
366 if (olddata != NULL && olddata->usecnt > 0
367 && atomic_add_zero (&olddata->usecnt, -1))
368 free (olddata);
370 return;
373 /* We cannot determine what interfaces are available. Be
374 pessimistic. */
375 *seen_ipv4 = true;
376 *seen_ipv6 = true;
380 void
381 __free_in6ai (struct in6addrinfo *ai)
383 if (ai != NULL)
385 struct cached_data *data =
386 (struct cached_data *) ((char *) ai
387 - offsetof (struct cached_data, in6ai));
389 if (atomic_add_zero (&data->usecnt, -1))
391 __libc_lock_lock (lock);
393 if (data->usecnt == 0)
394 /* Still unused. */
395 free (data);
397 __libc_lock_unlock (lock);