blob: 8b49cecc1842b00a7991adf88096b4ff9cf0214d [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
2 * Copyright (C) 2017 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
17#ifndef LOG_TAG
18#define LOG_TAG "bpfloader"
19#endif
20
21#include <arpa/inet.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070022#include <dirent.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070023#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
Steven Morelanda48639e2022-02-07 23:15:48 +000041#include <android-base/logging.h>
Steven Moreland0f10f3f2019-12-12 14:22:34 -080042#include <android-base/macros.h>
Joel Fernandesd3ec8712019-01-11 06:22:05 -050043#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070044#include <android-base/stringprintf.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070045#include <android-base/strings.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070046#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070047#include <libbpf_android.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070048#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070049#include <netdutils/Misc.h>
50#include <netdutils/Slice.h>
51#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070052
Joel Fernandesd76a2002018-10-16 13:19:58 -070053using android::base::EndsWith;
Joel Fernandesd76a2002018-10-16 13:19:58 -070054using std::string;
Chenbo Feng75b410b2018-10-10 15:01:19 -070055
Steven Moreland0f10f3f2019-12-12 14:22:34 -080056// see b/162057235. For arbitrary program types, the concern is that due to the lack of
57// SELinux access controls over BPF program attachpoints, we have no way to control the
58// attachment of programs to shared resources (or to detect when a shared resource
59// has one BPF program replace another that is attached there)
60constexpr bpf_prog_type kVendorAllowedProgTypes[] = {
Stephane Lee16c93602022-03-08 17:27:09 -080061 BPF_PROG_TYPE_SOCKET_FILTER,
Steven Moreland0f10f3f2019-12-12 14:22:34 -080062};
63
64struct Location {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080065 const char* const dir;
66 const char* const prefix;
Steven Moreland0f10f3f2019-12-12 14:22:34 -080067 const bpf_prog_type* allowedProgTypes = nullptr;
68 size_t allowedProgTypesLength = 0;
69};
70
71const Location locations[] = {
Ken Chen6d697842022-01-17 17:22:34 +080072 // Tethering mainline module: tether offload
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080073 {
74 .dir = "/apex/com.android.tethering/etc/bpf/",
75 .prefix = "tethering/",
76 },
Ken Chen6d697842022-01-17 17:22:34 +080077 // Tethering mainline module: netd, clatd, ...etc
78 {
79 .dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
Maciej Żenczykowski2a36ce42022-04-21 06:31:33 -070080 .prefix = "net_shared/",
Ken Chen6d697842022-01-17 17:22:34 +080081 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080082 // Core operating system
83 {
84 .dir = "/system/etc/bpf/",
85 .prefix = "",
86 },
Steven Moreland0f10f3f2019-12-12 14:22:34 -080087 // Vendor operating system
88 {
89 .dir = "/vendor/etc/bpf/",
90 .prefix = "vendor/",
91 .allowedProgTypes = kVendorAllowedProgTypes,
92 .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes),
93 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080094};
Chenbo Feng75b410b2018-10-10 15:01:19 -070095
Steven Moreland0f10f3f2019-12-12 14:22:34 -080096int loadAllElfObjects(const Location& location) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070097 int retVal = 0;
Joel Fernandesd76a2002018-10-16 13:19:58 -070098 DIR* dir;
99 struct dirent* ent;
100
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800101 if ((dir = opendir(location.dir)) != NULL) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700102 while ((ent = readdir(dir)) != NULL) {
103 string s = ent->d_name;
104 if (!EndsWith(s, ".o")) continue;
105
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800106 string progPath(location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800107 progPath += s;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700108
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700109 bool critical;
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800110 int ret = android::bpf::loadProg(progPath.c_str(), &critical, location.prefix,
111 location.allowedProgTypes,
112 location.allowedProgTypesLength);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700113 if (ret) {
114 if (critical) retVal = ret;
115 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
116 } else {
117 ALOGI("Loaded object: %s", progPath.c_str());
118 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700119 }
120 closedir(dir);
121 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700122 return retVal;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700123}
124
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800125void createSysFsBpfSubDir(const char* const prefix) {
126 if (*prefix) {
127 mode_t prevUmask = umask(0);
128
129 string s = "/sys/fs/bpf/";
130 s += prefix;
131
132 errno = 0;
133 int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
134 if (ret && errno != EEXIST) {
135 ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
136 }
137
138 umask(prevUmask);
139 }
140}
141
Steven Morelanda48639e2022-02-07 23:15:48 +0000142int main(int argc, char** argv) {
143 (void)argc;
144 android::base::InitLogging(argv, &android::base::KernelLogger);
145
Maciej Żenczykowski567dc562020-06-10 15:56:07 -0700146 // Load all ELF objects, create programs and maps, and pin them
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800147 for (const auto& location : locations) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800148 createSysFsBpfSubDir(location.prefix);
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800149 if (loadAllElfObjects(location) != 0) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800150 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800151 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
152 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
153 "problems or startup script race.");
154 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
155 sleep(20);
156 return 2;
157 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700158 }
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500159
160 if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700161 ALOGE("Failed to set bpf.progs_loaded property");
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500162 return 1;
163 }
164
165 return 0;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700166}