xref: /freebsd/sys/netipsec/ipsec.h (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 #include <sys/_lock.h>
51 #include <sys/_mutex.h>
52 #include <sys/_rwlock.h>
53 
54 #define	IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
55 
56 #define	IPSEC_IS_PRIVILEGED_SO(_so) \
57 	((_so)->so_cred != NULL && \
58 	 priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \
59 	 == 0)
60 
61 /*
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.
66  */
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 */
74 #ifdef notyet
75 	uid_t uids;
76 	uid_t uidd;
77 	gid_t gids;
78 	gid_t gidd;
79 #endif
80 };
81 
82 /* Security Policy Data Base */
83 struct secpolicy {
84 	TAILQ_ENTRY(secpolicy) chain;
85 
86 	struct secpolicyindex spidx;	/* selector */
87 	struct ipsecrequest *req;
88 				/* pointer to the ipsec request tree, */
89 				/* if policy == IPSEC else this value == NULL.*/
90 	u_int refcnt;			/* reference count */
91 	u_int policy;			/* policy_type per pfkeyv2.h */
92 	u_int32_t id;			/* It's unique number on the system. */
93 	/*
94 	 * lifetime handler.
95 	 * the policy can be used without limitiation if both lifetime and
96 	 * validtime are zero.
97 	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
98 	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
99 	 */
100 	time_t created;		/* time created the policy */
101 	time_t lastused;	/* updated every when kernel sends a packet */
102 	long lifetime;		/* duration of the lifetime of this policy */
103 	long validtime;		/* duration this policy is valid without use */
104 };
105 
106 /* Request for IPsec */
107 struct ipsecrequest {
108 	struct ipsecrequest *next;
109 				/* pointer to next structure */
110 				/* If NULL, it means the end of chain. */
111 	struct secasindex saidx;/* hint for search proper SA */
112 				/* if __ss_len == 0 then no address specified.*/
113 	u_int level;		/* IPsec level defined below. */
114 
115 	struct secasvar *sav;	/* place holder of SA for use */
116 	struct secpolicy *sp;	/* back pointer to SP */
117 	struct rwlock lock;	/* to interlock updates */
118 };
119 
120 /*
121  * Need recursion for when crypto callbacks happen directly,
122  * as in the case of software crypto.  Need to look at how
123  * hard it is to remove this...
124  */
125 #define	IPSECREQUEST_LOCK_INIT(_isr) \
126 	rw_init_flags(&(_isr)->lock, "ipsec request", RW_RECURSE)
127 #define	IPSECREQUEST_LOCK(_isr)		rw_rlock(&(_isr)->lock)
128 #define	IPSECREQUEST_UNLOCK(_isr)	rw_runlock(&(_isr)->lock)
129 #define	IPSECREQUEST_WLOCK(_isr)	rw_wlock(&(_isr)->lock)
130 #define	IPSECREQUEST_WUNLOCK(_isr)	rw_wunlock(&(_isr)->lock)
131 #define	IPSECREQUEST_UPGRADE(_isr)	rw_try_upgrade(&(_isr)->lock)
132 #define	IPSECREQUEST_DOWNGRADE(_isr)	rw_downgrade(&(_isr)->lock)
133 #define	IPSECREQUEST_LOCK_DESTROY(_isr)	rw_destroy(&(_isr)->lock)
134 #define	IPSECREQUEST_LOCK_ASSERT(_isr)	rw_assert(&(_isr)->lock, RA_LOCKED)
135 
136 /* security policy in PCB */
137 struct inpcbpolicy {
138 	struct secpolicy *sp_in;
139 	struct secpolicy *sp_out;
140 	int priv;			/* privileged socket ? */
141 };
142 
143 /* SP acquiring list table. */
144 struct secspacq {
145 	LIST_ENTRY(secspacq) chain;
146 
147 	struct secpolicyindex spidx;
148 
149 	time_t created;		/* for lifetime */
150 	int count;		/* for lifetime */
151 	/* XXX: here is mbuf place holder to be sent ? */
152 };
153 #endif /* _KERNEL */
154 
155 /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
156 #define IPSEC_PORT_ANY		0
157 #define IPSEC_ULPROTO_ANY	255
158 #define IPSEC_PROTO_ANY		255
159 
160 /* mode of security protocol */
161 /* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
162 #define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
163 #define	IPSEC_MODE_TRANSPORT	1
164 #define	IPSEC_MODE_TUNNEL	2
165 #define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
166 
167 /*
168  * Direction of security policy.
169  * NOTE: Since INVALID is used just as flag.
170  * The other are used for loop counter too.
171  */
172 #define IPSEC_DIR_ANY		0
173 #define IPSEC_DIR_INBOUND	1
174 #define IPSEC_DIR_OUTBOUND	2
175 #define IPSEC_DIR_MAX		3
176 #define IPSEC_DIR_INVALID	4
177 
178 /* Policy level */
179 /*
180  * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
181  * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
182  * DISCARD and NONE are allowed for system default.
183  */
184 #define IPSEC_POLICY_DISCARD	0	/* discarding packet */
185 #define IPSEC_POLICY_NONE	1	/* through IPsec engine */
186 #define IPSEC_POLICY_IPSEC	2	/* do IPsec */
187 #define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
188 #define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
189 
190 /* Security protocol level */
191 #define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
192 #define	IPSEC_LEVEL_USE		1	/* use SA if present. */
193 #define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
194 #define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
195 
196 #define IPSEC_MANUAL_REQID_MAX	0x3fff
197 				/*
198 				 * if security policy level == unique, this id
199 				 * indicate to a relative SA for use, else is
200 				 * zero.
201 				 * 1 - 0x3fff are reserved for manual keying.
202 				 * 0 are reserved for above reason.  Others is
203 				 * for kernel use.
204 				 * Note that this id doesn't identify SA
205 				 * by only itself.
206 				 */
207 #define IPSEC_REPLAYWSIZE  32
208 
209 /* statistics for ipsec processing */
210 struct ipsecstat {
211 	uint64_t ips_in_polvio;		/* input: sec policy violation */
212 	uint64_t ips_in_nomem;		/* input: no memory available */
213 	uint64_t ips_in_inval;		/* input: generic error */
214 
215 	uint64_t ips_out_polvio;	/* output: sec policy violation */
216 	uint64_t ips_out_nosa;		/* output: SA unavailable  */
217 	uint64_t ips_out_nomem;		/* output: no memory available */
218 	uint64_t ips_out_noroute;	/* output: no route available */
219 	uint64_t ips_out_inval;		/* output: generic error */
220 	uint64_t ips_out_bundlesa;	/* output: bundled SA processed */
221 
222 	uint64_t ips_mbcoalesced;	/* mbufs coalesced during clone */
223 	uint64_t ips_clcoalesced;	/* clusters coalesced during clone */
224 	uint64_t ips_clcopied;		/* clusters copied during clone */
225 	uint64_t ips_mbinserted;	/* mbufs inserted during makespace */
226 	/*
227 	 * Temporary statistics for performance analysis.
228 	 */
229 	/* See where ESP/AH/IPCOMP header land in mbuf on input */
230 	uint64_t ips_input_front;
231 	uint64_t ips_input_middle;
232 	uint64_t ips_input_end;
233 };
234 
235 /*
236  * Definitions for IPsec & Key sysctl operations.
237  */
238 #define IPSECCTL_STATS			1	/* stats */
239 #define IPSECCTL_DEF_POLICY		2
240 #define IPSECCTL_DEF_ESP_TRANSLEV	3	/* int; ESP transport mode */
241 #define IPSECCTL_DEF_ESP_NETLEV		4	/* int; ESP tunnel mode */
242 #define IPSECCTL_DEF_AH_TRANSLEV	5	/* int; AH transport mode */
243 #define IPSECCTL_DEF_AH_NETLEV		6	/* int; AH tunnel mode */
244 #if 0	/* obsolete, do not reuse */
245 #define IPSECCTL_INBOUND_CALL_IKE	7
246 #endif
247 #define	IPSECCTL_AH_CLEARTOS		8
248 #define	IPSECCTL_AH_OFFSETMASK		9
249 #define	IPSECCTL_DFBIT			10
250 #define	IPSECCTL_ECN			11
251 #define	IPSECCTL_DEBUG			12
252 #define	IPSECCTL_ESP_RANDPAD		13
253 
254 #ifdef _KERNEL
255 #include <sys/counter.h>
256 
257 VNET_DECLARE(int, ipsec_debug);
258 #define	V_ipsec_debug		VNET(ipsec_debug)
259 
260 #ifdef REGRESSION
261 VNET_DECLARE(int, ipsec_replay);
262 VNET_DECLARE(int, ipsec_integrity);
263 
264 #define	V_ipsec_replay		VNET(ipsec_replay)
265 #define	V_ipsec_integrity	VNET(ipsec_integrity)
266 #endif
267 
268 VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
269 VNET_DECLARE(int, ip4_esp_trans_deflev);
270 VNET_DECLARE(int, ip4_esp_net_deflev);
271 VNET_DECLARE(int, ip4_ah_trans_deflev);
272 VNET_DECLARE(int, ip4_ah_net_deflev);
273 VNET_DECLARE(int, ip4_ah_offsetmask);
274 VNET_DECLARE(int, ip4_ipsec_dfbit);
275 VNET_DECLARE(int, ip4_ipsec_ecn);
276 VNET_DECLARE(int, ip4_esp_randpad);
277 VNET_DECLARE(int, crypto_support);
278 
279 #define	IPSECSTAT_INC(name)	\
280     VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
281 #define	V_ip4_esp_trans_deflev	VNET(ip4_esp_trans_deflev)
282 #define	V_ip4_esp_net_deflev	VNET(ip4_esp_net_deflev)
283 #define	V_ip4_ah_trans_deflev	VNET(ip4_ah_trans_deflev)
284 #define	V_ip4_ah_net_deflev	VNET(ip4_ah_net_deflev)
285 #define	V_ip4_ah_offsetmask	VNET(ip4_ah_offsetmask)
286 #define	V_ip4_ipsec_dfbit	VNET(ip4_ipsec_dfbit)
287 #define	V_ip4_ipsec_ecn		VNET(ip4_ipsec_ecn)
288 #define	V_ip4_esp_randpad	VNET(ip4_esp_randpad)
289 #define	V_crypto_support	VNET(crypto_support)
290 
291 #define ipseclog(x)	do { if (V_ipsec_debug) log x; } while (0)
292 /* for openbsd compatibility */
293 #define	DPRINTF(x)	do { if (V_ipsec_debug) printf x; } while (0)
294 
295 extern	struct ipsecrequest *ipsec_newisr(void);
296 extern	void ipsec_delisr(struct ipsecrequest *);
297 
298 struct tdb_ident;
299 extern struct secpolicy *ipsec_getpolicy(struct tdb_ident*, u_int);
300 struct inpcb;
301 extern struct secpolicy *ipsec4_checkpolicy(struct mbuf *, u_int,
302 	int *, struct inpcb *);
303 extern struct secpolicy * ipsec_getpolicybyaddr(struct mbuf *, u_int, int *);
304 
305 struct inpcb;
306 extern int ipsec_init_policy(struct socket *so, struct inpcbpolicy **);
307 extern int ipsec_copy_policy(struct inpcbpolicy *, struct inpcbpolicy *);
308 extern u_int ipsec_get_reqlevel(struct ipsecrequest *);
309 extern int ipsec_in_reject(struct secpolicy *, struct mbuf *);
310 
311 extern int ipsec_set_policy(struct inpcb *inp, int optname,
312 	caddr_t request, size_t len, struct ucred *cred);
313 extern int ipsec_get_policy(struct inpcb *inpcb, caddr_t request,
314 	size_t len, struct mbuf **mp);
315 extern int ipsec_delete_pcbpolicy(struct inpcb *);
316 extern int ipsec4_in_reject(struct mbuf *, struct inpcb *);
317 
318 struct secas;
319 struct tcpcb;
320 extern int ipsec_chkreplay(u_int32_t, struct secasvar *);
321 extern int ipsec_updatereplay(u_int32_t, struct secasvar *);
322 
323 extern size_t ipsec_hdrsiz(struct mbuf *, u_int, struct inpcb *);
324 extern size_t ipsec_hdrsiz_tcp(struct tcpcb *);
325 
326 union sockaddr_union;
327 extern char * ipsec_address(union sockaddr_union* sa);
328 extern const char *ipsec_logsastr(struct secasvar *);
329 
330 extern void ipsec_dumpmbuf(struct mbuf *);
331 
332 struct m_tag;
333 extern int ah4_input(struct mbuf **mp, int *offp, int proto);
334 extern void ah4_ctlinput(int cmd, struct sockaddr *sa, void *);
335 extern int esp4_input(struct mbuf **mp, int *offp, int proto);
336 extern void esp4_ctlinput(int cmd, struct sockaddr *sa, void *);
337 extern int ipcomp4_input(struct mbuf **mp, int *offp, int proto);
338 extern int ipsec4_common_input(struct mbuf *m, ...);
339 extern int ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
340 			int skip, int protoff);
341 extern int ipsec4_process_packet(struct mbuf *, struct ipsecrequest *);
342 extern int ipsec_process_done(struct mbuf *, struct ipsecrequest *);
343 
344 extern struct mbuf *ipsec_copypkt(struct mbuf *);
345 
346 extern	void m_checkalignment(const char* where, struct mbuf *m0,
347 		int off, int len);
348 extern	struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
349 extern	caddr_t m_pad(struct mbuf *m, int n);
350 extern	int m_striphdr(struct mbuf *m, int skip, int hlen);
351 
352 #ifdef DEV_ENC
353 #define	ENC_BEFORE	0x0001
354 #define	ENC_AFTER	0x0002
355 #define	ENC_IN		0x0100
356 #define	ENC_OUT		0x0200
357 extern	int ipsec_filter(struct mbuf **, int, int);
358 extern	void ipsec_bpf(struct mbuf *, struct secasvar *, int, int);
359 #endif
360 #endif /* _KERNEL */
361 
362 #ifndef _KERNEL
363 extern caddr_t ipsec_set_policy(char *, int);
364 extern int ipsec_get_policylen(caddr_t);
365 extern char *ipsec_dump_policy(caddr_t, char *);
366 extern const char *ipsec_strerror(void);
367 
368 #endif /* ! KERNEL */
369 
370 #endif /* _NETIPSEC_IPSEC_H_ */
371