blob: bde12f995fe11ffc3d9aa20345a8c05bfcb8d696 [file] [log] [blame]
Chenbo Feng2236e1b2019-02-26 14:30:19 -08001/*
2 * Copyright (C) 2018 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
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080017#include <bpf_helpers.h>
Chenbo Feng2236e1b2019-02-26 14:30:19 -080018#include <linux/bpf.h>
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080019#include <linux/if.h>
20#include <linux/if_ether.h>
Maciej Żenczykowskic6c8d4f2019-09-19 08:28:19 -070021#include <linux/if_packet.h>
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080022#include <linux/in.h>
23#include <linux/in6.h>
24#include <linux/ip.h>
25#include <linux/ipv6.h>
26#include <stdbool.h>
27#include <stdint.h>
28#include "bpf_net_helpers.h"
29#include "netdbpf/bpf_shared.h"
30
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080031// This is defined for cgroup bpf filter only.
32#define BPF_PASS 1
33#define BPF_DROP 0
34
35// This is used for xt_bpf program only.
36#define BPF_NOMATCH 0
37#define BPF_MATCH 1
38
39#define BPF_EGRESS 0
40#define BPF_INGRESS 1
41
42#define IP_PROTO_OFF offsetof(struct iphdr, protocol)
43#define IPV6_PROTO_OFF offsetof(struct ipv6hdr, nexthdr)
44#define IPPROTO_IHL_OFF 0
45#define TCP_FLAG_OFF 13
46#define RST_OFFSET 2
47
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -080048DEFINE_BPF_MAP(cookie_tag_map, HASH, uint64_t, UidTagValue, COOKIE_UID_MAP_SIZE)
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080049DEFINE_BPF_MAP(uid_counterset_map, HASH, uint32_t, uint8_t, UID_COUNTERSET_MAP_SIZE)
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -080050DEFINE_BPF_MAP(app_uid_stats_map, HASH, uint32_t, StatsValue, APP_STATS_MAP_SIZE)
51DEFINE_BPF_MAP(stats_map_A, HASH, StatsKey, StatsValue, STATS_MAP_SIZE)
52DEFINE_BPF_MAP(stats_map_B, HASH, StatsKey, StatsValue, STATS_MAP_SIZE)
53DEFINE_BPF_MAP(iface_stats_map, HASH, uint32_t, StatsValue, IFACE_STATS_MAP_SIZE)
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080054DEFINE_BPF_MAP(configuration_map, HASH, uint32_t, uint8_t, CONFIGURATION_MAP_SIZE)
55DEFINE_BPF_MAP(uid_owner_map, HASH, uint32_t, UidOwnerValue, UID_OWNER_MAP_SIZE)
56
57/* never actually used from ebpf */
58DEFINE_BPF_MAP_NO_ACCESSORS(iface_index_name_map, HASH, uint32_t, IfaceValue,
59 IFACE_INDEX_NAME_MAP_SIZE)
60
61static __always_inline int is_system_uid(uint32_t uid) {
62 return (uid <= MAX_SYSTEM_UID) && (uid >= MIN_SYSTEM_UID);
63}
64
65#define DEFINE_UPDATE_STATS(the_stats_map, TypeOfKey) \
66 static __always_inline inline void update_##the_stats_map(struct __sk_buff* skb, \
67 int direction, TypeOfKey* key) { \
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -080068 StatsValue* value; \
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080069 value = bpf_##the_stats_map##_lookup_elem(key); \
70 if (!value) { \
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -080071 StatsValue newValue = {}; \
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080072 bpf_##the_stats_map##_update_elem(key, &newValue, BPF_NOEXIST); \
73 value = bpf_##the_stats_map##_lookup_elem(key); \
74 } \
75 if (value) { \
76 if (direction == BPF_EGRESS) { \
77 __sync_fetch_and_add(&value->txPackets, 1); \
78 __sync_fetch_and_add(&value->txBytes, skb->len); \
79 } else if (direction == BPF_INGRESS) { \
80 __sync_fetch_and_add(&value->rxPackets, 1); \
81 __sync_fetch_and_add(&value->rxBytes, skb->len); \
82 } \
83 } \
84 }
85
86DEFINE_UPDATE_STATS(app_uid_stats_map, uint32_t)
87DEFINE_UPDATE_STATS(iface_stats_map, uint32_t)
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -080088DEFINE_UPDATE_STATS(stats_map_A, StatsKey)
89DEFINE_UPDATE_STATS(stats_map_B, StatsKey)
Maciej Żenczykowskib601c042019-12-30 04:15:53 -080090
91static inline bool skip_owner_match(struct __sk_buff* skb) {
92 int offset = -1;
93 int ret = 0;
94 if (skb->protocol == htons(ETH_P_IP)) {
95 offset = IP_PROTO_OFF;
96 uint8_t proto, ihl;
97 uint16_t flag;
98 ret = bpf_skb_load_bytes(skb, offset, &proto, 1);
99 if (!ret) {
100 if (proto == IPPROTO_ESP) {
101 return true;
102 } else if (proto == IPPROTO_TCP) {
103 ret = bpf_skb_load_bytes(skb, IPPROTO_IHL_OFF, &ihl, 1);
104 ihl = ihl & 0x0F;
105 ret = bpf_skb_load_bytes(skb, ihl * 4 + TCP_FLAG_OFF, &flag, 1);
106 if (ret == 0 && (flag >> RST_OFFSET & 1)) {
107 return true;
108 }
109 }
110 }
111 } else if (skb->protocol == htons(ETH_P_IPV6)) {
112 offset = IPV6_PROTO_OFF;
113 uint8_t proto;
114 ret = bpf_skb_load_bytes(skb, offset, &proto, 1);
115 if (!ret) {
116 if (proto == IPPROTO_ESP) {
117 return true;
118 } else if (proto == IPPROTO_TCP) {
119 uint16_t flag;
120 ret = bpf_skb_load_bytes(skb, sizeof(struct ipv6hdr) + TCP_FLAG_OFF, &flag, 1);
121 if (ret == 0 && (flag >> RST_OFFSET & 1)) {
122 return true;
123 }
124 }
125 }
126 }
127 return false;
128}
129
130static __always_inline BpfConfig getConfig(uint32_t configKey) {
131 uint32_t mapSettingKey = configKey;
132 BpfConfig* config = bpf_configuration_map_lookup_elem(&mapSettingKey);
133 if (!config) {
134 // Couldn't read configuration entry. Assume everything is disabled.
135 return DEFAULT_CONFIG;
136 }
137 return *config;
138}
139
140static inline int bpf_owner_match(struct __sk_buff* skb, uint32_t uid, int direction) {
141 if (skip_owner_match(skb)) return BPF_PASS;
142
143 if ((uid <= MAX_SYSTEM_UID) && (uid >= MIN_SYSTEM_UID)) return BPF_PASS;
144
145 BpfConfig enabledRules = getConfig(UID_RULES_CONFIGURATION_KEY);
146
147 UidOwnerValue* uidEntry = bpf_uid_owner_map_lookup_elem(&uid);
148 uint8_t uidRules = uidEntry ? uidEntry->rule : 0;
149 uint32_t allowed_iif = uidEntry ? uidEntry->iif : 0;
150
151 if (enabledRules) {
152 if ((enabledRules & DOZABLE_MATCH) && !(uidRules & DOZABLE_MATCH)) {
153 return BPF_DROP;
154 }
155 if ((enabledRules & STANDBY_MATCH) && (uidRules & STANDBY_MATCH)) {
156 return BPF_DROP;
157 }
158 if ((enabledRules & POWERSAVE_MATCH) && !(uidRules & POWERSAVE_MATCH)) {
159 return BPF_DROP;
160 }
161 }
162 if (direction == BPF_INGRESS && (uidRules & IIF_MATCH)) {
163 // Drops packets not coming from lo nor the whitelisted interface
164 if (allowed_iif && skb->ifindex != 1 && skb->ifindex != allowed_iif) {
165 return BPF_DROP;
166 }
167 }
168 return BPF_PASS;
169}
170
171static __always_inline inline void update_stats_with_config(struct __sk_buff* skb, int direction,
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800172 StatsKey* key, uint8_t selectedMap) {
Maciej Żenczykowskib601c042019-12-30 04:15:53 -0800173 if (selectedMap == SELECT_MAP_A) {
174 update_stats_map_A(skb, direction, key);
175 } else if (selectedMap == SELECT_MAP_B) {
176 update_stats_map_B(skb, direction, key);
177 }
178}
179
180static __always_inline inline int bpf_traffic_account(struct __sk_buff* skb, int direction) {
181 uint32_t sock_uid = bpf_get_socket_uid(skb);
182 int match = bpf_owner_match(skb, sock_uid, direction);
183 if ((direction == BPF_EGRESS) && (match == BPF_DROP)) {
184 // If an outbound packet is going to be dropped, we do not count that
185 // traffic.
186 return match;
187 }
188
189 uint64_t cookie = bpf_get_socket_cookie(skb);
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800190 UidTagValue* utag = bpf_cookie_tag_map_lookup_elem(&cookie);
Maciej Żenczykowskib601c042019-12-30 04:15:53 -0800191 uint32_t uid, tag;
192 if (utag) {
193 uid = utag->uid;
194 tag = utag->tag;
195 } else {
196 uid = sock_uid;
197 tag = 0;
198 }
199
Maciej Żenczykowski11ec78b2019-12-30 06:39:32 -0800200 StatsKey key = {.uid = uid, .tag = tag, .counterSet = 0, .ifaceIndex = skb->ifindex};
Maciej Żenczykowskib601c042019-12-30 04:15:53 -0800201
202 uint8_t* counterSet = bpf_uid_counterset_map_lookup_elem(&uid);
203 if (counterSet) key.counterSet = (uint32_t)*counterSet;
204
205 uint32_t mapSettingKey = CURRENT_STATS_MAP_CONFIGURATION_KEY;
206 uint8_t* selectedMap = bpf_configuration_map_lookup_elem(&mapSettingKey);
207 if (!selectedMap) {
208 return match;
209 }
210
211 if (key.tag) {
212 update_stats_with_config(skb, direction, &key, *selectedMap);
213 key.tag = 0;
214 }
215
216 update_stats_with_config(skb, direction, &key, *selectedMap);
217 update_app_uid_stats_map(skb, direction, &uid);
218 return match;
219}
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800220
221SEC("cgroupskb/ingress/stats")
222int bpf_cgroup_ingress(struct __sk_buff* skb) {
223 return bpf_traffic_account(skb, BPF_INGRESS);
224}
225
226SEC("cgroupskb/egress/stats")
227int bpf_cgroup_egress(struct __sk_buff* skb) {
228 return bpf_traffic_account(skb, BPF_EGRESS);
229}
230
231SEC("skfilter/egress/xtbpf")
232int xt_bpf_egress_prog(struct __sk_buff* skb) {
233 uint32_t key = skb->ifindex;
Maciej Żenczykowski0f24d322019-04-19 17:51:33 -0700234 update_iface_stats_map(skb, BPF_EGRESS, &key);
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800235 return BPF_MATCH;
236}
237
238SEC("skfilter/ingress/xtbpf")
239int xt_bpf_ingress_prog(struct __sk_buff* skb) {
240 uint32_t key = skb->ifindex;
Maciej Żenczykowski0f24d322019-04-19 17:51:33 -0700241 update_iface_stats_map(skb, BPF_INGRESS, &key);
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800242 return BPF_MATCH;
243}
244
245SEC("skfilter/whitelist/xtbpf")
246int xt_bpf_whitelist_prog(struct __sk_buff* skb) {
247 uint32_t sock_uid = bpf_get_socket_uid(skb);
248 if (is_system_uid(sock_uid)) return BPF_MATCH;
Maciej Żenczykowskic6c8d4f2019-09-19 08:28:19 -0700249
250 // 65534 is the overflow 'nobody' uid, usually this being returned means
251 // that skb->sk is NULL during RX (early decap socket lookup failure),
252 // which commonly happens for incoming packets to an unconnected udp socket.
253 // Additionally bpf_get_socket_cookie() returns 0 if skb->sk is NULL
Maciej Żenczykowski0b60d602019-10-30 23:51:34 -0700254 if ((sock_uid == 65534) && !bpf_get_socket_cookie(skb) && is_received_skb(skb))
Maciej Żenczykowskic6c8d4f2019-09-19 08:28:19 -0700255 return BPF_MATCH;
256
Maciej Żenczykowskib8a7be52019-04-19 21:47:08 -0700257 UidOwnerValue* whitelistMatch = bpf_uid_owner_map_lookup_elem(&sock_uid);
Maciej Żenczykowskic6c8d4f2019-09-19 08:28:19 -0700258 if (whitelistMatch) return whitelistMatch->rule & HAPPY_BOX_MATCH ? BPF_MATCH : BPF_NOMATCH;
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800259 return BPF_NOMATCH;
260}
261
262SEC("skfilter/blacklist/xtbpf")
263int xt_bpf_blacklist_prog(struct __sk_buff* skb) {
264 uint32_t sock_uid = bpf_get_socket_uid(skb);
Maciej Żenczykowskib8a7be52019-04-19 21:47:08 -0700265 UidOwnerValue* blacklistMatch = bpf_uid_owner_map_lookup_elem(&sock_uid);
Maciej Żenczykowskic6c8d4f2019-09-19 08:28:19 -0700266 if (blacklistMatch) return blacklistMatch->rule & PENALTY_BOX_MATCH ? BPF_MATCH : BPF_NOMATCH;
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800267 return BPF_NOMATCH;
268}
269
Maciej Żenczykowskib8a7be52019-04-19 21:47:08 -0700270DEFINE_BPF_MAP(uid_permission_map, HASH, uint32_t, uint8_t, UID_OWNER_MAP_SIZE)
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800271
Chenbo Fengbf660aa2019-02-26 16:12:27 -0800272SEC("cgroupsock/inet/create")
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800273int inet_socket_create(struct bpf_sock* sk) {
274 uint64_t gid_uid = bpf_get_current_uid_gid();
275 /*
276 * A given app is guaranteed to have the same app ID in all the profiles in
277 * which it is installed, and install permission is granted to app for all
278 * user at install time so we only check the appId part of a request uid at
279 * run time. See UserHandle#isSameApp for detail.
280 */
281 uint32_t appId = (gid_uid & 0xffffffff) % PER_USER_RANGE;
Maciej Żenczykowskib8a7be52019-04-19 21:47:08 -0700282 uint8_t* permissions = bpf_uid_permission_map_lookup_elem(&appId);
Chenbo Fengbf660aa2019-02-26 16:12:27 -0800283 if (!permissions) {
284 // UID not in map. Default to just INTERNET permission.
285 return 1;
286 }
287
288 // A return value of 1 means allow, everything else means deny.
289 return (*permissions & BPF_PERMISSION_INTERNET) == BPF_PERMISSION_INTERNET;
Chenbo Feng2236e1b2019-02-26 14:30:19 -0800290}
291
292char _license[] SEC("license") = "Apache 2.0";