xref: /freebsd/sys/netinet6/dest6.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: dest6.c,v 1.59 2003/07/11 13:21:16 t-momose Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
36686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
37686cdd19SJun-ichiro itojun Hagino 
3882cd038dSYoshinobu Inoue #include <sys/param.h>
3982cd038dSYoshinobu Inoue #include <sys/systm.h>
4033841545SHajimu UMEMOTO #include <sys/malloc.h>
4182cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4282cd038dSYoshinobu Inoue #include <sys/domain.h>
4382cd038dSYoshinobu Inoue #include <sys/protosw.h>
4482cd038dSYoshinobu Inoue #include <sys/socket.h>
4582cd038dSYoshinobu Inoue #include <sys/errno.h>
4682cd038dSYoshinobu Inoue #include <sys/time.h>
4733841545SHajimu UMEMOTO #include <sys/kernel.h>
4882cd038dSYoshinobu Inoue 
4982cd038dSYoshinobu Inoue #include <net/if.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
5182cd038dSYoshinobu Inoue #include <net/route.h>
5282cd038dSYoshinobu Inoue 
5382cd038dSYoshinobu Inoue #include <netinet/in.h>
5482cd038dSYoshinobu Inoue #include <netinet/in_var.h>
55686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
5682cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
57686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
5882cd038dSYoshinobu Inoue 
5982cd038dSYoshinobu Inoue /*
6082cd038dSYoshinobu Inoue  * Destination options header processing.
6182cd038dSYoshinobu Inoue  */
6282cd038dSYoshinobu Inoue int
dest6_input(struct mbuf ** mp,int * offp,int proto)631272577eSXin LI dest6_input(struct mbuf **mp, int *offp, int proto)
6482cd038dSYoshinobu Inoue {
65503f4e47SBjoern A. Zeeb 	struct mbuf *m;
66503f4e47SBjoern A. Zeeb 	int off, dstoptlen, optlen;
6782cd038dSYoshinobu Inoue 	struct ip6_dest *dstopts;
6882cd038dSYoshinobu Inoue 	u_int8_t *opt;
6982cd038dSYoshinobu Inoue 
70503f4e47SBjoern A. Zeeb 	m = *mp;
71503f4e47SBjoern A. Zeeb 	off = *offp;
72503f4e47SBjoern A. Zeeb 
73a61b5cfbSBjoern A. Zeeb 	/* Validation of the length of the header. */
74*a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + sizeof(*dstopts)) {
75a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + sizeof(*dstopts));
76a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
77a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
78a61b5cfbSBjoern A. Zeeb 			*mp = m;
79a61b5cfbSBjoern A. Zeeb 			return (IPPROTO_DONE);
80a61b5cfbSBjoern A. Zeeb 		}
81*a4adf6ccSBjoern A. Zeeb 	}
8282cd038dSYoshinobu Inoue 	dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);
8382cd038dSYoshinobu Inoue 	dstoptlen = (dstopts->ip6d_len + 1) << 3;
8482cd038dSYoshinobu Inoue 
85*a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + dstoptlen) {
86a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + dstoptlen);
87a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
88a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
89a61b5cfbSBjoern A. Zeeb 			*mp = m;
90a61b5cfbSBjoern A. Zeeb 			return (IPPROTO_DONE);
91a61b5cfbSBjoern A. Zeeb 		}
92*a4adf6ccSBjoern A. Zeeb 	}
9382cd038dSYoshinobu Inoue 	dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);
9482cd038dSYoshinobu Inoue 	off += dstoptlen;
9582cd038dSYoshinobu Inoue 	dstoptlen -= sizeof(struct ip6_dest);
9682cd038dSYoshinobu Inoue 	opt = (u_int8_t *)dstopts + sizeof(struct ip6_dest);
9782cd038dSYoshinobu Inoue 
9882cd038dSYoshinobu Inoue 	/* search header for all options. */
99c187c034SJonathan T. Looney 	for (; dstoptlen > 0; dstoptlen -= optlen, opt += optlen) {
10019391949SKris Kennaway 		if (*opt != IP6OPT_PAD1 &&
10119391949SKris Kennaway 		    (dstoptlen < IP6OPT_MINLEN || *(opt + 1) + 2 > dstoptlen)) {
1029cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_toosmall);
10319391949SKris Kennaway 			goto bad;
10419391949SKris Kennaway 		}
10519391949SKris Kennaway 
10682cd038dSYoshinobu Inoue 		switch (*opt) {
10782cd038dSYoshinobu Inoue 		case IP6OPT_PAD1:
10882cd038dSYoshinobu Inoue 			optlen = 1;
10982cd038dSYoshinobu Inoue 			break;
11082cd038dSYoshinobu Inoue 		case IP6OPT_PADN:
11182cd038dSYoshinobu Inoue 			optlen = *(opt + 1) + 2;
11282cd038dSYoshinobu Inoue 			break;
11382cd038dSYoshinobu Inoue 		default:		/* unknown option */
11433841545SHajimu UMEMOTO 			optlen = ip6_unknown_opt(opt, m,
11533841545SHajimu UMEMOTO 			    opt - mtod(m, u_int8_t *));
116a8fe77d8SBjoern A. Zeeb 			if (optlen == -1) {
117a8fe77d8SBjoern A. Zeeb 				*mp = NULL;
11882cd038dSYoshinobu Inoue 				return (IPPROTO_DONE);
119a8fe77d8SBjoern A. Zeeb 			}
12082cd038dSYoshinobu Inoue 			optlen += 2;
12182cd038dSYoshinobu Inoue 			break;
12282cd038dSYoshinobu Inoue 		}
12382cd038dSYoshinobu Inoue 	}
12482cd038dSYoshinobu Inoue 
12582cd038dSYoshinobu Inoue 	*offp = off;
126a8fe77d8SBjoern A. Zeeb 	*mp = m;
12782cd038dSYoshinobu Inoue 	return (dstopts->ip6d_nxt);
12882cd038dSYoshinobu Inoue 
12982cd038dSYoshinobu Inoue   bad:
13082cd038dSYoshinobu Inoue 	m_freem(m);
131a8fe77d8SBjoern A. Zeeb 	*mp = NULL;
13282cd038dSYoshinobu Inoue 	return (IPPROTO_DONE);
13382cd038dSYoshinobu Inoue }
134