iPXE
Data Structures | Macros | Enumerations | Functions | Variables
tftp.c File Reference

TFTP protocol. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <ipxe/tcpip.h>
#include <ipxe/retry.h>
#include <ipxe/features.h>
#include <ipxe/bitmap.h>
#include <ipxe/settings.h>
#include <ipxe/dhcp.h>
#include <ipxe/tftp.h>

Go to the source code of this file.

Data Structures

struct  tftp_request
 A TFTP request. More...
 
struct  tftp_option
 A TFTP option. More...
 

Macros

#define EINVAL_BLKSIZE   __einfo_error ( EINFO_EINVAL_BLKSIZE )
 
#define EINFO_EINVAL_BLKSIZE
 
#define EINVAL_TSIZE   __einfo_error ( EINFO_EINVAL_TSIZE )
 
#define EINFO_EINVAL_TSIZE
 
#define EINVAL_MC_NO_PORT   __einfo_error ( EINFO_EINVAL_MC_NO_PORT )
 
#define EINFO_EINVAL_MC_NO_PORT
 
#define EINVAL_MC_NO_MC   __einfo_error ( EINFO_EINVAL_MC_NO_MC )
 
#define EINFO_EINVAL_MC_NO_MC
 
#define EINVAL_MC_INVALID_MC   __einfo_error ( EINFO_EINVAL_MC_INVALID_MC )
 
#define EINFO_EINVAL_MC_INVALID_MC
 
#define EINVAL_MC_INVALID_IP   __einfo_error ( EINFO_EINVAL_MC_INVALID_IP )
 
#define EINFO_EINVAL_MC_INVALID_IP
 
#define EINVAL_MC_INVALID_PORT   __einfo_error ( EINFO_EINVAL_MC_INVALID_PORT )
 
#define EINFO_EINVAL_MC_INVALID_PORT
 
#define MTFTP_MAX_TIMEOUTS   3
 Maximum number of MTFTP open requests before falling back to TFTP. More...
 

Enumerations

enum  { TFTP_FL_SEND_ACK = 0x0001, TFTP_FL_RRQ_SIZES = 0x0002, TFTP_FL_RRQ_MULTICAST = 0x0004, TFTP_FL_MTFTP_RECOVERY = 0x0008 }
 TFTP request flags. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FEATURE (FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1)
 
static void tftp_free (struct refcnt *refcnt)
 Free TFTP request. More...
 
static void tftp_done (struct tftp_request *tftp, int rc)
 Mark TFTP request as complete. More...
 
static int tftp_reopen (struct tftp_request *tftp)
 Reopen TFTP socket. More...
 
static int tftp_reopen_mc (struct tftp_request *tftp, struct sockaddr *local)
 Reopen TFTP multicast socket. More...
 
static int tftp_presize (struct tftp_request *tftp, size_t filesize)
 Presize TFTP receive buffers and block bitmap. More...
 
void tftp_set_mtftp_address (struct in_addr address)
 Set MTFTP multicast address. More...
 
void tftp_set_mtftp_port (unsigned int port)
 Set MTFTP multicast port. More...
 
static int tftp_send_rrq (struct tftp_request *tftp)
 Transmit RRQ. More...
 
static int tftp_send_ack (struct tftp_request *tftp)
 Transmit ACK. More...
 
static int tftp_send_error (struct tftp_request *tftp, int errcode, const char *errmsg)
 Transmit ERROR (Abort) More...
 
static int tftp_send_packet (struct tftp_request *tftp)
 Transmit next relevant packet. More...
 
static void tftp_timer_expired (struct retry_timer *timer, int fail)
 Handle TFTP retransmission timer expiry. More...
 
static int tftp_process_blksize (struct tftp_request *tftp, char *value)
 Process TFTP "blksize" option. More...
 
static int tftp_process_tsize (struct tftp_request *tftp, char *value)
 Process TFTP "tsize" option. More...
 
static int tftp_process_multicast (struct tftp_request *tftp, char *value)
 Process TFTP "multicast" option. More...
 
static int tftp_process_option (struct tftp_request *tftp, const char *name, char *value)
 Process TFTP option. More...
 
static int tftp_rx_oack (struct tftp_request *tftp, void *buf, size_t len)
 Receive OACK. More...
 
static int tftp_rx_data (struct tftp_request *tftp, struct io_buffer *iobuf)
 Receive DATA. More...
 
static int tftp_errcode_to_rc (unsigned int errcode)
 Convert TFTP error code to return status code. More...
 
static int tftp_rx_error (struct tftp_request *tftp, void *buf, size_t len)
 Receive ERROR. More...
 
