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