2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blobe694342998460d9715936a7a04d6d6533e9ef246
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 2006, 2007, 2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <ifaddrs.h>
23 #include <netdb.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.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>
38 #ifndef IFA_F_HOMEADDRESS
39 # define IFA_F_HOMEADDRESS 0
40 #endif
41 #ifndef IFA_F_OPTIMISTIC
42 # define IFA_F_OPTIMISTIC 0
43 #endif
46 static int
47 make_request (int fd, pid_t pid, bool *seen_ipv4, bool *seen_ipv6,
48 struct in6addrinfo **in6ai, size_t *in6ailen)
50 struct req
52 struct nlmsghdr nlh;
53 struct rtgenmsg g;
54 /* struct rtgenmsg consists of a single byte. This means there
55 are three bytes of padding included in the REQ definition.
56 We make them explicit here. */
57 char pad[3];
58 } req;
59 struct sockaddr_nl nladdr;
61 req.nlh.nlmsg_len = sizeof (req);
62 req.nlh.nlmsg_type = RTM_GETADDR;
63 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
64 req.nlh.nlmsg_pid = 0;
65 req.nlh.nlmsg_seq = time (NULL);
66 req.g.rtgen_family = AF_UNSPEC;
68 assert (sizeof (req) - offsetof (struct req, pad) == 3);
69 memset (req.pad, '\0', sizeof (req.pad));
71 memset (&nladdr, '\0', sizeof (nladdr));
72 nladdr.nl_family = AF_NETLINK;
74 #ifdef PAGE_SIZE
75 /* Help the compiler optimize out the malloc call if PAGE_SIZE
76 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
77 const size_t buf_size = PAGE_SIZE;
78 #else
79 const size_t buf_size = __getpagesize ();
80 #endif
81 bool use_malloc = false;
82 char *buf;
84 if (__libc_use_alloca (buf_size))
85 buf = alloca (buf_size);
86 else
88 buf = malloc (buf_size);
89 if (buf != NULL)
90 use_malloc = true;
91 else
92 goto out_fail;
95 struct iovec iov = { buf, buf_size };
97 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
98 (struct sockaddr *) &nladdr,
99 sizeof (nladdr))) < 0)
100 goto out_fail;
102 *seen_ipv4 = false;
103 *seen_ipv6 = false;
105 bool done = false;
106 struct in6ailist
108 struct in6addrinfo info;
109 struct in6ailist *next;
110 } *in6ailist = NULL;
111 size_t in6ailistlen = 0;
115 struct msghdr msg =
117 (void *) &nladdr, sizeof (nladdr),
118 &iov, 1,
119 NULL, 0,
123 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
124 if (read_len < 0)
125 goto out_fail;
127 if (msg.msg_flags & MSG_TRUNC)
128 goto out_fail;
130 struct nlmsghdr *nlmh;
131 for (nlmh = (struct nlmsghdr *) buf;
132 NLMSG_OK (nlmh, (size_t) read_len);
133 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
135 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
136 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
137 continue;
139 if (nlmh->nlmsg_type == RTM_NEWADDR)
141 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
142 struct rtattr *rta = IFA_RTA (ifam);
143 size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
145 if (ifam->ifa_family != AF_INET
146 && ifam->ifa_family != AF_INET6)
147 continue;
149 const void *local = NULL;
150 const void *address = NULL;
151 while (RTA_OK (rta, len))
153 switch (rta->rta_type)
155 case IFA_LOCAL:
156 local = RTA_DATA (rta);
157 break;
159 case IFA_ADDRESS:
160 address = RTA_DATA (rta);
161 goto out;
164 rta = RTA_NEXT (rta, len);
167 if (local != NULL)
169 address = local;
170 out:
171 if (ifam->ifa_family == AF_INET)
173 if (*(const in_addr_t *) address
174 != htonl (INADDR_LOOPBACK))
175 *seen_ipv4 = true;
177 else
179 if (!IN6_IS_ADDR_LOOPBACK (address))
180 *seen_ipv6 = true;
184 struct in6ailist *newp = alloca (sizeof (*newp));
185 newp->info.flags = (((ifam->ifa_flags
186 & (IFA_F_DEPRECATED
187 | IFA_F_OPTIMISTIC))
188 ? in6ai_deprecated : 0)
189 | ((ifam->ifa_flags
190 & IFA_F_HOMEADDRESS)
191 ? in6ai_homeaddress : 0));
192 newp->info.prefixlen = ifam->ifa_prefixlen;
193 newp->info.index = ifam->ifa_index;
194 if (ifam->ifa_family == AF_INET)
196 newp->info.addr[0] = 0;
197 newp->info.addr[1] = 0;
198 newp->info.addr[2] = htonl (0xffff);
199 newp->info.addr[3] = *(const in_addr_t *) address;
201 else
202 memcpy (newp->info.addr, address, sizeof (newp->info.addr));
203 newp->next = in6ailist;
204 in6ailist = newp;
205 ++in6ailistlen;
207 else if (nlmh->nlmsg_type == NLMSG_DONE)
208 /* We found the end, leave the loop. */
209 done = true;
212 while (! done);
214 close_not_cancel_no_status (fd);
216 if (*seen_ipv6 && in6ailist != NULL)
218 *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
219 if (*in6ai == NULL)
220 goto out_fail;
222 *in6ailen = in6ailistlen;
226 (*in6ai)[--in6ailistlen] = in6ailist->info;
227 in6ailist = in6ailist->next;
229 while (in6ailist != NULL);
232 if (use_malloc)
233 free (buf);
234 return 0;
236 out_fail:
237 if (use_malloc)
238 free (buf);
239 return -1;
243 /* We don't know if we have NETLINK support compiled in in our
244 Kernel. */
245 #if __ASSUME_NETLINK_SUPPORT == 0
246 /* Define in ifaddrs.h. */
247 extern int __no_netlink_support attribute_hidden;
248 #else
249 # define __no_netlink_support 0
250 #endif
253 void
254 attribute_hidden
255 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
256 struct in6addrinfo **in6ai, size_t *in6ailen)
258 *in6ai = NULL;
259 *in6ailen = 0;
261 if (! __no_netlink_support)
263 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
265 struct sockaddr_nl nladdr;
266 memset (&nladdr, '\0', sizeof (nladdr));
267 nladdr.nl_family = AF_NETLINK;
269 socklen_t addr_len = sizeof (nladdr);
271 if (fd >= 0
272 && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
273 && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
274 && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
275 in6ai, in6ailen) == 0)
276 /* It worked. */
277 return;
279 if (fd >= 0)
280 __close (fd);
282 #if __ASSUME_NETLINK_SUPPORT == 0
283 /* Remember that there is no netlink support. */
284 __no_netlink_support = 1;
285 #else
286 /* We cannot determine what interfaces are available. Be
287 pessimistic. */
288 *seen_ipv4 = true;
289 *seen_ipv6 = true;
290 #endif
293 #if __ASSUME_NETLINK_SUPPORT == 0
294 /* No netlink. Get the interface list via getifaddrs. */
295 struct ifaddrs *ifa = NULL;
296 if (getifaddrs (&ifa) != 0)
298 /* We cannot determine what interfaces are available. Be
299 pessimistic. */
300 *seen_ipv4 = true;
301 *seen_ipv6 = true;
302 return;
305 struct ifaddrs *runp;
306 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
307 if (runp->ifa_addr->sa_family == PF_INET)
308 *seen_ipv4 = true;
309 else if (runp->ifa_addr->sa_family == PF_INET6)
310 *seen_ipv6 = true;
312 (void) freeifaddrs (ifa);
313 #endif