]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_gif.c
c977e9a7a64cf4295e86ef9e731787ee2db3f086
2 * Copyright (c) 2009-2012 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@
29 /* $FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.3 2001/07/03 11:01:52 ume Exp $ */
30 /* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
67 #include <sys/errno.h>
68 #include <sys/queue.h>
69 #include <sys/syslog.h>
71 #include <sys/malloc.h>
72 #include <sys/protosw.h>
75 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
82 #include <netinet/ip_encap.h>
84 #include <netinet/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/in6_gif.h>
87 #include <netinet6/in6_var.h>
89 #include <netinet/ip_ecn.h>
91 #include <netinet6/ip6_ecn.h>
94 #include <net/if_gif.h>
96 #include <net/net_osdep.h>
98 static __inline__
void*
99 _cast_non_const(const void * ptr
) {
112 int family
, /* family of the packet to be encapsulate. */
114 __unused
struct rtentry
*rt
)
116 struct gif_softc
*sc
= ifnet_softc(ifp
);
117 struct sockaddr_in6
*dst
= (struct sockaddr_in6
*)&sc
->gif_ro6
.ro_dst
;
118 struct sockaddr_in6
*sin6_src
= (struct sockaddr_in6
*)(void *)sc
->gif_psrc
;
119 struct sockaddr_in6
*sin6_dst
= (struct sockaddr_in6
*)(void *)sc
->gif_pdst
;
124 if (sin6_src
== NULL
|| sin6_dst
== NULL
||
125 sin6_src
->sin6_family
!= AF_INET6
||
126 sin6_dst
->sin6_family
!= AF_INET6
) {
137 proto
= IPPROTO_IPV4
;
138 if (mbuf_len(m
) < sizeof(*ip
)) {
139 m
= m_pullup(m
, sizeof(*ip
));
143 ip
= mtod(m
, struct ip
*);
151 proto
= IPPROTO_IPV6
;
152 if (mbuf_len(m
) < sizeof(*ip6
)) {
153 m
= m_pullup(m
, sizeof(*ip6
));
157 ip6
= mtod(m
, struct ip6_hdr
*);
158 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
164 printf("in6_gif_output: warning: unknown family %d passed\n",
171 /* prepend new IP header */
172 M_PREPEND(m
, sizeof(struct ip6_hdr
), M_DONTWAIT
);
173 if (m
&& mbuf_len(m
) < sizeof(struct ip6_hdr
))
174 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
176 printf("ENOBUFS in in6_gif_output %d\n", __LINE__
);
180 ip6
= mtod(m
, struct ip6_hdr
*);
182 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
183 ip6
->ip6_vfc
|= IPV6_VERSION
;
184 ip6
->ip6_plen
= htons((u_short
)m
->m_pkthdr
.len
);
185 ip6
->ip6_nxt
= proto
;
186 ip6
->ip6_hlim
= ip6_gif_hlim
;
187 ip6
->ip6_src
= sin6_src
->sin6_addr
;
188 /* bidirectional configured tunnel mode */
189 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst
->sin6_addr
))
190 ip6
->ip6_dst
= sin6_dst
->sin6_addr
;
195 ip_ecn_ingress((ifp
->if_flags
& IFF_LINK1
) ? ECN_ALLOWED
: ECN_NOCARE
,
197 ip6
->ip6_flow
&= ~htonl(0xff << 20);
198 ip6
->ip6_flow
|= htonl((u_int32_t
)otos
<< 20);
200 if (dst
->sin6_family
!= sin6_dst
->sin6_family
||
201 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &sin6_dst
->sin6_addr
) ||
202 (sc
->gif_ro6
.ro_rt
!= NULL
&&
203 (sc
->gif_ro6
.ro_rt
->generation_id
!= route_generation
||
204 sc
->gif_ro6
.ro_rt
->rt_ifp
== ifp
))) {
205 /* cache route doesn't match or recursive route */
206 bzero(dst
, sizeof(*dst
));
207 dst
->sin6_family
= sin6_dst
->sin6_family
;
208 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
209 dst
->sin6_addr
= sin6_dst
->sin6_addr
;
210 if (sc
->gif_ro6
.ro_rt
) {
211 rtfree(sc
->gif_ro6
.ro_rt
);
212 sc
->gif_ro6
.ro_rt
= NULL
;
215 sc
->gif_if
.if_mtu
= GIF_MTU
;
219 if (sc
->gif_ro6
.ro_rt
== NULL
) {
220 rtalloc((struct route
*)&sc
->gif_ro6
);
221 if (sc
->gif_ro6
.ro_rt
== NULL
) {
225 RT_LOCK(sc
->gif_ro6
.ro_rt
);
226 /* if it constitutes infinite encapsulation, punt. */
227 if (sc
->gif_ro6
.ro_rt
->rt_ifp
== ifp
) {
228 RT_UNLOCK(sc
->gif_ro6
.ro_rt
);
230 return ENETUNREACH
; /*XXX*/
233 ifp
->if_mtu
= sc
->gif_ro6
.ro_rt
->rt_ifp
->if_mtu
234 - sizeof(struct ip6_hdr
);
236 RT_UNLOCK(sc
->gif_ro6
.ro_rt
);
241 * force fragmentation to minimum MTU, to avoid path MTU discovery.
242 * it is too painful to ask for resend of inner packet, to achieve
243 * path MTU discovery for encapsulated packets.
245 return(ip6_output(m
, 0, &sc
->gif_ro6
, IPV6_MINMTU
, 0, NULL
, NULL
));
247 return(ip6_output(m
, 0, &sc
->gif_ro6
, 0, 0, NULL
, NULL
));
251 int in6_gif_input(struct mbuf
**mp
, int *offp
, int proto
)
253 struct mbuf
*m
= *mp
;
254 struct ifnet
*gifp
= NULL
;
259 ip6
= mtod(m
, struct ip6_hdr
*);
261 gifp
= ((struct gif_softc
*)encap_getarg(m
))->gif_if
;
263 if (gifp
== NULL
|| (gifp
->if_flags
& IFF_UP
) == 0) {
265 ip6stat
.ip6s_nogif
++;
269 otos
= ip6
->ip6_flow
;
279 otos8
= (ntohl(otos
) >> 20) & 0xff;
280 if (mbuf_len(m
) < sizeof(*ip
)) {
281 m
= m_pullup(m
, sizeof(*ip
));
285 ip
= mtod(m
, struct ip
*);
286 if (gifp
->if_flags
& IFF_LINK1
)
287 ip_ecn_egress(ECN_ALLOWED
, &otos8
, &ip
->ip_tos
);
289 ip_ecn_egress(ECN_NOCARE
, &otos8
, &ip
->ip_tos
);
297 if (mbuf_len(m
) < sizeof(*ip6
)) {
298 m
= m_pullup(m
, sizeof(*ip6
));
302 ip6
= mtod(m
, struct ip6_hdr
*);
303 if (gifp
->if_flags
& IFF_LINK1
)
304 ip6_ecn_egress(ECN_ALLOWED
, &otos
, &ip6
->ip6_flow
);
306 ip6_ecn_egress(ECN_NOCARE
, &otos
, &ip6
->ip6_flow
);
311 ip6stat
.ip6s_nogif
++;
316 if (m
->m_pkthdr
.rcvif
) /* replace the rcvif by gifp for ifnet_input to route it correctly */
317 m
->m_pkthdr
.rcvif
= gifp
;
319 ifnet_input(gifp
, m
, NULL
);
324 * validate outer address.
328 const struct ip6_hdr
*ip6
,
329 struct gif_softc
*sc
,
332 struct sockaddr_in6
*src
, *dst
;
334 src
= (struct sockaddr_in6
*)(void *)sc
->gif_psrc
;
335 dst
= (struct sockaddr_in6
*)(void *)sc
->gif_pdst
;
338 * Check for address match. Note that the check is for an incoming
339 * packet. We should compare the *source* address in our configuration
340 * and the *destination* address of the packet, and vice versa.
342 if (!IN6_ARE_ADDR_EQUAL(&src
->sin6_addr
, &ip6
->ip6_dst
) ||
343 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_src
))
346 /* martian filters on outer source - done in ip6_input */
348 /* ingress filters on outer source */
349 if ((ifnet_flags(sc
->gif_if
) & IFF_LINK2
) == 0 && ifp
) {
350 struct sockaddr_in6 sin6
;
353 bzero(&sin6
, sizeof(sin6
));
354 sin6
.sin6_family
= AF_INET6
;
355 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
356 sin6
.sin6_addr
= ip6
->ip6_src
;
358 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
361 if (!rt
|| rt
->rt_ifp
!= ifp
) {
363 log(LOG_WARNING
, "%s: packet from %s dropped "
364 "due to ingress filter\n", if_name(&sc
->gif_if
),
365 ip6_sprintf(&sin6
.sin6_addr
));
381 * we know that we are in IFF_UP, outer address available, and outer family
382 * matched the physical addr family. see gif_encapcheck().
383 * sanity check for arg should have been done in the caller.
387 const struct mbuf
*m
,
393 struct gif_softc
*sc
;
396 /* sanity check done in caller */
397 sc
= (struct gif_softc
*)arg
;
399 mbuf_copydata((struct mbuf
*)(size_t)m
, 0, sizeof(ip6
), &ip6
);
400 ifp
= ((m
->m_flags
& M_PKTHDR
) != 0) ? m
->m_pkthdr
.rcvif
: NULL
;
402 return gif_validate6(&ip6
, sc
, ifp
);