* include/ifaddrs.h: Remove in6ai_temporary.
[glibc.git] / sysdeps / unix / sysv / linux / check_native.c
blob9b1bb619412e9885ca797facfc107c4cf9d8304b
1 /* Determine whether interfaces use native transport. Linux version.
2 Copyright (C) 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 <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <net/if.h>
29 #include <net/if_arp.h>
30 #include <sys/ioctl.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
35 #include <not-cancel.h>
38 void
39 __check_native (uint32_t a1_index, int *a1_native,
40 uint32_t a2_index, int *a2_native)
42 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
44 struct sockaddr_nl nladdr;
45 memset (&nladdr, '\0', sizeof (nladdr));
46 nladdr.nl_family = AF_NETLINK;
48 socklen_t addr_len = sizeof (nladdr);
50 if (fd < 0
51 || __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0
52 || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0)
53 return;
55 pid_t pid = nladdr.nl_pid;
56 struct req
58 struct nlmsghdr nlh;
59 struct rtgenmsg g;
60 /* struct rtgenmsg consists of a single byte. This means there
61 are three bytes of padding included in the REQ definition.
62 We make them explicit here. */
63 char pad[3];
64 } req;
66 req.nlh.nlmsg_len = sizeof (req);
67 req.nlh.nlmsg_type = RTM_GETLINK;
68 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
69 req.nlh.nlmsg_pid = 0;
70 req.nlh.nlmsg_seq = time (NULL);
71 req.g.rtgen_family = AF_UNSPEC;
73 assert (sizeof (req) - offsetof (struct req, pad) == 3);
74 memset (req.pad, '\0', sizeof (req.pad));
76 memset (&nladdr, '\0', sizeof (nladdr));
77 nladdr.nl_family = AF_NETLINK;
79 #ifdef PAGE_SIZE
80 /* Help the compiler optimize out the malloc call if PAGE_SIZE
81 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
82 const size_t buf_size = PAGE_SIZE;
83 #else
84 const size_t buf_size = __getpagesize ();
85 #endif
86 bool use_malloc = false;
87 char *buf;
89 if (__libc_use_alloca (buf_size))
90 buf = alloca (buf_size);
91 else
93 buf = malloc (buf_size);
94 if (buf != NULL)
95 use_malloc = true;
96 else
97 goto out_fail;
100 struct iovec iov = { buf, buf_size };
102 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
103 (struct sockaddr *) &nladdr,
104 sizeof (nladdr))) < 0)
105 goto out_fail;
107 bool done = false;
108 int v4fd = -1;
111 struct msghdr msg =
113 (void *) &nladdr, sizeof (nladdr),
114 &iov, 1,
115 NULL, 0,
119 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
120 if (read_len < 0)
121 goto out_fail;
123 if (msg.msg_flags & MSG_TRUNC)
124 goto out_fail;
126 struct nlmsghdr *nlmh;
127 for (nlmh = (struct nlmsghdr *) buf;
128 NLMSG_OK (nlmh, (size_t) read_len);
129 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
131 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
132 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
133 continue;
135 if (nlmh->nlmsg_type == RTM_NEWLINK)
137 /* A RTM_NEWLINK message can have IFLA_STATS data. We need to
138 know the size before creating the list to allocate enough
139 memory. */
140 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlmh);
141 struct rtattr *rta = IFLA_RTA (ifim);
142 size_t rtasize = IFLA_PAYLOAD (nlmh);
143 int index = ifim->ifi_index;
145 if (a1_index == index || a2_index == index)
146 while (RTA_OK (rta, rtasize))
148 char *rta_data = RTA_DATA (rta);
149 size_t rta_payload = RTA_PAYLOAD (rta);
151 if (rta->rta_type == IFLA_IFNAME)
153 struct ifreq ifr;
154 *((char *) mempcpy (ifr.ifr_name, rta_data,
155 rta_payload))= '\0';
157 if (v4fd == -1)
159 v4fd = __socket (AF_INET, SOCK_DGRAM, 0);
160 if (v4fd == -1)
161 return;
164 if (__ioctl (v4fd, SIOCGIFHWADDR, &ifr) >= 0)
166 int native
167 = (ifr.ifr_hwaddr.sa_family != ARPHRD_TUNNEL6
168 && ifr.ifr_hwaddr.sa_family != ARPHRD_TUNNEL
169 && ifr.ifr_hwaddr.sa_family != ARPHRD_SIT);
171 if (a1_index == index)
173 *a1_native = native;
174 a1_index = 0xffffffffu;
176 if (a2_index == index)
178 *a2_native = native;
179 a2_index = 0xffffffffu;
182 if (a1_index == 0xffffffffu
183 && a2_index == 0xffffffffu)
184 goto out;
186 break;
189 rta = RTA_NEXT (rta, rtasize);
192 else if (nlmh->nlmsg_type == NLMSG_DONE)
193 /* We found the end, leave the loop. */
194 done = true;
197 while (! done);
199 out:
200 close_not_cancel_no_status (fd);
201 if (v4fd != -1)
202 close_not_cancel_no_status (v4fd);
204 return;
206 out_fail:
207 if (use_malloc)
208 free (buf);