1*c5711800Sriastradh /* $NetBSD: ip6_forward.c,v 1.103 2024/06/29 13:00:44 riastradh Exp $ */
2255121cfSitojun /* $KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane Exp $ */
3cd3a345eSthorpej
474d3c214Sitojun /*
574d3c214Sitojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
674d3c214Sitojun * All rights reserved.
774d3c214Sitojun *
874d3c214Sitojun * Redistribution and use in source and binary forms, with or without
974d3c214Sitojun * modification, are permitted provided that the following conditions
1074d3c214Sitojun * are met:
1174d3c214Sitojun * 1. Redistributions of source code must retain the above copyright
1274d3c214Sitojun * notice, this list of conditions and the following disclaimer.
1374d3c214Sitojun * 2. Redistributions in binary form must reproduce the above copyright
1474d3c214Sitojun * notice, this list of conditions and the following disclaimer in the
1574d3c214Sitojun * documentation and/or other materials provided with the distribution.
1674d3c214Sitojun * 3. Neither the name of the project nor the names of its contributors
1774d3c214Sitojun * may be used to endorse or promote products derived from this software
1874d3c214Sitojun * without specific prior written permission.
1974d3c214Sitojun *
2074d3c214Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2174d3c214Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2274d3c214Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2374d3c214Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2474d3c214Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2574d3c214Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2674d3c214Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2774d3c214Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2874d3c214Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2974d3c214Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3074d3c214Sitojun * SUCH DAMAGE.
3174d3c214Sitojun */
3274d3c214Sitojun
334f2ad952Slukem #include <sys/cdefs.h>
34*c5711800Sriastradh __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.103 2024/06/29 13:00:44 riastradh Exp $");
354f2ad952Slukem
361c4a50f1Spooka #ifdef _KERNEL_OPT
373d7916e1Sjoerg #include "opt_gateway.h"
385f09b779Sitojun #include "opt_ipsec.h"
391c4a50f1Spooka #endif
405f09b779Sitojun
4174d3c214Sitojun #include <sys/param.h>
4274d3c214Sitojun #include <sys/systm.h>
4374d3c214Sitojun #include <sys/mbuf.h>
4474d3c214Sitojun #include <sys/errno.h>
4574d3c214Sitojun #include <sys/time.h>
4674d3c214Sitojun #include <sys/kernel.h>
4774d3c214Sitojun #include <sys/syslog.h>
48543e39c0Sozaki-r #include <sys/percpu.h>
4974d3c214Sitojun
5074d3c214Sitojun #include <net/if.h>
5174d3c214Sitojun #include <net/route.h>
52f04a92b1Srmind #include <net/pfil.h>
5374d3c214Sitojun
5474d3c214Sitojun #include <netinet/in.h>
5574d3c214Sitojun #include <netinet/in_var.h>
561a2a1e2bSitojun #include <netinet/ip_var.h>
5790736ab6Sitojun #include <netinet/ip6.h>
5874d3c214Sitojun #include <netinet6/ip6_var.h>
590dd41b37Sthorpej #include <netinet6/ip6_private.h>
6078678b13Srpaulo #include <netinet6/scope6_var.h>
6190736ab6Sitojun #include <netinet/icmp6.h>
62ea861f01Sitojun #include <netinet6/nd6.h>
63ea861f01Sitojun
6427fe772dSchristos #ifdef IPSEC
65e2211411Sdegroote #include <netipsec/ipsec.h>
66e2211411Sdegroote #include <netipsec/ipsec6.h>
67e2211411Sdegroote #include <netipsec/key.h>
68e62bbe68Smaxv #endif
69e2211411Sdegroote
70543e39c0Sozaki-r extern percpu_t *ip6_forward_rt_percpu;
7174d3c214Sitojun
72f04a92b1Srmind extern pfil_head_t *inet6_pfil_hook; /* XXX */
73bdbfdf94Sitojun
74e0b46781Schristos static void __printflike(4, 5)
ip6_cantforward(const struct ip6_hdr * ip6,const struct ifnet * srcifp,const struct ifnet * dstifp,const char * fmt,...)75cb7e0235Schristos ip6_cantforward(const struct ip6_hdr *ip6, const struct ifnet *srcifp,
76cb7e0235Schristos const struct ifnet *dstifp, const char *fmt, ...)
77cb7e0235Schristos {
78cb7e0235Schristos char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
79cb7e0235Schristos char reason[256];
80cb7e0235Schristos va_list ap;
81*c5711800Sriastradh net_stat_ref_t ip6s;
82cb7e0235Schristos
83cb7e0235Schristos /* update statistics */
84cb7e0235Schristos ip6s = IP6_STAT_GETREF();
85*c5711800Sriastradh _NET_STATINC_REF(ip6s, IP6_STAT_CANTFORWARD);
86cb7e0235Schristos if (dstifp)
87*c5711800Sriastradh _NET_STATINC_REF(ip6s, IP6_STAT_BADSCOPE);
88cb7e0235Schristos IP6_STAT_PUTREF();
89cb7e0235Schristos
90cb7e0235Schristos if (dstifp)
91cb7e0235Schristos in6_ifstat_inc(dstifp, ifs6_in_discard);
92cb7e0235Schristos
9355140c19Sozaki-r if (ip6_log_time + ip6_log_interval >= time_uptime)
94cb7e0235Schristos return;
9555140c19Sozaki-r ip6_log_time = time_uptime;
96cb7e0235Schristos
97cb7e0235Schristos va_start(ap, fmt);
98e0b46781Schristos vsnprintf(reason, sizeof(reason), fmt, ap);
99cb7e0235Schristos va_end(ap);
100cb7e0235Schristos
101cb7e0235Schristos log(LOG_DEBUG, "Cannot forward from %s@%s to %s@%s nxt %d (%s)\n",
102cb7e0235Schristos IN6_PRINT(sbuf, &ip6->ip6_src), srcifp ? if_name(srcifp) : "?",
103cb7e0235Schristos IN6_PRINT(dbuf, &ip6->ip6_dst), dstifp ? if_name(dstifp) : "?",
104cb7e0235Schristos ip6->ip6_nxt, reason);
105cb7e0235Schristos }
106cb7e0235Schristos
10774d3c214Sitojun /*
10874d3c214Sitojun * Forward a packet. If some error occurs return the sender
10974d3c214Sitojun * an icmp packet. Note we can't always generate a meaningful
11074d3c214Sitojun * icmp message because icmp doesn't have a large enough repertoire
11174d3c214Sitojun * of codes and types.
11274d3c214Sitojun *
11374d3c214Sitojun * If not forwarding, just drop the packet. This could be confusing
11474d3c214Sitojun * if ipforwarding was zero but some routing protocol was advancing
11574d3c214Sitojun * us as a gateway to somewhere. However, we must let the routing
11674d3c214Sitojun * protocol deal with that.
11774d3c214Sitojun */
11874d3c214Sitojun void
ip6_forward(struct mbuf * m,int srcrt,struct ifnet * rcvif)1194c639cc7Sozaki-r ip6_forward(struct mbuf *m, int srcrt, struct ifnet *rcvif)
12074d3c214Sitojun {
121ea861f01Sitojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
12272f0a6dfSdyoung const struct sockaddr_in6 *dst;
1234c25fb2fSozaki-r struct rtentry *rt = NULL;
124b791f5f7Sitojun int error = 0, type = 0, code = 0;
125ea861f01Sitojun struct mbuf *mcopy = NULL;
126fa5c89d6Sitojun struct ifnet *origifp; /* maybe unnecessary */
127cb7e0235Schristos uint32_t inzone, outzone;
12878678b13Srpaulo struct in6_addr src_in6, dst_in6;
1294c25fb2fSozaki-r struct route *ro = NULL;
13027fe772dSchristos #ifdef IPSEC
131e2211411Sdegroote int needipsec = 0;
1325d61e6c0Schristos struct secpolicy *sp = NULL;
133e2211411Sdegroote #endif
134e2211411Sdegroote
1351a57a79dSjoerg /*
1361a57a79dSjoerg * Clear any in-bound checksum flags for this packet.
1371a57a79dSjoerg */
1381a57a79dSjoerg m->m_pkthdr.csum_flags = 0;
139ea861f01Sitojun
14073a29e35Sitojun /*
14173a29e35Sitojun * Do not forward packets to multicast destination (should be handled
14290dd9967Smaxv * by ip6_mforward()). Do not forward packets with unspecified source.
14390dd9967Smaxv * It was discussed in July 2000, on ipngwg mailing list.
14473a29e35Sitojun */
1451450d6e6Sitojun if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
14673a29e35Sitojun IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
14773a29e35Sitojun IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
148fe6d4275Sozaki-r ip6_cantforward(ip6, rcvif, NULL,
149cb7e0235Schristos ((m->m_flags & (M_BCAST|M_MCAST)) != 0) ? "bcast/mcast" :
150cb7e0235Schristos IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ? "mcast/dst" :
151cb7e0235Schristos "unspec/src");
152fe6d4275Sozaki-r goto drop;
15374d3c214Sitojun }
15474d3c214Sitojun
15574d3c214Sitojun if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
156ea861f01Sitojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
15774d3c214Sitojun icmp6_error(m, ICMP6_TIME_EXCEEDED,
15874d3c214Sitojun ICMP6_TIME_EXCEED_TRANSIT, 0);
159fe6d4275Sozaki-r goto out;
16074d3c214Sitojun }
16174d3c214Sitojun ip6->ip6_hlim -= IPV6_HLIMDEC;
16274d3c214Sitojun
163ea861f01Sitojun /*
164ea861f01Sitojun * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
165ea861f01Sitojun * size of IPv6 + ICMPv6 headers) bytes of the packet in case
166ea861f01Sitojun * we need to generate an ICMP6 message to the src.
167ea861f01Sitojun * Thanks to M_EXT, in most cases copy will not occur.
168ea861f01Sitojun *
169ea861f01Sitojun * It is important to save it before IPsec processing as IPsec
170ea861f01Sitojun * processing may modify the mbuf.
171ea861f01Sitojun */
172eee3723dSmaxv mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
173eee3723dSmaxv M_DONTWAIT);
174ea861f01Sitojun
17527fe772dSchristos #ifdef IPSEC
1765d61e6c0Schristos if (ipsec_used) {
177e2211411Sdegroote /* Check the security policy (SP) for the packet */
178e2211411Sdegroote sp = ipsec6_check_policy(m, NULL, 0, &needipsec, &error);
179e2211411Sdegroote if (error != 0) {
180e2211411Sdegroote /*
181e2211411Sdegroote * Hack: -EINVAL is used to signal that a packet
182e2211411Sdegroote * should be silently discarded. This is typically
183e2211411Sdegroote * because we asked key management for an SA and
184e2211411Sdegroote * it was delayed (e.g. kicked up to IKE).
185e2211411Sdegroote */
186e2211411Sdegroote if (error == -EINVAL)
187e2211411Sdegroote error = 0;
188a7c05638Smaxv m_freem(m);
189c1e00d7dSozaki-r IP6_STATINC(IP6_STAT_IPSECDROP_OUT);
190e2211411Sdegroote goto freecopy;
191e2211411Sdegroote }
1925d61e6c0Schristos }
19390dd9967Smaxv #endif
194e2211411Sdegroote
195e524fb36Sozaki-r ro = rtcache_percpu_getref(ip6_forward_rt_percpu);
1961386ee4aSdyoung if (srcrt) {
19772f0a6dfSdyoung union {
19872f0a6dfSdyoung struct sockaddr dst;
19972f0a6dfSdyoung struct sockaddr_in6 dst6;
20072f0a6dfSdyoung } u;
20172f0a6dfSdyoung
20272f0a6dfSdyoung sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
2034c25fb2fSozaki-r rt = rtcache_lookup(ro, &u.dst);
2044c25fb2fSozaki-r if (rt == NULL) {
2050dd41b37Sthorpej IP6_STATINC(IP6_STAT_NOROUTE);
206ea861f01Sitojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
207ea861f01Sitojun if (mcopy) {
208ea861f01Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH,
20974d3c214Sitojun ICMP6_DST_UNREACH_NOROUTE, 0);
210ea861f01Sitojun }
211fe6d4275Sozaki-r goto drop;
21274d3c214Sitojun }
213543e39c0Sozaki-r } else if ((rt = rtcache_validate(ro)) == NULL &&
214543e39c0Sozaki-r (rt = rtcache_update(ro, 1)) == NULL) {
2151386ee4aSdyoung /*
2161386ee4aSdyoung * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
2171386ee4aSdyoung * ip6->ip6_dst
2181386ee4aSdyoung */
2190dd41b37Sthorpej IP6_STATINC(IP6_STAT_NOROUTE);
2201386ee4aSdyoung /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
2211386ee4aSdyoung if (mcopy) {
2221386ee4aSdyoung icmp6_error(mcopy, ICMP6_DST_UNREACH,
2231386ee4aSdyoung ICMP6_DST_UNREACH_NOROUTE, 0);
2241386ee4aSdyoung }
225fe6d4275Sozaki-r goto drop;
22674d3c214Sitojun }
227543e39c0Sozaki-r dst = satocsin6(rtcache_getdst(ro));
2281450d6e6Sitojun
2291450d6e6Sitojun /*
23078678b13Srpaulo * Source scope check: if a packet can't be delivered to its
23178678b13Srpaulo * destination for the reason that the destination is beyond the scope
23278678b13Srpaulo * of the source address, discard the packet and return an icmp6
23378678b13Srpaulo * destination unreachable error with Code 2 (beyond scope of source
23478678b13Srpaulo * address). We use a local copy of ip6_src, since in6_setscope()
23578678b13Srpaulo * will possibly modify its first argument.
23678678b13Srpaulo * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
2371450d6e6Sitojun */
23878678b13Srpaulo src_in6 = ip6->ip6_src;
239cb7e0235Schristos inzone = outzone = ~0;
240cb7e0235Schristos if (in6_setscope(&src_in6, rt->rt_ifp, &outzone) != 0 ||
241fe6d4275Sozaki-r in6_setscope(&src_in6, rcvif, &inzone) != 0 ||
242cb7e0235Schristos inzone != outzone) {
243fe6d4275Sozaki-r ip6_cantforward(ip6, rcvif, rt->rt_ifp,
244e0b46781Schristos "src[%s] inzone %d outzone %d",
245e0b46781Schristos in6_getscopename(&ip6->ip6_src), inzone, outzone);
2461450d6e6Sitojun if (mcopy)
2471450d6e6Sitojun icmp6_error(mcopy, ICMP6_DST_UNREACH,
2481450d6e6Sitojun ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
249fe6d4275Sozaki-r goto drop;
2501450d6e6Sitojun }
251cb7e0235Schristos
25227fe772dSchristos #ifdef IPSEC
253e2211411Sdegroote /*
25490dd9967Smaxv * If we need to encapsulate the packet, do it here.
25590dd9967Smaxv * ipsec6_process_packet will send the packet using ip6_output.
256e2211411Sdegroote */
257e2211411Sdegroote if (needipsec) {
2585d61e6c0Schristos int s = splsoftnet();
259c535599fSknakahara error = ipsec6_process_packet(m, sp->req, 0);
260e2211411Sdegroote splx(s);
261a7c05638Smaxv /* m is freed */
262e2211411Sdegroote if (mcopy)
263e2211411Sdegroote goto freecopy;
264a7c05638Smaxv goto out;
265e2211411Sdegroote }
266e2211411Sdegroote #endif
267e2211411Sdegroote
26878678b13Srpaulo /*
26978678b13Srpaulo * Destination scope check: if a packet is going to break the scope
27078678b13Srpaulo * zone of packet's destination address, discard it. This case should
27178678b13Srpaulo * usually be prevented by appropriately-configured routing table, but
27278678b13Srpaulo * we need an explicit check because we may mistakenly forward the
27378678b13Srpaulo * packet to a different zone by (e.g.) a default route.
27478678b13Srpaulo */
27578678b13Srpaulo dst_in6 = ip6->ip6_dst;
276cb7e0235Schristos inzone = outzone = ~0;
277fe6d4275Sozaki-r if (in6_setscope(&dst_in6, rcvif, &inzone) != 0 ||
27878678b13Srpaulo in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
27978678b13Srpaulo inzone != outzone) {
280fe6d4275Sozaki-r ip6_cantforward(ip6, rcvif, rt->rt_ifp,
281e0b46781Schristos "dst[%s] inzone %d outzone %d",
282e0b46781Schristos in6_getscopename(&ip6->ip6_dst), inzone, outzone);
283cb7e0235Schristos if (mcopy)
284cb7e0235Schristos icmp6_error(mcopy, ICMP6_DST_UNREACH,
285cb7e0235Schristos ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
286fe6d4275Sozaki-r goto drop;
28778678b13Srpaulo }
28878678b13Srpaulo
289b05648aaSroy if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
2909e214c7fSozaki-r IP6_STATINC(IP6_STAT_TOOBIG);
291ea861f01Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
292b05648aaSroy if (mcopy)
293b05648aaSroy icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
294b05648aaSroy rt->rt_ifp->if_mtu);
295fe6d4275Sozaki-r goto drop;
29674d3c214Sitojun }
29774d3c214Sitojun
29874d3c214Sitojun if (rt->rt_flags & RTF_GATEWAY)
29974d3c214Sitojun dst = (struct sockaddr_in6 *)rt->rt_gateway;
30074d3c214Sitojun
30174d3c214Sitojun /*
30274d3c214Sitojun * If we are to forward the packet using the same interface
30374d3c214Sitojun * as one we got the packet from, perhaps we should send a redirect
30474d3c214Sitojun * to sender to shortcut a hop.
30574d3c214Sitojun * Only send redirect if source is sending directly to us,
30674d3c214Sitojun * and if packet was not source routed (or has any options).
30774d3c214Sitojun * Also, don't send redirect if forwarding using a route
30874d3c214Sitojun * modified by a redirect.
30974d3c214Sitojun */
310fe6d4275Sozaki-r if (rt->rt_ifp == rcvif && !srcrt && ip6_sendredirects &&
31157d1913eSitojun (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
312fc35f336Sitojun if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
313543e39c0Sozaki-r nd6_is_addr_neighbor(satocsin6(rtcache_getdst(ro)),
3145493f188Sdyoung rt->rt_ifp)) {
31557d1913eSitojun /*
31657d1913eSitojun * If the incoming interface is equal to the outgoing
317fc35f336Sitojun * one, the link attached to the interface is
318fc35f336Sitojun * point-to-point, and the IPv6 destination is
319fc35f336Sitojun * regarded as on-link on the link, then it will be
320fc35f336Sitojun * highly probable that the destination address does
321fc35f336Sitojun * not exist on the link and that the packet is going
322fc35f336Sitojun * to loop. Thus, we immediately drop the packet and
323fc35f336Sitojun * send an ICMPv6 error message.
324fc35f336Sitojun * For other routing loops, we dare to let the packet
325fc35f336Sitojun * go to the loop, so that a remote diagnosing host
326fc35f336Sitojun * can detect the loop by traceroute.
32757d1913eSitojun * type/code is based on suggestion by Rich Draves.
32857d1913eSitojun * not sure if it is the best pick.
32957d1913eSitojun */
33057d1913eSitojun icmp6_error(mcopy, ICMP6_DST_UNREACH,
33157d1913eSitojun ICMP6_DST_UNREACH_ADDR, 0);
332fe6d4275Sozaki-r goto drop;
33357d1913eSitojun }
33474d3c214Sitojun type = ND_REDIRECT;
33557d1913eSitojun }
33674d3c214Sitojun
337fa5c89d6Sitojun /*
338fa5c89d6Sitojun * Fake scoped addresses. Note that even link-local source or
33990dd9967Smaxv * destination can appear, if the originating node just sends the
340fa5c89d6Sitojun * packet to us (without address resolution for the destination).
341fa5c89d6Sitojun * Since both icmp6_error and icmp6_redirect_output fill the embedded
342e1f4f779Sitojun * link identifiers, we can do this stuff after making a copy for
343e1f4f779Sitojun * returning an error.
344fa5c89d6Sitojun */
345fa5c89d6Sitojun if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
346fa5c89d6Sitojun /*
347fa5c89d6Sitojun * See corresponding comments in ip6_output.
348fa5c89d6Sitojun * XXX: but is it possible that ip6_forward() sends a packet
349fa5c89d6Sitojun * to a loopback interface? I don't think so, and thus
350fa5c89d6Sitojun * I bark here. (jinmei@kame.net)
3519d853e8aSitojun * XXX: it is common to route invalid packets to loopback.
3523ade2713Sitojun * also, the codepath will be visited on use of ::1 in
3533ade2713Sitojun * rthdr. (itojun)
354fa5c89d6Sitojun */
3553ade2713Sitojun #if 1
3563ade2713Sitojun if (0)
3573ade2713Sitojun #else
3583ade2713Sitojun if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
3593ade2713Sitojun #endif
3603ade2713Sitojun {
36128f4c24cSryo char ip6bufs[INET6_ADDRSTRLEN];
36228f4c24cSryo char ip6bufd[INET6_ADDRSTRLEN];
36328f4c24cSryo
364fa5c89d6Sitojun printf("ip6_forward: outgoing interface is loopback. "
365fa5c89d6Sitojun "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
36635561f6bSchristos IN6_PRINT(ip6bufs, &ip6->ip6_src),
36735561f6bSchristos IN6_PRINT(ip6bufd, &ip6->ip6_dst),
368fe6d4275Sozaki-r ip6->ip6_nxt, if_name(rcvif),
369fa5c89d6Sitojun if_name(rt->rt_ifp));
3709d853e8aSitojun }
371fa5c89d6Sitojun
37292969654Sitojun /* we can just use rcvif in forwarding. */
373fe6d4275Sozaki-r origifp = rcvif;
37490dd9967Smaxv } else {
375fa5c89d6Sitojun origifp = rt->rt_ifp;
37690dd9967Smaxv }
37790dd9967Smaxv
37878678b13Srpaulo /*
37978678b13Srpaulo * clear embedded scope identifiers if necessary.
38078678b13Srpaulo * in6_clearscope will touch the addresses only when necessary.
38178678b13Srpaulo */
38278678b13Srpaulo in6_clearscope(&ip6->ip6_src);
38378678b13Srpaulo in6_clearscope(&ip6->ip6_dst);
384fa5c89d6Sitojun
385bdbfdf94Sitojun /*
386bdbfdf94Sitojun * Run through list of hooks for output packets.
387bdbfdf94Sitojun */
388f04a92b1Srmind if ((error = pfil_run_hooks(inet6_pfil_hook, &m, rt->rt_ifp,
3896d8eb4f9Sozaki-r PFIL_OUT)) != 0) {
3906d8eb4f9Sozaki-r IP6_STATINC(IP6_STAT_PFILDROP_OUT);
391bdbfdf94Sitojun goto senderr;
3926d8eb4f9Sozaki-r }
3936d8eb4f9Sozaki-r if (m == NULL) {
3946d8eb4f9Sozaki-r IP6_STATINC(IP6_STAT_PFILDROP_OUT);
395bdbfdf94Sitojun goto freecopy;
3966d8eb4f9Sozaki-r }
397bdbfdf94Sitojun ip6 = mtod(m, struct ip6_hdr *);
398bdbfdf94Sitojun
3993f909d17Sozaki-r error = ip6_if_output(rt->rt_ifp, origifp, m, dst, rt);
400ea861f01Sitojun if (error) {
401ea861f01Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
4020dd41b37Sthorpej IP6_STATINC(IP6_STAT_CANTFORWARD);
403ea861f01Sitojun } else {
4040dd41b37Sthorpej IP6_STATINC(IP6_STAT_FORWARD);
405ea861f01Sitojun in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
40674d3c214Sitojun if (type)
4070dd41b37Sthorpej IP6_STATINC(IP6_STAT_REDIRECTSENT);
40874d3c214Sitojun else {
4098aa640daSliamjfoy #ifdef GATEWAY
4104c25fb2fSozaki-r /* Need to release rt here */
4114c25fb2fSozaki-r rtcache_unref(rt, ro);
4124c25fb2fSozaki-r rt = NULL;
4138c09e9f9Sozaki-r if (mcopy->m_flags & M_CANFASTFWD)
414a29d76a1Sozaki-r ip6flow_create(ro, mcopy);
4158aa640daSliamjfoy #endif
41674d3c214Sitojun if (mcopy)
41774d3c214Sitojun goto freecopy;
41874d3c214Sitojun }
41974d3c214Sitojun }
420bdbfdf94Sitojun
421bdbfdf94Sitojun senderr:
42274d3c214Sitojun if (mcopy == NULL)
423fe6d4275Sozaki-r goto out;
42474d3c214Sitojun switch (error) {
42574d3c214Sitojun case 0:
42674d3c214Sitojun if (type == ND_REDIRECT) {
42774d3c214Sitojun icmp6_redirect_output(mcopy, rt);
428fe6d4275Sozaki-r goto out;
42974d3c214Sitojun }
43074d3c214Sitojun goto freecopy;
43174d3c214Sitojun
43274d3c214Sitojun case EMSGSIZE:
43374d3c214Sitojun /* xxx MTU is constant in PPP? */
43474d3c214Sitojun goto freecopy;
43574d3c214Sitojun
43674d3c214Sitojun case ENOBUFS:
43774d3c214Sitojun /* Tell source to slow down like source quench in IP? */
43874d3c214Sitojun goto freecopy;
43974d3c214Sitojun
44074d3c214Sitojun case ENETUNREACH: /* shouldn't happen, checked above */
44174d3c214Sitojun case EHOSTUNREACH:
44274d3c214Sitojun case ENETDOWN:
44374d3c214Sitojun case EHOSTDOWN:
44474d3c214Sitojun default:
44574d3c214Sitojun type = ICMP6_DST_UNREACH;
44674d3c214Sitojun code = ICMP6_DST_UNREACH_ADDR;
44774d3c214Sitojun break;
44874d3c214Sitojun }
44974d3c214Sitojun icmp6_error(mcopy, type, code, 0);
450fe6d4275Sozaki-r goto out;
45174d3c214Sitojun
45274d3c214Sitojun freecopy:
45374d3c214Sitojun m_freem(mcopy);
454fe6d4275Sozaki-r goto out;
45590dd9967Smaxv
456fe6d4275Sozaki-r drop:
457fe6d4275Sozaki-r m_freem(m);
45890dd9967Smaxv
459fe6d4275Sozaki-r out:
460808b116aSozaki-r #ifdef IPSEC
461808b116aSozaki-r if (sp != NULL)
4620c084e85Sozaki-r KEY_SP_UNREF(&sp);
463808b116aSozaki-r #endif
4644c25fb2fSozaki-r rtcache_unref(rt, ro);
4654c25fb2fSozaki-r if (ro != NULL)
466e524fb36Sozaki-r rtcache_percpu_putref(ip6_forward_rt_percpu);
467ea861f01Sitojun return;
46874d3c214Sitojun }
469