xref: /netbsd-src/sys/netipsec/ipsec.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: ipsec.c,v 1.123 2017/11/21 07:03:08 ozaki-r Exp $	*/
2 /*	$FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $	*/
3 /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $	*/
4 
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 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.123 2017/11/21 07:03:08 ozaki-r Exp $");
36 
37 /*
38  * IPsec controller part.
39  */
40 
41 #if defined(_KERNEL_OPT)
42 #include "opt_inet.h"
43 #include "opt_ipsec.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/errno.h>
54 #include <sys/time.h>
55 #include <sys/kernel.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/proc.h>
59 #include <sys/kauth.h>
60 #include <sys/cpu.h>
61 #include <sys/kmem.h>
62 #include <sys/pserialize.h>
63 
64 #include <net/if.h>
65 #include <net/route.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/in_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include <netinet/ip_private.h>
78 
79 #include <netinet/ip6.h>
80 #ifdef INET6
81 #include <netinet6/ip6_var.h>
82 #endif
83 #include <netinet/in_pcb.h>
84 #ifdef INET6
85 #include <netinet6/in6_pcb.h>
86 #include <netinet/icmp6.h>
87 #endif
88 
89 #include <netipsec/ipsec.h>
90 #include <netipsec/ipsec_var.h>
91 #include <netipsec/ipsec_private.h>
92 #ifdef INET6
93 #include <netipsec/ipsec6.h>
94 #endif
95 #include <netipsec/ah_var.h>
96 #include <netipsec/esp_var.h>
97 #include <netipsec/ipcomp.h>		/*XXX*/
98 #include <netipsec/ipcomp_var.h>
99 
100 #include <netipsec/key.h>
101 #include <netipsec/keydb.h>
102 #include <netipsec/key_debug.h>
103 
104 #include <netipsec/xform.h>
105 
106 #include <net/net_osdep.h>
107 
108 int ipsec_used = 0;
109 int ipsec_enabled = 1;
110 
111 #ifdef IPSEC_DEBUG
112 int ipsec_debug = 1;
113 
114 /*
115  * When set to 1, IPsec will send packets with the same sequence number.
116  * This allows to verify if the other side has proper replay attacks detection.
117  */
118 int ipsec_replay = 0;
119 
120 /*
121  * When set 1, IPsec will send packets with corrupted HMAC.
122  * This allows to verify if the other side properly detects modified packets.
123  */
124 int ipsec_integrity = 0;
125 #else
126 int ipsec_debug = 0;
127 #endif
128 
129 percpu_t *ipsecstat_percpu;
130 int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
131 int ip4_ipsec_dfbit = 2;	/* DF bit on encap. 0: clear 1: set 2: copy */
132 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
133 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
134 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
135 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
136 struct secpolicy ip4_def_policy;
137 int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
138 int ip4_esp_randpad = -1;
139 
140 u_int ipsec_spdgen = 1;		/* SPD generation # */
141 
142 static struct secpolicy ipsec_dummy_sp __read_mostly = {
143 	.state		= IPSEC_SPSTATE_ALIVE,
144 	/* If ENTRUST, the dummy SP never be used. See ipsec_getpolicybysock. */
145 	.policy		= IPSEC_POLICY_ENTRUST,
146 };
147 
148 static struct secpolicy *ipsec_checkpcbcache (struct mbuf *,
149 	struct inpcbpolicy *, int);
150 static int ipsec_fillpcbcache (struct inpcbpolicy *, struct mbuf *,
151 	struct secpolicy *, int);
152 static int ipsec_invalpcbcache (struct inpcbpolicy *, int);
153 
154 /*
155  * Crypto support requirements:
156  *
157  *  1	require hardware support
158  * -1	require software support
159  *  0	take anything
160  */
161 int	crypto_support = 0;
162 
163 static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
164     struct inpcb_hdr *, int *);
165 
166 #ifdef __FreeBSD__
167 /* net.inet.ipsec */
168 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
169 	esp_randpad, CTLFLAG_RW,	&ip4_esp_randpad,	0, "");
170 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
171 	crypto_support,	CTLFLAG_RW,	&crypto_support,0, "");
172 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
173 	"Emulate replay attack");
174 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
175 	&ipsec_integrity, 0, "Emulate man-in-the-middle attack");
176 #endif /* __FreeBSD__ */
177 
178 #ifdef INET6
179 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
180 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
181 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
182 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
183 struct secpolicy ip6_def_policy;
184 int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
185 int ip6_esp_randpad = -1;
186 
187 
188 #ifdef __FreeBSD__
189 /* net.inet6.ipsec6 */
190 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
191 	esp_randpad, CTLFLAG_RW,	&ip6_esp_randpad,	0, "");
192 #endif /* __FreeBSD__ */
193 #endif /* INET6 */
194 
195 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *);
196 #ifdef INET6
197 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *);
198 #endif
199 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
200 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
201 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
202 #ifdef INET6
203 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
204 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
205 #endif
206 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
207 #if 0 /* unused */
208 static struct secpolicy *ipsec_deepcopy_policy (const struct secpolicy *);
209 #endif
210 static int ipsec_set_policy (struct secpolicy **, int, const void *, size_t,
211     kauth_cred_t);
212 static int ipsec_get_policy (struct secpolicy *, struct mbuf **);
213 static void ipsec_destroy_policy(struct secpolicy *);
214 static void vshiftl (unsigned char *, int, int);
215 static size_t ipsec_hdrsiz(const struct secpolicy *, const struct mbuf *);
216 
217 /*
218  * Try to validate and use cached policy on a PCB.
219  */
220 static struct secpolicy *
221 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
222 {
223 	struct secpolicyindex spidx;
224 	struct secpolicy *sp = NULL;
225 	int s;
226 
227 	KASSERT(IPSEC_DIR_IS_VALID(dir));
228 	KASSERT(pcbsp != NULL);
229 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
230 	KASSERT(inph_locked(pcbsp->sp_inph));
231 
232 	/*
233 	 * Checking the generation and sp->state and taking a reference to an SP
234 	 * must be in a critical section of pserialize. See key_unlink_sp.
235 	 */
236 	s = pserialize_read_enter();
237 	/* SPD table change invalidate all the caches. */
238 	if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
239 		ipsec_invalpcbcache(pcbsp, dir);
240 		goto out;
241 	}
242 	sp = pcbsp->sp_cache[dir].cachesp;
243 	if (sp == NULL)
244 		goto out;
245 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
246 		sp = NULL;
247 		ipsec_invalpcbcache(pcbsp, dir);
248 		goto out;
249 	}
250 	if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
251 		/* NB: assume ipsec_setspidx never sleep */
252 		if (ipsec_setspidx(m, &spidx, 1) != 0) {
253 			sp = NULL;
254 			goto out;
255 		}
256 
257 		/*
258 		 * We have to make an exact match here since the cached rule
259 		 * might have lower priority than a rule that would otherwise
260 		 * have matched the packet.
261 		 */
262 		if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
263 		    sizeof(spidx))) {
264 			sp = NULL;
265 			goto out;
266 		}
267 	} else {
268 		/*
269 		 * The pcb is connected, and the L4 code is sure that:
270 		 * - outgoing side uses inp_[lf]addr
271 		 * - incoming side looks up policy after inpcb lookup
272 		 * and address pair is know to be stable.  We do not need
273 		 * to generate spidx again, nor check the address match again.
274 		 *
275 		 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
276 		 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
277 		 */
278 	}
279 
280 	sp->lastused = time_second;
281 	KEY_SP_REF(sp);
282 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
283 	    "DP cause refcnt++:%d SP:%p\n",
284 	    key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
285 out:
286 	pserialize_read_exit(s);
287 	return sp;
288 }
289 
290 static int
291 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
292 	struct secpolicy *sp, int dir)
293 {
294 
295 	KASSERT(IPSEC_DIR_IS_INOROUT(dir));
296 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
297 	KASSERT(inph_locked(pcbsp->sp_inph));
298 
299 	pcbsp->sp_cache[dir].cachesp = NULL;
300 	pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
301 	if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, 1) != 0) {
302 		return EINVAL;
303 	}
304 	pcbsp->sp_cache[dir].cachesp = sp;
305 	if (pcbsp->sp_cache[dir].cachesp) {
306 		/*
307 		 * If the PCB is connected, we can remember a hint to
308 		 * possibly short-circuit IPsec processing in other places.
309 		 */
310 		if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
311 			switch (pcbsp->sp_cache[dir].cachesp->policy) {
312 			case IPSEC_POLICY_NONE:
313 			case IPSEC_POLICY_BYPASS:
314 				pcbsp->sp_cache[dir].cachehint =
315 				    IPSEC_PCBHINT_NO;
316 				break;
317 			default:
318 				pcbsp->sp_cache[dir].cachehint =
319 				    IPSEC_PCBHINT_YES;
320 			}
321 		}
322 	}
323 	pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
324 
325 	return 0;
326 }
327 
328 static int
329 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
330 {
331 	int i;
332 
333 	KASSERT(inph_locked(pcbsp->sp_inph));
334 
335 	for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
336 		if (dir != IPSEC_DIR_ANY && i != dir)
337 			continue;
338 		pcbsp->sp_cache[i].cachesp = NULL;
339 		pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
340 		pcbsp->sp_cache[i].cachegen = 0;
341 		memset(&pcbsp->sp_cache[i].cacheidx, 0,
342 		    sizeof(pcbsp->sp_cache[i].cacheidx));
343 	}
344 	return 0;
345 }
346 
347 void
348 ipsec_pcbconn(struct inpcbpolicy *pcbsp)
349 {
350 
351 	KASSERT(inph_locked(pcbsp->sp_inph));
352 
353 	pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
354 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
355 }
356 
357 void
358 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
359 {
360 
361 	KASSERT(inph_locked(pcbsp->sp_inph));
362 
363 	pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
364 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
365 }
366 
367 void
368 ipsec_invalpcbcacheall(void)
369 {
370 
371 	if (ipsec_spdgen == UINT_MAX)
372 		ipsec_spdgen = 1;
373 	else
374 		ipsec_spdgen++;
375 }
376 
377 /*
378  * Return a held reference to the default SP.
379  */
380 static struct secpolicy *
381 key_get_default_sp(int af, const char *where, int tag)
382 {
383 	struct secpolicy *sp;
384 
385 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
386 
387 	switch(af) {
388 	case AF_INET:
389 		sp = &ip4_def_policy;
390 		break;
391 #ifdef INET6
392 	case AF_INET6:
393 		sp = &ip6_def_policy;
394 		break;
395 #endif
396 	default:
397 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
398 		    "unexpected protocol family %u\n", af);
399 		return NULL;
400 	}
401 
402 	if (sp->policy != IPSEC_POLICY_DISCARD &&
403 		sp->policy != IPSEC_POLICY_NONE) {
404 		IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
405 		    sp->policy, IPSEC_POLICY_NONE);
406 		sp->policy = IPSEC_POLICY_NONE;
407 	}
408 	KEY_SP_REF(sp);
409 
410 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
411 	    sp, key_sp_refcnt(sp));
412 	return sp;
413 }
414 #define	KEY_GET_DEFAULT_SP(af) \
415 	key_get_default_sp((af), __func__, __LINE__)
416 
417 /*
418  * For OUTBOUND packet having a socket. Searching SPD for packet,
419  * and return a pointer to SP.
420  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
421  *		0	: bypass
422  *		EACCES	: discard packet.
423  *		ENOENT	: ipsec_acquire() in progress, maybe.
424  *		others	: error occurred.
425  *	others:	a pointer to SP
426  *
427  * NOTE: IPv6 mapped address concern is implemented here.
428  */
429 static struct secpolicy *
430 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph,
431     int *error)
432 {
433 	struct inpcbpolicy *pcbsp = NULL;
434 	struct secpolicy *currsp = NULL;	/* policy on socket */
435 	struct secpolicy *sp;
436 	int af;
437 
438 	KASSERT(m != NULL);
439 	KASSERT(inph != NULL);
440 	KASSERT(error != NULL);
441 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
442 
443 	KASSERT(inph->inph_socket != NULL);
444 	KASSERT(inph_locked(inph));
445 
446 	/* XXX FIXME inpcb/in6pcb  vs socket*/
447 	af = inph->inph_af;
448 	KASSERTMSG(af == AF_INET || af == AF_INET6,
449 	    "unexpected protocol family %u", af);
450 
451 	KASSERT(inph->inph_sp != NULL);
452 	/* If we have a cached entry, and if it is still valid, use it. */
453 	IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
454 	currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir);
455 	if (currsp) {
456 		*error = 0;
457 		return currsp;
458 	}
459 	IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
460 
461 	switch (af) {
462 	case AF_INET: {
463 		struct inpcb *in4p = (struct inpcb *)inph;
464 		/* set spidx in pcb */
465 		*error = ipsec4_setspidx_inpcb(m, in4p);
466 		pcbsp = in4p->inp_sp;
467 		break;
468 		}
469 
470 #if defined(INET6)
471 	case AF_INET6: {
472 		struct in6pcb *in6p = (struct in6pcb *)inph;
473 		/* set spidx in pcb */
474 		*error = ipsec6_setspidx_in6pcb(m, in6p);
475 		pcbsp = in6p->in6p_sp;
476 		break;
477 		}
478 #endif
479 	default:
480 		*error = EPFNOSUPPORT;
481 		break;
482 	}
483 	if (*error)
484 		return NULL;
485 
486 	KASSERT(pcbsp != NULL);
487 	switch (dir) {
488 	case IPSEC_DIR_INBOUND:
489 		currsp = pcbsp->sp_in;
490 		break;
491 	case IPSEC_DIR_OUTBOUND:
492 		currsp = pcbsp->sp_out;
493 		break;
494 	}
495 	KASSERT(currsp != NULL);
496 
497 	if (pcbsp->priv) {			/* when privilieged socket */
498 		switch (currsp->policy) {
499 		case IPSEC_POLICY_BYPASS:
500 		case IPSEC_POLICY_IPSEC:
501 			KEY_SP_REF(currsp);
502 			sp = currsp;
503 			break;
504 
505 		case IPSEC_POLICY_ENTRUST:
506 			/* look for a policy in SPD */
507 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
508 			if (sp == NULL)		/* no SP found */
509 				sp = KEY_GET_DEFAULT_SP(af);
510 			break;
511 
512 		default:
513 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
514 			    currsp->policy);
515 			*error = EINVAL;
516 			return NULL;
517 		}
518 	} else {				/* unpriv, SPD has policy */
519 		sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
520 		if (sp == NULL) {		/* no SP found */
521 			switch (currsp->policy) {
522 			case IPSEC_POLICY_BYPASS:
523 				IPSECLOG(LOG_ERR, "Illegal policy for "
524 				    "non-priviliged defined %d\n",
525 				    currsp->policy);
526 				*error = EINVAL;
527 				return NULL;
528 
529 			case IPSEC_POLICY_ENTRUST:
530 				sp = KEY_GET_DEFAULT_SP(af);
531 				break;
532 
533 			case IPSEC_POLICY_IPSEC:
534 				KEY_SP_REF(currsp);
535 				sp = currsp;
536 				break;
537 
538 			default:
539 				IPSECLOG(LOG_ERR, "Invalid policy for "
540 				    "PCB %d\n", currsp->policy);
541 				*error = EINVAL;
542 				return NULL;
543 			}
544 		}
545 	}
546 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
547 	    currsp->policy);
548 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
549 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
550 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
551 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
552 	return sp;
553 }
554 
555 /*
556  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
557  * and return a pointer to SP.
558  * OUT:	positive: a pointer to the entry for security policy leaf matched.
559  *	NULL:	no apropreate SP found, the following value is set to error.
560  *		0	: bypass
561  *		EACCES	: discard packet.
562  *		ENOENT	: ipsec_acquire() in progress, maybe.
563  *		others	: error occurred.
564  */
565 struct secpolicy *
566 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
567 {
568 	struct secpolicyindex spidx;
569 	struct secpolicy *sp;
570 
571 	KASSERT(m != NULL);
572 	KASSERT(error != NULL);
573 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
574 
575 	sp = NULL;
576 
577 	/* Make an index to look for a policy. */
578 	*error = ipsec_setspidx(m, &spidx, (flag & IP_FORWARDING) ? 0 : 1);
579 	if (*error != 0) {
580 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
581 		memset(&spidx, 0, sizeof (spidx));
582 		return NULL;
583 	}
584 
585 	spidx.dir = dir;
586 
587 	if (key_havesp(dir)) {
588 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
589 	}
590 
591 	if (sp == NULL)			/* no SP found, use system default */
592 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
593 	KASSERT(sp != NULL);
594 	return sp;
595 }
596 
597 struct secpolicy *
598 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
599 		   struct inpcb *inp)
600 {
601 	struct secpolicy *sp;
602 
603 	*error = 0;
604 
605 	if (inp == NULL) {
606 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
607 	} else {
608 		KASSERT(inp->inp_socket != NULL);
609 		sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)inp, error);
610 	}
611 	if (sp == NULL) {
612 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
613 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
614 		return NULL;
615 	}
616 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
617 	switch (sp->policy) {
618 	case IPSEC_POLICY_ENTRUST:
619 	default:
620 		printf("%s: invalid policy %u\n", __func__, sp->policy);
621 		/* fall thru... */
622 	case IPSEC_POLICY_DISCARD:
623 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
624 		*error = -EINVAL;	/* packet is discarded by caller */
625 		break;
626 	case IPSEC_POLICY_BYPASS:
627 	case IPSEC_POLICY_NONE:
628 		KEY_SP_UNREF(&sp);
629 		sp = NULL;		/* NB: force NULL result */
630 		break;
631 	case IPSEC_POLICY_IPSEC:
632 		KASSERT(sp->req != NULL);
633 		break;
634 	}
635 	if (*error != 0) {
636 		KEY_SP_UNREF(&sp);
637 		sp = NULL;
638 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
639 	}
640 	return sp;
641 }
642 
643 int
644 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
645     u_long *mtu, bool *natt_frag, bool *done)
646 {
647 	struct secpolicy *sp = NULL;
648 	int error, s;
649 
650 	/*
651 	 * Check the security policy (SP) for the packet and, if required,
652 	 * do IPsec-related processing.  There are two cases here; the first
653 	 * time a packet is sent through it will be untagged and handled by
654 	 * ipsec4_checkpolicy().  If the packet is resubmitted to ip_output
655 	 * (e.g. after AH, ESP, etc. processing), there will be a tag to
656 	 * bypass the lookup and related policy checking.
657 	 */
658 	if (ipsec_outdone(m)) {
659 		return 0;
660 	}
661 	s = splsoftnet();
662 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
663 		splx(s);
664 		return 0;
665 	}
666 	sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
667 
668 	/*
669 	 * There are four return cases:
670 	 *	sp != NULL			apply IPsec policy
671 	 *	sp == NULL, error == 0		no IPsec handling needed
672 	 *	sp == NULL, error == -EINVAL	discard packet w/o error
673 	 *	sp == NULL, error != 0		discard packet, report error
674 	 */
675 	if (sp == NULL) {
676 		splx(s);
677 		if (error) {
678 			/*
679 			 * Hack: -EINVAL is used to signal that a packet
680 			 * should be silently discarded.  This is typically
681 			 * because we asked key management for an SA and
682 			 * it was delayed (e.g. kicked up to IKE).
683 			 */
684 			if (error == -EINVAL)
685 				error = 0;
686 			m_freem(m);
687 			*done = true;
688 			return error;
689 		}
690 		/* No IPsec processing for this packet. */
691 		return 0;
692 	}
693 
694 	/*
695 	 * Do delayed checksums now because we send before
696 	 * this is done in the normal processing path.
697 	 */
698 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
699 		in_delayed_cksum(m);
700 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
701 	}
702 
703     {
704 	u_long _mtu = 0;
705 
706 	/* Note: callee frees mbuf */
707 	error = ipsec4_process_packet(m, sp->req, &_mtu);
708 
709 	if (error == 0 && _mtu != 0) {
710 		/*
711 		 * NAT-T ESP fragmentation: do not do IPSec processing
712 		 * now, we will do it on each fragmented packet.
713 		 */
714 		*mtu = _mtu;
715 		*natt_frag = true;
716 		KEY_SP_UNREF(&sp);
717 		splx(s);
718 		return 0;
719 	}
720     }
721 	/*
722 	 * Preserve KAME behaviour: ENOENT can be returned
723 	 * when an SA acquire is in progress.  Don't propagate
724 	 * this to user-level; it confuses applications.
725 	 *
726 	 * XXX this will go away when the SADB is redone.
727 	 */
728 	if (error == ENOENT)
729 		error = 0;
730 	KEY_SP_UNREF(&sp);
731 	splx(s);
732 	*done = true;
733 	return error;
734 }
735 
736 int
737 ipsec4_input(struct mbuf *m, int flags)
738 {
739 	struct secpolicy *sp;
740 	int error, s;
741 
742 	s = splsoftnet();
743 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
744 	if (sp == NULL) {
745 		splx(s);
746 		return EINVAL;
747 	}
748 
749 	/*
750 	 * Check security policy against packet attributes.
751 	 */
752 	error = ipsec_in_reject(sp, m);
753 	KEY_SP_UNREF(&sp);
754 	splx(s);
755 	if (error) {
756 		return error;
757 	}
758 
759 	if (flags == 0) {
760 		/* We are done. */
761 		return 0;
762 	}
763 
764 	/*
765 	 * Peek at the outbound SP for this packet to determine if
766 	 * it is a Fast Forward candidate.
767 	 */
768 	s = splsoftnet();
769 	sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, NULL);
770 	if (sp != NULL) {
771 		m->m_flags &= ~M_CANFASTFWD;
772 		KEY_SP_UNREF(&sp);
773 	}
774 	splx(s);
775 	return 0;
776 }
777 
778 int
779 ipsec4_forward(struct mbuf *m, int *destmtu)
780 {
781 	/*
782 	 * If the packet is routed over IPsec tunnel, tell the
783 	 * originator the tunnel MTU.
784 	 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
785 	 * XXX quickhack!!!
786 	 */
787 	struct secpolicy *sp;
788 	size_t ipsechdr;
789 	int error;
790 
791 	sp = ipsec4_getpolicybyaddr(m,
792 	    IPSEC_DIR_OUTBOUND, IP_FORWARDING, &error);
793 	if (sp == NULL) {
794 		return EINVAL;
795 	}
796 
797 	/* Count IPsec header size. */
798 	ipsechdr = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
799 
800 	/*
801 	 * Find the correct route for outer IPv4 header, compute tunnel MTU.
802 	 */
803 	if (sp->req) {
804 		struct secasvar *sav;
805 
806 		sav = ipsec_lookup_sa(sp->req, m);
807 		if (sav != NULL) {
808 			struct route *ro;
809 			struct rtentry *rt;
810 
811 			ro = &sav->sah->sa_route;
812 			rt = rtcache_validate(ro);
813 			if (rt && rt->rt_ifp) {
814 				*destmtu = rt->rt_rmx.rmx_mtu ?
815 				    rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
816 				*destmtu -= ipsechdr;
817 			}
818 			rtcache_unref(rt, ro);
819 			KEY_SA_UNREF(&sav);
820 		}
821 	}
822 	KEY_SP_UNREF(&sp);
823 	return 0;
824 }
825 
826 #ifdef INET6
827 struct secpolicy *
828 ipsec6_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
829 	 	   struct in6pcb *in6p)
830 {
831 	struct secpolicy *sp;
832 
833 	*error = 0;
834 
835 	if (in6p == NULL) {
836 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
837 	} else {
838 		KASSERT(in6p->in6p_socket != NULL);
839 		sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)in6p, error);
840 	}
841 	if (sp == NULL) {
842 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
843 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
844 		return NULL;
845 	}
846 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
847 	switch (sp->policy) {
848 	case IPSEC_POLICY_ENTRUST:
849 	default:
850 		printf("%s: invalid policy %u\n", __func__, sp->policy);
851 		/* fall thru... */
852 	case IPSEC_POLICY_DISCARD:
853 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
854 		*error = -EINVAL;   /* packet is discarded by caller */
855 		break;
856 	case IPSEC_POLICY_BYPASS:
857 	case IPSEC_POLICY_NONE:
858 		KEY_SP_UNREF(&sp);
859 		sp = NULL;	  /* NB: force NULL result */
860 		break;
861 	case IPSEC_POLICY_IPSEC:
862 		KASSERT(sp->req != NULL);
863 		break;
864 	}
865 	if (*error != 0) {
866 		KEY_SP_UNREF(&sp);
867 		sp = NULL;
868 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
869 	}
870 	return sp;
871 }
872 #endif /* INET6 */
873 
874 static int
875 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
876 {
877 	int error;
878 
879 	KASSERT(pcb != NULL);
880 	KASSERT(pcb->inp_sp != NULL);
881 	KASSERT(pcb->inp_sp->sp_out != NULL);
882 	KASSERT(pcb->inp_sp->sp_in != NULL);
883 
884 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
885 	if (error == 0) {
886 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
887 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
888 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
889 	} else {
890 		memset(&pcb->inp_sp->sp_in->spidx, 0,
891 		    sizeof(pcb->inp_sp->sp_in->spidx));
892 		memset(&pcb->inp_sp->sp_out->spidx, 0,
893 		    sizeof(pcb->inp_sp->sp_in->spidx));
894 	}
895 	return error;
896 }
897 
898 #ifdef INET6
899 static int
900 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
901 {
902 	struct secpolicyindex *spidx;
903 	int error;
904 
905 	KASSERT(pcb != NULL);
906 	KASSERT(pcb->in6p_sp != NULL);
907 	KASSERT(pcb->in6p_sp->sp_out != NULL);
908 	KASSERT(pcb->in6p_sp->sp_in != NULL);
909 
910 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
911 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
912 
913 	spidx = &pcb->in6p_sp->sp_in->spidx;
914 	error = ipsec_setspidx(m, spidx, 1);
915 	if (error)
916 		goto bad;
917 	spidx->dir = IPSEC_DIR_INBOUND;
918 
919 	spidx = &pcb->in6p_sp->sp_out->spidx;
920 	error = ipsec_setspidx(m, spidx, 1);
921 	if (error)
922 		goto bad;
923 	spidx->dir = IPSEC_DIR_OUTBOUND;
924 
925 	return 0;
926 
927 bad:
928 	memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
929 	memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
930 	return error;
931 }
932 #endif
933 
934 /*
935  * configure security policy index (src/dst/proto/sport/dport)
936  * by looking at the content of mbuf.
937  * the caller is responsible for error recovery (like clearing up spidx).
938  */
939 static int
940 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
941 {
942 	struct ip *ip = NULL;
943 	struct ip ipbuf;
944 	u_int v;
945 	struct mbuf *n;
946 	int len;
947 	int error;
948 
949 	KASSERT(m != NULL);
950 
951 	/*
952 	 * validate m->m_pkthdr.len.  we see incorrect length if we
953 	 * mistakenly call this function with inconsistent mbuf chain
954 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
955 	 */
956 	len = 0;
957 	for (n = m; n; n = n->m_next)
958 		len += n->m_len;
959 	if (m->m_pkthdr.len != len) {
960 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
961 		    "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
962 		    len, m->m_pkthdr.len);
963 		return EINVAL;
964 	}
965 
966 	if (m->m_pkthdr.len < sizeof(struct ip)) {
967 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
968 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
969 		    m->m_pkthdr.len);
970 		return EINVAL;
971 	}
972 
973 	if (m->m_len >= sizeof(*ip))
974 		ip = mtod(m, struct ip *);
975 	else {
976 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
977 		ip = &ipbuf;
978 	}
979 	v = ip->ip_v;
980 	switch (v) {
981 	case 4:
982 		error = ipsec4_setspidx_ipaddr(m, spidx);
983 		if (error)
984 			return error;
985 		ipsec4_get_ulp(m, spidx, needport);
986 		return 0;
987 #ifdef INET6
988 	case 6:
989 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
990 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
991 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
992 			    "ignored.\n", m->m_pkthdr.len);
993 			return EINVAL;
994 		}
995 		error = ipsec6_setspidx_ipaddr(m, spidx);
996 		if (error)
997 			return error;
998 		ipsec6_get_ulp(m, spidx, needport);
999 		return 0;
1000 #endif
1001 	default:
1002 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1003 		    "unknown IP version %u, ignored.\n", v);
1004 		return EINVAL;
1005 	}
1006 }
1007 
1008 static void
1009 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
1010 {
1011 	u_int8_t nxt;
1012 	int off;
1013 
1014 	/* sanity check */
1015 	KASSERT(m != NULL);
1016 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
1017 
1018 	/* NB: ip_input() flips it into host endian XXX need more checking */
1019 	if (m->m_len >= sizeof(struct ip)) {
1020 		struct ip *ip = mtod(m, struct ip *);
1021 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1022 			goto done;
1023 		off = ip->ip_hl << 2;
1024 		nxt = ip->ip_p;
1025 	} else {
1026 		struct ip ih;
1027 
1028 		m_copydata(m, 0, sizeof (struct ip), &ih);
1029 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
1030 			goto done;
1031 		off = ih.ip_hl << 2;
1032 		nxt = ih.ip_p;
1033 	}
1034 
1035 	while (off < m->m_pkthdr.len) {
1036 		struct ip6_ext ip6e;
1037 		struct tcphdr th;
1038 		struct udphdr uh;
1039 		struct icmp icmph;
1040 
1041 		switch (nxt) {
1042 		case IPPROTO_TCP:
1043 			spidx->ul_proto = nxt;
1044 			if (!needport)
1045 				goto done_proto;
1046 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1047 				goto done;
1048 			m_copydata(m, off, sizeof (th), &th);
1049 			spidx->src.sin.sin_port = th.th_sport;
1050 			spidx->dst.sin.sin_port = th.th_dport;
1051 			return;
1052 		case IPPROTO_UDP:
1053 			spidx->ul_proto = nxt;
1054 			if (!needport)
1055 				goto done_proto;
1056 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1057 				goto done;
1058 			m_copydata(m, off, sizeof (uh), &uh);
1059 			spidx->src.sin.sin_port = uh.uh_sport;
1060 			spidx->dst.sin.sin_port = uh.uh_dport;
1061 			return;
1062 		case IPPROTO_AH:
1063 			if (m->m_pkthdr.len > off + sizeof(ip6e))
1064 				goto done;
1065 			/* XXX sigh, this works but is totally bogus */
1066 			m_copydata(m, off, sizeof(ip6e), &ip6e);
1067 			off += (ip6e.ip6e_len + 2) << 2;
1068 			nxt = ip6e.ip6e_nxt;
1069 			break;
1070 		case IPPROTO_ICMP:
1071 			spidx->ul_proto = nxt;
1072 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
1073 				return;
1074 			m_copydata(m, off, sizeof(icmph), &icmph);
1075 			((struct sockaddr_in *)&spidx->src)->sin_port =
1076 			    htons((uint16_t)icmph.icmp_type);
1077 			((struct sockaddr_in *)&spidx->dst)->sin_port =
1078 			    htons((uint16_t)icmph.icmp_code);
1079 			return;
1080 		default:
1081 			/* XXX intermediate headers??? */
1082 			spidx->ul_proto = nxt;
1083 			goto done_proto;
1084 		}
1085 	}
1086 done:
1087 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
1088 done_proto:
1089 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
1090 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
1091 }
1092 
1093 /* assumes that m is sane */
1094 static int
1095 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1096 {
1097 	static const struct sockaddr_in template = {
1098 		sizeof (struct sockaddr_in),
1099 		AF_INET,
1100 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
1101 	};
1102 
1103 	spidx->src.sin = template;
1104 	spidx->dst.sin = template;
1105 
1106 	if (m->m_len < sizeof (struct ip)) {
1107 		m_copydata(m, offsetof(struct ip, ip_src),
1108 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
1109 		m_copydata(m, offsetof(struct ip, ip_dst),
1110 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
1111 	} else {
1112 		struct ip *ip = mtod(m, struct ip *);
1113 		spidx->src.sin.sin_addr = ip->ip_src;
1114 		spidx->dst.sin.sin_addr = ip->ip_dst;
1115 	}
1116 
1117 	spidx->prefs = sizeof(struct in_addr) << 3;
1118 	spidx->prefd = sizeof(struct in_addr) << 3;
1119 
1120 	return 0;
1121 }
1122 
1123 #ifdef INET6
1124 static void
1125 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx,
1126 	       int needport)
1127 {
1128 	int off, nxt;
1129 	struct tcphdr th;
1130 	struct udphdr uh;
1131 	struct icmp6_hdr icmph;
1132 
1133 	KASSERT(m != NULL);
1134 
1135 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1136 		kdebug_mbuf(__func__, m);
1137 	}
1138 
1139 	/* set default */
1140 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
1141 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
1142 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
1143 
1144 	nxt = -1;
1145 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
1146 	if (off < 0 || m->m_pkthdr.len < off)
1147 		return;
1148 
1149 	switch (nxt) {
1150 	case IPPROTO_TCP:
1151 		spidx->ul_proto = nxt;
1152 		if (!needport)
1153 			break;
1154 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1155 			break;
1156 		m_copydata(m, off, sizeof(th), &th);
1157 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
1158 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
1159 		break;
1160 	case IPPROTO_UDP:
1161 		spidx->ul_proto = nxt;
1162 		if (!needport)
1163 			break;
1164 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1165 			break;
1166 		m_copydata(m, off, sizeof(uh), &uh);
1167 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
1168 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1169 		break;
1170 	case IPPROTO_ICMPV6:
1171 		spidx->ul_proto = nxt;
1172 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
1173 			break;
1174 		m_copydata(m, off, sizeof(icmph), &icmph);
1175 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
1176 		    htons((uint16_t)icmph.icmp6_type);
1177 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
1178 		    htons((uint16_t)icmph.icmp6_code);
1179 		break;
1180 	default:
1181 		/* XXX intermediate headers??? */
1182 		spidx->ul_proto = nxt;
1183 		break;
1184 	}
1185 }
1186 
1187 /* assumes that m is sane */
1188 static int
1189 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1190 {
1191 	struct ip6_hdr *ip6 = NULL;
1192 	struct ip6_hdr ip6buf;
1193 	struct sockaddr_in6 *sin6;
1194 
1195 	if (m->m_len >= sizeof(*ip6))
1196 		ip6 = mtod(m, struct ip6_hdr *);
1197 	else {
1198 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
1199 		ip6 = &ip6buf;
1200 	}
1201 
1202 	sin6 = (struct sockaddr_in6 *)&spidx->src;
1203 	memset(sin6, 0, sizeof(*sin6));
1204 	sin6->sin6_family = AF_INET6;
1205 	sin6->sin6_len = sizeof(struct sockaddr_in6);
1206 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
1207 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1208 		sin6->sin6_addr.s6_addr16[1] = 0;
1209 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1210 	}
1211 	spidx->prefs = sizeof(struct in6_addr) << 3;
1212 
1213 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
1214 	memset(sin6, 0, sizeof(*sin6));
1215 	sin6->sin6_family = AF_INET6;
1216 	sin6->sin6_len = sizeof(struct sockaddr_in6);
1217 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
1218 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1219 		sin6->sin6_addr.s6_addr16[1] = 0;
1220 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1221 	}
1222 	spidx->prefd = sizeof(struct in6_addr) << 3;
1223 
1224 	return 0;
1225 }
1226 #endif
1227 
1228 static void
1229 ipsec_delpcbpolicy(struct inpcbpolicy *p)
1230 {
1231 
1232 	kmem_intr_free(p, sizeof(*p));
1233 }
1234 
1235 /* initialize policy in PCB */
1236 int
1237 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
1238 {
1239 	struct inpcbpolicy *new;
1240 
1241 	KASSERT(so != NULL);
1242 	KASSERT(policy != NULL);
1243 
1244 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
1245 	if (new == NULL) {
1246 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
1247 		return ENOBUFS;
1248 	}
1249 
1250 	if (IPSEC_PRIVILEGED_SO(so))
1251 		new->priv = 1;
1252 	else
1253 		new->priv = 0;
1254 
1255 	/*
1256 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
1257 	 */
1258 	new->sp_in = &ipsec_dummy_sp;
1259 	new->sp_out = &ipsec_dummy_sp;
1260 
1261 	*policy = new;
1262 
1263 	return 0;
1264 }
1265 
1266 #if 0 /* unused */
1267 /* copy old ipsec policy into new */
1268 int
1269 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
1270 {
1271 	struct secpolicy *sp;
1272 
1273 	sp = ipsec_deepcopy_policy(old->sp_in);
1274 	if (sp) {
1275 		KEY_SP_UNREF(&new->sp_in);
1276 		new->sp_in = sp;
1277 	} else
1278 		return ENOBUFS;
1279 
1280 	sp = ipsec_deepcopy_policy(old->sp_out);
1281 	if (sp) {
1282 		KEY_SP_UNREF(&new->sp_out);
1283 		new->sp_out = sp;
1284 	} else
1285 		return ENOBUFS;
1286 
1287 	new->priv = old->priv;
1288 
1289 	return 0;
1290 }
1291 
1292 /* deep-copy a policy in PCB */
1293 static struct secpolicy *
1294 ipsec_deepcopy_policy(const struct secpolicy *src)
1295 {
1296 	struct ipsecrequest *newchain = NULL;
1297 	const struct ipsecrequest *p;
1298 	struct ipsecrequest **q;
1299 	struct secpolicy *dst;
1300 
1301 	if (src == NULL)
1302 		return NULL;
1303 	dst = KEY_NEWSP();
1304 	if (dst == NULL)
1305 		return NULL;
1306 
1307 	/*
1308 	 * deep-copy IPsec request chain.  This is required since struct
1309 	 * ipsecrequest is not reference counted.
1310 	 */
1311 	q = &newchain;
1312 	for (p = src->req; p; p = p->next) {
1313 		*q = kmem_zalloc(sizeof(**q), KM_SLEEP);
1314 		(*q)->next = NULL;
1315 
1316 		(*q)->saidx.proto = p->saidx.proto;
1317 		(*q)->saidx.mode = p->saidx.mode;
1318 		(*q)->level = p->level;
1319 		(*q)->saidx.reqid = p->saidx.reqid;
1320 
1321 		memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
1322 		memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
1323 
1324 		(*q)->sp = dst;
1325 
1326 		q = &((*q)->next);
1327 	}
1328 
1329 	dst->req = newchain;
1330 	dst->state = src->state;
1331 	dst->policy = src->policy;
1332 	/* do not touch the refcnt fields */
1333 
1334 	return dst;
1335 }
1336 #endif
1337 
1338 static void
1339 ipsec_destroy_policy(struct secpolicy *sp)
1340 {
1341 
1342 	if (sp == &ipsec_dummy_sp)
1343 		; /* It's dummy. No need to free it. */
1344 	else {
1345 		/*
1346 		 * We cannot destroy here because it can be called in
1347 		 * softint. So mark the SP as DEAD and let the timer
1348 		 * destroy it. See key_timehandler_spd.
1349 		 */
1350 		sp->state = IPSEC_SPSTATE_DEAD;
1351 	}
1352 }
1353 
1354 /* set policy and ipsec request if present. */
1355 static int
1356 ipsec_set_policy(
1357 	struct secpolicy **policy,
1358 	int optname,
1359 	const void *request,
1360 	size_t len,
1361 	kauth_cred_t cred
1362 )
1363 {
1364 	const struct sadb_x_policy *xpl;
1365 	struct secpolicy *newsp = NULL, *oldsp;
1366 	int error;
1367 
1368 	KASSERT(!cpu_softintr_p());
1369 
1370 	/* sanity check. */
1371 	if (policy == NULL || *policy == NULL || request == NULL)
1372 		return EINVAL;
1373 	if (len < sizeof(*xpl))
1374 		return EINVAL;
1375 	xpl = (const struct sadb_x_policy *)request;
1376 
1377 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1378 		kdebug_sadb_xpolicy("set passed policy", request);
1379 	}
1380 
1381 	/* check policy type */
1382 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1383 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1384 	 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1385 		return EINVAL;
1386 
1387 	/* check privileged socket */
1388 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1389 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
1390 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
1391 		if (error)
1392 			return (error);
1393 	}
1394 
1395 	/* allocation new SP entry */
1396 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1397 		return error;
1398 
1399 	key_init_sp(newsp);
1400 	newsp->created = time_uptime;
1401 	/* Insert the global list for SPs for sockets */
1402 	key_socksplist_add(newsp);
1403 
1404 	/* clear old SP and set new SP */
1405 	oldsp = *policy;
1406 	*policy = newsp;
1407 	ipsec_destroy_policy(oldsp);
1408 
1409 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1410 		printf("%s: new policy\n", __func__);
1411 		kdebug_secpolicy(newsp);
1412 	}
1413 
1414 	return 0;
1415 }
1416 
1417 static int
1418 ipsec_get_policy(struct secpolicy *policy, struct mbuf **mp)
1419 {
1420 
1421 	/* sanity check. */
1422 	if (policy == NULL || mp == NULL)
1423 		return EINVAL;
1424 
1425 	*mp = key_sp2msg(policy, M_NOWAIT);
1426 	if (!*mp) {
1427 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
1428 		return ENOBUFS;
1429 	}
1430 
1431 	(*mp)->m_type = MT_DATA;
1432 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1433 		kdebug_mbuf(__func__, *mp);
1434 	}
1435 
1436 	return 0;
1437 }
1438 
1439 int
1440 ipsec4_set_policy(struct inpcb *inp, int optname, const void *request,
1441 		  size_t len, kauth_cred_t cred)
1442 {
1443 	const struct sadb_x_policy *xpl;
1444 	struct secpolicy **policy;
1445 
1446 	KASSERT(!cpu_softintr_p());
1447 	KASSERT(inp_locked(inp));
1448 
1449 	/* sanity check. */
1450 	if (inp == NULL || request == NULL)
1451 		return EINVAL;
1452 	if (len < sizeof(*xpl))
1453 		return EINVAL;
1454 	xpl = (const struct sadb_x_policy *)request;
1455 
1456 	KASSERT(inp->inp_sp != NULL);
1457 
1458 	/* select direction */
1459 	switch (xpl->sadb_x_policy_dir) {
1460 	case IPSEC_DIR_INBOUND:
1461 		policy = &inp->inp_sp->sp_in;
1462 		break;
1463 	case IPSEC_DIR_OUTBOUND:
1464 		policy = &inp->inp_sp->sp_out;
1465 		break;
1466 	default:
1467 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1468 		    xpl->sadb_x_policy_dir);
1469 		return EINVAL;
1470 	}
1471 
1472 	return ipsec_set_policy(policy, optname, request, len, cred);
1473 }
1474 
1475 int
1476 ipsec4_get_policy(struct inpcb *inp, const void *request, size_t len,
1477 		  struct mbuf **mp)
1478 {
1479 	const struct sadb_x_policy *xpl;
1480 	struct secpolicy *policy;
1481 
1482 	/* sanity check. */
1483 	if (inp == NULL || request == NULL || mp == NULL)
1484 		return EINVAL;
1485 	KASSERT(inp->inp_sp != NULL);
1486 	if (len < sizeof(*xpl))
1487 		return EINVAL;
1488 	xpl = (const struct sadb_x_policy *)request;
1489 
1490 	/* select direction */
1491 	switch (xpl->sadb_x_policy_dir) {
1492 	case IPSEC_DIR_INBOUND:
1493 		policy = inp->inp_sp->sp_in;
1494 		break;
1495 	case IPSEC_DIR_OUTBOUND:
1496 		policy = inp->inp_sp->sp_out;
1497 		break;
1498 	default:
1499 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1500 		    xpl->sadb_x_policy_dir);
1501 		return EINVAL;
1502 	}
1503 
1504 	return ipsec_get_policy(policy, mp);
1505 }
1506 
1507 /* delete policy in PCB */
1508 int
1509 ipsec4_delete_pcbpolicy(struct inpcb *inp)
1510 {
1511 
1512 	KASSERT(inp != NULL);
1513 
1514 	if (inp->inp_sp == NULL)
1515 		return 0;
1516 
1517 	if (inp->inp_sp->sp_in != NULL)
1518 		ipsec_destroy_policy(inp->inp_sp->sp_in);
1519 
1520 	if (inp->inp_sp->sp_out != NULL)
1521 		ipsec_destroy_policy(inp->inp_sp->sp_out);
1522 
1523 	ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
1524 
1525 	ipsec_delpcbpolicy(inp->inp_sp);
1526 	inp->inp_sp = NULL;
1527 
1528 	return 0;
1529 }
1530 
1531 #ifdef INET6
1532 int
1533 ipsec6_set_policy(struct in6pcb *in6p, int optname, const void *request,
1534 		  size_t len, kauth_cred_t cred)
1535 {
1536 	const struct sadb_x_policy *xpl;
1537 	struct secpolicy **policy;
1538 
1539 	KASSERT(!cpu_softintr_p());
1540 	KASSERT(in6p_locked(in6p));
1541 
1542 	/* sanity check. */
1543 	if (in6p == NULL || request == NULL)
1544 		return EINVAL;
1545 	if (len < sizeof(*xpl))
1546 		return EINVAL;
1547 	xpl = (const struct sadb_x_policy *)request;
1548 
1549 	/* select direction */
1550 	switch (xpl->sadb_x_policy_dir) {
1551 	case IPSEC_DIR_INBOUND:
1552 		policy = &in6p->in6p_sp->sp_in;
1553 		break;
1554 	case IPSEC_DIR_OUTBOUND:
1555 		policy = &in6p->in6p_sp->sp_out;
1556 		break;
1557 	default:
1558 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1559 		    xpl->sadb_x_policy_dir);
1560 		return EINVAL;
1561 	}
1562 
1563 	return ipsec_set_policy(policy, optname, request, len, cred);
1564 }
1565 
1566 int
1567 ipsec6_get_policy(struct in6pcb *in6p, const void *request, size_t len,
1568 		  struct mbuf **mp)
1569 {
1570 	const struct sadb_x_policy *xpl;
1571 	struct secpolicy *policy;
1572 
1573 	/* sanity check. */
1574 	if (in6p == NULL || request == NULL || mp == NULL)
1575 		return EINVAL;
1576 	KASSERT(in6p->in6p_sp != NULL);
1577 	if (len < sizeof(*xpl))
1578 		return EINVAL;
1579 	xpl = (const struct sadb_x_policy *)request;
1580 
1581 	/* select direction */
1582 	switch (xpl->sadb_x_policy_dir) {
1583 	case IPSEC_DIR_INBOUND:
1584 		policy = in6p->in6p_sp->sp_in;
1585 		break;
1586 	case IPSEC_DIR_OUTBOUND:
1587 		policy = in6p->in6p_sp->sp_out;
1588 		break;
1589 	default:
1590 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1591 		    xpl->sadb_x_policy_dir);
1592 		return EINVAL;
1593 	}
1594 
1595 	return ipsec_get_policy(policy, mp);
1596 }
1597 
1598 int
1599 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
1600 {
1601 
1602 	KASSERT(in6p != NULL);
1603 
1604 	if (in6p->in6p_sp == NULL)
1605 		return 0;
1606 
1607 	if (in6p->in6p_sp->sp_in != NULL)
1608 		ipsec_destroy_policy(in6p->in6p_sp->sp_in);
1609 
1610 	if (in6p->in6p_sp->sp_out != NULL)
1611 		ipsec_destroy_policy(in6p->in6p_sp->sp_out);
1612 
1613 	ipsec_invalpcbcache(in6p->in6p_sp, IPSEC_DIR_ANY);
1614 
1615 	ipsec_delpcbpolicy(in6p->in6p_sp);
1616 	in6p->in6p_sp = NULL;
1617 
1618 	return 0;
1619 }
1620 #endif
1621 
1622 /*
1623  * return current level.
1624  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1625  */
1626 u_int
1627 ipsec_get_reqlevel(const struct ipsecrequest *isr)
1628 {
1629 	u_int level = 0;
1630 	u_int esp_trans_deflev, esp_net_deflev;
1631 	u_int ah_trans_deflev, ah_net_deflev;
1632 
1633 	KASSERT(isr != NULL);
1634 	KASSERT(isr->sp != NULL);
1635 	KASSERTMSG(
1636 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1637 	    "af family mismatch, src %u, dst %u",
1638 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
1639 
1640 /* XXX note that we have ipseclog() expanded here - code sync issue */
1641 #define IPSEC_CHECK_DEFAULT(lev) 					\
1642     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
1643     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
1644 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
1645 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
1646 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
1647     : (lev))
1648 
1649 	/* set default level */
1650 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1651 #ifdef INET
1652 	case AF_INET:
1653 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1654 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1655 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1656 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1657 		break;
1658 #endif
1659 #ifdef INET6
1660 	case AF_INET6:
1661 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1662 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1663 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1664 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1665 		break;
1666 #endif /* INET6 */
1667 	default:
1668 		panic("%s: unknown af %u", __func__,
1669 		    isr->sp->spidx.src.sa.sa_family);
1670 	}
1671 
1672 #undef IPSEC_CHECK_DEFAULT
1673 
1674 	/* set level */
1675 	switch (isr->level) {
1676 	case IPSEC_LEVEL_DEFAULT:
1677 		switch (isr->saidx.proto) {
1678 		case IPPROTO_ESP:
1679 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1680 				level = esp_net_deflev;
1681 			else
1682 				level = esp_trans_deflev;
1683 			break;
1684 		case IPPROTO_AH:
1685 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1686 				level = ah_net_deflev;
1687 			else
1688 				level = ah_trans_deflev;
1689 			break;
1690 		case IPPROTO_IPCOMP:
1691 			/*
1692 			 * we don't really care, as IPcomp document says that
1693 			 * we shouldn't compress small packets
1694 			 */
1695 			level = IPSEC_LEVEL_USE;
1696 			break;
1697 		default:
1698 			panic("%s: Illegal protocol defined %u", __func__,
1699 			    isr->saidx.proto);
1700 		}
1701 		break;
1702 
1703 	case IPSEC_LEVEL_USE:
1704 	case IPSEC_LEVEL_REQUIRE:
1705 		level = isr->level;
1706 		break;
1707 	case IPSEC_LEVEL_UNIQUE:
1708 		level = IPSEC_LEVEL_REQUIRE;
1709 		break;
1710 
1711 	default:
1712 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
1713 	}
1714 
1715 	return level;
1716 }
1717 
1718 /*
1719  * Check security policy requirements against the actual
1720  * packet contents.  Return one if the packet should be
1721  * reject as "invalid"; otherwiser return zero to have the
1722  * packet treated as "valid".
1723  *
1724  * OUT:
1725  *	0: valid
1726  *	1: invalid
1727  */
1728 int
1729 ipsec_in_reject(const struct secpolicy *sp, const struct mbuf *m)
1730 {
1731 	struct ipsecrequest *isr;
1732 
1733 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1734 		printf("%s: using SP\n", __func__);
1735 		kdebug_secpolicy(sp);
1736 	}
1737 
1738 	/* check policy */
1739 	switch (sp->policy) {
1740 	case IPSEC_POLICY_DISCARD:
1741 		return 1;
1742 	case IPSEC_POLICY_BYPASS:
1743 	case IPSEC_POLICY_NONE:
1744 		return 0;
1745 	}
1746 
1747 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1748 	    "invalid policy %u", sp->policy);
1749 
1750 	/* XXX should compare policy against ipsec header history */
1751 
1752 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1753 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1754 			continue;
1755 		switch (isr->saidx.proto) {
1756 		case IPPROTO_ESP:
1757 			if ((m->m_flags & M_DECRYPTED) == 0) {
1758 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1759 				    "ESP m_flags:%x\n", m->m_flags);
1760 				return 1;
1761 			}
1762 			break;
1763 		case IPPROTO_AH:
1764 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
1765 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1766 				    "AH m_flags:%x\n", m->m_flags);
1767 				return 1;
1768 			}
1769 			break;
1770 		case IPPROTO_IPCOMP:
1771 			/*
1772 			 * we don't really care, as IPcomp document
1773 			 * says that we shouldn't compress small
1774 			 * packets, IPComp policy should always be
1775 			 * treated as being in "use" level.
1776 			 */
1777 			break;
1778 		}
1779 	}
1780 	return 0;		/* valid */
1781 }
1782 
1783 /*
1784  * Check AH/ESP integrity.
1785  * This function is called from tcp_input(), udp_input(),
1786  * and {ah,esp}4_input for tunnel mode
1787  */
1788 int
1789 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1790 {
1791 	struct secpolicy *sp;
1792 	int error;
1793 	int result;
1794 
1795 	KASSERT(m != NULL);
1796 
1797 	/* get SP for this packet.
1798 	 * When we are called from ip_forward(), we call
1799 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1800 	 */
1801 	if (inp == NULL)
1802 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1803 	else
1804 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1805 					   (struct inpcb_hdr *)inp, &error);
1806 
1807 	if (sp != NULL) {
1808 		result = ipsec_in_reject(sp, m);
1809 		if (result)
1810 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1811 		KEY_SP_UNREF(&sp);
1812 	} else {
1813 		result = 0;	/* XXX should be panic ?
1814 				 * -> No, there may be error. */
1815 	}
1816 	return result;
1817 }
1818 
1819 
1820 #ifdef INET6
1821 /*
1822  * Check AH/ESP integrity.
1823  * This function is called from tcp6_input(), udp6_input(),
1824  * and {ah,esp}6_input for tunnel mode
1825  */
1826 int
1827 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
1828 {
1829 	struct secpolicy *sp = NULL;
1830 	int error;
1831 	int result;
1832 
1833 	KASSERT(m != NULL);
1834 
1835 	/* get SP for this packet.
1836 	 * When we are called from ip_forward(), we call
1837 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1838 	 */
1839 	if (in6p == NULL)
1840 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1841 	else
1842 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1843 			(struct inpcb_hdr *)in6p,
1844 			&error);
1845 
1846 	if (sp != NULL) {
1847 		result = ipsec_in_reject(sp, m);
1848 		if (result)
1849 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1850 		KEY_SP_UNREF(&sp);
1851 	} else {
1852 		result = 0;
1853 	}
1854 	return result;
1855 }
1856 #endif
1857 
1858 /*
1859  * compute the byte size to be occupied by IPsec header.
1860  * in case it is tunneled, it includes the size of outer IP header.
1861  * NOTE: SP passed is free in this function.
1862  */
1863 static size_t
1864 ipsec_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
1865 {
1866 	struct ipsecrequest *isr;
1867 	size_t siz;
1868 
1869 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1870 		printf("%s: using SP\n", __func__);
1871 		kdebug_secpolicy(sp);
1872 	}
1873 
1874 	switch (sp->policy) {
1875 	case IPSEC_POLICY_DISCARD:
1876 	case IPSEC_POLICY_BYPASS:
1877 	case IPSEC_POLICY_NONE:
1878 		return 0;
1879 	}
1880 
1881 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1882 	    "invalid policy %u", sp->policy);
1883 
1884 	siz = 0;
1885 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1886 		size_t clen = 0;
1887 		struct secasvar *sav;
1888 
1889 		switch (isr->saidx.proto) {
1890 		case IPPROTO_ESP:
1891 			sav = ipsec_lookup_sa(isr, m);
1892 			if (sav != NULL) {
1893 				clen = esp_hdrsiz(sav);
1894 				KEY_SA_UNREF(&sav);
1895 			} else
1896 				clen = esp_hdrsiz(NULL);
1897 			break;
1898 		case IPPROTO_AH:
1899 			sav = ipsec_lookup_sa(isr, m);
1900 			if (sav != NULL) {
1901 				clen = ah_hdrsiz(sav);
1902 				KEY_SA_UNREF(&sav);
1903 			} else
1904 				clen = ah_hdrsiz(NULL);
1905 			break;
1906 		case IPPROTO_IPCOMP:
1907 			clen = sizeof(struct ipcomp);
1908 			break;
1909 		}
1910 
1911 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1912 			switch (isr->saidx.dst.sa.sa_family) {
1913 			case AF_INET:
1914 				clen += sizeof(struct ip);
1915 				break;
1916 #ifdef INET6
1917 			case AF_INET6:
1918 				clen += sizeof(struct ip6_hdr);
1919 				break;
1920 #endif
1921 			default:
1922 				IPSECLOG(LOG_ERR, "unknown AF %d in "
1923 				    "IPsec tunnel SA\n",
1924 				    ((const struct sockaddr *)&isr->saidx.dst)
1925 				    ->sa_family);
1926 				break;
1927 			}
1928 		}
1929 		siz += clen;
1930 	}
1931 
1932 	return siz;
1933 }
1934 
1935 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1936 size_t
1937 ipsec4_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1938 {
1939 	struct secpolicy *sp;
1940 	int error;
1941 	size_t size;
1942 
1943 	KASSERT(m != NULL);
1944 	KASSERTMSG(inp == NULL || inp->inp_socket != NULL, "socket w/o inpcb");
1945 
1946 	/* get SP for this packet.
1947 	 * When we are called from ip_forward(), we call
1948 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1949 	 */
1950 	if (inp == NULL)
1951 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1952 	else
1953 		sp = ipsec_getpolicybysock(m, dir,
1954 					   (struct inpcb_hdr *)inp, &error);
1955 
1956 	if (sp != NULL) {
1957 		size = ipsec_hdrsiz(sp, m);
1958 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%lu.\n",
1959 		    (unsigned long)size);
1960 
1961 		KEY_SP_UNREF(&sp);
1962 	} else {
1963 		size = 0;	/* XXX should be panic ? */
1964 	}
1965 	return size;
1966 }
1967 
1968 #ifdef INET6
1969 /* This function is called from ipsec6_hdrsize_tcp(),
1970  * and maybe from ip6_forward.()
1971  */
1972 size_t
1973 ipsec6_hdrsiz(struct mbuf *m, u_int dir, struct in6pcb *in6p)
1974 {
1975 	struct secpolicy *sp;
1976 	int error;
1977 	size_t size;
1978 
1979 	KASSERT(m != NULL);
1980 	KASSERTMSG(in6p == NULL || in6p->in6p_socket != NULL,
1981 	    "socket w/o inpcb");
1982 
1983 	/* get SP for this packet */
1984 	/* XXX Is it right to call with IP_FORWARDING. */
1985 	if (in6p == NULL)
1986 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1987 	else
1988 		sp = ipsec_getpolicybysock(m, dir,
1989 			(struct inpcb_hdr *)in6p,
1990 			&error);
1991 
1992 	if (sp == NULL)
1993 		return 0;
1994 	size = ipsec_hdrsiz(sp, m);
1995 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
1996 	KEY_SP_UNREF(&sp);
1997 
1998 	return size;
1999 }
2000 #endif /*INET6*/
2001 
2002 /*
2003  * Check the variable replay window.
2004  * ipsec_chkreplay() performs replay check before ICV verification.
2005  * ipsec_updatereplay() updates replay bitmap.  This must be called after
2006  * ICV verification (it also performs replay check, which is usually done
2007  * beforehand).
2008  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2009  *
2010  * based on RFC 2401.
2011  */
2012 int
2013 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
2014 {
2015 	const struct secreplay *replay;
2016 	u_int32_t diff;
2017 	int fr;
2018 	u_int32_t wsizeb;	/* constant: bits of window size */
2019 	int frlast;		/* constant: last frame */
2020 
2021 	IPSEC_SPLASSERT_SOFTNET(__func__);
2022 
2023 	KASSERT(sav != NULL);
2024 	KASSERT(sav->replay != NULL);
2025 
2026 	replay = sav->replay;
2027 
2028 	if (replay->wsize == 0)
2029 		return 1;	/* no need to check replay. */
2030 
2031 	/* constant */
2032 	frlast = replay->wsize - 1;
2033 	wsizeb = replay->wsize << 3;
2034 
2035 	/* sequence number of 0 is invalid */
2036 	if (seq == 0)
2037 		return 0;
2038 
2039 	/* first time is always okay */
2040 	if (replay->count == 0)
2041 		return 1;
2042 
2043 	if (seq > replay->lastseq) {
2044 		/* larger sequences are okay */
2045 		return 1;
2046 	} else {
2047 		/* seq is equal or less than lastseq. */
2048 		diff = replay->lastseq - seq;
2049 
2050 		/* over range to check, i.e. too old or wrapped */
2051 		if (diff >= wsizeb)
2052 			return 0;
2053 
2054 		fr = frlast - diff / 8;
2055 
2056 		/* this packet already seen ? */
2057 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2058 			return 0;
2059 
2060 		/* out of order but good */
2061 		return 1;
2062 	}
2063 }
2064 
2065 /*
2066  * check replay counter whether to update or not.
2067  * OUT:	0:	OK
2068  *	1:	NG
2069  */
2070 int
2071 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
2072 {
2073 	struct secreplay *replay;
2074 	u_int32_t diff;
2075 	int fr;
2076 	u_int32_t wsizeb;	/* constant: bits of window size */
2077 	int frlast;		/* constant: last frame */
2078 
2079 	IPSEC_SPLASSERT_SOFTNET(__func__);
2080 
2081 	KASSERT(sav != NULL);
2082 	KASSERT(sav->replay != NULL);
2083 
2084 	replay = sav->replay;
2085 
2086 	if (replay->wsize == 0)
2087 		goto ok;	/* no need to check replay. */
2088 
2089 	/* constant */
2090 	frlast = replay->wsize - 1;
2091 	wsizeb = replay->wsize << 3;
2092 
2093 	/* sequence number of 0 is invalid */
2094 	if (seq == 0)
2095 		return 1;
2096 
2097 	/* first time */
2098 	if (replay->count == 0) {
2099 		replay->lastseq = seq;
2100 		memset(replay->bitmap, 0, replay->wsize);
2101 		(replay->bitmap)[frlast] = 1;
2102 		goto ok;
2103 	}
2104 
2105 	if (seq > replay->lastseq) {
2106 		/* seq is larger than lastseq. */
2107 		diff = seq - replay->lastseq;
2108 
2109 		/* new larger sequence number */
2110 		if (diff < wsizeb) {
2111 			/* In window */
2112 			/* set bit for this packet */
2113 			vshiftl(replay->bitmap, diff, replay->wsize);
2114 			(replay->bitmap)[frlast] |= 1;
2115 		} else {
2116 			/* this packet has a "way larger" */
2117 			memset(replay->bitmap, 0, replay->wsize);
2118 			(replay->bitmap)[frlast] = 1;
2119 		}
2120 		replay->lastseq = seq;
2121 
2122 		/* larger is good */
2123 	} else {
2124 		/* seq is equal or less than lastseq. */
2125 		diff = replay->lastseq - seq;
2126 
2127 		/* over range to check, i.e. too old or wrapped */
2128 		if (diff >= wsizeb)
2129 			return 1;
2130 
2131 		fr = frlast - diff / 8;
2132 
2133 		/* this packet already seen ? */
2134 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2135 			return 1;
2136 
2137 		/* mark as seen */
2138 		(replay->bitmap)[fr] |= (1 << (diff % 8));
2139 
2140 		/* out of order but good */
2141 	}
2142 
2143 ok:
2144 	if (replay->count == ~0) {
2145 		char buf[IPSEC_LOGSASTRLEN];
2146 
2147 		/* set overflow flag */
2148 		replay->overflow++;
2149 
2150 		/* don't increment, no more packets accepted */
2151 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
2152 			return 1;
2153 
2154 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
2155 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
2156 	}
2157 
2158 	replay->count++;
2159 
2160 	return 0;
2161 }
2162 
2163 /*
2164  * shift variable length bunffer to left.
2165  * IN:	bitmap: pointer to the buffer
2166  * 	nbit:	the number of to shift.
2167  *	wsize:	buffer size (bytes).
2168  */
2169 static void
2170 vshiftl(unsigned char *bitmap, int nbit, int wsize)
2171 {
2172 	int s, j, i;
2173 	unsigned char over;
2174 
2175 	for (j = 0; j < nbit; j += 8) {
2176 		s = (nbit - j < 8) ? (nbit - j): 8;
2177 		bitmap[0] <<= s;
2178 		for (i = 1; i < wsize; i++) {
2179 			over = (bitmap[i] >> (8 - s));
2180 			bitmap[i] <<= s;
2181 			bitmap[i-1] |= over;
2182 		}
2183 	}
2184 
2185 	return;
2186 }
2187 
2188 /* Return a printable string for the address. */
2189 const char *
2190 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
2191 {
2192 	switch (sa->sa.sa_family) {
2193 #if INET
2194 	case AF_INET:
2195 		in_print(buf, size, &sa->sin.sin_addr);
2196 		return buf;
2197 #endif /* INET */
2198 
2199 #if INET6
2200 	case AF_INET6:
2201 		in6_print(buf, size, &sa->sin6.sin6_addr);
2202 		return buf;
2203 #endif /* INET6 */
2204 
2205 	default:
2206 		return "(unknown address family)";
2207 	}
2208 }
2209 
2210 const char *
2211 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
2212 {
2213 	const struct secasindex *saidx = &sav->sah->saidx;
2214 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
2215 
2216 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
2217 	    "af family mismatch, src %u, dst %u",
2218 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
2219 
2220 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
2221 	    (u_int32_t)ntohl(sav->spi),
2222 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
2223 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
2224 
2225 	return buf;
2226 }
2227 
2228 void
2229 ipsec_dumpmbuf(struct mbuf *m)
2230 {
2231 	int totlen;
2232 	int i;
2233 	u_char *p;
2234 
2235 	totlen = 0;
2236 	printf("---\n");
2237 	while (m) {
2238 		p = mtod(m, u_char *);
2239 		for (i = 0; i < m->m_len; i++) {
2240 			printf("%02x ", p[i]);
2241 			totlen++;
2242 			if (totlen % 16 == 0)
2243 				printf("\n");
2244 		}
2245 		m = m->m_next;
2246 	}
2247 	if (totlen % 16 != 0)
2248 		printf("\n");
2249 	printf("---\n");
2250 }
2251 
2252 #ifdef INET6
2253 struct secpolicy *
2254 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p,
2255 		    int flags, int *needipsecp, int *errorp)
2256 {
2257 	struct secpolicy *sp = NULL;
2258 	int s;
2259 	int error = 0;
2260 	int needipsec = 0;
2261 
2262 	if (!ipsec_outdone(m)) {
2263 		s = splsoftnet();
2264 		if (in6p != NULL &&
2265 		    ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
2266 			splx(s);
2267 			goto skippolicycheck;
2268 		}
2269 		sp = ipsec6_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,in6p);
2270 
2271 		/*
2272 		 * There are four return cases:
2273 		 *	sp != NULL			apply IPsec policy
2274 		 *	sp == NULL, error == 0		no IPsec handling needed
2275 		 *	sp == NULL, error == -EINVAL  discard packet w/o error
2276 		 *	sp == NULL, error != 0		discard packet, report error
2277 		 */
2278 
2279 		splx(s);
2280 		if (sp == NULL) {
2281 			/*
2282 			 * Caller must check the error return to see if it needs to discard
2283 			 * the packet.
2284 			 */
2285 			needipsec = 0;
2286 		} else {
2287 			needipsec = 1;
2288 		}
2289 	}
2290 skippolicycheck:;
2291 
2292 	*errorp = error;
2293 	*needipsecp = needipsec;
2294 	return sp;
2295 }
2296 
2297 int
2298 ipsec6_input(struct mbuf *m)
2299 {
2300 	struct secpolicy *sp;
2301 	int s, error;
2302 
2303 	s = splsoftnet();
2304 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
2305 	if (sp != NULL) {
2306 		/*
2307 		 * Check security policy against packet
2308 		 * attributes.
2309 		 */
2310 		error = ipsec_in_reject(sp, m);
2311 		KEY_SP_UNREF(&sp);
2312 	} else {
2313 		/* XXX error stat??? */
2314 		error = EINVAL;
2315 		IPSECLOG(LOG_DEBUG, "no SP, packet discarded\n");/*XXX*/
2316 	}
2317 	splx(s);
2318 
2319 	return error;
2320 }
2321 #endif /* INET6 */
2322 
2323 
2324 
2325 /* XXX this stuff doesn't belong here... */
2326 
2327 static	struct xformsw *xforms = NULL;
2328 
2329 /*
2330  * Register a transform; typically at system startup.
2331  */
2332 void
2333 xform_register(struct xformsw *xsp)
2334 {
2335 	xsp->xf_next = xforms;
2336 	xforms = xsp;
2337 }
2338 
2339 /*
2340  * Initialize transform support in an sav.
2341  */
2342 int
2343 xform_init(struct secasvar *sav, int xftype)
2344 {
2345 	struct xformsw *xsp;
2346 
2347 	if (sav->tdb_xform != NULL)	/* previously initialized */
2348 		return 0;
2349 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
2350 		if (xsp->xf_type == xftype)
2351 			return (*xsp->xf_init)(sav, xsp);
2352 
2353 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
2354 	return EINVAL;
2355 }
2356 
2357 void
2358 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport) {
2359 	struct m_tag *tag;
2360 
2361 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
2362 		*sport = ((u_int16_t *)(tag + 1))[0];
2363 		*dport = ((u_int16_t *)(tag + 1))[1];
2364 	} else
2365 		*sport = *dport = 0;
2366 }
2367 
2368 /*
2369  * XXXJRT This should be done as a protosw init call.
2370  */
2371 void
2372 ipsec_attach(void)
2373 {
2374 
2375 	ipsec_output_init();
2376 
2377 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
2378 
2379 	sysctl_net_inet_ipsec_setup(NULL);
2380 #ifdef INET6
2381 	sysctl_net_inet6_ipsec6_setup(NULL);
2382 #endif
2383 
2384 	ah_attach();
2385 	esp_attach();
2386 	ipcomp_attach();
2387 	ipe4_attach();
2388 #ifdef TCP_SIGNATURE
2389 	tcpsignature_attach();
2390 #endif
2391 }
2392