Ubuntu2.6.18 kernel packet travel Process Analysis

Source: Internet
Author: User
Note: This article only analyzes the process of receiving and sending data packets from the kernel to the local machine.) obtain data packets from the nic and first enter the processing function ip_rcv (). Its prototype is as follows: /** MainIPReceiveroutine */intip_rcv (structsk_buff * skb, structnet_device * dev, structpacket_type * pt, structnet _

Note: This article only analyzes the process in which the kernel receives and sends packets sent to the local machine ;)

Obtain the data packet from the NIC first enters the processing function ip_rcv (). Its prototype is as follows:
/*
* Main IP Receive routine
*/
Int ip_rcv (struct sk_buff * skb, struct net_device * dev, struct packet_type * pt, struct net_device * orig_dev );
The process of this function is as follows:
If the data packet is the then of another host
Directly discard;
If the packet has been referenced by another user, clone it. If the clone fails, then
Directly discard;
If header length <5 | version is not 4 then
Directly discard;
If the IP header checksum is incorrect then
Directly discard;
Clear the Flag Information in the cb Member of the socket packet;
Return NF_HOOK (PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL, ip_rcv_finish );
As you can see, the packet received from NIC will be transferred to the ip_rcv_finish () processing function at the fishing point NF_IP_PRE_ROUTING. Its prototype is as follows:
Static inline int ip_rcv_finish (struct sk_buff * skb );
The process of this function is as follows:
First, initialize virtual path cache for the data packet. It describes the travel process of the data packet in linux networking;
Call the ip_route_input () function for Route retrieval. // The route table is a large hash table. In this function, calculate the hash value and locate the corresponding bucket for Route retrieval.
// If the search is successful, then
// Return directly;
// If the target address is multicast address then
// Retrieve MAC-address and obtain the corresponding IP address
// Return ip_route_input_mc (skb, daddr, saddr, tos, dev, our );
// Return ip_route_input_slow (skb, daddr, saddr, tos, dev );
Return dst_input (skb );

2010-1-14
It can be seen that the data packet is transmitted to the dst_input () function again, and its prototype is as follows:
/* Input packet from network to transport .*/
Static inline int dst_input (struct sk_buff * skb)
{
Int err;

For (;;){
Err = skb-> dst-> input (skb );

If (likely (err = 0 ))
Return err;
/* Oh, Jamal... Seems, I will not forgive you this mess .:-)*/
If (unlikely (err! = NET_XMIT_BYPASS ))
Return err;
}
}
It can be seen that www.6688.cc the processing process of this processing function is very simple, it is an infinite for loop, and constantly send data packets to the processing function pointed to by the function pointer input. Tracking shows that the processing functions at this time are ip_forward () and ip_local_deliver (). As for how to determine the processing function to which the data packet is sent, we will not go into it for the time being. For project purposes, only the ip_local_deliver () processing functions are tracked here. The definition is as follows: (if the time is too long, follow ip_forward ())
/*
* Deliver IP Packets to the higher protocol layers.
*/
Int ip_local_deliver (struct sk_buff * skb)
{
/*
* Reassemble IP fragments.
*/

If (skb-> nh. iph-> frag_off & htons (IP_MF | IP_OFFSET )){
Skb = ip_defrag (skb, IP_DEFRAG_LOCAL_DELIVER );
If (! Skb)
Return 0;
}

Return NF_HOOK (PF_INET, NF_IP_LOCAL_IN, skb, skb-> dev, NULL,
Ip_local_deliver_finish );
}
We can see that in this function, we first determine whether the data packet is one of the shards (fragment). If so, we call the ip_defrag () function to restructure the shards. Otherwise, we use the hook function NF_HOOK () on the phishing node NF_IP_LOCAL_IN, send the data packet to the ip_local_deliver_finish () processing function. The function is defined as follows:
Static inline int ip_local_deliver_finish (struct sk_buff * skb );
The process is as follows:
First, use the following statement: skb-> h. raw = skb-> data; pointing to the data part of the data packet;
Obtain the first address of the struct sock linked list in the hash linked list;
If the original socket data stream is used, call the raw_v4_input () function to read data packets from the accept buffer through the following process: raw_rcv-> raw_rcv_skb-> sock_queue_rcv_skb-> 'skb-> rcvbuf, and perform corresponding processing. Otherwise, (struct sock *) raw_sk = NULL;
If (ipprot = rcu_dereference (inet_protos [hash])! = NULL) then // here is the address of the hash linked list of the corresponding protocol.
Int ret = ipprot-> handler (skb); // call handler to process data packets
Handler is the function pointer, which is defined in struct net_protocol. The processing function pointed to at this time is tcp_v4_rcv.
(Initialize the file af_inet.c as follows:
Static struct net_protocol tcp_protocol = {
. Handler = tcp_v4_rcv,
. Err_handler = tcp_v4_err,
. Gso_send_check = tcp_v4_gso_send_check,
. Gso_segment = tcp_tso_segment,
. No_policy = 1,
};)
The tcp_v4_rcv function is prototype:
/*
* From tcp_input.c
*/

