xref: /freebsd/sys/netipsec/ipsec.c (revision 3377c9613e3e63d79e28205e906412d5cda5d638)
188768458SSam Leffler /*	$FreeBSD$	*/
288768458SSam Leffler /*	$KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $	*/
388768458SSam Leffler 
4c398230bSWarner Losh /*-
588768458SSam Leffler  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
688768458SSam Leffler  * All rights reserved.
788768458SSam Leffler  *
888768458SSam Leffler  * Redistribution and use in source and binary forms, with or without
988768458SSam Leffler  * modification, are permitted provided that the following conditions
1088768458SSam Leffler  * are met:
1188768458SSam Leffler  * 1. Redistributions of source code must retain the above copyright
1288768458SSam Leffler  *    notice, this list of conditions and the following disclaimer.
1388768458SSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
1488768458SSam Leffler  *    notice, this list of conditions and the following disclaimer in the
1588768458SSam Leffler  *    documentation and/or other materials provided with the distribution.
1688768458SSam Leffler  * 3. Neither the name of the project nor the names of its contributors
1788768458SSam Leffler  *    may be used to endorse or promote products derived from this software
1888768458SSam Leffler  *    without specific prior written permission.
1988768458SSam Leffler  *
2088768458SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2188768458SSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2288768458SSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2388768458SSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2488768458SSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2588768458SSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2688768458SSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2788768458SSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2888768458SSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2988768458SSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3088768458SSam Leffler  * SUCH DAMAGE.
3188768458SSam Leffler  */
3288768458SSam Leffler 
3388768458SSam Leffler /*
3488768458SSam Leffler  * IPsec controller part.
3588768458SSam Leffler  */
3688768458SSam Leffler 
3788768458SSam Leffler #include "opt_inet.h"
3888768458SSam Leffler #include "opt_inet6.h"
3988768458SSam Leffler #include "opt_ipsec.h"
4088768458SSam Leffler 
4188768458SSam Leffler #include <sys/param.h>
4288768458SSam Leffler #include <sys/systm.h>
4388768458SSam Leffler #include <sys/malloc.h>
4488768458SSam Leffler #include <sys/mbuf.h>
4588768458SSam Leffler #include <sys/domain.h>
4646ee43b2SRobert Watson #include <sys/priv.h>
4788768458SSam Leffler #include <sys/protosw.h>
4888768458SSam Leffler #include <sys/socket.h>
4988768458SSam Leffler #include <sys/socketvar.h>
5088768458SSam Leffler #include <sys/errno.h>
5188768458SSam Leffler #include <sys/time.h>
5288768458SSam Leffler #include <sys/kernel.h>
5388768458SSam Leffler #include <sys/syslog.h>
5488768458SSam Leffler #include <sys/sysctl.h>
5588768458SSam Leffler #include <sys/proc.h>
5688768458SSam Leffler 
5788768458SSam Leffler #include <net/if.h>
5888768458SSam Leffler #include <net/route.h>
5988768458SSam Leffler 
6088768458SSam Leffler #include <netinet/in.h>
6188768458SSam Leffler #include <netinet/in_systm.h>
6288768458SSam Leffler #include <netinet/ip.h>
6388768458SSam Leffler #include <netinet/ip_var.h>
6488768458SSam Leffler #include <netinet/in_var.h>
6588768458SSam Leffler #include <netinet/udp.h>
6688768458SSam Leffler #include <netinet/udp_var.h>
6788768458SSam Leffler #include <netinet/tcp.h>
6888768458SSam Leffler #include <netinet/udp.h>
6988768458SSam Leffler 
7088768458SSam Leffler #include <netinet/ip6.h>
7188768458SSam Leffler #ifdef INET6
7288768458SSam Leffler #include <netinet6/ip6_var.h>
7388768458SSam Leffler #endif
7488768458SSam Leffler #include <netinet/in_pcb.h>
7588768458SSam Leffler #ifdef INET6
7688768458SSam Leffler #include <netinet/icmp6.h>
7788768458SSam Leffler #endif
7888768458SSam Leffler 
792cb64cb2SGeorge V. Neville-Neil #include <sys/types.h>
8088768458SSam Leffler #include <netipsec/ipsec.h>
8188768458SSam Leffler #ifdef INET6
8288768458SSam Leffler #include <netipsec/ipsec6.h>
8388768458SSam Leffler #endif
8488768458SSam Leffler #include <netipsec/ah_var.h>
8588768458SSam Leffler #include <netipsec/esp_var.h>
8688768458SSam Leffler #include <netipsec/ipcomp.h>		/*XXX*/
8788768458SSam Leffler #include <netipsec/ipcomp_var.h>
8888768458SSam Leffler 
8988768458SSam Leffler #include <netipsec/key.h>
9088768458SSam Leffler #include <netipsec/keydb.h>
9188768458SSam Leffler #include <netipsec/key_debug.h>
9288768458SSam Leffler 
9388768458SSam Leffler #include <netipsec/xform.h>
9488768458SSam Leffler 
9588768458SSam Leffler #include <machine/in_cksum.h>
9688768458SSam Leffler 
977aee3dd1SSam Leffler #include <opencrypto/cryptodev.h>
987aee3dd1SSam Leffler 
9988768458SSam Leffler #ifdef IPSEC_DEBUG
10088768458SSam Leffler int ipsec_debug = 1;
10188768458SSam Leffler #else
10288768458SSam Leffler int ipsec_debug = 0;
10388768458SSam Leffler #endif
10488768458SSam Leffler 
10588768458SSam Leffler /* NB: name changed so netstat doesn't use it */
1062cb64cb2SGeorge V. Neville-Neil struct ipsecstat ipsec4stat;
10788768458SSam Leffler int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
10888768458SSam Leffler int ip4_ipsec_dfbit = 0;	/* DF bit on encap. 0: clear 1: set 2: copy */
10988768458SSam Leffler int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
11088768458SSam Leffler int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
11188768458SSam Leffler int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
11288768458SSam Leffler int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
11388768458SSam Leffler struct secpolicy ip4_def_policy;
11488768458SSam Leffler int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
11588768458SSam Leffler int ip4_esp_randpad = -1;
11688768458SSam Leffler /*
11788768458SSam Leffler  * Crypto support requirements:
11888768458SSam Leffler  *
11988768458SSam Leffler  *  1	require hardware support
12088768458SSam Leffler  * -1	require software support
12188768458SSam Leffler  *  0	take anything
12288768458SSam Leffler  */
1236810ad6fSSam Leffler int	crypto_support = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
12488768458SSam Leffler 
12588768458SSam Leffler SYSCTL_DECL(_net_inet_ipsec);
12688768458SSam Leffler 
12788768458SSam Leffler /* net.inet.ipsec */
12888768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
1293377c961STom Rhodes 	def_policy, CTLFLAG_RW,	&ip4_def_policy.policy,	0, "IPsec default policy.");
13088768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
1313377c961STom Rhodes 	CTLFLAG_RW, &ip4_esp_trans_deflev,	0, "Default ESP transport mode level");
13288768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
1333377c961STom Rhodes 	CTLFLAG_RW, &ip4_esp_net_deflev,	0, "Default ESP tunnel mode level.");
13488768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
1353377c961STom Rhodes 	CTLFLAG_RW, &ip4_ah_trans_deflev,	0, "AH transfer mode default level.");
13688768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
1373377c961STom Rhodes 	CTLFLAG_RW, &ip4_ah_net_deflev,	0, "AH tunnel mode default level.");
13888768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
1393377c961STom Rhodes 	ah_cleartos, CTLFLAG_RW,	&ah_cleartos,	0,
1403377c961STom Rhodes 	"If set clear type-of-service field when doing AH computation.");
14188768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
1423377c961STom Rhodes 	ah_offsetmask, CTLFLAG_RW,	&ip4_ah_offsetmask,	0,
1433377c961STom Rhodes 	"If not set clear offset field mask when doing AH computation.");
14488768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
1453377c961STom Rhodes 	dfbit, CTLFLAG_RW,	&ip4_ipsec_dfbit,	0, "Do not fragment bit on encap.");
14688768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
1473377c961STom Rhodes 	ecn, CTLFLAG_RW,	&ip4_ipsec_ecn,	0, "Explicit Congestion Notification handling.");
14888768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
1493377c961STom Rhodes 	debug, CTLFLAG_RW,	&ipsec_debug,	0, "Enable IPsec debugging output when set.");
15088768458SSam Leffler SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
1513377c961STom Rhodes 	crypto_support,	CTLFLAG_RW,	&crypto_support,0, "Crypto driver selection.");
15288768458SSam Leffler SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
1533377c961STom Rhodes 	ipsecstats,	CTLFLAG_RD,	&ipsec4stat, ipsecstat, "IPsec IPv4 statistics.");
15488768458SSam Leffler 
1556131838bSPawel Jakub Dawidek #ifdef REGRESSION
156dfa9422bSPawel Jakub Dawidek /*
157dfa9422bSPawel Jakub Dawidek  * When set to 1, IPsec will send packets with the same sequence number.
158dfa9422bSPawel Jakub Dawidek  * This allows to verify if the other side has proper replay attacks detection.
159dfa9422bSPawel Jakub Dawidek  */
160dfa9422bSPawel Jakub Dawidek int ipsec_replay = 0;
161dfa9422bSPawel Jakub Dawidek SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
162dfa9422bSPawel Jakub Dawidek     "Emulate replay attack");
163dfa9422bSPawel Jakub Dawidek /*
164dfa9422bSPawel Jakub Dawidek  * When set 1, IPsec will send packets with corrupted HMAC.
165dfa9422bSPawel Jakub Dawidek  * This allows to verify if the other side properly detects modified packets.
166dfa9422bSPawel Jakub Dawidek  */
167dfa9422bSPawel Jakub Dawidek int ipsec_integrity = 0;
168dfa9422bSPawel Jakub Dawidek SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
169dfa9422bSPawel Jakub Dawidek     &ipsec_integrity, 0, "Emulate man-in-the-middle attack");
1706131838bSPawel Jakub Dawidek #endif
171dfa9422bSPawel Jakub Dawidek 
17288768458SSam Leffler #ifdef INET6
1732cb64cb2SGeorge V. Neville-Neil struct ipsecstat ipsec6stat;
17488768458SSam Leffler int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
17588768458SSam Leffler int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
17688768458SSam Leffler int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
17788768458SSam Leffler int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
17888768458SSam Leffler int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
17988768458SSam Leffler 
18088768458SSam Leffler SYSCTL_DECL(_net_inet6_ipsec6);
18188768458SSam Leffler 
18288768458SSam Leffler /* net.inet6.ipsec6 */
18388768458SSam Leffler #ifdef COMPAT_KAME
18488768458SSam Leffler SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
1853377c961STom Rhodes 	0,0, compat_ipsecstats_sysctl, "S", "IPsec IPv6 statistics.");
18688768458SSam Leffler #endif /* COMPAT_KAME */
18788768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
1883377c961STom Rhodes 	def_policy, CTLFLAG_RW,	&ip4_def_policy.policy,	0, "IPsec default policy.");
18988768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
1903377c961STom Rhodes 	CTLFLAG_RW, &ip6_esp_trans_deflev,	0, "Default ESP transport mode level.");
19188768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
1923377c961STom Rhodes 	CTLFLAG_RW, &ip6_esp_net_deflev,	0, "Default ESP tunnel mode level.");
19388768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
1943377c961STom Rhodes 	CTLFLAG_RW, &ip6_ah_trans_deflev,	0, "AH transfer mode default level.");
19588768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
1963377c961STom Rhodes 	CTLFLAG_RW, &ip6_ah_net_deflev,	0, "AH tunnel mode default level.");
19788768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
1983377c961STom Rhodes 	ecn, CTLFLAG_RW,	&ip6_ipsec_ecn,	0,
1993377c961STom Rhodes 	"Explicit Congestion Notification handling.");
20088768458SSam Leffler SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
2013377c961STom Rhodes 	debug, CTLFLAG_RW,	&ipsec_debug,	0, "Enable IPsec debugging output when set.");
2022cb64cb2SGeorge V. Neville-Neil SYSCTL_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
2033377c961STom Rhodes 	ipsecstats, CTLFLAG_RD, &ipsec6stat, ipsecstat, "IPsec IPv6 statistics.");
20488768458SSam Leffler #endif /* INET6 */
20588768458SSam Leffler 
20688768458SSam Leffler static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
20788768458SSam Leffler #ifdef INET6
20888768458SSam Leffler static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct in6pcb *pcb));
20988768458SSam Leffler #endif
21088768458SSam Leffler static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
21188768458SSam Leffler static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
21288768458SSam Leffler static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
21388768458SSam Leffler #ifdef INET6
21488768458SSam Leffler static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
21588768458SSam Leffler static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
21688768458SSam Leffler #endif
21788768458SSam Leffler static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
21888768458SSam Leffler static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
21988768458SSam Leffler static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
220c26fe973SBjoern A. Zeeb 	int optname, caddr_t request, size_t len, struct ucred *cred));
22188768458SSam Leffler static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
22288768458SSam Leffler static void vshiftl __P((unsigned char *, int, int));
22388768458SSam Leffler static size_t ipsec_hdrsiz __P((struct secpolicy *));
22488768458SSam Leffler 
2256464079fSSam Leffler MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
2266464079fSSam Leffler 
22788768458SSam Leffler /*
22888768458SSam Leffler  * Return a held reference to the default SP.
22988768458SSam Leffler  */
23088768458SSam Leffler static struct secpolicy *
23188768458SSam Leffler key_allocsp_default(const char* where, int tag)
23288768458SSam Leffler {
23388768458SSam Leffler 	struct secpolicy *sp;
23488768458SSam Leffler 
23588768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
23688768458SSam Leffler 		printf("DP key_allocsp_default from %s:%u\n", where, tag));
23788768458SSam Leffler 
23888768458SSam Leffler 	sp = &ip4_def_policy;
23988768458SSam Leffler 	if (sp->policy != IPSEC_POLICY_DISCARD &&
24088768458SSam Leffler 	    sp->policy != IPSEC_POLICY_NONE) {
24188768458SSam Leffler 		ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
24288768458SSam Leffler 		    sp->policy, IPSEC_POLICY_NONE));
24388768458SSam Leffler 		sp->policy = IPSEC_POLICY_NONE;
24488768458SSam Leffler 	}
245422e4f5bSSam Leffler 	key_addref(sp);
24688768458SSam Leffler 
24788768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
24888768458SSam Leffler 		printf("DP key_allocsp_default returns SP:%p (%u)\n",
24988768458SSam Leffler 			sp, sp->refcnt));
25088768458SSam Leffler 	return sp;
25188768458SSam Leffler }
25288768458SSam Leffler #define	KEY_ALLOCSP_DEFAULT() \
25388768458SSam Leffler 	key_allocsp_default(__FILE__, __LINE__)
25488768458SSam Leffler 
25588768458SSam Leffler /*
25688768458SSam Leffler  * For OUTBOUND packet having a socket. Searching SPD for packet,
25788768458SSam Leffler  * and return a pointer to SP.
25888768458SSam Leffler  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
25988768458SSam Leffler  *		0	: bypass
26088768458SSam Leffler  *		EACCES	: discard packet.
26188768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
26288768458SSam Leffler  *		others	: error occured.
26388768458SSam Leffler  *	others:	a pointer to SP
26488768458SSam Leffler  *
26588768458SSam Leffler  * NOTE: IPv6 mapped adddress concern is implemented here.
26688768458SSam Leffler  */
26788768458SSam Leffler struct secpolicy *
26888768458SSam Leffler ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
26988768458SSam Leffler {
27088768458SSam Leffler 	struct secpolicy *sp;
27188768458SSam Leffler 
2729ffa9677SSam Leffler 	IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
2739ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
2749ffa9677SSam Leffler 		("invalid direction %u", dir));
27588768458SSam Leffler 
27688768458SSam Leffler 	sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
27788768458SSam Leffler 	if (sp == NULL)			/*XXX????*/
27888768458SSam Leffler 		sp = KEY_ALLOCSP_DEFAULT();
2799ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL, ("null SP"));
28088768458SSam Leffler 	return sp;
28188768458SSam Leffler }
28288768458SSam Leffler 
28388768458SSam Leffler /*
28488768458SSam Leffler  * For OUTBOUND packet having a socket. Searching SPD for packet,
28588768458SSam Leffler  * and return a pointer to SP.
28688768458SSam Leffler  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
28788768458SSam Leffler  *		0	: bypass
28888768458SSam Leffler  *		EACCES	: discard packet.
28988768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
29088768458SSam Leffler  *		others	: error occured.
29188768458SSam Leffler  *	others:	a pointer to SP
29288768458SSam Leffler  *
29388768458SSam Leffler  * NOTE: IPv6 mapped adddress concern is implemented here.
29488768458SSam Leffler  */
29588768458SSam Leffler struct secpolicy *
29688768458SSam Leffler ipsec_getpolicybysock(m, dir, inp, error)
29788768458SSam Leffler 	struct mbuf *m;
29888768458SSam Leffler 	u_int dir;
29988768458SSam Leffler 	struct inpcb *inp;
30088768458SSam Leffler 	int *error;
30188768458SSam Leffler {
30288768458SSam Leffler 	struct inpcbpolicy *pcbsp = NULL;
30388768458SSam Leffler 	struct secpolicy *currsp = NULL;	/* policy on socket */
30488768458SSam Leffler 	struct secpolicy *sp;
30588768458SSam Leffler 
3069ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
3079ffa9677SSam Leffler 	IPSEC_ASSERT(inp != NULL, ("null inpcb"));
3089ffa9677SSam Leffler 	IPSEC_ASSERT(error != NULL, ("null error"));
3099ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
3109ffa9677SSam Leffler 		("invalid direction %u", dir));
31188768458SSam Leffler 
31288768458SSam Leffler 	/* set spidx in pcb */
3139c3309d1SJonathan Lemon 	if (inp->inp_vflag & INP_IPV6PROTO) {
314595064e8SSam Leffler #ifdef INET6
31588768458SSam Leffler 		*error = ipsec6_setspidx_in6pcb(m, inp);
31688768458SSam Leffler 		pcbsp = inp->in6p_sp;
317595064e8SSam Leffler #else
318595064e8SSam Leffler 		*error = EINVAL;		/* should not happen */
319595064e8SSam Leffler #endif
3209c3309d1SJonathan Lemon 	} else {
3219c3309d1SJonathan Lemon 		*error = ipsec4_setspidx_inpcb(m, inp);
3229c3309d1SJonathan Lemon 		pcbsp = inp->inp_sp;
32388768458SSam Leffler 	}
32488768458SSam Leffler 	if (*error)
32588768458SSam Leffler 		return NULL;
32688768458SSam Leffler 
3279ffa9677SSam Leffler 	IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
32888768458SSam Leffler 	switch (dir) {
32988768458SSam Leffler 	case IPSEC_DIR_INBOUND:
33088768458SSam Leffler 		currsp = pcbsp->sp_in;
33188768458SSam Leffler 		break;
33288768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
33388768458SSam Leffler 		currsp = pcbsp->sp_out;
33488768458SSam Leffler 		break;
33588768458SSam Leffler 	}
3369ffa9677SSam Leffler 	IPSEC_ASSERT(currsp != NULL, ("null currsp"));
33788768458SSam Leffler 
33888768458SSam Leffler 	if (pcbsp->priv) {			/* when privilieged socket */
33988768458SSam Leffler 		switch (currsp->policy) {
34088768458SSam Leffler 		case IPSEC_POLICY_BYPASS:
34188768458SSam Leffler 		case IPSEC_POLICY_IPSEC:
342422e4f5bSSam Leffler 			key_addref(currsp);
34388768458SSam Leffler 			sp = currsp;
34488768458SSam Leffler 			break;
34588768458SSam Leffler 
34688768458SSam Leffler 		case IPSEC_POLICY_ENTRUST:
34788768458SSam Leffler 			/* look for a policy in SPD */
34888768458SSam Leffler 			sp = KEY_ALLOCSP(&currsp->spidx, dir);
34988768458SSam Leffler 			if (sp == NULL)		/* no SP found */
35088768458SSam Leffler 				sp = KEY_ALLOCSP_DEFAULT();
35188768458SSam Leffler 			break;
35288768458SSam Leffler 
35388768458SSam Leffler 		default:
3549ffa9677SSam Leffler 			ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
3559ffa9677SSam Leffler 				__func__, currsp->policy));
35688768458SSam Leffler 			*error = EINVAL;
35788768458SSam Leffler 			return NULL;
35888768458SSam Leffler 		}
35988768458SSam Leffler 	} else {				/* unpriv, SPD has policy */
36088768458SSam Leffler 		sp = KEY_ALLOCSP(&currsp->spidx, dir);
36188768458SSam Leffler 		if (sp == NULL) {		/* no SP found */
36288768458SSam Leffler 			switch (currsp->policy) {
36388768458SSam Leffler 			case IPSEC_POLICY_BYPASS:
3649ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: Illegal policy for "
3659ffa9677SSam Leffler 					"non-priviliged defined %d\n",
3669ffa9677SSam Leffler 					__func__, currsp->policy));
36788768458SSam Leffler 				*error = EINVAL;
36888768458SSam Leffler 				return NULL;
36988768458SSam Leffler 
37088768458SSam Leffler 			case IPSEC_POLICY_ENTRUST:
37188768458SSam Leffler 				sp = KEY_ALLOCSP_DEFAULT();
37288768458SSam Leffler 				break;
37388768458SSam Leffler 
37488768458SSam Leffler 			case IPSEC_POLICY_IPSEC:
375422e4f5bSSam Leffler 				key_addref(currsp);
37688768458SSam Leffler 				sp = currsp;
37788768458SSam Leffler 				break;
37888768458SSam Leffler 
37988768458SSam Leffler 			default:
3809ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: Invalid policy for "
3819ffa9677SSam Leffler 					"PCB %d\n", __func__, currsp->policy));
38288768458SSam Leffler 				*error = EINVAL;
38388768458SSam Leffler 				return NULL;
38488768458SSam Leffler 			}
38588768458SSam Leffler 		}
38688768458SSam Leffler 	}
3879ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL,
3889ffa9677SSam Leffler 		("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
38988768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3909ffa9677SSam Leffler 		printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
3919ffa9677SSam Leffler 			__func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
39288768458SSam Leffler 	return sp;
39388768458SSam Leffler }
39488768458SSam Leffler 
39588768458SSam Leffler /*
39688768458SSam Leffler  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
39788768458SSam Leffler  * and return a pointer to SP.
39888768458SSam Leffler  * OUT:	positive: a pointer to the entry for security policy leaf matched.
39988768458SSam Leffler  *	NULL:	no apropreate SP found, the following value is set to error.
40088768458SSam Leffler  *		0	: bypass
40188768458SSam Leffler  *		EACCES	: discard packet.
40288768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
40388768458SSam Leffler  *		others	: error occured.
40488768458SSam Leffler  */
40588768458SSam Leffler struct secpolicy *
40688768458SSam Leffler ipsec_getpolicybyaddr(m, dir, flag, error)
40788768458SSam Leffler 	struct mbuf *m;
40888768458SSam Leffler 	u_int dir;
40988768458SSam Leffler 	int flag;
41088768458SSam Leffler 	int *error;
41188768458SSam Leffler {
41288768458SSam Leffler 	struct secpolicyindex spidx;
41388768458SSam Leffler 	struct secpolicy *sp;
41488768458SSam Leffler 
4159ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
4169ffa9677SSam Leffler 	IPSEC_ASSERT(error != NULL, ("null error"));
4179ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
4189ffa9677SSam Leffler 		("invalid direction %u", dir));
41988768458SSam Leffler 
42088768458SSam Leffler 	sp = NULL;
42188768458SSam Leffler 	if (key_havesp(dir)) {
4229d5abbddSJens Schweikhardt 		/* Make an index to look for a policy. */
42388768458SSam Leffler 		*error = ipsec_setspidx(m, &spidx,
42488768458SSam Leffler 					(flag & IP_FORWARDING) ? 0 : 1);
42588768458SSam Leffler 		if (*error != 0) {
4269ffa9677SSam Leffler 			DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
4279ffa9677SSam Leffler 				__func__, dir, flag));
42888768458SSam Leffler 			return NULL;
42988768458SSam Leffler 		}
43088768458SSam Leffler 		spidx.dir = dir;
43188768458SSam Leffler 
43288768458SSam Leffler 		sp = KEY_ALLOCSP(&spidx, dir);
43388768458SSam Leffler 	}
43488768458SSam Leffler 	if (sp == NULL)			/* no SP found, use system default */
43588768458SSam Leffler 		sp = KEY_ALLOCSP_DEFAULT();
4369ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL, ("null SP"));
43788768458SSam Leffler 	return sp;
43888768458SSam Leffler }
43988768458SSam Leffler 
44088768458SSam Leffler struct secpolicy *
44188768458SSam Leffler ipsec4_checkpolicy(m, dir, flag, error, inp)
44288768458SSam Leffler 	struct mbuf *m;
44388768458SSam Leffler 	u_int dir, flag;
44488768458SSam Leffler 	int *error;
44588768458SSam Leffler 	struct inpcb *inp;
44688768458SSam Leffler {
44788768458SSam Leffler 	struct secpolicy *sp;
44888768458SSam Leffler 
44988768458SSam Leffler 	*error = 0;
45088768458SSam Leffler 	if (inp == NULL)
45188768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
45288768458SSam Leffler 	else
45388768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, inp, error);
45488768458SSam Leffler 	if (sp == NULL) {
4559ffa9677SSam Leffler 		IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
4562cb64cb2SGeorge V. Neville-Neil 		ipsec4stat.ips_out_inval++;
45788768458SSam Leffler 		return NULL;
45888768458SSam Leffler 	}
4599ffa9677SSam Leffler 	IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
46088768458SSam Leffler 	switch (sp->policy) {
46188768458SSam Leffler 	case IPSEC_POLICY_ENTRUST:
46288768458SSam Leffler 	default:
4639ffa9677SSam Leffler 		printf("%s: invalid policy %u\n", __func__, sp->policy);
46488768458SSam Leffler 		/* fall thru... */
46588768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
4662cb64cb2SGeorge V. Neville-Neil 		ipsec4stat.ips_out_polvio++;
46788768458SSam Leffler 		*error = -EINVAL;	/* packet is discarded by caller */
46888768458SSam Leffler 		break;
46988768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
47088768458SSam Leffler 	case IPSEC_POLICY_NONE:
47188768458SSam Leffler 		KEY_FREESP(&sp);
47288768458SSam Leffler 		sp = NULL;		/* NB: force NULL result */
47388768458SSam Leffler 		break;
47488768458SSam Leffler 	case IPSEC_POLICY_IPSEC:
47588768458SSam Leffler 		if (sp->req == NULL)	/* acquire an SA */
47688768458SSam Leffler 			*error = key_spdacquire(sp);
47788768458SSam Leffler 		break;
47888768458SSam Leffler 	}
47988768458SSam Leffler 	if (*error != 0) {
48088768458SSam Leffler 		KEY_FREESP(&sp);
48188768458SSam Leffler 		sp = NULL;
48288768458SSam Leffler 	}
48388768458SSam Leffler 	return sp;
48488768458SSam Leffler }
48588768458SSam Leffler 
48688768458SSam Leffler static int
48788768458SSam Leffler ipsec4_setspidx_inpcb(m, pcb)
48888768458SSam Leffler 	struct mbuf *m;
48988768458SSam Leffler 	struct inpcb *pcb;
49088768458SSam Leffler {
49188768458SSam Leffler 	int error;
49288768458SSam Leffler 
4939ffa9677SSam Leffler 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
4949ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
4959ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
4969ffa9677SSam Leffler 		("null sp_in || sp_out"));
49788768458SSam Leffler 
49888768458SSam Leffler 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
49988768458SSam Leffler 	if (error == 0) {
50088768458SSam Leffler 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
50188768458SSam Leffler 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
50288768458SSam Leffler 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
50388768458SSam Leffler 	} else {
50488768458SSam Leffler 		bzero(&pcb->inp_sp->sp_in->spidx,
50588768458SSam Leffler 			sizeof (pcb->inp_sp->sp_in->spidx));
50688768458SSam Leffler 		bzero(&pcb->inp_sp->sp_out->spidx,
50788768458SSam Leffler 			sizeof (pcb->inp_sp->sp_in->spidx));
50888768458SSam Leffler 	}
50988768458SSam Leffler 	return error;
51088768458SSam Leffler }
51188768458SSam Leffler 
51288768458SSam Leffler #ifdef INET6
51388768458SSam Leffler static int
51488768458SSam Leffler ipsec6_setspidx_in6pcb(m, pcb)
51588768458SSam Leffler 	struct mbuf *m;
51688768458SSam Leffler 	struct in6pcb *pcb;
51788768458SSam Leffler {
51888768458SSam Leffler 	struct secpolicyindex *spidx;
51988768458SSam Leffler 	int error;
52088768458SSam Leffler 
5219ffa9677SSam Leffler 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
5229ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->in6p_sp != NULL, ("null inp_sp"));
5239ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
5249ffa9677SSam Leffler 		("null sp_in || sp_out"));
52588768458SSam Leffler 
52688768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
52788768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
52888768458SSam Leffler 
52988768458SSam Leffler 	spidx = &pcb->in6p_sp->sp_in->spidx;
53088768458SSam Leffler 	error = ipsec_setspidx(m, spidx, 1);
53188768458SSam Leffler 	if (error)
53288768458SSam Leffler 		goto bad;
53388768458SSam Leffler 	spidx->dir = IPSEC_DIR_INBOUND;
53488768458SSam Leffler 
53588768458SSam Leffler 	spidx = &pcb->in6p_sp->sp_out->spidx;
53688768458SSam Leffler 	error = ipsec_setspidx(m, spidx, 1);
53788768458SSam Leffler 	if (error)
53888768458SSam Leffler 		goto bad;
53988768458SSam Leffler 	spidx->dir = IPSEC_DIR_OUTBOUND;
54088768458SSam Leffler 
54188768458SSam Leffler 	return 0;
54288768458SSam Leffler 
54388768458SSam Leffler bad:
54488768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
54588768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
54688768458SSam Leffler 	return error;
54788768458SSam Leffler }
54888768458SSam Leffler #endif
54988768458SSam Leffler 
55088768458SSam Leffler /*
55188768458SSam Leffler  * configure security policy index (src/dst/proto/sport/dport)
55288768458SSam Leffler  * by looking at the content of mbuf.
55388768458SSam Leffler  * the caller is responsible for error recovery (like clearing up spidx).
55488768458SSam Leffler  */
55588768458SSam Leffler static int
55688768458SSam Leffler ipsec_setspidx(m, spidx, needport)
55788768458SSam Leffler 	struct mbuf *m;
55888768458SSam Leffler 	struct secpolicyindex *spidx;
55988768458SSam Leffler 	int needport;
56088768458SSam Leffler {
56188768458SSam Leffler 	struct ip *ip = NULL;
56288768458SSam Leffler 	struct ip ipbuf;
56388768458SSam Leffler 	u_int v;
56488768458SSam Leffler 	struct mbuf *n;
56588768458SSam Leffler 	int len;
56688768458SSam Leffler 	int error;
56788768458SSam Leffler 
5689ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
56988768458SSam Leffler 
57088768458SSam Leffler 	/*
57188768458SSam Leffler 	 * validate m->m_pkthdr.len.  we see incorrect length if we
57288768458SSam Leffler 	 * mistakenly call this function with inconsistent mbuf chain
57388768458SSam Leffler 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
57488768458SSam Leffler 	 */
57588768458SSam Leffler 	len = 0;
57688768458SSam Leffler 	for (n = m; n; n = n->m_next)
57788768458SSam Leffler 		len += n->m_len;
57888768458SSam Leffler 	if (m->m_pkthdr.len != len) {
57988768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
5809ffa9677SSam Leffler 			printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
5819ffa9677SSam Leffler 				__func__, len, m->m_pkthdr.len));
58288768458SSam Leffler 		return EINVAL;
58388768458SSam Leffler 	}
58488768458SSam Leffler 
58588768458SSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ip)) {
58688768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
5879ffa9677SSam Leffler 			printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
5889ffa9677SSam Leffler 			    __func__, m->m_pkthdr.len));
58988768458SSam Leffler 		return EINVAL;
59088768458SSam Leffler 	}
59188768458SSam Leffler 
59288768458SSam Leffler 	if (m->m_len >= sizeof(*ip))
59388768458SSam Leffler 		ip = mtod(m, struct ip *);
59488768458SSam Leffler 	else {
59588768458SSam Leffler 		m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
59688768458SSam Leffler 		ip = &ipbuf;
59788768458SSam Leffler 	}
59888768458SSam Leffler #ifdef _IP_VHL
59988768458SSam Leffler 	v = _IP_VHL_V(ip->ip_vhl);
60088768458SSam Leffler #else
60188768458SSam Leffler 	v = ip->ip_v;
60288768458SSam Leffler #endif
60388768458SSam Leffler 	switch (v) {
60488768458SSam Leffler 	case 4:
60588768458SSam Leffler 		error = ipsec4_setspidx_ipaddr(m, spidx);
60688768458SSam Leffler 		if (error)
60788768458SSam Leffler 			return error;
60888768458SSam Leffler 		ipsec4_get_ulp(m, spidx, needport);
60988768458SSam Leffler 		return 0;
61088768458SSam Leffler #ifdef INET6
61188768458SSam Leffler 	case 6:
61288768458SSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
61388768458SSam Leffler 			KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6149ffa9677SSam Leffler 				printf("%s: pkthdr len(%d) too small (v6), "
6159ffa9677SSam Leffler 				"ignored\n", __func__, m->m_pkthdr.len));
61688768458SSam Leffler 			return EINVAL;
61788768458SSam Leffler 		}
61888768458SSam Leffler 		error = ipsec6_setspidx_ipaddr(m, spidx);
61988768458SSam Leffler 		if (error)
62088768458SSam Leffler 			return error;
62188768458SSam Leffler 		ipsec6_get_ulp(m, spidx, needport);
62288768458SSam Leffler 		return 0;
62388768458SSam Leffler #endif
62488768458SSam Leffler 	default:
62588768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6269ffa9677SSam Leffler 			printf("%s: " "unknown IP version %u, ignored.\n",
6279ffa9677SSam Leffler 				__func__, v));
62888768458SSam Leffler 		return EINVAL;
62988768458SSam Leffler 	}
63088768458SSam Leffler }
63188768458SSam Leffler 
63288768458SSam Leffler static void
63388768458SSam Leffler ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
63488768458SSam Leffler {
63588768458SSam Leffler 	u_int8_t nxt;
63688768458SSam Leffler 	int off;
63788768458SSam Leffler 
63888768458SSam Leffler 	/* sanity check */
6399ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
6409ffa9677SSam Leffler 	IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
64188768458SSam Leffler 
64288768458SSam Leffler 	/* NB: ip_input() flips it into host endian XXX need more checking */
64388768458SSam Leffler 	if (m->m_len < sizeof (struct ip)) {
64488768458SSam Leffler 		struct ip *ip = mtod(m, struct ip *);
64588768458SSam Leffler 		if (ip->ip_off & (IP_MF | IP_OFFMASK))
64688768458SSam Leffler 			goto done;
64788768458SSam Leffler #ifdef _IP_VHL
64888768458SSam Leffler 		off = _IP_VHL_HL(ip->ip_vhl) << 2;
64988768458SSam Leffler #else
65088768458SSam Leffler 		off = ip->ip_hl << 2;
65188768458SSam Leffler #endif
65288768458SSam Leffler 		nxt = ip->ip_p;
65388768458SSam Leffler 	} else {
65488768458SSam Leffler 		struct ip ih;
65588768458SSam Leffler 
65688768458SSam Leffler 		m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
65788768458SSam Leffler 		if (ih.ip_off & (IP_MF | IP_OFFMASK))
65888768458SSam Leffler 			goto done;
65988768458SSam Leffler #ifdef _IP_VHL
66088768458SSam Leffler 		off = _IP_VHL_HL(ih.ip_vhl) << 2;
66188768458SSam Leffler #else
66288768458SSam Leffler 		off = ih.ip_hl << 2;
66388768458SSam Leffler #endif
66488768458SSam Leffler 		nxt = ih.ip_p;
66588768458SSam Leffler 	}
66688768458SSam Leffler 
66788768458SSam Leffler 	while (off < m->m_pkthdr.len) {
66888768458SSam Leffler 		struct ip6_ext ip6e;
66988768458SSam Leffler 		struct tcphdr th;
67088768458SSam Leffler 		struct udphdr uh;
67188768458SSam Leffler 
67288768458SSam Leffler 		switch (nxt) {
67388768458SSam Leffler 		case IPPROTO_TCP:
67488768458SSam Leffler 			spidx->ul_proto = nxt;
67588768458SSam Leffler 			if (!needport)
67688768458SSam Leffler 				goto done_proto;
67788768458SSam Leffler 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
67888768458SSam Leffler 				goto done;
67988768458SSam Leffler 			m_copydata(m, off, sizeof (th), (caddr_t) &th);
68088768458SSam Leffler 			spidx->src.sin.sin_port = th.th_sport;
68188768458SSam Leffler 			spidx->dst.sin.sin_port = th.th_dport;
68288768458SSam Leffler 			return;
68388768458SSam Leffler 		case IPPROTO_UDP:
68488768458SSam Leffler 			spidx->ul_proto = nxt;
68588768458SSam Leffler 			if (!needport)
68688768458SSam Leffler 				goto done_proto;
68788768458SSam Leffler 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
68888768458SSam Leffler 				goto done;
68988768458SSam Leffler 			m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
69088768458SSam Leffler 			spidx->src.sin.sin_port = uh.uh_sport;
69188768458SSam Leffler 			spidx->dst.sin.sin_port = uh.uh_dport;
69288768458SSam Leffler 			return;
69388768458SSam Leffler 		case IPPROTO_AH:
694afa3570dSSam Leffler 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
69588768458SSam Leffler 				goto done;
69688768458SSam Leffler 			/* XXX sigh, this works but is totally bogus */
69788768458SSam Leffler 			m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
69888768458SSam Leffler 			off += (ip6e.ip6e_len + 2) << 2;
69988768458SSam Leffler 			nxt = ip6e.ip6e_nxt;
70088768458SSam Leffler 			break;
70188768458SSam Leffler 		case IPPROTO_ICMP:
70288768458SSam Leffler 		default:
70388768458SSam Leffler 			/* XXX intermediate headers??? */
70488768458SSam Leffler 			spidx->ul_proto = nxt;
70588768458SSam Leffler 			goto done_proto;
70688768458SSam Leffler 		}
70788768458SSam Leffler 	}
70888768458SSam Leffler done:
70988768458SSam Leffler 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
71088768458SSam Leffler done_proto:
71188768458SSam Leffler 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
71288768458SSam Leffler 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
71388768458SSam Leffler }
71488768458SSam Leffler 
71588768458SSam Leffler /* assumes that m is sane */
71688768458SSam Leffler static int
71788768458SSam Leffler ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
71888768458SSam Leffler {
71988768458SSam Leffler 	static const struct sockaddr_in template = {
72088768458SSam Leffler 		sizeof (struct sockaddr_in),
72188768458SSam Leffler 		AF_INET,
72288768458SSam Leffler 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
72388768458SSam Leffler 	};
72488768458SSam Leffler 
72588768458SSam Leffler 	spidx->src.sin = template;
72688768458SSam Leffler 	spidx->dst.sin = template;
72788768458SSam Leffler 
72888768458SSam Leffler 	if (m->m_len < sizeof (struct ip)) {
72988768458SSam Leffler 		m_copydata(m, offsetof(struct ip, ip_src),
73088768458SSam Leffler 			   sizeof (struct  in_addr),
73188768458SSam Leffler 			   (caddr_t) &spidx->src.sin.sin_addr);
73288768458SSam Leffler 		m_copydata(m, offsetof(struct ip, ip_dst),
73388768458SSam Leffler 			   sizeof (struct  in_addr),
73488768458SSam Leffler 			   (caddr_t) &spidx->dst.sin.sin_addr);
73588768458SSam Leffler 	} else {
73688768458SSam Leffler 		struct ip *ip = mtod(m, struct ip *);
73788768458SSam Leffler 		spidx->src.sin.sin_addr = ip->ip_src;
73888768458SSam Leffler 		spidx->dst.sin.sin_addr = ip->ip_dst;
73988768458SSam Leffler 	}
74088768458SSam Leffler 
74188768458SSam Leffler 	spidx->prefs = sizeof(struct in_addr) << 3;
74288768458SSam Leffler 	spidx->prefd = sizeof(struct in_addr) << 3;
74388768458SSam Leffler 
74488768458SSam Leffler 	return 0;
74588768458SSam Leffler }
74688768458SSam Leffler 
74788768458SSam Leffler #ifdef INET6
74888768458SSam Leffler static void
74988768458SSam Leffler ipsec6_get_ulp(m, spidx, needport)
75088768458SSam Leffler 	struct mbuf *m;
75188768458SSam Leffler 	struct secpolicyindex *spidx;
75288768458SSam Leffler 	int needport;
75388768458SSam Leffler {
75488768458SSam Leffler 	int off, nxt;
75588768458SSam Leffler 	struct tcphdr th;
75688768458SSam Leffler 	struct udphdr uh;
7570e3c2be4SBjoern A. Zeeb 	struct icmp6_hdr ih;
75888768458SSam Leffler 
75988768458SSam Leffler 	/* sanity check */
76088768458SSam Leffler 	if (m == NULL)
7619ffa9677SSam Leffler 		panic("%s: NULL pointer was passed.\n", __func__);
76288768458SSam Leffler 
76388768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
7649ffa9677SSam Leffler 		printf("%s:\n", __func__); kdebug_mbuf(m));
76588768458SSam Leffler 
76688768458SSam Leffler 	/* set default */
76788768458SSam Leffler 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
76888768458SSam Leffler 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
76988768458SSam Leffler 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
77088768458SSam Leffler 
77188768458SSam Leffler 	nxt = -1;
77288768458SSam Leffler 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
77388768458SSam Leffler 	if (off < 0 || m->m_pkthdr.len < off)
77488768458SSam Leffler 		return;
77588768458SSam Leffler 
77688768458SSam Leffler 	switch (nxt) {
77788768458SSam Leffler 	case IPPROTO_TCP:
77888768458SSam Leffler 		spidx->ul_proto = nxt;
77988768458SSam Leffler 		if (!needport)
78088768458SSam Leffler 			break;
78188768458SSam Leffler 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
78288768458SSam Leffler 			break;
78388768458SSam Leffler 		m_copydata(m, off, sizeof(th), (caddr_t)&th);
78488768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
78588768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
78688768458SSam Leffler 		break;
78788768458SSam Leffler 	case IPPROTO_UDP:
78888768458SSam Leffler 		spidx->ul_proto = nxt;
78988768458SSam Leffler 		if (!needport)
79088768458SSam Leffler 			break;
79188768458SSam Leffler 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
79288768458SSam Leffler 			break;
79388768458SSam Leffler 		m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
79488768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
79588768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
79688768458SSam Leffler 		break;
79788768458SSam Leffler 	case IPPROTO_ICMPV6:
7980e3c2be4SBjoern A. Zeeb 		spidx->ul_proto = nxt;
7990e3c2be4SBjoern A. Zeeb 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
8000e3c2be4SBjoern A. Zeeb 			break;
8010e3c2be4SBjoern A. Zeeb 		m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
8020e3c2be4SBjoern A. Zeeb 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
8030e3c2be4SBjoern A. Zeeb 		    htons((uint16_t)ih.icmp6_type);
8040e3c2be4SBjoern A. Zeeb 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
8050e3c2be4SBjoern A. Zeeb 		    htons((uint16_t)ih.icmp6_code);
8060e3c2be4SBjoern A. Zeeb 		break;
80788768458SSam Leffler 	default:
80888768458SSam Leffler 		/* XXX intermediate headers??? */
80988768458SSam Leffler 		spidx->ul_proto = nxt;
81088768458SSam Leffler 		break;
81188768458SSam Leffler 	}
81288768458SSam Leffler }
81388768458SSam Leffler 
81488768458SSam Leffler /* assumes that m is sane */
81588768458SSam Leffler static int
81688768458SSam Leffler ipsec6_setspidx_ipaddr(m, spidx)
81788768458SSam Leffler 	struct mbuf *m;
81888768458SSam Leffler 	struct secpolicyindex *spidx;
81988768458SSam Leffler {
82088768458SSam Leffler 	struct ip6_hdr *ip6 = NULL;
82188768458SSam Leffler 	struct ip6_hdr ip6buf;
82288768458SSam Leffler 	struct sockaddr_in6 *sin6;
82388768458SSam Leffler 
82488768458SSam Leffler 	if (m->m_len >= sizeof(*ip6))
82588768458SSam Leffler 		ip6 = mtod(m, struct ip6_hdr *);
82688768458SSam Leffler 	else {
82788768458SSam Leffler 		m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
82888768458SSam Leffler 		ip6 = &ip6buf;
82988768458SSam Leffler 	}
83088768458SSam Leffler 
83188768458SSam Leffler 	sin6 = (struct sockaddr_in6 *)&spidx->src;
83288768458SSam Leffler 	bzero(sin6, sizeof(*sin6));
83388768458SSam Leffler 	sin6->sin6_family = AF_INET6;
83488768458SSam Leffler 	sin6->sin6_len = sizeof(struct sockaddr_in6);
83588768458SSam Leffler 	bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
83688768458SSam Leffler 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
83788768458SSam Leffler 		sin6->sin6_addr.s6_addr16[1] = 0;
83888768458SSam Leffler 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
83988768458SSam Leffler 	}
84088768458SSam Leffler 	spidx->prefs = sizeof(struct in6_addr) << 3;
84188768458SSam Leffler 
84288768458SSam Leffler 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
84388768458SSam Leffler 	bzero(sin6, sizeof(*sin6));
84488768458SSam Leffler 	sin6->sin6_family = AF_INET6;
84588768458SSam Leffler 	sin6->sin6_len = sizeof(struct sockaddr_in6);
84688768458SSam Leffler 	bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
84788768458SSam Leffler 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
84888768458SSam Leffler 		sin6->sin6_addr.s6_addr16[1] = 0;
84988768458SSam Leffler 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
85088768458SSam Leffler 	}
85188768458SSam Leffler 	spidx->prefd = sizeof(struct in6_addr) << 3;
85288768458SSam Leffler 
85388768458SSam Leffler 	return 0;
85488768458SSam Leffler }
85588768458SSam Leffler #endif
85688768458SSam Leffler 
85788768458SSam Leffler static void
85888768458SSam Leffler ipsec_delpcbpolicy(p)
85988768458SSam Leffler 	struct inpcbpolicy *p;
86088768458SSam Leffler {
8616464079fSSam Leffler 	free(p, M_IPSEC_INPCB);
86288768458SSam Leffler }
86388768458SSam Leffler 
86488768458SSam Leffler /* initialize policy in PCB */
86588768458SSam Leffler int
86688768458SSam Leffler ipsec_init_policy(so, pcb_sp)
86788768458SSam Leffler 	struct socket *so;
86888768458SSam Leffler 	struct inpcbpolicy **pcb_sp;
86988768458SSam Leffler {
87088768458SSam Leffler 	struct inpcbpolicy *new;
87188768458SSam Leffler 
87288768458SSam Leffler 	/* sanity check. */
87388768458SSam Leffler 	if (so == NULL || pcb_sp == NULL)
8749ffa9677SSam Leffler 		panic("%s: NULL pointer was passed.\n", __func__);
87588768458SSam Leffler 
87688768458SSam Leffler 	new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
8776464079fSSam Leffler 					    M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
87888768458SSam Leffler 	if (new == NULL) {
8799ffa9677SSam Leffler 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
88088768458SSam Leffler 		return ENOBUFS;
88188768458SSam Leffler 	}
88288768458SSam Leffler 
8839ffa9677SSam Leffler 	new->priv = IPSEC_IS_PRIVILEGED_SO(so);
88488768458SSam Leffler 
88588768458SSam Leffler 	if ((new->sp_in = KEY_NEWSP()) == NULL) {
88688768458SSam Leffler 		ipsec_delpcbpolicy(new);
88788768458SSam Leffler 		return ENOBUFS;
88888768458SSam Leffler 	}
88988768458SSam Leffler 	new->sp_in->state = IPSEC_SPSTATE_ALIVE;
89088768458SSam Leffler 	new->sp_in->policy = IPSEC_POLICY_ENTRUST;
89188768458SSam Leffler 
89288768458SSam Leffler 	if ((new->sp_out = KEY_NEWSP()) == NULL) {
89388768458SSam Leffler 		KEY_FREESP(&new->sp_in);
89488768458SSam Leffler 		ipsec_delpcbpolicy(new);
89588768458SSam Leffler 		return ENOBUFS;
89688768458SSam Leffler 	}
89788768458SSam Leffler 	new->sp_out->state = IPSEC_SPSTATE_ALIVE;
89888768458SSam Leffler 	new->sp_out->policy = IPSEC_POLICY_ENTRUST;
89988768458SSam Leffler 
90088768458SSam Leffler 	*pcb_sp = new;
90188768458SSam Leffler 
90288768458SSam Leffler 	return 0;
90388768458SSam Leffler }
90488768458SSam Leffler 
90588768458SSam Leffler /* copy old ipsec policy into new */
90688768458SSam Leffler int
90788768458SSam Leffler ipsec_copy_policy(old, new)
90888768458SSam Leffler 	struct inpcbpolicy *old, *new;
90988768458SSam Leffler {
91088768458SSam Leffler 	struct secpolicy *sp;
91188768458SSam Leffler 
91288768458SSam Leffler 	sp = ipsec_deepcopy_policy(old->sp_in);
91388768458SSam Leffler 	if (sp) {
91488768458SSam Leffler 		KEY_FREESP(&new->sp_in);
91588768458SSam Leffler 		new->sp_in = sp;
91688768458SSam Leffler 	} else
91788768458SSam Leffler 		return ENOBUFS;
91888768458SSam Leffler 
91988768458SSam Leffler 	sp = ipsec_deepcopy_policy(old->sp_out);
92088768458SSam Leffler 	if (sp) {
92188768458SSam Leffler 		KEY_FREESP(&new->sp_out);
92288768458SSam Leffler 		new->sp_out = sp;
92388768458SSam Leffler 	} else
92488768458SSam Leffler 		return ENOBUFS;
92588768458SSam Leffler 
92688768458SSam Leffler 	new->priv = old->priv;
92788768458SSam Leffler 
92888768458SSam Leffler 	return 0;
92988768458SSam Leffler }
93088768458SSam Leffler 
9316464079fSSam Leffler struct ipsecrequest *
9326464079fSSam Leffler ipsec_newisr(void)
9336464079fSSam Leffler {
9346464079fSSam Leffler 	struct ipsecrequest *p;
9356464079fSSam Leffler 
9366464079fSSam Leffler 	p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
9376464079fSSam Leffler 	if (p != NULL)
9389ffa9677SSam Leffler 		IPSECREQUEST_LOCK_INIT(p);
9396464079fSSam Leffler 	return p;
9406464079fSSam Leffler }
9416464079fSSam Leffler 
9426464079fSSam Leffler void
9436464079fSSam Leffler ipsec_delisr(struct ipsecrequest *p)
9446464079fSSam Leffler {
9459ffa9677SSam Leffler 	IPSECREQUEST_LOCK_DESTROY(p);
9466464079fSSam Leffler 	free(p, M_IPSEC_SR);
9476464079fSSam Leffler }
9486464079fSSam Leffler 
94988768458SSam Leffler /* deep-copy a policy in PCB */
95088768458SSam Leffler static struct secpolicy *
95188768458SSam Leffler ipsec_deepcopy_policy(src)
95288768458SSam Leffler 	struct secpolicy *src;
95388768458SSam Leffler {
95488768458SSam Leffler 	struct ipsecrequest *newchain = NULL;
95588768458SSam Leffler 	struct ipsecrequest *p;
95688768458SSam Leffler 	struct ipsecrequest **q;
95788768458SSam Leffler 	struct ipsecrequest *r;
95888768458SSam Leffler 	struct secpolicy *dst;
95988768458SSam Leffler 
96088768458SSam Leffler 	if (src == NULL)
96188768458SSam Leffler 		return NULL;
96288768458SSam Leffler 	dst = KEY_NEWSP();
96388768458SSam Leffler 	if (dst == NULL)
96488768458SSam Leffler 		return NULL;
96588768458SSam Leffler 
96688768458SSam Leffler 	/*
96788768458SSam Leffler 	 * deep-copy IPsec request chain.  This is required since struct
96888768458SSam Leffler 	 * ipsecrequest is not reference counted.
96988768458SSam Leffler 	 */
97088768458SSam Leffler 	q = &newchain;
97188768458SSam Leffler 	for (p = src->req; p; p = p->next) {
9726464079fSSam Leffler 		*q = ipsec_newisr();
97388768458SSam Leffler 		if (*q == NULL)
97488768458SSam Leffler 			goto fail;
97588768458SSam Leffler 		(*q)->saidx.proto = p->saidx.proto;
97688768458SSam Leffler 		(*q)->saidx.mode = p->saidx.mode;
97788768458SSam Leffler 		(*q)->level = p->level;
97888768458SSam Leffler 		(*q)->saidx.reqid = p->saidx.reqid;
97988768458SSam Leffler 
98088768458SSam Leffler 		bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
98188768458SSam Leffler 		bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
98288768458SSam Leffler 
98388768458SSam Leffler 		(*q)->sp = dst;
98488768458SSam Leffler 
98588768458SSam Leffler 		q = &((*q)->next);
98688768458SSam Leffler 	}
98788768458SSam Leffler 
98888768458SSam Leffler 	dst->req = newchain;
98988768458SSam Leffler 	dst->state = src->state;
99088768458SSam Leffler 	dst->policy = src->policy;
99188768458SSam Leffler 	/* do not touch the refcnt fields */
99288768458SSam Leffler 
99388768458SSam Leffler 	return dst;
99488768458SSam Leffler 
99588768458SSam Leffler fail:
99688768458SSam Leffler 	for (p = newchain; p; p = r) {
99788768458SSam Leffler 		r = p->next;
9986464079fSSam Leffler 		ipsec_delisr(p);
99988768458SSam Leffler 		p = NULL;
100088768458SSam Leffler 	}
100188768458SSam Leffler 	return NULL;
100288768458SSam Leffler }
100388768458SSam Leffler 
100488768458SSam Leffler /* set policy and ipsec request if present. */
100588768458SSam Leffler static int
1006c26fe973SBjoern A. Zeeb ipsec_set_policy(pcb_sp, optname, request, len, cred)
100788768458SSam Leffler 	struct secpolicy **pcb_sp;
100888768458SSam Leffler 	int optname;
100988768458SSam Leffler 	caddr_t request;
101088768458SSam Leffler 	size_t len;
1011c26fe973SBjoern A. Zeeb 	struct ucred *cred;
101288768458SSam Leffler {
101388768458SSam Leffler 	struct sadb_x_policy *xpl;
101488768458SSam Leffler 	struct secpolicy *newsp = NULL;
101588768458SSam Leffler 	int error;
101688768458SSam Leffler 
101788768458SSam Leffler 	/* sanity check. */
101888768458SSam Leffler 	if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
101988768458SSam Leffler 		return EINVAL;
102088768458SSam Leffler 	if (len < sizeof(*xpl))
102188768458SSam Leffler 		return EINVAL;
102288768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
102388768458SSam Leffler 
102488768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
10259ffa9677SSam Leffler 		printf("%s: passed policy\n", __func__);
102688768458SSam Leffler 		kdebug_sadb_x_policy((struct sadb_ext *)xpl));
102788768458SSam Leffler 
102888768458SSam Leffler 	/* check policy type */
102988768458SSam Leffler 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
103088768458SSam Leffler 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
103188768458SSam Leffler 	 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
103288768458SSam Leffler 		return EINVAL;
103388768458SSam Leffler 
103488768458SSam Leffler 	/* check privileged socket */
1035c26fe973SBjoern A. Zeeb 	if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1036c26fe973SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1037c26fe973SBjoern A. Zeeb 		if (error)
103888768458SSam Leffler 			return EACCES;
1039c26fe973SBjoern A. Zeeb 	}
104088768458SSam Leffler 
104188768458SSam Leffler 	/* allocation new SP entry */
104288768458SSam Leffler 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
104388768458SSam Leffler 		return error;
104488768458SSam Leffler 
104588768458SSam Leffler 	newsp->state = IPSEC_SPSTATE_ALIVE;
104688768458SSam Leffler 
104788768458SSam Leffler 	/* clear old SP and set new SP */
104888768458SSam Leffler 	KEY_FREESP(pcb_sp);
104988768458SSam Leffler 	*pcb_sp = newsp;
105088768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
10519ffa9677SSam Leffler 		printf("%s: new policy\n", __func__);
105288768458SSam Leffler 		kdebug_secpolicy(newsp));
105388768458SSam Leffler 
105488768458SSam Leffler 	return 0;
105588768458SSam Leffler }
105688768458SSam Leffler 
105788768458SSam Leffler static int
105888768458SSam Leffler ipsec_get_policy(pcb_sp, mp)
105988768458SSam Leffler 	struct secpolicy *pcb_sp;
106088768458SSam Leffler 	struct mbuf **mp;
106188768458SSam Leffler {
106288768458SSam Leffler 
106388768458SSam Leffler 	/* sanity check. */
106488768458SSam Leffler 	if (pcb_sp == NULL || mp == NULL)
106588768458SSam Leffler 		return EINVAL;
106688768458SSam Leffler 
106788768458SSam Leffler 	*mp = key_sp2msg(pcb_sp);
106888768458SSam Leffler 	if (!*mp) {
10699ffa9677SSam Leffler 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
107088768458SSam Leffler 		return ENOBUFS;
107188768458SSam Leffler 	}
107288768458SSam Leffler 
107388768458SSam Leffler 	(*mp)->m_type = MT_DATA;
107488768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
10759ffa9677SSam Leffler 		printf("%s:\n", __func__); kdebug_mbuf(*mp));
107688768458SSam Leffler 
107788768458SSam Leffler 	return 0;
107888768458SSam Leffler }
107988768458SSam Leffler 
108088768458SSam Leffler int
1081c26fe973SBjoern A. Zeeb ipsec4_set_policy(inp, optname, request, len, cred)
108288768458SSam Leffler 	struct inpcb *inp;
108388768458SSam Leffler 	int optname;
108488768458SSam Leffler 	caddr_t request;
108588768458SSam Leffler 	size_t len;
1086c26fe973SBjoern A. Zeeb 	struct ucred *cred;
108788768458SSam Leffler {
108888768458SSam Leffler 	struct sadb_x_policy *xpl;
108988768458SSam Leffler 	struct secpolicy **pcb_sp;
109088768458SSam Leffler 
109188768458SSam Leffler 	/* sanity check. */
109288768458SSam Leffler 	if (inp == NULL || request == NULL)
109388768458SSam Leffler 		return EINVAL;
109488768458SSam Leffler 	if (len < sizeof(*xpl))
109588768458SSam Leffler 		return EINVAL;
109688768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
109788768458SSam Leffler 
109888768458SSam Leffler 	/* select direction */
109988768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
110088768458SSam Leffler 	case IPSEC_DIR_INBOUND:
110188768458SSam Leffler 		pcb_sp = &inp->inp_sp->sp_in;
110288768458SSam Leffler 		break;
110388768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
110488768458SSam Leffler 		pcb_sp = &inp->inp_sp->sp_out;
110588768458SSam Leffler 		break;
110688768458SSam Leffler 	default:
11079ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
110888768458SSam Leffler 			xpl->sadb_x_policy_dir));
110988768458SSam Leffler 		return EINVAL;
111088768458SSam Leffler 	}
111188768458SSam Leffler 
1112c26fe973SBjoern A. Zeeb 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
111388768458SSam Leffler }
111488768458SSam Leffler 
111588768458SSam Leffler int
111688768458SSam Leffler ipsec4_get_policy(inp, request, len, mp)
111788768458SSam Leffler 	struct inpcb *inp;
111888768458SSam Leffler 	caddr_t request;
111988768458SSam Leffler 	size_t len;
112088768458SSam Leffler 	struct mbuf **mp;
112188768458SSam Leffler {
112288768458SSam Leffler 	struct sadb_x_policy *xpl;
112388768458SSam Leffler 	struct secpolicy *pcb_sp;
112488768458SSam Leffler 
112588768458SSam Leffler 	/* sanity check. */
112688768458SSam Leffler 	if (inp == NULL || request == NULL || mp == NULL)
112788768458SSam Leffler 		return EINVAL;
11289ffa9677SSam Leffler 	IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
112988768458SSam Leffler 	if (len < sizeof(*xpl))
113088768458SSam Leffler 		return EINVAL;
113188768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
113288768458SSam Leffler 
113388768458SSam Leffler 	/* select direction */
113488768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
113588768458SSam Leffler 	case IPSEC_DIR_INBOUND:
113688768458SSam Leffler 		pcb_sp = inp->inp_sp->sp_in;
113788768458SSam Leffler 		break;
113888768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
113988768458SSam Leffler 		pcb_sp = inp->inp_sp->sp_out;
114088768458SSam Leffler 		break;
114188768458SSam Leffler 	default:
11429ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
114388768458SSam Leffler 			xpl->sadb_x_policy_dir));
114488768458SSam Leffler 		return EINVAL;
114588768458SSam Leffler 	}
114688768458SSam Leffler 
114788768458SSam Leffler 	return ipsec_get_policy(pcb_sp, mp);
114888768458SSam Leffler }
114988768458SSam Leffler 
115088768458SSam Leffler /* delete policy in PCB */
115188768458SSam Leffler int
115288768458SSam Leffler ipsec4_delete_pcbpolicy(inp)
115388768458SSam Leffler 	struct inpcb *inp;
115488768458SSam Leffler {
11559ffa9677SSam Leffler 	IPSEC_ASSERT(inp != NULL, ("null inp"));
115688768458SSam Leffler 
115788768458SSam Leffler 	if (inp->inp_sp == NULL)
115888768458SSam Leffler 		return 0;
115988768458SSam Leffler 
116088768458SSam Leffler 	if (inp->inp_sp->sp_in != NULL)
116188768458SSam Leffler 		KEY_FREESP(&inp->inp_sp->sp_in);
116288768458SSam Leffler 
116388768458SSam Leffler 	if (inp->inp_sp->sp_out != NULL)
116488768458SSam Leffler 		KEY_FREESP(&inp->inp_sp->sp_out);
116588768458SSam Leffler 
116688768458SSam Leffler 	ipsec_delpcbpolicy(inp->inp_sp);
116788768458SSam Leffler 	inp->inp_sp = NULL;
116888768458SSam Leffler 
116988768458SSam Leffler 	return 0;
117088768458SSam Leffler }
117188768458SSam Leffler 
117288768458SSam Leffler #ifdef INET6
117388768458SSam Leffler int
1174c26fe973SBjoern A. Zeeb ipsec6_set_policy(in6p, optname, request, len, cred)
117588768458SSam Leffler 	struct in6pcb *in6p;
117688768458SSam Leffler 	int optname;
117788768458SSam Leffler 	caddr_t request;
117888768458SSam Leffler 	size_t len;
1179c26fe973SBjoern A. Zeeb 	struct ucred *cred;
118088768458SSam Leffler {
118188768458SSam Leffler 	struct sadb_x_policy *xpl;
118288768458SSam Leffler 	struct secpolicy **pcb_sp;
118388768458SSam Leffler 
118488768458SSam Leffler 	/* sanity check. */
118588768458SSam Leffler 	if (in6p == NULL || request == NULL)
118688768458SSam Leffler 		return EINVAL;
118788768458SSam Leffler 	if (len < sizeof(*xpl))
118888768458SSam Leffler 		return EINVAL;
118988768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
119088768458SSam Leffler 
119188768458SSam Leffler 	/* select direction */
119288768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
119388768458SSam Leffler 	case IPSEC_DIR_INBOUND:
119488768458SSam Leffler 		pcb_sp = &in6p->in6p_sp->sp_in;
119588768458SSam Leffler 		break;
119688768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
119788768458SSam Leffler 		pcb_sp = &in6p->in6p_sp->sp_out;
119888768458SSam Leffler 		break;
119988768458SSam Leffler 	default:
12009ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
120188768458SSam Leffler 			xpl->sadb_x_policy_dir));
120288768458SSam Leffler 		return EINVAL;
120388768458SSam Leffler 	}
120488768458SSam Leffler 
1205c26fe973SBjoern A. Zeeb 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
120688768458SSam Leffler }
120788768458SSam Leffler 
120888768458SSam Leffler int
120988768458SSam Leffler ipsec6_get_policy(in6p, request, len, mp)
121088768458SSam Leffler 	struct in6pcb *in6p;
121188768458SSam Leffler 	caddr_t request;
121288768458SSam Leffler 	size_t len;
121388768458SSam Leffler 	struct mbuf **mp;
121488768458SSam Leffler {
121588768458SSam Leffler 	struct sadb_x_policy *xpl;
121688768458SSam Leffler 	struct secpolicy *pcb_sp;
121788768458SSam Leffler 
121888768458SSam Leffler 	/* sanity check. */
121988768458SSam Leffler 	if (in6p == NULL || request == NULL || mp == NULL)
122088768458SSam Leffler 		return EINVAL;
12219ffa9677SSam Leffler 	IPSEC_ASSERT(in6p->in6p_sp != NULL, ("null in6p_sp"));
122288768458SSam Leffler 	if (len < sizeof(*xpl))
122388768458SSam Leffler 		return EINVAL;
122488768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
122588768458SSam Leffler 
122688768458SSam Leffler 	/* select direction */
122788768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
122888768458SSam Leffler 	case IPSEC_DIR_INBOUND:
122988768458SSam Leffler 		pcb_sp = in6p->in6p_sp->sp_in;
123088768458SSam Leffler 		break;
123188768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
123288768458SSam Leffler 		pcb_sp = in6p->in6p_sp->sp_out;
123388768458SSam Leffler 		break;
123488768458SSam Leffler 	default:
12359ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
123688768458SSam Leffler 			xpl->sadb_x_policy_dir));
123788768458SSam Leffler 		return EINVAL;
123888768458SSam Leffler 	}
123988768458SSam Leffler 
124088768458SSam Leffler 	return ipsec_get_policy(pcb_sp, mp);
124188768458SSam Leffler }
124288768458SSam Leffler 
124388768458SSam Leffler int
124488768458SSam Leffler ipsec6_delete_pcbpolicy(in6p)
124588768458SSam Leffler 	struct in6pcb *in6p;
124688768458SSam Leffler {
12479ffa9677SSam Leffler 	IPSEC_ASSERT(in6p != NULL, ("null in6p"));
124888768458SSam Leffler 
124988768458SSam Leffler 	if (in6p->in6p_sp == NULL)
125088768458SSam Leffler 		return 0;
125188768458SSam Leffler 
125288768458SSam Leffler 	if (in6p->in6p_sp->sp_in != NULL)
125388768458SSam Leffler 		KEY_FREESP(&in6p->in6p_sp->sp_in);
125488768458SSam Leffler 
125588768458SSam Leffler 	if (in6p->in6p_sp->sp_out != NULL)
125688768458SSam Leffler 		KEY_FREESP(&in6p->in6p_sp->sp_out);
125788768458SSam Leffler 
125888768458SSam Leffler 	ipsec_delpcbpolicy(in6p->in6p_sp);
125988768458SSam Leffler 	in6p->in6p_sp = NULL;
126088768458SSam Leffler 
126188768458SSam Leffler 	return 0;
126288768458SSam Leffler }
126388768458SSam Leffler #endif
126488768458SSam Leffler 
126588768458SSam Leffler /*
126688768458SSam Leffler  * return current level.
126788768458SSam Leffler  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
126888768458SSam Leffler  */
126988768458SSam Leffler u_int
127088768458SSam Leffler ipsec_get_reqlevel(isr)
127188768458SSam Leffler 	struct ipsecrequest *isr;
127288768458SSam Leffler {
127388768458SSam Leffler 	u_int level = 0;
127488768458SSam Leffler 	u_int esp_trans_deflev, esp_net_deflev;
127588768458SSam Leffler 	u_int ah_trans_deflev, ah_net_deflev;
127688768458SSam Leffler 
12779ffa9677SSam Leffler 	IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
12789ffa9677SSam Leffler 	IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
12799ffa9677SSam Leffler 		("af family mismatch, src %u, dst %u",
128088768458SSam Leffler 		 isr->sp->spidx.src.sa.sa_family,
128188768458SSam Leffler 		 isr->sp->spidx.dst.sa.sa_family));
128288768458SSam Leffler 
128388768458SSam Leffler /* XXX note that we have ipseclog() expanded here - code sync issue */
128488768458SSam Leffler #define IPSEC_CHECK_DEFAULT(lev) \
128588768458SSam Leffler 	(((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE	      \
128688768458SSam Leffler 			&& (lev) != IPSEC_LEVEL_UNIQUE)			      \
128788768458SSam Leffler 		? (ipsec_debug						      \
128888768458SSam Leffler 			? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
128988768458SSam Leffler 				(lev), IPSEC_LEVEL_REQUIRE)		      \
129088768458SSam Leffler 			: 0),						      \
129188768458SSam Leffler 			(lev) = IPSEC_LEVEL_REQUIRE,			      \
129288768458SSam Leffler 			(lev)						      \
129388768458SSam Leffler 		: (lev))
129488768458SSam Leffler 
129588768458SSam Leffler 	/* set default level */
129688768458SSam Leffler 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
129788768458SSam Leffler #ifdef INET
129888768458SSam Leffler 	case AF_INET:
129988768458SSam Leffler 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
130088768458SSam Leffler 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
130188768458SSam Leffler 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
130288768458SSam Leffler 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
130388768458SSam Leffler 		break;
130488768458SSam Leffler #endif
130588768458SSam Leffler #ifdef INET6
130688768458SSam Leffler 	case AF_INET6:
130788768458SSam Leffler 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
130888768458SSam Leffler 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
130988768458SSam Leffler 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
131088768458SSam Leffler 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
131188768458SSam Leffler 		break;
131288768458SSam Leffler #endif /* INET6 */
131388768458SSam Leffler 	default:
13149ffa9677SSam Leffler 		panic("%s: unknown af %u",
13159ffa9677SSam Leffler 			__func__, isr->sp->spidx.src.sa.sa_family);
131688768458SSam Leffler 	}
131788768458SSam Leffler 
131888768458SSam Leffler #undef IPSEC_CHECK_DEFAULT
131988768458SSam Leffler 
132088768458SSam Leffler 	/* set level */
132188768458SSam Leffler 	switch (isr->level) {
132288768458SSam Leffler 	case IPSEC_LEVEL_DEFAULT:
132388768458SSam Leffler 		switch (isr->saidx.proto) {
132488768458SSam Leffler 		case IPPROTO_ESP:
132588768458SSam Leffler 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
132688768458SSam Leffler 				level = esp_net_deflev;
132788768458SSam Leffler 			else
132888768458SSam Leffler 				level = esp_trans_deflev;
132988768458SSam Leffler 			break;
133088768458SSam Leffler 		case IPPROTO_AH:
133188768458SSam Leffler 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
133288768458SSam Leffler 				level = ah_net_deflev;
133388768458SSam Leffler 			else
133488768458SSam Leffler 				level = ah_trans_deflev;
13358381996eSSam Leffler 			break;
133688768458SSam Leffler 		case IPPROTO_IPCOMP:
133788768458SSam Leffler 			/*
133888768458SSam Leffler 			 * we don't really care, as IPcomp document says that
133988768458SSam Leffler 			 * we shouldn't compress small packets
134088768458SSam Leffler 			 */
134188768458SSam Leffler 			level = IPSEC_LEVEL_USE;
134288768458SSam Leffler 			break;
134388768458SSam Leffler 		default:
13449ffa9677SSam Leffler 			panic("%s: Illegal protocol defined %u\n", __func__,
134588768458SSam Leffler 				isr->saidx.proto);
134688768458SSam Leffler 		}
134788768458SSam Leffler 		break;
134888768458SSam Leffler 
134988768458SSam Leffler 	case IPSEC_LEVEL_USE:
135088768458SSam Leffler 	case IPSEC_LEVEL_REQUIRE:
135188768458SSam Leffler 		level = isr->level;
135288768458SSam Leffler 		break;
135388768458SSam Leffler 	case IPSEC_LEVEL_UNIQUE:
135488768458SSam Leffler 		level = IPSEC_LEVEL_REQUIRE;
135588768458SSam Leffler 		break;
135688768458SSam Leffler 
135788768458SSam Leffler 	default:
13589ffa9677SSam Leffler 		panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
135988768458SSam Leffler 	}
136088768458SSam Leffler 
136188768458SSam Leffler 	return level;
136288768458SSam Leffler }
136388768458SSam Leffler 
136488768458SSam Leffler /*
136588768458SSam Leffler  * Check security policy requirements against the actual
136688768458SSam Leffler  * packet contents.  Return one if the packet should be
136788768458SSam Leffler  * reject as "invalid"; otherwiser return zero to have the
136888768458SSam Leffler  * packet treated as "valid".
136988768458SSam Leffler  *
137088768458SSam Leffler  * OUT:
137188768458SSam Leffler  *	0: valid
137288768458SSam Leffler  *	1: invalid
137388768458SSam Leffler  */
137488768458SSam Leffler int
137588768458SSam Leffler ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
137688768458SSam Leffler {
137788768458SSam Leffler 	struct ipsecrequest *isr;
137888768458SSam Leffler 	int need_auth;
137988768458SSam Leffler 
138088768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
13819ffa9677SSam Leffler 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
138288768458SSam Leffler 
138388768458SSam Leffler 	/* check policy */
138488768458SSam Leffler 	switch (sp->policy) {
138588768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
138688768458SSam Leffler 		return 1;
138788768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
138888768458SSam Leffler 	case IPSEC_POLICY_NONE:
138988768458SSam Leffler 		return 0;
139088768458SSam Leffler 	}
139188768458SSam Leffler 
13929ffa9677SSam Leffler 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
13939ffa9677SSam Leffler 		("invalid policy %u", sp->policy));
139488768458SSam Leffler 
139588768458SSam Leffler 	/* XXX should compare policy against ipsec header history */
139688768458SSam Leffler 
139788768458SSam Leffler 	need_auth = 0;
139888768458SSam Leffler 	for (isr = sp->req; isr != NULL; isr = isr->next) {
139988768458SSam Leffler 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
140088768458SSam Leffler 			continue;
140188768458SSam Leffler 		switch (isr->saidx.proto) {
140288768458SSam Leffler 		case IPPROTO_ESP:
140388768458SSam Leffler 			if ((m->m_flags & M_DECRYPTED) == 0) {
140488768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14059ffa9677SSam Leffler 				    printf("%s: ESP m_flags:%x\n", __func__,
140688768458SSam Leffler 					    m->m_flags));
140788768458SSam Leffler 				return 1;
140888768458SSam Leffler 			}
140988768458SSam Leffler 
141088768458SSam Leffler 			if (!need_auth &&
141188768458SSam Leffler 			    isr->sav != NULL &&
141288768458SSam Leffler 			    isr->sav->tdb_authalgxform != NULL &&
141388768458SSam Leffler 			    (m->m_flags & M_AUTHIPDGM) == 0) {
141488768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14159ffa9677SSam Leffler 				    printf("%s: ESP/AH m_flags:%x\n", __func__,
141688768458SSam Leffler 					    m->m_flags));
141788768458SSam Leffler 				return 1;
141888768458SSam Leffler 			}
141988768458SSam Leffler 			break;
142088768458SSam Leffler 		case IPPROTO_AH:
142188768458SSam Leffler 			need_auth = 1;
142288768458SSam Leffler 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
142388768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14249ffa9677SSam Leffler 				    printf("%s: AH m_flags:%x\n", __func__,
142588768458SSam Leffler 					    m->m_flags));
142688768458SSam Leffler 				return 1;
142788768458SSam Leffler 			}
142888768458SSam Leffler 			break;
142988768458SSam Leffler 		case IPPROTO_IPCOMP:
143088768458SSam Leffler 			/*
143188768458SSam Leffler 			 * we don't really care, as IPcomp document
143288768458SSam Leffler 			 * says that we shouldn't compress small
143388768458SSam Leffler 			 * packets, IPComp policy should always be
143488768458SSam Leffler 			 * treated as being in "use" level.
143588768458SSam Leffler 			 */
143688768458SSam Leffler 			break;
143788768458SSam Leffler 		}
143888768458SSam Leffler 	}
143988768458SSam Leffler 	return 0;		/* valid */
144088768458SSam Leffler }
144188768458SSam Leffler 
144288768458SSam Leffler /*
144388768458SSam Leffler  * Check AH/ESP integrity.
144488768458SSam Leffler  * This function is called from tcp_input(), udp_input(),
144588768458SSam Leffler  * and {ah,esp}4_input for tunnel mode
144688768458SSam Leffler  */
144788768458SSam Leffler int
144888768458SSam Leffler ipsec4_in_reject(m, inp)
144988768458SSam Leffler 	struct mbuf *m;
145088768458SSam Leffler 	struct inpcb *inp;
145188768458SSam Leffler {
145288768458SSam Leffler 	struct secpolicy *sp;
145388768458SSam Leffler 	int error;
145488768458SSam Leffler 	int result;
145588768458SSam Leffler 
14569ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
145788768458SSam Leffler 
145888768458SSam Leffler 	/* get SP for this packet.
145988768458SSam Leffler 	 * When we are called from ip_forward(), we call
146088768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
146188768458SSam Leffler 	 */
146288768458SSam Leffler 	if (inp == NULL)
146388768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
146488768458SSam Leffler 	else
146588768458SSam Leffler 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
146688768458SSam Leffler 
146788768458SSam Leffler 	if (sp != NULL) {
146888768458SSam Leffler 		result = ipsec_in_reject(sp, m);
146988768458SSam Leffler 		if (result)
14702cb64cb2SGeorge V. Neville-Neil 			ipsec4stat.ips_in_polvio++;
147188768458SSam Leffler 		KEY_FREESP(&sp);
147288768458SSam Leffler 	} else {
147388768458SSam Leffler 		result = 0;	/* XXX should be panic ?
147488768458SSam Leffler 				 * -> No, there may be error. */
147588768458SSam Leffler 	}
147688768458SSam Leffler 	return result;
147788768458SSam Leffler }
147888768458SSam Leffler 
147988768458SSam Leffler #ifdef INET6
148088768458SSam Leffler /*
148188768458SSam Leffler  * Check AH/ESP integrity.
148288768458SSam Leffler  * This function is called from tcp6_input(), udp6_input(),
148388768458SSam Leffler  * and {ah,esp}6_input for tunnel mode
148488768458SSam Leffler  */
148588768458SSam Leffler int
148688768458SSam Leffler ipsec6_in_reject(m, inp)
148788768458SSam Leffler 	struct mbuf *m;
148888768458SSam Leffler 	struct inpcb *inp;
148988768458SSam Leffler {
149088768458SSam Leffler 	struct secpolicy *sp = NULL;
149188768458SSam Leffler 	int error;
149288768458SSam Leffler 	int result;
149388768458SSam Leffler 
149488768458SSam Leffler 	/* sanity check */
149588768458SSam Leffler 	if (m == NULL)
149688768458SSam Leffler 		return 0;	/* XXX should be panic ? */
149788768458SSam Leffler 
149888768458SSam Leffler 	/* get SP for this packet.
149988768458SSam Leffler 	 * When we are called from ip_forward(), we call
150088768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
150188768458SSam Leffler 	 */
150288768458SSam Leffler 	if (inp == NULL)
150388768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
150488768458SSam Leffler 	else
150588768458SSam Leffler 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
150688768458SSam Leffler 
150788768458SSam Leffler 	if (sp != NULL) {
150888768458SSam Leffler 		result = ipsec_in_reject(sp, m);
150988768458SSam Leffler 		if (result)
15102cb64cb2SGeorge V. Neville-Neil 			ipsec6stat.ips_in_polvio++;
151188768458SSam Leffler 		KEY_FREESP(&sp);
151288768458SSam Leffler 	} else {
151388768458SSam Leffler 		result = 0;
151488768458SSam Leffler 	}
151588768458SSam Leffler 	return result;
151688768458SSam Leffler }
151788768458SSam Leffler #endif
151888768458SSam Leffler 
151988768458SSam Leffler /*
152088768458SSam Leffler  * compute the byte size to be occupied by IPsec header.
152188768458SSam Leffler  * in case it is tunneled, it includes the size of outer IP header.
152288768458SSam Leffler  * NOTE: SP passed is free in this function.
152388768458SSam Leffler  */
152488768458SSam Leffler static size_t
152588768458SSam Leffler ipsec_hdrsiz(struct secpolicy *sp)
152688768458SSam Leffler {
152788768458SSam Leffler 	struct ipsecrequest *isr;
152888768458SSam Leffler 	size_t siz;
152988768458SSam Leffler 
153088768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
15319ffa9677SSam Leffler 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
153288768458SSam Leffler 
153388768458SSam Leffler 	switch (sp->policy) {
153488768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
153588768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
153688768458SSam Leffler 	case IPSEC_POLICY_NONE:
153788768458SSam Leffler 		return 0;
153888768458SSam Leffler 	}
153988768458SSam Leffler 
15409ffa9677SSam Leffler 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
15419ffa9677SSam Leffler 		("invalid policy %u", sp->policy));
154288768458SSam Leffler 
154388768458SSam Leffler 	siz = 0;
154488768458SSam Leffler 	for (isr = sp->req; isr != NULL; isr = isr->next) {
154588768458SSam Leffler 		size_t clen = 0;
154688768458SSam Leffler 
154788768458SSam Leffler 		switch (isr->saidx.proto) {
154888768458SSam Leffler 		case IPPROTO_ESP:
154988768458SSam Leffler 			clen = esp_hdrsiz(isr->sav);
155088768458SSam Leffler 			break;
155188768458SSam Leffler 		case IPPROTO_AH:
155288768458SSam Leffler 			clen = ah_hdrsiz(isr->sav);
155388768458SSam Leffler 			break;
155488768458SSam Leffler 		case IPPROTO_IPCOMP:
155588768458SSam Leffler 			clen = sizeof(struct ipcomp);
155688768458SSam Leffler 			break;
155788768458SSam Leffler 		}
155888768458SSam Leffler 
155988768458SSam Leffler 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
156088768458SSam Leffler 			switch (isr->saidx.dst.sa.sa_family) {
156188768458SSam Leffler 			case AF_INET:
156288768458SSam Leffler 				clen += sizeof(struct ip);
156388768458SSam Leffler 				break;
156488768458SSam Leffler #ifdef INET6
156588768458SSam Leffler 			case AF_INET6:
156688768458SSam Leffler 				clen += sizeof(struct ip6_hdr);
156788768458SSam Leffler 				break;
156888768458SSam Leffler #endif
156988768458SSam Leffler 			default:
15709ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: unknown AF %d in "
15719ffa9677SSam Leffler 				    "IPsec tunnel SA\n", __func__,
157288768458SSam Leffler 				    ((struct sockaddr *)&isr->saidx.dst)->sa_family));
157388768458SSam Leffler 				break;
157488768458SSam Leffler 			}
157588768458SSam Leffler 		}
157688768458SSam Leffler 		siz += clen;
157788768458SSam Leffler 	}
157888768458SSam Leffler 
157988768458SSam Leffler 	return siz;
158088768458SSam Leffler }
158188768458SSam Leffler 
158288768458SSam Leffler /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
158388768458SSam Leffler size_t
158488768458SSam Leffler ipsec4_hdrsiz(m, dir, inp)
158588768458SSam Leffler 	struct mbuf *m;
158688768458SSam Leffler 	u_int dir;
158788768458SSam Leffler 	struct inpcb *inp;
158888768458SSam Leffler {
158988768458SSam Leffler 	struct secpolicy *sp;
159088768458SSam Leffler 	int error;
159188768458SSam Leffler 	size_t size;
159288768458SSam Leffler 
15939ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
159488768458SSam Leffler 
159588768458SSam Leffler 	/* get SP for this packet.
159688768458SSam Leffler 	 * When we are called from ip_forward(), we call
159788768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
159888768458SSam Leffler 	 */
159988768458SSam Leffler 	if (inp == NULL)
160088768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
160188768458SSam Leffler 	else
160288768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, inp, &error);
160388768458SSam Leffler 
160488768458SSam Leffler 	if (sp != NULL) {
160588768458SSam Leffler 		size = ipsec_hdrsiz(sp);
160688768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DATA,
16079ffa9677SSam Leffler 			printf("%s: size:%lu.\n", __func__,
160888768458SSam Leffler 				(unsigned long)size));
160988768458SSam Leffler 
161088768458SSam Leffler 		KEY_FREESP(&sp);
161188768458SSam Leffler 	} else {
16124a67e051SBjoern A. Zeeb 		size = 0;	/* XXX should be panic ?
16134a67e051SBjoern A. Zeeb 				 * -> No, we are called w/o knowing if
16144a67e051SBjoern A. Zeeb 				 *    IPsec processing is needed. */
161588768458SSam Leffler 	}
161688768458SSam Leffler 	return size;
161788768458SSam Leffler }
161888768458SSam Leffler 
161988768458SSam Leffler #ifdef INET6
162088768458SSam Leffler /* This function is called from ipsec6_hdrsize_tcp(),
162188768458SSam Leffler  * and maybe from ip6_forward.()
162288768458SSam Leffler  */
162388768458SSam Leffler size_t
162488768458SSam Leffler ipsec6_hdrsiz(m, dir, in6p)
162588768458SSam Leffler 	struct mbuf *m;
162688768458SSam Leffler 	u_int dir;
162788768458SSam Leffler 	struct in6pcb *in6p;
162888768458SSam Leffler {
162988768458SSam Leffler 	struct secpolicy *sp;
163088768458SSam Leffler 	int error;
163188768458SSam Leffler 	size_t size;
163288768458SSam Leffler 
16339ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
16349ffa9677SSam Leffler 	IPSEC_ASSERT(in6p == NULL || in6p->in6p_socket != NULL,
16359ffa9677SSam Leffler 		("socket w/o inpcb"));
163688768458SSam Leffler 
163788768458SSam Leffler 	/* get SP for this packet */
163888768458SSam Leffler 	/* XXX Is it right to call with IP_FORWARDING. */
163988768458SSam Leffler 	if (in6p == NULL)
164088768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
164188768458SSam Leffler 	else
164288768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, in6p, &error);
164388768458SSam Leffler 
164488768458SSam Leffler 	if (sp == NULL)
164588768458SSam Leffler 		return 0;
164688768458SSam Leffler 	size = ipsec_hdrsiz(sp);
164788768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
16489ffa9677SSam Leffler 		printf("%s: size:%lu.\n", __func__, (unsigned long)size));
164988768458SSam Leffler 	KEY_FREESP(&sp);
165088768458SSam Leffler 
165188768458SSam Leffler 	return size;
165288768458SSam Leffler }
165388768458SSam Leffler #endif /*INET6*/
165488768458SSam Leffler 
165588768458SSam Leffler /*
165688768458SSam Leffler  * Check the variable replay window.
165788768458SSam Leffler  * ipsec_chkreplay() performs replay check before ICV verification.
165888768458SSam Leffler  * ipsec_updatereplay() updates replay bitmap.  This must be called after
165988768458SSam Leffler  * ICV verification (it also performs replay check, which is usually done
166088768458SSam Leffler  * beforehand).
166188768458SSam Leffler  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
166288768458SSam Leffler  *
166388768458SSam Leffler  * based on RFC 2401.
166488768458SSam Leffler  */
166588768458SSam Leffler int
166688768458SSam Leffler ipsec_chkreplay(seq, sav)
166788768458SSam Leffler 	u_int32_t seq;
166888768458SSam Leffler 	struct secasvar *sav;
166988768458SSam Leffler {
167088768458SSam Leffler 	const struct secreplay *replay;
167188768458SSam Leffler 	u_int32_t diff;
167288768458SSam Leffler 	int fr;
167388768458SSam Leffler 	u_int32_t wsizeb;	/* constant: bits of window size */
167488768458SSam Leffler 	int frlast;		/* constant: last frame */
167588768458SSam Leffler 
16769ffa9677SSam Leffler 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
16779ffa9677SSam Leffler 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
167888768458SSam Leffler 
167988768458SSam Leffler 	replay = sav->replay;
168088768458SSam Leffler 
168188768458SSam Leffler 	if (replay->wsize == 0)
168288768458SSam Leffler 		return 1;	/* no need to check replay. */
168388768458SSam Leffler 
168488768458SSam Leffler 	/* constant */
168588768458SSam Leffler 	frlast = replay->wsize - 1;
168688768458SSam Leffler 	wsizeb = replay->wsize << 3;
168788768458SSam Leffler 
168888768458SSam Leffler 	/* sequence number of 0 is invalid */
168988768458SSam Leffler 	if (seq == 0)
169088768458SSam Leffler 		return 0;
169188768458SSam Leffler 
169288768458SSam Leffler 	/* first time is always okay */
169388768458SSam Leffler 	if (replay->count == 0)
169488768458SSam Leffler 		return 1;
169588768458SSam Leffler 
169688768458SSam Leffler 	if (seq > replay->lastseq) {
169788768458SSam Leffler 		/* larger sequences are okay */
169888768458SSam Leffler 		return 1;
169988768458SSam Leffler 	} else {
170088768458SSam Leffler 		/* seq is equal or less than lastseq. */
170188768458SSam Leffler 		diff = replay->lastseq - seq;
170288768458SSam Leffler 
170388768458SSam Leffler 		/* over range to check, i.e. too old or wrapped */
170488768458SSam Leffler 		if (diff >= wsizeb)
170588768458SSam Leffler 			return 0;
170688768458SSam Leffler 
170788768458SSam Leffler 		fr = frlast - diff / 8;
170888768458SSam Leffler 
170988768458SSam Leffler 		/* this packet already seen ? */
171088768458SSam Leffler 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
171188768458SSam Leffler 			return 0;
171288768458SSam Leffler 
171388768458SSam Leffler 		/* out of order but good */
171488768458SSam Leffler 		return 1;
171588768458SSam Leffler 	}
171688768458SSam Leffler }
171788768458SSam Leffler 
171888768458SSam Leffler /*
171988768458SSam Leffler  * check replay counter whether to update or not.
172088768458SSam Leffler  * OUT:	0:	OK
172188768458SSam Leffler  *	1:	NG
172288768458SSam Leffler  */
172388768458SSam Leffler int
172488768458SSam Leffler ipsec_updatereplay(seq, sav)
172588768458SSam Leffler 	u_int32_t seq;
172688768458SSam Leffler 	struct secasvar *sav;
172788768458SSam Leffler {
172888768458SSam Leffler 	struct secreplay *replay;
172988768458SSam Leffler 	u_int32_t diff;
173088768458SSam Leffler 	int fr;
173188768458SSam Leffler 	u_int32_t wsizeb;	/* constant: bits of window size */
173288768458SSam Leffler 	int frlast;		/* constant: last frame */
173388768458SSam Leffler 
17349ffa9677SSam Leffler 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
17359ffa9677SSam Leffler 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
173688768458SSam Leffler 
173788768458SSam Leffler 	replay = sav->replay;
173888768458SSam Leffler 
173988768458SSam Leffler 	if (replay->wsize == 0)
174088768458SSam Leffler 		goto ok;	/* no need to check replay. */
174188768458SSam Leffler 
174288768458SSam Leffler 	/* constant */
174388768458SSam Leffler 	frlast = replay->wsize - 1;
174488768458SSam Leffler 	wsizeb = replay->wsize << 3;
174588768458SSam Leffler 
174688768458SSam Leffler 	/* sequence number of 0 is invalid */
174788768458SSam Leffler 	if (seq == 0)
174888768458SSam Leffler 		return 1;
174988768458SSam Leffler 
175088768458SSam Leffler 	/* first time */
175188768458SSam Leffler 	if (replay->count == 0) {
175288768458SSam Leffler 		replay->lastseq = seq;
175388768458SSam Leffler 		bzero(replay->bitmap, replay->wsize);
175488768458SSam Leffler 		(replay->bitmap)[frlast] = 1;
175588768458SSam Leffler 		goto ok;
175688768458SSam Leffler 	}
175788768458SSam Leffler 
175888768458SSam Leffler 	if (seq > replay->lastseq) {
175988768458SSam Leffler 		/* seq is larger than lastseq. */
176088768458SSam Leffler 		diff = seq - replay->lastseq;
176188768458SSam Leffler 
176288768458SSam Leffler 		/* new larger sequence number */
176388768458SSam Leffler 		if (diff < wsizeb) {
176488768458SSam Leffler 			/* In window */
176588768458SSam Leffler 			/* set bit for this packet */
176688768458SSam Leffler 			vshiftl(replay->bitmap, diff, replay->wsize);
176788768458SSam Leffler 			(replay->bitmap)[frlast] |= 1;
176888768458SSam Leffler 		} else {
176988768458SSam Leffler 			/* this packet has a "way larger" */
177088768458SSam Leffler 			bzero(replay->bitmap, replay->wsize);
177188768458SSam Leffler 			(replay->bitmap)[frlast] = 1;
177288768458SSam Leffler 		}
177388768458SSam Leffler 		replay->lastseq = seq;
177488768458SSam Leffler 
177588768458SSam Leffler 		/* larger is good */
177688768458SSam Leffler 	} else {
177788768458SSam Leffler 		/* seq is equal or less than lastseq. */
177888768458SSam Leffler 		diff = replay->lastseq - seq;
177988768458SSam Leffler 
178088768458SSam Leffler 		/* over range to check, i.e. too old or wrapped */
178188768458SSam Leffler 		if (diff >= wsizeb)
178288768458SSam Leffler 			return 1;
178388768458SSam Leffler 
178488768458SSam Leffler 		fr = frlast - diff / 8;
178588768458SSam Leffler 
178688768458SSam Leffler 		/* this packet already seen ? */
178788768458SSam Leffler 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
178888768458SSam Leffler 			return 1;
178988768458SSam Leffler 
179088768458SSam Leffler 		/* mark as seen */
179188768458SSam Leffler 		(replay->bitmap)[fr] |= (1 << (diff % 8));
179288768458SSam Leffler 
179388768458SSam Leffler 		/* out of order but good */
179488768458SSam Leffler 	}
179588768458SSam Leffler 
179688768458SSam Leffler ok:
179788768458SSam Leffler 	if (replay->count == ~0) {
179888768458SSam Leffler 
179988768458SSam Leffler 		/* set overflow flag */
180088768458SSam Leffler 		replay->overflow++;
180188768458SSam Leffler 
180288768458SSam Leffler 		/* don't increment, no more packets accepted */
180388768458SSam Leffler 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
180488768458SSam Leffler 			return 1;
180588768458SSam Leffler 
18069ffa9677SSam Leffler 		ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
18079ffa9677SSam Leffler 		    __func__, replay->overflow, ipsec_logsastr(sav)));
180888768458SSam Leffler 	}
180988768458SSam Leffler 
181088768458SSam Leffler 	replay->count++;
181188768458SSam Leffler 
181288768458SSam Leffler 	return 0;
181388768458SSam Leffler }
181488768458SSam Leffler 
181588768458SSam Leffler /*
1816d24ff94bSHiten Pandya  * shift variable length buffer to left.
181788768458SSam Leffler  * IN:	bitmap: pointer to the buffer
181888768458SSam Leffler  * 	nbit:	the number of to shift.
181988768458SSam Leffler  *	wsize:	buffer size (bytes).
182088768458SSam Leffler  */
182188768458SSam Leffler static void
182288768458SSam Leffler vshiftl(bitmap, nbit, wsize)
182388768458SSam Leffler 	unsigned char *bitmap;
182488768458SSam Leffler 	int nbit, wsize;
182588768458SSam Leffler {
182688768458SSam Leffler 	int s, j, i;
182788768458SSam Leffler 	unsigned char over;
182888768458SSam Leffler 
182988768458SSam Leffler 	for (j = 0; j < nbit; j += 8) {
183088768458SSam Leffler 		s = (nbit - j < 8) ? (nbit - j): 8;
183188768458SSam Leffler 		bitmap[0] <<= s;
183288768458SSam Leffler 		for (i = 1; i < wsize; i++) {
183388768458SSam Leffler 			over = (bitmap[i] >> (8 - s));
183488768458SSam Leffler 			bitmap[i] <<= s;
183588768458SSam Leffler 			bitmap[i-1] |= over;
183688768458SSam Leffler 		}
183788768458SSam Leffler 	}
183888768458SSam Leffler 
183988768458SSam Leffler 	return;
184088768458SSam Leffler }
184188768458SSam Leffler 
184288768458SSam Leffler /* Return a printable string for the IPv4 address. */
184388768458SSam Leffler static char *
184488768458SSam Leffler inet_ntoa4(struct in_addr ina)
184588768458SSam Leffler {
184688768458SSam Leffler 	static char buf[4][4 * sizeof "123" + 4];
184788768458SSam Leffler 	unsigned char *ucp = (unsigned char *) &ina;
184888768458SSam Leffler 	static int i = 3;
184988768458SSam Leffler 
18501d54aa3bSBjoern A. Zeeb 	/* XXX-BZ returns static buffer. */
185188768458SSam Leffler 	i = (i + 1) % 4;
185288768458SSam Leffler 	sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
185388768458SSam Leffler 	    ucp[2] & 0xff, ucp[3] & 0xff);
185488768458SSam Leffler 	return (buf[i]);
185588768458SSam Leffler }
185688768458SSam Leffler 
185788768458SSam Leffler /* Return a printable string for the address. */
185888768458SSam Leffler char *
185988768458SSam Leffler ipsec_address(union sockaddr_union* sa)
186088768458SSam Leffler {
1861224c45c4SBjoern A. Zeeb #ifdef INET6
18621d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
18631d54aa3bSBjoern A. Zeeb #endif
186488768458SSam Leffler 	switch (sa->sa.sa_family) {
186549ddabdfSPawel Jakub Dawidek #ifdef INET
186688768458SSam Leffler 	case AF_INET:
186788768458SSam Leffler 		return inet_ntoa4(sa->sin.sin_addr);
186888768458SSam Leffler #endif /* INET */
186988768458SSam Leffler 
187049ddabdfSPawel Jakub Dawidek #ifdef INET6
187188768458SSam Leffler 	case AF_INET6:
18721d54aa3bSBjoern A. Zeeb 		return ip6_sprintf(ip6buf, &sa->sin6.sin6_addr);
187388768458SSam Leffler #endif /* INET6 */
187488768458SSam Leffler 
187588768458SSam Leffler 	default:
187688768458SSam Leffler 		return "(unknown address family)";
187788768458SSam Leffler 	}
187888768458SSam Leffler }
187988768458SSam Leffler 
188088768458SSam Leffler const char *
188188768458SSam Leffler ipsec_logsastr(sav)
188288768458SSam Leffler 	struct secasvar *sav;
188388768458SSam Leffler {
188488768458SSam Leffler 	static char buf[256];
188588768458SSam Leffler 	char *p;
188688768458SSam Leffler 	struct secasindex *saidx = &sav->sah->saidx;
188788768458SSam Leffler 
18889ffa9677SSam Leffler 	IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
18899ffa9677SSam Leffler 		("address family mismatch"));
189088768458SSam Leffler 
189188768458SSam Leffler 	p = buf;
189288768458SSam Leffler 	snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
189388768458SSam Leffler 	while (p && *p)
189488768458SSam Leffler 		p++;
189588768458SSam Leffler 	/* NB: only use ipsec_address on one address at a time */
189688768458SSam Leffler 	snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
189788768458SSam Leffler 		ipsec_address(&saidx->src));
189888768458SSam Leffler 	while (p && *p)
189988768458SSam Leffler 		p++;
190088768458SSam Leffler 	snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
190188768458SSam Leffler 		ipsec_address(&saidx->dst));
190288768458SSam Leffler 
190388768458SSam Leffler 	return buf;
190488768458SSam Leffler }
190588768458SSam Leffler 
190688768458SSam Leffler void
190788768458SSam Leffler ipsec_dumpmbuf(m)
190888768458SSam Leffler 	struct mbuf *m;
190988768458SSam Leffler {
191088768458SSam Leffler 	int totlen;
191188768458SSam Leffler 	int i;
191288768458SSam Leffler 	u_char *p;
191388768458SSam Leffler 
191488768458SSam Leffler 	totlen = 0;
191588768458SSam Leffler 	printf("---\n");
191688768458SSam Leffler 	while (m) {
191788768458SSam Leffler 		p = mtod(m, u_char *);
191888768458SSam Leffler 		for (i = 0; i < m->m_len; i++) {
191988768458SSam Leffler 			printf("%02x ", p[i]);
192088768458SSam Leffler 			totlen++;
192188768458SSam Leffler 			if (totlen % 16 == 0)
192288768458SSam Leffler 				printf("\n");
192388768458SSam Leffler 		}
192488768458SSam Leffler 		m = m->m_next;
192588768458SSam Leffler 	}
192688768458SSam Leffler 	if (totlen % 16 != 0)
192788768458SSam Leffler 		printf("\n");
192888768458SSam Leffler 	printf("---\n");
192988768458SSam Leffler }
193088768458SSam Leffler 
19318381996eSSam Leffler static void
19328381996eSSam Leffler ipsec_attach(void)
19338381996eSSam Leffler {
19348381996eSSam Leffler 	SECPOLICY_LOCK_INIT(&ip4_def_policy);
19358381996eSSam Leffler 	ip4_def_policy.refcnt = 1;			/* NB: disallow free */
19368381996eSSam Leffler }
1937237fdd78SRobert Watson SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL);
19388381996eSSam Leffler 
19398381996eSSam Leffler 
194088768458SSam Leffler /* XXX this stuff doesn't belong here... */
194188768458SSam Leffler 
194288768458SSam Leffler static	struct xformsw* xforms = NULL;
194388768458SSam Leffler 
194488768458SSam Leffler /*
194588768458SSam Leffler  * Register a transform; typically at system startup.
194688768458SSam Leffler  */
194788768458SSam Leffler void
194888768458SSam Leffler xform_register(struct xformsw* xsp)
194988768458SSam Leffler {
195088768458SSam Leffler 	xsp->xf_next = xforms;
195188768458SSam Leffler 	xforms = xsp;
195288768458SSam Leffler }
195388768458SSam Leffler 
195488768458SSam Leffler /*
195588768458SSam Leffler  * Initialize transform support in an sav.
195688768458SSam Leffler  */
195788768458SSam Leffler int
195888768458SSam Leffler xform_init(struct secasvar *sav, int xftype)
195988768458SSam Leffler {
196088768458SSam Leffler 	struct xformsw *xsp;
196188768458SSam Leffler 
196282a6d6acSSam Leffler 	if (sav->tdb_xform != NULL)	/* previously initialized */
196382a6d6acSSam Leffler 		return 0;
196488768458SSam Leffler 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
196588768458SSam Leffler 		if (xsp->xf_type == xftype)
196688768458SSam Leffler 			return (*xsp->xf_init)(sav, xsp);
196788768458SSam Leffler 	return EINVAL;
196888768458SSam Leffler }
1969