allowedips.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include "allowedips.h"
  6. #include "peer.h"
  7. enum { MAX_ALLOWEDIPS_DEPTH = 129 };
  8. static struct kmem_cache *node_cache;
  9. static void swap_endian(u8 *dst, const u8 *src, u8 bits)
  10. {
  11. if (bits == 32) {
  12. *(u32 *)dst = be32_to_cpu(*(const __be32 *)src);
  13. } else if (bits == 128) {
  14. ((u64 *)dst)[0] = be64_to_cpu(((const __be64 *)src)[0]);
  15. ((u64 *)dst)[1] = be64_to_cpu(((const __be64 *)src)[1]);
  16. }
  17. }
  18. static void copy_and_assign_cidr(struct allowedips_node *node, const u8 *src,
  19. u8 cidr, u8 bits)
  20. {
  21. node->cidr = cidr;
  22. node->bit_at_a = cidr / 8U;
  23. #ifdef __LITTLE_ENDIAN
  24. node->bit_at_a ^= (bits / 8U - 1U) % 8U;
  25. #endif
  26. node->bit_at_b = 7U - (cidr % 8U);
  27. node->bitlen = bits;
  28. memcpy(node->bits, src, bits / 8U);
  29. }
  30. static inline u8 choose(struct allowedips_node *node, const u8 *key)
  31. {
  32. return (key[node->bit_at_a] >> node->bit_at_b) & 1;
  33. }
  34. static void push_rcu(struct allowedips_node **stack,
  35. struct allowedips_node __rcu *p, unsigned int *len)
  36. {
  37. if (rcu_access_pointer(p)) {
  38. if (WARN_ON(IS_ENABLED(DEBUG) && *len >= MAX_ALLOWEDIPS_DEPTH))
  39. return;
  40. stack[(*len)++] = rcu_dereference_raw(p);
  41. }
  42. }
  43. static void node_free_rcu(struct rcu_head *rcu)
  44. {
  45. kmem_cache_free(node_cache, container_of(rcu, struct allowedips_node, rcu));
  46. }
  47. static void root_free_rcu(struct rcu_head *rcu)
  48. {
  49. struct allowedips_node *node, *stack[MAX_ALLOWEDIPS_DEPTH] = {
  50. container_of(rcu, struct allowedips_node, rcu) };
  51. unsigned int len = 1;
  52. while (len > 0 && (node = stack[--len])) {
  53. push_rcu(stack, node->bit[0], &len);
  54. push_rcu(stack, node->bit[1], &len);
  55. kmem_cache_free(node_cache, node);
  56. }
  57. }
  58. static void root_remove_peer_lists(struct allowedips_node *root)
  59. {
  60. struct allowedips_node *node, *stack[MAX_ALLOWEDIPS_DEPTH] = { root };
  61. unsigned int len = 1;
  62. while (len > 0 && (node = stack[--len])) {
  63. push_rcu(stack, node->bit[0], &len);
  64. push_rcu(stack, node->bit[1], &len);
  65. if (rcu_access_pointer(node->peer))
  66. list_del(&node->peer_list);
  67. }
  68. }
  69. static unsigned int fls128(u64 a, u64 b)
  70. {
  71. return a ? fls64(a) + 64U : fls64(b);
  72. }
  73. static u8 common_bits(const struct allowedips_node *node, const u8 *key,
  74. u8 bits)
  75. {
  76. if (bits == 32)
  77. return 32U - fls(*(const u32 *)node->bits ^ *(const u32 *)key);
  78. else if (bits == 128)
  79. return 128U - fls128(
  80. *(const u64 *)&node->bits[0] ^ *(const u64 *)&key[0],
  81. *(const u64 *)&node->bits[8] ^ *(const u64 *)&key[8]);
  82. return 0;
  83. }
  84. static bool prefix_matches(const struct allowedips_node *node, const u8 *key,
  85. u8 bits)
  86. {
  87. /* This could be much faster if it actually just compared the common
  88. * bits properly, by precomputing a mask bswap(~0 << (32 - cidr)), and
  89. * the rest, but it turns out that common_bits is already super fast on
  90. * modern processors, even taking into account the unfortunate bswap.
  91. * So, we just inline it like this instead.
  92. */
  93. return common_bits(node, key, bits) >= node->cidr;
  94. }
  95. static struct allowedips_node *find_node(struct allowedips_node *trie, u8 bits,
  96. const u8 *key)
  97. {
  98. struct allowedips_node *node = trie, *found = NULL;
  99. while (node && prefix_matches(node, key, bits)) {
  100. if (rcu_access_pointer(node->peer))
  101. found = node;
  102. if (node->cidr == bits)
  103. break;
  104. node = rcu_dereference_bh(node->bit[choose(node, key)]);
  105. }
  106. return found;
  107. }
  108. /* Returns a strong reference to a peer */
  109. static struct wg_peer *lookup(struct allowedips_node __rcu *root, u8 bits,
  110. const void *be_ip)
  111. {
  112. /* Aligned so it can be passed to fls/fls64 */
  113. u8 ip[16] __aligned(__alignof(u64));
  114. struct allowedips_node *node;
  115. struct wg_peer *peer = NULL;
  116. swap_endian(ip, be_ip, bits);
  117. rcu_read_lock_bh();
  118. retry:
  119. node = find_node(rcu_dereference_bh(root), bits, ip);
  120. if (node) {
  121. peer = wg_peer_get_maybe_zero(rcu_dereference_bh(node->peer));
  122. if (!peer)
  123. goto retry;
  124. }
  125. rcu_read_unlock_bh();
  126. return peer;
  127. }
  128. static bool node_placement(struct allowedips_node __rcu *trie, const u8 *key,
  129. u8 cidr, u8 bits, struct allowedips_node **rnode,
  130. struct mutex *lock)
  131. {
  132. struct allowedips_node *node = rcu_dereference_protected(trie, lockdep_is_held(lock));
  133. struct allowedips_node *parent = NULL;
  134. bool exact = false;
  135. while (node && node->cidr <= cidr && prefix_matches(node, key, bits)) {
  136. parent = node;
  137. if (parent->cidr == cidr) {
  138. exact = true;
  139. break;
  140. }
  141. node = rcu_dereference_protected(parent->bit[choose(parent, key)], lockdep_is_held(lock));
  142. }
  143. *rnode = parent;
  144. return exact;
  145. }
  146. static inline void connect_node(struct allowedips_node __rcu **parent, u8 bit, struct allowedips_node *node)
  147. {
  148. node->parent_bit_packed = (unsigned long)parent | bit;
  149. rcu_assign_pointer(*parent, node);
  150. }
  151. static inline void choose_and_connect_node(struct allowedips_node *parent, struct allowedips_node *node)
  152. {
  153. u8 bit = choose(parent, node->bits);
  154. connect_node(&parent->bit[bit], bit, node);
  155. }
  156. static int add(struct allowedips_node __rcu **trie, u8 bits, const u8 *key,
  157. u8 cidr, struct wg_peer *peer, struct mutex *lock)
  158. {
  159. struct allowedips_node *node, *parent, *down, *newnode;
  160. if (unlikely(cidr > bits || !peer))
  161. return -EINVAL;
  162. if (!rcu_access_pointer(*trie)) {
  163. node = kmem_cache_zalloc(node_cache, GFP_KERNEL);
  164. if (unlikely(!node))
  165. return -ENOMEM;
  166. RCU_INIT_POINTER(node->peer, peer);
  167. list_add_tail(&node->peer_list, &peer->allowedips_list);
  168. copy_and_assign_cidr(node, key, cidr, bits);
  169. connect_node(trie, 2, node);
  170. return 0;
  171. }
  172. if (node_placement(*trie, key, cidr, bits, &node, lock)) {
  173. rcu_assign_pointer(node->peer, peer);
  174. list_move_tail(&node->peer_list, &peer->allowedips_list);
  175. return 0;
  176. }
  177. newnode = kmem_cache_zalloc(node_cache, GFP_KERNEL);
  178. if (unlikely(!newnode))
  179. return -ENOMEM;
  180. RCU_INIT_POINTER(newnode->peer, peer);
  181. list_add_tail(&newnode->peer_list, &peer->allowedips_list);
  182. copy_and_assign_cidr(newnode, key, cidr, bits);
  183. if (!node) {
  184. down = rcu_dereference_protected(*trie, lockdep_is_held(lock));
  185. } else {
  186. const u8 bit = choose(node, key);
  187. down = rcu_dereference_protected(node->bit[bit], lockdep_is_held(lock));
  188. if (!down) {
  189. connect_node(&node->bit[bit], bit, newnode);
  190. return 0;
  191. }
  192. }
  193. cidr = min(cidr, common_bits(down, key, bits));
  194. parent = node;
  195. if (newnode->cidr == cidr) {
  196. choose_and_connect_node(newnode, down);
  197. if (!parent)
  198. connect_node(trie, 2, newnode);
  199. else
  200. choose_and_connect_node(parent, newnode);
  201. return 0;
  202. }
  203. node = kmem_cache_zalloc(node_cache, GFP_KERNEL);
  204. if (unlikely(!node)) {
  205. list_del(&newnode->peer_list);
  206. kmem_cache_free(node_cache, newnode);
  207. return -ENOMEM;
  208. }
  209. INIT_LIST_HEAD(&node->peer_list);
  210. copy_and_assign_cidr(node, newnode->bits, cidr, bits);
  211. choose_and_connect_node(node, down);
  212. choose_and_connect_node(node, newnode);
  213. if (!parent)
  214. connect_node(trie, 2, node);
  215. else
  216. choose_and_connect_node(parent, node);
  217. return 0;
  218. }
  219. void wg_allowedips_init(struct allowedips *table)
  220. {
  221. table->root4 = table->root6 = NULL;
  222. table->seq = 1;
  223. }
  224. void wg_allowedips_free(struct allowedips *table, struct mutex *lock)
  225. {
  226. struct allowedips_node __rcu *old4 = table->root4, *old6 = table->root6;
  227. ++table->seq;
  228. RCU_INIT_POINTER(table->root4, NULL);
  229. RCU_INIT_POINTER(table->root6, NULL);
  230. if (rcu_access_pointer(old4)) {
  231. struct allowedips_node *node = rcu_dereference_protected(old4,
  232. lockdep_is_held(lock));
  233. root_remove_peer_lists(node);
  234. call_rcu(&node->rcu, root_free_rcu);
  235. }
  236. if (rcu_access_pointer(old6)) {
  237. struct allowedips_node *node = rcu_dereference_protected(old6,
  238. lockdep_is_held(lock));
  239. root_remove_peer_lists(node);
  240. call_rcu(&node->rcu, root_free_rcu);
  241. }
  242. }
  243. int wg_allowedips_insert_v4(struct allowedips *table, const struct in_addr *ip,
  244. u8 cidr, struct wg_peer *peer, struct mutex *lock)
  245. {
  246. /* Aligned so it can be passed to fls */
  247. u8 key[4] __aligned(__alignof(u32));
  248. ++table->seq;
  249. swap_endian(key, (const u8 *)ip, 32);
  250. return add(&table->root4, 32, key, cidr, peer, lock);
  251. }
  252. int wg_allowedips_insert_v6(struct allowedips *table, const struct in6_addr *ip,
  253. u8 cidr, struct wg_peer *peer, struct mutex *lock)
  254. {
  255. /* Aligned so it can be passed to fls64 */
  256. u8 key[16] __aligned(__alignof(u64));
  257. ++table->seq;
  258. swap_endian(key, (const u8 *)ip, 128);
  259. return add(&table->root6, 128, key, cidr, peer, lock);
  260. }
  261. void wg_allowedips_remove_by_peer(struct allowedips *table,
  262. struct wg_peer *peer, struct mutex *lock)
  263. {
  264. struct allowedips_node *node, *child, **parent_bit, *parent, *tmp;
  265. bool free_parent;
  266. if (list_empty(&peer->allowedips_list))
  267. return;
  268. ++table->seq;
  269. list_for_each_entry_safe(node, tmp, &peer->allowedips_list, peer_list) {
  270. list_del_init(&node->peer_list);
  271. RCU_INIT_POINTER(node->peer, NULL);
  272. if (node->bit[0] && node->bit[1])
  273. continue;
  274. child = rcu_dereference_protected(node->bit[!rcu_access_pointer(node->bit[0])],
  275. lockdep_is_held(lock));
  276. if (child)
  277. child->parent_bit_packed = node->parent_bit_packed;
  278. parent_bit = (struct allowedips_node **)(node->parent_bit_packed & ~3UL);
  279. *parent_bit = child;
  280. parent = (void *)parent_bit -
  281. offsetof(struct allowedips_node, bit[node->parent_bit_packed & 1]);
  282. free_parent = !rcu_access_pointer(node->bit[0]) &&
  283. !rcu_access_pointer(node->bit[1]) &&
  284. (node->parent_bit_packed & 3) <= 1 &&
  285. !rcu_access_pointer(parent->peer);
  286. if (free_parent)
  287. child = rcu_dereference_protected(
  288. parent->bit[!(node->parent_bit_packed & 1)],
  289. lockdep_is_held(lock));
  290. call_rcu(&node->rcu, node_free_rcu);
  291. if (!free_parent)
  292. continue;
  293. if (child)
  294. child->parent_bit_packed = parent->parent_bit_packed;
  295. *(struct allowedips_node **)(parent->parent_bit_packed & ~3UL) = child;
  296. call_rcu(&parent->rcu, node_free_rcu);
  297. }
  298. }
  299. int wg_allowedips_read_node(struct allowedips_node *node, u8 ip[16], u8 *cidr)
  300. {
  301. const unsigned int cidr_bytes = DIV_ROUND_UP(node->cidr, 8U);
  302. swap_endian(ip, node->bits, node->bitlen);
  303. memset(ip + cidr_bytes, 0, node->bitlen / 8U - cidr_bytes);
  304. if (node->cidr)
  305. ip[cidr_bytes - 1U] &= ~0U << (-node->cidr % 8U);
  306. *cidr = node->cidr;
  307. return node->bitlen == 32 ? AF_INET : AF_INET6;
  308. }
  309. /* Returns a strong reference to a peer */
  310. struct wg_peer *wg_allowedips_lookup_dst(struct allowedips *table,
  311. struct sk_buff *skb)
  312. {
  313. if (skb->protocol == htons(ETH_P_IP))
  314. return lookup(table->root4, 32, &ip_hdr(skb)->daddr);
  315. else if (skb->protocol == htons(ETH_P_IPV6))
  316. return lookup(table->root6, 128, &ipv6_hdr(skb)->daddr);
  317. return NULL;
  318. }
  319. /* Returns a strong reference to a peer */
  320. struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,
  321. struct sk_buff *skb)
  322. {
  323. if (skb->protocol == htons(ETH_P_IP))
  324. return lookup(table->root4, 32, &ip_hdr(skb)->saddr);
  325. else if (skb->protocol == htons(ETH_P_IPV6))
  326. return lookup(table->root6, 128, &ipv6_hdr(skb)->saddr);
  327. return NULL;
  328. }
  329. int __init wg_allowedips_slab_init(void)
  330. {
  331. node_cache = KMEM_CACHE(allowedips_node, 0);
  332. return node_cache ? 0 : -ENOMEM;
  333. }
  334. void wg_allowedips_slab_uninit(void)
  335. {
  336. rcu_barrier();
  337. kmem_cache_destroy(node_cache);
  338. }
  339. #include "selftest/allowedips.c"