blob: b9976e029716222e8b4e55068ec45d9c389fc91c [file] [log] [blame]
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001/*
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -07002 * Copyright (C) 2017-2023 The Android Open Source Project
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07003 *
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
17#ifndef LOG_TAG
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070018#define LOG_TAG "NetBpfLoad"
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070019#endif
20
21#include <arpa/inet.h>
22#include <dirent.h>
23#include <elf.h>
24#include <error.h>
25#include <fcntl.h>
26#include <inttypes.h>
27#include <linux/bpf.h>
28#include <linux/unistd.h>
29#include <net/if.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include <sys/mman.h>
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -070041#include <android/api-level.h>
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070042#include <android-base/logging.h>
43#include <android-base/macros.h>
44#include <android-base/properties.h>
45#include <android-base/stringprintf.h>
46#include <android-base/strings.h>
47#include <android-base/unique_fd.h>
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070048#include <log/log.h>
Maciej Żenczykowski40dfe532023-10-08 20:21:11 -070049
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070050#include "BpfSyscallWrappers.h"
51#include "bpf/BpfUtils.h"
Maciej Żenczykowski40dfe532023-10-08 20:21:11 -070052#include "loader.h"
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070053
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -070054namespace android {
55namespace bpf {
56
Maciej Żenczykowski68eab892024-05-24 03:17:59 -070057using base::StartsWith;
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -070058using base::EndsWith;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070059using std::string;
60
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -070061static bool exists(const char* const path) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070062 int v = access(path, F_OK);
Maciej Żenczykowski731acfe2024-04-30 10:09:57 +000063 if (!v) return true;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070064 if (errno == ENOENT) return false;
65 ALOGE("FATAL: access(%s, F_OK) -> %d [%d:%s]", path, v, errno, strerror(errno));
66 abort(); // can only hit this if permissions (likely selinux) are screwed up
67}
68
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070069
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -070070const Location locations[] = {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070071 // S+ Tethering mainline module (network_stack): tether offload
72 {
73 .dir = "/apex/com.android.tethering/etc/bpf/",
74 .prefix = "tethering/",
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070075 },
76 // T+ Tethering mainline module (shared with netd & system server)
77 // netutils_wrapper (for iptables xt_bpf) has access to programs
78 {
79 .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/",
80 .prefix = "netd_shared/",
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070081 },
82 // T+ Tethering mainline module (shared with netd & system server)
83 // netutils_wrapper has no access, netd has read only access
84 {
85 .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/",
86 .prefix = "netd_readonly/",
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070087 },
88 // T+ Tethering mainline module (shared with system server)
89 {
90 .dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
91 .prefix = "net_shared/",
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070092 },
93 // T+ Tethering mainline module (not shared, just network_stack)
94 {
95 .dir = "/apex/com.android.tethering/etc/bpf/net_private/",
96 .prefix = "net_private/",
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070097 },
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070098};
99
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700100static int loadAllElfObjects(const unsigned int bpfloader_ver, const Location& location) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700101 int retVal = 0;
102 DIR* dir;
103 struct dirent* ent;
104
105 if ((dir = opendir(location.dir)) != NULL) {
106 while ((ent = readdir(dir)) != NULL) {
107 string s = ent->d_name;
108 if (!EndsWith(s, ".o")) continue;
109
110 string progPath(location.dir);
111 progPath += s;
112
113 bool critical;
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700114 int ret = loadProg(progPath.c_str(), &critical, bpfloader_ver, location);
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700115 if (ret) {
116 if (critical) retVal = ret;
117 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
118 } else {
Maciej Żenczykowski5c057ed2024-04-30 11:59:13 +0000119 ALOGD("Loaded object: %s", progPath.c_str());
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700120 }
121 }
122 closedir(dir);
123 }
124 return retVal;
125}
126
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700127static int createSysFsBpfSubDir(const char* const prefix) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700128 if (*prefix) {
129 mode_t prevUmask = umask(0);
130
131 string s = "/sys/fs/bpf/";
132 s += prefix;
133
134 errno = 0;
135 int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
136 if (ret && errno != EEXIST) {
137 const int err = errno;
138 ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err));
139 return -err;
140 }
141
142 umask(prevUmask);
143 }
144 return 0;
145}
146
147// Technically 'value' doesn't need to be newline terminated, but it's best
148// to include a newline to match 'echo "value" > /proc/sys/...foo' behaviour,
149// which is usually how kernel devs test the actual sysctl interfaces.
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700150static int writeProcSysFile(const char *filename, const char *value) {
151 base::unique_fd fd(open(filename, O_WRONLY | O_CLOEXEC));
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700152 if (fd < 0) {
153 const int err = errno;
154 ALOGE("open('%s', O_WRONLY | O_CLOEXEC) -> %s", filename, strerror(err));
155 return -err;
156 }
157 int len = strlen(value);
158 int v = write(fd, value, len);
159 if (v < 0) {
160 const int err = errno;
161 ALOGE("write('%s', '%s', %d) -> %s", filename, value, len, strerror(err));
162 return -err;
163 }
164 if (v != len) {
165 // In practice, due to us only using this for /proc/sys/... files, this can't happen.
166 ALOGE("write('%s', '%s', %d) -> short write [%d]", filename, value, len, v);
167 return -EINVAL;
168 }
169 return 0;
170}
171
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -0800172#define APEX_MOUNT_POINT "/apex/com.android.tethering"
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +0000173const char * const platformBpfLoader = "/system/bin/bpfloader";
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -0800174
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700175static int logTetheringApexVersion(void) {
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -0800176 char * found_blockdev = NULL;
177 FILE * f = NULL;
178 char buf[4096];
179
180 f = fopen("/proc/mounts", "re");
181 if (!f) return 1;
182
183 // /proc/mounts format: block_device [space] mount_point [space] other stuff... newline
184 while (fgets(buf, sizeof(buf), f)) {
185 char * blockdev = buf;
186 char * space = strchr(blockdev, ' ');
187 if (!space) continue;
188 *space = '\0';
189 char * mntpath = space + 1;
190 space = strchr(mntpath, ' ');
191 if (!space) continue;
192 *space = '\0';
193 if (strcmp(mntpath, APEX_MOUNT_POINT)) continue;
194 found_blockdev = strdup(blockdev);
195 break;
196 }
197 fclose(f);
198 f = NULL;
199
200 if (!found_blockdev) return 2;
Maciej Żenczykowski5c057ed2024-04-30 11:59:13 +0000201 ALOGV("Found Tethering Apex mounted from blockdev %s", found_blockdev);
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -0800202
203 f = fopen("/proc/mounts", "re");
204 if (!f) { free(found_blockdev); return 3; }
205
206 while (fgets(buf, sizeof(buf), f)) {
207 char * blockdev = buf;
208 char * space = strchr(blockdev, ' ');
209 if (!space) continue;
210 *space = '\0';
211 char * mntpath = space + 1;
212 space = strchr(mntpath, ' ');
213 if (!space) continue;
214 *space = '\0';
215 if (strcmp(blockdev, found_blockdev)) continue;
216 if (strncmp(mntpath, APEX_MOUNT_POINT "@", strlen(APEX_MOUNT_POINT "@"))) continue;
217 char * at = strchr(mntpath, '@');
218 if (!at) continue;
219 char * ver = at + 1;
220 ALOGI("Tethering APEX version %s", ver);
221 }
222 fclose(f);
223 free(found_blockdev);
224 return 0;
225}
Maciej Żenczykowski2fe2db52024-02-07 01:23:58 +0000226
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -0700227static bool isGSI() {
228 // From //system/gsid/libgsi.cpp IsGsiRunning()
229 return !access("/metadata/gsi/dsu/booted", F_OK);
230}
231
Maciej Żenczykowski68eab892024-05-24 03:17:59 -0700232static bool hasGSM() {
233 static string ph = base::GetProperty("gsm.current.phone-type", "");
234 static bool gsm = (ph != "");
235 static bool logged = false;
236 if (!logged) {
237 logged = true;
238 ALOGI("hasGSM(gsm.current.phone-type='%s'): %s", ph.c_str(), gsm ? "true" : "false");
239 }
240 return gsm;
241}
242
243static bool isTV() {
244 if (hasGSM()) return false; // TVs don't do GSM
245
246 static string key = base::GetProperty("ro.oem.key1", "");
247 static bool tv = StartsWith(key, "ATV00");
248 static bool logged = false;
249 if (!logged) {
250 logged = true;
251 ALOGI("isTV(ro.oem.key1='%s'): %s.", key.c_str(), tv ? "true" : "false");
252 }
253 return tv;
254}
255
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -0700256static int doLoad(char** argv, char * const envp[]) {
Maciej Żenczykowski15f97312024-06-13 14:11:28 -0700257 const bool runningAsRoot = !getuid(); // true iff U QPR3 or V+
Maciej Żenczykowski7b95d992024-06-13 18:18:11 -0700258 const bool unreleased = (base::GetProperty("ro.build.version.codename", "") != "REL");
259
260 const int device_api_level = android_get_device_api_level() + (int)unreleased;
Maciej Żenczykowski041be522023-10-23 23:34:52 -0700261 const bool isAtLeastT = (device_api_level >= __ANDROID_API_T__);
262 const bool isAtLeastU = (device_api_level >= __ANDROID_API_U__);
263 const bool isAtLeastV = (device_api_level >= __ANDROID_API_V__);
264
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +0000265 // last in U QPR2 beta1
266 const bool has_platform_bpfloader_rc = exists("/system/etc/init/bpfloader.rc");
267 // first in U QPR2 beta~2
268 const bool has_platform_netbpfload_rc = exists("/system/etc/init/netbpfload.rc");
269
Maciej Żenczykowski62956142024-06-13 15:32:57 -0700270 // Version of Network BpfLoader depends on the Android OS version
Maciej Żenczykowski1a3b54f2024-06-13 15:35:46 -0700271 unsigned int bpfloader_ver = 42u; // [42] BPFLOADER_MAINLINE_VERSION
272 if (isAtLeastT) ++bpfloader_ver; // [43] BPFLOADER_MAINLINE_T_VERSION
273 if (isAtLeastU) ++bpfloader_ver; // [44] BPFLOADER_MAINLINE_U_VERSION
274 if (runningAsRoot) ++bpfloader_ver; // [45] BPFLOADER_MAINLINE_U_QPR3_VERSION
275 if (isAtLeastV) ++bpfloader_ver; // [46] BPFLOADER_MAINLINE_V_VERSION
Maciej Żenczykowski62956142024-06-13 15:32:57 -0700276
Maciej Żenczykowski7b95d992024-06-13 18:18:11 -0700277 ALOGI("NetBpfLoad v0.%u (%s) api:%d/%d kver:%07x (%s) uid:%d rc:%d%d",
278 bpfloader_ver, argv[0], android_get_device_api_level(), device_api_level,
279 kernelVersion(), describeArch(), getuid(),
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +0000280 has_platform_bpfloader_rc, has_platform_netbpfload_rc);
Maciej Żenczykowski041be522023-10-23 23:34:52 -0700281
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +0000282 if (!has_platform_bpfloader_rc && !has_platform_netbpfload_rc) {
283 ALOGE("Unable to find platform's bpfloader & netbpfload init scripts.");
284 return 1;
285 }
286
287 if (has_platform_bpfloader_rc && has_platform_netbpfload_rc) {
288 ALOGE("Platform has *both* bpfloader & netbpfload init scripts.");
289 return 1;
290 }
291
Maciej Żenczykowskib60599b2024-02-09 12:30:52 -0800292 logTetheringApexVersion();
293
Maciej Żenczykowski11141da2024-03-15 18:21:33 -0700294 if (!isAtLeastT) {
295 ALOGE("Impossible - not reachable on Android <T.");
Maciej Żenczykowski03ef12c2024-02-10 21:34:22 +0000296 return 1;
297 }
298
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +0000299 // both S and T require kernel 4.9 (and eBpf support)
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700300 if (isAtLeastT && !isAtLeastKernelVersion(4, 9, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -0700301 ALOGE("Android T requires kernel 4.9.");
302 return 1;
303 }
304
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +0000305 // U bumps the kernel requirement up to 4.14
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700306 if (isAtLeastU && !isAtLeastKernelVersion(4, 14, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -0700307 ALOGE("Android U requires kernel 4.14.");
308 return 1;
309 }
310
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +0000311 // V bumps the kernel requirement up to 4.19
312 // see also: //system/netd/tests/kernel_test.cpp TestKernel419
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700313 if (isAtLeastV && !isAtLeastKernelVersion(4, 19, 0)) {
Maciej Żenczykowski041be522023-10-23 23:34:52 -0700314 ALOGE("Android V requires kernel 4.19.");
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700315 return 1;
316 }
317
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +0000318 // Technically already required by U, but only enforce on V+
319 // see also: //system/netd/tests/kernel_test.cpp TestKernel64Bit
320 if (isAtLeastV && isKernel32Bit() && isAtLeastKernelVersion(5, 16, 0)) {
321 ALOGE("Android V+ platform with 32 bit kernel version >= 5.16.0 is unsupported");
322 if (!isTV()) return 1;
323 }
324
325 // Various known ABI layout issues, particularly wrt. bpf and ipsec/xfrm.
326 if (isAtLeastV && isKernel32Bit() && isX86()) {
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +0000327 ALOGE("Android V requires X86 kernel to be 64-bit.");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -0700328 if (!isTV()) return 1;
Maciej Żenczykowski7f6a4262024-02-17 00:42:42 +0000329 }
330
Maciej Żenczykowskic982a4b2024-04-25 23:04:09 -0700331 if (isAtLeastV) {
332 bool bad = false;
333
334 if (!isLtsKernel()) {
335 ALOGW("Android V only supports LTS kernels.");
336 bad = true;
337 }
338
339#define REQUIRE(maj, min, sub) \
340 if (isKernelVersion(maj, min) && !isAtLeastKernelVersion(maj, min, sub)) { \
341 ALOGW("Android V requires %d.%d kernel to be %d.%d.%d+.", maj, min, maj, min, sub); \
342 bad = true; \
343 }
344
345 REQUIRE(4, 19, 236)
346 REQUIRE(5, 4, 186)
347 REQUIRE(5, 10, 199)
348 REQUIRE(5, 15, 136)
349 REQUIRE(6, 1, 57)
350 REQUIRE(6, 6, 0)
351
352#undef REQUIRE
353
354 if (bad && !isGSI()) {
355 ALOGE("Unsupported kernel version (%07x).", kernelVersion());
356 }
357 }
358
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700359 if (isUserspace32bit() && isAtLeastKernelVersion(6, 2, 0)) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700360 /* Android 14/U should only launch on 64-bit kernels
361 * T launches on 5.10/5.15
362 * U launches on 5.15/6.1
363 * So >=5.16 implies isKernel64Bit()
364 *
365 * We thus added a test to V VTS which requires 5.16+ devices to use 64-bit kernels.
366 *
367 * Starting with Android V, which is the first to support a post 6.1 Linux Kernel,
368 * we also require 64-bit userspace.
369 *
370 * There are various known issues with 32-bit userspace talking to various
371 * kernel interfaces (especially CAP_NET_ADMIN ones) on a 64-bit kernel.
372 * Some of these have userspace or kernel workarounds/hacks.
373 * Some of them don't...
374 * We're going to be removing the hacks.
Maciej Żenczykowskic834fdb2024-06-02 22:24:01 +0000375 * (for example "ANDROID: xfrm: remove in_compat_syscall() checks").
376 * Note: this check/enforcement only applies to *system* userspace code,
377 * it does not affect unprivileged apps, the 32-on-64 compatibility
378 * problems are AFAIK limited to various CAP_NET_ADMIN protected interfaces.
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700379 *
380 * Additionally the 32-bit kernel jit support is poor,
381 * and 32-bit userspace on 64-bit kernel bpf ringbuffer compatibility is broken.
382 */
383 ALOGE("64-bit userspace required on 6.2+ kernels.");
Maciej Żenczykowski68eab892024-05-24 03:17:59 -0700384 if (!isTV()) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700385 }
386
387 // Ensure we can determine the Android build type.
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700388 if (!isEng() && !isUser() && !isUserdebug()) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700389 ALOGE("Failed to determine the build type: got %s, want 'eng', 'user', or 'userdebug'",
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700390 getBuildType().c_str());
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700391 return 1;
392 }
393
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -0700394 if (runningAsRoot) {
395 // Note: writing this proc file requires being root (always the case on V+)
396
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -0700397 // Linux 5.16-rc1 changed the default to 2 (disabled but changeable),
398 // but we need 0 (enabled)
399 // (this writeFile is known to fail on at least 4.19, but always defaults to 0 on
400 // pre-5.13, on 5.13+ it depends on CONFIG_BPF_UNPRIV_DEFAULT_OFF)
401 if (writeProcSysFile("/proc/sys/kernel/unprivileged_bpf_disabled", "0\n") &&
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700402 isAtLeastKernelVersion(5, 13, 0)) return 1;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700403 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700404
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700405 if (isAtLeastU) {
Maciej Żenczykowski48e476b2024-06-13 14:06:49 -0700406 // Note: writing these proc files requires CAP_NET_ADMIN
407 // and sepolicy which is only present on U+,
408 // on Android T and earlier versions they're written from the 'load_bpf_programs'
409 // trigger (ie. by init itself) instead.
410
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -0700411 // Enable the eBPF JIT -- but do note that on 64-bit kernels it is likely
412 // already force enabled by the kernel config option BPF_JIT_ALWAYS_ON.
413 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
414 // kernel does not have CONFIG_BPF_JIT=y)
415 // BPF_JIT is required by R VINTF (which means 4.14/4.19/5.4 kernels),
416 // but 4.14/4.19 were released with P & Q, and only 5.4 is new in R+.
417 if (writeProcSysFile("/proc/sys/net/core/bpf_jit_enable", "1\n")) return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700418
Maciej Żenczykowskif33f1282023-10-24 04:41:54 -0700419 // Enable JIT kallsyms export for privileged users only
420 // (Note: this (open) will fail with ENOENT 'No such file or directory' if
421 // kernel does not have CONFIG_HAVE_EBPF_JIT=y)
422 if (writeProcSysFile("/proc/sys/net/core/bpf_jit_kallsyms", "1\n")) return 1;
423 }
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700424
425 // Create all the pin subdirectories
426 // (this must be done first to allow selinux_context and pin_subdir functionality,
427 // which could otherwise fail with ENOENT during object pinning or renaming,
428 // due to ordering issues)
429 for (const auto& location : locations) {
430 if (createSysFsBpfSubDir(location.prefix)) return 1;
431 }
432
Maciej Żenczykowskia9209da2024-02-29 02:01:20 +0000433 // Note: there's no actual src dir for fs_bpf_loader .o's,
434 // so it is not listed in 'locations[].prefix'.
435 // This is because this is primarily meant for triggering genfscon rules,
436 // and as such this will likely always be the case.
437 // Thus we need to manually create the /sys/fs/bpf/loader subdirectory.
438 if (createSysFsBpfSubDir("loader")) return 1;
439
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700440 // Load all ELF objects, create programs and maps, and pin them
441 for (const auto& location : locations) {
Maciej Żenczykowski221b2482024-03-18 14:33:10 -0700442 if (loadAllElfObjects(bpfloader_ver, location) != 0) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700443 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
444 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
445 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
446 "problems or startup script race.");
447 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
448 sleep(20);
449 return 2;
450 }
451 }
452
453 int key = 1;
454 int value = 123;
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700455 base::unique_fd map(
456 createMap(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2, 0));
457 if (writeToMapEntry(map, &key, &value, BPF_ANY)) {
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700458 ALOGE("Critical kernel bug - failure to write into index 1 of 2 element bpf map array.");
459 return 1;
460 }
461
Maciej Żenczykowski15f97312024-06-13 14:11:28 -0700462 // leave a flag that we're done
463 if (createSysFsBpfSubDir("netd_shared/mainline_done")) return 1;
Maciej Żenczykowski58c18222023-10-20 14:40:16 -0700464
Maciej Żenczykowski15f97312024-06-13 14:11:28 -0700465 // platform bpfloader will only succeed when run as root
466 if (!runningAsRoot) {
467 // unreachable on U QPR3+ which always runs netbpfload as root
468
469 ALOGI("mainline done, no need to transfer control to platform bpf loader.");
470 return 0;
Maciej Żenczykowski732a1412024-03-14 00:17:18 -0700471 }
472
Maciej Żenczykowski15f97312024-06-13 14:11:28 -0700473 // unreachable before U QPR3
474 ALOGI("done, transferring control to platform bpfloader.");
475
476 // platform BpfLoader *needs* to run as root
477 const char * args[] = { platformBpfLoader, NULL, };
478 execve(args[0], (char**)args, envp);
479 ALOGE("FATAL: execve('%s'): %d[%s]", platformBpfLoader, errno, strerror(errno));
480 return 1;
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -0700481}
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700482
483} // namespace bpf
484} // namespace android
485
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -0700486int main(int argc, char** argv, char * const envp[]) {
487 android::base::InitLogging(argv, &android::base::KernelLogger);
488
489 if (argc == 2 && !strcmp(argv[1], "done")) {
490 // we're being re-exec'ed from platform bpfloader to 'finalize' things
491 if (!android::base::SetProperty("bpf.progs_loaded", "1")) {
492 ALOGE("Failed to set bpf.progs_loaded property to 1.");
493 return 125;
494 }
Maciej Żenczykowski66f16292024-05-06 23:52:33 -0700495 ALOGI("success.");
Maciej Żenczykowski6d151ef2024-04-30 23:55:57 -0700496 return 0;
497 }
498
499 return android::bpf::doLoad(argv, envp);
Maciej Żenczykowski75c2def2024-04-25 14:19:14 -0700500}