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