简体   繁体   中英

How does the UDP checksum change for IP fragments?

I need to program a basic network stack in c, but have some questions regarding proper practices. Let's assume that I only need to support UDP at L4 and IP at L3.

When I want to sent a big message that needs to be fragmented. What is the proper order in which to handle this?

1) Initialize the udp header and calculating the checksum over the whole data field (plus udp and pseudo header) Then fragment the data field, build the IP header for each fragment, then send out the fragments.

2) Fragment the data field, then build a udp header with a different checksum tacked on the front of each fragment. Then build an IP header to tack in fron of that for each fragment. Then send our the fragments.

My confusions stems from whether the udp checksum should encompass the entire assembled datagram, or just the individual fragment. I greatly appreciate any help you can provide.

The network layers work independently of each other. When the IP layer fragments a packet, the UDP layer has no knowledge of it. By the time an application-layer UDP listener receives the packet, it will have been re-assembled and will have had no idea that the packet was ever fragmented.

With that knowledge, we can say that the UDP checksum must cover the fully assembled packet and does not have to change during fragmentation. In fact, nothing in the UDP header changes.

To error check the fragmented packets, you use the IPv4 checksum header field. The IPv6 header does not have a checksum field since the lower layers are expected to have error-free packet delivery.

When you do fragment an IPv4 packet, you must re-compute the IPv4 header for the fragment. The IPv4 wikipedia entry tells us what must be changed when you fragment a packet:

  • The total length field is the segment size.
  • The more fragments (MF) flag is set for all segments except the last one, which is set to 0.
  • The fragment offset field is set, based on the offset of the segment in the original data payload. This is measured in units of eight-byte blocks.
  • The [IPv4] header checksum field is recomputed.

So in conclusion, you do the following:

  1. Take the application-level data and wrap it in a UDP header.
  2. Give the packet with UDP header to the IP layer.
  3. Divide the data into fragments.
  4. For each fragment, put an IPv4 fragment header on it.
    • Give the fragment to layer 2 and put the layer 2 header on it.
    • Send the fragment.

数字1和数据包之前的总校验和应与重构回原始格式的校验和相匹配

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM