1 /* $OpenBSD: ip6_forward.c,v 1.91 2016/06/15 11:49:34 mpi Exp $ */ 2 /* $KAME: ip6_forward.c,v 1.75 2001/06/29 12:42:13 jinmei 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 "pf.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/mbuf.h> 38 #include <sys/domain.h> 39 #include <sys/protosw.h> 40 #include <sys/socket.h> 41 #include <sys/errno.h> 42 #include <sys/time.h> 43 #include <sys/kernel.h> 44 #include <sys/syslog.h> 45 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/if_enc.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/ip_var.h> 53 #include <netinet6/in6_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 #if NPF > 0 60 #include <net/pfvar.h> 61 #endif 62 63 #ifdef IPSEC 64 #include <netinet/ip_ipsp.h> 65 #include <netinet/ip_ah.h> 66 #include <netinet/ip_esp.h> 67 #include <netinet/udp.h> 68 #include <netinet/tcp.h> 69 #endif 70 71 struct route_in6 ip6_forward_rt; 72 73 /* 74 * Forward a packet. If some error occurs return the sender 75 * an icmp packet. Note we can't always generate a meaningful 76 * icmp message because icmp doesn't have a large enough repertoire 77 * of codes and types. 78 * 79 * If not forwarding, just drop the packet. This could be confusing 80 * if ipforwarding was zero but some routing protocol was advancing 81 * us as a gateway to somewhere. However, we must let the routing 82 * protocol deal with that. 83 * 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; 91 struct rtentry *rt; 92 struct ifnet *ifp = NULL; 93 int error = 0, type = 0, code = 0; 94 struct mbuf *mcopy = NULL; 95 #ifdef IPSEC 96 struct tdb *tdb = NULL; 97 #endif /* IPSEC */ 98 u_int rtableid = 0; 99 char src6[INET6_ADDRSTRLEN], dst6[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 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.ip6s_cantforward++; 111 if (ip6_log_time + ip6_log_interval < time_second) { 112 ip6_log_time = time_second; 113 inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6)); 114 inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6)); 115 log(LOG_DEBUG, 116 "cannot forward " 117 "from %s to %s nxt %d received on inteface %u\n", 118 src6, dst6, 119 ip6->ip6_nxt, 120 m->m_pkthdr.ph_ifidx); 121 } 122 m_freem(m); 123 return; 124 } 125 126 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 127 icmp6_error(m, ICMP6_TIME_EXCEEDED, 128 ICMP6_TIME_EXCEED_TRANSIT, 0); 129 return; 130 } 131 ip6->ip6_hlim -= IPV6_HLIMDEC; 132 133 /* 134 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 135 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 136 * we need to generate an ICMP6 message to the src. 137 * Thanks to M_EXT, in most cases copy will not occur. 138 * 139 * It is important to save it before IPsec processing as IPsec 140 * processing may modify the mbuf. 141 */ 142 mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN), 143 M_NOWAIT); 144 145 #if NPF > 0 146 reroute: 147 #endif 148 149 #ifdef IPSEC 150 if (ipsec_in_use) { 151 tdb = ip6_output_ipsec_lookup(m, &error, NULL); 152 if (error != 0) { 153 /* 154 * -EINVAL is used to indicate that the packet should 155 * be silently dropped, typically because we've asked 156 * key management for an SA. 157 */ 158 if (error == -EINVAL) /* Should silently drop packet */ 159 error = 0; 160 161 m_freem(m); 162 goto freecopy; 163 } 164 } 165 #endif /* IPSEC */ 166 167 #if NPF > 0 168 rtableid = m->m_pkthdr.ph_rtableid; 169 #endif 170 171 dst = &ip6_forward_rt.ro_dst; 172 if (!rtisvalid(ip6_forward_rt.ro_rt) || 173 ISSET(ip6_forward_rt.ro_rt->rt_flags, RTF_MPATH) || 174 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr) || 175 ip6_forward_rt.ro_tableid != rtableid) { 176 if (ip6_forward_rt.ro_rt) { 177 rtfree(ip6_forward_rt.ro_rt); 178 ip6_forward_rt.ro_rt = NULL; 179 } 180 bzero(dst, sizeof(*dst)); 181 dst->sin6_len = sizeof(struct sockaddr_in6); 182 dst->sin6_family = AF_INET6; 183 dst->sin6_addr = ip6->ip6_dst; 184 ip6_forward_rt.ro_tableid = rtableid; 185 ip6_forward_rt.ro_rt = rtalloc_mpath( 186 sin6tosa(&ip6_forward_rt.ro_dst), 187 &ip6->ip6_src.s6_addr32[0], 188 ip6_forward_rt.ro_tableid); 189 190 if (ip6_forward_rt.ro_rt == NULL) { 191 ip6stat.ip6s_noroute++; 192 if (mcopy) { 193 icmp6_error(mcopy, ICMP6_DST_UNREACH, 194 ICMP6_DST_UNREACH_NOROUTE, 0); 195 } 196 m_freem(m); 197 return; 198 } 199 } 200 rt = ip6_forward_rt.ro_rt; 201 202 /* 203 * Scope check: if a packet can't be delivered to its destination 204 * for the reason that the destination is beyond the scope of the 205 * source address, discard the packet and return an icmp6 destination 206 * unreachable error with Code 2 (beyond scope of source address). 207 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1] 208 */ 209 if (in6_addr2scopeid(m->m_pkthdr.ph_ifidx, &ip6->ip6_src) != 210 in6_addr2scopeid(rt->rt_ifidx, &ip6->ip6_src)) { 211 ip6stat.ip6s_cantforward++; 212 ip6stat.ip6s_badscope++; 213 214 if (ip6_log_time + ip6_log_interval < time_second) { 215 ip6_log_time = time_second; 216 inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6)); 217 inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6)); 218 log(LOG_DEBUG, 219 "cannot forward " 220 "src %s, dst %s, nxt %d, rcvif %u, outif %u\n", 221 src6, dst6, 222 ip6->ip6_nxt, 223 m->m_pkthdr.ph_ifidx, rt->rt_ifidx); 224 } 225 if (mcopy) 226 icmp6_error(mcopy, ICMP6_DST_UNREACH, 227 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 228 m_freem(m); 229 goto out; 230 } 231 232 #ifdef IPSEC 233 /* 234 * Check if the packet needs encapsulation. 235 * ipsp_process_packet will never come back to here. 236 * XXX ipsp_process_packet() calls ip6_output(), and there'll be no 237 * PMTU notification. is it okay? 238 */ 239 if (tdb != NULL) { 240 /* Callee frees mbuf */ 241 error = ip6_output_ipsec_send(tdb, m, 0, 1); 242 if (error) 243 goto senderr; 244 goto freecopy; 245 } 246 #endif /* IPSEC */ 247 248 if (rt->rt_flags & RTF_GATEWAY) 249 dst = satosin6(rt->rt_gateway); 250 251 /* 252 * If we are to forward the packet using the same interface 253 * as one we got the packet from, perhaps we should send a redirect 254 * to sender to shortcut a hop. 255 * Only send redirect if source is sending directly to us, 256 * and if packet was not source routed (or has any options). 257 * Also, don't send redirect if forwarding using a route 258 * modified by a redirect. 259 */ 260 ifp = if_get(rt->rt_ifidx); 261 if (rt->rt_ifidx == m->m_pkthdr.ph_ifidx && !srcrt && 262 ip6_sendredirects && 263 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 264 if ((ifp->if_flags & IFF_POINTOPOINT) && 265 nd6_is_addr_neighbor(&ip6_forward_rt.ro_dst, ifp)) { 266 /* 267 * If the incoming interface is equal to the outgoing 268 * one, the link attached to the interface is 269 * point-to-point, and the IPv6 destination is 270 * regarded as on-link on the link, then it will be 271 * highly probable that the destination address does 272 * not exist on the link and that the packet is going 273 * to loop. Thus, we immediately drop the packet and 274 * send an ICMPv6 error message. 275 * For other routing loops, we dare to let the packet 276 * go to the loop, so that a remote diagnosing host 277 * can detect the loop by traceroute. 278 * type/code is based on suggestion by Rich Draves. 279 * not sure if it is the best pick. 280 */ 281 if (mcopy) 282 icmp6_error(mcopy, ICMP6_DST_UNREACH, 283 ICMP6_DST_UNREACH_ADDR, 0); 284 m_freem(m); 285 goto out; 286 } 287 type = ND_REDIRECT; 288 } 289 290 /* 291 * Fake scoped addresses. Note that even link-local source or 292 * destinaion can appear, if the originating node just sends the 293 * packet to us (without address resolution for the destination). 294 * Since both icmp6_error and icmp6_redirect_output fill the embedded 295 * link identifiers, we can do this stuff after making a copy for 296 * returning an error. 297 */ 298 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 299 ip6->ip6_src.s6_addr16[1] = 0; 300 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 301 ip6->ip6_dst.s6_addr16[1] = 0; 302 303 #if NPF > 0 304 if (pf_test(AF_INET6, PF_FWD, ifp, &m) != PF_PASS) { 305 m_freem(m); 306 goto senderr; 307 } 308 if (m == NULL) 309 goto senderr; 310 ip6 = mtod(m, struct ip6_hdr *); 311 if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) == 312 (PF_TAG_REROUTE | PF_TAG_GENERATED)) { 313 /* already rerun the route lookup, go on */ 314 m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE); 315 } else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) { 316 /* tag as generated to skip over pf_test on rerun */ 317 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED; 318 srcrt = 1; 319 if_put(ifp); 320 ifp = NULL; 321 goto reroute; 322 } 323 #endif 324 in6_proto_cksum_out(m, ifp); 325 326 /* Check the size after pf_test to give pf a chance to refragment. */ 327 if (m->m_pkthdr.len > ifp->if_mtu) { 328 if (mcopy) 329 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, 330 ifp->if_mtu); 331 m_freem(m); 332 goto out; 333 } 334 335 error = ifp->if_output(ifp, m, sin6tosa(dst), rt); 336 if (error) { 337 ip6stat.ip6s_cantforward++; 338 } else { 339 ip6stat.ip6s_forward++; 340 if (type) 341 ip6stat.ip6s_redirectsent++; 342 else { 343 if (mcopy) 344 goto freecopy; 345 } 346 } 347 348 #if NPF > 0 || defined(IPSEC) 349 senderr: 350 #endif 351 if (mcopy == NULL) 352 goto out; 353 switch (error) { 354 case 0: 355 if (type == ND_REDIRECT) { 356 icmp6_redirect_output(mcopy, rt); 357 goto out; 358 } 359 goto freecopy; 360 361 case EMSGSIZE: 362 /* xxx MTU is constant in PPP? */ 363 goto freecopy; 364 365 case ENOBUFS: 366 /* Tell source to slow down like source quench in IP? */ 367 goto freecopy; 368 369 case ENETUNREACH: /* shouldn't happen, checked above */ 370 case EHOSTUNREACH: 371 case ENETDOWN: 372 case EHOSTDOWN: 373 default: 374 type = ICMP6_DST_UNREACH; 375 code = ICMP6_DST_UNREACH_ADDR; 376 break; 377 } 378 icmp6_error(mcopy, type, code, 0); 379 goto out; 380 381 freecopy: 382 m_freem(mcopy); 383 out: 384 if_put(ifp); 385 } 386