SETKEY(8)
π SETKEY(8) β System Manager's Manual
bestie, welcome to setkey(8) β the manual page for manually manipulating the IPsec SA/SP database. Yes, itβs a lot. Yes, weβre getting through all of it. π«Ά
β¨ NAME β¨
setkey β manually manipulate the IPsec SA/SP database
πΈ SYNOPSIS πΈ
setkey [-v] -c
setkey [-v] -f filename
setkey [-v] -e script
setkey [-Pgltv] -D
setkey [-Pv] -F
setkey [-h] -x
π¦ DESCRIPTION π¦
Okay so the setkey utility adds, updates, dumps, or flushes Security Association Database (SAD) entries as well as Security Policy Database (SPD) entries in the kernel. Main character energy, honestly.
The setkey utility takes a series of operations from the standard input (if invoked with -c), from the file named filename (if invoked with -f filename), or from the command line argument following the option (if invoked with -e script). Pick your lane, bestie. π
-Dβ Dump the SAD entries. If itβs with-P, the SPD entries are dumped.-Fβ Flush the SAD entries. If itβs with-P, the SPD entries are flushed.-gβ Only SPD entries with global scope are dumped with-Dand-Pflags.-tβ Only SPD entries with ifnet scope are dumped with-Dand-Pflags. Such SPD entries are linked to the correspondingif_ipsec(4)virtual tunneling interface.-hβ Add hexadecimal dump on-xmode.-lβ Loop forever with short output on-D.-vβ Be verbose. The program will dump messages exchanged on PF_KEY socket, including messages sent from other processes to the kernel.-xβ Loop forever and dump all the messages transmitted to PF_KEY socket.-xxmakes each timestamp unformatted.
π Configuration syntax π
With -c or -f on the command line, setkey accepts the following configuration syntax. Lines starting with hash signs (#) are treated as comment lines. iykyk β¨
add [-46n] src dst protocol spi [extensions] algorithm ... ;β Add an SAD entry.addcan fail with multiple reasons, including when the key length does not match the specified algorithm.get [-46n] src dst protocol spi ;β Show an SAD entry.delete [-46n] src dst protocol spi ;β Remove an SAD entry.deleteall [-46n] src dst protocol ;β Remove all SAD entries that match the specification.flush [protocol] ;β Clear all SAD entries matched by the options.-Fon the command line achieves the same functionality.dump [protocol] ;β Dumps all SAD entries matched by the options.-Don the command line achieves the same functionality.spdadd [-46n] src_range dst_range upperspec policy ;β Add an SPD entry.spddelete [-46n] src_range dst_range upperspec -P direction ;β Delete an SPD entry.spdflush ;β Clear all SPD entries.-FPon the command line achieves the same functionality.spddump ;β Dumps all SPD entries.-DPon the command line achieves the same functionality.
Meta-arguments are as follows:
src,dstβ Source/destination of the secure communication is specified as IPv4/v6 address. Thesetkeyutility can resolve an FQDN into numeric addresses. If the FQDN resolves into multiple addresses,setkeywill install multiple SAD/SPD entries into the kernel by trying all possible combinations.-4,-6and-nrestricts the address resolution of FQDN in certain ways.-4and-6restrict results into IPv4/v6 addresses only, respectively.-navoids FQDN resolution and requires addresses to be numeric addresses.protocolβprotocolis one of following:espβ ESP based on rfc2406esp-oldβ ESP based on rfc1827ahβ AH based on rfc2402ah-oldβ AH based on rfc1826ipcompβ IPComptcpβ TCP-MD5 based on rfc2385
spiβ Security Parameter Index (SPI) for the SAD and the SPD.spimust be a decimal number, or a hexadecimal number with0xprefix. SPI values between0and255are reserved for future use by IANA and they cannot be used.extensionsβ take some of the following:-m modeβ Specify a security protocol mode for use.modeis one of following:transport,tunnelorany. The default value isany.-r sizeβ Specify the bitmap size in octets of the anti-replay window.sizeis a 32-bit unsigned integer, and its value is one eighth of the anti-replay window size in packets. Ifsizeis zero or not specified, an anti-replay check does not take place.-u idβ Specify the identifier of the policy entry in SPD. Seepolicy.-f pad_optionβ defines the content of the ESP padding.pad_optionis one of following:zero-padβ All of the padding are zero.random-padβ A series of randomized values are set.seq-padβ A series of sequential increasing numbers started from 1 are set.
-f nocyclic-seqβ Do not allow cyclic sequence number.-lh time,-ls timeβ Specify hard/soft life time duration of the SA.-natt oai [ sport ] oar [ dport ]β Manually configure NAT-T for the SA, by specifying initiatoroaiand requestoroarip addresses and ports. Note that the[and]symbols are part of the syntax for the ports specification, not indication of the optional components.-natt_mtu fragsizeβ Configure NAT-T fragment size.-esnβ Enable Extended Sequence Number extension for this SA.-hwif ifnameβ Request hardware offload to the specified interfaceifname(only). By default offload occurs to all capable interfaces.
algorithmβ these forms are giving:-E ealgo keyβ Specify an encryption or Authenticated Encryption with Associated Data (AEAD) algorithmealgofor ESP.-E ealgo key -A aalgo keyβ Specify a encryption algorithmealgo, as well as a payload authentication algorithmaalgo, for ESP.-A aalgo keyβ Specify an authentication algorithm for AH.-C calgo [-R]β Specify a compression algorithm for IPComp. If-Ris specified, thespifield value will be used as the IPComp CPI (compression parameter index) on wire as is. If-Ris not specified, the kernel will use well-known CPI on wire, andspifield will be used only as an index for kernel internal usage.
key must be double-quoted character string, or a series of hexadecimal digits preceded by 0x.
Possible values for ealgo, aalgo and calgo are specified in separate section.
src_range,dst_rangeβ These are selections of the secure communication specified as IPv4/v6 address or IPv4/v6 address range, and it may accompany TCP/UDP port specification. This takes the following form:
address
address/prefixlen
address[port]
address/prefixlen[port]
prefixlen and port must be a decimal number. The square brackets around port are necessary and are not manpage metacharacters. For FQDN resolution, the rules applicable to src and dst apply here as well.
upperspecβ The upper layer protocol to be used. You can use one of the words in/etc/protocolsasupperspec, as well asicmp6,ip4, orany. The wordanystands for βany protocolβ. The protocol number may also be used to specify theupperspec. A type and code related to ICMPv6 may also be specified as anupperspec. The type is specified first, followed by a comma and then the relevant code. The specification must be placed aftericmp6. The kernel considers a zero to be a wildcard but cannot distinguish between a wildcard and an ICMPv6 type which is zero. The following example shows a policy where IPSec is not required for inbound Neighbor Solicitations:
spdadd ::/0 ::/0 icmp6 135,0 -P in none;
NOTE:upperspecdoes not work in the forwarding case at this moment, as it requires extra reassembly at forwarding node, which is not implemented at this moment. Although there are many protocols in/etc/protocols, protocols other than TCP, UDP and ICMP may not be suitable to use with IPsec.
policyβpolicyis expressed in one of the following three formats:
-P direction discard
-P direction none
-P direction ipsec protocol/mode/src-dst/level [...]
directionβ The direction of a policy must be specified as one of:outorin.policy levelβ The direction is followed by one of the following policy levels:discard,none, oripsec.- The
discardpolicy level means that packets matching the supplied indices will be discarded. - The
nonepolicy level means that IPsec operations will not take place on the packet. - The
ipsecpolicy level means that IPsec operation will take place onto the packet.
- The
protocol/mode/src-dst/levelβ Theprotocol/mode/src-dst/levelstatement gives the rule for how to process the packet.- The
protocolis specified asah,esporipcomp. - The
modeis eithertransportortunnel.
- The
If mode is tunnel, you must specify the end-point addresses of the SA as src and dst with a dash, -, between the addresses.
If mode is transport, both src and dst can be omitted.
The level is one of the following: default, use, require or unique. If the SA is not available in every level, the kernel will request the SA from the key exchange daemon. π«Ά
defaultβ the kernel just uses the system-wide default protocol when it processes the packet, like the one from theesp_trans_deflevsysctlvariable. lowkey "whatever the house default is" energy πuseβ the kernel will use an SA if one's available, and if not it just passes the packet along as it normally would. so chill, so unbothered β¨requireβ an SA is required whenever the kernel sends a packet matching that policy. no SA, no vibes, no packet. periodt πuniqueβ same asrequire, but it also lets the policy bind with the unique out-bound SA. it's giving main character energy π
fr fr, like β if you set the policy level to unique, racoon(8) (ports/security/ipsec-tools) will configure the SA for that policy. and if you're out here configuring the SA by manual keying for that policy, you can drop the decimal number as the policy identifier after unique, separated by colon : β as in unique:number. for the bind to actually work, number has to be between 1 and 32767, which corresponds to extensions -u of manual SA configuration πΈ
When you want to use an SA bundle, bestie, you can define multiple rules. so if an IP header was followed by an AH header followed by an ESP header followed by an upper layer protocol header, the rule would be:
esp/transport//require ah/transport//require;
The rule order is very important. like, do not play with it π
Note that βdiscardβ and βnoneβ are not in the syntax described inipsec_set_policy(3). there are small but important differences in the syntax β seeipsec_set_policy(3)for details. iykyk π§
β¨ ALGORITHMS β¨
here's the whole roster of supported algorithms. slay π
Authentication Algorithms
these can be used as aalgo in the -A aalgo of the protocol parameter:
hmac-sha1160β ah/esp: rfc2404160β ah-old/esp-old: 128bit ICV (no document)
null0 to 2048β for debugging
hmac-sha2-256256β ah/esp: 128bit ICV (RFC4868)256β ah-old/esp-old: 128bit ICV (no document)
hmac-sha2-384384β ah/esp: 192bit ICV (RFC4868)384β ah-old/esp-old: 128bit ICV (no document)
hmac-sha2-512512β ah/esp: 256bit ICV (RFC4868)512β ah-old/esp-old: 128bit ICV (no document)
aes-xcbc-mac128β ah/esp: 96bit ICV (RFC3566)128β ah-old/esp-old: 128bit ICV (no document)
tcp-md58 to 640β tcp: rfc2385
chacha20-poly1305256β ah/esp: 128bit ICV (RFC7634)
Encryption Algorithms
these can be used as the ealgo in the -E ealgo of the protocol parameter:
null0 to 2048β rfc2410
aes-cbc128/192/256β rfc3602
aes-ctr160/224/288β rfc3686
aes-gcm-16160/224/288β AEAD; rfc4106
chacha20-poly1305256β rfc7634
heads up bestie: the first128/192/256bits of a key foraes-ctroraes-gcm-16get used as the AES key, and the remaining32bits get used as the nonce. don't @ me, that's just how it is π
AEAD encryption algorithms likeaes-gcm-16already include authentication, so do not pair them with a separate authentication algorithm via-A. that's a whole mess you do not need π
Compression Algorithms
these can be used as the calgo in the -C calgo of the protocol parameter:
deflateβ rfc2394
π EXIT STATUS π
The setkey utility exits 0 on success, and >0 if an error occurs. simple as that, icon behaviour β¨
π EXAMPLES π
Add an ESP SA between two IPv6 addresses using the AES-GCM AEAD algorithm.
add 3ffe:501:4819::1 3ffe:501:481d::1 esp 123457
-E aes-gcm-16 0x3ffe050148193ffe050148193ffe050148193ffe ;
Add an authentication SA between two FQDN specified hosts:
add -6 myhost.example.com yourhost.example.com ah 123456
-A hmac-sha2-256 "AH SA configuration!" ;
Get the SA information associated with first example above:
get 3ffe:501:4819::1 3ffe:501:481d::1 ah 123456 ;
Flush all entries from the database:
flush ;
Dump the ESP entries from the database:
dump esp ;
Add a security policy between two networks that uses ESP in tunnel mode:
spdadd 10.0.11.41/32[21] 10.0.11.33/32[any] any
-P out ipsec esp/tunnel/192.168.0.1-192.168.1.2/require ;
Use TCP MD5 between two numerically specified hosts:
add 10.1.10.34 10.1.10.36 tcp 0x1000 -A tcp-md5 "TCP-MD5 BGP secret" ;
add 10.1.10.36 10.1.10.34 tcp 0x1001 -A tcp-md5 "TCP-MD5 BGP secret" ;
π¦ SEE ALSO π¦
ipsec_set_policy(3)if_ipsec(4)racoon(8)(ports/security/ipsec-tools)sysctl(8)
also, changed manual key configuration for IPsec, https://www.kame.net/newsletter/19991007/, October 1999 β vintage but valid π
π HISTORY π
The setkey utility first appeared in WIDE Hydrangea IPv6 protocol stack kit. the utility was completely re-designed in June 1998, and it first appeared in FreeBSD 4.0. glow-up of the century tbh β
π BUGS π
The setkey utility should report and handle syntax errors better. literally could be doing more π
For IPsec gateway configuration, src_range and dst_range with TCP/UDP port number do not work, as the gateway does not reassemble packets (cannot inspect upper-layer headers). so that's a thing, no cap π
aight that's the whole vibe check, bestie β page courtesy of Debian, dated August 25, 2024, and it's SETKEY(8) signing off π«Άπ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms