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 63 #ifdef INET6 64 #include <netinet/ip6.h> 65 #endif 66 67 #ifdef MROUTING 68 #include <netinet/ip_mroute.h> 69 #endif /* MROUTING */ 70 71 #include <net/if_gif.h> 72 73 static int gif_validate4(const struct ip *, struct gif_softc *, 74 struct ifnet *); 75 76 extern struct domain inetdomain; 77 struct protosw in_gif_protosw = { 78 .pr_type = SOCK_RAW, 79 .pr_domain = &inetdomain, 80 .pr_protocol = 0/* IPPROTO_IPV[46] */, 81 .pr_flags = PR_ATOMIC|PR_ADDR, 82 .pr_input = in_gif_input, 83 .pr_output = (pr_output_t*)rip_output, 84 .pr_ctloutput = rip_ctloutput, 85 .pr_usrreqs = &rip_usrreqs 86 }; 87 88 #ifdef VIMAGE_GLOBALS 89 extern int ip_gif_ttl; 90 #endif 91 SYSCTL_V_INT(V_NET, vnet_gif, _net_inet_ip, IPCTL_GIF_TTL, gifttl, 92 CTLFLAG_RW, ip_gif_ttl, 0, ""); 93 94 int 95 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m) 96 { 97 INIT_VNET_GIF(ifp->if_vnet); 98 struct gif_softc *sc = ifp->if_softc; 99 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst; 100 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc; 101 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst; 102 struct ip iphdr; /* capsule IP header, host byte ordered */ 103 struct etherip_header eiphdr; 104 int proto, error; 105 u_int8_t tos; 106 107 GIF_LOCK_ASSERT(sc); 108 109 if (sin_src == NULL || sin_dst == NULL || 110 sin_src->sin_family != AF_INET || 111 sin_dst->sin_family != AF_INET) { 112 m_freem(m); 113 return EAFNOSUPPORT; 114 } 115 116 switch (family) { 117 #ifdef INET 118 case AF_INET: 119 { 120 struct ip *ip; 121 122 proto = IPPROTO_IPV4; 123 if (m->m_len < sizeof(*ip)) { 124 m = m_pullup(m, sizeof(*ip)); 125 if (!m) 126 return ENOBUFS; 127 } 128 ip = mtod(m, struct ip *); 129 tos = ip->ip_tos; 130 break; 131 } 132 #endif /* INET */ 133 #ifdef INET6 134 case AF_INET6: 135 { 136 struct ip6_hdr *ip6; 137 proto = IPPROTO_IPV6; 138 if (m->m_len < sizeof(*ip6)) { 139 m = m_pullup(m, sizeof(*ip6)); 140 if (!m) 141 return ENOBUFS; 142 } 143 ip6 = mtod(m, struct ip6_hdr *); 144 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 145 break; 146 } 147 #endif /* INET6 */ 148 case AF_LINK: 149 proto = IPPROTO_ETHERIP; 150 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK; 151 eiphdr.eip_pad = 0; 152 /* prepend Ethernet-in-IP header */ 153 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT); 154 if (m && m->m_len < sizeof(struct etherip_header)) 155 m = m_pullup(m, sizeof(struct etherip_header)); 156 if (m == NULL) 157 return ENOBUFS; 158 bcopy(&eiphdr, mtod(m, struct etherip_header *), 159 sizeof(struct etherip_header)); 160 break; 161 162 default: 163 #ifdef DEBUG 164 printf("in_gif_output: warning: unknown family %d passed\n", 165 family); 166 #endif 167 m_freem(m); 168 return EAFNOSUPPORT; 169 } 170 171 bzero(&iphdr, sizeof(iphdr)); 172 iphdr.ip_src = sin_src->sin_addr; 173 /* bidirectional configured tunnel mode */ 174 if (sin_dst->sin_addr.s_addr != INADDR_ANY) 175 iphdr.ip_dst = sin_dst->sin_addr; 176 else { 177 m_freem(m); 178 return ENETUNREACH; 179 } 180 iphdr.ip_p = proto; 181 /* version will be set in ip_output() */ 182 iphdr.ip_ttl = V_ip_gif_ttl; 183 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip); 184 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE, 185 &iphdr.ip_tos, &tos); 186 187 /* prepend new IP header */ 188 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 189 if (m && m->m_len < sizeof(struct ip)) 190 m = m_pullup(m, sizeof(struct ip)); 191 if (m == NULL) { 192 printf("ENOBUFS in in_gif_output %d\n", __LINE__); 193 return ENOBUFS; 194 } 195 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip)); 196 197 M_SETFIB(m, sc->gif_fibnum); 198 199 if (dst->sin_family != sin_dst->sin_family || 200 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) { 201 /* cache route doesn't match */ 202 bzero(dst, sizeof(*dst)); 203 dst->sin_family = sin_dst->sin_family; 204 dst->sin_len = sizeof(struct sockaddr_in); 205 dst->sin_addr = sin_dst->sin_addr; 206 if (sc->gif_ro.ro_rt) { 207 RTFREE(sc->gif_ro.ro_rt); 208 sc->gif_ro.ro_rt = NULL; 209 } 210 #if 0 211 GIF2IFP(sc)->if_mtu = GIF_MTU; 212 #endif 213 } 214 215 if (sc->gif_ro.ro_rt == NULL) { 216 in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum); 217 if (sc->gif_ro.ro_rt == NULL) { 218 m_freem(m); 219 return ENETUNREACH; 220 } 221 222 /* if it constitutes infinite encapsulation, punt. */ 223 if (sc->gif_ro.ro_rt->rt_ifp == ifp) { 224 m_freem(m); 225 return ENETUNREACH; /* XXX */ 226 } 227 #if 0 228 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu 229 - sizeof(struct ip); 230 #endif 231 } 232 233 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL); 234 235 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) && 236 sc->gif_ro.ro_rt != NULL) { 237 RTFREE(sc->gif_ro.ro_rt); 238 sc->gif_ro.ro_rt = NULL; 239 } 240 241 return (error); 242 } 243 244 void 245 in_gif_input(struct mbuf *m, int off) 246 { 247 INIT_VNET_INET(curvnet); 248 struct ifnet *gifp = NULL; 249 struct gif_softc *sc; 250 struct ip *ip; 251 int af; 252 u_int8_t otos; 253 int proto; 254 255 ip = mtod(m, struct ip *); 256 proto = ip->ip_p; 257 258 sc = (struct gif_softc *)encap_getarg(m); 259 if (sc == NULL) { 260 m_freem(m); 261 V_ipstat.ips_nogif++; 262 return; 263 } 264 265 gifp = GIF2IFP(sc); 266 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) { 267 m_freem(m); 268 V_ipstat.ips_nogif++; 269 return; 270 } 271 272 otos = ip->ip_tos; 273 m_adj(m, off); 274 275 switch (proto) { 276 #ifdef INET 277 case IPPROTO_IPV4: 278 { 279 struct ip *ip; 280 af = AF_INET; 281 if (m->m_len < sizeof(*ip)) { 282 m = m_pullup(m, sizeof(*ip)); 283 if (!m) 284 return; 285 } 286 ip = mtod(m, struct ip *); 287 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 288 ECN_ALLOWED : ECN_NOCARE, 289 &otos, &ip->ip_tos) == 0) { 290 m_freem(m); 291 return; 292 } 293 break; 294 } 295 #endif 296 #ifdef INET6 297 case IPPROTO_IPV6: 298 { 299 struct ip6_hdr *ip6; 300 u_int8_t itos, oitos; 301 302 af = AF_INET6; 303 if (m->m_len < sizeof(*ip6)) { 304 m = m_pullup(m, sizeof(*ip6)); 305 if (!m) 306 return; 307 } 308 ip6 = mtod(m, struct ip6_hdr *); 309 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 310 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ? 311 ECN_ALLOWED : ECN_NOCARE, 312 &otos, &itos) == 0) { 313 m_freem(m); 314 return; 315 } 316 if (itos != oitos) { 317 ip6->ip6_flow &= ~htonl(0xff << 20); 318 ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 319 } 320 break; 321 } 322 #endif /* INET6 */ 323 case IPPROTO_ETHERIP: 324 af = AF_LINK; 325 break; 326 327 default: 328 V_ipstat.ips_nogif++; 329 m_freem(m); 330 return; 331 } 332 gif_input(m, af, gifp); 333 return; 334 } 335 336 /* 337 * validate outer address. 338 */ 339 static int 340 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp) 341 { 342 INIT_VNET_INET(curvnet); 343 struct sockaddr_in *src, *dst; 344 struct in_ifaddr *ia4; 345 346 src = (struct sockaddr_in *)sc->gif_psrc; 347 dst = (struct sockaddr_in *)sc->gif_pdst; 348 349 /* check for address match */ 350 if (src->sin_addr.s_addr != ip->ip_dst.s_addr || 351 dst->sin_addr.s_addr != ip->ip_src.s_addr) 352 return 0; 353 354 /* martian filters on outer source - NOT done in ip_input! */ 355 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr))) 356 return 0; 357 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) { 358 case 0: case 127: case 255: 359 return 0; 360 } 361 /* reject packets with broadcast on source */ 362 TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) { 363 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 364 continue; 365 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) 366 return 0; 367 } 368 369 /* ingress filters on outer source */ 370 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) { 371 struct sockaddr_in sin; 372 struct rtentry *rt; 373 374 bzero(&sin, sizeof(sin)); 375 sin.sin_family = AF_INET; 376 sin.sin_len = sizeof(struct sockaddr_in); 377 sin.sin_addr = ip->ip_src; 378 /* XXX MRT check for the interface we would use on output */ 379 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 380 0UL, sc->gif_fibnum); 381 if (!rt || rt->rt_ifp != ifp) { 382 #if 0 383 log(LOG_WARNING, "%s: packet from 0x%x dropped " 384 "due to ingress filter\n", if_name(GIF2IFP(sc)), 385 (u_int32_t)ntohl(sin.sin_addr.s_addr)); 386 #endif 387 if (rt) 388 RTFREE_LOCKED(rt); 389 return 0; 390 } 391 RTFREE_LOCKED(rt); 392 } 393 394 return 32 * 2; 395 } 396 397 /* 398 * we know that we are in IFF_UP, outer address available, and outer family 399 * matched the physical addr family. see gif_encapcheck(). 400 */ 401 int 402 gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg) 403 { 404 struct ip ip; 405 struct gif_softc *sc; 406 struct ifnet *ifp; 407 408 /* sanity check done in caller */ 409 sc = (struct gif_softc *)arg; 410 411 /* LINTED const cast */ 412 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 413 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL; 414 415 return gif_validate4(&ip, sc, ifp); 416 } 417 418 int 419 in_gif_attach(struct gif_softc *sc) 420 { 421 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck, 422 &in_gif_protosw, sc); 423 if (sc->encap_cookie4 == NULL) 424 return EEXIST; 425 return 0; 426 } 427 428 int 429 in_gif_detach(struct gif_softc *sc) 430 { 431 int error; 432 433 error = encap_detach(sc->encap_cookie4); 434 if (error == 0) 435 sc->encap_cookie4 = NULL; 436 return error; 437 } 438