1 /* $FreeBSD$ */ 2 /* $KAME: ip6_forward.c,v 1.43 2000/07/16 07:50:49 itojun 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 "opt_ip6fw.h" 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mbuf.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/errno.h> 45 #include <sys/time.h> 46 #include <sys/syslog.h> 47 48 #include <net/if.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 #ifdef IPSEC 60 #include <netinet6/ipsec.h> 61 #include <netinet6/ipsec6.h> 62 #include <netkey/key.h> 63 #endif /* IPSEC */ 64 65 #include <netinet6/ip6_fw.h> 66 67 #include <net/net_osdep.h> 68 69 struct route_in6 ip6_forward_rt; 70 71 /* 72 * Forward a packet. If some error occurs return the sender 73 * an icmp packet. Note we can't always generate a meaningful 74 * icmp message because icmp doesn't have a large enough repertoire 75 * of codes and types. 76 * 77 * If not forwarding, just drop the packet. This could be confusing 78 * if ipforwarding was zero but some routing protocol was advancing 79 * us as a gateway to somewhere. However, we must let the routing 80 * protocol deal with that. 81 * 82 */ 83 84 void 85 ip6_forward(m, srcrt) 86 struct mbuf *m; 87 int srcrt; 88 { 89 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 90 register struct sockaddr_in6 *dst; 91 register struct rtentry *rt; 92 int error, type = 0, code = 0; 93 struct mbuf *mcopy = NULL; 94 struct ifnet *origifp; /* maybe unnecessary */ 95 #ifdef IPSEC 96 struct secpolicy *sp = NULL; 97 #endif 98 99 #ifdef IPSEC 100 /* 101 * Check AH/ESP integrity. 102 */ 103 /* 104 * Don't increment ip6s_cantforward because this is the check 105 * before forwarding packet actually. 106 */ 107 if (ipsec6_in_reject(m, NULL)) { 108 ipsec6stat.in_polvio++; 109 m_freem(m); 110 return; 111 } 112 #endif /*IPSEC*/ 113 114 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 115 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 116 ip6stat.ip6s_cantforward++; 117 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 118 if (ip6_log_time + ip6_log_interval < time_second) { 119 ip6_log_time = time_second; 120 log(LOG_DEBUG, 121 "cannot forward " 122 "from %s to %s nxt %d received on %s\n", 123 ip6_sprintf(&ip6->ip6_src), 124 ip6_sprintf(&ip6->ip6_dst), 125 ip6->ip6_nxt, 126 if_name(m->m_pkthdr.rcvif)); 127 } 128 m_freem(m); 129 return; 130 } 131 132 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 133 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 134 icmp6_error(m, ICMP6_TIME_EXCEEDED, 135 ICMP6_TIME_EXCEED_TRANSIT, 0); 136 return; 137 } 138 ip6->ip6_hlim -= IPV6_HLIMDEC; 139 140 /* 141 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 142 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 143 * we need to generate an ICMP6 message to the src. 144 * Thanks to M_EXT, in most cases copy will not occur. 145 * 146 * It is important to save it before IPsec processing as IPsec 147 * processing may modify the mbuf. 148 */ 149 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 150 151 #ifdef IPSEC 152 /* get a security policy for this packet */ 153 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 154 if (sp == NULL) { 155 ipsec6stat.out_inval++; 156 ip6stat.ip6s_cantforward++; 157 if (mcopy) { 158 #if 0 159 /* XXX: what icmp ? */ 160 #else 161 m_freem(mcopy); 162 #endif 163 } 164 m_freem(m); 165 return; 166 } 167 168 error = 0; 169 170 /* check policy */ 171 switch (sp->policy) { 172 case IPSEC_POLICY_DISCARD: 173 /* 174 * This packet is just discarded. 175 */ 176 ipsec6stat.out_polvio++; 177 ip6stat.ip6s_cantforward++; 178 key_freesp(sp); 179 if (mcopy) { 180 #if 0 181 /* XXX: what icmp ? */ 182 #else 183 m_freem(mcopy); 184 #endif 185 } 186 m_freem(m); 187 return; 188 189 case IPSEC_POLICY_BYPASS: 190 case IPSEC_POLICY_NONE: 191 /* no need to do IPsec. */ 192 key_freesp(sp); 193 goto skip_ipsec; 194 195 case IPSEC_POLICY_IPSEC: 196 if (sp->req == NULL) { 197 /* XXX should be panic ? */ 198 printf("ip6_forward: No IPsec request specified.\n"); 199 ip6stat.ip6s_cantforward++; 200 key_freesp(sp); 201 if (mcopy) { 202 #if 0 203 /* XXX: what icmp ? */ 204 #else 205 m_freem(mcopy); 206 #endif 207 } 208 m_freem(m); 209 return; 210 } 211 /* do IPsec */ 212 break; 213 214 case IPSEC_POLICY_ENTRUST: 215 default: 216 /* should be panic ?? */ 217 printf("ip6_forward: Invalid policy found. %d\n", sp->policy); 218 key_freesp(sp); 219 goto skip_ipsec; 220 } 221 222 { 223 struct ipsec_output_state state; 224 225 /* 226 * All the extension headers will become inaccessible 227 * (since they can be encrypted). 228 * Don't panic, we need no more updates to extension headers 229 * on inner IPv6 packet (since they are now encapsulated). 230 * 231 * IPv6 [ESP|AH] IPv6 [extension headers] payload 232 */ 233 bzero(&state, sizeof(state)); 234 state.m = m; 235 state.ro = NULL; /* update at ipsec6_output_tunnel() */ 236 state.dst = NULL; /* update at ipsec6_output_tunnel() */ 237 238 error = ipsec6_output_tunnel(&state, sp, 0); 239 240 m = state.m; 241 #if 0 /* XXX allocate a route (ro, dst) again later */ 242 ro = (struct route_in6 *)state.ro; 243 dst = (struct sockaddr_in6 *)state.dst; 244 #endif 245 key_freesp(sp); 246 247 if (error) { 248 /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 249 switch (error) { 250 case EHOSTUNREACH: 251 case ENETUNREACH: 252 case EMSGSIZE: 253 case ENOBUFS: 254 case ENOMEM: 255 break; 256 default: 257 printf("ip6_output (ipsec): error code %d\n", error); 258 /*fall through*/ 259 case ENOENT: 260 /* don't show these error codes to the user */ 261 break; 262 } 263 ip6stat.ip6s_cantforward++; 264 if (mcopy) { 265 #if 0 266 /* XXX: what icmp ? */ 267 #else 268 m_freem(mcopy); 269 #endif 270 } 271 m_freem(m); 272 return; 273 } 274 } 275 skip_ipsec: 276 #endif /* IPSEC */ 277 278 dst = &ip6_forward_rt.ro_dst; 279 if (!srcrt) { 280 /* 281 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst 282 */ 283 if (ip6_forward_rt.ro_rt == 0 || 284 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) { 285 if (ip6_forward_rt.ro_rt) { 286 RTFREE(ip6_forward_rt.ro_rt); 287 ip6_forward_rt.ro_rt = 0; 288 } 289 /* this probably fails but give it a try again */ 290 rtalloc_ign((struct route *)&ip6_forward_rt, 291 RTF_PRCLONING); 292 } 293 294 if (ip6_forward_rt.ro_rt == 0) { 295 ip6stat.ip6s_noroute++; 296 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 297 if (mcopy) { 298 icmp6_error(mcopy, ICMP6_DST_UNREACH, 299 ICMP6_DST_UNREACH_NOROUTE, 0); 300 } 301 m_freem(m); 302 return; 303 } 304 } else if ((rt = ip6_forward_rt.ro_rt) == 0 || 305 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 306 if (ip6_forward_rt.ro_rt) { 307 RTFREE(ip6_forward_rt.ro_rt); 308 ip6_forward_rt.ro_rt = 0; 309 } 310 bzero(dst, sizeof(*dst)); 311 dst->sin6_len = sizeof(struct sockaddr_in6); 312 dst->sin6_family = AF_INET6; 313 dst->sin6_addr = ip6->ip6_dst; 314 315 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 316 if (ip6_forward_rt.ro_rt == 0) { 317 ip6stat.ip6s_noroute++; 318 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 319 if (mcopy) { 320 icmp6_error(mcopy, ICMP6_DST_UNREACH, 321 ICMP6_DST_UNREACH_NOROUTE, 0); 322 } 323 m_freem(m); 324 return; 325 } 326 } 327 rt = ip6_forward_rt.ro_rt; 328 329 /* 330 * Scope check: if a packet can't be delivered to its destination 331 * for the reason that the destination is beyond the scope of the 332 * source address, discard the packet and return an icmp6 destination 333 * unreachable error with Code 2 (beyond scope of source address). 334 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1] 335 */ 336 if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) != 337 in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) { 338 ip6stat.ip6s_cantforward++; 339 ip6stat.ip6s_badscope++; 340 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 341 342 if (ip6_log_time + ip6_log_interval < time_second) { 343 ip6_log_time = time_second; 344 log(LOG_DEBUG, 345 "cannot forward " 346 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 347 ip6_sprintf(&ip6->ip6_src), 348 ip6_sprintf(&ip6->ip6_dst), 349 ip6->ip6_nxt, 350 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 351 } 352 if (mcopy) 353 icmp6_error(mcopy, ICMP6_DST_UNREACH, 354 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 355 m_freem(m); 356 return; 357 } 358 359 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) { 360 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 361 if (mcopy) { 362 u_long mtu; 363 #ifdef IPSEC 364 struct secpolicy *sp; 365 int ipsecerror; 366 size_t ipsechdrsiz; 367 #endif 368 369 mtu = rt->rt_ifp->if_mtu; 370 #ifdef IPSEC 371 /* 372 * When we do IPsec tunnel ingress, we need to play 373 * with if_mtu value (decrement IPsec header size 374 * from mtu value). The code is much simpler than v4 375 * case, as we have the outgoing interface for 376 * encapsulated packet as "rt->rt_ifp". 377 */ 378 sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND, 379 IP_FORWARDING, &ipsecerror); 380 if (sp) { 381 ipsechdrsiz = ipsec6_hdrsiz(mcopy, 382 IPSEC_DIR_OUTBOUND, NULL); 383 if (ipsechdrsiz < mtu) 384 mtu -= ipsechdrsiz; 385 } 386 387 /* 388 * if mtu becomes less than minimum MTU, 389 * tell minimum MTU (and I'll need to fragment it). 390 */ 391 if (mtu < IPV6_MMTU) 392 mtu = IPV6_MMTU; 393 #endif 394 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 395 } 396 m_freem(m); 397 return; 398 } 399 400 if (rt->rt_flags & RTF_GATEWAY) 401 dst = (struct sockaddr_in6 *)rt->rt_gateway; 402 403 /* 404 * If we are to forward the packet using the same interface 405 * as one we got the packet from, perhaps we should send a redirect 406 * to sender to shortcut a hop. 407 * Only send redirect if source is sending directly to us, 408 * and if packet was not source routed (or has any options). 409 * Also, don't send redirect if forwarding using a route 410 * modified by a redirect. 411 */ 412 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 413 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) 414 type = ND_REDIRECT; 415 416 /* 417 * Check with the firewall... 418 */ 419 if (ip6_fw_enable && ip6_fw_chk_ptr) { 420 u_short port = 0; 421 /* If ipfw says divert, we have to just drop packet */ 422 if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) { 423 m_freem(m); 424 goto freecopy; 425 } 426 if (!m) 427 goto freecopy; 428 } 429 430 /* 431 * Fake scoped addresses. Note that even link-local source or 432 * destinaion can appear, if the originating node just sends the 433 * packet to us (without address resolution for the destination). 434 * Since both icmp6_error and icmp6_redirect_output fill the embedded 435 * link identifiers, we can do this stuff after make a copy for 436 * returning error. 437 */ 438 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 439 /* 440 * See corresponding comments in ip6_output. 441 * XXX: but is it possible that ip6_forward() sends a packet 442 * to a loopback interface? I don't think so, and thus 443 * I bark here. (jinmei@kame.net) 444 * XXX: it is common to route invalid packets to loopback. 445 * also, the codepath will be visited on use of ::1 in 446 * rthdr. (itojun) 447 */ 448 #if 1 449 if (0) 450 #else 451 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 452 #endif 453 { 454 printf("ip6_forward: outgoing interface is loopback. " 455 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 456 ip6_sprintf(&ip6->ip6_src), 457 ip6_sprintf(&ip6->ip6_dst), 458 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 459 if_name(rt->rt_ifp)); 460 } 461 462 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 463 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])]; 464 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 465 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])]; 466 else 467 origifp = rt->rt_ifp; 468 } 469 else 470 origifp = rt->rt_ifp; 471 #ifndef FAKE_LOOPBACK_IF 472 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) 473 #else 474 if (1) 475 #endif 476 { 477 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 478 ip6->ip6_src.s6_addr16[1] = 0; 479 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 480 ip6->ip6_dst.s6_addr16[1] = 0; 481 } 482 483 #ifdef OLDIP6OUTPUT 484 error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, 485 (struct sockaddr *)dst, 486 ip6_forward_rt.ro_rt); 487 #else 488 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 489 #endif 490 if (error) { 491 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 492 ip6stat.ip6s_cantforward++; 493 } else { 494 ip6stat.ip6s_forward++; 495 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 496 if (type) 497 ip6stat.ip6s_redirectsent++; 498 else { 499 if (mcopy) 500 goto freecopy; 501 } 502 } 503 if (mcopy == NULL) 504 return; 505 506 switch (error) { 507 case 0: 508 #if 1 509 if (type == ND_REDIRECT) { 510 icmp6_redirect_output(mcopy, rt); 511 return; 512 } 513 #endif 514 goto freecopy; 515 516 case EMSGSIZE: 517 /* xxx MTU is constant in PPP? */ 518 goto freecopy; 519 520 case ENOBUFS: 521 /* Tell source to slow down like source quench in IP? */ 522 goto freecopy; 523 524 case ENETUNREACH: /* shouldn't happen, checked above */ 525 case EHOSTUNREACH: 526 case ENETDOWN: 527 case EHOSTDOWN: 528 default: 529 type = ICMP6_DST_UNREACH; 530 code = ICMP6_DST_UNREACH_ADDR; 531 break; 532 } 533 icmp6_error(mcopy, type, code, 0); 534 return; 535 536 freecopy: 537 m_freem(mcopy); 538 return; 539 } 540