1 /* SPDX-License-Identifier: ISC */
2 /*
3  * Copyright (C) 2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  * Copyright (c) 2020 Matt Dunwoodie <ncon@noconroy.net>
5  */
6 
7 #ifndef __IF_WG_H__
8 #define __IF_WG_H__
9 
10 #include <sys/limits.h>
11 #include <sys/errno.h>
12 
13 #include <net/if.h>
14 #include <netinet/in.h>
15 
16 
17 /*
18  * This is the public interface to the WireGuard network interface.
19  *
20  * It is designed to be used by tools such as ifconfig(8) and wg(8).
21  */
22 
23 #define WG_KEY_LEN 32
24 
25 #define SIOCSWG _IOWR('i', 210, struct wg_data_io)
26 #define SIOCGWG _IOWR('i', 211, struct wg_data_io)
27 
28 #define a_ipv4	a_addr.addr_ipv4
29 #define a_ipv6	a_addr.addr_ipv6
30 
31 struct wg_aip_io {
32 	sa_family_t	 a_af;
33 	int		 a_cidr;
34 	union wg_aip_addr {
35 		struct in_addr		addr_ipv4;
36 		struct in6_addr		addr_ipv6;
37 	}		 a_addr;
38 };
39 
40 #define WG_PEER_HAS_PUBLIC		(1 << 0)
41 #define WG_PEER_HAS_PSK			(1 << 1)
42 #define WG_PEER_HAS_PKA			(1 << 2)
43 #define WG_PEER_HAS_ENDPOINT		(1 << 3)
44 #define WG_PEER_REPLACE_AIPS		(1 << 4)
45 #define WG_PEER_REMOVE			(1 << 5)
46 #define WG_PEER_UPDATE			(1 << 6)
47 
48 #define p_sa		p_endpoint.sa_sa
49 #define p_sin		p_endpoint.sa_sin
50 #define p_sin6		p_endpoint.sa_sin6
51 
52 struct wg_peer_io {
53 	int			p_flags;
54 	int			p_protocol_version;
55 	uint8_t			p_public[WG_KEY_LEN];
56 	uint8_t			p_psk[WG_KEY_LEN];
57 	uint16_t		p_pka;
58 	union wg_peer_endpoint {
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 	size_t			p_aips_count;
67 	struct wg_aip_io	p_aips[];
68 };
69 
70 #define WG_INTERFACE_HAS_PUBLIC		(1 << 0)
71 #define WG_INTERFACE_HAS_PRIVATE	(1 << 1)
72 #define WG_INTERFACE_HAS_PORT		(1 << 2)
73 #define WG_INTERFACE_HAS_RTABLE		(1 << 3)
74 #define WG_INTERFACE_REPLACE_PEERS	(1 << 4)
75 
76 struct wg_interface_io {
77 	uint8_t			i_flags;
78 	in_port_t		i_port;
79 	int			i_rtable;
80 	uint8_t			i_public[WG_KEY_LEN];
81 	uint8_t			i_private[WG_KEY_LEN];
82 	size_t			i_peers_count;
83 	struct wg_peer_io	i_peers[];
84 };
85 
86 struct wg_data_io {
87 	char	 		 wgd_name[IFNAMSIZ];
88 	size_t			 wgd_size;	/* total size of the memory pointed to by wgd_interface */
89 	struct wg_interface_io	*wgd_interface;
90 };
91 
92 #endif /* __IF_WG_H__ */
93