xref: /freebsd/sys/netipsec/ipsec.c (revision 995dc984471c92c03daad19a1d35af46c086ef3e)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $	*/
3 
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * IPsec controller part.
35  */
36 
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
55 #include <sys/proc.h>
56 
57 #include <net/if.h>
58 #include <net/route.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/in_var.h>
65 #include <netinet/udp.h>
66 #include <netinet/udp_var.h>
67 #include <netinet/tcp.h>
68 #include <netinet/udp.h>
69 
70 #include <netinet/ip6.h>
71 #ifdef INET6
72 #include <netinet6/ip6_var.h>
73 #endif
74 #include <netinet/in_pcb.h>
75 #ifdef INET6
76 #include <netinet/icmp6.h>
77 #endif
78 
79 #include <sys/types.h>
80 #include <netipsec/ipsec.h>
81 #ifdef INET6
82 #include <netipsec/ipsec6.h>
83 #endif
84 #include <netipsec/ah_var.h>
85 #include <netipsec/esp_var.h>
86 #include <netipsec/ipcomp.h>		/*XXX*/
87 #include <netipsec/ipcomp_var.h>
88 
89 #include <netipsec/key.h>
90 #include <netipsec/keydb.h>
91 #include <netipsec/key_debug.h>
92 
93 #include <netipsec/xform.h>
94 
95 #include <machine/in_cksum.h>
96 
97 #include <opencrypto/cryptodev.h>
98 
99 #ifdef IPSEC_DEBUG
100 int ipsec_debug = 1;
101 #else
102 int ipsec_debug = 0;
103 #endif
104 
105 /* NB: name changed so netstat doesn't use it */
106 struct ipsecstat ipsec4stat;
107 int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
108 int ip4_ipsec_dfbit = 0;	/* DF bit on encap. 0: clear 1: set 2: copy */
109 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
110 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
111 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
112 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
113 struct secpolicy ip4_def_policy;
114 int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
115 int ip4_esp_randpad = -1;
116 /*
117  * Crypto support requirements:
118  *
119  *  1	require hardware support
120  * -1	require software support
121  *  0	take anything
122  */
123 int	crypto_support = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
124 
125 SYSCTL_DECL(_net_inet_ipsec);
126 
127 /* net.inet.ipsec */
128 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
129 	def_policy, CTLFLAG_RW,	&ip4_def_policy.policy,	0, "");
130 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
131 	CTLFLAG_RW, &ip4_esp_trans_deflev,	0, "");
132 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
133 	CTLFLAG_RW, &ip4_esp_net_deflev,	0, "");
134 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
135 	CTLFLAG_RW, &ip4_ah_trans_deflev,	0, "");
136 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
137 	CTLFLAG_RW, &ip4_ah_net_deflev,	0, "");
138 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
139 	ah_cleartos, CTLFLAG_RW,	&ah_cleartos,	0, "");
140 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
141 	ah_offsetmask, CTLFLAG_RW,	&ip4_ah_offsetmask,	0, "");
142 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
143 	dfbit, CTLFLAG_RW,	&ip4_ipsec_dfbit,	0, "");
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
145 	ecn, CTLFLAG_RW,	&ip4_ipsec_ecn,	0, "");
146 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
147 	debug, CTLFLAG_RW,	&ipsec_debug,	0, "");
148 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
149 	esp_randpad, CTLFLAG_RW,	&ip4_esp_randpad,	0, "");
150 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
151 	crypto_support,	CTLFLAG_RW,	&crypto_support,0, "");
152 SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
153 	ipsecstats,	CTLFLAG_RD,	&ipsec4stat, ipsecstat, "");
154 
155 #ifdef REGRESSION
156 /*
157  * When set to 1, IPsec will send packets with the same sequence number.
158  * This allows to verify if the other side has proper replay attacks detection.
159  */
160 int ipsec_replay = 0;
161 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
162     "Emulate replay attack");
163 /*
164  * When set 1, IPsec will send packets with corrupted HMAC.
165  * This allows to verify if the other side properly detects modified packets.
166  */
167 int ipsec_integrity = 0;
168 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
169     &ipsec_integrity, 0, "Emulate man-in-the-middle attack");
170 #endif
171 
172 #ifdef INET6
173 struct ipsecstat ipsec6stat;
174 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
175 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
176 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
177 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
178 int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
179 int ip6_esp_randpad = -1;
180 
181 SYSCTL_DECL(_net_inet6_ipsec6);
182 
183 /* net.inet6.ipsec6 */
184 #ifdef COMPAT_KAME
185 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
186 	0,0, compat_ipsecstats_sysctl, "S", "");
187 #endif /* COMPAT_KAME */
188 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
189 	def_policy, CTLFLAG_RW,	&ip4_def_policy.policy,	0, "");
190 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
191 	CTLFLAG_RW, &ip6_esp_trans_deflev,	0, "");
192 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
193 	CTLFLAG_RW, &ip6_esp_net_deflev,	0, "");
194 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
195 	CTLFLAG_RW, &ip6_ah_trans_deflev,	0, "");
196 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
197 	CTLFLAG_RW, &ip6_ah_net_deflev,	0, "");
198 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
199 	ecn, CTLFLAG_RW,	&ip6_ipsec_ecn,	0, "");
200 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
201 	debug, CTLFLAG_RW,	&ipsec_debug,	0, "");
202 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
203 	esp_randpad, CTLFLAG_RW,	&ip6_esp_randpad,	0, "");
204 SYSCTL_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
205 	ipsecstats, CTLFLAG_RD, &ipsec6stat, ipsecstat, "");
206 #endif /* INET6 */
207 
208 static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
209 #ifdef INET6
210 static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct in6pcb *pcb));
211 #endif
212 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
213 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
214 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
215 #ifdef INET6
216 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
217 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
218 #endif
219 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
220 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
221 static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
222 	int optname, caddr_t request, size_t len, struct ucred *cred));
223 static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
224 static void vshiftl __P((unsigned char *, int, int));
225 static size_t ipsec_hdrsiz __P((struct secpolicy *));
226 
227 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
228 
229 /*
230  * Return a held reference to the default SP.
231  */
232 static struct secpolicy *
233 key_allocsp_default(const char* where, int tag)
234 {
235 	struct secpolicy *sp;
236 
237 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
238 		printf("DP key_allocsp_default from %s:%u\n", where, tag));
239 
240 	sp = &ip4_def_policy;
241 	if (sp->policy != IPSEC_POLICY_DISCARD &&
242 	    sp->policy != IPSEC_POLICY_NONE) {
243 		ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
244 		    sp->policy, IPSEC_POLICY_NONE));
245 		sp->policy = IPSEC_POLICY_NONE;
246 	}
247 	key_addref(sp);
248 
249 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
250 		printf("DP key_allocsp_default returns SP:%p (%u)\n",
251 			sp, sp->refcnt));
252 	return sp;
253 }
254 #define	KEY_ALLOCSP_DEFAULT() \
255 	key_allocsp_default(__FILE__, __LINE__)
256 
257 /*
258  * For OUTBOUND packet having a socket. Searching SPD for packet,
259  * and return a pointer to SP.
260  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
261  *		0	: bypass
262  *		EACCES	: discard packet.
263  *		ENOENT	: ipsec_acquire() in progress, maybe.
264  *		others	: error occured.
265  *	others:	a pointer to SP
266  *
267  * NOTE: IPv6 mapped adddress concern is implemented here.
268  */
269 struct secpolicy *
270 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
271 {
272 	struct secpolicy *sp;
273 
274 	IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
275 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
276 		("invalid direction %u", dir));
277 
278 	sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
279 	if (sp == NULL)			/*XXX????*/
280 		sp = KEY_ALLOCSP_DEFAULT();
281 	IPSEC_ASSERT(sp != NULL, ("null SP"));
282 	return sp;
283 }
284 
285 /*
286  * For OUTBOUND packet having a socket. Searching SPD for packet,
287  * and return a pointer to SP.
288  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
289  *		0	: bypass
290  *		EACCES	: discard packet.
291  *		ENOENT	: ipsec_acquire() in progress, maybe.
292  *		others	: error occured.
293  *	others:	a pointer to SP
294  *
295  * NOTE: IPv6 mapped adddress concern is implemented here.
296  */
297 struct secpolicy *
298 ipsec_getpolicybysock(m, dir, inp, error)
299 	struct mbuf *m;
300 	u_int dir;
301 	struct inpcb *inp;
302 	int *error;
303 {
304 	struct inpcbpolicy *pcbsp = NULL;
305 	struct secpolicy *currsp = NULL;	/* policy on socket */
306 	struct secpolicy *sp;
307 
308 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
309 	IPSEC_ASSERT(inp != NULL, ("null inpcb"));
310 	IPSEC_ASSERT(error != NULL, ("null error"));
311 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
312 		("invalid direction %u", dir));
313 
314 	/* set spidx in pcb */
315 	if (inp->inp_vflag & INP_IPV6PROTO) {
316 #ifdef INET6
317 		*error = ipsec6_setspidx_in6pcb(m, inp);
318 		pcbsp = inp->in6p_sp;
319 #else
320 		*error = EINVAL;		/* should not happen */
321 #endif
322 	} else {
323 		*error = ipsec4_setspidx_inpcb(m, inp);
324 		pcbsp = inp->inp_sp;
325 	}
326 	if (*error)
327 		return NULL;
328 
329 	IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
330 	switch (dir) {
331 	case IPSEC_DIR_INBOUND:
332 		currsp = pcbsp->sp_in;
333 		break;
334 	case IPSEC_DIR_OUTBOUND:
335 		currsp = pcbsp->sp_out;
336 		break;
337 	}
338 	IPSEC_ASSERT(currsp != NULL, ("null currsp"));
339 
340 	if (pcbsp->priv) {			/* when privilieged socket */
341 		switch (currsp->policy) {
342 		case IPSEC_POLICY_BYPASS:
343 		case IPSEC_POLICY_IPSEC:
344 			key_addref(currsp);
345 			sp = currsp;
346 			break;
347 
348 		case IPSEC_POLICY_ENTRUST:
349 			/* look for a policy in SPD */
350 			sp = KEY_ALLOCSP(&currsp->spidx, dir);
351 			if (sp == NULL)		/* no SP found */
352 				sp = KEY_ALLOCSP_DEFAULT();
353 			break;
354 
355 		default:
356 			ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
357 				__func__, currsp->policy));
358 			*error = EINVAL;
359 			return NULL;
360 		}
361 	} else {				/* unpriv, SPD has policy */
362 		sp = KEY_ALLOCSP(&currsp->spidx, dir);
363 		if (sp == NULL) {		/* no SP found */
364 			switch (currsp->policy) {
365 			case IPSEC_POLICY_BYPASS:
366 				ipseclog((LOG_ERR, "%s: Illegal policy for "
367 					"non-priviliged defined %d\n",
368 					__func__, currsp->policy));
369 				*error = EINVAL;
370 				return NULL;
371 
372 			case IPSEC_POLICY_ENTRUST:
373 				sp = KEY_ALLOCSP_DEFAULT();
374 				break;
375 
376 			case IPSEC_POLICY_IPSEC:
377 				key_addref(currsp);
378 				sp = currsp;
379 				break;
380 
381 			default:
382 				ipseclog((LOG_ERR, "%s: Invalid policy for "
383 					"PCB %d\n", __func__, currsp->policy));
384 				*error = EINVAL;
385 				return NULL;
386 			}
387 		}
388 	}
389 	IPSEC_ASSERT(sp != NULL,
390 		("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
391 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
392 		printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
393 			__func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
394 	return sp;
395 }
396 
397 /*
398  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
399  * and return a pointer to SP.
400  * OUT:	positive: a pointer to the entry for security policy leaf matched.
401  *	NULL:	no apropreate SP found, the following value is set to error.
402  *		0	: bypass
403  *		EACCES	: discard packet.
404  *		ENOENT	: ipsec_acquire() in progress, maybe.
405  *		others	: error occured.
406  */
407 struct secpolicy *
408 ipsec_getpolicybyaddr(m, dir, flag, error)
409 	struct mbuf *m;
410 	u_int dir;
411 	int flag;
412 	int *error;
413 {
414 	struct secpolicyindex spidx;
415 	struct secpolicy *sp;
416 
417 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
418 	IPSEC_ASSERT(error != NULL, ("null error"));
419 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
420 		("invalid direction %u", dir));
421 
422 	sp = NULL;
423 	if (key_havesp(dir)) {
424 		/* Make an index to look for a policy. */
425 		*error = ipsec_setspidx(m, &spidx,
426 					(flag & IP_FORWARDING) ? 0 : 1);
427 		if (*error != 0) {
428 			DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
429 				__func__, dir, flag));
430 			return NULL;
431 		}
432 		spidx.dir = dir;
433 
434 		sp = KEY_ALLOCSP(&spidx, dir);
435 	}
436 	if (sp == NULL)			/* no SP found, use system default */
437 		sp = KEY_ALLOCSP_DEFAULT();
438 	IPSEC_ASSERT(sp != NULL, ("null SP"));
439 	return sp;
440 }
441 
442 struct secpolicy *
443 ipsec4_checkpolicy(m, dir, flag, error, inp)
444 	struct mbuf *m;
445 	u_int dir, flag;
446 	int *error;
447 	struct inpcb *inp;
448 {
449 	struct secpolicy *sp;
450 
451 	*error = 0;
452 	if (inp == NULL)
453 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
454 	else
455 		sp = ipsec_getpolicybysock(m, dir, inp, error);
456 	if (sp == NULL) {
457 		IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
458 		ipsec4stat.ips_out_inval++;
459 		return NULL;
460 	}
461 	IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
462 	switch (sp->policy) {
463 	case IPSEC_POLICY_ENTRUST:
464 	default:
465 		printf("%s: invalid policy %u\n", __func__, sp->policy);
466 		/* fall thru... */
467 	case IPSEC_POLICY_DISCARD:
468 		ipsec4stat.ips_out_polvio++;
469 		*error = -EINVAL;	/* packet is discarded by caller */
470 		break;
471 	case IPSEC_POLICY_BYPASS:
472 	case IPSEC_POLICY_NONE:
473 		KEY_FREESP(&sp);
474 		sp = NULL;		/* NB: force NULL result */
475 		break;
476 	case IPSEC_POLICY_IPSEC:
477 		if (sp->req == NULL)	/* acquire an SA */
478 			*error = key_spdacquire(sp);
479 		break;
480 	}
481 	if (*error != 0) {
482 		KEY_FREESP(&sp);
483 		sp = NULL;
484 	}
485 	return sp;
486 }
487 
488 static int
489 ipsec4_setspidx_inpcb(m, pcb)
490 	struct mbuf *m;
491 	struct inpcb *pcb;
492 {
493 	int error;
494 
495 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
496 	IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
497 	IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
498 		("null sp_in || sp_out"));
499 
500 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
501 	if (error == 0) {
502 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
503 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
504 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
505 	} else {
506 		bzero(&pcb->inp_sp->sp_in->spidx,
507 			sizeof (pcb->inp_sp->sp_in->spidx));
508 		bzero(&pcb->inp_sp->sp_out->spidx,
509 			sizeof (pcb->inp_sp->sp_in->spidx));
510 	}
511 	return error;
512 }
513 
514 #ifdef INET6
515 static int
516 ipsec6_setspidx_in6pcb(m, pcb)
517 	struct mbuf *m;
518 	struct in6pcb *pcb;
519 {
520 	struct secpolicyindex *spidx;
521 	int error;
522 
523 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
524 	IPSEC_ASSERT(pcb->in6p_sp != NULL, ("null inp_sp"));
525 	IPSEC_ASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
526 		("null sp_in || sp_out"));
527 
528 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
529 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
530 
531 	spidx = &pcb->in6p_sp->sp_in->spidx;
532 	error = ipsec_setspidx(m, spidx, 1);
533 	if (error)
534 		goto bad;
535 	spidx->dir = IPSEC_DIR_INBOUND;
536 
537 	spidx = &pcb->in6p_sp->sp_out->spidx;
538 	error = ipsec_setspidx(m, spidx, 1);
539 	if (error)
540 		goto bad;
541 	spidx->dir = IPSEC_DIR_OUTBOUND;
542 
543 	return 0;
544 
545 bad:
546 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
547 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
548 	return error;
549 }
550 #endif
551 
552 /*
553  * configure security policy index (src/dst/proto/sport/dport)
554  * by looking at the content of mbuf.
555  * the caller is responsible for error recovery (like clearing up spidx).
556  */
557 static int
558 ipsec_setspidx(m, spidx, needport)
559 	struct mbuf *m;
560 	struct secpolicyindex *spidx;
561 	int needport;
562 {
563 	struct ip *ip = NULL;
564 	struct ip ipbuf;
565 	u_int v;
566 	struct mbuf *n;
567 	int len;
568 	int error;
569 
570 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
571 
572 	/*
573 	 * validate m->m_pkthdr.len.  we see incorrect length if we
574 	 * mistakenly call this function with inconsistent mbuf chain
575 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
576 	 */
577 	len = 0;
578 	for (n = m; n; n = n->m_next)
579 		len += n->m_len;
580 	if (m->m_pkthdr.len != len) {
581 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
582 			printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
583 				__func__, len, m->m_pkthdr.len));
584 		return EINVAL;
585 	}
586 
587 	if (m->m_pkthdr.len < sizeof(struct ip)) {
588 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
589 			printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
590 			    __func__, m->m_pkthdr.len));
591 		return EINVAL;
592 	}
593 
594 	if (m->m_len >= sizeof(*ip))
595 		ip = mtod(m, struct ip *);
596 	else {
597 		m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
598 		ip = &ipbuf;
599 	}
600 #ifdef _IP_VHL
601 	v = _IP_VHL_V(ip->ip_vhl);
602 #else
603 	v = ip->ip_v;
604 #endif
605 	switch (v) {
606 	case 4:
607 		error = ipsec4_setspidx_ipaddr(m, spidx);
608 		if (error)
609 			return error;
610 		ipsec4_get_ulp(m, spidx, needport);
611 		return 0;
612 #ifdef INET6
613 	case 6:
614 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
615 			KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
616 				printf("%s: pkthdr len(%d) too small (v6), "
617 				"ignored\n", __func__, m->m_pkthdr.len));
618 			return EINVAL;
619 		}
620 		error = ipsec6_setspidx_ipaddr(m, spidx);
621 		if (error)
622 			return error;
623 		ipsec6_get_ulp(m, spidx, needport);
624 		return 0;
625 #endif
626 	default:
627 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
628 			printf("%s: " "unknown IP version %u, ignored.\n",
629 				__func__, v));
630 		return EINVAL;
631 	}
632 }
633 
634 static void
635 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
636 {
637 	u_int8_t nxt;
638 	int off;
639 
640 	/* sanity check */
641 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
642 	IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
643 
644 	/* NB: ip_input() flips it into host endian XXX need more checking */
645 	if (m->m_len < sizeof (struct ip)) {
646 		struct ip *ip = mtod(m, struct ip *);
647 		if (ip->ip_off & (IP_MF | IP_OFFMASK))
648 			goto done;
649 #ifdef _IP_VHL
650 		off = _IP_VHL_HL(ip->ip_vhl) << 2;
651 #else
652 		off = ip->ip_hl << 2;
653 #endif
654 		nxt = ip->ip_p;
655 	} else {
656 		struct ip ih;
657 
658 		m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
659 		if (ih.ip_off & (IP_MF | IP_OFFMASK))
660 			goto done;
661 #ifdef _IP_VHL
662 		off = _IP_VHL_HL(ih.ip_vhl) << 2;
663 #else
664 		off = ih.ip_hl << 2;
665 #endif
666 		nxt = ih.ip_p;
667 	}
668 
669 	while (off < m->m_pkthdr.len) {
670 		struct ip6_ext ip6e;
671 		struct tcphdr th;
672 		struct udphdr uh;
673 
674 		switch (nxt) {
675 		case IPPROTO_TCP:
676 			spidx->ul_proto = nxt;
677 			if (!needport)
678 				goto done_proto;
679 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
680 				goto done;
681 			m_copydata(m, off, sizeof (th), (caddr_t) &th);
682 			spidx->src.sin.sin_port = th.th_sport;
683 			spidx->dst.sin.sin_port = th.th_dport;
684 			return;
685 		case IPPROTO_UDP:
686 			spidx->ul_proto = nxt;
687 			if (!needport)
688 				goto done_proto;
689 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
690 				goto done;
691 			m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
692 			spidx->src.sin.sin_port = uh.uh_sport;
693 			spidx->dst.sin.sin_port = uh.uh_dport;
694 			return;
695 		case IPPROTO_AH:
696 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
697 				goto done;
698 			/* XXX sigh, this works but is totally bogus */
699 			m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
700 			off += (ip6e.ip6e_len + 2) << 2;
701 			nxt = ip6e.ip6e_nxt;
702 			break;
703 		case IPPROTO_ICMP:
704 		default:
705 			/* XXX intermediate headers??? */
706 			spidx->ul_proto = nxt;
707 			goto done_proto;
708 		}
709 	}
710 done:
711 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
712 done_proto:
713 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
714 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
715 }
716 
717 /* assumes that m is sane */
718 static int
719 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
720 {
721 	static const struct sockaddr_in template = {
722 		sizeof (struct sockaddr_in),
723 		AF_INET,
724 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
725 	};
726 
727 	spidx->src.sin = template;
728 	spidx->dst.sin = template;
729 
730 	if (m->m_len < sizeof (struct ip)) {
731 		m_copydata(m, offsetof(struct ip, ip_src),
732 			   sizeof (struct  in_addr),
733 			   (caddr_t) &spidx->src.sin.sin_addr);
734 		m_copydata(m, offsetof(struct ip, ip_dst),
735 			   sizeof (struct  in_addr),
736 			   (caddr_t) &spidx->dst.sin.sin_addr);
737 	} else {
738 		struct ip *ip = mtod(m, struct ip *);
739 		spidx->src.sin.sin_addr = ip->ip_src;
740 		spidx->dst.sin.sin_addr = ip->ip_dst;
741 	}
742 
743 	spidx->prefs = sizeof(struct in_addr) << 3;
744 	spidx->prefd = sizeof(struct in_addr) << 3;
745 
746 	return 0;
747 }
748 
749 #ifdef INET6
750 static void
751 ipsec6_get_ulp(m, spidx, needport)
752 	struct mbuf *m;
753 	struct secpolicyindex *spidx;
754 	int needport;
755 {
756 	int off, nxt;
757 	struct tcphdr th;
758 	struct udphdr uh;
759 	struct icmp6_hdr ih;
760 
761 	/* sanity check */
762 	if (m == NULL)
763 		panic("%s: NULL pointer was passed.\n", __func__);
764 
765 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
766 		printf("%s:\n", __func__); kdebug_mbuf(m));
767 
768 	/* set default */
769 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
770 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
771 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
772 
773 	nxt = -1;
774 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
775 	if (off < 0 || m->m_pkthdr.len < off)
776 		return;
777 
778 	switch (nxt) {
779 	case IPPROTO_TCP:
780 		spidx->ul_proto = nxt;
781 		if (!needport)
782 			break;
783 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
784 			break;
785 		m_copydata(m, off, sizeof(th), (caddr_t)&th);
786 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
787 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
788 		break;
789 	case IPPROTO_UDP:
790 		spidx->ul_proto = nxt;
791 		if (!needport)
792 			break;
793 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
794 			break;
795 		m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
796 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
797 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
798 		break;
799 	case IPPROTO_ICMPV6:
800 		spidx->ul_proto = nxt;
801 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
802 			break;
803 		m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
804 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
805 		    htons((uint16_t)ih.icmp6_type);
806 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
807 		    htons((uint16_t)ih.icmp6_code);
808 		break;
809 	default:
810 		/* XXX intermediate headers??? */
811 		spidx->ul_proto = nxt;
812 		break;
813 	}
814 }
815 
816 /* assumes that m is sane */
817 static int
818 ipsec6_setspidx_ipaddr(m, spidx)
819 	struct mbuf *m;
820 	struct secpolicyindex *spidx;
821 {
822 	struct ip6_hdr *ip6 = NULL;
823 	struct ip6_hdr ip6buf;
824 	struct sockaddr_in6 *sin6;
825 
826 	if (m->m_len >= sizeof(*ip6))
827 		ip6 = mtod(m, struct ip6_hdr *);
828 	else {
829 		m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
830 		ip6 = &ip6buf;
831 	}
832 
833 	sin6 = (struct sockaddr_in6 *)&spidx->src;
834 	bzero(sin6, sizeof(*sin6));
835 	sin6->sin6_family = AF_INET6;
836 	sin6->sin6_len = sizeof(struct sockaddr_in6);
837 	bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
838 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
839 		sin6->sin6_addr.s6_addr16[1] = 0;
840 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
841 	}
842 	spidx->prefs = sizeof(struct in6_addr) << 3;
843 
844 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
845 	bzero(sin6, sizeof(*sin6));
846 	sin6->sin6_family = AF_INET6;
847 	sin6->sin6_len = sizeof(struct sockaddr_in6);
848 	bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
849 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
850 		sin6->sin6_addr.s6_addr16[1] = 0;
851 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
852 	}
853 	spidx->prefd = sizeof(struct in6_addr) << 3;
854 
855 	return 0;
856 }
857 #endif
858 
859 static void
860 ipsec_delpcbpolicy(p)
861 	struct inpcbpolicy *p;
862 {
863 	free(p, M_IPSEC_INPCB);
864 }
865 
866 /* initialize policy in PCB */
867 int
868 ipsec_init_policy(so, pcb_sp)
869 	struct socket *so;
870 	struct inpcbpolicy **pcb_sp;
871 {
872 	struct inpcbpolicy *new;
873 
874 	/* sanity check. */
875 	if (so == NULL || pcb_sp == NULL)
876 		panic("%s: NULL pointer was passed.\n", __func__);
877 
878 	new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
879 					    M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
880 	if (new == NULL) {
881 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
882 		return ENOBUFS;
883 	}
884 
885 	new->priv = IPSEC_IS_PRIVILEGED_SO(so);
886 
887 	if ((new->sp_in = KEY_NEWSP()) == NULL) {
888 		ipsec_delpcbpolicy(new);
889 		return ENOBUFS;
890 	}
891 	new->sp_in->state = IPSEC_SPSTATE_ALIVE;
892 	new->sp_in->policy = IPSEC_POLICY_ENTRUST;
893 
894 	if ((new->sp_out = KEY_NEWSP()) == NULL) {
895 		KEY_FREESP(&new->sp_in);
896 		ipsec_delpcbpolicy(new);
897 		return ENOBUFS;
898 	}
899 	new->sp_out->state = IPSEC_SPSTATE_ALIVE;
900 	new->sp_out->policy = IPSEC_POLICY_ENTRUST;
901 
902 	*pcb_sp = new;
903 
904 	return 0;
905 }
906 
907 /* copy old ipsec policy into new */
908 int
909 ipsec_copy_policy(old, new)
910 	struct inpcbpolicy *old, *new;
911 {
912 	struct secpolicy *sp;
913 
914 	sp = ipsec_deepcopy_policy(old->sp_in);
915 	if (sp) {
916 		KEY_FREESP(&new->sp_in);
917 		new->sp_in = sp;
918 	} else
919 		return ENOBUFS;
920 
921 	sp = ipsec_deepcopy_policy(old->sp_out);
922 	if (sp) {
923 		KEY_FREESP(&new->sp_out);
924 		new->sp_out = sp;
925 	} else
926 		return ENOBUFS;
927 
928 	new->priv = old->priv;
929 
930 	return 0;
931 }
932 
933 struct ipsecrequest *
934 ipsec_newisr(void)
935 {
936 	struct ipsecrequest *p;
937 
938 	p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
939 	if (p != NULL)
940 		IPSECREQUEST_LOCK_INIT(p);
941 	return p;
942 }
943 
944 void
945 ipsec_delisr(struct ipsecrequest *p)
946 {
947 	IPSECREQUEST_LOCK_DESTROY(p);
948 	free(p, M_IPSEC_SR);
949 }
950 
951 /* deep-copy a policy in PCB */
952 static struct secpolicy *
953 ipsec_deepcopy_policy(src)
954 	struct secpolicy *src;
955 {
956 	struct ipsecrequest *newchain = NULL;
957 	struct ipsecrequest *p;
958 	struct ipsecrequest **q;
959 	struct ipsecrequest *r;
960 	struct secpolicy *dst;
961 
962 	if (src == NULL)
963 		return NULL;
964 	dst = KEY_NEWSP();
965 	if (dst == NULL)
966 		return NULL;
967 
968 	/*
969 	 * deep-copy IPsec request chain.  This is required since struct
970 	 * ipsecrequest is not reference counted.
971 	 */
972 	q = &newchain;
973 	for (p = src->req; p; p = p->next) {
974 		*q = ipsec_newisr();
975 		if (*q == NULL)
976 			goto fail;
977 		(*q)->saidx.proto = p->saidx.proto;
978 		(*q)->saidx.mode = p->saidx.mode;
979 		(*q)->level = p->level;
980 		(*q)->saidx.reqid = p->saidx.reqid;
981 
982 		bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
983 		bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
984 
985 		(*q)->sp = dst;
986 
987 		q = &((*q)->next);
988 	}
989 
990 	dst->req = newchain;
991 	dst->state = src->state;
992 	dst->policy = src->policy;
993 	/* do not touch the refcnt fields */
994 
995 	return dst;
996 
997 fail:
998 	for (p = newchain; p; p = r) {
999 		r = p->next;
1000 		ipsec_delisr(p);
1001 		p = NULL;
1002 	}
1003 	return NULL;
1004 }
1005 
1006 /* set policy and ipsec request if present. */
1007 static int
1008 ipsec_set_policy(pcb_sp, optname, request, len, cred)
1009 	struct secpolicy **pcb_sp;
1010 	int optname;
1011 	caddr_t request;
1012 	size_t len;
1013 	struct ucred *cred;
1014 {
1015 	struct sadb_x_policy *xpl;
1016 	struct secpolicy *newsp = NULL;
1017 	int error;
1018 
1019 	/* sanity check. */
1020 	if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
1021 		return EINVAL;
1022 	if (len < sizeof(*xpl))
1023 		return EINVAL;
1024 	xpl = (struct sadb_x_policy *)request;
1025 
1026 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1027 		printf("%s: passed policy\n", __func__);
1028 		kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1029 
1030 	/* check policy type */
1031 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1032 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1033 	 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1034 		return EINVAL;
1035 
1036 	/* check privileged socket */
1037 	if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1038 		error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1039 		if (error)
1040 			return EACCES;
1041 	}
1042 
1043 	/* allocation new SP entry */
1044 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1045 		return error;
1046 
1047 	newsp->state = IPSEC_SPSTATE_ALIVE;
1048 
1049 	/* clear old SP and set new SP */
1050 	KEY_FREESP(pcb_sp);
1051 	*pcb_sp = newsp;
1052 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1053 		printf("%s: new policy\n", __func__);
1054 		kdebug_secpolicy(newsp));
1055 
1056 	return 0;
1057 }
1058 
1059 static int
1060 ipsec_get_policy(pcb_sp, mp)
1061 	struct secpolicy *pcb_sp;
1062 	struct mbuf **mp;
1063 {
1064 
1065 	/* sanity check. */
1066 	if (pcb_sp == NULL || mp == NULL)
1067 		return EINVAL;
1068 
1069 	*mp = key_sp2msg(pcb_sp);
1070 	if (!*mp) {
1071 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1072 		return ENOBUFS;
1073 	}
1074 
1075 	(*mp)->m_type = MT_DATA;
1076 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1077 		printf("%s:\n", __func__); kdebug_mbuf(*mp));
1078 
1079 	return 0;
1080 }
1081 
1082 int
1083 ipsec4_set_policy(inp, optname, request, len, cred)
1084 	struct inpcb *inp;
1085 	int optname;
1086 	caddr_t request;
1087 	size_t len;
1088 	struct ucred *cred;
1089 {
1090 	struct sadb_x_policy *xpl;
1091 	struct secpolicy **pcb_sp;
1092 
1093 	/* sanity check. */
1094 	if (inp == NULL || request == NULL)
1095 		return EINVAL;
1096 	if (len < sizeof(*xpl))
1097 		return EINVAL;
1098 	xpl = (struct sadb_x_policy *)request;
1099 
1100 	/* select direction */
1101 	switch (xpl->sadb_x_policy_dir) {
1102 	case IPSEC_DIR_INBOUND:
1103 		pcb_sp = &inp->inp_sp->sp_in;
1104 		break;
1105 	case IPSEC_DIR_OUTBOUND:
1106 		pcb_sp = &inp->inp_sp->sp_out;
1107 		break;
1108 	default:
1109 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1110 			xpl->sadb_x_policy_dir));
1111 		return EINVAL;
1112 	}
1113 
1114 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
1115 }
1116 
1117 int
1118 ipsec4_get_policy(inp, request, len, mp)
1119 	struct inpcb *inp;
1120 	caddr_t request;
1121 	size_t len;
1122 	struct mbuf **mp;
1123 {
1124 	struct sadb_x_policy *xpl;
1125 	struct secpolicy *pcb_sp;
1126 
1127 	/* sanity check. */
1128 	if (inp == NULL || request == NULL || mp == NULL)
1129 		return EINVAL;
1130 	IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1131 	if (len < sizeof(*xpl))
1132 		return EINVAL;
1133 	xpl = (struct sadb_x_policy *)request;
1134 
1135 	/* select direction */
1136 	switch (xpl->sadb_x_policy_dir) {
1137 	case IPSEC_DIR_INBOUND:
1138 		pcb_sp = inp->inp_sp->sp_in;
1139 		break;
1140 	case IPSEC_DIR_OUTBOUND:
1141 		pcb_sp = inp->inp_sp->sp_out;
1142 		break;
1143 	default:
1144 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1145 			xpl->sadb_x_policy_dir));
1146 		return EINVAL;
1147 	}
1148 
1149 	return ipsec_get_policy(pcb_sp, mp);
1150 }
1151 
1152 /* delete policy in PCB */
1153 int
1154 ipsec4_delete_pcbpolicy(inp)
1155 	struct inpcb *inp;
1156 {
1157 	IPSEC_ASSERT(inp != NULL, ("null inp"));
1158 
1159 	if (inp->inp_sp == NULL)
1160 		return 0;
1161 
1162 	if (inp->inp_sp->sp_in != NULL)
1163 		KEY_FREESP(&inp->inp_sp->sp_in);
1164 
1165 	if (inp->inp_sp->sp_out != NULL)
1166 		KEY_FREESP(&inp->inp_sp->sp_out);
1167 
1168 	ipsec_delpcbpolicy(inp->inp_sp);
1169 	inp->inp_sp = NULL;
1170 
1171 	return 0;
1172 }
1173 
1174 #ifdef INET6
1175 int
1176 ipsec6_set_policy(in6p, optname, request, len, cred)
1177 	struct in6pcb *in6p;
1178 	int optname;
1179 	caddr_t request;
1180 	size_t len;
1181 	struct ucred *cred;
1182 {
1183 	struct sadb_x_policy *xpl;
1184 	struct secpolicy **pcb_sp;
1185 
1186 	/* sanity check. */
1187 	if (in6p == NULL || request == NULL)
1188 		return EINVAL;
1189 	if (len < sizeof(*xpl))
1190 		return EINVAL;
1191 	xpl = (struct sadb_x_policy *)request;
1192 
1193 	/* select direction */
1194 	switch (xpl->sadb_x_policy_dir) {
1195 	case IPSEC_DIR_INBOUND:
1196 		pcb_sp = &in6p->in6p_sp->sp_in;
1197 		break;
1198 	case IPSEC_DIR_OUTBOUND:
1199 		pcb_sp = &in6p->in6p_sp->sp_out;
1200 		break;
1201 	default:
1202 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1203 			xpl->sadb_x_policy_dir));
1204 		return EINVAL;
1205 	}
1206 
1207 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
1208 }
1209 
1210 int
1211 ipsec6_get_policy(in6p, request, len, mp)
1212 	struct in6pcb *in6p;
1213 	caddr_t request;
1214 	size_t len;
1215 	struct mbuf **mp;
1216 {
1217 	struct sadb_x_policy *xpl;
1218 	struct secpolicy *pcb_sp;
1219 
1220 	/* sanity check. */
1221 	if (in6p == NULL || request == NULL || mp == NULL)
1222 		return EINVAL;
1223 	IPSEC_ASSERT(in6p->in6p_sp != NULL, ("null in6p_sp"));
1224 	if (len < sizeof(*xpl))
1225 		return EINVAL;
1226 	xpl = (struct sadb_x_policy *)request;
1227 
1228 	/* select direction */
1229 	switch (xpl->sadb_x_policy_dir) {
1230 	case IPSEC_DIR_INBOUND:
1231 		pcb_sp = in6p->in6p_sp->sp_in;
1232 		break;
1233 	case IPSEC_DIR_OUTBOUND:
1234 		pcb_sp = in6p->in6p_sp->sp_out;
1235 		break;
1236 	default:
1237 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1238 			xpl->sadb_x_policy_dir));
1239 		return EINVAL;
1240 	}
1241 
1242 	return ipsec_get_policy(pcb_sp, mp);
1243 }
1244 
1245 int
1246 ipsec6_delete_pcbpolicy(in6p)
1247 	struct in6pcb *in6p;
1248 {
1249 	IPSEC_ASSERT(in6p != NULL, ("null in6p"));
1250 
1251 	if (in6p->in6p_sp == NULL)
1252 		return 0;
1253 
1254 	if (in6p->in6p_sp->sp_in != NULL)
1255 		KEY_FREESP(&in6p->in6p_sp->sp_in);
1256 
1257 	if (in6p->in6p_sp->sp_out != NULL)
1258 		KEY_FREESP(&in6p->in6p_sp->sp_out);
1259 
1260 	ipsec_delpcbpolicy(in6p->in6p_sp);
1261 	in6p->in6p_sp = NULL;
1262 
1263 	return 0;
1264 }
1265 #endif
1266 
1267 /*
1268  * return current level.
1269  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1270  */
1271 u_int
1272 ipsec_get_reqlevel(isr)
1273 	struct ipsecrequest *isr;
1274 {
1275 	u_int level = 0;
1276 	u_int esp_trans_deflev, esp_net_deflev;
1277 	u_int ah_trans_deflev, ah_net_deflev;
1278 
1279 	IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1280 	IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1281 		("af family mismatch, src %u, dst %u",
1282 		 isr->sp->spidx.src.sa.sa_family,
1283 		 isr->sp->spidx.dst.sa.sa_family));
1284 
1285 /* XXX note that we have ipseclog() expanded here - code sync issue */
1286 #define IPSEC_CHECK_DEFAULT(lev) \
1287 	(((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE	      \
1288 			&& (lev) != IPSEC_LEVEL_UNIQUE)			      \
1289 		? (ipsec_debug						      \
1290 			? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1291 				(lev), IPSEC_LEVEL_REQUIRE)		      \
1292 			: 0),						      \
1293 			(lev) = IPSEC_LEVEL_REQUIRE,			      \
1294 			(lev)						      \
1295 		: (lev))
1296 
1297 	/* set default level */
1298 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1299 #ifdef INET
1300 	case AF_INET:
1301 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1302 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1303 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1304 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1305 		break;
1306 #endif
1307 #ifdef INET6
1308 	case AF_INET6:
1309 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1310 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1311 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1312 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1313 		break;
1314 #endif /* INET6 */
1315 	default:
1316 		panic("%s: unknown af %u",
1317 			__func__, isr->sp->spidx.src.sa.sa_family);
1318 	}
1319 
1320 #undef IPSEC_CHECK_DEFAULT
1321 
1322 	/* set level */
1323 	switch (isr->level) {
1324 	case IPSEC_LEVEL_DEFAULT:
1325 		switch (isr->saidx.proto) {
1326 		case IPPROTO_ESP:
1327 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1328 				level = esp_net_deflev;
1329 			else
1330 				level = esp_trans_deflev;
1331 			break;
1332 		case IPPROTO_AH:
1333 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1334 				level = ah_net_deflev;
1335 			else
1336 				level = ah_trans_deflev;
1337 			break;
1338 		case IPPROTO_IPCOMP:
1339 			/*
1340 			 * we don't really care, as IPcomp document says that
1341 			 * we shouldn't compress small packets
1342 			 */
1343 			level = IPSEC_LEVEL_USE;
1344 			break;
1345 		default:
1346 			panic("%s: Illegal protocol defined %u\n", __func__,
1347 				isr->saidx.proto);
1348 		}
1349 		break;
1350 
1351 	case IPSEC_LEVEL_USE:
1352 	case IPSEC_LEVEL_REQUIRE:
1353 		level = isr->level;
1354 		break;
1355 	case IPSEC_LEVEL_UNIQUE:
1356 		level = IPSEC_LEVEL_REQUIRE;
1357 		break;
1358 
1359 	default:
1360 		panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1361 	}
1362 
1363 	return level;
1364 }
1365 
1366 /*
1367  * Check security policy requirements against the actual
1368  * packet contents.  Return one if the packet should be
1369  * reject as "invalid"; otherwiser return zero to have the
1370  * packet treated as "valid".
1371  *
1372  * OUT:
1373  *	0: valid
1374  *	1: invalid
1375  */
1376 int
1377 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1378 {
1379 	struct ipsecrequest *isr;
1380 	int need_auth;
1381 
1382 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1383 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1384 
1385 	/* check policy */
1386 	switch (sp->policy) {
1387 	case IPSEC_POLICY_DISCARD:
1388 		return 1;
1389 	case IPSEC_POLICY_BYPASS:
1390 	case IPSEC_POLICY_NONE:
1391 		return 0;
1392 	}
1393 
1394 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1395 		("invalid policy %u", sp->policy));
1396 
1397 	/* XXX should compare policy against ipsec header history */
1398 
1399 	need_auth = 0;
1400 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1401 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1402 			continue;
1403 		switch (isr->saidx.proto) {
1404 		case IPPROTO_ESP:
1405 			if ((m->m_flags & M_DECRYPTED) == 0) {
1406 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1407 				    printf("%s: ESP m_flags:%x\n", __func__,
1408 					    m->m_flags));
1409 				return 1;
1410 			}
1411 
1412 			if (!need_auth &&
1413 			    isr->sav != NULL &&
1414 			    isr->sav->tdb_authalgxform != NULL &&
1415 			    (m->m_flags & M_AUTHIPDGM) == 0) {
1416 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1417 				    printf("%s: ESP/AH m_flags:%x\n", __func__,
1418 					    m->m_flags));
1419 				return 1;
1420 			}
1421 			break;
1422 		case IPPROTO_AH:
1423 			need_auth = 1;
1424 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
1425 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1426 				    printf("%s: AH m_flags:%x\n", __func__,
1427 					    m->m_flags));
1428 				return 1;
1429 			}
1430 			break;
1431 		case IPPROTO_IPCOMP:
1432 			/*
1433 			 * we don't really care, as IPcomp document
1434 			 * says that we shouldn't compress small
1435 			 * packets, IPComp policy should always be
1436 			 * treated as being in "use" level.
1437 			 */
1438 			break;
1439 		}
1440 	}
1441 	return 0;		/* valid */
1442 }
1443 
1444 /*
1445  * Check AH/ESP integrity.
1446  * This function is called from tcp_input(), udp_input(),
1447  * and {ah,esp}4_input for tunnel mode
1448  */
1449 int
1450 ipsec4_in_reject(m, inp)
1451 	struct mbuf *m;
1452 	struct inpcb *inp;
1453 {
1454 	struct secpolicy *sp;
1455 	int error;
1456 	int result;
1457 
1458 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
1459 
1460 	/* get SP for this packet.
1461 	 * When we are called from ip_forward(), we call
1462 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1463 	 */
1464 	if (inp == NULL)
1465 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1466 	else
1467 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1468 
1469 	if (sp != NULL) {
1470 		result = ipsec_in_reject(sp, m);
1471 		if (result)
1472 			ipsec4stat.ips_in_polvio++;
1473 		KEY_FREESP(&sp);
1474 	} else {
1475 		result = 0;	/* XXX should be panic ?
1476 				 * -> No, there may be error. */
1477 	}
1478 	return result;
1479 }
1480 
1481 #ifdef INET6
1482 /*
1483  * Check AH/ESP integrity.
1484  * This function is called from tcp6_input(), udp6_input(),
1485  * and {ah,esp}6_input for tunnel mode
1486  */
1487 int
1488 ipsec6_in_reject(m, inp)
1489 	struct mbuf *m;
1490 	struct inpcb *inp;
1491 {
1492 	struct secpolicy *sp = NULL;
1493 	int error;
1494 	int result;
1495 
1496 	/* sanity check */
1497 	if (m == NULL)
1498 		return 0;	/* XXX should be panic ? */
1499 
1500 	/* get SP for this packet.
1501 	 * When we are called from ip_forward(), we call
1502 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1503 	 */
1504 	if (inp == NULL)
1505 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1506 	else
1507 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1508 
1509 	if (sp != NULL) {
1510 		result = ipsec_in_reject(sp, m);
1511 		if (result)
1512 			ipsec6stat.ips_in_polvio++;
1513 		KEY_FREESP(&sp);
1514 	} else {
1515 		result = 0;
1516 	}
1517 	return result;
1518 }
1519 #endif
1520 
1521 /*
1522  * compute the byte size to be occupied by IPsec header.
1523  * in case it is tunneled, it includes the size of outer IP header.
1524  * NOTE: SP passed is free in this function.
1525  */
1526 static size_t
1527 ipsec_hdrsiz(struct secpolicy *sp)
1528 {
1529 	struct ipsecrequest *isr;
1530 	size_t siz;
1531 
1532 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1533 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1534 
1535 	switch (sp->policy) {
1536 	case IPSEC_POLICY_DISCARD:
1537 	case IPSEC_POLICY_BYPASS:
1538 	case IPSEC_POLICY_NONE:
1539 		return 0;
1540 	}
1541 
1542 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1543 		("invalid policy %u", sp->policy));
1544 
1545 	siz = 0;
1546 	for (isr = sp->req; isr != NULL; isr = isr->next) {
1547 		size_t clen = 0;
1548 
1549 		switch (isr->saidx.proto) {
1550 		case IPPROTO_ESP:
1551 			clen = esp_hdrsiz(isr->sav);
1552 			break;
1553 		case IPPROTO_AH:
1554 			clen = ah_hdrsiz(isr->sav);
1555 			break;
1556 		case IPPROTO_IPCOMP:
1557 			clen = sizeof(struct ipcomp);
1558 			break;
1559 		}
1560 
1561 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1562 			switch (isr->saidx.dst.sa.sa_family) {
1563 			case AF_INET:
1564 				clen += sizeof(struct ip);
1565 				break;
1566 #ifdef INET6
1567 			case AF_INET6:
1568 				clen += sizeof(struct ip6_hdr);
1569 				break;
1570 #endif
1571 			default:
1572 				ipseclog((LOG_ERR, "%s: unknown AF %d in "
1573 				    "IPsec tunnel SA\n", __func__,
1574 				    ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1575 				break;
1576 			}
1577 		}
1578 		siz += clen;
1579 	}
1580 
1581 	return siz;
1582 }
1583 
1584 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1585 size_t
1586 ipsec4_hdrsiz(m, dir, inp)
1587 	struct mbuf *m;
1588 	u_int dir;
1589 	struct inpcb *inp;
1590 {
1591 	struct secpolicy *sp;
1592 	int error;
1593 	size_t size;
1594 
1595 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
1596 
1597 	/* get SP for this packet.
1598 	 * When we are called from ip_forward(), we call
1599 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1600 	 */
1601 	if (inp == NULL)
1602 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1603 	else
1604 		sp = ipsec_getpolicybysock(m, dir, inp, &error);
1605 
1606 	if (sp != NULL) {
1607 		size = ipsec_hdrsiz(sp);
1608 		KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1609 			printf("%s: size:%lu.\n", __func__,
1610 				(unsigned long)size));
1611 
1612 		KEY_FREESP(&sp);
1613 	} else {
1614 		size = 0;	/* XXX should be panic ?
1615 				 * -> No, we are called w/o knowing if
1616 				 *    IPsec processing is needed. */
1617 	}
1618 	return size;
1619 }
1620 
1621 #ifdef INET6
1622 /* This function is called from ipsec6_hdrsize_tcp(),
1623  * and maybe from ip6_forward.()
1624  */
1625 size_t
1626 ipsec6_hdrsiz(m, dir, in6p)
1627 	struct mbuf *m;
1628 	u_int dir;
1629 	struct in6pcb *in6p;
1630 {
1631 	struct secpolicy *sp;
1632 	int error;
1633 	size_t size;
1634 
1635 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
1636 	IPSEC_ASSERT(in6p == NULL || in6p->in6p_socket != NULL,
1637 		("socket w/o inpcb"));
1638 
1639 	/* get SP for this packet */
1640 	/* XXX Is it right to call with IP_FORWARDING. */
1641 	if (in6p == NULL)
1642 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1643 	else
1644 		sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1645 
1646 	if (sp == NULL)
1647 		return 0;
1648 	size = ipsec_hdrsiz(sp);
1649 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1650 		printf("%s: size:%lu.\n", __func__, (unsigned long)size));
1651 	KEY_FREESP(&sp);
1652 
1653 	return size;
1654 }
1655 #endif /*INET6*/
1656 
1657 /*
1658  * Check the variable replay window.
1659  * ipsec_chkreplay() performs replay check before ICV verification.
1660  * ipsec_updatereplay() updates replay bitmap.  This must be called after
1661  * ICV verification (it also performs replay check, which is usually done
1662  * beforehand).
1663  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1664  *
1665  * based on RFC 2401.
1666  */
1667 int
1668 ipsec_chkreplay(seq, sav)
1669 	u_int32_t seq;
1670 	struct secasvar *sav;
1671 {
1672 	const struct secreplay *replay;
1673 	u_int32_t diff;
1674 	int fr;
1675 	u_int32_t wsizeb;	/* constant: bits of window size */
1676 	int frlast;		/* constant: last frame */
1677 
1678 	IPSEC_SPLASSERT_SOFTNET(__func__);
1679 
1680 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
1681 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1682 
1683 	replay = sav->replay;
1684 
1685 	if (replay->wsize == 0)
1686 		return 1;	/* no need to check replay. */
1687 
1688 	/* constant */
1689 	frlast = replay->wsize - 1;
1690 	wsizeb = replay->wsize << 3;
1691 
1692 	/* sequence number of 0 is invalid */
1693 	if (seq == 0)
1694 		return 0;
1695 
1696 	/* first time is always okay */
1697 	if (replay->count == 0)
1698 		return 1;
1699 
1700 	if (seq > replay->lastseq) {
1701 		/* larger sequences are okay */
1702 		return 1;
1703 	} else {
1704 		/* seq is equal or less than lastseq. */
1705 		diff = replay->lastseq - seq;
1706 
1707 		/* over range to check, i.e. too old or wrapped */
1708 		if (diff >= wsizeb)
1709 			return 0;
1710 
1711 		fr = frlast - diff / 8;
1712 
1713 		/* this packet already seen ? */
1714 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1715 			return 0;
1716 
1717 		/* out of order but good */
1718 		return 1;
1719 	}
1720 }
1721 
1722 /*
1723  * check replay counter whether to update or not.
1724  * OUT:	0:	OK
1725  *	1:	NG
1726  */
1727 int
1728 ipsec_updatereplay(seq, sav)
1729 	u_int32_t seq;
1730 	struct secasvar *sav;
1731 {
1732 	struct secreplay *replay;
1733 	u_int32_t diff;
1734 	int fr;
1735 	u_int32_t wsizeb;	/* constant: bits of window size */
1736 	int frlast;		/* constant: last frame */
1737 
1738 	IPSEC_SPLASSERT_SOFTNET(__func__);
1739 
1740 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
1741 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1742 
1743 	replay = sav->replay;
1744 
1745 	if (replay->wsize == 0)
1746 		goto ok;	/* no need to check replay. */
1747 
1748 	/* constant */
1749 	frlast = replay->wsize - 1;
1750 	wsizeb = replay->wsize << 3;
1751 
1752 	/* sequence number of 0 is invalid */
1753 	if (seq == 0)
1754 		return 1;
1755 
1756 	/* first time */
1757 	if (replay->count == 0) {
1758 		replay->lastseq = seq;
1759 		bzero(replay->bitmap, replay->wsize);
1760 		(replay->bitmap)[frlast] = 1;
1761 		goto ok;
1762 	}
1763 
1764 	if (seq > replay->lastseq) {
1765 		/* seq is larger than lastseq. */
1766 		diff = seq - replay->lastseq;
1767 
1768 		/* new larger sequence number */
1769 		if (diff < wsizeb) {
1770 			/* In window */
1771 			/* set bit for this packet */
1772 			vshiftl(replay->bitmap, diff, replay->wsize);
1773 			(replay->bitmap)[frlast] |= 1;
1774 		} else {
1775 			/* this packet has a "way larger" */
1776 			bzero(replay->bitmap, replay->wsize);
1777 			(replay->bitmap)[frlast] = 1;
1778 		}
1779 		replay->lastseq = seq;
1780 
1781 		/* larger is good */
1782 	} else {
1783 		/* seq is equal or less than lastseq. */
1784 		diff = replay->lastseq - seq;
1785 
1786 		/* over range to check, i.e. too old or wrapped */
1787 		if (diff >= wsizeb)
1788 			return 1;
1789 
1790 		fr = frlast - diff / 8;
1791 
1792 		/* this packet already seen ? */
1793 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1794 			return 1;
1795 
1796 		/* mark as seen */
1797 		(replay->bitmap)[fr] |= (1 << (diff % 8));
1798 
1799 		/* out of order but good */
1800 	}
1801 
1802 ok:
1803 	if (replay->count == ~0) {
1804 
1805 		/* set overflow flag */
1806 		replay->overflow++;
1807 
1808 		/* don't increment, no more packets accepted */
1809 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1810 			return 1;
1811 
1812 		ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1813 		    __func__, replay->overflow, ipsec_logsastr(sav)));
1814 	}
1815 
1816 	replay->count++;
1817 
1818 	return 0;
1819 }
1820 
1821 /*
1822  * shift variable length buffer to left.
1823  * IN:	bitmap: pointer to the buffer
1824  * 	nbit:	the number of to shift.
1825  *	wsize:	buffer size (bytes).
1826  */
1827 static void
1828 vshiftl(bitmap, nbit, wsize)
1829 	unsigned char *bitmap;
1830 	int nbit, wsize;
1831 {
1832 	int s, j, i;
1833 	unsigned char over;
1834 
1835 	for (j = 0; j < nbit; j += 8) {
1836 		s = (nbit - j < 8) ? (nbit - j): 8;
1837 		bitmap[0] <<= s;
1838 		for (i = 1; i < wsize; i++) {
1839 			over = (bitmap[i] >> (8 - s));
1840 			bitmap[i] <<= s;
1841 			bitmap[i-1] |= over;
1842 		}
1843 	}
1844 
1845 	return;
1846 }
1847 
1848 /* Return a printable string for the IPv4 address. */
1849 static char *
1850 inet_ntoa4(struct in_addr ina)
1851 {
1852 	static char buf[4][4 * sizeof "123" + 4];
1853 	unsigned char *ucp = (unsigned char *) &ina;
1854 	static int i = 3;
1855 
1856 	/* XXX-BZ returns static buffer. */
1857 	i = (i + 1) % 4;
1858 	sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1859 	    ucp[2] & 0xff, ucp[3] & 0xff);
1860 	return (buf[i]);
1861 }
1862 
1863 /* Return a printable string for the address. */
1864 char *
1865 ipsec_address(union sockaddr_union* sa)
1866 {
1867 #ifdef INET6
1868 	char ip6buf[INET6_ADDRSTRLEN];
1869 #endif
1870 	switch (sa->sa.sa_family) {
1871 #ifdef INET
1872 	case AF_INET:
1873 		return inet_ntoa4(sa->sin.sin_addr);
1874 #endif /* INET */
1875 
1876 #ifdef INET6
1877 	case AF_INET6:
1878 		return ip6_sprintf(ip6buf, &sa->sin6.sin6_addr);
1879 #endif /* INET6 */
1880 
1881 	default:
1882 		return "(unknown address family)";
1883 	}
1884 }
1885 
1886 const char *
1887 ipsec_logsastr(sav)
1888 	struct secasvar *sav;
1889 {
1890 	static char buf[256];
1891 	char *p;
1892 	struct secasindex *saidx = &sav->sah->saidx;
1893 
1894 	IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1895 		("address family mismatch"));
1896 
1897 	p = buf;
1898 	snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1899 	while (p && *p)
1900 		p++;
1901 	/* NB: only use ipsec_address on one address at a time */
1902 	snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1903 		ipsec_address(&saidx->src));
1904 	while (p && *p)
1905 		p++;
1906 	snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1907 		ipsec_address(&saidx->dst));
1908 
1909 	return buf;
1910 }
1911 
1912 void
1913 ipsec_dumpmbuf(m)
1914 	struct mbuf *m;
1915 {
1916 	int totlen;
1917 	int i;
1918 	u_char *p;
1919 
1920 	totlen = 0;
1921 	printf("---\n");
1922 	while (m) {
1923 		p = mtod(m, u_char *);
1924 		for (i = 0; i < m->m_len; i++) {
1925 			printf("%02x ", p[i]);
1926 			totlen++;
1927 			if (totlen % 16 == 0)
1928 				printf("\n");
1929 		}
1930 		m = m->m_next;
1931 	}
1932 	if (totlen % 16 != 0)
1933 		printf("\n");
1934 	printf("---\n");
1935 }
1936 
1937 static void
1938 ipsec_attach(void)
1939 {
1940 	SECPOLICY_LOCK_INIT(&ip4_def_policy);
1941 	ip4_def_policy.refcnt = 1;			/* NB: disallow free */
1942 }
1943 SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL)
1944 
1945 
1946 /* XXX this stuff doesn't belong here... */
1947 
1948 static	struct xformsw* xforms = NULL;
1949 
1950 /*
1951  * Register a transform; typically at system startup.
1952  */
1953 void
1954 xform_register(struct xformsw* xsp)
1955 {
1956 	xsp->xf_next = xforms;
1957 	xforms = xsp;
1958 }
1959 
1960 /*
1961  * Initialize transform support in an sav.
1962  */
1963 int
1964 xform_init(struct secasvar *sav, int xftype)
1965 {
1966 	struct xformsw *xsp;
1967 
1968 	if (sav->tdb_xform != NULL)	/* previously initialized */
1969 		return 0;
1970 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
1971 		if (xsp->xf_type == xftype)
1972 			return (*xsp->xf_init)(sav, xsp);
1973 	return EINVAL;
1974 }
1975