Home | History | Annotate | Line # | Download | only in netinet6
ip6_forward.c revision 1.2
      1  1.2  itojun /*
      2  1.2  itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      3  1.2  itojun  * All rights reserved.
      4  1.2  itojun  *
      5  1.2  itojun  * Redistribution and use in source and binary forms, with or without
      6  1.2  itojun  * modification, are permitted provided that the following conditions
      7  1.2  itojun  * are met:
      8  1.2  itojun  * 1. Redistributions of source code must retain the above copyright
      9  1.2  itojun  *    notice, this list of conditions and the following disclaimer.
     10  1.2  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.2  itojun  *    notice, this list of conditions and the following disclaimer in the
     12  1.2  itojun  *    documentation and/or other materials provided with the distribution.
     13  1.2  itojun  * 3. Neither the name of the project nor the names of its contributors
     14  1.2  itojun  *    may be used to endorse or promote products derived from this software
     15  1.2  itojun  *    without specific prior written permission.
     16  1.2  itojun  *
     17  1.2  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     18  1.2  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.2  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.2  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     21  1.2  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.2  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.2  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.2  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.2  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.2  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.2  itojun  * SUCH DAMAGE.
     28  1.2  itojun  */
     29  1.2  itojun 
     30  1.2  itojun #include <sys/param.h>
     31  1.2  itojun #include <sys/systm.h>
     32  1.2  itojun #include <sys/malloc.h>
     33  1.2  itojun #include <sys/mbuf.h>
     34  1.2  itojun #include <sys/domain.h>
     35  1.2  itojun #include <sys/protosw.h>
     36  1.2  itojun #include <sys/socket.h>
     37  1.2  itojun #include <sys/errno.h>
     38  1.2  itojun #include <sys/time.h>
     39  1.2  itojun #include <sys/kernel.h>
     40  1.2  itojun #include <sys/syslog.h>
     41  1.2  itojun 
     42  1.2  itojun #include <net/if.h>
     43  1.2  itojun #include <net/route.h>
     44  1.2  itojun 
     45  1.2  itojun #include <netinet/in.h>
     46  1.2  itojun #include <netinet/in_var.h>
     47  1.2  itojun #include <netinet6/in6_systm.h>
     48  1.2  itojun #include <netinet6/ip6.h>
     49  1.2  itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     50  1.2  itojun #include <netinet6/in6_pcb.h>
     51  1.2  itojun #endif
     52  1.2  itojun #include <netinet6/ip6_var.h>
     53  1.2  itojun #include <netinet6/icmp6.h>
     54  1.2  itojun 
     55  1.2  itojun struct	route_in6 ip6_forward_rt;
     56  1.2  itojun 
     57  1.2  itojun /*
     58  1.2  itojun  * Forward a packet.  If some error occurs return the sender
     59  1.2  itojun  * an icmp packet.  Note we can't always generate a meaningful
     60  1.2  itojun  * icmp message because icmp doesn't have a large enough repertoire
     61  1.2  itojun  * of codes and types.
     62  1.2  itojun  *
     63  1.2  itojun  * If not forwarding, just drop the packet.  This could be confusing
     64  1.2  itojun  * if ipforwarding was zero but some routing protocol was advancing
     65  1.2  itojun  * us as a gateway to somewhere.  However, we must let the routing
     66  1.2  itojun  * protocol deal with that.
     67  1.2  itojun  *
     68  1.2  itojun  */
     69  1.2  itojun 
     70  1.2  itojun void
     71  1.2  itojun ip6_forward(m, srcrt)
     72  1.2  itojun 	struct mbuf *m;
     73  1.2  itojun 	int srcrt;
     74  1.2  itojun {
     75  1.2  itojun 	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
     76  1.2  itojun 	register struct sockaddr_in6 *dst;
     77  1.2  itojun 	register struct rtentry *rt;
     78  1.2  itojun 	int error, type = 0, code = 0;
     79  1.2  itojun 	struct mbuf *mcopy;
     80  1.2  itojun 
     81  1.2  itojun 	if (m->m_flags & (M_BCAST|M_MCAST) ||
     82  1.2  itojun 	   in6_canforward(&ip6->ip6_src, &ip6->ip6_dst) == 0) {
     83  1.2  itojun 		ip6stat.ip6s_cantforward++;
     84  1.2  itojun 		ip6stat.ip6s_badscope++;
     85  1.2  itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     86  1.2  itojun 		if (ip6_log_time + ip6_log_interval < time.tv_sec) {
     87  1.2  itojun 			ip6_log_time = time.tv_sec;
     88  1.2  itojun #else
     89  1.2  itojun 		if (ip6_log_time + ip6_log_interval < time_second) {
     90  1.2  itojun 			ip6_log_time = time_second;
     91  1.2  itojun #endif
     92  1.2  itojun 			log(LOG_DEBUG,
     93  1.2  itojun 			    "cannot forward "
     94  1.2  itojun 			    "from %s to %s nxt %d received on %s\n",
     95  1.2  itojun 			    ip6_sprintf(&ip6->ip6_src), /* XXX meaningless */
     96  1.2  itojun 			    ip6_sprintf(&ip6->ip6_dst),
     97  1.2  itojun 			    ip6->ip6_nxt,
     98  1.2  itojun 			    m->m_pkthdr.rcvif->if_xname);
     99  1.2  itojun 		}
    100  1.2  itojun 		m_freem(m);
    101  1.2  itojun 		return;
    102  1.2  itojun 	}
    103  1.2  itojun 
    104  1.2  itojun 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
    105  1.2  itojun 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
    106  1.2  itojun 				ICMP6_TIME_EXCEED_TRANSIT, 0);
    107  1.2  itojun 		return;
    108  1.2  itojun 	}
    109  1.2  itojun 	ip6->ip6_hlim -= IPV6_HLIMDEC;
    110  1.2  itojun 
    111  1.2  itojun 	dst = &ip6_forward_rt.ro_dst;
    112  1.2  itojun 	if (!srcrt) {
    113  1.2  itojun 		/*
    114  1.2  itojun 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
    115  1.2  itojun 		 */
    116  1.2  itojun 		if (ip6_forward_rt.ro_rt == 0 ||
    117  1.2  itojun 		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
    118  1.2  itojun 			if (ip6_forward_rt.ro_rt) {
    119  1.2  itojun 				RTFREE(ip6_forward_rt.ro_rt);
    120  1.2  itojun 				ip6_forward_rt.ro_rt = 0;
    121  1.2  itojun 			}
    122  1.2  itojun 			/* this probably fails but give it a try again */
    123  1.2  itojun #ifdef __NetBSD__
    124  1.2  itojun 			rtalloc((struct route *)&ip6_forward_rt);
    125  1.2  itojun #else
    126  1.2  itojun 			rtalloc_ign((struct route *)&ip6_forward_rt,
    127  1.2  itojun 				    RTF_PRCLONING);
    128  1.2  itojun #endif
    129  1.2  itojun 		}
    130  1.2  itojun 
    131  1.2  itojun 		if (ip6_forward_rt.ro_rt == 0) {
    132  1.2  itojun 			ip6stat.ip6s_noroute++;
    133  1.2  itojun 			icmp6_error(m, ICMP6_DST_UNREACH,
    134  1.2  itojun 				    ICMP6_DST_UNREACH_NOROUTE, 0);
    135  1.2  itojun 			return;
    136  1.2  itojun 		}
    137  1.2  itojun 	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
    138  1.2  itojun 		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
    139  1.2  itojun 		if (ip6_forward_rt.ro_rt) {
    140  1.2  itojun 			RTFREE(ip6_forward_rt.ro_rt);
    141  1.2  itojun 			ip6_forward_rt.ro_rt = 0;
    142  1.2  itojun 		}
    143  1.2  itojun 		bzero(dst, sizeof(*dst));
    144  1.2  itojun 		dst->sin6_len = sizeof(struct sockaddr_in6);
    145  1.2  itojun 		dst->sin6_family = AF_INET6;
    146  1.2  itojun 		dst->sin6_addr = ip6->ip6_dst;
    147  1.2  itojun 
    148  1.2  itojun #ifdef __NetBSD__
    149  1.2  itojun 		rtalloc((struct route *)&ip6_forward_rt);
    150  1.2  itojun #else
    151  1.2  itojun   		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
    152  1.2  itojun #endif
    153  1.2  itojun 		if (ip6_forward_rt.ro_rt == 0) {
    154  1.2  itojun 			ip6stat.ip6s_noroute++;
    155  1.2  itojun 			icmp6_error(m, ICMP6_DST_UNREACH,
    156  1.2  itojun 				    ICMP6_DST_UNREACH_NOROUTE, 0);
    157  1.2  itojun 			return;
    158  1.2  itojun 		}
    159  1.2  itojun 	}
    160  1.2  itojun 	rt = ip6_forward_rt.ro_rt;
    161  1.2  itojun 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu){
    162  1.2  itojun  		icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, rt->rt_ifp->if_mtu);
    163  1.2  itojun 		return;
    164  1.2  itojun  	}
    165  1.2  itojun 
    166  1.2  itojun 	if (rt->rt_flags & RTF_GATEWAY)
    167  1.2  itojun 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    168  1.2  itojun 	/*
    169  1.2  itojun 	 * Save at most 528 bytes of the packet in case
    170  1.2  itojun 	 * we need to generate an ICMP6 message to the src.
    171  1.2  itojun 	 * Thanks to M_EXT, in most cases copy will not occur.
    172  1.2  itojun 	 */
    173  1.2  itojun 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
    174  1.2  itojun 
    175  1.2  itojun 	/*
    176  1.2  itojun 	 * If we are to forward the packet using the same interface
    177  1.2  itojun 	 * as one we got the packet from, perhaps we should send a redirect
    178  1.2  itojun 	 * to sender to shortcut a hop.
    179  1.2  itojun 	 * Only send redirect if source is sending directly to us,
    180  1.2  itojun 	 * and if packet was not source routed (or has any options).
    181  1.2  itojun 	 * Also, don't send redirect if forwarding using a route
    182  1.2  itojun 	 * modified by a redirect.
    183  1.2  itojun 	 */
    184  1.2  itojun 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
    185  1.2  itojun 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
    186  1.2  itojun 		type = ND_REDIRECT;
    187  1.2  itojun 
    188  1.2  itojun 	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
    189  1.2  itojun 					 (struct sockaddr *)dst,
    190  1.2  itojun 					 ip6_forward_rt.ro_rt);
    191  1.2  itojun 	if (error)
    192  1.2  itojun 		ip6stat.ip6s_cantforward++;
    193  1.2  itojun 	else {
    194  1.2  itojun 		ip6stat.ip6s_forward++;
    195  1.2  itojun 		if (type)
    196  1.2  itojun 			ip6stat.ip6s_redirectsent++;
    197  1.2  itojun 		else {
    198  1.2  itojun 			if (mcopy)
    199  1.2  itojun 				goto freecopy;
    200  1.2  itojun 		}
    201  1.2  itojun 	}
    202  1.2  itojun 	if (mcopy == NULL)
    203  1.2  itojun 		return;
    204  1.2  itojun 
    205  1.2  itojun 	switch (error) {
    206  1.2  itojun 	case 0:
    207  1.2  itojun #if 1
    208  1.2  itojun 		if (type == ND_REDIRECT) {
    209  1.2  itojun 			icmp6_redirect_output(mcopy, rt);
    210  1.2  itojun 			return;
    211  1.2  itojun 		}
    212  1.2  itojun #endif
    213  1.2  itojun 		goto freecopy;
    214  1.2  itojun 
    215  1.2  itojun 	case EMSGSIZE:
    216  1.2  itojun 		/* xxx MTU is constant in PPP? */
    217  1.2  itojun 		goto freecopy;
    218  1.2  itojun 
    219  1.2  itojun 	case ENOBUFS:
    220  1.2  itojun 		/* Tell source to slow down like source quench in IP? */
    221  1.2  itojun 		goto freecopy;
    222  1.2  itojun 
    223  1.2  itojun 	case ENETUNREACH:	/* shouldn't happen, checked above */
    224  1.2  itojun 	case EHOSTUNREACH:
    225  1.2  itojun 	case ENETDOWN:
    226  1.2  itojun 	case EHOSTDOWN:
    227  1.2  itojun 	default:
    228  1.2  itojun 		type = ICMP6_DST_UNREACH;
    229  1.2  itojun 		code = ICMP6_DST_UNREACH_ADDR;
    230  1.2  itojun 		break;
    231  1.2  itojun 	}
    232  1.2  itojun 	icmp6_error(mcopy, type, code, 0);
    233  1.2  itojun 	return;
    234  1.2  itojun 
    235  1.2  itojun  freecopy:
    236  1.2  itojun 	m_freem(mcopy);
    237  1.2  itojun }
    238