blob: 17a354802fab2459308bda4d52ddd0007eb1b47c [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
32#include <netinet/in.h>
33#include <netinet/ip.h>
34#include <netinet/ip_icmp.h>
35#include <netinet/udp.h>
36#include <netinet/tcp.h>
37#include <netinet/ip6.h>
38#include <netinet/icmp6.h>
39#include <linux/icmp.h>
40
41#include <linux/capability.h>
42#include <linux/prctl.h>
43#include <linux/if.h>
44#include <linux/if_tun.h>
45#include <linux/if_ether.h>
46
47#include <private/android_filesystem_config.h>
48
49#include "ipv4.h"
50#include "ipv6.h"
51#include "clatd.h"
52#include "config.h"
53#include "logging.h"
54#include "setif.h"
55#include "setroute.h"
56#include "mtu.h"
57#include "getaddr.h"
58#include "dump.h"
59
60#define DEVICENAME6 "clat"
61#define DEVICENAME4 "clat4"
62
63int forwarding_fd = -1;
64volatile sig_atomic_t running = 1;
65
66struct tun_data {
67 char device6[IFNAMSIZ], device4[IFNAMSIZ];
68 int fd6, fd4;
69};
70
71/* function: set_forwarding
72 * enables/disables ipv6 forwarding
73 */
74void set_forwarding(int fd, const char *setting) {
75 /* we have to forward packets from the WAN to the tun interface */
76 if(write(fd, setting, strlen(setting)) < 0) {
77 logmsg(ANDROID_LOG_FATAL,"set_forwarding(%s) failed: %s", setting, strerror(errno));
78 exit(1);
79 }
80}
81
82/* function: set_accept_ra
83 * accepts IPv6 RAs on all interfaces, even when forwarding is on
84 */
85void set_accept_ra() {
86 int fd, i;
87 const char *interface_names[] = {"wlan0","default",NULL};
88 const char ipv6_sysctl[] = "/proc/sys/net/ipv6/conf/";
89 const char accept_ra[] = "/accept_ra";
90
91 for(i = 0; interface_names[i]; i++) {
92 ssize_t sysctl_path_len = strlen(ipv6_sysctl)+strlen(interface_names[i])+strlen(accept_ra)+1;
93 char *sysctl_path = malloc(sysctl_path_len);
94 if(!sysctl_path) {
95 logmsg(ANDROID_LOG_FATAL,"set_accept_ra: malloc failed");
96 exit(1);
97 }
98 snprintf(sysctl_path, sysctl_path_len, "%s%s%s", ipv6_sysctl, interface_names[i], accept_ra);
99
100 fd = open(sysctl_path, O_RDWR);
101 free(sysctl_path);
102 if(fd < 0) {
103 continue;
104 }
105 if(write(fd, "2\n", 2) < 0) {
106 logmsg(ANDROID_LOG_WARN,"write to (%s)accept_ra failed: %s",interface_names[i],strerror(errno));
107 }
108 close(fd);
109 }
110}
111
112/* function: got_sigterm
113 * signal handler: mark it time to clean up
114 */
115void got_sigterm(int signal) {
116 running = 0;
117}
118
119/* function: tun_open
120 * tries to open the tunnel device
121 */
122int tun_open() {
123 int fd;
124
125 fd = open("/dev/tun", O_RDWR);
126 if(fd < 0) {
127 fd = open("/dev/net/tun", O_RDWR);
128 }
129
130 return fd;
131}
132
133/* function: tun_alloc
134 * creates a tun interface and names it
135 * dev - the name for the new tun device
136 */
137int tun_alloc(char *dev, int fd) {
138 struct ifreq ifr;
139 int err;
140
141 memset(&ifr, 0, sizeof(ifr));
142
143 ifr.ifr_flags = IFF_TUN;
144 if( *dev ) {
145 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
146 ifr.ifr_name[IFNAMSIZ-1] = '\0';
147 }
148
149 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
150 close(fd);
151 return err;
152 }
153 strcpy(dev, ifr.ifr_name);
154 return 0;
155}
156
157/* function: deconfigure_tun_ipv6
158 * removes the ipv6 route
159 * tunnel - tun device data
160 */
161void deconfigure_tun_ipv6(const struct tun_data *tunnel) {
162 int status;
163
164 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
165 128, NULL, 1, 0, ROUTE_DELETE);
166 if(status < 0) {
167 logmsg(ANDROID_LOG_WARN,"deconfigure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
168 }
169}
170
171/* function: configure_tun_ipv6
172 * configures the ipv6 route
173 * note: routes a /128 out of the (assumed routed to us) /64 to the CLAT interface
174 * tunnel - tun device data
175 */
176void configure_tun_ipv6(const struct tun_data *tunnel) {
177 struct in6_addr local_nat64_prefix_6;
178 int status;
179
180 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
181 128, NULL, 1, 0, ROUTE_CREATE);
182 if(status < 0) {
183 logmsg(ANDROID_LOG_FATAL,"configure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
184 exit(1);
185 }
186}
187
188/* function: interface_poll
189 * polls the uplink network interface for address changes
190 * tunnel - tun device data
191 */
192void interface_poll(const struct tun_data *tunnel) {
193 union anyip *interface_ip;
194
195 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
196 if(!interface_ip) {
197 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",Global_Clatd_Config.default_pdp_interface);
198 return;
199 }
200
201 config_generate_local_ipv6_subnet(&interface_ip->ip6);
202
203 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
204 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
205 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
206 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
207 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
208
209 // remove old route
210 deconfigure_tun_ipv6(tunnel);
211
212 // add new route, start translating packets to the new prefix
213 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
214 configure_tun_ipv6(tunnel);
215 }
216
217 free(interface_ip);
218}
219
220/* function: configure_tun_ip
221 * configures the ipv4 and ipv6 addresses on the tunnel interface
222 * tunnel - tun device data
223 */
224void configure_tun_ip(const struct tun_data *tunnel) {
225 struct in_addr default_4;
226 int status;
227
228 default_4.s_addr = INADDR_ANY;
229
230 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
231 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
232 exit(1);
233 }
234 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
235 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
236 exit(1);
237 }
238 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
239 32, &Global_Clatd_Config.ipv4_local_subnet);
240 if(status < 0) {
241 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
242 exit(1);
243 }
244
245 configure_tun_ipv6(tunnel);
246
247 /* setup default ipv4 route */
248 status = if_route(tunnel->device4, AF_INET, &default_4, 0, NULL, 1, 0, ROUTE_REPLACE);
249 if(status < 0) {
250 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_route failed: %s",strerror(-status));
251 exit(1);
252 }
253}
254
255/* function: drop_root
256 * drops root privs but keeps the needed capability
257 */
258void drop_root() {
259 gid_t groups[] = { AID_INET };
260 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
261 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
262 exit(1);
263 }
264
265 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
266
267 if(setgid(AID_CLAT) < 0) {
268 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
269 exit(1);
270 }
271 if(setuid(AID_CLAT) < 0) {
272 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
273 exit(1);
274 }
275
276 struct __user_cap_header_struct header;
277 struct __user_cap_data_struct cap;
278 memset(&header, 0, sizeof(header));
279 memset(&cap, 0, sizeof(cap));
280
281 header.version = _LINUX_CAPABILITY_VERSION;
282 header.pid = 0; // 0 = change myself
283 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
284
285 if(capset(&header, &cap) < 0) {
286 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
287 exit(1);
288 }
289}
290
291/* function: configure_interface
292 * reads the configuration and applies it to the interface
293 * uplink_interface - network interface to use to reach the ipv6 internet
294 * plat_prefix - PLAT prefix to use
295 * tunnel - tun device data
296 */
297void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
298 int error;
299
300 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
301 logmsg(ANDROID_LOG_FATAL,"read_config failed");
302 exit(1);
303 }
304
305 if(Global_Clatd_Config.mtu > MAXMTU) {
306 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
307 Global_Clatd_Config.mtu = MAXMTU;
308 }
309 if(Global_Clatd_Config.mtu <= 0) {
310 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
311 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
312 }
313 if(Global_Clatd_Config.mtu < 1280) {
314 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
315 Global_Clatd_Config.mtu = 1280;
316 }
317
318 if(Global_Clatd_Config.ipv4mtu <= 0 || (Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - 20)) {
319 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu-20;
320 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
321 }
322
323 error = tun_alloc(tunnel->device6, tunnel->fd6);
324 if(error < 0) {
325 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
326 exit(1);
327 }
328
329 error = tun_alloc(tunnel->device4, tunnel->fd4);
330 if(error < 0) {
331 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
332 exit(1);
333 }
334
335 configure_tun_ip(tunnel);
336}
337
338/* function: packet_handler
339 * takes a tun header and a packet and sends it down the stack
340 * tunnel - tun device data
341 * tun_header - tun header
342 * packet - packet
343 * packetsize - size of packet
344 */
345void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet, size_t packetsize) {
346 tun_header->proto = ntohs(tun_header->proto);
347
348 if(tun_header->flags != 0) {
349 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
350 }
351
352 if(tun_header->proto == ETH_P_IP) {
353 ip_packet(tunnel->fd6,packet,packetsize);
354 } else if(tun_header->proto == ETH_P_IPV6) {
355 ipv6_packet(tunnel->fd4,packet,packetsize);
356 } else {
357 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
358 }
359}
360
361/* function: read_packet
362 * reads a packet from the tunnel fd and passes it down the stack
363 * active_fd - tun file descriptor marked ready for reading
364 * tunnel - tun device data
365 */
366void read_packet(int active_fd, const struct tun_data *tunnel) {
367 ssize_t readlen;
368 char packet[PACKETLEN];
369
370 // in case something ignores the packet length
371 memset(packet, 0, PACKETLEN);
372
373 readlen = read(active_fd,packet,PACKETLEN);
374
375 if(readlen < 0) {
376 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
377 return;
378 } else if(readlen == 0) {
379 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
380 running = 0;
381 } else {
382 struct tun_pi tun_header;
383 ssize_t header_size = sizeof(struct tun_pi);
384
385 if(readlen < header_size) {
386 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
387 return;
388 }
389
390 memcpy(&tun_header, packet, header_size);
391
392 packet_handler(tunnel, &tun_header, packet+header_size, readlen-header_size);
393 }
394}
395
396/* function: event_loop
397 * reads packets from the tun network interface and passes them down the stack
398 * tunnel - tun device data
399 */
400void event_loop(const struct tun_data *tunnel) {
401 time_t last_interface_poll;
402 struct pollfd wait_fd[2];
403
404 // start the poll timer
405 last_interface_poll = time(NULL);
406
407 wait_fd[0].fd = tunnel->fd6;
408 wait_fd[0].events = POLLIN;
409 wait_fd[0].revents = 0;
410 wait_fd[1].fd = tunnel->fd4;
411 wait_fd[1].events = POLLIN;
412 wait_fd[1].revents = 0;
413
414 while(running) {
415 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
416 if(errno != EINTR) {
417 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
418 }
419 } else {
420 int i;
421 for(i = 0; i < 2; i++) {
422 if((wait_fd[i].revents & POLLIN) != 0) {
423 read_packet(wait_fd[i].fd,tunnel);
424 }
425 }
426 }
427
428 time_t now = time(NULL);
429 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
430 interface_poll(tunnel);
431 last_interface_poll = now;
432 }
433 }
434}
435
436/* function: print_help
437 * in case the user is running this on the command line
438 */
439void print_help() {
440 printf("android-clat arguments:\n");
441 printf("-i [uplink interface]\n");
442 printf("-p [plat prefix]\n");
443}
444
445/* function: main
446 * allocate and setup the tun device, then run the event loop
447 */
448int main(int argc, char **argv) {
449 struct tun_data tunnel;
450 int opt;
451 char *uplink_interface = NULL, *plat_prefix = NULL;
452
453 strcpy(tunnel.device6, DEVICENAME6);
454 strcpy(tunnel.device4, DEVICENAME4);
455
456 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
457 switch(opt) {
458 case 'i':
459 uplink_interface = optarg;
460 break;
461 case 'p':
462 plat_prefix = optarg;
463 break;
464 case 'h':
465 default:
466 print_help();
467 exit(1);
468 break;
469 }
470 }
471
472 if(uplink_interface == NULL) {
473 logmsg(ANDROID_LOG_FATAL,"clatd called without an interface");
474 printf("I need an interface\n");
475 exit(1);
476 }
477
478 // open the tunnel device before dropping privs
479 tunnel.fd6 = tun_open();
480 if(tunnel.fd6 < 0) {
481 logmsg(ANDROID_LOG_FATAL,"tun_open failed: %s",strerror(errno));
482 exit(1);
483 }
484
485 tunnel.fd4 = tun_open();
486 if(tunnel.fd4 < 0) {
487 logmsg(ANDROID_LOG_FATAL,"tun_open4 failed: %s",strerror(errno));
488 exit(1);
489 }
490
491 // open the forwarding configuration before dropping privs
492 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
493 if(forwarding_fd < 0) {
494 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",strerror(errno));
495 exit(1);
496 }
497
498 // forwarding slows down IPv6 config while transitioning to wifi
499 // forwarding also removes default routes learned from a RA
500 set_accept_ra();
501
502 // run under a regular user
503 drop_root();
504
505 if(signal(SIGTERM, got_sigterm) == SIG_ERR) {
506 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
507 exit(1);
508 }
509
510 configure_interface(uplink_interface, plat_prefix, &tunnel);
511
512 // turning on forwarding in linux has a side effect of calling rt6_purge_dflt_routers
513 // workaround: turn the RA-learned default route into a static route
514 set_default_ipv6_route(uplink_interface);
515 set_forwarding(forwarding_fd,"1\n");
516
517 event_loop(&tunnel); // event_loop returns if someone sets the tun interface down manually
518
519 set_forwarding(forwarding_fd,"0\n");
520
521 return 0;
522}