net/ipv4/raw.c
Source file repositories/reference/linux-study-clean/net/ipv4/raw.c
File Facts
- System
- Linux kernel
- Corpus path
net/ipv4/raw.c- Extension
.c- Size
- 26126 bytes
- Lines
- 1121
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/types.hlinux/atomic.hasm/byteorder.hasm/current.hlinux/uaccess.hasm/ioctls.hlinux/stddef.hlinux/slab.hlinux/errno.hlinux/kernel.hlinux/export.hlinux/spinlock.hlinux/sockios.hlinux/socket.hlinux/in.hlinux/mroute.hlinux/netdevice.hlinux/in_route.hlinux/route.hlinux/skbuff.hlinux/igmp.hnet/net_namespace.hnet/dst.hnet/sock.hlinux/ip.hlinux/net.hnet/ip.hnet/icmp.hnet/udp.hnet/raw.hnet/snmp.hnet/tcp_states.h
Detected Declarations
struct raw_frag_vecfunction raw_hash_skfunction raw_unhash_skfunction raw_v4_matchfunction icmp_filterfunction raw_v4_inputfunction raw_local_deliverfunction raw_errfunction raw_icmp_errorfunction raw_rcv_skbfunction raw_rcvfunction raw_send_hdrincfunction raw_probe_proto_optfunction raw_getfragfunction raw_sendmsgfunction raw_closefunction raw_destroyfunction raw_bindfunction raw_recvmsgfunction raw_sk_initfunction raw_seticmpfilterfunction raw_geticmpfilterfunction do_raw_setsockoptfunction raw_setsockoptfunction do_raw_getsockoptfunction raw_getsockoptfunction raw_ioctlfunction compat_raw_ioctlfunction raw_abortfunction sk_for_eachfunction raw_seq_stopfunction raw_sock_seq_showfunction raw_seq_showfunction raw_init_netfunction raw_exit_netfunction raw_proc_initfunction raw_proc_exitfunction raw_sysctl_init_netfunction raw_sysctl_initfunction raw_initexport raw_v4_hashinfoexport raw_v4_match
Annotated Snippet
struct raw_frag_vec {
struct msghdr *msg;
union {
struct icmphdr icmph;
char c[1];
} hdr;
int hlen;
};
struct raw_hashinfo raw_v4_hashinfo;
EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
int raw_hash_sk(struct sock *sk)
{
struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
struct hlist_head *hlist;
hlist = &h->ht[raw_hashfunc(sock_net(sk), inet_sk(sk)->inet_num)];
spin_lock(&h->lock);
sk_add_node_rcu(sk, hlist);
sock_set_flag(sk, SOCK_RCU_FREE);
spin_unlock(&h->lock);
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
return 0;
}
void raw_unhash_sk(struct sock *sk)
{
struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
spin_lock(&h->lock);
if (sk_del_node_init_rcu(sk))
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
spin_unlock(&h->lock);
}
bool raw_v4_match(struct net *net, const struct sock *sk, unsigned short num,
__be32 raddr, __be32 laddr, int dif, int sdif)
{
const struct inet_sock *inet = inet_sk(sk);
if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
!(inet->inet_daddr && inet->inet_daddr != raddr) &&
!(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
return true;
return false;
}
EXPORT_SYMBOL_GPL(raw_v4_match);
/*
* 0 - deliver
* 1 - block
*/
static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
{
struct icmphdr _hdr;
const struct icmphdr *hdr;
hdr = skb_header_pointer(skb, skb_transport_offset(skb),
sizeof(_hdr), &_hdr);
if (!hdr)
return 1;
if (hdr->type < 32) {
__u32 data = raw_sk(sk)->filter.data;
return ((1U << hdr->type) & data) != 0;
}
/* Do not block unknown ICMP types */
return 0;
}
/* IP input processing comes here for RAW socket delivery.
* Caller owns SKB, so we must make clones.
*
* RFC 1122: SHOULD pass TOS value up to the transport layer.
* -> It does. And not only TOS, but all IP header.
*/
static int raw_v4_input(struct net *net, struct sk_buff *skb,
const struct iphdr *iph, int hash)
{
int sdif = inet_sdif(skb);
struct hlist_head *hlist;
int dif = inet_iif(skb);
int delivered = 0;
struct sock *sk;
Annotation
- Immediate include surface: `linux/types.h`, `linux/atomic.h`, `asm/byteorder.h`, `asm/current.h`, `linux/uaccess.h`, `asm/ioctls.h`, `linux/stddef.h`, `linux/slab.h`.
- Detected declarations: `struct raw_frag_vec`, `function raw_hash_sk`, `function raw_unhash_sk`, `function raw_v4_match`, `function icmp_filter`, `function raw_v4_input`, `function raw_local_deliver`, `function raw_err`, `function raw_icmp_error`, `function raw_rcv_skb`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.