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 #include <netipsec/ipsec_support.h> 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 void 87 ip6_forward(struct mbuf *m, int srcrt) 88 { 89 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 90 struct sockaddr_in6 *dst = NULL; 91 struct rtentry *rt = NULL; 92 struct route_in6 rin6; 93 int error, type = 0, code = 0; 94 struct mbuf *mcopy = NULL; 95 struct ifnet *origifp; /* maybe unnecessary */ 96 u_int32_t inzone, outzone; 97 struct in6_addr src_in6, dst_in6, odst; 98 struct m_tag *fwd_tag; 99 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 100 101 /* 102 * Do not forward packets to multicast destination (should be handled 103 * by ip6_mforward(). 104 * Do not forward packets with unspecified source. It was discussed 105 * in July 2000, on the ipngwg mailing list. 106 */ 107 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 108 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 109 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 110 IP6STAT_INC(ip6s_cantforward); 111 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 112 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 113 V_ip6_log_time = time_uptime; 114 log(LOG_DEBUG, 115 "cannot forward " 116 "from %s to %s nxt %d received on %s\n", 117 ip6_sprintf(ip6bufs, &ip6->ip6_src), 118 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 119 ip6->ip6_nxt, 120 if_name(m->m_pkthdr.rcvif)); 121 } 122 m_freem(m); 123 return; 124 } 125 126 if ( 127 #ifdef IPSTEALTH 128 V_ip6stealth == 0 && 129 #endif 130 ip6->ip6_hlim <= IPV6_HLIMDEC) { 131 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 132 icmp6_error(m, ICMP6_TIME_EXCEEDED, 133 ICMP6_TIME_EXCEED_TRANSIT, 0); 134 return; 135 } 136 137 /* 138 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 139 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 140 * we need to generate an ICMP6 message to the src. 141 * Thanks to M_EXT, in most cases copy will not occur. 142 * 143 * It is important to save it before IPsec processing as IPsec 144 * processing may modify the mbuf. 145 */ 146 mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN), 147 M_NOWAIT); 148 #ifdef IPSTEALTH 149 if (V_ip6stealth == 0) 150 #endif 151 ip6->ip6_hlim -= IPV6_HLIMDEC; 152 153 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 154 if (IPSEC_ENABLED(ipv6)) { 155 if ((error = IPSEC_FORWARD(ipv6, m)) != 0) { 156 /* mbuf consumed by IPsec */ 157 m_freem(mcopy); 158 if (error != EINPROGRESS) 159 IP6STAT_INC(ip6s_cantforward); 160 return; 161 } 162 /* No IPsec processing required */ 163 } 164 #endif 165 again: 166 bzero(&rin6, sizeof(struct route_in6)); 167 dst = (struct sockaddr_in6 *)&rin6.ro_dst; 168 dst->sin6_len = sizeof(struct sockaddr_in6); 169 dst->sin6_family = AF_INET6; 170 dst->sin6_addr = ip6->ip6_dst; 171 again2: 172 rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m)); 173 rt = rin6.ro_rt; 174 if (rin6.ro_rt != NULL) 175 RT_UNLOCK(rin6.ro_rt); 176 else { 177 IP6STAT_INC(ip6s_noroute); 178 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 179 if (mcopy) { 180 icmp6_error(mcopy, ICMP6_DST_UNREACH, 181 ICMP6_DST_UNREACH_NOROUTE, 0); 182 } 183 goto bad; 184 } 185 186 /* 187 * Source scope check: if a packet can't be delivered to its 188 * destination for the reason that the destination is beyond the scope 189 * of the source address, discard the packet and return an icmp6 190 * destination unreachable error with Code 2 (beyond scope of source 191 * address). We use a local copy of ip6_src, since in6_setscope() 192 * will possibly modify its first argument. 193 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1] 194 */ 195 src_in6 = ip6->ip6_src; 196 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 197 /* XXX: this should not happen */ 198 IP6STAT_INC(ip6s_cantforward); 199 IP6STAT_INC(ip6s_badscope); 200 goto bad; 201 } 202 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 203 IP6STAT_INC(ip6s_cantforward); 204 IP6STAT_INC(ip6s_badscope); 205 goto bad; 206 } 207 if (inzone != outzone) { 208 IP6STAT_INC(ip6s_cantforward); 209 IP6STAT_INC(ip6s_badscope); 210 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 211 212 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) { 213 V_ip6_log_time = time_uptime; 214 log(LOG_DEBUG, 215 "cannot forward " 216 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 217 ip6_sprintf(ip6bufs, &ip6->ip6_src), 218 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 219 ip6->ip6_nxt, 220 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 221 } 222 if (mcopy) 223 icmp6_error(mcopy, ICMP6_DST_UNREACH, 224 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 225 goto bad; 226 } 227 228 /* 229 * Destination scope check: if a packet is going to break the scope 230 * zone of packet's destination address, discard it. This case should 231 * usually be prevented by appropriately-configured routing table, but 232 * we need an explicit check because we may mistakenly forward the 233 * packet to a different zone by (e.g.) a default route. 234 */ 235 dst_in6 = ip6->ip6_dst; 236 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 237 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 238 inzone != outzone) { 239 IP6STAT_INC(ip6s_cantforward); 240 IP6STAT_INC(ip6s_badscope); 241 goto bad; 242 } 243 244 if (rt->rt_flags & RTF_GATEWAY) 245 dst = (struct sockaddr_in6 *)rt->rt_gateway; 246 247 /* 248 * If we are to forward the packet using the same interface 249 * as one we got the packet from, perhaps we should send a redirect 250 * to sender to shortcut a hop. 251 * Only send redirect if source is sending directly to us, 252 * and if packet was not source routed (or has any options). 253 * Also, don't send redirect if forwarding using a route 254 * modified by a redirect. 255 */ 256 if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 257 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 258 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) { 259 /* 260 * If the incoming interface is equal to the outgoing 261 * one, and the link attached to the interface is 262 * point-to-point, then it will be highly probable 263 * that a routing loop occurs. Thus, we immediately 264 * drop the packet and send an ICMPv6 error message. 265 * 266 * type/code is based on suggestion by Rich Draves. 267 * not sure if it is the best pick. 268 */ 269 icmp6_error(mcopy, ICMP6_DST_UNREACH, 270 ICMP6_DST_UNREACH_ADDR, 0); 271 goto bad; 272 } 273 type = ND_REDIRECT; 274 } 275 276 /* 277 * Fake scoped addresses. Note that even link-local source or 278 * destinaion can appear, if the originating node just sends the 279 * packet to us (without address resolution for the destination). 280 * Since both icmp6_error and icmp6_redirect_output fill the embedded 281 * link identifiers, we can do this stuff after making a copy for 282 * returning an error. 283 */ 284 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 285 /* 286 * See corresponding comments in ip6_output. 287 * XXX: but is it possible that ip6_forward() sends a packet 288 * to a loopback interface? I don't think so, and thus 289 * I bark here. (jinmei@kame.net) 290 * XXX: it is common to route invalid packets to loopback. 291 * also, the codepath will be visited on use of ::1 in 292 * rthdr. (itojun) 293 */ 294 #if 1 295 if (0) 296 #else 297 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 298 #endif 299 { 300 printf("ip6_forward: outgoing interface is loopback. " 301 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 302 ip6_sprintf(ip6bufs, &ip6->ip6_src), 303 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 304 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 305 if_name(rt->rt_ifp)); 306 } 307 308 /* we can just use rcvif in forwarding. */ 309 origifp = m->m_pkthdr.rcvif; 310 } 311 else 312 origifp = rt->rt_ifp; 313 /* 314 * clear embedded scope identifiers if necessary. 315 * in6_clearscope will touch the addresses only when necessary. 316 */ 317 in6_clearscope(&ip6->ip6_src); 318 in6_clearscope(&ip6->ip6_dst); 319 320 /* Jump over all PFIL processing if hooks are not active. */ 321 if (!PFIL_HOOKED(&V_inet6_pfil_hook)) 322 goto pass; 323 324 odst = ip6->ip6_dst; 325 /* Run through list of hooks for output packets. */ 326 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL); 327 if (error != 0 || m == NULL) 328 goto freecopy; /* consumed by filter */ 329 ip6 = mtod(m, struct ip6_hdr *); 330 331 /* See if destination IP address was changed by packet filter. */ 332 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) { 333 m->m_flags |= M_SKIP_FIREWALL; 334 /* If destination is now ourself drop to ip6_input(). */ 335 if (in6_localip(&ip6->ip6_dst)) 336 m->m_flags |= M_FASTFWD_OURS; 337 else { 338 RTFREE(rt); 339 goto again; /* Redo the routing table lookup. */ 340 } 341 } 342 343 /* See if local, if yes, send it to netisr. */ 344 if (m->m_flags & M_FASTFWD_OURS) { 345 if (m->m_pkthdr.rcvif == NULL) 346 m->m_pkthdr.rcvif = V_loif; 347 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 348 m->m_pkthdr.csum_flags |= 349 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR; 350 m->m_pkthdr.csum_data = 0xffff; 351 } 352 #ifdef SCTP 353 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) 354 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 355 #endif 356 error = netisr_queue(NETISR_IPV6, m); 357 goto out; 358 } 359 /* Or forward to some other address? */ 360 if ((m->m_flags & M_IP6_NEXTHOP) && 361 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 362 dst = (struct sockaddr_in6 *)&rin6.ro_dst; 363 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6)); 364 m->m_flags |= M_SKIP_FIREWALL; 365 m->m_flags &= ~M_IP6_NEXTHOP; 366 m_tag_delete(m, fwd_tag); 367 RTFREE(rt); 368 goto again2; 369 } 370 371 pass: 372 /* See if the size was changed by the packet filter. */ 373 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 374 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 375 if (mcopy) 376 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, 377 IN6_LINKMTU(rt->rt_ifp)); 378 goto bad; 379 } 380 381 error = nd6_output_ifp(rt->rt_ifp, origifp, m, dst, NULL); 382 if (error) { 383 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 384 IP6STAT_INC(ip6s_cantforward); 385 } else { 386 IP6STAT_INC(ip6s_forward); 387 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 388 if (type) 389 IP6STAT_INC(ip6s_redirectsent); 390 else { 391 if (mcopy) 392 goto freecopy; 393 } 394 } 395 396 if (mcopy == NULL) 397 goto out; 398 switch (error) { 399 case 0: 400 if (type == ND_REDIRECT) { 401 icmp6_redirect_output(mcopy, rt); 402 goto out; 403 } 404 goto freecopy; 405 406 case EMSGSIZE: 407 /* xxx MTU is constant in PPP? */ 408 goto freecopy; 409 410 case ENOBUFS: 411 /* Tell source to slow down like source quench in IP? */ 412 goto freecopy; 413 414 case ENETUNREACH: /* shouldn't happen, checked above */ 415 case EHOSTUNREACH: 416 case ENETDOWN: 417 case EHOSTDOWN: 418 default: 419 type = ICMP6_DST_UNREACH; 420 code = ICMP6_DST_UNREACH_ADDR; 421 break; 422 } 423 icmp6_error(mcopy, type, code, 0); 424 goto out; 425 426 freecopy: 427 m_freem(mcopy); 428 goto out; 429 bad: 430 m_freem(m); 431 out: 432 if (rt != NULL) 433 RTFREE(rt); 434 } 435