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