1*981ba490Sjmc.\" $OpenBSD: wg.4,v 1.10 2021/03/14 10:08:38 jmc Exp $ 228057f2dSdlg.\" Copyright (c) 2020 Matt Dunwoodie <ncon@noconroy.net> 328057f2dSdlg.\" 428057f2dSdlg.\" Permission to use, copy, modify, and distribute this software for any 528057f2dSdlg.\" purpose with or without fee is hereby granted, provided that the above 628057f2dSdlg.\" copyright notice and this permission notice appear in all copies. 728057f2dSdlg.\" 828057f2dSdlg.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 928057f2dSdlg.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1028057f2dSdlg.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1128057f2dSdlg.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1228057f2dSdlg.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1328057f2dSdlg.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1428057f2dSdlg.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1528057f2dSdlg.\" 16*981ba490Sjmc.Dd $Mdocdate: March 14 2021 $ 1728057f2dSdlg.Dt WG 4 1828057f2dSdlg.Os 1928057f2dSdlg.Sh NAME 2028057f2dSdlg.Nm wg 2128057f2dSdlg.Nd WireGuard pseudo-device 2228057f2dSdlg.Sh SYNOPSIS 2328057f2dSdlg.Cd "pseudo-device wg" 2428057f2dSdlg.Sh DESCRIPTION 2528057f2dSdlgThe 2628057f2dSdlg.Nm wg 27fee707d3Sprocterdriver provides Virtual Private Network (VPN) interfaces for the secure 28fee707d3Sprocterexchange of layer 3 traffic with other WireGuard peers using the WireGuard 29fee707d3Sprocterprotocol. 3028057f2dSdlg.Pp 31fee707d3SprocterA 32fee707d3Sprocter.Nm wg 33fee707d3Sprocterinterface recognises one or more peers, establishes a secure tunnel with 34fee707d3Sproctereach on demand, and tracks each peer's UDP endpoint for exchanging encrypted 35fee707d3Sproctertraffic with. 3628057f2dSdlg.Pp 3728057f2dSdlgThe interfaces can be created at runtime using the 3828057f2dSdlg.Ic ifconfig Cm wg Ns Ar N Cm create 3928057f2dSdlgcommand or by setting up a 4028057f2dSdlg.Xr hostname.if 5 4128057f2dSdlgconfiguration file for 4228057f2dSdlg.Xr netstart 8 . 4328057f2dSdlgThe interface itself can be configured with 4428057f2dSdlg.Xr ifconfig 8 . 4528057f2dSdlg.Pp 4628057f2dSdlg.Nm wg 4728057f2dSdlginterfaces support the following 4828057f2dSdlg.Xr ioctl 2 Ns s : 4928057f2dSdlg.Bl -tag -width Ds -offset indent 5028057f2dSdlg.It Dv SIOCSWG Fa "struct wg_data_io *" 5128057f2dSdlgSet the device configuration. 5228057f2dSdlg.It Dv SIOCGWG Fa "struct wg_data_io *" 5328057f2dSdlgGet the device configuration. 5428057f2dSdlg.El 5528057f2dSdlg.Pp 56fee707d3SprocterThe following glossary provides a brief overview of WireGuard 57fee707d3Sprocterterminology: 5828057f2dSdlg.Bl -tag -width indent -offset 3n 5928057f2dSdlg.It Peer 60fee707d3SprocterPeers exchange IPv4 or IPv6 traffic over secure tunnels. 61fee707d3SprocterEach 62fee707d3Sprocter.Nm wg 63fee707d3Sprocterinterface may be configured to recognise one or more peers. 6428057f2dSdlg.It Key 65fee707d3SprocterEach peer uses its private key and corresponding public key to 66fee707d3Sprocteridentify itself to others. 67fee707d3SprocterA peer configures a 68fee707d3Sprocter.Nm wg 69fee707d3Sprocterinterface with its own private key and with the public keys of its peers. 7028057f2dSdlg.It Preshared key 71fee707d3SprocterIn addition to the public keys, each peer pair may be configured with a 72fee707d3Sprocterunique pre-shared symmetric key. 73fee707d3SprocterThis is used in their handshake to guard against future compromise of the 74fee707d3Sprocterpeers' encrypted tunnel if a quantum-computational attack on their 75fee707d3SprocterDiffie-Hellman exchange becomes feasible. 76ade9e6ceSjmcIt is optional, but recommended. 7728057f2dSdlg.It Allowed IPs 78fee707d3SprocterA single 79fee707d3Sprocter.Nm wg 80fee707d3Sprocterinterface may maintain concurrent tunnels connecting diverse networks. 81fee707d3SprocterThe interface therefore implements rudimentary routing and reverse-path 82fee707d3Sprocterfiltering functions for its tunneled traffic. 83fee707d3SprocterThese functions reference a set of allowed IP ranges configured against 84fee707d3Sproctereach peer. 8528057f2dSdlg.Pp 86fee707d3SprocterThe interface will route outbound tunneled traffic to the peer configured 87fee707d3Sprocterwith the most specific matching allowed IP address range, or drop it 88fee707d3Sprocterif no such match exists. 89fee707d3Sprocter.Pp 90fee707d3SprocterThe interface will accept tunneled traffic only from the peer 91fee707d3Sprocterconfigured with the most specific matching allowed IP address range 92fee707d3Sprocterfor the incoming traffic, or drop it if no such match exists. 93fee707d3SprocterThat is, tunneled traffic routed to a given peer cannot return through 94fee707d3Sprocteranother peer of the same 95fee707d3Sprocter.Nm wg 96fee707d3Sprocterinterface. 97fee707d3SprocterThis ensures that peers cannot spoof another's traffic. 9828057f2dSdlg.It Handshake 99fee707d3SprocterTwo peers handshake to mutually authenticate each other and to 100fee707d3Sprocterestablish a shared series of secret ephemeral encryption keys. 101fee707d3SprocterAny peer may initiate a handshake. 102fee707d3SprocterHandshakes occur only when there is traffic to send, and recur every 103fee707d3Sproctertwo minutes during transfers. 10428057f2dSdlg.It Connectionless 10528057f2dSdlgDue to the handshake behavior, there is no connected or disconnected 10628057f2dSdlgstate. 10728057f2dSdlg.El 10828057f2dSdlg.Ss Keys 10928057f2dSdlgPrivate keys for WireGuard can be generated from any sufficiently 11028057f2dSdlgsecure random source. 11128057f2dSdlgThe Curve25519 keys and the preshared keys are both 32 bytes 11228057f2dSdlglong and are commonly encoded in base64 for ease of use. 11328057f2dSdlg.Pp 11428057f2dSdlgKeys can be generated with 11528057f2dSdlg.Xr openssl 1 11628057f2dSdlgas follows: 11728057f2dSdlg.Pp 11828057f2dSdlg.Dl $ openssl rand -base64 32 11928057f2dSdlg.Pp 120fee707d3SprocterAlthough a valid Curve25519 key must have 5 bits set to 121fee707d3Sprocterspecific values, this is done by the interface and so it 122fee707d3Sprocterwill accept any random 32-byte base64 string. 12328057f2dSdlg.Pp 12428057f2dSdlgWhen an interface has a private key set with 12528057f2dSdlg.Nm wgkey , 12628057f2dSdlgthe corresponding 127361a4d5aSsthenpublic key is shown in the status output of the interface: 128361a4d5aSsthen.Bd -literal -offset indent 129e3cf4c66Ssthen# ifconfig wg1 | grep wgpubkey 130303f3f5fStobhe wgpubkey NW5l2q2MArV5ZXpVXSZwBOyqhohOf8ImDgUB+jPtJps= 13128057f2dSdlg.Ed 13228057f2dSdlg.Sh EXAMPLES 13328057f2dSdlgCreate two 13428057f2dSdlg.Nm wg 13528057f2dSdlginterfaces in separate 13628057f2dSdlg.Xr rdomain 4 Ns s , 13728057f2dSdlgwhich is of no practical use 13828057f2dSdlgbut demonstrates two interfaces on the same machine: 139361a4d5aSsthen.Bd -literal -offset indent 14028057f2dSdlg#!/bin/sh 14128057f2dSdlg 14227830fd8Ssthen# create interfaces; set random private keys 143f31e8cceSsthenifconfig wg1 create wgport 7111 wgkey `openssl rand -base64 32` rdomain 1 144f31e8cceSsthenifconfig wg2 create wgport 7222 wgkey `openssl rand -base64 32` rdomain 2 14528057f2dSdlg 14627830fd8Ssthen# retrieve the public keys associated with the private keys 147303f3f5fStobhePUB1="`ifconfig wg1 | grep 'wgpubkey' | cut -d ' ' -f 2`" 148303f3f5fStobhePUB2="`ifconfig wg2 | grep 'wgpubkey' | cut -d ' ' -f 2`" 14928057f2dSdlg 150f31e8cceSsthenifconfig wg1 wgpeer $PUB2 wgendpoint 127.0.0.1 7222 wgaip 192.168.5.2/32 151f31e8cceSsthenifconfig wg2 wgpeer $PUB1 wgendpoint 127.0.0.1 7111 wgaip 192.168.5.1/32 15228057f2dSdlgifconfig wg1 192.168.5.1/24 15328057f2dSdlgifconfig wg2 192.168.5.2/24 15428057f2dSdlg.Ed 15528057f2dSdlg.Pp 15628057f2dSdlgAfter this, ping one interface from the other: 157ade9e6ceSjmc.Pp 158ade9e6ceSjmc.Dl $ route -T1 exec ping 192.168.5.2 15928057f2dSdlg.Pp 16028057f2dSdlgThe two interfaces are able to communicate through the UDP tunnel 16128057f2dSdlgwhich resides in the default 16228057f2dSdlg.Xr rdomain 4 . 16328057f2dSdlg.Pp 16428057f2dSdlgShow the listening sockets: 165ade9e6ceSjmc.Pp 166ade9e6ceSjmc.Dl $ netstat -ln 16728057f2dSdlg.Sh DIAGNOSTICS 16828057f2dSdlgThe 16928057f2dSdlg.Nm 17028057f2dSdlginterface supports runtime debugging, which can be enabled with: 17128057f2dSdlg.Pp 17228057f2dSdlg.D1 Ic ifconfig Cm wg Ns Ar N Cm debug 17328057f2dSdlg.Pp 17428057f2dSdlgSome common error messages include: 17528057f2dSdlg.Bl -diag 17628057f2dSdlg.It "Handshake for peer X did not complete after 5 seconds, retrying" 17728057f2dSdlgPeer X did not reply to our initiation packet, for example because: 17828057f2dSdlg.Bl -bullet 17928057f2dSdlg.It 18028057f2dSdlgThe peer does not have the local interface configured as a peer. 181ade9e6ceSjmcPeers must be able to mutually authenticate each other. 18228057f2dSdlg.It 18328057f2dSdlgThe peer endpoint IP address is incorrectly configured. 18428057f2dSdlg.It 18528057f2dSdlgThere are firewall rules preventing communication between hosts. 18628057f2dSdlg.El 18728057f2dSdlg.It "Invalid handshake initiation" 18828057f2dSdlgThe incoming handshake packet could not be processed. 18928057f2dSdlgThis is likely due to the local interface not containing 19028057f2dSdlgthe correct public key for the peer. 19128057f2dSdlg.It "Invalid initiation MAC" 19228057f2dSdlgThe incoming handshake initiation packet had an invalid MAC. 19328057f2dSdlgThis is likely because the initiation sender has the wrong public key 19428057f2dSdlgfor the handshake receiver. 19528057f2dSdlg.It "Packet has unallowed src IP from peer X" 19628057f2dSdlgAfter decryption, an incoming data packet has a source IP address that 19728057f2dSdlgis not assigned to the allowed IPs of Peer X. 19828057f2dSdlg.El 19928057f2dSdlg.Sh SEE ALSO 20028057f2dSdlg.Xr inet 4 , 20128057f2dSdlg.Xr ip 4 , 20228057f2dSdlg.Xr netintro 4 , 20328057f2dSdlg.Xr hostname.if 5 , 20428057f2dSdlg.Xr pf.conf 5 , 20528057f2dSdlg.Xr ifconfig 8 , 20605bb5c20Sjmc.Xr netstart 8 20728057f2dSdlg.Rs 20828057f2dSdlg.%T WireGuard whitepaper 20928057f2dSdlg.%U https://www.wireguard.com/papers/wireguard.pdf 21028057f2dSdlg.Re 211*981ba490Sjmc.Sh HISTORY 212*981ba490SjmcThe 213*981ba490Sjmc.Nm 214*981ba490Sjmcdriver first appeared in 215*981ba490Sjmc.Ox 6.8 . 21628057f2dSdlg.Sh AUTHORS 21728057f2dSdlg.An -nosplit 21828057f2dSdlgThe 21928057f2dSdlg.Ox 22028057f2dSdlg.Nm 22128057f2dSdlgdriver was developed by 22228057f2dSdlg.An Matt Dunwoodie Aq Mt ncon@noconroy.net 22328057f2dSdlgand 224ade9e6ceSjmc.An Jason A. Donenfeld Aq Mt Jason@zx2c4.com , 22528057f2dSdlgbased on code written by 22628057f2dSdlg.An Jason A. Donenfeld . 227