1f6bdcf8sam/*	$FreeBSD$	*/
2f6bdcf8sam/*	$KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $	*/
3f6bdcf8sam
4a50ffc2imp/*-
54736ccfpfg * SPDX-License-Identifier: BSD-3-Clause
64736ccfpfg *
7f6bdcf8sam * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8f6bdcf8sam * All rights reserved.
9f6bdcf8sam *
10f6bdcf8sam * Redistribution and use in source and binary forms, with or without
11f6bdcf8sam * modification, are permitted provided that the following conditions
12f6bdcf8sam * are met:
13f6bdcf8sam * 1. Redistributions of source code must retain the above copyright
14f6bdcf8sam *    notice, this list of conditions and the following disclaimer.
15f6bdcf8sam * 2. Redistributions in binary form must reproduce the above copyright
16f6bdcf8sam *    notice, this list of conditions and the following disclaimer in the
17f6bdcf8sam *    documentation and/or other materials provided with the distribution.
18f6bdcf8sam * 3. Neither the name of the project nor the names of its contributors
19f6bdcf8sam *    may be used to endorse or promote products derived from this software
20f6bdcf8sam *    without specific prior written permission.
21f6bdcf8sam *
22f6bdcf8sam * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23f6bdcf8sam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f6bdcf8sam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f6bdcf8sam * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26f6bdcf8sam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27f6bdcf8sam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28f6bdcf8sam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29f6bdcf8sam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30f6bdcf8sam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31f6bdcf8sam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f6bdcf8sam * SUCH DAMAGE.
33f6bdcf8sam */
34f6bdcf8sam
35f6bdcf8sam/*
36f6bdcf8sam * IPsec controller part.
37f6bdcf8sam */
38f6bdcf8sam
39f6bdcf8sam#ifndef _NETIPSEC_IPSEC_H_
40f6bdcf8sam#define _NETIPSEC_IPSEC_H_
41f6bdcf8sam
42f6bdcf8sam#include <net/pfkeyv2.h>
43f6bdcf8sam#include <netipsec/keydb.h>
44f6bdcf8sam
45f6bdcf8sam#ifdef _KERNEL
46f6bdcf8sam
47b82eb2fae#include <sys/_lock.h>
48b82eb2fae#include <sys/_mutex.h>
49b82eb2fae#include <sys/_rwlock.h>
50b82eb2fae
515e9c239gnn#define	IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
525e9c239gnn
53f6bdcf8sam/*
54f6bdcf8sam * Security Policy Index
55f6bdcf8sam * Ensure that both address families in the "src" and "dst" are same.
56f6bdcf8sam * When the value of the ul_proto is ICMPv6, the port field in "src"
57f6bdcf8sam * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
58f6bdcf8sam */
59f6bdcf8samstruct secpolicyindex {
60f6bdcf8sam	union sockaddr_union src;	/* IP src address for SP */
61f6bdcf8sam	union sockaddr_union dst;	/* IP dst address for SP */
620fb6ad5ae	uint8_t ul_proto;		/* upper layer Protocol */
630fb6ad5ae	uint8_t dir;			/* direction of packet flow */
640fb6ad5ae	uint8_t prefs;			/* prefix length in bits for src */
650fb6ad5ae	uint8_t prefd;			/* prefix length in bits for dst */
660fb6ad5ae};
670fb6ad5ae
680fb6ad5ae/* Request for IPsec */
690fb6ad5aestruct ipsecrequest {
700fb6ad5ae	struct secasindex saidx;/* hint for search proper SA */
710fb6ad5ae				/* if __ss_len == 0 then no address specified.*/
720fb6ad5ae	u_int level;		/* IPsec level defined below. */
73f6bdcf8sam};
74f6bdcf8sam
75f6bdcf8sam/* Security Policy Data Base */
76f6bdcf8samstruct secpolicy {
7784b82e8ae	TAILQ_ENTRY(secpolicy) chain;
780fb6ad5ae	LIST_ENTRY(secpolicy) idhash;
790fb6ad5ae	LIST_ENTRY(secpolicy) drainq;
80f6bdcf8sam
81f6bdcf8sam	struct secpolicyindex spidx;	/* selector */
820fb6ad5ae#define	IPSEC_MAXREQ		4
830fb6ad5ae	struct ipsecrequest *req[IPSEC_MAXREQ];
840fb6ad5ae	u_int tcount;			/* IPsec transforms count */
850fb6ad5ae	volatile u_int refcnt;		/* reference count */
8684b82e8ae	u_int policy;			/* policy_type per pfkeyv2.h */
87217cb9fae	u_int state;
88217cb9fae#define	IPSEC_SPSTATE_DEAD	0
890fb6ad5ae#define	IPSEC_SPSTATE_LARVAL	1
900fb6ad5ae#define	IPSEC_SPSTATE_ALIVE	2
910fb6ad5ae#define	IPSEC_SPSTATE_PCB	3
920fb6ad5ae#define	IPSEC_SPSTATE_IFNET	4
930fb6ad5ae	uint32_t priority;		/* priority of this policy */
940fb6ad5ae	uint32_t id;			/* It's unique number on the system. */
95f6bdcf8sam	/*
96f6bdcf8sam	 * lifetime handler.
97f6bdcf8sam	 * the policy can be used without limitiation if both lifetime and
98f6bdcf8sam	 * validtime are zero.
99f6bdcf8sam	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
100f6bdcf8sam	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
101f6bdcf8sam	 */
1020a6c1d4sam	time_t created;		/* time created the policy */
1030a6c1d4sam	time_t lastused;	/* updated every when kernel sends a packet */
104f6bdcf8sam	long lifetime;		/* duration of the lifetime of this policy */
105f6bdcf8sam	long validtime;		/* duration this policy is valid without use */
106f6bdcf8sam};
107f6bdcf8sam
1080a6c1d4sam/*
1090fb6ad5ae * PCB security policies.
1100fb6ad5ae * Application can setup private security policies for socket.
1110fb6ad5ae * Such policies can have IPSEC, BYPASS and ENTRUST type.
1120fb6ad5ae * By default, policies are set to NULL. This means that they have ENTRUST type.
1130fb6ad5ae * When application sets BYPASS or IPSEC type policy, the flags field
1140fb6ad5ae * is also updated. When flags is not set, the system could store
1150fb6ad5ae * used security policy into the sp_in/sp_out pointer to speed up further
1160fb6ad5ae * lookups.
1170a6c1d4sam */
118f6bdcf8samstruct inpcbpolicy {
1190fb6ad5ae	struct secpolicy	*sp_in;
1200fb6ad5ae	struct secpolicy	*sp_out;
1210fb6ad5ae
1220fb6ad5ae	uint32_t		genid;
1230fb6ad5ae	uint16_t		flags;
1240fb6ad5ae#define	INP_INBOUND_POLICY	0x0001
1250fb6ad5ae#define	INP_OUTBOUND_POLICY	0x0002
1260fb6ad5ae	uint16_t		hdrsz;
127f6bdcf8sam};
128f6bdcf8sam
129f6bdcf8sam/* SP acquiring list table. */
130f6bdcf8samstruct secspacq {
131f6bdcf8sam	LIST_ENTRY(secspacq) chain;
132f6bdcf8sam
133f6bdcf8sam	struct secpolicyindex spidx;
134f6bdcf8sam
1350a6c1d4sam	time_t created;		/* for lifetime */
136f6bdcf8sam	int count;		/* for lifetime */
137f6bdcf8sam	/* XXX: here is mbuf place holder to be sent ? */
138f6bdcf8sam};
139f6bdcf8sam#endif /* _KERNEL */
140f6bdcf8sam
1410fb6ad5ae/* buffer size for formatted output of ipsec address */
1420fb6ad5ae#define	IPSEC_ADDRSTRLEN	(INET6_ADDRSTRLEN + 11)
1430fb6ad5ae
144f6bdcf8sam/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
145f6bdcf8sam#define IPSEC_PORT_ANY		0
146f6bdcf8sam#define IPSEC_ULPROTO_ANY	255
147f6bdcf8sam#define IPSEC_PROTO_ANY		255
148f6bdcf8sam
149f6bdcf8sam/* mode of security protocol */
150f6bdcf8sam/* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
151f6bdcf8sam#define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
152f6bdcf8sam#define	IPSEC_MODE_TRANSPORT	1
153f6bdcf8sam#define	IPSEC_MODE_TUNNEL	2
154903cdeebms#define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
155f6bdcf8sam
156f6bdcf8sam/*
157f6bdcf8sam * Direction of security policy.
158f6bdcf8sam * NOTE: Since INVALID is used just as flag.
159f6bdcf8sam * The other are used for loop counter too.
160f6bdcf8sam */
161f6bdcf8sam#define IPSEC_DIR_ANY		0
162f6bdcf8sam#define IPSEC_DIR_INBOUND	1
163f6bdcf8sam#define IPSEC_DIR_OUTBOUND	2
164f6bdcf8sam#define IPSEC_DIR_MAX		3
165f6bdcf8sam#define IPSEC_DIR_INVALID	4
166f6bdcf8sam
167f6bdcf8sam/* Policy level */
168f6bdcf8sam/*
169f6bdcf8sam * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
170f6bdcf8sam * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
171f6bdcf8sam * DISCARD and NONE are allowed for system default.
172f6bdcf8sam */
173f6bdcf8sam#define IPSEC_POLICY_DISCARD	0	/* discarding packet */
174f6bdcf8sam#define IPSEC_POLICY_NONE	1	/* through IPsec engine */
175f6bdcf8sam#define IPSEC_POLICY_IPSEC	2	/* do IPsec */
176f6bdcf8sam#define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
177f6bdcf8sam#define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
178f6bdcf8sam
179749be37ae/* Policy scope */
180749be37ae#define	IPSEC_POLICYSCOPE_ANY		0x00	/* unspecified */
181749be37ae#define	IPSEC_POLICYSCOPE_GLOBAL	0x01	/* global scope */
182749be37ae#define	IPSEC_POLICYSCOPE_IFNET		0x02	/* if_ipsec(4) scope */
183749be37ae#define	IPSEC_POLICYSCOPE_PCB		0x04	/* PCB scope */
184749be37ae
185f6bdcf8sam/* Security protocol level */
186f6bdcf8sam#define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
187f6bdcf8sam#define	IPSEC_LEVEL_USE		1	/* use SA if present. */
188f6bdcf8sam#define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
189f6bdcf8sam#define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
190f6bdcf8sam
191f6bdcf8sam#define IPSEC_MANUAL_REQID_MAX	0x3fff
192f6bdcf8sam				/*
193f6bdcf8sam				 * if security policy level == unique, this id
194f6bdcf8sam				 * indicate to a relative SA for use, else is
195f6bdcf8sam				 * zero.
196f6bdcf8sam				 * 1 - 0x3fff are reserved for manual keying.
197f6bdcf8sam				 * 0 are reserved for above reason.  Others is
198f6bdcf8sam				 * for kernel use.
199f6bdcf8sam				 * Note that this id doesn't identify SA
200f6bdcf8sam				 * by only itself.
201f6bdcf8sam				 */
202f6bdcf8sam#define IPSEC_REPLAYWSIZE  32
203f6bdcf8sam
2040cd74dbgnn/* statistics for ipsec processing */
205f6bdcf8samstruct ipsecstat {
2061a36dfcae	uint64_t ips_in_polvio;		/* input: sec policy violation */
207afd48faae	uint64_t ips_in_nomem;		/* input: no memory available */
208afd48faae	uint64_t ips_in_inval;		/* input: generic error */
209afd48faae
2101a36dfcae	uint64_t ips_out_polvio;	/* output: sec policy violation */
2111a36dfcae	uint64_t ips_out_nosa;		/* output: SA unavailable  */
2121a36dfcae	uint64_t ips_out_nomem;		/* output: no memory available */
2131a36dfcae	uint64_t ips_out_noroute;	/* output: no route available */
2141a36dfcae	uint64_t ips_out_inval;		/* output: generic error */
2151a36dfcae	uint64_t ips_out_bundlesa;	/* output: bundled SA processed */
216afd48faae
21797541cafabient	uint64_t ips_spdcache_hits;	/* SPD cache hits */
21897541cafabient	uint64_t ips_spdcache_misses;	/* SPD cache misses */
21997541cafabient
2201a36dfcae	uint64_t ips_clcopied;		/* clusters copied during clone */
2211a36dfcae	uint64_t ips_mbinserted;	/* mbufs inserted during makespace */
222f6bdcf8sam	/*
223f6bdcf8sam	 * Temporary statistics for performance analysis.
224f6bdcf8sam	 */
225f6bdcf8sam	/* See where ESP/AH/IPCOMP header land in mbuf on input */
2261a36dfcae	uint64_t ips_input_front;
2271a36dfcae	uint64_t ips_input_middle;
2281a36dfcae	uint64_t ips_input_end;
229f6bdcf8sam};
230f6bdcf8sam
231f6bdcf8sam/*
232f6bdcf8sam * Definitions for IPsec & Key sysctl operations.
233f6bdcf8sam */
234f6bdcf8sam#define IPSECCTL_STATS			1	/* stats */
235f6bdcf8sam#define IPSECCTL_DEF_POLICY		2
236f6bdcf8sam#define IPSECCTL_DEF_ESP_TRANSLEV	3	/* int; ESP transport mode */
237f6bdcf8sam#define IPSECCTL_DEF_ESP_NETLEV		4	/* int; ESP tunnel mode */
238f6bdcf8sam#define IPSECCTL_DEF_AH_TRANSLEV	5	/* int; AH transport mode */
239f6bdcf8sam#define IPSECCTL_DEF_AH_NETLEV		6	/* int; AH tunnel mode */
240f6bdcf8sam#if 0	/* obsolete, do not reuse */
241f6bdcf8sam#define IPSECCTL_INBOUND_CALL_IKE	7
242f6bdcf8sam#endif
243f6bdcf8sam#define	IPSECCTL_AH_CLEARTOS		8
244f6bdcf8sam#define	IPSECCTL_AH_OFFSETMASK		9
245f6bdcf8sam#define	IPSECCTL_DFBIT			10
246f6bdcf8sam#define	IPSECCTL_ECN			11
247f6bdcf8sam#define	IPSECCTL_DEBUG			12
248f6bdcf8sam#define	IPSECCTL_ESP_RANDPAD		13
249f6bdcf8sam
250f6bdcf8sam#ifdef _KERNEL
251d467a41ae#include <sys/counter.h>
252d467a41ae
253d81208caestruct ipsec_ctx_data;
2544c7d86cae#define	IPSEC_INIT_CTX(_ctx, _mp, _inp, _sav, _af, _enc) do {	\
255d81208cae	(_ctx)->mp = (_mp);				\
2564c7d86cae	(_ctx)->inp = (_inp);				\
257d81208cae	(_ctx)->sav = (_sav);				\
258d81208cae	(_ctx)->af = (_af);				\
259d81208cae	(_ctx)->enc = (_enc);				\
260d81208cae} while(0)
261d81208caeint	ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction);
262d81208cae
26357ca458rwatsonVNET_DECLARE(int, ipsec_debug);
26488f8de4rwatson#define	V_ipsec_debug		VNET(ipsec_debug)
2650a90ef1bz
2660a90ef1bz#ifdef REGRESSION
2670a90ef1bzVNET_DECLARE(int, ipsec_replay);
2680a90ef1bzVNET_DECLARE(int, ipsec_integrity);
2690a90ef1bz
2700a90ef1bz#define	V_ipsec_replay		VNET(ipsec_replay)
2710a90ef1bz#define	V_ipsec_integrity	VNET(ipsec_integrity)
2720a90ef1bz#endif
2730a90ef1bz
274d467a41aeVNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
2750a90ef1bzVNET_DECLARE(int, ip4_esp_trans_deflev);
2760a90ef1bzVNET_DECLARE(int, ip4_esp_net_deflev);
2770a90ef1bzVNET_DECLARE(int, ip4_ah_trans_deflev);
2780a90ef1bzVNET_DECLARE(int, ip4_ah_net_deflev);
27957ca458rwatsonVNET_DECLARE(int, ip4_ipsec_dfbit);
2800a90ef1bzVNET_DECLARE(int, ip4_ipsec_ecn);
2810a90ef1bzVNET_DECLARE(int, crypto_support);
282fbc9da5fabientVNET_DECLARE(int, async_crypto);
2830fb6ad5aeVNET_DECLARE(int, natt_cksum_policy);
2840a90ef1bz
285d467a41ae#define	IPSECSTAT_INC(name)	\
286d467a41ae    VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
28788f8de4rwatson#define	V_ip4_esp_trans_deflev	VNET(ip4_esp_trans_deflev)
28888f8de4rwatson#define	V_ip4_esp_net_deflev	VNET(ip4_esp_net_deflev)
28988f8de4rwatson#define	V_ip4_ah_trans_deflev	VNET(ip4_ah_trans_deflev)
29088f8de4rwatson#define	V_ip4_ah_net_deflev	VNET(ip4_ah_net_deflev)
2910a90ef1bz#define	V_ip4_ipsec_dfbit	VNET(ip4_ipsec_dfbit)
29288f8de4rwatson#define	V_ip4_ipsec_ecn		VNET(ip4_ipsec_ecn)
29388f8de4rwatson#define	V_crypto_support	VNET(crypto_support)
294fbc9da5fabient#define	V_async_crypto		VNET(async_crypto)
295