2 * NET3: Implementation of the ICMP protocol layer.
4 * Alan Cox, <alan@lxorguk.ukuu.org.uk>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Some of the function names and the icmp unreach table for this
12 * module were derived from [icmp.c 1.0.11 06/02/93] by
13 * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
14 * Other than that this module is a complete rewrite.
17 * Clemens Fruhwirth : introduce global icmp rate limiting
18 * with icmp type masking ability instead
19 * of broken per type icmp timeouts.
20 * Mike Shaver : RFC1122 checks.
21 * Alan Cox : Multicast ping reply as self.
22 * Alan Cox : Fix atomicity lockup in ip_build_xmit
24 * Alan Cox : Added 216,128 byte paths to the MTU
26 * Martin Mares : RFC1812 checks.
27 * Martin Mares : Can be configured to follow redirects
28 * if acting as a router _without_ a
29 * routing protocol (RFC 1812).
30 * Martin Mares : Echo requests may be configured to
31 * be ignored (RFC 1812).
32 * Martin Mares : Limitation of ICMP error message
33 * transmit rate (RFC 1812).
34 * Martin Mares : TOS and Precedence set correctly
36 * Martin Mares : Now copying as much data from the
37 * original packet as we can without
38 * exceeding 576 bytes (RFC 1812).
39 * Willy Konynenberg : Transparent proxying support.
40 * Keith Owens : RFC1191 correction for 4.2BSD based
42 * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
44 * Andi Kleen : Check all packet lengths properly
45 * and moved all kfree_skb() up to
47 * Andi Kleen : Move the rate limit bookkeeping
48 * into the dest entry and use a token
49 * bucket filter (thanks to ANK). Make
50 * the rates sysctl configurable.
51 * Yu Tianli : Fixed two ugly bugs in icmp_send
52 * - IP option length was accounted wrongly
53 * - ICMP header length was not accounted
55 * Tristan Greaves : Added sysctl option to ignore bogus
56 * broadcast responses from broken routers.
60 * - Should use skb_pull() instead of all the manual checking.
61 * This would also greatly simply some upper layer error handlers. --AK
65 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
67 #include <linux/module.h>
68 #include <linux/types.h>
69 #include <linux/jiffies.h>
70 #include <linux/kernel.h>
71 #include <linux/fcntl.h>
72 #include <linux/sysrq.h>
73 #include <linux/socket.h>
75 #include <linux/inet.h>
76 #include <linux/inetdevice.h>
77 #include <linux/netdevice.h>
78 #include <linux/string.h>
79 #include <linux/netfilter_ipv4.h>
80 #include <linux/slab.h>
81 #include <linux/locallock.h>
84 #include <net/route.h>
85 #include <net/protocol.h>
91 #include <linux/skbuff.h>
93 #include <linux/errno.h>
94 #include <linux/timer.h>
95 #include <linux/init.h>
96 #include <asm/uaccess.h>
97 #include <net/checksum.h>
99 #include <net/inet_common.h>
100 #include <net/ip_fib.h>
101 #include <net/l3mdev.h>
104 * Build xmit assembly blocks
113 struct icmphdr icmph;
117 struct ip_options_data replyopts;
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'. */
123 const struct icmp_err icmp_err_convert[] = {
125 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
129 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
133 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
137 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
141 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
145 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
149 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
153 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
157 .errno = ENONET, /* ICMP_HOST_ISOLATED */
161 .errno = ENETUNREACH, /* ICMP_NET_ANO */
165 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
169 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
173 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
177 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
181 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
185 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
189 EXPORT_SYMBOL(icmp_err_convert);
192 * ICMP control array. This specifies what to do with each ICMP.
195 struct icmp_control {
196 bool (*handler)(struct sk_buff *skb);
197 short error; /* This ICMP is classed as an error message */
200 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
203 * The ICMP socket(s). This is the most convenient way to flow control
204 * our ICMP output as well as maintain a clean interface throughout
205 * all layers. All Socketless IP sends will soon be gone.
207 * On SMP we have one ICMP socket per-cpu.
209 static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
211 static struct sock *icmp_sk(struct net *net)
213 return *this_cpu_ptr(net->ipv4.icmp_sk);
216 static inline struct sock *icmp_xmit_lock(struct net *net)
222 local_lock(icmp_sk_lock);
225 if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
226 /* This can happen if the output path signals a
227 * dst_link_failure() for an outgoing ICMP packet.
229 local_unlock(icmp_sk_lock);
236 static inline void icmp_xmit_unlock(struct sock *sk)
238 spin_unlock_bh(&sk->sk_lock.slock);
239 local_unlock(icmp_sk_lock);
242 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
243 int sysctl_icmp_msgs_burst __read_mostly = 50;
250 .lock = __SPIN_LOCK_UNLOCKED(icmp_global.lock),
254 * icmp_global_allow - Are we allowed to send one more ICMP message ?
256 * Uses a token bucket to limit our ICMP messages to sysctl_icmp_msgs_per_sec.
257 * Returns false if we reached the limit and can not send another packet.
258 * Note: called with BH disabled
260 bool icmp_global_allow(void)
262 u32 credit, delta, incr = 0, now = (u32)jiffies;
265 /* Check if token bucket is empty and cannot be refilled
266 * without taking the spinlock.
268 if (!icmp_global.credit) {
269 delta = min_t(u32, now - icmp_global.stamp, HZ);
274 spin_lock(&icmp_global.lock);
275 delta = min_t(u32, now - icmp_global.stamp, HZ);
276 if (delta >= HZ / 50) {
277 incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
279 icmp_global.stamp = now;
281 credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
286 icmp_global.credit = credit;
287 spin_unlock(&icmp_global.lock);
290 EXPORT_SYMBOL(icmp_global_allow);
293 * Send an ICMP frame.
296 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
297 struct flowi4 *fl4, int type, int code)
299 struct dst_entry *dst = &rt->dst;
302 if (type > NR_ICMP_TYPES)
305 /* Don't limit PMTU discovery. */
306 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
309 /* No rate limit on loopback */
310 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
313 /* Limit if icmp type is enabled in ratemask. */
314 if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
318 if (icmp_global_allow()) {
319 int vif = l3mdev_master_ifindex(dst->dev);
320 struct inet_peer *peer;
322 peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
323 rc = inet_peer_xrlim_allow(peer,
324 net->ipv4.sysctl_icmp_ratelimit);
333 * Maintain the counters used in the SNMP statistics for outgoing ICMP
335 void icmp_out_count(struct net *net, unsigned char type)
337 ICMPMSGOUT_INC_STATS(net, type);
338 ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
342 * Checksum each fragment, and on the first include the headers and final
345 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
348 struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
351 csum = skb_copy_and_csum_bits(icmp_param->skb,
352 icmp_param->offset + offset,
355 skb->csum = csum_block_add(skb->csum, csum, odd);
356 if (icmp_pointers[icmp_param->data.icmph.type].error)
357 nf_ct_attach(skb, icmp_param->skb);
361 static void icmp_push_reply(struct icmp_bxm *icmp_param,
363 struct ipcm_cookie *ipc, struct rtable **rt)
368 local_lock(icmp_sk_lock);
369 sk = icmp_sk(dev_net((*rt)->dst.dev));
370 if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
371 icmp_param->data_len+icmp_param->head_len,
372 icmp_param->head_len,
373 ipc, rt, MSG_DONTWAIT) < 0) {
374 ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
375 ip_flush_pending_frames(sk);
376 } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
377 struct icmphdr *icmph = icmp_hdr(skb);
379 struct sk_buff *skb1;
381 skb_queue_walk(&sk->sk_write_queue, skb1) {
382 csum = csum_add(csum, skb1->csum);
384 csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
386 icmp_param->head_len, csum);
387 icmph->checksum = csum_fold(csum);
388 skb->ip_summed = CHECKSUM_NONE;
389 ip_push_pending_frames(sk, fl4);
391 local_unlock(icmp_sk_lock);
395 * Driving logic for building and sending ICMP messages.
398 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
400 struct ipcm_cookie ipc;
401 struct rtable *rt = skb_rtable(skb);
402 struct net *net = dev_net(rt->dst.dev);
405 struct inet_sock *inet;
407 u32 mark = IP4_REPLY_MARK(net, skb->mark);
409 if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
412 sk = icmp_xmit_lock(net);
417 icmp_param->data.icmph.checksum = 0;
419 inet->tos = ip_hdr(skb)->tos;
421 daddr = ipc.addr = ip_hdr(skb)->saddr;
422 saddr = fib_compute_spec_dst(skb);
428 if (icmp_param->replyopts.opt.opt.optlen) {
429 ipc.opt = &icmp_param->replyopts.opt;
430 if (ipc.opt->opt.srr)
431 daddr = icmp_param->replyopts.opt.opt.faddr;
433 memset(&fl4, 0, sizeof(fl4));
436 fl4.flowi4_mark = mark;
437 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
438 fl4.flowi4_proto = IPPROTO_ICMP;
439 fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
440 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
441 rt = ip_route_output_key(net, &fl4);
444 if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
445 icmp_param->data.icmph.code))
446 icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
449 icmp_xmit_unlock(sk);
452 #ifdef CONFIG_IP_ROUTE_MULTIPATH
454 /* Source and destination is swapped. See ip_multipath_icmp_hash */
455 static int icmp_multipath_hash_skb(const struct sk_buff *skb)
457 const struct iphdr *iph = ip_hdr(skb);
459 return fib_multipath_hash(iph->daddr, iph->saddr);
464 #define icmp_multipath_hash_skb(skb) (-1)
468 static struct rtable *icmp_route_lookup(struct net *net,
470 struct sk_buff *skb_in,
471 const struct iphdr *iph,
472 __be32 saddr, u8 tos, u32 mark,
474 struct icmp_bxm *param)
476 struct rtable *rt, *rt2;
477 struct flowi4 fl4_dec;
480 memset(fl4, 0, sizeof(*fl4));
481 fl4->daddr = (param->replyopts.opt.opt.srr ?
482 param->replyopts.opt.opt.faddr : iph->saddr);
484 fl4->flowi4_mark = mark;
485 fl4->flowi4_tos = RT_TOS(tos);
486 fl4->flowi4_proto = IPPROTO_ICMP;
487 fl4->fl4_icmp_type = type;
488 fl4->fl4_icmp_code = code;
489 fl4->flowi4_oif = l3mdev_master_ifindex(skb_in->dev);
491 security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
492 rt = __ip_route_output_key_hash(net, fl4,
493 icmp_multipath_hash_skb(skb_in));
497 /* No need to clone since we're just using its address. */
500 rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
501 flowi4_to_flowi(fl4), NULL, 0);
505 } else if (PTR_ERR(rt) == -EPERM) {
510 err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
512 goto relookup_failed;
514 if (inet_addr_type_dev_table(net, skb_in->dev,
515 fl4_dec.saddr) == RTN_LOCAL) {
516 rt2 = __ip_route_output_key(net, &fl4_dec);
520 struct flowi4 fl4_2 = {};
521 unsigned long orefdst;
523 fl4_2.daddr = fl4_dec.saddr;
524 rt2 = ip_route_output_key(net, &fl4_2);
527 goto relookup_failed;
530 orefdst = skb_in->_skb_refdst; /* save old refdst */
531 skb_dst_set(skb_in, NULL);
532 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
533 RT_TOS(tos), rt2->dst.dev);
535 dst_release(&rt2->dst);
536 rt2 = skb_rtable(skb_in);
537 skb_in->_skb_refdst = orefdst; /* restore old refdst */
541 goto relookup_failed;
543 rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
544 flowi4_to_flowi(&fl4_dec), NULL,
547 dst_release(&rt->dst);
548 memcpy(fl4, &fl4_dec, sizeof(*fl4));
550 } else if (PTR_ERR(rt2) == -EPERM) {
552 dst_release(&rt->dst);
556 goto relookup_failed;
567 * Send an ICMP message in response to a situation
569 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
570 * MAY send more (we do).
571 * MUST NOT change this header information.
572 * MUST NOT reply to a multicast/broadcast IP address.
573 * MUST NOT reply to a multicast/broadcast MAC address.
574 * MUST reply to only the first fragment.
577 void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
581 struct icmp_bxm *icmp_param;
582 struct rtable *rt = skb_rtable(skb_in);
583 struct ipcm_cookie ipc;
593 net = dev_net(rt->dst.dev);
596 * Find the original header. It is expected to be valid, of course.
597 * Check this, icmp_send is called from the most obscure devices
600 iph = ip_hdr(skb_in);
602 if ((u8 *)iph < skb_in->head ||
603 (skb_network_header(skb_in) + sizeof(*iph)) >
604 skb_tail_pointer(skb_in))
608 * No replies to physical multicast/broadcast
610 if (skb_in->pkt_type != PACKET_HOST)
614 * Now check at the protocol level
616 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
620 * Only reply to fragment 0. We byte re-order the constant
621 * mask for efficiency.
623 if (iph->frag_off & htons(IP_OFFSET))
627 * If we send an ICMP error to an ICMP error a mess would result..
629 if (icmp_pointers[type].error) {
631 * We are an error, check if we are replying to an
634 if (iph->protocol == IPPROTO_ICMP) {
635 u8 _inner_type, *itp;
637 itp = skb_header_pointer(skb_in,
638 skb_network_header(skb_in) +
640 offsetof(struct icmphdr,
649 * Assume any unknown ICMP type is an error. This
650 * isn't specified by the RFC, but think about it..
652 if (*itp > NR_ICMP_TYPES ||
653 icmp_pointers[*itp].error)
658 icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
662 sk = icmp_xmit_lock(net);
667 * Construct source address and options.
671 if (!(rt->rt_flags & RTCF_LOCAL)) {
672 struct net_device *dev = NULL;
675 if (rt_is_input_route(rt) &&
676 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
677 dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
680 saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
686 tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
687 IPTOS_PREC_INTERNETCONTROL) :
689 mark = IP4_REPLY_MARK(net, skb_in->mark);
691 if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in))
696 * Prepare data for ICMP header.
699 icmp_param->data.icmph.type = type;
700 icmp_param->data.icmph.code = code;
701 icmp_param->data.icmph.un.gateway = info;
702 icmp_param->data.icmph.checksum = 0;
703 icmp_param->skb = skb_in;
704 icmp_param->offset = skb_network_offset(skb_in);
705 inet_sk(sk)->tos = tos;
707 ipc.addr = iph->saddr;
708 ipc.opt = &icmp_param->replyopts.opt;
713 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
714 type, code, icmp_param);
718 if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
721 /* RFC says return as much as we can without exceeding 576 bytes. */
723 room = dst_mtu(&rt->dst);
726 room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
727 room -= sizeof(struct icmphdr);
729 icmp_param->data_len = skb_in->len - icmp_param->offset;
730 if (icmp_param->data_len > room)
731 icmp_param->data_len = room;
732 icmp_param->head_len = sizeof(struct icmphdr);
734 icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
738 icmp_xmit_unlock(sk);
743 EXPORT_SYMBOL(icmp_send);
746 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
748 const struct iphdr *iph = (const struct iphdr *) skb->data;
749 const struct net_protocol *ipprot;
750 int protocol = iph->protocol;
752 /* Checkin full IP header plus 8 bytes of protocol to
753 * avoid additional coding at protocol handlers.
755 if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
756 ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
760 raw_icmp_error(skb, protocol, info);
762 ipprot = rcu_dereference(inet_protos[protocol]);
763 if (ipprot && ipprot->err_handler)
764 ipprot->err_handler(skb, info);
767 static bool icmp_tag_validation(int proto)
772 ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
778 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, ICMP_QUENCH, and
779 * ICMP_PARAMETERPROB.
782 static bool icmp_unreach(struct sk_buff *skb)
784 const struct iphdr *iph;
785 struct icmphdr *icmph;
789 net = dev_net(skb_dst(skb)->dev);
792 * Incomplete header ?
793 * Only checks for the IP header, there should be an
794 * additional check for longer headers in upper levels.
797 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
800 icmph = icmp_hdr(skb);
801 iph = (const struct iphdr *)skb->data;
803 if (iph->ihl < 5) /* Mangled header, drop. */
806 if (icmph->type == ICMP_DEST_UNREACH) {
807 switch (icmph->code & 15) {
808 case ICMP_NET_UNREACH:
809 case ICMP_HOST_UNREACH:
810 case ICMP_PROT_UNREACH:
811 case ICMP_PORT_UNREACH:
813 case ICMP_FRAG_NEEDED:
814 /* for documentation of the ip_no_pmtu_disc
816 * Documentation/networking/ip-sysctl.txt
818 switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
820 net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
826 if (!icmp_tag_validation(iph->protocol))
830 info = ntohs(icmph->un.frag.mtu);
834 net_dbg_ratelimited("%pI4: Source Route Failed\n",
840 if (icmph->code > NR_ICMP_UNREACH)
842 } else if (icmph->type == ICMP_PARAMETERPROB)
843 info = ntohl(icmph->un.gateway) >> 24;
846 * Throw it at our lower layers
848 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
850 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
852 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
857 * Check the other end isn't violating RFC 1122. Some routers send
858 * bogus responses to broadcast frames. If you see this message
859 * first check your netmask matches at both ends, if it does then
860 * get the other vendor to fix their kit.
863 if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
864 inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
865 net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
867 icmph->type, icmph->code,
868 &iph->daddr, skb->dev->name);
872 icmp_socket_deliver(skb, info);
877 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
883 * Handle ICMP_REDIRECT.
886 static bool icmp_redirect(struct sk_buff *skb)
888 if (skb->len < sizeof(struct iphdr)) {
889 ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
893 if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
894 /* there aught to be a stat */
898 icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
903 * 32bit and 64bit have different timestamp length, so we check for
904 * the cookie at offset 20 and verify it is repeated at offset 50
908 #define CO_SIZE sizeof(int)
909 #define ICMP_SYSRQ_SIZE 57
912 * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
913 * pattern and if it matches send the next byte as a trigger to sysrq.
915 static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
917 int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
920 if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
921 !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
922 p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
923 handle_sysrq(p[CO_POS0 + CO_SIZE]);
927 * Handle ICMP_ECHO ("ping") requests.
929 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
931 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
932 * included in the reply.
933 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
934 * echo requests, MUST have default=NOT.
935 * See also WRT handling of options once they are done and working.
938 static bool icmp_echo(struct sk_buff *skb)
942 net = dev_net(skb_dst(skb)->dev);
943 if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
944 struct icmp_bxm icmp_param;
946 icmp_param.data.icmph = *icmp_hdr(skb);
947 icmp_param.data.icmph.type = ICMP_ECHOREPLY;
948 icmp_param.skb = skb;
949 icmp_param.offset = 0;
950 icmp_param.data_len = skb->len;
951 icmp_param.head_len = sizeof(struct icmphdr);
952 icmp_reply(&icmp_param, skb);
954 if (skb->len == ICMP_SYSRQ_SIZE &&
955 net->ipv4.sysctl_icmp_echo_sysrq) {
956 icmp_check_sysrq(net, skb);
959 /* should there be an ICMP stat for ignored echos? */
964 * Handle ICMP Timestamp requests.
965 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
966 * SHOULD be in the kernel for minimum random latency.
967 * MUST be accurate to a few minutes.
968 * MUST be updated at least at 15Hz.
970 static bool icmp_timestamp(struct sk_buff *skb)
973 struct icmp_bxm icmp_param;
981 * Fill in the current time as ms since midnight UT:
984 icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
985 tv.tv_nsec / NSEC_PER_MSEC);
986 icmp_param.data.times[2] = icmp_param.data.times[1];
987 if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
989 icmp_param.data.icmph = *icmp_hdr(skb);
990 icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
991 icmp_param.data.icmph.code = 0;
992 icmp_param.skb = skb;
993 icmp_param.offset = 0;
994 icmp_param.data_len = 0;
995 icmp_param.head_len = sizeof(struct icmphdr) + 12;
996 icmp_reply(&icmp_param, skb);
1000 ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
1004 static bool icmp_discard(struct sk_buff *skb)
1006 /* pretend it was a success */
1011 * Deal with incoming ICMP packets.
1013 int icmp_rcv(struct sk_buff *skb)
1015 struct icmphdr *icmph;
1016 struct rtable *rt = skb_rtable(skb);
1017 struct net *net = dev_net(rt->dst.dev);
1020 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1021 struct sec_path *sp = skb_sec_path(skb);
1024 if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1028 if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
1031 nh = skb_network_offset(skb);
1032 skb_set_network_header(skb, sizeof(*icmph));
1034 if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
1037 skb_set_network_header(skb, nh);
1040 ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
1042 if (skb_checksum_simple_validate(skb))
1045 if (!pskb_pull(skb, sizeof(*icmph)))
1048 icmph = icmp_hdr(skb);
1050 ICMPMSGIN_INC_STATS_BH(net, icmph->type);
1052 * 18 is the highest 'known' ICMP type. Anything else is a mystery
1054 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
1057 if (icmph->type > NR_ICMP_TYPES)
1062 * Parse the ICMP message
1065 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1067 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1068 * silently ignored (we let user decide with a sysctl).
1069 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1070 * discarded if to broadcast/multicast.
1072 if ((icmph->type == ICMP_ECHO ||
1073 icmph->type == ICMP_TIMESTAMP) &&
1074 net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
1077 if (icmph->type != ICMP_ECHO &&
1078 icmph->type != ICMP_TIMESTAMP &&
1079 icmph->type != ICMP_ADDRESS &&
1080 icmph->type != ICMP_ADDRESSREPLY) {
1085 success = icmp_pointers[icmph->type].handler(skb);
1096 ICMP_INC_STATS_BH(net, ICMP_MIB_CSUMERRORS);
1098 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
1102 void icmp_err(struct sk_buff *skb, u32 info)
1104 struct iphdr *iph = (struct iphdr *)skb->data;
1105 int offset = iph->ihl<<2;
1106 struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1107 int type = icmp_hdr(skb)->type;
1108 int code = icmp_hdr(skb)->code;
1109 struct net *net = dev_net(skb->dev);
1112 * Use ping_err to handle all icmp errors except those
1113 * triggered by ICMP_ECHOREPLY which sent from kernel.
1115 if (icmph->type != ICMP_ECHOREPLY) {
1116 ping_err(skb, offset, info);
1120 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1121 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
1122 else if (type == ICMP_REDIRECT)
1123 ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
1127 * This table is the definition of how we handle ICMP.
1129 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1130 [ICMP_ECHOREPLY] = {
1131 .handler = ping_rcv,
1134 .handler = icmp_discard,
1138 .handler = icmp_discard,
1141 [ICMP_DEST_UNREACH] = {
1142 .handler = icmp_unreach,
1145 [ICMP_SOURCE_QUENCH] = {
1146 .handler = icmp_unreach,
1150 .handler = icmp_redirect,
1154 .handler = icmp_discard,
1158 .handler = icmp_discard,
1162 .handler = icmp_echo,
1165 .handler = icmp_discard,
1169 .handler = icmp_discard,
1172 [ICMP_TIME_EXCEEDED] = {
1173 .handler = icmp_unreach,
1176 [ICMP_PARAMETERPROB] = {
1177 .handler = icmp_unreach,
1180 [ICMP_TIMESTAMP] = {
1181 .handler = icmp_timestamp,
1183 [ICMP_TIMESTAMPREPLY] = {
1184 .handler = icmp_discard,
1186 [ICMP_INFO_REQUEST] = {
1187 .handler = icmp_discard,
1189 [ICMP_INFO_REPLY] = {
1190 .handler = icmp_discard,
1193 .handler = icmp_discard,
1195 [ICMP_ADDRESSREPLY] = {
1196 .handler = icmp_discard,
1200 static void __net_exit icmp_sk_exit(struct net *net)
1204 for_each_possible_cpu(i)
1205 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
1206 free_percpu(net->ipv4.icmp_sk);
1207 net->ipv4.icmp_sk = NULL;
1210 static int __net_init icmp_sk_init(struct net *net)
1214 net->ipv4.icmp_sk = alloc_percpu(struct sock *);
1215 if (!net->ipv4.icmp_sk)
1218 for_each_possible_cpu(i) {
1221 err = inet_ctl_sock_create(&sk, PF_INET,
1222 SOCK_RAW, IPPROTO_ICMP, net);
1226 *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
1228 /* Enough space for 2 64K ICMP packets, including
1229 * sk_buff/skb_shared_info struct overhead.
1231 sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
1234 * Speedup sock_wfree()
1236 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1237 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1240 /* Control parameters for ECHO replies. */
1241 net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1242 net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1244 /* Control parameter - ignore bogus broadcast responses? */
1245 net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1248 * Configurable global rate limit.
1250 * ratelimit defines tokens/packet consumed for dst->rate_token
1251 * bucket ratemask defines which icmp types are ratelimited by
1252 * setting it's bit position.
1255 * dest unreachable (3), source quench (4),
1256 * time exceeded (11), parameter problem (12)
1259 net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1260 net->ipv4.sysctl_icmp_ratemask = 0x1818;
1261 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1266 for_each_possible_cpu(i)
1267 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
1268 free_percpu(net->ipv4.icmp_sk);
1272 static struct pernet_operations __net_initdata icmp_sk_ops = {
1273 .init = icmp_sk_init,
1274 .exit = icmp_sk_exit,
1277 int __init icmp_init(void)
1279 return register_pernet_subsys(&icmp_sk_ops);