xref: /freebsd/sys/netipsec/ipsec.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane 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 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/domain.h>
47 #include <sys/priv.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/errno.h>
52 #include <sys/hhook.h>
53 #include <sys/time.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
57 #include <sys/proc.h>
58 
59 #include <net/if.h>
60 #include <net/if_enc.h>
61 #include <net/if_var.h>
62 #include <net/vnet.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71 #include <netinet/tcp.h>
72 #include <netinet/udp.h>
73 
74 #include <netinet/ip6.h>
75 #ifdef INET6
76 #include <netinet6/ip6_var.h>
77 #endif
78 #include <netinet/in_pcb.h>
79 #ifdef INET6
80 #include <netinet/icmp6.h>
81 #endif
82 
83 #include <sys/types.h>
84 #include <netipsec/ipsec.h>
85 #ifdef INET6
86 #include <netipsec/ipsec6.h>
87 #endif
88 #include <netipsec/ah_var.h>
89 #include <netipsec/esp_var.h>
90 #include <netipsec/ipcomp.h>		/*XXX*/
91 #include <netipsec/ipcomp_var.h>
92 #include <netipsec/ipsec_support.h>
93 
94 #include <netipsec/key.h>
95 #include <netipsec/keydb.h>
96 #include <netipsec/key_debug.h>
97 
98 #include <netipsec/xform.h>
99 
100 #include <machine/in_cksum.h>
101 
102 #include <opencrypto/cryptodev.h>
103 
104 /* NB: name changed so netstat doesn't use it. */
105 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
106 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
107 
108 #ifdef VIMAGE
109 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
110 #endif /* VIMAGE */
111 
112 /* DF bit on encap. 0: clear 1: set 2: copy */
113 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
114 VNET_DEFINE(int, ip4_ipsec_min_pmtu) = 576;
115 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
116 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
117 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
118 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
119 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
120 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
121 
122 VNET_DEFINE_STATIC(int, ip4_filtertunnel) = 0;
123 #define	V_ip4_filtertunnel VNET(ip4_filtertunnel)
124 VNET_DEFINE_STATIC(int, check_policy_history) = 0;
125 #define	V_check_policy_history	VNET(check_policy_history)
126 VNET_DEFINE_STATIC(struct secpolicy *, def_policy) = NULL;
127 #define	V_def_policy	VNET(def_policy)
128 static int
129 sysctl_def_policy(SYSCTL_HANDLER_ARGS)
130 {
131 	int error, value;
132 
133 	value = V_def_policy->policy;
134 	error = sysctl_handle_int(oidp, &value, 0, req);
135 	if (error == 0) {
136 		if (value != IPSEC_POLICY_DISCARD &&
137 		    value != IPSEC_POLICY_NONE)
138 			return (EINVAL);
139 		V_def_policy->policy = value;
140 	}
141 	return (error);
142 }
143 
144 /*
145  * Crypto support requirements:
146  *
147  *  1	require hardware support
148  * -1	require software support
149  *  0	take anything
150  */
151 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
152 
153 /*
154  * Use asynchronous mode to parallelize crypto jobs:
155  *
156  *  0 - disabled
157  *  1 - enabled
158  */
159 VNET_DEFINE(int, async_crypto) = 0;
160 
161 /*
162  * TCP/UDP checksum handling policy for transport mode NAT-T (RFC3948)
163  *
164  * 0 - auto: incrementally recompute, when checksum delta is known;
165  *     if checksum delta isn't known, reset checksum to zero for UDP,
166  *     and mark csum_flags as valid for TCP.
167  * 1 - fully recompute TCP/UDP checksum.
168  */
169 VNET_DEFINE(int, natt_cksum_policy) = 0;
170 
171 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
172 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
173 
174 SYSCTL_DECL(_net_inet_ipsec);
175 
176 /* net.inet.ipsec */
177 SYSCTL_PROC(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
178     CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
179     0, 0, sysctl_def_policy, "I",
180     "IPsec default policy.");
181 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
182 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
183 	"Default ESP transport mode level");
184 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
185 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
186 	"Default ESP tunnel mode level.");
187 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
188 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
189 	"AH transfer mode default level.");
190 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
191 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
192 	"AH tunnel mode default level.");
193 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
194 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
195 	"If set, clear type-of-service field when doing AH computation.");
196 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
197 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
198 	"Do not fragment bit on encap.");
199 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_MIN_PMTU, min_pmtu,
200 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_min_pmtu), 0,
201 	"Lowest acceptable PMTU value.");
202 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
203 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
204 	"Explicit Congestion Notification handling.");
205 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
206 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
207 	"Crypto driver selection.");
208 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, async_crypto,
209 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(async_crypto), 0,
210 	"Use asynchronous mode to parallelize crypto jobs.");
211 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, check_policy_history,
212 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(check_policy_history), 0,
213 	"Use strict check of inbound packets to security policy compliance.");
214 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, natt_cksum_policy,
215 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(natt_cksum_policy), 0,
216 	"Method to fix TCP/UDP checksum for transport mode IPsec after NAT.");
217 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, filtertunnel,
218 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_filtertunnel), 0,
219 	"If set, filter packets from an IPsec tunnel.");
220 SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat,
221     ipsec4stat, "IPsec IPv4 statistics.");
222 
223 #ifdef REGRESSION
224 /*
225  * When set to 1, IPsec will send packets with the same sequence number.
226  * This allows to verify if the other side has proper replay attacks detection.
227  */
228 VNET_DEFINE(int, ipsec_replay) = 0;
229 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay,
230 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
231 	"Emulate replay attack");
232 /*
233  * When set 1, IPsec will send packets with corrupted HMAC.
234  * This allows to verify if the other side properly detects modified packets.
235  */
236 VNET_DEFINE(int, ipsec_integrity) = 0;
237 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
238 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
239 	"Emulate man-in-the-middle attack");
240 #endif
241 
242 #ifdef INET6
243 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
244 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
245 
246 #ifdef VIMAGE
247 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
248 #endif /* VIMAGE */
249 
250 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
251 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
252 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
253 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
254 VNET_DEFINE(int, ip6_ipsec_ecn) = 0;	/* ECN ignore(-1)/forbidden(0)/allowed(1) */
255 
256 VNET_DEFINE_STATIC(int, ip6_filtertunnel) = 0;
257 #define	V_ip6_filtertunnel	VNET(ip6_filtertunnel)
258 
259 SYSCTL_DECL(_net_inet6_ipsec6);
260 
261 /* net.inet6.ipsec6 */
262 SYSCTL_PROC(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy,
263     CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
264     0, 0, sysctl_def_policy, "I",
265     "IPsec default policy.");
266 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
267 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
268 	"Default ESP transport mode level.");
269 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
270 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0,
271 	"Default ESP tunnel mode level.");
272 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
273 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0,
274 	"AH transfer mode default level.");
275 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
276 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0,
277 	"AH tunnel mode default level.");
278 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn,
279 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0,
280 	"Explicit Congestion Notification handling.");
281 SYSCTL_INT(_net_inet6_ipsec6, OID_AUTO, filtertunnel,
282 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_filtertunnel),  0,
283 	"If set, filter packets from an IPsec tunnel.");
284 SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats,
285     struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics.");
286 #endif /* INET6 */
287 
288 static int ipsec_in_reject(struct secpolicy *, struct inpcb *,
289     const struct mbuf *);
290 
291 #ifdef INET
292 static void ipsec4_get_ulp(const struct mbuf *, struct secpolicyindex *, int);
293 static void ipsec4_setspidx_ipaddr(const struct mbuf *,
294     struct secpolicyindex *);
295 #endif
296 #ifdef INET6
297 static void ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *, int);
298 static void ipsec6_setspidx_ipaddr(const struct mbuf *,
299     struct secpolicyindex *);
300 #endif
301 
302 /*
303  * Return a held reference to the default SP.
304  */
305 static struct secpolicy *
306 key_allocsp_default(void)
307 {
308 
309 	key_addref(V_def_policy);
310 	return (V_def_policy);
311 }
312 
313 static void
314 ipsec_invalidate_cache(struct inpcb *inp, u_int dir)
315 {
316 	struct secpolicy *sp;
317 
318 	INP_WLOCK_ASSERT(inp);
319 	if (dir == IPSEC_DIR_OUTBOUND) {
320 		if (inp->inp_sp->flags & INP_INBOUND_POLICY)
321 			return;
322 		sp = inp->inp_sp->sp_in;
323 		inp->inp_sp->sp_in = NULL;
324 	} else {
325 		if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
326 			return;
327 		sp = inp->inp_sp->sp_out;
328 		inp->inp_sp->sp_out = NULL;
329 	}
330 	if (sp != NULL)
331 		key_freesp(&sp); /* release extra reference */
332 }
333 
334 static void
335 ipsec_cachepolicy(struct inpcb *inp, struct secpolicy *sp, u_int dir)
336 {
337 	uint32_t genid;
338 	int downgrade;
339 
340 	INP_LOCK_ASSERT(inp);
341 
342 	if (dir == IPSEC_DIR_OUTBOUND) {
343 		/* Do we have configured PCB policy? */
344 		if (inp->inp_sp->flags & INP_OUTBOUND_POLICY)
345 			return;
346 		/* Another thread has already set cached policy */
347 		if (inp->inp_sp->sp_out != NULL)
348 			return;
349 		/*
350 		 * Do not cache OUTBOUND policy if PCB isn't connected,
351 		 * i.e. foreign address is INADDR_ANY/UNSPECIFIED.
352 		 */
353 #ifdef INET
354 		if ((inp->inp_vflag & INP_IPV4) != 0 &&
355 		    inp->inp_faddr.s_addr == INADDR_ANY)
356 			return;
357 #endif
358 #ifdef INET6
359 		if ((inp->inp_vflag & INP_IPV6) != 0 &&
360 		    IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
361 			return;
362 #endif
363 	} else {
364 		/* Do we have configured PCB policy? */
365 		if (inp->inp_sp->flags & INP_INBOUND_POLICY)
366 			return;
367 		/* Another thread has already set cached policy */
368 		if (inp->inp_sp->sp_in != NULL)
369 			return;
370 		/*
371 		 * Do not cache INBOUND policy for listen socket,
372 		 * that is bound to INADDR_ANY/UNSPECIFIED address.
373 		 */
374 #ifdef INET
375 		if ((inp->inp_vflag & INP_IPV4) != 0 &&
376 		    inp->inp_faddr.s_addr == INADDR_ANY)
377 			return;
378 #endif
379 #ifdef INET6
380 		if ((inp->inp_vflag & INP_IPV6) != 0 &&
381 		    IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
382 			return;
383 #endif
384 	}
385 	downgrade = 0;
386 	if (!INP_WLOCKED(inp)) {
387 		if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
388 			return;
389 	}
390 	if (dir == IPSEC_DIR_OUTBOUND)
391 		inp->inp_sp->sp_out = sp;
392 	else
393 		inp->inp_sp->sp_in = sp;
394 	/*
395 	 * SP is already referenced by the lookup code.
396 	 * We take extra reference here to avoid race in the
397 	 * ipsec_getpcbpolicy() function - SP will not be freed in the
398 	 * time between we take SP pointer from the cache and key_addref()
399 	 * call.
400 	 */
401 	key_addref(sp);
402 	genid = key_getspgen();
403 	if (genid != inp->inp_sp->genid) {
404 		ipsec_invalidate_cache(inp, dir);
405 		inp->inp_sp->genid = genid;
406 	}
407 	KEYDBG(IPSEC_STAMP,
408 	    printf("%s: PCB(%p): cached %s SP(%p)\n",
409 	    __func__, inp, dir == IPSEC_DIR_OUTBOUND ? "OUTBOUND":
410 	    "INBOUND", sp));
411 	if (downgrade != 0)
412 		INP_DOWNGRADE(inp);
413 }
414 
415 static struct secpolicy *
416 ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error)
417 {
418 
419 	/* Save found OUTBOUND policy into PCB SP cache. */
420 	if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_out == NULL)
421 		ipsec_cachepolicy(inp, sp, IPSEC_DIR_OUTBOUND);
422 
423 	switch (sp->policy) {
424 	default:
425 		printf("%s: invalid policy %u\n", __func__, sp->policy);
426 		/* FALLTHROUGH */
427 	case IPSEC_POLICY_DISCARD:
428 		*error = -EINVAL;	/* Packet is discarded by caller. */
429 		/* FALLTHROUGH */
430 	case IPSEC_POLICY_BYPASS:
431 	case IPSEC_POLICY_NONE:
432 		key_freesp(&sp);
433 		sp = NULL;		/* NB: force NULL result. */
434 		break;
435 	case IPSEC_POLICY_IPSEC:
436 		/* XXXAE: handle LARVAL SP */
437 		break;
438 	}
439 	KEYDBG(IPSEC_DUMP,
440 	    printf("%s: get SP(%p), error %d\n", __func__, sp, *error));
441 	return (sp);
442 }
443 
444 static struct secpolicy *
445 ipsec_getpcbpolicy(struct inpcb *inp, u_int dir)
446 {
447 	struct secpolicy *sp;
448 	int flags, downgrade;
449 
450 	if (inp == NULL || inp->inp_sp == NULL)
451 		return (NULL);
452 
453 	INP_LOCK_ASSERT(inp);
454 
455 	flags = inp->inp_sp->flags;
456 	if (dir == IPSEC_DIR_OUTBOUND) {
457 		sp = inp->inp_sp->sp_out;
458 		flags &= INP_OUTBOUND_POLICY;
459 	} else {
460 		sp = inp->inp_sp->sp_in;
461 		flags &= INP_INBOUND_POLICY;
462 	}
463 	/*
464 	 * Check flags. If we have PCB SP, just return it.
465 	 * Otherwise we need to check that cached SP entry isn't stale.
466 	 */
467 	if (flags == 0) {
468 		if (sp == NULL)
469 			return (NULL);
470 		if (inp->inp_sp->genid != key_getspgen()) {
471 			/* Invalidate the cache. */
472 			downgrade = 0;
473 			if (!INP_WLOCKED(inp)) {
474 				if ((downgrade = INP_TRY_UPGRADE(inp)) == 0)
475 					return (NULL);
476 			}
477 			ipsec_invalidate_cache(inp, IPSEC_DIR_OUTBOUND);
478 			ipsec_invalidate_cache(inp, IPSEC_DIR_INBOUND);
479 			if (downgrade != 0)
480 				INP_DOWNGRADE(inp);
481 			return (NULL);
482 		}
483 		KEYDBG(IPSEC_STAMP,
484 		    printf("%s: PCB(%p): cache hit SP(%p)\n",
485 		    __func__, inp, sp));
486 		/* Return referenced cached policy */
487 	}
488 	key_addref(sp);
489 	return (sp);
490 }
491 
492 #ifdef INET
493 static void
494 ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
495     int needport)
496 {
497 	uint8_t nxt;
498 	int off;
499 
500 	/* Sanity check. */
501 	IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),
502 	    ("packet too short"));
503 
504 	if (m->m_len >= sizeof (struct ip)) {
505 		const struct ip *ip = mtod(m, const struct ip *);
506 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
507 			goto done;
508 		off = ip->ip_hl << 2;
509 		nxt = ip->ip_p;
510 	} else {
511 		struct ip ih;
512 
513 		m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
514 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
515 			goto done;
516 		off = ih.ip_hl << 2;
517 		nxt = ih.ip_p;
518 	}
519 
520 	while (off < m->m_pkthdr.len) {
521 		struct ip6_ext ip6e;
522 		struct tcphdr th;
523 		struct udphdr uh;
524 
525 		switch (nxt) {
526 		case IPPROTO_TCP:
527 			spidx->ul_proto = nxt;
528 			if (!needport)
529 				goto done_proto;
530 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
531 				goto done;
532 			m_copydata(m, off, sizeof (th), (caddr_t) &th);
533 			spidx->src.sin.sin_port = th.th_sport;
534 			spidx->dst.sin.sin_port = th.th_dport;
535 			return;
536 		case IPPROTO_UDP:
537 			spidx->ul_proto = nxt;
538 			if (!needport)
539 				goto done_proto;
540 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
541 				goto done;
542 			m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
543 			spidx->src.sin.sin_port = uh.uh_sport;
544 			spidx->dst.sin.sin_port = uh.uh_dport;
545 			return;
546 		case IPPROTO_AH:
547 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
548 				goto done;
549 			/* XXX Sigh, this works but is totally bogus. */
550 			m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
551 			off += (ip6e.ip6e_len + 2) << 2;
552 			nxt = ip6e.ip6e_nxt;
553 			break;
554 		case IPPROTO_ICMP:
555 		default:
556 			/* XXX Intermediate headers??? */
557 			spidx->ul_proto = nxt;
558 			goto done_proto;
559 		}
560 	}
561 done:
562 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
563 done_proto:
564 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
565 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
566 	KEYDBG(IPSEC_DUMP,
567 	    printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
568 }
569 
570 static void
571 ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
572 {
573 
574 	ipsec4_setsockaddrs(m, &spidx->src, &spidx->dst);
575 	spidx->prefs = sizeof(struct in_addr) << 3;
576 	spidx->prefd = sizeof(struct in_addr) << 3;
577 }
578 
579 static struct secpolicy *
580 ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
581     int needport)
582 {
583 	struct secpolicyindex spidx;
584 	struct secpolicy *sp;
585 
586 	sp = ipsec_getpcbpolicy(inp, dir);
587 	if (sp == NULL && key_havesp(dir)) {
588 		/* Make an index to look for a policy. */
589 		ipsec4_setspidx_ipaddr(m, &spidx);
590 		ipsec4_get_ulp(m, &spidx, needport);
591 		spidx.dir = dir;
592 		sp = key_allocsp(&spidx, dir);
593 	}
594 	if (sp == NULL)		/* No SP found, use system default. */
595 		sp = key_allocsp_default();
596 	return (sp);
597 }
598 
599 /*
600  * Check security policy for *OUTBOUND* IPv4 packet.
601  */
602 struct secpolicy *
603 ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
604     int needport)
605 {
606 	struct secpolicy *sp;
607 
608 	*error = 0;
609 	sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
610 	if (sp != NULL)
611 		sp = ipsec_checkpolicy(sp, inp, error);
612 	if (sp == NULL) {
613 		switch (*error) {
614 		case 0: /* No IPsec required: BYPASS or NONE */
615 			break;
616 		case -EINVAL:
617 			IPSECSTAT_INC(ips_out_polvio);
618 			break;
619 		default:
620 			IPSECSTAT_INC(ips_out_inval);
621 		}
622 	}
623 	KEYDBG(IPSEC_STAMP,
624 	    printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
625 	if (sp != NULL)
626 		KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
627 	return (sp);
628 }
629 
630 /*
631  * Check IPv4 packet against *INBOUND* security policy.
632  * This function is called from tcp_input(), udp_input(),
633  * rip_input() and sctp_input().
634  */
635 int
636 ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp)
637 {
638 	struct secpolicy *sp;
639 	int result;
640 
641 	sp = ipsec4_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
642 	result = ipsec_in_reject(sp, inp, m);
643 	key_freesp(&sp);
644 	if (result != 0)
645 		IPSECSTAT_INC(ips_in_polvio);
646 	return (result);
647 }
648 
649 /*
650  * IPSEC_CAP() method implementation for IPv4.
651  */
652 int
653 ipsec4_capability(struct mbuf *m, u_int cap)
654 {
655 
656 	switch (cap) {
657 	case IPSEC_CAP_BYPASS_FILTER:
658 		/*
659 		 * Bypass packet filtering for packets previously handled
660 		 * by IPsec.
661 		 */
662 		if (!V_ip4_filtertunnel &&
663 		    m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
664 			return (1);
665 		return (0);
666 	case IPSEC_CAP_OPERABLE:
667 		/* Do we have active security policies? */
668 		return (key_havesp_any());
669 	};
670 	return (EOPNOTSUPP);
671 }
672 
673 #endif /* INET */
674 
675 #ifdef INET6
676 static void
677 ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
678     int needport)
679 {
680 	struct tcphdr th;
681 	struct udphdr uh;
682 	struct icmp6_hdr ih;
683 	int off, nxt;
684 
685 	IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip6_hdr),
686 	    ("packet too short"));
687 
688 	/* Set default. */
689 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
690 	spidx->src.sin6.sin6_port = IPSEC_PORT_ANY;
691 	spidx->dst.sin6.sin6_port = IPSEC_PORT_ANY;
692 
693 	nxt = -1;
694 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
695 	if (off < 0 || m->m_pkthdr.len < off)
696 		return;
697 
698 	switch (nxt) {
699 	case IPPROTO_TCP:
700 		spidx->ul_proto = nxt;
701 		if (!needport)
702 			break;
703 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
704 			break;
705 		m_copydata(m, off, sizeof(th), (caddr_t)&th);
706 		spidx->src.sin6.sin6_port = th.th_sport;
707 		spidx->dst.sin6.sin6_port = th.th_dport;
708 		break;
709 	case IPPROTO_UDP:
710 		spidx->ul_proto = nxt;
711 		if (!needport)
712 			break;
713 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
714 			break;
715 		m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
716 		spidx->src.sin6.sin6_port = uh.uh_sport;
717 		spidx->dst.sin6.sin6_port = uh.uh_dport;
718 		break;
719 	case IPPROTO_ICMPV6:
720 		spidx->ul_proto = nxt;
721 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
722 			break;
723 		m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
724 		spidx->src.sin6.sin6_port = htons((uint16_t)ih.icmp6_type);
725 		spidx->dst.sin6.sin6_port = htons((uint16_t)ih.icmp6_code);
726 		break;
727 	default:
728 		/* XXX Intermediate headers??? */
729 		spidx->ul_proto = nxt;
730 		break;
731 	}
732 	KEYDBG(IPSEC_DUMP,
733 	    printf("%s: ", __func__); kdebug_secpolicyindex(spidx, NULL));
734 }
735 
736 static void
737 ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
738 {
739 
740 	ipsec6_setsockaddrs(m, &spidx->src, &spidx->dst);
741 	spidx->prefs = sizeof(struct in6_addr) << 3;
742 	spidx->prefd = sizeof(struct in6_addr) << 3;
743 }
744 
745 static struct secpolicy *
746 ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir,
747     int needport)
748 {
749 	struct secpolicyindex spidx;
750 	struct secpolicy *sp;
751 
752 	sp = ipsec_getpcbpolicy(inp, dir);
753 	if (sp == NULL && key_havesp(dir)) {
754 		/* Make an index to look for a policy. */
755 		ipsec6_setspidx_ipaddr(m, &spidx);
756 		ipsec6_get_ulp(m, &spidx, needport);
757 		spidx.dir = dir;
758 		sp = key_allocsp(&spidx, dir);
759 	}
760 	if (sp == NULL)		/* No SP found, use system default. */
761 		sp = key_allocsp_default();
762 	return (sp);
763 }
764 
765 /*
766  * Check security policy for *OUTBOUND* IPv6 packet.
767  */
768 struct secpolicy *
769 ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error,
770     int needport)
771 {
772 	struct secpolicy *sp;
773 
774 	*error = 0;
775 	sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport);
776 	if (sp != NULL)
777 		sp = ipsec_checkpolicy(sp, inp, error);
778 	if (sp == NULL) {
779 		switch (*error) {
780 		case 0: /* No IPsec required: BYPASS or NONE */
781 			break;
782 		case -EINVAL:
783 			IPSEC6STAT_INC(ips_out_polvio);
784 			break;
785 		default:
786 			IPSEC6STAT_INC(ips_out_inval);
787 		}
788 	}
789 	KEYDBG(IPSEC_STAMP,
790 	    printf("%s: using SP(%p), error %d\n", __func__, sp, *error));
791 	if (sp != NULL)
792 		KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
793 	return (sp);
794 }
795 
796 /*
797  * Check IPv6 packet against inbound security policy.
798  * This function is called from tcp6_input(), udp6_input(),
799  * rip6_input() and sctp_input().
800  */
801 int
802 ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
803 {
804 	struct secpolicy *sp;
805 	int result;
806 
807 	sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0);
808 	result = ipsec_in_reject(sp, inp, m);
809 	key_freesp(&sp);
810 	if (result)
811 		IPSEC6STAT_INC(ips_in_polvio);
812 	return (result);
813 }
814 
815 /*
816  * IPSEC_CAP() method implementation for IPv6.
817  */
818 int
819 ipsec6_capability(struct mbuf *m, u_int cap)
820 {
821 
822 	switch (cap) {
823 	case IPSEC_CAP_BYPASS_FILTER:
824 		/*
825 		 * Bypass packet filtering for packets previously handled
826 		 * by IPsec.
827 		 */
828 		if (!V_ip6_filtertunnel &&
829 		    m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
830 			return (1);
831 		return (0);
832 	case IPSEC_CAP_OPERABLE:
833 		/* Do we have active security policies? */
834 		return (key_havesp_any());
835 	};
836 	return (EOPNOTSUPP);
837 }
838 #endif /* INET6 */
839 
840 int
841 ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type)
842 {
843 	int idx;
844 
845 	switch (ctx->af) {
846 #ifdef INET
847 	case AF_INET:
848 		idx = HHOOK_IPSEC_INET;
849 		break;
850 #endif
851 #ifdef INET6
852 	case AF_INET6:
853 		idx = HHOOK_IPSEC_INET6;
854 		break;
855 #endif
856 	default:
857 		return (EPFNOSUPPORT);
858 	}
859 	if (type == HHOOK_TYPE_IPSEC_IN)
860 		HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL);
861 	else
862 		HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL);
863 	if (*ctx->mp == NULL)
864 		return (EACCES);
865 	return (0);
866 }
867 
868 /*
869  * Return current level.
870  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
871  */
872 u_int
873 ipsec_get_reqlevel(struct secpolicy *sp, u_int idx)
874 {
875 	struct ipsecrequest *isr;
876 	u_int esp_trans_deflev, esp_net_deflev;
877 	u_int ah_trans_deflev, ah_net_deflev;
878 	u_int level = 0;
879 
880 	IPSEC_ASSERT(idx < sp->tcount, ("Wrong IPsec request index %d", idx));
881 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
882 #define IPSEC_CHECK_DEFAULT(lev) \
883 	(((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE &&	\
884 	  (lev) != IPSEC_LEVEL_UNIQUE)					\
885 		? (V_ipsec_debug  ?					\
886 		log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
887 		(lev), IPSEC_LEVEL_REQUIRE) : 0),			\
888 		(lev) = IPSEC_LEVEL_REQUIRE, (lev) : (lev))
889 
890 	/*
891 	 * IPsec VTI uses unique security policy with fake spidx filled
892 	 * with zeroes. Just return IPSEC_LEVEL_REQUIRE instead of doing
893 	 * full level lookup for such policies.
894 	 */
895 	if (sp->state == IPSEC_SPSTATE_IFNET) {
896 		IPSEC_ASSERT(sp->req[idx]->level == IPSEC_LEVEL_UNIQUE,
897 		    ("Wrong IPsec request level %d", sp->req[idx]->level));
898 		return (IPSEC_LEVEL_REQUIRE);
899 	}
900 
901 	/* Set default level. */
902 	switch (sp->spidx.src.sa.sa_family) {
903 #ifdef INET
904 	case AF_INET:
905 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
906 		esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
907 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
908 		ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
909 		break;
910 #endif
911 #ifdef INET6
912 	case AF_INET6:
913 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
914 		esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
915 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
916 		ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
917 		break;
918 #endif /* INET6 */
919 	default:
920 		panic("%s: unknown af %u",
921 			__func__, sp->spidx.src.sa.sa_family);
922 	}
923 
924 #undef IPSEC_CHECK_DEFAULT
925 
926 	isr = sp->req[idx];
927 	/* Set level. */
928 	switch (isr->level) {
929 	case IPSEC_LEVEL_DEFAULT:
930 		switch (isr->saidx.proto) {
931 		case IPPROTO_ESP:
932 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
933 				level = esp_net_deflev;
934 			else
935 				level = esp_trans_deflev;
936 			break;
937 		case IPPROTO_AH:
938 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
939 				level = ah_net_deflev;
940 			else
941 				level = ah_trans_deflev;
942 			break;
943 		case IPPROTO_IPCOMP:
944 			/*
945 			 * We don't really care, as IPcomp document says that
946 			 * we shouldn't compress small packets.
947 			 */
948 			level = IPSEC_LEVEL_USE;
949 			break;
950 		default:
951 			panic("%s: Illegal protocol defined %u\n", __func__,
952 				isr->saidx.proto);
953 		}
954 		break;
955 
956 	case IPSEC_LEVEL_USE:
957 	case IPSEC_LEVEL_REQUIRE:
958 		level = isr->level;
959 		break;
960 	case IPSEC_LEVEL_UNIQUE:
961 		level = IPSEC_LEVEL_REQUIRE;
962 		break;
963 
964 	default:
965 		panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
966 	}
967 
968 	return (level);
969 }
970 
971 static int
972 ipsec_check_history(const struct mbuf *m, struct secpolicy *sp, u_int idx)
973 {
974 	struct xform_history *xh;
975 	struct m_tag *mtag;
976 
977 	mtag = NULL;
978 	while ((mtag = m_tag_find(__DECONST(struct mbuf *, m),
979 	    PACKET_TAG_IPSEC_IN_DONE, mtag)) != NULL) {
980 		xh = (struct xform_history *)(mtag + 1);
981 		KEYDBG(IPSEC_DATA,
982 		    char buf[IPSEC_ADDRSTRLEN];
983 		    printf("%s: mode %s proto %u dst %s\n", __func__,
984 			kdebug_secasindex_mode(xh->mode), xh->proto,
985 			ipsec_address(&xh->dst, buf, sizeof(buf))));
986 		if (xh->proto != sp->req[idx]->saidx.proto)
987 			continue;
988 		/* If SA had IPSEC_MODE_ANY, consider this as match. */
989 		if (xh->mode != sp->req[idx]->saidx.mode &&
990 		    xh->mode != IPSEC_MODE_ANY)
991 			continue;
992 		/*
993 		 * For transport mode IPsec request doesn't contain
994 		 * addresses. We need to use address from spidx.
995 		 */
996 		if (sp->req[idx]->saidx.mode == IPSEC_MODE_TRANSPORT) {
997 			if (key_sockaddrcmp_withmask(&xh->dst.sa,
998 			    &sp->spidx.dst.sa, sp->spidx.prefd) != 0)
999 				continue;
1000 		} else {
1001 			if (key_sockaddrcmp(&xh->dst.sa,
1002 			    &sp->req[idx]->saidx.dst.sa, 0) != 0)
1003 				continue;
1004 		}
1005 		return (0); /* matched */
1006 	}
1007 	return (1);
1008 }
1009 
1010 /*
1011  * Check security policy requirements against the actual
1012  * packet contents.  Return one if the packet should be
1013  * rejected as "invalid"; otherwise return zero to have the
1014  * packet treated as "valid".
1015  *
1016  * OUT:
1017  *	0: valid
1018  *	1: invalid
1019  */
1020 static int
1021 ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m)
1022 {
1023 	int i;
1024 
1025 	KEYDBG(IPSEC_STAMP,
1026 	    printf("%s: PCB(%p): using SP(%p)\n", __func__, inp, sp));
1027 	KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1028 
1029 	if (inp != NULL && inp->inp_sp != NULL && inp->inp_sp->sp_in == NULL)
1030 		ipsec_cachepolicy(inp, sp, IPSEC_DIR_INBOUND);
1031 
1032 	/* Check policy. */
1033 	switch (sp->policy) {
1034 	case IPSEC_POLICY_DISCARD:
1035 		return (1);
1036 	case IPSEC_POLICY_BYPASS:
1037 	case IPSEC_POLICY_NONE:
1038 		return (0);
1039 	}
1040 
1041 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1042 		("invalid policy %u", sp->policy));
1043 
1044 	/*
1045 	 * ipsec[46]_common_input_cb after each transform adds
1046 	 * PACKET_TAG_IPSEC_IN_DONE mbuf tag. It contains SPI, proto, mode
1047 	 * and destination address from saidx. We can compare info from
1048 	 * these tags with requirements in SP.
1049 	 */
1050 	for (i = 0; i < sp->tcount; i++) {
1051 		/*
1052 		 * Do not check IPcomp, since IPcomp document
1053 		 * says that we shouldn't compress small packets.
1054 		 * IPComp policy should always be treated as being
1055 		 * in "use" level.
1056 		 */
1057 		if (sp->req[i]->saidx.proto == IPPROTO_IPCOMP ||
1058 		    ipsec_get_reqlevel(sp, i) != IPSEC_LEVEL_REQUIRE)
1059 			continue;
1060 		if (V_check_policy_history != 0 &&
1061 		    ipsec_check_history(m, sp, i) != 0)
1062 			return (1);
1063 		else switch (sp->req[i]->saidx.proto) {
1064 		case IPPROTO_ESP:
1065 			if ((m->m_flags & M_DECRYPTED) == 0) {
1066 				KEYDBG(IPSEC_DUMP,
1067 				    printf("%s: ESP m_flags:%x\n", __func__,
1068 					    m->m_flags));
1069 				return (1);
1070 			}
1071 			break;
1072 		case IPPROTO_AH:
1073 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
1074 				KEYDBG(IPSEC_DUMP,
1075 				    printf("%s: AH m_flags:%x\n", __func__,
1076 					    m->m_flags));
1077 				return (1);
1078 			}
1079 			break;
1080 		}
1081 	}
1082 	return (0);		/* Valid. */
1083 }
1084 
1085 /*
1086  * Compute the byte size to be occupied by IPsec header.
1087  * In case it is tunnelled, it includes the size of outer IP header.
1088  */
1089 size_t
1090 ipsec_hdrsiz_internal(struct secpolicy *sp)
1091 {
1092 	size_t size;
1093 	int i;
1094 
1095 	KEYDBG(IPSEC_STAMP, printf("%s: using SP(%p)\n", __func__, sp));
1096 	KEYDBG(IPSEC_DATA, kdebug_secpolicy(sp));
1097 
1098 	switch (sp->policy) {
1099 	case IPSEC_POLICY_DISCARD:
1100 	case IPSEC_POLICY_BYPASS:
1101 	case IPSEC_POLICY_NONE:
1102 		return (0);
1103 	}
1104 
1105 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1106 		("invalid policy %u", sp->policy));
1107 
1108 	/*
1109 	 * XXX: for each transform we need to lookup suitable SA
1110 	 * and use info from SA to calculate headers size.
1111 	 * XXX: for NAT-T we need to cosider UDP header size.
1112 	 */
1113 	size = 0;
1114 	for (i = 0; i < sp->tcount; i++) {
1115 		switch (sp->req[i]->saidx.proto) {
1116 		case IPPROTO_ESP:
1117 			size += esp_hdrsiz(NULL);
1118 			break;
1119 		case IPPROTO_AH:
1120 			size += ah_hdrsiz(NULL);
1121 			break;
1122 		case IPPROTO_IPCOMP:
1123 			size += sizeof(struct ipcomp);
1124 			break;
1125 		}
1126 
1127 		if (sp->req[i]->saidx.mode == IPSEC_MODE_TUNNEL) {
1128 			switch (sp->req[i]->saidx.dst.sa.sa_family) {
1129 #ifdef INET
1130 			case AF_INET:
1131 				size += sizeof(struct ip);
1132 				break;
1133 #endif
1134 #ifdef INET6
1135 			case AF_INET6:
1136 				size += sizeof(struct ip6_hdr);
1137 				break;
1138 #endif
1139 			default:
1140 				ipseclog((LOG_ERR, "%s: unknown AF %d in "
1141 				    "IPsec tunnel SA\n", __func__,
1142 				    sp->req[i]->saidx.dst.sa.sa_family));
1143 				break;
1144 			}
1145 		}
1146 	}
1147 	return (size);
1148 }
1149 
1150 /*
1151  * Compute ESP/AH header size for protocols with PCB, including
1152  * outer IP header. Currently only tcp_output() uses it.
1153  */
1154 size_t
1155 ipsec_hdrsiz_inpcb(struct inpcb *inp)
1156 {
1157 	struct secpolicyindex spidx;
1158 	struct secpolicy *sp;
1159 	size_t sz;
1160 
1161 	sp = ipsec_getpcbpolicy(inp, IPSEC_DIR_OUTBOUND);
1162 	if (sp == NULL && key_havesp(IPSEC_DIR_OUTBOUND)) {
1163 		ipsec_setspidx_inpcb(inp, &spidx, IPSEC_DIR_OUTBOUND);
1164 		sp = key_allocsp(&spidx, IPSEC_DIR_OUTBOUND);
1165 	}
1166 	if (sp == NULL)
1167 		sp = key_allocsp_default();
1168 	sz = ipsec_hdrsiz_internal(sp);
1169 	key_freesp(&sp);
1170 	return (sz);
1171 }
1172 
1173 
1174 #define IPSEC_BITMAP_INDEX_MASK(w)	(w - 1)
1175 #define IPSEC_REDUNDANT_BIT_SHIFTS	5
1176 #define IPSEC_REDUNDANT_BITS		(1 << IPSEC_REDUNDANT_BIT_SHIFTS)
1177 #define IPSEC_BITMAP_LOC_MASK		(IPSEC_REDUNDANT_BITS - 1)
1178 
1179 /*
1180  * Functions below are responsible for checking and updating bitmap.
1181  * These are used to separate ipsec_chkreplay() and ipsec_updatereplay()
1182  * from window implementation
1183  *
1184  * Based on RFC 6479. Blocks are 32 bits unsigned integers
1185  */
1186 
1187 static inline int
1188 check_window(const struct secreplay *replay, uint64_t seq)
1189 {
1190 	int index, bit_location;
1191 
1192 	SECREPLAY_ASSERT(replay);
1193 
1194 	bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1195 	index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS)
1196 		& IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1197 
1198 	/* This packet already seen? */
1199 	return ((replay->bitmap)[index] & (1 << bit_location));
1200 }
1201 
1202 static inline void
1203 advance_window(const struct secreplay *replay, uint64_t seq)
1204 {
1205 	int i;
1206 	uint64_t index, index_cur, diff;
1207 
1208 	SECREPLAY_ASSERT(replay);
1209 
1210 	index_cur = replay->last >> IPSEC_REDUNDANT_BIT_SHIFTS;
1211 	index = seq >> IPSEC_REDUNDANT_BIT_SHIFTS;
1212 	diff = index - index_cur;
1213 
1214 	if (diff > replay->bitmap_size) {
1215 		/* something unusual in this case */
1216 		diff = replay->bitmap_size;
1217 	}
1218 
1219 	for (i = 0; i < diff; i++) {
1220 		replay->bitmap[(i + index_cur + 1)
1221 		& IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0;
1222 	}
1223 }
1224 
1225 static inline void
1226 set_window(const struct secreplay *replay, uint64_t seq)
1227 {
1228 	int index, bit_location;
1229 
1230 	SECREPLAY_ASSERT(replay);
1231 
1232 	bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1233 	index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS)
1234 		& IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1235 
1236 	replay->bitmap[index] |= (1 << bit_location);
1237 }
1238 
1239 /*
1240  * Check the variable replay window.
1241  * ipsec_chkreplay() performs replay check before ICV verification.
1242  * ipsec_updatereplay() updates replay bitmap.  This must be called after
1243  * ICV verification (it also performs replay check, which is usually done
1244  * beforehand).
1245  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1246  *
1247  * Based on RFC 4303
1248  */
1249 
1250 int
1251 ipsec_chkreplay(uint32_t seq, uint32_t *seqhigh, struct secasvar *sav)
1252 {
1253 	char buf[128];
1254 	struct secreplay *replay;
1255 	uint32_t window;
1256 	uint32_t tl, th, bl;
1257 	uint32_t seqh;
1258 
1259 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
1260 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1261 
1262 	replay = sav->replay;
1263 
1264 	/* No need to check replay if disabled. */
1265 	if (replay->wsize == 0) {
1266 		return (1);
1267 	}
1268 
1269 	SECREPLAY_LOCK(replay);
1270 
1271 	/* Zero sequence number is not allowed. */
1272 	if (seq == 0 && replay->last == 0) {
1273 		SECREPLAY_UNLOCK(replay);
1274 		return (0);
1275 	}
1276 
1277 	window = replay->wsize << 3;		/* Size of window */
1278 	tl = (uint32_t)replay->last;		/* Top of window, lower part */
1279 	th = (uint32_t)(replay->last >> 32);	/* Top of window, high part */
1280 	bl = tl - window + 1;			/* Bottom of window, lower part */
1281 
1282 	/*
1283 	 * We keep the high part intact when:
1284 	 * 1) the seq is within [bl, 0xffffffff] and the whole window is
1285 	 *    within one subspace;
1286 	 * 2) the seq is within [0, bl) and window spans two subspaces.
1287 	 */
1288 	if ((tl >= window - 1 && seq >= bl) ||
1289 	    (tl < window - 1 && seq < bl)) {
1290 		*seqhigh = th;
1291 		if (seq <= tl) {
1292 			/* Sequence number inside window - check against replay */
1293 			if (check_window(replay, seq)) {
1294 				SECREPLAY_UNLOCK(replay);
1295 				return (0);
1296 			}
1297 		}
1298 
1299 		SECREPLAY_UNLOCK(replay);
1300 		/* Sequence number above top of window or not found in bitmap */
1301 		return (1);
1302 	}
1303 
1304 	/*
1305 	 * If ESN is not enabled and packet with highest sequence number
1306 	 * was received we should report overflow
1307 	 */
1308 	if (tl == 0xffffffff && !(sav->flags & SADB_X_SAFLAGS_ESN)) {
1309 		/* Set overflow flag. */
1310 		replay->overflow++;
1311 
1312 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1313 			if (sav->sah->saidx.proto == IPPROTO_ESP)
1314 				ESPSTAT_INC(esps_wrap);
1315 			else if (sav->sah->saidx.proto == IPPROTO_AH)
1316 				AHSTAT_INC(ahs_wrap);
1317 			SECREPLAY_UNLOCK(replay);
1318 			return (0);
1319 		}
1320 
1321 		ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1322 		    __func__, replay->overflow,
1323 		    ipsec_sa2str(sav, buf, sizeof(buf))));
1324 	}
1325 
1326 	/*
1327 	 * Seq is within [bl, 0xffffffff] and bl is within
1328 	 * [0xffffffff-window, 0xffffffff].  This means we got a seq
1329 	 * which is within our replay window, but in the previous
1330 	 * subspace.
1331 	 */
1332 	if (tl < window - 1 && seq >= bl) {
1333 		if (th == 0)
1334 			return (0);
1335 		*seqhigh = th - 1;
1336 		seqh = th - 1;
1337 		if (check_window(replay, seq)) {
1338 			SECREPLAY_UNLOCK(replay);
1339 			return (0);
1340 		}
1341 		SECREPLAY_UNLOCK(replay);
1342 		return (1);
1343 	}
1344 
1345 	/*
1346 	 * Seq is within [0, bl) but the whole window is within one subspace.
1347 	 * This means that seq has wrapped and is in next subspace
1348 	 */
1349 	*seqhigh = th + 1;
1350 	seqh = th + 1;
1351 
1352 	/* Don't let high part wrap. */
1353 	if (seqh == 0) {
1354 		/* Set overflow flag. */
1355 		replay->overflow++;
1356 
1357 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1358 			if (sav->sah->saidx.proto == IPPROTO_ESP)
1359 				ESPSTAT_INC(esps_wrap);
1360 			else if (sav->sah->saidx.proto == IPPROTO_AH)
1361 				AHSTAT_INC(ahs_wrap);
1362 			SECREPLAY_UNLOCK(replay);
1363 			return (0);
1364 		}
1365 
1366 		ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1367 		    __func__, replay->overflow,
1368 		    ipsec_sa2str(sav, buf, sizeof(buf))));
1369 	}
1370 
1371 	SECREPLAY_UNLOCK(replay);
1372 	return (1);
1373 }
1374 
1375 /*
1376  * Check replay counter whether to update or not.
1377  * OUT:	0:	OK
1378  *	1:	NG
1379  */
1380 int
1381 ipsec_updatereplay(uint32_t seq, struct secasvar *sav)
1382 {
1383 	struct secreplay *replay;
1384 	uint32_t window;
1385 	uint32_t tl, th, bl;
1386 	uint32_t seqh;
1387 
1388 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
1389 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1390 
1391 	replay = sav->replay;
1392 
1393 	/* No need to check replay if disabled. */
1394 	if (replay->wsize == 0)
1395 		return (0);
1396 
1397 	SECREPLAY_LOCK(replay);
1398 
1399 	/* Zero sequence number is not allowed. */
1400 	if (seq == 0 && replay->last == 0) {
1401 		SECREPLAY_UNLOCK(replay);
1402 		return (1);
1403 	}
1404 
1405 	window = replay->wsize << 3;		/* Size of window */
1406 	tl = (uint32_t)replay->last;		/* Top of window, lower part */
1407 	th = (uint32_t)(replay->last >> 32);	/* Top of window, high part */
1408 	bl = tl - window + 1;			/* Bottom of window, lower part */
1409 
1410 	/*
1411 	 * We keep the high part intact when:
1412 	 * 1) the seq is within [bl, 0xffffffff] and the whole window is
1413 	 *    within one subspace;
1414 	 * 2) the seq is within [0, bl) and window spans two subspaces.
1415 	 */
1416 	if ((tl >= window - 1 && seq >= bl) ||
1417 	    (tl < window - 1 && seq < bl)) {
1418 		seqh = th;
1419 		if (seq <= tl) {
1420 			/* Sequence number inside window - check against replay */
1421 			if (check_window(replay, seq)) {
1422 				SECREPLAY_UNLOCK(replay);
1423 				return (1);
1424 			}
1425 			set_window(replay, seq);
1426 		} else {
1427 			advance_window(replay, ((uint64_t)seqh << 32) | seq);
1428 			set_window(replay, seq);
1429 			replay->last = ((uint64_t)seqh << 32) | seq;
1430 		}
1431 
1432 		/* Sequence number above top of window or not found in bitmap */
1433 		replay->count++;
1434 		SECREPLAY_UNLOCK(replay);
1435 		return (0);
1436 	}
1437 
1438 	if (!(sav->flags & SADB_X_SAFLAGS_ESN)) {
1439 		SECREPLAY_UNLOCK(replay);
1440 		return (1);
1441 	}
1442 
1443 	/*
1444 	 * Seq is within [bl, 0xffffffff] and bl is within
1445 	 * [0xffffffff-window, 0xffffffff].  This means we got a seq
1446 	 * which is within our replay window, but in the previous
1447 	 * subspace.
1448 	 */
1449 	if (tl < window - 1 && seq >= bl) {
1450 		if (th == 0) {
1451 			SECREPLAY_UNLOCK(replay);
1452 			return (1);
1453 		}
1454 		if (check_window(replay, seq)) {
1455 			SECREPLAY_UNLOCK(replay);
1456 			return (1);
1457 		}
1458 
1459 		set_window(replay, seq);
1460 		replay->count++;
1461 		SECREPLAY_UNLOCK(replay);
1462 		return (0);
1463 	}
1464 
1465 	/*
1466 	 * Seq is within [0, bl) but the whole window is within one subspace.
1467 	 * This means that seq has wrapped and is in next subspace
1468 	 */
1469 	seqh = th + 1;
1470 
1471 	/* Don't let high part wrap. */
1472 	if (seqh == 0) {
1473 		SECREPLAY_UNLOCK(replay);
1474 		return (1);
1475 	}
1476 
1477 	advance_window(replay, ((uint64_t)seqh << 32) | seq);
1478 	set_window(replay, seq);
1479 	replay->last = ((uint64_t)seqh << 32) | seq;
1480 	replay->count++;
1481 
1482 	SECREPLAY_UNLOCK(replay);
1483 	return (0);
1484 }
1485 int
1486 ipsec_updateid(struct secasvar *sav, crypto_session_t *new,
1487     crypto_session_t *old)
1488 {
1489 	crypto_session_t tmp;
1490 
1491 	/*
1492 	 * tdb_cryptoid is initialized by xform_init().
1493 	 * Then it can be changed only when some crypto error occurred or
1494 	 * when SA is deleted. We stored used cryptoid in the xform_data
1495 	 * structure. In case when crypto error occurred and crypto
1496 	 * subsystem has reinited the session, it returns new cryptoid
1497 	 * and EAGAIN error code.
1498 	 *
1499 	 * This function will be called when we got EAGAIN from crypto
1500 	 * subsystem.
1501 	 * *new is cryptoid that was returned by crypto subsystem in
1502 	 * the crp_sid.
1503 	 * *old is the original cryptoid that we stored in xform_data.
1504 	 *
1505 	 * For first failed request *old == sav->tdb_cryptoid, then
1506 	 * we update sav->tdb_cryptoid and redo crypto_dispatch().
1507 	 * For next failed request *old != sav->tdb_cryptoid, then
1508 	 * we store cryptoid from first request into the *new variable
1509 	 * and crp_sid from this second session will be returned via
1510 	 * *old pointer, so caller can release second session.
1511 	 *
1512 	 * XXXAE: check this more carefully.
1513 	 */
1514 	KEYDBG(IPSEC_STAMP,
1515 	    printf("%s: SA(%p) moves cryptoid %p -> %p\n",
1516 		__func__, sav, *old, *new));
1517 	KEYDBG(IPSEC_DATA, kdebug_secasv(sav));
1518 	SECASVAR_WLOCK(sav);
1519 	if (sav->tdb_cryptoid != *old) {
1520 		/* cryptoid was already updated */
1521 		tmp = *new;
1522 		*new = sav->tdb_cryptoid;
1523 		*old = tmp;
1524 		SECASVAR_WUNLOCK(sav);
1525 		return (1);
1526 	}
1527 	sav->tdb_cryptoid = *new;
1528 	SECASVAR_WUNLOCK(sav);
1529 	return (0);
1530 }
1531 
1532 int
1533 ipsec_initialized(void)
1534 {
1535 
1536 	return (V_def_policy != NULL);
1537 }
1538 
1539 static void
1540 def_policy_init(const void *unused __unused)
1541 {
1542 
1543 	V_def_policy = key_newsp();
1544 	if (V_def_policy != NULL) {
1545 		V_def_policy->policy = IPSEC_POLICY_NONE;
1546 		/* Force INPCB SP cache invalidation */
1547 		key_bumpspgen();
1548 	} else
1549 		printf("%s: failed to initialize default policy\n", __func__);
1550 }
1551 
1552 static void
1553 def_policy_uninit(const void *unused __unused)
1554 {
1555 
1556 	if (V_def_policy != NULL) {
1557 		key_freesp(&V_def_policy);
1558 		key_bumpspgen();
1559 	}
1560 }
1561 
1562 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1563     def_policy_init, NULL);
1564 VNET_SYSUNINIT(def_policy_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1565     def_policy_uninit, NULL);
1566