1/*	$FreeBSD: src/sys/netinet6/route6.c,v 1.1.2.3 2001/07/03 11:01:55 ume Exp $	*/
2/*	$KAME: route6.c,v 1.24 2001/03/14 03:07:05 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 <sys/param.h>
34#include <sys/mbuf.h>
35#include <sys/socket.h>
36#include <sys/queue.h>
37#include <string.h>
38
39#include <net/if.h>
40#include <net/route.h>
41
42#include <netinet/in.h>
43#include <netinet6/in6_var.h>
44#include <netinet/ip6.h>
45#include <netinet6/ip6_var.h>
46
47#include <netinet/icmp6.h>
48
49#if IP6_RTHDR0_ALLOWED
50static int ip6_rthdr0(struct mbuf *, struct ip6_hdr *,
51    struct ip6_rthdr0 *);
52#endif /* IP6_RTHDR0_ALLOWED */
53
54int
55route6_input(struct mbuf **mp, int *offp)
56{
57	struct ip6_hdr *ip6;
58	struct mbuf *m = *mp;
59	struct ip6_rthdr *rh;
60	int off = *offp, rhlen;
61	struct ip6aux *ip6a;
62
63	ip6a = ip6_findaux(m);
64	if (ip6a) {
65		/* XXX reject home-address option before rthdr */
66		if (ip6a->ip6a_flags & IP6A_SWAP) {
67			ip6stat.ip6s_badoptions++;
68			m_freem(m);
69			return IPPROTO_DONE;
70		}
71	}
72
73#ifndef PULLDOWN_TEST
74	IP6_EXTHDR_CHECK(m, off, sizeof(*rh), return IPPROTO_DONE);
75	ip6 = mtod(m, struct ip6_hdr *);
76	rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
77#else
78	ip6 = mtod(m, struct ip6_hdr *);
79	IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, sizeof(*rh));
80	if (rh == NULL) {
81		ip6stat.ip6s_tooshort++;
82		return IPPROTO_DONE;
83	}
84#endif
85
86	switch (rh->ip6r_type) {
87#if IP6_RTHDR0_ALLOWED
88	case IPV6_RTHDR_TYPE_0:
89		rhlen = (rh->ip6r_len + 1) << 3;
90#ifndef PULLDOWN_TEST
91		/*
92		 * note on option length:
93		 * due to IP6_EXTHDR_CHECK assumption, we cannot handle
94		 * very big routing header (max rhlen == 2048).
95		 */
96		IP6_EXTHDR_CHECK(m, off, rhlen, return IPPROTO_DONE);
97#else
98		/*
99		 * note on option length:
100		 * maximum rhlen: 2048
101		 * max mbuf m_pulldown can handle: MCLBYTES == usually 2048
102		 * so, here we are assuming that m_pulldown can handle
103		 * rhlen == 2048 case.  this may not be a good thing to
104		 * assume - we may want to avoid pulling it up altogether.
105		 */
106		IP6_EXTHDR_GET(rh, struct ip6_rthdr *, m, off, rhlen);
107		if (rh == NULL) {
108			ip6stat.ip6s_tooshort++;
109			return IPPROTO_DONE;
110		}
111#endif
112		if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
113			return(IPPROTO_DONE);
114		break;
115#endif /* IP6_RTHDR0_ALLOWED */
116	default:
117		/* unknown routing type */
118		if (rh->ip6r_segleft == 0) {
119			rhlen = (rh->ip6r_len + 1) << 3;
120			break;	/* Final dst. Just ignore the header. */
121		}
122		ip6stat.ip6s_badoptions++;
123		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
124			    (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
125		return(IPPROTO_DONE);
126	}
127
128	*offp += rhlen;
129	return(rh->ip6r_nxt);
130}
131
132#if IP6_RTHDR0_ALLOWED
133/*
134 * Type0 routing header processing
135 *
136 * RFC2292 backward compatibility warning: no support for strict/loose bitmap,
137 * as it was dropped between RFC1883 and RFC2460.
138 */
139static int
140ip6_rthdr0(m, ip6, rh0)
141	struct mbuf *m;
142	struct ip6_hdr *ip6;
143	struct ip6_rthdr0 *rh0;
144{
145	int addrs, index;
146	struct in6_addr *nextaddr, tmpaddr;
147	struct route_in6 ip6forward_rt;
148
149	if (rh0->ip6r0_segleft == 0)
150		return(0);
151
152	if (rh0->ip6r0_len % 2
153#if COMPAT_RFC1883
154	    || rh0->ip6r0_len > 46
155#endif
156		) {
157		/*
158		 * Type 0 routing header can't contain more than 23 addresses.
159		 * RFC 2462: this limitation was removed since stict/loose
160		 * bitmap field was deleted.
161		 */
162		ip6stat.ip6s_badoptions++;
163		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
164			    (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
165		return(-1);
166	}
167
168	if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
169		ip6stat.ip6s_badoptions++;
170		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
171			    (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
172		return(-1);
173	}
174
175	index = addrs - rh0->ip6r0_segleft;
176	rh0->ip6r0_segleft--;
177	/* note that ip6r0_addr does not exist in RFC2292bis */
178	nextaddr = rh0->ip6r0_addr + index;
179
180	/*
181	 * reject invalid addresses.  be proactive about malicious use of
182	 * IPv4 mapped/compat address.
183	 * XXX need more checks?
184	 */
185	if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
186	    IN6_IS_ADDR_UNSPECIFIED(nextaddr) ||
187	    IN6_IS_ADDR_V4MAPPED(nextaddr) ||
188	    IN6_IS_ADDR_V4COMPAT(nextaddr)) {
189		ip6stat.ip6s_badoptions++;
190		m_freem(m);
191		return(-1);
192	}
193	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
194	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst) ||
195	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst) ||
196	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
197		ip6stat.ip6s_badoptions++;
198		m_freem(m);
199		return(-1);
200	}
201
202	/*
203	 * Swap the IPv6 destination address and nextaddr. Forward the packet.
204	 */
205	tmpaddr = *nextaddr;
206	*nextaddr = ip6->ip6_dst;
207	if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
208		nextaddr->s6_addr16[1] = 0;
209	ip6->ip6_dst = tmpaddr;
210	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
211		ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
212
213	/*
214	 * Don't use the globally cached route to forward packet having
215	 * Type 0 routing header(s); instead, do an explicit lookup using
216	 * a local route entry variable, in case the next address in the
217	 * packet is bogus (which would otherwise unnecessarily invalidate
218	 * the globally cached route).
219	 */
220	bzero(&ip6forward_rt, sizeof (ip6forward_rt));
221
222#if COMPAT_RFC1883
223	if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
224		ip6_forward(m, &ip6forward_rt, IPV6_SRCRT_NEIGHBOR, 0);
225	else
226		ip6_forward(m, &ip6forward_rt, IPV6_SRCRT_NOTNEIGHBOR, 0);
227#else
228	ip6_forward(m, &ip6forward_rt, 1, 0);
229#endif
230
231	/* Release reference to the looked up route */
232	if (ip6forward_rt.ro_rt != NULL) {
233		rtfree(ip6forward_rt.ro_rt);
234		ip6forward_rt.ro_rt = NULL;
235	}
236
237	return(-1);			/* m would be freed in ip6_forward() */
238}
239#endif /* IP6_RTHDR0_ALLOWED */
240
241