• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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)
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 {
47     struct in6_addr srcIp;
48     struct in6_addr dstIp;
49     uint32_t ifindex;
50     __be16 srcPort;
51     __be16 dstPortStart;
52     __be16 dstPortEnd;
53     uint8_t proto;
54     uint8_t dscpVal;
55     uint8_t presentFields;
56     uint8_t pad[3];
57 } DscpPolicy;
58 STRUCT_SIZE(DscpPolicy, 2 * 16 + 4 + 3 * 2 + 3 * 1 + 3);  // 48
59 
60 typedef struct {
61     struct in6_addr srcIp;
62     struct in6_addr dstIp;
63     __u32 ifindex;
64     __be16 srcPort;
65     __be16 dstPort;
66     __u8 proto;
67     __u8 dscpVal;
68     __u8 pad[2];
69 } RuleEntry;
70 STRUCT_SIZE(RuleEntry, 2 * 16 + 1 * 4 + 2 * 2 + 2 * 1 + 2);  // 44