1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
482cd038dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue * All rights reserved.
682cd038dSYoshinobu Inoue *
782cd038dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue * are met:
1082cd038dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue * documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue * may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue * without specific prior written permission.
1882cd038dSYoshinobu Inoue *
1982cd038dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien *
31b48287a3SDavid E. O'Brien * $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $
3282cd038dSYoshinobu Inoue */
3382cd038dSYoshinobu Inoue
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
36686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38f8a8f9caSSUZUKI Shinsuke #include "opt_ipstealth.h"
3949c5659eSMark Johnston #include "opt_sctp.h"
4082cd038dSYoshinobu Inoue
4182cd038dSYoshinobu Inoue #include <sys/param.h>
4282cd038dSYoshinobu Inoue #include <sys/systm.h>
4333841545SHajimu UMEMOTO #include <sys/malloc.h>
4482cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4582cd038dSYoshinobu Inoue #include <sys/domain.h>
4682cd038dSYoshinobu Inoue #include <sys/protosw.h>
4782cd038dSYoshinobu Inoue #include <sys/socket.h>
4882cd038dSYoshinobu Inoue #include <sys/errno.h>
4982cd038dSYoshinobu Inoue #include <sys/time.h>
5033841545SHajimu UMEMOTO #include <sys/kernel.h>
5182cd038dSYoshinobu Inoue #include <sys/syslog.h>
5282cd038dSYoshinobu Inoue
5382cd038dSYoshinobu Inoue #include <net/if.h>
5476039bc8SGleb Smirnoff #include <net/if_var.h>
553d0d5b21SJustin Hibbits #include <net/if_private.h>
568a006adbSBjoern A. Zeeb #include <net/netisr.h>
5782cd038dSYoshinobu Inoue #include <net/route.h>
5853a4886dSAlexander V. Chernikov #include <net/route/nhop.h>
593a4d67f7SDarren Reed #include <net/pfil.h>
6082cd038dSYoshinobu Inoue
6182cd038dSYoshinobu Inoue #include <netinet/in.h>
6282cd038dSYoshinobu Inoue #include <netinet/in_var.h>
6333841545SHajimu UMEMOTO #include <netinet/in_systm.h>
6433841545SHajimu UMEMOTO #include <netinet/ip.h>
65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
6633841545SHajimu UMEMOTO #include <netinet6/in6_var.h>
67686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
6853a4886dSAlexander V. Chernikov #include <netinet6/in6_fib.h>
6982cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
70a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
7382cd038dSYoshinobu Inoue
7433841545SHajimu UMEMOTO #include <netinet/in_pcb.h>
7533841545SHajimu UMEMOTO
76fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
77b9234fafSSam Leffler
7882cd038dSYoshinobu Inoue /*
7982cd038dSYoshinobu Inoue * Forward a packet. If some error occurs return the sender
8082cd038dSYoshinobu Inoue * an icmp packet. Note we can't always generate a meaningful
8182cd038dSYoshinobu Inoue * icmp message because icmp doesn't have a large enough repertoire
8282cd038dSYoshinobu Inoue * of codes and types.
8382cd038dSYoshinobu Inoue *
8482cd038dSYoshinobu Inoue * If not forwarding, just drop the packet. This could be confusing
8582cd038dSYoshinobu Inoue * if ipforwarding was zero but some routing protocol was advancing
8682cd038dSYoshinobu Inoue * us as a gateway to somewhere. However, we must let the routing
8782cd038dSYoshinobu Inoue * protocol deal with that.
8882cd038dSYoshinobu Inoue *
8982cd038dSYoshinobu Inoue */
9082cd038dSYoshinobu Inoue void
ip6_forward(struct mbuf * m,int srcrt)911272577eSXin LI ip6_forward(struct mbuf *m, int srcrt)
9282cd038dSYoshinobu Inoue {
9382cd038dSYoshinobu Inoue struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9453a4886dSAlexander V. Chernikov struct sockaddr_in6 dst;
9553a4886dSAlexander V. Chernikov struct nhop_object *nh = NULL;
9682cd038dSYoshinobu Inoue int error, type = 0, code = 0;
97686cdd19SJun-ichiro itojun Hagino struct mbuf *mcopy = NULL;
98686cdd19SJun-ichiro itojun Hagino struct ifnet *origifp; /* maybe unnecessary */
99a1f7e5f8SHajimu UMEMOTO u_int32_t inzone, outzone;
10053a4886dSAlexander V. Chernikov struct in6_addr odst;
1018a006adbSBjoern A. Zeeb struct m_tag *fwd_tag;
1021d54aa3bSBjoern A. Zeeb char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
10382cd038dSYoshinobu Inoue
10433841545SHajimu UMEMOTO /*
10533841545SHajimu UMEMOTO * Do not forward packets to multicast destination (should be handled
10633841545SHajimu UMEMOTO * by ip6_mforward().
10733841545SHajimu UMEMOTO * Do not forward packets with unspecified source. It was discussed
1087efe5d92SHajimu UMEMOTO * in July 2000, on the ipngwg mailing list.
10933841545SHajimu UMEMOTO */
110686cdd19SJun-ichiro itojun Hagino if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
11133841545SHajimu UMEMOTO IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
11233841545SHajimu UMEMOTO IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1139cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_cantforward);
11482cd038dSYoshinobu Inoue /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
11535b6e52cSPawel Biernacki if (V_ip6_log_cannot_forward && ip6_log_ratelimit()) {
11682cd038dSYoshinobu Inoue log(LOG_DEBUG,
11782cd038dSYoshinobu Inoue "cannot forward "
11882cd038dSYoshinobu Inoue "from %s to %s nxt %d received on %s\n",
1191d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufs, &ip6->ip6_src),
1201d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufd, &ip6->ip6_dst),
12182cd038dSYoshinobu Inoue ip6->ip6_nxt,
12282cd038dSYoshinobu Inoue if_name(m->m_pkthdr.rcvif));
12382cd038dSYoshinobu Inoue }
12482cd038dSYoshinobu Inoue m_freem(m);
12582cd038dSYoshinobu Inoue return;
12682cd038dSYoshinobu Inoue }
12782cd038dSYoshinobu Inoue
128fcf59617SAndrey V. Elsukov if (
129f8a8f9caSSUZUKI Shinsuke #ifdef IPSTEALTH
130fcf59617SAndrey V. Elsukov V_ip6stealth == 0 &&
131f8a8f9caSSUZUKI Shinsuke #endif
132fcf59617SAndrey V. Elsukov ip6->ip6_hlim <= IPV6_HLIMDEC) {
13382cd038dSYoshinobu Inoue /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
13482cd038dSYoshinobu Inoue icmp6_error(m, ICMP6_TIME_EXCEEDED,
13582cd038dSYoshinobu Inoue ICMP6_TIME_EXCEED_TRANSIT, 0);
13682cd038dSYoshinobu Inoue return;
13782cd038dSYoshinobu Inoue }
138f8a8f9caSSUZUKI Shinsuke
139686cdd19SJun-ichiro itojun Hagino /*
140686cdd19SJun-ichiro itojun Hagino * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
141686cdd19SJun-ichiro itojun Hagino * size of IPv6 + ICMPv6 headers) bytes of the packet in case
142686cdd19SJun-ichiro itojun Hagino * we need to generate an ICMP6 message to the src.
143686cdd19SJun-ichiro itojun Hagino * Thanks to M_EXT, in most cases copy will not occur.
144686cdd19SJun-ichiro itojun Hagino *
145686cdd19SJun-ichiro itojun Hagino * It is important to save it before IPsec processing as IPsec
146686cdd19SJun-ichiro itojun Hagino * processing may modify the mbuf.
147686cdd19SJun-ichiro itojun Hagino */
148c3bef61eSKevin Lo mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
149c3bef61eSKevin Lo M_NOWAIT);
150fcf59617SAndrey V. Elsukov #ifdef IPSTEALTH
151fcf59617SAndrey V. Elsukov if (V_ip6stealth == 0)
152686cdd19SJun-ichiro itojun Hagino #endif
153fcf59617SAndrey V. Elsukov ip6->ip6_hlim -= IPV6_HLIMDEC;
154fcf59617SAndrey V. Elsukov
155fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
156fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv6)) {
157fcf59617SAndrey V. Elsukov if ((error = IPSEC_FORWARD(ipv6, m)) != 0) {
158fcf59617SAndrey V. Elsukov /* mbuf consumed by IPsec */
159fcf59617SAndrey V. Elsukov m_freem(mcopy);
160fcf59617SAndrey V. Elsukov if (error != EINPROGRESS)
161fcf59617SAndrey V. Elsukov IP6STAT_INC(ip6s_cantforward);
16282cd038dSYoshinobu Inoue return;
16382cd038dSYoshinobu Inoue }
164fcf59617SAndrey V. Elsukov /* No IPsec processing required */
165686cdd19SJun-ichiro itojun Hagino }
1660f9ade71SHajimu UMEMOTO #endif
16753a4886dSAlexander V. Chernikov /*
16853a4886dSAlexander V. Chernikov * ip6_forward() operates with IPv6 addresses with deembedded scope.
16953a4886dSAlexander V. Chernikov *
17053a4886dSAlexander V. Chernikov * There are 3 sources of IPv6 destination address:
17153a4886dSAlexander V. Chernikov *
17253a4886dSAlexander V. Chernikov * 1) ip6_input(), where ip6_dst contains deembedded address.
17353a4886dSAlexander V. Chernikov * In order to deal with forwarding of link-local packets,
17453a4886dSAlexander V. Chernikov * calculate the scope based on input interface (RFC 4007, clause 9).
17553a4886dSAlexander V. Chernikov * 2) packet filters changing ip6_dst directly. It would embed scope
17653a4886dSAlexander V. Chernikov * for LL addresses, so in6_localip() performs properly.
17753a4886dSAlexander V. Chernikov * 3) packet filters attaching PACKET_TAG_IPFORWARD would embed
17853a4886dSAlexander V. Chernikov * scope for the nexthop.
17953a4886dSAlexander V. Chernikov */
18053a4886dSAlexander V. Chernikov bzero(&dst, sizeof(struct sockaddr_in6));
18153a4886dSAlexander V. Chernikov dst.sin6_family = AF_INET6;
18253a4886dSAlexander V. Chernikov dst.sin6_addr = ip6->ip6_dst;
18353a4886dSAlexander V. Chernikov dst.sin6_scope_id = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
1848a006adbSBjoern A. Zeeb again:
18553a4886dSAlexander V. Chernikov nh = fib6_lookup(M_GETFIB(m), &dst.sin6_addr, dst.sin6_scope_id,
18653a4886dSAlexander V. Chernikov NHR_REF, m->m_pkthdr.flowid);
18753a4886dSAlexander V. Chernikov if (nh == NULL) {
1889cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_noroute);
18933841545SHajimu UMEMOTO in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
190686cdd19SJun-ichiro itojun Hagino if (mcopy) {
191686cdd19SJun-ichiro itojun Hagino icmp6_error(mcopy, ICMP6_DST_UNREACH,
19282cd038dSYoshinobu Inoue ICMP6_DST_UNREACH_NOROUTE, 0);
193686cdd19SJun-ichiro itojun Hagino }
19409f8c3ffSBjoern A. Zeeb goto bad;
19582cd038dSYoshinobu Inoue }
196686cdd19SJun-ichiro itojun Hagino
19730dd227cSAlexander V. Chernikov if (nh->nh_flags & (NHF_BLACKHOLE | NHF_REJECT)) {
19830dd227cSAlexander V. Chernikov IP6STAT_INC(ip6s_cantforward);
199*b50e1465SAlexander V. Chernikov if (mcopy != NULL) {
200*b50e1465SAlexander V. Chernikov if (nh->nh_flags & NHF_REJECT) {
20130dd227cSAlexander V. Chernikov icmp6_error(mcopy, ICMP6_DST_UNREACH,
20230dd227cSAlexander V. Chernikov ICMP6_DST_UNREACH_REJECT, 0);
203*b50e1465SAlexander V. Chernikov } else
204*b50e1465SAlexander V. Chernikov m_freem(mcopy);
20530dd227cSAlexander V. Chernikov }
20630dd227cSAlexander V. Chernikov goto bad;
20730dd227cSAlexander V. Chernikov }
20830dd227cSAlexander V. Chernikov
209686cdd19SJun-ichiro itojun Hagino /*
210a1f7e5f8SHajimu UMEMOTO * Source scope check: if a packet can't be delivered to its
211a1f7e5f8SHajimu UMEMOTO * destination for the reason that the destination is beyond the scope
212a1f7e5f8SHajimu UMEMOTO * of the source address, discard the packet and return an icmp6
213a1f7e5f8SHajimu UMEMOTO * destination unreachable error with Code 2 (beyond scope of source
21453a4886dSAlexander V. Chernikov * address).
215a1f7e5f8SHajimu UMEMOTO * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
216686cdd19SJun-ichiro itojun Hagino */
21753a4886dSAlexander V. Chernikov outzone = in6_get_unicast_scopeid(&ip6->ip6_src, nh->nh_ifp);
21853a4886dSAlexander V. Chernikov inzone = in6_get_unicast_scopeid(&ip6->ip6_src, m->m_pkthdr.rcvif);
21945b4fb04SAndrey V. Elsukov if (inzone != outzone) {
2209cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_cantforward);
2219cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_badscope);
22253a4886dSAlexander V. Chernikov in6_ifstat_inc(nh->nh_ifp, ifs6_in_discard);
223686cdd19SJun-ichiro itojun Hagino
22435b6e52cSPawel Biernacki if (V_ip6_log_cannot_forward && ip6_log_ratelimit()) {
225686cdd19SJun-ichiro itojun Hagino log(LOG_DEBUG,
226686cdd19SJun-ichiro itojun Hagino "cannot forward "
227686cdd19SJun-ichiro itojun Hagino "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
2281d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufs, &ip6->ip6_src),
2291d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufd, &ip6->ip6_dst),
230686cdd19SJun-ichiro itojun Hagino ip6->ip6_nxt,
23153a4886dSAlexander V. Chernikov if_name(m->m_pkthdr.rcvif), if_name(nh->nh_ifp));
232686cdd19SJun-ichiro itojun Hagino }
233686cdd19SJun-ichiro itojun Hagino if (mcopy)
234686cdd19SJun-ichiro itojun Hagino icmp6_error(mcopy, ICMP6_DST_UNREACH,
235686cdd19SJun-ichiro itojun Hagino ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
23609f8c3ffSBjoern A. Zeeb goto bad;
237686cdd19SJun-ichiro itojun Hagino }
238686cdd19SJun-ichiro itojun Hagino
239a1f7e5f8SHajimu UMEMOTO /*
240a1f7e5f8SHajimu UMEMOTO * Destination scope check: if a packet is going to break the scope
241a1f7e5f8SHajimu UMEMOTO * zone of packet's destination address, discard it. This case should
242a1f7e5f8SHajimu UMEMOTO * usually be prevented by appropriately-configured routing table, but
243a1f7e5f8SHajimu UMEMOTO * we need an explicit check because we may mistakenly forward the
244a1f7e5f8SHajimu UMEMOTO * packet to a different zone by (e.g.) a default route.
245a1f7e5f8SHajimu UMEMOTO */
24653a4886dSAlexander V. Chernikov inzone = in6_get_unicast_scopeid(&ip6->ip6_dst, m->m_pkthdr.rcvif);
24753a4886dSAlexander V. Chernikov outzone = in6_get_unicast_scopeid(&ip6->ip6_dst, nh->nh_ifp);
24853a4886dSAlexander V. Chernikov
24953a4886dSAlexander V. Chernikov if (inzone != outzone) {
2509cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_cantforward);
2519cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_badscope);
25209f8c3ffSBjoern A. Zeeb goto bad;
253a1f7e5f8SHajimu UMEMOTO }
254a1f7e5f8SHajimu UMEMOTO
25553a4886dSAlexander V. Chernikov if (nh->nh_flags & NHF_GATEWAY) {
25653a4886dSAlexander V. Chernikov /* Store gateway address in deembedded form */
25753a4886dSAlexander V. Chernikov dst.sin6_addr = nh->gw6_sa.sin6_addr;
25853a4886dSAlexander V. Chernikov dst.sin6_scope_id = ntohs(in6_getscope(&dst.sin6_addr));
25953a4886dSAlexander V. Chernikov in6_clearscope(&dst.sin6_addr);
26053a4886dSAlexander V. Chernikov }
26182cd038dSYoshinobu Inoue
26282cd038dSYoshinobu Inoue /*
26382cd038dSYoshinobu Inoue * If we are to forward the packet using the same interface
26482cd038dSYoshinobu Inoue * as one we got the packet from, perhaps we should send a redirect
26582cd038dSYoshinobu Inoue * to sender to shortcut a hop.
26682cd038dSYoshinobu Inoue * Only send redirect if source is sending directly to us,
26782cd038dSYoshinobu Inoue * and if packet was not source routed (or has any options).
26882cd038dSYoshinobu Inoue * Also, don't send redirect if forwarding using a route
26982cd038dSYoshinobu Inoue * modified by a redirect.
27082cd038dSYoshinobu Inoue */
27153a4886dSAlexander V. Chernikov if (V_ip6_sendredirects && nh->nh_ifp == m->m_pkthdr.rcvif && !srcrt &&
2721e9b8db9SKyle Evans (nh->nh_flags & NHF_REDIRECT) == 0)
27382cd038dSYoshinobu Inoue type = ND_REDIRECT;
27482cd038dSYoshinobu Inoue
27582cd038dSYoshinobu Inoue /*
276686cdd19SJun-ichiro itojun Hagino * Fake scoped addresses. Note that even link-local source or
277686cdd19SJun-ichiro itojun Hagino * destinaion can appear, if the originating node just sends the
278686cdd19SJun-ichiro itojun Hagino * packet to us (without address resolution for the destination).
279686cdd19SJun-ichiro itojun Hagino * Since both icmp6_error and icmp6_redirect_output fill the embedded
28033841545SHajimu UMEMOTO * link identifiers, we can do this stuff after making a copy for
28133841545SHajimu UMEMOTO * returning an error.
282686cdd19SJun-ichiro itojun Hagino */
28353a4886dSAlexander V. Chernikov if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) {
284686cdd19SJun-ichiro itojun Hagino /*
285686cdd19SJun-ichiro itojun Hagino * See corresponding comments in ip6_output.
286686cdd19SJun-ichiro itojun Hagino * XXX: but is it possible that ip6_forward() sends a packet
287686cdd19SJun-ichiro itojun Hagino * to a loopback interface? I don't think so, and thus
288686cdd19SJun-ichiro itojun Hagino * I bark here. (jinmei@kame.net)
289686cdd19SJun-ichiro itojun Hagino * XXX: it is common to route invalid packets to loopback.
290686cdd19SJun-ichiro itojun Hagino * also, the codepath will be visited on use of ::1 in
291686cdd19SJun-ichiro itojun Hagino * rthdr. (itojun)
292686cdd19SJun-ichiro itojun Hagino */
293686cdd19SJun-ichiro itojun Hagino #if 1
294686cdd19SJun-ichiro itojun Hagino if (0)
295686cdd19SJun-ichiro itojun Hagino #else
296686cdd19SJun-ichiro itojun Hagino if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
297686cdd19SJun-ichiro itojun Hagino #endif
298686cdd19SJun-ichiro itojun Hagino {
299686cdd19SJun-ichiro itojun Hagino printf("ip6_forward: outgoing interface is loopback. "
300686cdd19SJun-ichiro itojun Hagino "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
3011d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufs, &ip6->ip6_src),
3021d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6bufd, &ip6->ip6_dst),
303686cdd19SJun-ichiro itojun Hagino ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
30453a4886dSAlexander V. Chernikov if_name(nh->nh_ifp));
305686cdd19SJun-ichiro itojun Hagino }
306686cdd19SJun-ichiro itojun Hagino
30733841545SHajimu UMEMOTO /* we can just use rcvif in forwarding. */
30833841545SHajimu UMEMOTO origifp = m->m_pkthdr.rcvif;
309686cdd19SJun-ichiro itojun Hagino }
310686cdd19SJun-ichiro itojun Hagino else
31153a4886dSAlexander V. Chernikov origifp = nh->nh_ifp;
31233841545SHajimu UMEMOTO /*
31333841545SHajimu UMEMOTO * clear embedded scope identifiers if necessary.
31433841545SHajimu UMEMOTO * in6_clearscope will touch the addresses only when necessary.
31533841545SHajimu UMEMOTO */
31633841545SHajimu UMEMOTO in6_clearscope(&ip6->ip6_src);
31733841545SHajimu UMEMOTO in6_clearscope(&ip6->ip6_dst);
318686cdd19SJun-ichiro itojun Hagino
319c21fd232SAndre Oppermann /* Jump over all PFIL processing if hooks are not active. */
320b252313fSGleb Smirnoff if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
321c21fd232SAndre Oppermann goto pass;
322c21fd232SAndre Oppermann
3238a006adbSBjoern A. Zeeb odst = ip6->ip6_dst;
324effaab88SKristof Provost /* Run through list of hooks for forwarded packets. */
325b52b61c0SKristof Provost if (pfil_mbuf_fwd(V_inet6_pfil_head, &m, nh->nh_ifp,
32614c9a2dbSMateusz Guzik NULL) != PFIL_PASS)
327b252313fSGleb Smirnoff goto freecopy;
3283a4d67f7SDarren Reed ip6 = mtod(m, struct ip6_hdr *);
3293a4d67f7SDarren Reed
3308a006adbSBjoern A. Zeeb /* See if destination IP address was changed by packet filter. */
3318a006adbSBjoern A. Zeeb if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
3328a006adbSBjoern A. Zeeb m->m_flags |= M_SKIP_FIREWALL;
3338a006adbSBjoern A. Zeeb /* If destination is now ourself drop to ip6_input(). */
33431e2e88cSKristof Provost if (in6_localip(&ip6->ip6_dst))
3358a006adbSBjoern A. Zeeb m->m_flags |= M_FASTFWD_OURS;
336af8451a5SAlexander V. Chernikov else {
33753a4886dSAlexander V. Chernikov NH_FREE(nh);
33853a4886dSAlexander V. Chernikov
33953a4886dSAlexander V. Chernikov /* Update address and scopeid. Assume scope is embedded */
34053a4886dSAlexander V. Chernikov dst.sin6_scope_id = ntohs(in6_getscope(&ip6->ip6_dst));
34153a4886dSAlexander V. Chernikov dst.sin6_addr = ip6->ip6_dst;
34253a4886dSAlexander V. Chernikov in6_clearscope(&dst.sin6_addr);
3438a006adbSBjoern A. Zeeb goto again; /* Redo the routing table lookup. */
3448a006adbSBjoern A. Zeeb }
345af8451a5SAlexander V. Chernikov }
3468a006adbSBjoern A. Zeeb
3478a006adbSBjoern A. Zeeb /* See if local, if yes, send it to netisr. */
3488a006adbSBjoern A. Zeeb if (m->m_flags & M_FASTFWD_OURS) {
3498a006adbSBjoern A. Zeeb if (m->m_pkthdr.rcvif == NULL)
3508a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif = V_loif;
351356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3528a006adbSBjoern A. Zeeb m->m_pkthdr.csum_flags |=
353356ab07eSBjoern A. Zeeb CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
3548a006adbSBjoern A. Zeeb m->m_pkthdr.csum_data = 0xffff;
3558a006adbSBjoern A. Zeeb }
35695033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
357a6cff10fSMichael Tuexen if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
3588a006adbSBjoern A. Zeeb m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
3598a006adbSBjoern A. Zeeb #endif
3608a006adbSBjoern A. Zeeb error = netisr_queue(NETISR_IPV6, m);
3618a006adbSBjoern A. Zeeb goto out;
3628a006adbSBjoern A. Zeeb }
3638a006adbSBjoern A. Zeeb /* Or forward to some other address? */
364ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP6_NEXTHOP) &&
365ffdbf9daSAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
36653a4886dSAlexander V. Chernikov struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)(fwd_tag + 1);
36753a4886dSAlexander V. Chernikov
36853a4886dSAlexander V. Chernikov /* Update address and scopeid. Assume scope is embedded */
36953a4886dSAlexander V. Chernikov dst.sin6_scope_id = ntohs(in6_getscope(&gw6->sin6_addr));
37053a4886dSAlexander V. Chernikov dst.sin6_addr = gw6->sin6_addr;
37153a4886dSAlexander V. Chernikov in6_clearscope(&dst.sin6_addr);
37253a4886dSAlexander V. Chernikov
3738a006adbSBjoern A. Zeeb m->m_flags |= M_SKIP_FIREWALL;
374ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP6_NEXTHOP;
3758a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag);
37653a4886dSAlexander V. Chernikov NH_FREE(nh);
37753a4886dSAlexander V. Chernikov goto again;
3788a006adbSBjoern A. Zeeb }
3798a006adbSBjoern A. Zeeb
380c21fd232SAndre Oppermann pass:
38153deb05cSKristof Provost /* See if the size was changed by the packet filter. */
38253a4886dSAlexander V. Chernikov /* TODO: change to nh->nh_mtu */
38353a4886dSAlexander V. Chernikov if (m->m_pkthdr.len > IN6_LINKMTU(nh->nh_ifp)) {
38453a4886dSAlexander V. Chernikov in6_ifstat_inc(nh->nh_ifp, ifs6_in_toobig);
385fcf59617SAndrey V. Elsukov if (mcopy)
386fcf59617SAndrey V. Elsukov icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
38753a4886dSAlexander V. Chernikov IN6_LINKMTU(nh->nh_ifp));
38853deb05cSKristof Provost goto bad;
38953deb05cSKristof Provost }
39053deb05cSKristof Provost
39153a4886dSAlexander V. Chernikov /* Currently LLE layer stores embedded IPv6 addresses */
39253a4886dSAlexander V. Chernikov if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr)) {
39353a4886dSAlexander V. Chernikov in6_set_unicast_scopeid(&dst.sin6_addr, dst.sin6_scope_id);
39453a4886dSAlexander V. Chernikov dst.sin6_scope_id = 0;
39553a4886dSAlexander V. Chernikov }
39653a4886dSAlexander V. Chernikov error = nd6_output_ifp(nh->nh_ifp, origifp, m, &dst, NULL);
39782cd038dSYoshinobu Inoue if (error) {
39853a4886dSAlexander V. Chernikov in6_ifstat_inc(nh->nh_ifp, ifs6_out_discard);
3999cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_cantforward);
40082cd038dSYoshinobu Inoue } else {
4019cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_forward);
40253a4886dSAlexander V. Chernikov in6_ifstat_inc(nh->nh_ifp, ifs6_out_forward);
40382cd038dSYoshinobu Inoue if (type)
4049cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_redirectsent);
40582cd038dSYoshinobu Inoue else {
40682cd038dSYoshinobu Inoue if (mcopy)
40782cd038dSYoshinobu Inoue goto freecopy;
40882cd038dSYoshinobu Inoue }
40982cd038dSYoshinobu Inoue }
4107efe5d92SHajimu UMEMOTO
41182cd038dSYoshinobu Inoue if (mcopy == NULL)
41209f8c3ffSBjoern A. Zeeb goto out;
41382cd038dSYoshinobu Inoue switch (error) {
41482cd038dSYoshinobu Inoue case 0:
41582cd038dSYoshinobu Inoue if (type == ND_REDIRECT) {
41653a4886dSAlexander V. Chernikov icmp6_redirect_output(mcopy, nh);
41709f8c3ffSBjoern A. Zeeb goto out;
41882cd038dSYoshinobu Inoue }
41982cd038dSYoshinobu Inoue goto freecopy;
42082cd038dSYoshinobu Inoue
42182cd038dSYoshinobu Inoue case EMSGSIZE:
42282cd038dSYoshinobu Inoue /* xxx MTU is constant in PPP? */
42382cd038dSYoshinobu Inoue goto freecopy;
42482cd038dSYoshinobu Inoue
42582cd038dSYoshinobu Inoue case ENOBUFS:
42682cd038dSYoshinobu Inoue /* Tell source to slow down like source quench in IP? */
42782cd038dSYoshinobu Inoue goto freecopy;
42882cd038dSYoshinobu Inoue
42982cd038dSYoshinobu Inoue case ENETUNREACH: /* shouldn't happen, checked above */
43082cd038dSYoshinobu Inoue case EHOSTUNREACH:
43182cd038dSYoshinobu Inoue case ENETDOWN:
43282cd038dSYoshinobu Inoue case EHOSTDOWN:
43382cd038dSYoshinobu Inoue default:
43482cd038dSYoshinobu Inoue type = ICMP6_DST_UNREACH;
43582cd038dSYoshinobu Inoue code = ICMP6_DST_UNREACH_ADDR;
43682cd038dSYoshinobu Inoue break;
43782cd038dSYoshinobu Inoue }
43882cd038dSYoshinobu Inoue icmp6_error(mcopy, type, code, 0);
43909f8c3ffSBjoern A. Zeeb goto out;
44082cd038dSYoshinobu Inoue
44182cd038dSYoshinobu Inoue freecopy:
44282cd038dSYoshinobu Inoue m_freem(mcopy);
44309f8c3ffSBjoern A. Zeeb goto out;
44409f8c3ffSBjoern A. Zeeb bad:
44509f8c3ffSBjoern A. Zeeb m_freem(m);
44609f8c3ffSBjoern A. Zeeb out:
44753a4886dSAlexander V. Chernikov if (nh != NULL)
44853a4886dSAlexander V. Chernikov NH_FREE(nh);
44982cd038dSYoshinobu Inoue }
450