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 $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include "opt_ip6fw.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/syslog.h>
50 #include <net/route.h>
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>
64 #include <netinet/in_pcb.h>
66 #include <net/ip6fw/ip6_fw.h>
68 #include <net/net_osdep.h>
70 struct route_in6 ip6_forward_rt;
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
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.
86 ip6_forward(struct mbuf *m, int srcrt)
88 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
89 struct sockaddr_in6 *dst;
91 int error, type = 0, code = 0;
92 struct mbuf *mcopy = NULL;
93 struct ifnet *origifp; /* maybe unnecessary */
94 u_int32_t srczone, dstzone;
97 * Do not forward packets to multicast destination (should be handled
99 * Do not forward packets with unspecified source. It was discussed
100 * in July 2000, on the ipngwg mailing list.
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;
111 "from %s to %s nxt %d received on %s\n",
112 ip6_sprintf(&ip6->ip6_src),
113 ip6_sprintf(&ip6->ip6_dst),
115 if_name(m->m_pkthdr.rcvif));
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);
127 ip6->ip6_hlim -= IPV6_HLIMDEC;
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.
135 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
137 dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
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;
147 /* this probably fails but give it a try again */
148 rtalloc_ign((struct route *)&ip6_forward_rt,
152 if (ip6_forward_rt.ro_rt == NULL) {
153 ip6stat.ip6s_noroute++;
154 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
156 icmp6_error(mcopy, ICMP6_DST_UNREACH,
157 ICMP6_DST_UNREACH_NOROUTE, 0);
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;
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;
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);
178 icmp6_error(mcopy, ICMP6_DST_UNREACH,
179 ICMP6_DST_UNREACH_NOROUTE, 0);
185 rt = ip6_forward_rt.ro_rt;
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]
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);
201 if (ip6_log_time + ip6_log_interval < time_uptime) {
202 ip6_log_time = time_uptime;
205 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
206 ip6_sprintf(&ip6->ip6_src),
207 ip6_sprintf(&ip6->ip6_dst),
209 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
212 icmp6_error(mcopy, ICMP6_DST_UNREACH,
213 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
218 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
219 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
223 mtu = IN6_LINKMTU(rt->rt_ifp);
224 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
230 if (rt->rt_flags & RTF_GATEWAY)
231 dst = (struct sockaddr_in6 *)rt->rt_gateway;
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.
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) {
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.
252 * type/code is based on suggestion by Rich Draves.
253 * not sure if it is the best pick.
255 icmp6_error(mcopy, ICMP6_DST_UNREACH,
256 ICMP6_DST_UNREACH_ADDR, 0);
264 * Check with the firewall...
266 if (ip6_fw_enable && ip6_fw_chk_ptr) {
268 /* If ipfw says divert, we have to just drop packet */
269 if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
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.
285 if (rt->rt_ifp->if_flags & IFF_LOOPBACK) {
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
298 if ((rt->rt_flags & (RTF_BLACKHOLE | RTF_REJECT)) == 0)
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));
309 /* we can just use rcvif in forwarding. */
310 origifp = m->m_pkthdr.rcvif;
313 origifp = rt->rt_ifp;
315 * clear embedded scope identifiers if necessary.
316 * in6_clearscope will touch the addresses only when necessary.
318 in6_clearscope(&ip6->ip6_src);
319 in6_clearscope(&ip6->ip6_dst);
322 * Run through list of hooks for output packets.
324 if (pfil_has_hooks(&inet6_pfil_hook)) {
325 error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
331 ip6 = mtod(m, struct ip6_hdr *);
334 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
336 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
337 ip6stat.ip6s_cantforward++;
339 ip6stat.ip6s_forward++;
340 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
342 ip6stat.ip6s_redirectsent++;
353 if (type == ND_REDIRECT) {
354 icmp6_redirect_output(mcopy, rt);
360 /* xxx MTU is constant in PPP? */
364 /* Tell source to slow down like source quench in IP? */
367 case ENETUNREACH: /* shouldn't happen, checked above */
372 type = ICMP6_DST_UNREACH;
373 code = ICMP6_DST_UNREACH_ADDR;
376 icmp6_error(mcopy, type, code, 0);