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