blob: edd8a2def04c691ac93c1733b432d24eda45316d [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>
Daniel Drowna45056e2012-03-23 10:42:54 -050042#include <linux/prctl.h>
43#include <linux/if.h>
44#include <linux/if_tun.h>
45#include <linux/if_ether.h>
46
47#include <private/android_filesystem_config.h>
48
49#include "ipv4.h"
50#include "ipv6.h"
51#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 Colittibaf62992013-03-01 20:29:39 +090085void stop_loop(int signal) {
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
206 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
207 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
208 exit(1);
209 }
Lorenzo Colitti3ca03022013-03-27 12:39:10 +0900210
Daniel Drowna45056e2012-03-23 10:42:54 -0500211 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
212 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
213 exit(1);
214 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500215
216 configure_tun_ipv6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500217}
218
219/* function: drop_root
220 * drops root privs but keeps the needed capability
221 */
222void drop_root() {
223 gid_t groups[] = { AID_INET };
224 if(setgroups(sizeof(groups)/sizeof(groups[0]), groups) < 0) {
225 logmsg(ANDROID_LOG_FATAL,"drop_root/setgroups failed: %s",strerror(errno));
226 exit(1);
227 }
228
229 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
230
231 if(setgid(AID_CLAT) < 0) {
232 logmsg(ANDROID_LOG_FATAL,"drop_root/setgid failed: %s",strerror(errno));
233 exit(1);
234 }
235 if(setuid(AID_CLAT) < 0) {
236 logmsg(ANDROID_LOG_FATAL,"drop_root/setuid failed: %s",strerror(errno));
237 exit(1);
238 }
239
240 struct __user_cap_header_struct header;
241 struct __user_cap_data_struct cap;
242 memset(&header, 0, sizeof(header));
243 memset(&cap, 0, sizeof(cap));
244
245 header.version = _LINUX_CAPABILITY_VERSION;
246 header.pid = 0; // 0 = change myself
247 cap.effective = cap.permitted = (1 << CAP_NET_ADMIN);
248
249 if(capset(&header, &cap) < 0) {
250 logmsg(ANDROID_LOG_FATAL,"drop_root/capset failed: %s",strerror(errno));
251 exit(1);
252 }
253}
254
255/* function: configure_interface
256 * reads the configuration and applies it to the interface
257 * uplink_interface - network interface to use to reach the ipv6 internet
258 * plat_prefix - PLAT prefix to use
259 * tunnel - tun device data
260 */
261void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
262 int error;
263
264 if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
265 logmsg(ANDROID_LOG_FATAL,"read_config failed");
266 exit(1);
267 }
268
269 if(Global_Clatd_Config.mtu > MAXMTU) {
270 logmsg(ANDROID_LOG_WARN,"Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu);
271 Global_Clatd_Config.mtu = MAXMTU;
272 }
273 if(Global_Clatd_Config.mtu <= 0) {
274 Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface);
275 logmsg(ANDROID_LOG_WARN,"ifmtu=%d",Global_Clatd_Config.mtu);
276 }
277 if(Global_Clatd_Config.mtu < 1280) {
278 logmsg(ANDROID_LOG_WARN,"mtu too small = %d", Global_Clatd_Config.mtu);
279 Global_Clatd_Config.mtu = 1280;
280 }
281
282 if(Global_Clatd_Config.ipv4mtu <= 0 || (Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - 20)) {
283 Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu-20;
284 logmsg(ANDROID_LOG_WARN,"ipv4mtu now set to = %d",Global_Clatd_Config.ipv4mtu);
285 }
286
287 error = tun_alloc(tunnel->device6, tunnel->fd6);
288 if(error < 0) {
289 logmsg(ANDROID_LOG_FATAL,"tun_alloc failed: %s",strerror(errno));
290 exit(1);
291 }
292
293 error = tun_alloc(tunnel->device4, tunnel->fd4);
294 if(error < 0) {
295 logmsg(ANDROID_LOG_FATAL,"tun_alloc/4 failed: %s",strerror(errno));
296 exit(1);
297 }
298
299 configure_tun_ip(tunnel);
300}
301
302/* function: packet_handler
303 * takes a tun header and a packet and sends it down the stack
304 * tunnel - tun device data
305 * tun_header - tun header
306 * packet - packet
307 * packetsize - size of packet
308 */
309void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet, size_t packetsize) {
310 tun_header->proto = ntohs(tun_header->proto);
311
312 if(tun_header->flags != 0) {
313 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
314 }
315
316 if(tun_header->proto == ETH_P_IP) {
317 ip_packet(tunnel->fd6,packet,packetsize);
318 } else if(tun_header->proto == ETH_P_IPV6) {
319 ipv6_packet(tunnel->fd4,packet,packetsize);
320 } else {
321 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
322 }
323}
324
325/* function: read_packet
326 * reads a packet from the tunnel fd and passes it down the stack
327 * active_fd - tun file descriptor marked ready for reading
328 * tunnel - tun device data
329 */
330void read_packet(int active_fd, const struct tun_data *tunnel) {
331 ssize_t readlen;
332 char packet[PACKETLEN];
333
334 // in case something ignores the packet length
335 memset(packet, 0, PACKETLEN);
336
337 readlen = read(active_fd,packet,PACKETLEN);
338
339 if(readlen < 0) {
340 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
341 return;
342 } else if(readlen == 0) {
343 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
344 running = 0;
345 } else {
346 struct tun_pi tun_header;
347 ssize_t header_size = sizeof(struct tun_pi);
348
349 if(readlen < header_size) {
350 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
351 return;
352 }
353
354 memcpy(&tun_header, packet, header_size);
355
356 packet_handler(tunnel, &tun_header, packet+header_size, readlen-header_size);
357 }
358}
359
360/* function: event_loop
361 * reads packets from the tun network interface and passes them down the stack
362 * tunnel - tun device data
363 */
364void event_loop(const struct tun_data *tunnel) {
365 time_t last_interface_poll;
366 struct pollfd wait_fd[2];
367
368 // start the poll timer
369 last_interface_poll = time(NULL);
370
371 wait_fd[0].fd = tunnel->fd6;
372 wait_fd[0].events = POLLIN;
373 wait_fd[0].revents = 0;
374 wait_fd[1].fd = tunnel->fd4;
375 wait_fd[1].events = POLLIN;
376 wait_fd[1].revents = 0;
377
378 while(running) {
379 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
380 if(errno != EINTR) {
381 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
382 }
383 } else {
384 int i;
385 for(i = 0; i < 2; i++) {
386 if((wait_fd[i].revents & POLLIN) != 0) {
387 read_packet(wait_fd[i].fd,tunnel);
388 }
389 }
390 }
391
392 time_t now = time(NULL);
393 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
394 interface_poll(tunnel);
395 last_interface_poll = now;
396 }
397 }
398}
399
400/* function: print_help
401 * in case the user is running this on the command line
402 */
403void print_help() {
404 printf("android-clat arguments:\n");
405 printf("-i [uplink interface]\n");
406 printf("-p [plat prefix]\n");
407}
408
409/* function: main
410 * allocate and setup the tun device, then run the event loop
411 */
412int main(int argc, char **argv) {
413 struct tun_data tunnel;
414 int opt;
415 char *uplink_interface = NULL, *plat_prefix = NULL;
416
417 strcpy(tunnel.device6, DEVICENAME6);
418 strcpy(tunnel.device4, DEVICENAME4);
419
420 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
421 switch(opt) {
422 case 'i':
423 uplink_interface = optarg;
424 break;
425 case 'p':
426 plat_prefix = optarg;
427 break;
428 case 'h':
429 default:
430 print_help();
431 exit(1);
432 break;
433 }
434 }
435
436 if(uplink_interface == NULL) {
437 logmsg(ANDROID_LOG_FATAL,"clatd called without an interface");
438 printf("I need an interface\n");
439 exit(1);
440 }
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900441 logmsg(ANDROID_LOG_INFO,"Starting clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500442
443 // open the tunnel device before dropping privs
444 tunnel.fd6 = tun_open();
445 if(tunnel.fd6 < 0) {
446 logmsg(ANDROID_LOG_FATAL,"tun_open failed: %s",strerror(errno));
447 exit(1);
448 }
449
450 tunnel.fd4 = tun_open();
451 if(tunnel.fd4 < 0) {
452 logmsg(ANDROID_LOG_FATAL,"tun_open4 failed: %s",strerror(errno));
453 exit(1);
454 }
455
456 // open the forwarding configuration before dropping privs
457 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
458 if(forwarding_fd < 0) {
459 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",strerror(errno));
460 exit(1);
461 }
462
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 // run under a regular user
464 drop_root();
465
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900466 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
467 // "local", but that only works for the netd process itself.
468 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500469
470 configure_interface(uplink_interface, plat_prefix, &tunnel);
471
Daniel Drowna45056e2012-03-23 10:42:54 -0500472 set_forwarding(forwarding_fd,"1\n");
473
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900474 // Loop until someone sends us a signal or brings down the tun interface.
475 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
476 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
477 exit(1);
478 }
479 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500480
481 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900482 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500483
484 return 0;
485}