blob: b305ef310316d8e3b227aebf5bee35753fb560f3 [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>
Lorenzo Colittid9084182013-03-22 00:42:21 +090042#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050043#include <linux/prctl.h>
44#include <linux/if.h>
45#include <linux/if_tun.h>
46#include <linux/if_ether.h>
47
48#include <private/android_filesystem_config.h>
49
Lorenzo Colittid9084182013-03-22 00:42:21 +090050#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050051#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 Colitti9477a462013-11-18 15:56:02 +090085void stop_loop() {
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 */
Lorenzo Colittid9084182013-03-22 00:42:21 +0900309void packet_handler(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
310 size_t packetsize) {
311 int fd;
312 int iov_len = 0;
313
314 // Allocate buffers for all packet headers.
315 struct tun_pi tun_targ;
316 char iphdr[sizeof(struct ip6_hdr)];
317 char transporthdr[MAX_TCP_HDR];
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900318 char icmp_iphdr[sizeof(struct ip6_hdr)];
319 char icmp_transporthdr[MAX_TCP_HDR];
Lorenzo Colittid9084182013-03-22 00:42:21 +0900320
321 // iovec of the packets we'll send. This gets passed down to the translation functions.
322 clat_packet out = {
323 { &tun_targ, sizeof(tun_targ) }, // Tunnel header.
324 { iphdr, 0 }, // IP header.
325 { transporthdr, 0 }, // Transport layer header.
Lorenzo Colitticd70b352013-04-10 12:24:56 +0900326 { icmp_iphdr, 0 }, // ICMP error inner IP header.
327 { icmp_transporthdr, 0 }, // ICMP error transport layer header.
Lorenzo Colittid9084182013-03-22 00:42:21 +0900328 { NULL, 0 }, // Payload. No buffer, it's a pointer to the original payload.
329 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500330
331 if(tun_header->flags != 0) {
332 logmsg(ANDROID_LOG_WARN,"packet_handler: unexpected flags = %d", tun_header->flags);
333 }
334
Lorenzo Colittid9084182013-03-22 00:42:21 +0900335 if(ntohs(tun_header->proto) == ETH_P_IP) {
336 fd = tunnel->fd6;
337 fill_tun_header(&tun_targ, ETH_P_IPV6);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900338 iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Lorenzo Colittid9084182013-03-22 00:42:21 +0900339 } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
340 fd = tunnel->fd4;
341 fill_tun_header(&tun_targ, ETH_P_IP);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +0900342 iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
Daniel Drowna45056e2012-03-23 10:42:54 -0500343 } else {
344 logmsg(ANDROID_LOG_WARN,"packet_handler: unknown packet type = %x",tun_header->proto);
345 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900346
347 if (iov_len > 0) {
348 writev(fd, out, iov_len);
349 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500350}
351
352/* function: read_packet
353 * reads a packet from the tunnel fd and passes it down the stack
354 * active_fd - tun file descriptor marked ready for reading
355 * tunnel - tun device data
356 */
357void read_packet(int active_fd, const struct tun_data *tunnel) {
358 ssize_t readlen;
359 char packet[PACKETLEN];
360
361 // in case something ignores the packet length
362 memset(packet, 0, PACKETLEN);
363
364 readlen = read(active_fd,packet,PACKETLEN);
365
366 if(readlen < 0) {
367 logmsg(ANDROID_LOG_WARN,"read_packet/read error: %s", strerror(errno));
368 return;
369 } else if(readlen == 0) {
370 logmsg(ANDROID_LOG_WARN,"read_packet/tun interface removed");
371 running = 0;
372 } else {
373 struct tun_pi tun_header;
374 ssize_t header_size = sizeof(struct tun_pi);
375
376 if(readlen < header_size) {
377 logmsg(ANDROID_LOG_WARN,"read_packet/short read: got %ld bytes", readlen);
378 return;
379 }
380
Lorenzo Colittid9084182013-03-22 00:42:21 +0900381 packet_handler(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500382 }
383}
384
385/* function: event_loop
386 * reads packets from the tun network interface and passes them down the stack
387 * tunnel - tun device data
388 */
389void event_loop(const struct tun_data *tunnel) {
390 time_t last_interface_poll;
391 struct pollfd wait_fd[2];
392
393 // start the poll timer
394 last_interface_poll = time(NULL);
395
396 wait_fd[0].fd = tunnel->fd6;
397 wait_fd[0].events = POLLIN;
398 wait_fd[0].revents = 0;
399 wait_fd[1].fd = tunnel->fd4;
400 wait_fd[1].events = POLLIN;
401 wait_fd[1].revents = 0;
402
403 while(running) {
404 if(poll(wait_fd, 2, NO_TRAFFIC_INTERFACE_POLL_FREQUENCY*1000) == -1) {
405 if(errno != EINTR) {
406 logmsg(ANDROID_LOG_WARN,"event_loop/poll returned an error: %s",strerror(errno));
407 }
408 } else {
409 int i;
410 for(i = 0; i < 2; i++) {
411 if((wait_fd[i].revents & POLLIN) != 0) {
412 read_packet(wait_fd[i].fd,tunnel);
413 }
414 }
415 }
416
417 time_t now = time(NULL);
418 if(last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
419 interface_poll(tunnel);
420 last_interface_poll = now;
421 }
422 }
423}
424
425/* function: print_help
426 * in case the user is running this on the command line
427 */
428void print_help() {
429 printf("android-clat arguments:\n");
430 printf("-i [uplink interface]\n");
431 printf("-p [plat prefix]\n");
432}
433
434/* function: main
435 * allocate and setup the tun device, then run the event loop
436 */
437int main(int argc, char **argv) {
438 struct tun_data tunnel;
439 int opt;
440 char *uplink_interface = NULL, *plat_prefix = NULL;
441
442 strcpy(tunnel.device6, DEVICENAME6);
443 strcpy(tunnel.device4, DEVICENAME4);
444
445 while((opt = getopt(argc, argv, "i:p:h")) != -1) {
446 switch(opt) {
447 case 'i':
448 uplink_interface = optarg;
449 break;
450 case 'p':
451 plat_prefix = optarg;
452 break;
453 case 'h':
454 default:
455 print_help();
456 exit(1);
457 break;
458 }
459 }
460
461 if(uplink_interface == NULL) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900462 logmsg(ANDROID_LOG_FATAL, "clatd called without an interface");
Daniel Drowna45056e2012-03-23 10:42:54 -0500463 printf("I need an interface\n");
464 exit(1);
465 }
Lorenzo Colittid9084182013-03-22 00:42:21 +0900466 logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500467
468 // open the tunnel device before dropping privs
469 tunnel.fd6 = tun_open();
470 if(tunnel.fd6 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900471 logmsg(ANDROID_LOG_FATAL, "tun_open failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500472 exit(1);
473 }
474
475 tunnel.fd4 = tun_open();
476 if(tunnel.fd4 < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900477 logmsg(ANDROID_LOG_FATAL, "tun_open4 failed: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500478 exit(1);
479 }
480
481 // open the forwarding configuration before dropping privs
482 forwarding_fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDWR);
483 if(forwarding_fd < 0) {
Lorenzo Colittid9084182013-03-22 00:42:21 +0900484 logmsg(ANDROID_LOG_FATAL,"open /proc/sys/net/ipv6/conf/all/forwarding failed: %s",
485 strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500486 exit(1);
487 }
488
Daniel Drowna45056e2012-03-23 10:42:54 -0500489 // run under a regular user
490 drop_root();
491
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900492 // When run from netd, the environment variable ANDROID_DNS_MODE is set to
493 // "local", but that only works for the netd process itself.
494 unsetenv("ANDROID_DNS_MODE");
Daniel Drowna45056e2012-03-23 10:42:54 -0500495
496 configure_interface(uplink_interface, plat_prefix, &tunnel);
497
Daniel Drowna45056e2012-03-23 10:42:54 -0500498 set_forwarding(forwarding_fd,"1\n");
499
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900500 // Loop until someone sends us a signal or brings down the tun interface.
501 if(signal(SIGTERM, stop_loop) == SIG_ERR) {
502 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
503 exit(1);
504 }
505 event_loop(&tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500506
507 set_forwarding(forwarding_fd,"0\n");
Lorenzo Colittibaf62992013-03-01 20:29:39 +0900508 logmsg(ANDROID_LOG_INFO,"Shutting down clat on %s", uplink_interface);
Daniel Drowna45056e2012-03-23 10:42:54 -0500509
510 return 0;
511}