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