1 /* $NetBSD: ip6_forward.c,v 1.73 2014/05/30 01:39:03 christos Exp $ */ 2 /* $KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane 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 <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.73 2014/05/30 01:39:03 christos Exp $"); 35 36 #include "opt_gateway.h" 37 #include "opt_ipsec.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/domain.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 #include <sys/errno.h> 47 #include <sys/time.h> 48 #include <sys/kernel.h> 49 #include <sys/syslog.h> 50 51 #include <net/if.h> 52 #include <net/route.h> 53 #include <net/pfil.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_var.h> 57 #include <netinet/ip_var.h> 58 #include <netinet/ip6.h> 59 #include <netinet6/ip6_var.h> 60 #include <netinet6/ip6_private.h> 61 #include <netinet6/scope6_var.h> 62 #include <netinet/icmp6.h> 63 #include <netinet6/nd6.h> 64 65 #ifdef IPSEC 66 #include <netipsec/ipsec.h> 67 #include <netipsec/ipsec6.h> 68 #include <netipsec/key.h> 69 #include <netipsec/xform.h> 70 #endif /* IPSEC */ 71 72 #include <net/net_osdep.h> 73 74 struct route ip6_forward_rt; 75 76 extern pfil_head_t *inet6_pfil_hook; /* XXX */ 77 78 /* 79 * Forward a packet. If some error occurs return the sender 80 * an icmp packet. Note we can't always generate a meaningful 81 * icmp message because icmp doesn't have a large enough repertoire 82 * of codes and types. 83 * 84 * If not forwarding, just drop the packet. This could be confusing 85 * if ipforwarding was zero but some routing protocol was advancing 86 * us as a gateway to somewhere. However, we must let the routing 87 * protocol deal with that. 88 * 89 */ 90 91 void 92 ip6_forward(struct mbuf *m, int srcrt) 93 { 94 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 95 const struct sockaddr_in6 *dst; 96 struct rtentry *rt; 97 int error = 0, type = 0, code = 0; 98 struct mbuf *mcopy = NULL; 99 struct ifnet *origifp; /* maybe unnecessary */ 100 u_int32_t inzone, outzone; 101 struct in6_addr src_in6, dst_in6; 102 #ifdef IPSEC 103 int needipsec = 0; 104 struct secpolicy *sp = NULL; 105 #endif 106 107 /* 108 * Clear any in-bound checksum flags for this packet. 109 */ 110 m->m_pkthdr.csum_flags = 0; 111 112 /* 113 * Do not forward packets to multicast destination (should be handled 114 * by ip6_mforward(). 115 * Do not forward packets with unspecified source. It was discussed 116 * in July 2000, on ipngwg mailing list. 117 */ 118 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 119 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 120 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 121 IP6_STATINC(IP6_STAT_CANTFORWARD); 122 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 123 if (ip6_log_time + ip6_log_interval < time_second) { 124 ip6_log_time = time_second; 125 log(LOG_DEBUG, 126 "cannot forward " 127 "from %s to %s nxt %d received on %s\n", 128 ip6_sprintf(&ip6->ip6_src), 129 ip6_sprintf(&ip6->ip6_dst), 130 ip6->ip6_nxt, 131 if_name(m->m_pkthdr.rcvif)); 132 } 133 m_freem(m); 134 return; 135 } 136 137 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 138 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 139 icmp6_error(m, ICMP6_TIME_EXCEEDED, 140 ICMP6_TIME_EXCEED_TRANSIT, 0); 141 return; 142 } 143 ip6->ip6_hlim -= IPV6_HLIMDEC; 144 145 /* 146 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 147 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 148 * we need to generate an ICMP6 message to the src. 149 * Thanks to M_EXT, in most cases copy will not occur. 150 * 151 * It is important to save it before IPsec processing as IPsec 152 * processing may modify the mbuf. 153 */ 154 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 155 156 #ifdef IPSEC 157 if (ipsec_used) { 158 /* Check the security policy (SP) for the packet */ 159 160 sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error); 161 if (error != 0) { 162 /* 163 * Hack: -EINVAL is used to signal that a packet 164 * should be silently discarded. This is typically 165 * because we asked key management for an SA and 166 * it was delayed (e.g. kicked up to IKE). 167 */ 168 if (error == -EINVAL) 169 error = 0; 170 goto freecopy; 171 } 172 } 173 #endif /* IPSEC */ 174 175 if (srcrt) { 176 union { 177 struct sockaddr dst; 178 struct sockaddr_in6 dst6; 179 } u; 180 181 sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0); 182 if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) { 183 IP6_STATINC(IP6_STAT_NOROUTE); 184 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 185 if (mcopy) { 186 icmp6_error(mcopy, ICMP6_DST_UNREACH, 187 ICMP6_DST_UNREACH_NOROUTE, 0); 188 } 189 m_freem(m); 190 return; 191 } 192 } else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL && 193 (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) { 194 /* 195 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to 196 * ip6->ip6_dst 197 */ 198 IP6_STATINC(IP6_STAT_NOROUTE); 199 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */ 200 if (mcopy) { 201 icmp6_error(mcopy, ICMP6_DST_UNREACH, 202 ICMP6_DST_UNREACH_NOROUTE, 0); 203 } 204 m_freem(m); 205 return; 206 } 207 dst = satocsin6(rtcache_getdst(&ip6_forward_rt)); 208 209 /* 210 * Source scope check: if a packet can't be delivered to its 211 * destination for the reason that the destination is beyond the scope 212 * of the source address, discard the packet and return an icmp6 213 * destination unreachable error with Code 2 (beyond scope of source 214 * address). We use a local copy of ip6_src, since in6_setscope() 215 * will possibly modify its first argument. 216 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1] 217 */ 218 src_in6 = ip6->ip6_src; 219 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) { 220 /* XXX: this should not happen */ 221 uint64_t *ip6s = IP6_STAT_GETREF(); 222 ip6s[IP6_STAT_CANTFORWARD]++; 223 ip6s[IP6_STAT_BADSCOPE]++; 224 IP6_STAT_PUTREF(); 225 m_freem(m); 226 return; 227 } 228 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) { 229 uint64_t *ip6s = IP6_STAT_GETREF(); 230 ip6s[IP6_STAT_CANTFORWARD]++; 231 ip6s[IP6_STAT_BADSCOPE]++; 232 IP6_STAT_PUTREF(); 233 m_freem(m); 234 return; 235 } 236 if (inzone != outzone) { 237 uint64_t *ip6s = IP6_STAT_GETREF(); 238 ip6s[IP6_STAT_CANTFORWARD]++; 239 ip6s[IP6_STAT_BADSCOPE]++; 240 IP6_STAT_PUTREF(); 241 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 242 243 if (ip6_log_time + ip6_log_interval < time_second) { 244 ip6_log_time = time_second; 245 log(LOG_DEBUG, 246 "cannot forward " 247 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 248 ip6_sprintf(&ip6->ip6_src), 249 ip6_sprintf(&ip6->ip6_dst), 250 ip6->ip6_nxt, 251 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 252 } 253 if (mcopy) 254 icmp6_error(mcopy, ICMP6_DST_UNREACH, 255 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 256 m_freem(m); 257 return; 258 } 259 #ifdef IPSEC 260 /* 261 * If we need to encapsulate the packet, do it here 262 * ipsec6_proces_packet will send the packet using ip6_output 263 */ 264 if (needipsec) { 265 int s = splsoftnet(); 266 error = ipsec6_process_packet(m, sp->req); 267 splx(s); 268 if (mcopy) 269 goto freecopy; 270 } 271 #endif 272 273 /* 274 * Destination scope check: if a packet is going to break the scope 275 * zone of packet's destination address, discard it. This case should 276 * usually be prevented by appropriately-configured routing table, but 277 * we need an explicit check because we may mistakenly forward the 278 * packet to a different zone by (e.g.) a default route. 279 */ 280 dst_in6 = ip6->ip6_dst; 281 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 || 282 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 || 283 inzone != outzone) { 284 uint64_t *ip6s = IP6_STAT_GETREF(); 285 ip6s[IP6_STAT_CANTFORWARD]++; 286 ip6s[IP6_STAT_BADSCOPE]++; 287 IP6_STAT_PUTREF(); 288 m_freem(m); 289 return; 290 } 291 292 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 293 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 294 if (mcopy) { 295 u_long mtu; 296 297 mtu = IN6_LINKMTU(rt->rt_ifp); 298 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 299 } 300 m_freem(m); 301 return; 302 } 303 304 if (rt->rt_flags & RTF_GATEWAY) 305 dst = (struct sockaddr_in6 *)rt->rt_gateway; 306 307 /* 308 * If we are to forward the packet using the same interface 309 * as one we got the packet from, perhaps we should send a redirect 310 * to sender to shortcut a hop. 311 * Only send redirect if source is sending directly to us, 312 * and if packet was not source routed (or has any options). 313 * Also, don't send redirect if forwarding using a route 314 * modified by a redirect. 315 */ 316 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects && 317 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 318 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) && 319 nd6_is_addr_neighbor( 320 satocsin6(rtcache_getdst(&ip6_forward_rt)), 321 rt->rt_ifp)) { 322 /* 323 * If the incoming interface is equal to the outgoing 324 * one, the link attached to the interface is 325 * point-to-point, and the IPv6 destination is 326 * regarded as on-link on the link, then it will be 327 * highly probable that the destination address does 328 * not exist on the link and that the packet is going 329 * to loop. Thus, we immediately drop the packet and 330 * send an ICMPv6 error message. 331 * For other routing loops, we dare to let the packet 332 * go to the loop, so that a remote diagnosing host 333 * can detect the loop by traceroute. 334 * type/code is based on suggestion by Rich Draves. 335 * not sure if it is the best pick. 336 */ 337 icmp6_error(mcopy, ICMP6_DST_UNREACH, 338 ICMP6_DST_UNREACH_ADDR, 0); 339 m_freem(m); 340 return; 341 } 342 type = ND_REDIRECT; 343 } 344 345 /* 346 * Fake scoped addresses. Note that even link-local source or 347 * destinaion can appear, if the originating node just sends the 348 * packet to us (without address resolution for the destination). 349 * Since both icmp6_error and icmp6_redirect_output fill the embedded 350 * link identifiers, we can do this stuff after making a copy for 351 * returning an error. 352 */ 353 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 354 /* 355 * See corresponding comments in ip6_output. 356 * XXX: but is it possible that ip6_forward() sends a packet 357 * to a loopback interface? I don't think so, and thus 358 * I bark here. (jinmei@kame.net) 359 * XXX: it is common to route invalid packets to loopback. 360 * also, the codepath will be visited on use of ::1 in 361 * rthdr. (itojun) 362 */ 363 #if 1 364 if (0) 365 #else 366 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0) 367 #endif 368 { 369 printf("ip6_forward: outgoing interface is loopback. " 370 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 371 ip6_sprintf(&ip6->ip6_src), 372 ip6_sprintf(&ip6->ip6_dst), 373 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 374 if_name(rt->rt_ifp)); 375 } 376 377 /* we can just use rcvif in forwarding. */ 378 origifp = m->m_pkthdr.rcvif; 379 } 380 else 381 origifp = rt->rt_ifp; 382 /* 383 * clear embedded scope identifiers if necessary. 384 * in6_clearscope will touch the addresses only when necessary. 385 */ 386 in6_clearscope(&ip6->ip6_src); 387 in6_clearscope(&ip6->ip6_dst); 388 389 /* 390 * Run through list of hooks for output packets. 391 */ 392 if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp, 393 PFIL_OUT)) != 0) 394 goto senderr; 395 if (m == NULL) 396 goto freecopy; 397 ip6 = mtod(m, struct ip6_hdr *); 398 399 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 400 if (error) { 401 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 402 IP6_STATINC(IP6_STAT_CANTFORWARD); 403 } else { 404 IP6_STATINC(IP6_STAT_FORWARD); 405 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 406 if (type) 407 IP6_STATINC(IP6_STAT_REDIRECTSENT); 408 else { 409 #ifdef GATEWAY 410 if (m->m_flags & M_CANFASTFWD) 411 ip6flow_create(&ip6_forward_rt, m); 412 #endif 413 if (mcopy) 414 goto freecopy; 415 } 416 } 417 418 senderr: 419 if (mcopy == NULL) 420 return; 421 switch (error) { 422 case 0: 423 if (type == ND_REDIRECT) { 424 icmp6_redirect_output(mcopy, rt); 425 return; 426 } 427 goto freecopy; 428 429 case EMSGSIZE: 430 /* xxx MTU is constant in PPP? */ 431 goto freecopy; 432 433 case ENOBUFS: 434 /* Tell source to slow down like source quench in IP? */ 435 goto freecopy; 436 437 case ENETUNREACH: /* shouldn't happen, checked above */ 438 case EHOSTUNREACH: 439 case ENETDOWN: 440 case EHOSTDOWN: 441 default: 442 type = ICMP6_DST_UNREACH; 443 code = ICMP6_DST_UNREACH_ADDR; 444 break; 445 } 446 icmp6_error(mcopy, type, code, 0); 447 return; 448 449 freecopy: 450 m_freem(mcopy); 451 return; 452 } 453