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