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