blob: af54ff26c4d1a5ebad3890147f290a92679b1d09 [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
251/* function: configure_interface
252 * reads the configuration and applies it to the interface
253 * uplink_interface - network interface to use to reach the ipv6 internet
254 * plat_prefix - PLAT prefix to use
255 * tunnel - tun device data
Paul Jensen247dd712014-05-30 13:19:10 -0400256 * net_id - NetID to use, NETID_UNSET indicates use of default network
Daniel Drowna45056e2012-03-23 10:42:54 -0500257 */
Paul Jensen247dd712014-05-30 13:19:10 -0400258void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500259 int error;
260
Paul Jensen247dd712014-05-30 13:19:10 -0400261 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500262 logmsg(ANDROID_LOG_FATAL,"read_config failed");
263 exit(1);
264 }
265
266 if(Global_Clatd_Config.mtu > MAXMTU) {
267 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
268 Global_Clatd_Config.mtu = MAXMTU;
269 }
270 if(Global_Clatd_Config.mtu <= 0) {
271 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
272 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
273 }
274 if(Global_Clatd_Config.mtu < 1280) {
275 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
276 Global_Clatd_Config.mtu = 1280;
277 }
278
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900279 if(Global_Clatd_Config.ipv4mtu <= 0 ||
280 Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) {
281 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA;
Daniel Drowna45056e2012-03-23 10:42:54 -0500282 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
283 }
284
285 error = tun_alloc(tunnel->device6, tunnel->fd6);
286 if(error < 0) {
287 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
288 exit(1);
289 }
290
291 error = tun_alloc(tunnel->device4, tunnel->fd4);
292 if(error < 0) {
293 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
294 exit(1);
295 }
296
297 configure_tun_ip(tunnel);
298}
299
Daniel Drowna45056e2012-03-23 10:42:54 -0500300/* function: read_packet
301 * reads a packet from the tunnel fd and passes it down the stack
302 * active_fd - tun file descriptor marked ready for reading
303 * tunnel - tun device data
304 */
305void read_packet(int active_fd, const struct tun_data *tunnel) {
306 ssize_t readlen;
Brian Carlstromfcac4102014-02-24 20:03:01 -0800307 uint8_t packet[PACKETLEN];
Daniel Drowna45056e2012-03-23 10:42:54 -0500308
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900309 // In case something ignores the packet length.
310 // TODO: remove it.
Daniel Drowna45056e2012-03-23 10:42:54 -0500311 memset(packet, 0, PACKETLEN);
312
313 readlen = read(active_fd,packet,PACKETLEN);
314
315 if(readlen < 0) {
316 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
317 return;
318 } else if(readlen == 0) {
319 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
320 running = 0;
321 } else {
Daniel Drowna45056e2012-03-23 10:42:54 -0500322 ssize_t header_size = sizeof(struct tun_pi);
323
324 if(readlen < header_size) {
325 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
326 return;
327 }
328
Lorenzo Colittie24982e2014-06-02 15:49:36 +0900329 struct tun_pi *tun_header = (struct tun_pi *) packet;
330 if(tun_header->flags != 0) {
331 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
332 }
333
334 int fd;
335 uint16_t proto = ntohs(tun_header->proto);
336 if (proto == ETH_P_IP) {
337 fd = tunnel->fd6;
338 } else if (proto == ETH_P_IPV6) {
339 fd = tunnel->fd4;
340 } else {
341 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
342 return;
343 }
344
345 translate_packet(fd, (proto == ETH_P_IP), packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500346 }
347}
348
349/* function: event_loop
350 * reads packets from the tun network interface and passes them down the stack
351 * tunnel - tun device data
352 */
353void event_loop(const struct tun_data *tunnel) {
354 time_t last_interface_poll;
355 struct pollfd wait_fd[2];
356
357 // start the poll timer
358 last_interface_poll = time(NULL);
359
360 wait_fd[0].fd = tunnel->fd6;
361 wait_fd[0].events = POLLIN;
362 wait_fd[0].revents = 0;
363 wait_fd[1].fd = tunnel->fd4;
364 wait_fd[1].events = POLLIN;
365 wait_fd[1].revents = 0;
366
367 while(running) {
368 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
369 if(errno != EINTR) {
370 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
371 }
372 } else {
373 int i;
374 for(i = 0; i < 2; i++) {
375 if((wait_fd[i].revents & POLLIN) != 0) {
376 read_packet(wait_fd[i].fd,tunnel);
377 }
378 }
379 }
380
381 time_t now = time(NULL);
382 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
383 interface_poll(tunnel);
384 last_interface_poll = now;
385 }
386 }
387}
388
389/* function: print_help
390 * in case the user is running this on the command line
391 */
392void print_help() {
393 printf("android-clat arguments:\n");
394 printf("-i [uplink interface]\n");
395 printf("-p [plat prefix]\n");
Paul Jensen247dd712014-05-30 13:19:10 -0400396 printf("-n [NetId]\n");
Daniel Drowna45056e2012-03-23 10:42:54 -0500397}
398
399/* function: main
400 * allocate and setup the tun device, then run the event loop
401 */
402int main(int argc, char **argv) {
403 struct tun_data tunnel;
404 int opt;
Paul Jensen247dd712014-05-30 13:19:10 -0400405 char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL;
406 unsigned net_id = NETID_UNSET;
Daniel Drowna45056e2012-03-23 10:42:54 -0500407
408 strcpy(tunnel.device6, DEVICENAME6);
409 strcpy(tunnel.device4, DEVICENAME4);
410
Paul Jensen247dd712014-05-30 13:19:10 -0400411 while((opt = getopt(argc, argv, "i:p:n:h")) != -1) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500412 switch(opt) {
413 case 'i':
414 uplink_interface = optarg;
415 break;
416 case 'p':
417 plat_prefix = optarg;
418 break;
Paul Jensen247dd712014-05-30 13:19:10 -0400419 case 'n':
420 net_id_str = optarg;
421 break;
Daniel Drowna45056e2012-03-23 10:42:54 -0500422 case 'h':
423 default:
424 print_help();
425 exit(1);
426 break;
427 }
428 }
429
430 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900431 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500432 printf("I need an interface\n");
433 exit(1);
434 }
Paul Jensen247dd712014-05-30 13:19:10 -0400435 if (net_id_str != NULL) {
436 char *end_ptr;
437 net_id = strtoul(net_id_str, &end_ptr, 0);
Paul Jensen464fb262014-06-03 14:41:57 -0400438 if (*net_id_str == 0 || *end_ptr != 0) {
Paul Jensen247dd712014-05-30 13:19:10 -0400439 logmsg(ANDROID_LOG_FATAL, "clatd called with invalid NetID %s", net_id_str);
440 exit(1);
441 }
442 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900443 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500444
445 // open the tunnel device before dropping privs
446 tunnel.fd6 = tun_open();
447 if(tunnel.fd6 < 0) {
JP Abgrall4e0dd832013-12-20 14:51:26 -0800448 logmsg(ANDROID_LOG_FATAL, "tun_open6 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500449 exit(1);
450 }
451
452 tunnel.fd4 = tun_open();
453 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900454 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500455 exit(1);
456 }
457
458 // open the forwarding configuration before dropping privs
459 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
460 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900461 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
462 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 exit(1);
464 }
465
Daniel Drowna45056e2012-03-23 10:42:54 -0500466 // run under a regular user
467 drop_root();
468
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900469 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
470 // "local", but that only works for the netd process itself.
471 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500472
Paul Jensen247dd712014-05-30 13:19:10 -0400473 configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
Daniel Drowna45056e2012-03-23 10:42:54 -0500474
Daniel Drowna45056e2012-03-23 10:42:54 -0500475 set_forwarding(forwarding_fd,"1\n");
476
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900477 // Loop until someone sends us a signal or brings down the tun interface.
478 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
479 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
480 exit(1);
481 }
482 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500483
484 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900485 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500486
487 return 0;
488}