/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the project nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* $KAME: dest6.c,v 1.59 2003/07/11 13:21:16 t-momose Exp $31*/3233#include "opt_inet.h"34#include "opt_inet6.h"3536#include <sys/param.h>37#include <sys/systm.h>38#include <sys/malloc.h>39#include <sys/mbuf.h>40#include <sys/domain.h>41#include <sys/protosw.h>42#include <sys/socket.h>43#include <sys/errno.h>44#include <sys/time.h>45#include <sys/kernel.h>4647#include <net/if.h>48#include <net/if_var.h>49#include <net/route.h>5051#include <netinet/in.h>52#include <netinet/in_var.h>53#include <netinet/ip6.h>54#include <netinet6/ip6_var.h>55#include <netinet/icmp6.h>5657/*58* Destination options header processing.59*/60int61dest6_input(struct mbuf **mp, int *offp, int proto)62{63struct mbuf *m;64int off, dstoptlen, optlen;65struct ip6_dest *dstopts;66u_int8_t *opt;6768m = *mp;69off = *offp;7071/* Validation of the length of the header. */72if (m->m_len < off + sizeof(*dstopts)) {73m = m_pullup(m, off + sizeof(*dstopts));74if (m == NULL) {75IP6STAT_INC(ip6s_exthdrtoolong);76*mp = m;77return (IPPROTO_DONE);78}79}80dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);81dstoptlen = (dstopts->ip6d_len + 1) << 3;8283if (m->m_len < off + dstoptlen) {84m = m_pullup(m, off + dstoptlen);85if (m == NULL) {86IP6STAT_INC(ip6s_exthdrtoolong);87*mp = m;88return (IPPROTO_DONE);89}90}91dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);92off += dstoptlen;93dstoptlen -= sizeof(struct ip6_dest);94opt = (u_int8_t *)dstopts + sizeof(struct ip6_dest);9596/* search header for all options. */97for (; dstoptlen > 0; dstoptlen -= optlen, opt += optlen) {98if (*opt != IP6OPT_PAD1 &&99(dstoptlen < IP6OPT_MINLEN || *(opt + 1) + 2 > dstoptlen)) {100IP6STAT_INC(ip6s_toosmall);101goto bad;102}103104switch (*opt) {105case IP6OPT_PAD1:106optlen = 1;107break;108case IP6OPT_PADN:109optlen = *(opt + 1) + 2;110break;111default: /* unknown option */112optlen = ip6_unknown_opt(opt, m,113opt - mtod(m, u_int8_t *));114if (optlen == -1) {115*mp = NULL;116return (IPPROTO_DONE);117}118optlen += 2;119break;120}121}122123*offp = off;124*mp = m;125return (dstopts->ip6d_nxt);126127bad:128m_freem(m);129*mp = NULL;130return (IPPROTO_DONE);131}132133134