Deleted Added
sdiff udiff text old ( 252026 ) new ( 253081 )
full compact
1/* $FreeBSD: head/sys/netipsec/ipsec.h 252026 2013-06-20 09:55:53Z ae $ */
2/* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * IPsec controller part.
35 */
36
37#ifndef _NETIPSEC_IPSEC_H_
38#define _NETIPSEC_IPSEC_H_
39
40#if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
41#include "opt_inet.h"
42#include "opt_ipsec.h"
43#endif
44
45#include <net/pfkeyv2.h>
46#include <netipsec/keydb.h>
47
48#ifdef _KERNEL
49
50#define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
51
52#define IPSEC_IS_PRIVILEGED_SO(_so) \
53 ((_so)->so_cred != NULL && \
54 priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \
55 == 0)
56
57/*
58 * Security Policy Index
59 * Ensure that both address families in the "src" and "dst" are same.
60 * When the value of the ul_proto is ICMPv6, the port field in "src"
61 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
62 */
63struct secpolicyindex {
64 u_int8_t dir; /* direction of packet flow, see below */
65 union sockaddr_union src; /* IP src address for SP */
66 union sockaddr_union dst; /* IP dst address for SP */
67 u_int8_t prefs; /* prefix length in bits for src */
68 u_int8_t prefd; /* prefix length in bits for dst */
69 u_int16_t ul_proto; /* upper layer Protocol */
70#ifdef notyet
71 uid_t uids;
72 uid_t uidd;
73 gid_t gids;
74 gid_t gidd;
75#endif
76};
77
78/* Security Policy Data Base */
79struct secpolicy {
80 LIST_ENTRY(secpolicy) chain;
81 struct mtx lock;
82
83 u_int refcnt; /* reference count */
84 struct secpolicyindex spidx; /* selector */
85 u_int32_t id; /* It's unique number on the system. */
86 u_int state; /* 0: dead, others: alive */
87#define IPSEC_SPSTATE_DEAD 0
88#define IPSEC_SPSTATE_ALIVE 1
89 u_int16_t policy; /* policy_type per pfkeyv2.h */
90 u_int16_t scangen; /* scan generation # */
91 struct ipsecrequest *req;
92 /* pointer to the ipsec request tree, */
93 /* if policy == IPSEC else this value == NULL.*/
94
95 /*
96 * lifetime handler.
97 * the policy can be used without limitiation if both lifetime and
98 * validtime are zero.
99 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
100 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
101 */
102 time_t created; /* time created the policy */
103 time_t lastused; /* updated every when kernel sends a packet */
104 long lifetime; /* duration of the lifetime of this policy */
105 long validtime; /* duration this policy is valid without use */
106};
107
108#define SECPOLICY_LOCK_INIT(_sp) \
109 mtx_init(&(_sp)->lock, "ipsec policy", NULL, MTX_DEF)
110#define SECPOLICY_LOCK(_sp) mtx_lock(&(_sp)->lock)
111#define SECPOLICY_UNLOCK(_sp) mtx_unlock(&(_sp)->lock)
112#define SECPOLICY_LOCK_DESTROY(_sp) mtx_destroy(&(_sp)->lock)
113#define SECPOLICY_LOCK_ASSERT(_sp) mtx_assert(&(_sp)->lock, MA_OWNED)
114
115/* Request for IPsec */
116struct ipsecrequest {
117 struct ipsecrequest *next;
118 /* pointer to next structure */
119 /* If NULL, it means the end of chain. */
120 struct secasindex saidx;/* hint for search proper SA */
121 /* if __ss_len == 0 then no address specified.*/
122 u_int level; /* IPsec level defined below. */
123
124 struct secasvar *sav; /* place holder of SA for use */
125 struct secpolicy *sp; /* back pointer to SP */
126 struct rwlock lock; /* to interlock updates */
127};
128
129/*
130 * Need recursion for when crypto callbacks happen directly,
131 * as in the case of software crypto. Need to look at how
132 * hard it is to remove this...
133 */
134#define IPSECREQUEST_LOCK_INIT(_isr) \
135 rw_init_flags(&(_isr)->lock, "ipsec request", RW_RECURSE)
136#define IPSECREQUEST_LOCK(_isr) rw_rlock(&(_isr)->lock)
137#define IPSECREQUEST_UNLOCK(_isr) rw_runlock(&(_isr)->lock)
138#define IPSECREQUEST_WLOCK(_isr) rw_wlock(&(_isr)->lock)
139#define IPSECREQUEST_WUNLOCK(_isr) rw_wunlock(&(_isr)->lock)
140#define IPSECREQUEST_UPGRADE(_isr) rw_try_upgrade(&(_isr)->lock)
141#define IPSECREQUEST_DOWNGRADE(_isr) rw_downgrade(&(_isr)->lock)
142#define IPSECREQUEST_LOCK_DESTROY(_isr) rw_destroy(&(_isr)->lock)
143#define IPSECREQUEST_LOCK_ASSERT(_isr) rw_assert(&(_isr)->lock, RA_LOCKED)
144
145/* security policy in PCB */
146struct inpcbpolicy {
147 struct secpolicy *sp_in;
148 struct secpolicy *sp_out;
149 int priv; /* privileged socket ? */
150};
151
152/* SP acquiring list table. */
153struct secspacq {
154 LIST_ENTRY(secspacq) chain;
155
156 struct secpolicyindex spidx;
157
158 time_t created; /* for lifetime */
159 int count; /* for lifetime */
160 /* XXX: here is mbuf place holder to be sent ? */
161};
162#endif /* _KERNEL */
163
164/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
165#define IPSEC_PORT_ANY 0
166#define IPSEC_ULPROTO_ANY 255
167#define IPSEC_PROTO_ANY 255
168
169/* mode of security protocol */
170/* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
171#define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
172#define IPSEC_MODE_TRANSPORT 1
173#define IPSEC_MODE_TUNNEL 2
174#define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */
175
176/*
177 * Direction of security policy.
178 * NOTE: Since INVALID is used just as flag.
179 * The other are used for loop counter too.
180 */
181#define IPSEC_DIR_ANY 0
182#define IPSEC_DIR_INBOUND 1
183#define IPSEC_DIR_OUTBOUND 2
184#define IPSEC_DIR_MAX 3
185#define IPSEC_DIR_INVALID 4
186
187/* Policy level */
188/*
189 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
190 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
191 * DISCARD and NONE are allowed for system default.
192 */
193#define IPSEC_POLICY_DISCARD 0 /* discarding packet */
194#define IPSEC_POLICY_NONE 1 /* through IPsec engine */
195#define IPSEC_POLICY_IPSEC 2 /* do IPsec */
196#define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
197#define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
198
199/* Security protocol level */
200#define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
201#define IPSEC_LEVEL_USE 1 /* use SA if present. */
202#define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
203#define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
204
205#define IPSEC_MANUAL_REQID_MAX 0x3fff
206 /*
207 * if security policy level == unique, this id
208 * indicate to a relative SA for use, else is
209 * zero.
210 * 1 - 0x3fff are reserved for manual keying.
211 * 0 are reserved for above reason. Others is
212 * for kernel use.
213 * Note that this id doesn't identify SA
214 * by only itself.
215 */
216#define IPSEC_REPLAYWSIZE 32
217
218/* statistics for ipsec processing */
219struct ipsecstat {
220 u_quad_t in_success; /* succeeded inbound process */
221 u_quad_t in_polvio;
222 /* security policy violation for inbound process */
223 u_quad_t in_nosa; /* inbound SA is unavailable */
224 u_quad_t in_inval; /* inbound processing failed due to EINVAL */
225 u_quad_t in_nomem; /* inbound processing failed due to ENOBUFS */
226 u_quad_t in_badspi; /* failed getting a SPI */
227 u_quad_t in_ahreplay; /* AH replay check failed */
228 u_quad_t in_espreplay; /* ESP replay check failed */
229 u_quad_t in_ahauthsucc; /* AH authentication success */
230 u_quad_t in_ahauthfail; /* AH authentication failure */
231 u_quad_t in_espauthsucc; /* ESP authentication success */
232 u_quad_t in_espauthfail; /* ESP authentication failure */
233 u_quad_t in_esphist[256];
234 u_quad_t in_ahhist[256];
235 u_quad_t in_comphist[256];
236 u_quad_t out_success; /* succeeded outbound process */
237 u_quad_t out_polvio;
238 /* security policy violation for outbound process */
239 u_quad_t out_nosa; /* outbound SA is unavailable */
240 u_quad_t out_inval; /* outbound process failed due to EINVAL */
241 u_quad_t out_nomem; /* inbound processing failed due to ENOBUFS */
242 u_quad_t out_noroute; /* there is no route */
243 u_quad_t out_esphist[256];
244 u_quad_t out_ahhist[256];
245 u_quad_t out_comphist[256];
246
247 u_quad_t spdcachelookup;
248 u_quad_t spdcachemiss;
249
250 u_int32_t ips_in_polvio; /* input: sec policy violation */
251 u_int32_t ips_out_polvio; /* output: sec policy violation */
252 u_int32_t ips_out_nosa; /* output: SA unavailable */
253 u_int32_t ips_out_nomem; /* output: no memory available */
254 u_int32_t ips_out_noroute; /* output: no route available */
255 u_int32_t ips_out_inval; /* output: generic error */
256 u_int32_t ips_out_bundlesa; /* output: bundled SA processed */
257 u_int32_t ips_mbcoalesced; /* mbufs coalesced during clone */
258 u_int32_t ips_clcoalesced; /* clusters coalesced during clone */
259 u_int32_t ips_clcopied; /* clusters copied during clone */
260 u_int32_t ips_mbinserted; /* mbufs inserted during makespace */
261 /*
262 * Temporary statistics for performance analysis.
263 */
264 /* See where ESP/AH/IPCOMP header land in mbuf on input */
265 u_int32_t ips_input_front;
266 u_int32_t ips_input_middle;
267 u_int32_t ips_input_end;
268};
269
270/*
271 * Definitions for IPsec & Key sysctl operations.
272 */
273/*
274 * Names for IPsec & Key sysctl objects
275 */
276#define IPSECCTL_STATS 1 /* stats */
277#define IPSECCTL_DEF_POLICY 2
278#define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
279#define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
280#define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
281#define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
282#if 0 /* obsolete, do not reuse */
283#define IPSECCTL_INBOUND_CALL_IKE 7
284#endif
285#define IPSECCTL_AH_CLEARTOS 8
286#define IPSECCTL_AH_OFFSETMASK 9
287#define IPSECCTL_DFBIT 10
288#define IPSECCTL_ECN 11
289#define IPSECCTL_DEBUG 12
290#define IPSECCTL_ESP_RANDPAD 13
291#define IPSECCTL_MAXID 14
292
293#define IPSECCTL_NAMES { \
294 { 0, 0 }, \
295 { 0, 0 }, \
296 { "def_policy", CTLTYPE_INT }, \
297 { "esp_trans_deflev", CTLTYPE_INT }, \
298 { "esp_net_deflev", CTLTYPE_INT }, \
299 { "ah_trans_deflev", CTLTYPE_INT }, \
300 { "ah_net_deflev", CTLTYPE_INT }, \
301 { 0, 0 }, \
302 { "ah_cleartos", CTLTYPE_INT }, \
303 { "ah_offsetmask", CTLTYPE_INT }, \
304 { "dfbit", CTLTYPE_INT }, \
305 { "ecn", CTLTYPE_INT }, \
306 { "debug", CTLTYPE_INT }, \
307 { "esp_randpad", CTLTYPE_INT }, \
308}
309
310#define IPSEC6CTL_NAMES { \
311 { 0, 0 }, \
312 { 0, 0 }, \
313 { "def_policy", CTLTYPE_INT }, \
314 { "esp_trans_deflev", CTLTYPE_INT }, \
315 { "esp_net_deflev", CTLTYPE_INT }, \
316 { "ah_trans_deflev", CTLTYPE_INT }, \
317 { "ah_net_deflev", CTLTYPE_INT }, \
318 { 0, 0 }, \
319 { 0, 0 }, \
320 { 0, 0 }, \
321 { 0, 0 }, \
322 { "ecn", CTLTYPE_INT }, \
323 { "debug", CTLTYPE_INT }, \
324 { "esp_randpad", CTLTYPE_INT }, \
325}
326
327#ifdef _KERNEL
328struct ipsec_output_state {
329 struct mbuf *m;
330 struct route *ro;
331 struct sockaddr *dst;
332};
333
334struct ipsec_history {
335 int ih_proto;
336 u_int32_t ih_spi;
337};
338
339VNET_DECLARE(int, ipsec_debug);
340#define V_ipsec_debug VNET(ipsec_debug)
341
342#ifdef REGRESSION
343VNET_DECLARE(int, ipsec_replay);
344VNET_DECLARE(int, ipsec_integrity);
345
346#define V_ipsec_replay VNET(ipsec_replay)
347#define V_ipsec_integrity VNET(ipsec_integrity)
348#endif
349
350VNET_DECLARE(struct ipsecstat, ipsec4stat);
351VNET_DECLARE(struct secpolicy, ip4_def_policy);
352VNET_DECLARE(int, ip4_esp_trans_deflev);
353VNET_DECLARE(int, ip4_esp_net_deflev);
354VNET_DECLARE(int, ip4_ah_trans_deflev);
355VNET_DECLARE(int, ip4_ah_net_deflev);
356VNET_DECLARE(int, ip4_ah_offsetmask);
357VNET_DECLARE(int, ip4_ipsec_dfbit);
358VNET_DECLARE(int, ip4_ipsec_ecn);
359VNET_DECLARE(int, ip4_esp_randpad);
360VNET_DECLARE(int, crypto_support);
361
362#define IPSECSTAT_INC(name) V_ipsec4stat.name += 1
363#define V_ipsec4stat VNET(ipsec4stat)
364#define V_ip4_def_policy VNET(ip4_def_policy)
365#define V_ip4_esp_trans_deflev VNET(ip4_esp_trans_deflev)
366#define V_ip4_esp_net_deflev VNET(ip4_esp_net_deflev)
367#define V_ip4_ah_trans_deflev VNET(ip4_ah_trans_deflev)
368#define V_ip4_ah_net_deflev VNET(ip4_ah_net_deflev)
369#define V_ip4_ah_offsetmask VNET(ip4_ah_offsetmask)
370#define V_ip4_ipsec_dfbit VNET(ip4_ipsec_dfbit)
371#define V_ip4_ipsec_ecn VNET(ip4_ipsec_ecn)
372#define V_ip4_esp_randpad VNET(ip4_esp_randpad)
373#define V_crypto_support VNET(crypto_support)
374
375#define ipseclog(x) do { if (V_ipsec_debug) log x; } while (0)
376/* for openbsd compatibility */
377#define DPRINTF(x) do { if (V_ipsec_debug) printf x; } while (0)
378
379extern struct ipsecrequest *ipsec_newisr(void);
380extern void ipsec_delisr(struct ipsecrequest *);
381
382struct tdb_ident;
383extern struct secpolicy *ipsec_getpolicy __P((struct tdb_ident*, u_int));
384struct inpcb;
385extern struct secpolicy *ipsec4_checkpolicy __P((struct mbuf *, u_int, u_int,
386 int *, struct inpcb *));
387extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
388 int, int *);
389
390struct inpcb;
391extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **));
392extern int ipsec_copy_policy
393 __P((struct inpcbpolicy *, struct inpcbpolicy *));
394extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *));
395extern int ipsec_in_reject __P((struct secpolicy *, struct mbuf *));
396
397extern int ipsec_set_policy __P((struct inpcb *inp, int optname,
398 caddr_t request, size_t len, struct ucred *cred));
399extern int ipsec_get_policy __P((struct inpcb *inpcb, caddr_t request,
400 size_t len, struct mbuf **mp));
401extern int ipsec_delete_pcbpolicy __P((struct inpcb *));
402extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
403
404struct secas;
405struct tcpcb;
406extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
407extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
408
409extern size_t ipsec_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
410extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
411
412union sockaddr_union;
413extern char * ipsec_address(union sockaddr_union* sa);
414extern const char *ipsec_logsastr __P((struct secasvar *));
415
416extern void ipsec_dumpmbuf __P((struct mbuf *));
417
418struct m_tag;
419extern void ah4_input(struct mbuf *m, int off);
420extern void ah4_ctlinput(int cmd, struct sockaddr *sa, void *);
421extern void esp4_input(struct mbuf *m, int off);
422extern void esp4_ctlinput(int cmd, struct sockaddr *sa, void *);
423extern void ipcomp4_input(struct mbuf *m, int off);
424extern int ipsec4_common_input(struct mbuf *m, ...);
425extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
426 int skip, int protoff, struct m_tag *mt);
427extern int ipsec4_process_packet __P((struct mbuf *, struct ipsecrequest *,
428 int, int));
429extern int ipsec_process_done __P((struct mbuf *, struct ipsecrequest *));
430
431extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
432
433extern void m_checkalignment(const char* where, struct mbuf *m0,
434 int off, int len);
435extern struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
436extern caddr_t m_pad(struct mbuf *m, int n);
437extern int m_striphdr(struct mbuf *m, int skip, int hlen);
438
439#ifdef DEV_ENC
440#define ENC_BEFORE 0x0001
441#define ENC_AFTER 0x0002
442#define ENC_IN 0x0100
443#define ENC_OUT 0x0200
444extern int ipsec_filter(struct mbuf **, int, int);
445extern void ipsec_bpf(struct mbuf *, struct secasvar *, int, int);
446#endif
447#endif /* _KERNEL */
448
449#ifndef _KERNEL
450extern caddr_t ipsec_set_policy __P((char *, int));
451extern int ipsec_get_policylen __P((caddr_t));
452extern char *ipsec_dump_policy __P((caddr_t, char *));
453
454extern const char *ipsec_strerror __P((void));
455
456#endif /* ! KERNEL */
457
458#endif /* _NETIPSEC_IPSEC_H_ */