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 "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
41 #include <sys/errno.h>
42 #include <sys/queue.h>
43 #include <sys/syslog.h>
45 #include <sys/malloc.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
55 #include <netinet/ip_encap.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/in6_gif.h>
60 #include <netinet6/in6_var.h>
62 #include <netinet/ip_ecn.h>
64 #include <netinet6/ip6_ecn.h>
67 #include <net/if_gif.h>
69 #include <net/net_osdep.h>
72 in6_gif_output(ifp, family, m, rt)
74 int family; /* family of the packet to be encapsulate. */
78 struct gif_softc *sc = (struct gif_softc*)ifp;
79 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
80 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
81 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
86 if (sin6_src == NULL || sin6_dst == NULL ||
87 sin6_src->sin6_family != AF_INET6 ||
88 sin6_dst->sin6_family != AF_INET6) {
100 if (m->m_len < sizeof(*ip)) {
101 m = m_pullup(m, sizeof(*ip));
105 ip = mtod(m, struct ip *);
114 proto = IPPROTO_IPV6;
115 if (m->m_len < sizeof(*ip6)) {
116 m = m_pullup(m, sizeof(*ip6));
120 ip6 = mtod(m, struct ip6_hdr *);
121 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
127 printf("in6_gif_output: warning: unknown family %d passed\n",
134 /* prepend new IP header */
135 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
136 if (m && m->m_len < sizeof(struct ip6_hdr))
137 m = m_pullup(m, sizeof(struct ip6_hdr));
139 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
143 ip6 = mtod(m, struct ip6_hdr *);
145 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
146 ip6->ip6_vfc |= IPV6_VERSION;
147 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
148 ip6->ip6_nxt = proto;
149 ip6->ip6_hlim = ip6_gif_hlim;
150 ip6->ip6_src = sin6_src->sin6_addr;
151 /* bidirectional configured tunnel mode */
152 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
153 ip6->ip6_dst = sin6_dst->sin6_addr;
158 if (ifp->if_flags & IFF_LINK1)
159 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
161 ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
162 ip6->ip6_flow &= ~ntohl(0xff00000);
163 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
165 if (dst->sin6_family != sin6_dst->sin6_family ||
166 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
167 /* cache route doesn't match */
168 bzero(dst, sizeof(*dst));
169 dst->sin6_family = sin6_dst->sin6_family;
170 dst->sin6_len = sizeof(struct sockaddr_in6);
171 dst->sin6_addr = sin6_dst->sin6_addr;
172 if (sc->gif_ro6.ro_rt) {
173 RTFREE(sc->gif_ro6.ro_rt);
174 sc->gif_ro6.ro_rt = NULL;
177 sc->gif_if.if_mtu = GIF_MTU;
181 if (sc->gif_ro6.ro_rt == NULL) {
182 rtalloc((struct route *)&sc->gif_ro6);
183 if (sc->gif_ro6.ro_rt == NULL) {
188 /* if it constitutes infinite encapsulation, punt. */
189 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
191 return ENETUNREACH; /*XXX*/
194 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
195 - sizeof(struct ip6_hdr);
201 * force fragmentation to minimum MTU, to avoid path MTU discovery.
202 * it is too painful to ask for resend of inner packet, to achieve
203 * path MTU discovery for encapsulated packets.
205 return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL));
207 return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL));
211 int in6_gif_input(mp, offp, 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 ip6stat.ip6s_nogif++;
231 otos = ip6->ip6_flow;
241 otos8 = (ntohl(otos) >> 20) & 0xff;
242 if (m->m_len < sizeof(*ip)) {
243 m = m_pullup(m, sizeof(*ip));
247 ip = mtod(m, struct ip *);
248 if (gifp->if_flags & IFF_LINK1)
249 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
251 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
260 if (m->m_len < sizeof(*ip6)) {
261 m = m_pullup(m, sizeof(*ip6));
265 ip6 = mtod(m, struct ip6_hdr *);
266 if (gifp->if_flags & IFF_LINK1)
267 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
269 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
274 ip6stat.ip6s_nogif++;
279 gif_input(m, af, gifp);
284 * we know that we are in IFF_UP, outer address available, and outer family
285 * matched the physical addr family. see gif_encapcheck().
288 gif_encapcheck6(m, off, proto, arg)
289 const struct mbuf *m;
295 struct gif_softc *sc;
296 struct sockaddr_in6 *src, *dst;
299 /* sanity check done in caller */
300 sc = (struct gif_softc *)arg;
301 src = (struct sockaddr_in6 *)sc->gif_psrc;
302 dst = (struct sockaddr_in6 *)sc->gif_pdst;
304 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
306 /* check for address match */
308 if (IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6.ip6_dst))
310 if (IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6.ip6_src))
315 /* martian filters on outer source - done in ip6_input */
317 /* ingress filters on outer source */
318 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 &&
319 (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
320 struct sockaddr_in6 sin6;
323 bzero(&sin6, sizeof(sin6));
324 sin6.sin6_family = AF_INET6;
325 sin6.sin6_len = sizeof(struct sockaddr_in6);
326 sin6.sin6_addr = ip6.ip6_src;
328 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
329 if (!rt || rt->rt_ifp != m->m_pkthdr.rcvif) {
331 log(LOG_WARNING, "%s: packet from %s dropped "
332 "due to ingress filter\n", if_name(&sc->gif_if),
333 ip6_sprintf(&sin6.sin6_addr));