Session 4
Session 4
Today, Agenda:
1-Transport Layer
2-Network Address Translation (NAT)
3-Port Address Translation (PAT)
4-Network Socket
Transport Layer
Role of the Transport Layer:
The transport layer is:
• responsible for logical communications between applications running on different hosts.
• The link between the application layer and the lower layers that are responsible for network transmission.
Transport Layer Responsibilities
The transport layer has the following responsibilities:
• Tracking individual conversations
• Segmenting data and reassembling segments
• Adds header information
• Identify, separate, and manage multiple conversations
• Uses segmentation and multiplexing to enable different communication conversations to be interleaved on the same
network
Transport Layer Protocols
• IP does not specify how the delivery or transportation of the packets takes place.
• Transport layer protocols specify how to transfer messages between hosts and are responsible for managing
reliability requirements of a conversation.
• The transport layer includes the TCP and UDP protocols.
Transmission Control Protocol
TCP provides reliability and flow control. TCP basic operations:
• Number and track data segments transmitted to a specific host from a specific application.
• Acknowledge received data.
• Retransmit any unacknowledged data after a certain amount of time.
• Sequence data that might arrive in the wrong order.
• Send data at an efficient rate that is acceptable to the receiver.
User Datagram Protocol (UDP)
UDP provides the basic functions for delivering datagrams between the appropriate applications, with very little overhead
and data checking.
• UDP is a connectionless protocol.
• UDP is known as a best-effort delivery protocol because there is no acknowledgment that the data is received at the
destination.
The Right Transport Layer Protocol for the Right Application
UDP is also used by request-and-reply applications where the data is minimal, and retransmission can be done quickly.
If it is important that all the data arrives and that it can be processed in its proper sequence, TCP is used as the transport
protocol.
TCP Overview
TCP Features
• Establishes a Session - TCP is a connection-oriented protocol that negotiates and establishes a permanent
connection (or session) between source and destination devices prior to forwarding any traffic.
• Ensures Reliable Delivery - For many reasons, it is possible for a segment to become corrupted or lost completely, as
it is transmitted over the network. TCP ensures that each segment that is sent by the source arrives at the
destination.
• Provides Same-Order Delivery - Because networks may provide multiple routes that can have different transmission
rates, data can arrive in the wrong order.
• Supports Flow Control - Network hosts have limited resources (i.e., memory and processing power). When TCP is
aware that these resources are overtaxed, it can request that the sending application reduce the rate of data flow.
TCP Header
TCP is a stateful protocol which means it keeps track of the state of the communication session.
TCP records which information it has sent, and which information has been acknowledged.
TCP Header Fields
Applications that use TCP
TCP handles all tasks associated with dividing the data stream into segments, providing reliability, controlling data flow, and
reordering segments.
UDP Overview
UDP Features
UDP features include the following:
• Data is reconstructed in the order that it is received.
• Any segments that are lost are not resent.
• There is no session establishment.
• The sending is not informed about resource availability.
UDP Header
The UDP header is far simpler than the TCP header because it only has four fields and requires 8 bytes (i.e. 64 bits).
UDP Header Fields
The table identifies and describes the four fields in a UDP header.
Session Termination
• Step 1: When the client has no more data to send in the stream, it sends a segment with the FIN flag set.
• Step 2: The server sends an ACK to acknowledge the receipt of the FIN to terminate the session from client to server.
• Step 3: The server sends a FIN to the client to terminate the server-to-client session. Step 4: The client responds with an
ACK to acknowledge the FIN from the server.
TCP Flags
The six control bit flags are as follows:
• URG - Urgent pointer field significant.
• ACK - Acknowledgment flag used in connection establishment and session termination.
• PSH - Push function.
• RST - Reset the connection when an error or timeout occurs.
• SYN - Synchronize sequence numbers used in connection establishment.
• FIN - No more data from the sender and used in session termination.
Reliability and Flow Control
TCP Reliability- Guaranteed and Ordered Delivery
• TCP can also help maintain the flow of packets so that devices do not become overloaded.
• There may be times when TCP segments do not arrive at their destination or arrive out of order.
• All the data must be received and the data in these segments must be reassembled into the original order.
• Sequence numbers are assigned in the header of each packet to achieve this goal.
TCP Reliability – Data Loss and Retransmission
No matter how well-designed a network is, data loss occasionally occurs. TCP provides methods of managing these
segment losses. Among these is a mechanism to retransmit segments for unacknowledged data.
Host operating systems today typically employ an optional TCP feature called selective acknowledgment (SACK),
negotiated during the three-way handshake. If both hosts support SACK, the receiver can explicitly acknowledge which
segments (bytes) were received including any discontinuous segments.
TCP Flow Control – Window Size and Acknowledgments
Maximum Segment Size (MSS) is the maximum amount of data that the destination device can receive.
• A common MSS is 1,460 bytes when using IPv4.
• A host determines the value of its MSS field by subtracting the IP and TCP headers from the Ethernet maximum
transmission unit (MTU), which is 1500 bytes be default.
• 1500 minus 40 (20 bytes for the IPv4 header and 20 bytes for the TCP header) leaves 1460 bytes.
TCP Flow Control – Congestion Avoidance
When congestion occurs on a network, it results in packets being discarded by the overloaded router.
To avoid and control congestion, TCP employs several congestion handling mechanisms, timers, and algorithms.
UDP Communication
Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is
most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as Internet
socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport
protocol, IP address, and port number.
The term socket is also used for the software endpoint of node-internal inter-process communication (IPC), which often
uses the same API as a network socket.
Socket Pairs
The source and destination ports are placed within the segment.
• The segments are then encapsulated within an IP packet.
• The combination of the source IP address and source port number, or the destination IP address and destination
port number is known as a socket.
• Sockets enable multiple processes, running on a client, to distinguish themselves from each other, and multiple
connections to a server process to be distinguished from each other.
Uses of Socket
The use of the term socket in software is analogous to the function of an electrical female connector, a device in hardware
for communication between nodes interconnected with an electrical cable. Similarly, the term port is used for external
physical endpoints at a node or device.
The application programming interface (API) for the network protocol stack creates a handle for each socket created by an
application, commonly referred to as a socket descriptor. In Unix-like operating systems, this descriptor is a type of file
descriptor. It is stored by the application process for use with every read and write operation on the communication
channel.
At the time of creation with the API, a network socket is bound to the combination of a type of network protocol to be
used for transmissions, a network address of the host, and a port number. Ports are numbered resources that represent
another type of software structure of the node. They are used as service types, and, once created by a process, serve as an
externally (from the network) addressable location component, so that other hosts may establish connections.
Network sockets may be dedicated for persistent connections for communication between two nodes, or they may
participate in connectionless and multicast communications.
NOTE: In practice, due to the proliferation of the TCP/IP protocols in use on the Internet, the term network socket usually
refers to use with the Internet Protocol (IP). It is therefore often also called an Internet socket.
Socket addresses
An application can communicate with a remote process by exchanging data with TCP/IP by knowing the combination of
protocol type, IP address, and port number. This combination is often known as a socket address. It is the network-facing
access handle to the network socket. The remote process establishes a network socket in its own instance of the protocol
stack and uses the networking API to connect to the application, presenting its own socket address for use by the
application.
One-to-many
across 4 hosts. Each host had a unique address for online communication.
The majority of network address translators map multiple private hosts to one publicly exposed IP address.
Type of NAT and NAT traversal, role of port preservation for TCP
The NAT traversal problem arises when peers behind different NATs try to communicate. One way to solve this problem is
to use port forwarding. Another way is to use various NAT traversal techniques. The most popular technique for TCP NAT
traversal is TCP hole punching.
TCP hole punching requires the NAT to follow the port preservation design for TCP. For a given outgoing TCP
communication, the same port numbers are used on both sides of the NAT. NAT port preservation for outgoing TCP
connections is crucial for TCP NAT traversal because, under TCP, one port can only be used for one communication at a
time, so programs bind distinct TCP sockets to ephemeral ports for each TCP communication, rendering NAT port
prediction impossible for TCP.
On the other hand, for UDP, NATs do not need port preservation. Indeed, multiple UDP communications (each with a
distinct endpoint) can occur on the same source port, and applications usually reuse the same UDP socket to send packets
to distinct hosts. This makes port prediction straightforward, as it is the same source port for each packet.
Furthermore, port preservation in NAT for TCP allows P2P protocols to offer less complexity and less latency because there
is no need to use a third party (like STUN) to discover the NAT port since the application itself already knows the NAT port.
However, if two internal hosts attempt to communicate with the same external host using the same port number, the NAT
may attempt to use a different external IP address for the second connection or may need to forgo port preservation and
remap the port.
As of 2006, roughly 70% of the clients in P2P networks employed some form of NAT.
Implementation
Establishing two-way communication
Every TCP and UDP packet contains a source port number and a destination port number. Each of those packets is
encapsulated in an IP packet, whose IP header contains a source IP address and a destination IP address. The IP
address/protocol/port number triple defines an association with a network socket.
For publicly accessible services such as web and mail servers, the port number is important. For example, port 80
connects through a socket to the web server software and port 25 to a mail server's SMTP daemon. The IP address of a
public server is also important, similar in global uniqueness to a postal address or telephone number. Both IP address and
port number must be correctly known by all hosts wishing to successfully communicate.
Private IP addresses as described in RFC 1918 are usable only on private networks not directly connected to the internet.
Ports are endpoints of communication unique to that host, so a connection through the NAT device is maintained by the
combined mapping of port and IP address. A private address on the inside of the NAT is mapped to an external public
address. Port address translation (PAT) resolves conflicts that arise when multiple hosts happen to use the same source
port number to establish different external connections at the same time.
IP packets have a checksum in each packet header, which provides error detection only for the header. IP datagrams may
become fragmented, and it is necessary for a NAT to reassemble these fragments to allow correct recalculation of higher-
level checksums and correct tracking of which packets belong to which connection.
TCP and UDP, have a checksum that covers all the data they carry, as well as the TCP or UDP header, plus a pseudo-header
that contains the source and destination IP addresses of the packet carrying the TCP or UDP header. For an originating NAT
to pass TCP or UDP successfully, it must recompute the TCP or UDP header checksum based on the translated IP addresses,
not the original ones, and put that checksum into the TCP or UDP header of the first packet of the fragmented set of
packets.
Alternatively, the originating host may perform path MTU (Maximum Transmission Unit) Discovery to determine the
packet size that can be transmitted without fragmentation and then set the don't fragment (DF) bit in the appropriate
packet header field. This is only a one-way solution because the responding host can send packets of any size, which may
be fragmented before reaching the NAT.
Static NAT allows connections to originate from either side of the network, but translation is limited to one-to-one or
between blocks of addresses of the same size. For each private address, a public address must be allocated. No address
pools are necessary.
NAT in IPv6
Network address translation is not commonly used in IPv6 because one of the design goals of IPv6 is to restore end-to-end
network connectivity. The large addressing space of IPv6 obviates the need to conserve addresses and every device can be
given a unique globally routable address. Use of unique local addresses in combination with network prefix translation can
achieve results similar to NAT.
The large addressing space of IPv6 can still be defeated depending on the actual prefix length given by the carrier. It is not
uncommon to be handed a /64 prefix – the smallest recommended subnet – for an entire home network, requiring a
variety of techniques to be used to manually subdivide the range for all devices to remain reachable. Even actual IPv6-to-
IPv6 NAT, NAT66, can turn out useful at times: the APNIC blog outlines a case where the author was only provided a single
address (/128).
For example
The first inside computer in the figure above is assigned an inside local address of 10.0.1.2. The computer has two opened
connections, which are assigned ports 1487 and 1488. As a result, communications to that computer are through the
inside global address 192.168.35.4:1487 or 192.168.35.4:1487. In some cases, the router will assign different port numbers
to an inside local address and its associated inside global address, as in the table's final entry, but this too can be easily
accommodated.
Note: This process is repeated for each device on the inside network, whether a laptop, tablet, smartphone, or other type
of smart device. In this way, all devices on the inside network can share the same public IP address even if they access the
internet at the same time. The router knows exactly which device to send specific packets to because of the unique port
number that has been assigned to each connection.