ip6_forward.c revision 148385
1132332Smarcel/*	$FreeBSD: head/sys/netinet6/ip6_forward.c 148385 2005-07-25 12:31:43Z ume $	*/
2132950Sdavidxu/*	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $	*/
3132332Smarcel
4132332Smarcel/*-
5132332Smarcel * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6132332Smarcel * All rights reserved.
7132332Smarcel *
8132332Smarcel * Redistribution and use in source and binary forms, with or without
9132332Smarcel * modification, are permitted provided that the following conditions
10132332Smarcel * are met:
11132332Smarcel * 1. Redistributions of source code must retain the above copyright
12132332Smarcel *    notice, this list of conditions and the following disclaimer.
13132332Smarcel * 2. Redistributions in binary form must reproduce the above copyright
14132332Smarcel *    notice, this list of conditions and the following disclaimer in the
15132332Smarcel *    documentation and/or other materials provided with the distribution.
16132332Smarcel * 3. Neither the name of the project nor the names of its contributors
17132332Smarcel *    may be used to endorse or promote products derived from this software
18132332Smarcel *    without specific prior written permission.
19132332Smarcel *
20132332Smarcel * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21132332Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22132332Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23132332Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24132332Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25132332Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26132332Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27132332Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28132332Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29132332Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30132332Smarcel * SUCH DAMAGE.
31132360Smarcel */
32132360Smarcel
33132360Smarcel#include "opt_ip6fw.h"
34132332Smarcel#include "opt_inet.h"
35132332Smarcel#include "opt_inet6.h"
36132332Smarcel#include "opt_ipsec.h"
37132950Sdavidxu
38132950Sdavidxu#include <sys/param.h>
39132950Sdavidxu#include <sys/systm.h>
40132950Sdavidxu#include <sys/malloc.h>
41132950Sdavidxu#include <sys/mbuf.h>
42132950Sdavidxu#include <sys/domain.h>
43132950Sdavidxu#include <sys/protosw.h>
44132950Sdavidxu#include <sys/socket.h>
45132950Sdavidxu#include <sys/errno.h>
46132950Sdavidxu#include <sys/time.h>
47132950Sdavidxu#include <sys/kernel.h>
48132950Sdavidxu#include <sys/syslog.h>
49132950Sdavidxu
50132950Sdavidxu#include <net/if.h>
51132950Sdavidxu#include <net/route.h>
52132950Sdavidxu#include <net/pfil.h>
53132950Sdavidxu
54132950Sdavidxu#include <netinet/in.h>
55132950Sdavidxu#include <netinet/in_var.h>
56132950Sdavidxu#include <netinet/in_systm.h>
57132950Sdavidxu#include <netinet/ip.h>
58132950Sdavidxu#include <netinet/ip_var.h>
59132332Smarcel#include <netinet6/in6_var.h>
60132332Smarcel#include <netinet/ip6.h>
61132332Smarcel#include <netinet6/ip6_var.h>
62132332Smarcel#include <netinet6/scope6_var.h>
63132332Smarcel#include <netinet/icmp6.h>
64132950Sdavidxu#include <netinet6/nd6.h>
65132950Sdavidxu
66132950Sdavidxu#include <netinet/in_pcb.h>
67132950Sdavidxu
68132950Sdavidxu#ifdef IPSEC
69132950Sdavidxu#include <netinet6/ipsec.h>
70132950Sdavidxu#ifdef INET6
71132950Sdavidxu#include <netinet6/ipsec6.h>
72132950Sdavidxu#endif
73132950Sdavidxu#include <netkey/key.h>
74132950Sdavidxu#endif /* IPSEC */
75132950Sdavidxu
76132950Sdavidxu#ifdef FAST_IPSEC
77132950Sdavidxu#include <netipsec/ipsec.h>
78132950Sdavidxu#include <netipsec/ipsec6.h>
79132950Sdavidxu#include <netipsec/key.h>
80132950Sdavidxu#define	IPSEC
81132950Sdavidxu#endif /* FAST_IPSEC */
82132950Sdavidxu
83132950Sdavidxu#include <netinet6/ip6_fw.h>
84132950Sdavidxu
85132950Sdavidxu#include <net/net_osdep.h>
86132332Smarcel
87132332Smarcel#include <netinet6/ip6protosw.h>
88132332Smarcel
89132332Smarcelstruct	route_in6 ip6_forward_rt;
90132332Smarcel
91132950Sdavidxu/*
92132332Smarcel * Forward a packet.  If some error occurs return the sender
93132332Smarcel * an icmp packet.  Note we can't always generate a meaningful
94132332Smarcel * icmp message because icmp doesn't have a large enough repertoire
95132332Smarcel * of codes and types.
96132332Smarcel *
97132950Sdavidxu * If not forwarding, just drop the packet.  This could be confusing
98132332Smarcel * if ipforwarding was zero but some routing protocol was advancing
99132332Smarcel * us as a gateway to somewhere.  However, we must let the routing
100132332Smarcel * protocol deal with that.
101132332Smarcel *
102132332Smarcel */
103132950Sdavidxu
104132332Smarcelvoid
105132332Smarcelip6_forward(m, srcrt)
106132332Smarcel	struct mbuf *m;
107132332Smarcel	int srcrt;
108132332Smarcel{
109132950Sdavidxu	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
110132950Sdavidxu	struct sockaddr_in6 *dst = NULL;
111132950Sdavidxu	struct rtentry *rt = NULL;
112132950Sdavidxu	int error, type = 0, code = 0;
113132950Sdavidxu	struct mbuf *mcopy = NULL;
114132950Sdavidxu	struct ifnet *origifp;	/* maybe unnecessary */
115132950Sdavidxu	u_int32_t inzone, outzone;
116132950Sdavidxu	struct in6_addr src_in6, dst_in6;
117132332Smarcel#ifdef IPSEC
118	struct secpolicy *sp = NULL;
119	int ipsecrt = 0;
120#endif
121
122#ifdef IPSEC
123	/*
124	 * Check AH/ESP integrity.
125	 */
126	/*
127	 * Don't increment ip6s_cantforward because this is the check
128	 * before forwarding packet actually.
129	 */
130	if (ipsec6_in_reject(m, NULL)) {
131#if !defined(FAST_IPSEC)
132		ipsec6stat.in_polvio++;
133#endif
134		m_freem(m);
135		return;
136	}
137#endif /* IPSEC */
138
139	/*
140	 * Do not forward packets to multicast destination (should be handled
141	 * by ip6_mforward().
142	 * Do not forward packets with unspecified source.  It was discussed
143	 * in July 2000, on the ipngwg mailing list.
144	 */
145	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
146	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
147	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
148		ip6stat.ip6s_cantforward++;
149		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
150		if (ip6_log_time + ip6_log_interval < time_second) {
151			ip6_log_time = time_second;
152			log(LOG_DEBUG,
153			    "cannot forward "
154			    "from %s to %s nxt %d received on %s\n",
155			    ip6_sprintf(&ip6->ip6_src),
156			    ip6_sprintf(&ip6->ip6_dst),
157			    ip6->ip6_nxt,
158			    if_name(m->m_pkthdr.rcvif));
159		}
160		m_freem(m);
161		return;
162	}
163
164	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
165		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
166		icmp6_error(m, ICMP6_TIME_EXCEEDED,
167				ICMP6_TIME_EXCEED_TRANSIT, 0);
168		return;
169	}
170	ip6->ip6_hlim -= IPV6_HLIMDEC;
171
172	/*
173	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
174	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
175	 * we need to generate an ICMP6 message to the src.
176	 * Thanks to M_EXT, in most cases copy will not occur.
177	 *
178	 * It is important to save it before IPsec processing as IPsec
179	 * processing may modify the mbuf.
180	 */
181	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
182
183#ifdef IPSEC
184	/* get a security policy for this packet */
185	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
186	    IP_FORWARDING, &error);
187	if (sp == NULL) {
188		ipsec6stat.out_inval++;
189		ip6stat.ip6s_cantforward++;
190		if (mcopy) {
191#if 0
192			/* XXX: what icmp ? */
193#else
194			m_freem(mcopy);
195#endif
196		}
197		m_freem(m);
198		return;
199	}
200
201	error = 0;
202
203	/* check policy */
204	switch (sp->policy) {
205	case IPSEC_POLICY_DISCARD:
206		/*
207		 * This packet is just discarded.
208		 */
209		ipsec6stat.out_polvio++;
210		ip6stat.ip6s_cantforward++;
211		key_freesp(sp);
212		if (mcopy) {
213#if 0
214			/* XXX: what icmp ? */
215#else
216			m_freem(mcopy);
217#endif
218		}
219		m_freem(m);
220		return;
221
222	case IPSEC_POLICY_BYPASS:
223	case IPSEC_POLICY_NONE:
224		/* no need to do IPsec. */
225		key_freesp(sp);
226		goto skip_ipsec;
227
228	case IPSEC_POLICY_IPSEC:
229		if (sp->req == NULL) {
230			/* XXX should be panic ? */
231			printf("ip6_forward: No IPsec request specified.\n");
232			ip6stat.ip6s_cantforward++;
233			key_freesp(sp);
234			if (mcopy) {
235#if 0
236				/* XXX: what icmp ? */
237#else
238				m_freem(mcopy);
239#endif
240			}
241			m_freem(m);
242			return;
243		}
244		/* do IPsec */
245		break;
246
247	case IPSEC_POLICY_ENTRUST:
248	default:
249		/* should be panic ?? */
250		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
251		key_freesp(sp);
252		goto skip_ipsec;
253	}
254
255    {
256	struct ipsecrequest *isr = NULL;
257	struct ipsec_output_state state;
258
259	/*
260	 * when the kernel forwards a packet, it is not proper to apply
261	 * IPsec transport mode to the packet is not proper.  this check
262	 * avoid from this.
263	 * at present, if there is even a transport mode SA request in the
264	 * security policy, the kernel does not apply IPsec to the packet.
265	 * this check is not enough because the following case is valid.
266	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
267	 */
268	for (isr = sp->req; isr; isr = isr->next) {
269		if (isr->saidx.mode == IPSEC_MODE_ANY)
270			goto doipsectunnel;
271		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
272			goto doipsectunnel;
273	}
274
275	/*
276	 * if there's no need for tunnel mode IPsec, skip.
277	 */
278	if (!isr)
279		goto skip_ipsec;
280
281    doipsectunnel:
282	/*
283	 * All the extension headers will become inaccessible
284	 * (since they can be encrypted).
285	 * Don't panic, we need no more updates to extension headers
286	 * on inner IPv6 packet (since they are now encapsulated).
287	 *
288	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
289	 */
290	bzero(&state, sizeof(state));
291	state.m = m;
292	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
293	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
294
295	error = ipsec6_output_tunnel(&state, sp, 0);
296
297	m = state.m;
298	key_freesp(sp);
299
300	if (error) {
301		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
302		switch (error) {
303		case EHOSTUNREACH:
304		case ENETUNREACH:
305		case EMSGSIZE:
306		case ENOBUFS:
307		case ENOMEM:
308			break;
309		default:
310			printf("ip6_output (ipsec): error code %d\n", error);
311			/* FALLTHROUGH */
312		case ENOENT:
313			/* don't show these error codes to the user */
314			break;
315		}
316		ip6stat.ip6s_cantforward++;
317		if (mcopy) {
318#if 0
319			/* XXX: what icmp ? */
320#else
321			m_freem(mcopy);
322#endif
323		}
324		m_freem(m);
325		return;
326	}
327
328	if (ip6 != mtod(m, struct ip6_hdr *)) {
329		/*
330		 * now tunnel mode headers are added.  we are originating
331		 * packet instead of forwarding the packet.
332		 */
333		ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
334		    NULL);
335		goto freecopy;
336	}
337
338	/* adjust pointer */
339	dst = (struct sockaddr_in6 *)state.dst;
340	rt = state.ro ? state.ro->ro_rt : NULL;
341	if (dst != NULL && rt != NULL)
342		ipsecrt = 1;
343    }
344    skip_ipsec:
345#endif /* IPSEC */
346
347#ifdef IPSEC
348	if (ipsecrt)
349		goto skip_routing;
350#endif
351
352	dst = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
353	if (!srcrt) {
354		/* ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst */
355		if (ip6_forward_rt.ro_rt == 0 ||
356		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
357			if (ip6_forward_rt.ro_rt) {
358				RTFREE(ip6_forward_rt.ro_rt);
359				ip6_forward_rt.ro_rt = 0;
360			}
361
362			/* this probably fails but give it a try again */
363			rtalloc((struct route *)&ip6_forward_rt);
364		}
365
366		if (ip6_forward_rt.ro_rt == 0) {
367			ip6stat.ip6s_noroute++;
368			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
369			if (mcopy) {
370				icmp6_error(mcopy, ICMP6_DST_UNREACH,
371					    ICMP6_DST_UNREACH_NOROUTE, 0);
372			}
373			m_freem(m);
374			return;
375		}
376	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
377		   !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
378		if (ip6_forward_rt.ro_rt) {
379			RTFREE(ip6_forward_rt.ro_rt);
380			ip6_forward_rt.ro_rt = 0;
381		}
382		bzero(dst, sizeof(*dst));
383		dst->sin6_len = sizeof(struct sockaddr_in6);
384		dst->sin6_family = AF_INET6;
385		dst->sin6_addr = ip6->ip6_dst;
386
387  		rtalloc((struct route *)&ip6_forward_rt);
388		if (ip6_forward_rt.ro_rt == 0) {
389			ip6stat.ip6s_noroute++;
390			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
391			if (mcopy) {
392				icmp6_error(mcopy, ICMP6_DST_UNREACH,
393					    ICMP6_DST_UNREACH_NOROUTE, 0);
394			}
395			m_freem(m);
396			return;
397		}
398	}
399	rt = ip6_forward_rt.ro_rt;
400#ifdef IPSEC
401    skip_routing:;
402#endif
403
404	/*
405	 * Source scope check: if a packet can't be delivered to its
406	 * destination for the reason that the destination is beyond the scope
407	 * of the source address, discard the packet and return an icmp6
408	 * destination unreachable error with Code 2 (beyond scope of source
409	 * address).  We use a local copy of ip6_src, since in6_setscope()
410	 * will possibly modify its first argument.
411	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
412	 */
413	src_in6 = ip6->ip6_src;
414	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
415		/* XXX: this should not happen */
416		ip6stat.ip6s_cantforward++;
417		ip6stat.ip6s_badscope++;
418		m_freem(m);
419		return;
420	}
421	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
422		ip6stat.ip6s_cantforward++;
423		ip6stat.ip6s_badscope++;
424		m_freem(m);
425		return;
426	}
427	if (inzone != outzone
428#ifdef IPSEC
429	    && !ipsecrt
430#endif
431	    ) {
432		ip6stat.ip6s_cantforward++;
433		ip6stat.ip6s_badscope++;
434		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
435
436		if (ip6_log_time + ip6_log_interval < time_second) {
437			ip6_log_time = time_second;
438			log(LOG_DEBUG,
439			    "cannot forward "
440			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
441			    ip6_sprintf(&ip6->ip6_src),
442			    ip6_sprintf(&ip6->ip6_dst),
443			    ip6->ip6_nxt,
444			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
445		}
446		if (mcopy)
447			icmp6_error(mcopy, ICMP6_DST_UNREACH,
448				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
449		m_freem(m);
450		return;
451	}
452
453	/*
454	 * Destination scope check: if a packet is going to break the scope
455	 * zone of packet's destination address, discard it.  This case should
456	 * usually be prevented by appropriately-configured routing table, but
457	 * we need an explicit check because we may mistakenly forward the
458	 * packet to a different zone by (e.g.) a default route.
459	 */
460	dst_in6 = ip6->ip6_dst;
461	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
462	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
463	    inzone != outzone) {
464		ip6stat.ip6s_cantforward++;
465		ip6stat.ip6s_badscope++;
466		m_freem(m);
467		return;
468	}
469
470	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
471		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
472		if (mcopy) {
473			u_long mtu;
474#ifdef IPSEC
475			struct secpolicy *sp;
476			int ipsecerror;
477			size_t ipsechdrsiz;
478#endif
479
480			mtu = IN6_LINKMTU(rt->rt_ifp);
481#ifdef IPSEC
482			/*
483			 * When we do IPsec tunnel ingress, we need to play
484			 * with the link value (decrement IPsec header size
485			 * from mtu value).  The code is much simpler than v4
486			 * case, as we have the outgoing interface for
487			 * encapsulated packet as "rt->rt_ifp".
488			 */
489			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
490				IP_FORWARDING, &ipsecerror);
491			if (sp) {
492				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
493					IPSEC_DIR_OUTBOUND, NULL);
494				if (ipsechdrsiz < mtu)
495					mtu -= ipsechdrsiz;
496			}
497
498			/*
499			 * if mtu becomes less than minimum MTU,
500			 * tell minimum MTU (and I'll need to fragment it).
501			 */
502			if (mtu < IPV6_MMTU)
503				mtu = IPV6_MMTU;
504#endif
505			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
506		}
507		m_freem(m);
508		return;
509	}
510
511	if (rt->rt_flags & RTF_GATEWAY)
512		dst = (struct sockaddr_in6 *)rt->rt_gateway;
513
514	/*
515	 * If we are to forward the packet using the same interface
516	 * as one we got the packet from, perhaps we should send a redirect
517	 * to sender to shortcut a hop.
518	 * Only send redirect if source is sending directly to us,
519	 * and if packet was not source routed (or has any options).
520	 * Also, don't send redirect if forwarding using a route
521	 * modified by a redirect.
522	 */
523	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
524#ifdef IPSEC
525	    !ipsecrt &&
526#endif
527	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
528		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
529			/*
530			 * If the incoming interface is equal to the outgoing
531			 * one, and the link attached to the interface is
532			 * point-to-point, then it will be highly probable
533			 * that a routing loop occurs. Thus, we immediately
534			 * drop the packet and send an ICMPv6 error message.
535			 *
536			 * type/code is based on suggestion by Rich Draves.
537			 * not sure if it is the best pick.
538			 */
539			icmp6_error(mcopy, ICMP6_DST_UNREACH,
540				    ICMP6_DST_UNREACH_ADDR, 0);
541			m_freem(m);
542			return;
543		}
544		type = ND_REDIRECT;
545	}
546
547	/*
548	 * Check with the firewall...
549	 */
550	if (ip6_fw_enable && ip6_fw_chk_ptr) {
551		u_short port = 0;
552		/* If ipfw says divert, we have to just drop packet */
553		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
554			m_freem(m);
555			goto freecopy;
556		}
557		if (!m)
558			goto freecopy;
559	}
560
561	/*
562	 * Fake scoped addresses. Note that even link-local source or
563	 * destinaion can appear, if the originating node just sends the
564	 * packet to us (without address resolution for the destination).
565	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
566	 * link identifiers, we can do this stuff after making a copy for
567	 * returning an error.
568	 */
569	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
570		/*
571		 * See corresponding comments in ip6_output.
572		 * XXX: but is it possible that ip6_forward() sends a packet
573		 *      to a loopback interface? I don't think so, and thus
574		 *      I bark here. (jinmei@kame.net)
575		 * XXX: it is common to route invalid packets to loopback.
576		 *	also, the codepath will be visited on use of ::1 in
577		 *	rthdr. (itojun)
578		 */
579#if 1
580		if (0)
581#else
582		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
583#endif
584		{
585			printf("ip6_forward: outgoing interface is loopback. "
586			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
587			       ip6_sprintf(&ip6->ip6_src),
588			       ip6_sprintf(&ip6->ip6_dst),
589			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
590			       if_name(rt->rt_ifp));
591		}
592
593		/* we can just use rcvif in forwarding. */
594		origifp = m->m_pkthdr.rcvif;
595	}
596	else
597		origifp = rt->rt_ifp;
598	/*
599	 * clear embedded scope identifiers if necessary.
600	 * in6_clearscope will touch the addresses only when necessary.
601	 */
602	in6_clearscope(&ip6->ip6_src);
603	in6_clearscope(&ip6->ip6_dst);
604
605	/* Jump over all PFIL processing if hooks are not active. */
606	if (inet6_pfil_hook.ph_busy_count == -1)
607		goto pass;
608
609	/* Run through list of hooks for output packets. */
610	error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT, NULL);
611	if (error != 0)
612		goto senderr;
613	if (m == NULL)
614		goto freecopy;
615	ip6 = mtod(m, struct ip6_hdr *);
616
617pass:
618	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
619	if (error) {
620		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
621		ip6stat.ip6s_cantforward++;
622	} else {
623		ip6stat.ip6s_forward++;
624		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
625		if (type)
626			ip6stat.ip6s_redirectsent++;
627		else {
628			if (mcopy)
629				goto freecopy;
630		}
631	}
632
633senderr:
634	if (mcopy == NULL)
635		return;
636	switch (error) {
637	case 0:
638		if (type == ND_REDIRECT) {
639			icmp6_redirect_output(mcopy, rt);
640			return;
641		}
642		goto freecopy;
643
644	case EMSGSIZE:
645		/* xxx MTU is constant in PPP? */
646		goto freecopy;
647
648	case ENOBUFS:
649		/* Tell source to slow down like source quench in IP? */
650		goto freecopy;
651
652	case ENETUNREACH:	/* shouldn't happen, checked above */
653	case EHOSTUNREACH:
654	case ENETDOWN:
655	case EHOSTDOWN:
656	default:
657		type = ICMP6_DST_UNREACH;
658		code = ICMP6_DST_UNREACH_ADDR;
659		break;
660	}
661	icmp6_error(mcopy, type, code, 0);
662	return;
663
664 freecopy:
665	m_freem(mcopy);
666	return;
667}
668