1 /* $NetBSD: ip_icmp.c,v 1.177 2018/12/22 14:28:57 maxv Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2000 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 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the project nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65 /* 66 * Copyright (c) 1982, 1986, 1988, 1993 67 * The Regents of the University of California. All rights reserved. 68 * 69 * Redistribution and use in source and binary forms, with or without 70 * modification, are permitted provided that the following conditions 71 * are met: 72 * 1. Redistributions of source code must retain the above copyright 73 * notice, this list of conditions and the following disclaimer. 74 * 2. Redistributions in binary form must reproduce the above copyright 75 * notice, this list of conditions and the following disclaimer in the 76 * documentation and/or other materials provided with the distribution. 77 * 3. Neither the name of the University nor the names of its contributors 78 * may be used to endorse or promote products derived from this software 79 * without specific prior written permission. 80 * 81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 91 * SUCH DAMAGE. 92 * 93 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 94 */ 95 96 #include <sys/cdefs.h> 97 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.177 2018/12/22 14:28:57 maxv Exp $"); 98 99 #ifdef _KERNEL_OPT 100 #include "opt_ipsec.h" 101 #endif 102 103 #include <sys/param.h> 104 #include <sys/systm.h> 105 #include <sys/mbuf.h> 106 #include <sys/protosw.h> 107 #include <sys/socket.h> 108 #include <sys/socketvar.h> /* For softnet_lock */ 109 #include <sys/kmem.h> 110 #include <sys/time.h> 111 #include <sys/kernel.h> 112 #include <sys/syslog.h> 113 #include <sys/sysctl.h> 114 115 #include <net/if.h> 116 #include <net/route.h> 117 118 #include <netinet/in.h> 119 #include <netinet/in_systm.h> 120 #include <netinet/in_var.h> 121 #include <netinet/ip.h> 122 #include <netinet/ip_icmp.h> 123 #include <netinet/ip_var.h> 124 #include <netinet/in_pcb.h> 125 #include <netinet/in_proto.h> 126 #include <netinet/icmp_var.h> 127 #include <netinet/icmp_private.h> 128 #include <netinet/wqinput.h> 129 130 #ifdef IPSEC 131 #include <netipsec/ipsec.h> 132 #include <netipsec/key.h> 133 #endif 134 135 /* 136 * ICMP routines: error generation, receive packet processing, and 137 * routines to turnaround packets back to the originator, and 138 * host table maintenance routines. 139 */ 140 141 int icmpmaskrepl = 0; 142 int icmpbmcastecho = 0; 143 int icmpreturndatabytes = 8; 144 145 percpu_t *icmpstat_percpu; 146 147 /* 148 * List of callbacks to notify when Path MTU changes are made. 149 */ 150 struct icmp_mtudisc_callback { 151 LIST_ENTRY(icmp_mtudisc_callback) mc_list; 152 void (*mc_func)(struct in_addr); 153 }; 154 155 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks = 156 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks); 157 158 /* unused... */ 159 u_int ip_next_mtu(u_int, int); 160 161 static int icmperrppslim = 100; /* 100pps */ 162 static int icmperrpps_count = 0; 163 static struct timeval icmperrppslim_last; 164 static int icmp_rediraccept = 1; 165 static int icmp_redirtimeout = 600; 166 static struct rttimer_queue *icmp_redirect_timeout_q = NULL; 167 168 /* Protect mtudisc and redirect stuff */ 169 static kmutex_t icmp_mtx __cacheline_aligned; 170 171 static void icmp_send(struct mbuf *, struct mbuf *); 172 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *); 173 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *); 174 175 static void sysctl_netinet_icmp_setup(struct sysctllog **); 176 177 /* workqueue-based pr_input */ 178 static struct wqinput *icmp_wqinput; 179 static void _icmp_input(struct mbuf *, int, int); 180 181 void 182 icmp_init(void) 183 { 184 185 sysctl_netinet_icmp_setup(NULL); 186 187 mutex_init(&icmp_mtx, MUTEX_DEFAULT, IPL_NONE); 188 /* 189 * This is only useful if the user initializes redirtimeout to 190 * something other than zero. 191 */ 192 mutex_enter(&icmp_mtx); 193 icmp_redirect_timeout_q = rt_timer_queue_create(icmp_redirtimeout); 194 mutex_exit(&icmp_mtx); 195 196 icmpstat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS); 197 icmp_wqinput = wqinput_create("icmp", _icmp_input); 198 } 199 200 void 201 icmp_mtudisc_lock(void) 202 { 203 204 mutex_enter(&icmp_mtx); 205 } 206 207 void 208 icmp_mtudisc_unlock(void) 209 { 210 211 mutex_exit(&icmp_mtx); 212 } 213 214 /* 215 * Register a Path MTU Discovery callback. 216 */ 217 void 218 icmp_mtudisc_callback_register(void (*func)(struct in_addr)) 219 { 220 struct icmp_mtudisc_callback *mc, *new; 221 222 new = kmem_alloc(sizeof(*mc), KM_SLEEP); 223 224 mutex_enter(&icmp_mtx); 225 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL; 226 mc = LIST_NEXT(mc, mc_list)) { 227 if (mc->mc_func == func) { 228 mutex_exit(&icmp_mtx); 229 kmem_free(new, sizeof(*mc)); 230 return; 231 } 232 } 233 234 new->mc_func = func; 235 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, new, mc_list); 236 mutex_exit(&icmp_mtx); 237 } 238 239 /* 240 * Generate an error packet of type error in response to a bad IP packet. 'n' 241 * contains this packet. We create 'm' and send it. 242 * 243 * As we are not required to return everything we have, we return whatever 244 * we can return at ease. 245 * 246 * Note that ICMP datagrams longer than 576 octets are out of spec according 247 * to RFC1812; the limit on icmpreturndatabytes will keep things below that 248 * limit. 249 */ 250 void 251 icmp_error(struct mbuf *n, int type, int code, n_long dest, int destmtu) 252 { 253 struct ip *oip = mtod(n, struct ip *), *nip; 254 const unsigned oiphlen = oip->ip_hl << 2; 255 struct icmp *icp; 256 struct mbuf *m; 257 struct m_tag *mtag; 258 unsigned datalen, mblen; 259 int totlen; 260 261 if (type != ICMP_REDIRECT) 262 ICMP_STATINC(ICMP_STAT_ERROR); 263 264 /* 265 * Don't send error if: 266 * - The original packet was encrypted. 267 * - The packet is multicast or broadcast. 268 * - The packet is not the first fragment of the message. 269 * - The packet is an ICMP message with an unknown type. 270 */ 271 if (n->m_flags & M_DECRYPTED) 272 goto freeit; 273 if (n->m_flags & (M_BCAST|M_MCAST)) 274 goto freeit; 275 if (oip->ip_off &~ htons(IP_MF|IP_DF)) 276 goto freeit; 277 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 278 n->m_len >= oiphlen + ICMP_MINLEN) { 279 struct icmp *oicp = (struct icmp *)((char *)oip + oiphlen); 280 if (!ICMP_INFOTYPE(oicp->icmp_type)) { 281 ICMP_STATINC(ICMP_STAT_OLDICMP); 282 goto freeit; 283 } 284 } 285 286 /* 287 * First, do a rate limitation check. 288 */ 289 if (icmp_ratelimit(&oip->ip_src, type, code)) { 290 /* XXX stat */ 291 goto freeit; 292 } 293 294 /* 295 * Compute the number of bytes we will put in 'icmp_ip'. Truncate 296 * it to the size of the mbuf, if it's too big. 297 */ 298 datalen = oiphlen + uimin(icmpreturndatabytes, 299 ntohs(oip->ip_len) - oiphlen); 300 mblen = 0; 301 for (m = n; m && (mblen < datalen); m = m->m_next) 302 mblen += m->m_len; 303 datalen = uimin(mblen, datalen); 304 305 /* 306 * Compute the total length of the new packet. Truncate it if it's 307 * bigger than the size of a cluster. 308 */ 309 CTASSERT(ICMP_MINLEN + sizeof(struct ip) <= MCLBYTES); 310 totlen = sizeof(struct ip) + ICMP_MINLEN + datalen; 311 if (totlen > MCLBYTES) { 312 datalen = MCLBYTES - ICMP_MINLEN - sizeof(struct ip); 313 totlen = MCLBYTES; 314 } 315 316 /* 317 * Allocate the mbuf for the new packet. 318 */ 319 m = m_gethdr(M_DONTWAIT, MT_HEADER); 320 if (m && (totlen > MHLEN)) { 321 MCLGET(m, M_DONTWAIT); 322 if ((m->m_flags & M_EXT) == 0) { 323 m_freem(m); 324 m = NULL; 325 } 326 } 327 if (m == NULL) 328 goto freeit; 329 MCLAIM(m, n->m_owner); 330 m->m_len = totlen; 331 m->m_pkthdr.len = m->m_len; 332 m_copy_rcvif(m, n); 333 334 if ((u_int)type > ICMP_MAXTYPE) 335 panic("icmp_error"); 336 ICMP_STATINC(ICMP_STAT_OUTHIST + type); 337 338 if ((m->m_flags & M_EXT) == 0) 339 m_align(m, m->m_len); 340 341 /* 342 * Get pointers on the IP header and the ICMP header. 343 */ 344 nip = mtod(m, struct ip *); 345 icp = (struct icmp *)(nip + 1); 346 347 /* 348 * Fill in the fields of the ICMP header: icmp_type, icmp_code 349 * and icmp_ip. icmp_cksum gets filled later. 350 */ 351 icp->icmp_type = type; 352 if (type == ICMP_REDIRECT) { 353 icp->icmp_gwaddr.s_addr = dest; 354 } else { 355 icp->icmp_void = 0; 356 /* 357 * The following assignments assume an overlay with the 358 * zeroed icmp_void field. 359 */ 360 if (type == ICMP_PARAMPROB) { 361 icp->icmp_pptr = code; 362 code = 0; 363 } else if (type == ICMP_UNREACH && 364 code == ICMP_UNREACH_NEEDFRAG && destmtu) 365 icp->icmp_nextmtu = htons(destmtu); 366 } 367 icp->icmp_code = code; 368 m_copydata(n, 0, datalen, (void *)&icp->icmp_ip); 369 370 /* 371 * Now, copy the old IP header (without options) in front of the 372 * ICMP message. The src/dst fields will be swapped in icmp_reflect. 373 */ 374 /* ip_v set in ip_output */ 375 nip->ip_hl = sizeof(struct ip) >> 2; 376 nip->ip_tos = 0; 377 nip->ip_len = htons(m->m_len); 378 /* ip_id set in ip_output */ 379 nip->ip_off = htons(0); 380 /* ip_ttl set in icmp_reflect */ 381 nip->ip_p = IPPROTO_ICMP; 382 nip->ip_src = oip->ip_src; 383 nip->ip_dst = oip->ip_dst; 384 /* move PF m_tag to new packet, if it exists */ 385 mtag = m_tag_find(n, PACKET_TAG_PF); 386 if (mtag != NULL) { 387 m_tag_unlink(n, mtag); 388 m_tag_prepend(m, mtag); 389 } 390 391 icmp_reflect(m); 392 393 freeit: 394 m_freem(n); 395 } 396 397 struct sockaddr_in icmpsrc = { 398 .sin_len = sizeof(struct sockaddr_in), 399 .sin_family = AF_INET, 400 }; 401 402 /* 403 * Process a received ICMP message. 404 */ 405 static void 406 _icmp_input(struct mbuf *m, int hlen, int proto) 407 { 408 struct icmp *icp; 409 struct ip *ip = mtod(m, struct ip *); 410 int icmplen; 411 int i; 412 struct in_ifaddr *ia; 413 void *(*ctlfunc)(int, const struct sockaddr *, void *); 414 int code; 415 struct rtentry *rt; 416 struct sockaddr_in icmpdst = { 417 .sin_len = sizeof(struct sockaddr_in), 418 .sin_family = AF_INET, 419 }; 420 struct sockaddr_in icmpgw = { 421 .sin_len = sizeof(struct sockaddr_in), 422 .sin_family = AF_INET, 423 }; 424 425 /* 426 * Locate icmp structure in mbuf, and check 427 * that not corrupted and of at least minimum length. 428 */ 429 icmplen = ntohs(ip->ip_len) - hlen; 430 if (icmplen < ICMP_MINLEN) { 431 ICMP_STATINC(ICMP_STAT_TOOSHORT); 432 goto freeit; 433 } 434 i = hlen + uimin(icmplen, ICMP_ADVLENMIN); 435 if (M_UNWRITABLE(m, i) && (m = m_pullup(m, i)) == NULL) { 436 ICMP_STATINC(ICMP_STAT_TOOSHORT); 437 return; 438 } 439 ip = mtod(m, struct ip *); 440 m->m_len -= hlen; 441 m->m_data += hlen; 442 icp = mtod(m, struct icmp *); 443 /* Don't need to assert alignment, here. */ 444 if (in_cksum(m, icmplen)) { 445 ICMP_STATINC(ICMP_STAT_CHECKSUM); 446 goto freeit; 447 } 448 m->m_len += hlen; 449 m->m_data -= hlen; 450 451 if (icp->icmp_type > ICMP_MAXTYPE) 452 goto raw; 453 ICMP_STATINC(ICMP_STAT_INHIST + icp->icmp_type); 454 code = icp->icmp_code; 455 456 switch (icp->icmp_type) { 457 case ICMP_UNREACH: 458 switch (code) { 459 case ICMP_UNREACH_PROTOCOL: 460 code = PRC_UNREACH_PROTOCOL; 461 break; 462 463 case ICMP_UNREACH_PORT: 464 code = PRC_UNREACH_PORT; 465 break; 466 467 case ICMP_UNREACH_SRCFAIL: 468 code = PRC_UNREACH_SRCFAIL; 469 break; 470 471 case ICMP_UNREACH_NEEDFRAG: 472 code = PRC_MSGSIZE; 473 break; 474 475 case ICMP_UNREACH_NET: 476 case ICMP_UNREACH_NET_UNKNOWN: 477 case ICMP_UNREACH_NET_PROHIB: 478 case ICMP_UNREACH_TOSNET: 479 code = PRC_UNREACH_NET; 480 break; 481 482 case ICMP_UNREACH_HOST: 483 case ICMP_UNREACH_HOST_UNKNOWN: 484 case ICMP_UNREACH_ISOLATED: 485 case ICMP_UNREACH_HOST_PROHIB: 486 case ICMP_UNREACH_TOSHOST: 487 case ICMP_UNREACH_ADMIN_PROHIBIT: 488 case ICMP_UNREACH_HOST_PREC: 489 case ICMP_UNREACH_PREC_CUTOFF: 490 code = PRC_UNREACH_HOST; 491 break; 492 493 default: 494 goto badcode; 495 } 496 goto deliver; 497 498 case ICMP_TIMXCEED: 499 if (code > 1) 500 goto badcode; 501 code += PRC_TIMXCEED_INTRANS; 502 goto deliver; 503 504 case ICMP_PARAMPROB: 505 if (code > 1) 506 goto badcode; 507 code = PRC_PARAMPROB; 508 goto deliver; 509 510 case ICMP_SOURCEQUENCH: 511 if (code) 512 goto badcode; 513 code = PRC_QUENCH; 514 goto deliver; 515 516 deliver: 517 /* 518 * Problem with datagram; advise higher level routines. 519 */ 520 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 521 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 522 ICMP_STATINC(ICMP_STAT_BADLEN); 523 goto freeit; 524 } 525 if (m->m_len < hlen + ICMP_ADVLEN(icp)) { 526 m = m_pullup(m, hlen + ICMP_ADVLEN(icp)); 527 if (m == NULL) 528 goto freeit; 529 } 530 ip = mtod(m, struct ip *); 531 icp = (struct icmp *)(mtod(m, uint8_t *) + hlen); 532 533 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr)) 534 goto badcode; 535 536 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 537 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 538 if (ctlfunc) 539 (void) (*ctlfunc)(code, sintosa(&icmpsrc), 540 &icp->icmp_ip); 541 break; 542 543 badcode: 544 ICMP_STATINC(ICMP_STAT_BADCODE); 545 break; 546 547 case ICMP_ECHO: 548 if (!icmpbmcastecho && 549 (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 550 ICMP_STATINC(ICMP_STAT_BMCASTECHO); 551 break; 552 } 553 icp->icmp_type = ICMP_ECHOREPLY; 554 goto reflect; 555 556 case ICMP_TSTAMP: 557 if (icmplen < ICMP_TSLEN) { 558 ICMP_STATINC(ICMP_STAT_BADLEN); 559 break; 560 } 561 if (!icmpbmcastecho && 562 (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 563 ICMP_STATINC(ICMP_STAT_BMCASTTSTAMP); 564 break; 565 } 566 icp->icmp_type = ICMP_TSTAMPREPLY; 567 icp->icmp_rtime = iptime(); 568 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 569 goto reflect; 570 571 case ICMP_MASKREQ: { 572 struct ifnet *rcvif; 573 int s, ss; 574 struct ifaddr *ifa = NULL; 575 576 if (icmpmaskrepl == 0) 577 break; 578 /* 579 * We are not able to respond with all ones broadcast 580 * unless we receive it over a point-to-point interface. 581 */ 582 if (icmplen < ICMP_MASKLEN) { 583 ICMP_STATINC(ICMP_STAT_BADLEN); 584 break; 585 } 586 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 587 in_nullhost(ip->ip_dst)) 588 icmpdst.sin_addr = ip->ip_src; 589 else 590 icmpdst.sin_addr = ip->ip_dst; 591 ss = pserialize_read_enter(); 592 rcvif = m_get_rcvif(m, &s); 593 if (__predict_true(rcvif != NULL)) 594 ifa = ifaof_ifpforaddr(sintosa(&icmpdst), rcvif); 595 m_put_rcvif(rcvif, &s); 596 if (ifa == NULL) { 597 pserialize_read_exit(ss); 598 break; 599 } 600 ia = ifatoia(ifa); 601 icp->icmp_type = ICMP_MASKREPLY; 602 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 603 if (in_nullhost(ip->ip_src)) { 604 if (ia->ia_ifp->if_flags & IFF_BROADCAST) 605 ip->ip_src = ia->ia_broadaddr.sin_addr; 606 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 607 ip->ip_src = ia->ia_dstaddr.sin_addr; 608 } 609 pserialize_read_exit(ss); 610 reflect: 611 { 612 uint64_t *icps = percpu_getref(icmpstat_percpu); 613 icps[ICMP_STAT_REFLECT]++; 614 icps[ICMP_STAT_OUTHIST + icp->icmp_type]++; 615 percpu_putref(icmpstat_percpu); 616 } 617 icmp_reflect(m); 618 return; 619 } 620 621 case ICMP_REDIRECT: 622 if (code > 3) 623 goto badcode; 624 if (icmp_rediraccept == 0) 625 goto freeit; 626 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 627 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 628 ICMP_STATINC(ICMP_STAT_BADLEN); 629 break; 630 } 631 /* 632 * Short circuit routing redirects to force 633 * immediate change in the kernel's routing 634 * tables. The message is also handed to anyone 635 * listening on a raw socket (e.g. the routing 636 * daemon for use in updating its tables). 637 */ 638 icmpgw.sin_addr = ip->ip_src; 639 icmpdst.sin_addr = icp->icmp_gwaddr; 640 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 641 rt = NULL; 642 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst), 643 NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt); 644 mutex_enter(&icmp_mtx); 645 if (rt != NULL && icmp_redirtimeout != 0) { 646 i = rt_timer_add(rt, icmp_redirect_timeout, 647 icmp_redirect_timeout_q); 648 if (i) { 649 char buf[INET_ADDRSTRLEN]; 650 log(LOG_ERR, "ICMP: redirect failed to " 651 "register timeout for route to %s, " 652 "code %d\n", 653 IN_PRINT(buf, &icp->icmp_ip.ip_dst), i); 654 } 655 } 656 mutex_exit(&icmp_mtx); 657 if (rt != NULL) 658 rt_unref(rt); 659 660 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc)); 661 #if defined(IPSEC) 662 if (ipsec_used) 663 key_sa_routechange((struct sockaddr *)&icmpsrc); 664 #endif 665 break; 666 667 /* 668 * No kernel processing for the following; 669 * just fall through to send to raw listener. 670 */ 671 case ICMP_ECHOREPLY: 672 case ICMP_ROUTERADVERT: 673 case ICMP_ROUTERSOLICIT: 674 case ICMP_TSTAMPREPLY: 675 case ICMP_IREQREPLY: 676 case ICMP_MASKREPLY: 677 default: 678 break; 679 } 680 681 raw: 682 /* 683 * Currently, pim_input() is always called holding softnet_lock 684 * by ipintr()(!NET_MPSAFE) or PR_INPUT_WRAP()(NET_MPSAFE). 685 */ 686 KASSERT(mutex_owned(softnet_lock)); 687 rip_input(m, hlen, proto); 688 return; 689 690 freeit: 691 m_freem(m); 692 return; 693 } 694 695 void 696 icmp_input(struct mbuf *m, int off, int proto) 697 { 698 wqinput_input(icmp_wqinput, m, off, proto); 699 } 700 701 /* 702 * Reflect the ip packet back to the source 703 */ 704 void 705 icmp_reflect(struct mbuf *m) 706 { 707 struct ip *ip = mtod(m, struct ip *); 708 struct in_ifaddr *ia; 709 struct ifaddr *ifa; 710 struct sockaddr_in *sin; 711 struct in_addr t; 712 struct mbuf *opts = NULL; 713 int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 714 struct ifnet *rcvif; 715 struct psref psref, psref_ia; 716 int s; 717 int bound; 718 719 bound = curlwp_bind(); 720 721 if (!in_canforward(ip->ip_src) && 722 ((ip->ip_src.s_addr & IN_CLASSA_NET) != 723 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 724 m_freem(m); /* Bad return address */ 725 goto done; /* ip_output() will check for broadcast */ 726 } 727 t = ip->ip_dst; 728 ip->ip_dst = ip->ip_src; 729 730 /* 731 * If the incoming packet was addressed directly to us, use 732 * dst as the src for the reply. Otherwise (broadcast or 733 * anonymous), use an address which corresponds to the 734 * incoming interface, with a preference for the address which 735 * corresponds to the route to the destination of the ICMP. 736 */ 737 738 /* Look for packet addressed to us */ 739 ia = in_get_ia_psref(t, &psref_ia); 740 if (ia && (ia->ia4_flags & IN_IFF_NOTREADY)) { 741 ia4_release(ia, &psref_ia); 742 ia = NULL; 743 } 744 745 rcvif = m_get_rcvif_psref(m, &psref); 746 747 /* look for packet sent to broadcast address */ 748 if (ia == NULL && rcvif && 749 (rcvif->if_flags & IFF_BROADCAST)) { 750 s = pserialize_read_enter(); 751 IFADDR_READER_FOREACH(ifa, rcvif) { 752 if (ifa->ifa_addr->sa_family != AF_INET) 753 continue; 754 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) { 755 ia = ifatoia(ifa); 756 if ((ia->ia4_flags & IN_IFF_NOTREADY) == 0) 757 break; 758 ia = NULL; 759 } 760 } 761 if (ia != NULL) 762 ia4_acquire(ia, &psref_ia); 763 pserialize_read_exit(s); 764 } 765 766 sin = ia ? &ia->ia_addr : NULL; 767 768 /* 769 * if the packet is addressed somewhere else, compute the 770 * source address for packets routed back to the source, and 771 * use that, if it's an address on the interface which 772 * received the packet 773 */ 774 if (sin == NULL && rcvif) { 775 struct sockaddr_in sin_dst; 776 struct route icmproute; 777 int errornum; 778 779 sockaddr_in_init(&sin_dst, &ip->ip_dst, 0); 780 memset(&icmproute, 0, sizeof(icmproute)); 781 errornum = 0; 782 ia = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum, 783 &psref_ia); 784 /* errornum is never used */ 785 rtcache_free(&icmproute); 786 /* check to make sure sin is a source address on rcvif */ 787 if (ia != NULL) { 788 sin = &ia->ia_addr; 789 t = sin->sin_addr; 790 sin = NULL; 791 ia4_release(ia, &psref_ia); 792 ia = in_get_ia_on_iface_psref(t, rcvif, &psref_ia); 793 if (ia != NULL) 794 sin = &ia->ia_addr; 795 } 796 } 797 798 /* 799 * if it was not addressed to us, but the route doesn't go out 800 * the source interface, pick an address on the source 801 * interface. This can happen when routing is asymmetric, or 802 * when the incoming packet was encapsulated 803 */ 804 if (sin == NULL && rcvif) { 805 KASSERT(ia == NULL); 806 s = pserialize_read_enter(); 807 IFADDR_READER_FOREACH(ifa, rcvif) { 808 if (ifa->ifa_addr->sa_family != AF_INET) 809 continue; 810 sin = &(ifatoia(ifa)->ia_addr); 811 ia = ifatoia(ifa); 812 ia4_acquire(ia, &psref_ia); 813 break; 814 } 815 pserialize_read_exit(s); 816 } 817 818 m_put_rcvif_psref(rcvif, &psref); 819 820 /* 821 * The following happens if the packet was not addressed to us, 822 * and was received on an interface with no IP address: 823 * We find the first AF_INET address on the first non-loopback 824 * interface. 825 */ 826 if (sin == NULL) { 827 KASSERT(ia == NULL); 828 s = pserialize_read_enter(); 829 IN_ADDRLIST_READER_FOREACH(ia) { 830 if (ia->ia_ifp->if_flags & IFF_LOOPBACK) 831 continue; 832 sin = &ia->ia_addr; 833 ia4_acquire(ia, &psref_ia); 834 break; 835 } 836 pserialize_read_exit(s); 837 } 838 839 /* 840 * If we still didn't find an address, punt. We could have an 841 * interface up (and receiving packets) with no address. 842 */ 843 if (sin == NULL) { 844 KASSERT(ia == NULL); 845 m_freem(m); 846 goto done; 847 } 848 849 ip->ip_src = sin->sin_addr; 850 ip->ip_ttl = MAXTTL; 851 852 if (ia != NULL) 853 ia4_release(ia, &psref_ia); 854 855 if (optlen > 0) { 856 u_char *cp; 857 int opt, cnt; 858 u_int len; 859 860 /* 861 * Retrieve any source routing from the incoming packet; 862 * add on any record-route or timestamp options. 863 */ 864 cp = (u_char *)(ip + 1); 865 if ((opts = ip_srcroute(m)) == NULL && 866 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 867 MCLAIM(opts, m->m_owner); 868 opts->m_len = sizeof(struct in_addr); 869 *mtod(opts, struct in_addr *) = zeroin_addr; 870 } 871 872 if (opts) { 873 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 874 opt = cp[IPOPT_OPTVAL]; 875 if (opt == IPOPT_EOL) 876 break; 877 if (opt == IPOPT_NOP) 878 len = 1; 879 else { 880 if (cnt < IPOPT_OLEN + sizeof(*cp)) 881 break; 882 len = cp[IPOPT_OLEN]; 883 if (len < IPOPT_OLEN + sizeof(*cp) || 884 len > cnt) 885 break; 886 } 887 888 /* Overflows can't happen */ 889 KASSERT(opts->m_len + len <= MHLEN); 890 891 if (opt == IPOPT_RR || opt == IPOPT_TS || 892 opt == IPOPT_SECURITY) { 893 memmove(mtod(opts, char *) + 894 opts->m_len, cp, len); 895 opts->m_len += len; 896 } 897 } 898 899 /* Terminate & pad, if necessary */ 900 if ((cnt = opts->m_len % 4) != 0) { 901 for (; cnt < 4; cnt++) { 902 *(mtod(opts, char *) + opts->m_len) = 903 IPOPT_EOL; 904 opts->m_len++; 905 } 906 } 907 } 908 909 /* 910 * Now strip out original options by copying rest of first 911 * mbuf's data back, and adjust the IP length. 912 */ 913 ip->ip_len = htons(ntohs(ip->ip_len) - optlen); 914 ip->ip_hl = sizeof(struct ip) >> 2; 915 m->m_len -= optlen; 916 if (m->m_flags & M_PKTHDR) 917 m->m_pkthdr.len -= optlen; 918 optlen += sizeof(struct ip); 919 memmove(ip + 1, (char *)ip + optlen, 920 (unsigned)(m->m_len - sizeof(struct ip))); 921 } 922 m_tag_delete_chain(m); 923 m->m_flags &= ~(M_BCAST|M_MCAST); 924 925 /* 926 * Clear any in-bound checksum flags for this packet. 927 */ 928 if (m->m_flags & M_PKTHDR) 929 m->m_pkthdr.csum_flags = 0; 930 931 icmp_send(m, opts); 932 done: 933 curlwp_bindx(bound); 934 if (opts) 935 (void)m_free(opts); 936 } 937 938 /* 939 * Send an icmp packet back to the ip level, 940 * after supplying a checksum. 941 */ 942 static void 943 icmp_send(struct mbuf *m, struct mbuf *opts) 944 { 945 struct ip *ip = mtod(m, struct ip *); 946 int hlen; 947 struct icmp *icp; 948 949 hlen = ip->ip_hl << 2; 950 m->m_data += hlen; 951 m->m_len -= hlen; 952 icp = mtod(m, struct icmp *); 953 icp->icmp_cksum = 0; 954 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 955 m->m_data -= hlen; 956 m->m_len += hlen; 957 958 (void)ip_output(m, opts, NULL, 0, NULL, NULL); 959 } 960 961 n_time 962 iptime(void) 963 { 964 struct timeval atv; 965 u_long t; 966 967 microtime(&atv); 968 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 969 return (htonl(t)); 970 } 971 972 /* 973 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures 974 * that the new value is in the correct range. 975 */ 976 static int 977 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS) 978 { 979 int error, t; 980 struct sysctlnode node; 981 982 node = *rnode; 983 node.sysctl_data = &t; 984 t = icmpreturndatabytes; 985 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 986 if (error || newp == NULL) 987 return error; 988 989 if (t < 8 || t > 512) 990 return EINVAL; 991 icmpreturndatabytes = t; 992 993 return 0; 994 } 995 996 /* 997 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that 998 * the given value is not less than zero and then resets the timeout 999 * queue. 1000 */ 1001 static int 1002 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS) 1003 { 1004 int error, tmp; 1005 struct sysctlnode node; 1006 1007 mutex_enter(&icmp_mtx); 1008 1009 node = *rnode; 1010 node.sysctl_data = &tmp; 1011 tmp = icmp_redirtimeout; 1012 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1013 if (error || newp == NULL) 1014 goto out; 1015 if (tmp < 0) { 1016 error = EINVAL; 1017 goto out; 1018 } 1019 icmp_redirtimeout = tmp; 1020 1021 /* 1022 * was it a *defined* side-effect that anyone even *reading* 1023 * this value causes these things to happen? 1024 */ 1025 if (icmp_redirect_timeout_q != NULL) { 1026 if (icmp_redirtimeout == 0) { 1027 rt_timer_queue_destroy(icmp_redirect_timeout_q); 1028 icmp_redirect_timeout_q = NULL; 1029 } else { 1030 rt_timer_queue_change(icmp_redirect_timeout_q, 1031 icmp_redirtimeout); 1032 } 1033 } else if (icmp_redirtimeout > 0) { 1034 icmp_redirect_timeout_q = 1035 rt_timer_queue_create(icmp_redirtimeout); 1036 } 1037 error = 0; 1038 out: 1039 mutex_exit(&icmp_mtx); 1040 return error; 1041 } 1042 1043 static int 1044 sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS) 1045 { 1046 1047 return (NETSTAT_SYSCTL(icmpstat_percpu, ICMP_NSTATS)); 1048 } 1049 1050 static void 1051 sysctl_netinet_icmp_setup(struct sysctllog **clog) 1052 { 1053 1054 sysctl_createv(clog, 0, NULL, NULL, 1055 CTLFLAG_PERMANENT, 1056 CTLTYPE_NODE, "inet", NULL, 1057 NULL, 0, NULL, 0, 1058 CTL_NET, PF_INET, CTL_EOL); 1059 sysctl_createv(clog, 0, NULL, NULL, 1060 CTLFLAG_PERMANENT, 1061 CTLTYPE_NODE, "icmp", 1062 SYSCTL_DESCR("ICMPv4 related settings"), 1063 NULL, 0, NULL, 0, 1064 CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL); 1065 1066 sysctl_createv(clog, 0, NULL, NULL, 1067 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1068 CTLTYPE_INT, "maskrepl", 1069 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"), 1070 NULL, 0, &icmpmaskrepl, 0, 1071 CTL_NET, PF_INET, IPPROTO_ICMP, 1072 ICMPCTL_MASKREPL, CTL_EOL); 1073 sysctl_createv(clog, 0, NULL, NULL, 1074 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1075 CTLTYPE_INT, "returndatabytes", 1076 SYSCTL_DESCR("Number of bytes to return in an ICMP " 1077 "error message"), 1078 sysctl_net_inet_icmp_returndatabytes, 0, 1079 &icmpreturndatabytes, 0, 1080 CTL_NET, PF_INET, IPPROTO_ICMP, 1081 ICMPCTL_RETURNDATABYTES, CTL_EOL); 1082 sysctl_createv(clog, 0, NULL, NULL, 1083 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1084 CTLTYPE_INT, "errppslimit", 1085 SYSCTL_DESCR("Maximum number of outgoing ICMP error " 1086 "messages per second"), 1087 NULL, 0, &icmperrppslim, 0, 1088 CTL_NET, PF_INET, IPPROTO_ICMP, 1089 ICMPCTL_ERRPPSLIMIT, CTL_EOL); 1090 sysctl_createv(clog, 0, NULL, NULL, 1091 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1092 CTLTYPE_INT, "rediraccept", 1093 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"), 1094 NULL, 0, &icmp_rediraccept, 0, 1095 CTL_NET, PF_INET, IPPROTO_ICMP, 1096 ICMPCTL_REDIRACCEPT, CTL_EOL); 1097 sysctl_createv(clog, 0, NULL, NULL, 1098 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1099 CTLTYPE_INT, "redirtimeout", 1100 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated " 1101 "routes"), 1102 sysctl_net_inet_icmp_redirtimeout, 0, 1103 &icmp_redirtimeout, 0, 1104 CTL_NET, PF_INET, IPPROTO_ICMP, 1105 ICMPCTL_REDIRTIMEOUT, CTL_EOL); 1106 sysctl_createv(clog, 0, NULL, NULL, 1107 CTLFLAG_PERMANENT, 1108 CTLTYPE_STRUCT, "stats", 1109 SYSCTL_DESCR("ICMP statistics"), 1110 sysctl_net_inet_icmp_stats, 0, NULL, 0, 1111 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS, 1112 CTL_EOL); 1113 sysctl_createv(clog, 0, NULL, NULL, 1114 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1115 CTLTYPE_INT, "bmcastecho", 1116 SYSCTL_DESCR("Respond to ICMP_ECHO or ICMP_TIMESTAMP " 1117 "message to the broadcast or multicast"), 1118 NULL, 0, &icmpbmcastecho, 0, 1119 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_BMCASTECHO, 1120 CTL_EOL); 1121 } 1122 1123 void 1124 icmp_statinc(u_int stat) 1125 { 1126 1127 KASSERT(stat < ICMP_NSTATS); 1128 ICMP_STATINC(stat); 1129 } 1130 1131 /* Table of common MTUs */ 1132 static const u_int mtu_table[] = { 1133 65535, 65280, 32000, 17914, 9180, 8166, 1134 4352, 2002, 1492, 1006, 508, 296, 68, 0 1135 }; 1136 1137 void 1138 icmp_mtudisc(struct icmp *icp, struct in_addr faddr) 1139 { 1140 struct icmp_mtudisc_callback *mc; 1141 struct sockaddr *dst = sintosa(&icmpsrc); 1142 struct rtentry *rt; 1143 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */ 1144 int error; 1145 1146 rt = rtalloc1(dst, 1); 1147 if (rt == NULL) 1148 return; 1149 1150 /* If we didn't get a host route, allocate one */ 1151 if ((rt->rt_flags & RTF_HOST) == 0) { 1152 struct rtentry *nrt; 1153 1154 error = rtrequest(RTM_ADD, dst, rt->rt_gateway, NULL, 1155 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt); 1156 if (error) { 1157 rt_unref(rt); 1158 return; 1159 } 1160 nrt->rt_rmx = rt->rt_rmx; 1161 rt_unref(rt); 1162 rt = nrt; 1163 } 1164 1165 mutex_enter(&icmp_mtx); 1166 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q); 1167 mutex_exit(&icmp_mtx); 1168 if (error) { 1169 rt_unref(rt); 1170 return; 1171 } 1172 1173 if (mtu == 0) { 1174 int i = 0; 1175 1176 mtu = ntohs(icp->icmp_ip.ip_len); 1177 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */ 1178 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0) 1179 mtu -= (icp->icmp_ip.ip_hl << 2); 1180 1181 /* If we still can't guess a value, try the route */ 1182 if (mtu == 0) { 1183 mtu = rt->rt_rmx.rmx_mtu; 1184 1185 /* If no route mtu, default to the interface mtu */ 1186 if (mtu == 0) 1187 mtu = rt->rt_ifp->if_mtu; 1188 } 1189 1190 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++) { 1191 if (mtu > mtu_table[i]) { 1192 mtu = mtu_table[i]; 1193 break; 1194 } 1195 } 1196 } 1197 1198 /* 1199 * XXX: RTV_MTU is overloaded, since the admin can set it 1200 * to turn off PMTU for a route, and the kernel can 1201 * set it to indicate a serious problem with PMTU 1202 * on a route. We should be using a separate flag 1203 * for the kernel to indicate this. 1204 */ 1205 1206 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1207 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu) 1208 rt->rt_rmx.rmx_locks |= RTV_MTU; 1209 else if (rt->rt_rmx.rmx_mtu > mtu || 1210 rt->rt_rmx.rmx_mtu == 0) { 1211 ICMP_STATINC(ICMP_STAT_PMTUCHG); 1212 rt->rt_rmx.rmx_mtu = mtu; 1213 } 1214 } 1215 1216 if (rt != NULL) 1217 rt_unref(rt); 1218 1219 /* 1220 * Notify protocols that the MTU for this destination 1221 * has changed. 1222 */ 1223 mutex_enter(&icmp_mtx); 1224 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL; 1225 mc = LIST_NEXT(mc, mc_list)) 1226 (*mc->mc_func)(faddr); 1227 mutex_exit(&icmp_mtx); 1228 } 1229 1230 /* 1231 * Return the next larger or smaller MTU plateau (table from RFC 1191) 1232 * given current value MTU. If DIR is less than zero, a larger plateau 1233 * is returned; otherwise, a smaller value is returned. 1234 */ 1235 u_int 1236 ip_next_mtu(u_int mtu, int dir) /* XXX unused */ 1237 { 1238 int i; 1239 1240 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) { 1241 if (mtu >= mtu_table[i]) 1242 break; 1243 } 1244 1245 if (dir < 0) { 1246 if (i == 0) { 1247 return 0; 1248 } else { 1249 return mtu_table[i - 1]; 1250 } 1251 } else { 1252 if (mtu_table[i] == 0) { 1253 return 0; 1254 } else if (mtu > mtu_table[i]) { 1255 return mtu_table[i]; 1256 } else { 1257 return mtu_table[i + 1]; 1258 } 1259 } 1260 } 1261 1262 static void 1263 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r) 1264 { 1265 struct rtentry *retrt; 1266 1267 KASSERT(rt != NULL); 1268 rt_assert_referenced(rt); 1269 1270 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1271 (RTF_DYNAMIC | RTF_HOST)) { 1272 rtrequest(RTM_DELETE, rt_getkey(rt), 1273 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt); 1274 rt_unref(rt); 1275 rt_free(retrt); 1276 } else { 1277 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1278 rt->rt_rmx.rmx_mtu = 0; 1279 } 1280 } 1281 } 1282 1283 static void 1284 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r) 1285 { 1286 struct rtentry *retrt; 1287 1288 KASSERT(rt != NULL); 1289 rt_assert_referenced(rt); 1290 1291 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1292 (RTF_DYNAMIC | RTF_HOST)) { 1293 rtrequest(RTM_DELETE, rt_getkey(rt), 1294 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &retrt); 1295 rt_unref(rt); 1296 rt_free(retrt); 1297 } 1298 } 1299 1300 /* 1301 * Perform rate limit check. 1302 * Returns 0 if it is okay to send the icmp packet. 1303 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate 1304 * limitation. 1305 * 1306 * XXX per-destination/type check necessary? 1307 */ 1308 int 1309 icmp_ratelimit(const struct in_addr *dst, const int type, 1310 const int code) 1311 { 1312 1313 /* PPS limit */ 1314 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count, 1315 icmperrppslim)) { 1316 /* The packet is subject to rate limit */ 1317 return 1; 1318 } 1319 1320 /* okay to send */ 1321 return 0; 1322 } 1323