xref: /freebsd/sys/netipsec/ipsec.c (revision 8b615593fc0d78ef8366c1328f5966256b82a9c0)
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>
56603724d3SBjoern A. Zeeb #include <sys/vimage.h>
5788768458SSam Leffler 
5888768458SSam Leffler #include <net/if.h>
5988768458SSam Leffler #include <net/route.h>
6088768458SSam Leffler 
6188768458SSam Leffler #include <netinet/in.h>
6288768458SSam Leffler #include <netinet/in_systm.h>
6388768458SSam Leffler #include <netinet/ip.h>
6488768458SSam Leffler #include <netinet/ip_var.h>
6588768458SSam Leffler #include <netinet/in_var.h>
6688768458SSam Leffler #include <netinet/udp.h>
6788768458SSam Leffler #include <netinet/udp_var.h>
6888768458SSam Leffler #include <netinet/tcp.h>
6988768458SSam Leffler #include <netinet/udp.h>
7088768458SSam Leffler 
7188768458SSam Leffler #include <netinet/ip6.h>
7288768458SSam Leffler #ifdef INET6
7388768458SSam Leffler #include <netinet6/ip6_var.h>
7488768458SSam Leffler #endif
7588768458SSam Leffler #include <netinet/in_pcb.h>
7688768458SSam Leffler #ifdef INET6
7788768458SSam Leffler #include <netinet/icmp6.h>
7888768458SSam Leffler #endif
7988768458SSam Leffler 
802cb64cb2SGeorge V. Neville-Neil #include <sys/types.h>
8188768458SSam Leffler #include <netipsec/ipsec.h>
8288768458SSam Leffler #ifdef INET6
8388768458SSam Leffler #include <netipsec/ipsec6.h>
8488768458SSam Leffler #endif
8588768458SSam Leffler #include <netipsec/ah_var.h>
8688768458SSam Leffler #include <netipsec/esp_var.h>
8788768458SSam Leffler #include <netipsec/ipcomp.h>		/*XXX*/
8888768458SSam Leffler #include <netipsec/ipcomp_var.h>
8988768458SSam Leffler 
9088768458SSam Leffler #include <netipsec/key.h>
9188768458SSam Leffler #include <netipsec/keydb.h>
9288768458SSam Leffler #include <netipsec/key_debug.h>
9388768458SSam Leffler 
9488768458SSam Leffler #include <netipsec/xform.h>
9588768458SSam Leffler 
9688768458SSam Leffler #include <machine/in_cksum.h>
9788768458SSam Leffler 
987aee3dd1SSam Leffler #include <opencrypto/cryptodev.h>
997aee3dd1SSam Leffler 
10088768458SSam Leffler #ifdef IPSEC_DEBUG
10188768458SSam Leffler int ipsec_debug = 1;
10288768458SSam Leffler #else
10388768458SSam Leffler int ipsec_debug = 0;
10488768458SSam Leffler #endif
10588768458SSam Leffler 
10688768458SSam Leffler /* NB: name changed so netstat doesn't use it */
1072cb64cb2SGeorge V. Neville-Neil struct ipsecstat ipsec4stat;
10888768458SSam Leffler int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
10988768458SSam Leffler int ip4_ipsec_dfbit = 0;	/* DF bit on encap. 0: clear 1: set 2: copy */
11088768458SSam Leffler int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
11188768458SSam Leffler int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
11288768458SSam Leffler int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
11388768458SSam Leffler int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
11488768458SSam Leffler struct secpolicy ip4_def_policy;
11588768458SSam Leffler int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
11688768458SSam Leffler int ip4_esp_randpad = -1;
11788768458SSam Leffler /*
11888768458SSam Leffler  * Crypto support requirements:
11988768458SSam Leffler  *
12088768458SSam Leffler  *  1	require hardware support
12188768458SSam Leffler  * -1	require software support
12288768458SSam Leffler  *  0	take anything
12388768458SSam Leffler  */
1246810ad6fSSam Leffler int	crypto_support = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
12588768458SSam Leffler 
12688768458SSam Leffler SYSCTL_DECL(_net_inet_ipsec);
12788768458SSam Leffler 
12888768458SSam Leffler /* net.inet.ipsec */
1298b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_POLICY,
1308b615593SMarko Zec 	def_policy, CTLFLAG_RW, ip4_def_policy.policy,  0,
131be6b1304STom Rhodes 	"IPsec default policy.");
1328b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV,
1338b615593SMarko Zec 	esp_trans_deflev, CTLFLAG_RW, ip4_esp_trans_deflev,	0,
1348b615593SMarko Zec 	"Default ESP transport mode level");
1358b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV,
1368b615593SMarko Zec 	esp_net_deflev, CTLFLAG_RW, ip4_esp_net_deflev,	0,
1378b615593SMarko Zec 	"Default ESP tunnel mode level.");
1388b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV,
1398b615593SMarko Zec 	ah_trans_deflev, CTLFLAG_RW, ip4_ah_trans_deflev,	0,
1408b615593SMarko Zec 	"AH transfer mode default level.");
1418b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV,
1428b615593SMarko Zec 	ah_net_deflev, CTLFLAG_RW, ip4_ah_net_deflev,	0,
1438b615593SMarko Zec 	"AH tunnel mode default level.");
1448b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
1458b615593SMarko Zec 	ah_cleartos, CTLFLAG_RW,	ah_cleartos,	0,
1463377c961STom Rhodes 	"If set clear type-of-service field when doing AH computation.");
1478b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
1488b615593SMarko Zec 	ah_offsetmask, CTLFLAG_RW,	ip4_ah_offsetmask,	0,
1493377c961STom Rhodes 	"If not set clear offset field mask when doing AH computation.");
1508b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DFBIT,
1518b615593SMarko Zec 	dfbit, CTLFLAG_RW,	ip4_ipsec_dfbit,	0,
1528b615593SMarko Zec 	"Do not fragment bit on encap.");
1538b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_ECN,
1548b615593SMarko Zec 	ecn, CTLFLAG_RW,	ip4_ipsec_ecn,	0,
155be6b1304STom Rhodes 	"Explicit Congestion Notification handling.");
1568b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEBUG,
1578b615593SMarko Zec 	debug, CTLFLAG_RW,	ipsec_debug,	0,
158be6b1304STom Rhodes 	"Enable IPsec debugging output when set.");
1598b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, OID_AUTO,
1608b615593SMarko Zec 	crypto_support,	CTLFLAG_RW,	crypto_support,0,
161be6b1304STom Rhodes 	"Crypto driver selection.");
1628b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ipsec, OID_AUTO,
1638b615593SMarko Zec 	ipsecstats,	CTLFLAG_RD,	ipsec4stat, ipsecstat,
1648b615593SMarko Zec 	"IPsec IPv4 statistics.");
16588768458SSam Leffler 
1666131838bSPawel Jakub Dawidek #ifdef REGRESSION
167dfa9422bSPawel Jakub Dawidek /*
168dfa9422bSPawel Jakub Dawidek  * When set to 1, IPsec will send packets with the same sequence number.
169dfa9422bSPawel Jakub Dawidek  * This allows to verify if the other side has proper replay attacks detection.
170dfa9422bSPawel Jakub Dawidek  */
171dfa9422bSPawel Jakub Dawidek int ipsec_replay = 0;
1728b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec,_net_inet_ipsec, OID_AUTO, test_replay,
1738b615593SMarko Zec 	CTLFLAG_RW, ipsec_replay, 0, "Emulate replay attack");
174dfa9422bSPawel Jakub Dawidek /*
175dfa9422bSPawel Jakub Dawidek  * When set 1, IPsec will send packets with corrupted HMAC.
176dfa9422bSPawel Jakub Dawidek  * This allows to verify if the other side properly detects modified packets.
177dfa9422bSPawel Jakub Dawidek  */
178dfa9422bSPawel Jakub Dawidek int ipsec_integrity = 0;
1798b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec,_net_inet_ipsec, OID_AUTO, test_integrity,
1808b615593SMarko Zec 	CTLFLAG_RW, ipsec_integrity, 0, "Emulate man-in-the-middle attack");
1816131838bSPawel Jakub Dawidek #endif
182dfa9422bSPawel Jakub Dawidek 
18388768458SSam Leffler #ifdef INET6
1842cb64cb2SGeorge V. Neville-Neil struct ipsecstat ipsec6stat;
18588768458SSam Leffler int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
18688768458SSam Leffler int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
18788768458SSam Leffler int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
18888768458SSam Leffler int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
18988768458SSam Leffler int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
19088768458SSam Leffler 
19188768458SSam Leffler SYSCTL_DECL(_net_inet6_ipsec6);
19288768458SSam Leffler 
19388768458SSam Leffler /* net.inet6.ipsec6 */
19488768458SSam Leffler #ifdef COMPAT_KAME
19588768458SSam Leffler SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
1963377c961STom Rhodes     0, 0, compat_ipsecstats_sysctl, "S", "IPsec IPv6 statistics.");
19788768458SSam Leffler #endif /* COMPAT_KAME */
1988b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
1998b615593SMarko Zec 	def_policy, CTLFLAG_RW,	ip4_def_policy.policy,	0,
2008b615593SMarko Zec 	"IPsec default policy.");
2018b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV,
2028b615593SMarko Zec 	esp_trans_deflev, CTLFLAG_RW, ip6_esp_trans_deflev,	0,
2038b615593SMarko Zec 	"Default ESP transport mode level.");
2048b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV,
2058b615593SMarko Zec 	esp_net_deflev, CTLFLAG_RW, ip6_esp_net_deflev,	0,
2068b615593SMarko Zec 	"Default ESP tunnel mode level.");
2078b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV,
2088b615593SMarko Zec 	ah_trans_deflev, CTLFLAG_RW, ip6_ah_trans_deflev,	0,
2098b615593SMarko Zec 	"AH transfer mode default level.");
2108b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV,
2118b615593SMarko Zec 	ah_net_deflev, CTLFLAG_RW, ip6_ah_net_deflev,	0,
2128b615593SMarko Zec 	"AH tunnel mode default level.");
2138b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_ECN,
2148b615593SMarko Zec 	ecn, CTLFLAG_RW, ip6_ipsec_ecn,	0,
2153377c961STom Rhodes 	"Explicit Congestion Notification handling.");
2168b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEBUG,
2178b615593SMarko Zec 	debug, CTLFLAG_RW,	ipsec_debug,	0,
218be6b1304STom Rhodes 	"Enable IPsec debugging output when set.");
2198b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_STATS,
2208b615593SMarko Zec 	ipsecstats, CTLFLAG_RD, ipsec6stat, ipsecstat,
2218b615593SMarko Zec 	"IPsec IPv6 statistics.");
22288768458SSam Leffler #endif /* INET6 */
22388768458SSam Leffler 
22488768458SSam Leffler static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
22588768458SSam Leffler #ifdef INET6
22688768458SSam Leffler static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct in6pcb *pcb));
22788768458SSam Leffler #endif
22888768458SSam Leffler static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
22988768458SSam Leffler static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
23088768458SSam Leffler static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
23188768458SSam Leffler #ifdef INET6
23288768458SSam Leffler static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
23388768458SSam Leffler static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
23488768458SSam Leffler #endif
23588768458SSam Leffler static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
23688768458SSam Leffler static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
23788768458SSam Leffler static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
238c26fe973SBjoern A. Zeeb 	int optname, caddr_t request, size_t len, struct ucred *cred));
23988768458SSam Leffler static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
24088768458SSam Leffler static void vshiftl __P((unsigned char *, int, int));
24188768458SSam Leffler static size_t ipsec_hdrsiz __P((struct secpolicy *));
24288768458SSam Leffler 
2436464079fSSam Leffler MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
2446464079fSSam Leffler 
24588768458SSam Leffler /*
24688768458SSam Leffler  * Return a held reference to the default SP.
24788768458SSam Leffler  */
24888768458SSam Leffler static struct secpolicy *
24988768458SSam Leffler key_allocsp_default(const char* where, int tag)
25088768458SSam Leffler {
2518b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
25288768458SSam Leffler 	struct secpolicy *sp;
25388768458SSam Leffler 
25488768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
25588768458SSam Leffler 		printf("DP key_allocsp_default from %s:%u\n", where, tag));
25688768458SSam Leffler 
257603724d3SBjoern A. Zeeb 	sp = &V_ip4_def_policy;
25888768458SSam Leffler 	if (sp->policy != IPSEC_POLICY_DISCARD &&
25988768458SSam Leffler 	    sp->policy != IPSEC_POLICY_NONE) {
26088768458SSam Leffler 		ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
26188768458SSam Leffler 		    sp->policy, IPSEC_POLICY_NONE));
26288768458SSam Leffler 		sp->policy = IPSEC_POLICY_NONE;
26388768458SSam Leffler 	}
264422e4f5bSSam Leffler 	key_addref(sp);
26588768458SSam Leffler 
26688768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
26788768458SSam Leffler 		printf("DP key_allocsp_default returns SP:%p (%u)\n",
26888768458SSam Leffler 			sp, sp->refcnt));
26988768458SSam Leffler 	return sp;
27088768458SSam Leffler }
27188768458SSam Leffler #define	KEY_ALLOCSP_DEFAULT() \
27288768458SSam Leffler 	key_allocsp_default(__FILE__, __LINE__)
27388768458SSam Leffler 
27488768458SSam Leffler /*
27588768458SSam Leffler  * For OUTBOUND packet having a socket. Searching SPD for packet,
27688768458SSam Leffler  * and return a pointer to SP.
27788768458SSam Leffler  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
27888768458SSam Leffler  *		0	: bypass
27988768458SSam Leffler  *		EACCES	: discard packet.
28088768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
28188768458SSam Leffler  *		others	: error occured.
28288768458SSam Leffler  *	others:	a pointer to SP
28388768458SSam Leffler  *
28488768458SSam Leffler  * NOTE: IPv6 mapped adddress concern is implemented here.
28588768458SSam Leffler  */
28688768458SSam Leffler struct secpolicy *
28788768458SSam Leffler ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
28888768458SSam Leffler {
28988768458SSam Leffler 	struct secpolicy *sp;
29088768458SSam Leffler 
2919ffa9677SSam Leffler 	IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
2929ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
2939ffa9677SSam Leffler 		("invalid direction %u", dir));
29488768458SSam Leffler 
29588768458SSam Leffler 	sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
29688768458SSam Leffler 	if (sp == NULL)			/*XXX????*/
29788768458SSam Leffler 		sp = KEY_ALLOCSP_DEFAULT();
2989ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL, ("null SP"));
29988768458SSam Leffler 	return sp;
30088768458SSam Leffler }
30188768458SSam Leffler 
30288768458SSam Leffler /*
30388768458SSam Leffler  * For OUTBOUND packet having a socket. Searching SPD for packet,
30488768458SSam Leffler  * and return a pointer to SP.
30588768458SSam Leffler  * OUT:	NULL:	no apropreate SP found, the following value is set to error.
30688768458SSam Leffler  *		0	: bypass
30788768458SSam Leffler  *		EACCES	: discard packet.
30888768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
30988768458SSam Leffler  *		others	: error occured.
31088768458SSam Leffler  *	others:	a pointer to SP
31188768458SSam Leffler  *
31288768458SSam Leffler  * NOTE: IPv6 mapped adddress concern is implemented here.
31388768458SSam Leffler  */
31488768458SSam Leffler struct secpolicy *
31588768458SSam Leffler ipsec_getpolicybysock(m, dir, inp, error)
31688768458SSam Leffler 	struct mbuf *m;
31788768458SSam Leffler 	u_int dir;
31888768458SSam Leffler 	struct inpcb *inp;
31988768458SSam Leffler 	int *error;
32088768458SSam Leffler {
3218b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
32288768458SSam Leffler 	struct inpcbpolicy *pcbsp = NULL;
32388768458SSam Leffler 	struct secpolicy *currsp = NULL;	/* policy on socket */
32488768458SSam Leffler 	struct secpolicy *sp;
32588768458SSam Leffler 
3269ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
3279ffa9677SSam Leffler 	IPSEC_ASSERT(inp != NULL, ("null inpcb"));
3289ffa9677SSam Leffler 	IPSEC_ASSERT(error != NULL, ("null error"));
3299ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
3309ffa9677SSam Leffler 		("invalid direction %u", dir));
33188768458SSam Leffler 
33288768458SSam Leffler 	/* set spidx in pcb */
3339c3309d1SJonathan Lemon 	if (inp->inp_vflag & INP_IPV6PROTO) {
334595064e8SSam Leffler #ifdef INET6
33588768458SSam Leffler 		*error = ipsec6_setspidx_in6pcb(m, inp);
33688768458SSam Leffler 		pcbsp = inp->in6p_sp;
337595064e8SSam Leffler #else
338595064e8SSam Leffler 		*error = EINVAL;		/* should not happen */
339595064e8SSam Leffler #endif
3409c3309d1SJonathan Lemon 	} else {
3419c3309d1SJonathan Lemon 		*error = ipsec4_setspidx_inpcb(m, inp);
3429c3309d1SJonathan Lemon 		pcbsp = inp->inp_sp;
34388768458SSam Leffler 	}
34488768458SSam Leffler 	if (*error)
34588768458SSam Leffler 		return NULL;
34688768458SSam Leffler 
3479ffa9677SSam Leffler 	IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
34888768458SSam Leffler 	switch (dir) {
34988768458SSam Leffler 	case IPSEC_DIR_INBOUND:
35088768458SSam Leffler 		currsp = pcbsp->sp_in;
35188768458SSam Leffler 		break;
35288768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
35388768458SSam Leffler 		currsp = pcbsp->sp_out;
35488768458SSam Leffler 		break;
35588768458SSam Leffler 	}
3569ffa9677SSam Leffler 	IPSEC_ASSERT(currsp != NULL, ("null currsp"));
35788768458SSam Leffler 
35888768458SSam Leffler 	if (pcbsp->priv) {			/* when privilieged socket */
35988768458SSam Leffler 		switch (currsp->policy) {
36088768458SSam Leffler 		case IPSEC_POLICY_BYPASS:
36188768458SSam Leffler 		case IPSEC_POLICY_IPSEC:
362422e4f5bSSam Leffler 			key_addref(currsp);
36388768458SSam Leffler 			sp = currsp;
36488768458SSam Leffler 			break;
36588768458SSam Leffler 
36688768458SSam Leffler 		case IPSEC_POLICY_ENTRUST:
36788768458SSam Leffler 			/* look for a policy in SPD */
36888768458SSam Leffler 			sp = KEY_ALLOCSP(&currsp->spidx, dir);
36988768458SSam Leffler 			if (sp == NULL)		/* no SP found */
37088768458SSam Leffler 				sp = KEY_ALLOCSP_DEFAULT();
37188768458SSam Leffler 			break;
37288768458SSam Leffler 
37388768458SSam Leffler 		default:
3749ffa9677SSam Leffler 			ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
3759ffa9677SSam Leffler 				__func__, currsp->policy));
37688768458SSam Leffler 			*error = EINVAL;
37788768458SSam Leffler 			return NULL;
37888768458SSam Leffler 		}
37988768458SSam Leffler 	} else {				/* unpriv, SPD has policy */
38088768458SSam Leffler 		sp = KEY_ALLOCSP(&currsp->spidx, dir);
38188768458SSam Leffler 		if (sp == NULL) {		/* no SP found */
38288768458SSam Leffler 			switch (currsp->policy) {
38388768458SSam Leffler 			case IPSEC_POLICY_BYPASS:
3849ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: Illegal policy for "
3859ffa9677SSam Leffler 					"non-priviliged defined %d\n",
3869ffa9677SSam Leffler 					__func__, currsp->policy));
38788768458SSam Leffler 				*error = EINVAL;
38888768458SSam Leffler 				return NULL;
38988768458SSam Leffler 
39088768458SSam Leffler 			case IPSEC_POLICY_ENTRUST:
39188768458SSam Leffler 				sp = KEY_ALLOCSP_DEFAULT();
39288768458SSam Leffler 				break;
39388768458SSam Leffler 
39488768458SSam Leffler 			case IPSEC_POLICY_IPSEC:
395422e4f5bSSam Leffler 				key_addref(currsp);
39688768458SSam Leffler 				sp = currsp;
39788768458SSam Leffler 				break;
39888768458SSam Leffler 
39988768458SSam Leffler 			default:
4009ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: Invalid policy for "
4019ffa9677SSam Leffler 					"PCB %d\n", __func__, currsp->policy));
40288768458SSam Leffler 				*error = EINVAL;
40388768458SSam Leffler 				return NULL;
40488768458SSam Leffler 			}
40588768458SSam Leffler 		}
40688768458SSam Leffler 	}
4079ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL,
4089ffa9677SSam Leffler 		("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
40988768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4109ffa9677SSam Leffler 		printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
4119ffa9677SSam Leffler 			__func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
41288768458SSam Leffler 	return sp;
41388768458SSam Leffler }
41488768458SSam Leffler 
41588768458SSam Leffler /*
41688768458SSam Leffler  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
41788768458SSam Leffler  * and return a pointer to SP.
41888768458SSam Leffler  * OUT:	positive: a pointer to the entry for security policy leaf matched.
41988768458SSam Leffler  *	NULL:	no apropreate SP found, the following value is set to error.
42088768458SSam Leffler  *		0	: bypass
42188768458SSam Leffler  *		EACCES	: discard packet.
42288768458SSam Leffler  *		ENOENT	: ipsec_acquire() in progress, maybe.
42388768458SSam Leffler  *		others	: error occured.
42488768458SSam Leffler  */
42588768458SSam Leffler struct secpolicy *
42688768458SSam Leffler ipsec_getpolicybyaddr(m, dir, flag, error)
42788768458SSam Leffler 	struct mbuf *m;
42888768458SSam Leffler 	u_int dir;
42988768458SSam Leffler 	int flag;
43088768458SSam Leffler 	int *error;
43188768458SSam Leffler {
4328b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
43388768458SSam Leffler 	struct secpolicyindex spidx;
43488768458SSam Leffler 	struct secpolicy *sp;
43588768458SSam Leffler 
4369ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
4379ffa9677SSam Leffler 	IPSEC_ASSERT(error != NULL, ("null error"));
4389ffa9677SSam Leffler 	IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
4399ffa9677SSam Leffler 		("invalid direction %u", dir));
44088768458SSam Leffler 
44188768458SSam Leffler 	sp = NULL;
44288768458SSam Leffler 	if (key_havesp(dir)) {
4439d5abbddSJens Schweikhardt 		/* Make an index to look for a policy. */
44488768458SSam Leffler 		*error = ipsec_setspidx(m, &spidx,
44588768458SSam Leffler 					(flag & IP_FORWARDING) ? 0 : 1);
44688768458SSam Leffler 		if (*error != 0) {
4479ffa9677SSam Leffler 			DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
4489ffa9677SSam Leffler 				__func__, dir, flag));
44988768458SSam Leffler 			return NULL;
45088768458SSam Leffler 		}
45188768458SSam Leffler 		spidx.dir = dir;
45288768458SSam Leffler 
45388768458SSam Leffler 		sp = KEY_ALLOCSP(&spidx, dir);
45488768458SSam Leffler 	}
45588768458SSam Leffler 	if (sp == NULL)			/* no SP found, use system default */
45688768458SSam Leffler 		sp = KEY_ALLOCSP_DEFAULT();
4579ffa9677SSam Leffler 	IPSEC_ASSERT(sp != NULL, ("null SP"));
45888768458SSam Leffler 	return sp;
45988768458SSam Leffler }
46088768458SSam Leffler 
46188768458SSam Leffler struct secpolicy *
46288768458SSam Leffler ipsec4_checkpolicy(m, dir, flag, error, inp)
46388768458SSam Leffler 	struct mbuf *m;
46488768458SSam Leffler 	u_int dir, flag;
46588768458SSam Leffler 	int *error;
46688768458SSam Leffler 	struct inpcb *inp;
46788768458SSam Leffler {
4688b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
46988768458SSam Leffler 	struct secpolicy *sp;
47088768458SSam Leffler 
47188768458SSam Leffler 	*error = 0;
47288768458SSam Leffler 	if (inp == NULL)
47388768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
47488768458SSam Leffler 	else
47588768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, inp, error);
47688768458SSam Leffler 	if (sp == NULL) {
4779ffa9677SSam Leffler 		IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
478603724d3SBjoern A. Zeeb 		V_ipsec4stat.ips_out_inval++;
47988768458SSam Leffler 		return NULL;
48088768458SSam Leffler 	}
4819ffa9677SSam Leffler 	IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
48288768458SSam Leffler 	switch (sp->policy) {
48388768458SSam Leffler 	case IPSEC_POLICY_ENTRUST:
48488768458SSam Leffler 	default:
4859ffa9677SSam Leffler 		printf("%s: invalid policy %u\n", __func__, sp->policy);
48688768458SSam Leffler 		/* fall thru... */
48788768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
488603724d3SBjoern A. Zeeb 		V_ipsec4stat.ips_out_polvio++;
48988768458SSam Leffler 		*error = -EINVAL;	/* packet is discarded by caller */
49088768458SSam Leffler 		break;
49188768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
49288768458SSam Leffler 	case IPSEC_POLICY_NONE:
49388768458SSam Leffler 		KEY_FREESP(&sp);
49488768458SSam Leffler 		sp = NULL;		/* NB: force NULL result */
49588768458SSam Leffler 		break;
49688768458SSam Leffler 	case IPSEC_POLICY_IPSEC:
49788768458SSam Leffler 		if (sp->req == NULL)	/* acquire an SA */
49888768458SSam Leffler 			*error = key_spdacquire(sp);
49988768458SSam Leffler 		break;
50088768458SSam Leffler 	}
50188768458SSam Leffler 	if (*error != 0) {
50288768458SSam Leffler 		KEY_FREESP(&sp);
50388768458SSam Leffler 		sp = NULL;
50488768458SSam Leffler 	}
50588768458SSam Leffler 	return sp;
50688768458SSam Leffler }
50788768458SSam Leffler 
50888768458SSam Leffler static int
50988768458SSam Leffler ipsec4_setspidx_inpcb(m, pcb)
51088768458SSam Leffler 	struct mbuf *m;
51188768458SSam Leffler 	struct inpcb *pcb;
51288768458SSam Leffler {
51388768458SSam Leffler 	int error;
51488768458SSam Leffler 
5159ffa9677SSam Leffler 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
5169ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
5179ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
5189ffa9677SSam Leffler 		("null sp_in || sp_out"));
51988768458SSam Leffler 
52088768458SSam Leffler 	error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
52188768458SSam Leffler 	if (error == 0) {
52288768458SSam Leffler 		pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
52388768458SSam Leffler 		pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
52488768458SSam Leffler 		pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
52588768458SSam Leffler 	} else {
52688768458SSam Leffler 		bzero(&pcb->inp_sp->sp_in->spidx,
52788768458SSam Leffler 			sizeof (pcb->inp_sp->sp_in->spidx));
52888768458SSam Leffler 		bzero(&pcb->inp_sp->sp_out->spidx,
52988768458SSam Leffler 			sizeof (pcb->inp_sp->sp_in->spidx));
53088768458SSam Leffler 	}
53188768458SSam Leffler 	return error;
53288768458SSam Leffler }
53388768458SSam Leffler 
53488768458SSam Leffler #ifdef INET6
53588768458SSam Leffler static int
53688768458SSam Leffler ipsec6_setspidx_in6pcb(m, pcb)
53788768458SSam Leffler 	struct mbuf *m;
53888768458SSam Leffler 	struct in6pcb *pcb;
53988768458SSam Leffler {
5408b615593SMarko Zec 	//INIT_VNET_IPSEC(curvnet);
54188768458SSam Leffler 	struct secpolicyindex *spidx;
54288768458SSam Leffler 	int error;
54388768458SSam Leffler 
5449ffa9677SSam Leffler 	IPSEC_ASSERT(pcb != NULL, ("null pcb"));
5459ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->in6p_sp != NULL, ("null inp_sp"));
5469ffa9677SSam Leffler 	IPSEC_ASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
5479ffa9677SSam Leffler 		("null sp_in || sp_out"));
54888768458SSam Leffler 
54988768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
55088768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
55188768458SSam Leffler 
55288768458SSam Leffler 	spidx = &pcb->in6p_sp->sp_in->spidx;
55388768458SSam Leffler 	error = ipsec_setspidx(m, spidx, 1);
55488768458SSam Leffler 	if (error)
55588768458SSam Leffler 		goto bad;
55688768458SSam Leffler 	spidx->dir = IPSEC_DIR_INBOUND;
55788768458SSam Leffler 
55888768458SSam Leffler 	spidx = &pcb->in6p_sp->sp_out->spidx;
55988768458SSam Leffler 	error = ipsec_setspidx(m, spidx, 1);
56088768458SSam Leffler 	if (error)
56188768458SSam Leffler 		goto bad;
56288768458SSam Leffler 	spidx->dir = IPSEC_DIR_OUTBOUND;
56388768458SSam Leffler 
56488768458SSam Leffler 	return 0;
56588768458SSam Leffler 
56688768458SSam Leffler bad:
56788768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
56888768458SSam Leffler 	bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
56988768458SSam Leffler 	return error;
57088768458SSam Leffler }
57188768458SSam Leffler #endif
57288768458SSam Leffler 
57388768458SSam Leffler /*
57488768458SSam Leffler  * configure security policy index (src/dst/proto/sport/dport)
57588768458SSam Leffler  * by looking at the content of mbuf.
57688768458SSam Leffler  * the caller is responsible for error recovery (like clearing up spidx).
57788768458SSam Leffler  */
57888768458SSam Leffler static int
57988768458SSam Leffler ipsec_setspidx(m, spidx, needport)
58088768458SSam Leffler 	struct mbuf *m;
58188768458SSam Leffler 	struct secpolicyindex *spidx;
58288768458SSam Leffler 	int needport;
58388768458SSam Leffler {
5848b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
58588768458SSam Leffler 	struct ip *ip = NULL;
58688768458SSam Leffler 	struct ip ipbuf;
58788768458SSam Leffler 	u_int v;
58888768458SSam Leffler 	struct mbuf *n;
58988768458SSam Leffler 	int len;
59088768458SSam Leffler 	int error;
59188768458SSam Leffler 
5929ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
59388768458SSam Leffler 
59488768458SSam Leffler 	/*
59588768458SSam Leffler 	 * validate m->m_pkthdr.len.  we see incorrect length if we
59688768458SSam Leffler 	 * mistakenly call this function with inconsistent mbuf chain
59788768458SSam Leffler 	 * (like 4.4BSD tcp/udp processing).  XXX should we panic here?
59888768458SSam Leffler 	 */
59988768458SSam Leffler 	len = 0;
60088768458SSam Leffler 	for (n = m; n; n = n->m_next)
60188768458SSam Leffler 		len += n->m_len;
60288768458SSam Leffler 	if (m->m_pkthdr.len != len) {
60388768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6049ffa9677SSam Leffler 			printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
6059ffa9677SSam Leffler 				__func__, len, m->m_pkthdr.len));
60688768458SSam Leffler 		return EINVAL;
60788768458SSam Leffler 	}
60888768458SSam Leffler 
60988768458SSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ip)) {
61088768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6119ffa9677SSam Leffler 			printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
6129ffa9677SSam Leffler 			    __func__, m->m_pkthdr.len));
61388768458SSam Leffler 		return EINVAL;
61488768458SSam Leffler 	}
61588768458SSam Leffler 
61688768458SSam Leffler 	if (m->m_len >= sizeof(*ip))
61788768458SSam Leffler 		ip = mtod(m, struct ip *);
61888768458SSam Leffler 	else {
61988768458SSam Leffler 		m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
62088768458SSam Leffler 		ip = &ipbuf;
62188768458SSam Leffler 	}
62288768458SSam Leffler #ifdef _IP_VHL
62388768458SSam Leffler 	v = _IP_VHL_V(ip->ip_vhl);
62488768458SSam Leffler #else
62588768458SSam Leffler 	v = ip->ip_v;
62688768458SSam Leffler #endif
62788768458SSam Leffler 	switch (v) {
62888768458SSam Leffler 	case 4:
62988768458SSam Leffler 		error = ipsec4_setspidx_ipaddr(m, spidx);
63088768458SSam Leffler 		if (error)
63188768458SSam Leffler 			return error;
63288768458SSam Leffler 		ipsec4_get_ulp(m, spidx, needport);
63388768458SSam Leffler 		return 0;
63488768458SSam Leffler #ifdef INET6
63588768458SSam Leffler 	case 6:
63688768458SSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
63788768458SSam Leffler 			KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6389ffa9677SSam Leffler 				printf("%s: pkthdr len(%d) too small (v6), "
6399ffa9677SSam Leffler 				"ignored\n", __func__, m->m_pkthdr.len));
64088768458SSam Leffler 			return EINVAL;
64188768458SSam Leffler 		}
64288768458SSam Leffler 		error = ipsec6_setspidx_ipaddr(m, spidx);
64388768458SSam Leffler 		if (error)
64488768458SSam Leffler 			return error;
64588768458SSam Leffler 		ipsec6_get_ulp(m, spidx, needport);
64688768458SSam Leffler 		return 0;
64788768458SSam Leffler #endif
64888768458SSam Leffler 	default:
64988768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
6509ffa9677SSam Leffler 			printf("%s: " "unknown IP version %u, ignored.\n",
6519ffa9677SSam Leffler 				__func__, v));
65288768458SSam Leffler 		return EINVAL;
65388768458SSam Leffler 	}
65488768458SSam Leffler }
65588768458SSam Leffler 
65688768458SSam Leffler static void
65788768458SSam Leffler ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
65888768458SSam Leffler {
65988768458SSam Leffler 	u_int8_t nxt;
66088768458SSam Leffler 	int off;
66188768458SSam Leffler 
66288768458SSam Leffler 	/* sanity check */
6639ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
6649ffa9677SSam Leffler 	IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
66588768458SSam Leffler 
66688768458SSam Leffler 	/* NB: ip_input() flips it into host endian XXX need more checking */
66788768458SSam Leffler 	if (m->m_len < sizeof (struct ip)) {
66888768458SSam Leffler 		struct ip *ip = mtod(m, struct ip *);
66988768458SSam Leffler 		if (ip->ip_off & (IP_MF | IP_OFFMASK))
67088768458SSam Leffler 			goto done;
67188768458SSam Leffler #ifdef _IP_VHL
67288768458SSam Leffler 		off = _IP_VHL_HL(ip->ip_vhl) << 2;
67388768458SSam Leffler #else
67488768458SSam Leffler 		off = ip->ip_hl << 2;
67588768458SSam Leffler #endif
67688768458SSam Leffler 		nxt = ip->ip_p;
67788768458SSam Leffler 	} else {
67888768458SSam Leffler 		struct ip ih;
67988768458SSam Leffler 
68088768458SSam Leffler 		m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
68188768458SSam Leffler 		if (ih.ip_off & (IP_MF | IP_OFFMASK))
68288768458SSam Leffler 			goto done;
68388768458SSam Leffler #ifdef _IP_VHL
68488768458SSam Leffler 		off = _IP_VHL_HL(ih.ip_vhl) << 2;
68588768458SSam Leffler #else
68688768458SSam Leffler 		off = ih.ip_hl << 2;
68788768458SSam Leffler #endif
68888768458SSam Leffler 		nxt = ih.ip_p;
68988768458SSam Leffler 	}
69088768458SSam Leffler 
69188768458SSam Leffler 	while (off < m->m_pkthdr.len) {
69288768458SSam Leffler 		struct ip6_ext ip6e;
69388768458SSam Leffler 		struct tcphdr th;
69488768458SSam Leffler 		struct udphdr uh;
69588768458SSam Leffler 
69688768458SSam Leffler 		switch (nxt) {
69788768458SSam Leffler 		case IPPROTO_TCP:
69888768458SSam Leffler 			spidx->ul_proto = nxt;
69988768458SSam Leffler 			if (!needport)
70088768458SSam Leffler 				goto done_proto;
70188768458SSam Leffler 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
70288768458SSam Leffler 				goto done;
70388768458SSam Leffler 			m_copydata(m, off, sizeof (th), (caddr_t) &th);
70488768458SSam Leffler 			spidx->src.sin.sin_port = th.th_sport;
70588768458SSam Leffler 			spidx->dst.sin.sin_port = th.th_dport;
70688768458SSam Leffler 			return;
70788768458SSam Leffler 		case IPPROTO_UDP:
70888768458SSam Leffler 			spidx->ul_proto = nxt;
70988768458SSam Leffler 			if (!needport)
71088768458SSam Leffler 				goto done_proto;
71188768458SSam Leffler 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
71288768458SSam Leffler 				goto done;
71388768458SSam Leffler 			m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
71488768458SSam Leffler 			spidx->src.sin.sin_port = uh.uh_sport;
71588768458SSam Leffler 			spidx->dst.sin.sin_port = uh.uh_dport;
71688768458SSam Leffler 			return;
71788768458SSam Leffler 		case IPPROTO_AH:
718afa3570dSSam Leffler 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
71988768458SSam Leffler 				goto done;
72088768458SSam Leffler 			/* XXX sigh, this works but is totally bogus */
72188768458SSam Leffler 			m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
72288768458SSam Leffler 			off += (ip6e.ip6e_len + 2) << 2;
72388768458SSam Leffler 			nxt = ip6e.ip6e_nxt;
72488768458SSam Leffler 			break;
72588768458SSam Leffler 		case IPPROTO_ICMP:
72688768458SSam Leffler 		default:
72788768458SSam Leffler 			/* XXX intermediate headers??? */
72888768458SSam Leffler 			spidx->ul_proto = nxt;
72988768458SSam Leffler 			goto done_proto;
73088768458SSam Leffler 		}
73188768458SSam Leffler 	}
73288768458SSam Leffler done:
73388768458SSam Leffler 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
73488768458SSam Leffler done_proto:
73588768458SSam Leffler 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
73688768458SSam Leffler 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
73788768458SSam Leffler }
73888768458SSam Leffler 
73988768458SSam Leffler /* assumes that m is sane */
74088768458SSam Leffler static int
74188768458SSam Leffler ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
74288768458SSam Leffler {
74388768458SSam Leffler 	static const struct sockaddr_in template = {
74488768458SSam Leffler 		sizeof (struct sockaddr_in),
74588768458SSam Leffler 		AF_INET,
74688768458SSam Leffler 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
74788768458SSam Leffler 	};
74888768458SSam Leffler 
74988768458SSam Leffler 	spidx->src.sin = template;
75088768458SSam Leffler 	spidx->dst.sin = template;
75188768458SSam Leffler 
75288768458SSam Leffler 	if (m->m_len < sizeof (struct ip)) {
75388768458SSam Leffler 		m_copydata(m, offsetof(struct ip, ip_src),
75488768458SSam Leffler 			   sizeof (struct  in_addr),
75588768458SSam Leffler 			   (caddr_t) &spidx->src.sin.sin_addr);
75688768458SSam Leffler 		m_copydata(m, offsetof(struct ip, ip_dst),
75788768458SSam Leffler 			   sizeof (struct  in_addr),
75888768458SSam Leffler 			   (caddr_t) &spidx->dst.sin.sin_addr);
75988768458SSam Leffler 	} else {
76088768458SSam Leffler 		struct ip *ip = mtod(m, struct ip *);
76188768458SSam Leffler 		spidx->src.sin.sin_addr = ip->ip_src;
76288768458SSam Leffler 		spidx->dst.sin.sin_addr = ip->ip_dst;
76388768458SSam Leffler 	}
76488768458SSam Leffler 
76588768458SSam Leffler 	spidx->prefs = sizeof(struct in_addr) << 3;
76688768458SSam Leffler 	spidx->prefd = sizeof(struct in_addr) << 3;
76788768458SSam Leffler 
76888768458SSam Leffler 	return 0;
76988768458SSam Leffler }
77088768458SSam Leffler 
77188768458SSam Leffler #ifdef INET6
77288768458SSam Leffler static void
77388768458SSam Leffler ipsec6_get_ulp(m, spidx, needport)
77488768458SSam Leffler 	struct mbuf *m;
77588768458SSam Leffler 	struct secpolicyindex *spidx;
77688768458SSam Leffler 	int needport;
77788768458SSam Leffler {
7788b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
77988768458SSam Leffler 	int off, nxt;
78088768458SSam Leffler 	struct tcphdr th;
78188768458SSam Leffler 	struct udphdr uh;
7820e3c2be4SBjoern A. Zeeb 	struct icmp6_hdr ih;
78388768458SSam Leffler 
78488768458SSam Leffler 	/* sanity check */
78588768458SSam Leffler 	if (m == NULL)
7869ffa9677SSam Leffler 		panic("%s: NULL pointer was passed.\n", __func__);
78788768458SSam Leffler 
78888768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
7899ffa9677SSam Leffler 		printf("%s:\n", __func__); kdebug_mbuf(m));
79088768458SSam Leffler 
79188768458SSam Leffler 	/* set default */
79288768458SSam Leffler 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
79388768458SSam Leffler 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
79488768458SSam Leffler 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
79588768458SSam Leffler 
79688768458SSam Leffler 	nxt = -1;
79788768458SSam Leffler 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
79888768458SSam Leffler 	if (off < 0 || m->m_pkthdr.len < off)
79988768458SSam Leffler 		return;
80088768458SSam Leffler 
80188768458SSam Leffler 	switch (nxt) {
80288768458SSam Leffler 	case IPPROTO_TCP:
80388768458SSam Leffler 		spidx->ul_proto = nxt;
80488768458SSam Leffler 		if (!needport)
80588768458SSam Leffler 			break;
80688768458SSam Leffler 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
80788768458SSam Leffler 			break;
80888768458SSam Leffler 		m_copydata(m, off, sizeof(th), (caddr_t)&th);
80988768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
81088768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
81188768458SSam Leffler 		break;
81288768458SSam Leffler 	case IPPROTO_UDP:
81388768458SSam Leffler 		spidx->ul_proto = nxt;
81488768458SSam Leffler 		if (!needport)
81588768458SSam Leffler 			break;
81688768458SSam Leffler 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
81788768458SSam Leffler 			break;
81888768458SSam Leffler 		m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
81988768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
82088768458SSam Leffler 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
82188768458SSam Leffler 		break;
82288768458SSam Leffler 	case IPPROTO_ICMPV6:
8230e3c2be4SBjoern A. Zeeb 		spidx->ul_proto = nxt;
8240e3c2be4SBjoern A. Zeeb 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
8250e3c2be4SBjoern A. Zeeb 			break;
8260e3c2be4SBjoern A. Zeeb 		m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
8270e3c2be4SBjoern A. Zeeb 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
8280e3c2be4SBjoern A. Zeeb 		    htons((uint16_t)ih.icmp6_type);
8290e3c2be4SBjoern A. Zeeb 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
8300e3c2be4SBjoern A. Zeeb 		    htons((uint16_t)ih.icmp6_code);
8310e3c2be4SBjoern A. Zeeb 		break;
83288768458SSam Leffler 	default:
83388768458SSam Leffler 		/* XXX intermediate headers??? */
83488768458SSam Leffler 		spidx->ul_proto = nxt;
83588768458SSam Leffler 		break;
83688768458SSam Leffler 	}
83788768458SSam Leffler }
83888768458SSam Leffler 
83988768458SSam Leffler /* assumes that m is sane */
84088768458SSam Leffler static int
84188768458SSam Leffler ipsec6_setspidx_ipaddr(m, spidx)
84288768458SSam Leffler 	struct mbuf *m;
84388768458SSam Leffler 	struct secpolicyindex *spidx;
84488768458SSam Leffler {
84588768458SSam Leffler 	struct ip6_hdr *ip6 = NULL;
84688768458SSam Leffler 	struct ip6_hdr ip6buf;
84788768458SSam Leffler 	struct sockaddr_in6 *sin6;
84888768458SSam Leffler 
84988768458SSam Leffler 	if (m->m_len >= sizeof(*ip6))
85088768458SSam Leffler 		ip6 = mtod(m, struct ip6_hdr *);
85188768458SSam Leffler 	else {
85288768458SSam Leffler 		m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
85388768458SSam Leffler 		ip6 = &ip6buf;
85488768458SSam Leffler 	}
85588768458SSam Leffler 
85688768458SSam Leffler 	sin6 = (struct sockaddr_in6 *)&spidx->src;
85788768458SSam Leffler 	bzero(sin6, sizeof(*sin6));
85888768458SSam Leffler 	sin6->sin6_family = AF_INET6;
85988768458SSam Leffler 	sin6->sin6_len = sizeof(struct sockaddr_in6);
86088768458SSam Leffler 	bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
86188768458SSam Leffler 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
86288768458SSam Leffler 		sin6->sin6_addr.s6_addr16[1] = 0;
86388768458SSam Leffler 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
86488768458SSam Leffler 	}
86588768458SSam Leffler 	spidx->prefs = sizeof(struct in6_addr) << 3;
86688768458SSam Leffler 
86788768458SSam Leffler 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
86888768458SSam Leffler 	bzero(sin6, sizeof(*sin6));
86988768458SSam Leffler 	sin6->sin6_family = AF_INET6;
87088768458SSam Leffler 	sin6->sin6_len = sizeof(struct sockaddr_in6);
87188768458SSam Leffler 	bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
87288768458SSam Leffler 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
87388768458SSam Leffler 		sin6->sin6_addr.s6_addr16[1] = 0;
87488768458SSam Leffler 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
87588768458SSam Leffler 	}
87688768458SSam Leffler 	spidx->prefd = sizeof(struct in6_addr) << 3;
87788768458SSam Leffler 
87888768458SSam Leffler 	return 0;
87988768458SSam Leffler }
88088768458SSam Leffler #endif
88188768458SSam Leffler 
88288768458SSam Leffler static void
88388768458SSam Leffler ipsec_delpcbpolicy(p)
88488768458SSam Leffler 	struct inpcbpolicy *p;
88588768458SSam Leffler {
8866464079fSSam Leffler 	free(p, M_IPSEC_INPCB);
88788768458SSam Leffler }
88888768458SSam Leffler 
88988768458SSam Leffler /* initialize policy in PCB */
89088768458SSam Leffler int
89188768458SSam Leffler ipsec_init_policy(so, pcb_sp)
89288768458SSam Leffler 	struct socket *so;
89388768458SSam Leffler 	struct inpcbpolicy **pcb_sp;
89488768458SSam Leffler {
8958b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
89688768458SSam Leffler 	struct inpcbpolicy *new;
89788768458SSam Leffler 
89888768458SSam Leffler 	/* sanity check. */
89988768458SSam Leffler 	if (so == NULL || pcb_sp == NULL)
9009ffa9677SSam Leffler 		panic("%s: NULL pointer was passed.\n", __func__);
90188768458SSam Leffler 
90288768458SSam Leffler 	new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
9036464079fSSam Leffler 					    M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
90488768458SSam Leffler 	if (new == NULL) {
9059ffa9677SSam Leffler 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
90688768458SSam Leffler 		return ENOBUFS;
90788768458SSam Leffler 	}
90888768458SSam Leffler 
9099ffa9677SSam Leffler 	new->priv = IPSEC_IS_PRIVILEGED_SO(so);
91088768458SSam Leffler 
91188768458SSam Leffler 	if ((new->sp_in = KEY_NEWSP()) == NULL) {
91288768458SSam Leffler 		ipsec_delpcbpolicy(new);
91388768458SSam Leffler 		return ENOBUFS;
91488768458SSam Leffler 	}
91588768458SSam Leffler 	new->sp_in->state = IPSEC_SPSTATE_ALIVE;
91688768458SSam Leffler 	new->sp_in->policy = IPSEC_POLICY_ENTRUST;
91788768458SSam Leffler 
91888768458SSam Leffler 	if ((new->sp_out = KEY_NEWSP()) == NULL) {
91988768458SSam Leffler 		KEY_FREESP(&new->sp_in);
92088768458SSam Leffler 		ipsec_delpcbpolicy(new);
92188768458SSam Leffler 		return ENOBUFS;
92288768458SSam Leffler 	}
92388768458SSam Leffler 	new->sp_out->state = IPSEC_SPSTATE_ALIVE;
92488768458SSam Leffler 	new->sp_out->policy = IPSEC_POLICY_ENTRUST;
92588768458SSam Leffler 
92688768458SSam Leffler 	*pcb_sp = new;
92788768458SSam Leffler 
92888768458SSam Leffler 	return 0;
92988768458SSam Leffler }
93088768458SSam Leffler 
93188768458SSam Leffler /* copy old ipsec policy into new */
93288768458SSam Leffler int
93388768458SSam Leffler ipsec_copy_policy(old, new)
93488768458SSam Leffler 	struct inpcbpolicy *old, *new;
93588768458SSam Leffler {
93688768458SSam Leffler 	struct secpolicy *sp;
93788768458SSam Leffler 
93888768458SSam Leffler 	sp = ipsec_deepcopy_policy(old->sp_in);
93988768458SSam Leffler 	if (sp) {
94088768458SSam Leffler 		KEY_FREESP(&new->sp_in);
94188768458SSam Leffler 		new->sp_in = sp;
94288768458SSam Leffler 	} else
94388768458SSam Leffler 		return ENOBUFS;
94488768458SSam Leffler 
94588768458SSam Leffler 	sp = ipsec_deepcopy_policy(old->sp_out);
94688768458SSam Leffler 	if (sp) {
94788768458SSam Leffler 		KEY_FREESP(&new->sp_out);
94888768458SSam Leffler 		new->sp_out = sp;
94988768458SSam Leffler 	} else
95088768458SSam Leffler 		return ENOBUFS;
95188768458SSam Leffler 
95288768458SSam Leffler 	new->priv = old->priv;
95388768458SSam Leffler 
95488768458SSam Leffler 	return 0;
95588768458SSam Leffler }
95688768458SSam Leffler 
9576464079fSSam Leffler struct ipsecrequest *
9586464079fSSam Leffler ipsec_newisr(void)
9596464079fSSam Leffler {
9606464079fSSam Leffler 	struct ipsecrequest *p;
9616464079fSSam Leffler 
9626464079fSSam Leffler 	p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
9636464079fSSam Leffler 	if (p != NULL)
9649ffa9677SSam Leffler 		IPSECREQUEST_LOCK_INIT(p);
9656464079fSSam Leffler 	return p;
9666464079fSSam Leffler }
9676464079fSSam Leffler 
9686464079fSSam Leffler void
9696464079fSSam Leffler ipsec_delisr(struct ipsecrequest *p)
9706464079fSSam Leffler {
9719ffa9677SSam Leffler 	IPSECREQUEST_LOCK_DESTROY(p);
9726464079fSSam Leffler 	free(p, M_IPSEC_SR);
9736464079fSSam Leffler }
9746464079fSSam Leffler 
97588768458SSam Leffler /* deep-copy a policy in PCB */
97688768458SSam Leffler static struct secpolicy *
97788768458SSam Leffler ipsec_deepcopy_policy(src)
97888768458SSam Leffler 	struct secpolicy *src;
97988768458SSam Leffler {
98088768458SSam Leffler 	struct ipsecrequest *newchain = NULL;
98188768458SSam Leffler 	struct ipsecrequest *p;
98288768458SSam Leffler 	struct ipsecrequest **q;
98388768458SSam Leffler 	struct ipsecrequest *r;
98488768458SSam Leffler 	struct secpolicy *dst;
98588768458SSam Leffler 
98688768458SSam Leffler 	if (src == NULL)
98788768458SSam Leffler 		return NULL;
98888768458SSam Leffler 	dst = KEY_NEWSP();
98988768458SSam Leffler 	if (dst == NULL)
99088768458SSam Leffler 		return NULL;
99188768458SSam Leffler 
99288768458SSam Leffler 	/*
99388768458SSam Leffler 	 * deep-copy IPsec request chain.  This is required since struct
99488768458SSam Leffler 	 * ipsecrequest is not reference counted.
99588768458SSam Leffler 	 */
99688768458SSam Leffler 	q = &newchain;
99788768458SSam Leffler 	for (p = src->req; p; p = p->next) {
9986464079fSSam Leffler 		*q = ipsec_newisr();
99988768458SSam Leffler 		if (*q == NULL)
100088768458SSam Leffler 			goto fail;
100188768458SSam Leffler 		(*q)->saidx.proto = p->saidx.proto;
100288768458SSam Leffler 		(*q)->saidx.mode = p->saidx.mode;
100388768458SSam Leffler 		(*q)->level = p->level;
100488768458SSam Leffler 		(*q)->saidx.reqid = p->saidx.reqid;
100588768458SSam Leffler 
100688768458SSam Leffler 		bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
100788768458SSam Leffler 		bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
100888768458SSam Leffler 
100988768458SSam Leffler 		(*q)->sp = dst;
101088768458SSam Leffler 
101188768458SSam Leffler 		q = &((*q)->next);
101288768458SSam Leffler 	}
101388768458SSam Leffler 
101488768458SSam Leffler 	dst->req = newchain;
101588768458SSam Leffler 	dst->state = src->state;
101688768458SSam Leffler 	dst->policy = src->policy;
101788768458SSam Leffler 	/* do not touch the refcnt fields */
101888768458SSam Leffler 
101988768458SSam Leffler 	return dst;
102088768458SSam Leffler 
102188768458SSam Leffler fail:
102288768458SSam Leffler 	for (p = newchain; p; p = r) {
102388768458SSam Leffler 		r = p->next;
10246464079fSSam Leffler 		ipsec_delisr(p);
102588768458SSam Leffler 		p = NULL;
102688768458SSam Leffler 	}
102788768458SSam Leffler 	return NULL;
102888768458SSam Leffler }
102988768458SSam Leffler 
103088768458SSam Leffler /* set policy and ipsec request if present. */
103188768458SSam Leffler static int
1032c26fe973SBjoern A. Zeeb ipsec_set_policy(pcb_sp, optname, request, len, cred)
103388768458SSam Leffler 	struct secpolicy **pcb_sp;
103488768458SSam Leffler 	int optname;
103588768458SSam Leffler 	caddr_t request;
103688768458SSam Leffler 	size_t len;
1037c26fe973SBjoern A. Zeeb 	struct ucred *cred;
103888768458SSam Leffler {
10398b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
104088768458SSam Leffler 	struct sadb_x_policy *xpl;
104188768458SSam Leffler 	struct secpolicy *newsp = NULL;
104288768458SSam Leffler 	int error;
104388768458SSam Leffler 
104488768458SSam Leffler 	/* sanity check. */
104588768458SSam Leffler 	if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
104688768458SSam Leffler 		return EINVAL;
104788768458SSam Leffler 	if (len < sizeof(*xpl))
104888768458SSam Leffler 		return EINVAL;
104988768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
105088768458SSam Leffler 
105188768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
10529ffa9677SSam Leffler 		printf("%s: passed policy\n", __func__);
105388768458SSam Leffler 		kdebug_sadb_x_policy((struct sadb_ext *)xpl));
105488768458SSam Leffler 
105588768458SSam Leffler 	/* check policy type */
105688768458SSam Leffler 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
105788768458SSam Leffler 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
105888768458SSam Leffler 	 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
105988768458SSam Leffler 		return EINVAL;
106088768458SSam Leffler 
106188768458SSam Leffler 	/* check privileged socket */
1062c26fe973SBjoern A. Zeeb 	if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1063c26fe973SBjoern A. Zeeb 		error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1064c26fe973SBjoern A. Zeeb 		if (error)
106588768458SSam Leffler 			return EACCES;
1066c26fe973SBjoern A. Zeeb 	}
106788768458SSam Leffler 
106888768458SSam Leffler 	/* allocation new SP entry */
106988768458SSam Leffler 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
107088768458SSam Leffler 		return error;
107188768458SSam Leffler 
107288768458SSam Leffler 	newsp->state = IPSEC_SPSTATE_ALIVE;
107388768458SSam Leffler 
107488768458SSam Leffler 	/* clear old SP and set new SP */
107588768458SSam Leffler 	KEY_FREESP(pcb_sp);
107688768458SSam Leffler 	*pcb_sp = newsp;
107788768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
10789ffa9677SSam Leffler 		printf("%s: new policy\n", __func__);
107988768458SSam Leffler 		kdebug_secpolicy(newsp));
108088768458SSam Leffler 
108188768458SSam Leffler 	return 0;
108288768458SSam Leffler }
108388768458SSam Leffler 
108488768458SSam Leffler static int
108588768458SSam Leffler ipsec_get_policy(pcb_sp, mp)
108688768458SSam Leffler 	struct secpolicy *pcb_sp;
108788768458SSam Leffler 	struct mbuf **mp;
108888768458SSam Leffler {
10898b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
109088768458SSam Leffler 
109188768458SSam Leffler 	/* sanity check. */
109288768458SSam Leffler 	if (pcb_sp == NULL || mp == NULL)
109388768458SSam Leffler 		return EINVAL;
109488768458SSam Leffler 
109588768458SSam Leffler 	*mp = key_sp2msg(pcb_sp);
109688768458SSam Leffler 	if (!*mp) {
10979ffa9677SSam Leffler 		ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
109888768458SSam Leffler 		return ENOBUFS;
109988768458SSam Leffler 	}
110088768458SSam Leffler 
110188768458SSam Leffler 	(*mp)->m_type = MT_DATA;
110288768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
11039ffa9677SSam Leffler 		printf("%s:\n", __func__); kdebug_mbuf(*mp));
110488768458SSam Leffler 
110588768458SSam Leffler 	return 0;
110688768458SSam Leffler }
110788768458SSam Leffler 
110888768458SSam Leffler int
1109c26fe973SBjoern A. Zeeb ipsec4_set_policy(inp, optname, request, len, cred)
111088768458SSam Leffler 	struct inpcb *inp;
111188768458SSam Leffler 	int optname;
111288768458SSam Leffler 	caddr_t request;
111388768458SSam Leffler 	size_t len;
1114c26fe973SBjoern A. Zeeb 	struct ucred *cred;
111588768458SSam Leffler {
11168b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
111788768458SSam Leffler 	struct sadb_x_policy *xpl;
111888768458SSam Leffler 	struct secpolicy **pcb_sp;
111988768458SSam Leffler 
112088768458SSam Leffler 	/* sanity check. */
112188768458SSam Leffler 	if (inp == NULL || request == NULL)
112288768458SSam Leffler 		return EINVAL;
112388768458SSam Leffler 	if (len < sizeof(*xpl))
112488768458SSam Leffler 		return EINVAL;
112588768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
112688768458SSam Leffler 
112788768458SSam Leffler 	/* select direction */
112888768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
112988768458SSam Leffler 	case IPSEC_DIR_INBOUND:
113088768458SSam Leffler 		pcb_sp = &inp->inp_sp->sp_in;
113188768458SSam Leffler 		break;
113288768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
113388768458SSam Leffler 		pcb_sp = &inp->inp_sp->sp_out;
113488768458SSam Leffler 		break;
113588768458SSam Leffler 	default:
11369ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
113788768458SSam Leffler 			xpl->sadb_x_policy_dir));
113888768458SSam Leffler 		return EINVAL;
113988768458SSam Leffler 	}
114088768458SSam Leffler 
1141c26fe973SBjoern A. Zeeb 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
114288768458SSam Leffler }
114388768458SSam Leffler 
114488768458SSam Leffler int
114588768458SSam Leffler ipsec4_get_policy(inp, request, len, mp)
114688768458SSam Leffler 	struct inpcb *inp;
114788768458SSam Leffler 	caddr_t request;
114888768458SSam Leffler 	size_t len;
114988768458SSam Leffler 	struct mbuf **mp;
115088768458SSam Leffler {
11518b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
115288768458SSam Leffler 	struct sadb_x_policy *xpl;
115388768458SSam Leffler 	struct secpolicy *pcb_sp;
115488768458SSam Leffler 
115588768458SSam Leffler 	/* sanity check. */
115688768458SSam Leffler 	if (inp == NULL || request == NULL || mp == NULL)
115788768458SSam Leffler 		return EINVAL;
11589ffa9677SSam Leffler 	IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
115988768458SSam Leffler 	if (len < sizeof(*xpl))
116088768458SSam Leffler 		return EINVAL;
116188768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
116288768458SSam Leffler 
116388768458SSam Leffler 	/* select direction */
116488768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
116588768458SSam Leffler 	case IPSEC_DIR_INBOUND:
116688768458SSam Leffler 		pcb_sp = inp->inp_sp->sp_in;
116788768458SSam Leffler 		break;
116888768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
116988768458SSam Leffler 		pcb_sp = inp->inp_sp->sp_out;
117088768458SSam Leffler 		break;
117188768458SSam Leffler 	default:
11729ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
117388768458SSam Leffler 			xpl->sadb_x_policy_dir));
117488768458SSam Leffler 		return EINVAL;
117588768458SSam Leffler 	}
117688768458SSam Leffler 
117788768458SSam Leffler 	return ipsec_get_policy(pcb_sp, mp);
117888768458SSam Leffler }
117988768458SSam Leffler 
118088768458SSam Leffler /* delete policy in PCB */
118188768458SSam Leffler int
118288768458SSam Leffler ipsec4_delete_pcbpolicy(inp)
118388768458SSam Leffler 	struct inpcb *inp;
118488768458SSam Leffler {
11859ffa9677SSam Leffler 	IPSEC_ASSERT(inp != NULL, ("null inp"));
118688768458SSam Leffler 
118788768458SSam Leffler 	if (inp->inp_sp == NULL)
118888768458SSam Leffler 		return 0;
118988768458SSam Leffler 
119088768458SSam Leffler 	if (inp->inp_sp->sp_in != NULL)
119188768458SSam Leffler 		KEY_FREESP(&inp->inp_sp->sp_in);
119288768458SSam Leffler 
119388768458SSam Leffler 	if (inp->inp_sp->sp_out != NULL)
119488768458SSam Leffler 		KEY_FREESP(&inp->inp_sp->sp_out);
119588768458SSam Leffler 
119688768458SSam Leffler 	ipsec_delpcbpolicy(inp->inp_sp);
119788768458SSam Leffler 	inp->inp_sp = NULL;
119888768458SSam Leffler 
119988768458SSam Leffler 	return 0;
120088768458SSam Leffler }
120188768458SSam Leffler 
120288768458SSam Leffler #ifdef INET6
120388768458SSam Leffler int
1204c26fe973SBjoern A. Zeeb ipsec6_set_policy(in6p, optname, request, len, cred)
120588768458SSam Leffler 	struct in6pcb *in6p;
120688768458SSam Leffler 	int optname;
120788768458SSam Leffler 	caddr_t request;
120888768458SSam Leffler 	size_t len;
1209c26fe973SBjoern A. Zeeb 	struct ucred *cred;
121088768458SSam Leffler {
12118b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
121288768458SSam Leffler 	struct sadb_x_policy *xpl;
121388768458SSam Leffler 	struct secpolicy **pcb_sp;
121488768458SSam Leffler 
121588768458SSam Leffler 	/* sanity check. */
121688768458SSam Leffler 	if (in6p == NULL || request == NULL)
121788768458SSam Leffler 		return EINVAL;
121888768458SSam Leffler 	if (len < sizeof(*xpl))
121988768458SSam Leffler 		return EINVAL;
122088768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
122188768458SSam Leffler 
122288768458SSam Leffler 	/* select direction */
122388768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
122488768458SSam Leffler 	case IPSEC_DIR_INBOUND:
122588768458SSam Leffler 		pcb_sp = &in6p->in6p_sp->sp_in;
122688768458SSam Leffler 		break;
122788768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
122888768458SSam Leffler 		pcb_sp = &in6p->in6p_sp->sp_out;
122988768458SSam Leffler 		break;
123088768458SSam Leffler 	default:
12319ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
123288768458SSam Leffler 			xpl->sadb_x_policy_dir));
123388768458SSam Leffler 		return EINVAL;
123488768458SSam Leffler 	}
123588768458SSam Leffler 
1236c26fe973SBjoern A. Zeeb 	return ipsec_set_policy(pcb_sp, optname, request, len, cred);
123788768458SSam Leffler }
123888768458SSam Leffler 
123988768458SSam Leffler int
124088768458SSam Leffler ipsec6_get_policy(in6p, request, len, mp)
124188768458SSam Leffler 	struct in6pcb *in6p;
124288768458SSam Leffler 	caddr_t request;
124388768458SSam Leffler 	size_t len;
124488768458SSam Leffler 	struct mbuf **mp;
124588768458SSam Leffler {
12468b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
124788768458SSam Leffler 	struct sadb_x_policy *xpl;
124888768458SSam Leffler 	struct secpolicy *pcb_sp;
124988768458SSam Leffler 
125088768458SSam Leffler 	/* sanity check. */
125188768458SSam Leffler 	if (in6p == NULL || request == NULL || mp == NULL)
125288768458SSam Leffler 		return EINVAL;
12539ffa9677SSam Leffler 	IPSEC_ASSERT(in6p->in6p_sp != NULL, ("null in6p_sp"));
125488768458SSam Leffler 	if (len < sizeof(*xpl))
125588768458SSam Leffler 		return EINVAL;
125688768458SSam Leffler 	xpl = (struct sadb_x_policy *)request;
125788768458SSam Leffler 
125888768458SSam Leffler 	/* select direction */
125988768458SSam Leffler 	switch (xpl->sadb_x_policy_dir) {
126088768458SSam Leffler 	case IPSEC_DIR_INBOUND:
126188768458SSam Leffler 		pcb_sp = in6p->in6p_sp->sp_in;
126288768458SSam Leffler 		break;
126388768458SSam Leffler 	case IPSEC_DIR_OUTBOUND:
126488768458SSam Leffler 		pcb_sp = in6p->in6p_sp->sp_out;
126588768458SSam Leffler 		break;
126688768458SSam Leffler 	default:
12679ffa9677SSam Leffler 		ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
126888768458SSam Leffler 			xpl->sadb_x_policy_dir));
126988768458SSam Leffler 		return EINVAL;
127088768458SSam Leffler 	}
127188768458SSam Leffler 
127288768458SSam Leffler 	return ipsec_get_policy(pcb_sp, mp);
127388768458SSam Leffler }
127488768458SSam Leffler 
127588768458SSam Leffler int
127688768458SSam Leffler ipsec6_delete_pcbpolicy(in6p)
127788768458SSam Leffler 	struct in6pcb *in6p;
127888768458SSam Leffler {
12799ffa9677SSam Leffler 	IPSEC_ASSERT(in6p != NULL, ("null in6p"));
128088768458SSam Leffler 
128188768458SSam Leffler 	if (in6p->in6p_sp == NULL)
128288768458SSam Leffler 		return 0;
128388768458SSam Leffler 
128488768458SSam Leffler 	if (in6p->in6p_sp->sp_in != NULL)
128588768458SSam Leffler 		KEY_FREESP(&in6p->in6p_sp->sp_in);
128688768458SSam Leffler 
128788768458SSam Leffler 	if (in6p->in6p_sp->sp_out != NULL)
128888768458SSam Leffler 		KEY_FREESP(&in6p->in6p_sp->sp_out);
128988768458SSam Leffler 
129088768458SSam Leffler 	ipsec_delpcbpolicy(in6p->in6p_sp);
129188768458SSam Leffler 	in6p->in6p_sp = NULL;
129288768458SSam Leffler 
129388768458SSam Leffler 	return 0;
129488768458SSam Leffler }
129588768458SSam Leffler #endif
129688768458SSam Leffler 
129788768458SSam Leffler /*
129888768458SSam Leffler  * return current level.
129988768458SSam Leffler  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
130088768458SSam Leffler  */
130188768458SSam Leffler u_int
130288768458SSam Leffler ipsec_get_reqlevel(isr)
130388768458SSam Leffler 	struct ipsecrequest *isr;
130488768458SSam Leffler {
13058b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
130688768458SSam Leffler 	u_int level = 0;
130788768458SSam Leffler 	u_int esp_trans_deflev, esp_net_deflev;
130888768458SSam Leffler 	u_int ah_trans_deflev, ah_net_deflev;
130988768458SSam Leffler 
13109ffa9677SSam Leffler 	IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
13119ffa9677SSam Leffler 	IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
13129ffa9677SSam Leffler 		("af family mismatch, src %u, dst %u",
131388768458SSam Leffler 		 isr->sp->spidx.src.sa.sa_family,
131488768458SSam Leffler 		 isr->sp->spidx.dst.sa.sa_family));
131588768458SSam Leffler 
131688768458SSam Leffler /* XXX note that we have ipseclog() expanded here - code sync issue */
131788768458SSam Leffler #define IPSEC_CHECK_DEFAULT(lev) \
131888768458SSam Leffler 	(((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE	      \
131988768458SSam Leffler 			&& (lev) != IPSEC_LEVEL_UNIQUE)			      \
1320603724d3SBjoern A. Zeeb 		? (V_ipsec_debug						      \
132188768458SSam Leffler 			? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
132288768458SSam Leffler 				(lev), IPSEC_LEVEL_REQUIRE)		      \
132388768458SSam Leffler 			: 0),						      \
132488768458SSam Leffler 			(lev) = IPSEC_LEVEL_REQUIRE,			      \
132588768458SSam Leffler 			(lev)						      \
132688768458SSam Leffler 		: (lev))
132788768458SSam Leffler 
132888768458SSam Leffler 	/* set default level */
132988768458SSam Leffler 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
133088768458SSam Leffler #ifdef INET
133188768458SSam Leffler 	case AF_INET:
1332603724d3SBjoern A. Zeeb 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1333603724d3SBjoern A. Zeeb 		esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1334603724d3SBjoern A. Zeeb 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1335603724d3SBjoern A. Zeeb 		ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
133688768458SSam Leffler 		break;
133788768458SSam Leffler #endif
133888768458SSam Leffler #ifdef INET6
133988768458SSam Leffler 	case AF_INET6:
1340603724d3SBjoern A. Zeeb 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1341603724d3SBjoern A. Zeeb 		esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1342603724d3SBjoern A. Zeeb 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1343603724d3SBjoern A. Zeeb 		ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
134488768458SSam Leffler 		break;
134588768458SSam Leffler #endif /* INET6 */
134688768458SSam Leffler 	default:
13479ffa9677SSam Leffler 		panic("%s: unknown af %u",
13489ffa9677SSam Leffler 			__func__, isr->sp->spidx.src.sa.sa_family);
134988768458SSam Leffler 	}
135088768458SSam Leffler 
135188768458SSam Leffler #undef IPSEC_CHECK_DEFAULT
135288768458SSam Leffler 
135388768458SSam Leffler 	/* set level */
135488768458SSam Leffler 	switch (isr->level) {
135588768458SSam Leffler 	case IPSEC_LEVEL_DEFAULT:
135688768458SSam Leffler 		switch (isr->saidx.proto) {
135788768458SSam Leffler 		case IPPROTO_ESP:
135888768458SSam Leffler 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
135988768458SSam Leffler 				level = esp_net_deflev;
136088768458SSam Leffler 			else
136188768458SSam Leffler 				level = esp_trans_deflev;
136288768458SSam Leffler 			break;
136388768458SSam Leffler 		case IPPROTO_AH:
136488768458SSam Leffler 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
136588768458SSam Leffler 				level = ah_net_deflev;
136688768458SSam Leffler 			else
136788768458SSam Leffler 				level = ah_trans_deflev;
13688381996eSSam Leffler 			break;
136988768458SSam Leffler 		case IPPROTO_IPCOMP:
137088768458SSam Leffler 			/*
137188768458SSam Leffler 			 * we don't really care, as IPcomp document says that
137288768458SSam Leffler 			 * we shouldn't compress small packets
137388768458SSam Leffler 			 */
137488768458SSam Leffler 			level = IPSEC_LEVEL_USE;
137588768458SSam Leffler 			break;
137688768458SSam Leffler 		default:
13779ffa9677SSam Leffler 			panic("%s: Illegal protocol defined %u\n", __func__,
137888768458SSam Leffler 				isr->saidx.proto);
137988768458SSam Leffler 		}
138088768458SSam Leffler 		break;
138188768458SSam Leffler 
138288768458SSam Leffler 	case IPSEC_LEVEL_USE:
138388768458SSam Leffler 	case IPSEC_LEVEL_REQUIRE:
138488768458SSam Leffler 		level = isr->level;
138588768458SSam Leffler 		break;
138688768458SSam Leffler 	case IPSEC_LEVEL_UNIQUE:
138788768458SSam Leffler 		level = IPSEC_LEVEL_REQUIRE;
138888768458SSam Leffler 		break;
138988768458SSam Leffler 
139088768458SSam Leffler 	default:
13919ffa9677SSam Leffler 		panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
139288768458SSam Leffler 	}
139388768458SSam Leffler 
139488768458SSam Leffler 	return level;
139588768458SSam Leffler }
139688768458SSam Leffler 
139788768458SSam Leffler /*
139888768458SSam Leffler  * Check security policy requirements against the actual
139988768458SSam Leffler  * packet contents.  Return one if the packet should be
140088768458SSam Leffler  * reject as "invalid"; otherwiser return zero to have the
140188768458SSam Leffler  * packet treated as "valid".
140288768458SSam Leffler  *
140388768458SSam Leffler  * OUT:
140488768458SSam Leffler  *	0: valid
140588768458SSam Leffler  *	1: invalid
140688768458SSam Leffler  */
140788768458SSam Leffler int
140888768458SSam Leffler ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
140988768458SSam Leffler {
14108b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
141188768458SSam Leffler 	struct ipsecrequest *isr;
141288768458SSam Leffler 	int need_auth;
141388768458SSam Leffler 
141488768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
14159ffa9677SSam Leffler 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
141688768458SSam Leffler 
141788768458SSam Leffler 	/* check policy */
141888768458SSam Leffler 	switch (sp->policy) {
141988768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
142088768458SSam Leffler 		return 1;
142188768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
142288768458SSam Leffler 	case IPSEC_POLICY_NONE:
142388768458SSam Leffler 		return 0;
142488768458SSam Leffler 	}
142588768458SSam Leffler 
14269ffa9677SSam Leffler 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
14279ffa9677SSam Leffler 		("invalid policy %u", sp->policy));
142888768458SSam Leffler 
142988768458SSam Leffler 	/* XXX should compare policy against ipsec header history */
143088768458SSam Leffler 
143188768458SSam Leffler 	need_auth = 0;
143288768458SSam Leffler 	for (isr = sp->req; isr != NULL; isr = isr->next) {
143388768458SSam Leffler 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
143488768458SSam Leffler 			continue;
143588768458SSam Leffler 		switch (isr->saidx.proto) {
143688768458SSam Leffler 		case IPPROTO_ESP:
143788768458SSam Leffler 			if ((m->m_flags & M_DECRYPTED) == 0) {
143888768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14399ffa9677SSam Leffler 				    printf("%s: ESP m_flags:%x\n", __func__,
144088768458SSam Leffler 					    m->m_flags));
144188768458SSam Leffler 				return 1;
144288768458SSam Leffler 			}
144388768458SSam Leffler 
144488768458SSam Leffler 			if (!need_auth &&
144588768458SSam Leffler 			    isr->sav != NULL &&
144688768458SSam Leffler 			    isr->sav->tdb_authalgxform != NULL &&
144788768458SSam Leffler 			    (m->m_flags & M_AUTHIPDGM) == 0) {
144888768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14499ffa9677SSam Leffler 				    printf("%s: ESP/AH m_flags:%x\n", __func__,
145088768458SSam Leffler 					    m->m_flags));
145188768458SSam Leffler 				return 1;
145288768458SSam Leffler 			}
145388768458SSam Leffler 			break;
145488768458SSam Leffler 		case IPPROTO_AH:
145588768458SSam Leffler 			need_auth = 1;
145688768458SSam Leffler 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
145788768458SSam Leffler 				KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
14589ffa9677SSam Leffler 				    printf("%s: AH m_flags:%x\n", __func__,
145988768458SSam Leffler 					    m->m_flags));
146088768458SSam Leffler 				return 1;
146188768458SSam Leffler 			}
146288768458SSam Leffler 			break;
146388768458SSam Leffler 		case IPPROTO_IPCOMP:
146488768458SSam Leffler 			/*
146588768458SSam Leffler 			 * we don't really care, as IPcomp document
146688768458SSam Leffler 			 * says that we shouldn't compress small
146788768458SSam Leffler 			 * packets, IPComp policy should always be
146888768458SSam Leffler 			 * treated as being in "use" level.
146988768458SSam Leffler 			 */
147088768458SSam Leffler 			break;
147188768458SSam Leffler 		}
147288768458SSam Leffler 	}
147388768458SSam Leffler 	return 0;		/* valid */
147488768458SSam Leffler }
147588768458SSam Leffler 
147688768458SSam Leffler /*
147788768458SSam Leffler  * Check AH/ESP integrity.
147888768458SSam Leffler  * This function is called from tcp_input(), udp_input(),
147988768458SSam Leffler  * and {ah,esp}4_input for tunnel mode
148088768458SSam Leffler  */
148188768458SSam Leffler int
148288768458SSam Leffler ipsec4_in_reject(m, inp)
148388768458SSam Leffler 	struct mbuf *m;
148488768458SSam Leffler 	struct inpcb *inp;
148588768458SSam Leffler {
14868b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
148788768458SSam Leffler 	struct secpolicy *sp;
148888768458SSam Leffler 	int error;
148988768458SSam Leffler 	int result;
149088768458SSam Leffler 
14919ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
149288768458SSam Leffler 
149388768458SSam Leffler 	/* get SP for this packet.
149488768458SSam Leffler 	 * When we are called from ip_forward(), we call
149588768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
149688768458SSam Leffler 	 */
149788768458SSam Leffler 	if (inp == NULL)
149888768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
149988768458SSam Leffler 	else
150088768458SSam Leffler 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
150188768458SSam Leffler 
150288768458SSam Leffler 	if (sp != NULL) {
150388768458SSam Leffler 		result = ipsec_in_reject(sp, m);
150488768458SSam Leffler 		if (result)
1505603724d3SBjoern A. Zeeb 			V_ipsec4stat.ips_in_polvio++;
150688768458SSam Leffler 		KEY_FREESP(&sp);
150788768458SSam Leffler 	} else {
150888768458SSam Leffler 		result = 0;	/* XXX should be panic ?
150988768458SSam Leffler 				 * -> No, there may be error. */
151088768458SSam Leffler 	}
151188768458SSam Leffler 	return result;
151288768458SSam Leffler }
151388768458SSam Leffler 
151488768458SSam Leffler #ifdef INET6
151588768458SSam Leffler /*
151688768458SSam Leffler  * Check AH/ESP integrity.
151788768458SSam Leffler  * This function is called from tcp6_input(), udp6_input(),
151888768458SSam Leffler  * and {ah,esp}6_input for tunnel mode
151988768458SSam Leffler  */
152088768458SSam Leffler int
152188768458SSam Leffler ipsec6_in_reject(m, inp)
152288768458SSam Leffler 	struct mbuf *m;
152388768458SSam Leffler 	struct inpcb *inp;
152488768458SSam Leffler {
15258b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
152688768458SSam Leffler 	struct secpolicy *sp = NULL;
152788768458SSam Leffler 	int error;
152888768458SSam Leffler 	int result;
152988768458SSam Leffler 
153088768458SSam Leffler 	/* sanity check */
153188768458SSam Leffler 	if (m == NULL)
153288768458SSam Leffler 		return 0;	/* XXX should be panic ? */
153388768458SSam Leffler 
153488768458SSam Leffler 	/* get SP for this packet.
153588768458SSam Leffler 	 * When we are called from ip_forward(), we call
153688768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
153788768458SSam Leffler 	 */
153888768458SSam Leffler 	if (inp == NULL)
153988768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
154088768458SSam Leffler 	else
154188768458SSam Leffler 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
154288768458SSam Leffler 
154388768458SSam Leffler 	if (sp != NULL) {
154488768458SSam Leffler 		result = ipsec_in_reject(sp, m);
154588768458SSam Leffler 		if (result)
1546603724d3SBjoern A. Zeeb 			V_ipsec6stat.ips_in_polvio++;
154788768458SSam Leffler 		KEY_FREESP(&sp);
154888768458SSam Leffler 	} else {
154988768458SSam Leffler 		result = 0;
155088768458SSam Leffler 	}
155188768458SSam Leffler 	return result;
155288768458SSam Leffler }
155388768458SSam Leffler #endif
155488768458SSam Leffler 
155588768458SSam Leffler /*
155688768458SSam Leffler  * compute the byte size to be occupied by IPsec header.
155788768458SSam Leffler  * in case it is tunneled, it includes the size of outer IP header.
155888768458SSam Leffler  * NOTE: SP passed is free in this function.
155988768458SSam Leffler  */
156088768458SSam Leffler static size_t
156188768458SSam Leffler ipsec_hdrsiz(struct secpolicy *sp)
156288768458SSam Leffler {
15638b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
156488768458SSam Leffler 	struct ipsecrequest *isr;
156588768458SSam Leffler 	size_t siz;
156688768458SSam Leffler 
156788768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
15689ffa9677SSam Leffler 		printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
156988768458SSam Leffler 
157088768458SSam Leffler 	switch (sp->policy) {
157188768458SSam Leffler 	case IPSEC_POLICY_DISCARD:
157288768458SSam Leffler 	case IPSEC_POLICY_BYPASS:
157388768458SSam Leffler 	case IPSEC_POLICY_NONE:
157488768458SSam Leffler 		return 0;
157588768458SSam Leffler 	}
157688768458SSam Leffler 
15779ffa9677SSam Leffler 	IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
15789ffa9677SSam Leffler 		("invalid policy %u", sp->policy));
157988768458SSam Leffler 
158088768458SSam Leffler 	siz = 0;
158188768458SSam Leffler 	for (isr = sp->req; isr != NULL; isr = isr->next) {
158288768458SSam Leffler 		size_t clen = 0;
158388768458SSam Leffler 
158488768458SSam Leffler 		switch (isr->saidx.proto) {
158588768458SSam Leffler 		case IPPROTO_ESP:
158688768458SSam Leffler 			clen = esp_hdrsiz(isr->sav);
158788768458SSam Leffler 			break;
158888768458SSam Leffler 		case IPPROTO_AH:
158988768458SSam Leffler 			clen = ah_hdrsiz(isr->sav);
159088768458SSam Leffler 			break;
159188768458SSam Leffler 		case IPPROTO_IPCOMP:
159288768458SSam Leffler 			clen = sizeof(struct ipcomp);
159388768458SSam Leffler 			break;
159488768458SSam Leffler 		}
159588768458SSam Leffler 
159688768458SSam Leffler 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
159788768458SSam Leffler 			switch (isr->saidx.dst.sa.sa_family) {
159888768458SSam Leffler 			case AF_INET:
159988768458SSam Leffler 				clen += sizeof(struct ip);
160088768458SSam Leffler 				break;
160188768458SSam Leffler #ifdef INET6
160288768458SSam Leffler 			case AF_INET6:
160388768458SSam Leffler 				clen += sizeof(struct ip6_hdr);
160488768458SSam Leffler 				break;
160588768458SSam Leffler #endif
160688768458SSam Leffler 			default:
16079ffa9677SSam Leffler 				ipseclog((LOG_ERR, "%s: unknown AF %d in "
16089ffa9677SSam Leffler 				    "IPsec tunnel SA\n", __func__,
160988768458SSam Leffler 				    ((struct sockaddr *)&isr->saidx.dst)->sa_family));
161088768458SSam Leffler 				break;
161188768458SSam Leffler 			}
161288768458SSam Leffler 		}
161388768458SSam Leffler 		siz += clen;
161488768458SSam Leffler 	}
161588768458SSam Leffler 
161688768458SSam Leffler 	return siz;
161788768458SSam Leffler }
161888768458SSam Leffler 
161988768458SSam Leffler /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
162088768458SSam Leffler size_t
162188768458SSam Leffler ipsec4_hdrsiz(m, dir, inp)
162288768458SSam Leffler 	struct mbuf *m;
162388768458SSam Leffler 	u_int dir;
162488768458SSam Leffler 	struct inpcb *inp;
162588768458SSam Leffler {
16268b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
162788768458SSam Leffler 	struct secpolicy *sp;
162888768458SSam Leffler 	int error;
162988768458SSam Leffler 	size_t size;
163088768458SSam Leffler 
16319ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
163288768458SSam Leffler 
163388768458SSam Leffler 	/* get SP for this packet.
163488768458SSam Leffler 	 * When we are called from ip_forward(), we call
163588768458SSam Leffler 	 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
163688768458SSam Leffler 	 */
163788768458SSam Leffler 	if (inp == NULL)
163888768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
163988768458SSam Leffler 	else
164088768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, inp, &error);
164188768458SSam Leffler 
164288768458SSam Leffler 	if (sp != NULL) {
164388768458SSam Leffler 		size = ipsec_hdrsiz(sp);
164488768458SSam Leffler 		KEYDEBUG(KEYDEBUG_IPSEC_DATA,
16459ffa9677SSam Leffler 			printf("%s: size:%lu.\n", __func__,
164688768458SSam Leffler 				(unsigned long)size));
164788768458SSam Leffler 
164888768458SSam Leffler 		KEY_FREESP(&sp);
164988768458SSam Leffler 	} else {
16504a67e051SBjoern A. Zeeb 		size = 0;	/* XXX should be panic ?
16514a67e051SBjoern A. Zeeb 				 * -> No, we are called w/o knowing if
16524a67e051SBjoern A. Zeeb 				 *    IPsec processing is needed. */
165388768458SSam Leffler 	}
165488768458SSam Leffler 	return size;
165588768458SSam Leffler }
165688768458SSam Leffler 
165788768458SSam Leffler #ifdef INET6
165888768458SSam Leffler /* This function is called from ipsec6_hdrsize_tcp(),
165988768458SSam Leffler  * and maybe from ip6_forward.()
166088768458SSam Leffler  */
166188768458SSam Leffler size_t
166288768458SSam Leffler ipsec6_hdrsiz(m, dir, in6p)
166388768458SSam Leffler 	struct mbuf *m;
166488768458SSam Leffler 	u_int dir;
166588768458SSam Leffler 	struct in6pcb *in6p;
166688768458SSam Leffler {
16678b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
166888768458SSam Leffler 	struct secpolicy *sp;
166988768458SSam Leffler 	int error;
167088768458SSam Leffler 	size_t size;
167188768458SSam Leffler 
16729ffa9677SSam Leffler 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
16739ffa9677SSam Leffler 	IPSEC_ASSERT(in6p == NULL || in6p->in6p_socket != NULL,
16749ffa9677SSam Leffler 		("socket w/o inpcb"));
167588768458SSam Leffler 
167688768458SSam Leffler 	/* get SP for this packet */
167788768458SSam Leffler 	/* XXX Is it right to call with IP_FORWARDING. */
167888768458SSam Leffler 	if (in6p == NULL)
167988768458SSam Leffler 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
168088768458SSam Leffler 	else
168188768458SSam Leffler 		sp = ipsec_getpolicybysock(m, dir, in6p, &error);
168288768458SSam Leffler 
168388768458SSam Leffler 	if (sp == NULL)
168488768458SSam Leffler 		return 0;
168588768458SSam Leffler 	size = ipsec_hdrsiz(sp);
168688768458SSam Leffler 	KEYDEBUG(KEYDEBUG_IPSEC_DATA,
16879ffa9677SSam Leffler 		printf("%s: size:%lu.\n", __func__, (unsigned long)size));
168888768458SSam Leffler 	KEY_FREESP(&sp);
168988768458SSam Leffler 
169088768458SSam Leffler 	return size;
169188768458SSam Leffler }
169288768458SSam Leffler #endif /*INET6*/
169388768458SSam Leffler 
169488768458SSam Leffler /*
169588768458SSam Leffler  * Check the variable replay window.
169688768458SSam Leffler  * ipsec_chkreplay() performs replay check before ICV verification.
169788768458SSam Leffler  * ipsec_updatereplay() updates replay bitmap.  This must be called after
169888768458SSam Leffler  * ICV verification (it also performs replay check, which is usually done
169988768458SSam Leffler  * beforehand).
170088768458SSam Leffler  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
170188768458SSam Leffler  *
170288768458SSam Leffler  * based on RFC 2401.
170388768458SSam Leffler  */
170488768458SSam Leffler int
170588768458SSam Leffler ipsec_chkreplay(seq, sav)
170688768458SSam Leffler 	u_int32_t seq;
170788768458SSam Leffler 	struct secasvar *sav;
170888768458SSam Leffler {
170988768458SSam Leffler 	const struct secreplay *replay;
171088768458SSam Leffler 	u_int32_t diff;
171188768458SSam Leffler 	int fr;
171288768458SSam Leffler 	u_int32_t wsizeb;	/* constant: bits of window size */
171388768458SSam Leffler 	int frlast;		/* constant: last frame */
171488768458SSam Leffler 
17159ffa9677SSam Leffler 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
17169ffa9677SSam Leffler 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
171788768458SSam Leffler 
171888768458SSam Leffler 	replay = sav->replay;
171988768458SSam Leffler 
172088768458SSam Leffler 	if (replay->wsize == 0)
172188768458SSam Leffler 		return 1;	/* no need to check replay. */
172288768458SSam Leffler 
172388768458SSam Leffler 	/* constant */
172488768458SSam Leffler 	frlast = replay->wsize - 1;
172588768458SSam Leffler 	wsizeb = replay->wsize << 3;
172688768458SSam Leffler 
172788768458SSam Leffler 	/* sequence number of 0 is invalid */
172888768458SSam Leffler 	if (seq == 0)
172988768458SSam Leffler 		return 0;
173088768458SSam Leffler 
173188768458SSam Leffler 	/* first time is always okay */
173288768458SSam Leffler 	if (replay->count == 0)
173388768458SSam Leffler 		return 1;
173488768458SSam Leffler 
173588768458SSam Leffler 	if (seq > replay->lastseq) {
173688768458SSam Leffler 		/* larger sequences are okay */
173788768458SSam Leffler 		return 1;
173888768458SSam Leffler 	} else {
173988768458SSam Leffler 		/* seq is equal or less than lastseq. */
174088768458SSam Leffler 		diff = replay->lastseq - seq;
174188768458SSam Leffler 
174288768458SSam Leffler 		/* over range to check, i.e. too old or wrapped */
174388768458SSam Leffler 		if (diff >= wsizeb)
174488768458SSam Leffler 			return 0;
174588768458SSam Leffler 
174688768458SSam Leffler 		fr = frlast - diff / 8;
174788768458SSam Leffler 
174888768458SSam Leffler 		/* this packet already seen ? */
174988768458SSam Leffler 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
175088768458SSam Leffler 			return 0;
175188768458SSam Leffler 
175288768458SSam Leffler 		/* out of order but good */
175388768458SSam Leffler 		return 1;
175488768458SSam Leffler 	}
175588768458SSam Leffler }
175688768458SSam Leffler 
175788768458SSam Leffler /*
175888768458SSam Leffler  * check replay counter whether to update or not.
175988768458SSam Leffler  * OUT:	0:	OK
176088768458SSam Leffler  *	1:	NG
176188768458SSam Leffler  */
176288768458SSam Leffler int
176388768458SSam Leffler ipsec_updatereplay(seq, sav)
176488768458SSam Leffler 	u_int32_t seq;
176588768458SSam Leffler 	struct secasvar *sav;
176688768458SSam Leffler {
17678b615593SMarko Zec 	INIT_VNET_IPSEC(curvnet);
176888768458SSam Leffler 	struct secreplay *replay;
176988768458SSam Leffler 	u_int32_t diff;
177088768458SSam Leffler 	int fr;
177188768458SSam Leffler 	u_int32_t wsizeb;	/* constant: bits of window size */
177288768458SSam Leffler 	int frlast;		/* constant: last frame */
177388768458SSam Leffler 
17749ffa9677SSam Leffler 	IPSEC_ASSERT(sav != NULL, ("Null SA"));
17759ffa9677SSam Leffler 	IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
177688768458SSam Leffler 
177788768458SSam Leffler 	replay = sav->replay;
177888768458SSam Leffler 
177988768458SSam Leffler 	if (replay->wsize == 0)
178088768458SSam Leffler 		goto ok;	/* no need to check replay. */
178188768458SSam Leffler 
178288768458SSam Leffler 	/* constant */
178388768458SSam Leffler 	frlast = replay->wsize - 1;
178488768458SSam Leffler 	wsizeb = replay->wsize << 3;
178588768458SSam Leffler 
178688768458SSam Leffler 	/* sequence number of 0 is invalid */
178788768458SSam Leffler 	if (seq == 0)
178888768458SSam Leffler 		return 1;
178988768458SSam Leffler 
179088768458SSam Leffler 	/* first time */
179188768458SSam Leffler 	if (replay->count == 0) {
179288768458SSam Leffler 		replay->lastseq = seq;
179388768458SSam Leffler 		bzero(replay->bitmap, replay->wsize);
179488768458SSam Leffler 		(replay->bitmap)[frlast] = 1;
179588768458SSam Leffler 		goto ok;
179688768458SSam Leffler 	}
179788768458SSam Leffler 
179888768458SSam Leffler 	if (seq > replay->lastseq) {
179988768458SSam Leffler 		/* seq is larger than lastseq. */
180088768458SSam Leffler 		diff = seq - replay->lastseq;
180188768458SSam Leffler 
180288768458SSam Leffler 		/* new larger sequence number */
180388768458SSam Leffler 		if (diff < wsizeb) {
180488768458SSam Leffler 			/* In window */
180588768458SSam Leffler 			/* set bit for this packet */
180688768458SSam Leffler 			vshiftl(replay->bitmap, diff, replay->wsize);
180788768458SSam Leffler 			(replay->bitmap)[frlast] |= 1;
180888768458SSam Leffler 		} else {
180988768458SSam Leffler 			/* this packet has a "way larger" */
181088768458SSam Leffler 			bzero(replay->bitmap, replay->wsize);
181188768458SSam Leffler 			(replay->bitmap)[frlast] = 1;
181288768458SSam Leffler 		}
181388768458SSam Leffler 		replay->lastseq = seq;
181488768458SSam Leffler 
181588768458SSam Leffler 		/* larger is good */
181688768458SSam Leffler 	} else {
181788768458SSam Leffler 		/* seq is equal or less than lastseq. */
181888768458SSam Leffler 		diff = replay->lastseq - seq;
181988768458SSam Leffler 
182088768458SSam Leffler 		/* over range to check, i.e. too old or wrapped */
182188768458SSam Leffler 		if (diff >= wsizeb)
182288768458SSam Leffler 			return 1;
182388768458SSam Leffler 
182488768458SSam Leffler 		fr = frlast - diff / 8;
182588768458SSam Leffler 
182688768458SSam Leffler 		/* this packet already seen ? */
182788768458SSam Leffler 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
182888768458SSam Leffler 			return 1;
182988768458SSam Leffler 
183088768458SSam Leffler 		/* mark as seen */
183188768458SSam Leffler 		(replay->bitmap)[fr] |= (1 << (diff % 8));
183288768458SSam Leffler 
183388768458SSam Leffler 		/* out of order but good */
183488768458SSam Leffler 	}
183588768458SSam Leffler 
183688768458SSam Leffler ok:
183788768458SSam Leffler 	if (replay->count == ~0) {
183888768458SSam Leffler 
183988768458SSam Leffler 		/* set overflow flag */
184088768458SSam Leffler 		replay->overflow++;
184188768458SSam Leffler 
184288768458SSam Leffler 		/* don't increment, no more packets accepted */
184388768458SSam Leffler 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
184488768458SSam Leffler 			return 1;
184588768458SSam Leffler 
18469ffa9677SSam Leffler 		ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
18479ffa9677SSam Leffler 		    __func__, replay->overflow, ipsec_logsastr(sav)));
184888768458SSam Leffler 	}
184988768458SSam Leffler 
185088768458SSam Leffler 	replay->count++;
185188768458SSam Leffler 
185288768458SSam Leffler 	return 0;
185388768458SSam Leffler }
185488768458SSam Leffler 
185588768458SSam Leffler /*
1856d24ff94bSHiten Pandya  * shift variable length buffer to left.
185788768458SSam Leffler  * IN:	bitmap: pointer to the buffer
185888768458SSam Leffler  * 	nbit:	the number of to shift.
185988768458SSam Leffler  *	wsize:	buffer size (bytes).
186088768458SSam Leffler  */
186188768458SSam Leffler static void
186288768458SSam Leffler vshiftl(bitmap, nbit, wsize)
186388768458SSam Leffler 	unsigned char *bitmap;
186488768458SSam Leffler 	int nbit, wsize;
186588768458SSam Leffler {
186688768458SSam Leffler 	int s, j, i;
186788768458SSam Leffler 	unsigned char over;
186888768458SSam Leffler 
186988768458SSam Leffler 	for (j = 0; j < nbit; j += 8) {
187088768458SSam Leffler 		s = (nbit - j < 8) ? (nbit - j): 8;
187188768458SSam Leffler 		bitmap[0] <<= s;
187288768458SSam Leffler 		for (i = 1; i < wsize; i++) {
187388768458SSam Leffler 			over = (bitmap[i] >> (8 - s));
187488768458SSam Leffler 			bitmap[i] <<= s;
187588768458SSam Leffler 			bitmap[i-1] |= over;
187688768458SSam Leffler 		}
187788768458SSam Leffler 	}
187888768458SSam Leffler 
187988768458SSam Leffler 	return;
188088768458SSam Leffler }
188188768458SSam Leffler 
188288768458SSam Leffler /* Return a printable string for the IPv4 address. */
188388768458SSam Leffler static char *
188488768458SSam Leffler inet_ntoa4(struct in_addr ina)
188588768458SSam Leffler {
188688768458SSam Leffler 	static char buf[4][4 * sizeof "123" + 4];
188788768458SSam Leffler 	unsigned char *ucp = (unsigned char *) &ina;
188888768458SSam Leffler 	static int i = 3;
188988768458SSam Leffler 
18901d54aa3bSBjoern A. Zeeb 	/* XXX-BZ returns static buffer. */
189188768458SSam Leffler 	i = (i + 1) % 4;
189288768458SSam Leffler 	sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
189388768458SSam Leffler 	    ucp[2] & 0xff, ucp[3] & 0xff);
189488768458SSam Leffler 	return (buf[i]);
189588768458SSam Leffler }
189688768458SSam Leffler 
189788768458SSam Leffler /* Return a printable string for the address. */
189888768458SSam Leffler char *
189988768458SSam Leffler ipsec_address(union sockaddr_union* sa)
190088768458SSam Leffler {
1901224c45c4SBjoern A. Zeeb #ifdef INET6
19021d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
19031d54aa3bSBjoern A. Zeeb #endif
190488768458SSam Leffler 	switch (sa->sa.sa_family) {
190549ddabdfSPawel Jakub Dawidek #ifdef INET
190688768458SSam Leffler 	case AF_INET:
190788768458SSam Leffler 		return inet_ntoa4(sa->sin.sin_addr);
190888768458SSam Leffler #endif /* INET */
190988768458SSam Leffler 
191049ddabdfSPawel Jakub Dawidek #ifdef INET6
191188768458SSam Leffler 	case AF_INET6:
19121d54aa3bSBjoern A. Zeeb 		return ip6_sprintf(ip6buf, &sa->sin6.sin6_addr);
191388768458SSam Leffler #endif /* INET6 */
191488768458SSam Leffler 
191588768458SSam Leffler 	default:
191688768458SSam Leffler 		return "(unknown address family)";
191788768458SSam Leffler 	}
191888768458SSam Leffler }
191988768458SSam Leffler 
192088768458SSam Leffler const char *
192188768458SSam Leffler ipsec_logsastr(sav)
192288768458SSam Leffler 	struct secasvar *sav;
192388768458SSam Leffler {
192488768458SSam Leffler 	static char buf[256];
192588768458SSam Leffler 	char *p;
192688768458SSam Leffler 	struct secasindex *saidx = &sav->sah->saidx;
192788768458SSam Leffler 
19289ffa9677SSam Leffler 	IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
19299ffa9677SSam Leffler 		("address family mismatch"));
193088768458SSam Leffler 
193188768458SSam Leffler 	p = buf;
193288768458SSam Leffler 	snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
193388768458SSam Leffler 	while (p && *p)
193488768458SSam Leffler 		p++;
193588768458SSam Leffler 	/* NB: only use ipsec_address on one address at a time */
193688768458SSam Leffler 	snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
193788768458SSam Leffler 		ipsec_address(&saidx->src));
193888768458SSam Leffler 	while (p && *p)
193988768458SSam Leffler 		p++;
194088768458SSam Leffler 	snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
194188768458SSam Leffler 		ipsec_address(&saidx->dst));
194288768458SSam Leffler 
194388768458SSam Leffler 	return buf;
194488768458SSam Leffler }
194588768458SSam Leffler 
194688768458SSam Leffler void
194788768458SSam Leffler ipsec_dumpmbuf(m)
194888768458SSam Leffler 	struct mbuf *m;
194988768458SSam Leffler {
195088768458SSam Leffler 	int totlen;
195188768458SSam Leffler 	int i;
195288768458SSam Leffler 	u_char *p;
195388768458SSam Leffler 
195488768458SSam Leffler 	totlen = 0;
195588768458SSam Leffler 	printf("---\n");
195688768458SSam Leffler 	while (m) {
195788768458SSam Leffler 		p = mtod(m, u_char *);
195888768458SSam Leffler 		for (i = 0; i < m->m_len; i++) {
195988768458SSam Leffler 			printf("%02x ", p[i]);
196088768458SSam Leffler 			totlen++;
196188768458SSam Leffler 			if (totlen % 16 == 0)
196288768458SSam Leffler 				printf("\n");
196388768458SSam Leffler 		}
196488768458SSam Leffler 		m = m->m_next;
196588768458SSam Leffler 	}
196688768458SSam Leffler 	if (totlen % 16 != 0)
196788768458SSam Leffler 		printf("\n");
196888768458SSam Leffler 	printf("---\n");
196988768458SSam Leffler }
197088768458SSam Leffler 
19718381996eSSam Leffler static void
19728381996eSSam Leffler ipsec_attach(void)
19738381996eSSam Leffler {
1974603724d3SBjoern A. Zeeb 	SECPOLICY_LOCK_INIT(&V_ip4_def_policy);
19758381996eSSam Leffler 	ip4_def_policy.refcnt = 1;			/* NB: disallow free */
19768381996eSSam Leffler }
1977237fdd78SRobert Watson SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL);
19788381996eSSam Leffler 
19798381996eSSam Leffler 
198088768458SSam Leffler /* XXX this stuff doesn't belong here... */
198188768458SSam Leffler 
198288768458SSam Leffler static	struct xformsw* xforms = NULL;
198388768458SSam Leffler 
198488768458SSam Leffler /*
198588768458SSam Leffler  * Register a transform; typically at system startup.
198688768458SSam Leffler  */
198788768458SSam Leffler void
198888768458SSam Leffler xform_register(struct xformsw* xsp)
198988768458SSam Leffler {
199088768458SSam Leffler 	xsp->xf_next = xforms;
199188768458SSam Leffler 	xforms = xsp;
199288768458SSam Leffler }
199388768458SSam Leffler 
199488768458SSam Leffler /*
199588768458SSam Leffler  * Initialize transform support in an sav.
199688768458SSam Leffler  */
199788768458SSam Leffler int
199888768458SSam Leffler xform_init(struct secasvar *sav, int xftype)
199988768458SSam Leffler {
200088768458SSam Leffler 	struct xformsw *xsp;
200188768458SSam Leffler 
200282a6d6acSSam Leffler 	if (sav->tdb_xform != NULL)	/* previously initialized */
200382a6d6acSSam Leffler 		return 0;
200488768458SSam Leffler 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
200588768458SSam Leffler 		if (xsp->xf_type == xftype)
200688768458SSam Leffler 			return (*xsp->xf_init)(sav, xsp);
200788768458SSam Leffler 	return EINVAL;
200888768458SSam Leffler }
2009