send.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include "queueing.h"
  6. #include "timers.h"
  7. #include "device.h"
  8. #include "peer.h"
  9. #include "socket.h"
  10. #include "messages.h"
  11. #include "cookie.h"
  12. #include <linux/uio.h>
  13. #include <linux/inetdevice.h>
  14. #include <linux/socket.h>
  15. #include <net/ip_tunnels.h>
  16. #include <net/udp.h>
  17. #include <net/sock.h>
  18. static void wg_packet_send_handshake_initiation(struct wg_peer *peer)
  19. {
  20. struct message_handshake_initiation packet;
  21. if (!wg_birthdate_has_expired(atomic64_read(&peer->last_sent_handshake),
  22. REKEY_TIMEOUT))
  23. return; /* This function is rate limited. */
  24. atomic64_set(&peer->last_sent_handshake, ktime_get_coarse_boottime_ns());
  25. net_dbg_ratelimited("%s: Sending handshake initiation to peer %llu (%pISpfsc)\n",
  26. peer->device->dev->name, peer->internal_id,
  27. &peer->endpoint.addr);
  28. if (wg_noise_handshake_create_initiation(&packet, &peer->handshake)) {
  29. wg_cookie_add_mac_to_packet(&packet, sizeof(packet), peer);
  30. wg_timers_any_authenticated_packet_traversal(peer);
  31. wg_timers_any_authenticated_packet_sent(peer);
  32. atomic64_set(&peer->last_sent_handshake,
  33. ktime_get_coarse_boottime_ns());
  34. wg_socket_send_buffer_to_peer(peer, &packet, sizeof(packet),
  35. HANDSHAKE_DSCP);
  36. wg_timers_handshake_initiated(peer);
  37. }
  38. }
  39. void wg_packet_handshake_send_worker(struct work_struct *work)
  40. {
  41. struct wg_peer *peer = container_of(work, struct wg_peer,
  42. transmit_handshake_work);
  43. wg_packet_send_handshake_initiation(peer);
  44. wg_peer_put(peer);
  45. }
  46. void wg_packet_send_queued_handshake_initiation(struct wg_peer *peer,
  47. bool is_retry)
  48. {
  49. if (!is_retry)
  50. peer->timer_handshake_attempts = 0;
  51. rcu_read_lock_bh();
  52. /* We check last_sent_handshake here in addition to the actual function
  53. * we're queueing up, so that we don't queue things if not strictly
  54. * necessary:
  55. */
  56. if (!wg_birthdate_has_expired(atomic64_read(&peer->last_sent_handshake),
  57. REKEY_TIMEOUT) ||
  58. unlikely(READ_ONCE(peer->is_dead)))
  59. goto out;
  60. wg_peer_get(peer);
  61. /* Queues up calling packet_send_queued_handshakes(peer), where we do a
  62. * peer_put(peer) after:
  63. */
  64. if (!queue_work(peer->device->handshake_send_wq,
  65. &peer->transmit_handshake_work))
  66. /* If the work was already queued, we want to drop the
  67. * extra reference:
  68. */
  69. wg_peer_put(peer);
  70. out:
  71. rcu_read_unlock_bh();
  72. }
  73. void wg_packet_send_handshake_response(struct wg_peer *peer)
  74. {
  75. struct message_handshake_response packet;
  76. atomic64_set(&peer->last_sent_handshake, ktime_get_coarse_boottime_ns());
  77. net_dbg_ratelimited("%s: Sending handshake response to peer %llu (%pISpfsc)\n",
  78. peer->device->dev->name, peer->internal_id,
  79. &peer->endpoint.addr);
  80. if (wg_noise_handshake_create_response(&packet, &peer->handshake)) {
  81. wg_cookie_add_mac_to_packet(&packet, sizeof(packet), peer);
  82. if (wg_noise_handshake_begin_session(&peer->handshake,
  83. &peer->keypairs)) {
  84. wg_timers_session_derived(peer);
  85. wg_timers_any_authenticated_packet_traversal(peer);
  86. wg_timers_any_authenticated_packet_sent(peer);
  87. atomic64_set(&peer->last_sent_handshake,
  88. ktime_get_coarse_boottime_ns());
  89. wg_socket_send_buffer_to_peer(peer, &packet,
  90. sizeof(packet),
  91. HANDSHAKE_DSCP);
  92. }
  93. }
  94. }
  95. void wg_packet_send_handshake_cookie(struct wg_device *wg,
  96. struct sk_buff *initiating_skb,
  97. __le32 sender_index)
  98. {
  99. struct message_handshake_cookie packet;
  100. net_dbg_skb_ratelimited("%s: Sending cookie response for denied handshake message for %pISpfsc\n",
  101. wg->dev->name, initiating_skb);
  102. wg_cookie_message_create(&packet, initiating_skb, sender_index,
  103. &wg->cookie_checker);
  104. wg_socket_send_buffer_as_reply_to_skb(wg, initiating_skb, &packet,
  105. sizeof(packet));
  106. }
  107. static void keep_key_fresh(struct wg_peer *peer)
  108. {
  109. struct noise_keypair *keypair;
  110. bool send;
  111. rcu_read_lock_bh();
  112. keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
  113. send = keypair && READ_ONCE(keypair->sending.is_valid) &&
  114. (atomic64_read(&keypair->sending_counter) > REKEY_AFTER_MESSAGES ||
  115. (keypair->i_am_the_initiator &&
  116. wg_birthdate_has_expired(keypair->sending.birthdate, REKEY_AFTER_TIME)));
  117. rcu_read_unlock_bh();
  118. if (unlikely(send))
  119. wg_packet_send_queued_handshake_initiation(peer, false);
  120. }
  121. static unsigned int calculate_skb_padding(struct sk_buff *skb)
  122. {
  123. unsigned int padded_size, last_unit = skb->len;
  124. if (unlikely(!PACKET_CB(skb)->mtu))
  125. return ALIGN(last_unit, MESSAGE_PADDING_MULTIPLE) - last_unit;
  126. /* We do this modulo business with the MTU, just in case the networking
  127. * layer gives us a packet that's bigger than the MTU. In that case, we
  128. * wouldn't want the final subtraction to overflow in the case of the
  129. * padded_size being clamped. Fortunately, that's very rarely the case,
  130. * so we optimize for that not happening.
  131. */
  132. if (unlikely(last_unit > PACKET_CB(skb)->mtu))
  133. last_unit %= PACKET_CB(skb)->mtu;
  134. padded_size = min(PACKET_CB(skb)->mtu,
  135. ALIGN(last_unit, MESSAGE_PADDING_MULTIPLE));
  136. return padded_size - last_unit;
  137. }
  138. static bool encrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
  139. {
  140. unsigned int padding_len, plaintext_len, trailer_len;
  141. struct scatterlist sg[MAX_SKB_FRAGS + 8];
  142. struct message_data *header;
  143. struct sk_buff *trailer;
  144. int num_frags;
  145. /* Force hash calculation before encryption so that flow analysis is
  146. * consistent over the inner packet.
  147. */
  148. skb_get_hash(skb);
  149. /* Calculate lengths. */
  150. padding_len = calculate_skb_padding(skb);
  151. trailer_len = padding_len + noise_encrypted_len(0);
  152. plaintext_len = skb->len + padding_len;
  153. /* Expand data section to have room for padding and auth tag. */
  154. num_frags = skb_cow_data(skb, trailer_len, &trailer);
  155. if (unlikely(num_frags < 0 || num_frags > ARRAY_SIZE(sg)))
  156. return false;
  157. /* Set the padding to zeros, and make sure it and the auth tag are part
  158. * of the skb.
  159. */
  160. memset(skb_tail_pointer(trailer), 0, padding_len);
  161. /* Expand head section to have room for our header and the network
  162. * stack's headers.
  163. */
  164. if (unlikely(skb_cow_head(skb, DATA_PACKET_HEAD_ROOM) < 0))
  165. return false;
  166. /* Finalize checksum calculation for the inner packet, if required. */
  167. if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL &&
  168. skb_checksum_help(skb)))
  169. return false;
  170. /* Only after checksumming can we safely add on the padding at the end
  171. * and the header.
  172. */
  173. skb_set_inner_network_header(skb, 0);
  174. header = (struct message_data *)skb_push(skb, sizeof(*header));
  175. header->header.type = cpu_to_le32(MESSAGE_DATA);
  176. header->key_idx = keypair->remote_index;
  177. header->counter = cpu_to_le64(PACKET_CB(skb)->nonce);
  178. pskb_put(skb, trailer, trailer_len);
  179. /* Now we can encrypt the scattergather segments */
  180. sg_init_table(sg, num_frags);
  181. if (skb_to_sgvec(skb, sg, sizeof(struct message_data),
  182. noise_encrypted_len(plaintext_len)) <= 0)
  183. return false;
  184. return chacha20poly1305_encrypt_sg_inplace(sg, plaintext_len, NULL, 0,
  185. PACKET_CB(skb)->nonce,
  186. keypair->sending.key);
  187. }
  188. void wg_packet_send_keepalive(struct wg_peer *peer)
  189. {
  190. struct sk_buff *skb;
  191. if (skb_queue_empty(&peer->staged_packet_queue)) {
  192. skb = alloc_skb(DATA_PACKET_HEAD_ROOM + MESSAGE_MINIMUM_LENGTH,
  193. GFP_ATOMIC);
  194. if (unlikely(!skb))
  195. return;
  196. skb_reserve(skb, DATA_PACKET_HEAD_ROOM);
  197. skb->dev = peer->device->dev;
  198. PACKET_CB(skb)->mtu = skb->dev->mtu;
  199. skb_queue_tail(&peer->staged_packet_queue, skb);
  200. net_dbg_ratelimited("%s: Sending keepalive packet to peer %llu (%pISpfsc)\n",
  201. peer->device->dev->name, peer->internal_id,
  202. &peer->endpoint.addr);
  203. }
  204. wg_packet_send_staged_packets(peer);
  205. }
  206. static void wg_packet_create_data_done(struct wg_peer *peer, struct sk_buff *first)
  207. {
  208. struct sk_buff *skb, *next;
  209. bool is_keepalive, data_sent = false;
  210. wg_timers_any_authenticated_packet_traversal(peer);
  211. wg_timers_any_authenticated_packet_sent(peer);
  212. skb_list_walk_safe(first, skb, next) {
  213. is_keepalive = skb->len == message_data_len(0);
  214. if (likely(!wg_socket_send_skb_to_peer(peer, skb,
  215. PACKET_CB(skb)->ds) && !is_keepalive))
  216. data_sent = true;
  217. }
  218. if (likely(data_sent))
  219. wg_timers_data_sent(peer);
  220. keep_key_fresh(peer);
  221. }
  222. void wg_packet_tx_worker(struct work_struct *work)
  223. {
  224. struct wg_peer *peer = container_of(work, struct wg_peer, transmit_packet_work);
  225. struct noise_keypair *keypair;
  226. enum packet_state state;
  227. struct sk_buff *first;
  228. while ((first = wg_prev_queue_peek(&peer->tx_queue)) != NULL &&
  229. (state = atomic_read_acquire(&PACKET_CB(first)->state)) !=
  230. PACKET_STATE_UNCRYPTED) {
  231. wg_prev_queue_drop_peeked(&peer->tx_queue);
  232. keypair = PACKET_CB(first)->keypair;
  233. if (likely(state == PACKET_STATE_CRYPTED))
  234. wg_packet_create_data_done(peer, first);
  235. else
  236. kfree_skb_list(first);
  237. wg_noise_keypair_put(keypair, false);
  238. wg_peer_put(peer);
  239. if (need_resched())
  240. cond_resched();
  241. }
  242. }
  243. void wg_packet_encrypt_worker(struct work_struct *work)
  244. {
  245. struct crypt_queue *queue = container_of(work, struct multicore_worker,
  246. work)->ptr;
  247. struct sk_buff *first, *skb, *next;
  248. while ((first = ptr_ring_consume_bh(&queue->ring)) != NULL) {
  249. enum packet_state state = PACKET_STATE_CRYPTED;
  250. skb_list_walk_safe(first, skb, next) {
  251. if (likely(encrypt_packet(skb,
  252. PACKET_CB(first)->keypair))) {
  253. wg_reset_packet(skb, true);
  254. } else {
  255. state = PACKET_STATE_DEAD;
  256. break;
  257. }
  258. }
  259. wg_queue_enqueue_per_peer_tx(first, state);
  260. if (need_resched())
  261. cond_resched();
  262. }
  263. }
  264. static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)
  265. {
  266. struct wg_device *wg = peer->device;
  267. int ret = -EINVAL;
  268. rcu_read_lock_bh();
  269. if (unlikely(READ_ONCE(peer->is_dead)))
  270. goto err;
  271. ret = wg_queue_enqueue_per_device_and_peer(&wg->encrypt_queue, &peer->tx_queue, first,
  272. wg->packet_crypt_wq);
  273. if (unlikely(ret == -EPIPE))
  274. wg_queue_enqueue_per_peer_tx(first, PACKET_STATE_DEAD);
  275. err:
  276. rcu_read_unlock_bh();
  277. if (likely(!ret || ret == -EPIPE))
  278. return;
  279. wg_noise_keypair_put(PACKET_CB(first)->keypair, false);
  280. wg_peer_put(peer);
  281. kfree_skb_list(first);
  282. }
  283. void wg_packet_purge_staged_packets(struct wg_peer *peer)
  284. {
  285. spin_lock_bh(&peer->staged_packet_queue.lock);
  286. DEV_STATS_ADD(peer->device->dev, tx_dropped,
  287. peer->staged_packet_queue.qlen);
  288. __skb_queue_purge(&peer->staged_packet_queue);
  289. spin_unlock_bh(&peer->staged_packet_queue.lock);
  290. }
  291. void wg_packet_send_staged_packets(struct wg_peer *peer)
  292. {
  293. struct noise_keypair *keypair;
  294. struct sk_buff_head packets;
  295. struct sk_buff *skb;
  296. /* Steal the current queue into our local one. */
  297. __skb_queue_head_init(&packets);
  298. spin_lock_bh(&peer->staged_packet_queue.lock);
  299. skb_queue_splice_init(&peer->staged_packet_queue, &packets);
  300. spin_unlock_bh(&peer->staged_packet_queue.lock);
  301. if (unlikely(skb_queue_empty(&packets)))
  302. return;
  303. /* First we make sure we have a valid reference to a valid key. */
  304. rcu_read_lock_bh();
  305. keypair = wg_noise_keypair_get(
  306. rcu_dereference_bh(peer->keypairs.current_keypair));
  307. rcu_read_unlock_bh();
  308. if (unlikely(!keypair))
  309. goto out_nokey;
  310. if (unlikely(!READ_ONCE(keypair->sending.is_valid)))
  311. goto out_nokey;
  312. if (unlikely(wg_birthdate_has_expired(keypair->sending.birthdate,
  313. REJECT_AFTER_TIME)))
  314. goto out_invalid;
  315. /* After we know we have a somewhat valid key, we now try to assign
  316. * nonces to all of the packets in the queue. If we can't assign nonces
  317. * for all of them, we just consider it a failure and wait for the next
  318. * handshake.
  319. */
  320. skb_queue_walk(&packets, skb) {
  321. /* 0 for no outer TOS: no leak. TODO: at some later point, we
  322. * might consider using flowi->tos as outer instead.
  323. */
  324. PACKET_CB(skb)->ds = ip_tunnel_ecn_encap(0, ip_hdr(skb), skb);
  325. PACKET_CB(skb)->nonce =
  326. atomic64_inc_return(&keypair->sending_counter) - 1;
  327. if (unlikely(PACKET_CB(skb)->nonce >= REJECT_AFTER_MESSAGES))
  328. goto out_invalid;
  329. }
  330. packets.prev->next = NULL;
  331. wg_peer_get(keypair->entry.peer);
  332. PACKET_CB(packets.next)->keypair = keypair;
  333. wg_packet_create_data(peer, packets.next);
  334. return;
  335. out_invalid:
  336. WRITE_ONCE(keypair->sending.is_valid, false);
  337. out_nokey:
  338. wg_noise_keypair_put(keypair, false);
  339. /* We orphan the packets if we're waiting on a handshake, so that they
  340. * don't block a socket's pool.
  341. */
  342. skb_queue_walk(&packets, skb)
  343. skb_orphan(skb);
  344. /* Then we put them back on the top of the queue. We're not too
  345. * concerned about accidentally getting things a little out of order if
  346. * packets are being added really fast, because this queue is for before
  347. * packets can even be sent and it's small anyway.
  348. */
  349. spin_lock_bh(&peer->staged_packet_queue.lock);
  350. skb_queue_splice(&packets, &peer->staged_packet_queue);
  351. spin_unlock_bh(&peer->staged_packet_queue.lock);
  352. /* If we're exiting because there's something wrong with the key, it
  353. * means we should initiate a new handshake.
  354. */
  355. wg_packet_send_queued_handshake_initiation(peer, false);
  356. }