xref: /openbsd/sys/netinet6/dest6.c (revision 94c0e2bd)
1*94c0e2bdSbluhm /*	$OpenBSD: dest6.c,v 1.20 2024/02/13 12:22:09 bluhm Exp $	*/
2129efc0fSitojun /*	$KAME: dest6.c,v 1.25 2001/02/22 01:39:16 itojun Exp $	*/
3287546eaSitojun 
4287546eaSitojun /*
5287546eaSitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6287546eaSitojun  * All rights reserved.
7287546eaSitojun  *
8287546eaSitojun  * Redistribution and use in source and binary forms, with or without
9287546eaSitojun  * modification, are permitted provided that the following conditions
10287546eaSitojun  * are met:
11287546eaSitojun  * 1. Redistributions of source code must retain the above copyright
12287546eaSitojun  *    notice, this list of conditions and the following disclaimer.
13287546eaSitojun  * 2. Redistributions in binary form must reproduce the above copyright
14287546eaSitojun  *    notice, this list of conditions and the following disclaimer in the
15287546eaSitojun  *    documentation and/or other materials provided with the distribution.
16287546eaSitojun  * 3. Neither the name of the project nor the names of its contributors
17287546eaSitojun  *    may be used to endorse or promote products derived from this software
18287546eaSitojun  *    without specific prior written permission.
19287546eaSitojun  *
20287546eaSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21287546eaSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22287546eaSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23287546eaSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24287546eaSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25287546eaSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26287546eaSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27287546eaSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28287546eaSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29287546eaSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30287546eaSitojun  * SUCH DAMAGE.
31287546eaSitojun  */
32287546eaSitojun 
33287546eaSitojun #include <sys/param.h>
34287546eaSitojun #include <sys/systm.h>
35287546eaSitojun #include <sys/mbuf.h>
36287546eaSitojun #include <sys/socket.h>
37287546eaSitojun #include <sys/errno.h>
38287546eaSitojun #include <sys/time.h>
39287546eaSitojun #include <sys/kernel.h>
40287546eaSitojun 
41*94c0e2bdSbluhm #include <net/route.h>
42*94c0e2bdSbluhm 
43287546eaSitojun #include <netinet/in.h>
44fa86ee14Sitojun #include <netinet/ip6.h>
45287546eaSitojun #include <netinet6/ip6_var.h>
46fa86ee14Sitojun #include <netinet/icmp6.h>
47287546eaSitojun 
48287546eaSitojun /*
49287546eaSitojun  * Destination options header processing.
50287546eaSitojun  */
51287546eaSitojun int
dest6_input(struct mbuf ** mp,int * offp,int proto,int af)52459fa0feSbluhm dest6_input(struct mbuf **mp, int *offp, int proto, int af)
53287546eaSitojun {
54287546eaSitojun 	int off = *offp, dstoptlen, optlen;
55287546eaSitojun 	struct ip6_dest *dstopts;
56287546eaSitojun 	u_int8_t *opt;
57287546eaSitojun 
58287546eaSitojun 	/* validation of the length of the header */
5980467c39Sbluhm 	IP6_EXTHDR_GET(dstopts, struct ip6_dest *, *mp, off, sizeof(*dstopts));
60287546eaSitojun 	if (dstopts == NULL)
61287546eaSitojun 		return IPPROTO_DONE;
62287546eaSitojun 	dstoptlen = (dstopts->ip6d_len + 1) << 3;
63287546eaSitojun 
6480467c39Sbluhm 	IP6_EXTHDR_GET(dstopts, struct ip6_dest *, *mp, off, dstoptlen);
65287546eaSitojun 	if (dstopts == NULL)
66287546eaSitojun 		return IPPROTO_DONE;
67287546eaSitojun 	off += dstoptlen;
68287546eaSitojun 	dstoptlen -= sizeof(struct ip6_dest);
69287546eaSitojun 	opt = (u_int8_t *)dstopts + sizeof(struct ip6_dest);
70287546eaSitojun 
71287546eaSitojun 	/* search header for all options. */
72287546eaSitojun 	for (optlen = 0; dstoptlen > 0; dstoptlen -= optlen, opt += optlen) {
73129efc0fSitojun 		if (*opt != IP6OPT_PAD1 &&
74129efc0fSitojun 		    (dstoptlen < IP6OPT_MINLEN || *(opt + 1) + 2 > dstoptlen)) {
7531e14cacSjca 			ip6stat_inc(ip6s_toosmall);
7615bd77d2Sitojun 			goto bad;
7715bd77d2Sitojun 		}
7815bd77d2Sitojun 
79287546eaSitojun 		switch (*opt) {
80287546eaSitojun 		case IP6OPT_PAD1:
81287546eaSitojun 			optlen = 1;
82287546eaSitojun 			break;
83287546eaSitojun 		case IP6OPT_PADN:
84287546eaSitojun 			optlen = *(opt + 1) + 2;
85287546eaSitojun 			break;
86287546eaSitojun 		default:		/* unknown option */
8780467c39Sbluhm 			optlen = ip6_unknown_opt(mp, opt,
8880467c39Sbluhm 			    opt - mtod(*mp, u_int8_t *));
8996ef8dedSitojun 			if (optlen == -1)
90287546eaSitojun 				return (IPPROTO_DONE);
91287546eaSitojun 			optlen += 2;
92287546eaSitojun 			break;
93287546eaSitojun 		}
94287546eaSitojun 	}
95287546eaSitojun 
96287546eaSitojun 	*offp = off;
97287546eaSitojun 	return (dstopts->ip6d_nxt);
98287546eaSitojun 
99287546eaSitojun   bad:
10080467c39Sbluhm 	m_freemp(mp);
101287546eaSitojun 	return (IPPROTO_DONE);
102287546eaSitojun }
103