blob: 455a1219510a61fa35379ac17178062bf140a7f8 [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
17#define MAX_POLICIES 16
18#define MAP_A 1
19#define MAP_B 2
20
21#define SRC_IP_MASK_FLAG 1
22#define DST_IP_MASK_FLAG 2
23#define SRC_PORT_MASK_FLAG 4
24#define DST_PORT_MASK_FLAG 8
25#define PROTO_MASK_FLAG 16
26
27#define STRUCT_SIZE(name, size) _Static_assert(sizeof(name) == (size), "Incorrect struct size.")
28
Maciej Żenczykowski3fc472d2022-05-18 09:05:07 +000029#define v6_equal(a, b) \
30 (((a.s6_addr32[0] ^ b.s6_addr32[0]) | \
31 (a.s6_addr32[1] ^ b.s6_addr32[1]) | \
32 (a.s6_addr32[2] ^ b.s6_addr32[2]) | \
33 (a.s6_addr32[3] ^ b.s6_addr32[3])) == 0)
Tyler Wear3ad80892022-02-03 15:14:44 -080034
35// TODO: these are already defined in packages/modules/Connectivity/bpf_progs/bpf_net_helpers.h.
36// smove to common location in future.
37static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) =
38 (void*)BPF_FUNC_get_socket_cookie;
39static int (*bpf_skb_store_bytes)(struct __sk_buff* skb, __u32 offset, const void* from, __u32 len,
40 __u64 flags) = (void*)BPF_FUNC_skb_store_bytes;
41static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
42 __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
43static long (*bpf_skb_ecn_set_ce)(struct __sk_buff* skb) =
44 (void*)BPF_FUNC_skb_ecn_set_ce;
45
46typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070047 struct in6_addr src_ip;
48 struct in6_addr dst_ip;
Tyler Wear3ad80892022-02-03 15:14:44 -080049 uint32_t ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070050 __be16 src_port;
51 __be16 dst_port_start;
52 __be16 dst_port_end;
Tyler Wear3ad80892022-02-03 15:14:44 -080053 uint8_t proto;
Tyler Wear92281052022-06-22 15:32:14 -070054 uint8_t dscp_val;
55 uint8_t present_fields;
Tyler Wear3ad80892022-02-03 15:14:44 -080056 uint8_t pad[3];
57} DscpPolicy;
58STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3); // 48
59
60typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070061 struct in6_addr src_ip;
62 struct in6_addr dst_ip;
Tyler Wear3ad80892022-02-03 15:14:44 -080063 __u32 ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070064 __be16 src_port;
65 __be16 dst_port;
Tyler Wear3ad80892022-02-03 15:14:44 -080066 __u8 proto;
Tyler Wear92281052022-06-22 15:32:14 -070067 __u8 dscp_val;
Tyler Wear3ad80892022-02-03 15:14:44 -080068 __u8 pad[2];
69} RuleEntry;
70STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2); // 44