]>
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 /* $FreeBSD: src/sys/netinet/ip_encap.c,v 1.1.2.2 2001/07/03 11:01:46 ume Exp $ */
23 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */
26 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * My grandfather said that there's a devil inside tunnelling technology...
56 * We have surprisingly many protocols that want packets with IP protocol
57 * #4 or #41. Here's a list of protocols that want protocol #41:
58 * RFC1933 configured tunnel
59 * RFC1933 automatic tunnel
60 * RFC2401 IPsec tunnel
61 * RFC2473 IPv6 generic packet tunnelling
62 * RFC2529 6over4 tunnel
63 * mobile-ip6 (uses RFC2473)
65 * Here's a list of protocol that want protocol #4:
66 * RFC1853 IPv4-in-IPv4 tunnelling
67 * RFC2003 IPv4 encapsulation within IPv4
68 * RFC2344 reverse tunnelling for mobile-ip4
69 * RFC2401 IPsec tunnel
70 * Well, what can I say. They impose different en/decapsulation mechanism
71 * from each other, so they need separate protocol handler. The only one
72 * we can easily determine by protocol # is IPsec, which always has
73 * AH/ESP/IPComp header right after outer IP header.
75 * So, clearly good old protosw does not work for protocol #4 and #41.
76 * The code will let you match protocol via src/dst address pair.
78 /* XXX is M_NETADDR correct? */
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/socket.h>
83 #include <sys/sockio.h>
85 #include <sys/errno.h>
86 #include <sys/protosw.h>
87 #include <sys/queue.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/ip_encap.h>
98 #include <netinet/ip_mroute.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/ip6protosw.h>
108 #include <net/net_osdep.h>
111 #include <sys/kernel.h>
112 #include <sys/malloc.h>
113 MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
116 static void encap_add
__P((struct encaptab
*));
117 static int mask_match
__P((const struct encaptab
*, const struct sockaddr
*,
118 const struct sockaddr
*));
119 static void encap_fillarg
__P((struct mbuf
*, const struct encaptab
*));
121 #ifndef LIST_HEAD_INITIALIZER
122 /* rely upon BSS initialization */
123 LIST_HEAD(, encaptab
) encaptab
;
125 LIST_HEAD(, encaptab
) encaptab
= LIST_HEAD_INITIALIZER(&encaptab
);
131 static int initialized
= 0;
138 * we cannot use LIST_INIT() here, since drivers may want to call
139 * encap_attach(), on driver attach. encap_init() will be called
140 * on AF_INET{,6} initialization, which happens after driver
141 * initialization - using LIST_INIT() here can nuke encap_attach()
144 LIST_INIT(&encaptab
);
156 struct sockaddr_in s
, d
;
157 const struct protosw
*psw
;
158 struct encaptab
*ep
, *match
;
163 off
= va_arg(ap
, int);
164 proto
= va_arg(ap
, int);
168 ip
= mtod(m
, struct ip
*);
173 bzero(&s
, sizeof(s
));
174 s
.sin_family
= AF_INET
;
175 s
.sin_len
= sizeof(struct sockaddr_in
);
176 s
.sin_addr
= ip
->ip_src
;
177 bzero(&d
, sizeof(d
));
178 d
.sin_family
= AF_INET
;
179 d
.sin_len
= sizeof(struct sockaddr_in
);
180 d
.sin_addr
= ip
->ip_dst
;
184 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
185 if (ep
->af
!= AF_INET
)
187 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
190 prio
= (*ep
->func
)(m
, off
, proto
, ep
->arg
);
193 * it's inbound traffic, we need to match in reverse
196 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
197 (struct sockaddr
*)&s
);
201 * We prioritize the matches by using bit length of the
202 * matches. mask_match() and user-supplied matching function
203 * should return the bit length of the matches (for example,
204 * if both src/dst are matched for IPv4, 64 should be returned).
205 * 0 or negative return value means "it did not match".
207 * The question is, since we have two "mask" portion, we
208 * cannot really define total order between entries.
209 * For example, which of these should be preferred?
210 * mask_match() returns 48 (32 + 16) for both of them.
211 * src=3ffe::/16, dst=3ffe:501::/32
212 * src=3ffe:501::/32, dst=3ffe::/16
214 * We need to loop through all the possible candidates
215 * to get the best match - the search takes O(n) for
216 * n attachments (i.e. interfaces).
220 if (prio
> matchprio
) {
227 /* found a match, "match" has the best one */
228 psw
= (const struct protosw
*)match
->psw
;
229 if (psw
&& psw
->pr_input
) {
230 encap_fillarg(m
, match
);
231 (*psw
->pr_input
)(m
, off
);
237 /* for backward compatibility */
239 # define COMPATFUNC ipip_input
243 if (proto
== IPPROTO_IPV4
) {
249 /* last resort: inject to raw socket */
256 encap6_input(mp
, offp
)
260 struct mbuf
*m
= *mp
;
262 struct sockaddr_in6 s
, d
;
263 const struct ip6protosw
*psw
;
264 struct encaptab
*ep
, *match
;
268 ip6
= mtod(m
, struct ip6_hdr
*);
269 proto
= ip6
->ip6_nxt
;
271 bzero(&s
, sizeof(s
));
272 s
.sin6_family
= AF_INET6
;
273 s
.sin6_len
= sizeof(struct sockaddr_in6
);
274 s
.sin6_addr
= ip6
->ip6_src
;
275 bzero(&d
, sizeof(d
));
276 d
.sin6_family
= AF_INET6
;
277 d
.sin6_len
= sizeof(struct sockaddr_in6
);
278 d
.sin6_addr
= ip6
->ip6_dst
;
282 for (ep
= LIST_FIRST(&encaptab
); ep
; ep
= LIST_NEXT(ep
, chain
)) {
283 if (ep
->af
!= AF_INET6
)
285 if (ep
->proto
>= 0 && ep
->proto
!= proto
)
288 prio
= (*ep
->func
)(m
, *offp
, proto
, ep
->arg
);
291 * it's inbound traffic, we need to match in reverse
294 prio
= mask_match(ep
, (struct sockaddr
*)&d
,
295 (struct sockaddr
*)&s
);
298 /* see encap4_input() for issues here */
301 if (prio
> matchprio
) {
309 psw
= (const struct ip6protosw
*)match
->psw
;
310 if (psw
&& psw
->pr_input
) {
311 encap_fillarg(m
, match
);
312 return (*psw
->pr_input
)(mp
, offp
);
319 /* last resort: inject to raw socket */
320 return rip6_input(mp
, offp
);
329 LIST_INSERT_HEAD(&encaptab
, ep
, chain
);
333 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side.
334 * length of mask (sm and dm) is assumed to be same as sp/dp.
335 * Return value will be necessary as input (cookie) for encap_detach().
337 const struct encaptab
*
338 encap_attach(af
, proto
, sp
, sm
, dp
, dm
, psw
, arg
)
341 const struct sockaddr
*sp
, *sm
;
342 const struct sockaddr
*dp
, *dm
;
343 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
);
411 const struct encaptab
*
412 encap_attach_func(af
, proto
, func
, psw
, arg
)
415 int (*func
) __P((const struct mbuf
*, int, int, void *));
416 const struct protosw
*psw
;
424 /* sanity check on args */
430 ep
= _MALLOC(sizeof(*ep
), M_NETADDR
, M_WAITOK
); /*XXX*/
435 bzero(ep
, sizeof(*ep
));
456 const struct encaptab
*cookie
;
458 const struct encaptab
*ep
= cookie
;
461 for (p
= LIST_FIRST(&encaptab
); p
; p
= LIST_NEXT(p
, chain
)) {
463 LIST_REMOVE(p
, chain
);
464 _FREE(p
, M_NETADDR
); /*XXX*/
473 mask_match(ep
, sp
, dp
)
474 const struct encaptab
*ep
;
475 const struct sockaddr
*sp
;
476 const struct sockaddr
*dp
;
478 struct sockaddr_storage s
;
479 struct sockaddr_storage d
;
481 const u_int8_t
*p
, *q
;
485 if (sp
->sa_len
> sizeof(s
) || dp
->sa_len
> sizeof(d
))
487 if (sp
->sa_family
!= ep
->af
|| dp
->sa_family
!= ep
->af
)
489 if (sp
->sa_len
!= ep
->src
.ss_len
|| dp
->sa_len
!= ep
->dst
.ss_len
)
494 p
= (const u_int8_t
*)sp
;
495 q
= (const u_int8_t
*)&ep
->srcmask
;
497 for (i
= 0 ; i
< sp
->sa_len
; i
++) {
500 matchlen
+= (q
[i
] ? 8 : 0);
503 p
= (const u_int8_t
*)dp
;
504 q
= (const u_int8_t
*)&ep
->dstmask
;
506 for (i
= 0 ; i
< dp
->sa_len
; i
++) {
508 /* XXX rough estimate */
509 matchlen
+= (q
[i
] ? 8 : 0);
512 /* need to overwrite len/family portion as we don't compare them */
513 s
.ss_len
= sp
->sa_len
;
514 s
.ss_family
= sp
->sa_family
;
515 d
.ss_len
= dp
->sa_len
;
516 d
.ss_family
= dp
->sa_family
;
518 if (bcmp(&s
, &ep
->src
, ep
->src
.ss_len
) == 0 &&
519 bcmp(&d
, &ep
->dst
, ep
->dst
.ss_len
) == 0) {
528 const struct encaptab
*ep
;
531 m
->m_pkthdr
.aux
= ep
->arg
;
535 n
= m_aux_add(m
, AF_INET
, IPPROTO_IPV4
);
537 *mtod(n
, void **) = ep
->arg
;
538 n
->m_len
= sizeof(void *);
550 m
->m_pkthdr
.aux
= NULL
;
556 n
= m_aux_find(m
, AF_INET
, IPPROTO_IPV4
);
558 if (n
->m_len
== sizeof(void *))
559 p
= *mtod(n
, void **);