blob: bcbd9c5158125e4dc414fbfcf00aca1b848b4be0 [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
42#include <private/android_filesystem_config.h>
43
Daniel Drowna45056e2012-03-23 10:42:54 -050044#include "clatd.h"
45#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050046#include "dump.h"
junyulaic4e591a2018-11-26 22:36:10 +090047#include "getaddr.h"
48#include "logging.h"
49#include "mtu.h"
50#include "resolv_netid.h"
Lorenzo Colitti9353be22014-12-03 15:18:29 +090051#include "ring.h"
junyulaic4e591a2018-11-26 22:36:10 +090052#include "setif.h"
53#include "translate.h"
54#include "tun.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050055
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090056/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
57#define MTU_DELTA 28
58
Daniel Drowna45056e2012-03-23 10:42:54 -050059volatile sig_atomic_t running = 1;
60
Lorenzo Colittibaf62992013-03-01 20:29:39 +090061/* function: stop_loop
62 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050063 */
junyulaic4e591a2018-11-26 22:36:10 +090064void stop_loop() { running = 0; }
Daniel Drowna45056e2012-03-23 10:42:54 -050065
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090066/* function: configure_packet_socket
67 * Binds the packet socket and attaches the receive filter to it.
junyulaic4e591a2018-11-26 22:36:10 +090068 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050069 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090070int configure_packet_socket(int sock) {
71 struct sockaddr_ll sll = {
72 .sll_family = AF_PACKET,
73 .sll_protocol = htons(ETH_P_IPV6),
Masaya Hoshina96d69012015-06-10 22:17:49 +090074 .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090075 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
76 };
junyulaic4e591a2018-11-26 22:36:10 +090077 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090078 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
79 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050080 }
Daniel Drowna45056e2012-03-23 10:42:54 -050081
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090082 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
junyulaic4e591a2018-11-26 22:36:10 +090083
84 // clang-format off
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090085 struct sock_filter filter_code[] = {
86 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
87 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
88 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
89 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
90 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
91 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
92 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
93 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
94 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
95 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
96 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
97 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
98 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
99 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
junyulaic4e591a2018-11-26 22:36:10 +0900100 BPF_STMT(BPF_RET | BPF_K, 0),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900101 };
junyulaic4e591a2018-11-26 22:36:10 +0900102 // clang-format on
103 struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code };
Daniel Drowna45056e2012-03-23 10:42:54 -0500104
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900105 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
106 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
107 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500108 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900109
110 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500111}
112
Daniel Drowna45056e2012-03-23 10:42:54 -0500113/* function: configure_tun_ip
114 * configures the ipv4 and ipv6 addresses on the tunnel interface
junyulaic4e591a2018-11-26 22:36:10 +0900115 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500116 */
117void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500118 int status;
119
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900120 // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically,
121 // there is a race here - if another clatd calls config_select_ipv4_address after we do, but
122 // before we call add_address, it can end up having the same IP address as we do. But the time
123 // window in which this can happen is extremely small, and even if we end up with a duplicate
124 // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go
125 // down.
126 in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet,
127 Global_Clatd_Config.ipv4_local_prefixlen);
128 if (localaddr == INADDR_NONE) {
junyulaic4e591a2018-11-26 22:36:10 +0900129 logmsg(ANDROID_LOG_FATAL, "No free IPv4 address in %s/%d",
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900130 inet_ntoa(Global_Clatd_Config.ipv4_local_subnet),
131 Global_Clatd_Config.ipv4_local_prefixlen);
132 exit(1);
133 }
134 Global_Clatd_Config.ipv4_local_subnet.s_addr = localaddr;
135
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900136 // Configure the interface before bringing it up. As soon as we bring the interface up, the
137 // framework will be notified and will assume the interface's configuration has been finalized.
junyulaic4e591a2018-11-26 22:36:10 +0900138 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32,
139 &Global_Clatd_Config.ipv4_local_subnet);
140 if (status < 0) {
141 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status));
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900142 exit(1);
143 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500144
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900145 char addrstr[INET_ADDRSTRLEN];
146 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr));
147 logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4);
148
junyulaic4e591a2018-11-26 22:36:10 +0900149 if ((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
150 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status));
Daniel Drowna45056e2012-03-23 10:42:54 -0500151 exit(1);
152 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500153}
154
junyulaib5e8f972018-10-29 23:10:15 +0800155/* function: set_capability
156 * set the permitted, effective and inheritable capabilities of the current
157 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -0500158 */
junyulaib5e8f972018-10-29 23:10:15 +0800159void set_capability(uint64_t target_cap) {
160 struct __user_cap_header_struct header = {
161 .version = _LINUX_CAPABILITY_VERSION_3,
162 .pid = 0 // 0 = change myself
163 };
164 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
165
166 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
167 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
168
169 if (capset(&header, cap) < 0) {
170 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
171 exit(1);
172 }
173}
174
175/* function: drop_root_but_keep_caps
176 * drops root privs but keeps the needed capabilities
177 */
178void drop_root_but_keep_caps() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900179 gid_t groups[] = { AID_INET, AID_VPN };
junyulaic4e591a2018-11-26 22:36:10 +0900180 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +0800181 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500182 exit(1);
183 }
184
junyulaib5e8f972018-10-29 23:10:15 +0800185 prctl(PR_SET_KEEPCAPS, 1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500186
junyulaib5e8f972018-10-29 23:10:15 +0800187 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
188 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500189 exit(1);
190 }
junyulaib5e8f972018-10-29 23:10:15 +0800191 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
192 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500193 exit(1);
194 }
195
junyulaib5e8f972018-10-29 23:10:15 +0800196 // keep CAP_NET_RAW capability to open raw socket, and CAP_IPC_LOCK for mmap
197 // to lock memory.
198 set_capability((1 << CAP_NET_ADMIN) |
199 (1 << CAP_NET_RAW) |
200 (1 << CAP_IPC_LOCK));
Daniel Drowna45056e2012-03-23 10:42:54 -0500201}
202
Lorenzo Colitti75647612014-06-09 13:55:50 +0900203/* function: open_sockets
204 * opens a packet socket to receive IPv6 packets and a raw socket to send them
junyulaic4e591a2018-11-26 22:36:10 +0900205 * tunnel - tun device data
206 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900207 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900208void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900209 int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_RAW);
Lorenzo Colittice140882014-06-02 21:20:40 +0900210 if (rawsock < 0) {
211 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
212 exit(1);
213 }
214
215 int off = 0;
216 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
217 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
218 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900219 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
220 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
221 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900222
223 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900224
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900225 tunnel->read_fd6 = ring_create(tunnel);
226 if (tunnel->read_fd6 < 0) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900227 exit(1);
228 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900229}
230
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900231int ipv6_address_changed(const char *interface) {
232 union anyip *interface_ip;
233
234 interface_ip = getinterface_ip(interface, AF_INET6);
235 if (!interface_ip) {
236 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
237 return 1;
238 }
239
240 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
241 char oldstr[INET6_ADDRSTRLEN];
242 char newstr[INET6_ADDRSTRLEN];
243 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
244 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
245 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
246 free(interface_ip);
247 return 1;
248 } else {
249 free(interface_ip);
250 return 0;
251 }
252}
253
254/* function: configure_clat_ipv6_address
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900255 * picks the clat IPv6 address and configures packet translation to use it.
junyulaic4e591a2018-11-26 22:36:10 +0900256 * tunnel - tun device data
257 * interface - uplink interface name
258 * returns: 1 on success, 0 on failure
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900259 */
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900260int configure_clat_ipv6_address(const struct tun_data *tunnel, const char *interface) {
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900261 union anyip *interface_ip;
262 char addrstr[INET6_ADDRSTRLEN];
263
264 // TODO: check that the prefix length is /64.
265 interface_ip = getinterface_ip(interface, AF_INET6);
266 if (!interface_ip) {
267 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
268 return 0;
269 }
270
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900271 // Generate an interface ID.
272 config_generate_local_ipv6_subnet(&interface_ip->ip6);
273 inet_ntop(AF_INET6, &interface_ip->ip6, addrstr, sizeof(addrstr));
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900274 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900275
276 // Start translating packets to the new prefix.
277 Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6;
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900278 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900279 free(interface_ip);
280
281 // Update our packet socket filter to reflect the new 464xlat IP address.
282 if (!configure_packet_socket(tunnel->read_fd6)) {
junyulaic4e591a2018-11-26 22:36:10 +0900283 // Things aren't going to work. Bail out and hope we have better luck next time.
284 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900285 return 0;
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900286 }
287
288 return 1;
289}
290
Daniel Drowna45056e2012-03-23 10:42:54 -0500291/* function: configure_interface
292 * reads the configuration and applies it to the interface
junyulaic4e591a2018-11-26 22:36:10 +0900293 * uplink_interface - network interface to use to reach the ipv6 internet
294 * plat_prefix - PLAT prefix to use
295 * tunnel - tun device data
296 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500297 */
junyulaic4e591a2018-11-26 22:36:10 +0900298void configure_interface(const char *uplink_interface, const char *plat_prefix,
299 struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500300 int error;
301
junyulaic4e591a2018-11-26 22:36:10 +0900302 if (!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
303 logmsg(ANDROID_LOG_FATAL, "read_config failed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 exit(1);
305 }
306
junyulaic4e591a2018-11-26 22:36:10 +0900307 if (Global_Clatd_Config.mtu > MAXMTU) {
308 logmsg(ANDROID_LOG_WARN, "Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500309 Global_Clatd_Config.mtu = MAXMTU;
310 }
junyulaic4e591a2018-11-26 22:36:10 +0900311 if (Global_Clatd_Config.mtu <= 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500312 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900313 logmsg(ANDROID_LOG_WARN, "ifmtu=%d", Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500314 }
junyulaic4e591a2018-11-26 22:36:10 +0900315 if (Global_Clatd_Config.mtu < 1280) {
316 logmsg(ANDROID_LOG_WARN, "mtu too small = %d", Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500317 Global_Clatd_Config.mtu = 1280;
318 }
319
junyulaic4e591a2018-11-26 22:36:10 +0900320 if (Global_Clatd_Config.ipv4mtu <= 0 ||
321 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900322 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
junyulaic4e591a2018-11-26 22:36:10 +0900323 logmsg(ANDROID_LOG_WARN, "ipv4mtu now set to = %d", Global_Clatd_Config.ipv4mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500324 }
325
Daniel Drowna45056e2012-03-23 10:42:54 -0500326 error = tun_alloc(tunnel->device4, tunnel->fd4);
junyulaic4e591a2018-11-26 22:36:10 +0900327 if (error < 0) {
328 logmsg(ANDROID_LOG_FATAL, "tun_alloc/4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500329 exit(1);
330 }
331
Lorenzo Colitti49454812015-01-31 19:18:47 +0900332 error = set_nonblocking(tunnel->fd4);
333 if (error < 0) {
334 logmsg(ANDROID_LOG_FATAL, "set_nonblocking failed: %s", strerror(errno));
335 exit(1);
336 }
337
Daniel Drowna45056e2012-03-23 10:42:54 -0500338 configure_tun_ip(tunnel);
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900339
340 if (!configure_clat_ipv6_address(tunnel, uplink_interface)) {
341 exit(1);
342 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500343}
344
Daniel Drowna45056e2012-03-23 10:42:54 -0500345/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900346 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900347 * read_fd - file descriptor to read original packet from
348 * write_fd - file descriptor to write translated packet to
349 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500350 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900351void read_packet(int read_fd, int write_fd, int to_ipv6) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500352 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900353 uint8_t buf[PACKETLEN], *packet;
Daniel Drowna45056e2012-03-23 10:42:54 -0500354
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900355 readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500356
junyulaic4e591a2018-11-26 22:36:10 +0900357 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900358 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900359 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900360 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500361 return;
junyulaic4e591a2018-11-26 22:36:10 +0900362 } else if (readlen == 0) {
363 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500364 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900365 return;
366 }
367
junyulaic4e591a2018-11-26 22:36:10 +0900368 struct tun_pi *tun_header = (struct tun_pi *)buf;
369 if (readlen < (ssize_t)sizeof(*tun_header)) {
370 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900371 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500372 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900373
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900374 uint16_t proto = ntohs(tun_header->proto);
375 if (proto != ETH_P_IP) {
376 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
377 return;
378 }
379
junyulaic4e591a2018-11-26 22:36:10 +0900380 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900381 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
382 }
383
junyulaic4e591a2018-11-26 22:36:10 +0900384 packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900385 readlen -= sizeof(*tun_header);
386 translate_packet(write_fd, to_ipv6, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500387}
388
389/* function: event_loop
390 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900391 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500392 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900393void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500394 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700395 struct pollfd wait_fd[] = {
396 { tunnel->read_fd6, POLLIN, 0 },
397 { tunnel->fd4, POLLIN, 0 },
398 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500399
400 // start the poll timer
401 last_interface_poll = time(NULL);
402
junyulaic4e591a2018-11-26 22:36:10 +0900403 while (running) {
404 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900405 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900406 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500407 }
408 } else {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900409 if (wait_fd[0].revents & POLLIN) {
410 ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */);
411 }
412 // If any other bit is set, assume it's due to an error (i.e. POLLERR).
413 if (wait_fd[0].revents & ~POLLIN) {
414 // ring_read doesn't clear the error indication on the socket.
415 recv(tunnel->read_fd6, NULL, 0, MSG_PEEK);
junyulaic4e591a2018-11-26 22:36:10 +0900416 logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno));
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900417 }
418
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900419 // Call read_packet if the socket has data to be read, but also if an
420 // error is waiting. If we don't call read() after getting POLLERR, a
421 // subsequent poll() will return immediately with POLLERR again,
422 // causing this code to spin in a loop. Calling read() will clear the
423 // socket error flag instead.
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900424 if (wait_fd[1].revents) {
425 read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500426 }
427 }
428
429 time_t now = time(NULL);
junyulaic4e591a2018-11-26 22:36:10 +0900430 if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900431 if (ipv6_address_changed(Global_Clatd_Config.default_pdp_interface)) {
432 break;
433 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500434 }
435 }
436}
437
Lorenzo Colitti75647612014-06-09 13:55:50 +0900438/* function: parse_unsigned
439 * parses a string as a decimal/hex/octal unsigned integer
junyulaic4e591a2018-11-26 22:36:10 +0900440 * str - the string to parse
441 * out - the unsigned integer to write to, gets clobbered on failure
Lorenzo Colitti75647612014-06-09 13:55:50 +0900442 */
443int parse_unsigned(const char *str, unsigned *out) {
junyulaic4e591a2018-11-26 22:36:10 +0900444 char *end_ptr;
445 *out = strtoul(str, &end_ptr, 0);
446 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500447}