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