2018-10-11 00:01:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LOG_TAG
|
|
|
|
#define LOG_TAG "bpfloader"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2018-10-16 22:19:58 +02:00
|
|
|
#include <dirent.h>
|
2018-10-11 00:01:19 +02:00
|
|
|
#include <elf.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2022-02-08 00:15:48 +01:00
|
|
|
#include <android-base/logging.h>
|
2019-12-12 23:22:34 +01:00
|
|
|
#include <android-base/macros.h>
|
2019-01-11 12:22:05 +01:00
|
|
|
#include <android-base/properties.h>
|
2018-10-11 00:01:19 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2018-10-16 22:19:58 +02:00
|
|
|
#include <android-base/strings.h>
|
2018-10-11 00:01:19 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-10-16 22:19:58 +02:00
|
|
|
#include <libbpf_android.h>
|
2018-10-11 00:01:19 +02:00
|
|
|
#include <log/log.h>
|
|
|
|
#include <netdutils/Misc.h>
|
|
|
|
#include <netdutils/Slice.h>
|
|
|
|
#include "bpf/BpfUtils.h"
|
|
|
|
|
2018-10-16 22:19:58 +02:00
|
|
|
using android::base::EndsWith;
|
|
|
|
using std::string;
|
2018-10-11 00:01:19 +02:00
|
|
|
|
2019-12-12 23:22:34 +01:00
|
|
|
// see b/162057235. For arbitrary program types, the concern is that due to the lack of
|
|
|
|
// SELinux access controls over BPF program attachpoints, we have no way to control the
|
|
|
|
// attachment of programs to shared resources (or to detect when a shared resource
|
|
|
|
// has one BPF program replace another that is attached there)
|
|
|
|
constexpr bpf_prog_type kVendorAllowedProgTypes[] = {
|
2022-03-09 02:27:09 +01:00
|
|
|
BPF_PROG_TYPE_SOCKET_FILTER,
|
2019-12-12 23:22:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Location {
|
2021-01-15 08:36:32 +01:00
|
|
|
const char* const dir;
|
|
|
|
const char* const prefix;
|
2019-12-12 23:22:34 +01:00
|
|
|
const bpf_prog_type* allowedProgTypes = nullptr;
|
|
|
|
size_t allowedProgTypesLength = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Location locations[] = {
|
2022-01-17 10:22:34 +01:00
|
|
|
// Tethering mainline module: tether offload
|
2021-01-15 08:36:32 +01:00
|
|
|
{
|
|
|
|
.dir = "/apex/com.android.tethering/etc/bpf/",
|
|
|
|
.prefix = "tethering/",
|
|
|
|
},
|
2022-05-12 10:47:05 +02:00
|
|
|
// Tethering mainline module (shared with netd & system server)
|
|
|
|
{
|
|
|
|
.dir = "/apex/com.android.tethering/etc/bpf/netd_shared/",
|
|
|
|
.prefix = "netd_shared/",
|
|
|
|
},
|
|
|
|
// Tethering mainline module (shared with system server)
|
2022-01-17 10:22:34 +01:00
|
|
|
{
|
|
|
|
.dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
|
2022-04-21 15:31:33 +02:00
|
|
|
.prefix = "net_shared/",
|
2022-01-17 10:22:34 +01:00
|
|
|
},
|
2022-05-12 10:47:05 +02:00
|
|
|
// Tethering mainline module (not shared)
|
|
|
|
{
|
|
|
|
.dir = "/apex/com.android.tethering/etc/bpf/net_private/",
|
|
|
|
.prefix = "net_private/",
|
|
|
|
},
|
2021-01-15 08:36:32 +01:00
|
|
|
// Core operating system
|
|
|
|
{
|
|
|
|
.dir = "/system/etc/bpf/",
|
|
|
|
.prefix = "",
|
|
|
|
},
|
2019-12-12 23:22:34 +01:00
|
|
|
// Vendor operating system
|
|
|
|
{
|
|
|
|
.dir = "/vendor/etc/bpf/",
|
|
|
|
.prefix = "vendor/",
|
|
|
|
.allowedProgTypes = kVendorAllowedProgTypes,
|
|
|
|
.allowedProgTypesLength = arraysize(kVendorAllowedProgTypes),
|
|
|
|
},
|
2021-01-15 08:36:32 +01:00
|
|
|
};
|
2018-10-11 00:01:19 +02:00
|
|
|
|
2019-12-12 23:22:34 +01:00
|
|
|
int loadAllElfObjects(const Location& location) {
|
2020-06-15 04:27:33 +02:00
|
|
|
int retVal = 0;
|
2018-10-16 22:19:58 +02:00
|
|
|
DIR* dir;
|
|
|
|
struct dirent* ent;
|
|
|
|
|
2019-12-12 23:22:34 +01:00
|
|
|
if ((dir = opendir(location.dir)) != NULL) {
|
2018-10-16 22:19:58 +02:00
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
|
|
|
string s = ent->d_name;
|
|
|
|
if (!EndsWith(s, ".o")) continue;
|
|
|
|
|
2019-12-12 23:22:34 +01:00
|
|
|
string progPath(location.dir);
|
2020-09-10 09:51:59 +02:00
|
|
|
progPath += s;
|
2018-10-16 22:19:58 +02:00
|
|
|
|
2020-06-15 04:27:33 +02:00
|
|
|
bool critical;
|
2019-12-12 23:22:34 +01:00
|
|
|
int ret = android::bpf::loadProg(progPath.c_str(), &critical, location.prefix,
|
|
|
|
location.allowedProgTypes,
|
|
|
|
location.allowedProgTypesLength);
|
2020-06-15 04:27:33 +02:00
|
|
|
if (ret) {
|
|
|
|
if (critical) retVal = ret;
|
|
|
|
ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
|
|
|
|
} else {
|
|
|
|
ALOGI("Loaded object: %s", progPath.c_str());
|
|
|
|
}
|
2018-10-16 22:19:58 +02:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2020-06-15 04:27:33 +02:00
|
|
|
return retVal;
|
2018-10-16 22:19:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:36:32 +01:00
|
|
|
void createSysFsBpfSubDir(const char* const prefix) {
|
|
|
|
if (*prefix) {
|
|
|
|
mode_t prevUmask = umask(0);
|
|
|
|
|
|
|
|
string s = "/sys/fs/bpf/";
|
|
|
|
s += prefix;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
if (ret && errno != EEXIST) {
|
|
|
|
ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(prevUmask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 00:15:48 +01:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
(void)argc;
|
|
|
|
android::base::InitLogging(argv, &android::base::KernelLogger);
|
|
|
|
|
2020-06-11 00:56:07 +02:00
|
|
|
// Load all ELF objects, create programs and maps, and pin them
|
2019-12-12 23:22:34 +01:00
|
|
|
for (const auto& location : locations) {
|
2021-01-15 08:36:32 +01:00
|
|
|
createSysFsBpfSubDir(location.prefix);
|
2019-12-12 23:22:34 +01:00
|
|
|
if (loadAllElfObjects(location) != 0) {
|
2021-01-15 08:36:32 +01:00
|
|
|
ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
|
2020-09-10 09:51:59 +02:00
|
|
|
ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
|
|
|
|
ALOGE("If this triggers randomly, you might be hitting some memory allocation "
|
|
|
|
"problems or startup script race.");
|
|
|
|
ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
|
|
|
|
sleep(20);
|
|
|
|
return 2;
|
|
|
|
}
|
2020-06-15 04:27:33 +02:00
|
|
|
}
|
2019-01-11 12:22:05 +01:00
|
|
|
|
|
|
|
if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
|
2020-06-15 04:27:33 +02:00
|
|
|
ALOGE("Failed to set bpf.progs_loaded property");
|
2019-01-11 12:22:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-10-11 00:01:19 +02:00
|
|
|
}
|