1.\" $NetBSD: ipsec.4,v 1.46 2024/03/18 22:47:19 gutteridge Exp $ 2.\" $KAME: ipsec.4,v 1.17 2001/06/27 15:25:10 itojun Exp $ 3.\" 4.\" Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the project nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.Dd June 13, 2018 32.Dt IPSEC 4 33.Os 34.Sh NAME 35.Nm ipsec 36.Nd IP security protocol 37.Sh SYNOPSIS 38.Cd "options IPSEC" 39.Cd "options IPSEC_DEBUG" 40.Sh DESCRIPTION 41This manual page describes the IPsec protocol. 42For the network device driver please see 43.Xr ipsecif 4 . 44.Pp 45.Nm 46is a security protocol in the Internet Protocol (IP) layer. 47.Nm 48is defined for both IPv4 and IPv6 49.Po 50.Xr inet 4 51and 52.Xr inet6 4 53.Pc . 54.Nm 55consists of three sub-protocols: 56.Bl -hang 57.It Em Encapsulated Security Payload Pq ESP 58protects IP payloads from wire-tapping (interception) by encrypting them with 59secret key cryptography algorithms. 60.It Em Authentication Header Pq AH 61guarantees the integrity of IP packets 62and protects them from intermediate alteration or impersonation, 63by attaching cryptographic checksums computed by one-way hash functions. 64.It Em IP Payload Compression Protocol Pq IPComp 65increases the communication performance by compressing the datagrams. 66.El 67.Pp 68.Nm 69has two operation modes: 70.Bl -hang 71.It Em Transport mode 72is for protecting peer-to-peer communication between end nodes. 73.It Em Tunnel mode 74includes IP-in-IP encapsulation operation 75and is designed for security gateways, as in Virtual Private Network (VPN) 76configurations. 77.El 78.Ss Kernel interface 79.Nm 80is controlled by two engines in the kernel: one for key management 81and one for policy. 82.Pp 83The key management engine can be accessed from userland by using 84.Dv PF_KEY 85sockets. 86The 87.Dv PF_KEY 88socket API is defined in RFC2367. 89.Pp 90The policy engine can be controlled through the 91.Dv PF_KEY 92API, 93.Xr setsockopt 2 94operations, and 95the 96.Xr sysctl 3 97interface. 98The kernel implements an 99extended version of the 100.Dv PF_KEY 101interface and allows you to define IPsec policy like per-packet filters. 102.Xr setsockopt 2 103is used to define per-socket behavior, and 104.Xr sysctl 3 105is used to define host-wide default behavior. 106.Pp 107The kernel does not implement dynamic encryption key exchange protocols 108like IKE 109.Pq Internet Key Exchange . 110That should be done in userland 111.Pq usually as a daemon , 112using the APIs described above. 113.\" 114.Ss Policy management 115The kernel implements experimental policy management code. 116You can manage the IPsec policy in two ways. 117One is to configure per-socket policy using 118.Xr setsockopt 2 . 119The other is to configure kernel packet filter-based policy using the 120.Dv PF_KEY 121interface, via 122.Xr setkey 8 . 123In both cases, IPsec policy must be specified with syntax described in 124.Xr ipsec_set_policy 3 . 125.Pp 126With 127.Xr setsockopt 2 , 128you can define IPsec policy on a per-socket basis. 129You can enforce particular IPsec policy on packets that go through a 130particular socket. 131.Pp 132With 133.Xr setkey 8 134you can define IPsec policy for packets using a form of packet 135filtering rules. 136See 137.Xr setkey 8 138for details. 139.Pp 140In the latter case, 141.Dq Li default 142policy is allowed for use with 143.Xr setkey 8 . 144By configuring policy to 145.Li default , 146you can refer to system-wide 147.Xr sysctl 8 148variables for default settings. 149The following variables are available. 150.Li 1 151means 152.Dq Li use , 153and 154.Li 2 155means 156.Dq Li require 157in the syntax. 158.Bl -column net.inet6.ipsec6.esp_trans_deflev integerxxx 159.It Sy Name Ta Sy Type Ta Sy Changeable 160.It net.inet.ipsec.esp_trans_deflev Ta integer Ta yes 161.It net.inet.ipsec.esp_net_deflev Ta integer Ta yes 162.It net.inet.ipsec.ah_trans_deflev Ta integer Ta yes 163.It net.inet.ipsec.ah_net_deflev Ta integer Ta yes 164.It net.inet6.ipsec6.esp_trans_deflev Ta integer Ta yes 165.It net.inet6.ipsec6.esp_net_deflev Ta integer Ta yes 166.It net.inet6.ipsec6.ah_trans_deflev Ta integer Ta yes 167.It net.inet6.ipsec6.ah_net_deflev Ta integer Ta yes 168.El 169.Pp 170If the kernel finds no matching policy, the system-wide default 171value is applied. 172System-wide defaults are specified by the following 173.Xr sysctl 8 174variables. 175.Li 0 176means 177.Dq Li discard 178which asks the kernel to drop the packet. 179.Li 1 180means 181.Dq Li none . 182.Bl -column net.inet6.ipsec6.esp_trans_deflev integerxxx 183.It Sy Name Ta Sy Type Ta Sy Changeable 184.It net.inet.ipsec.def_policy Ta integer Ta yes 185.It net.inet6.ipsec6.def_policy Ta integer Ta yes 186.El 187.\" 188.Ss Miscellaneous sysctl variables 189The following variables are accessible via 190.Xr sysctl 8 , 191for tweaking kernel IPsec behavior: 192.Bl -column net.inet6.ipsec6.esp_trans_deflev integerxxx 193.It Sy Name Ta Sy Type Ta Sy Changeable 194.It net.inet.ipsec.ah_cleartos Ta integer Ta yes 195.It net.inet.ipsec.ah_offsetmask Ta integer Ta yes 196.It net.inet.ipsec.crypto_support Ta integer Ta yes 197.It net.inet.ipsec.dfbit Ta integer Ta yes 198.It net.inet.ipsec.ecn Ta integer Ta yes 199.It net.inet.ipsec.debug Ta integer Ta yes 200.It net.inet6.ipsec6.ecn Ta integer Ta yes 201.It net.inet6.ipsec6.debug Ta integer Ta yes 202.El 203.Pp 204The variables are interpreted as follows: 205.Bl -tag -width "123456" 206.It Li ipsec.ah_cleartos 207If set to non-zero, the kernel clears the type-of-service field in the 208IPv4 header during AH authentication data computation. 209The variable is for tweaking AH behavior to interoperate with devices that 210implement RFC1826 AH. 211It should be set to non-zero 212.Pq clear the type-of-service field 213for RFC2402 conformance. 214.It Li ipsec.ah_offsetmask 215During AH authentication data computation, the kernel will include a 21616 bit fragment offset field 217.Pq including flag bits 218in the IPv4 header, after computing logical AND with the variable. 219The variable is for tweaking AH behavior to interoperate with devices that 220implement RFC1826 AH. 221It should be set to zero 222.Pq clear the fragment offset field during computation 223for RFC2402 conformance. 224.It Li ipsec.crypto_support 225This variable configures the kernel behavior for selecting encryption drivers. 226If set to > 0, the kernel will select a hardware encryption driver first. 227If set to < 0, the kernel will select a software encryption driver first. 228If set to 0, the kernel will select either a hardware or software driver. 229.It Li ipsec.dfbit 230This variable configures the kernel behavior on IPv4 IPsec tunnel encapsulation. 231If set to 0, the DF bit on the outer IPv4 header will be cleared. 2321 means that the outer DF bit is set from the inner DF bit. 2332 means that the DF bit is copied from the inner header to the outer. 234The variable is supplied to conform to RFC2401 chapter 6.1. 235.It Li ipsec.ecn 236If set to non-zero, IPv4 IPsec tunnel encapsulation/decapsulation behavior will 237be friendly to ECN 238.Pq explicit congestion notification , 239as documented in 240.Li draft-ietf-ipsec-ecn-02.txt . 241.Xr gif 4 242talks more about the behavior. 243.It Li ipsec.debug 244If set to non-zero, debug messages will be generated via 245.Xr syslog 3 . 246.El 247.Pp 248Variables under the 249.Li net.inet6.ipsec6 250tree have similar meanings to their 251.Li net.inet.ipsec 252counterparts. 253.Ss Cryptographic operations 254The current IPsec implementation, formerly called Fast IPsec, 255uses the 256.Xr opencrypto 9 257subsystem to carry out cryptographic operations. 258This means, in particular, that cryptographic hardware devices are 259employed whenever possible to optimize the performance of sub-protocols. 260.Pp 261System configuration requires the 262.Xr opencrypto 9 263subsystem. 264When the 265Fast IPsec 266protocols are configured for use, all protocols are included in the system. 267To selectively enable/disable protocols, use 268.Xr sysctl 8 . 269.\" 270.Sh PROTOCOLS 271The 272.Nm 273protocol works like a plug-in to 274.Xr inet 4 275and 276.Xr inet6 4 277protocols. 278Therefore, 279.Nm 280supports most of the protocols defined upon those IP-layer protocols. 281Some of the protocols, like 282.Xr icmp 4 283or 284.Xr icmp6 4 , 285may behave differently with 286.Nm ipsec . 287This is because 288.Nm 289can prevent 290.Xr icmp 4 291or 292.Xr icmp6 4 293routines from looking into IP payload. 294.\" 295.Sh SEE ALSO 296.Xr ioctl 2 , 297.Xr socket 2 , 298.Xr ipsec_set_policy 3 , 299.Xr icmp6 4 , 300.Xr intro 4 , 301.Xr ip6 4 , 302.Xr ipsecif 4 , 303.Xr racoon 8 , 304.Xr setkey 8 , 305.Xr sysctl 8 306.Sh STANDARDS 307.Rs 308.%A Daniel L. McDonald 309.%A Craig Metz 310.%A Bao G. Phan 311.%T "PF_KEY Key Management API, Version 2" 312.%R RFC 313.%N 2367 314.Re 315.Sh HISTORY 316The protocols draw heavily on the 317.Ox 318implementation of the 319.Tn IPsec 320protocols. 321The policy management code is derived from the 322.Tn KAME 323implementation found in their 324.Tn IPsec 325protocols. 326The 327Fast IPsec 328protocols are based on code which appeared in 329.Fx 4.7 . 330The 331.Nx 332version is a close copy of the 333.Fx 334original, and first appeared in 335.Nx 2.0 . 336.Pp 337Support for IPv6 and 338.Tn IPcomp 339protocols has been added in 340.Nx 4.0 . 341.Pp 342Support for Network Address Translator Traversal as 343described in RFCs 3947 and 3948 has been added in 344.Nx 5.0 . 345.Pp 346Since 347.Nx 6.0 , 348the IPsec implementation formerly known as Fast IPsec is used. 349.Sh BUGS 350IPsec support is subject to change as the IPsec protocols develop. 351.Pp 352There is no single standard for policy engine API, 353so the policy engine API described herein is just for the version 354introduced by KAME. 355.Pp 356AH and tunnel mode encapsulation may not work as you might expect. 357If you configure inbound 358.Dq require 359policy against AH tunnel or any IPsec encapsulating policy with AH 360.Po 361like 362.Dq Li esp/tunnel/A-B/use ah/transport/A-B/require 363.Pc , 364tunneled packets will be rejected. 365This is because we enforce policy check on inner packet on reception, 366and AH authenticates encapsulating 367.Pq outer 368packet, not the encapsulated 369.Pq inner 370packet 371.Po 372so for the receiving kernel there's no sign of authenticity 373.Pc . 374The issue will be solved when we revamp our policy engine to keep all the 375packet decapsulation history. 376.Pp 377Under certain conditions, truncated results may be raised from the kernel 378against 379.Dv SADB_DUMP 380and 381.Dv SADB_SPDDUMP 382operations on 383.Dv PF_KEY 384sockets. 385This occurs if there are too many database entries in the kernel 386and socket buffer for the 387.Dv PF_KEY 388socket is insufficient. 389If you manipulate many IPsec key/policy database entries, 390increase the size of socket buffer or use 391.Xr sysctl 8 392interface. 393.Pp 394Certain legacy authentication algorithms are not supported because of 395issues with the 396.Xr opencrypto 9 397subsystem. 398