Home | History | Annotate | Line # | Download | only in netinet
ip_encap.c revision 1.6
      1  1.6    lukem /*	$NetBSD: ip_encap.c,v 1.6 2001/11/13 00:32:36 lukem Exp $	*/
      2  1.4   itojun /*	$KAME: ip_encap.c,v 1.39 2000/10/01 12:37:18 itojun Exp $	*/
      3  1.1   itojun 
      4  1.1   itojun /*
      5  1.1   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  1.1   itojun  * All rights reserved.
      7  1.1   itojun  *
      8  1.1   itojun  * Redistribution and use in source and binary forms, with or without
      9  1.1   itojun  * modification, are permitted provided that the following conditions
     10  1.1   itojun  * are met:
     11  1.1   itojun  * 1. Redistributions of source code must retain the above copyright
     12  1.1   itojun  *    notice, this list of conditions and the following disclaimer.
     13  1.1   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1   itojun  *    notice, this list of conditions and the following disclaimer in the
     15  1.1   itojun  *    documentation and/or other materials provided with the distribution.
     16  1.1   itojun  * 3. Neither the name of the project nor the names of its contributors
     17  1.1   itojun  *    may be used to endorse or promote products derived from this software
     18  1.1   itojun  *    without specific prior written permission.
     19  1.1   itojun  *
     20  1.1   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  1.1   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  1.1   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1   itojun  * SUCH DAMAGE.
     31  1.1   itojun  */
     32  1.1   itojun /*
     33  1.1   itojun  * My grandfather said that there's a devil inside tunnelling technology...
     34  1.1   itojun  *
     35  1.1   itojun  * We have surprisingly many protocols that want packets with IP protocol
     36  1.1   itojun  * #4 or #41.  Here's a list of protocols that want protocol #41:
     37  1.1   itojun  *	RFC1933 configured tunnel
     38  1.1   itojun  *	RFC1933 automatic tunnel
     39  1.1   itojun  *	RFC2401 IPsec tunnel
     40  1.1   itojun  *	RFC2473 IPv6 generic packet tunnelling
     41  1.1   itojun  *	RFC2529 6over4 tunnel
     42  1.1   itojun  *	mobile-ip6 (uses RFC2473)
     43  1.1   itojun  *	6to4 tunnel
     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.6    lukem #include <sys/cdefs.h>
     60  1.6    lukem __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.6 2001/11/13 00:32:36 lukem Exp $");
     61  1.1   itojun 
     62  1.4   itojun #include "opt_mrouting.h"
     63  1.4   itojun #include "opt_inet.h"
     64  1.1   itojun 
     65  1.1   itojun #include <sys/param.h>
     66  1.1   itojun #include <sys/systm.h>
     67  1.1   itojun #include <sys/socket.h>
     68  1.1   itojun #include <sys/sockio.h>
     69  1.1   itojun #include <sys/mbuf.h>
     70  1.1   itojun #include <sys/errno.h>
     71  1.1   itojun #include <sys/protosw.h>
     72  1.4   itojun #include <sys/queue.h>
     73  1.1   itojun 
     74  1.1   itojun #include <net/if.h>
     75  1.1   itojun #include <net/route.h>
     76  1.1   itojun 
     77  1.1   itojun #include <netinet/in.h>
     78  1.1   itojun #include <netinet/in_systm.h>
     79  1.1   itojun #include <netinet/ip.h>
     80  1.1   itojun #include <netinet/ip_var.h>
     81  1.1   itojun #include <netinet/ip_encap.h>
     82  1.1   itojun #ifdef MROUTING
     83  1.1   itojun #include <netinet/ip_mroute.h>
     84  1.1   itojun #endif /* MROUTING */
     85  1.1   itojun 
     86  1.1   itojun #ifdef INET6
     87  1.1   itojun #include <netinet/ip6.h>
     88  1.1   itojun #include <netinet6/ip6_var.h>
     89  1.1   itojun #include <netinet6/ip6protosw.h>
     90  1.1   itojun #endif
     91  1.1   itojun 
     92  1.1   itojun #include <machine/stdarg.h>
     93  1.1   itojun 
     94  1.4   itojun #include "ipip.h"
     95  1.4   itojun #if NIPIP > 0
     96  1.4   itojun # include <netinet/ip_ipip.h>
     97  1.4   itojun #else
     98  1.4   itojun # ifdef MROUTING
     99  1.4   itojun #  include <netinet/ip_mroute.h>
    100  1.1   itojun # endif
    101  1.1   itojun #endif
    102  1.1   itojun 
    103  1.1   itojun #include <net/net_osdep.h>
    104  1.1   itojun 
    105  1.1   itojun static void encap_add __P((struct encaptab *));
    106  1.1   itojun static int mask_match __P((const struct encaptab *, const struct sockaddr *,
    107  1.1   itojun 		const struct sockaddr *));
    108  1.1   itojun static void encap_fillarg __P((struct mbuf *, const struct encaptab *));
    109  1.1   itojun 
    110  1.1   itojun /* rely upon BSS initialization */
    111  1.2  thorpej LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
    112  1.1   itojun 
    113  1.1   itojun void
    114  1.1   itojun encap_init()
    115  1.1   itojun {
    116  1.1   itojun #if 0
    117  1.1   itojun 	/*
    118  1.1   itojun 	 * we cannot use LIST_INIT() here, since drivers may want to call
    119  1.4   itojun 	 * encap_attach(), on driver attach.  encap_init() will be called
    120  1.1   itojun 	 * on AF_INET{,6} initialization, which happens after driver
    121  1.1   itojun 	 * initialization - using LIST_INIT() here can nuke encap_attach()
    122  1.1   itojun 	 * from drivers.
    123  1.1   itojun 	 */
    124  1.1   itojun 	LIST_INIT(&encaptab);
    125  1.1   itojun #endif
    126  1.1   itojun }
    127  1.1   itojun 
    128  1.4   itojun #ifdef INET
    129  1.1   itojun void
    130  1.1   itojun #if __STDC__
    131  1.1   itojun encap4_input(struct mbuf *m, ...)
    132  1.1   itojun #else
    133  1.1   itojun encap4_input(m, va_alist)
    134  1.1   itojun 	struct mbuf *m;
    135  1.1   itojun 	va_dcl
    136  1.1   itojun #endif
    137  1.1   itojun {
    138  1.1   itojun 	int off, proto;
    139  1.1   itojun 	struct ip *ip;
    140  1.1   itojun 	struct sockaddr_in s, d;
    141  1.4   itojun 	const struct protosw *psw;
    142  1.1   itojun 	struct encaptab *ep, *match;
    143  1.1   itojun 	va_list ap;
    144  1.1   itojun 	int prio, matchprio;
    145  1.1   itojun 
    146  1.1   itojun 	va_start(ap, m);
    147  1.1   itojun 	off = va_arg(ap, int);
    148  1.1   itojun 	proto = va_arg(ap, int);
    149  1.1   itojun 	va_end(ap);
    150  1.1   itojun 
    151  1.1   itojun 	ip = mtod(m, struct ip *);
    152  1.1   itojun 
    153  1.1   itojun 	bzero(&s, sizeof(s));
    154  1.1   itojun 	s.sin_family = AF_INET;
    155  1.1   itojun 	s.sin_len = sizeof(struct sockaddr_in);
    156  1.1   itojun 	s.sin_addr = ip->ip_src;
    157  1.1   itojun 	bzero(&d, sizeof(d));
    158  1.1   itojun 	d.sin_family = AF_INET;
    159  1.1   itojun 	d.sin_len = sizeof(struct sockaddr_in);
    160  1.1   itojun 	d.sin_addr = ip->ip_dst;
    161  1.1   itojun 
    162  1.1   itojun 	match = NULL;
    163  1.1   itojun 	matchprio = 0;
    164  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    165  1.1   itojun 		if (ep->af != AF_INET)
    166  1.1   itojun 			continue;
    167  1.1   itojun 		if (ep->proto >= 0 && ep->proto != proto)
    168  1.1   itojun 			continue;
    169  1.1   itojun 		if (ep->func)
    170  1.1   itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    171  1.1   itojun 		else {
    172  1.1   itojun 			/*
    173  1.1   itojun 			 * it's inbound traffic, we need to match in reverse
    174  1.1   itojun 			 * order
    175  1.1   itojun 			 */
    176  1.1   itojun 			prio = mask_match(ep, (struct sockaddr *)&d,
    177  1.1   itojun 			    (struct sockaddr *)&s);
    178  1.1   itojun 		}
    179  1.1   itojun 
    180  1.1   itojun 		/*
    181  1.1   itojun 		 * We prioritize the matches by using bit length of the
    182  1.1   itojun 		 * matches.  mask_match() and user-supplied matching function
    183  1.1   itojun 		 * should return the bit length of the matches (for example,
    184  1.1   itojun 		 * if both src/dst are matched for IPv4, 64 should be returned).
    185  1.1   itojun 		 * 0 or negative return value means "it did not match".
    186  1.1   itojun 		 *
    187  1.1   itojun 		 * The question is, since we have two "mask" portion, we
    188  1.1   itojun 		 * cannot really define total order between entries.
    189  1.1   itojun 		 * For example, which of these should be preferred?
    190  1.1   itojun 		 * mask_match() returns 48 (32 + 16) for both of them.
    191  1.1   itojun 		 *	src=3ffe::/16, dst=3ffe:501::/32
    192  1.1   itojun 		 *	src=3ffe:501::/32, dst=3ffe::/16
    193  1.1   itojun 		 *
    194  1.1   itojun 		 * We need to loop through all the possible candidates
    195  1.1   itojun 		 * to get the best match - the search takes O(n) for
    196  1.1   itojun 		 * n attachments (i.e. interfaces).
    197  1.1   itojun 		 */
    198  1.1   itojun 		if (prio <= 0)
    199  1.1   itojun 			continue;
    200  1.1   itojun 		if (prio > matchprio) {
    201  1.1   itojun 			matchprio = prio;
    202  1.1   itojun 			match = ep;
    203  1.1   itojun 		}
    204  1.1   itojun 	}
    205  1.1   itojun 
    206  1.1   itojun 	if (match) {
    207  1.1   itojun 		/* found a match, "match" has the best one */
    208  1.4   itojun 		psw = match->psw;
    209  1.4   itojun 		if (psw && psw->pr_input) {
    210  1.1   itojun 			encap_fillarg(m, match);
    211  1.4   itojun 			(*psw->pr_input)(m, off, proto);
    212  1.1   itojun 		} else
    213  1.1   itojun 			m_freem(m);
    214  1.1   itojun 		return;
    215  1.1   itojun 	}
    216  1.1   itojun 
    217  1.1   itojun 	/* last resort: inject to raw socket */
    218  1.1   itojun 	rip_input(m, off, proto);
    219  1.1   itojun }
    220  1.1   itojun #endif
    221  1.1   itojun 
    222  1.1   itojun #ifdef INET6
    223  1.1   itojun int
    224  1.1   itojun encap6_input(mp, offp, proto)
    225  1.1   itojun 	struct mbuf **mp;
    226  1.1   itojun 	int *offp;
    227  1.1   itojun 	int proto;
    228  1.1   itojun {
    229  1.1   itojun 	struct mbuf *m = *mp;
    230  1.1   itojun 	struct ip6_hdr *ip6;
    231  1.1   itojun 	struct sockaddr_in6 s, d;
    232  1.4   itojun 	const struct ip6protosw *psw;
    233  1.1   itojun 	struct encaptab *ep, *match;
    234  1.1   itojun 	int prio, matchprio;
    235  1.1   itojun 
    236  1.1   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    237  1.1   itojun 
    238  1.1   itojun 	bzero(&s, sizeof(s));
    239  1.1   itojun 	s.sin6_family = AF_INET6;
    240  1.1   itojun 	s.sin6_len = sizeof(struct sockaddr_in6);
    241  1.1   itojun 	s.sin6_addr = ip6->ip6_src;
    242  1.1   itojun 	bzero(&d, sizeof(d));
    243  1.1   itojun 	d.sin6_family = AF_INET6;
    244  1.1   itojun 	d.sin6_len = sizeof(struct sockaddr_in6);
    245  1.1   itojun 	d.sin6_addr = ip6->ip6_dst;
    246  1.1   itojun 
    247  1.1   itojun 	match = NULL;
    248  1.1   itojun 	matchprio = 0;
    249  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    250  1.1   itojun 		if (ep->af != AF_INET6)
    251  1.1   itojun 			continue;
    252  1.1   itojun 		if (ep->proto >= 0 && ep->proto != proto)
    253  1.1   itojun 			continue;
    254  1.1   itojun 		if (ep->func)
    255  1.1   itojun 			prio = (*ep->func)(m, *offp, proto, ep->arg);
    256  1.1   itojun 		else {
    257  1.1   itojun 			/*
    258  1.1   itojun 			 * it's inbound traffic, we need to match in reverse
    259  1.1   itojun 			 * order
    260  1.1   itojun 			 */
    261  1.1   itojun 			prio = mask_match(ep, (struct sockaddr *)&d,
    262  1.1   itojun 			    (struct sockaddr *)&s);
    263  1.1   itojun 		}
    264  1.1   itojun 
    265  1.1   itojun 		/* see encap4_input() for issues here */
    266  1.1   itojun 		if (prio <= 0)
    267  1.1   itojun 			continue;
    268  1.1   itojun 		if (prio > matchprio) {
    269  1.1   itojun 			matchprio = prio;
    270  1.1   itojun 			match = ep;
    271  1.1   itojun 		}
    272  1.1   itojun 	}
    273  1.1   itojun 
    274  1.1   itojun 	if (match) {
    275  1.1   itojun 		/* found a match */
    276  1.4   itojun 		psw = (const struct ip6protosw *)match->psw;
    277  1.1   itojun 		if (psw && psw->pr_input) {
    278  1.1   itojun 			encap_fillarg(m, match);
    279  1.1   itojun 			return (*psw->pr_input)(mp, offp, proto);
    280  1.1   itojun 		} else {
    281  1.1   itojun 			m_freem(m);
    282  1.1   itojun 			return IPPROTO_DONE;
    283  1.1   itojun 		}
    284  1.1   itojun 	}
    285  1.1   itojun 
    286  1.1   itojun 	/* last resort: inject to raw socket */
    287  1.1   itojun 	return rip6_input(mp, offp, proto);
    288  1.1   itojun }
    289  1.1   itojun #endif
    290  1.1   itojun 
    291  1.1   itojun static void
    292  1.1   itojun encap_add(ep)
    293  1.1   itojun 	struct encaptab *ep;
    294  1.1   itojun {
    295  1.1   itojun 
    296  1.1   itojun 	LIST_INSERT_HEAD(&encaptab, ep, chain);
    297  1.1   itojun }
    298  1.1   itojun 
    299  1.1   itojun /*
    300  1.1   itojun  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
    301  1.1   itojun  * length of mask (sm and dm) is assumed to be same as sp/dp.
    302  1.1   itojun  * Return value will be necessary as input (cookie) for encap_detach().
    303  1.1   itojun  */
    304  1.1   itojun const struct encaptab *
    305  1.1   itojun encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
    306  1.1   itojun 	int af;
    307  1.1   itojun 	int proto;
    308  1.1   itojun 	const struct sockaddr *sp, *sm;
    309  1.1   itojun 	const struct sockaddr *dp, *dm;
    310  1.1   itojun 	const struct protosw *psw;
    311  1.1   itojun 	void *arg;
    312  1.1   itojun {
    313  1.1   itojun 	struct encaptab *ep;
    314  1.1   itojun 	int error;
    315  1.1   itojun 	int s;
    316  1.1   itojun 
    317  1.1   itojun 	s = splsoftnet();
    318  1.1   itojun 	/* sanity check on args */
    319  1.1   itojun 	if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
    320  1.1   itojun 		error = EINVAL;
    321  1.1   itojun 		goto fail;
    322  1.1   itojun 	}
    323  1.1   itojun 	if (sp->sa_len != dp->sa_len) {
    324  1.1   itojun 		error = EINVAL;
    325  1.1   itojun 		goto fail;
    326  1.1   itojun 	}
    327  1.1   itojun 	if (af != sp->sa_family || af != dp->sa_family) {
    328  1.1   itojun 		error = EINVAL;
    329  1.1   itojun 		goto fail;
    330  1.1   itojun 	}
    331  1.1   itojun 
    332  1.1   itojun 	/* check if anyone have already attached with exactly same config */
    333  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    334  1.1   itojun 		if (ep->af != af)
    335  1.1   itojun 			continue;
    336  1.1   itojun 		if (ep->proto != proto)
    337  1.1   itojun 			continue;
    338  1.1   itojun 		if (ep->src.ss_len != sp->sa_len ||
    339  1.1   itojun 		    bcmp(&ep->src, sp, sp->sa_len) != 0 ||
    340  1.1   itojun 		    bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
    341  1.1   itojun 			continue;
    342  1.1   itojun 		if (ep->dst.ss_len != dp->sa_len ||
    343  1.1   itojun 		    bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
    344  1.1   itojun 		    bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
    345  1.1   itojun 			continue;
    346  1.1   itojun 
    347  1.1   itojun 		error = EEXIST;
    348  1.1   itojun 		goto fail;
    349  1.1   itojun 	}
    350  1.3  thorpej 
    351  1.3  thorpej 	/*
    352  1.3  thorpej 	 * XXX NEED TO CHECK viftable IN THE ip_mroute CODE!!!
    353  1.3  thorpej 	 * XXX Actually, that code needs to be replaced with
    354  1.3  thorpej 	 * XXX new code that uses `gif' tunnels.
    355  1.3  thorpej 	 */
    356  1.1   itojun 
    357  1.1   itojun 	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
    358  1.1   itojun 	if (ep == NULL) {
    359  1.1   itojun 		error = ENOBUFS;
    360  1.1   itojun 		goto fail;
    361  1.1   itojun 	}
    362  1.1   itojun 	bzero(ep, sizeof(*ep));
    363  1.1   itojun 
    364  1.1   itojun 	ep->af = af;
    365  1.1   itojun 	ep->proto = proto;
    366  1.1   itojun 	bcopy(sp, &ep->src, sp->sa_len);
    367  1.1   itojun 	bcopy(sm, &ep->srcmask, sp->sa_len);
    368  1.1   itojun 	bcopy(dp, &ep->dst, dp->sa_len);
    369  1.1   itojun 	bcopy(dm, &ep->dstmask, dp->sa_len);
    370  1.1   itojun 	ep->psw = psw;
    371  1.1   itojun 	ep->arg = arg;
    372  1.1   itojun 
    373  1.1   itojun 	encap_add(ep);
    374  1.1   itojun 
    375  1.1   itojun 	error = 0;
    376  1.1   itojun 	splx(s);
    377  1.1   itojun 	return ep;
    378  1.1   itojun 
    379  1.1   itojun fail:
    380  1.1   itojun 	splx(s);
    381  1.1   itojun 	return NULL;
    382  1.1   itojun }
    383  1.1   itojun 
    384  1.1   itojun const struct encaptab *
    385  1.1   itojun encap_attach_func(af, proto, func, psw, arg)
    386  1.1   itojun 	int af;
    387  1.1   itojun 	int proto;
    388  1.1   itojun 	int (*func) __P((const struct mbuf *, int, int, void *));
    389  1.1   itojun 	const struct protosw *psw;
    390  1.1   itojun 	void *arg;
    391  1.1   itojun {
    392  1.1   itojun 	struct encaptab *ep;
    393  1.1   itojun 	int error;
    394  1.1   itojun 	int s;
    395  1.1   itojun 
    396  1.1   itojun 	s = splsoftnet();
    397  1.1   itojun 	/* sanity check on args */
    398  1.1   itojun 	if (!func) {
    399  1.1   itojun 		error = EINVAL;
    400  1.1   itojun 		goto fail;
    401  1.1   itojun 	}
    402  1.1   itojun 
    403  1.1   itojun 	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
    404  1.1   itojun 	if (ep == NULL) {
    405  1.1   itojun 		error = ENOBUFS;
    406  1.1   itojun 		goto fail;
    407  1.1   itojun 	}
    408  1.1   itojun 	bzero(ep, sizeof(*ep));
    409  1.1   itojun 
    410  1.1   itojun 	ep->af = af;
    411  1.1   itojun 	ep->proto = proto;
    412  1.1   itojun 	ep->func = func;
    413  1.1   itojun 	ep->psw = psw;
    414  1.1   itojun 	ep->arg = arg;
    415  1.1   itojun 
    416  1.1   itojun 	encap_add(ep);
    417  1.1   itojun 
    418  1.1   itojun 	error = 0;
    419  1.1   itojun 	splx(s);
    420  1.1   itojun 	return ep;
    421  1.1   itojun 
    422  1.1   itojun fail:
    423  1.1   itojun 	splx(s);
    424  1.1   itojun 	return NULL;
    425  1.1   itojun }
    426  1.1   itojun 
    427  1.1   itojun int
    428  1.1   itojun encap_detach(cookie)
    429  1.1   itojun 	const struct encaptab *cookie;
    430  1.1   itojun {
    431  1.1   itojun 	const struct encaptab *ep = cookie;
    432  1.1   itojun 	struct encaptab *p;
    433  1.1   itojun 
    434  1.1   itojun 	for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
    435  1.1   itojun 		if (p == ep) {
    436  1.1   itojun 			LIST_REMOVE(p, chain);
    437  1.1   itojun 			free(p, M_NETADDR);	/*XXX*/
    438  1.1   itojun 			return 0;
    439  1.1   itojun 		}
    440  1.1   itojun 	}
    441  1.1   itojun 
    442  1.1   itojun 	return EINVAL;
    443  1.1   itojun }
    444  1.1   itojun 
    445  1.1   itojun static int
    446  1.1   itojun mask_match(ep, sp, dp)
    447  1.1   itojun 	const struct encaptab *ep;
    448  1.1   itojun 	const struct sockaddr *sp;
    449  1.1   itojun 	const struct sockaddr *dp;
    450  1.1   itojun {
    451  1.1   itojun 	struct sockaddr_storage s;
    452  1.1   itojun 	struct sockaddr_storage d;
    453  1.1   itojun 	int i;
    454  1.4   itojun 	const u_int8_t *p, *q;
    455  1.4   itojun 	u_int8_t *r;
    456  1.1   itojun 	int matchlen;
    457  1.1   itojun 
    458  1.1   itojun 	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
    459  1.1   itojun 		return 0;
    460  1.1   itojun 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
    461  1.1   itojun 		return 0;
    462  1.1   itojun 	if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
    463  1.1   itojun 		return 0;
    464  1.1   itojun 
    465  1.1   itojun 	matchlen = 0;
    466  1.1   itojun 
    467  1.4   itojun 	p = (const u_int8_t *)sp;
    468  1.4   itojun 	q = (const u_int8_t *)&ep->srcmask;
    469  1.1   itojun 	r = (u_int8_t *)&s;
    470  1.1   itojun 	for (i = 0 ; i < sp->sa_len; i++) {
    471  1.1   itojun 		r[i] = p[i] & q[i];
    472  1.1   itojun 		/* XXX estimate */
    473  1.1   itojun 		matchlen += (q[i] ? 8 : 0);
    474  1.1   itojun 	}
    475  1.1   itojun 
    476  1.4   itojun 	p = (const u_int8_t *)dp;
    477  1.4   itojun 	q = (const u_int8_t *)&ep->dstmask;
    478  1.1   itojun 	r = (u_int8_t *)&d;
    479  1.1   itojun 	for (i = 0 ; i < dp->sa_len; i++) {
    480  1.1   itojun 		r[i] = p[i] & q[i];
    481  1.1   itojun 		/* XXX rough estimate */
    482  1.1   itojun 		matchlen += (q[i] ? 8 : 0);
    483  1.1   itojun 	}
    484  1.1   itojun 
    485  1.1   itojun 	/* need to overwrite len/family portion as we don't compare them */
    486  1.1   itojun 	s.ss_len = sp->sa_len;
    487  1.1   itojun 	s.ss_family = sp->sa_family;
    488  1.1   itojun 	d.ss_len = dp->sa_len;
    489  1.1   itojun 	d.ss_family = dp->sa_family;
    490  1.1   itojun 
    491  1.1   itojun 	if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
    492  1.1   itojun 	    bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
    493  1.1   itojun 		return matchlen;
    494  1.1   itojun 	} else
    495  1.1   itojun 		return 0;
    496  1.1   itojun }
    497  1.1   itojun 
    498  1.1   itojun static void
    499  1.1   itojun encap_fillarg(m, ep)
    500  1.1   itojun 	struct mbuf *m;
    501  1.1   itojun 	const struct encaptab *ep;
    502  1.1   itojun {
    503  1.1   itojun #if 0
    504  1.1   itojun 	m->m_pkthdr.aux = ep->arg;
    505  1.1   itojun #else
    506  1.1   itojun 	struct mbuf *n;
    507  1.1   itojun 
    508  1.1   itojun 	n = m_aux_add(m, AF_INET, IPPROTO_IPV4);
    509  1.1   itojun 	if (n) {
    510  1.1   itojun 		*mtod(n, void **) = ep->arg;
    511  1.1   itojun 		n->m_len = sizeof(void *);
    512  1.1   itojun 	}
    513  1.1   itojun #endif
    514  1.1   itojun }
    515  1.1   itojun 
    516  1.1   itojun void *
    517  1.1   itojun encap_getarg(m)
    518  1.1   itojun 	struct mbuf *m;
    519  1.1   itojun {
    520  1.1   itojun 	void *p;
    521  1.1   itojun #if 0
    522  1.1   itojun 	p = m->m_pkthdr.aux;
    523  1.1   itojun 	m->m_pkthdr.aux = NULL;
    524  1.1   itojun 	return p;
    525  1.1   itojun #else
    526  1.1   itojun 	struct mbuf *n;
    527  1.1   itojun 
    528  1.1   itojun 	p = NULL;
    529  1.1   itojun 	n = m_aux_find(m, AF_INET, IPPROTO_IPV4);
    530  1.1   itojun 	if (n) {
    531  1.1   itojun 		if (n->m_len == sizeof(void *))
    532  1.1   itojun 			p = *mtod(n, void **);
    533  1.1   itojun 		m_aux_delete(m, n);
    534  1.1   itojun 	}
    535  1.1   itojun 	return p;
    536  1.1   itojun #endif
    537  1.1   itojun }
    538