if_wg.c revision 1.89 1 /* $NetBSD: if_wg.c,v 1.89 2024/07/25 00:55:53 kre Exp $ */
2
3 /*
4 * Copyright (C) Ryota Ozaki <ozaki.ryota (at) gmail.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * This network interface aims to implement the WireGuard protocol.
34 * The implementation is based on the paper of WireGuard as of
35 * 2018-06-30 [1]. The paper is referred in the source code with label
36 * [W]. Also the specification of the Noise protocol framework as of
37 * 2018-07-11 [2] is referred with label [N].
38 *
39 * [1] https://www.wireguard.com/papers/wireguard.pdf
40 * [2] http://noiseprotocol.org/noise.pdf
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: if_wg.c,v 1.89 2024/07/25 00:55:53 kre Exp $");
45
46 #ifdef _KERNEL_OPT
47 #include "opt_altq_enabled.h"
48 #include "opt_inet.h"
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/types.h>
53
54 #include <sys/atomic.h>
55 #include <sys/callout.h>
56 #include <sys/cprng.h>
57 #include <sys/cpu.h>
58 #include <sys/device.h>
59 #include <sys/domain.h>
60 #include <sys/errno.h>
61 #include <sys/intr.h>
62 #include <sys/ioctl.h>
63 #include <sys/kernel.h>
64 #include <sys/kmem.h>
65 #include <sys/mbuf.h>
66 #include <sys/module.h>
67 #include <sys/mutex.h>
68 #include <sys/once.h>
69 #include <sys/percpu.h>
70 #include <sys/pserialize.h>
71 #include <sys/psref.h>
72 #include <sys/queue.h>
73 #include <sys/rwlock.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/sysctl.h>
78 #include <sys/syslog.h>
79 #include <sys/systm.h>
80 #include <sys/thmap.h>
81 #include <sys/threadpool.h>
82 #include <sys/time.h>
83 #include <sys/timespec.h>
84 #include <sys/workqueue.h>
85
86 #include <lib/libkern/libkern.h>
87
88 #include <net/bpf.h>
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/if_wg.h>
92 #include <net/pktqueue.h>
93 #include <net/route.h>
94
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_var.h>
100 #include <netinet/udp.h>
101 #include <netinet/udp_var.h>
102
103 #ifdef INET6
104 #include <netinet/ip6.h>
105 #include <netinet6/in6_pcb.h>
106 #include <netinet6/in6_var.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet6/udp6_var.h>
109 #endif /* INET6 */
110
111 #include <prop/proplib.h>
112
113 #include <crypto/blake2/blake2s.h>
114 #include <crypto/sodium/crypto_aead_chacha20poly1305.h>
115 #include <crypto/sodium/crypto_aead_xchacha20poly1305.h>
116 #include <crypto/sodium/crypto_scalarmult.h>
117
118 #include "ioconf.h"
119
120 #ifdef WG_RUMPKERNEL
121 #include "wg_user.h"
122 #endif
123
124 /*
125 * Data structures
126 * - struct wg_softc is an instance of wg interfaces
127 * - It has a list of peers (struct wg_peer)
128 * - It has a threadpool job that sends/receives handshake messages and
129 * runs event handlers
130 * - It has its own two routing tables: one is for IPv4 and the other IPv6
131 * - struct wg_peer is a representative of a peer
132 * - It has a struct work to handle handshakes and timer tasks
133 * - It has a pair of session instances (struct wg_session)
134 * - It has a pair of endpoint instances (struct wg_sockaddr)
135 * - Normally one endpoint is used and the second one is used only on
136 * a peer migration (a change of peer's IP address)
137 * - It has a list of IP addresses and sub networks called allowedips
138 * (struct wg_allowedip)
139 * - A packets sent over a session is allowed if its destination matches
140 * any IP addresses or sub networks of the list
141 * - struct wg_session represents a session of a secure tunnel with a peer
142 * - Two instances of sessions belong to a peer; a stable session and a
143 * unstable session
144 * - A handshake process of a session always starts with a unstable instance
145 * - Once a session is established, its instance becomes stable and the
146 * other becomes unstable instead
147 * - Data messages are always sent via a stable session
148 *
149 * Locking notes:
150 * - Each wg has a mutex(9) wg_lock, and a rwlock(9) wg_rwlock
151 * - Changes to the peer list are serialized by wg_lock
152 * - The peer list may be read with pserialize(9) and psref(9)
153 * - The rwlock (wg_rwlock) protects the routing tables (wg_rtable_ipv[46])
154 * => XXX replace by pserialize when routing table is psz-safe
155 * - Each peer (struct wg_peer, wgp) has a mutex wgp_lock, which can be taken
156 * only in thread context and serializes:
157 * - the stable and unstable session pointers
158 * - all unstable session state
159 * - Packet processing may be done in softint context:
160 * - The stable session can be read under pserialize(9) or psref(9)
161 * - The stable session is always ESTABLISHED
162 * - On a session swap, we must wait for all readers to release a
163 * reference to a stable session before changing wgs_state and
164 * session states
165 * - Lock order: wg_lock -> wgp_lock
166 */
167
168
169 #define WGLOG(level, fmt, args...) \
170 log(level, "%s: " fmt, __func__, ##args)
171
172 #define WG_DEBUG
173
174 /* Debug options */
175 #ifdef WG_DEBUG
176 /* Output debug logs */
177 #ifndef WG_DEBUG_LOG
178 #define WG_DEBUG_LOG
179 #endif
180 /* Output trace logs */
181 #ifndef WG_DEBUG_TRACE
182 #define WG_DEBUG_TRACE
183 #endif
184 /* Output hash values, etc. */
185 #ifndef WG_DEBUG_DUMP
186 #define WG_DEBUG_DUMP
187 #endif
188 /* debug packets */
189 #ifndef WG_DEBUG_PACKET
190 #define WG_DEBUG_PACKET
191 #endif
192 /* Make some internal parameters configurable for testing and debugging */
193 #ifndef WG_DEBUG_PARAMS
194 #define WG_DEBUG_PARAMS
195 #endif
196 #endif /* WG_DEBUG */
197
198 #ifndef WG_DEBUG
199 # if defined(WG_DEBUG_LOG) || defined(WG_DEBUG_TRACE) || \
200 defined(WG_DEBUG_DUMP) || defined(WG_DEBUG_PARAMS) || \
201 defined(WG_DEBUG_PACKET)
202 # define WG_DEBUG
203 # endif
204 #endif
205
206 #ifdef WG_DEBUG
207 int wg_debug;
208 #define WG_DEBUG_FLAGS_LOG 1
209 #define WG_DEBUG_FLAGS_TRACE 2
210 #define WG_DEBUG_FLAGS_DUMP 4
211 #define WG_DEBUG_FLAGS_PACKET 8
212 #endif
213
214
215 #ifdef WG_DEBUG_TRACE
216 #define WG_TRACE(msg) do { \
217 if (wg_debug & WG_DEBUG_FLAGS_TRACE) \
218 log(LOG_DEBUG, "%s:%d: %s\n", __func__, __LINE__, (msg)); \
219 } while (0)
220 #else
221 #define WG_TRACE(msg) __nothing
222 #endif
223
224 #ifdef WG_DEBUG_LOG
225 #define WG_DLOG(fmt, args...) do { \
226 if (wg_debug & WG_DEBUG_FLAGS_LOG) \
227 log(LOG_DEBUG, "%s: " fmt, __func__, ##args); \
228 } while (0)
229 #else
230 #define WG_DLOG(fmt, args...) __nothing
231 #endif
232
233 #define WG_LOG_RATECHECK(wgprc, level, fmt, args...) do { \
234 if (ppsratecheck(&(wgprc)->wgprc_lasttime, \
235 &(wgprc)->wgprc_curpps, 1)) { \
236 log(level, fmt, ##args); \
237 } \
238 } while (0)
239
240 #ifdef WG_DEBUG_PARAMS
241 static bool wg_force_underload = false;
242 #endif
243
244 #ifdef WG_DEBUG_DUMP
245
246 static char enomem[10] = "[enomem]";
247
248 static char *
249 gethexdump(const void *vp, size_t n)
250 {
251 char *buf;
252 const uint8_t *p = vp;
253 size_t i;
254
255 if (n > (SIZE_MAX - 1) / 3)
256 return enomem;
257 buf = kmem_alloc(3 * n + 1, KM_NOSLEEP);
258 if (buf == NULL)
259 return enomem;
260 for (i = 0; i < n; i++)
261 snprintf(buf + 3 * i, 3 + 1, " %02hhx", p[i]);
262 return buf;
263 }
264
265 static void
266 puthexdump(char *buf, const void *p, size_t n)
267 {
268
269 if (buf == NULL || buf == enomem)
270 return;
271 kmem_free(buf, 3*n + 1);
272 }
273
274 #ifdef WG_RUMPKERNEL
275 static void
276 wg_dump_buf(const char *func, const char *buf, const size_t size)
277 {
278 if ((wg_debug & WG_DEBUG_FLAGS_DUMP) == 0)
279 return;
280
281 char *hex = gethexdump(buf, size);
282
283 log(LOG_DEBUG, "%s: %s\n", func, hex);
284 puthexdump(hex, buf, size);
285 }
286 #endif
287
288 static void
289 wg_dump_hash(const uint8_t *func, const uint8_t *name, const uint8_t *hash,
290 const size_t size)
291 {
292 if ((wg_debug & WG_DEBUG_FLAGS_DUMP) == 0)
293 return;
294
295 char *hex = gethexdump(hash, size);
296
297 log(LOG_DEBUG, "%s: %s: %s\n", func, name, hex);
298 puthexdump(hex, hash, size);
299 }
300
301 #define WG_DUMP_HASH(name, hash) \
302 wg_dump_hash(__func__, name, hash, WG_HASH_LEN)
303 #define WG_DUMP_HASH48(name, hash) \
304 wg_dump_hash(__func__, name, hash, 48)
305 #define WG_DUMP_BUF(buf, size) \
306 wg_dump_buf(__func__, buf, size)
307 #else
308 #define WG_DUMP_HASH(name, hash) __nothing
309 #define WG_DUMP_HASH48(name, hash) __nothing
310 #define WG_DUMP_BUF(buf, size) __nothing
311 #endif /* WG_DEBUG_DUMP */
312
313 /* chosen somewhat arbitrarily -- fits in signed 16 bits NUL-terminated */
314 #define WG_MAX_PROPLEN 32766
315
316 #define WG_MTU 1420
317 #define WG_ALLOWEDIPS 16
318
319 #define CURVE25519_KEY_LEN 32
320 #define TAI64N_LEN sizeof(uint32_t) * 3
321 #define POLY1305_AUTHTAG_LEN 16
322 #define HMAC_BLOCK_LEN 64
323
324 /* [N] 4.1: "DHLEN must be 32 or greater." WireGuard chooses 32. */
325 /* [N] 4.3: Hash functions */
326 #define NOISE_DHLEN 32
327 /* [N] 4.3: "Must be 32 or 64." WireGuard chooses 32. */
328 #define NOISE_HASHLEN 32
329 #define NOISE_BLOCKLEN 64
330 #define NOISE_HKDF_OUTPUT_LEN NOISE_HASHLEN
331 /* [N] 5.1: "k" */
332 #define NOISE_CIPHER_KEY_LEN 32
333 /*
334 * [N] 9.2: "psk"
335 * "... psk is a 32-byte secret value provided by the application."
336 */
337 #define NOISE_PRESHARED_KEY_LEN 32
338
339 #define WG_STATIC_KEY_LEN CURVE25519_KEY_LEN
340 #define WG_TIMESTAMP_LEN TAI64N_LEN
341
342 #define WG_PRESHARED_KEY_LEN NOISE_PRESHARED_KEY_LEN
343
344 #define WG_COOKIE_LEN 16
345 #define WG_MAC_LEN 16
346 #define WG_RANDVAL_LEN 24
347
348 #define WG_EPHEMERAL_KEY_LEN CURVE25519_KEY_LEN
349 /* [N] 5.2: "ck: A chaining key of HASHLEN bytes" */
350 #define WG_CHAINING_KEY_LEN NOISE_HASHLEN
351 /* [N] 5.2: "h: A hash output of HASHLEN bytes" */
352 #define WG_HASH_LEN NOISE_HASHLEN
353 #define WG_CIPHER_KEY_LEN NOISE_CIPHER_KEY_LEN
354 #define WG_DH_OUTPUT_LEN NOISE_DHLEN
355 #define WG_KDF_OUTPUT_LEN NOISE_HKDF_OUTPUT_LEN
356 #define WG_AUTHTAG_LEN POLY1305_AUTHTAG_LEN
357 #define WG_DATA_KEY_LEN 32
358 #define WG_SALT_LEN 24
359
360 /*
361 * The protocol messages
362 */
363 struct wg_msg {
364 uint32_t wgm_type;
365 } __packed;
366
367 /* [W] 5.4.2 First Message: Initiator to Responder */
368 struct wg_msg_init {
369 uint32_t wgmi_type;
370 uint32_t wgmi_sender;
371 uint8_t wgmi_ephemeral[WG_EPHEMERAL_KEY_LEN];
372 uint8_t wgmi_static[WG_STATIC_KEY_LEN + WG_AUTHTAG_LEN];
373 uint8_t wgmi_timestamp[WG_TIMESTAMP_LEN + WG_AUTHTAG_LEN];
374 uint8_t wgmi_mac1[WG_MAC_LEN];
375 uint8_t wgmi_mac2[WG_MAC_LEN];
376 } __packed;
377
378 /* [W] 5.4.3 Second Message: Responder to Initiator */
379 struct wg_msg_resp {
380 uint32_t wgmr_type;
381 uint32_t wgmr_sender;
382 uint32_t wgmr_receiver;
383 uint8_t wgmr_ephemeral[WG_EPHEMERAL_KEY_LEN];
384 uint8_t wgmr_empty[0 + WG_AUTHTAG_LEN];
385 uint8_t wgmr_mac1[WG_MAC_LEN];
386 uint8_t wgmr_mac2[WG_MAC_LEN];
387 } __packed;
388
389 /* [W] 5.4.6 Subsequent Messages: Transport Data Messages */
390 struct wg_msg_data {
391 uint32_t wgmd_type;
392 uint32_t wgmd_receiver;
393 uint64_t wgmd_counter;
394 uint32_t wgmd_packet[0];
395 } __packed;
396
397 /* [W] 5.4.7 Under Load: Cookie Reply Message */
398 struct wg_msg_cookie {
399 uint32_t wgmc_type;
400 uint32_t wgmc_receiver;
401 uint8_t wgmc_salt[WG_SALT_LEN];
402 uint8_t wgmc_cookie[WG_COOKIE_LEN + WG_AUTHTAG_LEN];
403 } __packed;
404
405 #define WG_MSG_TYPE_INIT 1
406 #define WG_MSG_TYPE_RESP 2
407 #define WG_MSG_TYPE_COOKIE 3
408 #define WG_MSG_TYPE_DATA 4
409 #define WG_MSG_TYPE_MAX WG_MSG_TYPE_DATA
410
411 /* Sliding windows */
412
413 #define SLIWIN_BITS 2048u
414 #define SLIWIN_TYPE uint32_t
415 #define SLIWIN_BPW NBBY*sizeof(SLIWIN_TYPE)
416 #define SLIWIN_WORDS howmany(SLIWIN_BITS, SLIWIN_BPW)
417 #define SLIWIN_NPKT (SLIWIN_BITS - NBBY*sizeof(SLIWIN_TYPE))
418
419 struct sliwin {
420 SLIWIN_TYPE B[SLIWIN_WORDS];
421 uint64_t T;
422 };
423
424 static void
425 sliwin_reset(struct sliwin *W)
426 {
427
428 memset(W, 0, sizeof(*W));
429 }
430
431 static int
432 sliwin_check_fast(const volatile struct sliwin *W, uint64_t S)
433 {
434
435 /*
436 * If it's more than one window older than the highest sequence
437 * number we've seen, reject.
438 */
439 #ifdef __HAVE_ATOMIC64_LOADSTORE
440 if (S + SLIWIN_NPKT < atomic_load_relaxed(&W->T))
441 return EAUTH;
442 #endif
443
444 /*
445 * Otherwise, we need to take the lock to decide, so don't
446 * reject just yet. Caller must serialize a call to
447 * sliwin_update in this case.
448 */
449 return 0;
450 }
451
452 static int
453 sliwin_update(struct sliwin *W, uint64_t S)
454 {
455 unsigned word, bit;
456
457 /*
458 * If it's more than one window older than the highest sequence
459 * number we've seen, reject.
460 */
461 if (S + SLIWIN_NPKT < W->T)
462 return EAUTH;
463
464 /*
465 * If it's higher than the highest sequence number we've seen,
466 * advance the window.
467 */
468 if (S > W->T) {
469 uint64_t i = W->T / SLIWIN_BPW;
470 uint64_t j = S / SLIWIN_BPW;
471 unsigned k;
472
473 for (k = 0; k < MIN(j - i, SLIWIN_WORDS); k++)
474 W->B[(i + k + 1) % SLIWIN_WORDS] = 0;
475 #ifdef __HAVE_ATOMIC64_LOADSTORE
476 atomic_store_relaxed(&W->T, S);
477 #else
478 W->T = S;
479 #endif
480 }
481
482 /* Test and set the bit -- if already set, reject. */
483 word = (S / SLIWIN_BPW) % SLIWIN_WORDS;
484 bit = S % SLIWIN_BPW;
485 if (W->B[word] & (1UL << bit))
486 return EAUTH;
487 W->B[word] |= 1U << bit;
488
489 /* Accept! */
490 return 0;
491 }
492
493 struct wg_session {
494 struct wg_peer *wgs_peer;
495 struct psref_target
496 wgs_psref;
497
498 int wgs_state;
499 #define WGS_STATE_UNKNOWN 0
500 #define WGS_STATE_INIT_ACTIVE 1
501 #define WGS_STATE_INIT_PASSIVE 2
502 #define WGS_STATE_ESTABLISHED 3
503 #define WGS_STATE_DESTROYING 4
504
505 time_t wgs_time_established;
506 time_t wgs_time_last_data_sent;
507 bool wgs_is_initiator;
508
509 uint32_t wgs_local_index;
510 uint32_t wgs_remote_index;
511 #ifdef __HAVE_ATOMIC64_LOADSTORE
512 volatile uint64_t
513 wgs_send_counter;
514 #else
515 kmutex_t wgs_send_counter_lock;
516 uint64_t wgs_send_counter;
517 #endif
518
519 struct {
520 kmutex_t lock;
521 struct sliwin window;
522 } *wgs_recvwin;
523
524 uint8_t wgs_handshake_hash[WG_HASH_LEN];
525 uint8_t wgs_chaining_key[WG_CHAINING_KEY_LEN];
526 uint8_t wgs_ephemeral_key_pub[WG_EPHEMERAL_KEY_LEN];
527 uint8_t wgs_ephemeral_key_priv[WG_EPHEMERAL_KEY_LEN];
528 uint8_t wgs_ephemeral_key_peer[WG_EPHEMERAL_KEY_LEN];
529 uint8_t wgs_tkey_send[WG_DATA_KEY_LEN];
530 uint8_t wgs_tkey_recv[WG_DATA_KEY_LEN];
531 };
532
533 struct wg_sockaddr {
534 union {
535 struct sockaddr_storage _ss;
536 struct sockaddr _sa;
537 struct sockaddr_in _sin;
538 struct sockaddr_in6 _sin6;
539 };
540 struct psref_target wgsa_psref;
541 };
542
543 #define wgsatoss(wgsa) (&(wgsa)->_ss)
544 #define wgsatosa(wgsa) (&(wgsa)->_sa)
545 #define wgsatosin(wgsa) (&(wgsa)->_sin)
546 #define wgsatosin6(wgsa) (&(wgsa)->_sin6)
547
548 #define wgsa_family(wgsa) (wgsatosa(wgsa)->sa_family)
549
550 struct wg_peer;
551 struct wg_allowedip {
552 struct radix_node wga_nodes[2];
553 struct wg_sockaddr _wga_sa_addr;
554 struct wg_sockaddr _wga_sa_mask;
555 #define wga_sa_addr _wga_sa_addr._sa
556 #define wga_sa_mask _wga_sa_mask._sa
557
558 int wga_family;
559 uint8_t wga_cidr;
560 union {
561 struct in_addr _ip4;
562 struct in6_addr _ip6;
563 } wga_addr;
564 #define wga_addr4 wga_addr._ip4
565 #define wga_addr6 wga_addr._ip6
566
567 struct wg_peer *wga_peer;
568 };
569
570 typedef uint8_t wg_timestamp_t[WG_TIMESTAMP_LEN];
571
572 struct wg_ppsratecheck {
573 struct timeval wgprc_lasttime;
574 int wgprc_curpps;
575 };
576
577 struct wg_softc;
578 struct wg_peer {
579 struct wg_softc *wgp_sc;
580 char wgp_name[WG_PEER_NAME_MAXLEN + 1];
581 struct pslist_entry wgp_peerlist_entry;
582 pserialize_t wgp_psz;
583 struct psref_target wgp_psref;
584 kmutex_t *wgp_lock;
585 kmutex_t *wgp_intr_lock;
586
587 uint8_t wgp_pubkey[WG_STATIC_KEY_LEN];
588 struct wg_sockaddr *wgp_endpoint;
589 struct wg_sockaddr *wgp_endpoint0;
590 volatile unsigned wgp_endpoint_changing;
591 bool wgp_endpoint_available;
592
593 /* The preshared key (optional) */
594 uint8_t wgp_psk[WG_PRESHARED_KEY_LEN];
595
596 struct wg_session *wgp_session_stable;
597 struct wg_session *wgp_session_unstable;
598
599 /* first outgoing packet awaiting session initiation */
600 struct mbuf *wgp_pending;
601
602 /* timestamp in big-endian */
603 wg_timestamp_t wgp_timestamp_latest_init;
604
605 struct timespec wgp_last_handshake_time;
606
607 callout_t wgp_rekey_timer;
608 callout_t wgp_handshake_timeout_timer;
609 callout_t wgp_session_dtor_timer;
610
611 time_t wgp_handshake_start_time;
612
613 int wgp_n_allowedips;
614 struct wg_allowedip wgp_allowedips[WG_ALLOWEDIPS];
615
616 time_t wgp_latest_cookie_time;
617 uint8_t wgp_latest_cookie[WG_COOKIE_LEN];
618 uint8_t wgp_last_sent_mac1[WG_MAC_LEN];
619 bool wgp_last_sent_mac1_valid;
620 uint8_t wgp_last_sent_cookie[WG_COOKIE_LEN];
621 bool wgp_last_sent_cookie_valid;
622
623 time_t wgp_last_msg_received_time[WG_MSG_TYPE_MAX];
624
625 time_t wgp_last_genrandval_time;
626 uint32_t wgp_randval;
627
628 struct wg_ppsratecheck wgp_ppsratecheck;
629
630 struct work wgp_work;
631 unsigned int wgp_tasks;
632 #define WGP_TASK_SEND_INIT_MESSAGE __BIT(0)
633 #define WGP_TASK_RETRY_HANDSHAKE __BIT(1)
634 #define WGP_TASK_ESTABLISH_SESSION __BIT(2)
635 #define WGP_TASK_ENDPOINT_CHANGED __BIT(3)
636 #define WGP_TASK_SEND_KEEPALIVE_MESSAGE __BIT(4)
637 #define WGP_TASK_DESTROY_PREV_SESSION __BIT(5)
638 };
639
640 struct wg_ops;
641
642 struct wg_softc {
643 struct ifnet wg_if;
644 LIST_ENTRY(wg_softc) wg_list;
645 kmutex_t *wg_lock;
646 kmutex_t *wg_intr_lock;
647 krwlock_t *wg_rwlock;
648
649 uint8_t wg_privkey[WG_STATIC_KEY_LEN];
650 uint8_t wg_pubkey[WG_STATIC_KEY_LEN];
651
652 int wg_npeers;
653 struct pslist_head wg_peers;
654 struct thmap *wg_peers_bypubkey;
655 struct thmap *wg_peers_byname;
656 struct thmap *wg_sessions_byindex;
657 uint16_t wg_listen_port;
658
659 struct threadpool *wg_threadpool;
660
661 struct threadpool_job wg_job;
662 int wg_upcalls;
663 #define WG_UPCALL_INET __BIT(0)
664 #define WG_UPCALL_INET6 __BIT(1)
665
666 #ifdef INET
667 struct socket *wg_so4;
668 struct radix_node_head *wg_rtable_ipv4;
669 #endif
670 #ifdef INET6
671 struct socket *wg_so6;
672 struct radix_node_head *wg_rtable_ipv6;
673 #endif
674
675 struct wg_ppsratecheck wg_ppsratecheck;
676
677 struct wg_ops *wg_ops;
678
679 #ifdef WG_RUMPKERNEL
680 struct wg_user *wg_user;
681 #endif
682 };
683
684 /* [W] 6.1 Preliminaries */
685 #define WG_REKEY_AFTER_MESSAGES (1ULL << 60)
686 #define WG_REJECT_AFTER_MESSAGES (UINT64_MAX - (1 << 13))
687 #define WG_REKEY_AFTER_TIME 120
688 #define WG_REJECT_AFTER_TIME 180
689 #define WG_REKEY_ATTEMPT_TIME 90
690 #define WG_REKEY_TIMEOUT 5
691 #define WG_KEEPALIVE_TIMEOUT 10
692
693 #define WG_COOKIE_TIME 120
694 #define WG_RANDVAL_TIME (2 * 60)
695
696 static uint64_t wg_rekey_after_messages = WG_REKEY_AFTER_MESSAGES;
697 static uint64_t wg_reject_after_messages = WG_REJECT_AFTER_MESSAGES;
698 static unsigned wg_rekey_after_time = WG_REKEY_AFTER_TIME;
699 static unsigned wg_reject_after_time = WG_REJECT_AFTER_TIME;
700 static unsigned wg_rekey_attempt_time = WG_REKEY_ATTEMPT_TIME;
701 static unsigned wg_rekey_timeout = WG_REKEY_TIMEOUT;
702 static unsigned wg_keepalive_timeout = WG_KEEPALIVE_TIMEOUT;
703
704 static struct mbuf *
705 wg_get_mbuf(size_t, size_t);
706
707 static int wg_send_data_msg(struct wg_peer *, struct wg_session *,
708 struct mbuf *);
709 static int wg_send_cookie_msg(struct wg_softc *, struct wg_peer *,
710 const uint32_t, const uint8_t [WG_MAC_LEN],
711 const struct sockaddr *);
712 static int wg_send_handshake_msg_resp(struct wg_softc *, struct wg_peer *,
713 struct wg_session *, const struct wg_msg_init *);
714 static void wg_send_keepalive_msg(struct wg_peer *, struct wg_session *);
715
716 static struct wg_peer *
717 wg_pick_peer_by_sa(struct wg_softc *, const struct sockaddr *,
718 struct psref *);
719 static struct wg_peer *
720 wg_lookup_peer_by_pubkey(struct wg_softc *,
721 const uint8_t [WG_STATIC_KEY_LEN], struct psref *);
722
723 static struct wg_session *
724 wg_lookup_session_by_index(struct wg_softc *,
725 const uint32_t, struct psref *);
726
727 static void wg_update_endpoint_if_necessary(struct wg_peer *,
728 const struct sockaddr *);
729
730 static void wg_schedule_rekey_timer(struct wg_peer *);
731 static void wg_schedule_session_dtor_timer(struct wg_peer *);
732
733 static bool wg_is_underload(struct wg_softc *, struct wg_peer *, int);
734 static void wg_calculate_keys(struct wg_session *, const bool);
735
736 static void wg_clear_states(struct wg_session *);
737
738 static void wg_get_peer(struct wg_peer *, struct psref *);
739 static void wg_put_peer(struct wg_peer *, struct psref *);
740
741 static int wg_send_so(struct wg_peer *, struct mbuf *);
742 static int wg_send_udp(struct wg_peer *, struct mbuf *);
743 static int wg_output(struct ifnet *, struct mbuf *,
744 const struct sockaddr *, const struct rtentry *);
745 static void wg_input(struct ifnet *, struct mbuf *, const int);
746 static int wg_ioctl(struct ifnet *, u_long, void *);
747 static int wg_bind_port(struct wg_softc *, const uint16_t);
748 static int wg_init(struct ifnet *);
749 #ifdef ALTQ
750 static void wg_start(struct ifnet *);
751 #endif
752 static void wg_stop(struct ifnet *, int);
753
754 static void wg_peer_work(struct work *, void *);
755 static void wg_job(struct threadpool_job *);
756 static void wgintr(void *);
757 static void wg_purge_pending_packets(struct wg_peer *);
758
759 static int wg_clone_create(struct if_clone *, int);
760 static int wg_clone_destroy(struct ifnet *);
761
762 struct wg_ops {
763 int (*send_hs_msg)(struct wg_peer *, struct mbuf *);
764 int (*send_data_msg)(struct wg_peer *, struct mbuf *);
765 void (*input)(struct ifnet *, struct mbuf *, const int);
766 int (*bind_port)(struct wg_softc *, const uint16_t);
767 };
768
769 struct wg_ops wg_ops_rumpkernel = {
770 .send_hs_msg = wg_send_so,
771 .send_data_msg = wg_send_udp,
772 .input = wg_input,
773 .bind_port = wg_bind_port,
774 };
775
776 #ifdef WG_RUMPKERNEL
777 static bool wg_user_mode(struct wg_softc *);
778 static int wg_ioctl_linkstr(struct wg_softc *, struct ifdrv *);
779
780 static int wg_send_user(struct wg_peer *, struct mbuf *);
781 static void wg_input_user(struct ifnet *, struct mbuf *, const int);
782 static int wg_bind_port_user(struct wg_softc *, const uint16_t);
783
784 struct wg_ops wg_ops_rumpuser = {
785 .send_hs_msg = wg_send_user,
786 .send_data_msg = wg_send_user,
787 .input = wg_input_user,
788 .bind_port = wg_bind_port_user,
789 };
790 #endif
791
792 #define WG_PEER_READER_FOREACH(wgp, wg) \
793 PSLIST_READER_FOREACH((wgp), &(wg)->wg_peers, struct wg_peer, \
794 wgp_peerlist_entry)
795 #define WG_PEER_WRITER_FOREACH(wgp, wg) \
796 PSLIST_WRITER_FOREACH((wgp), &(wg)->wg_peers, struct wg_peer, \
797 wgp_peerlist_entry)
798 #define WG_PEER_WRITER_INSERT_HEAD(wgp, wg) \
799 PSLIST_WRITER_INSERT_HEAD(&(wg)->wg_peers, (wgp), wgp_peerlist_entry)
800 #define WG_PEER_WRITER_REMOVE(wgp) \
801 PSLIST_WRITER_REMOVE((wgp), wgp_peerlist_entry)
802
803 struct wg_route {
804 struct radix_node wgr_nodes[2];
805 struct wg_peer *wgr_peer;
806 };
807
808 static struct radix_node_head *
809 wg_rnh(struct wg_softc *wg, const int family)
810 {
811
812 switch (family) {
813 case AF_INET:
814 return wg->wg_rtable_ipv4;
815 #ifdef INET6
816 case AF_INET6:
817 return wg->wg_rtable_ipv6;
818 #endif
819 default:
820 return NULL;
821 }
822 }
823
824
825 /*
826 * Global variables
827 */
828 static volatile unsigned wg_count __cacheline_aligned;
829
830 struct psref_class *wg_psref_class __read_mostly;
831
832 static struct if_clone wg_cloner =
833 IF_CLONE_INITIALIZER("wg", wg_clone_create, wg_clone_destroy);
834
835 static struct pktqueue *wg_pktq __read_mostly;
836 static struct workqueue *wg_wq __read_mostly;
837
838 void wgattach(int);
839 /* ARGSUSED */
840 void
841 wgattach(int count)
842 {
843 /*
844 * Nothing to do here, initialization is handled by the
845 * module initialization code in wginit() below).
846 */
847 }
848
849 static void
850 wginit(void)
851 {
852
853 wg_psref_class = psref_class_create("wg", IPL_SOFTNET);
854
855 if_clone_attach(&wg_cloner);
856 }
857
858 /*
859 * XXX Kludge: This should just happen in wginit, but workqueue_create
860 * cannot be run until after CPUs have been detected, and wginit runs
861 * before configure.
862 */
863 static int
864 wginitqueues(void)
865 {
866 int error __diagused;
867
868 wg_pktq = pktq_create(IFQ_MAXLEN, wgintr, NULL);
869 KASSERT(wg_pktq != NULL);
870
871 error = workqueue_create(&wg_wq, "wgpeer", wg_peer_work, NULL,
872 PRI_NONE, IPL_SOFTNET, WQ_MPSAFE|WQ_PERCPU);
873 KASSERT(error == 0);
874
875 return 0;
876 }
877
878 static void
879 wg_guarantee_initialized(void)
880 {
881 static ONCE_DECL(init);
882 int error __diagused;
883
884 error = RUN_ONCE(&init, wginitqueues);
885 KASSERT(error == 0);
886 }
887
888 static int
889 wg_count_inc(void)
890 {
891 unsigned o, n;
892
893 do {
894 o = atomic_load_relaxed(&wg_count);
895 if (o == UINT_MAX)
896 return ENFILE;
897 n = o + 1;
898 } while (atomic_cas_uint(&wg_count, o, n) != o);
899
900 return 0;
901 }
902
903 static void
904 wg_count_dec(void)
905 {
906 unsigned c __diagused;
907
908 c = atomic_dec_uint_nv(&wg_count);
909 KASSERT(c != UINT_MAX);
910 }
911
912 static int
913 wgdetach(void)
914 {
915
916 /* Prevent new interface creation. */
917 if_clone_detach(&wg_cloner);
918
919 /* Check whether there are any existing interfaces. */
920 if (atomic_load_relaxed(&wg_count)) {
921 /* Back out -- reattach the cloner. */
922 if_clone_attach(&wg_cloner);
923 return EBUSY;
924 }
925
926 /* No interfaces left. Nuke it. */
927 workqueue_destroy(wg_wq);
928 pktq_destroy(wg_pktq);
929 psref_class_destroy(wg_psref_class);
930
931 return 0;
932 }
933
934 static void
935 wg_init_key_and_hash(uint8_t ckey[WG_CHAINING_KEY_LEN],
936 uint8_t hash[WG_HASH_LEN])
937 {
938 /* [W] 5.4: CONSTRUCTION */
939 const char *signature = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s";
940 /* [W] 5.4: IDENTIFIER */
941 const char *id = "WireGuard v1 zx2c4 Jason (at) zx2c4.com";
942 struct blake2s state;
943
944 blake2s(ckey, WG_CHAINING_KEY_LEN, NULL, 0,
945 signature, strlen(signature));
946
947 CTASSERT(WG_HASH_LEN == WG_CHAINING_KEY_LEN);
948 memcpy(hash, ckey, WG_CHAINING_KEY_LEN);
949
950 blake2s_init(&state, WG_HASH_LEN, NULL, 0);
951 blake2s_update(&state, ckey, WG_CHAINING_KEY_LEN);
952 blake2s_update(&state, id, strlen(id));
953 blake2s_final(&state, hash);
954
955 WG_DUMP_HASH("ckey", ckey);
956 WG_DUMP_HASH("hash", hash);
957 }
958
959 static void
960 wg_algo_hash(uint8_t hash[WG_HASH_LEN], const uint8_t input[],
961 const size_t inputsize)
962 {
963 struct blake2s state;
964
965 blake2s_init(&state, WG_HASH_LEN, NULL, 0);
966 blake2s_update(&state, hash, WG_HASH_LEN);
967 blake2s_update(&state, input, inputsize);
968 blake2s_final(&state, hash);
969 }
970
971 static void
972 wg_algo_mac(uint8_t out[], const size_t outsize,
973 const uint8_t key[], const size_t keylen,
974 const uint8_t input1[], const size_t input1len,
975 const uint8_t input2[], const size_t input2len)
976 {
977 struct blake2s state;
978
979 blake2s_init(&state, outsize, key, keylen);
980
981 blake2s_update(&state, input1, input1len);
982 if (input2 != NULL)
983 blake2s_update(&state, input2, input2len);
984 blake2s_final(&state, out);
985 }
986
987 static void
988 wg_algo_mac_mac1(uint8_t out[], const size_t outsize,
989 const uint8_t input1[], const size_t input1len,
990 const uint8_t input2[], const size_t input2len)
991 {
992 struct blake2s state;
993 /* [W] 5.4: LABEL-MAC1 */
994 const char *label = "mac1----";
995 uint8_t key[WG_HASH_LEN];
996
997 blake2s_init(&state, sizeof(key), NULL, 0);
998 blake2s_update(&state, label, strlen(label));
999 blake2s_update(&state, input1, input1len);
1000 blake2s_final(&state, key);
1001
1002 blake2s_init(&state, outsize, key, sizeof(key));
1003 if (input2 != NULL)
1004 blake2s_update(&state, input2, input2len);
1005 blake2s_final(&state, out);
1006 }
1007
1008 static void
1009 wg_algo_mac_cookie(uint8_t out[], const size_t outsize,
1010 const uint8_t input1[], const size_t input1len)
1011 {
1012 struct blake2s state;
1013 /* [W] 5.4: LABEL-COOKIE */
1014 const char *label = "cookie--";
1015
1016 blake2s_init(&state, outsize, NULL, 0);
1017 blake2s_update(&state, label, strlen(label));
1018 blake2s_update(&state, input1, input1len);
1019 blake2s_final(&state, out);
1020 }
1021
1022 static void
1023 wg_algo_generate_keypair(uint8_t pubkey[WG_EPHEMERAL_KEY_LEN],
1024 uint8_t privkey[WG_EPHEMERAL_KEY_LEN])
1025 {
1026
1027 CTASSERT(WG_EPHEMERAL_KEY_LEN == crypto_scalarmult_curve25519_BYTES);
1028
1029 cprng_strong(kern_cprng, privkey, WG_EPHEMERAL_KEY_LEN, 0);
1030 crypto_scalarmult_base(pubkey, privkey);
1031 }
1032
1033 static void
1034 wg_algo_dh(uint8_t out[WG_DH_OUTPUT_LEN],
1035 const uint8_t privkey[WG_STATIC_KEY_LEN],
1036 const uint8_t pubkey[WG_STATIC_KEY_LEN])
1037 {
1038
1039 CTASSERT(WG_STATIC_KEY_LEN == crypto_scalarmult_curve25519_BYTES);
1040
1041 int ret __diagused = crypto_scalarmult(out, privkey, pubkey);
1042 KASSERT(ret == 0);
1043 }
1044
1045 static void
1046 wg_algo_hmac(uint8_t out[], const size_t outlen,
1047 const uint8_t key[], const size_t keylen,
1048 const uint8_t in[], const size_t inlen)
1049 {
1050 #define IPAD 0x36
1051 #define OPAD 0x5c
1052 uint8_t hmackey[HMAC_BLOCK_LEN] = {0};
1053 uint8_t ipad[HMAC_BLOCK_LEN];
1054 uint8_t opad[HMAC_BLOCK_LEN];
1055 size_t i;
1056 struct blake2s state;
1057
1058 KASSERT(outlen == WG_HASH_LEN);
1059 KASSERT(keylen <= HMAC_BLOCK_LEN);
1060
1061 memcpy(hmackey, key, keylen);
1062
1063 for (i = 0; i < sizeof(hmackey); i++) {
1064 ipad[i] = hmackey[i] ^ IPAD;
1065 opad[i] = hmackey[i] ^ OPAD;
1066 }
1067
1068 blake2s_init(&state, WG_HASH_LEN, NULL, 0);
1069 blake2s_update(&state, ipad, sizeof(ipad));
1070 blake2s_update(&state, in, inlen);
1071 blake2s_final(&state, out);
1072
1073 blake2s_init(&state, WG_HASH_LEN, NULL, 0);
1074 blake2s_update(&state, opad, sizeof(opad));
1075 blake2s_update(&state, out, WG_HASH_LEN);
1076 blake2s_final(&state, out);
1077 #undef IPAD
1078 #undef OPAD
1079 }
1080
1081 static void
1082 wg_algo_kdf(uint8_t out1[WG_KDF_OUTPUT_LEN], uint8_t out2[WG_KDF_OUTPUT_LEN],
1083 uint8_t out3[WG_KDF_OUTPUT_LEN], const uint8_t ckey[WG_CHAINING_KEY_LEN],
1084 const uint8_t input[], const size_t inputlen)
1085 {
1086 uint8_t tmp1[WG_KDF_OUTPUT_LEN], tmp2[WG_KDF_OUTPUT_LEN + 1];
1087 uint8_t one[1];
1088
1089 /*
1090 * [N] 4.3: "an input_key_material byte sequence with length
1091 * either zero bytes, 32 bytes, or DHLEN bytes."
1092 */
1093 KASSERT(inputlen == 0 || inputlen == 32 || inputlen == NOISE_DHLEN);
1094
1095 WG_DUMP_HASH("ckey", ckey);
1096 if (input != NULL)
1097 WG_DUMP_HASH("input", input);
1098 wg_algo_hmac(tmp1, sizeof(tmp1), ckey, WG_CHAINING_KEY_LEN,
1099 input, inputlen);
1100 WG_DUMP_HASH("tmp1", tmp1);
1101 one[0] = 1;
1102 wg_algo_hmac(out1, WG_KDF_OUTPUT_LEN, tmp1, sizeof(tmp1),
1103 one, sizeof(one));
1104 WG_DUMP_HASH("out1", out1);
1105 if (out2 == NULL)
1106 return;
1107 memcpy(tmp2, out1, WG_KDF_OUTPUT_LEN);
1108 tmp2[WG_KDF_OUTPUT_LEN] = 2;
1109 wg_algo_hmac(out2, WG_KDF_OUTPUT_LEN, tmp1, sizeof(tmp1),
1110 tmp2, sizeof(tmp2));
1111 WG_DUMP_HASH("out2", out2);
1112 if (out3 == NULL)
1113 return;
1114 memcpy(tmp2, out2, WG_KDF_OUTPUT_LEN);
1115 tmp2[WG_KDF_OUTPUT_LEN] = 3;
1116 wg_algo_hmac(out3, WG_KDF_OUTPUT_LEN, tmp1, sizeof(tmp1),
1117 tmp2, sizeof(tmp2));
1118 WG_DUMP_HASH("out3", out3);
1119 }
1120
1121 static void __noinline
1122 wg_algo_dh_kdf(uint8_t ckey[WG_CHAINING_KEY_LEN],
1123 uint8_t cipher_key[WG_CIPHER_KEY_LEN],
1124 const uint8_t local_key[WG_STATIC_KEY_LEN],
1125 const uint8_t remote_key[WG_STATIC_KEY_LEN])
1126 {
1127 uint8_t dhout[WG_DH_OUTPUT_LEN];
1128
1129 wg_algo_dh(dhout, local_key, remote_key);
1130 wg_algo_kdf(ckey, cipher_key, NULL, ckey, dhout, sizeof(dhout));
1131
1132 WG_DUMP_HASH("dhout", dhout);
1133 WG_DUMP_HASH("ckey", ckey);
1134 if (cipher_key != NULL)
1135 WG_DUMP_HASH("cipher_key", cipher_key);
1136 }
1137
1138 static void
1139 wg_algo_aead_enc(uint8_t out[], size_t expected_outsize, const uint8_t key[],
1140 const uint64_t counter, const uint8_t plain[], const size_t plainsize,
1141 const uint8_t auth[], size_t authlen)
1142 {
1143 uint8_t nonce[(32 + 64) / 8] = {0};
1144 long long unsigned int outsize;
1145 int error __diagused;
1146
1147 le64enc(&nonce[4], counter);
1148
1149 error = crypto_aead_chacha20poly1305_ietf_encrypt(out, &outsize, plain,
1150 plainsize, auth, authlen, NULL, nonce, key);
1151 KASSERT(error == 0);
1152 KASSERT(outsize == expected_outsize);
1153 }
1154
1155 static int
1156 wg_algo_aead_dec(uint8_t out[], size_t expected_outsize, const uint8_t key[],
1157 const uint64_t counter, const uint8_t encrypted[],
1158 const size_t encryptedsize, const uint8_t auth[], size_t authlen)
1159 {
1160 uint8_t nonce[(32 + 64) / 8] = {0};
1161 long long unsigned int outsize;
1162 int error;
1163
1164 le64enc(&nonce[4], counter);
1165
1166 error = crypto_aead_chacha20poly1305_ietf_decrypt(out, &outsize, NULL,
1167 encrypted, encryptedsize, auth, authlen, nonce, key);
1168 if (error == 0)
1169 KASSERT(outsize == expected_outsize);
1170 return error;
1171 }
1172
1173 static void
1174 wg_algo_xaead_enc(uint8_t out[], const size_t expected_outsize,
1175 const uint8_t key[], const uint8_t plain[], const size_t plainsize,
1176 const uint8_t auth[], size_t authlen,
1177 const uint8_t nonce[WG_SALT_LEN])
1178 {
1179 long long unsigned int outsize;
1180 int error __diagused;
1181
1182 CTASSERT(WG_SALT_LEN == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
1183 error = crypto_aead_xchacha20poly1305_ietf_encrypt(out, &outsize,
1184 plain, plainsize, auth, authlen, NULL, nonce, key);
1185 KASSERT(error == 0);
1186 KASSERT(outsize == expected_outsize);
1187 }
1188
1189 static int
1190 wg_algo_xaead_dec(uint8_t out[], const size_t expected_outsize,
1191 const uint8_t key[], const uint8_t encrypted[], const size_t encryptedsize,
1192 const uint8_t auth[], size_t authlen,
1193 const uint8_t nonce[WG_SALT_LEN])
1194 {
1195 long long unsigned int outsize;
1196 int error;
1197
1198 error = crypto_aead_xchacha20poly1305_ietf_decrypt(out, &outsize, NULL,
1199 encrypted, encryptedsize, auth, authlen, nonce, key);
1200 if (error == 0)
1201 KASSERT(outsize == expected_outsize);
1202 return error;
1203 }
1204
1205 static void
1206 wg_algo_tai64n(wg_timestamp_t timestamp)
1207 {
1208 struct timespec ts;
1209
1210 /* FIXME strict TAI64N (https://cr.yp.to/libtai/tai64.html) */
1211 getnanotime(&ts);
1212 /* TAI64 label in external TAI64 format */
1213 be32enc(timestamp, 0x40000000U + (uint32_t)(ts.tv_sec >> 32));
1214 /* second beginning from 1970 TAI */
1215 be32enc(timestamp + 4, (uint32_t)(ts.tv_sec & 0xffffffffU));
1216 /* nanosecond in big-endian format */
1217 be32enc(timestamp + 8, (uint32_t)ts.tv_nsec);
1218 }
1219
1220 /*
1221 * wg_get_stable_session(wgp, psref)
1222 *
1223 * Get a passive reference to the current stable session, or
1224 * return NULL if there is no current stable session.
1225 *
1226 * The pointer is always there but the session is not necessarily
1227 * ESTABLISHED; if it is not ESTABLISHED, return NULL. However,
1228 * the session may transition from ESTABLISHED to DESTROYING while
1229 * holding the passive reference.
1230 */
1231 static struct wg_session *
1232 wg_get_stable_session(struct wg_peer *wgp, struct psref *psref)
1233 {
1234 int s;
1235 struct wg_session *wgs;
1236
1237 s = pserialize_read_enter();
1238 wgs = atomic_load_consume(&wgp->wgp_session_stable);
1239 if (__predict_false(wgs->wgs_state != WGS_STATE_ESTABLISHED))
1240 wgs = NULL;
1241 else
1242 psref_acquire(psref, &wgs->wgs_psref, wg_psref_class);
1243 pserialize_read_exit(s);
1244
1245 return wgs;
1246 }
1247
1248 static void
1249 wg_put_session(struct wg_session *wgs, struct psref *psref)
1250 {
1251
1252 psref_release(psref, &wgs->wgs_psref, wg_psref_class);
1253 }
1254
1255 static void
1256 wg_destroy_session(struct wg_softc *wg, struct wg_session *wgs)
1257 {
1258 struct wg_peer *wgp = wgs->wgs_peer;
1259 struct wg_session *wgs0 __diagused;
1260 void *garbage;
1261
1262 KASSERT(mutex_owned(wgp->wgp_lock));
1263 KASSERT(wgs->wgs_state != WGS_STATE_UNKNOWN);
1264
1265 /* Remove the session from the table. */
1266 wgs0 = thmap_del(wg->wg_sessions_byindex,
1267 &wgs->wgs_local_index, sizeof(wgs->wgs_local_index));
1268 KASSERT(wgs0 == wgs);
1269 garbage = thmap_stage_gc(wg->wg_sessions_byindex);
1270
1271 /* Wait for passive references to drain. */
1272 pserialize_perform(wgp->wgp_psz);
1273 psref_target_destroy(&wgs->wgs_psref, wg_psref_class);
1274
1275 /* Free memory, zero state, and transition to UNKNOWN. */
1276 thmap_gc(wg->wg_sessions_byindex, garbage);
1277 wg_clear_states(wgs);
1278 wgs->wgs_state = WGS_STATE_UNKNOWN;
1279 }
1280
1281 /*
1282 * wg_get_session_index(wg, wgs)
1283 *
1284 * Choose a session index for wgs->wgs_local_index, and store it
1285 * in wg's table of sessions by index.
1286 *
1287 * wgs must be the unstable session of its peer, and must be
1288 * transitioning out of the UNKNOWN state.
1289 */
1290 static void
1291 wg_get_session_index(struct wg_softc *wg, struct wg_session *wgs)
1292 {
1293 struct wg_peer *wgp __diagused = wgs->wgs_peer;
1294 struct wg_session *wgs0;
1295 uint32_t index;
1296
1297 KASSERT(mutex_owned(wgp->wgp_lock));
1298 KASSERT(wgs == wgp->wgp_session_unstable);
1299 KASSERT(wgs->wgs_state == WGS_STATE_UNKNOWN);
1300
1301 do {
1302 /* Pick a uniform random index. */
1303 index = cprng_strong32();
1304
1305 /* Try to take it. */
1306 wgs->wgs_local_index = index;
1307 wgs0 = thmap_put(wg->wg_sessions_byindex,
1308 &wgs->wgs_local_index, sizeof wgs->wgs_local_index, wgs);
1309
1310 /* If someone else beat us, start over. */
1311 } while (__predict_false(wgs0 != wgs));
1312 }
1313
1314 /*
1315 * wg_put_session_index(wg, wgs)
1316 *
1317 * Remove wgs from the table of sessions by index, wait for any
1318 * passive references to drain, and transition the session to the
1319 * UNKNOWN state.
1320 *
1321 * wgs must be the unstable session of its peer, and must not be
1322 * UNKNOWN or ESTABLISHED.
1323 */
1324 static void
1325 wg_put_session_index(struct wg_softc *wg, struct wg_session *wgs)
1326 {
1327 struct wg_peer *wgp __diagused = wgs->wgs_peer;
1328
1329 KASSERT(mutex_owned(wgp->wgp_lock));
1330 KASSERT(wgs == wgp->wgp_session_unstable);
1331 KASSERT(wgs->wgs_state != WGS_STATE_UNKNOWN);
1332 KASSERT(wgs->wgs_state != WGS_STATE_ESTABLISHED);
1333
1334 wg_destroy_session(wg, wgs);
1335 psref_target_init(&wgs->wgs_psref, wg_psref_class);
1336 }
1337
1338 /*
1339 * Handshake patterns
1340 *
1341 * [W] 5: "These messages use the "IK" pattern from Noise"
1342 * [N] 7.5. Interactive handshake patterns (fundamental)
1343 * "The first character refers to the initiators static key:"
1344 * "I = Static key for initiator Immediately transmitted to responder,
1345 * despite reduced or absent identity hiding"
1346 * "The second character refers to the responders static key:"
1347 * "K = Static key for responder Known to initiator"
1348 * "IK:
1349 * <- s
1350 * ...
1351 * -> e, es, s, ss
1352 * <- e, ee, se"
1353 * [N] 9.4. Pattern modifiers
1354 * "IKpsk2:
1355 * <- s
1356 * ...
1357 * -> e, es, s, ss
1358 * <- e, ee, se, psk"
1359 */
1360 static void
1361 wg_fill_msg_init(struct wg_softc *wg, struct wg_peer *wgp,
1362 struct wg_session *wgs, struct wg_msg_init *wgmi)
1363 {
1364 uint8_t ckey[WG_CHAINING_KEY_LEN]; /* [W] 5.4.2: Ci */
1365 uint8_t hash[WG_HASH_LEN]; /* [W] 5.4.2: Hi */
1366 uint8_t cipher_key[WG_CIPHER_KEY_LEN];
1367 uint8_t pubkey[WG_EPHEMERAL_KEY_LEN];
1368 uint8_t privkey[WG_EPHEMERAL_KEY_LEN];
1369
1370 KASSERT(mutex_owned(wgp->wgp_lock));
1371 KASSERT(wgs == wgp->wgp_session_unstable);
1372 KASSERT(wgs->wgs_state == WGS_STATE_INIT_ACTIVE);
1373
1374 wgmi->wgmi_type = htole32(WG_MSG_TYPE_INIT);
1375 wgmi->wgmi_sender = wgs->wgs_local_index;
1376
1377 /* [W] 5.4.2: First Message: Initiator to Responder */
1378
1379 /* Ci := HASH(CONSTRUCTION) */
1380 /* Hi := HASH(Ci || IDENTIFIER) */
1381 wg_init_key_and_hash(ckey, hash);
1382 /* Hi := HASH(Hi || Sr^pub) */
1383 wg_algo_hash(hash, wgp->wgp_pubkey, sizeof(wgp->wgp_pubkey));
1384
1385 WG_DUMP_HASH("hash", hash);
1386
1387 /* [N] 2.2: "e" */
1388 /* Ei^priv, Ei^pub := DH-GENERATE() */
1389 wg_algo_generate_keypair(pubkey, privkey);
1390 /* Ci := KDF1(Ci, Ei^pub) */
1391 wg_algo_kdf(ckey, NULL, NULL, ckey, pubkey, sizeof(pubkey));
1392 /* msg.ephemeral := Ei^pub */
1393 memcpy(wgmi->wgmi_ephemeral, pubkey, sizeof(wgmi->wgmi_ephemeral));
1394 /* Hi := HASH(Hi || msg.ephemeral) */
1395 wg_algo_hash(hash, pubkey, sizeof(pubkey));
1396
1397 WG_DUMP_HASH("ckey", ckey);
1398 WG_DUMP_HASH("hash", hash);
1399
1400 /* [N] 2.2: "es" */
1401 /* Ci, k := KDF2(Ci, DH(Ei^priv, Sr^pub)) */
1402 wg_algo_dh_kdf(ckey, cipher_key, privkey, wgp->wgp_pubkey);
1403
1404 /* [N] 2.2: "s" */
1405 /* msg.static := AEAD(k, 0, Si^pub, Hi) */
1406 wg_algo_aead_enc(wgmi->wgmi_static, sizeof(wgmi->wgmi_static),
1407 cipher_key, 0, wg->wg_pubkey, sizeof(wg->wg_pubkey),
1408 hash, sizeof(hash));
1409 /* Hi := HASH(Hi || msg.static) */
1410 wg_algo_hash(hash, wgmi->wgmi_static, sizeof(wgmi->wgmi_static));
1411
1412 WG_DUMP_HASH48("wgmi_static", wgmi->wgmi_static);
1413
1414 /* [N] 2.2: "ss" */
1415 /* Ci, k := KDF2(Ci, DH(Si^priv, Sr^pub)) */
1416 wg_algo_dh_kdf(ckey, cipher_key, wg->wg_privkey, wgp->wgp_pubkey);
1417
1418 /* msg.timestamp := AEAD(k, TIMESTAMP(), Hi) */
1419 wg_timestamp_t timestamp;
1420 wg_algo_tai64n(timestamp);
1421 wg_algo_aead_enc(wgmi->wgmi_timestamp, sizeof(wgmi->wgmi_timestamp),
1422 cipher_key, 0, timestamp, sizeof(timestamp), hash, sizeof(hash));
1423 /* Hi := HASH(Hi || msg.timestamp) */
1424 wg_algo_hash(hash, wgmi->wgmi_timestamp, sizeof(wgmi->wgmi_timestamp));
1425
1426 /* [W] 5.4.4 Cookie MACs */
1427 wg_algo_mac_mac1(wgmi->wgmi_mac1, sizeof(wgmi->wgmi_mac1),
1428 wgp->wgp_pubkey, sizeof(wgp->wgp_pubkey),
1429 (const uint8_t *)wgmi, offsetof(struct wg_msg_init, wgmi_mac1));
1430 /* Need mac1 to decrypt a cookie from a cookie message */
1431 memcpy(wgp->wgp_last_sent_mac1, wgmi->wgmi_mac1,
1432 sizeof(wgp->wgp_last_sent_mac1));
1433 wgp->wgp_last_sent_mac1_valid = true;
1434
1435 if (wgp->wgp_latest_cookie_time == 0 ||
1436 (time_uptime - wgp->wgp_latest_cookie_time) >= WG_COOKIE_TIME)
1437 memset(wgmi->wgmi_mac2, 0, sizeof(wgmi->wgmi_mac2));
1438 else {
1439 wg_algo_mac(wgmi->wgmi_mac2, sizeof(wgmi->wgmi_mac2),
1440 wgp->wgp_latest_cookie, WG_COOKIE_LEN,
1441 (const uint8_t *)wgmi,
1442 offsetof(struct wg_msg_init, wgmi_mac2),
1443 NULL, 0);
1444 }
1445
1446 memcpy(wgs->wgs_ephemeral_key_pub, pubkey, sizeof(pubkey));
1447 memcpy(wgs->wgs_ephemeral_key_priv, privkey, sizeof(privkey));
1448 memcpy(wgs->wgs_handshake_hash, hash, sizeof(hash));
1449 memcpy(wgs->wgs_chaining_key, ckey, sizeof(ckey));
1450 WG_DLOG("%s: sender=%x\n", __func__, wgs->wgs_local_index);
1451 }
1452
1453 static void __noinline
1454 wg_handle_msg_init(struct wg_softc *wg, const struct wg_msg_init *wgmi,
1455 const struct sockaddr *src)
1456 {
1457 uint8_t ckey[WG_CHAINING_KEY_LEN]; /* [W] 5.4.2: Ci */
1458 uint8_t hash[WG_HASH_LEN]; /* [W] 5.4.2: Hi */
1459 uint8_t cipher_key[WG_CIPHER_KEY_LEN];
1460 uint8_t peer_pubkey[WG_STATIC_KEY_LEN];
1461 struct wg_peer *wgp;
1462 struct wg_session *wgs;
1463 int error, ret;
1464 struct psref psref_peer;
1465 uint8_t mac1[WG_MAC_LEN];
1466
1467 WG_TRACE("init msg received");
1468
1469 wg_algo_mac_mac1(mac1, sizeof(mac1),
1470 wg->wg_pubkey, sizeof(wg->wg_pubkey),
1471 (const uint8_t *)wgmi, offsetof(struct wg_msg_init, wgmi_mac1));
1472
1473 /*
1474 * [W] 5.3: Denial of Service Mitigation & Cookies
1475 * "the responder, ..., must always reject messages with an invalid
1476 * msg.mac1"
1477 */
1478 if (!consttime_memequal(mac1, wgmi->wgmi_mac1, sizeof(mac1))) {
1479 WG_DLOG("mac1 is invalid\n");
1480 return;
1481 }
1482
1483 /*
1484 * [W] 5.4.2: First Message: Initiator to Responder
1485 * "When the responder receives this message, it does the same
1486 * operations so that its final state variables are identical,
1487 * replacing the operands of the DH function to produce equivalent
1488 * values."
1489 * Note that the following comments of operations are just copies of
1490 * the initiator's ones.
1491 */
1492
1493 /* Ci := HASH(CONSTRUCTION) */
1494 /* Hi := HASH(Ci || IDENTIFIER) */
1495 wg_init_key_and_hash(ckey, hash);
1496 /* Hi := HASH(Hi || Sr^pub) */
1497 wg_algo_hash(hash, wg->wg_pubkey, sizeof(wg->wg_pubkey));
1498
1499 /* [N] 2.2: "e" */
1500 /* Ci := KDF1(Ci, Ei^pub) */
1501 wg_algo_kdf(ckey, NULL, NULL, ckey, wgmi->wgmi_ephemeral,
1502 sizeof(wgmi->wgmi_ephemeral));
1503 /* Hi := HASH(Hi || msg.ephemeral) */
1504 wg_algo_hash(hash, wgmi->wgmi_ephemeral, sizeof(wgmi->wgmi_ephemeral));
1505
1506 WG_DUMP_HASH("ckey", ckey);
1507
1508 /* [N] 2.2: "es" */
1509 /* Ci, k := KDF2(Ci, DH(Ei^priv, Sr^pub)) */
1510 wg_algo_dh_kdf(ckey, cipher_key, wg->wg_privkey, wgmi->wgmi_ephemeral);
1511
1512 WG_DUMP_HASH48("wgmi_static", wgmi->wgmi_static);
1513
1514 /* [N] 2.2: "s" */
1515 /* msg.static := AEAD(k, 0, Si^pub, Hi) */
1516 error = wg_algo_aead_dec(peer_pubkey, WG_STATIC_KEY_LEN, cipher_key, 0,
1517 wgmi->wgmi_static, sizeof(wgmi->wgmi_static), hash, sizeof(hash));
1518 if (error != 0) {
1519 WG_LOG_RATECHECK(&wg->wg_ppsratecheck, LOG_DEBUG,
1520 "%s: wg_algo_aead_dec for secret key failed\n",
1521 if_name(&wg->wg_if));
1522 return;
1523 }
1524 /* Hi := HASH(Hi || msg.static) */
1525 wg_algo_hash(hash, wgmi->wgmi_static, sizeof(wgmi->wgmi_static));
1526
1527 wgp = wg_lookup_peer_by_pubkey(wg, peer_pubkey, &psref_peer);
1528 if (wgp == NULL) {
1529 WG_DLOG("peer not found\n");
1530 return;
1531 }
1532
1533 /*
1534 * Lock the peer to serialize access to cookie state.
1535 *
1536 * XXX Can we safely avoid holding the lock across DH? Take it
1537 * just to verify mac2 and then unlock/DH/lock?
1538 */
1539 mutex_enter(wgp->wgp_lock);
1540
1541 if (__predict_false(wg_is_underload(wg, wgp, WG_MSG_TYPE_INIT))) {
1542 WG_TRACE("under load");
1543 /*
1544 * [W] 5.3: Denial of Service Mitigation & Cookies
1545 * "the responder, ..., and when under load may reject messages
1546 * with an invalid msg.mac2. If the responder receives a
1547 * message with a valid msg.mac1 yet with an invalid msg.mac2,
1548 * and is under load, it may respond with a cookie reply
1549 * message"
1550 */
1551 uint8_t zero[WG_MAC_LEN] = {0};
1552 if (consttime_memequal(wgmi->wgmi_mac2, zero, sizeof(zero))) {
1553 WG_TRACE("sending a cookie message: no cookie included");
1554 (void)wg_send_cookie_msg(wg, wgp, wgmi->wgmi_sender,
1555 wgmi->wgmi_mac1, src);
1556 goto out;
1557 }
1558 if (!wgp->wgp_last_sent_cookie_valid) {
1559 WG_TRACE("sending a cookie message: no cookie sent ever");
1560 (void)wg_send_cookie_msg(wg, wgp, wgmi->wgmi_sender,
1561 wgmi->wgmi_mac1, src);
1562 goto out;
1563 }
1564 uint8_t mac2[WG_MAC_LEN];
1565 wg_algo_mac(mac2, sizeof(mac2), wgp->wgp_last_sent_cookie,
1566 WG_COOKIE_LEN, (const uint8_t *)wgmi,
1567 offsetof(struct wg_msg_init, wgmi_mac2), NULL, 0);
1568 if (!consttime_memequal(mac2, wgmi->wgmi_mac2, sizeof(mac2))) {
1569 WG_DLOG("mac2 is invalid\n");
1570 goto out;
1571 }
1572 WG_TRACE("under load, but continue to sending");
1573 }
1574
1575 /* [N] 2.2: "ss" */
1576 /* Ci, k := KDF2(Ci, DH(Si^priv, Sr^pub)) */
1577 wg_algo_dh_kdf(ckey, cipher_key, wg->wg_privkey, wgp->wgp_pubkey);
1578
1579 /* msg.timestamp := AEAD(k, TIMESTAMP(), Hi) */
1580 wg_timestamp_t timestamp;
1581 error = wg_algo_aead_dec(timestamp, sizeof(timestamp), cipher_key, 0,
1582 wgmi->wgmi_timestamp, sizeof(wgmi->wgmi_timestamp),
1583 hash, sizeof(hash));
1584 if (error != 0) {
1585 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
1586 "%s: peer %s: wg_algo_aead_dec for timestamp failed\n",
1587 if_name(&wg->wg_if), wgp->wgp_name);
1588 goto out;
1589 }
1590 /* Hi := HASH(Hi || msg.timestamp) */
1591 wg_algo_hash(hash, wgmi->wgmi_timestamp, sizeof(wgmi->wgmi_timestamp));
1592
1593 /*
1594 * [W] 5.1 "The responder keeps track of the greatest timestamp
1595 * received per peer and discards packets containing
1596 * timestamps less than or equal to it."
1597 */
1598 ret = memcmp(timestamp, wgp->wgp_timestamp_latest_init,
1599 sizeof(timestamp));
1600 if (ret <= 0) {
1601 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
1602 "%s: peer %s: invalid init msg: timestamp is old\n",
1603 if_name(&wg->wg_if), wgp->wgp_name);
1604 goto out;
1605 }
1606 memcpy(wgp->wgp_timestamp_latest_init, timestamp, sizeof(timestamp));
1607
1608 /*
1609 * Message is good -- we're committing to handle it now, unless
1610 * we were already initiating a session.
1611 */
1612 wgs = wgp->wgp_session_unstable;
1613 switch (wgs->wgs_state) {
1614 case WGS_STATE_UNKNOWN: /* new session initiated by peer */
1615 wg_get_session_index(wg, wgs);
1616 break;
1617 case WGS_STATE_INIT_ACTIVE: /* we're already initiating, drop */
1618 WG_TRACE("Session already initializing, ignoring the message");
1619 goto out;
1620 case WGS_STATE_INIT_PASSIVE: /* peer is retrying, start over */
1621 WG_TRACE("Session already initializing, destroying old states");
1622 wg_clear_states(wgs);
1623 /* keep session index */
1624 break;
1625 case WGS_STATE_ESTABLISHED: /* can't happen */
1626 panic("unstable session can't be established");
1627 break;
1628 case WGS_STATE_DESTROYING: /* rekey initiated by peer */
1629 WG_TRACE("Session destroying, but force to clear");
1630 callout_stop(&wgp->wgp_session_dtor_timer);
1631 wg_clear_states(wgs);
1632 /* keep session index */
1633 break;
1634 default:
1635 panic("invalid session state: %d", wgs->wgs_state);
1636 }
1637 wgs->wgs_state = WGS_STATE_INIT_PASSIVE;
1638
1639 memcpy(wgs->wgs_handshake_hash, hash, sizeof(hash));
1640 memcpy(wgs->wgs_chaining_key, ckey, sizeof(ckey));
1641 memcpy(wgs->wgs_ephemeral_key_peer, wgmi->wgmi_ephemeral,
1642 sizeof(wgmi->wgmi_ephemeral));
1643
1644 wg_update_endpoint_if_necessary(wgp, src);
1645
1646 (void)wg_send_handshake_msg_resp(wg, wgp, wgs, wgmi);
1647
1648 wg_calculate_keys(wgs, false);
1649 wg_clear_states(wgs);
1650
1651 out:
1652 mutex_exit(wgp->wgp_lock);
1653 wg_put_peer(wgp, &psref_peer);
1654 }
1655
1656 static struct socket *
1657 wg_get_so_by_af(struct wg_softc *wg, const int af)
1658 {
1659
1660 switch (af) {
1661 #ifdef INET
1662 case AF_INET:
1663 return wg->wg_so4;
1664 #endif
1665 #ifdef INET6
1666 case AF_INET6:
1667 return wg->wg_so6;
1668 #endif
1669 default:
1670 panic("wg: no such af: %d", af);
1671 }
1672 }
1673
1674 static struct socket *
1675 wg_get_so_by_peer(struct wg_peer *wgp, struct wg_sockaddr *wgsa)
1676 {
1677
1678 return wg_get_so_by_af(wgp->wgp_sc, wgsa_family(wgsa));
1679 }
1680
1681 static struct wg_sockaddr *
1682 wg_get_endpoint_sa(struct wg_peer *wgp, struct psref *psref)
1683 {
1684 struct wg_sockaddr *wgsa;
1685 int s;
1686
1687 s = pserialize_read_enter();
1688 wgsa = atomic_load_consume(&wgp->wgp_endpoint);
1689 psref_acquire(psref, &wgsa->wgsa_psref, wg_psref_class);
1690 pserialize_read_exit(s);
1691
1692 return wgsa;
1693 }
1694
1695 static void
1696 wg_put_sa(struct wg_peer *wgp, struct wg_sockaddr *wgsa, struct psref *psref)
1697 {
1698
1699 psref_release(psref, &wgsa->wgsa_psref, wg_psref_class);
1700 }
1701
1702 static int
1703 wg_send_so(struct wg_peer *wgp, struct mbuf *m)
1704 {
1705 int error;
1706 struct socket *so;
1707 struct psref psref;
1708 struct wg_sockaddr *wgsa;
1709
1710 wgsa = wg_get_endpoint_sa(wgp, &psref);
1711 so = wg_get_so_by_peer(wgp, wgsa);
1712 error = sosend(so, wgsatosa(wgsa), NULL, m, NULL, 0, curlwp);
1713 wg_put_sa(wgp, wgsa, &psref);
1714
1715 return error;
1716 }
1717
1718 static int
1719 wg_send_handshake_msg_init(struct wg_softc *wg, struct wg_peer *wgp)
1720 {
1721 int error;
1722 struct mbuf *m;
1723 struct wg_msg_init *wgmi;
1724 struct wg_session *wgs;
1725
1726 KASSERT(mutex_owned(wgp->wgp_lock));
1727
1728 wgs = wgp->wgp_session_unstable;
1729 /* XXX pull dispatch out into wg_task_send_init_message */
1730 switch (wgs->wgs_state) {
1731 case WGS_STATE_UNKNOWN: /* new session initiated by us */
1732 wg_get_session_index(wg, wgs);
1733 break;
1734 case WGS_STATE_INIT_ACTIVE: /* we're already initiating, stop */
1735 WG_TRACE("Session already initializing, skip starting new one");
1736 return EBUSY;
1737 case WGS_STATE_INIT_PASSIVE: /* peer was trying -- XXX what now? */
1738 WG_TRACE("Session already initializing, destroying old states");
1739 wg_clear_states(wgs);
1740 /* keep session index */
1741 break;
1742 case WGS_STATE_ESTABLISHED: /* can't happen */
1743 panic("unstable session can't be established");
1744 break;
1745 case WGS_STATE_DESTROYING: /* rekey initiated by us too early */
1746 WG_TRACE("Session destroying");
1747 /* XXX should wait? */
1748 return EBUSY;
1749 }
1750 wgs->wgs_state = WGS_STATE_INIT_ACTIVE;
1751
1752 m = m_gethdr(M_WAIT, MT_DATA);
1753 if (sizeof(*wgmi) > MHLEN) {
1754 m_clget(m, M_WAIT);
1755 CTASSERT(sizeof(*wgmi) <= MCLBYTES);
1756 }
1757 m->m_pkthdr.len = m->m_len = sizeof(*wgmi);
1758 wgmi = mtod(m, struct wg_msg_init *);
1759 wg_fill_msg_init(wg, wgp, wgs, wgmi);
1760
1761 error = wg->wg_ops->send_hs_msg(wgp, m);
1762 if (error == 0) {
1763 WG_TRACE("init msg sent");
1764
1765 if (wgp->wgp_handshake_start_time == 0)
1766 wgp->wgp_handshake_start_time = time_uptime;
1767 callout_schedule(&wgp->wgp_handshake_timeout_timer,
1768 MIN(wg_rekey_timeout, (unsigned)(INT_MAX / hz)) * hz);
1769 } else {
1770 wg_put_session_index(wg, wgs);
1771 /* Initiation failed; toss packet waiting for it if any. */
1772 m = atomic_swap_ptr(&wgp->wgp_pending, NULL);
1773 m_freem(m);
1774 }
1775
1776 return error;
1777 }
1778
1779 static void
1780 wg_fill_msg_resp(struct wg_softc *wg, struct wg_peer *wgp,
1781 struct wg_session *wgs, struct wg_msg_resp *wgmr,
1782 const struct wg_msg_init *wgmi)
1783 {
1784 uint8_t ckey[WG_CHAINING_KEY_LEN]; /* [W] 5.4.3: Cr */
1785 uint8_t hash[WG_HASH_LEN]; /* [W] 5.4.3: Hr */
1786 uint8_t cipher_key[WG_KDF_OUTPUT_LEN];
1787 uint8_t pubkey[WG_EPHEMERAL_KEY_LEN];
1788 uint8_t privkey[WG_EPHEMERAL_KEY_LEN];
1789
1790 KASSERT(mutex_owned(wgp->wgp_lock));
1791 KASSERT(wgs == wgp->wgp_session_unstable);
1792 KASSERT(wgs->wgs_state == WGS_STATE_INIT_PASSIVE);
1793
1794 memcpy(hash, wgs->wgs_handshake_hash, sizeof(hash));
1795 memcpy(ckey, wgs->wgs_chaining_key, sizeof(ckey));
1796
1797 wgmr->wgmr_type = htole32(WG_MSG_TYPE_RESP);
1798 wgmr->wgmr_sender = wgs->wgs_local_index;
1799 wgmr->wgmr_receiver = wgmi->wgmi_sender;
1800
1801 /* [W] 5.4.3 Second Message: Responder to Initiator */
1802
1803 /* [N] 2.2: "e" */
1804 /* Er^priv, Er^pub := DH-GENERATE() */
1805 wg_algo_generate_keypair(pubkey, privkey);
1806 /* Cr := KDF1(Cr, Er^pub) */
1807 wg_algo_kdf(ckey, NULL, NULL, ckey, pubkey, sizeof(pubkey));
1808 /* msg.ephemeral := Er^pub */
1809 memcpy(wgmr->wgmr_ephemeral, pubkey, sizeof(wgmr->wgmr_ephemeral));
1810 /* Hr := HASH(Hr || msg.ephemeral) */
1811 wg_algo_hash(hash, pubkey, sizeof(pubkey));
1812
1813 WG_DUMP_HASH("ckey", ckey);
1814 WG_DUMP_HASH("hash", hash);
1815
1816 /* [N] 2.2: "ee" */
1817 /* Cr := KDF1(Cr, DH(Er^priv, Ei^pub)) */
1818 wg_algo_dh_kdf(ckey, NULL, privkey, wgs->wgs_ephemeral_key_peer);
1819
1820 /* [N] 2.2: "se" */
1821 /* Cr := KDF1(Cr, DH(Er^priv, Si^pub)) */
1822 wg_algo_dh_kdf(ckey, NULL, privkey, wgp->wgp_pubkey);
1823
1824 /* [N] 9.2: "psk" */
1825 {
1826 uint8_t kdfout[WG_KDF_OUTPUT_LEN];
1827 /* Cr, r, k := KDF3(Cr, Q) */
1828 wg_algo_kdf(ckey, kdfout, cipher_key, ckey, wgp->wgp_psk,
1829 sizeof(wgp->wgp_psk));
1830 /* Hr := HASH(Hr || r) */
1831 wg_algo_hash(hash, kdfout, sizeof(kdfout));
1832 }
1833
1834 /* msg.empty := AEAD(k, 0, e, Hr) */
1835 wg_algo_aead_enc(wgmr->wgmr_empty, sizeof(wgmr->wgmr_empty),
1836 cipher_key, 0, NULL, 0, hash, sizeof(hash));
1837 /* Hr := HASH(Hr || msg.empty) */
1838 wg_algo_hash(hash, wgmr->wgmr_empty, sizeof(wgmr->wgmr_empty));
1839
1840 WG_DUMP_HASH("wgmr_empty", wgmr->wgmr_empty);
1841
1842 /* [W] 5.4.4: Cookie MACs */
1843 /* msg.mac1 := MAC(HASH(LABEL-MAC1 || Sm'^pub), msg_a) */
1844 wg_algo_mac_mac1(wgmr->wgmr_mac1, sizeof(wgmi->wgmi_mac1),
1845 wgp->wgp_pubkey, sizeof(wgp->wgp_pubkey),
1846 (const uint8_t *)wgmr, offsetof(struct wg_msg_resp, wgmr_mac1));
1847 /* Need mac1 to decrypt a cookie from a cookie message */
1848 memcpy(wgp->wgp_last_sent_mac1, wgmr->wgmr_mac1,
1849 sizeof(wgp->wgp_last_sent_mac1));
1850 wgp->wgp_last_sent_mac1_valid = true;
1851
1852 if (wgp->wgp_latest_cookie_time == 0 ||
1853 (time_uptime - wgp->wgp_latest_cookie_time) >= WG_COOKIE_TIME)
1854 /* msg.mac2 := 0^16 */
1855 memset(wgmr->wgmr_mac2, 0, sizeof(wgmr->wgmr_mac2));
1856 else {
1857 /* msg.mac2 := MAC(Lm, msg_b) */
1858 wg_algo_mac(wgmr->wgmr_mac2, sizeof(wgmi->wgmi_mac2),
1859 wgp->wgp_latest_cookie, WG_COOKIE_LEN,
1860 (const uint8_t *)wgmr,
1861 offsetof(struct wg_msg_resp, wgmr_mac2),
1862 NULL, 0);
1863 }
1864
1865 memcpy(wgs->wgs_handshake_hash, hash, sizeof(hash));
1866 memcpy(wgs->wgs_chaining_key, ckey, sizeof(ckey));
1867 memcpy(wgs->wgs_ephemeral_key_pub, pubkey, sizeof(pubkey));
1868 memcpy(wgs->wgs_ephemeral_key_priv, privkey, sizeof(privkey));
1869 wgs->wgs_remote_index = wgmi->wgmi_sender;
1870 WG_DLOG("sender=%x\n", wgs->wgs_local_index);
1871 WG_DLOG("receiver=%x\n", wgs->wgs_remote_index);
1872 }
1873
1874 static void
1875 wg_swap_sessions(struct wg_peer *wgp)
1876 {
1877 struct wg_session *wgs, *wgs_prev;
1878
1879 KASSERT(mutex_owned(wgp->wgp_lock));
1880
1881 wgs = wgp->wgp_session_unstable;
1882 KASSERT(wgs->wgs_state == WGS_STATE_ESTABLISHED);
1883
1884 wgs_prev = wgp->wgp_session_stable;
1885 KASSERT(wgs_prev->wgs_state == WGS_STATE_ESTABLISHED ||
1886 wgs_prev->wgs_state == WGS_STATE_UNKNOWN);
1887 atomic_store_release(&wgp->wgp_session_stable, wgs);
1888 wgp->wgp_session_unstable = wgs_prev;
1889 }
1890
1891 static void __noinline
1892 wg_handle_msg_resp(struct wg_softc *wg, const struct wg_msg_resp *wgmr,
1893 const struct sockaddr *src)
1894 {
1895 uint8_t ckey[WG_CHAINING_KEY_LEN]; /* [W] 5.4.3: Cr */
1896 uint8_t hash[WG_HASH_LEN]; /* [W] 5.4.3: Kr */
1897 uint8_t cipher_key[WG_KDF_OUTPUT_LEN];
1898 struct wg_peer *wgp;
1899 struct wg_session *wgs;
1900 struct psref psref;
1901 int error;
1902 uint8_t mac1[WG_MAC_LEN];
1903 struct wg_session *wgs_prev;
1904 struct mbuf *m;
1905
1906 wg_algo_mac_mac1(mac1, sizeof(mac1),
1907 wg->wg_pubkey, sizeof(wg->wg_pubkey),
1908 (const uint8_t *)wgmr, offsetof(struct wg_msg_resp, wgmr_mac1));
1909
1910 /*
1911 * [W] 5.3: Denial of Service Mitigation & Cookies
1912 * "the responder, ..., must always reject messages with an invalid
1913 * msg.mac1"
1914 */
1915 if (!consttime_memequal(mac1, wgmr->wgmr_mac1, sizeof(mac1))) {
1916 WG_DLOG("mac1 is invalid\n");
1917 return;
1918 }
1919
1920 WG_TRACE("resp msg received");
1921 wgs = wg_lookup_session_by_index(wg, wgmr->wgmr_receiver, &psref);
1922 if (wgs == NULL) {
1923 WG_TRACE("No session found");
1924 return;
1925 }
1926
1927 wgp = wgs->wgs_peer;
1928
1929 mutex_enter(wgp->wgp_lock);
1930
1931 /* If we weren't waiting for a handshake response, drop it. */
1932 if (wgs->wgs_state != WGS_STATE_INIT_ACTIVE) {
1933 WG_TRACE("peer sent spurious handshake response, ignoring");
1934 goto out;
1935 }
1936
1937 if (__predict_false(wg_is_underload(wg, wgp, WG_MSG_TYPE_RESP))) {
1938 WG_TRACE("under load");
1939 /*
1940 * [W] 5.3: Denial of Service Mitigation & Cookies
1941 * "the responder, ..., and when under load may reject messages
1942 * with an invalid msg.mac2. If the responder receives a
1943 * message with a valid msg.mac1 yet with an invalid msg.mac2,
1944 * and is under load, it may respond with a cookie reply
1945 * message"
1946 */
1947 uint8_t zero[WG_MAC_LEN] = {0};
1948 if (consttime_memequal(wgmr->wgmr_mac2, zero, sizeof(zero))) {
1949 WG_TRACE("sending a cookie message: no cookie included");
1950 (void)wg_send_cookie_msg(wg, wgp, wgmr->wgmr_sender,
1951 wgmr->wgmr_mac1, src);
1952 goto out;
1953 }
1954 if (!wgp->wgp_last_sent_cookie_valid) {
1955 WG_TRACE("sending a cookie message: no cookie sent ever");
1956 (void)wg_send_cookie_msg(wg, wgp, wgmr->wgmr_sender,
1957 wgmr->wgmr_mac1, src);
1958 goto out;
1959 }
1960 uint8_t mac2[WG_MAC_LEN];
1961 wg_algo_mac(mac2, sizeof(mac2), wgp->wgp_last_sent_cookie,
1962 WG_COOKIE_LEN, (const uint8_t *)wgmr,
1963 offsetof(struct wg_msg_resp, wgmr_mac2), NULL, 0);
1964 if (!consttime_memequal(mac2, wgmr->wgmr_mac2, sizeof(mac2))) {
1965 WG_DLOG("mac2 is invalid\n");
1966 goto out;
1967 }
1968 WG_TRACE("under load, but continue to sending");
1969 }
1970
1971 memcpy(hash, wgs->wgs_handshake_hash, sizeof(hash));
1972 memcpy(ckey, wgs->wgs_chaining_key, sizeof(ckey));
1973
1974 /*
1975 * [W] 5.4.3 Second Message: Responder to Initiator
1976 * "When the initiator receives this message, it does the same
1977 * operations so that its final state variables are identical,
1978 * replacing the operands of the DH function to produce equivalent
1979 * values."
1980 * Note that the following comments of operations are just copies of
1981 * the initiator's ones.
1982 */
1983
1984 /* [N] 2.2: "e" */
1985 /* Cr := KDF1(Cr, Er^pub) */
1986 wg_algo_kdf(ckey, NULL, NULL, ckey, wgmr->wgmr_ephemeral,
1987 sizeof(wgmr->wgmr_ephemeral));
1988 /* Hr := HASH(Hr || msg.ephemeral) */
1989 wg_algo_hash(hash, wgmr->wgmr_ephemeral, sizeof(wgmr->wgmr_ephemeral));
1990
1991 WG_DUMP_HASH("ckey", ckey);
1992 WG_DUMP_HASH("hash", hash);
1993
1994 /* [N] 2.2: "ee" */
1995 /* Cr := KDF1(Cr, DH(Er^priv, Ei^pub)) */
1996 wg_algo_dh_kdf(ckey, NULL, wgs->wgs_ephemeral_key_priv,
1997 wgmr->wgmr_ephemeral);
1998
1999 /* [N] 2.2: "se" */
2000 /* Cr := KDF1(Cr, DH(Er^priv, Si^pub)) */
2001 wg_algo_dh_kdf(ckey, NULL, wg->wg_privkey, wgmr->wgmr_ephemeral);
2002
2003 /* [N] 9.2: "psk" */
2004 {
2005 uint8_t kdfout[WG_KDF_OUTPUT_LEN];
2006 /* Cr, r, k := KDF3(Cr, Q) */
2007 wg_algo_kdf(ckey, kdfout, cipher_key, ckey, wgp->wgp_psk,
2008 sizeof(wgp->wgp_psk));
2009 /* Hr := HASH(Hr || r) */
2010 wg_algo_hash(hash, kdfout, sizeof(kdfout));
2011 }
2012
2013 {
2014 uint8_t out[sizeof(wgmr->wgmr_empty)]; /* for safety */
2015 /* msg.empty := AEAD(k, 0, e, Hr) */
2016 error = wg_algo_aead_dec(out, 0, cipher_key, 0, wgmr->wgmr_empty,
2017 sizeof(wgmr->wgmr_empty), hash, sizeof(hash));
2018 WG_DUMP_HASH("wgmr_empty", wgmr->wgmr_empty);
2019 if (error != 0) {
2020 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2021 "%s: peer %s: wg_algo_aead_dec for empty message failed\n",
2022 if_name(&wg->wg_if), wgp->wgp_name);
2023 goto out;
2024 }
2025 /* Hr := HASH(Hr || msg.empty) */
2026 wg_algo_hash(hash, wgmr->wgmr_empty, sizeof(wgmr->wgmr_empty));
2027 }
2028
2029 memcpy(wgs->wgs_handshake_hash, hash, sizeof(wgs->wgs_handshake_hash));
2030 memcpy(wgs->wgs_chaining_key, ckey, sizeof(wgs->wgs_chaining_key));
2031 wgs->wgs_remote_index = wgmr->wgmr_sender;
2032 WG_DLOG("receiver=%x\n", wgs->wgs_remote_index);
2033
2034 KASSERT(wgs->wgs_state == WGS_STATE_INIT_ACTIVE);
2035 wgs->wgs_state = WGS_STATE_ESTABLISHED;
2036 wgs->wgs_time_established = time_uptime;
2037 wgs->wgs_time_last_data_sent = 0;
2038 wgs->wgs_is_initiator = true;
2039 wg_calculate_keys(wgs, true);
2040 wg_clear_states(wgs);
2041 WG_TRACE("WGS_STATE_ESTABLISHED");
2042
2043 callout_stop(&wgp->wgp_handshake_timeout_timer);
2044
2045 wg_swap_sessions(wgp);
2046 KASSERT(wgs == wgp->wgp_session_stable);
2047 wgs_prev = wgp->wgp_session_unstable;
2048 getnanotime(&wgp->wgp_last_handshake_time);
2049 wgp->wgp_handshake_start_time = 0;
2050 wgp->wgp_last_sent_mac1_valid = false;
2051 wgp->wgp_last_sent_cookie_valid = false;
2052
2053 wg_schedule_rekey_timer(wgp);
2054
2055 wg_update_endpoint_if_necessary(wgp, src);
2056
2057 /*
2058 * If we had a data packet queued up, send it; otherwise send a
2059 * keepalive message -- either way we have to send something
2060 * immediately or else the responder will never answer.
2061 */
2062 if ((m = atomic_swap_ptr(&wgp->wgp_pending, NULL)) != NULL) {
2063 kpreempt_disable();
2064 const uint32_t h = curcpu()->ci_index; // pktq_rps_hash(m)
2065 M_SETCTX(m, wgp);
2066 if (__predict_false(!pktq_enqueue(wg_pktq, m, h))) {
2067 WGLOG(LOG_ERR, "%s: pktq full, dropping\n",
2068 if_name(&wg->wg_if));
2069 m_freem(m);
2070 }
2071 kpreempt_enable();
2072 } else {
2073 wg_send_keepalive_msg(wgp, wgs);
2074 }
2075
2076 if (wgs_prev->wgs_state == WGS_STATE_ESTABLISHED) {
2077 /* Wait for wg_get_stable_session to drain. */
2078 pserialize_perform(wgp->wgp_psz);
2079
2080 /* Transition ESTABLISHED->DESTROYING. */
2081 wgs_prev->wgs_state = WGS_STATE_DESTROYING;
2082
2083 /* We can't destroy the old session immediately */
2084 wg_schedule_session_dtor_timer(wgp);
2085 } else {
2086 KASSERTMSG(wgs_prev->wgs_state == WGS_STATE_UNKNOWN,
2087 "state=%d", wgs_prev->wgs_state);
2088 }
2089
2090 out:
2091 mutex_exit(wgp->wgp_lock);
2092 wg_put_session(wgs, &psref);
2093 }
2094
2095 static int
2096 wg_send_handshake_msg_resp(struct wg_softc *wg, struct wg_peer *wgp,
2097 struct wg_session *wgs, const struct wg_msg_init *wgmi)
2098 {
2099 int error;
2100 struct mbuf *m;
2101 struct wg_msg_resp *wgmr;
2102
2103 KASSERT(mutex_owned(wgp->wgp_lock));
2104 KASSERT(wgs == wgp->wgp_session_unstable);
2105 KASSERT(wgs->wgs_state == WGS_STATE_INIT_PASSIVE);
2106
2107 m = m_gethdr(M_WAIT, MT_DATA);
2108 if (sizeof(*wgmr) > MHLEN) {
2109 m_clget(m, M_WAIT);
2110 CTASSERT(sizeof(*wgmr) <= MCLBYTES);
2111 }
2112 m->m_pkthdr.len = m->m_len = sizeof(*wgmr);
2113 wgmr = mtod(m, struct wg_msg_resp *);
2114 wg_fill_msg_resp(wg, wgp, wgs, wgmr, wgmi);
2115
2116 error = wg->wg_ops->send_hs_msg(wgp, m);
2117 if (error == 0)
2118 WG_TRACE("resp msg sent");
2119 return error;
2120 }
2121
2122 static struct wg_peer *
2123 wg_lookup_peer_by_pubkey(struct wg_softc *wg,
2124 const uint8_t pubkey[WG_STATIC_KEY_LEN], struct psref *psref)
2125 {
2126 struct wg_peer *wgp;
2127
2128 int s = pserialize_read_enter();
2129 wgp = thmap_get(wg->wg_peers_bypubkey, pubkey, WG_STATIC_KEY_LEN);
2130 if (wgp != NULL)
2131 wg_get_peer(wgp, psref);
2132 pserialize_read_exit(s);
2133
2134 return wgp;
2135 }
2136
2137 static void
2138 wg_fill_msg_cookie(struct wg_softc *wg, struct wg_peer *wgp,
2139 struct wg_msg_cookie *wgmc, const uint32_t sender,
2140 const uint8_t mac1[WG_MAC_LEN], const struct sockaddr *src)
2141 {
2142 uint8_t cookie[WG_COOKIE_LEN];
2143 uint8_t key[WG_HASH_LEN];
2144 uint8_t addr[sizeof(struct in6_addr)];
2145 size_t addrlen;
2146 uint16_t uh_sport; /* be */
2147
2148 KASSERT(mutex_owned(wgp->wgp_lock));
2149
2150 wgmc->wgmc_type = htole32(WG_MSG_TYPE_COOKIE);
2151 wgmc->wgmc_receiver = sender;
2152 cprng_fast(wgmc->wgmc_salt, sizeof(wgmc->wgmc_salt));
2153
2154 /*
2155 * [W] 5.4.7: Under Load: Cookie Reply Message
2156 * "The secret variable, Rm, changes every two minutes to a
2157 * random value"
2158 */
2159 if ((time_uptime - wgp->wgp_last_genrandval_time) > WG_RANDVAL_TIME) {
2160 wgp->wgp_randval = cprng_strong32();
2161 wgp->wgp_last_genrandval_time = time_uptime;
2162 }
2163
2164 switch (src->sa_family) {
2165 case AF_INET: {
2166 const struct sockaddr_in *sin = satocsin(src);
2167 addrlen = sizeof(sin->sin_addr);
2168 memcpy(addr, &sin->sin_addr, addrlen);
2169 uh_sport = sin->sin_port;
2170 break;
2171 }
2172 #ifdef INET6
2173 case AF_INET6: {
2174 const struct sockaddr_in6 *sin6 = satocsin6(src);
2175 addrlen = sizeof(sin6->sin6_addr);
2176 memcpy(addr, &sin6->sin6_addr, addrlen);
2177 uh_sport = sin6->sin6_port;
2178 break;
2179 }
2180 #endif
2181 default:
2182 panic("invalid af=%d", src->sa_family);
2183 }
2184
2185 wg_algo_mac(cookie, sizeof(cookie),
2186 (const uint8_t *)&wgp->wgp_randval, sizeof(wgp->wgp_randval),
2187 addr, addrlen, (const uint8_t *)&uh_sport, sizeof(uh_sport));
2188 wg_algo_mac_cookie(key, sizeof(key), wg->wg_pubkey,
2189 sizeof(wg->wg_pubkey));
2190 wg_algo_xaead_enc(wgmc->wgmc_cookie, sizeof(wgmc->wgmc_cookie), key,
2191 cookie, sizeof(cookie), mac1, WG_MAC_LEN, wgmc->wgmc_salt);
2192
2193 /* Need to store to calculate mac2 */
2194 memcpy(wgp->wgp_last_sent_cookie, cookie, sizeof(cookie));
2195 wgp->wgp_last_sent_cookie_valid = true;
2196 }
2197
2198 static int
2199 wg_send_cookie_msg(struct wg_softc *wg, struct wg_peer *wgp,
2200 const uint32_t sender, const uint8_t mac1[WG_MAC_LEN],
2201 const struct sockaddr *src)
2202 {
2203 int error;
2204 struct mbuf *m;
2205 struct wg_msg_cookie *wgmc;
2206
2207 KASSERT(mutex_owned(wgp->wgp_lock));
2208
2209 m = m_gethdr(M_WAIT, MT_DATA);
2210 if (sizeof(*wgmc) > MHLEN) {
2211 m_clget(m, M_WAIT);
2212 CTASSERT(sizeof(*wgmc) <= MCLBYTES);
2213 }
2214 m->m_pkthdr.len = m->m_len = sizeof(*wgmc);
2215 wgmc = mtod(m, struct wg_msg_cookie *);
2216 wg_fill_msg_cookie(wg, wgp, wgmc, sender, mac1, src);
2217
2218 error = wg->wg_ops->send_hs_msg(wgp, m);
2219 if (error == 0)
2220 WG_TRACE("cookie msg sent");
2221 return error;
2222 }
2223
2224 static bool
2225 wg_is_underload(struct wg_softc *wg, struct wg_peer *wgp, int msgtype)
2226 {
2227 #ifdef WG_DEBUG_PARAMS
2228 if (wg_force_underload)
2229 return true;
2230 #endif
2231
2232 /*
2233 * XXX we don't have a means of a load estimation. The purpose of
2234 * the mechanism is a DoS mitigation, so we consider frequent handshake
2235 * messages as (a kind of) load; if a message of the same type comes
2236 * to a peer within 1 second, we consider we are under load.
2237 */
2238 time_t last = wgp->wgp_last_msg_received_time[msgtype];
2239 wgp->wgp_last_msg_received_time[msgtype] = time_uptime;
2240 return (time_uptime - last) == 0;
2241 }
2242
2243 static void
2244 wg_calculate_keys(struct wg_session *wgs, const bool initiator)
2245 {
2246
2247 KASSERT(mutex_owned(wgs->wgs_peer->wgp_lock));
2248
2249 /*
2250 * [W] 5.4.5: Ti^send = Tr^recv, Ti^recv = Tr^send := KDF2(Ci = Cr, e)
2251 */
2252 if (initiator) {
2253 wg_algo_kdf(wgs->wgs_tkey_send, wgs->wgs_tkey_recv, NULL,
2254 wgs->wgs_chaining_key, NULL, 0);
2255 } else {
2256 wg_algo_kdf(wgs->wgs_tkey_recv, wgs->wgs_tkey_send, NULL,
2257 wgs->wgs_chaining_key, NULL, 0);
2258 }
2259 WG_DUMP_HASH("wgs_tkey_send", wgs->wgs_tkey_send);
2260 WG_DUMP_HASH("wgs_tkey_recv", wgs->wgs_tkey_recv);
2261 }
2262
2263 static uint64_t
2264 wg_session_get_send_counter(struct wg_session *wgs)
2265 {
2266 #ifdef __HAVE_ATOMIC64_LOADSTORE
2267 return atomic_load_relaxed(&wgs->wgs_send_counter);
2268 #else
2269 uint64_t send_counter;
2270
2271 mutex_enter(&wgs->wgs_send_counter_lock);
2272 send_counter = wgs->wgs_send_counter;
2273 mutex_exit(&wgs->wgs_send_counter_lock);
2274
2275 return send_counter;
2276 #endif
2277 }
2278
2279 static uint64_t
2280 wg_session_inc_send_counter(struct wg_session *wgs)
2281 {
2282 #ifdef __HAVE_ATOMIC64_LOADSTORE
2283 return atomic_inc_64_nv(&wgs->wgs_send_counter) - 1;
2284 #else
2285 uint64_t send_counter;
2286
2287 mutex_enter(&wgs->wgs_send_counter_lock);
2288 send_counter = wgs->wgs_send_counter++;
2289 mutex_exit(&wgs->wgs_send_counter_lock);
2290
2291 return send_counter;
2292 #endif
2293 }
2294
2295 static void
2296 wg_clear_states(struct wg_session *wgs)
2297 {
2298
2299 KASSERT(mutex_owned(wgs->wgs_peer->wgp_lock));
2300
2301 wgs->wgs_send_counter = 0;
2302 sliwin_reset(&wgs->wgs_recvwin->window);
2303
2304 #define wgs_clear(v) explicit_memset(wgs->wgs_##v, 0, sizeof(wgs->wgs_##v))
2305 wgs_clear(handshake_hash);
2306 wgs_clear(chaining_key);
2307 wgs_clear(ephemeral_key_pub);
2308 wgs_clear(ephemeral_key_priv);
2309 wgs_clear(ephemeral_key_peer);
2310 #undef wgs_clear
2311 }
2312
2313 static struct wg_session *
2314 wg_lookup_session_by_index(struct wg_softc *wg, const uint32_t index,
2315 struct psref *psref)
2316 {
2317 struct wg_session *wgs;
2318
2319 int s = pserialize_read_enter();
2320 wgs = thmap_get(wg->wg_sessions_byindex, &index, sizeof index);
2321 if (wgs != NULL) {
2322 KASSERT(atomic_load_relaxed(&wgs->wgs_state) !=
2323 WGS_STATE_UNKNOWN);
2324 psref_acquire(psref, &wgs->wgs_psref, wg_psref_class);
2325 }
2326 pserialize_read_exit(s);
2327
2328 return wgs;
2329 }
2330
2331 static void
2332 wg_schedule_rekey_timer(struct wg_peer *wgp)
2333 {
2334 int timeout = MIN(wg_rekey_after_time, (unsigned)(INT_MAX / hz));
2335
2336 callout_schedule(&wgp->wgp_rekey_timer, timeout * hz);
2337 }
2338
2339 static void
2340 wg_send_keepalive_msg(struct wg_peer *wgp, struct wg_session *wgs)
2341 {
2342 struct mbuf *m;
2343
2344 /*
2345 * [W] 6.5 Passive Keepalive
2346 * "A keepalive message is simply a transport data message with
2347 * a zero-length encapsulated encrypted inner-packet."
2348 */
2349 WG_TRACE("");
2350 m = m_gethdr(M_WAIT, MT_DATA);
2351 wg_send_data_msg(wgp, wgs, m);
2352 }
2353
2354 static bool
2355 wg_need_to_send_init_message(struct wg_session *wgs)
2356 {
2357 /*
2358 * [W] 6.2 Transport Message Limits
2359 * "if a peer is the initiator of a current secure session,
2360 * WireGuard will send a handshake initiation message to begin
2361 * a new secure session ... if after receiving a transport data
2362 * message, the current secure session is (REJECT-AFTER-TIME
2363 * KEEPALIVE-TIMEOUT REKEY-TIMEOUT) seconds old and it has
2364 * not yet acted upon this event."
2365 */
2366 return wgs->wgs_is_initiator && wgs->wgs_time_last_data_sent == 0 &&
2367 (time_uptime - wgs->wgs_time_established) >=
2368 (wg_reject_after_time - wg_keepalive_timeout - wg_rekey_timeout);
2369 }
2370
2371 static void
2372 wg_schedule_peer_task(struct wg_peer *wgp, unsigned int task)
2373 {
2374
2375 mutex_enter(wgp->wgp_intr_lock);
2376 WG_DLOG("tasks=%d, task=%d\n", wgp->wgp_tasks, task);
2377 if (wgp->wgp_tasks == 0)
2378 /*
2379 * XXX If the current CPU is already loaded -- e.g., if
2380 * there's already a bunch of handshakes queued up --
2381 * consider tossing this over to another CPU to
2382 * distribute the load.
2383 */
2384 workqueue_enqueue(wg_wq, &wgp->wgp_work, NULL);
2385 wgp->wgp_tasks |= task;
2386 mutex_exit(wgp->wgp_intr_lock);
2387 }
2388
2389 static void
2390 wg_change_endpoint(struct wg_peer *wgp, const struct sockaddr *new)
2391 {
2392 struct wg_sockaddr *wgsa_prev;
2393
2394 WG_TRACE("Changing endpoint");
2395
2396 memcpy(wgp->wgp_endpoint0, new, new->sa_len);
2397 wgsa_prev = wgp->wgp_endpoint;
2398 atomic_store_release(&wgp->wgp_endpoint, wgp->wgp_endpoint0);
2399 wgp->wgp_endpoint0 = wgsa_prev;
2400 atomic_store_release(&wgp->wgp_endpoint_available, true);
2401
2402 wg_schedule_peer_task(wgp, WGP_TASK_ENDPOINT_CHANGED);
2403 }
2404
2405 static bool
2406 wg_validate_inner_packet(const char *packet, size_t decrypted_len, int *af)
2407 {
2408 uint16_t packet_len;
2409 const struct ip *ip;
2410
2411 if (__predict_false(decrypted_len < sizeof(*ip))) {
2412 WG_DLOG("decrypted_len=%zu < %zu\n", decrypted_len,
2413 sizeof(*ip));
2414 return false;
2415 }
2416
2417 ip = (const struct ip *)packet;
2418 if (ip->ip_v == 4)
2419 *af = AF_INET;
2420 else if (ip->ip_v == 6)
2421 *af = AF_INET6;
2422 else {
2423 WG_DLOG("ip_v=%d\n", ip->ip_v);
2424 return false;
2425 }
2426
2427 WG_DLOG("af=%d\n", *af);
2428
2429 switch (*af) {
2430 #ifdef INET
2431 case AF_INET:
2432 packet_len = ntohs(ip->ip_len);
2433 break;
2434 #endif
2435 #ifdef INET6
2436 case AF_INET6: {
2437 const struct ip6_hdr *ip6;
2438
2439 if (__predict_false(decrypted_len < sizeof(*ip6))) {
2440 WG_DLOG("decrypted_len=%zu < %zu\n", decrypted_len,
2441 sizeof(*ip6));
2442 return false;
2443 }
2444
2445 ip6 = (const struct ip6_hdr *)packet;
2446 packet_len = sizeof(*ip6) + ntohs(ip6->ip6_plen);
2447 break;
2448 }
2449 #endif
2450 default:
2451 return false;
2452 }
2453
2454 if (packet_len > decrypted_len) {
2455 WG_DLOG("packet_len %u > decrypted_len %zu\n", packet_len,
2456 decrypted_len);
2457 return false;
2458 }
2459
2460 return true;
2461 }
2462
2463 static bool
2464 wg_validate_route(struct wg_softc *wg, struct wg_peer *wgp_expected,
2465 int af, char *packet)
2466 {
2467 struct sockaddr_storage ss;
2468 struct sockaddr *sa;
2469 struct psref psref;
2470 struct wg_peer *wgp;
2471 bool ok;
2472
2473 /*
2474 * II CRYPTOKEY ROUTING
2475 * "it will only accept it if its source IP resolves in the
2476 * table to the public key used in the secure session for
2477 * decrypting it."
2478 */
2479
2480 if (af == AF_INET) {
2481 const struct ip *ip = (const struct ip *)packet;
2482 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
2483 sockaddr_in_init(sin, &ip->ip_src, 0);
2484 sa = sintosa(sin);
2485 #ifdef INET6
2486 } else {
2487 const struct ip6_hdr *ip6 = (const struct ip6_hdr *)packet;
2488 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
2489 sockaddr_in6_init(sin6, &ip6->ip6_src, 0, 0, 0);
2490 sa = sin6tosa(sin6);
2491 #endif
2492 }
2493
2494 wgp = wg_pick_peer_by_sa(wg, sa, &psref);
2495 ok = (wgp == wgp_expected);
2496 if (wgp != NULL)
2497 wg_put_peer(wgp, &psref);
2498
2499 return ok;
2500 }
2501
2502 static void
2503 wg_session_dtor_timer(void *arg)
2504 {
2505 struct wg_peer *wgp = arg;
2506
2507 WG_TRACE("enter");
2508
2509 wg_schedule_peer_task(wgp, WGP_TASK_DESTROY_PREV_SESSION);
2510 }
2511
2512 static void
2513 wg_schedule_session_dtor_timer(struct wg_peer *wgp)
2514 {
2515
2516 /* 1 second grace period */
2517 callout_schedule(&wgp->wgp_session_dtor_timer, hz);
2518 }
2519
2520 static bool
2521 sockaddr_port_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
2522 {
2523 if (sa1->sa_family != sa2->sa_family)
2524 return false;
2525
2526 switch (sa1->sa_family) {
2527 #ifdef INET
2528 case AF_INET:
2529 return satocsin(sa1)->sin_port == satocsin(sa2)->sin_port;
2530 #endif
2531 #ifdef INET6
2532 case AF_INET6:
2533 return satocsin6(sa1)->sin6_port == satocsin6(sa2)->sin6_port;
2534 #endif
2535 default:
2536 return false;
2537 }
2538 }
2539
2540 static void
2541 wg_update_endpoint_if_necessary(struct wg_peer *wgp,
2542 const struct sockaddr *src)
2543 {
2544 struct wg_sockaddr *wgsa;
2545 struct psref psref;
2546
2547 wgsa = wg_get_endpoint_sa(wgp, &psref);
2548
2549 #ifdef WG_DEBUG_LOG
2550 char oldaddr[128], newaddr[128];
2551 sockaddr_format(wgsatosa(wgsa), oldaddr, sizeof(oldaddr));
2552 sockaddr_format(src, newaddr, sizeof(newaddr));
2553 WG_DLOG("old=%s, new=%s\n", oldaddr, newaddr);
2554 #endif
2555
2556 /*
2557 * III: "Since the packet has authenticated correctly, the source IP of
2558 * the outer UDP/IP packet is used to update the endpoint for peer..."
2559 */
2560 if (__predict_false(sockaddr_cmp(src, wgsatosa(wgsa)) != 0 ||
2561 !sockaddr_port_match(src, wgsatosa(wgsa)))) {
2562 /* XXX We can't change the endpoint twice in a short period */
2563 if (atomic_swap_uint(&wgp->wgp_endpoint_changing, 1) == 0) {
2564 wg_change_endpoint(wgp, src);
2565 }
2566 }
2567
2568 wg_put_sa(wgp, wgsa, &psref);
2569 }
2570
2571 static void __noinline
2572 wg_handle_msg_data(struct wg_softc *wg, struct mbuf *m,
2573 const struct sockaddr *src)
2574 {
2575 struct wg_msg_data *wgmd;
2576 char *encrypted_buf = NULL, *decrypted_buf;
2577 size_t encrypted_len, decrypted_len;
2578 struct wg_session *wgs;
2579 struct wg_peer *wgp;
2580 int state;
2581 size_t mlen;
2582 struct psref psref;
2583 int error, af;
2584 bool success, free_encrypted_buf = false, ok;
2585 struct mbuf *n;
2586
2587 KASSERT(m->m_len >= sizeof(struct wg_msg_data));
2588 wgmd = mtod(m, struct wg_msg_data *);
2589
2590 KASSERT(wgmd->wgmd_type == htole32(WG_MSG_TYPE_DATA));
2591 WG_TRACE("data");
2592
2593 /* Find the putative session, or drop. */
2594 wgs = wg_lookup_session_by_index(wg, wgmd->wgmd_receiver, &psref);
2595 if (wgs == NULL) {
2596 WG_TRACE("No session found");
2597 m_freem(m);
2598 return;
2599 }
2600
2601 /*
2602 * We are only ready to handle data when in INIT_PASSIVE,
2603 * ESTABLISHED, or DESTROYING. All transitions out of that
2604 * state dissociate the session index and drain psrefs.
2605 */
2606 state = atomic_load_relaxed(&wgs->wgs_state);
2607 switch (state) {
2608 case WGS_STATE_UNKNOWN:
2609 panic("wg session %p in unknown state has session index %u",
2610 wgs, wgmd->wgmd_receiver);
2611 case WGS_STATE_INIT_ACTIVE:
2612 WG_TRACE("not yet ready for data");
2613 goto out;
2614 case WGS_STATE_INIT_PASSIVE:
2615 case WGS_STATE_ESTABLISHED:
2616 case WGS_STATE_DESTROYING:
2617 break;
2618 }
2619
2620 /*
2621 * Get the peer, for rate-limited logs (XXX MPSAFE, dtrace) and
2622 * to update the endpoint if authentication succeeds.
2623 */
2624 wgp = wgs->wgs_peer;
2625
2626 /*
2627 * Reject outrageously wrong sequence numbers before doing any
2628 * crypto work or taking any locks.
2629 */
2630 error = sliwin_check_fast(&wgs->wgs_recvwin->window,
2631 le64toh(wgmd->wgmd_counter));
2632 if (error) {
2633 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2634 "%s: peer %s: out-of-window packet: %"PRIu64"\n",
2635 if_name(&wg->wg_if), wgp->wgp_name,
2636 le64toh(wgmd->wgmd_counter));
2637 goto out;
2638 }
2639
2640 /* Ensure the payload and authenticator are contiguous. */
2641 mlen = m_length(m);
2642 encrypted_len = mlen - sizeof(*wgmd);
2643 if (encrypted_len < WG_AUTHTAG_LEN) {
2644 WG_DLOG("Short encrypted_len: %zu\n", encrypted_len);
2645 goto out;
2646 }
2647 success = m_ensure_contig(&m, sizeof(*wgmd) + encrypted_len);
2648 if (success) {
2649 encrypted_buf = mtod(m, char *) + sizeof(*wgmd);
2650 } else {
2651 encrypted_buf = kmem_intr_alloc(encrypted_len, KM_NOSLEEP);
2652 if (encrypted_buf == NULL) {
2653 WG_DLOG("failed to allocate encrypted_buf\n");
2654 goto out;
2655 }
2656 m_copydata(m, sizeof(*wgmd), encrypted_len, encrypted_buf);
2657 free_encrypted_buf = true;
2658 }
2659 /* m_ensure_contig may change m regardless of its result */
2660 KASSERT(m->m_len >= sizeof(*wgmd));
2661 wgmd = mtod(m, struct wg_msg_data *);
2662
2663 /*
2664 * Get a buffer for the plaintext. Add WG_AUTHTAG_LEN to avoid
2665 * a zero-length buffer (XXX). Drop if plaintext is longer
2666 * than MCLBYTES (XXX).
2667 */
2668 decrypted_len = encrypted_len - WG_AUTHTAG_LEN;
2669 if (decrypted_len > MCLBYTES) {
2670 /* FIXME handle larger data than MCLBYTES */
2671 WG_DLOG("couldn't handle larger data than MCLBYTES\n");
2672 goto out;
2673 }
2674 n = wg_get_mbuf(0, decrypted_len + WG_AUTHTAG_LEN);
2675 if (n == NULL) {
2676 WG_DLOG("wg_get_mbuf failed\n");
2677 goto out;
2678 }
2679 decrypted_buf = mtod(n, char *);
2680
2681 /* Decrypt and verify the packet. */
2682 WG_DLOG("mlen=%zu, encrypted_len=%zu\n", mlen, encrypted_len);
2683 error = wg_algo_aead_dec(decrypted_buf,
2684 encrypted_len - WG_AUTHTAG_LEN /* can be 0 */,
2685 wgs->wgs_tkey_recv, le64toh(wgmd->wgmd_counter), encrypted_buf,
2686 encrypted_len, NULL, 0);
2687 if (error != 0) {
2688 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2689 "%s: peer %s: failed to wg_algo_aead_dec\n",
2690 if_name(&wg->wg_if), wgp->wgp_name);
2691 m_freem(n);
2692 goto out;
2693 }
2694 WG_DLOG("outsize=%u\n", (u_int)decrypted_len);
2695
2696 /* Packet is genuine. Reject it if a replay or just too old. */
2697 mutex_enter(&wgs->wgs_recvwin->lock);
2698 error = sliwin_update(&wgs->wgs_recvwin->window,
2699 le64toh(wgmd->wgmd_counter));
2700 mutex_exit(&wgs->wgs_recvwin->lock);
2701 if (error) {
2702 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2703 "%s: peer %s: replay or out-of-window packet: %"PRIu64"\n",
2704 if_name(&wg->wg_if), wgp->wgp_name,
2705 le64toh(wgmd->wgmd_counter));
2706 m_freem(n);
2707 goto out;
2708 }
2709
2710 #ifdef WG_DEBUG_PACKET
2711 if (wg_debug & WG_DEBUG_FLAGS_PACKET) {
2712 hexdump(aprint_debug, "wgmd", wgmd, sizeof(*wgmd));
2713 hexdump(aprint_debug, "decrypted_buf", decrypted_buf,
2714 decrypted_len);
2715 }
2716 #endif
2717 /* We're done with m now; free it and chuck the pointers. */
2718 m_freem(m);
2719 m = NULL;
2720 wgmd = NULL;
2721
2722 /*
2723 * Validate the encapsulated packet header and get the address
2724 * family, or drop.
2725 */
2726 ok = wg_validate_inner_packet(decrypted_buf, decrypted_len, &af);
2727 if (!ok) {
2728 m_freem(n);
2729 goto out;
2730 }
2731
2732 /*
2733 * The packet is genuine. Update the peer's endpoint if the
2734 * source address changed.
2735 *
2736 * XXX How to prevent DoS by replaying genuine packets from the
2737 * wrong source address?
2738 */
2739 wg_update_endpoint_if_necessary(wgp, src);
2740
2741 /* Submit it into our network stack if routable. */
2742 ok = wg_validate_route(wg, wgp, af, decrypted_buf);
2743 if (ok) {
2744 wg->wg_ops->input(&wg->wg_if, n, af);
2745 } else {
2746 char addrstr[INET6_ADDRSTRLEN];
2747 memset(addrstr, 0, sizeof(addrstr));
2748 if (af == AF_INET) {
2749 const struct ip *ip = (const struct ip *)decrypted_buf;
2750 IN_PRINT(addrstr, &ip->ip_src);
2751 #ifdef INET6
2752 } else if (af == AF_INET6) {
2753 const struct ip6_hdr *ip6 =
2754 (const struct ip6_hdr *)decrypted_buf;
2755 IN6_PRINT(addrstr, &ip6->ip6_src);
2756 #endif
2757 }
2758 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2759 "%s: peer %s: invalid source address (%s)\n",
2760 if_name(&wg->wg_if), wgp->wgp_name, addrstr);
2761 m_freem(n);
2762 /*
2763 * The inner address is invalid however the session is valid
2764 * so continue the session processing below.
2765 */
2766 }
2767 n = NULL;
2768
2769 /* Update the state machine if necessary. */
2770 if (__predict_false(state == WGS_STATE_INIT_PASSIVE)) {
2771 /*
2772 * We were waiting for the initiator to send their
2773 * first data transport message, and that has happened.
2774 * Schedule a task to establish this session.
2775 */
2776 wg_schedule_peer_task(wgp, WGP_TASK_ESTABLISH_SESSION);
2777 } else {
2778 if (__predict_false(wg_need_to_send_init_message(wgs))) {
2779 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
2780 }
2781 /*
2782 * [W] 6.5 Passive Keepalive
2783 * "If a peer has received a validly-authenticated transport
2784 * data message (section 5.4.6), but does not have any packets
2785 * itself to send back for KEEPALIVE-TIMEOUT seconds, it sends
2786 * a keepalive message."
2787 */
2788 WG_DLOG("time_uptime=%ju wgs_time_last_data_sent=%ju\n",
2789 (uintmax_t)time_uptime,
2790 (uintmax_t)wgs->wgs_time_last_data_sent);
2791 if ((time_uptime - wgs->wgs_time_last_data_sent) >=
2792 wg_keepalive_timeout) {
2793 WG_TRACE("Schedule sending keepalive message");
2794 /*
2795 * We can't send a keepalive message here to avoid
2796 * a deadlock; we already hold the solock of a socket
2797 * that is used to send the message.
2798 */
2799 wg_schedule_peer_task(wgp,
2800 WGP_TASK_SEND_KEEPALIVE_MESSAGE);
2801 }
2802 }
2803 out:
2804 wg_put_session(wgs, &psref);
2805 m_freem(m);
2806 if (free_encrypted_buf)
2807 kmem_intr_free(encrypted_buf, encrypted_len);
2808 }
2809
2810 static void __noinline
2811 wg_handle_msg_cookie(struct wg_softc *wg, const struct wg_msg_cookie *wgmc)
2812 {
2813 struct wg_session *wgs;
2814 struct wg_peer *wgp;
2815 struct psref psref;
2816 int error;
2817 uint8_t key[WG_HASH_LEN];
2818 uint8_t cookie[WG_COOKIE_LEN];
2819
2820 WG_TRACE("cookie msg received");
2821
2822 /* Find the putative session. */
2823 wgs = wg_lookup_session_by_index(wg, wgmc->wgmc_receiver, &psref);
2824 if (wgs == NULL) {
2825 WG_TRACE("No session found");
2826 return;
2827 }
2828
2829 /* Lock the peer so we can update the cookie state. */
2830 wgp = wgs->wgs_peer;
2831 mutex_enter(wgp->wgp_lock);
2832
2833 if (!wgp->wgp_last_sent_mac1_valid) {
2834 WG_TRACE("No valid mac1 sent (or expired)");
2835 goto out;
2836 }
2837
2838 /* Decrypt the cookie and store it for later handshake retry. */
2839 wg_algo_mac_cookie(key, sizeof(key), wgp->wgp_pubkey,
2840 sizeof(wgp->wgp_pubkey));
2841 error = wg_algo_xaead_dec(cookie, sizeof(cookie), key,
2842 wgmc->wgmc_cookie, sizeof(wgmc->wgmc_cookie),
2843 wgp->wgp_last_sent_mac1, sizeof(wgp->wgp_last_sent_mac1),
2844 wgmc->wgmc_salt);
2845 if (error != 0) {
2846 WG_LOG_RATECHECK(&wgp->wgp_ppsratecheck, LOG_DEBUG,
2847 "%s: peer %s: wg_algo_aead_dec for cookie failed: "
2848 "error=%d\n", if_name(&wg->wg_if), wgp->wgp_name, error);
2849 goto out;
2850 }
2851 /*
2852 * [W] 6.6: Interaction with Cookie Reply System
2853 * "it should simply store the decrypted cookie value from the cookie
2854 * reply message, and wait for the expiration of the REKEY-TIMEOUT
2855 * timer for retrying a handshake initiation message."
2856 */
2857 wgp->wgp_latest_cookie_time = time_uptime;
2858 memcpy(wgp->wgp_latest_cookie, cookie, sizeof(wgp->wgp_latest_cookie));
2859 out:
2860 mutex_exit(wgp->wgp_lock);
2861 wg_put_session(wgs, &psref);
2862 }
2863
2864 static struct mbuf *
2865 wg_validate_msg_header(struct wg_softc *wg, struct mbuf *m)
2866 {
2867 struct wg_msg wgm;
2868 size_t mbuflen;
2869 size_t msglen;
2870
2871 /*
2872 * Get the mbuf chain length. It is already guaranteed, by
2873 * wg_overudp_cb, to be large enough for a struct wg_msg.
2874 */
2875 mbuflen = m_length(m);
2876 KASSERT(mbuflen >= sizeof(struct wg_msg));
2877
2878 /*
2879 * Copy the message header (32-bit message type) out -- we'll
2880 * worry about contiguity and alignment later.
2881 */
2882 m_copydata(m, 0, sizeof(wgm), &wgm);
2883 switch (le32toh(wgm.wgm_type)) {
2884 case WG_MSG_TYPE_INIT:
2885 msglen = sizeof(struct wg_msg_init);
2886 break;
2887 case WG_MSG_TYPE_RESP:
2888 msglen = sizeof(struct wg_msg_resp);
2889 break;
2890 case WG_MSG_TYPE_COOKIE:
2891 msglen = sizeof(struct wg_msg_cookie);
2892 break;
2893 case WG_MSG_TYPE_DATA:
2894 msglen = sizeof(struct wg_msg_data);
2895 break;
2896 default:
2897 WG_LOG_RATECHECK(&wg->wg_ppsratecheck, LOG_DEBUG,
2898 "%s: Unexpected msg type: %u\n", if_name(&wg->wg_if),
2899 le32toh(wgm.wgm_type));
2900 goto error;
2901 }
2902
2903 /* Verify the mbuf chain is long enough for this type of message. */
2904 if (__predict_false(mbuflen < msglen)) {
2905 WG_DLOG("Invalid msg size: mbuflen=%zu type=%u\n", mbuflen,
2906 le32toh(wgm.wgm_type));
2907 goto error;
2908 }
2909
2910 /* Make the message header contiguous if necessary. */
2911 if (__predict_false(m->m_len < msglen)) {
2912 m = m_pullup(m, msglen);
2913 if (m == NULL)
2914 return NULL;
2915 }
2916
2917 return m;
2918
2919 error:
2920 m_freem(m);
2921 return NULL;
2922 }
2923
2924 static void
2925 wg_handle_packet(struct wg_softc *wg, struct mbuf *m,
2926 const struct sockaddr *src)
2927 {
2928 struct wg_msg *wgm;
2929
2930 KASSERT(curlwp->l_pflag & LP_BOUND);
2931
2932 m = wg_validate_msg_header(wg, m);
2933 if (__predict_false(m == NULL))
2934 return;
2935
2936 KASSERT(m->m_len >= sizeof(struct wg_msg));
2937 wgm = mtod(m, struct wg_msg *);
2938 switch (le32toh(wgm->wgm_type)) {
2939 case WG_MSG_TYPE_INIT:
2940 wg_handle_msg_init(wg, (struct wg_msg_init *)wgm, src);
2941 break;
2942 case WG_MSG_TYPE_RESP:
2943 wg_handle_msg_resp(wg, (struct wg_msg_resp *)wgm, src);
2944 break;
2945 case WG_MSG_TYPE_COOKIE:
2946 wg_handle_msg_cookie(wg, (struct wg_msg_cookie *)wgm);
2947 break;
2948 case WG_MSG_TYPE_DATA:
2949 wg_handle_msg_data(wg, m, src);
2950 /* wg_handle_msg_data frees m for us */
2951 return;
2952 default:
2953 panic("invalid message type: %d", le32toh(wgm->wgm_type));
2954 }
2955
2956 m_freem(m);
2957 }
2958
2959 static void
2960 wg_receive_packets(struct wg_softc *wg, const int af)
2961 {
2962
2963 for (;;) {
2964 int error, flags;
2965 struct socket *so;
2966 struct mbuf *m = NULL;
2967 struct uio dummy_uio;
2968 struct mbuf *paddr = NULL;
2969 struct sockaddr *src;
2970
2971 so = wg_get_so_by_af(wg, af);
2972 flags = MSG_DONTWAIT;
2973 dummy_uio.uio_resid = 1000000000;
2974
2975 error = so->so_receive(so, &paddr, &dummy_uio, &m, NULL,
2976 &flags);
2977 if (error || m == NULL) {
2978 //if (error == EWOULDBLOCK)
2979 return;
2980 }
2981
2982 KASSERT(paddr != NULL);
2983 KASSERT(paddr->m_len >= sizeof(struct sockaddr));
2984 src = mtod(paddr, struct sockaddr *);
2985
2986 wg_handle_packet(wg, m, src);
2987 }
2988 }
2989
2990 static void
2991 wg_get_peer(struct wg_peer *wgp, struct psref *psref)
2992 {
2993
2994 psref_acquire(psref, &wgp->wgp_psref, wg_psref_class);
2995 }
2996
2997 static void
2998 wg_put_peer(struct wg_peer *wgp, struct psref *psref)
2999 {
3000
3001 psref_release(psref, &wgp->wgp_psref, wg_psref_class);
3002 }
3003
3004 static void
3005 wg_task_send_init_message(struct wg_softc *wg, struct wg_peer *wgp)
3006 {
3007 struct wg_session *wgs;
3008
3009 WG_TRACE("WGP_TASK_SEND_INIT_MESSAGE");
3010
3011 KASSERT(mutex_owned(wgp->wgp_lock));
3012
3013 if (!atomic_load_acquire(&wgp->wgp_endpoint_available)) {
3014 WGLOG(LOG_DEBUG, "%s: No endpoint available\n",
3015 if_name(&wg->wg_if));
3016 /* XXX should do something? */
3017 return;
3018 }
3019
3020 wgs = wgp->wgp_session_stable;
3021 if (wgs->wgs_state == WGS_STATE_UNKNOWN) {
3022 /* XXX What if the unstable session is already INIT_ACTIVE? */
3023 wg_send_handshake_msg_init(wg, wgp);
3024 } else {
3025 /* rekey */
3026 wgs = wgp->wgp_session_unstable;
3027 if (wgs->wgs_state != WGS_STATE_INIT_ACTIVE)
3028 wg_send_handshake_msg_init(wg, wgp);
3029 }
3030 }
3031
3032 static void
3033 wg_task_retry_handshake(struct wg_softc *wg, struct wg_peer *wgp)
3034 {
3035 struct wg_session *wgs;
3036
3037 WG_TRACE("WGP_TASK_RETRY_HANDSHAKE");
3038
3039 KASSERT(mutex_owned(wgp->wgp_lock));
3040 KASSERT(wgp->wgp_handshake_start_time != 0);
3041
3042 wgs = wgp->wgp_session_unstable;
3043 if (wgs->wgs_state != WGS_STATE_INIT_ACTIVE)
3044 return;
3045
3046 /*
3047 * XXX no real need to assign a new index here, but we do need
3048 * to transition to UNKNOWN temporarily
3049 */
3050 wg_put_session_index(wg, wgs);
3051
3052 /* [W] 6.4 Handshake Initiation Retransmission */
3053 if ((time_uptime - wgp->wgp_handshake_start_time) >
3054 wg_rekey_attempt_time) {
3055 /* Give up handshaking */
3056 wgp->wgp_handshake_start_time = 0;
3057 WG_TRACE("give up");
3058
3059 /*
3060 * If a new data packet comes, handshaking will be retried
3061 * and a new session would be established at that time,
3062 * however we don't want to send pending packets then.
3063 */
3064 wg_purge_pending_packets(wgp);
3065 return;
3066 }
3067
3068 wg_task_send_init_message(wg, wgp);
3069 }
3070
3071 static void
3072 wg_task_establish_session(struct wg_softc *wg, struct wg_peer *wgp)
3073 {
3074 struct wg_session *wgs, *wgs_prev;
3075 struct mbuf *m;
3076
3077 KASSERT(mutex_owned(wgp->wgp_lock));
3078
3079 wgs = wgp->wgp_session_unstable;
3080 if (wgs->wgs_state != WGS_STATE_INIT_PASSIVE)
3081 /* XXX Can this happen? */
3082 return;
3083
3084 wgs->wgs_state = WGS_STATE_ESTABLISHED;
3085 wgs->wgs_time_established = time_uptime;
3086 wgs->wgs_time_last_data_sent = 0;
3087 wgs->wgs_is_initiator = false;
3088 WG_TRACE("WGS_STATE_ESTABLISHED");
3089
3090 wg_swap_sessions(wgp);
3091 KASSERT(wgs == wgp->wgp_session_stable);
3092 wgs_prev = wgp->wgp_session_unstable;
3093 getnanotime(&wgp->wgp_last_handshake_time);
3094 wgp->wgp_handshake_start_time = 0;
3095 wgp->wgp_last_sent_mac1_valid = false;
3096 wgp->wgp_last_sent_cookie_valid = false;
3097
3098 /* If we had a data packet queued up, send it. */
3099 if ((m = atomic_swap_ptr(&wgp->wgp_pending, NULL)) != NULL) {
3100 kpreempt_disable();
3101 const uint32_t h = curcpu()->ci_index; // pktq_rps_hash(m)
3102 M_SETCTX(m, wgp);
3103 if (__predict_false(!pktq_enqueue(wg_pktq, m, h))) {
3104 WGLOG(LOG_ERR, "%s: pktq full, dropping\n",
3105 if_name(&wg->wg_if));
3106 m_freem(m);
3107 }
3108 kpreempt_enable();
3109 }
3110
3111 if (wgs_prev->wgs_state == WGS_STATE_ESTABLISHED) {
3112 /* Wait for wg_get_stable_session to drain. */
3113 pserialize_perform(wgp->wgp_psz);
3114
3115 /* Transition ESTABLISHED->DESTROYING. */
3116 wgs_prev->wgs_state = WGS_STATE_DESTROYING;
3117
3118 /* We can't destroy the old session immediately */
3119 wg_schedule_session_dtor_timer(wgp);
3120 } else {
3121 KASSERTMSG(wgs_prev->wgs_state == WGS_STATE_UNKNOWN,
3122 "state=%d", wgs_prev->wgs_state);
3123 wg_clear_states(wgs_prev);
3124 wgs_prev->wgs_state = WGS_STATE_UNKNOWN;
3125 }
3126 }
3127
3128 static void
3129 wg_task_endpoint_changed(struct wg_softc *wg, struct wg_peer *wgp)
3130 {
3131
3132 WG_TRACE("WGP_TASK_ENDPOINT_CHANGED");
3133
3134 KASSERT(mutex_owned(wgp->wgp_lock));
3135
3136 if (atomic_load_relaxed(&wgp->wgp_endpoint_changing)) {
3137 pserialize_perform(wgp->wgp_psz);
3138 mutex_exit(wgp->wgp_lock);
3139 psref_target_destroy(&wgp->wgp_endpoint0->wgsa_psref,
3140 wg_psref_class);
3141 psref_target_init(&wgp->wgp_endpoint0->wgsa_psref,
3142 wg_psref_class);
3143 mutex_enter(wgp->wgp_lock);
3144 atomic_store_release(&wgp->wgp_endpoint_changing, 0);
3145 }
3146 }
3147
3148 static void
3149 wg_task_send_keepalive_message(struct wg_softc *wg, struct wg_peer *wgp)
3150 {
3151 struct wg_session *wgs;
3152
3153 WG_TRACE("WGP_TASK_SEND_KEEPALIVE_MESSAGE");
3154
3155 KASSERT(mutex_owned(wgp->wgp_lock));
3156
3157 wgs = wgp->wgp_session_stable;
3158 if (wgs->wgs_state != WGS_STATE_ESTABLISHED)
3159 return;
3160
3161 wg_send_keepalive_msg(wgp, wgs);
3162 }
3163
3164 static void
3165 wg_task_destroy_prev_session(struct wg_softc *wg, struct wg_peer *wgp)
3166 {
3167 struct wg_session *wgs;
3168
3169 WG_TRACE("WGP_TASK_DESTROY_PREV_SESSION");
3170
3171 KASSERT(mutex_owned(wgp->wgp_lock));
3172
3173 wgs = wgp->wgp_session_unstable;
3174 if (wgs->wgs_state == WGS_STATE_DESTROYING) {
3175 wg_put_session_index(wg, wgs);
3176 }
3177 }
3178
3179 static void
3180 wg_peer_work(struct work *wk, void *cookie)
3181 {
3182 struct wg_peer *wgp = container_of(wk, struct wg_peer, wgp_work);
3183 struct wg_softc *wg = wgp->wgp_sc;
3184 unsigned int tasks;
3185
3186 mutex_enter(wgp->wgp_intr_lock);
3187 while ((tasks = wgp->wgp_tasks) != 0) {
3188 wgp->wgp_tasks = 0;
3189 mutex_exit(wgp->wgp_intr_lock);
3190
3191 mutex_enter(wgp->wgp_lock);
3192 if (ISSET(tasks, WGP_TASK_SEND_INIT_MESSAGE))
3193 wg_task_send_init_message(wg, wgp);
3194 if (ISSET(tasks, WGP_TASK_RETRY_HANDSHAKE))
3195 wg_task_retry_handshake(wg, wgp);
3196 if (ISSET(tasks, WGP_TASK_ESTABLISH_SESSION))
3197 wg_task_establish_session(wg, wgp);
3198 if (ISSET(tasks, WGP_TASK_ENDPOINT_CHANGED))
3199 wg_task_endpoint_changed(wg, wgp);
3200 if (ISSET(tasks, WGP_TASK_SEND_KEEPALIVE_MESSAGE))
3201 wg_task_send_keepalive_message(wg, wgp);
3202 if (ISSET(tasks, WGP_TASK_DESTROY_PREV_SESSION))
3203 wg_task_destroy_prev_session(wg, wgp);
3204 mutex_exit(wgp->wgp_lock);
3205
3206 mutex_enter(wgp->wgp_intr_lock);
3207 }
3208 mutex_exit(wgp->wgp_intr_lock);
3209 }
3210
3211 static void
3212 wg_job(struct threadpool_job *job)
3213 {
3214 struct wg_softc *wg = container_of(job, struct wg_softc, wg_job);
3215 int bound, upcalls;
3216
3217 mutex_enter(wg->wg_intr_lock);
3218 while ((upcalls = wg->wg_upcalls) != 0) {
3219 wg->wg_upcalls = 0;
3220 mutex_exit(wg->wg_intr_lock);
3221 bound = curlwp_bind();
3222 if (ISSET(upcalls, WG_UPCALL_INET))
3223 wg_receive_packets(wg, AF_INET);
3224 if (ISSET(upcalls, WG_UPCALL_INET6))
3225 wg_receive_packets(wg, AF_INET6);
3226 curlwp_bindx(bound);
3227 mutex_enter(wg->wg_intr_lock);
3228 }
3229 threadpool_job_done(job);
3230 mutex_exit(wg->wg_intr_lock);
3231 }
3232
3233 static int
3234 wg_bind_port(struct wg_softc *wg, const uint16_t port)
3235 {
3236 int error;
3237 uint16_t old_port = wg->wg_listen_port;
3238
3239 if (port != 0 && old_port == port)
3240 return 0;
3241
3242 struct sockaddr_in _sin, *sin = &_sin;
3243 sin->sin_len = sizeof(*sin);
3244 sin->sin_family = AF_INET;
3245 sin->sin_addr.s_addr = INADDR_ANY;
3246 sin->sin_port = htons(port);
3247
3248 error = sobind(wg->wg_so4, sintosa(sin), curlwp);
3249 if (error != 0)
3250 return error;
3251
3252 #ifdef INET6
3253 struct sockaddr_in6 _sin6, *sin6 = &_sin6;
3254 sin6->sin6_len = sizeof(*sin6);
3255 sin6->sin6_family = AF_INET6;
3256 sin6->sin6_addr = in6addr_any;
3257 sin6->sin6_port = htons(port);
3258
3259 error = sobind(wg->wg_so6, sin6tosa(sin6), curlwp);
3260 if (error != 0)
3261 return error;
3262 #endif
3263
3264 wg->wg_listen_port = port;
3265
3266 return 0;
3267 }
3268
3269 static void
3270 wg_so_upcall(struct socket *so, void *cookie, int events, int waitflag)
3271 {
3272 struct wg_softc *wg = cookie;
3273 int reason;
3274
3275 reason = (so->so_proto->pr_domain->dom_family == AF_INET) ?
3276 WG_UPCALL_INET :
3277 WG_UPCALL_INET6;
3278
3279 mutex_enter(wg->wg_intr_lock);
3280 wg->wg_upcalls |= reason;
3281 threadpool_schedule_job(wg->wg_threadpool, &wg->wg_job);
3282 mutex_exit(wg->wg_intr_lock);
3283 }
3284
3285 static int
3286 wg_overudp_cb(struct mbuf **mp, int offset, struct socket *so,
3287 struct sockaddr *src, void *arg)
3288 {
3289 struct wg_softc *wg = arg;
3290 struct wg_msg wgm;
3291 struct mbuf *m = *mp;
3292
3293 WG_TRACE("enter");
3294
3295 /* Verify the mbuf chain is long enough to have a wg msg header. */
3296 KASSERT(offset <= m_length(m));
3297 if (__predict_false(m_length(m) - offset < sizeof(struct wg_msg))) {
3298 /* drop on the floor */
3299 m_freem(m);
3300 return -1;
3301 }
3302
3303 /*
3304 * Copy the message header (32-bit message type) out -- we'll
3305 * worry about contiguity and alignment later.
3306 */
3307 m_copydata(m, offset, sizeof(struct wg_msg), &wgm);
3308 WG_DLOG("type=%d\n", le32toh(wgm.wgm_type));
3309
3310 /*
3311 * Handle DATA packets promptly as they arrive. Other packets
3312 * may require expensive public-key crypto and are not as
3313 * sensitive to latency, so defer them to the worker thread.
3314 */
3315 switch (le32toh(wgm.wgm_type)) {
3316 case WG_MSG_TYPE_DATA:
3317 /* handle immediately */
3318 m_adj(m, offset);
3319 if (__predict_false(m->m_len < sizeof(struct wg_msg_data))) {
3320 m = m_pullup(m, sizeof(struct wg_msg_data));
3321 if (m == NULL)
3322 return -1;
3323 }
3324 wg_handle_msg_data(wg, m, src);
3325 *mp = NULL;
3326 return 1;
3327 case WG_MSG_TYPE_INIT:
3328 case WG_MSG_TYPE_RESP:
3329 case WG_MSG_TYPE_COOKIE:
3330 /* pass through to so_receive in wg_receive_packets */
3331 return 0;
3332 default:
3333 /* drop on the floor */
3334 m_freem(m);
3335 return -1;
3336 }
3337 }
3338
3339 static int
3340 wg_socreate(struct wg_softc *wg, int af, struct socket **sop)
3341 {
3342 int error;
3343 struct socket *so;
3344
3345 error = socreate(af, &so, SOCK_DGRAM, 0, curlwp, NULL);
3346 if (error != 0)
3347 return error;
3348
3349 solock(so);
3350 so->so_upcallarg = wg;
3351 so->so_upcall = wg_so_upcall;
3352 so->so_rcv.sb_flags |= SB_UPCALL;
3353 inpcb_register_overudp_cb(sotoinpcb(so), wg_overudp_cb, wg);
3354 sounlock(so);
3355
3356 *sop = so;
3357
3358 return 0;
3359 }
3360
3361 static bool
3362 wg_session_hit_limits(struct wg_session *wgs)
3363 {
3364
3365 /*
3366 * [W] 6.2: Transport Message Limits
3367 * "After REJECT-AFTER-MESSAGES transport data messages or after the
3368 * current secure session is REJECT-AFTER-TIME seconds old, whichever
3369 * comes first, WireGuard will refuse to send any more transport data
3370 * messages using the current secure session, ..."
3371 */
3372 KASSERT(wgs->wgs_time_established != 0);
3373 if ((time_uptime - wgs->wgs_time_established) > wg_reject_after_time) {
3374 WG_DLOG("The session hits REJECT_AFTER_TIME\n");
3375 return true;
3376 } else if (wg_session_get_send_counter(wgs) >
3377 wg_reject_after_messages) {
3378 WG_DLOG("The session hits REJECT_AFTER_MESSAGES\n");
3379 return true;
3380 }
3381
3382 return false;
3383 }
3384
3385 static void
3386 wgintr(void *cookie)
3387 {
3388 struct wg_peer *wgp;
3389 struct wg_session *wgs;
3390 struct mbuf *m;
3391 struct psref psref;
3392
3393 while ((m = pktq_dequeue(wg_pktq)) != NULL) {
3394 wgp = M_GETCTX(m, struct wg_peer *);
3395 if ((wgs = wg_get_stable_session(wgp, &psref)) == NULL) {
3396 WG_TRACE("no stable session");
3397 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
3398 goto next0;
3399 }
3400 if (__predict_false(wg_session_hit_limits(wgs))) {
3401 WG_TRACE("stable session hit limits");
3402 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
3403 goto next1;
3404 }
3405 wg_send_data_msg(wgp, wgs, m);
3406 m = NULL; /* consumed */
3407 next1: wg_put_session(wgs, &psref);
3408 next0: m_freem(m);
3409 /* XXX Yield to avoid userland starvation? */
3410 }
3411 }
3412
3413 static void
3414 wg_rekey_timer(void *arg)
3415 {
3416 struct wg_peer *wgp = arg;
3417
3418 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
3419 }
3420
3421 static void
3422 wg_purge_pending_packets(struct wg_peer *wgp)
3423 {
3424 struct mbuf *m;
3425
3426 m = atomic_swap_ptr(&wgp->wgp_pending, NULL);
3427 m_freem(m);
3428 pktq_barrier(wg_pktq);
3429 }
3430
3431 static void
3432 wg_handshake_timeout_timer(void *arg)
3433 {
3434 struct wg_peer *wgp = arg;
3435
3436 WG_TRACE("enter");
3437
3438 wg_schedule_peer_task(wgp, WGP_TASK_RETRY_HANDSHAKE);
3439 }
3440
3441 static struct wg_peer *
3442 wg_alloc_peer(struct wg_softc *wg)
3443 {
3444 struct wg_peer *wgp;
3445
3446 wgp = kmem_zalloc(sizeof(*wgp), KM_SLEEP);
3447
3448 wgp->wgp_sc = wg;
3449 callout_init(&wgp->wgp_rekey_timer, CALLOUT_MPSAFE);
3450 callout_setfunc(&wgp->wgp_rekey_timer, wg_rekey_timer, wgp);
3451 callout_init(&wgp->wgp_handshake_timeout_timer, CALLOUT_MPSAFE);
3452 callout_setfunc(&wgp->wgp_handshake_timeout_timer,
3453 wg_handshake_timeout_timer, wgp);
3454 callout_init(&wgp->wgp_session_dtor_timer, CALLOUT_MPSAFE);
3455 callout_setfunc(&wgp->wgp_session_dtor_timer,
3456 wg_session_dtor_timer, wgp);
3457 PSLIST_ENTRY_INIT(wgp, wgp_peerlist_entry);
3458 wgp->wgp_endpoint_changing = false;
3459 wgp->wgp_endpoint_available = false;
3460 wgp->wgp_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
3461 wgp->wgp_intr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
3462 wgp->wgp_psz = pserialize_create();
3463 psref_target_init(&wgp->wgp_psref, wg_psref_class);
3464
3465 wgp->wgp_endpoint = kmem_zalloc(sizeof(*wgp->wgp_endpoint), KM_SLEEP);
3466 wgp->wgp_endpoint0 = kmem_zalloc(sizeof(*wgp->wgp_endpoint0), KM_SLEEP);
3467 psref_target_init(&wgp->wgp_endpoint->wgsa_psref, wg_psref_class);
3468 psref_target_init(&wgp->wgp_endpoint0->wgsa_psref, wg_psref_class);
3469
3470 struct wg_session *wgs;
3471 wgp->wgp_session_stable =
3472 kmem_zalloc(sizeof(*wgp->wgp_session_stable), KM_SLEEP);
3473 wgp->wgp_session_unstable =
3474 kmem_zalloc(sizeof(*wgp->wgp_session_unstable), KM_SLEEP);
3475 wgs = wgp->wgp_session_stable;
3476 wgs->wgs_peer = wgp;
3477 wgs->wgs_state = WGS_STATE_UNKNOWN;
3478 psref_target_init(&wgs->wgs_psref, wg_psref_class);
3479 #ifndef __HAVE_ATOMIC64_LOADSTORE
3480 mutex_init(&wgs->wgs_send_counter_lock, MUTEX_DEFAULT, IPL_SOFTNET);
3481 #endif
3482 wgs->wgs_recvwin = kmem_zalloc(sizeof(*wgs->wgs_recvwin), KM_SLEEP);
3483 mutex_init(&wgs->wgs_recvwin->lock, MUTEX_DEFAULT, IPL_SOFTNET);
3484
3485 wgs = wgp->wgp_session_unstable;
3486 wgs->wgs_peer = wgp;
3487 wgs->wgs_state = WGS_STATE_UNKNOWN;
3488 psref_target_init(&wgs->wgs_psref, wg_psref_class);
3489 #ifndef __HAVE_ATOMIC64_LOADSTORE
3490 mutex_init(&wgs->wgs_send_counter_lock, MUTEX_DEFAULT, IPL_SOFTNET);
3491 #endif
3492 wgs->wgs_recvwin = kmem_zalloc(sizeof(*wgs->wgs_recvwin), KM_SLEEP);
3493 mutex_init(&wgs->wgs_recvwin->lock, MUTEX_DEFAULT, IPL_SOFTNET);
3494
3495 return wgp;
3496 }
3497
3498 static void
3499 wg_destroy_peer(struct wg_peer *wgp)
3500 {
3501 struct wg_session *wgs;
3502 struct wg_softc *wg = wgp->wgp_sc;
3503
3504 /* Prevent new packets from this peer on any source address. */
3505 rw_enter(wg->wg_rwlock, RW_WRITER);
3506 for (int i = 0; i < wgp->wgp_n_allowedips; i++) {
3507 struct wg_allowedip *wga = &wgp->wgp_allowedips[i];
3508 struct radix_node_head *rnh = wg_rnh(wg, wga->wga_family);
3509 struct radix_node *rn;
3510
3511 KASSERT(rnh != NULL);
3512 rn = rnh->rnh_deladdr(&wga->wga_sa_addr,
3513 &wga->wga_sa_mask, rnh);
3514 if (rn == NULL) {
3515 char addrstr[128];
3516 sockaddr_format(&wga->wga_sa_addr, addrstr,
3517 sizeof(addrstr));
3518 WGLOG(LOG_WARNING, "%s: Couldn't delete %s",
3519 if_name(&wg->wg_if), addrstr);
3520 }
3521 }
3522 rw_exit(wg->wg_rwlock);
3523
3524 /* Purge pending packets. */
3525 wg_purge_pending_packets(wgp);
3526
3527 /* Halt all packet processing and timeouts. */
3528 callout_halt(&wgp->wgp_rekey_timer, NULL);
3529 callout_halt(&wgp->wgp_handshake_timeout_timer, NULL);
3530 callout_halt(&wgp->wgp_session_dtor_timer, NULL);
3531
3532 /* Wait for any queued work to complete. */
3533 workqueue_wait(wg_wq, &wgp->wgp_work);
3534
3535 wgs = wgp->wgp_session_unstable;
3536 if (wgs->wgs_state != WGS_STATE_UNKNOWN) {
3537 mutex_enter(wgp->wgp_lock);
3538 wg_destroy_session(wg, wgs);
3539 mutex_exit(wgp->wgp_lock);
3540 }
3541 mutex_destroy(&wgs->wgs_recvwin->lock);
3542 kmem_free(wgs->wgs_recvwin, sizeof(*wgs->wgs_recvwin));
3543 #ifndef __HAVE_ATOMIC64_LOADSTORE
3544 mutex_destroy(&wgs->wgs_send_counter_lock);
3545 #endif
3546 kmem_free(wgs, sizeof(*wgs));
3547
3548 wgs = wgp->wgp_session_stable;
3549 if (wgs->wgs_state != WGS_STATE_UNKNOWN) {
3550 mutex_enter(wgp->wgp_lock);
3551 wg_destroy_session(wg, wgs);
3552 mutex_exit(wgp->wgp_lock);
3553 }
3554 mutex_destroy(&wgs->wgs_recvwin->lock);
3555 kmem_free(wgs->wgs_recvwin, sizeof(*wgs->wgs_recvwin));
3556 #ifndef __HAVE_ATOMIC64_LOADSTORE
3557 mutex_destroy(&wgs->wgs_send_counter_lock);
3558 #endif
3559 kmem_free(wgs, sizeof(*wgs));
3560
3561 psref_target_destroy(&wgp->wgp_endpoint->wgsa_psref, wg_psref_class);
3562 psref_target_destroy(&wgp->wgp_endpoint0->wgsa_psref, wg_psref_class);
3563 kmem_free(wgp->wgp_endpoint, sizeof(*wgp->wgp_endpoint));
3564 kmem_free(wgp->wgp_endpoint0, sizeof(*wgp->wgp_endpoint0));
3565
3566 pserialize_destroy(wgp->wgp_psz);
3567 mutex_obj_free(wgp->wgp_intr_lock);
3568 mutex_obj_free(wgp->wgp_lock);
3569
3570 kmem_free(wgp, sizeof(*wgp));
3571 }
3572
3573 static void
3574 wg_destroy_all_peers(struct wg_softc *wg)
3575 {
3576 struct wg_peer *wgp, *wgp0 __diagused;
3577 void *garbage_byname, *garbage_bypubkey;
3578
3579 restart:
3580 garbage_byname = garbage_bypubkey = NULL;
3581 mutex_enter(wg->wg_lock);
3582 WG_PEER_WRITER_FOREACH(wgp, wg) {
3583 if (wgp->wgp_name[0]) {
3584 wgp0 = thmap_del(wg->wg_peers_byname, wgp->wgp_name,
3585 strlen(wgp->wgp_name));
3586 KASSERT(wgp0 == wgp);
3587 garbage_byname = thmap_stage_gc(wg->wg_peers_byname);
3588 }
3589 wgp0 = thmap_del(wg->wg_peers_bypubkey, wgp->wgp_pubkey,
3590 sizeof(wgp->wgp_pubkey));
3591 KASSERT(wgp0 == wgp);
3592 garbage_bypubkey = thmap_stage_gc(wg->wg_peers_bypubkey);
3593 WG_PEER_WRITER_REMOVE(wgp);
3594 wg->wg_npeers--;
3595 mutex_enter(wgp->wgp_lock);
3596 pserialize_perform(wgp->wgp_psz);
3597 mutex_exit(wgp->wgp_lock);
3598 PSLIST_ENTRY_DESTROY(wgp, wgp_peerlist_entry);
3599 break;
3600 }
3601 mutex_exit(wg->wg_lock);
3602
3603 if (wgp == NULL)
3604 return;
3605
3606 psref_target_destroy(&wgp->wgp_psref, wg_psref_class);
3607
3608 wg_destroy_peer(wgp);
3609 thmap_gc(wg->wg_peers_byname, garbage_byname);
3610 thmap_gc(wg->wg_peers_bypubkey, garbage_bypubkey);
3611
3612 goto restart;
3613 }
3614
3615 static int
3616 wg_destroy_peer_name(struct wg_softc *wg, const char *name)
3617 {
3618 struct wg_peer *wgp, *wgp0 __diagused;
3619 void *garbage_byname, *garbage_bypubkey;
3620
3621 mutex_enter(wg->wg_lock);
3622 wgp = thmap_del(wg->wg_peers_byname, name, strlen(name));
3623 if (wgp != NULL) {
3624 wgp0 = thmap_del(wg->wg_peers_bypubkey, wgp->wgp_pubkey,
3625 sizeof(wgp->wgp_pubkey));
3626 KASSERT(wgp0 == wgp);
3627 garbage_byname = thmap_stage_gc(wg->wg_peers_byname);
3628 garbage_bypubkey = thmap_stage_gc(wg->wg_peers_bypubkey);
3629 WG_PEER_WRITER_REMOVE(wgp);
3630 wg->wg_npeers--;
3631 if (wg->wg_npeers == 0)
3632 if_link_state_change(&wg->wg_if, LINK_STATE_DOWN);
3633 mutex_enter(wgp->wgp_lock);
3634 pserialize_perform(wgp->wgp_psz);
3635 mutex_exit(wgp->wgp_lock);
3636 PSLIST_ENTRY_DESTROY(wgp, wgp_peerlist_entry);
3637 }
3638 mutex_exit(wg->wg_lock);
3639
3640 if (wgp == NULL)
3641 return ENOENT;
3642
3643 psref_target_destroy(&wgp->wgp_psref, wg_psref_class);
3644
3645 wg_destroy_peer(wgp);
3646 thmap_gc(wg->wg_peers_byname, garbage_byname);
3647 thmap_gc(wg->wg_peers_bypubkey, garbage_bypubkey);
3648
3649 return 0;
3650 }
3651
3652 static int
3653 wg_if_attach(struct wg_softc *wg)
3654 {
3655
3656 wg->wg_if.if_addrlen = 0;
3657 wg->wg_if.if_mtu = WG_MTU;
3658 wg->wg_if.if_flags = IFF_MULTICAST;
3659 wg->wg_if.if_extflags = IFEF_MPSAFE;
3660 wg->wg_if.if_ioctl = wg_ioctl;
3661 wg->wg_if.if_output = wg_output;
3662 wg->wg_if.if_init = wg_init;
3663 #ifdef ALTQ
3664 wg->wg_if.if_start = wg_start;
3665 #endif
3666 wg->wg_if.if_stop = wg_stop;
3667 wg->wg_if.if_type = IFT_OTHER;
3668 wg->wg_if.if_dlt = DLT_NULL;
3669 wg->wg_if.if_softc = wg;
3670 #ifdef ALTQ
3671 IFQ_SET_READY(&wg->wg_if.if_snd);
3672 #endif
3673 if_initialize(&wg->wg_if);
3674
3675 wg->wg_if.if_link_state = LINK_STATE_DOWN;
3676 if_alloc_sadl(&wg->wg_if);
3677 if_register(&wg->wg_if);
3678
3679 bpf_attach(&wg->wg_if, DLT_NULL, sizeof(uint32_t));
3680
3681 return 0;
3682 }
3683
3684 static void
3685 wg_if_detach(struct wg_softc *wg)
3686 {
3687 struct ifnet *ifp = &wg->wg_if;
3688
3689 bpf_detach(ifp);
3690 if_detach(ifp);
3691 }
3692
3693 static int
3694 wg_clone_create(struct if_clone *ifc, int unit)
3695 {
3696 struct wg_softc *wg;
3697 int error;
3698
3699 wg_guarantee_initialized();
3700
3701 error = wg_count_inc();
3702 if (error)
3703 return error;
3704
3705 wg = kmem_zalloc(sizeof(*wg), KM_SLEEP);
3706
3707 if_initname(&wg->wg_if, ifc->ifc_name, unit);
3708
3709 PSLIST_INIT(&wg->wg_peers);
3710 wg->wg_peers_bypubkey = thmap_create(0, NULL, THMAP_NOCOPY);
3711 wg->wg_peers_byname = thmap_create(0, NULL, THMAP_NOCOPY);
3712 wg->wg_sessions_byindex = thmap_create(0, NULL, THMAP_NOCOPY);
3713 wg->wg_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
3714 wg->wg_intr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
3715 wg->wg_rwlock = rw_obj_alloc();
3716 threadpool_job_init(&wg->wg_job, wg_job, wg->wg_intr_lock,
3717 "%s", if_name(&wg->wg_if));
3718 wg->wg_ops = &wg_ops_rumpkernel;
3719
3720 error = threadpool_get(&wg->wg_threadpool, PRI_NONE);
3721 if (error)
3722 goto fail0;
3723
3724 #ifdef INET
3725 error = wg_socreate(wg, AF_INET, &wg->wg_so4);
3726 if (error)
3727 goto fail1;
3728 rn_inithead((void **)&wg->wg_rtable_ipv4,
3729 offsetof(struct sockaddr_in, sin_addr) * NBBY);
3730 #endif
3731 #ifdef INET6
3732 error = wg_socreate(wg, AF_INET6, &wg->wg_so6);
3733 if (error)
3734 goto fail2;
3735 rn_inithead((void **)&wg->wg_rtable_ipv6,
3736 offsetof(struct sockaddr_in6, sin6_addr) * NBBY);
3737 #endif
3738
3739 error = wg_if_attach(wg);
3740 if (error)
3741 goto fail3;
3742
3743 return 0;
3744
3745 fail4: __unused
3746 wg_if_detach(wg);
3747 fail3: wg_destroy_all_peers(wg);
3748 #ifdef INET6
3749 solock(wg->wg_so6);
3750 wg->wg_so6->so_rcv.sb_flags &= ~SB_UPCALL;
3751 sounlock(wg->wg_so6);
3752 #endif
3753 #ifdef INET
3754 solock(wg->wg_so4);
3755 wg->wg_so4->so_rcv.sb_flags &= ~SB_UPCALL;
3756 sounlock(wg->wg_so4);
3757 #endif
3758 mutex_enter(wg->wg_intr_lock);
3759 threadpool_cancel_job(wg->wg_threadpool, &wg->wg_job);
3760 mutex_exit(wg->wg_intr_lock);
3761 #ifdef INET6
3762 if (wg->wg_rtable_ipv6 != NULL)
3763 free(wg->wg_rtable_ipv6, M_RTABLE);
3764 soclose(wg->wg_so6);
3765 fail2:
3766 #endif
3767 #ifdef INET
3768 if (wg->wg_rtable_ipv4 != NULL)
3769 free(wg->wg_rtable_ipv4, M_RTABLE);
3770 soclose(wg->wg_so4);
3771 fail1:
3772 #endif
3773 threadpool_put(wg->wg_threadpool, PRI_NONE);
3774 fail0: threadpool_job_destroy(&wg->wg_job);
3775 rw_obj_free(wg->wg_rwlock);
3776 mutex_obj_free(wg->wg_intr_lock);
3777 mutex_obj_free(wg->wg_lock);
3778 thmap_destroy(wg->wg_sessions_byindex);
3779 thmap_destroy(wg->wg_peers_byname);
3780 thmap_destroy(wg->wg_peers_bypubkey);
3781 PSLIST_DESTROY(&wg->wg_peers);
3782 kmem_free(wg, sizeof(*wg));
3783 wg_count_dec();
3784 return error;
3785 }
3786
3787 static int
3788 wg_clone_destroy(struct ifnet *ifp)
3789 {
3790 struct wg_softc *wg = container_of(ifp, struct wg_softc, wg_if);
3791
3792 #ifdef WG_RUMPKERNEL
3793 if (wg_user_mode(wg)) {
3794 rumpuser_wg_destroy(wg->wg_user);
3795 wg->wg_user = NULL;
3796 }
3797 #endif
3798
3799 wg_if_detach(wg);
3800 wg_destroy_all_peers(wg);
3801 #ifdef INET6
3802 solock(wg->wg_so6);
3803 wg->wg_so6->so_rcv.sb_flags &= ~SB_UPCALL;
3804 sounlock(wg->wg_so6);
3805 #endif
3806 #ifdef INET
3807 solock(wg->wg_so4);
3808 wg->wg_so4->so_rcv.sb_flags &= ~SB_UPCALL;
3809 sounlock(wg->wg_so4);
3810 #endif
3811 mutex_enter(wg->wg_intr_lock);
3812 threadpool_cancel_job(wg->wg_threadpool, &wg->wg_job);
3813 mutex_exit(wg->wg_intr_lock);
3814 #ifdef INET6
3815 if (wg->wg_rtable_ipv6 != NULL)
3816 free(wg->wg_rtable_ipv6, M_RTABLE);
3817 soclose(wg->wg_so6);
3818 #endif
3819 #ifdef INET
3820 if (wg->wg_rtable_ipv4 != NULL)
3821 free(wg->wg_rtable_ipv4, M_RTABLE);
3822 soclose(wg->wg_so4);
3823 #endif
3824 threadpool_put(wg->wg_threadpool, PRI_NONE);
3825 threadpool_job_destroy(&wg->wg_job);
3826 rw_obj_free(wg->wg_rwlock);
3827 mutex_obj_free(wg->wg_intr_lock);
3828 mutex_obj_free(wg->wg_lock);
3829 thmap_destroy(wg->wg_sessions_byindex);
3830 thmap_destroy(wg->wg_peers_byname);
3831 thmap_destroy(wg->wg_peers_bypubkey);
3832 PSLIST_DESTROY(&wg->wg_peers);
3833 kmem_free(wg, sizeof(*wg));
3834 wg_count_dec();
3835
3836 return 0;
3837 }
3838
3839 static struct wg_peer *
3840 wg_pick_peer_by_sa(struct wg_softc *wg, const struct sockaddr *sa,
3841 struct psref *psref)
3842 {
3843 struct radix_node_head *rnh;
3844 struct radix_node *rn;
3845 struct wg_peer *wgp = NULL;
3846 struct wg_allowedip *wga;
3847
3848 #ifdef WG_DEBUG_LOG
3849 char addrstr[128];
3850 sockaddr_format(sa, addrstr, sizeof(addrstr));
3851 WG_DLOG("sa=%s\n", addrstr);
3852 #endif
3853
3854 rw_enter(wg->wg_rwlock, RW_READER);
3855
3856 rnh = wg_rnh(wg, sa->sa_family);
3857 if (rnh == NULL)
3858 goto out;
3859
3860 rn = rnh->rnh_matchaddr(sa, rnh);
3861 if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0)
3862 goto out;
3863
3864 WG_TRACE("success");
3865
3866 wga = container_of(rn, struct wg_allowedip, wga_nodes[0]);
3867 wgp = wga->wga_peer;
3868 wg_get_peer(wgp, psref);
3869
3870 out:
3871 rw_exit(wg->wg_rwlock);
3872 return wgp;
3873 }
3874
3875 static void
3876 wg_fill_msg_data(struct wg_softc *wg, struct wg_peer *wgp,
3877 struct wg_session *wgs, struct wg_msg_data *wgmd)
3878 {
3879
3880 memset(wgmd, 0, sizeof(*wgmd));
3881 wgmd->wgmd_type = htole32(WG_MSG_TYPE_DATA);
3882 wgmd->wgmd_receiver = wgs->wgs_remote_index;
3883 /* [W] 5.4.6: msg.counter := Nm^send */
3884 /* [W] 5.4.6: Nm^send := Nm^send + 1 */
3885 wgmd->wgmd_counter = htole64(wg_session_inc_send_counter(wgs));
3886 WG_DLOG("counter=%"PRIu64"\n", le64toh(wgmd->wgmd_counter));
3887 }
3888
3889 static int
3890 wg_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
3891 const struct rtentry *rt)
3892 {
3893 struct wg_softc *wg = ifp->if_softc;
3894 struct wg_peer *wgp = NULL;
3895 struct wg_session *wgs = NULL;
3896 struct psref wgp_psref, wgs_psref;
3897 int bound;
3898 int error;
3899
3900 bound = curlwp_bind();
3901
3902 /* TODO make the nest limit configurable via sysctl */
3903 error = if_tunnel_check_nesting(ifp, m, 1);
3904 if (error) {
3905 WGLOG(LOG_ERR,
3906 "%s: tunneling loop detected and packet dropped\n",
3907 if_name(&wg->wg_if));
3908 goto out0;
3909 }
3910
3911 #ifdef ALTQ
3912 bool altq = atomic_load_relaxed(&ifp->if_snd.altq_flags)
3913 & ALTQF_ENABLED;
3914 if (altq)
3915 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
3916 #endif
3917
3918 bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
3919
3920 m->m_flags &= ~(M_BCAST|M_MCAST);
3921
3922 wgp = wg_pick_peer_by_sa(wg, dst, &wgp_psref);
3923 if (wgp == NULL) {
3924 WG_TRACE("peer not found");
3925 error = EHOSTUNREACH;
3926 goto out0;
3927 }
3928
3929 /* Clear checksum-offload flags. */
3930 m->m_pkthdr.csum_flags = 0;
3931 m->m_pkthdr.csum_data = 0;
3932
3933 /* Check whether there's an established session. */
3934 wgs = wg_get_stable_session(wgp, &wgs_psref);
3935 if (wgs == NULL) {
3936 /*
3937 * No established session. If we're the first to try
3938 * sending data, schedule a handshake and queue the
3939 * packet for when the handshake is done; otherwise
3940 * just drop the packet and let the ongoing handshake
3941 * attempt continue. We could queue more data packets
3942 * but it's not clear that's worthwhile.
3943 */
3944 if (atomic_cas_ptr(&wgp->wgp_pending, NULL, m) == NULL) {
3945 m = NULL; /* consume */
3946 WG_TRACE("queued first packet; init handshake");
3947 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
3948 } else {
3949 WG_TRACE("first packet already queued, dropping");
3950 }
3951 goto out1;
3952 }
3953
3954 /* There's an established session. Toss it in the queue. */
3955 #ifdef ALTQ
3956 if (altq) {
3957 mutex_enter(ifp->if_snd.ifq_lock);
3958 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
3959 M_SETCTX(m, wgp);
3960 ALTQ_ENQUEUE(&ifp->if_snd, m, error);
3961 m = NULL; /* consume */
3962 }
3963 mutex_exit(ifp->if_snd.ifq_lock);
3964 if (m == NULL) {
3965 wg_start(ifp);
3966 goto out2;
3967 }
3968 }
3969 #endif
3970 kpreempt_disable();
3971 const uint32_t h = curcpu()->ci_index; // pktq_rps_hash(m)
3972 M_SETCTX(m, wgp);
3973 if (__predict_false(!pktq_enqueue(wg_pktq, m, h))) {
3974 WGLOG(LOG_ERR, "%s: pktq full, dropping\n",
3975 if_name(&wg->wg_if));
3976 error = ENOBUFS;
3977 goto out3;
3978 }
3979 m = NULL; /* consumed */
3980 error = 0;
3981 out3: kpreempt_enable();
3982
3983 #ifdef ALTQ
3984 out2:
3985 #endif
3986 wg_put_session(wgs, &wgs_psref);
3987 out1: wg_put_peer(wgp, &wgp_psref);
3988 out0: m_freem(m);
3989 curlwp_bindx(bound);
3990 return error;
3991 }
3992
3993 static int
3994 wg_send_udp(struct wg_peer *wgp, struct mbuf *m)
3995 {
3996 struct psref psref;
3997 struct wg_sockaddr *wgsa;
3998 int error;
3999 struct socket *so;
4000
4001 wgsa = wg_get_endpoint_sa(wgp, &psref);
4002 so = wg_get_so_by_peer(wgp, wgsa);
4003 solock(so);
4004 if (wgsatosa(wgsa)->sa_family == AF_INET) {
4005 error = udp_send(so, m, wgsatosa(wgsa), NULL, curlwp);
4006 } else {
4007 #ifdef INET6
4008 error = udp6_output(sotoinpcb(so), m, wgsatosin6(wgsa),
4009 NULL, curlwp);
4010 #else
4011 m_freem(m);
4012 error = EPFNOSUPPORT;
4013 #endif
4014 }
4015 sounlock(so);
4016 wg_put_sa(wgp, wgsa, &psref);
4017
4018 return error;
4019 }
4020
4021 /* Inspired by pppoe_get_mbuf */
4022 static struct mbuf *
4023 wg_get_mbuf(size_t leading_len, size_t len)
4024 {
4025 struct mbuf *m;
4026
4027 KASSERT(leading_len <= MCLBYTES);
4028 KASSERT(len <= MCLBYTES - leading_len);
4029
4030 m = m_gethdr(M_DONTWAIT, MT_DATA);
4031 if (m == NULL)
4032 return NULL;
4033 if (len + leading_len > MHLEN) {
4034 m_clget(m, M_DONTWAIT);
4035 if ((m->m_flags & M_EXT) == 0) {
4036 m_free(m);
4037 return NULL;
4038 }
4039 }
4040 m->m_data += leading_len;
4041 m->m_pkthdr.len = m->m_len = len;
4042
4043 return m;
4044 }
4045
4046 static int
4047 wg_send_data_msg(struct wg_peer *wgp, struct wg_session *wgs,
4048 struct mbuf *m)
4049 {
4050 struct wg_softc *wg = wgp->wgp_sc;
4051 int error;
4052 size_t inner_len, padded_len, encrypted_len;
4053 char *padded_buf = NULL;
4054 size_t mlen;
4055 struct wg_msg_data *wgmd;
4056 bool free_padded_buf = false;
4057 struct mbuf *n;
4058 size_t leading_len = max_hdr + sizeof(struct udphdr);
4059
4060 mlen = m_length(m);
4061 inner_len = mlen;
4062 padded_len = roundup(mlen, 16);
4063 encrypted_len = padded_len + WG_AUTHTAG_LEN;
4064 WG_DLOG("inner=%zu, padded=%zu, encrypted_len=%zu\n",
4065 inner_len, padded_len, encrypted_len);
4066 if (mlen != 0) {
4067 bool success;
4068 success = m_ensure_contig(&m, padded_len);
4069 if (success) {
4070 padded_buf = mtod(m, char *);
4071 } else {
4072 padded_buf = kmem_intr_alloc(padded_len, KM_NOSLEEP);
4073 if (padded_buf == NULL) {
4074 error = ENOBUFS;
4075 goto end;
4076 }
4077 free_padded_buf = true;
4078 m_copydata(m, 0, mlen, padded_buf);
4079 }
4080 memset(padded_buf + mlen, 0, padded_len - inner_len);
4081 }
4082
4083 n = wg_get_mbuf(leading_len, sizeof(*wgmd) + encrypted_len);
4084 if (n == NULL) {
4085 error = ENOBUFS;
4086 goto end;
4087 }
4088 KASSERT(n->m_len >= sizeof(*wgmd));
4089 wgmd = mtod(n, struct wg_msg_data *);
4090 wg_fill_msg_data(wg, wgp, wgs, wgmd);
4091 #ifdef WG_DEBUG_PACKET
4092 if (wg_debug & WG_DEBUG_FLAGS_PACKET) {
4093 hexdump(aprint_debug, "wgmd", wgmd, sizeof(*wgmd));
4094 hexdump(aprint_debug, "padded_buf", padded_buf,
4095 padded_len);
4096 }
4097 #endif
4098 /* [W] 5.4.6: AEAD(Tm^send, Nm^send, P, e) */
4099 wg_algo_aead_enc((char *)wgmd + sizeof(*wgmd), encrypted_len,
4100 wgs->wgs_tkey_send, le64toh(wgmd->wgmd_counter),
4101 padded_buf, padded_len,
4102 NULL, 0);
4103
4104 error = wg->wg_ops->send_data_msg(wgp, n);
4105 if (error == 0) {
4106 struct ifnet *ifp = &wg->wg_if;
4107 if_statadd(ifp, if_obytes, mlen);
4108 if_statinc(ifp, if_opackets);
4109 if (wgs->wgs_is_initiator &&
4110 wgs->wgs_time_last_data_sent == 0) {
4111 /*
4112 * [W] 6.2 Transport Message Limits
4113 * "if a peer is the initiator of a current secure
4114 * session, WireGuard will send a handshake initiation
4115 * message to begin a new secure session if, after
4116 * transmitting a transport data message, the current
4117 * secure session is REKEY-AFTER-TIME seconds old,"
4118 */
4119 wg_schedule_rekey_timer(wgp);
4120 }
4121 wgs->wgs_time_last_data_sent = time_uptime;
4122 if (wg_session_get_send_counter(wgs) >=
4123 wg_rekey_after_messages) {
4124 /*
4125 * [W] 6.2 Transport Message Limits
4126 * "WireGuard will try to create a new session, by
4127 * sending a handshake initiation message (section
4128 * 5.4.2), after it has sent REKEY-AFTER-MESSAGES
4129 * transport data messages..."
4130 */
4131 wg_schedule_peer_task(wgp, WGP_TASK_SEND_INIT_MESSAGE);
4132 }
4133 }
4134 end:
4135 m_freem(m);
4136 if (free_padded_buf)
4137 kmem_intr_free(padded_buf, padded_len);
4138 return error;
4139 }
4140
4141 static void
4142 wg_input(struct ifnet *ifp, struct mbuf *m, const int af)
4143 {
4144 pktqueue_t *pktq;
4145 size_t pktlen;
4146
4147 KASSERT(af == AF_INET || af == AF_INET6);
4148
4149 WG_TRACE("");
4150
4151 m_set_rcvif(m, ifp);
4152 pktlen = m->m_pkthdr.len;
4153
4154 bpf_mtap_af(ifp, af, m, BPF_D_IN);
4155
4156 switch (af) {
4157 case AF_INET:
4158 pktq = ip_pktq;
4159 break;
4160 #ifdef INET6
4161 case AF_INET6:
4162 pktq = ip6_pktq;
4163 break;
4164 #endif
4165 default:
4166 panic("invalid af=%d", af);
4167 }
4168
4169 kpreempt_disable();
4170 const u_int h = curcpu()->ci_index;
4171 if (__predict_true(pktq_enqueue(pktq, m, h))) {
4172 if_statadd(ifp, if_ibytes, pktlen);
4173 if_statinc(ifp, if_ipackets);
4174 } else {
4175 m_freem(m);
4176 }
4177 kpreempt_enable();
4178 }
4179
4180 static void
4181 wg_calc_pubkey(uint8_t pubkey[WG_STATIC_KEY_LEN],
4182 const uint8_t privkey[WG_STATIC_KEY_LEN])
4183 {
4184
4185 crypto_scalarmult_base(pubkey, privkey);
4186 }
4187
4188 static int
4189 wg_rtable_add_route(struct wg_softc *wg, struct wg_allowedip *wga)
4190 {
4191 struct radix_node_head *rnh;
4192 struct radix_node *rn;
4193 int error = 0;
4194
4195 rw_enter(wg->wg_rwlock, RW_WRITER);
4196 rnh = wg_rnh(wg, wga->wga_family);
4197 KASSERT(rnh != NULL);
4198 rn = rnh->rnh_addaddr(&wga->wga_sa_addr, &wga->wga_sa_mask, rnh,
4199 wga->wga_nodes);
4200 rw_exit(wg->wg_rwlock);
4201
4202 if (rn == NULL)
4203 error = EEXIST;
4204
4205 return error;
4206 }
4207
4208 static int
4209 wg_handle_prop_peer(struct wg_softc *wg, prop_dictionary_t peer,
4210 struct wg_peer **wgpp)
4211 {
4212 int error = 0;
4213 const void *pubkey;
4214 size_t pubkey_len;
4215 const void *psk;
4216 size_t psk_len;
4217 const char *name = NULL;
4218
4219 if (prop_dictionary_get_string(peer, "name", &name)) {
4220 if (strlen(name) > WG_PEER_NAME_MAXLEN) {
4221 error = EINVAL;
4222 goto out;
4223 }
4224 }
4225
4226 if (!prop_dictionary_get_data(peer, "public_key",
4227 &pubkey, &pubkey_len)) {
4228 error = EINVAL;
4229 goto out;
4230 }
4231 #ifdef WG_DEBUG_DUMP
4232 if (wg_debug & WG_DEBUG_FLAGS_DUMP) {
4233 char *hex = gethexdump(pubkey, pubkey_len);
4234 log(LOG_DEBUG, "pubkey=%p, pubkey_len=%zu\n%s\n",
4235 pubkey, pubkey_len, hex);
4236 puthexdump(hex, pubkey, pubkey_len);
4237 }
4238 #endif
4239
4240 struct wg_peer *wgp = wg_alloc_peer(wg);
4241 memcpy(wgp->wgp_pubkey, pubkey, sizeof(wgp->wgp_pubkey));
4242 if (name != NULL)
4243 strncpy(wgp->wgp_name, name, sizeof(wgp->wgp_name));
4244
4245 if (prop_dictionary_get_data(peer, "preshared_key", &psk, &psk_len)) {
4246 if (psk_len != sizeof(wgp->wgp_psk)) {
4247 error = EINVAL;
4248 goto out;
4249 }
4250 memcpy(wgp->wgp_psk, psk, sizeof(wgp->wgp_psk));
4251 }
4252
4253 const void *addr;
4254 size_t addr_len;
4255 struct wg_sockaddr *wgsa = wgp->wgp_endpoint;
4256
4257 if (!prop_dictionary_get_data(peer, "endpoint", &addr, &addr_len))
4258 goto skip_endpoint;
4259 if (addr_len < sizeof(*wgsatosa(wgsa)) ||
4260 addr_len > sizeof(*wgsatoss(wgsa))) {
4261 error = EINVAL;
4262 goto out;
4263 }
4264 memcpy(wgsatoss(wgsa), addr, addr_len);
4265 switch (wgsa_family(wgsa)) {
4266 case AF_INET:
4267 #ifdef INET6
4268 case AF_INET6:
4269 #endif
4270 break;
4271 default:
4272 error = EPFNOSUPPORT;
4273 goto out;
4274 }
4275 if (addr_len != sockaddr_getsize_by_family(wgsa_family(wgsa))) {
4276 error = EINVAL;
4277 goto out;
4278 }
4279 {
4280 char addrstr[128];
4281 sockaddr_format(wgsatosa(wgsa), addrstr, sizeof(addrstr));
4282 WG_DLOG("addr=%s\n", addrstr);
4283 }
4284 wgp->wgp_endpoint_available = true;
4285
4286 prop_array_t allowedips;
4287 skip_endpoint:
4288 allowedips = prop_dictionary_get(peer, "allowedips");
4289 if (allowedips == NULL)
4290 goto skip;
4291
4292 prop_object_iterator_t _it = prop_array_iterator(allowedips);
4293 prop_dictionary_t prop_allowedip;
4294 int j = 0;
4295 while ((prop_allowedip = prop_object_iterator_next(_it)) != NULL) {
4296 struct wg_allowedip *wga = &wgp->wgp_allowedips[j];
4297
4298 if (!prop_dictionary_get_int(prop_allowedip, "family",
4299 &wga->wga_family))
4300 continue;
4301 if (!prop_dictionary_get_data(prop_allowedip, "ip",
4302 &addr, &addr_len))
4303 continue;
4304 if (!prop_dictionary_get_uint8(prop_allowedip, "cidr",
4305 &wga->wga_cidr))
4306 continue;
4307
4308 switch (wga->wga_family) {
4309 case AF_INET: {
4310 struct sockaddr_in sin;
4311 char addrstr[128];
4312 struct in_addr mask;
4313 struct sockaddr_in sin_mask;
4314
4315 if (addr_len != sizeof(struct in_addr))
4316 return EINVAL;
4317 memcpy(&wga->wga_addr4, addr, addr_len);
4318
4319 sockaddr_in_init(&sin, (const struct in_addr *)addr,
4320 0);
4321 sockaddr_copy(&wga->wga_sa_addr,
4322 sizeof(sin), sintosa(&sin));
4323
4324 sockaddr_format(sintosa(&sin),
4325 addrstr, sizeof(addrstr));
4326 WG_DLOG("addr=%s/%d\n", addrstr, wga->wga_cidr);
4327
4328 in_len2mask(&mask, wga->wga_cidr);
4329 sockaddr_in_init(&sin_mask, &mask, 0);
4330 sockaddr_copy(&wga->wga_sa_mask,
4331 sizeof(sin_mask), sintosa(&sin_mask));
4332
4333 break;
4334 }
4335 #ifdef INET6
4336 case AF_INET6: {
4337 struct sockaddr_in6 sin6;
4338 char addrstr[128];
4339 struct in6_addr mask;
4340 struct sockaddr_in6 sin6_mask;
4341
4342 if (addr_len != sizeof(struct in6_addr))
4343 return EINVAL;
4344 memcpy(&wga->wga_addr6, addr, addr_len);
4345
4346 sockaddr_in6_init(&sin6, (const struct in6_addr *)addr,
4347 0, 0, 0);
4348 sockaddr_copy(&wga->wga_sa_addr,
4349 sizeof(sin6), sin6tosa(&sin6));
4350
4351 sockaddr_format(sin6tosa(&sin6),
4352 addrstr, sizeof(addrstr));
4353 WG_DLOG("addr=%s/%d\n", addrstr, wga->wga_cidr);
4354
4355 in6_prefixlen2mask(&mask, wga->wga_cidr);
4356 sockaddr_in6_init(&sin6_mask, &mask, 0, 0, 0);
4357 sockaddr_copy(&wga->wga_sa_mask,
4358 sizeof(sin6_mask), sin6tosa(&sin6_mask));
4359
4360 break;
4361 }
4362 #endif
4363 default:
4364 error = EINVAL;
4365 goto out;
4366 }
4367 wga->wga_peer = wgp;
4368
4369 error = wg_rtable_add_route(wg, wga);
4370 if (error != 0)
4371 goto out;
4372
4373 j++;
4374 }
4375 wgp->wgp_n_allowedips = j;
4376 skip:
4377 *wgpp = wgp;
4378 out:
4379 return error;
4380 }
4381
4382 static int
4383 wg_alloc_prop_buf(char **_buf, struct ifdrv *ifd)
4384 {
4385 int error;
4386 char *buf;
4387
4388 WG_DLOG("buf=%p, len=%zu\n", ifd->ifd_data, ifd->ifd_len);
4389 if (ifd->ifd_len >= WG_MAX_PROPLEN)
4390 return E2BIG;
4391 buf = kmem_alloc(ifd->ifd_len + 1, KM_SLEEP);
4392 error = copyin(ifd->ifd_data, buf, ifd->ifd_len);
4393 if (error != 0)
4394 return error;
4395 buf[ifd->ifd_len] = '\0';
4396 #ifdef WG_DEBUG_DUMP
4397 if (wg_debug & WG_DEBUG_FLAGS_DUMP) {
4398 log(LOG_DEBUG, "%.*s\n", (int)MIN(INT_MAX, ifd->ifd_len),
4399 (const char *)buf);
4400 }
4401 #endif
4402 *_buf = buf;
4403 return 0;
4404 }
4405
4406 static int
4407 wg_ioctl_set_private_key(struct wg_softc *wg, struct ifdrv *ifd)
4408 {
4409 int error;
4410 prop_dictionary_t prop_dict;
4411 char *buf = NULL;
4412 const void *privkey;
4413 size_t privkey_len;
4414
4415 error = wg_alloc_prop_buf(&buf, ifd);
4416 if (error != 0)
4417 return error;
4418 error = EINVAL;
4419 prop_dict = prop_dictionary_internalize(buf);
4420 if (prop_dict == NULL)
4421 goto out;
4422 if (!prop_dictionary_get_data(prop_dict, "private_key",
4423 &privkey, &privkey_len))
4424 goto out;
4425 #ifdef WG_DEBUG_DUMP
4426 if (wg_debug & WG_DEBUG_FLAGS_DUMP) {
4427 char *hex = gethexdump(privkey, privkey_len);
4428 log(LOG_DEBUG, "privkey=%p, privkey_len=%zu\n%s\n",
4429 privkey, privkey_len, hex);
4430 puthexdump(hex, privkey, privkey_len);
4431 }
4432 #endif
4433 if (privkey_len != WG_STATIC_KEY_LEN)
4434 goto out;
4435 memcpy(wg->wg_privkey, privkey, WG_STATIC_KEY_LEN);
4436 wg_calc_pubkey(wg->wg_pubkey, wg->wg_privkey);
4437 error = 0;
4438
4439 out:
4440 kmem_free(buf, ifd->ifd_len + 1);
4441 return error;
4442 }
4443
4444 static int
4445 wg_ioctl_set_listen_port(struct wg_softc *wg, struct ifdrv *ifd)
4446 {
4447 int error;
4448 prop_dictionary_t prop_dict;
4449 char *buf = NULL;
4450 uint16_t port;
4451
4452 error = wg_alloc_prop_buf(&buf, ifd);
4453 if (error != 0)
4454 return error;
4455 error = EINVAL;
4456 prop_dict = prop_dictionary_internalize(buf);
4457 if (prop_dict == NULL)
4458 goto out;
4459 if (!prop_dictionary_get_uint16(prop_dict, "listen_port", &port))
4460 goto out;
4461
4462 error = wg->wg_ops->bind_port(wg, (uint16_t)port);
4463
4464 out:
4465 kmem_free(buf, ifd->ifd_len + 1);
4466 return error;
4467 }
4468
4469 static int
4470 wg_ioctl_add_peer(struct wg_softc *wg, struct ifdrv *ifd)
4471 {
4472 int error;
4473 prop_dictionary_t prop_dict;
4474 char *buf = NULL;
4475 struct wg_peer *wgp = NULL, *wgp0 __diagused;
4476
4477 error = wg_alloc_prop_buf(&buf, ifd);
4478 if (error != 0)
4479 return error;
4480 error = EINVAL;
4481 prop_dict = prop_dictionary_internalize(buf);
4482 if (prop_dict == NULL)
4483 goto out;
4484
4485 error = wg_handle_prop_peer(wg, prop_dict, &wgp);
4486 if (error != 0)
4487 goto out;
4488
4489 mutex_enter(wg->wg_lock);
4490 if (thmap_get(wg->wg_peers_bypubkey, wgp->wgp_pubkey,
4491 sizeof(wgp->wgp_pubkey)) != NULL ||
4492 (wgp->wgp_name[0] &&
4493 thmap_get(wg->wg_peers_byname, wgp->wgp_name,
4494 strlen(wgp->wgp_name)) != NULL)) {
4495 mutex_exit(wg->wg_lock);
4496 wg_destroy_peer(wgp);
4497 error = EEXIST;
4498 goto out;
4499 }
4500 wgp0 = thmap_put(wg->wg_peers_bypubkey, wgp->wgp_pubkey,
4501 sizeof(wgp->wgp_pubkey), wgp);
4502 KASSERT(wgp0 == wgp);
4503 if (wgp->wgp_name[0]) {
4504 wgp0 = thmap_put(wg->wg_peers_byname, wgp->wgp_name,
4505 strlen(wgp->wgp_name), wgp);
4506 KASSERT(wgp0 == wgp);
4507 }
4508 WG_PEER_WRITER_INSERT_HEAD(wgp, wg);
4509 wg->wg_npeers++;
4510 mutex_exit(wg->wg_lock);
4511
4512 if_link_state_change(&wg->wg_if, LINK_STATE_UP);
4513
4514 out:
4515 kmem_free(buf, ifd->ifd_len + 1);
4516 return error;
4517 }
4518
4519 static int
4520 wg_ioctl_delete_peer(struct wg_softc *wg, struct ifdrv *ifd)
4521 {
4522 int error;
4523 prop_dictionary_t prop_dict;
4524 char *buf = NULL;
4525 const char *name;
4526
4527 error = wg_alloc_prop_buf(&buf, ifd);
4528 if (error != 0)
4529 return error;
4530 error = EINVAL;
4531 prop_dict = prop_dictionary_internalize(buf);
4532 if (prop_dict == NULL)
4533 goto out;
4534
4535 if (!prop_dictionary_get_string(prop_dict, "name", &name))
4536 goto out;
4537 if (strlen(name) > WG_PEER_NAME_MAXLEN)
4538 goto out;
4539
4540 error = wg_destroy_peer_name(wg, name);
4541 out:
4542 kmem_free(buf, ifd->ifd_len + 1);
4543 return error;
4544 }
4545
4546 static bool
4547 wg_is_authorized(struct wg_softc *wg, u_long cmd)
4548 {
4549 int au = cmd == SIOCGDRVSPEC ?
4550 KAUTH_REQ_NETWORK_INTERFACE_WG_GETPRIV :
4551 KAUTH_REQ_NETWORK_INTERFACE_WG_SETPRIV;
4552 return kauth_authorize_network(kauth_cred_get(),
4553 KAUTH_NETWORK_INTERFACE_WG, au, &wg->wg_if,
4554 (void *)cmd, NULL) == 0;
4555 }
4556
4557 static int
4558 wg_ioctl_get(struct wg_softc *wg, struct ifdrv *ifd)
4559 {
4560 int error = ENOMEM;
4561 prop_dictionary_t prop_dict;
4562 prop_array_t peers = NULL;
4563 char *buf;
4564 struct wg_peer *wgp;
4565 int s, i;
4566
4567 prop_dict = prop_dictionary_create();
4568 if (prop_dict == NULL)
4569 goto error;
4570
4571 if (wg_is_authorized(wg, SIOCGDRVSPEC)) {
4572 if (!prop_dictionary_set_data(prop_dict, "private_key",
4573 wg->wg_privkey, WG_STATIC_KEY_LEN))
4574 goto error;
4575 }
4576
4577 if (wg->wg_listen_port != 0) {
4578 if (!prop_dictionary_set_uint16(prop_dict, "listen_port",
4579 wg->wg_listen_port))
4580 goto error;
4581 }
4582
4583 if (wg->wg_npeers == 0)
4584 goto skip_peers;
4585
4586 peers = prop_array_create();
4587 if (peers == NULL)
4588 goto error;
4589
4590 s = pserialize_read_enter();
4591 i = 0;
4592 WG_PEER_READER_FOREACH(wgp, wg) {
4593 struct wg_sockaddr *wgsa;
4594 struct psref wgp_psref, wgsa_psref;
4595 prop_dictionary_t prop_peer;
4596
4597 wg_get_peer(wgp, &wgp_psref);
4598 pserialize_read_exit(s);
4599
4600 prop_peer = prop_dictionary_create();
4601 if (prop_peer == NULL)
4602 goto next;
4603
4604 if (strlen(wgp->wgp_name) > 0) {
4605 if (!prop_dictionary_set_string(prop_peer, "name",
4606 wgp->wgp_name))
4607 goto next;
4608 }
4609
4610 if (!prop_dictionary_set_data(prop_peer, "public_key",
4611 wgp->wgp_pubkey, sizeof(wgp->wgp_pubkey)))
4612 goto next;
4613
4614 uint8_t psk_zero[WG_PRESHARED_KEY_LEN] = {0};
4615 if (!consttime_memequal(wgp->wgp_psk, psk_zero,
4616 sizeof(wgp->wgp_psk))) {
4617 if (wg_is_authorized(wg, SIOCGDRVSPEC)) {
4618 if (!prop_dictionary_set_data(prop_peer,
4619 "preshared_key",
4620 wgp->wgp_psk, sizeof(wgp->wgp_psk)))
4621 goto next;
4622 }
4623 }
4624
4625 wgsa = wg_get_endpoint_sa(wgp, &wgsa_psref);
4626 CTASSERT(AF_UNSPEC == 0);
4627 if (wgsa_family(wgsa) != 0 /*AF_UNSPEC*/ &&
4628 !prop_dictionary_set_data(prop_peer, "endpoint",
4629 wgsatoss(wgsa),
4630 sockaddr_getsize_by_family(wgsa_family(wgsa)))) {
4631 wg_put_sa(wgp, wgsa, &wgsa_psref);
4632 goto next;
4633 }
4634 wg_put_sa(wgp, wgsa, &wgsa_psref);
4635
4636 const struct timespec *t = &wgp->wgp_last_handshake_time;
4637
4638 if (!prop_dictionary_set_uint64(prop_peer,
4639 "last_handshake_time_sec", (uint64_t)t->tv_sec))
4640 goto next;
4641 if (!prop_dictionary_set_uint32(prop_peer,
4642 "last_handshake_time_nsec", (uint32_t)t->tv_nsec))
4643 goto next;
4644
4645 if (wgp->wgp_n_allowedips == 0)
4646 goto skip_allowedips;
4647
4648 prop_array_t allowedips = prop_array_create();
4649 if (allowedips == NULL)
4650 goto next;
4651 for (int j = 0; j < wgp->wgp_n_allowedips; j++) {
4652 struct wg_allowedip *wga = &wgp->wgp_allowedips[j];
4653 prop_dictionary_t prop_allowedip;
4654
4655 prop_allowedip = prop_dictionary_create();
4656 if (prop_allowedip == NULL)
4657 break;
4658
4659 if (!prop_dictionary_set_int(prop_allowedip, "family",
4660 wga->wga_family))
4661 goto _next;
4662 if (!prop_dictionary_set_uint8(prop_allowedip, "cidr",
4663 wga->wga_cidr))
4664 goto _next;
4665
4666 switch (wga->wga_family) {
4667 case AF_INET:
4668 if (!prop_dictionary_set_data(prop_allowedip,
4669 "ip", &wga->wga_addr4,
4670 sizeof(wga->wga_addr4)))
4671 goto _next;
4672 break;
4673 #ifdef INET6
4674 case AF_INET6:
4675 if (!prop_dictionary_set_data(prop_allowedip,
4676 "ip", &wga->wga_addr6,
4677 sizeof(wga->wga_addr6)))
4678 goto _next;
4679 break;
4680 #endif
4681 default:
4682 break;
4683 }
4684 prop_array_set(allowedips, j, prop_allowedip);
4685 _next:
4686 prop_object_release(prop_allowedip);
4687 }
4688 prop_dictionary_set(prop_peer, "allowedips", allowedips);
4689 prop_object_release(allowedips);
4690
4691 skip_allowedips:
4692
4693 prop_array_set(peers, i, prop_peer);
4694 next:
4695 if (prop_peer)
4696 prop_object_release(prop_peer);
4697 i++;
4698
4699 s = pserialize_read_enter();
4700 wg_put_peer(wgp, &wgp_psref);
4701 }
4702 pserialize_read_exit(s);
4703
4704 prop_dictionary_set(prop_dict, "peers", peers);
4705 prop_object_release(peers);
4706 peers = NULL;
4707
4708 skip_peers:
4709 buf = prop_dictionary_externalize(prop_dict);
4710 if (buf == NULL)
4711 goto error;
4712 if (ifd->ifd_len < (strlen(buf) + 1)) {
4713 error = EINVAL;
4714 goto error;
4715 }
4716 error = copyout(buf, ifd->ifd_data, strlen(buf) + 1);
4717
4718 free(buf, 0);
4719 error:
4720 if (peers != NULL)
4721 prop_object_release(peers);
4722 if (prop_dict != NULL)
4723 prop_object_release(prop_dict);
4724
4725 return error;
4726 }
4727
4728 static int
4729 wg_ioctl(struct ifnet *ifp, u_long cmd, void *data)
4730 {
4731 struct wg_softc *wg = ifp->if_softc;
4732 struct ifreq *ifr = data;
4733 struct ifaddr *ifa = data;
4734 struct ifdrv *ifd = data;
4735 int error = 0;
4736
4737 switch (cmd) {
4738 case SIOCINITIFADDR:
4739 if (ifa->ifa_addr->sa_family != AF_LINK &&
4740 (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
4741 (IFF_UP | IFF_RUNNING)) {
4742 ifp->if_flags |= IFF_UP;
4743 error = if_init(ifp);
4744 }
4745 return error;
4746 case SIOCADDMULTI:
4747 case SIOCDELMULTI:
4748 switch (ifr->ifr_addr.sa_family) {
4749 case AF_INET: /* IP supports Multicast */
4750 break;
4751 #ifdef INET6
4752 case AF_INET6: /* IP6 supports Multicast */
4753 break;
4754 #endif
4755 default: /* Other protocols doesn't support Multicast */
4756 error = EAFNOSUPPORT;
4757 break;
4758 }
4759 return error;
4760 case SIOCSDRVSPEC:
4761 if (!wg_is_authorized(wg, cmd)) {
4762 return EPERM;
4763 }
4764 switch (ifd->ifd_cmd) {
4765 case WG_IOCTL_SET_PRIVATE_KEY:
4766 error = wg_ioctl_set_private_key(wg, ifd);
4767 break;
4768 case WG_IOCTL_SET_LISTEN_PORT:
4769 error = wg_ioctl_set_listen_port(wg, ifd);
4770 break;
4771 case WG_IOCTL_ADD_PEER:
4772 error = wg_ioctl_add_peer(wg, ifd);
4773 break;
4774 case WG_IOCTL_DELETE_PEER:
4775 error = wg_ioctl_delete_peer(wg, ifd);
4776 break;
4777 default:
4778 error = EINVAL;
4779 break;
4780 }
4781 return error;
4782 case SIOCGDRVSPEC:
4783 return wg_ioctl_get(wg, ifd);
4784 case SIOCSIFFLAGS:
4785 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
4786 break;
4787 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
4788 case IFF_RUNNING:
4789 /*
4790 * If interface is marked down and it is running,
4791 * then stop and disable it.
4792 */
4793 if_stop(ifp, 1);
4794 break;
4795 case IFF_UP:
4796 /*
4797 * If interface is marked up and it is stopped, then
4798 * start it.
4799 */
4800 error = if_init(ifp);
4801 break;
4802 default:
4803 break;
4804 }
4805 return error;
4806 #ifdef WG_RUMPKERNEL
4807 case SIOCSLINKSTR:
4808 error = wg_ioctl_linkstr(wg, ifd);
4809 if (error == 0)
4810 wg->wg_ops = &wg_ops_rumpuser;
4811 return error;
4812 #endif
4813 default:
4814 break;
4815 }
4816
4817 error = ifioctl_common(ifp, cmd, data);
4818
4819 #ifdef WG_RUMPKERNEL
4820 if (!wg_user_mode(wg))
4821 return error;
4822
4823 /* Do the same to the corresponding tun device on the host */
4824 /*
4825 * XXX Actually the command has not been handled yet. It
4826 * will be handled via pr_ioctl form doifioctl later.
4827 */
4828 switch (cmd) {
4829 case SIOCAIFADDR:
4830 case SIOCDIFADDR: {
4831 struct in_aliasreq _ifra = *(const struct in_aliasreq *)data;
4832 struct in_aliasreq *ifra = &_ifra;
4833 KASSERT(error == ENOTTY);
4834 strncpy(ifra->ifra_name, rumpuser_wg_get_tunname(wg->wg_user),
4835 IFNAMSIZ);
4836 error = rumpuser_wg_ioctl(wg->wg_user, cmd, ifra, AF_INET);
4837 if (error == 0)
4838 error = ENOTTY;
4839 break;
4840 }
4841 #ifdef INET6
4842 case SIOCAIFADDR_IN6:
4843 case SIOCDIFADDR_IN6: {
4844 struct in6_aliasreq _ifra = *(const struct in6_aliasreq *)data;
4845 struct in6_aliasreq *ifra = &_ifra;
4846 KASSERT(error == ENOTTY);
4847 strncpy(ifra->ifra_name, rumpuser_wg_get_tunname(wg->wg_user),
4848 IFNAMSIZ);
4849 error = rumpuser_wg_ioctl(wg->wg_user, cmd, ifra, AF_INET6);
4850 if (error == 0)
4851 error = ENOTTY;
4852 break;
4853 }
4854 #endif
4855 }
4856 #endif /* WG_RUMPKERNEL */
4857
4858 return error;
4859 }
4860
4861 static int
4862 wg_init(struct ifnet *ifp)
4863 {
4864
4865 ifp->if_flags |= IFF_RUNNING;
4866
4867 /* TODO flush pending packets. */
4868 return 0;
4869 }
4870
4871 #ifdef ALTQ
4872 static void
4873 wg_start(struct ifnet *ifp)
4874 {
4875 struct mbuf *m;
4876
4877 for (;;) {
4878 IFQ_DEQUEUE(&ifp->if_snd, m);
4879 if (m == NULL)
4880 break;
4881
4882 kpreempt_disable();
4883 const uint32_t h = curcpu()->ci_index; // pktq_rps_hash(m)
4884 if (__predict_false(!pktq_enqueue(wg_pktq, m, h))) {
4885 WGLOG(LOG_ERR, "%s: pktq full, dropping\n",
4886 if_name(ifp));
4887 m_freem(m);
4888 }
4889 kpreempt_enable();
4890 }
4891 }
4892 #endif
4893
4894 static void
4895 wg_stop(struct ifnet *ifp, int disable)
4896 {
4897
4898 KASSERT((ifp->if_flags & IFF_RUNNING) != 0);
4899 ifp->if_flags &= ~IFF_RUNNING;
4900
4901 /* Need to do something? */
4902 }
4903
4904 #ifdef WG_DEBUG_PARAMS
4905 SYSCTL_SETUP(sysctl_net_wg_setup, "sysctl net.wg setup")
4906 {
4907 const struct sysctlnode *node = NULL;
4908
4909 sysctl_createv(clog, 0, NULL, &node,
4910 CTLFLAG_PERMANENT,
4911 CTLTYPE_NODE, "wg",
4912 SYSCTL_DESCR("wg(4)"),
4913 NULL, 0, NULL, 0,
4914 CTL_NET, CTL_CREATE, CTL_EOL);
4915 sysctl_createv(clog, 0, &node, NULL,
4916 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4917 CTLTYPE_QUAD, "rekey_after_messages",
4918 SYSCTL_DESCR("session liftime by messages"),
4919 NULL, 0, &wg_rekey_after_messages, 0, CTL_CREATE, CTL_EOL);
4920 sysctl_createv(clog, 0, &node, NULL,
4921 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4922 CTLTYPE_INT, "rekey_after_time",
4923 SYSCTL_DESCR("session liftime"),
4924 NULL, 0, &wg_rekey_after_time, 0, CTL_CREATE, CTL_EOL);
4925 sysctl_createv(clog, 0, &node, NULL,
4926 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4927 CTLTYPE_INT, "rekey_timeout",
4928 SYSCTL_DESCR("session handshake retry time"),
4929 NULL, 0, &wg_rekey_timeout, 0, CTL_CREATE, CTL_EOL);
4930 sysctl_createv(clog, 0, &node, NULL,
4931 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4932 CTLTYPE_INT, "rekey_attempt_time",
4933 SYSCTL_DESCR("session handshake timeout"),
4934 NULL, 0, &wg_rekey_attempt_time, 0, CTL_CREATE, CTL_EOL);
4935 sysctl_createv(clog, 0, &node, NULL,
4936 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4937 CTLTYPE_INT, "keepalive_timeout",
4938 SYSCTL_DESCR("keepalive timeout"),
4939 NULL, 0, &wg_keepalive_timeout, 0, CTL_CREATE, CTL_EOL);
4940 sysctl_createv(clog, 0, &node, NULL,
4941 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4942 CTLTYPE_BOOL, "force_underload",
4943 SYSCTL_DESCR("force to detemine under load"),
4944 NULL, 0, &wg_force_underload, 0, CTL_CREATE, CTL_EOL);
4945 sysctl_createv(clog, 0, &node, NULL,
4946 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4947 CTLTYPE_INT, "debug",
4948 SYSCTL_DESCR("set debug flags 1=log 2=trace 4=dump 8=packet"),
4949 NULL, 0, &wg_debug, 0, CTL_CREATE, CTL_EOL);
4950 }
4951 #endif
4952
4953 #ifdef WG_RUMPKERNEL
4954 static bool
4955 wg_user_mode(struct wg_softc *wg)
4956 {
4957
4958 return wg->wg_user != NULL;
4959 }
4960
4961 static int
4962 wg_ioctl_linkstr(struct wg_softc *wg, struct ifdrv *ifd)
4963 {
4964 struct ifnet *ifp = &wg->wg_if;
4965 int error;
4966
4967 if (ifp->if_flags & IFF_UP)
4968 return EBUSY;
4969
4970 if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
4971 /* XXX do nothing */
4972 return 0;
4973 } else if (ifd->ifd_cmd != 0) {
4974 return EINVAL;
4975 } else if (wg->wg_user != NULL) {
4976 return EBUSY;
4977 }
4978
4979 /* Assume \0 included */
4980 if (ifd->ifd_len > IFNAMSIZ) {
4981 return E2BIG;
4982 } else if (ifd->ifd_len < 1) {
4983 return EINVAL;
4984 }
4985
4986 char tun_name[IFNAMSIZ];
4987 error = copyinstr(ifd->ifd_data, tun_name, ifd->ifd_len, NULL);
4988 if (error != 0)
4989 return error;
4990
4991 if (strncmp(tun_name, "tun", 3) != 0)
4992 return EINVAL;
4993
4994 error = rumpuser_wg_create(tun_name, wg, &wg->wg_user);
4995
4996 return error;
4997 }
4998
4999 static int
5000 wg_send_user(struct wg_peer *wgp, struct mbuf *m)
5001 {
5002 int error;
5003 struct psref psref;
5004 struct wg_sockaddr *wgsa;
5005 struct wg_softc *wg = wgp->wgp_sc;
5006 struct iovec iov[1];
5007
5008 wgsa = wg_get_endpoint_sa(wgp, &psref);
5009
5010 iov[0].iov_base = mtod(m, void *);
5011 iov[0].iov_len = m->m_len;
5012
5013 /* Send messages to a peer via an ordinary socket. */
5014 error = rumpuser_wg_send_peer(wg->wg_user, wgsatosa(wgsa), iov, 1);
5015
5016 wg_put_sa(wgp, wgsa, &psref);
5017
5018 m_freem(m);
5019
5020 return error;
5021 }
5022
5023 static void
5024 wg_input_user(struct ifnet *ifp, struct mbuf *m, const int af)
5025 {
5026 struct wg_softc *wg = ifp->if_softc;
5027 struct iovec iov[2];
5028 struct sockaddr_storage ss;
5029
5030 KASSERT(af == AF_INET || af == AF_INET6);
5031
5032 WG_TRACE("");
5033
5034 if (af == AF_INET) {
5035 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
5036 struct ip *ip;
5037
5038 KASSERT(m->m_len >= sizeof(struct ip));
5039 ip = mtod(m, struct ip *);
5040 sockaddr_in_init(sin, &ip->ip_dst, 0);
5041 } else {
5042 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
5043 struct ip6_hdr *ip6;
5044
5045 KASSERT(m->m_len >= sizeof(struct ip6_hdr));
5046 ip6 = mtod(m, struct ip6_hdr *);
5047 sockaddr_in6_init(sin6, &ip6->ip6_dst, 0, 0, 0);
5048 }
5049
5050 iov[0].iov_base = &ss;
5051 iov[0].iov_len = ss.ss_len;
5052 iov[1].iov_base = mtod(m, void *);
5053 iov[1].iov_len = m->m_len;
5054
5055 WG_DUMP_BUF(iov[1].iov_base, iov[1].iov_len);
5056
5057 /* Send decrypted packets to users via a tun. */
5058 rumpuser_wg_send_user(wg->wg_user, iov, 2);
5059
5060 m_freem(m);
5061 }
5062
5063 static int
5064 wg_bind_port_user(struct wg_softc *wg, const uint16_t port)
5065 {
5066 int error;
5067 uint16_t old_port = wg->wg_listen_port;
5068
5069 if (port != 0 && old_port == port)
5070 return 0;
5071
5072 error = rumpuser_wg_sock_bind(wg->wg_user, port);
5073 if (error == 0)
5074 wg->wg_listen_port = port;
5075 return error;
5076 }
5077
5078 /*
5079 * Receive user packets.
5080 */
5081 void
5082 rumpkern_wg_recv_user(struct wg_softc *wg, struct iovec *iov, size_t iovlen)
5083 {
5084 struct ifnet *ifp = &wg->wg_if;
5085 struct mbuf *m;
5086 const struct sockaddr *dst;
5087
5088 WG_TRACE("");
5089
5090 dst = iov[0].iov_base;
5091
5092 m = m_gethdr(M_DONTWAIT, MT_DATA);
5093 if (m == NULL)
5094 return;
5095 m->m_len = m->m_pkthdr.len = 0;
5096 m_copyback(m, 0, iov[1].iov_len, iov[1].iov_base);
5097
5098 WG_DLOG("iov_len=%zu\n", iov[1].iov_len);
5099 WG_DUMP_BUF(iov[1].iov_base, iov[1].iov_len);
5100
5101 (void)wg_output(ifp, m, dst, NULL);
5102 }
5103
5104 /*
5105 * Receive packets from a peer.
5106 */
5107 void
5108 rumpkern_wg_recv_peer(struct wg_softc *wg, struct iovec *iov, size_t iovlen)
5109 {
5110 struct mbuf *m;
5111 const struct sockaddr *src;
5112 int bound;
5113
5114 WG_TRACE("");
5115
5116 src = iov[0].iov_base;
5117
5118 m = m_gethdr(M_DONTWAIT, MT_DATA);
5119 if (m == NULL)
5120 return;
5121 m->m_len = m->m_pkthdr.len = 0;
5122 m_copyback(m, 0, iov[1].iov_len, iov[1].iov_base);
5123
5124 WG_DLOG("iov_len=%zu\n", iov[1].iov_len);
5125 WG_DUMP_BUF(iov[1].iov_base, iov[1].iov_len);
5126
5127 bound = curlwp_bind();
5128 wg_handle_packet(wg, m, src);
5129 curlwp_bindx(bound);
5130 }
5131 #endif /* WG_RUMPKERNEL */
5132
5133 /*
5134 * Module infrastructure
5135 */
5136 #include "if_module.h"
5137
5138 IF_MODULE(MODULE_CLASS_DRIVER, wg, "sodium,blake2s")
5139