blob: 1de39c87f36d31b3910d38d8b3ce170493a0da57 [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 */
18#include <poll.h>
19#include <signal.h>
20#include <time.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <sys/ioctl.h>
Elliott Hughes3afe9ae2014-07-18 17:25:26 -070024#include <sys/prctl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050025#include <sys/stat.h>
26#include <string.h>
27#include <errno.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <arpa/inet.h>
31#include <fcntl.h>
32
Nick Kralevich2edb7562013-02-28 14:15:22 -080033#include <sys/capability.h>
Lorenzo Colittid9084182013-03-22 00:42:21 +090034#include <sys/uio.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090035#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050036#include <linux/if.h>
37#include <linux/if_tun.h>
38#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090039#include <linux/if_packet.h>
40#include <net/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050041
42#include <private/android_filesystem_config.h>
43
Lorenzo Colittid9084182013-03-22 00:42:21 +090044#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050045#include "clatd.h"
46#include "config.h"
47#include "logging.h"
Paul Jensen247dd712014-05-30 13:19:10 -040048#include "resolv_netid.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050049#include "setif.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050050#include "mtu.h"
51#include "getaddr.h"
52#include "dump.h"
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090053#include "tun.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050054
Lorenzo Colitti76129162014-10-20 17:24:51 +090055#define DEVICEPREFIX "v4-"
Daniel Drowna45056e2012-03-23 10:42:54 -050056
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090057/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
58#define MTU_DELTA 28
59
Daniel Drowna45056e2012-03-23 10:42:54 -050060volatile sig_atomic_t running = 1;
61
Lorenzo Colittibaf62992013-03-01 20:29:39 +090062/* function: stop_loop
63 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050064 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090065void stop_loop() {
Daniel Drowna45056e2012-03-23 10:42:54 -050066 running = 0;
67}
68
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090069/* function: configure_packet_socket
70 * Binds the packet socket and attaches the receive filter to it.
71 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050072 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090073int configure_packet_socket(int sock) {
74 struct sockaddr_ll sll = {
75 .sll_family = AF_PACKET,
76 .sll_protocol = htons(ETH_P_IPV6),
77 .sll_ifindex = if_nametoindex((char *) &Global_Clatd_Config.default_pdp_interface),
78 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
79 };
80 if (bind(sock, (struct sockaddr *) &sll, sizeof(sll))) {
81 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
82 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050083 }
Daniel Drowna45056e2012-03-23 10:42:54 -050084
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090085 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
86 struct sock_filter filter_code[] = {
87 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
88 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
89 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
90 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
91 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
92 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
93 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
94 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
95 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
96 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
97 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
98 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
99 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
100 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
101 BPF_STMT(BPF_RET | BPF_K, 0)
102 };
103 struct sock_fprog filter = {
104 sizeof(filter_code) / sizeof(filter_code[0]),
105 filter_code
106 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500107
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900108 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
109 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
110 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500111 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900112
113 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500114}
115
Daniel Drowna45056e2012-03-23 10:42:54 -0500116/* function: configure_tun_ip
117 * configures the ipv4 and ipv6 addresses on the tunnel interface
118 * tunnel - tun device data
119 */
120void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500121 int status;
122
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900123 // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically,
124 // there is a race here - if another clatd calls config_select_ipv4_address after we do, but
125 // before we call add_address, it can end up having the same IP address as we do. But the time
126 // window in which this can happen is extremely small, and even if we end up with a duplicate
127 // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go
128 // down.
129 in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet,
130 Global_Clatd_Config.ipv4_local_prefixlen);
131 if (localaddr == INADDR_NONE) {
132 logmsg(ANDROID_LOG_FATAL,"No free IPv4 address in %s/%d",
133 inet_ntoa(Global_Clatd_Config.ipv4_local_subnet),
134 Global_Clatd_Config.ipv4_local_prefixlen);
135 exit(1);
136 }
137 Global_Clatd_Config.ipv4_local_subnet.s_addr = localaddr;
138
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900139 // Configure the interface before bringing it up. As soon as we bring the interface up, the
140 // framework will be notified and will assume the interface's configuration has been finalized.
141 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
142 32, &Global_Clatd_Config.ipv4_local_subnet);
143 if(status < 0) {
144 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
145 exit(1);
146 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500147
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900148 char addrstr[INET_ADDRSTRLEN];
149 inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr));
150 logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4);
151
Daniel Drowna45056e2012-03-23 10:42:54 -0500152 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
153 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
154 exit(1);
155 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500156}
157
158/* function: drop_root
159 * drops root privs but keeps the needed capability
160 */
161void drop_root() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900162 gid_t groups[] = { AID_INET, AID_VPN };
Daniel Drowna45056e2012-03-23 10:42:54 -0500163 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
164 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
165 exit(1);
166 }
167
168 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
169
170 if(setgid(AID_CLAT) < 0) {
171 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
172 exit(1);
173 }
174 if(setuid(AID_CLAT) < 0) {
175 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
176 exit(1);
177 }
178
179 struct __user_cap_header_struct header;
180 struct __user_cap_data_struct cap;
181 memset(&header, 0, sizeof(header));
182 memset(&cap, 0, sizeof(cap));
183
184 header.version = _LINUX_CAPABILITY_VERSION;
185 header.pid = 0; // 0 = change myself
186 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
187
188 if(capset(&header, &cap) < 0) {
189 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
190 exit(1);
191 }
192}
193
Lorenzo Colitti75647612014-06-09 13:55:50 +0900194/* function: open_sockets
195 * opens a packet socket to receive IPv6 packets and a raw socket to send them
196 * tunnel - tun device data
197 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900198 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900199void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900200 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
201 if (rawsock < 0) {
202 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
203 exit(1);
204 }
205
206 int off = 0;
207 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
208 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
209 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900210 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
211 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
212 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900213
214 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900215
216 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
217 if (packetsock < 0) {
218 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
219 exit(1);
220 }
221
222 tunnel->read_fd6 = packetsock;
Lorenzo Colittice140882014-06-02 21:20:40 +0900223}
224
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900225/* function: update_clat_ipv6_address
226 * picks the clat IPv6 address and configures packet translation to use it.
227 * tunnel - tun device data
228 * interface - uplink interface name
229 * returns: 1 on success, 0 on failure
230 */
231int update_clat_ipv6_address(const struct tun_data *tunnel, const char *interface) {
232 union anyip *interface_ip;
233 char addrstr[INET6_ADDRSTRLEN];
234
235 // TODO: check that the prefix length is /64.
236 interface_ip = getinterface_ip(interface, AF_INET6);
237 if (!interface_ip) {
238 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
239 return 0;
240 }
241
242 // If our prefix hasn't changed, do nothing. (If this is the first time we configure an IPv6
243 // address, Global_Clatd_Config.ipv6_local_subnet will be ::, which won't match our new prefix.)
244 if (ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
245 free(interface_ip);
246 return 1;
247 }
248
249 // Generate an interface ID.
250 config_generate_local_ipv6_subnet(&interface_ip->ip6);
251 inet_ntop(AF_INET6, &interface_ip->ip6, addrstr, sizeof(addrstr));
252
253 if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_local_subnet)) {
254 // Startup.
Lorenzo Colitti798f9932014-10-31 21:54:33 +0900255 logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900256 } else {
257 // Prefix change.
258 char from_addr[INET6_ADDRSTRLEN];
259 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
260 logmsg(ANDROID_LOG_INFO, "clat IPv6 address changed from %s to %s", from_addr, addrstr);
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900261 del_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900262 }
263
264 // Start translating packets to the new prefix.
265 Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6;
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900266 add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface);
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900267 free(interface_ip);
268
269 // Update our packet socket filter to reflect the new 464xlat IP address.
270 if (!configure_packet_socket(tunnel->read_fd6)) {
271 // Things aren't going to work. Bail out and hope we have better luck next time.
272 // We don't log an error here because configure_packet_socket has already done so.
273 exit(1);
274 }
275
276 return 1;
277}
278
Daniel Drowna45056e2012-03-23 10:42:54 -0500279/* function: configure_interface
280 * reads the configuration and applies it to the interface
281 * uplink_interface - network interface to use to reach the ipv6 internet
282 * plat_prefix - PLAT prefix to use
283 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400284 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500285 */
Paul Jensen247dd712014-05-30 13:19:10 -0400286void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500287 int error;
288
Paul Jensen247dd712014-05-30 13:19:10 -0400289 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500290 logmsg(ANDROID_LOG_FATAL,"read_config failed");
291 exit(1);
292 }
293
294 if(Global_Clatd_Config.mtu > MAXMTU) {
295 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
296 Global_Clatd_Config.mtu = MAXMTU;
297 }
298 if(Global_Clatd_Config.mtu <= 0) {
299 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
300 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
301 }
302 if(Global_Clatd_Config.mtu < 1280) {
303 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
304 Global_Clatd_Config.mtu = 1280;
305 }
306
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900307 if(Global_Clatd_Config.ipv4mtu <= 0 ||
308 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
309 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500310 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
311 }
312
Daniel Drowna45056e2012-03-23 10:42:54 -0500313 error = tun_alloc(tunnel->device4, tunnel->fd4);
314 if(error < 0) {
315 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
316 exit(1);
317 }
318
319 configure_tun_ip(tunnel);
320}
321
Daniel Drowna45056e2012-03-23 10:42:54 -0500322/* function: read_packet
323 * reads a packet from the tunnel fd and passes it down the stack
324 * active_fd - tun file descriptor marked ready for reading
325 * tunnel - tun device data
326 */
327void read_packet(int active_fd, const struct tun_data *tunnel) {
328 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900329 uint8_t buf[PACKETLEN], *packet;
330 int fd;
Daniel Drowna45056e2012-03-23 10:42:54 -0500331
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900332 readlen = read(active_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500333
334 if(readlen < 0) {
335 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
336 return;
337 } else if(readlen == 0) {
338 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
339 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900340 return;
341 }
342
343 if (active_fd == tunnel->fd4) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500344 ssize_t header_size = sizeof(struct tun_pi);
345
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900346 if (readlen < header_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500347 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
348 return;
349 }
350
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900351 struct tun_pi *tun_header = (struct tun_pi *) buf;
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900352 uint16_t proto = ntohs(tun_header->proto);
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900353 if (proto != ETH_P_IP) {
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900354 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
355 return;
356 }
357
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900358 if(tun_header->flags != 0) {
359 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
360 }
361
362 fd = tunnel->write_fd6;
363 packet = buf + header_size;
364 readlen -= header_size;
365 } else {
366 fd = tunnel->fd4;
367 packet = buf;
Daniel Drowna45056e2012-03-23 10:42:54 -0500368 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900369
370 translate_packet(fd, (fd == tunnel->write_fd6), packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500371}
372
373/* function: event_loop
374 * reads packets from the tun network interface and passes them down the stack
375 * tunnel - tun device data
376 */
377void event_loop(const struct tun_data *tunnel) {
378 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700379 struct pollfd wait_fd[] = {
380 { tunnel->read_fd6, POLLIN, 0 },
381 { tunnel->fd4, POLLIN, 0 },
382 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500383
384 // start the poll timer
385 last_interface_poll = time(NULL);
386
Daniel Drowna45056e2012-03-23 10:42:54 -0500387 while(running) {
388 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
389 if(errno != EINTR) {
390 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
391 }
392 } else {
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700393 size_t i;
394 for(i = 0; i < ARRAY_SIZE(wait_fd); i++) {
395 // Call read_packet if the socket has data to be read, but also if an
396 // error is waiting. If we don't call read() after getting POLLERR, a
397 // subsequent poll() will return immediately with POLLERR again,
398 // causing this code to spin in a loop. Calling read() will clear the
399 // socket error flag instead.
400 if(wait_fd[i].revents != 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500401 read_packet(wait_fd[i].fd,tunnel);
402 }
403 }
404 }
405
406 time_t now = time(NULL);
407 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900408 update_clat_ipv6_address(tunnel, Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500409 last_interface_poll = now;
410 }
411 }
412}
413
414/* function: print_help
415 * in case the user is running this on the command line
416 */
417void print_help() {
418 printf("android-clat arguments:\n");
419 printf("-i [uplink interface]\n");
420 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400421 printf("-n [NetId]\n");
Lorenzo Colitti75647612014-06-09 13:55:50 +0900422 printf("-m [socket mark]\n");
423}
424
425/* function: parse_unsigned
426 * parses a string as a decimal/hex/octal unsigned integer
427 * str - the string to parse
428 * out - the unsigned integer to write to, gets clobbered on failure
429 */
430int parse_unsigned(const char *str, unsigned *out) {
431 char *end_ptr;
432 *out = strtoul(str, &end_ptr, 0);
433 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500434}
435
436/* function: main
437 * allocate and setup the tun device, then run the event loop
438 */
439int main(int argc, char **argv) {
440 struct tun_data tunnel;
441 int opt;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900442 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Paul Jensen247dd712014-05-30 13:19:10 -0400443 unsigned net_id = NETID_UNSET;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900444 uint32_t mark = MARK_UNSET;
Lorenzo Colitti76129162014-10-20 17:24:51 +0900445 unsigned len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500446
Lorenzo Colitti75647612014-06-09 13:55:50 +0900447 while((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500448 switch(opt) {
449 case 'i':
450 uplink_interface = optarg;
451 break;
452 case 'p':
453 plat_prefix = optarg;
454 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400455 case 'n':
456 net_id_str = optarg;
457 break;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900458 case 'm':
459 mark_str = optarg;
460 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500461 case 'h':
Daniel Drowna45056e2012-03-23 10:42:54 -0500462 print_help();
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900463 exit(0);
464 default:
465 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char) optopt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500466 exit(1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500467 }
468 }
469
470 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900471 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500472 exit(1);
473 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900474
475 if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) {
476 logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str);
477 exit(1);
Paul Jensen247dd712014-05-30 13:19:10 -0400478 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900479
480 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
481 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
482 exit(1);
483 }
484
Lorenzo Colitti76129162014-10-20 17:24:51 +0900485 len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
486 if (len >= sizeof(tunnel.device4)) {
487 logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
488 exit(1);
489 }
490
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900491 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
492 CLATD_VERSION, uplink_interface,
493 net_id_str ? net_id_str : "(none)",
494 mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500495
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900496 // open our raw sockets before dropping privs
497 open_sockets(&tunnel, mark);
498
499 // run under a regular user
500 drop_root();
501
502 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500503 tunnel.fd4 = tun_open();
504 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900505 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500506 exit(1);
507 }
508
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900509 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
510 // "local", but that only works for the netd process itself.
511 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500512
Paul Jensen247dd712014-05-30 13:19:10 -0400513 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500514
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900515 update_clat_ipv6_address(&tunnel, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500516
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900517 // Loop until someone sends us a signal or brings down the tun interface.
518 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
519 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
520 exit(1);
521 }
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900522
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900523 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500524
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900525 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Lorenzo Colitti8a41a5d2014-10-21 12:37:48 +0900526 del_anycast_address(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
Daniel Drowna45056e2012-03-23 10:42:54 -0500527
528 return 0;
529}