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 * $FreeBSD$ 30 */ 31 32 /* 33 * in_gif.c 34 */ 35 36 #include "opt_mrouting.h" 37 #include "opt_inet6.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/kernel.h> 46 #include <sys/sysctl.h> 47 #include <sys/protosw.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 #include <netinet/ip.h> 55 #ifdef INET6 56 #include <netinet/ip6.h> 57 #endif 58 #include <netinet/ip_var.h> 59 #include <netinet/in_gif.h> 60 #include <netinet/ip_ecn.h> 61 #ifdef INET6 62 #include <netinet6/ip6_ecn.h> 63 #endif 64 65 #ifdef MROUTING 66 #include <netinet/ip_mroute.h> 67 #endif /* MROUTING */ 68 69 #include <net/if_gif.h> 70 71 #include "gif.h" 72 73 #include <machine/stdarg.h> 74 75 #include <net/net_osdep.h> 76 77 #if NGIF > 0 78 int ip_gif_ttl = GIF_TTL; 79 #else 80 int ip_gif_ttl = 0; 81 #endif 82 83 SYSCTL_DECL(_net_inet_ip); 84 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW, 85 &ip_gif_ttl, 0, ""); 86 87 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 88 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1])) 89 90 int 91 in_gif_output(ifp, family, m, rt) 92 struct ifnet *ifp; 93 int family; 94 struct mbuf *m; 95 struct rtentry *rt; 96 { 97 register struct gif_softc *sc = (struct gif_softc*)ifp; 98 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst; 99 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc; 100 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst; 101 struct ip iphdr; /* capsule IP header, host byte ordered */ 102 int proto, error; 103 u_int8_t tos; 104 #ifdef INET6 105 struct ip6_hdr *ip6 = NULL; 106 #endif 107 108 if (sin_src == NULL || sin_dst == NULL || 109 sin_src->sin_family != AF_INET || 110 sin_dst->sin_family != AF_INET) { 111 m_freem(m); 112 return EAFNOSUPPORT; 113 } 114 115 switch (family) { 116 case AF_INET: 117 { 118 struct ip *ip; 119 120 proto = IPPROTO_IPV4; 121 if (m->m_len < sizeof(*ip)) { 122 m = m_pullup(m, sizeof(*ip)); 123 if (!m) 124 return ENOBUFS; 125 } 126 ip = mtod(m, struct ip *); 127 tos = ip->ip_tos; 128 break; 129 } 130 #ifdef INET6 131 case AF_INET6: 132 { 133 proto = IPPROTO_IPV6; 134 if (m->m_len < sizeof(*ip6)) { 135 m = m_pullup(m, sizeof(*ip6)); 136 if (!m) 137 return ENOBUFS; 138 } 139 ip6 = mtod(m, struct ip6_hdr *); 140 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 141 break; 142 } 143 #endif /*INET6*/ 144 default: 145 #ifdef DIAGNOSTIC 146 printf("in_gif_output: warning: unknown family %d passed\n", 147 family); 148 #endif 149 m_freem(m); 150 return EAFNOSUPPORT; 151 } 152 153 bzero(&iphdr, sizeof(iphdr)); 154 iphdr.ip_src = sin_src->sin_addr; 155 #ifdef INET6 156 /* XXX: temporal stf support hack */ 157 if (bcmp(ifp->if_name, "stf", 3) == 0) { 158 if (ip6 == NULL) { 159 m_freem(m); 160 return ENETUNREACH; 161 } 162 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst)) 163 iphdr.ip_dst = *GET_V4(&ip6->ip6_dst); 164 else if (rt && rt->rt_gateway->sa_family == AF_INET6) { 165 struct in6_addr *dst6; 166 167 dst6 = &((struct sockaddr_in6 *) 168 (rt->rt_gateway))->sin6_addr; 169 if (IN6_IS_ADDR_6TO4(dst6)) 170 iphdr.ip_dst = *GET_V4(dst6); 171 else { 172 m_freem(m); 173 return ENETUNREACH; 174 } 175 } else { 176 m_freem(m); 177 return ENETUNREACH; 178 } 179 } else 180 #endif 181 if (ifp->if_flags & IFF_LINK0) { 182 /* multi-destination mode */ 183 if (sin_dst->sin_addr.s_addr != INADDR_ANY) 184 iphdr.ip_dst = sin_dst->sin_addr; 185 else if (rt) { 186 iphdr.ip_dst = ((struct sockaddr_in *) 187 (rt->rt_gateway))->sin_addr; 188 } else { 189 m_freem(m); 190 return ENETUNREACH; 191 } 192 } else { 193 /* bidirectional configured tunnel mode */ 194 if (sin_dst->sin_addr.s_addr != INADDR_ANY) 195 iphdr.ip_dst = sin_dst->sin_addr; 196 else { 197 m_freem(m); 198 return ENETUNREACH; 199 } 200 } 201 iphdr.ip_p = proto; 202 /* version will be set in ip_output() */ 203 iphdr.ip_ttl = ip_gif_ttl; 204 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip); 205 if (ifp->if_flags & IFF_LINK1) 206 ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos); 207 208 /* prepend new IP header */ 209 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 210 if (m && m->m_len < sizeof(struct ip)) 211 m = m_pullup(m, sizeof(struct ip)); 212 if (m == NULL) { 213 printf("ENOBUFS in in_gif_output %d\n", __LINE__); 214 return ENOBUFS; 215 } 216 217 *(mtod(m, struct ip *)) = iphdr; 218 219 if (dst->sin_family != sin_dst->sin_family || 220 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) { 221 /* cache route doesn't match */ 222 dst->sin_family = sin_dst->sin_family; 223 dst->sin_len = sizeof(struct sockaddr_in); 224 dst->sin_addr = sin_dst->sin_addr; 225 if (sc->gif_ro.ro_rt) { 226 RTFREE(sc->gif_ro.ro_rt); 227 sc->gif_ro.ro_rt = NULL; 228 } 229 } 230 231 if (sc->gif_ro.ro_rt == NULL) { 232 rtalloc(&sc->gif_ro); 233 if (sc->gif_ro.ro_rt == NULL) { 234 m_freem(m); 235 return ENETUNREACH; 236 } 237 } 238 239 error = ip_output(m, 0, &sc->gif_ro, 0, 0); 240 return(error); 241 } 242 243 void 244 in_gif_input(struct mbuf *m, int off, int proto) 245 { 246 struct gif_softc *sc; 247 struct ifnet *gifp = NULL; 248 struct ip *ip; 249 int i, af; 250 u_int8_t otos; 251 252 ip = mtod(m, struct ip *); 253 254 /* this code will be soon improved. */ 255 #define satosin(sa) ((struct sockaddr_in *)(sa)) 256 for (i = 0, sc = gif; i < ngif; i++, sc++) { 257 if (sc->gif_psrc == NULL 258 || sc->gif_pdst == NULL 259 || sc->gif_psrc->sa_family != AF_INET 260 || sc->gif_pdst->sa_family != AF_INET) { 261 continue; 262 } 263 264 if ((sc->gif_if.if_flags & IFF_UP) == 0) 265 continue; 266 267 #ifdef INET6 268 /* XXX: temporal stf support hack */ 269 if (proto == IPPROTO_IPV6 && 270 bcmp(sc->gif_if.if_name, "stf", 3) == 0 && 271 (satosin(sc->gif_psrc)->sin_addr.s_addr == 272 ip->ip_dst.s_addr || 273 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) && 274 satosin(sc->gif_pdst)->sin_addr.s_addr == 275 INADDR_BROADCAST) { 276 gifp = &sc->gif_if; 277 break; 278 } 279 #endif 280 281 if ((sc->gif_if.if_flags & IFF_LINK0) 282 && satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr 283 && satosin(sc->gif_pdst)->sin_addr.s_addr == INADDR_ANY) { 284 gifp = &sc->gif_if; 285 continue; 286 } 287 288 if (satosin(sc->gif_psrc)->sin_addr.s_addr == ip->ip_dst.s_addr 289 && satosin(sc->gif_pdst)->sin_addr.s_addr == ip->ip_src.s_addr) 290 { 291 gifp = &sc->gif_if; 292 break; 293 } 294 } 295 296 if (gifp == NULL) { 297 #ifdef MROUTING 298 /* for backward compatibility */ 299 if (proto == IPPROTO_IPV4) { 300 ipip_input(m, off, proto); 301 return; 302 } 303 #endif /*MROUTING*/ 304 m_freem(m); 305 ipstat.ips_nogif++; 306 return; 307 } 308 309 otos = ip->ip_tos; 310 m_adj(m, off); 311 312 switch (proto) { 313 case IPPROTO_IPV4: 314 { 315 struct ip *ip; 316 317 #ifdef INET6 318 if (bcmp(gifp->if_name, "stf", 3) == 0) { 319 m_freem(m); 320 return; 321 } 322 #endif 323 af = AF_INET; 324 if (m->m_len < sizeof(*ip)) { 325 m = m_pullup(m, sizeof(*ip)); 326 if (!m) 327 return; 328 } 329 ip = mtod(m, struct ip *); 330 if (gifp->if_flags & IFF_LINK1) 331 ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos); 332 break; 333 } 334 #ifdef INET6 335 case IPPROTO_IPV6: 336 { 337 struct ip6_hdr *ip6; 338 u_int8_t itos; 339 af = AF_INET6; 340 if (m->m_len < sizeof(*ip6)) { 341 m = m_pullup(m, sizeof(*ip6)); 342 if (!m) 343 return; 344 } 345 ip6 = mtod(m, struct ip6_hdr *); 346 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 347 if (gifp->if_flags & IFF_LINK1) 348 ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 349 ip6->ip6_flow &= ~htonl(0xff << 20); 350 ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 351 break; 352 } 353 #endif /* INET6 */ 354 default: 355 ipstat.ips_nogif++; 356 m_freem(m); 357 return; 358 } 359 gif_input(m, af, gifp); 360 return; 361 } 362