blob: 5cbcc1e6419ccc0883c62b8e72067d8ffe899d83 [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"
Lorenzo Colitti9353be22014-12-03 15:18:29 +090054#include "ring.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050055
Daniel Drowna45056e2012-03-23 10:42:54 -050056#define DEVICENAME4 "clat4"
57
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090058/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
59#define MTU_DELTA 28
60
Daniel Drowna45056e2012-03-23 10:42:54 -050061volatile sig_atomic_t running = 1;
62
Lorenzo Colittibaf62992013-03-01 20:29:39 +090063/* function: stop_loop
64 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050065 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090066void stop_loop() {
Daniel Drowna45056e2012-03-23 10:42:54 -050067 running = 0;
68}
69
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090070/* function: configure_packet_socket
71 * Binds the packet socket and attaches the receive filter to it.
72 * sock - the socket to configure
Daniel Drowna45056e2012-03-23 10:42:54 -050073 */
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090074int configure_packet_socket(int sock) {
75 struct sockaddr_ll sll = {
76 .sll_family = AF_PACKET,
77 .sll_protocol = htons(ETH_P_IPV6),
78 .sll_ifindex = if_nametoindex((char *) &Global_Clatd_Config.default_pdp_interface),
79 .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
80 };
81 if (bind(sock, (struct sockaddr *) &sll, sizeof(sll))) {
82 logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno));
83 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050084 }
Daniel Drowna45056e2012-03-23 10:42:54 -050085
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090086 uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32;
87 struct sock_filter filter_code[] = {
88 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
89 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
90 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
91 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
92 // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet).
93 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
94 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
95 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
96 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
97 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
98 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
99 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
100 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
101 BPF_STMT(BPF_RET | BPF_K, PACKETLEN),
102 BPF_STMT(BPF_RET | BPF_K, 0)
103 };
104 struct sock_fprog filter = {
105 sizeof(filter_code) / sizeof(filter_code[0]),
106 filter_code
107 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500108
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900109 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
110 logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno));
111 return 0;
Daniel Drowna45056e2012-03-23 10:42:54 -0500112 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900113
114 return 1;
Daniel Drowna45056e2012-03-23 10:42:54 -0500115}
116
117/* function: interface_poll
118 * polls the uplink network interface for address changes
119 * tunnel - tun device data
120 */
121void interface_poll(const struct tun_data *tunnel) {
122 union anyip *interface_ip;
123
124 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
125 if(!interface_ip) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900126 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",
127 Global_Clatd_Config.default_pdp_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500128 return;
129 }
130
131 config_generate_local_ipv6_subnet(&interface_ip->ip6);
132
133 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
134 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
135 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
136 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
137 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
138
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900139 // Start translating packets to the new prefix.
Daniel Drowna45056e2012-03-23 10:42:54 -0500140 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900141
142 // Update our packet socket filter to reflect the new 464xlat IP address.
143 if (!configure_packet_socket(tunnel->read_fd6)) {
144 // Things aren't going to work. Bail out and hope we have better luck next time.
Lorenzo Colitti75647612014-06-09 13:55:50 +0900145 // We don't log an error here because configure_packet_socket has already done so.
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900146 exit(1);
147 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500148 }
149
150 free(interface_ip);
151}
152
153/* 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
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900233 tunnel->read_fd6 = ring_create(tunnel);
234 if (tunnel->read_fd6 < 0) {
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900235 exit(1);
236 }
Lorenzo Colittice140882014-06-02 21:20:40 +0900237}
238
Daniel Drowna45056e2012-03-23 10:42:54 -0500239/* function: configure_interface
240 * reads the configuration and applies it to the interface
241 * uplink_interface - network interface to use to reach the ipv6 internet
242 * plat_prefix - PLAT prefix to use
243 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400244 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500245 */
Paul Jensen247dd712014-05-30 13:19:10 -0400246void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500247 int error;
248
Paul Jensen247dd712014-05-30 13:19:10 -0400249 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500250 logmsg(ANDROID_LOG_FATAL,"read_config failed");
251 exit(1);
252 }
253
254 if(Global_Clatd_Config.mtu > MAXMTU) {
255 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
256 Global_Clatd_Config.mtu = MAXMTU;
257 }
258 if(Global_Clatd_Config.mtu <= 0) {
259 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
260 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
261 }
262 if(Global_Clatd_Config.mtu < 1280) {
263 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
264 Global_Clatd_Config.mtu = 1280;
265 }
266
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900267 if(Global_Clatd_Config.ipv4mtu <= 0 ||
268 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
269 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500270 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
271 }
272
Daniel Drowna45056e2012-03-23 10:42:54 -0500273 error = tun_alloc(tunnel->device4, tunnel->fd4);
274 if(error < 0) {
275 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
276 exit(1);
277 }
278
279 configure_tun_ip(tunnel);
280}
281
Daniel Drowna45056e2012-03-23 10:42:54 -0500282/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900283 * reads a packet from the tunnel fd and translates it
284 * read_fd - file descriptor to read original packet from
285 * write_fd - file descriptor to write translated packet to
286 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -0500287 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900288void read_packet(int read_fd, int write_fd, int to_ipv6) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500289 ssize_t readlen;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900290 uint8_t buf[PACKETLEN], *packet;
Daniel Drowna45056e2012-03-23 10:42:54 -0500291
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900292 readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -0500293
294 if(readlen < 0) {
295 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
296 return;
297 } else if(readlen == 0) {
298 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
299 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900300 return;
301 }
302
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900303 struct tun_pi *tun_header = (struct tun_pi *) buf;
304 if (readlen < (ssize_t) sizeof(*tun_header)) {
305 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
306 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500307 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900308
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900309 uint16_t proto = ntohs(tun_header->proto);
310 if (proto != ETH_P_IP) {
311 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
312 return;
313 }
314
315 if(tun_header->flags != 0) {
316 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
317 }
318
319 packet = (uint8_t *) (tun_header + 1);
320 readlen -= sizeof(*tun_header);
321 translate_packet(write_fd, to_ipv6, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500322}
323
324/* function: event_loop
325 * reads packets from the tun network interface and passes them down the stack
326 * tunnel - tun device data
327 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900328void event_loop(struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500329 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700330 struct pollfd wait_fd[] = {
331 { tunnel->read_fd6, POLLIN, 0 },
332 { tunnel->fd4, POLLIN, 0 },
333 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500334
335 // start the poll timer
336 last_interface_poll = time(NULL);
337
Daniel Drowna45056e2012-03-23 10:42:54 -0500338 while(running) {
339 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
340 if(errno != EINTR) {
341 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
342 }
343 } else {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900344 // Call read_packet if the socket has data to be read, but also if an
345 // error is waiting. If we don't call read() after getting POLLERR, a
346 // subsequent poll() will return immediately with POLLERR again,
347 // causing this code to spin in a loop. Calling read() will clear the
348 // socket error flag instead.
349 if (wait_fd[0].revents) {
350 ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */);
351 }
352 if (wait_fd[1].revents) {
353 read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500354 }
355 }
356
357 time_t now = time(NULL);
358 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
359 interface_poll(tunnel);
360 last_interface_poll = now;
361 }
362 }
363}
364
365/* function: print_help
366 * in case the user is running this on the command line
367 */
368void print_help() {
369 printf("android-clat arguments:\n");
370 printf("-i [uplink interface]\n");
371 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400372 printf("-n [NetId]\n");
Lorenzo Colitti75647612014-06-09 13:55:50 +0900373 printf("-m [socket mark]\n");
374}
375
376/* function: parse_unsigned
377 * parses a string as a decimal/hex/octal unsigned integer
378 * str - the string to parse
379 * out - the unsigned integer to write to, gets clobbered on failure
380 */
381int parse_unsigned(const char *str, unsigned *out) {
382 char *end_ptr;
383 *out = strtoul(str, &end_ptr, 0);
384 return *str && !*end_ptr;
Daniel Drowna45056e2012-03-23 10:42:54 -0500385}
386
387/* function: main
388 * allocate and setup the tun device, then run the event loop
389 */
390int main(int argc, char **argv) {
391 struct tun_data tunnel;
392 int opt;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900393 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
Paul Jensen247dd712014-05-30 13:19:10 -0400394 unsigned net_id = NETID_UNSET;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900395 uint32_t mark = MARK_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500396
Daniel Drowna45056e2012-03-23 10:42:54 -0500397 strcpy(tunnel.device4, DEVICENAME4);
398
Lorenzo Colitti75647612014-06-09 13:55:50 +0900399 while((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500400 switch(opt) {
401 case 'i':
402 uplink_interface = optarg;
403 break;
404 case 'p':
405 plat_prefix = optarg;
406 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400407 case 'n':
408 net_id_str = optarg;
409 break;
Lorenzo Colitti75647612014-06-09 13:55:50 +0900410 case 'm':
411 mark_str = optarg;
412 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500413 case 'h':
Daniel Drowna45056e2012-03-23 10:42:54 -0500414 print_help();
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900415 exit(0);
416 default:
417 logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char) optopt);
Daniel Drowna45056e2012-03-23 10:42:54 -0500418 exit(1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500419 }
420 }
421
422 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900423 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500424 exit(1);
425 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900426
427 if (net_id_str != NULL && !parse_unsigned(net_id_str, &net_id)) {
428 logmsg(ANDROID_LOG_FATAL, "invalid NetID %s", net_id_str);
429 exit(1);
Paul Jensen247dd712014-05-30 13:19:10 -0400430 }
Lorenzo Colitti75647612014-06-09 13:55:50 +0900431
432 if (mark_str != NULL && !parse_unsigned(mark_str, &mark)) {
433 logmsg(ANDROID_LOG_FATAL, "invalid mark %s", mark_str);
434 exit(1);
435 }
436
Lorenzo Colittib9b471e2014-06-09 19:39:01 +0900437 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
438 CLATD_VERSION, uplink_interface,
439 net_id_str ? net_id_str : "(none)",
440 mark_str ? mark_str : "(none)");
Daniel Drowna45056e2012-03-23 10:42:54 -0500441
Lorenzo Colitti7045e272014-06-13 21:36:01 +0900442 // open our raw sockets before dropping privs
443 open_sockets(&tunnel, mark);
444
445 // run under a regular user
446 drop_root();
447
448 // we can create tun devices as non-root because we're in the VPN group.
Daniel Drowna45056e2012-03-23 10:42:54 -0500449 tunnel.fd4 = tun_open();
450 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900451 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500452 exit(1);
453 }
454
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900455 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
456 // "local", but that only works for the netd process itself.
457 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500458
Paul Jensen247dd712014-05-30 13:19:10 -0400459 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500460
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900461 if (!configure_packet_socket(tunnel.read_fd6)) {
462 // We've already logged an error.
463 exit(1);
464 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500465
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900466 // Loop until someone sends us a signal or brings down the tun interface.
467 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
468 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
469 exit(1);
470 }
471 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500472
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900473 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500474
475 return 0;
476}