blob: 7b8e43fec88fabe7149b3ae1f242fe0b6f463637 [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>
35#include <linux/if.h>
36#include <linux/if_tun.h>
37#include <linux/if_ether.h>
38
39#include <private/android_filesystem_config.h>
40
Lorenzo Colittid9084182013-03-22 00:42:21 +090041#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050042#include "clatd.h"
43#include "config.h"
44#include "logging.h"
Paul Jensen247dd712014-05-30 13:19:10 -040045#include "resolv_netid.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050046#include "setif.h"
47#include "setroute.h"
48#include "mtu.h"
49#include "getaddr.h"
50#include "dump.h"
51
52#define DEVICENAME6 "clat"
53#define DEVICENAME4 "clat4"
54
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090055/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
56#define MTU_DELTA 28
57
Daniel Drowna45056e2012-03-23 10:42:54 -050058int forwarding_fd = -1;
59volatile sig_atomic_t running = 1;
60
Daniel Drowna45056e2012-03-23 10:42:54 -050061/* function: set_forwarding
62 * enables/disables ipv6 forwarding
63 */
64void set_forwarding(int fd, const char *setting) {
65 /* we have to forward packets from the WAN to the tun interface */
66 if(write(fd, setting, strlen(setting)) < 0) {
67 logmsg(ANDROID_LOG_FATAL,"set_forwarding(%s) failed: %s", setting, strerror(errno));
68 exit(1);
69 }
70}
71
Lorenzo Colittibaf62992013-03-01 20:29:39 +090072/* function: stop_loop
73 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050074 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090075void stop_loop() {
Daniel Drowna45056e2012-03-23 10:42:54 -050076 running = 0;
77}
78
79/* function: tun_open
80 * tries to open the tunnel device
81 */
82int tun_open() {
83 int fd;
84
85 fd = open("/dev/tun", O_RDWR);
86 if(fd < 0) {
87 fd = open("/dev/net/tun", O_RDWR);
88 }
89
90 return fd;
91}
92
93/* function: tun_alloc
94 * creates a tun interface and names it
95 * dev - the name for the new tun device
96 */
97int tun_alloc(char *dev, int fd) {
98 struct ifreq ifr;
99 int err;
100
101 memset(&ifr, 0, sizeof(ifr));
102
103 ifr.ifr_flags = IFF_TUN;
104 if( *dev ) {
105 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
106 ifr.ifr_name[IFNAMSIZ-1] = '\0';
107 }
108
109 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
110 close(fd);
111 return err;
112 }
113 strcpy(dev, ifr.ifr_name);
114 return 0;
115}
116
117/* function: deconfigure_tun_ipv6
118 * removes the ipv6 route
119 * tunnel - tun device data
120 */
121void deconfigure_tun_ipv6(const struct tun_data *tunnel) {
122 int status;
123
124 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
125 128, NULL, 1, 0, ROUTE_DELETE);
126 if(status < 0) {
127 logmsg(ANDROID_LOG_WARN,"deconfigure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
128 }
129}
130
131/* function: configure_tun_ipv6
132 * configures the ipv6 route
133 * note: routes a /128 out of the (assumed routed to us) /64 to the CLAT interface
134 * tunnel - tun device data
135 */
136void configure_tun_ipv6(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500137 int status;
138
139 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
140 128, NULL, 1, 0, ROUTE_CREATE);
141 if(status < 0) {
142 logmsg(ANDROID_LOG_FATAL,"configure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
143 exit(1);
144 }
145}
146
147/* function: interface_poll
148 * polls the uplink network interface for address changes
149 * tunnel - tun device data
150 */
151void interface_poll(const struct tun_data *tunnel) {
152 union anyip *interface_ip;
153
154 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
155 if(!interface_ip) {
156 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",Global_Clatd_Config.default_pdp_interface);
157 return;
158 }
159
160 config_generate_local_ipv6_subnet(&interface_ip->ip6);
161
162 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
163 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
164 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
165 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
166 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
167
168 // remove old route
169 deconfigure_tun_ipv6(tunnel);
170
171 // add new route, start translating packets to the new prefix
172 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
173 configure_tun_ipv6(tunnel);
174 }
175
176 free(interface_ip);
177}
178
179/* function: configure_tun_ip
180 * configures the ipv4 and ipv6 addresses on the tunnel interface
181 * tunnel - tun device data
182 */
183void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500184 int status;
185
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900186 // Configure the interface before bringing it up. As soon as we bring the interface up, the
187 // framework will be notified and will assume the interface's configuration has been finalized.
188 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
189 32, &Global_Clatd_Config.ipv4_local_subnet);
190 if(status < 0) {
191 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
192 exit(1);
193 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500194
JP Abgrall4e0dd832013-12-20 14:51:26 -0800195 status = add_address(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_address,
196 64, NULL);
197 if(status < 0) {
198 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(6) failed: %s",strerror(-status));
199 exit(1);
200 }
201
Daniel Drowna45056e2012-03-23 10:42:54 -0500202 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
203 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
204 exit(1);
205 }
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900206
Daniel Drowna45056e2012-03-23 10:42:54 -0500207 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
208 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
209 exit(1);
210 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500211
212 configure_tun_ipv6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500213}
214
215/* function: drop_root
216 * drops root privs but keeps the needed capability
217 */
218void drop_root() {
219 gid_t groups[] = { AID_INET };
220 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
221 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
222 exit(1);
223 }
224
225 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
226
227 if(setgid(AID_CLAT) < 0) {
228 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
229 exit(1);
230 }
231 if(setuid(AID_CLAT) < 0) {
232 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
233 exit(1);
234 }
235
236 struct __user_cap_header_struct header;
237 struct __user_cap_data_struct cap;
238 memset(&header, 0, sizeof(header));
239 memset(&cap, 0, sizeof(cap));
240
241 header.version = _LINUX_CAPABILITY_VERSION;
242 header.pid = 0; // 0 = change myself
243 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
244
245 if(capset(&header, &cap) < 0) {
246 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
247 exit(1);
248 }
249}
250
251/* function: configure_interface
252 * reads the configuration and applies it to the interface
253 * uplink_interface - network interface to use to reach the ipv6 internet
254 * plat_prefix - PLAT prefix to use
255 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400256 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500257 */
Paul Jensen247dd712014-05-30 13:19:10 -0400258void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500259 int error;
260
Paul Jensen247dd712014-05-30 13:19:10 -0400261 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500262 logmsg(ANDROID_LOG_FATAL,"read_config failed");
263 exit(1);
264 }
265
266 if(Global_Clatd_Config.mtu > MAXMTU) {
267 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
268 Global_Clatd_Config.mtu = MAXMTU;
269 }
270 if(Global_Clatd_Config.mtu <= 0) {
271 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
272 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
273 }
274 if(Global_Clatd_Config.mtu < 1280) {
275 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
276 Global_Clatd_Config.mtu = 1280;
277 }
278
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900279 if(Global_Clatd_Config.ipv4mtu <= 0 ||
280 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
281 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500282 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
283 }
284
285 error = tun_alloc(tunnel->device6, tunnel->fd6);
286 if(error < 0) {
287 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
288 exit(1);
289 }
290
291 error = tun_alloc(tunnel->device4, tunnel->fd4);
292 if(error < 0) {
293 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
294 exit(1);
295 }
296
297 configure_tun_ip(tunnel);
298}
299
Daniel Drowna45056e2012-03-23 10:42:54 -0500300/* function: read_packet
301 * reads a packet from the tunnel fd and passes it down the stack
302 * active_fd - tun file descriptor marked ready for reading
303 * tunnel - tun device data
304 */
305void read_packet(int active_fd, const struct tun_data *tunnel) {
306 ssize_t readlen;
Brian Carlstromfcac4102014-02-24 20:03:01 -0800307 uint8_t packet[PACKETLEN];
Daniel Drowna45056e2012-03-23 10:42:54 -0500308
309 // in case something ignores the packet length
310 memset(packet, 0, PACKETLEN);
311
312 readlen = read(active_fd,packet,PACKETLEN);
313
314 if(readlen < 0) {
315 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
316 return;
317 } else if(readlen == 0) {
318 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
319 running = 0;
320 } else {
Daniel Drowna45056e2012-03-23 10:42:54 -0500321 ssize_t header_size = sizeof(struct tun_pi);
322
323 if(readlen < header_size) {
324 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
325 return;
326 }
327
Lorenzo Colittif9390602014-02-13 12:53:35 +0900328 translate_packet(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500329 }
330}
331
332/* function: event_loop
333 * reads packets from the tun network interface and passes them down the stack
334 * tunnel - tun device data
335 */
336void event_loop(const struct tun_data *tunnel) {
337 time_t last_interface_poll;
338 struct pollfd wait_fd[2];
339
340 // start the poll timer
341 last_interface_poll = time(NULL);
342
343 wait_fd[0].fd = tunnel->fd6;
344 wait_fd[0].events = POLLIN;
345 wait_fd[0].revents = 0;
346 wait_fd[1].fd = tunnel->fd4;
347 wait_fd[1].events = POLLIN;
348 wait_fd[1].revents = 0;
349
350 while(running) {
351 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
352 if(errno != EINTR) {
353 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
354 }
355 } else {
356 int i;
357 for(i = 0; i < 2; i++) {
358 if((wait_fd[i].revents & POLLIN) != 0) {
359 read_packet(wait_fd[i].fd,tunnel);
360 }
361 }
362 }
363
364 time_t now = time(NULL);
365 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
366 interface_poll(tunnel);
367 last_interface_poll = now;
368 }
369 }
370}
371
372/* function: print_help
373 * in case the user is running this on the command line
374 */
375void print_help() {
376 printf("android-clat arguments:\n");
377 printf("-i [uplink interface]\n");
378 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400379 printf("-n [NetId]\n");
Daniel Drowna45056e2012-03-23 10:42:54 -0500380}
381
382/* function: main
383 * allocate and setup the tun device, then run the event loop
384 */
385int main(int argc, char **argv) {
386 struct tun_data tunnel;
387 int opt;
Paul Jensen247dd712014-05-30 13:19:10 -0400388 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL;
389 unsigned net_id = NETID_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500390
391 strcpy(tunnel.device6, DEVICENAME6);
392 strcpy(tunnel.device4, DEVICENAME4);
393
Paul Jensen247dd712014-05-30 13:19:10 -0400394 while((opt = getopt(argc, argv, "i:p:n:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500395 switch(opt) {
396 case 'i':
397 uplink_interface = optarg;
398 break;
399 case 'p':
400 plat_prefix = optarg;
401 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400402 case 'n':
403 net_id_str = optarg;
404 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500405 case 'h':
406 default:
407 print_help();
408 exit(1);
409 break;
410 }
411 }
412
413 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900414 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500415 printf("I need an interface\n");
416 exit(1);
417 }
Paul Jensen247dd712014-05-30 13:19:10 -0400418 if (net_id_str != NULL) {
419 char *end_ptr;
420 net_id = strtoul(net_id_str, &end_ptr, 0);
421 if (net_id == ULONG_MAX || *net_id_str == 0 || *end_ptr != 0) {
422 logmsg(ANDROID_LOG_FATAL, "clatd called with invalid NetID %s", net_id_str);
423 exit(1);
424 }
425 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900426 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500427
428 // open the tunnel device before dropping privs
429 tunnel.fd6 = tun_open();
430 if(tunnel.fd6 < 0) {
JP Abgrall4e0dd832013-12-20 14:51:26 -0800431 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500432 exit(1);
433 }
434
435 tunnel.fd4 = tun_open();
436 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900437 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500438 exit(1);
439 }
440
441 // open the forwarding configuration before dropping privs
442 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
443 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900444 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
445 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500446 exit(1);
447 }
448
Daniel Drowna45056e2012-03-23 10:42:54 -0500449 // run under a regular user
450 drop_root();
451
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900452 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
453 // "local", but that only works for the netd process itself.
454 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500455
Paul Jensen247dd712014-05-30 13:19:10 -0400456 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500457
Daniel Drowna45056e2012-03-23 10:42:54 -0500458 set_forwarding(forwarding_fd,"1\n");
459
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900460 // Loop until someone sends us a signal or brings down the tun interface.
461 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
462 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
463 exit(1);
464 }
465 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500466
467 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900468 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500469
470 return 0;
471}