Home | History | Annotate | Line # | Download | only in netmpls
mpls_ttl.c revision 1.8.10.1
      1  1.8.10.1     snj /*	$NetBSD: mpls_ttl.c,v 1.8.10.1 2018/02/12 18:18:00 snj Exp $ */
      2       1.1  kefren 
      3       1.1  kefren /*
      4       1.1  kefren  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5       1.1  kefren  * All rights reserved.
      6       1.1  kefren  *
      7       1.1  kefren  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  kefren  * by Public Access Networks Corporation ("Panix").  It was developed under
      9       1.1  kefren  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     10       1.1  kefren  *
     11       1.1  kefren  * This code is derived from software contributed to The NetBSD Foundation
     12       1.1  kefren  * by Jason R. Thorpe of Zembu Labs, Inc.
     13       1.1  kefren  *
     14       1.1  kefren  * This code is derived from software contributed to The NetBSD Foundation
     15       1.1  kefren  * by Mihai Chelaru <kefren (at) NetBSD.org>
     16       1.1  kefren  *
     17       1.1  kefren  * Redistribution and use in source and binary forms, with or without
     18       1.1  kefren  * modification, are permitted provided that the following conditions
     19       1.1  kefren  * are met:
     20       1.1  kefren  * 1. Redistributions of source code must retain the above copyright
     21       1.1  kefren  *    notice, this list of conditions and the following disclaimer.
     22       1.1  kefren  * 2. Redistributions in binary form must reproduce the above copyright
     23       1.1  kefren  *    notice, this list of conditions and the following disclaimer in the
     24       1.1  kefren  *    documentation and/or other materials provided with the distribution.
     25       1.1  kefren  *
     26       1.1  kefren  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  kefren  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  kefren  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  kefren  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  kefren  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  kefren  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  kefren  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  kefren  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  kefren  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  kefren  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  kefren  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  kefren  */
     38       1.1  kefren 
     39       1.1  kefren /*
     40  1.8.10.1     snj  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     41  1.8.10.1     snj  * All rights reserved.
     42  1.8.10.1     snj  *
     43  1.8.10.1     snj  * Redistribution and use in source and binary forms, with or without
     44  1.8.10.1     snj  * modification, are permitted provided that the following conditions
     45  1.8.10.1     snj  * are met:
     46  1.8.10.1     snj  * 1. Redistributions of source code must retain the above copyright
     47  1.8.10.1     snj  *    notice, this list of conditions and the following disclaimer.
     48  1.8.10.1     snj  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.8.10.1     snj  *    notice, this list of conditions and the following disclaimer in the
     50  1.8.10.1     snj  *    documentation and/or other materials provided with the distribution.
     51  1.8.10.1     snj  * 3. Neither the name of the project nor the names of its contributors
     52  1.8.10.1     snj  *    may be used to endorse or promote products derived from this software
     53  1.8.10.1     snj  *    without specific prior written permission.
     54  1.8.10.1     snj  *
     55  1.8.10.1     snj  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     56  1.8.10.1     snj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  1.8.10.1     snj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  1.8.10.1     snj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     59  1.8.10.1     snj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  1.8.10.1     snj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  1.8.10.1     snj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  1.8.10.1     snj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  1.8.10.1     snj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  1.8.10.1     snj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  1.8.10.1     snj  * SUCH DAMAGE.
     66  1.8.10.1     snj  */
     67  1.8.10.1     snj 
     68  1.8.10.1     snj /*
     69       1.1  kefren  * Copyright (c) 1982, 1986, 1988, 1993
     70       1.1  kefren  *      The Regents of the University of California.  All rights reserved.
     71       1.1  kefren  *
     72       1.1  kefren  * Redistribution and use in source and binary forms, with or without
     73       1.1  kefren  * modification, are permitted provided that the following conditions
     74       1.1  kefren  * are met:
     75       1.1  kefren  * 1. Redistributions of source code must retain the above copyright
     76       1.1  kefren  *    notice, this list of conditions and the following disclaimer.
     77       1.1  kefren  * 2. Redistributions in binary form must reproduce the above copyright
     78       1.1  kefren  *    notice, this list of conditions and the following disclaimer in the
     79       1.1  kefren  *    documentation and/or other materials provided with the distribution.
     80       1.1  kefren  * 3. Neither the name of the University nor the names of its contributors
     81       1.1  kefren  *    may be used to endorse or promote products derived from this software
     82       1.1  kefren  *    without specific prior written permission.
     83       1.1  kefren  *
     84       1.1  kefren  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     85       1.1  kefren  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86       1.1  kefren  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87       1.1  kefren  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     88       1.1  kefren  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89       1.1  kefren  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90       1.1  kefren  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91       1.1  kefren  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92       1.1  kefren  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93       1.1  kefren  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94       1.1  kefren  * SUCH DAMAGE.
     95       1.1  kefren  *
     96       1.1  kefren  *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
     97       1.1  kefren  */
     98       1.1  kefren 
     99       1.1  kefren #include <sys/cdefs.h>
    100  1.8.10.1     snj __KERNEL_RCSID(0, "$NetBSD: mpls_ttl.c,v 1.8.10.1 2018/02/12 18:18:00 snj Exp $");
    101       1.1  kefren 
    102       1.5   pooka #ifdef _KERNEL_OPT
    103       1.1  kefren #include "opt_inet.h"
    104       1.1  kefren #include "opt_mpls.h"
    105       1.5   pooka #endif
    106       1.1  kefren 
    107       1.1  kefren #include <sys/param.h>
    108       1.1  kefren #include <sys/malloc.h>
    109       1.1  kefren #include <sys/mbuf.h>
    110       1.1  kefren #include <sys/errno.h>
    111       1.1  kefren #include <sys/socket.h>
    112       1.1  kefren #include <sys/systm.h>
    113       1.1  kefren 
    114       1.1  kefren #include <net/if.h>
    115       1.1  kefren #include <net/if_dl.h>
    116       1.1  kefren 
    117       1.1  kefren #include <netinet/in.h>
    118       1.1  kefren #include <netinet/in_systm.h>
    119       1.1  kefren #include <netinet/ip.h>
    120       1.1  kefren #include <netinet/ip_icmp.h>
    121       1.1  kefren #include <netinet/icmp_private.h>
    122       1.1  kefren #include <netinet/icmp_var.h>
    123       1.1  kefren 
    124       1.1  kefren #include <netmpls/mpls.h>
    125       1.1  kefren #include <netmpls/mpls_var.h>
    126       1.1  kefren 
    127       1.1  kefren #ifdef INET
    128       1.1  kefren 
    129       1.1  kefren /* in netinet/ip_icmp.c */
    130       1.1  kefren extern int icmpreturndatabytes;
    131       1.1  kefren 
    132       1.1  kefren /* ICMP Extensions */
    133       1.1  kefren 
    134       1.1  kefren #define ICMP_EXT_VERSION 2
    135       1.2  kefren #define	MPLS_RETURN_DATA 128
    136       1.2  kefren #define	ICMP_EXT_OFFSET	128
    137       1.1  kefren 
    138       1.1  kefren struct icmp_ext_cmn_hdr {
    139       1.1  kefren #if BYTE_ORDER == BIG_ENDIAN
    140       1.1  kefren         unsigned char   version:4;
    141       1.1  kefren         unsigned char   reserved1:4;
    142       1.1  kefren #else
    143       1.1  kefren         unsigned char   reserved1:4;
    144       1.1  kefren         unsigned char   version:4;
    145       1.1  kefren #endif
    146       1.1  kefren 	unsigned char   reserved2;
    147       1.1  kefren 	unsigned short  checksum;
    148       1.1  kefren };
    149       1.1  kefren 
    150       1.1  kefren struct icmp_ext_obj_hdr {
    151       1.1  kefren 	u_short length;
    152       1.1  kefren 	u_char  class_num;
    153       1.1  kefren #define MPLS_STACK_ENTRY_CLASS 1
    154       1.1  kefren 	u_char  c_type;
    155       1.1  kefren #define MPLS_STACK_ENTRY_C_TYPE 1
    156       1.1  kefren };
    157       1.1  kefren 
    158       1.1  kefren struct mpls_extension {
    159       1.1  kefren 	struct icmp_ext_cmn_hdr cmn_hdr;
    160       1.1  kefren 	struct icmp_ext_obj_hdr obj_hdr;
    161       1.1  kefren 	union mpls_shim ms;
    162       1.1  kefren } __packed;
    163       1.1  kefren 
    164       1.3  kefren static void mpls_icmp_error(struct mbuf *, int, int, n_long, int,
    165  1.8.10.1     snj     union mpls_shim *);
    166  1.8.10.1     snj static struct mbuf *ip4_check(struct mbuf *);
    167       1.1  kefren 
    168       1.1  kefren /*
    169  1.8.10.1     snj  * Send an ICMP Extended error message. References: RFC4884 and RFC4950.
    170  1.8.10.1     snj  *
    171       1.1  kefren  * This should be in sync with icmp_error() in sys/netinet/ip_icmp.c
    172  1.8.10.1     snj  * XXX: is called only for ICMP_TIMXCEED_INTRANS but code is too general.
    173  1.8.10.1     snj  *
    174  1.8.10.1     snj  * XXX We're not setting the 'length' field of the Extended ICMP header.
    175  1.8.10.1     snj  * According to RFC4884, we are in 'non-compliant' mode. Moreover, we're
    176  1.8.10.1     snj  * not computing the checksum of the Extended ICMP header.
    177       1.1  kefren  */
    178       1.1  kefren static void
    179       1.1  kefren mpls_icmp_error(struct mbuf *n, int type, int code, n_long dest,
    180       1.1  kefren     int destmtu, union mpls_shim *shim)
    181       1.1  kefren {
    182       1.2  kefren 	struct ip *oip = mtod(n, struct ip *), *nip;
    183       1.2  kefren 	unsigned oiplen = oip->ip_hl << 2;
    184       1.2  kefren 	struct icmp *icp;
    185       1.2  kefren 	struct mbuf *m;
    186       1.2  kefren 	unsigned icmplen, mblen, packetlen;
    187       1.1  kefren 	struct mpls_extension mpls_icmp_ext;
    188  1.8.10.1     snj 
    189       1.1  kefren 	memset(&mpls_icmp_ext, 0, sizeof(mpls_icmp_ext));
    190       1.1  kefren 	mpls_icmp_ext.cmn_hdr.version = ICMP_EXT_VERSION;
    191  1.8.10.1     snj 	mpls_icmp_ext.cmn_hdr.checksum = 0; /* XXX */
    192       1.1  kefren 
    193  1.8.10.1     snj 	mpls_icmp_ext.obj_hdr.length = htons(sizeof(union mpls_shim) +
    194  1.8.10.1     snj 	    sizeof(struct icmp_ext_obj_hdr));
    195       1.1  kefren 	mpls_icmp_ext.obj_hdr.class_num = MPLS_STACK_ENTRY_CLASS;
    196       1.1  kefren 	mpls_icmp_ext.obj_hdr.c_type = MPLS_STACK_ENTRY_C_TYPE;
    197       1.1  kefren 
    198       1.1  kefren 	mpls_icmp_ext.ms.s_addr = shim->s_addr;
    199       1.1  kefren 
    200       1.1  kefren #ifdef ICMPPRINTFS
    201       1.1  kefren 	if (icmpprintfs)
    202       1.1  kefren 		printf("mpls_icmp_error(%p, type:%d, code:%d)\n", oip, type,
    203       1.1  kefren 			code);
    204       1.1  kefren #endif
    205       1.1  kefren 	if (type != ICMP_REDIRECT)
    206       1.1  kefren 		ICMP_STATINC(ICMP_STAT_ERROR);
    207  1.8.10.1     snj 
    208       1.1  kefren 	/*
    209       1.1  kefren 	 * Don't send error if the original packet was encrypted.
    210       1.1  kefren 	 * Don't send error if not the first fragment of message.
    211       1.1  kefren 	 * Don't error if the old packet protocol was ICMP
    212       1.1  kefren 	 * error message, only known informational types.
    213       1.1  kefren 	 */
    214       1.1  kefren 	if (n->m_flags & M_DECRYPTED)
    215       1.1  kefren 		goto freeit;
    216       1.1  kefren 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
    217       1.1  kefren 		goto freeit;
    218       1.1  kefren 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
    219       1.1  kefren 	    n->m_len >= oiplen + ICMP_MINLEN &&
    220       1.1  kefren 	    !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type))
    221       1.1  kefren 	{
    222       1.1  kefren 		ICMP_STATINC(ICMP_STAT_OLDICMP);
    223       1.1  kefren 		goto freeit;
    224       1.1  kefren 	}
    225  1.8.10.1     snj 
    226       1.1  kefren 	/* Don't send error in response to a multicast or broadcast packet */
    227       1.1  kefren 	if (n->m_flags & (M_BCAST|M_MCAST))
    228       1.1  kefren 		goto freeit;
    229       1.1  kefren 
    230       1.1  kefren 	/*
    231       1.1  kefren 	 * First, do a rate limitation check.
    232       1.1  kefren 	 */
    233       1.1  kefren 	if (icmp_ratelimit(&oip->ip_src, type, code))
    234       1.1  kefren 		/* XXX stats */
    235       1.1  kefren 		goto freeit;
    236       1.1  kefren 
    237       1.1  kefren 	/*
    238       1.1  kefren 	 * Now, formulate icmp message
    239       1.1  kefren 	 */
    240       1.1  kefren 	icmplen = min(ICMP_EXT_OFFSET, ntohs(oip->ip_len));
    241  1.8.10.1     snj 
    242       1.1  kefren 	/*
    243       1.1  kefren 	 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
    244       1.1  kefren 	 */
    245       1.1  kefren 	mblen = 0;
    246       1.1  kefren 	for (m = n; m && (mblen < icmplen); m = m->m_next)
    247       1.1  kefren 		mblen += m->m_len;
    248       1.1  kefren 	icmplen = min(mblen, icmplen);
    249       1.1  kefren 
    250       1.4  kefren 	packetlen = sizeof(struct ip) + offsetof(struct icmp, icmp_ip) +
    251       1.4  kefren 	    ICMP_EXT_OFFSET + sizeof(mpls_icmp_ext);
    252       1.2  kefren 
    253       1.1  kefren 	/*
    254       1.1  kefren 	 * As we are not required to return everything we have,
    255       1.1  kefren 	 * we return whatever we can return at ease.
    256       1.1  kefren 	 *
    257       1.1  kefren 	 * Note that ICMP datagrams longer than 576 octets are out of spec
    258       1.1  kefren 	 * according to RFC1812; the limit on icmpreturndatabytes below in
    259       1.1  kefren 	 * icmp_sysctl will keep things below that limit.
    260       1.1  kefren 	 */
    261       1.1  kefren 
    262  1.8.10.1     snj 	KASSERT(packetlen <= MCLBYTES);
    263       1.1  kefren 
    264       1.1  kefren 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    265       1.2  kefren 	if (m && (packetlen > MHLEN)) {
    266       1.1  kefren 		MCLGET(m, M_DONTWAIT);
    267       1.1  kefren 		if ((m->m_flags & M_EXT) == 0) {
    268       1.1  kefren 			m_freem(m);
    269       1.1  kefren 			m = NULL;
    270       1.1  kefren 		}
    271       1.1  kefren 	}
    272       1.1  kefren 	if (m == NULL)
    273       1.1  kefren 		goto freeit;
    274  1.8.10.1     snj 
    275       1.1  kefren 	MCLAIM(m, n->m_owner);
    276       1.2  kefren 	m->m_len = packetlen;
    277       1.1  kefren 	if ((m->m_flags & M_EXT) == 0)
    278       1.1  kefren 		MH_ALIGN(m, m->m_len);
    279       1.4  kefren 	m->m_data += sizeof(struct ip);
    280       1.4  kefren 	m->m_len -= sizeof(struct ip);
    281       1.4  kefren 
    282       1.1  kefren 	icp = mtod(m, struct icmp *);
    283       1.1  kefren 	if ((u_int)type > ICMP_MAXTYPE)
    284       1.4  kefren 		panic("icmp error (mpls_ttl)");
    285       1.1  kefren 	ICMP_STATINC(ICMP_STAT_OUTHIST + type);
    286       1.1  kefren 	icp->icmp_type = type;
    287       1.1  kefren 	if (type == ICMP_REDIRECT)
    288       1.1  kefren 		icp->icmp_gwaddr.s_addr = dest;
    289       1.1  kefren 	else {
    290       1.1  kefren 		icp->icmp_void = 0;
    291       1.1  kefren 		/*
    292       1.1  kefren 		 * The following assignments assume an overlay with the
    293       1.1  kefren 		 * zeroed icmp_void field.
    294       1.1  kefren 		 */
    295       1.1  kefren 		if (type == ICMP_PARAMPROB) {
    296       1.1  kefren 			icp->icmp_pptr = code;
    297       1.1  kefren 			code = 0;
    298       1.1  kefren 		} else if (type == ICMP_UNREACH &&
    299       1.1  kefren 		    code == ICMP_UNREACH_NEEDFRAG && destmtu)
    300       1.1  kefren 			icp->icmp_nextmtu = htons(destmtu);
    301       1.1  kefren 	}
    302       1.1  kefren 
    303       1.1  kefren 	icp->icmp_code = code;
    304       1.2  kefren 
    305       1.2  kefren 	memset(&icp->icmp_ip, 0, ICMP_EXT_OFFSET);
    306       1.1  kefren 	m_copydata(n, 0, icmplen, (char *)&icp->icmp_ip);
    307       1.1  kefren 
    308       1.2  kefren 	/* Append the extension structure */
    309       1.2  kefren 	memcpy(((char*)&icp->icmp_ip) + ICMP_EXT_OFFSET,
    310       1.1  kefren 	    &mpls_icmp_ext, sizeof(mpls_icmp_ext));
    311       1.1  kefren 
    312       1.1  kefren 	/*
    313       1.1  kefren 	 * Now, copy old ip header (without options)
    314       1.1  kefren 	 * in front of icmp message.
    315  1.8.10.1     snj 	 */
    316       1.2  kefren 	if ((m->m_flags & M_EXT) == 0 &&
    317       1.2  kefren 	    m->m_data - sizeof(struct ip) < m->m_pktdat)
    318       1.2  kefren 		panic("icmp len");
    319       1.1  kefren 	m->m_data -= sizeof(struct ip);
    320       1.1  kefren 	m->m_len += sizeof(struct ip);
    321       1.1  kefren 	m->m_pkthdr.len = m->m_len;
    322       1.8   ozaki 	m_copy_rcvif(m, n);
    323       1.1  kefren 	nip = mtod(m, struct ip *);
    324       1.1  kefren 	/* ip_v set in ip_output */
    325       1.1  kefren 	nip->ip_hl = sizeof(struct ip) >> 2;
    326       1.1  kefren 	nip->ip_tos = 0;
    327       1.1  kefren 	nip->ip_len = htons(m->m_len);
    328       1.1  kefren 	/* ip_id set in ip_output */
    329       1.1  kefren 	nip->ip_off = htons(0);
    330       1.1  kefren 	/* ip_ttl set in icmp_reflect */
    331       1.1  kefren 	nip->ip_p = IPPROTO_ICMP;
    332       1.1  kefren 	nip->ip_src = oip->ip_src;
    333       1.1  kefren 	nip->ip_dst = oip->ip_dst;
    334       1.1  kefren 	icmp_reflect(m);
    335       1.1  kefren 
    336       1.1  kefren freeit:
    337       1.1  kefren 	m_freem(n);
    338       1.1  kefren }
    339       1.1  kefren 
    340  1.8.10.1     snj static struct mbuf *
    341       1.3  kefren ip4_check(struct mbuf *m)
    342       1.3  kefren {
    343       1.3  kefren 	struct ip *iph;
    344       1.3  kefren 	int hlen, len;
    345       1.3  kefren 
    346       1.3  kefren 	if (m->m_len < sizeof(struct ip) &&
    347       1.3  kefren 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
    348  1.8.10.1     snj 		return NULL;
    349       1.3  kefren 
    350       1.3  kefren 	iph = mtod(m, struct ip *);
    351       1.3  kefren 
    352       1.3  kefren 	if (iph->ip_v != IPVERSION)
    353       1.3  kefren 		goto freeit;
    354       1.3  kefren 	hlen = iph->ip_hl << 2;
    355       1.3  kefren 	if (hlen < sizeof(struct ip))
    356       1.3  kefren 		goto freeit;
    357       1.3  kefren 	if (hlen > m->m_len) {
    358       1.3  kefren 		if ((m = m_pullup(m, hlen)) == NULL)
    359  1.8.10.1     snj 			return NULL;
    360       1.3  kefren 		iph = mtod(m, struct ip *);
    361       1.3  kefren 	}
    362  1.8.10.1     snj 
    363  1.8.10.1     snj 	/*
    364  1.8.10.1     snj 	 * RFC1122: packets with a multicast source address are
    365  1.8.10.1     snj 	 * not allowed.
    366  1.8.10.1     snj 	 * RFC1122: 127/8 must not appear on wire.
    367  1.8.10.1     snj 	 */
    368       1.3  kefren 	if (IN_MULTICAST(iph->ip_src.s_addr) ||
    369       1.3  kefren 	    (ntohl(iph->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
    370       1.3  kefren 	    (ntohl(iph->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
    371       1.3  kefren 	    in_cksum(m, hlen) != 0)
    372       1.3  kefren 		goto freeit;
    373       1.3  kefren 
    374       1.3  kefren 	len = ntohs(iph->ip_len);
    375       1.3  kefren 	if (len < hlen || m->m_pkthdr.len < len)
    376       1.3  kefren 		goto freeit;
    377       1.3  kefren 
    378  1.8.10.1     snj 	return m;
    379  1.8.10.1     snj 
    380       1.3  kefren freeit:
    381       1.3  kefren 	m_freem(m);
    382  1.8.10.1     snj 	return NULL;
    383       1.3  kefren }
    384       1.1  kefren #endif	/* INET */
    385       1.1  kefren 
    386       1.1  kefren struct mbuf *
    387       1.1  kefren mpls_ttl_dec(struct mbuf *m)
    388       1.1  kefren {
    389       1.1  kefren 	union mpls_shim *mshim;
    390       1.1  kefren #ifdef INET
    391  1.8.10.1     snj 	union mpls_shim top_shim, bos_shim;
    392       1.1  kefren #endif
    393       1.1  kefren 
    394       1.4  kefren 	if (__predict_false(m->m_len < sizeof(union mpls_shim) &&
    395       1.4  kefren 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL))
    396       1.1  kefren 		return NULL;
    397       1.1  kefren 	mshim = mtod(m, union mpls_shim *);
    398       1.1  kefren 	mshim->s_addr = ntohl(mshim->s_addr);
    399       1.1  kefren 	mshim->shim.ttl--;
    400       1.1  kefren 
    401       1.1  kefren 	if (mshim->shim.ttl == 0) {
    402       1.1  kefren 		if (!mpls_icmp_respond) {
    403       1.1  kefren 			m_freem(m);
    404       1.1  kefren 			return NULL;
    405       1.1  kefren 		}
    406       1.1  kefren 
    407       1.1  kefren #ifdef INET
    408       1.1  kefren 		/*
    409  1.8.10.1     snj 		 * Shim ttl exceeded. Send back ICMP type 11 code 0.
    410       1.1  kefren 		 */
    411  1.8.10.1     snj 		bos_shim.s_addr = mshim->s_addr;
    412       1.2  kefren 		top_shim.s_addr = htonl(mshim->s_addr);
    413       1.1  kefren 		m_adj(m, sizeof(union mpls_shim));
    414       1.1  kefren 
    415       1.1  kefren 		/* Goto BOS */
    416  1.8.10.1     snj 		while (bos_shim.shim.bos == 0) {
    417       1.1  kefren 			if (m->m_len < sizeof(union mpls_shim) &&
    418       1.1  kefren 			    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
    419       1.1  kefren 				m_freem(m);
    420       1.1  kefren 				return NULL;
    421       1.1  kefren 			}
    422  1.8.10.1     snj 			bos_shim.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    423       1.1  kefren 			m_adj(m, sizeof(union mpls_shim));
    424       1.1  kefren 		}
    425  1.8.10.1     snj 
    426  1.8.10.1     snj 		if ((m = ip4_check(m)) != NULL)
    427       1.3  kefren 			mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
    428       1.3  kefren 			    0, 0, &top_shim);
    429       1.2  kefren #else
    430       1.2  kefren 		m_freem(m);
    431       1.1  kefren #endif
    432       1.1  kefren 		return NULL;
    433       1.1  kefren 	}
    434       1.1  kefren 
    435       1.1  kefren 	mshim->s_addr = htonl(mshim->s_addr);
    436       1.1  kefren 
    437       1.1  kefren 	return m;
    438       1.1  kefren }
    439