]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_gif.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: in_gif.c,v 1.54 2001/05/14 14:02:16 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
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
59 #include <sys/errno.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
63 #include <sys/malloc.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/in_gif.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip_encap.h>
75 #include <netinet/ip_ecn.h>
78 #include <netinet/ip6.h>
82 #include <netinet/ip_mroute.h>
85 #include <net/if_gif.h>
87 #include <net/net_osdep.h>
89 int ip_gif_ttl
= GIF_TTL
;
90 SYSCTL_INT(_net_inet_ip
, IPCTL_GIF_TTL
, gifttl
, CTLFLAG_RW
,
100 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
101 struct sockaddr_in
*dst
= (struct sockaddr_in
*)&sc
->gif_ro
.ro_dst
;
102 struct sockaddr_in
*sin_src
= (struct sockaddr_in
*)sc
->gif_psrc
;
103 struct sockaddr_in
*sin_dst
= (struct sockaddr_in
*)sc
->gif_pdst
;
104 struct ip iphdr
; /* capsule IP header, host byte ordered */
108 if (sin_src
== NULL
|| sin_dst
== NULL
||
109 sin_src
->sin_family
!= AF_INET
||
110 sin_dst
->sin_family
!= AF_INET
) {
121 proto
= IPPROTO_IPV4
;
122 if (m
->m_len
< sizeof(*ip
)) {
123 m
= m_pullup(m
, sizeof(*ip
));
127 ip
= mtod(m
, struct ip
*);
136 proto
= IPPROTO_IPV6
;
137 if (m
->m_len
< sizeof(*ip6
)) {
138 m
= m_pullup(m
, sizeof(*ip6
));
142 ip6
= mtod(m
, struct ip6_hdr
*);
143 tos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
149 printf("in_gif_output: warning: unknown family %d passed\n",
156 bzero(&iphdr
, sizeof(iphdr
));
157 iphdr
.ip_src
= sin_src
->sin_addr
;
158 /* bidirectional configured tunnel mode */
159 if (sin_dst
->sin_addr
.s_addr
!= INADDR_ANY
)
160 iphdr
.ip_dst
= sin_dst
->sin_addr
;
166 /* version will be set in ip_output() */
167 iphdr
.ip_ttl
= ip_gif_ttl
;
168 iphdr
.ip_len
= m
->m_pkthdr
.len
+ sizeof(struct ip
);
169 if (ifp
->if_flags
& IFF_LINK1
)
170 ip_ecn_ingress(ECN_ALLOWED
, &iphdr
.ip_tos
, &tos
);
172 ip_ecn_ingress(ECN_NOCARE
, &iphdr
.ip_tos
, &tos
);
174 /* prepend new IP header */
175 M_PREPEND(m
, sizeof(struct ip
), M_DONTWAIT
);
176 if (m
&& m
->m_len
< sizeof(struct ip
))
177 m
= m_pullup(m
, sizeof(struct ip
));
179 printf("ENOBUFS in in_gif_output %d\n", __LINE__
);
182 bcopy(&iphdr
, mtod(m
, struct ip
*), sizeof(struct ip
));
184 if (dst
->sin_family
!= sin_dst
->sin_family
||
185 dst
->sin_addr
.s_addr
!= sin_dst
->sin_addr
.s_addr
) {
186 /* cache route doesn't match */
187 dst
->sin_family
= sin_dst
->sin_family
;
188 dst
->sin_len
= sizeof(struct sockaddr_in
);
189 dst
->sin_addr
= sin_dst
->sin_addr
;
190 if (sc
->gif_ro
.ro_rt
) {
191 rtfree(sc
->gif_ro
.ro_rt
);
192 sc
->gif_ro
.ro_rt
= NULL
;
195 sc
->gif_if
.if_mtu
= GIF_MTU
;
199 if (sc
->gif_ro
.ro_rt
== NULL
) {
200 rtalloc(&sc
->gif_ro
);
201 if (sc
->gif_ro
.ro_rt
== NULL
) {
206 /* if it constitutes infinite encapsulation, punt. */
207 if (sc
->gif_ro
.ro_rt
->rt_ifp
== ifp
) {
209 return ENETUNREACH
; /*XXX*/
212 ifp
->if_mtu
= sc
->gif_ro
.ro_rt
->rt_ifp
->if_mtu
217 error
= ip_output(m
, NULL
, &sc
->gif_ro
, 0, NULL
);
226 struct ifnet
*gifp
= NULL
;
231 ip
= mtod(m
, struct ip
*);
235 gifp
= (struct ifnet
*)encap_getarg(m
);
237 if (gifp
== NULL
|| (gifp
->if_flags
& IFF_UP
) == 0) {
252 if (m
->m_len
< sizeof(*ip
)) {
253 m
= m_pullup(m
, sizeof(*ip
));
257 ip
= mtod(m
, struct ip
*);
258 if (gifp
->if_flags
& IFF_LINK1
)
259 ip_ecn_egress(ECN_ALLOWED
, &otos
, &ip
->ip_tos
);
261 ip_ecn_egress(ECN_NOCARE
, &otos
, &ip
->ip_tos
);
271 if (m
->m_len
< sizeof(*ip6
)) {
272 m
= m_pullup(m
, sizeof(*ip6
));
276 ip6
= mtod(m
, struct ip6_hdr
*);
277 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
278 if (gifp
->if_flags
& IFF_LINK1
)
279 ip_ecn_egress(ECN_ALLOWED
, &otos
, &itos
);
281 ip_ecn_egress(ECN_NOCARE
, &otos
, &itos
);
282 ip6
->ip6_flow
&= ~htonl(0xff << 20);
283 ip6
->ip6_flow
|= htonl((u_int32_t
)itos
<< 20);
293 /* Should we free m if dlil_input returns an error? */
294 if (m
->m_pkthdr
.rcvif
) /* replace the rcvif by gifp for dlil to route it correctly */
295 m
->m_pkthdr
.rcvif
= gifp
;
296 dlil_input_packet(gifp
, m
, NULL
);
298 gif_input(m
, af
, gifp
);
304 * we know that we are in IFF_UP, outer address available, and outer family
305 * matched the physical addr family. see gif_encapcheck().
308 gif_encapcheck4(m
, off
, proto
, arg
)
309 const struct mbuf
*m
;
315 struct gif_softc
*sc
;
316 struct sockaddr_in
*src
, *dst
;
318 struct in_ifaddr
*ia4
;
320 /* sanity check done in caller */
321 sc
= (struct gif_softc
*)arg
;
322 src
= (struct sockaddr_in
*)sc
->gif_psrc
;
323 dst
= (struct sockaddr_in
*)sc
->gif_pdst
;
325 /* LINTED const cast */
326 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
328 /* check for address match */
330 if (src
->sin_addr
.s_addr
== ip
.ip_dst
.s_addr
)
332 if (dst
->sin_addr
.s_addr
== ip
.ip_src
.s_addr
)
337 /* martian filters on outer source - NOT done in ip_input! */
338 if (IN_MULTICAST(ntohl(ip
.ip_src
.s_addr
)))
340 switch ((ntohl(ip
.ip_src
.s_addr
) & 0xff000000) >> 24) {
341 case 0: case 127: case 255:
344 /* reject packets with broadcast on source */
345 lck_mtx_lock(rt_mtx
);
346 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
); ia4
;
347 ia4
= TAILQ_NEXT(ia4
, ia_link
))
349 if ((ia4
->ia_ifa
.ifa_ifp
->if_flags
& IFF_BROADCAST
) == 0)
351 if (ip
.ip_src
.s_addr
== ia4
->ia_broadaddr
.sin_addr
.s_addr
) {
352 lck_mtx_unlock(rt_mtx
);
356 lck_mtx_unlock(rt_mtx
);
358 /* ingress filters on outer source */
359 if ((sc
->gif_if
.if_flags
& IFF_LINK2
) == 0 &&
360 (m
->m_flags
& M_PKTHDR
) != 0 && m
->m_pkthdr
.rcvif
) {
361 struct sockaddr_in sin
;
364 bzero(&sin
, sizeof(sin
));
365 sin
.sin_family
= AF_INET
;
366 sin
.sin_len
= sizeof(struct sockaddr_in
);
367 sin
.sin_addr
= ip
.ip_src
;
368 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0UL);
369 if (!rt
|| rt
->rt_ifp
!= m
->m_pkthdr
.rcvif
) {
371 log(LOG_WARNING
, "%s: packet from 0x%x dropped "
372 "due to ingress filter\n", if_name(&sc
->gif_if
),
373 (u_int32_t
)ntohl(sin
.sin_addr
.s_addr
));