Home | History | Annotate | Line # | Download | only in netinet
ip_encap.c revision 1.54
      1  1.54  knakahar /*	$NetBSD: ip_encap.c,v 1.54 2016/07/04 04:17:25 knakahara Exp $	*/
      2   1.7    itojun /*	$KAME: ip_encap.c,v 1.73 2001/10/02 08:30:58 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.7    itojun  *	RFC3056 6to4 tunnel
     43   1.7    itojun  *	isatap tunnel
     44   1.1    itojun  *	mobile-ip6 (uses RFC2473)
     45   1.1    itojun  * Here's a list of protocol that want protocol #4:
     46   1.1    itojun  *	RFC1853 IPv4-in-IPv4 tunnelling
     47   1.1    itojun  *	RFC2003 IPv4 encapsulation within IPv4
     48   1.1    itojun  *	RFC2344 reverse tunnelling for mobile-ip4
     49   1.1    itojun  *	RFC2401 IPsec tunnel
     50   1.1    itojun  * Well, what can I say.  They impose different en/decapsulation mechanism
     51   1.1    itojun  * from each other, so they need separate protocol handler.  The only one
     52   1.1    itojun  * we can easily determine by protocol # is IPsec, which always has
     53   1.1    itojun  * AH/ESP/IPComp header right after outer IP header.
     54   1.1    itojun  *
     55   1.1    itojun  * So, clearly good old protosw does not work for protocol #4 and #41.
     56   1.1    itojun  * The code will let you match protocol via src/dst address pair.
     57   1.1    itojun  */
     58   1.1    itojun /* XXX is M_NETADDR correct? */
     59   1.6     lukem 
     60   1.7    itojun /*
     61  1.45     ozaki  * The code will use radix table for tunnel lookup, for
     62   1.7    itojun  * tunnels registered with encap_attach() with a addr/mask pair.
     63   1.7    itojun  * Faster on machines with thousands of tunnel registerations (= interfaces).
     64   1.7    itojun  *
     65   1.7    itojun  * The code assumes that radix table code can handle non-continuous netmask,
     66   1.7    itojun  * as it will pass radix table memory region with (src + dst) sockaddr pair.
     67   1.7    itojun  */
     68   1.7    itojun 
     69   1.6     lukem #include <sys/cdefs.h>
     70  1.54  knakahar __KERNEL_RCSID(0, "$NetBSD: ip_encap.c,v 1.54 2016/07/04 04:17:25 knakahara Exp $");
     71   1.1    itojun 
     72  1.46     pooka #ifdef _KERNEL_OPT
     73   1.4    itojun #include "opt_mrouting.h"
     74   1.4    itojun #include "opt_inet.h"
     75  1.46     pooka #endif
     76   1.1    itojun 
     77   1.1    itojun #include <sys/param.h>
     78   1.1    itojun #include <sys/systm.h>
     79   1.1    itojun #include <sys/socket.h>
     80   1.1    itojun #include <sys/sockio.h>
     81   1.1    itojun #include <sys/mbuf.h>
     82   1.1    itojun #include <sys/errno.h>
     83   1.4    itojun #include <sys/queue.h>
     84  1.47  knakahar #include <sys/kmem.h>
     85  1.54  knakahar #include <sys/once.h>
     86  1.54  knakahar #include <sys/rwlock.h>
     87   1.1    itojun 
     88   1.1    itojun #include <net/if.h>
     89   1.1    itojun 
     90   1.1    itojun #include <netinet/in.h>
     91   1.1    itojun #include <netinet/in_systm.h>
     92   1.1    itojun #include <netinet/ip.h>
     93   1.1    itojun #include <netinet/ip_var.h>
     94   1.1    itojun #include <netinet/ip_encap.h>
     95   1.1    itojun #ifdef MROUTING
     96   1.1    itojun #include <netinet/ip_mroute.h>
     97   1.1    itojun #endif /* MROUTING */
     98   1.1    itojun 
     99   1.1    itojun #ifdef INET6
    100   1.1    itojun #include <netinet/ip6.h>
    101   1.1    itojun #include <netinet6/ip6_var.h>
    102  1.51  knakahar #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
    103   1.7    itojun #include <netinet6/in6_var.h>
    104   1.7    itojun #include <netinet6/in6_pcb.h>
    105   1.7    itojun #include <netinet/icmp6.h>
    106   1.1    itojun #endif
    107   1.1    itojun 
    108   1.1    itojun #include <net/net_osdep.h>
    109   1.1    itojun 
    110   1.7    itojun enum direction { INBOUND, OUTBOUND };
    111   1.7    itojun 
    112   1.7    itojun #ifdef INET
    113  1.22     perry static struct encaptab *encap4_lookup(struct mbuf *, int, int, enum direction);
    114   1.7    itojun #endif
    115   1.7    itojun #ifdef INET6
    116  1.22     perry static struct encaptab *encap6_lookup(struct mbuf *, int, int, enum direction);
    117   1.7    itojun #endif
    118  1.22     perry static int encap_add(struct encaptab *);
    119  1.22     perry static int encap_remove(struct encaptab *);
    120  1.22     perry static int encap_afcheck(int, const struct sockaddr *, const struct sockaddr *);
    121  1.22     perry static struct radix_node_head *encap_rnh(int);
    122  1.22     perry static int mask_matchlen(const struct sockaddr *);
    123  1.22     perry static void encap_fillarg(struct mbuf *, const struct encaptab *);
    124   1.1    itojun 
    125   1.2   thorpej LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
    126   1.1    itojun 
    127   1.7    itojun struct radix_node_head *encap_head[2];	/* 0 for AF_INET, 1 for AF_INET6 */
    128   1.7    itojun 
    129  1.54  knakahar static ONCE_DECL(encap_init_control);
    130  1.54  knakahar 
    131  1.54  knakahar static krwlock_t encap_whole_lock __cacheline_aligned;
    132  1.54  knakahar 
    133  1.54  knakahar static int encap_init_once(void);
    134  1.54  knakahar 
    135   1.1    itojun void
    136  1.23     perry encap_init(void)
    137   1.1    itojun {
    138   1.7    itojun 	static int initialized = 0;
    139   1.7    itojun 
    140   1.7    itojun 	if (initialized)
    141   1.7    itojun 		return;
    142   1.7    itojun 	initialized++;
    143   1.1    itojun #if 0
    144   1.1    itojun 	/*
    145   1.1    itojun 	 * we cannot use LIST_INIT() here, since drivers may want to call
    146   1.4    itojun 	 * encap_attach(), on driver attach.  encap_init() will be called
    147   1.1    itojun 	 * on AF_INET{,6} initialization, which happens after driver
    148   1.1    itojun 	 * initialization - using LIST_INIT() here can nuke encap_attach()
    149   1.1    itojun 	 * from drivers.
    150   1.1    itojun 	 */
    151   1.1    itojun 	LIST_INIT(&encaptab);
    152   1.1    itojun #endif
    153   1.7    itojun 
    154   1.7    itojun 	/*
    155  1.38     pooka 	 * initialize radix lookup table when the radix subsystem is inited.
    156   1.7    itojun 	 */
    157  1.38     pooka 	rn_delayedinit((void *)&encap_head[0],
    158  1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    159   1.7    itojun #ifdef INET6
    160  1.38     pooka 	rn_delayedinit((void *)&encap_head[1],
    161  1.38     pooka 	    sizeof(struct sockaddr_pack) << 3);
    162   1.7    itojun #endif
    163   1.1    itojun }
    164   1.1    itojun 
    165   1.4    itojun #ifdef INET
    166   1.7    itojun static struct encaptab *
    167  1.23     perry encap4_lookup(struct mbuf *m, int off, int proto, enum direction dir)
    168   1.1    itojun {
    169   1.1    itojun 	struct ip *ip;
    170  1.33     pooka 	struct ip_pack4 pack;
    171   1.1    itojun 	struct encaptab *ep, *match;
    172   1.1    itojun 	int prio, matchprio;
    173   1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET);
    174   1.7    itojun 	struct radix_node *rn;
    175   1.1    itojun 
    176  1.41     ozaki 	KASSERT(m->m_len >= sizeof(*ip));
    177  1.54  knakahar 	KASSERT(rw_read_held(&encap_whole_lock));
    178  1.41     ozaki 
    179   1.1    itojun 	ip = mtod(m, struct ip *);
    180   1.1    itojun 
    181  1.35    cegger 	memset(&pack, 0, sizeof(pack));
    182   1.7    itojun 	pack.p.sp_len = sizeof(pack);
    183   1.7    itojun 	pack.mine.sin_family = pack.yours.sin_family = AF_INET;
    184   1.7    itojun 	pack.mine.sin_len = pack.yours.sin_len = sizeof(struct sockaddr_in);
    185   1.7    itojun 	if (dir == INBOUND) {
    186   1.7    itojun 		pack.mine.sin_addr = ip->ip_dst;
    187   1.7    itojun 		pack.yours.sin_addr = ip->ip_src;
    188   1.7    itojun 	} else {
    189   1.7    itojun 		pack.mine.sin_addr = ip->ip_src;
    190   1.7    itojun 		pack.yours.sin_addr = ip->ip_dst;
    191   1.7    itojun 	}
    192   1.1    itojun 
    193   1.1    itojun 	match = NULL;
    194   1.1    itojun 	matchprio = 0;
    195   1.7    itojun 
    196  1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    197   1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    198   1.7    itojun 		match = (struct encaptab *)rn;
    199   1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    200   1.7    itojun 		    mask_matchlen(match->dstmask);
    201   1.7    itojun 	}
    202   1.7    itojun 
    203  1.31    dyoung 	LIST_FOREACH(ep, &encaptab, chain) {
    204   1.1    itojun 		if (ep->af != AF_INET)
    205   1.1    itojun 			continue;
    206   1.1    itojun 		if (ep->proto >= 0 && ep->proto != proto)
    207   1.1    itojun 			continue;
    208   1.1    itojun 		if (ep->func)
    209   1.1    itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    210  1.45     ozaki 		else
    211   1.7    itojun 			continue;
    212   1.1    itojun 
    213   1.1    itojun 		/*
    214   1.1    itojun 		 * We prioritize the matches by using bit length of the
    215   1.1    itojun 		 * matches.  mask_match() and user-supplied matching function
    216   1.1    itojun 		 * should return the bit length of the matches (for example,
    217   1.1    itojun 		 * if both src/dst are matched for IPv4, 64 should be returned).
    218   1.1    itojun 		 * 0 or negative return value means "it did not match".
    219   1.1    itojun 		 *
    220   1.1    itojun 		 * The question is, since we have two "mask" portion, we
    221   1.1    itojun 		 * cannot really define total order between entries.
    222   1.1    itojun 		 * For example, which of these should be preferred?
    223   1.1    itojun 		 * mask_match() returns 48 (32 + 16) for both of them.
    224   1.1    itojun 		 *	src=3ffe::/16, dst=3ffe:501::/32
    225   1.1    itojun 		 *	src=3ffe:501::/32, dst=3ffe::/16
    226   1.1    itojun 		 *
    227   1.1    itojun 		 * We need to loop through all the possible candidates
    228   1.1    itojun 		 * to get the best match - the search takes O(n) for
    229   1.1    itojun 		 * n attachments (i.e. interfaces).
    230   1.7    itojun 		 *
    231   1.7    itojun 		 * For radix-based lookup, I guess source takes precedence.
    232   1.7    itojun 		 * See rn_{refines,lexobetter} for the correct answer.
    233   1.1    itojun 		 */
    234   1.1    itojun 		if (prio <= 0)
    235   1.1    itojun 			continue;
    236   1.1    itojun 		if (prio > matchprio) {
    237   1.1    itojun 			matchprio = prio;
    238   1.1    itojun 			match = ep;
    239   1.1    itojun 		}
    240   1.1    itojun 	}
    241   1.1    itojun 
    242   1.7    itojun 	return match;
    243   1.7    itojun }
    244   1.7    itojun 
    245   1.7    itojun void
    246   1.7    itojun encap4_input(struct mbuf *m, ...)
    247   1.7    itojun {
    248   1.7    itojun 	int off, proto;
    249   1.7    itojun 	va_list ap;
    250  1.51  knakahar 	const struct encapsw *esw;
    251   1.7    itojun 	struct encaptab *match;
    252   1.7    itojun 
    253   1.7    itojun 	va_start(ap, m);
    254   1.7    itojun 	off = va_arg(ap, int);
    255   1.7    itojun 	proto = va_arg(ap, int);
    256   1.7    itojun 	va_end(ap);
    257   1.7    itojun 
    258  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_READER);
    259   1.7    itojun 	match = encap4_lookup(m, off, proto, INBOUND);
    260   1.7    itojun 
    261   1.1    itojun 	if (match) {
    262   1.1    itojun 		/* found a match, "match" has the best one */
    263  1.51  knakahar 		esw = match->esw;
    264  1.51  knakahar 		if (esw && esw->encapsw4.pr_input) {
    265   1.1    itojun 			encap_fillarg(m, match);
    266  1.54  knakahar 			rw_exit(&encap_whole_lock);
    267  1.51  knakahar 			(*esw->encapsw4.pr_input)(m, off, proto);
    268  1.54  knakahar 		} else {
    269  1.54  knakahar 			rw_exit(&encap_whole_lock);
    270   1.1    itojun 			m_freem(m);
    271  1.54  knakahar 		}
    272   1.1    itojun 		return;
    273   1.1    itojun 	}
    274  1.54  knakahar 	rw_exit(&encap_whole_lock);
    275   1.1    itojun 
    276   1.1    itojun 	/* last resort: inject to raw socket */
    277   1.1    itojun 	rip_input(m, off, proto);
    278   1.1    itojun }
    279   1.1    itojun #endif
    280   1.1    itojun 
    281   1.1    itojun #ifdef INET6
    282   1.7    itojun static struct encaptab *
    283  1.23     perry encap6_lookup(struct mbuf *m, int off, int proto, enum direction dir)
    284   1.1    itojun {
    285   1.1    itojun 	struct ip6_hdr *ip6;
    286  1.33     pooka 	struct ip_pack6 pack;
    287   1.7    itojun 	int prio, matchprio;
    288   1.1    itojun 	struct encaptab *ep, *match;
    289   1.7    itojun 	struct radix_node_head *rnh = encap_rnh(AF_INET6);
    290   1.7    itojun 	struct radix_node *rn;
    291   1.1    itojun 
    292  1.41     ozaki 	KASSERT(m->m_len >= sizeof(*ip6));
    293  1.54  knakahar 	KASSERT(rw_read_held(&encap_whole_lock));
    294  1.41     ozaki 
    295   1.1    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    296   1.1    itojun 
    297  1.35    cegger 	memset(&pack, 0, sizeof(pack));
    298   1.7    itojun 	pack.p.sp_len = sizeof(pack);
    299   1.7    itojun 	pack.mine.sin6_family = pack.yours.sin6_family = AF_INET6;
    300   1.7    itojun 	pack.mine.sin6_len = pack.yours.sin6_len = sizeof(struct sockaddr_in6);
    301   1.7    itojun 	if (dir == INBOUND) {
    302   1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_dst;
    303   1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_src;
    304   1.7    itojun 	} else {
    305   1.7    itojun 		pack.mine.sin6_addr = ip6->ip6_src;
    306   1.7    itojun 		pack.yours.sin6_addr = ip6->ip6_dst;
    307   1.7    itojun 	}
    308   1.1    itojun 
    309   1.1    itojun 	match = NULL;
    310   1.1    itojun 	matchprio = 0;
    311   1.7    itojun 
    312  1.30  christos 	rn = rnh->rnh_matchaddr((void *)&pack, rnh);
    313   1.7    itojun 	if (rn && (rn->rn_flags & RNF_ROOT) == 0) {
    314   1.7    itojun 		match = (struct encaptab *)rn;
    315   1.7    itojun 		matchprio = mask_matchlen(match->srcmask) +
    316   1.7    itojun 		    mask_matchlen(match->dstmask);
    317   1.7    itojun 	}
    318   1.7    itojun 
    319  1.31    dyoung 	LIST_FOREACH(ep, &encaptab, chain) {
    320   1.1    itojun 		if (ep->af != AF_INET6)
    321   1.1    itojun 			continue;
    322   1.1    itojun 		if (ep->proto >= 0 && ep->proto != proto)
    323   1.1    itojun 			continue;
    324   1.1    itojun 		if (ep->func)
    325   1.7    itojun 			prio = (*ep->func)(m, off, proto, ep->arg);
    326  1.45     ozaki 		else
    327   1.7    itojun 			continue;
    328   1.1    itojun 
    329   1.7    itojun 		/* see encap4_lookup() for issues here */
    330   1.1    itojun 		if (prio <= 0)
    331   1.1    itojun 			continue;
    332   1.1    itojun 		if (prio > matchprio) {
    333   1.1    itojun 			matchprio = prio;
    334   1.1    itojun 			match = ep;
    335   1.1    itojun 		}
    336   1.1    itojun 	}
    337   1.1    itojun 
    338   1.7    itojun 	return match;
    339   1.7    itojun }
    340   1.7    itojun 
    341   1.7    itojun int
    342  1.23     perry encap6_input(struct mbuf **mp, int *offp, int proto)
    343   1.7    itojun {
    344   1.7    itojun 	struct mbuf *m = *mp;
    345  1.51  knakahar 	const struct encapsw *esw;
    346   1.7    itojun 	struct encaptab *match;
    347   1.7    itojun 
    348  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_READER);
    349   1.7    itojun 	match = encap6_lookup(m, *offp, proto, INBOUND);
    350   1.7    itojun 
    351   1.1    itojun 	if (match) {
    352   1.1    itojun 		/* found a match */
    353  1.51  knakahar 		esw = match->esw;
    354  1.51  knakahar 		if (esw && esw->encapsw6.pr_input) {
    355   1.1    itojun 			encap_fillarg(m, match);
    356  1.54  knakahar 			rw_exit(&encap_whole_lock);
    357  1.51  knakahar 			return (*esw->encapsw6.pr_input)(mp, offp, proto);
    358   1.1    itojun 		} else {
    359  1.54  knakahar 			rw_exit(&encap_whole_lock);
    360   1.1    itojun 			m_freem(m);
    361   1.1    itojun 			return IPPROTO_DONE;
    362   1.1    itojun 		}
    363   1.1    itojun 	}
    364  1.54  knakahar 	rw_exit(&encap_whole_lock);
    365   1.1    itojun 
    366   1.1    itojun 	/* last resort: inject to raw socket */
    367   1.1    itojun 	return rip6_input(mp, offp, proto);
    368   1.1    itojun }
    369   1.1    itojun #endif
    370   1.1    itojun 
    371  1.54  knakahar /*
    372  1.54  knakahar  * XXX
    373  1.54  knakahar  * The encaptab list and the rnh radix tree must be manipulated atomically.
    374  1.54  knakahar  */
    375   1.7    itojun static int
    376  1.23     perry encap_add(struct encaptab *ep)
    377   1.1    itojun {
    378   1.7    itojun 	struct radix_node_head *rnh = encap_rnh(ep->af);
    379   1.7    itojun 	int error = 0;
    380   1.1    itojun 
    381  1.54  knakahar 	KASSERT(rw_write_held(&encap_whole_lock));
    382  1.54  knakahar 
    383   1.1    itojun 	LIST_INSERT_HEAD(&encaptab, ep, chain);
    384   1.7    itojun 	if (!ep->func && rnh) {
    385  1.30  christos 		if (!rnh->rnh_addaddr((void *)ep->addrpack,
    386  1.30  christos 		    (void *)ep->maskpack, rnh, ep->nodes)) {
    387   1.7    itojun 			error = EEXIST;
    388   1.7    itojun 			goto fail;
    389   1.7    itojun 		}
    390   1.7    itojun 	}
    391   1.7    itojun 	return error;
    392   1.7    itojun 
    393   1.7    itojun  fail:
    394   1.7    itojun 	LIST_REMOVE(ep, chain);
    395   1.7    itojun 	return error;
    396   1.7    itojun }
    397   1.7    itojun 
    398  1.54  knakahar /*
    399  1.54  knakahar  * XXX
    400  1.54  knakahar  * The encaptab list and the rnh radix tree must be manipulated atomically.
    401  1.54  knakahar  */
    402   1.7    itojun static int
    403  1.23     perry encap_remove(struct encaptab *ep)
    404   1.7    itojun {
    405   1.7    itojun 	struct radix_node_head *rnh = encap_rnh(ep->af);
    406   1.7    itojun 	int error = 0;
    407   1.7    itojun 
    408  1.54  knakahar 	KASSERT(rw_write_held(&encap_whole_lock));
    409  1.54  knakahar 
    410   1.7    itojun 	LIST_REMOVE(ep, chain);
    411   1.7    itojun 	if (!ep->func && rnh) {
    412  1.30  christos 		if (!rnh->rnh_deladdr((void *)ep->addrpack,
    413  1.30  christos 		    (void *)ep->maskpack, rnh))
    414   1.7    itojun 			error = ESRCH;
    415   1.7    itojun 	}
    416   1.7    itojun 	return error;
    417   1.7    itojun }
    418   1.7    itojun 
    419   1.7    itojun static int
    420  1.23     perry encap_afcheck(int af, const struct sockaddr *sp, const struct sockaddr *dp)
    421   1.7    itojun {
    422   1.7    itojun 	if (sp && dp) {
    423   1.7    itojun 		if (sp->sa_len != dp->sa_len)
    424   1.7    itojun 			return EINVAL;
    425   1.7    itojun 		if (af != sp->sa_family || af != dp->sa_family)
    426   1.7    itojun 			return EINVAL;
    427   1.7    itojun 	} else if (!sp && !dp)
    428   1.7    itojun 		;
    429   1.7    itojun 	else
    430   1.7    itojun 		return EINVAL;
    431   1.7    itojun 
    432   1.7    itojun 	switch (af) {
    433   1.7    itojun 	case AF_INET:
    434   1.7    itojun 		if (sp && sp->sa_len != sizeof(struct sockaddr_in))
    435   1.7    itojun 			return EINVAL;
    436   1.7    itojun 		if (dp && dp->sa_len != sizeof(struct sockaddr_in))
    437   1.7    itojun 			return EINVAL;
    438   1.7    itojun 		break;
    439   1.7    itojun #ifdef INET6
    440   1.7    itojun 	case AF_INET6:
    441   1.7    itojun 		if (sp && sp->sa_len != sizeof(struct sockaddr_in6))
    442   1.7    itojun 			return EINVAL;
    443   1.7    itojun 		if (dp && dp->sa_len != sizeof(struct sockaddr_in6))
    444   1.7    itojun 			return EINVAL;
    445   1.7    itojun 		break;
    446   1.7    itojun #endif
    447   1.7    itojun 	default:
    448   1.7    itojun 		return EAFNOSUPPORT;
    449   1.7    itojun 	}
    450   1.7    itojun 
    451   1.7    itojun 	return 0;
    452   1.1    itojun }
    453   1.1    itojun 
    454  1.54  knakahar static int
    455  1.54  knakahar encap_init_once(void)
    456  1.54  knakahar {
    457  1.54  knakahar 
    458  1.54  knakahar 	rw_init(&encap_whole_lock);
    459  1.54  knakahar 
    460  1.54  knakahar 	return 0;
    461  1.54  knakahar }
    462  1.54  knakahar 
    463   1.1    itojun /*
    464   1.1    itojun  * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
    465   1.1    itojun  * length of mask (sm and dm) is assumed to be same as sp/dp.
    466   1.1    itojun  * Return value will be necessary as input (cookie) for encap_detach().
    467   1.1    itojun  */
    468   1.1    itojun const struct encaptab *
    469  1.23     perry encap_attach(int af, int proto,
    470  1.23     perry     const struct sockaddr *sp, const struct sockaddr *sm,
    471  1.23     perry     const struct sockaddr *dp, const struct sockaddr *dm,
    472  1.51  knakahar     const struct encapsw *esw, void *arg)
    473   1.1    itojun {
    474   1.1    itojun 	struct encaptab *ep;
    475   1.1    itojun 	int error;
    476   1.1    itojun 	int s;
    477   1.7    itojun 	size_t l;
    478  1.33     pooka 	struct ip_pack4 *pack4;
    479   1.7    itojun #ifdef INET6
    480  1.33     pooka 	struct ip_pack6 *pack6;
    481   1.7    itojun #endif
    482   1.1    itojun 
    483  1.54  knakahar 	RUN_ONCE(&encap_init_control, encap_init_once);
    484  1.54  knakahar 
    485   1.1    itojun 	s = splsoftnet();
    486   1.1    itojun 	/* sanity check on args */
    487   1.7    itojun 	error = encap_afcheck(af, sp, dp);
    488   1.7    itojun 	if (error)
    489   1.1    itojun 		goto fail;
    490   1.1    itojun 
    491   1.1    itojun 	/* check if anyone have already attached with exactly same config */
    492  1.31    dyoung 	LIST_FOREACH(ep, &encaptab, chain) {
    493   1.1    itojun 		if (ep->af != af)
    494   1.1    itojun 			continue;
    495   1.1    itojun 		if (ep->proto != proto)
    496   1.1    itojun 			continue;
    497   1.7    itojun 		if (ep->func)
    498   1.7    itojun 			continue;
    499  1.41     ozaki 
    500  1.43  riastrad 		KASSERT(ep->src != NULL);
    501  1.43  riastrad 		KASSERT(ep->dst != NULL);
    502  1.43  riastrad 		KASSERT(ep->srcmask != NULL);
    503  1.43  riastrad 		KASSERT(ep->dstmask != NULL);
    504  1.41     ozaki 
    505   1.7    itojun 		if (ep->src->sa_len != sp->sa_len ||
    506  1.34    cegger 		    memcmp(ep->src, sp, sp->sa_len) != 0 ||
    507  1.34    cegger 		    memcmp(ep->srcmask, sm, sp->sa_len) != 0)
    508   1.7    itojun 			continue;
    509   1.7    itojun 		if (ep->dst->sa_len != dp->sa_len ||
    510  1.34    cegger 		    memcmp(ep->dst, dp, dp->sa_len) != 0 ||
    511  1.34    cegger 		    memcmp(ep->dstmask, dm, dp->sa_len) != 0)
    512   1.1    itojun 			continue;
    513   1.1    itojun 
    514   1.1    itojun 		error = EEXIST;
    515   1.1    itojun 		goto fail;
    516   1.1    itojun 	}
    517   1.3   thorpej 
    518   1.7    itojun 	switch (af) {
    519   1.7    itojun 	case AF_INET:
    520   1.7    itojun 		l = sizeof(*pack4);
    521   1.7    itojun 		break;
    522   1.7    itojun #ifdef INET6
    523   1.7    itojun 	case AF_INET6:
    524   1.7    itojun 		l = sizeof(*pack6);
    525   1.7    itojun 		break;
    526   1.7    itojun #endif
    527   1.7    itojun 	default:
    528   1.7    itojun 		goto fail;
    529   1.7    itojun 	}
    530   1.7    itojun 
    531  1.20    itojun 	/* M_NETADDR ok? */
    532  1.47  knakahar 	ep = kmem_zalloc(sizeof(*ep), KM_NOSLEEP);
    533   1.1    itojun 	if (ep == NULL) {
    534   1.1    itojun 		error = ENOBUFS;
    535   1.1    itojun 		goto fail;
    536   1.1    itojun 	}
    537  1.47  knakahar 	ep->addrpack = kmem_zalloc(l, KM_NOSLEEP);
    538   1.7    itojun 	if (ep->addrpack == NULL) {
    539   1.7    itojun 		error = ENOBUFS;
    540   1.7    itojun 		goto gc;
    541   1.7    itojun 	}
    542  1.47  knakahar 	ep->maskpack = kmem_zalloc(l, KM_NOSLEEP);
    543   1.7    itojun 	if (ep->maskpack == NULL) {
    544   1.7    itojun 		error = ENOBUFS;
    545   1.7    itojun 		goto gc;
    546   1.7    itojun 	}
    547   1.1    itojun 
    548   1.1    itojun 	ep->af = af;
    549   1.1    itojun 	ep->proto = proto;
    550   1.7    itojun 	ep->addrpack->sa_len = l & 0xff;
    551   1.7    itojun 	ep->maskpack->sa_len = l & 0xff;
    552   1.7    itojun 	switch (af) {
    553   1.7    itojun 	case AF_INET:
    554  1.33     pooka 		pack4 = (struct ip_pack4 *)ep->addrpack;
    555   1.7    itojun 		ep->src = (struct sockaddr *)&pack4->mine;
    556   1.7    itojun 		ep->dst = (struct sockaddr *)&pack4->yours;
    557  1.33     pooka 		pack4 = (struct ip_pack4 *)ep->maskpack;
    558   1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack4->mine;
    559   1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack4->yours;
    560   1.7    itojun 		break;
    561   1.7    itojun #ifdef INET6
    562   1.7    itojun 	case AF_INET6:
    563  1.33     pooka 		pack6 = (struct ip_pack6 *)ep->addrpack;
    564   1.7    itojun 		ep->src = (struct sockaddr *)&pack6->mine;
    565   1.7    itojun 		ep->dst = (struct sockaddr *)&pack6->yours;
    566  1.33     pooka 		pack6 = (struct ip_pack6 *)ep->maskpack;
    567   1.7    itojun 		ep->srcmask = (struct sockaddr *)&pack6->mine;
    568   1.7    itojun 		ep->dstmask = (struct sockaddr *)&pack6->yours;
    569   1.7    itojun 		break;
    570   1.7    itojun #endif
    571   1.7    itojun 	}
    572   1.7    itojun 
    573  1.37   tsutsui 	memcpy(ep->src, sp, sp->sa_len);
    574  1.37   tsutsui 	memcpy(ep->srcmask, sm, sp->sa_len);
    575  1.37   tsutsui 	memcpy(ep->dst, dp, dp->sa_len);
    576  1.37   tsutsui 	memcpy(ep->dstmask, dm, dp->sa_len);
    577  1.51  knakahar 	ep->esw = esw;
    578   1.1    itojun 	ep->arg = arg;
    579   1.1    itojun 
    580  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_WRITER);
    581   1.7    itojun 	error = encap_add(ep);
    582  1.54  knakahar 	rw_exit(&encap_whole_lock);
    583   1.7    itojun 	if (error)
    584   1.7    itojun 		goto gc;
    585   1.1    itojun 
    586   1.1    itojun 	error = 0;
    587   1.1    itojun 	splx(s);
    588   1.1    itojun 	return ep;
    589   1.1    itojun 
    590   1.7    itojun gc:
    591   1.7    itojun 	if (ep->addrpack)
    592  1.47  knakahar 		kmem_free(ep->addrpack, l);
    593   1.7    itojun 	if (ep->maskpack)
    594  1.47  knakahar 		kmem_free(ep->maskpack, l);
    595   1.7    itojun 	if (ep)
    596  1.47  knakahar 		kmem_free(ep, sizeof(*ep));
    597   1.1    itojun fail:
    598   1.1    itojun 	splx(s);
    599   1.1    itojun 	return NULL;
    600   1.1    itojun }
    601   1.1    itojun 
    602   1.1    itojun const struct encaptab *
    603  1.23     perry encap_attach_func(int af, int proto,
    604  1.26    martin     int (*func)(struct mbuf *, int, int, void *),
    605  1.51  knakahar     const struct encapsw *esw, void *arg)
    606   1.1    itojun {
    607   1.1    itojun 	struct encaptab *ep;
    608   1.1    itojun 	int error;
    609   1.1    itojun 	int s;
    610   1.1    itojun 
    611  1.54  knakahar 	RUN_ONCE(&encap_init_control, encap_init_once);
    612  1.54  knakahar 
    613   1.1    itojun 	s = splsoftnet();
    614   1.1    itojun 	/* sanity check on args */
    615   1.1    itojun 	if (!func) {
    616   1.1    itojun 		error = EINVAL;
    617   1.1    itojun 		goto fail;
    618   1.1    itojun 	}
    619   1.1    itojun 
    620   1.7    itojun 	error = encap_afcheck(af, NULL, NULL);
    621   1.7    itojun 	if (error)
    622   1.7    itojun 		goto fail;
    623   1.7    itojun 
    624  1.47  knakahar 	ep = kmem_alloc(sizeof(*ep), KM_NOSLEEP);	/*XXX*/
    625   1.1    itojun 	if (ep == NULL) {
    626   1.1    itojun 		error = ENOBUFS;
    627   1.1    itojun 		goto fail;
    628   1.1    itojun 	}
    629  1.35    cegger 	memset(ep, 0, sizeof(*ep));
    630   1.1    itojun 
    631   1.1    itojun 	ep->af = af;
    632   1.1    itojun 	ep->proto = proto;
    633   1.1    itojun 	ep->func = func;
    634  1.51  knakahar 	ep->esw = esw;
    635   1.1    itojun 	ep->arg = arg;
    636   1.1    itojun 
    637  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_WRITER);
    638   1.7    itojun 	error = encap_add(ep);
    639  1.54  knakahar 	rw_exit(&encap_whole_lock);
    640   1.7    itojun 	if (error)
    641   1.7    itojun 		goto fail;
    642   1.1    itojun 
    643   1.1    itojun 	error = 0;
    644   1.1    itojun 	splx(s);
    645   1.1    itojun 	return ep;
    646   1.1    itojun 
    647   1.1    itojun fail:
    648   1.1    itojun 	splx(s);
    649   1.1    itojun 	return NULL;
    650   1.1    itojun }
    651   1.1    itojun 
    652   1.7    itojun /* XXX encap4_ctlinput() is necessary if we set DF=1 on outer IPv4 header */
    653   1.7    itojun 
    654   1.7    itojun #ifdef INET6
    655  1.32        ad void *
    656  1.29    dyoung encap6_ctlinput(int cmd, const struct sockaddr *sa, void *d0)
    657   1.7    itojun {
    658   1.7    itojun 	void *d = d0;
    659   1.7    itojun 	struct ip6_hdr *ip6;
    660   1.7    itojun 	struct mbuf *m;
    661   1.7    itojun 	int off;
    662   1.7    itojun 	struct ip6ctlparam *ip6cp = NULL;
    663   1.7    itojun 	int nxt;
    664   1.7    itojun 	struct encaptab *ep;
    665  1.51  knakahar 	const struct encapsw *esw;
    666   1.7    itojun 
    667   1.7    itojun 	if (sa->sa_family != AF_INET6 ||
    668   1.7    itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    669  1.32        ad 		return NULL;
    670   1.7    itojun 
    671   1.7    itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    672  1.32        ad 		return NULL;
    673   1.7    itojun 	if (cmd == PRC_HOSTDEAD)
    674   1.7    itojun 		d = NULL;
    675   1.7    itojun 	else if (cmd == PRC_MSGSIZE)
    676   1.7    itojun 		; /* special code is present, see below */
    677   1.7    itojun 	else if (inet6ctlerrmap[cmd] == 0)
    678  1.32        ad 		return NULL;
    679   1.7    itojun 
    680   1.7    itojun 	/* if the parameter is from icmp6, decode it. */
    681   1.7    itojun 	if (d != NULL) {
    682   1.7    itojun 		ip6cp = (struct ip6ctlparam *)d;
    683   1.7    itojun 		m = ip6cp->ip6c_m;
    684   1.7    itojun 		ip6 = ip6cp->ip6c_ip6;
    685   1.7    itojun 		off = ip6cp->ip6c_off;
    686   1.7    itojun 		nxt = ip6cp->ip6c_nxt;
    687  1.15   mycroft 
    688  1.15   mycroft 		if (ip6 && cmd == PRC_MSGSIZE) {
    689  1.15   mycroft 			int valid = 0;
    690  1.15   mycroft 			struct encaptab *match;
    691  1.15   mycroft 
    692  1.15   mycroft 			/*
    693  1.15   mycroft 		 	* Check to see if we have a valid encap configuration.
    694  1.15   mycroft 		 	*/
    695  1.54  knakahar 			rw_enter(&encap_whole_lock, RW_READER);
    696  1.15   mycroft 			match = encap6_lookup(m, off, nxt, OUTBOUND);
    697  1.15   mycroft 			if (match)
    698  1.15   mycroft 				valid++;
    699  1.54  knakahar 			rw_exit(&encap_whole_lock);
    700  1.15   mycroft 
    701  1.15   mycroft 			/*
    702  1.15   mycroft 		 	* Depending on the value of "valid" and routing table
    703  1.15   mycroft 		 	* size (mtudisc_{hi,lo}wat), we will:
    704  1.15   mycroft 		 	* - recalcurate the new MTU and create the
    705  1.15   mycroft 		 	*   corresponding routing entry, or
    706  1.15   mycroft 		 	* - ignore the MTU change notification.
    707  1.15   mycroft 		 	*/
    708  1.15   mycroft 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    709  1.15   mycroft 		}
    710   1.7    itojun 	} else {
    711   1.7    itojun 		m = NULL;
    712   1.7    itojun 		ip6 = NULL;
    713   1.7    itojun 		nxt = -1;
    714   1.7    itojun 	}
    715   1.7    itojun 
    716   1.7    itojun 	/* inform all listeners */
    717  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_READER);
    718  1.31    dyoung 	LIST_FOREACH(ep, &encaptab, chain) {
    719   1.7    itojun 		if (ep->af != AF_INET6)
    720   1.7    itojun 			continue;
    721   1.7    itojun 		if (ep->proto >= 0 && ep->proto != nxt)
    722   1.7    itojun 			continue;
    723   1.7    itojun 
    724   1.7    itojun 		/* should optimize by looking at address pairs */
    725   1.7    itojun 
    726   1.7    itojun 		/* XXX need to pass ep->arg or ep itself to listeners */
    727  1.51  knakahar 		esw = ep->esw;
    728  1.51  knakahar 		if (esw && esw->encapsw6.pr_ctlinput) {
    729  1.52  knakahar 			(*esw->encapsw6.pr_ctlinput)(cmd, sa, d, ep->arg);
    730  1.51  knakahar 		}
    731   1.7    itojun 	}
    732  1.54  knakahar 	rw_exit(&encap_whole_lock);
    733   1.7    itojun 
    734   1.7    itojun 	rip6_ctlinput(cmd, sa, d0);
    735  1.32        ad 	return NULL;
    736   1.7    itojun }
    737   1.7    itojun #endif
    738   1.7    itojun 
    739   1.1    itojun int
    740  1.23     perry encap_detach(const struct encaptab *cookie)
    741   1.1    itojun {
    742   1.1    itojun 	const struct encaptab *ep = cookie;
    743  1.42     ozaki 	struct encaptab *p, *np;
    744   1.7    itojun 	int error;
    745   1.1    itojun 
    746  1.54  knakahar 	rw_enter(&encap_whole_lock, RW_WRITER);
    747  1.42     ozaki 	LIST_FOREACH_SAFE(p, &encaptab, chain, np) {
    748   1.1    itojun 		if (p == ep) {
    749   1.7    itojun 			error = encap_remove(p);
    750  1.54  knakahar 			rw_exit(&encap_whole_lock);
    751   1.7    itojun 			if (error)
    752   1.7    itojun 				return error;
    753  1.54  knakahar 
    754   1.7    itojun 			if (!ep->func) {
    755  1.47  knakahar 				kmem_free(p->addrpack, ep->addrpack->sa_len);
    756  1.47  knakahar 				kmem_free(p->maskpack, ep->maskpack->sa_len);
    757   1.7    itojun 			}
    758  1.47  knakahar 			kmem_free(p, sizeof(*p));	/*XXX*/
    759   1.1    itojun 			return 0;
    760   1.1    itojun 		}
    761   1.1    itojun 	}
    762  1.54  knakahar 	rw_exit(&encap_whole_lock);
    763   1.1    itojun 
    764   1.7    itojun 	return ENOENT;
    765   1.7    itojun }
    766   1.7    itojun 
    767   1.7    itojun static struct radix_node_head *
    768  1.23     perry encap_rnh(int af)
    769   1.7    itojun {
    770   1.7    itojun 
    771   1.7    itojun 	switch (af) {
    772   1.7    itojun 	case AF_INET:
    773   1.7    itojun 		return encap_head[0];
    774   1.7    itojun #ifdef INET6
    775   1.7    itojun 	case AF_INET6:
    776   1.7    itojun 		return encap_head[1];
    777   1.7    itojun #endif
    778   1.7    itojun 	default:
    779   1.7    itojun 		return NULL;
    780   1.7    itojun 	}
    781   1.7    itojun }
    782   1.7    itojun 
    783   1.7    itojun static int
    784  1.23     perry mask_matchlen(const struct sockaddr *sa)
    785   1.7    itojun {
    786   1.7    itojun 	const char *p, *ep;
    787   1.7    itojun 	int l;
    788   1.7    itojun 
    789   1.7    itojun 	p = (const char *)sa;
    790   1.7    itojun 	ep = p + sa->sa_len;
    791   1.7    itojun 	p += 2;	/* sa_len + sa_family */
    792   1.7    itojun 
    793   1.7    itojun 	l = 0;
    794   1.7    itojun 	while (p < ep) {
    795   1.7    itojun 		l += (*p ? 8 : 0);	/* estimate */
    796   1.7    itojun 		p++;
    797   1.7    itojun 	}
    798   1.7    itojun 	return l;
    799   1.1    itojun }
    800   1.1    itojun 
    801   1.1    itojun static void
    802  1.23     perry encap_fillarg(struct mbuf *m, const struct encaptab *ep)
    803   1.1    itojun {
    804  1.12    itojun 	struct m_tag *mtag;
    805   1.1    itojun 
    806  1.54  knakahar 	KASSERT(rw_read_held(&encap_whole_lock));
    807  1.54  knakahar 
    808  1.12    itojun 	mtag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT);
    809  1.12    itojun 	if (mtag) {
    810  1.12    itojun 		*(void **)(mtag + 1) = ep->arg;
    811  1.12    itojun 		m_tag_prepend(m, mtag);
    812   1.1    itojun 	}
    813   1.1    itojun }
    814   1.1    itojun 
    815   1.1    itojun void *
    816  1.23     perry encap_getarg(struct mbuf *m)
    817   1.1    itojun {
    818   1.1    itojun 	void *p;
    819  1.12    itojun 	struct m_tag *mtag;
    820   1.1    itojun 
    821   1.1    itojun 	p = NULL;
    822  1.12    itojun 	mtag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
    823  1.12    itojun 	if (mtag != NULL) {
    824  1.13    itojun 		p = *(void **)(mtag + 1);
    825  1.12    itojun 		m_tag_delete(m, mtag);
    826   1.1    itojun 	}
    827   1.1    itojun 	return p;
    828   1.1    itojun }
    829  1.54  knakahar 
    830  1.54  knakahar void
    831  1.54  knakahar encap_lock_enter(void)
    832  1.54  knakahar {
    833  1.54  knakahar 
    834  1.54  knakahar 	/* XXX future work
    835  1.54  knakahar 	 * change interruptable lock.
    836  1.54  knakahar 	 */
    837  1.54  knakahar 	KERNEL_LOCK(1, NULL);
    838  1.54  knakahar }
    839  1.54  knakahar 
    840  1.54  knakahar void
    841  1.54  knakahar encap_lock_exit(void)
    842  1.54  knakahar {
    843  1.54  knakahar 
    844  1.54  knakahar 	/* XXX future work
    845  1.54  knakahar 	 * change interruptable lock
    846  1.54  knakahar 	 */
    847  1.54  knakahar 	KERNEL_UNLOCK_ONE(NULL);
    848  1.54  knakahar }
    849