Home | History | Annotate | Line # | Download | only in netinet
ip_encap.c revision 1.39.30.8
      1  1.39.30.8     skrll /*	$NetBSD: ip_encap.c,v 1.39.30.8 2017/08/28 17:53:12 skrll Exp $	*/
      2        1.7    itojun /*	$KAME: ip_encap.c,v 1.73 2001/10/02 08:30:58 itojun Exp $	*/
      3        1.1    itojun 
      4        1.1    itojun /*
      5        1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6        1.1    itojun  * All rights reserved.
      7        1.1    itojun  *
      8        1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9        1.1    itojun  * modification, are permitted provided that the following conditions
     10        1.1    itojun  * are met:
     11        1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15        1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16        1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17        1.1    itojun  *    may be used to endorse or promote products derived from this software
     18        1.1    itojun  *    without specific prior written permission.
     19        1.1    itojun  *
     20        1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21        1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24        1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.1    itojun  * SUCH DAMAGE.
     31        1.1    itojun  */
     32        1.1    itojun /*
     33        1.1    itojun  * My grandfather said that there's a devil inside tunnelling technology...
     34        1.1    itojun  *
     35        1.1    itojun  * We have surprisingly many protocols that want packets with IP protocol
     36        1.1    itojun  * #4 or #41.  Here's a list of protocols that want protocol #41:
     37        1.1    itojun  *	RFC1933 configured tunnel
     38        1.1    itojun  *	RFC1933 automatic tunnel
     39        1.1    itojun  *	RFC2401 IPsec tunnel
     40        1.1    itojun  *	RFC2473 IPv6 generic packet tunnelling
     41        1.1    itojun  *	RFC2529 6over4 tunnel
     42        1.7    itojun  *	RFC3056 6to4 tunnel
     43        1.7    itojun  *	isatap tunnel
     44        1.1    itojun  *	mobile-ip6 (uses RFC2473)
     45        1.1    itojun  * Here's a list of protocol that want protocol #4:
     46        1.1    itojun  *	RFC1853 IPv4-in-IPv4 tunnelling
     47        1.1    itojun  *	RFC2003 IPv4 encapsulation within IPv4
     48        1.1    itojun  *	RFC2344 reverse tunnelling for mobile-ip4
     49        1.1    itojun  *	RFC2401 IPsec tunnel
     50        1.1    itojun  * Well, what can I say.  They impose different en/decapsulation mechanism
     51        1.1    itojun  * from each other, so they need separate protocol handler.  The only one
     52        1.1    itojun  * we can easily determine by protocol # is IPsec, which always has
     53        1.1    itojun  * AH/ESP/IPComp header right after outer IP header.
     54        1.1    itojun  *
     55        1.1    itojun  * So, clearly good old protosw does not work for protocol #4 and #41.
     56        1.1    itojun  * The code will let you match protocol via src/dst address pair.
     57        1.1    itojun  */
     58        1.1    itojun /* XXX is M_NETADDR correct? */
     59        1.6     lukem 
     60        1.7    itojun /*
     61  1.39.30.6     skrll  * With USE_RADIX the code will use radix table for tunnel lookup, for
     62        1.7    itojun  * tunnels registered with encap_attach() with a addr/mask pair.
     63        1.7    itojun  * Faster on machines with thousands of tunnel registerations (= interfaces).
     64        1.7    itojun  *
     65        1.7    itojun  * The code assumes that radix table code can handle non-continuous netmask,
     66        1.7    itojun  * as it will pass radix table memory region with (src + dst) sockaddr pair.
     67        1.7    itojun  */
     68  1.39.30.6     skrll #define USE_RADIX
     69        1.7    itojun 
     70        1.6     lukem #include <sys/cdefs.h>
     71  1.39.30.8     skrll __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.39.30.8 2017/08/28 17:53:12 skrll Exp $");
     72        1.1    itojun 
     73  1.39.30.2     skrll #ifdef _KERNEL_OPT
     74        1.4    itojun #include "opt_mrouting.h"
     75        1.4    itojun #include "opt_inet.h"
     76  1.39.30.6     skrll #include "opt_net_mpsafe.h"
     77  1.39.30.2     skrll #endif
     78        1.1    itojun 
     79        1.1    itojun #include <sys/param.h>
     80        1.1    itojun #include <sys/systm.h>
     81        1.1    itojun #include <sys/socket.h>
     82        1.1    itojun #include <sys/sockio.h>
     83        1.1    itojun #include <sys/mbuf.h>
     84        1.1    itojun #include <sys/errno.h>
     85        1.4    itojun #include <sys/queue.h>
     86  1.39.30.3     skrll #include <sys/kmem.h>
     87  1.39.30.6     skrll #include <sys/mutex.h>
     88  1.39.30.6     skrll #include <sys/condvar.h>
     89  1.39.30.6     skrll #include <sys/psref.h>
     90  1.39.30.6     skrll #include <sys/pslist.h>
     91        1.1    itojun 
     92        1.1    itojun #include <net/if.h>
     93        1.1    itojun 
     94        1.1    itojun #include <netinet/in.h>
     95        1.1    itojun #include <netinet/in_systm.h>
     96        1.1    itojun #include <netinet/ip.h>
     97        1.1    itojun #include <netinet/ip_var.h>
     98        1.1    itojun #include <netinet/ip_encap.h>
     99        1.1    itojun #ifdef MROUTING
    100        1.1    itojun #include <netinet/ip_mroute.h>
    101        1.1    itojun #endif /* MROUTING */
    102        1.1    itojun 
    103        1.1    itojun #ifdef INET6
    104        1.1    itojun #include <netinet/ip6.h>
    105        1.1    itojun #include <netinet6/ip6_var.h>
    106  1.39.30.4     skrll #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
    107        1.7    itojun #include <netinet6/in6_var.h>
    108        1.7    itojun #include <netinet6/in6_pcb.h>
    109        1.7    itojun #include <netinet/icmp6.h>
    110        1.1    itojun #endif
    111        1.1    itojun 
    112        1.1    itojun #include <net/net_osdep.h>
    113        1.1    itojun 
    114  1.39.30.6     skrll #ifdef NET_MPSAFE
    115  1.39.30.6     skrll #define ENCAP_MPSAFE	1
    116  1.39.30.6     skrll #endif
    117  1.39.30.6     skrll 
    118        1.7    itojun enum direction { INBOUND, OUTBOUND };
    119        1.7    itojun 
    120        1.7    itojun #ifdef INET
    121  1.39.30.6     skrll static struct encaptab *encap4_lookup(struct mbuf *, int, int, enum direction,
    122  1.39.30.6     skrll     struct psref *);
    123        1.7    itojun #endif
    124        1.7    itojun #ifdef INET6
    125  1.39.30.6     skrll static struct encaptab *encap6_lookup(struct mbuf *, int, int, enum direction,
    126  1.39.30.6     skrll     struct psref *);
    127        1.7    itojun #endif
    128       1.22     perry static int encap_add(struct encaptab *);
    129       1.22     perry static int encap_remove(struct encaptab *);
    130       1.22     perry static int encap_afcheck(int, const struct sockaddr *, const struct sockaddr *);
    131  1.39.30.6     skrll #ifdef USE_RADIX
    132       1.22     perry static struct radix_node_head *encap_rnh(int);
    133       1.22     perry static int mask_matchlen(const struct sockaddr *);
    134  1.39.30.6     skrll #else
    135  1.39.30.6     skrll static int mask_match(const struct encaptab *, const struct sockaddr *,
    136  1.39.30.6     skrll 		const struct sockaddr *);
    137  1.39.30.6     skrll #endif
    138       1.22     perry static void encap_fillarg(struct mbuf *, const struct encaptab *);
    139        1.1    itojun 
    140  1.39.30.6     skrll /*
    141  1.39.30.6     skrll  * In encap[46]_lookup(), ep->func can sleep(e.g. rtalloc1) while walking
    142  1.39.30.6     skrll  * encap_table. So, it cannot use pserialize_read_enter()
    143  1.39.30.6     skrll  */
    144  1.39.30.6     skrll static struct {
    145  1.39.30.6     skrll 	struct pslist_head	list;
    146  1.39.30.6     skrll 	pserialize_t		psz;
    147  1.39.30.6     skrll 	struct psref_class	*elem_class; /* for the element of et_list */
    148  1.39.30.6     skrll } encaptab  __cacheline_aligned = {
    149  1.39.30.6     skrll 	.list = PSLIST_INITIALIZER,
    150  1.39.30.6     skrll };
    151  1.39.30.6     skrll #define encap_table encaptab.list
    152  1.39.30.6     skrll 
    153  1.39.30.6     skrll static struct {
    154  1.39.30.6     skrll 	kmutex_t	lock;
    155  1.39.30.6     skrll 	kcondvar_t	cv;
    156  1.39.30.6     skrll 	struct lwp	*busy;
    157  1.39.30.6     skrll } encap_whole __cacheline_aligned;
    158        1.1    itojun 
    159  1.39.30.6     skrll #ifdef USE_RADIX
    160        1.7    itojun struct radix_node_head *encap_head[2];	/* 0 for AF_INET, 1 for AF_INET6 */
    161  1.39.30.6     skrll static bool encap_head_updating = false;
    162  1.39.30.6     skrll #endif
    163  1.39.30.6     skrll 
    164  1.39.30.8     skrll static bool encap_initialized = false;
    165  1.39.30.6     skrll /*
    166  1.39.30.6     skrll  * must be done before other encap interfaces initialization.
    167  1.39.30.6     skrll  */
    168  1.39.30.6     skrll void
    169  1.39.30.6     skrll encapinit(void)
    170  1.39.30.6     skrll {
    171  1.39.30.6     skrll 
    172  1.39.30.8     skrll 	if (encap_initialized)
    173  1.39.30.8     skrll 		return;
    174  1.39.30.8     skrll 
    175  1.39.30.6     skrll 	encaptab.psz = pserialize_create();
    176  1.39.30.6     skrll 	encaptab.elem_class = psref_class_create("encapelem", IPL_SOFTNET);
    177  1.39.30.6     skrll 
    178  1.39.30.6     skrll 	mutex_init(&encap_whole.lock, MUTEX_DEFAULT, IPL_NONE);
    179  1.39.30.6     skrll 	cv_init(&encap_whole.cv, "ip_encap cv");
    180  1.39.30.6     skrll 	encap_whole.busy = NULL;
    181  1.39.30.8     skrll 
    182  1.39.30.8     skrll 	encap_initialized = true;
    183  1.39.30.6     skrll }
    184        1.7    itojun 
    185        1.1    itojun void
    186       1.23     perry encap_init(void)
    187        1.1    itojun {
    188        1.7    itojun 	static int initialized = 0;
    189        1.7    itojun 
    190        1.7    itojun 	if (initialized)
    191        1.7    itojun 		return;
    192        1.7    itojun 	initialized++;
    193        1.1    itojun #if 0
    194        1.1    itojun 	/*
    195        1.1    itojun 	 * we cannot use LIST_INIT() here, since drivers may want to call
    196        1.4    itojun 	 * encap_attach(), on driver attach.  encap_init() will be called
    197        1.1    itojun 	 * on AF_INET{,6} initialization, which happens after driver
    198        1.1    itojun 	 * initialization - using LIST_INIT() here can nuke encap_attach()
    199        1.1    itojun 	 * from drivers.
    200        1.1    itojun 	 */
    201  1.39.30.6     skrll 	PSLIST_INIT(&encap_table);
    202        1.1    itojun #endif
    203        1.7    itojun 
    204  1.39.30.6     skrll #ifdef USE_RADIX
    205        1.7    itojun 	/*
    206       1.38     pooka 	 * initialize radix lookup table when the radix subsystem is inited.
    207        1.7    itojun 	 */
    208       1.38     pooka 	rn_delayedinit((void *)&encap_head[0],
    209       1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    210        1.7    itojun #ifdef INET6
    211       1.38     pooka 	rn_delayedinit((void *)&encap_head[1],
    212       1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    213        1.7    itojun #endif
    214  1.39.30.6     skrll #endif
    215        1.1    itojun }
    216        1.1    itojun 
    217        1.4    itojun #ifdef INET
    218        1.7    itojun static struct encaptab *
    219  1.39.30.6     skrll encap4_lookup(struct mbuf *m, int off, int proto, enum direction dir,
    220  1.39.30.6     skrll     struct psref *match_psref)
    221        1.1    itojun {
    222        1.1    itojun 	struct ip *ip;
    223       1.33     pooka 	struct ip_pack4 pack;
    224        1.1    itojun 	struct encaptab *ep, *match;
    225        1.1    itojun 	int prio, matchprio;
    226  1.39.30.6     skrll 	int s;
    227  1.39.30.6     skrll #ifdef USE_RADIX
    228        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET);
    229        1.7    itojun 	struct radix_node *rn;
    230  1.39.30.6     skrll #endif
    231        1.1    itojun 
    232  1.39.30.1     skrll 	KASSERT(m->m_len >= sizeof(*ip));
    233  1.39.30.1     skrll 
    234        1.1    itojun 	ip = mtod(m, struct ip *);
    235        1.1    itojun 
    236       1.35    cegger 	memset(&pack, 0, sizeof(pack));
    237        1.7    itojun 	pack.p.sp_len = sizeof(pack);
    238        1.7    itojun 	pack.mine.sin_family = pack.yours.sin_family = AF_INET;
    239        1.7    itojun 	pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
    240        1.7    itojun 	if (dir == INBOUND) {
    241        1.7    itojun 		pack.mine.sin_addr = ip->ip_dst;
    242        1.7    itojun 		pack.yours.sin_addr = ip->ip_src;
    243        1.7    itojun 	} else {
    244        1.7    itojun 		pack.mine.sin_addr = ip->ip_src;
    245        1.7    itojun 		pack.yours.sin_addr = ip->ip_dst;
    246        1.7    itojun 	}
    247        1.1    itojun 
    248        1.1    itojun 	match = NULL;
    249        1.1    itojun 	matchprio = 0;
    250        1.7    itojun 
    251  1.39.30.6     skrll 	s = pserialize_read_enter();
    252  1.39.30.6     skrll #ifdef USE_RADIX
    253  1.39.30.6     skrll 	if (encap_head_updating) {
    254  1.39.30.6     skrll 		/*
    255  1.39.30.6     skrll 		 * Update in progress. Do nothing.
    256  1.39.30.6     skrll 		 */
    257  1.39.30.6     skrll 		pserialize_read_exit(s);
    258  1.39.30.6     skrll 		return NULL;
    259  1.39.30.6     skrll 	}
    260  1.39.30.6     skrll 
    261       1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    262        1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    263  1.39.30.6     skrll 		struct encaptab *encapp = (struct encaptab *)rn;
    264  1.39.30.6     skrll 
    265  1.39.30.6     skrll 		psref_acquire(match_psref, &encapp->psref,
    266  1.39.30.6     skrll 		    encaptab.elem_class);
    267  1.39.30.6     skrll 		match = encapp;
    268        1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    269        1.7    itojun 		    mask_matchlen(match->dstmask);
    270        1.7    itojun 	}
    271  1.39.30.6     skrll #endif
    272  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    273  1.39.30.6     skrll 		struct psref elem_psref;
    274  1.39.30.6     skrll 
    275        1.1    itojun 		if (ep->af != AF_INET)
    276        1.1    itojun 			continue;
    277        1.1    itojun 		if (ep->proto >= 0 && ep->proto != proto)
    278        1.1    itojun 			continue;
    279  1.39.30.6     skrll 
    280  1.39.30.6     skrll 		psref_acquire(&elem_psref, &ep->psref,
    281  1.39.30.6     skrll 		    encaptab.elem_class);
    282  1.39.30.6     skrll 		if (ep->func) {
    283  1.39.30.6     skrll 			pserialize_read_exit(s);
    284  1.39.30.6     skrll 			/* ep->func is sleepable. e.g. rtalloc1 */
    285        1.1    itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    286  1.39.30.6     skrll 			s = pserialize_read_enter();
    287  1.39.30.6     skrll 		} else {
    288  1.39.30.6     skrll #ifdef USE_RADIX
    289  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    290  1.39.30.6     skrll 			    encaptab.elem_class);
    291        1.7    itojun 			continue;
    292  1.39.30.6     skrll #else
    293  1.39.30.6     skrll 			prio = mask_match(ep, (struct sockaddr *)&pack.mine,
    294  1.39.30.6     skrll 			    (struct sockaddr *)&pack.yours);
    295  1.39.30.6     skrll #endif
    296  1.39.30.6     skrll 		}
    297        1.1    itojun 
    298        1.1    itojun 		/*
    299        1.1    itojun 		 * We prioritize the matches by using bit length of the
    300        1.1    itojun 		 * matches.  mask_match() and user-supplied matching function
    301        1.1    itojun 		 * should return the bit length of the matches (for example,
    302        1.1    itojun 		 * if both src/dst are matched for IPv4, 64 should be returned).
    303        1.1    itojun 		 * 0 or negative return value means "it did not match".
    304        1.1    itojun 		 *
    305        1.1    itojun 		 * The question is, since we have two "mask" portion, we
    306        1.1    itojun 		 * cannot really define total order between entries.
    307        1.1    itojun 		 * For example, which of these should be preferred?
    308        1.1    itojun 		 * mask_match() returns 48 (32 + 16) for both of them.
    309        1.1    itojun 		 *	src=3ffe::/16, dst=3ffe:501::/32
    310        1.1    itojun 		 *	src=3ffe:501::/32, dst=3ffe::/16
    311        1.1    itojun 		 *
    312        1.1    itojun 		 * We need to loop through all the possible candidates
    313        1.1    itojun 		 * to get the best match - the search takes O(n) for
    314        1.1    itojun 		 * n attachments (i.e. interfaces).
    315        1.7    itojun 		 *
    316        1.7    itojun 		 * For radix-based lookup, I guess source takes precedence.
    317        1.7    itojun 		 * See rn_{refines,lexobetter} for the correct answer.
    318        1.1    itojun 		 */
    319  1.39.30.6     skrll 		if (prio <= 0) {
    320  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    321  1.39.30.6     skrll 			    encaptab.elem_class);
    322        1.1    itojun 			continue;
    323  1.39.30.6     skrll 		}
    324        1.1    itojun 		if (prio > matchprio) {
    325  1.39.30.6     skrll 			/* release last matched ep */
    326  1.39.30.6     skrll 			if (match != NULL)
    327  1.39.30.6     skrll 				psref_release(match_psref, &match->psref,
    328  1.39.30.6     skrll 				    encaptab.elem_class);
    329  1.39.30.6     skrll 
    330  1.39.30.6     skrll 			psref_copy(match_psref, &elem_psref,
    331  1.39.30.6     skrll 			    encaptab.elem_class);
    332        1.1    itojun 			matchprio = prio;
    333        1.1    itojun 			match = ep;
    334        1.1    itojun 		}
    335  1.39.30.6     skrll 		KASSERTMSG((match == NULL) || psref_held(&match->psref,
    336  1.39.30.6     skrll 			encaptab.elem_class),
    337  1.39.30.6     skrll 		    "current match = %p, but not hold its psref", match);
    338  1.39.30.6     skrll 
    339  1.39.30.6     skrll 		psref_release(&elem_psref, &ep->psref,
    340  1.39.30.6     skrll 		    encaptab.elem_class);
    341        1.1    itojun 	}
    342  1.39.30.6     skrll 	pserialize_read_exit(s);
    343        1.1    itojun 
    344        1.7    itojun 	return match;
    345        1.7    itojun }
    346        1.7    itojun 
    347        1.7    itojun void
    348        1.7    itojun encap4_input(struct mbuf *m, ...)
    349        1.7    itojun {
    350        1.7    itojun 	int off, proto;
    351        1.7    itojun 	va_list ap;
    352  1.39.30.4     skrll 	const struct encapsw *esw;
    353        1.7    itojun 	struct encaptab *match;
    354  1.39.30.6     skrll 	struct psref match_psref;
    355        1.7    itojun 
    356        1.7    itojun 	va_start(ap, m);
    357        1.7    itojun 	off = va_arg(ap, int);
    358        1.7    itojun 	proto = va_arg(ap, int);
    359        1.7    itojun 	va_end(ap);
    360        1.7    itojun 
    361  1.39.30.6     skrll 	match = encap4_lookup(m, off, proto, INBOUND, &match_psref);
    362        1.1    itojun 	if (match) {
    363        1.1    itojun 		/* found a match, "match" has the best one */
    364  1.39.30.4     skrll 		esw = match->esw;
    365  1.39.30.4     skrll 		if (esw && esw->encapsw4.pr_input) {
    366        1.1    itojun 			encap_fillarg(m, match);
    367  1.39.30.4     skrll 			(*esw->encapsw4.pr_input)(m, off, proto);
    368  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    369  1.39.30.6     skrll 			    encaptab.elem_class);
    370  1.39.30.6     skrll 		} else {
    371  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    372  1.39.30.6     skrll 			    encaptab.elem_class);
    373        1.1    itojun 			m_freem(m);
    374  1.39.30.6     skrll 		}
    375        1.1    itojun 		return;
    376        1.1    itojun 	}
    377        1.1    itojun 
    378        1.1    itojun 	/* last resort: inject to raw socket */
    379        1.1    itojun 	rip_input(m, off, proto);
    380        1.1    itojun }
    381        1.1    itojun #endif
    382        1.1    itojun 
    383        1.1    itojun #ifdef INET6
    384        1.7    itojun static struct encaptab *
    385  1.39.30.6     skrll encap6_lookup(struct mbuf *m, int off, int proto, enum direction dir,
    386  1.39.30.6     skrll     struct psref *match_psref)
    387        1.1    itojun {
    388        1.1    itojun 	struct ip6_hdr *ip6;
    389       1.33     pooka 	struct ip_pack6 pack;
    390        1.7    itojun 	int prio, matchprio;
    391  1.39.30.6     skrll 	int s;
    392        1.1    itojun 	struct encaptab *ep, *match;
    393  1.39.30.6     skrll #ifdef USE_RADIX
    394        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET6);
    395        1.7    itojun 	struct radix_node *rn;
    396  1.39.30.6     skrll #endif
    397        1.1    itojun 
    398  1.39.30.1     skrll 	KASSERT(m->m_len >= sizeof(*ip6));
    399  1.39.30.1     skrll 
    400        1.1    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    401        1.1    itojun 
    402       1.35    cegger 	memset(&pack, 0, sizeof(pack));
    403        1.7    itojun 	pack.p.sp_len = sizeof(pack);
    404        1.7    itojun 	pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
    405        1.7    itojun 	pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
    406        1.7    itojun 	if (dir == INBOUND) {
    407        1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_dst;
    408        1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_src;
    409        1.7    itojun 	} else {
    410        1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_src;
    411        1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_dst;
    412        1.7    itojun 	}
    413        1.1    itojun 
    414        1.1    itojun 	match = NULL;
    415        1.1    itojun 	matchprio = 0;
    416        1.7    itojun 
    417  1.39.30.6     skrll 	s = pserialize_read_enter();
    418  1.39.30.6     skrll #ifdef USE_RADIX
    419  1.39.30.6     skrll 	if (encap_head_updating) {
    420  1.39.30.6     skrll 		/*
    421  1.39.30.6     skrll 		 * Update in progress. Do nothing.
    422  1.39.30.6     skrll 		 */
    423  1.39.30.6     skrll 		pserialize_read_exit(s);
    424  1.39.30.6     skrll 		return NULL;
    425  1.39.30.6     skrll 	}
    426  1.39.30.6     skrll 
    427       1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    428        1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    429  1.39.30.6     skrll 		struct encaptab *encapp = (struct encaptab *)rn;
    430  1.39.30.6     skrll 
    431  1.39.30.6     skrll 		psref_acquire(match_psref, &encapp->psref,
    432  1.39.30.6     skrll 		    encaptab.elem_class);
    433  1.39.30.6     skrll 		match = encapp;
    434        1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    435        1.7    itojun 		    mask_matchlen(match->dstmask);
    436        1.7    itojun 	}
    437  1.39.30.6     skrll #endif
    438  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    439  1.39.30.6     skrll 		struct psref elem_psref;
    440  1.39.30.6     skrll 
    441        1.1    itojun 		if (ep->af != AF_INET6)
    442        1.1    itojun 			continue;
    443        1.1    itojun 		if (ep->proto >= 0 && ep->proto != proto)
    444        1.1    itojun 			continue;
    445  1.39.30.6     skrll 
    446  1.39.30.6     skrll 		psref_acquire(&elem_psref, &ep->psref,
    447  1.39.30.6     skrll 		    encaptab.elem_class);
    448  1.39.30.6     skrll 
    449  1.39.30.6     skrll 		if (ep->func) {
    450  1.39.30.6     skrll 			pserialize_read_exit(s);
    451  1.39.30.6     skrll 			/* ep->func is sleepable. e.g. rtalloc1 */
    452        1.7    itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    453  1.39.30.6     skrll 			s = pserialize_read_enter();
    454  1.39.30.6     skrll 		} else {
    455  1.39.30.6     skrll #ifdef USE_RADIX
    456  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    457  1.39.30.6     skrll 			    encaptab.elem_class);
    458        1.7    itojun 			continue;
    459  1.39.30.6     skrll #else
    460  1.39.30.6     skrll 			prio = mask_match(ep, (struct sockaddr *)&pack.mine,
    461  1.39.30.6     skrll 			    (struct sockaddr *)&pack.yours);
    462  1.39.30.6     skrll #endif
    463  1.39.30.6     skrll 		}
    464        1.1    itojun 
    465        1.7    itojun 		/* see encap4_lookup() for issues here */
    466  1.39.30.6     skrll 		if (prio <= 0) {
    467  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    468  1.39.30.6     skrll 			    encaptab.elem_class);
    469        1.1    itojun 			continue;
    470  1.39.30.6     skrll 		}
    471        1.1    itojun 		if (prio > matchprio) {
    472  1.39.30.6     skrll 			/* release last matched ep */
    473  1.39.30.6     skrll 			if (match != NULL)
    474  1.39.30.6     skrll 				psref_release(match_psref, &match->psref,
    475  1.39.30.6     skrll 				    encaptab.elem_class);
    476  1.39.30.6     skrll 
    477  1.39.30.6     skrll 			psref_copy(match_psref, &elem_psref,
    478  1.39.30.6     skrll 			    encaptab.elem_class);
    479        1.1    itojun 			matchprio = prio;
    480        1.1    itojun 			match = ep;
    481        1.1    itojun 		}
    482  1.39.30.6     skrll 		KASSERTMSG((match == NULL) || psref_held(&match->psref,
    483  1.39.30.6     skrll 			encaptab.elem_class),
    484  1.39.30.6     skrll 		    "current match = %p, but not hold its psref", match);
    485  1.39.30.6     skrll 
    486  1.39.30.6     skrll 		psref_release(&elem_psref, &ep->psref,
    487  1.39.30.6     skrll 		    encaptab.elem_class);
    488        1.1    itojun 	}
    489  1.39.30.6     skrll 	pserialize_read_exit(s);
    490        1.1    itojun 
    491        1.7    itojun 	return match;
    492        1.7    itojun }
    493        1.7    itojun 
    494        1.7    itojun int
    495       1.23     perry encap6_input(struct mbuf **mp, int *offp, int proto)
    496        1.7    itojun {
    497        1.7    itojun 	struct mbuf *m = *mp;
    498  1.39.30.4     skrll 	const struct encapsw *esw;
    499        1.7    itojun 	struct encaptab *match;
    500  1.39.30.6     skrll 	struct psref match_psref;
    501        1.7    itojun 
    502  1.39.30.6     skrll 	match = encap6_lookup(m, *offp, proto, INBOUND, &match_psref);
    503        1.7    itojun 
    504        1.1    itojun 	if (match) {
    505        1.1    itojun 		/* found a match */
    506  1.39.30.4     skrll 		esw = match->esw;
    507  1.39.30.4     skrll 		if (esw && esw->encapsw6.pr_input) {
    508  1.39.30.6     skrll 			int ret;
    509        1.1    itojun 			encap_fillarg(m, match);
    510  1.39.30.6     skrll 			ret = (*esw->encapsw6.pr_input)(mp, offp, proto);
    511  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    512  1.39.30.6     skrll 			    encaptab.elem_class);
    513  1.39.30.6     skrll 			return ret;
    514        1.1    itojun 		} else {
    515  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    516  1.39.30.6     skrll 			    encaptab.elem_class);
    517        1.1    itojun 			m_freem(m);
    518        1.1    itojun 			return IPPROTO_DONE;
    519        1.1    itojun 		}
    520        1.1    itojun 	}
    521        1.1    itojun 
    522        1.1    itojun 	/* last resort: inject to raw socket */
    523        1.1    itojun 	return rip6_input(mp, offp, proto);
    524        1.1    itojun }
    525        1.1    itojun #endif
    526        1.1    itojun 
    527  1.39.30.6     skrll /*
    528  1.39.30.6     skrll  * XXX
    529  1.39.30.6     skrll  * The encaptab list and the rnh radix tree must be manipulated atomically.
    530  1.39.30.6     skrll  */
    531        1.7    itojun static int
    532       1.23     perry encap_add(struct encaptab *ep)
    533        1.1    itojun {
    534  1.39.30.6     skrll #ifdef USE_RADIX
    535        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(ep->af);
    536  1.39.30.6     skrll #endif
    537  1.39.30.6     skrll 
    538  1.39.30.6     skrll 	KASSERT(encap_lock_held());
    539        1.1    itojun 
    540  1.39.30.6     skrll #ifdef USE_RADIX
    541        1.7    itojun 	if (!ep->func && rnh) {
    542  1.39.30.6     skrll 		/* Disable access to the radix tree for reader. */
    543  1.39.30.6     skrll 		encap_head_updating = true;
    544  1.39.30.6     skrll 		/* Wait for all readers to drain. */
    545  1.39.30.6     skrll 		pserialize_perform(encaptab.psz);
    546  1.39.30.6     skrll 
    547       1.30  christos 		if (!rnh->rnh_addaddr((void *)ep->addrpack,
    548       1.30  christos 		    (void *)ep->maskpack, rnh, ep->nodes)) {
    549  1.39.30.6     skrll 			encap_head_updating = false;
    550  1.39.30.6     skrll 			return EEXIST;
    551        1.7    itojun 		}
    552  1.39.30.6     skrll 
    553  1.39.30.6     skrll 		/*
    554  1.39.30.6     skrll 		 * The ep added to the radix tree must be skipped while
    555  1.39.30.6     skrll 		 * encap[46]_lookup walks encaptab list. In other words,
    556  1.39.30.6     skrll 		 * encap_add() does not need to care whether the ep has
    557  1.39.30.6     skrll 		 * been added encaptab list or not yet.
    558  1.39.30.6     skrll 		 * So, we can re-enable access to the radix tree for now.
    559  1.39.30.6     skrll 		 */
    560  1.39.30.6     skrll 		encap_head_updating = false;
    561        1.7    itojun 	}
    562  1.39.30.6     skrll #endif
    563  1.39.30.6     skrll 	PSLIST_WRITER_INSERT_HEAD(&encap_table, ep, chain);
    564        1.7    itojun 
    565  1.39.30.6     skrll 	return 0;
    566        1.7    itojun }
    567        1.7    itojun 
    568  1.39.30.6     skrll /*
    569  1.39.30.6     skrll  * XXX
    570  1.39.30.6     skrll  * The encaptab list and the rnh radix tree must be manipulated atomically.
    571  1.39.30.6     skrll  */
    572        1.7    itojun static int
    573       1.23     perry encap_remove(struct encaptab *ep)
    574        1.7    itojun {
    575  1.39.30.6     skrll #ifdef USE_RADIX
    576        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(ep->af);
    577  1.39.30.6     skrll #endif
    578        1.7    itojun 	int error = 0;
    579        1.7    itojun 
    580  1.39.30.6     skrll 	KASSERT(encap_lock_held());
    581  1.39.30.6     skrll 
    582  1.39.30.6     skrll #ifdef USE_RADIX
    583        1.7    itojun 	if (!ep->func && rnh) {
    584  1.39.30.6     skrll 		/* Disable access to the radix tree for reader. */
    585  1.39.30.6     skrll 		encap_head_updating = true;
    586  1.39.30.6     skrll 		/* Wait for all readers to drain. */
    587  1.39.30.6     skrll 		pserialize_perform(encaptab.psz);
    588  1.39.30.6     skrll 
    589       1.30  christos 		if (!rnh->rnh_deladdr((void *)ep->addrpack,
    590       1.30  christos 		    (void *)ep->maskpack, rnh))
    591        1.7    itojun 			error = ESRCH;
    592  1.39.30.6     skrll 
    593  1.39.30.6     skrll 		/*
    594  1.39.30.6     skrll 		 * The ep added to the radix tree must be skipped while
    595  1.39.30.6     skrll 		 * encap[46]_lookup walks encaptab list. In other words,
    596  1.39.30.6     skrll 		 * encap_add() does not need to care whether the ep has
    597  1.39.30.6     skrll 		 * been added encaptab list or not yet.
    598  1.39.30.6     skrll 		 * So, we can re-enable access to the radix tree for now.
    599  1.39.30.6     skrll 		 */
    600  1.39.30.6     skrll 		encap_head_updating = false;
    601        1.7    itojun 	}
    602  1.39.30.6     skrll #endif
    603  1.39.30.6     skrll 	PSLIST_WRITER_REMOVE(ep, chain);
    604  1.39.30.6     skrll 
    605        1.7    itojun 	return error;
    606        1.7    itojun }
    607        1.7    itojun 
    608        1.7    itojun static int
    609       1.23     perry encap_afcheck(int af, const struct sockaddr *sp, const struct sockaddr *dp)
    610        1.7    itojun {
    611        1.7    itojun 	if (sp && dp) {
    612        1.7    itojun 		if (sp->sa_len != dp->sa_len)
    613        1.7    itojun 			return EINVAL;
    614        1.7    itojun 		if (af != sp->sa_family || af != dp->sa_family)
    615        1.7    itojun 			return EINVAL;
    616        1.7    itojun 	} else if (!sp && !dp)
    617        1.7    itojun 		;
    618        1.7    itojun 	else
    619        1.7    itojun 		return EINVAL;
    620        1.7    itojun 
    621        1.7    itojun 	switch (af) {
    622        1.7    itojun 	case AF_INET:
    623        1.7    itojun 		if (sp && sp->sa_len != sizeof(struct sockaddr_in))
    624        1.7    itojun 			return EINVAL;
    625        1.7    itojun 		if (dp && dp->sa_len != sizeof(struct sockaddr_in))
    626        1.7    itojun 			return EINVAL;
    627        1.7    itojun 		break;
    628        1.7    itojun #ifdef INET6
    629        1.7    itojun 	case AF_INET6:
    630        1.7    itojun 		if (sp && sp->sa_len != sizeof(struct sockaddr_in6))
    631        1.7    itojun 			return EINVAL;
    632        1.7    itojun 		if (dp && dp->sa_len != sizeof(struct sockaddr_in6))
    633        1.7    itojun 			return EINVAL;
    634        1.7    itojun 		break;
    635        1.7    itojun #endif
    636        1.7    itojun 	default:
    637        1.7    itojun 		return EAFNOSUPPORT;
    638        1.7    itojun 	}
    639        1.7    itojun 
    640        1.7    itojun 	return 0;
    641        1.1    itojun }
    642        1.1    itojun 
    643        1.1    itojun /*
    644        1.1    itojun  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
    645        1.1    itojun  * length of mask (sm and dm) is assumed to be same as sp/dp.
    646        1.1    itojun  * Return value will be necessary as input (cookie) for encap_detach().
    647        1.1    itojun  */
    648        1.1    itojun const struct encaptab *
    649       1.23     perry encap_attach(int af, int proto,
    650       1.23     perry     const struct sockaddr *sp, const struct sockaddr *sm,
    651       1.23     perry     const struct sockaddr *dp, const struct sockaddr *dm,
    652  1.39.30.4     skrll     const struct encapsw *esw, void *arg)
    653        1.1    itojun {
    654        1.1    itojun 	struct encaptab *ep;
    655        1.1    itojun 	int error;
    656  1.39.30.6     skrll 	int pss;
    657        1.7    itojun 	size_t l;
    658       1.33     pooka 	struct ip_pack4 *pack4;
    659        1.7    itojun #ifdef INET6
    660       1.33     pooka 	struct ip_pack6 *pack6;
    661        1.7    itojun #endif
    662  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    663  1.39.30.6     skrll 	int s;
    664        1.1    itojun 
    665        1.1    itojun 	s = splsoftnet();
    666  1.39.30.6     skrll #endif
    667        1.1    itojun 	/* sanity check on args */
    668        1.7    itojun 	error = encap_afcheck(af, sp, dp);
    669        1.7    itojun 	if (error)
    670        1.1    itojun 		goto fail;
    671        1.1    itojun 
    672        1.1    itojun 	/* check if anyone have already attached with exactly same config */
    673  1.39.30.6     skrll 	pss = pserialize_read_enter();
    674  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    675        1.1    itojun 		if (ep->af != af)
    676        1.1    itojun 			continue;
    677        1.1    itojun 		if (ep->proto != proto)
    678        1.1    itojun 			continue;
    679        1.7    itojun 		if (ep->func)
    680        1.7    itojun 			continue;
    681  1.39.30.1     skrll 
    682  1.39.30.1     skrll 		KASSERT(ep->src != NULL);
    683  1.39.30.1     skrll 		KASSERT(ep->dst != NULL);
    684  1.39.30.1     skrll 		KASSERT(ep->srcmask != NULL);
    685  1.39.30.1     skrll 		KASSERT(ep->dstmask != NULL);
    686  1.39.30.1     skrll 
    687        1.7    itojun 		if (ep->src->sa_len != sp->sa_len ||
    688       1.34    cegger 		    memcmp(ep->src, sp, sp->sa_len) != 0 ||
    689       1.34    cegger 		    memcmp(ep->srcmask, sm, sp->sa_len) != 0)
    690        1.7    itojun 			continue;
    691        1.7    itojun 		if (ep->dst->sa_len != dp->sa_len ||
    692       1.34    cegger 		    memcmp(ep->dst, dp, dp->sa_len) != 0 ||
    693       1.34    cegger 		    memcmp(ep->dstmask, dm, dp->sa_len) != 0)
    694        1.1    itojun 			continue;
    695        1.1    itojun 
    696        1.1    itojun 		error = EEXIST;
    697  1.39.30.6     skrll 		pserialize_read_exit(pss);
    698        1.1    itojun 		goto fail;
    699        1.1    itojun 	}
    700  1.39.30.6     skrll 	pserialize_read_exit(pss);
    701        1.3   thorpej 
    702        1.7    itojun 	switch (af) {
    703        1.7    itojun 	case AF_INET:
    704        1.7    itojun 		l = sizeof(*pack4);
    705        1.7    itojun 		break;
    706        1.7    itojun #ifdef INET6
    707        1.7    itojun 	case AF_INET6:
    708        1.7    itojun 		l = sizeof(*pack6);
    709        1.7    itojun 		break;
    710        1.7    itojun #endif
    711        1.7    itojun 	default:
    712        1.7    itojun 		goto fail;
    713        1.7    itojun 	}
    714        1.7    itojun 
    715       1.20    itojun 	/* M_NETADDR ok? */
    716  1.39.30.3     skrll 	ep = kmem_zalloc(sizeof(*ep), KM_NOSLEEP);
    717        1.1    itojun 	if (ep == NULL) {
    718        1.1    itojun 		error = ENOBUFS;
    719        1.1    itojun 		goto fail;
    720        1.1    itojun 	}
    721  1.39.30.3     skrll 	ep->addrpack = kmem_zalloc(l, KM_NOSLEEP);
    722        1.7    itojun 	if (ep->addrpack == NULL) {
    723        1.7    itojun 		error = ENOBUFS;
    724        1.7    itojun 		goto gc;
    725        1.7    itojun 	}
    726  1.39.30.3     skrll 	ep->maskpack = kmem_zalloc(l, KM_NOSLEEP);
    727        1.7    itojun 	if (ep->maskpack == NULL) {
    728        1.7    itojun 		error = ENOBUFS;
    729        1.7    itojun 		goto gc;
    730        1.7    itojun 	}
    731        1.1    itojun 
    732        1.1    itojun 	ep->af = af;
    733        1.1    itojun 	ep->proto = proto;
    734        1.7    itojun 	ep->addrpack->sa_len = l & 0xff;
    735        1.7    itojun 	ep->maskpack->sa_len = l & 0xff;
    736        1.7    itojun 	switch (af) {
    737        1.7    itojun 	case AF_INET:
    738       1.33     pooka 		pack4 = (struct ip_pack4 *)ep->addrpack;
    739        1.7    itojun 		ep->src = (struct sockaddr *)&pack4->mine;
    740        1.7    itojun 		ep->dst = (struct sockaddr *)&pack4->yours;
    741       1.33     pooka 		pack4 = (struct ip_pack4 *)ep->maskpack;
    742        1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack4->mine;
    743        1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack4->yours;
    744        1.7    itojun 		break;
    745        1.7    itojun #ifdef INET6
    746        1.7    itojun 	case AF_INET6:
    747       1.33     pooka 		pack6 = (struct ip_pack6 *)ep->addrpack;
    748        1.7    itojun 		ep->src = (struct sockaddr *)&pack6->mine;
    749        1.7    itojun 		ep->dst = (struct sockaddr *)&pack6->yours;
    750       1.33     pooka 		pack6 = (struct ip_pack6 *)ep->maskpack;
    751        1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack6->mine;
    752        1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack6->yours;
    753        1.7    itojun 		break;
    754        1.7    itojun #endif
    755        1.7    itojun 	}
    756        1.7    itojun 
    757       1.37   tsutsui 	memcpy(ep->src, sp, sp->sa_len);
    758       1.37   tsutsui 	memcpy(ep->srcmask, sm, sp->sa_len);
    759       1.37   tsutsui 	memcpy(ep->dst, dp, dp->sa_len);
    760       1.37   tsutsui 	memcpy(ep->dstmask, dm, dp->sa_len);
    761  1.39.30.4     skrll 	ep->esw = esw;
    762        1.1    itojun 	ep->arg = arg;
    763  1.39.30.6     skrll 	psref_target_init(&ep->psref, encaptab.elem_class);
    764        1.1    itojun 
    765        1.7    itojun 	error = encap_add(ep);
    766        1.7    itojun 	if (error)
    767        1.7    itojun 		goto gc;
    768        1.1    itojun 
    769        1.1    itojun 	error = 0;
    770  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    771        1.1    itojun 	splx(s);
    772  1.39.30.6     skrll #endif
    773        1.1    itojun 	return ep;
    774        1.1    itojun 
    775        1.7    itojun gc:
    776        1.7    itojun 	if (ep->addrpack)
    777  1.39.30.3     skrll 		kmem_free(ep->addrpack, l);
    778        1.7    itojun 	if (ep->maskpack)
    779  1.39.30.3     skrll 		kmem_free(ep->maskpack, l);
    780        1.7    itojun 	if (ep)
    781  1.39.30.3     skrll 		kmem_free(ep, sizeof(*ep));
    782        1.1    itojun fail:
    783  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    784        1.1    itojun 	splx(s);
    785  1.39.30.6     skrll #endif
    786        1.1    itojun 	return NULL;
    787        1.1    itojun }
    788        1.1    itojun 
    789        1.1    itojun const struct encaptab *
    790       1.23     perry encap_attach_func(int af, int proto,
    791       1.26    martin     int (*func)(struct mbuf *, int, int, void *),
    792  1.39.30.4     skrll     const struct encapsw *esw, void *arg)
    793        1.1    itojun {
    794        1.1    itojun 	struct encaptab *ep;
    795        1.1    itojun 	int error;
    796  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    797        1.1    itojun 	int s;
    798        1.1    itojun 
    799        1.1    itojun 	s = splsoftnet();
    800  1.39.30.6     skrll #endif
    801        1.1    itojun 	/* sanity check on args */
    802        1.1    itojun 	if (!func) {
    803        1.1    itojun 		error = EINVAL;
    804        1.1    itojun 		goto fail;
    805        1.1    itojun 	}
    806        1.1    itojun 
    807        1.7    itojun 	error = encap_afcheck(af, NULL, NULL);
    808        1.7    itojun 	if (error)
    809        1.7    itojun 		goto fail;
    810        1.7    itojun 
    811  1.39.30.3     skrll 	ep = kmem_alloc(sizeof(*ep), KM_NOSLEEP);	/*XXX*/
    812        1.1    itojun 	if (ep == NULL) {
    813        1.1    itojun 		error = ENOBUFS;
    814        1.1    itojun 		goto fail;
    815        1.1    itojun 	}
    816       1.35    cegger 	memset(ep, 0, sizeof(*ep));
    817        1.1    itojun 
    818        1.1    itojun 	ep->af = af;
    819        1.1    itojun 	ep->proto = proto;
    820        1.1    itojun 	ep->func = func;
    821  1.39.30.4     skrll 	ep->esw = esw;
    822        1.1    itojun 	ep->arg = arg;
    823  1.39.30.6     skrll 	psref_target_init(&ep->psref, encaptab.elem_class);
    824        1.1    itojun 
    825        1.7    itojun 	error = encap_add(ep);
    826        1.7    itojun 	if (error)
    827        1.7    itojun 		goto fail;
    828        1.1    itojun 
    829        1.1    itojun 	error = 0;
    830  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    831        1.1    itojun 	splx(s);
    832  1.39.30.6     skrll #endif
    833        1.1    itojun 	return ep;
    834        1.1    itojun 
    835        1.1    itojun fail:
    836  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    837        1.1    itojun 	splx(s);
    838  1.39.30.6     skrll #endif
    839        1.1    itojun 	return NULL;
    840        1.1    itojun }
    841        1.1    itojun 
    842        1.7    itojun /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
    843        1.7    itojun 
    844        1.7    itojun #ifdef INET6
    845       1.32        ad void *
    846       1.29    dyoung encap6_ctlinput(int cmd, const struct sockaddr *sa, void *d0)
    847        1.7    itojun {
    848        1.7    itojun 	void *d = d0;
    849        1.7    itojun 	struct ip6_hdr *ip6;
    850        1.7    itojun 	struct mbuf *m;
    851        1.7    itojun 	int off;
    852        1.7    itojun 	struct ip6ctlparam *ip6cp = NULL;
    853        1.7    itojun 	int nxt;
    854  1.39.30.6     skrll 	int s;
    855        1.7    itojun 	struct encaptab *ep;
    856  1.39.30.4     skrll 	const struct encapsw *esw;
    857        1.7    itojun 
    858        1.7    itojun 	if (sa->sa_family != AF_INET6 ||
    859        1.7    itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    860       1.32        ad 		return NULL;
    861        1.7    itojun 
    862        1.7    itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    863       1.32        ad 		return NULL;
    864        1.7    itojun 	if (cmd == PRC_HOSTDEAD)
    865        1.7    itojun 		d = NULL;
    866        1.7    itojun 	else if (cmd == PRC_MSGSIZE)
    867        1.7    itojun 		; /* special code is present, see below */
    868        1.7    itojun 	else if (inet6ctlerrmap[cmd] == 0)
    869       1.32        ad 		return NULL;
    870        1.7    itojun 
    871        1.7    itojun 	/* if the parameter is from icmp6, decode it. */
    872        1.7    itojun 	if (d != NULL) {
    873        1.7    itojun 		ip6cp = (struct ip6ctlparam *)d;
    874        1.7    itojun 		m = ip6cp->ip6c_m;
    875        1.7    itojun 		ip6 = ip6cp->ip6c_ip6;
    876        1.7    itojun 		off = ip6cp->ip6c_off;
    877        1.7    itojun 		nxt = ip6cp->ip6c_nxt;
    878       1.15   mycroft 
    879       1.15   mycroft 		if (ip6 && cmd == PRC_MSGSIZE) {
    880       1.15   mycroft 			int valid = 0;
    881       1.15   mycroft 			struct encaptab *match;
    882  1.39.30.6     skrll 			struct psref elem_psref;
    883       1.15   mycroft 
    884       1.15   mycroft 			/*
    885       1.15   mycroft 		 	* Check to see if we have a valid encap configuration.
    886       1.15   mycroft 		 	*/
    887  1.39.30.6     skrll 			match = encap6_lookup(m, off, nxt, OUTBOUND,
    888  1.39.30.6     skrll 			    &elem_psref);
    889       1.15   mycroft 			if (match)
    890       1.15   mycroft 				valid++;
    891  1.39.30.6     skrll 			psref_release(&elem_psref, &match->psref,
    892  1.39.30.6     skrll 			    encaptab.elem_class);
    893       1.15   mycroft 
    894       1.15   mycroft 			/*
    895       1.15   mycroft 		 	* Depending on the value of "valid" and routing table
    896       1.15   mycroft 		 	* size (mtudisc_{hi,lo}wat), we will:
    897       1.15   mycroft 		 	* - recalcurate the new MTU and create the
    898       1.15   mycroft 		 	*   corresponding routing entry, or
    899       1.15   mycroft 		 	* - ignore the MTU change notification.
    900       1.15   mycroft 		 	*/
    901       1.15   mycroft 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    902       1.15   mycroft 		}
    903        1.7    itojun 	} else {
    904        1.7    itojun 		m = NULL;
    905        1.7    itojun 		ip6 = NULL;
    906        1.7    itojun 		nxt = -1;
    907        1.7    itojun 	}
    908        1.7    itojun 
    909        1.7    itojun 	/* inform all listeners */
    910  1.39.30.6     skrll 
    911  1.39.30.6     skrll 	s = pserialize_read_enter();
    912  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    913  1.39.30.6     skrll 		struct psref elem_psref;
    914  1.39.30.6     skrll 
    915        1.7    itojun 		if (ep->af != AF_INET6)
    916        1.7    itojun 			continue;
    917        1.7    itojun 		if (ep->proto >= 0 && ep->proto != nxt)
    918        1.7    itojun 			continue;
    919        1.7    itojun 
    920        1.7    itojun 		/* should optimize by looking at address pairs */
    921        1.7    itojun 
    922        1.7    itojun 		/* XXX need to pass ep->arg or ep itself to listeners */
    923  1.39.30.6     skrll 		psref_acquire(&elem_psref, &ep->psref,
    924  1.39.30.6     skrll 		    encaptab.elem_class);
    925  1.39.30.4     skrll 		esw = ep->esw;
    926  1.39.30.4     skrll 		if (esw && esw->encapsw6.pr_ctlinput) {
    927  1.39.30.6     skrll 			pserialize_read_exit(s);
    928  1.39.30.6     skrll 			/* pr_ctlinput is sleepable. e.g. rtcache_free */
    929  1.39.30.4     skrll 			(*esw->encapsw6.pr_ctlinput)(cmd, sa, d, ep->arg);
    930  1.39.30.6     skrll 			s = pserialize_read_enter();
    931  1.39.30.4     skrll 		}
    932  1.39.30.6     skrll 		psref_release(&elem_psref, &ep->psref,
    933  1.39.30.6     skrll 		    encaptab.elem_class);
    934        1.7    itojun 	}
    935  1.39.30.6     skrll 	pserialize_read_exit(s);
    936        1.7    itojun 
    937        1.7    itojun 	rip6_ctlinput(cmd, sa, d0);
    938       1.32        ad 	return NULL;
    939        1.7    itojun }
    940        1.7    itojun #endif
    941        1.7    itojun 
    942        1.1    itojun int
    943       1.23     perry encap_detach(const struct encaptab *cookie)
    944        1.1    itojun {
    945        1.1    itojun 	const struct encaptab *ep = cookie;
    946  1.39.30.6     skrll 	struct encaptab *p;
    947        1.7    itojun 	int error;
    948        1.1    itojun 
    949  1.39.30.6     skrll 	KASSERT(encap_lock_held());
    950  1.39.30.6     skrll 
    951  1.39.30.6     skrll 	PSLIST_WRITER_FOREACH(p, &encap_table, struct encaptab, chain) {
    952        1.1    itojun 		if (p == ep) {
    953        1.7    itojun 			error = encap_remove(p);
    954        1.7    itojun 			if (error)
    955        1.7    itojun 				return error;
    956  1.39.30.6     skrll 			else
    957  1.39.30.6     skrll 				break;
    958        1.1    itojun 		}
    959        1.1    itojun 	}
    960  1.39.30.6     skrll 	if (p == NULL)
    961  1.39.30.6     skrll 		return ENOENT;
    962        1.1    itojun 
    963  1.39.30.6     skrll 	pserialize_perform(encaptab.psz);
    964  1.39.30.6     skrll 	psref_target_destroy(&p->psref,
    965  1.39.30.6     skrll 	    encaptab.elem_class);
    966  1.39.30.6     skrll 	if (!ep->func) {
    967  1.39.30.6     skrll 		kmem_free(p->addrpack, ep->addrpack->sa_len);
    968  1.39.30.6     skrll 		kmem_free(p->maskpack, ep->maskpack->sa_len);
    969  1.39.30.6     skrll 	}
    970  1.39.30.6     skrll 	kmem_free(p, sizeof(*p));
    971  1.39.30.6     skrll 
    972  1.39.30.6     skrll 	return 0;
    973        1.7    itojun }
    974        1.7    itojun 
    975  1.39.30.6     skrll #ifdef USE_RADIX
    976        1.7    itojun static struct radix_node_head *
    977       1.23     perry encap_rnh(int af)
    978        1.7    itojun {
    979        1.7    itojun 
    980        1.7    itojun 	switch (af) {
    981        1.7    itojun 	case AF_INET:
    982        1.7    itojun 		return encap_head[0];
    983        1.7    itojun #ifdef INET6
    984        1.7    itojun 	case AF_INET6:
    985        1.7    itojun 		return encap_head[1];
    986        1.7    itojun #endif
    987        1.7    itojun 	default:
    988        1.7    itojun 		return NULL;
    989        1.7    itojun 	}
    990        1.7    itojun }
    991        1.7    itojun 
    992        1.7    itojun static int
    993       1.23     perry mask_matchlen(const struct sockaddr *sa)
    994        1.7    itojun {
    995        1.7    itojun 	const char *p, *ep;
    996        1.7    itojun 	int l;
    997        1.7    itojun 
    998        1.7    itojun 	p = (const char *)sa;
    999        1.7    itojun 	ep = p + sa->sa_len;
   1000        1.7    itojun 	p += 2;	/* sa_len + sa_family */
   1001        1.7    itojun 
   1002        1.7    itojun 	l = 0;
   1003        1.7    itojun 	while (p < ep) {
   1004        1.7    itojun 		l += (*p ? 8 : 0);	/* estimate */
   1005        1.7    itojun 		p++;
   1006        1.7    itojun 	}
   1007        1.7    itojun 	return l;
   1008        1.1    itojun }
   1009  1.39.30.6     skrll #endif
   1010  1.39.30.6     skrll 
   1011  1.39.30.6     skrll #ifndef USE_RADIX
   1012  1.39.30.6     skrll static int
   1013  1.39.30.6     skrll mask_match(const struct encaptab *ep,
   1014  1.39.30.6     skrll 	   const struct sockaddr *sp,
   1015  1.39.30.6     skrll 	   const struct sockaddr *dp)
   1016  1.39.30.6     skrll {
   1017  1.39.30.6     skrll 	struct sockaddr_storage s;
   1018  1.39.30.6     skrll 	struct sockaddr_storage d;
   1019  1.39.30.6     skrll 	int i;
   1020  1.39.30.6     skrll 	const u_int8_t *p, *q;
   1021  1.39.30.6     skrll 	u_int8_t *r;
   1022  1.39.30.6     skrll 	int matchlen;
   1023  1.39.30.6     skrll 
   1024  1.39.30.6     skrll 	KASSERTMSG(ep->func == NULL, "wrong encaptab passed to mask_match");
   1025  1.39.30.6     skrll 
   1026  1.39.30.6     skrll 	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
   1027  1.39.30.6     skrll 		return 0;
   1028  1.39.30.6     skrll 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
   1029  1.39.30.6     skrll 		return 0;
   1030  1.39.30.6     skrll 	if (sp->sa_len != ep->src->sa_len || dp->sa_len != ep->dst->sa_len)
   1031  1.39.30.6     skrll 		return 0;
   1032  1.39.30.6     skrll 
   1033  1.39.30.6     skrll 	matchlen = 0;
   1034  1.39.30.6     skrll 
   1035  1.39.30.6     skrll 	p = (const u_int8_t *)sp;
   1036  1.39.30.6     skrll 	q = (const u_int8_t *)ep->srcmask;
   1037  1.39.30.6     skrll 	r = (u_int8_t *)&s;
   1038  1.39.30.6     skrll 	for (i = 0 ; i < sp->sa_len; i++) {
   1039  1.39.30.6     skrll 		r[i] = p[i] & q[i];
   1040  1.39.30.6     skrll 		/* XXX estimate */
   1041  1.39.30.6     skrll 		matchlen += (q[i] ? 8 : 0);
   1042  1.39.30.6     skrll 	}
   1043  1.39.30.6     skrll 
   1044  1.39.30.6     skrll 	p = (const u_int8_t *)dp;
   1045  1.39.30.6     skrll 	q = (const u_int8_t *)ep->dstmask;
   1046  1.39.30.6     skrll 	r = (u_int8_t *)&d;
   1047  1.39.30.6     skrll 	for (i = 0 ; i < dp->sa_len; i++) {
   1048  1.39.30.6     skrll 		r[i] = p[i] & q[i];
   1049  1.39.30.6     skrll 		/* XXX rough estimate */
   1050  1.39.30.6     skrll 		matchlen += (q[i] ? 8 : 0);
   1051  1.39.30.6     skrll 	}
   1052  1.39.30.6     skrll 
   1053  1.39.30.6     skrll 	/* need to overwrite len/family portion as we don't compare them */
   1054  1.39.30.6     skrll 	s.ss_len = sp->sa_len;
   1055  1.39.30.6     skrll 	s.ss_family = sp->sa_family;
   1056  1.39.30.6     skrll 	d.ss_len = dp->sa_len;
   1057  1.39.30.6     skrll 	d.ss_family = dp->sa_family;
   1058  1.39.30.6     skrll 
   1059  1.39.30.6     skrll 	if (memcmp(&s, ep->src, ep->src->sa_len) == 0 &&
   1060  1.39.30.6     skrll 	    memcmp(&d, ep->dst, ep->dst->sa_len) == 0) {
   1061  1.39.30.6     skrll 		return matchlen;
   1062  1.39.30.6     skrll 	} else
   1063  1.39.30.6     skrll 		return 0;
   1064  1.39.30.6     skrll }
   1065  1.39.30.6     skrll #endif
   1066        1.1    itojun 
   1067        1.1    itojun static void
   1068       1.23     perry encap_fillarg(struct mbuf *m, const struct encaptab *ep)
   1069        1.1    itojun {
   1070       1.12    itojun 	struct m_tag *mtag;
   1071        1.1    itojun 
   1072       1.12    itojun 	mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
   1073       1.12    itojun 	if (mtag) {
   1074       1.12    itojun 		*(void **)(mtag + 1) = ep->arg;
   1075       1.12    itojun 		m_tag_prepend(m, mtag);
   1076        1.1    itojun 	}
   1077        1.1    itojun }
   1078        1.1    itojun 
   1079        1.1    itojun void *
   1080       1.23     perry encap_getarg(struct mbuf *m)
   1081        1.1    itojun {
   1082        1.1    itojun 	void *p;
   1083       1.12    itojun 	struct m_tag *mtag;
   1084        1.1    itojun 
   1085        1.1    itojun 	p = NULL;
   1086       1.12    itojun 	mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
   1087       1.12    itojun 	if (mtag != NULL) {
   1088       1.13    itojun 		p = *(void **)(mtag + 1);
   1089       1.12    itojun 		m_tag_delete(m, mtag);
   1090        1.1    itojun 	}
   1091        1.1    itojun 	return p;
   1092        1.1    itojun }
   1093  1.39.30.6     skrll 
   1094  1.39.30.6     skrll int
   1095  1.39.30.6     skrll encap_lock_enter(void)
   1096  1.39.30.6     skrll {
   1097  1.39.30.6     skrll 	int error;
   1098  1.39.30.6     skrll 
   1099  1.39.30.6     skrll 	mutex_enter(&encap_whole.lock);
   1100  1.39.30.6     skrll 	while (encap_whole.busy != NULL) {
   1101  1.39.30.6     skrll 		error = cv_wait_sig(&encap_whole.cv, &encap_whole.lock);
   1102  1.39.30.6     skrll 		if (error) {
   1103  1.39.30.6     skrll 			mutex_exit(&encap_whole.lock);
   1104  1.39.30.6     skrll 			return error;
   1105  1.39.30.6     skrll 		}
   1106  1.39.30.6     skrll 	}
   1107  1.39.30.6     skrll 	KASSERT(encap_whole.busy == NULL);
   1108  1.39.30.6     skrll 	encap_whole.busy = curlwp;
   1109  1.39.30.6     skrll 	mutex_exit(&encap_whole.lock);
   1110  1.39.30.6     skrll 
   1111  1.39.30.6     skrll 	return 0;
   1112  1.39.30.6     skrll }
   1113  1.39.30.6     skrll 
   1114  1.39.30.6     skrll void
   1115  1.39.30.6     skrll encap_lock_exit(void)
   1116  1.39.30.6     skrll {
   1117  1.39.30.6     skrll 
   1118  1.39.30.6     skrll 	mutex_enter(&encap_whole.lock);
   1119  1.39.30.6     skrll 	KASSERT(encap_whole.busy == curlwp);
   1120  1.39.30.6     skrll 	encap_whole.busy = NULL;
   1121  1.39.30.6     skrll 	cv_broadcast(&encap_whole.cv);
   1122  1.39.30.6     skrll 	mutex_exit(&encap_whole.lock);
   1123  1.39.30.6     skrll }
   1124  1.39.30.6     skrll 
   1125  1.39.30.6     skrll bool
   1126  1.39.30.6     skrll encap_lock_held(void)
   1127  1.39.30.6     skrll {
   1128  1.39.30.6     skrll 
   1129  1.39.30.6     skrll 	return (encap_whole.busy == curlwp);
   1130  1.39.30.6     skrll }
   1131