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