# Family wireguard netlink specification

_Peios Kernel / Internals / Netlink / specs_

> Protocol reference for the wireguard Netlink family, generated from the Peios kernel specification.

<!-- SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) -->


<a id="rst-family-wireguard-netlink-specification"></a>
<a id="rst-netlink-wireguard"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-summary"></a>



## Summary



**Netlink protocol to control WireGuard network devices.**



The below enums and macros are for interfacing with WireGuard, using generic
netlink, with family `WG_GENL_NAME` and version `WG_GENL_VERSION`. It
defines two commands: get and set. Note that while they share many common
attributes, these two commands actually accept a slightly different set of
inputs and outputs. These differences are noted under the individual
attributes.

<a id="rst-operations"></a>



## Operations

<a id="rst-get-device"></a>
<a id="rst-wireguard-operation-get-device"></a>



### get-device

<a id="rst-retrieve-wireguard-device"></a>



#### Retrieve WireGuard device



The command should be called with one but not both of:

- `WGDEVICE_A_IFINDEX`


- `WGDEVICE_A_IFNAME`




The kernel will then return several messages (`NLM_F_MULTI`). It is
possible that all of the allowed IPs of a single peer will not fit
within a single netlink message. In that case, the same peer will be
written in the following message, except it will only contain
`WGPEER_A_PUBLIC_KEY` and `WGPEER_A_ALLOWEDIPS`. This may occur
several times in a row for the same peer. It is then up to the receiver
to coalesce adjacent peers. Likewise, it is possible that all peers will
not fit within a single message. So, subsequent peers will be sent in
following messages, except those will only contain `WGDEVICE_A_IFNAME`
and `WGDEVICE_A_PEERS`. It is then up to the receiver to coalesce
these messages to form the complete list of peers.



Since this is an `NLA_F_DUMP` command, the final message will always
be `NLMSG_DONE`, even if an error occurs. However, this `NLMSG_DONE`
message contains an integer error code. It is either zero or a negative
error code corresponding to the errno.

- **value**:

  0

