1191762Simp/*	$OpenBSD: ip_ipsp.h,v 1.245 2024/04/17 20:48:51 bluhm Exp $	*/
2191762Simp/*
3191762Simp * The authors of this code are John Ioannidis (ji@tla.org),
4191762Simp * Angelos D. Keromytis (kermit@csd.uch.gr),
5191762Simp * Niels Provos (provos@physnet.uni-hamburg.de) and
6191762Simp * Niklas Hallqvist (niklas@appli.se).
7191762Simp *
8191762Simp * The original version of this code was written by John Ioannidis
9191762Simp * for BSD/OS in Athens, Greece, in November 1995.
10191762Simp *
11191762Simp * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12191762Simp * by Angelos D. Keromytis.
13191762Simp *
14191762Simp * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15191762Simp * and Niels Provos.
16191762Simp *
17191762Simp * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
18191762Simp *
19191762Simp * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20191762Simp * Angelos D. Keromytis and Niels Provos.
21191762Simp * Copyright (c) 1999 Niklas Hallqvist.
22191762Simp * Copyright (c) 2001, Angelos D. Keromytis.
23191762Simp *
24191762Simp * Permission to use, copy, and modify this software with or without fee
25191762Simp * is hereby granted, provided that this entire notice is included in
26191762Simp * all copies of any software which is or includes a copy or
27191762Simp * modification of this software.
28191762Simp * You may use this code under the GNU public license if you so wish. Please
29191762Simp * contribute changes back to the authors under this freer than GPL license
30191762Simp * so that we may further the use of strong encryption without limitations to
31191762Simp * all.
32191762Simp *
33191762Simp * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34191762Simp * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35191762Simp * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36191762Simp * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37191762Simp * PURPOSE.
38191762Simp */
39191762Simp
40191762Simp#ifndef _NETINET_IPSP_H_
41191762Simp#define _NETINET_IPSP_H_
42191762Simp
43191762Simp/*
44191762Simp * Locks used to protect struct members in this file:
45191762Simp *	I	immutable after creation
46191762Simp *	a	atomic operations
47191762Simp *	N	net lock
48191762Simp *	A	ipsec_acquire_mtx
49191762Simp *	F	ipsec_flows_mtx
50191762Simp *	P	ipo_tdb_mtx		link policy to TDB global mutex
51191762Simp *	D	tdb_sadb_mtx		SA database global mutex
52191762Simp *	m	tdb_mtx			fields of struct tdb
53191762Simp *	S	pfsync			fields of struct tdb
54191762Simp */
55191762Simp
56191762Simp/* IPSP global definitions. */
57191762Simp
58191762Simp#include <sys/types.h>
59191762Simp#include <netinet/in.h>
60191762Simp
61191762Simpunion sockaddr_union {
62191762Simp	struct sockaddr		sa;
63191762Simp	struct sockaddr_in	sin;
64191762Simp	struct sockaddr_in6	sin6;
65191762Simp};
66191762Simp
67191762Simp#define	AH_HMAC_MAX_HASHLEN	32	/* 256 bits of authenticator for SHA512 */
68191762Simp#define	AH_HMAC_RPLENGTH	4	/* 32 bits of replay counter */
69191762Simp#define	AH_HMAC_INITIAL_RPL	1	/* Replay counter initial value */
70191762Simp
71191762Simp/* Authenticator lengths */
72191762Simp#define	AH_MD5_ALEN		16
73191762Simp#define	AH_SHA1_ALEN		20
74191762Simp#define	AH_RMD160_ALEN		20
75191762Simp#define	AH_SHA2_256_ALEN	32
76191762Simp#define	AH_SHA2_384_ALEN	48
77191762Simp#define	AH_SHA2_512_ALEN	64
78191762Simp#define	AH_ALEN_MAX		64	/* Keep updated */
79191762Simp
80191762Simp/* Reserved SPI numbers */
81191762Simp#define	SPI_LOCAL_USE		0
82191762Simp#define	SPI_RESERVED_MIN	1
83191762Simp#define	SPI_RESERVED_MAX	255
84191762Simp
85191762Simp/* Reserved CPI numbers */
86191762Simp#define CPI_RESERVED_MIN	1
87191762Simp#define CPI_RESERVED_MAX	255
88191762Simp#define CPI_PRIVATE_MIN		61440
89191762Simp#define CPI_PRIVATE_MAX		65535
90191762Simp
91191762Simp/* sysctl default values */
92191762Simp#define	IPSEC_DEFAULT_EMBRYONIC_SA_TIMEOUT	60	/* 1 minute */
93191762Simp#define	IPSEC_DEFAULT_PFS			1
94191762Simp#define	IPSEC_DEFAULT_SOFT_ALLOCATIONS		0
95191762Simp#define	IPSEC_DEFAULT_EXP_ALLOCATIONS		0
96191762Simp#define	IPSEC_DEFAULT_SOFT_BYTES		0
97191762Simp#define	IPSEC_DEFAULT_EXP_BYTES			0
98191762Simp#define	IPSEC_DEFAULT_SOFT_TIMEOUT		80000
99191762Simp#define	IPSEC_DEFAULT_EXP_TIMEOUT		86400
100191762Simp#define	IPSEC_DEFAULT_SOFT_FIRST_USE		3600
101191762Simp#define	IPSEC_DEFAULT_EXP_FIRST_USE		7200
102191762Simp#define	IPSEC_DEFAULT_DEF_ENC			"aes"
103191762Simp#define	IPSEC_DEFAULT_DEF_AUTH			"hmac-sha1"
104191762Simp#define	IPSEC_DEFAULT_EXPIRE_ACQUIRE		30
105191762Simp#define	IPSEC_DEFAULT_DEF_COMP			"deflate"
106191762Simp
107191762Simpstruct sockaddr_encap {
108191762Simp	u_int8_t	sen_len;		/* length */
109191762Simp	u_int8_t	sen_family;		/* PF_KEY */
110191762Simp	u_int16_t	sen_type;		/* see SENT_* */
111191762Simp	union {
112191762Simp		struct {				/* SENT_IP4 */
113191762Simp			u_int8_t	Direction;
114191762Simp			struct in_addr	Src;
115191762Simp			struct in_addr	Dst;
116191762Simp			u_int8_t	Proto;
117191762Simp			u_int16_t	Sport;
118191762Simp			u_int16_t	Dport;
119191762Simp		} Sip4;
120191762Simp
121191762Simp		struct {				/* SENT_IP6 */
122191762Simp			u_int8_t	Direction;
123191762Simp			struct in6_addr	Src;
124191762Simp			struct in6_addr	Dst;
125191762Simp			u_int8_t	Proto;
126191762Simp			u_int16_t	Sport;
127191762Simp			u_int16_t	Dport;
128191762Simp		} Sip6;
129191762Simp	} Sen;
130191762Simp};
131191762Simp
132191762Simp#define	IPSP_DIRECTION_IN	0x1
133191762Simp#define	IPSP_DIRECTION_OUT	0x2
134191762Simp
135191762Simpstruct ipsecstat {
136191762Simp	uint64_t	ipsec_tunnels;		/* Number of active tunnels */
137191762Simp	uint64_t	ipsec_prevtunnels;	/* Past number of tunnels */
138191762Simp	uint64_t	ipsec_ipackets;		/* Input IPsec packets */
139191762Simp	uint64_t	ipsec_opackets;		/* Output IPsec packets */
140191762Simp	uint64_t	ipsec_ibytes;		/* Input bytes */
141191762Simp	uint64_t	ipsec_obytes;		/* Output bytes */
142191762Simp	uint64_t	ipsec_idecompbytes;	/* Input bytes, decompressed */
143191762Simp	uint64_t	ipsec_ouncompbytes;	/* Output bytes, uncompressed */
144191762Simp	uint64_t	ipsec_idrops;		/* Dropped on input */
145191762Simp	uint64_t	ipsec_odrops;		/* Dropped on output */
146191762Simp	uint64_t	ipsec_crypto;		/* Crypto processing failure */
147191762Simp	uint64_t	ipsec_notdb;		/* No TDB was found */
148191762Simp	uint64_t	ipsec_noxform;		/* Crypto error */
149191762Simp	uint64_t	ipsec_exctdb;		/* TDBs with hardlimit excess */
150191762Simp};
151191762Simp
152191762Simpstruct ipsec_level {
153191762Simp	u_char	sl_auth;	/* Authentication level */
154191762Simp	u_char	sl_esp_trans;	/* ESP transport level */
155191762Simp	u_char	sl_esp_network;	/* ESP network (encapsulation) level */
156191762Simp	u_char	sl_ipcomp;	/* Compression level */
157191762Simp};
158191762Simp
159191762Simp#ifdef _KERNEL
160191762Simp
161191762Simp#include <sys/timeout.h>
162191762Simp#include <sys/tree.h>
163191762Simp#include <sys/queue.h>
164191762Simp#include <net/radix.h>
165191762Simp#include <sys/percpu.h>
166191762Simp
167191762Simpenum ipsec_counters {
168191762Simp	ipsec_tunnels,
169191762Simp	ipsec_prevtunnels,
170191762Simp	ipsec_ipackets,
171191762Simp	ipsec_opackets,
172191762Simp	ipsec_ibytes,
173191762Simp	ipsec_obytes,
174191762Simp	ipsec_idecompbytes,
175191762Simp	ipsec_ouncompbytes,
176191762Simp	ipsec_idrops,
177191762Simp	ipsec_odrops,
178191762Simp	ipsec_crypto,
179191762Simp	ipsec_notdb,
180191762Simp	ipsec_noxform,
181191762Simp	ipsec_exctdb,
182191762Simp	ipsec_ncounters
183191762Simp};
184191762Simp
185191762Simpextern struct cpumem *ipseccounters;
186191762Simp
187191762Simpstatic inline void
188191762Simpipsecstat_inc(enum ipsec_counters c)
189191762Simp{
190191762Simp	counters_inc(ipseccounters, c);
191191762Simp}
192191762Simp
193191762Simpstatic inline void
194191762Simpipsecstat_dec(enum ipsec_counters c)
195191762Simp{
196191762Simp	counters_dec(ipseccounters, c);
197191762Simp}
198191762Simp
199191762Simpstatic inline void
200191762Simpipsecstat_add(enum ipsec_counters c, uint64_t v)
201191762Simp{
202191762Simp	counters_add(ipseccounters, c, v);
203191762Simp}
204191762Simp
205191762Simpstatic inline void
206191762Simpipsecstat_pkt(enum ipsec_counters p, enum ipsec_counters b, uint64_t v)
207191762Simp{
208191762Simp	counters_pkt(ipseccounters, p, b, v);
209191762Simp}
210191762Simp
211191762Simpstruct m_tag;
212191762Simp
213191762Simp#define	sen_data		Sen.Data
214191762Simp#define	sen_ip_src		Sen.Sip4.Src
215191762Simp#define	sen_ip_dst		Sen.Sip4.Dst
216191762Simp#define	sen_proto		Sen.Sip4.Proto
217191762Simp#define	sen_sport		Sen.Sip4.Sport
218191762Simp#define	sen_dport		Sen.Sip4.Dport
219191762Simp#define	sen_direction		Sen.Sip4.Direction
220191762Simp#define	sen_ip6_src		Sen.Sip6.Src
221191762Simp#define	sen_ip6_dst		Sen.Sip6.Dst
222191762Simp#define	sen_ip6_proto		Sen.Sip6.Proto
223191762Simp#define	sen_ip6_sport		Sen.Sip6.Sport
224191762Simp#define	sen_ip6_dport		Sen.Sip6.Dport
225191762Simp#define	sen_ip6_direction	Sen.Sip6.Direction
226191762Simp
227191762Simp/*
228191762Simp * The "type" is really part of the address as far as the routing
229191762Simp * system is concerned. By using only one bit in the type field
230191762Simp * for each type, we sort-of make sure that different types of
231191762Simp * encapsulation addresses won't be matched against the wrong type.
232191762Simp *
233191762Simp */
234191762Simp
235191762Simp#define	SENT_IP4	0x0001		/* data is two struct in_addr */
236191762Simp#define	SENT_IP6	0x0002
237191762Simp
238191762Simp#define	SENT_LEN	sizeof(struct sockaddr_encap)
239191762Simp
240191762Simpstruct ipsec_id {
241191762Simp	u_int16_t	type;		/* Subtype of data */
242191762Simp	int16_t		len;		/* Length of data following */
243191762Simp};
244191762Simp
245191762Simpstruct ipsec_ids {
246191762Simp	LIST_ENTRY(ipsec_ids)	id_gc_list;	/* [F] */
247191762Simp	RBT_ENTRY(ipsec_ids)	id_node_id;	/* [F] */
248191762Simp	RBT_ENTRY(ipsec_ids)	id_node_flow;	/* [F] */
249191762Simp	struct ipsec_id		*id_local;	/* [I] */
250191762Simp	struct ipsec_id		*id_remote;	/* [I] */
251191762Simp	u_int32_t		id_flow;	/* [I] */
252191762Simp	u_int			id_refcount;	/* [F] */
253191762Simp	u_int			id_gc_ttl;	/* [F] */
254};
255RBT_HEAD(ipsec_ids_flows, ipsec_ids);
256RBT_HEAD(ipsec_ids_tree, ipsec_ids);
257
258struct ipsec_acquire {
259	union sockaddr_union		ipa_addr;
260	u_int32_t			ipa_seq;
261	struct sockaddr_encap		ipa_info;
262	struct sockaddr_encap		ipa_mask;
263	struct refcnt			ipa_refcnt;
264	struct timeout			ipa_timeout;
265	struct ipsec_policy		*ipa_policy;	/* [A] back pointer */
266	TAILQ_ENTRY(ipsec_acquire)	ipa_ipo_next;	/* [A] per policy */
267	TAILQ_ENTRY(ipsec_acquire)	ipa_next;	/* [A] global list */
268};
269
270TAILQ_HEAD(ipsec_acquire_head, ipsec_acquire);
271
272struct ipsec_policy {
273	struct radix_node	ipo_nodes[2];	/* radix tree glue */
274	struct sockaddr_encap	ipo_addr;
275	struct sockaddr_encap	ipo_mask;
276
277	union sockaddr_union	ipo_src;	/* Local address to use */
278	union sockaddr_union	ipo_dst;	/* Remote gateway -- if it's zeroed:
279						 * - on output, we try to
280						 * contact the remote host
281						 * directly (if needed).
282						 * - on input, we accept on if
283						 * the inner source is the
284						 * same as the outer source
285						 * address, or if transport
286						 * mode was used.
287						 */
288
289	u_int64_t	ipo_last_searched;	/* [P] Timestamp of lookup */
290
291	u_int8_t		ipo_flags;	/* See IPSP_POLICY_* definitions */
292	u_int8_t		ipo_type;	/* USE/ACQUIRE/... */
293	u_int8_t		ipo_sproto;	/* ESP/AH; if zero, use system dflts */
294	u_int			ipo_rdomain;
295
296	struct refcnt		ipo_refcnt;
297
298	struct tdb		*ipo_tdb;	/* [P] Cached TDB entry */
299
300	struct ipsec_ids	*ipo_ids;
301
302	struct ipsec_acquire_head ipo_acquires;	/* [A] List of acquires */
303	TAILQ_ENTRY(ipsec_policy) ipo_tdb_next;	/* [P] List TDB policies */
304	TAILQ_ENTRY(ipsec_policy) ipo_list;	/* List of all policies */
305};
306
307#define	IPSP_POLICY_NONE	0x0000	/* No flags set */
308#define	IPSP_POLICY_STATIC	0x0002	/* Static policy */
309
310#define	IPSP_IPSEC_USE		0	/* Use if existing, don't acquire */
311#define	IPSP_IPSEC_ACQUIRE	1	/* Try acquire, let packet through */
312#define	IPSP_IPSEC_REQUIRE	2	/* Require SA */
313#define	IPSP_PERMIT		3	/* Permit traffic through */
314#define	IPSP_DENY		4	/* Deny traffic */
315#define	IPSP_IPSEC_DONTACQ	5	/* Require, but don't acquire */
316
317/* Identity types */
318#define	IPSP_IDENTITY_NONE		0
319#define	IPSP_IDENTITY_PREFIX		1
320#define	IPSP_IDENTITY_FQDN		2
321#define	IPSP_IDENTITY_USERFQDN		3
322#define	IPSP_IDENTITY_ASN1_DN		4
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;	/* [D] dst/spi/sproto table */
334	struct tdb	*tdb_dnext;	/* [D] dst/sproto table */
335	struct tdb	*tdb_snext;	/* [D] 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#define	TDBF_PFSYNC_SNAPPED	0x200000 /* entry is being dispatched to peer */
367#define	TDBF_IFACE		0x400000 /* entry policy is via sec(4) */
368
369#define TDBF_BITS ("\20" \
370	"\1UNIQUE\2TIMER\3BYTES\4ALLOCATIONS" \
371	"\5INVALID\6FIRSTUSE\7DELETED\10SOFT_TIMER" \
372	"\11SOFT_BYTES\12SOFT_ALLOCATIONS\13SOFT_FIRSTUSE\14PFS" \
373	"\15TUNNELING" \
374	"\21USEDTUNNEL\22UDPENCAP\23PFSYNC\24PFSYNC_RPL" \
375	"\25ESN" "\26IFACE")
376
377	u_int32_t	tdb_flags;	/* [m] Flags related to this TDB */
378
379	struct timeout	tdb_timer_tmo;
380	struct timeout	tdb_first_tmo;
381	struct timeout	tdb_stimer_tmo;
382	struct timeout	tdb_sfirst_tmo;
383
384	u_int32_t	tdb_seq;		/* Tracking number for PFKEY */
385	u_int32_t	tdb_exp_allocations;	/* Expire after so many flows */
386	u_int32_t	tdb_soft_allocations;	/* Expiration warning */
387	u_int32_t	tdb_cur_allocations;	/* Total number of allocs */
388
389	u_int64_t	tdb_exp_bytes;	/* Expire after so many bytes passed */
390	u_int64_t	tdb_soft_bytes;	/* Expiration warning */
391	u_int64_t	tdb_cur_bytes;	/* Current count of bytes */
392
393	u_int64_t	tdb_exp_timeout;	/* When does the SPI expire */
394	u_int64_t	tdb_soft_timeout;	/* Send soft-expire warning */
395	u_int64_t	tdb_established;	/* When was SPI established */
396
397	u_int64_t	tdb_first_use;		/* When was it first used */
398	u_int64_t	tdb_soft_first_use;	/* Soft warning */
399	u_int64_t	tdb_exp_first_use;	/* Expire if tdb_first_use +
400						 * tdb_exp_first_use <= curtime
401						 */
402
403	u_int64_t	tdb_last_used;	/* When was this SA last used */
404	u_int64_t	tdb_last_marked;/* Last SKIPCRYPTO status change */
405
406	struct cpumem   *tdb_counters;  /* stats about this TDB */
407
408	u_int64_t	tdb_cryptoid;	/* Crypto session ID */
409
410	u_int32_t	tdb_spi;	/* [I] SPI */
411	u_int16_t	tdb_amxkeylen;	/* Raw authentication key length */
412	u_int16_t	tdb_emxkeylen;	/* Raw encryption key length */
413	u_int16_t	tdb_ivlen;	/* IV length */
414	u_int8_t	tdb_sproto;	/* [I] IPsec protocol */
415	u_int8_t	tdb_wnd;	/* Replay window */
416	u_int8_t	tdb_satype;	/* SA type (RFC2367, PF_KEY) */
417	u_int8_t	tdb_iface_dir;	/* [I] sec(4) iface direction */
418
419	union sockaddr_union	tdb_dst;	/* [N] Destination address */
420	union sockaddr_union	tdb_src;	/* [N] Source address */
421
422	u_int8_t	*tdb_amxkey;	/* Raw authentication key */
423	u_int8_t	*tdb_emxkey;	/* Raw encryption key */
424
425#define TDB_REPLAYWASTE	32
426#define TDB_REPLAYMAX	(2100+TDB_REPLAYWASTE)
427
428	u_int64_t	tdb_rpl;	/* Replay counter */
429	u_int32_t	tdb_seen[howmany(TDB_REPLAYMAX, 32)]; /* Anti-replay window */
430
431	u_int8_t	tdb_iv[4];	/* Used for HALF-IV ESP */
432
433	struct ipsec_ids	*tdb_ids;	/* Src/Dst ID for this SA */
434	int		tdb_ids_swapped;	/* XXX */
435
436	u_int32_t	tdb_mtu;	/* MTU at this point in the chain */
437	u_int64_t	tdb_mtutimeout;	/* When to ignore this entry */
438
439	u_int16_t	tdb_udpencap_port;	/* Peer UDP port */
440
441	u_int16_t	tdb_tag;		/* Packet filter tag */
442	u_int32_t	tdb_tap;		/* Alternate enc(4) interface */
443	unsigned int	tdb_iface;		/* [I] sec(4) iface */
444
445	u_int		tdb_rdomain;		/* [I] Routing domain */
446	u_int		tdb_rdomain_post;	/* [I] Change domain */
447
448	struct sockaddr_encap   tdb_filter; /* What traffic is acceptable */
449	struct sockaddr_encap   tdb_filtermask; /* And the mask */
450
451	TAILQ_HEAD(tdb_policy_head, ipsec_policy) tdb_policy_head; /* [P] */
452	TAILQ_ENTRY(tdb)	tdb_sync_entry;	/* [S] pfsync tdb queue */
453	u_int32_t	tdb_updates;	/* [S] pfsync update counter */
454};
455
456enum tdb_counters {
457	tdb_ipackets,           /* Input IPsec packets */
458	tdb_opackets,           /* Output IPsec packets */
459	tdb_ibytes,             /* Input bytes */
460	tdb_obytes,             /* Output bytes */
461	tdb_idrops,             /* Dropped on input */
462	tdb_odrops,             /* Dropped on output */
463	tdb_idecompbytes,       /* Input bytes, decompressed */
464	tdb_ouncompbytes,       /* Output bytes, uncompressed */
465	tdb_ncounters
466};
467
468static inline void
469tdbstat_inc(struct tdb *tdb, enum tdb_counters c)
470{
471	counters_inc(tdb->tdb_counters, c);
472}
473
474static inline void
475tdbstat_add(struct tdb *tdb, enum tdb_counters c, uint64_t v)
476{
477	counters_add(tdb->tdb_counters, c, v);
478}
479
480static inline void
481tdbstat_pkt(struct tdb *tdb, enum tdb_counters pc, enum tdb_counters bc,
482    uint64_t bytes)
483{
484	counters_pkt(tdb->tdb_counters, pc, bc, bytes);
485}
486
487struct tdb_ident {
488	u_int32_t spi;
489	union sockaddr_union dst;
490	u_int8_t proto;
491	u_int rdomain;
492};
493
494struct tdb_crypto {
495	union sockaddr_union	tc_dst;
496	u_int64_t		tc_rpl;
497	u_int32_t		tc_spi;
498	int			tc_protoff;
499	int			tc_skip;
500	u_int			tc_rdomain;
501	u_int8_t		tc_proto;
502};
503
504struct ipsecinit {
505	u_int8_t	*ii_enckey;
506	u_int8_t	*ii_authkey;
507	u_int16_t	ii_enckeylen;
508	u_int16_t	ii_authkeylen;
509	u_int8_t	ii_encalg;
510	u_int8_t	ii_authalg;
511	u_int8_t	ii_compalg;
512};
513
514/* xform IDs */
515#define	XF_IP4		1	/* IP inside IP */
516#define	XF_AH		2	/* AH */
517#define	XF_ESP		3	/* ESP */
518#define	XF_TCPSIGNATURE	5	/* TCP MD5 Signature option, RFC 2358 */
519#define	XF_IPCOMP	6	/* IPCOMP */
520
521/* xform attributes */
522#define	XFT_AUTH	0x0001
523#define	XFT_CONF	0x0100
524#define	XFT_COMP	0x1000
525
526#define	IPSEC_ZEROES_SIZE	256	/* Larger than an IP6 extension hdr. */
527
528struct xformsw {
529	u_short	xf_type;		/* Unique ID of xform */
530	u_short	xf_flags;		/* flags (see below) */
531	char	*xf_name;		/* human-readable name */
532	int	(*xf_attach)(void);	/* called at config time */
533	int	(*xf_init)(struct tdb *, const struct xformsw *,
534		    struct ipsecinit *);
535	int	(*xf_zeroize)(struct tdb *); /* termination */
536	int	(*xf_input)(struct mbuf **, struct tdb *, int, int);
537	int	(*xf_output)(struct mbuf *, struct tdb *, int, int);
538};
539
540extern int ipsec_in_use;
541extern u_int64_t ipsec_last_added;
542extern int encdebug;			/* enable message reporting */
543extern struct pool tdb_pool;
544
545extern int ipsec_keep_invalid;		/* lifetime of embryonic SAs (in sec) */
546extern int ipsec_require_pfs;		/* use Perfect Forward Secrecy */
547extern int ipsec_expire_acquire;	/* wait for security assoc. (in sec) */
548extern int ipsec_soft_allocations;	/* flows/SA before renegotiation */
549extern int ipsec_exp_allocations;	/* num. of flows/SA before it expires */
550extern int ipsec_soft_bytes;		/* bytes/SA before renegotiation */
551extern int ipsec_exp_bytes;		/* num of bytes/SA before it expires */
552extern int ipsec_soft_timeout;		/* seconds/SA before renegotiation */
553extern int ipsec_exp_timeout;		/* seconds/SA before it expires */
554extern int ipsec_soft_first_use;	/* seconds between 1st asso & renego */
555extern int ipsec_exp_first_use;		/* seconds between 1st asso & expire */
556
557/*
558 * Names for IPsec sysctl objects
559 */
560#define	IPSEC_ENCDEBUG			IPCTL_ENCDEBUG			/* 12 */
561#define	IPSEC_STATS			IPCTL_IPSEC_STATS		/* 13 */
562#define IPSEC_EXPIRE_ACQUIRE		IPCTL_IPSEC_EXPIRE_ACQUIRE	/* 14 */
563#define IPSEC_EMBRYONIC_SA_TIMEOUT	IPCTL_IPSEC_EMBRYONIC_SA_TIMEOUT/* 15 */
564#define IPSEC_REQUIRE_PFS		IPCTL_IPSEC_REQUIRE_PFS		/* 16 */
565#define IPSEC_SOFT_ALLOCATIONS          IPCTL_IPSEC_SOFT_ALLOCATIONS	/* 17 */
566#define IPSEC_ALLOCATIONS		IPCTL_IPSEC_ALLOCATIONS		/* 18 */
567#define IPSEC_SOFT_BYTES		IPCTL_IPSEC_SOFT_BYTES		/* 19 */
568#define IPSEC_BYTES			IPCTL_IPSEC_BYTES		/* 20 */
569#define IPSEC_TIMEOUT			IPCTL_IPSEC_TIMEOUT		/* 21 */
570#define IPSEC_SOFT_TIMEOUT		IPCTL_IPSEC_SOFT_TIMEOUT	/* 22 */
571#define IPSEC_SOFT_FIRSTUSE		IPCTL_IPSEC_SOFT_FIRSTUSE	/* 23 */
572#define IPSEC_FIRSTUSE			IPCTL_IPSEC_FIRSTUSE		/* 24 */
573#define IPSEC_MAXID	25
574
575extern char ipsec_def_enc[];
576extern char ipsec_def_auth[];
577extern char ipsec_def_comp[];
578
579extern TAILQ_HEAD(ipsec_policy_head, ipsec_policy) ipsec_policy_head;
580
581extern struct mutex tdb_sadb_mtx;
582extern struct mutex ipo_tdb_mtx;
583
584struct cryptop;
585
586/* Misc. */
587const char *ipsp_address(union sockaddr_union *, char *, socklen_t);
588
589/* SPD tables */
590struct radix_node_head *spd_table_add(unsigned int);
591struct radix_node_head *spd_table_get(unsigned int);
592int spd_table_walk(unsigned int,
593    int (*walker)(struct ipsec_policy *, void *, unsigned int), void *);
594
595/* TDB management routines */
596uint32_t reserve_spi(u_int, u_int32_t, u_int32_t, union sockaddr_union *,
597		union sockaddr_union *, u_int8_t, int *);
598struct	tdb *gettdb_dir(u_int, u_int32_t, union sockaddr_union *, u_int8_t, int);
599#define gettdb(a,b,c,d)		gettdb_dir((a),(b),(c),(d),0)
600#define gettdb_rev(a,b,c,d)	gettdb_dir((a),(b),(c),(d),1)
601struct	tdb *gettdbbydst(u_int, union sockaddr_union *, u_int8_t,
602		struct ipsec_ids *,
603		struct sockaddr_encap *, struct sockaddr_encap *);
604struct	tdb *gettdbbysrc(u_int, union sockaddr_union *, u_int8_t,
605		struct ipsec_ids *,
606		struct sockaddr_encap *, struct sockaddr_encap *);
607struct	tdb *gettdbbysrcdst_dir(u_int, u_int32_t, union sockaddr_union *,
608		union sockaddr_union *, u_int8_t, int);
609#define gettdbbysrcdst(a,b,c,d,e) gettdbbysrcdst_dir((a),(b),(c),(d),(e),0)
610#define gettdbbysrcdst_rev(a,b,c,d,e) gettdbbysrcdst_dir((a),(b),(c),(d),(e),1)
611void	puttdb(struct tdb *);
612void	puttdb_locked(struct tdb *);
613void	tdb_delete(struct tdb *);
614struct	tdb *tdb_alloc(u_int);
615struct	tdb *tdb_ref(struct tdb *);
616void	tdb_unref(struct tdb *);
617void	tdb_free(struct tdb *);
618int	tdb_init(struct tdb *, u_int16_t, struct ipsecinit *);
619void	tdb_unlink(struct tdb *);
620void	tdb_unlink_locked(struct tdb *);
621void	tdb_cleanspd(struct tdb *);
622void	tdb_unbundle(struct tdb *);
623void	tdb_addtimeouts(struct tdb *);
624void	tdb_deltimeouts(struct tdb *);
625int	tdb_walk(u_int, int (*)(struct tdb *, void *, int), void *);
626void	tdb_printit(void *, int, int (*)(const char *, ...));
627
628/* XF_IP4 */
629int	ipe4_attach(void);
630int	ipe4_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
631int	ipe4_zeroize(struct tdb *);
632int	ipe4_input(struct mbuf **, struct tdb *, int, int);
633
634/* XF_AH */
635int	ah_attach(void);
636int	ah_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
637int	ah_zeroize(struct tdb *);
638int	ah_input(struct mbuf **, struct tdb *, int, int);
639int	ah_output(struct mbuf *, struct tdb *, int, int);
640int	ah_sysctl(int *, u_int, void *, size_t *, void *, size_t);
641
642int	ah46_input(struct mbuf **, int *, int, int);
643void	ah4_ctlinput(int, struct sockaddr *, u_int, void *);
644void	udpencap_ctlinput(int, struct sockaddr *, u_int, void *);
645
646/* XF_ESP */
647int	esp_attach(void);
648int	esp_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
649int	esp_zeroize(struct tdb *);
650int	esp_input(struct mbuf **, struct tdb *, int, int);
651int	esp_output(struct mbuf *, struct tdb *, int, int);
652int	esp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
653
654int	esp46_input(struct mbuf **, int *, int, int);
655void	esp4_ctlinput(int, struct sockaddr *, u_int, void *);
656
657/* XF_IPCOMP */
658int	ipcomp_attach(void);
659int	ipcomp_init(struct tdb *, const struct xformsw *, struct ipsecinit *);
660int	ipcomp_zeroize(struct tdb *);
661int	ipcomp_input(struct mbuf **, struct tdb *, int, int);
662int	ipcomp_output(struct mbuf *, struct tdb *, int, int);
663int	ipcomp_sysctl(int *, u_int, void *, size_t *, void *, size_t);
664int	ipcomp46_input(struct mbuf **, int *, int, int);
665
666/* XF_TCPSIGNATURE */
667int	tcp_signature_tdb_attach(void);
668int	tcp_signature_tdb_init(struct tdb *, const struct xformsw *,
669	    struct ipsecinit *);
670int	tcp_signature_tdb_zeroize(struct tdb *);
671int	tcp_signature_tdb_input(struct mbuf **, struct tdb *, int, int);
672int	tcp_signature_tdb_output(struct mbuf *, struct tdb *, int, int);
673
674/* Replay window */
675int	checkreplaywindow(struct tdb *, u_int64_t, u_int32_t, u_int32_t *, int);
676
677/* Packet processing */
678int	ipsp_process_packet(struct mbuf *, struct tdb *, int, int);
679int	ipsp_process_done(struct mbuf *, struct tdb *);
680int	ipsp_spd_lookup(struct mbuf *, int, int, int, struct tdb *,
681	    const struct ipsec_level *, struct tdb **, struct ipsec_ids *);
682int	ipsp_is_unspecified(union sockaddr_union);
683int	ipsp_aux_match(struct tdb *, struct ipsec_ids *,
684	    struct sockaddr_encap *, struct sockaddr_encap *);
685int	ipsp_ids_match(struct ipsec_ids *, struct ipsec_ids *);
686struct ipsec_ids *ipsp_ids_insert(struct ipsec_ids *);
687struct ipsec_ids *ipsp_ids_lookup(u_int32_t);
688void	ipsp_ids_free(struct ipsec_ids *);
689
690void	ipsp_init(void);
691void	ipsec_init(void);
692int	ipsec_sysctl(int *, u_int, void *, size_t *, void *, size_t);
693int	ipsec_common_input(struct mbuf **, int, int, int, int, int);
694int	ipsec_common_input_cb(struct mbuf **, struct tdb *, int, int);
695int	ipsec_input_disabled(struct mbuf **, int *, int, int);
696int	ipsec_protoff(struct mbuf *, int, int);
697int	ipsec_delete_policy(struct ipsec_policy *);
698ssize_t	ipsec_hdrsz(struct tdb *);
699void	ipsec_adjust_mtu(struct mbuf *, u_int32_t);
700void	ipsec_set_mtu(struct tdb *, u_int32_t);
701struct	ipsec_acquire *ipsec_get_acquire(u_int32_t);
702void	ipsec_unref_acquire(struct ipsec_acquire *);
703int	ipsec_forward_check(struct mbuf *, int, int);
704int	ipsec_local_check(struct mbuf *, int, int, int);
705
706#endif /* _KERNEL */
707#endif /* _NETINET_IPSP_H_ */
708