blob: 92ea0e2dc78cedbbd26c3a907233c0309d893993 [file] [log] [blame]
Tyler Wear72388212021-09-09 14:49:02 -07001/*
2 * Copyright (C) 2021 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
Tyler Wear72388212021-09-09 14:49:02 -070017#include <linux/bpf.h>
Tyler Wear11f494f2022-06-14 16:04:49 -070018#include <linux/if_ether.h>
Tyler Wear3ad80892022-02-03 15:14:44 -080019#include <linux/if_packet.h>
Tyler Wear72388212021-09-09 14:49:02 -070020#include <linux/ip.h>
21#include <linux/ipv6.h>
Tyler Wear72388212021-09-09 14:49:02 -070022#include <linux/pkt_cls.h>
23#include <linux/tcp.h>
Tyler Wear11f494f2022-06-14 16:04:49 -070024#include <linux/types.h>
Tyler Wear72388212021-09-09 14:49:02 -070025#include <netinet/in.h>
26#include <netinet/udp.h>
Tyler Wear11f494f2022-06-14 16:04:49 -070027#include <stdint.h>
Tyler Wear72388212021-09-09 14:49:02 -070028#include <string.h>
29
Maciej Żenczykowskif7699522022-05-24 15:56:03 -070030// The resulting .o needs to load on the Android T beta 3 bpfloader
31#define BPFLOADER_MIN_VER BPFLOADER_T_BETA3_VERSION
Maciej Żenczykowskiacebffb2022-05-16 16:05:15 -070032
Tyler Wear72388212021-09-09 14:49:02 -070033#include "bpf_helpers.h"
Tyler Wear3ad80892022-02-03 15:14:44 -080034#include "dscp_policy.h"
Tyler Wear72388212021-09-09 14:49:02 -070035
Tyler Wear11f494f2022-06-14 16:04:49 -070036#define ECN_MASK 3
37#define IP4_OFFSET(field, header) (header + offsetof(struct iphdr, field))
38#define UPDATE_TOS(dscp, tos) (dscp << 2) | (tos & ECN_MASK)
39#define UPDATE_PRIORITY(dscp) ((dscp >> 2) + 0x60)
40#define UPDATE_FLOW_LABEL(dscp, flow_lbl) ((dscp & 0xf) << 6) + (flow_lbl >> 6)
41
Tyler Wear72388212021-09-09 14:49:02 -070042DEFINE_BPF_MAP_GRW(switch_comp_map, ARRAY, int, uint64_t, 1, AID_SYSTEM)
43
Tyler Wear3ad80892022-02-03 15:14:44 -080044DEFINE_BPF_MAP_GRW(ipv4_socket_to_policies_map_A, HASH, uint64_t, RuleEntry, MAX_POLICIES,
Tyler Wear11f494f2022-06-14 16:04:49 -070045 AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080046DEFINE_BPF_MAP_GRW(ipv4_socket_to_policies_map_B, HASH, uint64_t, RuleEntry, MAX_POLICIES,
Tyler Wear11f494f2022-06-14 16:04:49 -070047 AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080048DEFINE_BPF_MAP_GRW(ipv6_socket_to_policies_map_A, HASH, uint64_t, RuleEntry, MAX_POLICIES,
Tyler Wear11f494f2022-06-14 16:04:49 -070049 AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080050DEFINE_BPF_MAP_GRW(ipv6_socket_to_policies_map_B, HASH, uint64_t, RuleEntry, MAX_POLICIES,
Tyler Wear11f494f2022-06-14 16:04:49 -070051 AID_SYSTEM)
Tyler Wear72388212021-09-09 14:49:02 -070052
Tyler Wear11f494f2022-06-14 16:04:49 -070053DEFINE_BPF_MAP_GRW(ipv4_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
54DEFINE_BPF_MAP_GRW(ipv6_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080055
56static inline __always_inline void match_policy(struct __sk_buff* skb, bool ipv4, bool is_eth) {
57 void* data = (void*)(long)skb->data;
58 const void* data_end = (void*)(long)skb->data_end;
59
60 const int l2_header_size = is_eth ? sizeof(struct ethhdr) : 0;
61 struct ethhdr* eth = is_eth ? data : NULL;
62
63 if (data + l2_header_size > data_end) return;
64
65 int zero = 0;
66 int hdr_size = 0;
67 uint64_t* selectedMap = bpf_switch_comp_map_lookup_elem(&zero);
Tyler Wear72388212021-09-09 14:49:02 -070068
69 // use this with HASH map so map lookup only happens once policies have been added?
70 if (!selectedMap) {
Tyler Wear3ad80892022-02-03 15:14:44 -080071 return;
Tyler Wear72388212021-09-09 14:49:02 -070072 }
73
74 // used for map lookup
75 uint64_t cookie = bpf_get_socket_cookie(skb);
Tyler Wear11f494f2022-06-14 16:04:49 -070076 if (!cookie) return;
Tyler Wear72388212021-09-09 14:49:02 -070077
Tyler Wear3ad80892022-02-03 15:14:44 -080078 uint16_t sport = 0;
79 uint16_t dport = 0;
Tyler Wear11f494f2022-06-14 16:04:49 -070080 uint8_t protocol = 0; // TODO: Use are reserved value? Or int (-1) and cast to uint below?
Tyler Wear3ad80892022-02-03 15:14:44 -080081 struct in6_addr srcIp = {};
82 struct in6_addr dstIp = {};
Tyler Wear11f494f2022-06-14 16:04:49 -070083 uint8_t tos = 0; // Only used for IPv4
84 uint8_t priority = 0; // Only used for IPv6
85 uint8_t flow_lbl = 0; // Only used for IPv6
Tyler Wear3ad80892022-02-03 15:14:44 -080086 if (ipv4) {
87 const struct iphdr* const iph = is_eth ? (void*)(eth + 1) : data;
Tyler Wear11f494f2022-06-14 16:04:49 -070088 hdr_size = l2_header_size + sizeof(struct iphdr);
Tyler Wear72388212021-09-09 14:49:02 -070089 // Must have ipv4 header
Tyler Wear11f494f2022-06-14 16:04:49 -070090 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -070091
92 // IP version must be 4
Tyler Wear3ad80892022-02-03 15:14:44 -080093 if (iph->version != 4) return;
Tyler Wear72388212021-09-09 14:49:02 -070094
95 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
Tyler Wear3ad80892022-02-03 15:14:44 -080096 if (iph->ihl != 5) return;
Tyler Wear72388212021-09-09 14:49:02 -070097
Tyler Wear3ad80892022-02-03 15:14:44 -080098 // V4 mapped address in in6_addr sets 10/11 position to 0xff.
99 srcIp.s6_addr32[2] = htonl(0x0000ffff);
100 dstIp.s6_addr32[2] = htonl(0x0000ffff);
Tyler Wear72388212021-09-09 14:49:02 -0700101
Tyler Wear3ad80892022-02-03 15:14:44 -0800102 // Copy IPv4 address into in6_addr for easy comparison below.
103 srcIp.s6_addr32[3] = iph->saddr;
104 dstIp.s6_addr32[3] = iph->daddr;
105 protocol = iph->protocol;
106 tos = iph->tos;
Tyler Wear3ad80892022-02-03 15:14:44 -0800107 } else {
108 struct ipv6hdr* ip6h = is_eth ? (void*)(eth + 1) : data;
Tyler Wear11f494f2022-06-14 16:04:49 -0700109 hdr_size = l2_header_size + sizeof(struct ipv6hdr);
Tyler Wear3ad80892022-02-03 15:14:44 -0800110 // Must have ipv6 header
Tyler Wear11f494f2022-06-14 16:04:49 -0700111 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -0700112
Tyler Wear3ad80892022-02-03 15:14:44 -0800113 if (ip6h->version != 6) return;
Tyler Wear72388212021-09-09 14:49:02 -0700114
Tyler Wear3ad80892022-02-03 15:14:44 -0800115 srcIp = ip6h->saddr;
116 dstIp = ip6h->daddr;
117 protocol = ip6h->nexthdr;
118 priority = ip6h->priority;
119 flow_lbl = ip6h->flow_lbl[0];
Tyler Wear3ad80892022-02-03 15:14:44 -0800120 }
Tyler Wear72388212021-09-09 14:49:02 -0700121
Tyler Wear3ad80892022-02-03 15:14:44 -0800122 switch (protocol) {
123 case IPPROTO_UDP:
Tyler Wear11f494f2022-06-14 16:04:49 -0700124 case IPPROTO_UDPLITE: {
125 struct udphdr* udp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800126 udp = data + hdr_size;
127 if ((void*)(udp + 1) > data_end) return;
128 sport = udp->source;
129 dport = udp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700130 } break;
131 case IPPROTO_TCP: {
132 struct tcphdr* tcp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800133 tcp = data + hdr_size;
134 if ((void*)(tcp + 1) > data_end) return;
135 sport = tcp->source;
136 dport = tcp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700137 } break;
Tyler Wear3ad80892022-02-03 15:14:44 -0800138 default:
139 return;
140 }
141
142 RuleEntry* existingRule;
143 if (ipv4) {
144 if (*selectedMap == MAP_A) {
145 existingRule = bpf_ipv4_socket_to_policies_map_A_lookup_elem(&cookie);
146 } else {
147 existingRule = bpf_ipv4_socket_to_policies_map_B_lookup_elem(&cookie);
148 }
149 } else {
150 if (*selectedMap == MAP_A) {
151 existingRule = bpf_ipv6_socket_to_policies_map_A_lookup_elem(&cookie);
152 } else {
153 existingRule = bpf_ipv6_socket_to_policies_map_B_lookup_elem(&cookie);
154 }
155 }
156
157 if (existingRule && v6_equal(srcIp, existingRule->srcIp) &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700158 v6_equal(dstIp, existingRule->dstIp) && skb->ifindex == existingRule->ifindex &&
159 ntohs(sport) == htons(existingRule->srcPort) &&
160 ntohs(dport) == htons(existingRule->dstPort) && protocol == existingRule->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800161 if (ipv4) {
Tyler Wear11f494f2022-06-14 16:04:49 -0700162 uint8_t newTos = UPDATE_TOS(existingRule->dscpVal, tos);
163 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(newTos),
164 sizeof(uint16_t));
165 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &newTos, sizeof(newTos), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800166 } else {
Tyler Wear11f494f2022-06-14 16:04:49 -0700167 uint8_t new_priority = UPDATE_PRIORITY(existingRule->dscpVal);
168 uint8_t new_flow_label = UPDATE_FLOW_LABEL(existingRule->dscpVal, flow_lbl);
169 bpf_skb_store_bytes(skb, 0 + l2_header_size, &new_priority, sizeof(uint8_t), 0);
170 bpf_skb_store_bytes(skb, 1 + l2_header_size, &new_flow_label, sizeof(uint8_t), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800171 }
172 return;
173 }
174
175 // Linear scan ipv4_dscp_policies_map since no stored params match skb.
176 int bestScore = -1;
177 uint32_t bestMatch = 0;
178
179 for (register uint64_t i = 0; i < MAX_POLICIES; i++) {
180 int score = 0;
181 uint8_t tempMask = 0;
182 // Using a uint64 in for loop prevents infinite loop during BPF load,
183 // but the key is uint32, so convert back.
184 uint32_t key = i;
185
186 DscpPolicy* policy;
187 if (ipv4) {
188 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&key);
189 } else {
190 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&key);
Tyler Wear72388212021-09-09 14:49:02 -0700191 }
192
Tyler Wear3ad80892022-02-03 15:14:44 -0800193 // If the policy lookup failed, presentFields is 0, or iface index does not match
194 // index on skb buff, then we can continue to next policy.
Tyler Wear11f494f2022-06-14 16:04:49 -0700195 if (!policy || policy->presentFields == 0 || policy->ifindex != skb->ifindex) continue;
Tyler Wear72388212021-09-09 14:49:02 -0700196
Tyler Wear3ad80892022-02-03 15:14:44 -0800197 if ((policy->presentFields & SRC_IP_MASK_FLAG) == SRC_IP_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700198 v6_equal(srcIp, policy->srcIp)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800199 score++;
200 tempMask |= SRC_IP_MASK_FLAG;
201 }
202 if ((policy->presentFields & DST_IP_MASK_FLAG) == DST_IP_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700203 v6_equal(dstIp, policy->dstIp)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800204 score++;
205 tempMask |= DST_IP_MASK_FLAG;
206 }
207 if ((policy->presentFields & SRC_PORT_MASK_FLAG) == SRC_PORT_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700208 ntohs(sport) == htons(policy->srcPort)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800209 score++;
210 tempMask |= SRC_PORT_MASK_FLAG;
211 }
212 if ((policy->presentFields & DST_PORT_MASK_FLAG) == DST_PORT_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700213 ntohs(dport) >= htons(policy->dstPortStart) &&
214 ntohs(dport) <= htons(policy->dstPortEnd)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800215 score++;
216 tempMask |= DST_PORT_MASK_FLAG;
217 }
218 if ((policy->presentFields & PROTO_MASK_FLAG) == PROTO_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700219 protocol == policy->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800220 score++;
221 tempMask |= PROTO_MASK_FLAG;
222 }
Tyler Wear72388212021-09-09 14:49:02 -0700223
Tyler Wear3ad80892022-02-03 15:14:44 -0800224 if (score > bestScore && tempMask == policy->presentFields) {
225 bestMatch = i;
226 bestScore = score;
227 }
228 }
Tyler Wear72388212021-09-09 14:49:02 -0700229
Tyler Wear11f494f2022-06-14 16:04:49 -0700230 uint8_t new_tos = 0; // Can 0 be used as default forwarding value?
231 uint8_t new_dscp = 0;
Tyler Wear3ad80892022-02-03 15:14:44 -0800232 uint8_t new_priority = 0;
233 uint8_t new_flow_lbl = 0;
234 if (bestScore > 0) {
235 DscpPolicy* policy;
236 if (ipv4) {
237 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&bestMatch);
238 } else {
239 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&bestMatch);
240 }
241
242 if (policy) {
Tyler Wear11f494f2022-06-14 16:04:49 -0700243 new_dscp = policy->dscpVal;
Tyler Wear3ad80892022-02-03 15:14:44 -0800244 if (ipv4) {
Tyler Wear11f494f2022-06-14 16:04:49 -0700245 new_tos = UPDATE_TOS(new_dscp, tos);
Tyler Wear3ad80892022-02-03 15:14:44 -0800246 } else {
Tyler Wear11f494f2022-06-14 16:04:49 -0700247 new_priority = UPDATE_PRIORITY(new_dscp);
248 new_flow_lbl = UPDATE_FLOW_LABEL(new_dscp, flow_lbl);
Tyler Wear72388212021-09-09 14:49:02 -0700249 }
250 }
Tyler Wear11f494f2022-06-14 16:04:49 -0700251 } else
252 return;
Tyler Wear72388212021-09-09 14:49:02 -0700253
Tyler Wear3ad80892022-02-03 15:14:44 -0800254 RuleEntry value = {
255 .srcIp = srcIp,
256 .dstIp = dstIp,
257 .ifindex = skb->ifindex,
258 .srcPort = sport,
259 .dstPort = dport,
260 .proto = protocol,
Tyler Wear11f494f2022-06-14 16:04:49 -0700261 .dscpVal = new_dscp,
Tyler Wear3ad80892022-02-03 15:14:44 -0800262 };
Tyler Wear72388212021-09-09 14:49:02 -0700263
Tyler Wear11f494f2022-06-14 16:04:49 -0700264 // Update map with new policy.
Tyler Wear3ad80892022-02-03 15:14:44 -0800265 if (ipv4) {
Tyler Wear72388212021-09-09 14:49:02 -0700266 if (*selectedMap == MAP_A) {
267 bpf_ipv4_socket_to_policies_map_A_update_elem(&cookie, &value, BPF_ANY);
268 } else {
269 bpf_ipv4_socket_to_policies_map_B_update_elem(&cookie, &value, BPF_ANY);
270 }
Tyler Wear3ad80892022-02-03 15:14:44 -0800271 } else {
Tyler Wear72388212021-09-09 14:49:02 -0700272 if (*selectedMap == MAP_A) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800273 bpf_ipv6_socket_to_policies_map_A_update_elem(&cookie, &value, BPF_ANY);
Tyler Wear72388212021-09-09 14:49:02 -0700274 } else {
Tyler Wear3ad80892022-02-03 15:14:44 -0800275 bpf_ipv6_socket_to_policies_map_B_update_elem(&cookie, &value, BPF_ANY);
Tyler Wear72388212021-09-09 14:49:02 -0700276 }
Tyler Wear3ad80892022-02-03 15:14:44 -0800277 }
Tyler Wear72388212021-09-09 14:49:02 -0700278
Tyler Wear3ad80892022-02-03 15:14:44 -0800279 // Need to store bytes after updating map or program will not load.
280 if (ipv4 && new_tos != (tos & 252)) {
Tyler Wear11f494f2022-06-14 16:04:49 -0700281 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(new_tos), 2);
282 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &new_tos, sizeof(new_tos), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800283 } else if (!ipv4 && (new_priority != priority || new_flow_lbl != flow_lbl)) {
Tyler Wear11f494f2022-06-14 16:04:49 -0700284 bpf_skb_store_bytes(skb, l2_header_size, &new_priority, sizeof(new_priority), 0);
285 bpf_skb_store_bytes(skb, l2_header_size + 1, &new_flow_lbl, sizeof(new_flow_lbl), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800286 }
287 return;
288}
Tyler Wear72388212021-09-09 14:49:02 -0700289
Tyler Wear3ad80892022-02-03 15:14:44 -0800290DEFINE_BPF_PROG_KVER("schedcls/set_dscp_ether", AID_ROOT, AID_SYSTEM,
Tyler Weara8ca5972022-06-29 12:45:20 -0700291 schedcls_set_dscp_ether, KVER(5, 15, 0))
Tyler Wear3ad80892022-02-03 15:14:44 -0800292(struct __sk_buff* skb) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800293 if (skb->pkt_type != PACKET_HOST) return TC_ACT_PIPE;
294
295 if (skb->protocol == htons(ETH_P_IP)) {
296 match_policy(skb, true, true);
297 } else if (skb->protocol == htons(ETH_P_IPV6)) {
298 match_policy(skb, false, true);
299 }
300
301 // Always return TC_ACT_PIPE
302 return TC_ACT_PIPE;
303}
304
305DEFINE_BPF_PROG_KVER("schedcls/set_dscp_raw_ip", AID_ROOT, AID_SYSTEM,
Tyler Weara8ca5972022-06-29 12:45:20 -0700306 schedcls_set_dscp_raw_ip, KVER(5, 15, 0))
Tyler Wear3ad80892022-02-03 15:14:44 -0800307(struct __sk_buff* skb) {
308 if (skb->protocol == htons(ETH_P_IP)) {
309 match_policy(skb, true, false);
310 } else if (skb->protocol == htons(ETH_P_IPV6)) {
311 match_policy(skb, false, false);
Tyler Wear72388212021-09-09 14:49:02 -0700312 }
313
314 // Always return TC_ACT_PIPE
315 return TC_ACT_PIPE;
316}
317
318LICENSE("Apache 2.0");
319CRITICAL("Connectivity");