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