xref: /netbsd-src/sys/netinet6/ip6_forward.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ip6_forward.c,v 1.58 2007/05/23 17:15:02 christos Exp $	*/
2 /*	$KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane 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 <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.58 2007/05/23 17:15:02 christos Exp $");
35 
36 #include "opt_ipsec.h"
37 #include "opt_pfil_hooks.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50 
51 #include <net/if.h>
52 #include <net/route.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/ip6_var.h>
59 #include <netinet6/scope6_var.h>
60 #include <netinet/icmp6.h>
61 #include <netinet6/nd6.h>
62 
63 #ifdef IPSEC
64 #include <netinet6/ipsec.h>
65 #include <netkey/key.h>
66 #endif /* IPSEC */
67 
68 #ifdef FAST_IPSEC
69 #include <netipsec/ipsec.h>
70 #include <netipsec/ipsec6.h>
71 #include <netipsec/key.h>
72 #include <netipsec/xform.h>
73 #endif /* FAST_IPSEC */
74 
75 #ifdef PFIL_HOOKS
76 #include <net/pfil.h>
77 #endif
78 
79 #include <net/net_osdep.h>
80 
81 struct	route ip6_forward_rt;
82 
83 #ifdef PFIL_HOOKS
84 extern struct pfil_head inet6_pfil_hook;	/* XXX */
85 #endif
86 
87 /*
88  * Forward a packet.  If some error occurs return the sender
89  * an icmp packet.  Note we can't always generate a meaningful
90  * icmp message because icmp doesn't have a large enough repertoire
91  * of codes and types.
92  *
93  * If not forwarding, just drop the packet.  This could be confusing
94  * if ipforwarding was zero but some routing protocol was advancing
95  * us as a gateway to somewhere.  However, we must let the routing
96  * protocol deal with that.
97  *
98  */
99 
100 void
101 ip6_forward(struct mbuf *m, int srcrt)
102 {
103 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
104 	const struct sockaddr_in6 *dst;
105 	struct rtentry *rt;
106 	int error = 0, type = 0, code = 0;
107 	struct mbuf *mcopy = NULL;
108 	struct ifnet *origifp;	/* maybe unnecessary */
109 	u_int32_t inzone, outzone;
110 	struct in6_addr src_in6, dst_in6;
111 #ifdef IPSEC
112 	struct secpolicy *sp = NULL;
113 	int ipsecrt = 0;
114 #endif
115 #ifdef FAST_IPSEC
116     struct secpolicy *sp = NULL;
117     int needipsec = 0;
118     int s;
119 #endif
120 
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 		ipsec6stat.in_polvio++;
132 		m_freem(m);
133 		return;
134 	}
135 #endif /* IPSEC */
136 
137 	/*
138 	 * Do not forward packets to multicast destination (should be handled
139 	 * by ip6_mforward().
140 	 * Do not forward packets with unspecified source.  It was discussed
141 	 * in July 2000, on ipngwg mailing list.
142 	 */
143 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
144 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
145 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
146 		ip6stat.ip6s_cantforward++;
147 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
148 		if (ip6_log_time + ip6_log_interval < time_second) {
149 			ip6_log_time = time_second;
150 			log(LOG_DEBUG,
151 			    "cannot forward "
152 			    "from %s to %s nxt %d received on %s\n",
153 			    ip6_sprintf(&ip6->ip6_src),
154 			    ip6_sprintf(&ip6->ip6_dst),
155 			    ip6->ip6_nxt,
156 			    if_name(m->m_pkthdr.rcvif));
157 		}
158 		m_freem(m);
159 		return;
160 	}
161 
162 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
163 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
164 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
165 				ICMP6_TIME_EXCEED_TRANSIT, 0);
166 		return;
167 	}
168 	ip6->ip6_hlim -= IPV6_HLIMDEC;
169 
170 	/*
171 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
172 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
173 	 * we need to generate an ICMP6 message to the src.
174 	 * Thanks to M_EXT, in most cases copy will not occur.
175 	 *
176 	 * It is important to save it before IPsec processing as IPsec
177 	 * processing may modify the mbuf.
178 	 */
179 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
180 
181 #ifdef IPSEC
182 	/* get a security policy for this packet */
183 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
184 	    IP_FORWARDING, &error);
185 	if (sp == NULL) {
186 		ipsec6stat.out_inval++;
187 		ip6stat.ip6s_cantforward++;
188 		if (mcopy) {
189 #if 0
190 			/* XXX: what icmp ? */
191 #else
192 			m_freem(mcopy);
193 #endif
194 		}
195 		m_freem(m);
196 		return;
197 	}
198 
199 	error = 0;
200 
201 	/* check policy */
202 	switch (sp->policy) {
203 	case IPSEC_POLICY_DISCARD:
204 		/*
205 		 * This packet is just discarded.
206 		 */
207 		ipsec6stat.out_polvio++;
208 		ip6stat.ip6s_cantforward++;
209 		key_freesp(sp);
210 		if (mcopy) {
211 #if 0
212 			/* XXX: what icmp ? */
213 #else
214 			m_freem(mcopy);
215 #endif
216 		}
217 		m_freem(m);
218 		return;
219 
220 	case IPSEC_POLICY_BYPASS:
221 	case IPSEC_POLICY_NONE:
222 		/* no need to do IPsec. */
223 		key_freesp(sp);
224 		goto skip_ipsec;
225 
226 	case IPSEC_POLICY_IPSEC:
227 		if (sp->req == NULL) {
228 			/* XXX should be panic ? */
229 			printf("ip6_forward: No IPsec request specified.\n");
230 			ip6stat.ip6s_cantforward++;
231 			key_freesp(sp);
232 			if (mcopy) {
233 #if 0
234 				/* XXX: what icmp ? */
235 #else
236 				m_freem(mcopy);
237 #endif
238 			}
239 			m_freem(m);
240 			return;
241 		}
242 		/* do IPsec */
243 		break;
244 
245 	case IPSEC_POLICY_ENTRUST:
246 	default:
247 		/* should be panic ?? */
248 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
249 		key_freesp(sp);
250 		goto skip_ipsec;
251 	}
252 
253     {
254 	struct ipsecrequest *isr = NULL;
255 	struct ipsec_output_state state;
256 
257 	/*
258 	 * when the kernel forwards a packet, it is not proper to apply
259 	 * IPsec transport mode to the packet is not proper.  this check
260 	 * avoid from this.
261 	 * at present, if there is even a transport mode SA request in the
262 	 * security policy, the kernel does not apply IPsec to the packet.
263 	 * this check is not enough because the following case is valid.
264 	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
265 	 */
266 	for (isr = sp->req; isr; isr = isr->next) {
267 		if (isr->saidx.mode == IPSEC_MODE_ANY)
268 			goto doipsectunnel;
269 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
270 			goto doipsectunnel;
271 	}
272 
273 	/*
274 	 * if there's no need for tunnel mode IPsec, skip.
275 	 */
276 	if (!isr)
277 		goto skip_ipsec;
278 
279     doipsectunnel:
280 	/*
281 	 * All the extension headers will become inaccessible
282 	 * (since they can be encrypted).
283 	 * Don't panic, we need no more updates to extension headers
284 	 * on inner IPv6 packet (since they are now encapsulated).
285 	 *
286 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
287 	 */
288 	bzero(&state, sizeof(state));
289 	state.m = m;
290 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
291 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
292 
293 	error = ipsec6_output_tunnel(&state, sp, 0);
294 
295 	m = state.m;
296 	key_freesp(sp);
297 
298 	if (error) {
299 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
300 		switch (error) {
301 		case EHOSTUNREACH:
302 		case ENETUNREACH:
303 		case EMSGSIZE:
304 		case ENOBUFS:
305 		case ENOMEM:
306 			break;
307 		default:
308 			printf("ip6_forward (ipsec): error code %d\n", error);
309 			/* FALLTHROUGH */
310 		case ENOENT:
311 			/* don't show these error codes to the user */
312 			break;
313 		}
314 		ip6stat.ip6s_cantforward++;
315 		if (mcopy) {
316 #if 0
317 			/* XXX: what icmp ? */
318 #else
319 			m_freem(mcopy);
320 #endif
321 		}
322 		m_freem(m);
323 		return;
324 	}
325 
326 	if (ip6 != mtod(m, struct ip6_hdr *)) {
327 		/*
328 		 * now tunnel mode headers are added.  we are originating
329 		 * packet instead of forwarding the packet.
330 		 */
331 		ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
332 		    NULL);
333 		goto freecopy;
334 	}
335 
336 	/* adjust pointer */
337 	rt = state.ro ? state.ro->ro_rt : NULL;
338 	dst = (const struct sockaddr_in6 *)state.dst;
339 	if (dst != NULL && rt != NULL) {
340 		ipsecrt = 1;
341 		goto skip_routing;
342 	}
343     }
344     skip_ipsec:
345 #endif /* IPSEC */
346 #ifdef FAST_IPSEC
347 	/* Check the security policy (SP) for the packet */
348 
349 	sp = ipsec6_check_policy(m,NULL,0,&needipsec,&error);
350 	if (error != 0) {
351 		/*
352 		 * Hack: -EINVAL is used to signal that a packet
353 		 * should be silently discarded.  This is typically
354 		 * because we asked key management for an SA and
355 		 * it was delayed (e.g. kicked up to IKE).
356 		 */
357 	if (error == -EINVAL)
358 		error = 0;
359 	goto freecopy;
360 	}
361 #endif /* FAST_IPSEC */
362 
363 	if (!srcrt) {
364 		/*
365 		 * rtcache_getdst(ip6_forward_rt)->sin6_addr is equal to
366 		 * ip6->ip6_dst
367 		 */
368 		rtcache_check(&ip6_forward_rt);
369 		if (ip6_forward_rt.ro_rt == NULL) {
370 			rtcache_init(&ip6_forward_rt);
371 
372 			if (ip6_forward_rt.ro_rt == NULL) {
373 				ip6stat.ip6s_noroute++;
374 				/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
375 				if (mcopy) {
376 					icmp6_error(mcopy, ICMP6_DST_UNREACH,
377 					    ICMP6_DST_UNREACH_NOROUTE, 0);
378 				}
379 				m_freem(m);
380 				return;
381 			}
382 		}
383 	} else {
384 		union {
385 			struct sockaddr		dst;
386 			struct sockaddr_in6	dst6;
387 		} u;
388 
389 		sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
390 		if (rtcache_lookup(&ip6_forward_rt, &u.dst) == NULL) {
391 			ip6stat.ip6s_noroute++;
392 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
393 			if (mcopy) {
394 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
395 					    ICMP6_DST_UNREACH_NOROUTE, 0);
396 			}
397 			m_freem(m);
398 			return;
399 		}
400 	}
401 	dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
402 	rt = ip6_forward_rt.ro_rt;
403 #ifdef IPSEC
404     skip_routing:;
405 #endif /* IPSEC */
406 
407 	/*
408 	 * Source scope check: if a packet can't be delivered to its
409 	 * destination for the reason that the destination is beyond the scope
410 	 * of the source address, discard the packet and return an icmp6
411 	 * destination unreachable error with Code 2 (beyond scope of source
412 	 * address).  We use a local copy of ip6_src, since in6_setscope()
413 	 * will possibly modify its first argument.
414 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
415 	 */
416 	src_in6 = ip6->ip6_src;
417 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
418 		/* XXX: this should not happen */
419 		ip6stat.ip6s_cantforward++;
420 		ip6stat.ip6s_badscope++;
421 		m_freem(m);
422 		return;
423 	}
424 	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
425 		ip6stat.ip6s_cantforward++;
426 		ip6stat.ip6s_badscope++;
427 		m_freem(m);
428 		return;
429 	}
430 	if (inzone != outzone
431 #ifdef IPSEC
432 	    && !ipsecrt
433 #endif
434 	    ) {
435 		ip6stat.ip6s_cantforward++;
436 		ip6stat.ip6s_badscope++;
437 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
438 
439 		if (ip6_log_time + ip6_log_interval < time_second) {
440 			ip6_log_time = time_second;
441 			log(LOG_DEBUG,
442 			    "cannot forward "
443 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
444 			    ip6_sprintf(&ip6->ip6_src),
445 			    ip6_sprintf(&ip6->ip6_dst),
446 			    ip6->ip6_nxt,
447 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
448 		}
449 		if (mcopy)
450 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
451 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
452 		m_freem(m);
453 		return;
454 	}
455 #ifdef FAST_IPSEC
456     /*
457      * If we need to encapsulate the packet, do it here
458      * ipsec6_proces_packet will send the packet using ip6_output
459      */
460 	if (needipsec) {
461 		s = splsoftnet();
462 		error = ipsec6_process_packet(m,sp->req);
463 		splx(s);
464 		if (mcopy)
465 			goto freecopy;
466     }
467 #endif
468 
469 
470 
471 	/*
472 	 * Destination scope check: if a packet is going to break the scope
473 	 * zone of packet's destination address, discard it.  This case should
474 	 * usually be prevented by appropriately-configured routing table, but
475 	 * we need an explicit check because we may mistakenly forward the
476 	 * packet to a different zone by (e.g.) a default route.
477 	 */
478 	dst_in6 = ip6->ip6_dst;
479 	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
480 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
481 	    inzone != outzone) {
482 		ip6stat.ip6s_cantforward++;
483 		ip6stat.ip6s_badscope++;
484 		m_freem(m);
485 		return;
486 	}
487 
488 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
489 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
490 		if (mcopy) {
491 			u_long mtu;
492 #ifdef IPSEC
493 			struct secpolicy *xsp;
494 			int ipsecerror;
495 			size_t ipsechdrsiz;
496 #endif
497 
498 			mtu = IN6_LINKMTU(rt->rt_ifp);
499 #ifdef IPSEC
500 			/*
501 			 * When we do IPsec tunnel ingress, we need to play
502 			 * with the link value (decrement IPsec header size
503 			 * from mtu value).  The code is much simpler than v4
504 			 * case, as we have the outgoing interface for
505 			 * encapsulated packet as "rt->rt_ifp".
506 			 */
507 			xsp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
508 				IP_FORWARDING, &ipsecerror);
509 			if (xsp) {
510 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
511 					IPSEC_DIR_OUTBOUND, NULL);
512 				if (ipsechdrsiz < mtu)
513 					mtu -= ipsechdrsiz;
514 			}
515 
516 			/*
517 			 * if mtu becomes less than minimum MTU,
518 			 * tell minimum MTU (and I'll need to fragment it).
519 			 */
520 			if (mtu < IPV6_MMTU)
521 				mtu = IPV6_MMTU;
522 #endif
523 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
524 		}
525 		m_freem(m);
526 		return;
527 	}
528 
529 	if (rt->rt_flags & RTF_GATEWAY)
530 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
531 
532 	/*
533 	 * If we are to forward the packet using the same interface
534 	 * as one we got the packet from, perhaps we should send a redirect
535 	 * to sender to shortcut a hop.
536 	 * Only send redirect if source is sending directly to us,
537 	 * and if packet was not source routed (or has any options).
538 	 * Also, don't send redirect if forwarding using a route
539 	 * modified by a redirect.
540 	 */
541 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
542 #ifdef IPSEC
543 	    !ipsecrt &&
544 #endif
545 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
546 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
547 		    nd6_is_addr_neighbor(
548 		        satocsin6(rtcache_getdst(&ip6_forward_rt)),
549 			rt->rt_ifp)) {
550 			/*
551 			 * If the incoming interface is equal to the outgoing
552 			 * one, the link attached to the interface is
553 			 * point-to-point, and the IPv6 destination is
554 			 * regarded as on-link on the link, then it will be
555 			 * highly probable that the destination address does
556 			 * not exist on the link and that the packet is going
557 			 * to loop.  Thus, we immediately drop the packet and
558 			 * send an ICMPv6 error message.
559 			 * For other routing loops, we dare to let the packet
560 			 * go to the loop, so that a remote diagnosing host
561 			 * can detect the loop by traceroute.
562 			 * type/code is based on suggestion by Rich Draves.
563 			 * not sure if it is the best pick.
564 			 */
565 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
566 				    ICMP6_DST_UNREACH_ADDR, 0);
567 			m_freem(m);
568 			return;
569 		}
570 		type = ND_REDIRECT;
571 	}
572 
573 	/*
574 	 * Fake scoped addresses. Note that even link-local source or
575 	 * destinaion can appear, if the originating node just sends the
576 	 * packet to us (without address resolution for the destination).
577 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
578 	 * link identifiers, we can do this stuff after making a copy for
579 	 * returning an error.
580 	 */
581 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
582 		/*
583 		 * See corresponding comments in ip6_output.
584 		 * XXX: but is it possible that ip6_forward() sends a packet
585 		 *      to a loopback interface? I don't think so, and thus
586 		 *      I bark here. (jinmei@kame.net)
587 		 * XXX: it is common to route invalid packets to loopback.
588 		 *	also, the codepath will be visited on use of ::1 in
589 		 *	rthdr. (itojun)
590 		 */
591 #if 1
592 		if (0)
593 #else
594 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
595 #endif
596 		{
597 			printf("ip6_forward: outgoing interface is loopback. "
598 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
599 			       ip6_sprintf(&ip6->ip6_src),
600 			       ip6_sprintf(&ip6->ip6_dst),
601 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
602 			       if_name(rt->rt_ifp));
603 		}
604 
605 		/* we can just use rcvif in forwarding. */
606 		origifp = m->m_pkthdr.rcvif;
607 	}
608 	else
609 		origifp = rt->rt_ifp;
610 	/*
611 	 * clear embedded scope identifiers if necessary.
612 	 * in6_clearscope will touch the addresses only when necessary.
613 	 */
614 	in6_clearscope(&ip6->ip6_src);
615 	in6_clearscope(&ip6->ip6_dst);
616 
617 #ifdef PFIL_HOOKS
618 	/*
619 	 * Run through list of hooks for output packets.
620 	 */
621 	if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
622 	    PFIL_OUT)) != 0)
623 		goto senderr;
624 	if (m == NULL)
625 		goto freecopy;
626 	ip6 = mtod(m, struct ip6_hdr *);
627 #endif /* PFIL_HOOKS */
628 
629 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
630 	if (error) {
631 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
632 		ip6stat.ip6s_cantforward++;
633 	} else {
634 		ip6stat.ip6s_forward++;
635 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
636 		if (type)
637 			ip6stat.ip6s_redirectsent++;
638 		else {
639 #ifdef GATEWAY
640 			if (m->m_flags & M_CANFASTFWD)
641 				ip6flow_create(&ip6_forward_rt, m);
642 #endif
643 			if (mcopy)
644 				goto freecopy;
645 		}
646 	}
647 
648 #ifdef PFIL_HOOKS
649  senderr:
650 #endif
651 	if (mcopy == NULL)
652 		return;
653 	switch (error) {
654 	case 0:
655 		if (type == ND_REDIRECT) {
656 			icmp6_redirect_output(mcopy, rt);
657 			return;
658 		}
659 		goto freecopy;
660 
661 	case EMSGSIZE:
662 		/* xxx MTU is constant in PPP? */
663 		goto freecopy;
664 
665 	case ENOBUFS:
666 		/* Tell source to slow down like source quench in IP? */
667 		goto freecopy;
668 
669 	case ENETUNREACH:	/* shouldn't happen, checked above */
670 	case EHOSTUNREACH:
671 	case ENETDOWN:
672 	case EHOSTDOWN:
673 	default:
674 		type = ICMP6_DST_UNREACH;
675 		code = ICMP6_DST_UNREACH_ADDR;
676 		break;
677 	}
678 	icmp6_error(mcopy, type, code, 0);
679 	return;
680 
681  freecopy:
682 	m_freem(mcopy);
683 	return;
684 }
685