ipsec.h revision 365277
1/*	$FreeBSD: stable/11/sys/netipsec/ipsec.h 365277 2020-09-02 20:36:33Z jhb $	*/
2/*	$KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $	*/
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * IPsec controller part.
35 */
36
37#ifndef _NETIPSEC_IPSEC_H_
38#define _NETIPSEC_IPSEC_H_
39
40#include <net/pfkeyv2.h>
41#include <netipsec/keydb.h>
42
43#ifdef _KERNEL
44
45#include <sys/_lock.h>
46#include <sys/_mutex.h>
47#include <sys/_rwlock.h>
48
49#define	IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
50
51/*
52 * Security Policy Index
53 * Ensure that both address families in the "src" and "dst" are same.
54 * When the value of the ul_proto is ICMPv6, the port field in "src"
55 * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
56 */
57struct secpolicyindex {
58	union sockaddr_union src;	/* IP src address for SP */
59	union sockaddr_union dst;	/* IP dst address for SP */
60	uint8_t ul_proto;		/* upper layer Protocol */
61	uint8_t dir;			/* direction of packet flow */
62	uint8_t prefs;			/* prefix length in bits for src */
63	uint8_t prefd;			/* prefix length in bits for dst */
64};
65
66/* Request for IPsec */
67struct ipsecrequest {
68	struct secasindex saidx;/* hint for search proper SA */
69				/* if __ss_len == 0 then no address specified.*/
70	u_int level;		/* IPsec level defined below. */
71};
72
73/* Security Policy Data Base */
74struct secpolicy {
75	TAILQ_ENTRY(secpolicy) chain;
76	LIST_ENTRY(secpolicy) idhash;
77	LIST_ENTRY(secpolicy) drainq;
78
79	struct secpolicyindex spidx;	/* selector */
80#define	IPSEC_MAXREQ		4
81	struct ipsecrequest *req[IPSEC_MAXREQ];
82	u_int tcount;			/* IPsec transforms count */
83	volatile u_int refcnt;		/* reference count */
84	u_int policy;			/* policy_type per pfkeyv2.h */
85	u_int state;
86#define	IPSEC_SPSTATE_DEAD	0
87#define	IPSEC_SPSTATE_LARVAL	1
88#define	IPSEC_SPSTATE_ALIVE	2
89#define	IPSEC_SPSTATE_PCB	3
90#define	IPSEC_SPSTATE_IFNET	4
91	uint32_t priority;		/* priority of this policy */
92	uint32_t id;			/* It's unique number on the system. */
93	/*
94	 * lifetime handler.
95	 * the policy can be used without limitiation if both lifetime and
96	 * validtime are zero.
97	 * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
98	 * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
99	 */
100	time_t created;		/* time created the policy */
101	time_t lastused;	/* updated every when kernel sends a packet */
102	long lifetime;		/* duration of the lifetime of this policy */
103	long validtime;		/* duration this policy is valid without use */
104};
105
106/*
107 * PCB security policies.
108 * Application can setup private security policies for socket.
109 * Such policies can have IPSEC, BYPASS and ENTRUST type.
110 * By default, policies are set to NULL. This means that they have ENTRUST type.
111 * When application sets BYPASS or IPSEC type policy, the flags field
112 * is also updated. When flags is not set, the system could store
113 * used security policy into the sp_in/sp_out pointer to speed up further
114 * lookups.
115 */
116struct inpcbpolicy {
117	struct secpolicy	*sp_in;
118	struct secpolicy	*sp_out;
119
120	uint32_t		genid;
121	uint16_t		flags;
122#define	INP_INBOUND_POLICY	0x0001
123#define	INP_OUTBOUND_POLICY	0x0002
124	uint16_t		hdrsz;
125};
126
127/* SP acquiring list table. */
128struct secspacq {
129	LIST_ENTRY(secspacq) chain;
130
131	struct secpolicyindex spidx;
132
133	time_t created;		/* for lifetime */
134	int count;		/* for lifetime */
135	/* XXX: here is mbuf place holder to be sent ? */
136};
137#endif /* _KERNEL */
138
139/* buffer size for formatted output of ipsec address */
140#define	IPSEC_ADDRSTRLEN	(INET6_ADDRSTRLEN + 11)
141
142/* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
143#define IPSEC_PORT_ANY		0
144#define IPSEC_ULPROTO_ANY	255
145#define IPSEC_PROTO_ANY		255
146
147/* mode of security protocol */
148/* NOTE: DON'T use IPSEC_MODE_ANY at SPD.  It's only use in SAD */
149#define	IPSEC_MODE_ANY		0	/* i.e. wildcard. */
150#define	IPSEC_MODE_TRANSPORT	1
151#define	IPSEC_MODE_TUNNEL	2
152#define	IPSEC_MODE_TCPMD5	3	/* TCP MD5 mode */
153
154/*
155 * Direction of security policy.
156 * NOTE: Since INVALID is used just as flag.
157 * The other are used for loop counter too.
158 */
159#define IPSEC_DIR_ANY		0
160#define IPSEC_DIR_INBOUND	1
161#define IPSEC_DIR_OUTBOUND	2
162#define IPSEC_DIR_MAX		3
163#define IPSEC_DIR_INVALID	4
164
165/* Policy level */
166/*
167 * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
168 * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
169 * DISCARD and NONE are allowed for system default.
170 */
171#define IPSEC_POLICY_DISCARD	0	/* discarding packet */
172#define IPSEC_POLICY_NONE	1	/* through IPsec engine */
173#define IPSEC_POLICY_IPSEC	2	/* do IPsec */
174#define IPSEC_POLICY_ENTRUST	3	/* consulting SPD if present. */
175#define IPSEC_POLICY_BYPASS	4	/* only for privileged socket. */
176
177/* Policy scope */
178#define	IPSEC_POLICYSCOPE_ANY		0x00	/* unspecified */
179#define	IPSEC_POLICYSCOPE_GLOBAL	0x01	/* global scope */
180#define	IPSEC_POLICYSCOPE_IFNET		0x02	/* if_ipsec(4) scope */
181#define	IPSEC_POLICYSCOPE_PCB		0x04	/* PCB scope */
182
183/* Security protocol level */
184#define	IPSEC_LEVEL_DEFAULT	0	/* reference to system default */
185#define	IPSEC_LEVEL_USE		1	/* use SA if present. */
186#define	IPSEC_LEVEL_REQUIRE	2	/* require SA. */
187#define	IPSEC_LEVEL_UNIQUE	3	/* unique SA. */
188
189#define IPSEC_MANUAL_REQID_MAX	0x3fff
190				/*
191				 * if security policy level == unique, this id
192				 * indicate to a relative SA for use, else is
193				 * zero.
194				 * 1 - 0x3fff are reserved for manual keying.
195				 * 0 are reserved for above reason.  Others is
196				 * for kernel use.
197				 * Note that this id doesn't identify SA
198				 * by only itself.
199				 */
200#define IPSEC_REPLAYWSIZE  32
201
202/* statistics for ipsec processing */
203struct ipsecstat {
204	uint64_t ips_in_polvio;		/* input: sec policy violation */
205	uint64_t ips_in_nomem;		/* input: no memory available */
206	uint64_t ips_in_inval;		/* input: generic error */
207
208	uint64_t ips_out_polvio;	/* output: sec policy violation */
209	uint64_t ips_out_nosa;		/* output: SA unavailable  */
210	uint64_t ips_out_nomem;		/* output: no memory available */
211	uint64_t ips_out_noroute;	/* output: no route available */
212	uint64_t ips_out_inval;		/* output: generic error */
213	uint64_t ips_out_bundlesa;	/* output: bundled SA processed */
214
215	uint64_t ips_mbcoalesced;	/* mbufs coalesced during clone */
216	uint64_t ips_clcoalesced;	/* clusters coalesced during clone */
217	uint64_t ips_clcopied;		/* clusters copied during clone */
218	uint64_t ips_mbinserted;	/* mbufs inserted during makespace */
219	/*
220	 * Temporary statistics for performance analysis.
221	 */
222	/* See where ESP/AH/IPCOMP header land in mbuf on input */
223	uint64_t ips_input_front;
224	uint64_t ips_input_middle;
225	uint64_t ips_input_end;
226};
227
228/*
229 * Definitions for IPsec & Key sysctl operations.
230 */
231#define IPSECCTL_STATS			1	/* stats */
232#define IPSECCTL_DEF_POLICY		2
233#define IPSECCTL_DEF_ESP_TRANSLEV	3	/* int; ESP transport mode */
234#define IPSECCTL_DEF_ESP_NETLEV		4	/* int; ESP tunnel mode */
235#define IPSECCTL_DEF_AH_TRANSLEV	5	/* int; AH transport mode */
236#define IPSECCTL_DEF_AH_NETLEV		6	/* int; AH tunnel mode */
237#if 0	/* obsolete, do not reuse */
238#define IPSECCTL_INBOUND_CALL_IKE	7
239#endif
240#define	IPSECCTL_AH_CLEARTOS		8
241#define	IPSECCTL_AH_OFFSETMASK		9
242#define	IPSECCTL_DFBIT			10
243#define	IPSECCTL_ECN			11
244#define	IPSECCTL_DEBUG			12
245#define	IPSECCTL_ESP_RANDPAD		13
246
247#ifdef _KERNEL
248#include <sys/counter.h>
249
250struct ipsec_ctx_data;
251#define	IPSEC_INIT_CTX(_ctx, _mp, _inp, _sav, _af, _enc) do {	\
252	(_ctx)->mp = (_mp);				\
253	(_ctx)->inp = (_inp);				\
254	(_ctx)->sav = (_sav);				\
255	(_ctx)->af = (_af);				\
256	(_ctx)->enc = (_enc);				\
257} while(0)
258int	ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction);
259
260VNET_DECLARE(int, ipsec_debug);
261#define	V_ipsec_debug		VNET(ipsec_debug)
262
263#ifdef REGRESSION
264VNET_DECLARE(int, ipsec_replay);
265VNET_DECLARE(int, ipsec_integrity);
266
267#define	V_ipsec_replay		VNET(ipsec_replay)
268#define	V_ipsec_integrity	VNET(ipsec_integrity)
269#endif
270
271VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
272VNET_DECLARE(int, ip4_esp_trans_deflev);
273VNET_DECLARE(int, ip4_esp_net_deflev);
274VNET_DECLARE(int, ip4_ah_trans_deflev);
275VNET_DECLARE(int, ip4_ah_net_deflev);
276VNET_DECLARE(int, ip4_ipsec_dfbit);
277VNET_DECLARE(int, ip4_ipsec_ecn);
278VNET_DECLARE(int, crypto_support);
279VNET_DECLARE(int, natt_cksum_policy);
280
281extern struct timeval ipsec_warn_interval;
282
283#define	IPSECSTAT_INC(name)	\
284    VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
285#define	V_ip4_esp_trans_deflev	VNET(ip4_esp_trans_deflev)
286#define	V_ip4_esp_net_deflev	VNET(ip4_esp_net_deflev)
287#define	V_ip4_ah_trans_deflev	VNET(ip4_ah_trans_deflev)
288#define	V_ip4_ah_net_deflev	VNET(ip4_ah_net_deflev)
289#define	V_ip4_ipsec_dfbit	VNET(ip4_ipsec_dfbit)
290#define	V_ip4_ipsec_ecn		VNET(ip4_ipsec_ecn)
291#define	V_crypto_support	VNET(crypto_support)
292#define	V_natt_cksum_policy	VNET(natt_cksum_policy)
293
294#define ipseclog(x)	do { if (V_ipsec_debug) log x; } while (0)
295/* for openbsd compatibility */
296#ifdef IPSEC_DEBUG
297#define	IPSEC_DEBUG_DECLARE(x)	x
298#define	DPRINTF(x)	do { if (V_ipsec_debug) printf x; } while (0)
299#else
300#define	IPSEC_DEBUG_DECLARE(x)
301#define	DPRINTF(x)
302#endif
303
304struct inpcb;
305struct m_tag;
306struct secasvar;
307struct sockopt;
308struct tcphdr;
309union sockaddr_union;
310
311int ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t);
312
313struct ipsecrequest *ipsec_newisr(void);
314void ipsec_delisr(struct ipsecrequest *);
315struct secpolicy *ipsec4_checkpolicy(const struct mbuf *, struct inpcb *,
316    int *, int);
317
318u_int ipsec_get_reqlevel(struct secpolicy *, u_int);
319
320void udp_ipsec_adjust_cksum(struct mbuf *, struct secasvar *, int, int);
321int udp_ipsec_output(struct mbuf *, struct secasvar *);
322int udp_ipsec_input(struct mbuf *, int, int);
323int udp_ipsec_pcbctl(struct inpcb *, struct sockopt *);
324
325int ipsec_chkreplay(uint32_t, struct secasvar *);
326int ipsec_updatereplay(uint32_t, struct secasvar *);
327int ipsec_updateid(struct secasvar *, uint64_t *, uint64_t *);
328int ipsec_initialized(void);
329
330void ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int);
331
332void ipsec4_setsockaddrs(const struct mbuf *, union sockaddr_union *,
333    union sockaddr_union *);
334int ipsec4_in_reject(const struct mbuf *, struct inpcb *);
335int ipsec4_input(struct mbuf *, int, int);
336int ipsec4_forward(struct mbuf *);
337int ipsec4_pcbctl(struct inpcb *, struct sockopt *);
338int ipsec4_output(struct mbuf *, struct inpcb *);
339int ipsec4_capability(struct mbuf *, u_int);
340int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
341int ipsec4_process_packet(struct mbuf *, struct secpolicy *, struct inpcb *);
342int ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *,
343    u_int);
344
345extern	void m_checkalignment(const char* where, struct mbuf *m0,
346		int off, int len);
347extern	struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
348extern	caddr_t m_pad(struct mbuf *m, int n);
349extern	int m_striphdr(struct mbuf *m, int skip, int hlen);
350
351#endif /* _KERNEL */
352
353#ifndef _KERNEL
354extern caddr_t ipsec_set_policy(char *, int);
355extern int ipsec_get_policylen(caddr_t);
356extern char *ipsec_dump_policy(caddr_t, char *);
357extern const char *ipsec_strerror(void);
358
359#endif /* ! KERNEL */
360
361#endif /* _NETIPSEC_IPSEC_H_ */
362