xref: /dragonfly/sys/net/wg/if_wg.h (revision 1671e44324064c7cb19c4845dfbcfba1037f65fa)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
5  * Copyright (C) 2019-2020 Matt Dunwoodie <ncon@noconroy.net>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 /*
20  * This is the public interface to the WireGuard network interface.
21  *
22  * It is designed to be used by tools such as ifconfig(8) and wg(8).
23  */
24 
25 #ifndef _NET_IF_WG_H_
26 #define _NET_IF_WG_H_
27 
28 #include <sys/ioccom.h>
29 #include <sys/time.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
32 
33 #define WG_KEY_SIZE 32
34 
35 /* Maximum length of the peer description, including the terminating NUL. */
36 #define WG_PEER_DESCR_SIZE 64
37 
38 #define SIOCSWG _IOWR('i', 210, struct wg_data_io)
39 #define SIOCGWG _IOWR('i', 211, struct wg_data_io)
40 
41 struct wg_aip_io {
42           sa_family_t          a_af;
43           int                  a_cidr;
44           union {
45                     struct in_addr                addr_ipv4;
46                     struct in6_addr               addr_ipv6;
47           }                    a_addr;
48 };
49 
50 #define a_ipv4      a_addr.addr_ipv4
51 #define a_ipv6      a_addr.addr_ipv6
52 
53 struct wg_peer_io {
54           int                           p_flags; /* WG_PEER_* */
55           uint8_t                       p_public[WG_KEY_SIZE];
56           uint8_t                       p_psk[WG_KEY_SIZE]; /* preshared key */
57           uint16_t            p_pka; /* persistent keepalive */
58           union {
59                     struct sockaddr               sa_sa;
60                     struct sockaddr_in  sa_sin;
61                     struct sockaddr_in6 sa_sin6;
62           }                             p_endpoint;
63           uint64_t            p_txbytes;
64           uint64_t            p_rxbytes;
65           struct timespec               p_last_handshake; /* nanotime */
66           uint64_t            p_id;
67           char                          p_description[WG_PEER_DESCR_SIZE];
68           size_t                        p_aips_count;
69           struct wg_aip_io    p_aips[];
70 };
71 
72 #define p_sa        p_endpoint.sa_sa
73 #define p_sin       p_endpoint.sa_sin
74 #define p_sin6      p_endpoint.sa_sin6
75 
76 #define WG_PEER_HAS_PUBLIC              (1 << 0)
77 #define WG_PEER_HAS_PSK                           (1 << 1)
78 #define WG_PEER_HAS_PKA                           (1 << 2)
79 #define WG_PEER_HAS_ENDPOINT            (1 << 3)
80 #define WG_PEER_REPLACE_AIPS            (1 << 4)
81 #define WG_PEER_REMOVE                            (1 << 5)
82 #define WG_PEER_UPDATE                            (1 << 6)
83 #define WG_PEER_SET_DESCRIPTION                   (1 << 7)
84 
85 struct wg_interface_io {
86           int                           i_flags; /* WG_INTERFACE_* */
87           in_port_t           i_port;
88           uint32_t            i_cookie;
89           uint8_t                       i_public[WG_KEY_SIZE];
90           uint8_t                       i_private[WG_KEY_SIZE];
91           size_t                        i_peers_count;
92           struct wg_peer_io   i_peers[];
93 };
94 
95 #define WG_INTERFACE_HAS_PUBLIC                   (1 << 0)
96 #define WG_INTERFACE_HAS_PRIVATE        (1 << 1)
97 #define WG_INTERFACE_HAS_PORT           (1 << 2)
98 #define WG_INTERFACE_HAS_COOKIE                   (1 << 3)
99 #define WG_INTERFACE_REPLACE_PEERS      (1 << 4)
100 
101 struct wg_data_io {
102           char                           wgd_name[IFNAMSIZ];
103           size_t                         wgd_size; /* size of wgd_interface */
104           struct wg_interface_io        *wgd_interface;
105 };
106 
107 #endif /* _NET_IF_WG_H_ */
108