xref: /openbsd-src/sys/netinet6/ip6_input.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ip6_input.c,v 1.168 2016/08/24 09:41:12 mpi 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 #include "carp.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/mbuf.h>
70 #include <sys/domain.h>
71 #include <sys/sysctl.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/timeout.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/task.h>
81 
82 #include <net/if.h>
83 #include <net/if_var.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/netisr.h>
87 
88 #include <netinet/in.h>
89 
90 #include <netinet/ip.h>
91 
92 #include <netinet/in_pcb.h>
93 #include <netinet/ip_var.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/nd6.h>
99 
100 #include <netinet6/ip6protosw.h>
101 
102 #include "gif.h"
103 #include "bpfilter.h"
104 
105 #ifdef MROUTING
106 #include <netinet6/ip6_mroute.h>
107 #endif
108 
109 #if NPF > 0
110 #include <net/pfvar.h>
111 #endif
112 
113 #if NCARP > 0
114 #include <netinet/ip_carp.h>
115 #endif
116 
117 struct in6_ifaddrhead in6_ifaddr;
118 struct niqueue ip6intrq = NIQUEUE_INITIALIZER(IFQ_MAXLEN, NETISR_IPV6);
119 
120 struct ip6stat ip6stat;
121 
122 void ip6_init2(void *);
123 int ip6_check_rh0hdr(struct mbuf *, int *);
124 
125 int ip6_hbhchcheck(struct mbuf *, int *, int *, int *);
126 int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
127 struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
128 
129 static struct mbuf_queue	ip6send_mq;
130 
131 static void ip6_send_dispatch(void *);
132 static struct task ip6send_task =
133 	TASK_INITIALIZER(ip6_send_dispatch, &ip6send_mq);
134 
135 /*
136  * IP6 initialization: fill in IP6 protocol switch table.
137  * All protocols not implemented in kernel go to raw IP6 protocol handler.
138  */
139 void
140 ip6_init(void)
141 {
142 	struct ip6protosw *pr;
143 	int i;
144 
145 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
146 	if (pr == NULL)
147 		panic("ip6_init");
148 	for (i = 0; i < IPPROTO_MAX; i++)
149 		ip6_protox[i] = pr - inet6sw;
150 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
151 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
152 		if (pr->pr_domain->dom_family == PF_INET6 &&
153 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW &&
154 		    pr->pr_protocol < IPPROTO_MAX)
155 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
156 	TAILQ_INIT(&in6_ifaddr);
157 	ip6_randomid_init();
158 	nd6_init();
159 	frag6_init();
160 	ip6_init2(NULL);
161 
162 	mq_init(&ip6send_mq, 64, IPL_SOFTNET);
163 }
164 
165 void
166 ip6_init2(void *dummy)
167 {
168 
169 	/* nd6_timer_init */
170 	bzero(&nd6_timer_ch, sizeof(nd6_timer_ch));
171 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
172 	timeout_add_sec(&nd6_timer_ch, 1);
173 }
174 
175 /*
176  * IP6 input interrupt handling. Just pass the packet to ip6_input.
177  */
178 void
179 ip6intr(void)
180 {
181 	struct mbuf *m;
182 
183 	while ((m = niq_dequeue(&ip6intrq)) != NULL)
184 		ip6_input(m);
185 }
186 
187 void
188 ip6_input(struct mbuf *m)
189 {
190 	struct ifnet *ifp;
191 	struct ip6_hdr *ip6;
192 	struct sockaddr_in6 sin6;
193 	struct rtentry *rt = NULL;
194 	int off, nest;
195 	u_int16_t src_scope, dst_scope;
196 	int nxt, ours = 0;
197 #if NPF > 0
198 	struct in6_addr odst;
199 #endif
200 	int srcrt = 0;
201 
202 	ifp = if_get(m->m_pkthdr.ph_ifidx);
203 	if (ifp == NULL)
204 		goto bad;
205 
206 	if (m->m_flags & M_EXT) {
207 		if (m->m_next)
208 			ip6stat.ip6s_mext2m++;
209 		else
210 			ip6stat.ip6s_mext1++;
211 	} else {
212 		if (m->m_next) {
213 			if (m->m_flags & M_LOOP) {
214 				ip6stat.ip6s_m2m[lo0ifidx]++;	/*XXX*/
215 			} else if (ifp->if_index < nitems(ip6stat.ip6s_m2m))
216 				ip6stat.ip6s_m2m[ifp->if_index]++;
217 			else
218 				ip6stat.ip6s_m2m[0]++;
219 		} else
220 			ip6stat.ip6s_m1++;
221 	}
222 
223 	ip6stat.ip6s_total++;
224 
225 	if (m->m_len < sizeof(struct ip6_hdr)) {
226 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
227 			ip6stat.ip6s_toosmall++;
228 			if_put(ifp);
229 			return;
230 		}
231 	}
232 
233 	ip6 = mtod(m, struct ip6_hdr *);
234 
235 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
236 		ip6stat.ip6s_badvers++;
237 		goto bad;
238 	}
239 
240 #if NCARP > 0
241 	if (ifp->if_type == IFT_CARP && ip6->ip6_nxt != IPPROTO_ICMPV6 &&
242 	    carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
243 	    ip6->ip6_dst.s6_addr32))
244 		goto bad;
245 #endif
246 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
247 
248 	/*
249 	 * Check against address spoofing/corruption.
250 	 */
251 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
252 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
253 		/*
254 		 * XXX: "badscope" is not very suitable for a multicast source.
255 		 */
256 		ip6stat.ip6s_badscope++;
257 		goto bad;
258 	}
259 	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
260 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
261 	    (ifp->if_flags & IFF_LOOPBACK) == 0) {
262 		    ip6stat.ip6s_badscope++;
263 		    goto bad;
264 	}
265 	/* Drop packets if interface ID portion is already filled. */
266 	if (((IN6_IS_SCOPE_EMBED(&ip6->ip6_src) && ip6->ip6_src.s6_addr16[1]) ||
267 	    (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) && ip6->ip6_dst.s6_addr16[1])) &&
268 	    (ifp->if_flags & IFF_LOOPBACK) == 0) {
269 		ip6stat.ip6s_badscope++;
270 		goto bad;
271 	}
272 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
273 	    !(m->m_flags & M_LOOP)) {
274 		/*
275 		 * In this case, the packet should come from the loopback
276 		 * interface.  However, we cannot just check the if_flags,
277 		 * because ip6_mloopback() passes the "actual" interface
278 		 * as the outgoing/incoming interface.
279 		 */
280 		ip6stat.ip6s_badscope++;
281 		goto bad;
282 	}
283 
284 	/*
285 	 * The following check is not documented in specs.  A malicious
286 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
287 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
288 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
289 	 *
290 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
291 	 * support IPv4-less kernel compilation, we cannot support SIIT
292 	 * environment at all.  So, it makes more sense for us to reject any
293 	 * malicious packets for non-SIIT environment, than try to do a
294 	 * partial support for SIIT environment.
295 	 */
296 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
297 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
298 		ip6stat.ip6s_badscope++;
299 		goto bad;
300 	}
301 
302 	/*
303 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
304 	 *
305 	 * The code forbids automatic tunneling as per RFC4213.
306 	 */
307 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
308 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
309 		ip6stat.ip6s_badscope++;
310 		goto bad;
311 	}
312 
313 	/*
314 	 * If the packet has been received on a loopback interface it
315 	 * can be destinated to any local address, not necessarily to
316 	 * an address configured on `ifp'.
317 	 */
318 	if (ifp->if_flags & IFF_LOOPBACK) {
319 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
320 			src_scope = ip6->ip6_src.s6_addr16[1];
321 			ip6->ip6_src.s6_addr16[1] = 0;
322 		}
323 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
324 			dst_scope = ip6->ip6_dst.s6_addr16[1];
325 			ip6->ip6_dst.s6_addr16[1] = 0;
326 		}
327 	}
328 
329 #if NPF > 0
330         /*
331          * Packet filter
332          */
333 	odst = ip6->ip6_dst;
334 	if (pf_test(AF_INET6, PF_IN, ifp, &m) != PF_PASS)
335 		goto bad;
336 	if (m == NULL)
337 		goto bad;
338 
339 	ip6 = mtod(m, struct ip6_hdr *);
340 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
341 #endif
342 
343 	/*
344 	 * Without embedded scope ID we cannot find link-local
345 	 * addresses in the routing table.
346 	 */
347 	if (ifp->if_flags & IFF_LOOPBACK) {
348 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
349 			ip6->ip6_src.s6_addr16[1] = src_scope;
350 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
351 			ip6->ip6_dst.s6_addr16[1] = dst_scope;
352 	} else {
353 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
354 			ip6->ip6_src.s6_addr16[1] = htons(ifp->if_index);
355 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
356 			ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
357 	}
358 
359 	/*
360 	 * Be more secure than RFC5095 and scan for type 0 routing headers.
361 	 * If pf has already scanned the header chain, do not do it twice.
362 	 */
363 	if (!(m->m_pkthdr.pf.flags & PF_TAG_PROCESSED) &&
364 	    ip6_check_rh0hdr(m, &off)) {
365 		ip6stat.ip6s_badoptions++;
366 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, off);
367 		/* m is already freed */
368 		if_put(ifp);
369 		return;
370 	}
371 
372 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
373 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
374 		ours = 1;
375 		goto hbhcheck;
376 	}
377 
378 #if NPF > 0
379 	if (pf_ouraddr(m) == 1) {
380 		ours = 1;
381 		goto hbhcheck;
382 	}
383 #endif
384 
385 	/*
386 	 * Multicast check
387 	 */
388 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
389 
390 		/*
391 		 * Make sure M_MCAST is set.  It should theoretically
392 		 * already be there, but let's play safe because upper
393 		 * layers check for this flag.
394 		 */
395 		m->m_flags |= M_MCAST;
396 
397 		/*
398 		 * See if we belong to the destination multicast group on the
399 		 * arrival interface.
400 		 */
401 		if (in6_hasmulti(&ip6->ip6_dst, ifp))
402 			ours = 1;
403 #ifdef MROUTING
404 		else if (!ip6_mforwarding || !ip6_mrouter)
405 #else
406 		else
407 #endif
408 		{
409 			ip6stat.ip6s_notmember++;
410 			if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
411 				ip6stat.ip6s_cantforward++;
412 			goto bad;
413 		}
414 		goto hbhcheck;
415 	}
416 
417 
418 	/*
419 	 *  Unicast check
420 	 */
421 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
422 	sin6.sin6_len = sizeof(struct sockaddr_in6);
423 	sin6.sin6_family = AF_INET6;
424 	sin6.sin6_addr = ip6->ip6_dst;
425 	rt = rtalloc_mpath(sin6tosa(&sin6), &ip6->ip6_src.s6_addr32[0],
426 	    m->m_pkthdr.ph_rtableid);
427 
428 	/*
429 	 * Accept the packet if the route to the destination is marked
430 	 * as local.
431 	 */
432 	if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_LOCAL)) {
433 		struct in6_ifaddr *ia6 = ifatoia6(rt->rt_ifa);
434 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
435 			m->m_flags |= M_ACAST;
436 		/*
437 		 * packets to a tentative, duplicated, or somehow invalid
438 		 * address must not be accepted.
439 		 */
440 		if ((ia6->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED))) {
441 			char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
442 
443 			inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src));
444 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst));
445 			/* address is not ready, so discard the packet. */
446 			nd6log((LOG_INFO,
447 			    "ip6_input: packet to an unready address %s->%s\n",
448 			    src, dst));
449 
450 			goto bad;
451 		} else {
452 			/* this address is ready */
453 			ours = 1;
454 			goto hbhcheck;
455 		}
456 	}
457 
458 #if NCARP > 0
459 	if (ifp->if_type == IFT_CARP && ip6->ip6_nxt == IPPROTO_ICMPV6 &&
460 	    carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
461 	    ip6->ip6_dst.s6_addr32))
462 		goto bad;
463 #endif
464 	/*
465 	 * Now there is no reason to process the packet if it's not our own
466 	 * and we're not a router.
467 	 */
468 	if (!ip6_forwarding) {
469 		ip6stat.ip6s_cantforward++;
470 		goto bad;
471 	}
472 
473   hbhcheck:
474 
475 	if (ip6_hbhchcheck(m, &off, &nxt, &ours)) {
476 		rtfree(rt);
477 		if_put(ifp);
478 		return;	/* m have already been freed */
479 	}
480 
481 	/* adjust pointer */
482 	ip6 = mtod(m, struct ip6_hdr *);
483 
484 	/*
485 	 * Forward if desirable.
486 	 */
487 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
488 		/*
489 		 * If we are acting as a multicast router, all
490 		 * incoming multicast packets are passed to the
491 		 * kernel-level multicast forwarding function.
492 		 * The packet is returned (relatively) intact; if
493 		 * ip6_mforward() returns a non-zero value, the packet
494 		 * must be discarded, else it may be accepted below.
495 		 */
496 #ifdef MROUTING
497 		if (ip6_mforwarding && ip6_mrouter &&
498 		    ip6_mforward(ip6, ifp, m)) {
499 			ip6stat.ip6s_cantforward++;
500 			goto bad;
501 		}
502 #endif
503 		if (!ours)
504 			goto bad;
505 	} else if (!ours) {
506 		ip6_forward(m, rt, srcrt);
507 		if_put(ifp);
508 		return;
509 	}
510 
511 	/* pf might have changed things */
512 	in6_proto_cksum_out(m, NULL);
513 
514 	ip6 = mtod(m, struct ip6_hdr *);
515 
516 	/*
517 	 * Tell launch routine the next header
518 	 */
519 	ip6stat.ip6s_delivered++;
520 	nest = 0;
521 
522 	while (nxt != IPPROTO_DONE) {
523 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
524 			ip6stat.ip6s_toomanyhdr++;
525 			goto bad;
526 		}
527 
528 		/*
529 		 * protection against faulty packet - there should be
530 		 * more sanity checks in header chain processing.
531 		 */
532 		if (m->m_pkthdr.len < off) {
533 			ip6stat.ip6s_tooshort++;
534 			goto bad;
535 		}
536 
537 		/* draft-itojun-ipv6-tcp-to-anycast */
538 		if (ISSET(m->m_flags, M_ACAST) && (nxt == IPPROTO_TCP)) {
539 			if (m->m_len >= sizeof(struct ip6_hdr)) {
540 				icmp6_error(m, ICMP6_DST_UNREACH,
541 					ICMP6_DST_UNREACH_ADDR,
542 					offsetof(struct ip6_hdr, ip6_dst));
543 				break;
544 			} else
545 				goto bad;
546 		}
547 
548 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
549 	}
550 	rtfree(rt);
551 	if_put(ifp);
552 	return;
553  bad:
554 	rtfree(rt);
555 	if_put(ifp);
556 	m_freem(m);
557 }
558 
559 int
560 ip6_hbhchcheck(struct mbuf *m, int *offp, int *nxtp, int *oursp)
561 {
562 	struct ip6_hdr *ip6;
563 	u_int32_t plen, rtalert = ~0;
564 
565 	ip6 = mtod(m, struct ip6_hdr *);
566 
567 	/*
568 	 * Process Hop-by-Hop options header if it's contained.
569 	 * m may be modified in ip6_hopopts_input().
570 	 * If a JumboPayload option is included, plen will also be modified.
571 	 */
572 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
573 	*offp = sizeof(struct ip6_hdr);
574 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
575 		struct ip6_hbh *hbh;
576 
577 		if (ip6_hopopts_input(&plen, &rtalert, &m, offp)) {
578 			return (-1);	/* m have already been freed */
579 		}
580 
581 		/* adjust pointer */
582 		ip6 = mtod(m, struct ip6_hdr *);
583 
584 		/*
585 		 * if the payload length field is 0 and the next header field
586 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
587 		 * option MUST be included.
588 		 */
589 		if (ip6->ip6_plen == 0 && plen == 0) {
590 			/*
591 			 * Note that if a valid jumbo payload option is
592 			 * contained, ip6_hopopts_input() must set a valid
593 			 * (non-zero) payload length to the variable plen.
594 			 */
595 			ip6stat.ip6s_badoptions++;
596 			icmp6_error(m, ICMP6_PARAM_PROB,
597 				    ICMP6_PARAMPROB_HEADER,
598 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
599 			return (-1);
600 		}
601 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
602 			sizeof(struct ip6_hbh));
603 		if (hbh == NULL) {
604 			ip6stat.ip6s_tooshort++;
605 			return (-1);
606 		}
607 		*nxtp = hbh->ip6h_nxt;
608 
609 		/*
610 		 * accept the packet if a router alert option is included
611 		 * and we act as an IPv6 router.
612 		 */
613 		if (rtalert != ~0 && ip6_forwarding)
614 			*oursp = 1;
615 	} else
616 		*nxtp = ip6->ip6_nxt;
617 
618 	/*
619 	 * Check that the amount of data in the buffers
620 	 * is as at least much as the IPv6 header would have us expect.
621 	 * Trim mbufs if longer than we expect.
622 	 * Drop packet if shorter than we expect.
623 	 */
624 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
625 		ip6stat.ip6s_tooshort++;
626 		m_freem(m);
627 		return (-1);
628 	}
629 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
630 		if (m->m_len == m->m_pkthdr.len) {
631 			m->m_len = sizeof(struct ip6_hdr) + plen;
632 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
633 		} else {
634 			m_adj(m,
635 			    sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
636 		}
637 	}
638 
639 	return (0);
640 }
641 
642 /* scan packet for RH0 routing header. Mostly stolen from pf.c:pf_test() */
643 int
644 ip6_check_rh0hdr(struct mbuf *m, int *offp)
645 {
646 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
647 	struct ip6_rthdr rthdr;
648 	struct ip6_ext opt6;
649 	u_int8_t proto = ip6->ip6_nxt;
650 	int done = 0, lim, off, rh_cnt = 0;
651 
652 	off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
653 	lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6));
654 	do {
655 		switch (proto) {
656 		case IPPROTO_ROUTING:
657 			*offp = off;
658 			if (rh_cnt++) {
659 				/* more then one rh header present */
660 				return (1);
661 			}
662 
663 			if (off + sizeof(rthdr) > lim) {
664 				/* packet to short to make sense */
665 				return (1);
666 			}
667 
668 			m_copydata(m, off, sizeof(rthdr), (caddr_t)&rthdr);
669 
670 			if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
671 				*offp += offsetof(struct ip6_rthdr, ip6r_type);
672 				return (1);
673 			}
674 
675 			off += (rthdr.ip6r_len + 1) * 8;
676 			proto = rthdr.ip6r_nxt;
677 			break;
678 		case IPPROTO_AH:
679 		case IPPROTO_HOPOPTS:
680 		case IPPROTO_DSTOPTS:
681 			/* get next header and header length */
682 			if (off + sizeof(opt6) > lim) {
683 				/*
684 				 * Packet to short to make sense, we could
685 				 * reject the packet but as a router we
686 				 * should not do that so forward it.
687 				 */
688 				return (0);
689 			}
690 
691 			m_copydata(m, off, sizeof(opt6), (caddr_t)&opt6);
692 
693 			if (proto == IPPROTO_AH)
694 				off += (opt6.ip6e_len + 2) * 4;
695 			else
696 				off += (opt6.ip6e_len + 1) * 8;
697 			proto = opt6.ip6e_nxt;
698 			break;
699 		case IPPROTO_FRAGMENT:
700 		default:
701 			/* end of header stack */
702 			done = 1;
703 			break;
704 		}
705 	} while (!done);
706 
707 	return (0);
708 }
709 
710 /*
711  * Hop-by-Hop options header processing. If a valid jumbo payload option is
712  * included, the real payload length will be stored in plenp.
713  *
714  * rtalertp - XXX: should be stored in a more smart way
715  */
716 int
717 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, struct mbuf **mp,
718     int *offp)
719 {
720 	struct mbuf *m = *mp;
721 	int off = *offp, hbhlen;
722 	struct ip6_hbh *hbh;
723 
724 	/* validation of the length of the header */
725 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
726 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
727 	if (hbh == NULL) {
728 		ip6stat.ip6s_tooshort++;
729 		return -1;
730 	}
731 	hbhlen = (hbh->ip6h_len + 1) << 3;
732 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
733 		hbhlen);
734 	if (hbh == NULL) {
735 		ip6stat.ip6s_tooshort++;
736 		return -1;
737 	}
738 	off += hbhlen;
739 	hbhlen -= sizeof(struct ip6_hbh);
740 
741 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
742 				hbhlen, rtalertp, plenp) < 0)
743 		return (-1);
744 
745 	*offp = off;
746 	*mp = m;
747 	return (0);
748 }
749 
750 /*
751  * Search header for all Hop-by-hop options and process each option.
752  * This function is separate from ip6_hopopts_input() in order to
753  * handle a case where the sending node itself process its hop-by-hop
754  * options header. In such a case, the function is called from ip6_output().
755  *
756  * The function assumes that hbh header is located right after the IPv6 header
757  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
758  * opthead + hbhlen is located in continuous memory region.
759  */
760 int
761 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
762     u_int32_t *rtalertp, u_int32_t *plenp)
763 {
764 	struct ip6_hdr *ip6;
765 	int optlen = 0;
766 	u_int8_t *opt = opthead;
767 	u_int16_t rtalert_val;
768 	u_int32_t jumboplen;
769 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
770 
771 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
772 		switch (*opt) {
773 		case IP6OPT_PAD1:
774 			optlen = 1;
775 			break;
776 		case IP6OPT_PADN:
777 			if (hbhlen < IP6OPT_MINLEN) {
778 				ip6stat.ip6s_toosmall++;
779 				goto bad;
780 			}
781 			optlen = *(opt + 1) + 2;
782 			break;
783 		case IP6OPT_ROUTER_ALERT:
784 			/* XXX may need check for alignment */
785 			if (hbhlen < IP6OPT_RTALERT_LEN) {
786 				ip6stat.ip6s_toosmall++;
787 				goto bad;
788 			}
789 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
790 				/* XXX stat */
791 				icmp6_error(m, ICMP6_PARAM_PROB,
792 				    ICMP6_PARAMPROB_HEADER,
793 				    erroff + opt + 1 - opthead);
794 				return (-1);
795 			}
796 			optlen = IP6OPT_RTALERT_LEN;
797 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
798 			*rtalertp = ntohs(rtalert_val);
799 			break;
800 		case IP6OPT_JUMBO:
801 			/* XXX may need check for alignment */
802 			if (hbhlen < IP6OPT_JUMBO_LEN) {
803 				ip6stat.ip6s_toosmall++;
804 				goto bad;
805 			}
806 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
807 				/* XXX stat */
808 				icmp6_error(m, ICMP6_PARAM_PROB,
809 				    ICMP6_PARAMPROB_HEADER,
810 				    erroff + opt + 1 - opthead);
811 				return (-1);
812 			}
813 			optlen = IP6OPT_JUMBO_LEN;
814 
815 			/*
816 			 * IPv6 packets that have non 0 payload length
817 			 * must not contain a jumbo payload option.
818 			 */
819 			ip6 = mtod(m, struct ip6_hdr *);
820 			if (ip6->ip6_plen) {
821 				ip6stat.ip6s_badoptions++;
822 				icmp6_error(m, ICMP6_PARAM_PROB,
823 				    ICMP6_PARAMPROB_HEADER,
824 				    erroff + opt - opthead);
825 				return (-1);
826 			}
827 
828 			/*
829 			 * We may see jumbolen in unaligned location, so
830 			 * we'd need to perform bcopy().
831 			 */
832 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
833 			jumboplen = (u_int32_t)htonl(jumboplen);
834 
835 #if 1
836 			/*
837 			 * if there are multiple jumbo payload options,
838 			 * *plenp will be non-zero and the packet will be
839 			 * rejected.
840 			 * the behavior may need some debate in ipngwg -
841 			 * multiple options does not make sense, however,
842 			 * there's no explicit mention in specification.
843 			 */
844 			if (*plenp != 0) {
845 				ip6stat.ip6s_badoptions++;
846 				icmp6_error(m, ICMP6_PARAM_PROB,
847 				    ICMP6_PARAMPROB_HEADER,
848 				    erroff + opt + 2 - opthead);
849 				return (-1);
850 			}
851 #endif
852 
853 			/*
854 			 * jumbo payload length must be larger than 65535.
855 			 */
856 			if (jumboplen <= IPV6_MAXPACKET) {
857 				ip6stat.ip6s_badoptions++;
858 				icmp6_error(m, ICMP6_PARAM_PROB,
859 				    ICMP6_PARAMPROB_HEADER,
860 				    erroff + opt + 2 - opthead);
861 				return (-1);
862 			}
863 			*plenp = jumboplen;
864 
865 			break;
866 		default:		/* unknown option */
867 			if (hbhlen < IP6OPT_MINLEN) {
868 				ip6stat.ip6s_toosmall++;
869 				goto bad;
870 			}
871 			optlen = ip6_unknown_opt(opt, m,
872 			    erroff + opt - opthead);
873 			if (optlen == -1)
874 				return (-1);
875 			optlen += 2;
876 			break;
877 		}
878 	}
879 
880 	return (0);
881 
882   bad:
883 	m_freem(m);
884 	return (-1);
885 }
886 
887 /*
888  * Unknown option processing.
889  * The third argument `off' is the offset from the IPv6 header to the option,
890  * which allows returning an ICMPv6 error even if the IPv6 header and the
891  * option header are not continuous.
892  */
893 int
894 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
895 {
896 	struct ip6_hdr *ip6;
897 
898 	switch (IP6OPT_TYPE(*optp)) {
899 	case IP6OPT_TYPE_SKIP: /* ignore the option */
900 		return ((int)*(optp + 1));
901 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
902 		m_freem(m);
903 		return (-1);
904 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
905 		ip6stat.ip6s_badoptions++;
906 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
907 		return (-1);
908 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
909 		ip6stat.ip6s_badoptions++;
910 		ip6 = mtod(m, struct ip6_hdr *);
911 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
912 		    (m->m_flags & (M_BCAST|M_MCAST)))
913 			m_freem(m);
914 		else
915 			icmp6_error(m, ICMP6_PARAM_PROB,
916 				    ICMP6_PARAMPROB_OPTION, off);
917 		return (-1);
918 	}
919 
920 	m_freem(m);		/* XXX: NOTREACHED */
921 	return (-1);
922 }
923 
924 /*
925  * Create the "control" list for this pcb.
926  *
927  * The routine will be called from upper layer handlers like tcp6_input().
928  * Thus the routine assumes that the caller (tcp6_input) have already
929  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
930  * very first mbuf on the mbuf chain.
931  * We may want to add some infinite loop prevention or sanity checks for safety.
932  * (This applies only when you are using KAME mbuf chain restriction, i.e.
933  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
934  */
935 void
936 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
937 {
938 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
939 
940 #ifdef SO_TIMESTAMP
941 	if (in6p->inp_socket->so_options & SO_TIMESTAMP) {
942 		struct timeval tv;
943 
944 		microtime(&tv);
945 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
946 		    SCM_TIMESTAMP, SOL_SOCKET);
947 		if (*mp)
948 			mp = &(*mp)->m_next;
949 	}
950 #endif
951 
952 	/* RFC 2292 sec. 5 */
953 	if ((in6p->inp_flags & IN6P_PKTINFO) != 0) {
954 		struct in6_pktinfo pi6;
955 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
956 		if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr))
957 			pi6.ipi6_addr.s6_addr16[1] = 0;
958 		pi6.ipi6_ifindex = m ? m->m_pkthdr.ph_ifidx : 0;
959 		*mp = sbcreatecontrol((caddr_t) &pi6,
960 		    sizeof(struct in6_pktinfo),
961 		    IPV6_PKTINFO, IPPROTO_IPV6);
962 		if (*mp)
963 			mp = &(*mp)->m_next;
964 	}
965 
966 	if ((in6p->inp_flags & IN6P_HOPLIMIT) != 0) {
967 		int hlim = ip6->ip6_hlim & 0xff;
968 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
969 		    IPV6_HOPLIMIT, IPPROTO_IPV6);
970 		if (*mp)
971 			mp = &(*mp)->m_next;
972 	}
973 
974 	if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
975 		u_int32_t flowinfo;
976 		int tclass;
977 
978 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
979 		flowinfo >>= 20;
980 
981 		tclass = flowinfo & 0xff;
982 		*mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass),
983 		    IPV6_TCLASS, IPPROTO_IPV6);
984 		if (*mp)
985 			mp = &(*mp)->m_next;
986 	}
987 
988 	/*
989 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
990 	 * privilege for the option (see ip6_ctloutput), but it might be too
991 	 * strict, since there might be some hop-by-hop options which can be
992 	 * returned to normal user.
993 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
994 	 */
995 	if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
996 		/*
997 		 * Check if a hop-by-hop options header is contained in the
998 		 * received packet, and if so, store the options as ancillary
999 		 * data. Note that a hop-by-hop options header must be
1000 		 * just after the IPv6 header, which is assured through the
1001 		 * IPv6 input processing.
1002 		 */
1003 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1004 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1005 			struct ip6_hbh *hbh;
1006 			int hbhlen = 0;
1007 			struct mbuf *ext;
1008 
1009 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1010 			    ip6->ip6_nxt);
1011 			if (ext == NULL) {
1012 				ip6stat.ip6s_tooshort++;
1013 				return;
1014 			}
1015 			hbh = mtod(ext, struct ip6_hbh *);
1016 			hbhlen = (hbh->ip6h_len + 1) << 3;
1017 			if (hbhlen != ext->m_len) {
1018 				m_freem(ext);
1019 				ip6stat.ip6s_tooshort++;
1020 				return;
1021 			}
1022 
1023 			/*
1024 			 * XXX: We copy the whole header even if a
1025 			 * jumbo payload option is included, the option which
1026 			 * is to be removed before returning according to
1027 			 * RFC2292.
1028 			 * Note: this constraint is removed in RFC3542.
1029 			 */
1030 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1031 			    IPV6_HOPOPTS,
1032 			    IPPROTO_IPV6);
1033 			if (*mp)
1034 				mp = &(*mp)->m_next;
1035 			m_freem(ext);
1036 		}
1037 	}
1038 
1039 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1040 	if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1041 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1042 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1043 
1044 		/*
1045 		 * Search for destination options headers or routing
1046 		 * header(s) through the header chain, and stores each
1047 		 * header as ancillary data.
1048 		 * Note that the order of the headers remains in
1049 		 * the chain of ancillary data.
1050 		 */
1051 		while (1) {	/* is explicit loop prevention necessary? */
1052 			struct ip6_ext *ip6e = NULL;
1053 			int elen;
1054 			struct mbuf *ext = NULL;
1055 
1056 			/*
1057 			 * if it is not an extension header, don't try to
1058 			 * pull it from the chain.
1059 			 */
1060 			switch (nxt) {
1061 			case IPPROTO_DSTOPTS:
1062 			case IPPROTO_ROUTING:
1063 			case IPPROTO_HOPOPTS:
1064 			case IPPROTO_AH: /* is it possible? */
1065 				break;
1066 			default:
1067 				goto loopend;
1068 			}
1069 
1070 			ext = ip6_pullexthdr(m, off, nxt);
1071 			if (ext == NULL) {
1072 				ip6stat.ip6s_tooshort++;
1073 				return;
1074 			}
1075 			ip6e = mtod(ext, struct ip6_ext *);
1076 			if (nxt == IPPROTO_AH)
1077 				elen = (ip6e->ip6e_len + 2) << 2;
1078 			else
1079 				elen = (ip6e->ip6e_len + 1) << 3;
1080 			if (elen != ext->m_len) {
1081 				m_freem(ext);
1082 				ip6stat.ip6s_tooshort++;
1083 				return;
1084 			}
1085 
1086 			switch (nxt) {
1087 			case IPPROTO_DSTOPTS:
1088 				if (!(in6p->inp_flags & IN6P_DSTOPTS))
1089 					break;
1090 
1091 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1092 				    IPV6_DSTOPTS,
1093 				    IPPROTO_IPV6);
1094 				if (*mp)
1095 					mp = &(*mp)->m_next;
1096 				break;
1097 
1098 			case IPPROTO_ROUTING:
1099 				if (!(in6p->inp_flags & IN6P_RTHDR))
1100 					break;
1101 
1102 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1103 				    IPV6_RTHDR,
1104 				    IPPROTO_IPV6);
1105 				if (*mp)
1106 					mp = &(*mp)->m_next;
1107 				break;
1108 
1109 			case IPPROTO_HOPOPTS:
1110 			case IPPROTO_AH: /* is it possible? */
1111 				break;
1112 
1113 			default:
1114 				/*
1115 				 * other cases have been filtered in the above.
1116 				 * none will visit this case.  here we supply
1117 				 * the code just in case (nxt overwritten or
1118 				 * other cases).
1119 				 */
1120 				m_freem(ext);
1121 				goto loopend;
1122 
1123 			}
1124 
1125 			/* proceed with the next header. */
1126 			off += elen;
1127 			nxt = ip6e->ip6e_nxt;
1128 			ip6e = NULL;
1129 			m_freem(ext);
1130 			ext = NULL;
1131 		}
1132 loopend:
1133 		;
1134 	}
1135 }
1136 
1137 /*
1138  * pull single extension header from mbuf chain.  returns single mbuf that
1139  * contains the result, or NULL on error.
1140  */
1141 struct mbuf *
1142 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1143 {
1144 	struct ip6_ext ip6e;
1145 	size_t elen;
1146 	struct mbuf *n;
1147 
1148 #ifdef DIAGNOSTIC
1149 	switch (nxt) {
1150 	case IPPROTO_DSTOPTS:
1151 	case IPPROTO_ROUTING:
1152 	case IPPROTO_HOPOPTS:
1153 	case IPPROTO_AH: /* is it possible? */
1154 		break;
1155 	default:
1156 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1157 	}
1158 #endif
1159 
1160 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1161 	if (nxt == IPPROTO_AH)
1162 		elen = (ip6e.ip6e_len + 2) << 2;
1163 	else
1164 		elen = (ip6e.ip6e_len + 1) << 3;
1165 
1166 	MGET(n, M_DONTWAIT, MT_DATA);
1167 	if (n && elen >= MLEN) {
1168 		MCLGET(n, M_DONTWAIT);
1169 		if ((n->m_flags & M_EXT) == 0) {
1170 			m_free(n);
1171 			n = NULL;
1172 		}
1173 	}
1174 	if (!n)
1175 		return NULL;
1176 
1177 	n->m_len = 0;
1178 	if (elen >= M_TRAILINGSPACE(n)) {
1179 		m_free(n);
1180 		return NULL;
1181 	}
1182 
1183 	m_copydata(m, off, elen, mtod(n, caddr_t));
1184 	n->m_len = elen;
1185 	return n;
1186 }
1187 
1188 /*
1189  * Get pointer to the previous header followed by the header
1190  * currently processed.
1191  * XXX: This function supposes that
1192  *	M includes all headers,
1193  *	the next header field and the header length field of each header
1194  *	are valid, and
1195  *	the sum of each header length equals to OFF.
1196  * Because of these assumptions, this function must be called very
1197  * carefully. Moreover, it will not be used in the near future when
1198  * we develop `neater' mechanism to process extension headers.
1199  */
1200 u_int8_t *
1201 ip6_get_prevhdr(struct mbuf *m, int off)
1202 {
1203 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1204 
1205 	if (off == sizeof(struct ip6_hdr))
1206 		return (&ip6->ip6_nxt);
1207 	else {
1208 		int len, nxt;
1209 		struct ip6_ext *ip6e = NULL;
1210 
1211 		nxt = ip6->ip6_nxt;
1212 		len = sizeof(struct ip6_hdr);
1213 		while (len < off) {
1214 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1215 
1216 			switch (nxt) {
1217 			case IPPROTO_FRAGMENT:
1218 				len += sizeof(struct ip6_frag);
1219 				break;
1220 			case IPPROTO_AH:
1221 				len += (ip6e->ip6e_len + 2) << 2;
1222 				break;
1223 			default:
1224 				len += (ip6e->ip6e_len + 1) << 3;
1225 				break;
1226 			}
1227 			nxt = ip6e->ip6e_nxt;
1228 		}
1229 		if (ip6e)
1230 			return (&ip6e->ip6e_nxt);
1231 		else
1232 			return NULL;
1233 	}
1234 }
1235 
1236 /*
1237  * get next header offset.  m will be retained.
1238  */
1239 int
1240 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1241 {
1242 	struct ip6_hdr ip6;
1243 	struct ip6_ext ip6e;
1244 	struct ip6_frag fh;
1245 
1246 	/* just in case */
1247 	if (m == NULL)
1248 		panic("ip6_nexthdr: m == NULL");
1249 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1250 		return -1;
1251 
1252 	switch (proto) {
1253 	case IPPROTO_IPV6:
1254 		if (m->m_pkthdr.len < off + sizeof(ip6))
1255 			return -1;
1256 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1257 		if (nxtp)
1258 			*nxtp = ip6.ip6_nxt;
1259 		off += sizeof(ip6);
1260 		return off;
1261 
1262 	case IPPROTO_FRAGMENT:
1263 		/*
1264 		 * terminate parsing if it is not the first fragment,
1265 		 * it does not make sense to parse through it.
1266 		 */
1267 		if (m->m_pkthdr.len < off + sizeof(fh))
1268 			return -1;
1269 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1270 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1271 			return -1;
1272 		if (nxtp)
1273 			*nxtp = fh.ip6f_nxt;
1274 		off += sizeof(struct ip6_frag);
1275 		return off;
1276 
1277 	case IPPROTO_AH:
1278 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1279 			return -1;
1280 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1281 		if (nxtp)
1282 			*nxtp = ip6e.ip6e_nxt;
1283 		off += (ip6e.ip6e_len + 2) << 2;
1284 		if (m->m_pkthdr.len < off)
1285 			return -1;
1286 		return off;
1287 
1288 	case IPPROTO_HOPOPTS:
1289 	case IPPROTO_ROUTING:
1290 	case IPPROTO_DSTOPTS:
1291 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1292 			return -1;
1293 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1294 		if (nxtp)
1295 			*nxtp = ip6e.ip6e_nxt;
1296 		off += (ip6e.ip6e_len + 1) << 3;
1297 		if (m->m_pkthdr.len < off)
1298 			return -1;
1299 		return off;
1300 
1301 	case IPPROTO_NONE:
1302 	case IPPROTO_ESP:
1303 	case IPPROTO_IPCOMP:
1304 		/* give up */
1305 		return -1;
1306 
1307 	default:
1308 		return -1;
1309 	}
1310 
1311 	return -1;
1312 }
1313 
1314 /*
1315  * get offset for the last header in the chain.  m will be kept untainted.
1316  */
1317 int
1318 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1319 {
1320 	int newoff;
1321 	int nxt;
1322 
1323 	if (!nxtp) {
1324 		nxt = -1;
1325 		nxtp = &nxt;
1326 	}
1327 	while (1) {
1328 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1329 		if (newoff < 0)
1330 			return off;
1331 		else if (newoff < off)
1332 			return -1;	/* invalid */
1333 		else if (newoff == off)
1334 			return newoff;
1335 
1336 		off = newoff;
1337 		proto = *nxtp;
1338 	}
1339 }
1340 
1341 /*
1342  * System control for IP6
1343  */
1344 
1345 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1346 	0,		0,		0,		0,
1347 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1348 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1349 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1350 	0,		0,		0,		0,
1351 	ENOPROTOOPT
1352 };
1353 
1354 int *ipv6ctl_vars[IPV6CTL_MAXID] = IPV6CTL_VARS;
1355 
1356 int
1357 ip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1358     void *newp, size_t newlen)
1359 {
1360 #ifdef MROUTING
1361 	extern int ip6_mrtproto;
1362 	extern struct mrt6stat mrt6stat;
1363 #endif
1364 	int error, s;
1365 
1366 	/* Almost all sysctl names at this level are terminal. */
1367 	if (namelen != 1 && name[0] != IPV6CTL_IFQUEUE)
1368 		return (ENOTDIR);
1369 
1370 	switch (name[0]) {
1371 	case IPV6CTL_DAD_PENDING:
1372 		return sysctl_rdint(oldp, oldlenp, newp, ip6_dad_pending);
1373 	case IPV6CTL_STATS:
1374 		if (newp != NULL)
1375 			return (EPERM);
1376 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
1377 		    &ip6stat, sizeof(ip6stat)));
1378 #ifdef MROUTING
1379 	case IPV6CTL_MRTSTATS:
1380 		if (newp != NULL)
1381 			return (EPERM);
1382 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
1383 		    &mrt6stat, sizeof(mrt6stat)));
1384 	case IPV6CTL_MRTPROTO:
1385 		return sysctl_rdint(oldp, oldlenp, newp, ip6_mrtproto);
1386 	case IPV6CTL_MRTMIF:
1387 		if (newp)
1388 			return (EPERM);
1389 		return mrt6_sysctl_mif(oldp, oldlenp);
1390 	case IPV6CTL_MRTMFC:
1391 		if (newp)
1392 			return (EPERM);
1393 		return mrt6_sysctl_mfc(oldp, oldlenp);
1394 #else
1395 	case IPV6CTL_MRTSTATS:
1396 	case IPV6CTL_MRTPROTO:
1397 	case IPV6CTL_MRTMIF:
1398 	case IPV6CTL_MRTMFC:
1399 		return (EOPNOTSUPP);
1400 #endif
1401 	case IPV6CTL_MTUDISCTIMEOUT:
1402 		error = sysctl_int(oldp, oldlenp, newp, newlen,
1403 		   &ip6_mtudisc_timeout);
1404 		if (icmp6_mtudisc_timeout_q != NULL) {
1405 			s = splsoftnet();
1406 			rt_timer_queue_change(icmp6_mtudisc_timeout_q,
1407 					      ip6_mtudisc_timeout);
1408 			splx(s);
1409 		}
1410 		return (error);
1411 	case IPV6CTL_IFQUEUE:
1412 		return (sysctl_niq(name + 1, namelen - 1,
1413 		    oldp, oldlenp, newp, newlen, &ip6intrq));
1414 	default:
1415 		if (name[0] < IPV6CTL_MAXID)
1416 			return (sysctl_int_arr(ipv6ctl_vars, name, namelen,
1417 			    oldp, oldlenp, newp, newlen));
1418 		return (EOPNOTSUPP);
1419 	}
1420 	/* NOTREACHED */
1421 }
1422 
1423 void
1424 ip6_send_dispatch(void *xmq)
1425 {
1426 	struct mbuf_queue *mq = xmq;
1427 	struct mbuf *m;
1428 	struct mbuf_list ml;
1429 	int s;
1430 
1431 	mq_delist(mq, &ml);
1432 	KERNEL_LOCK();
1433 	s = splsoftnet();
1434 	while ((m = ml_dequeue(&ml)) != NULL) {
1435 		ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL);
1436 	}
1437 	splx(s);
1438 	KERNEL_UNLOCK();
1439 }
1440 
1441 void
1442 ip6_send(struct mbuf *m)
1443 {
1444 	mq_enqueue(&ip6send_mq, m);
1445 	task_add(softnettq, &ip6send_task);
1446 }
1447