1 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * My grandfather said that there's a devil inside tunnelling technology...
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 * 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.
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.
57 /* XXX is M_NETADDR correct? */
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include "opt_mrouting.h"
64 #include "opt_inet6.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/socket.h>
69 #include <sys/sockio.h>
71 #include <sys/errno.h>
72 #include <sys/protosw.h>
73 #include <sys/queue.h>
76 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet/ip_encap.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet6/ip6protosw.h>
90 #include <machine/stdarg.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
94 static MALLOC_DEFINE(M_NETADDR, "encap_export_host", "Export host address structure");
96 static void encap_add(struct encaptab *);
97 static int mask_match(const struct encaptab *, const struct sockaddr *,
98 const struct sockaddr *);
99 static void encap_fillarg(struct mbuf *, const struct encaptab *);
102 * All global variables in ip_encap.c are locked using encapmtx.
104 static struct mtx encapmtx;
105 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
106 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(encaptab);
109 * We currently keey encap_init() for source code compatibility reasons --
110 * it's referenced by KAME pieces in netinet6.
119 encap4_input(struct mbuf *m, int off)
123 struct sockaddr_in s, d;
124 const struct protosw *psw;
125 struct encaptab *ep, *match;
128 ip = mtod(m, struct ip *);
131 bzero(&s, sizeof(s));
132 s.sin_family = AF_INET;
133 s.sin_len = sizeof(struct sockaddr_in);
134 s.sin_addr = ip->ip_src;
135 bzero(&d, sizeof(d));
136 d.sin_family = AF_INET;
137 d.sin_len = sizeof(struct sockaddr_in);
138 d.sin_addr = ip->ip_dst;
143 LIST_FOREACH(ep, &encaptab, chain) {
144 if (ep->af != AF_INET)
146 if (ep->proto >= 0 && ep->proto != proto)
149 prio = (*ep->func)(m, off, proto, ep->arg);
152 * it's inbound traffic, we need to match in reverse
155 prio = mask_match(ep, (struct sockaddr *)&d,
156 (struct sockaddr *)&s);
160 * We prioritize the matches by using bit length of the
161 * matches. mask_match() and user-supplied matching function
162 * should return the bit length of the matches (for example,
163 * if both src/dst are matched for IPv4, 64 should be returned).
164 * 0 or negative return value means "it did not match".
166 * The question is, since we have two "mask" portion, we
167 * cannot really define total order between entries.
168 * For example, which of these should be preferred?
169 * mask_match() returns 48 (32 + 16) for both of them.
170 * src=3ffe::/16, dst=3ffe:501::/32
171 * src=3ffe:501::/32, dst=3ffe::/16
173 * We need to loop through all the possible candidates
174 * to get the best match - the search takes O(n) for
175 * n attachments (i.e. interfaces).
179 if (prio > matchprio) {
184 mtx_unlock(&encapmtx);
187 /* found a match, "match" has the best one */
189 if (psw && psw->pr_input) {
190 encap_fillarg(m, match);
191 (*psw->pr_input)(m, off);
197 /* last resort: inject to raw socket */
204 encap6_input(struct mbuf **mp, int *offp, int proto)
206 struct mbuf *m = *mp;
208 struct sockaddr_in6 s, d;
209 const struct ip6protosw *psw;
210 struct encaptab *ep, *match;
213 ip6 = mtod(m, struct ip6_hdr *);
215 bzero(&s, sizeof(s));
216 s.sin6_family = AF_INET6;
217 s.sin6_len = sizeof(struct sockaddr_in6);
218 s.sin6_addr = ip6->ip6_src;
219 bzero(&d, sizeof(d));
220 d.sin6_family = AF_INET6;
221 d.sin6_len = sizeof(struct sockaddr_in6);
222 d.sin6_addr = ip6->ip6_dst;
227 LIST_FOREACH(ep, &encaptab, chain) {
228 if (ep->af != AF_INET6)
230 if (ep->proto >= 0 && ep->proto != proto)
233 prio = (*ep->func)(m, *offp, proto, ep->arg);
236 * it's inbound traffic, we need to match in reverse
239 prio = mask_match(ep, (struct sockaddr *)&d,
240 (struct sockaddr *)&s);
243 /* see encap4_input() for issues here */
246 if (prio > matchprio) {
251 mtx_unlock(&encapmtx);
255 psw = (const struct ip6protosw *)match->psw;
256 if (psw && psw->pr_input) {
257 encap_fillarg(m, match);
258 return (*psw->pr_input)(mp, offp, proto);
265 /* last resort: inject to raw socket */
266 return rip6_input(mp, offp, proto);
270 /*lint -sem(encap_add, custodial(1)) */
272 encap_add(struct encaptab *ep)
275 mtx_assert(&encapmtx, MA_OWNED);
276 LIST_INSERT_HEAD(&encaptab, ep, chain);
280 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
281 * length of mask (sm and dm) is assumed to be same as sp/dp.
282 * Return value will be necessary as input (cookie) for encap_detach().
284 const struct encaptab *
285 encap_attach(int af, int proto, const struct sockaddr *sp,
286 const struct sockaddr *sm, const struct sockaddr *dp,
287 const struct sockaddr *dm, const struct protosw *psw, void *arg)
291 /* sanity check on args */
292 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst))
294 if (sp->sa_len != dp->sa_len)
296 if (af != sp->sa_family || af != dp->sa_family)
299 /* check if anyone have already attached with exactly same config */
301 LIST_FOREACH(ep, &encaptab, chain) {
304 if (ep->proto != proto)
306 if (ep->src.ss_len != sp->sa_len ||
307 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
308 bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
310 if (ep->dst.ss_len != dp->sa_len ||
311 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
312 bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
315 mtx_unlock(&encapmtx);
319 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
321 mtx_unlock(&encapmtx);
324 bzero(ep, sizeof(*ep));
328 bcopy(sp, &ep->src, sp->sa_len);
329 bcopy(sm, &ep->srcmask, sp->sa_len);
330 bcopy(dp, &ep->dst, dp->sa_len);
331 bcopy(dm, &ep->dstmask, dp->sa_len);
336 mtx_unlock(&encapmtx);
340 const struct encaptab *
341 encap_attach_func(int af, int proto,
342 int (*func)(const struct mbuf *, int, int, void *),
343 const struct protosw *psw, void *arg)
347 /* sanity check on args */
351 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
354 bzero(ep, sizeof(*ep));
364 mtx_unlock(&encapmtx);
369 encap_detach(const struct encaptab *cookie)
371 const struct encaptab *ep = cookie;
375 LIST_FOREACH(p, &encaptab, chain) {
377 LIST_REMOVE(p, chain);
378 mtx_unlock(&encapmtx);
379 free(p, M_NETADDR); /*XXX*/
383 mtx_unlock(&encapmtx);
389 mask_match(const struct encaptab *ep, const struct sockaddr *sp,
390 const struct sockaddr *dp)
392 struct sockaddr_storage s;
393 struct sockaddr_storage d;
395 const u_int8_t *p, *q;
399 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
401 if (sp->sa_family != ep->af || dp->sa_family != ep->af)
403 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
408 p = (const u_int8_t *)sp;
409 q = (const u_int8_t *)&ep->srcmask;
411 for (i = 0 ; i < sp->sa_len; i++) {
414 matchlen += (q[i] ? 8 : 0);
417 p = (const u_int8_t *)dp;
418 q = (const u_int8_t *)&ep->dstmask;
420 for (i = 0 ; i < dp->sa_len; i++) {
422 /* XXX rough estimate */
423 matchlen += (q[i] ? 8 : 0);
426 /* need to overwrite len/family portion as we don't compare them */
427 s.ss_len = sp->sa_len;
428 s.ss_family = sp->sa_family;
429 d.ss_len = dp->sa_len;
430 d.ss_family = dp->sa_family;
432 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
433 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
440 encap_fillarg(struct mbuf *m, const struct encaptab *ep)
444 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof (void*), M_NOWAIT);
446 *(void**)(tag+1) = ep->arg;
447 m_tag_prepend(m, tag);
452 encap_getarg(struct mbuf *m)
457 tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL);
459 p = *(void**)(tag+1);
460 m_tag_delete(m, tag);