blob: 09a86de8424a7a3fa7933383f15ad5f5ffc06295 [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
Nick Kralevich2edb7562013-02-28 14:15:22 -080041#include <sys/capability.h>
Lorenzo Colittid9084182013-03-22 00:42:21 +090042#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050043#include <linux/prctl.h>
44#include <linux/if.h>
45#include <linux/if_tun.h>
46#include <linux/if_ether.h>
47
48#include <private/android_filesystem_config.h>
49
Lorenzo Colittid9084182013-03-22 00:42:21 +090050#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050051#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
Lorenzo Colittibaf62992013-03-01 20:29:39 +090082/* function: stop_loop
83 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050084 */
Lorenzo Colitti9477a462013-11-18 15:56:02 +090085void stop_loop() {
Daniel Drowna45056e2012-03-23 10:42:54 -050086 running = 0;
87}
88
89/* function: tun_open
90 * tries to open the tunnel device
91 */
92int tun_open() {
93 int fd;
94
95 fd = open("/dev/tun", O_RDWR);
96 if(fd < 0) {
97 fd = open("/dev/net/tun", O_RDWR);
98 }
99
100 return fd;
101}
102
103/* function: tun_alloc
104 * creates a tun interface and names it
105 * dev - the name for the new tun device
106 */
107int tun_alloc(char *dev, int fd) {
108 struct ifreq ifr;
109 int err;
110
111 memset(&ifr, 0, sizeof(ifr));
112
113 ifr.ifr_flags = IFF_TUN;
114 if( *dev ) {
115 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
116 ifr.ifr_name[IFNAMSIZ-1] = '\0';
117 }
118
119 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
120 close(fd);
121 return err;
122 }
123 strcpy(dev, ifr.ifr_name);
124 return 0;
125}
126
127/* function: deconfigure_tun_ipv6
128 * removes the ipv6 route
129 * tunnel - tun device data
130 */
131void deconfigure_tun_ipv6(const struct tun_data *tunnel) {
132 int status;
133
134 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
135 128, NULL, 1, 0, ROUTE_DELETE);
136 if(status < 0) {
137 logmsg(ANDROID_LOG_WARN,"deconfigure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
138 }
139}
140
141/* function: configure_tun_ipv6
142 * configures the ipv6 route
143 * note: routes a /128 out of the (assumed routed to us) /64 to the CLAT interface
144 * tunnel - tun device data
145 */
146void configure_tun_ipv6(const struct tun_data *tunnel) {
147 struct in6_addr local_nat64_prefix_6;
148 int status;
149
150 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
151 128, NULL, 1, 0, ROUTE_CREATE);
152 if(status < 0) {
153 logmsg(ANDROID_LOG_FATAL,"configure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
154 exit(1);
155 }
156}
157
158/* function: interface_poll
159 * polls the uplink network interface for address changes
160 * tunnel - tun device data
161 */
162void interface_poll(const struct tun_data *tunnel) {
163 union anyip *interface_ip;
164
165 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
166 if(!interface_ip) {
167 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",Global_Clatd_Config.default_pdp_interface);
168 return;
169 }
170
171 config_generate_local_ipv6_subnet(&interface_ip->ip6);
172
173 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
174 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
175 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
176 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
177 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
178
179 // remove old route
180 deconfigure_tun_ipv6(tunnel);
181
182 // add new route, start translating packets to the new prefix
183 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
184 configure_tun_ipv6(tunnel);
185 }
186
187 free(interface_ip);
188}
189
190/* function: configure_tun_ip
191 * configures the ipv4 and ipv6 addresses on the tunnel interface
192 * tunnel - tun device data
193 */
194void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500195 int status;
196
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900197 // Configure the interface before bringing it up. As soon as we bring the interface up, the
198 // framework will be notified and will assume the interface's configuration has been finalized.
199 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
200 32, &Global_Clatd_Config.ipv4_local_subnet);
201 if(status < 0) {
202 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
203 exit(1);
204 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500205
JP Abgrall4e0dd832013-12-20 14:51:26 -0800206 status = add_address(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_address,
207 64, NULL);
208 if(status < 0) {
209 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(6) failed: %s",strerror(-status));
210 exit(1);
211 }
212
Daniel Drowna45056e2012-03-23 10:42:54 -0500213 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
214 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
215 exit(1);
216 }
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900217
Daniel Drowna45056e2012-03-23 10:42:54 -0500218 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
219 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
220 exit(1);
221 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500222
223 configure_tun_ipv6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500224}
225
226/* function: drop_root
227 * drops root privs but keeps the needed capability
228 */
229void drop_root() {
230 gid_t groups[] = { AID_INET };
231 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
232 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
233 exit(1);
234 }
235
236 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
237
238 if(setgid(AID_CLAT) < 0) {
239 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
240 exit(1);
241 }
242 if(setuid(AID_CLAT) < 0) {
243 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
244 exit(1);
245 }
246
247 struct __user_cap_header_struct header;
248 struct __user_cap_data_struct cap;
249 memset(&header, 0, sizeof(header));
250 memset(&cap, 0, sizeof(cap));
251
252 header.version = _LINUX_CAPABILITY_VERSION;
253 header.pid = 0; // 0 = change myself
254 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
255
256 if(capset(&header, &cap) < 0) {
257 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
258 exit(1);
259 }
260}
261
262/* function: configure_interface
263 * reads the configuration and applies it to the interface
264 * uplink_interface - network interface to use to reach the ipv6 internet
265 * plat_prefix - PLAT prefix to use
266 * tunnel - tun device data
267 */
268void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
269 int error;
270
271 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
272 logmsg(ANDROID_LOG_FATAL,"read_config failed");
273 exit(1);
274 }
275
276 if(Global_Clatd_Config.mtu > MAXMTU) {
277 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
278 Global_Clatd_Config.mtu = MAXMTU;
279 }
280 if(Global_Clatd_Config.mtu <= 0) {
281 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
282 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
283 }
284 if(Global_Clatd_Config.mtu < 1280) {
285 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
286 Global_Clatd_Config.mtu = 1280;
287 }
288
289 if(Global_Clatd_Config.ipv4mtu <= 0 || (Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - 20)) {
290 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu-20;
291 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
292 }
293
294 error = tun_alloc(tunnel->device6, tunnel->fd6);
295 if(error < 0) {
296 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
297 exit(1);
298 }
299
300 error = tun_alloc(tunnel->device4, tunnel->fd4);
301 if(error < 0) {
302 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
303 exit(1);
304 }
305
306 configure_tun_ip(tunnel);
307}
308
309/* function: packet_handler
310 * takes a tun header and a packet and sends it down the stack
311 * tunnel - tun device data
312 * tun_header - tun header
313 * packet - packet
314 * packetsize - size of packet
315 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900316void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
317 size_t packetsize) {
318 int fd;
319 int iov_len = 0;
320
321 // Allocate buffers for all packet headers.
322 struct tun_pi tun_targ;
323 char iphdr[sizeof(struct ip6_hdr)];
324 char transporthdr[MAX_TCP_HDR];
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900325 char icmp_iphdr[sizeof(struct ip6_hdr)];
326 char icmp_transporthdr[MAX_TCP_HDR];
Lorenzo Colittid9084182013-03-22 00:42:21 +0900327
328 // iovec of the packets we'll send. This gets passed down to the translation functions.
329 clat_packet out = {
330 { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
331 { iphdr, 0 }, // IP header.
332 { transporthdr, 0 }, // Transport layer header.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900333 { icmp_iphdr, 0 }, // ICMP error inner IP header.
334 { icmp_transporthdr, 0 }, // ICMP error transport layer header.
Lorenzo Colittid9084182013-03-22 00:42:21 +0900335 { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
336 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500337
338 if(tun_header->flags != 0) {
339 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
340 }
341
Lorenzo Colittid9084182013-03-22 00:42:21 +0900342 if(ntohs(tun_header->proto) == ETH_P_IP) {
343 fd = tunnel->fd6;
344 fill_tun_header(&tun_targ, ETH_P_IPV6);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900345 iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900346 } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
347 fd = tunnel->fd4;
348 fill_tun_header(&tun_targ, ETH_P_IP);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900349 iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Daniel Drowna45056e2012-03-23 10:42:54 -0500350 } else {
351 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
352 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900353
354 if (iov_len > 0) {
355 writev(fd, out, iov_len);
356 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500357}
358
359/* function: read_packet
360 * reads a packet from the tunnel fd and passes it down the stack
361 * active_fd - tun file descriptor marked ready for reading
362 * tunnel - tun device data
363 */
364void read_packet(int active_fd, const struct tun_data *tunnel) {
365 ssize_t readlen;
366 char packet[PACKETLEN];
367
368 // in case something ignores the packet length
369 memset(packet, 0, PACKETLEN);
370
371 readlen = read(active_fd,packet,PACKETLEN);
372
373 if(readlen < 0) {
374 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
375 return;
376 } else if(readlen == 0) {
377 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
378 running = 0;
379 } else {
380 struct tun_pi tun_header;
381 ssize_t header_size = sizeof(struct tun_pi);
382
383 if(readlen < header_size) {
384 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
385 return;
386 }
387
Lorenzo Colittid9084182013-03-22 00:42:21 +0900388 packet_handler(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500389 }
390}
391
392/* function: event_loop
393 * reads packets from the tun network interface and passes them down the stack
394 * tunnel - tun device data
395 */
396void event_loop(const struct tun_data *tunnel) {
397 time_t last_interface_poll;
398 struct pollfd wait_fd[2];
399
400 // start the poll timer
401 last_interface_poll = time(NULL);
402
403 wait_fd[0].fd = tunnel->fd6;
404 wait_fd[0].events = POLLIN;
405 wait_fd[0].revents = 0;
406 wait_fd[1].fd = tunnel->fd4;
407 wait_fd[1].events = POLLIN;
408 wait_fd[1].revents = 0;
409
410 while(running) {
411 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
412 if(errno != EINTR) {
413 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
414 }
415 } else {
416 int i;
417 for(i = 0; i < 2; i++) {
418 if((wait_fd[i].revents & POLLIN) != 0) {
419 read_packet(wait_fd[i].fd,tunnel);
420 }
421 }
422 }
423
424 time_t now = time(NULL);
425 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
426 interface_poll(tunnel);
427 last_interface_poll = now;
428 }
429 }
430}
431
432/* function: print_help
433 * in case the user is running this on the command line
434 */
435void print_help() {
436 printf("android-clat arguments:\n");
437 printf("-i [uplink interface]\n");
438 printf("-p [plat prefix]\n");
439}
440
441/* function: main
442 * allocate and setup the tun device, then run the event loop
443 */
444int main(int argc, char **argv) {
445 struct tun_data tunnel;
446 int opt;
447 char *uplink_interface = NULL, *plat_prefix = NULL;
448
449 strcpy(tunnel.device6, DEVICENAME6);
450 strcpy(tunnel.device4, DEVICENAME4);
451
452 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
453 switch(opt) {
454 case 'i':
455 uplink_interface = optarg;
456 break;
457 case 'p':
458 plat_prefix = optarg;
459 break;
460 case 'h':
461 default:
462 print_help();
463 exit(1);
464 break;
465 }
466 }
467
468 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900469 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500470 printf("I need an interface\n");
471 exit(1);
472 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900473 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500474
475 // open the tunnel device before dropping privs
476 tunnel.fd6 = tun_open();
477 if(tunnel.fd6 < 0) {
JP Abgrall4e0dd832013-12-20 14:51:26 -0800478 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500479 exit(1);
480 }
481
482 tunnel.fd4 = tun_open();
483 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900484 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500485 exit(1);
486 }
487
488 // open the forwarding configuration before dropping privs
489 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
490 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900491 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
492 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500493 exit(1);
494 }
495
Daniel Drowna45056e2012-03-23 10:42:54 -0500496 // run under a regular user
497 drop_root();
498
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900499 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
500 // "local", but that only works for the netd process itself.
501 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500502
503 configure_interface(uplink_interface, plat_prefix, &tunnel);
504
Daniel Drowna45056e2012-03-23 10:42:54 -0500505 set_forwarding(forwarding_fd,"1\n");
506
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900507 // Loop until someone sends us a signal or brings down the tun interface.
508 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
509 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
510 exit(1);
511 }
512 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500513
514 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900515 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500516
517 return 0;
518}