1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #include "opt_ipsec.h" 38 #include "opt_ipstealth.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/domain.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/errno.h> 48 #include <sys/time.h> 49 #include <sys/kernel.h> 50 #include <sys/syslog.h> 51 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <net/netisr.h> 55 #include <net/route.h> 56 #include <net/pfil.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_var.h> 60 #include <netinet/in_systm.h> 61 #include <netinet/ip.h> 62 #include <netinet/ip_var.h> 63 #include <netinet6/in6_var.h> 64 #include <netinet/ip6.h> 65 #include <netinet6/ip6_var.h> 66 #include <netinet6/scope6_var.h> 67 #include <netinet/icmp6.h> 68 #include <netinet6/nd6.h> 69 70 #include <netinet/in_pcb.h> 71 72 #ifdef IPSEC 73 #include <netinet6/ip6_ipsec.h> 74 #include <netipsec/ipsec.h> 75 #include <netipsec/ipsec6.h> 76 #include <netipsec/key.h> 77 #endif /* IPSEC */ 78 79 /* 80 * Forward a packet. If some error occurs return the sender 81 * an icmp packet. Note we can't always generate a meaningful 82 * icmp message because icmp doesn't have a large enough repertoire 83 * of codes and types. 84 * 85 * If not forwarding, just drop the packet. This could be confusing 86 * if ipforwarding was zero but some routing protocol was advancing 87 * us as a gateway to somewhere. However, we must let the routing 88 * protocol deal with that. 89 * 90 */ 91 void 92 ip6_forward(struct mbuf *m, int srcrt) 93 { 94 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 95 struct sockaddr_in6 *dst = NULL; 96 struct rtentry *rt = NULL; 97 struct route_in6 rin6; 98 int error, type = 0, code = 0; 99 struct mbuf *mcopy = NULL; 100 struct ifnet *origifp; /* maybe unnecessary */ 101 u_int32_t inzone, outzone; 102 struct in6_addr src_in6, dst_in6, odst; 103 #ifdef IPSEC 104 struct secpolicy *sp = NULL; 105 #endif 106 struct m_tag *fwd_tag; 107 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 108 109 /* 110 * Do not forward packets to multicast destination (should be handled 111 * by ip6_mforward(). 112 * Do not forward packets with unspecified source. It was discussed 113 * in July 2000, on the ipngwg mailing list. 114 */ 115 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 116 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 117 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 118 IP6STAT_INC(ip6s_cantforward); 119 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 120 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 121 V_ip6_log_time = time_uptime; 122 log(LOG_DEBUG, 123 "cannot forward " 124 "from %s to %s nxt %d received on %s\n", 125 ip6_sprintf(ip6bufs, &ip6->ip6_src), 126 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 127 ip6->ip6_nxt, 128 if_name(m->m_pkthdr.rcvif)); 129 } 130 m_freem(m); 131 return; 132 } 133 #ifdef IPSEC 134 /* 135 * Check if this packet has an active SA and needs to be dropped 136 * instead of forwarded. 137 */ 138 if (ip6_ipsec_fwd(m) != 0) { 139 IP6STAT_INC(ip6s_cantforward); 140 m_freem(m); 141 return; 142 } 143 #endif /* IPSEC */ 144 145 #ifdef IPSTEALTH 146 if (!V_ip6stealth) { 147 #endif 148 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 149 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 150 icmp6_error(m, ICMP6_TIME_EXCEEDED, 151 ICMP6_TIME_EXCEED_TRANSIT, 0); 152 return; 153 } 154 ip6->ip6_hlim -= IPV6_HLIMDEC; 155 156 #ifdef IPSTEALTH 157 } 158 #endif 159 160 /* 161 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 162 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 163 * we need to generate an ICMP6 message to the src. 164 * Thanks to M_EXT, in most cases copy will not occur. 165 * 166 * It is important to save it before IPsec processing as IPsec 167 * processing may modify the mbuf. 168 */ 169 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 170 171 #ifdef IPSEC 172 /* get a security policy for this packet */ 173 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, &error); 174 if (sp == NULL) { 175 IPSEC6STAT_INC(ips_out_inval); 176 IP6STAT_INC(ip6s_cantforward); 177 if (mcopy) { 178 #if 0 179 /* XXX: what icmp ? */ 180 #else 181 m_freem(mcopy); 182 #endif 183 } 184 m_freem(m); 185 return; 186 } 187 188 error = 0; 189 190 /* check policy */ 191 switch (sp->policy) { 192 case IPSEC_POLICY_DISCARD: 193 /* 194 * This packet is just discarded. 195 */ 196 IPSEC6STAT_INC(ips_out_polvio); 197 IP6STAT_INC(ip6s_cantforward); 198 KEY_FREESP(&sp); 199 if (mcopy) { 200 #if 0 201 /* XXX: what icmp ? */ 202 #else 203 m_freem(mcopy); 204 #endif 205 } 206 m_freem(m); 207 return; 208 209 case IPSEC_POLICY_BYPASS: 210 case IPSEC_POLICY_NONE: 211 /* no need to do IPsec. */ 212 KEY_FREESP(&sp); 213 goto skip_ipsec; 214 215 case IPSEC_POLICY_IPSEC: 216 if (sp->req == NULL) { 217 /* XXX should be panic ? */ 218 printf("ip6_forward: No IPsec request specified.\n"); 219 IP6STAT_INC(ip6s_cantforward); 220 KEY_FREESP(&sp); 221 if (mcopy) { 222 #if 0 223 /* XXX: what icmp ? */ 224 #else 225 m_freem(mcopy); 226 #endif 227 } 228 m_freem(m); 229 return; 230 } 231 /* do IPsec */ 232 break; 233 234 case IPSEC_POLICY_ENTRUST: 235 default: 236 /* should be panic ?? */ 237 printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 238 KEY_FREESP(&sp); 239 goto skip_ipsec; 240 } 241 242 { 243 struct ipsecrequest *isr = NULL; 244 245 /* 246 * when the kernel forwards a packet, it is not proper to apply 247 * IPsec transport mode to the packet. This check avoid from this. 248 * at present, if there is even a transport mode SA request in the 249 * security policy, the kernel does not apply IPsec to the packet. 250 * this check is not enough because the following case is valid. 251 * ipsec esp/tunnel/xxx-xxx/require esp/transport//require; 252 */ 253 for (isr = sp->req; isr; isr = isr->next) { 254 if (isr->saidx.mode == IPSEC_MODE_ANY) 255 goto doipsectunnel; 256 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 257 goto doipsectunnel; 258 } 259 260 /* 261 * if there's no need for tunnel mode IPsec, skip. 262 */ 263 if (!isr) 264 goto skip_ipsec; 265 266 doipsectunnel: 267 /* 268 * All the extension headers will become inaccessible 269 * (since they can be encrypted). 270 * Don't panic, we need no more updates to extension headers 271 * on inner IPv6 packet (since they are now encapsulated). 272 * 273 * IPv6 [ESP|AH] IPv6 [extension headers] payload 274 */ 275 276 /* 277 * If we need to encapsulate the packet, do it here 278 * ipsec6_proces_packet will send the packet using ip6_output 279 */ 280 error = ipsec6_process_packet(m, sp->req); 281 /* Release SP if an error occurred */ 282 if (error != 0) 283 KEY_FREESP(&sp); 284 if (error == EJUSTRETURN) { 285 /* 286 * We had a SP with a level of 'use' and no SA. We 287 * will just continue to process the packet without 288 * IPsec processing. 289 */ 290 error = 0; 291 goto skip_ipsec; 292 } 293 294 if (error) { 295 /* mbuf is already reclaimed in ipsec6_process_packet. */ 296 switch (error) { 297 case EHOSTUNREACH: 298 case ENETUNREACH: 299 case EMSGSIZE: 300 case ENOBUFS: 301 case ENOMEM: 302 break; 303 default: 304 printf("ip6_output (ipsec): error code %d\n", error); 305 /* FALLTHROUGH */ 306 case ENOENT: 307 /* don't show these error codes to the user */ 308 break; 309 } 310 IP6STAT_INC(ip6s_cantforward); 311 if (mcopy) { 312 #if 0 313 /* XXX: what icmp ? */ 314 #else 315 m_freem(mcopy); 316 #endif 317 } 318 return; 319 } else { 320 /* 321 * In the FAST IPSec case we have already 322 * re-injected the packet and it has been freed 323 * by the ipsec_done() function. So, just clean 324 * up after ourselves. 325 */ 326 m = NULL; 327 goto freecopy; 328 } 329 } 330 skip_ipsec: 331 #endif 332 again: 333 bzero(&rin6, sizeof(struct route_in6)); 334 dst = (struct sockaddr_in6 *)&rin6.ro_dst; 335 dst->sin6_len = sizeof(struct sockaddr_in6); 336 dst->sin6_family = AF_INET6; 337 dst->sin6_addr = ip6->ip6_dst; 338 again2: 339 rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 340 rt = rin6.ro_rt; 341 if (rin6.ro_rt != NULL) 342 RT_UNLOCK(rin6.ro_rt); 343 else { 344 IP6STAT_INC(ip6s_noroute); 345 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 346 if (mcopy) { 347 icmp6_error(mcopy, ICMP6_DST_UNREACH, 348 ICMP6_DST_UNREACH_NOROUTE, 0); 349 } 350 goto bad; 351 } 352 353 /* 354 * Source scope check: if a packet can't be delivered to its 355 * destination for the reason that the destination is beyond the scope 356 * of the source address, discard the packet and return an icmp6 357 * destination unreachable error with Code 2 (beyond scope of source 358 * address). We use a local copy of ip6_src, since in6_setscope() 359 * will possibly modify its first argument. 360 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1] 361 */ 362 src_in6 = ip6->ip6_src; 363 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 364 /* XXX: this should not happen */ 365 IP6STAT_INC(ip6s_cantforward); 366 IP6STAT_INC(ip6s_badscope); 367 goto bad; 368 } 369 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 370 IP6STAT_INC(ip6s_cantforward); 371 IP6STAT_INC(ip6s_badscope); 372 goto bad; 373 } 374 if (inzone != outzone) { 375 IP6STAT_INC(ip6s_cantforward); 376 IP6STAT_INC(ip6s_badscope); 377 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 378 379 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 380 V_ip6_log_time = time_uptime; 381 log(LOG_DEBUG, 382 "cannot forward " 383 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 384 ip6_sprintf(ip6bufs, &ip6->ip6_src), 385 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 386 ip6->ip6_nxt, 387 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 388 } 389 if (mcopy) 390 icmp6_error(mcopy, ICMP6_DST_UNREACH, 391 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 392 goto bad; 393 } 394 395 /* 396 * Destination scope check: if a packet is going to break the scope 397 * zone of packet's destination address, discard it. This case should 398 * usually be prevented by appropriately-configured routing table, but 399 * we need an explicit check because we may mistakenly forward the 400 * packet to a different zone by (e.g.) a default route. 401 */ 402 dst_in6 = ip6->ip6_dst; 403 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 404 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 405 inzone != outzone) { 406 IP6STAT_INC(ip6s_cantforward); 407 IP6STAT_INC(ip6s_badscope); 408 goto bad; 409 } 410 411 if (rt->rt_flags & RTF_GATEWAY) 412 dst = (struct sockaddr_in6 *)rt->rt_gateway; 413 414 /* 415 * If we are to forward the packet using the same interface 416 * as one we got the packet from, perhaps we should send a redirect 417 * to sender to shortcut a hop. 418 * Only send redirect if source is sending directly to us, 419 * and if packet was not source routed (or has any options). 420 * Also, don't send redirect if forwarding using a route 421 * modified by a redirect. 422 */ 423 if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 424 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 425 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) { 426 /* 427 * If the incoming interface is equal to the outgoing 428 * one, and the link attached to the interface is 429 * point-to-point, then it will be highly probable 430 * that a routing loop occurs. Thus, we immediately 431 * drop the packet and send an ICMPv6 error message. 432 * 433 * type/code is based on suggestion by Rich Draves. 434 * not sure if it is the best pick. 435 */ 436 icmp6_error(mcopy, ICMP6_DST_UNREACH, 437 ICMP6_DST_UNREACH_ADDR, 0); 438 goto bad; 439 } 440 type = ND_REDIRECT; 441 } 442 443 /* 444 * Fake scoped addresses. Note that even link-local source or 445 * destinaion can appear, if the originating node just sends the 446 * packet to us (without address resolution for the destination). 447 * Since both icmp6_error and icmp6_redirect_output fill the embedded 448 * link identifiers, we can do this stuff after making a copy for 449 * returning an error. 450 */ 451 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 452 /* 453 * See corresponding comments in ip6_output. 454 * XXX: but is it possible that ip6_forward() sends a packet 455 * to a loopback interface? I don't think so, and thus 456 * I bark here. (jinmei@kame.net) 457 * XXX: it is common to route invalid packets to loopback. 458 * also, the codepath will be visited on use of ::1 in 459 * rthdr. (itojun) 460 */ 461 #if 1 462 if (0) 463 #else 464 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 465 #endif 466 { 467 printf("ip6_forward: outgoing interface is loopback. " 468 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 469 ip6_sprintf(ip6bufs, &ip6->ip6_src), 470 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 471 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 472 if_name(rt->rt_ifp)); 473 } 474 475 /* we can just use rcvif in forwarding. */ 476 origifp = m->m_pkthdr.rcvif; 477 } 478 else 479 origifp = rt->rt_ifp; 480 /* 481 * clear embedded scope identifiers if necessary. 482 * in6_clearscope will touch the addresses only when necessary. 483 */ 484 in6_clearscope(&ip6->ip6_src); 485 in6_clearscope(&ip6->ip6_dst); 486 487 /* Jump over all PFIL processing if hooks are not active. */ 488 if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 489 goto pass; 490 491 odst = ip6->ip6_dst; 492 /* Run through list of hooks for output packets. */ 493 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 494 if (error != 0 || m == NULL) 495 goto freecopy; /* consumed by filter */ 496 ip6 = mtod(m, struct ip6_hdr *); 497 498 /* See if destination IP address was changed by packet filter. */ 499 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 500 m->m_flags |= M_SKIP_FIREWALL; 501 /* If destination is now ourself drop to ip6_input(). */ 502 if (in6_localip(&ip6->ip6_dst)) 503 m->m_flags |= M_FASTFWD_OURS; 504 else { 505 RTFREE(rt); 506 goto again; /* Redo the routing table lookup. */ 507 } 508 } 509 510 /* See if local, if yes, send it to netisr. */ 511 if (m->m_flags & M_FASTFWD_OURS) { 512 if (m->m_pkthdr.rcvif == NULL) 513 m->m_pkthdr.rcvif = V_loif; 514 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 515 m->m_pkthdr.csum_flags |= 516 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 517 m->m_pkthdr.csum_data = 0xffff; 518 } 519 #ifdef SCTP 520 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 521 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 522 #endif 523 error = netisr_queue(NETISR_IPV6, m); 524 goto out; 525 } 526 /* Or forward to some other address? */ 527 if ((m->m_flags & M_IP6_NEXTHOP) && 528 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 529 dst = (struct sockaddr_in6 *)&rin6.ro_dst; 530 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6)); 531 m->m_flags |= M_SKIP_FIREWALL; 532 m->m_flags &= ~M_IP6_NEXTHOP; 533 m_tag_delete(m, fwd_tag); 534 RTFREE(rt); 535 goto again2; 536 } 537 538 pass: 539 /* See if the size was changed by the packet filter. */ 540 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 541 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 542 if (mcopy) { 543 u_long mtu; 544 #ifdef IPSEC 545 size_t ipsechdrsiz; 546 #endif /* IPSEC */ 547 548 mtu = IN6_LINKMTU(rt->rt_ifp); 549 #ifdef IPSEC 550 /* 551 * When we do IPsec tunnel ingress, we need to play 552 * with the link value (decrement IPsec header size 553 * from mtu value). The code is much simpler than v4 554 * case, as we have the outgoing interface for 555 * encapsulated packet as "rt->rt_ifp". 556 */ 557 ipsechdrsiz = ipsec_hdrsiz(mcopy, IPSEC_DIR_OUTBOUND, 558 NULL); 559 if (ipsechdrsiz < mtu) 560 mtu -= ipsechdrsiz; 561 /* 562 * if mtu becomes less than minimum MTU, 563 * tell minimum MTU (and I'll need to fragment it). 564 */ 565 if (mtu < IPV6_MMTU) 566 mtu = IPV6_MMTU; 567 #endif /* IPSEC */ 568 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 569 } 570 goto bad; 571 } 572 573 error = nd6_output_ifp(rt->rt_ifp, origifp, m, dst, NULL); 574 if (error) { 575 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 576 IP6STAT_INC(ip6s_cantforward); 577 } else { 578 IP6STAT_INC(ip6s_forward); 579 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 580 if (type) 581 IP6STAT_INC(ip6s_redirectsent); 582 else { 583 if (mcopy) 584 goto freecopy; 585 } 586 } 587 588 if (mcopy == NULL) 589 goto out; 590 switch (error) { 591 case 0: 592 if (type == ND_REDIRECT) { 593 icmp6_redirect_output(mcopy, rt); 594 goto out; 595 } 596 goto freecopy; 597 598 case EMSGSIZE: 599 /* xxx MTU is constant in PPP? */ 600 goto freecopy; 601 602 case ENOBUFS: 603 /* Tell source to slow down like source quench in IP? */ 604 goto freecopy; 605 606 case ENETUNREACH: /* shouldn't happen, checked above */ 607 case EHOSTUNREACH: 608 case ENETDOWN: 609 case EHOSTDOWN: 610 default: 611 type = ICMP6_DST_UNREACH; 612 code = ICMP6_DST_UNREACH_ADDR; 613 break; 614 } 615 icmp6_error(mcopy, type, code, 0); 616 goto out; 617 618 freecopy: 619 m_freem(mcopy); 620 goto out; 621 bad: 622 m_freem(m); 623 out: 624 if (rt != NULL) 625 RTFREE(rt); 626 } 627