1 /*!\file netinet6/ipsec.h
5 /* $FreeBSD: src/sys/netinet6/ipsec.h,v 1.9 2002/04/19 04:46:23 suz Exp $ */
6 /* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */
9 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * IPsec controller part.
41 #ifndef _NETINET6_IPSEC_H_
42 #define _NETINET6_IPSEC_H_
44 #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
46 #include "opt_ipsec.h"
49 #include <net/pfkeyv2.h>
50 #include <netkey/keydb.h>
55 * Security Policy Index
56 * Ensure that both address families in the "src" and "dst" are same.
57 * When the value of the ul_proto is ICMPv6, the port field in "src"
58 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
60 struct secpolicyindex {
61 u_int8_t dir; /* direction of packet flow, see blow */
62 struct sockaddr_storage src; /* IP src address for SP */
63 struct sockaddr_storage dst; /* IP dst address for SP */
64 u_int8_t prefs; /* prefix length in bits for src */
65 u_int8_t prefd; /* prefix length in bits for dst */
66 u_int16_t ul_proto; /* upper layer Protocol */
75 /* Security Policy Data Base */
77 LIST_ENTRY(secpolicy) chain;
79 int refcnt; /* reference count */
80 struct secpolicyindex spidx; /* selector */
81 u_int32_t id; /* It's unique number on the system. */
82 u_int state; /* 0: dead, others: alive */
83 #define IPSEC_SPSTATE_DEAD 0
84 #define IPSEC_SPSTATE_ALIVE 1
86 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */
87 struct ipsecrequest *req;
88 /* pointer to the ipsec request tree, */
89 /* if policy == IPSEC else this value == NULL.*/
93 * the policy can be used without limitiation if both lifetime and
95 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
96 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
98 long created; /* time created the policy */
99 long lastused; /* updated every when kernel sends a packet */
100 long lifetime; /* duration of the lifetime of this policy */
101 long validtime; /* duration this policy is valid without use */
104 /* Request for IPsec */
105 struct ipsecrequest {
106 struct ipsecrequest *next;
107 /* pointer to next structure */
108 /* If NULL, it means the end of chain. */
109 struct secasindex saidx;/* hint for search proper SA */
110 /* if __ss_len == 0 then no address specified.*/
111 u_int level; /* IPsec level defined below. */
113 struct secasvar *sav; /* place holder of SA for use */
114 struct secpolicy *sp; /* back pointer to SP */
117 /* security policy in PCB */
119 struct secpolicy *sp_in;
120 struct secpolicy *sp_out;
121 int priv; /* privileged socket ? */
124 /* SP acquiring list table. */
126 LIST_ENTRY(secspacq) chain;
128 struct secpolicyindex spidx;
130 long created; /* for lifetime */
131 int count; /* for lifetime */
132 /* XXX: here is mbuf place holder to be sent ? */
136 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
137 #define IPSEC_PORT_ANY 0
138 #define IPSEC_ULPROTO_ANY 255
139 #define IPSEC_PROTO_ANY 255
141 /* mode of security protocol */
142 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
143 #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
144 #define IPSEC_MODE_TRANSPORT 1
145 #define IPSEC_MODE_TUNNEL 2
148 * Direction of security policy.
149 * NOTE: Since INVALID is used just as flag.
150 * The other are used for loop counter too.
152 #define IPSEC_DIR_ANY 0
153 #define IPSEC_DIR_INBOUND 1
154 #define IPSEC_DIR_OUTBOUND 2
155 #define IPSEC_DIR_MAX 3
156 #define IPSEC_DIR_INVALID 4
160 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
161 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
162 * DISCARD and NONE are allowed for system default.
164 #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
165 #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
166 #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
167 #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
168 #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
170 /* Security protocol level */
171 #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
172 #define IPSEC_LEVEL_USE 1 /* use SA if present. */
173 #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
174 #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
176 #define IPSEC_MANUAL_REQID_MAX 0x3fff
178 * if security policy level == unique, this id
179 * indicate to a relative SA for use, else is
181 * 1 - 0x3fff are reserved for manual keying.
182 * 0 are reserved for above reason. Others is
184 * Note that this id doesn't identify SA
187 #define IPSEC_REPLAYWSIZE 32
189 /* statistics for ipsec processing */
191 u_quad_t in_success; /* succeeded inbound process */
193 /* security policy violation for inbound process */
194 u_quad_t in_nosa; /* inbound SA is unavailable */
195 u_quad_t in_inval; /* inbound processing failed due to EINVAL */
196 u_quad_t in_nomem; /* inbound processing failed due to ENOBUFS */
197 u_quad_t in_badspi; /* failed getting a SPI */
198 u_quad_t in_ahreplay; /* AH replay check failed */
199 u_quad_t in_espreplay; /* ESP replay check failed */
200 u_quad_t in_ahauthsucc; /* AH authentication success */
201 u_quad_t in_ahauthfail; /* AH authentication failure */
202 u_quad_t in_espauthsucc; /* ESP authentication success */
203 u_quad_t in_espauthfail; /* ESP authentication failure */
204 u_quad_t in_esphist[256];
205 u_quad_t in_ahhist[256];
206 u_quad_t in_comphist[256];
207 u_quad_t out_success; /* succeeded outbound process */
209 /* security policy violation for outbound process */
210 u_quad_t out_nosa; /* outbound SA is unavailable */
211 u_quad_t out_inval; /* outbound process failed due to EINVAL */
212 u_quad_t out_nomem; /* inbound processing failed due to ENOBUFS */
213 u_quad_t out_noroute; /* there is no route */
214 u_quad_t out_esphist[256];
215 u_quad_t out_ahhist[256];
216 u_quad_t out_comphist[256];
220 * Definitions for IPsec & Key sysctl operations.
223 * Names for IPsec & Key sysctl objects
225 #define IPSECCTL_STATS 1 /* stats */
226 #define IPSECCTL_DEF_POLICY 2
227 #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
228 #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
229 #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
230 #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
231 #if 0 /* obsolete, do not reuse */
232 #define IPSECCTL_INBOUND_CALL_IKE 7
234 #define IPSECCTL_AH_CLEARTOS 8
235 #define IPSECCTL_AH_OFFSETMASK 9
236 #define IPSECCTL_DFBIT 10
237 #define IPSECCTL_ECN 11
238 #define IPSECCTL_DEBUG 12
239 #define IPSECCTL_ESP_RANDPAD 13
240 #define IPSECCTL_MAXID 14
242 #define IPSECCTL_NAMES { \
245 { "def_policy", CTLTYPE_INT }, \
246 { "esp_trans_deflev", CTLTYPE_INT }, \
247 { "esp_net_deflev", CTLTYPE_INT }, \
248 { "ah_trans_deflev", CTLTYPE_INT }, \
249 { "ah_net_deflev", CTLTYPE_INT }, \
251 { "ah_cleartos", CTLTYPE_INT }, \
252 { "ah_offsetmask", CTLTYPE_INT }, \
253 { "dfbit", CTLTYPE_INT }, \
254 { "ecn", CTLTYPE_INT }, \
255 { "debug", CTLTYPE_INT }, \
256 { "esp_randpad", CTLTYPE_INT }, \
259 #define IPSEC6CTL_NAMES { \
262 { "def_policy", CTLTYPE_INT }, \
263 { "esp_trans_deflev", CTLTYPE_INT }, \
264 { "esp_net_deflev", CTLTYPE_INT }, \
265 { "ah_trans_deflev", CTLTYPE_INT }, \
266 { "ah_net_deflev", CTLTYPE_INT }, \
271 { "ecn", CTLTYPE_INT }, \
272 { "debug", CTLTYPE_INT }, \
273 { "esp_randpad", CTLTYPE_INT }, \
277 struct ipsec_output_state {
280 struct sockaddr *dst;
283 struct ipsec_history {
288 extern int ipsec_debug;
290 extern struct ipsecstat ipsecstat;
291 extern struct secpolicy ip4_def_policy;
292 extern int ip4_esp_trans_deflev;
293 extern int ip4_esp_net_deflev;
294 extern int ip4_ah_trans_deflev;
295 extern int ip4_ah_net_deflev;
296 extern int ip4_ah_cleartos;
297 extern int ip4_ah_offsetmask;
298 extern int ip4_ipsec_dfbit;
299 extern int ip4_ipsec_ecn;
300 extern int ip4_esp_randpad;
302 #define ipseclog(x) do { if (ipsec_debug) log x; } while (0)
304 extern struct secpolicy *ipsec4_getpolicybysock
305 __P((struct mbuf *, u_int, struct socket *, int *));
306 extern struct secpolicy *ipsec4_getpolicybyaddr
307 __P((struct mbuf *, u_int, int, int *));
310 extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **));
311 extern int ipsec_copy_policy
312 __P((struct inpcbpolicy *, struct inpcbpolicy *));
313 extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *));
315 extern int ipsec4_set_policy __P((struct inpcb *inp, int optname,
316 caddr_t request, size_t len, int priv));
317 extern int ipsec4_get_policy __P((struct inpcb *inpcb, caddr_t request,
318 size_t len, struct mbuf **mp));
319 extern int ipsec4_delete_pcbpolicy __P((struct inpcb *));
320 extern int ipsec4_in_reject_so __P((struct mbuf *, struct socket *));
321 extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
325 extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
326 extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
328 extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
329 extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
332 extern const char *ipsec4_logpacketstr __P((struct ip *, u_int32_t));
333 extern const char *ipsec_logsastr __P((struct secasvar *));
335 extern void ipsec_dumpmbuf __P((struct mbuf *));
337 extern int ipsec4_output __P((struct ipsec_output_state *, struct secpolicy *,
339 extern int ipsec4_tunnel_validate __P((struct mbuf *, int, u_int,
341 extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
342 extern void ipsec_delaux __P((struct mbuf *));
343 extern int ipsec_setsocket __P((struct mbuf *, struct socket *));
344 extern struct socket *ipsec_getsocket __P((struct mbuf *));
345 extern int ipsec_addhist __P((struct mbuf *, int, u_int32_t));
346 extern struct ipsec_history *ipsec_gethist __P((struct mbuf *, int *));
347 extern void ipsec_clearhist __P((struct mbuf *));
351 extern caddr_t ipsec_set_policy __P((char *, int));
352 extern int ipsec_get_policylen __P((caddr_t));
353 extern char *ipsec_dump_policy __P((caddr_t, char *));
355 extern const char *ipsec_strerror __P((void));
356 #endif /* !_KERNEL */
358 #endif /* _NETINET6_IPSEC_H_ */