1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// src/sys/netinet6/route6.c
|
4 |
|
|
//
|
5 |
|
|
//==========================================================================
|
6 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
7 |
|
|
//
|
8 |
|
|
// -------------------------------------------
|
9 |
|
|
//
|
10 |
|
|
// Portions of this software may have been derived from OpenBSD,
|
11 |
|
|
// FreeBSD or other sources, and are covered by the appropriate
|
12 |
|
|
// copyright disclaimers included herein.
|
13 |
|
|
//
|
14 |
|
|
// Portions created by Red Hat are
|
15 |
|
|
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
16 |
|
|
//
|
17 |
|
|
// -------------------------------------------
|
18 |
|
|
//
|
19 |
|
|
//####BSDCOPYRIGHTEND####
|
20 |
|
|
//==========================================================================
|
21 |
|
|
|
22 |
|
|
/* $KAME: route6.c,v 1.28 2001/09/21 08:50:05 keiichi Exp $ */
|
23 |
|
|
|
24 |
|
|
/*
|
25 |
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
26 |
|
|
* All rights reserved.
|
27 |
|
|
*
|
28 |
|
|
* Redistribution and use in source and binary forms, with or without
|
29 |
|
|
* modification, are permitted provided that the following conditions
|
30 |
|
|
* are met:
|
31 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
32 |
|
|
* notice, this list of conditions and the following disclaimer.
|
33 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
34 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
35 |
|
|
* documentation and/or other materials provided with the distribution.
|
36 |
|
|
* 3. Neither the name of the project nor the names of its contributors
|
37 |
|
|
* may be used to endorse or promote products derived from this software
|
38 |
|
|
* without specific prior written permission.
|
39 |
|
|
*
|
40 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
41 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
42 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
43 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
44 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
45 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
46 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
47 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
48 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
49 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
50 |
|
|
* SUCH DAMAGE.
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
#include <sys/param.h>
|
54 |
|
|
#include <sys/mbuf.h>
|
55 |
|
|
#include <sys/socket.h>
|
56 |
|
|
#include <sys/queue.h>
|
57 |
|
|
|
58 |
|
|
#include <net/if.h>
|
59 |
|
|
#ifdef NEW_STRUCT_ROUTE
|
60 |
|
|
#include <net/route.h>
|
61 |
|
|
#endif
|
62 |
|
|
|
63 |
|
|
#include <netinet/in.h>
|
64 |
|
|
#include <netinet6/in6_var.h>
|
65 |
|
|
#include <netinet/ip6.h>
|
66 |
|
|
#include <netinet6/ip6_var.h>
|
67 |
|
|
|
68 |
|
|
#include <netinet/icmp6.h>
|
69 |
|
|
|
70 |
|
|
#ifdef MIP6
|
71 |
|
|
#include <netinet6/mip6.h>
|
72 |
|
|
#endif
|
73 |
|
|
|
74 |
|
|
static int ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *,
|
75 |
|
|
struct ip6_rthdr0 *));
|
76 |
|
|
|
77 |
|
|
int
|
78 |
|
|
route6_input(mp, offp, proto)
|
79 |
|
|
struct mbuf **mp;
|
80 |
|
|
int *offp, proto; /* proto is unused */
|
81 |
|
|
{
|
82 |
|
|
struct ip6_hdr *ip6;
|
83 |
|
|
struct mbuf *m = *mp;
|
84 |
|
|
struct ip6_rthdr *rh;
|
85 |
|
|
int off = *offp, rhlen;
|
86 |
|
|
struct mbuf *n;
|
87 |
|
|
|
88 |
|
|
n = ip6_findaux(m);
|
89 |
|
|
if (n) {
|
90 |
|
|
struct ip6aux *ip6a = mtod(n, struct ip6aux *);
|
91 |
|
|
/* XXX reject home-address option before rthdr */
|
92 |
|
|
if (ip6a->ip6a_flags & IP6A_SWAP) {
|
93 |
|
|
ip6stat.ip6s_badoptions++;
|
94 |
|
|
m_freem(m);
|
95 |
|
|
return IPPROTO_DONE;
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
#ifndef PULLDOWN_TEST
|
100 |
|
|
IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
|
101 |
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
102 |
|
|
rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
|
103 |
|
|
#else
|
104 |
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
105 |
|
|
IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
|
106 |
|
|
if (rh == NULL) {
|
107 |
|
|
ip6stat.ip6s_tooshort++;
|
108 |
|
|
return IPPROTO_DONE;
|
109 |
|
|
}
|
110 |
|
|
#endif
|
111 |
|
|
|
112 |
|
|
switch (rh->ip6r_type) {
|
113 |
|
|
case IPV6_RTHDR_TYPE_0:
|
114 |
|
|
rhlen = (rh->ip6r_len + 1) << 3;
|
115 |
|
|
#ifndef PULLDOWN_TEST
|
116 |
|
|
/*
|
117 |
|
|
* note on option length:
|
118 |
|
|
* due to IP6_EXTHDR_CHECK assumption, we cannot handle
|
119 |
|
|
* very big routing header (max rhlen == 2048).
|
120 |
|
|
*/
|
121 |
|
|
IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
|
122 |
|
|
#else
|
123 |
|
|
/*
|
124 |
|
|
* note on option length:
|
125 |
|
|
* maximum rhlen: 2048
|
126 |
|
|
* max mbuf m_pulldown can handle: MCLBYTES == usually 2048
|
127 |
|
|
* so, here we are assuming that m_pulldown can handle
|
128 |
|
|
* rhlen == 2048 case. this may not be a good thing to
|
129 |
|
|
* assume - we may want to avoid pulling it up altogether.
|
130 |
|
|
*/
|
131 |
|
|
IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
|
132 |
|
|
if (rh == NULL) {
|
133 |
|
|
ip6stat.ip6s_tooshort++;
|
134 |
|
|
return IPPROTO_DONE;
|
135 |
|
|
}
|
136 |
|
|
#endif
|
137 |
|
|
if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
|
138 |
|
|
return(IPPROTO_DONE);
|
139 |
|
|
break;
|
140 |
|
|
default:
|
141 |
|
|
/* unknown routing type */
|
142 |
|
|
if (rh->ip6r_segleft == 0) {
|
143 |
|
|
rhlen = (rh->ip6r_len + 1) << 3;
|
144 |
|
|
break; /* Final dst. Just ignore the header. */
|
145 |
|
|
}
|
146 |
|
|
ip6stat.ip6s_badoptions++;
|
147 |
|
|
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
|
148 |
|
|
(caddr_t)&rh->ip6r_type - (caddr_t)ip6);
|
149 |
|
|
return(IPPROTO_DONE);
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
*offp += rhlen;
|
153 |
|
|
return(rh->ip6r_nxt);
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/*
|
157 |
|
|
* Type0 routing header processing
|
158 |
|
|
*
|
159 |
|
|
* RFC2292 backward compatibility warning: no support for strict/loose bitmap,
|
160 |
|
|
* as it was dropped between RFC1883 and RFC2460.
|
161 |
|
|
*/
|
162 |
|
|
static int
|
163 |
|
|
ip6_rthdr0(m, ip6, rh0)
|
164 |
|
|
struct mbuf *m;
|
165 |
|
|
struct ip6_hdr *ip6;
|
166 |
|
|
struct ip6_rthdr0 *rh0;
|
167 |
|
|
{
|
168 |
|
|
int addrs, index;
|
169 |
|
|
struct in6_addr *nextaddr, tmpaddr;
|
170 |
|
|
|
171 |
|
|
if (rh0->ip6r0_segleft == 0)
|
172 |
|
|
#ifdef MIP6
|
173 |
|
|
{
|
174 |
|
|
struct hif_softc *sc;
|
175 |
|
|
struct mip6_bu *mbu;
|
176 |
|
|
int lastindex;
|
177 |
|
|
struct in6_addr *prevhop;
|
178 |
|
|
struct mbuf *n;
|
179 |
|
|
struct ip6aux *ip6a;
|
180 |
|
|
|
181 |
|
|
if (!MIP6_IS_MN)
|
182 |
|
|
return (0);
|
183 |
|
|
|
184 |
|
|
/*
|
185 |
|
|
* check if the ip6_dst is my home address or not. if
|
186 |
|
|
* true, check if the previous hop is the coa of the
|
187 |
|
|
* home address stored in the ip6_dst field. if above
|
188 |
|
|
* two is true, this packet is route optimized packet.
|
189 |
|
|
*/
|
190 |
|
|
for (sc = TAILQ_FIRST(&hif_softc_list);
|
191 |
|
|
sc;
|
192 |
|
|
sc = TAILQ_NEXT(sc, hif_entry)) {
|
193 |
|
|
for (mbu = LIST_FIRST(&sc->hif_bu_list);
|
194 |
|
|
mbu;
|
195 |
|
|
mbu = LIST_NEXT(mbu, mbu_entry)) {
|
196 |
|
|
if ((mbu->mbu_flags & IP6_BUF_HOME) == 0)
|
197 |
|
|
continue;
|
198 |
|
|
|
199 |
|
|
/* XXX registration status ? */
|
200 |
|
|
|
201 |
|
|
/* check the final dest is a home address. */
|
202 |
|
|
if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
|
203 |
|
|
&mbu->mbu_haddr))
|
204 |
|
|
continue;
|
205 |
|
|
|
206 |
|
|
/* check the last hop is a coa. */
|
207 |
|
|
lastindex = rh0->ip6r0_len / 2;
|
208 |
|
|
prevhop = (struct in6_addr *)(rh0 + 1)
|
209 |
|
|
+ lastindex - 1;
|
210 |
|
|
if (!IN6_ARE_ADDR_EQUAL(prevhop,
|
211 |
|
|
&mbu->mbu_coa))
|
212 |
|
|
continue;
|
213 |
|
|
|
214 |
|
|
/*
|
215 |
|
|
* route is already optimized. set
|
216 |
|
|
* optimized flag in m_aux.
|
217 |
|
|
*/
|
218 |
|
|
n = ip6_addaux(m);
|
219 |
|
|
if (n) {
|
220 |
|
|
ip6a = mtod(n, struct ip6aux *);
|
221 |
|
|
ip6a->ip6a_flags = IP6A_ROUTEOPTIMIZED;
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
return (0);
|
226 |
|
|
}
|
227 |
|
|
#else /* MIP6 */
|
228 |
|
|
return(0);
|
229 |
|
|
#endif /* MIP6 */
|
230 |
|
|
|
231 |
|
|
if (rh0->ip6r0_len % 2
|
232 |
|
|
#ifdef COMPAT_RFC1883
|
233 |
|
|
|| rh0->ip6r0_len > 46
|
234 |
|
|
#endif
|
235 |
|
|
) {
|
236 |
|
|
/*
|
237 |
|
|
* Type 0 routing header can't contain more than 23 addresses.
|
238 |
|
|
* RFC 2462: this limitation was removed since strict/loose
|
239 |
|
|
* bitmap field was deleted.
|
240 |
|
|
*/
|
241 |
|
|
ip6stat.ip6s_badoptions++;
|
242 |
|
|
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
|
243 |
|
|
(caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
|
244 |
|
|
return(-1);
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
|
248 |
|
|
ip6stat.ip6s_badoptions++;
|
249 |
|
|
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
|
250 |
|
|
(caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
|
251 |
|
|
return(-1);
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
index = addrs - rh0->ip6r0_segleft;
|
255 |
|
|
rh0->ip6r0_segleft--;
|
256 |
|
|
nextaddr = ((struct in6_addr *)(rh0 + 1)) + index;
|
257 |
|
|
|
258 |
|
|
/*
|
259 |
|
|
* reject invalid addresses. be proactive about malicious use of
|
260 |
|
|
* IPv4 mapped/compat address.
|
261 |
|
|
* XXX need more checks?
|
262 |
|
|
*/
|
263 |
|
|
if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
|
264 |
|
|
IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
|
265 |
|
|
IN6_IS_ADDR_V4MAPPED(nextaddr) ||
|
266 |
|
|
IN6_IS_ADDR_V4COMPAT(nextaddr)) {
|
267 |
|
|
ip6stat.ip6s_badoptions++;
|
268 |
|
|
m_freem(m);
|
269 |
|
|
return(-1);
|
270 |
|
|
}
|
271 |
|
|
if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
|
272 |
|
|
IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
|
273 |
|
|
IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
|
274 |
|
|
IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
|
275 |
|
|
ip6stat.ip6s_badoptions++;
|
276 |
|
|
m_freem(m);
|
277 |
|
|
return(-1);
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
/*
|
281 |
|
|
* Swap the IPv6 destination address and nextaddr. Forward the packet.
|
282 |
|
|
*/
|
283 |
|
|
tmpaddr = *nextaddr;
|
284 |
|
|
*nextaddr = ip6->ip6_dst;
|
285 |
|
|
if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
|
286 |
|
|
nextaddr->s6_addr16[1] = 0;
|
287 |
|
|
ip6->ip6_dst = tmpaddr;
|
288 |
|
|
if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
|
289 |
|
|
ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
|
290 |
|
|
|
291 |
|
|
#ifdef COMPAT_RFC1883
|
292 |
|
|
if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
|
293 |
|
|
ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
|
294 |
|
|
else
|
295 |
|
|
ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
|
296 |
|
|
#else
|
297 |
|
|
ip6_forward(m, 1);
|
298 |
|
|
#endif
|
299 |
|
|
|
300 |
|
|
return(-1); /* m would be freed in ip6_forward() */
|
301 |
|
|
}
|