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