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