]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ip6_forward.c
xnu-1699.24.23.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_forward.c
CommitLineData
b0d623f7 1/*
6d2010ae 2 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
b0d623f7
A
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
55e303ae 29/* $FreeBSD: src/sys/netinet6/ip6_forward.c,v 1.16 2002/10/16 02:25:05 sam Exp $ */
9bccf70c 30/* $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $ */
1c79356b
A
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
1c79356b
A
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>
9bccf70c
A
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
1c79356b 81#include <netinet/ip_var.h>
9bccf70c 82#include <netinet6/in6_var.h>
1c79356b
A
83#include <netinet/ip6.h>
84#include <netinet6/ip6_var.h>
85#include <netinet/icmp6.h>
86#include <netinet6/nd6.h>
6d2010ae 87#include <netinet6/scope6_var.h>
1c79356b 88
9bccf70c
A
89#include <netinet/in_pcb.h>
90
91#if IPSEC
1c79356b 92#include <netinet6/ipsec.h>
9bccf70c
A
93#if INET6
94#include <netinet6/ipsec6.h>
95#endif
1c79356b 96#include <netkey/key.h>
9bccf70c
A
97extern int ipsec_bypass;
98#endif /* IPSEC */
1c79356b 99
1c79356b 100#include <netinet6/ip6_fw.h>
1c79356b
A
101
102#include <net/net_osdep.h>
103
b0d623f7
A
104#if PF
105#include <net/pfvar.h>
106#endif /* PF */
107
1c79356b
A
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
121void
2d21ac55 122ip6_forward(struct mbuf *m, struct route_in6 *ip6forward_rt,
6d2010ae 123 int srcrt)
1c79356b
A
124{
125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9bccf70c
A
126 struct sockaddr_in6 *dst;
127 struct rtentry *rt;
1c79356b
A
128 int error, type = 0, code = 0;
129 struct mbuf *mcopy = NULL;
b0d623f7 130 struct ifnet *ifp, *origifp; /* maybe unnecessary */
6d2010ae
A
131 u_int32_t inzone, outzone;
132 struct in6_addr src_in6, dst_in6;
9bccf70c 133#if IPSEC
1c79356b
A
134 struct secpolicy *sp = NULL;
135#endif
91447636 136 struct timeval timenow;
6d2010ae
A
137 unsigned int ifscope = IFSCOPE_NONE;
138#if PF
139 struct pf_mtag *pf_mtag;
140#endif /* PF */
91447636
A
141
142 getmicrotime(&timenow);
6d2010ae
A
143#if PF
144 pf_mtag = pf_find_mtag(m);
145 if (pf_mtag != NULL && pf_mtag->rtableid != IFSCOPE_NONE)
146 ifscope = pf_mtag->rtableid;
147#endif /* PF */
1c79356b 148
9bccf70c 149#if IPSEC
1c79356b
A
150 /*
151 * Check AH/ESP integrity.
152 */
153 /*
154 * Don't increment ip6s_cantforward because this is the check
155 * before forwarding packet actually.
156 */
91447636 157 if (ipsec_bypass == 0) {
91447636 158 if (ipsec6_in_reject(m, NULL)) {
2d21ac55 159 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
91447636
A
160 m_freem(m);
161 return;
162 }
1c79356b 163 }
9bccf70c 164#endif /*IPSEC*/
1c79356b 165
9bccf70c
A
166 /*
167 * Do not forward packets to multicast destination (should be handled
168 * by ip6_mforward().
169 * Do not forward packets with unspecified source. It was discussed
170 * in July 2000, on ipngwg mailing list.
171 */
1c79356b 172 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
9bccf70c
A
173 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
174 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1c79356b
A
175 ip6stat.ip6s_cantforward++;
176 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
91447636
A
177 if (ip6_log_time + ip6_log_interval < timenow.tv_sec) {
178 ip6_log_time = timenow.tv_sec;
1c79356b
A
179 log(LOG_DEBUG,
180 "cannot forward "
181 "from %s to %s nxt %d received on %s\n",
182 ip6_sprintf(&ip6->ip6_src),
183 ip6_sprintf(&ip6->ip6_dst),
184 ip6->ip6_nxt,
185 if_name(m->m_pkthdr.rcvif));
186 }
187 m_freem(m);
188 return;
189 }
190
191 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
192 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
193 icmp6_error(m, ICMP6_TIME_EXCEEDED,
194 ICMP6_TIME_EXCEED_TRANSIT, 0);
195 return;
196 }
197 ip6->ip6_hlim -= IPV6_HLIMDEC;
198
199 /*
200 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
201 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
202 * we need to generate an ICMP6 message to the src.
203 * Thanks to M_EXT, in most cases copy will not occur.
204 *
205 * It is important to save it before IPsec processing as IPsec
206 * processing may modify the mbuf.
207 */
208 mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
209
9bccf70c
A
210#if IPSEC
211 if (ipsec_bypass != 0)
212 goto skip_ipsec;
1c79356b 213 /* get a security policy for this packet */
9bccf70c
A
214 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
215 &error);
1c79356b 216 if (sp == NULL) {
2d21ac55 217 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
1c79356b
A
218 ip6stat.ip6s_cantforward++;
219 if (mcopy) {
220#if 0
221 /* XXX: what icmp ? */
222#else
223 m_freem(mcopy);
224#endif
225 }
226 m_freem(m);
227 return;
228 }
229
230 error = 0;
231
232 /* check policy */
233 switch (sp->policy) {
234 case IPSEC_POLICY_DISCARD:
2d21ac55 235 case IPSEC_POLICY_GENERATE:
1c79356b
A
236 /*
237 * This packet is just discarded.
238 */
2d21ac55 239 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
1c79356b 240 ip6stat.ip6s_cantforward++;
2d21ac55 241 key_freesp(sp, KEY_SADB_UNLOCKED);
1c79356b
A
242 if (mcopy) {
243#if 0
244 /* XXX: what icmp ? */
245#else
246 m_freem(mcopy);
247#endif
248 }
249 m_freem(m);
250 return;
251
252 case IPSEC_POLICY_BYPASS:
253 case IPSEC_POLICY_NONE:
254 /* no need to do IPsec. */
2d21ac55 255 key_freesp(sp, KEY_SADB_UNLOCKED);
1c79356b 256 goto skip_ipsec;
9bccf70c 257
1c79356b
A
258 case IPSEC_POLICY_IPSEC:
259 if (sp->req == NULL) {
260 /* XXX should be panic ? */
261 printf("ip6_forward: No IPsec request specified.\n");
262 ip6stat.ip6s_cantforward++;
2d21ac55 263 key_freesp(sp, KEY_SADB_UNLOCKED);
1c79356b
A
264 if (mcopy) {
265#if 0
266 /* XXX: what icmp ? */
267#else
268 m_freem(mcopy);
269#endif
270 }
271 m_freem(m);
272 return;
273 }
274 /* do IPsec */
275 break;
276
277 case IPSEC_POLICY_ENTRUST:
278 default:
279 /* should be panic ?? */
280 printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
2d21ac55 281 key_freesp(sp, KEY_SADB_UNLOCKED);
1c79356b
A
282 goto skip_ipsec;
283 }
284
285 {
286 struct ipsec_output_state state;
287
288 /*
289 * All the extension headers will become inaccessible
290 * (since they can be encrypted).
291 * Don't panic, we need no more updates to extension headers
292 * on inner IPv6 packet (since they are now encapsulated).
293 *
294 * IPv6 [ESP|AH] IPv6 [extension headers] payload
295 */
296 bzero(&state, sizeof(state));
297 state.m = m;
1c79356b
A
298 state.dst = NULL; /* update at ipsec6_output_tunnel() */
299
ebb1b9f4 300 error = ipsec6_output_tunnel(&state, sp, 0);
2d21ac55 301 key_freesp(sp, KEY_SADB_UNLOCKED);
ebb1b9f4 302 if (state.tunneled == 4)
2d21ac55
A
303 return; /* packet is gone - sent over IPv4 */
304
1c79356b 305 m = state.m;
ebb1b9f4
A
306 if (state.ro.ro_rt) {
307 rtfree(state.ro.ro_rt);
308 state.ro.ro_rt = NULL;
309 }
1c79356b
A
310 if (error) {
311 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
312 switch (error) {
313 case EHOSTUNREACH:
314 case ENETUNREACH:
315 case EMSGSIZE:
316 case ENOBUFS:
317 case ENOMEM:
318 break;
319 default:
320 printf("ip6_output (ipsec): error code %d\n", error);
55e303ae 321 /* fall through */
1c79356b
A
322 case ENOENT:
323 /* don't show these error codes to the user */
324 break;
325 }
326 ip6stat.ip6s_cantforward++;
327 if (mcopy) {
328#if 0
329 /* XXX: what icmp ? */
330#else
331 m_freem(mcopy);
332#endif
333 }
334 m_freem(m);
335 return;
336 }
337 }
338 skip_ipsec:
9bccf70c
A
339#endif /* IPSEC */
340
2d21ac55 341 dst = (struct sockaddr_in6 *)&ip6forward_rt->ro_dst;
b0d623f7
A
342 if ((rt = ip6forward_rt->ro_rt) != NULL) {
343 RT_LOCK(rt);
344 /* Take an extra ref for ourselves */
345 RT_ADDREF_LOCKED(rt);
346 }
347
1c79356b
A
348 if (!srcrt) {
349 /*
2d21ac55 350 * ip6forward_rt->ro_dst.sin6_addr is equal to ip6->ip6_dst
1c79356b 351 */
b0d623f7
A
352 if (rt == NULL || !(rt->rt_flags & RTF_UP) ||
353 rt->generation_id != route_generation) {
354 if (rt != NULL) {
355 /* Release extra ref */
356 RT_REMREF_LOCKED(rt);
357 RT_UNLOCK(rt);
358 rtfree(rt);
359 ip6forward_rt->ro_rt = NULL;
1c79356b
A
360 }
361 /* this probably fails but give it a try again */
6d2010ae
A
362 rtalloc_scoped_ign((struct route *)ip6forward_rt,
363 RTF_PRCLONING, ifscope);
b0d623f7
A
364 if ((rt = ip6forward_rt->ro_rt) != NULL) {
365 RT_LOCK(rt);
366 /* Take an extra ref for ourselves */
367 RT_ADDREF_LOCKED(rt);
368 }
1c79356b 369 }
9bccf70c 370
b0d623f7 371 if (rt == NULL) {
1c79356b 372 ip6stat.ip6s_noroute++;
9bccf70c 373 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
6d2010ae 374 if (mcopy)
1c79356b
A
375 icmp6_error(mcopy, ICMP6_DST_UNREACH,
376 ICMP6_DST_UNREACH_NOROUTE, 0);
1c79356b
A
377 m_freem(m);
378 return;
379 }
b0d623f7
A
380 RT_LOCK_ASSERT_HELD(rt);
381 } else if (rt == NULL || !(rt->rt_flags & RTF_UP) ||
382 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr) ||
383 rt->generation_id != route_generation) {
384 if (rt != NULL) {
385 /* Release extra ref */
386 RT_REMREF_LOCKED(rt);
387 RT_UNLOCK(rt);
388 rtfree(rt);
389 ip6forward_rt->ro_rt = NULL;
1c79356b
A
390 }
391 bzero(dst, sizeof(*dst));
392 dst->sin6_len = sizeof(struct sockaddr_in6);
393 dst->sin6_family = AF_INET6;
394 dst->sin6_addr = ip6->ip6_dst;
395
6d2010ae
A
396 rtalloc_scoped_ign((struct route *)ip6forward_rt,
397 RTF_PRCLONING, ifscope);
b0d623f7 398 if ((rt = ip6forward_rt->ro_rt) == NULL) {
1c79356b 399 ip6stat.ip6s_noroute++;
9bccf70c 400 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
6d2010ae 401 if (mcopy)
1c79356b 402 icmp6_error(mcopy, ICMP6_DST_UNREACH,
b0d623f7 403 ICMP6_DST_UNREACH_NOROUTE, 0);
1c79356b
A
404 m_freem(m);
405 return;
406 }
b0d623f7
A
407 RT_LOCK(rt);
408 /* Take an extra ref for ourselves */
409 RT_ADDREF_LOCKED(rt);
1c79356b 410 }
1c79356b
A
411
412 /*
6d2010ae
A
413 * Source scope check: if a packet can't be delivered to its
414 * destination for the reason that the destination is beyond the scope
415 * of the source address, discard the packet and return an icmp6
416 * destination unreachable error with Code 2 (beyond scope of source
417 * address). We use a local copy of ip6_src, since in6_setscope()
418 * will possibly modify its first argument.
419 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
1c79356b 420 */
6d2010ae
A
421 src_in6 = ip6->ip6_src;
422 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
423 /* XXX: this should not happen */
424 ip6stat.ip6s_cantforward++;
425 ip6stat.ip6s_badscope++;
426 m_freem(m);
427 return;
428 }
429 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
430 ip6stat.ip6s_cantforward++;
431 ip6stat.ip6s_badscope++;
432 m_freem(m);
433 return;
434 }
435 if (inzone != outzone) {
1c79356b
A
436 ip6stat.ip6s_cantforward++;
437 ip6stat.ip6s_badscope++;
438 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
439
91447636
A
440 if (ip6_log_time + ip6_log_interval < timenow.tv_sec) {
441 ip6_log_time = timenow.tv_sec;
1c79356b
A
442 log(LOG_DEBUG,
443 "cannot forward "
444 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
445 ip6_sprintf(&ip6->ip6_src),
446 ip6_sprintf(&ip6->ip6_dst),
447 ip6->ip6_nxt,
448 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
449 }
b0d623f7
A
450 /* Release extra ref */
451 RT_REMREF_LOCKED(rt);
452 RT_UNLOCK(rt);
91447636 453 if (mcopy) {
1c79356b
A
454 icmp6_error(mcopy, ICMP6_DST_UNREACH,
455 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
91447636 456 }
1c79356b
A
457 m_freem(m);
458 return;
459 }
460
6d2010ae
A
461 /*
462 * Destination scope check: if a packet is going to break the scope
463 * zone of packet's destination address, discard it. This case should
464 * usually be prevented by appropriately-configured routing table, but
465 * we need an explicit check because we may mistakenly forward the
466 * packet to a different zone by (e.g.) a default route.
467 */
468 dst_in6 = ip6->ip6_dst;
469 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
470 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
471 inzone != outzone) {
472 ip6stat.ip6s_cantforward++;
473 ip6stat.ip6s_badscope++;
474 m_freem(m);
475 return;
476 }
477
1c79356b
A
478 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
479 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
480 if (mcopy) {
b0d623f7 481 uint32_t mtu;
9bccf70c 482#if IPSEC
2d21ac55 483 struct secpolicy *sp2;
1c79356b
A
484 int ipsecerror;
485 size_t ipsechdrsiz;
486#endif
487
488 mtu = rt->rt_ifp->if_mtu;
55e303ae 489#if IPSEC
1c79356b
A
490 /*
491 * When we do IPsec tunnel ingress, we need to play
6d2010ae 492 * with the link value (decrement IPsec header size
1c79356b
A
493 * from mtu value). The code is much simpler than v4
494 * case, as we have the outgoing interface for
495 * encapsulated packet as "rt->rt_ifp".
496 */
2d21ac55 497 sp2 = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
1c79356b 498 IP_FORWARDING, &ipsecerror);
2d21ac55 499 if (sp2) {
1c79356b
A
500 ipsechdrsiz = ipsec6_hdrsiz(mcopy,
501 IPSEC_DIR_OUTBOUND, NULL);
502 if (ipsechdrsiz < mtu)
503 mtu -= ipsechdrsiz;
2d21ac55 504 key_freesp(sp2, KEY_SADB_UNLOCKED);
1c79356b 505 }
1c79356b
A
506 /*
507 * if mtu becomes less than minimum MTU,
508 * tell minimum MTU (and I'll need to fragment it).
509 */
510 if (mtu < IPV6_MMTU)
511 mtu = IPV6_MMTU;
512#endif
b0d623f7
A
513 /* Release extra ref */
514 RT_REMREF_LOCKED(rt);
515 RT_UNLOCK(rt);
1c79356b 516 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
b0d623f7
A
517 } else {
518 /* Release extra ref */
519 RT_REMREF_LOCKED(rt);
520 RT_UNLOCK(rt);
1c79356b
A
521 }
522 m_freem(m);
523 return;
524 }
525
526 if (rt->rt_flags & RTF_GATEWAY)
527 dst = (struct sockaddr_in6 *)rt->rt_gateway;
528
529 /*
530 * If we are to forward the packet using the same interface
531 * as one we got the packet from, perhaps we should send a redirect
532 * to sender to shortcut a hop.
533 * Only send redirect if source is sending directly to us,
534 * and if packet was not source routed (or has any options).
535 * Also, don't send redirect if forwarding using a route
536 * modified by a redirect.
537 */
6d2010ae 538 if (ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
9bccf70c
A
539 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
540 if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) != 0) {
541 /*
542 * If the incoming interface is equal to the outgoing
543 * one, and the link attached to the interface is
544 * point-to-point, then it will be highly probable
545 * that a routing loop occurs. Thus, we immediately
546 * drop the packet and send an ICMPv6 error message.
547 *
548 * type/code is based on suggestion by Rich Draves.
549 * not sure if it is the best pick.
550 */
b0d623f7
A
551 RT_REMREF_LOCKED(rt); /* Release extra ref */
552 RT_UNLOCK(rt);
9bccf70c
A
553 icmp6_error(mcopy, ICMP6_DST_UNREACH,
554 ICMP6_DST_UNREACH_ADDR, 0);
555 m_freem(m);
556 return;
557 }
1c79356b 558 type = ND_REDIRECT;
9bccf70c 559 }
1c79356b 560
7e4a7d39 561#if IPFW2
1c79356b
A
562 /*
563 * Check with the firewall...
564 */
9bccf70c 565 if (ip6_fw_enable && ip6_fw_chk_ptr) {
1c79356b 566 u_short port = 0;
b0d623f7
A
567 ifp = rt->rt_ifp;
568 /* Drop the lock but retain the extra ref */
569 RT_UNLOCK(rt);
1c79356b 570 /* If ipfw says divert, we have to just drop packet */
b0d623f7 571 if (ip6_fw_chk_ptr(&ip6, ifp, &port, &m)) {
1c79356b
A
572 m_freem(m);
573 goto freecopy;
574 }
b0d623f7 575 if (!m) {
1c79356b 576 goto freecopy;
b0d623f7
A
577 }
578 /* We still have the extra ref on rt */
579 RT_LOCK(rt);
1c79356b 580 }
7e4a7d39 581#endif
1c79356b 582
9bccf70c
A
583 /*
584 * Fake scoped addresses. Note that even link-local source or
585 * destinaion can appear, if the originating node just sends the
586 * packet to us (without address resolution for the destination).
587 * Since both icmp6_error and icmp6_redirect_output fill the embedded
588 * link identifiers, we can do this stuff after making a copy for
589 * returning an error.
590 */
591 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
592 /*
593 * See corresponding comments in ip6_output.
594 * XXX: but is it possible that ip6_forward() sends a packet
595 * to a loopback interface? I don't think so, and thus
596 * I bark here. (jinmei@kame.net)
597 * XXX: it is common to route invalid packets to loopback.
598 * also, the codepath will be visited on use of ::1 in
599 * rthdr. (itojun)
600 */
601#if 1
602 if (0)
1c79356b 603#else
9bccf70c
A
604 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
605#endif
606 {
607 printf("ip6_forward: outgoing interface is loopback. "
55e303ae
A
608 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
609 ip6_sprintf(&ip6->ip6_src),
610 ip6_sprintf(&ip6->ip6_dst),
611 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
612 if_name(rt->rt_ifp));
9bccf70c
A
613 }
614
615 /* we can just use rcvif in forwarding. */
616 origifp = m->m_pkthdr.rcvif;
617 }
618 else
619 origifp = rt->rt_ifp;
9bccf70c
A
620 /*
621 * clear embedded scope identifiers if necessary.
622 * in6_clearscope will touch the addresses only when necessary.
623 */
624 in6_clearscope(&ip6->ip6_src);
625 in6_clearscope(&ip6->ip6_dst);
9bccf70c 626
b0d623f7
A
627 ifp = rt->rt_ifp;
628 /* Drop the lock but retain the extra ref */
629 RT_UNLOCK(rt);
630
631#if PF
b0d623f7
A
632 /* Invoke outbound packet filter */
633 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE);
634
b0d623f7
A
635 if (error) {
636 if (m != NULL) {
637 panic("%s: unexpected packet %p\n", __func__, m);
638 /* NOTREACHED */
639 }
640 /* Already freed by callee */
641 goto senderr;
642 }
643 ip6 = mtod(m, struct ip6_hdr *);
644#endif /* PF */
645
6d2010ae 646 error = nd6_output(ifp, origifp, m, dst, rt);
1c79356b 647 if (error) {
b0d623f7 648 in6_ifstat_inc(ifp, ifs6_out_discard);
1c79356b
A
649 ip6stat.ip6s_cantforward++;
650 } else {
651 ip6stat.ip6s_forward++;
b0d623f7 652 in6_ifstat_inc(ifp, ifs6_out_forward);
1c79356b
A
653 if (type)
654 ip6stat.ip6s_redirectsent++;
655 else {
b0d623f7 656 if (mcopy) {
1c79356b 657 goto freecopy;
b0d623f7 658 }
1c79356b
A
659 }
660 }
b0d623f7
A
661#if PF
662senderr:
663#endif /* PF */
664 if (mcopy == NULL) {
665 /* Release extra ref */
666 RT_REMREF(rt);
1c79356b 667 return;
b0d623f7 668 }
1c79356b
A
669 switch (error) {
670 case 0:
671#if 1
672 if (type == ND_REDIRECT) {
673 icmp6_redirect_output(mcopy, rt);
b0d623f7
A
674 /* Release extra ref */
675 RT_REMREF(rt);
1c79356b
A
676 return;
677 }
678#endif
679 goto freecopy;
680
681 case EMSGSIZE:
682 /* xxx MTU is constant in PPP? */
683 goto freecopy;
684
685 case ENOBUFS:
686 /* Tell source to slow down like source quench in IP? */
687 goto freecopy;
688
689 case ENETUNREACH: /* shouldn't happen, checked above */
690 case EHOSTUNREACH:
691 case ENETDOWN:
692 case EHOSTDOWN:
693 default:
694 type = ICMP6_DST_UNREACH;
695 code = ICMP6_DST_UNREACH_ADDR;
696 break;
697 }
698 icmp6_error(mcopy, type, code, 0);
b0d623f7
A
699 /* Release extra ref */
700 RT_REMREF(rt);
1c79356b
A
701 return;
702
703 freecopy:
704 m_freem(mcopy);
b0d623f7
A
705 /* Release extra ref */
706 RT_REMREF(rt);
1c79356b
A
707 return;
708}