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