* sysdeps/unix/sysv/linux/netlinkaccess.h: Include linux/if_addr.h
[glibc/history.git] / sysdeps / unix / sysv / linux / check_pf.c
blobebe4d699b17035c584439005938bbc710353f7ea
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 2006 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_TEMPORARY
36 # define IFA_F_TEMPORARY IFA_F_SECONDARY
37 #endif
38 #ifndef IFA_F_HOMEADDRESS
39 # define IFA_F_HOMEADDRESS 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 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
72 (struct sockaddr *) &nladdr,
73 sizeof (nladdr))) < 0)
74 return -1;
76 *seen_ipv4 = false;
77 *seen_ipv6 = false;
79 bool done = false;
80 char buf[4096];
81 struct iovec iov = { buf, sizeof (buf) };
82 struct in6ailist
84 struct in6addrinfo info;
85 struct in6ailist *next;
86 } *in6ailist = NULL;
87 size_t in6ailistlen = 0;
91 struct msghdr msg =
93 (void *) &nladdr, sizeof (nladdr),
94 &iov, 1,
95 NULL, 0,
99 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
100 if (read_len < 0)
101 return -1;
103 if (msg.msg_flags & MSG_TRUNC)
104 return -1;
106 struct nlmsghdr *nlmh;
107 for (nlmh = (struct nlmsghdr *) buf;
108 NLMSG_OK (nlmh, (size_t) read_len);
109 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
111 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
112 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
113 continue;
115 if (nlmh->nlmsg_type == RTM_NEWADDR)
117 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
119 switch (ifam->ifa_family)
121 case AF_INET:
122 *seen_ipv4 = true;
123 break;
124 case AF_INET6:
125 *seen_ipv6 = true;
127 if (ifam->ifa_flags & (IFA_F_DEPRECATED
128 | IFA_F_TEMPORARY
129 | IFA_F_HOMEADDRESS))
131 struct rtattr *rta = IFA_RTA (ifam);
132 size_t len = (nlmh->nlmsg_len
133 - NLMSG_LENGTH (sizeof (*ifam)));
134 void *local = NULL;
135 void *address = NULL;
136 while (RTA_OK (rta, len))
138 switch (rta->rta_type)
140 case IFA_LOCAL:
141 local = RTA_DATA (rta);
142 break;
144 case IFA_ADDRESS:
145 address = RTA_DATA (rta);
146 break;
149 rta = RTA_NEXT (rta, len);
152 struct in6ailist *newp = alloca (sizeof (*newp));
153 newp->info.flags = (((ifam->ifa_flags & IFA_F_DEPRECATED)
154 ? in6ai_deprecated : 0)
155 | ((ifam->ifa_flags
156 & IFA_F_TEMPORARY)
157 ? in6ai_temporary : 0)
158 | ((ifam->ifa_flags
159 & IFA_F_HOMEADDRESS)
160 ? in6ai_homeaddress : 0));
161 memcpy (newp->info.addr, address ?: local,
162 sizeof (newp->info.addr));
163 newp->next = in6ailist;
164 in6ailist = newp;
165 ++in6ailistlen;
167 break;
168 default:
169 /* Ignore. */
170 break;
173 else if (nlmh->nlmsg_type == NLMSG_DONE)
174 /* We found the end, leave the loop. */
175 done = true;
178 while (! done);
180 close_not_cancel_no_status (fd);
182 if (in6ailist != NULL)
184 *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
185 if (*in6ai == NULL)
186 return -1;
188 *in6ailen = in6ailistlen;
192 (*in6ai)[--in6ailistlen] = in6ailist->info;
193 in6ailist = in6ailist->next;
195 while (in6ailist != NULL);
198 return 0;
202 /* We don't know if we have NETLINK support compiled in in our
203 Kernel. */
204 #if __ASSUME_NETLINK_SUPPORT == 0
205 /* Define in ifaddrs.h. */
206 extern int __no_netlink_support attribute_hidden;
207 #else
208 # define __no_netlink_support 0
209 #endif
212 void
213 attribute_hidden
214 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
215 struct in6addrinfo **in6ai, size_t *in6ailen)
217 *in6ai = NULL;
218 *in6ailen = 0;
220 if (! __no_netlink_support)
222 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
224 struct sockaddr_nl nladdr;
225 memset (&nladdr, '\0', sizeof (nladdr));
226 nladdr.nl_family = AF_NETLINK;
228 socklen_t addr_len = sizeof (nladdr);
230 if (fd >= 0
231 && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
232 && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
233 && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
234 in6ai, in6ailen) == 0)
235 /* It worked. */
236 return;
238 if (fd >= 0)
239 __close (fd);
241 #if __ASSUME_NETLINK_SUPPORT == 0
242 /* Remember that there is no netlink support. */
243 __no_netlink_support = 1;
244 #else
245 /* We cannot determine what interfaces are available. Be
246 pessimistic. */
247 *seen_ipv4 = true;
248 *seen_ipv6 = true;
249 #endif
252 #if __ASSUME_NETLINK_SUPPORT == 0
253 /* No netlink. Get the interface list via getifaddrs. */
254 struct ifaddrs *ifa = NULL;
255 if (getifaddrs (&ifa) != 0)
257 /* We cannot determine what interfaces are available. Be
258 pessimistic. */
259 *seen_ipv4 = true;
260 *seen_ipv6 = true;
261 return;
264 struct ifaddrs *runp;
265 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
266 if (runp->ifa_addr->sa_family == PF_INET)
267 *seen_ipv4 = true;
268 else if (runp->ifa_addr->sa_family == PF_INET6)
269 *seen_ipv6 = true;
271 (void) freeifaddrs (ifa);
272 #endif