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