1 /* $NetBSD: route6.c,v 1.6 2000/02/06 12:49:49 itojun 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/mbuf.h> 34 #include <sys/socket.h> 35 #include <sys/systm.h> 36 37 #include <net/if.h> 38 39 #include <netinet/in.h> 40 #include <netinet6/in6_var.h> 41 #include <netinet/ip6.h> 42 #include <netinet6/ip6_var.h> 43 44 #include <netinet/icmp6.h> 45 46 static int ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *, struct ip6_rthdr0 *)); 47 48 int 49 route6_input(mp, offp, proto) 50 struct mbuf **mp; 51 int *offp, proto; /* proto is unused */ 52 { 53 register struct ip6_hdr *ip6; 54 register struct mbuf *m = *mp; 55 register struct ip6_rthdr *rh; 56 int off = *offp, rhlen; 57 58 #ifndef PULLDOWN_TEST 59 IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE); 60 ip6 = mtod(m, struct ip6_hdr *); 61 rh = (struct ip6_rthdr *)((caddr_t)ip6 + off); 62 #else 63 ip6 = mtod(m, struct ip6_hdr *); 64 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh)); 65 if (rh == NULL) { 66 ip6stat.ip6s_tooshort++; 67 return IPPROTO_DONE; 68 } 69 #endif 70 71 switch(rh->ip6r_type) { 72 case IPV6_RTHDR_TYPE_0: 73 rhlen = (rh->ip6r_len + 1) << 3; 74 #ifndef PULLDOWN_TEST 75 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE); 76 #else 77 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen); 78 if (rh == NULL) { 79 ip6stat.ip6s_tooshort++; 80 return IPPROTO_DONE; 81 } 82 #endif 83 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh)) 84 return(IPPROTO_DONE); 85 break; 86 default: 87 /* unknown routing type */ 88 if (rh->ip6r_segleft == 0) { 89 rhlen = (rh->ip6r_len + 1) << 3; 90 break; /* Final dst. Just ignore the header. */ 91 } 92 ip6stat.ip6s_badoptions++; 93 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 94 (caddr_t)&rh->ip6r_type - (caddr_t)ip6); 95 return(IPPROTO_DONE); 96 } 97 98 *offp += rhlen; 99 return(rh->ip6r_nxt); 100 } 101 102 /* 103 * Type0 routing header processing 104 */ 105 static int 106 ip6_rthdr0(m, ip6, rh0) 107 struct mbuf *m; 108 struct ip6_hdr *ip6; 109 struct ip6_rthdr0 *rh0; 110 { 111 int addrs, index; 112 struct in6_addr *nextaddr, tmpaddr; 113 114 if (rh0->ip6r0_segleft == 0) 115 return(0); 116 117 if (rh0->ip6r0_len % 2 118 #ifdef COMPAT_RFC1883 119 || rh0->ip6r0_len > 46 120 #endif 121 ) { 122 /* 123 * Type 0 routing header can't contain more than 23 addresses. 124 * RFC 2462: this limitation was removed since stict/loose 125 * bitmap field was deleted. 126 */ 127 ip6stat.ip6s_badoptions++; 128 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 129 (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6); 130 return(-1); 131 } 132 133 if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) { 134 ip6stat.ip6s_badoptions++; 135 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 136 (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6); 137 return(-1); 138 } 139 140 index = addrs - rh0->ip6r0_segleft; 141 rh0->ip6r0_segleft--; 142 nextaddr = rh0->ip6r0_addr + index; 143 144 /* 145 * reject invalid addresses. be proactive about malicious use of 146 * IPv4 mapped/compat address. 147 * XXX need more checks? 148 */ 149 if (IN6_IS_ADDR_MULTICAST(nextaddr) || 150 IN6_IS_ADDR_UNSPECIFIED(nextaddr) || 151 IN6_IS_ADDR_V4MAPPED(nextaddr) || 152 IN6_IS_ADDR_V4COMPAT(nextaddr)) { 153 ip6stat.ip6s_badoptions++; 154 m_freem(m); 155 return(-1); 156 } 157 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 158 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) || 159 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) || 160 IN6_IS_ADDR_V4COMPAT(nextaddr)) { 161 ip6stat.ip6s_badoptions++; 162 m_freem(m); 163 return(-1); 164 } 165 166 /* 167 * Swap the IPv6 destination address and nextaddr. Forward the packet. 168 */ 169 tmpaddr = *nextaddr; 170 *nextaddr = ip6->ip6_dst; 171 if (IN6_IS_ADDR_LINKLOCAL(nextaddr)) 172 nextaddr->s6_addr16[1] = 0; 173 ip6->ip6_dst = tmpaddr; 174 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) 175 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 176 177 #ifdef COMPAT_RFC1883 178 if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8)))) 179 ip6_forward(m, IPV6_SRCRT_NEIGHBOR); 180 else 181 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR); 182 #else 183 ip6_forward(m, 1); 184 #endif 185 186 return(-1); /* m would be freed in ip6_forward() */ 187 } 188