blob: c2199ac33dfb1cf367ba72d4d93b02d382f5078c [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>
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include <linux/prctl.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090036#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050037#include <linux/if.h>
38#include <linux/if_tun.h>
39#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090040#include <linux/if_packet.h>
41#include <net/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050042
43#include <private/android_filesystem_config.h>
44
Lorenzo Colittid9084182013-03-22 00:42:21 +090045#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050046#include "clatd.h"
47#include "config.h"
48#include "logging.h"
Paul Jensen247dd712014-05-30 13:19:10 -040049#include "resolv_netid.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050050#include "setif.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050051#include "mtu.h"
52#include "getaddr.h"
53#include "dump.h"
54
Daniel Drowna45056e2012-03-23 10:42:54 -050055#define DEVICENAME4 "clat4"
56
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
69/* function: tun_open
70 * tries to open the tunnel device
71 */
72int tun_open() {
73 int fd;
74
75 fd = open("/dev/tun", O_RDWR);
76 if(fd < 0) {
77 fd = open("/dev/net/tun", O_RDWR);
78 }
79
80 return fd;
81}
82
83/* function: tun_alloc
84 * creates a tun interface and names it
85 * dev - the name for the new tun device
86 */
87int tun_alloc(char *dev, int fd) {
88 struct ifreq ifr;
89 int err;
90
91 memset(&ifr, 0, sizeof(ifr));
92
93 ifr.ifr_flags = IFF_TUN;
94 if( *dev ) {
95 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
96 ifr.ifr_name[IFNAMSIZ-1] = '\0';
97 }
98
99 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
100 close(fd);
101 return err;
102 }
103 strcpy(dev, ifr.ifr_name);
104 return 0;
105}
106
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900107/* function: configure_packet_socket
108 * Binds the packet socket and attaches the receive filter to it.
109 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900111int configure_packet_socket(int sock) {
112 struct sockaddr_ll sll = {
113 .sll_family = AF_PACKET,
114 .sll_protocol = htons(ETH_P_IPV6),
115 .sll_ifindex = if_nametoindex((char *) &Global_Clatd_Config.default_pdp_interface),
116 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
117 };
118 if (bind(sock, (struct sockaddr *) &sll, sizeof(sll))) {
119 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
120 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500121 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500122
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900123 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
124 struct sock_filter filter_code[] = {
125 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
126 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
127 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
128 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
129 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
130 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
131 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
132 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
133 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
134 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
135 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
136 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
137 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
138 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
139 BPF_STMT(BPF_RET | BPF_K, 0)
140 };
141 struct sock_fprog filter = {
142 sizeof(filter_code) / sizeof(filter_code[0]),
143 filter_code
144 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500145
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900146 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
147 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
148 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500149 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900150
151 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500152}
153
154/* function: interface_poll
155 * polls the uplink network interface for address changes
156 * tunnel - tun device data
157 */
158void interface_poll(const struct tun_data *tunnel) {
159 union anyip *interface_ip;
160
161 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
162 if(!interface_ip) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900163 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",
164 Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500165 return;
166 }
167
168 config_generate_local_ipv6_subnet(&interface_ip->ip6);
169
170 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
171 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
172 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
173 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
174 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
175
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900176 // Start translating packets to the new prefix.
Daniel Drowna45056e2012-03-23 10:42:54 -0500177 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900178
179 // Update our packet socket filter to reflect the new 464xlat IP address.
180 if (!configure_packet_socket(tunnel->read_fd6)) {
181 // Things aren't going to work. Bail out and hope we have better luck next time.
Lorenzo Colitti75647612014-06-09 13:55:50 +0900182 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900183 exit(1);
184 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500185 }
186
187 free(interface_ip);
188}
189
190/* function: configure_tun_ip
191 * configures the ipv4 and ipv6 addresses on the tunnel interface
192 * tunnel - tun device data
193 */
194void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500195 int status;
196
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900197 // Configure the interface before bringing it up. As soon as we bring the interface up, the
198 // framework will be notified and will assume the interface's configuration has been finalized.
199 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
200 32, &Global_Clatd_Config.ipv4_local_subnet);
201 if(status < 0) {
202 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
203 exit(1);
204 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500205
Daniel Drowna45056e2012-03-23 10:42:54 -0500206 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
207 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
208 exit(1);
209 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500210}
211
212/* function: drop_root
213 * drops root privs but keeps the needed capability
214 */
215void drop_root() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900216 gid_t groups[] = { AID_INET, AID_VPN };
Daniel Drowna45056e2012-03-23 10:42:54 -0500217 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
218 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
219 exit(1);
220 }
221
222 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
223
224 if(setgid(AID_CLAT) < 0) {
225 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
226 exit(1);
227 }
228 if(setuid(AID_CLAT) < 0) {
229 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
230 exit(1);
231 }
232
233 struct __user_cap_header_struct header;
234 struct __user_cap_data_struct cap;
235 memset(&header, 0, sizeof(header));
236 memset(&cap, 0, sizeof(cap));
237
238 header.version = _LINUX_CAPABILITY_VERSION;
239 header.pid = 0; // 0 = change myself
240 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
241
242 if(capset(&header, &cap) < 0) {
243 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
244 exit(1);
245 }
246}
247
Lorenzo Colitti75647612014-06-09 13:55:50 +0900248/* function: open_sockets
249 * opens a packet socket to receive IPv6 packets and a raw socket to send them
250 * tunnel - tun device data
251 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900252 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900253void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900254 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
255 if (rawsock < 0) {
256 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
257 exit(1);
258 }
259
260 int off = 0;
261 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
262 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
263 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900264 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
265 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
266 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900267
268 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900269
270 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
271 if (packetsock < 0) {
272 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
273 exit(1);
274 }
275
276 tunnel->read_fd6 = packetsock;
Lorenzo Colittice140882014-06-02 21:20:40 +0900277}
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;
379 struct pollfd wait_fd[2];
380
381 // start the poll timer
382 last_interface_poll = time(NULL);
383
Lorenzo Colittice140882014-06-02 21:20:40 +0900384 wait_fd[0].fd = tunnel->read_fd6;
Daniel Drowna45056e2012-03-23 10:42:54 -0500385 wait_fd[0].events = POLLIN;
386 wait_fd[0].revents = 0;
387 wait_fd[1].fd = tunnel->fd4;
388 wait_fd[1].events = POLLIN;
389 wait_fd[1].revents = 0;
390
391 while(running) {
392 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
393 if(errno != EINTR) {
394 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
395 }
396 } else {
397 int i;
398 for(i = 0; i < 2; i++) {
399 if((wait_fd[i].revents & POLLIN) != 0) {
400 read_packet(wait_fd[i].fd,tunnel);
401 }
402 }
403 }
404
405 time_t now = time(NULL);
406 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
407 interface_poll(tunnel);
408 last_interface_poll = now;
409 }
410 }
411}
412
413/* function: print_help
414 * in case the user is running this on the command line
415 */
416void print_help() {
417 printf("android-clat arguments:\n");
418 printf("-i [uplink interface]\n");
419 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400420 printf("-n [NetId]\n");
Lorenzo Colitti75647612014-06-09 13:55:50 +0900421 printf("-m [socket mark]\n");
422}
423
424/* function: parse_unsigned
425 * parses a string as a decimal/hex/octal unsigned integer
426 * str - the string to parse
427 * out - the unsigned integer to write to, gets clobbered on failure
428 */
429int parse_unsigned(const char *str, unsigned *out) {
430 char *end_ptr;
431 *out = strtoul(str, &end_ptr, 0);
432 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500433}
434
435/* function: main
436 * allocate and setup the tun device, then run the event loop
437 */
438int main(int argc, char **argv) {
439 struct tun_data tunnel;
440 int opt;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900441 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Paul Jensen247dd712014-05-30 13:19:10 -0400442 unsigned net_id = NETID_UNSET;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900443 uint32_t mark = MARK_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500444
Daniel Drowna45056e2012-03-23 10:42:54 -0500445 strcpy(tunnel.device4, DEVICENAME4);
446
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 Colittib9b471e2014-06-09 19:39:01 +0900485 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
486 CLATD_VERSION, uplink_interface,
487 net_id_str ? net_id_str : "(none)",
488 mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500489
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900490 // open our raw sockets before dropping privs
491 open_sockets(&tunnel, mark);
492
493 // run under a regular user
494 drop_root();
495
496 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500497 tunnel.fd4 = tun_open();
498 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900499 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500500 exit(1);
501 }
502
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900503 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
504 // "local", but that only works for the netd process itself.
505 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500506
Paul Jensen247dd712014-05-30 13:19:10 -0400507 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500508
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900509 if (!configure_packet_socket(tunnel.read_fd6)) {
510 // We've already logged an error.
511 exit(1);
512 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500513
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900514 // Loop until someone sends us a signal or brings down the tun interface.
515 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
516 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
517 exit(1);
518 }
519 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500520
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900521 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500522
523 return 0;
524}