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