2 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 * My grandfather said that there's a devil inside tunnelling technology...
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 * 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 "opt_mrouting.h"
61 #include "opt_inet6.h"
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/socket.h>
66 #include <sys/sockio.h>
68 #include <sys/errno.h>
69 #include <sys/protosw.h>
70 #include <sys/queue.h>
73 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/ip_encap.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/ip6protosw.h>
87 #include <machine/stdarg.h>
89 #include <net/net_osdep.h>
91 #include <sys/kernel.h>
92 #include <sys/malloc.h>
93 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
95 static void encap_add(struct encaptab *);
96 static int mask_match(const struct encaptab *, const struct sockaddr *,
97 const struct sockaddr *);
98 static void encap_fillarg(struct mbuf *, const struct encaptab *);
100 #ifndef LIST_HEAD_INITIALIZER
101 /* rely upon BSS initialization */
102 LIST_HEAD(, encaptab) encaptab;
104 LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
110 static int initialized = 0;
117 * we cannot use LIST_INIT() here, since drivers may want to call
118 * encap_attach(), on driver attach. encap_init() will be called
119 * on AF_INET{,6} initialization, which happens after driver
120 * initialization - using LIST_INIT() here can nuke encap_attach()
123 LIST_INIT(&encaptab);
135 struct sockaddr_in s, d;
136 const struct protosw *psw;
137 struct encaptab *ep, *match;
140 ip = mtod(m, struct ip *);
143 bzero(&s, sizeof(s));
144 s.sin_family = AF_INET;
145 s.sin_len = sizeof(struct sockaddr_in);
146 s.sin_addr = ip->ip_src;
147 bzero(&d, sizeof(d));
148 d.sin_family = AF_INET;
149 d.sin_len = sizeof(struct sockaddr_in);
150 d.sin_addr = ip->ip_dst;
154 LIST_FOREACH(ep, &encaptab, chain) {
155 if (ep->af != AF_INET)
157 if (ep->proto >= 0 && ep->proto != proto)
160 prio = (*ep->func)(m, off, proto, ep->arg);
163 * it's inbound traffic, we need to match in reverse
166 prio = mask_match(ep, (struct sockaddr *)&d,
167 (struct sockaddr *)&s);
171 * We prioritize the matches by using bit length of the
172 * matches. mask_match() and user-supplied matching function
173 * should return the bit length of the matches (for example,
174 * if both src/dst are matched for IPv4, 64 should be returned).
175 * 0 or negative return value means "it did not match".
177 * The question is, since we have two "mask" portion, we
178 * cannot really define total order between entries.
179 * For example, which of these should be preferred?
180 * mask_match() returns 48 (32 + 16) for both of them.
181 * src=3ffe::/16, dst=3ffe:501::/32
182 * src=3ffe:501::/32, dst=3ffe::/16
184 * We need to loop through all the possible candidates
185 * to get the best match - the search takes O(n) for
186 * n attachments (i.e. interfaces).
190 if (prio > matchprio) {
197 /* found a match, "match" has the best one */
199 if (psw && psw->pr_input) {
200 encap_fillarg(m, match);
201 (*psw->pr_input)(m, off);
207 /* last resort: inject to raw socket */
214 encap6_input(mp, offp, proto)
219 struct mbuf *m = *mp;
221 struct sockaddr_in6 s, d;
222 const struct ip6protosw *psw;
223 struct encaptab *ep, *match;
226 ip6 = mtod(m, struct ip6_hdr *);
228 bzero(&s, sizeof(s));
229 s.sin6_family = AF_INET6;
230 s.sin6_len = sizeof(struct sockaddr_in6);
231 s.sin6_addr = ip6->ip6_src;
232 bzero(&d, sizeof(d));
233 d.sin6_family = AF_INET6;
234 d.sin6_len = sizeof(struct sockaddr_in6);
235 d.sin6_addr = ip6->ip6_dst;
239 LIST_FOREACH(ep, &encaptab, chain) {
240 if (ep->af != AF_INET6)
242 if (ep->proto >= 0 && ep->proto != proto)
245 prio = (*ep->func)(m, *offp, proto, ep->arg);
248 * it's inbound traffic, we need to match in reverse
251 prio = mask_match(ep, (struct sockaddr *)&d,
252 (struct sockaddr *)&s);
255 /* see encap4_input() for issues here */
258 if (prio > matchprio) {
266 psw = (const struct ip6protosw *)match->psw;
267 if (psw && psw->pr_input) {
268 encap_fillarg(m, match);
269 return (*psw->pr_input)(mp, offp, proto);
276 /* last resort: inject to raw socket */
277 return rip6_input(mp, offp, proto);
286 LIST_INSERT_HEAD(&encaptab, ep, chain);
290 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
291 * length of mask (sm and dm) is assumed to be same as sp/dp.
292 * Return value will be necessary as input (cookie) for encap_detach().
294 const struct encaptab *
295 encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
298 const struct sockaddr *sp, *sm;
299 const struct sockaddr *dp, *dm;
300 const struct protosw *psw;
308 /* sanity check on args */
309 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
313 if (sp->sa_len != dp->sa_len) {
317 if (af != sp->sa_family || af != dp->sa_family) {
322 /* check if anyone have already attached with exactly same config */
323 LIST_FOREACH(ep, &encaptab, chain) {
326 if (ep->proto != proto)
328 if (ep->src.ss_len != sp->sa_len ||
329 bcmp(&ep->src, sp, sp->sa_len) != 0 ||
330 bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
332 if (ep->dst.ss_len != dp->sa_len ||
333 bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
334 bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
341 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
346 bzero(ep, sizeof(*ep));
350 bcopy(sp, &ep->src, sp->sa_len);
351 bcopy(sm, &ep->srcmask, sp->sa_len);
352 bcopy(dp, &ep->dst, dp->sa_len);
353 bcopy(dm, &ep->dstmask, dp->sa_len);
368 const struct encaptab *
369 encap_attach_func(af, proto, func, psw, arg)
372 int (*func)(const struct mbuf *, int, int, void *);
373 const struct protosw *psw;
381 /* sanity check on args */
387 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/
392 bzero(ep, sizeof(*ep));
413 const struct encaptab *cookie;
415 const struct encaptab *ep = cookie;
418 LIST_FOREACH(p, &encaptab, chain) {
420 LIST_REMOVE(p, chain);
421 free(p, M_NETADDR); /*XXX*/
430 mask_match(ep, sp, dp)
431 const struct encaptab *ep;
432 const struct sockaddr *sp;
433 const struct sockaddr *dp;
435 struct sockaddr_storage s;
436 struct sockaddr_storage d;
438 const u_int8_t *p, *q;
442 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
444 if (sp->sa_family != ep->af || dp->sa_family != ep->af)
446 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
451 p = (const u_int8_t *)sp;
452 q = (const u_int8_t *)&ep->srcmask;
454 for (i = 0 ; i < sp->sa_len; i++) {
457 matchlen += (q[i] ? 8 : 0);
460 p = (const u_int8_t *)dp;
461 q = (const u_int8_t *)&ep->dstmask;
463 for (i = 0 ; i < dp->sa_len; i++) {
465 /* XXX rough estimate */
466 matchlen += (q[i] ? 8 : 0);
469 /* need to overwrite len/family portion as we don't compare them */
470 s.ss_len = sp->sa_len;
471 s.ss_family = sp->sa_family;
472 d.ss_len = dp->sa_len;
473 d.ss_family = dp->sa_family;
475 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
476 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
485 const struct encaptab *ep;
488 m->m_pkthdr.aux = ep->arg;
492 n = m_aux_add(m, AF_INET, IPPROTO_IPV4);
494 *mtod(n, void **) = ep->arg;
495 n->m_len = sizeof(void *);
507 m->m_pkthdr.aux = NULL;
513 n = m_aux_find(m, AF_INET, IPPROTO_IPV4);
515 if (n->m_len == sizeof(void *))
516 p = *mtod(n, void **);