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