]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_encap.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $FreeBSD: src/sys/netinet/ip_encap.c,v 1.1.2.2 2001/07/03 11:01:46 ume Exp $ */
26 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * My grandfather said that there's a devil inside tunnelling technology...
59 * We have surprisingly many protocols that want packets with IP protocol
60 * #4 or #41. Here's a list of protocols that want protocol #41:
61 * RFC1933 configured tunnel
62 * RFC1933 automatic tunnel
63 * RFC2401 IPsec tunnel
64 * RFC2473 IPv6 generic packet tunnelling
65 * RFC2529 6over4 tunnel
66 * mobile-ip6 (uses RFC2473)
68 * Here's a list of protocol that want protocol #4:
69 * RFC1853 IPv4-in-IPv4 tunnelling
70 * RFC2003 IPv4 encapsulation within IPv4
71 * RFC2344 reverse tunnelling for mobile-ip4
72 * RFC2401 IPsec tunnel
73 * Well, what can I say. They impose different en/decapsulation mechanism
74 * from each other, so they need separate protocol handler. The only one
75 * we can easily determine by protocol # is IPsec, which always has
76 * AH/ESP/IPComp header right after outer IP header.
78 * So, clearly good old protosw does not work for protocol #4 and #41.
79 * The code will let you match protocol via src/dst address pair.
81 /* XXX is M_NETADDR correct? */
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/socket.h>
86 #include <sys/sockio.h>
88 #include <sys/errno.h>
89 #include <sys/protosw.h>
90 #include <sys/queue.h>
93 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/ip_encap.h>
101 #include <netinet/ip_mroute.h>
102 #endif /* MROUTING */
105 #include <netinet/ip6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/ip6protosw.h>
111 #include <net/net_osdep.h>
114 #include <sys/kernel.h>
115 #include <sys/malloc.h>
116 MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
119 static void encap_add
__P((struct encaptab
*));
120 static int mask_match
__P((const struct encaptab
*, const struct sockaddr
*,
121 const struct sockaddr
*));
122 static void encap_fillarg
__P((struct mbuf
*, const struct encaptab
*));
124 #ifndef LIST_HEAD_INITIALIZER
125 /* rely upon BSS initialization */
126 LIST_HEAD(, encaptab
) encaptab
;
128 LIST_HEAD(, encaptab
) encaptab
= LIST_HEAD_INITIALIZER(&encaptab
);
134 static int initialized
= 0;
141 * we cannot use LIST_INIT() here, since drivers may want to call
142 * encap_attach(), on driver attach. encap_init() will be called
143 * on AF_INET{,6} initialization, which happens after driver
144 * initialization - using LIST_INIT() here can nuke encap_attach()
147 LIST_INIT(&encaptab
);
159 struct sockaddr_in s
, d
;
160 const struct protosw
*psw
;
161 struct encaptab
*ep
, *match
;
166 off
= va_arg(ap
, int);
167 proto
= va_arg(ap
, int);
171 ip
= mtod(m
, struct ip
*);
176 bzero(&s
, sizeof(s
));
177 s
.sin_family
= AF_INET
;
178 s
.sin_len
= sizeof(struct sockaddr_in
);
179 s
.sin_addr
= ip
->ip_src
;
180 bzero(&d
, sizeof(d
));
181 d
.sin_family
= AF_INET
;
182 d
.sin_len
= sizeof(struct sockaddr_in
);
183 d
.sin_addr
= ip
->ip_dst
;
187 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
188 if (ep
->af
!= AF_INET
)
190 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
193 prio
= (*ep
->func
)(m
, off
, proto
, ep
->arg
);
196 * it's inbound traffic, we need to match in reverse
199 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
200 (struct sockaddr
*)&s
);
204 * We prioritize the matches by using bit length of the
205 * matches. mask_match() and user-supplied matching function
206 * should return the bit length of the matches (for example,
207 * if both src/dst are matched for IPv4, 64 should be returned).
208 * 0 or negative return value means "it did not match".
210 * The question is, since we have two "mask" portion, we
211 * cannot really define total order between entries.
212 * For example, which of these should be preferred?
213 * mask_match() returns 48 (32 + 16) for both of them.
214 * src=3ffe::/16, dst=3ffe:501::/32
215 * src=3ffe:501::/32, dst=3ffe::/16
217 * We need to loop through all the possible candidates
218 * to get the best match - the search takes O(n) for
219 * n attachments (i.e. interfaces).
223 if (prio
> matchprio
) {
230 /* found a match, "match" has the best one */
231 psw
= (const struct protosw
*)match
->psw
;
232 if (psw
&& psw
->pr_input
) {
233 encap_fillarg(m
, match
);
234 (*psw
->pr_input
)(m
, off
);
240 /* for backward compatibility */
242 # define COMPATFUNC ipip_input
246 if (proto
== IPPROTO_IPV4
) {
252 /* last resort: inject to raw socket */
259 encap6_input(mp
, offp
)
263 struct mbuf
*m
= *mp
;
265 struct sockaddr_in6 s
, d
;
266 const struct ip6protosw
*psw
;
267 struct encaptab
*ep
, *match
;
271 ip6
= mtod(m
, struct ip6_hdr
*);
272 proto
= ip6
->ip6_nxt
;
274 bzero(&s
, sizeof(s
));
275 s
.sin6_family
= AF_INET6
;
276 s
.sin6_len
= sizeof(struct sockaddr_in6
);
277 s
.sin6_addr
= ip6
->ip6_src
;
278 bzero(&d
, sizeof(d
));
279 d
.sin6_family
= AF_INET6
;
280 d
.sin6_len
= sizeof(struct sockaddr_in6
);
281 d
.sin6_addr
= ip6
->ip6_dst
;
285 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
286 if (ep
->af
!= AF_INET6
)
288 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
291 prio
= (*ep
->func
)(m
, *offp
, proto
, ep
->arg
);
294 * it's inbound traffic, we need to match in reverse
297 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
298 (struct sockaddr
*)&s
);
301 /* see encap4_input() for issues here */
304 if (prio
> matchprio
) {
312 psw
= (const struct ip6protosw
*)match
->psw
;
313 if (psw
&& psw
->pr_input
) {
314 encap_fillarg(m
, match
);
315 return (*psw
->pr_input
)(mp
, offp
);
322 /* last resort: inject to raw socket */
323 return rip6_input(mp
, offp
);
332 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
336 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
337 * length of mask (sm and dm) is assumed to be same as sp/dp.
338 * Return value will be necessary as input (cookie) for encap_detach().
340 const struct encaptab
*
341 encap_attach(af
, proto
, sp
, sm
, dp
, dm
, psw
, arg
)
344 const struct sockaddr
*sp
, *sm
;
345 const struct sockaddr
*dp
, *dm
;
346 const struct protosw
*psw
;
354 /* sanity check on args */
355 if (sp
->sa_len
> sizeof(ep
->src
) || dp
->sa_len
> sizeof(ep
->dst
)) {
359 if (sp
->sa_len
!= dp
->sa_len
) {
363 if (af
!= sp
->sa_family
|| af
!= dp
->sa_family
) {
368 /* check if anyone have already attached with exactly same config */
369 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
372 if (ep
->proto
!= proto
)
374 if (ep
->src
.ss_len
!= sp
->sa_len
||
375 bcmp(&ep
->src
, sp
, sp
->sa_len
) != 0 ||
376 bcmp(&ep
->srcmask
, sm
, sp
->sa_len
) != 0)
378 if (ep
->dst
.ss_len
!= dp
->sa_len
||
379 bcmp(&ep
->dst
, dp
, dp
->sa_len
) != 0 ||
380 bcmp(&ep
->dstmask
, dm
, dp
->sa_len
) != 0)
387 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_WAITOK
); /*XXX*/
392 bzero(ep
, sizeof(*ep
));
396 bcopy(sp
, &ep
->src
, sp
->sa_len
);
397 bcopy(sm
, &ep
->srcmask
, sp
->sa_len
);
398 bcopy(dp
, &ep
->dst
, dp
->sa_len
);
399 bcopy(dm
, &ep
->dstmask
, dp
->sa_len
);
414 const struct encaptab
*
415 encap_attach_func(af
, proto
, func
, psw
, arg
)
418 int (*func
) __P((const struct mbuf
*, int, int, void *));
419 const struct protosw
*psw
;
427 /* sanity check on args */
433 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_WAITOK
); /*XXX*/
438 bzero(ep
, sizeof(*ep
));
459 const struct encaptab
*cookie
;
461 const struct encaptab
*ep
= cookie
;
464 for (p
= LIST_FIRST(&encaptab
); p
; p
= LIST_NEXT(p
, chain
)) {
466 LIST_REMOVE(p
, chain
);
467 _FREE(p
, M_NETADDR
); /*XXX*/
476 mask_match(ep
, sp
, dp
)
477 const struct encaptab
*ep
;
478 const struct sockaddr
*sp
;
479 const struct sockaddr
*dp
;
481 struct sockaddr_storage s
;
482 struct sockaddr_storage d
;
484 const u_int8_t
*p
, *q
;
488 if (sp
->sa_len
> sizeof(s
) || dp
->sa_len
> sizeof(d
))
490 if (sp
->sa_family
!= ep
->af
|| dp
->sa_family
!= ep
->af
)
492 if (sp
->sa_len
!= ep
->src
.ss_len
|| dp
->sa_len
!= ep
->dst
.ss_len
)
497 p
= (const u_int8_t
*)sp
;
498 q
= (const u_int8_t
*)&ep
->srcmask
;
500 for (i
= 0 ; i
< sp
->sa_len
; i
++) {
503 matchlen
+= (q
[i
] ? 8 : 0);
506 p
= (const u_int8_t
*)dp
;
507 q
= (const u_int8_t
*)&ep
->dstmask
;
509 for (i
= 0 ; i
< dp
->sa_len
; i
++) {
511 /* XXX rough estimate */
512 matchlen
+= (q
[i
] ? 8 : 0);
515 /* need to overwrite len/family portion as we don't compare them */
516 s
.ss_len
= sp
->sa_len
;
517 s
.ss_family
= sp
->sa_family
;
518 d
.ss_len
= dp
->sa_len
;
519 d
.ss_family
= dp
->sa_family
;
521 if (bcmp(&s
, &ep
->src
, ep
->src
.ss_len
) == 0 &&
522 bcmp(&d
, &ep
->dst
, ep
->dst
.ss_len
) == 0) {
531 const struct encaptab
*ep
;
534 m
->m_pkthdr
.aux
= ep
->arg
;
538 n
= m_aux_add(m
, AF_INET
, IPPROTO_IPV4
);
540 *mtod(n
, void **) = ep
->arg
;
541 n
->m_len
= sizeof(void *);
553 m
->m_pkthdr
.aux
= NULL
;
559 n
= m_aux_find(m
, AF_INET
, IPPROTO_IPV4
);
561 if (n
->m_len
== sizeof(void *))
562 p
= *mtod(n
, void **);