inet6: require RTF_ANNOUNCE to proxy NS
[dragonfly.git] / sys / netinet6 / ip6_forward.c
blob0093cd2c55b43ee794e91e497d52eb1b604e8cc2
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 $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
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.
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.
33 #include "opt_ip6fw.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
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>
49 #include <net/if.h>
50 #include <net/route.h>
51 #include <net/pfil.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
76 * of codes and types.
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.
85 void
86 ip6_forward(struct mbuf *m, int srcrt)
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;
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.
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));
117 m_freem(m);
118 return;
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;
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_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
136 M_NOWAIT);
138 dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
139 if (!srcrt) {
140 /* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */
141 if (ip6_forward_rt.ro_rt == NULL ||
142 !(ip6_forward_rt.ro_rt->rt_flags & RTF_UP)) {
143 if (ip6_forward_rt.ro_rt) {
144 RTFREE(ip6_forward_rt.ro_rt);
145 ip6_forward_rt.ro_rt = 0;
148 /* this probably fails but give it a try again */
149 rtalloc_ign((struct route *)&ip6_forward_rt,
150 RTF_PRCLONING);
153 if (ip6_forward_rt.ro_rt == NULL) {
154 ip6stat.ip6s_noroute++;
155 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
156 if (mcopy) {
157 icmp6_error(mcopy, ICMP6_DST_UNREACH,
158 ICMP6_DST_UNREACH_NOROUTE, 0);
160 m_freem(m);
161 return;
163 } else if ((rt = ip6_forward_rt.ro_rt) == NULL ||
164 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
165 if (ip6_forward_rt.ro_rt) {
166 RTFREE(ip6_forward_rt.ro_rt);
167 ip6_forward_rt.ro_rt = 0;
169 bzero(dst, sizeof(*dst));
170 dst->sin6_len = sizeof(struct sockaddr_in6);
171 dst->sin6_family = AF_INET6;
172 dst->sin6_addr = ip6->ip6_dst;
174 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
175 if (ip6_forward_rt.ro_rt == NULL) {
176 ip6stat.ip6s_noroute++;
177 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
178 if (mcopy) {
179 icmp6_error(mcopy, ICMP6_DST_UNREACH,
180 ICMP6_DST_UNREACH_NOROUTE, 0);
182 m_freem(m);
183 return;
186 rt = ip6_forward_rt.ro_rt;
189 * Scope check: if a packet can't be delivered to its destination
190 * for the reason that the destination is beyond the scope of the
191 * source address, discard the packet and return an icmp6 destination
192 * unreachable error with Code 2 (beyond scope of source address).
193 * [draft-ietf-ipngwg-icmp-v3-02.txt, Section 3.1]
195 if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) ||
196 in6_addr2zoneid(rt->rt_ifp, &ip6->ip6_src, &dstzone) ||
197 srczone != dstzone) {
198 ip6stat.ip6s_cantforward++;
199 ip6stat.ip6s_badscope++;
200 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
202 if (ip6_log_time + ip6_log_interval < time_uptime) {
203 ip6_log_time = time_uptime;
204 log(LOG_DEBUG,
205 "cannot forward "
206 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
207 ip6_sprintf(&ip6->ip6_src),
208 ip6_sprintf(&ip6->ip6_dst),
209 ip6->ip6_nxt,
210 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
212 if (mcopy)
213 icmp6_error(mcopy, ICMP6_DST_UNREACH,
214 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
215 m_freem(m);
216 return;
219 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
220 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
221 if (mcopy) {
222 u_long mtu;
224 mtu = IN6_LINKMTU(rt->rt_ifp);
225 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
227 m_freem(m);
228 return;
231 if (rt->rt_flags & RTF_GATEWAY)
232 dst = (struct sockaddr_in6 *)rt->rt_gateway;
235 * If we are to forward the packet using the same interface
236 * as one we got the packet from, perhaps we should send a redirect
237 * to sender to shortcut a hop.
238 * Only send redirect if source is sending directly to us,
239 * and if packet was not source routed (or has any options).
240 * Also, don't send redirect if forwarding using a route
241 * modified by a redirect.
243 if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
244 (rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) == 0) {
245 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) {
247 * If the incoming interface is equal to the outgoing
248 * one, and the link attached to the interface is
249 * point-to-point, then it will be highly probable
250 * that a routing loop occurs. Thus, we immediately
251 * drop the packet and send an ICMPv6 error message.
253 * type/code is based on suggestion by Rich Draves.
254 * not sure if it is the best pick.
256 icmp6_error(mcopy, ICMP6_DST_UNREACH,
257 ICMP6_DST_UNREACH_ADDR, 0);
258 m_freem(m);
259 return;
261 type = ND_REDIRECT;
265 * Check with the firewall...
267 if (ip6_fw_enable && ip6_fw_chk_ptr) {
268 u_short port = 0;
269 /* If ipfw says divert, we have to just drop packet */
270 if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
271 m_freem(m);
272 goto freecopy;
274 if (!m)
275 goto freecopy;
279 * Fake scoped addresses. Note that even link-local source or
280 * destinaion can appear, if the originating node just sends the
281 * packet to us (without address resolution for the destination).
282 * Since both icmp6_error and icmp6_redirect_output fill the embedded
283 * link identifiers, we can do this stuff after making a copy for
284 * returning an error.
286 if (rt->rt_ifp->if_flags & IFF_LOOPBACK) {
288 * See corresponding comments in ip6_output.
289 * XXX: but is it possible that ip6_forward() sends a packet
290 * to a loopback interface? I don't think so, and thus
291 * I bark here. (jinmei@kame.net)
292 * XXX: it is common to route invalid packets to loopback.
293 * also, the codepath will be visited on use of ::1 in
294 * rthdr. (itojun)
296 #if 1
297 if (0)
298 #else
299 if ((rt->rt_flags & (RTF_BLACKHOLE | RTF_REJECT)) == 0)
300 #endif
302 kprintf("ip6_forward: outgoing interface is loopback. "
303 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
304 ip6_sprintf(&ip6->ip6_src),
305 ip6_sprintf(&ip6->ip6_dst),
306 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
307 if_name(rt->rt_ifp));
310 /* we can just use rcvif in forwarding. */
311 origifp = m->m_pkthdr.rcvif;
313 else
314 origifp = rt->rt_ifp;
316 * clear embedded scope identifiers if necessary.
317 * in6_clearscope will touch the addresses only when necessary.
319 in6_clearscope(&ip6->ip6_src);
320 in6_clearscope(&ip6->ip6_dst);
323 * Run through list of hooks for output packets.
325 if (pfil_has_hooks(&inet6_pfil_hook)) {
326 error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
327 PFIL_OUT);
328 if (error != 0)
329 goto senderr;
330 if (m == NULL)
331 goto freecopy;
332 ip6 = mtod(m, struct ip6_hdr *);
335 error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
336 if (error) {
337 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
338 ip6stat.ip6s_cantforward++;
339 } else {
340 ip6stat.ip6s_forward++;
341 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
342 if (type)
343 ip6stat.ip6s_redirectsent++;
344 else {
345 if (mcopy)
346 goto freecopy;
349 senderr:
350 if (mcopy == NULL)
351 return;
352 switch (error) {
353 case 0:
354 if (type == ND_REDIRECT) {
355 icmp6_redirect_output(mcopy, rt);
356 return;
358 goto freecopy;
360 case EMSGSIZE:
361 /* xxx MTU is constant in PPP? */
362 goto freecopy;
364 case ENOBUFS:
365 /* Tell source to slow down like source quench in IP? */
366 goto freecopy;
368 case ENETUNREACH: /* shouldn't happen, checked above */
369 case EHOSTUNREACH:
370 case ENETDOWN:
371 case EHOSTDOWN:
372 default:
373 type = ICMP6_DST_UNREACH;
374 code = ICMP6_DST_UNREACH_ADDR;
375 break;
377 icmp6_error(mcopy, type, code, 0);
378 return;
380 freecopy:
381 m_freem(mcopy);
382 return;