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