xref: /freebsd/sys/netinet6/ip6_input.c (revision 6574b8ed19b093f0af09501d2c9676c28993cb97)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1988, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipfw.h"
69 #include "opt_ipsec.h"
70 #include "opt_route.h"
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/proc.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/sdt.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/errno.h>
83 #include <sys/time.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86 
87 #include <net/if.h>
88 #include <net/if_var.h>
89 #include <net/if_types.h>
90 #include <net/if_dl.h>
91 #include <net/route.h>
92 #include <net/netisr.h>
93 #include <net/pfil.h>
94 #include <net/vnet.h>
95 
96 #include <netinet/in.h>
97 #include <netinet/in_kdtrace.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/in_systm.h>
100 #include <net/if_llatbl.h>
101 #ifdef INET
102 #include <netinet/ip.h>
103 #include <netinet/ip_icmp.h>
104 #endif /* INET */
105 #include <netinet/ip6.h>
106 #include <netinet6/in6_var.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet/icmp6.h>
110 #include <netinet6/scope6_var.h>
111 #include <netinet6/in6_ifattach.h>
112 #include <netinet6/nd6.h>
113 
114 #ifdef IPSEC
115 #include <netipsec/ipsec.h>
116 #include <netinet6/ip6_ipsec.h>
117 #include <netipsec/ipsec6.h>
118 #endif /* IPSEC */
119 
120 #include <netinet6/ip6protosw.h>
121 
122 extern struct domain inet6domain;
123 
124 u_char ip6_protox[IPPROTO_MAX];
125 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
126 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
127 VNET_DEFINE(u_long, in6_ifaddrhmask);
128 
129 static struct netisr_handler ip6_nh = {
130 	.nh_name = "ip6",
131 	.nh_handler = ip6_input,
132 	.nh_proto = NETISR_IPV6,
133 	.nh_policy = NETISR_POLICY_FLOW,
134 };
135 
136 VNET_DECLARE(struct callout, in6_tmpaddrtimer_ch);
137 #define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
138 
139 VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
140 
141 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
142 VNET_PCPUSTAT_SYSINIT(ip6stat);
143 #ifdef VIMAGE
144 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
145 #endif /* VIMAGE */
146 
147 struct rwlock in6_ifaddr_lock;
148 RW_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
149 
150 static void ip6_init2(void *);
151 static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
152 static struct ip6aux *ip6_addaux(struct mbuf *);
153 static struct ip6aux *ip6_findaux(struct mbuf *m);
154 static void ip6_delaux (struct mbuf *);
155 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
156 #ifdef PULLDOWN_TEST
157 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
158 #endif
159 
160 /*
161  * IP6 initialization: fill in IP6 protocol switch table.
162  * All protocols not implemented in kernel go to raw IP6 protocol handler.
163  */
164 void
165 ip6_init(void)
166 {
167 	struct protosw *pr;
168 	int i;
169 
170 	TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
171 	    &V_ip6_auto_linklocal);
172 	TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
173 	TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
174 
175 	TAILQ_INIT(&V_in6_ifaddrhead);
176 	V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
177 	    &V_in6_ifaddrhmask);
178 
179 	/* Initialize packet filter hooks. */
180 	V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
181 	V_inet6_pfil_hook.ph_af = AF_INET6;
182 	if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
183 		printf("%s: WARNING: unable to register pfil hook, "
184 			"error %d\n", __func__, i);
185 
186 	scope6_init();
187 	addrsel_policy_init();
188 	nd6_init();
189 	frag6_init();
190 
191 	V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
192 
193 	/* Skip global initialization stuff for non-default instances. */
194 	if (!IS_DEFAULT_VNET(curvnet))
195 		return;
196 
197 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
198 	if (pr == NULL)
199 		panic("ip6_init");
200 
201 	/* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
202 	for (i = 0; i < IPPROTO_MAX; i++)
203 		ip6_protox[i] = pr - inet6sw;
204 	/*
205 	 * Cycle through IP protocols and put them into the appropriate place
206 	 * in ip6_protox[].
207 	 */
208 	for (pr = inet6domain.dom_protosw;
209 	    pr < inet6domain.dom_protoswNPROTOSW; pr++)
210 		if (pr->pr_domain->dom_family == PF_INET6 &&
211 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
212 			/* Be careful to only index valid IP protocols. */
213 			if (pr->pr_protocol < IPPROTO_MAX)
214 				ip6_protox[pr->pr_protocol] = pr - inet6sw;
215 		}
216 
217 	netisr_register(&ip6_nh);
218 }
219 
220 /*
221  * The protocol to be inserted into ip6_protox[] must be already registered
222  * in inet6sw[], either statically or through pf_proto_register().
223  */
224 int
225 ip6proto_register(short ip6proto)
226 {
227 	struct protosw *pr;
228 
229 	/* Sanity checks. */
230 	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
231 		return (EPROTONOSUPPORT);
232 
233 	/*
234 	 * The protocol slot must not be occupied by another protocol
235 	 * already.  An index pointing to IPPROTO_RAW is unused.
236 	 */
237 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
238 	if (pr == NULL)
239 		return (EPFNOSUPPORT);
240 	if (ip6_protox[ip6proto] != pr - inet6sw)	/* IPPROTO_RAW */
241 		return (EEXIST);
242 
243 	/*
244 	 * Find the protocol position in inet6sw[] and set the index.
245 	 */
246 	for (pr = inet6domain.dom_protosw;
247 	    pr < inet6domain.dom_protoswNPROTOSW; pr++) {
248 		if (pr->pr_domain->dom_family == PF_INET6 &&
249 		    pr->pr_protocol && pr->pr_protocol == ip6proto) {
250 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
251 			return (0);
252 		}
253 	}
254 	return (EPROTONOSUPPORT);
255 }
256 
257 int
258 ip6proto_unregister(short ip6proto)
259 {
260 	struct protosw *pr;
261 
262 	/* Sanity checks. */
263 	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
264 		return (EPROTONOSUPPORT);
265 
266 	/* Check if the protocol was indeed registered. */
267 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
268 	if (pr == NULL)
269 		return (EPFNOSUPPORT);
270 	if (ip6_protox[ip6proto] == pr - inet6sw)	/* IPPROTO_RAW */
271 		return (ENOENT);
272 
273 	/* Reset the protocol slot to IPPROTO_RAW. */
274 	ip6_protox[ip6proto] = pr - inet6sw;
275 	return (0);
276 }
277 
278 #ifdef VIMAGE
279 void
280 ip6_destroy()
281 {
282 	int i;
283 
284 	if ((i = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
285 		printf("%s: WARNING: unable to unregister pfil hook, "
286 		    "error %d\n", __func__, i);
287 	hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
288 	nd6_destroy();
289 	callout_drain(&V_in6_tmpaddrtimer_ch);
290 }
291 #endif
292 
293 static int
294 ip6_init2_vnet(const void *unused __unused)
295 {
296 
297 	/* nd6_timer_init */
298 	callout_init(&V_nd6_timer_ch, 0);
299 	callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet);
300 
301 	/* timer for regeneranation of temporary addresses randomize ID */
302 	callout_init(&V_in6_tmpaddrtimer_ch, 0);
303 	callout_reset(&V_in6_tmpaddrtimer_ch,
304 		      (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
305 		       V_ip6_temp_regen_advance) * hz,
306 		      in6_tmpaddrtimer, curvnet);
307 
308 	return (0);
309 }
310 
311 static void
312 ip6_init2(void *dummy)
313 {
314 
315 	ip6_init2_vnet(NULL);
316 }
317 
318 /* cheat */
319 /* This must be after route_init(), which is now SI_ORDER_THIRD */
320 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
321 
322 static int
323 ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
324     int *nxt, int *ours)
325 {
326 	struct ip6_hdr *ip6;
327 	struct ip6_hbh *hbh;
328 
329 	if (ip6_hopopts_input(plen, rtalert, &m, off)) {
330 #if 0	/*touches NULL pointer*/
331 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
332 #endif
333 		goto out;	/* m have already been freed */
334 	}
335 
336 	/* adjust pointer */
337 	ip6 = mtod(m, struct ip6_hdr *);
338 
339 	/*
340 	 * if the payload length field is 0 and the next header field
341 	 * indicates Hop-by-Hop Options header, then a Jumbo Payload
342 	 * option MUST be included.
343 	 */
344 	if (ip6->ip6_plen == 0 && *plen == 0) {
345 		/*
346 		 * Note that if a valid jumbo payload option is
347 		 * contained, ip6_hopopts_input() must set a valid
348 		 * (non-zero) payload length to the variable plen.
349 		 */
350 		IP6STAT_INC(ip6s_badoptions);
351 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
352 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
353 		icmp6_error(m, ICMP6_PARAM_PROB,
354 			    ICMP6_PARAMPROB_HEADER,
355 			    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
356 		goto out;
357 	}
358 #ifndef PULLDOWN_TEST
359 	/* ip6_hopopts_input() ensures that mbuf is contiguous */
360 	hbh = (struct ip6_hbh *)(ip6 + 1);
361 #else
362 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
363 		sizeof(struct ip6_hbh));
364 	if (hbh == NULL) {
365 		IP6STAT_INC(ip6s_tooshort);
366 		goto out;
367 	}
368 #endif
369 	*nxt = hbh->ip6h_nxt;
370 
371 	/*
372 	 * If we are acting as a router and the packet contains a
373 	 * router alert option, see if we know the option value.
374 	 * Currently, we only support the option value for MLD, in which
375 	 * case we should pass the packet to the multicast routing
376 	 * daemon.
377 	 */
378 	if (*rtalert != ~0) {
379 		switch (*rtalert) {
380 		case IP6OPT_RTALERT_MLD:
381 			if (V_ip6_forwarding)
382 				*ours = 1;
383 			break;
384 		default:
385 			/*
386 			 * RFC2711 requires unrecognized values must be
387 			 * silently ignored.
388 			 */
389 			break;
390 		}
391 	}
392 
393 	return (0);
394 
395 out:
396 	return (1);
397 }
398 
399 void
400 ip6_input(struct mbuf *m)
401 {
402 	struct ip6_hdr *ip6;
403 	int off = sizeof(struct ip6_hdr), nest;
404 	u_int32_t plen;
405 	u_int32_t rtalert = ~0;
406 	int nxt, ours = 0;
407 	struct ifnet *deliverifp = NULL, *ifp = NULL;
408 	struct in6_addr odst;
409 	struct route_in6 rin6;
410 	int srcrt = 0;
411 	struct llentry *lle = NULL;
412 	struct sockaddr_in6 dst6, *dst;
413 
414 	bzero(&rin6, sizeof(struct route_in6));
415 #ifdef IPSEC
416 	/*
417 	 * should the inner packet be considered authentic?
418 	 * see comment in ah4_input().
419 	 * NB: m cannot be NULL when passed to the input routine
420 	 */
421 
422 	m->m_flags &= ~M_AUTHIPHDR;
423 	m->m_flags &= ~M_AUTHIPDGM;
424 
425 #endif /* IPSEC */
426 
427 	/*
428 	 * make sure we don't have onion peering information into m_tag.
429 	 */
430 	ip6_delaux(m);
431 
432 	if (m->m_flags & M_FASTFWD_OURS) {
433 		/*
434 		 * Firewall changed destination to local.
435 		 */
436 		m->m_flags &= ~M_FASTFWD_OURS;
437 		ours = 1;
438 		deliverifp = m->m_pkthdr.rcvif;
439 		ip6 = mtod(m, struct ip6_hdr *);
440 		goto hbhcheck;
441 	}
442 
443 	/*
444 	 * mbuf statistics
445 	 */
446 	if (m->m_flags & M_EXT) {
447 		if (m->m_next)
448 			IP6STAT_INC(ip6s_mext2m);
449 		else
450 			IP6STAT_INC(ip6s_mext1);
451 	} else {
452 		if (m->m_next) {
453 			if (m->m_flags & M_LOOP) {
454 				IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
455 			} else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX)
456 				IP6STAT_INC(
457 				    ip6s_m2m[m->m_pkthdr.rcvif->if_index]);
458 			else
459 				IP6STAT_INC(ip6s_m2m[0]);
460 		} else
461 			IP6STAT_INC(ip6s_m1);
462 	}
463 
464 	/* drop the packet if IPv6 operation is disabled on the IF */
465 	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
466 		m_freem(m);
467 		return;
468 	}
469 
470 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
471 	IP6STAT_INC(ip6s_total);
472 
473 #ifndef PULLDOWN_TEST
474 	/*
475 	 * L2 bridge code and some other code can return mbuf chain
476 	 * that does not conform to KAME requirement.  too bad.
477 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
478 	 */
479 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
480 		struct mbuf *n;
481 
482 		if (m->m_pkthdr.len > MHLEN)
483 			n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
484 		else
485 			n = m_gethdr(M_NOWAIT, MT_DATA);
486 		if (n == NULL) {
487 			m_freem(m);
488 			return;	/* ENOBUFS */
489 		}
490 
491 		m_move_pkthdr(n, m);
492 		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
493 		n->m_len = n->m_pkthdr.len;
494 		m_freem(m);
495 		m = n;
496 	}
497 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
498 #endif
499 
500 	if (m->m_len < sizeof(struct ip6_hdr)) {
501 		struct ifnet *inifp;
502 		inifp = m->m_pkthdr.rcvif;
503 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
504 			IP6STAT_INC(ip6s_toosmall);
505 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
506 			return;
507 		}
508 	}
509 
510 	ip6 = mtod(m, struct ip6_hdr *);
511 
512 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
513 		IP6STAT_INC(ip6s_badvers);
514 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
515 		goto bad;
516 	}
517 
518 	IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
519 
520 	IP_PROBE(receive, NULL, NULL, ip6, m->m_pkthdr.rcvif, NULL, ip6);
521 
522 	/*
523 	 * Check against address spoofing/corruption.
524 	 */
525 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
526 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
527 		/*
528 		 * XXX: "badscope" is not very suitable for a multicast source.
529 		 */
530 		IP6STAT_INC(ip6s_badscope);
531 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
532 		goto bad;
533 	}
534 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
535 	    !(m->m_flags & M_LOOP)) {
536 		/*
537 		 * In this case, the packet should come from the loopback
538 		 * interface.  However, we cannot just check the if_flags,
539 		 * because ip6_mloopback() passes the "actual" interface
540 		 * as the outgoing/incoming interface.
541 		 */
542 		IP6STAT_INC(ip6s_badscope);
543 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
544 		goto bad;
545 	}
546 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
547 	    IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
548 		/*
549 		 * RFC4291 2.7:
550 		 * Nodes must not originate a packet to a multicast address
551 		 * whose scop field contains the reserved value 0; if such
552 		 * a packet is received, it must be silently dropped.
553 		 */
554 		IP6STAT_INC(ip6s_badscope);
555 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
556 		goto bad;
557 	}
558 #ifdef ALTQ
559 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
560 		/* packet is dropped by traffic conditioner */
561 		return;
562 	}
563 #endif
564 	/*
565 	 * The following check is not documented in specs.  A malicious
566 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
567 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
568 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
569 	 *
570 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
571 	 * support IPv4-less kernel compilation, we cannot support SIIT
572 	 * environment at all.  So, it makes more sense for us to reject any
573 	 * malicious packets for non-SIIT environment, than try to do a
574 	 * partial support for SIIT environment.
575 	 */
576 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
577 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
578 		IP6STAT_INC(ip6s_badscope);
579 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
580 		goto bad;
581 	}
582 #if 0
583 	/*
584 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
585 	 *
586 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
587 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
588 	 * is revised to forbid relaying case.
589 	 */
590 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
591 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
592 		IP6STAT_INC(ip6s_badscope);
593 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
594 		goto bad;
595 	}
596 #endif
597 #ifdef IPSEC
598 	/*
599 	 * Bypass packet filtering for packets previously handled by IPsec.
600 	 */
601 	if (ip6_ipsec_filtertunnel(m))
602 		goto passin;
603 #endif /* IPSEC */
604 
605 	/*
606 	 * Run through list of hooks for input packets.
607 	 *
608 	 * NB: Beware of the destination address changing
609 	 *     (e.g. by NAT rewriting).  When this happens,
610 	 *     tell ip6_forward to do the right thing.
611 	 */
612 	odst = ip6->ip6_dst;
613 
614 	/* Jump over all PFIL processing if hooks are not active. */
615 	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
616 		goto passin;
617 
618 	if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
619 	    m->m_pkthdr.rcvif, PFIL_IN, NULL))
620 		return;
621 	if (m == NULL)			/* consumed by filter */
622 		return;
623 	ip6 = mtod(m, struct ip6_hdr *);
624 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
625 
626 	if (m->m_flags & M_FASTFWD_OURS) {
627 		m->m_flags &= ~M_FASTFWD_OURS;
628 		ours = 1;
629 		deliverifp = m->m_pkthdr.rcvif;
630 		goto hbhcheck;
631 	}
632 	if ((m->m_flags & M_IP6_NEXTHOP) &&
633 	    m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
634 		/*
635 		 * Directly ship the packet on.  This allows forwarding
636 		 * packets originally destined to us to some other directly
637 		 * connected host.
638 		 */
639 		ip6_forward(m, 1);
640 		goto out;
641 	}
642 
643 passin:
644 	/*
645 	 * Disambiguate address scope zones (if there is ambiguity).
646 	 * We first make sure that the original source or destination address
647 	 * is not in our internal form for scoped addresses.  Such addresses
648 	 * are not necessarily invalid spec-wise, but we cannot accept them due
649 	 * to the usage conflict.
650 	 * in6_setscope() then also checks and rejects the cases where src or
651 	 * dst are the loopback address and the receiving interface
652 	 * is not loopback.
653 	 */
654 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
655 		IP6STAT_INC(ip6s_badscope); /* XXX */
656 		goto bad;
657 	}
658 	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
659 	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
660 		IP6STAT_INC(ip6s_badscope);
661 		goto bad;
662 	}
663 
664 	/*
665 	 * Multicast check. Assume packet is for us to avoid
666 	 * prematurely taking locks.
667 	 */
668 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
669 		ours = 1;
670 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
671 		deliverifp = m->m_pkthdr.rcvif;
672 		goto hbhcheck;
673 	}
674 
675 	/*
676 	 *  Unicast check
677 	 */
678 
679 	bzero(&dst6, sizeof(dst6));
680 	dst6.sin6_family = AF_INET6;
681 	dst6.sin6_len = sizeof(struct sockaddr_in6);
682 	dst6.sin6_addr = ip6->ip6_dst;
683 	ifp = m->m_pkthdr.rcvif;
684 	IF_AFDATA_RLOCK(ifp);
685 	lle = lla_lookup(LLTABLE6(ifp), 0,
686 	     (struct sockaddr *)&dst6);
687 	IF_AFDATA_RUNLOCK(ifp);
688 	if ((lle != NULL) && (lle->la_flags & LLE_IFADDR)) {
689 		struct ifaddr *ifa;
690 		struct in6_ifaddr *ia6;
691 		int bad;
692 
693 		bad = 1;
694 		IF_ADDR_RLOCK(ifp);
695 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
696 			if (ifa->ifa_addr->sa_family != dst6.sin6_family)
697 				continue;
698 			if (sa_equal(&dst6, ifa->ifa_addr))
699 				break;
700 		}
701 		KASSERT(ifa != NULL, ("%s: ifa not found for lle %p",
702 		    __func__, lle));
703 
704 		ia6 = (struct in6_ifaddr *)ifa;
705 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
706 			/* Count the packet in the ip address stats */
707 			counter_u64_add(ia6->ia_ifa.ifa_ipackets, 1);
708 			counter_u64_add(ia6->ia_ifa.ifa_ibytes,
709 			    m->m_pkthdr.len);
710 
711 			/*
712 			 * record address information into m_tag.
713 			 */
714 			(void)ip6_setdstifaddr(m, ia6);
715 
716 			bad = 0;
717 		} else {
718 			char ip6bufs[INET6_ADDRSTRLEN];
719 			char ip6bufd[INET6_ADDRSTRLEN];
720 			/* address is not ready, so discard the packet. */
721 			nd6log((LOG_INFO,
722 			    "ip6_input: packet to an unready address %s->%s\n",
723 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
724 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
725 		}
726 		IF_ADDR_RUNLOCK(ifp);
727 		LLE_RUNLOCK(lle);
728 		if (bad)
729 			goto bad;
730 		else {
731 			ours = 1;
732 			deliverifp = ifp;
733 			goto hbhcheck;
734 		}
735 	}
736 	if (lle != NULL)
737 		LLE_RUNLOCK(lle);
738 
739 	dst = &rin6.ro_dst;
740 	dst->sin6_len = sizeof(struct sockaddr_in6);
741 	dst->sin6_family = AF_INET6;
742 	dst->sin6_addr = ip6->ip6_dst;
743 	rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m));
744 	if (rin6.ro_rt)
745 		RT_UNLOCK(rin6.ro_rt);
746 
747 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
748 
749 	/*
750 	 * Accept the packet if the forwarding interface to the destination
751 	 * according to the routing table is the loopback interface,
752 	 * unless the associated route has a gateway.
753 	 * Note that this approach causes to accept a packet if there is a
754 	 * route to the loopback interface for the destination of the packet.
755 	 * But we think it's even useful in some situations, e.g. when using
756 	 * a special daemon which wants to intercept the packet.
757 	 *
758 	 * XXX: some OSes automatically make a cloned route for the destination
759 	 * of an outgoing packet.  If the outgoing interface of the packet
760 	 * is a loopback one, the kernel would consider the packet to be
761 	 * accepted, even if we have no such address assinged on the interface.
762 	 * We check the cloned flag of the route entry to reject such cases,
763 	 * assuming that route entries for our own addresses are not made by
764 	 * cloning (it should be true because in6_addloop explicitly installs
765 	 * the host route).  However, we might have to do an explicit check
766 	 * while it would be less efficient.  Or, should we rather install a
767 	 * reject route for such a case?
768 	 */
769 	if (rin6.ro_rt &&
770 	    (rin6.ro_rt->rt_flags &
771 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
772 #ifdef RTF_WASCLONED
773 	    !(rin6.ro_rt->rt_flags & RTF_WASCLONED) &&
774 #endif
775 #ifdef RTF_CLONED
776 	    !(rin6.ro_rt->rt_flags & RTF_CLONED) &&
777 #endif
778 #if 0
779 	    /*
780 	     * The check below is redundant since the comparison of
781 	     * the destination and the key of the rtentry has
782 	     * already done through looking up the routing table.
783 	     */
784 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
785 	    &rt6_key(rin6.ro_rt)->sin6_addr)
786 #endif
787 	    rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) {
788 		int free_ia6 = 0;
789 		struct in6_ifaddr *ia6;
790 
791 		/*
792 		 * found the loopback route to the interface address
793 		 */
794 		if (rin6.ro_rt->rt_gateway->sa_family == AF_LINK) {
795 			struct sockaddr_in6 dest6;
796 
797 			bzero(&dest6, sizeof(dest6));
798 			dest6.sin6_family = AF_INET6;
799 			dest6.sin6_len = sizeof(dest6);
800 			dest6.sin6_addr = ip6->ip6_dst;
801 			ia6 = (struct in6_ifaddr *)
802 			    ifa_ifwithaddr((struct sockaddr *)&dest6);
803 			if (ia6 == NULL)
804 				goto bad;
805 			free_ia6 = 1;
806 		}
807 		else
808 			ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa;
809 
810 		/*
811 		 * record address information into m_tag.
812 		 */
813 		(void)ip6_setdstifaddr(m, ia6);
814 
815 		/*
816 		 * packets to a tentative, duplicated, or somehow invalid
817 		 * address must not be accepted.
818 		 */
819 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
820 			/* this address is ready */
821 			ours = 1;
822 			deliverifp = ia6->ia_ifp;	/* correct? */
823 			/* Count the packet in the ip address stats */
824 			counter_u64_add(ia6->ia_ifa.ifa_ipackets, 1);
825 			counter_u64_add(ia6->ia_ifa.ifa_ibytes,
826 			    m->m_pkthdr.len);
827 			if (free_ia6)
828 				ifa_free(&ia6->ia_ifa);
829 			goto hbhcheck;
830 		} else {
831 			char ip6bufs[INET6_ADDRSTRLEN];
832 			char ip6bufd[INET6_ADDRSTRLEN];
833 			/* address is not ready, so discard the packet. */
834 			nd6log((LOG_INFO,
835 			    "ip6_input: packet to an unready address %s->%s\n",
836 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
837 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
838 
839 			if (free_ia6)
840 				ifa_free(&ia6->ia_ifa);
841 			goto bad;
842 		}
843 	}
844 
845 	/*
846 	 * FAITH (Firewall Aided Internet Translator)
847 	 */
848 	if (V_ip6_keepfaith) {
849 		if (rin6.ro_rt && rin6.ro_rt->rt_ifp &&
850 		    rin6.ro_rt->rt_ifp->if_type == IFT_FAITH) {
851 			/* XXX do we need more sanity checks? */
852 			ours = 1;
853 			deliverifp = rin6.ro_rt->rt_ifp; /* faith */
854 			goto hbhcheck;
855 		}
856 	}
857 
858 	/*
859 	 * Now there is no reason to process the packet if it's not our own
860 	 * and we're not a router.
861 	 */
862 	if (!V_ip6_forwarding) {
863 		IP6STAT_INC(ip6s_cantforward);
864 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
865 		goto bad;
866 	}
867 
868   hbhcheck:
869 	/*
870 	 * record address information into m_tag, if we don't have one yet.
871 	 * note that we are unable to record it, if the address is not listed
872 	 * as our interface address (e.g. multicast addresses, addresses
873 	 * within FAITH prefixes and such).
874 	 */
875 	if (deliverifp) {
876 		struct in6_ifaddr *ia6;
877 
878  		if ((ia6 = ip6_getdstifaddr(m)) != NULL) {
879 			ifa_free(&ia6->ia_ifa);
880 		} else {
881 			ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
882 			if (ia6) {
883 				if (!ip6_setdstifaddr(m, ia6)) {
884 					/*
885 					 * XXX maybe we should drop the packet here,
886 					 * as we could not provide enough information
887 					 * to the upper layers.
888 					 */
889 				}
890 				ifa_free(&ia6->ia_ifa);
891 			}
892 		}
893 	}
894 
895 	/*
896 	 * Process Hop-by-Hop options header if it's contained.
897 	 * m may be modified in ip6_hopopts_input().
898 	 * If a JumboPayload option is included, plen will also be modified.
899 	 */
900 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
901 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
902 		int error;
903 
904 		error = ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours);
905 		if (error != 0)
906 			goto out;
907 	} else
908 		nxt = ip6->ip6_nxt;
909 
910 	/*
911 	 * Check that the amount of data in the buffers
912 	 * is as at least much as the IPv6 header would have us expect.
913 	 * Trim mbufs if longer than we expect.
914 	 * Drop packet if shorter than we expect.
915 	 */
916 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
917 		IP6STAT_INC(ip6s_tooshort);
918 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
919 		goto bad;
920 	}
921 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
922 		if (m->m_len == m->m_pkthdr.len) {
923 			m->m_len = sizeof(struct ip6_hdr) + plen;
924 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
925 		} else
926 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
927 	}
928 
929 	/*
930 	 * Forward if desirable.
931 	 */
932 	if (V_ip6_mrouter &&
933 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
934 		/*
935 		 * If we are acting as a multicast router, all
936 		 * incoming multicast packets are passed to the
937 		 * kernel-level multicast forwarding function.
938 		 * The packet is returned (relatively) intact; if
939 		 * ip6_mforward() returns a non-zero value, the packet
940 		 * must be discarded, else it may be accepted below.
941 		 *
942 		 * XXX TODO: Check hlim and multicast scope here to avoid
943 		 * unnecessarily calling into ip6_mforward().
944 		 */
945 		if (ip6_mforward &&
946 		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
947 			IP6STAT_INC(ip6s_cantforward);
948 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
949 			goto bad;
950 		}
951 	} else if (!ours) {
952 		ip6_forward(m, srcrt);
953 		goto out;
954 	}
955 
956 	ip6 = mtod(m, struct ip6_hdr *);
957 
958 	/*
959 	 * Malicious party may be able to use IPv4 mapped addr to confuse
960 	 * tcp/udp stack and bypass security checks (act as if it was from
961 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
962 	 *
963 	 * For SIIT end node behavior, you may want to disable the check.
964 	 * However, you will  become vulnerable to attacks using IPv4 mapped
965 	 * source.
966 	 */
967 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
968 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
969 		IP6STAT_INC(ip6s_badscope);
970 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
971 		goto bad;
972 	}
973 
974 	/*
975 	 * Tell launch routine the next header
976 	 */
977 	IP6STAT_INC(ip6s_delivered);
978 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
979 	nest = 0;
980 
981 	while (nxt != IPPROTO_DONE) {
982 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
983 			IP6STAT_INC(ip6s_toomanyhdr);
984 			goto bad;
985 		}
986 
987 		/*
988 		 * protection against faulty packet - there should be
989 		 * more sanity checks in header chain processing.
990 		 */
991 		if (m->m_pkthdr.len < off) {
992 			IP6STAT_INC(ip6s_tooshort);
993 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
994 			goto bad;
995 		}
996 
997 #ifdef IPSEC
998 		/*
999 		 * enforce IPsec policy checking if we are seeing last header.
1000 		 * note that we do not visit this with protocols with pcb layer
1001 		 * code - like udp/tcp/raw ip.
1002 		 */
1003 		if (ip6_ipsec_input(m, nxt))
1004 			goto bad;
1005 #endif /* IPSEC */
1006 
1007 		/*
1008 		 * Use mbuf flags to propagate Router Alert option to
1009 		 * ICMPv6 layer, as hop-by-hop options have been stripped.
1010 		 */
1011 		if (nxt == IPPROTO_ICMPV6 && rtalert != ~0)
1012 			m->m_flags |= M_RTALERT_MLD;
1013 
1014 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
1015 	}
1016 	goto out;
1017 bad:
1018 	m_freem(m);
1019 out:
1020 	if (rin6.ro_rt)
1021 		RTFREE(rin6.ro_rt);
1022 }
1023 
1024 /*
1025  * set/grab in6_ifaddr correspond to IPv6 destination address.
1026  * XXX backward compatibility wrapper
1027  *
1028  * XXXRW: We should bump the refcount on ia6 before sticking it in the m_tag,
1029  * and then bump it when the tag is copied, and release it when the tag is
1030  * freed.  Unfortunately, m_tags don't support deep copies (yet), so instead
1031  * we just bump the ia refcount when we receive it.  This should be fixed.
1032  */
1033 static struct ip6aux *
1034 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
1035 {
1036 	struct ip6aux *ip6a;
1037 
1038 	ip6a = ip6_addaux(m);
1039 	if (ip6a)
1040 		ip6a->ip6a_dstia6 = ia6;
1041 	return ip6a;	/* NULL if failed to set */
1042 }
1043 
1044 struct in6_ifaddr *
1045 ip6_getdstifaddr(struct mbuf *m)
1046 {
1047 	struct ip6aux *ip6a;
1048 	struct in6_ifaddr *ia;
1049 
1050 	ip6a = ip6_findaux(m);
1051 	if (ip6a) {
1052 		ia = ip6a->ip6a_dstia6;
1053 		ifa_ref(&ia->ia_ifa);
1054 		return ia;
1055 	} else
1056 		return NULL;
1057 }
1058 
1059 /*
1060  * Hop-by-Hop options header processing. If a valid jumbo payload option is
1061  * included, the real payload length will be stored in plenp.
1062  *
1063  * rtalertp - XXX: should be stored more smart way
1064  */
1065 static int
1066 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
1067     struct mbuf **mp, int *offp)
1068 {
1069 	struct mbuf *m = *mp;
1070 	int off = *offp, hbhlen;
1071 	struct ip6_hbh *hbh;
1072 
1073 	/* validation of the length of the header */
1074 #ifndef PULLDOWN_TEST
1075 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
1076 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1077 	hbhlen = (hbh->ip6h_len + 1) << 3;
1078 
1079 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
1080 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1081 #else
1082 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1083 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1084 	if (hbh == NULL) {
1085 		IP6STAT_INC(ip6s_tooshort);
1086 		return -1;
1087 	}
1088 	hbhlen = (hbh->ip6h_len + 1) << 3;
1089 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
1090 		hbhlen);
1091 	if (hbh == NULL) {
1092 		IP6STAT_INC(ip6s_tooshort);
1093 		return -1;
1094 	}
1095 #endif
1096 	off += hbhlen;
1097 	hbhlen -= sizeof(struct ip6_hbh);
1098 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1099 				hbhlen, rtalertp, plenp) < 0)
1100 		return (-1);
1101 
1102 	*offp = off;
1103 	*mp = m;
1104 	return (0);
1105 }
1106 
1107 /*
1108  * Search header for all Hop-by-hop options and process each option.
1109  * This function is separate from ip6_hopopts_input() in order to
1110  * handle a case where the sending node itself process its hop-by-hop
1111  * options header. In such a case, the function is called from ip6_output().
1112  *
1113  * The function assumes that hbh header is located right after the IPv6 header
1114  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1115  * opthead + hbhlen is located in contiguous memory region.
1116  */
1117 int
1118 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1119     u_int32_t *rtalertp, u_int32_t *plenp)
1120 {
1121 	struct ip6_hdr *ip6;
1122 	int optlen = 0;
1123 	u_int8_t *opt = opthead;
1124 	u_int16_t rtalert_val;
1125 	u_int32_t jumboplen;
1126 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1127 
1128 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1129 		switch (*opt) {
1130 		case IP6OPT_PAD1:
1131 			optlen = 1;
1132 			break;
1133 		case IP6OPT_PADN:
1134 			if (hbhlen < IP6OPT_MINLEN) {
1135 				IP6STAT_INC(ip6s_toosmall);
1136 				goto bad;
1137 			}
1138 			optlen = *(opt + 1) + 2;
1139 			break;
1140 		case IP6OPT_ROUTER_ALERT:
1141 			/* XXX may need check for alignment */
1142 			if (hbhlen < IP6OPT_RTALERT_LEN) {
1143 				IP6STAT_INC(ip6s_toosmall);
1144 				goto bad;
1145 			}
1146 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1147 				/* XXX stat */
1148 				icmp6_error(m, ICMP6_PARAM_PROB,
1149 				    ICMP6_PARAMPROB_HEADER,
1150 				    erroff + opt + 1 - opthead);
1151 				return (-1);
1152 			}
1153 			optlen = IP6OPT_RTALERT_LEN;
1154 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1155 			*rtalertp = ntohs(rtalert_val);
1156 			break;
1157 		case IP6OPT_JUMBO:
1158 			/* XXX may need check for alignment */
1159 			if (hbhlen < IP6OPT_JUMBO_LEN) {
1160 				IP6STAT_INC(ip6s_toosmall);
1161 				goto bad;
1162 			}
1163 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1164 				/* XXX stat */
1165 				icmp6_error(m, ICMP6_PARAM_PROB,
1166 				    ICMP6_PARAMPROB_HEADER,
1167 				    erroff + opt + 1 - opthead);
1168 				return (-1);
1169 			}
1170 			optlen = IP6OPT_JUMBO_LEN;
1171 
1172 			/*
1173 			 * IPv6 packets that have non 0 payload length
1174 			 * must not contain a jumbo payload option.
1175 			 */
1176 			ip6 = mtod(m, struct ip6_hdr *);
1177 			if (ip6->ip6_plen) {
1178 				IP6STAT_INC(ip6s_badoptions);
1179 				icmp6_error(m, ICMP6_PARAM_PROB,
1180 				    ICMP6_PARAMPROB_HEADER,
1181 				    erroff + opt - opthead);
1182 				return (-1);
1183 			}
1184 
1185 			/*
1186 			 * We may see jumbolen in unaligned location, so
1187 			 * we'd need to perform bcopy().
1188 			 */
1189 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1190 			jumboplen = (u_int32_t)htonl(jumboplen);
1191 
1192 #if 1
1193 			/*
1194 			 * if there are multiple jumbo payload options,
1195 			 * *plenp will be non-zero and the packet will be
1196 			 * rejected.
1197 			 * the behavior may need some debate in ipngwg -
1198 			 * multiple options does not make sense, however,
1199 			 * there's no explicit mention in specification.
1200 			 */
1201 			if (*plenp != 0) {
1202 				IP6STAT_INC(ip6s_badoptions);
1203 				icmp6_error(m, ICMP6_PARAM_PROB,
1204 				    ICMP6_PARAMPROB_HEADER,
1205 				    erroff + opt + 2 - opthead);
1206 				return (-1);
1207 			}
1208 #endif
1209 
1210 			/*
1211 			 * jumbo payload length must be larger than 65535.
1212 			 */
1213 			if (jumboplen <= IPV6_MAXPACKET) {
1214 				IP6STAT_INC(ip6s_badoptions);
1215 				icmp6_error(m, ICMP6_PARAM_PROB,
1216 				    ICMP6_PARAMPROB_HEADER,
1217 				    erroff + opt + 2 - opthead);
1218 				return (-1);
1219 			}
1220 			*plenp = jumboplen;
1221 
1222 			break;
1223 		default:		/* unknown option */
1224 			if (hbhlen < IP6OPT_MINLEN) {
1225 				IP6STAT_INC(ip6s_toosmall);
1226 				goto bad;
1227 			}
1228 			optlen = ip6_unknown_opt(opt, m,
1229 			    erroff + opt - opthead);
1230 			if (optlen == -1)
1231 				return (-1);
1232 			optlen += 2;
1233 			break;
1234 		}
1235 	}
1236 
1237 	return (0);
1238 
1239   bad:
1240 	m_freem(m);
1241 	return (-1);
1242 }
1243 
1244 /*
1245  * Unknown option processing.
1246  * The third argument `off' is the offset from the IPv6 header to the option,
1247  * which is necessary if the IPv6 header the and option header and IPv6 header
1248  * is not contiguous in order to return an ICMPv6 error.
1249  */
1250 int
1251 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1252 {
1253 	struct ip6_hdr *ip6;
1254 
1255 	switch (IP6OPT_TYPE(*optp)) {
1256 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1257 		return ((int)*(optp + 1));
1258 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1259 		m_freem(m);
1260 		return (-1);
1261 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1262 		IP6STAT_INC(ip6s_badoptions);
1263 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1264 		return (-1);
1265 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1266 		IP6STAT_INC(ip6s_badoptions);
1267 		ip6 = mtod(m, struct ip6_hdr *);
1268 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1269 		    (m->m_flags & (M_BCAST|M_MCAST)))
1270 			m_freem(m);
1271 		else
1272 			icmp6_error(m, ICMP6_PARAM_PROB,
1273 				    ICMP6_PARAMPROB_OPTION, off);
1274 		return (-1);
1275 	}
1276 
1277 	m_freem(m);		/* XXX: NOTREACHED */
1278 	return (-1);
1279 }
1280 
1281 /*
1282  * Create the "control" list for this pcb.
1283  * These functions will not modify mbuf chain at all.
1284  *
1285  * With KAME mbuf chain restriction:
1286  * The routine will be called from upper layer handlers like tcp6_input().
1287  * Thus the routine assumes that the caller (tcp6_input) have already
1288  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1289  * very first mbuf on the mbuf chain.
1290  *
1291  * ip6_savecontrol_v4 will handle those options that are possible to be
1292  * set on a v4-mapped socket.
1293  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1294  * options and handle the v6-only ones itself.
1295  */
1296 struct mbuf **
1297 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1298     int *v4only)
1299 {
1300 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1301 
1302 #ifdef SO_TIMESTAMP
1303 	if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1304 		struct timeval tv;
1305 
1306 		microtime(&tv);
1307 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1308 		    SCM_TIMESTAMP, SOL_SOCKET);
1309 		if (*mp)
1310 			mp = &(*mp)->m_next;
1311 	}
1312 #endif
1313 
1314 #define IS2292(inp, x, y)	(((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1315 	/* RFC 2292 sec. 5 */
1316 	if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1317 		struct in6_pktinfo pi6;
1318 
1319 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1320 #ifdef INET
1321 			struct ip *ip;
1322 
1323 			ip = mtod(m, struct ip *);
1324 			pi6.ipi6_addr.s6_addr32[0] = 0;
1325 			pi6.ipi6_addr.s6_addr32[1] = 0;
1326 			pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1327 			pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1328 #else
1329 			/* We won't hit this code */
1330 			bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1331 #endif
1332 		} else {
1333 			bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1334 			in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1335 		}
1336 		pi6.ipi6_ifindex =
1337 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1338 
1339 		*mp = sbcreatecontrol((caddr_t) &pi6,
1340 		    sizeof(struct in6_pktinfo),
1341 		    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1342 		if (*mp)
1343 			mp = &(*mp)->m_next;
1344 	}
1345 
1346 	if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1347 		int hlim;
1348 
1349 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1350 #ifdef INET
1351 			struct ip *ip;
1352 
1353 			ip = mtod(m, struct ip *);
1354 			hlim = ip->ip_ttl;
1355 #else
1356 			/* We won't hit this code */
1357 			hlim = 0;
1358 #endif
1359 		} else {
1360 			hlim = ip6->ip6_hlim & 0xff;
1361 		}
1362 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1363 		    IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1364 		    IPPROTO_IPV6);
1365 		if (*mp)
1366 			mp = &(*mp)->m_next;
1367 	}
1368 
1369 	if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1370 		int tclass;
1371 
1372 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1373 #ifdef INET
1374 			struct ip *ip;
1375 
1376 			ip = mtod(m, struct ip *);
1377 			tclass = ip->ip_tos;
1378 #else
1379 			/* We won't hit this code */
1380 			tclass = 0;
1381 #endif
1382 		} else {
1383 			u_int32_t flowinfo;
1384 
1385 			flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1386 			flowinfo >>= 20;
1387 			tclass = flowinfo & 0xff;
1388 		}
1389 		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1390 		    IPV6_TCLASS, IPPROTO_IPV6);
1391 		if (*mp)
1392 			mp = &(*mp)->m_next;
1393 	}
1394 
1395 	if (v4only != NULL) {
1396 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1397 			*v4only = 1;
1398 		} else {
1399 			*v4only = 0;
1400 		}
1401 	}
1402 
1403 	return (mp);
1404 }
1405 
1406 void
1407 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1408 {
1409 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1410 	int v4only = 0;
1411 
1412 	mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1413 	if (v4only)
1414 		return;
1415 
1416 	/*
1417 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1418 	 * privilege for the option (see ip6_ctloutput), but it might be too
1419 	 * strict, since there might be some hop-by-hop options which can be
1420 	 * returned to normal user.
1421 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1422 	 */
1423 	if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1424 		/*
1425 		 * Check if a hop-by-hop options header is contatined in the
1426 		 * received packet, and if so, store the options as ancillary
1427 		 * data. Note that a hop-by-hop options header must be
1428 		 * just after the IPv6 header, which is assured through the
1429 		 * IPv6 input processing.
1430 		 */
1431 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1432 			struct ip6_hbh *hbh;
1433 			int hbhlen = 0;
1434 #ifdef PULLDOWN_TEST
1435 			struct mbuf *ext;
1436 #endif
1437 
1438 #ifndef PULLDOWN_TEST
1439 			hbh = (struct ip6_hbh *)(ip6 + 1);
1440 			hbhlen = (hbh->ip6h_len + 1) << 3;
1441 #else
1442 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1443 			    ip6->ip6_nxt);
1444 			if (ext == NULL) {
1445 				IP6STAT_INC(ip6s_tooshort);
1446 				return;
1447 			}
1448 			hbh = mtod(ext, struct ip6_hbh *);
1449 			hbhlen = (hbh->ip6h_len + 1) << 3;
1450 			if (hbhlen != ext->m_len) {
1451 				m_freem(ext);
1452 				IP6STAT_INC(ip6s_tooshort);
1453 				return;
1454 			}
1455 #endif
1456 
1457 			/*
1458 			 * XXX: We copy the whole header even if a
1459 			 * jumbo payload option is included, the option which
1460 			 * is to be removed before returning according to
1461 			 * RFC2292.
1462 			 * Note: this constraint is removed in RFC3542
1463 			 */
1464 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1465 			    IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1466 			    IPPROTO_IPV6);
1467 			if (*mp)
1468 				mp = &(*mp)->m_next;
1469 #ifdef PULLDOWN_TEST
1470 			m_freem(ext);
1471 #endif
1472 		}
1473 	}
1474 
1475 	if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1476 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1477 
1478 		/*
1479 		 * Search for destination options headers or routing
1480 		 * header(s) through the header chain, and stores each
1481 		 * header as ancillary data.
1482 		 * Note that the order of the headers remains in
1483 		 * the chain of ancillary data.
1484 		 */
1485 		while (1) {	/* is explicit loop prevention necessary? */
1486 			struct ip6_ext *ip6e = NULL;
1487 			int elen;
1488 #ifdef PULLDOWN_TEST
1489 			struct mbuf *ext = NULL;
1490 #endif
1491 
1492 			/*
1493 			 * if it is not an extension header, don't try to
1494 			 * pull it from the chain.
1495 			 */
1496 			switch (nxt) {
1497 			case IPPROTO_DSTOPTS:
1498 			case IPPROTO_ROUTING:
1499 			case IPPROTO_HOPOPTS:
1500 			case IPPROTO_AH: /* is it possible? */
1501 				break;
1502 			default:
1503 				goto loopend;
1504 			}
1505 
1506 #ifndef PULLDOWN_TEST
1507 			if (off + sizeof(*ip6e) > m->m_len)
1508 				goto loopend;
1509 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1510 			if (nxt == IPPROTO_AH)
1511 				elen = (ip6e->ip6e_len + 2) << 2;
1512 			else
1513 				elen = (ip6e->ip6e_len + 1) << 3;
1514 			if (off + elen > m->m_len)
1515 				goto loopend;
1516 #else
1517 			ext = ip6_pullexthdr(m, off, nxt);
1518 			if (ext == NULL) {
1519 				IP6STAT_INC(ip6s_tooshort);
1520 				return;
1521 			}
1522 			ip6e = mtod(ext, struct ip6_ext *);
1523 			if (nxt == IPPROTO_AH)
1524 				elen = (ip6e->ip6e_len + 2) << 2;
1525 			else
1526 				elen = (ip6e->ip6e_len + 1) << 3;
1527 			if (elen != ext->m_len) {
1528 				m_freem(ext);
1529 				IP6STAT_INC(ip6s_tooshort);
1530 				return;
1531 			}
1532 #endif
1533 
1534 			switch (nxt) {
1535 			case IPPROTO_DSTOPTS:
1536 				if (!(in6p->inp_flags & IN6P_DSTOPTS))
1537 					break;
1538 
1539 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1540 				    IS2292(in6p,
1541 					IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1542 				    IPPROTO_IPV6);
1543 				if (*mp)
1544 					mp = &(*mp)->m_next;
1545 				break;
1546 			case IPPROTO_ROUTING:
1547 				if (!(in6p->inp_flags & IN6P_RTHDR))
1548 					break;
1549 
1550 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1551 				    IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1552 				    IPPROTO_IPV6);
1553 				if (*mp)
1554 					mp = &(*mp)->m_next;
1555 				break;
1556 			case IPPROTO_HOPOPTS:
1557 			case IPPROTO_AH: /* is it possible? */
1558 				break;
1559 
1560 			default:
1561 				/*
1562 				 * other cases have been filtered in the above.
1563 				 * none will visit this case.  here we supply
1564 				 * the code just in case (nxt overwritten or
1565 				 * other cases).
1566 				 */
1567 #ifdef PULLDOWN_TEST
1568 				m_freem(ext);
1569 #endif
1570 				goto loopend;
1571 
1572 			}
1573 
1574 			/* proceed with the next header. */
1575 			off += elen;
1576 			nxt = ip6e->ip6e_nxt;
1577 			ip6e = NULL;
1578 #ifdef PULLDOWN_TEST
1579 			m_freem(ext);
1580 			ext = NULL;
1581 #endif
1582 		}
1583 	  loopend:
1584 		;
1585 	}
1586 }
1587 #undef IS2292
1588 
1589 void
1590 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
1591 {
1592 	struct socket *so;
1593 	struct mbuf *m_mtu;
1594 	struct ip6_mtuinfo mtuctl;
1595 
1596 	so =  in6p->inp_socket;
1597 
1598 	if (mtu == NULL)
1599 		return;
1600 
1601 #ifdef DIAGNOSTIC
1602 	if (so == NULL)		/* I believe this is impossible */
1603 		panic("ip6_notify_pmtu: socket is NULL");
1604 #endif
1605 
1606 	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1607 	mtuctl.ip6m_mtu = *mtu;
1608 	mtuctl.ip6m_addr = *dst;
1609 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1610 		return;
1611 
1612 	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1613 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1614 		return;
1615 
1616 	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1617 	    == 0) {
1618 		m_freem(m_mtu);
1619 		/* XXX: should count statistics */
1620 	} else
1621 		sorwakeup(so);
1622 
1623 	return;
1624 }
1625 
1626 #ifdef PULLDOWN_TEST
1627 /*
1628  * pull single extension header from mbuf chain.  returns single mbuf that
1629  * contains the result, or NULL on error.
1630  */
1631 static struct mbuf *
1632 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1633 {
1634 	struct ip6_ext ip6e;
1635 	size_t elen;
1636 	struct mbuf *n;
1637 
1638 #ifdef DIAGNOSTIC
1639 	switch (nxt) {
1640 	case IPPROTO_DSTOPTS:
1641 	case IPPROTO_ROUTING:
1642 	case IPPROTO_HOPOPTS:
1643 	case IPPROTO_AH: /* is it possible? */
1644 		break;
1645 	default:
1646 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1647 	}
1648 #endif
1649 
1650 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1651 	if (nxt == IPPROTO_AH)
1652 		elen = (ip6e.ip6e_len + 2) << 2;
1653 	else
1654 		elen = (ip6e.ip6e_len + 1) << 3;
1655 
1656 	if (elen > MLEN)
1657 		n = m_getcl(M_NOWAIT, MT_DATA, 0);
1658 	else
1659 		n = m_get(M_NOWAIT, MT_DATA);
1660 	if (n == NULL)
1661 		return NULL;
1662 
1663 	m_copydata(m, off, elen, mtod(n, caddr_t));
1664 	n->m_len = elen;
1665 	return n;
1666 }
1667 #endif
1668 
1669 /*
1670  * Get pointer to the previous header followed by the header
1671  * currently processed.
1672  * XXX: This function supposes that
1673  *	M includes all headers,
1674  *	the next header field and the header length field of each header
1675  *	are valid, and
1676  *	the sum of each header length equals to OFF.
1677  * Because of these assumptions, this function must be called very
1678  * carefully. Moreover, it will not be used in the near future when
1679  * we develop `neater' mechanism to process extension headers.
1680  */
1681 char *
1682 ip6_get_prevhdr(struct mbuf *m, int off)
1683 {
1684 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1685 
1686 	if (off == sizeof(struct ip6_hdr))
1687 		return (&ip6->ip6_nxt);
1688 	else {
1689 		int len, nxt;
1690 		struct ip6_ext *ip6e = NULL;
1691 
1692 		nxt = ip6->ip6_nxt;
1693 		len = sizeof(struct ip6_hdr);
1694 		while (len < off) {
1695 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1696 
1697 			switch (nxt) {
1698 			case IPPROTO_FRAGMENT:
1699 				len += sizeof(struct ip6_frag);
1700 				break;
1701 			case IPPROTO_AH:
1702 				len += (ip6e->ip6e_len + 2) << 2;
1703 				break;
1704 			default:
1705 				len += (ip6e->ip6e_len + 1) << 3;
1706 				break;
1707 			}
1708 			nxt = ip6e->ip6e_nxt;
1709 		}
1710 		if (ip6e)
1711 			return (&ip6e->ip6e_nxt);
1712 		else
1713 			return NULL;
1714 	}
1715 }
1716 
1717 /*
1718  * get next header offset.  m will be retained.
1719  */
1720 int
1721 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1722 {
1723 	struct ip6_hdr ip6;
1724 	struct ip6_ext ip6e;
1725 	struct ip6_frag fh;
1726 
1727 	/* just in case */
1728 	if (m == NULL)
1729 		panic("ip6_nexthdr: m == NULL");
1730 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1731 		return -1;
1732 
1733 	switch (proto) {
1734 	case IPPROTO_IPV6:
1735 		if (m->m_pkthdr.len < off + sizeof(ip6))
1736 			return -1;
1737 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1738 		if (nxtp)
1739 			*nxtp = ip6.ip6_nxt;
1740 		off += sizeof(ip6);
1741 		return off;
1742 
1743 	case IPPROTO_FRAGMENT:
1744 		/*
1745 		 * terminate parsing if it is not the first fragment,
1746 		 * it does not make sense to parse through it.
1747 		 */
1748 		if (m->m_pkthdr.len < off + sizeof(fh))
1749 			return -1;
1750 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1751 		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1752 		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1753 			return -1;
1754 		if (nxtp)
1755 			*nxtp = fh.ip6f_nxt;
1756 		off += sizeof(struct ip6_frag);
1757 		return off;
1758 
1759 	case IPPROTO_AH:
1760 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1761 			return -1;
1762 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1763 		if (nxtp)
1764 			*nxtp = ip6e.ip6e_nxt;
1765 		off += (ip6e.ip6e_len + 2) << 2;
1766 		return off;
1767 
1768 	case IPPROTO_HOPOPTS:
1769 	case IPPROTO_ROUTING:
1770 	case IPPROTO_DSTOPTS:
1771 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1772 			return -1;
1773 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1774 		if (nxtp)
1775 			*nxtp = ip6e.ip6e_nxt;
1776 		off += (ip6e.ip6e_len + 1) << 3;
1777 		return off;
1778 
1779 	case IPPROTO_NONE:
1780 	case IPPROTO_ESP:
1781 	case IPPROTO_IPCOMP:
1782 		/* give up */
1783 		return -1;
1784 
1785 	default:
1786 		return -1;
1787 	}
1788 
1789 	return -1;
1790 }
1791 
1792 /*
1793  * get offset for the last header in the chain.  m will be kept untainted.
1794  */
1795 int
1796 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1797 {
1798 	int newoff;
1799 	int nxt;
1800 
1801 	if (!nxtp) {
1802 		nxt = -1;
1803 		nxtp = &nxt;
1804 	}
1805 	while (1) {
1806 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1807 		if (newoff < 0)
1808 			return off;
1809 		else if (newoff < off)
1810 			return -1;	/* invalid */
1811 		else if (newoff == off)
1812 			return newoff;
1813 
1814 		off = newoff;
1815 		proto = *nxtp;
1816 	}
1817 }
1818 
1819 static struct ip6aux *
1820 ip6_addaux(struct mbuf *m)
1821 {
1822 	struct m_tag *mtag;
1823 
1824 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1825 	if (!mtag) {
1826 		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1827 		    M_NOWAIT);
1828 		if (mtag) {
1829 			m_tag_prepend(m, mtag);
1830 			bzero(mtag + 1, sizeof(struct ip6aux));
1831 		}
1832 	}
1833 	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1834 }
1835 
1836 static struct ip6aux *
1837 ip6_findaux(struct mbuf *m)
1838 {
1839 	struct m_tag *mtag;
1840 
1841 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1842 	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1843 }
1844 
1845 static void
1846 ip6_delaux(struct mbuf *m)
1847 {
1848 	struct m_tag *mtag;
1849 
1850 	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1851 	if (mtag)
1852 		m_tag_delete(m, mtag);
1853 }
1854 
1855 /*
1856  * System control for IP6
1857  */
1858 
1859 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1860 	0,		0,		0,		0,
1861 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1862 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1863 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1864 	0,		0,		0,		0,
1865 	ENOPROTOOPT
1866 };
1867