static int tftp_rx (struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Receive new data. More...
 
static int tftp_socket_deliver (struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Receive new data via socket. More...
 
static size_t tftp_xfer_window (struct tftp_request *tftp)
 Check flow control window. More...
 
static void tftp_close (struct tftp_request *tftp, int rc)
 Terminate download. More...
 
static int tftp_core_open (struct interface *xfer, struct uri *uri, unsigned int default_port, struct sockaddr *multicast, unsigned int flags)
 Initiate TFTP/TFTM/MTFTP download. More...
 
static int tftp_open (struct interface *xfer, struct uri *uri)
 Initiate TFTP download. More...
 
static int tftm_open (struct interface *xfer, struct uri *uri)
 Initiate TFTM download. More...
 
static int mtftp_open (struct interface *xfer, struct uri *uri)
 Initiate MTFTP download. More...
 
static int tftp_apply_settings (void)
 Apply TFTP configuration settings. More...
 

Variables

static struct sockaddr_in tftp_mtftp_socket
 MTFTP multicast receive address. More...
 
static struct tftp_option tftp_options []
 Recognised TFTP options. More...
 
static struct interface_operation tftp_socket_operations []
 TFTP socket operations. More...
 
static struct interface_descriptor tftp_socket_desc
 TFTP socket interface descriptor. More...
 
static struct interface_operation tftp_mc_socket_operations []
 TFTP multicast socket operations. More...
 
static struct interface_descriptor tftp_mc_socket_desc
 TFTP multicast socket interface descriptor. More...
 
static struct interface_operation tftp_xfer_operations []
 TFTP data transfer interface operations. More...
 
static struct interface_descriptor tftp_xfer_desc
 TFTP data transfer interface descriptor. More...
 
struct uri_opener tftp_uri_opener __uri_opener
 TFTP URI opener. More...
 
struct settings_applicator tftp_settings_applicator __settings_applicator
 TFTP settings applicator. More...
 

Detailed Description

TFTP protocol.

Definition in file tftp.c.

Macro Definition Documentation

◆ EINVAL_BLKSIZE

#define EINVAL_BLKSIZE   __einfo_error ( EINFO_EINVAL_BLKSIZE )

Definition at line 57 of file tftp.c.

◆ EINFO_EINVAL_BLKSIZE

#define EINFO_EINVAL_BLKSIZE
Value:
( EINFO_EINVAL, 0x01, "Invalid blksize" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 58 of file tftp.c.

◆ EINVAL_TSIZE

#define EINVAL_TSIZE   __einfo_error ( EINFO_EINVAL_TSIZE )

Definition at line 60 of file tftp.c.

◆ EINFO_EINVAL_TSIZE

#define EINFO_EINVAL_TSIZE
Value:
( EINFO_EINVAL, 0x02, "Invalid tsize" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 61 of file tftp.c.

◆ EINVAL_MC_NO_PORT

#define EINVAL_MC_NO_PORT   __einfo_error ( EINFO_EINVAL_MC_NO_PORT )

Definition at line 63 of file tftp.c.

◆ EINFO_EINVAL_MC_NO_PORT

#define EINFO_EINVAL_MC_NO_PORT
Value:
( EINFO_EINVAL, 0x03, "Missing multicast port" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 64 of file tftp.c.

◆ EINVAL_MC_NO_MC

#define EINVAL_MC_NO_MC   __einfo_error ( EINFO_EINVAL_MC_NO_MC )

Definition at line 66 of file tftp.c.

◆ EINFO_EINVAL_MC_NO_MC

#define EINFO_EINVAL_MC_NO_MC
Value:
( EINFO_EINVAL, 0x04, "Missing multicast mc" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 67 of file tftp.c.

◆ EINVAL_MC_INVALID_MC

#define EINVAL_MC_INVALID_MC   __einfo_error ( EINFO_EINVAL_MC_INVALID_MC )

Definition at line 69 of file tftp.c.

◆ EINFO_EINVAL_MC_INVALID_MC

#define EINFO_EINVAL_MC_INVALID_MC
Value:
( EINFO_EINVAL, 0x05, "Missing multicast IP" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 70 of file tftp.c.

◆ EINVAL_MC_INVALID_IP

#define EINVAL_MC_INVALID_IP   __einfo_error ( EINFO_EINVAL_MC_INVALID_IP )

Definition at line 72 of file tftp.c.

◆ EINFO_EINVAL_MC_INVALID_IP

#define EINFO_EINVAL_MC_INVALID_IP
Value:
( EINFO_EINVAL, 0x06, "Invalid multicast IP" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 73 of file tftp.c.

◆ EINVAL_MC_INVALID_PORT

#define EINVAL_MC_INVALID_PORT   __einfo_error ( EINFO_EINVAL_MC_INVALID_PORT )

Definition at line 75 of file tftp.c.

◆ EINFO_EINVAL_MC_INVALID_PORT

#define EINFO_EINVAL_MC_INVALID_PORT
Value:
( EINFO_EINVAL, 0x07, "Invalid multicast port" )
#define EINFO_EINVAL
Definition: errno.h:429
#define __einfo_uniqify(einfo_base, uniq, desc)
Declare disambiguated error.
Definition: errno.h:180

Definition at line 76 of file tftp.c.

◆ MTFTP_MAX_TIMEOUTS

#define MTFTP_MAX_TIMEOUTS   3

Maximum number of MTFTP open requests before falling back to TFTP.

Definition at line 159 of file tftp.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

TFTP request flags.

Enumerator
TFTP_FL_SEND_ACK 

Send ACK packets.

TFTP_FL_RRQ_SIZES 

Request blksize and tsize options.

TFTP_FL_RRQ_MULTICAST 

Request multicast option.

TFTP_FL_MTFTP_RECOVERY 

Perform MTFTP recovery on timeout.

Definition at line 147 of file tftp.c.

147  {
148  /** Send ACK packets */
149  TFTP_FL_SEND_ACK = 0x0001,
150  /** Request blksize and tsize options */
151  TFTP_FL_RRQ_SIZES = 0x0002,
152  /** Request multicast option */
153  TFTP_FL_RRQ_MULTICAST = 0x0004,
154  /** Perform MTFTP recovery on timeout */
155  TFTP_FL_MTFTP_RECOVERY = 0x0008,
156 };
Send ACK packets.
Definition: tftp.c:149
Perform MTFTP recovery on timeout.
Definition: tftp.c:155
Request blksize and tsize options.
Definition: tftp.c:151
Request multicast option.
Definition: tftp.c:153

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FEATURE()

FEATURE ( FEATURE_PROTOCOL  ,
"TFTP"  ,
DHCP_EB_FEATURE_TFTP  ,
 
)

◆ tftp_free()

static void tftp_free ( struct refcnt refcnt)
static

Free TFTP request.

Parameters
refcntReference counter

Definition at line 166 of file tftp.c.

166  {
167  struct tftp_request *tftp =
168  container_of ( refcnt, struct tftp_request, refcnt );
169 
170  uri_put ( tftp->uri );
171  bitmap_free ( &tftp->bitmap );
172  free ( tftp );
173 }
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:188
static void bitmap_free(struct bitmap *bitmap)
Free bitmap resources.
Definition: bitmap.h:57
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:129
A TFTP request.
Definition: tftp.c:84
A reference counter.
Definition: refcnt.h:26
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct uri * uri
URI being fetched.
Definition: tftp.c:91

References tftp_request::bitmap, bitmap_free(), container_of, free, tftp_request::uri, and uri_put().

Referenced by tftp_core_open().

◆ tftp_done()

static void tftp_done ( struct tftp_request tftp,
int  rc 
)
static

Mark TFTP request as complete.

Parameters
tftpTFTP connection
rcReturn status code

Definition at line 181 of file tftp.c.

181  {
182 
183  DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
184  tftp, rc, strerror ( rc ) );
185 
186  /* Stop the retry timer */
187  stop_timer ( &tftp->timer );
188 
189  /* Close all data transfer interfaces */
190  intf_shutdown ( &tftp->socket, rc );
191  intf_shutdown ( &tftp->mc_socket, rc );
192  intf_shutdown ( &tftp->xfer, rc );
193 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
#define DBGC(...)
Definition: compiler.h:505
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
struct interface mc_socket
Multicast transport layer interface.
Definition: tftp.c:95
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct retry_timer timer
Retransmission timer.
Definition: tftp.c:143
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117

References DBGC, intf_shutdown(), tftp_request::mc_socket, rc, tftp_request::socket, stop_timer(), strerror(), tftp_request::timer, and tftp_request::xfer.

Referenced by tftp_close(), tftp_core_open(), tftp_rx_data(), tftp_rx_error(), tftp_rx_oack(), and tftp_timer_expired().

◆ tftp_reopen()

static int tftp_reopen ( struct tftp_request tftp)
static

Reopen TFTP socket.

Parameters
tftpTFTP connection
Return values
rcReturn status code

Definition at line 201 of file tftp.c.

201  {
202  struct sockaddr_tcpip server;
203  int rc;
204 
205  /* Close socket */
206  intf_restart ( &tftp->socket, 0 );
207 
208  /* Disable ACK sending. */
209  tftp->flags &= ~TFTP_FL_SEND_ACK;
210 
211  /* Reset peer address */
212  memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
213 
214  /* Open socket */
215  memset ( &server, 0, sizeof ( server ) );
216  server.st_port = htons ( tftp->port );
217  if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
218  ( struct sockaddr * ) &server,
219  tftp->uri->host, NULL ) ) != 0 ) {
220  DBGC ( tftp, "TFTP %p could not open socket: %s\n",
221  tftp, strerror ( rc ) );
222  return rc;
223  }
224 
225  return 0;
226 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
TCP/IP socket address.
Definition: tcpip.h:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:337
Send ACK packets.
Definition: tftp.c:149
#define SOCK_DGRAM
Definition: socket.h:29
#define DBGC(...)
Definition: compiler.h:505
unsigned int flags
Request flags.
Definition: tftp.c:124
Generalized socket address structure.
Definition: socket.h:96
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
const char * host
Host name.
Definition: uri.h:62
unsigned int port
Server port.
Definition: tftp.c:116
struct uri * uri
URI being fetched.
Definition: tftp.c:91
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define htons(value)
Definition: byteswap.h:135
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
void * memset(void *dest, int character, size_t len) __nonnull

References DBGC, tftp_request::flags, uri::host, htons, intf_restart(), memset(), NULL, tftp_request::peer, tftp_request::port, rc, SOCK_DGRAM, tftp_request::socket, sockaddr_tcpip::st_port, strerror(), TFTP_FL_SEND_ACK, tftp_request::uri, and xfer_open_named_socket().

Referenced by tftp_core_open(), and tftp_timer_expired().

◆ tftp_reopen_mc()

static int tftp_reopen_mc ( struct tftp_request tftp,
struct sockaddr local 
)
static

Reopen TFTP multicast socket.

Parameters
tftpTFTP connection
localLocal socket address
Return values
rcReturn status code

Definition at line 235 of file tftp.c.

236  {
237  int rc;
238 
239  /* Close multicast socket */
240  intf_restart ( &tftp->mc_socket, 0 );
241 
242  /* Open multicast socket. We never send via this socket, so
243  * use the local address as the peer address (since the peer
244  * address cannot be NULL).
245  */
246  if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
247  local, local ) ) != 0 ) {
248  DBGC ( tftp, "TFTP %p could not open multicast "
249  "socket: %s\n", tftp, strerror ( rc ) );
250  return rc;
251  }
252 
253  return 0;
254 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:337
int xfer_open_socket(struct interface *intf, int semantics, struct sockaddr *peer, struct sockaddr *local)
Open socket.
Definition: open.c:141
#define SOCK_DGRAM
Definition: socket.h:29
#define DBGC(...)
Definition: compiler.h:505
struct interface mc_socket
Multicast transport layer interface.
Definition: tftp.c:95
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78

References DBGC, intf_restart(), tftp_request::mc_socket, rc, SOCK_DGRAM, strerror(), and xfer_open_socket().

Referenced by tftp_core_open(), and tftp_process_multicast().

◆ tftp_presize()

static int tftp_presize ( struct tftp_request tftp,
size_t  filesize 
)
static

Presize TFTP receive buffers and block bitmap.

Parameters
tftpTFTP connection
filesizeKnown minimum file size
Return values
rcReturn status code

Definition at line 263 of file tftp.c.

263  {
264  unsigned int num_blocks;
265  int rc;
266 
267  /* Do nothing if we are already large enough */
268  if ( filesize <= tftp->filesize )
269  return 0;
270 
271  /* Record filesize */
272  tftp->filesize = filesize;
273 
274  /* Notify recipient of file size */
275  xfer_seek ( &tftp->xfer, filesize );
276  xfer_seek ( &tftp->xfer, 0 );
277 
278  /* Calculate expected number of blocks. Note that files whose
279  * length is an exact multiple of the blocksize will have a
280  * trailing zero-length block, which must be included.
281  */
282  if ( tftp->blksize == 0 )
283  return -EINVAL;
284  num_blocks = ( ( filesize / tftp->blksize ) + 1 );
285  if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
286  DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
287  "%s\n", tftp, num_blocks, strerror ( rc ) );
288  return rc;
289  }
290 
291  return 0;
292 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:129
#define DBGC(...)
Definition: compiler.h:505
unsigned int blksize
Data block size.
Definition: tftp.c:103
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
int bitmap_resize(struct bitmap *bitmap, unsigned int new_length)
Resize bitmap.
Definition: bitmap.c:42
int xfer_seek(struct interface *intf, off_t offset)
Seek to position.
Definition: xfer.c:350
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t filesize
Maximum known length.
Definition: tftp.c:141

References tftp_request::bitmap, bitmap_resize(), tftp_request::blksize, DBGC, EINVAL, tftp_request::filesize, rc, strerror(), tftp_request::xfer, and xfer_seek().

Referenced by tftp_rx_data(), and tftp_rx_oack().

◆ tftp_set_mtftp_address()

void tftp_set_mtftp_address ( struct in_addr  address)

Set MTFTP multicast address.

Parameters
addressMulticast IPv4 address

Definition at line 310 of file tftp.c.

310  {
312 }
uint64_t address
Base address.
Definition: ena.h:24
static struct sockaddr_in tftp_mtftp_socket
MTFTP multicast receive address.
Definition: tftp.c:299
struct in_addr sin_addr
IPv4 address.
Definition: in.h:98

References address, sockaddr_in::sin_addr, and tftp_mtftp_socket.

◆ tftp_set_mtftp_port()

void tftp_set_mtftp_port ( unsigned int  port)

Set MTFTP multicast port.

Parameters
portMulticast port

Definition at line 319 of file tftp.c.

319  {
321 }
u8 port
Port number.
Definition: CIB_PRM.h:31
uint16_t sin_port
TCP/IP port (part of struct sockaddr_tcpip)
Definition: in.h:91
static struct sockaddr_in tftp_mtftp_socket
MTFTP multicast receive address.
Definition: tftp.c:299
#define htons(value)
Definition: byteswap.h:135

References htons, port, sockaddr_in::sin_port, and tftp_mtftp_socket.

◆ tftp_send_rrq()

static int tftp_send_rrq ( struct tftp_request tftp)
static

Transmit RRQ.

Parameters
tftpTFTP connection
Return values
rcReturn status code

Definition at line 329 of file tftp.c.

329  {
330  const char *path = ( tftp->uri->path + 1 /* skip '/' */ );
331  struct tftp_rrq *rrq;
332  size_t len;
333  struct io_buffer *iobuf;
334  size_t blksize;
335 
336  DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
337 
338  /* Allocate buffer */
339  len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
340  + 5 + 1 /* "octet" + NUL */
341  + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
342  + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
343  + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
344  iobuf = xfer_alloc_iob ( &tftp->socket, len );
345  if ( ! iobuf )
346  return -ENOMEM;
347 
348  /* Determine block size */
349  blksize = xfer_window ( &tftp->xfer );
350  if ( blksize > TFTP_MAX_BLKSIZE )
352 
353  /* Build request */
354  rrq = iob_put ( iobuf, sizeof ( *rrq ) );
355  rrq->opcode = htons ( TFTP_RRQ );
356  iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
357  "%s%coctet", path, 0 ) + 1 );
358  if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
359  iob_put ( iobuf, snprintf ( iobuf->tail,
360  iob_tailroom ( iobuf ),
361  "blksize%c%zd%ctsize%c0",
362  0, blksize, 0, 0 ) + 1 );
363  }
364  if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
365  iob_put ( iobuf, snprintf ( iobuf->tail,
366  iob_tailroom ( iobuf ),
367  "multicast%c", 0 ) + 1 );
368  }
369 
370  /* RRQ always goes to the address specified in the initial
371  * xfer_open() call
372  */
373  return xfer_deliver_iob ( &tftp->socket, iobuf );
374 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
#define TFTP_MAX_BLKSIZE
Definition: tftp.h:16
#define iob_put(iobuf, len)
Definition: iobuf.h:116
int xfer_deliver_iob(struct interface *intf, struct io_buffer *iobuf)
Deliver datagram as I/O buffer without metadata.
Definition: xfer.c:254
#define DBGC(...)
Definition: compiler.h:505
uint32_t blksize
Block size for this segment.
Definition: pccrc.h:24
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:157
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:115
void * tail
End of data.
Definition: iobuf.h:46
#define ENOMEM
Not enough space.
Definition: errno.h:534
unsigned int flags
Request flags.
Definition: tftp.c:124
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
const char * path
Path.
Definition: uri.h:66
uint16_t opcode
Definition: tftp.h:38
Request blksize and tsize options.
Definition: tftp.c:151
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:171
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
A TFTP read request (RRQ) packet.
Definition: tftp.h:37
Request multicast option.
Definition: tftp.c:153
uint32_t len
Length.
Definition: ena.h:14
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
struct uri * uri
URI being fetched.
Definition: tftp.c:91
#define htons(value)
Definition: byteswap.h:135
#define TFTP_RRQ
Read request opcode.
Definition: tftp.h:18
A persistent I/O buffer.
Definition: iobuf.h:32

