ICMP sockets
Subsystems (3)
RFC v2 1/1 ICMP sockets
(David, please tell me whether it is acceptable as a whole)
This patch adds IPPROTO_ICMP socket kind. It makes it possible to send ICMP_ECHO messages and receive corresponding ICMP_ECHOREPLY messages without any special privileges. In other words, the patch makes it possible to implement setuid-less /bin/ping.
A new ping socket is created with
socket(PF_INET, SOCK_DGRAM, PROT_ICMP)
Message identifiers (octets 4-5 of ICMP header) are interpreted as local ports. Addresses are stored in struct sockaddr_in. No port numbers are reserved for privileged processes, port 0 is reserved for API ("let the kernel pick a free number"). There is no notion of remote ports, remote port numbers provided by the user (e.g. in connect()) are ignored.
Data sent and received include ICMP headers. This is deliberate to:
1) Avoid the need to transport headers values like sequence numbers by other means
2) Make it easier to port existing programs using raw sockets.
ICMP headers given to send() are checked and sanitized. The type must be ICMP_ECHO and the code must be zero (future extensions might relax this, see below). The id is set to the number (local port) of the socket, the checksum is always recomputed.
ICMP reply packets received from the network are demultiplexed according to their id's and returned by recv() without any modifications. IP header information and ICMP errors of those packets may be obtained via ancillary data (IP_RECVTTL, IP_RETOPTS, and IP_RECVERR). ICMP source quenches and redirects are reported as fake errors via the error queue (IP_RECVERR); the next hop address for redirects is saved to ee_info (in network order).
Socket(2) is restricted to the group range specified in "/proc/sys/net/ipv4/ping_group_range" (or the owner of CAP_NET_RAW). It is "1 0" by default, meaning that nobody except root may create ping sockets. Setting it to "100 100" would gain permissions to the single group, "0 65535" would enable it for the world.
The existing code might be (in the unlikely case anyone needs it) extended rather easily to handle other similar pairs of ICMP messages (Timestamp/Reply, Information Request/Reply, Address Mask Request/Reply etc.).
Userspace ping util & patch for it:
http://openwall.info/wiki/segoon/ping
Similar functionality is implemented in Mac OS X:
http://www.manpagez.com/man/4/icmp/
All ping options (-b, -p, -Q, -R, -s, -t, -T, -M, -I), are tested with the patch.
Tested with 2K "ping -i0.2" running on Core i7 (8 cores), Load average = 80, and with 20 simultanious "ping -c1", each for an hour. Namespace functionality is tested with LXC.
Initially this functionality was written by Pavel Kankovsky (CC'ed him) for linux 2.4.32, but unfortunately it was never made public.
Changes since v1:
- minor cleanups.
- introduced sysctl'able group range to restrict socket(2).
--
include/net/netns/ipv4.h +2 -0
@@ -55,6 +55,8 @@ struct netns_ipv4 {int sysctl_rt_cache_rebuild_count;int current_rt_cache_rebuild_count; +unsigned int sysctl_ping_group_range[2 ]; +atomic_t rt_genid;#ifdef CONFIG_IP_MROUTE
include/net/ping.h new file +73 -0
73 lines not on this page · raw message
net/ipv4/Kconfig +21 -0
@@ -14,6 +14,27 @@ config IP_MULTICAST <file:Documentation/networking/multicast.txt>. For most people, it's safe to say N. +config IP_PING + bool "IP: ping socket" + depends on EXPERIMENTAL + help + This option introduces a new kind of sockets - "ping sockets". + + A ping socket makes it possible to send ICMP Echo messages and receive + corresponding ICMP Echo Reply messages without any special privileges. + In other words, it makes is possible to implement setuid-less /bin/ping. + + A new ping socket is created with socket(PF_INET, SOCK_DGRAM, PROT_ICMP). + +config IP_PING_DEBUG + bool "IP: ping socket debug output" + depends on IP_PING + default n + help + Enable the inclusion of debug code in the ICMP ping sockets. + Be aware that doing this will impact performance. + If unsure say N. + config IP_ADVANCED_ROUTER bool "IP: advanced router" ---help---
net/ipv4/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_IP_FIB_TRIE) += fib_trie.o obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_IP_MULTIPLE_TABLES) += fib_rules.o obj-$(CONFIG_IP_MROUTE) += ipmr.o +obj-$(CONFIG_IP_PING) += ping.o ping_proc.o obj-$(CONFIG_NET_IPIP) += ipip.o obj-$(CONFIG_NET_IPGRE_DEMUX) += gre.o obj-$(CONFIG_NET_IPGRE) += ip_gre.o
net/ipv4/af_inet.c +36 -0
@@ -105,6 +105,7 @@#include <net/tcp.h> #include <net/udp.h> #include <net/udplite.h> +#include <net/ping.h> #include <linux/skbuff.h> #include <net/sock.h> #include <net/raw.h>
@@ -992,6 +993,16 @@ static struct inet_protosw inetsw_array[] = .flags = INET_PROTOSW_PERMANENT, }, +#ifdef CONFIG_IP_PING + { + .type = SOCK_DGRAM, + .protocol = IPPROTO_ICMP, + .prot = &ping_prot, + .ops = &inet_dgram_ops, + .no_check = UDP_CSUM_DEFAULT, + .flags = INET_PROTOSW_REUSE, + }, +#endif { .type = SOCK_RAW,
@@ -1520,6 +1531,9 @@ static const struct net_protocol udp_protocol = {static const struct net_protocol icmp_protocol = { .handler = icmp_rcv, +#ifdef CONFIG_IP_PING + .err_handler = ping_err, +#endif .no_policy =1 , .netns_ok =1 , };
@@ -1635,6 +1649,12 @@ static int __init inet_init(void)if (rc) goto out_unregister_udp_proto; +#ifdef CONFIG_IP_PING + rc =proto_register (&ping_prot,1 ); +if (rc) + goto out_unregister_raw_proto; +#endif +/* * Tell SOCKET that we are alive... */
@@ -1690,6 +1710,10 @@ static int __init inet_init(void)/* Add UDP-Lite (RFC 3828) */ udplite4_register (); +#ifdef CONFIG_IP_PING +ping_init (); +#endif +/* * Set the ICMP layer up */
@@ -1720,6 +1744,10 @@ static int __init inet_init(void) rc =0 ; out:return rc; +#ifdef CONFIG_IP_PING +out_unregister_raw_proto: +proto_unregister (&raw_prot); +#endif out_unregister_udp_proto:proto_unregister (&udp_prot); out_unregister_tcp_proto:
@@ -1744,11 +1772,19 @@ static int __init ipv4_proc_init(void) goto out_tcp;if (udp4_proc_init ()) goto out_udp; +#ifdef CONFIG_IP_PING +if (ping_proc_init ()) + goto out_ping; +#endif if (ip_misc_proc_init ()) goto out_misc; out:return rc; out_misc: +#ifdef CONFIG_IP_PING +ping_proc_exit (); +out_ping: +#endifudp4_proc_exit (); out_udp:tcp4_proc_exit ();
net/ipv4/icmp.c +13 -1
@@ -83,6 +83,7 @@#include <net/tcp.h> #include <net/udp.h> #include <net/raw.h> +#include <net/ping.h> #include <linux/skbuff.h> #include <net/sock.h> #include <linux/errno.h>
@@ -808,6 +809,17 @@ static void icmp_redirect(struct sk_buff *skb) iph->saddr, skb->dev);break ; } + +#ifdef CONFIG_IP_PING +/* Ping wants to see redirects. +* Let's pretend they are errors of sorts... */ +if (iph->protocol ==IPPROTO_ICMP && + iph->ihl >=5 && +pskb_may_pull (skb, (iph->ihl<<2 )+8 )) { +ping_err (skb,icmp_hdr (skb)->un.gateway); + } +#endif + out:return ; out_err:
@@ -1068,7 +1080,7 @@ error:*/ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES +1 ] = { [ICMP_ECHOREPLY ] = { - .handler = icmp_discard, + .handler = icmp_echoreply, }, [1 ] = { .handler = icmp_discard,
net/ipv4/ping.c new file +787 -0
787 lines not on this page · raw message
net/ipv4/ping_proc.c new file +216 -0
216 lines not on this page · raw message
net/ipv4/sysctl_net_ipv4.c +54 -0
91 lines not on this page · raw message
--