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