1 /* $FreeBSD: src/sys/netinet/ip_encap.c,v 1.1.2.5 2003/01/23 21:06:45 sam Exp $ */
2 /* $DragonFly: src/sys/netinet/ip_encap.c,v 1.15 2006/09/05 00:55:48 dillon Exp $ */
3 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * My grandfather said that there's a devil inside tunnelling technology...
36 * We have surprisingly many protocols that want packets with IP protocol
37 * #4 or #41. Here's a list of protocols that want protocol #41:
38 * RFC1933 configured tunnel
39 * RFC1933 automatic tunnel
40 * RFC2401 IPsec tunnel
41 * RFC2473 IPv6 generic packet tunnelling
42 * RFC2529 6over4 tunnel
43 * mobile-ip6 (uses RFC2473)
46 * Here's a list of protocol that want protocol #4:
47 * RFC1853 IPv4-in-IPv4 tunnelling
48 * RFC2003 IPv4 encapsulation within IPv4
49 * RFC2344 reverse tunnelling for mobile-ip4
50 * RFC2401 IPsec tunnel
51 * Well, what can I say. They impose different en/decapsulation mechanism
52 * from each other, so they need separate protocol handler. The only one
53 * we can easily determine by protocol # is IPsec, which always has
54 * AH/ESP/IPComp header right after outer IP header.
56 * So, clearly good old protosw does not work for protocol #4 and #41.
57 * The code will let you match protocol via src/dst address pair.
59 /* XXX is M_NETADDR correct? */
62 #include "opt_inet6.h"
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
69 #include <sys/errno.h>
70 #include <sys/protosw.h>
71 #include <sys/queue.h>
74 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_var.h>
80 #include <netinet/ip_encap.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet6/ip6protosw.h>
88 #include <machine/stdarg.h>
90 #include <net/net_osdep.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
94 #include <sys/thread2.h>
95 MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
97 static void encap_add (struct encaptab
*);
98 static int mask_match (const struct encaptab
*, const struct sockaddr
*,
99 const struct sockaddr
*);
100 static void encap_fillarg (struct mbuf
*, const struct encaptab
*);
102 #ifndef LIST_HEAD_INITIALIZER
103 /* rely upon BSS initialization */
104 LIST_HEAD(, encaptab
) encaptab
;
106 LIST_HEAD(, encaptab
) encaptab
= LIST_HEAD_INITIALIZER(&encaptab
);
109 void (*ipip_input
)(struct mbuf
*, int, int); /* hook for mrouting */
114 static int initialized
= 0;
121 * we cannot use LIST_INIT() here, since drivers may want to call
122 * encap_attach(), on driver attach. encap_init() will be called
123 * on AF_INET{,6} initialization, which happens after driver
124 * initialization - using LIST_INIT() here can nuke encap_attach()
127 LIST_INIT(&encaptab
);
133 encap4_input(struct mbuf
*m
, ...)
137 struct sockaddr_in s
, d
;
138 const struct protosw
*psw
;
139 struct encaptab
*ep
, *match
;
144 off
= __va_arg(ap
, int);
145 proto
= __va_arg(ap
, int);
148 ip
= mtod(m
, struct ip
*);
151 s
.sin_family
= AF_INET
;
152 s
.sin_len
= sizeof(struct sockaddr_in
);
153 s
.sin_addr
= ip
->ip_src
;
155 d
.sin_family
= AF_INET
;
156 d
.sin_len
= sizeof(struct sockaddr_in
);
157 d
.sin_addr
= ip
->ip_dst
;
161 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
162 if (ep
->af
!= AF_INET
)
164 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
167 prio
= (*ep
->func
)(m
, off
, proto
, ep
->arg
);
170 * it's inbound traffic, we need to match in reverse
173 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
174 (struct sockaddr
*)&s
);
178 * We prioritize the matches by using bit length of the
179 * matches. mask_match() and user-supplied matching function
180 * should return the bit length of the matches (for example,
181 * if both src/dst are matched for IPv4, 64 should be returned).
182 * 0 or negative return value means "it did not match".
184 * The question is, since we have two "mask" portion, we
185 * cannot really define total order between entries.
186 * For example, which of these should be preferred?
187 * mask_match() returns 48 (32 + 16) for both of them.
188 * src=3ffe::/16, dst=3ffe:501::/32
189 * src=3ffe:501::/32, dst=3ffe::/16
191 * We need to loop through all the possible candidates
192 * to get the best match - the search takes O(n) for
193 * n attachments (i.e. interfaces).
197 if (prio
> matchprio
) {
204 /* found a match, "match" has the best one */
206 if (psw
&& psw
->pr_input
) {
207 encap_fillarg(m
, match
);
208 (*psw
->pr_input
)(m
, off
, proto
);
214 /* for backward compatibility */
215 if (proto
== IPPROTO_IPV4
&& ipip_input
) {
216 ipip_input(m
, off
, proto
);
220 /* last resort: inject to raw socket */
221 rip_input(m
, off
, proto
);
227 encap6_input(struct mbuf
**mp
, int *offp
, int proto
)
229 struct mbuf
*m
= *mp
;
231 struct sockaddr_in6 s
, d
;
232 const struct ip6protosw
*psw
;
233 struct encaptab
*ep
, *match
;
236 ip6
= mtod(m
, struct ip6_hdr
*);
239 s
.sin6_family
= AF_INET6
;
240 s
.sin6_len
= sizeof(struct sockaddr_in6
);
241 s
.sin6_addr
= ip6
->ip6_src
;
243 d
.sin6_family
= AF_INET6
;
244 d
.sin6_len
= sizeof(struct sockaddr_in6
);
245 d
.sin6_addr
= ip6
->ip6_dst
;
249 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
250 if (ep
->af
!= AF_INET6
)
252 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
255 prio
= (*ep
->func
)(m
, *offp
, proto
, ep
->arg
);
258 * it's inbound traffic, we need to match in reverse
261 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
262 (struct sockaddr
*)&s
);
265 /* see encap4_input() for issues here */
268 if (prio
> matchprio
) {
276 psw
= (const struct ip6protosw
*)match
->psw
;
277 if (psw
&& psw
->pr_input
) {
278 encap_fillarg(m
, match
);
279 return (*psw
->pr_input
)(mp
, offp
, proto
);
286 /* last resort: inject to raw socket */
287 return rip6_input(mp
, offp
, proto
);
292 encap_add(struct encaptab
*ep
)
295 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
299 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
300 * length of mask (sm and dm) is assumed to be same as sp/dp.
301 * Return value will be necessary as input (cookie) for encap_detach().
303 const struct encaptab
*
304 encap_attach(int af
, int proto
, const struct sockaddr
*sp
,
305 const struct sockaddr
*sm
, const struct sockaddr
*dp
,
306 const struct sockaddr
*dm
, const struct protosw
*psw
, void *arg
)
312 /* sanity check on args */
313 if (sp
->sa_len
> sizeof ep
->src
|| dp
->sa_len
> sizeof ep
->dst
) {
317 if (sp
->sa_len
!= dp
->sa_len
) {
321 if (af
!= sp
->sa_family
|| af
!= dp
->sa_family
) {
326 /* check if anyone have already attached with exactly same config */
327 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
330 if (ep
->proto
!= proto
)
332 if (ep
->src
.ss_len
!= sp
->sa_len
||
333 bcmp(&ep
->src
, sp
, sp
->sa_len
) != 0 ||
334 bcmp(&ep
->srcmask
, sm
, sp
->sa_len
) != 0)
336 if (ep
->dst
.ss_len
!= dp
->sa_len
||
337 bcmp(&ep
->dst
, dp
, dp
->sa_len
) != 0 ||
338 bcmp(&ep
->dstmask
, dm
, dp
->sa_len
) != 0)
345 ep
= kmalloc(sizeof *ep
, M_NETADDR
, M_INTWAIT
| M_ZERO
| M_NULLOK
);
353 bcopy(sp
, &ep
->src
, sp
->sa_len
);
354 bcopy(sm
, &ep
->srcmask
, sp
->sa_len
);
355 bcopy(dp
, &ep
->dst
, dp
->sa_len
);
356 bcopy(dm
, &ep
->dstmask
, dp
->sa_len
);
371 const struct encaptab
*
372 encap_attach_func(int af
, int proto
,
373 int (*func
)(const struct mbuf
*, int, int, void *),
374 const struct protosw
*psw
, void *arg
)
380 /* sanity check on args */
386 ep
= kmalloc(sizeof *ep
, M_NETADDR
, M_INTWAIT
| M_ZERO
| M_NULLOK
);
410 encap_detach(const struct encaptab
*cookie
)
412 const struct encaptab
*ep
= cookie
;
415 for (p
= LIST_FIRST(&encaptab
); p
; p
= LIST_NEXT(p
, chain
)) {
417 LIST_REMOVE(p
, chain
);
418 kfree(p
, M_NETADDR
); /*XXX*/
427 mask_match(const struct encaptab
*ep
, const struct sockaddr
*sp
,
428 const struct sockaddr
*dp
)
430 struct sockaddr_storage s
;
431 struct sockaddr_storage d
;
433 const u_int8_t
*p
, *q
;
437 if (sp
->sa_len
> sizeof s
|| dp
->sa_len
> sizeof d
)
439 if (sp
->sa_family
!= ep
->af
|| dp
->sa_family
!= ep
->af
)
441 if (sp
->sa_len
!= ep
->src
.ss_len
|| dp
->sa_len
!= ep
->dst
.ss_len
)
446 p
= (const u_int8_t
*)sp
;
447 q
= (const u_int8_t
*)&ep
->srcmask
;
449 for (i
= 0 ; i
< sp
->sa_len
; i
++) {
452 matchlen
+= (q
[i
] ? 8 : 0);
455 p
= (const u_int8_t
*)dp
;
456 q
= (const u_int8_t
*)&ep
->dstmask
;
458 for (i
= 0 ; i
< dp
->sa_len
; i
++) {
460 /* XXX rough estimate */
461 matchlen
+= (q
[i
] ? 8 : 0);
464 /* need to overwrite len/family portion as we don't compare them */
465 s
.ss_len
= sp
->sa_len
;
466 s
.ss_family
= sp
->sa_family
;
467 d
.ss_len
= dp
->sa_len
;
468 d
.ss_family
= dp
->sa_family
;
470 if (bcmp(&s
, &ep
->src
, ep
->src
.ss_len
) == 0 &&
471 bcmp(&d
, &ep
->dst
, ep
->dst
.ss_len
) == 0) {
478 encap_fillarg(struct mbuf
*m
, const struct encaptab
*ep
)
482 tag
= m_tag_get(PACKET_TAG_ENCAP
, sizeof(void *), MB_DONTWAIT
);
484 *(void **)m_tag_data(tag
) = ep
->arg
;
485 m_tag_prepend(m
, tag
);
490 encap_getarg(struct mbuf
*m
)
495 tag
= m_tag_find(m
, PACKET_TAG_ENCAP
, NULL
);
497 p
= *(void **)m_tag_data(tag
);
498 m_tag_delete(m
, tag
);