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>
44 #include <sys/protosw.h>
46 #include <sys/malloc.h>
49 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
56 #include <netinet/ip_encap.h>
58 #include <netinet/ip6.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/in6_gif.h>
61 #include <netinet6/in6_var.h>
63 #include <netinet6/ip6protosw.h>
64 #include <netinet/ip_ecn.h>
66 #include <netinet6/ip6_ecn.h>
69 #include <net/if_gif.h>
71 #include <net/net_osdep.h>
73 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
76 extern struct domain inet6domain;
77 struct ip6protosw in6_gif_protosw =
78 { SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
79 in6_gif_input, rip6_output, 0, rip6_ctloutput,
86 in6_gif_output(ifp, family, m)
88 int family; /* family of the packet to be encapsulate. */
91 struct gif_softc *sc = ifp->if_softc;
92 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
93 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
94 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
96 struct etherip_header eiphdr;
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 *);
130 proto = IPPROTO_IPV6;
131 if (m->m_len < sizeof(*ip6)) {
132 m = m_pullup(m, sizeof(*ip6));
136 ip6 = mtod(m, struct ip6_hdr *);
137 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
142 proto = IPPROTO_ETHERIP;
143 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
145 /* prepend Ethernet-in-IP header */
146 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
147 if (m && m->m_len < sizeof(struct etherip_header))
148 m = m_pullup(m, sizeof(struct etherip_header));
151 bcopy(&eiphdr, mtod(m, struct etherip_header *),
152 sizeof(struct etherip_header));
157 printf("in6_gif_output: warning: unknown family %d passed\n",
164 /* prepend new IP header */
165 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
166 if (m && m->m_len < sizeof(struct ip6_hdr))
167 m = m_pullup(m, sizeof(struct ip6_hdr));
169 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
173 ip6 = mtod(m, struct ip6_hdr *);
175 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
176 ip6->ip6_vfc |= IPV6_VERSION;
177 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
178 ip6->ip6_nxt = proto;
179 ip6->ip6_hlim = ip6_gif_hlim;
180 ip6->ip6_src = sin6_src->sin6_addr;
181 /* bidirectional configured tunnel mode */
182 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
183 ip6->ip6_dst = sin6_dst->sin6_addr;
188 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
190 ip6->ip6_flow &= ~htonl(0xff << 20);
191 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
193 if (dst->sin6_family != sin6_dst->sin6_family ||
194 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
195 /* cache route doesn't match */
196 bzero(dst, sizeof(*dst));
197 dst->sin6_family = sin6_dst->sin6_family;
198 dst->sin6_len = sizeof(struct sockaddr_in6);
199 dst->sin6_addr = sin6_dst->sin6_addr;
200 if (sc->gif_ro6.ro_rt) {
201 RTFREE(sc->gif_ro6.ro_rt);
202 sc->gif_ro6.ro_rt = NULL;
205 GIF2IFP(sc)->if_mtu = GIF_MTU;
209 if (sc->gif_ro6.ro_rt == NULL) {
210 rtalloc((struct route *)&sc->gif_ro6);
211 if (sc->gif_ro6.ro_rt == NULL) {
216 /* if it constitutes infinite encapsulation, punt. */
217 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
219 return ENETUNREACH; /*XXX*/
222 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
223 - sizeof(struct ip6_hdr);
229 * force fragmentation to minimum MTU, to avoid path MTU discovery.
230 * it is too painful to ask for resend of inner packet, to achieve
231 * path MTU discovery for encapsulated packets.
233 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
235 error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
238 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
239 sc->gif_ro6.ro_rt != NULL) {
240 RTFREE(sc->gif_ro6.ro_rt);
241 sc->gif_ro6.ro_rt = NULL;
248 in6_gif_input(mp, offp, proto)
252 struct mbuf *m = *mp;
253 struct ifnet *gifp = NULL;
254 struct gif_softc *sc;
259 ip6 = mtod(m, struct ip6_hdr *);
261 sc = (struct gif_softc *)encap_getarg(m);
264 ip6stat.ip6s_nogif++;
269 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
271 ip6stat.ip6s_nogif++;
275 otos = ip6->ip6_flow;
285 otos8 = (ntohl(otos) >> 20) & 0xff;
286 if (m->m_len < sizeof(*ip)) {
287 m = m_pullup(m, sizeof(*ip));
291 ip = mtod(m, struct ip *);
292 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
293 ECN_ALLOWED : ECN_NOCARE,
294 &otos8, &ip->ip_tos) == 0) {
306 if (m->m_len < sizeof(*ip6)) {
307 m = m_pullup(m, sizeof(*ip6));
311 ip6 = mtod(m, struct ip6_hdr *);
312 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
313 ECN_ALLOWED : ECN_NOCARE,
314 &otos, &ip6->ip6_flow) == 0) {
321 case IPPROTO_ETHERIP:
326 ip6stat.ip6s_nogif++;
331 gif_input(m, af, gifp);
336 * validate outer address.
339 gif_validate6(ip6, sc, ifp)
340 const struct ip6_hdr *ip6;
341 struct gif_softc *sc;
344 struct sockaddr_in6 *src, *dst;
346 src = (struct sockaddr_in6 *)sc->gif_psrc;
347 dst = (struct sockaddr_in6 *)sc->gif_pdst;
350 * Check for address match. Note that the check is for an incoming
351 * packet. We should compare the *source* address in our configuration
352 * and the *destination* address of the packet, and vice versa.
354 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
355 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
358 /* martian filters on outer source - done in ip6_input */
360 /* ingress filters on outer source */
361 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
362 struct sockaddr_in6 sin6;
365 bzero(&sin6, sizeof(sin6));
366 sin6.sin6_family = AF_INET6;
367 sin6.sin6_len = sizeof(struct sockaddr_in6);
368 sin6.sin6_addr = ip6->ip6_src;
369 sin6.sin6_scope_id = 0; /* XXX */
371 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
372 if (!rt || rt->rt_ifp != ifp) {
374 log(LOG_WARNING, "%s: packet from %s dropped "
375 "due to ingress filter\n", if_name(GIF2IFP(sc)),
376 ip6_sprintf(&sin6.sin6_addr));
389 * we know that we are in IFF_UP, outer address available, and outer family
390 * matched the physical addr family. see gif_encapcheck().
391 * sanity check for arg should have been done in the caller.
394 gif_encapcheck6(m, off, proto, arg)
395 const struct mbuf *m;
401 struct gif_softc *sc;
404 /* sanity check done in caller */
405 sc = (struct gif_softc *)arg;
407 /* LINTED const cast */
408 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
409 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
411 return gif_validate6(&ip6, sc, ifp);
416 struct gif_softc *sc;
418 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
419 (void *)&in6_gif_protosw, sc);
420 if (sc->encap_cookie6 == NULL)
427 struct gif_softc *sc;
431 error = encap_detach(sc->encap_cookie6);
433 sc->encap_cookie6 = NULL;