1.\" $OpenBSD: ipsec.4,v 1.81 2014/11/16 19:57:24 schwarze Exp $ 2.\" 3.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by Niels Provos. 17.\" 4. The name of the author may not be used to endorse or promote products 18.\" derived from this software without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30.\" 31.\" Manual page, using -mandoc macros 32.\" 33.Dd $Mdocdate: November 16 2014 $ 34.Dt IPSEC 4 35.Os 36.Sh NAME 37.Nm ipsec 38.Nd IP Security Protocol 39.Sh DESCRIPTION 40IPsec is a pair of protocols, 41Encapsulating Security Payload (ESP) 42and Authentication Header (AH), 43which provide security services for IP datagrams. 44.Pp 45Both protocols may be enabled or disabled using the following 46.Xr sysctl 3 47variables in 48.Pa /etc/sysctl.conf . 49By default, both protocols are enabled: 50.Pp 51.Bl -tag -width "net.inet.esp.enableXX" -offset indent -compact 52.It net.inet.esp.enable 53Enable the ESP IPsec protocol 54.It net.inet.ah.enable 55Enable the AH IPsec protocol 56.El 57.Pp 58There are four main security properties provided by IPsec: 59.Bl -inset -offset indent 60.It Confidentiality 61\- Ensure it is hard for anyone but the 62receiver to understand what data has been communicated. 63For example, ensuring the secrecy of passwords when logging 64into a remote machine over the Internet. 65.It Integrity 66\- Guarantee that the data does not get changed 67in transit. 68If you are on a line carrying invoicing data you 69probably want to know that the amounts and account numbers 70are correct and have not been modified by a third party. 71.It Authenticity 72\- Sign your data so that others can see that it 73is really you that sent it. 74It is clearly nice to know that documents are not forged. 75.It Replay protection 76\- We need ways to ensure a datagram is processed only once, regardless 77of how many times it is received. 78That is, it should not be possible for an attacker 79to record a transaction (such as a bank account withdrawal), and then 80by replaying it verbatim cause the peer to think a new message 81(withdrawal request) had been received. 82WARNING: as per the standard's specification, replay protection is not 83performed when using manual-keyed IPsec (e.g. when using 84.Xr ipsecctl 8 ) . 85.El 86.Ss IPsec Protocols 87IPsec provides these services using two new protocols: 88Authentication Header (AH), and 89Encapsulating Security Payload (ESP). 90.Pp 91ESP can provide the properties authentication, integrity, replay protection, 92and confidentiality of the data 93(it secures everything in the packet that follows the IP header). 94Replay protection requires authentication and 95integrity (these two always go together). 96Confidentiality (encryption) can be used with or without 97authentication/integrity. 98Similarly, one could use authentication/integrity with or without 99confidentiality. 100.Pp 101AH provides authentication, integrity, and replay protection 102(but not confidentiality). 103The main difference between the authentication features of 104AH and ESP is that AH also authenticates portions of the IP 105header of the packet 106(such as the source/destination addresses). 107ESP authenticates only the packet payload. 108.\".Pp 109.\"A third protocol is available which is highly relevant to IPsec: IPComp. 110.\"IPComp is a protocol used to reduce the size of IP datagrams 111.\"(i.e. it provides compression). 112.\"See 113.\".Xr ipcomp 4 114.\"for further information. 115.Ss Authentication Header (AH) 116AH works by computing a value that depends on all of the payload 117data, some of the IP header data, and a certain secret value 118(the authentication key). 119This value is then sent with the rest of each packet. 120The receiver performs the same computation, and if the value matches, 121he knows no one tampered with the data (integrity), the address information 122(authenticity) or a sequence number (replay protection). 123He knows this because the secret authentication key makes sure no 124active attacker (man-in-the-middle) can recompute the correct value after 125altering the packet. 126The algorithms used to compute these values are called hash algorithms and are 127parameters in the SA, just like the authentication key. 128.Ss Encapsulating Security Payload (ESP) 129ESP optionally does almost everything that AH does 130except that it does not protect the outer IP 131header but furthermore it encrypts the payload data with an encryption 132algorithm using a secret encryption key. 133Only the ones knowing this key can decrypt the data, thus providing 134confidentiality. 135Both the algorithm and the encryption key are parameters of the SA. 136.Ss Security Associations (SAs) 137These protocols require certain parameters for each connection, describing 138exactly how the desired protection will be achieved. 139These parameters are collected in an entity called a security association, 140or SA for short. 141Typical SA parameters include encryption algorithm, hash algorithm, 142encryption key, and authentication key, to name a few. 143When two peers have established matching SAs 144(one at each end), 145packets protected with one end's SA may be verified and/or decrypted 146using the information in the other end's SA. 147The only issue remaining is to ensure that both ends have matching SAs. 148This may be done manually, or automatically using a key management daemon. 149.Pp 150Further information on manual SA establishment is described in 151.Xr ipsecctl 8 . 152Information on automated key management for IKEv1 can be found in 153.Xr isakmpd 8 154and for IKEv2 in 155.Xr iked.conf 5 . 156.Ss Security Parameter Indexes (SPIs) 157In order to identify an SA we need to have a unique name for it. 158This name is a triplet, consisting of the destination address, security 159parameter index (aka SPI) and the security protocol (ESP or AH). 160Since the destination address is part of the name, an SA is necessarily a 161unidirectional construct. 162For a bidirectional communication channel, two SAs are required, one 163outgoing and one incoming, where the destination address is our local 164IP address. 165The SPI is just a number that helps us make the name unique; 166it can be arbitrarily chosen in the range 0x100 \- 0xffffffff. 167The security protocol number should be 50 for ESP and 51 for AH, 168as these are the protocol numbers assigned by IANA. 169.Ss Modes of Operation 170IPsec can operate in two modes, either tunnel or transport mode. 171In transport mode the ordinary IP 172header is used to deliver the packets to their endpoint; 173in tunnel mode the ordinary IP 174header just tells us the address of a security gateway 175which knows how to verify/decrypt the payload and forward the 176packet to a destination given by another IP 177header contained in the protected payload. 178Tunnel mode can be used for establishing virtual private networks (VPNs), 179where parts of the networks can be spread out over an 180unsafe public network, but security gateways at each subnet are responsible 181for encrypting and decrypting the data passing over the public net. 182An SA will contain information specifying 183whether it is a tunnel or transport mode SA, 184and for tunnels it will contain values to fill in into the outer IP header. 185.Ss Lifetimes 186The SA also holds a couple of other parameters, especially useful for 187automatic keying, called lifetimes, which puts a limit on how much we can 188use an SA for protecting our data. 189These limits can be in wall-clock time or in volume of our data. 190.Ss IPsec Examples 191To better illustrate how IPsec works, consider a typical TCP packet: 192.Pp 193.Dl [IP header] [TCP header] [data...] 194.Pp 195If we apply ESP in transport mode to the above packet, we will get: 196.Pp 197.Dl [IP header] [ESP header] [TCP header] [data...] 198.Pp 199Everything after the ESP 200header is protected by whatever services of ESP we are using 201(authentication/integrity, replay protection, confidentiality). 202This means the IP header itself is not protected. 203.Pp 204If we apply ESP in tunnel mode to the original packet, we would get: 205.Pp 206.Dl [IP header] [ESP header] [IP header] [TCP header] [data...] 207.Pp 208Again, everything after the ESP header is cryptographically protected. 209Notice the insertion of an IP header between the ESP and TCP header. 210This mode of operation allows us to hide who the true 211source and destination addresses of a packet are 212(since the protected and the unprotected IP 213headers don't have to be exactly the same). 214A typical application of this is in Virtual Private Networks (or VPNs), 215where two firewalls use IPsec 216to secure the traffic of all the hosts behind them. 217For example: 218.Bd -literal -offset indent 219Net A \*(Lt----\*(Gt Firewall 1 \*(Lt--- Internet ---\*(Gt Firewall 2 \*(Lt----\*(Gt Net B 220.Ed 221.Pp 222Firewall 1 and Firewall 2 can protect all communications between Net A 223and Net B by using IPsec in tunnel mode, as illustrated above. 224.Pp 225This implementation makes use of a virtual interface, 226.Nm enc0 , 227which can be used in packet filters to specify those packets that have 228been or will be processed by IPsec. 229.Pp 230NAT can also be applied to 231.Nm enc# 232interfaces, but special care should be taken because of the interactions 233between NAT and the IPsec flow matching, especially on the packet output path. 234Inside the TCP/IP stack, packets go through the following stages: 235.Bd -literal -offset indent 236UL/R -\*(Gt [X] -\*(Gt PF/NAT(enc0) -\*(Gt IPsec -\*(Gt PF/NAT(IF) -\*(Gt IF 237UL/R \*(Lt-------- PF/NAT(enc0) \*(Lt- IPsec \*(Lt- PF/NAT(IF) \*(Lt- IF 238.Ed 239.Pp 240With IF being the real interface and UL/R the Upper Layer or Routing code. 241The [X] 242stage on the output path represents the point where the packet 243is matched against the IPsec flow database (SPD) to determine if and how 244the packet has to be IPsec-processed. 245If, at this point, it is determined that the packet should be IPsec-processed, 246it is processed by the PF/NAT code. 247Unless PF drops the packet, it will then be IPsec-processed, even if the 248packet has been modified by NAT. 249.Pp 250Security Associations can be set up manually with 251.Xr ipsecctl 8 252or automatically with the 253.Xr isakmpd 8 254or 255.Xr iked 8 256key management daemons. 257.Ss Additional Variables 258A number of 259.Xr sysctl 8 260variables are relevant to 261.Nm . 262These are generally 263.Va net.inet.ah.* , 264.Va net.inet.esp.* , 265.Va net.inet.ip.forwarding , 266.Va net.inet6.ip6.forwarding , 267and 268.Va net.inet.ip.ipsec-* . 269Full explanations can be found in 270.Xr sysctl 3 , 271and variables can be set using the 272.Xr sysctl 8 273interface. 274.Pp 275A number of kernel options are also relevant to 276.Nm . 277See 278.Xr options 4 279for further information. 280.Ss API Details 281The following IP-level 282.Xr setsockopt 2 283and 284.Xr getsockopt 2 285options are specific to 286.Nm ipsec . 287A socket can specify security levels for three different categories: 288.Bl -tag -width IP_ESP_NETWORK_LEVEL -offset 2n 289.It IP_AUTH_LEVEL 290Specifies the use of authentication for packets sent or received by the 291socket. 292.It IP_ESP_TRANS_LEVEL 293Specifies the use of encryption in transport mode for packets sent or 294received by the socket. 295.It IP_ESP_NETWORK_LEVEL 296Specifies the use of encryption in tunnel mode. 297.El 298.Pp 299For each of the categories there are five possible levels which 300specify the security policy to use in that category: 301.Bl -tag -width IPSEC_LEVEL_REQUIRE -offset 2n 302.It IPSEC_LEVEL_BYPASS 303Bypass the default system security policy. 304This option can only be used by privileged processes. 305This level is necessary for the key management daemon, 306.Xr isakmpd 8 . 307.It IPSEC_LEVEL_AVAIL 308If a Security Association is available it will be used for sending packets 309by that socket. 310.It IPSEC_LEVEL_USE 311Use IP Security for sending packets but still accept packets which are not 312secured. 313.It IPSEC_LEVEL_REQUIRE 314Use IP Security for sending packets and also require IP Security for 315received data. 316.It IPSEC_LEVEL_UNIQUE 317The outbound Security Association will only be used by this socket. 318.El 319.Pp 320When a new socket is created, it is assigned the default system security 321level in each category. 322These levels can be queried with 323.Xr getsockopt 2 . 324Only a privileged process can lower the security level with a 325.Xr setsockopt 2 326call. 327.Pp 328For example, a server process might want to accept only authenticated 329connections to prevent session hijacking. 330It would issue the following 331.Xr setsockopt 2 332call: 333.Bd -literal -offset 4n 334int level = IPSEC_LEVEL_REQUIRE; 335error = setsockopt(s, IPPROTO_IP, IP_AUTH_LEVEL, &level, sizeof(int)); 336.Ed 337.Pp 338The system does guarantee that it will succeed at establishing the 339required security associations. 340In any case a properly configured key management daemon is required which 341listens to messages from the kernel. 342.Pp 343A list of all security associations in the kernel tables can be 344obtained using the 345.Xr ipsecctl 8 346command. 347.Sh DIAGNOSTICS 348A socket operation may fail with one of the following errors returned: 349.Bl -tag -width [EINVAL] 350.It Bq Er EACCES 351An attempt was made to lower the security level below the system default 352by a non-privileged process. 353.It Bq Er EINVAL 354The length of option field did not match or an unknown security level 355was given. 356.El 357.Pp 358.Xr netstat 1 359can be used to obtain some statistics about AH and ESP usage, using the 360.Fl p 361flag. 362Using the 363.Fl r 364flag, 365.Xr netstat 1 366displays information about IPsec flows. 367.Pp 368.Xr vmstat 8 369displays information about memory use by IPsec with the 370.Fl m 371flag (look for 372.Dq tdb 373and 374.Dq xform 375allocations). 376.Sh SEE ALSO 377.Xr enc 4 , 378.\".Xr ipcomp 4 , 379.Xr options 4 , 380.Xr iked 8 , 381.Xr ipsecctl 8 , 382.Xr isakmpd 8 , 383.Xr sysctl 8 384.Sh HISTORY 385IPsec was originally designed to provide security services 386for Internet Protocol IPv6. 387It has since been engineered to provide those services for 388the original Internet Protocol, IPv4. 389.Pp 390The IPsec protocol design process was started in 1992 by 391John Ioannidis, Phil Karn, and William Allen Simpson. 392In 1995, the former wrote an implementation for 393.Bsx . 394Angelos D. Keromytis ported it to 395.Ox 396and 397.Nx . 398The latest transforms and new features were 399implemented by Angelos D. Keromytis and Niels Provos. 400.Sh AUTHORS 401.An -nosplit 402The authors of the IPsec code proper are 403.An John Ioannidis , 404.An Angelos D. Keromytis , 405and 406.An Niels Provos . 407.Pp 408.An Niklas Hallqvist 409and 410.An Niels Provos 411are the authors of 412.Xr isakmpd 8 . 413.Pp 414.An Eric Young Ap s 415libdeslite was used in this implementation for the DES algorithm. 416.Pp 417.An Steve Reid Ap s 418SHA-1 code was also used. 419.Pp 420The 421.Xr setsockopt 2 Ns / Ns Xr getsockopt 2 422interface follows somewhat loosely the 423draft-mcdonald-simple-ipsec-api 424(since expired, but still available from 425.Lk ftp://ftp.kame.net/pub/internet-drafts/ ) . 426.Sh BUGS 427There's a lot more to be said on this subject. 428This is just a beginning. 429At the moment the socket options are not fully implemented. 430