1 /* $OpenBSD: ip6_forward.c,v 1.92 2016/08/24 09:41:12 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 /* 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(struct mbuf *m, struct rtentry *rt, int srcrt) 86 { 87 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 88 struct sockaddr_in6 *dst, sin6; 89 struct ifnet *ifp = NULL; 90 int error = 0, type = 0, code = 0; 91 struct mbuf *mcopy = NULL; 92 #ifdef IPSEC 93 struct tdb *tdb = NULL; 94 #endif /* IPSEC */ 95 char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN]; 96 97 /* 98 * Do not forward packets to multicast destination (should be handled 99 * by ip6_mforward(). 100 * Do not forward packets with unspecified source. It was discussed 101 * in July 2000, on ipngwg mailing list. 102 */ 103 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 || 104 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 105 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 106 ip6stat.ip6s_cantforward++; 107 if (ip6_log_time + ip6_log_interval < time_second) { 108 ip6_log_time = time_second; 109 inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6)); 110 inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6)); 111 log(LOG_DEBUG, 112 "cannot forward " 113 "from %s to %s nxt %d received on inteface %u\n", 114 src6, dst6, 115 ip6->ip6_nxt, 116 m->m_pkthdr.ph_ifidx); 117 } 118 m_freem(m); 119 goto out; 120 } 121 122 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 123 icmp6_error(m, ICMP6_TIME_EXCEEDED, 124 ICMP6_TIME_EXCEED_TRANSIT, 0); 125 goto out; 126 } 127 ip6->ip6_hlim -= IPV6_HLIMDEC; 128 129 /* 130 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU - 131 * size of IPv6 + ICMPv6 headers) bytes of the packet in case 132 * we need to generate an ICMP6 message to the src. 133 * Thanks to M_EXT, in most cases copy will not occur. 134 * 135 * It is important to save it before IPsec processing as IPsec 136 * processing may modify the mbuf. 137 */ 138 mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN), 139 M_NOWAIT); 140 141 #if NPF > 0 142 reroute: 143 #endif 144 145 #ifdef IPSEC 146 if (ipsec_in_use) { 147 tdb = ip6_output_ipsec_lookup(m, &error, NULL); 148 if (error != 0) { 149 /* 150 * -EINVAL is used to indicate that the packet should 151 * be silently dropped, typically because we've asked 152 * key management for an SA. 153 */ 154 if (error == -EINVAL) /* Should silently drop packet */ 155 error = 0; 156 157 m_freem(m); 158 goto freecopy; 159 } 160 } 161 #endif /* IPSEC */ 162 163 dst = &sin6; 164 memset(dst, 0, sizeof(*dst)); 165 dst->sin6_len = sizeof(struct sockaddr_in6); 166 dst->sin6_family = AF_INET6; 167 dst->sin6_addr = ip6->ip6_dst; 168 169 if (!rtisvalid(rt)) { 170 rtfree(rt); 171 rt = rtalloc_mpath(sin6tosa(dst), &ip6->ip6_src.s6_addr32[0], 172 m->m_pkthdr.ph_rtableid); 173 if (rt == NULL) { 174 ip6stat.ip6s_noroute++; 175 if (mcopy) { 176 icmp6_error(mcopy, ICMP6_DST_UNREACH, 177 ICMP6_DST_UNREACH_NOROUTE, 0); 178 } 179 m_freem(m); 180 goto out; 181 } 182 } 183 184 /* 185 * Scope check: if a packet can't be delivered to its destination 186 * for the reason that the destination is beyond the scope of the 187 * source address, discard the packet and return an icmp6 destination 188 * unreachable error with Code 2 (beyond scope of source address). 189 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1] 190 */ 191 if (in6_addr2scopeid(m->m_pkthdr.ph_ifidx, &ip6->ip6_src) != 192 in6_addr2scopeid(rt->rt_ifidx, &ip6->ip6_src)) { 193 ip6stat.ip6s_cantforward++; 194 ip6stat.ip6s_badscope++; 195 196 if (ip6_log_time + ip6_log_interval < time_second) { 197 ip6_log_time = time_second; 198 inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6)); 199 inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6)); 200 log(LOG_DEBUG, 201 "cannot forward " 202 "src %s, dst %s, nxt %d, rcvif %u, outif %u\n", 203 src6, dst6, 204 ip6->ip6_nxt, 205 m->m_pkthdr.ph_ifidx, rt->rt_ifidx); 206 } 207 if (mcopy) 208 icmp6_error(mcopy, ICMP6_DST_UNREACH, 209 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 210 m_freem(m); 211 goto out; 212 } 213 214 #ifdef IPSEC 215 /* 216 * Check if the packet needs encapsulation. 217 * ipsp_process_packet will never come back to here. 218 * XXX ipsp_process_packet() calls ip6_output(), and there'll be no 219 * PMTU notification. is it okay? 220 */ 221 if (tdb != NULL) { 222 /* Callee frees mbuf */ 223 error = ip6_output_ipsec_send(tdb, m, 0, 1); 224 if (error) 225 goto senderr; 226 goto freecopy; 227 } 228 #endif /* IPSEC */ 229 230 if (rt->rt_flags & RTF_GATEWAY) 231 dst = satosin6(rt->rt_gateway); 232 233 /* 234 * If we are to forward the packet using the same interface 235 * as one we got the packet from, perhaps we should send a redirect 236 * to sender to shortcut a hop. 237 * Only send redirect if source is sending directly to us, 238 * and if packet was not source routed (or has any options). 239 * Also, don't send redirect if forwarding using a route 240 * modified by a redirect. 241 */ 242 ifp = if_get(rt->rt_ifidx); 243 if (rt->rt_ifidx == m->m_pkthdr.ph_ifidx && !srcrt && 244 ip6_sendredirects && 245 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) { 246 if ((ifp->if_flags & IFF_POINTOPOINT) && 247 nd6_is_addr_neighbor(&sin6, ifp)) { 248 /* 249 * If the incoming interface is equal to the outgoing 250 * one, the link attached to the interface is 251 * point-to-point, and the IPv6 destination is 252 * regarded as on-link on the link, then it will be 253 * highly probable that the destination address does 254 * not exist on the link and that the packet is going 255 * to loop. Thus, we immediately drop the packet and 256 * send an ICMPv6 error message. 257 * For other routing loops, we dare to let the packet 258 * go to the loop, so that a remote diagnosing host 259 * can detect the loop by traceroute. 260 * type/code is based on suggestion by Rich Draves. 261 * not sure if it is the best pick. 262 */ 263 if (mcopy) 264 icmp6_error(mcopy, ICMP6_DST_UNREACH, 265 ICMP6_DST_UNREACH_ADDR, 0); 266 m_freem(m); 267 goto out; 268 } 269 type = ND_REDIRECT; 270 } 271 272 /* 273 * Fake scoped addresses. Note that even link-local source or 274 * destinaion can appear, if the originating node just sends the 275 * packet to us (without address resolution for the destination). 276 * Since both icmp6_error and icmp6_redirect_output fill the embedded 277 * link identifiers, we can do this stuff after making a copy for 278 * returning an error. 279 */ 280 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) 281 ip6->ip6_src.s6_addr16[1] = 0; 282 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) 283 ip6->ip6_dst.s6_addr16[1] = 0; 284 285 #if NPF > 0 286 if (pf_test(AF_INET6, PF_FWD, ifp, &m) != PF_PASS) { 287 m_freem(m); 288 goto senderr; 289 } 290 if (m == NULL) 291 goto senderr; 292 ip6 = mtod(m, struct ip6_hdr *); 293 if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) == 294 (PF_TAG_REROUTE | PF_TAG_GENERATED)) { 295 /* already rerun the route lookup, go on */ 296 m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE); 297 } else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) { 298 /* tag as generated to skip over pf_test on rerun */ 299 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED; 300 srcrt = 1; 301 if_put(ifp); 302 ifp = NULL; 303 goto reroute; 304 } 305 #endif 306 in6_proto_cksum_out(m, ifp); 307 308 /* Check the size after pf_test to give pf a chance to refragment. */ 309 if (m->m_pkthdr.len > ifp->if_mtu) { 310 if (mcopy) 311 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, 312 ifp->if_mtu); 313 m_freem(m); 314 goto out; 315 } 316 317 error = ifp->if_output(ifp, m, sin6tosa(dst), rt); 318 if (error) { 319 ip6stat.ip6s_cantforward++; 320 } else { 321 ip6stat.ip6s_forward++; 322 if (type) 323 ip6stat.ip6s_redirectsent++; 324 else { 325 if (mcopy) 326 goto freecopy; 327 } 328 } 329 330 #if NPF > 0 || defined(IPSEC) 331 senderr: 332 #endif 333 if (mcopy == NULL) 334 goto out; 335 switch (error) { 336 case 0: 337 if (type == ND_REDIRECT) { 338 icmp6_redirect_output(mcopy, rt); 339 goto out; 340 } 341 goto freecopy; 342 343 case EMSGSIZE: 344 /* xxx MTU is constant in PPP? */ 345 goto freecopy; 346 347 case ENOBUFS: 348 /* Tell source to slow down like source quench in IP? */ 349 goto freecopy; 350 351 case ENETUNREACH: /* shouldn't happen, checked above */ 352 case EHOSTUNREACH: 353 case ENETDOWN: 354 case EHOSTDOWN: 355 default: 356 type = ICMP6_DST_UNREACH; 357 code = ICMP6_DST_UNREACH_ADDR; 358 break; 359 } 360 icmp6_error(mcopy, type, code, 0); 361 goto out; 362 363 freecopy: 364 m_freem(mcopy); 365 out: 366 rtfree(rt); 367 if_put(ifp); 368 } 369