123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /* $OpenBSD: ip6_forward.c,v 1.79 2015/07/16 21:14:21 mpi Exp $ */
- /* $KAME: ip6_forward.c,v 1.75 2001/06/29 12:42:13 jinmei Exp $ */
- /*
- * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the project nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include "pf.h"
- #include <sys/param.h>
- #include <sys/systm.h>
- #include <sys/mbuf.h>
- #include <sys/domain.h>
- #include <sys/protosw.h>
- #include <sys/socket.h>
- #include <sys/errno.h>
- #include <sys/time.h>
- #include <sys/kernel.h>
- #include <sys/syslog.h>
- #include <net/if.h>
- #include <net/if_var.h>
- #include <net/if_enc.h>
- #include <net/route.h>
- #include <netinet/in.h>
- #include <netinet/ip_var.h>
- #include <netinet6/in6_var.h>
- #include <netinet/ip6.h>
- #include <netinet6/ip6_var.h>
- #include <netinet/icmp6.h>
- #include <netinet6/nd6.h>
- #if NPF > 0
- #include <net/pfvar.h>
- #endif
- #ifdef IPSEC
- #include <netinet/ip_ipsp.h>
- #include <netinet/ip_ah.h>
- #include <netinet/ip_esp.h>
- #include <netinet/udp.h>
- #include <netinet/tcp.h>
- #endif
- struct route_in6 ip6_forward_rt;
- /*
- * Forward a packet. If some error occurs return the sender
- * an icmp packet. Note we can't always generate a meaningful
- * icmp message because icmp doesn't have a large enough repertoire
- * of codes and types.
- *
- * If not forwarding, just drop the packet. This could be confusing
- * if ipforwarding was zero but some routing protocol was advancing
- * us as a gateway to somewhere. However, we must let the routing
- * protocol deal with that.
- *
- */
- void
- ip6_forward(struct mbuf *m, int srcrt)
- {
- struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
- struct sockaddr_in6 *dst;
- struct rtentry *rt;
- int error = 0, type = 0, code = 0;
- struct mbuf *mcopy = NULL;
- #ifdef IPSEC
- u_int8_t sproto = 0;
- struct m_tag *mtag;
- union sockaddr_union sdst;
- struct tdb_ident *tdbi;
- u_int32_t sspi;
- struct tdb *tdb;
- #if NPF > 0
- struct ifnet *encif;
- #endif
- #endif /* IPSEC */
- u_int rtableid = 0;
- char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
- /*
- * Do not forward packets to multicast destination (should be handled
- * by ip6_mforward().
- * Do not forward packets with unspecified source. It was discussed
- * in July 2000, on ipngwg mailing list.
- */
- if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
- IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
- IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
- ip6stat.ip6s_cantforward++;
- /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
- if (ip6_log_time + ip6_log_interval < time_second) {
- ip6_log_time = time_second;
- inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
- inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
- log(LOG_DEBUG,
- "cannot forward "
- "from %s to %s nxt %d received on inteface %u\n",
- src6, dst6,
- ip6->ip6_nxt,
- m->m_pkthdr.ph_ifidx);
- }
- m_freem(m);
- return;
- }
- if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
- /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
- icmp6_error(m, ICMP6_TIME_EXCEEDED,
- ICMP6_TIME_EXCEED_TRANSIT, 0);
- return;
- }
- ip6->ip6_hlim -= IPV6_HLIMDEC;
- /*
- * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
- * size of IPv6 + ICMPv6 headers) bytes of the packet in case
- * we need to generate an ICMP6 message to the src.
- * Thanks to M_EXT, in most cases copy will not occur.
- *
- * It is important to save it before IPsec processing as IPsec
- * processing may modify the mbuf.
- */
- mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
- M_NOWAIT);
- #if NPF > 0
- reroute:
- #endif
- #ifdef IPSEC
- if (!ipsec_in_use)
- goto done_spd;
- /*
- * Check if there was an outgoing SA bound to the flow
- * from a transport protocol.
- */
- /* Do we have any pending SAs to apply ? */
- tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
- &error, IPSP_DIRECTION_OUT, NULL, NULL, 0);
- if (tdb == NULL) {
- if (error == 0) {
- /*
- * No IPsec processing required, we'll just send the
- * packet out.
- */
- sproto = 0;
- /* Fall through to routing/multicast handling */
- } else {
- /*
- * -EINVAL is used to indicate that the packet should
- * be silently dropped, typically because we've asked
- * key management for an SA.
- */
- if (error == -EINVAL) /* Should silently drop packet */
- error = 0;
- m_freem(m);
- goto freecopy;
- }
- } else {
- /* Loop detection */
- for (mtag = m_tag_first(m); mtag != NULL;
- mtag = m_tag_next(m, mtag)) {
- if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
- continue;
- tdbi = (struct tdb_ident *)(mtag + 1);
- if (tdbi->spi == tdb->tdb_spi &&
- tdbi->proto == tdb->tdb_sproto &&
- tdbi->rdomain == tdb->tdb_rdomain &&
- !bcmp(&tdbi->dst, &tdb->tdb_dst,
- sizeof(union sockaddr_union))) {
- sproto = 0; /* mark as no-IPsec-needed */
- goto done_spd;
- }
- }
- /* We need to do IPsec */
- bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst));
- sspi = tdb->tdb_spi;
- sproto = tdb->tdb_sproto;
- }
- /* Fall through to the routing/multicast handling code */
- done_spd:
- #endif /* IPSEC */
- #if NPF > 0
- rtableid = m->m_pkthdr.ph_rtableid;
- #endif
- dst = &ip6_forward_rt.ro_dst;
- if (!srcrt) {
- /*
- * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
- */
- if (ip6_forward_rt.ro_rt == NULL ||
- (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 ||
- ip6_forward_rt.ro_tableid != rtableid) {
- if (ip6_forward_rt.ro_rt) {
- rtfree(ip6_forward_rt.ro_rt);
- ip6_forward_rt.ro_rt = NULL;
- }
- /* this probably fails but give it a try again */
- ip6_forward_rt.ro_tableid = rtableid;
- ip6_forward_rt.ro_rt = rtalloc_mpath(
- sin6tosa(&ip6_forward_rt.ro_dst),
- &ip6->ip6_src.s6_addr32[0],
- ip6_forward_rt.ro_tableid);
- }
- if (ip6_forward_rt.ro_rt == NULL) {
- ip6stat.ip6s_noroute++;
- /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
- if (mcopy) {
- icmp6_error(mcopy, ICMP6_DST_UNREACH,
- ICMP6_DST_UNREACH_NOROUTE, 0);
- }
- m_freem(m);
- return;
- }
- } else if (ip6_forward_rt.ro_rt == NULL ||
- (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 ||
- !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr) ||
- ip6_forward_rt.ro_tableid != rtableid) {
- if (ip6_forward_rt.ro_rt) {
- rtfree(ip6_forward_rt.ro_rt);
- ip6_forward_rt.ro_rt = NULL;
- }
- bzero(dst, sizeof(*dst));
- dst->sin6_len = sizeof(struct sockaddr_in6);
- dst->sin6_family = AF_INET6;
- dst->sin6_addr = ip6->ip6_dst;
- ip6_forward_rt.ro_tableid = rtableid;
- ip6_forward_rt.ro_rt = rtalloc_mpath(
- sin6tosa(&ip6_forward_rt.ro_dst),
- &ip6->ip6_src.s6_addr32[0],
- ip6_forward_rt.ro_tableid);
- if (ip6_forward_rt.ro_rt == NULL) {
- ip6stat.ip6s_noroute++;
- /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
- if (mcopy) {
- icmp6_error(mcopy, ICMP6_DST_UNREACH,
- ICMP6_DST_UNREACH_NOROUTE, 0);
- }
- m_freem(m);
- return;
- }
- }
- rt = ip6_forward_rt.ro_rt;
- /*
- * Scope check: if a packet can't be delivered to its destination
- * for the reason that the destination is beyond the scope of the
- * source address, discard the packet and return an icmp6 destination
- * unreachable error with Code 2 (beyond scope of source address).
- * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
- */
- if (in6_addr2scopeid(m->m_pkthdr.ph_ifidx, &ip6->ip6_src) !=
- in6_addr2scopeid(rt->rt_ifp->if_index, &ip6->ip6_src)) {
- ip6stat.ip6s_cantforward++;
- ip6stat.ip6s_badscope++;
- in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
- if (ip6_log_time + ip6_log_interval < time_second) {
- ip6_log_time = time_second;
- inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
- inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
- log(LOG_DEBUG,
- "cannot forward "
- "src %s, dst %s, nxt %d, rcvif %u, outif %u\n",
- src6, dst6,
- ip6->ip6_nxt,
- m->m_pkthdr.ph_ifidx, rt->rt_ifp->if_index);
- }
- if (mcopy)
- icmp6_error(mcopy, ICMP6_DST_UNREACH,
- ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
- m_freem(m);
- goto freert;
- }
- #ifdef IPSEC
- /*
- * Check if the packet needs encapsulation.
- * ipsp_process_packet will never come back to here.
- * XXX ipsp_process_packet() calls ip6_output(), and there'll be no
- * PMTU notification. is it okay?
- */
- if (sproto != 0) {
- tdb = gettdb(rtable_l2(m->m_pkthdr.ph_rtableid),
- sspi, &sdst, sproto);
- if (tdb == NULL) {
- error = EHOSTUNREACH;
- m_freem(m);
- goto senderr; /*XXX*/
- }
- #if NPF > 0
- if ((encif = enc_getif(tdb->tdb_rdomain,
- tdb->tdb_tap)) == NULL ||
- pf_test(AF_INET6, PF_FWD, encif, &m) != PF_PASS) {
- error = EHOSTUNREACH;
- m_freem(m);
- goto senderr;
- }
- if (m == NULL)
- goto senderr;
- ip6 = mtod(m, struct ip6_hdr *);
- /*
- * PF_TAG_REROUTE handling or not...
- * Packet is entering IPsec so the routing is
- * already overruled by the IPsec policy.
- * Until now the change was not reconsidered.
- * What's the behaviour?
- */
- #endif
- in6_proto_cksum_out(m, encif);
- m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
- /* Callee frees mbuf */
- error = ipsp_process_packet(m, tdb, AF_INET6, 0);
- m_freem(mcopy);
- goto freert;
- }
- #endif /* IPSEC */
- if (rt->rt_flags & RTF_GATEWAY)
- dst = satosin6(rt->rt_gateway);
- /*
- * If we are to forward the packet using the same interface
- * as one we got the packet from, perhaps we should send a redirect
- * to sender to shortcut a hop.
- * Only send redirect if source is sending directly to us,
- * and if packet was not source routed (or has any options).
- * Also, don't send redirect if forwarding using a route
- * modified by a redirect.
- */
- if (rt->rt_ifp->if_index == m->m_pkthdr.ph_ifidx && !srcrt &&
- ip6_sendredirects &&
- (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
- if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
- nd6_is_addr_neighbor(&ip6_forward_rt.ro_dst, rt->rt_ifp)) {
- /*
- * If the incoming interface is equal to the outgoing
- * one, the link attached to the interface is
- * point-to-point, and the IPv6 destination is
- * regarded as on-link on the link, then it will be
- * highly probable that the destination address does
- * not exist on the link and that the packet is going
- * to loop. Thus, we immediately drop the packet and
- * send an ICMPv6 error message.
- * For other routing loops, we dare to let the packet
- * go to the loop, so that a remote diagnosing host
- * can detect the loop by traceroute.
- * type/code is based on suggestion by Rich Draves.
- * not sure if it is the best pick.
- */
- if (mcopy)
- icmp6_error(mcopy, ICMP6_DST_UNREACH,
- ICMP6_DST_UNREACH_ADDR, 0);
- m_freem(m);
- goto freert;
- }
- type = ND_REDIRECT;
- }
- /*
- * Fake scoped addresses. Note that even link-local source or
- * destinaion can appear, if the originating node just sends the
- * packet to us (without address resolution for the destination).
- * Since both icmp6_error and icmp6_redirect_output fill the embedded
- * link identifiers, we can do this stuff after making a copy for
- * returning an error.
- */
- if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
- ip6->ip6_src.s6_addr16[1] = 0;
- if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
- ip6->ip6_dst.s6_addr16[1] = 0;
- #if NPF > 0
- if (pf_test(AF_INET6, PF_FWD, rt->rt_ifp, &m) != PF_PASS) {
- m_freem(m);
- goto senderr;
- }
- if (m == NULL)
- goto senderr;
- ip6 = mtod(m, struct ip6_hdr *);
- if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
- (PF_TAG_REROUTE | PF_TAG_GENERATED)) {
- /* already rerun the route lookup, go on */
- m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
- } else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
- /* tag as generated to skip over pf_test on rerun */
- m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
- srcrt = 1;
- goto reroute;
- }
- #endif
- in6_proto_cksum_out(m, rt->rt_ifp);
- /* Check the size after pf_test to give pf a chance to refragment. */
- if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
- in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
- if (mcopy) {
- u_long mtu;
- mtu = IN6_LINKMTU(rt->rt_ifp);
- icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
- }
- m_freem(m);
- goto freert;
- }
- error = nd6_output(rt->rt_ifp, m, dst, rt);
- if (error) {
- in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
- ip6stat.ip6s_cantforward++;
- } else {
- ip6stat.ip6s_forward++;
- in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
- if (type)
- ip6stat.ip6s_redirectsent++;
- else {
- if (mcopy)
- goto freecopy;
- }
- }
- #if NPF > 0 || defined(IPSEC)
- senderr:
- #endif
- if (mcopy == NULL)
- goto freert;
- switch (error) {
- case 0:
- if (type == ND_REDIRECT) {
- icmp6_redirect_output(mcopy, rt);
- goto freert;
- }
- goto freecopy;
- case EMSGSIZE:
- /* xxx MTU is constant in PPP? */
- goto freecopy;
- case ENOBUFS:
- /* Tell source to slow down like source quench in IP? */
- goto freecopy;
- case ENETUNREACH: /* shouldn't happen, checked above */
- case EHOSTUNREACH:
- case ENETDOWN:
- case EHOSTDOWN:
- default:
- type = ICMP6_DST_UNREACH;
- code = ICMP6_DST_UNREACH_ADDR;
- break;
- }
- icmp6_error(mcopy, type, code, 0);
- goto freert;
- freecopy:
- m_freem(mcopy);
- freert:
- #ifndef SMALL_KERNEL
- if (ip6_multipath && ip6_forward_rt.ro_rt &&
- (ip6_forward_rt.ro_rt->rt_flags & RTF_MPATH)) {
- rtfree(ip6_forward_rt.ro_rt);
- ip6_forward_rt.ro_rt = NULL;
- }
- #endif
- return;
- }
|