blob: c1db6ab05161aaa81d4b08bdc7f37f17becfdda6 [file] [log] [blame]
Tyler Wear3ad80892022-02-03 15:14:44 -08001/*
2 * Copyright (C) 2022 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 Żenczykowskif75b7e12022-07-27 12:56:01 +000017#define CACHE_MAP_SIZE 1024
Tyler Wear3ad80892022-02-03 15:14:44 -080018#define MAX_POLICIES 16
Tyler Wear3ad80892022-02-03 15:14:44 -080019
20#define SRC_IP_MASK_FLAG 1
21#define DST_IP_MASK_FLAG 2
22#define SRC_PORT_MASK_FLAG 4
23#define DST_PORT_MASK_FLAG 8
24#define PROTO_MASK_FLAG 16
25
26#define STRUCT_SIZE(name, size) _Static_assert(sizeof(name) == (size), "Incorrect struct size.")
27
Maciej Żenczykowski3fc472d2022-05-18 09:05:07 +000028#define v6_equal(a, b) \
29 (((a.s6_addr32[0] ^ b.s6_addr32[0]) | \
30 (a.s6_addr32[1] ^ b.s6_addr32[1]) | \
31 (a.s6_addr32[2] ^ b.s6_addr32[2]) | \
32 (a.s6_addr32[3] ^ b.s6_addr32[3])) == 0)
Tyler Wear3ad80892022-02-03 15:14:44 -080033
34// TODO: these are already defined in packages/modules/Connectivity/bpf_progs/bpf_net_helpers.h.
35// smove to common location in future.
36static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) =
37 (void*)BPF_FUNC_get_socket_cookie;
38static int (*bpf_skb_store_bytes)(struct __sk_buff* skb, __u32 offset, const void* from, __u32 len,
39 __u64 flags) = (void*)BPF_FUNC_skb_store_bytes;
40static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
41 __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
42static long (*bpf_skb_ecn_set_ce)(struct __sk_buff* skb) =
43 (void*)BPF_FUNC_skb_ecn_set_ce;
44
45typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070046 struct in6_addr src_ip;
47 struct in6_addr dst_ip;
Tyler Wear3ad80892022-02-03 15:14:44 -080048 uint32_t ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070049 __be16 src_port;
50 __be16 dst_port_start;
51 __be16 dst_port_end;
Tyler Wear3ad80892022-02-03 15:14:44 -080052 uint8_t proto;
Maciej Żenczykowskid7b92c02022-07-27 19:57:15 +000053 int8_t dscp_val; // -1 none, or 0..63 DSCP value
Tyler Wear92281052022-06-22 15:32:14 -070054 uint8_t present_fields;
Tyler Wear3ad80892022-02-03 15:14:44 -080055 uint8_t pad[3];
56} DscpPolicy;
57STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3); // 48
58
59typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070060 struct in6_addr src_ip;
61 struct in6_addr dst_ip;
Tyler Wear3ad80892022-02-03 15:14:44 -080062 __u32 ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070063 __be16 src_port;
64 __be16 dst_port;
Tyler Wear3ad80892022-02-03 15:14:44 -080065 __u8 proto;
Maciej Żenczykowskid7b92c02022-07-27 19:57:15 +000066 __s8 dscp_val; // -1 none, or 0..63 DSCP value
Tyler Wear3ad80892022-02-03 15:14:44 -080067 __u8 pad[2];
68} RuleEntry;
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +000069STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2); // 44