ip_ipsp.h revision 1.232
1/*	$OpenBSD: ip_ipsp.h,v 1.232 2021/12/19 23:30:08 bluhm Exp $	*/
2/*
3 * The authors of this code are John Ioannidis (ji@tla.org),
4 * Angelos D. Keromytis (kermit@csd.uch.gr),
5 * Niels Provos (provos@physnet.uni-hamburg.de) and
6 * Niklas Hallqvist (niklas@appli.se).
7 *
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
18 *
19 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
21 * Copyright (c) 1999 Niklas Hallqvist.
22 * Copyright (c) 2001, Angelos D. Keromytis.
23 *
24 * Permission to use, copy, and modify this software with or without fee
25 * is hereby granted, provided that this entire notice is included in
26 * all copies of any software which is or includes a copy or
27 * modification of this software.
28 * You may use this code under the GNU public license if you so wish. Please
29 * contribute changes back to the authors under this freer than GPL license
30 * so that we may further the use of strong encryption without limitations to
31 * all.
32 *
33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37 * PURPOSE.
38 */
39
40#ifndef _NETINET_IPSP_H_
41#define _NETINET_IPSP_H_
42
43/* IPSP global definitions. */
44
45#include <sys/types.h>
46#include <netinet/in.h>
47
48union sockaddr_union {
49	struct sockaddr		sa;
50	struct sockaddr_in	sin;
51	struct sockaddr_in6	sin6;
52};
53
54#define	AH_HMAC_MAX_HASHLEN	32	/* 256 bits of authenticator for SHA512 */
55#define	AH_HMAC_RPLENGTH	4	/* 32 bits of replay counter */
56#define	AH_HMAC_INITIAL_RPL	1	/* Replay counter initial value */
57
58/* Authenticator lengths */
59#define	AH_MD5_ALEN		16
60#define	AH_SHA1_ALEN		20
61#define	AH_RMD160_ALEN		20
62#define	AH_SHA2_256_ALEN	32
63#define	AH_SHA2_384_ALEN	48
64#define	AH_SHA2_512_ALEN	64
65#define	AH_ALEN_MAX		64	/* Keep updated */
66
67/* Reserved SPI numbers */
68#define	SPI_LOCAL_USE		0
69#define	SPI_RESERVED_MIN	1
70#define	SPI_RESERVED_MAX	255
71
72/* Reserved CPI numbers */
73#define CPI_RESERVED_MIN	1
74#define CPI_RESERVED_MAX	255
75#define CPI_PRIVATE_MIN		61440
76#define CPI_PRIVATE_MAX		65535
77
78/* sysctl default values */
79#define	IPSEC_DEFAULT_EMBRYONIC_SA_TIMEOUT	60	/* 1 minute */
80#define	IPSEC_DEFAULT_PFS			1
81#define	IPSEC_DEFAULT_SOFT_ALLOCATIONS		0
82#define	IPSEC_DEFAULT_EXP_ALLOCATIONS		0
83#define	IPSEC_DEFAULT_SOFT_BYTES		0
84#define	IPSEC_DEFAULT_EXP_BYTES			0
85#define	IPSEC_DEFAULT_SOFT_TIMEOUT		80000
86#define	IPSEC_DEFAULT_EXP_TIMEOUT		86400
87#define	IPSEC_DEFAULT_SOFT_FIRST_USE		3600
88#define	IPSEC_DEFAULT_EXP_FIRST_USE		7200
89#define	IPSEC_DEFAULT_DEF_ENC			"aes"
90#define	IPSEC_DEFAULT_DEF_AUTH			"hmac-sha1"
91#define	IPSEC_DEFAULT_EXPIRE_ACQUIRE		30
92#define	IPSEC_DEFAULT_DEF_COMP			"deflate"
93
94struct sockaddr_encap {
95	u_int8_t	sen_len;		/* length */
96	u_int8_t	sen_family;		/* PF_KEY */
97	u_int16_t	sen_type;		/* see SENT_* */
98	union {
99		struct {				/* SENT_IP4 */
100			u_int8_t	Direction;
101			struct in_addr	Src;
102			struct in_addr	Dst;
103			u_int8_t	Proto;
104			u_int16_t	Sport;
105			u_int16_t	Dport;
106		} Sip4;
107
108		struct {				/* SENT_IP6 */
109			u_int8_t	Direction;
110			struct in6_addr	Src;
111			struct in6_addr	Dst;
112			u_int8_t	Proto;
113			u_int16_t	Sport;
114			u_int16_t	Dport;
115		} Sip6;
116	} Sen;
117};
118
119#define	IPSP_DIRECTION_IN	0x1
120#define	IPSP_DIRECTION_OUT	0x2
121
122struct ipsecstat {
123	uint64_t	ipsec_tunnels;		/* Number of active tunnels */
124	uint64_t	ipsec_prevtunnels;	/* Past number of tunnels */
125	uint64_t	ipsec_ipackets;		/* Input IPsec packets */
126	uint64_t	ipsec_opackets;		/* Output IPsec packets */
127	uint64_t	ipsec_ibytes;		/* Input bytes */
128	uint64_t	ipsec_obytes;		/* Output bytes */
129	uint64_t	ipsec_idecompbytes;	/* Input bytes, decompressed */
130	uint64_t	ipsec_ouncompbytes;	/* Output bytes, uncompressed */
131	uint64_t	ipsec_idrops;		/* Dropped on input */
132	uint64_t	ipsec_odrops;		/* Dropped on output */
133	uint64_t	ipsec_crypto;		/* Crypto processing failure */
134	uint64_t	ipsec_notdb;		/* No TDB was found */
135	uint64_t	ipsec_noxform;		/* Crypto error */
136	uint64_t	ipsec_exctdb;		/* TDBs with hardlimit excess */
137};
138
139struct tdb_data {
140	uint64_t	tdd_ipackets;		/* Input IPsec packets */
141	uint64_t	tdd_opackets;		/* Output IPsec packets */
142	uint64_t	tdd_ibytes;		/* Input bytes */
143	uint64_t	tdd_obytes;		/* Output bytes */
144	uint64_t	tdd_idrops;		/* Dropped on input */
145	uint64_t	tdd_odrops;		/* Dropped on output */
146	uint64_t	tdd_idecompbytes;	/* Input bytes, decompressed */
147	uint64_t	tdd_ouncompbytes;	/* Output bytes, uncompressed */
148};
149
150#ifdef _KERNEL
151
152#include <sys/timeout.h>
153#include <sys/tree.h>
154#include <sys/queue.h>
155#include <net/radix.h>
156#include <sys/percpu.h>
157
158enum ipsec_counters {
159	ipsec_tunnels,
160	ipsec_prevtunnels,
161	ipsec_ipackets,
162	ipsec_opackets,
163	ipsec_ibytes,
164	ipsec_obytes,
165	ipsec_idecompbytes,
166	ipsec_ouncompbytes,
167	ipsec_idrops,
168	ipsec_odrops,
169	ipsec_crypto,
170	ipsec_notdb,
171	ipsec_noxform,
172	ipsec_exctdb,
173	ipsec_ncounters
174};
175
176extern struct cpumem *ipseccounters;
177
178static inline void
179ipsecstat_inc(enum ipsec_counters c)
180{
181	counters_inc(ipseccounters, c);
182}
183
184static inline void
185ipsecstat_dec(enum ipsec_counters c)
186{
187	counters_dec(ipseccounters, c);
188}
189
190static inline void
191ipsecstat_add(enum ipsec_counters c, uint64_t v)
192{
193	counters_add(ipseccounters, c, v);
194}
195
196static inline void
197ipsecstat_pkt(enum ipsec_counters p, enum ipsec_counters b, uint64_t v)
198{
199	counters_pkt(ipseccounters, p, b, v);
200}
201
202struct m_tag;
203
204#define	sen_data		Sen.Data
205#define	sen_ip_src		Sen.Sip4.Src
206#define	sen_ip_dst		Sen.Sip4.Dst
207#define	sen_proto		Sen.Sip4.Proto
208#define	sen_sport		Sen.Sip4.Sport
209#define	sen_dport		Sen.Sip4.Dport
210#define	sen_direction		Sen.Sip4.Direction
211#define	sen_ip6_src		Sen.Sip6.Src
212#define	sen_ip6_dst		Sen.Sip6.Dst
213#define	sen_ip6_proto		Sen.Sip6.Proto
214#define	sen_ip6_sport		Sen.Sip6.Sport
215#define	sen_ip6_dport		Sen.Sip6.Dport
216#define	sen_ip6_direction	Sen.Sip6.Direction
217
218/*
219 * The "type" is really part of the address as far as the routing
220 * system is concerned. By using only one bit in the type field
221 * for each type, we sort-of make sure that different types of
222 * encapsulation addresses won't be matched against the wrong type.
223 *
224 */
225
226#define	SENT_IP4	0x0001		/* data is two struct in_addr */
227#define	SENT_IP6	0x0002
228
229#define	SENT_LEN	sizeof(struct sockaddr_encap)
230
231struct ipsec_id {
232	u_int16_t	type;		/* Subtype of data */
233	int16_t		len;		/* Length of data following */
234};
235
236struct ipsec_ids {
237	LIST_ENTRY(ipsec_ids)	id_gc_list;
238	RBT_ENTRY(ipsec_ids)	id_node_id;
239	RBT_ENTRY(ipsec_ids)	id_node_flow;
240	struct ipsec_id		*id_local;
241	struct ipsec_id		*id_remote;
242	u_int32_t		id_flow;
243	int			id_refcount;
244	u_int			id_gc_ttl;
245};
246RBT_HEAD(ipsec_ids_flows, ipsec_ids);
247RBT_HEAD(ipsec_ids_tree, ipsec_ids);
248
249struct ipsec_acquire {
250	union sockaddr_union		ipa_addr;
251	u_int32_t			ipa_seq;
252	struct sockaddr_encap		ipa_info;
253	struct sockaddr_encap		ipa_mask;
254	struct timeout			ipa_timeout;
255	struct ipsec_policy		*ipa_policy;
256	TAILQ_ENTRY(ipsec_acquire)	ipa_ipo_next;
257	TAILQ_ENTRY(ipsec_acquire)	ipa_next;
258};
259
260/*
261 * Locks used to protect struct members in this file:
262 *	p	ipo_tdb_mtx		link policy to TDB global mutex
263 */
264struct ipsec_policy {
265	struct radix_node	ipo_nodes[2];	/* radix tree glue */
266	struct sockaddr_encap	ipo_addr;
267	struct sockaddr_encap	ipo_mask;
268
269	union sockaddr_union	ipo_src;	/* Local address to use */
270	union sockaddr_union	ipo_dst;	/* Remote gateway -- if it's zeroed:
271						 * - on output, we try to
272						 * contact the remote host
273						 * directly (if needed).
274						 * - on input, we accept on if
275						 * the inner source is the
276						 * same as the outer source
277						 * address, or if transport
278						 * mode was used.
279						 */
280
281	u_int64_t	ipo_last_searched;	/* [p] Timestamp of lookup */
282
283	u_int8_t		ipo_flags;	/* See IPSP_POLICY_* definitions */
284	u_int8_t		ipo_type;	/* USE/ACQUIRE/... */
285	u_int8_t		ipo_sproto;	/* ESP/AH; if zero, use system dflts */
286	u_int			ipo_rdomain;
287
288	int                     ipo_ref_count;
289
290	struct tdb		*ipo_tdb;	/* [p] Cached TDB entry */
291
292	struct ipsec_ids	*ipo_ids;
293
294	TAILQ_HEAD(ipo_acquires_head, ipsec_acquire) ipo_acquires; /* List of acquires */
295	TAILQ_ENTRY(ipsec_policy)	ipo_tdb_next;	/* List TDB policies */
296	TAILQ_ENTRY(ipsec_policy)	ipo_list;	/* List of all policies */
297};
298
299#define	IPSP_POLICY_NONE	0x0000	/* No flags set */
300#define	IPSP_POLICY_STATIC	0x0002	/* Static policy */
301
302#define	IPSP_IPSEC_USE		0	/* Use if existing, don't acquire */
303#define	IPSP_IPSEC_ACQUIRE	1	/* Try acquire, let packet through */
304#define	IPSP_IPSEC_REQUIRE	2	/* Require SA */
305#define	IPSP_PERMIT		3	/* Permit traffic through */
306#define	IPSP_DENY		4	/* Deny traffic */
307#define	IPSP_IPSEC_DONTACQ	5	/* Require, but don't acquire */
308
309/* Identity types */
310#define	IPSP_IDENTITY_NONE		0
311#define	IPSP_IDENTITY_PREFIX		1
312#define	IPSP_IDENTITY_FQDN		2
313#define	IPSP_IDENTITY_USERFQDN		3
314#define	IPSP_IDENTITY_ASN1_DN		4
315
316/*
317 * Locks used to protect struct members in this file:
318 *	I	immutable after creation
319 *	N	net lock
320 *	m	tdb_mtx
321 *	p	ipo_tdb_mtx		link policy to TDB global mutex
322 *	s	tdb_sadb_mtx		SA database global mutex
323 */
324struct tdb {				/* tunnel descriptor block */
325	/*
326	 * Each TDB is on three hash tables: one keyed on dst/spi/sproto,
327	 * one keyed on dst/sproto, and one keyed on src/sproto. The first
328	 * is used for finding a specific TDB, the second for finding TDBs
329	 * for outgoing policy matching, and the third for incoming
330	 * policy matching. The following three fields maintain the hash
331	 * queues in those three tables.
332	 */
333	struct tdb	*tdb_hnext;	/* [s] dst/spi/sproto table */
334	struct tdb	*tdb_dnext;	/* [s] dst/sproto table */
335	struct tdb	*tdb_snext;	/* [s] src/sproto table */
336	struct tdb	*tdb_inext;
337	struct tdb	*tdb_onext;
338	SIMPLEQ_ENTRY(tdb) tdb_walk;	/* [N] temp list for tdb walker */
339
340	struct refcnt	tdb_refcnt;
341	struct mutex	tdb_mtx;
342
343	const struct xformsw	*tdb_xform;		/* Transform to use */
344	const struct enc_xform	*tdb_encalgxform;	/* Enc algorithm */
345	const struct auth_hash	*tdb_authalgxform;	/* Auth algorithm */
346	const struct comp_algo	*tdb_compalgxform;	/* Compression algo */
347
348#define	TDBF_UNIQUE		0x00001	/* This should not be used by others */
349#define	TDBF_TIMER		0x00002	/* Absolute expiration timer in use */
350#define	TDBF_BYTES		0x00004	/* Check the byte counters */
351#define	TDBF_ALLOCATIONS	0x00008	/* Check the flows counters */
352#define	TDBF_INVALID		0x00010	/* This SPI is not valid yet/anymore */
353#define	TDBF_FIRSTUSE		0x00020	/* Expire after first use */
354#define	TDBF_DELETED		0x00040	/* This TDB has already been deleted */
355#define	TDBF_SOFT_TIMER		0x00080	/* Soft expiration */
356#define	TDBF_SOFT_BYTES		0x00100	/* Soft expiration */
357#define	TDBF_SOFT_ALLOCATIONS	0x00200	/* Soft expiration */
358#define	TDBF_SOFT_FIRSTUSE	0x00400	/* Soft expiration */
359#define	TDBF_PFS		0x00800	/* Ask for PFS from Key Mgmt. */
360#define	TDBF_TUNNELING		0x01000	/* Force IP-IP encapsulation */
361#define	TDBF_USEDTUNNEL		0x10000	/* Appended a tunnel header in past */
362#define	TDBF_UDPENCAP		0x20000	/* UDP encapsulation */
363#define	TDBF_PFSYNC		0x40000	/* TDB will be synced */
364#define	TDBF_PFSYNC_RPL		0x80000	/* Replay counter should be bumped */
365#define	TDBF_ESN		0x100000 /* 64-bit sequence numbers (ESN) */
366
367#define TDBF_BITS ("\20" \
368	"\1UNIQUE\2TIMER\3BYTES\4ALLOCATIONS" \
369	"\5INVALID\6FIRSTUSE\7DELETED\10SOFT_TIMER" \
370	"\11SOFT_BYTES\12SOFT_ALLOCATIONS\13SOFT_FIRSTUSE\14PFS" \
371	"\15TUNNELING" \
372	"\21USEDTUNNEL\22UDPENCAP\23PFSYNC\24PFSYNC_RPL" \
373	"\25ESN")
374
375	u_int32_t	tdb_flags;	/* [m] Flags related to this TDB */
376
377	struct timeout	tdb_timer_tmo;
378	struct timeout	tdb_first_tmo;
379	struct timeout	tdb_stimer_tmo;
380	struct timeout	tdb_sfirst_tmo;
381
382	u_int32_t	tdb_seq;		/* Tracking number for PFKEY */
383	u_int32_t	tdb_exp_allocations;	/* Expire after so many flows */
384	u_int32_t	tdb_soft_allocations;	/* Expiration warning */
385	u_int32_t	tdb_cur_allocations;	/* Total number of allocs */
386
387	u_int64_t	tdb_exp_bytes;	/* Expire after so many bytes passed */
388	u_int64_t	tdb_soft_bytes;	/* Expiration warning */
389	u_int64_t	tdb_cur_bytes;	/* Current count of bytes */
390
391	u_int64_t	tdb_exp_timeout;	/* When does the SPI expire */
392	u_int64_t	tdb_soft_timeout;	/* Send soft-expire warning */
393	u_int64_t	tdb_established;	/* When was SPI established */
394
395	u_int64_t	tdb_first_use;		/* When was it first used */
396	u_int64_t	tdb_soft_first_use;	/* Soft warning */
397	u_int64_t	tdb_exp_first_use;	/* Expire if tdb_first_use +
398						 * tdb_exp_first_use <= curtime
399						 */
400
401	u_int64_t	tdb_last_used;	/* When was this SA last used */
402	u_int64_t	tdb_last_marked;/* Last SKIPCRYPTO status change */
403
404	struct tdb_data	tdb_data;	/* stats about this TDB */
405	u_int64_t	tdb_cryptoid;	/* Crypto session ID */
406
407	u_int32_t	tdb_spi;	/* [I] SPI */
408	u_int16_t	tdb_amxkeylen;	/* Raw authentication key length */
409	u_int16_t	tdb_emxkeylen;	/* Raw encryption key length */
410	u_int16_t	tdb_ivlen;	/* IV length */
411	u_int8_t	tdb_sproto;	/* [I] IPsec protocol */
412	u_int8_t	tdb_wnd;	/* Replay window */
413	u_int8_t	tdb_satype;	/* SA type (RFC2367, PF_KEY) */
414	u_int8_t	tdb_updates;	/* pfsync update counter */
415
416	union sockaddr_union	tdb_dst;	/* [N] Destination address */
417	union sockaddr_union	tdb_src;	/* [N] Source address */
418
419	u_int8_t	*tdb_amxkey;	/* Raw authentication key */
420	u_int8_t	*tdb_emxkey;	/* Raw encryption key */
421
422#define TDB_REPLAYWASTE	32
423#define TDB_REPLAYMAX	(2100+TDB_REPLAYWASTE)
424
425	u_int64_t	tdb_rpl;	/* Replay counter */
426	u_int32_t	tdb_seen[howmany(TDB_REPLAYMAX, 32)]; /* Anti-replay window */
427
428	u_int8_t	tdb_iv[4];	/* Used for HALF-IV ESP */
429
430	struct ipsec_ids	*tdb_ids;	/* Src/Dst ID for this SA */
431	int		tdb_ids_swapped;	/* XXX */
432
433	u_int32_t	tdb_mtu;	/* MTU at this point in the chain */
434	u_int64_t	tdb_mtutimeout;	/* When to ignore this entry */
435
436	u_int16_t	tdb_udpencap_port;	/* Peer UDP port */
437
438	u_int16_t	tdb_tag;		/* Packet filter tag */
439	u_int32_t	tdb_tap;		/* Alternate enc(4) interface */
440
441	u_int		tdb_rdomain;		/* [I] Routing domain */
442	u_int		tdb_rdomain_post;	/* [I] Change domain */
443
444	struct sockaddr_encap   tdb_filter; /* What traffic is acceptable */
445	struct sockaddr_encap   tdb_filtermask; /* And the mask */
446
447	TAILQ_HEAD(tdb_policy_head, ipsec_policy) tdb_policy_head; /* [p] */
448	TAILQ_ENTRY(tdb)	tdb_sync_entry;
449};
450#define tdb_ipackets		tdb_data.tdd_ipackets
451#define tdb_opackets		tdb_data.tdd_opackets
452#define tdb_ibytes		tdb_data.tdd_ibytes
453#define tdb_obytes		tdb_data.tdd_obytes
454#define tdb_idrops		tdb_data.tdd_idrops
455#define tdb_odrops		tdb_data.tdd_odrops
456#define tdb_idecompbytes	tdb_data.tdd_idecompbytes
457#define tdb_ouncompbytes	tdb_data.tdd_ouncompbytes
458
459
460struct tdb_ident {
461	u_int32_t spi;
462	union sockaddr_union dst;
463	u_int8_t proto;
464	u_int rdomain;
465};
466
467struct tdb_crypto {
468	union sockaddr_union	tc_dst;
469	u_int64_t		tc_rpl;
470	u_int32_t		tc_spi;
471	int			tc_protoff;
472	int			tc_skip;
473	u_int			tc_rdomain;
474	u_int8_t		tc_proto;
475};
476
477struct ipsecinit {
478	u_int8_t	*ii_enckey;
479	u_int8_t	*ii_authkey;
480	u_int16_t	ii_enckeylen;
481	u_int16_t	ii_authkeylen;
482	u_int8_t	ii_encalg;
483	u_int8_t	ii_authalg;
484	u_int8_t	ii_compalg;
485};
486
487/* xform IDs */
488#define	XF_IP4		1	/* IP inside IP */
489#define	XF_AH		2	/* AH */
490#define	XF_ESP		3	/* ESP */
491#define	XF_TCPSIGNATURE	5	/* TCP MD5 Signature option, RFC 2358 */
492#define	XF_IPCOMP	6	/* IPCOMP */
493
494/* xform attributes */
495#define	XFT_AUTH	0x0001
496#define	XFT_CONF	0x0100
497#define	XFT_COMP	0x1000
498
499#define	IPSEC_ZEROES_SIZE	256	/* Larger than an IP6 extension hdr. */
500
501struct xformsw {
502	u_short	xf_type;		/* Unique ID of xform */
503	u_short	xf_flags;		/* flags (see below) */
504	char	*xf_name;		/* human-readable name */
505	int	(*xf_attach)(void);	/* called at config time */
506	int	(*xf_init)(struct tdb *, const struct xformsw *,
507		    struct ipsecinit *);
508	int	(*xf_zeroize)(struct tdb *); /* termination */
509	int	(*xf_input)(struct mbuf **, struct tdb *, int, int);
510	int	(*xf_output)(struct mbuf *, struct tdb *, int, int);
511};
512
513extern int ipsec_in_use;
514extern u_int64_t ipsec_last_added;
515extern int encdebug;			/* enable message reporting */
516extern struct pool tdb_pool;
517
518extern int ipsec_keep_invalid;		/* lifetime of embryonic SAs (in sec) */
519extern int ipsec_require_pfs;		/* use Perfect Forward Secrecy */
520extern int ipsec_expire_acquire;	/* wait for security assoc. (in sec) */
521extern int ipsec_soft_allocations;	/* flows/SA before renegotiation */
522extern int ipsec_exp_allocations;	/* num. of flows/SA before it expires */
523extern int ipsec_soft_bytes;		/* bytes/SA before renegotiation */
524extern int ipsec_exp_bytes;		/* num of bytes/SA before it expires */
525extern int ipsec_soft_timeout;		/* seconds/SA before renegotiation */
526extern int ipsec_exp_timeout;		/* seconds/SA before it expires */
527extern int ipsec_soft_first_use;	/* seconds between 1st asso & renego */
528extern int ipsec_exp_first_use;		/* seconds between 1st asso & expire */
529
530/*
531 * Names for IPsec sysctl objects
532 */
533#define	IPSEC_ENCDEBUG			IPCTL_ENCDEBUG			/* 12 */
534#define	IPSEC_STATS			IPCTL_IPSEC_STATS		/* 13 */
535#define IPSEC_EXPIRE_ACQUIRE		IPCTL_IPSEC_EXPIRE_ACQUIRE	/* 14 */
536#define IPSEC_EMBRYONIC_SA_TIMEOUT	IPCTL_IPSEC_EMBRYONIC_SA_TIMEOUT/* 15 */
537#define IPSEC_REQUIRE_PFS		IPCTL_IPSEC_REQUIRE_PFS		/* 16 */
538#define IPSEC_SOFT_ALLOCATIONS          IPCTL_IPSEC_SOFT_ALLOCATIONS	/* 17 */
539#define IPSEC_ALLOCATIONS		IPCTL_IPSEC_ALLOCATIONS		/* 18 */
540#define IPSEC_SOFT_BYTES		IPCTL_IPSEC_SOFT_BYTES		/* 19 */
541#define IPSEC_BYTES			IPCTL_IPSEC_BYTES		/* 20 */
542#define IPSEC_TIMEOUT			IPCTL_IPSEC_TIMEOUT		/* 21 */
543#define IPSEC_SOFT_TIMEOUT		IPCTL_IPSEC_SOFT_TIMEOUT	/* 22 */
544#define IPSEC_SOFT_FIRSTUSE		IPCTL_IPSEC_SOFT_FIRSTUSE	/* 23 */
545#define IPSEC_FIRSTUSE			IPCTL_IPSEC_FIRSTUSE		/* 24 */
546#define IPSEC_MAXID	25
547
548extern char ipsec_def_enc[];
549extern char ipsec_def_auth[];
550extern char ipsec_def_comp[];
551
552extern TAILQ_HEAD(ipsec_policy_head, ipsec_policy) ipsec_policy_head;
553
554extern struct mutex tdb_sadb_mtx;
555extern struct mutex ipo_tdb_mtx;
556
557struct cryptop;
558
559/* Misc. */
560const char *ipsp_address(union sockaddr_union *, char *, socklen_t);
561
562/* SPD tables */
563struct radix_node_head *spd_table_add(unsigned int);
564struct radix_node_head *spd_table_get(unsigned int);
565int spd_table_walk(unsigned int,
566    int (*walker)(struct ipsec_policy *, void *, unsigned int), void *);
567
568/* TDB management routines */
569uint32_t reserve_spi(u_int, u_int32_t, u_int32_t, union sockaddr_union *,
570		union sockaddr_union *, u_int8_t, int *);
571struct	tdb *gettdb_dir(u_int, u_int32_t, union sockaddr_union *, u_int8_t, int);
572#define gettdb(a,b,c,d)		gettdb_dir((a),(b),(c),(d),0)
573#define gettdb_rev(a,b,c,d)	gettdb_dir((a),(b),(c),(d),1)
574struct	tdb *gettdbbydst(u_int, union sockaddr_union *, u_int8_t,
575		struct ipsec_ids *,
576		struct sockaddr_encap *, struct sockaddr_encap *);
577struct	tdb *gettdbbysrc(u_int, union sockaddr_union *, u_int8_t,
578		struct ipsec_ids *,
579		struct sockaddr_encap *, struct sockaddr_encap *);
580struct	tdb *gettdbbysrcdst_dir(u_int, u_int32_t, union sockaddr_union *,
581		union sockaddr_union *, u_int8_t, int);
582#define gettdbbysrcdst(a,b,c,d,e) gettdbbysrcdst_dir((a),(b),(c),(d),(e),0)
583#define gettdbbysrcdst_rev(a,b,c,d,e) gettdbbysrcdst_dir((a),(b),(c),(d),(e),1)
584void	puttdb(struct tdb *);
585void	puttdb_locked(struct tdb *);
586void	tdb_delete(struct tdb *);
587struct	tdb *tdb_alloc(u_int);
588struct	tdb *tdb_ref(struct tdb *);
589void	tdb_unref(struct tdb *);
590void	tdb_free(struct tdb *);
591int	tdb_init(struct tdb *, u_int16_t, struct ipsecinit *);
592void	tdb_unlink(struct tdb *);
593void	tdb_unlink_locked(struct tdb *);
594void	tdb_cleanspd(struct tdb *);
595void	tdb_unbundle(struct tdb *);
596void	tdb_deltimeouts(struct tdb *);
597int	tdb_walk(u_int, int (*)(struct tdb *, void *, int), void *);
598void	tdb_printit(void *, int, int (*)(const char *, ...));
599
600/* XF_IP4 */
601int	ipe4_attach(void);
602int	ipe4_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
603int	ipe4_zeroize(struct tdb *);
604int	ipe4_input(struct mbuf **, struct tdb *, int, int);
605
606/* XF_AH */
607int	ah_attach(void);
608int	ah_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
609int	ah_zeroize(struct tdb *);
610int	ah_input(struct mbuf **, struct tdb *, int, int);
611int	ah_output(struct mbuf *, struct tdb *, int, int);
612int	ah_sysctl(int *, u_int, void *, size_t *, void *, size_t);
613
614int	ah46_input(struct mbuf **, int *, int, int);
615void	ah4_ctlinput(int, struct sockaddr *, u_int, void *);
616void	udpencap_ctlinput(int, struct sockaddr *, u_int, void *);
617
618/* XF_ESP */
619int	esp_attach(void);
620int	esp_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
621int	esp_zeroize(struct tdb *);
622int	esp_input(struct mbuf **, struct tdb *, int, int);
623int	esp_output(struct mbuf *, struct tdb *, int, int);
624int	esp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
625
626int	esp46_input(struct mbuf **, int *, int, int);
627void	esp4_ctlinput(int, struct sockaddr *, u_int, void *);
628
629/* XF_IPCOMP */
630int	ipcomp_attach(void);
631int	ipcomp_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
632int	ipcomp_zeroize(struct tdb *);
633int	ipcomp_input(struct mbuf **, struct tdb *, int, int);
634int	ipcomp_output(struct mbuf *, struct tdb *, int, int);
635int	ipcomp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
636int	ipcomp46_input(struct mbuf **, int *, int, int);
637
638/* XF_TCPSIGNATURE */
639int	tcp_signature_tdb_attach(void);
640int	tcp_signature_tdb_init(struct tdb *, const struct xformsw *,
641	    struct ipsecinit *);
642int	tcp_signature_tdb_zeroize(struct tdb *);
643int	tcp_signature_tdb_input(struct mbuf **, struct tdb *, int, int);
644int	tcp_signature_tdb_output(struct mbuf *, struct tdb *, int, int);
645
646/* Replay window */
647int	checkreplaywindow(struct tdb *, u_int64_t, u_int32_t, u_int32_t *, int);
648
649/* Packet processing */
650int	ipsp_process_packet(struct mbuf *, struct tdb *, int, int);
651int	ipsp_process_done(struct mbuf *, struct tdb *);
652int	ipsp_spd_lookup(struct mbuf *, int, int, int, struct tdb *,
653	    struct inpcb *, struct tdb **, u_int32_t);
654int	ipsp_is_unspecified(union sockaddr_union);
655int	ipsp_aux_match(struct tdb *, struct ipsec_ids *,
656	    struct sockaddr_encap *, struct sockaddr_encap *);
657int	ipsp_ids_match(struct ipsec_ids *, struct ipsec_ids *);
658struct ipsec_ids *ipsp_ids_insert(struct ipsec_ids *);
659struct ipsec_ids *ipsp_ids_lookup(u_int32_t);
660void	ipsp_ids_free(struct ipsec_ids *);
661
662void	ipsp_init(void);
663void	ipsec_init(void);
664int	ipsec_sysctl(int *, u_int, void *, size_t *, void *, size_t);
665int	ipsec_common_input(struct mbuf **, int, int, int, int, int);
666int	ipsec_common_input_cb(struct mbuf **, struct tdb *, int, int);
667int	ipsec_input_disabled(struct mbuf **, int *, int, int);
668int	ipsec_protoff(struct mbuf *, int, int);
669int	ipsec_delete_policy(struct ipsec_policy *);
670ssize_t	ipsec_hdrsz(struct tdb *);
671void	ipsec_adjust_mtu(struct mbuf *, u_int32_t);
672void	ipsec_set_mtu(struct tdb *, u_int32_t);
673struct	ipsec_acquire *ipsec_get_acquire(u_int32_t);
674int	ipsec_forward_check(struct mbuf *, int, int);
675int	ipsec_local_check(struct mbuf *, int, int, int);
676
677#endif /* _KERNEL */
678#endif /* _NETINET_IPSP_H_ */
679