ephemeral (source) Ports and TCP-Stack - TCP-IP

This is a discussion on ephemeral (source) Ports and TCP-Stack - TCP-IP ; Hi @all, Whenever a TCP connection is established, the OS chooses a "random" port as the source-port. Let's say we want to connect to a webserver at port 80 and the OS uses port 50000 as the source-port. Now let's ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: ephemeral (source) Ports and TCP-Stack

  1. ephemeral (source) Ports and TCP-Stack

    Hi @all,

    Whenever a TCP connection is established, the OS chooses a "random" port
    as the source-port.
    Let's say we want to connect to a webserver at port 80 and the OS uses
    port 50000 as the source-port.

    Now let's assume we have 2 Interfaces on that PC. The connection to the
    webserver uses Interface eth0. Now we want to establish a new connection
    (let's say to port 22) using the other Interface (simply be connecting
    to an IP-address belonging to that network). The OS then assignes a
    random port as well.

    Now my question:
    Is the source Port unique for every connection made from one PC or just
    for every Interface?
    In our example, would it be possible that the second source-port equals
    the first one or is a socket only identified by the port and protocol?

    More background if you want to know the real problem :-):
    I set up a tun-interface which is used by legacy Applications. I get a
    packet with the randomly chosen source Port X on tun0 and replace the
    Destination IP and Port as well as the SourceIP by "real" values.
    The only thing that doesn't change is the SourcePort. After that the
    packet is written to eth0.
    - Can I assume that the source port is still unique or could it be taken
    by another connection while I was rewriting the packets?
    - Can I be sure that any incoming packet with the destination Port set
    to X belongs to the connection set up by the application?

    Many thanks in advance!!!

  2. Re: ephemeral (source) Ports and TCP-Stack

    Andreas Mueller wrote:
    > Is the source Port unique for every connection made from one PC or just
    > for every Interface?


    Consider a web server (or any TCP server). Is the listening port number for
    the listening socket different for each interface? No - it is always the
    same port number (e.g. 80 or 8080 or whatever). You should therefore assume
    that any ephemeral port number you receive from the TCP/IP stack is unique
    independent of the number of internet interfaces attached to the computer.

    > In our example, would it be possible that the second source-port equals
    > the first one or is a socket only identified by the port and protocol?


    If the OS allows multiple interfaces to be assigned the same IP address,
    you should assume the ephemeral port number comes from a single pool.

  3. Re: ephemeral (source) Ports and TCP-Stack

    On Jun 22, 12:23 pm, Andreas Mueller wrote:
    > Hi @all,
    >
    > Whenever a TCP connection is established, the OS chooses a "random" port
    > as the source-port.
    > Let's say we want to connect to a webserver at port 80 and the OS uses
    > port 50000 as the source-port.
    >
    > Now let's assume we have 2 Interfaces on that PC. The connection to the
    > webserver uses Interface eth0. Now we want to establish a new connection
    > (let's say to port 22) using the other Interface (simply be connecting
    > to an IP-address belonging to that network). The OS then assignes a
    > random port as well.
    >
    > Now my question:
    > Is the source Port unique for every connection made from one PC or just
    > for every Interface?
    > In our example, would it be possible that the second source-port equals
    > the first one or is a socket only identified by the port and protocol?
    >
    > More background if you want to know the real problem :-):
    > I set up a tun-interface which is used by legacy Applications. I get a
    > packet with the randomly chosen source Port X on tun0 and replace the
    > Destination IP and Port as well as the SourceIP by "real" values.
    > The only thing that doesn't change is the SourcePort. After that the
    > packet is written to eth0.
    > - Can I assume that the source port is still unique or could it be taken
    > by another connection while I was rewriting the packets?
    > - Can I be sure that any incoming packet with the destination Port set
    > to X belongs to the connection set up by the application?



    None of the above. A TCP connection is identified by the quad (source
    IP, source port, dest IP, dest port), and any component value may be
    reused (or not) as the OS sees fit. IOW, TCP connections (1.1.1.1,
    10, 2.2.2.2, 20) and (1.1.1.1, 10, 2.2.2.2, 21), with the same source
    IP and port (and dest IP) might both be active at the same time.

    Some OS's have been implemented such that stronger conditions apply.
    (Semi-)Unique ports for connections originated from a host is fairly
    common.

    Oh, and "Whenever a TCP connection is established, the OS chooses a
    'random' port as the source-port." is incorrect - it does so only if
    you do not specify the local endpoint by doing a bind() before the
    connect() (assuming convention BSD socket semantics).


  4. Re: ephemeral (source) Ports and TCP-Stack

    On Jun 22, 10:23 am, Andreas Mueller wrote:

    > More background if you want to know the real problem :-):
    > I set up a tun-interface which is used by legacy Applications. I get a
    > packet with the randomly chosen source Port X on tun0 and replace the
    > Destination IP and Port as well as the SourceIP by "real" values.
    > The only thing that doesn't change is the SourcePort. After that the
    > packet is written to eth0.
    > - Can I assume that the source port is still unique or could it be taken
    > by another connection while I was rewriting the packets?


    Absolutely not!

    > - Can I be sure that any incoming packet with the destination Port set
    > to X belongs to the connection set up by the application?


    No!

    DS



+ Reply to Thread