receive.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 "device.h"
  7. #include "peer.h"
  8. #include "timers.h"
  9. #include "messages.h"
  10. #include "cookie.h"
  11. #include "socket.h"
  12. #include <linux/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/udp.h>
  15. #include <net/ip_tunnels.h>
  16. /* Must be called with bh disabled. */
  17. static void update_rx_stats(struct wg_peer *peer, size_t len)
  18. {
  19. dev_sw_netstats_rx_add(peer->device->dev, len);
  20. peer->rx_bytes += len;
  21. }
  22. #define SKB_TYPE_LE32(skb) (((struct message_header *)(skb)->data)->type)
  23. static size_t validate_header_len(struct sk_buff *skb)
  24. {
  25. if (unlikely(skb->len < sizeof(struct message_header)))
  26. return 0;
  27. if (SKB_TYPE_LE32(skb) == cpu_to_le32(MESSAGE_DATA) &&
  28. skb->len >= MESSAGE_MINIMUM_LENGTH)
  29. return sizeof(struct message_data);
  30. if (SKB_TYPE_LE32(skb) == cpu_to_le32(MESSAGE_HANDSHAKE_INITIATION) &&
  31. skb->len == sizeof(struct message_handshake_initiation))
  32. return sizeof(struct message_handshake_initiation);
  33. if (SKB_TYPE_LE32(skb) == cpu_to_le32(MESSAGE_HANDSHAKE_RESPONSE) &&
  34. skb->len == sizeof(struct message_handshake_response))
  35. return sizeof(struct message_handshake_response);
  36. if (SKB_TYPE_LE32(skb) == cpu_to_le32(MESSAGE_HANDSHAKE_COOKIE) &&
  37. skb->len == sizeof(struct message_handshake_cookie))
  38. return sizeof(struct message_handshake_cookie);
  39. return 0;
  40. }
  41. static int prepare_skb_header(struct sk_buff *skb, struct wg_device *wg)
  42. {
  43. size_t data_offset, data_len, header_len;
  44. struct udphdr *udp;
  45. if (unlikely(!wg_check_packet_protocol(skb) ||
  46. skb_transport_header(skb) < skb->head ||
  47. (skb_transport_header(skb) + sizeof(struct udphdr)) >
  48. skb_tail_pointer(skb)))
  49. return -EINVAL; /* Bogus IP header */
  50. udp = udp_hdr(skb);
  51. data_offset = (u8 *)udp - skb->data;
  52. if (unlikely(data_offset > U16_MAX ||
  53. data_offset + sizeof(struct udphdr) > skb->len))
  54. /* Packet has offset at impossible location or isn't big enough
  55. * to have UDP fields.
  56. */
  57. return -EINVAL;
  58. data_len = ntohs(udp->len);
  59. if (unlikely(data_len < sizeof(struct udphdr) ||
  60. data_len > skb->len - data_offset))
  61. /* UDP packet is reporting too small of a size or lying about
  62. * its size.
  63. */
  64. return -EINVAL;
  65. data_len -= sizeof(struct udphdr);
  66. data_offset = (u8 *)udp + sizeof(struct udphdr) - skb->data;
  67. if (unlikely(!pskb_may_pull(skb,
  68. data_offset + sizeof(struct message_header)) ||
  69. pskb_trim(skb, data_len + data_offset) < 0))
  70. return -EINVAL;
  71. skb_pull(skb, data_offset);
  72. if (unlikely(skb->len != data_len))
  73. /* Final len does not agree with calculated len */
  74. return -EINVAL;
  75. header_len = validate_header_len(skb);
  76. if (unlikely(!header_len))
  77. return -EINVAL;
  78. __skb_push(skb, data_offset);
  79. if (unlikely(!pskb_may_pull(skb, data_offset + header_len)))
  80. return -EINVAL;
  81. __skb_pull(skb, data_offset);
  82. return 0;
  83. }
  84. static void wg_receive_handshake_packet(struct wg_device *wg,
  85. struct sk_buff *skb)
  86. {
  87. enum cookie_mac_state mac_state;
  88. struct wg_peer *peer = NULL;
  89. /* This is global, so that our load calculation applies to the whole
  90. * system. We don't care about races with it at all.
  91. */
  92. static u64 last_under_load;
  93. bool packet_needs_cookie;
  94. bool under_load;
  95. if (SKB_TYPE_LE32(skb) == cpu_to_le32(MESSAGE_HANDSHAKE_COOKIE)) {
  96. net_dbg_skb_ratelimited("%s: Receiving cookie response from %pISpfsc\n",
  97. wg->dev->name, skb);
  98. wg_cookie_message_consume(
  99. (struct message_handshake_cookie *)skb->data, wg);
  100. return;
  101. }
  102. under_load = atomic_read(&wg->handshake_queue_len) >=
  103. MAX_QUEUED_INCOMING_HANDSHAKES / 8;
  104. if (under_load) {
  105. last_under_load = ktime_get_coarse_boottime_ns();
  106. } else if (last_under_load) {
  107. under_load = !wg_birthdate_has_expired(last_under_load, 1);
  108. if (!under_load)
  109. last_under_load = 0;
  110. }
  111. mac_state = wg_cookie_validate_packet(&wg->cookie_checker, skb,
  112. under_load);
  113. if ((under_load && mac_state == VALID_MAC_WITH_COOKIE) ||
  114. (!under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)) {
  115. packet_needs_cookie = false;
  116. } else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE) {
  117. packet_needs_cookie = true;
  118. } else {
  119. net_dbg_skb_ratelimited("%s: Invalid MAC of handshake, dropping packet from %pISpfsc\n",
  120. wg->dev->name, skb);
  121. return;
  122. }
  123. switch (SKB_TYPE_LE32(skb)) {
  124. case cpu_to_le32(MESSAGE_HANDSHAKE_INITIATION): {
  125. struct message_handshake_initiation *message =
  126. (struct message_handshake_initiation *)skb->data;
  127. if (packet_needs_cookie) {
  128. wg_packet_send_handshake_cookie(wg, skb,
  129. message->sender_index);
  130. return;
  131. }
  132. peer = wg_noise_handshake_consume_initiation(message, wg);
  133. if (unlikely(!peer)) {
  134. net_dbg_skb_ratelimited("%s: Invalid handshake initiation from %pISpfsc\n",
  135. wg->dev->name, skb);
  136. return;
  137. }
  138. wg_socket_set_peer_endpoint_from_skb(peer, skb);
  139. net_dbg_ratelimited("%s: Receiving handshake initiation from peer %llu (%pISpfsc)\n",
  140. wg->dev->name, peer->internal_id,
  141. &peer->endpoint.addr);
  142. wg_packet_send_handshake_response(peer);
  143. break;
  144. }
  145. case cpu_to_le32(MESSAGE_HANDSHAKE_RESPONSE): {
  146. struct message_handshake_response *message =
  147. (struct message_handshake_response *)skb->data;
  148. if (packet_needs_cookie) {
  149. wg_packet_send_handshake_cookie(wg, skb,
  150. message->sender_index);
  151. return;
  152. }
  153. peer = wg_noise_handshake_consume_response(message, wg);
  154. if (unlikely(!peer)) {
  155. net_dbg_skb_ratelimited("%s: Invalid handshake response from %pISpfsc\n",
  156. wg->dev->name, skb);
  157. return;
  158. }
  159. wg_socket_set_peer_endpoint_from_skb(peer, skb);
  160. net_dbg_ratelimited("%s: Receiving handshake response from peer %llu (%pISpfsc)\n",
  161. wg->dev->name, peer->internal_id,
  162. &peer->endpoint.addr);
  163. if (wg_noise_handshake_begin_session(&peer->handshake,
  164. &peer->keypairs)) {
  165. wg_timers_session_derived(peer);
  166. wg_timers_handshake_complete(peer);
  167. /* Calling this function will either send any existing
  168. * packets in the queue and not send a keepalive, which
  169. * is the best case, Or, if there's nothing in the
  170. * queue, it will send a keepalive, in order to give
  171. * immediate confirmation of the session.
  172. */
  173. wg_packet_send_keepalive(peer);
  174. }
  175. break;
  176. }
  177. }
  178. if (unlikely(!peer)) {
  179. WARN(1, "Somehow a wrong type of packet wound up in the handshake queue!\n");
  180. return;
  181. }
  182. local_bh_disable();
  183. update_rx_stats(peer, skb->len);
  184. local_bh_enable();
  185. wg_timers_any_authenticated_packet_received(peer);
  186. wg_timers_any_authenticated_packet_traversal(peer);
  187. wg_peer_put(peer);
  188. }
  189. void wg_packet_handshake_receive_worker(struct work_struct *work)
  190. {
  191. struct crypt_queue *queue = container_of(work, struct multicore_worker, work)->ptr;
  192. struct wg_device *wg = container_of(queue, struct wg_device, handshake_queue);
  193. struct sk_buff *skb;
  194. while ((skb = ptr_ring_consume_bh(&queue->ring)) != NULL) {
  195. wg_receive_handshake_packet(wg, skb);
  196. dev_kfree_skb(skb);
  197. atomic_dec(&wg->handshake_queue_len);
  198. cond_resched();
  199. }
  200. }
  201. static void keep_key_fresh(struct wg_peer *peer)
  202. {
  203. struct noise_keypair *keypair;
  204. bool send;
  205. if (peer->sent_lastminute_handshake)
  206. return;
  207. rcu_read_lock_bh();
  208. keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
  209. send = keypair && READ_ONCE(keypair->sending.is_valid) &&
  210. keypair->i_am_the_initiator &&
  211. wg_birthdate_has_expired(keypair->sending.birthdate,
  212. REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT);
  213. rcu_read_unlock_bh();
  214. if (unlikely(send)) {
  215. peer->sent_lastminute_handshake = true;
  216. wg_packet_send_queued_handshake_initiation(peer, false);
  217. }
  218. }
  219. static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
  220. {
  221. struct scatterlist sg[MAX_SKB_FRAGS + 8];
  222. struct sk_buff *trailer;
  223. unsigned int offset;
  224. int num_frags;
  225. if (unlikely(!keypair))
  226. return false;
  227. if (unlikely(!READ_ONCE(keypair->receiving.is_valid) ||
  228. wg_birthdate_has_expired(keypair->receiving.birthdate, REJECT_AFTER_TIME) ||
  229. keypair->receiving_counter.counter >= REJECT_AFTER_MESSAGES)) {
  230. WRITE_ONCE(keypair->receiving.is_valid, false);
  231. return false;
  232. }
  233. PACKET_CB(skb)->nonce =
  234. le64_to_cpu(((struct message_data *)skb->data)->counter);
  235. /* We ensure that the network header is part of the packet before we
  236. * call skb_cow_data, so that there's no chance that data is removed
  237. * from the skb, so that later we can extract the original endpoint.
  238. */
  239. offset = skb->data - skb_network_header(skb);
  240. skb_push(skb, offset);
  241. num_frags = skb_cow_data(skb, 0, &trailer);
  242. offset += sizeof(struct message_data);
  243. skb_pull(skb, offset);
  244. if (unlikely(num_frags < 0 || num_frags > ARRAY_SIZE(sg)))
  245. return false;
  246. sg_init_table(sg, num_frags);
  247. if (skb_to_sgvec(skb, sg, 0, skb->len) <= 0)
  248. return false;
  249. if (!chacha20poly1305_decrypt_sg_inplace(sg, skb->len, NULL, 0,
  250. PACKET_CB(skb)->nonce,
  251. keypair->receiving.key))
  252. return false;
  253. /* Another ugly situation of pushing and pulling the header so as to
  254. * keep endpoint information intact.
  255. */
  256. skb_push(skb, offset);
  257. if (pskb_trim(skb, skb->len - noise_encrypted_len(0)))
  258. return false;
  259. skb_pull(skb, offset);
  260. return true;
  261. }
  262. /* This is RFC6479, a replay detection bitmap algorithm that avoids bitshifts */
  263. static bool counter_validate(struct noise_replay_counter *counter, u64 their_counter)
  264. {
  265. unsigned long index, index_current, top, i;
  266. bool ret = false;
  267. spin_lock_bh(&counter->lock);
  268. if (unlikely(counter->counter >= REJECT_AFTER_MESSAGES + 1 ||
  269. their_counter >= REJECT_AFTER_MESSAGES))
  270. goto out;
  271. ++their_counter;
  272. if (unlikely((COUNTER_WINDOW_SIZE + their_counter) <
  273. counter->counter))
  274. goto out;
  275. index = their_counter >> ilog2(BITS_PER_LONG);
  276. if (likely(their_counter > counter->counter)) {
  277. index_current = counter->counter >> ilog2(BITS_PER_LONG);
  278. top = min_t(unsigned long, index - index_current,
  279. COUNTER_BITS_TOTAL / BITS_PER_LONG);
  280. for (i = 1; i <= top; ++i)
  281. counter->backtrack[(i + index_current) &
  282. ((COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1)] = 0;
  283. counter->counter = their_counter;
  284. }
  285. index &= (COUNTER_BITS_TOTAL / BITS_PER_LONG) - 1;
  286. ret = !test_and_set_bit(their_counter & (BITS_PER_LONG - 1),
  287. &counter->backtrack[index]);
  288. out:
  289. spin_unlock_bh(&counter->lock);
  290. return ret;
  291. }
  292. #include "selftest/counter.c"
  293. static void wg_packet_consume_data_done(struct wg_peer *peer,
  294. struct sk_buff *skb,
  295. struct endpoint *endpoint)
  296. {
  297. struct net_device *dev = peer->device->dev;
  298. unsigned int len, len_before_trim;
  299. struct wg_peer *routed_peer;
  300. wg_socket_set_peer_endpoint(peer, endpoint);
  301. if (unlikely(wg_noise_received_with_keypair(&peer->keypairs,
  302. PACKET_CB(skb)->keypair))) {
  303. wg_timers_handshake_complete(peer);
  304. wg_packet_send_staged_packets(peer);
  305. }
  306. keep_key_fresh(peer);
  307. wg_timers_any_authenticated_packet_received(peer);
  308. wg_timers_any_authenticated_packet_traversal(peer);
  309. /* A packet with length 0 is a keepalive packet */
  310. if (unlikely(!skb->len)) {
  311. update_rx_stats(peer, message_data_len(0));
  312. net_dbg_ratelimited("%s: Receiving keepalive packet from peer %llu (%pISpfsc)\n",
  313. dev->name, peer->internal_id,
  314. &peer->endpoint.addr);
  315. goto packet_processed;
  316. }
  317. wg_timers_data_received(peer);
  318. if (unlikely(skb_network_header(skb) < skb->head))
  319. goto dishonest_packet_size;
  320. if (unlikely(!(pskb_network_may_pull(skb, sizeof(struct iphdr)) &&
  321. (ip_hdr(skb)->version == 4 ||
  322. (ip_hdr(skb)->version == 6 &&
  323. pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))))))
  324. goto dishonest_packet_type;
  325. skb->dev = dev;
  326. /* We've already verified the Poly1305 auth tag, which means this packet
  327. * was not modified in transit. We can therefore tell the networking
  328. * stack that all checksums of every layer of encapsulation have already
  329. * been checked "by the hardware" and therefore is unnecessary to check
  330. * again in software.
  331. */
  332. skb->ip_summed = CHECKSUM_UNNECESSARY;
  333. skb->csum_level = ~0; /* All levels */
  334. skb->protocol = ip_tunnel_parse_protocol(skb);
  335. if (skb->protocol == htons(ETH_P_IP)) {
  336. len = ntohs(ip_hdr(skb)->tot_len);
  337. if (unlikely(len < sizeof(struct iphdr)))
  338. goto dishonest_packet_size;
  339. INET_ECN_decapsulate(skb, PACKET_CB(skb)->ds, ip_hdr(skb)->tos);
  340. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  341. len = ntohs(ipv6_hdr(skb)->payload_len) +
  342. sizeof(struct ipv6hdr);
  343. INET_ECN_decapsulate(skb, PACKET_CB(skb)->ds, ipv6_get_dsfield(ipv6_hdr(skb)));
  344. } else {
  345. goto dishonest_packet_type;
  346. }
  347. if (unlikely(len > skb->len))
  348. goto dishonest_packet_size;
  349. len_before_trim = skb->len;
  350. if (unlikely(pskb_trim(skb, len)))
  351. goto packet_processed;
  352. routed_peer = wg_allowedips_lookup_src(&peer->device->peer_allowedips,
  353. skb);
  354. wg_peer_put(routed_peer); /* We don't need the extra reference. */
  355. if (unlikely(routed_peer != peer))
  356. goto dishonest_packet_peer;
  357. napi_gro_receive(&peer->napi, skb);
  358. update_rx_stats(peer, message_data_len(len_before_trim));
  359. return;
  360. dishonest_packet_peer:
  361. net_dbg_skb_ratelimited("%s: Packet has unallowed src IP (%pISc) from peer %llu (%pISpfsc)\n",
  362. dev->name, skb, peer->internal_id,
  363. &peer->endpoint.addr);
  364. DEV_STATS_INC(dev, rx_errors);
  365. DEV_STATS_INC(dev, rx_frame_errors);
  366. goto packet_processed;
  367. dishonest_packet_type:
  368. net_dbg_ratelimited("%s: Packet is neither ipv4 nor ipv6 from peer %llu (%pISpfsc)\n",
  369. dev->name, peer->internal_id, &peer->endpoint.addr);
  370. DEV_STATS_INC(dev, rx_errors);
  371. DEV_STATS_INC(dev, rx_frame_errors);
  372. goto packet_processed;
  373. dishonest_packet_size:
  374. net_dbg_ratelimited("%s: Packet has incorrect size from peer %llu (%pISpfsc)\n",
  375. dev->name, peer->internal_id, &peer->endpoint.addr);
  376. DEV_STATS_INC(dev, rx_errors);
  377. DEV_STATS_INC(dev, rx_length_errors);
  378. goto packet_processed;
  379. packet_processed:
  380. dev_kfree_skb(skb);
  381. }
  382. int wg_packet_rx_poll(struct napi_struct *napi, int budget)
  383. {
  384. struct wg_peer *peer = container_of(napi, struct wg_peer, napi);
  385. struct noise_keypair *keypair;
  386. struct endpoint endpoint;
  387. enum packet_state state;
  388. struct sk_buff *skb;
  389. int work_done = 0;
  390. bool free;
  391. if (unlikely(budget <= 0))
  392. return 0;
  393. while ((skb = wg_prev_queue_peek(&peer->rx_queue)) != NULL &&
  394. (state = atomic_read_acquire(&PACKET_CB(skb)->state)) !=
  395. PACKET_STATE_UNCRYPTED) {
  396. wg_prev_queue_drop_peeked(&peer->rx_queue);
  397. keypair = PACKET_CB(skb)->keypair;
  398. free = true;
  399. if (unlikely(state != PACKET_STATE_CRYPTED))
  400. goto next;
  401. if (unlikely(!counter_validate(&keypair->receiving_counter,
  402. PACKET_CB(skb)->nonce))) {
  403. net_dbg_ratelimited("%s: Packet has invalid nonce %llu (max %llu)\n",
  404. peer->device->dev->name,
  405. PACKET_CB(skb)->nonce,
  406. keypair->receiving_counter.counter);
  407. goto next;
  408. }
  409. if (unlikely(wg_socket_endpoint_from_skb(&endpoint, skb)))
  410. goto next;
  411. wg_reset_packet(skb, false);
  412. wg_packet_consume_data_done(peer, skb, &endpoint);
  413. free = false;
  414. next:
  415. wg_noise_keypair_put(keypair, false);
  416. wg_peer_put(peer);
  417. if (unlikely(free))
  418. dev_kfree_skb(skb);
  419. if (++work_done >= budget)
  420. break;
  421. }
  422. if (work_done < budget)
  423. napi_complete_done(napi, work_done);
  424. return work_done;
  425. }
  426. void wg_packet_decrypt_worker(struct work_struct *work)
  427. {
  428. struct crypt_queue *queue = container_of(work, struct multicore_worker,
  429. work)->ptr;
  430. struct sk_buff *skb;
  431. while ((skb = ptr_ring_consume_bh(&queue->ring)) != NULL) {
  432. enum packet_state state =
  433. likely(decrypt_packet(skb, PACKET_CB(skb)->keypair)) ?
  434. PACKET_STATE_CRYPTED : PACKET_STATE_DEAD;
  435. wg_queue_enqueue_per_peer_rx(skb, state);
  436. if (need_resched())
  437. cond_resched();
  438. }
  439. }
  440. static void wg_packet_consume_data(struct wg_device *wg, struct sk_buff *skb)
  441. {
  442. __le32 idx = ((struct message_data *)skb->data)->key_idx;
  443. struct wg_peer *peer = NULL;
  444. int ret;
  445. rcu_read_lock_bh();
  446. PACKET_CB(skb)->keypair =
  447. (struct noise_keypair *)wg_index_hashtable_lookup(
  448. wg->index_hashtable, INDEX_HASHTABLE_KEYPAIR, idx,
  449. &peer);
  450. if (unlikely(!wg_noise_keypair_get(PACKET_CB(skb)->keypair)))
  451. goto err_keypair;
  452. if (unlikely(READ_ONCE(peer->is_dead)))
  453. goto err;
  454. ret = wg_queue_enqueue_per_device_and_peer(&wg->decrypt_queue, &peer->rx_queue, skb,
  455. wg->packet_crypt_wq);
  456. if (unlikely(ret == -EPIPE))
  457. wg_queue_enqueue_per_peer_rx(skb, PACKET_STATE_DEAD);
  458. if (likely(!ret || ret == -EPIPE)) {
  459. rcu_read_unlock_bh();
  460. return;
  461. }
  462. err:
  463. wg_noise_keypair_put(PACKET_CB(skb)->keypair, false);
  464. err_keypair:
  465. rcu_read_unlock_bh();
  466. wg_peer_put(peer);
  467. dev_kfree_skb(skb);
  468. }
  469. void wg_packet_receive(struct wg_device *wg, struct sk_buff *skb)
  470. {
  471. if (unlikely(prepare_skb_header(skb, wg) < 0))
  472. goto err;
  473. switch (SKB_TYPE_LE32(skb)) {
  474. case cpu_to_le32(MESSAGE_HANDSHAKE_INITIATION):
  475. case cpu_to_le32(MESSAGE_HANDSHAKE_RESPONSE):
  476. case cpu_to_le32(MESSAGE_HANDSHAKE_COOKIE): {
  477. int cpu, ret = -EBUSY;
  478. if (unlikely(!rng_is_initialized()))
  479. goto drop;
  480. if (atomic_read(&wg->handshake_queue_len) > MAX_QUEUED_INCOMING_HANDSHAKES / 2) {
  481. if (spin_trylock_bh(&wg->handshake_queue.ring.producer_lock)) {
  482. ret = __ptr_ring_produce(&wg->handshake_queue.ring, skb);
  483. spin_unlock_bh(&wg->handshake_queue.ring.producer_lock);
  484. }
  485. } else
  486. ret = ptr_ring_produce_bh(&wg->handshake_queue.ring, skb);
  487. if (ret) {
  488. drop:
  489. net_dbg_skb_ratelimited("%s: Dropping handshake packet from %pISpfsc\n",
  490. wg->dev->name, skb);
  491. goto err;
  492. }
  493. atomic_inc(&wg->handshake_queue_len);
  494. cpu = wg_cpumask_next_online(&wg->handshake_queue.last_cpu);
  495. /* Queues up a call to packet_process_queued_handshake_packets(skb): */
  496. queue_work_on(cpu, wg->handshake_receive_wq,
  497. &per_cpu_ptr(wg->handshake_queue.worker, cpu)->work);
  498. break;
  499. }
  500. case cpu_to_le32(MESSAGE_DATA):
  501. PACKET_CB(skb)->ds = ip_tunnel_get_dsfield(ip_hdr(skb), skb);
  502. wg_packet_consume_data(wg, skb);
  503. break;
  504. default:
  505. WARN(1, "Non-exhaustive parsing of packet header lead to unknown packet type!\n");
  506. goto err;
  507. }
  508. return;
  509. err:
  510. dev_kfree_skb(skb);
  511. }