1 /* $OpenBSD: ip6_forward.c,v 1.42 2008/11/23 13:30:59 claudio Exp $ */ 2 /* $KAME: ip6_forward.c,v 1.75 2001/06/29 12:42:13 jinmei 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 "pf.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/domain.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/errno.h> 43 #include <sys/time.h> 44 #include <sys/kernel.h> 45 #include <sys/syslog.h> 46 47 #include <net/if.h> 48 #include <net/if_enc.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_var.h> 53 #include <netinet/ip_var.h> 54 #include <netinet/ip6.h> 55 #include <netinet6/ip6_var.h> 56 #include <netinet/icmp6.h> 57 #include <netinet6/nd6.h> 58 59 #if NPF > 0 60 #include <net/pfvar.h> 61 #endif 62 63 #ifdef IPSEC 64 #include <netinet/ip_ipsp.h> 65 #include <netinet/ip_ah.h> 66 #include <netinet/ip_esp.h> 67 #include <netinet/udp.h> 68 #include <netinet/tcp.h> 69 #include <net/pfkeyv2.h> 70 #endif 71 72 struct route_in6 ip6_forward_rt; 73 int ip6_forward_rtableid; 74 75 /* 76 * Forward a packet. If some error occurs return the sender 77 * an icmp packet. Note we can't always generate a meaningful 78 * icmp message because icmp doesn't have a large enough repertoire 79 * of codes and types. 80 * 81 * If not forwarding, just drop the packet. This could be confusing 82 * if ipforwarding was zero but some routing protocol was advancing 83 * us as a gateway to somewhere. However, we must let the routing 84 * protocol deal with that. 85 * 86 */ 87 88 void 89 ip6_forward(struct mbuf *m, int srcrt) 90 { 91 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 92 struct sockaddr_in6 *dst; 93 struct rtentry *rt; 94 int error = 0, type = 0, code = 0; 95 struct mbuf *mcopy = NULL; 96 struct ifnet *origifp; /* maybe unnecessary */ 97 #ifdef IPSEC 98 u_int8_t sproto = 0; 99 struct m_tag *mtag; 100 union sockaddr_union sdst; 101 struct tdb_ident *tdbi; 102 u_int32_t sspi; 103 struct tdb *tdb; 104 int s; 105 #endif /* IPSEC */ 106 int rtableid = 0; 107 108 /* 109 * Do not forward packets to multicast destination (should be handled 110 * by ip6_mforward(). 111 * Do not forward packets with unspecified source. It was discussed 112 * in July 2000, on ipngwg mailing list. 113 */ 114 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 115 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 116 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 117 ip6stat.ip6s_cantforward++; 118 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 119 if (ip6_log_time + ip6_log_interval < time_second) { 120 ip6_log_time = time_second; 121 log(LOG_DEBUG, 122 "cannot forward " 123 "from %s to %s nxt %d received on %s\n", 124 ip6_sprintf(&ip6->ip6_src), 125 ip6_sprintf(&ip6->ip6_dst), 126 ip6->ip6_nxt, 127 m->m_pkthdr.rcvif->if_xname); 128 } 129 m_freem(m); 130 return; 131 } 132 133 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 134 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 135 icmp6_error(m, ICMP6_TIME_EXCEEDED, 136 ICMP6_TIME_EXCEED_TRANSIT, 0); 137 return; 138 } 139 ip6->ip6_hlim -= IPV6_HLIMDEC; 140 141 #ifdef IPSEC 142 if (!ipsec_in_use) 143 goto done_spd; 144 145 s = splnet(); 146 147 /* 148 * Check if there was an outgoing SA bound to the flow 149 * from a transport protocol. 150 */ 151 152 /* Do we have any pending SAs to apply ? */ 153 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 154 if (mtag != NULL) { 155 #ifdef DIAGNOSTIC 156 if (mtag->m_tag_len != sizeof (struct tdb_ident)) 157 panic("ip6_forward: tag of length %d (should be %d", 158 mtag->m_tag_len, sizeof (struct tdb_ident)); 159 #endif 160 tdbi = (struct tdb_ident *)(mtag + 1); 161 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 162 if (tdb == NULL) 163 error = -EINVAL; 164 m_tag_delete(m, mtag); 165 } else 166 tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr), 167 &error, IPSP_DIRECTION_OUT, NULL, NULL); 168 169 if (tdb == NULL) { 170 splx(s); 171 172 if (error == 0) { 173 /* 174 * No IPsec processing required, we'll just send the 175 * packet out. 176 */ 177 sproto = 0; 178 179 /* Fall through to routing/multicast handling */ 180 } else { 181 /* 182 * -EINVAL is used to indicate that the packet should 183 * be silently dropped, typically because we've asked 184 * key management for an SA. 185 */ 186 if (error == -EINVAL) /* Should silently drop packet */ 187 error = 0; 188 189 goto freecopy; 190 } 191 } else { 192 /* Loop detection */ 193 for (mtag = m_tag_first(m); mtag != NULL; 194 mtag = m_tag_next(m, mtag)) { 195 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 196 mtag->m_tag_id != 197 PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 198 continue; 199 tdbi = (struct tdb_ident *)(mtag + 1); 200 if (tdbi->spi == tdb->tdb_spi && 201 tdbi->proto == tdb->tdb_sproto && 202 !bcmp(&tdbi->dst, &tdb->tdb_dst, 203 sizeof(union sockaddr_union))) { 204 splx(s); 205 sproto = 0; /* mark as no-IPsec-needed */ 206 goto done_spd; 207 } 208 } 209 210 /* We need to do IPsec */ 211 bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst)); 212 sspi = tdb->tdb_spi; 213 sproto = tdb->tdb_sproto; 214 splx(s); 215 } 216 217 /* Fall through to the routing/multicast handling code */ 218 done_spd: 219 #endif /* IPSEC */ 220 221 #if NPF > 0 222 rtableid = m->m_pkthdr.pf.rtableid; 223 #endif 224 225 /* 226 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 227 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 228 * we need to generate an ICMP6 message to the src. 229 * Thanks to M_EXT, in most cases copy will not occur. 230 * 231 * It is important to save it before IPsec processing as IPsec 232 * processing may modify the mbuf. 233 */ 234 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 235 236 dst = &ip6_forward_rt.ro_dst; 237 if (!srcrt) { 238 /* 239 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst 240 */ 241 if (ip6_forward_rt.ro_rt == 0 || 242 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 || 243 ip6_forward_rtableid != rtableid) { 244 if (ip6_forward_rt.ro_rt) { 245 RTFREE(ip6_forward_rt.ro_rt); 246 ip6_forward_rt.ro_rt = 0; 247 } 248 /* this probably fails but give it a try again */ 249 rtalloc_mpath((struct route *)&ip6_forward_rt, 250 &ip6->ip6_src.s6_addr32[0], rtableid); 251 ip6_forward_rtableid = rtableid; 252 } 253 254 if (ip6_forward_rt.ro_rt == 0) { 255 ip6stat.ip6s_noroute++; 256 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 257 if (mcopy) { 258 icmp6_error(mcopy, ICMP6_DST_UNREACH, 259 ICMP6_DST_UNREACH_NOROUTE, 0); 260 } 261 m_freem(m); 262 return; 263 } 264 } else if (ip6_forward_rt.ro_rt == 0 || 265 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 || 266 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 267 if (ip6_forward_rt.ro_rt) { 268 RTFREE(ip6_forward_rt.ro_rt); 269 ip6_forward_rt.ro_rt = 0; 270 } 271 bzero(dst, sizeof(*dst)); 272 dst->sin6_len = sizeof(struct sockaddr_in6); 273 dst->sin6_family = AF_INET6; 274 dst->sin6_addr = ip6->ip6_dst; 275 276 rtalloc_mpath((struct route *)&ip6_forward_rt, 277 &ip6->ip6_src.s6_addr32[0], 0); 278 279 if (ip6_forward_rt.ro_rt == 0) { 280 ip6stat.ip6s_noroute++; 281 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 282 if (mcopy) { 283 icmp6_error(mcopy, ICMP6_DST_UNREACH, 284 ICMP6_DST_UNREACH_NOROUTE, 0); 285 } 286 m_freem(m); 287 return; 288 } 289 } 290 rt = ip6_forward_rt.ro_rt; 291 292 /* 293 * Scope check: if a packet can't be delivered to its destination 294 * for the reason that the destination is beyond the scope of the 295 * source address, discard the packet and return an icmp6 destination 296 * unreachable error with Code 2 (beyond scope of source address). 297 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1] 298 */ 299 if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) != 300 in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) { 301 ip6stat.ip6s_cantforward++; 302 ip6stat.ip6s_badscope++; 303 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 304 305 if (ip6_log_time + ip6_log_interval < time_second) { 306 ip6_log_time = time_second; 307 log(LOG_DEBUG, 308 "cannot forward " 309 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 310 ip6_sprintf(&ip6->ip6_src), 311 ip6_sprintf(&ip6->ip6_dst), 312 ip6->ip6_nxt, 313 m->m_pkthdr.rcvif->if_xname, rt->rt_ifp->if_xname); 314 } 315 if (mcopy) 316 icmp6_error(mcopy, ICMP6_DST_UNREACH, 317 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 318 m_freem(m); 319 goto freert; 320 } 321 322 #ifdef IPSEC 323 /* 324 * Check if the packet needs encapsulation. 325 * ipsp_process_packet will never come back to here. 326 * XXX ipsp_process_packet() calls ip6_output(), and there'll be no 327 * PMTU notification. is it okay? 328 */ 329 if (sproto != 0) { 330 s = splnet(); 331 332 #if NPF > 0 333 if (pf_test6(PF_OUT, &encif[0].sc_if, &m, NULL) != PF_PASS) { 334 splx(s); 335 error = EHOSTUNREACH; 336 m_freem(m); 337 goto senderr; 338 } 339 if (m == NULL) { 340 splx(s); 341 goto senderr; 342 } 343 ip6 = mtod(m, struct ip6_hdr *); 344 #endif 345 tdb = gettdb(sspi, &sdst, sproto); 346 if (tdb == NULL) { 347 splx(s); 348 error = EHOSTUNREACH; 349 m_freem(m); 350 goto senderr; /*XXX*/ 351 } 352 353 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 354 355 /* Callee frees mbuf */ 356 error = ipsp_process_packet(m, tdb, AF_INET6, 0); 357 splx(s); 358 m_freem(mcopy); 359 goto freert; 360 } 361 #endif /* IPSEC */ 362 363 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 364 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 365 if (mcopy) { 366 u_long mtu; 367 368 mtu = IN6_LINKMTU(rt->rt_ifp); 369 370 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 371 } 372 m_freem(m); 373 goto freert; 374 } 375 376 if (rt->rt_flags & RTF_GATEWAY) 377 dst = (struct sockaddr_in6 *)rt->rt_gateway; 378 379 /* 380 * If we are to forward the packet using the same interface 381 * as one we got the packet from, perhaps we should send a redirect 382 * to sender to shortcut a hop. 383 * Only send redirect if source is sending directly to us, 384 * and if packet was not source routed (or has any options). 385 * Also, don't send redirect if forwarding using a route 386 * modified by a redirect. 387 */ 388 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects && 389 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 390 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) && 391 nd6_is_addr_neighbor((struct sockaddr_in6 *)&ip6_forward_rt.ro_dst, rt->rt_ifp)) { 392 /* 393 * If the incoming interface is equal to the outgoing 394 * one, the link attached to the interface is 395 * point-to-point, and the IPv6 destination is 396 * regarded as on-link on the link, then it will be 397 * highly probable that the destination address does 398 * not exist on the link and that the packet is going 399 * to loop. Thus, we immediately drop the packet and 400 * send an ICMPv6 error message. 401 * For other routing loops, we dare to let the packet 402 * go to the loop, so that a remote diagnosing host 403 * can detect the loop by traceroute. 404 * type/code is based on suggestion by Rich Draves. 405 * not sure if it is the best pick. 406 */ 407 icmp6_error(mcopy, ICMP6_DST_UNREACH, 408 ICMP6_DST_UNREACH_ADDR, 0); 409 m_freem(m); 410 goto freert; 411 } 412 type = ND_REDIRECT; 413 } 414 415 /* 416 * Fake scoped addresses. Note that even link-local source or 417 * destinaion can appear, if the originating node just sends the 418 * packet to us (without address resolution for the destination). 419 * Since both icmp6_error and icmp6_redirect_output fill the embedded 420 * link identifiers, we can do this stuff after making a copy for 421 * returning an error. 422 */ 423 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 424 /* 425 * See corresponding comments in ip6_output. 426 * XXX: but is it possible that ip6_forward() sends a packet 427 * to a loopback interface? I don't think so, and thus 428 * I bark here. (jinmei@kame.net) 429 * XXX: it is common to route invalid packets to loopback. 430 * also, the codepath will be visited on use of ::1 in 431 * rthdr. (itojun) 432 */ 433 #if 1 434 if (0) 435 #else 436 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 437 #endif 438 { 439 printf("ip6_forward: outgoing interface is loopback. " 440 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 441 ip6_sprintf(&ip6->ip6_src), 442 ip6_sprintf(&ip6->ip6_dst), 443 ip6->ip6_nxt, m->m_pkthdr.rcvif->if_xname, 444 rt->rt_ifp->if_xname); 445 } 446 447 /* we can just use rcvif in forwarding. */ 448 origifp = m->m_pkthdr.rcvif; 449 } 450 else 451 origifp = rt->rt_ifp; 452 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 453 ip6->ip6_src.s6_addr16[1] = 0; 454 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 455 ip6->ip6_dst.s6_addr16[1] = 0; 456 457 #if NPF > 0 458 if (pf_test6(PF_OUT, rt->rt_ifp, &m, NULL) != PF_PASS) { 459 m_freem(m); 460 goto senderr; 461 } 462 if (m == NULL) 463 goto senderr; 464 465 ip6 = mtod(m, struct ip6_hdr *); 466 #endif 467 468 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 469 if (error) { 470 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 471 ip6stat.ip6s_cantforward++; 472 } else { 473 ip6stat.ip6s_forward++; 474 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 475 if (type) 476 ip6stat.ip6s_redirectsent++; 477 else { 478 if (mcopy) 479 goto freecopy; 480 } 481 } 482 483 #if NPF > 0 || defined(IPSEC) 484 senderr: 485 #endif 486 if (mcopy == NULL) 487 goto freert; 488 switch (error) { 489 case 0: 490 if (type == ND_REDIRECT) { 491 icmp6_redirect_output(mcopy, rt); 492 goto freert; 493 } 494 goto freecopy; 495 496 case EMSGSIZE: 497 /* xxx MTU is constant in PPP? */ 498 goto freecopy; 499 500 case ENOBUFS: 501 /* Tell source to slow down like source quench in IP? */ 502 goto freecopy; 503 504 case ENETUNREACH: /* shouldn't happen, checked above */ 505 case EHOSTUNREACH: 506 case ENETDOWN: 507 case EHOSTDOWN: 508 default: 509 type = ICMP6_DST_UNREACH; 510 code = ICMP6_DST_UNREACH_ADDR; 511 break; 512 } 513 icmp6_error(mcopy, type, code, 0); 514 goto freert; 515 516 freecopy: 517 m_freem(mcopy); 518 freert: 519 #ifndef SMALL_KERNEL 520 if (ip6_multipath && ip6_forward_rt.ro_rt && 521 (ip6_forward_rt.ro_rt->rt_flags & RTF_MPATH)) { 522 RTFREE(ip6_forward_rt.ro_rt); 523 ip6_forward_rt.ro_rt = 0; 524 } 525 #endif 526 return; 527 } 528