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