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