24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
29 #include <linux/errno.h>
33 #include <linux/netdevice.h>
41 #include <linux/export.h>
52 static u16 ping_port_rover;
54 static inline int ping_hashfn(
struct net *
net,
unsigned int num,
unsigned int mask)
56 int res = (num + net_hash_mix(net)) & mask;
58 pr_debug(
"hash(%d) = %d\n", num, res);
63 struct net *
net,
unsigned int num)
68 static int ping_v4_get_port(
struct sock *
sk,
unsigned short ident)
81 for (i = 0; i < (1
L << 16); i++, result++) {
84 hlist = ping_hashslot(&
ping_table, sock_net(sk),
94 ping_port_rover = ident =
result;
102 hlist = ping_hashslot(&
ping_table, sock_net(sk), ident);
108 (!sk2->sk_reuse || !sk->sk_reuse))
113 pr_debug(
"found port/ident = %d\n", ident);
115 if (sk_unhashed(sk)) {
118 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
119 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
129 static void ping_v4_hash(
struct sock *sk)
131 pr_debug(
"ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
135 static void ping_v4_unhash(
struct sock *sk)
141 hlist_nulls_del(&sk->sk_nulls_node);
145 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
158 pr_debug(
"try to find: num = %d, daddr = %pI4, dif = %d\n",
159 (
int)ident, &daddr, dif);
165 pr_debug(
"found: %p: num = %d, daddr = %pI4, dif = %d\n", sk,
166 (
int)isk->
inet_num, &isk->inet_rcv_saddr,
167 sk->sk_bound_dev_if);
172 if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr)
174 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
188 static void inet_get_ping_group_range_net(
struct net *net,
kgid_t *
low,
203 static int ping_init_sock(
struct sock *sk)
205 struct net *net = sock_net(sk);
211 inet_get_ping_group_range_net(net, &low, &high);
212 if (gid_lte(low, group) && gid_lte(group, high))
215 for (i = 0; i < group_info->
nblocks; i++) {
217 for (j = 0; j < cp_count; j++) {
219 if (gid_lte(low, gid) && gid_lte(gid, high))
229 static void ping_close(
struct sock *sk,
long timeout)
231 pr_debug(
"ping_close(sk=%p,sk->num=%u)\n",
232 inet_sk(sk), inet_sk(sk)->inet_num);
233 pr_debug(
"isk->refcnt = %d\n", sk->sk_refcnt.counter);
254 pr_debug(
"ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n",
261 if ((sysctl_ip_nonlocal_bind == 0 &&
277 if (ping_v4_get_port(sk, snum) != 0) {
282 pr_debug(
"after bind(): num = %d, daddr = %pI4, dif = %d\n",
284 &isk->inet_rcv_saddr,
285 (
int)sk->sk_bound_dev_if);
288 if (isk->inet_rcv_saddr)
298 pr_debug(
"ping_v4_bind -> %d\n", err);
306 static inline int ping_supported(
int type,
int code)
318 static int ping_queue_rcv_skb(
struct sock *sk,
struct sk_buff *
skb);
325 int type = icmph->type;
326 int code = icmph->code;
327 struct net *net = dev_net(skb->
dev);
334 if (!ping_supported(icmph->type, icmph->code))
337 pr_debug(
"ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type,
338 code,
ntohs(icmph->un.echo.id),
ntohs(icmph->un.echo.sequence));
340 sk = ping_v4_lookup(net, iph->
daddr, iph->
saddr,
341 ntohs(icmph->un.echo.id), skb->
dev->ifindex);
350 inet_sock = inet_sk(sk);
416 static int ping_getfrag(
void *
from,
char *to,
422 if (fraglen <
sizeof(
struct icmphdr))
425 pfh->
iov, 0, fraglen -
sizeof(
struct icmphdr),
431 if (offset <
sizeof(
struct icmphdr))
434 (to, pfh->
iov, offset -
sizeof(
struct icmphdr),
440 static int ping_push_pending_frames(
struct sock *sk,
struct pingfakehdr *pfh,
456 struct net *net = sock_net(sk);
491 if (!ping_supported(user_icmph.type, user_icmph.code))
509 daddr = inet->inet_daddr;
515 ipc.oif = sk->sk_bound_dev_if;
534 memcpy(&opt_copy, inet_opt,
535 sizeof(*inet_opt) + inet_opt->
opt.optlen);
536 ipc.opt = &opt_copy.opt;
542 ipc.addr = faddr =
daddr;
544 if (ipc.opt && ipc.opt->opt.srr) {
547 faddr = ipc.opt->opt.faddr;
552 (ipc.opt && ipc.opt->opt.is_strictroute)) {
556 if (ipv4_is_multicast(daddr)) {
564 flowi4_init_output(&fl4, ipc.oif, sk->
sk_mark, tos,
566 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
568 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
588 ipc.addr = fl4.
daddr;
592 pfh.
icmph.type = user_icmph.type;
593 pfh.
icmph.code = user_icmph.code;
594 pfh.
icmph.checksum = 0;
596 pfh.
icmph.un.echo.sequence = user_icmph.un.echo.sequence;
605 err = ping_push_pending_frames(sk, &pfh, &fl4);
619 dst_confirm(&rt->
dst);
621 goto back_from_confirm;
626 static int ping_recvmsg(
struct kiocb *iocb,
struct sock *sk,
struct msghdr *msg,
627 size_t len,
int noblock,
int flags,
int *addr_len)
641 *addr_len =
sizeof(*sin);
661 sock_recv_timestamp(msg, sk, skb);
667 sin->
sin_addr.s_addr = ip_hdr(skb)->saddr;
668 memset(sin->sin_zero, 0,
sizeof(sin->sin_zero));
677 pr_debug(
"ping_recvmsg -> %d\n", err);
681 static int ping_queue_rcv_skb(
struct sock *sk,
struct sk_buff *skb)
683 pr_debug(
"ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
684 inet_sk(sk), inet_sk(sk)->inet_num, skb);
687 pr_debug(
"ping_queue_rcv_skb -> failed\n");
701 struct net *net = dev_net(skb->
dev);
702 struct iphdr *iph = ip_hdr(skb);
703 struct icmphdr *icmph = icmp_hdr(skb);
709 pr_debug(
"ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
715 sk = ping_v4_lookup(net, saddr, daddr,
ntohs(icmph->
un.
echo.id),
719 ping_queue_rcv_skb(sk, skb_get(skb));
731 .init = ping_init_sock,
737 .sendmsg = ping_sendmsg,
738 .recvmsg = ping_recvmsg,
740 .backlog_rcv = ping_queue_rcv_skb,
741 .hash = ping_v4_hash,
742 .unhash = ping_v4_unhash,
743 .get_port = ping_v4_get_port,
748 #ifdef CONFIG_PROC_FS
754 struct net *net = seq_file_net(seq);
763 if (hlist_nulls_empty(hslot))
767 if (net_eq(sock_net(sk), net))
779 struct net *net = seq_file_net(seq);
782 sk = sk_nulls_next(sk);
783 }
while (sk && (!net_eq(sock_net(sk), net)));
786 return ping_get_first(seq, state->
bucket + 1);
792 struct sock *sk = ping_get_first(seq, 0);
795 while (pos && (sk = ping_get_next(seq, sk)) !=
NULL)
800 static void *ping_seq_start(
struct seq_file *seq, loff_t *pos)
810 static void *ping_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
815 sk = ping_get_idx(seq, 0);
817 sk = ping_get_next(seq, v);
823 static void ping_seq_stop(
struct seq_file *seq,
void *v)
829 int bucket,
int *len)
838 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
839 bucket, src, srcp, dest, destp, sp->sk_state,
840 sk_wmem_alloc_get(sp),
841 sk_rmem_alloc_get(sp),
849 static int ping_seq_show(
struct seq_file *seq,
void *v)
853 " sl local_address rem_address st tx_queue "
854 "rx_queue tr tm->when retrnsmt uid timeout "
855 "inode ref pointer drops");
860 ping_format_sock(v, seq, state->
bucket, &len);
867 .
show = ping_seq_show,
868 .start = ping_seq_start,
869 .next = ping_seq_next,
870 .stop = ping_seq_stop,
880 .
open = ping_seq_open,
886 static int ping_proc_register(
struct net *net)
897 static void ping_proc_unregister(
struct net *net)
903 static int __net_init ping_proc_init_net(
struct net *net)
905 return ping_proc_register(net);
908 static void __net_exit ping_proc_exit_net(
struct net *net)
910 ping_proc_unregister(net);
914 .
init = ping_proc_init_net,
915 .exit = ping_proc_exit_net,
918 int __init ping_proc_init(
void)
923 void ping_proc_exit(
void)