in6_gif.c revision 269054
1/*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/netinet6/in6_gif.c 269054 2014-07-24 10:42:47Z hrs $"); 34 35#include "opt_inet.h" 36#include "opt_inet6.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/socket.h> 41#include <sys/sockio.h> 42#include <sys/mbuf.h> 43#include <sys/errno.h> 44#include <sys/kernel.h> 45#include <sys/queue.h> 46#include <sys/syslog.h> 47#include <sys/sysctl.h> 48#include <sys/protosw.h> 49#include <sys/malloc.h> 50 51#include <net/if.h> 52#include <net/if_var.h> 53#include <net/route.h> 54#include <net/vnet.h> 55 56#include <netinet/in.h> 57#include <netinet/in_systm.h> 58#ifdef INET 59#include <netinet/ip.h> 60#endif 61#include <netinet/ip_encap.h> 62#ifdef INET6 63#include <netinet/ip6.h> 64#include <netinet6/ip6_var.h> 65#include <netinet6/in6_gif.h> 66#include <netinet6/in6_var.h> 67#endif 68#include <netinet6/ip6protosw.h> 69#include <netinet/ip_ecn.h> 70#ifdef INET6 71#include <netinet6/ip6_ecn.h> 72#endif 73 74#include <net/if_gif.h> 75 76VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM; 77#define V_ip6_gif_hlim VNET(ip6_gif_hlim) 78 79SYSCTL_DECL(_net_inet6_ip6); 80SYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_RW, 81 &VNET_NAME(ip6_gif_hlim), 0, ""); 82 83static int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 84 struct ifnet *); 85 86extern struct domain inet6domain; 87struct ip6protosw in6_gif_protosw = { 88 .pr_type = SOCK_RAW, 89 .pr_domain = &inet6domain, 90 .pr_protocol = 0, /* IPPROTO_IPV[46] */ 91 .pr_flags = PR_ATOMIC|PR_ADDR, 92 .pr_input = in6_gif_input, 93 .pr_output = rip6_output, 94 .pr_ctloutput = rip6_ctloutput, 95 .pr_usrreqs = &rip6_usrreqs 96}; 97 98int 99in6_gif_output(struct ifnet *ifp, 100 int family, /* family of the packet to be encapsulate */ 101 struct mbuf *m) 102{ 103 struct gif_softc *sc = ifp->if_softc; 104 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 105 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 106 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 107 struct ip6_hdr *ip6; 108 struct etherip_header eiphdr; 109 int error, len, proto; 110 u_int8_t itos, otos; 111 112 GIF_LOCK_ASSERT(sc); 113 114 if (sin6_src == NULL || sin6_dst == NULL || 115 sin6_src->sin6_family != AF_INET6 || 116 sin6_dst->sin6_family != AF_INET6) { 117 m_freem(m); 118 return EAFNOSUPPORT; 119 } 120 121 switch (family) { 122#ifdef INET 123 case AF_INET: 124 { 125 struct ip *ip; 126 127 proto = IPPROTO_IPV4; 128 if (m->m_len < sizeof(*ip)) { 129 m = m_pullup(m, sizeof(*ip)); 130 if (!m) 131 return ENOBUFS; 132 } 133 ip = mtod(m, struct ip *); 134 itos = ip->ip_tos; 135 break; 136 } 137#endif 138#ifdef INET6 139 case AF_INET6: 140 { 141 struct ip6_hdr *ip6; 142 proto = IPPROTO_IPV6; 143 if (m->m_len < sizeof(*ip6)) { 144 m = m_pullup(m, sizeof(*ip6)); 145 if (!m) 146 return ENOBUFS; 147 } 148 ip6 = mtod(m, struct ip6_hdr *); 149 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 150 break; 151 } 152#endif 153 case AF_LINK: 154 proto = IPPROTO_ETHERIP; 155 156 /* 157 * GIF_SEND_REVETHIP (disabled by default) intentionally 158 * sends an EtherIP packet with revered version field in 159 * the header. This is a knob for backward compatibility 160 * with FreeBSD 7.2R or prior. 161 */ 162 if ((sc->gif_options & GIF_SEND_REVETHIP)) { 163 eiphdr.eip_ver = 0; 164 eiphdr.eip_resvl = ETHERIP_VERSION; 165 eiphdr.eip_resvh = 0; 166 } else { 167 eiphdr.eip_ver = ETHERIP_VERSION; 168 eiphdr.eip_resvl = 0; 169 eiphdr.eip_resvh = 0; 170 } 171 /* prepend Ethernet-in-IP header */ 172 M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 173 if (m && m->m_len < sizeof(struct etherip_header)) 174 m = m_pullup(m, sizeof(struct etherip_header)); 175 if (m == NULL) 176 return ENOBUFS; 177 bcopy(&eiphdr, mtod(m, struct etherip_header *), 178 sizeof(struct etherip_header)); 179 itos = 0; 180 break; 181 182 default: 183#ifdef DEBUG 184 printf("in6_gif_output: warning: unknown family %d passed\n", 185 family); 186#endif 187 m_freem(m); 188 return EAFNOSUPPORT; 189 } 190 191 /* prepend new IP header */ 192 len = sizeof(struct ip6_hdr); 193#ifndef __NO_STRICT_ALIGNMENT 194 if (family == AF_LINK) 195 len += ETHERIP_ALIGN; 196#endif 197 M_PREPEND(m, len, M_NOWAIT); 198 if (m != NULL && m->m_len < len) 199 m = m_pullup(m, len); 200 if (m == NULL) { 201 printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 202 return ENOBUFS; 203 } 204#ifndef __NO_STRICT_ALIGNMENT 205 if (family == AF_LINK) { 206 len = mtod(m, vm_offset_t) & 3; 207 KASSERT(len == 0 || len == ETHERIP_ALIGN, 208 ("in6_gif_output: unexpected misalignment")); 209 m->m_data += len; 210 m->m_len -= ETHERIP_ALIGN; 211 } 212#endif 213 214 ip6 = mtod(m, struct ip6_hdr *); 215 ip6->ip6_flow = 0; 216 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 217 ip6->ip6_vfc |= IPV6_VERSION; 218 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 219 ip6->ip6_nxt = proto; 220 ip6->ip6_hlim = V_ip6_gif_hlim; 221 ip6->ip6_src = sin6_src->sin6_addr; 222 /* bidirectional configured tunnel mode */ 223 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 224 ip6->ip6_dst = sin6_dst->sin6_addr; 225 else { 226 m_freem(m); 227 return ENETUNREACH; 228 } 229 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 230 &otos, &itos); 231 ip6->ip6_flow &= ~htonl(0xff << 20); 232 ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 233 234 M_SETFIB(m, sc->gif_fibnum); 235 236 if (dst->sin6_family != sin6_dst->sin6_family || 237 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 238 /* cache route doesn't match */ 239 bzero(dst, sizeof(*dst)); 240 dst->sin6_family = sin6_dst->sin6_family; 241 dst->sin6_len = sizeof(struct sockaddr_in6); 242 dst->sin6_addr = sin6_dst->sin6_addr; 243 if (sc->gif_ro6.ro_rt) { 244 RTFREE(sc->gif_ro6.ro_rt); 245 sc->gif_ro6.ro_rt = NULL; 246 } 247#if 0 248 GIF2IFP(sc)->if_mtu = GIF_MTU; 249#endif 250 } 251 252 if (sc->gif_ro6.ro_rt == NULL) { 253 in6_rtalloc(&sc->gif_ro6, sc->gif_fibnum); 254 if (sc->gif_ro6.ro_rt == NULL) { 255 m_freem(m); 256 return ENETUNREACH; 257 } 258 259 /* if it constitutes infinite encapsulation, punt. */ 260 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 261 m_freem(m); 262 return ENETUNREACH; /*XXX*/ 263 } 264#if 0 265 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 266 - sizeof(struct ip6_hdr); 267#endif 268 } 269 270 m->m_flags &= ~(M_BCAST|M_MCAST); 271#ifdef IPV6_MINMTU 272 /* 273 * force fragmentation to minimum MTU, to avoid path MTU discovery. 274 * it is too painful to ask for resend of inner packet, to achieve 275 * path MTU discovery for encapsulated packets. 276 */ 277 error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL); 278#else 279 error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL); 280#endif 281 282 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 283 sc->gif_ro6.ro_rt != NULL) { 284 RTFREE(sc->gif_ro6.ro_rt); 285 sc->gif_ro6.ro_rt = NULL; 286 } 287 288 return (error); 289} 290 291int 292in6_gif_input(struct mbuf **mp, int *offp, int proto) 293{ 294 struct mbuf *m = *mp; 295 struct ifnet *gifp = NULL; 296 struct gif_softc *sc; 297 struct ip6_hdr *ip6; 298 int af = 0; 299 u_int32_t otos; 300 301 ip6 = mtod(m, struct ip6_hdr *); 302 303 sc = (struct gif_softc *)encap_getarg(m); 304 if (sc == NULL) { 305 m_freem(m); 306 IP6STAT_INC(ip6s_nogif); 307 return IPPROTO_DONE; 308 } 309 310 gifp = GIF2IFP(sc); 311 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 312 m_freem(m); 313 IP6STAT_INC(ip6s_nogif); 314 return IPPROTO_DONE; 315 } 316 317 otos = ip6->ip6_flow; 318 m_adj(m, *offp); 319 320 switch (proto) { 321#ifdef INET 322 case IPPROTO_IPV4: 323 { 324 struct ip *ip; 325 u_int8_t otos8; 326 af = AF_INET; 327 otos8 = (ntohl(otos) >> 20) & 0xff; 328 if (m->m_len < sizeof(*ip)) { 329 m = m_pullup(m, sizeof(*ip)); 330 if (!m) 331 return IPPROTO_DONE; 332 } 333 ip = mtod(m, struct ip *); 334 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 335 ECN_ALLOWED : ECN_NOCARE, 336 &otos8, &ip->ip_tos) == 0) { 337 m_freem(m); 338 return IPPROTO_DONE; 339 } 340 break; 341 } 342#endif /* INET */ 343#ifdef INET6 344 case IPPROTO_IPV6: 345 { 346 struct ip6_hdr *ip6; 347 af = AF_INET6; 348 if (m->m_len < sizeof(*ip6)) { 349 m = m_pullup(m, sizeof(*ip6)); 350 if (!m) 351 return IPPROTO_DONE; 352 } 353 ip6 = mtod(m, struct ip6_hdr *); 354 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ? 355 ECN_ALLOWED : ECN_NOCARE, 356 &otos, &ip6->ip6_flow) == 0) { 357 m_freem(m); 358 return IPPROTO_DONE; 359 } 360 break; 361 } 362#endif 363 case IPPROTO_ETHERIP: 364 af = AF_LINK; 365 break; 366 367 default: 368 IP6STAT_INC(ip6s_nogif); 369 m_freem(m); 370 return IPPROTO_DONE; 371 } 372 373 gif_input(m, af, gifp); 374 return IPPROTO_DONE; 375} 376 377/* 378 * validate outer address. 379 */ 380static int 381gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc, 382 struct ifnet *ifp) 383{ 384 struct sockaddr_in6 *src, *dst; 385 386 src = (struct sockaddr_in6 *)sc->gif_psrc; 387 dst = (struct sockaddr_in6 *)sc->gif_pdst; 388 389 /* 390 * Check for address match. Note that the check is for an incoming 391 * packet. We should compare the *source* address in our configuration 392 * and the *destination* address of the packet, and vice versa. 393 */ 394 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 395 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 396 return 0; 397 398 /* martian filters on outer source - done in ip6_input */ 399 400 /* ingress filters on outer source */ 401 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 402 struct sockaddr_in6 sin6; 403 struct rtentry *rt; 404 405 bzero(&sin6, sizeof(sin6)); 406 sin6.sin6_family = AF_INET6; 407 sin6.sin6_len = sizeof(struct sockaddr_in6); 408 sin6.sin6_addr = ip6->ip6_src; 409 sin6.sin6_scope_id = 0; /* XXX */ 410 411 rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, 412 sc->gif_fibnum); 413 if (!rt || rt->rt_ifp != ifp) { 414#if 0 415 char ip6buf[INET6_ADDRSTRLEN]; 416 log(LOG_WARNING, "%s: packet from %s dropped " 417 "due to ingress filter\n", if_name(GIF2IFP(sc)), 418 ip6_sprintf(ip6buf, &sin6.sin6_addr)); 419#endif 420 if (rt) 421 RTFREE_LOCKED(rt); 422 return 0; 423 } 424 RTFREE_LOCKED(rt); 425 } 426 427 return 128 * 2; 428} 429 430/* 431 * we know that we are in IFF_UP, outer address available, and outer family 432 * matched the physical addr family. see gif_encapcheck(). 433 * sanity check for arg should have been done in the caller. 434 */ 435int 436gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg) 437{ 438 struct ip6_hdr ip6; 439 struct gif_softc *sc; 440 struct ifnet *ifp; 441 442 /* sanity check done in caller */ 443 sc = (struct gif_softc *)arg; 444 445 /* LINTED const cast */ 446 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 447 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 448 449 return gif_validate6(&ip6, sc, ifp); 450} 451 452int 453in6_gif_attach(struct gif_softc *sc) 454{ 455 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 456 (void *)&in6_gif_protosw, sc); 457 if (sc->encap_cookie6 == NULL) 458 return EEXIST; 459 return 0; 460} 461 462int 463in6_gif_detach(struct gif_softc *sc) 464{ 465 int error; 466 467 error = encap_detach(sc->encap_cookie6); 468 if (error == 0) 469 sc->encap_cookie6 = NULL; 470 return error; 471} 472