Home | History | Annotate | Line # | Download | only in netinet6
route6.c revision 1.7
      1 /*	$NetBSD: route6.c,v 1.7 2000/09/20 23:35:51 itojun Exp $	*/
      2 /*	$KAME: route6.c,v 1.21 2000/09/20 23:00:49 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/mbuf.h>
     35 #include <sys/socket.h>
     36 #include <sys/systm.h>
     37 
     38 #include <net/if.h>
     39 
     40 #include <netinet/in.h>
     41 #include <netinet6/in6_var.h>
     42 #include <netinet/ip6.h>
     43 #include <netinet6/ip6_var.h>
     44 
     45 #include <netinet/icmp6.h>
     46 
     47 static int ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *, struct ip6_rthdr0 *));
     48 
     49 int
     50 route6_input(mp, offp, proto)
     51 	struct mbuf **mp;
     52 	int *offp, proto;	/* proto is unused */
     53 {
     54 	register struct ip6_hdr *ip6;
     55 	register struct mbuf *m = *mp;
     56 	register struct ip6_rthdr *rh;
     57 	int off = *offp, rhlen;
     58 
     59 #ifndef PULLDOWN_TEST
     60 	IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
     61 	ip6 = mtod(m, struct ip6_hdr *);
     62 	rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
     63 #else
     64 	ip6 = mtod(m, struct ip6_hdr *);
     65 	IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
     66 	if (rh == NULL) {
     67 		ip6stat.ip6s_tooshort++;
     68 		return IPPROTO_DONE;
     69 	}
     70 #endif
     71 
     72 	switch(rh->ip6r_type) {
     73 	 case IPV6_RTHDR_TYPE_0:
     74 		 rhlen = (rh->ip6r_len + 1) << 3;
     75 #ifndef PULLDOWN_TEST
     76 		 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
     77 #else
     78 		 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
     79 		 if (rh == NULL) {
     80 			ip6stat.ip6s_tooshort++;
     81 			return IPPROTO_DONE;
     82 		 }
     83 #endif
     84 		 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
     85 			 return(IPPROTO_DONE);
     86 		 break;
     87 	 default:
     88 		 /* unknown routing type */
     89 		 if (rh->ip6r_segleft == 0) {
     90 			 rhlen = (rh->ip6r_len + 1) << 3;
     91 			 break;	/* Final dst. Just ignore the header. */
     92 		 }
     93 		 ip6stat.ip6s_badoptions++;
     94 		 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
     95 			     (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
     96 		 return(IPPROTO_DONE);
     97 	}
     98 
     99 	*offp += rhlen;
    100 	return(rh->ip6r_nxt);
    101 }
    102 
    103 /*
    104  * Type0 routing header processing
    105  */
    106 static int
    107 ip6_rthdr0(m, ip6, rh0)
    108 	struct mbuf *m;
    109 	struct ip6_hdr *ip6;
    110 	struct ip6_rthdr0 *rh0;
    111 {
    112 	int addrs, index;
    113 	struct in6_addr *nextaddr, tmpaddr;
    114 
    115 	if (rh0->ip6r0_segleft == 0)
    116 		return(0);
    117 
    118 	if (rh0->ip6r0_len % 2
    119 #ifdef COMPAT_RFC1883
    120 	    || rh0->ip6r0_len > 46
    121 #endif
    122 		) {
    123 		/*
    124 		 * Type 0 routing header can't contain more than 23 addresses.
    125 		 * RFC 2462: this limitation was removed since stict/loose
    126 		 * bitmap field was deleted.
    127 		 */
    128 		ip6stat.ip6s_badoptions++;
    129 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    130 			    (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
    131 		return(-1);
    132 	}
    133 
    134 	if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
    135 		ip6stat.ip6s_badoptions++;
    136 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
    137 			    (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
    138 		return(-1);
    139 	}
    140 
    141 	index = addrs - rh0->ip6r0_segleft;
    142 	rh0->ip6r0_segleft--;
    143 	nextaddr = rh0->ip6r0_addr + index;
    144 
    145 	/*
    146 	 * reject invalid addresses.  be proactive about malicious use of
    147 	 * IPv4 mapped/compat address.
    148 	 * XXX need more checks?
    149 	 */
    150 	if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
    151 	    IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
    152 	    IN6_IS_ADDR_V4MAPPED(nextaddr) ||
    153 	    IN6_IS_ADDR_V4COMPAT(nextaddr)) {
    154 		ip6stat.ip6s_badoptions++;
    155 		m_freem(m);
    156 		return(-1);
    157 	}
    158 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    159 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
    160 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
    161 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
    162 		ip6stat.ip6s_badoptions++;
    163 		m_freem(m);
    164 		return(-1);
    165 	}
    166 
    167 	/*
    168 	 * Swap the IPv6 destination address and nextaddr. Forward the packet.
    169 	 */
    170 	tmpaddr = *nextaddr;
    171 	*nextaddr = ip6->ip6_dst;
    172 	if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
    173 		nextaddr->s6_addr16[1] = 0;
    174 	ip6->ip6_dst = tmpaddr;
    175 	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
    176 		ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
    177 
    178 #ifdef COMPAT_RFC1883
    179 	if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
    180 		ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
    181 	else
    182 		ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
    183 #else
    184 	ip6_forward(m, 1);
    185 #endif
    186 
    187 	return(-1);			/* m would be freed in ip6_forward() */
    188 }
    189