blob: f826d466fdc2d9a9fba7d976c1dd2837a61fa435 [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) {
195 struct in_addr default_4;
196 int status;
197
198 default_4.s_addr = INADDR_ANY;
199
200 if((status = if_up(tunnel->device6, Global_Clatd_Config.mtu)) < 0) {
201 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(6) failed: %s",strerror(-status));
202 exit(1);
203 }
204 if((status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu)) < 0) {
205 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_up(4) failed: %s",strerror(-status));
206 exit(1);
207 }
208 status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet,
209 32, &Global_Clatd_Config.ipv4_local_subnet);
210 if(status < 0) {
211 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_address(4) failed: %s",strerror(-status));
212 exit(1);
213 }
214
215 configure_tun_ipv6(tunnel);
216
217 /* setup default ipv4 route */
218 status = if_route(tunnel->device4, AF_INET, &default_4, 0, NULL, 1, 0, ROUTE_REPLACE);
219 if(status < 0) {
220 logmsg(ANDROID_LOG_FATAL,"configure_tun_ip/if_route failed: %s",strerror(-status));
221 exit(1);
222 }
223}
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 */
315void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet, size_t packetsize) {
316 tun_header->proto = ntohs(tun_header->proto);
317
318 if(tun_header->flags != 0) {
319 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
320 }
321
322 if(tun_header->proto == ETH_P_IP) {
323 ip_packet(tunnel->fd6,packet,packetsize);
324 } else if(tun_header->proto == ETH_P_IPV6) {
325 ipv6_packet(tunnel->fd4,packet,packetsize);
326 } else {
327 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
328 }
329}
330
331/* function: read_packet
332 * reads a packet from the tunnel fd and passes it down the stack
333 * active_fd - tun file descriptor marked ready for reading
334 * tunnel - tun device data
335 */
336void read_packet(int active_fd, const struct tun_data *tunnel) {
337 ssize_t readlen;
338 char packet[PACKETLEN];
339
340 // in case something ignores the packet length
341 memset(packet, 0, PACKETLEN);
342
343 readlen = read(active_fd,packet,PACKETLEN);
344
345 if(readlen < 0) {
346 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
347 return;
348 } else if(readlen == 0) {
349 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
350 running = 0;
351 } else {
352 struct tun_pi tun_header;
353 ssize_t header_size = sizeof(struct tun_pi);
354
355 if(readlen < header_size) {
356 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
357 return;
358 }
359
360 memcpy(&tun_header, packet, header_size);
361
362 packet_handler(tunnel, &tun_header, packet+header_size, readlen-header_size);
363 }
364}
365
366/* function: event_loop
367 * reads packets from the tun network interface and passes them down the stack
368 * tunnel - tun device data
369 */
370void event_loop(const struct tun_data *tunnel) {
371 time_t last_interface_poll;
372 struct pollfd wait_fd[2];
373
374 // start the poll timer
375 last_interface_poll = time(NULL);
376
377 wait_fd[0].fd = tunnel->fd6;
378 wait_fd[0].events = POLLIN;
379 wait_fd[0].revents = 0;
380 wait_fd[1].fd = tunnel->fd4;
381 wait_fd[1].events = POLLIN;
382 wait_fd[1].revents = 0;
383
384 while(running) {
385 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
386 if(errno != EINTR) {
387 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
388 }
389 } else {
390 int i;
391 for(i = 0; i < 2; i++) {
392 if((wait_fd[i].revents & POLLIN) != 0) {
393 read_packet(wait_fd[i].fd,tunnel);
394 }
395 }
396 }
397
398 time_t now = time(NULL);
399 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
400 interface_poll(tunnel);
401 last_interface_poll = now;
402 }
403 }
404}
405
406/* function: print_help
407 * in case the user is running this on the command line
408 */
409void print_help() {
410 printf("android-clat arguments:\n");
411 printf("-i [uplink interface]\n");
412 printf("-p [plat prefix]\n");
413}
414
415/* function: main
416 * allocate and setup the tun device, then run the event loop
417 */
418int main(int argc, char **argv) {
419 struct tun_data tunnel;
420 int opt;
421 char *uplink_interface = NULL, *plat_prefix = NULL;
422
423 strcpy(tunnel.device6, DEVICENAME6);
424 strcpy(tunnel.device4, DEVICENAME4);
425
426 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
427 switch(opt) {
428 case 'i':
429 uplink_interface = optarg;
430 break;
431 case 'p':
432 plat_prefix = optarg;
433 break;
434 case 'h':
435 default:
436 print_help();
437 exit(1);
438 break;
439 }
440 }
441
442 if(uplink_interface == NULL) {
443 logmsg(ANDROID_LOG_FATAL,"clatd called without an interface");
444 printf("I need an interface\n");
445 exit(1);
446 }
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900447 logmsg(ANDROID_LOG_INFO,"Starting clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500448
449 // open the tunnel device before dropping privs
450 tunnel.fd6 = tun_open();
451 if(tunnel.fd6 < 0) {
452 logmsg(ANDROID_LOG_FATAL,"tun_open failed: %s",strerror(errno));
453 exit(1);
454 }
455
456 tunnel.fd4 = tun_open();
457 if(tunnel.fd4 < 0) {
458 logmsg(ANDROID_LOG_FATAL,"tun_open4 failed: %s",strerror(errno));
459 exit(1);
460 }
461
462 // open the forwarding configuration before dropping privs
463 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
464 if(forwarding_fd < 0) {
465 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",strerror(errno));
466 exit(1);
467 }
468
Daniel Drowna45056e2012-03-23 10:42:54 -0500469 // run under a regular user
470 drop_root();
471
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900472 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
473 // "local", but that only works for the netd process itself.
474 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500475
476 configure_interface(uplink_interface, plat_prefix, &tunnel);
477
Daniel Drowna45056e2012-03-23 10:42:54 -0500478 set_forwarding(forwarding_fd,"1\n");
479
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900480 // Loop until someone sends us a signal or brings down the tun interface.
481 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
482 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
483 exit(1);
484 }
485 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500486
487 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900488 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500489
490 return 0;
491}