- **attribute-set**:

  [wgdevice](#rst-wireguard-attribute-set-wgdevice)

- **flags**:

  \[`uns-admin-perm`\]

- **dump**:

  **pre**



  wg-get-device-start

  **post**



  wg-get-device-done

  **request**

  - **attributes**:

    \[`ifindex`, `ifname`\]

  **reply**

  - **attributes**:

    \[`ifindex`, `ifname`, `private-key`, `public-key`, `flags`, `listen-port`, `fwmark`, `peers`\]

<a id="rst-set-device"></a>
<a id="rst-wireguard-operation-set-device"></a>



### set-device

<a id="rst-set-wireguard-device"></a>



#### Set WireGuard device



This command should be called with a wgdevice set, containing one but
not both of `WGDEVICE_A_IFINDEX` and `WGDEVICE_A_IFNAME`.



It is possible that the amount of configuration data exceeds that of the
maximum message length accepted by the kernel. In that case, several
messages should be sent one after another, with each successive one
filling in information not contained in the prior. Note that if
`WGDEVICE_F_REPLACE_PEERS` is specified in the first message, it
probably should not be specified in fragments that come after, so that
the list of peers is only cleared the first time but appended after.
Likewise for peers, if `WGPEER_F_REPLACE_ALLOWEDIPS` is specified in
the first message of a peer, it likely should not be specified in
subsequent fragments.



If an error occurs, `NLMSG_ERROR` will reply containing an errno.

- **value**:

  1

- **attribute-set**:

  [wgdevice](#rst-wireguard-attribute-set-wgdevice)

- **flags**:

  \[`uns-admin-perm`\]

- **do**:

  **request**

  - **attributes**:

    \[`ifindex`, `ifname`, `private-key`, `public-key`, `flags`, `listen-port`, `fwmark`, `peers`\]

<a id="rst-definitions"></a>



## Definitions

<a id="rst-key-len"></a>
<a id="rst-wireguard-definition-key-len"></a>



### key-len

- **name-prefix**:

  wg-

- **type**:

  const

- **value**:

  32

<a id="rst-kernel-timespec"></a>
<a id="rst-wireguard-definition-kernel-timespec"></a>



### --kernel-timespec

- **type**:

  struct

- **header**:

  linux/time\_types.h

- **members**:

  - **sec (`u64`)**:

    Number of seconds, since UNIX epoch.

  - **nsec (`u64`)**:

    Number of nanoseconds, after the second began.

<a id="rst-wgdevice-flags"></a>
<a id="rst-wireguard-definition-wgdevice-flags"></a>



### wgdevice-flags

- **name-prefix**:

  wgdevice-f-

- **enum-name**:

  wgdevice-flag

- **type**:

  flags

- **entries**:

  - `replace-peers`

<a id="rst-wgpeer-flags"></a>
<a id="rst-wireguard-definition-wgpeer-flags"></a>



### wgpeer-flags

- **name-prefix**:

  wgpeer-f-

- **enum-name**:

  wgpeer-flag

- **type**:

  flags

- **entries**:

  - `remove-me`


  - `replace-allowedips`


  - `update-only`

<a id="rst-wgallowedip-flags"></a>
<a id="rst-wireguard-definition-wgallowedip-flags"></a>



### wgallowedip-flags

- **name-prefix**:

  wgallowedip-f-

- **enum-name**:

  wgallowedip-flag

- **type**:

  flags

- **entries**:

  - `remove-me`

<a id="rst-attribute-sets"></a>



## Attribute sets

<a id="rst-wgdevice"></a>
<a id="rst-wireguard-attribute-set-wgdevice"></a>



### wgdevice

<a id="rst-unspec-unused"></a>



#### unspec (`unused`)

- **value**:

  0

<a id="rst-ifindex-u32"></a>



#### ifindex (`u32`)

<a id="rst-ifname-string"></a>



#### ifname (`string`)

<a id="rst-private-key-binary"></a>



#### private-key (`binary`)

- **doc**:

  Set to all zeros to remove.

- **display-hint**:

  hex

<a id="rst-public-key-binary"></a>



#### public-key (`binary`)

- **display-hint**:

  hex

<a id="rst-flags-u32"></a>



#### flags (`u32`)

- **doc**:

  `0` or `WGDEVICE_F_REPLACE_PEERS` if all current peers should be removed prior to adding the list below.

- **enum**:

  [wgdevice-flags](#rst-wireguard-definition-wgdevice-flags)

<a id="rst-listen-port-u16"></a>



#### listen-port (`u16`)

- **doc**:

  Set as `0` to choose randomly.

<a id="rst-fwmark-u32"></a>



#### fwmark (`u32`)

- **doc**:

  Set as `0` to disable.

<a id="rst-peers-indexed-array"></a>



#### peers (`indexed-array`)

- **sub-type**:

  nest

- **nested-attributes**:

  [wgpeer](#rst-wireguard-attribute-set-wgpeer)

- **doc**:

  The index/type parameter is unused on `SET_DEVICE` operations and is zero on `GET_DEVICE` operations.

<a id="rst-wgpeer"></a>
<a id="rst-wireguard-attribute-set-wgpeer"></a>



### wgpeer

<a id="rst-unspec-unused-1"></a>



#### unspec (`unused`)

- **value**:

  0

<a id="rst-public-key-binary-1"></a>



#### public-key (`binary`)

- **display-hint**:

  hex

<a id="rst-preshared-key-binary"></a>



#### preshared-key (`binary`)

- **doc**:

  Set as all zeros to remove.

- **display-hint**:

  hex

<a id="rst-flags-u32-1"></a>



#### flags (`u32`)

- **doc**:

  `0` and/or `WGPEER_F_REMOVE_ME` if the specified peer should not exist at the end of the operation, rather than added/updated and/or `WGPEER_F_REPLACE_ALLOWEDIPS` if all current allowed IPs of this peer should be removed prior to adding the list below and/or `WGPEER_F_UPDATE_ONLY` if the peer should only be set if it already exists.

- **enum**:

  [wgpeer-flags](#rst-wireguard-definition-wgpeer-flags)

<a id="rst-endpoint-binary"></a>



#### endpoint (`binary`)

- **doc**:

  struct sockaddr\_in or struct sockaddr\_in6

<a id="rst-persistent-keepalive-interval-u16"></a>



#### persistent-keepalive-interval (`u16`)

- **doc**:

  Set as `0` to disable.

<a id="rst-last-handshake-time-binary"></a>



#### last-handshake-time (`binary`)

- **struct**:

  [--kernel-timespec](#rst-wireguard-definition-kernel-timespec)

<a id="rst-rx-bytes-u64"></a>



#### rx-bytes (`u64`)

<a id="rst-tx-bytes-u64"></a>



#### tx-bytes (`u64`)

<a id="rst-allowedips-indexed-array"></a>



#### allowedips (`indexed-array`)

- **sub-type**:

  nest

- **nested-attributes**:

  [wgallowedip](#rst-wireguard-attribute-set-wgallowedip)

- **doc**:

  The index/type parameter is unused on `SET_DEVICE` operations and is zero on `GET_DEVICE` operations.

<a id="rst-protocol-version-u32"></a>



#### protocol-version (`u32`)

- **doc**:

  Should not be set or used at all by most users of this API, as the most recent protocol will be used when this is unset. Otherwise, must be set to `1`.

<a id="rst-wgallowedip"></a>
<a id="rst-wireguard-attribute-set-wgallowedip"></a>



### wgallowedip

<a id="rst-unspec-unused-2"></a>



#### unspec (`unused`)

- **value**:

  0

<a id="rst-family-u16"></a>



#### family (`u16`)

- **doc**:

  IP family, either `AF_INET` or `AF_INET6`.

<a id="rst-ipaddr-binary"></a>



#### ipaddr (`binary`)

- **doc**:

  Either `struct in_addr` or `struct in6_addr`.

- **display-hint**:

  ipv4-or-v6

<a id="rst-cidr-mask-u8"></a>



#### cidr-mask (`u8`)

<a id="rst-flags-u32-2"></a>



#### flags (`u32`)

- **doc**:

  `WGALLOWEDIP_F_REMOVE_ME` if the specified IP should be removed; otherwise, this IP will be added if it is not already present.

- **enum**:

  [wgallowedip-flags](#rst-wireguard-definition-wgallowedip-flags)
