1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NET3: Implementation of the ICMP protocol layer. 4 * 5 * Alan Cox, <alan@lxorguk.ukuu.org.uk> 6 * 7 * Some of the function names and the icmp unreach table for this 8 * module were derived from [icmp.c 1.0.11 06/02/93] by 9 * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting. 10 * Other than that this module is a complete rewrite. 11 * 12 * Fixes: 13 * Clemens Fruhwirth : introduce global icmp rate limiting 14 * with icmp type masking ability instead 15 * of broken per type icmp timeouts. 16 * Mike Shaver : RFC1122 checks. 17 * Alan Cox : Multicast ping reply as self. 18 * Alan Cox : Fix atomicity lockup in ip_build_xmit 19 * call. 20 * Alan Cox : Added 216,128 byte paths to the MTU 21 * code. 22 * Martin Mares : RFC1812 checks. 23 * Martin Mares : Can be configured to follow redirects 24 * if acting as a router _without_ a 25 * routing protocol (RFC 1812). 26 * Martin Mares : Echo requests may be configured to 27 * be ignored (RFC 1812). 28 * Martin Mares : Limitation of ICMP error message 29 * transmit rate (RFC 1812). 30 * Martin Mares : TOS and Precedence set correctly 31 * (RFC 1812). 32 * Martin Mares : Now copying as much data from the 33 * original packet as we can without 34 * exceeding 576 bytes (RFC 1812). 35 * Willy Konynenberg : Transparent proxying support. 36 * Keith Owens : RFC1191 correction for 4.2BSD based 37 * path MTU bug. 38 * Thomas Quinot : ICMP Dest Unreach codes up to 15 are 39 * valid (RFC 1812). 40 * Andi Kleen : Check all packet lengths properly 41 * and moved all kfree_skb() up to 42 * icmp_rcv. 43 * Andi Kleen : Move the rate limit bookkeeping 44 * into the dest entry and use a token 45 * bucket filter (thanks to ANK). Make 46 * the rates sysctl configurable. 47 * Yu Tianli : Fixed two ugly bugs in icmp_send 48 * - IP option length was accounted wrongly 49 * - ICMP header length was not accounted 50 * at all. 51 * Tristan Greaves : Added sysctl option to ignore bogus 52 * broadcast responses from broken routers. 53 * 54 * To Fix: 55 * 56 * - Should use skb_pull() instead of all the manual checking. 57 * This would also greatly simply some upper layer error handlers. --AK 58 */ 59 60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 61 62 #include <linux/module.h> 63 #include <linux/types.h> 64 #include <linux/jiffies.h> 65 #include <linux/kernel.h> 66 #include <linux/fcntl.h> 67 #include <linux/socket.h> 68 #include <linux/in.h> 69 #include <linux/inet.h> 70 #include <linux/inetdevice.h> 71 #include <linux/netdevice.h> 72 #include <linux/string.h> 73 #include <linux/netfilter_ipv4.h> 74 #include <linux/slab.h> 75 #include <net/snmp.h> 76 #include <net/ip.h> 77 #include <net/route.h> 78 #include <net/protocol.h> 79 #include <net/icmp.h> 80 #include <net/tcp.h> 81 #include <net/udp.h> 82 #include <net/raw.h> 83 #include <net/ping.h> 84 #include <linux/skbuff.h> 85 #include <net/sock.h> 86 #include <linux/errno.h> 87 #include <linux/timer.h> 88 #include <linux/init.h> 89 #include <linux/uaccess.h> 90 #include <net/checksum.h> 91 #include <net/xfrm.h> 92 #include <net/inet_common.h> 93 #include <net/ip_fib.h> 94 #include <net/l3mdev.h> 95 #include <net/addrconf.h> 96 #include <net/inet_dscp.h> 97 #define CREATE_TRACE_POINTS 98 #include <trace/events/icmp.h> 99 100 /* 101 * Build xmit assembly blocks 102 */ 103 104 struct icmp_bxm { 105 struct sk_buff *skb; 106 int offset; 107 int data_len; 108 109 struct { 110 struct icmphdr icmph; 111 __be32 times[3]; 112 } data; 113 int head_len; 114 struct ip_options_data replyopts; 115 }; 116 117 /* An array of errno for error messages from dest unreach. */ 118 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */ 119 120 const struct icmp_err icmp_err_convert[] = { 121 { 122 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */ 123 .fatal = 0, 124 }, 125 { 126 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */ 127 .fatal = 0, 128 }, 129 { 130 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */, 131 .fatal = 1, 132 }, 133 { 134 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */ 135 .fatal = 1, 136 }, 137 { 138 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */ 139 .fatal = 0, 140 }, 141 { 142 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */ 143 .fatal = 0, 144 }, 145 { 146 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */ 147 .fatal = 1, 148 }, 149 { 150 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */ 151 .fatal = 1, 152 }, 153 { 154 .errno = ENONET, /* ICMP_HOST_ISOLATED */ 155 .fatal = 1, 156 }, 157 { 158 .errno = ENETUNREACH, /* ICMP_NET_ANO */ 159 .fatal = 1, 160 }, 161 { 162 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */ 163 .fatal = 1, 164 }, 165 { 166 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */ 167 .fatal = 0, 168 }, 169 { 170 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */ 171 .fatal = 0, 172 }, 173 { 174 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */ 175 .fatal = 1, 176 }, 177 { 178 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */ 179 .fatal = 1, 180 }, 181 { 182 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */ 183 .fatal = 1, 184 }, 185 }; 186 EXPORT_SYMBOL(icmp_err_convert); 187 188 /* 189 * ICMP control array. This specifies what to do with each ICMP. 190 */ 191 192 struct icmp_control { 193 enum skb_drop_reason (*handler)(struct sk_buff *skb); 194 short error; /* This ICMP is classed as an error message */ 195 }; 196 197 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1]; 198 199 static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk); 200 201 /* Called with BH disabled */ 202 static inline struct sock *icmp_xmit_lock(struct net *net) 203 { 204 struct sock *sk; 205 206 sk = this_cpu_read(ipv4_icmp_sk); 207 208 if (unlikely(!spin_trylock(&sk->sk_lock.slock))) { 209 /* This can happen if the output path signals a 210 * dst_link_failure() for an outgoing ICMP packet. 211 */ 212 return NULL; 213 } 214 sock_net_set(sk, net); 215 return sk; 216 } 217 218 static inline void icmp_xmit_unlock(struct sock *sk) 219 { 220 sock_net_set(sk, &init_net); 221 spin_unlock(&sk->sk_lock.slock); 222 } 223 224 /** 225 * icmp_global_allow - Are we allowed to send one more ICMP message ? 226 * @net: network namespace 227 * 228 * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec. 229 * Returns false if we reached the limit and can not send another packet. 230 * Works in tandem with icmp_global_consume(). 231 */ 232 bool icmp_global_allow(struct net *net) 233 { 234 u32 delta, now, oldstamp; 235 int incr, new, old; 236 237 /* Note: many cpus could find this condition true. 238 * Then later icmp_global_consume() could consume more credits, 239 * this is an acceptable race. 240 */ 241 if (atomic_read(&net->ipv4.icmp_global_credit) > 0) 242 return true; 243 244 now = jiffies; 245 oldstamp = READ_ONCE(net->ipv4.icmp_global_stamp); 246 delta = min_t(u32, now - oldstamp, HZ); 247 if (delta < HZ / 50) 248 return false; 249 250 incr = READ_ONCE(net->ipv4.sysctl_icmp_msgs_per_sec) * delta / HZ; 251 if (!incr) 252 return false; 253 254 if (cmpxchg(&net->ipv4.icmp_global_stamp, oldstamp, now) == oldstamp) { 255 old = atomic_read(&net->ipv4.icmp_global_credit); 256 do { 257 new = min(old + incr, READ_ONCE(net->ipv4.sysctl_icmp_msgs_burst)); 258 } while (!atomic_try_cmpxchg(&net->ipv4.icmp_global_credit, &old, new)); 259 } 260 return true; 261 } 262 EXPORT_SYMBOL(icmp_global_allow); 263 264 void icmp_global_consume(struct net *net) 265 { 266 int credits = get_random_u32_below(3); 267 268 /* Note: this might make icmp_global.credit negative. */ 269 if (credits) 270 atomic_sub(credits, &net->ipv4.icmp_global_credit); 271 } 272 EXPORT_SYMBOL(icmp_global_consume); 273 274 static bool icmpv4_mask_allow(struct net *net, int type, int code) 275 { 276 if (type > NR_ICMP_TYPES) 277 return true; 278 279 /* Don't limit PMTU discovery. */ 280 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 281 return true; 282 283 /* Limit if icmp type is enabled in ratemask. */ 284 if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask))) 285 return true; 286 287 return false; 288 } 289 290 static bool icmpv4_global_allow(struct net *net, int type, int code, 291 bool *apply_ratelimit) 292 { 293 if (icmpv4_mask_allow(net, type, code)) 294 return true; 295 296 if (icmp_global_allow(net)) { 297 *apply_ratelimit = true; 298 return true; 299 } 300 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL); 301 return false; 302 } 303 304 /* 305 * Send an ICMP frame. 306 */ 307 308 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt, 309 struct flowi4 *fl4, int type, int code, 310 bool apply_ratelimit) 311 { 312 struct dst_entry *dst = &rt->dst; 313 struct inet_peer *peer; 314 bool rc = true; 315 int vif; 316 317 if (!apply_ratelimit) 318 return true; 319 320 /* No rate limit on loopback */ 321 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) 322 goto out; 323 324 vif = l3mdev_master_ifindex(dst->dev); 325 peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1); 326 rc = inet_peer_xrlim_allow(peer, 327 READ_ONCE(net->ipv4.sysctl_icmp_ratelimit)); 328 if (peer) 329 inet_putpeer(peer); 330 out: 331 if (!rc) 332 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST); 333 else 334 icmp_global_consume(net); 335 return rc; 336 } 337 338 /* 339 * Maintain the counters used in the SNMP statistics for outgoing ICMP 340 */ 341 void icmp_out_count(struct net *net, unsigned char type) 342 { 343 ICMPMSGOUT_INC_STATS(net, type); 344 ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS); 345 } 346 347 /* 348 * Checksum each fragment, and on the first include the headers and final 349 * checksum. 350 */ 351 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd, 352 struct sk_buff *skb) 353 { 354 struct icmp_bxm *icmp_param = from; 355 __wsum csum; 356 357 csum = skb_copy_and_csum_bits(icmp_param->skb, 358 icmp_param->offset + offset, 359 to, len); 360 361 skb->csum = csum_block_add(skb->csum, csum, odd); 362 if (icmp_pointers[icmp_param->data.icmph.type].error) 363 nf_ct_attach(skb, icmp_param->skb); 364 return 0; 365 } 366 367 static void icmp_push_reply(struct sock *sk, 368 struct icmp_bxm *icmp_param, 369 struct flowi4 *fl4, 370 struct ipcm_cookie *ipc, struct rtable **rt) 371 { 372 struct sk_buff *skb; 373 374 if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param, 375 icmp_param->data_len+icmp_param->head_len, 376 icmp_param->head_len, 377 ipc, rt, MSG_DONTWAIT) < 0) { 378 __ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS); 379 ip_flush_pending_frames(sk); 380 } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) { 381 struct icmphdr *icmph = icmp_hdr(skb); 382 __wsum csum; 383 struct sk_buff *skb1; 384 385 csum = csum_partial_copy_nocheck((void *)&icmp_param->data, 386 (char *)icmph, 387 icmp_param->head_len); 388 skb_queue_walk(&sk->sk_write_queue, skb1) { 389 csum = csum_add(csum, skb1->csum); 390 } 391 icmph->checksum = csum_fold(csum); 392 skb->ip_summed = CHECKSUM_NONE; 393 ip_push_pending_frames(sk, fl4); 394 } 395 } 396 397 /* 398 * Driving logic for building and sending ICMP messages. 399 */ 400 401 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) 402 { 403 struct ipcm_cookie ipc; 404 struct rtable *rt = skb_rtable(skb); 405 struct net *net = dev_net(rt->dst.dev); 406 bool apply_ratelimit = false; 407 struct flowi4 fl4; 408 struct sock *sk; 409 struct inet_sock *inet; 410 __be32 daddr, saddr; 411 u32 mark = IP4_REPLY_MARK(net, skb->mark); 412 int type = icmp_param->data.icmph.type; 413 int code = icmp_param->data.icmph.code; 414 415 if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb)) 416 return; 417 418 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */ 419 local_bh_disable(); 420 421 /* is global icmp_msgs_per_sec exhausted ? */ 422 if (!icmpv4_global_allow(net, type, code, &apply_ratelimit)) 423 goto out_bh_enable; 424 425 sk = icmp_xmit_lock(net); 426 if (!sk) 427 goto out_bh_enable; 428 inet = inet_sk(sk); 429 430 icmp_param->data.icmph.checksum = 0; 431 432 ipcm_init(&ipc); 433 inet->tos = ip_hdr(skb)->tos; 434 ipc.sockc.mark = mark; 435 daddr = ipc.addr = ip_hdr(skb)->saddr; 436 saddr = fib_compute_spec_dst(skb); 437 438 if (icmp_param->replyopts.opt.opt.optlen) { 439 ipc.opt = &icmp_param->replyopts.opt; 440 if (ipc.opt->opt.srr) 441 daddr = icmp_param->replyopts.opt.opt.faddr; 442 } 443 memset(&fl4, 0, sizeof(fl4)); 444 fl4.daddr = daddr; 445 fl4.saddr = saddr; 446 fl4.flowi4_mark = mark; 447 fl4.flowi4_uid = sock_net_uid(net, NULL); 448 fl4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(ip_hdr(skb))); 449 fl4.flowi4_proto = IPPROTO_ICMP; 450 fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev); 451 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4)); 452 rt = ip_route_output_key(net, &fl4); 453 if (IS_ERR(rt)) 454 goto out_unlock; 455 if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit)) 456 icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt); 457 ip_rt_put(rt); 458 out_unlock: 459 icmp_xmit_unlock(sk); 460 out_bh_enable: 461 local_bh_enable(); 462 } 463 464 /* 465 * The device used for looking up which routing table to use for sending an ICMP 466 * error is preferably the source whenever it is set, which should ensure the 467 * icmp error can be sent to the source host, else lookup using the routing 468 * table of the destination device, else use the main routing table (index 0). 469 */ 470 static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb) 471 { 472 struct net_device *route_lookup_dev = NULL; 473 474 if (skb->dev) 475 route_lookup_dev = skb->dev; 476 else if (skb_dst(skb)) 477 route_lookup_dev = skb_dst(skb)->dev; 478 return route_lookup_dev; 479 } 480 481 static struct rtable *icmp_route_lookup(struct net *net, struct flowi4 *fl4, 482 struct sk_buff *skb_in, 483 const struct iphdr *iph, __be32 saddr, 484 dscp_t dscp, u32 mark, int type, 485 int code, struct icmp_bxm *param) 486 { 487 struct net_device *route_lookup_dev; 488 struct dst_entry *dst, *dst2; 489 struct rtable *rt, *rt2; 490 struct flowi4 fl4_dec; 491 int err; 492 493 memset(fl4, 0, sizeof(*fl4)); 494 fl4->daddr = (param->replyopts.opt.opt.srr ? 495 param->replyopts.opt.opt.faddr : iph->saddr); 496 fl4->saddr = saddr; 497 fl4->flowi4_mark = mark; 498 fl4->flowi4_uid = sock_net_uid(net, NULL); 499 fl4->flowi4_tos = inet_dscp_to_dsfield(dscp); 500 fl4->flowi4_proto = IPPROTO_ICMP; 501 fl4->fl4_icmp_type = type; 502 fl4->fl4_icmp_code = code; 503 route_lookup_dev = icmp_get_route_lookup_dev(skb_in); 504 fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev); 505 506 security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4)); 507 rt = ip_route_output_key_hash(net, fl4, skb_in); 508 if (IS_ERR(rt)) 509 return rt; 510 511 /* No need to clone since we're just using its address. */ 512 rt2 = rt; 513 514 dst = xfrm_lookup(net, &rt->dst, 515 flowi4_to_flowi(fl4), NULL, 0); 516 rt = dst_rtable(dst); 517 if (!IS_ERR(dst)) { 518 if (rt != rt2) 519 return rt; 520 } else if (PTR_ERR(dst) == -EPERM) { 521 rt = NULL; 522 } else { 523 return rt; 524 } 525 err = xfrm_decode_session_reverse(net, skb_in, flowi4_to_flowi(&fl4_dec), AF_INET); 526 if (err) 527 goto relookup_failed; 528 529 if (inet_addr_type_dev_table(net, route_lookup_dev, 530 fl4_dec.saddr) == RTN_LOCAL) { 531 rt2 = __ip_route_output_key(net, &fl4_dec); 532 if (IS_ERR(rt2)) 533 err = PTR_ERR(rt2); 534 } else { 535 struct flowi4 fl4_2 = {}; 536 unsigned long orefdst; 537 538 fl4_2.daddr = fl4_dec.saddr; 539 rt2 = ip_route_output_key(net, &fl4_2); 540 if (IS_ERR(rt2)) { 541 err = PTR_ERR(rt2); 542 goto relookup_failed; 543 } 544 /* Ugh! */ 545 orefdst = skb_in->_skb_refdst; /* save old refdst */ 546 skb_dst_set(skb_in, NULL); 547 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr, 548 dscp, rt2->dst.dev) ? -EINVAL : 0; 549 550 dst_release(&rt2->dst); 551 rt2 = skb_rtable(skb_in); 552 skb_in->_skb_refdst = orefdst; /* restore old refdst */ 553 } 554 555 if (err) 556 goto relookup_failed; 557 558 dst2 = xfrm_lookup(net, &rt2->dst, flowi4_to_flowi(&fl4_dec), NULL, 559 XFRM_LOOKUP_ICMP); 560 rt2 = dst_rtable(dst2); 561 if (!IS_ERR(dst2)) { 562 dst_release(&rt->dst); 563 memcpy(fl4, &fl4_dec, sizeof(*fl4)); 564 rt = rt2; 565 } else if (PTR_ERR(dst2) == -EPERM) { 566 if (rt) 567 dst_release(&rt->dst); 568 return rt2; 569 } else { 570 err = PTR_ERR(dst2); 571 goto relookup_failed; 572 } 573 return rt; 574 575 relookup_failed: 576 if (rt) 577 return rt; 578 return ERR_PTR(err); 579 } 580 581 /* 582 * Send an ICMP message in response to a situation 583 * 584 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. 585 * MAY send more (we do). 586 * MUST NOT change this header information. 587 * MUST NOT reply to a multicast/broadcast IP address. 588 * MUST NOT reply to a multicast/broadcast MAC address. 589 * MUST reply to only the first fragment. 590 */ 591 592 void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info, 593 const struct ip_options *opt) 594 { 595 struct iphdr *iph; 596 int room; 597 struct icmp_bxm icmp_param; 598 struct rtable *rt = skb_rtable(skb_in); 599 bool apply_ratelimit = false; 600 struct ipcm_cookie ipc; 601 struct flowi4 fl4; 602 __be32 saddr; 603 u8 tos; 604 u32 mark; 605 struct net *net; 606 struct sock *sk; 607 608 if (!rt) 609 goto out; 610 611 if (rt->dst.dev) 612 net = dev_net(rt->dst.dev); 613 else if (skb_in->dev) 614 net = dev_net(skb_in->dev); 615 else 616 goto out; 617 618 /* 619 * Find the original header. It is expected to be valid, of course. 620 * Check this, icmp_send is called from the most obscure devices 621 * sometimes. 622 */ 623 iph = ip_hdr(skb_in); 624 625 if ((u8 *)iph < skb_in->head || 626 (skb_network_header(skb_in) + sizeof(*iph)) > 627 skb_tail_pointer(skb_in)) 628 goto out; 629 630 /* 631 * No replies to physical multicast/broadcast 632 */ 633 if (skb_in->pkt_type != PACKET_HOST) 634 goto out; 635 636 /* 637 * Now check at the protocol level 638 */ 639 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 640 goto out; 641 642 /* 643 * Only reply to fragment 0. We byte re-order the constant 644 * mask for efficiency. 645 */ 646 if (iph->frag_off & htons(IP_OFFSET)) 647 goto out; 648 649 /* 650 * If we send an ICMP error to an ICMP error a mess would result.. 651 */ 652 if (icmp_pointers[type].error) { 653 /* 654 * We are an error, check if we are replying to an 655 * ICMP error 656 */ 657 if (iph->protocol == IPPROTO_ICMP) { 658 u8 _inner_type, *itp; 659 660 itp = skb_header_pointer(skb_in, 661 skb_network_header(skb_in) + 662 (iph->ihl << 2) + 663 offsetof(struct icmphdr, 664 type) - 665 skb_in->data, 666 sizeof(_inner_type), 667 &_inner_type); 668 if (!itp) 669 goto out; 670 671 /* 672 * Assume any unknown ICMP type is an error. This 673 * isn't specified by the RFC, but think about it.. 674 */ 675 if (*itp > NR_ICMP_TYPES || 676 icmp_pointers[*itp].error) 677 goto out; 678 } 679 } 680 681 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */ 682 local_bh_disable(); 683 684 /* Check global sysctl_icmp_msgs_per_sec ratelimit, unless 685 * incoming dev is loopback. If outgoing dev change to not be 686 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow) 687 */ 688 if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) && 689 !icmpv4_global_allow(net, type, code, &apply_ratelimit)) 690 goto out_bh_enable; 691 692 sk = icmp_xmit_lock(net); 693 if (!sk) 694 goto out_bh_enable; 695 696 /* 697 * Construct source address and options. 698 */ 699 700 saddr = iph->daddr; 701 if (!(rt->rt_flags & RTCF_LOCAL)) { 702 struct net_device *dev = NULL; 703 704 rcu_read_lock(); 705 if (rt_is_input_route(rt) && 706 READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)) 707 dev = dev_get_by_index_rcu(net, inet_iif(skb_in)); 708 709 if (dev) 710 saddr = inet_select_addr(dev, iph->saddr, 711 RT_SCOPE_LINK); 712 else 713 saddr = 0; 714 rcu_read_unlock(); 715 } 716 717 tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) | 718 IPTOS_PREC_INTERNETCONTROL) : 719 iph->tos; 720 mark = IP4_REPLY_MARK(net, skb_in->mark); 721 722 if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt)) 723 goto out_unlock; 724 725 726 /* 727 * Prepare data for ICMP header. 728 */ 729 730 icmp_param.data.icmph.type = type; 731 icmp_param.data.icmph.code = code; 732 icmp_param.data.icmph.un.gateway = info; 733 icmp_param.data.icmph.checksum = 0; 734 icmp_param.skb = skb_in; 735 icmp_param.offset = skb_network_offset(skb_in); 736 inet_sk(sk)->tos = tos; 737 ipcm_init(&ipc); 738 ipc.addr = iph->saddr; 739 ipc.opt = &icmp_param.replyopts.opt; 740 ipc.sockc.mark = mark; 741 742 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, 743 inet_dsfield_to_dscp(tos), mark, type, code, 744 &icmp_param); 745 if (IS_ERR(rt)) 746 goto out_unlock; 747 748 /* peer icmp_ratelimit */ 749 if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit)) 750 goto ende; 751 752 /* RFC says return as much as we can without exceeding 576 bytes. */ 753 754 room = dst_mtu(&rt->dst); 755 if (room > 576) 756 room = 576; 757 room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen; 758 room -= sizeof(struct icmphdr); 759 /* Guard against tiny mtu. We need to include at least one 760 * IP network header for this message to make any sense. 761 */ 762 if (room <= (int)sizeof(struct iphdr)) 763 goto ende; 764 765 icmp_param.data_len = skb_in->len - icmp_param.offset; 766 if (icmp_param.data_len > room) 767 icmp_param.data_len = room; 768 icmp_param.head_len = sizeof(struct icmphdr); 769 770 /* if we don't have a source address at this point, fall back to the 771 * dummy address instead of sending out a packet with a source address 772 * of 0.0.0.0 773 */ 774 if (!fl4.saddr) 775 fl4.saddr = htonl(INADDR_DUMMY); 776 777 trace_icmp_send(skb_in, type, code); 778 779 icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt); 780 ende: 781 ip_rt_put(rt); 782 out_unlock: 783 icmp_xmit_unlock(sk); 784 out_bh_enable: 785 local_bh_enable(); 786 out:; 787 } 788 EXPORT_SYMBOL(__icmp_send); 789 790 #if IS_ENABLED(CONFIG_NF_NAT) 791 #include <net/netfilter/nf_conntrack.h> 792 void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info) 793 { 794 struct sk_buff *cloned_skb = NULL; 795 struct ip_options opts = { 0 }; 796 enum ip_conntrack_info ctinfo; 797 struct nf_conn *ct; 798 __be32 orig_ip; 799 800 ct = nf_ct_get(skb_in, &ctinfo); 801 if (!ct || !(ct->status & IPS_SRC_NAT)) { 802 __icmp_send(skb_in, type, code, info, &opts); 803 return; 804 } 805 806 if (skb_shared(skb_in)) 807 skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC); 808 809 if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head || 810 (skb_network_header(skb_in) + sizeof(struct iphdr)) > 811 skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in, 812 skb_network_offset(skb_in) + sizeof(struct iphdr)))) 813 goto out; 814 815 orig_ip = ip_hdr(skb_in)->saddr; 816 ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip; 817 __icmp_send(skb_in, type, code, info, &opts); 818 ip_hdr(skb_in)->saddr = orig_ip; 819 out: 820 consume_skb(cloned_skb); 821 } 822 EXPORT_SYMBOL(icmp_ndo_send); 823 #endif 824 825 static void icmp_socket_deliver(struct sk_buff *skb, u32 info) 826 { 827 const struct iphdr *iph = (const struct iphdr *)skb->data; 828 const struct net_protocol *ipprot; 829 int protocol = iph->protocol; 830 831 /* Checkin full IP header plus 8 bytes of protocol to 832 * avoid additional coding at protocol handlers. 833 */ 834 if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) { 835 __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS); 836 return; 837 } 838 839 raw_icmp_error(skb, protocol, info); 840 841 ipprot = rcu_dereference(inet_protos[protocol]); 842 if (ipprot && ipprot->err_handler) 843 ipprot->err_handler(skb, info); 844 } 845 846 static bool icmp_tag_validation(int proto) 847 { 848 bool ok; 849 850 rcu_read_lock(); 851 ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation; 852 rcu_read_unlock(); 853 return ok; 854 } 855 856 /* 857 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and 858 * ICMP_PARAMETERPROB. 859 */ 860 861 static enum skb_drop_reason icmp_unreach(struct sk_buff *skb) 862 { 863 enum skb_drop_reason reason = SKB_NOT_DROPPED_YET; 864 const struct iphdr *iph; 865 struct icmphdr *icmph; 866 struct net *net; 867 u32 info = 0; 868 869 net = dev_net(skb_dst(skb)->dev); 870 871 /* 872 * Incomplete header ? 873 * Only checks for the IP header, there should be an 874 * additional check for longer headers in upper levels. 875 */ 876 877 if (!pskb_may_pull(skb, sizeof(struct iphdr))) 878 goto out_err; 879 880 icmph = icmp_hdr(skb); 881 iph = (const struct iphdr *)skb->data; 882 883 if (iph->ihl < 5) { /* Mangled header, drop. */ 884 reason = SKB_DROP_REASON_IP_INHDR; 885 goto out_err; 886 } 887 888 switch (icmph->type) { 889 case ICMP_DEST_UNREACH: 890 switch (icmph->code & 15) { 891 case ICMP_NET_UNREACH: 892 case ICMP_HOST_UNREACH: 893 case ICMP_PROT_UNREACH: 894 case ICMP_PORT_UNREACH: 895 break; 896 case ICMP_FRAG_NEEDED: 897 /* for documentation of the ip_no_pmtu_disc 898 * values please see 899 * Documentation/networking/ip-sysctl.rst 900 */ 901 switch (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) { 902 default: 903 net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n", 904 &iph->daddr); 905 break; 906 case 2: 907 goto out; 908 case 3: 909 if (!icmp_tag_validation(iph->protocol)) 910 goto out; 911 fallthrough; 912 case 0: 913 info = ntohs(icmph->un.frag.mtu); 914 } 915 break; 916 case ICMP_SR_FAILED: 917 net_dbg_ratelimited("%pI4: Source Route Failed\n", 918 &iph->daddr); 919 break; 920 default: 921 break; 922 } 923 if (icmph->code > NR_ICMP_UNREACH) 924 goto out; 925 break; 926 case ICMP_PARAMETERPROB: 927 info = ntohl(icmph->un.gateway) >> 24; 928 break; 929 case ICMP_TIME_EXCEEDED: 930 __ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS); 931 if (icmph->code == ICMP_EXC_FRAGTIME) 932 goto out; 933 break; 934 } 935 936 /* 937 * Throw it at our lower layers 938 * 939 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed 940 * header. 941 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the 942 * transport layer. 943 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to 944 * transport layer. 945 */ 946 947 /* 948 * Check the other end isn't violating RFC 1122. Some routers send 949 * bogus responses to broadcast frames. If you see this message 950 * first check your netmask matches at both ends, if it does then 951 * get the other vendor to fix their kit. 952 */ 953 954 if (!READ_ONCE(net->ipv4.sysctl_icmp_ignore_bogus_error_responses) && 955 inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) { 956 net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n", 957 &ip_hdr(skb)->saddr, 958 icmph->type, icmph->code, 959 &iph->daddr, skb->dev->name); 960 goto out; 961 } 962 963 icmp_socket_deliver(skb, info); 964 965 out: 966 return reason; 967 out_err: 968 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 969 return reason ?: SKB_DROP_REASON_NOT_SPECIFIED; 970 } 971 972 973 /* 974 * Handle ICMP_REDIRECT. 975 */ 976 977 static enum skb_drop_reason icmp_redirect(struct sk_buff *skb) 978 { 979 if (skb->len < sizeof(struct iphdr)) { 980 __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS); 981 return SKB_DROP_REASON_PKT_TOO_SMALL; 982 } 983 984 if (!pskb_may_pull(skb, sizeof(struct iphdr))) { 985 /* there aught to be a stat */ 986 return SKB_DROP_REASON_NOMEM; 987 } 988 989 icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway)); 990 return SKB_NOT_DROPPED_YET; 991 } 992 993 /* 994 * Handle ICMP_ECHO ("ping") and ICMP_EXT_ECHO ("PROBE") requests. 995 * 996 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo 997 * requests. 998 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be 999 * included in the reply. 1000 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring 1001 * echo requests, MUST have default=NOT. 1002 * RFC 8335: 8 MUST have a config option to enable/disable ICMP 1003 * Extended Echo Functionality, MUST be disabled by default 1004 * See also WRT handling of options once they are done and working. 1005 */ 1006 1007 static enum skb_drop_reason icmp_echo(struct sk_buff *skb) 1008 { 1009 struct icmp_bxm icmp_param; 1010 struct net *net; 1011 1012 net = dev_net(skb_dst(skb)->dev); 1013 /* should there be an ICMP stat for ignored echos? */ 1014 if (READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_all)) 1015 return SKB_NOT_DROPPED_YET; 1016 1017 icmp_param.data.icmph = *icmp_hdr(skb); 1018 icmp_param.skb = skb; 1019 icmp_param.offset = 0; 1020 icmp_param.data_len = skb->len; 1021 icmp_param.head_len = sizeof(struct icmphdr); 1022 1023 if (icmp_param.data.icmph.type == ICMP_ECHO) 1024 icmp_param.data.icmph.type = ICMP_ECHOREPLY; 1025 else if (!icmp_build_probe(skb, &icmp_param.data.icmph)) 1026 return SKB_NOT_DROPPED_YET; 1027 1028 icmp_reply(&icmp_param, skb); 1029 return SKB_NOT_DROPPED_YET; 1030 } 1031 1032 /* Helper for icmp_echo and icmpv6_echo_reply. 1033 * Searches for net_device that matches PROBE interface identifier 1034 * and builds PROBE reply message in icmphdr. 1035 * 1036 * Returns false if PROBE responses are disabled via sysctl 1037 */ 1038 1039 bool icmp_build_probe(struct sk_buff *skb, struct icmphdr *icmphdr) 1040 { 1041 struct icmp_ext_hdr *ext_hdr, _ext_hdr; 1042 struct icmp_ext_echo_iio *iio, _iio; 1043 struct net *net = dev_net(skb->dev); 1044 struct inet6_dev *in6_dev; 1045 struct in_device *in_dev; 1046 struct net_device *dev; 1047 char buff[IFNAMSIZ]; 1048 u16 ident_len; 1049 u8 status; 1050 1051 if (!READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe)) 1052 return false; 1053 1054 /* We currently only support probing interfaces on the proxy node 1055 * Check to ensure L-bit is set 1056 */ 1057 if (!(ntohs(icmphdr->un.echo.sequence) & 1)) 1058 return false; 1059 /* Clear status bits in reply message */ 1060 icmphdr->un.echo.sequence &= htons(0xFF00); 1061 if (icmphdr->type == ICMP_EXT_ECHO) 1062 icmphdr->type = ICMP_EXT_ECHOREPLY; 1063 else 1064 icmphdr->type = ICMPV6_EXT_ECHO_REPLY; 1065 ext_hdr = skb_header_pointer(skb, 0, sizeof(_ext_hdr), &_ext_hdr); 1066 /* Size of iio is class_type dependent. 1067 * Only check header here and assign length based on ctype in the switch statement 1068 */ 1069 iio = skb_header_pointer(skb, sizeof(_ext_hdr), sizeof(iio->extobj_hdr), &_iio); 1070 if (!ext_hdr || !iio) 1071 goto send_mal_query; 1072 if (ntohs(iio->extobj_hdr.length) <= sizeof(iio->extobj_hdr) || 1073 ntohs(iio->extobj_hdr.length) > sizeof(_iio)) 1074 goto send_mal_query; 1075 ident_len = ntohs(iio->extobj_hdr.length) - sizeof(iio->extobj_hdr); 1076 iio = skb_header_pointer(skb, sizeof(_ext_hdr), 1077 sizeof(iio->extobj_hdr) + ident_len, &_iio); 1078 if (!iio) 1079 goto send_mal_query; 1080 1081 status = 0; 1082 dev = NULL; 1083 switch (iio->extobj_hdr.class_type) { 1084 case ICMP_EXT_ECHO_CTYPE_NAME: 1085 if (ident_len >= IFNAMSIZ) 1086 goto send_mal_query; 1087 memset(buff, 0, sizeof(buff)); 1088 memcpy(buff, &iio->ident.name, ident_len); 1089 dev = dev_get_by_name(net, buff); 1090 break; 1091 case ICMP_EXT_ECHO_CTYPE_INDEX: 1092 if (ident_len != sizeof(iio->ident.ifindex)) 1093 goto send_mal_query; 1094 dev = dev_get_by_index(net, ntohl(iio->ident.ifindex)); 1095 break; 1096 case ICMP_EXT_ECHO_CTYPE_ADDR: 1097 if (ident_len < sizeof(iio->ident.addr.ctype3_hdr) || 1098 ident_len != sizeof(iio->ident.addr.ctype3_hdr) + 1099 iio->ident.addr.ctype3_hdr.addrlen) 1100 goto send_mal_query; 1101 switch (ntohs(iio->ident.addr.ctype3_hdr.afi)) { 1102 case ICMP_AFI_IP: 1103 if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in_addr)) 1104 goto send_mal_query; 1105 dev = ip_dev_find(net, iio->ident.addr.ip_addr.ipv4_addr); 1106 break; 1107 #if IS_ENABLED(CONFIG_IPV6) 1108 case ICMP_AFI_IP6: 1109 if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in6_addr)) 1110 goto send_mal_query; 1111 dev = ipv6_stub->ipv6_dev_find(net, &iio->ident.addr.ip_addr.ipv6_addr, dev); 1112 dev_hold(dev); 1113 break; 1114 #endif 1115 default: 1116 goto send_mal_query; 1117 } 1118 break; 1119 default: 1120 goto send_mal_query; 1121 } 1122 if (!dev) { 1123 icmphdr->code = ICMP_EXT_CODE_NO_IF; 1124 return true; 1125 } 1126 /* Fill bits in reply message */ 1127 if (dev->flags & IFF_UP) 1128 status |= ICMP_EXT_ECHOREPLY_ACTIVE; 1129 1130 in_dev = __in_dev_get_rcu(dev); 1131 if (in_dev && rcu_access_pointer(in_dev->ifa_list)) 1132 status |= ICMP_EXT_ECHOREPLY_IPV4; 1133 1134 in6_dev = __in6_dev_get(dev); 1135 if (in6_dev && !list_empty(&in6_dev->addr_list)) 1136 status |= ICMP_EXT_ECHOREPLY_IPV6; 1137 1138 dev_put(dev); 1139 icmphdr->un.echo.sequence |= htons(status); 1140 return true; 1141 send_mal_query: 1142 icmphdr->code = ICMP_EXT_CODE_MAL_QUERY; 1143 return true; 1144 } 1145 EXPORT_SYMBOL_GPL(icmp_build_probe); 1146 1147 /* 1148 * Handle ICMP Timestamp requests. 1149 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests. 1150 * SHOULD be in the kernel for minimum random latency. 1151 * MUST be accurate to a few minutes. 1152 * MUST be updated at least at 15Hz. 1153 */ 1154 static enum skb_drop_reason icmp_timestamp(struct sk_buff *skb) 1155 { 1156 struct icmp_bxm icmp_param; 1157 /* 1158 * Too short. 1159 */ 1160 if (skb->len < 4) 1161 goto out_err; 1162 1163 /* 1164 * Fill in the current time as ms since midnight UT: 1165 */ 1166 icmp_param.data.times[1] = inet_current_timestamp(); 1167 icmp_param.data.times[2] = icmp_param.data.times[1]; 1168 1169 BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4)); 1170 1171 icmp_param.data.icmph = *icmp_hdr(skb); 1172 icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY; 1173 icmp_param.data.icmph.code = 0; 1174 icmp_param.skb = skb; 1175 icmp_param.offset = 0; 1176 icmp_param.data_len = 0; 1177 icmp_param.head_len = sizeof(struct icmphdr) + 12; 1178 icmp_reply(&icmp_param, skb); 1179 return SKB_NOT_DROPPED_YET; 1180 1181 out_err: 1182 __ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS); 1183 return SKB_DROP_REASON_PKT_TOO_SMALL; 1184 } 1185 1186 static enum skb_drop_reason icmp_discard(struct sk_buff *skb) 1187 { 1188 /* pretend it was a success */ 1189 return SKB_NOT_DROPPED_YET; 1190 } 1191 1192 /* 1193 * Deal with incoming ICMP packets. 1194 */ 1195 int icmp_rcv(struct sk_buff *skb) 1196 { 1197 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 1198 struct rtable *rt = skb_rtable(skb); 1199 struct net *net = dev_net(rt->dst.dev); 1200 struct icmphdr *icmph; 1201 1202 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { 1203 struct sec_path *sp = skb_sec_path(skb); 1204 int nh; 1205 1206 if (!(sp && sp->xvec[sp->len - 1]->props.flags & 1207 XFRM_STATE_ICMP)) { 1208 reason = SKB_DROP_REASON_XFRM_POLICY; 1209 goto drop; 1210 } 1211 1212 if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr))) 1213 goto drop; 1214 1215 nh = skb_network_offset(skb); 1216 skb_set_network_header(skb, sizeof(*icmph)); 1217 1218 if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, 1219 skb)) { 1220 reason = SKB_DROP_REASON_XFRM_POLICY; 1221 goto drop; 1222 } 1223 1224 skb_set_network_header(skb, nh); 1225 } 1226 1227 __ICMP_INC_STATS(net, ICMP_MIB_INMSGS); 1228 1229 if (skb_checksum_simple_validate(skb)) 1230 goto csum_error; 1231 1232 if (!pskb_pull(skb, sizeof(*icmph))) 1233 goto error; 1234 1235 icmph = icmp_hdr(skb); 1236 1237 ICMPMSGIN_INC_STATS(net, icmph->type); 1238 1239 /* Check for ICMP Extended Echo (PROBE) messages */ 1240 if (icmph->type == ICMP_EXT_ECHO) { 1241 /* We can't use icmp_pointers[].handler() because it is an array of 1242 * size NR_ICMP_TYPES + 1 (19 elements) and PROBE has code 42. 1243 */ 1244 reason = icmp_echo(skb); 1245 goto reason_check; 1246 } 1247 1248 if (icmph->type == ICMP_EXT_ECHOREPLY) { 1249 reason = ping_rcv(skb); 1250 goto reason_check; 1251 } 1252 1253 /* 1254 * 18 is the highest 'known' ICMP type. Anything else is a mystery 1255 * 1256 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently 1257 * discarded. 1258 */ 1259 if (icmph->type > NR_ICMP_TYPES) { 1260 reason = SKB_DROP_REASON_UNHANDLED_PROTO; 1261 goto error; 1262 } 1263 1264 /* 1265 * Parse the ICMP message 1266 */ 1267 1268 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 1269 /* 1270 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be 1271 * silently ignored (we let user decide with a sysctl). 1272 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently 1273 * discarded if to broadcast/multicast. 1274 */ 1275 if ((icmph->type == ICMP_ECHO || 1276 icmph->type == ICMP_TIMESTAMP) && 1277 READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_broadcasts)) { 1278 reason = SKB_DROP_REASON_INVALID_PROTO; 1279 goto error; 1280 } 1281 if (icmph->type != ICMP_ECHO && 1282 icmph->type != ICMP_TIMESTAMP && 1283 icmph->type != ICMP_ADDRESS && 1284 icmph->type != ICMP_ADDRESSREPLY) { 1285 reason = SKB_DROP_REASON_INVALID_PROTO; 1286 goto error; 1287 } 1288 } 1289 1290 reason = icmp_pointers[icmph->type].handler(skb); 1291 reason_check: 1292 if (!reason) { 1293 consume_skb(skb); 1294 return NET_RX_SUCCESS; 1295 } 1296 1297 drop: 1298 kfree_skb_reason(skb, reason); 1299 return NET_RX_DROP; 1300 csum_error: 1301 reason = SKB_DROP_REASON_ICMP_CSUM; 1302 __ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS); 1303 error: 1304 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS); 1305 goto drop; 1306 } 1307 1308 static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off) 1309 { 1310 struct icmp_extobj_hdr *objh, _objh; 1311 struct icmp_ext_hdr *exth, _exth; 1312 u16 olen; 1313 1314 exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth); 1315 if (!exth) 1316 return false; 1317 if (exth->version != 2) 1318 return true; 1319 1320 if (exth->checksum && 1321 csum_fold(skb_checksum(skb, off, skb->len - off, 0))) 1322 return false; 1323 1324 off += sizeof(_exth); 1325 while (off < skb->len) { 1326 objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh); 1327 if (!objh) 1328 return false; 1329 1330 olen = ntohs(objh->length); 1331 if (olen < sizeof(_objh)) 1332 return false; 1333 1334 off += olen; 1335 if (off > skb->len) 1336 return false; 1337 } 1338 1339 return true; 1340 } 1341 1342 void ip_icmp_error_rfc4884(const struct sk_buff *skb, 1343 struct sock_ee_data_rfc4884 *out, 1344 int thlen, int off) 1345 { 1346 int hlen; 1347 1348 /* original datagram headers: end of icmph to payload (skb->data) */ 1349 hlen = -skb_transport_offset(skb) - thlen; 1350 1351 /* per rfc 4884: minimal datagram length of 128 bytes */ 1352 if (off < 128 || off < hlen) 1353 return; 1354 1355 /* kernel has stripped headers: return payload offset in bytes */ 1356 off -= hlen; 1357 if (off + sizeof(struct icmp_ext_hdr) > skb->len) 1358 return; 1359 1360 out->len = off; 1361 1362 if (!ip_icmp_error_rfc4884_validate(skb, off)) 1363 out->flags |= SO_EE_RFC4884_FLAG_INVALID; 1364 } 1365 EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884); 1366 1367 int icmp_err(struct sk_buff *skb, u32 info) 1368 { 1369 struct iphdr *iph = (struct iphdr *)skb->data; 1370 int offset = iph->ihl<<2; 1371 struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset); 1372 int type = icmp_hdr(skb)->type; 1373 int code = icmp_hdr(skb)->code; 1374 struct net *net = dev_net(skb->dev); 1375 1376 /* 1377 * Use ping_err to handle all icmp errors except those 1378 * triggered by ICMP_ECHOREPLY which sent from kernel. 1379 */ 1380 if (icmph->type != ICMP_ECHOREPLY) { 1381 ping_err(skb, offset, info); 1382 return 0; 1383 } 1384 1385 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 1386 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP); 1387 else if (type == ICMP_REDIRECT) 1388 ipv4_redirect(skb, net, 0, IPPROTO_ICMP); 1389 1390 return 0; 1391 } 1392 1393 /* 1394 * This table is the definition of how we handle ICMP. 1395 */ 1396 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = { 1397 [ICMP_ECHOREPLY] = { 1398 .handler = ping_rcv, 1399 }, 1400 [1] = { 1401 .handler = icmp_discard, 1402 .error = 1, 1403 }, 1404 [2] = { 1405 .handler = icmp_discard, 1406 .error = 1, 1407 }, 1408 [ICMP_DEST_UNREACH] = { 1409 .handler = icmp_unreach, 1410 .error = 1, 1411 }, 1412 [ICMP_SOURCE_QUENCH] = { 1413 .handler = icmp_unreach, 1414 .error = 1, 1415 }, 1416 [ICMP_REDIRECT] = { 1417 .handler = icmp_redirect, 1418 .error = 1, 1419 }, 1420 [6] = { 1421 .handler = icmp_discard, 1422 .error = 1, 1423 }, 1424 [7] = { 1425 .handler = icmp_discard, 1426 .error = 1, 1427 }, 1428 [ICMP_ECHO] = { 1429 .handler = icmp_echo, 1430 }, 1431 [9] = { 1432 .handler = icmp_discard, 1433 .error = 1, 1434 }, 1435 [10] = { 1436 .handler = icmp_discard, 1437 .error = 1, 1438 }, 1439 [ICMP_TIME_EXCEEDED] = { 1440 .handler = icmp_unreach, 1441 .error = 1, 1442 }, 1443 [ICMP_PARAMETERPROB] = { 1444 .handler = icmp_unreach, 1445 .error = 1, 1446 }, 1447 [ICMP_TIMESTAMP] = { 1448 .handler = icmp_timestamp, 1449 }, 1450 [ICMP_TIMESTAMPREPLY] = { 1451 .handler = icmp_discard, 1452 }, 1453 [ICMP_INFO_REQUEST] = { 1454 .handler = icmp_discard, 1455 }, 1456 [ICMP_INFO_REPLY] = { 1457 .handler = icmp_discard, 1458 }, 1459 [ICMP_ADDRESS] = { 1460 .handler = icmp_discard, 1461 }, 1462 [ICMP_ADDRESSREPLY] = { 1463 .handler = icmp_discard, 1464 }, 1465 }; 1466 1467 static int __net_init icmp_sk_init(struct net *net) 1468 { 1469 /* Control parameters for ECHO replies. */ 1470 net->ipv4.sysctl_icmp_echo_ignore_all = 0; 1471 net->ipv4.sysctl_icmp_echo_enable_probe = 0; 1472 net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1; 1473 1474 /* Control parameter - ignore bogus broadcast responses? */ 1475 net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1; 1476 1477 /* 1478 * Configurable global rate limit. 1479 * 1480 * ratelimit defines tokens/packet consumed for dst->rate_token 1481 * bucket ratemask defines which icmp types are ratelimited by 1482 * setting it's bit position. 1483 * 1484 * default: 1485 * dest unreachable (3), source quench (4), 1486 * time exceeded (11), parameter problem (12) 1487 */ 1488 1489 net->ipv4.sysctl_icmp_ratelimit = 1 * HZ; 1490 net->ipv4.sysctl_icmp_ratemask = 0x1818; 1491 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0; 1492 net->ipv4.sysctl_icmp_msgs_per_sec = 1000; 1493 net->ipv4.sysctl_icmp_msgs_burst = 50; 1494 1495 return 0; 1496 } 1497 1498 static struct pernet_operations __net_initdata icmp_sk_ops = { 1499 .init = icmp_sk_init, 1500 }; 1501 1502 int __init icmp_init(void) 1503 { 1504 int err, i; 1505 1506 for_each_possible_cpu(i) { 1507 struct sock *sk; 1508 1509 err = inet_ctl_sock_create(&sk, PF_INET, 1510 SOCK_RAW, IPPROTO_ICMP, &init_net); 1511 if (err < 0) 1512 return err; 1513 1514 per_cpu(ipv4_icmp_sk, i) = sk; 1515 1516 /* Enough space for 2 64K ICMP packets, including 1517 * sk_buff/skb_shared_info struct overhead. 1518 */ 1519 sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024); 1520 1521 /* 1522 * Speedup sock_wfree() 1523 */ 1524 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); 1525 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT; 1526 } 1527 return register_pernet_subsys(&icmp_sk_ops); 1528 } 1529