blob: a0ee07def2fe60022672d434c4b871b10e351926 [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 Colittid0024fb2013-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) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500147 int status;
148
149 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
150 128, NULL, 1, 0, ROUTE_CREATE);
151 if(status < 0) {
152 logmsg(ANDROID_LOG_FATAL,"configure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
153 exit(1);
154 }
155}
156
157/* function: interface_poll
158 * polls the uplink network interface for address changes
159 * tunnel - tun device data
160 */
161void interface_poll(const struct tun_data *tunnel) {
162 union anyip *interface_ip;
163
164 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
165 if(!interface_ip) {
166 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",Global_Clatd_Config.default_pdp_interface);
167 return;
168 }
169
170 config_generate_local_ipv6_subnet(&interface_ip->ip6);
171
172 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
173 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
174 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
175 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
176 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
177
178 // remove old route
179 deconfigure_tun_ipv6(tunnel);
180
181 // add new route, start translating packets to the new prefix
182 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
183 configure_tun_ipv6(tunnel);
184 }
185
186 free(interface_ip);
187}
188
189/* function: configure_tun_ip
190 * configures the ipv4 and ipv6 addresses on the tunnel interface
191 * tunnel - tun device data
192 */
193void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500194 int status;
195
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900196 // Configure the interface before bringing it up. As soon as we bring the interface up, the
197 // framework will be notified and will assume the interface's configuration has been finalized.
198 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
199 32, &Global_Clatd_Config.ipv4_local_subnet);
200 if(status < 0) {
201 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
202 exit(1);
203 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500204
JP Abgrallade83082013-12-20 14:51:26 -0800205 status = add_address(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_address,
206 64, NULL);
207 if(status < 0) {
208 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(6) failed: %s",strerror(-status));
209 exit(1);
210 }
211
Daniel Drowna45056e2012-03-23 10:42:54 -0500212 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
213 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
214 exit(1);
215 }
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900216
Daniel Drowna45056e2012-03-23 10:42:54 -0500217 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
218 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
219 exit(1);
220 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500221
222 configure_tun_ipv6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500223}
224
225/* function: drop_root
226 * drops root privs but keeps the needed capability
227 */
228void drop_root() {
229 gid_t groups[] = { AID_INET };
230 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
231 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
232 exit(1);
233 }
234
235 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
236
237 if(setgid(AID_CLAT) < 0) {
238 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
239 exit(1);
240 }
241 if(setuid(AID_CLAT) < 0) {
242 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
243 exit(1);
244 }
245
246 struct __user_cap_header_struct header;
247 struct __user_cap_data_struct cap;
248 memset(&header, 0, sizeof(header));
249 memset(&cap, 0, sizeof(cap));
250
251 header.version = _LINUX_CAPABILITY_VERSION;
252 header.pid = 0; // 0 = change myself
253 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
254
255 if(capset(&header, &cap) < 0) {
256 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
257 exit(1);
258 }
259}
260
261/* function: configure_interface
262 * reads the configuration and applies it to the interface
263 * uplink_interface - network interface to use to reach the ipv6 internet
264 * plat_prefix - PLAT prefix to use
265 * tunnel - tun device data
266 */
267void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
268 int error;
269
270 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
271 logmsg(ANDROID_LOG_FATAL,"read_config failed");
272 exit(1);
273 }
274
275 if(Global_Clatd_Config.mtu > MAXMTU) {
276 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
277 Global_Clatd_Config.mtu = MAXMTU;
278 }
279 if(Global_Clatd_Config.mtu <= 0) {
280 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
281 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
282 }
283 if(Global_Clatd_Config.mtu < 1280) {
284 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
285 Global_Clatd_Config.mtu = 1280;
286 }
287
288 if(Global_Clatd_Config.ipv4mtu <= 0 || (Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - 20)) {
289 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu-20;
290 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
291 }
292
293 error = tun_alloc(tunnel->device6, tunnel->fd6);
294 if(error < 0) {
295 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
296 exit(1);
297 }
298
299 error = tun_alloc(tunnel->device4, tunnel->fd4);
300 if(error < 0) {
301 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
302 exit(1);
303 }
304
305 configure_tun_ip(tunnel);
306}
307
308/* function: packet_handler
309 * takes a tun header and a packet and sends it down the stack
310 * tunnel - tun device data
311 * tun_header - tun header
312 * packet - packet
313 * packetsize - size of packet
314 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900315void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
316 size_t packetsize) {
317 int fd;
318 int iov_len = 0;
319
320 // Allocate buffers for all packet headers.
321 struct tun_pi tun_targ;
322 char iphdr[sizeof(struct ip6_hdr)];
323 char transporthdr[MAX_TCP_HDR];
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900324 char icmp_iphdr[sizeof(struct ip6_hdr)];
325 char icmp_transporthdr[MAX_TCP_HDR];
Lorenzo Colittid9084182013-03-22 00:42:21 +0900326
327 // iovec of the packets we'll send. This gets passed down to the translation functions.
328 clat_packet out = {
329 { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
330 { iphdr, 0 }, // IP header.
331 { transporthdr, 0 }, // Transport layer header.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900332 { icmp_iphdr, 0 }, // ICMP error inner IP header.
333 { icmp_transporthdr, 0 }, // ICMP error transport layer header.
Lorenzo Colittid9084182013-03-22 00:42:21 +0900334 { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
335 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500336
337 if(tun_header->flags != 0) {
338 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
339 }
340
Lorenzo Colittid9084182013-03-22 00:42:21 +0900341 if(ntohs(tun_header->proto) == ETH_P_IP) {
342 fd = tunnel->fd6;
343 fill_tun_header(&tun_targ, ETH_P_IPV6);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900344 iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900345 } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
346 fd = tunnel->fd4;
347 fill_tun_header(&tun_targ, ETH_P_IP);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900348 iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Daniel Drowna45056e2012-03-23 10:42:54 -0500349 } else {
350 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
351 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900352
353 if (iov_len > 0) {
354 writev(fd, out, iov_len);
355 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500356}
357
358/* function: read_packet
359 * reads a packet from the tunnel fd and passes it down the stack
360 * active_fd - tun file descriptor marked ready for reading
361 * tunnel - tun device data
362 */
363void read_packet(int active_fd, const struct tun_data *tunnel) {
364 ssize_t readlen;
365 char packet[PACKETLEN];
366
367 // in case something ignores the packet length
368 memset(packet, 0, PACKETLEN);
369
370 readlen = read(active_fd,packet,PACKETLEN);
371
372 if(readlen < 0) {
373 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
374 return;
375 } else if(readlen == 0) {
376 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
377 running = 0;
378 } else {
Daniel Drowna45056e2012-03-23 10:42:54 -0500379 ssize_t header_size = sizeof(struct tun_pi);
380
381 if(readlen < header_size) {
382 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
383 return;
384 }
385
Lorenzo Colittid9084182013-03-22 00:42:21 +0900386 packet_handler(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500387 }
388}
389
390/* function: event_loop
391 * reads packets from the tun network interface and passes them down the stack
392 * tunnel - tun device data
393 */
394void event_loop(const struct tun_data *tunnel) {
395 time_t last_interface_poll;
396 struct pollfd wait_fd[2];
397
398 // start the poll timer
399 last_interface_poll = time(NULL);
400
401 wait_fd[0].fd = tunnel->fd6;
402 wait_fd[0].events = POLLIN;
403 wait_fd[0].revents = 0;
404 wait_fd[1].fd = tunnel->fd4;
405 wait_fd[1].events = POLLIN;
406 wait_fd[1].revents = 0;
407
408 while(running) {
409 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
410 if(errno != EINTR) {
411 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
412 }
413 } else {
414 int i;
415 for(i = 0; i < 2; i++) {
416 if((wait_fd[i].revents & POLLIN) != 0) {
417 read_packet(wait_fd[i].fd,tunnel);
418 }
419 }
420 }
421
422 time_t now = time(NULL);
423 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
424 interface_poll(tunnel);
425 last_interface_poll = now;
426 }
427 }
428}
429
430/* function: print_help
431 * in case the user is running this on the command line
432 */
433void print_help() {
434 printf("android-clat arguments:\n");
435 printf("-i [uplink interface]\n");
436 printf("-p [plat prefix]\n");
437}
438
439/* function: main
440 * allocate and setup the tun device, then run the event loop
441 */
442int main(int argc, char **argv) {
443 struct tun_data tunnel;
444 int opt;
445 char *uplink_interface = NULL, *plat_prefix = NULL;
446
447 strcpy(tunnel.device6, DEVICENAME6);
448 strcpy(tunnel.device4, DEVICENAME4);
449
450 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
451 switch(opt) {
452 case 'i':
453 uplink_interface = optarg;
454 break;
455 case 'p':
456 plat_prefix = optarg;
457 break;
458 case 'h':
459 default:
460 print_help();
461 exit(1);
462 break;
463 }
464 }
465
466 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900467 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500468 printf("I need an interface\n");
469 exit(1);
470 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900471 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500472
473 // open the tunnel device before dropping privs
474 tunnel.fd6 = tun_open();
475 if(tunnel.fd6 < 0) {
JP Abgrallade83082013-12-20 14:51:26 -0800476 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500477 exit(1);
478 }
479
480 tunnel.fd4 = tun_open();
481 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900482 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500483 exit(1);
484 }
485
486 // open the forwarding configuration before dropping privs
487 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
488 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900489 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
490 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500491 exit(1);
492 }
493
Daniel Drowna45056e2012-03-23 10:42:54 -0500494 // run under a regular user
495 drop_root();
496
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900497 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
498 // "local", but that only works for the netd process itself.
499 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500500
501 configure_interface(uplink_interface, plat_prefix, &tunnel);
502
Daniel Drowna45056e2012-03-23 10:42:54 -0500503 set_forwarding(forwarding_fd,"1\n");
504
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900505 // Loop until someone sends us a signal or brings down the tun interface.
506 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
507 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
508 exit(1);
509 }
510 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500511
512 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900513 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500514
515 return 0;
516}