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 #include <linux/bpf.h>
18 #include <linux/if_ether.h>
19 #include <linux/in.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <sys/utsname.h>
26 #include <sstream>
27 #include <string>
28 
29 #include <android-base/properties.h>
30 #include <android-base/stringprintf.h>
31 #include <android-base/unique_fd.h>
32 #include <netdutils/Slice.h>
33 #include <netdutils/StatusOr.h>
34 #include "bpf/BpfUtils.h"
35 
36 using android::base::StringPrintf;
37 using android::base::unique_fd;
38 using android::base::GetUintProperty;
39 using android::netdutils::Slice;
40 using android::netdutils::statusFromErrno;
41 using android::netdutils::StatusOr;
42 
43 #define ptr_to_u64(x) ((uint64_t)(uintptr_t)x)
44 #define DEFAULT_LOG_LEVEL 1
45 
46 namespace android {
47 namespace bpf {
48 
49 /*  The bpf_attr is a union which might have a much larger size then the struct we are using, while
50  *  The inline initializer only reset the field we are using and leave the reset of the memory as
51  *  is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So
52  *  this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr.
53  */
operator ==(const StatsKey & lhs,const StatsKey & rhs)54 bool operator==(const StatsKey& lhs, const StatsKey& rhs) {
55     return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag) && (lhs.counterSet == rhs.counterSet) &&
56             (lhs.ifaceIndex == rhs.ifaceIndex));
57 }
58 
operator ==(const UidTag & lhs,const UidTag & rhs)59 bool operator==(const UidTag& lhs, const UidTag& rhs) {
60     return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag));
61 }
62 
operator ==(const StatsValue & lhs,const StatsValue & rhs)63 bool operator==(const StatsValue& lhs, const StatsValue& rhs) {
64     return ((lhs.rxBytes == rhs.rxBytes) && (lhs.txBytes == rhs.txBytes) &&
65             (lhs.rxPackets == rhs.rxPackets) && (lhs.txPackets == rhs.txPackets));
66 }
67 
bpf(int cmd,Slice bpfAttr)68 int bpf(int cmd, Slice bpfAttr) {
69     return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size());
70 }
71 
createMap(bpf_map_type map_type,uint32_t key_size,uint32_t value_size,uint32_t max_entries,uint32_t map_flags)72 int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
73               uint32_t map_flags) {
74     bpf_attr attr;
75     memset(&attr, 0, sizeof(attr));
76     attr.map_type = map_type;
77     attr.key_size = key_size;
78     attr.value_size = value_size;
79     attr.max_entries = max_entries;
80     attr.map_flags = map_flags;
81 
82     return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr)));
83 }
84 
writeToMapEntry(const base::unique_fd & map_fd,void * key,void * value,uint64_t flags)85 int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) {
86     bpf_attr attr;
87     memset(&attr, 0, sizeof(attr));
88     attr.map_fd = map_fd.get();
89     attr.key = ptr_to_u64(key);
90     attr.value = ptr_to_u64(value);
91     attr.flags = flags;
92 
93     return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr)));
94 }
95 
findMapEntry(const base::unique_fd & map_fd,void * key,void * value)96 int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) {
97     bpf_attr attr;
98     memset(&attr, 0, sizeof(attr));
99     attr.map_fd = map_fd.get();
100     attr.key = ptr_to_u64(key);
101     attr.value = ptr_to_u64(value);
102 
103     return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr)));
104 }
105 
deleteMapEntry(const base::unique_fd & map_fd,void * key)106 int deleteMapEntry(const base::unique_fd& map_fd, void* key) {
107     bpf_attr attr;
108     memset(&attr, 0, sizeof(attr));
109     attr.map_fd = map_fd.get();
110     attr.key = ptr_to_u64(key);
111 
112     return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr)));
113 }
114 
getNextMapKey(const base::unique_fd & map_fd,void * key,void * next_key)115 int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) {
116     bpf_attr attr;
117     memset(&attr, 0, sizeof(attr));
118     attr.map_fd = map_fd.get();
119     attr.key = ptr_to_u64(key);
120     attr.next_key = ptr_to_u64(next_key);
121 
122     return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
123 }
124 
getFirstMapKey(const base::unique_fd & map_fd,void * firstKey)125 int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
126     bpf_attr attr;
127     memset(&attr, 0, sizeof(attr));
128     attr.map_fd = map_fd.get();
129     attr.key = 0;
130     attr.next_key = ptr_to_u64(firstKey);
131 
132     return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
133 }
134 
bpfProgLoad(bpf_prog_type prog_type,Slice bpf_insns,const char * license,uint32_t kern_version,Slice bpf_log)135 int bpfProgLoad(bpf_prog_type prog_type, Slice bpf_insns, const char* license,
136                 uint32_t kern_version, Slice bpf_log) {
137     bpf_attr attr;
138     memset(&attr, 0, sizeof(attr));
139     attr.prog_type = prog_type;
140     attr.insns = ptr_to_u64(bpf_insns.base());
141     attr.insn_cnt = bpf_insns.size() / sizeof(struct bpf_insn);
142     attr.license = ptr_to_u64((void*)license);
143     attr.log_buf = ptr_to_u64(bpf_log.base());
144     attr.log_size = bpf_log.size();
145     attr.log_level = DEFAULT_LOG_LEVEL;
146     attr.kern_version = kern_version;
147     int ret = bpf(BPF_PROG_LOAD, Slice(&attr, sizeof(attr)));
148 
149     if (ret < 0) {
150         std::string prog_log = netdutils::toString(bpf_log);
151         std::istringstream iss(prog_log);
152         for (std::string line; std::getline(iss, line);) {
153             ALOGE("%s", line.c_str());
154         }
155     }
156     return ret;
157 }
158 
mapPin(const base::unique_fd & map_fd,const char * pathname)159 int mapPin(const base::unique_fd& map_fd, const char* pathname) {
160     bpf_attr attr;
161     memset(&attr, 0, sizeof(attr));
162     attr.pathname = ptr_to_u64((void*)pathname);
163     attr.bpf_fd = map_fd.get();
164 
165     return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr)));
166 }
167 
mapRetrieve(const char * pathname,uint32_t flag)168 int mapRetrieve(const char* pathname, uint32_t flag) {
169     bpf_attr attr;
170     memset(&attr, 0, sizeof(attr));
171     attr.pathname = ptr_to_u64((void*)pathname);
172     attr.file_flags = flag;
173     return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr)));
174 }
175 
attachProgram(bpf_attach_type type,uint32_t prog_fd,uint32_t cg_fd)176 int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
177     bpf_attr attr;
178     memset(&attr, 0, sizeof(attr));
179     attr.target_fd = cg_fd;
180     attr.attach_bpf_fd = prog_fd;
181     attr.attach_type = type;
182 
183     return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr)));
184 }
185 
detachProgram(bpf_attach_type type,uint32_t cg_fd)186 int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
187     bpf_attr attr;
188     memset(&attr, 0, sizeof(attr));
189     attr.target_fd = cg_fd;
190     attr.attach_type = type;
191 
192     return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr)));
193 }
194 
getSocketCookie(int sockFd)195 uint64_t getSocketCookie(int sockFd) {
196     uint64_t sock_cookie;
197     socklen_t cookie_len = sizeof(sock_cookie);
198     int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
199     if (res < 0) {
200         res = -errno;
201         ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
202         errno = -res;
203         // 0 is an invalid cookie. See sock_gen_cookie.
204         return NONEXISTENT_COOKIE;
205     }
206     return sock_cookie;
207 }
208 
hasBpfSupport()209 bool hasBpfSupport() {
210     struct utsname buf;
211     int kernel_version_major;
212     int kernel_version_minor;
213 
214     uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
215     if (api_level == 0) {
216         ALOGE("Cannot determine initial API level of the device");
217         api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
218     }
219 
220     int ret = uname(&buf);
221     if (ret) {
222         return false;
223     }
224     char dummy;
225     ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
226     if (ret >= 2 && ((kernel_version_major > 4) ||
227                          (kernel_version_major == 4 && kernel_version_minor >= 9))) {
228         // Check if the device is shipped originally with android P.
229         return api_level >= MINIMUM_API_REQUIRED;
230     }
231     return false;
232 }
233 
234 }  // namespace bpf
235 }  // namespace android
236