| Maciej Żenczykowski | aab04f1 | 2023-03-08 19:02:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #pragma once |
| 18 | |
| 19 | // Accept the full packet |
| 20 | #define BPF_ACCEPT BPF_STMT(BPF_RET | BPF_K, 0xFFFFFFFF) |
| 21 | |
| 22 | // Reject the packet |
| 23 | #define BPF_REJECT BPF_STMT(BPF_RET | BPF_K, 0) |
| 24 | |
| Maciej Żenczykowski | d5bf206 | 2023-10-04 22:24:17 +0000 | [diff] [blame] | 25 | // Note arguments to BPF_JUMP(opcode, operand, true_offset, false_offset) |
| 26 | |
| 27 | // If not equal, jump over count instructions |
| 28 | #define BPF_JUMP_IF_NOT_EQUAL(v, count) \ |
| 29 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (v), 0, (count)) |
| 30 | |
| Maciej Żenczykowski | 2cd8b69 | 2023-07-21 22:37:18 +0000 | [diff] [blame] | 31 | // *TWO* instructions: compare and if not equal jump over the accept statement |
| 32 | #define BPF2_ACCEPT_IF_EQUAL(v) \ |
| Maciej Żenczykowski | d5bf206 | 2023-10-04 22:24:17 +0000 | [diff] [blame] | 33 | BPF_JUMP_IF_NOT_EQUAL((v), 1), \ |
| Maciej Żenczykowski | 2cd8b69 | 2023-07-21 22:37:18 +0000 | [diff] [blame] | 34 | BPF_ACCEPT |
| 35 | |
| Maciej Żenczykowski | aab04f1 | 2023-03-08 19:02:48 -0800 | [diff] [blame] | 36 | // *TWO* instructions: compare and if equal jump over the reject statement |
| 37 | #define BPF2_REJECT_IF_NOT_EQUAL(v) \ |
| 38 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, (v), 1, 0), \ |
| 39 | BPF_REJECT |
| 40 | |
| Maciej Żenczykowski | d5bf206 | 2023-10-04 22:24:17 +0000 | [diff] [blame] | 41 | // *TWO* instructions: compare and if greater or equal jump over the reject statement |
| 42 | #define BPF2_REJECT_IF_LESS_THAN(v) \ |
| 43 | BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, (v), 1, 0), \ |
| 44 | BPF_REJECT |
| 45 | |
| 46 | // *TWO* instructions: compare and if *NOT* greater jump over the reject statement |
| 47 | #define BPF2_REJECT_IF_GREATER_THAN(v) \ |
| 48 | BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, (v), 0, 1), \ |
| 49 | BPF_REJECT |
| 50 | |
| 51 | // *THREE* instructions: compare and if *NOT* in range [lo, hi], jump over the reject statement |
| 52 | #define BPF3_REJECT_IF_NOT_IN_RANGE(lo, hi) \ |
| 53 | BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, (lo), 0, 1), \ |
| 54 | BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, (hi), 0, 1), \ |
| 55 | BPF_REJECT |
| 56 | |
| Maciej Żenczykowski | 2cd8b69 | 2023-07-21 22:37:18 +0000 | [diff] [blame] | 57 | // *TWO* instructions: compare and if none of the bits are set jump over the reject statement |
| Maciej Żenczykowski | b900201 | 2023-10-01 18:32:33 +0000 | [diff] [blame] | 58 | #define BPF2_REJECT_IF_ANY_MASKED_BITS_SET(v) \ |
| Maciej Żenczykowski | 2cd8b69 | 2023-07-21 22:37:18 +0000 | [diff] [blame] | 59 | BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, (v), 0, 1), \ |
| 60 | BPF_REJECT |
| 61 | |
| 62 | // loads skb->protocol |
| 63 | #define BPF_LOAD_SKB_PROTOCOL \ |
| 64 | BPF_STMT(BPF_LD | BPF_H | BPF_ABS, (__u32)SKF_AD_OFF + SKF_AD_PROTOCOL) |
| 65 | |
| Maciej Żenczykowski | c58cfb7 | 2024-11-19 20:16:44 +0000 | [diff] [blame] | 66 | // loads skb->pkt_type (0..7: see uapi/linux/if_packet.h PACKET_* constants) |
| 67 | #define BPF_LOAD_SKB_PKTTYPE \ |
| 68 | BPF_STMT(BPF_LD | BPF_B | BPF_ABS, (__u32)SKF_AD_OFF + SKF_AD_PKTTYPE) |
| 69 | |
| Maciej Żenczykowski | aab04f1 | 2023-03-08 19:02:48 -0800 | [diff] [blame] | 70 | // 8-bit load relative to start of link layer (mac/ethernet) header. |
| 71 | #define BPF_LOAD_MAC_RELATIVE_U8(ofs) \ |
| 72 | BPF_STMT(BPF_LD | BPF_B | BPF_ABS, (__u32)SKF_LL_OFF + (ofs)) |
| 73 | |
| 74 | // Big/Network Endian 16-bit load relative to start of link layer (mac/ethernet) header. |
| 75 | #define BPF_LOAD_MAC_RELATIVE_BE16(ofs) \ |
| 76 | BPF_STMT(BPF_LD | BPF_H | BPF_ABS, (__u32)SKF_LL_OFF + (ofs)) |
| 77 | |
| 78 | // Big/Network Endian 32-bit load relative to start of link layer (mac/ethernet) header. |
| 79 | #define BPF_LOAD_MAC_RELATIVE_BE32(ofs) \ |
| 80 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (__u32)SKF_LL_OFF + (ofs)) |
| 81 | |
| 82 | // 8-bit load relative to start of network (IPv4/IPv6) header. |
| 83 | #define BPF_LOAD_NET_RELATIVE_U8(ofs) \ |
| 84 | BPF_STMT(BPF_LD | BPF_B | BPF_ABS, (__u32)SKF_NET_OFF + (ofs)) |
| 85 | |
| 86 | // Big/Network Endian 16-bit load relative to start of network (IPv4/IPv6) header. |
| 87 | #define BPF_LOAD_NET_RELATIVE_BE16(ofs) \ |
| 88 | BPF_STMT(BPF_LD | BPF_H | BPF_ABS, (__u32)SKF_NET_OFF + (ofs)) |
| 89 | |
| 90 | // Big/Network Endian 32-bit load relative to start of network (IPv4/IPv6) header. |
| 91 | #define BPF_LOAD_NET_RELATIVE_BE32(ofs) \ |
| 92 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (__u32)SKF_NET_OFF + (ofs)) |
| 93 | |
| 94 | #define field_sizeof(struct_type,field) sizeof(((struct_type *)0)->field) |
| 95 | |
| 96 | // 8-bit load from IPv4 header field. |
| 97 | #define BPF_LOAD_IPV4_U8(field) \ |
| 98 | BPF_LOAD_NET_RELATIVE_U8(({ \ |
| 99 | _Static_assert(field_sizeof(struct iphdr, field) == 1, "field of wrong size"); \ |
| 100 | offsetof(iphdr, field); \ |
| 101 | })) |
| 102 | |
| 103 | // Big/Network Endian 16-bit load from IPv4 header field. |
| 104 | #define BPF_LOAD_IPV4_BE16(field) \ |
| 105 | BPF_LOAD_NET_RELATIVE_BE16(({ \ |
| 106 | _Static_assert(field_sizeof(struct iphdr, field) == 2, "field of wrong size"); \ |
| 107 | offsetof(iphdr, field); \ |
| 108 | })) |
| 109 | |
| 110 | // Big/Network Endian 32-bit load from IPv4 header field. |
| 111 | #define BPF_LOAD_IPV4_BE32(field) \ |
| 112 | BPF_LOAD_NET_RELATIVE_BE32(({ \ |
| 113 | _Static_assert(field_sizeof(struct iphdr, field) == 4, "field of wrong size"); \ |
| 114 | offsetof(iphdr, field); \ |
| 115 | })) |
| 116 | |
| 117 | // 8-bit load from IPv6 header field. |
| 118 | #define BPF_LOAD_IPV6_U8(field) \ |
| 119 | BPF_LOAD_NET_RELATIVE_U8(({ \ |
| 120 | _Static_assert(field_sizeof(struct ipv6hdr, field) == 1, "field of wrong size"); \ |
| 121 | offsetof(ipv6hdr, field); \ |
| 122 | })) |
| 123 | |
| 124 | // Big/Network Endian 16-bit load from IPv6 header field. |
| 125 | #define BPF_LOAD_IPV6_BE16(field) \ |
| 126 | BPF_LOAD_NET_RELATIVE_BE16(({ \ |
| 127 | _Static_assert(field_sizeof(struct ipv6hdr, field) == 2, "field of wrong size"); \ |
| 128 | offsetof(ipv6hdr, field); \ |
| 129 | })) |
| 130 | |
| 131 | // Big/Network Endian 32-bit load from IPv6 header field. |
| 132 | #define BPF_LOAD_IPV6_BE32(field) \ |
| 133 | BPF_LOAD_NET_RELATIVE_BE32(({ \ |
| 134 | _Static_assert(field_sizeof(struct ipv6hdr, field) == 4, "field of wrong size"); \ |
| 135 | offsetof(ipv6hdr, field); \ |
| 136 | })) |
| Maciej Żenczykowski | 73df741 | 2023-10-01 19:20:13 +0000 | [diff] [blame] | 137 | |
| 138 | // Load the length of the IPv4 header into X index register. |
| 139 | // ie. X := 4 * IPv4.IHL, where IPv4.IHL is the bottom nibble |
| 140 | // of the first byte of the IPv4 (aka network layer) header. |
| 141 | #define BPF_LOADX_NET_RELATIVE_IPV4_HLEN \ |
| 142 | BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, (__u32)SKF_NET_OFF) |
| 143 | |
| 144 | // Blindly assumes no IPv6 extension headers, just does X := 40 |
| 145 | // You may later adjust this as you parse through IPv6 ext hdrs. |
| 146 | #define BPF_LOADX_CONSTANT_IPV6_HLEN \ |
| 147 | BPF_STMT(BPF_LDX | BPF_W | BPF_IMM, sizeof(struct ipv6hdr)) |
| 148 | |
| 149 | // NOTE: all the following require X to be setup correctly (v4: 20+, v6: 40+) |
| 150 | |
| 151 | // 8-bit load from L4 (TCP/UDP/...) header |
| 152 | #define BPF_LOAD_NETX_RELATIVE_L4_U8(ofs) \ |
| 153 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) |
| 154 | |
| 155 | // Big/Network Endian 16-bit load from L4 (TCP/UDP/...) header |
| 156 | #define BPF_LOAD_NETX_RELATIVE_L4_BE16(ofs) \ |
| 157 | BPF_STMT(BPF_LD | BPF_H | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) |
| 158 | |
| 159 | // Big/Network Endian 32-bit load from L4 (TCP/UDP/...) header |
| 160 | #define BPF_LOAD_NETX_RELATIVE_L4_BE32(ofs) \ |
| 161 | BPF_STMT(BPF_LD | BPF_W | BPF_IND, (__u32)SKF_NET_OFF + (ofs)) |
| 162 | |
| 163 | // Both ICMPv4 and ICMPv6 start with u8 type, u8 code |
| 164 | #define BPF_LOAD_NETX_RELATIVE_ICMP_TYPE BPF_LOAD_NETX_RELATIVE_L4_U8(0) |
| 165 | #define BPF_LOAD_NETX_RELATIVE_ICMP_CODE BPF_LOAD_NETX_RELATIVE_L4_U8(1) |
| 166 | |
| Jimi Chen | 32691a9 | 2024-11-11 12:47:41 +0000 | [diff] [blame] | 167 | // IGMP start with u8 type |
| 168 | #define BPF_LOAD_NETX_RELATIVE_IGMP_TYPE BPF_LOAD_NETX_RELATIVE_L4_U8(0) |
| 169 | |
| Maciej Żenczykowski | 73df741 | 2023-10-01 19:20:13 +0000 | [diff] [blame] | 170 | // IPv6 extension headers (HOPOPTS, DSTOPS, FRAG) begin with a u8 nexthdr |
| 171 | #define BPF_LOAD_NETX_RELATIVE_V6EXTHDR_NEXTHDR BPF_LOAD_NETX_RELATIVE_L4_U8(0) |
| 172 | |
| 173 | // IPv6 fragment header is always exactly 8 bytes long |
| 174 | #define BPF_LOAD_CONSTANT_V6FRAGHDR_LEN \ |
| 175 | BPF_STMT(BPF_LD | BPF_IMM, 8) |
| 176 | |
| 177 | // HOPOPTS/DSTOPS follow up with 'u8 len', counting 8 byte units, (0->8, 1->16) |
| 178 | // *THREE* instructions |
| 179 | #define BPF3_LOAD_NETX_RELATIVE_V6EXTHDR_LEN \ |
| Maciej Żenczykowski | 1e32277 | 2023-10-05 19:23:33 -0700 | [diff] [blame] | 180 | BPF_LOAD_NETX_RELATIVE_L4_U8(1), \ |
| 181 | BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1), \ |
| Maciej Żenczykowski | 73df741 | 2023-10-01 19:20:13 +0000 | [diff] [blame] | 182 | BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 3) |
| 183 | |
| 184 | // *TWO* instructions: A += X; X := A |
| 185 | #define BPF2_ADD_A_TO_X \ |
| Maciej Żenczykowski | 1e32277 | 2023-10-05 19:23:33 -0700 | [diff] [blame] | 186 | BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), \ |
| Maciej Żenczykowski | 73df741 | 2023-10-01 19:20:13 +0000 | [diff] [blame] | 187 | BPF_STMT(BPF_MISC | BPF_TAX, 0) |
| 188 | |
| 189 | // UDP/UDPLITE/TCP/SCTP/DCCP all start with be16 srcport, dstport |
| 190 | #define BPF_LOAD_NETX_RELATIVE_SRC_PORT BPF_LOAD_NETX_RELATIVE_L4_BE16(0) |
| 191 | #define BPF_LOAD_NETX_RELATIVE_DST_PORT BPF_LOAD_NETX_RELATIVE_L4_BE16(2) |