]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_encap.c
2 * Copyright (c) 2000,2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $FreeBSD: src/sys/netinet/ip_encap.c,v 1.1.2.2 2001/07/03 11:01:46 ume Exp $ */
29 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * My grandfather said that there's a devil inside tunnelling technology...
62 * We have surprisingly many protocols that want packets with IP protocol
63 * #4 or #41. Here's a list of protocols that want protocol #41:
64 * RFC1933 configured tunnel
65 * RFC1933 automatic tunnel
66 * RFC2401 IPsec tunnel
67 * RFC2473 IPv6 generic packet tunnelling
68 * RFC2529 6over4 tunnel
69 * mobile-ip6 (uses RFC2473)
71 * Here's a list of protocol that want protocol #4:
72 * RFC1853 IPv4-in-IPv4 tunnelling
73 * RFC2003 IPv4 encapsulation within IPv4
74 * RFC2344 reverse tunnelling for mobile-ip4
75 * RFC2401 IPsec tunnel
76 * Well, what can I say. They impose different en/decapsulation mechanism
77 * from each other, so they need separate protocol handler. The only one
78 * we can easily determine by protocol # is IPsec, which always has
79 * AH/ESP/IPComp header right after outer IP header.
81 * So, clearly good old protosw does not work for protocol #4 and #41.
82 * The code will let you match protocol via src/dst address pair.
84 /* XXX is M_NETADDR correct? */
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/socket.h>
89 #include <sys/sockio.h>
91 #include <sys/errno.h>
92 #include <sys/protosw.h>
93 #include <sys/queue.h>
96 #include <net/route.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/ip.h>
101 #include <netinet/ip_var.h>
102 #include <netinet/ip_encap.h>
104 #include <netinet/ip_mroute.h>
105 #endif /* MROUTING */
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/ip6protosw.h>
114 #include <net/net_osdep.h>
117 #include <sys/kernel.h>
118 #include <sys/malloc.h>
119 MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
122 static void encap_add(struct encaptab
*);
123 static int mask_match(const struct encaptab
*, const struct sockaddr
*,
124 const struct sockaddr
*);
125 static void encap_fillarg(struct mbuf
*, const struct encaptab
*);
127 #ifndef LIST_HEAD_INITIALIZER
128 /* rely upon BSS initialization */
129 LIST_HEAD(, encaptab
) encaptab
;
131 LIST_HEAD(, encaptab
) encaptab
= LIST_HEAD_INITIALIZER(&encaptab
);
137 static int initialized
= 0;
144 * we cannot use LIST_INIT() here, since drivers may want to call
145 * encap_attach(), on driver attach. encap_init() will be called
146 * on AF_INET{,6} initialization, which happens after driver
147 * initialization - using LIST_INIT() here can nuke encap_attach()
150 LIST_INIT(&encaptab
);
162 struct sockaddr_in s
, d
;
163 const struct protosw
*psw
;
164 struct encaptab
*ep
, *match
;
169 off
= va_arg(ap
, int);
170 proto
= va_arg(ap
, int);
174 ip
= mtod(m
, struct ip
*);
179 bzero(&s
, sizeof(s
));
180 s
.sin_family
= AF_INET
;
181 s
.sin_len
= sizeof(struct sockaddr_in
);
182 s
.sin_addr
= ip
->ip_src
;
183 bzero(&d
, sizeof(d
));
184 d
.sin_family
= AF_INET
;
185 d
.sin_len
= sizeof(struct sockaddr_in
);
186 d
.sin_addr
= ip
->ip_dst
;
190 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
191 if (ep
->af
!= AF_INET
)
193 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
196 prio
= (*ep
->func
)(m
, off
, proto
, ep
->arg
);
199 * it's inbound traffic, we need to match in reverse
202 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
203 (struct sockaddr
*)&s
);
207 * We prioritize the matches by using bit length of the
208 * matches. mask_match() and user-supplied matching function
209 * should return the bit length of the matches (for example,
210 * if both src/dst are matched for IPv4, 64 should be returned).
211 * 0 or negative return value means "it did not match".
213 * The question is, since we have two "mask" portion, we
214 * cannot really define total order between entries.
215 * For example, which of these should be preferred?
216 * mask_match() returns 48 (32 + 16) for both of them.
217 * src=3ffe::/16, dst=3ffe:501::/32
218 * src=3ffe:501::/32, dst=3ffe::/16
220 * We need to loop through all the possible candidates
221 * to get the best match - the search takes O(n) for
222 * n attachments (i.e. interfaces).
226 if (prio
> matchprio
) {
233 /* found a match, "match" has the best one */
234 psw
= (const struct protosw
*)match
->psw
;
235 if (psw
&& psw
->pr_input
) {
236 encap_fillarg(m
, match
);
237 (*psw
->pr_input
)(m
, off
);
243 /* for backward compatibility */
245 # define COMPATFUNC ipip_input
249 if (proto
== IPPROTO_IPV4
) {
255 /* last resort: inject to raw socket */
262 encap6_input(struct mbuf
**mp
, int *offp
, int proto
)
264 struct mbuf
*m
= *mp
;
266 struct sockaddr_in6 s
, d
;
267 const struct ip6protosw
*psw
;
268 struct encaptab
*ep
, *match
;
271 ip6
= mtod(m
, struct ip6_hdr
*);
273 bzero(&s
, sizeof(s
));
274 s
.sin6_family
= AF_INET6
;
275 s
.sin6_len
= sizeof(struct sockaddr_in6
);
276 s
.sin6_addr
= ip6
->ip6_src
;
277 bzero(&d
, sizeof(d
));
278 d
.sin6_family
= AF_INET6
;
279 d
.sin6_len
= sizeof(struct sockaddr_in6
);
280 d
.sin6_addr
= ip6
->ip6_dst
;
284 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
285 if (ep
->af
!= AF_INET6
)
287 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
290 prio
= (*ep
->func
)(m
, *offp
, proto
, ep
->arg
);
293 * it's inbound traffic, we need to match in reverse
296 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
297 (struct sockaddr
*)&s
);
300 /* see encap4_input() for issues here */
303 if (prio
> matchprio
) {
311 psw
= (const struct ip6protosw
*)match
->psw
;
312 if (psw
&& psw
->pr_input
) {
313 encap_fillarg(m
, match
);
314 return (*psw
->pr_input
)(mp
, offp
, proto
);
321 /* last resort: inject to raw socket */
322 return rip6_input(mp
, offp
, proto
);
331 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
335 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
336 * length of mask (sm and dm) is assumed to be same as sp/dp.
337 * Return value will be necessary as input (cookie) for encap_detach().
339 const struct encaptab
*
340 encap_attach(af
, proto
, sp
, sm
, dp
, dm
, psw
, arg
)
343 const struct sockaddr
*sp
, *sm
;
344 const struct sockaddr
*dp
, *dm
;
345 const struct protosw
*psw
;
351 /* sanity check on args */
352 if (sp
->sa_len
> sizeof(ep
->src
) || dp
->sa_len
> sizeof(ep
->dst
)) {
356 if (sp
->sa_len
!= dp
->sa_len
) {
360 if (af
!= sp
->sa_family
|| af
!= dp
->sa_family
) {
365 /* check if anyone have already attached with exactly same config */
366 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
369 if (ep
->proto
!= proto
)
371 if (ep
->src
.ss_len
!= sp
->sa_len
||
372 bcmp(&ep
->src
, sp
, sp
->sa_len
) != 0 ||
373 bcmp(&ep
->srcmask
, sm
, sp
->sa_len
) != 0)
375 if (ep
->dst
.ss_len
!= dp
->sa_len
||
376 bcmp(&ep
->dst
, dp
, dp
->sa_len
) != 0 ||
377 bcmp(&ep
->dstmask
, dm
, dp
->sa_len
) != 0)
384 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_WAITOK
); /*XXX*/
389 bzero(ep
, sizeof(*ep
));
393 bcopy(sp
, &ep
->src
, sp
->sa_len
);
394 bcopy(sm
, &ep
->srcmask
, sp
->sa_len
);
395 bcopy(dp
, &ep
->dst
, dp
->sa_len
);
396 bcopy(dm
, &ep
->dstmask
, dp
->sa_len
);
409 const struct encaptab
*
410 encap_attach_func(af
, proto
, func
, psw
, arg
)
413 int (*func
)(const struct mbuf
*, int, int, void *);
414 const struct protosw
*psw
;
420 /* sanity check on args */
426 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_WAITOK
); /*XXX*/
431 bzero(ep
, sizeof(*ep
));
450 const struct encaptab
*cookie
;
452 const struct encaptab
*ep
= cookie
;
455 for (p
= LIST_FIRST(&encaptab
); p
; p
= LIST_NEXT(p
, chain
)) {
457 LIST_REMOVE(p
, chain
);
458 _FREE(p
, M_NETADDR
); /*XXX*/
467 mask_match(ep
, sp
, dp
)
468 const struct encaptab
*ep
;
469 const struct sockaddr
*sp
;
470 const struct sockaddr
*dp
;
472 struct sockaddr_storage s
;
473 struct sockaddr_storage d
;
475 const u_int8_t
*p
, *q
;
479 if (sp
->sa_len
> sizeof(s
) || dp
->sa_len
> sizeof(d
))
481 if (sp
->sa_family
!= ep
->af
|| dp
->sa_family
!= ep
->af
)
483 if (sp
->sa_len
!= ep
->src
.ss_len
|| dp
->sa_len
!= ep
->dst
.ss_len
)
488 p
= (const u_int8_t
*)sp
;
489 q
= (const u_int8_t
*)&ep
->srcmask
;
491 for (i
= 0 ; i
< sp
->sa_len
; i
++) {
494 matchlen
+= (q
[i
] ? 8 : 0);
497 p
= (const u_int8_t
*)dp
;
498 q
= (const u_int8_t
*)&ep
->dstmask
;
500 for (i
= 0 ; i
< dp
->sa_len
; i
++) {
502 /* XXX rough estimate */
503 matchlen
+= (q
[i
] ? 8 : 0);
506 /* need to overwrite len/family portion as we don't compare them */
507 s
.ss_len
= sp
->sa_len
;
508 s
.ss_family
= sp
->sa_family
;
509 d
.ss_len
= dp
->sa_len
;
510 d
.ss_family
= dp
->sa_family
;
512 if (bcmp(&s
, &ep
->src
, ep
->src
.ss_len
) == 0 &&
513 bcmp(&d
, &ep
->dst
, ep
->dst
.ss_len
) == 0) {
526 const struct encaptab
*ep
)
529 struct encaptabtag
*et
;
531 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_ENCAP
,
532 sizeof(struct encaptabtag
), M_WAITOK
, m
);
535 et
= (struct encaptabtag
*)(tag
+ 1);
537 m_tag_prepend(m
, tag
);
546 struct encaptabtag
*et
;
549 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_ENCAP
, NULL
);
551 et
= (struct encaptabtag
*)(tag
+ 1);
553 m_tag_delete(m
, tag
);