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