2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019-2020 Rubicon Communications, LLC (Netgate)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/socket.h>
35 #include <net/if_var.h>
36 #include <sys/support.h>
39 #include <sys/types.h>
40 #include <sys/epoch.h>
42 #include <sys/mutex.h>
46 #include <crypto/curve25519.h>
47 #include <zinc/chacha20poly1305.h>
48 #include <crypto/blake2s.h>
52 NOISE_PUBLIC_KEY_LEN = CURVE25519_KEY_SIZE,
53 NOISE_SYMMETRIC_KEY_LEN = CHACHA20POLY1305_KEY_SIZE,
54 NOISE_TIMESTAMP_LEN = sizeof(uint64_t) + sizeof(uint32_t),
55 NOISE_AUTHTAG_LEN = CHACHA20POLY1305_AUTHTAG_SIZE,
56 NOISE_HASH_LEN = BLAKE2S_HASH_SIZE
59 #define noise_encrypted_len(plain_len) ((plain_len) + NOISE_AUTHTAG_LEN)
62 COOKIE_SECRET_MAX_AGE = 2 * 60,
63 COOKIE_SECRET_LATENCY = 5,
64 COOKIE_NONCE_LEN = XCHACHA20POLY1305_NONCE_SIZE,
70 INITIATIONS_PER_SECOND = 50,
71 MAX_PEERS_PER_DEVICE = 1U << 20,
72 KEEPALIVE_TIMEOUT = 10,
73 MAX_TIMER_HANDSHAKES = 90 / REKEY_TIMEOUT,
74 MAX_QUEUED_INCOMING_HANDSHAKES = 4096, /* TODO: replace this with DQL */
75 MAX_STAGED_PACKETS = 128,
76 MAX_QUEUED_PACKETS = 1024 /* TODO: replace this with DQL */
79 #define zfree(addr, type) \
81 explicit_bzero(addr, sizeof(*addr)); \
93 #define __ATOMIC_LOAD_SIZE \
96 case 1: *(uint8_t *)res = *(volatile uint8_t *)p; break; \
97 case 2: *(uint16_t *)res = *(volatile uint16_t *)p; break; \
98 case 4: *(uint32_t *)res = *(volatile uint32_t *)p; break; \
99 case 8: *(uint64_t *)res = *(volatile uint64_t *)p; break; \
104 __atomic_load_acq_size(volatile void *p, void *res, int size)
109 #define atomic_load_acq(x) \
111 union { __typeof(x) __val; char __c[1]; } __u; \
112 __atomic_load_acq_size(&(x), __u.__c, sizeof(x)); \
117 int wg_ctx_init(void);
118 void wg_ctx_uninit(void);