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