1 /* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */ 2 3 /*- 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_mrouting.h" 36 #include "opt_inet.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 #include <sys/malloc.h> 49 #include <sys/vimage.h> 50 51 #include <net/if.h> 52 #include <net/route.h> 53 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 #include <netinet/ip_var.h> 58 #include <netinet/in_gif.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip_encap.h> 61 #include <netinet/ip_ecn.h> 62 #include <netinet/vinet.h> 63 64 #ifdef INET6 65 #include <netinet/ip6.h> 66 #endif 67 68 #ifdef MROUTING 69 #include <netinet/ip_mroute.h> 70 #endif /* MROUTING */ 71 72 #include <net/if_gif.h> 73 74 static int gif_validate4(const struct ip *, struct gif_softc *, 75 struct ifnet *); 76 77 extern struct domain inetdomain; 78 struct protosw in_gif_protosw = { 79 .pr_type = SOCK_RAW, 80 .pr_domain = &inetdomain, 81 .pr_protocol = 0/* IPPROTO_IPV[46] */, 82 .pr_flags = PR_ATOMIC|PR_ADDR, 83 .pr_input = in_gif_input, 84 .pr_output = (pr_output_t*)rip_output, 85 .pr_ctloutput = rip_ctloutput, 86 .pr_usrreqs = &rip_usrreqs 87 }; 88 89 #ifdef VIMAGE_GLOBALS 90 extern int ip_gif_ttl; 91 #endif 92 SYSCTL_V_INT(V_NET, vnet_gif, _net_inet_ip, IPCTL_GIF_TTL, gifttl, 93 CTLFLAG_RW, ip_gif_ttl, 0, ""); 94 95 int 96 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m) 97 { 98 INIT_VNET_GIF(ifp->if_vnet); 99 struct gif_softc *sc = ifp->if_softc; 100 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst; 101 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc; 102 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst; 103 struct ip iphdr; /* capsule IP header, host byte ordered */ 104 struct etherip_header eiphdr; 105 int proto, error; 106 u_int8_t tos; 107 108 GIF_LOCK_ASSERT(sc); 109 110 if (sin_src == NULL || sin_dst == NULL || 111 sin_src->sin_family != AF_INET || 112 sin_dst->sin_family != AF_INET) { 113 m_freem(m); 114 return EAFNOSUPPORT; 115 } 116 117 switch (family) { 118 #ifdef INET 119 case AF_INET: 120 { 121 struct ip *ip; 122 123 proto = IPPROTO_IPV4; 124 if (m->m_len < sizeof(*ip)) { 125 m = m_pullup(m, sizeof(*ip)); 126 if (!m) 127 return ENOBUFS; 128 } 129 ip = mtod(m, struct ip *); 130 tos = ip->ip_tos; 131 break; 132 } 133 #endif /* INET */ 134 #ifdef INET6 135 case AF_INET6: 136 { 137 struct ip6_hdr *ip6; 138 proto = IPPROTO_IPV6; 139 if (m->m_len < sizeof(*ip6)) { 140 m = m_pullup(m, sizeof(*ip6)); 141 if (!m) 142 return ENOBUFS; 143 } 144 ip6 = mtod(m, struct ip6_hdr *); 145 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 146 break; 147 } 148 #endif /* INET6 */ 149 case AF_LINK: 150 proto = IPPROTO_ETHERIP; 151 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK; 152 eiphdr.eip_pad = 0; 153 /* prepend Ethernet-in-IP header */ 154 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); 155 if (m && m->m_len < sizeof(struct etherip_header)) 156 m = m_pullup(m, sizeof(struct etherip_header)); 157 if (m == NULL) 158 return ENOBUFS; 159 bcopy(&eiphdr, mtod(m, struct etherip_header *), 160 sizeof(struct etherip_header)); 161 break; 162 163 default: 164 #ifdef DEBUG 165 printf("in_gif_output: warning: unknown family %d passed\n", 166 family); 167 #endif 168 m_freem(m); 169 return EAFNOSUPPORT; 170 } 171 172 bzero(&iphdr, sizeof(iphdr)); 173 iphdr.ip_src = sin_src->sin_addr; 174 /* bidirectional configured tunnel mode */ 175 if (sin_dst->sin_addr.s_addr != INADDR_ANY) 176 iphdr.ip_dst = sin_dst->sin_addr; 177 else { 178 m_freem(m); 179 return ENETUNREACH; 180 } 181 iphdr.ip_p = proto; 182 /* version will be set in ip_output() */ 183 iphdr.ip_ttl = V_ip_gif_ttl; 184 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip); 185 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 186 &iphdr.ip_tos, &tos); 187 188 /* prepend new IP header */ 189 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 190 if (m && m->m_len < sizeof(struct ip)) 191 m = m_pullup(m, sizeof(struct ip)); 192 if (m == NULL) { 193 printf("ENOBUFS in in_gif_output %d\n", __LINE__); 194 return ENOBUFS; 195 } 196 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip)); 197 198 M_SETFIB(m, sc->gif_fibnum); 199 200 if (dst->sin_family != sin_dst->sin_family || 201 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) { 202 /* cache route doesn't match */ 203 bzero(dst, sizeof(*dst)); 204 dst->sin_family = sin_dst->sin_family; 205 dst->sin_len = sizeof(struct sockaddr_in); 206 dst->sin_addr = sin_dst->sin_addr; 207 if (sc->gif_ro.ro_rt) { 208 RTFREE(sc->gif_ro.ro_rt); 209 sc->gif_ro.ro_rt = NULL; 210 } 211 #if 0 212 GIF2IFP(sc)->if_mtu = GIF_MTU; 213 #endif 214 } 215 216 if (sc->gif_ro.ro_rt == NULL) { 217 in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum); 218 if (sc->gif_ro.ro_rt == NULL) { 219 m_freem(m); 220 return ENETUNREACH; 221 } 222 223 /* if it constitutes infinite encapsulation, punt. */ 224 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 225 m_freem(m); 226 return ENETUNREACH; /* XXX */ 227 } 228 #if 0 229 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu 230 - sizeof(struct ip); 231 #endif 232 } 233 234 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL); 235 236 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 237 sc->gif_ro.ro_rt != NULL) { 238 RTFREE(sc->gif_ro.ro_rt); 239 sc->gif_ro.ro_rt = NULL; 240 } 241 242 return (error); 243 } 244 245 void 246 in_gif_input(struct mbuf *m, int off) 247 { 248 INIT_VNET_INET(curvnet); 249 struct ifnet *gifp = NULL; 250 struct gif_softc *sc; 251 struct ip *ip; 252 int af; 253 u_int8_t otos; 254 int proto; 255 256 ip = mtod(m, struct ip *); 257 proto = ip->ip_p; 258 259 sc = (struct gif_softc *)encap_getarg(m); 260 if (sc == NULL) { 261 m_freem(m); 262 V_ipstat.ips_nogif++; 263 return; 264 } 265 266 gifp = GIF2IFP(sc); 267 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 268 m_freem(m); 269 V_ipstat.ips_nogif++; 270 return; 271 } 272 273 otos = ip->ip_tos; 274 m_adj(m, off); 275 276 switch (proto) { 277 #ifdef INET 278 case IPPROTO_IPV4: 279 { 280 struct ip *ip; 281 af = AF_INET; 282 if (m->m_len < sizeof(*ip)) { 283 m = m_pullup(m, sizeof(*ip)); 284 if (!m) 285 return; 286 } 287 ip = mtod(m, struct ip *); 288 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 289 ECN_ALLOWED : ECN_NOCARE, 290 &otos, &ip->ip_tos) == 0) { 291 m_freem(m); 292 return; 293 } 294 break; 295 } 296 #endif 297 #ifdef INET6 298 case IPPROTO_IPV6: 299 { 300 struct ip6_hdr *ip6; 301 u_int8_t itos, oitos; 302 303 af = AF_INET6; 304 if (m->m_len < sizeof(*ip6)) { 305 m = m_pullup(m, sizeof(*ip6)); 306 if (!m) 307 return; 308 } 309 ip6 = mtod(m, struct ip6_hdr *); 310 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 311 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 312 ECN_ALLOWED : ECN_NOCARE, 313 &otos, &itos) == 0) { 314 m_freem(m); 315 return; 316 } 317 if (itos != oitos) { 318 ip6->ip6_flow &= ~htonl(0xff << 20); 319 ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 320 } 321 break; 322 } 323 #endif /* INET6 */ 324 case IPPROTO_ETHERIP: 325 af = AF_LINK; 326 break; 327 328 default: 329 V_ipstat.ips_nogif++; 330 m_freem(m); 331 return; 332 } 333 gif_input(m, af, gifp); 334 return; 335 } 336 337 /* 338 * validate outer address. 339 */ 340 static int 341 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp) 342 { 343 INIT_VNET_INET(curvnet); 344 struct sockaddr_in *src, *dst; 345 struct in_ifaddr *ia4; 346 347 src = (struct sockaddr_in *)sc->gif_psrc; 348 dst = (struct sockaddr_in *)sc->gif_pdst; 349 350 /* check for address match */ 351 if (src->sin_addr.s_addr != ip->ip_dst.s_addr || 352 dst->sin_addr.s_addr != ip->ip_src.s_addr) 353 return 0; 354 355 /* martian filters on outer source - NOT done in ip_input! */ 356 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr))) 357 return 0; 358 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) { 359 case 0: case 127: case 255: 360 return 0; 361 } 362 /* reject packets with broadcast on source */ 363 TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) { 364 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 365 continue; 366 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) 367 return 0; 368 } 369 370 /* ingress filters on outer source */ 371 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 372 struct sockaddr_in sin; 373 struct rtentry *rt; 374 375 bzero(&sin, sizeof(sin)); 376 sin.sin_family = AF_INET; 377 sin.sin_len = sizeof(struct sockaddr_in); 378 sin.sin_addr = ip->ip_src; 379 /* XXX MRT check for the interface we would use on output */ 380 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 381 0UL, sc->gif_fibnum); 382 if (!rt || rt->rt_ifp != ifp) { 383 #if 0 384 log(LOG_WARNING, "%s: packet from 0x%x dropped " 385 "due to ingress filter\n", if_name(GIF2IFP(sc)), 386 (u_int32_t)ntohl(sin.sin_addr.s_addr)); 387 #endif 388 if (rt) 389 RTFREE_LOCKED(rt); 390 return 0; 391 } 392 RTFREE_LOCKED(rt); 393 } 394 395 return 32 * 2; 396 } 397 398 /* 399 * we know that we are in IFF_UP, outer address available, and outer family 400 * matched the physical addr family. see gif_encapcheck(). 401 */ 402 int 403 gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg) 404 { 405 struct ip ip; 406 struct gif_softc *sc; 407 struct ifnet *ifp; 408 409 /* sanity check done in caller */ 410 sc = (struct gif_softc *)arg; 411 412 /* LINTED const cast */ 413 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 414 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 415 416 return gif_validate4(&ip, sc, ifp); 417 } 418 419 int 420 in_gif_attach(struct gif_softc *sc) 421 { 422 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck, 423 &in_gif_protosw, sc); 424 if (sc->encap_cookie4 == NULL) 425 return EEXIST; 426 return 0; 427 } 428 429 int 430 in_gif_detach(struct gif_softc *sc) 431 { 432 int error; 433 434 error = encap_detach(sc->encap_cookie4); 435 if (error == 0) 436 sc->encap_cookie4 = NULL; 437 return error; 438 } 439