xref: /freebsd/sys/netinet6/ip6_input.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 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  * 4. 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 "opt_inet.h"
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/proc.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/errno.h>
78 #include <sys/time.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
81 
82 #include <net/if.h>
83 #include <net/if_types.h>
84 #include <net/if_dl.h>
85 #include <net/route.h>
86 #include <net/netisr.h>
87 #include <net/pfil.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #ifdef INET
92 #include <netinet/ip.h>
93 #include <netinet/ip_icmp.h>
94 #endif /* INET */
95 #include <netinet/ip6.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/scope6_var.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/nd6.h>
103 
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #ifdef INET6
107 #include <netinet6/ipsec6.h>
108 #endif
109 #endif
110 
111 #ifdef FAST_IPSEC
112 #include <netipsec/ipsec.h>
113 #include <netipsec/ipsec6.h>
114 #define	IPSEC
115 #endif /* FAST_IPSEC */
116 
117 #include <netinet6/ip6protosw.h>
118 
119 extern struct domain inet6domain;
120 
121 u_char ip6_protox[IPPROTO_MAX];
122 static struct ifqueue ip6intrq;
123 static int ip6qmaxlen = IFQ_MAXLEN;
124 struct in6_ifaddr *in6_ifaddr;
125 
126 extern struct callout in6_tmpaddrtimer_ch;
127 
128 int ip6_forward_srcrt;			/* XXX */
129 int ip6_sourcecheck;			/* XXX */
130 int ip6_sourcecheck_interval;		/* XXX */
131 
132 int ip6_ours_check_algorithm;
133 
134 struct pfil_head inet6_pfil_hook;
135 
136 struct ip6stat ip6stat;
137 
138 static void ip6_init2 __P((void *));
139 static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
140 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
141 #ifdef PULLDOWN_TEST
142 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
143 #endif
144 
145 /*
146  * IP6 initialization: fill in IP6 protocol switch table.
147  * All protocols not implemented in kernel go to raw IP6 protocol handler.
148  */
149 void
150 ip6_init()
151 {
152 	struct ip6protosw *pr;
153 	int i;
154 
155 #ifdef DIAGNOSTIC
156 	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
157 		panic("sizeof(protosw) != sizeof(ip6protosw)");
158 #endif
159 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
160 	if (pr == 0)
161 		panic("ip6_init");
162 
163 	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
164 	for (i = 0; i < IPPROTO_MAX; i++)
165 		ip6_protox[i] = pr - inet6sw;
166 	/*
167 	 * Cycle through IP protocols and put them into the appropriate place
168 	 * in ip6_protox[].
169 	 */
170 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
171 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
172 		if (pr->pr_domain->dom_family == PF_INET6 &&
173 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
174 			/* Be careful to only index valid IP protocols. */
175 			if (pr->pr_protocol < IPPROTO_MAX)
176 				ip6_protox[pr->pr_protocol] = pr - inet6sw;
177 		}
178 
179 	/* Initialize packet filter hooks. */
180 	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
181 	inet6_pfil_hook.ph_af = AF_INET6;
182 	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
183 		printf("%s: WARNING: unable to register pfil hook, "
184 			"error %d\n", __func__, i);
185 
186 	ip6intrq.ifq_maxlen = ip6qmaxlen;
187 	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
188 	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
189 	scope6_init();
190 	addrsel_policy_init();
191 	nd6_init();
192 	frag6_init();
193 	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
194 }
195 
196 static void
197 ip6_init2(dummy)
198 	void *dummy;
199 {
200 
201 	/* nd6_timer_init */
202 	callout_init(&nd6_timer_ch, 0);
203 	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
204 
205 	/* timer for regeneranation of temporary addresses randomize ID */
206 	callout_init(&in6_tmpaddrtimer_ch, 0);
207 	callout_reset(&in6_tmpaddrtimer_ch,
208 		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
209 		       ip6_temp_regen_advance) * hz,
210 		      in6_tmpaddrtimer, NULL);
211 }
212 
213 /* cheat */
214 /* This must be after route_init(), which is now SI_ORDER_THIRD */
215 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
216 
217 extern struct	route_in6 ip6_forward_rt;
218 
219 void
220 ip6_input(m)
221 	struct mbuf *m;
222 {
223 	struct ip6_hdr *ip6;
224 	int off = sizeof(struct ip6_hdr), nest;
225 	u_int32_t plen;
226 	u_int32_t rtalert = ~0;
227 	int nxt, ours = 0;
228 	struct ifnet *deliverifp = NULL;
229 	struct in6_addr odst;
230 	int srcrt = 0;
231 
232 	GIANT_REQUIRED;			/* XXX for now */
233 #ifdef IPSEC
234 	/*
235 	 * should the inner packet be considered authentic?
236 	 * see comment in ah4_input().
237 	 */
238 	if (m) {
239 		m->m_flags &= ~M_AUTHIPHDR;
240 		m->m_flags &= ~M_AUTHIPDGM;
241 	}
242 #endif
243 
244 	/*
245 	 * make sure we don't have onion peering information into m_tag.
246 	 */
247 	ip6_delaux(m);
248 
249 	/*
250 	 * mbuf statistics
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 #define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
259 		if (m->m_next) {
260 			if (m->m_flags & M_LOOP) {
261 				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
262 			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
263 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
264 			else
265 				ip6stat.ip6s_m2m[0]++;
266 		} else
267 			ip6stat.ip6s_m1++;
268 #undef M2MMAX
269 	}
270 
271 	/* drop the packet if IPv6 operation is disabled on the IF */
272 	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
273 		m_freem(m);
274 		return;
275 	}
276 
277 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
278 	ip6stat.ip6s_total++;
279 
280 #ifndef PULLDOWN_TEST
281 	/*
282 	 * L2 bridge code and some other code can return mbuf chain
283 	 * that does not conform to KAME requirement.  too bad.
284 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
285 	 */
286 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
287 		struct mbuf *n;
288 
289 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
290 		if (n)
291 			M_MOVE_PKTHDR(n, m);
292 		if (n && n->m_pkthdr.len > MHLEN) {
293 			MCLGET(n, M_DONTWAIT);
294 			if ((n->m_flags & M_EXT) == 0) {
295 				m_freem(n);
296 				n = NULL;
297 			}
298 		}
299 		if (n == NULL) {
300 			m_freem(m);
301 			return;	/* ENOBUFS */
302 		}
303 
304 		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
305 		n->m_len = n->m_pkthdr.len;
306 		m_freem(m);
307 		m = n;
308 	}
309 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
310 #endif
311 
312 	if (m->m_len < sizeof(struct ip6_hdr)) {
313 		struct ifnet *inifp;
314 		inifp = m->m_pkthdr.rcvif;
315 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
316 			ip6stat.ip6s_toosmall++;
317 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
318 			return;
319 		}
320 	}
321 
322 	ip6 = mtod(m, struct ip6_hdr *);
323 
324 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
325 		ip6stat.ip6s_badvers++;
326 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
327 		goto bad;
328 	}
329 
330 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
331 
332 	/*
333 	 * Check against address spoofing/corruption.
334 	 */
335 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
336 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
337 		/*
338 		 * XXX: "badscope" is not very suitable for a multicast source.
339 		 */
340 		ip6stat.ip6s_badscope++;
341 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
342 		goto bad;
343 	}
344 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
345 	    !(m->m_flags & M_LOOP)) {
346 		/*
347 		 * In this case, the packet should come from the loopback
348 		 * interface.  However, we cannot just check the if_flags,
349 		 * because ip6_mloopback() passes the "actual" interface
350 		 * as the outgoing/incoming interface.
351 		 */
352 		ip6stat.ip6s_badscope++;
353 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
354 		goto bad;
355 	}
356 
357 #ifdef ALTQ
358 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
359 		/* packet is dropped by traffic conditioner */
360 		return;
361 	}
362 #endif
363 	/*
364 	 * The following check is not documented in specs.  A malicious
365 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
366 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
367 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
368 	 *
369 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
370 	 * support IPv4-less kernel compilation, we cannot support SIIT
371 	 * environment at all.  So, it makes more sense for us to reject any
372 	 * malicious packets for non-SIIT environment, than try to do a
373 	 * partial support for SIIT environment.
374 	 */
375 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
376 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
377 		ip6stat.ip6s_badscope++;
378 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
379 		goto bad;
380 	}
381 #if 0
382 	/*
383 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
384 	 *
385 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
386 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
387 	 * is revised to forbid relaying case.
388 	 */
389 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
390 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
391 		ip6stat.ip6s_badscope++;
392 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
393 		goto bad;
394 	}
395 #endif
396 
397 	/*
398 	 * Run through list of hooks for input packets.
399 	 *
400 	 * NB: Beware of the destination address changing
401 	 *     (e.g. by NAT rewriting).  When this happens,
402 	 *     tell ip6_forward to do the right thing.
403 	 */
404 	odst = ip6->ip6_dst;
405 
406 	/* Jump over all PFIL processing if hooks are not active. */
407 	if (!PFIL_HOOKED(&inet6_pfil_hook))
408 		goto passin;
409 
410 	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
411 		return;
412 	if (m == NULL)			/* consumed by filter */
413 		return;
414 	ip6 = mtod(m, struct ip6_hdr *);
415 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
416 
417 passin:
418 	/*
419 	 * Disambiguate address scope zones (if there is ambiguity).
420 	 * We first make sure that the original source or destination address
421 	 * is not in our internal form for scoped addresses.  Such addresses
422 	 * are not necessarily invalid spec-wise, but we cannot accept them due
423 	 * to the usage conflict.
424 	 * in6_setscope() then also checks and rejects the cases where src or
425 	 * dst are the loopback address and the receiving interface
426 	 * is not loopback.
427 	 */
428 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
429 		ip6stat.ip6s_badscope++; /* XXX */
430 		goto bad;
431 	}
432 	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
433 	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
434 		ip6stat.ip6s_badscope++;
435 		goto bad;
436 	}
437 
438 	/*
439 	 * Multicast check
440 	 */
441 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
442 	  	struct in6_multi *in6m = 0;
443 
444 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
445 		/*
446 		 * See if we belong to the destination multicast group on the
447 		 * arrival interface.
448 		 */
449 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
450 		if (in6m)
451 			ours = 1;
452 		else if (!ip6_mrouter) {
453 			ip6stat.ip6s_notmember++;
454 			ip6stat.ip6s_cantforward++;
455 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
456 			goto bad;
457 		}
458 		deliverifp = m->m_pkthdr.rcvif;
459 		goto hbhcheck;
460 	}
461 
462 	/*
463 	 *  Unicast check
464 	 */
465 	if (ip6_forward_rt.ro_rt != NULL &&
466 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
467 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
468 	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
469 		ip6stat.ip6s_forward_cachehit++;
470 	else {
471 		struct sockaddr_in6 *dst6;
472 
473 		if (ip6_forward_rt.ro_rt) {
474 			/* route is down or destination is different */
475 			ip6stat.ip6s_forward_cachemiss++;
476 			RTFREE(ip6_forward_rt.ro_rt);
477 			ip6_forward_rt.ro_rt = 0;
478 		}
479 
480 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
481 		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
482 		dst6->sin6_len = sizeof(struct sockaddr_in6);
483 		dst6->sin6_family = AF_INET6;
484 		dst6->sin6_addr = ip6->ip6_dst;
485 
486 		rtalloc((struct route *)&ip6_forward_rt);
487 	}
488 
489 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
490 
491 	/*
492 	 * Accept the packet if the forwarding interface to the destination
493 	 * according to the routing table is the loopback interface,
494 	 * unless the associated route has a gateway.
495 	 * Note that this approach causes to accept a packet if there is a
496 	 * route to the loopback interface for the destination of the packet.
497 	 * But we think it's even useful in some situations, e.g. when using
498 	 * a special daemon which wants to intercept the packet.
499 	 *
500 	 * XXX: some OSes automatically make a cloned route for the destination
501 	 * of an outgoing packet.  If the outgoing interface of the packet
502 	 * is a loopback one, the kernel would consider the packet to be
503 	 * accepted, even if we have no such address assinged on the interface.
504 	 * We check the cloned flag of the route entry to reject such cases,
505 	 * assuming that route entries for our own addresses are not made by
506 	 * cloning (it should be true because in6_addloop explicitly installs
507 	 * the host route).  However, we might have to do an explicit check
508 	 * while it would be less efficient.  Or, should we rather install a
509 	 * reject route for such a case?
510 	 */
511 	if (ip6_forward_rt.ro_rt &&
512 	    (ip6_forward_rt.ro_rt->rt_flags &
513 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
514 #ifdef RTF_WASCLONED
515 	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
516 #endif
517 #ifdef RTF_CLONED
518 	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
519 #endif
520 #if 0
521 	    /*
522 	     * The check below is redundant since the comparison of
523 	     * the destination and the key of the rtentry has
524 	     * already done through looking up the routing table.
525 	     */
526 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
527 	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
528 #endif
529 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
530 		struct in6_ifaddr *ia6 =
531 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
532 
533 		/*
534 		 * record address information into m_tag.
535 		 */
536 		(void)ip6_setdstifaddr(m, ia6);
537 
538 		/*
539 		 * packets to a tentative, duplicated, or somehow invalid
540 		 * address must not be accepted.
541 		 */
542 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
543 			/* this address is ready */
544 			ours = 1;
545 			deliverifp = ia6->ia_ifp;	/* correct? */
546 			/* Count the packet in the ip address stats */
547 			ia6->ia_ifa.if_ipackets++;
548 			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
549 			goto hbhcheck;
550 		} else {
551 			char ip6bufs[INET6_ADDRSTRLEN];
552 			char ip6bufd[INET6_ADDRSTRLEN];
553 			/* address is not ready, so discard the packet. */
554 			nd6log((LOG_INFO,
555 			    "ip6_input: packet to an unready address %s->%s\n",
556 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
557 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
558 
559 			goto bad;
560 		}
561 	}
562 
563 	/*
564 	 * FAITH (Firewall Aided Internet Translator)
565 	 */
566 	if (ip6_keepfaith) {
567 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
568 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
569 			/* XXX do we need more sanity checks? */
570 			ours = 1;
571 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
572 			goto hbhcheck;
573 		}
574 	}
575 
576 	/*
577 	 * Now there is no reason to process the packet if it's not our own
578 	 * and we're not a router.
579 	 */
580 	if (!ip6_forwarding) {
581 		ip6stat.ip6s_cantforward++;
582 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
583 		goto bad;
584 	}
585 
586   hbhcheck:
587 	/*
588 	 * record address information into m_tag, if we don't have one yet.
589 	 * note that we are unable to record it, if the address is not listed
590 	 * as our interface address (e.g. multicast addresses, addresses
591 	 * within FAITH prefixes and such).
592 	 */
593 	if (deliverifp && !ip6_getdstifaddr(m)) {
594 		struct in6_ifaddr *ia6;
595 
596 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
597 		if (ia6) {
598 			if (!ip6_setdstifaddr(m, ia6)) {
599 				/*
600 				 * XXX maybe we should drop the packet here,
601 				 * as we could not provide enough information
602 				 * to the upper layers.
603 				 */
604 			}
605 		}
606 	}
607 
608 	/*
609 	 * Process Hop-by-Hop options header if it's contained.
610 	 * m may be modified in ip6_hopopts_input().
611 	 * If a JumboPayload option is included, plen will also be modified.
612 	 */
613 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
614 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
615 		struct ip6_hbh *hbh;
616 
617 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
618 #if 0	/*touches NULL pointer*/
619 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
620 #endif
621 			return;	/* m have already been freed */
622 		}
623 
624 		/* adjust pointer */
625 		ip6 = mtod(m, struct ip6_hdr *);
626 
627 		/*
628 		 * if the payload length field is 0 and the next header field
629 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
630 		 * option MUST be included.
631 		 */
632 		if (ip6->ip6_plen == 0 && plen == 0) {
633 			/*
634 			 * Note that if a valid jumbo payload option is
635 			 * contained, ip6_hopopts_input() must set a valid
636 			 * (non-zero) payload length to the variable plen.
637 			 */
638 			ip6stat.ip6s_badoptions++;
639 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
640 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
641 			icmp6_error(m, ICMP6_PARAM_PROB,
642 				    ICMP6_PARAMPROB_HEADER,
643 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
644 			return;
645 		}
646 #ifndef PULLDOWN_TEST
647 		/* ip6_hopopts_input() ensures that mbuf is contiguous */
648 		hbh = (struct ip6_hbh *)(ip6 + 1);
649 #else
650 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
651 			sizeof(struct ip6_hbh));
652 		if (hbh == NULL) {
653 			ip6stat.ip6s_tooshort++;
654 			return;
655 		}
656 #endif
657 		nxt = hbh->ip6h_nxt;
658 
659 		/*
660 		 * accept the packet if a router alert option is included
661 		 * and we act as an IPv6 router.
662 		 */
663 		if (rtalert != ~0 && ip6_forwarding)
664 			ours = 1;
665 	} else
666 		nxt = ip6->ip6_nxt;
667 
668 	/*
669 	 * Check that the amount of data in the buffers
670 	 * is as at least much as the IPv6 header would have us expect.
671 	 * Trim mbufs if longer than we expect.
672 	 * Drop packet if shorter than we expect.
673 	 */
674 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
675 		ip6stat.ip6s_tooshort++;
676 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
677 		goto bad;
678 	}
679 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
680 		if (m->m_len == m->m_pkthdr.len) {
681 			m->m_len = sizeof(struct ip6_hdr) + plen;
682 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
683 		} else
684 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
685 	}
686 
687 	/*
688 	 * Forward if desirable.
689 	 */
690 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
691 		/*
692 		 * If we are acting as a multicast router, all
693 		 * incoming multicast packets are passed to the
694 		 * kernel-level multicast forwarding function.
695 		 * The packet is returned (relatively) intact; if
696 		 * ip6_mforward() returns a non-zero value, the packet
697 		 * must be discarded, else it may be accepted below.
698 		 */
699 		if (ip6_mrouter && ip6_mforward &&
700 		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
701 			ip6stat.ip6s_cantforward++;
702 			m_freem(m);
703 			return;
704 		}
705 		if (!ours) {
706 			m_freem(m);
707 			return;
708 		}
709 	} else if (!ours) {
710 		ip6_forward(m, srcrt);
711 		return;
712 	}
713 
714 	ip6 = mtod(m, struct ip6_hdr *);
715 
716 	/*
717 	 * Malicious party may be able to use IPv4 mapped addr to confuse
718 	 * tcp/udp stack and bypass security checks (act as if it was from
719 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
720 	 *
721 	 * For SIIT end node behavior, you may want to disable the check.
722 	 * However, you will  become vulnerable to attacks using IPv4 mapped
723 	 * source.
724 	 */
725 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
726 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
727 		ip6stat.ip6s_badscope++;
728 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
729 		goto bad;
730 	}
731 
732 	/*
733 	 * Tell launch routine the next header
734 	 */
735 	ip6stat.ip6s_delivered++;
736 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
737 	nest = 0;
738 
739 	while (nxt != IPPROTO_DONE) {
740 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
741 			ip6stat.ip6s_toomanyhdr++;
742 			goto bad;
743 		}
744 
745 		/*
746 		 * protection against faulty packet - there should be
747 		 * more sanity checks in header chain processing.
748 		 */
749 		if (m->m_pkthdr.len < off) {
750 			ip6stat.ip6s_tooshort++;
751 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
752 			goto bad;
753 		}
754 
755 #ifdef IPSEC
756 		/*
757 		 * enforce IPsec policy checking if we are seeing last header.
758 		 * note that we do not visit this with protocols with pcb layer
759 		 * code - like udp/tcp/raw ip.
760 		 */
761 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
762 		    ipsec6_in_reject(m, NULL)) {
763 			ipsec6stat.in_polvio++;
764 			goto bad;
765 		}
766 #endif
767 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
768 	}
769 	return;
770  bad:
771 	m_freem(m);
772 }
773 
774 /*
775  * set/grab in6_ifaddr correspond to IPv6 destination address.
776  * XXX backward compatibility wrapper
777  */
778 static struct ip6aux *
779 ip6_setdstifaddr(m, ia6)
780 	struct mbuf *m;
781 	struct in6_ifaddr *ia6;
782 {
783 	struct ip6aux *ip6a;
784 
785 	ip6a = ip6_addaux(m);
786 	if (ip6a)
787 		ip6a->ip6a_dstia6 = ia6;
788 	return ip6a;	/* NULL if failed to set */
789 }
790 
791 struct in6_ifaddr *
792 ip6_getdstifaddr(m)
793 	struct mbuf *m;
794 {
795 	struct ip6aux *ip6a;
796 
797 	ip6a = ip6_findaux(m);
798 	if (ip6a)
799 		return ip6a->ip6a_dstia6;
800 	else
801 		return NULL;
802 }
803 
804 /*
805  * Hop-by-Hop options header processing. If a valid jumbo payload option is
806  * included, the real payload length will be stored in plenp.
807  */
808 static int
809 ip6_hopopts_input(plenp, rtalertp, mp, offp)
810 	u_int32_t *plenp;
811 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
812 	struct mbuf **mp;
813 	int *offp;
814 {
815 	struct mbuf *m = *mp;
816 	int off = *offp, hbhlen;
817 	struct ip6_hbh *hbh;
818 	u_int8_t *opt;
819 
820 	/* validation of the length of the header */
821 #ifndef PULLDOWN_TEST
822 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
823 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
824 	hbhlen = (hbh->ip6h_len + 1) << 3;
825 
826 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
827 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
828 #else
829 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
830 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
831 	if (hbh == NULL) {
832 		ip6stat.ip6s_tooshort++;
833 		return -1;
834 	}
835 	hbhlen = (hbh->ip6h_len + 1) << 3;
836 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
837 		hbhlen);
838 	if (hbh == NULL) {
839 		ip6stat.ip6s_tooshort++;
840 		return -1;
841 	}
842 #endif
843 	off += hbhlen;
844 	hbhlen -= sizeof(struct ip6_hbh);
845 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
846 
847 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
848 				hbhlen, rtalertp, plenp) < 0)
849 		return (-1);
850 
851 	*offp = off;
852 	*mp = m;
853 	return (0);
854 }
855 
856 /*
857  * Search header for all Hop-by-hop options and process each option.
858  * This function is separate from ip6_hopopts_input() in order to
859  * handle a case where the sending node itself process its hop-by-hop
860  * options header. In such a case, the function is called from ip6_output().
861  *
862  * The function assumes that hbh header is located right after the IPv6 header
863  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
864  * opthead + hbhlen is located in continuous memory region.
865  */
866 int
867 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
868 	struct mbuf *m;
869 	u_int8_t *opthead;
870 	int hbhlen;
871 	u_int32_t *rtalertp;
872 	u_int32_t *plenp;
873 {
874 	struct ip6_hdr *ip6;
875 	int optlen = 0;
876 	u_int8_t *opt = opthead;
877 	u_int16_t rtalert_val;
878 	u_int32_t jumboplen;
879 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
880 
881 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
882 		switch (*opt) {
883 		case IP6OPT_PAD1:
884 			optlen = 1;
885 			break;
886 		case IP6OPT_PADN:
887 			if (hbhlen < IP6OPT_MINLEN) {
888 				ip6stat.ip6s_toosmall++;
889 				goto bad;
890 			}
891 			optlen = *(opt + 1) + 2;
892 			break;
893 		case IP6OPT_ROUTER_ALERT:
894 			/* XXX may need check for alignment */
895 			if (hbhlen < IP6OPT_RTALERT_LEN) {
896 				ip6stat.ip6s_toosmall++;
897 				goto bad;
898 			}
899 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
900 				/* XXX stat */
901 				icmp6_error(m, ICMP6_PARAM_PROB,
902 				    ICMP6_PARAMPROB_HEADER,
903 				    erroff + opt + 1 - opthead);
904 				return (-1);
905 			}
906 			optlen = IP6OPT_RTALERT_LEN;
907 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
908 			*rtalertp = ntohs(rtalert_val);
909 			break;
910 		case IP6OPT_JUMBO:
911 			/* XXX may need check for alignment */
912 			if (hbhlen < IP6OPT_JUMBO_LEN) {
913 				ip6stat.ip6s_toosmall++;
914 				goto bad;
915 			}
916 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
917 				/* XXX stat */
918 				icmp6_error(m, ICMP6_PARAM_PROB,
919 				    ICMP6_PARAMPROB_HEADER,
920 				    erroff + opt + 1 - opthead);
921 				return (-1);
922 			}
923 			optlen = IP6OPT_JUMBO_LEN;
924 
925 			/*
926 			 * IPv6 packets that have non 0 payload length
927 			 * must not contain a jumbo payload option.
928 			 */
929 			ip6 = mtod(m, struct ip6_hdr *);
930 			if (ip6->ip6_plen) {
931 				ip6stat.ip6s_badoptions++;
932 				icmp6_error(m, ICMP6_PARAM_PROB,
933 				    ICMP6_PARAMPROB_HEADER,
934 				    erroff + opt - opthead);
935 				return (-1);
936 			}
937 
938 			/*
939 			 * We may see jumbolen in unaligned location, so
940 			 * we'd need to perform bcopy().
941 			 */
942 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
943 			jumboplen = (u_int32_t)htonl(jumboplen);
944 
945 #if 1
946 			/*
947 			 * if there are multiple jumbo payload options,
948 			 * *plenp will be non-zero and the packet will be
949 			 * rejected.
950 			 * the behavior may need some debate in ipngwg -
951 			 * multiple options does not make sense, however,
952 			 * there's no explicit mention in specification.
953 			 */
954 			if (*plenp != 0) {
955 				ip6stat.ip6s_badoptions++;
956 				icmp6_error(m, ICMP6_PARAM_PROB,
957 				    ICMP6_PARAMPROB_HEADER,
958 				    erroff + opt + 2 - opthead);
959 				return (-1);
960 			}
961 #endif
962 
963 			/*
964 			 * jumbo payload length must be larger than 65535.
965 			 */
966 			if (jumboplen <= IPV6_MAXPACKET) {
967 				ip6stat.ip6s_badoptions++;
968 				icmp6_error(m, ICMP6_PARAM_PROB,
969 				    ICMP6_PARAMPROB_HEADER,
970 				    erroff + opt + 2 - opthead);
971 				return (-1);
972 			}
973 			*plenp = jumboplen;
974 
975 			break;
976 		default:		/* unknown option */
977 			if (hbhlen < IP6OPT_MINLEN) {
978 				ip6stat.ip6s_toosmall++;
979 				goto bad;
980 			}
981 			optlen = ip6_unknown_opt(opt, m,
982 			    erroff + opt - opthead);
983 			if (optlen == -1)
984 				return (-1);
985 			optlen += 2;
986 			break;
987 		}
988 	}
989 
990 	return (0);
991 
992   bad:
993 	m_freem(m);
994 	return (-1);
995 }
996 
997 /*
998  * Unknown option processing.
999  * The third argument `off' is the offset from the IPv6 header to the option,
1000  * which is necessary if the IPv6 header the and option header and IPv6 header
1001  * is not continuous in order to return an ICMPv6 error.
1002  */
1003 int
1004 ip6_unknown_opt(optp, m, off)
1005 	u_int8_t *optp;
1006 	struct mbuf *m;
1007 	int off;
1008 {
1009 	struct ip6_hdr *ip6;
1010 
1011 	switch (IP6OPT_TYPE(*optp)) {
1012 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1013 		return ((int)*(optp + 1));
1014 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1015 		m_freem(m);
1016 		return (-1);
1017 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1018 		ip6stat.ip6s_badoptions++;
1019 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1020 		return (-1);
1021 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1022 		ip6stat.ip6s_badoptions++;
1023 		ip6 = mtod(m, struct ip6_hdr *);
1024 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1025 		    (m->m_flags & (M_BCAST|M_MCAST)))
1026 			m_freem(m);
1027 		else
1028 			icmp6_error(m, ICMP6_PARAM_PROB,
1029 				    ICMP6_PARAMPROB_OPTION, off);
1030 		return (-1);
1031 	}
1032 
1033 	m_freem(m);		/* XXX: NOTREACHED */
1034 	return (-1);
1035 }
1036 
1037 /*
1038  * Create the "control" list for this pcb.
1039  * The function will not modify mbuf chain at all.
1040  *
1041  * with KAME mbuf chain restriction:
1042  * The routine will be called from upper layer handlers like tcp6_input().
1043  * Thus the routine assumes that the caller (tcp6_input) have already
1044  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1045  * very first mbuf on the mbuf chain.
1046  */
1047 void
1048 ip6_savecontrol(in6p, m, mp)
1049 	struct inpcb *in6p;
1050 	struct mbuf *m, **mp;
1051 {
1052 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1053 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1054 
1055 #ifdef SO_TIMESTAMP
1056 	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1057 		struct timeval tv;
1058 
1059 		microtime(&tv);
1060 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1061 		    SCM_TIMESTAMP, SOL_SOCKET);
1062 		if (*mp)
1063 			mp = &(*mp)->m_next;
1064 	}
1065 #endif
1066 
1067 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1068 		return;
1069 
1070 	/* RFC 2292 sec. 5 */
1071 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1072 		struct in6_pktinfo pi6;
1073 
1074 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1075 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1076 		pi6.ipi6_ifindex =
1077 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1078 
1079 		*mp = sbcreatecontrol((caddr_t) &pi6,
1080 		    sizeof(struct in6_pktinfo),
1081 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1082 		if (*mp)
1083 			mp = &(*mp)->m_next;
1084 	}
1085 
1086 	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1087 		int hlim = ip6->ip6_hlim & 0xff;
1088 
1089 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1090 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1091 		if (*mp)
1092 			mp = &(*mp)->m_next;
1093 	}
1094 
1095 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1096 		u_int32_t flowinfo;
1097 		int tclass;
1098 
1099 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1100 		flowinfo >>= 20;
1101 
1102 		tclass = flowinfo & 0xff;
1103 		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1104 		    IPV6_TCLASS, IPPROTO_IPV6);
1105 		if (*mp)
1106 			mp = &(*mp)->m_next;
1107 	}
1108 
1109 	/*
1110 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1111 	 * privilege for the option (see ip6_ctloutput), but it might be too
1112 	 * strict, since there might be some hop-by-hop options which can be
1113 	 * returned to normal user.
1114 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1115 	 */
1116 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1117 		/*
1118 		 * Check if a hop-by-hop options header is contatined in the
1119 		 * received packet, and if so, store the options as ancillary
1120 		 * data. Note that a hop-by-hop options header must be
1121 		 * just after the IPv6 header, which is assured through the
1122 		 * IPv6 input processing.
1123 		 */
1124 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1125 			struct ip6_hbh *hbh;
1126 			int hbhlen = 0;
1127 #ifdef PULLDOWN_TEST
1128 			struct mbuf *ext;
1129 #endif
1130 
1131 #ifndef PULLDOWN_TEST
1132 			hbh = (struct ip6_hbh *)(ip6 + 1);
1133 			hbhlen = (hbh->ip6h_len + 1) << 3;
1134 #else
1135 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1136 			    ip6->ip6_nxt);
1137 			if (ext == NULL) {
1138 				ip6stat.ip6s_tooshort++;
1139 				return;
1140 			}
1141 			hbh = mtod(ext, struct ip6_hbh *);
1142 			hbhlen = (hbh->ip6h_len + 1) << 3;
1143 			if (hbhlen != ext->m_len) {
1144 				m_freem(ext);
1145 				ip6stat.ip6s_tooshort++;
1146 				return;
1147 			}
1148 #endif
1149 
1150 			/*
1151 			 * XXX: We copy the whole header even if a
1152 			 * jumbo payload option is included, the option which
1153 			 * is to be removed before returning according to
1154 			 * RFC2292.
1155 			 * Note: this constraint is removed in RFC3542
1156 			 */
1157 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1158 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1159 			    IPPROTO_IPV6);
1160 			if (*mp)
1161 				mp = &(*mp)->m_next;
1162 #ifdef PULLDOWN_TEST
1163 			m_freem(ext);
1164 #endif
1165 		}
1166 	}
1167 
1168 	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1169 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1170 
1171 		/*
1172 		 * Search for destination options headers or routing
1173 		 * header(s) through the header chain, and stores each
1174 		 * header as ancillary data.
1175 		 * Note that the order of the headers remains in
1176 		 * the chain of ancillary data.
1177 		 */
1178 		while (1) {	/* is explicit loop prevention necessary? */
1179 			struct ip6_ext *ip6e = NULL;
1180 			int elen;
1181 #ifdef PULLDOWN_TEST
1182 			struct mbuf *ext = NULL;
1183 #endif
1184 
1185 			/*
1186 			 * if it is not an extension header, don't try to
1187 			 * pull it from the chain.
1188 			 */
1189 			switch (nxt) {
1190 			case IPPROTO_DSTOPTS:
1191 			case IPPROTO_ROUTING:
1192 			case IPPROTO_HOPOPTS:
1193 			case IPPROTO_AH: /* is it possible? */
1194 				break;
1195 			default:
1196 				goto loopend;
1197 			}
1198 
1199 #ifndef PULLDOWN_TEST
1200 			if (off + sizeof(*ip6e) > m->m_len)
1201 				goto loopend;
1202 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1203 			if (nxt == IPPROTO_AH)
1204 				elen = (ip6e->ip6e_len + 2) << 2;
1205 			else
1206 				elen = (ip6e->ip6e_len + 1) << 3;
1207 			if (off + elen > m->m_len)
1208 				goto loopend;
1209 #else
1210 			ext = ip6_pullexthdr(m, off, nxt);
1211 			if (ext == NULL) {
1212 				ip6stat.ip6s_tooshort++;
1213 				return;
1214 			}
1215 			ip6e = mtod(ext, struct ip6_ext *);
1216 			if (nxt == IPPROTO_AH)
1217 				elen = (ip6e->ip6e_len + 2) << 2;
1218 			else
1219 				elen = (ip6e->ip6e_len + 1) << 3;
1220 			if (elen != ext->m_len) {
1221 				m_freem(ext);
1222 				ip6stat.ip6s_tooshort++;
1223 				return;
1224 			}
1225 #endif
1226 
1227 			switch (nxt) {
1228 			case IPPROTO_DSTOPTS:
1229 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1230 					break;
1231 
1232 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1233 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1234 				    IPPROTO_IPV6);
1235 				if (*mp)
1236 					mp = &(*mp)->m_next;
1237 				break;
1238 			case IPPROTO_ROUTING:
1239 				if (!in6p->in6p_flags & IN6P_RTHDR)
1240 					break;
1241 
1242 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1243 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1244 				    IPPROTO_IPV6);
1245 				if (*mp)
1246 					mp = &(*mp)->m_next;
1247 				break;
1248 			case IPPROTO_HOPOPTS:
1249 			case IPPROTO_AH: /* is it possible? */
1250 				break;
1251 
1252 			default:
1253 				/*
1254 			 	 * other cases have been filtered in the above.
1255 				 * none will visit this case.  here we supply
1256 				 * the code just in case (nxt overwritten or
1257 				 * other cases).
1258 				 */
1259 #ifdef PULLDOWN_TEST
1260 				m_freem(ext);
1261 #endif
1262 				goto loopend;
1263 
1264 			}
1265 
1266 			/* proceed with the next header. */
1267 			off += elen;
1268 			nxt = ip6e->ip6e_nxt;
1269 			ip6e = NULL;
1270 #ifdef PULLDOWN_TEST
1271 			m_freem(ext);
1272 			ext = NULL;
1273 #endif
1274 		}
1275 	  loopend:
1276 		;
1277 	}
1278 
1279 #undef IS2292
1280 }
1281 
1282 void
1283 ip6_notify_pmtu(in6p, dst, mtu)
1284 	struct inpcb *in6p;
1285 	struct sockaddr_in6 *dst;
1286 	u_int32_t *mtu;
1287 {
1288 	struct socket *so;
1289 	struct mbuf *m_mtu;
1290 	struct ip6_mtuinfo mtuctl;
1291 
1292 	so =  in6p->inp_socket;
1293 
1294 	if (mtu == NULL)
1295 		return;
1296 
1297 #ifdef DIAGNOSTIC
1298 	if (so == NULL)		/* I believe this is impossible */
1299 		panic("ip6_notify_pmtu: socket is NULL");
1300 #endif
1301 
1302 	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1303 	mtuctl.ip6m_mtu = *mtu;
1304 	mtuctl.ip6m_addr = *dst;
1305 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1306 		return;
1307 
1308 	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1309 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1310 		return;
1311 
1312 	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1313 	    == 0) {
1314 		m_freem(m_mtu);
1315 		/* XXX: should count statistics */
1316 	} else
1317 		sorwakeup(so);
1318 
1319 	return;
1320 }
1321 
1322 #ifdef PULLDOWN_TEST
1323 /*
1324  * pull single extension header from mbuf chain.  returns single mbuf that
1325  * contains the result, or NULL on error.
1326  */
1327 static struct mbuf *
1328 ip6_pullexthdr(m, off, nxt)
1329 	struct mbuf *m;
1330 	size_t off;
1331 	int nxt;
1332 {
1333 	struct ip6_ext ip6e;
1334 	size_t elen;
1335 	struct mbuf *n;
1336 
1337 #ifdef DIAGNOSTIC
1338 	switch (nxt) {
1339 	case IPPROTO_DSTOPTS:
1340 	case IPPROTO_ROUTING:
1341 	case IPPROTO_HOPOPTS:
1342 	case IPPROTO_AH: /* is it possible? */
1343 		break;
1344 	default:
1345 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1346 	}
1347 #endif
1348 
1349 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1350 	if (nxt == IPPROTO_AH)
1351 		elen = (ip6e.ip6e_len + 2) << 2;
1352 	else
1353 		elen = (ip6e.ip6e_len + 1) << 3;
1354 
1355 	MGET(n, M_DONTWAIT, MT_DATA);
1356 	if (n && elen >= MLEN) {
1357 		MCLGET(n, M_DONTWAIT);
1358 		if ((n->m_flags & M_EXT) == 0) {
1359 			m_free(n);
1360 			n = NULL;
1361 		}
1362 	}
1363 	if (!n)
1364 		return NULL;
1365 
1366 	n->m_len = 0;
1367 	if (elen >= M_TRAILINGSPACE(n)) {
1368 		m_free(n);
1369 		return NULL;
1370 	}
1371 
1372 	m_copydata(m, off, elen, mtod(n, caddr_t));
1373 	n->m_len = elen;
1374 	return n;
1375 }
1376 #endif
1377 
1378 /*
1379  * Get pointer to the previous header followed by the header
1380  * currently processed.
1381  * XXX: This function supposes that
1382  *	M includes all headers,
1383  *	the next header field and the header length field of each header
1384  *	are valid, and
1385  *	the sum of each header length equals to OFF.
1386  * Because of these assumptions, this function must be called very
1387  * carefully. Moreover, it will not be used in the near future when
1388  * we develop `neater' mechanism to process extension headers.
1389  */
1390 char *
1391 ip6_get_prevhdr(m, off)
1392 	struct mbuf *m;
1393 	int off;
1394 {
1395 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1396 
1397 	if (off == sizeof(struct ip6_hdr))
1398 		return (&ip6->ip6_nxt);
1399 	else {
1400 		int len, nxt;
1401 		struct ip6_ext *ip6e = NULL;
1402 
1403 		nxt = ip6->ip6_nxt;
1404 		len = sizeof(struct ip6_hdr);
1405 		while (len < off) {
1406 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1407 
1408 			switch (nxt) {
1409 			case IPPROTO_FRAGMENT:
1410 				len += sizeof(struct ip6_frag);
1411 				break;
1412 			case IPPROTO_AH:
1413 				len += (ip6e->ip6e_len + 2) << 2;
1414 				break;
1415 			default:
1416 				len += (ip6e->ip6e_len + 1) << 3;
1417 				break;
1418 			}
1419 			nxt = ip6e->ip6e_nxt;
1420 		}
1421 		if (ip6e)
1422 			return (&ip6e->ip6e_nxt);
1423 		else
1424 			return NULL;
1425 	}
1426 }
1427 
1428 /*
1429  * get next header offset.  m will be retained.
1430  */
1431 int
1432 ip6_nexthdr(m, off, proto, nxtp)
1433 	struct mbuf *m;
1434 	int off;
1435 	int proto;
1436 	int *nxtp;
1437 {
1438 	struct ip6_hdr ip6;
1439 	struct ip6_ext ip6e;
1440 	struct ip6_frag fh;
1441 
1442 	/* just in case */
1443 	if (m == NULL)
1444 		panic("ip6_nexthdr: m == NULL");
1445 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1446 		return -1;
1447 
1448 	switch (proto) {
1449 	case IPPROTO_IPV6:
1450 		if (m->m_pkthdr.len < off + sizeof(ip6))
1451 			return -1;
1452 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1453 		if (nxtp)
1454 			*nxtp = ip6.ip6_nxt;
1455 		off += sizeof(ip6);
1456 		return off;
1457 
1458 	case IPPROTO_FRAGMENT:
1459 		/*
1460 		 * terminate parsing if it is not the first fragment,
1461 		 * it does not make sense to parse through it.
1462 		 */
1463 		if (m->m_pkthdr.len < off + sizeof(fh))
1464 			return -1;
1465 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1466 		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1467 		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1468 			return -1;
1469 		if (nxtp)
1470 			*nxtp = fh.ip6f_nxt;
1471 		off += sizeof(struct ip6_frag);
1472 		return off;
1473 
1474 	case IPPROTO_AH:
1475 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1476 			return -1;
1477 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1478 		if (nxtp)
1479 			*nxtp = ip6e.ip6e_nxt;
1480 		off += (ip6e.ip6e_len + 2) << 2;
1481 		return off;
1482 
1483 	case IPPROTO_HOPOPTS:
1484 	case IPPROTO_ROUTING:
1485 	case IPPROTO_DSTOPTS:
1486 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1487 			return -1;
1488 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1489 		if (nxtp)
1490 			*nxtp = ip6e.ip6e_nxt;
1491 		off += (ip6e.ip6e_len + 1) << 3;
1492 		return off;
1493 
1494 	case IPPROTO_NONE:
1495 	case IPPROTO_ESP:
1496 	case IPPROTO_IPCOMP:
1497 		/* give up */
1498 		return -1;
1499 
1500 	default:
1501 		return -1;
1502 	}
1503 
1504 	return -1;
1505 }
1506 
1507 /*
1508  * get offset for the last header in the chain.  m will be kept untainted.
1509  */
1510 int
1511 ip6_lasthdr(m, off, proto, nxtp)
1512 	struct mbuf *m;
1513 	int off;
1514 	int proto;
1515 	int *nxtp;
1516 {
1517 	int newoff;
1518 	int nxt;
1519 
1520 	if (!nxtp) {
1521 		nxt = -1;
1522 		nxtp = &nxt;
1523 	}
1524 	while (1) {
1525 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1526 		if (newoff < 0)
1527 			return off;
1528 		else if (newoff < off)
1529 			return -1;	/* invalid */
1530 		else if (newoff == off)
1531 			return newoff;
1532 
1533 		off = newoff;
1534 		proto = *nxtp;
1535 	}
1536 }
1537 
1538 struct ip6aux *
1539 ip6_addaux(m)
1540 	struct mbuf *m;
1541 {
1542 	struct m_tag *mtag;
1543 
1544 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1545 	if (!mtag) {
1546 		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1547 		    M_NOWAIT);
1548 		if (mtag) {
1549 			m_tag_prepend(m, mtag);
1550 			bzero(mtag + 1, sizeof(struct ip6aux));
1551 		}
1552 	}
1553 	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1554 }
1555 
1556 struct ip6aux *
1557 ip6_findaux(m)
1558 	struct mbuf *m;
1559 {
1560 	struct m_tag *mtag;
1561 
1562 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1563 	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1564 }
1565 
1566 void
1567 ip6_delaux(m)
1568 	struct mbuf *m;
1569 {
1570 	struct m_tag *mtag;
1571 
1572 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1573 	if (mtag)
1574 		m_tag_delete(m, mtag);
1575 }
1576 
1577 /*
1578  * System control for IP6
1579  */
1580 
1581 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1582 	0,		0,		0,		0,
1583 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1584 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1585 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1586 	0,		0,		0,		0,
1587 	ENOPROTOOPT
1588 };
1589