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>
83 #include <net/route.h>
84 #include <net/protocol.h>
90 #include <linux/skbuff.h>
92 #include <linux/errno.h>
93 #include <linux/timer.h>
94 #include <linux/init.h>
95 #include <asm/uaccess.h>
96 #include <net/checksum.h>
98 #include <net/inet_common.h>
99 #include <net/ip_fib.h>
100 #include <net/l3mdev.h>
103 * Build xmit assembly blocks
112 struct icmphdr icmph;
116 struct ip_options_data replyopts;
119 /* An array of errno for error messages from dest unreach. */
120 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
122 const struct icmp_err icmp_err_convert[] = {
124 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
128 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
132 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
136 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
140 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
144 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
148 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
152 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
156 .errno = ENONET, /* ICMP_HOST_ISOLATED */
160 .errno = ENETUNREACH, /* ICMP_NET_ANO */
164 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
168 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
172 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
176 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
180 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
184 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
188 EXPORT_SYMBOL(icmp_err_convert);
191 * ICMP control array. This specifies what to do with each ICMP.
194 struct icmp_control {
195 bool (*handler)(struct sk_buff *skb);
196 short error; /* This ICMP is classed as an error message */
199 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
202 * The ICMP socket(s). This is the most convenient way to flow control
203 * our ICMP output as well as maintain a clean interface throughout
204 * all layers. All Socketless IP sends will soon be gone.
206 * On SMP we have one ICMP socket per-cpu.
208 static struct sock *icmp_sk(struct net *net)
210 return *this_cpu_ptr(net->ipv4.icmp_sk);
213 static inline struct sock *icmp_xmit_lock(struct net *net)
221 if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
222 /* This can happen if the output path signals a
223 * dst_link_failure() for an outgoing ICMP packet.
231 static inline void icmp_xmit_unlock(struct sock *sk)
233 spin_unlock_bh(&sk->sk_lock.slock);
236 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
237 int sysctl_icmp_msgs_burst __read_mostly = 50;
244 .lock = __SPIN_LOCK_UNLOCKED(icmp_global.lock),
248 * icmp_global_allow - Are we allowed to send one more ICMP message ?
250 * Uses a token bucket to limit our ICMP messages to sysctl_icmp_msgs_per_sec.
251 * Returns false if we reached the limit and can not send another packet.
252 * Note: called with BH disabled
254 bool icmp_global_allow(void)
256 u32 credit, delta, incr = 0, now = (u32)jiffies;
259 /* Check if token bucket is empty and cannot be refilled
260 * without taking the spinlock.
262 if (!icmp_global.credit) {
263 delta = min_t(u32, now - icmp_global.stamp, HZ);
268 spin_lock(&icmp_global.lock);
269 delta = min_t(u32, now - icmp_global.stamp, HZ);
270 if (delta >= HZ / 50) {
271 incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
273 icmp_global.stamp = now;
275 credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
280 icmp_global.credit = credit;
281 spin_unlock(&icmp_global.lock);
284 EXPORT_SYMBOL(icmp_global_allow);
287 * Send an ICMP frame.
290 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
291 struct flowi4 *fl4, int type, int code)
293 struct dst_entry *dst = &rt->dst;
296 if (type > NR_ICMP_TYPES)
299 /* Don't limit PMTU discovery. */
300 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
303 /* No rate limit on loopback */
304 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
307 /* Limit if icmp type is enabled in ratemask. */
308 if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
312 if (icmp_global_allow()) {
313 int vif = l3mdev_master_ifindex(dst->dev);
314 struct inet_peer *peer;
316 peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
317 rc = inet_peer_xrlim_allow(peer,
318 net->ipv4.sysctl_icmp_ratelimit);
327 * Maintain the counters used in the SNMP statistics for outgoing ICMP
329 void icmp_out_count(struct net *net, unsigned char type)
331 ICMPMSGOUT_INC_STATS(net, type);
332 ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
336 * Checksum each fragment, and on the first include the headers and final
339 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
342 struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
345 csum = skb_copy_and_csum_bits(icmp_param->skb,
346 icmp_param->offset + offset,
349 skb->csum = csum_block_add(skb->csum, csum, odd);
350 if (icmp_pointers[icmp_param->data.icmph.type].error)
351 nf_ct_attach(skb, icmp_param->skb);
355 static void icmp_push_reply(struct icmp_bxm *icmp_param,
357 struct ipcm_cookie *ipc, struct rtable **rt)
362 sk = icmp_sk(dev_net((*rt)->dst.dev));
363 if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
364 icmp_param->data_len+icmp_param->head_len,
365 icmp_param->head_len,
366 ipc, rt, MSG_DONTWAIT) < 0) {
367 ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
368 ip_flush_pending_frames(sk);
369 } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
370 struct icmphdr *icmph = icmp_hdr(skb);
372 struct sk_buff *skb1;
374 skb_queue_walk(&sk->sk_write_queue, skb1) {
375 csum = csum_add(csum, skb1->csum);
377 csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
379 icmp_param->head_len, csum);
380 icmph->checksum = csum_fold(csum);
381 skb->ip_summed = CHECKSUM_NONE;
382 ip_push_pending_frames(sk, fl4);
387 * Driving logic for building and sending ICMP messages.
390 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
392 struct ipcm_cookie ipc;
393 struct rtable *rt = skb_rtable(skb);
394 struct net *net = dev_net(rt->dst.dev);
397 struct inet_sock *inet;
399 u32 mark = IP4_REPLY_MARK(net, skb->mark);
401 if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
404 sk = icmp_xmit_lock(net);
409 icmp_param->data.icmph.checksum = 0;
411 inet->tos = ip_hdr(skb)->tos;
413 daddr = ipc.addr = ip_hdr(skb)->saddr;
414 saddr = fib_compute_spec_dst(skb);
420 if (icmp_param->replyopts.opt.opt.optlen) {
421 ipc.opt = &icmp_param->replyopts.opt;
422 if (ipc.opt->opt.srr)
423 daddr = icmp_param->replyopts.opt.opt.faddr;
425 memset(&fl4, 0, sizeof(fl4));
428 fl4.flowi4_mark = mark;
429 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
430 fl4.flowi4_proto = IPPROTO_ICMP;
431 fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
432 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
433 rt = ip_route_output_key(net, &fl4);
436 if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
437 icmp_param->data.icmph.code))
438 icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
441 icmp_xmit_unlock(sk);
444 #ifdef CONFIG_IP_ROUTE_MULTIPATH
446 /* Source and destination is swapped. See ip_multipath_icmp_hash */
447 static int icmp_multipath_hash_skb(const struct sk_buff *skb)
449 const struct iphdr *iph = ip_hdr(skb);
451 return fib_multipath_hash(iph->daddr, iph->saddr);
456 #define icmp_multipath_hash_skb(skb) (-1)
460 static struct rtable *icmp_route_lookup(struct net *net,
462 struct sk_buff *skb_in,
463 const struct iphdr *iph,
464 __be32 saddr, u8 tos, u32 mark,
466 struct icmp_bxm *param)
468 struct rtable *rt, *rt2;
469 struct flowi4 fl4_dec;
472 memset(fl4, 0, sizeof(*fl4));
473 fl4->daddr = (param->replyopts.opt.opt.srr ?
474 param->replyopts.opt.opt.faddr : iph->saddr);
476 fl4->flowi4_mark = mark;
477 fl4->flowi4_tos = RT_TOS(tos);
478 fl4->flowi4_proto = IPPROTO_ICMP;
479 fl4->fl4_icmp_type = type;
480 fl4->fl4_icmp_code = code;
481 fl4->flowi4_oif = l3mdev_master_ifindex(skb_in->dev);
483 security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
484 rt = __ip_route_output_key_hash(net, fl4,
485 icmp_multipath_hash_skb(skb_in));
489 /* No need to clone since we're just using its address. */
492 rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
493 flowi4_to_flowi(fl4), NULL, 0);
497 } else if (PTR_ERR(rt) == -EPERM) {
502 err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
504 goto relookup_failed;
506 if (inet_addr_type_dev_table(net, skb_in->dev,
507 fl4_dec.saddr) == RTN_LOCAL) {
508 rt2 = __ip_route_output_key(net, &fl4_dec);
512 struct flowi4 fl4_2 = {};
513 unsigned long orefdst;
515 fl4_2.daddr = fl4_dec.saddr;
516 rt2 = ip_route_output_key(net, &fl4_2);
519 goto relookup_failed;
522 orefdst = skb_in->_skb_refdst; /* save old refdst */
523 skb_dst_set(skb_in, NULL);
524 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
525 RT_TOS(tos), rt2->dst.dev);
527 dst_release(&rt2->dst);
528 rt2 = skb_rtable(skb_in);
529 skb_in->_skb_refdst = orefdst; /* restore old refdst */
533 goto relookup_failed;
535 rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
536 flowi4_to_flowi(&fl4_dec), NULL,
539 dst_release(&rt->dst);
540 memcpy(fl4, &fl4_dec, sizeof(*fl4));
542 } else if (PTR_ERR(rt2) == -EPERM) {
544 dst_release(&rt->dst);
548 goto relookup_failed;
559 * Send an ICMP message in response to a situation
561 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
562 * MAY send more (we do).
563 * MUST NOT change this header information.
564 * MUST NOT reply to a multicast/broadcast IP address.
565 * MUST NOT reply to a multicast/broadcast MAC address.
566 * MUST reply to only the first fragment.
569 void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
573 struct icmp_bxm *icmp_param;
574 struct rtable *rt = skb_rtable(skb_in);
575 struct ipcm_cookie ipc;
585 net = dev_net(rt->dst.dev);
588 * Find the original header. It is expected to be valid, of course.
589 * Check this, icmp_send is called from the most obscure devices
592 iph = ip_hdr(skb_in);
594 if ((u8 *)iph < skb_in->head ||
595 (skb_network_header(skb_in) + sizeof(*iph)) >
596 skb_tail_pointer(skb_in))
600 * No replies to physical multicast/broadcast
602 if (skb_in->pkt_type != PACKET_HOST)
606 * Now check at the protocol level
608 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
612 * Only reply to fragment 0. We byte re-order the constant
613 * mask for efficiency.
615 if (iph->frag_off & htons(IP_OFFSET))
619 * If we send an ICMP error to an ICMP error a mess would result..
621 if (icmp_pointers[type].error) {
623 * We are an error, check if we are replying to an
626 if (iph->protocol == IPPROTO_ICMP) {
627 u8 _inner_type, *itp;
629 itp = skb_header_pointer(skb_in,
630 skb_network_header(skb_in) +
632 offsetof(struct icmphdr,
641 * Assume any unknown ICMP type is an error. This
642 * isn't specified by the RFC, but think about it..
644 if (*itp > NR_ICMP_TYPES ||
645 icmp_pointers[*itp].error)
650 icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
654 sk = icmp_xmit_lock(net);
659 * Construct source address and options.
663 if (!(rt->rt_flags & RTCF_LOCAL)) {
664 struct net_device *dev = NULL;
667 if (rt_is_input_route(rt) &&
668 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
669 dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
672 saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
678 tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
679 IPTOS_PREC_INTERNETCONTROL) :
681 mark = IP4_REPLY_MARK(net, skb_in->mark);
683 if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in))
688 * Prepare data for ICMP header.
691 icmp_param->data.icmph.type = type;
692 icmp_param->data.icmph.code = code;
693 icmp_param->data.icmph.un.gateway = info;
694 icmp_param->data.icmph.checksum = 0;
695 icmp_param->skb = skb_in;
696 icmp_param->offset = skb_network_offset(skb_in);
697 inet_sk(sk)->tos = tos;
699 ipc.addr = iph->saddr;
700 ipc.opt = &icmp_param->replyopts.opt;
705 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
706 type, code, icmp_param);
710 if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
713 /* RFC says return as much as we can without exceeding 576 bytes. */
715 room = dst_mtu(&rt->dst);
718 room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
719 room -= sizeof(struct icmphdr);
721 icmp_param->data_len = skb_in->len - icmp_param->offset;
722 if (icmp_param->data_len > room)
723 icmp_param->data_len = room;
724 icmp_param->head_len = sizeof(struct icmphdr);
726 icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
730 icmp_xmit_unlock(sk);
735 EXPORT_SYMBOL(icmp_send);
738 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
740 const struct iphdr *iph = (const struct iphdr *) skb->data;
741 const struct net_protocol *ipprot;
742 int protocol = iph->protocol;
744 /* Checkin full IP header plus 8 bytes of protocol to
745 * avoid additional coding at protocol handlers.
747 if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
748 ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
752 raw_icmp_error(skb, protocol, info);
754 ipprot = rcu_dereference(inet_protos[protocol]);
755 if (ipprot && ipprot->err_handler)
756 ipprot->err_handler(skb, info);
759 static bool icmp_tag_validation(int proto)
764 ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
770 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, ICMP_QUENCH, and
771 * ICMP_PARAMETERPROB.
774 static bool icmp_unreach(struct sk_buff *skb)
776 const struct iphdr *iph;
777 struct icmphdr *icmph;
781 net = dev_net(skb_dst(skb)->dev);
784 * Incomplete header ?
785 * Only checks for the IP header, there should be an
786 * additional check for longer headers in upper levels.
789 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
792 icmph = icmp_hdr(skb);
793 iph = (const struct iphdr *)skb->data;
795 if (iph->ihl < 5) /* Mangled header, drop. */
798 if (icmph->type == ICMP_DEST_UNREACH) {
799 switch (icmph->code & 15) {
800 case ICMP_NET_UNREACH:
801 case ICMP_HOST_UNREACH:
802 case ICMP_PROT_UNREACH:
803 case ICMP_PORT_UNREACH:
805 case ICMP_FRAG_NEEDED:
806 /* for documentation of the ip_no_pmtu_disc
808 * Documentation/networking/ip-sysctl.txt
810 switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
812 net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
818 if (!icmp_tag_validation(iph->protocol))
822 info = ntohs(icmph->un.frag.mtu);
826 net_dbg_ratelimited("%pI4: Source Route Failed\n",
832 if (icmph->code > NR_ICMP_UNREACH)
834 } else if (icmph->type == ICMP_PARAMETERPROB)
835 info = ntohl(icmph->un.gateway) >> 24;
838 * Throw it at our lower layers
840 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
842 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
844 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
849 * Check the other end isn't violating RFC 1122. Some routers send
850 * bogus responses to broadcast frames. If you see this message
851 * first check your netmask matches at both ends, if it does then
852 * get the other vendor to fix their kit.
855 if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
856 inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
857 net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
859 icmph->type, icmph->code,
860 &iph->daddr, skb->dev->name);
864 icmp_socket_deliver(skb, info);
869 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
875 * Handle ICMP_REDIRECT.
878 static bool icmp_redirect(struct sk_buff *skb)
880 if (skb->len < sizeof(struct iphdr)) {
881 ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
885 if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
886 /* there aught to be a stat */
890 icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
895 * 32bit and 64bit have different timestamp length, so we check for
896 * the cookie at offset 20 and verify it is repeated at offset 50
900 #define CO_SIZE sizeof(int)
901 #define ICMP_SYSRQ_SIZE 57
904 * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
905 * pattern and if it matches send the next byte as a trigger to sysrq.
907 static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
909 int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
912 if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
913 !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
914 p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
915 handle_sysrq(p[CO_POS0 + CO_SIZE]);
919 * Handle ICMP_ECHO ("ping") requests.
921 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
923 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
924 * included in the reply.
925 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
926 * echo requests, MUST have default=NOT.
927 * See also WRT handling of options once they are done and working.
930 static bool icmp_echo(struct sk_buff *skb)
934 net = dev_net(skb_dst(skb)->dev);
935 if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
936 struct icmp_bxm icmp_param;
938 icmp_param.data.icmph = *icmp_hdr(skb);
939 icmp_param.data.icmph.type = ICMP_ECHOREPLY;
940 icmp_param.skb = skb;
941 icmp_param.offset = 0;
942 icmp_param.data_len = skb->len;
943 icmp_param.head_len = sizeof(struct icmphdr);
944 icmp_reply(&icmp_param, skb);
946 if (skb->len == ICMP_SYSRQ_SIZE &&
947 net->ipv4.sysctl_icmp_echo_sysrq) {
948 icmp_check_sysrq(net, skb);
951 /* should there be an ICMP stat for ignored echos? */
956 * Handle ICMP Timestamp requests.
957 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
958 * SHOULD be in the kernel for minimum random latency.
959 * MUST be accurate to a few minutes.
960 * MUST be updated at least at 15Hz.
962 static bool icmp_timestamp(struct sk_buff *skb)
965 struct icmp_bxm icmp_param;
973 * Fill in the current time as ms since midnight UT:
976 icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
977 tv.tv_nsec / NSEC_PER_MSEC);
978 icmp_param.data.times[2] = icmp_param.data.times[1];
979 if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
981 icmp_param.data.icmph = *icmp_hdr(skb);
982 icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
983 icmp_param.data.icmph.code = 0;
984 icmp_param.skb = skb;
985 icmp_param.offset = 0;
986 icmp_param.data_len = 0;
987 icmp_param.head_len = sizeof(struct icmphdr) + 12;
988 icmp_reply(&icmp_param, skb);
992 ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
996 static bool icmp_discard(struct sk_buff *skb)
998 /* pretend it was a success */
1003 * Deal with incoming ICMP packets.
1005 int icmp_rcv(struct sk_buff *skb)
1007 struct icmphdr *icmph;
1008 struct rtable *rt = skb_rtable(skb);
1009 struct net *net = dev_net(rt->dst.dev);
1012 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1013 struct sec_path *sp = skb_sec_path(skb);
1016 if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1020 if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
1023 nh = skb_network_offset(skb);
1024 skb_set_network_header(skb, sizeof(*icmph));
1026 if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
1029 skb_set_network_header(skb, nh);
1032 ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
1034 if (skb_checksum_simple_validate(skb))
1037 if (!pskb_pull(skb, sizeof(*icmph)))
1040 icmph = icmp_hdr(skb);
1042 ICMPMSGIN_INC_STATS_BH(net, icmph->type);
1044 * 18 is the highest 'known' ICMP type. Anything else is a mystery
1046 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
1049 if (icmph->type > NR_ICMP_TYPES)
1054 * Parse the ICMP message
1057 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1059 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1060 * silently ignored (we let user decide with a sysctl).
1061 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1062 * discarded if to broadcast/multicast.
1064 if ((icmph->type == ICMP_ECHO ||
1065 icmph->type == ICMP_TIMESTAMP) &&
1066 net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
1069 if (icmph->type != ICMP_ECHO &&
1070 icmph->type != ICMP_TIMESTAMP &&
1071 icmph->type != ICMP_ADDRESS &&
1072 icmph->type != ICMP_ADDRESSREPLY) {
1077 success = icmp_pointers[icmph->type].handler(skb);
1088 ICMP_INC_STATS_BH(net, ICMP_MIB_CSUMERRORS);
1090 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
1094 void icmp_err(struct sk_buff *skb, u32 info)
1096 struct iphdr *iph = (struct iphdr *)skb->data;
1097 int offset = iph->ihl<<2;
1098 struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1099 int type = icmp_hdr(skb)->type;
1100 int code = icmp_hdr(skb)->code;
1101 struct net *net = dev_net(skb->dev);
1104 * Use ping_err to handle all icmp errors except those
1105 * triggered by ICMP_ECHOREPLY which sent from kernel.
1107 if (icmph->type != ICMP_ECHOREPLY) {
1108 ping_err(skb, offset, info);
1112 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1113 ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
1114 else if (type == ICMP_REDIRECT)
1115 ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
1119 * This table is the definition of how we handle ICMP.
1121 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1122 [ICMP_ECHOREPLY] = {
1123 .handler = ping_rcv,
1126 .handler = icmp_discard,
1130 .handler = icmp_discard,
1133 [ICMP_DEST_UNREACH] = {
1134 .handler = icmp_unreach,
1137 [ICMP_SOURCE_QUENCH] = {
1138 .handler = icmp_unreach,
1142 .handler = icmp_redirect,
1146 .handler = icmp_discard,
1150 .handler = icmp_discard,
1154 .handler = icmp_echo,
1157 .handler = icmp_discard,
1161 .handler = icmp_discard,
1164 [ICMP_TIME_EXCEEDED] = {
1165 .handler = icmp_unreach,
1168 [ICMP_PARAMETERPROB] = {
1169 .handler = icmp_unreach,
1172 [ICMP_TIMESTAMP] = {
1173 .handler = icmp_timestamp,
1175 [ICMP_TIMESTAMPREPLY] = {
1176 .handler = icmp_discard,
1178 [ICMP_INFO_REQUEST] = {
1179 .handler = icmp_discard,
1181 [ICMP_INFO_REPLY] = {
1182 .handler = icmp_discard,
1185 .handler = icmp_discard,
1187 [ICMP_ADDRESSREPLY] = {
1188 .handler = icmp_discard,
1192 static void __net_exit icmp_sk_exit(struct net *net)
1196 for_each_possible_cpu(i)
1197 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
1198 free_percpu(net->ipv4.icmp_sk);
1199 net->ipv4.icmp_sk = NULL;
1202 static int __net_init icmp_sk_init(struct net *net)
1206 net->ipv4.icmp_sk = alloc_percpu(struct sock *);
1207 if (!net->ipv4.icmp_sk)
1210 for_each_possible_cpu(i) {
1213 err = inet_ctl_sock_create(&sk, PF_INET,
1214 SOCK_RAW, IPPROTO_ICMP, net);
1218 *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
1220 /* Enough space for 2 64K ICMP packets, including
1221 * sk_buff/skb_shared_info struct overhead.
1223 sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
1226 * Speedup sock_wfree()
1228 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1229 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1232 /* Control parameters for ECHO replies. */
1233 net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1234 net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1236 /* Control parameter - ignore bogus broadcast responses? */
1237 net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1240 * Configurable global rate limit.
1242 * ratelimit defines tokens/packet consumed for dst->rate_token
1243 * bucket ratemask defines which icmp types are ratelimited by
1244 * setting it's bit position.
1247 * dest unreachable (3), source quench (4),
1248 * time exceeded (11), parameter problem (12)
1251 net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1252 net->ipv4.sysctl_icmp_ratemask = 0x1818;
1253 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1258 for_each_possible_cpu(i)
1259 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
1260 free_percpu(net->ipv4.icmp_sk);
1264 static struct pernet_operations __net_initdata icmp_sk_ops = {
1265 .init = icmp_sk_init,
1266 .exit = icmp_sk_exit,
1269 int __init icmp_init(void)
1271 return register_pernet_subsys(&icmp_sk_ops);