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