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