1 /* $FreeBSD$ */ 2 /* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 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 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/socket.h> 39 #include <sys/sockio.h> 40 #include <sys/mbuf.h> 41 #include <sys/errno.h> 42 #include <sys/queue.h> 43 #include <sys/syslog.h> 44 #include <sys/protosw.h> 45 46 #include <sys/malloc.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #ifdef INET 54 #include <netinet/ip.h> 55 #endif 56 #include <netinet/ip_encap.h> 57 #ifdef INET6 58 #include <netinet/ip6.h> 59 #include <netinet6/ip6_var.h> 60 #include <netinet6/in6_gif.h> 61 #include <netinet6/in6_var.h> 62 #endif 63 #include <netinet6/ip6protosw.h> 64 #include <netinet/ip_ecn.h> 65 #ifdef INET6 66 #include <netinet6/ip6_ecn.h> 67 #endif 68 69 #include <net/if_gif.h> 70 71 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 72 struct ifnet *); 73 74 extern struct domain inet6domain; 75 struct ip6protosw in6_gif_protosw = 76 { SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR, 77 in6_gif_input, rip6_output, 0, rip6_ctloutput, 78 0, 79 0, 0, 0, 0, 80 &rip6_usrreqs 81 }; 82 83 int 84 in6_gif_output(ifp, family, m) 85 struct ifnet *ifp; 86 int family; /* family of the packet to be encapsulate. */ 87 struct mbuf *m; 88 { 89 struct gif_softc *sc = ifp->if_softc; 90 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 91 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 92 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 93 struct ip6_hdr *ip6; 94 struct etherip_header eiphdr; 95 int proto, error; 96 u_int8_t itos, otos; 97 98 GIF_LOCK_ASSERT(sc); 99 100 if (sin6_src == NULL || sin6_dst == NULL || 101 sin6_src->sin6_family != AF_INET6 || 102 sin6_dst->sin6_family != AF_INET6) { 103 m_freem(m); 104 return EAFNOSUPPORT; 105 } 106 107 switch (family) { 108 #ifdef INET 109 case AF_INET: 110 { 111 struct ip *ip; 112 113 proto = IPPROTO_IPV4; 114 if (m->m_len < sizeof(*ip)) { 115 m = m_pullup(m, sizeof(*ip)); 116 if (!m) 117 return ENOBUFS; 118 } 119 ip = mtod(m, struct ip *); 120 itos = ip->ip_tos; 121 break; 122 } 123 #endif 124 #ifdef INET6 125 case AF_INET6: 126 { 127 struct ip6_hdr *ip6; 128 proto = IPPROTO_IPV6; 129 if (m->m_len < sizeof(*ip6)) { 130 m = m_pullup(m, sizeof(*ip6)); 131 if (!m) 132 return ENOBUFS; 133 } 134 ip6 = mtod(m, struct ip6_hdr *); 135 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 136 break; 137 } 138 #endif 139 case AF_LINK: 140 proto = IPPROTO_ETHERIP; 141 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK; 142 eiphdr.eip_pad = 0; 143 /* prepend Ethernet-in-IP header */ 144 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); 145 if (m && m->m_len < sizeof(struct etherip_header)) 146 m = m_pullup(m, sizeof(struct etherip_header)); 147 if (m == NULL) 148 return ENOBUFS; 149 bcopy(&eiphdr, mtod(m, struct etherip_header *), 150 sizeof(struct etherip_header)); 151 break; 152 153 default: 154 #ifdef DEBUG 155 printf("in6_gif_output: warning: unknown family %d passed\n", 156 family); 157 #endif 158 m_freem(m); 159 return EAFNOSUPPORT; 160 } 161 162 /* prepend new IP header */ 163 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); 164 if (m && m->m_len < sizeof(struct ip6_hdr)) 165 m = m_pullup(m, sizeof(struct ip6_hdr)); 166 if (m == NULL) { 167 printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 168 return ENOBUFS; 169 } 170 171 ip6 = mtod(m, struct ip6_hdr *); 172 ip6->ip6_flow = 0; 173 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 174 ip6->ip6_vfc |= IPV6_VERSION; 175 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 176 ip6->ip6_nxt = proto; 177 ip6->ip6_hlim = ip6_gif_hlim; 178 ip6->ip6_src = sin6_src->sin6_addr; 179 /* bidirectional configured tunnel mode */ 180 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 181 ip6->ip6_dst = sin6_dst->sin6_addr; 182 else { 183 m_freem(m); 184 return ENETUNREACH; 185 } 186 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 187 &otos, &itos); 188 ip6->ip6_flow &= ~htonl(0xff << 20); 189 ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 190 191 if (dst->sin6_family != sin6_dst->sin6_family || 192 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 193 /* cache route doesn't match */ 194 bzero(dst, sizeof(*dst)); 195 dst->sin6_family = sin6_dst->sin6_family; 196 dst->sin6_len = sizeof(struct sockaddr_in6); 197 dst->sin6_addr = sin6_dst->sin6_addr; 198 if (sc->gif_ro6.ro_rt) { 199 RTFREE(sc->gif_ro6.ro_rt); 200 sc->gif_ro6.ro_rt = NULL; 201 } 202 #if 0 203 GIF2IFP(sc)->if_mtu = GIF_MTU; 204 #endif 205 } 206 207 if (sc->gif_ro6.ro_rt == NULL) { 208 rtalloc((struct route *)&sc->gif_ro6); 209 if (sc->gif_ro6.ro_rt == NULL) { 210 m_freem(m); 211 return ENETUNREACH; 212 } 213 214 /* if it constitutes infinite encapsulation, punt. */ 215 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 216 m_freem(m); 217 return ENETUNREACH; /*XXX*/ 218 } 219 #if 0 220 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 221 - sizeof(struct ip6_hdr); 222 #endif 223 } 224 225 #ifdef IPV6_MINMTU 226 /* 227 * force fragmentation to minimum MTU, to avoid path MTU discovery. 228 * it is too painful to ask for resend of inner packet, to achieve 229 * path MTU discovery for encapsulated packets. 230 */ 231 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 232 #else 233 error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 234 #endif 235 236 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 237 sc->gif_ro6.ro_rt != NULL) { 238 RTFREE(sc->gif_ro6.ro_rt); 239 sc->gif_ro6.ro_rt = NULL; 240 } 241 242 return (error); 243 } 244 245 int 246 in6_gif_input(mp, offp, proto) 247 struct mbuf **mp; 248 int *offp, proto; 249 { 250 struct mbuf *m = *mp; 251 struct ifnet *gifp = NULL; 252 struct gif_softc *sc; 253 struct ip6_hdr *ip6; 254 int af = 0; 255 u_int32_t otos; 256 257 ip6 = mtod(m, struct ip6_hdr *); 258 259 sc = (struct gif_softc *)encap_getarg(m); 260 if (sc == NULL) { 261 m_freem(m); 262 ip6stat.ip6s_nogif++; 263 return IPPROTO_DONE; 264 } 265 266 gifp = GIF2IFP(sc); 267 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 268 m_freem(m); 269 ip6stat.ip6s_nogif++; 270 return IPPROTO_DONE; 271 } 272 273 otos = ip6->ip6_flow; 274 m_adj(m, *offp); 275 276 switch (proto) { 277 #ifdef INET 278 case IPPROTO_IPV4: 279 { 280 struct ip *ip; 281 u_int8_t otos8; 282 af = AF_INET; 283 otos8 = (ntohl(otos) >> 20) & 0xff; 284 if (m->m_len < sizeof(*ip)) { 285 m = m_pullup(m, sizeof(*ip)); 286 if (!m) 287 return IPPROTO_DONE; 288 } 289 ip = mtod(m, struct ip *); 290 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 291 ECN_ALLOWED : ECN_NOCARE, 292 &otos8, &ip->ip_tos) == 0) { 293 m_freem(m); 294 return IPPROTO_DONE; 295 } 296 break; 297 } 298 #endif /* INET */ 299 #ifdef INET6 300 case IPPROTO_IPV6: 301 { 302 struct ip6_hdr *ip6; 303 af = AF_INET6; 304 if (m->m_len < sizeof(*ip6)) { 305 m = m_pullup(m, sizeof(*ip6)); 306 if (!m) 307 return IPPROTO_DONE; 308 } 309 ip6 = mtod(m, struct ip6_hdr *); 310 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 311 ECN_ALLOWED : ECN_NOCARE, 312 &otos, &ip6->ip6_flow) == 0) { 313 m_freem(m); 314 return IPPROTO_DONE; 315 } 316 break; 317 } 318 #endif 319 case IPPROTO_ETHERIP: 320 af = AF_LINK; 321 break; 322 323 default: 324 ip6stat.ip6s_nogif++; 325 m_freem(m); 326 return IPPROTO_DONE; 327 } 328 329 gif_input(m, af, gifp); 330 return IPPROTO_DONE; 331 } 332 333 /* 334 * validate outer address. 335 */ 336 static int 337 gif_validate6(ip6, sc, ifp) 338 const struct ip6_hdr *ip6; 339 struct gif_softc *sc; 340 struct ifnet *ifp; 341 { 342 struct sockaddr_in6 *src, *dst; 343 344 src = (struct sockaddr_in6 *)sc->gif_psrc; 345 dst = (struct sockaddr_in6 *)sc->gif_pdst; 346 347 /* 348 * Check for address match. Note that the check is for an incoming 349 * packet. We should compare the *source* address in our configuration 350 * and the *destination* address of the packet, and vice versa. 351 */ 352 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 353 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 354 return 0; 355 356 /* martian filters on outer source - done in ip6_input */ 357 358 /* ingress filters on outer source */ 359 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 360 struct sockaddr_in6 sin6; 361 struct rtentry *rt; 362 363 bzero(&sin6, sizeof(sin6)); 364 sin6.sin6_family = AF_INET6; 365 sin6.sin6_len = sizeof(struct sockaddr_in6); 366 sin6.sin6_addr = ip6->ip6_src; 367 sin6.sin6_scope_id = 0; /* XXX */ 368 369 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); 370 if (!rt || rt->rt_ifp != ifp) { 371 #if 0 372 char ip6buf[INET6_ADDRSTRLEN]; 373 log(LOG_WARNING, "%s: packet from %s dropped " 374 "due to ingress filter\n", if_name(GIF2IFP(sc)), 375 ip6_sprintf(ip6buf, &sin6.sin6_addr)); 376 #endif 377 if (rt) 378 rtfree(rt); 379 return 0; 380 } 381 rtfree(rt); 382 } 383 384 return 128 * 2; 385 } 386 387 /* 388 * we know that we are in IFF_UP, outer address available, and outer family 389 * matched the physical addr family. see gif_encapcheck(). 390 * sanity check for arg should have been done in the caller. 391 */ 392 int 393 gif_encapcheck6(m, off, proto, arg) 394 const struct mbuf *m; 395 int off; 396 int proto; 397 void *arg; 398 { 399 struct ip6_hdr ip6; 400 struct gif_softc *sc; 401 struct ifnet *ifp; 402 403 /* sanity check done in caller */ 404 sc = (struct gif_softc *)arg; 405 406 /* LINTED const cast */ 407 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 408 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 409 410 return gif_validate6(&ip6, sc, ifp); 411 } 412 413 int 414 in6_gif_attach(sc) 415 struct gif_softc *sc; 416 { 417 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 418 (void *)&in6_gif_protosw, sc); 419 if (sc->encap_cookie6 == NULL) 420 return EEXIST; 421 return 0; 422 } 423 424 int 425 in6_gif_detach(sc) 426 struct gif_softc *sc; 427 { 428 int error; 429 430 error = encap_detach(sc->encap_cookie6); 431 if (error == 0) 432 sc->encap_cookie6 = NULL; 433 return error; 434 } 435