xref: /netbsd-src/sys/netmpls/mpls_ttl.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: mpls_ttl.c,v 1.4 2013/08/07 06:55:00 kefren 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@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 2013/08/07 06:55:00 kefren 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 bool 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 
332 static bool
333 ip4_check(struct mbuf *m)
334 {
335 	struct ip *iph;
336 	int hlen, len;
337 
338 	if (m->m_len < sizeof(struct ip) &&
339 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
340 		return false;
341 
342 	iph = mtod(m, struct ip *);
343 
344 	if (iph->ip_v != IPVERSION)
345 		goto freeit;
346 	hlen = iph->ip_hl << 2;
347 	if (hlen < sizeof(struct ip))
348 		goto freeit;
349 	if (hlen > m->m_len) {
350 		if ((m = m_pullup(m, hlen)) == NULL)
351 			return false;
352 		iph = mtod(m, struct ip *);
353 	}
354 	if (IN_MULTICAST(iph->ip_src.s_addr) ||
355 	    (ntohl(iph->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
356 	    (ntohl(iph->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
357 	    in_cksum(m, hlen) != 0)
358 		goto freeit;
359 
360 	len = ntohs(iph->ip_len);
361 	if (len < hlen || m->m_pkthdr.len < len)
362 		goto freeit;
363 
364 	return true;
365 freeit:
366 	m_freem(m);
367 	return false;
368 
369 }
370 
371 #endif	/* INET */
372 
373 struct mbuf *
374 mpls_ttl_dec(struct mbuf *m)
375 {
376 	union mpls_shim *mshim;
377 #ifdef INET
378 	union mpls_shim top_shim, bossh;
379 #endif
380 
381 	if (__predict_false(m->m_len < sizeof(union mpls_shim) &&
382 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL))
383 		return NULL;
384 	mshim = mtod(m, union mpls_shim *);
385 	mshim->s_addr = ntohl(mshim->s_addr);
386 	mshim->shim.ttl--;
387 
388 	if (mshim->shim.ttl == 0) {
389 		if (!mpls_icmp_respond) {
390 			m_freem(m);
391 			return NULL;
392 		}
393 
394 #ifdef INET
395 		/*
396 		 * shim ttl exceeded
397 		 * send back ICMP type 11 code 0
398 		 */
399 		bossh.s_addr = mshim->s_addr;
400 		top_shim.s_addr = htonl(mshim->s_addr);
401 		m_adj(m, sizeof(union mpls_shim));
402 
403 		/* Goto BOS */
404 		while(bossh.shim.bos == 0) {
405 			if (m->m_len < sizeof(union mpls_shim) &&
406 			    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
407 				m_freem(m);
408 				return NULL;
409 			}
410 			bossh.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
411 			m_adj(m, sizeof(union mpls_shim));
412 		}
413 
414 		if (ip4_check(m) == true)
415 			mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
416 			    0, 0, &top_shim);
417 #else
418 		m_freem(m);
419 #endif
420 		return NULL;
421 	}
422 
423 	mshim->s_addr = htonl(mshim->s_addr);
424 
425 	return m;
426 }
427