Linux TCP/IP protocol stack

Source: Internet
Author: User

The following is the time to analyze tcp_v4_init. This function is in net/IPv4/tcp_ipv4.c:
_ Initfunc (voidtcp_v4_init (structnet_proto_family * OPS ))
{
Interr;

Tcp_inode. I _mode = s_ifsock;
Tcp_inode. I _sock = 1;
Tcp_inode. I _uid = 0;
Tcp_inode. I _gid = 0;

Tcp_socket-> inode = & tcp_inode;
Tcp_socket-> state = ss_unconnected;
Tcp_socket-> type = sock_raw;

If (ERR = OPS-> Create (tcp_socket, ipproto_tcp) <0)
Panic ("failedtocreatethetcpcontrolsocket./N ");
Tcp_socket-> SK-> allocation = gfp_atomic;
Tcp_socket-> SK-> num = 256;
Tcp_socket-> SK-> ip_ttl = maxttl;
}
Tcp_inode is of course an inode node, and tcp_socket is equal to tcp_inode.u.socket_ I,
They point to the same memory through a pointer.
Tcp_socket is used for communication. It can be called TCP controlsocket or communication.
Socket. When no socket exists for TCP communication, this socket acts as the socket. for example, if an RST is sent when SYN is received on a closed port, or the SYN is sent when the three-way handshake is received (no new socket has been generated by accept)

It is worth noting that the OPS-> Create function is called. We have previously seen that for af_inet, this callback function is the inet_create function of net/IPv4/af_inet.c, this function is used to create a socket. Because the function is relatively long, the analysis is skipped here. The first analysis is just a rough process.

Because of socket creation and communication, this code is protocol-related, so the code is extracted from the original TCP. C.
The following is the tcp_init function. In net/IPv4/tcp. C, it creates several hash tables and buckets. This Code creates the following global objects:
Tcp_openreq_cache
Tcp_bucket_cache
Tcp_timewait_cache
Tcp_ehash
Tcp_bhash
Here, ehash represents establishedhash, and bhash represents bindhash. Of course, they are all sock meeting the tcp_established <= Sk-> state <tcp_close state. but I don't know what bucket means here. anyoneknows? The roles of the cachets are not very clear, especially the second one. The function is mainly used for memory allocation and error handling.

The next step is the icmp_init function. in C, in fact, if ipproto_tcp in tcp_v4_init is replaced with ipproto_icmp, it is basically the same. the rest of the proc_net_register function has been mentioned before.

So far, we should have some knowledge about the starting work of the IP stack in Linux, including several key functions:
Dev_add_pack: registers a processing function at or above the link layer. It is generally used to use the new Network Layer Protocol. However, if it is repeated during registration, the system will set a copy bit. if it is eth_p_all, all data packets will be received. the added elements are stored in the center of the ptype_all and ptype_basehash linked lists.

Inet_add_protocol: registers a protocol established above the IP layer, such as TCP and UDP.

Proc_net_register (similar proc_register ):
Create a sub-directory under the/proc/NET directory so that the administrator can obtain statistics through the file system.

There are still many obfuscation points. One is the meaning of each member of the struct sk_buff, and the other is the meaning of the struct sock. But these two problems should be known later. next I plan to analyze the processing of each protocol one by one, including status conversion, data transmission, and receipt.
--
Wishyourskybesunny,
Wishyourheartbehappy,
Wishyourbodybehealthy,
Wishuneverbelazy.
:) :>;); >:-):->;-);->

 

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.