allowedips.c 20 KB

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