xref: /xnu-8792.61.2/bsd/netinet6/ip6_forward.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 2009-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*	$FreeBSD: src/sys/netinet6/ip6_forward.c,v 1.16 2002/10/16 02:25:05 sam Exp $	*/
30 /*	$KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $	*/
31 
32 /*
33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the project nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
65 #include <sys/mbuf.h>
66 #include <sys/domain.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
70 #include <sys/time.h>
71 #include <sys/kernel.h>
72 #include <sys/syslog.h>
73 
74 #include <net/if.h>
75 #include <net/route.h>
76 
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/icmp6.h>
86 #include <netinet6/nd6.h>
87 #include <netinet6/scope6_var.h>
88 
89 #include <netinet/in_pcb.h>
90 
91 #if IPSEC
92 #include <netinet6/ipsec.h>
93 #include <netinet6/ipsec6.h>
94 #include <netkey/key.h>
95 extern int ipsec_bypass;
96 #endif /* IPSEC */
97 
98 #include <net/net_osdep.h>
99 
100 #if DUMMYNET
101 #include <netinet/ip_dummynet.h>
102 #endif /* DUMMYNET */
103 
104 #if PF
105 #include <net/pfvar.h>
106 static void
adjust_scope_and_pktlen(struct mbuf * m,unsigned int * ifscope_p,uint32_t * mpktlen_p)107 adjust_scope_and_pktlen(struct mbuf *m,
108     unsigned int *ifscope_p, uint32_t *mpktlen_p)
109 {
110 	struct pf_mtag *pf_mtag;
111 	struct pf_fragment_tag *pf_ftagp;
112 
113 	pf_mtag = pf_find_mtag(m);
114 	ASSERT(pf_mtag != NULL);
115 	if (pf_mtag->pftag_rtableid != IFSCOPE_NONE) {
116 		*ifscope_p = pf_mtag->pftag_rtableid;
117 	}
118 	pf_ftagp = pf_find_fragment_tag(m);
119 	if (pf_ftagp != NULL) {
120 		ASSERT(pf_mtag->pftag_flags & PF_TAG_REASSEMBLED);
121 		*mpktlen_p = pf_ftagp->ft_maxlen;
122 		ASSERT(*mpktlen_p);
123 	}
124 }
125 
126 #endif /* PF */
127 
128 /*
129  * Forward a packet.  If some error occurs return the sender
130  * an icmp packet.  Note we can't always generate a meaningful
131  * icmp message because icmp doesn't have a large enough repertoire
132  * of codes and types.
133  *
134  * If not forwarding, just drop the packet.  This could be confusing
135  * if ipforwarding was zero but some routing protocol was advancing
136  * us as a gateway to somewhere.  However, we must let the routing
137  * protocol deal with that.
138  *
139  */
140 
141 struct mbuf *
ip6_forward(struct mbuf * m,struct route_in6 * ip6forward_rt,int srcrt)142 ip6_forward(struct mbuf *m, struct route_in6 *ip6forward_rt,
143     int srcrt)
144 {
145 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
146 	struct sockaddr_in6 *dst;
147 	struct rtentry *rt;
148 	int error, type = 0, code = 0;
149 	boolean_t proxy = FALSE;
150 	struct mbuf *mcopy = NULL;
151 	struct ifnet *ifp, *rcvifp, *origifp;   /* maybe unnecessary */
152 	u_int32_t inzone, outzone, len = 0, pktcnt = 0;
153 	struct in6_addr src_in6, dst_in6;
154 	uint64_t curtime = net_uptime();
155 #if IPSEC
156 	struct secpolicy *sp = NULL;
157 #endif
158 	unsigned int ifscope = IFSCOPE_NONE;
159 	uint32_t mpktlen = 0;
160 
161 	/*
162 	 * In the prefix proxying case, the route to the proxied node normally
163 	 * gets created by nd6_prproxy_ns_output(), as part of forwarding a
164 	 * NS (NUD/AR) packet to the proxied node.  In the event that such
165 	 * packet did not arrive in time before the correct route gets created,
166 	 * ip6_input() would have performed a rtalloc() which most likely will
167 	 * create the wrong cloned route; this route points back to the same
168 	 * interface as the inbound interface, since the parent non-scoped
169 	 * prefix route points there.  Therefore we check if that is the case
170 	 * and perform the necessary fixup to get the correct route installed.
171 	 */
172 	if (!srcrt && nd6_prproxy &&
173 	    (rt = ip6forward_rt->ro_rt) != NULL && (rt->rt_flags & RTF_PROXY)) {
174 		nd6_proxy_find_fwdroute(m->m_pkthdr.rcvif, ip6forward_rt);
175 		if ((rt = ip6forward_rt->ro_rt) != NULL) {
176 			ifscope = rt->rt_ifp->if_index;
177 		}
178 	}
179 
180 #if PF
181 	adjust_scope_and_pktlen(m, &ifscope, &mpktlen);
182 
183 	/*
184 	 * If the caller provides a route which is on a different interface
185 	 * than the one specified for scoped forwarding, discard the route
186 	 * and do a lookup below.
187 	 */
188 	if (ifscope != IFSCOPE_NONE && (rt = ip6forward_rt->ro_rt) != NULL) {
189 		RT_LOCK(rt);
190 		if (rt->rt_ifp->if_index != ifscope) {
191 			RT_UNLOCK(rt);
192 			ROUTE_RELEASE(ip6forward_rt);
193 			rt = NULL;
194 		} else {
195 			RT_UNLOCK(rt);
196 		}
197 	}
198 #endif /* PF */
199 
200 #if IPSEC
201 	/*
202 	 * Check AH/ESP integrity.
203 	 */
204 	/*
205 	 * Don't increment ip6s_cantforward because this is the check
206 	 * before forwarding packet actually.
207 	 */
208 	if (ipsec_bypass == 0) {
209 		if (ipsec6_in_reject(m, NULL)) {
210 			IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
211 			m_freem(m);
212 			return NULL;
213 		}
214 	}
215 #endif /*IPSEC*/
216 
217 	/*
218 	 * Do not forward packets to multicast destination.
219 	 * Do not forward packets with unspecified source.  It was discussed
220 	 * in July 2000, on ipngwg mailing list.
221 	 */
222 	if ((m->m_flags & (M_BCAST | M_MCAST)) != 0 ||
223 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
224 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
225 		ip6stat.ip6s_cantforward++;
226 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
227 		if (ip6_log_time + ip6_log_interval < curtime) {
228 			ip6_log_time = curtime;
229 			log(LOG_DEBUG,
230 			    "cannot forward "
231 			    "from %s to %s nxt %d received on %s\n",
232 			    ip6_sprintf(&ip6->ip6_src),
233 			    ip6_sprintf(&ip6->ip6_dst),
234 			    ip6->ip6_nxt,
235 			    if_name(m->m_pkthdr.rcvif));
236 		}
237 		m_freem(m);
238 		return NULL;
239 	}
240 
241 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
242 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
243 		icmp6_error_flag(m, ICMP6_TIME_EXCEEDED,
244 		    ICMP6_TIME_EXCEED_TRANSIT, 0, 0);
245 		return NULL;
246 	}
247 
248 	/*
249 	 * See if the destination is a proxied address, and if so pretend
250 	 * that it's for us.  This is mostly to handle NUD probes against
251 	 * the proxied addresses.  We filter for ICMPv6 here and will let
252 	 * icmp6_input handle the rest.
253 	 */
254 	if (!srcrt && nd6_prproxy) {
255 		VERIFY(!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst));
256 		proxy = nd6_prproxy_isours(m, ip6, ip6forward_rt, ifscope);
257 		/*
258 		 * Don't update hop limit while proxying; RFC 4389 4.1.
259 		 * Also skip IPsec forwarding path processing as this
260 		 * packet is not to be forwarded.
261 		 */
262 		if (proxy) {
263 			goto skip_ipsec;
264 		}
265 	}
266 
267 	ip6->ip6_hlim -= IPV6_HLIMDEC;
268 
269 	/*
270 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
271 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
272 	 * we need to generate an ICMP6 message to the src.
273 	 * Thanks to M_EXT, in most cases copy will not occur.
274 	 *
275 	 * It is important to save it before IPsec processing as IPsec
276 	 * processing may modify the mbuf.
277 	 */
278 	mcopy = m_copym_mode(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
279 	    M_DONTWAIT, M_COPYM_COPY_HDR);
280 #if IPSEC
281 	if (ipsec_bypass != 0) {
282 		goto skip_ipsec;
283 	}
284 	/* get a security policy for this packet */
285 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
286 	    &error);
287 	if (sp == NULL) {
288 		IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
289 		ip6stat.ip6s_cantforward++;
290 		if (mcopy) {
291 #if 0
292 			/* XXX: what icmp ? */
293 #else
294 			m_freem(mcopy);
295 #endif
296 		}
297 		m_freem(m);
298 		return NULL;
299 	}
300 
301 	error = 0;
302 
303 	/* check policy */
304 	switch (sp->policy) {
305 	case IPSEC_POLICY_DISCARD:
306 	case IPSEC_POLICY_GENERATE:
307 		/*
308 		 * This packet is just discarded.
309 		 */
310 		IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
311 		ip6stat.ip6s_cantforward++;
312 		key_freesp(sp, KEY_SADB_UNLOCKED);
313 		if (mcopy) {
314 #if 0
315 			/* XXX: what icmp ? */
316 #else
317 			m_freem(mcopy);
318 #endif
319 		}
320 		m_freem(m);
321 		return NULL;
322 
323 	case IPSEC_POLICY_BYPASS:
324 	case IPSEC_POLICY_NONE:
325 		/* no need to do IPsec. */
326 		key_freesp(sp, KEY_SADB_UNLOCKED);
327 		goto skip_ipsec;
328 
329 	case IPSEC_POLICY_IPSEC:
330 		if (sp->req == NULL) {
331 			/* XXX should be panic ? */
332 			printf("ip6_forward: No IPsec request specified.\n");
333 			ip6stat.ip6s_cantforward++;
334 			key_freesp(sp, KEY_SADB_UNLOCKED);
335 			if (mcopy) {
336 #if 0
337 				/* XXX: what icmp ? */
338 #else
339 				m_freem(mcopy);
340 #endif
341 			}
342 			m_freem(m);
343 			return NULL;
344 		}
345 		/* do IPsec */
346 		break;
347 
348 	case IPSEC_POLICY_ENTRUST:
349 	default:
350 		/* should be panic ?? */
351 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
352 		key_freesp(sp, KEY_SADB_UNLOCKED);
353 		goto skip_ipsec;
354 	}
355 
356 	{
357 		struct ipsec_output_state state;
358 
359 		/*
360 		 * All the extension headers will become inaccessible
361 		 * (since they can be encrypted).
362 		 * Don't panic, we need no more updates to extension headers
363 		 * on inner IPv6 packet (since they are now encapsulated).
364 		 *
365 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
366 		 */
367 		bzero(&state, sizeof(state));
368 		state.m = m;
369 		state.dst = NULL; /* update at ipsec6_output_tunnel() */
370 
371 		error = ipsec6_output_tunnel(&state, sp, 0);
372 		key_freesp(sp, KEY_SADB_UNLOCKED);
373 		if (state.tunneled == 4) {
374 			ROUTE_RELEASE(&state.ro);
375 			return NULL; /* packet is gone - sent over IPv4 */
376 		}
377 
378 		m = state.m;
379 		ROUTE_RELEASE(&state.ro);
380 
381 		if (error) {
382 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
383 			switch (error) {
384 			case EHOSTUNREACH:
385 			case ENETUNREACH:
386 			case EMSGSIZE:
387 			case ENOBUFS:
388 			case ENOMEM:
389 				break;
390 			default:
391 				printf("ip6_output (ipsec): error code %d\n", error);
392 				OS_FALLTHROUGH;
393 			case ENOENT:
394 				/* don't show these error codes to the user */
395 				break;
396 			}
397 			ip6stat.ip6s_cantforward++;
398 			if (mcopy) {
399 #if 0
400 				/* XXX: what icmp ? */
401 #else
402 				m_freem(mcopy);
403 #endif
404 			}
405 			m_freem(m);
406 			return NULL;
407 		}
408 	}
409 #endif /* IPSEC */
410 skip_ipsec:
411 
412 	dst = (struct sockaddr_in6 *)&ip6forward_rt->ro_dst;
413 	if ((rt = ip6forward_rt->ro_rt) != NULL) {
414 		RT_LOCK(rt);
415 		/* Take an extra ref for ourselves */
416 		RT_ADDREF_LOCKED(rt);
417 	}
418 
419 	VERIFY(rt == NULL || rt == ip6forward_rt->ro_rt);
420 	if (!srcrt) {
421 		/*
422 		 * ip6forward_rt->ro_dst.sin6_addr is equal to ip6->ip6_dst
423 		 */
424 		if (ROUTE_UNUSABLE(ip6forward_rt)) {
425 			if (rt != NULL) {
426 				/* Release extra ref */
427 				RT_REMREF_LOCKED(rt);
428 				RT_UNLOCK(rt);
429 			}
430 			ROUTE_RELEASE(ip6forward_rt);
431 
432 			/* this probably fails but give it a try again */
433 			rtalloc_scoped_ign((struct route *)ip6forward_rt,
434 			    RTF_PRCLONING, ifscope);
435 			if ((rt = ip6forward_rt->ro_rt) != NULL) {
436 				RT_LOCK(rt);
437 				/* Take an extra ref for ourselves */
438 				RT_ADDREF_LOCKED(rt);
439 			}
440 		}
441 
442 		if (rt == NULL) {
443 			ip6stat.ip6s_noroute++;
444 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
445 			if (mcopy) {
446 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
447 				    ICMP6_DST_UNREACH_NOROUTE, 0);
448 			}
449 			m_freem(m);
450 			return NULL;
451 		}
452 		RT_LOCK_ASSERT_HELD(rt);
453 	} else if (ROUTE_UNUSABLE(ip6forward_rt) ||
454 	    !in6_are_addr_equal_scoped(&ip6->ip6_dst, &dst->sin6_addr, ip6_input_getdstifscope(m), dst->sin6_scope_id)) {
455 		if (rt != NULL) {
456 			/* Release extra ref */
457 			RT_REMREF_LOCKED(rt);
458 			RT_UNLOCK(rt);
459 		}
460 		ROUTE_RELEASE(ip6forward_rt);
461 
462 		bzero(dst, sizeof(*dst));
463 		dst->sin6_len = sizeof(struct sockaddr_in6);
464 		dst->sin6_family = AF_INET6;
465 		dst->sin6_addr = ip6->ip6_dst;
466 
467 		rtalloc_scoped_ign((struct route *)ip6forward_rt,
468 		    RTF_PRCLONING, ifscope);
469 		if ((rt = ip6forward_rt->ro_rt) == NULL) {
470 			ip6stat.ip6s_noroute++;
471 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
472 			if (mcopy) {
473 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
474 				    ICMP6_DST_UNREACH_NOROUTE, 0);
475 			}
476 			m_freem(m);
477 			return NULL;
478 		}
479 		RT_LOCK(rt);
480 		/* Take an extra ref for ourselves */
481 		RT_ADDREF_LOCKED(rt);
482 	}
483 
484 	/*
485 	 * Source scope check: if a packet can't be delivered to its
486 	 * destination for the reason that the destination is beyond the scope
487 	 * of the source address, discard the packet and return an icmp6
488 	 * destination unreachable error with Code 2 (beyond scope of source
489 	 * address) unless we are proxying (source address is link local
490 	 * for NUDs.)  We use a local copy of ip6_src, since in6_setscope()
491 	 * will possibly modify its first argument.
492 	 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
493 	 */
494 	src_in6 = ip6->ip6_src;
495 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
496 		RT_REMREF_LOCKED(rt);
497 		RT_UNLOCK(rt);
498 		/* XXX: this should not happen */
499 		ip6stat.ip6s_cantforward++;
500 		ip6stat.ip6s_badscope++;
501 		m_freem(m);
502 		return NULL;
503 	}
504 	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
505 		RT_REMREF_LOCKED(rt);
506 		RT_UNLOCK(rt);
507 		ip6stat.ip6s_cantforward++;
508 		ip6stat.ip6s_badscope++;
509 		m_freem(m);
510 		return NULL;
511 	}
512 
513 	if (inzone != outzone && !proxy) {
514 		ip6stat.ip6s_cantforward++;
515 		ip6stat.ip6s_badscope++;
516 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
517 
518 		if (ip6_log_time + ip6_log_interval < curtime) {
519 			ip6_log_time = curtime;
520 			log(LOG_DEBUG,
521 			    "cannot forward "
522 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
523 			    ip6_sprintf(&ip6->ip6_src),
524 			    ip6_sprintf(&ip6->ip6_dst),
525 			    ip6->ip6_nxt,
526 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
527 		}
528 		/* Release extra ref */
529 		RT_REMREF_LOCKED(rt);
530 		RT_UNLOCK(rt);
531 		if (mcopy) {
532 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
533 			    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
534 		}
535 		m_freem(m);
536 		return NULL;
537 	}
538 
539 	/*
540 	 * Destination scope check: if a packet is going to break the scope
541 	 * zone of packet's destination address, discard it.  This case should
542 	 * usually be prevented by appropriately-configured routing table, but
543 	 * we need an explicit check because we may mistakenly forward the
544 	 * packet to a different zone by (e.g.) a default route.
545 	 */
546 	dst_in6 = ip6->ip6_dst;
547 	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
548 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
549 	    inzone != outzone) {
550 		RT_REMREF_LOCKED(rt);
551 		RT_UNLOCK(rt);
552 		ip6stat.ip6s_cantforward++;
553 		ip6stat.ip6s_badscope++;
554 		m_freem(m);
555 		return NULL;
556 	}
557 
558 	if (mpktlen == 0) {
559 		mpktlen = m->m_pkthdr.len;
560 	}
561 
562 	if (mpktlen > rt->rt_ifp->if_mtu) {
563 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
564 		if (mcopy) {
565 			uint32_t mtu;
566 #if IPSEC
567 			struct secpolicy *sp2;
568 			int ipsecerror;
569 			size_t ipsechdrsiz;
570 #endif
571 
572 			mtu = rt->rt_ifp->if_mtu;
573 #if IPSEC
574 			/*
575 			 * When we do IPsec tunnel ingress, we need to play
576 			 * with the link value (decrement IPsec header size
577 			 * from mtu value).  The code is much simpler than v4
578 			 * case, as we have the outgoing interface for
579 			 * encapsulated packet as "rt->rt_ifp".
580 			 */
581 			sp2 = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
582 			    IP_FORWARDING, &ipsecerror);
583 			if (sp2) {
584 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
585 				    IPSEC_DIR_OUTBOUND, NULL);
586 				if (ipsechdrsiz < mtu) {
587 					mtu -= ipsechdrsiz;
588 				}
589 				key_freesp(sp2, KEY_SADB_UNLOCKED);
590 			}
591 			/*
592 			 * if mtu becomes less than minimum MTU,
593 			 * tell minimum MTU (and I'll need to fragment it).
594 			 */
595 			if (mtu < IPV6_MMTU) {
596 				mtu = IPV6_MMTU;
597 			}
598 #endif
599 			/* Release extra ref */
600 			RT_REMREF_LOCKED(rt);
601 			RT_UNLOCK(rt);
602 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
603 		} else {
604 			/* Release extra ref */
605 			RT_REMREF_LOCKED(rt);
606 			RT_UNLOCK(rt);
607 		}
608 		m_freem(m);
609 		return NULL;
610 	}
611 
612 	if (rt->rt_flags & RTF_GATEWAY) {
613 		dst = (struct sockaddr_in6 *)(void *)rt->rt_gateway;
614 	}
615 
616 	/*
617 	 * If we are to forward the packet using the same interface
618 	 * as one we got the packet from, perhaps we should send a redirect
619 	 * to sender to shortcut a hop.
620 	 * Only send redirect if source is sending directly to us,
621 	 * and if packet was not source routed (or has any options).
622 	 * Also, don't send redirect if forwarding using a route
623 	 * modified by a redirect.
624 	 */
625 	if (!proxy &&
626 	    ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
627 	    (rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) == 0) {
628 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
629 			/*
630 			 * If the incoming interface is equal to the outgoing
631 			 * one, and the link attached to the interface is
632 			 * point-to-point, then it will be highly probable
633 			 * that a routing loop occurs. Thus, we immediately
634 			 * drop the packet and send an ICMPv6 error message.
635 			 *
636 			 * type/code is based on suggestion by Rich Draves.
637 			 * not sure if it is the best pick.
638 			 */
639 			RT_REMREF_LOCKED(rt);   /* Release extra ref */
640 			RT_UNLOCK(rt);
641 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
642 			    ICMP6_DST_UNREACH_ADDR, 0);
643 			m_freem(m);
644 			return NULL;
645 		}
646 		type = ND_REDIRECT;
647 	}
648 	/*
649 	 * Fake scoped addresses. Note that even link-local source or
650 	 * destinaion can appear, if the originating node just sends the
651 	 * packet to us (without address resolution for the destination).
652 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
653 	 * link identifiers, we can do this stuff after making a copy for
654 	 * returning an error.
655 	 */
656 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
657 		/*
658 		 * See corresponding comments in ip6_output.
659 		 * XXX: but is it possible that ip6_forward() sends a packet
660 		 *      to a loopback interface? I don't think so, and thus
661 		 *      I bark here. ([email protected])
662 		 * XXX: it is common to route invalid packets to loopback.
663 		 *	also, the codepath will be visited on use of ::1 in
664 		 *	rthdr. (itojun)
665 		 */
666 #if 1
667 		if ((0))
668 #else
669 		if ((rt->rt_flags & (RTF_BLACKHOLE | RTF_REJECT)) == 0)
670 #endif
671 		{
672 			printf("ip6_forward: outgoing interface is loopback. "
673 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
674 			    ip6_sprintf(&ip6->ip6_src),
675 			    ip6_sprintf(&ip6->ip6_dst),
676 			    ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
677 			    if_name(rt->rt_ifp));
678 		}
679 
680 		/* we can just use rcvif in forwarding. */
681 		origifp = rcvifp = m->m_pkthdr.rcvif;
682 	} else if (nd6_prproxy) {
683 		/*
684 		 * In the prefix proxying case, we need to inform nd6_output()
685 		 * about the inbound interface, so that any subsequent NS
686 		 * packets generated by nd6_prproxy_ns_output() will not be
687 		 * sent back to that same interface.
688 		 */
689 		origifp = rcvifp = m->m_pkthdr.rcvif;
690 	} else {
691 		rcvifp = m->m_pkthdr.rcvif;
692 		origifp = rt->rt_ifp;
693 	}
694 	/*
695 	 * clear embedded scope identifiers if necessary.
696 	 * in6_clearscope will touch the addresses only when necessary.
697 	 */
698 	in6_clearscope(&ip6->ip6_src);
699 	in6_clearscope(&ip6->ip6_dst);
700 
701 	ifp = rt->rt_ifp;
702 	/* Drop the lock but retain the extra ref */
703 	RT_UNLOCK(rt);
704 
705 	/*
706 	 * If this is to be processed locally, let ip6_input have it.
707 	 */
708 	if (proxy) {
709 		VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST);
710 		/* Release extra ref */
711 		RT_REMREF(rt);
712 		if (mcopy != NULL) {
713 			m_freem(mcopy);
714 		}
715 		return m;
716 	}
717 
718 	/* Mark this packet as being forwarded from another interface */
719 	m->m_pkthdr.pkt_flags |= PKTF_FORWARDED;
720 
721 #if PF
722 	if (PF_IS_ENABLED) {
723 		/*
724 		 * PF refragments any packet which it reassembled due to scrub
725 		 * rules, in which case it will set the PF_TAG_REFRAGMENTED
726 		 * flag in PF mbuf tag.
727 		 */
728 #if DUMMYNET
729 		struct ip_fw_args args;
730 		struct pf_mtag *pf_mtag;
731 
732 		bzero(&args, sizeof(args));
733 
734 		args.fwa_oif = ifp;
735 		args.fwa_oflags = 0;
736 		args.fwa_ro6 = ip6forward_rt;
737 		args.fwa_ro6_pmtu = ip6forward_rt;
738 		args.fwa_mtu = rt->rt_ifp->if_mtu;
739 		args.fwa_dst6 = dst;
740 		args.fwa_origifp = origifp;
741 		/* Invoke outbound packet filter */
742 		error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, &args);
743 #else /* !DUMMYNET */
744 		error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, NULL);
745 #endif /* !DUMMYNET */
746 		if (error != 0 || m == NULL) {
747 			if (m != NULL) {
748 				panic("%s: unexpected packet %p", __func__, m);
749 				/* NOTREACHED */
750 			}
751 			/* Already freed by callee */
752 			goto senderr;
753 		}
754 
755 		pf_mtag = pf_find_mtag(m);
756 		/*
757 		 * refragmented packets from PF.
758 		 */
759 		if ((pf_mtag->pftag_flags & PF_TAG_REFRAGMENTED) != 0) {
760 			struct mbuf *t;
761 
762 			pf_mtag->pftag_flags &= ~PF_TAG_REFRAGMENTED;
763 			/* for statistics */
764 			t = m;
765 			while (t != NULL) {
766 				pktcnt++;
767 				len += m_pktlen(t);
768 				t = t->m_nextpkt;
769 			}
770 
771 			/*
772 			 * nd6_output() frees packetchain in both success and
773 			 * failure cases.
774 			 */
775 			error = nd6_output(ifp, origifp, m, dst, rt, NULL);
776 			m = NULL;
777 			goto sent;
778 		}
779 		/*
780 		 * We do not use ip6 header again in the code below,
781 		 * however still adding the bit here so that any new
782 		 * code in future doesn't end up working with the
783 		 * wrong pointer
784 		 */
785 		ip6 = mtod(m, struct ip6_hdr *);
786 	}
787 #endif /* PF */
788 
789 	len = m_pktlen(m);
790 	pktcnt = 1;
791 	error = nd6_output(ifp, origifp, m, dst, rt, NULL);
792 sent:
793 	if (error) {
794 		in6_ifstat_add(ifp, ifs6_out_discard, pktcnt);
795 		ip6stat.ip6s_cantforward += pktcnt;
796 	} else {
797 		/*
798 		 * Increment stats on the source interface; the ones
799 		 * for destination interface has been taken care of
800 		 * during output above by virtue of PKTF_FORWARDED.
801 		 */
802 		rcvifp->if_fpackets += pktcnt;
803 		rcvifp->if_fbytes += len;
804 
805 		ip6stat.ip6s_forward += pktcnt;
806 		in6_ifstat_add(ifp, ifs6_out_forward, pktcnt);
807 		if (type) {
808 			ip6stat.ip6s_redirectsent++;
809 		} else {
810 			if (mcopy) {
811 				goto freecopy;
812 			}
813 		}
814 	}
815 #if PF
816 senderr:
817 #endif /* PF */
818 	if (mcopy == NULL) {
819 		/* Release extra ref */
820 		RT_REMREF(rt);
821 		return NULL;
822 	}
823 	switch (error) {
824 	case 0:
825 #if 1
826 		if (type == ND_REDIRECT) {
827 			icmp6_redirect_output(mcopy, rt);
828 			/* Release extra ref */
829 			RT_REMREF(rt);
830 			return NULL;
831 		}
832 #endif
833 		goto freecopy;
834 
835 	case EMSGSIZE:
836 		/* xxx MTU is constant in PPP? */
837 		goto freecopy;
838 
839 	case ENOBUFS:
840 		/* Tell source to slow down like source quench in IP? */
841 		goto freecopy;
842 
843 	case ENETUNREACH:       /* shouldn't happen, checked above */
844 	case EHOSTUNREACH:
845 	case ENETDOWN:
846 	case EHOSTDOWN:
847 	default:
848 		type = ICMP6_DST_UNREACH;
849 		code = ICMP6_DST_UNREACH_ADDR;
850 		break;
851 	}
852 	icmp6_error(mcopy, type, code, 0);
853 	/* Release extra ref */
854 	RT_REMREF(rt);
855 	return NULL;
856 
857 freecopy:
858 	m_freem(mcopy);
859 	/* Release extra ref */
860 	RT_REMREF(rt);
861 	return NULL;
862 }
863