blob: 4ce94f7ee592aa6b851d051c091d568e723bfeed [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 */
junyulaic4e591a2018-11-26 22:36:10 +090018#include <arpa/inet.h>
19#include <errno.h>
20#include <fcntl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021#include <poll.h>
22#include <signal.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050023#include <stdio.h>
junyulaic4e591a2018-11-26 22:36:10 +090024#include <stdlib.h>
25#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050026#include <sys/ioctl.h>
Elliott Hughes3afe9ae2014-07-18 17:25:26 -070027#include <sys/prctl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050028#include <sys/stat.h>
junyulaic4e591a2018-11-26 22:36:10 +090029#include <sys/types.h>
30#include <time.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050031#include <unistd.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050032
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090033#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050034#include <linux/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090036#include <linux/if_packet.h>
junyulaic4e591a2018-11-26 22:36:10 +090037#include <linux/if_tun.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090038#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090039#include <sys/capability.h>
40#include <sys/uio.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050041
Daniel Drowna45056e2012-03-23 10:42:54 -050042#include "clatd.h"
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -070043#include "checksum.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050044#include "config.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050045#include "dump.h"
junyulaic4e591a2018-11-26 22:36:10 +090046#include "getaddr.h"
47#include "logging.h"
junyulaic4e591a2018-11-26 22:36:10 +090048#include "translate.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050049
Maciej Żenczykowski5ce6cda2020-06-02 14:39:33 -070050struct clat_config Global_Clatd_Config;
51
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090052/* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
53#define MTU_DELTA 28
54
Daniel Drowna45056e2012-03-23 10:42:54 -050055volatile sig_atomic_t running = 1;
56
Lorenzo Colitti66deecd2019-01-04 12:27:27 +090057int ipv6_address_changed(const char *interface) {
58 union anyip *interface_ip;
59
60 interface_ip = getinterface_ip(interface, AF_INET6);
61 if (!interface_ip) {
62 logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface);
63 return 1;
64 }
65
66 if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) {
67 char oldstr[INET6_ADDRSTRLEN];
68 char newstr[INET6_ADDRSTRLEN];
69 inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr));
70 inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr));
71 logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr);
72 free(interface_ip);
73 return 1;
74 } else {
75 free(interface_ip);
76 return 0;
77 }
78}
79
Daniel Drowna45056e2012-03-23 10:42:54 -050080/* function: read_packet
Lorenzo Colitti9353be22014-12-03 15:18:29 +090081 * reads a packet from the tunnel fd and translates it
junyulaic4e591a2018-11-26 22:36:10 +090082 * read_fd - file descriptor to read original packet from
83 * write_fd - file descriptor to write translated packet to
84 * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4
Daniel Drowna45056e2012-03-23 10:42:54 -050085 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +090086void read_packet(int read_fd, int write_fd, int to_ipv6) {
Maciej Żenczykowski50303532020-06-02 14:46:45 -070087 uint8_t buf[PACKETLEN];
88 ssize_t readlen = read(read_fd, buf, PACKETLEN);
Daniel Drowna45056e2012-03-23 10:42:54 -050089
junyulaic4e591a2018-11-26 22:36:10 +090090 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +090091 if (errno != EAGAIN) {
junyulaic4e591a2018-11-26 22:36:10 +090092 logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +090093 }
Daniel Drowna45056e2012-03-23 10:42:54 -050094 return;
junyulaic4e591a2018-11-26 22:36:10 +090095 } else if (readlen == 0) {
96 logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed");
Daniel Drowna45056e2012-03-23 10:42:54 -050097 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090098 return;
99 }
100
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700101 if (!to_ipv6) {
102 translate_packet(write_fd, 0 /* to_ipv6 */, buf, readlen);
103 return;
104 }
105
junyulaic4e591a2018-11-26 22:36:10 +0900106 struct tun_pi *tun_header = (struct tun_pi *)buf;
107 if (readlen < (ssize_t)sizeof(*tun_header)) {
108 logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900109 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900111
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900112 uint16_t proto = ntohs(tun_header->proto);
113 if (proto != ETH_P_IP) {
114 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
115 return;
116 }
117
junyulaic4e591a2018-11-26 22:36:10 +0900118 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900119 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
120 }
121
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700122 uint8_t *packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900123 readlen -= sizeof(*tun_header);
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700124 translate_packet(write_fd, 1 /* to_ipv6 */, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500125}
126
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700127// IPv6 DAD packet format:
128// Ethernet header (if needed) will be added by the kernel:
129// u8[6] src_mac; u8[6] dst_mac '33:33:ff:XX:XX:XX'; be16 ethertype '0x86DD'
130// IPv6 header:
131// be32 0x60000000 - ipv6, tclass 0, flowlabel 0
132// be16 payload_length '32'; u8 nxt_hdr ICMPv6 '58'; u8 hop limit '255'
133// u128 src_ip6 '::'
134// u128 dst_ip6 'ff02::1:ffXX:XXXX'
135// ICMPv6 header:
136// u8 type '135'; u8 code '0'; u16 icmp6 checksum; u32 reserved '0'
137// ICMPv6 neighbour solicitation payload:
138// u128 tgt_ip6
139// ICMPv6 ND options:
140// u8 opt nr '14'; u8 length '1'; u8[6] nonce '6 random bytes'
141void send_dad(int fd, const struct in6_addr* tgt, int count) {
142 struct {
143 struct ip6_hdr ip6h;
144 struct nd_neighbor_solicit ns;
145 uint8_t ns_opt_nr;
146 uint8_t ns_opt_len;
147 uint8_t ns_opt_nonce[6];
148 } dad_pkt = {
149 .ip6h = {
150 .ip6_flow = htonl(6 << 28), // v6, 0 tclass, 0 flowlabel
151 .ip6_plen = htons(sizeof(dad_pkt) - sizeof(struct ip6_hdr)), // payload length, ie. 32
152 .ip6_nxt = IPPROTO_ICMPV6, // 58
153 .ip6_hlim = 255,
154 .ip6_src = {}, // ::
155 .ip6_dst.s6_addr = {
156 0xFF, 0x02, 0, 0,
157 0, 0, 0, 0,
158 0, 0, 0, 1,
159 0xFF, tgt->s6_addr[13], tgt->s6_addr[14], tgt->s6_addr[15],
160 }, // ff02::1:ffXX:XXXX - multicast group address derived from bottom 24-bits of tgt
161 },
162 .ns = {
163 .nd_ns_type = ND_NEIGHBOR_SOLICIT, // 135
164 .nd_ns_code = 0,
165 .nd_ns_cksum = 0, // will be calculated later
166 .nd_ns_reserved = 0,
167 .nd_ns_target = *tgt,
168 },
169 .ns_opt_nr = 14, // icmp6 option 'nonce' from RFC3971
170 .ns_opt_len = 1, // in units of 8 bytes, including option nr and len
171 .ns_opt_nonce = { // 'opt_len' * 8 - [size of u8: opt nr] - [size of u8: opt len] = 6 bytes
172 0, 0, 'C', 'L', 'A', 'T',
173 }, // first 2 bytes filled in below with random bytes, CLAT left to ease id in tcpdump
174 };
175 arc4random_buf(&dad_pkt.ns_opt_nonce, 2);
176
177 // 40 byte IPv6 header + 8 byte ICMPv6 header + 16 byte ipv6 target address + 8 byte nonce option
178 _Static_assert(sizeof(dad_pkt) == 40 + 8 + 16 + 8, "sizeof dad packet != 72");
179
180 // IPv6 header checksum is standard negated 16-bit one's complement sum over the icmpv6 pseudo
181 // header (which includes payload length, nextheader, and src/dst ip) and the icmpv6 payload.
182 //
183 // Src/dst ip immediately prefix the icmpv6 header itself, so can be handled along
184 // with the payload. We thus only need to manually account for payload len & next header.
185 //
186 // The magic '8' is simply the offset of the ip6_src field in the ipv6 header,
187 // ie. we're skipping over the ipv6 version, tclass, flowlabel, payload length, next header
188 // and hop limit fields, because they're not quite where we want them to be.
189 //
190 // ip6_plen is already in network order, while ip6_nxt is a single byte and thus needs htons().
191 uint32_t csum = dad_pkt.ip6h.ip6_plen + htons(dad_pkt.ip6h.ip6_nxt);
192 csum = ip_checksum_add(csum, &dad_pkt.ip6h.ip6_src, sizeof(dad_pkt) - 8);
193 dad_pkt.ns.nd_ns_cksum = ip_checksum_finish(csum);
194
195 const struct sockaddr_in6 dst = {
196 .sin6_family = AF_INET6,
197 .sin6_addr = dad_pkt.ip6h.ip6_dst,
198 .sin6_scope_id = if_nametoindex(Global_Clatd_Config.native_ipv6_interface),
199 };
200
201 while (count--) {
202 sendto(fd, &dad_pkt, sizeof(dad_pkt), 0 /*flags*/, (const struct sockaddr *)&dst, sizeof(dst));
203 }
204}
205
Daniel Drowna45056e2012-03-23 10:42:54 -0500206/* function: event_loop
207 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900208 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500209 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900210void event_loop(struct tun_data *tunnel) {
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700211 // Apparently some network gear will refuse to perform NS for IPs that aren't DAD'ed,
212 // this would then result in an ipv6-only network with working native ipv6, working
213 // IPv4 via DNS64, but non-functioning IPv4 via CLAT (ie. IPv4 literals + IPv4 only apps).
214 // The kernel itself doesn't do DAD for anycast ips (but does handle IPV6 MLD and handle ND).
215 // So we'll spoof dad here, and yeah, we really should check for a response and in
216 // case of failure pick a different IP. Seeing as 48-bits of the IP are utterly random
217 // (with the other 16 chosen to guarantee checksum neutrality) this seems like a remote
218 // concern...
219 // TODO: actually perform true DAD
220 send_dad(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, 2);
221
Daniel Drowna45056e2012-03-23 10:42:54 -0500222 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700223 struct pollfd wait_fd[] = {
224 { tunnel->read_fd6, POLLIN, 0 },
225 { tunnel->fd4, POLLIN, 0 },
226 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500227
228 // start the poll timer
229 last_interface_poll = time(NULL);
230
junyulaic4e591a2018-11-26 22:36:10 +0900231 while (running) {
232 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900233 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900234 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500235 }
236 } else {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900237 // Call read_packet if the socket has data to be read, but also if an
238 // error is waiting. If we don't call read() after getting POLLERR, a
239 // subsequent poll() will return immediately with POLLERR again,
240 // causing this code to spin in a loop. Calling read() will clear the
241 // socket error flag instead.
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700242 if (wait_fd[0].revents) read_packet(tunnel->read_fd6, tunnel->fd4, 0 /* to_ipv6 */);
243 if (wait_fd[1].revents) read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */);
Daniel Drowna45056e2012-03-23 10:42:54 -0500244 }
245
246 time_t now = time(NULL);
Rocco Yuee4b7da62020-09-02 15:21:41 +0800247 if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) {
248 last_interface_poll = now;
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700249 if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900250 break;
251 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500252 }
253 }
254}