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