1 /* $NetBSD: in6_gif.c,v 1.57 2008/11/07 00:20:18 dyoung Exp $ */
2 /* $KAME: in6_gif.c,v 1.62 2001/07/29 04:27:25 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
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: in6_gif.c,v 1.57 2008/11/07 00:20:18 dyoung Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/queue.h>
47 #include <sys/syslog.h>
48 #include <sys/protosw.h>
49 #include <sys/kernel.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
59 #include <netinet/ip_encap.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/ip6_private.h>
64 #include <netinet6/in6_gif.h>
65 #include <netinet6/in6_var.h>
67 #include <netinet6/ip6protosw.h>
68 #include <netinet/ip_ecn.h>
70 #include <net/if_gif.h>
72 #include <net/net_osdep.h>
74 static int gif_validate6(const struct ip6_hdr
*, struct gif_softc
*,
77 int ip6_gif_hlim
= GIF_HLIM
;
79 extern LIST_HEAD(, gif_softc
) gif_softc_list
;
81 extern const struct ip6protosw in6_gif_protosw
;
84 * family - family of the packet to be encapsulate.
88 in6_gif_output(struct ifnet
*ifp
, int family
, struct mbuf
*m
)
91 struct gif_softc
*sc
= ifp
->if_softc
;
92 struct sockaddr_in6
*sin6_src
= (struct sockaddr_in6
*)sc
->gif_psrc
;
93 struct sockaddr_in6
*sin6_dst
= (struct sockaddr_in6
*)sc
->gif_pdst
;
99 struct sockaddr_in6 dst6
;
102 if (sin6_src
== NULL
|| sin6_dst
== NULL
||
103 sin6_src
->sin6_family
!= AF_INET6
||
104 sin6_dst
->sin6_family
!= AF_INET6
) {
115 proto
= IPPROTO_IPV4
;
116 if (m
->m_len
< sizeof(*ip
)) {
117 m
= m_pullup(m
, sizeof(*ip
));
121 ip
= mtod(m
, struct ip
*);
129 proto
= IPPROTO_IPV6
;
130 if (m
->m_len
< sizeof(*ip6
)) {
131 m
= m_pullup(m
, sizeof(*ip6
));
135 ip6
= mtod(m
, struct ip6_hdr
*);
136 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
148 printf("in6_gif_output: warning: unknown family %d passed\n",
155 /* prepend new IP header */
156 M_PREPEND(m
, sizeof(struct ip6_hdr
), M_DONTWAIT
);
157 if (m
&& m
->m_len
< sizeof(struct ip6_hdr
))
158 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
162 ip6
= mtod(m
, struct ip6_hdr
*);
164 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
165 ip6
->ip6_vfc
|= IPV6_VERSION
;
166 #if 0 /* ip6->ip6_plen will be filled by ip6_output */
167 ip6
->ip6_plen
= htons((u_int16_t
)m
->m_pkthdr
.len
);
169 ip6
->ip6_nxt
= proto
;
170 ip6
->ip6_hlim
= ip6_gif_hlim
;
171 ip6
->ip6_src
= sin6_src
->sin6_addr
;
172 /* bidirectional configured tunnel mode */
173 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst
->sin6_addr
))
174 ip6
->ip6_dst
= sin6_dst
->sin6_addr
;
179 if (ifp
->if_flags
& IFF_LINK1
)
180 ip_ecn_ingress(ECN_ALLOWED
, &otos
, &itos
);
182 ip_ecn_ingress(ECN_NOCARE
, &otos
, &itos
);
183 ip6
->ip6_flow
&= ~ntohl(0xff00000);
184 ip6
->ip6_flow
|= htonl((u_int32_t
)otos
<< 20);
186 sockaddr_in6_init(&u
.dst6
, &sin6_dst
->sin6_addr
, 0, 0, 0);
187 if ((rt
= rtcache_lookup(&sc
->gif_ro
, &u
.dst
)) == NULL
) {
192 /* If the route constitutes infinite encapsulation, punt. */
193 if (rt
->rt_ifp
== ifp
) {
195 return ENETUNREACH
; /* XXX */
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 error
= ip6_output(m
, 0, &sc
->gif_ro
, IPV6_MINMTU
, NULL
, NULL
, NULL
);
206 error
= ip6_output(m
, 0, &sc
->gif_ro
, 0, NULL
, NULL
, NULL
);
213 in6_gif_input(struct mbuf
**mp
, int *offp
, int proto
)
215 struct mbuf
*m
= *mp
;
216 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 IP6_STATINC(IP6_STAT_NOGIF
);
230 #ifndef GIF_ENCAPCHECK
231 if (!gif_validate6(ip6
, gifp
->if_softc
, m
->m_pkthdr
.rcvif
)) {
233 IP6_STATINC(IP6_STAT_NOGIF
);
238 otos
= ip6
->ip6_flow
;
248 otos8
= (ntohl(otos
) >> 20) & 0xff;
249 if (m
->m_len
< sizeof(*ip
)) {
250 m
= m_pullup(m
, sizeof(*ip
));
254 ip
= mtod(m
, struct ip
*);
255 if (gifp
->if_flags
& IFF_LINK1
)
256 ip_ecn_egress(ECN_ALLOWED
, &otos8
, &ip
->ip_tos
);
258 ip_ecn_egress(ECN_NOCARE
, &otos8
, &ip
->ip_tos
);
265 struct ip6_hdr
*ip6x
;
267 if (m
->m_len
< sizeof(*ip6x
)) {
268 m
= m_pullup(m
, sizeof(*ip6x
));
272 ip6x
= mtod(m
, struct ip6_hdr
*);
273 if (gifp
->if_flags
& IFF_LINK1
)
274 ip6_ecn_egress(ECN_ALLOWED
, &otos
, &ip6x
->ip6_flow
);
276 ip6_ecn_egress(ECN_NOCARE
, &otos
, &ip6x
->ip6_flow
);
286 IP6_STATINC(IP6_STAT_NOGIF
);
291 gif_input(m
, af
, gifp
);
296 * validate outer address.
299 gif_validate6(const struct ip6_hdr
*ip6
, struct gif_softc
*sc
,
302 const struct sockaddr_in6
*src
, *dst
;
304 src
= (struct sockaddr_in6
*)sc
->gif_psrc
;
305 dst
= (struct sockaddr_in6
*)sc
->gif_pdst
;
307 /* check for address match */
308 if (!IN6_ARE_ADDR_EQUAL(&src
->sin6_addr
, &ip6
->ip6_dst
) ||
309 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_src
))
312 /* martian filters on outer source - done in ip6_input */
314 /* ingress filters on outer source */
315 if ((sc
->gif_if
.if_flags
& IFF_LINK2
) == 0 && ifp
) {
318 struct sockaddr_in6 sin6
;
323 sockaddr_in6_init(&u
.sin6
, &ip6
->ip6_src
, 0, 0, 0);
324 rt
= rtalloc1(&u
.sa
, 0);
325 if (rt
== NULL
|| rt
->rt_ifp
!= ifp
) {
327 log(LOG_WARNING
, "%s: packet from %s dropped "
328 "due to ingress filter\n", if_name(&sc
->gif_if
),
329 ip6_sprintf(&u
.sin6
.sin6_addr
));
341 #ifdef GIF_ENCAPCHECK
343 * we know that we are in IFF_UP, outer address available, and outer family
344 * matched the physical addr family. see gif_encapcheck().
347 gif_encapcheck6(struct mbuf
*m
, int off
, int proto
, void *arg
)
350 struct gif_softc
*sc
;
353 /* sanity check done in caller */
356 m_copydata(m
, 0, sizeof(ip6
), (void *)&ip6
);
357 ifp
= ((m
->m_flags
& M_PKTHDR
) != 0) ? m
->m_pkthdr
.rcvif
: NULL
;
359 return gif_validate6(&ip6
, sc
, ifp
);
364 in6_gif_attach(struct gif_softc
*sc
)
366 #ifndef GIF_ENCAPCHECK
367 struct sockaddr_in6 mask6
;
369 memset(&mask6
, 0, sizeof(mask6
));
370 mask6
.sin6_len
= sizeof(struct sockaddr_in6
);
371 mask6
.sin6_addr
.s6_addr32
[0] = mask6
.sin6_addr
.s6_addr32
[1] =
372 mask6
.sin6_addr
.s6_addr32
[2] = mask6
.sin6_addr
.s6_addr32
[3] = ~0;
374 if (!sc
->gif_psrc
|| !sc
->gif_pdst
)
376 sc
->encap_cookie6
= encap_attach(AF_INET6
, -1, sc
->gif_psrc
,
377 (struct sockaddr
*)&mask6
, sc
->gif_pdst
, (struct sockaddr
*)&mask6
,
378 (const void *)&in6_gif_protosw
, sc
);
380 sc
->encap_cookie6
= encap_attach_func(AF_INET6
, -1, gif_encapcheck
,
381 (struct protosw
*)&in6_gif_protosw
, sc
);
383 if (sc
->encap_cookie6
== NULL
)
389 in6_gif_detach(struct gif_softc
*sc
)
393 error
= encap_detach(sc
->encap_cookie6
);
395 sc
->encap_cookie6
= NULL
;
397 rtcache_free(&sc
->gif_ro
);
403 in6_gif_ctlinput(int cmd
, const struct sockaddr
*sa
, void *d
)
405 struct gif_softc
*sc
;
406 struct ip6ctlparam
*ip6cp
= NULL
;
408 const struct sockaddr_in6
*dst6
;
410 if (sa
->sa_family
!= AF_INET6
||
411 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
414 if ((unsigned)cmd
>= PRC_NCMDS
)
416 if (cmd
== PRC_HOSTDEAD
)
418 else if (inet6ctlerrmap
[cmd
] == 0)
421 /* if the parameter is from icmp6, decode it. */
423 ip6cp
= (struct ip6ctlparam
*)d
;
424 ip6
= ip6cp
->ip6c_ip6
;
433 * for now we don't care which type it was, just flush the route cache.
434 * XXX slow. sc (or sc->encap_cookie6) should be passed from
437 LIST_FOREACH(sc
, &gif_softc_list
, gif_list
) {
438 if ((sc
->gif_if
.if_flags
& IFF_RUNNING
) == 0)
440 if (sc
->gif_psrc
->sa_family
!= AF_INET6
)
443 dst6
= satocsin6(rtcache_getdst(&sc
->gif_ro
));
447 else if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &dst6
->sin6_addr
))
448 rtcache_free(&sc
->gif_ro
);
454 PR_WRAP_CTLINPUT(in6_gif_ctlinput
)
455 PR_WRAP_CTLOUTPUT(rip6_ctloutput
)
456 PR_WRAP_USRREQ(rip6_usrreq
)
458 #define in6_gif_ctlinput in6_gif_ctlinput_wrapper
459 #define rip6_ctloutput rip6_ctloutput_wrapper
460 #define rip6_usrreq rip6_usrreq_wrapper
462 extern struct domain inet6domain
;
463 const struct ip6protosw in6_gif_protosw
=
464 { SOCK_RAW
, &inet6domain
, 0/* IPPROTO_IPV[46] */, PR_ATOMIC
|PR_ADDR
,
465 in6_gif_input
, rip6_output
, in6_gif_ctlinput
, rip6_ctloutput
,