xref: /freebsd/sys/netinet6/ip6_input.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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 <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/proc.h>
80 
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/if_dl.h>
84 #include <net/route.h>
85 #include <net/netisr.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/icmp6.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/in6_prefix.h>
99 
100 #ifdef IPV6FIREWALL
101 #include <netinet6/ip6_fw.h>
102 #endif
103 
104 #ifdef ALTQ
105 #include <netinet/altq_cdnr.h>
106 #endif
107 
108 #include <netinet6/ip6protosw.h>
109 
110 /* we need it for NLOOP. */
111 #include "loop.h"
112 
113 #include "faith.h"
114 #include "gif.h"
115 
116 #include <net/net_osdep.h>
117 
118 extern struct	domain inet6domain;
119 extern struct	ip6protosw inet6sw[];
120 
121 u_char	ip6_protox[IPPROTO_MAX];
122 static int	ip6qmaxlen = IFQ_MAXLEN;
123 struct	in6_ifaddr *in6_ifaddr;
124 struct	ifqueue ip6intrq;
125 
126 int	ip6_forward_srcrt;			/* XXX */
127 int	ip6_sourcecheck;			/* XXX */
128 int	ip6_sourcecheck_interval;		/* XXX */
129 
130 #ifdef IPV6FIREWALL
131 /* firewall hooks */
132 ip6_fw_chk_t	*ip6_fw_chk_ptr;
133 ip6_fw_ctl_t	*ip6_fw_ctl_ptr;
134 #endif
135 
136 struct	ip6stat ip6stat;
137 
138 static void	ip6_init2 __P((void *));
139 
140 static int	ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
141 
142 #if defined(PTR)
143 extern int	ip6_protocol_tr;
144 
145 int	ptr_in6		__P((struct mbuf *, struct mbuf **));
146 extern void	ip_forward __P((struct mbuf *, int));
147 #endif
148 
149 /*
150  * IP6 initialization: fill in IP6 protocol switch table.
151  * All protocols not implemented in kernel go to raw IP6 protocol handler.
152  */
153 void
154 ip6_init()
155 {
156 	register struct ip6protosw *pr;
157 	register int i;
158 	struct timeval tv;
159 
160 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
161 	if (pr == 0)
162 		panic("ip6_init");
163 	for (i = 0; i < IPPROTO_MAX; i++)
164 		ip6_protox[i] = pr - inet6sw;
165 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
166 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
167 		if (pr->pr_domain->dom_family == PF_INET6 &&
168 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
169 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
170 	ip6intrq.ifq_maxlen = ip6qmaxlen;
171 	nd6_init();
172 	frag6_init();
173 #ifdef IPV6FIREWALL
174 	ip6_fw_init();
175 #endif
176 	/*
177 	 * in many cases, random() here does NOT return random number
178 	 * as initialization during bootstrap time occur in fixed order.
179 	 */
180 	microtime(&tv);
181 	ip6_flow_seq = random() ^ tv.tv_usec;
182 }
183 
184 static void
185 ip6_init2(dummy)
186 	void *dummy;
187 {
188 	int i;
189 	int ret;
190 
191 	/* get EUI64 from somewhere */
192 	ret = in6_ifattach_getifid(NULL);
193 
194 	/*
195 	 * to route local address of p2p link to loopback,
196 	 * assign loopback address first.
197 	 */
198 	for (i = 0; i < NLOOP; i++)
199 		in6_ifattach(&loif[i], IN6_IFT_LOOP, NULL, 0);
200 
201 	/* attach pseudo interfaces */
202 	if (ret == 0)
203 		in6_ifattach_p2p();
204 
205 	/* nd6_timer_init */
206 	timeout(nd6_timer, (caddr_t)0, hz);
207 	/* router renumbering prefix list maintenance */
208 	timeout(in6_rr_timer, (caddr_t)0, hz);
209 }
210 
211 /* cheat */
212 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init2, NULL);
213 
214 /*
215  * IP6 input interrupt handling. Just pass the packet to ip6_input.
216  */
217 void
218 ip6intr()
219 {
220 	int s;
221 	struct mbuf *m;
222 
223 	for (;;) {
224 		s = splimp();
225 		IF_DEQUEUE(&ip6intrq, m);
226 		splx(s);
227 		if (m == 0)
228 			return;
229 		ip6_input(m);
230 	}
231 }
232 
233 NETISR_SET(NETISR_IPV6, ip6intr);
234 
235 extern struct	route_in6 ip6_forward_rt;
236 
237 void
238 ip6_input(m)
239 	struct mbuf *m;
240 {
241 	struct ip6_hdr *ip6;
242 	int off = sizeof(struct ip6_hdr), nest;
243 	u_int32_t plen;
244 	u_int32_t rtalert = ~0;
245 	int nxt, ours = 0;
246 	struct ifnet *deliverifp = NULL;
247 
248 #ifdef IPSEC
249 	/*
250 	 * should the inner packet be considered authentic?
251 	 * see comment in ah4_input().
252 	 */
253 	if (m) {
254 		m->m_flags &= ~M_AUTHIPHDR;
255 		m->m_flags &= ~M_AUTHIPDGM;
256 	}
257 #endif
258 
259 	/*
260 	 * mbuf statistics by kazu
261 	 */
262 	if (m->m_flags & M_EXT) {
263 		if (m->m_next)
264 			ip6stat.ip6s_mext2m++;
265 		else
266 			ip6stat.ip6s_mext1++;
267 	} else {
268 		if (m->m_next) {
269 			if (m->m_flags & M_LOOP) {
270 				ip6stat.ip6s_m2m[loif[0].if_index]++;	/*XXX*/
271 			} else if (m->m_pkthdr.rcvif->if_index <= 31)
272 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
273 			else
274 				ip6stat.ip6s_m2m[0]++;
275 		} else
276 			ip6stat.ip6s_m1++;
277 	}
278 
279 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
280 	ip6stat.ip6s_total++;
281 
282 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
283 
284 	if (m->m_len < sizeof(struct ip6_hdr)) {
285 		struct ifnet *inifp;
286 		inifp = m->m_pkthdr.rcvif;
287 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
288 			ip6stat.ip6s_toosmall++;
289 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
290 			return;
291 		}
292 	}
293 
294 	ip6 = mtod(m, struct ip6_hdr *);
295 
296 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
297 		ip6stat.ip6s_badvers++;
298 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
299 		goto bad;
300 	}
301 
302 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
303 
304 #ifdef IPV6FIREWALL
305 	/*
306 	 * Check with the firewall...
307 	 */
308 	if (ip6_fw_chk_ptr) {
309 		u_short port = 0;
310 		/* If ipfw says divert, we have to just drop packet */
311 		/* use port as a dummy argument */
312 		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
313 			m_freem(m);
314 			m = NULL;
315 		}
316 		if (!m)
317 			return;
318 	}
319 #endif
320 
321 #ifdef ALTQ
322 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
323 		/* packet is dropped by traffic conditioner */
324 		return;
325 	}
326 #endif
327 
328 	/*
329 	 * Scope check
330 	 */
331 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
332 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
333 		ip6stat.ip6s_badscope++;
334 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
335 		goto bad;
336 	}
337 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
338 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
339 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
340 			ours = 1;
341 			deliverifp = m->m_pkthdr.rcvif;
342 			goto hbhcheck;
343 		} else {
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 {
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 		if (!ours) {
542 			m_freem(m);
543 			return;
544 		}
545 	} else if (!ours) {
546 		ip6_forward(m, 0);
547 		return;
548 	}
549 
550 	/*
551 	 * Tell launch routine the next header
552 	 */
553 #if defined(__NetBSD__) && defined(IFA_STATS)
554 	if (IFA_STATS && deliverifp != NULL) {
555 		struct in6_ifaddr *ia6;
556 		ip6 = mtod(m, struct ip6_hdr *);
557 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
558 		if (ia6)
559 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
560 	}
561 #endif
562 	ip6stat.ip6s_delivered++;
563 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
564 	nest = 0;
565 	while (nxt != IPPROTO_DONE) {
566 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
567 			ip6stat.ip6s_toomanyhdr++;
568 			goto bad;
569 		}
570 
571 		/*
572 		 * protection against faulty packet - there should be
573 		 * more sanity checks in header chain processing.
574 		 */
575 		if (m->m_pkthdr.len < off) {
576 			ip6stat.ip6s_tooshort++;
577 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
578 			goto bad;
579 		}
580 
581 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
582 	}
583 	return;
584  bad:
585 	m_freem(m);
586 }
587 
588 /*
589  * Hop-by-Hop options header processing. If a valid jumbo payload option is
590  * included, the real payload length will be stored in plenp.
591  */
592 static int
593 ip6_hopopts_input(plenp, rtalertp, mp, offp)
594 	u_int32_t *plenp;
595 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
596 	struct mbuf **mp;
597 	int *offp;
598 {
599 	register struct mbuf *m = *mp;
600 	int off = *offp, hbhlen;
601 	struct ip6_hbh *hbh;
602 	u_int8_t *opt;
603 
604 	/* validation of the length of the header */
605 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
606 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
607 	hbhlen = (hbh->ip6h_len + 1) << 3;
608 
609 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
610 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
611 	off += hbhlen;
612 	hbhlen -= sizeof(struct ip6_hbh);
613 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
614 
615 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
616 				hbhlen, rtalertp, plenp) < 0)
617 		return(-1);
618 
619 	*offp = off;
620 	*mp = m;
621 	return(0);
622 }
623 
624 /*
625  * Search header for all Hop-by-hop options and process each option.
626  * This function is separate from ip6_hopopts_input() in order to
627  * handle a case where the sending node itself process its hop-by-hop
628  * options header. In such a case, the function is called from ip6_output().
629  */
630 int
631 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
632 	struct mbuf *m;
633 	u_int8_t *opthead;
634 	int hbhlen;
635 	u_int32_t *rtalertp;
636 	u_int32_t *plenp;
637 {
638 	struct ip6_hdr *ip6;
639 	int optlen = 0;
640 	u_int8_t *opt = opthead;
641 	u_int16_t rtalert_val;
642 
643 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
644 		switch(*opt) {
645 		 case IP6OPT_PAD1:
646 			 optlen = 1;
647 			 break;
648 		 case IP6OPT_PADN:
649 			 if (hbhlen < IP6OPT_MINLEN) {
650 				 ip6stat.ip6s_toosmall++;
651 				 goto bad;
652 			 }
653 			 optlen = *(opt + 1) + 2;
654 			 break;
655 		 case IP6OPT_RTALERT:
656 			 /* XXX may need check for alignment */
657 			 if (hbhlen < IP6OPT_RTALERT_LEN) {
658 				 ip6stat.ip6s_toosmall++;
659 				 goto bad;
660 			 }
661 			 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
662 				  /* XXX: should we discard the packet? */
663 				 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
664 				     *(opt + 1));
665 			 optlen = IP6OPT_RTALERT_LEN;
666 			 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
667 			 *rtalertp = ntohs(rtalert_val);
668 			 break;
669 		 case IP6OPT_JUMBO:
670 			 /* XXX may need check for alignment */
671 			 if (hbhlen < IP6OPT_JUMBO_LEN) {
672 				 ip6stat.ip6s_toosmall++;
673 				 goto bad;
674 			 }
675 			 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
676 				  /* XXX: should we discard the packet? */
677 				 log(LOG_ERR, "length of jumbopayload opt "
678 				     "is inconsistent(%d)",
679 				     *(opt + 1));
680 			 optlen = IP6OPT_JUMBO_LEN;
681 
682 			 bcopy(opt + 2, plenp, sizeof(*plenp));
683 			 *plenp = htonl(*plenp);
684 			 if (*plenp <= IPV6_MAXPACKET) {
685 				 /*
686 				  * jumbo payload length must be larger
687 				  * than 65535
688 				  */
689 				 ip6stat.ip6s_badoptions++;
690 				 icmp6_error(m, ICMP6_PARAM_PROB,
691 					     ICMP6_PARAMPROB_HEADER,
692 					     sizeof(struct ip6_hdr) +
693 					     sizeof(struct ip6_hbh) +
694 					     opt + 2 - opthead);
695 				 return(-1);
696 			 }
697 
698 			 ip6 = mtod(m, struct ip6_hdr *);
699 			 if (ip6->ip6_plen) {
700 				 /*
701 				  * IPv6 packets that have non 0 payload length
702 				  * must not contain a jumbo paylod option.
703 				  */
704 				 ip6stat.ip6s_badoptions++;
705 				 icmp6_error(m, ICMP6_PARAM_PROB,
706 					     ICMP6_PARAMPROB_HEADER,
707 					     sizeof(struct ip6_hdr) +
708 					     sizeof(struct ip6_hbh) +
709 					     opt - opthead);
710 				 return(-1);
711 			 }
712 			 break;
713 		 default:		/* unknown option */
714 			 if (hbhlen < IP6OPT_MINLEN) {
715 				 ip6stat.ip6s_toosmall++;
716 				 goto bad;
717 			 }
718 			 if ((optlen = ip6_unknown_opt(opt, m,
719 						       sizeof(struct ip6_hdr) +
720 						       sizeof(struct ip6_hbh) +
721 						       opt - opthead)) == -1)
722 				 return(-1);
723 			 optlen += 2;
724 			 break;
725 		}
726 	}
727 
728 	return(0);
729 
730   bad:
731 	m_freem(m);
732 	return(-1);
733 }
734 
735 /*
736  * Unknown option processing.
737  * The third argument `off' is the offset from the IPv6 header to the option,
738  * which is necessary if the IPv6 header the and option header and IPv6 header
739  * is not continuous in order to return an ICMPv6 error.
740  */
741 int
742 ip6_unknown_opt(optp, m, off)
743 	u_int8_t *optp;
744 	struct mbuf *m;
745 	int off;
746 {
747 	struct ip6_hdr *ip6;
748 
749 	switch(IP6OPT_TYPE(*optp)) {
750 	 case IP6OPT_TYPE_SKIP: /* ignore the option */
751 		 return((int)*(optp + 1));
752 	 case IP6OPT_TYPE_DISCARD:	/* silently discard */
753 		 m_freem(m);
754 		 return(-1);
755 	 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
756 		 ip6stat.ip6s_badoptions++;
757 		 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
758 		 return(-1);
759 	 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
760 		 ip6stat.ip6s_badoptions++;
761 		 ip6 = mtod(m, struct ip6_hdr *);
762 		 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
763 		     (m->m_flags & (M_BCAST|M_MCAST)))
764 			 m_freem(m);
765 		 else
766 			 icmp6_error(m, ICMP6_PARAM_PROB,
767 				     ICMP6_PARAMPROB_OPTION, off);
768 		 return(-1);
769 	}
770 
771 	m_freem(m);		/* XXX: NOTREACHED */
772 	return(-1);
773 }
774 
775 /*
776  * Create the "control" list for this pcb
777  */
778 void
779 ip6_savecontrol(in6p, mp, ip6, m)
780 	register struct inpcb *in6p;
781 	register struct mbuf **mp;
782 	register struct ip6_hdr *ip6;
783 	register struct mbuf *m;
784 {
785 	struct proc *p = curproc;	/* XXX */
786 	int privileged;
787 
788 	privileged = 0;
789 	if (p && !suser(p))
790 		privileged++;
791 
792 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
793 		struct timeval tv;
794 
795 		microtime(&tv);
796 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
797 			SCM_TIMESTAMP, SOL_SOCKET);
798 		if (*mp)
799 			mp = &(*mp)->m_next;
800 	}
801 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
802 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
803 			sizeof(struct in6_addr), IPV6_RECVDSTADDR,
804 			IPPROTO_IPV6);
805 		if (*mp)
806 			mp = &(*mp)->m_next;
807 	}
808 
809 	/* RFC 2292 sec. 5 */
810 	if (in6p->in6p_flags & IN6P_PKTINFO) {
811 		struct in6_pktinfo pi6;
812 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
813 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
814 			pi6.ipi6_addr.s6_addr16[1] = 0;
815 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
816 					? m->m_pkthdr.rcvif->if_index
817 					: 0;
818 		*mp = sbcreatecontrol((caddr_t) &pi6,
819 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
820 			IPPROTO_IPV6);
821 		if (*mp)
822 			mp = &(*mp)->m_next;
823 	}
824 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
825 		int hlim = ip6->ip6_hlim & 0xff;
826 		*mp = sbcreatecontrol((caddr_t) &hlim,
827 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
828 		if (*mp)
829 			mp = &(*mp)->m_next;
830 	}
831 	/* IN6P_NEXTHOP - for outgoing packet only */
832 
833 	/*
834 	 * IPV6_HOPOPTS socket option. We require super-user privilege
835 	 * for the option, but it might be too strict, since there might
836 	 * be some hop-by-hop options which can be returned to normal user.
837 	 * See RFC 2292 section 6.
838 	 */
839 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
840 		/*
841 		 * Check if a hop-by-hop options header is contatined in the
842 		 * received packet, and if so, store the options as ancillary
843 		 * data. Note that a hop-by-hop options header must be
844 		 * just after the IPv6 header, which fact is assured through
845 		 * the IPv6 input processing.
846 		 */
847 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
848 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
849 			struct ip6_hbh *hbh = (struct ip6_hbh *)(ip6 + 1);
850 
851 			/*
852 			 * XXX: We copy whole the header even if a jumbo
853 			 * payload option is included, which option is to
854 			 * be removed before returning in the RFC 2292.
855 			 * But it's too painful operation...
856 			 */
857 			*mp = sbcreatecontrol((caddr_t)hbh,
858 					      (hbh->ip6h_len + 1) << 3,
859 					      IPV6_HOPOPTS, IPPROTO_IPV6);
860 			if (*mp)
861 				mp = &(*mp)->m_next;
862 		}
863 	}
864 
865 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
866 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
867 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
868 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
869 
870 		/*
871 		 * Search for destination options headers or routing
872 		 * header(s) through the header chain, and stores each
873 		 * header as ancillary data.
874 		 * Note that the order of the headers remains in
875 		 * the chain of ancillary data.
876 		 */
877 		while(1) {	/* is explicit loop prevention necessary? */
878 			struct ip6_ext *ip6e =
879 				(struct ip6_ext *)(mtod(m, caddr_t) + off);
880 
881 			switch(nxt) {
882 		         case IPPROTO_DSTOPTS:
883 				 if (!in6p->in6p_flags & IN6P_DSTOPTS)
884 					 break;
885 
886 				 /*
887 				  * We also require super-user privilege for
888 				  * the option.
889 				  * See the comments on IN6_HOPOPTS.
890 				  */
891 				 if (!privileged)
892 					 break;
893 
894 				 *mp = sbcreatecontrol((caddr_t)ip6e,
895 						       (ip6e->ip6e_len + 1) << 3,
896 						       IPV6_DSTOPTS,
897 						       IPPROTO_IPV6);
898 				 if (*mp)
899 					 mp = &(*mp)->m_next;
900 				 break;
901 
902 			 case IPPROTO_ROUTING:
903 				 if (!in6p->in6p_flags & IN6P_RTHDR)
904 					 break;
905 
906 				 *mp = sbcreatecontrol((caddr_t)ip6e,
907 						       (ip6e->ip6e_len + 1) << 3,
908 						       IPV6_RTHDR,
909 						       IPPROTO_IPV6);
910 				 if (*mp)
911 					 mp = &(*mp)->m_next;
912 				 break;
913 
914 			 case IPPROTO_UDP:
915 			 case IPPROTO_TCP:
916 			 case IPPROTO_ICMPV6:
917 			 default:
918 				 /*
919 				  * stop search if we encounter an upper
920 				  * layer protocol headers.
921 				  */
922 				 goto loopend;
923 
924 			 case IPPROTO_HOPOPTS:
925 			 case IPPROTO_AH: /* is it possible? */
926 				 break;
927 			}
928 
929 			/* proceed with the next header. */
930 			if (nxt == IPPROTO_AH)
931 				off += (ip6e->ip6e_len + 2) << 2;
932 			else
933 				off += (ip6e->ip6e_len + 1) << 3;
934 			nxt = ip6e->ip6e_nxt;
935 		}
936 	  loopend:
937 	}
938 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
939 		/* to be done */
940 	}
941 	if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
942 		/* to be done */
943 	}
944 	/* IN6P_RTHDR - to be done */
945 }
946 
947 /*
948  * Get pointer to the previous header followed by the header
949  * currently processed.
950  * XXX: This function supposes that
951  *	M includes all headers,
952  *	the next header field and the header length field of each header
953  *	are valid, and
954  *	the sum of each header length equals to OFF.
955  * Because of these assumptions, this function must be called very
956  * carefully. Moreover, it will not be used in the near future when
957  * we develop `neater' mechanism to process extension headers.
958  */
959 char *
960 ip6_get_prevhdr(m, off)
961 	struct mbuf *m;
962 	int off;
963 {
964 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
965 
966 	if (off == sizeof(struct ip6_hdr))
967 		return(&ip6->ip6_nxt);
968 	else {
969 		int len, nxt;
970 		struct ip6_ext *ip6e = NULL;
971 
972 		nxt = ip6->ip6_nxt;
973 		len = sizeof(struct ip6_hdr);
974 		while (len < off) {
975 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
976 
977 			switch(nxt) {
978 			case IPPROTO_FRAGMENT:
979 				len += sizeof(struct ip6_frag);
980 				break;
981 			case IPPROTO_AH:
982 				len += (ip6e->ip6e_len + 2) << 2;
983 				break;
984 			default:
985 				len += (ip6e->ip6e_len + 1) << 3;
986 				break;
987 			}
988 			nxt = ip6e->ip6e_nxt;
989 		}
990 		if (ip6e)
991 			return(&ip6e->ip6e_nxt);
992 		else
993 			return NULL;
994 	}
995 }
996 
997 /*
998  * System control for IP6
999  */
1000 
1001 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1002 	0,		0,		0,		0,
1003 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1004 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1005 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1006 	0,		0,		0,		0,
1007 	ENOPROTOOPT
1008 };
1009