diff --git a/loader/BpfLoader.cpp b/loader/BpfLoader.cpp new file mode 100644 index 0000000..a44d354 --- /dev/null +++ b/loader/BpfLoader.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2024 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. + */ + +#include + +int main(int argc, char** argv, char * const envp[]) { + return android::bpf::legacyBpfLoader(argc, argv, envp); +} diff --git a/loader/Loader.cpp b/loader/Loader.cpp index 919b4b3..4dd64b7 100644 --- a/loader/Loader.cpp +++ b/loader/Loader.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include @@ -57,11 +58,13 @@ // Unspecified attach type is 0 which is BPF_CGROUP_INET_INGRESS. #define BPF_ATTACH_TYPE_UNSPEC BPF_CGROUP_INET_INGRESS +using android::base::EndsWith; using android::base::StartsWith; using android::base::unique_fd; using std::ifstream; using std::ios; using std::optional; +using std::strerror; using std::string; using std::vector; @@ -877,60 +880,6 @@ int loadProg(const char* elfPath, bool* isCritical, const Location& location) { return ret; } -} // namespace bpf -} // namespace android - -/* - * 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. - */ - -#define LOG_TAG "BpfLoader" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include "BpfSyscallWrappers.h" -#include "bpf/BpfUtils.h" - -using android::base::EndsWith; -using std::string; - // Networking-related program types are limited to the Tethering Apex // to prevent things from breaking due to conflicts on mainline updates // (exception made for socket filters, ie. xt_bpf for potential use in iptables, @@ -955,7 +904,7 @@ constexpr bpf_prog_type kVendorAllowedProgTypes[] = { BPF_PROG_TYPE_SOCKET_FILTER, }; -const android::bpf::Location locations[] = { +const Location locations[] = { // Core operating system { .dir = "/system/etc/bpf/", @@ -979,7 +928,7 @@ const android::bpf::Location locations[] = { }, }; -int loadAllElfObjects(const android::bpf::Location& location) { +int loadAllElfObjects(const Location& location) { int retVal = 0; DIR* dir; struct dirent* ent; @@ -993,10 +942,10 @@ int loadAllElfObjects(const android::bpf::Location& location) { progPath += s; bool critical; - int ret = android::bpf::loadProg(progPath.c_str(), &critical, location); + int ret = loadProg(progPath.c_str(), &critical, location); if (ret) { if (critical) retVal = ret; - ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); + ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), strerror(-ret)); } else { ALOGV("Loaded object: %s", progPath.c_str()); } @@ -1017,7 +966,7 @@ int createSysFsBpfSubDir(const char* const prefix) { int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); if (ret && errno != EEXIST) { const int err = errno; - ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(err)); + ALOGE("Failed to create directory: %s, ret: %s", s.c_str(), strerror(err)); return -err; } @@ -1026,8 +975,8 @@ int createSysFsBpfSubDir(const char* const prefix) { return 0; } -int main(int __unused argc, char** argv, char * const envp[]) { - android::base::InitLogging(argv, &android::base::KernelLogger); +int legacyBpfLoader(int __unused argc, char** argv, char * const envp[]) { + base::InitLogging(argv, &base::KernelLogger); // Load all ELF objects, create programs and maps, and pin them for (const auto& location : locations) { @@ -1047,3 +996,6 @@ int main(int __unused argc, char** argv, char * const envp[]) { ALOGE("FATAL: execve(): %d[%s]", errno, strerror(errno)); return 121; } + +} // namespace bpf +} // namespace android diff --git a/loader/include/libbpf_android.h b/loader/include/libbpf_android.h index 0a98a1d..df4e32c 100644 --- a/loader/include/libbpf_android.h +++ b/loader/include/libbpf_android.h @@ -37,5 +37,7 @@ int loadProg(const char* elfPath, bool* isCritical, const Location &location = { // Exposed for testing unsigned int readSectionUint(const char* name, std::ifstream& elfFile, unsigned int defVal); +int legacyBpfLoader(int argc, char** argv, char * const envp[]); + } // namespace bpf } // namespace android