]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_gif.c
1 /* $FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.3 2001/07/03 11:01:52 ume Exp $ */
2 /* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
39 #include <sys/errno.h>
40 #include <sys/queue.h>
41 #include <sys/syslog.h>
43 #include <sys/malloc.h>
44 #include <sys/protosw.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
54 #include <netinet/ip_encap.h>
56 #include <netinet/ip6.h>
57 #include <netinet6/ip6_var.h>
58 #include <netinet6/in6_gif.h>
59 #include <netinet6/in6_var.h>
61 #include <netinet/ip_ecn.h>
63 #include <netinet6/ip6_ecn.h>
66 #include <net/if_gif.h>
68 #include <net/net_osdep.h>
73 int family
, /* family of the packet to be encapsulate. */
77 struct gif_softc
*sc
= (struct gif_softc
*)ifp
;
78 struct sockaddr_in6
*dst
= (struct sockaddr_in6
*)&sc
->gif_ro6
.ro_dst
;
79 struct sockaddr_in6
*sin6_src
= (struct sockaddr_in6
*)sc
->gif_psrc
;
80 struct sockaddr_in6
*sin6_dst
= (struct sockaddr_in6
*)sc
->gif_pdst
;
85 if (sin6_src
== NULL
|| sin6_dst
== NULL
||
86 sin6_src
->sin6_family
!= AF_INET6
||
87 sin6_dst
->sin6_family
!= AF_INET6
) {
99 if (m
->m_len
< sizeof(*ip
)) {
100 m
= m_pullup(m
, sizeof(*ip
));
104 ip
= mtod(m
, struct ip
*);
113 proto
= IPPROTO_IPV6
;
114 if (m
->m_len
< sizeof(*ip6
)) {
115 m
= m_pullup(m
, sizeof(*ip6
));
119 ip6
= mtod(m
, struct ip6_hdr
*);
120 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
126 printf("in6_gif_output: warning: unknown family %d passed\n",
133 /* prepend new IP header */
134 M_PREPEND(m
, sizeof(struct ip6_hdr
), M_DONTWAIT
);
135 if (m
&& m
->m_len
< sizeof(struct ip6_hdr
))
136 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
138 printf("ENOBUFS in in6_gif_output %d\n", __LINE__
);
142 ip6
= mtod(m
, struct ip6_hdr
*);
144 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
145 ip6
->ip6_vfc
|= IPV6_VERSION
;
146 ip6
->ip6_plen
= htons((u_short
)m
->m_pkthdr
.len
);
147 ip6
->ip6_nxt
= proto
;
148 ip6
->ip6_hlim
= ip6_gif_hlim
;
149 ip6
->ip6_src
= sin6_src
->sin6_addr
;
150 /* bidirectional configured tunnel mode */
151 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst
->sin6_addr
))
152 ip6
->ip6_dst
= sin6_dst
->sin6_addr
;
157 if (ifp
->if_flags
& IFF_LINK1
)
158 ip_ecn_ingress(ECN_ALLOWED
, &otos
, &itos
);
160 ip_ecn_ingress(ECN_NOCARE
, &otos
, &itos
);
161 ip6
->ip6_flow
&= ~ntohl(0xff00000);
162 ip6
->ip6_flow
|= htonl((u_int32_t
)otos
<< 20);
164 if (dst
->sin6_family
!= sin6_dst
->sin6_family
||
165 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &sin6_dst
->sin6_addr
)) {
166 /* cache route doesn't match */
167 bzero(dst
, sizeof(*dst
));
168 dst
->sin6_family
= sin6_dst
->sin6_family
;
169 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
170 dst
->sin6_addr
= sin6_dst
->sin6_addr
;
171 if (sc
->gif_ro6
.ro_rt
) {
172 rtfree(sc
->gif_ro6
.ro_rt
);
173 sc
->gif_ro6
.ro_rt
= NULL
;
176 sc
->gif_if
.if_mtu
= GIF_MTU
;
180 if (sc
->gif_ro6
.ro_rt
== NULL
) {
181 rtalloc((struct route
*)&sc
->gif_ro6
);
182 if (sc
->gif_ro6
.ro_rt
== NULL
) {
187 /* if it constitutes infinite encapsulation, punt. */
188 if (sc
->gif_ro
.ro_rt
->rt_ifp
== ifp
) {
190 return ENETUNREACH
; /*XXX*/
193 ifp
->if_mtu
= sc
->gif_ro6
.ro_rt
->rt_ifp
->if_mtu
194 - sizeof(struct ip6_hdr
);
200 * force fragmentation to minimum MTU, to avoid path MTU discovery.
201 * it is too painful to ask for resend of inner packet, to achieve
202 * path MTU discovery for encapsulated packets.
204 return(ip6_output(m
, 0, &sc
->gif_ro6
, IPV6_MINMTU
, 0, NULL
, 0));
206 return(ip6_output(m
, 0, &sc
->gif_ro6
, 0, 0, NULL
, 0));
210 int in6_gif_input(mp
, offp
)
214 struct mbuf
*m
= *mp
;
215 struct ifnet
*gifp
= NULL
;
221 ip6
= mtod(m
, struct ip6_hdr
*);
223 gifp
= (struct ifnet
*)encap_getarg(m
);
225 if (gifp
== NULL
|| (gifp
->if_flags
& IFF_UP
) == 0) {
227 ip6stat
.ip6s_nogif
++;
231 proto
= ip6
->ip6_nxt
;
232 otos
= ip6
->ip6_flow
;
242 otos8
= (ntohl(otos
) >> 20) & 0xff;
243 if (m
->m_len
< sizeof(*ip
)) {
244 m
= m_pullup(m
, sizeof(*ip
));
248 ip
= mtod(m
, struct ip
*);
249 if (gifp
->if_flags
& IFF_LINK1
)
250 ip_ecn_egress(ECN_ALLOWED
, &otos8
, &ip
->ip_tos
);
252 ip_ecn_egress(ECN_NOCARE
, &otos8
, &ip
->ip_tos
);
261 if (m
->m_len
< sizeof(*ip6
)) {
262 m
= m_pullup(m
, sizeof(*ip6
));
266 ip6
= mtod(m
, struct ip6_hdr
*);
267 if (gifp
->if_flags
& IFF_LINK1
)
268 ip6_ecn_egress(ECN_ALLOWED
, &otos
, &ip6
->ip6_flow
);
270 ip6_ecn_egress(ECN_NOCARE
, &otos
, &ip6
->ip6_flow
);
275 ip6stat
.ip6s_nogif
++;
280 dlil_input(gifp
, m
, m
);
285 * validate outer address.
288 gif_validate6(ip6
, sc
, ifp
)
289 const struct ip6_hdr
*ip6
;
290 struct gif_softc
*sc
;
293 struct sockaddr_in6
*src
, *dst
;
295 src
= (struct sockaddr_in6
*)sc
->gif_psrc
;
296 dst
= (struct sockaddr_in6
*)sc
->gif_pdst
;
299 * Check for address match. Note that the check is for an incoming
300 * packet. We should compare the *source* address in our configuration
301 * and the *destination* address of the packet, and vice versa.
303 if (!IN6_ARE_ADDR_EQUAL(&src
->sin6_addr
, &ip6
->ip6_dst
) ||
304 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_src
))
307 /* martian filters on outer source - done in ip6_input */
309 /* ingress filters on outer source */
310 if ((sc
->gif_if
.if_flags
& IFF_LINK2
) == 0 && ifp
) {
311 struct sockaddr_in6 sin6
;
314 bzero(&sin6
, sizeof(sin6
));
315 sin6
.sin6_family
= AF_INET6
;
316 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
317 sin6
.sin6_addr
= ip6
->ip6_src
;
318 #ifndef SCOPEDROUTING
319 sin6
.sin6_scope_id
= 0; /* XXX */
322 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
323 if (!rt
|| rt
->rt_ifp
!= ifp
) {
325 log(LOG_WARNING
, "%s: packet from %s dropped "
326 "due to ingress filter\n", if_name(&sc
->gif_if
),
327 ip6_sprintf(&sin6
.sin6_addr
));
340 * we know that we are in IFF_UP, outer address available, and outer family
341 * matched the physical addr family. see gif_encapcheck().
342 * sanity check for arg should have been done in the caller.
345 gif_encapcheck6(m
, off
, proto
, arg
)
346 const struct mbuf
*m
;
352 struct gif_softc
*sc
;
355 /* sanity check done in caller */
356 sc
= (struct gif_softc
*)arg
;
358 /* LINTED const cast */
359 m_copydata(m
, 0, sizeof(ip6
), (caddr_t
)&ip6
);
360 ifp
= ((m
->m_flags
& M_PKTHDR
) != 0) ? m
->m_pkthdr
.rcvif
: NULL
;
362 return gif_validate6(&ip6
, sc
, ifp
);