blob: edae7618f2d89467d6b93bf2146cff3f0051f499 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2012 Daniel Drown
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * clatd.c - tun interface setup and main event loop
17 */
junyulaic4e591a2018-11-26 22:36:10 +090018#include <arpa/inet.h>
19#include <errno.h>
20#include <fcntl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021#include <poll.h>
22#include <signal.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050023#include <stdio.h>
junyulaic4e591a2018-11-26 22:36:10 +090024#include <stdlib.h>
25#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050026#include <sys/ioctl.h>
Elliott Hughes3afe9ae2014-07-18 17:25:26 -070027#include <sys/prctl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050028#include <sys/stat.h>
junyulaic4e591a2018-11-26 22:36:10 +090029#include <sys/types.h>
30#include <time.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050031#include <unistd.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050032
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090033#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050034#include <linux/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090036#include <linux/if_packet.h>
junyulaic4e591a2018-11-26 22:36:10 +090037#include <linux/if_tun.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090038#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090039#include <sys/capability.h>
40#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050041
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +090042#include <netid_client.h> // For MARK_UNSET.
43#include <private/android_filesystem_config.h> // For AID_CLAT.
Daniel Drowna45056e2012-03-23 10:42:54 -050044
Daniel Drowna45056e2012-03-23 10:42:54 -050045#include "clatd.h"
46#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050047#include "dump.h"
junyulaic4e591a2018-11-26 22:36:10 +090048#include "getaddr.h"
49#include "logging.h"
Lorenzo Colitti9353be22014-12-03 15:18:29 +090050#include "ring.h"
junyulaic4e591a2018-11-26 22:36:10 +090051#include "setif.h"
52#include "translate.h"
53#include "tun.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050054
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090055/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
56#define MTU_DELTA 28
57
Daniel Drowna45056e2012-03-23 10:42:54 -050058volatile sig_atomic_t running = 1;
59
Lorenzo Colittibaf62992013-03-01 20:29:39 +090060/* function: stop_loop
61 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050062 */
junyulaic4e591a2018-11-26 22:36:10 +090063void stop_loop() { running = 0; }
Daniel Drowna45056e2012-03-23 10:42:54 -050064
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090065/* function: configure_packet_socket
66 * Binds the packet socket and attaches the receive filter to it.
junyulaic4e591a2018-11-26 22:36:10 +090067 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050068 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090069int configure_packet_socket(int sock) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090070 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
junyulaic4e591a2018-11-26 22:36:10 +090071
72 // clang-format off
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090073 struct sock_filter filter_code[] = {
74 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
75 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
76 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
77 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
78 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
79 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
80 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
81 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
82 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
83 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
84 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
85 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
86 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
87 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
junyulaic4e591a2018-11-26 22:36:10 +090088 BPF_STMT(BPF_RET | BPF_K, 0),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090089 };
junyulaic4e591a2018-11-26 22:36:10 +090090 // clang-format on
91 struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code };
Daniel Drowna45056e2012-03-23 10:42:54 -050092
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090093 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
94 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
95 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050096 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090097
Maciej Żenczykowski69c840f2019-11-18 12:00:49 -080098 struct sockaddr_ll sll = {
99 .sll_family = AF_PACKET,
100 .sll_protocol = htons(ETH_P_IPV6),
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700101 .sll_ifindex = if_nametoindex(Global_Clatd_Config.native_ipv6_interface),
Maciej Żenczykowski69c840f2019-11-18 12:00:49 -0800102 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
103 };
104 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
105 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
106 return 0;
107 }
108
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900109 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500110}
111
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900112/* function: configure_tun_ip
113 * configures the ipv4 and ipv6 addresses on the tunnel interface
114 * tunnel - tun device data
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800115 * mtu - mtu of tun device
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900116 */
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800117void configure_tun_ip(const struct tun_data *tunnel, const char *v4_addr, int mtu) {
Lorenzo Colittibaa3c6a2020-06-02 01:55:12 +0900118 if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) {
119 logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr);
120 exit(1);
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900121 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500122
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900123 char addrstr[INET_ADDRSTRLEN];
124 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr));
125 logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4);
126
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900127 // Configure the interface before bringing it up. As soon as we bring the interface up, the
128 // framework will be notified and will assume the interface's configuration has been finalized.
129 int status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32,
130 &Global_Clatd_Config.ipv4_local_subnet);
131 if (status < 0) {
132 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status));
133 exit(1);
134 }
135
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800136 status = if_up(tunnel->device4, mtu);
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900137 if (status < 0) {
junyulaic4e591a2018-11-26 22:36:10 +0900138 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status));
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 exit(1);
140 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500141}
142
junyulaib5e8f972018-10-29 23:10:15 +0800143/* function: set_capability
144 * set the permitted, effective and inheritable capabilities of the current
145 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -0500146 */
junyulaib5e8f972018-10-29 23:10:15 +0800147void set_capability(uint64_t target_cap) {
148 struct __user_cap_header_struct header = {
149 .version = _LINUX_CAPABILITY_VERSION_3,
150 .pid = 0 // 0 = change myself
151 };
152 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
153
154 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
155 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
156
157 if (capset(&header, cap) < 0) {
158 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
159 exit(1);
160 }
161}
162
163/* function: drop_root_but_keep_caps
164 * drops root privs but keeps the needed capabilities
165 */
166void drop_root_but_keep_caps() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900167 gid_t groups[] = { AID_INET, AID_VPN };
junyulaic4e591a2018-11-26 22:36:10 +0900168 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +0800169 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500170 exit(1);
171 }
172
junyulaib5e8f972018-10-29 23:10:15 +0800173 prctl(PR_SET_KEEPCAPS, 1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500174
junyulaib5e8f972018-10-29 23:10:15 +0800175 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
176 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500177 exit(1);
178 }
junyulaib5e8f972018-10-29 23:10:15 +0800179 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
180 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500181 exit(1);
182 }
183
junyulaib5e8f972018-10-29 23:10:15 +0800184 // keep CAP_NET_RAW capability to open raw socket, and CAP_IPC_LOCK for mmap
185 // to lock memory.
186 set_capability((1 << CAP_NET_ADMIN) |
187 (1 << CAP_NET_RAW) |
188 (1 << CAP_IPC_LOCK));
Daniel Drowna45056e2012-03-23 10:42:54 -0500189}
190
Lorenzo Colitti75647612014-06-09 13:55:50 +0900191/* function: open_sockets
192 * opens a packet socket to receive IPv6 packets and a raw socket to send them
junyulaic4e591a2018-11-26 22:36:10 +0900193 * tunnel - tun device data
194 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900195 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900196void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Maciej Żenczykowski60bce372019-04-09 01:58:52 -0700197 int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_RAW);
Lorenzo Colittice140882014-06-02 21:20:40 +0900198 if (rawsock < 0) {
199 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
200 exit(1);
201 }
202
Lorenzo Colitti75647612014-06-09 13:55:50 +0900203 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
204 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
205 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900206
207 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900208
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900209 tunnel->read_fd6 = ring_create(tunnel);
210 if (tunnel->read_fd6 < 0) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900211 exit(1);
212 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900213}
214
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900215int ipv6_address_changed(const char *interface) {
216 union anyip *interface_ip;
217
218 interface_ip = getinterface_ip(interface, AF_INET6);
219 if (!interface_ip) {
220 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
221 return 1;
222 }
223
224 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
225 char oldstr[INET6_ADDRSTRLEN];
226 char newstr[INET6_ADDRSTRLEN];
227 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
228 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
229 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
230 free(interface_ip);
231 return 1;
232 } else {
233 free(interface_ip);
234 return 0;
235 }
236}
237
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900238/* function: configure_clat_ipv6_address
239 * picks the clat IPv6 address and configures packet translation to use it.
240 * tunnel - tun device data
241 * interface - uplink interface name
242 * returns: 1 on success, 0 on failure
243 */
244int configure_clat_ipv6_address(const struct tun_data *tunnel, const char *interface,
245 const char *v6_addr) {
Lorenzo Colittibaa3c6a2020-06-02 01:55:12 +0900246 if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
247 logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
248 return 0;
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900249 }
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900250
251 char addrstr[INET6_ADDRSTRLEN];
252 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, addrstr, sizeof(addrstr));
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900253 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900254
255 // Start translating packets to the new prefix.
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900256 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900257
258 // Update our packet socket filter to reflect the new 464xlat IP address.
259 if (!configure_packet_socket(tunnel->read_fd6)) {
junyulaic4e591a2018-11-26 22:36:10 +0900260 // Things aren't going to work. Bail out and hope we have better luck next time.
261 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900262 return 0;
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900263 }
264
265 return 1;
266}
267
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800268int detect_mtu(const struct in6_addr *plat_subnet, uint32_t plat_suffix, uint32_t mark) {
269 // Create an IPv6 UDP socket.
270 int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
271 if (s < 0) {
272 logmsg(ANDROID_LOG_FATAL, "socket(AF_INET6, SOCK_DGRAM, 0) failed");
273 exit(1);
274 }
275
276 // Socket's mark affects routing decisions (network selection)
277 if ((mark != MARK_UNSET) && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) {
278 logmsg(ANDROID_LOG_FATAL, "setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno));
279 exit(1);
280 }
281
282 // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits)
283 struct sockaddr_in6 dst = {
284 .sin6_family = AF_INET6,
285 .sin6_addr = *plat_subnet,
286 };
287 dst.sin6_addr.s6_addr32[3] = plat_suffix;
288 if (connect(s, (struct sockaddr *)&dst, sizeof(dst))) {
289 logmsg(ANDROID_LOG_FATAL, "connect() failed: %s", strerror(errno));
290 exit(1);
291 }
292
293 // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table
294 int mtu;
295 socklen_t sz_mtu = sizeof(mtu);
296 if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) {
297 logmsg(ANDROID_LOG_FATAL, "getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(errno));
298 exit(1);
299 }
300 if (sz_mtu != sizeof(mtu)) {
301 logmsg(ANDROID_LOG_FATAL, "getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d",
302 sz_mtu);
303 exit(1);
304 }
305 close(s);
306
307 return mtu;
308}
309
Daniel Drowna45056e2012-03-23 10:42:54 -0500310/* function: configure_interface
311 * reads the configuration and applies it to the interface
junyulaic4e591a2018-11-26 22:36:10 +0900312 * uplink_interface - network interface to use to reach the ipv6 internet
313 * plat_prefix - PLAT prefix to use
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900314 * v4_addr - the v4 address to use on the tunnel interface
315 * v6_addr - the v6 address to use on the native interface
junyulaic4e591a2018-11-26 22:36:10 +0900316 * tunnel - tun device data
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800317 * mark - the socket mark to use for the sending raw socket
Daniel Drowna45056e2012-03-23 10:42:54 -0500318 */
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900319void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v4_addr,
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900320 const char *v6_addr, struct tun_data *tunnel, uint32_t mark) {
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700321 Global_Clatd_Config.native_ipv6_interface = uplink_interface;
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900322 if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
323 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
324 exit(1);
325 }
326
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800327 int mtu = detect_mtu(&Global_Clatd_Config.plat_subnet, htonl(0x08080808), mark);
328 // clamp to minimum ipv6 mtu - this probably cannot ever trigger
329 if (mtu < 1280) mtu = 1280;
330 // clamp to buffer size
331 if (mtu > MAXMTU) mtu = MAXMTU;
332 // decrease by ipv6(40) + ipv6 fragmentation header(8) vs ipv4(20) overhead of 28 bytes
333 mtu -= MTU_DELTA;
334 logmsg(ANDROID_LOG_WARN, "ipv4 mtu is %d", mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500335
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800336 configure_tun_ip(tunnel, v4_addr, mtu);
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900337
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900338 if (!configure_clat_ipv6_address(tunnel, uplink_interface, v6_addr)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900339 exit(1);
340 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500341}
342
Daniel Drowna45056e2012-03-23 10:42:54 -0500343/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900344 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900345 * read_fd - file descriptor to read original packet from
346 * write_fd - file descriptor to write translated packet to
347 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500348 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900349void read_packet(int read_fd, int write_fd, int to_ipv6) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500350 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900351 uint8_t buf[PACKETLEN], *packet;
Daniel Drowna45056e2012-03-23 10:42:54 -0500352
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900353 readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500354
junyulaic4e591a2018-11-26 22:36:10 +0900355 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900356 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900357 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900358 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500359 return;
junyulaic4e591a2018-11-26 22:36:10 +0900360 } else if (readlen == 0) {
361 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500362 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900363 return;
364 }
365
junyulaic4e591a2018-11-26 22:36:10 +0900366 struct tun_pi *tun_header = (struct tun_pi *)buf;
367 if (readlen < (ssize_t)sizeof(*tun_header)) {
368 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900369 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500370 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900371
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900372 uint16_t proto = ntohs(tun_header->proto);
373 if (proto != ETH_P_IP) {
374 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
375 return;
376 }
377
junyulaic4e591a2018-11-26 22:36:10 +0900378 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900379 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
380 }
381
junyulaic4e591a2018-11-26 22:36:10 +0900382 packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900383 readlen -= sizeof(*tun_header);
384 translate_packet(write_fd, to_ipv6, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500385}
386
387/* function: event_loop
388 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900389 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500390 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900391void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500392 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700393 struct pollfd wait_fd[] = {
394 { tunnel->read_fd6, POLLIN, 0 },
395 { tunnel->fd4, POLLIN, 0 },
396 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500397
398 // start the poll timer
399 last_interface_poll = time(NULL);
400
junyulaic4e591a2018-11-26 22:36:10 +0900401 while (running) {
402 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900403 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900404 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500405 }
406 } else {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900407 if (wait_fd[0].revents & POLLIN) {
408 ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */);
409 }
410 // If any other bit is set, assume it's due to an error (i.e. POLLERR).
411 if (wait_fd[0].revents & ~POLLIN) {
412 // ring_read doesn't clear the error indication on the socket.
413 recv(tunnel->read_fd6, NULL, 0, MSG_PEEK);
junyulaic4e591a2018-11-26 22:36:10 +0900414 logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno));
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900415 }
416
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900417 // Call read_packet if the socket has data to be read, but also if an
418 // error is waiting. If we don't call read() after getting POLLERR, a
419 // subsequent poll() will return immediately with POLLERR again,
420 // causing this code to spin in a loop. Calling read() will clear the
421 // socket error flag instead.
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900422 if (wait_fd[1].revents) {
423 read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500424 }
425 }
426
427 time_t now = time(NULL);
junyulaic4e591a2018-11-26 22:36:10 +0900428 if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700429 if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900430 break;
431 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500432 }
433 }
434}