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