peer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include "peer.h"
  6. #include "device.h"
  7. #include "queueing.h"
  8. #include "timers.h"
  9. #include "peerlookup.h"
  10. #include "noise.h"
  11. #include <linux/kref.h>
  12. #include <linux/lockdep.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/list.h>
  15. static struct kmem_cache *peer_cache;
  16. static atomic64_t peer_counter = ATOMIC64_INIT(0);
  17. struct wg_peer *wg_peer_create(struct wg_device *wg,
  18. const u8 public_key[NOISE_PUBLIC_KEY_LEN],
  19. const u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN])
  20. {
  21. struct wg_peer *peer;
  22. int ret = -ENOMEM;
  23. lockdep_assert_held(&wg->device_update_lock);
  24. if (wg->num_peers >= MAX_PEERS_PER_DEVICE)
  25. return ERR_PTR(ret);
  26. peer = kmem_cache_zalloc(peer_cache, GFP_KERNEL);
  27. if (unlikely(!peer))
  28. return ERR_PTR(ret);
  29. if (unlikely(dst_cache_init(&peer->endpoint_cache, GFP_KERNEL)))
  30. goto err;
  31. peer->device = wg;
  32. wg_noise_handshake_init(&peer->handshake, &wg->static_identity,
  33. public_key, preshared_key, peer);
  34. peer->internal_id = atomic64_inc_return(&peer_counter);
  35. peer->serial_work_cpu = nr_cpumask_bits;
  36. wg_cookie_init(&peer->latest_cookie);
  37. wg_timers_init(peer);
  38. wg_cookie_checker_precompute_peer_keys(peer);
  39. spin_lock_init(&peer->keypairs.keypair_update_lock);
  40. INIT_WORK(&peer->transmit_handshake_work, wg_packet_handshake_send_worker);
  41. INIT_WORK(&peer->transmit_packet_work, wg_packet_tx_worker);
  42. wg_prev_queue_init(&peer->tx_queue);
  43. wg_prev_queue_init(&peer->rx_queue);
  44. rwlock_init(&peer->endpoint_lock);
  45. kref_init(&peer->refcount);
  46. skb_queue_head_init(&peer->staged_packet_queue);
  47. wg_noise_reset_last_sent_handshake(&peer->last_sent_handshake);
  48. set_bit(NAPI_STATE_NO_BUSY_POLL, &peer->napi.state);
  49. netif_napi_add(wg->dev, &peer->napi, wg_packet_rx_poll);
  50. napi_enable(&peer->napi);
  51. list_add_tail(&peer->peer_list, &wg->peer_list);
  52. INIT_LIST_HEAD(&peer->allowedips_list);
  53. wg_pubkey_hashtable_add(wg->peer_hashtable, peer);
  54. ++wg->num_peers;
  55. pr_debug("%s: Peer %llu created\n", wg->dev->name, peer->internal_id);
  56. return peer;
  57. err:
  58. kmem_cache_free(peer_cache, peer);
  59. return ERR_PTR(ret);
  60. }
  61. struct wg_peer *wg_peer_get_maybe_zero(struct wg_peer *peer)
  62. {
  63. RCU_LOCKDEP_WARN(!rcu_read_lock_bh_held(),
  64. "Taking peer reference without holding the RCU read lock");
  65. if (unlikely(!peer || !kref_get_unless_zero(&peer->refcount)))
  66. return NULL;
  67. return peer;
  68. }
  69. static void peer_make_dead(struct wg_peer *peer)
  70. {
  71. /* Remove from configuration-time lookup structures. */
  72. list_del_init(&peer->peer_list);
  73. wg_allowedips_remove_by_peer(&peer->device->peer_allowedips, peer,
  74. &peer->device->device_update_lock);
  75. wg_pubkey_hashtable_remove(peer->device->peer_hashtable, peer);
  76. /* Mark as dead, so that we don't allow jumping contexts after. */
  77. WRITE_ONCE(peer->is_dead, true);
  78. /* The caller must now synchronize_net() for this to take effect. */
  79. }
  80. static void peer_remove_after_dead(struct wg_peer *peer)
  81. {
  82. WARN_ON(!peer->is_dead);
  83. /* No more keypairs can be created for this peer, since is_dead protects
  84. * add_new_keypair, so we can now destroy existing ones.
  85. */
  86. wg_noise_keypairs_clear(&peer->keypairs);
  87. /* Destroy all ongoing timers that were in-flight at the beginning of
  88. * this function.
  89. */
  90. wg_timers_stop(peer);
  91. /* The transition between packet encryption/decryption queues isn't
  92. * guarded by is_dead, but each reference's life is strictly bounded by
  93. * two generations: once for parallel crypto and once for serial
  94. * ingestion, so we can simply flush twice, and be sure that we no
  95. * longer have references inside these queues.
  96. */
  97. /* a) For encrypt/decrypt. */
  98. flush_workqueue(peer->device->packet_crypt_wq);
  99. /* b.1) For send (but not receive, since that's napi). */
  100. flush_workqueue(peer->device->packet_crypt_wq);
  101. /* b.2.1) For receive (but not send, since that's wq). */
  102. napi_disable(&peer->napi);
  103. /* b.2.1) It's now safe to remove the napi struct, which must be done
  104. * here from process context.
  105. */
  106. netif_napi_del(&peer->napi);
  107. /* Ensure any workstructs we own (like transmit_handshake_work or
  108. * clear_peer_work) no longer are in use.
  109. */
  110. flush_workqueue(peer->device->handshake_send_wq);
  111. /* After the above flushes, a peer might still be active in a few
  112. * different contexts: 1) from xmit(), before hitting is_dead and
  113. * returning, 2) from wg_packet_consume_data(), before hitting is_dead
  114. * and returning, 3) from wg_receive_handshake_packet() after a point
  115. * where it has processed an incoming handshake packet, but where
  116. * all calls to pass it off to timers fails because of is_dead. We won't
  117. * have new references in (1) eventually, because we're removed from
  118. * allowedips; we won't have new references in (2) eventually, because
  119. * wg_index_hashtable_lookup will always return NULL, since we removed
  120. * all existing keypairs and no more can be created; we won't have new
  121. * references in (3) eventually, because we're removed from the pubkey
  122. * hash table, which allows for a maximum of one handshake response,
  123. * via the still-uncleared index hashtable entry, but not more than one,
  124. * and in wg_cookie_message_consume, the lookup eventually gets a peer
  125. * with a refcount of zero, so no new reference is taken.
  126. */
  127. --peer->device->num_peers;
  128. wg_peer_put(peer);
  129. }
  130. /* We have a separate "remove" function make sure that all active places where
  131. * a peer is currently operating will eventually come to an end and not pass
  132. * their reference onto another context.
  133. */
  134. void wg_peer_remove(struct wg_peer *peer)
  135. {
  136. if (unlikely(!peer))
  137. return;
  138. lockdep_assert_held(&peer->device->device_update_lock);
  139. peer_make_dead(peer);
  140. synchronize_net();
  141. peer_remove_after_dead(peer);
  142. }
  143. void wg_peer_remove_all(struct wg_device *wg)
  144. {
  145. struct wg_peer *peer, *temp;
  146. LIST_HEAD(dead_peers);
  147. lockdep_assert_held(&wg->device_update_lock);
  148. /* Avoid having to traverse individually for each one. */
  149. wg_allowedips_free(&wg->peer_allowedips, &wg->device_update_lock);
  150. list_for_each_entry_safe(peer, temp, &wg->peer_list, peer_list) {
  151. peer_make_dead(peer);
  152. list_add_tail(&peer->peer_list, &dead_peers);
  153. }
  154. synchronize_net();
  155. list_for_each_entry_safe(peer, temp, &dead_peers, peer_list)
  156. peer_remove_after_dead(peer);
  157. }
  158. static void rcu_release(struct rcu_head *rcu)
  159. {
  160. struct wg_peer *peer = container_of(rcu, struct wg_peer, rcu);
  161. dst_cache_destroy(&peer->endpoint_cache);
  162. WARN_ON(wg_prev_queue_peek(&peer->tx_queue) || wg_prev_queue_peek(&peer->rx_queue));
  163. /* The final zeroing takes care of clearing any remaining handshake key
  164. * material and other potentially sensitive information.
  165. */
  166. memzero_explicit(peer, sizeof(*peer));
  167. kmem_cache_free(peer_cache, peer);
  168. }
  169. static void kref_release(struct kref *refcount)
  170. {
  171. struct wg_peer *peer = container_of(refcount, struct wg_peer, refcount);
  172. pr_debug("%s: Peer %llu (%pISpfsc) destroyed\n",
  173. peer->device->dev->name, peer->internal_id,
  174. &peer->endpoint.addr);
  175. /* Remove ourself from dynamic runtime lookup structures, now that the
  176. * last reference is gone.
  177. */
  178. wg_index_hashtable_remove(peer->device->index_hashtable,
  179. &peer->handshake.entry);
  180. /* Remove any lingering packets that didn't have a chance to be
  181. * transmitted.
  182. */
  183. wg_packet_purge_staged_packets(peer);
  184. /* Free the memory used. */
  185. call_rcu(&peer->rcu, rcu_release);
  186. }
  187. void wg_peer_put(struct wg_peer *peer)
  188. {
  189. if (unlikely(!peer))
  190. return;
  191. kref_put(&peer->refcount, kref_release);
  192. }
  193. int __init wg_peer_init(void)
  194. {
  195. peer_cache = KMEM_CACHE(wg_peer, 0);
  196. return peer_cache ? 0 : -ENOMEM;
  197. }
  198. void wg_peer_uninit(void)
  199. {
  200. kmem_cache_destroy(peer_cache);
  201. }