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