Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [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 | |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <elf.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 19 | #include <error.h> |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <inttypes.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 22 | #include <linux/bpf.h> |
| 23 | #include <linux/unistd.h> |
| 24 | #include <net/if.h> |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 35 | |
| 36 | #include <android-base/stringprintf.h> |
| 37 | #include <android-base/unique_fd.h> |
| 38 | |
| 39 | #include <netdutils/Misc.h> |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 40 | #include <netdutils/Slice.h> |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 41 | #include "bpf/BpfUtils.h" |
| 42 | |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 43 | #include "bpf_shared.h" |
| 44 | |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 45 | using android::base::unique_fd; |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 46 | using android::netdutils::Slice; |
| 47 | |
| 48 | #define INGRESS_PROG "/system/bin/cgroup_bpf_ingress_prog" |
| 49 | #define EGRESS_PROG "/system/bin/cgroup_bpf_egress_prog" |
| 50 | #define MAP_LD_CMD_HEAD 0x18 |
| 51 | |
| 52 | #define FAIL(str) \ |
| 53 | do { \ |
| 54 | perror((str)); \ |
| 55 | return -1; \ |
| 56 | } while (0) |
| 57 | |
| 58 | // The BPF instruction bytes that we need to replace. x is a placeholder (e.g., COOKIE_TAG_MAP). |
| 59 | #define MAP_SEARCH_PATTERN(x) \ |
| 60 | { \ |
| 61 | 0x18, 0x01, 0x00, 0x00, \ |
| 62 | (x)[0], (x)[1], (x)[2], (x)[3], \ |
| 63 | 0x00, 0x00, 0x00, 0x00, \ |
| 64 | (x)[4], (x)[5], (x)[6], (x)[7] \ |
| 65 | } |
| 66 | |
| 67 | // The bytes we'll replace them with. x is the actual fd number for the map at runtime. |
| 68 | // The second byte is changed from 0x01 to 0x11 since 0x11 is the special command used |
| 69 | // for bpf map fd loading. The original 0x01 is only a normal load command. |
| 70 | #define MAP_REPLACE_PATTERN(x) \ |
| 71 | { \ |
| 72 | 0x18, 0x11, 0x00, 0x00, \ |
| 73 | (x)[0], (x)[1], (x)[2], (x)[3], \ |
| 74 | 0x00, 0x00, 0x00, 0x00, \ |
| 75 | (x)[4], (x)[5], (x)[6], (x)[7] \ |
| 76 | } |
| 77 | |
| 78 | #define MAP_CMD_SIZE 16 |
| 79 | #define LOG_BUF_SIZE 65536 |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 80 | |
| 81 | namespace android { |
| 82 | namespace bpf { |
| 83 | |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 84 | void makeFdReplacePattern(uint64_t code, uint64_t mapFd, char* pattern, char* cmd) { |
| 85 | char mapCode[sizeof(uint64_t)]; |
| 86 | char mapCmd[sizeof(uint64_t)]; |
| 87 | // The byte order is little endian for arm devices. |
| 88 | for (uint32_t i = 0; i < sizeof(uint64_t); i++) { |
| 89 | mapCode[i] = (code >> (i * 8)) & 0xFF; |
| 90 | mapCmd[i] = (mapFd >> (i * 8)) & 0xFF; |
| 91 | } |
| 92 | |
| 93 | char tmpPattern[] = MAP_SEARCH_PATTERN(mapCode); |
| 94 | memcpy(pattern, tmpPattern, MAP_CMD_SIZE); |
| 95 | char tmpCmd[] = MAP_REPLACE_PATTERN(mapCmd); |
| 96 | memcpy(cmd, tmpCmd, MAP_CMD_SIZE); |
| 97 | } |
| 98 | |
| 99 | int loadProg(const char* path, int cookieTagMap, int uidStatsMap, int tagStatsMap, |
| 100 | int uidCounterSetMap) { |
| 101 | int fd = open(path, O_RDONLY); |
| 102 | if (fd == -1) { |
| 103 | fprintf(stderr, "Failed to open %s program: %s", path, strerror(errno)); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | struct stat stat; |
| 108 | if (fstat(fd, &stat)) FAIL("Fail to get file size"); |
| 109 | |
| 110 | off_t fileLen = stat.st_size; |
| 111 | char* baseAddr = (char*)mmap(NULL, fileLen, PROT_READ, MAP_PRIVATE, fd, 0); |
| 112 | if (baseAddr == MAP_FAILED) FAIL("Failed to map the program into memory"); |
| 113 | |
| 114 | if ((uint32_t)fileLen < sizeof(Elf64_Ehdr)) FAIL("file size too small for Elf64_Ehdr"); |
| 115 | |
| 116 | Elf64_Ehdr* elf = (Elf64_Ehdr*)baseAddr; |
| 117 | |
| 118 | // Find section names string table. This is the section whose index is e_shstrndx. |
| 119 | if (elf->e_shstrndx == SHN_UNDEF || |
| 120 | elf->e_shoff + (elf->e_shstrndx + 1) * sizeof(Elf64_Shdr) > (uint32_t)fileLen) { |
| 121 | FAIL("cannot locate namesSection\n"); |
| 122 | } |
| 123 | |
| 124 | Elf64_Shdr* sections = (Elf64_Shdr*)(baseAddr + elf->e_shoff); |
| 125 | |
| 126 | Elf64_Shdr* namesSection = sections + elf->e_shstrndx; |
| 127 | |
| 128 | if (namesSection->sh_offset + namesSection->sh_size > (uint32_t)fileLen) |
| 129 | FAIL("namesSection out of bound\n"); |
| 130 | |
| 131 | const char* strTab = baseAddr + namesSection->sh_offset; |
| 132 | void* progSection = nullptr; |
| 133 | uint64_t progSize = 0; |
| 134 | for (int i = 0; i < elf->e_shnum; i++) { |
| 135 | Elf64_Shdr* section = sections + i; |
| 136 | if (((char*)section - baseAddr) + sizeof(Elf64_Shdr) > (uint32_t)fileLen) { |
| 137 | FAIL("next section is out of bound\n"); |
| 138 | } |
| 139 | |
| 140 | if (!strcmp(strTab + section->sh_name, BPF_PROG_SEC_NAME)) { |
| 141 | progSection = baseAddr + section->sh_offset; |
| 142 | progSize = (uint64_t)section->sh_size; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (!progSection) FAIL("program section not found"); |
| 148 | if ((char*)progSection - baseAddr + progSize > (uint32_t)fileLen) |
| 149 | FAIL("programSection out of bound\n"); |
| 150 | |
| 151 | char* prog = new char[progSize](); |
| 152 | memcpy(prog, progSection, progSize); |
| 153 | |
| 154 | char cookieTagMapFdPattern[MAP_CMD_SIZE]; |
| 155 | char cookieTagMapFdLoadByte[MAP_CMD_SIZE]; |
| 156 | makeFdReplacePattern(COOKIE_TAG_MAP, cookieTagMap, cookieTagMapFdPattern, |
| 157 | cookieTagMapFdLoadByte); |
| 158 | |
| 159 | char uidCounterSetMapFdPattern[MAP_CMD_SIZE]; |
| 160 | char uidCounterSetMapFdLoadByte[MAP_CMD_SIZE]; |
| 161 | makeFdReplacePattern(UID_COUNTERSET_MAP, uidCounterSetMap, uidCounterSetMapFdPattern, |
| 162 | uidCounterSetMapFdLoadByte); |
| 163 | |
| 164 | char tagStatsMapFdPattern[MAP_CMD_SIZE]; |
| 165 | char tagStatsMapFdLoadByte[MAP_CMD_SIZE]; |
| 166 | makeFdReplacePattern(TAG_STATS_MAP, tagStatsMap, tagStatsMapFdPattern, tagStatsMapFdLoadByte); |
| 167 | |
| 168 | char uidStatsMapFdPattern[MAP_CMD_SIZE]; |
| 169 | char uidStatsMapFdLoadByte[MAP_CMD_SIZE]; |
| 170 | makeFdReplacePattern(UID_STATS_MAP, uidStatsMap, uidStatsMapFdPattern, uidStatsMapFdLoadByte); |
| 171 | |
| 172 | char* mapHead = prog; |
| 173 | while ((uint64_t)(mapHead - prog + MAP_CMD_SIZE) < progSize) { |
| 174 | // Scan the program, examining all possible places that might be the start of a map load |
| 175 | // operation (i.e., all byes of value MAP_LD_CMD_HEAD). |
| 176 | // |
| 177 | // In each of these places, check whether it is the start of one of the patterns we want to |
| 178 | // replace, and if so, replace it. |
| 179 | mapHead = (char*)memchr(mapHead, MAP_LD_CMD_HEAD, progSize); |
| 180 | if (!mapHead) break; |
| 181 | if ((uint64_t)(mapHead - prog + MAP_CMD_SIZE) < progSize) { |
| 182 | if (!memcmp(mapHead, cookieTagMapFdPattern, MAP_CMD_SIZE)) { |
| 183 | memcpy(mapHead, cookieTagMapFdLoadByte, MAP_CMD_SIZE); |
| 184 | mapHead += MAP_CMD_SIZE; |
| 185 | } else if (!memcmp(mapHead, uidCounterSetMapFdPattern, MAP_CMD_SIZE)) { |
| 186 | memcpy(mapHead, uidCounterSetMapFdLoadByte, MAP_CMD_SIZE); |
| 187 | mapHead += MAP_CMD_SIZE; |
| 188 | } else if (!memcmp(mapHead, tagStatsMapFdPattern, MAP_CMD_SIZE)) { |
| 189 | memcpy(mapHead, tagStatsMapFdLoadByte, MAP_CMD_SIZE); |
| 190 | mapHead += MAP_CMD_SIZE; |
| 191 | } else if (!memcmp(mapHead, uidStatsMapFdPattern, MAP_CMD_SIZE)) { |
| 192 | memcpy(mapHead, uidStatsMapFdLoadByte, MAP_CMD_SIZE); |
| 193 | mapHead += MAP_CMD_SIZE; |
| 194 | } |
| 195 | } |
| 196 | mapHead++; |
| 197 | } |
| 198 | Slice insns = Slice(prog, progSize); |
| 199 | char bpf_log_buf[LOG_BUF_SIZE]; |
| 200 | Slice bpfLog = Slice(bpf_log_buf, sizeof(bpf_log_buf)); |
| 201 | return bpfProgLoad(BPF_PROG_TYPE_CGROUP_SKB, insns, "Apache 2.0", 0, bpfLog); |
| 202 | } |
| 203 | |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 204 | int loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 205 | const unique_fd& cookieTagMap, const unique_fd& uidCounterSetMap, |
| 206 | const unique_fd& uidStatsMap, const unique_fd& tagStatsMap) { |
| 207 | unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); |
| 208 | if (cg_fd < 0) { |
| 209 | perror("Failed to open the cgroup directory"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | unique_fd fd; |
| 214 | if (type == BPF_CGROUP_INET_EGRESS) { |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 215 | fd.reset(loadProg(INGRESS_PROG, cookieTagMap.get(), uidStatsMap.get(), tagStatsMap.get(), |
| 216 | uidCounterSetMap.get())); |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 217 | } else { |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 218 | fd.reset(loadProg(EGRESS_PROG, cookieTagMap.get(), uidStatsMap.get(), tagStatsMap.get(), |
| 219 | uidCounterSetMap.get())); |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (fd < 0) { |
| 223 | fprintf(stderr, "load %s failed: %s", name, strerror(errno)); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | int ret = attachProgram(type, fd, cg_fd); |
| 228 | if (ret) { |
| 229 | fprintf(stderr, "%s attach failed: %s", name, strerror(errno)); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | ret = mapPin(fd, path); |
| 234 | if (ret) { |
| 235 | fprintf(stderr, "Pin %s as file %s failed: %s", name, path, strerror(errno)); |
| 236 | return -1; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | } // namespace bpf |
| 242 | } // namespace android |
| 243 | |
| 244 | using android::bpf::BPF_EGRESS_PROG_PATH; |
| 245 | using android::bpf::BPF_INGRESS_PROG_PATH; |
| 246 | using android::bpf::COOKIE_UID_MAP_PATH; |
| 247 | using android::bpf::TAG_STATS_MAP_PATH; |
| 248 | using android::bpf::UID_COUNTERSET_MAP_PATH; |
| 249 | using android::bpf::UID_STATS_MAP_PATH; |
| 250 | |
| 251 | static void usage(void) { |
| 252 | fprintf(stderr, |
| 253 | "Usage: ./bpfloader [-i] [-e]\n" |
| 254 | " -i load ingress bpf program\n" |
| 255 | " -e load egress bpf program\n"); |
| 256 | } |
| 257 | |
| 258 | int main(int argc, char** argv) { |
| 259 | int ret = 0; |
| 260 | unique_fd cookieTagMap(android::bpf::mapRetrieve(COOKIE_UID_MAP_PATH, 0)); |
| 261 | if (cookieTagMap < 0) { |
| 262 | perror("Failed to get cookieTagMap"); |
| 263 | exit(-1); |
| 264 | } |
| 265 | |
| 266 | unique_fd uidCounterSetMap(android::bpf::mapRetrieve(UID_COUNTERSET_MAP_PATH, 0)); |
| 267 | if (uidCounterSetMap < 0) { |
| 268 | perror("Failed to get uidCounterSetMap"); |
| 269 | exit(-1); |
| 270 | } |
| 271 | |
| 272 | unique_fd uidStatsMap(android::bpf::mapRetrieve(UID_STATS_MAP_PATH, 0)); |
| 273 | if (uidStatsMap < 0) { |
| 274 | perror("Failed to get uidStatsMap"); |
| 275 | exit(-1); |
| 276 | } |
| 277 | |
| 278 | unique_fd tagStatsMap(android::bpf::mapRetrieve(TAG_STATS_MAP_PATH, 0)); |
| 279 | if (tagStatsMap < 0) { |
| 280 | perror("Failed to get tagStatsMap"); |
| 281 | exit(-1); |
| 282 | } |
| 283 | int opt; |
| 284 | bool doIngress = false, doEgress = false; |
| 285 | while ((opt = getopt(argc, argv, "ie")) != -1) { |
| 286 | switch (opt) { |
| 287 | case 'i': |
| 288 | doIngress = true; |
| 289 | break; |
| 290 | case 'e': |
| 291 | doEgress = true; |
| 292 | break; |
| 293 | default: |
| 294 | fprintf(stderr, "unknown argument %c", opt); |
| 295 | usage(); |
| 296 | exit(-1); |
| 297 | } |
| 298 | } |
| 299 | if (doIngress) { |
| 300 | ret = android::bpf::loadAndAttachProgram(BPF_CGROUP_INET_INGRESS, BPF_INGRESS_PROG_PATH, |
| 301 | "ingress_prog", cookieTagMap, uidCounterSetMap, |
| 302 | uidStatsMap, tagStatsMap); |
| 303 | if (ret) { |
| 304 | fprintf(stderr, "Failed to set up ingress program"); |
| 305 | return ret; |
| 306 | } |
| 307 | } |
| 308 | if (doEgress) { |
| 309 | ret = android::bpf::loadAndAttachProgram(BPF_CGROUP_INET_EGRESS, BPF_EGRESS_PROG_PATH, |
| 310 | "egress_prog", cookieTagMap, uidCounterSetMap, |
| 311 | uidStatsMap, tagStatsMap); |
| 312 | if (ret) { |
| 313 | fprintf(stderr, "Failed to set up ingress program"); |
| 314 | return ret; |
| 315 | } |
| 316 | } |
| 317 | return ret; |
| 318 | } |