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