References blksize, DBGC, ENOMEM, tftp_request::flags, htons, iob_put, iob_tailroom(), len, tftp_rrq::opcode, uri::path, snprintf(), tftp_request::socket, strlen(), io_buffer::tail, TFTP_FL_RRQ_MULTICAST, TFTP_FL_RRQ_SIZES, TFTP_MAX_BLKSIZE, TFTP_RRQ, tftp_request::uri, tftp_request::xfer, xfer_alloc_iob(), xfer_deliver_iob(), and xfer_window().

Referenced by tftp_send_packet().

◆ tftp_send_ack()

static int tftp_send_ack ( struct tftp_request tftp)
static

Transmit ACK.

Parameters
tftpTFTP connection
Return values
rcReturn status code

Definition at line 382 of file tftp.c.

382  {
383  struct tftp_ack *ack;
384  struct io_buffer *iobuf;
385  struct xfer_metadata meta = {
386  .dest = ( struct sockaddr * ) &tftp->peer,
387  };
388  unsigned int block;
389 
390  /* Determine next required block number */
391  block = bitmap_first_gap ( &tftp->bitmap );
392  DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
393 
394  /* Allocate buffer */
395  iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
396  if ( ! iobuf )
397  return -ENOMEM;
398 
399  /* Build ACK */
400  ack = iob_put ( iobuf, sizeof ( *ack ) );
401  ack->opcode = htons ( TFTP_ACK );
402  ack->block = htons ( block );
403 
404  /* ACK always goes to the peer recorded from the RRQ response */
405  return xfer_deliver ( &tftp->socket, iobuf, &meta );
406 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
#define iob_put(iobuf, len)
Definition: iobuf.h:116
Data transfer metadata.
Definition: xfer.h:22
uint16_t opcode
Definition: tftp.h:51
uint16_t block
Definition: tftp.h:12
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:129
uint16_t block
Definition: tftp.h:52
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:157
#define TFTP_ACK
Data block acknowledgement opcode.
Definition: tftp.h:21
#define ENOMEM
Not enough space.
Definition: errno.h:534
int meta(WINDOW *, bool)
Generalized socket address structure.
Definition: socket.h:96
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
#define DBGC2(...)
Definition: compiler.h:522
static unsigned int bitmap_first_gap(struct bitmap *bitmap)
Get first gap within bitmap.
Definition: bitmap.h:69
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122
#define htons(value)
Definition: byteswap.h:135
A TFTP acknowledgement (ACK) packet.
Definition: tftp.h:50
A persistent I/O buffer.
Definition: iobuf.h:32

References tftp_request::bitmap, bitmap_first_gap(), block, tftp_ack::block, DBGC2, ENOMEM, htons, iob_put, meta(), tftp_ack::opcode, tftp_request::peer, tftp_request::socket, TFTP_ACK, xfer_alloc_iob(), and xfer_deliver().

Referenced by tftp_send_packet().

◆ tftp_send_error()

static int tftp_send_error ( struct tftp_request tftp,
int  errcode,
const char *  errmsg 
)
static

Transmit ERROR (Abort)

Parameters
tftpTFTP connection
errcodeTFTP error code
errmsgError message string
Return values
rcReturn status code

Definition at line 416 of file tftp.c.

417  {
418  struct tftp_error *err;
419  struct io_buffer *iobuf;
420  struct xfer_metadata meta = {
421  .dest = ( struct sockaddr * ) &tftp->peer,
422  };
423  size_t msglen;
424 
425  DBGC2 ( tftp, "TFTP %p sending ERROR %d: %s\n", tftp, errcode,
426  errmsg );
427 
428  /* Allocate buffer */
429  msglen = sizeof ( *err ) + strlen ( errmsg ) + 1 /* NUL */;
430  iobuf = xfer_alloc_iob ( &tftp->socket, msglen );
431  if ( ! iobuf )
432  return -ENOMEM;
433 
434  /* Build ERROR */
435  err = iob_put ( iobuf, msglen );
436  err->opcode = htons ( TFTP_ERROR );
437  err->errcode = htons ( errcode );
438  strcpy ( err->errmsg, errmsg );
439 
440  /* ERR always goes to the peer recorded from the RRQ response */
441  return xfer_deliver ( &tftp->socket, iobuf, &meta );
442 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
#define iob_put(iobuf, len)
Definition: iobuf.h:116
Data transfer metadata.
Definition: xfer.h:22
uint16_t errcode
Definition: tftp.h:12
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:157
#define ENOMEM
Not enough space.
Definition: errno.h:534
uint16_t errcode
Definition: tftp.h:58
char errmsg[0]
Definition: tftp.h:13
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:296
int meta(WINDOW *, bool)
Generalized socket address structure.
Definition: socket.h:96
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
#define TFTP_ERROR
Error opcode.
Definition: tftp.h:22
uint16_t opcode
Definition: tftp.h:57
char errmsg[0]
Definition: tftp.h:59
#define DBGC2(...)
Definition: compiler.h:522
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122
#define htons(value)
Definition: byteswap.h:135
A TFTP error (ERROR) packet.
Definition: tftp.h:56
A persistent I/O buffer.
Definition: iobuf.h:32

References DBGC2, ENOMEM, errcode, tftp_error::errcode, errmsg, tftp_error::errmsg, htons, iob_put, meta(), tftp_error::opcode, tftp_request::peer, tftp_request::socket, strcpy(), strlen(), TFTP_ERROR, xfer_alloc_iob(), and xfer_deliver().

Referenced by tftp_close().

◆ tftp_send_packet()

static int tftp_send_packet ( struct tftp_request tftp)
static

Transmit next relevant packet.

Parameters
tftpTFTP connection
Return values
rcReturn status code

Definition at line 450 of file tftp.c.

450  {
451 
452  /* Update retransmission timer. While name resolution takes place the
453  * window is zero. Avoid unnecessary delay after name resolution
454  * completes by retrying immediately.
455  */
456  stop_timer ( &tftp->timer );
457  if ( xfer_window ( &tftp->socket ) ) {
458  start_timer ( &tftp->timer );
459  } else {
460  start_timer_nodelay ( &tftp->timer );
461  }
462 
463  /* Send RRQ or ACK as appropriate */
464  if ( ! tftp->peer.st_family ) {
465  return tftp_send_rrq ( tftp );
466  } else {
467  if ( tftp->flags & TFTP_FL_SEND_ACK ) {
468  return tftp_send_ack ( tftp );
469  } else {
470  return 0;
471  }
472  }
473 }
struct interface socket
Transport layer interface.
Definition: tftp.c:93
Send ACK packets.
Definition: tftp.c:149
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:99
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:77
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:115
unsigned int flags
Request flags.
Definition: tftp.c:124
static int tftp_send_rrq(struct tftp_request *tftp)
Transmit RRQ.
Definition: tftp.c:329
void start_timer(struct retry_timer *timer)
Start timer.
Definition: retry.c:93
static int tftp_send_ack(struct tftp_request *tftp)
Transmit ACK.
Definition: tftp.c:382
struct retry_timer timer
Retransmission timer.
Definition: tftp.c:143
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122

References tftp_request::flags, tftp_request::peer, tftp_request::socket, sockaddr_tcpip::st_family, start_timer(), start_timer_nodelay(), stop_timer(), TFTP_FL_SEND_ACK, tftp_send_ack(), tftp_send_rrq(), tftp_request::timer, and xfer_window().

Referenced by tftp_rx_data(), tftp_rx_oack(), and tftp_timer_expired().

◆ tftp_timer_expired()

static void tftp_timer_expired ( struct retry_timer timer,
int  fail 
)
static

Handle TFTP retransmission timer expiry.

Parameters
timerRetry timer
failFailure indicator

Definition at line 481 of file tftp.c.

481  {
482  struct tftp_request *tftp =
483  container_of ( timer, struct tftp_request, timer );
484  int rc;
485 
486  /* If we are doing MTFTP, attempt the various recovery strategies */
487  if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
488  if ( tftp->peer.st_family ) {
489  /* If we have received any response from the server,
490  * try resending the RRQ to restart the download.
491  */
492  DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
493  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
494  goto err;
495  } else {
496  /* Fall back to plain TFTP after several attempts */
497  tftp->mtftp_timeouts++;
498  DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
499  "open\n", tftp, tftp->mtftp_timeouts );
500 
501  if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
502  DBGC ( tftp, "TFTP %p falling back to plain "
503  "TFTP\n", tftp );
504  tftp->flags = TFTP_FL_RRQ_SIZES;
505 
506  /* Close multicast socket */
507  intf_restart ( &tftp->mc_socket, 0 );
508 
509  /* Reset retry timer */
510  start_timer_nodelay ( &tftp->timer );
511 
512  /* The blocksize may change: discard
513  * the block bitmap
514  */
515  bitmap_free ( &tftp->bitmap );
516  memset ( &tftp->bitmap, 0,
517  sizeof ( tftp->bitmap ) );
518 
519  /* Reopen on standard TFTP port */
520  tftp->port = TFTP_PORT;
521  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
522  goto err;
523  }
524  }
525  } else {
526  /* Not doing MTFTP (or have fallen back to plain
527  * TFTP); fail as per normal.
528  */
529  if ( fail ) {
530  rc = -ETIMEDOUT;
531  goto err;
532  }
533  }
534  tftp_send_packet ( tftp );
535  return;
536 
537  err:
538  tftp_done ( tftp, rc );
539 }
unsigned int mtftp_timeouts
MTFTP timeout count.
Definition: tftp.c:126
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:337
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:99
static int tftp_reopen(struct tftp_request *tftp)
Reopen TFTP socket.
Definition: tftp.c:201
static void bitmap_free(struct bitmap *bitmap)
Free bitmap resources.
Definition: bitmap.h:57
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:77
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:129
#define DBGC(...)
Definition: compiler.h:505
#define MTFTP_MAX_TIMEOUTS
Maximum number of MTFTP open requests before falling back to TFTP.
Definition: tftp.c:159
A TFTP request.
Definition: tftp.c:84
A timer.
Definition: timer.h:28
Perform MTFTP recovery on timeout.
Definition: tftp.c:155
unsigned int flags
Request flags.
Definition: tftp.c:124
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
struct interface mc_socket
Multicast transport layer interface.
Definition: tftp.c:95
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181
static int tftp_send_packet(struct tftp_request *tftp)
Transmit next relevant packet.
Definition: tftp.c:450
Request blksize and tsize options.
Definition: tftp.c:151
#define TFTP_PORT
Default TFTP server port.
Definition: tftp.h:14
struct retry_timer timer
Retransmission timer.
Definition: tftp.c:143
unsigned int port
Server port.
Definition: tftp.c:116
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
void * memset(void *dest, int character, size_t len) __nonnull

