Home | History | Annotate | Line # | Download | only in netipsec
ipsec.c revision 1.175
      1  1.175     ozaki /* $NetBSD: ipsec.c,v 1.175 2022/11/04 09:00:58 ozaki-r Exp $ */
      2  1.157      maxv /* $FreeBSD: ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $ */
      3  1.129      maxv /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
      4    1.1  jonathan 
      5    1.1  jonathan /*
      6    1.1  jonathan  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7    1.1  jonathan  * All rights reserved.
      8    1.1  jonathan  *
      9    1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10    1.1  jonathan  * modification, are permitted provided that the following conditions
     11    1.1  jonathan  * are met:
     12    1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13   1.26  degroote  *	notice, this list of conditions and the following disclaimer.
     14    1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.26  degroote  *	notice, this list of conditions and the following disclaimer in the
     16   1.26  degroote  *	documentation and/or other materials provided with the distribution.
     17    1.1  jonathan  * 3. Neither the name of the project nor the names of its contributors
     18   1.26  degroote  *	may be used to endorse or promote products derived from this software
     19   1.26  degroote  *	without specific prior written permission.
     20    1.1  jonathan  *
     21    1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22    1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23    1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24    1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25    1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26    1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27    1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28    1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29    1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30    1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31    1.1  jonathan  * SUCH DAMAGE.
     32    1.1  jonathan  */
     33    1.1  jonathan 
     34    1.1  jonathan #include <sys/cdefs.h>
     35  1.175     ozaki __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.175 2022/11/04 09:00:58 ozaki-r Exp $");
     36    1.1  jonathan 
     37    1.1  jonathan /*
     38    1.1  jonathan  * IPsec controller part.
     39    1.1  jonathan  */
     40    1.1  jonathan 
     41   1.71     ozaki #if defined(_KERNEL_OPT)
     42    1.1  jonathan #include "opt_inet.h"
     43    1.1  jonathan #include "opt_ipsec.h"
     44   1.71     ozaki #endif
     45    1.1  jonathan 
     46    1.1  jonathan #include <sys/param.h>
     47    1.1  jonathan #include <sys/systm.h>
     48    1.1  jonathan #include <sys/mbuf.h>
     49    1.1  jonathan #include <sys/domain.h>
     50    1.1  jonathan #include <sys/protosw.h>
     51    1.1  jonathan #include <sys/socket.h>
     52    1.1  jonathan #include <sys/socketvar.h>
     53    1.1  jonathan #include <sys/errno.h>
     54    1.1  jonathan #include <sys/time.h>
     55    1.1  jonathan #include <sys/kernel.h>
     56    1.1  jonathan #include <sys/syslog.h>
     57    1.1  jonathan #include <sys/sysctl.h>
     58    1.1  jonathan #include <sys/proc.h>
     59   1.44      elad #include <sys/kauth.h>
     60   1.90     ozaki #include <sys/cpu.h>
     61   1.90     ozaki #include <sys/kmem.h>
     62  1.113     ozaki #include <sys/pserialize.h>
     63    1.1  jonathan 
     64    1.1  jonathan #include <net/if.h>
     65    1.1  jonathan #include <net/route.h>
     66    1.1  jonathan 
     67    1.1  jonathan #include <netinet/in.h>
     68    1.1  jonathan #include <netinet/in_systm.h>
     69    1.1  jonathan #include <netinet/ip.h>
     70    1.1  jonathan #include <netinet/ip_var.h>
     71    1.1  jonathan #include <netinet/in_var.h>
     72    1.1  jonathan #include <netinet/udp.h>
     73    1.1  jonathan #include <netinet/udp_var.h>
     74    1.1  jonathan #include <netinet/tcp.h>
     75    1.1  jonathan #include <netinet/udp.h>
     76   1.38   mlelstv #include <netinet/ip_icmp.h>
     77   1.60     rmind #include <netinet/ip_private.h>
     78    1.1  jonathan 
     79    1.1  jonathan #include <netinet/ip6.h>
     80    1.1  jonathan #ifdef INET6
     81    1.1  jonathan #include <netinet6/ip6_var.h>
     82    1.1  jonathan #endif
     83    1.1  jonathan #include <netinet/in_pcb.h>
     84  1.165      maxv #include <netinet/in_offload.h>
     85    1.1  jonathan #ifdef INET6
     86    1.5  jonathan #include <netinet6/in6_pcb.h>
     87    1.1  jonathan #include <netinet/icmp6.h>
     88    1.1  jonathan #endif
     89    1.1  jonathan 
     90    1.1  jonathan #include <netipsec/ipsec.h>
     91   1.13  jonathan #include <netipsec/ipsec_var.h>
     92   1.37   thorpej #include <netipsec/ipsec_private.h>
     93    1.1  jonathan #ifdef INET6
     94    1.1  jonathan #include <netipsec/ipsec6.h>
     95    1.1  jonathan #endif
     96    1.1  jonathan #include <netipsec/ah_var.h>
     97    1.1  jonathan #include <netipsec/esp_var.h>
     98    1.1  jonathan #include <netipsec/ipcomp.h>		/*XXX*/
     99    1.1  jonathan #include <netipsec/ipcomp_var.h>
    100    1.1  jonathan 
    101    1.4       tls #include <netipsec/key.h>
    102    1.4       tls #include <netipsec/keydb.h>
    103    1.4       tls #include <netipsec/key_debug.h>
    104    1.1  jonathan 
    105    1.1  jonathan #include <netipsec/xform.h>
    106    1.1  jonathan 
    107   1.63  christos int ipsec_used = 0;
    108   1.63  christos int ipsec_enabled = 1;
    109   1.63  christos 
    110    1.1  jonathan #ifdef IPSEC_DEBUG
    111    1.1  jonathan int ipsec_debug = 1;
    112   1.21    rpaulo 
    113  1.129      maxv /*
    114   1.21    rpaulo  * When set to 1, IPsec will send packets with the same sequence number.
    115   1.21    rpaulo  * This allows to verify if the other side has proper replay attacks detection.
    116   1.21    rpaulo  */
    117   1.21    rpaulo int ipsec_replay = 0;
    118   1.21    rpaulo 
    119  1.129      maxv /*
    120   1.21    rpaulo  * When set 1, IPsec will send packets with corrupted HMAC.
    121   1.21    rpaulo  * This allows to verify if the other side properly detects modified packets.
    122   1.21    rpaulo  */
    123   1.21    rpaulo int ipsec_integrity = 0;
    124    1.1  jonathan #else
    125    1.1  jonathan int ipsec_debug = 0;
    126    1.1  jonathan #endif
    127    1.1  jonathan 
    128   1.37   thorpej percpu_t *ipsecstat_percpu;
    129  1.129      maxv 
    130    1.1  jonathan int ip4_ah_offsetmask = 0;	/* maybe IP_DF? */
    131   1.18  christos int ip4_ipsec_dfbit = 2;	/* DF bit on encap. 0: clear 1: set 2: copy */
    132    1.1  jonathan int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
    133    1.1  jonathan int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
    134    1.1  jonathan int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
    135    1.1  jonathan int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
    136    1.1  jonathan struct secpolicy ip4_def_policy;
    137    1.1  jonathan int ip4_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
    138    1.9   thorpej 
    139    1.9   thorpej u_int ipsec_spdgen = 1;		/* SPD generation # */
    140    1.9   thorpej 
    141  1.119     ozaki static struct secpolicy ipsec_dummy_sp __read_mostly = {
    142  1.119     ozaki 	.state		= IPSEC_SPSTATE_ALIVE,
    143  1.119     ozaki 	/* If ENTRUST, the dummy SP never be used. See ipsec_getpolicybysock. */
    144  1.119     ozaki 	.policy		= IPSEC_POLICY_ENTRUST,
    145  1.119     ozaki };
    146  1.119     ozaki 
    147  1.129      maxv static struct secpolicy *ipsec_checkpcbcache(struct mbuf *,
    148  1.129      maxv     struct inpcbpolicy *, int);
    149  1.129      maxv static int ipsec_fillpcbcache(struct inpcbpolicy *, struct mbuf *,
    150  1.129      maxv     struct secpolicy *, int);
    151  1.129      maxv static int ipsec_invalpcbcache(struct inpcbpolicy *, int);
    152    1.9   thorpej 
    153    1.1  jonathan /*
    154    1.1  jonathan  * Crypto support requirements:
    155    1.1  jonathan  *
    156    1.1  jonathan  *  1	require hardware support
    157    1.1  jonathan  * -1	require software support
    158    1.1  jonathan  *  0	take anything
    159    1.1  jonathan  */
    160  1.129      maxv int crypto_support = 0;
    161    1.1  jonathan 
    162    1.5  jonathan static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
    163  1.174     ozaki     struct inpcb *, int *);
    164    1.5  jonathan 
    165    1.1  jonathan #ifdef INET6
    166    1.1  jonathan int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
    167    1.1  jonathan int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
    168    1.1  jonathan int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
    169    1.1  jonathan int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
    170    1.5  jonathan struct secpolicy ip6_def_policy;
    171    1.1  jonathan int ip6_ipsec_ecn = 0;		/* ECN ignore(-1)/forbidden(0)/allowed(1) */
    172  1.129      maxv #endif
    173    1.1  jonathan 
    174  1.174     ozaki static int ipsec_setspidx_inpcb(struct mbuf *, struct inpcb *);
    175  1.169      maxv static int ipsec_setspidx(struct mbuf *, struct secpolicyindex *, int, int);
    176  1.129      maxv static void ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
    177  1.129      maxv static int ipsec4_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
    178    1.1  jonathan #ifdef INET6
    179  1.129      maxv static void ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
    180  1.129      maxv static int ipsec6_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
    181    1.1  jonathan #endif
    182  1.129      maxv static void ipsec_delpcbpolicy(struct inpcbpolicy *);
    183  1.113     ozaki static void ipsec_destroy_policy(struct secpolicy *);
    184  1.132      maxv static int ipsec_sp_reject(const struct secpolicy *, const struct mbuf *);
    185  1.129      maxv static void vshiftl(unsigned char *, int, int);
    186  1.132      maxv static size_t ipsec_sp_hdrsiz(const struct secpolicy *, const struct mbuf *);
    187    1.1  jonathan 
    188    1.9   thorpej /*
    189    1.9   thorpej  * Try to validate and use cached policy on a PCB.
    190    1.9   thorpej  */
    191    1.9   thorpej static struct secpolicy *
    192    1.9   thorpej ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
    193    1.9   thorpej {
    194    1.9   thorpej 	struct secpolicyindex spidx;
    195  1.113     ozaki 	struct secpolicy *sp = NULL;
    196  1.113     ozaki 	int s;
    197    1.9   thorpej 
    198   1.81     ozaki 	KASSERT(IPSEC_DIR_IS_VALID(dir));
    199   1.76     ozaki 	KASSERT(pcbsp != NULL);
    200   1.94     ozaki 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    201  1.174     ozaki 	KASSERT(inp_locked(pcbsp->sp_inp));
    202   1.76     ozaki 
    203  1.113     ozaki 	/*
    204  1.113     ozaki 	 * Checking the generation and sp->state and taking a reference to an SP
    205  1.113     ozaki 	 * must be in a critical section of pserialize. See key_unlink_sp.
    206  1.113     ozaki 	 */
    207  1.113     ozaki 	s = pserialize_read_enter();
    208    1.9   thorpej 	/* SPD table change invalidate all the caches. */
    209    1.9   thorpej 	if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
    210    1.9   thorpej 		ipsec_invalpcbcache(pcbsp, dir);
    211  1.113     ozaki 		goto out;
    212    1.9   thorpej 	}
    213  1.113     ozaki 	sp = pcbsp->sp_cache[dir].cachesp;
    214  1.113     ozaki 	if (sp == NULL)
    215  1.113     ozaki 		goto out;
    216  1.113     ozaki 	if (sp->state != IPSEC_SPSTATE_ALIVE) {
    217  1.113     ozaki 		sp = NULL;
    218    1.9   thorpej 		ipsec_invalpcbcache(pcbsp, dir);
    219  1.113     ozaki 		goto out;
    220    1.9   thorpej 	}
    221    1.9   thorpej 	if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
    222  1.113     ozaki 		/* NB: assume ipsec_setspidx never sleep */
    223  1.169      maxv 		if (ipsec_setspidx(m, &spidx, dir, 1) != 0) {
    224  1.113     ozaki 			sp = NULL;
    225  1.113     ozaki 			goto out;
    226  1.113     ozaki 		}
    227   1.29  degroote 
    228   1.29  degroote 		/*
    229   1.29  degroote 		 * We have to make an exact match here since the cached rule
    230   1.29  degroote 		 * might have lower priority than a rule that would otherwise
    231  1.129      maxv 		 * have matched the packet.
    232   1.29  degroote 		 */
    233  1.113     ozaki 		if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
    234  1.113     ozaki 		    sizeof(spidx))) {
    235  1.113     ozaki 			sp = NULL;
    236  1.113     ozaki 			goto out;
    237  1.113     ozaki 		}
    238    1.9   thorpej 	} else {
    239    1.9   thorpej 		/*
    240    1.9   thorpej 		 * The pcb is connected, and the L4 code is sure that:
    241    1.9   thorpej 		 * - outgoing side uses inp_[lf]addr
    242    1.9   thorpej 		 * - incoming side looks up policy after inpcb lookup
    243    1.9   thorpej 		 * and address pair is know to be stable.  We do not need
    244    1.9   thorpej 		 * to generate spidx again, nor check the address match again.
    245    1.9   thorpej 		 *
    246    1.9   thorpej 		 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
    247  1.175     ozaki 		 * and there are calls to ipsec_pcbconn() from inpcb_connect().
    248    1.9   thorpej 		 */
    249    1.9   thorpej 	}
    250    1.9   thorpej 
    251  1.113     ozaki 	sp->lastused = time_second;
    252  1.113     ozaki 	KEY_SP_REF(sp);
    253   1.77     ozaki 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    254   1.77     ozaki 	    "DP cause refcnt++:%d SP:%p\n",
    255  1.113     ozaki 	    key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
    256  1.113     ozaki out:
    257  1.113     ozaki 	pserialize_read_exit(s);
    258  1.113     ozaki 	return sp;
    259    1.9   thorpej }
    260    1.9   thorpej 
    261    1.9   thorpej static int
    262    1.9   thorpej ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
    263  1.129      maxv     struct secpolicy *sp, int dir)
    264    1.9   thorpej {
    265    1.9   thorpej 
    266   1.81     ozaki 	KASSERT(IPSEC_DIR_IS_INOROUT(dir));
    267   1.94     ozaki 	KASSERT(dir < __arraycount(pcbsp->sp_cache));
    268  1.174     ozaki 	KASSERT(inp_locked(pcbsp->sp_inp));
    269    1.9   thorpej 
    270    1.9   thorpej 	pcbsp->sp_cache[dir].cachesp = NULL;
    271   1.98     ozaki 	pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
    272  1.169      maxv 	if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, dir, 1) != 0) {
    273    1.9   thorpej 		return EINVAL;
    274    1.9   thorpej 	}
    275    1.9   thorpej 	pcbsp->sp_cache[dir].cachesp = sp;
    276    1.9   thorpej 	if (pcbsp->sp_cache[dir].cachesp) {
    277    1.9   thorpej 		/*
    278    1.9   thorpej 		 * If the PCB is connected, we can remember a hint to
    279    1.9   thorpej 		 * possibly short-circuit IPsec processing in other places.
    280    1.9   thorpej 		 */
    281    1.9   thorpej 		if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
    282    1.9   thorpej 			switch (pcbsp->sp_cache[dir].cachesp->policy) {
    283    1.9   thorpej 			case IPSEC_POLICY_NONE:
    284    1.9   thorpej 			case IPSEC_POLICY_BYPASS:
    285    1.9   thorpej 				pcbsp->sp_cache[dir].cachehint =
    286  1.100     ozaki 				    IPSEC_PCBHINT_NO;
    287    1.9   thorpej 				break;
    288    1.9   thorpej 			default:
    289    1.9   thorpej 				pcbsp->sp_cache[dir].cachehint =
    290  1.100     ozaki 				    IPSEC_PCBHINT_YES;
    291    1.9   thorpej 			}
    292    1.9   thorpej 		}
    293    1.9   thorpej 	}
    294    1.9   thorpej 	pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
    295    1.9   thorpej 
    296    1.9   thorpej 	return 0;
    297    1.9   thorpej }
    298    1.9   thorpej 
    299    1.9   thorpej static int
    300    1.9   thorpej ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
    301    1.9   thorpej {
    302    1.9   thorpej 	int i;
    303    1.9   thorpej 
    304  1.174     ozaki 	KASSERT(inp_locked(pcbsp->sp_inp));
    305   1.84     ozaki 
    306    1.9   thorpej 	for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
    307    1.9   thorpej 		if (dir != IPSEC_DIR_ANY && i != dir)
    308    1.9   thorpej 			continue;
    309    1.9   thorpej 		pcbsp->sp_cache[i].cachesp = NULL;
    310   1.98     ozaki 		pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
    311    1.9   thorpej 		pcbsp->sp_cache[i].cachegen = 0;
    312   1.41    cegger 		memset(&pcbsp->sp_cache[i].cacheidx, 0,
    313  1.100     ozaki 		    sizeof(pcbsp->sp_cache[i].cacheidx));
    314    1.9   thorpej 	}
    315    1.9   thorpej 	return 0;
    316    1.9   thorpej }
    317    1.9   thorpej 
    318    1.9   thorpej void
    319    1.9   thorpej ipsec_pcbconn(struct inpcbpolicy *pcbsp)
    320    1.9   thorpej {
    321    1.9   thorpej 
    322  1.174     ozaki 	KASSERT(inp_locked(pcbsp->sp_inp));
    323   1.84     ozaki 
    324    1.9   thorpej 	pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
    325    1.9   thorpej 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    326    1.9   thorpej }
    327    1.9   thorpej 
    328    1.9   thorpej void
    329    1.9   thorpej ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
    330    1.9   thorpej {
    331    1.9   thorpej 
    332  1.174     ozaki 	KASSERT(inp_locked(pcbsp->sp_inp));
    333   1.84     ozaki 
    334    1.9   thorpej 	pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
    335    1.9   thorpej 	ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
    336    1.9   thorpej }
    337    1.9   thorpej 
    338    1.9   thorpej void
    339    1.9   thorpej ipsec_invalpcbcacheall(void)
    340    1.9   thorpej {
    341    1.9   thorpej 
    342    1.9   thorpej 	if (ipsec_spdgen == UINT_MAX)
    343    1.9   thorpej 		ipsec_spdgen = 1;
    344    1.9   thorpej 	else
    345    1.9   thorpej 		ipsec_spdgen++;
    346    1.9   thorpej }
    347    1.9   thorpej 
    348    1.1  jonathan /*
    349    1.1  jonathan  * Return a held reference to the default SP.
    350    1.1  jonathan  */
    351    1.1  jonathan static struct secpolicy *
    352  1.101     ozaki key_get_default_sp(int af, const char *where, int tag)
    353    1.1  jonathan {
    354    1.1  jonathan 	struct secpolicy *sp;
    355    1.1  jonathan 
    356   1.77     ozaki 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
    357    1.1  jonathan 
    358   1.78     ozaki 	switch(af) {
    359   1.78     ozaki 	case AF_INET:
    360   1.78     ozaki 		sp = &ip4_def_policy;
    361   1.78     ozaki 		break;
    362   1.31  degroote #ifdef INET6
    363   1.78     ozaki 	case AF_INET6:
    364   1.78     ozaki 		sp = &ip6_def_policy;
    365   1.78     ozaki 		break;
    366   1.31  degroote #endif
    367   1.78     ozaki 	default:
    368   1.77     ozaki 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    369   1.77     ozaki 		    "unexpected protocol family %u\n", af);
    370   1.78     ozaki 		return NULL;
    371   1.78     ozaki 	}
    372   1.31  degroote 
    373    1.1  jonathan 	if (sp->policy != IPSEC_POLICY_DISCARD &&
    374  1.132      maxv 	    sp->policy != IPSEC_POLICY_NONE) {
    375   1.92     ozaki 		IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
    376   1.92     ozaki 		    sp->policy, IPSEC_POLICY_NONE);
    377    1.1  jonathan 		sp->policy = IPSEC_POLICY_NONE;
    378    1.1  jonathan 	}
    379   1.95     ozaki 	KEY_SP_REF(sp);
    380    1.1  jonathan 
    381   1.77     ozaki 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
    382  1.111     ozaki 	    sp, key_sp_refcnt(sp));
    383    1.1  jonathan 	return sp;
    384    1.1  jonathan }
    385  1.129      maxv 
    386  1.101     ozaki #define	KEY_GET_DEFAULT_SP(af) \
    387  1.101     ozaki 	key_get_default_sp((af), __func__, __LINE__)
    388    1.1  jonathan 
    389    1.1  jonathan /*
    390    1.1  jonathan  * For OUTBOUND packet having a socket. Searching SPD for packet,
    391    1.1  jonathan  * and return a pointer to SP.
    392  1.152      maxv  * OUT:	NULL:	no appropriate SP found, the following value is set to error.
    393    1.1  jonathan  *		0	: bypass
    394    1.1  jonathan  *		EACCES	: discard packet.
    395    1.1  jonathan  *		ENOENT	: ipsec_acquire() in progress, maybe.
    396    1.7       wiz  *		others	: error occurred.
    397    1.1  jonathan  *	others:	a pointer to SP
    398    1.1  jonathan  *
    399   1.20       wiz  * NOTE: IPv6 mapped address concern is implemented here.
    400    1.1  jonathan  */
    401    1.5  jonathan static struct secpolicy *
    402  1.174     ozaki ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp,
    403   1.75     ozaki     int *error)
    404    1.1  jonathan {
    405    1.1  jonathan 	struct inpcbpolicy *pcbsp = NULL;
    406    1.1  jonathan 	struct secpolicy *currsp = NULL;	/* policy on socket */
    407    1.1  jonathan 	struct secpolicy *sp;
    408    1.1  jonathan 	int af;
    409    1.1  jonathan 
    410   1.73     ozaki 	KASSERT(m != NULL);
    411  1.174     ozaki 	KASSERT(inp != NULL);
    412   1.73     ozaki 	KASSERT(error != NULL);
    413   1.81     ozaki 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    414    1.1  jonathan 
    415  1.174     ozaki 	KASSERT(inp->inp_socket != NULL);
    416  1.174     ozaki 	KASSERT(inp_locked(inp));
    417    1.5  jonathan 
    418  1.174     ozaki 	/* XXX FIXME inpcb vs socket*/
    419  1.174     ozaki 	af = inp->inp_af;
    420   1.73     ozaki 	KASSERTMSG(af == AF_INET || af == AF_INET6,
    421   1.73     ozaki 	    "unexpected protocol family %u", af);
    422    1.1  jonathan 
    423  1.174     ozaki 	KASSERT(inp->inp_sp != NULL);
    424    1.9   thorpej 	/* If we have a cached entry, and if it is still valid, use it. */
    425   1.37   thorpej 	IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
    426  1.174     ozaki 	currsp = ipsec_checkpcbcache(m, inp->inp_sp, dir);
    427    1.9   thorpej 	if (currsp) {
    428    1.9   thorpej 		*error = 0;
    429    1.9   thorpej 		return currsp;
    430    1.9   thorpej 	}
    431   1.37   thorpej 	IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
    432    1.9   thorpej 
    433    1.1  jonathan 	switch (af) {
    434  1.150      maxv 	case AF_INET:
    435    1.5  jonathan #if defined(INET6)
    436  1.150      maxv 	case AF_INET6:
    437  1.150      maxv #endif
    438  1.174     ozaki 		*error = ipsec_setspidx_inpcb(m, inp);
    439  1.174     ozaki 		pcbsp = inp->inp_sp;
    440    1.1  jonathan 		break;
    441    1.1  jonathan 	default:
    442    1.1  jonathan 		*error = EPFNOSUPPORT;
    443    1.1  jonathan 		break;
    444    1.1  jonathan 	}
    445    1.1  jonathan 	if (*error)
    446    1.1  jonathan 		return NULL;
    447    1.1  jonathan 
    448   1.73     ozaki 	KASSERT(pcbsp != NULL);
    449    1.1  jonathan 	switch (dir) {
    450    1.1  jonathan 	case IPSEC_DIR_INBOUND:
    451    1.1  jonathan 		currsp = pcbsp->sp_in;
    452    1.1  jonathan 		break;
    453    1.1  jonathan 	case IPSEC_DIR_OUTBOUND:
    454    1.1  jonathan 		currsp = pcbsp->sp_out;
    455    1.1  jonathan 		break;
    456    1.1  jonathan 	}
    457   1.73     ozaki 	KASSERT(currsp != NULL);
    458    1.1  jonathan 
    459  1.151      maxv 	if (pcbsp->priv) {	/* when privileged socket */
    460    1.1  jonathan 		switch (currsp->policy) {
    461    1.1  jonathan 		case IPSEC_POLICY_BYPASS:
    462    1.1  jonathan 		case IPSEC_POLICY_IPSEC:
    463   1.95     ozaki 			KEY_SP_REF(currsp);
    464    1.1  jonathan 			sp = currsp;
    465    1.1  jonathan 			break;
    466    1.1  jonathan 
    467    1.1  jonathan 		case IPSEC_POLICY_ENTRUST:
    468    1.1  jonathan 			/* look for a policy in SPD */
    469  1.170  knakahar 			if (key_havesp(dir))
    470  1.170  knakahar 				sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    471  1.170  knakahar 			else
    472  1.170  knakahar 				sp = NULL;
    473    1.1  jonathan 			if (sp == NULL)		/* no SP found */
    474  1.101     ozaki 				sp = KEY_GET_DEFAULT_SP(af);
    475    1.1  jonathan 			break;
    476    1.1  jonathan 
    477    1.1  jonathan 		default:
    478   1.92     ozaki 			IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
    479   1.92     ozaki 			    currsp->policy);
    480    1.1  jonathan 			*error = EINVAL;
    481    1.1  jonathan 			return NULL;
    482    1.1  jonathan 		}
    483    1.1  jonathan 	} else {				/* unpriv, SPD has policy */
    484  1.170  knakahar 		if (key_havesp(dir))
    485  1.170  knakahar 			sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
    486  1.170  knakahar 		else
    487  1.170  knakahar 			sp = NULL;
    488    1.1  jonathan 		if (sp == NULL) {		/* no SP found */
    489    1.1  jonathan 			switch (currsp->policy) {
    490    1.1  jonathan 			case IPSEC_POLICY_BYPASS:
    491   1.92     ozaki 				IPSECLOG(LOG_ERR, "Illegal policy for "
    492   1.92     ozaki 				    "non-priviliged defined %d\n",
    493   1.92     ozaki 				    currsp->policy);
    494    1.1  jonathan 				*error = EINVAL;
    495    1.1  jonathan 				return NULL;
    496    1.1  jonathan 
    497    1.1  jonathan 			case IPSEC_POLICY_ENTRUST:
    498  1.101     ozaki 				sp = KEY_GET_DEFAULT_SP(af);
    499    1.1  jonathan 				break;
    500    1.1  jonathan 
    501    1.1  jonathan 			case IPSEC_POLICY_IPSEC:
    502   1.95     ozaki 				KEY_SP_REF(currsp);
    503    1.1  jonathan 				sp = currsp;
    504    1.1  jonathan 				break;
    505    1.1  jonathan 
    506    1.1  jonathan 			default:
    507   1.92     ozaki 				IPSECLOG(LOG_ERR, "Invalid policy for "
    508   1.92     ozaki 				    "PCB %d\n", currsp->policy);
    509    1.1  jonathan 				*error = EINVAL;
    510    1.1  jonathan 				return NULL;
    511    1.1  jonathan 			}
    512    1.1  jonathan 		}
    513    1.1  jonathan 	}
    514   1.73     ozaki 	KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
    515   1.73     ozaki 	    currsp->policy);
    516   1.77     ozaki 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
    517   1.77     ozaki 	    "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
    518  1.111     ozaki 	    pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
    519    1.9   thorpej 	ipsec_fillpcbcache(pcbsp, m, sp, dir);
    520    1.1  jonathan 	return sp;
    521    1.1  jonathan }
    522    1.1  jonathan 
    523    1.1  jonathan /*
    524  1.132      maxv  * For FORWARDING packet or OUTBOUND without a socket. Searching SPD for packet,
    525    1.1  jonathan  * and return a pointer to SP.
    526    1.1  jonathan  * OUT:	positive: a pointer to the entry for security policy leaf matched.
    527  1.152      maxv  *	NULL:	no appropriate SP found, the following value is set to error.
    528    1.1  jonathan  *		0	: bypass
    529    1.1  jonathan  *		EACCES	: discard packet.
    530    1.1  jonathan  *		ENOENT	: ipsec_acquire() in progress, maybe.
    531    1.7       wiz  *		others	: error occurred.
    532    1.1  jonathan  */
    533  1.131      maxv static struct secpolicy *
    534   1.33  degroote ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
    535    1.1  jonathan {
    536    1.1  jonathan 	struct secpolicyindex spidx;
    537    1.1  jonathan 	struct secpolicy *sp;
    538    1.1  jonathan 
    539   1.73     ozaki 	KASSERT(m != NULL);
    540   1.73     ozaki 	KASSERT(error != NULL);
    541   1.81     ozaki 	KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
    542    1.1  jonathan 
    543    1.1  jonathan 	sp = NULL;
    544   1.32  degroote 
    545   1.32  degroote 	/* Make an index to look for a policy. */
    546  1.169      maxv 	*error = ipsec_setspidx(m, &spidx, dir, (flag & IP_FORWARDING) ? 0 : 1);
    547   1.32  degroote 	if (*error != 0) {
    548   1.92     ozaki 		IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
    549  1.129      maxv 		memset(&spidx, 0, sizeof(spidx));
    550   1.32  degroote 		return NULL;
    551   1.32  degroote 	}
    552   1.32  degroote 
    553   1.32  degroote 	spidx.dir = dir;
    554   1.32  degroote 
    555    1.1  jonathan 	if (key_havesp(dir)) {
    556  1.101     ozaki 		sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
    557    1.1  jonathan 	}
    558  1.154      maxv 	if (sp == NULL) {
    559  1.154      maxv 		/* no SP found, use system default */
    560  1.154      maxv 		sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
    561  1.154      maxv 	}
    562   1.32  degroote 
    563   1.73     ozaki 	KASSERT(sp != NULL);
    564    1.1  jonathan 	return sp;
    565    1.1  jonathan }
    566    1.1  jonathan 
    567  1.131      maxv static struct secpolicy *
    568  1.137      maxv ipsec_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
    569  1.174     ozaki     struct inpcb *inp)
    570    1.1  jonathan {
    571    1.1  jonathan 	struct secpolicy *sp;
    572    1.1  jonathan 
    573    1.1  jonathan 	*error = 0;
    574    1.5  jonathan 
    575   1.82     ozaki 	if (inp == NULL) {
    576    1.1  jonathan 		sp = ipsec_getpolicybyaddr(m, dir, flag, error);
    577   1.82     ozaki 	} else {
    578  1.174     ozaki 		KASSERT(inp->inp_socket != NULL);
    579  1.174     ozaki 		sp = ipsec_getpolicybysock(m, dir, inp, error);
    580   1.82     ozaki 	}
    581    1.1  jonathan 	if (sp == NULL) {
    582   1.73     ozaki 		KASSERTMSG(*error != 0, "getpolicy failed w/o error");
    583   1.37   thorpej 		IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
    584    1.1  jonathan 		return NULL;
    585    1.1  jonathan 	}
    586   1.73     ozaki 	KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
    587  1.135      maxv 
    588    1.1  jonathan 	switch (sp->policy) {
    589    1.1  jonathan 	case IPSEC_POLICY_ENTRUST:
    590    1.1  jonathan 	default:
    591   1.62  christos 		printf("%s: invalid policy %u\n", __func__, sp->policy);
    592    1.1  jonathan 		/* fall thru... */
    593    1.1  jonathan 	case IPSEC_POLICY_DISCARD:
    594   1.37   thorpej 		IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
    595    1.1  jonathan 		*error = -EINVAL;	/* packet is discarded by caller */
    596    1.1  jonathan 		break;
    597    1.1  jonathan 	case IPSEC_POLICY_BYPASS:
    598    1.1  jonathan 	case IPSEC_POLICY_NONE:
    599  1.113     ozaki 		KEY_SP_UNREF(&sp);
    600    1.1  jonathan 		sp = NULL;		/* NB: force NULL result */
    601    1.1  jonathan 		break;
    602    1.1  jonathan 	case IPSEC_POLICY_IPSEC:
    603   1.93     ozaki 		KASSERT(sp->req != NULL);
    604    1.1  jonathan 		break;
    605    1.1  jonathan 	}
    606  1.135      maxv 
    607    1.1  jonathan 	if (*error != 0) {
    608  1.113     ozaki 		KEY_SP_UNREF(&sp);
    609    1.1  jonathan 		sp = NULL;
    610   1.92     ozaki 		IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
    611    1.1  jonathan 	}
    612  1.135      maxv 
    613    1.1  jonathan 	return sp;
    614    1.1  jonathan }
    615    1.1  jonathan 
    616   1.59     rmind int
    617   1.70     ozaki ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
    618  1.171     ozaki     u_long *mtu, bool *natt_frag, bool *done, bool *count_drop)
    619   1.59     rmind {
    620   1.59     rmind 	struct secpolicy *sp = NULL;
    621  1.151      maxv 	u_long _mtu = 0;
    622   1.59     rmind 	int error, s;
    623   1.59     rmind 
    624   1.59     rmind 	/*
    625   1.59     rmind 	 * Check the security policy (SP) for the packet and, if required,
    626   1.59     rmind 	 * do IPsec-related processing.  There are two cases here; the first
    627   1.59     rmind 	 * time a packet is sent through it will be untagged and handled by
    628  1.137      maxv 	 * ipsec_checkpolicy().  If the packet is resubmitted to ip_output
    629   1.59     rmind 	 * (e.g. after AH, ESP, etc. processing), there will be a tag to
    630   1.59     rmind 	 * bypass the lookup and related policy checking.
    631   1.59     rmind 	 */
    632   1.59     rmind 	if (ipsec_outdone(m)) {
    633   1.59     rmind 		return 0;
    634   1.59     rmind 	}
    635   1.59     rmind 	s = splsoftnet();
    636   1.99     ozaki 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
    637   1.59     rmind 		splx(s);
    638   1.59     rmind 		return 0;
    639   1.59     rmind 	}
    640  1.137      maxv 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
    641   1.59     rmind 
    642   1.59     rmind 	/*
    643   1.59     rmind 	 * There are four return cases:
    644  1.151      maxv 	 *	sp != NULL                    apply IPsec policy
    645  1.151      maxv 	 *	sp == NULL, error == 0        no IPsec handling needed
    646  1.151      maxv 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
    647  1.151      maxv 	 *	sp == NULL, error != 0        discard packet, report error
    648   1.59     rmind 	 */
    649   1.59     rmind 	if (sp == NULL) {
    650   1.59     rmind 		splx(s);
    651   1.59     rmind 		if (error) {
    652   1.59     rmind 			/*
    653   1.59     rmind 			 * Hack: -EINVAL is used to signal that a packet
    654   1.59     rmind 			 * should be silently discarded.  This is typically
    655   1.59     rmind 			 * because we asked key management for an SA and
    656   1.59     rmind 			 * it was delayed (e.g. kicked up to IKE).
    657   1.59     rmind 			 */
    658   1.59     rmind 			if (error == -EINVAL)
    659   1.59     rmind 				error = 0;
    660   1.59     rmind 			m_freem(m);
    661   1.59     rmind 			*done = true;
    662  1.171     ozaki 			*count_drop = true;
    663   1.59     rmind 			return error;
    664   1.59     rmind 		}
    665   1.59     rmind 		/* No IPsec processing for this packet. */
    666   1.59     rmind 		return 0;
    667   1.59     rmind 	}
    668   1.59     rmind 
    669   1.59     rmind 	/*
    670   1.59     rmind 	 * Do delayed checksums now because we send before
    671   1.59     rmind 	 * this is done in the normal processing path.
    672   1.59     rmind 	 */
    673   1.59     rmind 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    674  1.165      maxv 		in_undefer_cksum_tcpudp(m);
    675   1.59     rmind 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    676   1.59     rmind 	}
    677   1.59     rmind 
    678  1.108     ozaki 	error = ipsec4_process_packet(m, sp->req, &_mtu);
    679  1.108     ozaki 	if (error == 0 && _mtu != 0) {
    680  1.108     ozaki 		/*
    681  1.108     ozaki 		 * NAT-T ESP fragmentation: do not do IPSec processing
    682  1.108     ozaki 		 * now, we will do it on each fragmented packet.
    683  1.108     ozaki 		 */
    684  1.108     ozaki 		*mtu = _mtu;
    685  1.108     ozaki 		*natt_frag = true;
    686  1.113     ozaki 		KEY_SP_UNREF(&sp);
    687  1.108     ozaki 		splx(s);
    688  1.108     ozaki 		return 0;
    689  1.112     ozaki 	}
    690  1.135      maxv 
    691   1.59     rmind 	/*
    692   1.59     rmind 	 * Preserve KAME behaviour: ENOENT can be returned
    693   1.59     rmind 	 * when an SA acquire is in progress.  Don't propagate
    694   1.59     rmind 	 * this to user-level; it confuses applications.
    695   1.59     rmind 	 *
    696   1.59     rmind 	 * XXX this will go away when the SADB is redone.
    697   1.59     rmind 	 */
    698   1.59     rmind 	if (error == ENOENT)
    699   1.59     rmind 		error = 0;
    700  1.113     ozaki 	KEY_SP_UNREF(&sp);
    701   1.59     rmind 	splx(s);
    702   1.59     rmind 	*done = true;
    703   1.59     rmind 	return error;
    704   1.59     rmind }
    705   1.59     rmind 
    706   1.60     rmind int
    707  1.172     ozaki ipsec_ip_input_checkpolicy(struct mbuf *m, bool forward)
    708   1.60     rmind {
    709   1.60     rmind 	struct secpolicy *sp;
    710   1.60     rmind 	int error, s;
    711   1.60     rmind 
    712   1.60     rmind 	s = splsoftnet();
    713  1.141      maxv 	error = ipsec_in_reject(m, NULL);
    714   1.60     rmind 	splx(s);
    715   1.60     rmind 	if (error) {
    716  1.141      maxv 		return EINVAL;
    717   1.60     rmind 	}
    718   1.60     rmind 
    719  1.164      maxv 	if (!forward || !(m->m_flags & M_CANFASTFWD)) {
    720   1.60     rmind 		return 0;
    721   1.60     rmind 	}
    722   1.60     rmind 
    723  1.104     ozaki 	/*
    724  1.104     ozaki 	 * Peek at the outbound SP for this packet to determine if
    725  1.104     ozaki 	 * it is a Fast Forward candidate.
    726  1.104     ozaki 	 */
    727   1.60     rmind 	s = splsoftnet();
    728  1.164      maxv 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
    729  1.164      maxv 	    &error, NULL);
    730   1.60     rmind 	if (sp != NULL) {
    731   1.60     rmind 		m->m_flags &= ~M_CANFASTFWD;
    732  1.113     ozaki 		KEY_SP_UNREF(&sp);
    733   1.60     rmind 	}
    734   1.60     rmind 	splx(s);
    735  1.164      maxv 
    736   1.60     rmind 	return 0;
    737   1.60     rmind }
    738   1.60     rmind 
    739  1.154      maxv /*
    740  1.154      maxv  * If the packet is routed over IPsec tunnel, tell the originator the
    741  1.154      maxv  * tunnel MTU.
    742  1.154      maxv  *     tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
    743  1.154      maxv  *
    744  1.154      maxv  * XXX: Quick hack!!!
    745  1.154      maxv  *
    746  1.154      maxv  * XXX: And what if the MTU goes negative?
    747  1.154      maxv  */
    748  1.162      maxv void
    749  1.162      maxv ipsec_mtu(struct mbuf *m, int *destmtu)
    750   1.60     rmind {
    751   1.60     rmind 	struct secpolicy *sp;
    752   1.60     rmind 	size_t ipsechdr;
    753   1.60     rmind 	int error;
    754   1.60     rmind 
    755  1.135      maxv 	sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
    756  1.135      maxv 	    &error);
    757   1.60     rmind 	if (sp == NULL) {
    758  1.162      maxv 		return;
    759   1.60     rmind 	}
    760   1.60     rmind 
    761   1.60     rmind 	/* Count IPsec header size. */
    762  1.142      maxv 	ipsechdr = ipsec_sp_hdrsiz(sp, m);
    763   1.60     rmind 
    764   1.60     rmind 	/*
    765  1.162      maxv 	 * Find the correct route for outer IP header, compute tunnel MTU.
    766   1.60     rmind 	 */
    767  1.106     ozaki 	if (sp->req) {
    768  1.121     ozaki 		struct secasvar *sav;
    769   1.60     rmind 
    770  1.121     ozaki 		sav = ipsec_lookup_sa(sp->req, m);
    771  1.121     ozaki 		if (sav != NULL) {
    772  1.121     ozaki 			struct route *ro;
    773  1.121     ozaki 			struct rtentry *rt;
    774  1.121     ozaki 
    775  1.121     ozaki 			ro = &sav->sah->sa_route;
    776  1.121     ozaki 			rt = rtcache_validate(ro);
    777  1.121     ozaki 			if (rt && rt->rt_ifp) {
    778  1.121     ozaki 				*destmtu = rt->rt_rmx.rmx_mtu ?
    779  1.121     ozaki 				    rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
    780  1.121     ozaki 				*destmtu -= ipsechdr;
    781  1.121     ozaki 			}
    782  1.121     ozaki 			rtcache_unref(rt, ro);
    783  1.121     ozaki 			KEY_SA_UNREF(&sav);
    784   1.60     rmind 		}
    785   1.60     rmind 	}
    786  1.113     ozaki 	KEY_SP_UNREF(&sp);
    787   1.60     rmind }
    788   1.60     rmind 
    789    1.1  jonathan static int
    790  1.174     ozaki ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp)
    791    1.1  jonathan {
    792    1.1  jonathan 	int error;
    793    1.1  jonathan 
    794  1.174     ozaki 	KASSERT(inp != NULL);
    795  1.174     ozaki 	KASSERT(inp->inp_sp != NULL);
    796  1.174     ozaki 	KASSERT(inp->inp_sp->sp_out != NULL);
    797  1.174     ozaki 	KASSERT(inp->inp_sp->sp_in != NULL);
    798    1.1  jonathan 
    799  1.174     ozaki 	error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx,
    800  1.169      maxv 	    IPSEC_DIR_INBOUND, 1);
    801    1.1  jonathan 	if (error == 0) {
    802  1.174     ozaki 		inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
    803  1.174     ozaki 		inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
    804    1.1  jonathan 	} else {
    805  1.174     ozaki 		memset(&inp->inp_sp->sp_in->spidx, 0,
    806  1.174     ozaki 		    sizeof(inp->inp_sp->sp_in->spidx));
    807  1.174     ozaki 		memset(&inp->inp_sp->sp_out->spidx, 0,
    808  1.174     ozaki 		    sizeof(inp->inp_sp->sp_out->spidx));
    809    1.1  jonathan 	}
    810    1.1  jonathan 	return error;
    811    1.1  jonathan }
    812    1.1  jonathan 
    813    1.1  jonathan /*
    814    1.1  jonathan  * configure security policy index (src/dst/proto/sport/dport)
    815    1.1  jonathan  * by looking at the content of mbuf.
    816    1.1  jonathan  * the caller is responsible for error recovery (like clearing up spidx).
    817    1.1  jonathan  */
    818    1.1  jonathan static int
    819  1.169      maxv ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int dir,
    820  1.169      maxv     int needport)
    821    1.1  jonathan {
    822    1.1  jonathan 	struct ip *ip = NULL;
    823    1.1  jonathan 	struct ip ipbuf;
    824    1.1  jonathan 	u_int v;
    825    1.1  jonathan 	int error;
    826    1.1  jonathan 
    827   1.73     ozaki 	KASSERT(m != NULL);
    828  1.163      maxv 	M_VERIFY_PACKET(m);
    829    1.1  jonathan 
    830    1.1  jonathan 	if (m->m_pkthdr.len < sizeof(struct ip)) {
    831   1.77     ozaki 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    832   1.77     ozaki 		    "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
    833   1.77     ozaki 		    m->m_pkthdr.len);
    834    1.1  jonathan 		return EINVAL;
    835    1.1  jonathan 	}
    836    1.1  jonathan 
    837  1.169      maxv 	memset(spidx, 0, sizeof(*spidx));
    838  1.169      maxv 	spidx->dir = dir;
    839  1.169      maxv 
    840  1.129      maxv 	if (m->m_len >= sizeof(*ip)) {
    841    1.1  jonathan 		ip = mtod(m, struct ip *);
    842  1.129      maxv 	} else {
    843   1.28  degroote 		m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
    844    1.1  jonathan 		ip = &ipbuf;
    845    1.1  jonathan 	}
    846    1.1  jonathan 	v = ip->ip_v;
    847    1.1  jonathan 	switch (v) {
    848    1.1  jonathan 	case 4:
    849    1.1  jonathan 		error = ipsec4_setspidx_ipaddr(m, spidx);
    850    1.1  jonathan 		if (error)
    851    1.1  jonathan 			return error;
    852    1.1  jonathan 		ipsec4_get_ulp(m, spidx, needport);
    853    1.1  jonathan 		return 0;
    854    1.1  jonathan #ifdef INET6
    855    1.1  jonathan 	case 6:
    856    1.1  jonathan 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
    857   1.77     ozaki 			KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    858   1.62  christos 			    "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
    859   1.77     ozaki 			    "ignored.\n", m->m_pkthdr.len);
    860    1.1  jonathan 			return EINVAL;
    861    1.1  jonathan 		}
    862    1.1  jonathan 		error = ipsec6_setspidx_ipaddr(m, spidx);
    863    1.1  jonathan 		if (error)
    864    1.1  jonathan 			return error;
    865    1.1  jonathan 		ipsec6_get_ulp(m, spidx, needport);
    866    1.1  jonathan 		return 0;
    867    1.1  jonathan #endif
    868    1.1  jonathan 	default:
    869   1.77     ozaki 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
    870   1.77     ozaki 		    "unknown IP version %u, ignored.\n", v);
    871    1.1  jonathan 		return EINVAL;
    872    1.1  jonathan 	}
    873    1.1  jonathan }
    874    1.1  jonathan 
    875    1.1  jonathan static void
    876    1.1  jonathan ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    877    1.1  jonathan {
    878    1.1  jonathan 	u_int8_t nxt;
    879    1.1  jonathan 	int off;
    880    1.1  jonathan 
    881   1.73     ozaki 	KASSERT(m != NULL);
    882   1.73     ozaki 	KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
    883    1.1  jonathan 
    884    1.1  jonathan 	/* NB: ip_input() flips it into host endian XXX need more checking */
    885    1.8   thorpej 	if (m->m_len >= sizeof(struct ip)) {
    886    1.1  jonathan 		struct ip *ip = mtod(m, struct ip *);
    887   1.75     ozaki 		if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
    888    1.1  jonathan 			goto done;
    889    1.1  jonathan 		off = ip->ip_hl << 2;
    890    1.1  jonathan 		nxt = ip->ip_p;
    891    1.1  jonathan 	} else {
    892    1.1  jonathan 		struct ip ih;
    893    1.1  jonathan 
    894  1.129      maxv 		m_copydata(m, 0, sizeof(struct ip), &ih);
    895   1.75     ozaki 		if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
    896    1.1  jonathan 			goto done;
    897    1.1  jonathan 		off = ih.ip_hl << 2;
    898    1.1  jonathan 		nxt = ih.ip_p;
    899    1.1  jonathan 	}
    900    1.1  jonathan 
    901    1.1  jonathan 	while (off < m->m_pkthdr.len) {
    902    1.1  jonathan 		struct ip6_ext ip6e;
    903    1.1  jonathan 		struct tcphdr th;
    904    1.1  jonathan 		struct udphdr uh;
    905   1.38   mlelstv 		struct icmp icmph;
    906    1.1  jonathan 
    907    1.1  jonathan 		switch (nxt) {
    908    1.1  jonathan 		case IPPROTO_TCP:
    909    1.1  jonathan 			spidx->ul_proto = nxt;
    910    1.1  jonathan 			if (!needport)
    911    1.1  jonathan 				goto done_proto;
    912    1.1  jonathan 			if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
    913    1.1  jonathan 				goto done;
    914  1.129      maxv 			m_copydata(m, off, sizeof(th), &th);
    915    1.1  jonathan 			spidx->src.sin.sin_port = th.th_sport;
    916    1.1  jonathan 			spidx->dst.sin.sin_port = th.th_dport;
    917    1.1  jonathan 			return;
    918    1.1  jonathan 		case IPPROTO_UDP:
    919    1.1  jonathan 			spidx->ul_proto = nxt;
    920    1.1  jonathan 			if (!needport)
    921    1.1  jonathan 				goto done_proto;
    922    1.1  jonathan 			if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
    923    1.1  jonathan 				goto done;
    924  1.129      maxv 			m_copydata(m, off, sizeof(uh), &uh);
    925    1.1  jonathan 			spidx->src.sin.sin_port = uh.uh_sport;
    926    1.1  jonathan 			spidx->dst.sin.sin_port = uh.uh_dport;
    927    1.1  jonathan 			return;
    928    1.1  jonathan 		case IPPROTO_AH:
    929  1.130      maxv 			if (off + sizeof(ip6e) > m->m_pkthdr.len)
    930    1.1  jonathan 				goto done;
    931    1.1  jonathan 			/* XXX sigh, this works but is totally bogus */
    932   1.28  degroote 			m_copydata(m, off, sizeof(ip6e), &ip6e);
    933    1.1  jonathan 			off += (ip6e.ip6e_len + 2) << 2;
    934    1.1  jonathan 			nxt = ip6e.ip6e_nxt;
    935    1.1  jonathan 			break;
    936    1.1  jonathan 		case IPPROTO_ICMP:
    937   1.38   mlelstv 			spidx->ul_proto = nxt;
    938   1.38   mlelstv 			if (off + sizeof(struct icmp) > m->m_pkthdr.len)
    939  1.134      maxv 				goto done;
    940   1.39  degroote 			m_copydata(m, off, sizeof(icmph), &icmph);
    941   1.38   mlelstv 			((struct sockaddr_in *)&spidx->src)->sin_port =
    942   1.38   mlelstv 			    htons((uint16_t)icmph.icmp_type);
    943   1.38   mlelstv 			((struct sockaddr_in *)&spidx->dst)->sin_port =
    944   1.38   mlelstv 			    htons((uint16_t)icmph.icmp_code);
    945   1.38   mlelstv 			return;
    946    1.1  jonathan 		default:
    947    1.1  jonathan 			/* XXX intermediate headers??? */
    948    1.1  jonathan 			spidx->ul_proto = nxt;
    949    1.1  jonathan 			goto done_proto;
    950    1.1  jonathan 		}
    951    1.1  jonathan 	}
    952    1.1  jonathan done:
    953    1.1  jonathan 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
    954    1.1  jonathan done_proto:
    955    1.1  jonathan 	spidx->src.sin.sin_port = IPSEC_PORT_ANY;
    956    1.1  jonathan 	spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
    957    1.1  jonathan }
    958    1.1  jonathan 
    959    1.1  jonathan static int
    960    1.1  jonathan ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
    961    1.1  jonathan {
    962    1.1  jonathan 	static const struct sockaddr_in template = {
    963  1.129      maxv 		sizeof(struct sockaddr_in),
    964    1.1  jonathan 		AF_INET,
    965    1.1  jonathan 		0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
    966    1.1  jonathan 	};
    967    1.1  jonathan 
    968    1.1  jonathan 	spidx->src.sin = template;
    969    1.1  jonathan 	spidx->dst.sin = template;
    970    1.1  jonathan 
    971  1.129      maxv 	if (m->m_len < sizeof(struct ip)) {
    972    1.1  jonathan 		m_copydata(m, offsetof(struct ip, ip_src),
    973  1.100     ozaki 		    sizeof(struct in_addr), &spidx->src.sin.sin_addr);
    974    1.1  jonathan 		m_copydata(m, offsetof(struct ip, ip_dst),
    975  1.100     ozaki 		    sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
    976    1.1  jonathan 	} else {
    977    1.1  jonathan 		struct ip *ip = mtod(m, struct ip *);
    978    1.1  jonathan 		spidx->src.sin.sin_addr = ip->ip_src;
    979    1.1  jonathan 		spidx->dst.sin.sin_addr = ip->ip_dst;
    980    1.1  jonathan 	}
    981    1.1  jonathan 
    982    1.1  jonathan 	spidx->prefs = sizeof(struct in_addr) << 3;
    983    1.1  jonathan 	spidx->prefd = sizeof(struct in_addr) << 3;
    984    1.1  jonathan 
    985    1.1  jonathan 	return 0;
    986    1.1  jonathan }
    987    1.1  jonathan 
    988    1.1  jonathan #ifdef INET6
    989    1.1  jonathan static void
    990  1.129      maxv ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
    991    1.1  jonathan {
    992    1.1  jonathan 	int off, nxt;
    993    1.1  jonathan 	struct tcphdr th;
    994    1.1  jonathan 	struct udphdr uh;
    995   1.38   mlelstv 	struct icmp6_hdr icmph;
    996    1.1  jonathan 
    997   1.80     ozaki 	KASSERT(m != NULL);
    998    1.1  jonathan 
    999   1.77     ozaki 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1000  1.120  christos 		kdebug_mbuf(__func__, m);
   1001   1.77     ozaki 	}
   1002    1.1  jonathan 
   1003    1.1  jonathan 	/* set default */
   1004    1.1  jonathan 	spidx->ul_proto = IPSEC_ULPROTO_ANY;
   1005    1.1  jonathan 	((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
   1006    1.1  jonathan 	((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
   1007    1.1  jonathan 
   1008    1.1  jonathan 	nxt = -1;
   1009    1.1  jonathan 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
   1010    1.1  jonathan 	if (off < 0 || m->m_pkthdr.len < off)
   1011    1.1  jonathan 		return;
   1012    1.1  jonathan 
   1013    1.1  jonathan 	switch (nxt) {
   1014    1.1  jonathan 	case IPPROTO_TCP:
   1015    1.1  jonathan 		spidx->ul_proto = nxt;
   1016    1.1  jonathan 		if (!needport)
   1017    1.1  jonathan 			break;
   1018    1.1  jonathan 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
   1019    1.1  jonathan 			break;
   1020   1.28  degroote 		m_copydata(m, off, sizeof(th), &th);
   1021    1.1  jonathan 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
   1022    1.1  jonathan 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
   1023    1.1  jonathan 		break;
   1024    1.1  jonathan 	case IPPROTO_UDP:
   1025    1.1  jonathan 		spidx->ul_proto = nxt;
   1026    1.1  jonathan 		if (!needport)
   1027    1.1  jonathan 			break;
   1028    1.1  jonathan 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
   1029    1.1  jonathan 			break;
   1030   1.28  degroote 		m_copydata(m, off, sizeof(uh), &uh);
   1031    1.1  jonathan 		((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
   1032    1.1  jonathan 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
   1033    1.1  jonathan 		break;
   1034    1.1  jonathan 	case IPPROTO_ICMPV6:
   1035   1.38   mlelstv 		spidx->ul_proto = nxt;
   1036   1.38   mlelstv 		if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
   1037   1.38   mlelstv 			break;
   1038   1.39  degroote 		m_copydata(m, off, sizeof(icmph), &icmph);
   1039   1.38   mlelstv 		((struct sockaddr_in6 *)&spidx->src)->sin6_port =
   1040   1.38   mlelstv 		    htons((uint16_t)icmph.icmp6_type);
   1041   1.38   mlelstv 		((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
   1042   1.38   mlelstv 		    htons((uint16_t)icmph.icmp6_code);
   1043   1.38   mlelstv 		break;
   1044    1.1  jonathan 	default:
   1045    1.1  jonathan 		/* XXX intermediate headers??? */
   1046    1.1  jonathan 		spidx->ul_proto = nxt;
   1047    1.1  jonathan 		break;
   1048    1.1  jonathan 	}
   1049    1.1  jonathan }
   1050    1.1  jonathan 
   1051    1.1  jonathan static int
   1052   1.33  degroote ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
   1053    1.1  jonathan {
   1054    1.1  jonathan 	struct ip6_hdr *ip6 = NULL;
   1055    1.1  jonathan 	struct ip6_hdr ip6buf;
   1056    1.1  jonathan 	struct sockaddr_in6 *sin6;
   1057    1.1  jonathan 
   1058  1.154      maxv 	if (m->m_len >= sizeof(*ip6)) {
   1059    1.1  jonathan 		ip6 = mtod(m, struct ip6_hdr *);
   1060  1.154      maxv 	} else {
   1061   1.28  degroote 		m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
   1062    1.1  jonathan 		ip6 = &ip6buf;
   1063    1.1  jonathan 	}
   1064    1.1  jonathan 
   1065    1.1  jonathan 	sin6 = (struct sockaddr_in6 *)&spidx->src;
   1066   1.41    cegger 	memset(sin6, 0, sizeof(*sin6));
   1067    1.1  jonathan 	sin6->sin6_family = AF_INET6;
   1068    1.1  jonathan 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1069   1.43   tsutsui 	memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
   1070    1.1  jonathan 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
   1071    1.1  jonathan 		sin6->sin6_addr.s6_addr16[1] = 0;
   1072    1.1  jonathan 		sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
   1073    1.1  jonathan 	}
   1074    1.1  jonathan 	spidx->prefs = sizeof(struct in6_addr) << 3;
   1075    1.1  jonathan 
   1076    1.1  jonathan 	sin6 = (struct sockaddr_in6 *)&spidx->dst;
   1077   1.41    cegger 	memset(sin6, 0, sizeof(*sin6));
   1078    1.1  jonathan 	sin6->sin6_family = AF_INET6;
   1079    1.1  jonathan 	sin6->sin6_len = sizeof(struct sockaddr_in6);
   1080   1.43   tsutsui 	memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
   1081    1.1  jonathan 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
   1082    1.1  jonathan 		sin6->sin6_addr.s6_addr16[1] = 0;
   1083    1.1  jonathan 		sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
   1084    1.1  jonathan 	}
   1085    1.1  jonathan 	spidx->prefd = sizeof(struct in6_addr) << 3;
   1086    1.1  jonathan 
   1087    1.1  jonathan 	return 0;
   1088    1.1  jonathan }
   1089    1.1  jonathan #endif
   1090    1.1  jonathan 
   1091    1.1  jonathan static void
   1092   1.33  degroote ipsec_delpcbpolicy(struct inpcbpolicy *p)
   1093    1.1  jonathan {
   1094   1.90     ozaki 
   1095   1.91     ozaki 	kmem_intr_free(p, sizeof(*p));
   1096    1.1  jonathan }
   1097    1.1  jonathan 
   1098    1.1  jonathan int
   1099  1.159      maxv ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **policy)
   1100    1.1  jonathan {
   1101    1.1  jonathan 	struct inpcbpolicy *new;
   1102    1.1  jonathan 
   1103   1.80     ozaki 	KASSERT(so != NULL);
   1104   1.80     ozaki 	KASSERT(policy != NULL);
   1105    1.1  jonathan 
   1106   1.91     ozaki 	new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
   1107   1.91     ozaki 	if (new == NULL) {
   1108   1.92     ozaki 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1109   1.91     ozaki 		return ENOBUFS;
   1110   1.91     ozaki 	}
   1111    1.1  jonathan 
   1112    1.1  jonathan 	if (IPSEC_PRIVILEGED_SO(so))
   1113    1.1  jonathan 		new->priv = 1;
   1114    1.1  jonathan 	else
   1115    1.1  jonathan 		new->priv = 0;
   1116    1.1  jonathan 
   1117  1.113     ozaki 	/*
   1118  1.119     ozaki 	 * Set dummy SPs. Actual SPs will be allocated later if needed.
   1119  1.113     ozaki 	 */
   1120  1.119     ozaki 	new->sp_in = &ipsec_dummy_sp;
   1121  1.119     ozaki 	new->sp_out = &ipsec_dummy_sp;
   1122    1.1  jonathan 
   1123   1.57  christos 	*policy = new;
   1124    1.1  jonathan 
   1125    1.1  jonathan 	return 0;
   1126    1.1  jonathan }
   1127    1.1  jonathan 
   1128  1.113     ozaki static void
   1129  1.113     ozaki ipsec_destroy_policy(struct secpolicy *sp)
   1130  1.113     ozaki {
   1131  1.113     ozaki 
   1132  1.154      maxv 	if (sp == &ipsec_dummy_sp) {
   1133  1.119     ozaki 		; /* It's dummy. No need to free it. */
   1134  1.154      maxv 	} else {
   1135  1.113     ozaki 		/*
   1136  1.113     ozaki 		 * We cannot destroy here because it can be called in
   1137  1.113     ozaki 		 * softint. So mark the SP as DEAD and let the timer
   1138  1.113     ozaki 		 * destroy it. See key_timehandler_spd.
   1139  1.113     ozaki 		 */
   1140  1.113     ozaki 		sp->state = IPSEC_SPSTATE_DEAD;
   1141  1.113     ozaki 	}
   1142  1.113     ozaki }
   1143  1.113     ozaki 
   1144  1.146      maxv int
   1145  1.174     ozaki ipsec_set_policy(struct inpcb *inp, const void *request, size_t len,
   1146  1.146      maxv     kauth_cred_t cred)
   1147    1.1  jonathan {
   1148   1.55  drochner 	const struct sadb_x_policy *xpl;
   1149  1.145      maxv 	struct secpolicy *newsp, *oldsp;
   1150  1.146      maxv 	struct secpolicy **policy;
   1151    1.1  jonathan 	int error;
   1152    1.1  jonathan 
   1153   1.90     ozaki 	KASSERT(!cpu_softintr_p());
   1154  1.174     ozaki 	KASSERT(inp != NULL);
   1155  1.174     ozaki 	KASSERT(inp_locked(inp));
   1156  1.146      maxv 	KASSERT(request != NULL);
   1157  1.146      maxv 
   1158  1.146      maxv 	if (len < sizeof(*xpl))
   1159  1.146      maxv 		return EINVAL;
   1160  1.146      maxv 	xpl = (const struct sadb_x_policy *)request;
   1161  1.146      maxv 
   1162  1.174     ozaki 	KASSERT(inp->inp_sp != NULL);
   1163  1.146      maxv 
   1164  1.146      maxv 	/* select direction */
   1165  1.146      maxv 	switch (xpl->sadb_x_policy_dir) {
   1166  1.146      maxv 	case IPSEC_DIR_INBOUND:
   1167  1.174     ozaki 		policy = &inp->inp_sp->sp_in;
   1168  1.146      maxv 		break;
   1169  1.146      maxv 	case IPSEC_DIR_OUTBOUND:
   1170  1.174     ozaki 		policy = &inp->inp_sp->sp_out;
   1171  1.146      maxv 		break;
   1172  1.146      maxv 	default:
   1173  1.146      maxv 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1174  1.146      maxv 		    xpl->sadb_x_policy_dir);
   1175  1.146      maxv 		return EINVAL;
   1176  1.146      maxv 	}
   1177   1.90     ozaki 
   1178    1.1  jonathan 	/* sanity check. */
   1179  1.145      maxv 	if (policy == NULL || *policy == NULL)
   1180    1.1  jonathan 		return EINVAL;
   1181    1.1  jonathan 
   1182   1.77     ozaki 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1183  1.120  christos 		kdebug_sadb_xpolicy("set passed policy", request);
   1184   1.77     ozaki 	}
   1185    1.1  jonathan 
   1186    1.1  jonathan 	/* check policy type */
   1187    1.1  jonathan 	/* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
   1188  1.129      maxv 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
   1189  1.129      maxv 	    xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
   1190    1.1  jonathan 		return EINVAL;
   1191    1.1  jonathan 
   1192    1.1  jonathan 	/* check privileged socket */
   1193   1.44      elad 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
   1194   1.56      elad 		error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
   1195   1.56      elad 		    KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
   1196   1.44      elad 		if (error)
   1197  1.129      maxv 			return error;
   1198   1.44      elad 	}
   1199    1.1  jonathan 
   1200    1.1  jonathan 	/* allocation new SP entry */
   1201    1.1  jonathan 	if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
   1202    1.1  jonathan 		return error;
   1203    1.1  jonathan 
   1204  1.113     ozaki 	key_init_sp(newsp);
   1205  1.113     ozaki 	newsp->created = time_uptime;
   1206  1.113     ozaki 	/* Insert the global list for SPs for sockets */
   1207  1.113     ozaki 	key_socksplist_add(newsp);
   1208    1.1  jonathan 
   1209    1.1  jonathan 	/* clear old SP and set new SP */
   1210  1.113     ozaki 	oldsp = *policy;
   1211   1.57  christos 	*policy = newsp;
   1212  1.113     ozaki 	ipsec_destroy_policy(oldsp);
   1213  1.113     ozaki 
   1214   1.77     ozaki 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1215   1.77     ozaki 		printf("%s: new policy\n", __func__);
   1216   1.77     ozaki 		kdebug_secpolicy(newsp);
   1217   1.77     ozaki 	}
   1218    1.1  jonathan 
   1219    1.1  jonathan 	return 0;
   1220    1.1  jonathan }
   1221    1.1  jonathan 
   1222    1.1  jonathan int
   1223  1.174     ozaki ipsec_get_policy(struct inpcb *inp, const void *request, size_t len,
   1224  1.129      maxv     struct mbuf **mp)
   1225    1.1  jonathan {
   1226   1.55  drochner 	const struct sadb_x_policy *xpl;
   1227   1.57  christos 	struct secpolicy *policy;
   1228    1.1  jonathan 
   1229    1.1  jonathan 	/* sanity check. */
   1230  1.174     ozaki 	if (inp == NULL || request == NULL || mp == NULL)
   1231    1.1  jonathan 		return EINVAL;
   1232  1.174     ozaki 	KASSERT(inp->inp_sp != NULL);
   1233    1.1  jonathan 	if (len < sizeof(*xpl))
   1234    1.1  jonathan 		return EINVAL;
   1235   1.55  drochner 	xpl = (const struct sadb_x_policy *)request;
   1236    1.1  jonathan 
   1237    1.1  jonathan 	/* select direction */
   1238    1.1  jonathan 	switch (xpl->sadb_x_policy_dir) {
   1239    1.1  jonathan 	case IPSEC_DIR_INBOUND:
   1240  1.174     ozaki 		policy = inp->inp_sp->sp_in;
   1241    1.1  jonathan 		break;
   1242    1.1  jonathan 	case IPSEC_DIR_OUTBOUND:
   1243  1.174     ozaki 		policy = inp->inp_sp->sp_out;
   1244    1.1  jonathan 		break;
   1245    1.1  jonathan 	default:
   1246   1.92     ozaki 		IPSECLOG(LOG_ERR, "invalid direction=%u\n",
   1247   1.92     ozaki 		    xpl->sadb_x_policy_dir);
   1248    1.1  jonathan 		return EINVAL;
   1249    1.1  jonathan 	}
   1250    1.1  jonathan 
   1251  1.145      maxv 	if (policy == NULL)
   1252  1.144      maxv 		return EINVAL;
   1253  1.144      maxv 
   1254  1.144      maxv 	*mp = key_sp2msg(policy, M_NOWAIT);
   1255  1.144      maxv 	if (!*mp) {
   1256  1.144      maxv 		IPSECLOG(LOG_DEBUG, "No more memory.\n");
   1257  1.144      maxv 		return ENOBUFS;
   1258  1.144      maxv 	}
   1259  1.144      maxv 
   1260  1.144      maxv 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
   1261  1.144      maxv 		kdebug_mbuf(__func__, *mp);
   1262  1.144      maxv 	}
   1263  1.144      maxv 
   1264  1.144      maxv 	return 0;
   1265    1.1  jonathan }
   1266    1.1  jonathan 
   1267    1.1  jonathan int
   1268  1.174     ozaki ipsec_delete_pcbpolicy(struct inpcb *inp)
   1269    1.1  jonathan {
   1270   1.73     ozaki 
   1271  1.174     ozaki 	KASSERT(inp != NULL);
   1272    1.1  jonathan 
   1273  1.174     ozaki 	if (inp->inp_sp == NULL)
   1274    1.1  jonathan 		return 0;
   1275    1.1  jonathan 
   1276  1.174     ozaki 	if (inp->inp_sp->sp_in != NULL)
   1277  1.174     ozaki 		ipsec_destroy_policy(inp->inp_sp->sp_in);
   1278    1.1  jonathan 
   1279  1.174     ozaki 	if (inp->inp_sp->sp_out != NULL)
   1280  1.174     ozaki 		ipsec_destroy_policy(inp->inp_sp->sp_out);
   1281    1.1  jonathan 
   1282  1.174     ozaki 	ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
   1283   1.49  drochner 
   1284  1.174     ozaki 	ipsec_delpcbpolicy(inp->inp_sp);
   1285  1.174     ozaki 	inp->inp_sp = NULL;
   1286    1.1  jonathan 
   1287    1.1  jonathan 	return 0;
   1288    1.1  jonathan }
   1289    1.1  jonathan 
   1290    1.1  jonathan /*
   1291  1.135      maxv  * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
   1292    1.1  jonathan  */
   1293    1.1  jonathan u_int
   1294   1.52  christos ipsec_get_reqlevel(const struct ipsecrequest *isr)
   1295    1.1  jonathan {
   1296    1.1  jonathan 	u_int level = 0;
   1297    1.1  jonathan 	u_int esp_trans_deflev, esp_net_deflev;
   1298    1.1  jonathan 	u_int ah_trans_deflev, ah_net_deflev;
   1299    1.1  jonathan 
   1300   1.73     ozaki 	KASSERT(isr != NULL);
   1301   1.73     ozaki 	KASSERT(isr->sp != NULL);
   1302   1.73     ozaki 	KASSERTMSG(
   1303   1.73     ozaki 	    isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
   1304   1.73     ozaki 	    "af family mismatch, src %u, dst %u",
   1305   1.73     ozaki 	    isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
   1306    1.1  jonathan 
   1307    1.1  jonathan /* XXX note that we have ipseclog() expanded here - code sync issue */
   1308  1.135      maxv #define IPSEC_CHECK_DEFAULT(lev)					\
   1309   1.62  christos     (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE		\
   1310   1.62  christos     && (lev) != IPSEC_LEVEL_UNIQUE) ?					\
   1311   1.62  christos 	(ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
   1312   1.64    plunky 	":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0),		\
   1313   1.62  christos 	(lev) = IPSEC_LEVEL_REQUIRE, (lev)				\
   1314   1.62  christos     : (lev))
   1315    1.1  jonathan 
   1316    1.1  jonathan 	/* set default level */
   1317    1.1  jonathan 	switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
   1318    1.1  jonathan #ifdef INET
   1319    1.1  jonathan 	case AF_INET:
   1320    1.1  jonathan 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
   1321    1.1  jonathan 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
   1322    1.1  jonathan 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
   1323    1.1  jonathan 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
   1324    1.1  jonathan 		break;
   1325    1.1  jonathan #endif
   1326    1.1  jonathan #ifdef INET6
   1327    1.1  jonathan 	case AF_INET6:
   1328    1.1  jonathan 		esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
   1329    1.1  jonathan 		esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
   1330    1.1  jonathan 		ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
   1331    1.1  jonathan 		ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
   1332    1.1  jonathan 		break;
   1333  1.154      maxv #endif
   1334    1.1  jonathan 	default:
   1335   1.62  christos 		panic("%s: unknown af %u", __func__,
   1336   1.62  christos 		    isr->sp->spidx.src.sa.sa_family);
   1337    1.1  jonathan 	}
   1338    1.1  jonathan 
   1339    1.1  jonathan #undef IPSEC_CHECK_DEFAULT
   1340    1.1  jonathan 
   1341    1.1  jonathan 	/* set level */
   1342    1.1  jonathan 	switch (isr->level) {
   1343    1.1  jonathan 	case IPSEC_LEVEL_DEFAULT:
   1344    1.1  jonathan 		switch (isr->saidx.proto) {
   1345    1.1  jonathan 		case IPPROTO_ESP:
   1346    1.1  jonathan 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1347    1.1  jonathan 				level = esp_net_deflev;
   1348    1.1  jonathan 			else
   1349    1.1  jonathan 				level = esp_trans_deflev;
   1350    1.1  jonathan 			break;
   1351    1.1  jonathan 		case IPPROTO_AH:
   1352    1.1  jonathan 			if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
   1353    1.1  jonathan 				level = ah_net_deflev;
   1354    1.1  jonathan 			else
   1355    1.1  jonathan 				level = ah_trans_deflev;
   1356   1.14  jonathan 			break;
   1357    1.1  jonathan 		case IPPROTO_IPCOMP:
   1358    1.1  jonathan 			/*
   1359    1.1  jonathan 			 * we don't really care, as IPcomp document says that
   1360    1.1  jonathan 			 * we shouldn't compress small packets
   1361    1.1  jonathan 			 */
   1362    1.1  jonathan 			level = IPSEC_LEVEL_USE;
   1363    1.1  jonathan 			break;
   1364    1.1  jonathan 		default:
   1365   1.62  christos 			panic("%s: Illegal protocol defined %u", __func__,
   1366   1.62  christos 			    isr->saidx.proto);
   1367    1.1  jonathan 		}
   1368    1.1  jonathan 		break;
   1369    1.1  jonathan 
   1370    1.1  jonathan 	case IPSEC_LEVEL_USE:
   1371    1.1  jonathan 	case IPSEC_LEVEL_REQUIRE:
   1372    1.1  jonathan 		level = isr->level;
   1373    1.1  jonathan 		break;
   1374    1.1  jonathan 	case IPSEC_LEVEL_UNIQUE:
   1375    1.1  jonathan 		level = IPSEC_LEVEL_REQUIRE;
   1376    1.1  jonathan 		break;
   1377    1.1  jonathan 
   1378    1.1  jonathan 	default:
   1379   1.62  christos 		panic("%s: Illegal IPsec level %u", __func__, isr->level);
   1380    1.1  jonathan 	}
   1381    1.1  jonathan 
   1382    1.1  jonathan 	return level;
   1383    1.1  jonathan }
   1384    1.1  jonathan 
   1385    1.1  jonathan /*
   1386  1.131      maxv  * Check security policy requirements against the actual packet contents.
   1387    1.1  jonathan  *
   1388  1.131      maxv  * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
   1389  1.131      maxv  * then kick it.
   1390    1.1  jonathan  */
   1391  1.149      maxv static int
   1392  1.132      maxv ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
   1393    1.1  jonathan {
   1394    1.1  jonathan 	struct ipsecrequest *isr;
   1395    1.1  jonathan 
   1396   1.77     ozaki 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1397   1.77     ozaki 		printf("%s: using SP\n", __func__);
   1398   1.77     ozaki 		kdebug_secpolicy(sp);
   1399   1.77     ozaki 	}
   1400    1.1  jonathan 
   1401    1.1  jonathan 	/* check policy */
   1402    1.1  jonathan 	switch (sp->policy) {
   1403    1.1  jonathan 	case IPSEC_POLICY_DISCARD:
   1404    1.1  jonathan 		return 1;
   1405    1.1  jonathan 	case IPSEC_POLICY_BYPASS:
   1406    1.1  jonathan 	case IPSEC_POLICY_NONE:
   1407    1.1  jonathan 		return 0;
   1408    1.1  jonathan 	}
   1409    1.1  jonathan 
   1410   1.73     ozaki 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1411   1.73     ozaki 	    "invalid policy %u", sp->policy);
   1412    1.1  jonathan 
   1413    1.1  jonathan 	/* XXX should compare policy against ipsec header history */
   1414    1.1  jonathan 
   1415    1.1  jonathan 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1416    1.1  jonathan 		if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
   1417    1.1  jonathan 			continue;
   1418    1.1  jonathan 		switch (isr->saidx.proto) {
   1419    1.1  jonathan 		case IPPROTO_ESP:
   1420    1.1  jonathan 			if ((m->m_flags & M_DECRYPTED) == 0) {
   1421   1.77     ozaki 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1422   1.77     ozaki 				    "ESP m_flags:%x\n", m->m_flags);
   1423    1.1  jonathan 				return 1;
   1424    1.1  jonathan 			}
   1425    1.1  jonathan 			break;
   1426    1.1  jonathan 		case IPPROTO_AH:
   1427    1.1  jonathan 			if ((m->m_flags & M_AUTHIPHDR) == 0) {
   1428   1.77     ozaki 				KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
   1429   1.77     ozaki 				    "AH m_flags:%x\n", m->m_flags);
   1430    1.1  jonathan 				return 1;
   1431    1.1  jonathan 			}
   1432    1.1  jonathan 			break;
   1433    1.1  jonathan 		case IPPROTO_IPCOMP:
   1434    1.1  jonathan 			/*
   1435  1.131      maxv 			 * We don't really care, as IPcomp document
   1436    1.1  jonathan 			 * says that we shouldn't compress small
   1437    1.1  jonathan 			 * packets, IPComp policy should always be
   1438    1.1  jonathan 			 * treated as being in "use" level.
   1439    1.1  jonathan 			 */
   1440    1.1  jonathan 			break;
   1441    1.1  jonathan 		}
   1442    1.1  jonathan 	}
   1443  1.132      maxv 
   1444  1.132      maxv 	return 0;
   1445    1.1  jonathan }
   1446    1.1  jonathan 
   1447    1.1  jonathan /*
   1448  1.139      maxv  * Check security policy requirements.
   1449    1.1  jonathan  */
   1450    1.1  jonathan int
   1451  1.174     ozaki ipsec_in_reject(struct mbuf *m, struct inpcb *inp)
   1452    1.1  jonathan {
   1453    1.1  jonathan 	struct secpolicy *sp;
   1454    1.1  jonathan 	int error;
   1455    1.1  jonathan 	int result;
   1456    1.1  jonathan 
   1457   1.73     ozaki 	KASSERT(m != NULL);
   1458    1.1  jonathan 
   1459  1.174     ozaki 	if (inp == NULL)
   1460  1.131      maxv 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
   1461  1.131      maxv 		    IP_FORWARDING, &error);
   1462    1.1  jonathan 	else
   1463    1.5  jonathan 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
   1464  1.174     ozaki 		    inp, &error);
   1465    1.1  jonathan 
   1466    1.1  jonathan 	if (sp != NULL) {
   1467  1.132      maxv 		result = ipsec_sp_reject(sp, m);
   1468    1.1  jonathan 		if (result)
   1469   1.37   thorpej 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
   1470  1.113     ozaki 		KEY_SP_UNREF(&sp);
   1471    1.1  jonathan 	} else {
   1472  1.131      maxv 		result = 0;
   1473    1.1  jonathan 	}
   1474    1.1  jonathan 	return result;
   1475    1.1  jonathan }
   1476    1.1  jonathan 
   1477    1.1  jonathan /*
   1478  1.132      maxv  * Compute the byte size to be occupied by the IPsec header. If it is
   1479  1.132      maxv  * tunneled, it includes the size of outer IP header.
   1480    1.1  jonathan  */
   1481    1.1  jonathan static size_t
   1482  1.132      maxv ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
   1483    1.1  jonathan {
   1484  1.107     ozaki 	struct ipsecrequest *isr;
   1485    1.1  jonathan 	size_t siz;
   1486    1.1  jonathan 
   1487   1.77     ozaki 	if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
   1488   1.77     ozaki 		printf("%s: using SP\n", __func__);
   1489   1.77     ozaki 		kdebug_secpolicy(sp);
   1490   1.77     ozaki 	}
   1491    1.1  jonathan 
   1492    1.1  jonathan 	switch (sp->policy) {
   1493    1.1  jonathan 	case IPSEC_POLICY_DISCARD:
   1494    1.1  jonathan 	case IPSEC_POLICY_BYPASS:
   1495    1.1  jonathan 	case IPSEC_POLICY_NONE:
   1496    1.1  jonathan 		return 0;
   1497    1.1  jonathan 	}
   1498    1.1  jonathan 
   1499   1.73     ozaki 	KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
   1500   1.73     ozaki 	    "invalid policy %u", sp->policy);
   1501    1.1  jonathan 
   1502    1.1  jonathan 	siz = 0;
   1503    1.1  jonathan 	for (isr = sp->req; isr != NULL; isr = isr->next) {
   1504    1.1  jonathan 		size_t clen = 0;
   1505  1.121     ozaki 		struct secasvar *sav;
   1506    1.1  jonathan 
   1507    1.1  jonathan 		switch (isr->saidx.proto) {
   1508    1.1  jonathan 		case IPPROTO_ESP:
   1509  1.121     ozaki 			sav = ipsec_lookup_sa(isr, m);
   1510  1.121     ozaki 			if (sav != NULL) {
   1511  1.107     ozaki 				clen = esp_hdrsiz(sav);
   1512  1.116     ozaki 				KEY_SA_UNREF(&sav);
   1513  1.107     ozaki 			} else
   1514  1.107     ozaki 				clen = esp_hdrsiz(NULL);
   1515    1.1  jonathan 			break;
   1516    1.1  jonathan 		case IPPROTO_AH:
   1517  1.121     ozaki 			sav = ipsec_lookup_sa(isr, m);
   1518  1.121     ozaki 			if (sav != NULL) {
   1519  1.107     ozaki 				clen = ah_hdrsiz(sav);
   1520  1.116     ozaki 				KEY_SA_UNREF(&sav);
   1521  1.107     ozaki 			} else
   1522  1.107     ozaki 				clen = ah_hdrsiz(NULL);
   1523    1.1  jonathan 			break;
   1524    1.1  jonathan 		case IPPROTO_IPCOMP:
   1525    1.1  jonathan 			clen = sizeof(struct ipcomp);
   1526    1.1  jonathan 			break;
   1527    1.1  jonathan 		}
   1528    1.1  jonathan 
   1529    1.1  jonathan 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
   1530    1.1  jonathan 			switch (isr->saidx.dst.sa.sa_family) {
   1531    1.1  jonathan 			case AF_INET:
   1532    1.1  jonathan 				clen += sizeof(struct ip);
   1533    1.1  jonathan 				break;
   1534    1.1  jonathan #ifdef INET6
   1535    1.1  jonathan 			case AF_INET6:
   1536    1.1  jonathan 				clen += sizeof(struct ip6_hdr);
   1537    1.1  jonathan 				break;
   1538    1.1  jonathan #endif
   1539    1.1  jonathan 			default:
   1540   1.92     ozaki 				IPSECLOG(LOG_ERR, "unknown AF %d in "
   1541   1.92     ozaki 				    "IPsec tunnel SA\n",
   1542   1.62  christos 				    ((const struct sockaddr *)&isr->saidx.dst)
   1543   1.92     ozaki 				    ->sa_family);
   1544    1.1  jonathan 				break;
   1545    1.1  jonathan 			}
   1546    1.1  jonathan 		}
   1547    1.1  jonathan 		siz += clen;
   1548    1.1  jonathan 	}
   1549    1.1  jonathan 
   1550    1.1  jonathan 	return siz;
   1551    1.1  jonathan }
   1552    1.1  jonathan 
   1553    1.1  jonathan size_t
   1554  1.174     ozaki ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
   1555    1.1  jonathan {
   1556    1.1  jonathan 	struct secpolicy *sp;
   1557    1.1  jonathan 	int error;
   1558    1.1  jonathan 	size_t size;
   1559    1.1  jonathan 
   1560   1.73     ozaki 	KASSERT(m != NULL);
   1561  1.174     ozaki 	KASSERTMSG(inp == NULL || inp->inp_socket != NULL,
   1562  1.133      maxv 	    "socket w/o inpcb");
   1563    1.1  jonathan 
   1564  1.174     ozaki 	if (inp == NULL)
   1565    1.1  jonathan 		sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
   1566    1.1  jonathan 	else
   1567  1.174     ozaki 		sp = ipsec_getpolicybysock(m, dir, inp, &error);
   1568    1.1  jonathan 
   1569    1.1  jonathan 	if (sp != NULL) {
   1570  1.132      maxv 		size = ipsec_sp_hdrsiz(sp, m);
   1571  1.131      maxv 		KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
   1572  1.113     ozaki 		KEY_SP_UNREF(&sp);
   1573    1.1  jonathan 	} else {
   1574  1.133      maxv 		size = 0;
   1575    1.1  jonathan 	}
   1576  1.131      maxv 
   1577    1.1  jonathan 	return size;
   1578    1.1  jonathan }
   1579    1.1  jonathan 
   1580    1.1  jonathan /*
   1581    1.1  jonathan  * Check the variable replay window.
   1582    1.1  jonathan  * ipsec_chkreplay() performs replay check before ICV verification.
   1583    1.1  jonathan  * ipsec_updatereplay() updates replay bitmap.  This must be called after
   1584    1.1  jonathan  * ICV verification (it also performs replay check, which is usually done
   1585    1.1  jonathan  * beforehand).
   1586    1.1  jonathan  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
   1587    1.1  jonathan  *
   1588    1.1  jonathan  * based on RFC 2401.
   1589    1.1  jonathan  */
   1590    1.1  jonathan int
   1591   1.50  drochner ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
   1592    1.1  jonathan {
   1593    1.1  jonathan 	const struct secreplay *replay;
   1594    1.1  jonathan 	u_int32_t diff;
   1595    1.1  jonathan 	int fr;
   1596    1.1  jonathan 	u_int32_t wsizeb;	/* constant: bits of window size */
   1597    1.1  jonathan 	int frlast;		/* constant: last frame */
   1598    1.1  jonathan 
   1599   1.73     ozaki 	KASSERT(sav != NULL);
   1600   1.73     ozaki 	KASSERT(sav->replay != NULL);
   1601    1.1  jonathan 
   1602    1.1  jonathan 	replay = sav->replay;
   1603    1.1  jonathan 
   1604    1.1  jonathan 	if (replay->wsize == 0)
   1605    1.1  jonathan 		return 1;	/* no need to check replay. */
   1606    1.1  jonathan 
   1607    1.1  jonathan 	/* constant */
   1608    1.1  jonathan 	frlast = replay->wsize - 1;
   1609    1.1  jonathan 	wsizeb = replay->wsize << 3;
   1610    1.1  jonathan 
   1611    1.1  jonathan 	/* sequence number of 0 is invalid */
   1612    1.1  jonathan 	if (seq == 0)
   1613    1.1  jonathan 		return 0;
   1614    1.1  jonathan 
   1615    1.1  jonathan 	/* first time is always okay */
   1616    1.1  jonathan 	if (replay->count == 0)
   1617    1.1  jonathan 		return 1;
   1618    1.1  jonathan 
   1619    1.1  jonathan 	if (seq > replay->lastseq) {
   1620    1.1  jonathan 		/* larger sequences are okay */
   1621    1.1  jonathan 		return 1;
   1622    1.1  jonathan 	} else {
   1623    1.1  jonathan 		/* seq is equal or less than lastseq. */
   1624    1.1  jonathan 		diff = replay->lastseq - seq;
   1625    1.1  jonathan 
   1626    1.1  jonathan 		/* over range to check, i.e. too old or wrapped */
   1627    1.1  jonathan 		if (diff >= wsizeb)
   1628    1.1  jonathan 			return 0;
   1629    1.1  jonathan 
   1630    1.1  jonathan 		fr = frlast - diff / 8;
   1631    1.1  jonathan 
   1632    1.1  jonathan 		/* this packet already seen ? */
   1633    1.1  jonathan 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1634    1.1  jonathan 			return 0;
   1635    1.1  jonathan 
   1636    1.1  jonathan 		/* out of order but good */
   1637    1.1  jonathan 		return 1;
   1638    1.1  jonathan 	}
   1639    1.1  jonathan }
   1640    1.1  jonathan 
   1641    1.1  jonathan /*
   1642    1.1  jonathan  * check replay counter whether to update or not.
   1643    1.1  jonathan  * OUT:	0:	OK
   1644    1.1  jonathan  *	1:	NG
   1645    1.1  jonathan  */
   1646    1.1  jonathan int
   1647   1.50  drochner ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
   1648    1.1  jonathan {
   1649    1.1  jonathan 	struct secreplay *replay;
   1650    1.1  jonathan 	u_int32_t diff;
   1651    1.1  jonathan 	int fr;
   1652    1.1  jonathan 	u_int32_t wsizeb;	/* constant: bits of window size */
   1653    1.1  jonathan 	int frlast;		/* constant: last frame */
   1654    1.1  jonathan 
   1655   1.73     ozaki 	KASSERT(sav != NULL);
   1656   1.73     ozaki 	KASSERT(sav->replay != NULL);
   1657    1.1  jonathan 
   1658    1.1  jonathan 	replay = sav->replay;
   1659    1.1  jonathan 
   1660    1.1  jonathan 	if (replay->wsize == 0)
   1661    1.1  jonathan 		goto ok;	/* no need to check replay. */
   1662    1.1  jonathan 
   1663    1.1  jonathan 	/* constant */
   1664    1.1  jonathan 	frlast = replay->wsize - 1;
   1665    1.1  jonathan 	wsizeb = replay->wsize << 3;
   1666    1.1  jonathan 
   1667    1.1  jonathan 	/* sequence number of 0 is invalid */
   1668    1.1  jonathan 	if (seq == 0)
   1669    1.1  jonathan 		return 1;
   1670    1.1  jonathan 
   1671    1.1  jonathan 	/* first time */
   1672    1.1  jonathan 	if (replay->count == 0) {
   1673    1.1  jonathan 		replay->lastseq = seq;
   1674   1.41    cegger 		memset(replay->bitmap, 0, replay->wsize);
   1675    1.1  jonathan 		(replay->bitmap)[frlast] = 1;
   1676    1.1  jonathan 		goto ok;
   1677    1.1  jonathan 	}
   1678    1.1  jonathan 
   1679    1.1  jonathan 	if (seq > replay->lastseq) {
   1680    1.1  jonathan 		/* seq is larger than lastseq. */
   1681    1.1  jonathan 		diff = seq - replay->lastseq;
   1682    1.1  jonathan 
   1683    1.1  jonathan 		/* new larger sequence number */
   1684    1.1  jonathan 		if (diff < wsizeb) {
   1685    1.1  jonathan 			/* In window */
   1686    1.1  jonathan 			/* set bit for this packet */
   1687    1.1  jonathan 			vshiftl(replay->bitmap, diff, replay->wsize);
   1688    1.1  jonathan 			(replay->bitmap)[frlast] |= 1;
   1689    1.1  jonathan 		} else {
   1690    1.1  jonathan 			/* this packet has a "way larger" */
   1691   1.41    cegger 			memset(replay->bitmap, 0, replay->wsize);
   1692    1.1  jonathan 			(replay->bitmap)[frlast] = 1;
   1693    1.1  jonathan 		}
   1694    1.1  jonathan 		replay->lastseq = seq;
   1695    1.1  jonathan 
   1696    1.1  jonathan 		/* larger is good */
   1697    1.1  jonathan 	} else {
   1698    1.1  jonathan 		/* seq is equal or less than lastseq. */
   1699    1.1  jonathan 		diff = replay->lastseq - seq;
   1700    1.1  jonathan 
   1701    1.1  jonathan 		/* over range to check, i.e. too old or wrapped */
   1702    1.1  jonathan 		if (diff >= wsizeb)
   1703    1.1  jonathan 			return 1;
   1704    1.1  jonathan 
   1705    1.1  jonathan 		fr = frlast - diff / 8;
   1706    1.1  jonathan 
   1707    1.1  jonathan 		/* this packet already seen ? */
   1708    1.1  jonathan 		if ((replay->bitmap)[fr] & (1 << (diff % 8)))
   1709    1.1  jonathan 			return 1;
   1710    1.1  jonathan 
   1711    1.1  jonathan 		/* mark as seen */
   1712    1.1  jonathan 		(replay->bitmap)[fr] |= (1 << (diff % 8));
   1713    1.1  jonathan 
   1714    1.1  jonathan 		/* out of order but good */
   1715    1.1  jonathan 	}
   1716    1.1  jonathan 
   1717    1.1  jonathan ok:
   1718    1.1  jonathan 	if (replay->count == ~0) {
   1719  1.122     ozaki 		char buf[IPSEC_LOGSASTRLEN];
   1720    1.1  jonathan 
   1721    1.1  jonathan 		/* set overflow flag */
   1722    1.1  jonathan 		replay->overflow++;
   1723    1.1  jonathan 
   1724    1.1  jonathan 		/* don't increment, no more packets accepted */
   1725    1.1  jonathan 		if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
   1726    1.1  jonathan 			return 1;
   1727    1.1  jonathan 
   1728   1.92     ozaki 		IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
   1729   1.92     ozaki 		    replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
   1730    1.1  jonathan 	}
   1731    1.1  jonathan 
   1732    1.1  jonathan 	replay->count++;
   1733    1.1  jonathan 
   1734    1.1  jonathan 	return 0;
   1735    1.1  jonathan }
   1736    1.1  jonathan 
   1737    1.1  jonathan /*
   1738  1.132      maxv  * shift variable length buffer to left.
   1739    1.1  jonathan  * IN:	bitmap: pointer to the buffer
   1740  1.129      maxv  *	nbit:	the number of to shift.
   1741    1.1  jonathan  *	wsize:	buffer size (bytes).
   1742    1.1  jonathan  */
   1743    1.1  jonathan static void
   1744   1.33  degroote vshiftl(unsigned char *bitmap, int nbit, int wsize)
   1745    1.1  jonathan {
   1746    1.1  jonathan 	int s, j, i;
   1747    1.1  jonathan 	unsigned char over;
   1748    1.1  jonathan 
   1749    1.1  jonathan 	for (j = 0; j < nbit; j += 8) {
   1750    1.1  jonathan 		s = (nbit - j < 8) ? (nbit - j): 8;
   1751    1.1  jonathan 		bitmap[0] <<= s;
   1752    1.1  jonathan 		for (i = 1; i < wsize; i++) {
   1753    1.1  jonathan 			over = (bitmap[i] >> (8 - s));
   1754    1.1  jonathan 			bitmap[i] <<= s;
   1755    1.1  jonathan 			bitmap[i-1] |= over;
   1756    1.1  jonathan 		}
   1757    1.1  jonathan 	}
   1758    1.1  jonathan 
   1759    1.1  jonathan 	return;
   1760    1.1  jonathan }
   1761    1.1  jonathan 
   1762    1.1  jonathan /* Return a printable string for the address. */
   1763   1.17  christos const char *
   1764   1.88       ryo ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
   1765    1.1  jonathan {
   1766    1.1  jonathan 	switch (sa->sa.sa_family) {
   1767    1.1  jonathan 	case AF_INET:
   1768   1.88       ryo 		in_print(buf, size, &sa->sin.sin_addr);
   1769   1.88       ryo 		return buf;
   1770    1.1  jonathan #if INET6
   1771    1.1  jonathan 	case AF_INET6:
   1772   1.88       ryo 		in6_print(buf, size, &sa->sin6.sin6_addr);
   1773   1.88       ryo 		return buf;
   1774  1.129      maxv #endif
   1775    1.1  jonathan 	default:
   1776    1.1  jonathan 		return "(unknown address family)";
   1777    1.1  jonathan 	}
   1778    1.1  jonathan }
   1779    1.1  jonathan 
   1780    1.1  jonathan const char *
   1781   1.88       ryo ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
   1782    1.1  jonathan {
   1783   1.50  drochner 	const struct secasindex *saidx = &sav->sah->saidx;
   1784   1.88       ryo 	char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
   1785    1.1  jonathan 
   1786   1.74     ozaki 	KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
   1787   1.74     ozaki 	    "af family mismatch, src %u, dst %u",
   1788   1.74     ozaki 	    saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
   1789    1.1  jonathan 
   1790   1.88       ryo 	snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
   1791   1.88       ryo 	    (u_int32_t)ntohl(sav->spi),
   1792   1.88       ryo 	    ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
   1793   1.88       ryo 	    ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
   1794    1.1  jonathan 
   1795    1.1  jonathan 	return buf;
   1796    1.1  jonathan }
   1797    1.1  jonathan 
   1798   1.26  degroote #ifdef INET6
   1799  1.129      maxv struct secpolicy *
   1800  1.174     ozaki ipsec6_check_policy(struct mbuf *m, struct inpcb *inp, int flags,
   1801  1.129      maxv     int *needipsecp, int *errorp)
   1802   1.26  degroote {
   1803   1.26  degroote 	struct secpolicy *sp = NULL;
   1804   1.26  degroote 	int s;
   1805   1.26  degroote 	int error = 0;
   1806   1.26  degroote 	int needipsec = 0;
   1807   1.26  degroote 
   1808  1.151      maxv 	if (ipsec_outdone(m)) {
   1809  1.151      maxv 		goto skippolicycheck;
   1810  1.151      maxv 	}
   1811  1.151      maxv 	s = splsoftnet();
   1812  1.174     ozaki 	if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
   1813  1.151      maxv 		splx(s);
   1814  1.151      maxv 		goto skippolicycheck;
   1815  1.151      maxv 	}
   1816  1.174     ozaki 	sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
   1817  1.151      maxv 	splx(s);
   1818   1.26  degroote 
   1819  1.151      maxv 	/*
   1820  1.151      maxv 	 * There are four return cases:
   1821  1.151      maxv 	 *	sp != NULL                    apply IPsec policy
   1822  1.151      maxv 	 *	sp == NULL, error == 0        no IPsec handling needed
   1823  1.151      maxv 	 *	sp == NULL, error == -EINVAL  discard packet w/o error
   1824  1.151      maxv 	 *	sp == NULL, error != 0        discard packet, report error
   1825  1.151      maxv 	 */
   1826  1.151      maxv 	if (sp == NULL) {
   1827  1.151      maxv 		needipsec = 0;
   1828  1.151      maxv 	} else {
   1829  1.151      maxv 		needipsec = 1;
   1830   1.26  degroote 	}
   1831   1.26  degroote 
   1832  1.151      maxv skippolicycheck:
   1833   1.26  degroote 	*errorp = error;
   1834   1.26  degroote 	*needipsecp = needipsec;
   1835   1.26  degroote 	return sp;
   1836   1.26  degroote }
   1837  1.167  knakahar 
   1838  1.167  knakahar /*
   1839  1.167  knakahar  * calculate UDP checksum for UDP encapsulated ESP for IPv6.
   1840  1.167  knakahar  *
   1841  1.167  knakahar  * RFC2460(Internet Protocol, Version 6 Specification) says:
   1842  1.167  knakahar  *
   1843  1.167  knakahar  *   IPv6 receivers MUST discard UDP packets with a zero checksum.
   1844  1.167  knakahar  *
   1845  1.173    andvar  * There is more relaxed specification RFC6935(IPv6 and UDP Checksums for
   1846  1.167  knakahar  * Tunneled Packets). The document allows zero checksum. It's too
   1847  1.167  knakahar  * late to publish, there are a lot of interoperability problems...
   1848  1.167  knakahar  */
   1849  1.167  knakahar void
   1850  1.167  knakahar ipsec6_udp_cksum(struct mbuf *m)
   1851  1.167  knakahar {
   1852  1.167  knakahar 	struct ip6_hdr *ip6;
   1853  1.167  knakahar 	uint16_t plen, uh_sum;
   1854  1.167  knakahar 	int off;
   1855  1.167  knakahar 
   1856  1.167  knakahar 	/* must called after m_pullup() */
   1857  1.167  knakahar 	KASSERT(m->m_len >= sizeof(struct ip6_hdr));
   1858  1.167  knakahar 
   1859  1.167  knakahar 	ip6 = mtod(m, struct ip6_hdr *);
   1860  1.167  knakahar 	KASSERT(ip6->ip6_nxt == IPPROTO_UDP);
   1861  1.167  knakahar 
   1862  1.167  knakahar 	/* ip6->ip6_plen can not be updated before ip6_output() */
   1863  1.167  knakahar 	plen = m->m_pkthdr.len - sizeof(*ip6);
   1864  1.167  knakahar 	KASSERT(plen >= sizeof(struct udphdr));
   1865  1.167  knakahar 
   1866  1.167  knakahar 	uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(*ip6), plen);
   1867  1.167  knakahar 	if (uh_sum == 0)
   1868  1.167  knakahar 		uh_sum = 0xffff;
   1869  1.167  knakahar 
   1870  1.167  knakahar 	off = sizeof(*ip6) + offsetof(struct udphdr, uh_sum);
   1871  1.167  knakahar 	m_copyback(m, off, sizeof(uh_sum), (void *)&uh_sum);
   1872  1.167  knakahar }
   1873   1.66     ozaki #endif /* INET6 */
   1874   1.26  degroote 
   1875  1.135      maxv /*
   1876  1.135      maxv  * -----------------------------------------------------------------------------
   1877  1.135      maxv  */
   1878   1.26  degroote 
   1879    1.1  jonathan /* XXX this stuff doesn't belong here... */
   1880    1.1  jonathan 
   1881  1.135      maxv static struct xformsw *xforms = NULL;
   1882    1.1  jonathan 
   1883    1.1  jonathan /*
   1884    1.1  jonathan  * Register a transform; typically at system startup.
   1885    1.1  jonathan  */
   1886    1.1  jonathan void
   1887   1.51  drochner xform_register(struct xformsw *xsp)
   1888    1.1  jonathan {
   1889    1.1  jonathan 	xsp->xf_next = xforms;
   1890    1.1  jonathan 	xforms = xsp;
   1891    1.1  jonathan }
   1892    1.1  jonathan 
   1893    1.1  jonathan /*
   1894    1.1  jonathan  * Initialize transform support in an sav.
   1895    1.1  jonathan  */
   1896    1.1  jonathan int
   1897    1.1  jonathan xform_init(struct secasvar *sav, int xftype)
   1898    1.1  jonathan {
   1899    1.1  jonathan 	struct xformsw *xsp;
   1900    1.1  jonathan 
   1901    1.1  jonathan 	if (sav->tdb_xform != NULL)	/* previously initialized */
   1902    1.1  jonathan 		return 0;
   1903    1.1  jonathan 	for (xsp = xforms; xsp; xsp = xsp->xf_next)
   1904    1.1  jonathan 		if (xsp->xf_type == xftype)
   1905    1.1  jonathan 			return (*xsp->xf_init)(sav, xsp);
   1906    1.1  jonathan 
   1907   1.92     ozaki 	IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
   1908    1.1  jonathan 	return EINVAL;
   1909    1.1  jonathan }
   1910    1.1  jonathan 
   1911   1.37   thorpej /*
   1912   1.37   thorpej  * XXXJRT This should be done as a protosw init call.
   1913   1.37   thorpej  */
   1914    1.1  jonathan void
   1915    1.1  jonathan ipsec_attach(void)
   1916    1.1  jonathan {
   1917   1.37   thorpej 
   1918  1.118     ozaki 	ipsec_output_init();
   1919  1.118     ozaki 
   1920   1.37   thorpej 	ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
   1921   1.37   thorpej 
   1922   1.71     ozaki 	sysctl_net_inet_ipsec_setup(NULL);
   1923   1.71     ozaki #ifdef INET6
   1924   1.71     ozaki 	sysctl_net_inet6_ipsec6_setup(NULL);
   1925   1.71     ozaki #endif
   1926   1.71     ozaki 
   1927    1.1  jonathan 	ah_attach();
   1928    1.1  jonathan 	esp_attach();
   1929    1.1  jonathan 	ipcomp_attach();
   1930    1.1  jonathan 	ipe4_attach();
   1931   1.12  jonathan #ifdef TCP_SIGNATURE
   1932   1.12  jonathan 	tcpsignature_attach();
   1933   1.12  jonathan #endif
   1934    1.1  jonathan }
   1935