|
| 1 | +/* Firebee BPF Helper Function Tests */ |
| 2 | + |
| 3 | +#include <linux/bpf.h> |
| 4 | +#include <linux/if_ether.h> |
| 5 | +#include <linux/ip.h> |
| 6 | +#include <linux/ipv6.h> |
| 7 | +#include <linux/tcp.h> |
| 8 | +#include <linux/udp.h> |
| 9 | +#include <bpf/bpf_helpers.h> |
| 10 | +#include <bpf/bpf_endian.h> |
| 11 | + |
| 12 | +#include "firebee_common.h" |
| 13 | +#include "firebee_helpers.h" |
| 14 | +#include "test_common.h" |
| 15 | + |
| 16 | +char __license[] SEC("license") = "GPL"; |
| 17 | + |
| 18 | +/* IPv4 Matching Tests */ |
| 19 | +CHECK("ip_matches") |
| 20 | +int test_ip_matches(struct xdp_md *ctx) |
| 21 | +{ |
| 22 | + test_init(); |
| 23 | + |
| 24 | + TEST("exact_match", { |
| 25 | + __u32 packet_ip = 0xC0A80101; /* 192.168.1.1 */ |
| 26 | + __u32 rule_ip = 0xC0A80101; |
| 27 | + __u32 subnet_mask = 0xFFFFFFFF; /* /32 */ |
| 28 | + |
| 29 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 30 | + if (!result) |
| 31 | + test_fatal("Expected exact IP match for 192.168.1.1/32"); |
| 32 | + }); |
| 33 | + |
| 34 | + TEST("exact_mismatch", { |
| 35 | + __u32 packet_ip = 0xC0A80101; /* 192.168.1.1 */ |
| 36 | + __u32 rule_ip = 0xC0A80102; /* 192.168.1.2 */ |
| 37 | + __u32 subnet_mask = 0xFFFFFFFF; |
| 38 | + |
| 39 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 40 | + if (result) |
| 41 | + test_fatal("Expected IP mismatch for different IPs"); |
| 42 | + }); |
| 43 | + |
| 44 | + TEST("cidr_24_match", { |
| 45 | + __u32 packet_ip = 0xC0A80101; /* 192.168.1.1 */ |
| 46 | + __u32 rule_ip = 0xC0A80100; /* 192.168.1.0 */ |
| 47 | + __u32 subnet_mask = 0xFFFFFF00; /* /24 */ |
| 48 | + |
| 49 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 50 | + if (!result) |
| 51 | + test_fatal("Expected /24 CIDR match"); |
| 52 | + }); |
| 53 | + |
| 54 | + TEST("cidr_24_mismatch", { |
| 55 | + __u32 packet_ip = 0xC0A80201; /* 192.168.2.1 */ |
| 56 | + __u32 rule_ip = 0xC0A80100; /* 192.168.1.0 */ |
| 57 | + __u32 subnet_mask = 0xFFFFFF00; /* /24 */ |
| 58 | + |
| 59 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 60 | + if (result) |
| 61 | + test_fatal("Expected /24 CIDR mismatch for different subnets"); |
| 62 | + }); |
| 63 | + |
| 64 | + TEST("cidr_16_match", { |
| 65 | + __u32 packet_ip = 0xC0A80101; /* 192.168.1.1 */ |
| 66 | + __u32 rule_ip = 0xC0A80000; /* 192.168.0.0 */ |
| 67 | + __u32 subnet_mask = 0xFFFF0000; /* /16 */ |
| 68 | + |
| 69 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 70 | + if (!result) |
| 71 | + test_fatal("Expected /16 CIDR match"); |
| 72 | + }); |
| 73 | + |
| 74 | + TEST("any_ip_match", { |
| 75 | + __u32 packet_ip = 0xC0A80101; /* 192.168.1.1 */ |
| 76 | + __u32 rule_ip = 0x00000000; /* 0.0.0.0 */ |
| 77 | + __u32 subnet_mask = 0x00000000; /* /0 - any */ |
| 78 | + |
| 79 | + int result = ip_matches(packet_ip, rule_ip, subnet_mask); |
| 80 | + if (!result) |
| 81 | + test_fatal("Expected any IP (0.0.0.0/0) to match"); |
| 82 | + }); |
| 83 | + |
| 84 | + test_finish(); |
| 85 | +} |
| 86 | + |
| 87 | +/* Port Matching Tests */ |
| 88 | +CHECK("port_matches") |
| 89 | +int test_port_matches(struct xdp_md *ctx) |
| 90 | +{ |
| 91 | + test_init(); |
| 92 | + |
| 93 | + TEST("exact_match", { |
| 94 | + __u16 packet_port = 80; |
| 95 | + __u16 rule_port = 80; |
| 96 | + |
| 97 | + int result = port_matches(packet_port, rule_port); |
| 98 | + if (!result) |
| 99 | + test_fatal("Expected exact port match for port 80"); |
| 100 | + }); |
| 101 | + |
| 102 | + TEST("exact_mismatch", { |
| 103 | + __u16 packet_port = 80; |
| 104 | + __u16 rule_port = 443; |
| 105 | + |
| 106 | + int result = port_matches(packet_port, rule_port); |
| 107 | + if (result) |
| 108 | + test_fatal("Expected port mismatch for 80 vs 443"); |
| 109 | + }); |
| 110 | + |
| 111 | + TEST("any_port_match", { |
| 112 | + __u16 packet_port = 12345; |
| 113 | + __u16 rule_port = PORT_ANY; |
| 114 | + |
| 115 | + int result = port_matches(packet_port, rule_port); |
| 116 | + if (!result) |
| 117 | + test_fatal("Expected PORT_ANY (0) to match any port"); |
| 118 | + }); |
| 119 | + |
| 120 | + test_finish(); |
| 121 | +} |
| 122 | + |
| 123 | +/* Protocol Matching Tests */ |
| 124 | +CHECK("protocol_matches") |
| 125 | +int test_protocol_matches(struct xdp_md *ctx) |
| 126 | +{ |
| 127 | + test_init(); |
| 128 | + |
| 129 | + TEST("tcp_match", { |
| 130 | + __u8 packet_proto = IPPROTO_TCP; |
| 131 | + __u8 rule_proto = IPPROTO_TCP; |
| 132 | + |
| 133 | + int result = protocol_matches(packet_proto, rule_proto); |
| 134 | + if (!result) |
| 135 | + test_fatal("Expected TCP protocol match"); |
| 136 | + }); |
| 137 | + |
| 138 | + TEST("tcp_udp_mismatch", { |
| 139 | + __u8 packet_proto = IPPROTO_TCP; |
| 140 | + __u8 rule_proto = IPPROTO_UDP; |
| 141 | + |
| 142 | + int result = protocol_matches(packet_proto, rule_proto); |
| 143 | + if (result) |
| 144 | + test_fatal("Expected TCP/UDP protocol mismatch"); |
| 145 | + }); |
| 146 | + |
| 147 | + TEST("any_protocol_match", { |
| 148 | + __u8 packet_proto = IPPROTO_ICMP; |
| 149 | + __u8 rule_proto = IPPROTO_ANY; |
| 150 | + |
| 151 | + int result = protocol_matches(packet_proto, rule_proto); |
| 152 | + if (!result) |
| 153 | + test_fatal("Expected IPPROTO_ANY to match ICMP"); |
| 154 | + }); |
| 155 | + |
| 156 | + test_finish(); |
| 157 | +} |
| 158 | + |
| 159 | +/* IPv6 Matching Tests */ |
| 160 | +CHECK("ipv6_matches") |
| 161 | +int test_ipv6_matches(struct xdp_md *ctx) |
| 162 | +{ |
| 163 | + test_init(); |
| 164 | + |
| 165 | + TEST("exact_match", { |
| 166 | + /* 2001:db8::1 */ |
| 167 | + __u32 packet_ip[4]; |
| 168 | + __u32 rule_ip[4]; |
| 169 | + packet_ip[0] = bpf_htonl(0x20010db8); |
| 170 | + packet_ip[1] = 0; |
| 171 | + packet_ip[2] = 0; |
| 172 | + packet_ip[3] = bpf_htonl(0x1); |
| 173 | + rule_ip[0] = bpf_htonl(0x20010db8); |
| 174 | + rule_ip[1] = 0; |
| 175 | + rule_ip[2] = 0; |
| 176 | + rule_ip[3] = bpf_htonl(0x1); |
| 177 | + __u8 prefix_len = 128; /* /128 - exact match */ |
| 178 | + |
| 179 | + int result = ipv6_matches(packet_ip, rule_ip, prefix_len); |
| 180 | + if (!result) |
| 181 | + test_fatal("Expected exact IPv6 match for 2001:db8::1/128"); |
| 182 | + }); |
| 183 | + |
| 184 | + TEST("exact_mismatch", { |
| 185 | + /* 2001:db8::1 vs 2001:db8::2 */ |
| 186 | + __u32 packet_ip[4]; |
| 187 | + __u32 rule_ip[4]; |
| 188 | + packet_ip[0] = bpf_htonl(0x20010db8); |
| 189 | + packet_ip[1] = 0; |
| 190 | + packet_ip[2] = 0; |
| 191 | + packet_ip[3] = bpf_htonl(0x1); |
| 192 | + rule_ip[0] = bpf_htonl(0x20010db8); |
| 193 | + rule_ip[1] = 0; |
| 194 | + rule_ip[2] = 0; |
| 195 | + rule_ip[3] = bpf_htonl(0x2); |
| 196 | + __u8 prefix_len = 128; |
| 197 | + |
| 198 | + int result = ipv6_matches(packet_ip, rule_ip, prefix_len); |
| 199 | + if (result) |
| 200 | + test_fatal("Expected IPv6 mismatch for different addresses"); |
| 201 | + }); |
| 202 | + |
| 203 | + TEST("prefix_64_match", { |
| 204 | + /* 2001:db8:1234:5678::1 matches 2001:db8:1234:5678::/64 */ |
| 205 | + __u32 packet_ip[4]; |
| 206 | + __u32 rule_ip[4]; |
| 207 | + packet_ip[0] = bpf_htonl(0x20010db8); |
| 208 | + packet_ip[1] = bpf_htonl(0x12345678); |
| 209 | + packet_ip[2] = 0; |
| 210 | + packet_ip[3] = bpf_htonl(0x1); |
| 211 | + rule_ip[0] = bpf_htonl(0x20010db8); |
| 212 | + rule_ip[1] = bpf_htonl(0x12345678); |
| 213 | + rule_ip[2] = 0; |
| 214 | + rule_ip[3] = 0; |
| 215 | + __u8 prefix_len = 64; |
| 216 | + |
| 217 | + int result = ipv6_matches(packet_ip, rule_ip, prefix_len); |
| 218 | + if (!result) |
| 219 | + test_fatal("Expected /64 prefix match"); |
| 220 | + }); |
| 221 | + |
| 222 | + TEST("prefix_32_match", { |
| 223 | + /* 2001:db8:1234:5678::1 matches 2001:db8::/32 */ |
| 224 | + __u32 packet_ip[4]; |
| 225 | + __u32 rule_ip[4]; |
| 226 | + packet_ip[0] = bpf_htonl(0x20010db8); |
| 227 | + packet_ip[1] = bpf_htonl(0x12345678); |
| 228 | + packet_ip[2] = 0; |
| 229 | + packet_ip[3] = bpf_htonl(0x1); |
| 230 | + rule_ip[0] = bpf_htonl(0x20010db8); |
| 231 | + rule_ip[1] = 0; |
| 232 | + rule_ip[2] = 0; |
| 233 | + rule_ip[3] = 0; |
| 234 | + __u8 prefix_len = 32; |
| 235 | + |
| 236 | + int result = ipv6_matches(packet_ip, rule_ip, prefix_len); |
| 237 | + if (!result) |
| 238 | + test_fatal("Expected /32 prefix match for 2001:db8::/32"); |
| 239 | + }); |
| 240 | + |
| 241 | + TEST("any_ipv6_match", { |
| 242 | + /* Any IPv6 address matches ::/0 */ |
| 243 | + __u32 packet_ip[4]; |
| 244 | + __u32 rule_ip[4]; |
| 245 | + packet_ip[0] = bpf_htonl(0x20010db8); |
| 246 | + packet_ip[1] = bpf_htonl(0x12345678); |
| 247 | + packet_ip[2] = 0; |
| 248 | + packet_ip[3] = bpf_htonl(0x1); |
| 249 | + rule_ip[0] = 0; |
| 250 | + rule_ip[1] = 0; |
| 251 | + rule_ip[2] = 0; |
| 252 | + rule_ip[3] = 0; |
| 253 | + __u8 prefix_len = 0; |
| 254 | + |
| 255 | + int result = ipv6_matches(packet_ip, rule_ip, prefix_len); |
| 256 | + if (!result) |
| 257 | + test_fatal("Expected any IPv6 (::/0) to match"); |
| 258 | + }); |
| 259 | + |
| 260 | + test_finish(); |
| 261 | +} |
0 commit comments