TCP FLAG OPTIONS

TCP FLAG OPTIONS

As we know that in TCP 3-way handshake some TCP segments carry data while others are simple acknowledgements for previously received data. The popular 3-way handshake utilizes the SYNs and ACKs available in the TCP to help complete the connection before data is transferred.

Our conclusion is that each TCP segment has a purpose, and this is determined with the help of the TCP flag options, allowing the sender or receiver to specify which flags should be used so the segment is handled correctly by the other end.

Let's take a look at the TCP flags field to begin our analysis:

No alt text provided for this image

You can see the 2 flags that are used during the 3-way handshake (SYN, ACK) and data transfers.As with all flags, a value of '1' means that a particular flag is 'set' or, if you like, is 'on'. In this example, only the "SYN" flag is set, indicating that this is the first segment of a new TCP connection.

In addition to this, each flag is one bit long, and since there are 6 flags, this makes the Flags section 6 bits in total.

As we know that the most popular flags are the "SYN", "ACK" and "FIN", used to establish connections, acknowledge successful segment transfers and, lastly, terminate connections. While the rest of the flags are not as well known, their role and purpose makes them, in some cases, equally important.

We will begin our analysis by examining all six flags:

1st FLAG - PUSH

To understand the function of the PSH flag, we first need to understand how TCP buffers data. TCP operates at layer four of the OSI model; it presents to upper layers a simple socket which can be read from and written to, masking the complexities of packet-based communications. To allow applications to read from and write to this socket at any time, buffers are implemented on both sides of a TCP connection in both directions.

The diagram below shows how data is buffered by the sender before sending, and by the receiver upon reception.

No alt text provided for this image

Buffers allow for more efficient transfer of data when sending more than one maximum segment size (MSS) worth of data (for example, transferring a large file). However, large buffers do more harm than good when dealing with real-time applications which require that data be transmitted as quickly as possible. Consider what would happen to a Telnet session, for instance, if TCP waited until there was enough data to fill a packet before it would send one: You would have to type over a thousand characters before the first packet would make it to the remote device. Not very useful.

This is where the PSH flag comes in. The socket that TCP makes available at the session level can be written to by the application with the option of "pushing" data out immediately, rather than waiting for additional data to enter the buffer. When this happens, the PSH flag in the outgoing TCP packet is set to 1 (on). Upon receiving a packet with the PSH flag set, the other side of the connection knows to immediately forward the segment up to the application. To summarize, TCP's push capability accomplishes two things:

  • The sending application informs TCP that data should be sent immediately.
  • The PSH flag in the TCP header informs the receiving host that the data should be pushed up to the receiving application immediately.

 Please be noted that the PUSH flag is usually set on the last segment of a file to prevent buffer deadlocks. It is also seen when used to send HTTP or other types of requests through a proxy - ensuring the request is handled appropriately and effectively.

We can see an example of the PSH flag being used in this packet capture of an HTTP GET request. In packet #4, we see that the initial HTTP request has its PSH flag set, indicating that the client has no further data to add and the request should be sent up to the application (in this case, a web daemon) immediately. We also see that the server has set the PSH flag on packet #36, which contains the last bytes of the file requested. Again, the PSH flag is used to inform the receiver that the sender has no further data to transmit (for now).

No alt text provided for this image

As mentioned, the PSH flag is also used to facilitate real-time communication via TCP. Let’s take an example :

When you are using an interactive application such as the Telnet Protocol, you want each keystroke to be sent immediately to the other application; you don't want TCP to accumulate hundreds of keystrokes and then send them all at once. The latter may be more “efficient” but it makes the application unusable, which is really putting the cart before the horse.

Even with a very ordinary protocol that transfers files, there are many situations in which we need to say “send the data now”. For example, many protocols begin with a client sending a request to a server—like a request for a Web page sent by a Web browser. In that circumstance, we want the client's request sent immediately; we don't want to wait until enough requests have been accumulated by TCP to fill an “optimal-sized” segment.

2nd FLAG - URG FLAG

The URG flag is used to inform a receiving station that certain data within a segment is urgent and should be prioritized.

If the URG flag is set, the receiving station evaluates the urgent pointer, a 16-bit field in the TCP header. This pointer indicates how much of the data in the segment, counting from the first byte, is urgent.
No alt text provided for this image


