blob: f5741bb489691c4ed558eb4ab817cea3eee2668d [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#define LOG_TAG "BpfUtils"
18
Bernie Innocenti26ffded2018-10-19 15:41:53 +090019#include "bpf/BpfUtils.h"
20
Chenbo Feng75b410b2018-10-10 15:01:19 -070021#include <elf.h>
22#include <inttypes.h>
23#include <linux/bpf.h>
24#include <linux/if_ether.h>
25#include <linux/in.h>
Chenbo Feng9cd8f142018-12-04 16:54:56 -080026#include <linux/pfkeyv2.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070027#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/socket.h>
31#include <sys/stat.h>
32#include <sys/utsname.h>
33#include <sstream>
34#include <string>
35
36#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070037#include <android-base/unique_fd.h>
Bernie Innocenti26ffded2018-10-19 15:41:53 +090038#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070039#include <netdutils/MemBlock.h>
40#include <netdutils/Slice.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070041
42using android::base::GetUintProperty;
Chenbo Feng75b410b2018-10-10 15:01:19 -070043using android::base::unique_fd;
44using android::netdutils::MemBlock;
45using android::netdutils::Slice;
Chenbo Feng75b410b2018-10-10 15:01:19 -070046
Chenbo Feng9cd8f142018-12-04 16:54:56 -080047// The buffer size for the buffer that records program loading logs, needs to be large enough for
48// the largest kernel program.
49constexpr size_t LOG_BUF_SIZE = 0x20000;
Chenbo Feng75b410b2018-10-10 15:01:19 -070050
51namespace android {
52namespace bpf {
53
54/* The bpf_attr is a union which might have a much larger size then the struct we are using, while
55 * The inline initializer only reset the field we are using and leave the reset of the memory as
56 * is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So
57 * this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr.
58 */
59bool operator==(const StatsKey& lhs, const StatsKey& rhs) {
60 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag) && (lhs.counterSet == rhs.counterSet) &&
61 (lhs.ifaceIndex == rhs.ifaceIndex));
62}
63
64bool operator==(const UidTag& lhs, const UidTag& rhs) {
65 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag));
66}
67
68bool operator==(const StatsValue& lhs, const StatsValue& rhs) {
69 return ((lhs.rxBytes == rhs.rxBytes) && (lhs.txBytes == rhs.txBytes) &&
70 (lhs.rxPackets == rhs.rxPackets) && (lhs.txPackets == rhs.txPackets));
71}
72
73int bpf(int cmd, Slice bpfAttr) {
74 return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size());
75}
76
77int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
78 uint32_t map_flags) {
79 bpf_attr attr;
80 memset(&attr, 0, sizeof(attr));
81 attr.map_type = map_type;
82 attr.key_size = key_size;
83 attr.value_size = value_size;
84 attr.max_entries = max_entries;
85 attr.map_flags = map_flags;
86
87 return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr)));
88}
89
90int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) {
91 bpf_attr attr;
92 memset(&attr, 0, sizeof(attr));
93 attr.map_fd = map_fd.get();
94 attr.key = ptr_to_u64(key);
95 attr.value = ptr_to_u64(value);
96 attr.flags = flags;
97
98 return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr)));
99}
100
101int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) {
102 bpf_attr attr;
103 memset(&attr, 0, sizeof(attr));
104 attr.map_fd = map_fd.get();
105 attr.key = ptr_to_u64(key);
106 attr.value = ptr_to_u64(value);
107
108 return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr)));
109}
110
111int deleteMapEntry(const base::unique_fd& map_fd, void* key) {
112 bpf_attr attr;
113 memset(&attr, 0, sizeof(attr));
114 attr.map_fd = map_fd.get();
115 attr.key = ptr_to_u64(key);
116
117 return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr)));
118}
119
120int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) {
121 bpf_attr attr;
122 memset(&attr, 0, sizeof(attr));
123 attr.map_fd = map_fd.get();
124 attr.key = ptr_to_u64(key);
125 attr.next_key = ptr_to_u64(next_key);
126
127 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
128}
129
130int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
131 bpf_attr attr;
132 memset(&attr, 0, sizeof(attr));
133 attr.map_fd = map_fd.get();
134 attr.key = 0;
135 attr.next_key = ptr_to_u64(firstKey);
136
137 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
138}
139
140int bpfProgLoad(bpf_prog_type prog_type, Slice bpf_insns, const char* license,
141 uint32_t kern_version, Slice bpf_log) {
142 bpf_attr attr;
143 memset(&attr, 0, sizeof(attr));
144 attr.prog_type = prog_type;
145 attr.insns = ptr_to_u64(bpf_insns.base());
146 attr.insn_cnt = bpf_insns.size() / sizeof(struct bpf_insn);
147 attr.license = ptr_to_u64((void*)license);
148 attr.log_buf = ptr_to_u64(bpf_log.base());
149 attr.log_size = bpf_log.size();
150 attr.log_level = DEFAULT_LOG_LEVEL;
151 attr.kern_version = kern_version;
152 int ret = bpf(BPF_PROG_LOAD, Slice(&attr, sizeof(attr)));
153
154 if (ret < 0) {
155 std::string prog_log = netdutils::toString(bpf_log);
156 std::istringstream iss(prog_log);
157 for (std::string line; std::getline(iss, line);) {
158 ALOGE("%s", line.c_str());
159 }
160 }
161 return ret;
162}
163
164int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) {
165 bpf_attr attr;
166 memset(&attr, 0, sizeof(attr));
167 attr.pathname = ptr_to_u64((void*)pathname);
168 attr.bpf_fd = map_fd.get();
169
170 return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr)));
171}
172
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800173int bpfFdGet(const char* pathname, uint32_t flag) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700174 bpf_attr attr;
175 memset(&attr, 0, sizeof(attr));
176 attr.pathname = ptr_to_u64((void*)pathname);
177 attr.file_flags = flag;
178 return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr)));
179}
180
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800181int mapRetrieve(const char* pathname, uint32_t flag) {
182 return bpfFdGet(pathname, flag);
183}
184
Chenbo Feng75b410b2018-10-10 15:01:19 -0700185int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
186 bpf_attr attr;
187 memset(&attr, 0, sizeof(attr));
188 attr.target_fd = cg_fd;
189 attr.attach_bpf_fd = prog_fd;
190 attr.attach_type = type;
191
192 return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr)));
193}
194
195int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
196 bpf_attr attr;
197 memset(&attr, 0, sizeof(attr));
198 attr.target_fd = cg_fd;
199 attr.attach_type = type;
200
201 return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr)));
202}
203
204uint64_t getSocketCookie(int sockFd) {
205 uint64_t sock_cookie;
206 socklen_t cookie_len = sizeof(sock_cookie);
207 int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
208 if (res < 0) {
209 res = -errno;
210 ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
211 errno = -res;
212 // 0 is an invalid cookie. See sock_gen_cookie.
213 return NONEXISTENT_COOKIE;
214 }
215 return sock_cookie;
216}
217
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800218int synchronizeKernelRCU() {
219 // This is a temporary hack for network stats map swap on devices running
220 // 4.9 kernels. The kernel code of socket release on pf_key socket will
221 // explicitly call synchronize_rcu() which is exactly what we need.
222 int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
223
224 if (pfSocket < 0) {
225 int ret = -errno;
226 ALOGE("create PF_KEY socket failed: %s", strerror(errno));
227 return ret;
228 }
229
230 // When closing socket, synchronize_rcu() gets called in sock_release().
231 if (close(pfSocket)) {
232 int ret = -errno;
233 ALOGE("failed to close the PF_KEY socket: %s", strerror(errno));
234 return ret;
235 }
236 return 0;
237}
238
Chenbo Feng75b410b2018-10-10 15:01:19 -0700239bool hasBpfSupport() {
240 struct utsname buf;
241 int kernel_version_major;
242 int kernel_version_minor;
243
244 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
245 if (api_level == 0) {
246 ALOGE("Cannot determine initial API level of the device");
247 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
248 }
249
250 int ret = uname(&buf);
251 if (ret) {
252 return false;
253 }
254 char dummy;
255 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800256 if (ret >= 2 &&
257 ((kernel_version_major > 4) || (kernel_version_major == 4 && kernel_version_minor >= 9))) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700258 // Check if the device is shipped originally with android P.
259 return api_level >= MINIMUM_API_REQUIRED;
260 }
261 return false;
262}
263
264int loadAndPinProgram(BpfProgInfo* prog, Slice progBlock) {
265 // Program doesn't exist. Try to load it.
266 char bpf_log_buf[LOG_BUF_SIZE];
267 Slice bpfLog = Slice(bpf_log_buf, sizeof(bpf_log_buf));
268 prog->fd.reset(bpfProgLoad(prog->loadType, progBlock, "Apache 2.0", 0, bpfLog));
269 if (prog->fd < 0) {
270 int ret = -errno;
271 ALOGE("load %s failed: %s", prog->name, strerror(errno));
272 return ret;
273 }
274 if (prog->attachType == BPF_CGROUP_INET_EGRESS || prog->attachType == BPF_CGROUP_INET_INGRESS) {
275 unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
276 if (cg_fd < 0) {
277 int ret = -errno;
278 ALOGE("Failed to open the cgroup directory");
279 return ret;
280 }
281 int ret = android::bpf::attachProgram(prog->attachType, prog->fd, cg_fd);
282 if (ret) {
283 ret = -errno;
284 ALOGE("%s attach failed: %s", prog->name, strerror(errno));
285 return ret;
286 }
287 }
288 if (prog->path) {
289 int ret = android::bpf::bpfFdPin(prog->fd, prog->path);
290 if (ret) {
291 ret = -errno;
292 ALOGE("Pin %s as file %s failed: %s", prog->name, prog->path, strerror(errno));
293 return ret;
294 }
295 }
296 return 0;
297}
298
299int extractAndLoadProg(BpfProgInfo* prog, Elf64_Shdr* sectionPtr, Slice fileContents,
300 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800301 uint64_t progSize = (uint64_t)sectionPtr->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700302 Slice progSection = take(drop(fileContents, sectionPtr->sh_offset), progSize);
303 if (progSection.size() < progSize) {
304 ALOGE("programSection out of bound");
305 return -EINVAL;
306 }
307 MemBlock progCopy(progSection);
308 if (progCopy.get().size() != progSize) {
309 ALOGE("program cannot be extracted");
310 return -EINVAL;
311 }
312 Slice remaining = progCopy.get();
313 while (remaining.size() >= MAP_CMD_SIZE) {
314 // Scan the program, examining all possible places that might be the start of a
315 // map load operation (i.e., all bytes of value MAP_LD_CMD_HEAD).
316 // In each of these places, check whether it is the start of one of the patterns
317 // we want to replace, and if so, replace it.
318 Slice mapHead = findFirstMatching(remaining, MAP_LD_CMD_HEAD);
319 if (mapHead.size() < MAP_CMD_SIZE) break;
320 bool replaced = false;
321 for (const auto& pattern : mapPatterns) {
322 if (!memcmp(mapHead.base(), pattern.search.data(), MAP_CMD_SIZE)) {
323 memcpy(mapHead.base(), pattern.replace.data(), MAP_CMD_SIZE);
324 replaced = true;
325 break;
326 }
327 }
328 remaining = drop(mapHead, replaced ? MAP_CMD_SIZE : sizeof(uint8_t));
329 }
330 if (!(prog->path) || access(prog->path, R_OK) == -1) {
331 return loadAndPinProgram(prog, progCopy.get());
332 }
333 return 0;
334}
335
336int parsePrograms(Slice fileContents, BpfProgInfo* programs, size_t size,
337 const std::vector<BpfMapInfo>& mapPatterns) {
338 Slice elfHeader = take(fileContents, sizeof(Elf64_Ehdr));
339 if (elfHeader.size() < sizeof(Elf64_Ehdr)) {
340 ALOGE("bpf fileContents does not have complete elf header");
341 return -EINVAL;
342 }
343
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800344 Elf64_Ehdr* elf = (Elf64_Ehdr*)elfHeader.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700345 // Find section names string table. This is the section whose index is e_shstrndx.
346 if (elf->e_shstrndx == SHN_UNDEF) {
347 ALOGE("cannot locate namesSection\n");
348 return -EINVAL;
349 }
350 size_t totalSectionSize = (elf->e_shnum) * sizeof(Elf64_Shdr);
351 Slice sections = take(drop(fileContents, elf->e_shoff), totalSectionSize);
352 if (sections.size() < totalSectionSize) {
353 ALOGE("sections corrupted");
354 return -EMSGSIZE;
355 }
356
357 Slice namesSection =
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800358 take(drop(sections, elf->e_shstrndx * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700359 if (namesSection.size() != sizeof(Elf64_Shdr)) {
360 ALOGE("namesSection corrupted");
361 return -EMSGSIZE;
362 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800363 size_t strTabOffset = ((Elf64_Shdr*)namesSection.base())->sh_offset;
364 size_t strTabSize = ((Elf64_Shdr*)namesSection.base())->sh_size;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700365
366 Slice strTab = take(drop(fileContents, strTabOffset), strTabSize);
367 if (strTab.size() < strTabSize) {
368 ALOGE("string table out of bound\n");
369 return -EMSGSIZE;
370 }
371
372 for (int i = 0; i < elf->e_shnum; i++) {
373 Slice section = take(drop(sections, i * sizeof(Elf64_Shdr)), sizeof(Elf64_Shdr));
374 if (section.size() < sizeof(Elf64_Shdr)) {
375 ALOGE("section %d is out of bound, section size: %zu, header size: %zu, total size: "
376 "%zu",
377 i, section.size(), sizeof(Elf64_Shdr), sections.size());
378 return -EBADF;
379 }
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800380 Elf64_Shdr* sectionPtr = (Elf64_Shdr*)section.base();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700381 Slice nameSlice = drop(strTab, sectionPtr->sh_name);
382 if (nameSlice.size() == 0) {
383 ALOGE("nameSlice out of bound, i: %d, strTabSize: %zu, sh_name: %u", i, strTabSize,
384 sectionPtr->sh_name);
385 return -EBADF;
386 }
387 for (size_t i = 0; i < size; i++) {
388 BpfProgInfo* prog = programs + i;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800389 if (!strcmp((char*)nameSlice.base(), prog->name)) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700390 int ret = extractAndLoadProg(prog, sectionPtr, fileContents, mapPatterns);
391 if (ret) return ret;
392 }
393 }
394 }
395
396 // Check all the program struct passed in to make sure they all have a valid fd.
397 for (size_t i = 0; i < size; i++) {
398 BpfProgInfo* prog = programs + i;
399 if (access(prog->path, R_OK) == -1) {
400 ALOGE("Load program %s failed", prog->name);
401 return -EINVAL;
402 }
403 }
404 return 0;
405}
406
407int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
408 const std::vector<BpfMapInfo>& mapPatterns) {
Chenbo Feng4c9e9ec2018-10-16 20:31:52 -0700409 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700410 int ret;
411 if (fd < 0) {
412 ret = -errno;
413 ALOGE("Failed to open %s program: %s", path, strerror(errno));
414 return ret;
415 }
416
417 struct stat stat;
418 if (fstat(fd.get(), &stat)) {
419 ret = -errno;
420 ALOGE("Failed to get file (%s) size: %s", path, strerror(errno));
421 return ret;
422 }
423
424 off_t fileLen = stat.st_size;
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800425 char* baseAddr = (char*)mmap(NULL, fileLen, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd.get(), 0);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700426 if (baseAddr == MAP_FAILED) {
427 ALOGE("Failed to map the program (%s) into memory: %s", path, strerror(errno));
428 ret = -errno;
429 return ret;
430 }
431
432 ret = parsePrograms(Slice(baseAddr, fileLen), programs, size, mapPatterns);
433
434 munmap(baseAddr, fileLen);
435 return ret;
436}
437
438} // namespace bpf
439} // namespace android