1.\" SPDX-License-Identifier: BSD-2-Clause 2.\" 3.\" Copyright (c) 2020 Gordon Bergling <gbe@FreeBSD.org> 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.Dd February 12, 2025 27.Dt WG 4 28.Os 29.Sh NAME 30.Nm wg 31.Nd "WireGuard protocol driver" 32.Sh SYNOPSIS 33To compile this driver into the kernel, 34place the following lines in your kernel configuration file: 35.Bd -ragged -offset indent 36.Cd "device wg" 37.Ed 38.Pp 39Alternatively, to load the driver as a module at boot time, 40place the following line in 41.Xr loader.conf 5 : 42.Bd -literal -offset indent 43if_wg_load="YES" 44.Ed 45.Sh DESCRIPTION 46The 47.Nm 48driver provides Virtual Private Network (VPN) interfaces for the secure 49exchange of layer 3 traffic with other WireGuard peers using the WireGuard 50protocol. 51.Pp 52A 53.Nm 54interface recognizes one or more peers, establishes a secure tunnel with 55each on demand, and tracks each peer's UDP endpoint for exchanging encrypted 56traffic with. 57.Pp 58The interfaces can be created at runtime using the 59.Ic ifconfig Cm wg Ns Ar N Cm create 60command. 61The interface itself can be configured with 62.Xr wg 8 . 63.Pp 64The following glossary provides a brief overview of WireGuard 65terminology: 66.Bl -tag -width indent -offset 3n 67.It Peer 68Peers exchange IPv4 or IPv6 traffic over secure tunnels. 69Each 70.Nm 71interface may be configured to recognize one or more peers. 72.It Key 73Each peer uses its private key and corresponding public key to 74identify itself to others. 75A peer configures a 76.Nm 77interface with its own private key and with the public keys of its peers. 78.It Pre-shared key 79In addition to the public keys, each peer pair may be configured with a 80unique pre-shared symmetric key. 81This is used in their handshake to guard against future compromise of the 82peers' encrypted tunnel if an attack on their 83Diffie-Hellman exchange becomes feasible. 84It is optional, but recommended. 85.It Allowed IP addresses 86A single 87.Nm 88interface may maintain concurrent tunnels connecting diverse networks. 89The interface therefore implements rudimentary routing and reverse-path 90filtering functions for its tunneled traffic. 91These functions reference a set of allowed IP address ranges configured 92against each peer. 93.Pp 94The interface will route outbound tunneled traffic to the peer configured 95with the most specific matching allowed IP address range, or drop it 96if no such match exists. 97.Pp 98The interface will accept tunneled traffic only from the peer 99configured with the most specific matching allowed IP address range 100for the incoming traffic, or drop it if no such match exists. 101That is, tunneled traffic routed to a given peer cannot return through 102another peer of the same 103.Nm 104interface. 105This ensures that peers cannot spoof one another's traffic. 106.It Handshake 107Two peers handshake to mutually authenticate each other and to 108establish a shared series of secret ephemeral encryption keys. 109Either peer may initiate a handshake. 110Handshakes occur only when there is traffic to send, and recur every 111two minutes during transfers. 112.It Connectionless 113Due to the handshake behavior, there is no connected or disconnected 114state. 115.El 116.Ss Keys 117Private keys for WireGuard can be generated from any sufficiently 118secure random source. 119The Curve25519 keys and the pre-shared keys are both 32 bytes 120long and are commonly encoded in base64 for ease of use. 121.Pp 122Keys can be generated with 123.Xr wg 8 124as follows: 125.Pp 126.Dl $ wg genkey 127.Pp 128Although a valid Curve25519 key must have 5 bits set to 129specific values, this is done by the interface and so it 130will accept any random 32-byte base64 string. 131.Sh NETMAP 132.Xr netmap 4 133applications may open a WireGuard interface in emulated mode. 134The netmap application will receive decrypted, unencapsulated packets prepended 135by a dummy Ethernet header. 136The Ethertype field will be one of 137.Dv ETHERTYPE_IP 138or 139.Dv ETHERTYPE_IPV6 140depending on the address family of the packet. 141Packets transmitted by the application should similarly begin with a dummy 142Ethernet header; this header will be stripped before the packet is encrypted 143and tunneled. 144.Sh EXAMPLES 145Create a 146.Nm 147interface and set random private key. 148.Bd -literal -offset indent 149# ifconfig wg0 create 150# wg genkey | wg set wg0 listen-port 54321 private-key /dev/stdin 151.Ed 152.Pp 153Retrieve the associated public key from a 154.Nm 155interface. 156.Bd -literal -offset indent 157$ wg show wg0 public-key 158.Ed 159.Pp 160Connect to a specific endpoint using its public-key and set the allowed IP address 161.Bd -literal -offset indent 162# wg set wg0 peer '7lWtsDdqaGB3EY9WNxRN3hVaHMtu1zXw71+bOjNOVUw=' endpoint 10.0.1.100:54321 allowed-ips 192.168.2.100/32 163.Ed 164.Pp 165Remove a peer 166.Bd -literal -offset indent 167# wg set wg0 peer '7lWtsDdqaGB3EY9WNxRN3hVaHMtu1zXw71+bOjNOVUw=' remove 168.Ed 169.Sh DIAGNOSTICS 170The 171.Nm 172interface supports runtime debugging, which can be enabled with: 173.Pp 174.D1 Ic ifconfig Cm wg Ns Ar N Cm debug 175.Pp 176Some common error messages include: 177.Bl -diag 178.It "Handshake for peer X did not complete after 5 seconds, retrying" 179Peer X did not reply to our initiation packet, for example because: 180.Bl -bullet 181.It 182The peer does not have the local interface configured as a peer. 183Peers must be able to mutually authenticate each other. 184.It 185The peer endpoint IP address is incorrectly configured. 186.It 187There are firewall rules preventing communication between hosts. 188.El 189.It "Invalid handshake initiation" 190The incoming handshake packet could not be processed. 191This is likely due to the local interface not containing 192the correct public key for the peer. 193.It "Invalid initiation MAC" 194The incoming handshake initiation packet had an invalid MAC. 195This is likely because the initiation sender has the wrong public key 196for the handshake receiver. 197.It "Packet has unallowed src IP from peer X" 198After decryption, an incoming data packet has a source IP address that 199is not assigned to the allowed IPs of Peer X. 200.El 201.Sh SEE ALSO 202.Xr inet 4 , 203.Xr ip 4 , 204.Xr ipsec 4 , 205.Xr netintro 4 , 206.Xr netmap 4 , 207.Xr ovpn 4 , 208.Xr ipf 5 , 209.Xr pf.conf 5 , 210.Xr ifconfig 8 , 211.Xr ipfw 8 , 212.Xr wg 8 213.Rs 214.%T WireGuard whitepaper 215.%U https://www.wireguard.com/papers/wireguard.pdf 216.Re 217.Sh HISTORY 218The 219.Nm 220device driver first appeared in 221.Fx 13.2 . 222.Sh AUTHORS 223.An -nosplit 224The 225.Nm 226device driver was written by 227.An Jason A. Donenfeld Aq Mt Jason@zx2c4.com , 228.An Matt Dunwoodie Aq Mt ncon@nconroy.net , 229.An Kyle Evans Aq Mt kevans@FreeBSD.org , 230and 231.An Matt Macy Aq Mt mmacy@FreeBSD.org . 232.Pp 233This manual page was written by 234.An Gordon Bergling Aq Mt gbe@FreeBSD.org 235and is based on the 236.Ox 237manual page written by 238.An David Gwynne Aq Mt dlg@openbsd.org . 239