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 #define CREATE_TRACE_POINTS 97 #include <trace/events/icmp.h> 98 99 /* 100 * Build xmit assembly blocks 101 */ 102 103 struct icmp_bxm { 104 struct sk_buff *skb; 105 int offset; 106 int data_len; 107 108 struct { 109 struct icmphdr icmph; 110 __be32 times[3]; 111 } data; 112 int head_len; 113 struct ip_options_data replyopts; 114 }; 115 116 /* An array of errno for error messages from dest unreach. */ 117 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */ 118 119 const struct icmp_err icmp_err_convert[] = { 120 { 121 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */ 122 .fatal = 0, 123 }, 124 { 125 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */ 126 .fatal = 0, 127 }, 128 { 129 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */, 130 .fatal = 1, 131 }, 132 { 133 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */ 134 .fatal = 1, 135 }, 136 { 137 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */ 138 .fatal = 0, 139 }, 140 { 141 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */ 142 .fatal = 0, 143 }, 144 { 145 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */ 146 .fatal = 1, 147 }, 148 { 149 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */ 150 .fatal = 1, 151 }, 152 { 153 .errno = ENONET, /* ICMP_HOST_ISOLATED */ 154 .fatal = 1, 155 }, 156 { 157 .errno = ENETUNREACH, /* ICMP_NET_ANO */ 158 .fatal = 1, 159 }, 160 { 161 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */ 162 .fatal = 1, 163 }, 164 { 165 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */ 166 .fatal = 0, 167 }, 168 { 169 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */ 170 .fatal = 0, 171 }, 172 { 173 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */ 174 .fatal = 1, 175 }, 176 { 177 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */ 178 .fatal = 1, 179 }, 180 { 181 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */ 182 .fatal = 1, 183 }, 184 }; 185 EXPORT_SYMBOL(icmp_err_convert); 186 187 /* 188 * ICMP control array. This specifies what to do with each ICMP. 189 */ 190 191 struct icmp_control { 192 enum skb_drop_reason (*handler)(struct sk_buff *skb); 193 short error; /* This ICMP is classed as an error message */ 194 }; 195 196 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1]; 197 198 static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk); 199 200 /* Called with BH disabled */ 201 static inline struct sock *icmp_xmit_lock(struct net *net) 202 { 203 struct sock *sk; 204 205 sk = this_cpu_read(ipv4_icmp_sk); 206 207 if (unlikely(!spin_trylock(&sk->sk_lock.slock))) { 208 /* This can happen if the output path signals a 209 * dst_link_failure() for an outgoing ICMP packet. 210 */ 211 return NULL; 212 } 213 sock_net_set(sk, net); 214 return sk; 215 } 216 217 static inline void icmp_xmit_unlock(struct sock *sk) 218 { 219 sock_net_set(sk, &init_net); 220 spin_unlock(&sk->sk_lock.slock); 221 } 222 223 /** 224 * icmp_global_allow - Are we allowed to send one more ICMP message ? 225 * @net: network namespace 226 * 227 * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec. 228 * Returns false if we reached the limit and can not send another packet. 229 * Works in tandem with icmp_global_consume(). 230 */ 231 bool icmp_global_allow(struct net *net) 232 { 233 u32 delta, now, oldstamp; 234 int incr, new, old; 235 236 /* Note: many cpus could find this condition true. 237 * Then later icmp_global_consume() could consume more credits, 238 * this is an acceptable race. 239 */ 240 if (atomic_read(&net->ipv4.icmp_global_credit) > 0) 241 return true; 242 243 now = jiffies; 244 oldstamp = READ_ONCE(net->ipv4.icmp_global_stamp); 245 delta = min_t(u32, now - oldstamp, HZ); 246 if (delta < HZ / 50) 247 return false; 248 249 incr = READ_ONCE(net->ipv4.sysctl_icmp_msgs_per_sec) * delta / HZ; 250 if (!incr) 251 return false; 252 253 if (cmpxchg(&net->ipv4.icmp_global_stamp, oldstamp, now) == oldstamp) { 254 old = atomic_read(&net->ipv4.icmp_global_credit); 255 do { 256 new = min(old + incr, READ_ONCE(net->ipv4.sysctl_icmp_msgs_burst)); 257 } while (!atomic_try_cmpxchg(&net->ipv4.icmp_global_credit, &old, new)); 258 } 259 return true; 260 } 261 EXPORT_SYMBOL(icmp_global_allow); 262 263 void icmp_global_consume(struct net *net) 264 { 265 int credits = get_random_u32_below(3); 266 267 /* Note: this might make icmp_global.credit negative. */ 268 if (credits) 269 atomic_sub(credits, &net->ipv4.icmp_global_credit); 270 } 271 EXPORT_SYMBOL(icmp_global_consume); 272 273 static bool icmpv4_mask_allow(struct net *net, int type, int code) 274 { 275 if (type > NR_ICMP_TYPES) 276 return true; 277 278 /* Don't limit PMTU discovery. */ 279 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 280 return true; 281 282 /* Limit if icmp type is enabled in ratemask. */ 283 if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask))) 284 return true; 285 286 return false; 287 } 288 289 static bool icmpv4_global_allow(struct net *net, int type, int code, 290 bool *apply_ratelimit) 291 { 292 if (icmpv4_mask_allow(net, type, code)) 293 return true; 294 295 if (icmp_global_allow(net)) { 296 *apply_ratelimit = true; 297 return true; 298 } 299 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL); 300 return false; 301 } 302 303 /* 304 * Send an ICMP frame. 305 */ 306 307 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt, 308 struct flowi4 *fl4, int type, int code, 309 bool apply_ratelimit) 310 { 311 struct dst_entry *dst = &rt->dst; 312 struct inet_peer *peer; 313 bool rc = true; 314 int vif; 315 316 if (!apply_ratelimit) 317 return true; 318 319 /* No rate limit on loopback */ 320 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) 321 goto out; 322 323 vif = l3mdev_master_ifindex(dst->dev); 324 peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1); 325 rc = inet_peer_xrlim_allow(peer, 326 READ_ONCE(net->ipv4.sysctl_icmp_ratelimit)); 327 if (peer) 328 inet_putpeer(peer); 329 out: 330 if (!rc) 331 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST); 332 else 333 icmp_global_consume(net); 334 return rc; 335 } 336 337 /* 338 * Maintain the counters used in the SNMP statistics for outgoing ICMP 339 */ 340 void icmp_out_count(struct net *net, unsigned char type) 341 { 342 ICMPMSGOUT_INC_STATS(net, type); 343 ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS); 344 } 345 346 /* 347 * Checksum each fragment, and on the first include the headers and final 348 * checksum. 349 */ 350 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd, 351 struct sk_buff *skb) 352 { 353 struct icmp_bxm *icmp_param = from; 354 __wsum csum; 355 356 csum = skb_copy_and_csum_bits(icmp_param->skb, 357 icmp_param->offset + offset, 358 to, len); 359 360 skb->csum = csum_block_add(skb->csum, csum, odd); 361 if (icmp_pointers[icmp_param->data.icmph.type].error) 362 nf_ct_attach(skb, icmp_param->skb); 363 return 0; 364 } 365 366 static void icmp_push_reply(struct sock *sk, 367 struct icmp_bxm *icmp_param, 368 struct flowi4 *fl4, 369 struct ipcm_cookie *ipc, struct rtable **rt) 370 { 371 struct sk_buff *skb; 372 373 if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param, 374 icmp_param->data_len+icmp_param->head_len, 375 icmp_param->head_len, 376 ipc, rt, MSG_DONTWAIT) < 0) { 377 __ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS); 378 ip_flush_pending_frames(sk); 379 } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) { 380 struct icmphdr *icmph = icmp_hdr(skb); 381 __wsum csum; 382 struct sk_buff *skb1; 383 384 csum = csum_partial_copy_nocheck((void *)&icmp_param->data, 385 (char *)icmph, 386 icmp_param->head_len); 387 skb_queue_walk(&sk->sk_write_queue, skb1) { 388 csum = csum_add(csum, skb1->csum); 389 } 390 icmph->checksum = csum_fold(csum); 391 skb->ip_summed = CHECKSUM_NONE; 392 ip_push_pending_frames(sk, fl4); 393 } 394 } 395 396 /* 397 * Driving logic for building and sending ICMP messages. 398 */ 399 400 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) 401 { 402 struct ipcm_cookie ipc; 403 struct rtable *rt = skb_rtable(skb); 404 struct net *net = dev_net(rt->dst.dev); 405 bool apply_ratelimit = false; 406 struct flowi4 fl4; 407 struct sock *sk; 408 struct inet_sock *inet; 409 __be32 daddr, saddr; 410 u32 mark = IP4_REPLY_MARK(net, skb->mark); 411 int type = icmp_param->data.icmph.type; 412 int code = icmp_param->data.icmph.code; 413 414 if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb)) 415 return; 416 417 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */ 418 local_bh_disable(); 419 420 /* is global icmp_msgs_per_sec exhausted ? */ 421 if (!icmpv4_global_allow(net, type, code, &apply_ratelimit)) 422 goto out_bh_enable; 423 424 sk = icmp_xmit_lock(net); 425 if (!sk) 426 goto out_bh_enable; 427 inet = inet_sk(sk); 428 429 icmp_param->data.icmph.checksum = 0; 430 431 ipcm_init(&ipc); 432 inet->tos = ip_hdr(skb)->tos; 433 ipc.sockc.mark = mark; 434 daddr = ipc.addr = ip_hdr(skb)->saddr; 435 saddr = fib_compute_spec_dst(skb); 436 437 if (icmp_param->replyopts.opt.opt.optlen) { 438 ipc.opt = &icmp_param->replyopts.opt; 439 if (ipc.opt->opt.srr) 440 daddr = icmp_param->replyopts.opt.opt.faddr; 441 } 442 memset(&fl4, 0, sizeof(fl4)); 443 fl4.daddr = daddr; 444 fl4.saddr = saddr; 445 fl4.flowi4_mark = mark; 446 fl4.flowi4_uid = sock_net_uid(net, NULL); 447 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos); 448 fl4.flowi4_proto = IPPROTO_ICMP; 449 fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev); 450 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4)); 451 rt = ip_route_output_key(net, &fl4); 452 if (IS_ERR(rt)) 453 goto out_unlock; 454 if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit)) 455 icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt); 456 ip_rt_put(rt); 457 out_unlock: 458 icmp_xmit_unlock(sk); 459 out_bh_enable: 460 local_bh_enable(); 461 } 462 463 /* 464 * The device used for looking up which routing table to use for sending an ICMP 465 * error is preferably the source whenever it is set, which should ensure the 466 * icmp error can be sent to the source host, else lookup using the routing 467 * table of the destination device, else use the main routing table (index 0). 468 */ 469 static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb) 470 { 471 struct net_device *route_lookup_dev = NULL; 472 473 if (skb->dev) 474 route_lookup_dev = skb->dev; 475 else if (skb_dst(skb)) 476 route_lookup_dev = skb_dst(skb)->dev; 477 return route_lookup_dev; 478 } 479 480 static struct rtable *icmp_route_lookup(struct net *net, 481 struct flowi4 *fl4, 482 struct sk_buff *skb_in, 483 const struct iphdr *iph, 484 __be32 saddr, u8 tos, u32 mark, 485 int type, int code, 486 struct icmp_bxm *param) 487 { 488 struct net_device *route_lookup_dev; 489 struct dst_entry *dst, *dst2; 490 struct rtable *rt, *rt2; 491 struct flowi4 fl4_dec; 492 int err; 493 494 memset(fl4, 0, sizeof(*fl4)); 495 fl4->daddr = (param->replyopts.opt.opt.srr ? 496 param->replyopts.opt.opt.faddr : iph->saddr); 497 fl4->saddr = saddr; 498 fl4->flowi4_mark = mark; 499 fl4->flowi4_uid = sock_net_uid(net, NULL); 500 fl4->flowi4_tos = RT_TOS(tos); 501 fl4->flowi4_proto = IPPROTO_ICMP; 502 fl4->fl4_icmp_type = type; 503 fl4->fl4_icmp_code = code; 504 route_lookup_dev = icmp_get_route_lookup_dev(skb_in); 505 fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev); 506 507 security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4)); 508 rt = ip_route_output_key_hash(net, fl4, skb_in); 509 if (IS_ERR(rt)) 510 return rt; 511 512 /* No need to clone since we're just using its address. */ 513 rt2 = rt; 514 515 dst = xfrm_lookup(net, &rt->dst, 516 flowi4_to_flowi(fl4), NULL, 0); 517 rt = dst_rtable(dst); 518 if (!IS_ERR(dst)) { 519 if (rt != rt2) 520 return rt; 521 } else if (PTR_ERR(dst) == -EPERM) { 522 rt = NULL; 523 } else { 524 return rt; 525 } 526 err = xfrm_decode_session_reverse(net, skb_in, flowi4_to_flowi(&fl4_dec), AF_INET); 527 if (err) 528 goto relookup_failed; 529 530 if (inet_addr_type_dev_table(net, route_lookup_dev, 531 fl4_dec.saddr) == RTN_LOCAL) { 532 rt2 = __ip_route_output_key(net, &fl4_dec); 533 if (IS_ERR(rt2)) 534 err = PTR_ERR(rt2); 535 } else { 536 struct flowi4 fl4_2 = {}; 537 unsigned long orefdst; 538 539 fl4_2.daddr = fl4_dec.saddr; 540 rt2 = ip_route_output_key(net, &fl4_2); 541 if (IS_ERR(rt2)) { 542 err = PTR_ERR(rt2); 543 goto relookup_failed; 544 } 545 /* Ugh! */ 546 orefdst = skb_in->_skb_refdst; /* save old refdst */ 547 skb_dst_set(skb_in, NULL); 548 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr, 549 tos, rt2->dst.dev); 550 551 dst_release(&rt2->dst); 552 rt2 = skb_rtable(skb_in); 553 skb_in->_skb_refdst = orefdst; /* restore old refdst */ 554 } 555 556 if (err) 557 goto relookup_failed; 558 559 dst2 = xfrm_lookup(net, &rt2->dst, flowi4_to_flowi(&fl4_dec), NULL, 560 XFRM_LOOKUP_ICMP); 561 rt2 = dst_rtable(dst2); 562 if (!IS_ERR(dst2)) { 563 dst_release(&rt->dst); 564 memcpy(fl4, &fl4_dec, sizeof(*fl4)); 565 rt = rt2; 566 } else if (PTR_ERR(dst2) == -EPERM) { 567 if (rt) 568 dst_release(&rt->dst); 569 return rt2; 570 } else { 571 err = PTR_ERR(dst2); 572 goto relookup_failed; 573 } 574 return rt; 575 576 relookup_failed: 577 if (rt) 578 return rt; 579 return ERR_PTR(err); 580 } 581 582 /* 583 * Send an ICMP message in response to a situation 584 * 585 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. 586 * MAY send more (we do). 587 * MUST NOT change this header information. 588 * MUST NOT reply to a multicast/broadcast IP address. 589 * MUST NOT reply to a multicast/broadcast MAC address. 590 * MUST reply to only the first fragment. 591 */ 592 593 void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info, 594 const struct ip_options *opt) 595 { 596 struct iphdr *iph; 597 int room; 598 struct icmp_bxm icmp_param; 599 struct rtable *rt = skb_rtable(skb_in); 600 bool apply_ratelimit = false; 601 struct ipcm_cookie ipc; 602 struct flowi4 fl4; 603 __be32 saddr; 604 u8 tos; 605 u32 mark; 606 struct net *net; 607 struct sock *sk; 608 609 if (!rt) 610 goto out; 611 612 if (rt->dst.dev) 613 net = dev_net(rt->dst.dev); 614 else if (skb_in->dev) 615 net = dev_net(skb_in->dev); 616 else 617 goto out; 618 619 /* 620 * Find the original header. It is expected to be valid, of course. 621 * Check this, icmp_send is called from the most obscure devices 622 * sometimes. 623 */ 624 iph = ip_hdr(skb_in); 625 626 if ((u8 *)iph < skb_in->head || 627 (skb_network_header(skb_in) + sizeof(*iph)) > 628 skb_tail_pointer(skb_in)) 629 goto out; 630 631 /* 632 * No replies to physical multicast/broadcast 633 */ 634 if (skb_in->pkt_type != PACKET_HOST) 635 goto out; 636 637 /* 638 * Now check at the protocol level 639 */ 640 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) 641 goto out; 642 643 /* 644 * Only reply to fragment 0. We byte re-order the constant 645 * mask for efficiency. 646 */ 647 if (iph->frag_off & htons(IP_OFFSET)) 648 goto out; 649 650 /* 651 * If we send an ICMP error to an ICMP error a mess would result.. 652 */ 653 if (icmp_pointers[type].error) { 654 /* 655 * We are an error, check if we are replying to an 656 * ICMP error 657 */ 658 if (iph->protocol == IPPROTO_ICMP) { 659 u8 _inner_type, *itp; 660 661 itp = skb_header_pointer(skb_in, 662 skb_network_header(skb_in) + 663 (iph->ihl << 2) + 664 offsetof(struct icmphdr, 665 type) - 666 skb_in->data, 667 sizeof(_inner_type), 668 &_inner_type); 669 if (!itp) 670 goto out; 671 672 /* 673 * Assume any unknown ICMP type is an error. This 674 * isn't specified by the RFC, but think about it.. 675 */ 676 if (*itp > NR_ICMP_TYPES || 677 icmp_pointers[*itp].error) 678 goto out; 679 } 680 } 681 682 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */ 683 local_bh_disable(); 684 685 /* Check global sysctl_icmp_msgs_per_sec ratelimit, unless 686 * incoming dev is loopback. If outgoing dev change to not be 687 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow) 688 */ 689 if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) && 690 !icmpv4_global_allow(net, type, code, &apply_ratelimit)) 691 goto out_bh_enable; 692 693 sk = icmp_xmit_lock(net); 694 if (!sk) 695 goto out_bh_enable; 696 697 /* 698 * Construct source address and options. 699 */ 700 701 saddr = iph->daddr; 702 if (!(rt->rt_flags & RTCF_LOCAL)) { 703 struct net_device *dev = NULL; 704 705 rcu_read_lock(); 706 if (rt_is_input_route(rt) && 707 READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)) 708 dev = dev_get_by_index_rcu(net, inet_iif(skb_in)); 709 710 if (dev) 711 saddr = inet_select_addr(dev, iph->saddr, 712 RT_SCOPE_LINK); 713 else 714 saddr = 0; 715 rcu_read_unlock(); 716 } 717 718 tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) | 719 IPTOS_PREC_INTERNETCONTROL) : 720 iph->tos; 721 mark = IP4_REPLY_MARK(net, skb_in->mark); 722 723 if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt)) 724 goto out_unlock; 725 726 727 /* 728 * Prepare data for ICMP header. 729 */ 730 731 icmp_param.data.icmph.type = type; 732 icmp_param.data.icmph.code = code; 733 icmp_param.data.icmph.un.gateway = info; 734 icmp_param.data.icmph.checksum = 0; 735 icmp_param.skb = skb_in; 736 icmp_param.offset = skb_network_offset(skb_in); 737 inet_sk(sk)->tos = tos; 738 ipcm_init(&ipc); 739 ipc.addr = iph->saddr; 740 ipc.opt = &icmp_param.replyopts.opt; 741 ipc.sockc.mark = mark; 742 743 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark, 744 type, code, &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