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