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