0

I have few questions about how UDP segments at the transport layer works. I read that

The transport layer encapsulates the application layer data along with destination IP and destination port information and passes it along to the network layer. Note that no information about the source IP or source port is included.

If this is the case, then why does the UDP datagram header contain a space for a source port?

enter image description here

My other question is that since after each layer from the OSI model headers get stripped (frames -> packets -> segments -> finally just data at the application level), how does the application layer know what IP to respond to if the IP header has been stripped?

2
  • The quote is a typo/thinko... src/dst IP is in the L3 header, ports are in the L4 header. The application simply uses the same connection/stream/file descriptor/...; it isn't concerned with all the other layers, a write will flow back up the network stack getting the appropriate layers added on the way. (there are various function calls to get the various bits of upper layer information if the app needs it.)
    – Ricky
    Oct 13, 2021 at 21:33
  • 1
    Do not get hung up on the OSI model because OSes do not implement it. It is merely a model, and it helps to grasp the encapsulation and abstraction concepts, but it does not exist in the real world.
    – Ron Maupin
    Oct 13, 2021 at 22:27

2 Answers 2

1

This is a case of, "in theory there is no difference between theory and practice, ..."

Usually, applications interact with operating system's network stack using an abstraction, called sockets. Sockets, which is also some sort of object that is maintained inside the operating systems, contain necessary information.

If this is the case, then why does the UDP datagram header contain a space for a source port?

a UDP header must contain these values. Your OS (or implementation of network stack) can choose when to set these values.

I frankly speaking do not think that it works like described, and instead OS requires you to specify local address for a socket, before you can send packet through the socket.

But here is a plausible explanation, why it can work like this. Source addres is determined by routing function. You can have several network interfaces, each of which has different source address. Then your host can be configured to send packets to one set of destinations to one address and another set of destinations to another address. Then, you can't really set source address, before the decision of routing is done. In this case network layer will have to set the values in the headers.

update actually, sorry, that quote does not make sense.

The transport layer encapsulates the application layer data along with destination IP and destination port information and passes it along to the network layer.

ports only exist at transport layer, so transport layer definitelly does not pass destination port to the network layer. it passes destination (and potentially source) ip address, but not port.

Note that no information about the source IP or source port is included.

a source port is assigned before a packet can be sent. if the source port is not specified by an application, OS will just select one and put it into the header. This actually works the same with TCP. When you initiate a connection, you specify destination port, and source port is randomly selected for you.

end update

My other question is that since after each layer from the OSI model headers get stripped (frames -> packets -> segments -> finally just data at the application level), how does the application layer know what IP to respond to if the IP header has been stripped?

Operating system/network stack has to provide you with this information, and how it does this depends on the socket API. Here is a link to linux man page for recvfrom, check the second function.

In general, layers communicate with each other, and when a lower layer strips packet header, it can still pass all the necessary information from that header to the upper layer. How this is done, is specific to how the network stack in question is implemented. But it has to be done and it can't work otherwise.

2
  • I understand that the UDP header must contain these values, but UDP won't actually use the source port for anything right? If I am understanding correctly, it's probably in the header to pass it along the networking layer?
    – Pablo
    Oct 13, 2021 at 20:47
  • i think it is more there to pass it to applications. so, in your example when you receive a UPD packet and want to send a responce, you will need to put something in the destination port of the responce. You can learn this something from source port of the original packet. Although UDP does not use connections, it still uses ports to identify and separate flows and to decide which socket to associate packet with.
    – Effie
    Oct 13, 2021 at 21:07
0

The transport layer encapsulates the application layer data along with destination IP and destination port information and passes it along to the network layer.

That's not entirely correct. While the transport layer does use and pass on source and destination IP information, that information isn't encapsulated by the transport layer but by the underlying network layer.

(The transport layer needs to be aware of the source IP address because a host can have multiple interfaces or IP-interface bindings.)

why does the UDP datagram header contain a space for a source port?

Ports are a concept of the transport-layer protocol (L4). Not all those protocols use ports but the most common ones TCP and UDP do.

There are no ports on the network layer (IP), so it wouldn't make sense to put that field in the IP header.

how does the application layer know what IP to respond to if the IP header has been stripped?

The IP header isn't visible to the application but its information is. The OS stack provides an API for the application to use, usually BSD-style sockets. That API also provides information about a connection partner's IP and L4 port.

3
  • I understand that UDP uses ports (especially the destination port to map to the right socket). My question is specific to the 'source port' of the UDP header, why does it need that information if it doesn't use it?
    – Pablo
    Oct 13, 2021 at 20:37
  • The source port may be important for the application to create a somewhat session concept (e.g. for DNS).
    – Zac67
    Oct 13, 2021 at 21:11
  • It does use it. Multiple senders (even from the same source IP) must be differentiated from each other.
    – Ricky
    Oct 13, 2021 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.