blob: e56596602b9016f53d14a10151d599143864d8fc [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
Maciej Żenczykowski1feaa432022-07-29 21:17:07 +000023#define PROTO_MASK_FLAG 8
Tyler Wear3ad80892022-02-03 15:14:44 -080024
25#define STRUCT_SIZE(name, size) _Static_assert(sizeof(name) == (size), "Incorrect struct size.")
26
Maciej Żenczykowski1feaa432022-07-29 21:17:07 +000027// Retrieve the first (ie. high) 64 bits of an IPv6 address (in network order)
28#define v6_hi_be64(v) (*(uint64_t*)&((v).s6_addr32[0]))
29
30// Retrieve the last (ie. low) 64 bits of an IPv6 address (in network order)
31#define v6_lo_be64(v) (*(uint64_t*)&((v).s6_addr32[2]))
32
33// This returns a non-zero u64 iff a != b
34#define v6_not_equal(a, b) ((v6_hi_be64(a) ^ v6_hi_be64(b)) \
35 | (v6_lo_be64(a) ^ v6_lo_be64(b)))
36
37// Returns 'a == b' as boolean
38#define v6_equal(a, b) (!v6_not_equal((a), (b)))
Tyler Wear3ad80892022-02-03 15:14:44 -080039
40// TODO: these are already defined in packages/modules/Connectivity/bpf_progs/bpf_net_helpers.h.
41// smove to common location in future.
42static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) =
43 (void*)BPF_FUNC_get_socket_cookie;
44static int (*bpf_skb_store_bytes)(struct __sk_buff* skb, __u32 offset, const void* from, __u32 len,
45 __u64 flags) = (void*)BPF_FUNC_skb_store_bytes;
46static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
47 __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
48static long (*bpf_skb_ecn_set_ce)(struct __sk_buff* skb) =
49 (void*)BPF_FUNC_skb_ecn_set_ce;
50
51typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070052 struct in6_addr src_ip;
53 struct in6_addr dst_ip;
Tyler Wear3ad80892022-02-03 15:14:44 -080054 uint32_t ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070055 __be16 src_port;
Maciej Żenczykowskia44510b2022-08-09 14:59:25 +000056 uint16_t dst_port_start;
57 uint16_t dst_port_end;
Tyler Wear3ad80892022-02-03 15:14:44 -080058 uint8_t proto;
Maciej Żenczykowskid7b92c02022-07-27 19:57:15 +000059 int8_t dscp_val; // -1 none, or 0..63 DSCP value
Tyler Wear92281052022-06-22 15:32:14 -070060 uint8_t present_fields;
Tyler Wear3ad80892022-02-03 15:14:44 -080061 uint8_t pad[3];
62} DscpPolicy;
63STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3); // 48
64
65typedef struct {
Tyler Wear92281052022-06-22 15:32:14 -070066 struct in6_addr src_ip;
67 struct in6_addr dst_ip;
Maciej Żenczykowski640752b2022-08-09 23:02:57 +000068 uint32_t ifindex;
Tyler Wear92281052022-06-22 15:32:14 -070069 __be16 src_port;
Maciej Żenczykowski640752b2022-08-09 23:02:57 +000070 uint16_t dst_port;
71 uint8_t proto;
72 int8_t dscp_val; // -1 none, or 0..63 DSCP value
73 uint8_t pad[2];
Tyler Wear3ad80892022-02-03 15:14:44 -080074} RuleEntry;
Maciej Żenczykowski0ff4ec02022-07-27 11:04:23 +000075STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2); // 44