blob: 4e5a6ce4eef245bcd2b278f29f1597ef4f7c57ec [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[] = {
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070072 // S+ Tethering mainline module (network_stack): tether offload
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080073 {
74 .dir = "/apex/com.android.tethering/etc/bpf/",
75 .prefix = "tethering/",
76 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070077 // T+ Tethering mainline module (shared with netd & system server)
78 // netutils_wrapper (for iptables xt_bpf) has access to programs
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +000079 {
80 .dir = "/apex/com.android.tethering/etc/bpf/netd_shared/",
81 .prefix = "netd_shared/",
82 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070083 // T+ Tethering mainline module (shared with netd & system server)
84 // netutils_wrapper has no access, netd has read only access
85 {
86 .dir = "/apex/com.android.tethering/etc/bpf/netd_readonly/",
87 .prefix = "netd_readonly/",
88 },
89 // T+ Tethering mainline module (shared with system server)
Ken Chen6d697842022-01-17 17:22:34 +080090 {
91 .dir = "/apex/com.android.tethering/etc/bpf/net_shared/",
Maciej Żenczykowski2a36ce42022-04-21 06:31:33 -070092 .prefix = "net_shared/",
Ken Chen6d697842022-01-17 17:22:34 +080093 },
Maciej Żenczykowski32c0b8f2022-06-16 18:58:22 -070094 // T+ Tethering mainline module (not shared, just network_stack)
Maciej Żenczykowski7acf9382022-05-12 08:47:05 +000095 {
96 .dir = "/apex/com.android.tethering/etc/bpf/net_private/",
97 .prefix = "net_private/",
98 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -080099 // Core operating system
100 {
101 .dir = "/system/etc/bpf/",
102 .prefix = "",
103 },
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800104 // Vendor operating system
105 {
106 .dir = "/vendor/etc/bpf/",
107 .prefix = "vendor/",
108 .allowedProgTypes = kVendorAllowedProgTypes,
109 .allowedProgTypesLength = arraysize(kVendorAllowedProgTypes),
110 },
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800111};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700112
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800113int loadAllElfObjects(const Location& location) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700114 int retVal = 0;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700115 DIR* dir;
116 struct dirent* ent;
117
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800118 if ((dir = opendir(location.dir)) != NULL) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700119 while ((ent = readdir(dir)) != NULL) {
120 string s = ent->d_name;
121 if (!EndsWith(s, ".o")) continue;
122
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800123 string progPath(location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800124 progPath += s;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700125
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700126 bool critical;
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800127 int ret = android::bpf::loadProg(progPath.c_str(), &critical, location.prefix,
128 location.allowedProgTypes,
129 location.allowedProgTypesLength);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700130 if (ret) {
131 if (critical) retVal = ret;
132 ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret));
133 } else {
134 ALOGI("Loaded object: %s", progPath.c_str());
135 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700136 }
137 closedir(dir);
138 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700139 return retVal;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700140}
141
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800142void createSysFsBpfSubDir(const char* const prefix) {
143 if (*prefix) {
144 mode_t prevUmask = umask(0);
145
146 string s = "/sys/fs/bpf/";
147 s += prefix;
148
149 errno = 0;
150 int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
151 if (ret && errno != EEXIST) {
152 ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno));
153 }
154
155 umask(prevUmask);
156 }
157}
158
Steven Morelanda48639e2022-02-07 23:15:48 +0000159int main(int argc, char** argv) {
160 (void)argc;
161 android::base::InitLogging(argv, &android::base::KernelLogger);
162
Maciej Żenczykowski567dc562020-06-10 15:56:07 -0700163 // Load all ELF objects, create programs and maps, and pin them
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800164 for (const auto& location : locations) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800165 createSysFsBpfSubDir(location.prefix);
Steven Moreland0f10f3f2019-12-12 14:22:34 -0800166 if (loadAllElfObjects(location) != 0) {
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800167 ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir);
Hungming Chen4b8e9822020-09-10 15:51:59 +0800168 ALOGE("If this triggers reliably, you're probably missing kernel options or patches.");
169 ALOGE("If this triggers randomly, you might be hitting some memory allocation "
170 "problems or startup script race.");
171 ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---");
172 sleep(20);
173 return 2;
174 }
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700175 }
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500176
177 if (android::base::SetProperty("bpf.progs_loaded", "1") == false) {
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700178 ALOGE("Failed to set bpf.progs_loaded property");
Joel Fernandesd3ec8712019-01-11 06:22:05 -0500179 return 1;
180 }
181
182 return 0;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700183}