Home | History | Annotate | Line # | Download | only in netmpls
mpls_ttl.c revision 1.2.2.2
      1 /*	$NetBSD: mpls_ttl.c,v 1.2.2.2 2010/07/03 01:20:00 rmind 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.2.2.2 2010/07/03 01:20:00 rmind 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, union mpls_shim *);
    164 
    165 /*
    166  * http://www.ietf.org/proceedings/01aug/I-D/draft-ietf-mpls-icmp-02.txt
    167  * This should be in sync with icmp_error() in sys/netinet/ip_icmp.c
    168  */
    169 
    170 static void
    171 mpls_icmp_error(struct mbuf *n, int type, int code, n_long dest,
    172     int destmtu, union mpls_shim *shim)
    173 {
    174 	struct ip *oip = mtod(n, struct ip *), *nip;
    175 	unsigned oiplen = oip->ip_hl << 2;
    176 	struct icmp *icp;
    177 	struct mbuf *m;
    178 	unsigned icmplen, mblen, packetlen;
    179 	struct mpls_extension mpls_icmp_ext;
    180 
    181 	memset(&mpls_icmp_ext, 0, sizeof(mpls_icmp_ext));
    182 	mpls_icmp_ext.cmn_hdr.version = ICMP_EXT_VERSION;
    183 	mpls_icmp_ext.cmn_hdr.checksum = 0;
    184 
    185 	mpls_icmp_ext.obj_hdr.length = htons(sizeof(union mpls_shim) +
    186 					sizeof(struct icmp_ext_obj_hdr));
    187 	mpls_icmp_ext.obj_hdr.class_num = MPLS_STACK_ENTRY_CLASS;
    188 	mpls_icmp_ext.obj_hdr.c_type = MPLS_STACK_ENTRY_C_TYPE;
    189 
    190 	mpls_icmp_ext.ms.s_addr = shim->s_addr;
    191 
    192 #ifdef ICMPPRINTFS
    193 	if (icmpprintfs)
    194 		printf("mpls_icmp_error(%p, type:%d, code:%d)\n", oip, type,
    195 			code);
    196 #endif
    197 	if (type != ICMP_REDIRECT)
    198 		ICMP_STATINC(ICMP_STAT_ERROR);
    199 	/*
    200 	 * Don't send error if the original packet was encrypted.
    201 	 * Don't send error if not the first fragment of message.
    202 	 * Don't error if the old packet protocol was ICMP
    203 	 * error message, only known informational types.
    204 	 */
    205 	if (n->m_flags & M_DECRYPTED)
    206 		goto freeit;
    207 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
    208 		goto freeit;
    209 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
    210 	    n->m_len >= oiplen + ICMP_MINLEN &&
    211 	    !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type))
    212 	{
    213 		ICMP_STATINC(ICMP_STAT_OLDICMP);
    214 		goto freeit;
    215 	}
    216 	/* Don't send error in response to a multicast or broadcast packet */
    217 	if (n->m_flags & (M_BCAST|M_MCAST))
    218 		goto freeit;
    219 
    220 	/*
    221 	 * First, do a rate limitation check.
    222 	 */
    223 	if (icmp_ratelimit(&oip->ip_src, type, code))
    224 		/* XXX stats */
    225 		goto freeit;
    226 
    227 	/*
    228 	 * Now, formulate icmp message
    229 	 */
    230 	icmplen = min(ICMP_EXT_OFFSET, ntohs(oip->ip_len));
    231 	/*
    232 	 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
    233 	 */
    234 	mblen = 0;
    235 	for (m = n; m && (mblen < icmplen); m = m->m_next)
    236 		mblen += m->m_len;
    237 	icmplen = min(mblen, icmplen);
    238 
    239 	packetlen = sizeof(struct ip) + ICMP_EXT_OFFSET + sizeof(mpls_icmp_ext);
    240 
    241 	/*
    242 	 * As we are not required to return everything we have,
    243 	 * we return whatever we can return at ease.
    244 	 *
    245 	 * Note that ICMP datagrams longer than 576 octets are out of spec
    246 	 * according to RFC1812; the limit on icmpreturndatabytes below in
    247 	 * icmp_sysctl will keep things below that limit.
    248 	 */
    249 
    250 	KASSERT (packetlen <= MCLBYTES);
    251 
    252 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
    253 	if (m && (packetlen > MHLEN)) {
    254 		MCLGET(m, M_DONTWAIT);
    255 		if ((m->m_flags & M_EXT) == 0) {
    256 			m_freem(m);
    257 			m = NULL;
    258 		}
    259 	}
    260 	if (m == NULL)
    261 		goto freeit;
    262 	MCLAIM(m, n->m_owner);
    263 	m->m_len = packetlen;
    264 	if ((m->m_flags & M_EXT) == 0)
    265 		MH_ALIGN(m, m->m_len);
    266 	else {
    267 		m->m_data += sizeof(struct ip);
    268 		m->m_len -= sizeof(struct ip);
    269 	}
    270 	icp = mtod(m, struct icmp *);
    271 	if ((u_int)type > ICMP_MAXTYPE)
    272 		panic("icmp error");
    273 	ICMP_STATINC(ICMP_STAT_OUTHIST + type);
    274 	icp->icmp_type = type;
    275 	if (type == ICMP_REDIRECT)
    276 		icp->icmp_gwaddr.s_addr = dest;
    277 	else {
    278 		icp->icmp_void = 0;
    279 		/*
    280 		 * The following assignments assume an overlay with the
    281 		 * zeroed icmp_void field.
    282 		 */
    283 		if (type == ICMP_PARAMPROB) {
    284 			icp->icmp_pptr = code;
    285 			code = 0;
    286 		} else if (type == ICMP_UNREACH &&
    287 		    code == ICMP_UNREACH_NEEDFRAG && destmtu)
    288 			icp->icmp_nextmtu = htons(destmtu);
    289 	}
    290 
    291 	icp->icmp_code = code;
    292 
    293 	memset(&icp->icmp_ip, 0, ICMP_EXT_OFFSET);
    294 	m_copydata(n, 0, icmplen, (char *)&icp->icmp_ip);
    295 
    296 	/* Append the extension structure */
    297 	memcpy(((char*)&icp->icmp_ip) + ICMP_EXT_OFFSET,
    298 	    &mpls_icmp_ext, sizeof(mpls_icmp_ext));
    299 
    300 	/*
    301 	 * Now, copy old ip header (without options)
    302 	 * in front of icmp message.
    303 	*/
    304 	if ((m->m_flags & M_EXT) == 0 &&
    305 	    m->m_data - sizeof(struct ip) < m->m_pktdat)
    306 		panic("icmp len");
    307 	m->m_data -= sizeof(struct ip);
    308 	m->m_len += sizeof(struct ip);
    309 	m->m_pkthdr.len = m->m_len;
    310 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
    311 	nip = mtod(m, struct ip *);
    312 	/* ip_v set in ip_output */
    313 	nip->ip_hl = sizeof(struct ip) >> 2;
    314 	nip->ip_tos = 0;
    315 	nip->ip_len = htons(m->m_len);
    316 	/* ip_id set in ip_output */
    317 	nip->ip_off = htons(0);
    318 	/* ip_ttl set in icmp_reflect */
    319 	nip->ip_p = IPPROTO_ICMP;
    320 	nip->ip_src = oip->ip_src;
    321 	nip->ip_dst = oip->ip_dst;
    322 	icmp_reflect(m);
    323 
    324 freeit:
    325 	m_freem(n);
    326 
    327 }
    328 
    329 #endif	/* INET */
    330 
    331 struct mbuf *
    332 mpls_ttl_dec(struct mbuf *m)
    333 {
    334 	union mpls_shim *mshim;
    335 #ifdef INET
    336 	union mpls_shim top_shim, bossh;
    337 #endif
    338 
    339 	if (m->m_len < sizeof(union mpls_shim) &&
    340 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL)
    341 		return NULL;
    342 	mshim = mtod(m, union mpls_shim *);
    343 	mshim->s_addr = ntohl(mshim->s_addr);
    344 	mshim->shim.ttl--;
    345 
    346 	if (mshim->shim.ttl == 0) {
    347 		if (!mpls_icmp_respond) {
    348 			m_freem(m);
    349 			return NULL;
    350 		}
    351 
    352 #ifdef INET
    353 		/*
    354 		 * shim ttl exceeded
    355 		 * send back ICMP type 11 code 0
    356 		 */
    357 		bossh.s_addr = mshim->s_addr;
    358 		top_shim.s_addr = htonl(mshim->s_addr);
    359 		m_adj(m, sizeof(union mpls_shim));
    360 
    361 		/* Goto BOS */
    362 		while(bossh.shim.bos == 0) {
    363 			if (m->m_len < sizeof(union mpls_shim) &&
    364 			    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
    365 				m_freem(m);
    366 				return NULL;
    367 			}
    368 			bossh.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
    369 			m_adj(m, sizeof(union mpls_shim));
    370 		}
    371 
    372 		mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
    373 		    0, 0, &top_shim);
    374 #else
    375 		m_freem(m);
    376 #endif
    377 		return NULL;
    378 	}
    379 
    380 	mshim->s_addr = htonl(mshim->s_addr);
    381 
    382 	return m;
    383 }
    384