1 /* $FreeBSD: src/sys/netinet6/ip6_forward.c,v 1.4.2.7 2003/01/24 05:11:35 sam Exp $ */ 2 /* $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun 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 "opt_ip6fw.h" 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/errno.h> 45 #include <sys/time.h> 46 #include <sys/kernel.h> 47 #include <sys/syslog.h> 48 49 #include <net/if.h> 50 #include <net/route.h> 51 #include <net/pfil.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_var.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 #include <netinet/ip_var.h> 58 #include <netinet6/in6_var.h> 59 #include <netinet/ip6.h> 60 #include <netinet6/ip6_var.h> 61 #include <netinet/icmp6.h> 62 #include <netinet6/nd6.h> 63 64 #include <netinet/in_pcb.h> 65 66 #include <net/ip6fw/ip6_fw.h> 67 68 #include <net/net_osdep.h> 69 70 struct route_in6 ip6_forward_rt; 71 72 /* 73 * Forward a packet. If some error occurs return the sender 74 * an icmp packet. Note we can't always generate a meaningful 75 * icmp message because icmp doesn't have a large enough repertoire 76 * of codes and types. 77 * 78 * If not forwarding, just drop the packet. This could be confusing 79 * if ipforwarding was zero but some routing protocol was advancing 80 * us as a gateway to somewhere. However, we must let the routing 81 * protocol deal with that. 82 * 83 */ 84 85 void 86 ip6_forward(struct mbuf *m, int srcrt) 87 { 88 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 89 struct sockaddr_in6 *dst; 90 struct rtentry *rt; 91 int error, type = 0, code = 0; 92 struct mbuf *mcopy = NULL; 93 struct ifnet *origifp; /* maybe unnecessary */ 94 u_int32_t srczone, dstzone; 95 96 /* 97 * Do not forward packets to multicast destination (should be handled 98 * by ip6_mforward(). 99 * Do not forward packets with unspecified source. It was discussed 100 * in July 2000, on the ipngwg mailing list. 101 */ 102 if ((m->m_flags & (M_BCAST | M_MCAST)) != 0 || 103 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 104 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 105 ip6stat.ip6s_cantforward++; 106 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 107 if (ip6_log_time + ip6_log_interval < time_uptime) { 108 ip6_log_time = time_uptime; 109 log(LOG_DEBUG, 110 "cannot forward " 111 "from %s to %s nxt %d received on %s\n", 112 ip6_sprintf(&ip6->ip6_src), 113 ip6_sprintf(&ip6->ip6_dst), 114 ip6->ip6_nxt, 115 if_name(m->m_pkthdr.rcvif)); 116 } 117 m_freem(m); 118 return; 119 } 120 121 if (ip6->ip6_hlim <= IPV6_HLIMDEC) { 122 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */ 123 icmp6_error(m, ICMP6_TIME_EXCEEDED, 124 ICMP6_TIME_EXCEED_TRANSIT, 0); 125 return; 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 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN)); 136 137 dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 138 if (!srcrt) { 139 /* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */ 140 if (ip6_forward_rt.ro_rt == NULL || 141 !(ip6_forward_rt.ro_rt->rt_flags & RTF_UP)) { 142 if (ip6_forward_rt.ro_rt) { 143 RTFREE(ip6_forward_rt.ro_rt); 144 ip6_forward_rt.ro_rt = 0; 145 } 146 147 /* this probably fails but give it a try again */ 148 rtalloc_ign((struct route *)&ip6_forward_rt, 149 RTF_PRCLONING); 150 } 151 152 if (ip6_forward_rt.ro_rt == NULL) { 153 ip6stat.ip6s_noroute++; 154 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 155 if (mcopy) { 156 icmp6_error(mcopy, ICMP6_DST_UNREACH, 157 ICMP6_DST_UNREACH_NOROUTE, 0); 158 } 159 m_freem(m); 160 return; 161 } 162 } else if ((rt = ip6_forward_rt.ro_rt) == NULL || 163 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) { 164 if (ip6_forward_rt.ro_rt) { 165 RTFREE(ip6_forward_rt.ro_rt); 166 ip6_forward_rt.ro_rt = 0; 167 } 168 bzero(dst, sizeof(*dst)); 169 dst->sin6_len = sizeof(struct sockaddr_in6); 170 dst->sin6_family = AF_INET6; 171 dst->sin6_addr = ip6->ip6_dst; 172 173 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 174 if (ip6_forward_rt.ro_rt == NULL) { 175 ip6stat.ip6s_noroute++; 176 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute); 177 if (mcopy) { 178 icmp6_error(mcopy, ICMP6_DST_UNREACH, 179 ICMP6_DST_UNREACH_NOROUTE, 0); 180 } 181 m_freem(m); 182 return; 183 } 184 } 185 rt = ip6_forward_rt.ro_rt; 186 187 /* 188 * Scope check: if a packet can't be delivered to its destination 189 * for the reason that the destination is beyond the scope of the 190 * source address, discard the packet and return an icmp6 destination 191 * unreachable error with Code 2 (beyond scope of source address). 192 * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1] 193 */ 194 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) || 195 in6_addr2zoneid(rt->rt_ifp, &ip6->ip6_src, &dstzone) || 196 srczone != dstzone) { 197 ip6stat.ip6s_cantforward++; 198 ip6stat.ip6s_badscope++; 199 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard); 200 201 if (ip6_log_time + ip6_log_interval < time_uptime) { 202 ip6_log_time = time_uptime; 203 log(LOG_DEBUG, 204 "cannot forward " 205 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 206 ip6_sprintf(&ip6->ip6_src), 207 ip6_sprintf(&ip6->ip6_dst), 208 ip6->ip6_nxt, 209 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp)); 210 } 211 if (mcopy) 212 icmp6_error(mcopy, ICMP6_DST_UNREACH, 213 ICMP6_DST_UNREACH_BEYONDSCOPE, 0); 214 m_freem(m); 215 return; 216 } 217 218 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) { 219 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig); 220 if (mcopy) { 221 u_long mtu; 222 223 mtu = IN6_LINKMTU(rt->rt_ifp); 224 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu); 225 } 226 m_freem(m); 227 return; 228 } 229 230 if (rt->rt_flags & RTF_GATEWAY) 231 dst = (struct sockaddr_in6 *)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 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && 243 (rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) == 0) { 244 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) { 245 /* 246 * If the incoming interface is equal to the outgoing 247 * one, and the link attached to the interface is 248 * point-to-point, then it will be highly probable 249 * that a routing loop occurs. Thus, we immediately 250 * drop the packet and send an ICMPv6 error message. 251 * 252 * type/code is based on suggestion by Rich Draves. 253 * not sure if it is the best pick. 254 */ 255 icmp6_error(mcopy, ICMP6_DST_UNREACH, 256 ICMP6_DST_UNREACH_ADDR, 0); 257 m_freem(m); 258 return; 259 } 260 type = ND_REDIRECT; 261 } 262 263 /* 264 * Check with the firewall... 265 */ 266 if (ip6_fw_enable && ip6_fw_chk_ptr) { 267 u_short port = 0; 268 /* If ipfw says divert, we have to just drop packet */ 269 if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) { 270 m_freem(m); 271 goto freecopy; 272 } 273 if (!m) 274 goto freecopy; 275 } 276 277 /* 278 * Fake scoped addresses. Note that even link-local source or 279 * destinaion can appear, if the originating node just sends the 280 * packet to us (without address resolution for the destination). 281 * Since both icmp6_error and icmp6_redirect_output fill the embedded 282 * link identifiers, we can do this stuff after making a copy for 283 * returning an error. 284 */ 285 if (rt->rt_ifp->if_flags & IFF_LOOPBACK) { 286 /* 287 * See corresponding comments in ip6_output. 288 * XXX: but is it possible that ip6_forward() sends a packet 289 * to a loopback interface? I don't think so, and thus 290 * I bark here. (jinmei@kame.net) 291 * XXX: it is common to route invalid packets to loopback. 292 * also, the codepath will be visited on use of ::1 in 293 * rthdr. (itojun) 294 */ 295 #if 1 296 if (0) 297 #else 298 if ((rt->rt_flags & (RTF_BLACKHOLE | RTF_REJECT)) == 0) 299 #endif 300 { 301 kprintf("ip6_forward: outgoing interface is loopback. " 302 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n", 303 ip6_sprintf(&ip6->ip6_src), 304 ip6_sprintf(&ip6->ip6_dst), 305 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif), 306 if_name(rt->rt_ifp)); 307 } 308 309 /* we can just use rcvif in forwarding. */ 310 origifp = m->m_pkthdr.rcvif; 311 } 312 else 313 origifp = rt->rt_ifp; 314 /* 315 * clear embedded scope identifiers if necessary. 316 * in6_clearscope will touch the addresses only when necessary. 317 */ 318 in6_clearscope(&ip6->ip6_src); 319 in6_clearscope(&ip6->ip6_dst); 320 321 /* 322 * Run through list of hooks for output packets. 323 */ 324 if (pfil_has_hooks(&inet6_pfil_hook)) { 325 error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, 326 PFIL_OUT); 327 if (error != 0) 328 goto senderr; 329 if (m == NULL) 330 goto freecopy; 331 ip6 = mtod(m, struct ip6_hdr *); 332 } 333 334 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt); 335 if (error) { 336 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard); 337 ip6stat.ip6s_cantforward++; 338 } else { 339 ip6stat.ip6s_forward++; 340 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward); 341 if (type) 342 ip6stat.ip6s_redirectsent++; 343 else { 344 if (mcopy) 345 goto freecopy; 346 } 347 } 348 senderr: 349 if (mcopy == NULL) 350 return; 351 switch (error) { 352 case 0: 353 if (type == ND_REDIRECT) { 354 icmp6_redirect_output(mcopy, rt); 355 return; 356 } 357 goto freecopy; 358 359 case EMSGSIZE: 360 /* xxx MTU is constant in PPP? */ 361 goto freecopy; 362 363 case ENOBUFS: 364 /* Tell source to slow down like source quench in IP? */ 365 goto freecopy; 366 367 case ENETUNREACH: /* shouldn't happen, checked above */ 368 case EHOSTUNREACH: 369 case ENETDOWN: 370 case EHOSTDOWN: 371 default: 372 type = ICMP6_DST_UNREACH; 373 code = ICMP6_DST_UNREACH_ADDR; 374 break; 375 } 376 icmp6_error(mcopy, type, code, 0); 377 return; 378 379 freecopy: 380 m_freem(mcopy); 381 return; 382 } 383