blob: e2c96e60099e4bb43a1bb7d70295526f87e47a66 [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
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
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
116/* function: interface_poll
117 * polls the uplink network interface for address changes
118 * tunnel - tun device data
119 */
120void interface_poll(const struct tun_data *tunnel) {
121 union anyip *interface_ip;
122
123 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
124 if(!interface_ip) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900125 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",
126 Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500127 return;
128 }
129
130 config_generate_local_ipv6_subnet(&interface_ip->ip6);
131
132 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
133 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
134 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
135 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
136 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
137
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900138 // Start translating packets to the new prefix.
Daniel Drowna45056e2012-03-23 10:42:54 -0500139 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900140
141 // Update our packet socket filter to reflect the new 464xlat IP address.
142 if (!configure_packet_socket(tunnel->read_fd6)) {
143 // Things aren't going to work. Bail out and hope we have better luck next time.
Lorenzo Colitti75647612014-06-09 13:55:50 +0900144 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900145 exit(1);
146 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500147 }
148
149 free(interface_ip);
150}
151
152/* function: configure_tun_ip
153 * configures the ipv4 and ipv6 addresses on the tunnel interface
154 * tunnel - tun device data
155 */
156void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500157 int status;
158
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900159 // Configure the interface before bringing it up. As soon as we bring the interface up, the
160 // framework will be notified and will assume the interface's configuration has been finalized.
161 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
162 32, &Global_Clatd_Config.ipv4_local_subnet);
163 if(status < 0) {
164 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
165 exit(1);
166 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500167
Daniel Drowna45056e2012-03-23 10:42:54 -0500168 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
169 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
170 exit(1);
171 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500172}
173
174/* function: drop_root
175 * drops root privs but keeps the needed capability
176 */
177void drop_root() {
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900178 gid_t groups[] = { AID_INET, AID_VPN };
Daniel Drowna45056e2012-03-23 10:42:54 -0500179 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
180 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
181 exit(1);
182 }
183
184 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
185
186 if(setgid(AID_CLAT) < 0) {
187 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
188 exit(1);
189 }
190 if(setuid(AID_CLAT) < 0) {
191 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
192 exit(1);
193 }
194
195 struct __user_cap_header_struct header;
196 struct __user_cap_data_struct cap;
197 memset(&header, 0, sizeof(header));
198 memset(&cap, 0, sizeof(cap));
199
200 header.version = _LINUX_CAPABILITY_VERSION;
201 header.pid = 0; // 0 = change myself
202 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
203
204 if(capset(&header, &cap) < 0) {
205 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
206 exit(1);
207 }
208}
209
Lorenzo Colitti75647612014-06-09 13:55:50 +0900210/* function: open_sockets
211 * opens a packet socket to receive IPv6 packets and a raw socket to send them
212 * tunnel - tun device data
213 * mark - the socket mark to use for the sending raw socket
Lorenzo Colittice140882014-06-02 21:20:40 +0900214 */
Lorenzo Colitti75647612014-06-09 13:55:50 +0900215void open_sockets(struct tun_data *tunnel, uint32_t mark) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900216 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
217 if (rawsock < 0) {
218 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
219 exit(1);
220 }
221
222 int off = 0;
223 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
224 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
225 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900226 if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
227 logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno));
228 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900229
230 tunnel->write_fd6 = rawsock;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900231
232 int packetsock = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
233 if (packetsock < 0) {
234 logmsg(ANDROID_LOG_FATAL, "packet socket failed: %s", strerror(errno));
235 exit(1);
236 }
237
238 tunnel->read_fd6 = packetsock;
Lorenzo Colittice140882014-06-02 21:20:40 +0900239}
240
Daniel Drowna45056e2012-03-23 10:42:54 -0500241/* function: configure_interface
242 * reads the configuration and applies it to the interface
243 * uplink_interface - network interface to use to reach the ipv6 internet
244 * plat_prefix - PLAT prefix to use
245 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400246 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500247 */
Paul Jensen247dd712014-05-30 13:19:10 -0400248void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500249 int error;
250
Paul Jensen247dd712014-05-30 13:19:10 -0400251 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500252 logmsg(ANDROID_LOG_FATAL,"read_config failed");
253 exit(1);
254 }
255
256 if(Global_Clatd_Config.mtu > MAXMTU) {
257 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
258 Global_Clatd_Config.mtu = MAXMTU;
259 }
260 if(Global_Clatd_Config.mtu <= 0) {
261 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
262 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
263 }
264 if(Global_Clatd_Config.mtu < 1280) {
265 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
266 Global_Clatd_Config.mtu = 1280;
267 }
268
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900269 if(Global_Clatd_Config.ipv4mtu <= 0 ||
270 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
271 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500272 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
273 }
274
Daniel Drowna45056e2012-03-23 10:42:54 -0500275 error = tun_alloc(tunnel->device4, tunnel->fd4);
276 if(error < 0) {
277 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
278 exit(1);
279 }
280
281 configure_tun_ip(tunnel);
282}
283
Daniel Drowna45056e2012-03-23 10:42:54 -0500284/* function: read_packet
285 * reads a packet from the tunnel fd and passes it down the stack
286 * active_fd - tun file descriptor marked ready for reading
287 * tunnel - tun device data
288 */
289void read_packet(int active_fd, const struct tun_data *tunnel) {
290 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900291 uint8_t buf[PACKETLEN], *packet;
292 int fd;
Daniel Drowna45056e2012-03-23 10:42:54 -0500293
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900294 readlen = read(active_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500295
296 if(readlen < 0) {
297 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
298 return;
299 } else if(readlen == 0) {
300 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
301 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900302 return;
303 }
304
305 if (active_fd == tunnel->fd4) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500306 ssize_t header_size = sizeof(struct tun_pi);
307
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900308 if (readlen < header_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500309 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
310 return;
311 }
312
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900313 struct tun_pi *tun_header = (struct tun_pi *) buf;
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900314 uint16_t proto = ntohs(tun_header->proto);
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900315 if (proto != ETH_P_IP) {
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900316 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
317 return;
318 }
319
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900320 if(tun_header->flags != 0) {
321 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
322 }
323
324 fd = tunnel->write_fd6;
325 packet = buf + header_size;
326 readlen -= header_size;
327 } else {
328 fd = tunnel->fd4;
329 packet = buf;
Daniel Drowna45056e2012-03-23 10:42:54 -0500330 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900331
332 translate_packet(fd, (fd == tunnel->write_fd6), packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500333}
334
335/* function: event_loop
336 * reads packets from the tun network interface and passes them down the stack
337 * tunnel - tun device data
338 */
339void event_loop(const struct tun_data *tunnel) {
340 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700341 struct pollfd wait_fd[] = {
342 { tunnel->read_fd6, POLLIN, 0 },
343 { tunnel->fd4, POLLIN, 0 },
344 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500345
346 // start the poll timer
347 last_interface_poll = time(NULL);
348
Daniel Drowna45056e2012-03-23 10:42:54 -0500349 while(running) {
350 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
351 if(errno != EINTR) {
352 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
353 }
354 } else {
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700355 size_t i;
356 for(i = 0; i < ARRAY_SIZE(wait_fd); i++) {
357 // Call read_packet if the socket has data to be read, but also if an
358 // error is waiting. If we don't call read() after getting POLLERR, a
359 // subsequent poll() will return immediately with POLLERR again,
360 // causing this code to spin in a loop. Calling read() will clear the
361 // socket error flag instead.
362 if(wait_fd[i].revents != 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500363 read_packet(wait_fd[i].fd,tunnel);
364 }
365 }
366 }
367
368 time_t now = time(NULL);
369 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
370 interface_poll(tunnel);
371 last_interface_poll = now;
372 }
373 }
374}
375
376/* function: print_help
377 * in case the user is running this on the command line
378 */
379void print_help() {
380 printf("android-clat arguments:\n");
381 printf("-i [uplink interface]\n");
382 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400383 printf("-n [NetId]\n");
Lorenzo Colitti75647612014-06-09 13:55:50 +0900384 printf("-m [socket mark]\n");
385}
386
387/* function: parse_unsigned
388 * parses a string as a decimal/hex/octal unsigned integer
389 * str - the string to parse
390 * out - the unsigned integer to write to, gets clobbered on failure
391 */
392int parse_unsigned(const char *str, unsigned *out) {
393 char *end_ptr;
394 *out = strtoul(str, &end_ptr, 0);
395 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500396}
397
398/* function: main
399 * allocate and setup the tun device, then run the event loop
400 */
401int main(int argc, char **argv) {
402 struct tun_data tunnel;
403 int opt;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900404 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Paul Jensen247dd712014-05-30 13:19:10 -0400405 unsigned net_id = NETID_UNSET;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900406 uint32_t mark = MARK_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500407
Daniel Drowna45056e2012-03-23 10:42:54 -0500408 strcpy(tunnel.device4, DEVICENAME4);
409
Lorenzo Colitti75647612014-06-09 13:55:50 +0900410 while((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500411 switch(opt) {
412 case 'i':
413 uplink_interface = optarg;
414 break;
415 case 'p':
416 plat_prefix = optarg;
417 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400418 case 'n':
419 net_id_str = optarg;
420 break;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900421 case 'm':
422 mark_str = optarg;
423 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500424 case 'h':
Daniel Drowna45056e2012-03-23 10:42:54 -0500425 print_help();
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900426 exit(0);
427 default:
428 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char) optopt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500429 exit(1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500430 }
431 }
432
433 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900434 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500435 exit(1);
436 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900437
438 if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) {
439 logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str);
440 exit(1);
Paul Jensen247dd712014-05-30 13:19:10 -0400441 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900442
443 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
444 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
445 exit(1);
446 }
447
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900448 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
449 CLATD_VERSION, uplink_interface,
450 net_id_str ? net_id_str : "(none)",
451 mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500452
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900453 // open our raw sockets before dropping privs
454 open_sockets(&tunnel, mark);
455
456 // run under a regular user
457 drop_root();
458
459 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500460 tunnel.fd4 = tun_open();
461 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900462 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 exit(1);
464 }
465
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900466 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
467 // "local", but that only works for the netd process itself.
468 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500469
Paul Jensen247dd712014-05-30 13:19:10 -0400470 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500471
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900472 if (!configure_packet_socket(tunnel.read_fd6)) {
473 // We've already logged an error.
474 exit(1);
475 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500476
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900477 // Loop until someone sends us a signal or brings down the tun interface.
478 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
479 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
480 exit(1);
481 }
482 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500483
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900484 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500485
486 return 0;
487}