Home | History | Annotate | Line # | Download | only in netinet
ip_encap.c revision 1.5
      1  1.5   itojun /*	$NetBSD: ip_encap.c,v 1.5 2001/05/08 10:07:15 itojun 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.1   itojun 
     59  1.4   itojun #include "opt_mrouting.h"
     60  1.4   itojun #include "opt_inet.h"
     61  1.1   itojun 
     62  1.1   itojun #include <sys/param.h>
     63  1.1   itojun #include <sys/systm.h>
     64  1.1   itojun #include <sys/socket.h>
     65  1.1   itojun #include <sys/sockio.h>
     66  1.1   itojun #include <sys/mbuf.h>
     67  1.1   itojun #include <sys/errno.h>
     68  1.1   itojun #include <sys/protosw.h>
     69  1.4   itojun #include <sys/queue.h>
     70  1.1   itojun 
     71  1.1   itojun #include <net/if.h>
     72  1.1   itojun #include <net/route.h>
     73  1.1   itojun 
     74  1.1   itojun #include <netinet/in.h>
     75  1.1   itojun #include <netinet/in_systm.h>
     76  1.1   itojun #include <netinet/ip.h>
     77  1.1   itojun #include <netinet/ip_var.h>
     78  1.1   itojun #include <netinet/ip_encap.h>
     79  1.1   itojun #ifdef MROUTING
     80  1.1   itojun #include <netinet/ip_mroute.h>
     81  1.1   itojun #endif /* MROUTING */
     82  1.1   itojun 
     83  1.1   itojun #ifdef INET6
     84  1.1   itojun #include <netinet/ip6.h>
     85  1.1   itojun #include <netinet6/ip6_var.h>
     86  1.1   itojun #include <netinet6/ip6protosw.h>
     87  1.1   itojun #endif
     88  1.1   itojun 
     89  1.1   itojun #include <machine/stdarg.h>
     90  1.1   itojun 
     91  1.4   itojun #include "ipip.h"
     92  1.4   itojun #if NIPIP > 0
     93  1.4   itojun # include <netinet/ip_ipip.h>
     94  1.4   itojun #else
     95  1.4   itojun # ifdef MROUTING
     96  1.4   itojun #  include <netinet/ip_mroute.h>
     97  1.1   itojun # endif
     98  1.1   itojun #endif
     99  1.1   itojun 
    100  1.1   itojun #include <net/net_osdep.h>
    101  1.1   itojun 
    102  1.1   itojun static void encap_add __P((struct encaptab *));
    103  1.1   itojun static int mask_match __P((const struct encaptab *, const struct sockaddr *,
    104  1.1   itojun 		const struct sockaddr *));
    105  1.1   itojun static void encap_fillarg __P((struct mbuf *, const struct encaptab *));
    106  1.1   itojun 
    107  1.1   itojun /* rely upon BSS initialization */
    108  1.2  thorpej LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
    109  1.1   itojun 
    110  1.1   itojun void
    111  1.1   itojun encap_init()
    112  1.1   itojun {
    113  1.1   itojun #if 0
    114  1.1   itojun 	/*
    115  1.1   itojun 	 * we cannot use LIST_INIT() here, since drivers may want to call
    116  1.4   itojun 	 * encap_attach(), on driver attach.  encap_init() will be called
    117  1.1   itojun 	 * on AF_INET{,6} initialization, which happens after driver
    118  1.1   itojun 	 * initialization - using LIST_INIT() here can nuke encap_attach()
    119  1.1   itojun 	 * from drivers.
    120  1.1   itojun 	 */
    121  1.1   itojun 	LIST_INIT(&encaptab);
    122  1.1   itojun #endif
    123  1.1   itojun }
    124  1.1   itojun 
    125  1.4   itojun #ifdef INET
    126  1.1   itojun void
    127  1.1   itojun #if __STDC__
    128  1.1   itojun encap4_input(struct mbuf *m, ...)
    129  1.1   itojun #else
    130  1.1   itojun encap4_input(m, va_alist)
    131  1.1   itojun 	struct mbuf *m;
    132  1.1   itojun 	va_dcl
    133  1.1   itojun #endif
    134  1.1   itojun {
    135  1.1   itojun 	int off, proto;
    136  1.1   itojun 	struct ip *ip;
    137  1.1   itojun 	struct sockaddr_in s, d;
    138  1.4   itojun 	const struct protosw *psw;
    139  1.1   itojun 	struct encaptab *ep, *match;
    140  1.1   itojun 	va_list ap;
    141  1.1   itojun 	int prio, matchprio;
    142  1.1   itojun 
    143  1.1   itojun 	va_start(ap, m);
    144  1.1   itojun 	off = va_arg(ap, int);
    145  1.1   itojun 	proto = va_arg(ap, int);
    146  1.1   itojun 	va_end(ap);
    147  1.1   itojun 
    148  1.1   itojun 	ip = mtod(m, struct ip *);
    149  1.1   itojun 
    150  1.1   itojun 	bzero(&s, sizeof(s));
    151  1.1   itojun 	s.sin_family = AF_INET;
    152  1.1   itojun 	s.sin_len = sizeof(struct sockaddr_in);
    153  1.1   itojun 	s.sin_addr = ip->ip_src;
    154  1.1   itojun 	bzero(&d, sizeof(d));
    155  1.1   itojun 	d.sin_family = AF_INET;
    156  1.1   itojun 	d.sin_len = sizeof(struct sockaddr_in);
    157  1.1   itojun 	d.sin_addr = ip->ip_dst;
    158  1.1   itojun 
    159  1.1   itojun 	match = NULL;
    160  1.1   itojun 	matchprio = 0;
    161  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    162  1.1   itojun 		if (ep->af != AF_INET)
    163  1.1   itojun 			continue;
    164  1.1   itojun 		if (ep->proto >= 0 && ep->proto != proto)
    165  1.1   itojun 			continue;
    166  1.1   itojun 		if (ep->func)
    167  1.1   itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    168  1.1   itojun 		else {
    169  1.1   itojun 			/*
    170  1.1   itojun 			 * it's inbound traffic, we need to match in reverse
    171  1.1   itojun 			 * order
    172  1.1   itojun 			 */
    173  1.1   itojun 			prio = mask_match(ep, (struct sockaddr *)&d,
    174  1.1   itojun 			    (struct sockaddr *)&s);
    175  1.1   itojun 		}
    176  1.1   itojun 
    177  1.1   itojun 		/*
    178  1.1   itojun 		 * We prioritize the matches by using bit length of the
    179  1.1   itojun 		 * matches.  mask_match() and user-supplied matching function
    180  1.1   itojun 		 * should return the bit length of the matches (for example,
    181  1.1   itojun 		 * if both src/dst are matched for IPv4, 64 should be returned).
    182  1.1   itojun 		 * 0 or negative return value means "it did not match".
    183  1.1   itojun 		 *
    184  1.1   itojun 		 * The question is, since we have two "mask" portion, we
    185  1.1   itojun 		 * cannot really define total order between entries.
    186  1.1   itojun 		 * For example, which of these should be preferred?
    187  1.1   itojun 		 * mask_match() returns 48 (32 + 16) for both of them.
    188  1.1   itojun 		 *	src=3ffe::/16, dst=3ffe:501::/32
    189  1.1   itojun 		 *	src=3ffe:501::/32, dst=3ffe::/16
    190  1.1   itojun 		 *
    191  1.1   itojun 		 * We need to loop through all the possible candidates
    192  1.1   itojun 		 * to get the best match - the search takes O(n) for
    193  1.1   itojun 		 * n attachments (i.e. interfaces).
    194  1.1   itojun 		 */
    195  1.1   itojun 		if (prio <= 0)
    196  1.1   itojun 			continue;
    197  1.1   itojun 		if (prio > matchprio) {
    198  1.1   itojun 			matchprio = prio;
    199  1.1   itojun 			match = ep;
    200  1.1   itojun 		}
    201  1.1   itojun 	}
    202  1.1   itojun 
    203  1.1   itojun 	if (match) {
    204  1.1   itojun 		/* found a match, "match" has the best one */
    205  1.4   itojun 		psw = match->psw;
    206  1.4   itojun 		if (psw && psw->pr_input) {
    207  1.1   itojun 			encap_fillarg(m, match);
    208  1.4   itojun 			(*psw->pr_input)(m, off, proto);
    209  1.1   itojun 		} else
    210  1.1   itojun 			m_freem(m);
    211  1.1   itojun 		return;
    212  1.1   itojun 	}
    213  1.1   itojun 
    214  1.1   itojun 	/* last resort: inject to raw socket */
    215  1.1   itojun 	rip_input(m, off, proto);
    216  1.1   itojun }
    217  1.1   itojun #endif
    218  1.1   itojun 
    219  1.1   itojun #ifdef INET6
    220  1.1   itojun int
    221  1.1   itojun encap6_input(mp, offp, proto)
    222  1.1   itojun 	struct mbuf **mp;
    223  1.1   itojun 	int *offp;
    224  1.1   itojun 	int proto;
    225  1.1   itojun {
    226  1.1   itojun 	struct mbuf *m = *mp;
    227  1.1   itojun 	struct ip6_hdr *ip6;
    228  1.1   itojun 	struct sockaddr_in6 s, d;
    229  1.4   itojun 	const struct ip6protosw *psw;
    230  1.1   itojun 	struct encaptab *ep, *match;
    231  1.1   itojun 	int prio, matchprio;
    232  1.1   itojun 
    233  1.1   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    234  1.1   itojun 
    235  1.1   itojun 	bzero(&s, sizeof(s));
    236  1.1   itojun 	s.sin6_family = AF_INET6;
    237  1.1   itojun 	s.sin6_len = sizeof(struct sockaddr_in6);
    238  1.1   itojun 	s.sin6_addr = ip6->ip6_src;
    239  1.1   itojun 	bzero(&d, sizeof(d));
    240  1.1   itojun 	d.sin6_family = AF_INET6;
    241  1.1   itojun 	d.sin6_len = sizeof(struct sockaddr_in6);
    242  1.1   itojun 	d.sin6_addr = ip6->ip6_dst;
    243  1.1   itojun 
    244  1.1   itojun 	match = NULL;
    245  1.1   itojun 	matchprio = 0;
    246  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    247  1.1   itojun 		if (ep->af != AF_INET6)
    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, *offp, proto, ep->arg);
    253  1.1   itojun 		else {
    254  1.1   itojun 			/*
    255  1.1   itojun 			 * it's inbound traffic, we need to match in reverse
    256  1.1   itojun 			 * order
    257  1.1   itojun 			 */
    258  1.1   itojun 			prio = mask_match(ep, (struct sockaddr *)&d,
    259  1.1   itojun 			    (struct sockaddr *)&s);
    260  1.1   itojun 		}
    261  1.1   itojun 
    262  1.1   itojun 		/* see encap4_input() for issues here */
    263  1.1   itojun 		if (prio <= 0)
    264  1.1   itojun 			continue;
    265  1.1   itojun 		if (prio > matchprio) {
    266  1.1   itojun 			matchprio = prio;
    267  1.1   itojun 			match = ep;
    268  1.1   itojun 		}
    269  1.1   itojun 	}
    270  1.1   itojun 
    271  1.1   itojun 	if (match) {
    272  1.1   itojun 		/* found a match */
    273  1.4   itojun 		psw = (const struct ip6protosw *)match->psw;
    274  1.1   itojun 		if (psw && psw->pr_input) {
    275  1.1   itojun 			encap_fillarg(m, match);
    276  1.1   itojun 			return (*psw->pr_input)(mp, offp, proto);
    277  1.1   itojun 		} else {
    278  1.1   itojun 			m_freem(m);
    279  1.1   itojun 			return IPPROTO_DONE;
    280  1.1   itojun 		}
    281  1.1   itojun 	}
    282  1.1   itojun 
    283  1.1   itojun 	/* last resort: inject to raw socket */
    284  1.1   itojun 	return rip6_input(mp, offp, proto);
    285  1.1   itojun }
    286  1.1   itojun #endif
    287  1.1   itojun 
    288  1.1   itojun static void
    289  1.1   itojun encap_add(ep)
    290  1.1   itojun 	struct encaptab *ep;
    291  1.1   itojun {
    292  1.1   itojun 
    293  1.1   itojun 	LIST_INSERT_HEAD(&encaptab, ep, chain);
    294  1.1   itojun }
    295  1.1   itojun 
    296  1.1   itojun /*
    297  1.1   itojun  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
    298  1.1   itojun  * length of mask (sm and dm) is assumed to be same as sp/dp.
    299  1.1   itojun  * Return value will be necessary as input (cookie) for encap_detach().
    300  1.1   itojun  */
    301  1.1   itojun const struct encaptab *
    302  1.1   itojun encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
    303  1.1   itojun 	int af;
    304  1.1   itojun 	int proto;
    305  1.1   itojun 	const struct sockaddr *sp, *sm;
    306  1.1   itojun 	const struct sockaddr *dp, *dm;
    307  1.1   itojun 	const struct protosw *psw;
    308  1.1   itojun 	void *arg;
    309  1.1   itojun {
    310  1.1   itojun 	struct encaptab *ep;
    311  1.1   itojun 	int error;
    312  1.1   itojun 	int s;
    313  1.1   itojun 
    314  1.1   itojun 	s = splsoftnet();
    315  1.1   itojun 	/* sanity check on args */
    316  1.1   itojun 	if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
    317  1.1   itojun 		error = EINVAL;
    318  1.1   itojun 		goto fail;
    319  1.1   itojun 	}
    320  1.1   itojun 	if (sp->sa_len != dp->sa_len) {
    321  1.1   itojun 		error = EINVAL;
    322  1.1   itojun 		goto fail;
    323  1.1   itojun 	}
    324  1.1   itojun 	if (af != sp->sa_family || af != dp->sa_family) {
    325  1.1   itojun 		error = EINVAL;
    326  1.1   itojun 		goto fail;
    327  1.1   itojun 	}
    328  1.1   itojun 
    329  1.1   itojun 	/* check if anyone have already attached with exactly same config */
    330  1.1   itojun 	for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
    331  1.1   itojun 		if (ep->af != af)
    332  1.1   itojun 			continue;
    333  1.1   itojun 		if (ep->proto != proto)
    334  1.1   itojun 			continue;
    335  1.1   itojun 		if (ep->src.ss_len != sp->sa_len ||
    336  1.1   itojun 		    bcmp(&ep->src, sp, sp->sa_len) != 0 ||
    337  1.1   itojun 		    bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
    338  1.1   itojun 			continue;
    339  1.1   itojun 		if (ep->dst.ss_len != dp->sa_len ||
    340  1.1   itojun 		    bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
    341  1.1   itojun 		    bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
    342  1.1   itojun 			continue;
    343  1.1   itojun 
    344  1.1   itojun 		error = EEXIST;
    345  1.1   itojun 		goto fail;
    346  1.1   itojun 	}
    347  1.3  thorpej 
    348  1.3  thorpej 	/*
    349  1.3  thorpej 	 * XXX NEED TO CHECK viftable IN THE ip_mroute CODE!!!
    350  1.3  thorpej 	 * XXX Actually, that code needs to be replaced with
    351  1.3  thorpej 	 * XXX new code that uses `gif' tunnels.
    352  1.3  thorpej 	 */
    353  1.1   itojun 
    354  1.1   itojun 	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
    355  1.1   itojun 	if (ep == NULL) {
    356  1.1   itojun 		error = ENOBUFS;
    357  1.1   itojun 		goto fail;
    358  1.1   itojun 	}
    359  1.1   itojun 	bzero(ep, sizeof(*ep));
    360  1.1   itojun 
    361  1.1   itojun 	ep->af = af;
    362  1.1   itojun 	ep->proto = proto;
    363  1.1   itojun 	bcopy(sp, &ep->src, sp->sa_len);
    364  1.1   itojun 	bcopy(sm, &ep->srcmask, sp->sa_len);
    365  1.1   itojun 	bcopy(dp, &ep->dst, dp->sa_len);
    366  1.1   itojun 	bcopy(dm, &ep->dstmask, dp->sa_len);
    367  1.1   itojun 	ep->psw = psw;
    368  1.1   itojun 	ep->arg = arg;
    369  1.1   itojun 
    370  1.1   itojun 	encap_add(ep);
    371  1.1   itojun 
    372  1.1   itojun 	error = 0;
    373  1.1   itojun 	splx(s);
    374  1.1   itojun 	return ep;
    375  1.1   itojun 
    376  1.1   itojun fail:
    377  1.1   itojun 	splx(s);
    378  1.1   itojun 	return NULL;
    379  1.1   itojun }
    380  1.1   itojun 
    381  1.1   itojun const struct encaptab *
    382  1.1   itojun encap_attach_func(af, proto, func, psw, arg)
    383  1.1   itojun 	int af;
    384  1.1   itojun 	int proto;
    385  1.1   itojun 	int (*func) __P((const struct mbuf *, int, int, void *));
    386  1.1   itojun 	const struct protosw *psw;
    387  1.1   itojun 	void *arg;
    388  1.1   itojun {
    389  1.1   itojun 	struct encaptab *ep;
    390  1.1   itojun 	int error;
    391  1.1   itojun 	int s;
    392  1.1   itojun 
    393  1.1   itojun 	s = splsoftnet();
    394  1.1   itojun 	/* sanity check on args */
    395  1.1   itojun 	if (!func) {
    396  1.1   itojun 		error = EINVAL;
    397  1.1   itojun 		goto fail;
    398  1.1   itojun 	}
    399  1.1   itojun 
    400  1.1   itojun 	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
    401  1.1   itojun 	if (ep == NULL) {
    402  1.1   itojun 		error = ENOBUFS;
    403  1.1   itojun 		goto fail;
    404  1.1   itojun 	}
    405  1.1   itojun 	bzero(ep, sizeof(*ep));
    406  1.1   itojun 
    407  1.1   itojun 	ep->af = af;
    408  1.1   itojun 	ep->proto = proto;
    409  1.1   itojun 	ep->func = func;
    410  1.1   itojun 	ep->psw = psw;
    411  1.1   itojun 	ep->arg = arg;
    412  1.1   itojun 
    413  1.1   itojun 	encap_add(ep);
    414  1.1   itojun 
    415  1.1   itojun 	error = 0;
    416  1.1   itojun 	splx(s);
    417  1.1   itojun 	return ep;
    418  1.1   itojun 
    419  1.1   itojun fail:
    420  1.1   itojun 	splx(s);
    421  1.1   itojun 	return NULL;
    422  1.1   itojun }
    423  1.1   itojun 
    424  1.1   itojun int
    425  1.1   itojun encap_detach(cookie)
    426  1.1   itojun 	const struct encaptab *cookie;
    427  1.1   itojun {
    428  1.1   itojun 	const struct encaptab *ep = cookie;
    429  1.1   itojun 	struct encaptab *p;
    430  1.1   itojun 
    431  1.1   itojun 	for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
    432  1.1   itojun 		if (p == ep) {
    433  1.1   itojun 			LIST_REMOVE(p, chain);
    434  1.1   itojun 			free(p, M_NETADDR);	/*XXX*/
    435  1.1   itojun 			return 0;
    436  1.1   itojun 		}
    437  1.1   itojun 	}
    438  1.1   itojun 
    439  1.1   itojun 	return EINVAL;
    440  1.1   itojun }
    441  1.1   itojun 
    442  1.1   itojun static int
    443  1.1   itojun mask_match(ep, sp, dp)
    444  1.1   itojun 	const struct encaptab *ep;
    445  1.1   itojun 	const struct sockaddr *sp;
    446  1.1   itojun 	const struct sockaddr *dp;
    447  1.1   itojun {
    448  1.1   itojun 	struct sockaddr_storage s;
    449  1.1   itojun 	struct sockaddr_storage d;
    450  1.1   itojun 	int i;
    451  1.4   itojun 	const u_int8_t *p, *q;
    452  1.4   itojun 	u_int8_t *r;
    453  1.1   itojun 	int matchlen;
    454  1.1   itojun 
    455  1.1   itojun 	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
    456  1.1   itojun 		return 0;
    457  1.1   itojun 	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
    458  1.1   itojun 		return 0;
    459  1.1   itojun 	if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
    460  1.1   itojun 		return 0;
    461  1.1   itojun 
    462  1.1   itojun 	matchlen = 0;
    463  1.1   itojun 
    464  1.4   itojun 	p = (const u_int8_t *)sp;
    465  1.4   itojun 	q = (const u_int8_t *)&ep->srcmask;
    466  1.1   itojun 	r = (u_int8_t *)&s;
    467  1.1   itojun 	for (i = 0 ; i < sp->sa_len; i++) {
    468  1.1   itojun 		r[i] = p[i] & q[i];
    469  1.1   itojun 		/* XXX estimate */
    470  1.1   itojun 		matchlen += (q[i] ? 8 : 0);
    471  1.1   itojun 	}
    472  1.1   itojun 
    473  1.4   itojun 	p = (const u_int8_t *)dp;
    474  1.4   itojun 	q = (const u_int8_t *)&ep->dstmask;
    475  1.1   itojun 	r = (u_int8_t *)&d;
    476  1.1   itojun 	for (i = 0 ; i < dp->sa_len; i++) {
    477  1.1   itojun 		r[i] = p[i] & q[i];
    478  1.1   itojun 		/* XXX rough estimate */
    479  1.1   itojun 		matchlen += (q[i] ? 8 : 0);
    480  1.1   itojun 	}
    481  1.1   itojun 
    482  1.1   itojun 	/* need to overwrite len/family portion as we don't compare them */
    483  1.1   itojun 	s.ss_len = sp->sa_len;
    484  1.1   itojun 	s.ss_family = sp->sa_family;
    485  1.1   itojun 	d.ss_len = dp->sa_len;
    486  1.1   itojun 	d.ss_family = dp->sa_family;
    487  1.1   itojun 
    488  1.1   itojun 	if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
    489  1.1   itojun 	    bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
    490  1.1   itojun 		return matchlen;
    491  1.1   itojun 	} else
    492  1.1   itojun 		return 0;
    493  1.1   itojun }
    494  1.1   itojun 
    495  1.1   itojun static void
    496  1.1   itojun encap_fillarg(m, ep)
    497  1.1   itojun 	struct mbuf *m;
    498  1.1   itojun 	const struct encaptab *ep;
    499  1.1   itojun {
    500  1.1   itojun #if 0
    501  1.1   itojun 	m->m_pkthdr.aux = ep->arg;
    502  1.1   itojun #else
    503  1.1   itojun 	struct mbuf *n;
    504  1.1   itojun 
    505  1.1   itojun 	n = m_aux_add(m, AF_INET, IPPROTO_IPV4);
    506  1.1   itojun 	if (n) {
    507  1.1   itojun 		*mtod(n, void **) = ep->arg;
    508  1.1   itojun 		n->m_len = sizeof(void *);
    509  1.1   itojun 	}
    510  1.1   itojun #endif
    511  1.1   itojun }
    512  1.1   itojun 
    513  1.1   itojun void *
    514  1.1   itojun encap_getarg(m)
    515  1.1   itojun 	struct mbuf *m;
    516  1.1   itojun {
    517  1.1   itojun 	void *p;
    518  1.1   itojun #if 0
    519  1.1   itojun 	p = m->m_pkthdr.aux;
    520  1.1   itojun 	m->m_pkthdr.aux = NULL;
    521  1.1   itojun 	return p;
    522  1.1   itojun #else
    523  1.1   itojun 	struct mbuf *n;
    524  1.1   itojun 
    525  1.1   itojun 	p = NULL;
    526  1.1   itojun 	n = m_aux_find(m, AF_INET, IPPROTO_IPV4);
    527  1.1   itojun 	if (n) {
    528  1.1   itojun 		if (n->m_len == sizeof(void *))
    529  1.1   itojun 			p = *mtod(n, void **);
    530  1.1   itojun 		m_aux_delete(m, n);
    531  1.1   itojun 	}
    532  1.1   itojun 	return p;
    533  1.1   itojun #endif
    534  1.1   itojun }
    535