]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $KAME: ip_encap.c,v 1.21 2000/03/30 14:30:06 itojun Exp $ */
25 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * My grandfather said that there's a devil inside tunnelling technology...
55 * We have surprisingly many protocols that want packets with IP protocol
56 * #4 or #41. Here's a list of protocols that want protocol #41:
57 * RFC1933 configured tunnel
58 * RFC1933 automatic tunnel
59 * RFC2401 IPsec tunnel
60 * RFC2473 IPv6 generic packet tunnelling
61 * RFC2529 6over4 tunnel
62 * mobile-ip6 (uses RFC2473)
64 * Here's a list of protocol that want protocol #4:
65 * RFC1853 IPv4-in-IPv4 tunnel
66 * RFC2344 reverse tunnelling for mobile-ip4
67 * RFC2401 IPsec tunnel
68 * Well, what can I say. They impose different en/decapsulation mechanism
69 * from each other, so they need separate protocol handler. The only one
70 * we can easily determine by protocol # is IPsec, which always has
71 * AH/ESP/IPComp header right after outer IP header.
73 * So, clearly good old protosw does not work for protocol #4 and #41.
74 * The code will let you match protocol via src/dst address pair.
78 # include "opt_mrouting.h"
80 # include "opt_inet.h"
83 # include "opt_inet.h"
84 # include "opt_inet6.h"
88 # include "opt_inet.h"
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/socket.h>
95 #include <sys/sockio.h>
97 #include <sys/errno.h>
98 #include <sys/protosw.h>
99 #include <sys/malloc.h>
102 #include <net/route.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <netinet/ip_var.h>
108 #include <netinet/ip_encap.h>
110 #include <netinet/ip_mroute.h>
111 #endif /* MROUTING */
113 #include <netinet/ip_ipsp.h>
117 #include <netinet/ip6.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet6/ip6protosw.h>
123 #include <net/net_osdep.h>
125 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
126 #include <sys/kernel.h>
127 #include <sys/malloc.h>
128 MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
131 static int mask_match
__P((const struct encaptab
*, const struct sockaddr
*,
132 const struct sockaddr
*));
133 static void encap_fillarg
__P((struct mbuf
*, const struct encaptab
*));
135 /* rely upon BSS initialization */
136 LIST_HEAD(, encaptab
) encaptab
;
143 * we cannot use LIST_INIT() here, since drivers may want to call
144 * encap_attach(), on driver attach. encap_init() wlil be called
145 * on AF_INET{,6} initialization, which happens after driver
146 * initialization - using LIST_INIT() here can nuke encap_attach()
149 LIST_INIT(&encaptab
);
154 encap4_input(m
, off
, proto
)
160 struct sockaddr_in s
, d
;
164 ip
= mtod(m
, struct ip
*);
169 bzero(&s
, sizeof(s
));
170 s
.sin_family
= AF_INET
;
171 s
.sin_len
= sizeof(struct sockaddr_in
);
172 s
.sin_addr
= ip
->ip_src
;
173 bzero(&d
, sizeof(d
));
174 d
.sin_family
= AF_INET
;
175 d
.sin_len
= sizeof(struct sockaddr_in
);
176 d
.sin_addr
= ip
->ip_dst
;
178 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
179 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
183 if ((*ep
->func
)(m
, off
, proto
, ep
->arg
) == 0)
187 * it's inbound traffic, we need to match in reverse
190 if (mask_match(ep
, (struct sockaddr
*)&d
,
191 (struct sockaddr
*)&s
) == 0)
196 if (ep
->psw
&& ep
->psw
->pr_input
) {
197 encap_fillarg(m
, ep
);
198 #warning watchout pr_input!
199 (*ep
->psw
->pr_input
)(m
, off
);
205 /* for backward compatibility */
206 if (proto
== IPPROTO_IPV4
) {
208 #if defined(MROUTING) || defined(IPSEC)
209 ip4_input(m
, off
, proto
);
220 /* last resort: inject to raw socket */
226 encap6_input(mp
, offp
, proto
)
231 struct mbuf
*m
= *mp
;
233 struct sockaddr_in6 s
, d
;
234 struct ip6protosw
*psw
;
237 ip6
= mtod(m
, struct ip6_hdr
*);
239 bzero(&s
, sizeof(s
));
240 s
.sin6_family
= AF_INET6
;
241 s
.sin6_len
= sizeof(struct sockaddr_in6
);
242 s
.sin6_addr
= ip6
->ip6_src
;
243 bzero(&d
, sizeof(d
));
244 d
.sin6_family
= AF_INET6
;
245 d
.sin6_len
= sizeof(struct sockaddr_in6
);
246 d
.sin6_addr
= ip6
->ip6_dst
;
248 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
249 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
252 if ((*ep
->func
)(m
, *offp
, proto
, ep
->arg
) == 0)
256 * it's inbound traffic, we need to match in reverse
259 if (mask_match(ep
, (struct sockaddr
*)&d
,
260 (struct sockaddr
*)&s
) == 0)
265 psw
= (struct ip6protosw
*)ep
->psw
;
266 #warning watchout pr_input!
267 if (psw
&& psw
->pr_input
) {
268 encap_fillarg(m
, ep
);
269 return (*psw
->pr_input
)(mp
, offp
, proto
);
276 /* last resort: inject to raw socket */
277 return rip6_input(mp
, offp
, proto
);
282 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
283 * length of mask (sm and dm) is assumed to be same as sp/dp.
284 * Return value will be necessary as input (cookie) for encap_detach().
286 const struct encaptab
*
287 encap_attach(af
, proto
, sp
, sm
, dp
, dm
, psw
, arg
)
290 const struct sockaddr
*sp
, *sm
;
291 const struct sockaddr
*dp
, *dm
;
292 const struct protosw
*psw
;
299 #if defined(__NetBSD__) || defined(__OpenBSD__)
304 /* sanity check on args */
305 if (sp
->sa_len
> sizeof(ep
->src
) || dp
->sa_len
> sizeof(ep
->dst
)) {
309 if (sp
->sa_len
!= dp
->sa_len
) {
313 if (af
!= sp
->sa_family
|| af
!= dp
->sa_family
) {
318 /* check if anyone have already attached with exactly same config */
319 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
322 if (ep
->proto
!= proto
)
324 if (ep
->src
.ss_len
!= sp
->sa_len
||
325 bcmp(&ep
->src
, sp
, sp
->sa_len
) != 0 ||
326 bcmp(&ep
->srcmask
, sm
, sp
->sa_len
) != 0)
328 if (ep
->dst
.ss_len
!= dp
->sa_len
||
329 bcmp(&ep
->dst
, dp
, dp
->sa_len
) != 0 ||
330 bcmp(&ep
->dstmask
, dm
, dp
->sa_len
) != 0)
337 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_NOWAIT
); /*XXX*/
342 bzero(ep
, sizeof(*ep
));
346 bcopy(sp
, &ep
->src
, sp
->sa_len
);
347 bcopy(sm
, &ep
->srcmask
, sp
->sa_len
);
348 bcopy(dp
, &ep
->dst
, dp
->sa_len
);
349 bcopy(dm
, &ep
->dstmask
, dp
->sa_len
);
354 * Order of insertion will determine the priority in lookup.
355 * We should be careful putting them in specific-one-first order.
356 * The question is, since we have two "mask" portion, we cannot really
357 * define total order between entries.
358 * For example, which of these should be preferred?
359 * src=3ffe::/16, dst=3ffe:501::/32
360 * src=3ffe:501::/32, dst=3ffe::/16
362 * At this moment we don't care about the ordering.
364 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
374 const struct encaptab
*
375 encap_attach_func(af
, proto
, func
, psw
, arg
)
378 int (*func
) __P((const struct mbuf
*, int, int, void *));
379 const struct protosw
*psw
;
386 #if defined(__NetBSD__) || defined(__OpenBSD__)
391 /* sanity check on args */
397 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_NOWAIT
); /*XXX*/
402 bzero(ep
, sizeof(*ep
));
411 * Order of insertion will determine the priority in lookup.
412 * We should be careful putting them in specific-one-first order.
413 * The question is, since we have two "mask" portion, we cannot really
414 * define total order between entries.
415 * For example, which of these should be checked first?
416 * src=3ffe::/16, dst=3ffe:501::/32
417 * src=3ffe:501::/32, dst=3ffe::/16
419 * At this moment we don't care about the ordering.
421 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
433 const struct encaptab
*cookie
;
435 const struct encaptab
*ep
= cookie
;
438 for (p
= LIST_FIRST(&encaptab
); p
; p
= LIST_NEXT(p
, chain
)) {
440 LIST_REMOVE(p
, chain
);
441 _FREE(p
, M_NETADDR
); /*XXX*/
450 mask_match(ep
, sp
, dp
)
451 const struct encaptab
*ep
;
452 const struct sockaddr
*sp
;
453 const struct sockaddr
*dp
;
455 struct sockaddr_storage s
;
456 struct sockaddr_storage d
;
460 if (sp
->sa_len
> sizeof(s
) || dp
->sa_len
> sizeof(d
))
462 if (sp
->sa_family
!= ep
->af
|| dp
->sa_family
!= ep
->af
)
464 if (sp
->sa_len
!= ep
->src
.ss_len
|| dp
->sa_len
!= ep
->dst
.ss_len
)
468 q
= (u_int8_t
*)&ep
->srcmask
;
470 for (i
= 0 ; i
< sp
->sa_len
; i
++)
474 q
= (u_int8_t
*)&ep
->dstmask
;
476 for (i
= 0 ; i
< dp
->sa_len
; i
++)
479 /* need to overwrite len/family portion as we don't compare them */
480 s
.ss_len
= sp
->sa_len
;
481 s
.ss_family
= sp
->sa_family
;
482 d
.ss_len
= dp
->sa_len
;
483 d
.ss_family
= dp
->sa_family
;
485 if (bcmp(&s
, &ep
->src
, ep
->src
.ss_len
) == 0 &&
486 bcmp(&d
, &ep
->dst
, ep
->dst
.ss_len
) == 0) {
495 const struct encaptab
*ep
;
498 m
->m_pkthdr
.aux
= ep
->arg
;
502 n
= m_aux_add(m
, AF_INET
, IPPROTO_IPV4
);
504 *mtod(n
, void **) = ep
->arg
;
505 n
->m_len
= sizeof(void *);
517 m
->m_pkthdr
.aux
= NULL
;
523 n
= m_aux_find(m
, AF_INET
, IPPROTO_IPV4
);
525 if (n
->m_len
== sizeof(void *))
526 p
= *mtod(n
, void **);