net/ipv4/ping.c
Source file repositories/reference/linux-study-clean/net/ipv4/ping.c
File Facts
- System
- Linux kernel
- Corpus path
net/ipv4/ping.c- Extension
.c- Size
- 28278 bytes
- Lines
- 1172
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: implementation source
- Status
- source implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/uaccess.hlinux/types.hlinux/fcntl.hlinux/socket.hlinux/sockios.hlinux/in.hlinux/errno.hlinux/timer.hlinux/mm.hlinux/inet.hlinux/netdevice.hnet/snmp.hnet/ip.hnet/icmp.hnet/protocol.hlinux/skbuff.hlinux/proc_fs.hlinux/export.hlinux/bpf-cgroup.hnet/sock.hnet/ping.hnet/udp.hnet/route.hnet/inet_common.hnet/checksum.hlinux/in6.hlinux/icmpv6.hnet/addrconf.hnet/ipv6.hnet/transp_v6.h
Detected Declarations
struct ping_tablefunction ping_hashfnfunction ping_get_portfunction sk_for_eachfunction sk_for_eachfunction ping_unhashfunction sk_for_each_rcufunction inet_get_ping_group_range_netfunction ping_init_sockfunction ping_closefunction ping_pre_connectfunction ping_check_bind_addrfunction ping_set_saddrfunction ping_bindfunction ping_supportedfunction ping_errfunction ping_getfragfunction ping_v4_push_pending_framesfunction ping_common_sendmsgfunction ping_v4_sendmsgfunction ping_recvmsgfunction __ping_queue_rcv_skbfunction ping_queue_rcv_skbfunction ping_rcvfunction sk_for_eachfunction ping_seq_stopfunction ping_v4_format_sockfunction ping_v4_seq_showfunction ping_v4_proc_init_netfunction ping_v4_proc_exit_netfunction ping_proc_initfunction ping_proc_exitfunction ping_init
Annotated Snippet
struct ping_table {
struct hlist_head hash[PING_HTABLE_SIZE];
spinlock_t lock;
};
static struct ping_table ping_table;
struct pingv6_ops pingv6_ops;
static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
{
u32 res = (num + net_hash_mix(net)) & mask;
pr_debug("hash(%u) = %u\n", num, res);
return res;
}
static inline struct hlist_head *ping_hashslot(struct ping_table *table,
struct net *net, unsigned int num)
{
return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
}
int ping_get_port(struct sock *sk, unsigned short ident)
{
struct net *net = sock_net(sk);
struct inet_sock *isk, *isk2;
struct hlist_head *hlist;
struct sock *sk2 = NULL;
isk = inet_sk(sk);
spin_lock(&ping_table.lock);
if (ident == 0) {
u16 result = net->ipv4.ping_port_rover + 1;
u32 i;
for (i = 0; i < (1L << 16); i++, result++) {
if (!result)
continue; /* avoid zero */
hlist = ping_hashslot(&ping_table, net, result);
sk_for_each(sk2, hlist) {
if (!net_eq(sock_net(sk2), net))
continue;
isk2 = inet_sk(sk2);
if (isk2->inet_num == result)
goto next_port;
}
/* found */
net->ipv4.ping_port_rover = ident = result;
break;
next_port:
;
}
if (i >= (1L << 16))
goto fail;
} else {
hlist = ping_hashslot(&ping_table, net, ident);
sk_for_each(sk2, hlist) {
if (!net_eq(sock_net(sk2), net))
continue;
isk2 = inet_sk(sk2);
/* BUG? Why is this reuse and not reuseaddr? ping.c
* doesn't turn off SO_REUSEADDR, and it doesn't expect
* that other ping processes can steal its packets.
*/
if ((isk2->inet_num == ident) &&
(sk2 != sk) &&
(!sk2->sk_reuse || !sk->sk_reuse))
goto fail;
}
}
pr_debug("found port/ident = %d\n", ident);
isk->inet_num = ident;
if (sk_unhashed(sk)) {
pr_debug("was not hashed\n");
sk_add_node_rcu(sk, hlist);
sock_set_flag(sk, SOCK_RCU_FREE);
sock_prot_inuse_add(net, sk->sk_prot, 1);
}
spin_unlock(&ping_table.lock);
return 0;
fail:
spin_unlock(&ping_table.lock);
return -EADDRINUSE;
}
Annotation
- Immediate include surface: `linux/uaccess.h`, `linux/types.h`, `linux/fcntl.h`, `linux/socket.h`, `linux/sockios.h`, `linux/in.h`, `linux/errno.h`, `linux/timer.h`.
- Detected declarations: `struct ping_table`, `function ping_hashfn`, `function ping_get_port`, `function sk_for_each`, `function sk_for_each`, `function ping_unhash`, `function sk_for_each_rcu`, `function inet_get_ping_group_range_net`, `function ping_init_sock`, `function ping_close`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.