route6.c revision 1.2 1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33
34 #include <net/if.h>
35
36 #include <netinet/in.h>
37 #include <netinet6/in6.h>
38 #include <netinet6/ip6.h>
39 #include <netinet6/ip6_var.h>
40
41 #include <netinet/icmp6.h>
42
43 static int ip6_rthdr0 __P((struct mbuf *, struct ip6_hdr *, struct ip6_rthdr0 *));
44
45 int
46 route6_input(mp, offp, proto)
47 struct mbuf **mp;
48 int *offp, proto; /* proto is unused */
49 {
50 register struct ip6_hdr *ip6;
51 register struct mbuf *m = *mp;
52 register struct ip6_rthdr *rh;
53 int off = *offp, rhlen;
54
55 IP6_EXTHDR_CHECK(m, off, sizeof(*rh), IPPROTO_DONE);
56 ip6 = mtod(m, struct ip6_hdr *);
57 rh = (struct ip6_rthdr *)((caddr_t)ip6 + off);
58
59 switch(rh->ip6r_type) {
60 case IPV6_RTHDR_TYPE_0:
61 rhlen = (rh->ip6r_len + 1) << 3;
62 IP6_EXTHDR_CHECK(m, off, rhlen, IPPROTO_DONE);
63 if (ip6_rthdr0(m, ip6, (struct ip6_rthdr0 *)rh))
64 return(IPPROTO_DONE);
65 break;
66 default:
67 /* unknown routing type */
68 if (rh->ip6r_segleft == 0) {
69 rhlen = (rh->ip6r_len + 1) << 3;
70 break; /* Final dst. Just ignore the header. */
71 }
72 ip6stat.ip6s_badoptions++;
73 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
74 (caddr_t)&rh->ip6r_type - (caddr_t)ip6);
75 return(IPPROTO_DONE);
76 }
77
78 *offp += rhlen;
79 return(rh->ip6r_nxt);
80 }
81
82 /*
83 * Type0 routing header processing
84 */
85 static int
86 ip6_rthdr0(m, ip6, rh0)
87 struct mbuf *m;
88 struct ip6_hdr *ip6;
89 struct ip6_rthdr0 *rh0;
90 {
91 int addrs, index;
92 struct in6_addr *nextaddr, tmpaddr;
93
94 if (rh0->ip6r0_segleft == 0)
95 return(0);
96
97 if (rh0->ip6r0_len % 2
98 #ifdef COMPAT_RFC1883
99 || rh0->ip6r0_len > 46
100 #endif
101 ) {
102 /*
103 * Type 0 routing header can't contain more than 23 addresses.
104 * RFC 2462: this limitation was removed since stict/loose
105 * bitmap field was deleted.
106 */
107 ip6stat.ip6s_badoptions++;
108 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
109 (caddr_t)&rh0->ip6r0_len - (caddr_t)ip6);
110 return(-1);
111 }
112
113 if ((addrs = rh0->ip6r0_len / 2) < rh0->ip6r0_segleft) {
114 ip6stat.ip6s_badoptions++;
115 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
116 (caddr_t)&rh0->ip6r0_segleft - (caddr_t)ip6);
117 return(-1);
118 }
119
120 index = addrs - rh0->ip6r0_segleft;
121 rh0->ip6r0_segleft--;
122 nextaddr = rh0->ip6r0_addr + index;
123
124 if (IN6_IS_ADDR_MULTICAST(nextaddr) ||
125 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
126 ip6stat.ip6s_badoptions++;
127 m_freem(m);
128 return(-1);
129 }
130
131 /*
132 * Swap the IPv6 destination address and nextaddr. Forward the packet.
133 */
134 tmpaddr = *nextaddr;
135 *nextaddr = ip6->ip6_dst;
136 if (IN6_IS_ADDR_LINKLOCAL(nextaddr))
137 nextaddr->s6_addr16[1] = 0;
138 ip6->ip6_dst = tmpaddr;
139 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
140 ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
141
142 #ifdef COMPAT_RFC1883
143 if (rh0->ip6r0_slmap[index / 8] & (1 << (7 - (index % 8))))
144 ip6_forward(m, IPV6_SRCRT_NEIGHBOR);
145 else
146 ip6_forward(m, IPV6_SRCRT_NOTNEIGHBOR);
147 #else
148 ip6_forward(m, 1);
149 #endif
150
151 return(-1); /* m would be freed in ip6_forward() */
152 }
153