• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <arpa/inet.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <poll.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/prctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <time.h>
31 #include <unistd.h>
32 
33 #include <linux/filter.h>
34 #include <linux/if.h>
35 #include <linux/if_ether.h>
36 #include <linux/if_packet.h>
37 #include <linux/if_tun.h>
38 #include <net/if.h>
39 #include <sys/capability.h>
40 #include <sys/uio.h>
41 
42 #include "clatd.h"
43 #include "config.h"
44 #include "dump.h"
45 #include "getaddr.h"
46 #include "logging.h"
47 #include "translate.h"
48 
49 struct clat_config Global_Clatd_Config;
50 
51 /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
52 #define MTU_DELTA 28
53 
54 volatile sig_atomic_t running = 1;
55 
ipv6_address_changed(const char * interface)56 int ipv6_address_changed(const char *interface) {
57   union anyip *interface_ip;
58 
59   interface_ip = getinterface_ip(interface, AF_INET6);
60   if (!interface_ip) {
61     logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
62     return 1;
63   }
64 
65   if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
66     char oldstr[INET6_ADDRSTRLEN];
67     char newstr[INET6_ADDRSTRLEN];
68     inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
69     inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
70     logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
71     free(interface_ip);
72     return 1;
73   } else {
74     free(interface_ip);
75     return 0;
76   }
77 }
78 
79 /* function: read_packet
80  * reads a packet from the tunnel fd and translates it
81  *   read_fd  - file descriptor to read original packet from
82  *   write_fd - file descriptor to write translated packet to
83  *   to_ipv6  - whether the packet is to be translated to ipv6 or ipv4
84  */
read_packet(int read_fd,int write_fd,int to_ipv6)85 void read_packet(int read_fd, int write_fd, int to_ipv6) {
86   uint8_t buf[PACKETLEN];
87   ssize_t readlen = read(read_fd, buf, PACKETLEN);
88 
89   if (readlen < 0) {
90     if (errno != EAGAIN) {
91       logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
92     }
93     return;
94   } else if (readlen == 0) {
95     logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
96     running = 0;
97     return;
98   }
99 
100   if (!to_ipv6) {
101     translate_packet(write_fd, 0 /* to_ipv6 */, buf, readlen);
102     return;
103   }
104 
105   struct tun_pi *tun_header = (struct tun_pi *)buf;
106   if (readlen < (ssize_t)sizeof(*tun_header)) {
107     logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
108     return;
109   }
110 
111   uint16_t proto = ntohs(tun_header->proto);
112   if (proto != ETH_P_IP) {
113     logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
114     return;
115   }
116 
117   if (tun_header->flags != 0) {
118     logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
119   }
120 
121   uint8_t *packet = (uint8_t *)(tun_header + 1);
122   readlen -= sizeof(*tun_header);
123   translate_packet(write_fd, 1 /* to_ipv6 */, packet, readlen);
124 }
125 
126 /* function: event_loop
127  * reads packets from the tun network interface and passes them down the stack
128  *   tunnel - tun device data
129  */
event_loop(struct tun_data * tunnel)130 void event_loop(struct tun_data *tunnel) {
131   time_t last_interface_poll;
132   struct pollfd wait_fd[] = {
133     { tunnel->read_fd6, POLLIN, 0 },
134     { tunnel->fd4, POLLIN, 0 },
135   };
136 
137   // start the poll timer
138   last_interface_poll = time(NULL);
139 
140   while (running) {
141     if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
142       if (errno != EINTR) {
143         logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
144       }
145     } else {
146       // Call read_packet if the socket has data to be read, but also if an
147       // error is waiting. If we don't call read() after getting POLLERR, a
148       // subsequent poll() will return immediately with POLLERR again,
149       // causing this code to spin in a loop. Calling read() will clear the
150       // socket error flag instead.
151       if (wait_fd[0].revents) read_packet(tunnel->read_fd6, tunnel->fd4, 0 /* to_ipv6 */);
152       if (wait_fd[1].revents) read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
153     }
154 
155     time_t now = time(NULL);
156     if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) {
157       last_interface_poll = now;
158       if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
159         break;
160       }
161     }
162   }
163 }
164