blob: 777c4fff77d11a03b5535bc33f854baeed5742f5 [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
29#ifndef v6_equal
30#define v6_equal(a, b) (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])
34#endif
35
36// TODO: these are already defined in packages/modules/Connectivity/bpf_progs/bpf_net_helpers.h.
37// smove to common location in future.
38static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) =
39 (void*)BPF_FUNC_get_socket_cookie;
40static int (*bpf_skb_store_bytes)(struct __sk_buff* skb, __u32 offset, const void* from, __u32 len,
41 __u64 flags) = (void*)BPF_FUNC_skb_store_bytes;
42static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
43 __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
44static long (*bpf_skb_ecn_set_ce)(struct __sk_buff* skb) =
45 (void*)BPF_FUNC_skb_ecn_set_ce;
46
47typedef struct {
48 struct in6_addr srcIp;
49 struct in6_addr dstIp;
50 uint32_t ifindex;
51 __be16 srcPort;
52 __be16 dstPortStart;
53 __be16 dstPortEnd;
54 uint8_t proto;
55 uint8_t dscpVal;
56 uint8_t presentFields;
57 uint8_t pad[3];
58} DscpPolicy;
59STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3); // 48
60
61typedef struct {
62 struct in6_addr srcIp;
63 struct in6_addr dstIp;
64 __u32 ifindex;
65 __be16 srcPort;
66 __be16 dstPort;
67 __u8 proto;
68 __u8 dscpVal;
69 __u8 pad[2];
70} RuleEntry;
71STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2); // 44