Conntrack kernel Reading Notes in netfilter (4)
3, init_conntrack:
Init_conntrack is used to create a new ip_conntrack and initialize it.
/* 1. Each connection contains two tuple, original, and reply. ip_ct_invert_tuple obtains the reply tuple Based on the imported original tuple, and finally calls the invert_tuple of the protocol for processing */
If (! Ip_ct_invert_tuple (& repl_tuple, tuple, protocol )){
Debugp ("can't invert tuple./N ");
Return NULL;
}
/* 2. allocate memory for conntrack from the cache and perform general initialization, such as initializing tuplehash, timeout, and ct_general. If the current number of connections has reached the upper limit, call early_drop to release the unanswered items in the hash chain where the tuple is located */
Conntrack = ip_conntrack_alloc (tuple, & repl_tuple );
/* 3. Call the new function of the protocol to initialize the conntrack and protocol-related private Processing Based on the packet data, which will be discussed during TCP analysis of specific protocols */
If (! Protocol-> New (conntrack, SKB )){
Ip_conntrack_free (conntrack );
Return NULL;
}
/* 4. Keep CT and helper are both related to the dynamic protocol. This article will focus on the analysis of the FTP protocol */
Exp = find_expectation (tuple );
If (exp ){
...
} Else {
Conntrack-> helper = _ ip_conntrack_helper_find (& repl_tuple );
Conntrack_stat_inc (new );
}
...
Write_unlock_bh (& ip_conntrack_lock );
If (exp ){
...
}
/* 5. Put original tuple on the unconfirmed chain ?? */List_add (& conntrack-> tuplehash [ip_ct_dir_original]. List, & unconfirmed );
4, tuplehash_to_ctrack:
The tuplehash_to_ctrack function is used to convert the hashtuple obtained in the global join table to the corresponding ip_conntrack structure. It uses the container_of macro for processing. Container_of obtains the structure pointer through the pointer of a member in the structure. Its implementation is very interesting:
/* 1. Forcibly expand the struct In the 0 address to obtain the offset of its member */
# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)
/* 2. Based on the member pointer PTR, subtract its relative offset to obtain the conntrack pointer of its host structure */
# Define container_of (PTR, type, member )({/
Const typeof (type *) 0)-> member) * _ mptr = (PTR );/
(Type *) (char *) _ mptr-offsetof (type, member ));})
5, TCP
The IP layer can carry multiple protocols and different protocols. When establishing the status table of the corresponding connection, the required information and the processing of the information are also different, below we will take the TCP protocol as an example to describe the specific protocol processing process.
First, in step 3 of ip_conntrack_in, when we determine the specific bearer protocol, we need to call the relevant error function to verify the message validity, this function is the tcp_error function in ip_conntrack_protocol_tcp.c. It mainly checks the integrity of the TCP packet header, the correctness of the checksum and the flag validity.
In step 4 of ip_conntrack_in, call the tcp_pkt_to_tuple function of ip_ct_get_tuple (resolve_normal_ct Step1) to convert the information in the packet into the tuple required for a TCP connection. Then, find the global connection table based on the tuple and obtain the corresponding status table (resolve_normal_ct step2). For new connections, call tcp_new to create and initialize the status table (init_conntrack ). The process of creating a status table for a TCP connection is divided into two types. A filter knows the connection establishment process and records the connection status starting from the first SYN Packet of the connection; the other is that the filter does not know when the connection is established. When the filter starts to record the connection status, the connection has already been established successfully, and the filter does not know the connection history. For example, when the filter restarts, all original connection status information is lost. In the latter case, the system determines whether the global variable ip_ct_tcp_loose is supported. In this case, we will provide a special introduction to this issue. Tcp_new is set for both cases.
In step 5 of ip_conntrack_in, tcp_packet is called to process the packets and update the connection status. The most important thing here is to use tcp_in_window to determine the validity of the message. Before introducing this function, let's first learn about TCP filter.