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