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 | |
| 41 | #include <android-base/stringprintf.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 42 | #include <android-base/strings.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 43 | #include <android-base/unique_fd.h> |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 44 | #include <libbpf_android.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 45 | #include <log/log.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 46 | #include <netdutils/Misc.h> |
| 47 | #include <netdutils/Slice.h> |
| 48 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 4c9e9ec | 2018-10-16 20:31:52 -0700 | [diff] [blame] | 49 | #include "netdbpf/bpf_shared.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; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 52 | using android::base::unique_fd; |
| 53 | using android::netdutils::Slice; |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 54 | using std::string; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 55 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 56 | #define BPF_PROG_PATH "/system/etc/bpf/" |
| 57 | #define BPF_PROG_SRC BPF_PROG_PATH "bpf_kern.o" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 58 | |
Yi Kong | 4a74569 | 2018-12-16 18:21:50 -0800 | [diff] [blame] | 59 | #define CLEANANDEXIT(ret, mapPatterns) \ |
| 60 | do { \ |
| 61 | for (size_t i = 0; i < mapPatterns.size(); i++) { \ |
| 62 | if (mapPatterns[i].fd > -1) { \ |
| 63 | close(mapPatterns[i].fd); \ |
| 64 | } \ |
| 65 | } \ |
| 66 | return ret; \ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 67 | } while (0) |
| 68 | |
| 69 | using android::bpf::BpfMapInfo; |
| 70 | using android::bpf::BpfProgInfo; |
| 71 | |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 72 | void loadAllElfObjects(void) { |
| 73 | DIR* dir; |
| 74 | struct dirent* ent; |
| 75 | |
| 76 | if ((dir = opendir(BPF_PROG_PATH)) != NULL) { |
| 77 | while ((ent = readdir(dir)) != NULL) { |
| 78 | string s = ent->d_name; |
| 79 | if (!EndsWith(s, ".o")) continue; |
| 80 | |
| 81 | string progPath = BPF_PROG_PATH + s; |
| 82 | |
| 83 | int ret = android::bpf::loadProg(progPath.c_str()); |
| 84 | ALOGI("Attempted load object: %s, ret: %s", progPath.c_str(), std::strerror(-ret)); |
| 85 | } |
| 86 | closedir(dir); |
| 87 | } |
| 88 | } |
| 89 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 90 | int main() { |
Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame^] | 91 | // Load all ELF objects, create programs and maps, and pin them |
| 92 | loadAllElfObjects(); |
| 93 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 94 | const std::vector<BpfMapInfo> mapPatterns = { |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 95 | BpfMapInfo(COOKIE_TAG_MAP, COOKIE_TAG_MAP_PATH), |
| 96 | BpfMapInfo(UID_COUNTERSET_MAP, UID_COUNTERSET_MAP_PATH), |
| 97 | BpfMapInfo(APP_UID_STATS_MAP, APP_UID_STATS_MAP_PATH), |
| 98 | BpfMapInfo(UID_STATS_MAP, UID_STATS_MAP_PATH), |
| 99 | BpfMapInfo(TAG_STATS_MAP, TAG_STATS_MAP_PATH), |
| 100 | BpfMapInfo(IFACE_STATS_MAP, IFACE_STATS_MAP_PATH), |
| 101 | BpfMapInfo(CONFIGURATION_MAP, CONFIGURATION_MAP_PATH), |
| 102 | BpfMapInfo(UID_OWNER_MAP, UID_OWNER_MAP_PATH), |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 103 | }; |
Yi Kong | 4a74569 | 2018-12-16 18:21:50 -0800 | [diff] [blame] | 104 | for (size_t i = 0; i < mapPatterns.size(); i++) { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 105 | if (mapPatterns[i].fd < 0) { |
| 106 | ALOGE("Rerieve Map from %s failed: %d", mapPatterns[i].path.c_str(), mapPatterns[i].fd); |
| 107 | CLEANANDEXIT(-1, mapPatterns); |
| 108 | } |
| 109 | } |
| 110 | BpfProgInfo programs[] = { |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 111 | {BPF_CGROUP_INET_EGRESS, BPF_EGRESS_PROG_PATH, BPF_CGROUP_EGRESS_PROG_NAME, |
| 112 | BPF_PROG_TYPE_CGROUP_SKB, unique_fd(-1)}, |
| 113 | {BPF_CGROUP_INET_INGRESS, BPF_INGRESS_PROG_PATH, BPF_CGROUP_INGRESS_PROG_NAME, |
| 114 | BPF_PROG_TYPE_CGROUP_SKB, unique_fd(-1)}, |
| 115 | {MAX_BPF_ATTACH_TYPE, XT_BPF_INGRESS_PROG_PATH, XT_BPF_INGRESS_PROG_NAME, |
| 116 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 117 | {MAX_BPF_ATTACH_TYPE, XT_BPF_EGRESS_PROG_PATH, XT_BPF_EGRESS_PROG_NAME, |
| 118 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 119 | {MAX_BPF_ATTACH_TYPE, XT_BPF_WHITELIST_PROG_PATH, XT_BPF_WHITELIST_PROG_NAME, |
| 120 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 121 | {MAX_BPF_ATTACH_TYPE, XT_BPF_BLACKLIST_PROG_PATH, XT_BPF_BLACKLIST_PROG_NAME, |
| 122 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 123 | int ret = android::bpf::parseProgramsFromFile(BPF_PROG_SRC, programs, ARRAY_SIZE(programs), |
| 124 | mapPatterns); |
| 125 | CLEANANDEXIT(ret, mapPatterns); |
| 126 | } |