An Urgent Pointer could be used during a stream of data transfer where a host is sending data to an application running on a remote machine. The most common example of this is when it is necessary to interrupt an application's data transfer. Suppose we have an application that sends large files in both directions between two devices. The user of the application realizes that the wrong file is being transferred. When he or she tells the application to stop the file being sent, we want this to be communicated to the other end of the TCP connection immediately. We don't want the “abort” command to just be placed at the end of the line after the file we are trying to send.Under normal circumstances, the abort signal will be sent and queued at the remote machine until all previously sent data is processed, however, in this case, we need the abort signal to be processed immediately.

By setting the abort signal's segment Urgent Pointer flag to '1', the remote machine will not wait till all queued data is processed and then execute the abort. Instead, it will give that specific segment priority, processing it immediately and stopping all further data processing.

3rd FLAG - ACKNOWLEDGEMENT

The ACKnowledgement flag is used to acknowledge the successful receipt of packets.If you run a packet sniffer while transferring data using the TCP, you will notice that, in most cases, for every packet you send or receive, an ACKnowledgement follows. So if you received a packet from a remote host, then your workstation will most probably send one back with the ACK field set to "1".

In some cases where the sender requires one ACKnowledgement for every 3 packets sent, the receiving end will send the ACK expected once (the 3rd sequential packet is received). This is also called Windowing and is covered extensively in my previous article.

https://www.dhirubhai.net/pulse/how-adjust-tcp-window-size-improve-network-priyanka-kumari/

4TH FLAG - RESET (RST) FLAG

The reset flag is used when a segment arrives that is not intended for the current connection. In other words, if you were to send a packet to a host in order to establish a connection, and there was no such service waiting to answer at the remote host, then the host would automatically reject your request and then send you a reply with the RST flag set. This indicates that the remote host has reset the connection.

While this might prove very simple and logical, the truth is that in most cases this 'feature' is used by most hackers in order to scan hosts for 'open' ports. All modern port scanners are able to detect 'open' or 'listening' ports thanks to the 'reset' function.

The method used to detect these ports is very simple: When attempting to scan a remote host, a valid TCP segment is constructed with the SYN flag set (1) and sent to the target host. If there is no service listening for incoming connections on the specific port, then the remote host will reply with ACK and RST flag set (1). If, on the other hand, there is a service listening on the port, the remote host will construct a TCP segment with the ACK flag set (1). This is, of course, part of the standard 3-way handshake we have covered.

Once the host scanning for open ports receives this segment, it will complete the 3-way handshake and then terminate it using the FIN (see below) flag, and mark the specific port as "active".

5TH FLAG - SYNCHRONISATION FLAG

The fifth flag contained in the TCP Flag options is perhaps the most well know flag used in TCP communications. As you might be aware, the SYN flag is initially sent when establishing the classical 3-way handshake between two hosts:

No alt text provided for this image

In the above diagram, Host A needs to download data from Host B using TCP as its transport protocol. The protocol requires the 3-way handshake to take place so a virtual connection can be established by both ends in order to exchange data.

During the 3-way handshake we are able to count a total of 2 SYN flags transmitted, one by each host. As files are exchanged and new connections created, we will see more SYN flags being sent and received.

6TH FLAG - FIN FLAG

The final flag available is the FIN flag, standing for the word FINished. This flag is used to tear down the virtual connections created using the previous flag (SYN), so because of this reason, the FIN flag always appears when the last packets are exchanged between a connection.

It is important to note that when a host sends a FIN flag to close a connection, it may continue to receive data until the remote host has also closed the connection, although this occurs only under certain circumstances. Once the connection is teared down by both sides, the buffers set aside on each end for the connection are released.

A normal teardown procedure is depicted below

No alt text provided for this image

The above diagram represents an existing connection between Host A and B, where the two hosts are exchanging data. Once the data transfer is complete, Host A sends a packet with the FIN, ACK flags set (STEP 1).

With this packet, Host A is ACKnowledging the previous stream while at the same time initiating the TCP close procedure to kill this connection. At this point, Host A's application will stop receiving any data and will close the connection from this side.

In response to Host A's request to close the connection, Host B will send an ACKnowledgement (STEP 2) back, and also notify its application that the connection is no longer available. Once this is complete, the host (B) will send its own FIN, ACK flags (STEP 3) to close their part of the connection.

