2 /* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * IPsec controller part.
37 #ifndef _NETIPSEC_IPSEC_H_
38 #define _NETIPSEC_IPSEC_H_
40 #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
42 #include "opt_ipsec.h"
45 #include <net/pfkeyv2.h>
46 #include <netipsec/keydb.h>
50 #include <sys/_lock.h>
51 #include <sys/_mutex.h>
52 #include <sys/_rwlock.h>
54 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
56 #define IPSEC_IS_PRIVILEGED_SO(_so) \
57 ((_so)->so_cred != NULL && \
58 priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \
62 * Security Policy Index
63 * Ensure that both address families in the "src" and "dst" are same.
64 * When the value of the ul_proto is ICMPv6, the port field in "src"
65 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
67 struct secpolicyindex {
68 u_int8_t dir; /* direction of packet flow, see below */
69 union sockaddr_union src; /* IP src address for SP */
70 union sockaddr_union dst; /* IP dst address for SP */
71 u_int8_t prefs; /* prefix length in bits for src */
72 u_int8_t prefd; /* prefix length in bits for dst */
73 u_int16_t ul_proto; /* upper layer Protocol */
82 /* Security Policy Data Base */
84 LIST_ENTRY(secpolicy) chain;
87 u_int refcnt; /* reference count */
88 struct secpolicyindex spidx; /* selector */
89 u_int32_t id; /* It's unique number on the system. */
90 u_int state; /* 0: dead, others: alive */
91 #define IPSEC_SPSTATE_DEAD 0
92 #define IPSEC_SPSTATE_ALIVE 1
93 u_int policy; /* policy_type per pfkeyv2.h */
94 u_int16_t scangen; /* scan generation # */
95 struct ipsecrequest *req;
96 /* pointer to the ipsec request tree, */
97 /* if policy == IPSEC else this value == NULL.*/
101 * the policy can be used without limitiation if both lifetime and
102 * validtime are zero.
103 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
104 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
106 time_t created; /* time created the policy */
107 time_t lastused; /* updated every when kernel sends a packet */
108 long lifetime; /* duration of the lifetime of this policy */
109 long validtime; /* duration this policy is valid without use */
112 #define SECPOLICY_LOCK_INIT(_sp) \
113 mtx_init(&(_sp)->lock, "ipsec policy", NULL, MTX_DEF)
114 #define SECPOLICY_LOCK(_sp) mtx_lock(&(_sp)->lock)
115 #define SECPOLICY_UNLOCK(_sp) mtx_unlock(&(_sp)->lock)
116 #define SECPOLICY_LOCK_DESTROY(_sp) mtx_destroy(&(_sp)->lock)
117 #define SECPOLICY_LOCK_ASSERT(_sp) mtx_assert(&(_sp)->lock, MA_OWNED)
119 /* Request for IPsec */
120 struct ipsecrequest {
121 struct ipsecrequest *next;
122 /* pointer to next structure */
123 /* If NULL, it means the end of chain. */
124 struct secasindex saidx;/* hint for search proper SA */
125 /* if __ss_len == 0 then no address specified.*/
126 u_int level; /* IPsec level defined below. */
128 struct secasvar *sav; /* place holder of SA for use */
129 struct secpolicy *sp; /* back pointer to SP */
130 struct rwlock lock; /* to interlock updates */
134 * Need recursion for when crypto callbacks happen directly,
135 * as in the case of software crypto. Need to look at how
136 * hard it is to remove this...
138 #define IPSECREQUEST_LOCK_INIT(_isr) \
139 rw_init_flags(&(_isr)->lock, "ipsec request", RW_RECURSE)
140 #define IPSECREQUEST_LOCK(_isr) rw_rlock(&(_isr)->lock)
141 #define IPSECREQUEST_UNLOCK(_isr) rw_runlock(&(_isr)->lock)
142 #define IPSECREQUEST_WLOCK(_isr) rw_wlock(&(_isr)->lock)
143 #define IPSECREQUEST_WUNLOCK(_isr) rw_wunlock(&(_isr)->lock)
144 #define IPSECREQUEST_UPGRADE(_isr) rw_try_upgrade(&(_isr)->lock)
145 #define IPSECREQUEST_DOWNGRADE(_isr) rw_downgrade(&(_isr)->lock)
146 #define IPSECREQUEST_LOCK_DESTROY(_isr) rw_destroy(&(_isr)->lock)
147 #define IPSECREQUEST_LOCK_ASSERT(_isr) rw_assert(&(_isr)->lock, RA_LOCKED)
149 /* security policy in PCB */
151 struct secpolicy *sp_in;
152 struct secpolicy *sp_out;
153 int priv; /* privileged socket ? */
156 /* SP acquiring list table. */
158 LIST_ENTRY(secspacq) chain;
160 struct secpolicyindex spidx;
162 time_t created; /* for lifetime */
163 int count; /* for lifetime */
164 /* XXX: here is mbuf place holder to be sent ? */
168 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
169 #define IPSEC_PORT_ANY 0
170 #define IPSEC_ULPROTO_ANY 255
171 #define IPSEC_PROTO_ANY 255
173 /* mode of security protocol */
174 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
175 #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
176 #define IPSEC_MODE_TRANSPORT 1
177 #define IPSEC_MODE_TUNNEL 2
178 #define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */
181 * Direction of security policy.
182 * NOTE: Since INVALID is used just as flag.
183 * The other are used for loop counter too.
185 #define IPSEC_DIR_ANY 0
186 #define IPSEC_DIR_INBOUND 1
187 #define IPSEC_DIR_OUTBOUND 2
188 #define IPSEC_DIR_MAX 3
189 #define IPSEC_DIR_INVALID 4
193 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
194 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
195 * DISCARD and NONE are allowed for system default.
197 #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
198 #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
199 #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
200 #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
201 #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
203 /* Security protocol level */
204 #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
205 #define IPSEC_LEVEL_USE 1 /* use SA if present. */
206 #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
207 #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
209 #define IPSEC_MANUAL_REQID_MAX 0x3fff
211 * if security policy level == unique, this id
212 * indicate to a relative SA for use, else is
214 * 1 - 0x3fff are reserved for manual keying.
215 * 0 are reserved for above reason. Others is
217 * Note that this id doesn't identify SA
220 #define IPSEC_REPLAYWSIZE 32
222 /* statistics for ipsec processing */
224 uint64_t ips_in_polvio; /* input: sec policy violation */
225 uint64_t ips_in_nomem; /* input: no memory available */
226 uint64_t ips_in_inval; /* input: generic error */
228 uint64_t ips_out_polvio; /* output: sec policy violation */
229 uint64_t ips_out_nosa; /* output: SA unavailable */
230 uint64_t ips_out_nomem; /* output: no memory available */
231 uint64_t ips_out_noroute; /* output: no route available */
232 uint64_t ips_out_inval; /* output: generic error */
233 uint64_t ips_out_bundlesa; /* output: bundled SA processed */
235 uint64_t ips_mbcoalesced; /* mbufs coalesced during clone */
236 uint64_t ips_clcoalesced; /* clusters coalesced during clone */
237 uint64_t ips_clcopied; /* clusters copied during clone */
238 uint64_t ips_mbinserted; /* mbufs inserted during makespace */
240 * Temporary statistics for performance analysis.
242 /* See where ESP/AH/IPCOMP header land in mbuf on input */
243 uint64_t ips_input_front;
244 uint64_t ips_input_middle;
245 uint64_t ips_input_end;
249 * Definitions for IPsec & Key sysctl operations.
252 * Names for IPsec & Key sysctl objects
254 #define IPSECCTL_STATS 1 /* stats */
255 #define IPSECCTL_DEF_POLICY 2
256 #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
257 #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
258 #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
259 #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
260 #if 0 /* obsolete, do not reuse */
261 #define IPSECCTL_INBOUND_CALL_IKE 7
263 #define IPSECCTL_AH_CLEARTOS 8
264 #define IPSECCTL_AH_OFFSETMASK 9
265 #define IPSECCTL_DFBIT 10
266 #define IPSECCTL_ECN 11
267 #define IPSECCTL_DEBUG 12
268 #define IPSECCTL_ESP_RANDPAD 13
269 #define IPSECCTL_MAXID 14
272 #include <sys/counter.h>
274 struct ipsec_output_state {
277 struct sockaddr *dst;
280 struct ipsec_history {
285 VNET_DECLARE(int, ipsec_debug);
286 #define V_ipsec_debug VNET(ipsec_debug)
289 VNET_DECLARE(int, ipsec_replay);
290 VNET_DECLARE(int, ipsec_integrity);
292 #define V_ipsec_replay VNET(ipsec_replay)
293 #define V_ipsec_integrity VNET(ipsec_integrity)
296 VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
297 VNET_DECLARE(struct secpolicy, ip4_def_policy);
298 VNET_DECLARE(int, ip4_esp_trans_deflev);
299 VNET_DECLARE(int, ip4_esp_net_deflev);
300 VNET_DECLARE(int, ip4_ah_trans_deflev);
301 VNET_DECLARE(int, ip4_ah_net_deflev);
302 VNET_DECLARE(int, ip4_ah_offsetmask);
303 VNET_DECLARE(int, ip4_ipsec_dfbit);
304 VNET_DECLARE(int, ip4_ipsec_ecn);
305 VNET_DECLARE(int, ip4_esp_randpad);
306 VNET_DECLARE(int, crypto_support);
308 #define IPSECSTAT_INC(name) \
309 VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
310 #define V_ip4_def_policy VNET(ip4_def_policy)
311 #define V_ip4_esp_trans_deflev VNET(ip4_esp_trans_deflev)
312 #define V_ip4_esp_net_deflev VNET(ip4_esp_net_deflev)
313 #define V_ip4_ah_trans_deflev VNET(ip4_ah_trans_deflev)
314 #define V_ip4_ah_net_deflev VNET(ip4_ah_net_deflev)
315 #define V_ip4_ah_offsetmask VNET(ip4_ah_offsetmask)
316 #define V_ip4_ipsec_dfbit VNET(ip4_ipsec_dfbit)
317 #define V_ip4_ipsec_ecn VNET(ip4_ipsec_ecn)
318 #define V_ip4_esp_randpad VNET(ip4_esp_randpad)
319 #define V_crypto_support VNET(crypto_support)
321 #define ipseclog(x) do { if (V_ipsec_debug) log x; } while (0)
322 /* for openbsd compatibility */
323 #define DPRINTF(x) do { if (V_ipsec_debug) printf x; } while (0)
325 extern struct ipsecrequest *ipsec_newisr(void);
326 extern void ipsec_delisr(struct ipsecrequest *);
329 extern struct secpolicy *ipsec_getpolicy(struct tdb_ident*, u_int);
331 extern struct secpolicy *ipsec4_checkpolicy(struct mbuf *, u_int, u_int,
332 int *, struct inpcb *);
333 extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
337 extern int ipsec_init_policy(struct socket *so, struct inpcbpolicy **);
338 extern int ipsec_copy_policy(struct inpcbpolicy *, struct inpcbpolicy *);
339 extern u_int ipsec_get_reqlevel(struct ipsecrequest *);
340 extern int ipsec_in_reject(struct secpolicy *, struct mbuf *);
342 extern int ipsec_set_policy(struct inpcb *inp, int optname,
343 caddr_t request, size_t len, struct ucred *cred);
344 extern int ipsec_get_policy(struct inpcb *inpcb, caddr_t request,
345 size_t len, struct mbuf **mp);
346 extern int ipsec_delete_pcbpolicy(struct inpcb *);
347 extern int ipsec4_in_reject(struct mbuf *, struct inpcb *);
351 extern int ipsec_chkreplay(u_int32_t, struct secasvar *);
352 extern int ipsec_updatereplay(u_int32_t, struct secasvar *);
354 extern size_t ipsec_hdrsiz(struct mbuf *, u_int, struct inpcb *);
355 extern size_t ipsec_hdrsiz_tcp(struct tcpcb *);
357 union sockaddr_union;
358 extern char * ipsec_address(union sockaddr_union* sa);
359 extern const char *ipsec_logsastr(struct secasvar *);
361 extern void ipsec_dumpmbuf(struct mbuf *);
364 extern void ah4_input(struct mbuf *m, int off);
365 extern void ah4_ctlinput(int cmd, struct sockaddr *sa, void *);
366 extern void esp4_input(struct mbuf *m, int off);
367 extern void esp4_ctlinput(int cmd, struct sockaddr *sa, void *);
368 extern void ipcomp4_input(struct mbuf *m, int off);
369 extern int ipsec4_common_input(struct mbuf *m, ...);
370 extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
371 int skip, int protoff, struct m_tag *mt);
372 extern int ipsec4_process_packet(struct mbuf *, struct ipsecrequest *,
374 extern int ipsec_process_done(struct mbuf *, struct ipsecrequest *);
376 extern struct mbuf *ipsec_copypkt(struct mbuf *);
378 extern void m_checkalignment(const char* where, struct mbuf *m0,
380 extern struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
381 extern caddr_t m_pad(struct mbuf *m, int n);
382 extern int m_striphdr(struct mbuf *m, int skip, int hlen);
385 #define ENC_BEFORE 0x0001
386 #define ENC_AFTER 0x0002
387 #define ENC_IN 0x0100
388 #define ENC_OUT 0x0200
389 extern int ipsec_filter(struct mbuf **, int, int);
390 extern void ipsec_bpf(struct mbuf *, struct secasvar *, int, int);
395 extern caddr_t ipsec_set_policy(char *, int);
396 extern int ipsec_get_policylen(caddr_t);
397 extern char *ipsec_dump_policy(caddr_t, char *);
398 extern const char *ipsec_strerror(void);
400 #endif /* ! KERNEL */
402 #endif /* _NETIPSEC_IPSEC_H_ */