]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_wg/include/sys/wg_module.h
service(8): use an environment more consistent with init(8)
[FreeBSD/FreeBSD.git] / sys / dev / if_wg / include / sys / wg_module.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019-2020 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 #ifndef MODULE_H_
30 #define MODULE_H_
31
32 #include <sys/mbuf.h>
33 #include <sys/socket.h>
34 #include <net/if.h>
35 #include <net/if_var.h>
36 #include <sys/support.h>
37
38
39 #include <sys/types.h>
40 #include <sys/epoch.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43
44
45
46 #include <crypto/curve25519.h>
47 #include <zinc/chacha20poly1305.h>
48 #include <crypto/blake2s.h>
49
50
51 enum noise_lengths {
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
57 };
58
59 #define noise_encrypted_len(plain_len) ((plain_len) + NOISE_AUTHTAG_LEN)
60
61 enum cookie_values {
62         COOKIE_SECRET_MAX_AGE = 2 * 60,
63         COOKIE_SECRET_LATENCY = 5,
64         COOKIE_NONCE_LEN = XCHACHA20POLY1305_NONCE_SIZE,
65         COOKIE_LEN = 16
66 };
67
68 enum limits {
69         REKEY_TIMEOUT = 5,
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 */
77 };
78
79 #define zfree(addr, type)                                               \
80         do {                                                                            \
81                 explicit_bzero(addr, sizeof(*addr));    \
82                 free(addr, type);                                               \
83         } while (0)
84
85 struct crypt_queue {
86         union {
87                 struct {
88                         int last_cpu;
89                 };
90         };
91 };
92
93 #define __ATOMIC_LOAD_SIZE                                              \
94         ({                                                                      \
95         switch (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;              \
100         }                                                               \
101 })
102
103 static inline void
104 __atomic_load_acq_size(volatile void *p, void *res, int size)
105 {
106         __ATOMIC_LOAD_SIZE;
107 }
108
109 #define atomic_load_acq(x)                                              \
110         ({                                                                                      \
111         union { __typeof(x) __val; char __c[1]; } __u;                  \
112         __atomic_load_acq_size(&(x), __u.__c, sizeof(x));               \
113         __u.__val;                                                                                              \
114 })
115
116
117 int wg_ctx_init(void);
118 void wg_ctx_uninit(void);
119
120
121 #endif