1.\" $OpenBSD: wg.4,v 1.5 2020/09/29 19:37:08 procter Exp $ 2.\" Copyright (c) 2020 Matt Dunwoodie <ncon@noconroy.net> 3.\" 4.\" Permission to use, copy, modify, and distribute this software for any 5.\" purpose with or without fee is hereby granted, provided that the above 6.\" copyright notice and this permission notice appear in all copies. 7.\" 8.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15.\" 16.Dd $Mdocdate: September 29 2020 $ 17.Dt WG 4 18.Os 19.Sh NAME 20.Nm wg 21.Nd WireGuard pseudo-device 22.Sh SYNOPSIS 23.Cd "pseudo-device wg" 24.Sh DESCRIPTION 25The 26.Nm wg 27driver provides Virtual Private Network (VPN) interfaces for the secure 28exchange of layer 3 traffic with other WireGuard peers using the WireGuard 29protocol. 30.Pp 31A 32.Nm wg 33interface recognises one or more peers, establishes a secure tunnel with 34each on demand, and tracks each peer's UDP endpoint for exchanging encrypted 35traffic with. 36.Pp 37The interfaces can be created at runtime using the 38.Ic ifconfig Cm wg Ns Ar N Cm create 39command or by setting up a 40.Xr hostname.if 5 41configuration file for 42.Xr netstart 8 . 43The interface itself can be configured with 44.Xr ifconfig 8 . 45.Pp 46.Nm wg 47interfaces support the following 48.Xr ioctl 2 Ns s : 49.Bl -tag -width Ds -offset indent 50.It Dv SIOCSWG Fa "struct wg_data_io *" 51Set the device configuration. 52.It Dv SIOCGWG Fa "struct wg_data_io *" 53Get the device configuration. 54.El 55.Pp 56The following glossary provides a brief overview of WireGuard 57terminology: 58.Bl -tag -width indent -offset 3n 59.It Peer 60Peers exchange IPv4 or IPv6 traffic over secure tunnels. 61Each 62.Nm wg 63interface may be configured to recognise one or more peers. 64.It Key 65Each peer uses its private key and corresponding public key to 66identify itself to others. 67A peer configures a 68.Nm wg 69interface with its own private key and with the public keys of its peers. 70.It Preshared key 71In addition to the public keys, each peer pair may be configured with a 72unique pre-shared symmetric key. 73This is used in their handshake to guard against future compromise of the 74peers' encrypted tunnel if a quantum-computational attack on their 75Diffie-Hellman exchange becomes feasible. 76It is optional, but recommended. 77.It Allowed IPs 78A single 79.Nm wg 80interface may maintain concurrent tunnels connecting diverse networks. 81The interface therefore implements rudimentary routing and reverse-path 82filtering functions for its tunneled traffic. 83These functions reference a set of allowed IP ranges configured against 84each peer. 85.Pp 86The interface will route outbound tunneled traffic to the peer configured 87with the most specific matching allowed IP address range, or drop it 88if no such match exists. 89.Pp 90The interface will accept tunneled traffic only from the peer 91configured with the most specific matching allowed IP address range 92for the incoming traffic, or drop it if no such match exists. 93That is, tunneled traffic routed to a given peer cannot return through 94another peer of the same 95.Nm wg 96interface. 97This ensures that peers cannot spoof another's traffic. 98.It Handshake 99Two peers handshake to mutually authenticate each other and to 100establish a shared series of secret ephemeral encryption keys. 101Any peer may initiate a handshake. 102Handshakes occur only when there is traffic to send, and recur every 103two minutes during transfers. 104.It Connectionless 105Due to the handshake behavior, there is no connected or disconnected 106state. 107.El 108.Ss Keys 109Private keys for WireGuard can be generated from any sufficiently 110secure random source. 111The Curve25519 keys and the preshared keys are both 32 bytes 112long and are commonly encoded in base64 for ease of use. 113.Pp 114Keys can be generated with 115.Xr openssl 1 116as follows: 117.Pp 118.Dl $ openssl rand -base64 32 119.Pp 120Although a valid Curve25519 key must have 5 bits set to 121specific values, this is done by the interface and so it 122will accept any random 32-byte base64 string. 123.Pp 124When an interface has a private key set with 125.Nm wgkey , 126the corresponding 127public key is shown in the status output of the interface, like so: 128.Bd -literal -offset indent 129wgpubkey NW5l2q2MArV5ZXpVXSZwBOyqhohOf8ImDgUB+jPtJps= 130.Ed 131.Sh EXAMPLES 132Create two 133.Nm wg 134interfaces in separate 135.Xr rdomain 4 Ns s , 136which is of no practical use 137but demonstrates two interfaces on the same machine: 138.Bd -literal 139#!/bin/sh 140 141ifconfig wg1 create wgport 111 wgkey `openssl rand -base64 32` rdomain 1 142ifconfig wg2 create wgport 222 wgkey `openssl rand -base64 32` rdomain 2 143 144PUB1="`ifconfig wg1 | grep 'wgpubkey' | cut -d ' ' -f 2`" 145PUB2="`ifconfig wg2 | grep 'wgpubkey' | cut -d ' ' -f 2`" 146 147ifconfig wg1 wgpeer $PUB2 wgendpoint 127.0.0.1 222 wgaip 192.168.5.2/32 148ifconfig wg2 wgpeer $PUB1 wgendpoint 127.0.0.1 111 wgaip 192.168.5.1/32 149ifconfig wg1 192.168.5.1/24 150ifconfig wg2 192.168.5.2/24 151.Ed 152.Pp 153After this, ping one interface from the other: 154.Pp 155.Dl $ route -T1 exec ping 192.168.5.2 156.Pp 157The two interfaces are able to communicate through the UDP tunnel 158which resides in the default 159.Xr rdomain 4 . 160.Pp 161Show the listening sockets: 162.Pp 163.Dl $ netstat -ln 164.Sh DIAGNOSTICS 165The 166.Nm 167interface supports runtime debugging, which can be enabled with: 168.Pp 169.D1 Ic ifconfig Cm wg Ns Ar N Cm debug 170.Pp 171Some common error messages include: 172.Bl -diag 173.It "Handshake for peer X did not complete after 5 seconds, retrying" 174Peer X did not reply to our initiation packet, for example because: 175.Bl -bullet 176.It 177The peer does not have the local interface configured as a peer. 178Peers must be able to mutually authenticate each other. 179.It 180The peer endpoint IP address is incorrectly configured. 181.It 182There are firewall rules preventing communication between hosts. 183.El 184.It "Invalid handshake initiation" 185The incoming handshake packet could not be processed. 186This is likely due to the local interface not containing 187the correct public key for the peer. 188.It "Invalid initiation MAC" 189The incoming handshake initiation packet had an invalid MAC. 190This is likely because the initiation sender has the wrong public key 191for the handshake receiver. 192.It "Packet has unallowed src IP from peer X" 193After decryption, an incoming data packet has a source IP address that 194is not assigned to the allowed IPs of Peer X. 195.El 196.Sh SEE ALSO 197.Xr inet 4 , 198.Xr ip 4 , 199.Xr netintro 4 , 200.Xr hostname.if 5 , 201.Xr pf.conf 5 , 202.Xr ifconfig 8 , 203.Xr netstart 8 204.Rs 205.%T WireGuard whitepaper 206.%U https://www.wireguard.com/papers/wireguard.pdf 207.Re 208.Sh AUTHORS 209.An -nosplit 210The 211.Ox 212.Nm 213driver was developed by 214.An Matt Dunwoodie Aq Mt ncon@noconroy.net 215and 216.An Jason A. Donenfeld Aq Mt Jason@zx2c4.com , 217based on code written by 218.An Jason A. Donenfeld . 219