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