xref: /freebsd/sys/netipsec/ipsec.h (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*	$FreeBSD$	*/
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 /*
51  * Security Policy Index
52  * Ensure that both address families in the "src" and "dst" are same.
53  * When the value of the ul_proto is ICMPv6, the port field in "src"
54  * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
55  */
56 struct secpolicyindex {
57 	u_int8_t dir;			/* direction of packet flow, see blow */
58 	union sockaddr_union src;	/* IP src address for SP */
59 	union sockaddr_union dst;	/* IP dst address for SP */
60 	u_int8_t prefs;			/* prefix length in bits for src */
61 	u_int8_t prefd;			/* prefix length in bits for dst */
62 	u_int16_t ul_proto;		/* upper layer Protocol */
63 #ifdef notyet
64 	uid_t uids;
65 	uid_t uidd;
66 	gid_t gids;
67 	gid_t gidd;
68 #endif
69 };
70 
71 /* Security Policy Data Base */
72 struct secpolicy {
73 	LIST_ENTRY(secpolicy) chain;
74 	struct mtx lock;
75 
76 	u_int refcnt;			/* reference count */
77 	struct secpolicyindex spidx;	/* selector */
78 	u_int32_t id;			/* It's unique number on the system. */
79 	u_int state;			/* 0: dead, others: alive */
80 #define IPSEC_SPSTATE_DEAD	0
81 #define IPSEC_SPSTATE_ALIVE	1
82 
83 	u_int policy;		/* DISCARD, NONE or IPSEC, see keyv2.h */
84 	struct ipsecrequest *req;
85 				/* pointer to the ipsec request tree, */
86 				/* if policy == IPSEC else this value == NULL.*/
87 
88 	/*
89 	 * lifetime handler.
90 	 * the policy can be used without limitiation if both lifetime and
91 	 * validtime are zero.
92 	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
93 	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
94 	 */
95 	long created;		/* time created the policy */
96 	long lastused;		/* updated every when kernel sends a packet */
97 	long lifetime;		/* duration of the lifetime of this policy */
98 	long validtime;		/* duration this policy is valid without use */
99 };
100 
101 /* Request for IPsec */
102 struct ipsecrequest {
103 	struct ipsecrequest *next;
104 				/* pointer to next structure */
105 				/* If NULL, it means the end of chain. */
106 	struct secasindex saidx;/* hint for search proper SA */
107 				/* if __ss_len == 0 then no address specified.*/
108 	u_int level;		/* IPsec level defined below. */
109 
110 	struct secasvar *sav;	/* place holder of SA for use */
111 	struct secpolicy *sp;	/* back pointer to SP */
112 	struct mtx lock;	/* to interlock updates */
113 };
114 
115 /* security policy in PCB */
116 struct inpcbpolicy {
117 	struct secpolicy *sp_in;
118 	struct secpolicy *sp_out;
119 	int priv;			/* privileged socket ? */
120 };
121 
122 /* SP acquiring list table. */
123 struct secspacq {
124 	LIST_ENTRY(secspacq) chain;
125 
126 	struct secpolicyindex spidx;
127 
128 	long created;		/* for lifetime */
129 	int count;		/* for lifetime */
130 	/* XXX: here is mbuf place holder to be sent ? */
131 };
132 #endif /* _KERNEL */
133 
134 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
135 #define IPSEC_PORT_ANY		0
136 #define IPSEC_ULPROTO_ANY	255
137 #define IPSEC_PROTO_ANY		255
138 
139 /* mode of security protocol */
140 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
141 #define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
142 #define	IPSEC_MODE_TRANSPORT	1
143 #define	IPSEC_MODE_TUNNEL	2
144 
145 /*
146  * Direction of security policy.
147  * NOTE: Since INVALID is used just as flag.
148  * The other are used for loop counter too.
149  */
150 #define IPSEC_DIR_ANY		0
151 #define IPSEC_DIR_INBOUND	1
152 #define IPSEC_DIR_OUTBOUND	2
153 #define IPSEC_DIR_MAX		3
154 #define IPSEC_DIR_INVALID	4
155 
156 /* Policy level */
157 /*
158  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
159  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
160  * DISCARD and NONE are allowed for system default.
161  */
162 #define IPSEC_POLICY_DISCARD	0	/* discarding packet */
163 #define IPSEC_POLICY_NONE	1	/* through IPsec engine */
164 #define IPSEC_POLICY_IPSEC	2	/* do IPsec */
165 #define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
166 #define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
167 
168 /* Security protocol level */
169 #define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
170 #define	IPSEC_LEVEL_USE		1	/* use SA if present. */
171 #define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
172 #define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
173 
174 #define IPSEC_MANUAL_REQID_MAX	0x3fff
175 				/*
176 				 * if security policy level == unique, this id
177 				 * indicate to a relative SA for use, else is
178 				 * zero.
179 				 * 1 - 0x3fff are reserved for manual keying.
180 				 * 0 are reserved for above reason.  Others is
181 				 * for kernel use.
182 				 * Note that this id doesn't identify SA
183 				 * by only itself.
184 				 */
185 #define IPSEC_REPLAYWSIZE  32
186 
187 /* old statistics for ipsec processing */
188 struct ipsecstat {
189 	u_quad_t in_success;  /* succeeded inbound process */
190 	u_quad_t in_polvio;
191 			/* security policy violation for inbound process */
192 	u_quad_t in_nosa;     /* inbound SA is unavailable */
193 	u_quad_t in_inval;    /* inbound processing failed due to EINVAL */
194 	u_quad_t in_nomem;    /* inbound processing failed due to ENOBUFS */
195 	u_quad_t in_badspi;   /* failed getting a SPI */
196 	u_quad_t in_ahreplay; /* AH replay check failed */
197 	u_quad_t in_espreplay; /* ESP replay check failed */
198 	u_quad_t in_ahauthsucc; /* AH authentication success */
199 	u_quad_t in_ahauthfail; /* AH authentication failure */
200 	u_quad_t in_espauthsucc; /* ESP authentication success */
201 	u_quad_t in_espauthfail; /* ESP authentication failure */
202 	u_quad_t in_esphist[256];
203 	u_quad_t in_ahhist[256];
204 	u_quad_t in_comphist[256];
205 	u_quad_t out_success; /* succeeded outbound process */
206 	u_quad_t out_polvio;
207 			/* security policy violation for outbound process */
208 	u_quad_t out_nosa;    /* outbound SA is unavailable */
209 	u_quad_t out_inval;   /* outbound process failed due to EINVAL */
210 	u_quad_t out_nomem;    /* inbound processing failed due to ENOBUFS */
211 	u_quad_t out_noroute; /* there is no route */
212 	u_quad_t out_esphist[256];
213 	u_quad_t out_ahhist[256];
214 	u_quad_t out_comphist[256];
215 };
216 
217 /* statistics for ipsec processing */
218 struct newipsecstat {
219 	u_int32_t ips_in_polvio;	/* input: sec policy violation */
220 	u_int32_t ips_out_polvio;	/* output: sec policy violation */
221 	u_int32_t ips_out_nosa;		/* output: SA unavailable  */
222 	u_int32_t ips_out_nomem;	/* output: no memory available */
223 	u_int32_t ips_out_noroute;	/* output: no route available */
224 	u_int32_t ips_out_inval;	/* output: generic error */
225 	u_int32_t ips_out_bundlesa;	/* output: bundled SA processed */
226 	u_int32_t ips_mbcoalesced;	/* mbufs coalesced during clone */
227 	u_int32_t ips_clcoalesced;	/* clusters coalesced during clone */
228 	u_int32_t ips_clcopied;		/* clusters copied during clone */
229 	u_int32_t ips_mbinserted;	/* mbufs inserted during makespace */
230 	/*
231 	 * Temporary statistics for performance analysis.
232 	 */
233 	/* See where ESP/AH/IPCOMP header land in mbuf on input */
234 	u_int32_t ips_input_front;
235 	u_int32_t ips_input_middle;
236 	u_int32_t ips_input_end;
237 };
238 
239 /*
240  * Definitions for IPsec & Key sysctl operations.
241  */
242 /*
243  * Names for IPsec & Key sysctl objects
244  */
245 #define IPSECCTL_STATS			1	/* stats */
246 #define IPSECCTL_DEF_POLICY		2
247 #define IPSECCTL_DEF_ESP_TRANSLEV	3	/* int; ESP transport mode */
248 #define IPSECCTL_DEF_ESP_NETLEV		4	/* int; ESP tunnel mode */
249 #define IPSECCTL_DEF_AH_TRANSLEV	5	/* int; AH transport mode */
250 #define IPSECCTL_DEF_AH_NETLEV		6	/* int; AH tunnel mode */
251 #if 0	/* obsolete, do not reuse */
252 #define IPSECCTL_INBOUND_CALL_IKE	7
253 #endif
254 #define	IPSECCTL_AH_CLEARTOS		8
255 #define	IPSECCTL_AH_OFFSETMASK		9
256 #define	IPSECCTL_DFBIT			10
257 #define	IPSECCTL_ECN			11
258 #define	IPSECCTL_DEBUG			12
259 #define	IPSECCTL_ESP_RANDPAD		13
260 #define IPSECCTL_MAXID			14
261 
262 #define IPSECCTL_NAMES { \
263 	{ 0, 0 }, \
264 	{ 0, 0 }, \
265 	{ "def_policy", CTLTYPE_INT }, \
266 	{ "esp_trans_deflev", CTLTYPE_INT }, \
267 	{ "esp_net_deflev", CTLTYPE_INT }, \
268 	{ "ah_trans_deflev", CTLTYPE_INT }, \
269 	{ "ah_net_deflev", CTLTYPE_INT }, \
270 	{ 0, 0 }, \
271 	{ "ah_cleartos", CTLTYPE_INT }, \
272 	{ "ah_offsetmask", CTLTYPE_INT }, \
273 	{ "dfbit", CTLTYPE_INT }, \
274 	{ "ecn", CTLTYPE_INT }, \
275 	{ "debug", CTLTYPE_INT }, \
276 	{ "esp_randpad", CTLTYPE_INT }, \
277 }
278 
279 #define IPSEC6CTL_NAMES { \
280 	{ 0, 0 }, \
281 	{ 0, 0 }, \
282 	{ "def_policy", CTLTYPE_INT }, \
283 	{ "esp_trans_deflev", CTLTYPE_INT }, \
284 	{ "esp_net_deflev", CTLTYPE_INT }, \
285 	{ "ah_trans_deflev", CTLTYPE_INT }, \
286 	{ "ah_net_deflev", CTLTYPE_INT }, \
287 	{ 0, 0 }, \
288 	{ 0, 0 }, \
289 	{ 0, 0 }, \
290 	{ 0, 0 }, \
291 	{ "ecn", CTLTYPE_INT }, \
292 	{ "debug", CTLTYPE_INT }, \
293 	{ "esp_randpad", CTLTYPE_INT }, \
294 }
295 
296 #ifdef _KERNEL
297 struct ipsec_output_state {
298 	struct mbuf *m;
299 	struct route *ro;
300 	struct sockaddr *dst;
301 };
302 
303 struct ipsec_history {
304 	int ih_proto;
305 	u_int32_t ih_spi;
306 };
307 
308 extern int ipsec_debug;
309 
310 extern struct newipsecstat newipsecstat;
311 extern struct secpolicy ip4_def_policy;
312 extern int ip4_esp_trans_deflev;
313 extern int ip4_esp_net_deflev;
314 extern int ip4_ah_trans_deflev;
315 extern int ip4_ah_net_deflev;
316 extern int ip4_ah_cleartos;
317 extern int ip4_ah_offsetmask;
318 extern int ip4_ipsec_dfbit;
319 extern int ip4_ipsec_ecn;
320 extern int ip4_esp_randpad;
321 extern int crypto_support;
322 
323 #define ipseclog(x)	do { if (ipsec_debug) log x; } while (0)
324 /* for openbsd compatibility */
325 #define	DPRINTF(x)	do { if (ipsec_debug) printf x; } while (0)
326 
327 extern	struct ipsecrequest *ipsec_newisr(void);
328 extern	void ipsec_delisr(struct ipsecrequest *);
329 
330 struct tdb_ident;
331 extern struct secpolicy *ipsec_getpolicy __P((struct tdb_ident*, u_int));
332 struct inpcb;
333 extern struct secpolicy *ipsec4_checkpolicy __P((struct mbuf *, u_int, u_int,
334 	int *, struct inpcb *));
335 extern struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
336 	struct inpcb *, int *);
337 extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int,
338 	int, int *);
339 
340 struct inpcb;
341 extern int ipsec_init_policy __P((struct socket *so, struct inpcbpolicy **));
342 extern int ipsec_copy_policy
343 	__P((struct inpcbpolicy *, struct inpcbpolicy *));
344 extern u_int ipsec_get_reqlevel __P((struct ipsecrequest *));
345 extern int ipsec_in_reject __P((struct secpolicy *, struct mbuf *));
346 
347 extern int ipsec4_set_policy __P((struct inpcb *inp, int optname,
348 	caddr_t request, size_t len, int priv));
349 extern int ipsec4_get_policy __P((struct inpcb *inpcb, caddr_t request,
350 	size_t len, struct mbuf **mp));
351 extern int ipsec4_delete_pcbpolicy __P((struct inpcb *));
352 extern int ipsec4_in_reject __P((struct mbuf *, struct inpcb *));
353 
354 struct secas;
355 struct tcpcb;
356 extern int ipsec_chkreplay __P((u_int32_t, struct secasvar *));
357 extern int ipsec_updatereplay __P((u_int32_t, struct secasvar *));
358 
359 extern size_t ipsec4_hdrsiz __P((struct mbuf *, u_int, struct inpcb *));
360 extern size_t ipsec_hdrsiz_tcp __P((struct tcpcb *));
361 
362 union sockaddr_union;
363 extern char * ipsec_address(union sockaddr_union* sa);
364 extern const char *ipsec_logsastr __P((struct secasvar *));
365 
366 extern void ipsec_dumpmbuf __P((struct mbuf *));
367 
368 struct m_tag;
369 extern void ah4_input(struct mbuf *m, int off);
370 extern void esp4_input(struct mbuf *m, int off);
371 extern void ipcomp4_input(struct mbuf *m, int off);
372 extern int ipsec4_common_input(struct mbuf *m, ...);
373 extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
374 			int skip, int protoff, struct m_tag *mt);
375 extern int ipsec4_process_packet __P((struct mbuf *, struct ipsecrequest *,
376 			int, int));
377 extern int ipsec_process_done __P((struct mbuf *, struct ipsecrequest *));
378 
379 extern struct mbuf *ipsec_copypkt __P((struct mbuf *));
380 
381 extern	void m_checkalignment(const char* where, struct mbuf *m0,
382 		int off, int len);
383 extern	struct mbuf *m_clone(struct mbuf *m0);
384 extern	struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
385 extern	caddr_t m_pad(struct mbuf *m, int n);
386 extern	int m_striphdr(struct mbuf *m, int skip, int hlen);
387 #endif /* _KERNEL */
388 
389 #ifndef _KERNEL
390 extern caddr_t ipsec_set_policy __P((char *, int));
391 extern int ipsec_get_policylen __P((caddr_t));
392 extern char *ipsec_dump_policy __P((caddr_t, char *));
393 
394 extern const char *ipsec_strerror __P((void));
395 #endif /* !_KERNEL */
396 
397 #endif /* _NETIPSEC_IPSEC_H_ */
398