in6_gif.c revision 1.47
1/* $NetBSD: in6_gif.c,v 1.47 2006/12/15 21:18:54 joerg Exp $ */ 2/* $KAME: in6_gif.c,v 1.62 2001/07/29 04:27: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#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: in6_gif.c,v 1.47 2006/12/15 21:18:54 joerg Exp $"); 35 36#include "opt_inet.h" 37#include "opt_iso.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/socket.h> 42#include <sys/sockio.h> 43#include <sys/mbuf.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> 50 51#include <net/if.h> 52#include <net/route.h> 53 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#ifdef INET 57#include <netinet/ip.h> 58#endif 59#include <netinet/ip_encap.h> 60#ifdef INET6 61#include <netinet/ip6.h> 62#include <netinet6/ip6_var.h> 63#include <netinet6/in6_gif.h> 64#include <netinet6/in6_var.h> 65#endif 66#include <netinet6/ip6protosw.h> 67#include <netinet/ip_ecn.h> 68 69#include <net/if_gif.h> 70 71#include <net/net_osdep.h> 72 73static int gif_validate6 __P((const struct ip6_hdr *, struct gif_softc *, 74 struct ifnet *)); 75 76int ip6_gif_hlim = GIF_HLIM; 77 78extern struct domain inet6domain; 79const struct ip6protosw in6_gif_protosw = 80{ SOCK_RAW, &inet6domain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR, 81 in6_gif_input, rip6_output, in6_gif_ctlinput, rip6_ctloutput, 82 rip6_usrreq, 83 0, 0, 0, 0, 84}; 85 86extern LIST_HEAD(, gif_softc) gif_softc_list; 87 88int 89in6_gif_output(ifp, family, m) 90 struct ifnet *ifp; 91 int family; /* family of the packet to be encapsulate. */ 92 struct mbuf *m; 93{ 94 struct gif_softc *sc = (struct gif_softc*)ifp; 95 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 96 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 97 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 98 struct ip6_hdr *ip6; 99 int proto, error; 100 u_int8_t itos, otos; 101 102 if (sin6_src == NULL || sin6_dst == NULL || 103 sin6_src->sin6_family != AF_INET6 || 104 sin6_dst->sin6_family != AF_INET6) { 105 m_freem(m); 106 return EAFNOSUPPORT; 107 } 108 109 switch (family) { 110#ifdef INET 111 case AF_INET: 112 { 113 struct ip *ip; 114 115 proto = IPPROTO_IPV4; 116 if (m->m_len < sizeof(*ip)) { 117 m = m_pullup(m, sizeof(*ip)); 118 if (!m) 119 return ENOBUFS; 120 } 121 ip = mtod(m, struct ip *); 122 itos = ip->ip_tos; 123 break; 124 } 125#endif 126#ifdef INET6 127 case AF_INET6: 128 { 129 proto = IPPROTO_IPV6; 130 if (m->m_len < sizeof(*ip6)) { 131 m = m_pullup(m, sizeof(*ip6)); 132 if (!m) 133 return ENOBUFS; 134 } 135 ip6 = mtod(m, struct ip6_hdr *); 136 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 137 break; 138 } 139#endif 140#ifdef ISO 141 case AF_ISO: 142 proto = IPPROTO_EON; 143 itos = 0; 144 break; 145#endif 146 default: 147#ifdef DEBUG 148 printf("in6_gif_output: warning: unknown family %d passed\n", 149 family); 150#endif 151 m_freem(m); 152 return EAFNOSUPPORT; 153 } 154 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)); 159 if (m == NULL) 160 return ENOBUFS; 161 162 ip6 = mtod(m, struct ip6_hdr *); 163 ip6->ip6_flow = 0; 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); 168#endif 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; 175 else { 176 m_freem(m); 177 return ENETUNREACH; 178 } 179 if (ifp->if_flags & IFF_LINK1) 180 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos); 181 else 182 ip_ecn_ingress(ECN_NOCARE, &otos, &itos); 183 ip6->ip6_flow &= ~ntohl(0xff00000); 184 ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 185 186 if (dst->sin6_family != sin6_dst->sin6_family || 187 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) 188 rtcache_free((struct route *)&sc->gif_ro6); 189 else 190 rtcache_check((struct route *)&sc->gif_ro6); 191 192 if (sc->gif_ro6.ro_rt == NULL) { 193 bzero(dst, sizeof(*dst)); 194 dst->sin6_family = sin6_dst->sin6_family; 195 dst->sin6_len = sizeof(struct sockaddr_in6); 196 dst->sin6_addr = sin6_dst->sin6_addr; 197 rtcache_init((struct route *)&sc->gif_ro6); 198 if (sc->gif_ro6.ro_rt == NULL) { 199 m_freem(m); 200 return ENETUNREACH; 201 } 202 } 203 204 /* If the route constitutes infinite encapsulation, punt. */ 205 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 206 m_freem(m); 207 return ENETUNREACH; /* XXX */ 208 } 209 210#ifdef IPV6_MINMTU 211 /* 212 * force fragmentation to minimum MTU, to avoid path MTU discovery. 213 * it is too painful to ask for resend of inner packet, to achieve 214 * path MTU discovery for encapsulated packets. 215 */ 216 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 217 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL); 218#else 219 error = ip6_output(m, 0, &sc->gif_ro6, 0, 220 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL); 221#endif 222 223 return (error); 224} 225 226int 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#ifndef GIF_ENCAPCHECK 246 if (!gif_validate6(ip6, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) { 247 m_freem(m); 248 ip6stat.ip6s_nogif++; 249 return IPPROTO_DONE; 250 } 251#endif 252 253 otos = ip6->ip6_flow; 254 m_adj(m, *offp); 255 256 switch (proto) { 257#ifdef INET 258 case IPPROTO_IPV4: 259 { 260 struct ip *ip; 261 u_int8_t otos8; 262 af = AF_INET; 263 otos8 = (ntohl(otos) >> 20) & 0xff; 264 if (m->m_len < sizeof(*ip)) { 265 m = m_pullup(m, sizeof(*ip)); 266 if (!m) 267 return IPPROTO_DONE; 268 } 269 ip = mtod(m, struct ip *); 270 if (gifp->if_flags & IFF_LINK1) 271 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos); 272 else 273 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos); 274 break; 275 } 276#endif /* INET */ 277#ifdef INET6 278 case IPPROTO_IPV6: 279 { 280 struct ip6_hdr *ip6x; 281 af = AF_INET6; 282 if (m->m_len < sizeof(*ip6x)) { 283 m = m_pullup(m, sizeof(*ip6x)); 284 if (!m) 285 return IPPROTO_DONE; 286 } 287 ip6x = mtod(m, struct ip6_hdr *); 288 if (gifp->if_flags & IFF_LINK1) 289 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6x->ip6_flow); 290 else 291 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6x->ip6_flow); 292 break; 293 } 294#endif 295#ifdef ISO 296 case IPPROTO_EON: 297 af = AF_ISO; 298 break; 299#endif 300 default: 301 ip6stat.ip6s_nogif++; 302 m_freem(m); 303 return IPPROTO_DONE; 304 } 305 306 gif_input(m, af, gifp); 307 return IPPROTO_DONE; 308} 309 310/* 311 * validate outer address. 312 */ 313static int 314gif_validate6(ip6, sc, ifp) 315 const struct ip6_hdr *ip6; 316 struct gif_softc *sc; 317 struct ifnet *ifp; 318{ 319 struct sockaddr_in6 *src, *dst; 320 321 src = (struct sockaddr_in6 *)sc->gif_psrc; 322 dst = (struct sockaddr_in6 *)sc->gif_pdst; 323 324 /* check for address match */ 325 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 326 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 327 return 0; 328 329 /* martian filters on outer source - done in ip6_input */ 330 331 /* ingress filters on outer source */ 332 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) { 333 struct sockaddr_in6 sin6; 334 struct rtentry *rt; 335 336 bzero(&sin6, sizeof(sin6)); 337 sin6.sin6_family = AF_INET6; 338 sin6.sin6_len = sizeof(struct sockaddr_in6); 339 sin6.sin6_addr = ip6->ip6_src; 340 /* XXX scopeid */ 341 rt = rtalloc1((struct sockaddr *)&sin6, 0); 342 if (!rt || rt->rt_ifp != ifp) { 343#if 0 344 log(LOG_WARNING, "%s: packet from %s dropped " 345 "due to ingress filter\n", if_name(&sc->gif_if), 346 ip6_sprintf(&sin6.sin6_addr)); 347#endif 348 if (rt) 349 rtfree(rt); 350 return 0; 351 } 352 rtfree(rt); 353 } 354 355 return 128 * 2; 356} 357 358#ifdef GIF_ENCAPCHECK 359/* 360 * we know that we are in IFF_UP, outer address available, and outer family 361 * matched the physical addr family. see gif_encapcheck(). 362 */ 363int 364gif_encapcheck6(m, off, proto, arg) 365 struct mbuf *m; 366 int off; 367 int proto; 368 void *arg; 369{ 370 struct ip6_hdr ip6; 371 struct gif_softc *sc; 372 struct ifnet *ifp; 373 374 /* sanity check done in caller */ 375 sc = (struct gif_softc *)arg; 376 377 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 378 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 379 380 return gif_validate6(&ip6, sc, ifp); 381} 382#endif 383 384int 385in6_gif_attach(sc) 386 struct gif_softc *sc; 387{ 388#ifndef GIF_ENCAPCHECK 389 struct sockaddr_in6 mask6; 390 391 bzero(&mask6, sizeof(mask6)); 392 mask6.sin6_len = sizeof(struct sockaddr_in6); 393 mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] = 394 mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0; 395 396 if (!sc->gif_psrc || !sc->gif_pdst) 397 return EINVAL; 398 sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc, 399 (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6, 400 (const void *)&in6_gif_protosw, sc); 401#else 402 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 403 (struct protosw *)&in6_gif_protosw, sc); 404#endif 405 if (sc->encap_cookie6 == NULL) 406 return EEXIST; 407 return 0; 408} 409 410int 411in6_gif_detach(sc) 412 struct gif_softc *sc; 413{ 414 int error; 415 416 error = encap_detach(sc->encap_cookie6); 417 if (error == 0) 418 sc->encap_cookie6 = NULL; 419 420 rtcache_free((struct route *)&sc->gif_ro6); 421 422 return error; 423} 424 425void 426in6_gif_ctlinput(cmd, sa, d) 427 int cmd; 428 struct sockaddr *sa; 429 void *d; 430{ 431 struct gif_softc *sc; 432 struct ip6ctlparam *ip6cp = NULL; 433 struct ip6_hdr *ip6; 434 struct sockaddr_in6 *dst6; 435 436 if (sa->sa_family != AF_INET6 || 437 sa->sa_len != sizeof(struct sockaddr_in6)) 438 return; 439 440 if ((unsigned)cmd >= PRC_NCMDS) 441 return; 442 if (cmd == PRC_HOSTDEAD) 443 d = NULL; 444 else if (inet6ctlerrmap[cmd] == 0) 445 return; 446 447 /* if the parameter is from icmp6, decode it. */ 448 if (d != NULL) { 449 ip6cp = (struct ip6ctlparam *)d; 450 ip6 = ip6cp->ip6c_ip6; 451 } else { 452 ip6 = NULL; 453 } 454 455 if (!ip6) 456 return; 457 458 /* 459 * for now we don't care which type it was, just flush the route cache. 460 * XXX slow. sc (or sc->encap_cookie6) should be passed from 461 * ip_encap.c. 462 */ 463 for (sc = LIST_FIRST(&gif_softc_list); sc; 464 sc = LIST_NEXT(sc, gif_list)) { 465 if ((sc->gif_if.if_flags & IFF_RUNNING) == 0) 466 continue; 467 if (sc->gif_psrc->sa_family != AF_INET6) 468 continue; 469 if (sc->gif_ro6.ro_rt == NULL) 470 continue; 471 472 dst6 = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 473 /* XXX scope */ 474 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr)) 475 rtcache_free((struct route *)&sc->gif_ro6); 476 } 477} 478