blob: c5ab952c5092ac9274f9063e24eaa84507793975 [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>
Maciej Żenczykowski1f395ef2023-02-16 05:11:54 +000023#include <stdbool.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050024#include <stdio.h>
junyulaic4e591a2018-11-26 22:36:10 +090025#include <stdlib.h>
26#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050027#include <sys/ioctl.h>
Elliott Hughes3afe9ae2014-07-18 17:25:26 -070028#include <sys/prctl.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050029#include <sys/stat.h>
junyulaic4e591a2018-11-26 22:36:10 +090030#include <sys/types.h>
31#include <time.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050032#include <unistd.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050033
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090034#include <linux/filter.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include <linux/if.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050036#include <linux/if_ether.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090037#include <linux/if_packet.h>
junyulaic4e591a2018-11-26 22:36:10 +090038#include <linux/if_tun.h>
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +090039#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090040#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
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +000080// reads L3 IPv6 packet from AF_PACKET socket, translates to IPv4, writes to tun
81void process_packet_6_to_4(struct tun_data *tunnel) {
Maciej Żenczykowski4e764172023-03-13 21:55:54 +000082 // ethernet header is 14 bytes, plus 4 for a normal VLAN tag or 8 for Q-in-Q
83 // we don't really support vlans (or especially Q-in-Q)...
84 // but a few bytes of extra buffer space doesn't hurt...
85 uint8_t buf[22 + MAXMTU + 1]; // +1 to make packet truncation obvious
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +000086 struct iovec iov = {
87 .iov_base = buf,
Maciej Żenczykowskif3eeff92023-03-13 21:50:01 +000088 .iov_len = sizeof(buf),
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +000089 };
Maciej Żenczykowski4e764172023-03-13 21:55:54 +000090 char cmsg_buf[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +000091 struct msghdr msgh = {
92 .msg_iov = &iov,
93 .msg_iovlen = 1,
94 .msg_control = cmsg_buf,
95 .msg_controllen = sizeof(cmsg_buf),
96 };
97 ssize_t readlen = recvmsg(tunnel->read_fd6, &msgh, /*flags*/ 0);
Daniel Drowna45056e2012-03-23 10:42:54 -050098
junyulaic4e591a2018-11-26 22:36:10 +090099 if (readlen < 0) {
Lorenzo Colitti49454812015-01-31 19:18:47 +0900100 if (errno != EAGAIN) {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000101 logmsg(ANDROID_LOG_WARN, "%s: read error: %s", __func__, strerror(errno));
Lorenzo Colitti49454812015-01-31 19:18:47 +0900102 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500103 return;
junyulaic4e591a2018-11-26 22:36:10 +0900104 } else if (readlen == 0) {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000105 logmsg(ANDROID_LOG_WARN, "%s: packet socket removed?", __func__);
Daniel Drowna45056e2012-03-23 10:42:54 -0500106 running = 0;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900107 return;
Maciej Żenczykowskif3eeff92023-03-13 21:50:01 +0000108 } else if (readlen >= sizeof(buf)) {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000109 logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__);
110 return;
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900111 }
112
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +0000113 __u32 tp_status = 0;
Maciej Żenczykowski4e764172023-03-13 21:55:54 +0000114 __u16 tp_net = 0;
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +0000115
116 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
117 if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) {
118 struct tpacket_auxdata *aux = (struct tpacket_auxdata *)CMSG_DATA(cmsg);
119 tp_status = aux->tp_status;
Maciej Żenczykowski4e764172023-03-13 21:55:54 +0000120 tp_net = aux->tp_net;
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +0000121 break;
122 }
123 }
124
Maciej Żenczykowski4e764172023-03-13 21:55:54 +0000125 if (readlen < tp_net) {
126 logmsg(ANDROID_LOG_WARN, "%s: ignoring %zd byte pkt shorter than %u L2 header",
127 __func__, readlen, tp_net);
128 return;
129 }
130
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +0000131 // This will detect a skb->ip_summed == CHECKSUM_PARTIAL packet with non-final L4 checksum
132 if (tp_status & TP_STATUS_CSUMNOTREADY) {
Maciej Żenczykowski1f395ef2023-02-16 05:11:54 +0000133 static bool logged = false;
134 if (!logged) {
135 logmsg(ANDROID_LOG_WARN, "read_packet checksum not ready");
136 logged = true;
137 }
Maciej Żenczykowskife7a1672023-01-17 21:28:22 +0000138 }
139
Maciej Żenczykowski4e764172023-03-13 21:55:54 +0000140 translate_packet(tunnel->fd4, 0 /* to_ipv6 */, buf + tp_net, readlen - tp_net);
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000141}
142
143// reads TUN_PI + L3 IPv4 packet from tun, translates to IPv6, writes to AF_INET6/RAW socket
144void process_packet_4_to_6(struct tun_data *tunnel) {
Maciej Żenczykowskif3eeff92023-03-13 21:50:01 +0000145 uint8_t buf[sizeof(struct tun_pi) + MAXMTU + 1]; // +1 to make packet truncation obvious
146 ssize_t readlen = read(tunnel->fd4, buf, sizeof(buf));
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000147
148 if (readlen < 0) {
149 if (errno != EAGAIN) {
150 logmsg(ANDROID_LOG_WARN, "%s: read error: %s", __func__, strerror(errno));
151 }
152 return;
153 } else if (readlen == 0) {
154 logmsg(ANDROID_LOG_WARN, "%s: tun interface removed", __func__);
155 running = 0;
156 return;
Maciej Żenczykowskif3eeff92023-03-13 21:50:01 +0000157 } else if (readlen >= sizeof(buf)) {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000158 logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__);
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700159 return;
160 }
161
junyulaic4e591a2018-11-26 22:36:10 +0900162 struct tun_pi *tun_header = (struct tun_pi *)buf;
163 if (readlen < (ssize_t)sizeof(*tun_header)) {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000164 logmsg(ANDROID_LOG_WARN, "%s: short read: got %ld bytes", __func__, readlen);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900165 return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500166 }
Lorenzo Colittif08c5aa2014-06-03 12:56:38 +0900167
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900168 uint16_t proto = ntohs(tun_header->proto);
169 if (proto != ETH_P_IP) {
170 logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
171 return;
172 }
173
junyulaic4e591a2018-11-26 22:36:10 +0900174 if (tun_header->flags != 0) {
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900175 logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
176 }
177
Maciej Żenczykowski50303532020-06-02 14:46:45 -0700178 uint8_t *packet = (uint8_t *)(tun_header + 1);
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900179 readlen -= sizeof(*tun_header);
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000180 translate_packet(tunnel->write_fd6, 1 /* to_ipv6 */, packet, readlen);
Daniel Drowna45056e2012-03-23 10:42:54 -0500181}
182
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700183// IPv6 DAD packet format:
184// Ethernet header (if needed) will be added by the kernel:
185// u8[6] src_mac; u8[6] dst_mac '33:33:ff:XX:XX:XX'; be16 ethertype '0x86DD'
186// IPv6 header:
187// be32 0x60000000 - ipv6, tclass 0, flowlabel 0
188// be16 payload_length '32'; u8 nxt_hdr ICMPv6 '58'; u8 hop limit '255'
189// u128 src_ip6 '::'
190// u128 dst_ip6 'ff02::1:ffXX:XXXX'
191// ICMPv6 header:
192// u8 type '135'; u8 code '0'; u16 icmp6 checksum; u32 reserved '0'
193// ICMPv6 neighbour solicitation payload:
194// u128 tgt_ip6
195// ICMPv6 ND options:
196// u8 opt nr '14'; u8 length '1'; u8[6] nonce '6 random bytes'
Maciej Żenczykowskia1cb0f32022-07-19 09:22:58 -0700197void send_dad(int fd, const struct in6_addr* tgt) {
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700198 struct {
199 struct ip6_hdr ip6h;
200 struct nd_neighbor_solicit ns;
201 uint8_t ns_opt_nr;
202 uint8_t ns_opt_len;
203 uint8_t ns_opt_nonce[6];
204 } dad_pkt = {
205 .ip6h = {
206 .ip6_flow = htonl(6 << 28), // v6, 0 tclass, 0 flowlabel
207 .ip6_plen = htons(sizeof(dad_pkt) - sizeof(struct ip6_hdr)), // payload length, ie. 32
208 .ip6_nxt = IPPROTO_ICMPV6, // 58
209 .ip6_hlim = 255,
210 .ip6_src = {}, // ::
211 .ip6_dst.s6_addr = {
212 0xFF, 0x02, 0, 0,
213 0, 0, 0, 0,
214 0, 0, 0, 1,
215 0xFF, tgt->s6_addr[13], tgt->s6_addr[14], tgt->s6_addr[15],
216 }, // ff02::1:ffXX:XXXX - multicast group address derived from bottom 24-bits of tgt
217 },
218 .ns = {
219 .nd_ns_type = ND_NEIGHBOR_SOLICIT, // 135
220 .nd_ns_code = 0,
221 .nd_ns_cksum = 0, // will be calculated later
222 .nd_ns_reserved = 0,
223 .nd_ns_target = *tgt,
224 },
225 .ns_opt_nr = 14, // icmp6 option 'nonce' from RFC3971
226 .ns_opt_len = 1, // in units of 8 bytes, including option nr and len
Maciej Żenczykowskia1cb0f32022-07-19 09:22:58 -0700227 .ns_opt_nonce = {}, // opt_len *8 - sizeof u8(opt_nr) - sizeof u8(opt_len) = 6 ranodmized bytes
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700228 };
Maciej Żenczykowskia1cb0f32022-07-19 09:22:58 -0700229 arc4random_buf(&dad_pkt.ns_opt_nonce, sizeof(dad_pkt.ns_opt_nonce));
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700230
231 // 40 byte IPv6 header + 8 byte ICMPv6 header + 16 byte ipv6 target address + 8 byte nonce option
232 _Static_assert(sizeof(dad_pkt) == 40 + 8 + 16 + 8, "sizeof dad packet != 72");
233
234 // IPv6 header checksum is standard negated 16-bit one's complement sum over the icmpv6 pseudo
235 // header (which includes payload length, nextheader, and src/dst ip) and the icmpv6 payload.
236 //
237 // Src/dst ip immediately prefix the icmpv6 header itself, so can be handled along
238 // with the payload. We thus only need to manually account for payload len & next header.
239 //
240 // The magic '8' is simply the offset of the ip6_src field in the ipv6 header,
241 // ie. we're skipping over the ipv6 version, tclass, flowlabel, payload length, next header
242 // and hop limit fields, because they're not quite where we want them to be.
243 //
244 // ip6_plen is already in network order, while ip6_nxt is a single byte and thus needs htons().
245 uint32_t csum = dad_pkt.ip6h.ip6_plen + htons(dad_pkt.ip6h.ip6_nxt);
246 csum = ip_checksum_add(csum, &dad_pkt.ip6h.ip6_src, sizeof(dad_pkt) - 8);
247 dad_pkt.ns.nd_ns_cksum = ip_checksum_finish(csum);
248
249 const struct sockaddr_in6 dst = {
250 .sin6_family = AF_INET6,
251 .sin6_addr = dad_pkt.ip6h.ip6_dst,
252 .sin6_scope_id = if_nametoindex(Global_Clatd_Config.native_ipv6_interface),
253 };
254
Maciej Żenczykowskia1cb0f32022-07-19 09:22:58 -0700255 sendto(fd, &dad_pkt, sizeof(dad_pkt), 0 /*flags*/, (const struct sockaddr *)&dst, sizeof(dst));
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700256}
257
Daniel Drowna45056e2012-03-23 10:42:54 -0500258/* function: event_loop
259 * reads packets from the tun network interface and passes them down the stack
junyulaic4e591a2018-11-26 22:36:10 +0900260 * tunnel - tun device data
Daniel Drowna45056e2012-03-23 10:42:54 -0500261 */
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900262void event_loop(struct tun_data *tunnel) {
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700263 // Apparently some network gear will refuse to perform NS for IPs that aren't DAD'ed,
264 // this would then result in an ipv6-only network with working native ipv6, working
265 // IPv4 via DNS64, but non-functioning IPv4 via CLAT (ie. IPv4 literals + IPv4 only apps).
266 // The kernel itself doesn't do DAD for anycast ips (but does handle IPV6 MLD and handle ND).
267 // So we'll spoof dad here, and yeah, we really should check for a response and in
268 // case of failure pick a different IP. Seeing as 48-bits of the IP are utterly random
269 // (with the other 16 chosen to guarantee checksum neutrality) this seems like a remote
270 // concern...
271 // TODO: actually perform true DAD
Maciej Żenczykowskia1cb0f32022-07-19 09:22:58 -0700272 send_dad(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet);
Maciej Żenczykowskif6ec94e2022-07-12 16:17:33 -0700273
Daniel Drowna45056e2012-03-23 10:42:54 -0500274 time_t last_interface_poll;
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -0700275 struct pollfd wait_fd[] = {
276 { tunnel->read_fd6, POLLIN, 0 },
277 { tunnel->fd4, POLLIN, 0 },
278 };
Daniel Drowna45056e2012-03-23 10:42:54 -0500279
280 // start the poll timer
281 last_interface_poll = time(NULL);
282
junyulaic4e591a2018-11-26 22:36:10 +0900283 while (running) {
284 if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) {
Bernie Innocenti69dc60d2018-05-14 20:40:49 +0900285 if (errno != EINTR) {
junyulaic4e591a2018-11-26 22:36:10 +0900286 logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno));
Daniel Drowna45056e2012-03-23 10:42:54 -0500287 }
288 } else {
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000289 // Call process_packet if the socket has data to be read, but also if an
Lorenzo Colitti9353be22014-12-03 15:18:29 +0900290 // error is waiting. If we don't call read() after getting POLLERR, a
291 // subsequent poll() will return immediately with POLLERR again,
292 // causing this code to spin in a loop. Calling read() will clear the
293 // socket error flag instead.
Maciej Żenczykowskie6e0c002023-01-18 23:57:35 +0000294 if (wait_fd[0].revents) process_packet_6_to_4(tunnel);
295 if (wait_fd[1].revents) process_packet_4_to_6(tunnel);
Daniel Drowna45056e2012-03-23 10:42:54 -0500296 }
297
298 time_t now = time(NULL);
Rocco Yuee4b7da62020-09-02 15:21:41 +0800299 if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) {
300 last_interface_poll = now;
Maciej Żenczykowskiba667df2020-06-02 01:41:54 -0700301 if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
Lorenzo Colitti66deecd2019-01-04 12:27:27 +0900302 break;
303 }
Daniel Drowna45056e2012-03-23 10:42:54 -0500304 }
305 }
306}