Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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