Netfilter framework implementation in Linux2.4

Source: Internet
Author: User
Article title: netfilter framework implementation in Linux2.4. 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.
Netfilter is a functional framework that implements data packet filtering, data packet processing, and NAT in the linux2.4 kernel. This article discusses the linux 2.4 kernel's netfilter function framework, as well as packet filtering, NAT, and packet processing (packet mangling) based on the netfilter framework. Read this article to learn about the principles and usage of ipchains in the 2.2 kernel as a preliminary knowledge. if you do not have this knowledge, please read IPCHAINS-HOWTO.
  
   Part 1: Basics and concepts of Netfilter
  1. what is Netfilter?
Netfilter is more powerful than the firewall subsystem of any previous Linux kernel version. Netfilter provides an abstract and universal framework. the implementation of a sub-function defined by this framework is the packet filtering subsystem. Therefore, do not expect to discuss topics such as "how to set up a firewall or disguised gateway in 2.4" in section 2.4. these are only part of the Netfilter function. The Netfilter framework consists of the following three parts:
  
1. define a set of hook functions for each network protocol (IPv4, IPv6, and so on). These hook functions are called at several key points when the datagram flows through the protocol stack. In these vertices, the protocol stack uses the datagram and hook function labels as parameters to call the netfilter framework.
  
2. any module in the kernel can register one or more hooks of each protocol for mounting. when a packet is transmitted to the netfilter framework, whether there are any modules in the inner nuclear energy detection that have registered the protocol and hook functions. If the module is registered, call the callback function used for registration of the module, so that these modules have the opportunity to check (and may modify it) this packet, discard the packet, and instruct netfilter to pass the packet into the queue of the user space.
  
3. the queued data packets are passed to the user space for asynchronous processing. A user process can check the data packet, modify the data packet, and even reinject the data packet into the kernel through the same hook function that leaves the kernel.
  
All packet filtering/NAT are based on this framework. The kernel network code is no longer full of messy code for modifying data packets. The current netfilter framework is implemented in IPv4, IPv6, and Decnet network stacks.
  
  2. why Netfilter?
In fact, this problem can also be changed to ipchains. What are the disadvantages that may lead to rejection? The following are several reasons:
  
Because the 2.2 kernel-based ipchains does not provide a framework for transmitting data packets to the user space, any code that needs to process data packets must run in the kernel space, but kernel programming is very complicated, it can only be implemented in C language and is prone to errors that pose a threat to kernel stability.
  
The transparent proxy implementation is very complex. you must check each data packet to determine whether a socket specially processes this address exists. 34 "# ifdef" condition compilations appear in 11 files in the network stack code.
  
Creating a datagram filter rule that does not depend on the interface address is impossible. We must use the local interface address to determine whether the datagram is sent locally, sent locally, or forwarded. The forwarding chain only outputs interface information. Therefore, the administrator must consider the source of the datagram.
  
Both camouflage and packet filtering are implemented in the same module, resulting in overly complicated firewall code.
  
IPchains code is not modular and not easy to expand (for example, filtering mac addresses)
  
  3. Author of Netfilter
The concept and main implementation of the Netfilter framework are completed by Rusty Russell. he is the partner of ipchains and the maintainer of the Linux Kernel IP firewall. Marc Boucher, James Morris, and Harald Welte all participate in the Netfilter project.
  
  4. Structure of Netfilter in IPv4
A data packet passes the Netfilter system as shown in the following process:
  
  
---> [1] ---> [ROUTE] ---> [3] ---> [4] --->
| ^
Local |
| [ROUTE]
V |
[2] [5]
| ^
|
V |
  
There are five hook functions in IPv4:
  
1 NF_IP_PRE_ROUTING
2 NF_IP_LOCAL_IN
3 NF_IP_FORWARD
4 NF_IP_POST_ROUTING
5 NF_IP_LOCAL_OUT
  
