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>
95extern 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>
106static void
107adjust_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#include <net/sockaddr_utils.h>
129
130/*
131 * Forward a packet. If some error occurs return the sender
132 * an icmp packet. Note we can't always generate a meaningful
133 * icmp message because icmp doesn't have a large enough repertoire
134 * of codes and types.
135 *
136 * If not forwarding, just drop the packet. This could be confusing
137 * if ipforwarding was zero but some routing protocol was advancing
138 * us as a gateway to somewhere. However, we must let the routing
139 * protocol deal with that.
140 *
141 */
142
143struct mbuf *
144ip6_forward(struct mbuf *m, struct route_in6 *ip6forward_rt,
145 int srcrt)
146{
147 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
148 struct sockaddr_in6 *dst;
149 struct rtentry *rt;
150 int error, type = 0, code = 0;
151 boolean_t proxy = FALSE;
152 struct mbuf *mcopy = NULL;
153 struct ifnet *ifp, *rcvifp, *origifp; /* maybe unnecessary */
154 u_int32_t inzone, outzone, len = 0, pktcnt = 0;
155 struct in6_addr src_in6, dst_in6;
156 uint64_t curtime = net_uptime();
157#if IPSEC
158 struct secpolicy *sp = NULL;
159#endif
160 unsigned int ifscope = IFSCOPE_NONE;
161 uint32_t mpktlen = 0;
162
163 /*
164 * In the prefix proxying case, the route to the proxied node normally
165 * gets created by nd6_prproxy_ns_output(), as part of forwarding a
166 * NS (NUD/AR) packet to the proxied node. In the event that such
167 * packet did not arrive in time before the correct route gets created,
168 * ip6_input() would have performed a rtalloc() which most likely will
169 * create the wrong cloned route; this route points back to the same
170 * interface as the inbound interface, since the parent non-scoped
171 * prefix route points there. Therefore we check if that is the case
172 * and perform the necessary fixup to get the correct route installed.
173 */
174 if (!srcrt && nd6_prproxy &&
175 (rt = ip6forward_rt->ro_rt) != NULL && (rt->rt_flags & RTF_PROXY)) {
176 nd6_proxy_find_fwdroute(m->m_pkthdr.rcvif, ip6forward_rt);
177 if ((rt = ip6forward_rt->ro_rt) != NULL) {
178 ifscope = rt->rt_ifp->if_index;
179 }
180 }
181
182#if PF
183 adjust_scope_and_pktlen(m, ifscope_p: &ifscope, mpktlen_p: &mpktlen);
184
185 /*
186 * If the caller provides a route which is on a different interface
187 * than the one specified for scoped forwarding, discard the route
188 * and do a lookup below.
189 */
190 if (ifscope != IFSCOPE_NONE && (rt = ip6forward_rt->ro_rt) != NULL) {
191 RT_LOCK(rt);
192 if (rt->rt_ifp->if_index != ifscope) {
193 RT_UNLOCK(rt);
194 ROUTE_RELEASE(ip6forward_rt);
195 rt = NULL;
196 } else {
197 RT_UNLOCK(rt);
198 }
199 }
200#endif /* PF */
201
202#if IPSEC
203 /*
204 * Check AH/ESP integrity.
205 */
206 /*
207 * Don't increment ip6s_cantforward because this is the check
208 * before forwarding packet actually.
209 */
210 if (ipsec_bypass == 0) {
211 if (ipsec6_in_reject(m, NULL)) {
212 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
213 m_freem(m);
214 return NULL;
215 }
216 }
217#endif /*IPSEC*/
218
219 /*
220 * Do not forward packets to multicast destination.
221 * Do not forward packets with unspecified source. It was discussed
222 * in July 2000, on ipngwg mailing list.
223 */
224 if ((m->m_flags & (M_BCAST | M_MCAST)) != 0 ||
225 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
226 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
227 ip6stat.ip6s_cantforward++;
228 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
229 if (ip6_log_time + ip6_log_interval < curtime) {
230 ip6_log_time = curtime;
231 log(LOG_DEBUG,
232 "cannot forward "
233 "from %s to %s nxt %d received on %s\n",
234 ip6_sprintf(&ip6->ip6_src),
235 ip6_sprintf(&ip6->ip6_dst),
236 ip6->ip6_nxt,
237 if_name(m->m_pkthdr.rcvif));
238 }
239 m_freem(m);
240 return NULL;
241 }
242
243 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
244 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
245 icmp6_error_flag(m, ICMP6_TIME_EXCEEDED,
246 ICMP6_TIME_EXCEED_TRANSIT, 0, 0);
247 return NULL;
248 }
249
250 /*
251 * See if the destination is a proxied address, and if so pretend
252 * that it's for us. This is mostly to handle NUD probes against
253 * the proxied addresses. We filter for ICMPv6 here and will let
254 * icmp6_input handle the rest.
255 */
256 if (!srcrt && nd6_prproxy) {
257 VERIFY(!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst));
258 proxy = nd6_prproxy_isours(m, ip6, ip6forward_rt, ifscope);
259 /*
260 * Don't update hop limit while proxying; RFC 4389 4.1.
261 * Also skip IPsec forwarding path processing as this
262 * packet is not to be forwarded.
263 */
264 if (proxy) {
265 goto skip_ipsec;
266 }
267 }
268
269 ip6->ip6_hlim -= IPV6_HLIMDEC;
270
271 /*
272 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
273 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
274 * we need to generate an ICMP6 message to the src.
275 * Thanks to M_EXT, in most cases copy will not occur.
276 *
277 * It is important to save it before IPsec processing as IPsec
278 * processing may modify the mbuf.
279 */
280 mcopy = m_copym_mode(m, 0, imin(a: m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
281 M_DONTWAIT, NULL, NULL, M_COPYM_COPY_HDR);
282#if IPSEC
283 if (ipsec_bypass != 0) {
284 goto skip_ipsec;
285 }
286 /* get a security policy for this packet */
287 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
288 &error);
289 if (sp == NULL) {
290 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
291 ip6stat.ip6s_cantforward++;
292 if (mcopy) {
293#if 0
294 /* XXX: what icmp ? */
295#else
296 m_freem(mcopy);
297#endif
298 }
299 m_freem(m);
300 return NULL;
301 }
302
303 error = 0;
304
305 /* check policy */
306 switch (sp->policy) {
307 case IPSEC_POLICY_DISCARD:
308 case IPSEC_POLICY_GENERATE:
309 /*
310 * This packet is just discarded.
311 */
312 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
313 ip6stat.ip6s_cantforward++;
314 key_freesp(sp, KEY_SADB_UNLOCKED);
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 NULL;
324
325 case IPSEC_POLICY_BYPASS:
326 case IPSEC_POLICY_NONE:
327 /* no need to do IPsec. */
328 key_freesp(sp, KEY_SADB_UNLOCKED);
329 goto skip_ipsec;
330
331 case IPSEC_POLICY_IPSEC:
332 if (sp->req == NULL) {
333 /* XXX should be panic ? */
334 printf("ip6_forward: No IPsec request specified.\n");
335 ip6stat.ip6s_cantforward++;
336 key_freesp(sp, KEY_SADB_UNLOCKED);
337 if (mcopy) {
338#if 0
339 /* XXX: what icmp ? */
340#else
341 m_freem(mcopy);
342#endif
343 }
344 m_freem(m);
345 return NULL;
346 }
347 /* do IPsec */
348 break;
349
350 case IPSEC_POLICY_ENTRUST:
351 default:
352 /* should be panic ?? */
353 printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
354 key_freesp(sp, KEY_SADB_UNLOCKED);
355 goto skip_ipsec;
356 }
357
358 {
359 struct ipsec_output_state state;
360
361 /*
362 * All the extension headers will become inaccessible
363 * (since they can be encrypted).
364 * Don't panic, we need no more updates to extension headers
365 * on inner IPv6 packet (since they are now encapsulated).
366 *
367 * IPv6 [ESP|AH] IPv6 [extension headers] payload
368 */
369 bzero(s: &state, n: sizeof(state));
370 state.m = m;
371 state.dst = NULL; /* update at ipsec6_output_tunnel() */
372
373 error = ipsec6_output_tunnel(&state, sp, 0);
374 key_freesp(sp, KEY_SADB_UNLOCKED);
375 if (state.tunneled == 4) {
376 ROUTE_RELEASE(&state.ro);
377 return NULL; /* packet is gone - sent over IPv4 */
378 }
379
380 m = state.m;
381 ROUTE_RELEASE(&state.ro);
382
383 if (error) {
384 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
385 switch (error) {
386 case EHOSTUNREACH:
387 case ENETUNREACH:
388 case EMSGSIZE:
389 case ENOBUFS:
390 case ENOMEM:
391 break;
392 default:
393 printf("ip6_output (ipsec): error code %d\n", error);
394 OS_FALLTHROUGH;
395 case ENOENT:
396 /* don't show these error codes to the user */
397 break;
398 }
399 ip6stat.ip6s_cantforward++;
400 if (mcopy) {
401#if 0
402 /* XXX: what icmp ? */
403#else
404 m_freem(mcopy);
405#endif
406 }
407 m_freem(m);
408 return NULL;
409 }
410 }
411#endif /* IPSEC */
412skip_ipsec:
413
414 dst = SIN6(&ip6forward_rt->ro_dst);
415 if ((rt = ip6forward_rt->ro_rt) != NULL) {
416 RT_LOCK(rt);
417 /* Take an extra ref for ourselves */
418 RT_ADDREF_LOCKED(rt);
419 }
420
421 VERIFY(rt == NULL || rt == ip6forward_rt->ro_rt);
422 if (!srcrt) {
423 /*
424 * ip6forward_rt->ro_dst.sin6_addr is equal to ip6->ip6_dst
425 */
426 if (ROUTE_UNUSABLE(ip6forward_rt)) {
427 if (rt != NULL) {
428 /* Release extra ref */
429 RT_REMREF_LOCKED(rt);
430 RT_UNLOCK(rt);
431 }
432 ROUTE_RELEASE(ip6forward_rt);
433
434 /* this probably fails but give it a try again */
435 rtalloc_scoped_ign((struct route *)ip6forward_rt,
436 RTF_PRCLONING, ifscope);
437 if ((rt = ip6forward_rt->ro_rt) != NULL) {
438 RT_LOCK(rt);
439 /* Take an extra ref for ourselves */
440 RT_ADDREF_LOCKED(rt);
441 }
442 }
443
444 if (rt == NULL) {
445 ip6stat.ip6s_noroute++;
446 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
447 if (mcopy) {
448 icmp6_error(mcopy, ICMP6_DST_UNREACH,
449 ICMP6_DST_UNREACH_NOROUTE, 0);
450 }
451 m_freem(m);
452 return NULL;
453 }
454 RT_LOCK_ASSERT_HELD(rt);
455 } else if (ROUTE_UNUSABLE(ip6forward_rt) ||
456 !in6_are_addr_equal_scoped(&ip6->ip6_dst, &dst->sin6_addr, ip6_input_getdstifscope(m), dst->sin6_scope_id)) {
457 if (rt != NULL) {
458 /* Release extra ref */
459 RT_REMREF_LOCKED(rt);
460 RT_UNLOCK(rt);
461 }
462 ROUTE_RELEASE(ip6forward_rt);
463
464 SOCKADDR_ZERO(dst, sizeof(*dst));
465 dst->sin6_len = sizeof(struct sockaddr_in6);
466 dst->sin6_family = AF_INET6;
467 dst->sin6_addr = ip6->ip6_dst;
468
469 rtalloc_scoped_ign((struct route *)ip6forward_rt,
470 RTF_PRCLONING, ifscope);
471 if ((rt = ip6forward_rt->ro_rt) == NULL) {
472 ip6stat.ip6s_noroute++;
473 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
474 if (mcopy) {
475 icmp6_error(mcopy, ICMP6_DST_UNREACH,
476 ICMP6_DST_UNREACH_NOROUTE, 0);
477 }
478 m_freem(m);
479 return NULL;
480 }
481 RT_LOCK(rt);
482 /* Take an extra ref for ourselves */
483 RT_ADDREF_LOCKED(rt);
484 }
485
486 /*
487 * Source scope check: if a packet can't be delivered to its
488 * destination for the reason that the destination is beyond the scope
489 * of the source address, discard the packet and return an icmp6
490 * destination unreachable error with Code 2 (beyond scope of source
491 * address) unless we are proxying (source address is link local
492 * for NUDs.) We use a local copy of ip6_src, since in6_setscope()
493 * will possibly modify its first argument.
494 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
495 */
496 src_in6 = ip6->ip6_src;
497 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
498 RT_REMREF_LOCKED(rt);
499 RT_UNLOCK(rt);
500 /* XXX: this should not happen */
501 ip6stat.ip6s_cantforward++;
502 ip6stat.ip6s_badscope++;
503 m_freem(m);
504 return NULL;
505 }
506 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
507 RT_REMREF_LOCKED(rt);
508 RT_UNLOCK(rt);
509 ip6stat.ip6s_cantforward++;
510 ip6stat.ip6s_badscope++;
511 m_freem(m);
512 return NULL;
513 }
514
515 if (inzone != outzone && !proxy) {
516 ip6stat.ip6s_cantforward++;
517 ip6stat.ip6s_badscope++;
518 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
519
520 if (ip6_log_time + ip6_log_interval < curtime) {
521 ip6_log_time = curtime;
522 log(LOG_DEBUG,
523 "cannot forward "
524 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
525 ip6_sprintf(&ip6->ip6_src),
526 ip6_sprintf(&ip6->ip6_dst),
527 ip6->ip6_nxt,
528 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
529 }
530 /* Release extra ref */
531 RT_REMREF_LOCKED(rt);
532 RT_UNLOCK(rt);
533 if (mcopy) {
534 icmp6_error(mcopy, ICMP6_DST_UNREACH,
535 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
536 }
537 m_freem(m);
538 return NULL;
539 }
540
541 /*
542 * Destination scope check: if a packet is going to break the scope
543 * zone of packet's destination address, discard it. This case should
544 * usually be prevented by appropriately-configured routing table, but
545 * we need an explicit check because we may mistakenly forward the
546 * packet to a different zone by (e.g.) a default route.
547 */
548 dst_in6 = ip6->ip6_dst;
549 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
550 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
551 inzone != outzone) {
552 RT_REMREF_LOCKED(rt);
553 RT_UNLOCK(rt);
554 ip6stat.ip6s_cantforward++;
555 ip6stat.ip6s_badscope++;
556 m_freem(m);
557 return NULL;
558 }
559
560 if (mpktlen == 0) {
561 mpktlen = m->m_pkthdr.len;
562 }
563
564 if (mpktlen > rt->rt_ifp->if_mtu) {
565 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
566 if (mcopy) {
567 uint32_t mtu;
568#if IPSEC
569 struct secpolicy *sp2;
570 int ipsecerror;
571 size_t ipsechdrsiz;
572#endif
573
574 mtu = rt->rt_ifp->if_mtu;
575#if IPSEC
576 /*
577 * When we do IPsec tunnel ingress, we need to play
578 * with the link value (decrement IPsec header size
579 * from mtu value). The code is much simpler than v4
580 * case, as we have the outgoing interface for
581 * encapsulated packet as "rt->rt_ifp".
582 */
583 sp2 = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
584 IP_FORWARDING, &ipsecerror);
585 if (sp2) {
586 ipsechdrsiz = ipsec6_hdrsiz(mcopy,
587 IPSEC_DIR_OUTBOUND, NULL);
588 if (ipsechdrsiz < mtu) {
589 mtu -= ipsechdrsiz;
590 }
591 key_freesp(sp2, KEY_SADB_UNLOCKED);
592 }
593 /*
594 * if mtu becomes less than minimum MTU,
595 * tell minimum MTU (and I'll need to fragment it).
596 */
597 if (mtu < IPV6_MMTU) {
598 mtu = IPV6_MMTU;
599 }
600#endif
601 /* Release extra ref */
602 RT_REMREF_LOCKED(rt);
603 RT_UNLOCK(rt);
604 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
605 } else {
606 /* Release extra ref */
607 RT_REMREF_LOCKED(rt);
608 RT_UNLOCK(rt);
609 }
610 m_freem(m);
611 return NULL;
612 }
613
614 if (rt->rt_flags & RTF_GATEWAY) {
615 dst = SIN6(rt->rt_gateway);
616 }
617
618 /*
619 * If we are to forward the packet using the same interface
620 * as one we got the packet from, perhaps we should send a redirect
621 * to sender to shortcut a hop.
622 * Only send redirect if source is sending directly to us,
623 * and if packet was not source routed (or has any options).
624 * Also, don't send redirect if forwarding using a route
625 * modified by a redirect.
626 */
627 if (!proxy &&
628 ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
629 (rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) == 0) {
630 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
631 /*
632 * If the incoming interface is equal to the outgoing
633 * one, and the link attached to the interface is
634 * point-to-point, then it will be highly probable
635 * that a routing loop occurs. Thus, we immediately
636 * drop the packet and send an ICMPv6 error message.
637 *
638 * type/code is based on suggestion by Rich Draves.
639 * not sure if it is the best pick.
640 */
641 RT_REMREF_LOCKED(rt); /* Release extra ref */
642 RT_UNLOCK(rt);
643 icmp6_error(mcopy, ICMP6_DST_UNREACH,
644 ICMP6_DST_UNREACH_ADDR, 0);
645 m_freem(m);
646 return NULL;
647 }
648 type = ND_REDIRECT;
649 }
650 /*
651 * Fake scoped addresses. Note that even link-local source or
652 * destinaion can appear, if the originating node just sends the
653 * packet to us (without address resolution for the destination).
654 * Since both icmp6_error and icmp6_redirect_output fill the embedded
655 * link identifiers, we can do this stuff after making a copy for
656 * returning an error.
657 */
658 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
659 /*
660 * See corresponding comments in ip6_output.
661 * XXX: but is it possible that ip6_forward() sends a packet
662 * to a loopback interface? I don't think so, and thus
663 * I bark here. (jinmei@kame.net)
664 * XXX: it is common to route invalid packets to loopback.
665 * also, the codepath will be visited on use of ::1 in
666 * rthdr. (itojun)
667 */
668#if 1
669 if ((0))
670#else
671 if ((rt->rt_flags & (RTF_BLACKHOLE | RTF_REJECT)) == 0)
672#endif
673 {
674 printf("ip6_forward: outgoing interface is loopback. "
675 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
676 ip6_sprintf(&ip6->ip6_src),
677 ip6_sprintf(&ip6->ip6_dst),
678 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
679 if_name(rt->rt_ifp));
680 }
681
682 /* we can just use rcvif in forwarding. */
683 origifp = rcvifp = m->m_pkthdr.rcvif;
684 } else if (nd6_prproxy) {
685 /*
686 * In the prefix proxying case, we need to inform nd6_output()
687 * about the inbound interface, so that any subsequent NS
688 * packets generated by nd6_prproxy_ns_output() will not be
689 * sent back to that same interface.
690 */
691 origifp = rcvifp = m->m_pkthdr.rcvif;
692 } else {
693 rcvifp = m->m_pkthdr.rcvif;
694 origifp = rt->rt_ifp;
695 }
696 /*
697 * clear embedded scope identifiers if necessary.
698 * in6_clearscope will touch the addresses only when necessary.
699 */
700 in6_clearscope(&ip6->ip6_src);
701 in6_clearscope(&ip6->ip6_dst);
702
703 ifp = rt->rt_ifp;
704 /* Drop the lock but retain the extra ref */
705 RT_UNLOCK(rt);
706
707 /*
708 * If this is to be processed locally, let ip6_input have it.
709 */
710 if (proxy) {
711 VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST);
712 /* Release extra ref */
713 RT_REMREF(rt);
714 if (mcopy != NULL) {
715 m_freem(mcopy);
716 }
717 return m;
718 }
719
720 /* Mark this packet as being forwarded from another interface */
721 m->m_pkthdr.pkt_flags |= PKTF_FORWARDED;
722
723#if PF
724 if (PF_IS_ENABLED) {
725 /*
726 * PF refragments any packet which it reassembled due to scrub
727 * rules, in which case it will set the PF_TAG_REFRAGMENTED
728 * flag in PF mbuf tag.
729 */
730#if DUMMYNET
731 struct ip_fw_args args;
732 struct pf_mtag *pf_mtag;
733
734 bzero(s: &args, n: sizeof(args));
735
736 args.fwa_oif = ifp;
737 args.fwa_oflags = 0;
738 args.fwa_ro6 = ip6forward_rt;
739 args.fwa_ro6_pmtu = ip6forward_rt;
740 args.fwa_mtu = rt->rt_ifp->if_mtu;
741 args.fwa_dst6 = dst;
742 args.fwa_origifp = origifp;
743 /* Invoke outbound packet filter */
744 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, &args);
745#else /* !DUMMYNET */
746 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, NULL);
747#endif /* !DUMMYNET */
748 if (error != 0 || m == NULL) {
749 if (m != NULL) {
750 panic("%s: unexpected packet %p", __func__, m);
751 /* NOTREACHED */
752 }
753 /* Already freed by callee */
754 goto senderr;
755 }
756
757 pf_mtag = pf_find_mtag(m);
758 /*
759 * refragmented packets from PF.
760 */
761 if ((pf_mtag->pftag_flags & PF_TAG_REFRAGMENTED) != 0) {
762 struct mbuf *t;
763
764 pf_mtag->pftag_flags &= ~PF_TAG_REFRAGMENTED;
765 /* for statistics */
766 t = m;
767 while (t != NULL) {
768 pktcnt++;
769 len += m_pktlen(t);
770 t = t->m_nextpkt;
771 }
772
773 /*
774 * nd6_output() frees packetchain in both success and
775 * failure cases.
776 */
777 error = nd6_output(ifp, origifp, m, dst, rt, NULL);
778 m = NULL;
779 goto sent;
780 }
781 /*
782 * We do not use ip6 header again in the code below,
783 * however still adding the bit here so that any new
784 * code in future doesn't end up working with the
785 * wrong pointer
786 */
787 ip6 = mtod(m, struct ip6_hdr *);
788 }
789#endif /* PF */
790
791 len = m_pktlen(m);
792 pktcnt = 1;
793 error = nd6_output(ifp, origifp, m, dst, rt, NULL);
794sent:
795 if (error) {
796 in6_ifstat_add(ifp, ifs6_out_discard, pktcnt);
797 ip6stat.ip6s_cantforward += pktcnt;
798 } else {
799 /*
800 * Increment stats on the source interface; the ones
801 * for destination interface has been taken care of
802 * during output above by virtue of PKTF_FORWARDED.
803 */
804 rcvifp->if_fpackets += pktcnt;
805 rcvifp->if_fbytes += len;
806
807 ip6stat.ip6s_forward += pktcnt;
808 in6_ifstat_add(ifp, ifs6_out_forward, pktcnt);
809 if (type) {
810 ip6stat.ip6s_redirectsent++;
811 } else {
812 if (mcopy) {
813 goto freecopy;
814 }
815 }
816 }
817#if PF
818senderr:
819#endif /* PF */
820 if (mcopy == NULL) {
821 /* Release extra ref */
822 RT_REMREF(rt);
823 return NULL;
824 }
825 switch (error) {
826 case 0:
827#if 1
828 if (type == ND_REDIRECT) {
829 icmp6_redirect_output(mcopy, rt);
830 /* Release extra ref */
831 RT_REMREF(rt);
832 return NULL;
833 }
834#endif
835 goto freecopy;
836
837 case EMSGSIZE:
838 /* xxx MTU is constant in PPP? */
839 goto freecopy;
840
841 case ENOBUFS:
842 /* Tell source to slow down like source quench in IP? */
843 goto freecopy;
844
845 case ENETUNREACH: /* shouldn't happen, checked above */
846 case EHOSTUNREACH:
847 case ENETDOWN:
848 case EHOSTDOWN:
849 default:
850 type = ICMP6_DST_UNREACH;
851 code = ICMP6_DST_UNREACH_ADDR;
852 break;
853 }
854 icmp6_error(mcopy, type, code, 0);
855 /* Release extra ref */
856 RT_REMREF(rt);
857 return NULL;
858
859freecopy:
860 m_freem(mcopy);
861 /* Release extra ref */
862 RT_REMREF(rt);
863 return NULL;
864}
865