2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
34 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet6/in6.h>
40 #include <netinet6/in6_var.h>
41 #include <netinet6/ip6.h>
42 #include <netinet6/ip6_var.h>
44 #include <netinet/icmp6.h>
46 static int ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *, struct ip6_rthdr0 *));
49 route6_input(mp, offp, proto)
51 int *offp, proto; /* proto is unused */
53 register struct ip6_hdr *ip6;
54 register struct mbuf *m = *mp;
55 register struct ip6_rthdr *rh;
56 int off = *offp, rhlen;
58 IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
59 ip6 = mtod(m, struct ip6_hdr *);
60 rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
62 switch(rh->ip6r_type) {
63 case IPV6_RTHDR_TYPE_0:
64 rhlen = (rh->ip6r_len + 1) << 3;
65 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
66 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
70 /* unknown routing type */
71 if (rh->ip6r_segleft == 0) {
72 rhlen = (rh->ip6r_len + 1) << 3;
73 break; /* Final dst. Just ignore the header. */
75 ip6stat.ip6s_badoptions++;
76 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
77 (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
86 * Type0 routing header processing
89 ip6_rthdr0(m, ip6, rh0)
92 struct ip6_rthdr0 *rh0;
95 struct in6_addr *nextaddr, tmpaddr;
97 if (rh0->ip6r0_segleft == 0)
100 if (rh0->ip6r0_len % 2
101 #ifdef COMPAT_RFC1883
102 || rh0->ip6r0_len > 46
106 * Type 0 routing header can't contain more than 23 addresses.
107 * RFC 2462: this limitation was removed since stict/loose
108 * bitmap field was deleted.
110 ip6stat.ip6s_badoptions++;
111 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
112 (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
116 if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
117 ip6stat.ip6s_badoptions++;
118 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
119 (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
123 index = addrs - rh0->ip6r0_segleft;
124 rh0->ip6r0_segleft--;
125 nextaddr = rh0->ip6r0_addr + index;
127 if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
128 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
129 ip6stat.ip6s_badoptions++;
135 * Swap the IPv6 destination address and nextaddr. Forward the packet.
138 *nextaddr = ip6->ip6_dst;
139 if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
140 nextaddr->s6_addr16[1] = 0;
141 ip6->ip6_dst = tmpaddr;
142 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
143 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
145 #ifdef COMPAT_RFC1883
146 if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
147 ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
149 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
154 return(-1); /* m would be freed in ip6_forward() */