How TFTP works?

Trivial File Transfer Protocol (TFTP) is a simple light weight file transfer protocol, used for transferring files over the network. This protocol is similar to FTP but supports much lesser features and hence comes with a smaller foot print.

What TFTP provides

Faster file transfer, as it uses UDP as the transport layer protocol

Lesser Code size or foot print

Ascii and binary modes of file transfer

What TFTP does not provide

does not provide authentication

does not support a rich set of user interface commands

Use of TFTP

TFTP is mainly used during device bootstrap process for downloading device OS/firmware and configuration files. It is typically used for copying bootstrap and configuration files between nodes belonging to the same LAN.

TFTP is used in situations where all the features of a full file transfer protocol like FTP are not needed.

It is used along with boot protocols like BOOTP and DHCP to initialize devices. Whenever an IP enabled node boots up, it gets its IP address and other device and network related parameters through BOOTP or DHCP. As part of these parameters, the client also receives the TFTP server address, bootstrap file and configuration file details (file name and directory location). The client then uses the TFTP protocol to download the bootstrap image and configuration files from the TFTP server.

Basic Theory of Operation

TFTP uses UDP as the underlying transport layer protocol. Since UDP is much simpler when compared to the complicated TCP, it requires much lesser code space and hence TFTP can fit even inside small boot ROMs.

TFTP servers waits on the well known UDP port number 69. A TFTP client, that wishes to send or receive files from the server, establishes a UDP connection to the server, by opening a UDP socket to the server’s IP address on port 69.

The TFTP client then sends a read request (RRQ) to the server if it wants to get a file or sends a write request (WRQ) if it wants to transfer a file onto the server.

TFTP splits a file, to be transferred, into blocks of size 512 bytes and transfers it as TFTP DATA messages. Each TFTP DATA block is numbered and carried inside separate UDP messages.

The last block of a file is always sent with a size lesser than 512. When the peer receives a block with size less than 512 bytes, it treats that block as the last block of the file that is being transferred. Even if the file size happens to be an exact multiple of 512 bytes, TFTP sends a block with zero bytes as the final block, to indicate to the peer that the file transfer is over.

Reliability : Each block is numbered and sent inside a separate UDP message. Since TFTP uses UDP, reliabile delivery of each block is not guaranteed by the underlying network protocols. So, TFTP itself takes care of reliability by requiring the peer to acknowledge each successfully received block.

Flow Control: TFTP sends data block by block. After sending a block, the sending end starts a block timer. If an acknowledgment is received for the block from the peer before the timer expires, then the next block of the file is sent. Otherwise, the current block is resent as soon as the block timer expires and the whole process repeats itself till the block is successfully acknowledged. Hence, TFTP is basically a stop and wait protocol and flow control is achieved by the sender sending atmost one outstanding block at any instant of time.

TFTP messages

The TFTP protocol has basically 5 types of messages as given in the diagram below:

The RRQ and WRQ messages are used by the client to request the server to start reading or writing a file respectively. Both these messages send the file name and transfer mode (ascii or binary) as additional parameters.

The DATA messages carry the actual file blocks, with each message carrying a block of data. Each block has a sequence number field indicating the block number.

The ACK message is used to acknowledge successfully received data blocks. It has the sequence number as the additional parameter, indicating the block number that was successfully received. Whenever a block is received error free (indicated by the UDP checksum), then the receiving TFTP node immediately acknowledges the block to the peer, by sending an ACK message.

The ERROR message is sent to the peer whenever some operation could not be performed (e.g. invalid file name, file does not have read/write permissions etc).