Updated to fedora-glibc-20041018T0940
[glibc.git] / sysdeps / unix / sysv / linux / check_pf.c
blob69924769bc8d8989bc32d6ffa76328524a04b262
1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003 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 <errno.h>
21 #include <ifaddrs.h>
22 #include <netdb.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
28 #include <asm/types.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
32 #include "kernel-features.h"
35 static int
36 make_request (int fd, pid_t pid, bool *seen_ipv4, bool *seen_ipv6)
38 struct
40 struct nlmsghdr nlh;
41 struct rtgenmsg g;
42 } req;
43 struct sockaddr_nl nladdr;
45 req.nlh.nlmsg_len = sizeof (req);
46 req.nlh.nlmsg_type = RTM_GETADDR;
47 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
48 req.nlh.nlmsg_pid = 0;
49 req.nlh.nlmsg_seq = time (NULL);
50 req.g.rtgen_family = AF_UNSPEC;
52 memset (&nladdr, '\0', sizeof (nladdr));
53 nladdr.nl_family = AF_NETLINK;
55 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
56 (struct sockaddr *) &nladdr,
57 sizeof (nladdr))) < 0)
58 return -1;
60 *seen_ipv4 = false;
61 *seen_ipv6 = false;
63 bool done = false;
64 char buf[4096];
65 struct iovec iov = { buf, sizeof (buf) };
69 struct msghdr msg =
71 (void *) &nladdr, sizeof (nladdr),
72 &iov, 1,
73 NULL, 0,
77 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
78 if (read_len < 0)
79 return -1;
81 if (msg.msg_flags & MSG_TRUNC)
82 return -1;
84 struct nlmsghdr *nlmh;
85 for (nlmh = (struct nlmsghdr *) buf;
86 NLMSG_OK (nlmh, (size_t) read_len);
87 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
89 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
90 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
91 continue;
93 if (nlmh->nlmsg_type == RTM_NEWADDR)
95 struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
97 switch (ifam->ifa_family)
99 case AF_INET:
100 *seen_ipv4 = true;
101 break;
102 case AF_INET6:
103 *seen_ipv6 = true;
104 break;
105 default:
106 /* Ignore. */
107 break;
110 else if (nlmh->nlmsg_type == NLMSG_DONE)
111 /* We found the end, leave the loop. */
112 done = true;
113 else ;
116 while (! done);
118 __close (fd);
120 return 0;
124 /* We don't know if we have NETLINK support compiled in in our
125 Kernel. */
126 #if __ASSUME_NETLINK_SUPPORT == 0
127 /* Define in ifaddrs.h. */
128 extern int __no_netlink_support attribute_hidden;
129 #else
130 # define __no_netlink_support 0
131 #endif
134 void
135 attribute_hidden
136 __check_pf (bool *seen_ipv4, bool *seen_ipv6)
138 if (! __no_netlink_support)
140 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
142 struct sockaddr_nl nladdr;
143 memset (&nladdr, '\0', sizeof (nladdr));
144 nladdr.nl_family = AF_NETLINK;
146 socklen_t addr_len = sizeof (nladdr);
148 if (fd >= 0
149 && __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
150 && __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) == 0
151 && make_request (fd, nladdr.nl_pid, seen_ipv4, seen_ipv6) == 0)
152 /* It worked. */
153 return;
155 if (fd >= 0)
156 __close (fd);
158 #if __ASSUME_NETLINK_SUPPORT == 0
159 /* Remember that there is no netlink support. */
160 __no_netlink_support = 1;
161 #else
162 /* We cannot determine what interfaces are available. Be
163 pessimistic. */
164 *seen_ipv4 = true;
165 *seen_ipv6 = true;
166 #endif
169 #if __ASSUME_NETLINK_SUPPORT == 0
170 /* No netlink. Get the interface list via getifaddrs. */
171 struct ifaddrs *ifa = NULL;
172 if (getifaddrs (&ifa) != 0)
174 /* We cannot determine what interfaces are available. Be
175 pessimistic. */
176 *seen_ipv4 = true;
177 *seen_ipv6 = true;
178 return;
181 *seen_ipv4 = false;
182 *seen_ipv6 = false;
184 struct ifaddrs *runp;
185 for (runp = ifa; runp != NULL; runp = runp->ifa_next)
186 if (runp->ifa_addr->sa_family == PF_INET)
187 *seen_ipv4 = true;
188 else if (runp->ifa_addr->sa_family == PF_INET6)
189 *seen_ipv6 = true;
191 (void) freeifaddrs (ifa);
192 #endif