The Datagram enters the system from the left. after IP address verification, the datagram goes through the first hook function NF_IP_PRE_ROUTING [1] for processing. then, it enters the routing code, it determines whether the data packet needs to be forwarded or sent to the local machine. if the data packet is sent to the local machine, the data is processed by the hook function NF_IP_LOCAL_IN [2] and then transmitted to the upper layer protocol; if the data packet should be forwarded, it will be processed by NF_IP_FORWARD [3]. the forwarded data packet will be transmitted to the network after being processed by the last hook function NF_IP_POST_ROUTING [4.
  
After the locally generated data is processed by the hook function NF_IP_LOCAL_OUT [5], route selection is performed and then sent to the network after being processed by NF_IP_POST_ROUTING [4.
  
  5. Netfilter basics
From the above example about netfilter of IPv4, we can see how the hook function is activated.
  
The kernel module registers and hooks one or more of these hook functions, and is called when the datagram passes through these hook functions. Therefore, the module can modify these datagram functions, return the following value to netfilter:
  
NF_ACCEPT continues normal data transmission
NF_DROP discards the datagram and no longer transmits it.
The NF_STOLEN module takes over the datagram. do not continue to transmit the datagram.
NF_QUEUE queues the data packet (usually used to process the process that sends the data packet to the user space)
NF_REPEAT calls the hook function again.
  
  6. use iptables to select a datagram
A Netfilter-based Datagram selection system called iptables is applied in the Linux2.4 kernel. In fact, it is a successor tool of ipchains, but it has higher scalability.
  
The kernel module can register a new rule table and require that the datagram flow through the specified rule table. This type of datagram selection is used to implement datagram filtering (filter table), network address translation (Nat table), and Datagram processing (mangle table ).
  
The three datagram processing functions provided by the Linux kernel are based on the netfilter hook function and IP table. They are independent modules and mutually independent. They are all perfectly integrated into the framework provided by Netfileter.
  
Package filtering
The filter table does not modify the Datagram, but only filters the datagram. Iptables is smaller and faster than ipchains. It uses the hook functions NF_IP_LOCAL_IN, NF_IP_FORWARD, and NF_IP_LOCAL_OUT to access the netfilter framework. Therefore, only one part of a datagram can be filtered. This is a huge improvement compared to ipchains, because a forwarded datagram in ipchains traverses three chains.
  
NAT
The NAT table listens to three Netfilter hook functions: NF_IP_PRE_ROUTING, NF_IP_POST_ROUTING, and NF_IP_LOCAL_OUT. NF_IP_PRE_ROUTING converts the source address of the data packet to be forwarded, while NF_IP_POST_ROUTING converts the destination address of the data packet to be forwarded. The conversion of the destination address of the local datagram is implemented by NF_IP_LOCAL_OUT.
  
The NAT table is different from the filter table, because only the first newly connected datagram will traverse the table, and the subsequent datagram will perform the same conversion based on the result of the first datagram.
  
NAT tables are used in the source NAT, destination NAT, disguise (which is a special case of Source NAT), and transparent proxy (which is a special case of destination NAT ).
  
Packet mangling)
The mangle table is registered in the NF_IP_PRE_ROUTING and NF_IP_LOCAL_OUT hooks. Using the mangle table, you can modify the datagram or attach some out-of-band data to the datagram. Currently, the mangle table supports modifying the TOS bit and setting the nfmard field of skb.
  
  VII. connection tracking
Connection tracing is the basis of NAT, but it has been implemented as a separate module. This function is used to expand the packet filtering function. connection tracking is used to implement a "status-based" firewall.
  
  The second part uses iptables and netfilter to filter data packets.
  I. Overview
The following content requires an understanding of the basic concepts of TCP/IP, routing, firewall, and packet filtering.
  
As explained in the first part, the filter table and three hooks are mounted, so three links are provided for data filtering. All data packets from the network and sent to the local machine traverse the INPUT rule chain. All forwarded data packets only traverse the FORWARD rule chain. Finally, the local datagram traverses the OUTPUT chain.
  
  2. Insert rules into the rule chain
Linux2.4 provides a simple and powerful tool "iptables" to insert, delete, and modify rules in the rule chain. I will not introduce iptalbes in detail here, but just discuss some of its main features:
  
This command is used to process all ip address tables. Currently, three tables, filter, nat, and mangle, and the extended table modules, are included.
This command supports plug-ins to support new matching parameters and target actions. Therefore, any extension to netfilter is very simple. You only need to write a module to complete the actual target action processing and the iptalbes plug-in (dynamic connection library) to add everything you need.
It has two implementations: iptables (IPV4) and ip6tables. Both are based on the same library and basically the same code.
Basic iptables commands
An iptables command basically contains the following five parts:
  
Table on which you want to work
Link of the table to be used
Operations (insert, add, delete, modify)
Target action for a specific rule
Match the datagram condition
The basic syntax is:
  
Iptables-t table-Operation chain-j target match (es)
For example, you want to add a rule to allow all connections from anywhere to the local smtp Port:
  
Iptables-t filter-a input-j ACCEPT-p tcp -- dport smtp
  
Of course, there are other commands for operating rules, such as clearing the linked list, setting the chain default policy, and adding a custom chain ....
  
Basic operations:
  
-A adds A rule at the end of the chain
-I insert rules
-D. delete a rule.
-R replaces a rule.
-L list rules
  
Basic target action, applicable to all links
  
A

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.