]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/route6.c
a0dc6c6a603decab6de889615a36868b2aa1a311
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/route6.c,v 1.1.2.3 2001/07/03 11:01:55 ume Exp $ */
30 /* $KAME: route6.c,v 1.24 2001/03/14 03:07:05 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <sys/queue.h>
68 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
75 #include <netinet/icmp6.h>
77 #if IP6_RTHDR0_ALLOWED
78 static int ip6_rthdr0(struct mbuf
*, struct ip6_hdr
*,
80 #endif /* IP6_RTHDR0_ALLOWED */
83 route6_input(struct mbuf
**mp
, int *offp
, int proto
)
89 int off
= *offp
, rhlen
;
92 ip6a
= ip6_findaux(m
);
94 /* XXX reject home-address option before rthdr */
95 if (ip6a
->ip6a_flags
& IP6A_SWAP
) {
96 ip6stat
.ip6s_badoptions
++;
102 #ifndef PULLDOWN_TEST
103 IP6_EXTHDR_CHECK(m
, off
, sizeof(*rh
), return IPPROTO_DONE
);
104 ip6
= mtod(m
, struct ip6_hdr
*);
105 rh
= (struct ip6_rthdr
*)((caddr_t
)ip6
+ off
);
107 ip6
= mtod(m
, struct ip6_hdr
*);
108 IP6_EXTHDR_GET(rh
, struct ip6_rthdr
*, m
, off
, sizeof(*rh
));
110 ip6stat
.ip6s_tooshort
++;
115 switch (rh
->ip6r_type
) {
116 #if IP6_RTHDR0_ALLOWED
117 case IPV6_RTHDR_TYPE_0
:
118 rhlen
= (rh
->ip6r_len
+ 1) << 3;
119 #ifndef PULLDOWN_TEST
121 * note on option length:
122 * due to IP6_EXTHDR_CHECK assumption, we cannot handle
123 * very big routing header (max rhlen == 2048).
125 IP6_EXTHDR_CHECK(m
, off
, rhlen
, return IPPROTO_DONE
);
128 * note on option length:
129 * maximum rhlen: 2048
130 * max mbuf m_pulldown can handle: MCLBYTES == usually 2048
131 * so, here we are assuming that m_pulldown can handle
132 * rhlen == 2048 case. this may not be a good thing to
133 * assume - we may want to avoid pulling it up altogether.
135 IP6_EXTHDR_GET(rh
, struct ip6_rthdr
*, m
, off
, rhlen
);
137 ip6stat
.ip6s_tooshort
++;
141 if (ip6_rthdr0(m
, ip6
, (struct ip6_rthdr0
*)rh
))
142 return(IPPROTO_DONE
);
144 #endif /* IP6_RTHDR0_ALLOWED */
146 /* unknown routing type */
147 if (rh
->ip6r_segleft
== 0) {
148 rhlen
= (rh
->ip6r_len
+ 1) << 3;
149 break; /* Final dst. Just ignore the header. */
151 ip6stat
.ip6s_badoptions
++;
152 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
153 (caddr_t
)&rh
->ip6r_type
- (caddr_t
)ip6
);
154 return(IPPROTO_DONE
);
158 return(rh
->ip6r_nxt
);
161 #if IP6_RTHDR0_ALLOWED
163 * Type0 routing header processing
165 * RFC2292 backward compatibility warning: no support for strict/loose bitmap,
166 * as it was dropped between RFC1883 and RFC2460.
169 ip6_rthdr0(m
, ip6
, rh0
)
172 struct ip6_rthdr0
*rh0
;
175 struct in6_addr
*nextaddr
, tmpaddr
, ia6
= NULL
;
176 struct route_in6 ip6forward_rt
;
178 if (rh0
->ip6r0_segleft
== 0)
181 if (rh0
->ip6r0_len
% 2
183 || rh0
->ip6r0_len
> 46
187 * Type 0 routing header can't contain more than 23 addresses.
188 * RFC 2462: this limitation was removed since strict/loose
189 * bitmap field was deleted.
191 ip6stat
.ip6s_badoptions
++;
192 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
193 (caddr_t
)&rh0
->ip6r0_len
- (caddr_t
)ip6
);
197 if ((addrs
= rh0
->ip6r0_len
/ 2) < rh0
->ip6r0_segleft
) {
198 ip6stat
.ip6s_badoptions
++;
199 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
200 (caddr_t
)&rh0
->ip6r0_segleft
- (caddr_t
)ip6
);
204 index
= addrs
- rh0
->ip6r0_segleft
;
205 rh0
->ip6r0_segleft
--;
206 /* note that ip6r0_addr does not exist in RFC2292bis */
207 nextaddr
= rh0
->ip6r0_addr
+ index
;
210 * reject invalid addresses. be proactive about malicious use of
211 * IPv4 mapped/compat address.
212 * XXX need more checks?
214 if (IN6_IS_ADDR_MULTICAST(nextaddr
) ||
215 IN6_IS_ADDR_UNSPECIFIED(nextaddr
) ||
216 IN6_IS_ADDR_V4MAPPED(nextaddr
) ||
217 IN6_IS_ADDR_V4COMPAT(nextaddr
)) {
218 ip6stat
.ip6s_badoptions
++;
222 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
223 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
) ||
224 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
) ||
225 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
226 ip6stat
.ip6s_badoptions
++;
232 * Determine the scope zone of the next hop, based on the interface
233 * of the current hop. [RFC4007, Section 9]
234 * Then disambiguate the scope zone for the next hop (if necessary).
236 if ((ia6
= ip6_getdstifaddr(m
)) == NULL
)
238 if (in6_setscope(nextaddr
, ia6
->ia_ifp
, NULL
) != 0) {
239 ip6stat
.ip6s_badscope
++;
240 IFA_REMREF(&ia6
->ia_ifa
);
244 IFA_REMREF(&ia6
->ia_ifa
);
248 * Swap the IPv6 destination address and nextaddr. Forward the packet.
251 *nextaddr
= ip6
->ip6_dst
;
252 in6_clearscope(nextaddr
); /* XXX */
253 ip6
->ip6_dst
= tmpaddr
;
254 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
255 ip6
->ip6_dst
.s6_addr16
[1] = htons(m
->m_pkthdr
.rcvif
->if_index
);
258 * Don't use the globally cached route to forward packet having
259 * Type 0 routing header(s); instead, do an explicit lookup using
260 * a local route entry variable, in case the next address in the
261 * packet is bogus (which would otherwise unnecessarily invalidate
262 * the globally cached route).
264 bzero(&ip6forward_rt
, sizeof (ip6forward_rt
));
267 if (rh0
->ip6r0_slmap
[index
/ 8] & (1 << (7 - (index
% 8))))
268 ip6_forward(m
, &ip6forward_rt
, IPV6_SRCRT_NEIGHBOR
, 0);
270 ip6_forward(m
, &ip6forward_rt
, IPV6_SRCRT_NOTNEIGHBOR
, 0);
272 ip6_forward(m
, &ip6forward_rt
, 1, 0);
275 /* Release reference to the looked up route */
276 if (ip6forward_rt
.ro_rt
!= NULL
) {
277 rtfree(ip6forward_rt
.ro_rt
);
278 ip6forward_rt
.ro_rt
= NULL
;
281 return(-1); /* m would be freed in ip6_forward() */
283 #endif /* IP6_RTHDR0_ALLOWED */