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