UDP Socket-Level Receive - Linux

we saw how all the socket send calls are converged at the socket layer into one function at the transport layer. On the send side, all the socket-level send functions converge on one function. For UDP, this function is udp_sendmsg. As is the case with the send side, when the application calls any of the read functions on an open socket, the socket layer calls the function pointed to by the rcvmsg field in the prot structure. At compile time, the rcvmsg field is initialized to udp_rcvmsg. Udp_rcvmsg, in the file linux/net/ipv4/udp.c, is the receiving function that is executed for all SOCK_DGRAM type sockets.

Set the application’s address length argument. Check to see if there are any messages in the error queue of the socket, sk, and if so, process them by calling ip_recv_error and going out.

De-queue packets from the socket sk’s receive queue by calling the generic datagram receive queue function, skb_recv_datagram, and if it returns without a packet, get out.

Check to see if the user is asking for more data than the payload in the packet, skb.

The ip_summed field in the socket buffer determines if checksums are required. If ip_summed is not equal to CHECKSUM_ UNNECESSARY, checksum calculation must be done, and data is copied from kernel space to user space while calculating a checksum.

The copying and checksum calculation is done by skb_copy_and_csum_datagram_iovec. However, if a checksum is not required, skb_copy_datagram_iovec is the function called to do the copying. It is also possible that a partial checksum was calculated because the data spanned more than one buffer, and in this case, __udp_checksum_complete finishes the checksum calculation. A checksum is the amount that makes an integer sum always add up to zero. Checksums can be done partially and finished later.

Next, the incoming packet is timestamped. If the user supplied a valid buffer, sin, to receive the packet’s source address and port, the information is copied from the packet header.

Before leaving, udp_recv checks the control message flags field, cmsg_flags, to see if any IP socket options are set. For example, certain socket options such as IP_TOS require parts of the IP header to be copied into user space. If there are any flags set, ip_cmsg_recv retrieves the associated option values.

There are three error exits at the end of udp_rcv. The most interesting is csum_copy_err.

When the flags argument is set to MSG_PEEK, it means that the caller wants to "peek" at the incoming message without removing it from the receive queue. However, we know the packet has a bad checksum, so we must remove it from the receive queue before deleting it.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Linux Topics