ipsec.h revision 1.14 1 1.14 perry /* $NetBSD: ipsec.h,v 1.14 2006/02/16 20:17:20 perry Exp $ */
2 1.8 jonathan /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.h,v 1.2.4.2 2004/02/14 22:23:23 bms Exp $ */
3 1.1 jonathan /* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 1.1 jonathan * All rights reserved.
8 1.1 jonathan *
9 1.1 jonathan * Redistribution and use in source and binary forms, with or without
10 1.1 jonathan * modification, are permitted provided that the following conditions
11 1.1 jonathan * are met:
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan * 3. Neither the name of the project nor the names of its contributors
18 1.1 jonathan * may be used to endorse or promote products derived from this software
19 1.1 jonathan * without specific prior written permission.
20 1.1 jonathan *
21 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 1.1 jonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 jonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 jonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 1.1 jonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 jonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 jonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 jonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 jonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 jonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 jonathan * SUCH DAMAGE.
32 1.1 jonathan */
33 1.1 jonathan
34 1.1 jonathan /*
35 1.1 jonathan * IPsec controller part.
36 1.1 jonathan */
37 1.1 jonathan
38 1.1 jonathan #ifndef _NETIPSEC_IPSEC_H_
39 1.1 jonathan #define _NETIPSEC_IPSEC_H_
40 1.1 jonathan
41 1.1 jonathan #if defined(_KERNEL) && !defined(_LKM) && !defined(KLD_MODULE)
42 1.1 jonathan #include "opt_inet.h"
43 1.1 jonathan #include "opt_ipsec.h"
44 1.1 jonathan #endif
45 1.1 jonathan
46 1.1 jonathan #include <net/pfkeyv2.h>
47 1.3 tls #include <netipsec/ipsec_osdep.h>
48 1.1 jonathan #include <netipsec/keydb.h>
49 1.1 jonathan
50 1.1 jonathan #ifdef _KERNEL
51 1.1 jonathan
52 1.1 jonathan /*
53 1.1 jonathan * Security Policy Index
54 1.1 jonathan * Ensure that both address families in the "src" and "dst" are same.
55 1.1 jonathan * When the value of the ul_proto is ICMPv6, the port field in "src"
56 1.1 jonathan * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
57 1.1 jonathan */
58 1.1 jonathan struct secpolicyindex {
59 1.1 jonathan u_int8_t dir; /* direction of packet flow, see blow */
60 1.1 jonathan union sockaddr_union src; /* IP src address for SP */
61 1.1 jonathan union sockaddr_union dst; /* IP dst address for SP */
62 1.1 jonathan u_int8_t prefs; /* prefix length in bits for src */
63 1.1 jonathan u_int8_t prefd; /* prefix length in bits for dst */
64 1.1 jonathan u_int16_t ul_proto; /* upper layer Protocol */
65 1.1 jonathan #ifdef notyet
66 1.1 jonathan uid_t uids;
67 1.1 jonathan uid_t uidd;
68 1.1 jonathan gid_t gids;
69 1.1 jonathan gid_t gidd;
70 1.1 jonathan #endif
71 1.1 jonathan };
72 1.1 jonathan
73 1.1 jonathan /* Security Policy Data Base */
74 1.1 jonathan struct secpolicy {
75 1.1 jonathan LIST_ENTRY(secpolicy) chain;
76 1.1 jonathan
77 1.1 jonathan u_int refcnt; /* reference count */
78 1.1 jonathan struct secpolicyindex spidx; /* selector */
79 1.1 jonathan u_int32_t id; /* It's unique number on the system. */
80 1.1 jonathan u_int state; /* 0: dead, others: alive */
81 1.1 jonathan #define IPSEC_SPSTATE_DEAD 0
82 1.1 jonathan #define IPSEC_SPSTATE_ALIVE 1
83 1.1 jonathan
84 1.1 jonathan u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */
85 1.1 jonathan struct ipsecrequest *req;
86 1.1 jonathan /* pointer to the ipsec request tree, */
87 1.1 jonathan /* if policy == IPSEC else this value == NULL.*/
88 1.1 jonathan
89 1.1 jonathan /*
90 1.1 jonathan * lifetime handler.
91 1.1 jonathan * the policy can be used without limitiation if both lifetime and
92 1.1 jonathan * validtime are zero.
93 1.1 jonathan * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
94 1.1 jonathan * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
95 1.1 jonathan */
96 1.1 jonathan long created; /* time created the policy */
97 1.1 jonathan long lastused; /* updated every when kernel sends a packet */
98 1.1 jonathan long lifetime; /* duration of the lifetime of this policy */
99 1.1 jonathan long validtime; /* duration this policy is valid without use */
100 1.1 jonathan };
101 1.1 jonathan
102 1.1 jonathan /* Request for IPsec */
103 1.1 jonathan struct ipsecrequest {
104 1.1 jonathan struct ipsecrequest *next;
105 1.1 jonathan /* pointer to next structure */
106 1.1 jonathan /* If NULL, it means the end of chain. */
107 1.1 jonathan struct secasindex saidx;/* hint for search proper SA */
108 1.1 jonathan /* if __ss_len == 0 then no address specified.*/
109 1.1 jonathan u_int level; /* IPsec level defined below. */
110 1.1 jonathan
111 1.1 jonathan struct secasvar *sav; /* place holder of SA for use */
112 1.1 jonathan struct secpolicy *sp; /* back pointer to SP */
113 1.1 jonathan };
114 1.1 jonathan
115 1.1 jonathan /* security policy in PCB */
116 1.1 jonathan struct inpcbpolicy {
117 1.1 jonathan struct secpolicy *sp_in;
118 1.1 jonathan struct secpolicy *sp_out;
119 1.1 jonathan int priv; /* privileged socket ? */
120 1.7 thorpej
121 1.7 thorpej #ifdef __NetBSD__
122 1.7 thorpej /* cached policy */
123 1.7 thorpej struct {
124 1.7 thorpej struct secpolicy *cachesp;
125 1.7 thorpej struct secpolicyindex cacheidx;
126 1.7 thorpej int cachehint; /* processing requirement hint: */
127 1.7 thorpej #define IPSEC_PCBHINT_MAYBE 0 /* IPsec processing maybe required */
128 1.7 thorpej #define IPSEC_PCBHINT_YES 1 /* IPsec processing is required */
129 1.7 thorpej #define IPSEC_PCBHINT_NO 2 /* IPsec processing not required */
130 1.7 thorpej u_int cachegen; /* spdgen when cache filled */
131 1.7 thorpej } sp_cache[3]; /* XXX 3 == IPSEC_DIR_MAX */
132 1.7 thorpej int sp_cacheflags;
133 1.7 thorpej #define IPSEC_PCBSP_CONNECTED 1
134 1.7 thorpej #endif /* __NetBSD__ */
135 1.1 jonathan };
136 1.1 jonathan
137 1.7 thorpej #ifdef __NetBSD__
138 1.7 thorpej #define IPSEC_PCB_SKIP_IPSEC(inpp, dir) \
139 1.7 thorpej ((inpp)->sp_cache[(dir)].cachehint == IPSEC_PCBHINT_NO && \
140 1.7 thorpej (inpp)->sp_cache[(dir)].cachegen == ipsec_spdgen)
141 1.7 thorpej #endif /* __NetBSD__ */
142 1.7 thorpej
143 1.1 jonathan /* SP acquiring list table. */
144 1.1 jonathan struct secspacq {
145 1.1 jonathan LIST_ENTRY(secspacq) chain;
146 1.1 jonathan
147 1.1 jonathan struct secpolicyindex spidx;
148 1.1 jonathan
149 1.1 jonathan long created; /* for lifetime */
150 1.1 jonathan int count; /* for lifetime */
151 1.1 jonathan /* XXX: here is mbuf place holder to be sent ? */
152 1.1 jonathan };
153 1.1 jonathan #endif /* _KERNEL */
154 1.1 jonathan
155 1.1 jonathan /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
156 1.1 jonathan #define IPSEC_PORT_ANY 0
157 1.1 jonathan #define IPSEC_ULPROTO_ANY 255
158 1.1 jonathan #define IPSEC_PROTO_ANY 255
159 1.1 jonathan
160 1.1 jonathan /* mode of security protocol */
161 1.1 jonathan /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
162 1.1 jonathan #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
163 1.1 jonathan #define IPSEC_MODE_TRANSPORT 1
164 1.1 jonathan #define IPSEC_MODE_TUNNEL 2
165 1.8 jonathan #define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */
166 1.1 jonathan
167 1.1 jonathan /*
168 1.1 jonathan * Direction of security policy.
169 1.1 jonathan * NOTE: Since INVALID is used just as flag.
170 1.1 jonathan * The other are used for loop counter too.
171 1.1 jonathan */
172 1.1 jonathan #define IPSEC_DIR_ANY 0
173 1.1 jonathan #define IPSEC_DIR_INBOUND 1
174 1.1 jonathan #define IPSEC_DIR_OUTBOUND 2
175 1.1 jonathan #define IPSEC_DIR_MAX 3
176 1.1 jonathan #define IPSEC_DIR_INVALID 4
177 1.1 jonathan
178 1.1 jonathan /* Policy level */
179 1.1 jonathan /*
180 1.1 jonathan * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
181 1.1 jonathan * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
182 1.1 jonathan * DISCARD and NONE are allowed for system default.
183 1.1 jonathan */
184 1.1 jonathan #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
185 1.1 jonathan #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
186 1.1 jonathan #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
187 1.1 jonathan #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
188 1.1 jonathan #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
189 1.1 jonathan
190 1.1 jonathan /* Security protocol level */
191 1.1 jonathan #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
192 1.1 jonathan #define IPSEC_LEVEL_USE 1 /* use SA if present. */
193 1.1 jonathan #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
194 1.1 jonathan #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
195 1.1 jonathan
196 1.1 jonathan #define IPSEC_MANUAL_REQID_MAX 0x3fff
197 1.1 jonathan /*
198 1.1 jonathan * if security policy level == unique, this id
199 1.1 jonathan * indicate to a relative SA for use, else is
200 1.1 jonathan * zero.
201 1.1 jonathan * 1 - 0x3fff are reserved for manual keying.
202 1.1 jonathan * 0 are reserved for above reason. Others is
203 1.1 jonathan * for kernel use.
204 1.1 jonathan * Note that this id doesn't identify SA
205 1.1 jonathan * by only itself.
206 1.1 jonathan */
207 1.1 jonathan #define IPSEC_REPLAYWSIZE 32
208 1.1 jonathan
209 1.1 jonathan #ifdef _KERNEL
210 1.1 jonathan struct ipsec_output_state {
211 1.1 jonathan struct mbuf *m;
212 1.1 jonathan struct route *ro;
213 1.1 jonathan struct sockaddr *dst;
214 1.1 jonathan };
215 1.1 jonathan
216 1.1 jonathan struct ipsec_history {
217 1.1 jonathan int ih_proto;
218 1.1 jonathan u_int32_t ih_spi;
219 1.1 jonathan };
220 1.1 jonathan
221 1.1 jonathan extern int ipsec_debug;
222 1.1 jonathan
223 1.1 jonathan extern struct newipsecstat newipsecstat;
224 1.1 jonathan extern struct secpolicy ip4_def_policy;
225 1.1 jonathan extern int ip4_esp_trans_deflev;
226 1.1 jonathan extern int ip4_esp_net_deflev;
227 1.1 jonathan extern int ip4_ah_trans_deflev;
228 1.1 jonathan extern int ip4_ah_net_deflev;
229 1.1 jonathan extern int ip4_ah_cleartos;
230 1.1 jonathan extern int ip4_ah_offsetmask;
231 1.1 jonathan extern int ip4_ipsec_dfbit;
232 1.1 jonathan extern int ip4_ipsec_ecn;
233 1.1 jonathan extern int ip4_esp_randpad;
234 1.1 jonathan extern int crypto_support;
235 1.1 jonathan
236 1.1 jonathan #define ipseclog(x) do { if (ipsec_debug) log x; } while (0)
237 1.1 jonathan /* for openbsd compatibility */
238 1.1 jonathan #define DPRINTF(x) do { if (ipsec_debug) printf x; } while (0)
239 1.1 jonathan
240 1.7 thorpej #ifdef __NetBSD__
241 1.7 thorpej extern void ipsec_pcbconn __P((struct inpcbpolicy *));
242 1.7 thorpej extern void ipsec_pcbdisconn __P((struct inpcbpolicy *));
243 1.7 thorpej extern void ipsec_invalpcbcacheall __P((void));
244 1.7 thorpej
245 1.7 thorpej extern u_int ipsec_spdgen;
246 1.7 thorpej #endif /* __NetBSD__ */
247 1.7 thorpej
248 1.1 jonathan struct tdb_ident;
249 1.1 jonathan extern struct secpolicy *ipsec_getpolicy __P((struct tdb_ident*, u_int));
250 1.1 jonathan struct inpcb;
251 1.1 jonathan extern struct secpolicy *ipsec4_checkpolicy __P((struct mbuf *, u_int, u_int,
252 1.1 jonathan int *, struct inpcb *));
253 1.1 jonathan extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
254 1.1 jonathan int, int *);
255 1.1 jonathan
256 1.1 jonathan
257 1.14 perry static __inline struct secpolicy*
258 1.1 jonathan ipsec4_getpolicybysock(struct mbuf *m, u_int dir, const struct socket *so, int *err)
259 1.1 jonathan {
260 1.1 jonathan panic("ipsec4_getpolicybysock");
261 1.1 jonathan }
262 1.1 jonathan
263 1.14 perry static __inline int
264 1.1 jonathan ipsec_copy_pcbpolicy(struct inpcbpolicy *old, struct inpcbpolicy *new)
265 1.1 jonathan {
266 1.1 jonathan /*XXX do nothing */
267 1.1 jonathan return (0);
268 1.1 jonathan }
269 1.1 jonathan
270 1.1 jonathan struct inpcb;
271 1.1 jonathan #define ipsec_init_pcbpolicy ipsec_init_policy
272 1.1 jonathan extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **));
273 1.1 jonathan extern int ipsec_copy_policy
274 1.1 jonathan __P((struct inpcbpolicy *, struct inpcbpolicy *));
275 1.1 jonathan extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *));
276 1.1 jonathan extern int ipsec_in_reject __P((struct secpolicy *, struct mbuf *));
277 1.1 jonathan
278 1.1 jonathan extern int ipsec4_set_policy __P((struct inpcb *inp, int optname,
279 1.1 jonathan caddr_t request, size_t len, int priv));
280 1.1 jonathan extern int ipsec4_get_policy __P((struct inpcb *inpcb, caddr_t request,
281 1.1 jonathan size_t len, struct mbuf **mp));
282 1.1 jonathan extern int ipsec4_delete_pcbpolicy __P((struct inpcb *));
283 1.1 jonathan extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
284 1.1 jonathan /*
285 1.1 jonathan * KAME ipsec4_in_reject_so(struct mbuf*, struct so) compatibility shim
286 1.1 jonathan */
287 1.1 jonathan #define ipsec4_in_reject_so(m, _so) \
288 1.1 jonathan ipsec4_in_reject(m, ((_so) == NULL? NULL : sotoinpcb(_so)))
289 1.1 jonathan
290 1.1 jonathan
291 1.1 jonathan struct secas;
292 1.1 jonathan struct tcpcb;
293 1.1 jonathan extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
294 1.1 jonathan extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
295 1.1 jonathan
296 1.1 jonathan extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
297 1.1 jonathan #ifdef __FreeBSD__
298 1.1 jonathan extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
299 1.1 jonathan #else
300 1.1 jonathan extern size_t ipsec4_hdrsiz_tcp __P((struct tcpcb *));
301 1.1 jonathan #define ipsec4_getpolicybyaddr ipsec_getpolicybyaddr
302 1.1 jonathan #endif
303 1.1 jonathan
304 1.1 jonathan union sockaddr_union;
305 1.11 christos extern const char *ipsec_address(union sockaddr_union* sa);
306 1.1 jonathan extern const char *ipsec_logsastr __P((struct secasvar *));
307 1.1 jonathan
308 1.1 jonathan extern void ipsec_dumpmbuf __P((struct mbuf *));
309 1.1 jonathan
310 1.1 jonathan /* NetBSD protosw ctlin entrypoint */
311 1.1 jonathan extern void *esp4_ctlinput __P((int, struct sockaddr *, void *));
312 1.1 jonathan extern void *ah4_ctlinput __P((int, struct sockaddr *, void *));
313 1.1 jonathan
314 1.1 jonathan struct m_tag;
315 1.2 jonathan extern void ipsec4_common_input(struct mbuf *m, ...);
316 1.1 jonathan extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
317 1.1 jonathan int skip, int protoff, struct m_tag *mt);
318 1.1 jonathan extern int ipsec4_process_packet __P((struct mbuf *, struct ipsecrequest *,
319 1.1 jonathan int, int));
320 1.1 jonathan extern int ipsec_process_done __P((struct mbuf *, struct ipsecrequest *));
321 1.4 scw #define ipsec_indone(m) \
322 1.4 scw (m_tag_find((m), PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
323 1.1 jonathan
324 1.1 jonathan extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
325 1.1 jonathan
326 1.1 jonathan extern void m_checkalignment(const char* where, struct mbuf *m0,
327 1.1 jonathan int off, int len);
328 1.1 jonathan extern struct mbuf *m_clone(struct mbuf *m0);
329 1.1 jonathan extern struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
330 1.1 jonathan extern caddr_t m_pad(struct mbuf *m, int n);
331 1.1 jonathan extern int m_striphdr(struct mbuf *m, int skip, int hlen);
332 1.1 jonathan
333 1.1 jonathan /* Per-socket caching of IPsec output policy */
334 1.14 perry static __inline int ipsec_clear_socket_cache(struct mbuf *m)
335 1.1 jonathan {
336 1.1 jonathan return 0;
337 1.1 jonathan }
338 1.1 jonathan
339 1.1 jonathan
340 1.1 jonathan #endif /* _KERNEL */
341 1.1 jonathan
342 1.1 jonathan #ifndef _KERNEL
343 1.1 jonathan extern caddr_t ipsec_set_policy __P((char *, int));
344 1.1 jonathan extern int ipsec_get_policylen __P((caddr_t));
345 1.1 jonathan extern char *ipsec_dump_policy __P((caddr_t, char *));
346 1.1 jonathan
347 1.1 jonathan extern const char *ipsec_strerror __P((void));
348 1.1 jonathan #endif /* !_KERNEL */
349 1.1 jonathan
350 1.9 jonathan #ifdef _KERNEL
351 1.1 jonathan /* External declarations of per-file init functions */
352 1.1 jonathan INITFN void ah_attach(void);
353 1.1 jonathan INITFN void esp_attach(void);
354 1.1 jonathan INITFN void ipcomp_attach(void);
355 1.1 jonathan INITFN void ipe4_attach(void);
356 1.8 jonathan INITFN void ipe4_attach(void);
357 1.8 jonathan INITFN void tcpsignature_attach(void);
358 1.1 jonathan
359 1.1 jonathan INITFN void ipsec_attach(void);
360 1.9 jonathan #endif /* _KERNEL */
361 1.12 elad #endif /* !_NETIPSEC_IPSEC_H_ */
362