References tftp_request::bitmap, bitmap_free(), container_of, DBGC, ETIMEDOUT, tftp_request::flags, intf_restart(), tftp_request::mc_socket, memset(), MTFTP_MAX_TIMEOUTS, tftp_request::mtftp_timeouts, tftp_request::peer, tftp_request::port, rc, sockaddr_tcpip::st_family, start_timer_nodelay(), tftp_done(), TFTP_FL_MTFTP_RECOVERY, TFTP_FL_RRQ_SIZES, TFTP_PORT, tftp_reopen(), tftp_send_packet(), and tftp_request::timer.

Referenced by tftp_core_open().

◆ tftp_process_blksize()

static int tftp_process_blksize ( struct tftp_request tftp,
char *  value 
)
static

Process TFTP "blksize" option.

Parameters
tftpTFTP connection
valueOption value
Return values
rcReturn status code

Definition at line 548 of file tftp.c.

548  {
549  char *end;
550 
551  tftp->blksize = strtoul ( value, &end, 10 );
552  if ( *end ) {
553  DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
554  tftp, value );
555  return -EINVAL_BLKSIZE;
556  }
557  DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
558 
559  return 0;
560 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
#define DBGC(...)
Definition: compiler.h:505
unsigned int blksize
Data block size.
Definition: tftp.c:103
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define EINVAL_BLKSIZE
Definition: tftp.c:57
uint32_t end
Ending offset.
Definition: netvsc.h:18

References tftp_request::blksize, DBGC, EINVAL_BLKSIZE, end, strtoul(), and value.

◆ tftp_process_tsize()

static int tftp_process_tsize ( struct tftp_request tftp,
char *  value 
)
static

Process TFTP "tsize" option.

Parameters
tftpTFTP connection
valueOption value
Return values
rcReturn status code

Definition at line 569 of file tftp.c.

569  {
570  char *end;
571 
572  tftp->tsize = strtoul ( value, &end, 10 );
573  if ( *end ) {
574  DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
575  tftp, value );
576  return -EINVAL_TSIZE;
577  }
578  DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
579 
580  return 0;
581 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
#define DBGC(...)
Definition: compiler.h:505
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint32_t end
Ending offset.
Definition: netvsc.h:18
unsigned long tsize
File size.
Definition: tftp.c:110
#define EINVAL_TSIZE
Definition: tftp.c:60

References DBGC, EINVAL_TSIZE, end, strtoul(), tftp_request::tsize, and value.

◆ tftp_process_multicast()

static int tftp_process_multicast ( struct tftp_request tftp,
char *  value 
)
static

Process TFTP "multicast" option.

Parameters
tftpTFTP connection
valueOption value
Return values
rcReturn status code

Definition at line 590 of file tftp.c.

