blob: be7d0c64e4f65740cfc3a080601bc7fcf6feea37 [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
Lorenzo Colittice140882014-06-02 21:20:40 +0900251/* function: open_raw_socket
252 * opens the raw socket for sending IPv6 packets
253 */
254void open_raw_socket(struct tun_data *tunnel) {
255 int rawsock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
256 if (rawsock < 0) {
257 logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno));
258 exit(1);
259 }
260
261 int off = 0;
262 if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) {
263 logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno));
264 }
265
266 tunnel->write_fd6 = rawsock;
267}
268
Daniel Drowna45056e2012-03-23 10:42:54 -0500269/* function: configure_interface
270 * reads the configuration and applies it to the interface
271 * uplink_interface - network interface to use to reach the ipv6 internet
272 * plat_prefix - PLAT prefix to use
273 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400274 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500275 */
Paul Jensen247dd712014-05-30 13:19:10 -0400276void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500277 int error;
278
Paul Jensen247dd712014-05-30 13:19:10 -0400279 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500280 logmsg(ANDROID_LOG_FATAL,"read_config failed");
281 exit(1);
282 }
283
284 if(Global_Clatd_Config.mtu > MAXMTU) {
285 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
286 Global_Clatd_Config.mtu = MAXMTU;
287 }
288 if(Global_Clatd_Config.mtu <= 0) {
289 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
290 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
291 }
292 if(Global_Clatd_Config.mtu < 1280) {
293 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
294 Global_Clatd_Config.mtu = 1280;
295 }
296
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900297 if(Global_Clatd_Config.ipv4mtu <= 0 ||
298 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
299 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500300 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
301 }
302
Lorenzo Colittice140882014-06-02 21:20:40 +0900303 error = tun_alloc(tunnel->device6, tunnel->read_fd6);
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 if(error < 0) {
305 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
306 exit(1);
307 }
308
309 error = tun_alloc(tunnel->device4, tunnel->fd4);
310 if(error < 0) {
311 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
312 exit(1);
313 }
314
315 configure_tun_ip(tunnel);
316}
317
Daniel Drowna45056e2012-03-23 10:42:54 -0500318/* function: read_packet
319 * reads a packet from the tunnel fd and passes it down the stack
320 * active_fd - tun file descriptor marked ready for reading
321 * tunnel - tun device data
322 */
323void read_packet(int active_fd, const struct tun_data *tunnel) {
324 ssize_t readlen;
Brian Carlstromfcac4102014-02-24 20:03:01 -0800325 uint8_t packet[PACKETLEN];
Daniel Drowna45056e2012-03-23 10:42:54 -0500326
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900327 // In case something ignores the packet length.
328 // TODO: remove it.
Daniel Drowna45056e2012-03-23 10:42:54 -0500329 memset(packet, 0, PACKETLEN);
330
331 readlen = read(active_fd,packet,PACKETLEN);
332
333 if(readlen < 0) {
334 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
335 return;
336 } else if(readlen == 0) {
337 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
338 running = 0;
339 } else {
Daniel Drowna45056e2012-03-23 10:42:54 -0500340 ssize_t header_size = sizeof(struct tun_pi);
341
342 if(readlen < header_size) {
343 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
344 return;
345 }
346
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900347 struct tun_pi *tun_header = (struct tun_pi *) packet;
348 if(tun_header->flags != 0) {
349 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
350 }
351
352 int fd;
353 uint16_t proto = ntohs(tun_header->proto);
354 if (proto == ETH_P_IP) {
Lorenzo Colittice140882014-06-02 21:20:40 +0900355 fd = tunnel->write_fd6;
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900356 } else if (proto == ETH_P_IPV6) {
357 fd = tunnel->fd4;
358 } else {
359 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
360 return;
361 }
362
363 translate_packet(fd, (proto == ETH_P_IP), packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500364 }
365}
366
367/* function: event_loop
368 * reads packets from the tun network interface and passes them down the stack
369 * tunnel - tun device data
370 */
371void event_loop(const struct tun_data *tunnel) {
372 time_t last_interface_poll;
373 struct pollfd wait_fd[2];
374
375 // start the poll timer
376 last_interface_poll = time(NULL);
377
Lorenzo Colittice140882014-06-02 21:20:40 +0900378 wait_fd[0].fd = tunnel->read_fd6;
Daniel Drowna45056e2012-03-23 10:42:54 -0500379 wait_fd[0].events = POLLIN;
380 wait_fd[0].revents = 0;
381 wait_fd[1].fd = tunnel->fd4;
382 wait_fd[1].events = POLLIN;
383 wait_fd[1].revents = 0;
384
385 while(running) {
386 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
387 if(errno != EINTR) {
388 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
389 }
390 } else {
391 int i;
392 for(i = 0; i < 2; i++) {
393 if((wait_fd[i].revents & POLLIN) != 0) {
394 read_packet(wait_fd[i].fd,tunnel);
395 }
396 }
397 }
398
399 time_t now = time(NULL);
400 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
401 interface_poll(tunnel);
402 last_interface_poll = now;
403 }
404 }
405}
406
407/* function: print_help
408 * in case the user is running this on the command line
409 */
410void print_help() {
411 printf("android-clat arguments:\n");
412 printf("-i [uplink interface]\n");
413 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400414 printf("-n [NetId]\n");
Daniel Drowna45056e2012-03-23 10:42:54 -0500415}
416
417/* function: main
418 * allocate and setup the tun device, then run the event loop
419 */
420int main(int argc, char **argv) {
421 struct tun_data tunnel;
422 int opt;
Paul Jensen247dd712014-05-30 13:19:10 -0400423 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL;
424 unsigned net_id = NETID_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500425
426 strcpy(tunnel.device6, DEVICENAME6);
427 strcpy(tunnel.device4, DEVICENAME4);
428
Paul Jensen247dd712014-05-30 13:19:10 -0400429 while((opt = getopt(argc, argv, "i:p:n:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500430 switch(opt) {
431 case 'i':
432 uplink_interface = optarg;
433 break;
434 case 'p':
435 plat_prefix = optarg;
436 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400437 case 'n':
438 net_id_str = optarg;
439 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500440 case 'h':
441 default:
442 print_help();
443 exit(1);
444 break;
445 }
446 }
447
448 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900449 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500450 printf("I need an interface\n");
451 exit(1);
452 }
Paul Jensen247dd712014-05-30 13:19:10 -0400453 if (net_id_str != NULL) {
454 char *end_ptr;
455 net_id = strtoul(net_id_str, &end_ptr, 0);
Paul Jensen464fb262014-06-03 14:41:57 -0400456 if (*net_id_str == 0 || *end_ptr != 0) {
Paul Jensen247dd712014-05-30 13:19:10 -0400457 logmsg(ANDROID_LOG_FATAL, "clatd called with invalid NetID %s", net_id_str);
458 exit(1);
459 }
460 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900461 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500462
463 // open the tunnel device before dropping privs
Lorenzo Colittice140882014-06-02 21:20:40 +0900464 tunnel.read_fd6 = tun_open();
465 if(tunnel.read_fd6 < 0) {
JP Abgrall4e0dd832013-12-20 14:51:26 -0800466 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500467 exit(1);
468 }
469
470 tunnel.fd4 = tun_open();
471 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900472 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500473 exit(1);
474 }
475
476 // open the forwarding configuration before dropping privs
477 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
478 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900479 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
480 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500481 exit(1);
482 }
483
Lorenzo Colittice140882014-06-02 21:20:40 +0900484 open_raw_socket(&tunnel);
485
Daniel Drowna45056e2012-03-23 10:42:54 -0500486 // run under a regular user
487 drop_root();
488
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900489 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
490 // "local", but that only works for the netd process itself.
491 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500492
Paul Jensen247dd712014-05-30 13:19:10 -0400493 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500494
Daniel Drowna45056e2012-03-23 10:42:54 -0500495 set_forwarding(forwarding_fd,"1\n");
496
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900497 // Loop until someone sends us a signal or brings down the tun interface.
498 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
499 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
500 exit(1);
501 }
502 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500503
504 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900505 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500506
507 return 0;
508}