blob: 78ccccc6261c40005758cbe5159ba1e74b6b4de4 [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"
53
Lorenzo Colitti76129162014-10-20 17:24:51 +090054#define DEVICEPREFIX "v4-"
Daniel Drowna45056e2012-03-23 10:42:54 -050055
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090056/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
57#define MTU_DELTA 28
58
Daniel Drowna45056e2012-03-23 10:42:54 -050059volatile sig_atomic_t running = 1;
60
Lorenzo Colittibaf62992013-03-01 20:29:39 +090061/* function: stop_loop
62 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050063 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090064void stop_loop() {
Daniel Drowna45056e2012-03-23 10:42:54 -050065 running = 0;
66}
67
68/* function: tun_open
69 * tries to open the tunnel device
70 */
71int tun_open() {
72 int fd;
73
74 fd = open("/dev/tun", O_RDWR);
75 if(fd < 0) {
76 fd = open("/dev/net/tun", O_RDWR);
77 }
78
79 return fd;
80}
81
82/* function: tun_alloc
83 * creates a tun interface and names it
84 * dev - the name for the new tun device
85 */
86int tun_alloc(char *dev, int fd) {
87 struct ifreq ifr;
88 int err;
89
90 memset(&ifr, 0, sizeof(ifr));
91
92 ifr.ifr_flags = IFF_TUN;
93 if( *dev ) {
94 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
95 ifr.ifr_name[IFNAMSIZ-1] = '\0';
96 }
97
98 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
99 close(fd);
100 return err;
101 }
102 strcpy(dev, ifr.ifr_name);
103 return 0;
104}
105
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900106/* function: configure_packet_socket
107 * Binds the packet socket and attaches the receive filter to it.
108 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -0500109 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900110int configure_packet_socket(int sock) {
111 struct sockaddr_ll sll = {
112 .sll_family = AF_PACKET,
113 .sll_protocol = htons(ETH_P_IPV6),
114 .sll_ifindex = if_nametoindex((char *) &Global_Clatd_Config.default_pdp_interface),
115 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
116 };
117 if (bind(sock, (struct sockaddr *) &sll, sizeof(sll))) {
118 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
119 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500120 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500121
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900122 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
123 struct sock_filter filter_code[] = {
124 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
125 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
126 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
127 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
128 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
129 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
130 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
131 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
132 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
133 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
134 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
135 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
136 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
137 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
138 BPF_STMT(BPF_RET | BPF_K, 0)
139 };
140 struct sock_fprog filter = {
141 sizeof(filter_code) / sizeof(filter_code[0]),
142 filter_code
143 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500144
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900145 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
146 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
147 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500148 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900149
150 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500151}
152
Daniel Drowna45056e2012-03-23 10:42:54 -0500153/* function: configure_tun_ip
154 * configures the ipv4 and ipv6 addresses on the tunnel interface
155 * tunnel - tun device data
156 */
157void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500158 int status;
159
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900160 // Configure the interface before bringing it up. As soon as we bring the interface up, the
161 // framework will be notified and will assume the interface's configuration has been finalized.
162 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
163 32, &Global_Clatd_Config.ipv4_local_subnet);
164 if(status < 0) {
165 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
166 exit(1);
167 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500168
Daniel Drowna45056e2012-03-23 10:42:54 -0500169 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
170 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
171 exit(1);
172 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500173}
174
175/* function: drop_root
176 * drops root privs but keeps the needed capability
177 */
178void drop_root() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900179 gid_t groups[] = { AID_INET, AID_VPN };
Daniel Drowna45056e2012-03-23 10:42:54 -0500180 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
181 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
182 exit(1);
183 }
184
185 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
186
187 if(setgid(AID_CLAT) < 0) {
188 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
189 exit(1);
190 }
191 if(setuid(AID_CLAT) < 0) {
192 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
193 exit(1);
194 }
195
196 struct __user_cap_header_struct header;
197 struct __user_cap_data_struct cap;
198 memset(&header, 0, sizeof(header));
199 memset(&cap, 0, sizeof(cap));
200
201 header.version = _LINUX_CAPABILITY_VERSION;
202 header.pid = 0; // 0 = change myself
203 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
204
205 if(capset(&header, &cap) < 0) {
206 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
207 exit(1);
208 }
209}
210
Lorenzo Colitti75647612014-06-09 13:55:50 +0900211/* function: open_sockets
212 * opens a packet socket to receive IPv6 packets and a raw socket to send them
213 * tunnel - tun device data
214 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900215 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900216void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900217 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
218 if (rawsock < 0) {
219 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
220 exit(1);
221 }
222
223 int off = 0;
224 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
225 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
226 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900227 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
228 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
229 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900230
231 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900232
233 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
234 if (packetsock < 0) {
235 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
236 exit(1);
237 }
238
239 tunnel->read_fd6 = packetsock;
Lorenzo Colittice140882014-06-02 21:20:40 +0900240}
241
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900242/* function: update_clat_ipv6_address
243 * picks the clat IPv6 address and configures packet translation to use it.
244 * tunnel - tun device data
245 * interface - uplink interface name
246 * returns: 1 on success, 0 on failure
247 */
248int update_clat_ipv6_address(const struct tun_data *tunnel, const char *interface) {
249 union anyip *interface_ip;
250 char addrstr[INET6_ADDRSTRLEN];
251
252 // TODO: check that the prefix length is /64.
253 interface_ip = getinterface_ip(interface, AF_INET6);
254 if (!interface_ip) {
255 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
256 return 0;
257 }
258
259 // If our prefix hasn't changed, do nothing. (If this is the first time we configure an IPv6
260 // address, Global_Clatd_Config.ipv6_local_subnet will be ::, which won't match our new prefix.)
261 if (ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
262 free(interface_ip);
263 return 1;
264 }
265
266 // Generate an interface ID.
267 config_generate_local_ipv6_subnet(&interface_ip->ip6);
268 inet_ntop(AF_INET6, &interface_ip->ip6, addrstr, sizeof(addrstr));
269
270 if (IN6_IS_ADDR_UNSPECIFIED(&Global_Clatd_Config.ipv6_local_subnet)) {
271 // Startup.
272 logmsg(ANDROID_LOG_INFO, "Using %s on %s", addrstr, interface);
273 } else {
274 // Prefix change.
275 char from_addr[INET6_ADDRSTRLEN];
276 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
277 logmsg(ANDROID_LOG_INFO, "clat IPv6 address changed from %s to %s", from_addr, addrstr);
278 }
279
280 // Start translating packets to the new prefix.
281 Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6;
282 free(interface_ip);
283
284 // Update our packet socket filter to reflect the new 464xlat IP address.
285 if (!configure_packet_socket(tunnel->read_fd6)) {
286 // Things aren't going to work. Bail out and hope we have better luck next time.
287 // We don't log an error here because configure_packet_socket has already done so.
288 exit(1);
289 }
290
291 return 1;
292}
293
Daniel Drowna45056e2012-03-23 10:42:54 -0500294/* function: configure_interface
295 * reads the configuration and applies it to the interface
296 * uplink_interface - network interface to use to reach the ipv6 internet
297 * plat_prefix - PLAT prefix to use
298 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400299 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500300 */
Paul Jensen247dd712014-05-30 13:19:10 -0400301void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500302 int error;
303
Paul Jensen247dd712014-05-30 13:19:10 -0400304 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500305 logmsg(ANDROID_LOG_FATAL,"read_config failed");
306 exit(1);
307 }
308
309 if(Global_Clatd_Config.mtu > MAXMTU) {
310 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
311 Global_Clatd_Config.mtu = MAXMTU;
312 }
313 if(Global_Clatd_Config.mtu <= 0) {
314 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
315 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
316 }
317 if(Global_Clatd_Config.mtu < 1280) {
318 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
319 Global_Clatd_Config.mtu = 1280;
320 }
321
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900322 if(Global_Clatd_Config.ipv4mtu <= 0 ||
323 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
324 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500325 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
326 }
327
Daniel Drowna45056e2012-03-23 10:42:54 -0500328 error = tun_alloc(tunnel->device4, tunnel->fd4);
329 if(error < 0) {
330 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
331 exit(1);
332 }
333
334 configure_tun_ip(tunnel);
335}
336
Daniel Drowna45056e2012-03-23 10:42:54 -0500337/* function: read_packet
338 * reads a packet from the tunnel fd and passes it down the stack
339 * active_fd - tun file descriptor marked ready for reading
340 * tunnel - tun device data
341 */
342void read_packet(int active_fd, const struct tun_data *tunnel) {
343 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900344 uint8_t buf[PACKETLEN], *packet;
345 int fd;
Daniel Drowna45056e2012-03-23 10:42:54 -0500346
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900347 readlen = read(active_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500348
349 if(readlen < 0) {
350 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
351 return;
352 } else if(readlen == 0) {
353 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
354 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900355 return;
356 }
357
358 if (active_fd == tunnel->fd4) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500359 ssize_t header_size = sizeof(struct tun_pi);
360
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900361 if (readlen < header_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500362 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
363 return;
364 }
365
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900366 struct tun_pi *tun_header = (struct tun_pi *) buf;
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900367 uint16_t proto = ntohs(tun_header->proto);
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900368 if (proto != ETH_P_IP) {
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900369 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
370 return;
371 }
372
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900373 if(tun_header->flags != 0) {
374 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
375 }
376
377 fd = tunnel->write_fd6;
378 packet = buf + header_size;
379 readlen -= header_size;
380 } else {
381 fd = tunnel->fd4;
382 packet = buf;
Daniel Drowna45056e2012-03-23 10:42:54 -0500383 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900384
385 translate_packet(fd, (fd == tunnel->write_fd6), packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500386}
387
388/* function: event_loop
389 * reads packets from the tun network interface and passes them down the stack
390 * tunnel - tun device data
391 */
392void event_loop(const struct tun_data *tunnel) {
393 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700394 struct pollfd wait_fd[] = {
395 { tunnel->read_fd6, POLLIN, 0 },
396 { tunnel->fd4, POLLIN, 0 },
397 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500398
399 // start the poll timer
400 last_interface_poll = time(NULL);
401
Daniel Drowna45056e2012-03-23 10:42:54 -0500402 while(running) {
403 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
404 if(errno != EINTR) {
405 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
406 }
407 } else {
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700408 size_t i;
409 for(i = 0; i < ARRAY_SIZE(wait_fd); i++) {
410 // Call read_packet if the socket has data to be read, but also if an
411 // error is waiting. If we don't call read() after getting POLLERR, a
412 // subsequent poll() will return immediately with POLLERR again,
413 // causing this code to spin in a loop. Calling read() will clear the
414 // socket error flag instead.
415 if(wait_fd[i].revents != 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500416 read_packet(wait_fd[i].fd,tunnel);
417 }
418 }
419 }
420
421 time_t now = time(NULL);
422 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
442 * str - the string to parse
443 * out - the unsigned integer to write to, gets clobbered on failure
444 */
445int parse_unsigned(const char *str, unsigned *out) {
446 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;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900459 uint32_t mark = MARK_UNSET;
Lorenzo Colitti76129162014-10-20 17:24:51 +0900460 unsigned len;
Daniel Drowna45056e2012-03-23 10:42:54 -0500461
Lorenzo Colitti75647612014-06-09 13:55:50 +0900462 while((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 switch(opt) {
464 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:
480 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
485 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
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900506 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
507 CLATD_VERSION, uplink_interface,
508 net_id_str ? net_id_str : "(none)",
509 mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500510
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900511 // open our raw sockets before dropping privs
512 open_sockets(&tunnel, mark);
513
514 // run under a regular user
515 drop_root();
516
517 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500518 tunnel.fd4 = tun_open();
519 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900520 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500521 exit(1);
522 }
523
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900524 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
525 // "local", but that only works for the netd process itself.
526 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500527
Paul Jensen247dd712014-05-30 13:19:10 -0400528 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500529
Lorenzo Colitti1352a3a2014-10-21 13:41:21 +0900530 update_clat_ipv6_address(&tunnel, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500531
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900532 // Loop until someone sends us a signal or brings down the tun interface.
533 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
534 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
535 exit(1);
536 }
537 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500538
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900539 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500540
541 return 0;
542}