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