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