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