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