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> |
| 22 | #include <elf.h> |
| 23 | #include <error.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <linux/bpf.h> |
| 27 | #include <linux/unistd.h> |
| 28 | #include <net/if.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <android-base/stringprintf.h> |
| 41 | #include <android-base/unique_fd.h> |
| 42 | #include <log/log.h> |
| 43 | |
| 44 | #include <netdutils/Misc.h> |
| 45 | #include <netdutils/Slice.h> |
| 46 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 4c9e9ec | 2018-10-16 20:31:52 -0700 | [diff] [blame] | 47 | #include "netdbpf/bpf_shared.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 48 | |
| 49 | using android::base::unique_fd; |
| 50 | using android::netdutils::Slice; |
| 51 | |
| 52 | #define BPF_PROG_PATH "/system/etc/bpf" |
| 53 | #define BPF_PROG_SRC BPF_PROG_PATH "/bpf_kern.o" |
| 54 | |
Yi Kong | cb8e0a0 | 2018-12-14 14:28:20 -0800 | [diff] [blame^] | 55 | #define CLEANANDEXIT(ret, mapPatterns) \ |
| 56 | do { \ |
| 57 | for (unsigned long i = 0; i < mapPatterns.size(); i++) { \ |
| 58 | if (mapPatterns[i].fd > -1) { \ |
| 59 | close(mapPatterns[i].fd); \ |
| 60 | } \ |
| 61 | } \ |
| 62 | return ret; \ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 63 | } while (0) |
| 64 | |
| 65 | using android::bpf::BpfMapInfo; |
| 66 | using android::bpf::BpfProgInfo; |
| 67 | |
| 68 | int main() { |
| 69 | const std::vector<BpfMapInfo> mapPatterns = { |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 70 | BpfMapInfo(COOKIE_TAG_MAP, COOKIE_TAG_MAP_PATH), |
| 71 | BpfMapInfo(UID_COUNTERSET_MAP, UID_COUNTERSET_MAP_PATH), |
| 72 | BpfMapInfo(APP_UID_STATS_MAP, APP_UID_STATS_MAP_PATH), |
| 73 | BpfMapInfo(UID_STATS_MAP, UID_STATS_MAP_PATH), |
| 74 | BpfMapInfo(TAG_STATS_MAP, TAG_STATS_MAP_PATH), |
| 75 | BpfMapInfo(IFACE_STATS_MAP, IFACE_STATS_MAP_PATH), |
| 76 | BpfMapInfo(CONFIGURATION_MAP, CONFIGURATION_MAP_PATH), |
| 77 | BpfMapInfo(UID_OWNER_MAP, UID_OWNER_MAP_PATH), |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 78 | }; |
Yi Kong | cb8e0a0 | 2018-12-14 14:28:20 -0800 | [diff] [blame^] | 79 | for (unsigned long i = 0; i < mapPatterns.size(); i++) { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 80 | if (mapPatterns[i].fd < 0) { |
| 81 | ALOGE("Rerieve Map from %s failed: %d", mapPatterns[i].path.c_str(), mapPatterns[i].fd); |
| 82 | CLEANANDEXIT(-1, mapPatterns); |
| 83 | } |
| 84 | } |
| 85 | BpfProgInfo programs[] = { |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 86 | {BPF_CGROUP_INET_EGRESS, BPF_EGRESS_PROG_PATH, BPF_CGROUP_EGRESS_PROG_NAME, |
| 87 | BPF_PROG_TYPE_CGROUP_SKB, unique_fd(-1)}, |
| 88 | {BPF_CGROUP_INET_INGRESS, BPF_INGRESS_PROG_PATH, BPF_CGROUP_INGRESS_PROG_NAME, |
| 89 | BPF_PROG_TYPE_CGROUP_SKB, unique_fd(-1)}, |
| 90 | {MAX_BPF_ATTACH_TYPE, XT_BPF_INGRESS_PROG_PATH, XT_BPF_INGRESS_PROG_NAME, |
| 91 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 92 | {MAX_BPF_ATTACH_TYPE, XT_BPF_EGRESS_PROG_PATH, XT_BPF_EGRESS_PROG_NAME, |
| 93 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 94 | {MAX_BPF_ATTACH_TYPE, XT_BPF_WHITELIST_PROG_PATH, XT_BPF_WHITELIST_PROG_NAME, |
| 95 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}, |
| 96 | {MAX_BPF_ATTACH_TYPE, XT_BPF_BLACKLIST_PROG_PATH, XT_BPF_BLACKLIST_PROG_NAME, |
| 97 | BPF_PROG_TYPE_SOCKET_FILTER, unique_fd(-1)}}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 98 | int ret = android::bpf::parseProgramsFromFile(BPF_PROG_SRC, programs, ARRAY_SIZE(programs), |
| 99 | mapPatterns); |
| 100 | CLEANANDEXIT(ret, mapPatterns); |
| 101 | } |