xref: /netbsd-src/sys/netmpls/mpls_ttl.c (revision 506a6ab369065dfaab6d7f236d4d35260e8bb801)
1 /*	$NetBSD: mpls_ttl.c,v 1.15 2018/12/27 14:03:55 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.15 2018/12/27 14:03:55 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
mpls_icmp_error(struct mbuf * n,int type,int code,n_long dest,int destmtu,union mpls_shim * shim)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 	if (type != ICMP_REDIRECT)
181 		ICMP_STATINC(ICMP_STAT_ERROR);
182 
183 	/*
184 	 * Don't send error if the original packet was encrypted.
185 	 * Don't send error if not the first fragment of message.
186 	 * Don't error if the old packet protocol was ICMP
187 	 * error message, only known informational types.
188 	 */
189 	if (n->m_flags & M_DECRYPTED)
190 		goto freeit;
191 	if (oip->ip_off &~ htons(IP_MF|IP_DF))
192 		goto freeit;
193 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
194 	    n->m_len >= oiplen + ICMP_MINLEN &&
195 	    !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type))
196 	{
197 		ICMP_STATINC(ICMP_STAT_OLDICMP);
198 		goto freeit;
199 	}
200 
201 	/* Don't send error in response to a multicast or broadcast packet */
202 	if (n->m_flags & (M_BCAST|M_MCAST))
203 		goto freeit;
204 
205 	/*
206 	 * First, do a rate limitation check.
207 	 */
208 	if (icmp_ratelimit(&oip->ip_src, type, code))
209 		/* XXX stats */
210 		goto freeit;
211 
212 	/*
213 	 * Now, formulate icmp message
214 	 */
215 	icmplen = uimin(ICMP_EXT_OFFSET, ntohs(oip->ip_len));
216 
217 	/*
218 	 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
219 	 */
220 	mblen = 0;
221 	for (m = n; m && (mblen < icmplen); m = m->m_next)
222 		mblen += m->m_len;
223 	icmplen = uimin(mblen, icmplen);
224 
225 	packetlen = sizeof(struct ip) + offsetof(struct icmp, icmp_ip) +
226 	    ICMP_EXT_OFFSET + sizeof(mpls_icmp_ext);
227 
228 	/*
229 	 * As we are not required to return everything we have,
230 	 * we return whatever we can return at ease.
231 	 *
232 	 * Note that ICMP datagrams longer than 576 octets are out of spec
233 	 * according to RFC1812; the limit on icmpreturndatabytes below in
234 	 * icmp_sysctl will keep things below that limit.
235 	 */
236 
237 	KASSERT(packetlen <= MCLBYTES);
238 
239 	m = m_gethdr(M_DONTWAIT, MT_HEADER);
240 	if (m && (packetlen > MHLEN)) {
241 		MCLGET(m, M_DONTWAIT);
242 		if ((m->m_flags & M_EXT) == 0) {
243 			m_freem(m);
244 			m = NULL;
245 		}
246 	}
247 	if (m == NULL)
248 		goto freeit;
249 
250 	MCLAIM(m, n->m_owner);
251 	m->m_len = packetlen;
252 	if ((m->m_flags & M_EXT) == 0)
253 		m_align(m, m->m_len);
254 	m->m_data += sizeof(struct ip);
255 	m->m_len -= sizeof(struct ip);
256 
257 	icp = mtod(m, struct icmp *);
258 	if ((u_int)type > ICMP_MAXTYPE)
259 		panic("icmp error (mpls_ttl)");
260 	ICMP_STATINC(ICMP_STAT_OUTHIST + type);
261 	icp->icmp_type = type;
262 	if (type == ICMP_REDIRECT)
263 		icp->icmp_gwaddr.s_addr = dest;
264 	else {
265 		icp->icmp_void = 0;
266 		/*
267 		 * The following assignments assume an overlay with the
268 		 * zeroed icmp_void field.
269 		 */
270 		if (type == ICMP_PARAMPROB) {
271 			icp->icmp_pptr = code;
272 			code = 0;
273 		} else if (type == ICMP_UNREACH &&
274 		    code == ICMP_UNREACH_NEEDFRAG && destmtu)
275 			icp->icmp_nextmtu = htons(destmtu);
276 	}
277 
278 	icp->icmp_code = code;
279 
280 	memset(&icp->icmp_ip, 0, ICMP_EXT_OFFSET);
281 	m_copydata(n, 0, icmplen, (char *)&icp->icmp_ip);
282 
283 	/* Append the extension structure */
284 	memcpy(((char*)&icp->icmp_ip) + ICMP_EXT_OFFSET,
285 	    &mpls_icmp_ext, sizeof(mpls_icmp_ext));
286 
287 	/*
288 	 * Now, copy old ip header (without options)
289 	 * in front of icmp message.
290 	 */
291 	if ((m->m_flags & M_EXT) == 0 &&
292 	    m->m_data - sizeof(struct ip) < m->m_pktdat)
293 		panic("icmp len");
294 	m->m_data -= sizeof(struct ip);
295 	m->m_len += sizeof(struct ip);
296 	m->m_pkthdr.len = m->m_len;
297 	m_copy_rcvif(m, n);
298 	nip = mtod(m, struct ip *);
299 	/* ip_v set in ip_output */
300 	nip->ip_hl = sizeof(struct ip) >> 2;
301 	nip->ip_tos = 0;
302 	nip->ip_len = htons(m->m_len);
303 	/* ip_id set in ip_output */
304 	nip->ip_off = htons(0);
305 	/* ip_ttl set in icmp_reflect */
306 	nip->ip_p = IPPROTO_ICMP;
307 	nip->ip_src = oip->ip_src;
308 	nip->ip_dst = oip->ip_dst;
309 	icmp_reflect(m);
310 
311 freeit:
312 	m_freem(n);
313 }
314 
315 static struct mbuf *
ip4_check(struct mbuf * m)316 ip4_check(struct mbuf *m)
317 {
318 	struct ip *iph;
319 	int hlen, len;
320 
321 	if (m->m_len < sizeof(struct ip) &&
322 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
323 		return NULL;
324 
325 	iph = mtod(m, struct ip *);
326 
327 	if (iph->ip_v != IPVERSION)
328 		goto freeit;
329 	hlen = iph->ip_hl << 2;
330 	if (hlen < sizeof(struct ip))
331 		goto freeit;
332 	if (hlen > m->m_len) {
333 		if ((m = m_pullup(m, hlen)) == NULL)
334 			return NULL;
335 		iph = mtod(m, struct ip *);
336 	}
337 
338 	/*
339 	 * RFC1122: packets with a multicast source address are
340 	 * not allowed.
341 	 * RFC1122: 127/8 must not appear on wire.
342 	 */
343 	if (IN_MULTICAST(iph->ip_src.s_addr) ||
344 	    (ntohl(iph->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
345 	    (ntohl(iph->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
346 	    in_cksum(m, hlen) != 0)
347 		goto freeit;
348 
349 	len = ntohs(iph->ip_len);
350 	if (len < hlen || m->m_pkthdr.len < len)
351 		goto freeit;
352 
353 	return m;
354 
355 freeit:
356 	m_freem(m);
357 	return NULL;
358 }
359 #endif	/* INET */
360 
361 struct mbuf *
mpls_ttl_dec(struct mbuf * m)362 mpls_ttl_dec(struct mbuf *m)
363 {
364 	union mpls_shim *mshim;
365 #ifdef INET
366 	union mpls_shim top_shim, bos_shim;
367 #endif
368 
369 	if (__predict_false(m->m_len < sizeof(union mpls_shim) &&
370 	    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL))
371 		return NULL;
372 	mshim = mtod(m, union mpls_shim *);
373 	mshim->s_addr = ntohl(mshim->s_addr);
374 	mshim->shim.ttl--;
375 
376 	if (mshim->shim.ttl == 0) {
377 		if (!mpls_icmp_respond) {
378 			m_freem(m);
379 			return NULL;
380 		}
381 
382 #ifdef INET
383 		/*
384 		 * Shim ttl exceeded. Send back ICMP type 11 code 0.
385 		 */
386 		bos_shim.s_addr = mshim->s_addr;
387 		top_shim.s_addr = htonl(mshim->s_addr);
388 		m_adj(m, sizeof(union mpls_shim));
389 
390 		/* Goto BOS */
391 		while (bos_shim.shim.bos == 0) {
392 			if (m->m_len < sizeof(union mpls_shim) &&
393 			    (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
394 				m_freem(m);
395 				return NULL;
396 			}
397 			bos_shim.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
398 			m_adj(m, sizeof(union mpls_shim));
399 		}
400 
401 		if ((m = ip4_check(m)) != NULL)
402 			mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
403 			    0, 0, &top_shim);
404 #else
405 		m_freem(m);
406 #endif
407 		return NULL;
408 	}
409 
410 	mshim->s_addr = htonl(mshim->s_addr);
411 
412 	return m;
413 }
414