590  {
591  union {
592  struct sockaddr sa;
593  struct sockaddr_in sin;
594  } socket;
595  char *addr;
596  char *port;
597  char *port_end;
598  char *mc;
599  char *mc_end;
600  int rc;
601 
602  /* Split value into "addr,port,mc" fields */
603  addr = value;
604  port = strchr ( addr, ',' );
605  if ( ! port ) {
606  DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
607  return -EINVAL_MC_NO_PORT;
608  }
609  *(port++) = '\0';
610  mc = strchr ( port, ',' );
611  if ( ! mc ) {
612  DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
613  return -EINVAL_MC_NO_MC;
614  }
615  *(mc++) = '\0';
616 
617  /* Parse parameters */
618  if ( strtoul ( mc, &mc_end, 0 ) == 0 )
619  tftp->flags &= ~TFTP_FL_SEND_ACK;
620  if ( *mc_end ) {
621  DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
622  return -EINVAL_MC_INVALID_MC;
623  }
624  DBGC ( tftp, "TFTP %p is%s the master client\n",
625  tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
626  if ( *addr && *port ) {
627  socket.sin.sin_family = AF_INET;
628  if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
629  DBGC ( tftp, "TFTP %p multicast invalid IP address "
630  "%s\n", tftp, addr );
631  return -EINVAL_MC_INVALID_IP;
632  }
633  DBGC ( tftp, "TFTP %p multicast IP address %s\n",
634  tftp, inet_ntoa ( socket.sin.sin_addr ) );
635  socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
636  if ( *port_end ) {
637  DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
638  tftp, port );
639  return -EINVAL_MC_INVALID_PORT;
640  }
641  DBGC ( tftp, "TFTP %p multicast port %d\n",
642  tftp, ntohs ( socket.sin.sin_port ) );
643  if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
644  return rc;
645  }
646 
647  return 0;
648 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Send ACK packets.
Definition: tftp.c:149
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
struct sockaddr sa
Definition: dns.c:68
#define EINVAL_MC_NO_MC
Definition: tftp.c:66
#define EINVAL_MC_INVALID_IP
Definition: tftp.c:72
#define DBGC(...)
Definition: compiler.h:505
#define EINVAL_MC_INVALID_PORT
Definition: tftp.c:75
IPv4 socket address.
Definition: in.h:82
#define ntohs(value)
Definition: byteswap.h:136
unsigned int flags
Request flags.
Definition: tftp.c:124
u8 port
Port number.
Definition: CIB_PRM.h:31
struct sockaddr_in sin
Definition: dns.c:70
#define EINVAL_MC_NO_PORT
Definition: tftp.c:63
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
Generalized socket address structure.
Definition: socket.h:96
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:241
int inet_aton(const char *string, struct in_addr *in)
Parse IPv4 address.
Definition: ipv4.c:631
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:658
u32 addr
Definition: sky2.h:8
#define EINVAL_MC_INVALID_MC
Definition: tftp.c:69
static int tftp_reopen_mc(struct tftp_request *tftp, struct sockaddr *local)
Reopen TFTP multicast socket.
Definition: tftp.c:235
#define htons(value)
Definition: byteswap.h:135
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
if(natsemi->flags &NATSEMI_64BIT) return 1

References addr, AF_INET, DBGC, EINVAL_MC_INVALID_IP, EINVAL_MC_INVALID_MC, EINVAL_MC_INVALID_PORT, EINVAL_MC_NO_MC, EINVAL_MC_NO_PORT, tftp_request::flags, htons, if(), inet_aton(), inet_ntoa(), ntohs, port, rc, sa, sin, strchr(), strtoul(), TFTP_FL_SEND_ACK, tftp_reopen_mc(), and value.

◆ tftp_process_option()

static int tftp_process_option ( struct tftp_request tftp,
const char *  name,
char *  value 
)
static

Process TFTP option.

Parameters
tftpTFTP connection
nameOption name
valueOption value
Return values
rcReturn status code

Definition at line 679 of file tftp.c.

680  {
681  struct tftp_option *option;
682 
683  for ( option = tftp_options ; option->name ; option++ ) {
684  if ( strcasecmp ( name, option->name ) == 0 )
685  return option->process ( tftp, value );
686  }
687 
688  DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
689  tftp, name, value );
690 
691  /* Unknown options should be silently ignored */
692  return 0;
693 }
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:192
A TFTP option.
Definition: tftp.c:651
A long option, as used for getopt_long()
Definition: getopt.h:24
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
const char * name
Long name of this option.
Definition: getopt.h:26
static struct tftp_option tftp_options[]
Recognised TFTP options.
Definition: tftp.c:664

References DBGC, option::name, name, strcasecmp(), tftp_options, and value.

Referenced by tftp_rx_oack().

◆ tftp_rx_oack()

static int tftp_rx_oack ( struct tftp_request tftp,
void *  buf,
size_t  len 
)
static

Receive OACK.

Parameters
tftpTFTP connection
bufTemporary data buffer
lenLength of temporary data buffer
Return values
rcReturn status code

Definition at line 703 of file tftp.c.

703  {
704  struct tftp_oack *oack = buf;
705  char *end = buf + len;
706  char *name;
707  char *value;
708  char *next;
709  int rc = 0;
710 
711  /* Sanity check */
712  if ( len < sizeof ( *oack ) ) {
713  DBGC ( tftp, "TFTP %p received underlength OACK packet "
714  "length %zd\n", tftp, len );
715  rc = -EINVAL;
716  goto done;
717  }
718 
719  /* Process each option in turn */
720  for ( name = oack->data ; name < end ; name = next ) {
721 
722  /* Parse option name and value
723  *
724  * We treat parsing errors as non-fatal, because there
725  * exists at least one TFTP server (IBM Tivoli PXE
726  * Server 5.1.0.3) that has been observed to send
727  * malformed OACKs containing trailing garbage bytes.
728  */
729  value = ( name + strnlen ( name, ( end - name ) ) + 1 );
730  if ( value > end ) {
731  DBGC ( tftp, "TFTP %p received OACK with malformed "
732  "option name:\n", tftp );
733  DBGC_HD ( tftp, oack, len );
734  break;
735  }
736  if ( value == end ) {
737  DBGC ( tftp, "TFTP %p received OACK missing value "
738  "for option \"%s\"\n", tftp, name );
739  DBGC_HD ( tftp, oack, len );
740  break;
741  }
742  next = ( value + strnlen ( value, ( end - value ) ) + 1 );
743  if ( next > end ) {
744  DBGC ( tftp, "TFTP %p received OACK with malformed "
745  "value for option \"%s\":\n", tftp, name );
746  DBGC_HD ( tftp, oack, len );
747  break;
748  }
749 
750  /* Process option */
751  if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
752  goto done;
753  }
754 
755  /* Process tsize information, if available */
756  if ( tftp->tsize ) {
757  if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
758  goto done;
759  }
760 
761  /* Request next data block */
762  tftp_send_packet ( tftp );
763 
764  done:
765  if ( rc )
766  tftp_done ( tftp, rc );
767  return rc;
768 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
A TFTP options acknowledgement (OACK) packet.
Definition: tftp.h:63
uint32_t next
Next descriptor address.
Definition: myson.h:18
static int tftp_process_option(struct tftp_request *tftp, const char *name, char *value)
Process TFTP option.
Definition: tftp.c:679
#define DBGC(...)
Definition: compiler.h:505
static int tftp_presize(struct tftp_request *tftp, size_t filesize)
Presize TFTP receive buffers and block bitmap.
Definition: tftp.c:263
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static int tftp_send_packet(struct tftp_request *tftp)
Transmit next relevant packet.
Definition: tftp.c:450
#define DBGC_HD(...)
Definition: compiler.h:507
char data[0]
Definition: tftp.h:65
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:225
uint32_t len
Length.
Definition: ena.h:14
uint32_t end
Ending offset.
Definition: netvsc.h:18
unsigned long tsize
File size.
Definition: tftp.c:110
struct bofm_section_header done
Definition: bofm_test.c:46

References tftp_oack::data, DBGC, DBGC_HD, done, EINVAL, end, len, name, next, rc, strnlen(), tftp_done(), tftp_presize(), tftp_process_option(), tftp_send_packet(), tftp_request::tsize, and value.

Referenced by tftp_rx().

◆ tftp_rx_data()

static int tftp_rx_data ( struct tftp_request tftp,
struct io_buffer iobuf 
)
static

Receive DATA.

Parameters
tftpTFTP connection
iobufI/O buffer
Return values
rcReturn status code

Takes ownership of I/O buffer.

Definition at line 779 of file tftp.c.

780  {
781  struct tftp_data *data = iobuf->data;
782  struct xfer_metadata meta;
783  unsigned int block;
784  off_t offset;
785  size_t data_len;
786  int rc;
787 
788  /* Sanity check */
789  if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
790  DBGC ( tftp, "TFTP %p received underlength DATA packet "
791  "length %zd\n", tftp, iob_len ( iobuf ) );
792  rc = -EINVAL;
793  goto done;
794  }
795 
796  /* Calculate block number */
797  block = ( ( bitmap_first_gap ( &tftp->bitmap ) + 1 ) & ~0xffff );
798  if ( data->block == 0 && block == 0 ) {
799  DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
800  rc = -EINVAL;
801  goto done;
802  }
803  block += ( ntohs ( data->block ) - 1 );
804 
805  /* Extract data */
806  offset = ( block * tftp->blksize );
807  iob_pull ( iobuf, sizeof ( *data ) );
808  data_len = iob_len ( iobuf );
809  if ( data_len > tftp->blksize ) {
810  DBGC ( tftp, "TFTP %p received overlength DATA packet "
811  "length %zd\n", tftp, data_len );
812  rc = -EINVAL;
813  goto done;
814  }
815 
816  /* Deliver data */
817  memset ( &meta, 0, sizeof ( meta ) );
818  meta.flags = XFER_FL_ABS_OFFSET;
819  meta.offset = offset;
820  if ( ( rc = xfer_deliver ( &tftp->xfer, iob_disown ( iobuf ),
821  &meta ) ) != 0 ) {
822  DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
823  tftp, strerror ( rc ) );
824  goto done;
825  }
826 
827  /* Ensure block bitmap is ready */
828  if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
829  goto done;
830 
831  /* Mark block as received */
832  bitmap_set ( &tftp->bitmap, block );
833 
834  /* Acknowledge block */
835  tftp_send_packet ( tftp );
836 
837  /* If all blocks have been received, finish. */
838  if ( bitmap_full ( &tftp->bitmap ) )
839  tftp_done ( tftp, 0 );
840 
841  done:
842  free_iob ( iobuf );
843  if ( rc )
844  tftp_done ( tftp, rc );
845  return rc;
846 }
#define iob_pull(iobuf, len)
Definition: iobuf.h:98
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Data transfer metadata.
Definition: xfer.h:22
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition: xfer.h:47
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
uint16_t block
Definition: tftp.h:12
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:129
#define DBGC(...)
Definition: compiler.h:505
A TFTP data (DATA) packet.
Definition: tftp.h:43
#define ntohs(value)
Definition: byteswap.h:136
static int tftp_presize(struct tftp_request *tftp, size_t filesize)
Presize TFTP receive buffers and block bitmap.
Definition: tftp.c:263
unsigned int blksize
Data block size.
Definition: tftp.c:103
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181
int meta(WINDOW *, bool)
static int tftp_send_packet(struct tftp_request *tftp)
Transmit next relevant packet.
Definition: tftp.c:450
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
void bitmap_set(struct bitmap *bitmap, unsigned int bit)
Set bit in bitmap.
Definition: bitmap.c:93
signed long off_t
Definition: stdint.h:8
void * data
Start of data.
Definition: iobuf.h:44
static unsigned int bitmap_first_gap(struct bitmap *bitmap)
Get first gap within bitmap.
Definition: bitmap.h:69
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
static int bitmap_full(struct bitmap *bitmap)
Check to see if bitmap is full.
Definition: bitmap.h:81
struct bofm_section_header done
Definition: bofm_test.c:46
void * memset(void *dest, int character, size_t len) __nonnull

