Home | History | Annotate | Line # | Download | only in netinet
ip_encap.c revision 1.39.30.6
      1  1.39.30.6     skrll /*	$NetBSD: ip_encap.c,v 1.39.30.6 2016/07/09 20:25:22 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.6     skrll __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.39.30.6 2016/07/09 20:25:22 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.6     skrll /*
    165  1.39.30.6     skrll  * must be done before other encap interfaces initialization.
    166  1.39.30.6     skrll  */
    167  1.39.30.6     skrll void
    168  1.39.30.6     skrll encapinit(void)
    169  1.39.30.6     skrll {
    170  1.39.30.6     skrll 
    171  1.39.30.6     skrll 	encaptab.psz = pserialize_create();
    172  1.39.30.6     skrll 	encaptab.elem_class = psref_class_create("encapelem", IPL_SOFTNET);
    173  1.39.30.6     skrll 	if (encaptab.elem_class == NULL)
    174  1.39.30.6     skrll 		panic("encaptab.elem_class cannot be allocated.\n");
    175  1.39.30.6     skrll 
    176  1.39.30.6     skrll 	mutex_init(&encap_whole.lock, MUTEX_DEFAULT, IPL_NONE);
    177  1.39.30.6     skrll 	cv_init(&encap_whole.cv, "ip_encap cv");
    178  1.39.30.6     skrll 	encap_whole.busy = NULL;
    179  1.39.30.6     skrll }
    180        1.7    itojun 
    181        1.1    itojun void
    182       1.23     perry encap_init(void)
    183        1.1    itojun {
    184        1.7    itojun 	static int initialized = 0;
    185        1.7    itojun 
    186        1.7    itojun 	if (initialized)
    187        1.7    itojun 		return;
    188        1.7    itojun 	initialized++;
    189        1.1    itojun #if 0
    190        1.1    itojun 	/*
    191        1.1    itojun 	 * we cannot use LIST_INIT() here, since drivers may want to call
    192        1.4    itojun 	 * encap_attach(), on driver attach.  encap_init() will be called
    193        1.1    itojun 	 * on AF_INET{,6} initialization, which happens after driver
    194        1.1    itojun 	 * initialization - using LIST_INIT() here can nuke encap_attach()
    195        1.1    itojun 	 * from drivers.
    196        1.1    itojun 	 */
    197  1.39.30.6     skrll 	PSLIST_INIT(&encap_table);
    198        1.1    itojun #endif
    199        1.7    itojun 
    200  1.39.30.6     skrll #ifdef USE_RADIX
    201        1.7    itojun 	/*
    202       1.38     pooka 	 * initialize radix lookup table when the radix subsystem is inited.
    203        1.7    itojun 	 */
    204       1.38     pooka 	rn_delayedinit((void *)&encap_head[0],
    205       1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    206        1.7    itojun #ifdef INET6
    207       1.38     pooka 	rn_delayedinit((void *)&encap_head[1],
    208       1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    209        1.7    itojun #endif
    210  1.39.30.6     skrll #endif
    211        1.1    itojun }
    212        1.1    itojun 
    213        1.4    itojun #ifdef INET
    214        1.7    itojun static struct encaptab *
    215  1.39.30.6     skrll encap4_lookup(struct mbuf *m, int off, int proto, enum direction dir,
    216  1.39.30.6     skrll     struct psref *match_psref)
    217        1.1    itojun {
    218        1.1    itojun 	struct ip *ip;
    219       1.33     pooka 	struct ip_pack4 pack;
    220        1.1    itojun 	struct encaptab *ep, *match;
    221        1.1    itojun 	int prio, matchprio;
    222  1.39.30.6     skrll 	int s;
    223  1.39.30.6     skrll #ifdef USE_RADIX
    224        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET);
    225        1.7    itojun 	struct radix_node *rn;
    226  1.39.30.6     skrll #endif
    227        1.1    itojun 
    228  1.39.30.1     skrll 	KASSERT(m->m_len >= sizeof(*ip));
    229  1.39.30.1     skrll 
    230        1.1    itojun 	ip = mtod(m, struct ip *);
    231        1.1    itojun 
    232       1.35    cegger 	memset(&pack, 0, sizeof(pack));
    233        1.7    itojun 	pack.p.sp_len = sizeof(pack);
    234        1.7    itojun 	pack.mine.sin_family = pack.yours.sin_family = AF_INET;
    235        1.7    itojun 	pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
    236        1.7    itojun 	if (dir == INBOUND) {
    237        1.7    itojun 		pack.mine.sin_addr = ip->ip_dst;
    238        1.7    itojun 		pack.yours.sin_addr = ip->ip_src;
    239        1.7    itojun 	} else {
    240        1.7    itojun 		pack.mine.sin_addr = ip->ip_src;
    241        1.7    itojun 		pack.yours.sin_addr = ip->ip_dst;
    242        1.7    itojun 	}
    243        1.1    itojun 
    244        1.1    itojun 	match = NULL;
    245        1.1    itojun 	matchprio = 0;
    246        1.7    itojun 
    247  1.39.30.6     skrll 	s = pserialize_read_enter();
    248  1.39.30.6     skrll #ifdef USE_RADIX
    249  1.39.30.6     skrll 	if (encap_head_updating) {
    250  1.39.30.6     skrll 		/*
    251  1.39.30.6     skrll 		 * Update in progress. Do nothing.
    252  1.39.30.6     skrll 		 */
    253  1.39.30.6     skrll 		pserialize_read_exit(s);
    254  1.39.30.6     skrll 		return NULL;
    255  1.39.30.6     skrll 	}
    256  1.39.30.6     skrll 
    257       1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    258        1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    259  1.39.30.6     skrll 		struct encaptab *encapp = (struct encaptab *)rn;
    260  1.39.30.6     skrll 
    261  1.39.30.6     skrll 		psref_acquire(match_psref, &encapp->psref,
    262  1.39.30.6     skrll 		    encaptab.elem_class);
    263  1.39.30.6     skrll 		match = encapp;
    264        1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    265        1.7    itojun 		    mask_matchlen(match->dstmask);
    266        1.7    itojun 	}
    267  1.39.30.6     skrll #endif
    268  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    269  1.39.30.6     skrll 		struct psref elem_psref;
    270  1.39.30.6     skrll 
    271  1.39.30.6     skrll 		membar_datadep_consumer();
    272        1.7    itojun 
    273        1.1    itojun 		if (ep->af != AF_INET)
    274        1.1    itojun 			continue;
    275        1.1    itojun 		if (ep->proto >= 0 && ep->proto != proto)
    276        1.1    itojun 			continue;
    277  1.39.30.6     skrll 
    278  1.39.30.6     skrll 		psref_acquire(&elem_psref, &ep->psref,
    279  1.39.30.6     skrll 		    encaptab.elem_class);
    280  1.39.30.6     skrll 		if (ep->func) {
    281  1.39.30.6     skrll 			pserialize_read_exit(s);
    282  1.39.30.6     skrll 			/* ep->func is sleepable. e.g. rtalloc1 */
    283        1.1    itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    284  1.39.30.6     skrll 			s = pserialize_read_enter();
    285  1.39.30.6     skrll 		} else {
    286  1.39.30.6     skrll #ifdef USE_RADIX
    287  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    288  1.39.30.6     skrll 			    encaptab.elem_class);
    289        1.7    itojun 			continue;
    290  1.39.30.6     skrll #else
    291  1.39.30.6     skrll 			prio = mask_match(ep, (struct sockaddr *)&pack.mine,
    292  1.39.30.6     skrll 			    (struct sockaddr *)&pack.yours);
    293  1.39.30.6     skrll #endif
    294  1.39.30.6     skrll 		}
    295        1.1    itojun 
    296        1.1    itojun 		/*
    297        1.1    itojun 		 * We prioritize the matches by using bit length of the
    298        1.1    itojun 		 * matches.  mask_match() and user-supplied matching function
    299        1.1    itojun 		 * should return the bit length of the matches (for example,
    300        1.1    itojun 		 * if both src/dst are matched for IPv4, 64 should be returned).
    301        1.1    itojun 		 * 0 or negative return value means "it did not match".
    302        1.1    itojun 		 *
    303        1.1    itojun 		 * The question is, since we have two "mask" portion, we
    304        1.1    itojun 		 * cannot really define total order between entries.
    305        1.1    itojun 		 * For example, which of these should be preferred?
    306        1.1    itojun 		 * mask_match() returns 48 (32 + 16) for both of them.
    307        1.1    itojun 		 *	src=3ffe::/16, dst=3ffe:501::/32
    308        1.1    itojun 		 *	src=3ffe:501::/32, dst=3ffe::/16
    309        1.1    itojun 		 *
    310        1.1    itojun 		 * We need to loop through all the possible candidates
    311        1.1    itojun 		 * to get the best match - the search takes O(n) for
    312        1.1    itojun 		 * n attachments (i.e. interfaces).
    313        1.7    itojun 		 *
    314        1.7    itojun 		 * For radix-based lookup, I guess source takes precedence.
    315        1.7    itojun 		 * See rn_{refines,lexobetter} for the correct answer.
    316        1.1    itojun 		 */
    317  1.39.30.6     skrll 		if (prio <= 0) {
    318  1.39.30.6     skrll 			psref_release(&elem_psref, &ep->psref,
    319  1.39.30.6     skrll 			    encaptab.elem_class);
    320        1.1    itojun 			continue;
    321  1.39.30.6     skrll 		}
    322        1.1    itojun 		if (prio > matchprio) {
    323  1.39.30.6     skrll 			/* release last matched ep */
    324  1.39.30.6     skrll 			if (match != NULL)
    325  1.39.30.6     skrll 				psref_release(match_psref, &match->psref,
    326  1.39.30.6     skrll 				    encaptab.elem_class);
    327  1.39.30.6     skrll 
    328  1.39.30.6     skrll 			psref_copy(match_psref, &elem_psref,
    329  1.39.30.6     skrll 			    encaptab.elem_class);
    330        1.1    itojun 			matchprio = prio;
    331        1.1    itojun 			match = ep;
    332        1.1    itojun 		}
    333  1.39.30.6     skrll 		KASSERTMSG((match == NULL) || psref_held(&match->psref,
    334  1.39.30.6     skrll 			encaptab.elem_class),
    335  1.39.30.6     skrll 		    "current match = %p, but not hold its psref", match);
    336  1.39.30.6     skrll 
    337  1.39.30.6     skrll 		psref_release(&elem_psref, &ep->psref,
    338  1.39.30.6     skrll 		    encaptab.elem_class);
    339        1.1    itojun 	}
    340  1.39.30.6     skrll 	pserialize_read_exit(s);
    341        1.1    itojun 
    342        1.7    itojun 	return match;
    343        1.7    itojun }
    344        1.7    itojun 
    345        1.7    itojun void
    346        1.7    itojun encap4_input(struct mbuf *m, ...)
    347        1.7    itojun {
    348        1.7    itojun 	int off, proto;
    349        1.7    itojun 	va_list ap;
    350  1.39.30.4     skrll 	const struct encapsw *esw;
    351        1.7    itojun 	struct encaptab *match;
    352  1.39.30.6     skrll 	struct psref match_psref;
    353        1.7    itojun 
    354        1.7    itojun 	va_start(ap, m);
    355        1.7    itojun 	off = va_arg(ap, int);
    356        1.7    itojun 	proto = va_arg(ap, int);
    357        1.7    itojun 	va_end(ap);
    358        1.7    itojun 
    359  1.39.30.6     skrll 	match = encap4_lookup(m, off, proto, INBOUND, &match_psref);
    360        1.1    itojun 	if (match) {
    361        1.1    itojun 		/* found a match, "match" has the best one */
    362  1.39.30.4     skrll 		esw = match->esw;
    363  1.39.30.4     skrll 		if (esw && esw->encapsw4.pr_input) {
    364        1.1    itojun 			encap_fillarg(m, match);
    365  1.39.30.4     skrll 			(*esw->encapsw4.pr_input)(m, off, proto);
    366  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    367  1.39.30.6     skrll 			    encaptab.elem_class);
    368  1.39.30.6     skrll 		} else {
    369  1.39.30.6     skrll 			psref_release(&match_psref, &match->psref,
    370  1.39.30.6     skrll 			    encaptab.elem_class);
    371        1.1    itojun 			m_freem(m);
    372  1.39.30.6     skrll 		}
    373        1.1    itojun 		return;
    374        1.1    itojun 	}
    375        1.1    itojun 
    376        1.1    itojun 	/* last resort: inject to raw socket */
    377        1.1    itojun 	rip_input(m, off, proto);
    378        1.1    itojun }
    379        1.1    itojun #endif
    380        1.1    itojun 
    381        1.1    itojun #ifdef INET6
    382        1.7    itojun static struct encaptab *
    383  1.39.30.6     skrll encap6_lookup(struct mbuf *m, int off, int proto, enum direction dir,
    384  1.39.30.6     skrll     struct psref *match_psref)
    385        1.1    itojun {
    386        1.1    itojun 	struct ip6_hdr *ip6;
    387       1.33     pooka 	struct ip_pack6 pack;
    388        1.7    itojun 	int prio, matchprio;
    389  1.39.30.6     skrll 	int s;
    390        1.1    itojun 	struct encaptab *ep, *match;
    391  1.39.30.6     skrll #ifdef USE_RADIX
    392        1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET6);
    393        1.7    itojun 	struct radix_node *rn;
    394  1.39.30.6     skrll #endif
    395        1.1    itojun 
    396  1.39.30.1     skrll 	KASSERT(m->m_len >= sizeof(*ip6));
    397  1.39.30.1     skrll 
    398        1.1    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    399        1.1    itojun 
    400       1.35    cegger 	memset(&pack, 0, sizeof(pack));
    401        1.7    itojun 	pack.p.sp_len = sizeof(pack);
    402        1.7    itojun 	pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
    403        1.7    itojun 	pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
    404        1.7    itojun 	if (dir == INBOUND) {
    405        1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_dst;
    406        1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_src;
    407        1.7    itojun 	} else {
    408        1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_src;
    409        1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_dst;
    410        1.7    itojun 	}
    411        1.1    itojun 
    412        1.1    itojun 	match = NULL;
    413        1.1    itojun 	matchprio = 0;
    414        1.7    itojun 
    415  1.39.30.6     skrll 	s = pserialize_read_enter();
    416  1.39.30.6     skrll #ifdef USE_RADIX
    417  1.39.30.6     skrll 	if (encap_head_updating) {
    418  1.39.30.6     skrll 		/*
    419  1.39.30.6     skrll 		 * Update in progress. Do nothing.
    420  1.39.30.6     skrll 		 */
    421  1.39.30.6     skrll 		pserialize_read_exit(s);
    422  1.39.30.6     skrll 		return NULL;
    423  1.39.30.6     skrll 	}
    424  1.39.30.6     skrll 
    425       1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    426        1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    427  1.39.30.6     skrll 		struct encaptab *encapp = (struct encaptab *)rn;
    428  1.39.30.6     skrll 
    429  1.39.30.6     skrll 		psref_acquire(match_psref, &encapp->psref,
    430  1.39.30.6     skrll 		    encaptab.elem_class);
    431  1.39.30.6     skrll 		match = encapp;
    432        1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    433        1.7    itojun 		    mask_matchlen(match->dstmask);
    434        1.7    itojun 	}
    435  1.39.30.6     skrll #endif
    436  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    437  1.39.30.6     skrll 		struct psref elem_psref;
    438  1.39.30.6     skrll 
    439  1.39.30.6     skrll 		membar_datadep_consumer();
    440        1.7    itojun 
    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.39.30.6     skrll 		membar_datadep_consumer();
    676  1.39.30.6     skrll 
    677        1.1    itojun 		if (ep->af != af)
    678        1.1    itojun 			continue;
    679        1.1    itojun 		if (ep->proto != proto)
    680        1.1    itojun 			continue;
    681        1.7    itojun 		if (ep->func)
    682        1.7    itojun 			continue;
    683  1.39.30.1     skrll 
    684  1.39.30.1     skrll 		KASSERT(ep->src != NULL);
    685  1.39.30.1     skrll 		KASSERT(ep->dst != NULL);
    686  1.39.30.1     skrll 		KASSERT(ep->srcmask != NULL);
    687  1.39.30.1     skrll 		KASSERT(ep->dstmask != NULL);
    688  1.39.30.1     skrll 
    689        1.7    itojun 		if (ep->src->sa_len != sp->sa_len ||
    690       1.34    cegger 		    memcmp(ep->src, sp, sp->sa_len) != 0 ||
    691       1.34    cegger 		    memcmp(ep->srcmask, sm, sp->sa_len) != 0)
    692        1.7    itojun 			continue;
    693        1.7    itojun 		if (ep->dst->sa_len != dp->sa_len ||
    694       1.34    cegger 		    memcmp(ep->dst, dp, dp->sa_len) != 0 ||
    695       1.34    cegger 		    memcmp(ep->dstmask, dm, dp->sa_len) != 0)
    696        1.1    itojun 			continue;
    697        1.1    itojun 
    698        1.1    itojun 		error = EEXIST;
    699  1.39.30.6     skrll 		pserialize_read_exit(pss);
    700        1.1    itojun 		goto fail;
    701        1.1    itojun 	}
    702  1.39.30.6     skrll 	pserialize_read_exit(pss);
    703        1.3   thorpej 
    704        1.7    itojun 	switch (af) {
    705        1.7    itojun 	case AF_INET:
    706        1.7    itojun 		l = sizeof(*pack4);
    707        1.7    itojun 		break;
    708        1.7    itojun #ifdef INET6
    709        1.7    itojun 	case AF_INET6:
    710        1.7    itojun 		l = sizeof(*pack6);
    711        1.7    itojun 		break;
    712        1.7    itojun #endif
    713        1.7    itojun 	default:
    714        1.7    itojun 		goto fail;
    715        1.7    itojun 	}
    716        1.7    itojun 
    717       1.20    itojun 	/* M_NETADDR ok? */
    718  1.39.30.3     skrll 	ep = kmem_zalloc(sizeof(*ep), KM_NOSLEEP);
    719        1.1    itojun 	if (ep == NULL) {
    720        1.1    itojun 		error = ENOBUFS;
    721        1.1    itojun 		goto fail;
    722        1.1    itojun 	}
    723  1.39.30.3     skrll 	ep->addrpack = kmem_zalloc(l, KM_NOSLEEP);
    724        1.7    itojun 	if (ep->addrpack == NULL) {
    725        1.7    itojun 		error = ENOBUFS;
    726        1.7    itojun 		goto gc;
    727        1.7    itojun 	}
    728  1.39.30.3     skrll 	ep->maskpack = kmem_zalloc(l, KM_NOSLEEP);
    729        1.7    itojun 	if (ep->maskpack == NULL) {
    730        1.7    itojun 		error = ENOBUFS;
    731        1.7    itojun 		goto gc;
    732        1.7    itojun 	}
    733        1.1    itojun 
    734        1.1    itojun 	ep->af = af;
    735        1.1    itojun 	ep->proto = proto;
    736        1.7    itojun 	ep->addrpack->sa_len = l & 0xff;
    737        1.7    itojun 	ep->maskpack->sa_len = l & 0xff;
    738        1.7    itojun 	switch (af) {
    739        1.7    itojun 	case AF_INET:
    740       1.33     pooka 		pack4 = (struct ip_pack4 *)ep->addrpack;
    741        1.7    itojun 		ep->src = (struct sockaddr *)&pack4->mine;
    742        1.7    itojun 		ep->dst = (struct sockaddr *)&pack4->yours;
    743       1.33     pooka 		pack4 = (struct ip_pack4 *)ep->maskpack;
    744        1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack4->mine;
    745        1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack4->yours;
    746        1.7    itojun 		break;
    747        1.7    itojun #ifdef INET6
    748        1.7    itojun 	case AF_INET6:
    749       1.33     pooka 		pack6 = (struct ip_pack6 *)ep->addrpack;
    750        1.7    itojun 		ep->src = (struct sockaddr *)&pack6->mine;
    751        1.7    itojun 		ep->dst = (struct sockaddr *)&pack6->yours;
    752       1.33     pooka 		pack6 = (struct ip_pack6 *)ep->maskpack;
    753        1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack6->mine;
    754        1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack6->yours;
    755        1.7    itojun 		break;
    756        1.7    itojun #endif
    757        1.7    itojun 	}
    758        1.7    itojun 
    759       1.37   tsutsui 	memcpy(ep->src, sp, sp->sa_len);
    760       1.37   tsutsui 	memcpy(ep->srcmask, sm, sp->sa_len);
    761       1.37   tsutsui 	memcpy(ep->dst, dp, dp->sa_len);
    762       1.37   tsutsui 	memcpy(ep->dstmask, dm, dp->sa_len);
    763  1.39.30.4     skrll 	ep->esw = esw;
    764        1.1    itojun 	ep->arg = arg;
    765  1.39.30.6     skrll 	psref_target_init(&ep->psref, encaptab.elem_class);
    766        1.1    itojun 
    767        1.7    itojun 	error = encap_add(ep);
    768        1.7    itojun 	if (error)
    769        1.7    itojun 		goto gc;
    770        1.1    itojun 
    771        1.1    itojun 	error = 0;
    772  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    773        1.1    itojun 	splx(s);
    774  1.39.30.6     skrll #endif
    775        1.1    itojun 	return ep;
    776        1.1    itojun 
    777        1.7    itojun gc:
    778        1.7    itojun 	if (ep->addrpack)
    779  1.39.30.3     skrll 		kmem_free(ep->addrpack, l);
    780        1.7    itojun 	if (ep->maskpack)
    781  1.39.30.3     skrll 		kmem_free(ep->maskpack, l);
    782        1.7    itojun 	if (ep)
    783  1.39.30.3     skrll 		kmem_free(ep, sizeof(*ep));
    784        1.1    itojun fail:
    785  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    786        1.1    itojun 	splx(s);
    787  1.39.30.6     skrll #endif
    788        1.1    itojun 	return NULL;
    789        1.1    itojun }
    790        1.1    itojun 
    791        1.1    itojun const struct encaptab *
    792       1.23     perry encap_attach_func(int af, int proto,
    793       1.26    martin     int (*func)(struct mbuf *, int, int, void *),
    794  1.39.30.4     skrll     const struct encapsw *esw, void *arg)
    795        1.1    itojun {
    796        1.1    itojun 	struct encaptab *ep;
    797        1.1    itojun 	int error;
    798  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    799        1.1    itojun 	int s;
    800        1.1    itojun 
    801        1.1    itojun 	s = splsoftnet();
    802  1.39.30.6     skrll #endif
    803        1.1    itojun 	/* sanity check on args */
    804        1.1    itojun 	if (!func) {
    805        1.1    itojun 		error = EINVAL;
    806        1.1    itojun 		goto fail;
    807        1.1    itojun 	}
    808        1.1    itojun 
    809        1.7    itojun 	error = encap_afcheck(af, NULL, NULL);
    810        1.7    itojun 	if (error)
    811        1.7    itojun 		goto fail;
    812        1.7    itojun 
    813  1.39.30.3     skrll 	ep = kmem_alloc(sizeof(*ep), KM_NOSLEEP);	/*XXX*/
    814        1.1    itojun 	if (ep == NULL) {
    815        1.1    itojun 		error = ENOBUFS;
    816        1.1    itojun 		goto fail;
    817        1.1    itojun 	}
    818       1.35    cegger 	memset(ep, 0, sizeof(*ep));
    819        1.1    itojun 
    820        1.1    itojun 	ep->af = af;
    821        1.1    itojun 	ep->proto = proto;
    822        1.1    itojun 	ep->func = func;
    823  1.39.30.4     skrll 	ep->esw = esw;
    824        1.1    itojun 	ep->arg = arg;
    825  1.39.30.6     skrll 	psref_target_init(&ep->psref, encaptab.elem_class);
    826        1.1    itojun 
    827        1.7    itojun 	error = encap_add(ep);
    828        1.7    itojun 	if (error)
    829        1.7    itojun 		goto fail;
    830        1.1    itojun 
    831        1.1    itojun 	error = 0;
    832  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    833        1.1    itojun 	splx(s);
    834  1.39.30.6     skrll #endif
    835        1.1    itojun 	return ep;
    836        1.1    itojun 
    837        1.1    itojun fail:
    838  1.39.30.6     skrll #ifndef ENCAP_MPSAFE
    839        1.1    itojun 	splx(s);
    840  1.39.30.6     skrll #endif
    841        1.1    itojun 	return NULL;
    842        1.1    itojun }
    843        1.1    itojun 
    844        1.7    itojun /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
    845        1.7    itojun 
    846        1.7    itojun #ifdef INET6
    847       1.32        ad void *
    848       1.29    dyoung encap6_ctlinput(int cmd, const struct sockaddr *sa, void *d0)
    849        1.7    itojun {
    850        1.7    itojun 	void *d = d0;
    851        1.7    itojun 	struct ip6_hdr *ip6;
    852        1.7    itojun 	struct mbuf *m;
    853        1.7    itojun 	int off;
    854        1.7    itojun 	struct ip6ctlparam *ip6cp = NULL;
    855        1.7    itojun 	int nxt;
    856  1.39.30.6     skrll 	int s;
    857        1.7    itojun 	struct encaptab *ep;
    858  1.39.30.4     skrll 	const struct encapsw *esw;
    859        1.7    itojun 
    860        1.7    itojun 	if (sa->sa_family != AF_INET6 ||
    861        1.7    itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    862       1.32        ad 		return NULL;
    863        1.7    itojun 
    864        1.7    itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    865       1.32        ad 		return NULL;
    866        1.7    itojun 	if (cmd == PRC_HOSTDEAD)
    867        1.7    itojun 		d = NULL;
    868        1.7    itojun 	else if (cmd == PRC_MSGSIZE)
    869        1.7    itojun 		; /* special code is present, see below */
    870        1.7    itojun 	else if (inet6ctlerrmap[cmd] == 0)
    871       1.32        ad 		return NULL;
    872        1.7    itojun 
    873        1.7    itojun 	/* if the parameter is from icmp6, decode it. */
    874        1.7    itojun 	if (d != NULL) {
    875        1.7    itojun 		ip6cp = (struct ip6ctlparam *)d;
    876        1.7    itojun 		m = ip6cp->ip6c_m;
    877        1.7    itojun 		ip6 = ip6cp->ip6c_ip6;
    878        1.7    itojun 		off = ip6cp->ip6c_off;
    879        1.7    itojun 		nxt = ip6cp->ip6c_nxt;
    880       1.15   mycroft 
    881       1.15   mycroft 		if (ip6 && cmd == PRC_MSGSIZE) {
    882       1.15   mycroft 			int valid = 0;
    883       1.15   mycroft 			struct encaptab *match;
    884  1.39.30.6     skrll 			struct psref elem_psref;
    885       1.15   mycroft 
    886       1.15   mycroft 			/*
    887       1.15   mycroft 		 	* Check to see if we have a valid encap configuration.
    888       1.15   mycroft 		 	*/
    889  1.39.30.6     skrll 			match = encap6_lookup(m, off, nxt, OUTBOUND,
    890  1.39.30.6     skrll 			    &elem_psref);
    891       1.15   mycroft 			if (match)
    892       1.15   mycroft 				valid++;
    893  1.39.30.6     skrll 			psref_release(&elem_psref, &match->psref,
    894  1.39.30.6     skrll 			    encaptab.elem_class);
    895       1.15   mycroft 
    896       1.15   mycroft 			/*
    897       1.15   mycroft 		 	* Depending on the value of "valid" and routing table
    898       1.15   mycroft 		 	* size (mtudisc_{hi,lo}wat), we will:
    899       1.15   mycroft 		 	* - recalcurate the new MTU and create the
    900       1.15   mycroft 		 	*   corresponding routing entry, or
    901       1.15   mycroft 		 	* - ignore the MTU change notification.
    902       1.15   mycroft 		 	*/
    903       1.15   mycroft 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    904       1.15   mycroft 		}
    905        1.7    itojun 	} else {
    906        1.7    itojun 		m = NULL;
    907        1.7    itojun 		ip6 = NULL;
    908        1.7    itojun 		nxt = -1;
    909        1.7    itojun 	}
    910        1.7    itojun 
    911        1.7    itojun 	/* inform all listeners */
    912  1.39.30.6     skrll 
    913  1.39.30.6     skrll 	s = pserialize_read_enter();
    914  1.39.30.6     skrll 	PSLIST_READER_FOREACH(ep, &encap_table, struct encaptab, chain) {
    915  1.39.30.6     skrll 		struct psref elem_psref;
    916  1.39.30.6     skrll 
    917  1.39.30.6     skrll 		membar_datadep_consumer();
    918  1.39.30.6     skrll 
    919        1.7    itojun 		if (ep->af != AF_INET6)
    920        1.7    itojun 			continue;
    921        1.7    itojun 		if (ep->proto >= 0 && ep->proto != nxt)
    922        1.7    itojun 			continue;
    923        1.7    itojun 
    924        1.7    itojun 		/* should optimize by looking at address pairs */
    925        1.7    itojun 
    926        1.7    itojun 		/* XXX need to pass ep->arg or ep itself to listeners */
    927  1.39.30.6     skrll 		psref_acquire(&elem_psref, &ep->psref,
    928  1.39.30.6     skrll 		    encaptab.elem_class);
    929  1.39.30.4     skrll 		esw = ep->esw;
    930  1.39.30.4     skrll 		if (esw && esw->encapsw6.pr_ctlinput) {
    931  1.39.30.6     skrll 			pserialize_read_exit(s);
    932  1.39.30.6     skrll 			/* pr_ctlinput is sleepable. e.g. rtcache_free */
    933  1.39.30.4     skrll 			(*esw->encapsw6.pr_ctlinput)(cmd, sa, d, ep->arg);
    934  1.39.30.6     skrll 			s = pserialize_read_enter();
    935  1.39.30.4     skrll 		}
    936  1.39.30.6     skrll 		psref_release(&elem_psref, &ep->psref,
    937  1.39.30.6     skrll 		    encaptab.elem_class);
    938        1.7    itojun 	}
    939  1.39.30.6     skrll 	pserialize_read_exit(s);
    940        1.7    itojun 
    941        1.7    itojun 	rip6_ctlinput(cmd, sa, d0);
    942       1.32        ad 	return NULL;
    943        1.7    itojun }
    944        1.7    itojun #endif
    945        1.7    itojun 
    946        1.1    itojun int
    947       1.23     perry encap_detach(const struct encaptab *cookie)
    948        1.1    itojun {
    949        1.1    itojun 	const struct encaptab *ep = cookie;
    950  1.39.30.6     skrll 	struct encaptab *p;
    951        1.7    itojun 	int error;
    952        1.1    itojun 
    953  1.39.30.6     skrll 	KASSERT(encap_lock_held());
    954  1.39.30.6     skrll 
    955  1.39.30.6     skrll 	PSLIST_WRITER_FOREACH(p, &encap_table, struct encaptab, chain) {
    956  1.39.30.6     skrll 		membar_datadep_consumer();
    957  1.39.30.6     skrll 
    958        1.1    itojun 		if (p == ep) {
    959        1.7    itojun 			error = encap_remove(p);
    960        1.7    itojun 			if (error)
    961        1.7    itojun 				return error;
    962  1.39.30.6     skrll 			else
    963  1.39.30.6     skrll 				break;
    964        1.1    itojun 		}
    965        1.1    itojun 	}
    966  1.39.30.6     skrll 	if (p == NULL)
    967  1.39.30.6     skrll 		return ENOENT;
    968        1.1    itojun 
    969  1.39.30.6     skrll #ifndef USE_RADIX
    970  1.39.30.6     skrll 	/*
    971  1.39.30.6     skrll 	 * pserialize_perform(encaptab.psz) is already done in encap_remove().
    972  1.39.30.6     skrll 	 */
    973  1.39.30.6     skrll 	pserialize_perform(encaptab.psz);
    974  1.39.30.6     skrll #endif
    975  1.39.30.6     skrll 	psref_target_destroy(&p->psref,
    976  1.39.30.6     skrll 	    encaptab.elem_class);
    977  1.39.30.6     skrll 	if (!ep->func) {
    978  1.39.30.6     skrll 		kmem_free(p->addrpack, ep->addrpack->sa_len);
    979  1.39.30.6     skrll 		kmem_free(p->maskpack, ep->maskpack->sa_len);
    980  1.39.30.6     skrll 	}
    981  1.39.30.6     skrll 	kmem_free(p, sizeof(*p));
    982  1.39.30.6     skrll 
    983  1.39.30.6     skrll 	return 0;
    984        1.7    itojun }
    985        1.7    itojun 
    986  1.39.30.6     skrll #ifdef USE_RADIX
    987        1.7    itojun static struct radix_node_head *
    988       1.23     perry encap_rnh(int af)
    989        1.7    itojun {
    990        1.7    itojun 
    991        1.7    itojun 	switch (af) {
    992        1.7    itojun 	case AF_INET:
    993        1.7    itojun 		return encap_head[0];
    994        1.7    itojun #ifdef INET6
    995        1.7    itojun 	case AF_INET6:
    996        1.7    itojun 		return encap_head[1];
    997        1.7    itojun #endif
    998        1.7    itojun 	default:
    999        1.7    itojun 		return NULL;
   1000        1.7    itojun 	}
   1001        1.7    itojun }
   1002        1.7    itojun 
   1003        1.7    itojun static int
   1004       1.23     perry mask_matchlen(const struct sockaddr *sa)
   1005        1.7    itojun {
   1006        1.7    itojun 	const char *p, *ep;
   1007        1.7    itojun 	int l;
   1008        1.7    itojun 
   1009        1.7    itojun 	p = (const char *)sa;
   1010        1.7    itojun 	ep = p + sa->sa_len;
   1011        1.7    itojun 	p += 2;	/* sa_len + sa_family */
   1012        1.7    itojun 
   1013        1.7    itojun 	l = 0;
   1014        1.7    itojun 	while (p < ep) {
   1015        1.7    itojun 		l += (*p ? 8 : 0);	/* estimate */
   1016        1.7    itojun 		p++;
   1017        1.7    itojun 	}
   1018        1.7    itojun 	return l;
   1019        1.1    itojun }
   1020  1.39.30.6     skrll #endif
   1021  1.39.30.6     skrll 
   1022  1.39.30.6     skrll #ifndef USE_RADIX
   1023  1.39.30.6     skrll static int
   1024  1.39.30.6     skrll mask_match(const struct encaptab *ep,
   1025  1.39.30.6     skrll 	   const struct sockaddr *sp,
   1026  1.39.30.6     skrll 	   const struct sockaddr *dp)
   1027  1.39.30.6     skrll {
   1028  1.39.30.6     skrll 	struct sockaddr_storage s;
   1029  1.39.30.6     skrll 	struct sockaddr_storage d;
   1030  1.39.30.6     skrll 	int i;
   1031  1.39.30.6     skrll 	const u_int8_t *p, *q;
   1032  1.39.30.6     skrll 	u_int8_t *r;
   1033  1.39.30.6     skrll 	int matchlen;
   1034  1.39.30.6     skrll 
   1035  1.39.30.6     skrll 	KASSERTMSG(ep->func == NULL, "wrong encaptab passed to mask_match");
   1036  1.39.30.6     skrll 
   1037  1.39.30.6     skrll 	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
   1038  1.39.30.6     skrll 		return 0;
   1039  1.39.30.6     skrll 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
   1040  1.39.30.6     skrll 		return 0;
   1041  1.39.30.6     skrll 	if (sp->sa_len != ep->src->sa_len || dp->sa_len != ep->dst->sa_len)
   1042  1.39.30.6     skrll 		return 0;
   1043  1.39.30.6     skrll 
   1044  1.39.30.6     skrll 	matchlen = 0;
   1045  1.39.30.6     skrll 
   1046  1.39.30.6     skrll 	p = (const u_int8_t *)sp;
   1047  1.39.30.6     skrll 	q = (const u_int8_t *)ep->srcmask;
   1048  1.39.30.6     skrll 	r = (u_int8_t *)&s;
   1049  1.39.30.6     skrll 	for (i = 0 ; i < sp->sa_len; i++) {
   1050  1.39.30.6     skrll 		r[i] = p[i] & q[i];
   1051  1.39.30.6     skrll 		/* XXX estimate */
   1052  1.39.30.6     skrll 		matchlen += (q[i] ? 8 : 0);
   1053  1.39.30.6     skrll 	}
   1054  1.39.30.6     skrll 
   1055  1.39.30.6     skrll 	p = (const u_int8_t *)dp;
   1056  1.39.30.6     skrll 	q = (const u_int8_t *)ep->dstmask;
   1057  1.39.30.6     skrll 	r = (u_int8_t *)&d;
   1058  1.39.30.6     skrll 	for (i = 0 ; i < dp->sa_len; i++) {
   1059  1.39.30.6     skrll 		r[i] = p[i] & q[i];
   1060  1.39.30.6     skrll 		/* XXX rough estimate */
   1061  1.39.30.6     skrll 		matchlen += (q[i] ? 8 : 0);
   1062  1.39.30.6     skrll 	}
   1063  1.39.30.6     skrll 
   1064  1.39.30.6     skrll 	/* need to overwrite len/family portion as we don't compare them */
   1065  1.39.30.6     skrll 	s.ss_len = sp->sa_len;
   1066  1.39.30.6     skrll 	s.ss_family = sp->sa_family;
   1067  1.39.30.6     skrll 	d.ss_len = dp->sa_len;
   1068  1.39.30.6     skrll 	d.ss_family = dp->sa_family;
   1069  1.39.30.6     skrll 
   1070  1.39.30.6     skrll 	if (memcmp(&s, ep->src, ep->src->sa_len) == 0 &&
   1071  1.39.30.6     skrll 	    memcmp(&d, ep->dst, ep->dst->sa_len) == 0) {
   1072  1.39.30.6     skrll 		return matchlen;
   1073  1.39.30.6     skrll 	} else
   1074  1.39.30.6     skrll 		return 0;
   1075  1.39.30.6     skrll }
   1076  1.39.30.6     skrll #endif
   1077        1.1    itojun 
   1078        1.1    itojun static void
   1079       1.23     perry encap_fillarg(struct mbuf *m, const struct encaptab *ep)
   1080        1.1    itojun {
   1081       1.12    itojun 	struct m_tag *mtag;
   1082        1.1    itojun 
   1083       1.12    itojun 	mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
   1084       1.12    itojun 	if (mtag) {
   1085       1.12    itojun 		*(void **)(mtag + 1) = ep->arg;
   1086       1.12    itojun 		m_tag_prepend(m, mtag);
   1087        1.1    itojun 	}
   1088        1.1    itojun }
   1089        1.1    itojun 
   1090        1.1    itojun void *
   1091       1.23     perry encap_getarg(struct mbuf *m)
   1092        1.1    itojun {
   1093        1.1    itojun 	void *p;
   1094       1.12    itojun 	struct m_tag *mtag;
   1095        1.1    itojun 
   1096        1.1    itojun 	p = NULL;
   1097       1.12    itojun 	mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
   1098       1.12    itojun 	if (mtag != NULL) {
   1099       1.13    itojun 		p = *(void **)(mtag + 1);
   1100       1.12    itojun 		m_tag_delete(m, mtag);
   1101        1.1    itojun 	}
   1102        1.1    itojun 	return p;
   1103        1.1    itojun }
   1104  1.39.30.6     skrll 
   1105  1.39.30.6     skrll int
   1106  1.39.30.6     skrll encap_lock_enter(void)
   1107  1.39.30.6     skrll {
   1108  1.39.30.6     skrll 	int error;
   1109  1.39.30.6     skrll 
   1110  1.39.30.6     skrll 	mutex_enter(&encap_whole.lock);
   1111  1.39.30.6     skrll 	while (encap_whole.busy != NULL) {
   1112  1.39.30.6     skrll 		error = cv_wait_sig(&encap_whole.cv, &encap_whole.lock);
   1113  1.39.30.6     skrll 		if (error) {
   1114  1.39.30.6     skrll 			mutex_exit(&encap_whole.lock);
   1115  1.39.30.6     skrll 			return error;
   1116  1.39.30.6     skrll 		}
   1117  1.39.30.6     skrll 	}
   1118  1.39.30.6     skrll 	KASSERT(encap_whole.busy == NULL);
   1119  1.39.30.6     skrll 	encap_whole.busy = curlwp;
   1120  1.39.30.6     skrll 	mutex_exit(&encap_whole.lock);
   1121  1.39.30.6     skrll 
   1122  1.39.30.6     skrll 	return 0;
   1123  1.39.30.6     skrll }
   1124  1.39.30.6     skrll 
   1125  1.39.30.6     skrll void
   1126  1.39.30.6     skrll encap_lock_exit(void)
   1127  1.39.30.6     skrll {
   1128  1.39.30.6     skrll 
   1129  1.39.30.6     skrll 	mutex_enter(&encap_whole.lock);
   1130  1.39.30.6     skrll 	KASSERT(encap_whole.busy == curlwp);
   1131  1.39.30.6     skrll 	encap_whole.busy = NULL;
   1132  1.39.30.6     skrll 	cv_broadcast(&encap_whole.cv);
   1133  1.39.30.6     skrll 	mutex_exit(&encap_whole.lock);
   1134  1.39.30.6     skrll }
   1135  1.39.30.6     skrll 
   1136  1.39.30.6     skrll bool
   1137  1.39.30.6     skrll encap_lock_held(void)
   1138  1.39.30.6     skrll {
   1139  1.39.30.6     skrll 
   1140  1.39.30.6     skrll 	return (encap_whole.busy == curlwp);
   1141  1.39.30.6     skrll }
   1142