1 /* $FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.7 2003/01/23 21:06:47 sam Exp $ */ 2 /* $DragonFly: src/sys/netinet6/in6_gif.c,v 1.14 2005/03/04 03:48:25 hsu Exp $ */ 3 /* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/socket.h> 40 #include <sys/sockio.h> 41 #include <sys/mbuf.h> 42 #include <sys/errno.h> 43 #include <sys/queue.h> 44 #include <sys/syslog.h> 45 #include <sys/protosw.h> 46 47 #include <sys/malloc.h> 48 49 #include <net/if.h> 50 #include <net/route.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_systm.h> 54 #ifdef INET 55 #include <netinet/ip.h> 56 #endif 57 #include <netinet/ip_encap.h> 58 #ifdef INET6 59 #include <netinet/ip6.h> 60 #include <netinet6/ip6_var.h> 61 #include <netinet6/in6_gif.h> 62 #include <netinet6/in6_var.h> 63 #endif 64 #include <netinet6/ip6protosw.h> 65 #include <netinet/ip_ecn.h> 66 #ifdef INET6 67 #include <netinet6/ip6_ecn.h> 68 #endif 69 70 #include <net/gif/if_gif.h> 71 72 #include <net/net_osdep.h> 73 74 #ifdef INET6 75 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *, 76 struct ifnet *); 77 78 extern struct domain inet6domain; 79 struct ip6protosw in6_gif_protosw = 80 { SOCK_RAW, &inet6domain, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR, 81 in6_gif_input, rip6_output, 0, rip6_ctloutput, 82 cpu0_soport, 83 0, 0, 0, 0, 84 &rip6_usrreqs 85 }; 86 87 int 88 in6_gif_output(struct ifnet *ifp, 89 int family, /* family of the packet to be encapsulate. */ 90 struct mbuf *m) 91 { 92 struct gif_softc *sc = (struct gif_softc*)ifp; 93 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst; 94 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc; 95 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst; 96 struct ip6_hdr *ip6; 97 int proto; 98 u_int8_t itos, otos; 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 default: 140 #ifdef DEBUG 141 printf("in6_gif_output: warning: unknown family %d passed\n", 142 family); 143 #endif 144 m_freem(m); 145 return EAFNOSUPPORT; 146 } 147 148 /* prepend new IP header */ 149 M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT); 150 if (m && m->m_len < sizeof(struct ip6_hdr)) 151 m = m_pullup(m, sizeof(struct ip6_hdr)); 152 if (m == NULL) { 153 printf("ENOBUFS in in6_gif_output %d\n", __LINE__); 154 return ENOBUFS; 155 } 156 157 ip6 = mtod(m, struct ip6_hdr *); 158 ip6->ip6_flow = 0; 159 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 160 ip6->ip6_vfc |= IPV6_VERSION; 161 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len); 162 ip6->ip6_nxt = proto; 163 ip6->ip6_hlim = ip6_gif_hlim; 164 ip6->ip6_src = sin6_src->sin6_addr; 165 /* bidirectional configured tunnel mode */ 166 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) 167 ip6->ip6_dst = sin6_dst->sin6_addr; 168 else { 169 m_freem(m); 170 return ENETUNREACH; 171 } 172 if (ifp->if_flags & IFF_LINK1) 173 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos); 174 else 175 ip_ecn_ingress(ECN_NOCARE, &otos, &itos); 176 ip6->ip6_flow &= ~ntohl(0xff00000); 177 ip6->ip6_flow |= htonl((u_int32_t)otos << 20); 178 179 if (dst->sin6_family != sin6_dst->sin6_family || 180 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) { 181 /* cache route doesn't match */ 182 bzero(dst, sizeof(*dst)); 183 dst->sin6_family = sin6_dst->sin6_family; 184 dst->sin6_len = sizeof(struct sockaddr_in6); 185 dst->sin6_addr = sin6_dst->sin6_addr; 186 if (sc->gif_ro6.ro_rt != NULL) { 187 RTFREE(sc->gif_ro6.ro_rt); 188 sc->gif_ro6.ro_rt = NULL; 189 } 190 #if 0 191 sc->gif_if.if_mtu = GIF_MTU; 192 #endif 193 } 194 195 if (sc->gif_ro6.ro_rt == NULL) { 196 rtalloc((struct route *)&sc->gif_ro6); 197 if (sc->gif_ro6.ro_rt == NULL) { 198 m_freem(m); 199 return ENETUNREACH; 200 } 201 202 /* if it constitutes infinite encapsulation, punt. */ 203 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 204 m_freem(m); 205 return ENETUNREACH; /*XXX*/ 206 } 207 #if 0 208 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu 209 - sizeof(struct ip6_hdr); 210 #endif 211 } 212 213 #ifdef IPV6_MINMTU 214 /* 215 * force fragmentation to minimum MTU, to avoid path MTU discovery. 216 * it is too painful to ask for resend of inner packet, to achieve 217 * path MTU discovery for encapsulated packets. 218 */ 219 return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL)); 220 #else 221 return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL)); 222 #endif 223 } 224 225 int in6_gif_input(struct mbuf **mp, int *offp, int proto) 226 { 227 struct mbuf *m = *mp; 228 struct ifnet *gifp = NULL; 229 struct ip6_hdr *ip6; 230 int af = 0; 231 u_int32_t otos; 232 233 ip6 = mtod(m, struct ip6_hdr *); 234 235 gifp = (struct ifnet *)encap_getarg(m); 236 237 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 238 m_freem(m); 239 ip6stat.ip6s_nogif++; 240 return IPPROTO_DONE; 241 } 242 243 otos = ip6->ip6_flow; 244 m_adj(m, *offp); 245 246 switch (proto) { 247 #ifdef INET 248 case IPPROTO_IPV4: 249 { 250 struct ip *ip; 251 u_int8_t otos8; 252 af = AF_INET; 253 otos8 = (ntohl(otos) >> 20) & 0xff; 254 if (m->m_len < sizeof(*ip)) { 255 m = m_pullup(m, sizeof(*ip)); 256 if (!m) 257 return IPPROTO_DONE; 258 } 259 ip = mtod(m, struct ip *); 260 if (gifp->if_flags & IFF_LINK1) 261 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos); 262 else 263 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos); 264 break; 265 } 266 #endif /* INET */ 267 #ifdef INET6 268 case IPPROTO_IPV6: 269 { 270 struct ip6_hdr *ip6; 271 af = AF_INET6; 272 if (m->m_len < sizeof(*ip6)) { 273 m = m_pullup(m, sizeof(*ip6)); 274 if (!m) 275 return IPPROTO_DONE; 276 } 277 ip6 = mtod(m, struct ip6_hdr *); 278 if (gifp->if_flags & IFF_LINK1) 279 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow); 280 else 281 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow); 282 break; 283 } 284 #endif 285 default: 286 ip6stat.ip6s_nogif++; 287 m_freem(m); 288 return IPPROTO_DONE; 289 } 290 291 gif_input(m, af, gifp); 292 return IPPROTO_DONE; 293 } 294 295 /* 296 * validate outer address. 297 */ 298 static int 299 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc, 300 struct ifnet *ifp) 301 { 302 struct sockaddr_in6 *src, *dst; 303 304 src = (struct sockaddr_in6 *)sc->gif_psrc; 305 dst = (struct sockaddr_in6 *)sc->gif_pdst; 306 307 /* 308 * Check for address match. Note that the check is for an incoming 309 * packet. We should compare the *source* address in our configuration 310 * and the *destination* address of the packet, and vice versa. 311 */ 312 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) || 313 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) 314 return 0; 315 316 /* martian filters on outer source - done in ip6_input */ 317 318 /* ingress filters on outer source */ 319 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) { 320 struct sockaddr_in6 sin6; 321 struct rtentry *rt; 322 323 bzero(&sin6, sizeof(sin6)); 324 sin6.sin6_family = AF_INET6; 325 sin6.sin6_len = sizeof(struct sockaddr_in6); 326 sin6.sin6_addr = ip6->ip6_src; 327 sin6.sin6_scope_id = 0; /* XXX */ 328 329 rt = rtpurelookup((struct sockaddr *)&sin6); 330 if (rt != NULL) 331 --rt->rt_refcnt; 332 if (rt == NULL || rt->rt_ifp != ifp) { 333 #if 0 334 log(LOG_WARNING, "%s: packet from %s dropped " 335 "due to ingress filter\n", if_name(&sc->gif_if), 336 ip6_sprintf(&sin6.sin6_addr)); 337 #endif 338 return 0; 339 } 340 } 341 342 return 128 * 2; 343 } 344 345 /* 346 * we know that we are in IFF_UP, outer address available, and outer family 347 * matched the physical addr family. see gif_encapcheck(). 348 * sanity check for arg should have been done in the caller. 349 */ 350 int 351 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg) 352 { 353 struct ip6_hdr ip6; 354 struct gif_softc *sc; 355 struct ifnet *ifp; 356 357 /* sanity check done in caller */ 358 sc = (struct gif_softc *)arg; 359 360 m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6); 361 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 362 363 return gif_validate6(&ip6, sc, ifp); 364 } 365 366 int 367 in6_gif_attach(struct gif_softc *sc) 368 { 369 sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck, 370 (struct protosw *)&in6_gif_protosw, sc); 371 if (sc->encap_cookie6 == NULL) 372 return EEXIST; 373 return 0; 374 } 375 376 int 377 in6_gif_detach(struct gif_softc *sc) 378 { 379 int error; 380 381 error = encap_detach(sc->encap_cookie6); 382 if (error == 0) 383 sc->encap_cookie6 = NULL; 384 return error; 385 } 386 387 #endif /* INET6 */ 388