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