xref: /freebsd/sys/netinet6/ip6_input.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1988, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
65  */
66 
67 #include "opt_ipsec.h"
68 #include "opt_ip6fw.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/mbuf.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 #include <sys/proc.h>
82 
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/if_dl.h>
86 #include <net/route.h>
87 #include <net/netisr.h>
88 #include <net/intrq.h>
89 
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_icmp.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/icmp6.h>
99 #include <netinet6/in6_ifattach.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/in6_prefix.h>
102 
103 #ifdef IPV6FIREWALL
104 #include <netinet6/ip6_fw.h>
105 #endif
106 
107 #ifdef ALTQ
108 #include <netinet/altq_cdnr.h>
109 #endif
110 
111 #include <netinet6/ip6protosw.h>
112 
113 /* we need it for NLOOP. */
114 #include "loop.h"
115 
116 #include "faith.h"
117 #include "gif.h"
118 
119 #include <net/net_osdep.h>
120 
121 extern struct	domain inet6domain;
122 extern struct	ip6protosw inet6sw[];
123 
124 u_char	ip6_protox[IPPROTO_MAX];
125 static int	ip6qmaxlen = IFQ_MAXLEN;
126 struct	in6_ifaddr *in6_ifaddr;
127 
128 int	ip6_forward_srcrt;			/* XXX */
129 int	ip6_sourcecheck;			/* XXX */
130 int	ip6_sourcecheck_interval;		/* XXX */
131 
132 const int	int6intrq_present = 1;
133 
134 #ifdef IPV6FIREWALL
135 /* firewall hooks */
136 ip6_fw_chk_t	*ip6_fw_chk_ptr;
137 ip6_fw_ctl_t	*ip6_fw_ctl_ptr;
138 #endif
139 
140 struct	ip6stat ip6stat;
141 
142 static void	ip6_init2 __P((void *));
143 
144 static int	ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
145 
146 #if defined(PTR)
147 extern int	ip6_protocol_tr;
148 
149 int	ptr_in6		__P((struct mbuf *, struct mbuf **));
150 extern void	ip_forward __P((struct mbuf *, int));
151 #endif
152 
153 /*
154  * IP6 initialization: fill in IP6 protocol switch table.
155  * All protocols not implemented in kernel go to raw IP6 protocol handler.
156  */
157 void
158 ip6_init()
159 {
160 	register struct ip6protosw *pr;
161 	register int i;
162 	struct timeval tv;
163 
164 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
165 	if (pr == 0)
166 		panic("ip6_init");
167 	for (i = 0; i < IPPROTO_MAX; i++)
168 		ip6_protox[i] = pr - inet6sw;
169 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
170 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
171 		if (pr->pr_domain->dom_family == PF_INET6 &&
172 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
173 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
174 	ip6intrq.ifq_maxlen = ip6qmaxlen;
175 	register_netisr(NETISR_IPV6, ip6intr);
176 	nd6_init();
177 	frag6_init();
178 #ifdef IPV6FIREWALL
179 	ip6_fw_init();
180 #endif
181 	/*
182 	 * in many cases, random() here does NOT return random number
183 	 * as initialization during bootstrap time occur in fixed order.
184 	 */
185 	microtime(&tv);
186 	ip6_flow_seq = random() ^ tv.tv_usec;
187 }
188 
189 static void
190 ip6_init2(dummy)
191 	void *dummy;
192 {
193 	int i;
194 	int ret;
195 
196 	/* get EUI64 from somewhere */
197 	ret = in6_ifattach_getifid(NULL);
198 
199 	/*
200 	 * to route local address of p2p link to loopback,
201 	 * assign loopback address first.
202 	 */
203 	for (i = 0; i < NLOOP; i++)
204 		in6_ifattach(&loif[i], IN6_IFT_LOOP, NULL, 0);
205 
206 	/* attach pseudo interfaces */
207 	if (ret == 0)
208 		in6_ifattach_p2p();
209 
210 	/* nd6_timer_init */
211 	timeout(nd6_timer, (caddr_t)0, hz);
212 	/* router renumbering prefix list maintenance */
213 	timeout(in6_rr_timer, (caddr_t)0, hz);
214 }
215 
216 /* cheat */
217 /* This must be after route_init(), which is now SI_ORDER_THIRD */
218 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
219 
220 /*
221  * IP6 input interrupt handling. Just pass the packet to ip6_input.
222  */
223 void
224 ip6intr()
225 {
226 	int s;
227 	struct mbuf *m;
228 
229 	for (;;) {
230 		s = splimp();
231 		IF_DEQUEUE(&ip6intrq, m);
232 		splx(s);
233 		if (m == 0)
234 			return;
235 		ip6_input(m);
236 	}
237 }
238 
239 extern struct	route_in6 ip6_forward_rt;
240 
241 void
242 ip6_input(m)
243 	struct mbuf *m;
244 {
245 	struct ip6_hdr *ip6;
246 	int off = sizeof(struct ip6_hdr), nest;
247 	u_int32_t plen;
248 	u_int32_t rtalert = ~0;
249 	int nxt, ours = 0;
250 	struct ifnet *deliverifp = NULL;
251 
252 #ifdef IPSEC
253 	/*
254 	 * should the inner packet be considered authentic?
255 	 * see comment in ah4_input().
256 	 */
257 	if (m) {
258 		m->m_flags &= ~M_AUTHIPHDR;
259 		m->m_flags &= ~M_AUTHIPDGM;
260 	}
261 #endif
262 
263 	/*
264 	 * mbuf statistics by kazu
265 	 */
266 	if (m->m_flags & M_EXT) {
267 		if (m->m_next)
268 			ip6stat.ip6s_mext2m++;
269 		else
270 			ip6stat.ip6s_mext1++;
271 	} else {
272 		if (m->m_next) {
273 			if (m->m_flags & M_LOOP) {
274 				ip6stat.ip6s_m2m[loif[0].if_index]++;	/*XXX*/
275 			} else if (m->m_pkthdr.rcvif->if_index <= 31)
276 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
277 			else
278 				ip6stat.ip6s_m2m[0]++;
279 		} else
280 			ip6stat.ip6s_m1++;
281 	}
282 
283 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
284 	ip6stat.ip6s_total++;
285 
286 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
287 
288 	if (m->m_len < sizeof(struct ip6_hdr)) {
289 		struct ifnet *inifp;
290 		inifp = m->m_pkthdr.rcvif;
291 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
292 			ip6stat.ip6s_toosmall++;
293 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
294 			return;
295 		}
296 	}
297 
298 	ip6 = mtod(m, struct ip6_hdr *);
299 
300 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
301 		ip6stat.ip6s_badvers++;
302 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
303 		goto bad;
304 	}
305 
306 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
307 
308 #ifdef IPV6FIREWALL
309 	/*
310 	 * Check with the firewall...
311 	 */
312 	if (ip6_fw_chk_ptr) {
313 		u_short port = 0;
314 		/* If ipfw says divert, we have to just drop packet */
315 		/* use port as a dummy argument */
316 		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
317 			m_freem(m);
318 			m = NULL;
319 		}
320 		if (!m)
321 			return;
322 	}
323 #endif
324 
325 #ifdef ALTQ
326 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
327 		/* packet is dropped by traffic conditioner */
328 		return;
329 	}
330 #endif
331 
332 	/*
333 	 * Scope check
334 	 */
335 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
336 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
337 		ip6stat.ip6s_badscope++;
338 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
339 		goto bad;
340 	}
341 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
342 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
343 		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
344 			ip6stat.ip6s_badscope++;
345 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
346 			goto bad;
347 		}
348 	}
349 
350 	if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
351 		if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
352 			ours = 1;
353 			deliverifp = m->m_pkthdr.rcvif;
354 			goto hbhcheck;
355 		}
356 	} else {
357 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
358 			ip6->ip6_src.s6_addr16[1]
359 				= htons(m->m_pkthdr.rcvif->if_index);
360 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
361 			ip6->ip6_dst.s6_addr16[1]
362 				= htons(m->m_pkthdr.rcvif->if_index);
363 	}
364 
365 #if defined(PTR)
366 	/*
367 	 *
368 	 */
369 	if (ip6_protocol_tr)
370 	{
371 	    struct mbuf *m1 = NULL;
372 
373 	    switch (ptr_in6(m, &m1))
374 	    {
375 	      case IPPROTO_IP:					goto mcastcheck;
376 	      case IPPROTO_IPV4:	ip_forward(m1, 0);	break;
377 	      case IPPROTO_IPV6:	ip6_forward(m1, 0);	break;
378 	      case IPPROTO_MAX:			/* discard this packet	*/
379 	      default:
380 	    }
381 
382 	    if (m != m1)
383 		m_freem(m);
384 
385 	    return;
386 	}
387 
388   mcastcheck:
389 #endif
390 
391 	/*
392 	 * Multicast check
393 	 */
394 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
395 	  	struct	in6_multi *in6m = 0;
396 
397 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
398 		/*
399 		 * See if we belong to the destination multicast group on the
400 		 * arrival interface.
401 		 */
402 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
403 		if (in6m)
404 			ours = 1;
405 		else if (!ip6_mrouter) {
406 			ip6stat.ip6s_notmember++;
407 			ip6stat.ip6s_cantforward++;
408 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
409 			goto bad;
410 		}
411 		deliverifp = m->m_pkthdr.rcvif;
412 		goto hbhcheck;
413 	}
414 
415 	/*
416 	 *  Unicast check
417 	 */
418 	if (ip6_forward_rt.ro_rt == 0 ||
419 	    !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
420 				&ip6_forward_rt.ro_dst.sin6_addr)) {
421 		if (ip6_forward_rt.ro_rt) {
422 			RTFREE(ip6_forward_rt.ro_rt);
423 			ip6_forward_rt.ro_rt = 0;
424 		}
425 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
426 		ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
427 		ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
428 		ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
429 
430 		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
431 	}
432 
433 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
434 
435 	/*
436 	 * Accept the packet if the forwarding interface to the destination
437 	 * according to the routing table is the loopback interface,
438 	 * unless the associated route has a gateway.
439 	 * Note that this approach causes to accept a packet if there is a
440 	 * route to the loopback interface for the destination of the packet.
441 	 * But we think it's even useful in some situations, e.g. when using
442 	 * a special daemon which wants to intercept the packet.
443 	 */
444 	if (ip6_forward_rt.ro_rt &&
445 	    (ip6_forward_rt.ro_rt->rt_flags &
446 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
447 	    /*
448 	     * The comparison of the destination and the key of the rtentry
449 	     * has already done through looking up the routing table,
450 	     * so no need to do such a comparison here again.
451 	     */
452 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
453 		struct in6_ifaddr *ia6 =
454 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
455 		/* packet to tentative address must not be received */
456 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
457 			m->m_flags |= M_ANYCAST6;
458 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
459 			/* this interface is ready */
460 			ours = 1;
461 			deliverifp = ia6->ia_ifp;	/* correct? */
462 			goto hbhcheck;
463 		} else {
464 			/* this interface is not ready, fall through */
465 		}
466 	}
467 
468 	/*
469 	 * FAITH(Firewall Aided Internet Translator)
470 	 */
471 #if defined(NFAITH) && 0 < NFAITH
472 	if (ip6_keepfaith) {
473 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
474 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
475 			/* XXX do we need more sanity checks? */
476 			ours = 1;
477 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
478 			goto hbhcheck;
479 		}
480 	}
481 #endif
482 
483 	/*
484 	 * Now there is no reason to process the packet if it's not our own
485 	 * and we're not a router.
486 	 */
487 	if (!ip6_forwarding) {
488 		ip6stat.ip6s_cantforward++;
489 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
490 		goto bad;
491 	}
492 
493   hbhcheck:
494 	/*
495 	 * Process Hop-by-Hop options header if it's contained.
496 	 * m may be modified in ip6_hopopts_input().
497 	 * If a JumboPayload option is included, plen will also be modified.
498 	 */
499 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
500 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
501 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
502 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
503 			return;	/* m have already been freed */
504 		}
505 		/* adjust pointer */
506 		ip6 = mtod(m, struct ip6_hdr *);
507 		nxt = ((struct ip6_hbh *)(ip6 + 1))->ip6h_nxt;
508 
509 		/*
510 		 * accept the packet if a router alert option is included
511 		 * and we act as an IPv6 router.
512 		 */
513 		if (rtalert != ~0 && ip6_forwarding)
514 			ours = 1;
515 	} else
516 		nxt = ip6->ip6_nxt;
517 
518 	/*
519 	 * Check that the amount of data in the buffers
520 	 * is as at least much as the IPv6 header would have us expect.
521 	 * Trim mbufs if longer than we expect.
522 	 * Drop packet if shorter than we expect.
523 	 */
524 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
525 		ip6stat.ip6s_tooshort++;
526 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
527 		goto bad;
528 	}
529 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
530 		if (m->m_len == m->m_pkthdr.len) {
531 			m->m_len = sizeof(struct ip6_hdr) + plen;
532 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
533 		} else
534 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
535 	}
536 
537 	/*
538 	 * Forward if desirable.
539 	 */
540 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
541 		/*
542 		 * If we are acting as a multicast router, all
543 		 * incoming multicast packets are passed to the
544 		 * kernel-level multicast forwarding function.
545 		 * The packet is returned (relatively) intact; if
546 		 * ip6_mforward() returns a non-zero value, the packet
547 		 * must be discarded, else it may be accepted below.
548 		 */
549 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
550 			ip6stat.ip6s_cantforward++;
551 			m_freem(m);
552 			return;
553 		}
554 		if (!ours) {
555 			m_freem(m);
556 			return;
557 		}
558 	} else if (!ours) {
559 		ip6_forward(m, 0);
560 		return;
561 	}
562 
563 	/*
564 	 * Tell launch routine the next header
565 	 */
566 #if defined(__NetBSD__) && defined(IFA_STATS)
567 	if (IFA_STATS && deliverifp != NULL) {
568 		struct in6_ifaddr *ia6;
569 		ip6 = mtod(m, struct ip6_hdr *);
570 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
571 		if (ia6)
572 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
573 	}
574 #endif
575 	ip6stat.ip6s_delivered++;
576 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
577 	nest = 0;
578 	while (nxt != IPPROTO_DONE) {
579 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
580 			ip6stat.ip6s_toomanyhdr++;
581 			goto bad;
582 		}
583 
584 		/*
585 		 * protection against faulty packet - there should be
586 		 * more sanity checks in header chain processing.
587 		 */
588 		if (m->m_pkthdr.len < off) {
589 			ip6stat.ip6s_tooshort++;
590 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
591 			goto bad;
592 		}
593 
594 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
595 	}
596 	return;
597  bad:
598 	m_freem(m);
599 }
600 
601 /*
602  * Hop-by-Hop options header processing. If a valid jumbo payload option is
603  * included, the real payload length will be stored in plenp.
604  */
605 static int
606 ip6_hopopts_input(plenp, rtalertp, mp, offp)
607 	u_int32_t *plenp;
608 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
609 	struct mbuf **mp;
610 	int *offp;
611 {
612 	register struct mbuf *m = *mp;
613 	int off = *offp, hbhlen;
614 	struct ip6_hbh *hbh;
615 	u_int8_t *opt;
616 
617 	/* validation of the length of the header */
618 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
619 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
620 	hbhlen = (hbh->ip6h_len + 1) << 3;
621 
622 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
623 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
624 	off += hbhlen;
625 	hbhlen -= sizeof(struct ip6_hbh);
626 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
627 
628 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
629 				hbhlen, rtalertp, plenp) < 0)
630 		return(-1);
631 
632 	*offp = off;
633 	*mp = m;
634 	return(0);
635 }
636 
637 /*
638  * Search header for all Hop-by-hop options and process each option.
639  * This function is separate from ip6_hopopts_input() in order to
640  * handle a case where the sending node itself process its hop-by-hop
641  * options header. In such a case, the function is called from ip6_output().
642  */
643 int
644 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
645 	struct mbuf *m;
646 	u_int8_t *opthead;
647 	int hbhlen;
648 	u_int32_t *rtalertp;
649 	u_int32_t *plenp;
650 {
651 	struct ip6_hdr *ip6;
652 	int optlen = 0;
653 	u_int8_t *opt = opthead;
654 	u_int16_t rtalert_val;
655 
656 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
657 		switch(*opt) {
658 		 case IP6OPT_PAD1:
659 			 optlen = 1;
660 			 break;
661 		 case IP6OPT_PADN:
662 			 if (hbhlen < IP6OPT_MINLEN) {
663 				 ip6stat.ip6s_toosmall++;
664 				 goto bad;
665 			 }
666 			 optlen = *(opt + 1) + 2;
667 			 break;
668 		 case IP6OPT_RTALERT:
669 			 /* XXX may need check for alignment */
670 			 if (hbhlen < IP6OPT_RTALERT_LEN) {
671 				 ip6stat.ip6s_toosmall++;
672 				 goto bad;
673 			 }
674 			 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
675 				  /* XXX: should we discard the packet? */
676 				 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
677 				     *(opt + 1));
678 			 optlen = IP6OPT_RTALERT_LEN;
679 			 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
680 			 *rtalertp = ntohs(rtalert_val);
681 			 break;
682 		 case IP6OPT_JUMBO:
683 			 /* XXX may need check for alignment */
684 			 if (hbhlen < IP6OPT_JUMBO_LEN) {
685 				 ip6stat.ip6s_toosmall++;
686 				 goto bad;
687 			 }
688 			 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
689 				  /* XXX: should we discard the packet? */
690 				 log(LOG_ERR, "length of jumbopayload opt "
691 				     "is inconsistent(%d)",
692 				     *(opt + 1));
693 			 optlen = IP6OPT_JUMBO_LEN;
694 
695 			 bcopy(opt + 2, plenp, sizeof(*plenp));
696 			 *plenp = htonl(*plenp);
697 			 if (*plenp <= IPV6_MAXPACKET) {
698 				 /*
699 				  * jumbo payload length must be larger
700 				  * than 65535
701 				  */
702 				 ip6stat.ip6s_badoptions++;
703 				 icmp6_error(m, ICMP6_PARAM_PROB,
704 					     ICMP6_PARAMPROB_HEADER,
705 					     sizeof(struct ip6_hdr) +
706 					     sizeof(struct ip6_hbh) +
707 					     opt + 2 - opthead);
708 				 return(-1);
709 			 }
710 
711 			 ip6 = mtod(m, struct ip6_hdr *);
712 			 if (ip6->ip6_plen) {
713 				 /*
714 				  * IPv6 packets that have non 0 payload length
715 				  * must not contain a jumbo paylod option.
716 				  */
717 				 ip6stat.ip6s_badoptions++;
718 				 icmp6_error(m, ICMP6_PARAM_PROB,
719 					     ICMP6_PARAMPROB_HEADER,
720 					     sizeof(struct ip6_hdr) +
721 					     sizeof(struct ip6_hbh) +
722 					     opt - opthead);
723 				 return(-1);
724 			 }
725 			 break;
726 		 default:		/* unknown option */
727 			 if (hbhlen < IP6OPT_MINLEN) {
728 				 ip6stat.ip6s_toosmall++;
729 				 goto bad;
730 			 }
731 			 if ((optlen = ip6_unknown_opt(opt, m,
732 						       sizeof(struct ip6_hdr) +
733 						       sizeof(struct ip6_hbh) +
734 						       opt - opthead)) == -1)
735 				 return(-1);
736 			 optlen += 2;
737 			 break;
738 		}
739 	}
740 
741 	return(0);
742 
743   bad:
744 	m_freem(m);
745 	return(-1);
746 }
747 
748 /*
749  * Unknown option processing.
750  * The third argument `off' is the offset from the IPv6 header to the option,
751  * which is necessary if the IPv6 header the and option header and IPv6 header
752  * is not continuous in order to return an ICMPv6 error.
753  */
754 int
755 ip6_unknown_opt(optp, m, off)
756 	u_int8_t *optp;
757 	struct mbuf *m;
758 	int off;
759 {
760 	struct ip6_hdr *ip6;
761 
762 	switch(IP6OPT_TYPE(*optp)) {
763 	 case IP6OPT_TYPE_SKIP: /* ignore the option */
764 		 return((int)*(optp + 1));
765 	 case IP6OPT_TYPE_DISCARD:	/* silently discard */
766 		 m_freem(m);
767 		 return(-1);
768 	 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
769 		 ip6stat.ip6s_badoptions++;
770 		 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
771 		 return(-1);
772 	 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
773 		 ip6stat.ip6s_badoptions++;
774 		 ip6 = mtod(m, struct ip6_hdr *);
775 		 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
776 		     (m->m_flags & (M_BCAST|M_MCAST)))
777 			 m_freem(m);
778 		 else
779 			 icmp6_error(m, ICMP6_PARAM_PROB,
780 				     ICMP6_PARAMPROB_OPTION, off);
781 		 return(-1);
782 	}
783 
784 	m_freem(m);		/* XXX: NOTREACHED */
785 	return(-1);
786 }
787 
788 /*
789  * Create the "control" list for this pcb
790  */
791 void
792 ip6_savecontrol(in6p, mp, ip6, m)
793 	register struct inpcb *in6p;
794 	register struct mbuf **mp;
795 	register struct ip6_hdr *ip6;
796 	register struct mbuf *m;
797 {
798 	struct proc *p = curproc;	/* XXX */
799 	int privileged;
800 
801 	privileged = 0;
802 	if (p && !suser(p))
803 		privileged++;
804 
805 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
806 		struct timeval tv;
807 
808 		microtime(&tv);
809 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
810 			SCM_TIMESTAMP, SOL_SOCKET);
811 		if (*mp)
812 			mp = &(*mp)->m_next;
813 	}
814 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
815 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
816 			sizeof(struct in6_addr), IPV6_RECVDSTADDR,
817 			IPPROTO_IPV6);
818 		if (*mp)
819 			mp = &(*mp)->m_next;
820 	}
821 
822 	/* RFC 2292 sec. 5 */
823 	if (in6p->in6p_flags & IN6P_PKTINFO) {
824 		struct in6_pktinfo pi6;
825 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
826 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
827 			pi6.ipi6_addr.s6_addr16[1] = 0;
828 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
829 					? m->m_pkthdr.rcvif->if_index
830 					: 0;
831 		*mp = sbcreatecontrol((caddr_t) &pi6,
832 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
833 			IPPROTO_IPV6);
834 		if (*mp)
835 			mp = &(*mp)->m_next;
836 	}
837 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
838 		int hlim = ip6->ip6_hlim & 0xff;
839 		*mp = sbcreatecontrol((caddr_t) &hlim,
840 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
841 		if (*mp)
842 			mp = &(*mp)->m_next;
843 	}
844 	/* IN6P_NEXTHOP - for outgoing packet only */
845 
846 	/*
847 	 * IPV6_HOPOPTS socket option. We require super-user privilege
848 	 * for the option, but it might be too strict, since there might
849 	 * be some hop-by-hop options which can be returned to normal user.
850 	 * See RFC 2292 section 6.
851 	 */
852 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
853 		/*
854 		 * Check if a hop-by-hop options header is contatined in the
855 		 * received packet, and if so, store the options as ancillary
856 		 * data. Note that a hop-by-hop options header must be
857 		 * just after the IPv6 header, which fact is assured through
858 		 * the IPv6 input processing.
859 		 */
860 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
861 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
862 			struct ip6_hbh *hbh = (struct ip6_hbh *)(ip6 + 1);
863 
864 			/*
865 			 * XXX: We copy whole the header even if a jumbo
866 			 * payload option is included, which option is to
867 			 * be removed before returning in the RFC 2292.
868 			 * But it's too painful operation...
869 			 */
870 			*mp = sbcreatecontrol((caddr_t)hbh,
871 					      (hbh->ip6h_len + 1) << 3,
872 					      IPV6_HOPOPTS, IPPROTO_IPV6);
873 			if (*mp)
874 				mp = &(*mp)->m_next;
875 		}
876 	}
877 
878 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
879 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
880 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
881 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
882 
883 		/*
884 		 * Search for destination options headers or routing
885 		 * header(s) through the header chain, and stores each
886 		 * header as ancillary data.
887 		 * Note that the order of the headers remains in
888 		 * the chain of ancillary data.
889 		 */
890 		while(1) {	/* is explicit loop prevention necessary? */
891 			struct ip6_ext *ip6e =
892 				(struct ip6_ext *)(mtod(m, caddr_t) + off);
893 
894 			switch(nxt) {
895 		         case IPPROTO_DSTOPTS:
896 				 if (!in6p->in6p_flags & IN6P_DSTOPTS)
897 					 break;
898 
899 				 /*
900 				  * We also require super-user privilege for
901 				  * the option.
902 				  * See the comments on IN6_HOPOPTS.
903 				  */
904 				 if (!privileged)
905 					 break;
906 
907 				 *mp = sbcreatecontrol((caddr_t)ip6e,
908 						       (ip6e->ip6e_len + 1) << 3,
909 						       IPV6_DSTOPTS,
910 						       IPPROTO_IPV6);
911 				 if (*mp)
912 					 mp = &(*mp)->m_next;
913 				 break;
914 
915 			 case IPPROTO_ROUTING:
916 				 if (!in6p->in6p_flags & IN6P_RTHDR)
917 					 break;
918 
919 				 *mp = sbcreatecontrol((caddr_t)ip6e,
920 						       (ip6e->ip6e_len + 1) << 3,
921 						       IPV6_RTHDR,
922 						       IPPROTO_IPV6);
923 				 if (*mp)
924 					 mp = &(*mp)->m_next;
925 				 break;
926 
927 			 case IPPROTO_UDP:
928 			 case IPPROTO_TCP:
929 			 case IPPROTO_ICMPV6:
930 			 default:
931 				 /*
932 				  * stop search if we encounter an upper
933 				  * layer protocol headers.
934 				  */
935 				 goto loopend;
936 
937 			 case IPPROTO_HOPOPTS:
938 			 case IPPROTO_AH: /* is it possible? */
939 				 break;
940 			}
941 
942 			/* proceed with the next header. */
943 			if (nxt == IPPROTO_AH)
944 				off += (ip6e->ip6e_len + 2) << 2;
945 			else
946 				off += (ip6e->ip6e_len + 1) << 3;
947 			nxt = ip6e->ip6e_nxt;
948 		}
949 	  loopend:
950 	}
951 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
952 		/* to be done */
953 	}
954 	if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
955 		/* to be done */
956 	}
957 	/* IN6P_RTHDR - to be done */
958 }
959 
960 /*
961  * Get pointer to the previous header followed by the header
962  * currently processed.
963  * XXX: This function supposes that
964  *	M includes all headers,
965  *	the next header field and the header length field of each header
966  *	are valid, and
967  *	the sum of each header length equals to OFF.
968  * Because of these assumptions, this function must be called very
969  * carefully. Moreover, it will not be used in the near future when
970  * we develop `neater' mechanism to process extension headers.
971  */
972 char *
973 ip6_get_prevhdr(m, off)
974 	struct mbuf *m;
975 	int off;
976 {
977 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
978 
979 	if (off == sizeof(struct ip6_hdr))
980 		return(&ip6->ip6_nxt);
981 	else {
982 		int len, nxt;
983 		struct ip6_ext *ip6e = NULL;
984 
985 		nxt = ip6->ip6_nxt;
986 		len = sizeof(struct ip6_hdr);
987 		while (len < off) {
988 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
989 
990 			switch(nxt) {
991 			case IPPROTO_FRAGMENT:
992 				len += sizeof(struct ip6_frag);
993 				break;
994 			case IPPROTO_AH:
995 				len += (ip6e->ip6e_len + 2) << 2;
996 				break;
997 			default:
998 				len += (ip6e->ip6e_len + 1) << 3;
999 				break;
1000 			}
1001 			nxt = ip6e->ip6e_nxt;
1002 		}
1003 		if (ip6e)
1004 			return(&ip6e->ip6e_nxt);
1005 		else
1006 			return NULL;
1007 	}
1008 }
1009 
1010 /*
1011  * System control for IP6
1012  */
1013 
1014 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1015 	0,		0,		0,		0,
1016 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1017 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1018 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1019 	0,		0,		0,		0,
1020 	ENOPROTOOPT
1021 };
1022