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