Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 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 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 18 | #include <arpa/inet.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 21 | #include <poll.h> |
| 22 | #include <signal.h> |
Maciej Żenczykowski | 1f395ef | 2023-02-16 05:11:54 +0000 | [diff] [blame] | 23 | #include <stdbool.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 24 | #include <stdio.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Elliott Hughes | 3afe9ae | 2014-07-18 17:25:26 -0700 | [diff] [blame] | 28 | #include <sys/prctl.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 29 | #include <sys/stat.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <time.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 32 | #include <unistd.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 33 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 34 | #include <linux/filter.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 | #include <linux/if.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 36 | #include <linux/if_ether.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 37 | #include <linux/if_packet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 38 | #include <linux/if_tun.h> |
Maciej Żenczykowski | 8eb4888 | 2023-03-14 03:55:22 +0000 | [diff] [blame] | 39 | #include <linux/virtio_net.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 40 | #include <net/if.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 41 | #include <sys/uio.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 42 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 43 | #include "clatd.h" |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 44 | #include "checksum.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 45 | #include "config.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 46 | #include "dump.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 47 | #include "getaddr.h" |
| 48 | #include "logging.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 49 | #include "translate.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 50 | |
Maciej Żenczykowski | 5ce6cda | 2020-06-02 14:39:33 -0700 | [diff] [blame] | 51 | struct clat_config Global_Clatd_Config; |
| 52 | |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 53 | /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */ |
| 54 | #define MTU_DELTA 28 |
| 55 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 56 | volatile sig_atomic_t running = 1; |
| 57 | |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 58 | int ipv6_address_changed(const char *interface) { |
| 59 | union anyip *interface_ip; |
| 60 | |
| 61 | interface_ip = getinterface_ip(interface, AF_INET6); |
| 62 | if (!interface_ip) { |
| 63 | logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 68 | char oldstr[INET6_ADDRSTRLEN]; |
| 69 | char newstr[INET6_ADDRSTRLEN]; |
| 70 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr)); |
| 71 | inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr)); |
| 72 | logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr); |
| 73 | free(interface_ip); |
| 74 | return 1; |
| 75 | } else { |
| 76 | free(interface_ip); |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | |
Maciej Żenczykowski | 206b238 | 2023-03-14 03:50:28 +0000 | [diff] [blame] | 81 | // reads IPv6 packet from AF_PACKET socket, translates to IPv4, writes to tun |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 82 | void process_packet_6_to_4(struct tun_data *tunnel) { |
Maciej Żenczykowski | 4e76417 | 2023-03-13 21:55:54 +0000 | [diff] [blame] | 83 | // ethernet header is 14 bytes, plus 4 for a normal VLAN tag or 8 for Q-in-Q |
| 84 | // we don't really support vlans (or especially Q-in-Q)... |
| 85 | // but a few bytes of extra buffer space doesn't hurt... |
Maciej Żenczykowski | 206b238 | 2023-03-14 03:50:28 +0000 | [diff] [blame] | 86 | struct { |
Maciej Żenczykowski | 8eb4888 | 2023-03-14 03:55:22 +0000 | [diff] [blame] | 87 | struct virtio_net_hdr vnet; |
Maciej Żenczykowski | 206b238 | 2023-03-14 03:50:28 +0000 | [diff] [blame] | 88 | uint8_t payload[22 + MAXMTU]; |
| 89 | char pad; // +1 to make packet truncation obvious |
| 90 | } buf; |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 91 | struct iovec iov = { |
Maciej Żenczykowski | 206b238 | 2023-03-14 03:50:28 +0000 | [diff] [blame] | 92 | .iov_base = &buf, |
Maciej Żenczykowski | f3eeff9 | 2023-03-13 21:50:01 +0000 | [diff] [blame] | 93 | .iov_len = sizeof(buf), |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 94 | }; |
Maciej Żenczykowski | 4e76417 | 2023-03-13 21:55:54 +0000 | [diff] [blame] | 95 | char cmsg_buf[CMSG_SPACE(sizeof(struct tpacket_auxdata))]; |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 96 | struct msghdr msgh = { |
| 97 | .msg_iov = &iov, |
| 98 | .msg_iovlen = 1, |
| 99 | .msg_control = cmsg_buf, |
| 100 | .msg_controllen = sizeof(cmsg_buf), |
| 101 | }; |
| 102 | ssize_t readlen = recvmsg(tunnel->read_fd6, &msgh, /*flags*/ 0); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 103 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 104 | if (readlen < 0) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 105 | if (errno != EAGAIN) { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 106 | logmsg(ANDROID_LOG_WARN, "%s: read error: %s", __func__, strerror(errno)); |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 107 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 108 | return; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 109 | } else if (readlen == 0) { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 110 | logmsg(ANDROID_LOG_WARN, "%s: packet socket removed?", __func__); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 111 | running = 0; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 112 | return; |
Maciej Żenczykowski | f3eeff9 | 2023-03-13 21:50:01 +0000 | [diff] [blame] | 113 | } else if (readlen >= sizeof(buf)) { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 114 | logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__); |
| 115 | return; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 118 | __u32 tp_status = 0; |
Maciej Żenczykowski | 4e76417 | 2023-03-13 21:55:54 +0000 | [diff] [blame] | 119 | __u16 tp_net = 0; |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh,cmsg)) { |
| 122 | if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) { |
| 123 | struct tpacket_auxdata *aux = (struct tpacket_auxdata *)CMSG_DATA(cmsg); |
| 124 | tp_status = aux->tp_status; |
Maciej Żenczykowski | 4e76417 | 2023-03-13 21:55:54 +0000 | [diff] [blame] | 125 | tp_net = aux->tp_net; |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Maciej Żenczykowski | a4df101 | 2023-03-16 02:42:37 +0000 | [diff] [blame^] | 130 | const int payload_offset = offsetof(typeof(buf), payload); |
| 131 | if (readlen < payload_offset + tp_net) { |
| 132 | logmsg(ANDROID_LOG_WARN, "%s: ignoring %zd byte pkt shorter than %d+%u L2 header", |
| 133 | __func__, readlen, payload_offset, tp_net); |
Maciej Żenczykowski | 4e76417 | 2023-03-13 21:55:54 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Maciej Żenczykowski | a4df101 | 2023-03-16 02:42:37 +0000 | [diff] [blame^] | 137 | const int pkt_len = readlen - payload_offset; |
Maciej Żenczykowski | aae2fb9 | 2023-03-15 00:51:54 +0000 | [diff] [blame] | 138 | |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 139 | // This will detect a skb->ip_summed == CHECKSUM_PARTIAL packet with non-final L4 checksum |
| 140 | if (tp_status & TP_STATUS_CSUMNOTREADY) { |
Maciej Żenczykowski | 1f395ef | 2023-02-16 05:11:54 +0000 | [diff] [blame] | 141 | static bool logged = false; |
| 142 | if (!logged) { |
Maciej Żenczykowski | aae2fb9 | 2023-03-15 00:51:54 +0000 | [diff] [blame] | 143 | logmsg(ANDROID_LOG_WARN, "%s: L4 checksum calculation required", __func__); |
Maciej Żenczykowski | 1f395ef | 2023-02-16 05:11:54 +0000 | [diff] [blame] | 144 | logged = true; |
| 145 | } |
Maciej Żenczykowski | aae2fb9 | 2023-03-15 00:51:54 +0000 | [diff] [blame] | 146 | |
| 147 | // These are non-negative by virtue of csum_start/offset being u16 |
| 148 | const int cs_start = buf.vnet.csum_start; |
| 149 | const int cs_offset = cs_start + buf.vnet.csum_offset; |
| 150 | if (cs_start > pkt_len) { |
| 151 | logmsg(ANDROID_LOG_ERROR, "%s: out of range - checksum start %d > %d", |
| 152 | __func__, cs_start, pkt_len); |
| 153 | } else if (cs_offset + 1 >= pkt_len) { |
| 154 | logmsg(ANDROID_LOG_ERROR, "%s: out of range - checksum offset %d + 1 >= %d", |
| 155 | __func__, cs_offset, pkt_len); |
| 156 | } else { |
| 157 | uint16_t csum = ip_checksum(buf.payload + cs_start, pkt_len - cs_start); |
| 158 | if (!csum) csum = 0xFFFF; // required fixup for UDP, TCP must live with it |
| 159 | buf.payload[cs_offset] = csum & 0xFF; |
| 160 | buf.payload[cs_offset + 1] = csum >> 8; |
| 161 | } |
Maciej Żenczykowski | fe7a167 | 2023-01-17 21:28:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Maciej Żenczykowski | aae2fb9 | 2023-03-15 00:51:54 +0000 | [diff] [blame] | 164 | translate_packet(tunnel->fd4, 0 /* to_ipv6 */, buf.payload + tp_net, pkt_len - tp_net); |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // reads TUN_PI + L3 IPv4 packet from tun, translates to IPv6, writes to AF_INET6/RAW socket |
| 168 | void process_packet_4_to_6(struct tun_data *tunnel) { |
Maciej Żenczykowski | cfa100f | 2023-03-14 03:04:50 +0000 | [diff] [blame] | 169 | struct { |
| 170 | struct tun_pi pi; |
| 171 | uint8_t payload[MAXMTU]; |
| 172 | char pad; // +1 byte to make packet truncation obvious |
| 173 | } buf; |
| 174 | ssize_t readlen = read(tunnel->fd4, &buf, sizeof(buf)); |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 175 | |
| 176 | if (readlen < 0) { |
| 177 | if (errno != EAGAIN) { |
| 178 | logmsg(ANDROID_LOG_WARN, "%s: read error: %s", __func__, strerror(errno)); |
| 179 | } |
| 180 | return; |
| 181 | } else if (readlen == 0) { |
| 182 | logmsg(ANDROID_LOG_WARN, "%s: tun interface removed", __func__); |
| 183 | running = 0; |
| 184 | return; |
Maciej Żenczykowski | f3eeff9 | 2023-03-13 21:50:01 +0000 | [diff] [blame] | 185 | } else if (readlen >= sizeof(buf)) { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 186 | logmsg(ANDROID_LOG_WARN, "%s: read truncation - ignoring pkt", __func__); |
Maciej Żenczykowski | 5030353 | 2020-06-02 14:46:45 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Maciej Żenczykowski | a4df101 | 2023-03-16 02:42:37 +0000 | [diff] [blame^] | 190 | const int payload_offset = offsetof(typeof(buf), payload); |
| 191 | |
| 192 | if (readlen < payload_offset) { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 193 | logmsg(ANDROID_LOG_WARN, "%s: short read: got %ld bytes", __func__, readlen); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 194 | return; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 195 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 196 | |
Maciej Żenczykowski | a4df101 | 2023-03-16 02:42:37 +0000 | [diff] [blame^] | 197 | const int pkt_len = readlen - payload_offset; |
| 198 | |
Maciej Żenczykowski | cfa100f | 2023-03-14 03:04:50 +0000 | [diff] [blame] | 199 | uint16_t proto = ntohs(buf.pi.proto); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 200 | if (proto != ETH_P_IP) { |
| 201 | logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto); |
| 202 | return; |
| 203 | } |
| 204 | |
Maciej Żenczykowski | cfa100f | 2023-03-14 03:04:50 +0000 | [diff] [blame] | 205 | if (buf.pi.flags != 0) { |
| 206 | logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, buf.pi.flags); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Maciej Żenczykowski | a4df101 | 2023-03-16 02:42:37 +0000 | [diff] [blame^] | 209 | translate_packet(tunnel->write_fd6, 1 /* to_ipv6 */, buf.payload, pkt_len); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 212 | // IPv6 DAD packet format: |
| 213 | // Ethernet header (if needed) will be added by the kernel: |
| 214 | // u8[6] src_mac; u8[6] dst_mac '33:33:ff:XX:XX:XX'; be16 ethertype '0x86DD' |
| 215 | // IPv6 header: |
| 216 | // be32 0x60000000 - ipv6, tclass 0, flowlabel 0 |
| 217 | // be16 payload_length '32'; u8 nxt_hdr ICMPv6 '58'; u8 hop limit '255' |
| 218 | // u128 src_ip6 '::' |
| 219 | // u128 dst_ip6 'ff02::1:ffXX:XXXX' |
| 220 | // ICMPv6 header: |
| 221 | // u8 type '135'; u8 code '0'; u16 icmp6 checksum; u32 reserved '0' |
| 222 | // ICMPv6 neighbour solicitation payload: |
| 223 | // u128 tgt_ip6 |
| 224 | // ICMPv6 ND options: |
| 225 | // u8 opt nr '14'; u8 length '1'; u8[6] nonce '6 random bytes' |
Maciej Żenczykowski | a1cb0f3 | 2022-07-19 09:22:58 -0700 | [diff] [blame] | 226 | void send_dad(int fd, const struct in6_addr* tgt) { |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 227 | struct { |
| 228 | struct ip6_hdr ip6h; |
| 229 | struct nd_neighbor_solicit ns; |
| 230 | uint8_t ns_opt_nr; |
| 231 | uint8_t ns_opt_len; |
| 232 | uint8_t ns_opt_nonce[6]; |
| 233 | } dad_pkt = { |
| 234 | .ip6h = { |
| 235 | .ip6_flow = htonl(6 << 28), // v6, 0 tclass, 0 flowlabel |
| 236 | .ip6_plen = htons(sizeof(dad_pkt) - sizeof(struct ip6_hdr)), // payload length, ie. 32 |
| 237 | .ip6_nxt = IPPROTO_ICMPV6, // 58 |
| 238 | .ip6_hlim = 255, |
| 239 | .ip6_src = {}, // :: |
| 240 | .ip6_dst.s6_addr = { |
| 241 | 0xFF, 0x02, 0, 0, |
| 242 | 0, 0, 0, 0, |
| 243 | 0, 0, 0, 1, |
| 244 | 0xFF, tgt->s6_addr[13], tgt->s6_addr[14], tgt->s6_addr[15], |
| 245 | }, // ff02::1:ffXX:XXXX - multicast group address derived from bottom 24-bits of tgt |
| 246 | }, |
| 247 | .ns = { |
| 248 | .nd_ns_type = ND_NEIGHBOR_SOLICIT, // 135 |
| 249 | .nd_ns_code = 0, |
| 250 | .nd_ns_cksum = 0, // will be calculated later |
| 251 | .nd_ns_reserved = 0, |
| 252 | .nd_ns_target = *tgt, |
| 253 | }, |
| 254 | .ns_opt_nr = 14, // icmp6 option 'nonce' from RFC3971 |
| 255 | .ns_opt_len = 1, // in units of 8 bytes, including option nr and len |
Maciej Żenczykowski | a1cb0f3 | 2022-07-19 09:22:58 -0700 | [diff] [blame] | 256 | .ns_opt_nonce = {}, // opt_len *8 - sizeof u8(opt_nr) - sizeof u8(opt_len) = 6 ranodmized bytes |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 257 | }; |
Maciej Żenczykowski | a1cb0f3 | 2022-07-19 09:22:58 -0700 | [diff] [blame] | 258 | arc4random_buf(&dad_pkt.ns_opt_nonce, sizeof(dad_pkt.ns_opt_nonce)); |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 259 | |
| 260 | // 40 byte IPv6 header + 8 byte ICMPv6 header + 16 byte ipv6 target address + 8 byte nonce option |
| 261 | _Static_assert(sizeof(dad_pkt) == 40 + 8 + 16 + 8, "sizeof dad packet != 72"); |
| 262 | |
| 263 | // IPv6 header checksum is standard negated 16-bit one's complement sum over the icmpv6 pseudo |
| 264 | // header (which includes payload length, nextheader, and src/dst ip) and the icmpv6 payload. |
| 265 | // |
| 266 | // Src/dst ip immediately prefix the icmpv6 header itself, so can be handled along |
| 267 | // with the payload. We thus only need to manually account for payload len & next header. |
| 268 | // |
| 269 | // The magic '8' is simply the offset of the ip6_src field in the ipv6 header, |
| 270 | // ie. we're skipping over the ipv6 version, tclass, flowlabel, payload length, next header |
| 271 | // and hop limit fields, because they're not quite where we want them to be. |
| 272 | // |
| 273 | // ip6_plen is already in network order, while ip6_nxt is a single byte and thus needs htons(). |
| 274 | uint32_t csum = dad_pkt.ip6h.ip6_plen + htons(dad_pkt.ip6h.ip6_nxt); |
| 275 | csum = ip_checksum_add(csum, &dad_pkt.ip6h.ip6_src, sizeof(dad_pkt) - 8); |
| 276 | dad_pkt.ns.nd_ns_cksum = ip_checksum_finish(csum); |
| 277 | |
| 278 | const struct sockaddr_in6 dst = { |
| 279 | .sin6_family = AF_INET6, |
| 280 | .sin6_addr = dad_pkt.ip6h.ip6_dst, |
| 281 | .sin6_scope_id = if_nametoindex(Global_Clatd_Config.native_ipv6_interface), |
| 282 | }; |
| 283 | |
Maciej Żenczykowski | a1cb0f3 | 2022-07-19 09:22:58 -0700 | [diff] [blame] | 284 | sendto(fd, &dad_pkt, sizeof(dad_pkt), 0 /*flags*/, (const struct sockaddr *)&dst, sizeof(dst)); |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 287 | /* function: event_loop |
| 288 | * reads packets from the tun network interface and passes them down the stack |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 289 | * tunnel - tun device data |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 290 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 291 | void event_loop(struct tun_data *tunnel) { |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 292 | // Apparently some network gear will refuse to perform NS for IPs that aren't DAD'ed, |
| 293 | // this would then result in an ipv6-only network with working native ipv6, working |
| 294 | // IPv4 via DNS64, but non-functioning IPv4 via CLAT (ie. IPv4 literals + IPv4 only apps). |
| 295 | // The kernel itself doesn't do DAD for anycast ips (but does handle IPV6 MLD and handle ND). |
| 296 | // So we'll spoof dad here, and yeah, we really should check for a response and in |
| 297 | // case of failure pick a different IP. Seeing as 48-bits of the IP are utterly random |
| 298 | // (with the other 16 chosen to guarantee checksum neutrality) this seems like a remote |
| 299 | // concern... |
| 300 | // TODO: actually perform true DAD |
Maciej Żenczykowski | a1cb0f3 | 2022-07-19 09:22:58 -0700 | [diff] [blame] | 301 | send_dad(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet); |
Maciej Żenczykowski | f6ec94e | 2022-07-12 16:17:33 -0700 | [diff] [blame] | 302 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 303 | time_t last_interface_poll; |
Lorenzo Colitti | dce3ddf | 2014-08-25 16:07:12 -0700 | [diff] [blame] | 304 | struct pollfd wait_fd[] = { |
| 305 | { tunnel->read_fd6, POLLIN, 0 }, |
| 306 | { tunnel->fd4, POLLIN, 0 }, |
| 307 | }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 308 | |
| 309 | // start the poll timer |
| 310 | last_interface_poll = time(NULL); |
| 311 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 312 | while (running) { |
| 313 | if (poll(wait_fd, ARRAY_SIZE(wait_fd), NO_TRAFFIC_INTERFACE_POLL_FREQUENCY * 1000) == -1) { |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 314 | if (errno != EINTR) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 315 | logmsg(ANDROID_LOG_WARN, "event_loop/poll returned an error: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 316 | } |
| 317 | } else { |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 318 | // Call process_packet if the socket has data to be read, but also if an |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 319 | // error is waiting. If we don't call read() after getting POLLERR, a |
| 320 | // subsequent poll() will return immediately with POLLERR again, |
| 321 | // causing this code to spin in a loop. Calling read() will clear the |
| 322 | // socket error flag instead. |
Maciej Żenczykowski | e6e0c00 | 2023-01-18 23:57:35 +0000 | [diff] [blame] | 323 | if (wait_fd[0].revents) process_packet_6_to_4(tunnel); |
| 324 | if (wait_fd[1].revents) process_packet_4_to_6(tunnel); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | time_t now = time(NULL); |
Rocco Yue | e4b7da6 | 2020-09-02 15:21:41 +0800 | [diff] [blame] | 328 | if (now >= (last_interface_poll + INTERFACE_POLL_FREQUENCY)) { |
| 329 | last_interface_poll = now; |
Maciej Żenczykowski | ba667df | 2020-06-02 01:41:54 -0700 | [diff] [blame] | 330 | if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) { |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 331 | break; |
| 332 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |