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