6

I'm trying to understand something related to network ports, as it's not quite clear to me.

The question is, why can't you use the same port number for both incoming and outgoing traffic?

This question arose due to some advice on NAT port forwarding.

From what I can tell, IP packet headers contain fields for source address and destination address, which I'd imagine would be all you'd need to know to determine whether the packet was incoming or outgoing.

I realise this is a simple question. If there are learning resources you could point me to that you think would be useful in building up a deeper understanding of networking from the fundamentals up, I'd appreciate the links, even if there's a lot to learn.

Thanks.

3
  • 1
    I think this is an interesting question. +1
    – yoyo_fun
    Jul 13, 2016 at 10:58
  • 1
    When you say "network ports", do you actually mean "TCP and UDP ports"? Jul 13, 2016 at 15:40
  • Todd Wilcox: Yes, TCP and UDP ports. Are there others that you can use with NAT port forwarding?
    – ZenoArrow
    Jul 14, 2016 at 15:19

4 Answers 4

6

You can, and some protocols do (isakmp), but it's not very flexible. There are two drawbacks:

  1. You can have only one data stream. A typical web browser makes several tcp connections to a web server to increase performance. That's why you often see the text appear before the graphics on a web page. you can't do that with only one stream.
  2. It's possible for a host to be both a client and a server at the same time. If the port numbers are the same, you can't do that either.

Ultimately, using well-known ports for destination and ephemeral ports for source gives the most flexibility.

3
  • Thank you for your advice Ron. So is it necessary to split out the data stream from the control stream? For example, AFAIK in FTP the default ports are port 20 (for incoming and outgoing data) and port 21 (for control). I realise there are performance benefits from splitting out the two functions, but is it the protocol design that determines the number of ports required or is there something else that blocks data and control being shared on a single port?
    – ZenoArrow
    Jul 13, 2016 at 11:16
  • 1
    This is a protocol design and if we are talking about FTP, this is done on purpose when the FTP protocol was designed, way back. So yes, this is all specified within the protocol design. Other protocols will use a different amount of ports per design requierments. Have a look at slacksite.com/other/ftp.html which will give you an idea behind passive and active and show you how the ports are used for this particular protocol. Might give you a good overview on how this works.
    – SleepyMan
    Jul 13, 2016 at 13:49
  • Thanks SleepyMan. It helps to know that the port requirements are protocol-specific. Thanks for the link too.
    – ZenoArrow
    Jul 14, 2016 at 15:21
3

A physical port can and is used for receiving and transmitting unless the port has been specifically configured to half-duplex.

I think where you are getting confused is the actual port source and destination which has nothing to do with a physical port.

When it comes to NAT, the destination and source port number will be different. The destination port number which will sit on a server that provides the service such as port 80 for HTTP. The software will receive many connection requests on this port number and the software will need to distinguish the different processes for each connection that comes in.

The source port number will be a randomly chosen upper port that is not used for any particular service and this will reside on the client device as they are the source of the connection. This also helps the software to distinguish the various connections that are being made for different clients. Imagine a HTTP server with hundreds of simultanious connections and not to mention the different connections the browser will make on port 80 itself as destination. When the data comes back, it relies on the source port number to tell the software where the data is destined for in the upper layers of the stack model.

NAT uses the source port number to manage the different connections that are being NAT'd. If they all used the same source port, then NAT, using it's table, would not be able to distinguish between the different connections, and know where it needs to forward the data to on the local network.So a source port is mapped to a local IP address that is on the inside of the NAT for as long as the TCP connection is still alive.

To get a better idea of what is happening and to udnerstand why the source port can't be the same, check out this resource:

http://www.firewall.cx/networking-topics/protocols/tcp/133-tcp-source-destination-ports.html

http://www.firewall.cx/networking-topics/network-address-translation-nat/233-nat-overload-part-1.html

Hope this helps you on your quest.

SleepyMan

0
1

If your question is about service ports associated with UDP or TCP then there is no objection to have same port for both incoming and outgoing traffic . Both incoming and outgoing traffic are independent to its direction .

When application is hosted on webserver . Application is hosted on specific service port example tcp-443 or any other port . Initially service has to be in listening mode for specific port at server end in this case when traffic is incoming this specific port should opened at network level on its path towards destination .same scenario will apply for outbound traffic as well.

0

In networking, we often speak of physical and logical. Physical ports are actually pairs of links - 1 send and 1 receive, TX & RX. Logical ports are defined in software, for example, an IP Stack.

A T1 physical port, for example, is 1.544Mbps - in both directions. It's actually two links -send and recieve. Same goes for Ethernet.

enter image description here

1
  • Thanks Ron. I was referring to ports at the logical level. It seems that some protocols can use the same logical port for both sending and receiving, so the requirements to have multiple logical ports for other protocols is due to the design of the protocol, rather than something that is working around an intrinsic limitation in the IP stack.
    – ZenoArrow
    Jul 14, 2016 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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