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