ip6_input.c revision 1.60 1 1.60 simonb /* $NetBSD: ip6_input.c,v 1.60 2003/01/20 05:30:11 simonb Exp $ */
2 1.40 itojun /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.21 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.21 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.2 itojun
33 1.2 itojun /*
34 1.2 itojun * Copyright (c) 1982, 1986, 1988, 1993
35 1.2 itojun * The Regents of the University of California. All rights reserved.
36 1.2 itojun *
37 1.2 itojun * Redistribution and use in source and binary forms, with or without
38 1.2 itojun * modification, are permitted provided that the following conditions
39 1.2 itojun * are met:
40 1.2 itojun * 1. Redistributions of source code must retain the above copyright
41 1.2 itojun * notice, this list of conditions and the following disclaimer.
42 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
43 1.2 itojun * notice, this list of conditions and the following disclaimer in the
44 1.2 itojun * documentation and/or other materials provided with the distribution.
45 1.2 itojun * 3. All advertising materials mentioning features or use of this software
46 1.2 itojun * must display the following acknowledgement:
47 1.2 itojun * This product includes software developed by the University of
48 1.2 itojun * California, Berkeley and its contributors.
49 1.2 itojun * 4. Neither the name of the University nor the names of its contributors
50 1.2 itojun * may be used to endorse or promote products derived from this software
51 1.2 itojun * without specific prior written permission.
52 1.2 itojun *
53 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.2 itojun * SUCH DAMAGE.
64 1.2 itojun *
65 1.2 itojun * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
66 1.2 itojun */
67 1.48 lukem
68 1.48 lukem #include <sys/cdefs.h>
69 1.60 simonb __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.60 2003/01/20 05:30:11 simonb Exp $");
70 1.2 itojun
71 1.2 itojun #include "opt_inet.h"
72 1.4 thorpej #include "opt_ipsec.h"
73 1.15 darrenr #include "opt_pfil_hooks.h"
74 1.2 itojun
75 1.2 itojun #include <sys/param.h>
76 1.2 itojun #include <sys/systm.h>
77 1.2 itojun #include <sys/malloc.h>
78 1.2 itojun #include <sys/mbuf.h>
79 1.2 itojun #include <sys/domain.h>
80 1.2 itojun #include <sys/protosw.h>
81 1.2 itojun #include <sys/socket.h>
82 1.2 itojun #include <sys/socketvar.h>
83 1.2 itojun #include <sys/errno.h>
84 1.2 itojun #include <sys/time.h>
85 1.2 itojun #include <sys/kernel.h>
86 1.2 itojun #include <sys/syslog.h>
87 1.2 itojun #include <sys/proc.h>
88 1.46 simonb #include <sys/sysctl.h>
89 1.2 itojun
90 1.2 itojun #include <net/if.h>
91 1.2 itojun #include <net/if_types.h>
92 1.2 itojun #include <net/if_dl.h>
93 1.2 itojun #include <net/route.h>
94 1.2 itojun #include <net/netisr.h>
95 1.15 darrenr #ifdef PFIL_HOOKS
96 1.15 darrenr #include <net/pfil.h>
97 1.15 darrenr #endif
98 1.2 itojun
99 1.2 itojun #include <netinet/in.h>
100 1.9 itojun #include <netinet/in_systm.h>
101 1.9 itojun #ifdef INET
102 1.9 itojun #include <netinet/ip.h>
103 1.9 itojun #include <netinet/ip_icmp.h>
104 1.45 itojun #endif /* INET */
105 1.14 itojun #include <netinet/ip6.h>
106 1.2 itojun #include <netinet6/in6_var.h>
107 1.11 itojun #include <netinet6/ip6_var.h>
108 1.2 itojun #include <netinet6/in6_pcb.h>
109 1.14 itojun #include <netinet/icmp6.h>
110 1.2 itojun #include <netinet6/in6_ifattach.h>
111 1.2 itojun #include <netinet6/nd6.h>
112 1.2 itojun
113 1.37 itojun #ifdef IPSEC
114 1.37 itojun #include <netinet6/ipsec.h>
115 1.37 itojun #endif
116 1.37 itojun
117 1.2 itojun #include <netinet6/ip6protosw.h>
118 1.2 itojun
119 1.2 itojun /* we need it for NLOOP. */
120 1.2 itojun #include "loop.h"
121 1.2 itojun #include "faith.h"
122 1.2 itojun #include "gif.h"
123 1.2 itojun #include "bpfilter.h"
124 1.50 itojun
125 1.50 itojun #if NGIF > 0
126 1.50 itojun #include <netinet6/in6_gif.h>
127 1.50 itojun #endif
128 1.2 itojun
129 1.9 itojun #include <net/net_osdep.h>
130 1.9 itojun
131 1.2 itojun extern struct domain inet6domain;
132 1.2 itojun
133 1.2 itojun u_char ip6_protox[IPPROTO_MAX];
134 1.2 itojun static int ip6qmaxlen = IFQ_MAXLEN;
135 1.2 itojun struct in6_ifaddr *in6_ifaddr;
136 1.2 itojun struct ifqueue ip6intrq;
137 1.2 itojun
138 1.2 itojun extern struct ifnet loif[NLOOP];
139 1.2 itojun int ip6_forward_srcrt; /* XXX */
140 1.2 itojun int ip6_sourcecheck; /* XXX */
141 1.2 itojun int ip6_sourcecheck_interval; /* XXX */
142 1.9 itojun
143 1.29 thorpej #ifdef PFIL_HOOKS
144 1.29 thorpej struct pfil_head inet6_pfil_hook;
145 1.29 thorpej #endif
146 1.29 thorpej
147 1.2 itojun struct ip6stat ip6stat;
148 1.2 itojun
149 1.2 itojun static void ip6_init2 __P((void *));
150 1.2 itojun
151 1.5 itojun static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
152 1.2 itojun
153 1.2 itojun /*
154 1.2 itojun * IP6 initialization: fill in IP6 protocol switch table.
155 1.2 itojun * All protocols not implemented in kernel go to raw IP6 protocol handler.
156 1.2 itojun */
157 1.2 itojun void
158 1.2 itojun ip6_init()
159 1.2 itojun {
160 1.35 itojun struct ip6protosw *pr;
161 1.35 itojun int i;
162 1.2 itojun
163 1.2 itojun pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
164 1.2 itojun if (pr == 0)
165 1.2 itojun panic("ip6_init");
166 1.2 itojun for (i = 0; i < IPPROTO_MAX; i++)
167 1.2 itojun ip6_protox[i] = pr - inet6sw;
168 1.2 itojun for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
169 1.2 itojun pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
170 1.2 itojun if (pr->pr_domain->dom_family == PF_INET6 &&
171 1.2 itojun pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
172 1.2 itojun ip6_protox[pr->pr_protocol] = pr - inet6sw;
173 1.2 itojun ip6intrq.ifq_maxlen = ip6qmaxlen;
174 1.2 itojun nd6_init();
175 1.2 itojun frag6_init();
176 1.54 itojun ip6_flow_seq = arc4random();
177 1.2 itojun
178 1.2 itojun ip6_init2((void *)0);
179 1.29 thorpej
180 1.29 thorpej #ifdef PFIL_HOOKS
181 1.29 thorpej /* Register our Packet Filter hook. */
182 1.33 thorpej inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
183 1.33 thorpej inet6_pfil_hook.ph_af = AF_INET6;
184 1.29 thorpej i = pfil_head_register(&inet6_pfil_hook);
185 1.29 thorpej if (i != 0)
186 1.29 thorpej printf("ip6_init: WARNING: unable to register pfil hook, "
187 1.29 thorpej "error %d\n", i);
188 1.29 thorpej #endif /* PFIL_HOOKS */
189 1.2 itojun }
190 1.2 itojun
191 1.2 itojun static void
192 1.2 itojun ip6_init2(dummy)
193 1.2 itojun void *dummy;
194 1.2 itojun {
195 1.2 itojun
196 1.2 itojun /* nd6_timer_init */
197 1.19 thorpej callout_init(&nd6_timer_ch);
198 1.19 thorpej callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
199 1.2 itojun }
200 1.2 itojun
201 1.2 itojun /*
202 1.2 itojun * IP6 input interrupt handling. Just pass the packet to ip6_input.
203 1.2 itojun */
204 1.2 itojun void
205 1.2 itojun ip6intr()
206 1.2 itojun {
207 1.2 itojun int s;
208 1.2 itojun struct mbuf *m;
209 1.2 itojun
210 1.2 itojun for (;;) {
211 1.41 thorpej s = splnet();
212 1.2 itojun IF_DEQUEUE(&ip6intrq, m);
213 1.2 itojun splx(s);
214 1.2 itojun if (m == 0)
215 1.2 itojun return;
216 1.2 itojun ip6_input(m);
217 1.2 itojun }
218 1.2 itojun }
219 1.2 itojun
220 1.2 itojun extern struct route_in6 ip6_forward_rt;
221 1.2 itojun
222 1.2 itojun void
223 1.2 itojun ip6_input(m)
224 1.2 itojun struct mbuf *m;
225 1.2 itojun {
226 1.9 itojun struct ip6_hdr *ip6;
227 1.2 itojun int off = sizeof(struct ip6_hdr), nest;
228 1.2 itojun u_int32_t plen;
229 1.5 itojun u_int32_t rtalert = ~0;
230 1.2 itojun int nxt, ours = 0;
231 1.9 itojun struct ifnet *deliverifp = NULL;
232 1.2 itojun
233 1.2 itojun #ifdef IPSEC
234 1.2 itojun /*
235 1.2 itojun * should the inner packet be considered authentic?
236 1.2 itojun * see comment in ah4_input().
237 1.2 itojun */
238 1.49 itojun m->m_flags &= ~M_AUTHIPHDR;
239 1.49 itojun m->m_flags &= ~M_AUTHIPDGM;
240 1.2 itojun #endif
241 1.9 itojun
242 1.2 itojun /*
243 1.44 itojun * mbuf statistics
244 1.2 itojun */
245 1.2 itojun if (m->m_flags & M_EXT) {
246 1.2 itojun if (m->m_next)
247 1.2 itojun ip6stat.ip6s_mext2m++;
248 1.2 itojun else
249 1.2 itojun ip6stat.ip6s_mext1++;
250 1.2 itojun } else {
251 1.40 itojun #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
252 1.2 itojun if (m->m_next) {
253 1.9 itojun if (m->m_flags & M_LOOP) {
254 1.44 itojun ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
255 1.40 itojun } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
256 1.2 itojun ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
257 1.2 itojun else
258 1.2 itojun ip6stat.ip6s_m2m[0]++;
259 1.2 itojun } else
260 1.2 itojun ip6stat.ip6s_m1++;
261 1.40 itojun #undef M2MMAX
262 1.2 itojun }
263 1.2 itojun
264 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
265 1.9 itojun ip6stat.ip6s_total++;
266 1.9 itojun
267 1.9 itojun #ifndef PULLDOWN_TEST
268 1.9 itojun /* XXX is the line really necessary? */
269 1.2 itojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
270 1.9 itojun #endif
271 1.2 itojun
272 1.57 thorpej /*
273 1.57 thorpej * If the IPv6 header is not aligned, slurp it up into a new
274 1.57 thorpej * mbuf with space for link headers, in the event we forward
275 1.57 thorpej * it. OTherwise, if it is aligned, make sure the entire base
276 1.57 thorpej * IPv6 header is in the first mbuf of the chain.
277 1.57 thorpej */
278 1.57 thorpej if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
279 1.57 thorpej struct ifnet *inifp = m->m_pkthdr.rcvif;
280 1.57 thorpej if ((m = m_copyup(m, sizeof(struct ip6_hdr),
281 1.57 thorpej (max_linkhdr + 3) & ~3)) == NULL) {
282 1.57 thorpej /* XXXJRT new stat, please */
283 1.57 thorpej ip6stat.ip6s_toosmall++;
284 1.57 thorpej in6_ifstat_inc(inifp, ifs6_in_hdrerr);
285 1.57 thorpej return;
286 1.57 thorpej }
287 1.57 thorpej } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
288 1.57 thorpej struct ifnet *inifp = m->m_pkthdr.rcvif;
289 1.55 itojun if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
290 1.9 itojun ip6stat.ip6s_toosmall++;
291 1.9 itojun in6_ifstat_inc(inifp, ifs6_in_hdrerr);
292 1.9 itojun return;
293 1.9 itojun }
294 1.2 itojun }
295 1.2 itojun
296 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
297 1.2 itojun
298 1.2 itojun if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
299 1.2 itojun ip6stat.ip6s_badvers++;
300 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
301 1.2 itojun goto bad;
302 1.2 itojun }
303 1.15 darrenr
304 1.15 darrenr #ifdef PFIL_HOOKS
305 1.15 darrenr /*
306 1.15 darrenr * Run through list of hooks for input packets. If there are any
307 1.15 darrenr * filters which require that additional packets in the flow are
308 1.15 darrenr * not fast-forwarded, they must clear the M_CANFASTFWD flag.
309 1.15 darrenr * Note that filters must _never_ set this flag, as another filter
310 1.15 darrenr * in the list may have previously cleared it.
311 1.15 darrenr */
312 1.39 itojun /*
313 1.39 itojun * let ipfilter look at packet on the wire,
314 1.39 itojun * not the decapsulated packet.
315 1.39 itojun */
316 1.39 itojun #ifdef IPSEC
317 1.42 itojun if (!ipsec_getnhist(m))
318 1.39 itojun #else
319 1.39 itojun if (1)
320 1.39 itojun #endif
321 1.39 itojun {
322 1.39 itojun if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
323 1.39 itojun PFIL_IN) != 0)
324 1.39 itojun return;
325 1.39 itojun if (m == NULL)
326 1.39 itojun return;
327 1.39 itojun ip6 = mtod(m, struct ip6_hdr *);
328 1.39 itojun }
329 1.15 darrenr #endif /* PFIL_HOOKS */
330 1.15 darrenr
331 1.2 itojun ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
332 1.2 itojun
333 1.30 thorpej #ifdef ALTQ
334 1.30 thorpej if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
335 1.30 thorpej /* packet is dropped by traffic conditioner */
336 1.30 thorpej return;
337 1.9 itojun }
338 1.9 itojun #endif
339 1.9 itojun
340 1.2 itojun /*
341 1.44 itojun * Check against address spoofing/corruption.
342 1.2 itojun */
343 1.2 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
344 1.2 itojun IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
345 1.55 itojun /*
346 1.55 itojun * XXX: "badscope" is not very suitable for a multicast source.
347 1.55 itojun */
348 1.2 itojun ip6stat.ip6s_badscope++;
349 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
350 1.2 itojun goto bad;
351 1.2 itojun }
352 1.13 itojun /*
353 1.44 itojun * The following check is not documented in specs. A malicious
354 1.44 itojun * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
355 1.44 itojun * and bypass security checks (act as if it was from 127.0.0.1 by using
356 1.44 itojun * IPv6 src ::ffff:127.0.0.1). Be cautious.
357 1.35 itojun *
358 1.44 itojun * This check chokes if we are in an SIIT cloud. As none of BSDs
359 1.44 itojun * support IPv4-less kernel compilation, we cannot support SIIT
360 1.44 itojun * environment at all. So, it makes more sense for us to reject any
361 1.44 itojun * malicious packets for non-SIIT environment, than try to do a
362 1.52 wiz * partial support for SIIT environment.
363 1.13 itojun */
364 1.13 itojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
365 1.13 itojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
366 1.13 itojun ip6stat.ip6s_badscope++;
367 1.13 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
368 1.13 itojun goto bad;
369 1.13 itojun }
370 1.17 itojun #if 0
371 1.13 itojun /*
372 1.17 itojun * Reject packets with IPv4 compatible addresses (auto tunnel).
373 1.17 itojun *
374 1.17 itojun * The code forbids auto tunnel relay case in RFC1933 (the check is
375 1.17 itojun * stronger than RFC1933). We may want to re-enable it if mech-xx
376 1.17 itojun * is revised to forbid relaying case.
377 1.13 itojun */
378 1.13 itojun if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
379 1.13 itojun IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
380 1.13 itojun ip6stat.ip6s_badscope++;
381 1.13 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
382 1.13 itojun goto bad;
383 1.13 itojun }
384 1.13 itojun #endif
385 1.35 itojun
386 1.2 itojun if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
387 1.2 itojun IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
388 1.2 itojun if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
389 1.2 itojun ours = 1;
390 1.9 itojun deliverifp = m->m_pkthdr.rcvif;
391 1.2 itojun goto hbhcheck;
392 1.2 itojun } else {
393 1.2 itojun ip6stat.ip6s_badscope++;
394 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
395 1.2 itojun goto bad;
396 1.2 itojun }
397 1.2 itojun }
398 1.2 itojun
399 1.38 itojun /* drop packets if interface ID portion is already filled */
400 1.38 itojun if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
401 1.38 itojun if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
402 1.38 itojun ip6->ip6_src.s6_addr16[1]) {
403 1.38 itojun ip6stat.ip6s_badscope++;
404 1.38 itojun goto bad;
405 1.38 itojun }
406 1.38 itojun if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
407 1.38 itojun ip6->ip6_dst.s6_addr16[1]) {
408 1.38 itojun ip6stat.ip6s_badscope++;
409 1.38 itojun goto bad;
410 1.38 itojun }
411 1.38 itojun }
412 1.2 itojun
413 1.40 itojun if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
414 1.40 itojun ip6->ip6_src.s6_addr16[1]
415 1.40 itojun = htons(m->m_pkthdr.rcvif->if_index);
416 1.40 itojun if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
417 1.40 itojun ip6->ip6_dst.s6_addr16[1]
418 1.40 itojun = htons(m->m_pkthdr.rcvif->if_index);
419 1.40 itojun
420 1.40 itojun /*
421 1.40 itojun * We use rt->rt_ifp to determine if the address is ours or not.
422 1.40 itojun * If rt_ifp is lo0, the address is ours.
423 1.40 itojun * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
424 1.40 itojun * so any address under fe80::%lo0/64 will be mistakenly considered
425 1.40 itojun * local. The special case is supplied to handle the case properly
426 1.40 itojun * by actually looking at interface addresses
427 1.40 itojun * (using in6ifa_ifpwithaddr).
428 1.40 itojun */
429 1.40 itojun if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
430 1.40 itojun IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
431 1.40 itojun if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
432 1.40 itojun icmp6_error(m, ICMP6_DST_UNREACH,
433 1.40 itojun ICMP6_DST_UNREACH_ADDR, 0);
434 1.40 itojun /* m is already freed */
435 1.40 itojun return;
436 1.21 itojun }
437 1.40 itojun
438 1.40 itojun ours = 1;
439 1.40 itojun deliverifp = m->m_pkthdr.rcvif;
440 1.40 itojun goto hbhcheck;
441 1.9 itojun }
442 1.9 itojun
443 1.2 itojun /*
444 1.2 itojun * Multicast check
445 1.2 itojun */
446 1.2 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
447 1.2 itojun struct in6_multi *in6m = 0;
448 1.9 itojun
449 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
450 1.2 itojun /*
451 1.2 itojun * See if we belong to the destination multicast group on the
452 1.2 itojun * arrival interface.
453 1.2 itojun */
454 1.2 itojun IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
455 1.2 itojun if (in6m)
456 1.2 itojun ours = 1;
457 1.2 itojun else if (!ip6_mrouter) {
458 1.2 itojun ip6stat.ip6s_notmember++;
459 1.2 itojun ip6stat.ip6s_cantforward++;
460 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
461 1.2 itojun goto bad;
462 1.2 itojun }
463 1.9 itojun deliverifp = m->m_pkthdr.rcvif;
464 1.2 itojun goto hbhcheck;
465 1.2 itojun }
466 1.2 itojun
467 1.2 itojun /*
468 1.2 itojun * Unicast check
469 1.2 itojun */
470 1.22 itojun if (ip6_forward_rt.ro_rt != NULL &&
471 1.55 itojun (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
472 1.22 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
473 1.35 itojun &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
474 1.25 itojun ip6stat.ip6s_forward_cachehit++;
475 1.22 itojun else {
476 1.35 itojun struct sockaddr_in6 *dst6;
477 1.35 itojun
478 1.2 itojun if (ip6_forward_rt.ro_rt) {
479 1.22 itojun /* route is down or destination is different */
480 1.25 itojun ip6stat.ip6s_forward_cachemiss++;
481 1.2 itojun RTFREE(ip6_forward_rt.ro_rt);
482 1.2 itojun ip6_forward_rt.ro_rt = 0;
483 1.2 itojun }
484 1.22 itojun
485 1.2 itojun bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
486 1.35 itojun dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
487 1.35 itojun dst6->sin6_len = sizeof(struct sockaddr_in6);
488 1.35 itojun dst6->sin6_family = AF_INET6;
489 1.35 itojun dst6->sin6_addr = ip6->ip6_dst;
490 1.2 itojun
491 1.9 itojun rtalloc((struct route *)&ip6_forward_rt);
492 1.2 itojun }
493 1.2 itojun
494 1.2 itojun #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
495 1.2 itojun
496 1.2 itojun /*
497 1.2 itojun * Accept the packet if the forwarding interface to the destination
498 1.2 itojun * according to the routing table is the loopback interface,
499 1.2 itojun * unless the associated route has a gateway.
500 1.2 itojun * Note that this approach causes to accept a packet if there is a
501 1.2 itojun * route to the loopback interface for the destination of the packet.
502 1.2 itojun * But we think it's even useful in some situations, e.g. when using
503 1.2 itojun * a special daemon which wants to intercept the packet.
504 1.2 itojun */
505 1.2 itojun if (ip6_forward_rt.ro_rt &&
506 1.2 itojun (ip6_forward_rt.ro_rt->rt_flags &
507 1.2 itojun (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
508 1.55 itojun !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
509 1.2 itojun #if 0
510 1.2 itojun /*
511 1.2 itojun * The check below is redundant since the comparison of
512 1.2 itojun * the destination and the key of the rtentry has
513 1.2 itojun * already done through looking up the routing table.
514 1.2 itojun */
515 1.2 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
516 1.55 itojun &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
517 1.2 itojun #endif
518 1.9 itojun ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
519 1.2 itojun struct in6_ifaddr *ia6 =
520 1.2 itojun (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
521 1.2 itojun if (ia6->ia6_flags & IN6_IFF_ANYCAST)
522 1.2 itojun m->m_flags |= M_ANYCAST6;
523 1.24 itojun /*
524 1.24 itojun * packets to a tentative, duplicated, or somehow invalid
525 1.24 itojun * address must not be accepted.
526 1.24 itojun */
527 1.2 itojun if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
528 1.24 itojun /* this address is ready */
529 1.2 itojun ours = 1;
530 1.9 itojun deliverifp = ia6->ia_ifp; /* correct? */
531 1.2 itojun goto hbhcheck;
532 1.2 itojun } else {
533 1.24 itojun /* address is not ready, so discard the packet. */
534 1.34 itojun nd6log((LOG_INFO,
535 1.27 itojun "ip6_input: packet to an unready address %s->%s\n",
536 1.24 itojun ip6_sprintf(&ip6->ip6_src),
537 1.34 itojun ip6_sprintf(&ip6->ip6_dst)));
538 1.24 itojun
539 1.24 itojun goto bad;
540 1.2 itojun }
541 1.2 itojun }
542 1.2 itojun
543 1.2 itojun /*
544 1.55 itojun * FAITH (Firewall Aided Internet Translator)
545 1.2 itojun */
546 1.2 itojun #if defined(NFAITH) && 0 < NFAITH
547 1.2 itojun if (ip6_keepfaith) {
548 1.2 itojun if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
549 1.2 itojun && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
550 1.2 itojun /* XXX do we need more sanity checks? */
551 1.2 itojun ours = 1;
552 1.45 itojun deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
553 1.9 itojun goto hbhcheck;
554 1.9 itojun }
555 1.9 itojun }
556 1.9 itojun #endif
557 1.9 itojun
558 1.9 itojun #if 0
559 1.9 itojun {
560 1.9 itojun /*
561 1.9 itojun * Last resort: check in6_ifaddr for incoming interface.
562 1.9 itojun * The code is here until I update the "goto ours hack" code above
563 1.9 itojun * working right.
564 1.9 itojun */
565 1.9 itojun struct ifaddr *ifa;
566 1.9 itojun for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
567 1.9 itojun ifa;
568 1.9 itojun ifa = ifa->ifa_list.tqe_next) {
569 1.9 itojun if (ifa->ifa_addr == NULL)
570 1.9 itojun continue; /* just for safety */
571 1.9 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
572 1.9 itojun continue;
573 1.9 itojun if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
574 1.9 itojun ours = 1;
575 1.9 itojun deliverifp = ifa->ifa_ifp;
576 1.2 itojun goto hbhcheck;
577 1.2 itojun }
578 1.2 itojun }
579 1.9 itojun }
580 1.2 itojun #endif
581 1.2 itojun
582 1.2 itojun /*
583 1.2 itojun * Now there is no reason to process the packet if it's not our own
584 1.2 itojun * and we're not a router.
585 1.2 itojun */
586 1.2 itojun if (!ip6_forwarding) {
587 1.2 itojun ip6stat.ip6s_cantforward++;
588 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
589 1.2 itojun goto bad;
590 1.2 itojun }
591 1.2 itojun
592 1.2 itojun hbhcheck:
593 1.2 itojun /*
594 1.2 itojun * Process Hop-by-Hop options header if it's contained.
595 1.2 itojun * m may be modified in ip6_hopopts_input().
596 1.2 itojun * If a JumboPayload option is included, plen will also be modified.
597 1.2 itojun */
598 1.2 itojun plen = (u_int32_t)ntohs(ip6->ip6_plen);
599 1.2 itojun if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
600 1.9 itojun struct ip6_hbh *hbh;
601 1.9 itojun
602 1.9 itojun if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
603 1.9 itojun #if 0 /*touches NULL pointer*/
604 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
605 1.9 itojun #endif
606 1.2 itojun return; /* m have already been freed */
607 1.9 itojun }
608 1.22 itojun
609 1.2 itojun /* adjust pointer */
610 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
611 1.22 itojun
612 1.22 itojun /*
613 1.49 itojun * if the payload length field is 0 and the next header field
614 1.22 itojun * indicates Hop-by-Hop Options header, then a Jumbo Payload
615 1.22 itojun * option MUST be included.
616 1.22 itojun */
617 1.22 itojun if (ip6->ip6_plen == 0 && plen == 0) {
618 1.22 itojun /*
619 1.22 itojun * Note that if a valid jumbo payload option is
620 1.22 itojun * contained, ip6_hoptops_input() must set a valid
621 1.55 itojun * (non-zero) payload length to the variable plen.
622 1.22 itojun */
623 1.22 itojun ip6stat.ip6s_badoptions++;
624 1.22 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
625 1.22 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
626 1.22 itojun icmp6_error(m, ICMP6_PARAM_PROB,
627 1.22 itojun ICMP6_PARAMPROB_HEADER,
628 1.22 itojun (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
629 1.22 itojun return;
630 1.22 itojun }
631 1.9 itojun #ifndef PULLDOWN_TEST
632 1.9 itojun /* ip6_hopopts_input() ensures that mbuf is contiguous */
633 1.9 itojun hbh = (struct ip6_hbh *)(ip6 + 1);
634 1.9 itojun #else
635 1.9 itojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
636 1.9 itojun sizeof(struct ip6_hbh));
637 1.9 itojun if (hbh == NULL) {
638 1.9 itojun ip6stat.ip6s_tooshort++;
639 1.9 itojun return;
640 1.9 itojun }
641 1.9 itojun #endif
642 1.57 thorpej KASSERT(IP6_HDR_ALIGNED_P(hbh));
643 1.9 itojun nxt = hbh->ip6h_nxt;
644 1.2 itojun
645 1.2 itojun /*
646 1.2 itojun * accept the packet if a router alert option is included
647 1.2 itojun * and we act as an IPv6 router.
648 1.2 itojun */
649 1.5 itojun if (rtalert != ~0 && ip6_forwarding)
650 1.2 itojun ours = 1;
651 1.2 itojun } else
652 1.2 itojun nxt = ip6->ip6_nxt;
653 1.2 itojun
654 1.2 itojun /*
655 1.2 itojun * Check that the amount of data in the buffers
656 1.2 itojun * is as at least much as the IPv6 header would have us expect.
657 1.2 itojun * Trim mbufs if longer than we expect.
658 1.2 itojun * Drop packet if shorter than we expect.
659 1.2 itojun */
660 1.2 itojun if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
661 1.2 itojun ip6stat.ip6s_tooshort++;
662 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
663 1.2 itojun goto bad;
664 1.2 itojun }
665 1.2 itojun if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
666 1.2 itojun if (m->m_len == m->m_pkthdr.len) {
667 1.2 itojun m->m_len = sizeof(struct ip6_hdr) + plen;
668 1.2 itojun m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
669 1.2 itojun } else
670 1.2 itojun m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
671 1.2 itojun }
672 1.2 itojun
673 1.2 itojun /*
674 1.2 itojun * Forward if desirable.
675 1.2 itojun */
676 1.2 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
677 1.2 itojun /*
678 1.2 itojun * If we are acting as a multicast router, all
679 1.2 itojun * incoming multicast packets are passed to the
680 1.2 itojun * kernel-level multicast forwarding function.
681 1.2 itojun * The packet is returned (relatively) intact; if
682 1.2 itojun * ip6_mforward() returns a non-zero value, the packet
683 1.2 itojun * must be discarded, else it may be accepted below.
684 1.2 itojun */
685 1.2 itojun if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
686 1.2 itojun ip6stat.ip6s_cantforward++;
687 1.2 itojun m_freem(m);
688 1.2 itojun return;
689 1.2 itojun }
690 1.2 itojun if (!ours) {
691 1.2 itojun m_freem(m);
692 1.2 itojun return;
693 1.2 itojun }
694 1.22 itojun } else if (!ours) {
695 1.2 itojun ip6_forward(m, 0);
696 1.2 itojun return;
697 1.56 itojun }
698 1.25 itojun
699 1.25 itojun ip6 = mtod(m, struct ip6_hdr *);
700 1.25 itojun
701 1.25 itojun /*
702 1.25 itojun * Malicious party may be able to use IPv4 mapped addr to confuse
703 1.25 itojun * tcp/udp stack and bypass security checks (act as if it was from
704 1.25 itojun * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
705 1.25 itojun *
706 1.25 itojun * For SIIT end node behavior, you may want to disable the check.
707 1.25 itojun * However, you will become vulnerable to attacks using IPv4 mapped
708 1.25 itojun * source.
709 1.25 itojun */
710 1.25 itojun if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
711 1.25 itojun IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
712 1.25 itojun ip6stat.ip6s_badscope++;
713 1.25 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
714 1.25 itojun goto bad;
715 1.25 itojun }
716 1.2 itojun
717 1.2 itojun /*
718 1.2 itojun * Tell launch routine the next header
719 1.2 itojun */
720 1.12 itojun #ifdef IFA_STATS
721 1.28 itojun if (deliverifp != NULL) {
722 1.9 itojun struct in6_ifaddr *ia6;
723 1.9 itojun ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
724 1.9 itojun if (ia6)
725 1.9 itojun ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
726 1.9 itojun }
727 1.9 itojun #endif
728 1.2 itojun ip6stat.ip6s_delivered++;
729 1.9 itojun in6_ifstat_inc(deliverifp, ifs6_in_deliver);
730 1.2 itojun nest = 0;
731 1.40 itojun
732 1.2 itojun while (nxt != IPPROTO_DONE) {
733 1.2 itojun if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
734 1.2 itojun ip6stat.ip6s_toomanyhdr++;
735 1.2 itojun goto bad;
736 1.2 itojun }
737 1.8 itojun
738 1.8 itojun /*
739 1.8 itojun * protection against faulty packet - there should be
740 1.8 itojun * more sanity checks in header chain processing.
741 1.8 itojun */
742 1.8 itojun if (m->m_pkthdr.len < off) {
743 1.8 itojun ip6stat.ip6s_tooshort++;
744 1.9 itojun in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
745 1.8 itojun goto bad;
746 1.8 itojun }
747 1.8 itojun
748 1.37 itojun #ifdef IPSEC
749 1.37 itojun /*
750 1.37 itojun * enforce IPsec policy checking if we are seeing last header.
751 1.37 itojun * note that we do not visit this with protocols with pcb layer
752 1.37 itojun * code - like udp/tcp/raw ip.
753 1.37 itojun */
754 1.37 itojun if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
755 1.37 itojun ipsec6_in_reject(m, NULL)) {
756 1.37 itojun ipsec6stat.in_polvio++;
757 1.37 itojun goto bad;
758 1.37 itojun }
759 1.37 itojun #endif
760 1.56 itojun
761 1.2 itojun nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
762 1.2 itojun }
763 1.2 itojun return;
764 1.2 itojun bad:
765 1.2 itojun m_freem(m);
766 1.2 itojun }
767 1.2 itojun
768 1.2 itojun /*
769 1.2 itojun * Hop-by-Hop options header processing. If a valid jumbo payload option is
770 1.2 itojun * included, the real payload length will be stored in plenp.
771 1.2 itojun */
772 1.2 itojun static int
773 1.2 itojun ip6_hopopts_input(plenp, rtalertp, mp, offp)
774 1.2 itojun u_int32_t *plenp;
775 1.5 itojun u_int32_t *rtalertp; /* XXX: should be stored more smart way */
776 1.2 itojun struct mbuf **mp;
777 1.2 itojun int *offp;
778 1.2 itojun {
779 1.35 itojun struct mbuf *m = *mp;
780 1.2 itojun int off = *offp, hbhlen;
781 1.2 itojun struct ip6_hbh *hbh;
782 1.2 itojun u_int8_t *opt;
783 1.2 itojun
784 1.2 itojun /* validation of the length of the header */
785 1.9 itojun #ifndef PULLDOWN_TEST
786 1.2 itojun IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
787 1.2 itojun hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
788 1.2 itojun hbhlen = (hbh->ip6h_len + 1) << 3;
789 1.2 itojun
790 1.2 itojun IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
791 1.2 itojun hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
792 1.9 itojun #else
793 1.9 itojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
794 1.9 itojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
795 1.9 itojun if (hbh == NULL) {
796 1.9 itojun ip6stat.ip6s_tooshort++;
797 1.9 itojun return -1;
798 1.9 itojun }
799 1.9 itojun hbhlen = (hbh->ip6h_len + 1) << 3;
800 1.9 itojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
801 1.9 itojun hbhlen);
802 1.9 itojun if (hbh == NULL) {
803 1.9 itojun ip6stat.ip6s_tooshort++;
804 1.9 itojun return -1;
805 1.9 itojun }
806 1.9 itojun #endif
807 1.57 thorpej KASSERT(IP6_HDR_ALIGNED_P(hbh));
808 1.2 itojun off += hbhlen;
809 1.2 itojun hbhlen -= sizeof(struct ip6_hbh);
810 1.2 itojun opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
811 1.2 itojun
812 1.2 itojun if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
813 1.2 itojun hbhlen, rtalertp, plenp) < 0)
814 1.58 itojun return (-1);
815 1.2 itojun
816 1.2 itojun *offp = off;
817 1.2 itojun *mp = m;
818 1.58 itojun return (0);
819 1.2 itojun }
820 1.2 itojun
821 1.2 itojun /*
822 1.2 itojun * Search header for all Hop-by-hop options and process each option.
823 1.2 itojun * This function is separate from ip6_hopopts_input() in order to
824 1.2 itojun * handle a case where the sending node itself process its hop-by-hop
825 1.2 itojun * options header. In such a case, the function is called from ip6_output().
826 1.55 itojun *
827 1.55 itojun * The function assumes that hbh header is located right after the IPv6 header
828 1.55 itojun * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
829 1.55 itojun * opthead + hbhlen is located in continuous memory region.
830 1.2 itojun */
831 1.2 itojun int
832 1.2 itojun ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
833 1.2 itojun struct mbuf *m;
834 1.2 itojun u_int8_t *opthead;
835 1.2 itojun int hbhlen;
836 1.5 itojun u_int32_t *rtalertp;
837 1.2 itojun u_int32_t *plenp;
838 1.2 itojun {
839 1.2 itojun struct ip6_hdr *ip6;
840 1.2 itojun int optlen = 0;
841 1.2 itojun u_int8_t *opt = opthead;
842 1.2 itojun u_int16_t rtalert_val;
843 1.22 itojun u_int32_t jumboplen;
844 1.55 itojun const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
845 1.2 itojun
846 1.2 itojun for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
847 1.35 itojun switch (*opt) {
848 1.35 itojun case IP6OPT_PAD1:
849 1.35 itojun optlen = 1;
850 1.35 itojun break;
851 1.35 itojun case IP6OPT_PADN:
852 1.35 itojun if (hbhlen < IP6OPT_MINLEN) {
853 1.35 itojun ip6stat.ip6s_toosmall++;
854 1.35 itojun goto bad;
855 1.35 itojun }
856 1.35 itojun optlen = *(opt + 1) + 2;
857 1.35 itojun break;
858 1.35 itojun case IP6OPT_RTALERT:
859 1.35 itojun /* XXX may need check for alignment */
860 1.35 itojun if (hbhlen < IP6OPT_RTALERT_LEN) {
861 1.35 itojun ip6stat.ip6s_toosmall++;
862 1.35 itojun goto bad;
863 1.35 itojun }
864 1.35 itojun if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
865 1.55 itojun /* XXX stat */
866 1.55 itojun icmp6_error(m, ICMP6_PARAM_PROB,
867 1.55 itojun ICMP6_PARAMPROB_HEADER,
868 1.55 itojun erroff + opt + 1 - opthead);
869 1.55 itojun return (-1);
870 1.35 itojun }
871 1.35 itojun optlen = IP6OPT_RTALERT_LEN;
872 1.35 itojun bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
873 1.35 itojun *rtalertp = ntohs(rtalert_val);
874 1.35 itojun break;
875 1.35 itojun case IP6OPT_JUMBO:
876 1.22 itojun /* XXX may need check for alignment */
877 1.22 itojun if (hbhlen < IP6OPT_JUMBO_LEN) {
878 1.22 itojun ip6stat.ip6s_toosmall++;
879 1.22 itojun goto bad;
880 1.22 itojun }
881 1.35 itojun if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
882 1.55 itojun /* XXX stat */
883 1.55 itojun icmp6_error(m, ICMP6_PARAM_PROB,
884 1.55 itojun ICMP6_PARAMPROB_HEADER,
885 1.55 itojun erroff + opt + 1 - opthead);
886 1.55 itojun return (-1);
887 1.35 itojun }
888 1.22 itojun optlen = IP6OPT_JUMBO_LEN;
889 1.22 itojun
890 1.22 itojun /*
891 1.22 itojun * IPv6 packets that have non 0 payload length
892 1.35 itojun * must not contain a jumbo payload option.
893 1.22 itojun */
894 1.22 itojun ip6 = mtod(m, struct ip6_hdr *);
895 1.22 itojun if (ip6->ip6_plen) {
896 1.22 itojun ip6stat.ip6s_badoptions++;
897 1.22 itojun icmp6_error(m, ICMP6_PARAM_PROB,
898 1.55 itojun ICMP6_PARAMPROB_HEADER,
899 1.55 itojun erroff + opt - opthead);
900 1.55 itojun return (-1);
901 1.22 itojun }
902 1.2 itojun
903 1.22 itojun /*
904 1.22 itojun * We may see jumbolen in unaligned location, so
905 1.22 itojun * we'd need to perform bcopy().
906 1.22 itojun */
907 1.22 itojun bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
908 1.22 itojun jumboplen = (u_int32_t)htonl(jumboplen);
909 1.22 itojun
910 1.22 itojun #if 1
911 1.22 itojun /*
912 1.22 itojun * if there are multiple jumbo payload options,
913 1.22 itojun * *plenp will be non-zero and the packet will be
914 1.22 itojun * rejected.
915 1.22 itojun * the behavior may need some debate in ipngwg -
916 1.22 itojun * multiple options does not make sense, however,
917 1.22 itojun * there's no explicit mention in specification.
918 1.22 itojun */
919 1.22 itojun if (*plenp != 0) {
920 1.22 itojun ip6stat.ip6s_badoptions++;
921 1.22 itojun icmp6_error(m, ICMP6_PARAM_PROB,
922 1.55 itojun ICMP6_PARAMPROB_HEADER,
923 1.55 itojun erroff + opt + 2 - opthead);
924 1.55 itojun return (-1);
925 1.22 itojun }
926 1.8 itojun #endif
927 1.2 itojun
928 1.22 itojun /*
929 1.22 itojun * jumbo payload length must be larger than 65535.
930 1.22 itojun */
931 1.22 itojun if (jumboplen <= IPV6_MAXPACKET) {
932 1.22 itojun ip6stat.ip6s_badoptions++;
933 1.22 itojun icmp6_error(m, ICMP6_PARAM_PROB,
934 1.55 itojun ICMP6_PARAMPROB_HEADER,
935 1.55 itojun erroff + opt + 2 - opthead);
936 1.55 itojun return (-1);
937 1.22 itojun }
938 1.22 itojun *plenp = jumboplen;
939 1.22 itojun
940 1.22 itojun break;
941 1.35 itojun default: /* unknown option */
942 1.35 itojun if (hbhlen < IP6OPT_MINLEN) {
943 1.35 itojun ip6stat.ip6s_toosmall++;
944 1.35 itojun goto bad;
945 1.35 itojun }
946 1.55 itojun optlen = ip6_unknown_opt(opt, m,
947 1.55 itojun erroff + opt - opthead);
948 1.55 itojun if (optlen == -1)
949 1.55 itojun return (-1);
950 1.35 itojun optlen += 2;
951 1.35 itojun break;
952 1.2 itojun }
953 1.2 itojun }
954 1.2 itojun
955 1.55 itojun return (0);
956 1.2 itojun
957 1.2 itojun bad:
958 1.2 itojun m_freem(m);
959 1.55 itojun return (-1);
960 1.2 itojun }
961 1.2 itojun
962 1.2 itojun /*
963 1.2 itojun * Unknown option processing.
964 1.2 itojun * The third argument `off' is the offset from the IPv6 header to the option,
965 1.2 itojun * which is necessary if the IPv6 header the and option header and IPv6 header
966 1.2 itojun * is not continuous in order to return an ICMPv6 error.
967 1.2 itojun */
968 1.2 itojun int
969 1.2 itojun ip6_unknown_opt(optp, m, off)
970 1.2 itojun u_int8_t *optp;
971 1.2 itojun struct mbuf *m;
972 1.2 itojun int off;
973 1.2 itojun {
974 1.2 itojun struct ip6_hdr *ip6;
975 1.2 itojun
976 1.35 itojun switch (IP6OPT_TYPE(*optp)) {
977 1.35 itojun case IP6OPT_TYPE_SKIP: /* ignore the option */
978 1.58 itojun return ((int)*(optp + 1));
979 1.35 itojun case IP6OPT_TYPE_DISCARD: /* silently discard */
980 1.35 itojun m_freem(m);
981 1.58 itojun return (-1);
982 1.35 itojun case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
983 1.35 itojun ip6stat.ip6s_badoptions++;
984 1.35 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
985 1.58 itojun return (-1);
986 1.35 itojun case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
987 1.35 itojun ip6stat.ip6s_badoptions++;
988 1.35 itojun ip6 = mtod(m, struct ip6_hdr *);
989 1.35 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
990 1.35 itojun (m->m_flags & (M_BCAST|M_MCAST)))
991 1.35 itojun m_freem(m);
992 1.35 itojun else
993 1.35 itojun icmp6_error(m, ICMP6_PARAM_PROB,
994 1.35 itojun ICMP6_PARAMPROB_OPTION, off);
995 1.58 itojun return (-1);
996 1.2 itojun }
997 1.2 itojun
998 1.2 itojun m_freem(m); /* XXX: NOTREACHED */
999 1.58 itojun return (-1);
1000 1.2 itojun }
1001 1.2 itojun
1002 1.2 itojun /*
1003 1.9 itojun * Create the "control" list for this pcb.
1004 1.9 itojun *
1005 1.9 itojun * The routine will be called from upper layer handlers like tcp6_input().
1006 1.9 itojun * Thus the routine assumes that the caller (tcp6_input) have already
1007 1.9 itojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1008 1.9 itojun * very first mbuf on the mbuf chain.
1009 1.9 itojun * We may want to add some infinite loop prevention or sanity checks for safety.
1010 1.9 itojun * (This applies only when you are using KAME mbuf chain restriction, i.e.
1011 1.9 itojun * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1012 1.2 itojun */
1013 1.2 itojun void
1014 1.2 itojun ip6_savecontrol(in6p, mp, ip6, m)
1015 1.35 itojun struct in6pcb *in6p;
1016 1.35 itojun struct mbuf **mp;
1017 1.35 itojun struct ip6_hdr *ip6;
1018 1.35 itojun struct mbuf *m;
1019 1.2 itojun {
1020 1.2 itojun struct proc *p = curproc; /* XXX */
1021 1.9 itojun int privileged;
1022 1.2 itojun
1023 1.9 itojun privileged = 0;
1024 1.9 itojun if (p && !suser(p->p_ucred, &p->p_acflag))
1025 1.9 itojun privileged++;
1026 1.9 itojun
1027 1.9 itojun #ifdef SO_TIMESTAMP
1028 1.2 itojun if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
1029 1.2 itojun struct timeval tv;
1030 1.2 itojun
1031 1.2 itojun microtime(&tv);
1032 1.2 itojun *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1033 1.2 itojun SCM_TIMESTAMP, SOL_SOCKET);
1034 1.2 itojun if (*mp)
1035 1.2 itojun mp = &(*mp)->m_next;
1036 1.2 itojun }
1037 1.9 itojun #endif
1038 1.2 itojun if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
1039 1.2 itojun *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
1040 1.2 itojun sizeof(struct in6_addr), IPV6_RECVDSTADDR,
1041 1.2 itojun IPPROTO_IPV6);
1042 1.2 itojun if (*mp)
1043 1.2 itojun mp = &(*mp)->m_next;
1044 1.2 itojun }
1045 1.2 itojun
1046 1.2 itojun #ifdef noyet
1047 1.2 itojun /* options were tossed above */
1048 1.2 itojun if (in6p->in6p_flags & IN6P_RECVOPTS)
1049 1.2 itojun /* broken */
1050 1.2 itojun /* ip6_srcroute doesn't do what we want here, need to fix */
1051 1.2 itojun if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
1052 1.2 itojun /* broken */
1053 1.2 itojun #endif
1054 1.2 itojun
1055 1.2 itojun /* RFC 2292 sec. 5 */
1056 1.35 itojun if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1057 1.2 itojun struct in6_pktinfo pi6;
1058 1.2 itojun bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1059 1.2 itojun if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1060 1.2 itojun pi6.ipi6_addr.s6_addr16[1] = 0;
1061 1.2 itojun pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1062 1.2 itojun ? m->m_pkthdr.rcvif->if_index
1063 1.2 itojun : 0;
1064 1.2 itojun *mp = sbcreatecontrol((caddr_t) &pi6,
1065 1.2 itojun sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1066 1.2 itojun IPPROTO_IPV6);
1067 1.2 itojun if (*mp)
1068 1.2 itojun mp = &(*mp)->m_next;
1069 1.2 itojun }
1070 1.2 itojun if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1071 1.2 itojun int hlim = ip6->ip6_hlim & 0xff;
1072 1.2 itojun *mp = sbcreatecontrol((caddr_t) &hlim,
1073 1.2 itojun sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1074 1.2 itojun if (*mp)
1075 1.2 itojun mp = &(*mp)->m_next;
1076 1.2 itojun }
1077 1.2 itojun /* IN6P_NEXTHOP - for outgoing packet only */
1078 1.2 itojun
1079 1.2 itojun /*
1080 1.2 itojun * IPV6_HOPOPTS socket option. We require super-user privilege
1081 1.2 itojun * for the option, but it might be too strict, since there might
1082 1.2 itojun * be some hop-by-hop options which can be returned to normal user.
1083 1.2 itojun * See RFC 2292 section 6.
1084 1.2 itojun */
1085 1.35 itojun if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1086 1.2 itojun /*
1087 1.2 itojun * Check if a hop-by-hop options header is contatined in the
1088 1.2 itojun * received packet, and if so, store the options as ancillary
1089 1.2 itojun * data. Note that a hop-by-hop options header must be
1090 1.2 itojun * just after the IPv6 header, which fact is assured through
1091 1.2 itojun * the IPv6 input processing.
1092 1.2 itojun */
1093 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1094 1.2 itojun if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1095 1.9 itojun struct ip6_hbh *hbh;
1096 1.9 itojun int hbhlen;
1097 1.9 itojun
1098 1.9 itojun #ifndef PULLDOWN_TEST
1099 1.9 itojun hbh = (struct ip6_hbh *)(ip6 + 1);
1100 1.9 itojun hbhlen = (hbh->ip6h_len + 1) << 3;
1101 1.9 itojun #else
1102 1.9 itojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1103 1.9 itojun sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1104 1.9 itojun if (hbh == NULL) {
1105 1.9 itojun ip6stat.ip6s_tooshort++;
1106 1.9 itojun return;
1107 1.9 itojun }
1108 1.9 itojun hbhlen = (hbh->ip6h_len + 1) << 3;
1109 1.9 itojun IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1110 1.9 itojun sizeof(struct ip6_hdr), hbhlen);
1111 1.9 itojun if (hbh == NULL) {
1112 1.9 itojun ip6stat.ip6s_tooshort++;
1113 1.9 itojun return;
1114 1.9 itojun }
1115 1.9 itojun #endif
1116 1.2 itojun
1117 1.2 itojun /*
1118 1.2 itojun * XXX: We copy whole the header even if a jumbo
1119 1.2 itojun * payload option is included, which option is to
1120 1.2 itojun * be removed before returning in the RFC 2292.
1121 1.2 itojun * But it's too painful operation...
1122 1.2 itojun */
1123 1.9 itojun *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1124 1.2 itojun IPV6_HOPOPTS, IPPROTO_IPV6);
1125 1.2 itojun if (*mp)
1126 1.2 itojun mp = &(*mp)->m_next;
1127 1.2 itojun }
1128 1.2 itojun }
1129 1.2 itojun
1130 1.2 itojun /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1131 1.2 itojun if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1132 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1133 1.60 simonb int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1134 1.2 itojun
1135 1.2 itojun /*
1136 1.2 itojun * Search for destination options headers or routing
1137 1.2 itojun * header(s) through the header chain, and stores each
1138 1.2 itojun * header as ancillary data.
1139 1.2 itojun * Note that the order of the headers remains in
1140 1.2 itojun * the chain of ancillary data.
1141 1.2 itojun */
1142 1.35 itojun while (1) { /* is explicit loop prevention necessary? */
1143 1.9 itojun struct ip6_ext *ip6e;
1144 1.9 itojun int elen;
1145 1.9 itojun
1146 1.9 itojun #ifndef PULLDOWN_TEST
1147 1.9 itojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1148 1.9 itojun if (nxt == IPPROTO_AH)
1149 1.9 itojun elen = (ip6e->ip6e_len + 2) << 2;
1150 1.9 itojun else
1151 1.9 itojun elen = (ip6e->ip6e_len + 1) << 3;
1152 1.9 itojun #else
1153 1.9 itojun IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
1154 1.9 itojun sizeof(struct ip6_ext));
1155 1.9 itojun if (ip6e == NULL) {
1156 1.9 itojun ip6stat.ip6s_tooshort++;
1157 1.9 itojun return;
1158 1.9 itojun }
1159 1.9 itojun if (nxt == IPPROTO_AH)
1160 1.9 itojun elen = (ip6e->ip6e_len + 2) << 2;
1161 1.9 itojun else
1162 1.9 itojun elen = (ip6e->ip6e_len + 1) << 3;
1163 1.9 itojun IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
1164 1.9 itojun if (ip6e == NULL) {
1165 1.9 itojun ip6stat.ip6s_tooshort++;
1166 1.9 itojun return;
1167 1.9 itojun }
1168 1.9 itojun #endif
1169 1.57 thorpej KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1170 1.2 itojun
1171 1.35 itojun switch (nxt) {
1172 1.35 itojun case IPPROTO_DSTOPTS:
1173 1.35 itojun if (!in6p->in6p_flags & IN6P_DSTOPTS)
1174 1.35 itojun break;
1175 1.35 itojun
1176 1.35 itojun /*
1177 1.35 itojun * We also require super-user privilege for
1178 1.35 itojun * the option.
1179 1.35 itojun * See the comments on IN6_HOPOPTS.
1180 1.35 itojun */
1181 1.35 itojun if (!privileged)
1182 1.35 itojun break;
1183 1.35 itojun
1184 1.35 itojun *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1185 1.35 itojun IPV6_DSTOPTS,
1186 1.35 itojun IPPROTO_IPV6);
1187 1.35 itojun if (*mp)
1188 1.35 itojun mp = &(*mp)->m_next;
1189 1.35 itojun break;
1190 1.35 itojun
1191 1.35 itojun case IPPROTO_ROUTING:
1192 1.35 itojun if (!in6p->in6p_flags & IN6P_RTHDR)
1193 1.35 itojun break;
1194 1.35 itojun
1195 1.35 itojun *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1196 1.35 itojun IPV6_RTHDR,
1197 1.35 itojun IPPROTO_IPV6);
1198 1.35 itojun if (*mp)
1199 1.35 itojun mp = &(*mp)->m_next;
1200 1.35 itojun break;
1201 1.35 itojun
1202 1.35 itojun case IPPROTO_UDP:
1203 1.35 itojun case IPPROTO_TCP:
1204 1.35 itojun case IPPROTO_ICMPV6:
1205 1.35 itojun default:
1206 1.35 itojun /*
1207 1.35 itojun * stop search if we encounter an upper
1208 1.35 itojun * layer protocol headers.
1209 1.35 itojun */
1210 1.35 itojun goto loopend;
1211 1.35 itojun
1212 1.35 itojun case IPPROTO_HOPOPTS:
1213 1.35 itojun case IPPROTO_AH: /* is it possible? */
1214 1.35 itojun break;
1215 1.2 itojun }
1216 1.2 itojun
1217 1.2 itojun /* proceed with the next header. */
1218 1.9 itojun off += elen;
1219 1.2 itojun nxt = ip6e->ip6e_nxt;
1220 1.2 itojun }
1221 1.2 itojun loopend:
1222 1.36 cgd ;
1223 1.2 itojun }
1224 1.9 itojun if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1225 1.2 itojun /* to be done */
1226 1.2 itojun }
1227 1.9 itojun if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1228 1.2 itojun /* to be done */
1229 1.2 itojun }
1230 1.2 itojun /* IN6P_RTHDR - to be done */
1231 1.2 itojun
1232 1.2 itojun }
1233 1.2 itojun
1234 1.2 itojun /*
1235 1.2 itojun * Get pointer to the previous header followed by the header
1236 1.2 itojun * currently processed.
1237 1.2 itojun * XXX: This function supposes that
1238 1.2 itojun * M includes all headers,
1239 1.2 itojun * the next header field and the header length field of each header
1240 1.2 itojun * are valid, and
1241 1.2 itojun * the sum of each header length equals to OFF.
1242 1.2 itojun * Because of these assumptions, this function must be called very
1243 1.2 itojun * carefully. Moreover, it will not be used in the near future when
1244 1.2 itojun * we develop `neater' mechanism to process extension headers.
1245 1.2 itojun */
1246 1.58 itojun u_int8_t *
1247 1.2 itojun ip6_get_prevhdr(m, off)
1248 1.2 itojun struct mbuf *m;
1249 1.2 itojun int off;
1250 1.2 itojun {
1251 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1252 1.2 itojun
1253 1.2 itojun if (off == sizeof(struct ip6_hdr))
1254 1.58 itojun return (&ip6->ip6_nxt);
1255 1.2 itojun else {
1256 1.2 itojun int len, nxt;
1257 1.2 itojun struct ip6_ext *ip6e = NULL;
1258 1.2 itojun
1259 1.2 itojun nxt = ip6->ip6_nxt;
1260 1.2 itojun len = sizeof(struct ip6_hdr);
1261 1.2 itojun while (len < off) {
1262 1.2 itojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1263 1.2 itojun
1264 1.35 itojun switch (nxt) {
1265 1.2 itojun case IPPROTO_FRAGMENT:
1266 1.2 itojun len += sizeof(struct ip6_frag);
1267 1.2 itojun break;
1268 1.2 itojun case IPPROTO_AH:
1269 1.2 itojun len += (ip6e->ip6e_len + 2) << 2;
1270 1.2 itojun break;
1271 1.2 itojun default:
1272 1.2 itojun len += (ip6e->ip6e_len + 1) << 3;
1273 1.2 itojun break;
1274 1.2 itojun }
1275 1.2 itojun nxt = ip6e->ip6e_nxt;
1276 1.2 itojun }
1277 1.2 itojun if (ip6e)
1278 1.58 itojun return (&ip6e->ip6e_nxt);
1279 1.2 itojun else
1280 1.2 itojun return NULL;
1281 1.18 itojun }
1282 1.18 itojun }
1283 1.18 itojun
1284 1.18 itojun /*
1285 1.18 itojun * get next header offset. m will be retained.
1286 1.18 itojun */
1287 1.18 itojun int
1288 1.18 itojun ip6_nexthdr(m, off, proto, nxtp)
1289 1.18 itojun struct mbuf *m;
1290 1.18 itojun int off;
1291 1.18 itojun int proto;
1292 1.18 itojun int *nxtp;
1293 1.18 itojun {
1294 1.18 itojun struct ip6_hdr ip6;
1295 1.18 itojun struct ip6_ext ip6e;
1296 1.18 itojun struct ip6_frag fh;
1297 1.18 itojun
1298 1.18 itojun /* just in case */
1299 1.18 itojun if (m == NULL)
1300 1.18 itojun panic("ip6_nexthdr: m == NULL");
1301 1.18 itojun if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1302 1.18 itojun return -1;
1303 1.18 itojun
1304 1.18 itojun switch (proto) {
1305 1.18 itojun case IPPROTO_IPV6:
1306 1.18 itojun if (m->m_pkthdr.len < off + sizeof(ip6))
1307 1.18 itojun return -1;
1308 1.18 itojun m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1309 1.18 itojun if (nxtp)
1310 1.18 itojun *nxtp = ip6.ip6_nxt;
1311 1.18 itojun off += sizeof(ip6);
1312 1.18 itojun return off;
1313 1.18 itojun
1314 1.18 itojun case IPPROTO_FRAGMENT:
1315 1.18 itojun /*
1316 1.18 itojun * terminate parsing if it is not the first fragment,
1317 1.18 itojun * it does not make sense to parse through it.
1318 1.18 itojun */
1319 1.18 itojun if (m->m_pkthdr.len < off + sizeof(fh))
1320 1.18 itojun return -1;
1321 1.18 itojun m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1322 1.18 itojun if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1323 1.18 itojun return -1;
1324 1.18 itojun if (nxtp)
1325 1.18 itojun *nxtp = fh.ip6f_nxt;
1326 1.18 itojun off += sizeof(struct ip6_frag);
1327 1.18 itojun return off;
1328 1.18 itojun
1329 1.18 itojun case IPPROTO_AH:
1330 1.18 itojun if (m->m_pkthdr.len < off + sizeof(ip6e))
1331 1.18 itojun return -1;
1332 1.18 itojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1333 1.18 itojun if (nxtp)
1334 1.18 itojun *nxtp = ip6e.ip6e_nxt;
1335 1.18 itojun off += (ip6e.ip6e_len + 2) << 2;
1336 1.47 itojun if (m->m_pkthdr.len < off)
1337 1.47 itojun return -1;
1338 1.18 itojun return off;
1339 1.18 itojun
1340 1.18 itojun case IPPROTO_HOPOPTS:
1341 1.18 itojun case IPPROTO_ROUTING:
1342 1.18 itojun case IPPROTO_DSTOPTS:
1343 1.18 itojun if (m->m_pkthdr.len < off + sizeof(ip6e))
1344 1.18 itojun return -1;
1345 1.18 itojun m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1346 1.18 itojun if (nxtp)
1347 1.18 itojun *nxtp = ip6e.ip6e_nxt;
1348 1.18 itojun off += (ip6e.ip6e_len + 1) << 3;
1349 1.47 itojun if (m->m_pkthdr.len < off)
1350 1.47 itojun return -1;
1351 1.18 itojun return off;
1352 1.18 itojun
1353 1.18 itojun case IPPROTO_NONE:
1354 1.18 itojun case IPPROTO_ESP:
1355 1.18 itojun case IPPROTO_IPCOMP:
1356 1.18 itojun /* give up */
1357 1.18 itojun return -1;
1358 1.18 itojun
1359 1.18 itojun default:
1360 1.18 itojun return -1;
1361 1.18 itojun }
1362 1.18 itojun }
1363 1.18 itojun
1364 1.18 itojun /*
1365 1.18 itojun * get offset for the last header in the chain. m will be kept untainted.
1366 1.18 itojun */
1367 1.18 itojun int
1368 1.18 itojun ip6_lasthdr(m, off, proto, nxtp)
1369 1.18 itojun struct mbuf *m;
1370 1.18 itojun int off;
1371 1.18 itojun int proto;
1372 1.18 itojun int *nxtp;
1373 1.18 itojun {
1374 1.18 itojun int newoff;
1375 1.18 itojun int nxt;
1376 1.18 itojun
1377 1.18 itojun if (!nxtp) {
1378 1.18 itojun nxt = -1;
1379 1.18 itojun nxtp = &nxt;
1380 1.18 itojun }
1381 1.18 itojun while (1) {
1382 1.18 itojun newoff = ip6_nexthdr(m, off, proto, nxtp);
1383 1.18 itojun if (newoff < 0)
1384 1.18 itojun return off;
1385 1.18 itojun else if (newoff < off)
1386 1.18 itojun return -1; /* invalid */
1387 1.18 itojun else if (newoff == off)
1388 1.18 itojun return newoff;
1389 1.18 itojun
1390 1.18 itojun off = newoff;
1391 1.18 itojun proto = *nxtp;
1392 1.2 itojun }
1393 1.2 itojun }
1394 1.2 itojun
1395 1.2 itojun /*
1396 1.2 itojun * System control for IP6
1397 1.2 itojun */
1398 1.2 itojun
1399 1.2 itojun u_char inet6ctlerrmap[PRC_NCMDS] = {
1400 1.2 itojun 0, 0, 0, 0,
1401 1.2 itojun 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1402 1.2 itojun EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1403 1.2 itojun EMSGSIZE, EHOSTUNREACH, 0, 0,
1404 1.2 itojun 0, 0, 0, 0,
1405 1.2 itojun ENOPROTOOPT
1406 1.2 itojun };
1407 1.2 itojun
1408 1.2 itojun int
1409 1.2 itojun ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1410 1.2 itojun int *name;
1411 1.2 itojun u_int namelen;
1412 1.2 itojun void *oldp;
1413 1.2 itojun size_t *oldlenp;
1414 1.2 itojun void *newp;
1415 1.2 itojun size_t newlen;
1416 1.2 itojun {
1417 1.26 itojun int old, error;
1418 1.26 itojun
1419 1.2 itojun /* All sysctl names at this level are terminal. */
1420 1.2 itojun if (namelen != 1)
1421 1.2 itojun return ENOTDIR;
1422 1.2 itojun
1423 1.2 itojun switch (name[0]) {
1424 1.2 itojun
1425 1.2 itojun case IPV6CTL_FORWARDING:
1426 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1427 1.2 itojun &ip6_forwarding);
1428 1.2 itojun case IPV6CTL_SENDREDIRECTS:
1429 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1430 1.2 itojun &ip6_sendredirects);
1431 1.2 itojun case IPV6CTL_DEFHLIM:
1432 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
1433 1.2 itojun case IPV6CTL_MAXFRAGPACKETS:
1434 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1435 1.2 itojun &ip6_maxfragpackets);
1436 1.2 itojun case IPV6CTL_ACCEPT_RTADV:
1437 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1438 1.2 itojun &ip6_accept_rtadv);
1439 1.2 itojun case IPV6CTL_KEEPFAITH:
1440 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
1441 1.2 itojun case IPV6CTL_LOG_INTERVAL:
1442 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1443 1.2 itojun &ip6_log_interval);
1444 1.2 itojun case IPV6CTL_HDRNESTLIMIT:
1445 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1446 1.2 itojun &ip6_hdrnestlimit);
1447 1.2 itojun case IPV6CTL_DAD_COUNT:
1448 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
1449 1.2 itojun case IPV6CTL_AUTO_FLOWLABEL:
1450 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1451 1.2 itojun &ip6_auto_flowlabel);
1452 1.2 itojun case IPV6CTL_DEFMCASTHLIM:
1453 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1454 1.2 itojun &ip6_defmcasthlim);
1455 1.51 itojun #if NGIF > 0
1456 1.2 itojun case IPV6CTL_GIF_HLIM:
1457 1.2 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1458 1.2 itojun &ip6_gif_hlim);
1459 1.51 itojun #endif
1460 1.2 itojun case IPV6CTL_KAME_VERSION:
1461 1.2 itojun return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1462 1.9 itojun case IPV6CTL_USE_DEPRECATED:
1463 1.9 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1464 1.9 itojun &ip6_use_deprecated);
1465 1.10 itojun case IPV6CTL_RR_PRUNE:
1466 1.10 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1467 1.43 itojun case IPV6CTL_V6ONLY:
1468 1.43 itojun #ifdef INET6_BINDV6ONLY
1469 1.43 itojun return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
1470 1.43 itojun #else
1471 1.43 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_v6only);
1472 1.26 itojun #endif
1473 1.26 itojun case IPV6CTL_ANONPORTMIN:
1474 1.26 itojun old = ip6_anonportmin;
1475 1.26 itojun error = sysctl_int(oldp, oldlenp, newp, newlen,
1476 1.26 itojun &ip6_anonportmin);
1477 1.26 itojun if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
1478 1.26 itojun ip6_anonportmin > 65535
1479 1.26 itojun #ifndef IPNOPRIVPORTS
1480 1.26 itojun || ip6_anonportmin < IPV6PORT_RESERVED
1481 1.26 itojun #endif
1482 1.26 itojun ) {
1483 1.26 itojun ip6_anonportmin = old;
1484 1.26 itojun return (EINVAL);
1485 1.26 itojun }
1486 1.26 itojun return (error);
1487 1.26 itojun case IPV6CTL_ANONPORTMAX:
1488 1.26 itojun old = ip6_anonportmax;
1489 1.26 itojun error = sysctl_int(oldp, oldlenp, newp, newlen,
1490 1.26 itojun &ip6_anonportmax);
1491 1.26 itojun if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
1492 1.26 itojun ip6_anonportmax > 65535
1493 1.26 itojun #ifndef IPNOPRIVPORTS
1494 1.26 itojun || ip6_anonportmax < IPV6PORT_RESERVED
1495 1.26 itojun #endif
1496 1.26 itojun ) {
1497 1.26 itojun ip6_anonportmax = old;
1498 1.26 itojun return (EINVAL);
1499 1.26 itojun }
1500 1.26 itojun return (error);
1501 1.26 itojun #ifndef IPNOPRIVPORTS
1502 1.26 itojun case IPV6CTL_LOWPORTMIN:
1503 1.26 itojun old = ip6_lowportmin;
1504 1.26 itojun error = sysctl_int(oldp, oldlenp, newp, newlen,
1505 1.26 itojun &ip6_lowportmin);
1506 1.26 itojun if (ip6_lowportmin >= ip6_lowportmax ||
1507 1.26 itojun ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
1508 1.26 itojun ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
1509 1.26 itojun ip6_lowportmin = old;
1510 1.26 itojun return (EINVAL);
1511 1.26 itojun }
1512 1.26 itojun return (error);
1513 1.26 itojun case IPV6CTL_LOWPORTMAX:
1514 1.26 itojun old = ip6_lowportmax;
1515 1.26 itojun error = sysctl_int(oldp, oldlenp, newp, newlen,
1516 1.26 itojun &ip6_lowportmax);
1517 1.26 itojun if (ip6_lowportmin >= ip6_lowportmax ||
1518 1.26 itojun ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
1519 1.26 itojun ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
1520 1.26 itojun ip6_lowportmax = old;
1521 1.26 itojun return (EINVAL);
1522 1.26 itojun }
1523 1.26 itojun return (error);
1524 1.11 itojun #endif
1525 1.53 itojun case IPV6CTL_MAXFRAGS:
1526 1.53 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_maxfrags);
1527 1.2 itojun default:
1528 1.2 itojun return EOPNOTSUPP;
1529 1.2 itojun }
1530 1.2 itojun /* NOTREACHED */
1531 1.2 itojun }
1532