Int tcp_v4_rcv (struct sk_buff * skb );
The function processing process is as follows:
If the local loopback packet then
Direct loss;
If tcp Header error then
Releases the page occupied by the data packet and discards the data packet;
If skb has not verified then
Call tcp_v4_checksum_init (skb) for verification;
Initialize the flag control information in skb-> cb. // The Flag Information only takes effect when the packet is in the kernel game duration. From now on, it will always exist in skb, of course, the intermediate process may be modified.
Call sk =__ inet_lookup (). First, use the function _ inet_lookup_established () to find the hash table of the established link. If yes, return the link entry information address directly; otherwise, call inet_lookup_listener () query and listen to the hash linked list. If successful, return the entry information address; otherwise, return NULL;
If (! Sk)
Goto no_tcp_socket;
Process:
Check the packet status (timeout ?) Reset the netfilter filter rules and match the netfilter rule chain;
(The Execution here shows that the corresponding set of interfaces (established? Listener ?). For the subsequent processing process, see the following code :)
Bh_lock_sock_nested (sk );
Ret = 0;
If (! Sock_owned_by_user (sk )){
# Ifdef CONFIG_NET_DMA
Struct tcp_sock * tp = tcp_sk (sk );
If (! Tp-> ucopy. dma_chan & tp-> ucopy. pinned_list)
Tp-> ucopy. dma_chan = get_softnet_dma ();
If (tp-> ucopy. dma_chan)
Ret = tcp_v4_do_rcv (sk, skb );
Else
# Endif
{
If (! Tcp_prequeue (sk, skb ))
Ret = tcp_v4_do_rcv (sk, skb );
}
} Else
Sk_add_backlog (sk, skb );
Bh_unlock_sock (sk );
Sock_put (sk );

Return ret;

No_tcp_socket:
If (! Xfrm4_policy_check (NULL, XFRM_POLICY_IN, skb ))
Goto discard_it;

If (skb-> len <(th-> doff <2) | tcp_checksum_complete (skb )){
Bad_packet:
TCP_INC_STATS_BH (TCP_MIB_INERRS );
} Else {
Tcp_v4_send_reset (skb );
}

Discard_it:
/* Discard frame .*/
Kfree_skb (skb );
Return 0;
Discard_and_relse:
Sock_put (sk );
Goto discard_it;

Do_time_wait:
If (! Xfrm4_policy_check (NULL, XFRM_POLICY_IN, skb )){
Inet_twsk_put (struct inet_timewait_sock *) sk );
Goto discard_it;
}

If (skb-> len <(th-> doff <2) | tcp_checksum_complete (skb )){
TCP_INC_STATS_BH (TCP_MIB_INERRS );
Inet_twsk_put (struct inet_timewait_sock *) sk );
Goto discard_it;
}
Switch (tcp_timewait_state_process (struct inet_timewait_sock *) sk,
Skb, th )){
Case TCP_TW_SYN :{
Struct sock * sk2 = inet_lookup_listener (& tcp_hashinfo,
Skb-> nh. iph-> daddr,
Ntohs (th-> dest ),
Inet_iif (skb ));
If (sk2 ){
Inet_twsk_deschedule (struct inet_timewait_sock *) sk,
& Tcp_death_row );
Inet_twsk_put (struct inet_timewait_sock *) sk );
Sk = sk2;
Goto process;
}
/* Fall through to ACK */
}
Case TCP_TW_ACK:
Tcp_v4_timewait_ack (sk, skb );
Break;
Case TCP_TW_RST:
Goto no_tcp_socket;
Case TCP_TW_SUCCESS :;
}
Goto discard_it;
In the above processing function, we found that the data packet was passed through a function named tcp_v4_do_rcv. The following describes the function. Its prototype is as follows:
Int tcp_v4_do_rcv (struct sock * sk, struct sk_buff * skb );
The function processing process is as follows:
When searching for the hash linked list, caller may return the entry information in the established linked list or the entry information in the listener linked list. Therefore, the caller first checks whether the link status of the datagram is TCP_ESTABLISHED, if yes, call tcp_rcv_established (sk, skb, skb-> h. th, skb-> len) function. This is required by the project. It is not the focus of tracking. It is temporarily paused so that the execution flow can continue to run down. In fact, I should have tracked it in detail here. Due to the time relationship, I will not write it here. If the time is plentiful, I will add it again...
If (skb-> len <(skb-> h. th-> doff <2) | TCP packet checksum error) then
Goto csum_err;
If the packet status is TCP_LISTEN then
Struct sock * nsk = tcp_v4_hnd_req (sk, skb); // tcp three-way handshake request processing function
If (! Nsk) // If the nsk pointer value is NULL, the three handshakes fail. The possible cause is that the accept link queue is full!
Goto discard;

If (nsk! = Sk) {// if the corresponding Linked List entry address returned by tcp_v4_hnd_req is from the entry address passed by caller
// If the information is different, it indicates that a new struct sock has been generated through inet_csk_clone ().
// Tcp_child_process () Processing Function
If (tcp_child_process (sk, nsk, skb) --> //
Goto reset;
Return 0;
}
Tcp_rcv_state_process (sk, skb, skb-> h. th, skb-> len); // data packet status processing function
Here, the three-way handshake processing function tcp_v4_hnd_req is tracked. Its prototype is as follows:
Static struct sock * tcp_v4_hnd_req (struct sock * sk, struct sk_buff * skb );

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.