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