Implementation and usage of network HOOK in Linux

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.