blob: 36a2d5b0ff2eb624b6ead116cda08f478df871c7 [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 Żenczykowski0ff4ec02022-07-27 11:04:23 +000040DEFINE_BPF_MAP_GRW(ipv4_socket_to_policies_map, HASH, uint64_t, RuleEntry, MAX_POLICIES, AID_SYSTEM)
41DEFINE_BPF_MAP_GRW(ipv6_socket_to_policies_map, HASH, uint64_t, RuleEntry, MAX_POLICIES, AID_SYSTEM)
Tyler Wear72388212021-09-09 14:49:02 -070042
Tyler Wear11f494f2022-06-14 16:04:49 -070043DEFINE_BPF_MAP_GRW(ipv4_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
44DEFINE_BPF_MAP_GRW(ipv6_dscp_policies_map, ARRAY, uint32_t, DscpPolicy, MAX_POLICIES, AID_SYSTEM)
Tyler Wear3ad80892022-02-03 15:14:44 -080045
Patrick Rohr7f325cc2022-07-25 10:15:02 -070046static inline __always_inline void match_policy(struct __sk_buff* skb, bool ipv4) {
Tyler Wear3ad80892022-02-03 15:14:44 -080047 void* data = (void*)(long)skb->data;
48 const void* data_end = (void*)(long)skb->data_end;
49
Patrick Rohr7f325cc2022-07-25 10:15:02 -070050 const int l2_header_size = sizeof(struct ethhdr);
51 struct ethhdr* eth = data;
Tyler Wear3ad80892022-02-03 15:14:44 -080052
53 if (data + l2_header_size > data_end) return;
54
Tyler Wear3ad80892022-02-03 15:14:44 -080055 int hdr_size = 0;
Tyler Wear72388212021-09-09 14:49:02 -070056
57 // used for map lookup
58 uint64_t cookie = bpf_get_socket_cookie(skb);
Tyler Wear11f494f2022-06-14 16:04:49 -070059 if (!cookie) return;
Tyler Wear72388212021-09-09 14:49:02 -070060
Tyler Wear3ad80892022-02-03 15:14:44 -080061 uint16_t sport = 0;
62 uint16_t dport = 0;
Tyler Wear11f494f2022-06-14 16:04:49 -070063 uint8_t protocol = 0; // TODO: Use are reserved value? Or int (-1) and cast to uint below?
Tyler Wear92281052022-06-22 15:32:14 -070064 struct in6_addr src_ip = {};
65 struct in6_addr dst_ip = {};
Tyler Wear4e8949b2022-06-23 14:15:58 -070066 uint8_t tos = 0; // Only used for IPv4
67 uint32_t old_first_u32 = 0; // Only used for IPv6
Tyler Wear3ad80892022-02-03 15:14:44 -080068 if (ipv4) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -070069 const struct iphdr* const iph = (void*)(eth + 1);
Tyler Wear11f494f2022-06-14 16:04:49 -070070 hdr_size = l2_header_size + sizeof(struct iphdr);
Tyler Wear72388212021-09-09 14:49:02 -070071 // Must have ipv4 header
Tyler Wear11f494f2022-06-14 16:04:49 -070072 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -070073
74 // IP version must be 4
Tyler Wear3ad80892022-02-03 15:14:44 -080075 if (iph->version != 4) return;
Tyler Wear72388212021-09-09 14:49:02 -070076
77 // We cannot handle IP options, just standard 20 byte == 5 dword minimal IPv4 header
Tyler Wear3ad80892022-02-03 15:14:44 -080078 if (iph->ihl != 5) return;
Tyler Wear72388212021-09-09 14:49:02 -070079
Tyler Wear3ad80892022-02-03 15:14:44 -080080 // V4 mapped address in in6_addr sets 10/11 position to 0xff.
Tyler Wear92281052022-06-22 15:32:14 -070081 src_ip.s6_addr32[2] = htonl(0x0000ffff);
82 dst_ip.s6_addr32[2] = htonl(0x0000ffff);
Tyler Wear72388212021-09-09 14:49:02 -070083
Tyler Wear3ad80892022-02-03 15:14:44 -080084 // Copy IPv4 address into in6_addr for easy comparison below.
Tyler Wear92281052022-06-22 15:32:14 -070085 src_ip.s6_addr32[3] = iph->saddr;
86 dst_ip.s6_addr32[3] = iph->daddr;
Tyler Wear3ad80892022-02-03 15:14:44 -080087 protocol = iph->protocol;
88 tos = iph->tos;
Tyler Wear3ad80892022-02-03 15:14:44 -080089 } else {
Patrick Rohr7f325cc2022-07-25 10:15:02 -070090 struct ipv6hdr* ip6h = (void*)(eth + 1);
Tyler Wear11f494f2022-06-14 16:04:49 -070091 hdr_size = l2_header_size + sizeof(struct ipv6hdr);
Tyler Wear3ad80892022-02-03 15:14:44 -080092 // Must have ipv6 header
Tyler Wear11f494f2022-06-14 16:04:49 -070093 if (data + hdr_size > data_end) return;
Tyler Wear72388212021-09-09 14:49:02 -070094
Tyler Wear3ad80892022-02-03 15:14:44 -080095 if (ip6h->version != 6) return;
Tyler Wear72388212021-09-09 14:49:02 -070096
Tyler Wear92281052022-06-22 15:32:14 -070097 src_ip = ip6h->saddr;
98 dst_ip = ip6h->daddr;
Tyler Wear3ad80892022-02-03 15:14:44 -080099 protocol = ip6h->nexthdr;
Tyler Wear4e8949b2022-06-23 14:15:58 -0700100 old_first_u32 = *(uint32_t*)ip6h;
Tyler Wear3ad80892022-02-03 15:14:44 -0800101 }
Tyler Wear72388212021-09-09 14:49:02 -0700102
Tyler Wear3ad80892022-02-03 15:14:44 -0800103 switch (protocol) {
104 case IPPROTO_UDP:
Tyler Wear11f494f2022-06-14 16:04:49 -0700105 case IPPROTO_UDPLITE: {
106 struct udphdr* udp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800107 udp = data + hdr_size;
108 if ((void*)(udp + 1) > data_end) return;
109 sport = udp->source;
110 dport = udp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700111 } break;
112 case IPPROTO_TCP: {
113 struct tcphdr* tcp;
Tyler Wear3ad80892022-02-03 15:14:44 -0800114 tcp = data + hdr_size;
115 if ((void*)(tcp + 1) > data_end) return;
116 sport = tcp->source;
117 dport = tcp->dest;
Tyler Wear11f494f2022-06-14 16:04:49 -0700118 } break;
Tyler Wear3ad80892022-02-03 15:14:44 -0800119 default:
120 return;
121 }
122
Tyler Wear92281052022-06-22 15:32:14 -0700123 RuleEntry* existing_rule;
Tyler Wear3ad80892022-02-03 15:14:44 -0800124 if (ipv4) {
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +0000125 existing_rule = bpf_ipv4_socket_to_policies_map_lookup_elem(&cookie);
Tyler Wear3ad80892022-02-03 15:14:44 -0800126 } else {
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +0000127 existing_rule = bpf_ipv6_socket_to_policies_map_lookup_elem(&cookie);
Tyler Wear3ad80892022-02-03 15:14:44 -0800128 }
129
Tyler Wear92281052022-06-22 15:32:14 -0700130 if (existing_rule && v6_equal(src_ip, existing_rule->src_ip) &&
131 v6_equal(dst_ip, existing_rule->dst_ip) && skb->ifindex == existing_rule->ifindex &&
132 ntohs(sport) == htons(existing_rule->src_port) &&
133 ntohs(dport) == htons(existing_rule->dst_port) && protocol == existing_rule->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800134 if (ipv4) {
Tyler Wear92281052022-06-22 15:32:14 -0700135 uint8_t newTos = UPDATE_TOS(existing_rule->dscp_val, tos);
Tyler Wear11f494f2022-06-14 16:04:49 -0700136 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(newTos),
137 sizeof(uint16_t));
138 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &newTos, sizeof(newTos), 0);
Tyler Wear3ad80892022-02-03 15:14:44 -0800139 } else {
Tyler Wear4e8949b2022-06-23 14:15:58 -0700140 uint32_t new_first_u32 =
141 htonl(ntohl(old_first_u32) & 0xF03FFFFF | (existing_rule->dscp_val << 22));
142 bpf_skb_store_bytes(skb, l2_header_size, &new_first_u32, sizeof(uint32_t),
143 BPF_F_RECOMPUTE_CSUM);
Tyler Wear3ad80892022-02-03 15:14:44 -0800144 }
145 return;
146 }
147
148 // Linear scan ipv4_dscp_policies_map since no stored params match skb.
Tyler Wear92281052022-06-22 15:32:14 -0700149 int best_score = -1;
150 uint32_t best_match = 0;
Tyler Wear3ad80892022-02-03 15:14:44 -0800151
152 for (register uint64_t i = 0; i < MAX_POLICIES; i++) {
153 int score = 0;
Tyler Wear92281052022-06-22 15:32:14 -0700154 uint8_t temp_mask = 0;
Tyler Wear3ad80892022-02-03 15:14:44 -0800155 // Using a uint64 in for loop prevents infinite loop during BPF load,
156 // but the key is uint32, so convert back.
157 uint32_t key = i;
158
159 DscpPolicy* policy;
160 if (ipv4) {
161 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&key);
162 } else {
163 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&key);
Tyler Wear72388212021-09-09 14:49:02 -0700164 }
165
Tyler Wear92281052022-06-22 15:32:14 -0700166 // If the policy lookup failed, present_fields is 0, or iface index does not match
Tyler Wear3ad80892022-02-03 15:14:44 -0800167 // index on skb buff, then we can continue to next policy.
Tyler Wear92281052022-06-22 15:32:14 -0700168 if (!policy || policy->present_fields == 0 || policy->ifindex != skb->ifindex) continue;
Tyler Wear72388212021-09-09 14:49:02 -0700169
Tyler Wear92281052022-06-22 15:32:14 -0700170 if ((policy->present_fields & SRC_IP_MASK_FLAG) == SRC_IP_MASK_FLAG &&
171 v6_equal(src_ip, policy->src_ip)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800172 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700173 temp_mask |= SRC_IP_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800174 }
Tyler Wear92281052022-06-22 15:32:14 -0700175 if ((policy->present_fields & DST_IP_MASK_FLAG) == DST_IP_MASK_FLAG &&
176 v6_equal(dst_ip, policy->dst_ip)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800177 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700178 temp_mask |= DST_IP_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800179 }
Tyler Wear92281052022-06-22 15:32:14 -0700180 if ((policy->present_fields & SRC_PORT_MASK_FLAG) == SRC_PORT_MASK_FLAG &&
181 ntohs(sport) == htons(policy->src_port)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800182 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700183 temp_mask |= SRC_PORT_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800184 }
Tyler Wear92281052022-06-22 15:32:14 -0700185 if ((policy->present_fields & DST_PORT_MASK_FLAG) == DST_PORT_MASK_FLAG &&
186 ntohs(dport) >= htons(policy->dst_port_start) &&
187 ntohs(dport) <= htons(policy->dst_port_end)) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800188 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700189 temp_mask |= DST_PORT_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800190 }
Tyler Wear92281052022-06-22 15:32:14 -0700191 if ((policy->present_fields & PROTO_MASK_FLAG) == PROTO_MASK_FLAG &&
Tyler Wear11f494f2022-06-14 16:04:49 -0700192 protocol == policy->proto) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800193 score++;
Tyler Wear92281052022-06-22 15:32:14 -0700194 temp_mask |= PROTO_MASK_FLAG;
Tyler Wear3ad80892022-02-03 15:14:44 -0800195 }
Tyler Wear72388212021-09-09 14:49:02 -0700196
Tyler Wear92281052022-06-22 15:32:14 -0700197 if (score > best_score && temp_mask == policy->present_fields) {
198 best_match = i;
199 best_score = score;
Tyler Wear3ad80892022-02-03 15:14:44 -0800200 }
201 }
Tyler Wear72388212021-09-09 14:49:02 -0700202
Tyler Wear11f494f2022-06-14 16:04:49 -0700203 uint8_t new_dscp = 0;
Tyler Wear92281052022-06-22 15:32:14 -0700204 if (best_score > 0) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800205 DscpPolicy* policy;
206 if (ipv4) {
Tyler Wear92281052022-06-22 15:32:14 -0700207 policy = bpf_ipv4_dscp_policies_map_lookup_elem(&best_match);
Tyler Wear3ad80892022-02-03 15:14:44 -0800208 } else {
Tyler Wear92281052022-06-22 15:32:14 -0700209 policy = bpf_ipv6_dscp_policies_map_lookup_elem(&best_match);
Tyler Wear3ad80892022-02-03 15:14:44 -0800210 }
211
212 if (policy) {
Tyler Wear92281052022-06-22 15:32:14 -0700213 new_dscp = policy->dscp_val;
Tyler Wear72388212021-09-09 14:49:02 -0700214 }
Tyler Wear11f494f2022-06-14 16:04:49 -0700215 } else
216 return;
Tyler Wear72388212021-09-09 14:49:02 -0700217
Tyler Wear3ad80892022-02-03 15:14:44 -0800218 RuleEntry value = {
Tyler Wear92281052022-06-22 15:32:14 -0700219 .src_ip = src_ip,
220 .dst_ip = dst_ip,
Tyler Wear3ad80892022-02-03 15:14:44 -0800221 .ifindex = skb->ifindex,
Tyler Wear92281052022-06-22 15:32:14 -0700222 .src_port = sport,
223 .dst_port = dport,
Tyler Wear3ad80892022-02-03 15:14:44 -0800224 .proto = protocol,
Tyler Wear92281052022-06-22 15:32:14 -0700225 .dscp_val = new_dscp,
Tyler Wear3ad80892022-02-03 15:14:44 -0800226 };
Tyler Wear72388212021-09-09 14:49:02 -0700227
Tyler Wear11f494f2022-06-14 16:04:49 -0700228 // Update map with new policy.
Tyler Wear3ad80892022-02-03 15:14:44 -0800229 if (ipv4) {
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +0000230 bpf_ipv4_socket_to_policies_map_update_elem(&cookie, &value, BPF_ANY);
Tyler Wear3ad80892022-02-03 15:14:44 -0800231 } else {
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +0000232 bpf_ipv6_socket_to_policies_map_update_elem(&cookie, &value, BPF_ANY);
Tyler Wear3ad80892022-02-03 15:14:44 -0800233 }
Tyler Wear72388212021-09-09 14:49:02 -0700234
Tyler Wear3ad80892022-02-03 15:14:44 -0800235 // Need to store bytes after updating map or program will not load.
Tyler Wear4e8949b2022-06-23 14:15:58 -0700236 if (ipv4) {
237 uint8_t new_tos = UPDATE_TOS(new_dscp, tos);
Tyler Wear11f494f2022-06-14 16:04:49 -0700238 bpf_l3_csum_replace(skb, IP4_OFFSET(check, l2_header_size), htons(tos), htons(new_tos), 2);
239 bpf_skb_store_bytes(skb, IP4_OFFSET(tos, l2_header_size), &new_tos, sizeof(new_tos), 0);
Tyler Wear4e8949b2022-06-23 14:15:58 -0700240 } else {
241 uint32_t new_first_u32 = htonl(ntohl(old_first_u32) & 0xF03FFFFF | (new_dscp << 22));
242 bpf_skb_store_bytes(skb, l2_header_size, &new_first_u32, sizeof(uint32_t),
243 BPF_F_RECOMPUTE_CSUM);
Tyler Wear3ad80892022-02-03 15:14:44 -0800244 }
245 return;
246}
Tyler Wear72388212021-09-09 14:49:02 -0700247
Tyler Wear4e8949b2022-06-23 14:15:58 -0700248DEFINE_BPF_PROG_KVER("schedcls/set_dscp_ether", AID_ROOT, AID_SYSTEM, schedcls_set_dscp_ether,
249 KVER(5, 15, 0))
Tyler Wear3ad80892022-02-03 15:14:44 -0800250(struct __sk_buff* skb) {
Tyler Wear3ad80892022-02-03 15:14:44 -0800251 if (skb->pkt_type != PACKET_HOST) return TC_ACT_PIPE;
252
253 if (skb->protocol == htons(ETH_P_IP)) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -0700254 match_policy(skb, true);
Tyler Wear3ad80892022-02-03 15:14:44 -0800255 } else if (skb->protocol == htons(ETH_P_IPV6)) {
Patrick Rohr7f325cc2022-07-25 10:15:02 -0700256 match_policy(skb, false);
Tyler Wear3ad80892022-02-03 15:14:44 -0800257 }
258
259 // Always return TC_ACT_PIPE
260 return TC_ACT_PIPE;
261}
262
Tyler Wear72388212021-09-09 14:49:02 -0700263LICENSE("Apache 2.0");
264CRITICAL("Connectivity");