Receiving Data in the Transport Layer, UDP, and TCP
In this, we show what happens when a packet arrives in the transport layer. We cover the UDP protocol, which processes packets for SOCK_DGRAM type sockets, and the TCP protocol, which processes packets for SOCK_STREAM type sockets. As we saw in "Sending the Data from the Socket through UDP and TCP," UDP is far simpler than TCP because it doesnâ€™t handle any state information, so the majority of this chapter is devoted to TCP. However, in this chapter, we discuss the packet handling for both protocols. Since TCP is far more complicated than UDP, the discussion includes handling of the queues and processing of input TCP segments in the various states. In general, we continue the discussion of earlier chapters by showing how each transport layer protocol works by following the data as it flows up from the network layer, IP through the transport layer, and up into the receiving socket. The flow of input packets is straightforward for UDP and can be discussed separately from the flow of output packets. This is because UDP is limited to individual packet processing and doesnâ€™t have to maintain as much internal state information. However, with TCP, since the received packet flow depends on the internal state of the protocol, it isnâ€™t possible to completely separate the discussion of the receive side from the send side.
As is the case with the send side, all the user-level read functions are converted into a single function at the socket layer. In UDP, the function doing the work is udp_recvmsg, and for TCP, it is tcp_recvmsg. The bulk of this chapter covers these two functions and other associated processing. UDP is a simpler case because packet reception in UDP essentially consists of little more than checksum calculation and copying of the packet data form kernel space to user space cover the UDP-specific receive packet handling. TCP is far more complex, and the receive side must ensure coordination with the sendside processing, TCP state handling, and keeping track of acknowledgments. This cover the processing of received packets in TCP.
Receive-Side Packet Handling
The transport layer protocols—TCP, UDP, and other member protocols in the AF_INET protocol family—all receive packets form the network layer, IP. As we saw in, "The Network Layer, IP," when IP is done processing an input packet it dispatches the packet to a higher-layer protocol’s receive function by decoding the 1-byte protocol field in the IP header. It uses a hash table to determine whether TCP, UDP, or some other protocol should receive the packet.
In, “The Linux TCP/IP Stack,” we discussed the hash tables, the inet_protocol structure, and how incoming IP packets are de-multiplexed. we discussed the flow of incoming packets through the IP input routine. In this chapter, we will start following the packets as soon as they arrive in each of the two transport layer protocols’ handler functions.