Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 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 | |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 43 | #include <android-base/strings.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 44 | #include <android-base/unique_fd.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 45 | #include <libbpf_android.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 46 | #include <log/log.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 47 | #include <netdutils/Misc.h> |
| 48 | #include <netdutils/Slice.h> |
| 49 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 50 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 51 | using android::base::EndsWith; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 52 | using std::string; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 53 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 54 | struct { |
| 55 | const char* const dir; |
| 56 | const char* const prefix; |
| 57 | } locations[] = { |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 58 | // Tethering mainline module: tether offload |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 59 | { |
| 60 | .dir = "/apex/com.android.tethering/etc/bpf/", |
| 61 | .prefix = "tethering/", |
| 62 | }, |
Ken Chen | 6d69784 | 2022-01-17 17:22:34 +0800 | [diff] [blame] | 63 | // Tethering mainline module: netd, clatd, ...etc |
| 64 | { |
| 65 | .dir = "/apex/com.android.tethering/etc/bpf/net_shared/", |
| 66 | .prefix = "", |
| 67 | }, |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 68 | // Core operating system |
| 69 | { |
| 70 | .dir = "/system/etc/bpf/", |
| 71 | .prefix = "", |
| 72 | }, |
| 73 | }; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 74 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 75 | int loadAllElfObjects(const char* const progDir, const char* const prefix) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 76 | int retVal = 0; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 77 | DIR* dir; |
| 78 | struct dirent* ent; |
| 79 | |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 80 | if ((dir = opendir(progDir)) != NULL) { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 81 | while ((ent = readdir(dir)) != NULL) { |
| 82 | string s = ent->d_name; |
| 83 | if (!EndsWith(s, ".o")) continue; |
| 84 | |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 85 | string progPath(progDir); |
| 86 | progPath += s; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 87 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 88 | bool critical; |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 89 | int ret = android::bpf::loadProg(progPath.c_str(), &critical, prefix); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 90 | if (ret) { |
| 91 | if (critical) retVal = ret; |
| 92 | ALOGE("Failed to load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 93 | } else { |
| 94 | ALOGI("Loaded object: %s", progPath.c_str()); |
| 95 | } |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 96 | } |
| 97 | closedir(dir); |
| 98 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 99 | return retVal; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 102 | void createSysFsBpfSubDir(const char* const prefix) { |
| 103 | if (*prefix) { |
| 104 | mode_t prevUmask = umask(0); |
| 105 | |
| 106 | string s = "/sys/fs/bpf/"; |
| 107 | s += prefix; |
| 108 | |
| 109 | errno = 0; |
| 110 | int ret = mkdir(s.c_str(), S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 111 | if (ret && errno != EEXIST) { |
| 112 | ALOGW("Failed to create directory: %s, ret: %s", s.c_str(), std::strerror(errno)); |
| 113 | } |
| 114 | |
| 115 | umask(prevUmask); |
| 116 | } |
| 117 | } |
| 118 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 119 | int main() { |
Maciej Żenczykowski | 567dc56 | 2020-06-10 15:56:07 -0700 | [diff] [blame] | 120 | // Load all ELF objects, create programs and maps, and pin them |
Maciej Żenczykowski | d8a4578 | 2021-01-14 23:36:32 -0800 | [diff] [blame] | 121 | for (const auto location : locations) { |
| 122 | createSysFsBpfSubDir(location.prefix); |
| 123 | if (loadAllElfObjects(location.dir, location.prefix) != 0) { |
| 124 | ALOGE("=== CRITICAL FAILURE LOADING BPF PROGRAMS FROM %s ===", location.dir); |
Hungming Chen | 4b8e982 | 2020-09-10 15:51:59 +0800 | [diff] [blame] | 125 | ALOGE("If this triggers reliably, you're probably missing kernel options or patches."); |
| 126 | ALOGE("If this triggers randomly, you might be hitting some memory allocation " |
| 127 | "problems or startup script race."); |
| 128 | ALOGE("--- DO NOT EXPECT SYSTEM TO BOOT SUCCESSFULLY ---"); |
| 129 | sleep(20); |
| 130 | return 2; |
| 131 | } |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 132 | } |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 133 | |
| 134 | if (android::base::SetProperty("bpf.progs_loaded", "1") == false) { |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 135 | ALOGE("Failed to set bpf.progs_loaded property"); |
Joel Fernandes | d3ec871 | 2019-01-11 06:22:05 -0500 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | return 0; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 140 | } |