| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. |
| 4 |
* |
| 5 |
* This contains some basic static unit tests for the allowedips data structure. |
| 6 |
* It also has two additional modes that are disabled and meant to be used by |
| 7 |
* folks directly playing with this file. If you define the macro |
| 8 |
* DEBUG_PRINT_TRIE_GRAPHVIZ to be 1, then every time there's a full tree in |
| 9 |
* memory, it will be printed out as KERN_DEBUG in a format that can be passed |
| 10 |
* to graphviz (the dot command) to visualize it. If you define the macro |
| 11 |
* DEBUG_RANDOM_TRIE to be 1, then there will be an extremely costly set of |
| 12 |
* randomized tests done against a trivial implementation, which may take |
| 13 |
* upwards of a half-hour to complete. There's no set of users who should be |
| 14 |
* enabling these, and the only developers that should go anywhere near these |
| 15 |
* nobs are the ones who are reading this comment. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifdef DEBUG |
| 19 |
|
| 20 |
#include <linux/siphash.h> |
| 21 |
|
| 22 |
static __init void print_node(struct allowedips_node *node, u8 bits) |
| 23 |
{ |
| 24 |
char *fmt_connection = KERN_DEBUG "\t\"%p/%d\" -> \"%p/%d\";\n"; |
| 25 |
char *fmt_declaration = KERN_DEBUG "\t\"%p/%d\"[style=%s, color=\"#%06x\"];\n"; |
| 26 |
u8 ip1[16], ip2[16], cidr1, cidr2; |
| 27 |
char *style = "dotted"; |
| 28 |
u32 color = 0; |
| 29 |
|
| 30 |
if (node == NULL) |
| 31 |
return; |
| 32 |
if (bits == 32) { |
| 33 |
fmt_connection = KERN_DEBUG "\t\"%pI4/%d\" -> \"%pI4/%d\";\n"; |
| 34 |
fmt_declaration = KERN_DEBUG "\t\"%pI4/%d\"[style=%s, color=\"#%06x\"];\n"; |
| 35 |
} else if (bits == 128) { |
| 36 |
fmt_connection = KERN_DEBUG "\t\"%pI6/%d\" -> \"%pI6/%d\";\n"; |
| 37 |
fmt_declaration = KERN_DEBUG "\t\"%pI6/%d\"[style=%s, color=\"#%06x\"];\n"; |
| 38 |
} |
| 39 |
if (node->peer) { |
| 40 |
hsiphash_key_t key = { { 0 } }; |
| 41 |
|
| 42 |
memcpy(&key, &node->peer, sizeof(node->peer)); |
| 43 |
color = hsiphash_1u32(0xdeadbeef, &key) % 200 << 16 | |
| 44 |
hsiphash_1u32(0xbabecafe, &key) % 200 << 8 | |
| 45 |
hsiphash_1u32(0xabad1dea, &key) % 200; |
| 46 |
style = "bold"; |
| 47 |
} |
| 48 |
wg_allowedips_read_node(node, ip1, &cidr1); |
| 49 |
printk(fmt_declaration, ip1, cidr1, style, color); |
| 50 |
if (node->bit[0]) { |
| 51 |
wg_allowedips_read_node(rcu_dereference_raw(node->bit[0]), ip2, &cidr2); |
| 52 |
printk(fmt_connection, ip1, cidr1, ip2, cidr2); |
| 53 |
} |
| 54 |
if (node->bit[1]) { |
| 55 |
wg_allowedips_read_node(rcu_dereference_raw(node->bit[1]), ip2, &cidr2); |
| 56 |
printk(fmt_connection, ip1, cidr1, ip2, cidr2); |
| 57 |
} |
| 58 |
if (node->bit[0]) |
| 59 |
print_node(rcu_dereference_raw(node->bit[0]), bits); |
| 60 |
if (node->bit[1]) |
| 61 |
print_node(rcu_dereference_raw(node->bit[1]), bits); |
| 62 |
} |
| 63 |
|
| 64 |
static __init void print_tree(struct allowedips_node __rcu *top, u8 bits) |
| 65 |
{ |
| 66 |
printk(KERN_DEBUG "digraph trie {\n"); |
| 67 |
print_node(rcu_dereference_raw(top), bits); |
| 68 |
printk(KERN_DEBUG "}\n"); |
| 69 |
} |
| 70 |
|
| 71 |
enum { |
| 72 |
NUM_PEERS = 2000, |
| 73 |
NUM_RAND_ROUTES = 400, |
| 74 |
NUM_MUTATED_ROUTES = 100, |
| 75 |
NUM_QUERIES = NUM_RAND_ROUTES * NUM_MUTATED_ROUTES * 30 |
| 76 |
}; |
| 77 |
|
| 78 |
struct horrible_allowedips { |
| 79 |
struct hlist_head head; |
| 80 |
}; |
| 81 |
|
| 82 |
struct horrible_allowedips_node { |
| 83 |
struct hlist_node table; |
| 84 |
union nf_inet_addr ip; |
| 85 |
union nf_inet_addr mask; |
| 86 |
u8 ip_version; |
| 87 |
void *value; |
| 88 |
}; |
| 89 |
|
| 90 |
static __init void horrible_allowedips_init(struct horrible_allowedips *table) |
| 91 |
{ |
| 92 |
INIT_HLIST_HEAD(&table->head); |
| 93 |
} |
| 94 |
|
| 95 |
static __init void horrible_allowedips_free(struct horrible_allowedips *table) |
| 96 |
{ |
| 97 |
struct horrible_allowedips_node *node; |
| 98 |
struct hlist_node *h; |
| 99 |
|
| 100 |
hlist_for_each_entry_safe(node, h, &table->head, table) { |
| 101 |
hlist_del(&node->table); |
| 102 |
kfree(node); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static __init inline union nf_inet_addr horrible_cidr_to_mask(u8 cidr) |
| 107 |
{ |
| 108 |
union nf_inet_addr mask; |
| 109 |
|
| 110 |
memset(&mask, 0, sizeof(mask)); |
| 111 |
memset(&mask.all, 0xff, cidr / 8); |
| 112 |
if (cidr % 32) |
| 113 |
mask.all[cidr / 32] = (__force u32)htonl( |
| 114 |
(0xFFFFFFFFUL << (32 - (cidr % 32))) & 0xFFFFFFFFUL); |
| 115 |
return mask; |
| 116 |
} |
| 117 |
|
| 118 |
static __init inline u8 horrible_mask_to_cidr(union nf_inet_addr subnet) |
| 119 |
{ |
| 120 |
return hweight32(subnet.all[0]) + hweight32(subnet.all[1]) + |
| 121 |
hweight32(subnet.all[2]) + hweight32(subnet.all[3]); |
| 122 |
} |
| 123 |
|
| 124 |
static __init inline void |
| 125 |
horrible_mask_self(struct horrible_allowedips_node *node) |
| 126 |
{ |
| 127 |
if (node->ip_version == 4) { |
| 128 |
node->ip.ip &= node->mask.ip; |
| 129 |
} else if (node->ip_version == 6) { |
| 130 |
node->ip.ip6[0] &= node->mask.ip6[0]; |
| 131 |
node->ip.ip6[1] &= node->mask.ip6[1]; |
| 132 |
node->ip.ip6[2] &= node->mask.ip6[2]; |
| 133 |
node->ip.ip6[3] &= node->mask.ip6[3]; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static __init inline bool |
| 138 |
horrible_match_v4(const struct horrible_allowedips_node *node, struct in_addr *ip) |
| 139 |
{ |
| 140 |
return (ip->s_addr & node->mask.ip) == node->ip.ip; |
| 141 |
} |
| 142 |
|
| 143 |
static __init inline bool |
| 144 |
horrible_match_v6(const struct horrible_allowedips_node *node, struct in6_addr *ip) |
| 145 |
{ |
| 146 |
return (ip->in6_u.u6_addr32[0] & node->mask.ip6[0]) == node->ip.ip6[0] && |
| 147 |
(ip->in6_u.u6_addr32[1] & node->mask.ip6[1]) == node->ip.ip6[1] && |
| 148 |
(ip->in6_u.u6_addr32[2] & node->mask.ip6[2]) == node->ip.ip6[2] && |
| 149 |
(ip->in6_u.u6_addr32[3] & node->mask.ip6[3]) == node->ip.ip6[3]; |
| 150 |
} |
| 151 |
|
| 152 |
static __init void |
| 153 |
horrible_insert_ordered(struct horrible_allowedips *table, struct horrible_allowedips_node *node) |
| 154 |
{ |
| 155 |
struct horrible_allowedips_node *other = NULL, *where = NULL; |
| 156 |
u8 my_cidr = horrible_mask_to_cidr(node->mask); |
| 157 |
|
| 158 |
hlist_for_each_entry(other, &table->head, table) { |
| 159 |
if (other->ip_version == node->ip_version && |
| 160 |
!memcmp(&other->mask, &node->mask, sizeof(union nf_inet_addr)) && |
| 161 |
!memcmp(&other->ip, &node->ip, sizeof(union nf_inet_addr))) { |
| 162 |
other->value = node->value; |
| 163 |
kfree(node); |
| 164 |
return; |
| 165 |
} |
| 166 |
} |
| 167 |
hlist_for_each_entry(other, &table->head, table) { |
| 168 |
where = other; |
| 169 |
if (horrible_mask_to_cidr(other->mask) <= my_cidr) |
| 170 |
break; |
| 171 |
} |
| 172 |
if (!other && !where) |
| 173 |
hlist_add_head(&node->table, &table->head); |
| 174 |
else if (!other) |
| 175 |
hlist_add_behind(&node->table, &where->table); |
| 176 |
else |
| 177 |
hlist_add_before(&node->table, &where->table); |
| 178 |
} |
| 179 |
|
| 180 |
static __init int |
| 181 |
horrible_allowedips_insert_v4(struct horrible_allowedips *table, |
| 182 |
struct in_addr *ip, u8 cidr, void *value) |
| 183 |
{ |
| 184 |
struct horrible_allowedips_node *node = kzalloc_obj(*node); |
| 185 |
|
| 186 |
if (unlikely(!node)) |
| 187 |
return -ENOMEM; |
| 188 |
node->ip.in = *ip; |
| 189 |
node->mask = horrible_cidr_to_mask(cidr); |
| 190 |
node->ip_version = 4; |
| 191 |
node->value = value; |
| 192 |
horrible_mask_self(node); |
| 193 |
horrible_insert_ordered(table, node); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static __init int |
| 198 |
horrible_allowedips_insert_v6(struct horrible_allowedips *table, |
| 199 |
struct in6_addr *ip, u8 cidr, void *value) |
| 200 |
{ |
| 201 |
struct horrible_allowedips_node *node = kzalloc_obj(*node); |
| 202 |
|
| 203 |
if (unlikely(!node)) |
| 204 |
return -ENOMEM; |
| 205 |
node->ip.in6 = *ip; |
| 206 |
node->mask = horrible_cidr_to_mask(cidr); |
| 207 |
node->ip_version = 6; |
| 208 |
node->value = value; |
| 209 |
horrible_mask_self(node); |
| 210 |
horrible_insert_ordered(table, node); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static __init void * |
| 215 |
horrible_allowedips_lookup_v4(struct horrible_allowedips *table, struct in_addr *ip) |
| 216 |
{ |
| 217 |
struct horrible_allowedips_node *node; |
| 218 |
|
| 219 |
hlist_for_each_entry(node, &table->head, table) { |
| 220 |
if (node->ip_version == 4 && horrible_match_v4(node, ip)) |
| 221 |
return node->value; |
| 222 |
} |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
static __init void * |
| 227 |
horrible_allowedips_lookup_v6(struct horrible_allowedips *table, struct in6_addr *ip) |
| 228 |
{ |
| 229 |
struct horrible_allowedips_node *node; |
| 230 |
|
| 231 |
hlist_for_each_entry(node, &table->head, table) { |
| 232 |
if (node->ip_version == 6 && horrible_match_v6(node, ip)) |
| 233 |
return node->value; |
| 234 |
} |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
static __init void |
| 240 |
horrible_allowedips_remove_by_value(struct horrible_allowedips *table, void *value) |
| 241 |
{ |
| 242 |
struct horrible_allowedips_node *node; |
| 243 |
struct hlist_node *h; |
| 244 |
|
| 245 |
hlist_for_each_entry_safe(node, h, &table->head, table) { |
| 246 |
if (node->value != value) |
| 247 |
continue; |
| 248 |
hlist_del(&node->table); |
| 249 |
kfree(node); |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
static __init bool randomized_test(void) |
| 255 |
{ |
| 256 |
unsigned int i, j, k, mutate_amount, cidr; |
| 257 |
u8 ip[16], mutate_mask[16], mutated[16]; |
| 258 |
struct wg_peer **peers, *peer; |
| 259 |
struct horrible_allowedips h; |
| 260 |
DEFINE_MUTEX(mutex); |
| 261 |
struct allowedips t; |
| 262 |
bool ret = false; |
| 263 |
|
| 264 |
mutex_init(&mutex); |
| 265 |
|
| 266 |
wg_allowedips_init(&t); |
| 267 |
horrible_allowedips_init(&h); |
| 268 |
|
| 269 |
peers = kzalloc_objs(*peers, NUM_PEERS); |
| 270 |
if (unlikely(!peers)) { |
| 271 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 272 |
goto free; |
| 273 |
} |
| 274 |
for (i = 0; i < NUM_PEERS; ++i) { |
| 275 |
peers[i] = kzalloc_obj(*peers[i]); |
| 276 |
if (unlikely(!peers[i])) { |
| 277 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 278 |
goto free; |
| 279 |
} |
| 280 |
kref_init(&peers[i]->refcount); |
| 281 |
INIT_LIST_HEAD(&peers[i]->allowedips_list); |
| 282 |
} |
| 283 |
|
| 284 |
mutex_lock(&mutex); |
| 285 |
|
| 286 |
for (i = 0; i < NUM_RAND_ROUTES; ++i) { |
| 287 |
get_random_bytes(ip, 4); |
| 288 |
cidr = get_random_u32_inclusive(1, 32); |
| 289 |
peer = peers[get_random_u32_below(NUM_PEERS)]; |
| 290 |
if (wg_allowedips_insert_v4(&t, (struct in_addr *)ip, cidr, |
| 291 |
peer, &mutex) < 0) { |
| 292 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 293 |
goto free_locked; |
| 294 |
} |
| 295 |
if (horrible_allowedips_insert_v4(&h, (struct in_addr *)ip, |
| 296 |
cidr, peer) < 0) { |
| 297 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 298 |
goto free_locked; |
| 299 |
} |
| 300 |
for (j = 0; j < NUM_MUTATED_ROUTES; ++j) { |
| 301 |
memcpy(mutated, ip, 4); |
| 302 |
get_random_bytes(mutate_mask, 4); |
| 303 |
mutate_amount = get_random_u32_below(32); |
| 304 |
for (k = 0; k < mutate_amount / 8; ++k) |
| 305 |
mutate_mask[k] = 0xff; |
| 306 |
mutate_mask[k] = 0xff |
| 307 |
<< ((8 - (mutate_amount % 8)) % 8); |
| 308 |
for (; k < 4; ++k) |
| 309 |
mutate_mask[k] = 0; |
| 310 |
for (k = 0; k < 4; ++k) |
| 311 |
mutated[k] = (mutated[k] & mutate_mask[k]) | |
| 312 |
(~mutate_mask[k] & |
| 313 |
get_random_u8()); |
| 314 |
cidr = get_random_u32_inclusive(1, 32); |
| 315 |
peer = peers[get_random_u32_below(NUM_PEERS)]; |
| 316 |
if (wg_allowedips_insert_v4(&t, |
| 317 |
(struct in_addr *)mutated, |
| 318 |
cidr, peer, &mutex) < 0) { |
| 319 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 320 |
goto free_locked; |
| 321 |
} |
| 322 |
if (horrible_allowedips_insert_v4(&h, |
| 323 |
(struct in_addr *)mutated, cidr, peer)) { |
| 324 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 325 |
goto free_locked; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
for (i = 0; i < NUM_RAND_ROUTES; ++i) { |
| 331 |
get_random_bytes(ip, 16); |
| 332 |
cidr = get_random_u32_inclusive(1, 128); |
| 333 |
peer = peers[get_random_u32_below(NUM_PEERS)]; |
| 334 |
if (wg_allowedips_insert_v6(&t, (struct in6_addr *)ip, cidr, |
| 335 |
peer, &mutex) < 0) { |
| 336 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 337 |
goto free_locked; |
| 338 |
} |
| 339 |
if (horrible_allowedips_insert_v6(&h, (struct in6_addr *)ip, |
| 340 |
cidr, peer) < 0) { |
| 341 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 342 |
goto free_locked; |
| 343 |
} |
| 344 |
for (j = 0; j < NUM_MUTATED_ROUTES; ++j) { |
| 345 |
memcpy(mutated, ip, 16); |
| 346 |
get_random_bytes(mutate_mask, 16); |
| 347 |
mutate_amount = get_random_u32_below(128); |
| 348 |
for (k = 0; k < mutate_amount / 8; ++k) |
| 349 |
mutate_mask[k] = 0xff; |
| 350 |
mutate_mask[k] = 0xff |
| 351 |
<< ((8 - (mutate_amount % 8)) % 8); |
| 352 |
for (; k < 4; ++k) |
| 353 |
mutate_mask[k] = 0; |
| 354 |
for (k = 0; k < 4; ++k) |
| 355 |
mutated[k] = (mutated[k] & mutate_mask[k]) | |
| 356 |
(~mutate_mask[k] & |
| 357 |
get_random_u8()); |
| 358 |
cidr = get_random_u32_inclusive(1, 128); |
| 359 |
peer = peers[get_random_u32_below(NUM_PEERS)]; |
| 360 |
if (wg_allowedips_insert_v6(&t, |
| 361 |
(struct in6_addr *)mutated, |
| 362 |
cidr, peer, &mutex) < 0) { |
| 363 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 364 |
goto free_locked; |
| 365 |
} |
| 366 |
if (horrible_allowedips_insert_v6( |
| 367 |
&h, (struct in6_addr *)mutated, cidr, |
| 368 |
peer)) { |
| 369 |
pr_err("allowedips random self-test malloc: FAIL\n"); |
| 370 |
goto free_locked; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
mutex_unlock(&mutex); |
| 376 |
|
| 377 |
if (IS_ENABLED(DEBUG_PRINT_TRIE_GRAPHVIZ)) { |
| 378 |
print_tree(t.root4, 32); |
| 379 |
print_tree(t.root6, 128); |
| 380 |
} |
| 381 |
|
| 382 |
for (j = 0;; ++j) { |
| 383 |
for (i = 0; i < NUM_QUERIES; ++i) { |
| 384 |
get_random_bytes(ip, 4); |
| 385 |
if (lookup(t.root4, 32, ip) != horrible_allowedips_lookup_v4(&h, (struct in_addr *)ip)) { |
| 386 |
pr_err("allowedips random v4 self-test: FAIL\n"); |
| 387 |
goto free; |
| 388 |
} |
| 389 |
get_random_bytes(ip, 16); |
| 390 |
if (lookup(t.root6, 128, ip) != horrible_allowedips_lookup_v6(&h, (struct in6_addr *)ip)) { |
| 391 |
pr_err("allowedips random v6 self-test: FAIL\n"); |
| 392 |
goto free; |
| 393 |
} |
| 394 |
} |
| 395 |
if (j >= NUM_PEERS) |
| 396 |
break; |
| 397 |
mutex_lock(&mutex); |
| 398 |
wg_allowedips_remove_by_peer(&t, peers[j], &mutex); |
| 399 |
mutex_unlock(&mutex); |
| 400 |
horrible_allowedips_remove_by_value(&h, peers[j]); |
| 401 |
} |
| 402 |
|
| 403 |
if (t.root4 || t.root6) { |
| 404 |
pr_err("allowedips random self-test removal: FAIL\n"); |
| 405 |
goto free; |
| 406 |
} |
| 407 |
|
| 408 |
ret = true; |
| 409 |
|
| 410 |
free: |
| 411 |
mutex_lock(&mutex); |
| 412 |
free_locked: |
| 413 |
wg_allowedips_free(&t, &mutex); |
| 414 |
mutex_unlock(&mutex); |
| 415 |
horrible_allowedips_free(&h); |
| 416 |
if (peers) { |
| 417 |
for (i = 0; i < NUM_PEERS; ++i) |
| 418 |
kfree(peers[i]); |
| 419 |
} |
| 420 |
kfree(peers); |
| 421 |
return ret; |
| 422 |
} |
| 423 |
|
| 424 |
static __init inline struct in_addr *ip4(u8 a, u8 b, u8 c, u8 d) |
| 425 |
{ |
| 426 |
static struct in_addr ip; |
| 427 |
u8 *split = (u8 *)&ip; |
| 428 |
|
| 429 |
split[0] = a; |
| 430 |
split[1] = b; |
| 431 |
split[2] = c; |
| 432 |
split[3] = d; |
| 433 |
return &ip; |
| 434 |
} |
| 435 |
|
| 436 |
static __init inline struct in6_addr *ip6(u32 a, u32 b, u32 c, u32 d) |
| 437 |
{ |
| 438 |
static struct in6_addr ip; |
| 439 |
__be32 *split = (__be32 *)&ip; |
| 440 |
|
| 441 |
split[0] = cpu_to_be32(a); |
| 442 |
split[1] = cpu_to_be32(b); |
| 443 |
split[2] = cpu_to_be32(c); |
| 444 |
split[3] = cpu_to_be32(d); |
| 445 |
return &ip; |
| 446 |
} |
| 447 |
|
| 448 |
static __init struct wg_peer *init_peer(void) |
| 449 |
{ |
| 450 |
struct wg_peer *peer = kzalloc_obj(*peer); |
| 451 |
|
| 452 |
if (!peer) |
| 453 |
return NULL; |
| 454 |
kref_init(&peer->refcount); |
| 455 |
INIT_LIST_HEAD(&peer->allowedips_list); |
| 456 |
return peer; |
| 457 |
} |
| 458 |
|
| 459 |
#define insert(version, mem, ipa, ipb, ipc, ipd, cidr) \ |
| 460 |
wg_allowedips_insert_v##version(&t, ip##version(ipa, ipb, ipc, ipd), \ |
| 461 |
cidr, mem, &mutex) |
| 462 |
|
| 463 |
#define remove(version, mem, ipa, ipb, ipc, ipd, cidr) \ |
| 464 |
wg_allowedips_remove_v##version(&t, ip##version(ipa, ipb, ipc, ipd), \ |
| 465 |
cidr, mem, &mutex) |
| 466 |
|
| 467 |
#define maybe_fail() do { \ |
| 468 |
++i; \ |
| 469 |
if (!_s) { \ |
| 470 |
pr_info("allowedips self-test %zu: FAIL\n", i); \ |
| 471 |
success = false; \ |
| 472 |
} \ |
| 473 |
} while (0) |
| 474 |
|
| 475 |
#define test(version, mem, ipa, ipb, ipc, ipd) do { \ |
| 476 |
bool _s = lookup(t.root##version, (version) == 4 ? 32 : 128, \ |
| 477 |
ip##version(ipa, ipb, ipc, ipd)) == (mem); \ |
| 478 |
maybe_fail(); \ |
| 479 |
} while (0) |
| 480 |
|
| 481 |
#define test_negative(version, mem, ipa, ipb, ipc, ipd) do { \ |
| 482 |
bool _s = lookup(t.root##version, (version) == 4 ? 32 : 128, \ |
| 483 |
ip##version(ipa, ipb, ipc, ipd)) != (mem); \ |
| 484 |
maybe_fail(); \ |
| 485 |
} while (0) |
| 486 |
|
| 487 |
#define test_boolean(cond) do { \ |
| 488 |
bool _s = (cond); \ |
| 489 |
maybe_fail(); \ |
| 490 |
} while (0) |
| 491 |
|
| 492 |
bool __init wg_allowedips_selftest(void) |
| 493 |
{ |
| 494 |
bool found_a = false, found_b = false, found_c = false, found_d = false, |
| 495 |
found_e = false, found_other = false; |
| 496 |
struct wg_peer *a = init_peer(), *b = init_peer(), *c = init_peer(), |
| 497 |
*d = init_peer(), *e = init_peer(), *f = init_peer(), |
| 498 |
*g = init_peer(), *h = init_peer(); |
| 499 |
struct allowedips_node *iter_node; |
| 500 |
bool success = false; |
| 501 |
struct allowedips t; |
| 502 |
DEFINE_MUTEX(mutex); |
| 503 |
struct in6_addr ip; |
| 504 |
size_t i = 0, count = 0; |
| 505 |
__be64 part; |
| 506 |
|
| 507 |
mutex_init(&mutex); |
| 508 |
mutex_lock(&mutex); |
| 509 |
wg_allowedips_init(&t); |
| 510 |
|
| 511 |
if (!a || !b || !c || !d || !e || !f || !g || !h) { |
| 512 |
pr_err("allowedips self-test malloc: FAIL\n"); |
| 513 |
goto free; |
| 514 |
} |
| 515 |
|
| 516 |
insert(4, a, 192, 168, 4, 0, 24); |
| 517 |
insert(4, b, 192, 168, 4, 4, 32); |
| 518 |
insert(4, c, 192, 168, 0, 0, 16); |
| 519 |
insert(4, d, 192, 95, 5, 64, 27); |
| 520 |
/* replaces previous entry, and maskself is required */ |
| 521 |
insert(4, c, 192, 95, 5, 65, 27); |
| 522 |
insert(6, d, 0x26075300, 0x60006b00, 0, 0xc05f0543, 128); |
| 523 |
insert(6, c, 0x26075300, 0x60006b00, 0, 0, 64); |
| 524 |
insert(4, e, 0, 0, 0, 0, 0); |
| 525 |
insert(6, e, 0, 0, 0, 0, 0); |
| 526 |
/* replaces previous entry */ |
| 527 |
insert(6, f, 0, 0, 0, 0, 0); |
| 528 |
insert(6, g, 0x24046800, 0, 0, 0, 32); |
| 529 |
/* maskself is required */ |
| 530 |
insert(6, h, 0x24046800, 0x40040800, 0xdeadbeef, 0xdeadbeef, 64); |
| 531 |
insert(6, a, 0x24046800, 0x40040800, 0xdeadbeef, 0xdeadbeef, 128); |
| 532 |
insert(6, c, 0x24446800, 0x40e40800, 0xdeaebeef, 0xdefbeef, 128); |
| 533 |
insert(6, b, 0x24446800, 0xf0e40800, 0xeeaebeef, 0, 98); |
| 534 |
insert(4, g, 64, 15, 112, 0, 20); |
| 535 |
/* maskself is required */ |
| 536 |
insert(4, h, 64, 15, 123, 211, 25); |
| 537 |
insert(4, a, 10, 0, 0, 0, 25); |
| 538 |
insert(4, b, 10, 0, 0, 128, 25); |
| 539 |
insert(4, a, 10, 1, 0, 0, 30); |
| 540 |
insert(4, b, 10, 1, 0, 4, 30); |
| 541 |
insert(4, c, 10, 1, 0, 8, 29); |
| 542 |
insert(4, d, 10, 1, 0, 16, 29); |
| 543 |
|
| 544 |
if (IS_ENABLED(DEBUG_PRINT_TRIE_GRAPHVIZ)) { |
| 545 |
print_tree(t.root4, 32); |
| 546 |
print_tree(t.root6, 128); |
| 547 |
} |
| 548 |
|
| 549 |
success = true; |
| 550 |
|
| 551 |
test(4, a, 192, 168, 4, 20); |
| 552 |
test(4, a, 192, 168, 4, 0); |
| 553 |
test(4, b, 192, 168, 4, 4); |
| 554 |
test(4, c, 192, 168, 200, 182); |
| 555 |
test(4, c, 192, 95, 5, 68); |
| 556 |
test(4, e, 192, 95, 5, 96); |
| 557 |
test(6, d, 0x26075300, 0x60006b00, 0, 0xc05f0543); |
| 558 |
test(6, c, 0x26075300, 0x60006b00, 0, 0xc02e01ee); |
| 559 |
test(6, f, 0x26075300, 0x60006b01, 0, 0); |
| 560 |
test(6, g, 0x24046800, 0x40040806, 0, 0x1006); |
| 561 |
test(6, g, 0x24046800, 0x40040806, 0x1234, 0x5678); |
| 562 |
test(6, f, 0x240467ff, 0x40040806, 0x1234, 0x5678); |
| 563 |
test(6, f, 0x24046801, 0x40040806, 0x1234, 0x5678); |
| 564 |
test(6, h, 0x24046800, 0x40040800, 0x1234, 0x5678); |
| 565 |
test(6, h, 0x24046800, 0x40040800, 0, 0); |
| 566 |
test(6, h, 0x24046800, 0x40040800, 0x10101010, 0x10101010); |
| 567 |
test(6, a, 0x24046800, 0x40040800, 0xdeadbeef, 0xdeadbeef); |
| 568 |
test(4, g, 64, 15, 116, 26); |
| 569 |
test(4, g, 64, 15, 127, 3); |
| 570 |
test(4, g, 64, 15, 123, 1); |
| 571 |
test(4, h, 64, 15, 123, 128); |
| 572 |
test(4, h, 64, 15, 123, 129); |
| 573 |
test(4, a, 10, 0, 0, 52); |
| 574 |
test(4, b, 10, 0, 0, 220); |
| 575 |
test(4, a, 10, 1, 0, 2); |
| 576 |
test(4, b, 10, 1, 0, 6); |
| 577 |
test(4, c, 10, 1, 0, 10); |
| 578 |
test(4, d, 10, 1, 0, 20); |
| 579 |
|
| 580 |
insert(4, a, 1, 0, 0, 0, 32); |
| 581 |
insert(4, a, 64, 0, 0, 0, 32); |
| 582 |
insert(4, a, 128, 0, 0, 0, 32); |
| 583 |
insert(4, a, 192, 0, 0, 0, 32); |
| 584 |
insert(4, a, 255, 0, 0, 0, 32); |
| 585 |
wg_allowedips_remove_by_peer(&t, a, &mutex); |
| 586 |
test_negative(4, a, 1, 0, 0, 0); |
| 587 |
test_negative(4, a, 64, 0, 0, 0); |
| 588 |
test_negative(4, a, 128, 0, 0, 0); |
| 589 |
test_negative(4, a, 192, 0, 0, 0); |
| 590 |
test_negative(4, a, 255, 0, 0, 0); |
| 591 |
|
| 592 |
insert(4, a, 1, 0, 0, 0, 32); |
| 593 |
insert(4, a, 192, 0, 0, 0, 24); |
| 594 |
insert(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 128); |
| 595 |
insert(6, a, 0x24446800, 0xf0e40800, 0xeeaebeef, 0, 98); |
| 596 |
test(4, a, 1, 0, 0, 0); |
| 597 |
test(4, a, 192, 0, 0, 1); |
| 598 |
test(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 599 |
test(6, a, 0x24446800, 0xf0e40800, 0xeeaebeef, 0x10101010); |
| 600 |
/* Must be an exact match to remove */ |
| 601 |
remove(4, a, 192, 0, 0, 0, 32); |
| 602 |
test(4, a, 192, 0, 0, 1); |
| 603 |
/* NULL peer should have no effect and return 0 */ |
| 604 |
test_boolean(!remove(4, NULL, 192, 0, 0, 0, 24)); |
| 605 |
test(4, a, 192, 0, 0, 1); |
| 606 |
/* different peer should have no effect and return 0 */ |
| 607 |
test_boolean(!remove(4, b, 192, 0, 0, 0, 24)); |
| 608 |
test(4, a, 192, 0, 0, 1); |
| 609 |
/* invalid CIDR should have no effect and return -EINVAL */ |
| 610 |
test_boolean(remove(4, b, 192, 0, 0, 0, 33) == -EINVAL); |
| 611 |
test(4, a, 192, 0, 0, 1); |
| 612 |
remove(4, a, 192, 0, 0, 0, 24); |
| 613 |
test_negative(4, a, 192, 0, 0, 1); |
| 614 |
remove(4, a, 1, 0, 0, 0, 32); |
| 615 |
test_negative(4, a, 1, 0, 0, 0); |
| 616 |
/* Must be an exact match to remove */ |
| 617 |
remove(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 96); |
| 618 |
test(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 619 |
/* NULL peer should have no effect and return 0 */ |
| 620 |
test_boolean(!remove(6, NULL, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 128)); |
| 621 |
test(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 622 |
/* different peer should have no effect and return 0 */ |
| 623 |
test_boolean(!remove(6, b, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 128)); |
| 624 |
test(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 625 |
/* invalid CIDR should have no effect and return -EINVAL */ |
| 626 |
test_boolean(remove(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 129) == -EINVAL); |
| 627 |
test(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 628 |
remove(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef, 128); |
| 629 |
test_negative(6, a, 0x24446801, 0x40e40800, 0xdeaebeef, 0xdefbeef); |
| 630 |
/* Must match the peer to remove */ |
| 631 |
remove(6, b, 0x24446800, 0xf0e40800, 0xeeaebeef, 0, 98); |
| 632 |
test(6, a, 0x24446800, 0xf0e40800, 0xeeaebeef, 0x10101010); |
| 633 |
remove(6, a, 0x24446800, 0xf0e40800, 0xeeaebeef, 0, 98); |
| 634 |
test_negative(6, a, 0x24446800, 0xf0e40800, 0xeeaebeef, 0x10101010); |
| 635 |
|
| 636 |
wg_allowedips_free(&t, &mutex); |
| 637 |
wg_allowedips_init(&t); |
| 638 |
insert(4, a, 192, 168, 0, 0, 16); |
| 639 |
insert(4, a, 192, 168, 0, 0, 24); |
| 640 |
wg_allowedips_remove_by_peer(&t, a, &mutex); |
| 641 |
test_negative(4, a, 192, 168, 0, 1); |
| 642 |
|
| 643 |
/* These will hit the WARN_ON(len >= MAX_ALLOWEDIPS_DEPTH) in free_node |
| 644 |
* if something goes wrong. |
| 645 |
*/ |
| 646 |
for (i = 0; i < 64; ++i) { |
| 647 |
part = cpu_to_be64(~0LLU << i); |
| 648 |
memset(&ip, 0xff, 8); |
| 649 |
memcpy((u8 *)&ip + 8, &part, 8); |
| 650 |
wg_allowedips_insert_v6(&t, &ip, 128, a, &mutex); |
| 651 |
memcpy(&ip, &part, 8); |
| 652 |
memset((u8 *)&ip + 8, 0, 8); |
| 653 |
wg_allowedips_insert_v6(&t, &ip, 128, a, &mutex); |
| 654 |
} |
| 655 |
memset(&ip, 0, 16); |
| 656 |
wg_allowedips_insert_v6(&t, &ip, 128, a, &mutex); |
| 657 |
wg_allowedips_free(&t, &mutex); |
| 658 |
|
| 659 |
wg_allowedips_init(&t); |
| 660 |
insert(4, a, 192, 95, 5, 93, 27); |
| 661 |
insert(6, a, 0x26075300, 0x60006b00, 0, 0xc05f0543, 128); |
| 662 |
insert(4, a, 10, 1, 0, 20, 29); |
| 663 |
insert(6, a, 0x26075300, 0x6d8a6bf8, 0xdab1f1df, 0xc05f1523, 83); |
| 664 |
insert(6, a, 0x26075300, 0x6d8a6bf8, 0xdab1f1df, 0xc05f1523, 21); |
| 665 |
list_for_each_entry(iter_node, &a->allowedips_list, peer_list) { |
| 666 |
u8 cidr, ip[16] __aligned(__alignof(u64)); |
| 667 |
int family = wg_allowedips_read_node(iter_node, ip, &cidr); |
| 668 |
|
| 669 |
count++; |
| 670 |
|
| 671 |
if (cidr == 27 && family == AF_INET && |
| 672 |
!memcmp(ip, ip4(192, 95, 5, 64), sizeof(struct in_addr))) |
| 673 |
found_a = true; |
| 674 |
else if (cidr == 128 && family == AF_INET6 && |
| 675 |
!memcmp(ip, ip6(0x26075300, 0x60006b00, 0, 0xc05f0543), |
| 676 |
sizeof(struct in6_addr))) |
| 677 |
found_b = true; |
| 678 |
else if (cidr == 29 && family == AF_INET && |
| 679 |
!memcmp(ip, ip4(10, 1, 0, 16), sizeof(struct in_addr))) |
| 680 |
found_c = true; |
| 681 |
else if (cidr == 83 && family == AF_INET6 && |
| 682 |
!memcmp(ip, ip6(0x26075300, 0x6d8a6bf8, 0xdab1e000, 0), |
| 683 |
sizeof(struct in6_addr))) |
| 684 |
found_d = true; |
| 685 |
else if (cidr == 21 && family == AF_INET6 && |
| 686 |
!memcmp(ip, ip6(0x26075000, 0, 0, 0), |
| 687 |
sizeof(struct in6_addr))) |
| 688 |
found_e = true; |
| 689 |
else |
| 690 |
found_other = true; |
| 691 |
} |
| 692 |
test_boolean(count == 5); |
| 693 |
test_boolean(found_a); |
| 694 |
test_boolean(found_b); |
| 695 |
test_boolean(found_c); |
| 696 |
test_boolean(found_d); |
| 697 |
test_boolean(found_e); |
| 698 |
test_boolean(!found_other); |
| 699 |
|
| 700 |
if (IS_ENABLED(DEBUG_RANDOM_TRIE) && success) |
| 701 |
success = randomized_test(); |
| 702 |
|
| 703 |
if (success) |
| 704 |
pr_info("allowedips self-tests: pass\n"); |
| 705 |
|
| 706 |
free: |
| 707 |
wg_allowedips_free(&t, &mutex); |
| 708 |
kfree(a); |
| 709 |
kfree(b); |
| 710 |
kfree(c); |
| 711 |
kfree(d); |
| 712 |
kfree(e); |
| 713 |
kfree(f); |
| 714 |
kfree(g); |
| 715 |
kfree(h); |
| 716 |
mutex_unlock(&mutex); |
| 717 |
|
| 718 |
return success; |
| 719 |
} |
| 720 |
|
| 721 |
#undef test_negative |
| 722 |
#undef test |
| 723 |
#undef remove |
| 724 |
#undef insert |
| 725 |
#undef init_peer |
| 726 |
|
| 727 |
#endif |