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