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