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