xref: /netbsd-src/sys/netipsec/ipsec.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: ipsec.c,v 1.173 2021/12/08 20:03:26 andvar Exp $ */
2 /* $FreeBSD: 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.173 2021/12/08 20:03:26 andvar 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 #include <netinet/in_offload.h>
85 #ifdef INET6
86 #include <netinet6/in6_pcb.h>
87 #include <netinet/icmp6.h>
88 #endif
89 
90 #include <netipsec/ipsec.h>
91 #include <netipsec/ipsec_var.h>
92 #include <netipsec/ipsec_private.h>
93 #ifdef INET6
94 #include <netipsec/ipsec6.h>
95 #endif
96 #include <netipsec/ah_var.h>
97 #include <netipsec/esp_var.h>
98 #include <netipsec/ipcomp.h>		/*XXX*/
99 #include <netipsec/ipcomp_var.h>
100 
101 #include <netipsec/key.h>
102 #include <netipsec/keydb.h>
103 #include <netipsec/key_debug.h>
104 
105 #include <netipsec/xform.h>
106 
107 int ipsec_used = 0;
108 int ipsec_enabled = 1;
109 
110 #ifdef IPSEC_DEBUG
111 int ipsec_debug = 1;
112 
113 /*
114  * When set to 1, IPsec will send packets with the same sequence number.
115  * This allows to verify if the other side has proper replay attacks detection.
116  */
117 int ipsec_replay = 0;
118 
119 /*
120  * When set 1, IPsec will send packets with corrupted HMAC.
121  * This allows to verify if the other side properly detects modified packets.
122  */
123 int ipsec_integrity = 0;
124 #else
125 int ipsec_debug = 0;
126 #endif
127 
128 percpu_t *ipsecstat_percpu;
129 
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 
139 u_int ipsec_spdgen = 1;		/* SPD generation # */
140 
141 static struct secpolicy ipsec_dummy_sp __read_mostly = {
142 	.state		= IPSEC_SPSTATE_ALIVE,
143 	/* If ENTRUST, the dummy SP never be used. See ipsec_getpolicybysock. */
144 	.policy		= IPSEC_POLICY_ENTRUST,
145 };
146 
147 static struct secpolicy *ipsec_checkpcbcache(struct mbuf *,
148     struct inpcbpolicy *, int);
149 static int ipsec_fillpcbcache(struct inpcbpolicy *, struct mbuf *,
150     struct secpolicy *, int);
151 static int ipsec_invalpcbcache(struct inpcbpolicy *, int);
152 
153 /*
154  * Crypto support requirements:
155  *
156  *  1	require hardware support
157  * -1	require software support
158  *  0	take anything
159  */
160 int crypto_support = 0;
161 
162 static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
163     struct inpcb_hdr *, int *);
164 
165 #ifdef INET6
166 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
167 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
168 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
169 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
170 struct secpolicy ip6_def_policy;
171 int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
172 #endif
173 
174 static int ipsec_setspidx_inpcb(struct mbuf *, void *);
175 static int ipsec_setspidx(struct mbuf *, struct secpolicyindex *, int, int);
176 static void ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
177 static int ipsec4_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
178 #ifdef INET6
179 static void ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
180 static int ipsec6_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
181 #endif
182 static void ipsec_delpcbpolicy(struct inpcbpolicy *);
183 static void ipsec_destroy_policy(struct secpolicy *);
184 static int ipsec_sp_reject(const struct secpolicy *, const struct mbuf *);
185 static void vshiftl(unsigned char *, int, int);
186 static size_t ipsec_sp_hdrsiz(const struct secpolicy *, const struct mbuf *);
187 
188 /*
189  * Try to validate and use cached policy on a PCB.
190  */
191 static struct secpolicy *
192 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
193 {
194 	struct secpolicyindex spidx;
195 	struct secpolicy *sp = NULL;
196 	int s;
197 
198 	KASSERT(IPSEC_DIR_IS_VALID(dir));
199 	KASSERT(pcbsp != NULL);
200 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
201 	KASSERT(inph_locked(pcbsp->sp_inph));
202 
203 	/*
204 	 * Checking the generation and sp->state and taking a reference to an SP
205 	 * must be in a critical section of pserialize. See key_unlink_sp.
206 	 */
207 	s = pserialize_read_enter();
208 	/* SPD table change invalidate all the caches. */
209 	if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
210 		ipsec_invalpcbcache(pcbsp, dir);
211 		goto out;
212 	}
213 	sp = pcbsp->sp_cache[dir].cachesp;
214 	if (sp == NULL)
215 		goto out;
216 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
217 		sp = NULL;
218 		ipsec_invalpcbcache(pcbsp, dir);
219 		goto out;
220 	}
221 	if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
222 		/* NB: assume ipsec_setspidx never sleep */
223 		if (ipsec_setspidx(m, &spidx, dir, 1) != 0) {
224 			sp = NULL;
225 			goto out;
226 		}
227 
228 		/*
229 		 * We have to make an exact match here since the cached rule
230 		 * might have lower priority than a rule that would otherwise
231 		 * have matched the packet.
232 		 */
233 		if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
234 		    sizeof(spidx))) {
235 			sp = NULL;
236 			goto out;
237 		}
238 	} else {
239 		/*
240 		 * The pcb is connected, and the L4 code is sure that:
241 		 * - outgoing side uses inp_[lf]addr
242 		 * - incoming side looks up policy after inpcb lookup
243 		 * and address pair is know to be stable.  We do not need
244 		 * to generate spidx again, nor check the address match again.
245 		 *
246 		 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
247 		 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
248 		 */
249 	}
250 
251 	sp->lastused = time_second;
252 	KEY_SP_REF(sp);
253 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
254 	    "DP cause refcnt++:%d SP:%p\n",
255 	    key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
256 out:
257 	pserialize_read_exit(s);
258 	return sp;
259 }
260 
261 static int
262 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
263     struct secpolicy *sp, int dir)
264 {
265 
266 	KASSERT(IPSEC_DIR_IS_INOROUT(dir));
267 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
268 	KASSERT(inph_locked(pcbsp->sp_inph));
269 
270 	pcbsp->sp_cache[dir].cachesp = NULL;
271 	pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
272 	if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, dir, 1) != 0) {
273 		return EINVAL;
274 	}
275 	pcbsp->sp_cache[dir].cachesp = sp;
276 	if (pcbsp->sp_cache[dir].cachesp) {
277 		/*
278 		 * If the PCB is connected, we can remember a hint to
279 		 * possibly short-circuit IPsec processing in other places.
280 		 */
281 		if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
282 			switch (pcbsp->sp_cache[dir].cachesp->policy) {
283 			case IPSEC_POLICY_NONE:
284 			case IPSEC_POLICY_BYPASS:
285 				pcbsp->sp_cache[dir].cachehint =
286 				    IPSEC_PCBHINT_NO;
287 				break;
288 			default:
289 				pcbsp->sp_cache[dir].cachehint =
290 				    IPSEC_PCBHINT_YES;
291 			}
292 		}
293 	}
294 	pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
295 
296 	return 0;
297 }
298 
299 static int
300 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
301 {
302 	int i;
303 
304 	KASSERT(inph_locked(pcbsp->sp_inph));
305 
306 	for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
307 		if (dir != IPSEC_DIR_ANY && i != dir)
308 			continue;
309 		pcbsp->sp_cache[i].cachesp = NULL;
310 		pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
311 		pcbsp->sp_cache[i].cachegen = 0;
312 		memset(&pcbsp->sp_cache[i].cacheidx, 0,
313 		    sizeof(pcbsp->sp_cache[i].cacheidx));
314 	}
315 	return 0;
316 }
317 
318 void
319 ipsec_pcbconn(struct inpcbpolicy *pcbsp)
320 {
321 
322 	KASSERT(inph_locked(pcbsp->sp_inph));
323 
324 	pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
325 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
326 }
327 
328 void
329 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
330 {
331 
332 	KASSERT(inph_locked(pcbsp->sp_inph));
333 
334 	pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
335 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
336 }
337 
338 void
339 ipsec_invalpcbcacheall(void)
340 {
341 
342 	if (ipsec_spdgen == UINT_MAX)
343 		ipsec_spdgen = 1;
344 	else
345 		ipsec_spdgen++;
346 }
347 
348 /*
349  * Return a held reference to the default SP.
350  */
351 static struct secpolicy *
352 key_get_default_sp(int af, const char *where, int tag)
353 {
354 	struct secpolicy *sp;
355 
356 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
357 
358 	switch(af) {
359 	case AF_INET:
360 		sp = &ip4_def_policy;
361 		break;
362 #ifdef INET6
363 	case AF_INET6:
364 		sp = &ip6_def_policy;
365 		break;
366 #endif
367 	default:
368 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
369 		    "unexpected protocol family %u\n", af);
370 		return NULL;
371 	}
372 
373 	if (sp->policy != IPSEC_POLICY_DISCARD &&
374 	    sp->policy != IPSEC_POLICY_NONE) {
375 		IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
376 		    sp->policy, IPSEC_POLICY_NONE);
377 		sp->policy = IPSEC_POLICY_NONE;
378 	}
379 	KEY_SP_REF(sp);
380 
381 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
382 	    sp, key_sp_refcnt(sp));
383 	return sp;
384 }
385 
386 #define	KEY_GET_DEFAULT_SP(af) \
387 	key_get_default_sp((af), __func__, __LINE__)
388 
389 /*
390  * For OUTBOUND packet having a socket. Searching SPD for packet,
391  * and return a pointer to SP.
392  * OUT:	NULL:	no appropriate SP found, the following value is set to error.
393  *		0	: bypass
394  *		EACCES	: discard packet.
395  *		ENOENT	: ipsec_acquire() in progress, maybe.
396  *		others	: error occurred.
397  *	others:	a pointer to SP
398  *
399  * NOTE: IPv6 mapped address concern is implemented here.
400  */
401 static struct secpolicy *
402 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph,
403     int *error)
404 {
405 	struct inpcbpolicy *pcbsp = NULL;
406 	struct secpolicy *currsp = NULL;	/* policy on socket */
407 	struct secpolicy *sp;
408 	int af;
409 
410 	KASSERT(m != NULL);
411 	KASSERT(inph != NULL);
412 	KASSERT(error != NULL);
413 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
414 
415 	KASSERT(inph->inph_socket != NULL);
416 	KASSERT(inph_locked(inph));
417 
418 	/* XXX FIXME inpcb/in6pcb vs socket*/
419 	af = inph->inph_af;
420 	KASSERTMSG(af == AF_INET || af == AF_INET6,
421 	    "unexpected protocol family %u", af);
422 
423 	KASSERT(inph->inph_sp != NULL);
424 	/* If we have a cached entry, and if it is still valid, use it. */
425 	IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
426 	currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir);
427 	if (currsp) {
428 		*error = 0;
429 		return currsp;
430 	}
431 	IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
432 
433 	switch (af) {
434 	case AF_INET:
435 #if defined(INET6)
436 	case AF_INET6:
437 #endif
438 		*error = ipsec_setspidx_inpcb(m, inph);
439 		pcbsp = inph->inph_sp;
440 		break;
441 	default:
442 		*error = EPFNOSUPPORT;
443 		break;
444 	}
445 	if (*error)
446 		return NULL;
447 
448 	KASSERT(pcbsp != NULL);
449 	switch (dir) {
450 	case IPSEC_DIR_INBOUND:
451 		currsp = pcbsp->sp_in;
452 		break;
453 	case IPSEC_DIR_OUTBOUND:
454 		currsp = pcbsp->sp_out;
455 		break;
456 	}
457 	KASSERT(currsp != NULL);
458 
459 	if (pcbsp->priv) {	/* when privileged socket */
460 		switch (currsp->policy) {
461 		case IPSEC_POLICY_BYPASS:
462 		case IPSEC_POLICY_IPSEC:
463 			KEY_SP_REF(currsp);
464 			sp = currsp;
465 			break;
466 
467 		case IPSEC_POLICY_ENTRUST:
468 			/* look for a policy in SPD */
469 			if (key_havesp(dir))
470 				sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
471 			else
472 				sp = NULL;
473 			if (sp == NULL)		/* no SP found */
474 				sp = KEY_GET_DEFAULT_SP(af);
475 			break;
476 
477 		default:
478 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
479 			    currsp->policy);
480 			*error = EINVAL;
481 			return NULL;
482 		}
483 	} else {				/* unpriv, SPD has policy */
484 		if (key_havesp(dir))
485 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
486 		else
487 			sp = NULL;
488 		if (sp == NULL) {		/* no SP found */
489 			switch (currsp->policy) {
490 			case IPSEC_POLICY_BYPASS:
491 				IPSECLOG(LOG_ERR, "Illegal policy for "
492 				    "non-priviliged defined %d\n",
493 				    currsp->policy);
494 				*error = EINVAL;
495 				return NULL;
496 
497 			case IPSEC_POLICY_ENTRUST:
498 				sp = KEY_GET_DEFAULT_SP(af);
499 				break;
500 
501 			case IPSEC_POLICY_IPSEC:
502 				KEY_SP_REF(currsp);
503 				sp = currsp;
504 				break;
505 
506 			default:
507 				IPSECLOG(LOG_ERR, "Invalid policy for "
508 				    "PCB %d\n", currsp->policy);
509 				*error = EINVAL;
510 				return NULL;
511 			}
512 		}
513 	}
514 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
515 	    currsp->policy);
516 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
517 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
518 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
519 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
520 	return sp;
521 }
522 
523 /*
524  * For FORWARDING packet or OUTBOUND without a socket. Searching SPD for packet,
525  * and return a pointer to SP.
526  * OUT:	positive: a pointer to the entry for security policy leaf matched.
527  *	NULL:	no appropriate SP found, the following value is set to error.
528  *		0	: bypass
529  *		EACCES	: discard packet.
530  *		ENOENT	: ipsec_acquire() in progress, maybe.
531  *		others	: error occurred.
532  */
533 static struct secpolicy *
534 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
535 {
536 	struct secpolicyindex spidx;
537 	struct secpolicy *sp;
538 
539 	KASSERT(m != NULL);
540 	KASSERT(error != NULL);
541 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
542 
543 	sp = NULL;
544 
545 	/* Make an index to look for a policy. */
546 	*error = ipsec_setspidx(m, &spidx, dir, (flag & IP_FORWARDING) ? 0 : 1);
547 	if (*error != 0) {
548 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
549 		memset(&spidx, 0, sizeof(spidx));
550 		return NULL;
551 	}
552 
553 	spidx.dir = dir;
554 
555 	if (key_havesp(dir)) {
556 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
557 	}
558 	if (sp == NULL) {
559 		/* no SP found, use system default */
560 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
561 	}
562 
563 	KASSERT(sp != NULL);
564 	return sp;
565 }
566 
567 static struct secpolicy *
568 ipsec_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
569     void *inp)
570 {
571 	struct secpolicy *sp;
572 
573 	*error = 0;
574 
575 	if (inp == NULL) {
576 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
577 	} else {
578 		struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
579 		KASSERT(inph->inph_socket != NULL);
580 		sp = ipsec_getpolicybysock(m, dir, inph, error);
581 	}
582 	if (sp == NULL) {
583 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
584 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
585 		return NULL;
586 	}
587 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
588 
589 	switch (sp->policy) {
590 	case IPSEC_POLICY_ENTRUST:
591 	default:
592 		printf("%s: invalid policy %u\n", __func__, sp->policy);
593 		/* fall thru... */
594 	case IPSEC_POLICY_DISCARD:
595 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
596 		*error = -EINVAL;	/* packet is discarded by caller */
597 		break;
598 	case IPSEC_POLICY_BYPASS:
599 	case IPSEC_POLICY_NONE:
600 		KEY_SP_UNREF(&sp);
601 		sp = NULL;		/* NB: force NULL result */
602 		break;
603 	case IPSEC_POLICY_IPSEC:
604 		KASSERT(sp->req != NULL);
605 		break;
606 	}
607 
608 	if (*error != 0) {
609 		KEY_SP_UNREF(&sp);
610 		sp = NULL;
611 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
612 	}
613 
614 	return sp;
615 }
616 
617 int
618 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
619     u_long *mtu, bool *natt_frag, bool *done, bool *count_drop)
620 {
621 	struct secpolicy *sp = NULL;
622 	u_long _mtu = 0;
623 	int error, s;
624 
625 	/*
626 	 * Check the security policy (SP) for the packet and, if required,
627 	 * do IPsec-related processing.  There are two cases here; the first
628 	 * time a packet is sent through it will be untagged and handled by
629 	 * ipsec_checkpolicy().  If the packet is resubmitted to ip_output
630 	 * (e.g. after AH, ESP, etc. processing), there will be a tag to
631 	 * bypass the lookup and related policy checking.
632 	 */
633 	if (ipsec_outdone(m)) {
634 		return 0;
635 	}
636 	s = splsoftnet();
637 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
638 		splx(s);
639 		return 0;
640 	}
641 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
642 
643 	/*
644 	 * There are four return cases:
645 	 *	sp != NULL                    apply IPsec policy
646 	 *	sp == NULL, error == 0        no IPsec handling needed
647 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
648 	 *	sp == NULL, error != 0        discard packet, report error
649 	 */
650 	if (sp == NULL) {
651 		splx(s);
652 		if (error) {
653 			/*
654 			 * Hack: -EINVAL is used to signal that a packet
655 			 * should be silently discarded.  This is typically
656 			 * because we asked key management for an SA and
657 			 * it was delayed (e.g. kicked up to IKE).
658 			 */
659 			if (error == -EINVAL)
660 				error = 0;
661 			m_freem(m);
662 			*done = true;
663 			*count_drop = true;
664 			return error;
665 		}
666 		/* No IPsec processing for this packet. */
667 		return 0;
668 	}
669 
670 	/*
671 	 * Do delayed checksums now because we send before
672 	 * this is done in the normal processing path.
673 	 */
674 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
675 		in_undefer_cksum_tcpudp(m);
676 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
677 	}
678 
679 	error = ipsec4_process_packet(m, sp->req, &_mtu);
680 	if (error == 0 && _mtu != 0) {
681 		/*
682 		 * NAT-T ESP fragmentation: do not do IPSec processing
683 		 * now, we will do it on each fragmented packet.
684 		 */
685 		*mtu = _mtu;
686 		*natt_frag = true;
687 		KEY_SP_UNREF(&sp);
688 		splx(s);
689 		return 0;
690 	}
691 
692 	/*
693 	 * Preserve KAME behaviour: ENOENT can be returned
694 	 * when an SA acquire is in progress.  Don't propagate
695 	 * this to user-level; it confuses applications.
696 	 *
697 	 * XXX this will go away when the SADB is redone.
698 	 */
699 	if (error == ENOENT)
700 		error = 0;
701 	KEY_SP_UNREF(&sp);
702 	splx(s);
703 	*done = true;
704 	return error;
705 }
706 
707 int
708 ipsec_ip_input_checkpolicy(struct mbuf *m, bool forward)
709 {
710 	struct secpolicy *sp;
711 	int error, s;
712 
713 	s = splsoftnet();
714 	error = ipsec_in_reject(m, NULL);
715 	splx(s);
716 	if (error) {
717 		return EINVAL;
718 	}
719 
720 	if (!forward || !(m->m_flags & M_CANFASTFWD)) {
721 		return 0;
722 	}
723 
724 	/*
725 	 * Peek at the outbound SP for this packet to determine if
726 	 * it is a Fast Forward candidate.
727 	 */
728 	s = splsoftnet();
729 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
730 	    &error, NULL);
731 	if (sp != NULL) {
732 		m->m_flags &= ~M_CANFASTFWD;
733 		KEY_SP_UNREF(&sp);
734 	}
735 	splx(s);
736 
737 	return 0;
738 }
739 
740 /*
741  * If the packet is routed over IPsec tunnel, tell the originator the
742  * tunnel MTU.
743  *     tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
744  *
745  * XXX: Quick hack!!!
746  *
747  * XXX: And what if the MTU goes negative?
748  */
749 void
750 ipsec_mtu(struct mbuf *m, int *destmtu)
751 {
752 	struct secpolicy *sp;
753 	size_t ipsechdr;
754 	int error;
755 
756 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
757 	    &error);
758 	if (sp == NULL) {
759 		return;
760 	}
761 
762 	/* Count IPsec header size. */
763 	ipsechdr = ipsec_sp_hdrsiz(sp, m);
764 
765 	/*
766 	 * Find the correct route for outer IP header, compute tunnel MTU.
767 	 */
768 	if (sp->req) {
769 		struct secasvar *sav;
770 
771 		sav = ipsec_lookup_sa(sp->req, m);
772 		if (sav != NULL) {
773 			struct route *ro;
774 			struct rtentry *rt;
775 
776 			ro = &sav->sah->sa_route;
777 			rt = rtcache_validate(ro);
778 			if (rt && rt->rt_ifp) {
779 				*destmtu = rt->rt_rmx.rmx_mtu ?
780 				    rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
781 				*destmtu -= ipsechdr;
782 			}
783 			rtcache_unref(rt, ro);
784 			KEY_SA_UNREF(&sav);
785 		}
786 	}
787 	KEY_SP_UNREF(&sp);
788 }
789 
790 static int
791 ipsec_setspidx_inpcb(struct mbuf *m, void *pcb)
792 {
793 	struct inpcb_hdr *inph = (struct inpcb_hdr *)pcb;
794 	int error;
795 
796 	KASSERT(inph != NULL);
797 	KASSERT(inph->inph_sp != NULL);
798 	KASSERT(inph->inph_sp->sp_out != NULL);
799 	KASSERT(inph->inph_sp->sp_in != NULL);
800 
801 	error = ipsec_setspidx(m, &inph->inph_sp->sp_in->spidx,
802 	    IPSEC_DIR_INBOUND, 1);
803 	if (error == 0) {
804 		inph->inph_sp->sp_out->spidx = inph->inph_sp->sp_in->spidx;
805 		inph->inph_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
806 	} else {
807 		memset(&inph->inph_sp->sp_in->spidx, 0,
808 		    sizeof(inph->inph_sp->sp_in->spidx));
809 		memset(&inph->inph_sp->sp_out->spidx, 0,
810 		    sizeof(inph->inph_sp->sp_out->spidx));
811 	}
812 	return error;
813 }
814 
815 /*
816  * configure security policy index (src/dst/proto/sport/dport)
817  * by looking at the content of mbuf.
818  * the caller is responsible for error recovery (like clearing up spidx).
819  */
820 static int
821 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int dir,
822     int needport)
823 {
824 	struct ip *ip = NULL;
825 	struct ip ipbuf;
826 	u_int v;
827 	int error;
828 
829 	KASSERT(m != NULL);
830 	M_VERIFY_PACKET(m);
831 
832 	if (m->m_pkthdr.len < sizeof(struct ip)) {
833 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
834 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
835 		    m->m_pkthdr.len);
836 		return EINVAL;
837 	}
838 
839 	memset(spidx, 0, sizeof(*spidx));
840 	spidx->dir = dir;
841 
842 	if (m->m_len >= sizeof(*ip)) {
843 		ip = mtod(m, struct ip *);
844 	} else {
845 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
846 		ip = &ipbuf;
847 	}
848 	v = ip->ip_v;
849 	switch (v) {
850 	case 4:
851 		error = ipsec4_setspidx_ipaddr(m, spidx);
852 		if (error)
853 			return error;
854 		ipsec4_get_ulp(m, spidx, needport);
855 		return 0;
856 #ifdef INET6
857 	case 6:
858 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
859 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
860 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
861 			    "ignored.\n", m->m_pkthdr.len);
862 			return EINVAL;
863 		}
864 		error = ipsec6_setspidx_ipaddr(m, spidx);
865 		if (error)
866 			return error;
867 		ipsec6_get_ulp(m, spidx, needport);
868 		return 0;
869 #endif
870 	default:
871 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
872 		    "unknown IP version %u, ignored.\n", v);
873 		return EINVAL;
874 	}
875 }
876 
877 static void
878 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
879 {
880 	u_int8_t nxt;
881 	int off;
882 
883 	KASSERT(m != NULL);
884 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
885 
886 	/* NB: ip_input() flips it into host endian XXX need more checking */
887 	if (m->m_len >= sizeof(struct ip)) {
888 		struct ip *ip = mtod(m, struct ip *);
889 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
890 			goto done;
891 		off = ip->ip_hl << 2;
892 		nxt = ip->ip_p;
893 	} else {
894 		struct ip ih;
895 
896 		m_copydata(m, 0, sizeof(struct ip), &ih);
897 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
898 			goto done;
899 		off = ih.ip_hl << 2;
900 		nxt = ih.ip_p;
901 	}
902 
903 	while (off < m->m_pkthdr.len) {
904 		struct ip6_ext ip6e;
905 		struct tcphdr th;
906 		struct udphdr uh;
907 		struct icmp icmph;
908 
909 		switch (nxt) {
910 		case IPPROTO_TCP:
911 			spidx->ul_proto = nxt;
912 			if (!needport)
913 				goto done_proto;
914 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
915 				goto done;
916 			m_copydata(m, off, sizeof(th), &th);
917 			spidx->src.sin.sin_port = th.th_sport;
918 			spidx->dst.sin.sin_port = th.th_dport;
919 			return;
920 		case IPPROTO_UDP:
921 			spidx->ul_proto = nxt;
922 			if (!needport)
923 				goto done_proto;
924 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
925 				goto done;
926 			m_copydata(m, off, sizeof(uh), &uh);
927 			spidx->src.sin.sin_port = uh.uh_sport;
928 			spidx->dst.sin.sin_port = uh.uh_dport;
929 			return;
930 		case IPPROTO_AH:
931 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
932 				goto done;
933 			/* XXX sigh, this works but is totally bogus */
934 			m_copydata(m, off, sizeof(ip6e), &ip6e);
935 			off += (ip6e.ip6e_len + 2) << 2;
936 			nxt = ip6e.ip6e_nxt;
937 			break;
938 		case IPPROTO_ICMP:
939 			spidx->ul_proto = nxt;
940 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
941 				goto done;
942 			m_copydata(m, off, sizeof(icmph), &icmph);
943 			((struct sockaddr_in *)&spidx->src)->sin_port =
944 			    htons((uint16_t)icmph.icmp_type);
945 			((struct sockaddr_in *)&spidx->dst)->sin_port =
946 			    htons((uint16_t)icmph.icmp_code);
947 			return;
948 		default:
949 			/* XXX intermediate headers??? */
950 			spidx->ul_proto = nxt;
951 			goto done_proto;
952 		}
953 	}
954 done:
955 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
956 done_proto:
957 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
958 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
959 }
960 
961 static int
962 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
963 {
964 	static const struct sockaddr_in template = {
965 		sizeof(struct sockaddr_in),
966 		AF_INET,
967 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
968 	};
969 
970 	spidx->src.sin = template;
971 	spidx->dst.sin = template;
972 
973 	if (m->m_len < sizeof(struct ip)) {
974 		m_copydata(m, offsetof(struct ip, ip_src),
975 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
976 		m_copydata(m, offsetof(struct ip, ip_dst),
977 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
978 	} else {
979 		struct ip *ip = mtod(m, struct ip *);
980 		spidx->src.sin.sin_addr = ip->ip_src;
981 		spidx->dst.sin.sin_addr = ip->ip_dst;
982 	}
983 
984 	spidx->prefs = sizeof(struct in_addr) << 3;
985 	spidx->prefd = sizeof(struct in_addr) << 3;
986 
987 	return 0;
988 }
989 
990 #ifdef INET6
991 static void
992 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
993 {
994 	int off, nxt;
995 	struct tcphdr th;
996 	struct udphdr uh;
997 	struct icmp6_hdr icmph;
998 
999 	KASSERT(m != NULL);
1000 
1001 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1002 		kdebug_mbuf(__func__, m);
1003 	}
1004 
1005 	/* set default */
1006 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
1007 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
1008 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
1009 
1010 	nxt = -1;
1011 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
1012 	if (off < 0 || m->m_pkthdr.len < off)
1013 		return;
1014 
1015 	switch (nxt) {
1016 	case IPPROTO_TCP:
1017 		spidx->ul_proto = nxt;
1018 		if (!needport)
1019 			break;
1020 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1021 			break;
1022 		m_copydata(m, off, sizeof(th), &th);
1023 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
1024 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
1025 		break;
1026 	case IPPROTO_UDP:
1027 		spidx->ul_proto = nxt;
1028 		if (!needport)
1029 			break;
1030 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1031 			break;
1032 		m_copydata(m, off, sizeof(uh), &uh);
1033 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
1034 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1035 		break;
1036 	case IPPROTO_ICMPV6:
1037 		spidx->ul_proto = nxt;
1038 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
1039 			break;
1040 		m_copydata(m, off, sizeof(icmph), &icmph);
1041 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
1042 		    htons((uint16_t)icmph.icmp6_type);
1043 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
1044 		    htons((uint16_t)icmph.icmp6_code);
1045 		break;
1046 	default:
1047 		/* XXX intermediate headers??? */
1048 		spidx->ul_proto = nxt;
1049 		break;
1050 	}
1051 }
1052 
1053 static int
1054 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1055 {
1056 	struct ip6_hdr *ip6 = NULL;
1057 	struct ip6_hdr ip6buf;
1058 	struct sockaddr_in6 *sin6;
1059 
1060 	if (m->m_len >= sizeof(*ip6)) {
1061 		ip6 = mtod(m, struct ip6_hdr *);
1062 	} else {
1063 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
1064 		ip6 = &ip6buf;
1065 	}
1066 
1067 	sin6 = (struct sockaddr_in6 *)&spidx->src;
1068 	memset(sin6, 0, sizeof(*sin6));
1069 	sin6->sin6_family = AF_INET6;
1070 	sin6->sin6_len = sizeof(struct sockaddr_in6);
1071 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
1072 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1073 		sin6->sin6_addr.s6_addr16[1] = 0;
1074 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1075 	}
1076 	spidx->prefs = sizeof(struct in6_addr) << 3;
1077 
1078 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
1079 	memset(sin6, 0, sizeof(*sin6));
1080 	sin6->sin6_family = AF_INET6;
1081 	sin6->sin6_len = sizeof(struct sockaddr_in6);
1082 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
1083 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1084 		sin6->sin6_addr.s6_addr16[1] = 0;
1085 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1086 	}
1087 	spidx->prefd = sizeof(struct in6_addr) << 3;
1088 
1089 	return 0;
1090 }
1091 #endif
1092 
1093 static void
1094 ipsec_delpcbpolicy(struct inpcbpolicy *p)
1095 {
1096 
1097 	kmem_intr_free(p, sizeof(*p));
1098 }
1099 
1100 int
1101 ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **policy)
1102 {
1103 	struct inpcbpolicy *new;
1104 
1105 	KASSERT(so != NULL);
1106 	KASSERT(policy != NULL);
1107 
1108 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
1109 	if (new == NULL) {
1110 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
1111 		return ENOBUFS;
1112 	}
1113 
1114 	if (IPSEC_PRIVILEGED_SO(so))
1115 		new->priv = 1;
1116 	else
1117 		new->priv = 0;
1118 
1119 	/*
1120 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
1121 	 */
1122 	new->sp_in = &ipsec_dummy_sp;
1123 	new->sp_out = &ipsec_dummy_sp;
1124 
1125 	*policy = new;
1126 
1127 	return 0;
1128 }
1129 
1130 static void
1131 ipsec_destroy_policy(struct secpolicy *sp)
1132 {
1133 
1134 	if (sp == &ipsec_dummy_sp) {
1135 		; /* It's dummy. No need to free it. */
1136 	} else {
1137 		/*
1138 		 * We cannot destroy here because it can be called in
1139 		 * softint. So mark the SP as DEAD and let the timer
1140 		 * destroy it. See key_timehandler_spd.
1141 		 */
1142 		sp->state = IPSEC_SPSTATE_DEAD;
1143 	}
1144 }
1145 
1146 int
1147 ipsec_set_policy(void *inp, const void *request, size_t len,
1148     kauth_cred_t cred)
1149 {
1150 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1151 	const struct sadb_x_policy *xpl;
1152 	struct secpolicy *newsp, *oldsp;
1153 	struct secpolicy **policy;
1154 	int error;
1155 
1156 	KASSERT(!cpu_softintr_p());
1157 	KASSERT(inph != NULL);
1158 	KASSERT(inph_locked(inph));
1159 	KASSERT(request != NULL);
1160 
1161 	if (len < sizeof(*xpl))
1162 		return EINVAL;
1163 	xpl = (const struct sadb_x_policy *)request;
1164 
1165 	KASSERT(inph->inph_sp != NULL);
1166 
1167 	/* select direction */
1168 	switch (xpl->sadb_x_policy_dir) {
1169 	case IPSEC_DIR_INBOUND:
1170 		policy = &inph->inph_sp->sp_in;
1171 		break;
1172 	case IPSEC_DIR_OUTBOUND:
1173 		policy = &inph->inph_sp->sp_out;
1174 		break;
1175 	default:
1176 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1177 		    xpl->sadb_x_policy_dir);
1178 		return EINVAL;
1179 	}
1180 
1181 	/* sanity check. */
1182 	if (policy == NULL || *policy == NULL)
1183 		return EINVAL;
1184 
1185 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1186 		kdebug_sadb_xpolicy("set passed policy", request);
1187 	}
1188 
1189 	/* check policy type */
1190 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1191 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
1192 	    xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1193 		return EINVAL;
1194 
1195 	/* check privileged socket */
1196 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1197 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
1198 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
1199 		if (error)
1200 			return error;
1201 	}
1202 
1203 	/* allocation new SP entry */
1204 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1205 		return error;
1206 
1207 	key_init_sp(newsp);
1208 	newsp->created = time_uptime;
1209 	/* Insert the global list for SPs for sockets */
1210 	key_socksplist_add(newsp);
1211 
1212 	/* clear old SP and set new SP */
1213 	oldsp = *policy;
1214 	*policy = newsp;
1215 	ipsec_destroy_policy(oldsp);
1216 
1217 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1218 		printf("%s: new policy\n", __func__);
1219 		kdebug_secpolicy(newsp);
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 int
1226 ipsec_get_policy(void *inp, const void *request, size_t len,
1227     struct mbuf **mp)
1228 {
1229 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1230 	const struct sadb_x_policy *xpl;
1231 	struct secpolicy *policy;
1232 
1233 	/* sanity check. */
1234 	if (inph == NULL || request == NULL || mp == NULL)
1235 		return EINVAL;
1236 	KASSERT(inph->inph_sp != NULL);
1237 	if (len < sizeof(*xpl))
1238 		return EINVAL;
1239 	xpl = (const struct sadb_x_policy *)request;
1240 
1241 	/* select direction */
1242 	switch (xpl->sadb_x_policy_dir) {
1243 	case IPSEC_DIR_INBOUND:
1244 		policy = inph->inph_sp->sp_in;
1245 		break;
1246 	case IPSEC_DIR_OUTBOUND:
1247 		policy = inph->inph_sp->sp_out;
1248 		break;
1249 	default:
1250 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1251 		    xpl->sadb_x_policy_dir);
1252 		return EINVAL;
1253 	}
1254 
1255 	if (policy == NULL)
1256 		return EINVAL;
1257 
1258 	*mp = key_sp2msg(policy, M_NOWAIT);
1259 	if (!*mp) {
1260 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
1261 		return ENOBUFS;
1262 	}
1263 
1264 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1265 		kdebug_mbuf(__func__, *mp);
1266 	}
1267 
1268 	return 0;
1269 }
1270 
1271 int
1272 ipsec_delete_pcbpolicy(void *inp)
1273 {
1274 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1275 
1276 	KASSERT(inph != NULL);
1277 
1278 	if (inph->inph_sp == NULL)
1279 		return 0;
1280 
1281 	if (inph->inph_sp->sp_in != NULL)
1282 		ipsec_destroy_policy(inph->inph_sp->sp_in);
1283 
1284 	if (inph->inph_sp->sp_out != NULL)
1285 		ipsec_destroy_policy(inph->inph_sp->sp_out);
1286 
1287 	ipsec_invalpcbcache(inph->inph_sp, IPSEC_DIR_ANY);
1288 
1289 	ipsec_delpcbpolicy(inph->inph_sp);
1290 	inph->inph_sp = NULL;
1291 
1292 	return 0;
1293 }
1294 
1295 /*
1296  * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
1297  */
1298 u_int
1299 ipsec_get_reqlevel(const struct ipsecrequest *isr)
1300 {
1301 	u_int level = 0;
1302 	u_int esp_trans_deflev, esp_net_deflev;
1303 	u_int ah_trans_deflev, ah_net_deflev;
1304 
1305 	KASSERT(isr != NULL);
1306 	KASSERT(isr->sp != NULL);
1307 	KASSERTMSG(
1308 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1309 	    "af family mismatch, src %u, dst %u",
1310 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
1311 
1312 /* XXX note that we have ipseclog() expanded here - code sync issue */
1313 #define IPSEC_CHECK_DEFAULT(lev)					\
1314     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
1315     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
1316 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
1317 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
1318 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
1319     : (lev))
1320 
1321 	/* set default level */
1322 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1323 #ifdef INET
1324 	case AF_INET:
1325 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1326 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1327 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1328 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1329 		break;
1330 #endif
1331 #ifdef INET6
1332 	case AF_INET6:
1333 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1334 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1335 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1336 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1337 		break;
1338 #endif
1339 	default:
1340 		panic("%s: unknown af %u", __func__,
1341 		    isr->sp->spidx.src.sa.sa_family);
1342 	}
1343 
1344 #undef IPSEC_CHECK_DEFAULT
1345 
1346 	/* set level */
1347 	switch (isr->level) {
1348 	case IPSEC_LEVEL_DEFAULT:
1349 		switch (isr->saidx.proto) {
1350 		case IPPROTO_ESP:
1351 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1352 				level = esp_net_deflev;
1353 			else
1354 				level = esp_trans_deflev;
1355 			break;
1356 		case IPPROTO_AH:
1357 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1358 				level = ah_net_deflev;
1359 			else
1360 				level = ah_trans_deflev;
1361 			break;
1362 		case IPPROTO_IPCOMP:
1363 			/*
1364 			 * we don't really care, as IPcomp document says that
1365 			 * we shouldn't compress small packets
1366 			 */
1367 			level = IPSEC_LEVEL_USE;
1368 			break;
1369 		default:
1370 			panic("%s: Illegal protocol defined %u", __func__,
1371 			    isr->saidx.proto);
1372 		}
1373 		break;
1374 
1375 	case IPSEC_LEVEL_USE:
1376 	case IPSEC_LEVEL_REQUIRE:
1377 		level = isr->level;
1378 		break;
1379 	case IPSEC_LEVEL_UNIQUE:
1380 		level = IPSEC_LEVEL_REQUIRE;
1381 		break;
1382 
1383 	default:
1384 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
1385 	}
1386 
1387 	return level;
1388 }
1389 
1390 /*
1391  * Check security policy requirements against the actual packet contents.
1392  *
1393  * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
1394  * then kick it.
1395  */
1396 static int
1397 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
1398 {
1399 	struct ipsecrequest *isr;
1400 
1401 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1402 		printf("%s: using SP\n", __func__);
1403 		kdebug_secpolicy(sp);
1404 	}
1405 
1406 	/* check policy */
1407 	switch (sp->policy) {
1408 	case IPSEC_POLICY_DISCARD:
1409 		return 1;
1410 	case IPSEC_POLICY_BYPASS:
1411 	case IPSEC_POLICY_NONE:
1412 		return 0;
1413 	}
1414 
1415 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1416 	    "invalid policy %u", sp->policy);
1417 
1418 	/* XXX should compare policy against ipsec header history */
1419 
1420 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1421 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1422 			continue;
1423 		switch (isr->saidx.proto) {
1424 		case IPPROTO_ESP:
1425 			if ((m->m_flags & M_DECRYPTED) == 0) {
1426 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1427 				    "ESP m_flags:%x\n", m->m_flags);
1428 				return 1;
1429 			}
1430 			break;
1431 		case IPPROTO_AH:
1432 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
1433 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1434 				    "AH m_flags:%x\n", m->m_flags);
1435 				return 1;
1436 			}
1437 			break;
1438 		case IPPROTO_IPCOMP:
1439 			/*
1440 			 * We don't really care, as IPcomp document
1441 			 * says that we shouldn't compress small
1442 			 * packets, IPComp policy should always be
1443 			 * treated as being in "use" level.
1444 			 */
1445 			break;
1446 		}
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 /*
1453  * Check security policy requirements.
1454  */
1455 int
1456 ipsec_in_reject(struct mbuf *m, void *inp)
1457 {
1458 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1459 	struct secpolicy *sp;
1460 	int error;
1461 	int result;
1462 
1463 	KASSERT(m != NULL);
1464 
1465 	if (inph == NULL)
1466 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1467 		    IP_FORWARDING, &error);
1468 	else
1469 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1470 		    inph, &error);
1471 
1472 	if (sp != NULL) {
1473 		result = ipsec_sp_reject(sp, m);
1474 		if (result)
1475 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1476 		KEY_SP_UNREF(&sp);
1477 	} else {
1478 		result = 0;
1479 	}
1480 	return result;
1481 }
1482 
1483 /*
1484  * Compute the byte size to be occupied by the IPsec header. If it is
1485  * tunneled, it includes the size of outer IP header.
1486  */
1487 static size_t
1488 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
1489 {
1490 	struct ipsecrequest *isr;
1491 	size_t siz;
1492 
1493 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1494 		printf("%s: using SP\n", __func__);
1495 		kdebug_secpolicy(sp);
1496 	}
1497 
1498 	switch (sp->policy) {
1499 	case IPSEC_POLICY_DISCARD:
1500 	case IPSEC_POLICY_BYPASS:
1501 	case IPSEC_POLICY_NONE:
1502 		return 0;
1503 	}
1504 
1505 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1506 	    "invalid policy %u", sp->policy);
1507 
1508 	siz = 0;
1509 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1510 		size_t clen = 0;
1511 		struct secasvar *sav;
1512 
1513 		switch (isr->saidx.proto) {
1514 		case IPPROTO_ESP:
1515 			sav = ipsec_lookup_sa(isr, m);
1516 			if (sav != NULL) {
1517 				clen = esp_hdrsiz(sav);
1518 				KEY_SA_UNREF(&sav);
1519 			} else
1520 				clen = esp_hdrsiz(NULL);
1521 			break;
1522 		case IPPROTO_AH:
1523 			sav = ipsec_lookup_sa(isr, m);
1524 			if (sav != NULL) {
1525 				clen = ah_hdrsiz(sav);
1526 				KEY_SA_UNREF(&sav);
1527 			} else
1528 				clen = ah_hdrsiz(NULL);
1529 			break;
1530 		case IPPROTO_IPCOMP:
1531 			clen = sizeof(struct ipcomp);
1532 			break;
1533 		}
1534 
1535 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1536 			switch (isr->saidx.dst.sa.sa_family) {
1537 			case AF_INET:
1538 				clen += sizeof(struct ip);
1539 				break;
1540 #ifdef INET6
1541 			case AF_INET6:
1542 				clen += sizeof(struct ip6_hdr);
1543 				break;
1544 #endif
1545 			default:
1546 				IPSECLOG(LOG_ERR, "unknown AF %d in "
1547 				    "IPsec tunnel SA\n",
1548 				    ((const struct sockaddr *)&isr->saidx.dst)
1549 				    ->sa_family);
1550 				break;
1551 			}
1552 		}
1553 		siz += clen;
1554 	}
1555 
1556 	return siz;
1557 }
1558 
1559 size_t
1560 ipsec_hdrsiz(struct mbuf *m, u_int dir, void *inp)
1561 {
1562 	struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1563 	struct secpolicy *sp;
1564 	int error;
1565 	size_t size;
1566 
1567 	KASSERT(m != NULL);
1568 	KASSERTMSG(inph == NULL || inph->inph_socket != NULL,
1569 	    "socket w/o inpcb");
1570 
1571 	if (inph == NULL)
1572 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1573 	else
1574 		sp = ipsec_getpolicybysock(m, dir, inph, &error);
1575 
1576 	if (sp != NULL) {
1577 		size = ipsec_sp_hdrsiz(sp, m);
1578 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
1579 		KEY_SP_UNREF(&sp);
1580 	} else {
1581 		size = 0;
1582 	}
1583 
1584 	return size;
1585 }
1586 
1587 /*
1588  * Check the variable replay window.
1589  * ipsec_chkreplay() performs replay check before ICV verification.
1590  * ipsec_updatereplay() updates replay bitmap.  This must be called after
1591  * ICV verification (it also performs replay check, which is usually done
1592  * beforehand).
1593  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1594  *
1595  * based on RFC 2401.
1596  */
1597 int
1598 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
1599 {
1600 	const struct secreplay *replay;
1601 	u_int32_t diff;
1602 	int fr;
1603 	u_int32_t wsizeb;	/* constant: bits of window size */
1604 	int frlast;		/* constant: last frame */
1605 
1606 	KASSERT(sav != NULL);
1607 	KASSERT(sav->replay != NULL);
1608 
1609 	replay = sav->replay;
1610 
1611 	if (replay->wsize == 0)
1612 		return 1;	/* no need to check replay. */
1613 
1614 	/* constant */
1615 	frlast = replay->wsize - 1;
1616 	wsizeb = replay->wsize << 3;
1617 
1618 	/* sequence number of 0 is invalid */
1619 	if (seq == 0)
1620 		return 0;
1621 
1622 	/* first time is always okay */
1623 	if (replay->count == 0)
1624 		return 1;
1625 
1626 	if (seq > replay->lastseq) {
1627 		/* larger sequences are okay */
1628 		return 1;
1629 	} else {
1630 		/* seq is equal or less than lastseq. */
1631 		diff = replay->lastseq - seq;
1632 
1633 		/* over range to check, i.e. too old or wrapped */
1634 		if (diff >= wsizeb)
1635 			return 0;
1636 
1637 		fr = frlast - diff / 8;
1638 
1639 		/* this packet already seen ? */
1640 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1641 			return 0;
1642 
1643 		/* out of order but good */
1644 		return 1;
1645 	}
1646 }
1647 
1648 /*
1649  * check replay counter whether to update or not.
1650  * OUT:	0:	OK
1651  *	1:	NG
1652  */
1653 int
1654 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
1655 {
1656 	struct secreplay *replay;
1657 	u_int32_t diff;
1658 	int fr;
1659 	u_int32_t wsizeb;	/* constant: bits of window size */
1660 	int frlast;		/* constant: last frame */
1661 
1662 	KASSERT(sav != NULL);
1663 	KASSERT(sav->replay != NULL);
1664 
1665 	replay = sav->replay;
1666 
1667 	if (replay->wsize == 0)
1668 		goto ok;	/* no need to check replay. */
1669 
1670 	/* constant */
1671 	frlast = replay->wsize - 1;
1672 	wsizeb = replay->wsize << 3;
1673 
1674 	/* sequence number of 0 is invalid */
1675 	if (seq == 0)
1676 		return 1;
1677 
1678 	/* first time */
1679 	if (replay->count == 0) {
1680 		replay->lastseq = seq;
1681 		memset(replay->bitmap, 0, replay->wsize);
1682 		(replay->bitmap)[frlast] = 1;
1683 		goto ok;
1684 	}
1685 
1686 	if (seq > replay->lastseq) {
1687 		/* seq is larger than lastseq. */
1688 		diff = seq - replay->lastseq;
1689 
1690 		/* new larger sequence number */
1691 		if (diff < wsizeb) {
1692 			/* In window */
1693 			/* set bit for this packet */
1694 			vshiftl(replay->bitmap, diff, replay->wsize);
1695 			(replay->bitmap)[frlast] |= 1;
1696 		} else {
1697 			/* this packet has a "way larger" */
1698 			memset(replay->bitmap, 0, replay->wsize);
1699 			(replay->bitmap)[frlast] = 1;
1700 		}
1701 		replay->lastseq = seq;
1702 
1703 		/* larger is good */
1704 	} else {
1705 		/* seq is equal or less than lastseq. */
1706 		diff = replay->lastseq - seq;
1707 
1708 		/* over range to check, i.e. too old or wrapped */
1709 		if (diff >= wsizeb)
1710 			return 1;
1711 
1712 		fr = frlast - diff / 8;
1713 
1714 		/* this packet already seen ? */
1715 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1716 			return 1;
1717 
1718 		/* mark as seen */
1719 		(replay->bitmap)[fr] |= (1 << (diff % 8));
1720 
1721 		/* out of order but good */
1722 	}
1723 
1724 ok:
1725 	if (replay->count == ~0) {
1726 		char buf[IPSEC_LOGSASTRLEN];
1727 
1728 		/* set overflow flag */
1729 		replay->overflow++;
1730 
1731 		/* don't increment, no more packets accepted */
1732 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1733 			return 1;
1734 
1735 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
1736 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
1737 	}
1738 
1739 	replay->count++;
1740 
1741 	return 0;
1742 }
1743 
1744 /*
1745  * shift variable length buffer to left.
1746  * IN:	bitmap: pointer to the buffer
1747  *	nbit:	the number of to shift.
1748  *	wsize:	buffer size (bytes).
1749  */
1750 static void
1751 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1752 {
1753 	int s, j, i;
1754 	unsigned char over;
1755 
1756 	for (j = 0; j < nbit; j += 8) {
1757 		s = (nbit - j < 8) ? (nbit - j): 8;
1758 		bitmap[0] <<= s;
1759 		for (i = 1; i < wsize; i++) {
1760 			over = (bitmap[i] >> (8 - s));
1761 			bitmap[i] <<= s;
1762 			bitmap[i-1] |= over;
1763 		}
1764 	}
1765 
1766 	return;
1767 }
1768 
1769 /* Return a printable string for the address. */
1770 const char *
1771 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
1772 {
1773 	switch (sa->sa.sa_family) {
1774 	case AF_INET:
1775 		in_print(buf, size, &sa->sin.sin_addr);
1776 		return buf;
1777 #if INET6
1778 	case AF_INET6:
1779 		in6_print(buf, size, &sa->sin6.sin6_addr);
1780 		return buf;
1781 #endif
1782 	default:
1783 		return "(unknown address family)";
1784 	}
1785 }
1786 
1787 const char *
1788 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
1789 {
1790 	const struct secasindex *saidx = &sav->sah->saidx;
1791 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
1792 
1793 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1794 	    "af family mismatch, src %u, dst %u",
1795 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
1796 
1797 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
1798 	    (u_int32_t)ntohl(sav->spi),
1799 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
1800 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
1801 
1802 	return buf;
1803 }
1804 
1805 #ifdef INET6
1806 struct secpolicy *
1807 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p, int flags,
1808     int *needipsecp, int *errorp)
1809 {
1810 	struct secpolicy *sp = NULL;
1811 	int s;
1812 	int error = 0;
1813 	int needipsec = 0;
1814 
1815 	if (ipsec_outdone(m)) {
1816 		goto skippolicycheck;
1817 	}
1818 	s = splsoftnet();
1819 	if (in6p && ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
1820 		splx(s);
1821 		goto skippolicycheck;
1822 	}
1823 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, in6p);
1824 	splx(s);
1825 
1826 	/*
1827 	 * There are four return cases:
1828 	 *	sp != NULL                    apply IPsec policy
1829 	 *	sp == NULL, error == 0        no IPsec handling needed
1830 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
1831 	 *	sp == NULL, error != 0        discard packet, report error
1832 	 */
1833 	if (sp == NULL) {
1834 		needipsec = 0;
1835 	} else {
1836 		needipsec = 1;
1837 	}
1838 
1839 skippolicycheck:
1840 	*errorp = error;
1841 	*needipsecp = needipsec;
1842 	return sp;
1843 }
1844 
1845 /*
1846  * calculate UDP checksum for UDP encapsulated ESP for IPv6.
1847  *
1848  * RFC2460(Internet Protocol, Version 6 Specification) says:
1849  *
1850  *   IPv6 receivers MUST discard UDP packets with a zero checksum.
1851  *
1852  * There is more relaxed specification RFC6935(IPv6 and UDP Checksums for
1853  * Tunneled Packets). The document allows zero checksum. It's too
1854  * late to publish, there are a lot of interoperability problems...
1855  */
1856 void
1857 ipsec6_udp_cksum(struct mbuf *m)
1858 {
1859 	struct ip6_hdr *ip6;
1860 	uint16_t plen, uh_sum;
1861 	int off;
1862 
1863 	/* must called after m_pullup() */
1864 	KASSERT(m->m_len >= sizeof(struct ip6_hdr));
1865 
1866 	ip6 = mtod(m, struct ip6_hdr *);
1867 	KASSERT(ip6->ip6_nxt == IPPROTO_UDP);
1868 
1869 	/* ip6->ip6_plen can not be updated before ip6_output() */
1870 	plen = m->m_pkthdr.len - sizeof(*ip6);
1871 	KASSERT(plen >= sizeof(struct udphdr));
1872 
1873 	uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(*ip6), plen);
1874 	if (uh_sum == 0)
1875 		uh_sum = 0xffff;
1876 
1877 	off = sizeof(*ip6) + offsetof(struct udphdr, uh_sum);
1878 	m_copyback(m, off, sizeof(uh_sum), (void *)&uh_sum);
1879 }
1880 #endif /* INET6 */
1881 
1882 /*
1883  * -----------------------------------------------------------------------------
1884  */
1885 
1886 /* XXX this stuff doesn't belong here... */
1887 
1888 static struct xformsw *xforms = NULL;
1889 
1890 /*
1891  * Register a transform; typically at system startup.
1892  */
1893 void
1894 xform_register(struct xformsw *xsp)
1895 {
1896 	xsp->xf_next = xforms;
1897 	xforms = xsp;
1898 }
1899 
1900 /*
1901  * Initialize transform support in an sav.
1902  */
1903 int
1904 xform_init(struct secasvar *sav, int xftype)
1905 {
1906 	struct xformsw *xsp;
1907 
1908 	if (sav->tdb_xform != NULL)	/* previously initialized */
1909 		return 0;
1910 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
1911 		if (xsp->xf_type == xftype)
1912 			return (*xsp->xf_init)(sav, xsp);
1913 
1914 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
1915 	return EINVAL;
1916 }
1917 
1918 /*
1919  * XXXJRT This should be done as a protosw init call.
1920  */
1921 void
1922 ipsec_attach(void)
1923 {
1924 
1925 	ipsec_output_init();
1926 
1927 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
1928 
1929 	sysctl_net_inet_ipsec_setup(NULL);
1930 #ifdef INET6
1931 	sysctl_net_inet6_ipsec6_setup(NULL);
1932 #endif
1933 
1934 	ah_attach();
1935 	esp_attach();
1936 	ipcomp_attach();
1937 	ipe4_attach();
1938 #ifdef TCP_SIGNATURE
1939 	tcpsignature_attach();
1940 #endif
1941 }
1942