We must be wondering why this procedure is required, please be informed that TCP is a Full Duplex connection, meaning that there are two directions of data flow. In our example this is the connection flow from Host A to Host B and vice versa. In addition, it requires both hosts to close the connection from their side, hence the reason behind the fact that both hosts must send a FIN flag and the other host must ACKnowledge it.

Lastly, at Step 4, Host A willl acknowledge the request Host B sent at STEP 3 and the closedown procedure for both sides is now complete!

KeyNote:

Perhaps the most important information given on this page that is beneficial to remember is the TCP handshake procedure and the fact that TCP is a Full Duplex connection.

Adding some more information about the URG and PSH flag, as we often get confused between these two:

  • The URG bit, if set prioritizes the data, meaning thereby instead of waiting for the entire byte stream to be transmitted which is ahead of the "Urgent" data, the urgent data will be sent on urgent basis and will not wait for the entire byte stream to be transmitted which is ahead of it.
  • When the URG bit is set the Urgent Pointer is also set (in the TCP header Options field: 16 bit).
  • The URG pointer tell how many bytes of the data is urgent in the segment that has arrived. (Example if the data size is 100 bytes and only firs 50 bytes is urgent, the urgent pointer will have a value of 50).
  • Now coming to the PSH bit. The purpose of the PSH bit is to tell TCP that do not wait for the buffer to become full and send the data immediately. Similarly when the receiver receives the segment with PSH flag set, should send the data immediately to the upper layer without waiting for the receive buffer to become full. The practical example of this is the telnet application where the application sends data in the form of few keystrokes. The telnet will become unusable if it waits for the buffer to become full and then transits the data to the receiver.

 




 

KattuBava Edaisal Shahul Hameed

L2L3 Protocol Testing | System/Solution Testing | Network Consulting Engineer | TAC Support

2 年

Wow, very useful.

回复
Vikas Gupta

Senior Application Security Engineer | ISO 27001 Lead Implementor | CxCE | CEH

3 年

Described in quite easy way and focused on important points. ??

回复
Leonar Tambunan

Non-Functional Quality (Architecture, Performance, and Security) Specialist

4 年

Thank you, Priyanka. It does helpful.

要查看或添加评论,请登录

Priyanka Shyam的更多文章

  • Designing a Data Center

    Designing a Data Center

    Happy Friday!! I have seen people asking quite a few times about designing a data center from scratch in interviews, in…

  • Distribute-list and Redistribute in Routing

    Distribute-list and Redistribute in Routing

    Happy Monday!! In the realm of networking and routing, understanding the nuances of commands like distribute-list and…

    2 条评论
  • Routing Table Codes

    Routing Table Codes

    Happy Friday!! Understanding routing table codes is crucial for network engineers to efficiently manage and…

    4 条评论
  • Implicit and Explicit Denial Rule in Firewall

    Implicit and Explicit Denial Rule in Firewall

    Happy Tuesday!! The purpose of this article is to discuss implicit denial and explicit denial within a firewall, and…

  • Spine and Leaf data center design.

    Spine and Leaf data center design.

    The topic of today's post is spine and leaf data center design. I would like to highlight a bit about east-west and…

  • All About Multicast IP Range

    All About Multicast IP Range

    Happy Tuesday!! In this post, we will discuss IP addressing for multicast applications. Multicast applications use an…

    1 条评论
  • ASDM "this app won't run on your computer" - Windows 10

    ASDM "this app won't run on your computer" - Windows 10

    Happy Friday!! As we all know, Cisco Adaptive Security Device Manager (ASDM) is software that enables users to manage…

    3 条评论
  • How Do Internet Bandwidth And Speed Differ?

    How Do Internet Bandwidth And Speed Differ?

    People often confuse bandwidth with speed. Some people believe that there is no difference between internet speed and…

    14 条评论
  • How does HTTP Tunneling work?

    How does HTTP Tunneling work?

    We all know that http method includes GET,POST,PUT,CONNECT,OPTIONS,TRACE,DELETE. We have already discussed the…

    2 条评论
  • A guide to creating self-signed certificates

    A guide to creating self-signed certificates

    During the SD-WAN implementation in my lab, I had to create the Root CA, generate CSRs, and generate self-signed…

    3 条评论

社区洞察

其他会员也浏览了