Updated to fedora-glibc-20071212T1051
[glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blobd66f029ed9f0e0b5d259e37cf1c474bb0b3c6719
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 2006, 2007 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 <not-cancel.h>
31 #include <kernel-features.h>
33 #include "netlinkaccess.h"
35 #ifndef IFA_F_HOMEADDRESS
36 # define IFA_F_HOMEADDRESS 0
37 #endif
38 #ifndef IFA_F_OPTIMISTIC
39 # define IFA_F_OPTIMISTIC 0
40 #endif
43 static int
44 make_request (int fd, pid_t pid, bool *seen_ipv4, bool *seen_ipv6,
45 struct in6addrinfo **in6ai, size_t *in6ailen)
47 struct req
49 struct nlmsghdr nlh;
50 struct rtgenmsg g;
51 /* struct rtgenmsg consists of a single byte. This means there
52 are three bytes of padding included in the REQ definition.
53 We make them explicit here. */
54 char pad[3];
55 } req;
56 struct sockaddr_nl nladdr;
58 req.nlh.nlmsg_len = sizeof (req);
59 req.nlh.nlmsg_type = RTM_GETADDR;
60 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
61 req.nlh.nlmsg_pid = 0;
62 req.nlh.nlmsg_seq = time (NULL);
63 req.g.rtgen_family = AF_UNSPEC;
65 assert (sizeof (req) - offsetof (struct req, pad) == 3);
66 memset (req.pad, '\0', sizeof (req.pad));
68 memset (&nladdr, '\0', sizeof (nladdr));
69 nladdr.nl_family = AF_NETLINK;
71 #ifdef PAGE_SIZE
72 /* Help the compiler optimize out the malloc call if PAGE_SIZE
73 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
74 const size_t buf_size = PAGE_SIZE;
75 #else
76 const size_t buf_size = __getpagesize ();
77 #endif
78 bool use_malloc = false;
79 char *buf;
81 if (__libc_use_alloca (buf_size))
82 buf = alloca (buf_size);
83 else
85 buf = malloc (buf_size);
86 if (buf != NULL)
87 use_malloc = true;
88 else
89 goto out_fail;
92 struct iovec iov = { buf, buf_size };
94 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
95 (struct sockaddr *) &nladdr,
96 sizeof (nladdr))) < 0)
97 goto out_fail;
99 *seen_ipv4 = false;
100 *seen_ipv6 = false;
102 bool done = false;
103 struct in6ailist
105 struct in6addrinfo info;
106 struct in6ailist *next;
107 } *in6ailist = NULL;
108 size_t in6ailistlen = 0;
112 struct msghdr msg =
114 (void *) &nladdr, sizeof (nladdr),
115 &iov, 1,
116 NULL, 0,
120 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
121 if (read_len < 0)
122 goto out_fail;
124 if (msg.msg_flags & MSG_TRUNC)
125 goto out_fail;
127 struct nlmsghdr *nlmh;
128 for (nlmh = (struct nlmsghdr *) buf;
129 NLMSG_OK (nlmh, (size_t) read_len);
130 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
132 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
133 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
134 continue;
136 if (nlmh->nlmsg_type == RTM_NEWADDR)
138 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
139 struct rtattr *rta = IFA_RTA (ifam);
140 size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
142 if (ifam->ifa_family != AF_INET
143 && ifam->ifa_family != AF_INET6)
144 continue;
146 const void *local = NULL;
147 const void *address = NULL;
148 while (RTA_OK (rta, len))
150 switch (rta->rta_type)
152 case IFA_LOCAL:
153 local = RTA_DATA (rta);
154 break;
156 case IFA_ADDRESS:
157 address = RTA_DATA (rta);
158 goto out;
161 rta = RTA_NEXT (rta, len);
164 if (local != NULL)
166 address = local;
167 out:
168 if (ifam->ifa_family != AF_INET)
170 if (*(const in_addr_t *) address
171 != htonl (INADDR_LOOPBACK))
172 *seen_ipv4 = true;
174 else
176 if (!IN6_IS_ADDR_LOOPBACK (address))
177 *seen_ipv6 = true;
181 struct in6ailist *newp = alloca (sizeof (*newp));
182 newp->info.flags = (((ifam->ifa_flags
183 & (IFA_F_DEPRECATED
184 | IFA_F_OPTIMISTIC))
185 ? in6ai_deprecated : 0)
186 | ((ifam->ifa_flags
187 & IFA_F_HOMEADDRESS)
188 ? in6ai_homeaddress : 0));
189 newp->info.prefixlen = ifam->ifa_prefixlen;
190 newp->info.index = ifam->ifa_index;
191 if (ifam->ifa_family == AF_INET)
193 newp->info.addr[0] = 0;
194 newp->info.addr[1] = 0;
195 newp->info.addr[2] = htonl (0xffff);
196 newp->info.addr[3] = *(const in_addr_t *) address;
198 else
199 memcpy (newp->info.addr, address, sizeof (newp->info.addr));
200 newp->next = in6ailist;
201 in6ailist = newp;
202 ++in6ailistlen;
204 else if (nlmh->nlmsg_type == NLMSG_DONE)
205 /* We found the end, leave the loop. */
206 done = true;
209 while (! done);
211 close_not_cancel_no_status (fd);
213 if (*seen_ipv6 && in6ailist != NULL)
215 *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
216 if (*in6ai == NULL)
217 goto out_fail;
219 *in6ailen = in6ailistlen;
223 (*in6ai)[--in6ailistlen] = in6ailist->info;
224 in6ailist = in6ailist->next;
226 while (in6ailist != NULL);
229 if (use_malloc)
230 free (buf);
231 return 0;
233 out_fail:
234 if (use_malloc)
235 free (buf);
236 return -1;
240 /* We don't know if we have NETLINK support compiled in in our
241 Kernel. */
242 #if __ASSUME_NETLINK_SUPPORT == 0
243 /* Define in ifaddrs.h. */
244 extern int __no_netlink_support attribute_hidden;
245 #else
246 # define __no_netlink_support 0
247 #endif
250 void
251 attribute_hidden
252 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
253 struct in6addrinfo **in6ai, size_t *in6ailen)
255 *in6ai = NULL;
256 *in6ailen = 0;
258 if (! __no_netlink_support)
260 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
262 struct sockaddr_nl nladdr;
263 memset (&nladdr, '\0', sizeof (nladdr));
264 nladdr.nl_family = AF_NETLINK;
266 socklen_t addr_len = sizeof (nladdr);
268 if (fd >= 0
269 && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
270 && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
271 && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
272 in6ai, in6ailen) == 0)
273 /* It worked. */
274 return;
276 if (fd >= 0)
277 __close (fd);
279 #if __ASSUME_NETLINK_SUPPORT == 0
280 /* Remember that there is no netlink support. */
281 __no_netlink_support = 1;
282 #else
283 /* We cannot determine what interfaces are available. Be
284 pessimistic. */
285 *seen_ipv4 = true;
286 *seen_ipv6 = true;
287 #endif
290 #if __ASSUME_NETLINK_SUPPORT == 0
291 /* No netlink. Get the interface list via getifaddrs. */
292 struct ifaddrs *ifa = NULL;
293 if (getifaddrs (&ifa) != 0)
295 /* We cannot determine what interfaces are available. Be
296 pessimistic. */
297 *seen_ipv4 = true;
298 *seen_ipv6 = true;
299 return;
302 struct ifaddrs *runp;
303 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
304 if (runp->ifa_addr->sa_family == PF_INET)
305 *seen_ipv4 = true;
306 else if (runp->ifa_addr->sa_family == PF_INET6)
307 *seen_ipv6 = true;
309 (void) freeifaddrs (ifa);
310 #endif