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