1 /* $FreeBSD$ */ 2 /* $KAME: in6_gif.c,v 1.37 2000/06/17 20:34:25 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * in6_gif.c 35 */ 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/socket.h> 43 #include <sys/sockio.h> 44 #include <sys/mbuf.h> 45 #include <sys/errno.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #ifdef INET 53 #include <netinet/ip.h> 54 #endif 55 #include <netinet/ip_encap.h> 56 #ifdef INET6 57 #include <netinet/ip6.h> 58 #include <netinet6/ip6_var.h> 59 #include <netinet6/in6_gif.h> 60 #include <netinet6/in6_var.h> 61 #endif 62 #include <netinet/ip_ecn.h> 63 #ifdef INET6 64 #include <netinet6/ip6_ecn.h> 65 #endif 66 67 #include <net/if_gif.h> 68 69 #include <net/net_osdep.h> 70 71 int 72 in6_gif_output(ifp, family, m, rt) 73 struct ifnet *ifp; 74 int family; /* family of the packet to be encapsulate. */ 75 struct mbuf *m; 76 struct rtentry *rt; 77 { 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; 82 struct ip6_hdr *ip6; 83 int proto; 84 u_int8_t itos, otos; 85 86 if (sin6_src == NULL || sin6_dst == NULL || 87 sin6_src->sin6_family != AF_INET6 || 88 sin6_dst->sin6_family != AF_INET6) { 89 m_freem(m); 90 return EAFNOSUPPORT; 91 } 92 93 switch (family) { 94 #ifdef INET 95 case AF_INET: 96 { 97 struct ip *ip; 98 99 proto = IPPROTO_IPV4; 100 if (m->m_len < sizeof(*ip)) { 101 m = m_pullup(m, sizeof(*ip)); 102 if (!m) 103 return ENOBUFS; 104 } 105 ip = mtod(m, struct ip *); 106 itos = ip->ip_tos; 107 break; 108 } 109 #endif 110 #ifdef INET6 111 case AF_INET6: 112 { 113 struct ip6_hdr *ip6; 114 proto = IPPROTO_IPV6; 115 if (m->m_len < sizeof(*ip6)) { 116 m = m_pullup(m, sizeof(*ip6)); 117 if (!m) 118 return ENOBUFS; 119 } 120 ip6 = mtod(m, struct ip6_hdr *); 121 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 122 break; 123 } 124 #endif 125 default: 126 #ifdef DEBUG 127 printf("in6_gif_output: warning: unknown family %d passed\n", 128 family); 129 #endif 130 m_freem(m); 131 return EAFNOSUPPORT; 132 } 133 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)); 138 if (m == NULL) { 139 printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 140 return ENOBUFS; 141 } 142 143 ip6 = mtod(m, struct ip6_hdr *); 144 ip6->ip6_flow = 0; 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 if (ifp->if_flags & IFF_LINK0) { 152 /* multi-destination mode */ 153 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 154 ip6->ip6_dst = sin6_dst->sin6_addr; 155 else if (rt) { 156 if (family != AF_INET6) { 157 m_freem(m); 158 return EINVAL; /*XXX*/ 159 } 160 ip6->ip6_dst = ((struct sockaddr_in6 *)(rt->rt_gateway))->sin6_addr; 161 } else { 162 m_freem(m); 163 return ENETUNREACH; 164 } 165 } else { 166 /* bidirectional configured tunnel mode */ 167 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 168 ip6->ip6_dst = sin6_dst->sin6_addr; 169 else { 170 m_freem(m); 171 return ENETUNREACH; 172 } 173 } 174 if (ifp->if_flags & IFF_LINK1) { 175 otos = 0; 176 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos); 177 ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 178 } 179 180 if (dst->sin6_family != sin6_dst->sin6_family || 181 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 182 /* cache route doesn't match */ 183 bzero(dst, sizeof(*dst)); 184 dst->sin6_family = sin6_dst->sin6_family; 185 dst->sin6_len = sizeof(struct sockaddr_in6); 186 dst->sin6_addr = sin6_dst->sin6_addr; 187 if (sc->gif_ro6.ro_rt) { 188 RTFREE(sc->gif_ro6.ro_rt); 189 sc->gif_ro6.ro_rt = NULL; 190 } 191 #if 0 192 sc->gif_if.if_mtu = GIF_MTU; 193 #endif 194 } 195 196 if (sc->gif_ro6.ro_rt == NULL) { 197 rtalloc((struct route *)&sc->gif_ro6); 198 if (sc->gif_ro6.ro_rt == NULL) { 199 m_freem(m); 200 return ENETUNREACH; 201 } 202 203 /* if it constitutes infinite encapsulation, punt. */ 204 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 205 m_freem(m); 206 return ENETUNREACH; /*XXX*/ 207 } 208 #if 0 209 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 210 - sizeof(struct ip6_hdr); 211 #endif 212 } 213 214 #ifdef IPV6_MINMTU 215 /* 216 * force fragmentation to minimum MTU, to avoid path MTU discovery. 217 * it is too painful to ask for resend of inner packet, to achieve 218 * path MTU discovery for encapsulated packets. 219 */ 220 return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL)); 221 #else 222 return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL)); 223 #endif 224 } 225 226 int in6_gif_input(mp, offp, proto) 227 struct mbuf **mp; 228 int *offp, proto; 229 { 230 struct mbuf *m = *mp; 231 struct ifnet *gifp = NULL; 232 struct ip6_hdr *ip6; 233 int af = 0; 234 u_int32_t otos; 235 236 ip6 = mtod(m, struct ip6_hdr *); 237 238 gifp = (struct ifnet *)encap_getarg(m); 239 240 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 241 m_freem(m); 242 ip6stat.ip6s_nogif++; 243 return IPPROTO_DONE; 244 } 245 246 otos = ip6->ip6_flow; 247 m_adj(m, *offp); 248 249 switch (proto) { 250 #ifdef INET 251 case IPPROTO_IPV4: 252 { 253 struct ip *ip; 254 u_int8_t otos8; 255 af = AF_INET; 256 otos8 = (ntohl(otos) >> 20) & 0xff; 257 if (m->m_len < sizeof(*ip)) { 258 m = m_pullup(m, sizeof(*ip)); 259 if (!m) 260 return IPPROTO_DONE; 261 } 262 ip = mtod(m, struct ip *); 263 if (gifp->if_flags & IFF_LINK1) 264 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos); 265 break; 266 } 267 #endif /* INET */ 268 #ifdef INET6 269 case IPPROTO_IPV6: 270 { 271 struct ip6_hdr *ip6; 272 af = AF_INET6; 273 if (m->m_len < sizeof(*ip6)) { 274 m = m_pullup(m, sizeof(*ip6)); 275 if (!m) 276 return IPPROTO_DONE; 277 } 278 ip6 = mtod(m, struct ip6_hdr *); 279 if (gifp->if_flags & IFF_LINK1) 280 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow); 281 break; 282 } 283 #endif 284 default: 285 ip6stat.ip6s_nogif++; 286 m_freem(m); 287 return IPPROTO_DONE; 288 } 289 290 gif_input(m, af, gifp); 291 return IPPROTO_DONE; 292 } 293 294 /* 295 * we know that we are in IFF_UP, outer address available, and outer family 296 * matched the physical addr family. see gif_encapcheck(). 297 */ 298 int 299 gif_encapcheck6(m, off, proto, arg) 300 const struct mbuf *m; 301 int off; 302 int proto; 303 void *arg; 304 { 305 struct ip6_hdr ip6; 306 struct gif_softc *sc; 307 struct sockaddr_in6 *src, *dst; 308 int addrmatch; 309 310 /* sanity check done in caller */ 311 sc = (struct gif_softc *)arg; 312 src = (struct sockaddr_in6 *)sc->gif_psrc; 313 dst = (struct sockaddr_in6 *)sc->gif_pdst; 314 315 /* LINTED const cast */ 316 m_copydata((struct mbuf *)m, 0, sizeof(ip6), (caddr_t)&ip6); 317 318 /* check for address match */ 319 addrmatch = 0; 320 if (IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6.ip6_dst)) 321 addrmatch |= 1; 322 if (IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6.ip6_src)) 323 addrmatch |= 2; 324 else if ((sc->gif_if.if_flags & IFF_LINK0) != 0 && 325 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) { 326 addrmatch |= 2; /* we accept any source */ 327 } 328 if (addrmatch != 3) 329 return 0; 330 331 /* martian filters on outer source - done in ip6_input */ 332 333 /* ingress filters on outer source */ 334 if ((m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) { 335 struct sockaddr_in6 sin6; 336 struct rtentry *rt; 337 338 bzero(&sin6, sizeof(sin6)); 339 sin6.sin6_family = AF_INET6; 340 sin6.sin6_len = sizeof(struct sockaddr_in6); 341 sin6.sin6_addr = ip6.ip6_src; 342 /* XXX scopeid */ 343 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 344 if (!rt) 345 return 0; 346 if (rt->rt_ifp != m->m_pkthdr.rcvif) { 347 rtfree(rt); 348 return 0; 349 } 350 rtfree(rt); 351 } 352 353 /* prioritize: IFF_LINK0 mode is less preferred */ 354 return (sc->gif_if.if_flags & IFF_LINK0) ? 128 : 128 * 2; 355 } 356