blob: 18d75621eb37e4041bc0ad640820a4f1c27c32d5 [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 Colitti76129162014-10-20 17:24:51 +090056#define DEVICEPREFIX "v4-"
Daniel Drowna45056e2012-03-23 10:42:54 -050057
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090058/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
59#define MTU_DELTA 28
60
Daniel Drowna45056e2012-03-23 10:42:54 -050061volatile sig_atomic_t running = 1;
62
Lorenzo Colittibaf62992013-03-01 20:29:39 +090063/* function: stop_loop
64 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050065 */
junyulaic4e591a2018-11-26 22:36:10 +090066void stop_loop() { running = 0; }
Daniel Drowna45056e2012-03-23 10:42:54 -050067
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090068/* function: configure_packet_socket
69 * Binds the packet socket and attaches the receive filter to it.
junyulaic4e591a2018-11-26 22:36:10 +090070 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050071 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090072int configure_packet_socket(int sock) {
73 struct sockaddr_ll sll = {
74 .sll_family = AF_PACKET,
75 .sll_protocol = htons(ETH_P_IPV6),
Masaya Hoshina96d69012015-06-10 22:17:49 +090076 .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090077 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
78 };
junyulaic4e591a2018-11-26 22:36:10 +090079 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090080 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
81 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050082 }
Daniel Drowna45056e2012-03-23 10:42:54 -050083
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090084 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
junyulaic4e591a2018-11-26 22:36:10 +090085
86 // clang-format off
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090087 struct sock_filter filter_code[] = {
88 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
89 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
90 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
91 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
92 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
93 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
94 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
95 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
96 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
97 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
98 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
99 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
100 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
101 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
junyulaic4e591a2018-11-26 22:36:10 +0900102 BPF_STMT(BPF_RET | BPF_K, 0),
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900103 };
junyulaic4e591a2018-11-26 22:36:10 +0900104 // clang-format on
105 struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code };
Daniel Drowna45056e2012-03-23 10:42:54 -0500106
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900107 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
108 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
109 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900111
112 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500113}
114
Daniel Drowna45056e2012-03-23 10:42:54 -0500115/* function: configure_tun_ip
116 * configures the ipv4 and ipv6 addresses on the tunnel interface
junyulaic4e591a2018-11-26 22:36:10 +0900117 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500118 */
119void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500120 int status;
121
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900122 // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically,
123 // there is a race here - if another clatd calls config_select_ipv4_address after we do, but
124 // before we call add_address, it can end up having the same IP address as we do. But the time
125 // window in which this can happen is extremely small, and even if we end up with a duplicate
126 // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go
127 // down.
128 in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet,
129 Global_Clatd_Config.ipv4_local_prefixlen);
130 if (localaddr == INADDR_NONE) {
junyulaic4e591a2018-11-26 22:36:10 +0900131 logmsg(ANDROID_LOG_FATAL, "No free IPv4 address in %s/%d",
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900132 inet_ntoa(Global_Clatd_Config.ipv4_local_subnet),
133 Global_Clatd_Config.ipv4_local_prefixlen);
134 exit(1);
135 }
136 Global_Clatd_Config.ipv4_local_subnet.s_addr = localaddr;
137
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900138 // Configure the interface before bringing it up. As soon as we bring the interface up, the
139 // framework will be notified and will assume the interface's configuration has been finalized.
junyulaic4e591a2018-11-26 22:36:10 +0900140 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32,
141 &Global_Clatd_Config.ipv4_local_subnet);
142 if (status < 0) {
143 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status));
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900144 exit(1);
145 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500146
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900147 char addrstr[INET_ADDRSTRLEN];
148 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr));
149 logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4);
150
junyulaic4e591a2018-11-26 22:36:10 +0900151 if ((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
152 logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status));
Daniel Drowna45056e2012-03-23 10:42:54 -0500153 exit(1);
154 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500155}
156
junyulaib5e8f972018-10-29 23:10:15 +0800157/* function: set_capability
158 * set the permitted, effective and inheritable capabilities of the current
159 * thread
Daniel Drowna45056e2012-03-23 10:42:54 -0500160 */
junyulaib5e8f972018-10-29 23:10:15 +0800161void set_capability(uint64_t target_cap) {
162 struct __user_cap_header_struct header = {
163 .version = _LINUX_CAPABILITY_VERSION_3,
164 .pid = 0 // 0 = change myself
165 };
166 struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {};
167
168 cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap;
169 cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32;
170
171 if (capset(&header, cap) < 0) {
172 logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno));
173 exit(1);
174 }
175}
176
177/* function: drop_root_but_keep_caps
178 * drops root privs but keeps the needed capabilities
179 */
180void drop_root_but_keep_caps() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900181 gid_t groups[] = { AID_INET, AID_VPN };
junyulaic4e591a2018-11-26 22:36:10 +0900182 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) {
junyulaib5e8f972018-10-29 23:10:15 +0800183 logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500184 exit(1);
185 }
186
junyulaib5e8f972018-10-29 23:10:15 +0800187 prctl(PR_SET_KEEPCAPS, 1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500188
junyulaib5e8f972018-10-29 23:10:15 +0800189 if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
190 logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500191 exit(1);
192 }
junyulaib5e8f972018-10-29 23:10:15 +0800193 if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) {
194 logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500195 exit(1);
196 }
197
junyulaib5e8f972018-10-29 23:10:15 +0800198 // keep CAP_NET_RAW capability to open raw socket, and CAP_IPC_LOCK for mmap
199 // to lock memory.
200 set_capability((1 << CAP_NET_ADMIN) |
201 (1 << CAP_NET_RAW) |
202 (1 << CAP_IPC_LOCK));
Daniel Drowna45056e2012-03-23 10:42:54 -0500203}
204
Lorenzo Colitti75647612014-06-09 13:55:50 +0900205/* function: open_sockets
206 * opens a packet socket to receive IPv6 packets and a raw socket to send them
junyulaic4e591a2018-11-26 22:36:10 +0900207 * tunnel - tun device data
208 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900209 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900210void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900211 int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_RAW);
Lorenzo Colittice140882014-06-02 21:20:40 +0900212 if (rawsock < 0) {
213 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
214 exit(1);
215 }
216
217 int off = 0;
218 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
219 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
220 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900221 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
222 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
223 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900224
225 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900226
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900227 tunnel->read_fd6 = ring_create(tunnel);
228 if (tunnel->read_fd6 < 0) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900229 exit(1);
230 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900231}
232
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900233/* function: update_clat_ipv6_address
234 * picks the clat IPv6 address and configures packet translation to use it.
junyulaic4e591a2018-11-26 22:36:10 +0900235 * tunnel - tun device data
236 * interface - uplink interface name
237 * returns: 1 on success, 0 on failure
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900238 */
239int update_clat_ipv6_address(const struct tun_data *tunnel, const char *interface) {
240 union anyip *interface_ip;
241 char addrstr[INET6_ADDRSTRLEN];
242
243 // TODO: check that the prefix length is /64.
244 interface_ip = getinterface_ip(interface, AF_INET6);
245 if (!interface_ip) {
246 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
247 return 0;
248 }
249
250 // If our prefix hasn't changed, do nothing. (If this is the first time we configure an IPv6
251 // address, Global_Clatd_Config.ipv6_local_subnet will be ::, which won't match our new prefix.)
252 if (ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
253 free(interface_ip);
254 return 1;
255 }
256
257 // Generate an interface ID.
258 config_generate_local_ipv6_subnet(&interface_ip->ip6);
259 inet_ntop(AF_INET6, &interface_ip->ip6, addrstr, sizeof(addrstr));
260
261 if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_local_subnet)) {
262 // Startup.
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900263 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900264 } else {
265 // Prefix change.
266 char from_addr[INET6_ADDRSTRLEN];
267 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
268 logmsg(ANDROID_LOG_INFO, "clat IPv6 address changed from %s to %s", from_addr, addrstr);
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900269 del_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900270 }
271
272 // Start translating packets to the new prefix.
273 Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6;
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900274 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900275 free(interface_ip);
276
277 // Update our packet socket filter to reflect the new 464xlat IP address.
278 if (!configure_packet_socket(tunnel->read_fd6)) {
junyulaic4e591a2018-11-26 22:36:10 +0900279 // Things aren't going to work. Bail out and hope we have better luck next time.
280 // We don't log an error here because configure_packet_socket has already done so.
281 exit(1);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900282 }
283
284 return 1;
285}
286
Daniel Drowna45056e2012-03-23 10:42:54 -0500287/* function: configure_interface
288 * reads the configuration and applies it to the interface
junyulaic4e591a2018-11-26 22:36:10 +0900289 * uplink_interface - network interface to use to reach the ipv6 internet
290 * plat_prefix - PLAT prefix to use
291 * tunnel - tun device data
292 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500293 */
junyulaic4e591a2018-11-26 22:36:10 +0900294void configure_interface(const char *uplink_interface, const char *plat_prefix,
295 struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500296 int error;
297
junyulaic4e591a2018-11-26 22:36:10 +0900298 if (!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
299 logmsg(ANDROID_LOG_FATAL, "read_config failed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500300 exit(1);
301 }
302
junyulaic4e591a2018-11-26 22:36:10 +0900303 if (Global_Clatd_Config.mtu > MAXMTU) {
304 logmsg(ANDROID_LOG_WARN, "Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500305 Global_Clatd_Config.mtu = MAXMTU;
306 }
junyulaic4e591a2018-11-26 22:36:10 +0900307 if (Global_Clatd_Config.mtu <= 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500308 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
junyulaic4e591a2018-11-26 22:36:10 +0900309 logmsg(ANDROID_LOG_WARN, "ifmtu=%d", Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500310 }
junyulaic4e591a2018-11-26 22:36:10 +0900311 if (Global_Clatd_Config.mtu < 1280) {
312 logmsg(ANDROID_LOG_WARN, "mtu too small = %d", Global_Clatd_Config.mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500313 Global_Clatd_Config.mtu = 1280;
314 }
315
junyulaic4e591a2018-11-26 22:36:10 +0900316 if (Global_Clatd_Config.ipv4mtu <= 0 ||
317 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900318 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
junyulaic4e591a2018-11-26 22:36:10 +0900319 logmsg(ANDROID_LOG_WARN, "ipv4mtu now set to = %d", Global_Clatd_Config.ipv4mtu);
Daniel Drowna45056e2012-03-23 10:42:54 -0500320 }
321
Daniel Drowna45056e2012-03-23 10:42:54 -0500322 error = tun_alloc(tunnel->device4, tunnel->fd4);
junyulaic4e591a2018-11-26 22:36:10 +0900323 if (error < 0) {
324 logmsg(ANDROID_LOG_FATAL, "tun_alloc/4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500325 exit(1);
326 }
327
Lorenzo Colitti49454812015-01-31 19:18:47 +0900328 error = set_nonblocking(tunnel->fd4);
329 if (error < 0) {
330 logmsg(ANDROID_LOG_FATAL, "set_nonblocking failed: %s", strerror(errno));
331 exit(1);
332 }
333
Daniel Drowna45056e2012-03-23 10:42:54 -0500334 configure_tun_ip(tunnel);
335}
336
Daniel Drowna45056e2012-03-23 10:42:54 -0500337/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900338 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +0900339 * read_fd - file descriptor to read original packet from
340 * write_fd - file descriptor to write translated packet to
341 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500342 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900343void read_packet(int read_fd, int write_fd, int to_ipv6) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500344 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900345 uint8_t buf[PACKETLEN], *packet;
Daniel Drowna45056e2012-03-23 10:42:54 -0500346
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900347 readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500348
junyulaic4e591a2018-11-26 22:36:10 +0900349 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900350 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +0900351 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900352 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500353 return;
junyulaic4e591a2018-11-26 22:36:10 +0900354 } else if (readlen == 0) {
355 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -0500356 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900357 return;
358 }
359
junyulaic4e591a2018-11-26 22:36:10 +0900360 struct tun_pi *tun_header = (struct tun_pi *)buf;
361 if (readlen < (ssize_t)sizeof(*tun_header)) {
362 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900363 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500364 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900365
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900366 uint16_t proto = ntohs(tun_header->proto);
367 if (proto != ETH_P_IP) {
368 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
369 return;
370 }
371
junyulaic4e591a2018-11-26 22:36:10 +0900372 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900373 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
374 }
375
junyulaic4e591a2018-11-26 22:36:10 +0900376 packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900377 readlen -= sizeof(*tun_header);
378 translate_packet(write_fd, to_ipv6, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500379}
380
381/* function: event_loop
382 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900383 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500384 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900385void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500386 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700387 struct pollfd wait_fd[] = {
388 { tunnel->read_fd6, POLLIN, 0 },
389 { tunnel->fd4, POLLIN, 0 },
390 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500391
392 // start the poll timer
393 last_interface_poll = time(NULL);
394
junyulaic4e591a2018-11-26 22:36:10 +0900395 while (running) {
396 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900397 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900398 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500399 }
400 } else {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900401 if (wait_fd[0].revents & POLLIN) {
402 ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */);
403 }
404 // If any other bit is set, assume it's due to an error (i.e. POLLERR).
405 if (wait_fd[0].revents & ~POLLIN) {
406 // ring_read doesn't clear the error indication on the socket.
407 recv(tunnel->read_fd6, NULL, 0, MSG_PEEK);
junyulaic4e591a2018-11-26 22:36:10 +0900408 logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno));
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900409 }
410
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900411 // Call read_packet if the socket has data to be read, but also if an
412 // error is waiting. If we don't call read() after getting POLLERR, a
413 // subsequent poll() will return immediately with POLLERR again,
414 // causing this code to spin in a loop. Calling read() will clear the
415 // socket error flag instead.
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900416 if (wait_fd[1].revents) {
417 read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500418 }
419 }
420
421 time_t now = time(NULL);
junyulaic4e591a2018-11-26 22:36:10 +0900422 if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900423 update_clat_ipv6_address(tunnel, Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500424 last_interface_poll = now;
425 }
426 }
427}
428
429/* function: print_help
430 * in case the user is running this on the command line
431 */
432void print_help() {
433 printf("android-clat arguments:\n");
434 printf("-i [uplink interface]\n");
435 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400436 printf("-n [NetId]\n");
Lorenzo Colitti75647612014-06-09 13:55:50 +0900437 printf("-m [socket mark]\n");
438}
439
440/* function: parse_unsigned
441 * parses a string as a decimal/hex/octal unsigned integer
junyulaic4e591a2018-11-26 22:36:10 +0900442 * str - the string to parse
443 * out - the unsigned integer to write to, gets clobbered on failure
Lorenzo Colitti75647612014-06-09 13:55:50 +0900444 */
445int parse_unsigned(const char *str, unsigned *out) {
junyulaic4e591a2018-11-26 22:36:10 +0900446 char *end_ptr;
447 *out = strtoul(str, &end_ptr, 0);
448 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500449}
450
451/* function: main
452 * allocate and setup the tun device, then run the event loop
453 */
454int main(int argc, char **argv) {
455 struct tun_data tunnel;
456 int opt;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900457 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Paul Jensen247dd712014-05-30 13:19:10 -0400458 unsigned net_id = NETID_UNSET;
junyulaic4e591a2018-11-26 22:36:10 +0900459 uint32_t mark = MARK_UNSET;
Lorenzo Colitti76129162014-10-20 17:24:51 +0900460 unsigned len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500461
junyulaic4e591a2018-11-26 22:36:10 +0900462 while ((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
463 switch (opt) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500464 case 'i':
465 uplink_interface = optarg;
466 break;
467 case 'p':
468 plat_prefix = optarg;
469 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400470 case 'n':
471 net_id_str = optarg;
472 break;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900473 case 'm':
474 mark_str = optarg;
475 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500476 case 'h':
Daniel Drowna45056e2012-03-23 10:42:54 -0500477 print_help();
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900478 exit(0);
479 default:
junyulaic4e591a2018-11-26 22:36:10 +0900480 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500481 exit(1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500482 }
483 }
484
junyulaic4e591a2018-11-26 22:36:10 +0900485 if (uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900486 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500487 exit(1);
488 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900489
490 if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) {
491 logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str);
492 exit(1);
Paul Jensen247dd712014-05-30 13:19:10 -0400493 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900494
495 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
496 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
497 exit(1);
498 }
499
Lorenzo Colitti76129162014-10-20 17:24:51 +0900500 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
501 if (len >= sizeof(tunnel.device4)) {
502 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
503 exit(1);
504 }
505
junyulaic4e591a2018-11-26 22:36:10 +0900506 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s", CLATD_VERSION,
507 uplink_interface, net_id_str ? net_id_str : "(none)", mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500508
junyulaib5e8f972018-10-29 23:10:15 +0800509 // run under a regular user but keep needed capabilities
510 drop_root_but_keep_caps();
511
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900512 // open our raw sockets before dropping privs
513 open_sockets(&tunnel, mark);
514
junyulaib5e8f972018-10-29 23:10:15 +0800515 // keeps only admin capability
516 set_capability(1 << CAP_NET_ADMIN);
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900517
518 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500519 tunnel.fd4 = tun_open();
junyulaic4e591a2018-11-26 22:36:10 +0900520 if (tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900521 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500522 exit(1);
523 }
524
Jayachandran Chinnakkannuf5fe2672018-01-04 18:08:53 +0000525 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
526 // "local", but that only works for the netd process itself. Removing the
527 // following line causes XLAT failure in permissive mode.
528 unsetenv("ANDROID_DNS_MODE");
529
Paul Jensen247dd712014-05-30 13:19:10 -0400530 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500531
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900532 update_clat_ipv6_address(&tunnel, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500533
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900534 // Loop until someone sends us a signal or brings down the tun interface.
junyulaic4e591a2018-11-26 22:36:10 +0900535 if (signal(SIGTERM, stop_loop) == SIG_ERR) {
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900536 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
537 exit(1);
538 }
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900539
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900540 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500541
junyulaic4e591a2018-11-26 22:36:10 +0900542 logmsg(ANDROID_LOG_INFO, "Shutting down clat on %s", uplink_interface);
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900543 del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
Daniel Drowna45056e2012-03-23 10:42:54 -0500544
545 return 0;
546}