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