blob: 26ecb57bd8c92ae4a9f0115e53eecfa986ed2044 [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
50#include "ipv4.h"
51#include "ipv6.h"
Lorenzo Colittid9084182013-03-22 00:42:21 +090052#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050053#include "clatd.h"
54#include "config.h"
55#include "logging.h"
56#include "setif.h"
57#include "setroute.h"
58#include "mtu.h"
59#include "getaddr.h"
60#include "dump.h"
61
62#define DEVICENAME6 "clat"
63#define DEVICENAME4 "clat4"
64
65int forwarding_fd = -1;
66volatile sig_atomic_t running = 1;
67
68struct tun_data {
69 char device6[IFNAMSIZ], device4[IFNAMSIZ];
70 int fd6, fd4;
71};
72
73/* function: set_forwarding
74 * enables/disables ipv6 forwarding
75 */
76void set_forwarding(int fd, const char *setting) {
77 /* we have to forward packets from the WAN to the tun interface */
78 if(write(fd, setting, strlen(setting)) < 0) {
79 logmsg(ANDROID_LOG_FATAL,"set_forwarding(%s) failed: %s", setting, strerror(errno));
80 exit(1);
81 }
82}
83
Lorenzo Colittibaf62992013-03-01 20:29:39 +090084/* function: stop_loop
85 * signal handler: stop the event loop
Daniel Drowna45056e2012-03-23 10:42:54 -050086 */
Lorenzo Colittibaf62992013-03-01 20:29:39 +090087void stop_loop(int signal) {
Daniel Drowna45056e2012-03-23 10:42:54 -050088 running = 0;
89}
90
91/* function: tun_open
92 * tries to open the tunnel device
93 */
94int tun_open() {
95 int fd;
96
97 fd = open("/dev/tun", O_RDWR);
98 if(fd < 0) {
99 fd = open("/dev/net/tun", O_RDWR);
100 }
101
102 return fd;
103}
104
105/* function: tun_alloc
106 * creates a tun interface and names it
107 * dev - the name for the new tun device
108 */
109int tun_alloc(char *dev, int fd) {
110 struct ifreq ifr;
111 int err;
112
113 memset(&ifr, 0, sizeof(ifr));
114
115 ifr.ifr_flags = IFF_TUN;
116 if( *dev ) {
117 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
118 ifr.ifr_name[IFNAMSIZ-1] = '\0';
119 }
120
121 if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
122 close(fd);
123 return err;
124 }
125 strcpy(dev, ifr.ifr_name);
126 return 0;
127}
128
129/* function: deconfigure_tun_ipv6
130 * removes the ipv6 route
131 * tunnel - tun device data
132 */
133void deconfigure_tun_ipv6(const struct tun_data *tunnel) {
134 int status;
135
136 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
137 128, NULL, 1, 0, ROUTE_DELETE);
138 if(status < 0) {
139 logmsg(ANDROID_LOG_WARN,"deconfigure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
140 }
141}
142
143/* function: configure_tun_ipv6
144 * configures the ipv6 route
145 * note: routes a /128 out of the (assumed routed to us) /64 to the CLAT interface
146 * tunnel - tun device data
147 */
148void configure_tun_ipv6(const struct tun_data *tunnel) {
149 struct in6_addr local_nat64_prefix_6;
150 int status;
151
152 status = if_route(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_subnet,
153 128, NULL, 1, 0, ROUTE_CREATE);
154 if(status < 0) {
155 logmsg(ANDROID_LOG_FATAL,"configure_tun_ipv6/if_route(6) failed: %s",strerror(-status));
156 exit(1);
157 }
158}
159
160/* function: interface_poll
161 * polls the uplink network interface for address changes
162 * tunnel - tun device data
163 */
164void interface_poll(const struct tun_data *tunnel) {
165 union anyip *interface_ip;
166
167 interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
168 if(!interface_ip) {
169 logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",Global_Clatd_Config.default_pdp_interface);
170 return;
171 }
172
173 config_generate_local_ipv6_subnet(&interface_ip->ip6);
174
175 if(!IN6_ARE_ADDR_EQUAL(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
176 char from_addr[INET6_ADDRSTRLEN], to_addr[INET6_ADDRSTRLEN];
177 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, from_addr, sizeof(from_addr));
178 inet_ntop(AF_INET6, &interface_ip->ip6, to_addr, sizeof(to_addr));
179 logmsg(ANDROID_LOG_WARN, "clat subnet changed from %s to %s", from_addr, to_addr);
180
181 // remove old route
182 deconfigure_tun_ipv6(tunnel);
183
184 // add new route, start translating packets to the new prefix
185 memcpy(&Global_Clatd_Config.ipv6_local_subnet, &interface_ip->ip6, sizeof(struct in6_addr));
186 configure_tun_ipv6(tunnel);
187 }
188
189 free(interface_ip);
190}
191
192/* function: configure_tun_ip
193 * configures the ipv4 and ipv6 addresses on the tunnel interface
194 * tunnel - tun device data
195 */
196void configure_tun_ip(const struct tun_data *tunnel) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500197 int status;
198
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900199 // Configure the interface before bringing it up. As soon as we bring the interface up, the
200 // framework will be notified and will assume the interface's configuration has been finalized.
201 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
202 32, &Global_Clatd_Config.ipv4_local_subnet);
203 if(status < 0) {
204 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
205 exit(1);
206 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500207
JP Abgrallade83082013-12-20 14:51:26 -0800208 status = add_address(tunnel->device6, AF_INET6, &Global_Clatd_Config.ipv6_local_address,
209 64, NULL);
210 if(status < 0) {
211 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(6) failed: %s",strerror(-status));
212 exit(1);
213 }
214
Daniel Drowna45056e2012-03-23 10:42:54 -0500215 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
216 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
217 exit(1);
218 }
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900219
Daniel Drowna45056e2012-03-23 10:42:54 -0500220 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
221 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
222 exit(1);
223 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500224
225 configure_tun_ipv6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500226}
227
228/* function: drop_root
229 * drops root privs but keeps the needed capability
230 */
231void drop_root() {
232 gid_t groups[] = { AID_INET };
233 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
234 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
235 exit(1);
236 }
237
238 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
239
240 if(setgid(AID_CLAT) < 0) {
241 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
242 exit(1);
243 }
244 if(setuid(AID_CLAT) < 0) {
245 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
246 exit(1);
247 }
248
249 struct __user_cap_header_struct header;
250 struct __user_cap_data_struct cap;
251 memset(&header, 0, sizeof(header));
252 memset(&cap, 0, sizeof(cap));
253
254 header.version = _LINUX_CAPABILITY_VERSION;
255 header.pid = 0; // 0 = change myself
256 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
257
258 if(capset(&header, &cap) < 0) {
259 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
260 exit(1);
261 }
262}
263
264/* function: configure_interface
265 * reads the configuration and applies it to the interface
266 * uplink_interface - network interface to use to reach the ipv6 internet
267 * plat_prefix - PLAT prefix to use
268 * tunnel - tun device data
269 */
270void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
271 int error;
272
273 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
274 logmsg(ANDROID_LOG_FATAL,"read_config failed");
275 exit(1);
276 }
277
278 if(Global_Clatd_Config.mtu > MAXMTU) {
279 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
280 Global_Clatd_Config.mtu = MAXMTU;
281 }
282 if(Global_Clatd_Config.mtu <= 0) {
283 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
284 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
285 }
286 if(Global_Clatd_Config.mtu < 1280) {
287 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
288 Global_Clatd_Config.mtu = 1280;
289 }
290
291 if(Global_Clatd_Config.ipv4mtu <= 0 || (Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - 20)) {
292 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu-20;
293 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
294 }
295
296 error = tun_alloc(tunnel->device6, tunnel->fd6);
297 if(error < 0) {
298 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
299 exit(1);
300 }
301
302 error = tun_alloc(tunnel->device4, tunnel->fd4);
303 if(error < 0) {
304 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
305 exit(1);
306 }
307
308 configure_tun_ip(tunnel);
309}
310
311/* function: packet_handler
312 * takes a tun header and a packet and sends it down the stack
313 * tunnel - tun device data
314 * tun_header - tun header
315 * packet - packet
316 * packetsize - size of packet
317 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900318void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
319 size_t packetsize) {
320 int fd;
321 int iov_len = 0;
322
323 // Allocate buffers for all packet headers.
324 struct tun_pi tun_targ;
325 char iphdr[sizeof(struct ip6_hdr)];
326 char transporthdr[MAX_TCP_HDR];
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900327 char icmp_iphdr[sizeof(struct ip6_hdr)];
328 char icmp_transporthdr[MAX_TCP_HDR];
Lorenzo Colittid9084182013-03-22 00:42:21 +0900329
330 // iovec of the packets we'll send. This gets passed down to the translation functions.
331 clat_packet out = {
332 { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
333 { iphdr, 0 }, // IP header.
334 { transporthdr, 0 }, // Transport layer header.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900335 { icmp_iphdr, 0 }, // ICMP error inner IP header.
336 { icmp_transporthdr, 0 }, // ICMP error transport layer header.
Lorenzo Colittid9084182013-03-22 00:42:21 +0900337 { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
338 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500339
340 if(tun_header->flags != 0) {
341 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
342 }
343
Lorenzo Colittid9084182013-03-22 00:42:21 +0900344 if(ntohs(tun_header->proto) == ETH_P_IP) {
345 fd = tunnel->fd6;
346 fill_tun_header(&tun_targ, ETH_P_IPV6);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900347 iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900348 } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
349 fd = tunnel->fd4;
350 fill_tun_header(&tun_targ, ETH_P_IP);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900351 iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Daniel Drowna45056e2012-03-23 10:42:54 -0500352 } else {
353 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
354 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900355
356 if (iov_len > 0) {
357 writev(fd, out, iov_len);
358 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500359}
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
Lorenzo Colittid9084182013-03-22 00:42:21 +0900390 packet_handler(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500391 }
392}
393
394/* function: event_loop
395 * reads packets from the tun network interface and passes them down the stack
396 * tunnel - tun device data
397 */
398void event_loop(const struct tun_data *tunnel) {
399 time_t last_interface_poll;
400 struct pollfd wait_fd[2];
401
402 // start the poll timer
403 last_interface_poll = time(NULL);
404
405 wait_fd[0].fd = tunnel->fd6;
406 wait_fd[0].events = POLLIN;
407 wait_fd[0].revents = 0;
408 wait_fd[1].fd = tunnel->fd4;
409 wait_fd[1].events = POLLIN;
410 wait_fd[1].revents = 0;
411
412 while(running) {
413 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
414 if(errno != EINTR) {
415 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
416 }
417 } else {
418 int i;
419 for(i = 0; i < 2; i++) {
420 if((wait_fd[i].revents & POLLIN) != 0) {
421 read_packet(wait_fd[i].fd,tunnel);
422 }
423 }
424 }
425
426 time_t now = time(NULL);
427 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
428 interface_poll(tunnel);
429 last_interface_poll = now;
430 }
431 }
432}
433
434/* function: print_help
435 * in case the user is running this on the command line
436 */
437void print_help() {
438 printf("android-clat arguments:\n");
439 printf("-i [uplink interface]\n");
440 printf("-p [plat prefix]\n");
441}
442
443/* function: main
444 * allocate and setup the tun device, then run the event loop
445 */
446int main(int argc, char **argv) {
447 struct tun_data tunnel;
448 int opt;
449 char *uplink_interface = NULL, *plat_prefix = NULL;
450
451 strcpy(tunnel.device6, DEVICENAME6);
452 strcpy(tunnel.device4, DEVICENAME4);
453
454 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
455 switch(opt) {
456 case 'i':
457 uplink_interface = optarg;
458 break;
459 case 'p':
460 plat_prefix = optarg;
461 break;
462 case 'h':
463 default:
464 print_help();
465 exit(1);
466 break;
467 }
468 }
469
470 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900471 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500472 printf("I need an interface\n");
473 exit(1);
474 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900475 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500476
477 // open the tunnel device before dropping privs
478 tunnel.fd6 = tun_open();
479 if(tunnel.fd6 < 0) {
JP Abgrallade83082013-12-20 14:51:26 -0800480 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500481 exit(1);
482 }
483
484 tunnel.fd4 = tun_open();
485 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900486 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500487 exit(1);
488 }
489
490 // open the forwarding configuration before dropping privs
491 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
492 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900493 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
494 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500495 exit(1);
496 }
497
Daniel Drowna45056e2012-03-23 10:42:54 -0500498 // run under a regular user
499 drop_root();
500
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900501 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
502 // "local", but that only works for the netd process itself.
503 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500504
505 configure_interface(uplink_interface, plat_prefix, &tunnel);
506
Daniel Drowna45056e2012-03-23 10:42:54 -0500507 set_forwarding(forwarding_fd,"1\n");
508
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900509 // Loop until someone sends us a signal or brings down the tun interface.
510 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
511 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
512 exit(1);
513 }
514 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500515
516 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900517 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500518
519 return 0;
520}