Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.67
      1  1.67     joerg /*	$NetBSD: ip6_forward.c,v 1.67 2009/11/11 22:19:22 joerg Exp $	*/
      2  1.32    itojun /*	$KAME: ip6_forward.c,v 1.109 2002/09/11 08:10:17 sakane Exp $	*/
      3   1.3   thorpej 
      4   1.2    itojun /*
      5   1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.2    itojun  * All rights reserved.
      7  1.10    itojun  *
      8   1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.2    itojun  * modification, are permitted provided that the following conditions
     10   1.2    itojun  * are met:
     11   1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.2    itojun  *    may be used to endorse or promote products derived from this software
     18   1.2    itojun  *    without specific prior written permission.
     19  1.10    itojun  *
     20   1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2    itojun  * SUCH DAMAGE.
     31   1.2    itojun  */
     32  1.26     lukem 
     33  1.26     lukem #include <sys/cdefs.h>
     34  1.67     joerg __KERNEL_RCSID(0, "$NetBSD: ip6_forward.c,v 1.67 2009/11/11 22:19:22 joerg Exp $");
     35   1.2    itojun 
     36  1.15    itojun #include "opt_ipsec.h"
     37  1.20    itojun #include "opt_pfil_hooks.h"
     38  1.15    itojun 
     39   1.2    itojun #include <sys/param.h>
     40   1.2    itojun #include <sys/systm.h>
     41   1.2    itojun #include <sys/malloc.h>
     42   1.2    itojun #include <sys/mbuf.h>
     43   1.2    itojun #include <sys/domain.h>
     44   1.2    itojun #include <sys/protosw.h>
     45   1.2    itojun #include <sys/socket.h>
     46   1.2    itojun #include <sys/errno.h>
     47   1.2    itojun #include <sys/time.h>
     48   1.2    itojun #include <sys/kernel.h>
     49   1.2    itojun #include <sys/syslog.h>
     50   1.2    itojun 
     51   1.2    itojun #include <net/if.h>
     52   1.2    itojun #include <net/route.h>
     53   1.2    itojun 
     54   1.2    itojun #include <netinet/in.h>
     55   1.2    itojun #include <netinet/in_var.h>
     56   1.7    itojun #include <netinet/ip_var.h>
     57   1.8    itojun #include <netinet/ip6.h>
     58   1.2    itojun #include <netinet6/ip6_var.h>
     59  1.64   thorpej #include <netinet6/ip6_private.h>
     60  1.47    rpaulo #include <netinet6/scope6_var.h>
     61   1.8    itojun #include <netinet/icmp6.h>
     62   1.5    itojun #include <netinet6/nd6.h>
     63   1.5    itojun 
     64  1.15    itojun #ifdef IPSEC
     65   1.5    itojun #include <netinet6/ipsec.h>
     66  1.65   thorpej #include <netinet6/ipsec_private.h>
     67   1.5    itojun #include <netkey/key.h>
     68  1.15    itojun #endif /* IPSEC */
     69   1.5    itojun 
     70  1.54  degroote #ifdef FAST_IPSEC
     71  1.54  degroote #include <netipsec/ipsec.h>
     72  1.54  degroote #include <netipsec/ipsec6.h>
     73  1.54  degroote #include <netipsec/key.h>
     74  1.54  degroote #include <netipsec/xform.h>
     75  1.54  degroote #endif /* FAST_IPSEC */
     76  1.54  degroote 
     77  1.20    itojun #ifdef PFIL_HOOKS
     78  1.20    itojun #include <net/pfil.h>
     79  1.20    itojun #endif
     80  1.20    itojun 
     81   1.5    itojun #include <net/net_osdep.h>
     82   1.2    itojun 
     83  1.57    dyoung struct	route ip6_forward_rt;
     84   1.2    itojun 
     85  1.20    itojun #ifdef PFIL_HOOKS
     86  1.20    itojun extern struct pfil_head inet6_pfil_hook;	/* XXX */
     87  1.20    itojun #endif
     88  1.20    itojun 
     89   1.2    itojun /*
     90   1.2    itojun  * Forward a packet.  If some error occurs return the sender
     91   1.2    itojun  * an icmp packet.  Note we can't always generate a meaningful
     92   1.2    itojun  * icmp message because icmp doesn't have a large enough repertoire
     93   1.2    itojun  * of codes and types.
     94   1.2    itojun  *
     95   1.2    itojun  * If not forwarding, just drop the packet.  This could be confusing
     96   1.2    itojun  * if ipforwarding was zero but some routing protocol was advancing
     97   1.2    itojun  * us as a gateway to somewhere.  However, we must let the routing
     98   1.2    itojun  * protocol deal with that.
     99   1.2    itojun  *
    100   1.2    itojun  */
    101   1.2    itojun 
    102   1.2    itojun void
    103  1.58  christos ip6_forward(struct mbuf *m, int srcrt)
    104   1.2    itojun {
    105   1.5    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    106  1.57    dyoung 	const struct sockaddr_in6 *dst;
    107  1.18    itojun 	struct rtentry *rt;
    108  1.42    itojun 	int error = 0, type = 0, code = 0;
    109   1.5    itojun 	struct mbuf *mcopy = NULL;
    110  1.10    itojun 	struct ifnet *origifp;	/* maybe unnecessary */
    111  1.47    rpaulo 	u_int32_t inzone, outzone;
    112  1.47    rpaulo 	struct in6_addr src_in6, dst_in6;
    113  1.15    itojun #ifdef IPSEC
    114   1.5    itojun 	struct secpolicy *sp = NULL;
    115  1.37    itojun 	int ipsecrt = 0;
    116   1.5    itojun #endif
    117  1.54  degroote #ifdef FAST_IPSEC
    118  1.54  degroote     struct secpolicy *sp = NULL;
    119  1.54  degroote     int needipsec = 0;
    120  1.54  degroote     int s;
    121  1.54  degroote #endif
    122  1.54  degroote 
    123  1.67     joerg 	/*
    124  1.67     joerg 	 * Clear any in-bound checksum flags for this packet.
    125  1.67     joerg 	 */
    126  1.67     joerg 	m->m_pkthdr.csum_flags = 0;
    127   1.5    itojun 
    128  1.15    itojun #ifdef IPSEC
    129   1.5    itojun 	/*
    130   1.5    itojun 	 * Check AH/ESP integrity.
    131   1.5    itojun 	 */
    132   1.5    itojun 	/*
    133   1.5    itojun 	 * Don't increment ip6s_cantforward because this is the check
    134   1.5    itojun 	 * before forwarding packet actually.
    135   1.5    itojun 	 */
    136   1.5    itojun 	if (ipsec6_in_reject(m, NULL)) {
    137  1.65   thorpej 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
    138   1.5    itojun 		m_freem(m);
    139   1.5    itojun 		return;
    140   1.5    itojun 	}
    141  1.25    itojun #endif /* IPSEC */
    142   1.2    itojun 
    143  1.16    itojun 	/*
    144  1.16    itojun 	 * Do not forward packets to multicast destination (should be handled
    145  1.16    itojun 	 * by ip6_mforward().
    146  1.16    itojun 	 * Do not forward packets with unspecified source.  It was discussed
    147  1.16    itojun 	 * in July 2000, on ipngwg mailing list.
    148  1.16    itojun 	 */
    149   1.9    itojun 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
    150  1.16    itojun 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    151  1.16    itojun 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
    152  1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    153   1.5    itojun 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    154  1.48    kardel 		if (ip6_log_time + ip6_log_interval < time_second) {
    155  1.48    kardel 			ip6_log_time = time_second;
    156   1.2    itojun 			log(LOG_DEBUG,
    157   1.2    itojun 			    "cannot forward "
    158   1.2    itojun 			    "from %s to %s nxt %d received on %s\n",
    159   1.9    itojun 			    ip6_sprintf(&ip6->ip6_src),
    160   1.9    itojun 			    ip6_sprintf(&ip6->ip6_dst),
    161   1.2    itojun 			    ip6->ip6_nxt,
    162   1.5    itojun 			    if_name(m->m_pkthdr.rcvif));
    163   1.2    itojun 		}
    164   1.2    itojun 		m_freem(m);
    165   1.2    itojun 		return;
    166   1.2    itojun 	}
    167   1.2    itojun 
    168   1.2    itojun 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
    169   1.5    itojun 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
    170   1.2    itojun 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
    171   1.2    itojun 				ICMP6_TIME_EXCEED_TRANSIT, 0);
    172   1.2    itojun 		return;
    173   1.2    itojun 	}
    174   1.2    itojun 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    175   1.2    itojun 
    176   1.5    itojun 	/*
    177   1.5    itojun 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
    178   1.5    itojun 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
    179   1.5    itojun 	 * we need to generate an ICMP6 message to the src.
    180   1.5    itojun 	 * Thanks to M_EXT, in most cases copy will not occur.
    181   1.5    itojun 	 *
    182   1.5    itojun 	 * It is important to save it before IPsec processing as IPsec
    183   1.5    itojun 	 * processing may modify the mbuf.
    184   1.5    itojun 	 */
    185   1.5    itojun 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
    186   1.5    itojun 
    187  1.15    itojun #ifdef IPSEC
    188   1.5    itojun 	/* get a security policy for this packet */
    189  1.35    itojun 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
    190  1.35    itojun 	    IP_FORWARDING, &error);
    191   1.5    itojun 	if (sp == NULL) {
    192  1.65   thorpej 		IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL);
    193  1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    194   1.5    itojun 		if (mcopy) {
    195   1.5    itojun #if 0
    196   1.5    itojun 			/* XXX: what icmp ? */
    197   1.5    itojun #else
    198   1.5    itojun 			m_freem(mcopy);
    199   1.5    itojun #endif
    200   1.5    itojun 		}
    201   1.5    itojun 		m_freem(m);
    202   1.5    itojun 		return;
    203   1.5    itojun 	}
    204   1.5    itojun 
    205   1.5    itojun 	error = 0;
    206   1.5    itojun 
    207   1.5    itojun 	/* check policy */
    208   1.5    itojun 	switch (sp->policy) {
    209   1.5    itojun 	case IPSEC_POLICY_DISCARD:
    210   1.5    itojun 		/*
    211   1.5    itojun 		 * This packet is just discarded.
    212   1.5    itojun 		 */
    213  1.65   thorpej 		IPSEC6_STATINC(IPSEC_STAT_OUT_POLVIO);
    214  1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    215   1.5    itojun 		key_freesp(sp);
    216   1.5    itojun 		if (mcopy) {
    217   1.5    itojun #if 0
    218   1.5    itojun 			/* XXX: what icmp ? */
    219   1.5    itojun #else
    220   1.5    itojun 			m_freem(mcopy);
    221   1.5    itojun #endif
    222   1.5    itojun 		}
    223   1.5    itojun 		m_freem(m);
    224   1.5    itojun 		return;
    225   1.5    itojun 
    226   1.5    itojun 	case IPSEC_POLICY_BYPASS:
    227   1.5    itojun 	case IPSEC_POLICY_NONE:
    228   1.5    itojun 		/* no need to do IPsec. */
    229   1.5    itojun 		key_freesp(sp);
    230   1.5    itojun 		goto skip_ipsec;
    231  1.13    itojun 
    232   1.5    itojun 	case IPSEC_POLICY_IPSEC:
    233   1.5    itojun 		if (sp->req == NULL) {
    234   1.5    itojun 			/* XXX should be panic ? */
    235   1.5    itojun 			printf("ip6_forward: No IPsec request specified.\n");
    236  1.64   thorpej 			IP6_STATINC(IP6_STAT_CANTFORWARD);
    237   1.5    itojun 			key_freesp(sp);
    238   1.5    itojun 			if (mcopy) {
    239   1.5    itojun #if 0
    240   1.5    itojun 				/* XXX: what icmp ? */
    241   1.5    itojun #else
    242   1.5    itojun 				m_freem(mcopy);
    243   1.5    itojun #endif
    244   1.5    itojun 			}
    245   1.5    itojun 			m_freem(m);
    246   1.5    itojun 			return;
    247   1.5    itojun 		}
    248   1.5    itojun 		/* do IPsec */
    249   1.5    itojun 		break;
    250   1.5    itojun 
    251   1.5    itojun 	case IPSEC_POLICY_ENTRUST:
    252   1.5    itojun 	default:
    253   1.5    itojun 		/* should be panic ?? */
    254   1.5    itojun 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
    255   1.5    itojun 		key_freesp(sp);
    256   1.5    itojun 		goto skip_ipsec;
    257   1.5    itojun 	}
    258   1.5    itojun 
    259   1.5    itojun     {
    260  1.32    itojun 	struct ipsecrequest *isr = NULL;
    261   1.5    itojun 	struct ipsec_output_state state;
    262   1.5    itojun 
    263  1.32    itojun 	/*
    264  1.32    itojun 	 * when the kernel forwards a packet, it is not proper to apply
    265  1.32    itojun 	 * IPsec transport mode to the packet is not proper.  this check
    266  1.32    itojun 	 * avoid from this.
    267  1.32    itojun 	 * at present, if there is even a transport mode SA request in the
    268  1.32    itojun 	 * security policy, the kernel does not apply IPsec to the packet.
    269  1.32    itojun 	 * this check is not enough because the following case is valid.
    270  1.32    itojun 	 *      ipsec esp/tunnel/xxx-xxx/require esp/transport//require;
    271  1.32    itojun 	 */
    272  1.32    itojun 	for (isr = sp->req; isr; isr = isr->next) {
    273  1.41    itojun 		if (isr->saidx.mode == IPSEC_MODE_ANY)
    274  1.41    itojun 			goto doipsectunnel;
    275  1.41    itojun 		if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
    276  1.41    itojun 			goto doipsectunnel;
    277  1.32    itojun 	}
    278  1.41    itojun 
    279  1.41    itojun 	/*
    280  1.41    itojun 	 * if there's no need for tunnel mode IPsec, skip.
    281  1.41    itojun 	 */
    282  1.41    itojun 	if (!isr)
    283  1.41    itojun 		goto skip_ipsec;
    284  1.44     perry 
    285  1.41    itojun     doipsectunnel:
    286   1.5    itojun 	/*
    287   1.5    itojun 	 * All the extension headers will become inaccessible
    288   1.5    itojun 	 * (since they can be encrypted).
    289   1.5    itojun 	 * Don't panic, we need no more updates to extension headers
    290   1.5    itojun 	 * on inner IPv6 packet (since they are now encapsulated).
    291   1.5    itojun 	 *
    292   1.5    itojun 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    293   1.5    itojun 	 */
    294  1.66    cegger 	memset(&state, 0, sizeof(state));
    295   1.5    itojun 	state.m = m;
    296   1.5    itojun 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
    297   1.5    itojun 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
    298   1.5    itojun 
    299   1.5    itojun 	error = ipsec6_output_tunnel(&state, sp, 0);
    300   1.5    itojun 
    301   1.5    itojun 	m = state.m;
    302   1.5    itojun 	key_freesp(sp);
    303   1.5    itojun 
    304   1.5    itojun 	if (error) {
    305   1.5    itojun 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    306   1.5    itojun 		switch (error) {
    307   1.5    itojun 		case EHOSTUNREACH:
    308   1.5    itojun 		case ENETUNREACH:
    309   1.5    itojun 		case EMSGSIZE:
    310   1.5    itojun 		case ENOBUFS:
    311   1.5    itojun 		case ENOMEM:
    312   1.5    itojun 			break;
    313   1.5    itojun 		default:
    314  1.49  liamjfoy 			printf("ip6_forward (ipsec): error code %d\n", error);
    315  1.30    itojun 			/* FALLTHROUGH */
    316   1.5    itojun 		case ENOENT:
    317   1.5    itojun 			/* don't show these error codes to the user */
    318   1.5    itojun 			break;
    319   1.5    itojun 		}
    320  1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    321   1.5    itojun 		if (mcopy) {
    322   1.5    itojun #if 0
    323   1.5    itojun 			/* XXX: what icmp ? */
    324   1.5    itojun #else
    325   1.5    itojun 			m_freem(mcopy);
    326   1.5    itojun #endif
    327   1.5    itojun 		}
    328   1.5    itojun 		m_freem(m);
    329   1.5    itojun 		return;
    330   1.5    itojun 	}
    331  1.37    itojun 
    332  1.41    itojun 	if (ip6 != mtod(m, struct ip6_hdr *)) {
    333  1.41    itojun 		/*
    334  1.41    itojun 		 * now tunnel mode headers are added.  we are originating
    335  1.44     perry 		 * packet instead of forwarding the packet.
    336  1.41    itojun 		 */
    337  1.41    itojun 		ip6_output(m, NULL, NULL, IPV6_FORWARDING/*XXX*/, NULL, NULL,
    338  1.41    itojun 		    NULL);
    339  1.43    itojun 		goto freecopy;
    340  1.41    itojun 	}
    341  1.41    itojun 
    342  1.37    itojun 	/* adjust pointer */
    343  1.62    dyoung 	rt = state.ro ? rtcache_validate(state.ro) : NULL;
    344  1.57    dyoung 	dst = (const struct sockaddr_in6 *)state.dst;
    345  1.40   mycroft 	if (dst != NULL && rt != NULL) {
    346  1.37    itojun 		ipsecrt = 1;
    347  1.40   mycroft 		goto skip_routing;
    348  1.40   mycroft 	}
    349   1.5    itojun     }
    350   1.5    itojun     skip_ipsec:
    351  1.15    itojun #endif /* IPSEC */
    352  1.54  degroote #ifdef FAST_IPSEC
    353  1.54  degroote 	/* Check the security policy (SP) for the packet */
    354  1.54  degroote 
    355  1.54  degroote 	sp = ipsec6_check_policy(m,NULL,0,&needipsec,&error);
    356  1.54  degroote 	if (error != 0) {
    357  1.54  degroote 		/*
    358  1.54  degroote 		 * Hack: -EINVAL is used to signal that a packet
    359  1.54  degroote 		 * should be silently discarded.  This is typically
    360  1.54  degroote 		 * because we asked key management for an SA and
    361  1.54  degroote 		 * it was delayed (e.g. kicked up to IKE).
    362  1.54  degroote 		 */
    363  1.54  degroote 	if (error == -EINVAL)
    364  1.54  degroote 		error = 0;
    365  1.54  degroote 	goto freecopy;
    366  1.54  degroote 	}
    367  1.54  degroote #endif /* FAST_IPSEC */
    368  1.54  degroote 
    369  1.61    dyoung 	if (srcrt) {
    370  1.57    dyoung 		union {
    371  1.57    dyoung 			struct sockaddr		dst;
    372  1.57    dyoung 			struct sockaddr_in6	dst6;
    373  1.57    dyoung 		} u;
    374  1.57    dyoung 
    375  1.57    dyoung 		sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
    376  1.62    dyoung 		if ((rt = rtcache_lookup(&ip6_forward_rt, &u.dst)) == NULL) {
    377  1.64   thorpej 			IP6_STATINC(IP6_STAT_NOROUTE);
    378   1.5    itojun 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    379   1.5    itojun 			if (mcopy) {
    380   1.5    itojun 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
    381   1.5    itojun 					    ICMP6_DST_UNREACH_NOROUTE, 0);
    382   1.5    itojun 			}
    383   1.5    itojun 			m_freem(m);
    384   1.2    itojun 			return;
    385   1.2    itojun 		}
    386  1.62    dyoung 	} else if ((rt = rtcache_validate(&ip6_forward_rt)) == NULL &&
    387  1.62    dyoung 	           (rt = rtcache_update(&ip6_forward_rt, 1)) == NULL) {
    388  1.61    dyoung 		/*
    389  1.61    dyoung 		 * rtcache_getdst(ip6_forward_rt)->sin6_addr was equal to
    390  1.61    dyoung 		 * ip6->ip6_dst
    391  1.61    dyoung 		 */
    392  1.64   thorpej 		IP6_STATINC(IP6_STAT_NOROUTE);
    393  1.61    dyoung 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
    394  1.61    dyoung 		if (mcopy) {
    395  1.61    dyoung 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    396  1.61    dyoung 			    ICMP6_DST_UNREACH_NOROUTE, 0);
    397  1.61    dyoung 		}
    398  1.61    dyoung 		m_freem(m);
    399  1.61    dyoung 		return;
    400   1.2    itojun 	}
    401  1.57    dyoung 	dst = satocsin6(rtcache_getdst(&ip6_forward_rt));
    402  1.37    itojun #ifdef IPSEC
    403  1.37    itojun     skip_routing:;
    404  1.37    itojun #endif /* IPSEC */
    405   1.9    itojun 
    406   1.9    itojun 	/*
    407  1.47    rpaulo 	 * Source scope check: if a packet can't be delivered to its
    408  1.47    rpaulo 	 * destination for the reason that the destination is beyond the scope
    409  1.47    rpaulo 	 * of the source address, discard the packet and return an icmp6
    410  1.47    rpaulo 	 * destination unreachable error with Code 2 (beyond scope of source
    411  1.47    rpaulo 	 * address).  We use a local copy of ip6_src, since in6_setscope()
    412  1.47    rpaulo 	 * will possibly modify its first argument.
    413  1.47    rpaulo 	 * [draft-ietf-ipngwg-icmp-v3-07, Section 3.1]
    414  1.47    rpaulo 	 */
    415  1.47    rpaulo 	src_in6 = ip6->ip6_src;
    416  1.47    rpaulo 	if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
    417  1.47    rpaulo 		/* XXX: this should not happen */
    418  1.64   thorpej 		uint64_t *ip6s = IP6_STAT_GETREF();
    419  1.64   thorpej 		ip6s[IP6_STAT_CANTFORWARD]++;
    420  1.64   thorpej 		ip6s[IP6_STAT_BADSCOPE]++;
    421  1.64   thorpej 		IP6_STAT_PUTREF();
    422  1.47    rpaulo 		m_freem(m);
    423  1.47    rpaulo 		return;
    424  1.47    rpaulo 	}
    425  1.47    rpaulo 	if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
    426  1.64   thorpej 		uint64_t *ip6s = IP6_STAT_GETREF();
    427  1.64   thorpej 		ip6s[IP6_STAT_CANTFORWARD]++;
    428  1.64   thorpej 		ip6s[IP6_STAT_BADSCOPE]++;
    429  1.64   thorpej 		IP6_STAT_PUTREF();
    430  1.47    rpaulo 		m_freem(m);
    431  1.47    rpaulo 		return;
    432  1.47    rpaulo 	}
    433  1.47    rpaulo 	if (inzone != outzone
    434  1.39    itojun #ifdef IPSEC
    435  1.39    itojun 	    && !ipsecrt
    436  1.39    itojun #endif
    437  1.39    itojun 	    ) {
    438  1.64   thorpej 		uint64_t *ip6s = IP6_STAT_GETREF();
    439  1.64   thorpej 		ip6s[IP6_STAT_CANTFORWARD]++;
    440  1.64   thorpej 		ip6s[IP6_STAT_BADSCOPE]++;
    441  1.64   thorpej 		IP6_STAT_PUTREF();
    442   1.9    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
    443   1.9    itojun 
    444  1.48    kardel 		if (ip6_log_time + ip6_log_interval < time_second) {
    445  1.48    kardel 			ip6_log_time = time_second;
    446   1.9    itojun 			log(LOG_DEBUG,
    447   1.9    itojun 			    "cannot forward "
    448   1.9    itojun 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    449   1.9    itojun 			    ip6_sprintf(&ip6->ip6_src),
    450   1.9    itojun 			    ip6_sprintf(&ip6->ip6_dst),
    451   1.9    itojun 			    ip6->ip6_nxt,
    452   1.9    itojun 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
    453   1.9    itojun 		}
    454   1.9    itojun 		if (mcopy)
    455   1.9    itojun 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    456   1.9    itojun 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
    457   1.9    itojun 		m_freem(m);
    458   1.9    itojun 		return;
    459   1.9    itojun 	}
    460  1.54  degroote #ifdef FAST_IPSEC
    461  1.54  degroote     /*
    462  1.54  degroote      * If we need to encapsulate the packet, do it here
    463  1.54  degroote      * ipsec6_proces_packet will send the packet using ip6_output
    464  1.54  degroote      */
    465  1.54  degroote 	if (needipsec) {
    466  1.54  degroote 		s = splsoftnet();
    467  1.54  degroote 		error = ipsec6_process_packet(m,sp->req);
    468  1.54  degroote 		splx(s);
    469  1.54  degroote 		if (mcopy)
    470  1.54  degroote 			goto freecopy;
    471  1.54  degroote     }
    472  1.54  degroote #endif
    473  1.54  degroote 
    474  1.54  degroote 
    475   1.9    itojun 
    476  1.47    rpaulo 	/*
    477  1.47    rpaulo 	 * Destination scope check: if a packet is going to break the scope
    478  1.47    rpaulo 	 * zone of packet's destination address, discard it.  This case should
    479  1.47    rpaulo 	 * usually be prevented by appropriately-configured routing table, but
    480  1.47    rpaulo 	 * we need an explicit check because we may mistakenly forward the
    481  1.47    rpaulo 	 * packet to a different zone by (e.g.) a default route.
    482  1.47    rpaulo 	 */
    483  1.47    rpaulo 	dst_in6 = ip6->ip6_dst;
    484  1.47    rpaulo 	if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
    485  1.47    rpaulo 	    in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
    486  1.47    rpaulo 	    inzone != outzone) {
    487  1.64   thorpej 		uint64_t *ip6s = IP6_STAT_GETREF();
    488  1.64   thorpej 		ip6s[IP6_STAT_CANTFORWARD]++;
    489  1.64   thorpej 		ip6s[IP6_STAT_BADSCOPE]++;
    490  1.64   thorpej 		IP6_STAT_PUTREF();
    491  1.47    rpaulo 		m_freem(m);
    492  1.47    rpaulo 		return;
    493  1.47    rpaulo 	}
    494  1.47    rpaulo 
    495  1.28    itojun 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
    496   1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
    497   1.5    itojun 		if (mcopy) {
    498   1.5    itojun 			u_long mtu;
    499  1.15    itojun #ifdef IPSEC
    500  1.45  christos 			struct secpolicy *xsp;
    501   1.7    itojun 			int ipsecerror;
    502   1.7    itojun 			size_t ipsechdrsiz;
    503   1.7    itojun #endif
    504   1.5    itojun 
    505  1.28    itojun 			mtu = IN6_LINKMTU(rt->rt_ifp);
    506  1.15    itojun #ifdef IPSEC
    507   1.7    itojun 			/*
    508   1.7    itojun 			 * When we do IPsec tunnel ingress, we need to play
    509  1.28    itojun 			 * with the link value (decrement IPsec header size
    510   1.7    itojun 			 * from mtu value).  The code is much simpler than v4
    511   1.7    itojun 			 * case, as we have the outgoing interface for
    512   1.7    itojun 			 * encapsulated packet as "rt->rt_ifp".
    513   1.7    itojun 			 */
    514  1.45  christos 			xsp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
    515   1.7    itojun 				IP_FORWARDING, &ipsecerror);
    516  1.45  christos 			if (xsp) {
    517   1.7    itojun 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
    518   1.7    itojun 					IPSEC_DIR_OUTBOUND, NULL);
    519   1.7    itojun 				if (ipsechdrsiz < mtu)
    520   1.7    itojun 					mtu -= ipsechdrsiz;
    521   1.7    itojun 			}
    522   1.7    itojun 
    523   1.7    itojun 			/*
    524  1.10    itojun 			 * if mtu becomes less than minimum MTU,
    525   1.7    itojun 			 * tell minimum MTU (and I'll need to fragment it).
    526   1.7    itojun 			 */
    527   1.7    itojun 			if (mtu < IPV6_MMTU)
    528   1.7    itojun 				mtu = IPV6_MMTU;
    529   1.7    itojun #endif
    530   1.5    itojun 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
    531   1.5    itojun 		}
    532   1.5    itojun 		m_freem(m);
    533   1.2    itojun 		return;
    534  1.31    itojun 	}
    535   1.2    itojun 
    536   1.2    itojun 	if (rt->rt_flags & RTF_GATEWAY)
    537   1.2    itojun 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    538   1.2    itojun 
    539   1.2    itojun 	/*
    540   1.2    itojun 	 * If we are to forward the packet using the same interface
    541   1.2    itojun 	 * as one we got the packet from, perhaps we should send a redirect
    542   1.2    itojun 	 * to sender to shortcut a hop.
    543   1.2    itojun 	 * Only send redirect if source is sending directly to us,
    544   1.2    itojun 	 * and if packet was not source routed (or has any options).
    545   1.2    itojun 	 * Also, don't send redirect if forwarding using a route
    546   1.2    itojun 	 * modified by a redirect.
    547   1.2    itojun 	 */
    548  1.36    itojun 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt && ip6_sendredirects &&
    549  1.37    itojun #ifdef IPSEC
    550  1.37    itojun 	    !ipsecrt &&
    551  1.37    itojun #endif
    552  1.22    itojun 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
    553  1.23    itojun 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
    554  1.55    dyoung 		    nd6_is_addr_neighbor(
    555  1.57    dyoung 		        satocsin6(rtcache_getdst(&ip6_forward_rt)),
    556  1.55    dyoung 			rt->rt_ifp)) {
    557  1.22    itojun 			/*
    558  1.22    itojun 			 * If the incoming interface is equal to the outgoing
    559  1.23    itojun 			 * one, the link attached to the interface is
    560  1.23    itojun 			 * point-to-point, and the IPv6 destination is
    561  1.23    itojun 			 * regarded as on-link on the link, then it will be
    562  1.23    itojun 			 * highly probable that the destination address does
    563  1.23    itojun 			 * not exist on the link and that the packet is going
    564  1.23    itojun 			 * to loop.  Thus, we immediately drop the packet and
    565  1.23    itojun 			 * send an ICMPv6 error message.
    566  1.23    itojun 			 * For other routing loops, we dare to let the packet
    567  1.23    itojun 			 * go to the loop, so that a remote diagnosing host
    568  1.23    itojun 			 * can detect the loop by traceroute.
    569  1.22    itojun 			 * type/code is based on suggestion by Rich Draves.
    570  1.22    itojun 			 * not sure if it is the best pick.
    571  1.22    itojun 			 */
    572  1.22    itojun 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
    573  1.22    itojun 				    ICMP6_DST_UNREACH_ADDR, 0);
    574  1.22    itojun 			m_freem(m);
    575  1.22    itojun 			return;
    576  1.22    itojun 		}
    577   1.2    itojun 		type = ND_REDIRECT;
    578  1.22    itojun 	}
    579   1.2    itojun 
    580  1.10    itojun 	/*
    581  1.10    itojun 	 * Fake scoped addresses. Note that even link-local source or
    582  1.10    itojun 	 * destinaion can appear, if the originating node just sends the
    583  1.10    itojun 	 * packet to us (without address resolution for the destination).
    584  1.10    itojun 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
    585  1.18    itojun 	 * link identifiers, we can do this stuff after making a copy for
    586  1.18    itojun 	 * returning an error.
    587  1.10    itojun 	 */
    588  1.10    itojun 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
    589  1.10    itojun 		/*
    590  1.10    itojun 		 * See corresponding comments in ip6_output.
    591  1.10    itojun 		 * XXX: but is it possible that ip6_forward() sends a packet
    592  1.10    itojun 		 *      to a loopback interface? I don't think so, and thus
    593  1.10    itojun 		 *      I bark here. (jinmei (at) kame.net)
    594  1.12    itojun 		 * XXX: it is common to route invalid packets to loopback.
    595  1.13    itojun 		 *	also, the codepath will be visited on use of ::1 in
    596  1.13    itojun 		 *	rthdr. (itojun)
    597  1.10    itojun 		 */
    598  1.13    itojun #if 1
    599  1.13    itojun 		if (0)
    600  1.13    itojun #else
    601  1.13    itojun 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
    602  1.13    itojun #endif
    603  1.13    itojun 		{
    604  1.12    itojun 			printf("ip6_forward: outgoing interface is loopback. "
    605  1.12    itojun 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
    606  1.12    itojun 			       ip6_sprintf(&ip6->ip6_src),
    607  1.12    itojun 			       ip6_sprintf(&ip6->ip6_dst),
    608  1.12    itojun 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
    609  1.12    itojun 			       if_name(rt->rt_ifp));
    610  1.12    itojun 		}
    611  1.13    itojun 
    612  1.19    itojun 		/* we can just use rcvif in forwarding. */
    613  1.19    itojun 		origifp = m->m_pkthdr.rcvif;
    614  1.10    itojun 	}
    615  1.10    itojun 	else
    616  1.10    itojun 		origifp = rt->rt_ifp;
    617  1.47    rpaulo 	/*
    618  1.47    rpaulo 	 * clear embedded scope identifiers if necessary.
    619  1.47    rpaulo 	 * in6_clearscope will touch the addresses only when necessary.
    620  1.47    rpaulo 	 */
    621  1.47    rpaulo 	in6_clearscope(&ip6->ip6_src);
    622  1.47    rpaulo 	in6_clearscope(&ip6->ip6_dst);
    623  1.10    itojun 
    624  1.20    itojun #ifdef PFIL_HOOKS
    625  1.20    itojun 	/*
    626  1.20    itojun 	 * Run through list of hooks for output packets.
    627  1.20    itojun 	 */
    628  1.20    itojun 	if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, rt->rt_ifp,
    629  1.34    itojun 	    PFIL_OUT)) != 0)
    630  1.20    itojun 		goto senderr;
    631  1.20    itojun 	if (m == NULL)
    632  1.20    itojun 		goto freecopy;
    633  1.20    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    634  1.20    itojun #endif /* PFIL_HOOKS */
    635  1.20    itojun 
    636  1.10    itojun 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
    637   1.5    itojun 	if (error) {
    638   1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
    639  1.64   thorpej 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    640   1.5    itojun 	} else {
    641  1.64   thorpej 		IP6_STATINC(IP6_STAT_FORWARD);
    642   1.5    itojun 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
    643   1.2    itojun 		if (type)
    644  1.64   thorpej 			IP6_STATINC(IP6_STAT_REDIRECTSENT);
    645   1.2    itojun 		else {
    646  1.56  liamjfoy #ifdef GATEWAY
    647  1.56  liamjfoy 			if (m->m_flags & M_CANFASTFWD)
    648  1.56  liamjfoy 				ip6flow_create(&ip6_forward_rt, m);
    649  1.56  liamjfoy #endif
    650   1.2    itojun 			if (mcopy)
    651   1.2    itojun 				goto freecopy;
    652   1.2    itojun 		}
    653   1.2    itojun 	}
    654  1.20    itojun 
    655  1.21      matt #ifdef PFIL_HOOKS
    656  1.20    itojun  senderr:
    657  1.21      matt #endif
    658   1.2    itojun 	if (mcopy == NULL)
    659   1.2    itojun 		return;
    660   1.2    itojun 	switch (error) {
    661   1.2    itojun 	case 0:
    662   1.2    itojun 		if (type == ND_REDIRECT) {
    663   1.2    itojun 			icmp6_redirect_output(mcopy, rt);
    664   1.2    itojun 			return;
    665   1.2    itojun 		}
    666   1.2    itojun 		goto freecopy;
    667   1.2    itojun 
    668   1.2    itojun 	case EMSGSIZE:
    669   1.2    itojun 		/* xxx MTU is constant in PPP? */
    670   1.2    itojun 		goto freecopy;
    671   1.2    itojun 
    672   1.2    itojun 	case ENOBUFS:
    673   1.2    itojun 		/* Tell source to slow down like source quench in IP? */
    674   1.2    itojun 		goto freecopy;
    675   1.2    itojun 
    676   1.2    itojun 	case ENETUNREACH:	/* shouldn't happen, checked above */
    677   1.2    itojun 	case EHOSTUNREACH:
    678   1.2    itojun 	case ENETDOWN:
    679   1.2    itojun 	case EHOSTDOWN:
    680   1.2    itojun 	default:
    681   1.2    itojun 		type = ICMP6_DST_UNREACH;
    682   1.2    itojun 		code = ICMP6_DST_UNREACH_ADDR;
    683   1.2    itojun 		break;
    684   1.2    itojun 	}
    685   1.2    itojun 	icmp6_error(mcopy, type, code, 0);
    686   1.2    itojun 	return;
    687   1.2    itojun 
    688   1.2    itojun  freecopy:
    689   1.2    itojun 	m_freem(mcopy);
    690   1.5    itojun 	return;
    691   1.2    itojun }
    692