(make_request): Recognize IFA_F_HOMEADDRESS flag for interfaces.
[glibc/history.git] / sysdeps / unix / sysv / linux / check_pf.c
blob13ccd7acb45ee5f448036e6cb25b331b99384c19
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 <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_TEMPORARY
39 # define IFA_F_TEMPORARY IFA_F_SECONDARY
40 #endif
41 #ifndef IFA_F_HOMEADDRESS
42 # define IFA_F_HOMEADDRESS 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 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
75 (struct sockaddr *) &nladdr,
76 sizeof (nladdr))) < 0)
77 return -1;
79 *seen_ipv4 = false;
80 *seen_ipv6 = false;
82 bool done = false;
83 char buf[4096];
84 struct iovec iov = { buf, sizeof (buf) };
85 struct in6ailist
87 struct in6addrinfo info;
88 struct in6ailist *next;
89 } *in6ailist = NULL;
90 size_t in6ailistlen = 0;
94 struct msghdr msg =
96 (void *) &nladdr, sizeof (nladdr),
97 &iov, 1,
98 NULL, 0,
102 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
103 if (read_len < 0)
104 return -1;
106 if (msg.msg_flags & MSG_TRUNC)
107 return -1;
109 struct nlmsghdr *nlmh;
110 for (nlmh = (struct nlmsghdr *) buf;
111 NLMSG_OK (nlmh, (size_t) read_len);
112 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
114 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
115 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
116 continue;
118 if (nlmh->nlmsg_type == RTM_NEWADDR)
120 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
122 switch (ifam->ifa_family)
124 case AF_INET:
125 *seen_ipv4 = true;
126 break;
127 case AF_INET6:
128 *seen_ipv6 = true;
130 if (ifam->ifa_flags & (IFA_F_DEPRECATED
131 | IFA_F_TEMPORARY
132 | IFA_F_HOMEADDRESS))
134 struct rtattr *rta = IFA_RTA (ifam);
135 size_t len = (nlmh->nlmsg_len
136 - NLMSG_LENGTH (sizeof (*ifam)));
137 void *local = NULL;
138 void *address = NULL;
139 while (RTA_OK (rta, len))
141 switch (rta->rta_type)
143 case IFA_LOCAL:
144 local = RTA_DATA (rta);
145 break;
147 case IFA_ADDRESS:
148 address = RTA_DATA (rta);
149 break;
152 rta = RTA_NEXT (rta, len);
155 struct in6ailist *newp = alloca (sizeof (*newp));
156 newp->info.flags = (((ifam->ifa_flags & IFA_F_DEPRECATED)
157 ? in6ai_deprecated : 0)
158 | ((ifam->ifa_flags
159 & IFA_F_TEMPORARY)
160 ? in6ai_temporary : 0)
161 | ((ifam->ifa_flags
162 & IFA_F_HOMEADDRESS)
163 ? in6ai_homeaddress : 0));
164 memcpy (newp->info.addr, address ?: local,
165 sizeof (newp->info.addr));
166 newp->next = in6ailist;
167 in6ailist = newp;
168 ++in6ailistlen;
170 break;
171 default:
172 /* Ignore. */
173 break;
176 else if (nlmh->nlmsg_type == NLMSG_DONE)
177 /* We found the end, leave the loop. */
178 done = true;
181 while (! done);
183 close_not_cancel_no_status (fd);
185 if (in6ailist != NULL)
187 *in6ai = malloc (in6ailistlen * sizeof (**in6ai));
188 if (*in6ai == NULL)
189 return -1;
191 *in6ailen = in6ailistlen;
195 (*in6ai)[--in6ailistlen] = in6ailist->info;
196 in6ailist = in6ailist->next;
198 while (in6ailist != NULL);
201 return 0;
205 /* We don't know if we have NETLINK support compiled in in our
206 Kernel. */
207 #if __ASSUME_NETLINK_SUPPORT == 0
208 /* Define in ifaddrs.h. */
209 extern int __no_netlink_support attribute_hidden;
210 #else
211 # define __no_netlink_support 0
212 #endif
215 void
216 attribute_hidden
217 __check_pf (bool *seen_ipv4, bool *seen_ipv6,
218 struct in6addrinfo **in6ai, size_t *in6ailen)
220 *in6ai = NULL;
221 *in6ailen = 0;
223 if (! __no_netlink_support)
225 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
227 struct sockaddr_nl nladdr;
228 memset (&nladdr, '\0', sizeof (nladdr));
229 nladdr.nl_family = AF_NETLINK;
231 socklen_t addr_len = sizeof (nladdr);
233 if (fd >= 0
234 && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
235 && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
236 && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6,
237 in6ai, in6ailen) == 0)
238 /* It worked. */
239 return;
241 if (fd >= 0)
242 __close (fd);
244 #if __ASSUME_NETLINK_SUPPORT == 0
245 /* Remember that there is no netlink support. */
246 __no_netlink_support = 1;
247 #else
248 /* We cannot determine what interfaces are available. Be
249 pessimistic. */
250 *seen_ipv4 = true;
251 *seen_ipv6 = true;
252 #endif
255 #if __ASSUME_NETLINK_SUPPORT == 0
256 /* No netlink. Get the interface list via getifaddrs. */
257 struct ifaddrs *ifa = NULL;
258 if (getifaddrs (&ifa) != 0)
260 /* We cannot determine what interfaces are available. Be
261 pessimistic. */
262 *seen_ipv4 = true;
263 *seen_ipv6 = true;
264 return;
267 struct ifaddrs *runp;
268 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
269 if (runp->ifa_addr->sa_family == PF_INET)
270 *seen_ipv4 = true;
271 else if (runp->ifa_addr->sa_family == PF_INET6)
272 *seen_ipv6 = true;
274 (void) freeifaddrs (ifa);
275 #endif