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> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 23 | #include <stdio.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Elliott Hughes | 3afe9ae | 2014-07-18 17:25:26 -0700 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 28 | #include <sys/stat.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <time.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 31 | #include <unistd.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 32 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 33 | #include <linux/filter.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 34 | #include <linux/if.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 | #include <linux/if_ether.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 36 | #include <linux/if_packet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 37 | #include <linux/if_tun.h> |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 38 | #include <net/if.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 39 | #include <sys/capability.h> |
| 40 | #include <sys/uio.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 41 | |
| 42 | #include <private/android_filesystem_config.h> |
| 43 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 44 | #include "clatd.h" |
| 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" |
| 49 | #include "mtu.h" |
| 50 | #include "resolv_netid.h" |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 51 | #include "ring.h" |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 52 | #include "setif.h" |
| 53 | #include "translate.h" |
| 54 | #include "tun.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 55 | |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 56 | /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */ |
| 57 | #define MTU_DELTA 28 |
| 58 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 59 | volatile sig_atomic_t running = 1; |
| 60 | |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 61 | /* function: stop_loop |
| 62 | * signal handler: stop the event loop |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 63 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 64 | void stop_loop() { running = 0; } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 65 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 66 | /* function: configure_packet_socket |
| 67 | * Binds the packet socket and attaches the receive filter to it. |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 68 | * sock - the socket to configure |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 69 | */ |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 70 | int configure_packet_socket(int sock) { |
| 71 | struct sockaddr_ll sll = { |
| 72 | .sll_family = AF_PACKET, |
| 73 | .sll_protocol = htons(ETH_P_IPV6), |
Masaya Hoshina | 96d6901 | 2015-06-10 22:17:49 +0900 | [diff] [blame] | 74 | .sll_ifindex = if_nametoindex(Global_Clatd_Config.default_pdp_interface), |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 75 | .sll_pkttype = PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel. |
| 76 | }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 77 | if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) { |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 78 | logmsg(ANDROID_LOG_FATAL, "binding packet socket: %s", strerror(errno)); |
| 79 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 80 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 81 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 82 | uint32_t *ipv6 = Global_Clatd_Config.ipv6_local_subnet.s6_addr32; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 83 | |
| 84 | // clang-format off |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 85 | struct sock_filter filter_code[] = { |
| 86 | // Load the first four bytes of the IPv6 destination address (starts 24 bytes in). |
| 87 | // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads |
| 88 | // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it |
| 89 | // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other |
| 90 | // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet). |
| 91 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24), |
| 92 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7), |
| 93 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28), |
| 94 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5), |
| 95 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32), |
| 96 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3), |
| 97 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36), |
| 98 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1), |
| 99 | BPF_STMT(BPF_RET | BPF_K, PACKETLEN), |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 100 | BPF_STMT(BPF_RET | BPF_K, 0), |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 101 | }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 102 | // clang-format on |
| 103 | struct sock_fprog filter = { sizeof(filter_code) / sizeof(filter_code[0]), filter_code }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 104 | |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 105 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) { |
| 106 | logmsg(ANDROID_LOG_FATAL, "attach packet filter failed: %s", strerror(errno)); |
| 107 | return 0; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 108 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 109 | |
| 110 | return 1; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 113 | /* function: ipv4_address_generate |
| 114 | * picks a free IPv4 address from the local subnet or exits if there are no free addresses |
| 115 | * returns: the IPv4 address as an in_addr_t |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 116 | */ |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 117 | static in_addr_t ipv4_address_generate() { |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 118 | // Pick an IPv4 address to use by finding a free address in the configured prefix. Technically, |
| 119 | // there is a race here - if another clatd calls config_select_ipv4_address after we do, but |
| 120 | // before we call add_address, it can end up having the same IP address as we do. But the time |
| 121 | // window in which this can happen is extremely small, and even if we end up with a duplicate |
| 122 | // address, the only damage is that IPv4 TCP connections won't be reset until both interfaces go |
| 123 | // down. |
| 124 | in_addr_t localaddr = config_select_ipv4_address(&Global_Clatd_Config.ipv4_local_subnet, |
| 125 | Global_Clatd_Config.ipv4_local_prefixlen); |
| 126 | if (localaddr == INADDR_NONE) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 127 | logmsg(ANDROID_LOG_FATAL, "No free IPv4 address in %s/%d", |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 128 | inet_ntoa(Global_Clatd_Config.ipv4_local_subnet), |
| 129 | Global_Clatd_Config.ipv4_local_prefixlen); |
| 130 | exit(1); |
| 131 | } |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 132 | return localaddr; |
| 133 | } |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 134 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 135 | /* function: ipv4_address_from_cmdline |
| 136 | * configures the IPv4 address specified on the command line, or exits if the address is not valid |
| 137 | * v4_addr - a string, the IPv4 address |
| 138 | * returns: the IPv4 address as an in_addr_t |
| 139 | */ |
| 140 | static in_addr_t ipv4_address_from_cmdline(const char *v4_addr) { |
| 141 | in_addr_t localaddr; |
| 142 | if (!inet_pton(AF_INET, v4_addr, &localaddr)) { |
| 143 | logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr); |
Lorenzo Colitti | 3ca0302 | 2013-03-27 12:39:10 +0900 | [diff] [blame] | 144 | exit(1); |
| 145 | } |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 146 | return localaddr; |
| 147 | } |
| 148 | |
| 149 | /* function: configure_tun_ip |
| 150 | * configures the ipv4 and ipv6 addresses on the tunnel interface |
| 151 | * tunnel - tun device data |
| 152 | */ |
| 153 | void configure_tun_ip(const struct tun_data *tunnel, const char *v4_addr) { |
| 154 | if (v4_addr) { |
| 155 | Global_Clatd_Config.ipv4_local_subnet.s_addr = ipv4_address_from_cmdline(v4_addr); |
| 156 | } else { |
| 157 | Global_Clatd_Config.ipv4_local_subnet.s_addr = ipv4_address_generate(); |
| 158 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 159 | |
Lorenzo Colitti | 798f993 | 2014-10-31 21:54:33 +0900 | [diff] [blame] | 160 | char addrstr[INET_ADDRSTRLEN]; |
| 161 | inet_ntop(AF_INET, &Global_Clatd_Config.ipv4_local_subnet, addrstr, sizeof(addrstr)); |
| 162 | logmsg(ANDROID_LOG_INFO, "Using IPv4 address %s on %s", addrstr, tunnel->device4); |
| 163 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 164 | // Configure the interface before bringing it up. As soon as we bring the interface up, the |
| 165 | // framework will be notified and will assume the interface's configuration has been finalized. |
| 166 | int status = add_address(tunnel->device4, AF_INET, &Global_Clatd_Config.ipv4_local_subnet, 32, |
| 167 | &Global_Clatd_Config.ipv4_local_subnet); |
| 168 | if (status < 0) { |
| 169 | logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_address(4) failed: %s", strerror(-status)); |
| 170 | exit(1); |
| 171 | } |
| 172 | |
| 173 | status = if_up(tunnel->device4, Global_Clatd_Config.ipv4mtu); |
| 174 | if (status < 0) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 175 | logmsg(ANDROID_LOG_FATAL, "configure_tun_ip/if_up(4) failed: %s", strerror(-status)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 176 | exit(1); |
| 177 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 178 | } |
| 179 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 180 | /* function: set_capability |
| 181 | * set the permitted, effective and inheritable capabilities of the current |
| 182 | * thread |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 183 | */ |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 184 | void set_capability(uint64_t target_cap) { |
| 185 | struct __user_cap_header_struct header = { |
| 186 | .version = _LINUX_CAPABILITY_VERSION_3, |
| 187 | .pid = 0 // 0 = change myself |
| 188 | }; |
| 189 | struct __user_cap_data_struct cap[_LINUX_CAPABILITY_U32S_3] = {}; |
| 190 | |
| 191 | cap[0].permitted = cap[0].effective = cap[0].inheritable = target_cap; |
| 192 | cap[1].permitted = cap[1].effective = cap[1].inheritable = target_cap >> 32; |
| 193 | |
| 194 | if (capset(&header, cap) < 0) { |
| 195 | logmsg(ANDROID_LOG_FATAL, "capset failed: %s", strerror(errno)); |
| 196 | exit(1); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* function: drop_root_but_keep_caps |
| 201 | * drops root privs but keeps the needed capabilities |
| 202 | */ |
| 203 | void drop_root_but_keep_caps() { |
Lorenzo Colitti | 7045e27 | 2014-06-13 21:36:01 +0900 | [diff] [blame] | 204 | gid_t groups[] = { AID_INET, AID_VPN }; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 205 | if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) < 0) { |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 206 | logmsg(ANDROID_LOG_FATAL, "setgroups failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 207 | exit(1); |
| 208 | } |
| 209 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 210 | prctl(PR_SET_KEEPCAPS, 1); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 211 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 212 | if (setresgid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) { |
| 213 | logmsg(ANDROID_LOG_FATAL, "setresgid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 214 | exit(1); |
| 215 | } |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 216 | if (setresuid(AID_CLAT, AID_CLAT, AID_CLAT) < 0) { |
| 217 | logmsg(ANDROID_LOG_FATAL, "setresuid failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 218 | exit(1); |
| 219 | } |
| 220 | |
junyulai | b5e8f97 | 2018-10-29 23:10:15 +0800 | [diff] [blame] | 221 | // keep CAP_NET_RAW capability to open raw socket, and CAP_IPC_LOCK for mmap |
| 222 | // to lock memory. |
| 223 | set_capability((1 << CAP_NET_ADMIN) | |
| 224 | (1 << CAP_NET_RAW) | |
| 225 | (1 << CAP_IPC_LOCK)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 228 | /* function: open_sockets |
| 229 | * opens a packet socket to receive IPv6 packets and a raw socket to send them |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 230 | * tunnel - tun device data |
| 231 | * mark - the socket mark to use for the sending raw socket |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 232 | */ |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 233 | void open_sockets(struct tun_data *tunnel, uint32_t mark) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 234 | int rawsock = socket(AF_INET6, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_RAW); |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 235 | if (rawsock < 0) { |
| 236 | logmsg(ANDROID_LOG_FATAL, "raw socket failed: %s", strerror(errno)); |
| 237 | exit(1); |
| 238 | } |
| 239 | |
| 240 | int off = 0; |
| 241 | if (setsockopt(rawsock, SOL_IPV6, IPV6_CHECKSUM, &off, sizeof(off)) < 0) { |
| 242 | logmsg(ANDROID_LOG_WARN, "could not disable checksum on raw socket: %s", strerror(errno)); |
| 243 | } |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 244 | if (mark != MARK_UNSET && setsockopt(rawsock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 245 | logmsg(ANDROID_LOG_ERROR, "could not set mark on raw socket: %s", strerror(errno)); |
| 246 | } |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 247 | |
| 248 | tunnel->write_fd6 = rawsock; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 249 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 250 | tunnel->read_fd6 = ring_create(tunnel); |
| 251 | if (tunnel->read_fd6 < 0) { |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 252 | exit(1); |
| 253 | } |
Lorenzo Colitti | ce14088 | 2014-06-02 21:20:40 +0900 | [diff] [blame] | 254 | } |
| 255 | |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 256 | int ipv6_address_changed(const char *interface) { |
| 257 | union anyip *interface_ip; |
| 258 | |
| 259 | interface_ip = getinterface_ip(interface, AF_INET6); |
| 260 | if (!interface_ip) { |
| 261 | logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface); |
| 262 | return 1; |
| 263 | } |
| 264 | |
| 265 | if (!ipv6_prefix_equal(&interface_ip->ip6, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 266 | char oldstr[INET6_ADDRSTRLEN]; |
| 267 | char newstr[INET6_ADDRSTRLEN]; |
| 268 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, oldstr, sizeof(oldstr)); |
| 269 | inet_ntop(AF_INET6, &interface_ip->ip6, newstr, sizeof(newstr)); |
| 270 | logmsg(ANDROID_LOG_INFO, "IPv6 prefix on %s changed: %s -> %s", interface, oldstr, newstr); |
| 271 | free(interface_ip); |
| 272 | return 1; |
| 273 | } else { |
| 274 | free(interface_ip); |
| 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 279 | /* function: clat_ipv6_address_from_interface |
| 280 | * picks the clat IPv6 address based on the interface address |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 281 | * interface - uplink interface name |
| 282 | * returns: 1 on success, 0 on failure |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 283 | */ |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 284 | static int clat_ipv6_address_from_interface(const char *interface) { |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 285 | union anyip *interface_ip; |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 286 | |
| 287 | // TODO: check that the prefix length is /64. |
| 288 | interface_ip = getinterface_ip(interface, AF_INET6); |
| 289 | if (!interface_ip) { |
| 290 | logmsg(ANDROID_LOG_ERROR, "Unable to find an IPv6 address on interface %s", interface); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 294 | // Generate an interface ID. |
| 295 | config_generate_local_ipv6_subnet(&interface_ip->ip6); |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 296 | |
| 297 | Global_Clatd_Config.ipv6_local_subnet = interface_ip->ip6; |
| 298 | free(interface_ip); |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | /* function: clat_ipv6_address_from_cmdline |
| 303 | * parses the clat IPv6 address from the command line |
| 304 | * v4_addr - a string, the IPv6 address |
| 305 | * returns: 1 on success, 0 on failure |
| 306 | */ |
| 307 | static int clat_ipv6_address_from_cmdline(const char *v6_addr) { |
| 308 | if (!inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 309 | logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | /* function: configure_clat_ipv6_address |
| 317 | * picks the clat IPv6 address and configures packet translation to use it. |
| 318 | * tunnel - tun device data |
| 319 | * interface - uplink interface name |
| 320 | * returns: 1 on success, 0 on failure |
| 321 | */ |
| 322 | int configure_clat_ipv6_address(const struct tun_data *tunnel, const char *interface, |
| 323 | const char *v6_addr) { |
| 324 | int ret; |
| 325 | if (v6_addr) { |
| 326 | ret = clat_ipv6_address_from_cmdline(v6_addr); |
| 327 | } else { |
| 328 | ret = clat_ipv6_address_from_interface(interface); |
| 329 | } |
| 330 | if (!ret) return 0; |
| 331 | |
| 332 | char addrstr[INET6_ADDRSTRLEN]; |
| 333 | inet_ntop(AF_INET6, &Global_Clatd_Config.ipv6_local_subnet, addrstr, sizeof(addrstr)); |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 334 | logmsg(ANDROID_LOG_INFO, "Using IPv6 address %s on %s", addrstr, interface); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 335 | |
| 336 | // Start translating packets to the new prefix. |
Lorenzo Colitti | 8a41a5d | 2014-10-21 12:37:48 +0900 | [diff] [blame] | 337 | add_anycast_address(tunnel->write_fd6, &Global_Clatd_Config.ipv6_local_subnet, interface); |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 338 | |
| 339 | // Update our packet socket filter to reflect the new 464xlat IP address. |
| 340 | if (!configure_packet_socket(tunnel->read_fd6)) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 341 | // Things aren't going to work. Bail out and hope we have better luck next time. |
| 342 | // We don't log an error here because configure_packet_socket has already done so. |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 343 | return 0; |
Lorenzo Colitti | 1352a3a | 2014-10-21 13:41:21 +0900 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return 1; |
| 347 | } |
| 348 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 349 | /* function: configure_interface |
| 350 | * reads the configuration and applies it to the interface |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 351 | * uplink_interface - network interface to use to reach the ipv6 internet |
| 352 | * plat_prefix - PLAT prefix to use |
| 353 | * tunnel - tun device data |
| 354 | * net_id - NetID to use, NETID_UNSET indicates use of default network |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 355 | */ |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 356 | void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v4_addr, |
| 357 | const char *v6_addr, struct tun_data *tunnel, unsigned net_id) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 358 | int error; |
| 359 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 360 | if (!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) { |
| 361 | logmsg(ANDROID_LOG_FATAL, "read_config failed"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 362 | exit(1); |
| 363 | } |
| 364 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 365 | if (Global_Clatd_Config.mtu > MAXMTU) { |
| 366 | logmsg(ANDROID_LOG_WARN, "Max MTU is %d, requested %d", MAXMTU, Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 367 | Global_Clatd_Config.mtu = MAXMTU; |
| 368 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 369 | if (Global_Clatd_Config.mtu <= 0) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 370 | Global_Clatd_Config.mtu = getifmtu(Global_Clatd_Config.default_pdp_interface); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 371 | logmsg(ANDROID_LOG_WARN, "ifmtu=%d", Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 372 | } |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 373 | if (Global_Clatd_Config.mtu < 1280) { |
| 374 | logmsg(ANDROID_LOG_WARN, "mtu too small = %d", Global_Clatd_Config.mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 375 | Global_Clatd_Config.mtu = 1280; |
| 376 | } |
| 377 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 378 | if (Global_Clatd_Config.ipv4mtu <= 0 || |
| 379 | Global_Clatd_Config.ipv4mtu > Global_Clatd_Config.mtu - MTU_DELTA) { |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 380 | Global_Clatd_Config.ipv4mtu = Global_Clatd_Config.mtu - MTU_DELTA; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 381 | logmsg(ANDROID_LOG_WARN, "ipv4mtu now set to = %d", Global_Clatd_Config.ipv4mtu); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 384 | error = tun_alloc(tunnel->device4, tunnel->fd4); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 385 | if (error < 0) { |
| 386 | logmsg(ANDROID_LOG_FATAL, "tun_alloc/4 failed: %s", strerror(errno)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 387 | exit(1); |
| 388 | } |
| 389 | |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 390 | error = set_nonblocking(tunnel->fd4); |
| 391 | if (error < 0) { |
| 392 | logmsg(ANDROID_LOG_FATAL, "set_nonblocking failed: %s", strerror(errno)); |
| 393 | exit(1); |
| 394 | } |
| 395 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 396 | configure_tun_ip(tunnel, v4_addr); |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 397 | |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame^] | 398 | if (!configure_clat_ipv6_address(tunnel, uplink_interface, v6_addr)) { |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 399 | exit(1); |
| 400 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 403 | /* function: read_packet |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 404 | * reads a packet from the tunnel fd and translates it |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 405 | * read_fd - file descriptor to read original packet from |
| 406 | * write_fd - file descriptor to write translated packet to |
| 407 | * to_ipv6 - whether the packet is to be translated to ipv6 or ipv4 |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 408 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 409 | void read_packet(int read_fd, int write_fd, int to_ipv6) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 410 | ssize_t readlen; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 411 | uint8_t buf[PACKETLEN], *packet; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 412 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 413 | readlen = read(read_fd, buf, PACKETLEN); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 414 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 415 | if (readlen < 0) { |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 416 | if (errno != EAGAIN) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 417 | logmsg(ANDROID_LOG_WARN, "read_packet/read error: %s", strerror(errno)); |
Lorenzo Colitti | 4945481 | 2015-01-31 19:18:47 +0900 | [diff] [blame] | 418 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 419 | return; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 420 | } else if (readlen == 0) { |
| 421 | logmsg(ANDROID_LOG_WARN, "read_packet/tun interface removed"); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 422 | running = 0; |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 426 | struct tun_pi *tun_header = (struct tun_pi *)buf; |
| 427 | if (readlen < (ssize_t)sizeof(*tun_header)) { |
| 428 | logmsg(ANDROID_LOG_WARN, "read_packet/short read: got %ld bytes", readlen); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 429 | return; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 430 | } |
Lorenzo Colitti | f08c5aa | 2014-06-03 12:56:38 +0900 | [diff] [blame] | 431 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 432 | uint16_t proto = ntohs(tun_header->proto); |
| 433 | if (proto != ETH_P_IP) { |
| 434 | logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto); |
| 435 | return; |
| 436 | } |
| 437 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 438 | if (tun_header->flags != 0) { |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 439 | logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags); |
| 440 | } |
| 441 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 442 | packet = (uint8_t *)(tun_header + 1); |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 443 | readlen -= sizeof(*tun_header); |
| 444 | translate_packet(write_fd, to_ipv6, packet, readlen); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* function: event_loop |
| 448 | * reads packets from the tun network interface and passes them down the stack |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 449 | * tunnel - tun device data |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 450 | */ |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 451 | void event_loop(struct tun_data *tunnel) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 452 | time_t last_interface_poll; |
Lorenzo Colitti | dce3ddf | 2014-08-25 16:07:12 -0700 | [diff] [blame] | 453 | struct pollfd wait_fd[] = { |
| 454 | { tunnel->read_fd6, POLLIN, 0 }, |
| 455 | { tunnel->fd4, POLLIN, 0 }, |
| 456 | }; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 457 | |
| 458 | // start the poll timer |
| 459 | last_interface_poll = time(NULL); |
| 460 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 461 | while (running) { |
| 462 | 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] | 463 | if (errno != EINTR) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 464 | 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] | 465 | } |
| 466 | } else { |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 467 | if (wait_fd[0].revents & POLLIN) { |
| 468 | ring_read(&tunnel->ring, tunnel->fd4, 0 /* to_ipv6 */); |
| 469 | } |
| 470 | // If any other bit is set, assume it's due to an error (i.e. POLLERR). |
| 471 | if (wait_fd[0].revents & ~POLLIN) { |
| 472 | // ring_read doesn't clear the error indication on the socket. |
| 473 | recv(tunnel->read_fd6, NULL, 0, MSG_PEEK); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 474 | logmsg(ANDROID_LOG_WARN, "event_loop: clearing error on read_fd6: %s", strerror(errno)); |
Bernie Innocenti | 69dc60d | 2018-05-14 20:40:49 +0900 | [diff] [blame] | 475 | } |
| 476 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 477 | // Call read_packet if the socket has data to be read, but also if an |
| 478 | // error is waiting. If we don't call read() after getting POLLERR, a |
| 479 | // subsequent poll() will return immediately with POLLERR again, |
| 480 | // causing this code to spin in a loop. Calling read() will clear the |
| 481 | // socket error flag instead. |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 482 | if (wait_fd[1].revents) { |
| 483 | read_packet(tunnel->fd4, tunnel->write_fd6, 1 /* to_ipv6 */); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | time_t now = time(NULL); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 488 | if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) { |
Lorenzo Colitti | 66deecd | 2019-01-04 12:27:27 +0900 | [diff] [blame] | 489 | if (ipv6_address_changed(Global_Clatd_Config.default_pdp_interface)) { |
| 490 | break; |
| 491 | } |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 496 | /* function: parse_unsigned |
| 497 | * parses a string as a decimal/hex/octal unsigned integer |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 498 | * str - the string to parse |
| 499 | * out - the unsigned integer to write to, gets clobbered on failure |
Lorenzo Colitti | 7564761 | 2014-06-09 13:55:50 +0900 | [diff] [blame] | 500 | */ |
| 501 | int parse_unsigned(const char *str, unsigned *out) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 502 | char *end_ptr; |
| 503 | *out = strtoul(str, &end_ptr, 0); |
| 504 | return *str && !*end_ptr; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 505 | } |