mpls_ttl.c revision 1.8.10.1 1 /* $NetBSD: mpls_ttl.c,v 1.8.10.1 2018/02/12 18:18:00 snj 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 (at) 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.8.10.1 2018/02/12 18:18:00 snj 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
134 #define ICMP_EXT_VERSION 2
135 #define MPLS_RETURN_DATA 128
136 #define ICMP_EXT_OFFSET 128
137
138 struct icmp_ext_cmn_hdr {
139 #if BYTE_ORDER == BIG_ENDIAN
140 unsigned char version:4;
141 unsigned char reserved1:4;
142 #else
143 unsigned char reserved1:4;
144 unsigned char version:4;
145 #endif
146 unsigned char reserved2;
147 unsigned short checksum;
148 };
149
150 struct icmp_ext_obj_hdr {
151 u_short length;
152 u_char class_num;
153 #define MPLS_STACK_ENTRY_CLASS 1
154 u_char c_type;
155 #define MPLS_STACK_ENTRY_C_TYPE 1
156 };
157
158 struct mpls_extension {
159 struct icmp_ext_cmn_hdr cmn_hdr;
160 struct icmp_ext_obj_hdr obj_hdr;
161 union mpls_shim ms;
162 } __packed;
163
164 static void mpls_icmp_error(struct mbuf *, int, int, n_long, int,
165 union mpls_shim *);
166 static struct mbuf *ip4_check(struct mbuf *);
167
168 /*
169 * Send an ICMP Extended error message. References: RFC4884 and RFC4950.
170 *
171 * This should be in sync with icmp_error() in sys/netinet/ip_icmp.c
172 * XXX: is called only for ICMP_TIMXCEED_INTRANS but code is too general.
173 *
174 * XXX We're not setting the 'length' field of the Extended ICMP header.
175 * According to RFC4884, we are in 'non-compliant' mode. Moreover, we're
176 * not computing the checksum of the Extended ICMP header.
177 */
178 static void
179 mpls_icmp_error(struct mbuf *n, int type, int code, n_long dest,
180 int destmtu, union mpls_shim *shim)
181 {
182 struct ip *oip = mtod(n, struct ip *), *nip;
183 unsigned oiplen = oip->ip_hl << 2;
184 struct icmp *icp;
185 struct mbuf *m;
186 unsigned icmplen, mblen, packetlen;
187 struct mpls_extension mpls_icmp_ext;
188
189 memset(&mpls_icmp_ext, 0, sizeof(mpls_icmp_ext));
190 mpls_icmp_ext.cmn_hdr.version = ICMP_EXT_VERSION;
191 mpls_icmp_ext.cmn_hdr.checksum = 0; /* XXX */
192
193 mpls_icmp_ext.obj_hdr.length = htons(sizeof(union mpls_shim) +
194 sizeof(struct icmp_ext_obj_hdr));
195 mpls_icmp_ext.obj_hdr.class_num = MPLS_STACK_ENTRY_CLASS;
196 mpls_icmp_ext.obj_hdr.c_type = MPLS_STACK_ENTRY_C_TYPE;
197
198 mpls_icmp_ext.ms.s_addr = shim->s_addr;
199
200 #ifdef ICMPPRINTFS
201 if (icmpprintfs)
202 printf("mpls_icmp_error(%p, type:%d, code:%d)\n", oip, type,
203 code);
204 #endif
205 if (type != ICMP_REDIRECT)
206 ICMP_STATINC(ICMP_STAT_ERROR);
207
208 /*
209 * Don't send error if the original packet was encrypted.
210 * Don't send error if not the first fragment of message.
211 * Don't error if the old packet protocol was ICMP
212 * error message, only known informational types.
213 */
214 if (n->m_flags & M_DECRYPTED)
215 goto freeit;
216 if (oip->ip_off &~ htons(IP_MF|IP_DF))
217 goto freeit;
218 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
219 n->m_len >= oiplen + ICMP_MINLEN &&
220 !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type))
221 {
222 ICMP_STATINC(ICMP_STAT_OLDICMP);
223 goto freeit;
224 }
225
226 /* Don't send error in response to a multicast or broadcast packet */
227 if (n->m_flags & (M_BCAST|M_MCAST))
228 goto freeit;
229
230 /*
231 * First, do a rate limitation check.
232 */
233 if (icmp_ratelimit(&oip->ip_src, type, code))
234 /* XXX stats */
235 goto freeit;
236
237 /*
238 * Now, formulate icmp message
239 */
240 icmplen = min(ICMP_EXT_OFFSET, ntohs(oip->ip_len));
241
242 /*
243 * Defend against mbuf chains shorter than oip->ip_len - oiplen:
244 */
245 mblen = 0;
246 for (m = n; m && (mblen < icmplen); m = m->m_next)
247 mblen += m->m_len;
248 icmplen = min(mblen, icmplen);
249
250 packetlen = sizeof(struct ip) + offsetof(struct icmp, icmp_ip) +
251 ICMP_EXT_OFFSET + sizeof(mpls_icmp_ext);
252
253 /*
254 * As we are not required to return everything we have,
255 * we return whatever we can return at ease.
256 *
257 * Note that ICMP datagrams longer than 576 octets are out of spec
258 * according to RFC1812; the limit on icmpreturndatabytes below in
259 * icmp_sysctl will keep things below that limit.
260 */
261
262 KASSERT(packetlen <= MCLBYTES);
263
264 m = m_gethdr(M_DONTWAIT, MT_HEADER);
265 if (m && (packetlen > MHLEN)) {
266 MCLGET(m, M_DONTWAIT);
267 if ((m->m_flags & M_EXT) == 0) {
268 m_freem(m);
269 m = NULL;
270 }
271 }
272 if (m == NULL)
273 goto freeit;
274
275 MCLAIM(m, n->m_owner);
276 m->m_len = packetlen;
277 if ((m->m_flags & M_EXT) == 0)
278 MH_ALIGN(m, m->m_len);
279 m->m_data += sizeof(struct ip);
280 m->m_len -= sizeof(struct ip);
281
282 icp = mtod(m, struct icmp *);
283 if ((u_int)type > ICMP_MAXTYPE)
284 panic("icmp error (mpls_ttl)");
285 ICMP_STATINC(ICMP_STAT_OUTHIST + type);
286 icp->icmp_type = type;
287 if (type == ICMP_REDIRECT)
288 icp->icmp_gwaddr.s_addr = dest;
289 else {
290 icp->icmp_void = 0;
291 /*
292 * The following assignments assume an overlay with the
293 * zeroed icmp_void field.
294 */
295 if (type == ICMP_PARAMPROB) {
296 icp->icmp_pptr = code;
297 code = 0;
298 } else if (type == ICMP_UNREACH &&
299 code == ICMP_UNREACH_NEEDFRAG && destmtu)
300 icp->icmp_nextmtu = htons(destmtu);
301 }
302
303 icp->icmp_code = code;
304
305 memset(&icp->icmp_ip, 0, ICMP_EXT_OFFSET);
306 m_copydata(n, 0, icmplen, (char *)&icp->icmp_ip);
307
308 /* Append the extension structure */
309 memcpy(((char*)&icp->icmp_ip) + ICMP_EXT_OFFSET,
310 &mpls_icmp_ext, sizeof(mpls_icmp_ext));
311
312 /*
313 * Now, copy old ip header (without options)
314 * in front of icmp message.
315 */
316 if ((m->m_flags & M_EXT) == 0 &&
317 m->m_data - sizeof(struct ip) < m->m_pktdat)
318 panic("icmp len");
319 m->m_data -= sizeof(struct ip);
320 m->m_len += sizeof(struct ip);
321 m->m_pkthdr.len = m->m_len;
322 m_copy_rcvif(m, n);
323 nip = mtod(m, struct ip *);
324 /* ip_v set in ip_output */
325 nip->ip_hl = sizeof(struct ip) >> 2;
326 nip->ip_tos = 0;
327 nip->ip_len = htons(m->m_len);
328 /* ip_id set in ip_output */
329 nip->ip_off = htons(0);
330 /* ip_ttl set in icmp_reflect */
331 nip->ip_p = IPPROTO_ICMP;
332 nip->ip_src = oip->ip_src;
333 nip->ip_dst = oip->ip_dst;
334 icmp_reflect(m);
335
336 freeit:
337 m_freem(n);
338 }
339
340 static struct mbuf *
341 ip4_check(struct mbuf *m)
342 {
343 struct ip *iph;
344 int hlen, len;
345
346 if (m->m_len < sizeof(struct ip) &&
347 (m = m_pullup(m, sizeof(struct ip))) == NULL)
348 return NULL;
349
350 iph = mtod(m, struct ip *);
351
352 if (iph->ip_v != IPVERSION)
353 goto freeit;
354 hlen = iph->ip_hl << 2;
355 if (hlen < sizeof(struct ip))
356 goto freeit;
357 if (hlen > m->m_len) {
358 if ((m = m_pullup(m, hlen)) == NULL)
359 return NULL;
360 iph = mtod(m, struct ip *);
361 }
362
363 /*
364 * RFC1122: packets with a multicast source address are
365 * not allowed.
366 * RFC1122: 127/8 must not appear on wire.
367 */
368 if (IN_MULTICAST(iph->ip_src.s_addr) ||
369 (ntohl(iph->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
370 (ntohl(iph->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
371 in_cksum(m, hlen) != 0)
372 goto freeit;
373
374 len = ntohs(iph->ip_len);
375 if (len < hlen || m->m_pkthdr.len < len)
376 goto freeit;
377
378 return m;
379
380 freeit:
381 m_freem(m);
382 return NULL;
383 }
384 #endif /* INET */
385
386 struct mbuf *
387 mpls_ttl_dec(struct mbuf *m)
388 {
389 union mpls_shim *mshim;
390 #ifdef INET
391 union mpls_shim top_shim, bos_shim;
392 #endif
393
394 if (__predict_false(m->m_len < sizeof(union mpls_shim) &&
395 (m = m_pullup(m, sizeof(union mpls_shim))) == NULL))
396 return NULL;
397 mshim = mtod(m, union mpls_shim *);
398 mshim->s_addr = ntohl(mshim->s_addr);
399 mshim->shim.ttl--;
400
401 if (mshim->shim.ttl == 0) {
402 if (!mpls_icmp_respond) {
403 m_freem(m);
404 return NULL;
405 }
406
407 #ifdef INET
408 /*
409 * Shim ttl exceeded. Send back ICMP type 11 code 0.
410 */
411 bos_shim.s_addr = mshim->s_addr;
412 top_shim.s_addr = htonl(mshim->s_addr);
413 m_adj(m, sizeof(union mpls_shim));
414
415 /* Goto BOS */
416 while (bos_shim.shim.bos == 0) {
417 if (m->m_len < sizeof(union mpls_shim) &&
418 (m = m_pullup(m, sizeof(union mpls_shim))) == NULL) {
419 m_freem(m);
420 return NULL;
421 }
422 bos_shim.s_addr = ntohl(mtod(m, union mpls_shim *)->s_addr);
423 m_adj(m, sizeof(union mpls_shim));
424 }
425
426 if ((m = ip4_check(m)) != NULL)
427 mpls_icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
428 0, 0, &top_shim);
429 #else
430 m_freem(m);
431 #endif
432 return NULL;
433 }
434
435 mshim->s_addr = htonl(mshim->s_addr);
436
437 return m;
438 }
439