Article Title: network HOOK implementation and usage in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Recently, I have been crazy about Linux functions. Here I will talk about the Net Hook in Linux. Using net Hook can implement many very underlying functions, such as packet filtering, firewall and proxy.
Now, Let's Go!
Linux 2.6.19.1 kernel code is used.
First, the function nf_register_hook in the./Source/net/netfilter/core. c file:
Static DEFINE_SPINLOCK (nf_hook_lock );
Int nf_register_hook (struct nf_hook_ops * reg)
{
Struct list_head * I;
Spin_lock_bh (& nf_hook_lock );
List_for_each (I, & nf_hooks [reg-> pf] [reg-> hooknum]) {
If (reg-> priority <(struct nf_hook_ops *) I)-> priority)
Break;
}
List_add_rcu (®-> List, I-> prev );
Spin_unlock_bh (& nf_hook_lock );
Synchronize_net ();
Return 0;
}
EXPORT_SYMBOL (nf_register_hook );
Void nf_unregister_hook (struct nf_hook_ops * reg)
{
Spin_lock_bh (& nf_hook_lock );
List_del_rcu (®-> List );
Spin_unlock_bh (& nf_hook_lock );
Synchronize_net ();
}
EXPORT_SYMBOL (nf_unregister_hook );
Int nf_register_hooks (struct nf_hook_ops * reg, unsigned int n)
{
Unsigned int I;
Int err = 0;
For (I = 0; I <n; I ++ ){
Err = nf_register_hook (®[I]);
If (err)
Goto err;
}
Return err;
Err:
If (I> 0)
Nf_unregister_hooks (reg, I );
Return err;
}
EXPORT_SYMBOL (nf_register_hooks );
Void nf_unregister_hooks (struct nf_hook_ops * reg, unsigned int n)
{
Unsigned int I;
For (I = 0; I <n; I ++)
Nf_unregister_hook (®[I]);
}
EXPORT_SYMBOL (nf_unregister_hooks );
[1] [2] Next page