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