net/netfilter/nf_bpf_link.c
Source file repositories/reference/linux-study-clean/net/netfilter/nf_bpf_link.c
File Facts
- System
- Linux kernel
- Corpus path
net/netfilter/nf_bpf_link.c- Extension
.c- Size
- 8152 bytes
- Lines
- 333
- 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.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf.hlinux/filter.hlinux/kmod.hlinux/module.hlinux/netfilter.hnet/netfilter/nf_bpf_link.huapi/linux/netfilter_ipv4.h
Detected Declarations
struct bpf_nf_linkfunction nf_hook_run_bpffunction get_proto_defrag_hookfunction bpf_nf_enable_defragfunction bpf_nf_disable_defragfunction bpf_nf_link_releasefunction bpf_nf_link_deallocfunction bpf_nf_link_detachfunction bpf_nf_link_show_infofunction bpf_nf_link_fill_link_infofunction bpf_nf_link_updatefunction bpf_nf_check_pf_and_hooksfunction bpf_nf_link_attachfunction nf_ptr_to_btf_idfunction nf_is_valid_accessfunction bpf_nf_func_proto
Annotated Snippet
struct bpf_nf_link {
struct bpf_link link;
struct nf_hook_ops hook_ops;
netns_tracker ns_tracker;
struct net *net;
u32 dead;
const struct nf_defrag_hook *defrag_hook;
};
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
static const struct nf_defrag_hook *
get_proto_defrag_hook(struct bpf_nf_link *link,
const struct nf_defrag_hook __rcu **ptr_global_hook,
const char *mod)
{
const struct nf_defrag_hook *hook;
int err;
/* RCU protects us from races against module unloading */
rcu_read_lock();
hook = rcu_dereference(*ptr_global_hook);
if (!hook) {
rcu_read_unlock();
err = request_module("%s", mod);
if (err)
return ERR_PTR(err < 0 ? err : -EINVAL);
rcu_read_lock();
hook = rcu_dereference(*ptr_global_hook);
}
if (hook && try_module_get(hook->owner)) {
/* Once we have a refcnt on the module, we no longer need RCU */
hook = rcu_pointer_handoff(hook);
} else {
WARN_ONCE(!hook, "%s has bad registration", mod);
hook = ERR_PTR(-ENOENT);
}
rcu_read_unlock();
if (!IS_ERR(hook)) {
err = hook->enable(link->net);
if (err) {
module_put(hook->owner);
hook = ERR_PTR(err);
}
}
return hook;
}
#endif
static int bpf_nf_enable_defrag(struct bpf_nf_link *link)
{
const struct nf_defrag_hook __maybe_unused *hook;
switch (link->hook_ops.pf) {
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
case NFPROTO_IPV4:
hook = get_proto_defrag_hook(link, &nf_defrag_v4_hook, "nf_defrag_ipv4");
if (IS_ERR(hook))
return PTR_ERR(hook);
link->defrag_hook = hook;
return 0;
#endif
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
case NFPROTO_IPV6:
hook = get_proto_defrag_hook(link, &nf_defrag_v6_hook, "nf_defrag_ipv6");
if (IS_ERR(hook))
return PTR_ERR(hook);
link->defrag_hook = hook;
return 0;
#endif
default:
return -EAFNOSUPPORT;
}
}
static void bpf_nf_disable_defrag(struct bpf_nf_link *link)
{
const struct nf_defrag_hook *hook = link->defrag_hook;
if (!hook)
return;
hook->disable(link->net);
module_put(hook->owner);
}
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/filter.h`, `linux/kmod.h`, `linux/module.h`, `linux/netfilter.h`, `net/netfilter/nf_bpf_link.h`, `uapi/linux/netfilter_ipv4.h`.
- Detected declarations: `struct bpf_nf_link`, `function nf_hook_run_bpf`, `function get_proto_defrag_hook`, `function bpf_nf_enable_defrag`, `function bpf_nf_disable_defrag`, `function bpf_nf_link_release`, `function bpf_nf_link_dealloc`, `function bpf_nf_link_detach`, `function bpf_nf_link_show_info`, `function bpf_nf_link_fill_link_info`.
- 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.