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