Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blob7d839062d43c1711e8a61eb9d81d2088f5e6c833
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 2006-2008, 2010, 2011 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 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
82 static struct cached_data *
83 make_request (int fd, pid_t pid)
85 struct req
87 struct nlmsghdr nlh;
88 struct rtgenmsg g;
89 /* struct rtgenmsg consists of a single byte. This means there
90 are three bytes of padding included in the REQ definition.
91 We make them explicit here. */
92 char pad[3];
93 } req;
94 struct sockaddr_nl nladdr;
96 req.nlh.nlmsg_len = sizeof (req);
97 req.nlh.nlmsg_type = RTM_GETADDR;
98 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
99 req.nlh.nlmsg_pid = 0;
100 req.nlh.nlmsg_seq = time (NULL);
101 req.g.rtgen_family = AF_UNSPEC;
103 assert (sizeof (req) - offsetof (struct req, pad) == 3);
104 memset (req.pad, '\0', sizeof (req.pad));
106 memset (&nladdr, '\0', sizeof (nladdr));
107 nladdr.nl_family = AF_NETLINK;
109 #ifdef PAGE_SIZE
110 /* Help the compiler optimize out the malloc call if PAGE_SIZE
111 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
112 const size_t buf_size = PAGE_SIZE;
113 #else
114 const size_t buf_size = __getpagesize ();
115 #endif
116 bool use_malloc = false;
117 char *buf;
119 if (__libc_use_alloca (buf_size))
120 buf = alloca (buf_size);
121 else
123 buf = malloc (buf_size);
124 if (buf != NULL)
125 use_malloc = true;
126 else
127 goto out_fail;
130 struct iovec iov = { buf, buf_size };
132 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
133 (struct sockaddr *) &nladdr,
134 sizeof (nladdr))) < 0)
135 goto out_fail;
137 bool done = false;
138 struct in6ailist
140 struct in6addrinfo info;
141 struct in6ailist *next;
142 } *in6ailist = NULL;
143 size_t in6ailistlen = 0;
144 bool seen_ipv4 = false;
145 bool seen_ipv6 = false;
149 struct msghdr msg =
151 (void *) &nladdr, sizeof (nladdr),
152 &iov, 1,
153 NULL, 0,
157 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
158 if (read_len < 0)
159 goto out_fail;
161 if (msg.msg_flags & MSG_TRUNC)
162 goto out_fail;
164 struct nlmsghdr *nlmh;
165 for (nlmh = (struct nlmsghdr *) buf;
166 NLMSG_OK (nlmh, (size_t) read_len);
167 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
169 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
170 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
171 continue;
173 if (nlmh->nlmsg_type == RTM_NEWADDR)
175 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
176 struct rtattr *rta = IFA_RTA (ifam);
177 size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
179 if (ifam->ifa_family != AF_INET
180 && ifam->ifa_family != AF_INET6)
181 continue;
183 const void *local = NULL;
184 const void *address = NULL;
185 while (RTA_OK (rta, len))
187 switch (rta->rta_type)
189 case IFA_LOCAL:
190 local = RTA_DATA (rta);
191 break;
193 case IFA_ADDRESS:
194 address = RTA_DATA (rta);
195 goto out;
198 rta = RTA_NEXT (rta, len);
201 if (local != NULL)
203 address = local;
204 out:
205 if (ifam->ifa_family == AF_INET)
207 if (*(const in_addr_t *) address
208 != htonl (INADDR_LOOPBACK))
209 seen_ipv4 = true;
211 else
213 if (!IN6_IS_ADDR_LOOPBACK (address))
214 seen_ipv6 = true;
218 struct in6ailist *newp = alloca (sizeof (*newp));
219 newp->info.flags = (((ifam->ifa_flags
220 & (IFA_F_DEPRECATED
221 | IFA_F_OPTIMISTIC))
222 ? in6ai_deprecated : 0)
223 | ((ifam->ifa_flags
224 & IFA_F_HOMEADDRESS)
225 ? in6ai_homeaddress : 0));
226 newp->info.prefixlen = ifam->ifa_prefixlen;
227 newp->info.index = ifam->ifa_index;
228 if (ifam->ifa_family == AF_INET)
230 newp->info.addr[0] = 0;
231 newp->info.addr[1] = 0;
232 newp->info.addr[2] = htonl (0xffff);
233 newp->info.addr[3] = *(const in_addr_t *) address;
235 else
236 memcpy (newp->info.addr, address, sizeof (newp->info.addr));
237 newp->next = in6ailist;
238 in6ailist = newp;
239 ++in6ailistlen;
241 else if (nlmh->nlmsg_type == NLMSG_DONE)
242 /* We found the end, leave the loop. */
243 done = true;
246 while (! done);
248 struct cached_data *result;
249 if (seen_ipv6 && in6ailist != NULL)
251 result = malloc (sizeof (*result)
252 + in6ailistlen * sizeof (struct in6addrinfo));
253 if (result == NULL)
254 goto out_fail;
256 #ifdef IS_IN_nscd
257 result->timestamp = nl_timestamp;
258 #else
259 result->timestamp = __nscd_get_nl_timestamp ();
260 #endif
261 result->usecnt = 2;
262 result->seen_ipv4 = seen_ipv4;
263 result->seen_ipv6 = true;
264 result->in6ailen = in6ailistlen;
268 result->in6ai[--in6ailistlen] = in6ailist->info;
269 in6ailist = in6ailist->next;
271 while (in6ailist != NULL);
273 else
275 atomic_add (&noai6ai_cached.usecnt, 2);
276 noai6ai_cached.seen_ipv4 = seen_ipv4;
277 noai6ai_cached.seen_ipv6 = seen_ipv6;
278 result = &noai6ai_cached;
281 if (use_malloc)
282 free (buf);
283 return result;
285 out_fail:
286 if (use_malloc)
287 free (buf);
288 return NULL;
292 /* We don't know if we have NETLINK support compiled into our
293 Kernel. */
294 #if __ASSUME_NETLINK_SUPPORT == 0
295 /* Define in ifaddrs.h. */
296 extern int __no_netlink_support attribute_hidden;
297 #else
298 # define __no_netlink_support 0
299 #endif
302 void
303 attribute_hidden
304 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
305 struct in6addrinfo **in6ai, size_t *in6ailen)
307 *in6ai = NULL;
308 *in6ailen = 0;
310 if (! __no_netlink_support)
312 struct cached_data *olddata = NULL;
313 struct cached_data *data = NULL;
315 __libc_lock_lock (lock);
317 #ifdef IS_IN_nscd
318 # define cache_valid() nl_timestamp != 0 && cache->timestamp == nl_timestamp
319 #else
320 # define cache_valid() \
321 ({ uint32_t val = __nscd_get_nl_timestamp (); \
322 val != 0 && cache->timestamp == val; })
323 #endif
324 if (cache != NULL && cache_valid ())
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 #if __ASSUME_NETLINK_SUPPORT == 0
374 /* Remember that there is no netlink support. */
375 __no_netlink_support = 1;
376 #else
377 /* We cannot determine what interfaces are available. Be
378 pessimistic. */
379 *seen_ipv4 = true;
380 *seen_ipv6 = true;
381 #endif
384 #if __ASSUME_NETLINK_SUPPORT == 0
385 /* No netlink. Get the interface list via getifaddrs. */
386 struct ifaddrs *ifa = NULL;
387 if (getifaddrs (&ifa) != 0)
389 /* We cannot determine what interfaces are available. Be
390 pessimistic. */
391 *seen_ipv4 = true;
392 *seen_ipv6 = true;
393 return;
396 struct ifaddrs *runp;
397 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
398 if (runp->ifa_addr != NULL)
400 if (runp->ifa_addr->sa_family == PF_INET)
401 *seen_ipv4 = true;
402 else if (runp->ifa_addr->sa_family == PF_INET6)
403 *seen_ipv6 = true;
406 (void) freeifaddrs (ifa);
407 #endif
411 void
412 __free_in6ai (struct in6addrinfo *ai)
414 if (ai != NULL)
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)
425 /* Still unused. */
426 free (data);
428 __libc_lock_unlock (lock);