ip_encap.c revision 78064
162587Sitojun/*	$FreeBSD: head/sys/netinet/ip_encap.c 78064 2001-06-11 12:39:29Z ume $	*/
278064Sume/*	$KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $	*/
362587Sitojun
462587Sitojun/*
562587Sitojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
662587Sitojun * All rights reserved.
762587Sitojun *
862587Sitojun * Redistribution and use in source and binary forms, with or without
962587Sitojun * modification, are permitted provided that the following conditions
1062587Sitojun * are met:
1162587Sitojun * 1. Redistributions of source code must retain the above copyright
1262587Sitojun *    notice, this list of conditions and the following disclaimer.
1362587Sitojun * 2. Redistributions in binary form must reproduce the above copyright
1462587Sitojun *    notice, this list of conditions and the following disclaimer in the
1562587Sitojun *    documentation and/or other materials provided with the distribution.
1662587Sitojun * 3. Neither the name of the project nor the names of its contributors
1762587Sitojun *    may be used to endorse or promote products derived from this software
1862587Sitojun *    without specific prior written permission.
1962587Sitojun *
2062587Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2162587Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2262587Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2362587Sitojun * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2462587Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2562587Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2662587Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2762587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2862587Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2962587Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3062587Sitojun * SUCH DAMAGE.
3162587Sitojun */
3262587Sitojun/*
3362587Sitojun * My grandfather said that there's a devil inside tunnelling technology...
3462587Sitojun *
3562587Sitojun * We have surprisingly many protocols that want packets with IP protocol
3662587Sitojun * #4 or #41.  Here's a list of protocols that want protocol #41:
3762587Sitojun *	RFC1933 configured tunnel
3862587Sitojun *	RFC1933 automatic tunnel
3962587Sitojun *	RFC2401 IPsec tunnel
4062587Sitojun *	RFC2473 IPv6 generic packet tunnelling
4162587Sitojun *	RFC2529 6over4 tunnel
4262587Sitojun *	mobile-ip6 (uses RFC2473)
4362587Sitojun *	6to4 tunnel
4462587Sitojun * Here's a list of protocol that want protocol #4:
4562587Sitojun *	RFC1853 IPv4-in-IPv4 tunnelling
4662587Sitojun *	RFC2003 IPv4 encapsulation within IPv4
4762587Sitojun *	RFC2344 reverse tunnelling for mobile-ip4
4862587Sitojun *	RFC2401 IPsec tunnel
4962587Sitojun * Well, what can I say.  They impose different en/decapsulation mechanism
5062587Sitojun * from each other, so they need separate protocol handler.  The only one
5162587Sitojun * we can easily determine by protocol # is IPsec, which always has
5262587Sitojun * AH/ESP/IPComp header right after outer IP header.
5362587Sitojun *
5462587Sitojun * So, clearly good old protosw does not work for protocol #4 and #41.
5562587Sitojun * The code will let you match protocol via src/dst address pair.
5662587Sitojun */
5762587Sitojun/* XXX is M_NETADDR correct? */
5862587Sitojun
5962587Sitojun#include "opt_mrouting.h"
6062587Sitojun#include "opt_inet.h"
6162587Sitojun#include "opt_inet6.h"
6262587Sitojun
6362587Sitojun#include <sys/param.h>
6462587Sitojun#include <sys/systm.h>
6562587Sitojun#include <sys/socket.h>
6662587Sitojun#include <sys/sockio.h>
6762587Sitojun#include <sys/mbuf.h>
6862587Sitojun#include <sys/errno.h>
6962587Sitojun#include <sys/protosw.h>
7078064Sume#include <sys/queue.h>
7162587Sitojun
7262587Sitojun#include <net/if.h>
7362587Sitojun#include <net/route.h>
7462587Sitojun
7562587Sitojun#include <netinet/in.h>
7662587Sitojun#include <netinet/in_systm.h>
7762587Sitojun#include <netinet/ip.h>
7862587Sitojun#include <netinet/ip_var.h>
7962587Sitojun#include <netinet/ip_encap.h>
8062587Sitojun#ifdef MROUTING
8162587Sitojun#include <netinet/ip_mroute.h>
8262587Sitojun#endif /* MROUTING */
8362587Sitojun#include <netinet/ipprotosw.h>
8462587Sitojun
8562587Sitojun#ifdef INET6
8662587Sitojun#include <netinet/ip6.h>
8762587Sitojun#include <netinet6/ip6_var.h>
8862587Sitojun#include <netinet6/ip6protosw.h>
8962587Sitojun#endif
9062587Sitojun
9162587Sitojun#include <machine/stdarg.h>
9262587Sitojun
9362587Sitojun#include <net/net_osdep.h>
9462587Sitojun
9562587Sitojun#include <sys/kernel.h>
9662587Sitojun#include <sys/malloc.h>
9769774Sphkstatic MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
9862587Sitojun
9962587Sitojunstatic void encap_add __P((struct encaptab *));
10062587Sitojunstatic int mask_match __P((const struct encaptab *, const struct sockaddr *,
10162587Sitojun		const struct sockaddr *));
10262587Sitojunstatic void encap_fillarg __P((struct mbuf *, const struct encaptab *));
10362587Sitojun
10478064Sume#ifndef LIST_HEAD_INITIALIZER
10562587Sitojun/* rely upon BSS initialization */
10662587SitojunLIST_HEAD(, encaptab) encaptab;
10778064Sume#else
10878064SumeLIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
10978064Sume#endif
11062587Sitojun
11162587Sitojunvoid
11262587Sitojunencap_init()
11362587Sitojun{
11478064Sume	static int initialized = 0;
11578064Sume
11678064Sume	if (initialized)
11778064Sume		return;
11878064Sume	initialized++;
11962587Sitojun#if 0
12062587Sitojun	/*
12162587Sitojun	 * we cannot use LIST_INIT() here, since drivers may want to call
12262587Sitojun	 * encap_attach(), on driver attach.  encap_init() will be called
12362587Sitojun	 * on AF_INET{,6} initialization, which happens after driver
12462587Sitojun	 * initialization - using LIST_INIT() here can nuke encap_attach()
12562587Sitojun	 * from drivers.
12662587Sitojun	 */
12762587Sitojun	LIST_INIT(&encaptab);
12862587Sitojun#endif
12962587Sitojun}
13062587Sitojun
13178064Sume#ifdef INET
13262587Sitojunvoid
13362587Sitojun#if __STDC__
13462587Sitojunencap4_input(struct mbuf *m, ...)
13562587Sitojun#else
13662587Sitojunencap4_input(m, va_alist)
13762587Sitojun	struct mbuf *m;
13862587Sitojun	va_dcl
13962587Sitojun#endif
14062587Sitojun{
14162587Sitojun	int off, proto;
14262587Sitojun	struct ip *ip;
14362587Sitojun	struct sockaddr_in s, d;
14462587Sitojun	const struct ipprotosw *psw;
14562587Sitojun	struct encaptab *ep, *match;
14662587Sitojun	va_list ap;
14762587Sitojun	int prio, matchprio;
14862587Sitojun
14962587Sitojun	va_start(ap, m);
15062587Sitojun	off = va_arg(ap, int);
15162587Sitojun	proto = va_arg(ap, int);
15262587Sitojun	va_end(ap);
15362587Sitojun
15462587Sitojun	ip = mtod(m, struct ip *);
15562587Sitojun
15662587Sitojun	bzero(&s, sizeof(s));
15762587Sitojun	s.sin_family = AF_INET;
15862587Sitojun	s.sin_len = sizeof(struct sockaddr_in);
15962587Sitojun	s.sin_addr = ip->ip_src;
16062587Sitojun	bzero(&d, sizeof(d));
16162587Sitojun	d.sin_family = AF_INET;
16262587Sitojun	d.sin_len = sizeof(struct sockaddr_in);
16362587Sitojun	d.sin_addr = ip->ip_dst;
16462587Sitojun
16562587Sitojun	match = NULL;
16662587Sitojun	matchprio = 0;
16771999Sphk	LIST_FOREACH(ep, &encaptab, chain) {
16862587Sitojun		if (ep->af != AF_INET)
16962587Sitojun			continue;
17062587Sitojun		if (ep->proto >= 0 && ep->proto != proto)
17162587Sitojun			continue;
17262587Sitojun		if (ep->func)
17362587Sitojun			prio = (*ep->func)(m, off, proto, ep->arg);
17462587Sitojun		else {
17562587Sitojun			/*
17662587Sitojun			 * it's inbound traffic, we need to match in reverse
17762587Sitojun			 * order
17862587Sitojun			 */
17962587Sitojun			prio = mask_match(ep, (struct sockaddr *)&d,
18062587Sitojun			    (struct sockaddr *)&s);
18162587Sitojun		}
18262587Sitojun
18362587Sitojun		/*
18462587Sitojun		 * We prioritize the matches by using bit length of the
18562587Sitojun		 * matches.  mask_match() and user-supplied matching function
18662587Sitojun		 * should return the bit length of the matches (for example,
18762587Sitojun		 * if both src/dst are matched for IPv4, 64 should be returned).
18862587Sitojun		 * 0 or negative return value means "it did not match".
18962587Sitojun		 *
19062587Sitojun		 * The question is, since we have two "mask" portion, we
19162587Sitojun		 * cannot really define total order between entries.
19262587Sitojun		 * For example, which of these should be preferred?
19362587Sitojun		 * mask_match() returns 48 (32 + 16) for both of them.
19462587Sitojun		 *	src=3ffe::/16, dst=3ffe:501::/32
19562587Sitojun		 *	src=3ffe:501::/32, dst=3ffe::/16
19662587Sitojun		 *
19762587Sitojun		 * We need to loop through all the possible candidates
19862587Sitojun		 * to get the best match - the search takes O(n) for
19962587Sitojun		 * n attachments (i.e. interfaces).
20062587Sitojun		 */
20162587Sitojun		if (prio <= 0)
20262587Sitojun			continue;
20362587Sitojun		if (prio > matchprio) {
20462587Sitojun			matchprio = prio;
20562587Sitojun			match = ep;
20662587Sitojun		}
20762587Sitojun	}
20862587Sitojun
20962587Sitojun	if (match) {
21062587Sitojun		/* found a match, "match" has the best one */
21162587Sitojun		psw = (const struct ipprotosw *)match->psw;
21262587Sitojun		if (psw && psw->pr_input) {
21362587Sitojun			encap_fillarg(m, match);
21462587Sitojun			(*psw->pr_input)(m, off, proto);
21562587Sitojun		} else
21662587Sitojun			m_freem(m);
21762587Sitojun		return;
21862587Sitojun	}
21962587Sitojun
22062587Sitojun	/* for backward compatibility */
22162587Sitojun# ifdef MROUTING
22262587Sitojun#  define COMPATFUNC	ipip_input
22362587Sitojun# endif /*MROUTING*/
22462587Sitojun
22562587Sitojun#ifdef COMPATFUNC
22662587Sitojun	if (proto == IPPROTO_IPV4) {
22762587Sitojun		COMPATFUNC(m, off, proto);
22862587Sitojun		return;
22962587Sitojun	}
23062587Sitojun#endif
23162587Sitojun
23262587Sitojun	/* last resort: inject to raw socket */
23362587Sitojun	rip_input(m, off, proto);
23462587Sitojun}
23578064Sume#endif
23662587Sitojun
23762587Sitojun#ifdef INET6
23862587Sitojunint
23962587Sitojunencap6_input(mp, offp, proto)
24062587Sitojun	struct mbuf **mp;
24162587Sitojun	int *offp;
24262587Sitojun	int proto;
24362587Sitojun{
24462587Sitojun	struct mbuf *m = *mp;
24562587Sitojun	struct ip6_hdr *ip6;
24662587Sitojun	struct sockaddr_in6 s, d;
24762587Sitojun	const struct ip6protosw *psw;
24862587Sitojun	struct encaptab *ep, *match;
24962587Sitojun	int prio, matchprio;
25062587Sitojun
25162587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
25262587Sitojun
25362587Sitojun	bzero(&s, sizeof(s));
25462587Sitojun	s.sin6_family = AF_INET6;
25562587Sitojun	s.sin6_len = sizeof(struct sockaddr_in6);
25662587Sitojun	s.sin6_addr = ip6->ip6_src;
25762587Sitojun	bzero(&d, sizeof(d));
25862587Sitojun	d.sin6_family = AF_INET6;
25962587Sitojun	d.sin6_len = sizeof(struct sockaddr_in6);
26062587Sitojun	d.sin6_addr = ip6->ip6_dst;
26162587Sitojun
26262587Sitojun	match = NULL;
26362587Sitojun	matchprio = 0;
26471999Sphk	LIST_FOREACH(ep, &encaptab, chain) {
26562587Sitojun		if (ep->af != AF_INET6)
26662587Sitojun			continue;
26762587Sitojun		if (ep->proto >= 0 && ep->proto != proto)
26862587Sitojun			continue;
26962587Sitojun		if (ep->func)
27062587Sitojun			prio = (*ep->func)(m, *offp, proto, ep->arg);
27162587Sitojun		else {
27262587Sitojun			/*
27362587Sitojun			 * it's inbound traffic, we need to match in reverse
27462587Sitojun			 * order
27562587Sitojun			 */
27662587Sitojun			prio = mask_match(ep, (struct sockaddr *)&d,
27762587Sitojun			    (struct sockaddr *)&s);
27862587Sitojun		}
27962587Sitojun
28062587Sitojun		/* see encap4_input() for issues here */
28162587Sitojun		if (prio <= 0)
28262587Sitojun			continue;
28362587Sitojun		if (prio > matchprio) {
28462587Sitojun			matchprio = prio;
28562587Sitojun			match = ep;
28662587Sitojun		}
28762587Sitojun	}
28862587Sitojun
28962587Sitojun	if (match) {
29062587Sitojun		/* found a match */
29162587Sitojun		psw = (const struct ip6protosw *)match->psw;
29262587Sitojun		if (psw && psw->pr_input) {
29362587Sitojun			encap_fillarg(m, match);
29462587Sitojun			return (*psw->pr_input)(mp, offp, proto);
29562587Sitojun		} else {
29662587Sitojun			m_freem(m);
29762587Sitojun			return IPPROTO_DONE;
29862587Sitojun		}
29962587Sitojun	}
30062587Sitojun
30162587Sitojun	/* last resort: inject to raw socket */
30262587Sitojun	return rip6_input(mp, offp, proto);
30362587Sitojun}
30462587Sitojun#endif
30562587Sitojun
30662587Sitojunstatic void
30762587Sitojunencap_add(ep)
30862587Sitojun	struct encaptab *ep;
30962587Sitojun{
31062587Sitojun
31162587Sitojun	LIST_INSERT_HEAD(&encaptab, ep, chain);
31262587Sitojun}
31362587Sitojun
31462587Sitojun/*
31562587Sitojun * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
31662587Sitojun * length of mask (sm and dm) is assumed to be same as sp/dp.
31762587Sitojun * Return value will be necessary as input (cookie) for encap_detach().
31862587Sitojun */
31962587Sitojunconst struct encaptab *
32062587Sitojunencap_attach(af, proto, sp, sm, dp, dm, psw, arg)
32162587Sitojun	int af;
32262587Sitojun	int proto;
32362587Sitojun	const struct sockaddr *sp, *sm;
32462587Sitojun	const struct sockaddr *dp, *dm;
32562587Sitojun	const struct protosw *psw;
32662587Sitojun	void *arg;
32762587Sitojun{
32862587Sitojun	struct encaptab *ep;
32962587Sitojun	int error;
33062587Sitojun	int s;
33162587Sitojun
33262587Sitojun	s = splnet();
33362587Sitojun	/* sanity check on args */
33462587Sitojun	if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
33562587Sitojun		error = EINVAL;
33662587Sitojun		goto fail;
33762587Sitojun	}
33862587Sitojun	if (sp->sa_len != dp->sa_len) {
33962587Sitojun		error = EINVAL;
34062587Sitojun		goto fail;
34162587Sitojun	}
34262587Sitojun	if (af != sp->sa_family || af != dp->sa_family) {
34362587Sitojun		error = EINVAL;
34462587Sitojun		goto fail;
34562587Sitojun	}
34662587Sitojun
34762587Sitojun	/* check if anyone have already attached with exactly same config */
34871999Sphk	LIST_FOREACH(ep, &encaptab, chain) {
34962587Sitojun		if (ep->af != af)
35062587Sitojun			continue;
35162587Sitojun		if (ep->proto != proto)
35262587Sitojun			continue;
35362587Sitojun		if (ep->src.ss_len != sp->sa_len ||
35462587Sitojun		    bcmp(&ep->src, sp, sp->sa_len) != 0 ||
35562587Sitojun		    bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
35662587Sitojun			continue;
35762587Sitojun		if (ep->dst.ss_len != dp->sa_len ||
35862587Sitojun		    bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
35962587Sitojun		    bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
36062587Sitojun			continue;
36162587Sitojun
36262587Sitojun		error = EEXIST;
36362587Sitojun		goto fail;
36462587Sitojun	}
36562587Sitojun
36662587Sitojun	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
36762587Sitojun	if (ep == NULL) {
36862587Sitojun		error = ENOBUFS;
36962587Sitojun		goto fail;
37062587Sitojun	}
37162587Sitojun	bzero(ep, sizeof(*ep));
37262587Sitojun
37362587Sitojun	ep->af = af;
37462587Sitojun	ep->proto = proto;
37562587Sitojun	bcopy(sp, &ep->src, sp->sa_len);
37662587Sitojun	bcopy(sm, &ep->srcmask, sp->sa_len);
37762587Sitojun	bcopy(dp, &ep->dst, dp->sa_len);
37862587Sitojun	bcopy(dm, &ep->dstmask, dp->sa_len);
37962587Sitojun	ep->psw = psw;
38062587Sitojun	ep->arg = arg;
38162587Sitojun
38262587Sitojun	encap_add(ep);
38362587Sitojun
38462587Sitojun	error = 0;
38562587Sitojun	splx(s);
38662587Sitojun	return ep;
38762587Sitojun
38862587Sitojunfail:
38962587Sitojun	splx(s);
39062587Sitojun	return NULL;
39162587Sitojun}
39262587Sitojun
39362587Sitojunconst struct encaptab *
39462587Sitojunencap_attach_func(af, proto, func, psw, arg)
39562587Sitojun	int af;
39662587Sitojun	int proto;
39762587Sitojun	int (*func) __P((const struct mbuf *, int, int, void *));
39862587Sitojun	const struct protosw *psw;
39962587Sitojun	void *arg;
40062587Sitojun{
40162587Sitojun	struct encaptab *ep;
40262587Sitojun	int error;
40362587Sitojun	int s;
40462587Sitojun
40562587Sitojun	s = splnet();
40662587Sitojun	/* sanity check on args */
40762587Sitojun	if (!func) {
40862587Sitojun		error = EINVAL;
40962587Sitojun		goto fail;
41062587Sitojun	}
41162587Sitojun
41262587Sitojun	ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT);	/*XXX*/
41362587Sitojun	if (ep == NULL) {
41462587Sitojun		error = ENOBUFS;
41562587Sitojun		goto fail;
41662587Sitojun	}
41762587Sitojun	bzero(ep, sizeof(*ep));
41862587Sitojun
41962587Sitojun	ep->af = af;
42062587Sitojun	ep->proto = proto;
42162587Sitojun	ep->func = func;
42262587Sitojun	ep->psw = psw;
42362587Sitojun	ep->arg = arg;
42462587Sitojun
42562587Sitojun	encap_add(ep);
42662587Sitojun
42762587Sitojun	error = 0;
42862587Sitojun	splx(s);
42962587Sitojun	return ep;
43062587Sitojun
43162587Sitojunfail:
43262587Sitojun	splx(s);
43362587Sitojun	return NULL;
43462587Sitojun}
43562587Sitojun
43662587Sitojunint
43762587Sitojunencap_detach(cookie)
43862587Sitojun	const struct encaptab *cookie;
43962587Sitojun{
44062587Sitojun	const struct encaptab *ep = cookie;
44162587Sitojun	struct encaptab *p;
44262587Sitojun
44371999Sphk	LIST_FOREACH(p, &encaptab, chain) {
44462587Sitojun		if (p == ep) {
44562587Sitojun			LIST_REMOVE(p, chain);
44662587Sitojun			free(p, M_NETADDR);	/*XXX*/
44762587Sitojun			return 0;
44862587Sitojun		}
44962587Sitojun	}
45062587Sitojun
45162587Sitojun	return EINVAL;
45262587Sitojun}
45362587Sitojun
45462587Sitojunstatic int
45562587Sitojunmask_match(ep, sp, dp)
45662587Sitojun	const struct encaptab *ep;
45762587Sitojun	const struct sockaddr *sp;
45862587Sitojun	const struct sockaddr *dp;
45962587Sitojun{
46062587Sitojun	struct sockaddr_storage s;
46162587Sitojun	struct sockaddr_storage d;
46262587Sitojun	int i;
46362587Sitojun	const u_int8_t *p, *q;
46462587Sitojun	u_int8_t *r;
46562587Sitojun	int matchlen;
46662587Sitojun
46762587Sitojun	if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
46862587Sitojun		return 0;
46962587Sitojun	if (sp->sa_family != ep->af || dp->sa_family != ep->af)
47062587Sitojun		return 0;
47162587Sitojun	if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
47262587Sitojun		return 0;
47362587Sitojun
47462587Sitojun	matchlen = 0;
47562587Sitojun
47662587Sitojun	p = (const u_int8_t *)sp;
47762587Sitojun	q = (const u_int8_t *)&ep->srcmask;
47862587Sitojun	r = (u_int8_t *)&s;
47962587Sitojun	for (i = 0 ; i < sp->sa_len; i++) {
48062587Sitojun		r[i] = p[i] & q[i];
48162587Sitojun		/* XXX estimate */
48262587Sitojun		matchlen += (q[i] ? 8 : 0);
48362587Sitojun	}
48462587Sitojun
48562587Sitojun	p = (const u_int8_t *)dp;
48662587Sitojun	q = (const u_int8_t *)&ep->dstmask;
48762587Sitojun	r = (u_int8_t *)&d;
48862587Sitojun	for (i = 0 ; i < dp->sa_len; i++) {
48962587Sitojun		r[i] = p[i] & q[i];
49062587Sitojun		/* XXX rough estimate */
49162587Sitojun		matchlen += (q[i] ? 8 : 0);
49262587Sitojun	}
49362587Sitojun
49462587Sitojun	/* need to overwrite len/family portion as we don't compare them */
49562587Sitojun	s.ss_len = sp->sa_len;
49662587Sitojun	s.ss_family = sp->sa_family;
49762587Sitojun	d.ss_len = dp->sa_len;
49862587Sitojun	d.ss_family = dp->sa_family;
49962587Sitojun
50062587Sitojun	if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
50162587Sitojun	    bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
50262587Sitojun		return matchlen;
50362587Sitojun	} else
50462587Sitojun		return 0;
50562587Sitojun}
50662587Sitojun
50762587Sitojunstatic void
50862587Sitojunencap_fillarg(m, ep)
50962587Sitojun	struct mbuf *m;
51062587Sitojun	const struct encaptab *ep;
51162587Sitojun{
51262587Sitojun#if 0
51362587Sitojun	m->m_pkthdr.aux = ep->arg;
51462587Sitojun#else
51562587Sitojun	struct mbuf *n;
51662587Sitojun
51762587Sitojun	n = m_aux_add(m, AF_INET, IPPROTO_IPV4);
51862587Sitojun	if (n) {
51962587Sitojun		*mtod(n, void **) = ep->arg;
52062587Sitojun		n->m_len = sizeof(void *);
52162587Sitojun	}
52262587Sitojun#endif
52362587Sitojun}
52462587Sitojun
52562587Sitojunvoid *
52662587Sitojunencap_getarg(m)
52762587Sitojun	struct mbuf *m;
52862587Sitojun{
52962587Sitojun	void *p;
53062587Sitojun#if 0
53162587Sitojun	p = m->m_pkthdr.aux;
53262587Sitojun	m->m_pkthdr.aux = NULL;
53362587Sitojun	return p;
53462587Sitojun#else
53562587Sitojun	struct mbuf *n;
53662587Sitojun
53762587Sitojun	p = NULL;
53862587Sitojun	n = m_aux_find(m, AF_INET, IPPROTO_IPV4);
53962587Sitojun	if (n) {
54062587Sitojun		if (n->m_len == sizeof(void *))
54162587Sitojun			p = *mtod(n, void **);
54262587Sitojun		m_aux_delete(m, n);
54362587Sitojun	}
54462587Sitojun	return p;
54562587Sitojun#endif
54662587Sitojun}
547