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.7 2007/04/25 21:55:33 dillon Exp $ */
3 /* $KAME: route6.c,v 1.24 2001/03/14 03:07:05 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 #include "opt_inet6.h"
37 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/ip6.h>
48 #include <netinet6/ip6_var.h>
50 #include <netinet/icmp6.h>
52 static int ip6_rthdr0 (struct mbuf *, struct ip6_hdr *,
56 route6_input(struct mbuf **mp, int *offp, int proto) /* proto is unused */
61 int off = *offp, rhlen;
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);
68 ip6 = mtod(m, struct ip6_hdr *);
69 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
71 ip6stat.ip6s_tooshort++;
76 switch (rh->ip6r_type) {
77 case IPV6_RTHDR_TYPE_0:
78 if ((ip6_forwarding && ip6_rht0 == 0) || ip6_rht0 > 0) {
79 rhlen = (rh->ip6r_len + 1) << 3;
82 * note on option length:
83 * due to IP6_EXTHDR_CHECK assumption, we cannot handle
84 * very big routing header (max rhlen == 2048).
86 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
89 * note on option length:
91 * max mbuf m_pulldown can handle: MCLBYTES == usually
92 * 2048 so, here we are assuming that m_pulldown can
93 * handle rhlen == 2048 case. this may not be a good
94 * thing to assume - we may want to avoid pulling it
97 IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
99 ip6stat.ip6s_tooshort++;
103 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
104 return (IPPROTO_DONE);
109 /* unknown routing type */
110 if (rh->ip6r_segleft == 0) {
111 rhlen = (rh->ip6r_len + 1) << 3;
112 break; /* Final dst. Just ignore the header. */
114 ip6stat.ip6s_badoptions++;
115 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
116 (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
117 return (IPPROTO_DONE);
121 return (rh->ip6r_nxt);
125 * Type0 routing header processing
127 * RFC2292 backward compatibility warning: no support for strict/loose bitmap,
128 * as it was dropped between RFC1883 and RFC2460.
131 ip6_rthdr0(struct mbuf *m, struct ip6_hdr *ip6, struct ip6_rthdr0 *rh0)
134 struct in6_addr *nextaddr, tmpaddr;
136 if (rh0->ip6r0_segleft == 0)
139 if (rh0->ip6r0_len % 2
140 #ifdef COMPAT_RFC1883
141 || rh0->ip6r0_len > 46
145 * Type 0 routing header can't contain more than 23 addresses.
146 * RFC 2462: this limitation was removed since strict/loose
147 * bitmap field was deleted.
149 ip6stat.ip6s_badoptions++;
150 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
151 (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
155 if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
156 ip6stat.ip6s_badoptions++;
157 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
158 (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
162 index = addrs - rh0->ip6r0_segleft;
163 rh0->ip6r0_segleft--;
164 /* note that ip6r0_addr does not exist in RFC2292bis */
165 nextaddr = rh0->ip6r0_addr + index;
168 * reject invalid addresses. be proactive about malicious use of
169 * IPv4 mapped/compat address.
170 * XXX need more checks?
172 if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
173 IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
174 IN6_IS_ADDR_V4MAPPED(nextaddr) ||
175 IN6_IS_ADDR_V4COMPAT(nextaddr)) {
176 ip6stat.ip6s_badoptions++;
180 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
181 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
182 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
183 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
184 ip6stat.ip6s_badoptions++;
190 * Swap the IPv6 destination address and nextaddr. Forward the packet.
193 *nextaddr = ip6->ip6_dst;
194 if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
195 nextaddr->s6_addr16[1] = 0;
196 ip6->ip6_dst = tmpaddr;
197 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
198 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
200 #ifdef COMPAT_RFC1883
201 if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
202 ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
204 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
209 return (-1); /* m would be freed in ip6_forward() */