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