References tftp_request::bitmap, bitmap_first_gap(), bitmap_full(), bitmap_set(), tftp_request::blksize, block, data, io_buffer::data, DBGC, done, EINVAL, free_iob(), iob_disown, iob_len(), iob_pull, memset(), meta(), ntohs, offset, rc, strerror(), tftp_done(), tftp_presize(), tftp_send_packet(), tftp_request::xfer, xfer_deliver(), and XFER_FL_ABS_OFFSET.

Referenced by tftp_rx().

◆ tftp_errcode_to_rc()

static int tftp_errcode_to_rc ( unsigned int  errcode)
static

Convert TFTP error code to return status code.

Parameters
errcodeTFTP error code
Return values
rcReturn status code

Definition at line 854 of file tftp.c.

854  {
855  switch ( errcode ) {
856  case TFTP_ERR_FILE_NOT_FOUND: return -ENOENT;
857  case TFTP_ERR_ACCESS_DENIED: return -EACCES;
858  case TFTP_ERR_ILLEGAL_OP: return -ENOTTY;
859  default: return -ENOTSUP;
860  }
861 }
uint16_t errcode
Definition: tftp.h:12
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define EACCES
Permission denied.
Definition: errno.h:298
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
#define TFTP_ERR_ILLEGAL_OP
Illegal TFTP operation.
Definition: tftp.h:28
#define TFTP_ERR_ACCESS_DENIED
Access violation.
Definition: tftp.h:26
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
#define TFTP_ERR_FILE_NOT_FOUND
File not found.
Definition: tftp.h:25

References EACCES, ENOENT, ENOTSUP, ENOTTY, errcode, TFTP_ERR_ACCESS_DENIED, TFTP_ERR_FILE_NOT_FOUND, and TFTP_ERR_ILLEGAL_OP.

Referenced by tftp_rx_error().

◆ tftp_rx_error()

static int tftp_rx_error ( struct tftp_request tftp,
void *  buf,
size_t  len 
)
static

Receive ERROR.

Parameters
tftpTFTP connection
bufTemporary data buffer
lenLength of temporary data buffer
Return values
rcReturn status code

Definition at line 871 of file tftp.c.

