blob: e55f473df18895358d0bf05f1715922749033828 [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"
Ken Chen74ff3ee2022-07-14 16:46:39 +080034#include "dscpPolicy.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)
Tyler Wear11f494f2022-06-14 16:04:49 -070039
Maciej Żenczykowskif75b7e12022-07-27 12:56:01 +000040DEFINE_BPF_MAP_GRW(socket_policy_cache_map, HASH, uint64_t, RuleEntry, CACHE_MAP_SIZE, AID_SYSTEM)
Tyler Wear72388212021-09-09 14:49:02 -070041
Tyler Wear11f494f2022-06-14 16:04:49 -070042DEFINE_BPF_MAP_GRW(ipv4_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
43DEFINE_BPF_MAP_GRW(ipv6_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080044
Patrick Rohr7f325cc2022-07-25 10:15:02 -070045static inline __always_inline void match_policy(struct __sk_buff* skb, bool ipv4) {
Tyler Wear3ad80892022-02-03 15:14:44 -080046 void* data = (void*)(long)skb->data;
47 const void* data_end = (void*)(long)skb->data_end;
48
Patrick Rohr7f325cc2022-07-25 10:15:02 -070049 const int l2_header_size = sizeof(struct ethhdr);
50 struct ethhdr* eth = data;
Tyler Wear3ad80892022-02-03 15:14:44 -080051
52 if (data + l2_header_size > data_end) return;
53
Tyler Wear3ad80892022-02-03 15:14:44 -080054 int hdr_size = 0;
Tyler Wear72388212021-09-09 14:49:02 -070055
56 // used for map lookup
57 uint64_t cookie = bpf_get_socket_cookie(skb);
Tyler Wear11f494f2022-06-14 16:04:49 -070058 if (!cookie) return;
Tyler Wear72388212021-09-09 14:49:02 -070059
Tyler Wear3ad80892022-02-03 15:14:44 -080060 uint16_t sport = 0;
61 uint16_t dport = 0;
Tyler Wear11f494f2022-06-14 16:04:49 -070062 uint8_t protocol = 0; // TODO: Use are reserved value? Or int (-1) and cast to uint below?
Tyler Wear92281052022-06-22 15:32:14 -070063 struct in6_addr src_ip = {};
64 struct in6_addr dst_ip = {};
Tyler Wear4e8949b2022-06-23 14:15:58 -070065 uint8_t tos = 0; // Only used for IPv4
66 uint32_t old_first_u32 = 0; // Only used for IPv6
Tyler Wear3ad80892022-02-03 15:14:44 -080067 if (ipv4) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -070068 const struct iphdr* const iph = (void*)(eth + 1);
Tyler Wear11f494f2022-06-14 16:04:49 -070069 hdr_size = l2_header_size + sizeof(struct iphdr);
Tyler Wear72388212021-09-09 14:49:02 -070070 // Must have ipv4 header
Tyler Wear11f494f2022-06-14 16:04:49 -070071 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -070072
73 // IP version must be 4
Tyler Wear3ad80892022-02-03 15:14:44 -080074 if (iph->version != 4) return;
Tyler Wear72388212021-09-09 14:49:02 -070075
76 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
Tyler Wear3ad80892022-02-03 15:14:44 -080077 if (iph->ihl != 5) return;
Tyler Wear72388212021-09-09 14:49:02 -070078
Tyler Wear3ad80892022-02-03 15:14:44 -080079 // V4 mapped address in in6_addr sets 10/11 position to 0xff.
Tyler Wear92281052022-06-22 15:32:14 -070080 src_ip.s6_addr32[2] = htonl(0x0000ffff);
81 dst_ip.s6_addr32[2] = htonl(0x0000ffff);
Tyler Wear72388212021-09-09 14:49:02 -070082
Tyler Wear3ad80892022-02-03 15:14:44 -080083 // Copy IPv4 address into in6_addr for easy comparison below.
Tyler Wear92281052022-06-22 15:32:14 -070084 src_ip.s6_addr32[3] = iph->saddr;
85 dst_ip.s6_addr32[3] = iph->daddr;
Tyler Wear3ad80892022-02-03 15:14:44 -080086 protocol = iph->protocol;
87 tos = iph->tos;
Tyler Wear3ad80892022-02-03 15:14:44 -080088 } else {
Patrick Rohr7f325cc2022-07-25 10:15:02 -070089 struct ipv6hdr* ip6h = (void*)(eth + 1);
Tyler Wear11f494f2022-06-14 16:04:49 -070090 hdr_size = l2_header_size + sizeof(struct ipv6hdr);
Tyler Wear3ad80892022-02-03 15:14:44 -080091 // Must have ipv6 header
Tyler Wear11f494f2022-06-14 16:04:49 -070092 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -070093
Tyler Wear3ad80892022-02-03 15:14:44 -080094 if (ip6h->version != 6) return;
Tyler Wear72388212021-09-09 14:49:02 -070095
Tyler Wear92281052022-06-22 15:32:14 -070096 src_ip = ip6h->saddr;
97 dst_ip = ip6h->daddr;
Tyler Wear3ad80892022-02-03 15:14:44 -080098 protocol = ip6h->nexthdr;
Tyler Wear4e8949b2022-06-23 14:15:58 -070099 old_first_u32 = *(uint32_t*)ip6h;
Tyler Wear3ad80892022-02-03 15:14:44 -0800100 }
Tyler Wear72388212021-09-09 14:49:02 -0700101
Tyler Wear3ad80892022-02-03 15:14:44 -0800102 switch (protocol) {
103 case IPPROTO_UDP:
Tyler Wear11f494f2022-06-14 16:04:49 -0700104 case IPPROTO_UDPLITE: {
105 struct udphdr* udp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800106 udp = data + hdr_size;
107 if ((void*)(udp + 1) > data_end) return;
108 sport = udp->source;
109 dport = udp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700110 } break;
111 case IPPROTO_TCP: {
112 struct tcphdr* tcp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800113 tcp = data + hdr_size;
114 if ((void*)(tcp + 1) > data_end) return;
115 sport = tcp->source;
116 dport = tcp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700117 } break;
Tyler Wear3ad80892022-02-03 15:14:44 -0800118 default:
119 return;
120 }
121
Maciej Żenczykowskif75b7e12022-07-27 12:56:01 +0000122 RuleEntry* existing_rule = bpf_socket_policy_cache_map_lookup_elem(&cookie);
Tyler Wear3ad80892022-02-03 15:14:44 -0800123
Tyler Wear92281052022-06-22 15:32:14 -0700124 if (existing_rule && v6_equal(src_ip, existing_rule->src_ip) &&
125 v6_equal(dst_ip, existing_rule->dst_ip) && skb->ifindex == existing_rule->ifindex &&
126 ntohs(sport) == htons(existing_rule->src_port) &&
127 ntohs(dport) == htons(existing_rule->dst_port) && protocol == existing_rule->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800128 if (ipv4) {
Tyler Wear92281052022-06-22 15:32:14 -0700129 uint8_t newTos = UPDATE_TOS(existing_rule->dscp_val, tos);
Tyler Wear11f494f2022-06-14 16:04:49 -0700130 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(newTos),
131 sizeof(uint16_t));
132 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &newTos, sizeof(newTos), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800133 } else {
Tyler Wear4e8949b2022-06-23 14:15:58 -0700134 uint32_t new_first_u32 =
135 htonl(ntohl(old_first_u32) & 0xF03FFFFF | (existing_rule->dscp_val << 22));
136 bpf_skb_store_bytes(skb, l2_header_size, &new_first_u32, sizeof(uint32_t),
137 BPF_F_RECOMPUTE_CSUM);
Tyler Wear3ad80892022-02-03 15:14:44 -0800138 }
139 return;
140 }
141
142 // Linear scan ipv4_dscp_policies_map since no stored params match skb.
Tyler Wear92281052022-06-22 15:32:14 -0700143 int best_score = -1;
144 uint32_t best_match = 0;
Tyler Wear3ad80892022-02-03 15:14:44 -0800145
146 for (register uint64_t i = 0; i < MAX_POLICIES; i++) {
147 int score = 0;
Tyler Wear92281052022-06-22 15:32:14 -0700148 uint8_t temp_mask = 0;
Tyler Wear3ad80892022-02-03 15:14:44 -0800149 // Using a uint64 in for loop prevents infinite loop during BPF load,
150 // but the key is uint32, so convert back.
151 uint32_t key = i;
152
153 DscpPolicy* policy;
154 if (ipv4) {
155 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&key);
156 } else {
157 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&key);
Tyler Wear72388212021-09-09 14:49:02 -0700158 }
159
Tyler Wear92281052022-06-22 15:32:14 -0700160 // If the policy lookup failed, present_fields is 0, or iface index does not match
Tyler Wear3ad80892022-02-03 15:14:44 -0800161 // index on skb buff, then we can continue to next policy.
Tyler Wear92281052022-06-22 15:32:14 -0700162 if (!policy || policy->present_fields == 0 || policy->ifindex != skb->ifindex) continue;
Tyler Wear72388212021-09-09 14:49:02 -0700163
Tyler Wear92281052022-06-22 15:32:14 -0700164 if ((policy->present_fields & SRC_IP_MASK_FLAG) == SRC_IP_MASK_FLAG &&
165 v6_equal(src_ip, policy->src_ip)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800166 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700167 temp_mask |= SRC_IP_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800168 }
Tyler Wear92281052022-06-22 15:32:14 -0700169 if ((policy->present_fields & DST_IP_MASK_FLAG) == DST_IP_MASK_FLAG &&
170 v6_equal(dst_ip, policy->dst_ip)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800171 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700172 temp_mask |= DST_IP_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800173 }
Tyler Wear92281052022-06-22 15:32:14 -0700174 if ((policy->present_fields & SRC_PORT_MASK_FLAG) == SRC_PORT_MASK_FLAG &&
175 ntohs(sport) == htons(policy->src_port)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800176 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700177 temp_mask |= SRC_PORT_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800178 }
Tyler Wear92281052022-06-22 15:32:14 -0700179 if ((policy->present_fields & DST_PORT_MASK_FLAG) == DST_PORT_MASK_FLAG &&
180 ntohs(dport) >= htons(policy->dst_port_start) &&
181 ntohs(dport) <= htons(policy->dst_port_end)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800182 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700183 temp_mask |= DST_PORT_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800184 }
Tyler Wear92281052022-06-22 15:32:14 -0700185 if ((policy->present_fields & PROTO_MASK_FLAG) == PROTO_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700186 protocol == policy->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800187 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700188 temp_mask |= PROTO_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800189 }
Tyler Wear72388212021-09-09 14:49:02 -0700190
Tyler Wear92281052022-06-22 15:32:14 -0700191 if (score > best_score && temp_mask == policy->present_fields) {
192 best_match = i;
193 best_score = score;
Tyler Wear3ad80892022-02-03 15:14:44 -0800194 }
195 }
Tyler Wear72388212021-09-09 14:49:02 -0700196
Tyler Wear11f494f2022-06-14 16:04:49 -0700197 uint8_t new_dscp = 0;
Tyler Wear92281052022-06-22 15:32:14 -0700198 if (best_score > 0) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800199 DscpPolicy* policy;
200 if (ipv4) {
Tyler Wear92281052022-06-22 15:32:14 -0700201 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&best_match);
Tyler Wear3ad80892022-02-03 15:14:44 -0800202 } else {
Tyler Wear92281052022-06-22 15:32:14 -0700203 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&best_match);
Tyler Wear3ad80892022-02-03 15:14:44 -0800204 }
205
206 if (policy) {
Tyler Wear92281052022-06-22 15:32:14 -0700207 new_dscp = policy->dscp_val;
Tyler Wear72388212021-09-09 14:49:02 -0700208 }
Tyler Wear11f494f2022-06-14 16:04:49 -0700209 } else
210 return;
Tyler Wear72388212021-09-09 14:49:02 -0700211
Tyler Wear3ad80892022-02-03 15:14:44 -0800212 RuleEntry value = {
Tyler Wear92281052022-06-22 15:32:14 -0700213 .src_ip = src_ip,
214 .dst_ip = dst_ip,
Tyler Wear3ad80892022-02-03 15:14:44 -0800215 .ifindex = skb->ifindex,
Tyler Wear92281052022-06-22 15:32:14 -0700216 .src_port = sport,
217 .dst_port = dport,
Tyler Wear3ad80892022-02-03 15:14:44 -0800218 .proto = protocol,
Tyler Wear92281052022-06-22 15:32:14 -0700219 .dscp_val = new_dscp,
Tyler Wear3ad80892022-02-03 15:14:44 -0800220 };
Tyler Wear72388212021-09-09 14:49:02 -0700221
Tyler Wear11f494f2022-06-14 16:04:49 -0700222 // Update map with new policy.
Maciej Żenczykowskif75b7e12022-07-27 12:56:01 +0000223 bpf_socket_policy_cache_map_update_elem(&cookie, &value, BPF_ANY);
Tyler Wear72388212021-09-09 14:49:02 -0700224
Tyler Wear3ad80892022-02-03 15:14:44 -0800225 // Need to store bytes after updating map or program will not load.
Tyler Wear4e8949b2022-06-23 14:15:58 -0700226 if (ipv4) {
227 uint8_t new_tos = UPDATE_TOS(new_dscp, tos);
Tyler Wear11f494f2022-06-14 16:04:49 -0700228 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(new_tos), 2);
229 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &new_tos, sizeof(new_tos), 0);
Tyler Wear4e8949b2022-06-23 14:15:58 -0700230 } else {
231 uint32_t new_first_u32 = htonl(ntohl(old_first_u32) & 0xF03FFFFF | (new_dscp << 22));
232 bpf_skb_store_bytes(skb, l2_header_size, &new_first_u32, sizeof(uint32_t),
233 BPF_F_RECOMPUTE_CSUM);
Tyler Wear3ad80892022-02-03 15:14:44 -0800234 }
235 return;
236}
Tyler Wear72388212021-09-09 14:49:02 -0700237
Tyler Wear4e8949b2022-06-23 14:15:58 -0700238DEFINE_BPF_PROG_KVER("schedcls/set_dscp_ether", AID_ROOT, AID_SYSTEM, schedcls_set_dscp_ether,
239 KVER(5, 15, 0))
Tyler Wear3ad80892022-02-03 15:14:44 -0800240(struct __sk_buff* skb) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800241 if (skb->pkt_type != PACKET_HOST) return TC_ACT_PIPE;
242
243 if (skb->protocol == htons(ETH_P_IP)) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -0700244 match_policy(skb, true);
Tyler Wear3ad80892022-02-03 15:14:44 -0800245 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -0700246 match_policy(skb, false);
Tyler Wear3ad80892022-02-03 15:14:44 -0800247 }
248
249 // Always return TC_ACT_PIPE
250 return TC_ACT_PIPE;
251}
252
Tyler Wear72388212021-09-09 14:49:02 -0700253LICENSE("Apache 2.0");
254CRITICAL("Connectivity");