blob: 3f9dc71c72086c4112dbaebd318c0e150a1293a0 [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),
101 .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface),
102 .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: ipv4_address_generate
113 * picks a free IPv4 address from the local subnet or exits if there are no free addresses
114 * returns: the IPv4 address as an in_addr_t
Daniel Drowna45056e2012-03-23 10:42:54 -0500115 */
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900116static in_addr_t ipv4_address_generate() {
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900117 // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically,
118 // there is a race here - if another clatd calls config_select_ipv4_address after we do, but
119 // before we call add_address, it can end up having the same IP address as we do. But the time
120 // window in which this can happen is extremely small, and even if we end up with a duplicate
121 // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go
122 // down.
123 in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet,
124 Global_Clatd_Config.ipv4_local_prefixlen);
125 if (localaddr == INADDR_NONE) {
junyulaic4e591a2018-11-26 22:36:10 +0900126 logmsg(ANDROID_LOG_FATAL, "No free IPv4 address in %s/%d",
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900127 inet_ntoa(Global_Clatd_Config.ipv4_local_subnet),
128 Global_Clatd_Config.ipv4_local_prefixlen);
129 exit(1);
130 }
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900131 return localaddr;
132}
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900133
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900134/* function: ipv4_address_from_cmdline
135 * configures the IPv4 address specified on the command line, or exits if the address is not valid
136 * v4_addr - a string, the IPv4 address
137 * returns: the IPv4 address as an in_addr_t
138 */
139static in_addr_t ipv4_address_from_cmdline(const char *v4_addr) {
140 in_addr_t localaddr;
141 if (!inet_pton(AF_INET, v4_addr, &localaddr)) {
142 logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr);
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900143 exit(1);
144 }
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900145 return localaddr;
146}
147
148/* function: configure_tun_ip
149 * configures the ipv4 and ipv6 addresses on the tunnel interface
150 * tunnel - tun device data
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800151 * mtu - mtu of tun device
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900152 */
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800153void configure_tun_ip(const struct tun_data *tunnel, const char *v4_addr, int mtu) {
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900154 if (v4_addr) {
155 Global_Clatd_Config.ipv4_local_subnet.s_addr = ipv4_address_from_cmdline(v4_addr);
156 } else {
157 Global_Clatd_Config.ipv4_local_subnet.s_addr = ipv4_address_generate();
158 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500159
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900160 char addrstr[INET_ADDRSTRLEN];
161 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr));
162 logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4);
163
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900164 // Configure the interface before bringing it up. As soon as we bring the interface up, the
165 // framework will be notified and will assume the interface's configuration has been finalized.
166 int status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32,
167 &Global_Clatd_Config.ipv4_local_subnet);
168 if (status < 0) {
169 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status));
170 exit(1);
171 }
172
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800173 status = if_up(tunnel->device4, mtu);
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900174 if (status < 0) {
junyulaic4e591a2018-11-26 22:36:10 +0900175 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status));
Daniel Drowna45056e2012-03-23 10:42:54 -0500176 exit(1);
177 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500178}
179
junyulaib5e8f972018-10-29 23:10:15 +0800180/* function: set_capability
181 * set the permitted, effective and inheritable capabilities of the current
182 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -0500183 */
junyulaib5e8f972018-10-29 23:10:15 +0800184void set_capability(uint64_t target_cap) {
185 struct __user_cap_header_struct header = {
186 .version = _LINUX_CAPABILITY_VERSION_3,
187 .pid = 0 // 0 = change myself
188 };
189 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
190
191 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
192 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
193
194 if (capset(&header, cap) < 0) {
195 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
196 exit(1);
197 }
198}
199
200/* function: drop_root_but_keep_caps
201 * drops root privs but keeps the needed capabilities
202 */
203void drop_root_but_keep_caps() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900204 gid_t groups[] = { AID_INET, AID_VPN };
junyulaic4e591a2018-11-26 22:36:10 +0900205 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +0800206 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500207 exit(1);
208 }
209
junyulaib5e8f972018-10-29 23:10:15 +0800210 prctl(PR_SET_KEEPCAPS, 1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500211
junyulaib5e8f972018-10-29 23:10:15 +0800212 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
213 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500214 exit(1);
215 }
junyulaib5e8f972018-10-29 23:10:15 +0800216 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
217 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500218 exit(1);
219 }
220
junyulaib5e8f972018-10-29 23:10:15 +0800221 // keep CAP_NET_RAW capability to open raw socket, and CAP_IPC_LOCK for mmap
222 // to lock memory.
223 set_capability((1 << CAP_NET_ADMIN) |
224 (1 << CAP_NET_RAW) |
225 (1 << CAP_IPC_LOCK));
Daniel Drowna45056e2012-03-23 10:42:54 -0500226}
227
Lorenzo Colitti75647612014-06-09 13:55:50 +0900228/* function: open_sockets
229 * opens a packet socket to receive IPv6 packets and a raw socket to send them
junyulaic4e591a2018-11-26 22:36:10 +0900230 * tunnel - tun device data
231 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900232 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900233void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Maciej Żenczykowski60bce372019-04-09 01:58:52 -0700234 int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_RAW);
Lorenzo Colittice140882014-06-02 21:20:40 +0900235 if (rawsock < 0) {
236 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
237 exit(1);
238 }
239
Lorenzo Colitti75647612014-06-09 13:55:50 +0900240 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
241 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
242 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900243
244 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900245
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900246 tunnel->read_fd6 = ring_create(tunnel);
247 if (tunnel->read_fd6 < 0) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900248 exit(1);
249 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900250}
251
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900252int ipv6_address_changed(const char *interface) {
253 union anyip *interface_ip;
254
255 interface_ip = getinterface_ip(interface, AF_INET6);
256 if (!interface_ip) {
257 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
258 return 1;
259 }
260
261 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
262 char oldstr[INET6_ADDRSTRLEN];
263 char newstr[INET6_ADDRSTRLEN];
264 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
265 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
266 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
267 free(interface_ip);
268 return 1;
269 } else {
270 free(interface_ip);
271 return 0;
272 }
273}
274
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900275/* function: clat_ipv6_address_from_interface
276 * picks the clat IPv6 address based on the interface address
junyulaic4e591a2018-11-26 22:36:10 +0900277 * interface - uplink interface name
278 * returns: 1 on success, 0 on failure
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900279 */
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900280static int clat_ipv6_address_from_interface(const char *interface) {
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900281 union anyip *interface_ip;
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900282
283 // TODO: check that the prefix length is /64.
284 interface_ip = getinterface_ip(interface, AF_INET6);
285 if (!interface_ip) {
286 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
287 return 0;
288 }
289
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900290 // Generate an interface ID.
291 config_generate_local_ipv6_subnet(&interface_ip->ip6);
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900292
293 Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6;
294 free(interface_ip);
295 return 1;
296}
297
298/* function: clat_ipv6_address_from_cmdline
299 * parses the clat IPv6 address from the command line
300 * v4_addr - a string, the IPv6 address
301 * returns: 1 on success, 0 on failure
302 */
303static int clat_ipv6_address_from_cmdline(const char *v6_addr) {
304 if (!inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) {
305 logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr);
306 return 0;
307 }
308
309 return 1;
310}
311
312/* function: configure_clat_ipv6_address
313 * picks the clat IPv6 address and configures packet translation to use it.
314 * tunnel - tun device data
315 * interface - uplink interface name
316 * returns: 1 on success, 0 on failure
317 */
318int configure_clat_ipv6_address(const struct tun_data *tunnel, const char *interface,
319 const char *v6_addr) {
320 int ret;
321 if (v6_addr) {
322 ret = clat_ipv6_address_from_cmdline(v6_addr);
323 } else {
324 ret = clat_ipv6_address_from_interface(interface);
325 }
326 if (!ret) return 0;
327
328 char addrstr[INET6_ADDRSTRLEN];
329 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, addrstr, sizeof(addrstr));
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900330 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900331
332 // Start translating packets to the new prefix.
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900333 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900334
335 // Update our packet socket filter to reflect the new 464xlat IP address.
336 if (!configure_packet_socket(tunnel->read_fd6)) {
junyulaic4e591a2018-11-26 22:36:10 +0900337 // Things aren't going to work. Bail out and hope we have better luck next time.
338 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900339 return 0;
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900340 }
341
342 return 1;
343}
344
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800345int detect_mtu(const struct in6_addr *plat_subnet, uint32_t plat_suffix, uint32_t mark) {
346 // Create an IPv6 UDP socket.
347 int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
348 if (s < 0) {
349 logmsg(ANDROID_LOG_FATAL, "socket(AF_INET6, SOCK_DGRAM, 0) failed");
350 exit(1);
351 }
352
353 // Socket's mark affects routing decisions (network selection)
354 if ((mark != MARK_UNSET) && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) {
355 logmsg(ANDROID_LOG_FATAL, "setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno));
356 exit(1);
357 }
358
359 // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits)
360 struct sockaddr_in6 dst = {
361 .sin6_family = AF_INET6,
362 .sin6_addr = *plat_subnet,
363 };
364 dst.sin6_addr.s6_addr32[3] = plat_suffix;
365 if (connect(s, (struct sockaddr *)&dst, sizeof(dst))) {
366 logmsg(ANDROID_LOG_FATAL, "connect() failed: %s", strerror(errno));
367 exit(1);
368 }
369
370 // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table
371 int mtu;
372 socklen_t sz_mtu = sizeof(mtu);
373 if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) {
374 logmsg(ANDROID_LOG_FATAL, "getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(errno));
375 exit(1);
376 }
377 if (sz_mtu != sizeof(mtu)) {
378 logmsg(ANDROID_LOG_FATAL, "getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d",
379 sz_mtu);
380 exit(1);
381 }
382 close(s);
383
384 return mtu;
385}
386
Daniel Drowna45056e2012-03-23 10:42:54 -0500387/* function: configure_interface
388 * reads the configuration and applies it to the interface
junyulaic4e591a2018-11-26 22:36:10 +0900389 * uplink_interface - network interface to use to reach the ipv6 internet
390 * plat_prefix - PLAT prefix to use
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900391 * v4_addr - the v4 address to use on the tunnel interface
392 * v6_addr - the v6 address to use on the native interface
junyulaic4e591a2018-11-26 22:36:10 +0900393 * tunnel - tun device data
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800394 * mark - the socket mark to use for the sending raw socket
Daniel Drowna45056e2012-03-23 10:42:54 -0500395 */
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900396void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v4_addr,
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900397 const char *v6_addr, struct tun_data *tunnel, uint32_t mark) {
398 if (!read_config("/system/etc/clatd.conf", uplink_interface)) {
junyulaic4e591a2018-11-26 22:36:10 +0900399 logmsg(ANDROID_LOG_FATAL, "read_config failed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500400 exit(1);
401 }
402
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900403 if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
404 logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
405 exit(1);
406 }
407
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800408 int mtu = detect_mtu(&Global_Clatd_Config.plat_subnet, htonl(0x08080808), mark);
409 // clamp to minimum ipv6 mtu - this probably cannot ever trigger
410 if (mtu < 1280) mtu = 1280;
411 // clamp to buffer size
412 if (mtu > MAXMTU) mtu = MAXMTU;
413 // decrease by ipv6(40) + ipv6 fragmentation header(8) vs ipv4(20) overhead of 28 bytes
414 mtu -= MTU_DELTA;
415 logmsg(ANDROID_LOG_WARN, "ipv4 mtu is %d", mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500416
Maciej Żenczykowskib00113f2020-01-18 23:56:14 -0800417 configure_tun_ip(tunnel, v4_addr, mtu);
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900418
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900419 if (!configure_clat_ipv6_address(tunnel, uplink_interface, v6_addr)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900420 exit(1);
421 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500422}
423
Daniel Drowna45056e2012-03-23 10:42:54 -0500424/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900425 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900426 * read_fd - file descriptor to read original packet from
427 * write_fd - file descriptor to write translated packet to
428 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500429 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900430void read_packet(int read_fd, int write_fd, int to_ipv6) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500431 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900432 uint8_t buf[PACKETLEN], *packet;
Daniel Drowna45056e2012-03-23 10:42:54 -0500433
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900434 readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500435
junyulaic4e591a2018-11-26 22:36:10 +0900436 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900437 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900438 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900439 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500440 return;
junyulaic4e591a2018-11-26 22:36:10 +0900441 } else if (readlen == 0) {
442 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500443 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900444 return;
445 }
446
junyulaic4e591a2018-11-26 22:36:10 +0900447 struct tun_pi *tun_header = (struct tun_pi *)buf;
448 if (readlen < (ssize_t)sizeof(*tun_header)) {
449 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900450 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500451 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900452
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900453 uint16_t proto = ntohs(tun_header->proto);
454 if (proto != ETH_P_IP) {
455 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
456 return;
457 }
458
junyulaic4e591a2018-11-26 22:36:10 +0900459 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900460 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
461 }
462
junyulaic4e591a2018-11-26 22:36:10 +0900463 packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900464 readlen -= sizeof(*tun_header);
465 translate_packet(write_fd, to_ipv6, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500466}
467
468/* function: event_loop
469 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900470 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500471 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900472void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500473 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700474 struct pollfd wait_fd[] = {
475 { tunnel->read_fd6, POLLIN, 0 },
476 { tunnel->fd4, POLLIN, 0 },
477 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500478
479 // start the poll timer
480 last_interface_poll = time(NULL);
481
junyulaic4e591a2018-11-26 22:36:10 +0900482 while (running) {
483 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900484 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900485 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500486 }
487 } else {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900488 if (wait_fd[0].revents & POLLIN) {
489 ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */);
490 }
491 // If any other bit is set, assume it's due to an error (i.e. POLLERR).
492 if (wait_fd[0].revents & ~POLLIN) {
493 // ring_read doesn't clear the error indication on the socket.
494 recv(tunnel->read_fd6, NULL, 0, MSG_PEEK);
junyulaic4e591a2018-11-26 22:36:10 +0900495 logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno));
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900496 }
497
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900498 // Call read_packet if the socket has data to be read, but also if an
499 // error is waiting. If we don't call read() after getting POLLERR, a
500 // subsequent poll() will return immediately with POLLERR again,
501 // causing this code to spin in a loop. Calling read() will clear the
502 // socket error flag instead.
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900503 if (wait_fd[1].revents) {
504 read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500505 }
506 }
507
508 time_t now = time(NULL);
junyulaic4e591a2018-11-26 22:36:10 +0900509 if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900510 if (ipv6_address_changed(Global_Clatd_Config.default_pdp_interface)) {
511 break;
512 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500513 }
514 }
515}