xref: /dragonfly/sys/netinet6/route6.c (revision 3f625015)
1 /*	$FreeBSD: src/sys/netinet6/route6.c,v 1.1.2.5 2003/01/23 21:06:47 sam Exp $	*/
2 /*	$DragonFly: src/sys/netinet6/route6.c,v 1.9 2007/05/07 13:00:16 hasso Exp $	*/
3 /*	$KAME: route6.c,v 1.24 2001/03/14 03:07:05 itojun Exp $	*/
4 
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 
37 #include <sys/param.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
42 
43 #include <net/if.h>
44 
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/ip6.h>
48 #include <netinet6/ip6_var.h>
49 
50 #include <netinet/icmp6.h>
51 
52 #if 0
53 static int ip6_rthdr0 (struct mbuf *, struct ip6_hdr *,
54     struct ip6_rthdr0 *);
55 #endif
56 
57 int
58 route6_input(struct mbuf **mp, int *offp, int proto) /* proto is unused */
59 {
60 	struct ip6_hdr *ip6;
61 	struct mbuf *m = *mp;
62 	struct ip6_rthdr *rh;
63 	int off = *offp, rhlen;
64 
65 #ifndef PULLDOWN_TEST
66 	IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
67 	ip6 = mtod(m, struct ip6_hdr *);
68 	rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
69 #else
70 	ip6 = mtod(m, struct ip6_hdr *);
71 	IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
72 	if (rh == NULL) {
73 		ip6stat.ip6s_tooshort++;
74 		return IPPROTO_DONE;
75 	}
76 #endif
77 
78 	switch (rh->ip6r_type) {
79 #if 0
80 	/*
81 	 * See http://www.secdev.org/conf/IPv6_RH_security-csw07.pdf
82 	 * for why IPV6_RTHDR_TYPE_0 is baned here.
83 	 *
84 	 * We return ICMPv6 parameter problem so that innocent people
85 	 * (not an attacker) would notice about the use of IPV6_RTHDR_TYPE_0.
86 	 * Since there's no amplification, and ICMPv6 error will be rate-
87 	 * controlled, it shouldn't cause any problem.
88 	 * If you are concerned about this, you may want to use the following
89 	 * code fragment:
90 	 *
91 	 * case IPV6_RTHDR_TYPE_0:
92 	 *	m_freem(m);
93 	 *	return (IPPROTO_DONE);
94 	 */
95 	case IPV6_RTHDR_TYPE_0:
96 		rhlen = (rh->ip6r_len + 1) << 3;
97 #ifndef PULLDOWN_TEST
98 		/*
99 		 * note on option length:
100 		 * due to IP6_EXTHDR_CHECK assumption, we cannot handle
101 		 * very big routing header (max rhlen == 2048).
102 		 */
103 		IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
104 #else
105 		/*
106 		 * note on option length:
107 		 * maximum rhlen: 2048
108 		 * max mbuf m_pulldown can handle: MCLBYTES == usually 2048
109 		 * so, here we are assuming that m_pulldown can handle
110 		 * rhlen == 2048 case.  this may not be a good thing to
111 		 * assume - we may want to avoid pulling it up altogether.
112 		 */
113 		IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
114 		if (rh == NULL) {
115 			ip6stat.ip6s_tooshort++;
116 			return IPPROTO_DONE;
117 		}
118 #endif
119 		if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
120 			return (IPPROTO_DONE);
121 		break;
122 #endif
123 	default:
124 		/* unknown routing type */
125 		if (rh->ip6r_segleft == 0) {
126 			rhlen = (rh->ip6r_len + 1) << 3;
127 			break;	/* Final dst. Just ignore the header. */
128 		}
129 		ip6stat.ip6s_badoptions++;
130 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
131 			    (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
132 		return (IPPROTO_DONE);
133 	}
134 
135 	*offp += rhlen;
136 	return (rh->ip6r_nxt);
137 }
138 
139 #if 0
140 /*
141  * Type0 routing header processing
142  *
143  * RFC2292 backward compatibility warning: no support for strict/loose bitmap,
144  * as it was dropped between RFC1883 and RFC2460.
145  */
146 static int
147 ip6_rthdr0(struct mbuf *m, struct ip6_hdr *ip6, struct ip6_rthdr0 *rh0)
148 {
149 	int addrs, index;
150 	struct in6_addr *nextaddr, tmpaddr;
151 
152 	if (rh0->ip6r0_segleft == 0)
153 		return (0);
154 
155 	if (rh0->ip6r0_len % 2
156 #ifdef COMPAT_RFC1883
157 	    || rh0->ip6r0_len > 46
158 #endif
159 		) {
160 		/*
161 		 * Type 0 routing header can't contain more than 23 addresses.
162 		 * RFC 2462: this limitation was removed since strict/loose
163 		 * bitmap field was deleted.
164 		 */
165 		ip6stat.ip6s_badoptions++;
166 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
167 			    (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
168 		return (-1);
169 	}
170 
171 	if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
172 		ip6stat.ip6s_badoptions++;
173 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
174 			    (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
175 		return (-1);
176 	}
177 
178 	index = addrs - rh0->ip6r0_segleft;
179 	rh0->ip6r0_segleft--;
180 	/* note that ip6r0_addr does not exist in RFC2292bis */
181 	nextaddr = rh0->ip6r0_addr + index;
182 
183 	/*
184 	 * reject invalid addresses.  be proactive about malicious use of
185 	 * IPv4 mapped/compat address.
186 	 * XXX need more checks?
187 	 */
188 	if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
189 	    IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
190 	    IN6_IS_ADDR_V4MAPPED(nextaddr) ||
191 	    IN6_IS_ADDR_V4COMPAT(nextaddr)) {
192 		ip6stat.ip6s_badoptions++;
193 		m_freem(m);
194 		return (-1);
195 	}
196 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
197 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
198 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
199 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
200 		ip6stat.ip6s_badoptions++;
201 		m_freem(m);
202 		return (-1);
203 	}
204 
205 	/*
206 	 * Swap the IPv6 destination address and nextaddr. Forward the packet.
207 	 */
208 	tmpaddr = *nextaddr;
209 	*nextaddr = ip6->ip6_dst;
210 	if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
211 		nextaddr->s6_addr16[1] = 0;
212 	ip6->ip6_dst = tmpaddr;
213 	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
214 		ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
215 
216 #ifdef COMPAT_RFC1883
217 	if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
218 		ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
219 	else
220 		ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
221 #else
222 	ip6_forward(m, 1);
223 #endif
224 
225 	return (-1);			/* m would be freed in ip6_forward() */
226 }
227 #endif
228