Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 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 | * main.c - main function |
| 17 | */ |
| 18 | |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 19 | #include <arpa/inet.h> |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <netinet/in.h> |
Maciej Żenczykowski | 88cb78e | 2023-01-30 22:30:55 +0000 | [diff] [blame] | 22 | #include <stdbool.h> |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Maciej Żenczykowski | 88cb78e | 2023-01-30 22:30:55 +0000 | [diff] [blame] | 26 | #include <sys/personality.h> |
| 27 | #include <sys/utsname.h> |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 30 | #include "clatd.h" |
| 31 | #include "common.h" |
| 32 | #include "config.h" |
| 33 | #include "logging.h" |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 34 | |
| 35 | #define DEVICEPREFIX "v4-" |
| 36 | |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 37 | /* function: handle_sigterm |
Maciej Żenczykowski | 8ab7e13 | 2021-02-03 17:15:41 -0800 | [diff] [blame] | 38 | * signal handler: stop the event loop |
| 39 | */ |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 40 | static void handle_sigterm(__attribute__((unused)) int unused) { sigterm = 1; }; |
Maciej Żenczykowski | 8ab7e13 | 2021-02-03 17:15:41 -0800 | [diff] [blame] | 41 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 42 | /* function: print_help |
| 43 | * in case the user is running this on the command line |
| 44 | */ |
| 45 | void print_help() { |
| 46 | printf("android-clat arguments:\n"); |
| 47 | printf("-i [uplink interface]\n"); |
| 48 | printf("-p [plat prefix]\n"); |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 49 | printf("-4 [IPv4 address]\n"); |
| 50 | printf("-6 [IPv6 address]\n"); |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 51 | printf("-t [tun file descriptor number]\n"); |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 52 | printf("-r [read socket descriptor number]\n"); |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 53 | printf("-w [write socket descriptor number]\n"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* function: main |
| 57 | * allocate and setup the tun device, then run the event loop |
| 58 | */ |
| 59 | int main(int argc, char **argv) { |
| 60 | struct tun_data tunnel; |
| 61 | int opt; |
Hungming Chen | 9e718f4 | 2022-01-04 22:48:54 +0800 | [diff] [blame] | 62 | char *uplink_interface = NULL, *plat_prefix = NULL; |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 63 | char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL, |
| 64 | *write_sock_str = NULL; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 65 | unsigned len; |
| 66 | |
Maciej Żenczykowski | 19199ca | 2025-03-14 12:08:05 -0700 | [diff] [blame^] | 67 | // Clatd binary is setuid/gid CLAT, thus when we reach here we have: |
| 68 | // $ adb shell ps | grep clat |
| 69 | // [pid] [ppid] |
| 70 | // clat 7650 1393 10785364 2612 do_sys_poll 0 S clatd-wlan0 |
| 71 | // $ adb shell cat /proc/7650/status | egrep -i '^(Uid:|Gid:|Groups:)' |
| 72 | // [real][effective][saved][filesystem] |
| 73 | // [uid] [euid] [suid] [fsuid] |
| 74 | // Uid: 1000 1029 1029 1029 |
| 75 | // [gid] [egid] [sgid] [fsgid] |
| 76 | // Gid: 1000 1029 1029 1029 |
| 77 | // Groups: 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1018 1021 1023 1024 1032 1065 3001 3002 3003 3005 3006 3007 3009 3010 3011 3012 |
| 78 | // This mismatch between uid & euid appears to cause periodic (every 5 minutes): |
| 79 | // objhash pid ppid uid |
| 80 | // W ActivityManager: Stale PhantomProcessRecord {xxxxxxx 7650:1393:clatd-wlan0/1000}, removing |
| 81 | // This is due to: |
| 82 | // $ adbz shell ls -ld /proc/7650 |
| 83 | // dr-xr-xr-x 9 clat clat 0 2025-03-14 11:37 /proc/7650 |
| 84 | // which is used by |
| 85 | // //frameworks/base/core/java/com/android/internal/os/ProcessCpuTracker.java |
| 86 | // which thus returns the uid 'clat' vs |
| 87 | // //frameworks/base/core/java/android/os/Process.java |
| 88 | // getUidForPid() which grabs *real* 'uid' from /proc/<pid>/status and is used in: |
| 89 | // //frameworks/base/services/core/java/com/android/server/am/PhantomProcessList.java |
| 90 | // (perhaps this should grab euid instead? unclear) |
| 91 | // |
| 92 | // However, we want to drop as many privs as possible, hence: |
| 93 | gid_t egid = getegid(); // documented to never fail, hence should return AID_CLAT == 1029 |
| 94 | uid_t euid = geteuid(); // (ditto) |
| 95 | setresgid(egid, egid, egid); // ignore any failure |
| 96 | setresuid(euid, euid, euid); // ignore any failure |
| 97 | // ideally we'd somehow drop supplementary groups too... |
| 98 | // but for historical reasons that actually requires CAP_SETGID which we don't have |
| 99 | // (see man 2 setgroups) |
| 100 | // |
| 101 | // Now we (should) have: |
| 102 | // $ adb shell ps | grep clat |
| 103 | // clat 5370 1479 10785364 2528 do_sys_poll 0 S clatd-wlan0 |
| 104 | // # adb shell cat /proc/5370/status | egrep -i '^(Uid:|Gid:|Groups:)' |
| 105 | // Uid: 1029 1029 1029 1029 |
| 106 | // Gid: 1029 1029 1029 1029 |
| 107 | // Groups: 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1018 1021 1023 1024 1032 1065 3001 3002 3003 3005 3006 3007 3009 3010 3011 3012 |
| 108 | |
Hungming Chen | 9e718f4 | 2022-01-04 22:48:54 +0800 | [diff] [blame] | 109 | while ((opt = getopt(argc, argv, "i:p:4:6:t:r:w:h")) != -1) { |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 110 | switch (opt) { |
| 111 | case 'i': |
| 112 | uplink_interface = optarg; |
| 113 | break; |
| 114 | case 'p': |
| 115 | plat_prefix = optarg; |
| 116 | break; |
Lorenzo Colitti | f0fac86 | 2019-01-11 18:10:11 +0900 | [diff] [blame] | 117 | case '4': |
| 118 | v4_addr = optarg; |
| 119 | break; |
| 120 | case '6': |
| 121 | v6_addr = optarg; |
| 122 | break; |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 123 | case 't': |
| 124 | tunfd_str = optarg; |
| 125 | break; |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 126 | case 'r': |
| 127 | read_sock_str = optarg; |
| 128 | break; |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 129 | case 'w': |
| 130 | write_sock_str = optarg; |
| 131 | break; |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 132 | case 'h': |
| 133 | print_help(); |
| 134 | exit(0); |
| 135 | default: |
| 136 | logmsg(ANDROID_LOG_FATAL, "Unknown option -%c. Exiting.", (char)optopt); |
| 137 | exit(1); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (uplink_interface == NULL) { |
| 142 | logmsg(ANDROID_LOG_FATAL, "clatd called without an interface"); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 146 | if (tunfd_str != NULL && !parse_int(tunfd_str, &tunnel.fd4)) { |
| 147 | logmsg(ANDROID_LOG_FATAL, "invalid tunfd %s", tunfd_str); |
| 148 | exit(1); |
| 149 | } |
| 150 | if (!tunnel.fd4) { |
| 151 | logmsg(ANDROID_LOG_FATAL, "no tunfd specified on commandline."); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 155 | if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) { |
Hungming Chen | 9e718f4 | 2022-01-04 22:48:54 +0800 | [diff] [blame] | 156 | logmsg(ANDROID_LOG_FATAL, "invalid read socket %s", read_sock_str); |
Hungming Chen | 06367f3 | 2021-11-24 17:22:52 +0800 | [diff] [blame] | 157 | exit(1); |
| 158 | } |
| 159 | if (!tunnel.read_fd6) { |
| 160 | logmsg(ANDROID_LOG_FATAL, "no read_fd6 specified on commandline."); |
| 161 | exit(1); |
| 162 | } |
| 163 | |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 164 | if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) { |
Hungming Chen | 9e718f4 | 2022-01-04 22:48:54 +0800 | [diff] [blame] | 165 | logmsg(ANDROID_LOG_FATAL, "invalid write socket %s", write_sock_str); |
Nucca Chen | 0714a18 | 2021-12-13 09:24:38 +0000 | [diff] [blame] | 166 | exit(1); |
| 167 | } |
| 168 | if (!tunnel.write_fd6) { |
| 169 | logmsg(ANDROID_LOG_FATAL, "no write_fd6 specified on commandline."); |
| 170 | exit(1); |
| 171 | } |
| 172 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 173 | len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface); |
| 174 | if (len >= sizeof(tunnel.device4)) { |
| 175 | logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4); |
| 176 | exit(1); |
| 177 | } |
| 178 | |
Hungming Chen | 5c11213 | 2021-11-25 09:40:17 +0800 | [diff] [blame] | 179 | Global_Clatd_Config.native_ipv6_interface = uplink_interface; |
| 180 | if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) { |
| 181 | logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix); |
| 182 | exit(1); |
| 183 | } |
| 184 | |
Hungming Chen | 5dafb0e | 2021-11-24 20:19:43 +0800 | [diff] [blame] | 185 | if (!v4_addr || !inet_pton(AF_INET, v4_addr, &Global_Clatd_Config.ipv4_local_subnet.s_addr)) { |
| 186 | logmsg(ANDROID_LOG_FATAL, "Invalid IPv4 address %s", v4_addr); |
| 187 | exit(1); |
| 188 | } |
| 189 | |
Hungming Chen | 5c11213 | 2021-11-25 09:40:17 +0800 | [diff] [blame] | 190 | if (!v6_addr || !inet_pton(AF_INET6, v6_addr, &Global_Clatd_Config.ipv6_local_subnet)) { |
| 191 | logmsg(ANDROID_LOG_FATAL, "Invalid source address %s", v6_addr); |
| 192 | exit(1); |
| 193 | } |
| 194 | |
Maciej Żenczykowski | 1961f67 | 2025-03-14 13:42:33 -0700 | [diff] [blame] | 195 | logmsg(ANDROID_LOG_INFO, "Starting clat version " CLATD_VERSION " on %s plat=%s v4=%s v6=%s", |
Hungming Chen | 9e718f4 | 2022-01-04 22:48:54 +0800 | [diff] [blame] | 196 | uplink_interface, plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)", |
Lorenzo Colitti | 27da0ad | 2020-06-01 12:15:20 +0900 | [diff] [blame] | 197 | v6_addr ? v6_addr : "(none)"); |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 198 | |
Maciej Żenczykowski | 88cb78e | 2023-01-30 22:30:55 +0000 | [diff] [blame] | 199 | { |
| 200 | // Compile time detection of 32 vs 64-bit build. (note: C does not have 'constexpr') |
| 201 | // Avoid use of preprocessor macros to get compile time syntax checking even on 64-bit. |
| 202 | const int user_bits = sizeof(void*) * 8; |
| 203 | const bool user32 = (user_bits == 32); |
| 204 | |
| 205 | // Note that on 64-bit all this personality related code simply compile optimizes out. |
| 206 | // 32-bit: fetch current personality (see 'man personality': 0xFFFFFFFF means retrieve only) |
| 207 | // On Linux fetching personality cannot fail. |
| 208 | const int prev_personality = user32 ? personality(0xFFFFFFFFuL) : PER_LINUX; |
| 209 | // 32-bit: attempt to get rid of kernel spoofing of 'uts.machine' architecture, |
| 210 | // In theory this cannot fail, as PER_LINUX should always be supported. |
| 211 | if (user32) (void)personality((prev_personality & ~PER_MASK) | PER_LINUX); |
| 212 | // 64-bit: this will compile time evaluate to false. |
| 213 | const bool was_linux32 = (prev_personality & PER_MASK) == PER_LINUX32; |
| 214 | |
| 215 | struct utsname uts = {}; |
| 216 | if (uname(&uts)) exit(1); // only possible error is EFAULT, but 'uts' is on stack |
| 217 | |
| 218 | // sysname is likely 'Linux', release is 'kver', machine is kernel's *true* architecture |
| 219 | logmsg(ANDROID_LOG_INFO, "%d-bit userspace on %s kernel %s for %s%s.", user_bits, |
| 220 | uts.sysname, uts.release, uts.machine, was_linux32 ? " (was spoofed)" : ""); |
| 221 | |
| 222 | // 32-bit: try to return to the 'default' personality |
| 223 | // In theory this cannot fail, because it was already previously in use. |
| 224 | if (user32) (void)personality(prev_personality); |
| 225 | } |
| 226 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 227 | // Loop until someone sends us a signal or brings down the tun interface. |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 228 | if (signal(SIGTERM, handle_sigterm) == SIG_ERR) { |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 229 | logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno)); |
| 230 | exit(1); |
| 231 | } |
| 232 | |
Maciej Żenczykowski | 2f96f2c | 2025-03-14 15:41:30 -0700 | [diff] [blame] | 233 | // Apparently some network gear will refuse to perform NS for IPs that aren't DAD'ed, |
| 234 | // this would then result in an ipv6-only network with working native ipv6, working |
| 235 | // IPv4 via DNS64, but non-functioning IPv4 via CLAT (ie. IPv4 literals + IPv4 only apps). |
| 236 | // The kernel itself doesn't do DAD for anycast ips (but does handle IPV6 MLD and handle ND). |
| 237 | // So we'll spoof dad here, and yeah, we really should check for a response and in |
| 238 | // case of failure pick a different IP. Seeing as 48-bits of the IP are utterly random |
| 239 | // (with the other 16 chosen to guarantee checksum neutrality) this seems like a remote |
| 240 | // concern... |
| 241 | // TODO: actually perform true DAD |
| 242 | send_dad(tunnel.write_fd6, &Global_Clatd_Config.ipv6_local_subnet); |
| 243 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 244 | event_loop(&tunnel); |
| 245 | |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 246 | if (sigterm) { |
Maciej Żenczykowski | 0372f29 | 2025-03-14 19:21:00 -0700 | [diff] [blame] | 247 | logmsg(ANDROID_LOG_INFO, "Shutting down clatd on %s, already received SIGTERM", uplink_interface); |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 248 | } else { |
| 249 | // this implies running == false, ie. we received EOF or ENETDOWN error. |
Maciej Żenczykowski | 0372f29 | 2025-03-14 19:21:00 -0700 | [diff] [blame] | 250 | logmsg(ANDROID_LOG_INFO, "Shutting down clatd on %s, waiting for SIGTERM", uplink_interface); |
Maciej Żenczykowski | 9c05f75 | 2023-04-13 09:15:01 +0000 | [diff] [blame] | 251 | // let's give higher level java code 15 seconds to kill us, |
| 252 | // but eventually terminate anyway, in case system server forgets about us... |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 253 | // sleep() should be interrupted by SIGTERM, the handler should set 'sigterm' |
Maciej Żenczykowski | 9c05f75 | 2023-04-13 09:15:01 +0000 | [diff] [blame] | 254 | sleep(15); |
| 255 | logmsg(ANDROID_LOG_INFO, "Clatd on %s %s SIGTERM", uplink_interface, |
Maciej Żenczykowski | b984f31 | 2025-03-14 17:32:48 -0700 | [diff] [blame] | 256 | sigterm ? "received" : "timed out waiting for"); |
Maciej Żenczykowski | 05b0541 | 2021-04-01 05:06:14 -0700 | [diff] [blame] | 257 | } |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 258 | return 0; |
| 259 | } |