| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Żenczykowski | 3fc472d | 2022-05-18 09:05:07 +0000 | [diff] [blame] | 29 | #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 Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | // TODO: these are already defined in packages/modules/Connectivity/bpf_progs/bpf_net_helpers.h. | 
|  | 36 | // smove to common location in future. | 
|  | 37 | static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = | 
|  | 38 | (void*)BPF_FUNC_get_socket_cookie; | 
|  | 39 | static 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; | 
|  | 41 | static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to, | 
|  | 42 | __u64 flags) = (void*)BPF_FUNC_l3_csum_replace; | 
|  | 43 | static long (*bpf_skb_ecn_set_ce)(struct __sk_buff* skb) = | 
|  | 44 | (void*)BPF_FUNC_skb_ecn_set_ce; | 
|  | 45 |  | 
|  | 46 | typedef struct { | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 47 | struct in6_addr src_ip; | 
|  | 48 | struct in6_addr dst_ip; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 49 | uint32_t ifindex; | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 50 | __be16 src_port; | 
|  | 51 | __be16 dst_port_start; | 
|  | 52 | __be16 dst_port_end; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 53 | uint8_t proto; | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 54 | uint8_t dscp_val; | 
|  | 55 | uint8_t present_fields; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 56 | uint8_t pad[3]; | 
|  | 57 | } DscpPolicy; | 
|  | 58 | STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3);  // 48 | 
|  | 59 |  | 
|  | 60 | typedef struct { | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 61 | struct in6_addr src_ip; | 
|  | 62 | struct in6_addr dst_ip; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 63 | __u32 ifindex; | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 64 | __be16 src_port; | 
|  | 65 | __be16 dst_port; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 66 | __u8 proto; | 
| Tyler Wear | 9228105 | 2022-06-22 15:32:14 -0700 | [diff] [blame] | 67 | __u8 dscp_val; | 
| Tyler Wear | 3ad8089 | 2022-02-03 15:14:44 -0800 | [diff] [blame] | 68 | __u8 pad[2]; | 
|  | 69 | } RuleEntry; | 
|  | 70 | STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2);  // 44 |