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