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