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