blob: 001c4cb9571ee1d433172eb2abbfa5fd32ddfad0 [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>
24#include <sys/stat.h>
25#include <string.h>
26#include <errno.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <fcntl.h>
31
Nick Kralevich2edb7562013-02-28 14:15:22 -080032#include <sys/capability.h>
Lorenzo Colittid9084182013-03-22 00:42:21 +090033#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050034#include <linux/prctl.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
Daniel Drowna45056e2012-03-23 10:42:54 -050054#define DEVICENAME4 "clat4"
55
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
153/* function: interface_poll
154 * polls the uplink network interface for address changes
155 * tunnel - tun device data
156 */
157void interface_poll(const struct tun_data *tunnel) {
158 union anyip *interface_ip;
159
160 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
161 if(!interface_ip) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900162 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",
163 Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500164 return;
165 }
166
167 config_generate_local_ipv6_subnet(&interface_ip->ip6);
168
169 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
170 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
171 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
172 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
173 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
174
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900175 // Start translating packets to the new prefix.
Daniel Drowna45056e2012-03-23 10:42:54 -0500176 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900177
178 // Update our packet socket filter to reflect the new 464xlat IP address.
179 if (!configure_packet_socket(tunnel->read_fd6)) {
180 // Things aren't going to work. Bail out and hope we have better luck next time.
181 // We don't log an error here because attach_filter has already done so.
182 exit(1);
183 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500184 }
185
186 free(interface_ip);
187}
188
189/* function: configure_tun_ip
190 * configures the ipv4 and ipv6 addresses on the tunnel interface
191 * tunnel - tun device data
192 */
193void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500194 int status;
195
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900196 // Configure the interface before bringing it up. As soon as we bring the interface up, the
197 // framework will be notified and will assume the interface's configuration has been finalized.
198 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
199 32, &Global_Clatd_Config.ipv4_local_subnet);
200 if(status < 0) {
201 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
202 exit(1);
203 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500204
Daniel Drowna45056e2012-03-23 10:42:54 -0500205 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
206 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
207 exit(1);
208 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500209}
210
211/* function: drop_root
212 * drops root privs but keeps the needed capability
213 */
214void drop_root() {
215 gid_t groups[] = { AID_INET };
216 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
217 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
218 exit(1);
219 }
220
221 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
222
223 if(setgid(AID_CLAT) < 0) {
224 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
225 exit(1);
226 }
227 if(setuid(AID_CLAT) < 0) {
228 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
229 exit(1);
230 }
231
232 struct __user_cap_header_struct header;
233 struct __user_cap_data_struct cap;
234 memset(&header, 0, sizeof(header));
235 memset(&cap, 0, sizeof(cap));
236
237 header.version = _LINUX_CAPABILITY_VERSION;
238 header.pid = 0; // 0 = change myself
239 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
240
241 if(capset(&header, &cap) < 0) {
242 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
243 exit(1);
244 }
245}
246
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900247/* function: open_socket
248 * opens raw and packet sockets
Lorenzo Colittice140882014-06-02 21:20:40 +0900249 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900250void open_sockets(struct tun_data *tunnel) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900251 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
252 if (rawsock < 0) {
253 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
254 exit(1);
255 }
256
257 int off = 0;
258 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
259 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
260 }
261
262 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900263
264 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
265 if (packetsock < 0) {
266 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
267 exit(1);
268 }
269
270 tunnel->read_fd6 = packetsock;
Lorenzo Colittice140882014-06-02 21:20:40 +0900271}
272
Daniel Drowna45056e2012-03-23 10:42:54 -0500273/* function: configure_interface
274 * reads the configuration and applies it to the interface
275 * uplink_interface - network interface to use to reach the ipv6 internet
276 * plat_prefix - PLAT prefix to use
277 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400278 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500279 */
Paul Jensen247dd712014-05-30 13:19:10 -0400280void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500281 int error;
282
Paul Jensen247dd712014-05-30 13:19:10 -0400283 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500284 logmsg(ANDROID_LOG_FATAL,"read_config failed");
285 exit(1);
286 }
287
288 if(Global_Clatd_Config.mtu > MAXMTU) {
289 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
290 Global_Clatd_Config.mtu = MAXMTU;
291 }
292 if(Global_Clatd_Config.mtu <= 0) {
293 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
294 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
295 }
296 if(Global_Clatd_Config.mtu < 1280) {
297 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
298 Global_Clatd_Config.mtu = 1280;
299 }
300
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900301 if(Global_Clatd_Config.ipv4mtu <= 0 ||
302 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
303 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
305 }
306
Daniel Drowna45056e2012-03-23 10:42:54 -0500307 error = tun_alloc(tunnel->device4, tunnel->fd4);
308 if(error < 0) {
309 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
310 exit(1);
311 }
312
313 configure_tun_ip(tunnel);
314}
315
Daniel Drowna45056e2012-03-23 10:42:54 -0500316/* function: read_packet
317 * reads a packet from the tunnel fd and passes it down the stack
318 * active_fd - tun file descriptor marked ready for reading
319 * tunnel - tun device data
320 */
321void read_packet(int active_fd, const struct tun_data *tunnel) {
322 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900323 uint8_t buf[PACKETLEN], *packet;
324 int fd;
Daniel Drowna45056e2012-03-23 10:42:54 -0500325
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900326 readlen = read(active_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500327
328 if(readlen < 0) {
329 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
330 return;
331 } else if(readlen == 0) {
332 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
333 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900334 return;
335 }
336
337 if (active_fd == tunnel->fd4) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500338 ssize_t header_size = sizeof(struct tun_pi);
339
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900340 if (readlen < header_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500341 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
342 return;
343 }
344
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900345 struct tun_pi *tun_header = (struct tun_pi *) buf;
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900346 uint16_t proto = ntohs(tun_header->proto);
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900347 if (proto != ETH_P_IP) {
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900348 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
349 return;
350 }
351
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900352 if(tun_header->flags != 0) {
353 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
354 }
355
356 fd = tunnel->write_fd6;
357 packet = buf + header_size;
358 readlen -= header_size;
359 } else {
360 fd = tunnel->fd4;
361 packet = buf;
Daniel Drowna45056e2012-03-23 10:42:54 -0500362 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900363
364 translate_packet(fd, (fd == tunnel->write_fd6), packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500365}
366
367/* function: event_loop
368 * reads packets from the tun network interface and passes them down the stack
369 * tunnel - tun device data
370 */
371void event_loop(const struct tun_data *tunnel) {
372 time_t last_interface_poll;
373 struct pollfd wait_fd[2];
374
375 // start the poll timer
376 last_interface_poll = time(NULL);
377
Lorenzo Colittice140882014-06-02 21:20:40 +0900378 wait_fd[0].fd = tunnel->read_fd6;
Daniel Drowna45056e2012-03-23 10:42:54 -0500379 wait_fd[0].events = POLLIN;
380 wait_fd[0].revents = 0;
381 wait_fd[1].fd = tunnel->fd4;
382 wait_fd[1].events = POLLIN;
383 wait_fd[1].revents = 0;
384
385 while(running) {
386 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
387 if(errno != EINTR) {
388 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
389 }
390 } else {
391 int i;
392 for(i = 0; i < 2; i++) {
393 if((wait_fd[i].revents & POLLIN) != 0) {
394 read_packet(wait_fd[i].fd,tunnel);
395 }
396 }
397 }
398
399 time_t now = time(NULL);
400 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
401 interface_poll(tunnel);
402 last_interface_poll = now;
403 }
404 }
405}
406
407/* function: print_help
408 * in case the user is running this on the command line
409 */
410void print_help() {
411 printf("android-clat arguments:\n");
412 printf("-i [uplink interface]\n");
413 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400414 printf("-n [NetId]\n");
Daniel Drowna45056e2012-03-23 10:42:54 -0500415}
416
417/* function: main
418 * allocate and setup the tun device, then run the event loop
419 */
420int main(int argc, char **argv) {
421 struct tun_data tunnel;
422 int opt;
Paul Jensen247dd712014-05-30 13:19:10 -0400423 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL;
424 unsigned net_id = NETID_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500425
Daniel Drowna45056e2012-03-23 10:42:54 -0500426 strcpy(tunnel.device4, DEVICENAME4);
427
Paul Jensen247dd712014-05-30 13:19:10 -0400428 while((opt = getopt(argc, argv, "i:p:n:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500429 switch(opt) {
430 case 'i':
431 uplink_interface = optarg;
432 break;
433 case 'p':
434 plat_prefix = optarg;
435 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400436 case 'n':
437 net_id_str = optarg;
438 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500439 case 'h':
440 default:
441 print_help();
442 exit(1);
443 break;
444 }
445 }
446
447 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900448 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500449 printf("I need an interface\n");
450 exit(1);
451 }
Paul Jensen247dd712014-05-30 13:19:10 -0400452 if (net_id_str != NULL) {
453 char *end_ptr;
454 net_id = strtoul(net_id_str, &end_ptr, 0);
Paul Jensen464fb262014-06-03 14:41:57 -0400455 if (*net_id_str == 0 || *end_ptr != 0) {
Paul Jensen247dd712014-05-30 13:19:10 -0400456 logmsg(ANDROID_LOG_FATAL, "clatd called with invalid NetID %s", net_id_str);
457 exit(1);
458 }
459 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900460 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500461
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900462 // open the tunnel device and our raw sockets before dropping privs
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 tunnel.fd4 = tun_open();
464 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900465 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500466 exit(1);
467 }
468
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900469 open_sockets(&tunnel);
Lorenzo Colittice140882014-06-02 21:20:40 +0900470
Daniel Drowna45056e2012-03-23 10:42:54 -0500471 // run under a regular user
472 drop_root();
473
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900474 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
475 // "local", but that only works for the netd process itself.
476 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500477
Paul Jensen247dd712014-05-30 13:19:10 -0400478 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500479
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900480 if (!configure_packet_socket(tunnel.read_fd6)) {
481 // We've already logged an error.
482 exit(1);
483 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500484
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900485 // Loop until someone sends us a signal or brings down the tun interface.
486 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
487 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
488 exit(1);
489 }
490 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500491
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900492 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500493
494 return 0;
495}