871  {
872  struct tftp_error *error = buf;
873  int rc;
874 
875  /* Sanity check */
876  if ( len < sizeof ( *error ) ) {
877  DBGC ( tftp, "TFTP %p received underlength ERROR packet "
878  "length %zd\n", tftp, len );
879  return -EINVAL;
880  }
881 
882  DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
883  "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
884 
885  /* Determine final operation result */
886  rc = tftp_errcode_to_rc ( ntohs ( error->errcode ) );
887 
888  /* Close TFTP request */
889  tftp_done ( tftp, rc );
890 
891  return 0;
892 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int tftp_errcode_to_rc(unsigned int errcode)
Convert TFTP error code to return status code.
Definition: tftp.c:854
struct arbelprm_completion_with_error error
Definition: arbel.h:12
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181
uint32_t len
Length.
Definition: ena.h:14
A TFTP error (ERROR) packet.
Definition: tftp.h:56

References DBGC, EINVAL, error, len, ntohs, rc, tftp_done(), and tftp_errcode_to_rc().

Referenced by tftp_rx().

◆ tftp_rx()

static int tftp_rx ( struct tftp_request tftp,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Receive new data.

Parameters
tftpTFTP connection
iobufI/O buffer
metaTransfer metadata
Return values
rcReturn status code

Definition at line 902 of file tftp.c.

904  {
905  struct sockaddr_tcpip *st_src;
906  struct tftp_common *common = iobuf->data;
907  size_t len = iob_len ( iobuf );
908  int rc = -EINVAL;
909 
910  /* Sanity checks */
911  if ( len < sizeof ( *common ) ) {
912  DBGC ( tftp, "TFTP %p received underlength packet length "
913  "%zd\n", tftp, len );
914  goto done;
915  }
916  if ( ! meta->src ) {
917  DBGC ( tftp, "TFTP %p received packet without source port\n",
918  tftp );
919  goto done;
920  }
921 
922  /* Filter by TID. Set TID on first response received */
923  st_src = ( struct sockaddr_tcpip * ) meta->src;
924  if ( ! tftp->peer.st_family ) {
925  memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
926  DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
927  ntohs ( tftp->peer.st_port ) );
928  } else if ( memcmp ( &tftp->peer, st_src,
929  sizeof ( tftp->peer ) ) != 0 ) {
930  DBGC ( tftp, "TFTP %p received packet from wrong source (got "
931  "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
932  ntohs ( tftp->peer.st_port ) );
933  goto done;
934  }
935 
936  switch ( common->opcode ) {
937  case htons ( TFTP_OACK ):
938  rc = tftp_rx_oack ( tftp, iobuf->data, len );
939  break;
940  case htons ( TFTP_DATA ):
941  rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
942  break;
943  case htons ( TFTP_ERROR ):
944  rc = tftp_rx_error ( tftp, iobuf->data, len );
945  break;
946  default:
947  DBGC ( tftp, "TFTP %p received strange packet type %d\n",
948  tftp, ntohs ( common->opcode ) );
949  break;
950  };
951 
952  done:
953  free_iob ( iobuf );
954  return rc;
955 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
TCP/IP socket address.
Definition: tcpip.h:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:77
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
static int tftp_rx_error(struct tftp_request *tftp, void *buf, size_t len)
Receive ERROR.
Definition: tftp.c:871
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static int tftp_rx_data(struct tftp_request *tftp, struct io_buffer *iobuf)
Receive DATA.
Definition: tftp.c:779
int meta(WINDOW *, bool)
uint16_t st_port
TCP/IP port.
Definition: tcpip.h:81
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
The common header of all TFTP packets.
Definition: tftp.h:69
struct ib_cm_common common
Definition: ib_mad.h:11
#define TFTP_ERROR
Error opcode.
Definition: tftp.h:22
#define TFTP_OACK
Options acknowledgement opcode.
Definition: tftp.h:23
static int tftp_rx_oack(struct tftp_request *tftp, void *buf, size_t len)
Receive OACK.
Definition: tftp.c:703
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:44
#define TFTP_DATA
Data block opcode.
Definition: tftp.h:20
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:122
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define htons(value)
Definition: byteswap.h:135
struct bofm_section_header done
Definition: bofm_test.c:46
if(natsemi->flags &NATSEMI_64BIT) return 1

References common, io_buffer::data, DBGC, done, EINVAL, free_iob(), htons, if(), iob_disown, iob_len(), len, memcmp(), memcpy(), meta(), ntohs, tftp_request::peer, rc, sockaddr_tcpip::st_family, sockaddr_tcpip::st_port, TFTP_DATA, TFTP_ERROR, TFTP_OACK, tftp_rx_data(), tftp_rx_error(), and tftp_rx_oack().

Referenced by tftp_socket_deliver().

◆ tftp_socket_deliver()

static int tftp_socket_deliver ( struct tftp_request tftp,
struct io_buffer iobuf,
struct xfer_metadata meta 
)
static

Receive new data via socket.

Parameters
tftpTFTP connection
iobufI/O buffer
metaTransfer metadata
Return values
rcReturn status code

Definition at line 965 of file tftp.c.

967  {
968 
969  /* Enable sending ACKs when we receive a unicast packet. This
970  * covers three cases:
971  *
972  * 1. Standard TFTP; we should always send ACKs, and will
973  * always receive a unicast packet before we need to send the
974  * first ACK.
975  *
976  * 2. RFC2090 multicast TFTP; the only unicast packets we will
977  * receive are the OACKs; enable sending ACKs here (before
978  * processing the OACK) and disable it when processing the
979  * multicast option if we are not the master client.
980  *
981  * 3. MTFTP; receiving a unicast datagram indicates that we
982  * are the "master client" and should send ACKs.
983  */
984  tftp->flags |= TFTP_FL_SEND_ACK;
985 
986  return tftp_rx ( tftp, iobuf, meta );
987 }
Send ACK packets.
Definition: tftp.c:149
unsigned int flags
Request flags.
Definition: tftp.c:124
int meta(WINDOW *, bool)
static int tftp_rx(struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive new data.
Definition: tftp.c:902

References tftp_request::flags, meta(), TFTP_FL_SEND_ACK, and tftp_rx().

◆ tftp_xfer_window()

static size_t tftp_xfer_window ( struct tftp_request tftp)
static

Check flow control window.

Parameters
tftpTFTP connection
Return values
lenLength of window

Definition at line 1013 of file tftp.c.

1013  {
1014 
1015  /* We abuse this data-xfer method to convey the blocksize to
1016  * the caller. This really should be done using some kind of
1017  * stat() method, but we don't yet have the facility to do
1018  * that.
1019  */
1020  return tftp->blksize;
1021 }
unsigned int blksize
Data block size.
Definition: tftp.c:103

References tftp_request::blksize.

◆ tftp_close()

static void tftp_close ( struct tftp_request tftp,
int  rc 
)
static

Terminate download.

Parameters
tftpTFTP connection
rcReason for close

Definition at line 1029 of file tftp.c.

1029  {
1030 
1031  /* Abort download */
1032  tftp_send_error ( tftp, 0, "TFTP Aborted" );
1033 
1034  /* Close TFTP request */
1035  tftp_done ( tftp, rc );
1036 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int tftp_send_error(struct tftp_request *tftp, int errcode, const char *errmsg)
Transmit ERROR (Abort)
Definition: tftp.c:416
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181

References rc, tftp_done(), and tftp_send_error().

Referenced by pxenv_tftp_close().

◆ tftp_core_open()

static int tftp_core_open ( struct interface xfer,
struct uri uri,
unsigned int  default_port,
struct sockaddr multicast,
unsigned int  flags 
)
static

Initiate TFTP/TFTM/MTFTP download.

Parameters
xferData transfer interface
uriUniform Resource Identifier
Return values
rcReturn status code

Definition at line 1055 of file tftp.c.

1058  {
1059  struct tftp_request *tftp;
1060  int rc;
1061 
1062  /* Sanity checks */
1063  if ( ! uri->host )
1064  return -EINVAL;
1065  if ( ! uri->path )
1066  return -EINVAL;
1067  if ( uri->path[0] != '/' )
1068  return -EINVAL;
1069 
1070  /* Allocate and populate TFTP structure */
1071  tftp = zalloc ( sizeof ( *tftp ) );
1072  if ( ! tftp )
1073  return -ENOMEM;
1074  ref_init ( &tftp->refcnt, tftp_free );
1075  intf_init ( &tftp->xfer, &tftp_xfer_desc, &tftp->refcnt );
1076  intf_init ( &tftp->socket, &tftp_socket_desc, &tftp->refcnt );
1077  intf_init ( &tftp->mc_socket, &tftp_mc_socket_desc, &tftp->refcnt );
1078  timer_init ( &tftp->timer, tftp_timer_expired, &tftp->refcnt );
1079  tftp->uri = uri_get ( uri );
1080  tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1081  tftp->flags = flags;
1082 
1083  /* Open socket */
1084  tftp->port = uri_port ( tftp->uri, default_port );
1085  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1086  goto err;
1087 
1088  /* Open multicast socket */
1089  if ( multicast ) {
1090  if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1091  goto err;
1092  }
1093 
1094  /* Start timer to initiate RRQ */
1095  start_timer_nodelay ( &tftp->timer );
1096 
1097  /* Attach to parent interface, mortalise self, and return */
1098  intf_plug_plug ( &tftp->xfer, xfer );
1099  ref_put ( &tftp->refcnt );
1100  return 0;
1101 
1102  err:
1103  DBGC ( tftp, "TFTP %p could not create request: %s\n",
1104  tftp, strerror ( rc ) );
1105  tftp_done ( tftp, rc );
1106  ref_put ( &tftp->refcnt );
1107  return rc;
1108 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct interface socket
Transport layer interface.
Definition: tftp.c:93
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:99
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:177
static int tftp_reopen(struct tftp_request *tftp)
Reopen TFTP socket.
Definition: tftp.c:201
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
static struct interface_descriptor tftp_xfer_desc
TFTP data transfer interface descriptor.
Definition: tftp.c:1045
#define DBGC(...)
Definition: compiler.h:505
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
struct refcnt refcnt
Reference count.
Definition: tftp.c:86
A TFTP request.
Definition: tftp.c:84
unsigned int blksize
Data block size.
Definition: tftp.c:103
static void tftp_timer_expired(struct retry_timer *timer, int fail)
Handle TFTP retransmission timer expiry.
Definition: tftp.c:481
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct interface_descriptor tftp_socket_desc
TFTP socket interface descriptor.
Definition: tftp.c:995
unsigned int flags
Request flags.
Definition: tftp.c:124
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
static struct interface_descriptor tftp_mc_socket_desc
TFTP multicast socket interface descriptor.
Definition: tftp.c:1004
const char * path
Path.
Definition: uri.h:66
struct interface mc_socket
Multicast transport layer interface.
Definition: tftp.c:95
static void tftp_free(struct refcnt *refcnt)
Free TFTP request.
Definition: tftp.c:166
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:181
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
const char * host
Host name.
Definition: uri.h:62
static int tftp_reopen_mc(struct tftp_request *tftp, struct sockaddr *local)
Reopen TFTP multicast socket.
Definition: tftp.c:235
struct retry_timer timer
Retransmission timer.
Definition: tftp.c:143
unsigned int port
Server port.
Definition: tftp.c:116
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:441
#define TFTP_DEFAULT_BLKSIZE
Default TFTP data block size.
Definition: tftp.h:15
A Uniform Resource Identifier.
Definition: uri.h:50
struct uri * uri
URI being fetched.
Definition: tftp.c:91
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
uint8_t flags
Flags.
Definition: ena.h:18

References tftp_request::blksize, DBGC, EINVAL, ENOMEM, flags, tftp_request::flags, uri::host, intf_init(), intf_plug_plug(), tftp_request::mc_socket, uri::path, tftp_request::port, rc, ref_init, ref_put, tftp_request::refcnt, tftp_request::socket, start_timer_nodelay(), strerror(), TFTP_DEFAULT_BLKSIZE, tftp_done(), tftp_free(), tftp_mc_socket_desc, tftp_reopen(), tftp_reopen_mc(), tftp_socket_desc, tftp_timer_expired(), tftp_xfer_desc, tftp_request::timer, tftp_request::uri, uri_get(), uri_port(), tftp_request::xfer, and zalloc().

Referenced by mtftp_open(), tftm_open(), and tftp_open().

◆ tftp_open()

static int tftp_open ( struct interface xfer,
struct uri uri 
)
static

Initiate TFTP download.

Parameters
xferData transfer interface
uriUniform Resource Identifier
Return values
rcReturn status code

Definition at line 1117 of file tftp.c.

1117  {
1118  return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1120 
1121 }
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
Request blksize and tsize options.
Definition: tftp.c:151
#define TFTP_PORT
Default TFTP server port.
Definition: tftp.h:14
A Uniform Resource Identifier.
Definition: uri.h:50
static int tftp_core_open(struct interface *xfer, struct uri *uri, unsigned int default_port, struct sockaddr *multicast, unsigned int flags)
Initiate TFTP/TFTM/MTFTP download.
Definition: tftp.c:1055
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References NULL, tftp_core_open(), TFTP_FL_RRQ_SIZES, TFTP_PORT, and tftp_request::xfer.

Referenced by pxenv_tftp_open().

◆ tftm_open()

static int tftm_open ( struct interface xfer,
struct uri uri 
)
static

Initiate TFTM download.

Parameters
xferData transfer interface
uriUniform Resource Identifier
Return values
rcReturn status code

Definition at line 1136 of file tftp.c.

1136  {
1137  return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1138  ( TFTP_FL_RRQ_SIZES |
1140 
1141 }
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
Request blksize and tsize options.
Definition: tftp.c:151
#define TFTP_PORT
Default TFTP server port.
Definition: tftp.h:14
Request multicast option.
Definition: tftp.c:153
A Uniform Resource Identifier.
Definition: uri.h:50
static int tftp_core_open(struct interface *xfer, struct uri *uri, unsigned int default_port, struct sockaddr *multicast, unsigned int flags)
Initiate TFTP/TFTM/MTFTP download.
Definition: tftp.c:1055
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References NULL, tftp_core_open(), TFTP_FL_RRQ_MULTICAST, TFTP_FL_RRQ_SIZES, and TFTP_PORT.

◆ mtftp_open()

static int mtftp_open ( struct interface xfer,
struct uri uri 
)
static

Initiate MTFTP download.

Parameters
xferData transfer interface
uriUniform Resource Identifier
Return values
rcReturn status code

Definition at line 1156 of file tftp.c.

1156  {
1157  return tftp_core_open ( xfer, uri, MTFTP_PORT,
1158  ( struct sockaddr * ) &tftp_mtftp_socket,
1160 }
#define MTFTP_PORT
Default MTFTP server port.
Definition: tftp.h:34
Perform MTFTP recovery on timeout.
Definition: tftp.c:155
struct interface xfer
Data transfer interface.
Definition: tftp.c:88
Generalized socket address structure.
Definition: socket.h:96
static struct sockaddr_in tftp_mtftp_socket
MTFTP multicast receive address.
Definition: tftp.c:299
A Uniform Resource Identifier.
Definition: uri.h:50
static int tftp_core_open(struct interface *xfer, struct uri *uri, unsigned int default_port, struct sockaddr *multicast, unsigned int flags)
Initiate TFTP/TFTM/MTFTP download.
Definition: tftp.c:1055

References MTFTP_PORT, tftp_core_open(), TFTP_FL_MTFTP_RECOVERY, and tftp_mtftp_socket.

◆ tftp_apply_settings()

static int tftp_apply_settings ( void  )
static

Apply TFTP configuration settings.

Return values
rcReturn status code

Definition at line 1180 of file tftp.c.

1180  {
1181  static struct in_addr tftp_server = { 0 };
1182  struct in_addr new_tftp_server;
1183  char uri_string[32];
1184  struct uri *uri;
1185 
1186  /* Retrieve TFTP server setting */
1187  fetch_ipv4_setting ( NULL, &next_server_setting, &new_tftp_server );
1188 
1189  /* If TFTP server setting has changed, set the current working
1190  * URI to match. Do it only when the TFTP server has changed
1191  * to try to minimise surprises to the user, who probably
1192  * won't expect the CWURI to change just because they updated
1193  * an unrelated setting and triggered all the settings
1194  * applicators.
1195  */
1196  if ( new_tftp_server.s_addr &&
1197  ( new_tftp_server.s_addr != tftp_server.s_addr ) ) {
1198  DBGC ( &tftp_server, "TFTP server changed %s => ",
1199  inet_ntoa ( tftp_server ) );
1200  DBGC ( &tftp_server, "%s\n", inet_ntoa ( new_tftp_server ) );
1201  snprintf ( uri_string, sizeof ( uri_string ),
1202  "tftp://%s/", inet_ntoa ( new_tftp_server ) );
1203  uri = parse_uri ( uri_string );
1204  if ( ! uri )
1205  return -ENOMEM;
1206  churi ( uri );
1207  uri_put ( uri );
1208  tftp_server = new_tftp_server;
1209  }
1210 
1211  return 0;
1212 }
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:188
int fetch_ipv4_setting(struct settings *settings, const struct setting *setting, struct in_addr *inp)
Fetch value of IPv4 address setting.
Definition: settings.c:916
#define DBGC(...)
Definition: compiler.h:505
#define ENOMEM
Not enough space.
Definition: errno.h:534
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:45
IP address structure.
Definition: in.h:39
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:658
uint32_t s_addr
Definition: in.h:40
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
A Uniform Resource Identifier.
Definition: uri.h:50
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:295

References churi(), DBGC, ENOMEM, fetch_ipv4_setting(), inet_ntoa(), NULL, parse_uri(), in_addr::s_addr, snprintf(), and uri_put().

Variable Documentation

◆ tftp_mtftp_socket

struct sockaddr_in tftp_mtftp_socket
static
Initial value:
= {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl ( 0xefff0101 ),
.sin_port = htons ( 3001 ),
}
#define htonl(value)
Definition: byteswap.h:133
#define htons(value)
Definition: byteswap.h:135
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63

MTFTP multicast receive address.

This is treated as a global configuration parameter.

Definition at line 299 of file tftp.c.

Referenced by mtftp_open(), tftp_set_mtftp_address(), and tftp_set_mtftp_port().

◆ tftp_options

struct tftp_option tftp_options[]
static
Initial value:
= {
{ "blksize", tftp_process_blksize },
{ "tsize", tftp_process_tsize },
{ "multicast", tftp_process_multicast },
{ NULL, NULL }
}
static int tftp_process_multicast(struct tftp_request *tftp, char *value)
Process TFTP "multicast" option.
Definition: tftp.c:590
static int tftp_process_tsize(struct tftp_request *tftp, char *value)
Process TFTP "tsize" option.
Definition: tftp.c:569
static int tftp_process_blksize(struct tftp_request *tftp, char *value)
Process TFTP "blksize" option.
Definition: tftp.c:548
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

Recognised TFTP options.

Definition at line 664 of file tftp.c.

Referenced by tftp_process_option().

◆ tftp_socket_operations

struct interface_operation tftp_socket_operations[]
static
Initial value:
= {
}
A TFTP request.
Definition: tftp.c:84
static int tftp_socket_deliver(struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive new data via socket.
Definition: tftp.c:965
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193

TFTP socket operations.

Definition at line 990 of file tftp.c.

◆ tftp_socket_desc

struct interface_descriptor tftp_socket_desc
static
Initial value:
=
A TFTP request.
Definition: tftp.c:84
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation tftp_socket_operations[]
TFTP socket operations.
Definition: tftp.c:990

TFTP socket interface descriptor.

Definition at line 995 of file tftp.c.

Referenced by tftp_core_open().

◆ tftp_mc_socket_operations

struct interface_operation tftp_mc_socket_operations[]
static
Initial value:
= {
}
A TFTP request.
Definition: tftp.c:84
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
static int tftp_rx(struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive new data.
Definition: tftp.c:902

TFTP multicast socket operations.

Definition at line 999 of file tftp.c.

◆ tftp_mc_socket_desc

struct interface_descriptor tftp_mc_socket_desc
static
Initial value:
=
A TFTP request.
Definition: tftp.c:84
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation tftp_mc_socket_operations[]
TFTP multicast socket operations.
Definition: tftp.c:999

TFTP multicast socket interface descriptor.

Definition at line 1004 of file tftp.c.

Referenced by tftp_core_open().

◆ tftp_xfer_operations

struct interface_operation tftp_xfer_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
A TFTP request.
Definition: tftp.c:84
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:115
static void tftp_close(struct tftp_request *tftp, int rc)
Terminate download.
Definition: tftp.c:1029
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
static size_t tftp_xfer_window(struct tftp_request *tftp)
Check flow control window.
Definition: tftp.c:1013

TFTP data transfer interface operations.

Definition at line 1039 of file tftp.c.

◆ tftp_xfer_desc

struct interface_descriptor tftp_xfer_desc
static
Initial value:
=
A TFTP request.
Definition: tftp.c:84
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation tftp_xfer_operations[]
TFTP data transfer interface operations.
Definition: tftp.c:1039

TFTP data transfer interface descriptor.

Definition at line 1045 of file tftp.c.

Referenced by tftp_core_open().

◆ __uri_opener

struct uri_opener mtftp_uri_opener __uri_opener
Initial value:
= {
.scheme = "tftp",
.open = tftp_open,
}
static int tftp_open(struct interface *xfer, struct uri *uri)
Initiate TFTP download.
Definition: tftp.c:1117

TFTP URI opener.

MTFTP URI opener.

TFTM URI opener.

Definition at line 1124 of file tftp.c.

◆ __settings_applicator

struct settings_applicator tftp_settings_applicator __settings_applicator
Initial value:
= {
}
static int tftp_apply_settings(void)
Apply TFTP configuration settings.
Definition: tftp.c:1180

TFTP settings applicator.

Definition at line 1215 of file tftp.c.