xref: /f-stack/freebsd/netinet6/ip6_forward.c (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang  * 3. Neither the name of the project nor the names of its contributors
16a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
17a9643ea8Slogwang  *    without specific prior written permission.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a9643ea8Slogwang  * SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  *	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang #include <sys/cdefs.h>
35a9643ea8Slogwang __FBSDID("$FreeBSD$");
36a9643ea8Slogwang 
37a9643ea8Slogwang #include "opt_inet.h"
38a9643ea8Slogwang #include "opt_inet6.h"
39a9643ea8Slogwang #include "opt_ipsec.h"
40a9643ea8Slogwang #include "opt_ipstealth.h"
41*22ce4affSfengbojiang #include "opt_sctp.h"
42a9643ea8Slogwang 
43a9643ea8Slogwang #include <sys/param.h>
44a9643ea8Slogwang #include <sys/systm.h>
45a9643ea8Slogwang #include <sys/malloc.h>
46a9643ea8Slogwang #include <sys/mbuf.h>
47a9643ea8Slogwang #include <sys/domain.h>
48a9643ea8Slogwang #include <sys/protosw.h>
49a9643ea8Slogwang #include <sys/socket.h>
50a9643ea8Slogwang #include <sys/errno.h>
51a9643ea8Slogwang #include <sys/time.h>
52a9643ea8Slogwang #include <sys/kernel.h>
53a9643ea8Slogwang #include <sys/syslog.h>
54a9643ea8Slogwang 
55a9643ea8Slogwang #include <net/if.h>
56a9643ea8Slogwang #include <net/if_var.h>
57a9643ea8Slogwang #include <net/netisr.h>
58a9643ea8Slogwang #include <net/route.h>
59*22ce4affSfengbojiang #include <net/route/nhop.h>
60a9643ea8Slogwang #include <net/pfil.h>
61a9643ea8Slogwang 
62a9643ea8Slogwang #include <netinet/in.h>
63a9643ea8Slogwang #include <netinet/in_var.h>
64a9643ea8Slogwang #include <netinet/in_systm.h>
65a9643ea8Slogwang #include <netinet/ip.h>
66a9643ea8Slogwang #include <netinet/ip_var.h>
67a9643ea8Slogwang #include <netinet6/in6_var.h>
68a9643ea8Slogwang #include <netinet/ip6.h>
69*22ce4affSfengbojiang #include <netinet6/in6_fib.h>
70a9643ea8Slogwang #include <netinet6/ip6_var.h>
71a9643ea8Slogwang #include <netinet6/scope6_var.h>
72a9643ea8Slogwang #include <netinet/icmp6.h>
73a9643ea8Slogwang #include <netinet6/nd6.h>
74a9643ea8Slogwang 
75a9643ea8Slogwang #include <netinet/in_pcb.h>
76a9643ea8Slogwang 
77*22ce4affSfengbojiang #include <netipsec/ipsec_support.h>
78a9643ea8Slogwang 
79a9643ea8Slogwang /*
80a9643ea8Slogwang  * Forward a packet.  If some error occurs return the sender
81a9643ea8Slogwang  * an icmp packet.  Note we can't always generate a meaningful
82a9643ea8Slogwang  * icmp message because icmp doesn't have a large enough repertoire
83a9643ea8Slogwang  * of codes and types.
84a9643ea8Slogwang  *
85a9643ea8Slogwang  * If not forwarding, just drop the packet.  This could be confusing
86a9643ea8Slogwang  * if ipforwarding was zero but some routing protocol was advancing
87a9643ea8Slogwang  * us as a gateway to somewhere.  However, we must let the routing
88a9643ea8Slogwang  * protocol deal with that.
89a9643ea8Slogwang  *
90a9643ea8Slogwang  */
91a9643ea8Slogwang void
ip6_forward(struct mbuf * m,int srcrt)92a9643ea8Slogwang ip6_forward(struct mbuf *m, int srcrt)
93a9643ea8Slogwang {
94a9643ea8Slogwang 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
95*22ce4affSfengbojiang 	struct sockaddr_in6 dst;
96*22ce4affSfengbojiang 	struct nhop_object *nh = NULL;
97a9643ea8Slogwang 	int error, type = 0, code = 0;
98a9643ea8Slogwang 	struct mbuf *mcopy = NULL;
99a9643ea8Slogwang 	struct ifnet *origifp;	/* maybe unnecessary */
100a9643ea8Slogwang 	u_int32_t inzone, outzone;
101*22ce4affSfengbojiang 	struct in6_addr odst;
102a9643ea8Slogwang 	struct m_tag *fwd_tag;
103a9643ea8Slogwang 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
104a9643ea8Slogwang 
105a9643ea8Slogwang 	/*
106a9643ea8Slogwang 	 * Do not forward packets to multicast destination (should be handled
107a9643ea8Slogwang 	 * by ip6_mforward().
108a9643ea8Slogwang 	 * Do not forward packets with unspecified source.  It was discussed
109a9643ea8Slogwang 	 * in July 2000, on the ipngwg mailing list.
110a9643ea8Slogwang 	 */
111a9643ea8Slogwang 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
112a9643ea8Slogwang 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
113a9643ea8Slogwang 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
114a9643ea8Slogwang 		IP6STAT_INC(ip6s_cantforward);
115a9643ea8Slogwang 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
116a9643ea8Slogwang 		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
117a9643ea8Slogwang 			V_ip6_log_time = time_uptime;
118a9643ea8Slogwang 			log(LOG_DEBUG,
119a9643ea8Slogwang 			    "cannot forward "
120a9643ea8Slogwang 			    "from %s to %s nxt %d received on %s\n",
121a9643ea8Slogwang 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
122a9643ea8Slogwang 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
123a9643ea8Slogwang 			    ip6->ip6_nxt,
124a9643ea8Slogwang 			    if_name(m->m_pkthdr.rcvif));
125a9643ea8Slogwang 		}
126a9643ea8Slogwang 		m_freem(m);
127a9643ea8Slogwang 		return;
128a9643ea8Slogwang 	}
129a9643ea8Slogwang 
130*22ce4affSfengbojiang 	if (
131a9643ea8Slogwang #ifdef IPSTEALTH
132*22ce4affSfengbojiang 	    V_ip6stealth == 0 &&
133a9643ea8Slogwang #endif
134*22ce4affSfengbojiang 	    ip6->ip6_hlim <= IPV6_HLIMDEC) {
135a9643ea8Slogwang 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
136a9643ea8Slogwang 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
137a9643ea8Slogwang 		    ICMP6_TIME_EXCEED_TRANSIT, 0);
138a9643ea8Slogwang 		return;
139a9643ea8Slogwang 	}
140a9643ea8Slogwang 
141a9643ea8Slogwang 	/*
142a9643ea8Slogwang 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
143a9643ea8Slogwang 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
144a9643ea8Slogwang 	 * we need to generate an ICMP6 message to the src.
145a9643ea8Slogwang 	 * Thanks to M_EXT, in most cases copy will not occur.
146a9643ea8Slogwang 	 *
147a9643ea8Slogwang 	 * It is important to save it before IPsec processing as IPsec
148a9643ea8Slogwang 	 * processing may modify the mbuf.
149a9643ea8Slogwang 	 */
150*22ce4affSfengbojiang 	mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
151*22ce4affSfengbojiang 	    M_NOWAIT);
152*22ce4affSfengbojiang #ifdef IPSTEALTH
153*22ce4affSfengbojiang 	if (V_ip6stealth == 0)
154a9643ea8Slogwang #endif
155*22ce4affSfengbojiang 		ip6->ip6_hlim -= IPV6_HLIMDEC;
156*22ce4affSfengbojiang 
157*22ce4affSfengbojiang #if defined(IPSEC) || defined(IPSEC_SUPPORT)
158*22ce4affSfengbojiang 	if (IPSEC_ENABLED(ipv6)) {
159*22ce4affSfengbojiang 		if ((error = IPSEC_FORWARD(ipv6, m)) != 0) {
160*22ce4affSfengbojiang 			/* mbuf consumed by IPsec */
161*22ce4affSfengbojiang 			m_freem(mcopy);
162*22ce4affSfengbojiang 			if (error != EINPROGRESS)
163*22ce4affSfengbojiang 				IP6STAT_INC(ip6s_cantforward);
164a9643ea8Slogwang 			return;
165a9643ea8Slogwang 		}
166*22ce4affSfengbojiang 		/* No IPsec processing required */
167*22ce4affSfengbojiang 	}
168a9643ea8Slogwang #endif
169a9643ea8Slogwang 	/*
170*22ce4affSfengbojiang 	 * ip6_forward() operates with IPv6 addresses with deembedded scope.
171a9643ea8Slogwang 	 *
172*22ce4affSfengbojiang 	 * There are 3 sources of IPv6 destination address:
173*22ce4affSfengbojiang 	 *
174*22ce4affSfengbojiang 	 * 1) ip6_input(), where ip6_dst contains deembedded address.
175*22ce4affSfengbojiang 	 *   In order to deal with forwarding of link-local packets,
176*22ce4affSfengbojiang 	 *   calculate the scope based on input interface (RFC 4007, clause 9).
177*22ce4affSfengbojiang 	 * 2) packet filters changing ip6_dst directly. It would embed scope
178*22ce4affSfengbojiang 	 *   for LL addresses, so in6_localip() performs properly.
179*22ce4affSfengbojiang 	 * 3) packet filters attaching PACKET_TAG_IPFORWARD would embed
180*22ce4affSfengbojiang 	 *   scope for the nexthop.
181a9643ea8Slogwang 	 */
182*22ce4affSfengbojiang 	bzero(&dst, sizeof(struct sockaddr_in6));
183*22ce4affSfengbojiang 	dst.sin6_family = AF_INET6;
184*22ce4affSfengbojiang 	dst.sin6_addr = ip6->ip6_dst;
185*22ce4affSfengbojiang 	dst.sin6_scope_id = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
186a9643ea8Slogwang again:
187*22ce4affSfengbojiang 	nh = fib6_lookup(M_GETFIB(m), &dst.sin6_addr, dst.sin6_scope_id,
188*22ce4affSfengbojiang 	    NHR_REF, m->m_pkthdr.flowid);
189*22ce4affSfengbojiang 	if (nh == NULL) {
190a9643ea8Slogwang 		IP6STAT_INC(ip6s_noroute);
191a9643ea8Slogwang 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
192a9643ea8Slogwang 		if (mcopy) {
193a9643ea8Slogwang 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
194a9643ea8Slogwang 			ICMP6_DST_UNREACH_NOROUTE, 0);
195a9643ea8Slogwang 		}
196a9643ea8Slogwang 		goto bad;
197a9643ea8Slogwang 	}
198a9643ea8Slogwang 
199a9643ea8Slogwang 	/*
200a9643ea8Slogwang 	 * Source scope check: if a packet can't be delivered to its
201a9643ea8Slogwang 	 * destination for the reason that the destination is beyond the scope
202a9643ea8Slogwang 	 * of the source address, discard the packet and return an icmp6
203a9643ea8Slogwang 	 * destination unreachable error with Code 2 (beyond scope of source
204*22ce4affSfengbojiang 	 * address).
205a9643ea8Slogwang 	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
206a9643ea8Slogwang 	 */
207*22ce4affSfengbojiang 	outzone = in6_get_unicast_scopeid(&ip6->ip6_src, nh->nh_ifp);
208*22ce4affSfengbojiang 	inzone = in6_get_unicast_scopeid(&ip6->ip6_src, m->m_pkthdr.rcvif);
209a9643ea8Slogwang 	if (inzone != outzone) {
210a9643ea8Slogwang 		IP6STAT_INC(ip6s_cantforward);
211a9643ea8Slogwang 		IP6STAT_INC(ip6s_badscope);
212*22ce4affSfengbojiang 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_discard);
213a9643ea8Slogwang 
214a9643ea8Slogwang 		if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
215a9643ea8Slogwang 			V_ip6_log_time = time_uptime;
216a9643ea8Slogwang 			log(LOG_DEBUG,
217a9643ea8Slogwang 			    "cannot forward "
218a9643ea8Slogwang 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
219a9643ea8Slogwang 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
220a9643ea8Slogwang 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
221a9643ea8Slogwang 			    ip6->ip6_nxt,
222*22ce4affSfengbojiang 			    if_name(m->m_pkthdr.rcvif), if_name(nh->nh_ifp));
223a9643ea8Slogwang 		}
224a9643ea8Slogwang 		if (mcopy)
225a9643ea8Slogwang 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
226a9643ea8Slogwang 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
227a9643ea8Slogwang 		goto bad;
228a9643ea8Slogwang 	}
229a9643ea8Slogwang 
230a9643ea8Slogwang 	/*
231a9643ea8Slogwang 	 * Destination scope check: if a packet is going to break the scope
232a9643ea8Slogwang 	 * zone of packet's destination address, discard it.  This case should
233a9643ea8Slogwang 	 * usually be prevented by appropriately-configured routing table, but
234a9643ea8Slogwang 	 * we need an explicit check because we may mistakenly forward the
235a9643ea8Slogwang 	 * packet to a different zone by (e.g.) a default route.
236a9643ea8Slogwang 	 */
237*22ce4affSfengbojiang 	inzone = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
238*22ce4affSfengbojiang 	outzone = in6_get_unicast_scopeid(&ip6->ip6_dst, nh->nh_ifp);
239*22ce4affSfengbojiang 
240*22ce4affSfengbojiang 	if (inzone != outzone) {
241a9643ea8Slogwang 		IP6STAT_INC(ip6s_cantforward);
242a9643ea8Slogwang 		IP6STAT_INC(ip6s_badscope);
243a9643ea8Slogwang 		goto bad;
244a9643ea8Slogwang 	}
245a9643ea8Slogwang 
246*22ce4affSfengbojiang 	if (nh->nh_flags & NHF_GATEWAY) {
247*22ce4affSfengbojiang 		/* Store gateway address in deembedded form */
248*22ce4affSfengbojiang 		dst.sin6_addr = nh->gw6_sa.sin6_addr;
249*22ce4affSfengbojiang 		dst.sin6_scope_id = ntohs(in6_getscope(&dst.sin6_addr));
250*22ce4affSfengbojiang 		in6_clearscope(&dst.sin6_addr);
251*22ce4affSfengbojiang 	}
252a9643ea8Slogwang 
253a9643ea8Slogwang 	/*
254a9643ea8Slogwang 	 * If we are to forward the packet using the same interface
255a9643ea8Slogwang 	 * as one we got the packet from, perhaps we should send a redirect
256a9643ea8Slogwang 	 * to sender to shortcut a hop.
257a9643ea8Slogwang 	 * Only send redirect if source is sending directly to us,
258a9643ea8Slogwang 	 * and if packet was not source routed (or has any options).
259a9643ea8Slogwang 	 * Also, don't send redirect if forwarding using a route
260a9643ea8Slogwang 	 * modified by a redirect.
261a9643ea8Slogwang 	 */
262*22ce4affSfengbojiang 	if (V_ip6_sendredirects && nh->nh_ifp == m->m_pkthdr.rcvif && !srcrt &&
263*22ce4affSfengbojiang 	    (nh->nh_flags & NHF_REDIRECT) == 0)
264a9643ea8Slogwang 		type = ND_REDIRECT;
265a9643ea8Slogwang 
266a9643ea8Slogwang 	/*
267a9643ea8Slogwang 	 * Fake scoped addresses. Note that even link-local source or
268a9643ea8Slogwang 	 * destinaion can appear, if the originating node just sends the
269a9643ea8Slogwang 	 * packet to us (without address resolution for the destination).
270a9643ea8Slogwang 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
271a9643ea8Slogwang 	 * link identifiers, we can do this stuff after making a copy for
272a9643ea8Slogwang 	 * returning an error.
273a9643ea8Slogwang 	 */
274*22ce4affSfengbojiang 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
275a9643ea8Slogwang 		/*
276a9643ea8Slogwang 		 * See corresponding comments in ip6_output.
277a9643ea8Slogwang 		 * XXX: but is it possible that ip6_forward() sends a packet
278a9643ea8Slogwang 		 *      to a loopback interface? I don't think so, and thus
279a9643ea8Slogwang 		 *      I bark here. ([email protected])
280a9643ea8Slogwang 		 * XXX: it is common to route invalid packets to loopback.
281a9643ea8Slogwang 		 *	also, the codepath will be visited on use of ::1 in
282a9643ea8Slogwang 		 *	rthdr. (itojun)
283a9643ea8Slogwang 		 */
284a9643ea8Slogwang #if 1
285a9643ea8Slogwang 		if (0)
286a9643ea8Slogwang #else
287a9643ea8Slogwang 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
288a9643ea8Slogwang #endif
289a9643ea8Slogwang 		{
290a9643ea8Slogwang 			printf("ip6_forward: outgoing interface is loopback. "
291a9643ea8Slogwang 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
292a9643ea8Slogwang 			       ip6_sprintf(ip6bufs, &ip6->ip6_src),
293a9643ea8Slogwang 			       ip6_sprintf(ip6bufd, &ip6->ip6_dst),
294a9643ea8Slogwang 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
295*22ce4affSfengbojiang 			       if_name(nh->nh_ifp));
296a9643ea8Slogwang 		}
297a9643ea8Slogwang 
298a9643ea8Slogwang 		/* we can just use rcvif in forwarding. */
299a9643ea8Slogwang 		origifp = m->m_pkthdr.rcvif;
300a9643ea8Slogwang 	}
301a9643ea8Slogwang 	else
302*22ce4affSfengbojiang 		origifp = nh->nh_ifp;
303a9643ea8Slogwang 	/*
304a9643ea8Slogwang 	 * clear embedded scope identifiers if necessary.
305a9643ea8Slogwang 	 * in6_clearscope will touch the addresses only when necessary.
306a9643ea8Slogwang 	 */
307a9643ea8Slogwang 	in6_clearscope(&ip6->ip6_src);
308a9643ea8Slogwang 	in6_clearscope(&ip6->ip6_dst);
309a9643ea8Slogwang 
310a9643ea8Slogwang 	/* Jump over all PFIL processing if hooks are not active. */
311*22ce4affSfengbojiang 	if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
312a9643ea8Slogwang 		goto pass;
313a9643ea8Slogwang 
314a9643ea8Slogwang 	odst = ip6->ip6_dst;
315*22ce4affSfengbojiang 	/* Run through list of hooks for forwarded packets. */
316*22ce4affSfengbojiang 	if (pfil_run_hooks(V_inet6_pfil_head, &m, nh->nh_ifp, PFIL_OUT |
317*22ce4affSfengbojiang 	    PFIL_FWD, NULL) != PFIL_PASS)
318*22ce4affSfengbojiang 		goto freecopy;
319a9643ea8Slogwang 	ip6 = mtod(m, struct ip6_hdr *);
320a9643ea8Slogwang 
321a9643ea8Slogwang 	/* See if destination IP address was changed by packet filter. */
322a9643ea8Slogwang 	if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
323a9643ea8Slogwang 		m->m_flags |= M_SKIP_FIREWALL;
324a9643ea8Slogwang 		/* If destination is now ourself drop to ip6_input(). */
325a9643ea8Slogwang 		if (in6_localip(&ip6->ip6_dst))
326a9643ea8Slogwang 			m->m_flags |= M_FASTFWD_OURS;
327a9643ea8Slogwang 		else {
328*22ce4affSfengbojiang 			NH_FREE(nh);
329*22ce4affSfengbojiang 
330*22ce4affSfengbojiang 			/* Update address and scopeid. Assume scope is embedded */
331*22ce4affSfengbojiang 			dst.sin6_scope_id = ntohs(in6_getscope(&ip6->ip6_dst));
332*22ce4affSfengbojiang 			dst.sin6_addr = ip6->ip6_dst;
333*22ce4affSfengbojiang 			in6_clearscope(&dst.sin6_addr);
334a9643ea8Slogwang 			goto again;	/* Redo the routing table lookup. */
335a9643ea8Slogwang 		}
336a9643ea8Slogwang 	}
337a9643ea8Slogwang 
338a9643ea8Slogwang 	/* See if local, if yes, send it to netisr. */
339a9643ea8Slogwang 	if (m->m_flags & M_FASTFWD_OURS) {
340a9643ea8Slogwang 		if (m->m_pkthdr.rcvif == NULL)
341a9643ea8Slogwang 			m->m_pkthdr.rcvif = V_loif;
342a9643ea8Slogwang 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
343a9643ea8Slogwang 			m->m_pkthdr.csum_flags |=
344a9643ea8Slogwang 			    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
345a9643ea8Slogwang 			m->m_pkthdr.csum_data = 0xffff;
346a9643ea8Slogwang 		}
347*22ce4affSfengbojiang #if defined(SCTP) || defined(SCTP_SUPPORT)
348a9643ea8Slogwang 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
349a9643ea8Slogwang 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
350a9643ea8Slogwang #endif
351a9643ea8Slogwang 		error = netisr_queue(NETISR_IPV6, m);
352a9643ea8Slogwang 		goto out;
353a9643ea8Slogwang 	}
354a9643ea8Slogwang 	/* Or forward to some other address? */
355a9643ea8Slogwang 	if ((m->m_flags & M_IP6_NEXTHOP) &&
356a9643ea8Slogwang 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
357*22ce4affSfengbojiang 		struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)(fwd_tag + 1);
358*22ce4affSfengbojiang 
359*22ce4affSfengbojiang 		/* Update address and scopeid. Assume scope is embedded */
360*22ce4affSfengbojiang 		dst.sin6_scope_id = ntohs(in6_getscope(&gw6->sin6_addr));
361*22ce4affSfengbojiang 		dst.sin6_addr = gw6->sin6_addr;
362*22ce4affSfengbojiang 		in6_clearscope(&dst.sin6_addr);
363*22ce4affSfengbojiang 
364a9643ea8Slogwang 		m->m_flags |= M_SKIP_FIREWALL;
365a9643ea8Slogwang 		m->m_flags &= ~M_IP6_NEXTHOP;
366a9643ea8Slogwang 		m_tag_delete(m, fwd_tag);
367*22ce4affSfengbojiang 		NH_FREE(nh);
368*22ce4affSfengbojiang 		goto again;
369a9643ea8Slogwang 	}
370a9643ea8Slogwang 
371a9643ea8Slogwang pass:
372a9643ea8Slogwang 	/* See if the size was changed by the packet filter. */
373*22ce4affSfengbojiang 	/* TODO: change to nh->nh_mtu */
374*22ce4affSfengbojiang 	if (m->m_pkthdr.len > IN6_LINKMTU(nh->nh_ifp)) {
375*22ce4affSfengbojiang 		in6_ifstat_inc(nh->nh_ifp, ifs6_in_toobig);
376*22ce4affSfengbojiang 		if (mcopy)
377*22ce4affSfengbojiang 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
378*22ce4affSfengbojiang 			    IN6_LINKMTU(nh->nh_ifp));
379a9643ea8Slogwang 		goto bad;
380a9643ea8Slogwang 	}
381a9643ea8Slogwang 
382*22ce4affSfengbojiang 	/* Currently LLE layer stores embedded IPv6 addresses */
383*22ce4affSfengbojiang 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr)) {
384*22ce4affSfengbojiang 		in6_set_unicast_scopeid(&dst.sin6_addr, dst.sin6_scope_id);
385*22ce4affSfengbojiang 		dst.sin6_scope_id = 0;
386*22ce4affSfengbojiang 	}
387*22ce4affSfengbojiang 	error = nd6_output_ifp(nh->nh_ifp, origifp, m, &dst, NULL);
388a9643ea8Slogwang 	if (error) {
389*22ce4affSfengbojiang 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_discard);
390a9643ea8Slogwang 		IP6STAT_INC(ip6s_cantforward);
391a9643ea8Slogwang 	} else {
392a9643ea8Slogwang 		IP6STAT_INC(ip6s_forward);
393*22ce4affSfengbojiang 		in6_ifstat_inc(nh->nh_ifp, ifs6_out_forward);
394a9643ea8Slogwang 		if (type)
395a9643ea8Slogwang 			IP6STAT_INC(ip6s_redirectsent);
396a9643ea8Slogwang 		else {
397a9643ea8Slogwang 			if (mcopy)
398a9643ea8Slogwang 				goto freecopy;
399a9643ea8Slogwang 		}
400a9643ea8Slogwang 	}
401a9643ea8Slogwang 
402a9643ea8Slogwang 	if (mcopy == NULL)
403a9643ea8Slogwang 		goto out;
404a9643ea8Slogwang 	switch (error) {
405a9643ea8Slogwang 	case 0:
406a9643ea8Slogwang 		if (type == ND_REDIRECT) {
407*22ce4affSfengbojiang 			icmp6_redirect_output(mcopy, nh);
408a9643ea8Slogwang 			goto out;
409a9643ea8Slogwang 		}
410a9643ea8Slogwang 		goto freecopy;
411a9643ea8Slogwang 
412a9643ea8Slogwang 	case EMSGSIZE:
413a9643ea8Slogwang 		/* xxx MTU is constant in PPP? */
414a9643ea8Slogwang 		goto freecopy;
415a9643ea8Slogwang 
416a9643ea8Slogwang 	case ENOBUFS:
417a9643ea8Slogwang 		/* Tell source to slow down like source quench in IP? */
418a9643ea8Slogwang 		goto freecopy;
419a9643ea8Slogwang 
420a9643ea8Slogwang 	case ENETUNREACH:	/* shouldn't happen, checked above */
421a9643ea8Slogwang 	case EHOSTUNREACH:
422a9643ea8Slogwang 	case ENETDOWN:
423a9643ea8Slogwang 	case EHOSTDOWN:
424a9643ea8Slogwang 	default:
425a9643ea8Slogwang 		type = ICMP6_DST_UNREACH;
426a9643ea8Slogwang 		code = ICMP6_DST_UNREACH_ADDR;
427a9643ea8Slogwang 		break;
428a9643ea8Slogwang 	}
429a9643ea8Slogwang 	icmp6_error(mcopy, type, code, 0);
430a9643ea8Slogwang 	goto out;
431a9643ea8Slogwang 
432a9643ea8Slogwang  freecopy:
433a9643ea8Slogwang 	m_freem(mcopy);
434a9643ea8Slogwang 	goto out;
435a9643ea8Slogwang bad:
436a9643ea8Slogwang 	m_freem(m);
437a9643ea8Slogwang out:
438*22ce4affSfengbojiang 	if (nh != NULL)
439*22ce4affSfengbojiang 		NH_FREE(nh);
440a9643ea8Slogwang }
441