blob: bc290417d88b6a0441751c428c063ee212aed43d [file] [log] [blame]
Lorenzo Colittieb92f482019-01-04 14:59:11 +09001/*
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 Colitti27da0ad2020-06-01 12:15:20 +090019#include <arpa/inet.h>
Lorenzo Colittieb92f482019-01-04 14:59:11 +090020#include <errno.h>
21#include <netinet/in.h>
Maciej Żenczykowski88cb78e2023-01-30 22:30:55 +000022#include <stdbool.h>
Lorenzo Colittieb92f482019-01-04 14:59:11 +090023#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
Maciej Żenczykowski88cb78e2023-01-30 22:30:55 +000026#include <sys/personality.h>
27#include <sys/utsname.h>
Lorenzo Colittieb92f482019-01-04 14:59:11 +090028#include <unistd.h>
29
Lorenzo Colittieb92f482019-01-04 14:59:11 +090030#include "clatd.h"
31#include "common.h"
32#include "config.h"
33#include "logging.h"
Lorenzo Colittieb92f482019-01-04 14:59:11 +090034
35#define DEVICEPREFIX "v4-"
36
Maciej Żenczykowskib984f312025-03-14 17:32:48 -070037/* function: handle_sigterm
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080038 * signal handler: stop the event loop
39 */
Maciej Żenczykowskib984f312025-03-14 17:32:48 -070040static void handle_sigterm(__attribute__((unused)) int unused) { sigterm = 1; };
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080041
Lorenzo Colittieb92f482019-01-04 14:59:11 +090042/* function: print_help
43 * in case the user is running this on the command line
44 */
45void print_help() {
46 printf("android-clat arguments:\n");
47 printf("-i [uplink interface]\n");
48 printf("-p [plat prefix]\n");
Lorenzo Colittif0fac862019-01-11 18:10:11 +090049 printf("-4 [IPv4 address]\n");
50 printf("-6 [IPv6 address]\n");
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070051 printf("-t [tun file descriptor number]\n");
Hungming Chen06367f32021-11-24 17:22:52 +080052 printf("-r [read socket descriptor number]\n");
Nucca Chen0714a182021-12-13 09:24:38 +000053 printf("-w [write socket descriptor number]\n");
Lorenzo Colittieb92f482019-01-04 14:59:11 +090054}
55
56/* function: main
57 * allocate and setup the tun device, then run the event loop
58 */
59int main(int argc, char **argv) {
60 struct tun_data tunnel;
61 int opt;
Hungming Chen9e718f42022-01-04 22:48:54 +080062 char *uplink_interface = NULL, *plat_prefix = NULL;
Hungming Chen06367f32021-11-24 17:22:52 +080063 char *v4_addr = NULL, *v6_addr = NULL, *tunfd_str = NULL, *read_sock_str = NULL,
64 *write_sock_str = NULL;
Lorenzo Colittieb92f482019-01-04 14:59:11 +090065 unsigned len;
66
Maciej Żenczykowski19199ca2025-03-14 12:08:05 -070067 // 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 Chen9e718f42022-01-04 22:48:54 +0800109 while ((opt = getopt(argc, argv, "i:p:4:6:t:r:w:h")) != -1) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900110 switch (opt) {
111 case 'i':
112 uplink_interface = optarg;
113 break;
114 case 'p':
115 plat_prefix = optarg;
116 break;
Lorenzo Colittif0fac862019-01-11 18:10:11 +0900117 case '4':
118 v4_addr = optarg;
119 break;
120 case '6':
121 v6_addr = optarg;
122 break;
Maciej Żenczykowski716518d2019-04-08 17:46:48 -0700123 case 't':
124 tunfd_str = optarg;
125 break;
Hungming Chen06367f32021-11-24 17:22:52 +0800126 case 'r':
127 read_sock_str = optarg;
128 break;
Nucca Chen0714a182021-12-13 09:24:38 +0000129 case 'w':
130 write_sock_str = optarg;
131 break;
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900132 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 Żenczykowski716518d2019-04-08 17:46:48 -0700146 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 Chen06367f32021-11-24 17:22:52 +0800155 if (read_sock_str != NULL && !parse_int(read_sock_str, &tunnel.read_fd6)) {
Hungming Chen9e718f42022-01-04 22:48:54 +0800156 logmsg(ANDROID_LOG_FATAL, "invalid read socket %s", read_sock_str);
Hungming Chen06367f32021-11-24 17:22:52 +0800157 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 Chen0714a182021-12-13 09:24:38 +0000164 if (write_sock_str != NULL && !parse_int(write_sock_str, &tunnel.write_fd6)) {
Hungming Chen9e718f42022-01-04 22:48:54 +0800165 logmsg(ANDROID_LOG_FATAL, "invalid write socket %s", write_sock_str);
Nucca Chen0714a182021-12-13 09:24:38 +0000166 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 Colittieb92f482019-01-04 14:59:11 +0900173 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 Chen5c112132021-11-25 09:40:17 +0800179 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 Chen5dafb0e2021-11-24 20:19:43 +0800185 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 Chen5c112132021-11-25 09:40:17 +0800190 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 Żenczykowski1961f672025-03-14 13:42:33 -0700195 logmsg(ANDROID_LOG_INFO, "Starting clat version " CLATD_VERSION " on %s plat=%s v4=%s v6=%s",
Hungming Chen9e718f42022-01-04 22:48:54 +0800196 uplink_interface, plat_prefix ? plat_prefix : "(none)", v4_addr ? v4_addr : "(none)",
Lorenzo Colitti27da0ad2020-06-01 12:15:20 +0900197 v6_addr ? v6_addr : "(none)");
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900198
Maciej Żenczykowski88cb78e2023-01-30 22:30:55 +0000199 {
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 Colittieb92f482019-01-04 14:59:11 +0900227 // Loop until someone sends us a signal or brings down the tun interface.
Maciej Żenczykowskib984f312025-03-14 17:32:48 -0700228 if (signal(SIGTERM, handle_sigterm) == SIG_ERR) {
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900229 logmsg(ANDROID_LOG_FATAL, "sigterm handler failed: %s", strerror(errno));
230 exit(1);
231 }
232
Maciej Żenczykowski2f96f2c2025-03-14 15:41:30 -0700233 // 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 Colittieb92f482019-01-04 14:59:11 +0900244 event_loop(&tunnel);
245
Maciej Żenczykowskib984f312025-03-14 17:32:48 -0700246 if (sigterm) {
Maciej Żenczykowski0372f292025-03-14 19:21:00 -0700247 logmsg(ANDROID_LOG_INFO, "Shutting down clatd on %s, already received SIGTERM", uplink_interface);
Maciej Żenczykowskib984f312025-03-14 17:32:48 -0700248 } else {
249 // this implies running == false, ie. we received EOF or ENETDOWN error.
Maciej Żenczykowski0372f292025-03-14 19:21:00 -0700250 logmsg(ANDROID_LOG_INFO, "Shutting down clatd on %s, waiting for SIGTERM", uplink_interface);
Maciej Żenczykowski9c05f752023-04-13 09:15:01 +0000251 // 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 Żenczykowskib984f312025-03-14 17:32:48 -0700253 // sleep() should be interrupted by SIGTERM, the handler should set 'sigterm'
Maciej Żenczykowski9c05f752023-04-13 09:15:01 +0000254 sleep(15);
255 logmsg(ANDROID_LOG_INFO, "Clatd on %s %s SIGTERM", uplink_interface,
Maciej Żenczykowskib984f312025-03-14 17:32:48 -0700256 sigterm ? "received" : "timed out waiting for");
Maciej Żenczykowski05b05412021-04-01 05:06:14 -0700257 }
Lorenzo Colittieb92f482019-01-04 14:59:11 +0900258 return 0;
259}