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