iPXE
tftp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <byteswap.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <ipxe/refcnt.h>
36 #include <ipxe/iobuf.h>
37 #include <ipxe/xfer.h>
38 #include <ipxe/open.h>
39 #include <ipxe/uri.h>
40 #include <ipxe/tcpip.h>
41 #include <ipxe/retry.h>
42 #include <ipxe/features.h>
43 #include <ipxe/bitmap.h>
44 #include <ipxe/settings.h>
45 #include <ipxe/dhcp.h>
46 #include <ipxe/uri.h>
47 #include <ipxe/profile.h>
48 #include <ipxe/errortab.h>
49 #include <ipxe/tftp.h>
50 
51 /** @file
52  *
53  * TFTP protocol
54  *
55  */
56 
58 
59 /* TFTP-specific error codes */
60 #define EINVAL_BLKSIZE __einfo_error ( EINFO_EINVAL_BLKSIZE )
61 #define EINFO_EINVAL_BLKSIZE __einfo_uniqify \
62  ( EINFO_EINVAL, 0x01, "Invalid blksize" )
63 #define EINVAL_TSIZE __einfo_error ( EINFO_EINVAL_TSIZE )
64 #define EINFO_EINVAL_TSIZE __einfo_uniqify \
65  ( EINFO_EINVAL, 0x02, "Invalid tsize" )
66 #define EINVAL_MC_NO_PORT __einfo_error ( EINFO_EINVAL_MC_NO_PORT )
67 #define EINFO_EINVAL_MC_NO_PORT __einfo_uniqify \
68  ( EINFO_EINVAL, 0x03, "Missing multicast port" )
69 #define EINVAL_MC_NO_MC __einfo_error ( EINFO_EINVAL_MC_NO_MC )
70 #define EINFO_EINVAL_MC_NO_MC __einfo_uniqify \
71  ( EINFO_EINVAL, 0x04, "Missing multicast mc" )
72 #define EINVAL_MC_INVALID_MC __einfo_error ( EINFO_EINVAL_MC_INVALID_MC )
73 #define EINFO_EINVAL_MC_INVALID_MC __einfo_uniqify \
74  ( EINFO_EINVAL, 0x05, "Missing multicast IP" )
75 #define EINVAL_MC_INVALID_IP __einfo_error ( EINFO_EINVAL_MC_INVALID_IP )
76 #define EINFO_EINVAL_MC_INVALID_IP __einfo_uniqify \
77  ( EINFO_EINVAL, 0x06, "Invalid multicast IP" )
78 #define EINVAL_MC_INVALID_PORT __einfo_error ( EINFO_EINVAL_MC_INVALID_PORT )
79 #define EINFO_EINVAL_MC_INVALID_PORT __einfo_uniqify \
80  ( EINFO_EINVAL, 0x07, "Invalid multicast port" )
81 #define ENOENT_NOT_FOUND __einfo_error ( EINFO_ENOENT_NOT_FOUND )
82 #define EINFO_ENOENT_NOT_FOUND __einfo_uniqify \
83  ( EINFO_ENOENT, 0x01, "Not found" )
84 
85 /**
86  * A TFTP request
87  *
88  * This data structure holds the state for an ongoing TFTP transfer.
89  */
90 struct tftp_request {
91  /** Reference count */
92  struct refcnt refcnt;
93  /** Data transfer interface */
94  struct interface xfer;
95 
96  /** URI being fetched */
97  struct uri *uri;
98  /** Transport layer interface */
99  struct interface socket;
100  /** Multicast transport layer interface */
102 
103  /** Data block size
104  *
105  * This is the "blksize" option negotiated with the TFTP
106  * server. (If the TFTP server does not support TFTP options,
107  * this will default to 512).
108  */
109  unsigned int blksize;
110  /** File size
111  *
112  * This is the value returned in the "tsize" option from the
113  * TFTP server. If the TFTP server does not support the
114  * "tsize" option, this value will be zero.
115  */
116  unsigned long tsize;
117 
118  /** Server port
119  *
120  * This is the port to which RRQ packets are sent.
121  */
122  unsigned int port;
123  /** Peer address
124  *
125  * The peer address is determined by the first response
126  * received to the TFTP RRQ.
127  */
129  /** Request flags */
130  unsigned int flags;
131  /** MTFTP timeout count */
132  unsigned int mtftp_timeouts;
133 
134  /** Block bitmap */
135  struct bitmap bitmap;
136  /** Maximum known length
137  *
138  * We don't always know the file length in advance. In
139  * particular, if the TFTP server doesn't support the tsize
140  * option, or we are using MTFTP, then we don't know the file
141  * length until we see the end-of-file block (which, in the
142  * case of MTFTP, may not be the last block we see).
143  *
144  * This value is updated whenever we obtain information about
145  * the file length.
146  */
147  size_t filesize;
148  /** Retransmission timer */
150 };
151 
152 /** TFTP request flags */
153 enum {
154  /** Send ACK packets */
156  /** Request blksize and tsize options */
158  /** Request multicast option */
160  /** Perform MTFTP recovery on timeout */
162 };
163 
164 /** Maximum number of MTFTP open requests before falling back to TFTP */
165 #define MTFTP_MAX_TIMEOUTS 3
166 
167 /** Client profiler */
168 static struct profiler tftp_client_profiler __profiler =
169  { .name = "tftp.client" };
170 
171 /** Server profiler */
172 static struct profiler tftp_server_profiler __profiler =
173  { .name = "tftp.server" };
174 
175 /** Human-readable error messages */
176 struct errortab tftp_errors[] __errortab = {
178 };
179 
180 /**
181  * Free TFTP request
182  *
183  * @v refcnt Reference counter
184  */
185 static void tftp_free ( struct refcnt *refcnt ) {
186  struct tftp_request *tftp =
187  container_of ( refcnt, struct tftp_request, refcnt );
188 
189  uri_put ( tftp->uri );
190  bitmap_free ( &tftp->bitmap );
191  free ( tftp );
192 }
193 
194 /**
195  * Mark TFTP request as complete
196  *
197  * @v tftp TFTP connection
198  * @v rc Return status code
199  */
200 static void tftp_done ( struct tftp_request *tftp, int rc ) {
201 
202  DBGC ( tftp, "TFTP %p finished with status %d (%s)\n",
203  tftp, rc, strerror ( rc ) );
204 
205  /* Stop the retry timer */
206  stop_timer ( &tftp->timer );
207 
208  /* Close all data transfer interfaces */
209  intf_shutdown ( &tftp->socket, rc );
210  intf_shutdown ( &tftp->mc_socket, rc );
211  intf_shutdown ( &tftp->xfer, rc );
212 }
213 
214 /**
215  * Reopen TFTP socket
216  *
217  * @v tftp TFTP connection
218  * @ret rc Return status code
219  */
220 static int tftp_reopen ( struct tftp_request *tftp ) {
221  struct sockaddr_tcpip server;
222  int rc;
223 
224  /* Close socket */
225  intf_restart ( &tftp->socket, 0 );
226 
227  /* Disable ACK sending. */
228  tftp->flags &= ~TFTP_FL_SEND_ACK;
229 
230  /* Reset peer address */
231  memset ( &tftp->peer, 0, sizeof ( tftp->peer ) );
232 
233  /* Open socket */
234  memset ( &server, 0, sizeof ( server ) );
235  server.st_port = htons ( tftp->port );
236  if ( ( rc = xfer_open_named_socket ( &tftp->socket, SOCK_DGRAM,
237  ( struct sockaddr * ) &server,
238  tftp->uri->host, NULL ) ) != 0 ) {
239  DBGC ( tftp, "TFTP %p could not open socket: %s\n",
240  tftp, strerror ( rc ) );
241  return rc;
242  }
243 
244  return 0;
245 }
246 
247 /**
248  * Reopen TFTP multicast socket
249  *
250  * @v tftp TFTP connection
251  * @v local Local socket address
252  * @ret rc Return status code
253  */
254 static int tftp_reopen_mc ( struct tftp_request *tftp,
255  struct sockaddr *local ) {
256  int rc;
257 
258  /* Close multicast socket */
259  intf_restart ( &tftp->mc_socket, 0 );
260 
261  /* Open multicast socket. We never send via this socket, so
262  * use the local address as the peer address (since the peer
263  * address cannot be NULL).
264  */
265  if ( ( rc = xfer_open_socket ( &tftp->mc_socket, SOCK_DGRAM,
266  local, local ) ) != 0 ) {
267  DBGC ( tftp, "TFTP %p could not open multicast "
268  "socket: %s\n", tftp, strerror ( rc ) );
269  return rc;
270  }
271 
272  return 0;
273 }
274 
275 /**
276  * Presize TFTP receive buffers and block bitmap
277  *
278  * @v tftp TFTP connection
279  * @v filesize Known minimum file size
280  * @ret rc Return status code
281  */
282 static int tftp_presize ( struct tftp_request *tftp, size_t filesize ) {
283  unsigned int num_blocks;
284  int rc;
285 
286  /* Do nothing if we are already large enough */
287  if ( filesize <= tftp->filesize )
288  return 0;
289 
290  /* Record filesize */
291  tftp->filesize = filesize;
292 
293  /* Notify recipient of file size */
294  xfer_seek ( &tftp->xfer, filesize );
295  xfer_seek ( &tftp->xfer, 0 );
296 
297  /* Calculate expected number of blocks. Note that files whose
298  * length is an exact multiple of the blocksize will have a
299  * trailing zero-length block, which must be included.
300  */
301  if ( tftp->blksize == 0 )
302  return -EINVAL;
303  num_blocks = ( ( filesize / tftp->blksize ) + 1 );
304  if ( ( rc = bitmap_resize ( &tftp->bitmap, num_blocks ) ) != 0 ) {
305  DBGC ( tftp, "TFTP %p could not resize bitmap to %d blocks: "
306  "%s\n", tftp, num_blocks, strerror ( rc ) );
307  return rc;
308  }
309 
310  return 0;
311 }
312 
313 /**
314  * MTFTP multicast receive address
315  *
316  * This is treated as a global configuration parameter.
317  */
319  .sin_family = AF_INET,
320  .sin_addr.s_addr = htonl ( 0xefff0101 ),
321  .sin_port = htons ( 3001 ),
322 };
323 
324 /**
325  * Set MTFTP multicast address
326  *
327  * @v address Multicast IPv4 address
328  */
331 }
332 
333 /**
334  * Set MTFTP multicast port
335  *
336  * @v port Multicast port
337  */
338 void tftp_set_mtftp_port ( unsigned int port ) {
340 }
341 
342 /**
343  * Transmit RRQ
344  *
345  * @v tftp TFTP connection
346  * @ret rc Return status code
347  */
348 static int tftp_send_rrq ( struct tftp_request *tftp ) {
349  const char *path = ( tftp->uri->path + 1 /* skip '/' */ );
350  struct tftp_rrq *rrq;
351  size_t len;
352  struct io_buffer *iobuf;
353  size_t blksize;
354 
355  DBGC ( tftp, "TFTP %p requesting \"%s\"\n", tftp, path );
356 
357  /* Allocate buffer */
358  len = ( sizeof ( *rrq ) + strlen ( path ) + 1 /* NUL */
359  + 5 + 1 /* "octet" + NUL */
360  + 7 + 1 + 5 + 1 /* "blksize" + NUL + ddddd + NUL */
361  + 5 + 1 + 1 + 1 /* "tsize" + NUL + "0" + NUL */
362  + 9 + 1 + 1 /* "multicast" + NUL + NUL */ );
363  iobuf = xfer_alloc_iob ( &tftp->socket, len );
364  if ( ! iobuf )
365  return -ENOMEM;
366 
367  /* Determine block size */
368  blksize = xfer_window ( &tftp->xfer );
369  if ( blksize > TFTP_MAX_BLKSIZE )
371 
372  /* Build request */
373  rrq = iob_put ( iobuf, sizeof ( *rrq ) );
374  rrq->opcode = htons ( TFTP_RRQ );
375  iob_put ( iobuf, snprintf ( iobuf->tail, iob_tailroom ( iobuf ),
376  "%s%coctet", path, 0 ) + 1 );
377  if ( tftp->flags & TFTP_FL_RRQ_SIZES ) {
378  iob_put ( iobuf, snprintf ( iobuf->tail,
379  iob_tailroom ( iobuf ),
380  "blksize%c%zd%ctsize%c0",
381  0, blksize, 0, 0 ) + 1 );
382  }
383  if ( tftp->flags & TFTP_FL_RRQ_MULTICAST ) {
384  iob_put ( iobuf, snprintf ( iobuf->tail,
385  iob_tailroom ( iobuf ),
386  "multicast%c", 0 ) + 1 );
387  }
388 
389  /* RRQ always goes to the address specified in the initial
390  * xfer_open() call
391  */
392  return xfer_deliver_iob ( &tftp->socket, iobuf );
393 }
394 
395 /**
396  * Transmit ACK
397  *
398  * @v tftp TFTP connection
399  * @ret rc Return status code
400  */
401 static int tftp_send_ack ( struct tftp_request *tftp ) {
402  struct tftp_ack *ack;
403  struct io_buffer *iobuf;
404  struct xfer_metadata meta = {
405  .dest = ( struct sockaddr * ) &tftp->peer,
406  };
407  unsigned int block;
408 
409  /* Determine next required block number */
410  block = bitmap_first_gap ( &tftp->bitmap );
411  DBGC2 ( tftp, "TFTP %p sending ACK for block %d\n", tftp, block );
412 
413  /* Allocate buffer */
414  iobuf = xfer_alloc_iob ( &tftp->socket, sizeof ( *ack ) );
415  if ( ! iobuf )
416  return -ENOMEM;
417 
418  /* Build ACK */
419  ack = iob_put ( iobuf, sizeof ( *ack ) );
420  ack->opcode = htons ( TFTP_ACK );
421  ack->block = htons ( block );
422 
423  /* ACK always goes to the peer recorded from the RRQ response */
424  return xfer_deliver ( &tftp->socket, iobuf, &meta );
425 }
426 
427 /**
428  * Transmit ERROR (Abort)
429  *
430  * @v tftp TFTP connection
431  * @v errcode TFTP error code
432  * @v errmsg Error message string
433  * @ret rc Return status code
434  */
435 static int tftp_send_error ( struct tftp_request *tftp, int errcode,
436  const char *errmsg ) {
437  struct tftp_error *err;
438  struct io_buffer *iobuf;
439  struct xfer_metadata meta = {
440  .dest = ( struct sockaddr * ) &tftp->peer,
441  };
442  size_t msglen;
443 
444  DBGC2 ( tftp, "TFTP %p sending ERROR %d: %s\n", tftp, errcode,
445  errmsg );
446 
447  /* Allocate buffer */
448  msglen = sizeof ( *err ) + strlen ( errmsg ) + 1 /* NUL */;
449  iobuf = xfer_alloc_iob ( &tftp->socket, msglen );
450  if ( ! iobuf )
451  return -ENOMEM;
452 
453  /* Build ERROR */
454  err = iob_put ( iobuf, msglen );
455  err->opcode = htons ( TFTP_ERROR );
456  err->errcode = htons ( errcode );
457  strcpy ( err->errmsg, errmsg );
458 
459  /* ERR always goes to the peer recorded from the RRQ response */
460  return xfer_deliver ( &tftp->socket, iobuf, &meta );
461 }
462 
463 /**
464  * Transmit next relevant packet
465  *
466  * @v tftp TFTP connection
467  * @ret rc Return status code
468  */
469 static int tftp_send_packet ( struct tftp_request *tftp ) {
470 
471  /* Update retransmission timer. While name resolution takes place the
472  * window is zero. Avoid unnecessary delay after name resolution
473  * completes by retrying immediately.
474  */
475  stop_timer ( &tftp->timer );
476  if ( xfer_window ( &tftp->socket ) ) {
477  start_timer ( &tftp->timer );
478  } else {
479  start_timer_nodelay ( &tftp->timer );
480  }
481 
482  /* Send RRQ or ACK as appropriate */
483  if ( ! tftp->peer.st_family ) {
484  return tftp_send_rrq ( tftp );
485  } else {
486  if ( tftp->flags & TFTP_FL_SEND_ACK ) {
487  return tftp_send_ack ( tftp );
488  } else {
489  return 0;
490  }
491  }
492 }
493 
494 /**
495  * Handle TFTP retransmission timer expiry
496  *
497  * @v timer Retry timer
498  * @v fail Failure indicator
499  */
500 static void tftp_timer_expired ( struct retry_timer *timer, int fail ) {
501  struct tftp_request *tftp =
502  container_of ( timer, struct tftp_request, timer );
503  int rc;
504 
505  /* If we are doing MTFTP, attempt the various recovery strategies */
506  if ( tftp->flags & TFTP_FL_MTFTP_RECOVERY ) {
507  if ( tftp->peer.st_family ) {
508  /* If we have received any response from the server,
509  * try resending the RRQ to restart the download.
510  */
511  DBGC ( tftp, "TFTP %p attempting reopen\n", tftp );
512  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
513  goto err;
514  } else {
515  /* Fall back to plain TFTP after several attempts */
516  tftp->mtftp_timeouts++;
517  DBGC ( tftp, "TFTP %p timeout %d waiting for MTFTP "
518  "open\n", tftp, tftp->mtftp_timeouts );
519 
520  if ( tftp->mtftp_timeouts > MTFTP_MAX_TIMEOUTS ) {
521  DBGC ( tftp, "TFTP %p falling back to plain "
522  "TFTP\n", tftp );
523  tftp->flags = TFTP_FL_RRQ_SIZES;
524 
525  /* Close multicast socket */
526  intf_restart ( &tftp->mc_socket, 0 );
527 
528  /* Reset retry timer */
529  start_timer_nodelay ( &tftp->timer );
530 
531  /* The blocksize may change: discard
532  * the block bitmap
533  */
534  bitmap_free ( &tftp->bitmap );
535  memset ( &tftp->bitmap, 0,
536  sizeof ( tftp->bitmap ) );
537 
538  /* Reopen on standard TFTP port */
539  tftp->port = TFTP_PORT;
540  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
541  goto err;
542  }
543  }
544  } else {
545  /* Not doing MTFTP (or have fallen back to plain
546  * TFTP); fail as per normal.
547  */
548  if ( fail ) {
549  rc = -ETIMEDOUT;
550  goto err;
551  }
552  }
553  tftp_send_packet ( tftp );
554  return;
555 
556  err:
557  tftp_done ( tftp, rc );
558 }
559 
560 /**
561  * Process TFTP "blksize" option
562  *
563  * @v tftp TFTP connection
564  * @v value Option value
565  * @ret rc Return status code
566  */
567 static int tftp_process_blksize ( struct tftp_request *tftp, char *value ) {
568  char *end;
569 
570  tftp->blksize = strtoul ( value, &end, 10 );
571  if ( *end ) {
572  DBGC ( tftp, "TFTP %p got invalid blksize \"%s\"\n",
573  tftp, value );
574  return -EINVAL_BLKSIZE;
575  }
576  DBGC ( tftp, "TFTP %p blksize=%d\n", tftp, tftp->blksize );
577 
578  return 0;
579 }
580 
581 /**
582  * Process TFTP "tsize" option
583  *
584  * @v tftp TFTP connection
585  * @v value Option value
586  * @ret rc Return status code
587  */
588 static int tftp_process_tsize ( struct tftp_request *tftp, char *value ) {
589  char *end;
590 
591  tftp->tsize = strtoul ( value, &end, 10 );
592  if ( *end ) {
593  DBGC ( tftp, "TFTP %p got invalid tsize \"%s\"\n",
594  tftp, value );
595  return -EINVAL_TSIZE;
596  }
597  DBGC ( tftp, "TFTP %p tsize=%ld\n", tftp, tftp->tsize );
598 
599  return 0;
600 }
601 
602 /**
603  * Process TFTP "multicast" option
604  *
605  * @v tftp TFTP connection
606  * @v value Option value
607  * @ret rc Return status code
608  */
609 static int tftp_process_multicast ( struct tftp_request *tftp, char *value ) {
610  union {
611  struct sockaddr sa;
612  struct sockaddr_in sin;
613  } socket;
614  char *addr;
615  char *port;
616  char *port_end;
617  char *mc;
618  char *mc_end;
619  int rc;
620 
621  /* Split value into "addr,port,mc" fields */
622  addr = value;
623  port = strchr ( addr, ',' );
624  if ( ! port ) {
625  DBGC ( tftp, "TFTP %p multicast missing port,mc\n", tftp );
626  return -EINVAL_MC_NO_PORT;
627  }
628  *(port++) = '\0';
629  mc = strchr ( port, ',' );
630  if ( ! mc ) {
631  DBGC ( tftp, "TFTP %p multicast missing mc\n", tftp );
632  return -EINVAL_MC_NO_MC;
633  }
634  *(mc++) = '\0';
635 
636  /* Parse parameters */
637  if ( strtoul ( mc, &mc_end, 0 ) == 0 )
638  tftp->flags &= ~TFTP_FL_SEND_ACK;
639  if ( *mc_end ) {
640  DBGC ( tftp, "TFTP %p multicast invalid mc %s\n", tftp, mc );
641  return -EINVAL_MC_INVALID_MC;
642  }
643  DBGC ( tftp, "TFTP %p is%s the master client\n",
644  tftp, ( ( tftp->flags & TFTP_FL_SEND_ACK ) ? "" : " not" ) );
645  if ( *addr && *port ) {
646  socket.sin.sin_family = AF_INET;
647  if ( inet_aton ( addr, &socket.sin.sin_addr ) == 0 ) {
648  DBGC ( tftp, "TFTP %p multicast invalid IP address "
649  "%s\n", tftp, addr );
650  return -EINVAL_MC_INVALID_IP;
651  }
652  DBGC ( tftp, "TFTP %p multicast IP address %s\n",
653  tftp, inet_ntoa ( socket.sin.sin_addr ) );
654  socket.sin.sin_port = htons ( strtoul ( port, &port_end, 0 ) );
655  if ( *port_end ) {
656  DBGC ( tftp, "TFTP %p multicast invalid port %s\n",
657  tftp, port );
658  return -EINVAL_MC_INVALID_PORT;
659  }
660  DBGC ( tftp, "TFTP %p multicast port %d\n",
661  tftp, ntohs ( socket.sin.sin_port ) );
662  if ( ( rc = tftp_reopen_mc ( tftp, &socket.sa ) ) != 0 )
663  return rc;
664  }
665 
666  return 0;
667 }
668 
669 /** A TFTP option */
670 struct tftp_option {
671  /** Option name */
672  const char *name;
673  /** Option processor
674  *
675  * @v tftp TFTP connection
676  * @v value Option value
677  * @ret rc Return status code
678  */
679  int ( * process ) ( struct tftp_request *tftp, char *value );
680 };
681 
682 /** Recognised TFTP options */
683 static struct tftp_option tftp_options[] = {
684  { "blksize", tftp_process_blksize },
685  { "tsize", tftp_process_tsize },
686  { "multicast", tftp_process_multicast },
687  { NULL, NULL }
688 };
689 
690 /**
691  * Process TFTP option
692  *
693  * @v tftp TFTP connection
694  * @v name Option name
695  * @v value Option value
696  * @ret rc Return status code
697  */
698 static int tftp_process_option ( struct tftp_request *tftp,
699  const char *name, char *value ) {
700  struct tftp_option *option;
701 
702  for ( option = tftp_options ; option->name ; option++ ) {
703  if ( strcasecmp ( name, option->name ) == 0 )
704  return option->process ( tftp, value );
705  }
706 
707  DBGC ( tftp, "TFTP %p received unknown option \"%s\" = \"%s\"\n",
708  tftp, name, value );
709 
710  /* Unknown options should be silently ignored */
711  return 0;
712 }
713 
714 /**
715  * Receive OACK
716  *
717  * @v tftp TFTP connection
718  * @v buf Temporary data buffer
719  * @v len Length of temporary data buffer
720  * @ret rc Return status code
721  */
722 static int tftp_rx_oack ( struct tftp_request *tftp, void *buf, size_t len ) {
723  struct tftp_oack *oack = buf;
724  char *end = buf + len;
725  char *name;
726  char *value;
727  char *next;
728  int rc = 0;
729 
730  /* Sanity check */
731  if ( len < sizeof ( *oack ) ) {
732  DBGC ( tftp, "TFTP %p received underlength OACK packet "
733  "length %zd\n", tftp, len );
734  rc = -EINVAL;
735  goto done;
736  }
737 
738  /* Process each option in turn */
739  for ( name = oack->data ; name < end ; name = next ) {
740 
741  /* Parse option name and value
742  *
743  * We treat parsing errors as non-fatal, because there
744  * exists at least one TFTP server (IBM Tivoli PXE
745  * Server 5.1.0.3) that has been observed to send
746  * malformed OACKs containing trailing garbage bytes.
747  */
748  value = ( name + strnlen ( name, ( end - name ) ) + 1 );
749  if ( value > end ) {
750  DBGC ( tftp, "TFTP %p received OACK with malformed "
751  "option name:\n", tftp );
752  DBGC_HD ( tftp, oack, len );
753  break;
754  }
755  if ( value == end ) {
756  DBGC ( tftp, "TFTP %p received OACK missing value "
757  "for option \"%s\"\n", tftp, name );
758  DBGC_HD ( tftp, oack, len );
759  break;
760  }
761  next = ( value + strnlen ( value, ( end - value ) ) + 1 );
762  if ( next > end ) {
763  DBGC ( tftp, "TFTP %p received OACK with malformed "
764  "value for option \"%s\":\n", tftp, name );
765  DBGC_HD ( tftp, oack, len );
766  break;
767  }
768 
769  /* Process option */
770  if ( ( rc = tftp_process_option ( tftp, name, value ) ) != 0 )
771  goto done;
772  }
773 
774  /* Process tsize information, if available */
775  if ( tftp->tsize ) {
776  if ( ( rc = tftp_presize ( tftp, tftp->tsize ) ) != 0 )
777  goto done;
778  }
779 
780  /* Request next data block */
781  tftp_send_packet ( tftp );
782 
783  done:
784  if ( rc )
785  tftp_done ( tftp, rc );
786  return rc;
787 }
788 
789 /**
790  * Receive DATA
791  *
792  * @v tftp TFTP connection
793  * @v iobuf I/O buffer
794  * @ret rc Return status code
795  *
796  * Takes ownership of I/O buffer.
797  */
798 static int tftp_rx_data ( struct tftp_request *tftp,
799  struct io_buffer *iobuf ) {
800  struct tftp_data *data = iobuf->data;
801  struct xfer_metadata meta;
802  unsigned int block;
803  off_t offset;
804  size_t data_len;
805  int rc;
806 
807  /* Sanity check */
808  if ( iob_len ( iobuf ) < sizeof ( *data ) ) {
809  DBGC ( tftp, "TFTP %p received underlength DATA packet "
810  "length %zd\n", tftp, iob_len ( iobuf ) );
811  rc = -EINVAL;
812  goto done;
813  }
814 
815  /* Calculate block number */
816  block = ( ( bitmap_first_gap ( &tftp->bitmap ) + 1 ) & ~0xffff );
817  if ( data->block == 0 && block == 0 ) {
818  DBGC ( tftp, "TFTP %p received data block 0\n", tftp );
819  rc = -EINVAL;
820  goto done;
821  }
822  block += ( ntohs ( data->block ) - 1 );
823 
824  /* Stop profiling server turnaround if applicable */
825  if ( block )
826  profile_stop ( &tftp_server_profiler );
827 
828  /* Extract data */
829  offset = ( block * tftp->blksize );
830  iob_pull ( iobuf, sizeof ( *data ) );
831  data_len = iob_len ( iobuf );
832  if ( data_len > tftp->blksize ) {
833  DBGC ( tftp, "TFTP %p received overlength DATA packet "
834  "length %zd\n", tftp, data_len );
835  rc = -EINVAL;
836  goto done;
837  }
838 
839  /* Deliver data */
840  memset ( &meta, 0, sizeof ( meta ) );
841  meta.flags = XFER_FL_ABS_OFFSET;
842  meta.offset = offset;
843  if ( ( rc = xfer_deliver ( &tftp->xfer, iob_disown ( iobuf ),
844  &meta ) ) != 0 ) {
845  DBGC ( tftp, "TFTP %p could not deliver data: %s\n",
846  tftp, strerror ( rc ) );
847  goto done;
848  }
849 
850  /* Ensure block bitmap is ready */
851  if ( ( rc = tftp_presize ( tftp, ( offset + data_len ) ) ) != 0 )
852  goto done;
853 
854  /* Mark block as received */
855  bitmap_set ( &tftp->bitmap, block );
856 
857  /* Acknowledge block */
858  tftp_send_packet ( tftp );
859 
860  /* Stop profiling client turnaround */
861  profile_stop ( &tftp_client_profiler );
862 
863  /* Start profiling server turnaround */
864  profile_start ( &tftp_server_profiler );
865 
866  /* If all blocks have been received, finish. */
867  if ( bitmap_full ( &tftp->bitmap ) )
868  tftp_done ( tftp, 0 );
869 
870  done:
871  free_iob ( iobuf );
872  if ( rc )
873  tftp_done ( tftp, rc );
874  return rc;
875 }
876 
877 /**
878  * Convert TFTP error code to return status code
879  *
880  * @v errcode TFTP error code
881  * @ret rc Return status code
882  */
883 static int tftp_errcode_to_rc ( unsigned int errcode ) {
884  switch ( errcode ) {
886  case TFTP_ERR_ACCESS_DENIED: return -EACCES;
887  case TFTP_ERR_ILLEGAL_OP: return -ENOTTY;
888  default: return -ENOTSUP;
889  }
890 }
891 
892 /**
893  * Receive ERROR
894  *
895  * @v tftp TFTP connection
896  * @v buf Temporary data buffer
897  * @v len Length of temporary data buffer
898  * @ret rc Return status code
899  */
900 static int tftp_rx_error ( struct tftp_request *tftp, void *buf, size_t len ) {
901  struct tftp_error *error = buf;
902  int rc;
903 
904  /* Sanity check */
905  if ( len < sizeof ( *error ) ) {
906  DBGC ( tftp, "TFTP %p received underlength ERROR packet "
907  "length %zd\n", tftp, len );
908  return -EINVAL;
909  }
910 
911  DBGC ( tftp, "TFTP %p received ERROR packet with code %d, message "
912  "\"%s\"\n", tftp, ntohs ( error->errcode ), error->errmsg );
913 
914  /* Determine final operation result */
915  rc = tftp_errcode_to_rc ( ntohs ( error->errcode ) );
916 
917  /* Close TFTP request */
918  tftp_done ( tftp, rc );
919 
920  return 0;
921 }
922 
923 /**
924  * Receive new data
925  *
926  * @v tftp TFTP connection
927  * @v iobuf I/O buffer
928  * @v meta Transfer metadata
929  * @ret rc Return status code
930  */
931 static int tftp_rx ( struct tftp_request *tftp,
932  struct io_buffer *iobuf,
933  struct xfer_metadata *meta ) {
934  struct sockaddr_tcpip *st_src;
935  struct tftp_common *common = iobuf->data;
936  size_t len = iob_len ( iobuf );
937  int rc = -EINVAL;
938 
939  /* Start profiling client turnaround */
940  profile_start ( &tftp_client_profiler );
941 
942  /* Sanity checks */
943  if ( len < sizeof ( *common ) ) {
944  DBGC ( tftp, "TFTP %p received underlength packet length "
945  "%zd\n", tftp, len );
946  goto done;
947  }
948  if ( ! meta->src ) {
949  DBGC ( tftp, "TFTP %p received packet without source port\n",
950  tftp );
951  goto done;
952  }
953 
954  /* Filter by TID. Set TID on first response received */
955  st_src = ( struct sockaddr_tcpip * ) meta->src;
956  if ( ! tftp->peer.st_family ) {
957  memcpy ( &tftp->peer, st_src, sizeof ( tftp->peer ) );
958  DBGC ( tftp, "TFTP %p using remote port %d\n", tftp,
959  ntohs ( tftp->peer.st_port ) );
960  } else if ( memcmp ( &tftp->peer, st_src,
961  sizeof ( tftp->peer ) ) != 0 ) {
962  DBGC ( tftp, "TFTP %p received packet from wrong source (got "
963  "%d, wanted %d)\n", tftp, ntohs ( st_src->st_port ),
964  ntohs ( tftp->peer.st_port ) );
965  goto done;
966  }
967 
968  switch ( common->opcode ) {
969  case htons ( TFTP_OACK ):
970  rc = tftp_rx_oack ( tftp, iobuf->data, len );
971  break;
972  case htons ( TFTP_DATA ):
973  rc = tftp_rx_data ( tftp, iob_disown ( iobuf ) );
974  break;
975  case htons ( TFTP_ERROR ):
976  rc = tftp_rx_error ( tftp, iobuf->data, len );
977  break;
978  default:
979  DBGC ( tftp, "TFTP %p received strange packet type %d\n",
980  tftp, ntohs ( common->opcode ) );
981  break;
982  };
983 
984  done:
985  free_iob ( iobuf );
986  return rc;
987 }
988 
989 /**
990  * Receive new data via socket
991  *
992  * @v tftp TFTP connection
993  * @v iobuf I/O buffer
994  * @v meta Transfer metadata
995  * @ret rc Return status code
996  */
997 static int tftp_socket_deliver ( struct tftp_request *tftp,
998  struct io_buffer *iobuf,
999  struct xfer_metadata *meta ) {
1000 
1001  /* Enable sending ACKs when we receive a unicast packet. This
1002  * covers three cases:
1003  *
1004  * 1. Standard TFTP; we should always send ACKs, and will
1005  * always receive a unicast packet before we need to send the
1006  * first ACK.
1007  *
1008  * 2. RFC2090 multicast TFTP; the only unicast packets we will
1009  * receive are the OACKs; enable sending ACKs here (before
1010  * processing the OACK) and disable it when processing the
1011  * multicast option if we are not the master client.
1012  *
1013  * 3. MTFTP; receiving a unicast datagram indicates that we
1014  * are the "master client" and should send ACKs.
1015  */
1016  tftp->flags |= TFTP_FL_SEND_ACK;
1017 
1018  return tftp_rx ( tftp, iobuf, meta );
1019 }
1020 
1021 /** TFTP socket operations */
1024 };
1025 
1026 /** TFTP socket interface descriptor */
1028  INTF_DESC ( struct tftp_request, socket, tftp_socket_operations );
1029 
1030 /** TFTP multicast socket operations */
1032  INTF_OP ( xfer_deliver, struct tftp_request *, tftp_rx ),
1033 };
1034 
1035 /** TFTP multicast socket interface descriptor */
1037  INTF_DESC ( struct tftp_request, mc_socket, tftp_mc_socket_operations );
1038 
1039 /**
1040  * Check flow control window
1041  *
1042  * @v tftp TFTP connection
1043  * @ret len Length of window
1044  */
1045 static size_t tftp_xfer_window ( struct tftp_request *tftp ) {
1046 
1047  /* We abuse this data-xfer method to convey the blocksize to
1048  * the caller. This really should be done using some kind of
1049  * stat() method, but we don't yet have the facility to do
1050  * that.
1051  */
1052  return tftp->blksize;
1053 }
1054 
1055 /**
1056  * Terminate download
1057  *
1058  * @v tftp TFTP connection
1059  * @v rc Reason for close
1060  */
1061 static void tftp_close ( struct tftp_request *tftp, int rc ) {
1062 
1063  /* Abort download */
1064  tftp_send_error ( tftp, 0, "TFTP Aborted" );
1065 
1066  /* Close TFTP request */
1067  tftp_done ( tftp, rc );
1068 }
1069 
1070 /** TFTP data transfer interface operations */
1073  INTF_OP ( intf_close, struct tftp_request *, tftp_close ),
1074 };
1075 
1076 /** TFTP data transfer interface descriptor */
1078  INTF_DESC ( struct tftp_request, xfer, tftp_xfer_operations );
1079 
1080 /**
1081  * Initiate TFTP/TFTM/MTFTP download
1082  *
1083  * @v xfer Data transfer interface
1084  * @v uri Uniform Resource Identifier
1085  * @ret rc Return status code
1086  */
1087 static int tftp_core_open ( struct interface *xfer, struct uri *uri,
1088  unsigned int default_port,
1089  struct sockaddr *multicast,
1090  unsigned int flags ) {
1091  struct tftp_request *tftp;
1092  int rc;
1093 
1094  /* Sanity checks */
1095  if ( ! uri->host )
1096  return -EINVAL;
1097  if ( ! uri->path )
1098  return -EINVAL;
1099  if ( uri->path[0] != '/' )
1100  return -EINVAL;
1101 
1102  /* Allocate and populate TFTP structure */
1103  tftp = zalloc ( sizeof ( *tftp ) );
1104  if ( ! tftp )
1105  return -ENOMEM;
1106  ref_init ( &tftp->refcnt, tftp_free );
1107  intf_init ( &tftp->xfer, &tftp_xfer_desc, &tftp->refcnt );
1108  intf_init ( &tftp->socket, &tftp_socket_desc, &tftp->refcnt );
1109  intf_init ( &tftp->mc_socket, &tftp_mc_socket_desc, &tftp->refcnt );
1110  timer_init ( &tftp->timer, tftp_timer_expired, &tftp->refcnt );
1111  tftp->uri = uri_get ( uri );
1112  tftp->blksize = TFTP_DEFAULT_BLKSIZE;
1113  tftp->flags = flags;
1114 
1115  /* Open socket */
1116  tftp->port = uri_port ( tftp->uri, default_port );
1117  if ( ( rc = tftp_reopen ( tftp ) ) != 0 )
1118  goto err;
1119 
1120  /* Open multicast socket */
1121  if ( multicast ) {
1122  if ( ( rc = tftp_reopen_mc ( tftp, multicast ) ) != 0 )
1123  goto err;
1124  }
1125 
1126  /* Start timer to initiate RRQ */
1127  start_timer_nodelay ( &tftp->timer );
1128 
1129  /* Attach to parent interface, mortalise self, and return */
1130  intf_plug_plug ( &tftp->xfer, xfer );
1131  ref_put ( &tftp->refcnt );
1132  return 0;
1133 
1134  err:
1135  DBGC ( tftp, "TFTP %p could not create request: %s\n",
1136  tftp, strerror ( rc ) );
1137  tftp_done ( tftp, rc );
1138  ref_put ( &tftp->refcnt );
1139  return rc;
1140 }
1141 
1142 /**
1143  * Initiate TFTP download
1144  *
1145  * @v xfer Data transfer interface
1146  * @v uri Uniform Resource Identifier
1147  * @ret rc Return status code
1148  */
1149 static int tftp_open ( struct interface *xfer, struct uri *uri ) {
1150  return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1152 
1153 }
1154 
1155 /** TFTP URI opener */
1156 struct uri_opener tftp_uri_opener __uri_opener = {
1157  .scheme = "tftp",
1158  .open = tftp_open,
1159 };
1160 
1161 /**
1162  * Initiate TFTM download
1163  *
1164  * @v xfer Data transfer interface
1165  * @v uri Uniform Resource Identifier
1166  * @ret rc Return status code
1167  */
1168 static int tftm_open ( struct interface *xfer, struct uri *uri ) {
1169  return tftp_core_open ( xfer, uri, TFTP_PORT, NULL,
1170  ( TFTP_FL_RRQ_SIZES |
1172 
1173 }
1174 
1175 /** TFTM URI opener */
1176 struct uri_opener tftm_uri_opener __uri_opener = {
1177  .scheme = "tftm",
1178  .open = tftm_open,
1179 };
1180 
1181 /**
1182  * Initiate MTFTP download
1183  *
1184  * @v xfer Data transfer interface
1185  * @v uri Uniform Resource Identifier
1186  * @ret rc Return status code
1187  */
1188 static int mtftp_open ( struct interface *xfer, struct uri *uri ) {
1189  return tftp_core_open ( xfer, uri, MTFTP_PORT,
1190  ( struct sockaddr * ) &tftp_mtftp_socket,
1192 }
1193 
1194 /** MTFTP URI opener */
1195 struct uri_opener mtftp_uri_opener __uri_opener = {
1196  .scheme = "mtftp",
1197  .open = mtftp_open,
1198 };
1199 
1200 /******************************************************************************
1201  *
1202  * Settings
1203  *
1204  ******************************************************************************
1205  */
1206 
1207 /**
1208  * Apply TFTP configuration settings
1209  *
1210  * @ret rc Return status code
1211  */
1212 static int tftp_apply_settings ( void ) {
1213  static struct in_addr tftp_server = { 0 };
1214  struct in_addr new_tftp_server;
1215  char uri_string[32];
1216  struct uri *uri;
1217 
1218  /* Retrieve TFTP server setting */
1219  fetch_ipv4_setting ( NULL, &next_server_setting, &new_tftp_server );
1220 
1221  /* If TFTP server setting has changed, set the current working
1222  * URI to match. Do it only when the TFTP server has changed
1223  * to try to minimise surprises to the user, who probably
1224  * won't expect the CWURI to change just because they updated
1225  * an unrelated setting and triggered all the settings
1226  * applicators.
1227  */
1228  if ( new_tftp_server.s_addr &&
1229  ( new_tftp_server.s_addr != tftp_server.s_addr ) ) {
1230  DBGC ( &tftp_server, "TFTP server changed %s => ",
1231  inet_ntoa ( tftp_server ) );
1232  DBGC ( &tftp_server, "%s\n", inet_ntoa ( new_tftp_server ) );
1233  snprintf ( uri_string, sizeof ( uri_string ),
1234  "tftp://%s/", inet_ntoa ( new_tftp_server ) );
1235  uri = parse_uri ( uri_string );
1236  if ( ! uri )
1237  return -ENOMEM;
1238  churi ( uri );
1239  uri_put ( uri );
1240  tftp_server = new_tftp_server;
1241  }
1242 
1243  return 0;
1244 }
1245 
1246 /** TFTP settings applicator */
1247 struct settings_applicator tftp_settings_applicator __settings_applicator = {
1249 };
A process.
Definition: process.h:18
#define iob_pull(iobuf, len)
Definition: iobuf.h:107
unsigned int mtftp_timeouts
MTFTP timeout count.
Definition: tftp.c:132
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct interface socket
Transport layer interface.
Definition: tftp.c:99
An object interface operation.
Definition: interface.h:18
#define TFTP_MAX_BLKSIZE
Definition: tftp.h:17
TCP/IP socket address.
Definition: tcpip.h:76
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
#define MTFTP_PORT
Default MTFTP server port.
Definition: tftp.h:35
Dynamic Host Configuration Protocol.
#define iob_put(iobuf, len)
Definition: iobuf.h:125
Error message tables.
Data transfer metadata.
Definition: xfer.h:23
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:279
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:206
int xfer_deliver_iob(struct interface *intf, struct io_buffer *iobuf)
Deliver datagram as I/O buffer without metadata.
Definition: xfer.c:256
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:100
A TFTP options acknowledgement (OACK) packet.
Definition: tftp.h:64
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:485
#define EINVAL_MC_NO_MC
Definition: tftp.c:69
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:195
static int tftp_errcode_to_rc(unsigned int errcode)
Convert TFTP error code to return status code.
Definition: tftp.c:883
#define FEATURE_PROTOCOL
Network protocols.
Definition: features.h:22
static int tftp_reopen(struct tftp_request *tftp)
Reopen TFTP socket.
Definition: tftp.c:220
static void bitmap_free(struct bitmap *bitmap)
Free bitmap resources.
Definition: bitmap.h:58
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition: xfer.h:48
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
Error codes.
uint16_t errcode
Definition: tftp.h:13
static int mtftp_open(struct interface *xfer, struct uri *uri)
Initiate MTFTP download.
Definition: tftp.c:1188
int xfer_open_socket(struct interface *intf, int semantics, struct sockaddr *peer, struct sockaddr *local)
Open socket.
Definition: open.c:143
static struct interface_descriptor tftp_xfer_desc
TFTP data transfer interface descriptor.
Definition: tftp.c:1077
uint16_t opcode
Definition: tftp.h:52
#define EINVAL_MC_INVALID_IP
Definition: tftp.c:75
static int tftp_apply_settings(void)
Apply TFTP configuration settings.
Definition: tftp.c:1212
int fetch_ipv4_setting(struct settings *settings, const struct setting *setting, struct in_addr *inp)
Fetch value of IPv4 address setting.
Definition: settings.c:913
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
uint64_t address
Base address.
Definition: ena.h:24
#define SOCK_DGRAM
Definition: socket.h:30
#define __einfo_errortab(einfo)
Definition: errortab.h:24
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:78
Retry timers.
struct arbelprm_completion_with_error error
Definition: arbel.h:12
static int tftp_process_multicast(struct tftp_request *tftp, char *value)
Process TFTP "multicast" option.
Definition: tftp.c:609
struct bitmap bitmap
Block bitmap.
Definition: tftp.c:135
static int tftp_process_option(struct tftp_request *tftp, const char *name, char *value)
Process TFTP option.
Definition: tftp.c:698
uint16_t block
Definition: tftp.h:53
#define DBGC(...)
Definition: compiler.h:505
#define MTFTP_MAX_TIMEOUTS
Maximum number of MTFTP open requests before falling back to TFTP.
Definition: tftp.c:165
A retry timer.
Definition: retry.h:22
static int tftp_process_tsize(struct tftp_request *tftp, char *value)
Process TFTP "tsize" option.
Definition: tftp.c:588
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:108
struct uri_opener tftp_uri_opener __uri_opener
TFTP URI opener.
Definition: tftp.c:1156
#define EINVAL_MC_INVALID_PORT
Definition: tftp.c:78
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:209
A TFTP data (DATA) packet.
Definition: tftp.h:44
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:159
A settings applicator.
Definition: settings.h:252
uint32_t data_len
Microcode data size (or 0 to indicate 2000 bytes)
Definition: ucode.h:26
struct refcnt refcnt
Reference count.
Definition: tftp.c:92
A TFTP request.
Definition: tftp.c:90
#define EACCES
Permission denied.
Definition: errno.h:299
IPv4 socket address.
Definition: in.h:85
#define ntohs(value)
Definition: byteswap.h:137
A data structure for storing profiling information.
Definition: profile.h:27
static int tftp_presize(struct tftp_request *tftp, size_t filesize)
Presize TFTP receive buffers and block bitmap.
Definition: tftp.c:282
Uniform Resource Identifiers.
sa_family_t sin_family
Socket address family (part of struct sockaddr)
Definition: in.h:90
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:174
unsigned int blksize
Data block size.
Definition: tftp.c:109
#define htonl(value)
Definition: byteswap.h:134
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:117
#define TFTP_ACK
Data block acknowledgement opcode.
Definition: tftp.h:22
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
#define ENOENT_NOT_FOUND
Definition: tftp.c:81
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:27
A timer.
Definition: timer.h:29
A TFTP option.
Definition: tftp.c:670
static void tftp_timer_expired(struct retry_timer *timer, int fail)
Handle TFTP retransmission timer expiry.
Definition: tftp.c:500
void * tail
End of data.
Definition: iobuf.h:55
#define ENOMEM
Not enough space.
Definition: errno.h:535
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:217
static struct interface_descriptor tftp_socket_desc
TFTP socket interface descriptor.
Definition: tftp.c:1027
static int tftp_rx_error(struct tftp_request *tftp, void *buf, size_t len)
Receive ERROR.
Definition: tftp.c:900
void * memcpy(void *dest, const void *src, size_t len) __nonnull
unsigned int flags
Request flags.
Definition: tftp.c:130
const char * name
Name.
Definition: profile.h:29
u8 port
Port number.
Definition: CIB_PRM.h:31
#define TFTP_ERR_ILLEGAL_OP
Illegal TFTP operation.
Definition: tftp.h:29
#define EINFO_ENOENT_NOT_FOUND
Definition: tftp.c:82
Send ACK packets.
Definition: tftp.c:155
Assertions.
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:46
struct interface xfer
Data transfer interface.
Definition: tftp.c:94
uint16_t errcode
Definition: tftp.h:59
static int tftp_send_error(struct tftp_request *tftp, int errcode, const char *errmsg)
Transmit ERROR (Abort)
Definition: tftp.c:435
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:36
An object interface.
Definition: interface.h:125
Request blksize and tsize options.
Definition: tftp.c:157
A long option, as used for getopt_long()
Definition: getopt.h:25
static struct interface_descriptor tftp_mc_socket_desc
TFTP multicast socket interface descriptor.
Definition: tftp.c:1036
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static int tftp_open(struct interface *xfer, struct uri *uri)
Initiate TFTP download.
Definition: tftp.c:1149
char errmsg[0]
Definition: tftp.h:14
FEATURE(FEATURE_PROTOCOL, "TFTP", DHCP_EB_FEATURE_TFTP, 1)
ring len
Length.
Definition: dwmac.h:231
const char * path
Path (after URI decoding)
Definition: uri.h:81
struct interface mc_socket
Multicast transport layer interface.
Definition: tftp.c:101
static int tftp_rx_data(struct tftp_request *tftp, struct io_buffer *iobuf)
Receive DATA.
Definition: tftp.c:798
const char * scheme
URI protocol name.
Definition: open.h:54
struct sockaddr sa
Definition: syslog.c:57
#define EINVAL_MC_NO_PORT
Definition: tftp.c:66
Transport-network layer interface.
Feature list.
FILE_SECBOOT(PERMITTED)
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:347
int bitmap_resize(struct bitmap *bitmap, unsigned int new_length)
Resize bitmap.
Definition: bitmap.c:43
struct settings_applicator tftp_settings_applicator __settings_applicator
TFTP settings applicator.
Definition: tftp.c:1247
static int tftp_send_rrq(struct tftp_request *tftp)
Transmit RRQ.
Definition: tftp.c:348
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:161
A bitmap.
Definition: bitmap.h:40
Profiling.
static void tftp_free(struct refcnt *refcnt)
Free TFTP request.
Definition: tftp.c:185
static void tftp_close(struct tftp_request *tftp, int rc)
Terminate download.
Definition: tftp.c:1061
static void tftp_done(struct tftp_request *tftp, int rc)
Mark TFTP request as complete.
Definition: tftp.c:200
int xfer_seek(struct interface *intf, off_t offset)
Seek to position.
Definition: xfer.c:352
Configuration settings.
uint16_t st_port
TCP/IP port.
Definition: tcpip.h:82
Generalized socket address structure.
Definition: socket.h:97
An object interface descriptor.
Definition: interface.h:56
static int tftp_send_packet(struct tftp_request *tftp)
Transmit next relevant packet.
Definition: tftp.c:469
static int tftp_process_blksize(struct tftp_request *tftp, char *value)
Process TFTP "blksize" option.
Definition: tftp.c:567
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
void tftp_set_mtftp_address(struct in_addr address)
Set MTFTP multicast address.
Definition: tftp.c:329
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
uint16_t opcode
Definition: tftp.h:39
const char * name
Long name of this option.
Definition: getopt.h:27
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
IP address structure.
Definition: in.h:42
uint16_t sin_port
TCP/IP port (part of struct sockaddr_tcpip)
Definition: in.h:94
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:997
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:272
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:160
TFTP protocol.
static struct sockaddr_in tftp_mtftp_socket
MTFTP multicast receive address.
Definition: tftp.c:318
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
uint32_t addr
Buffer address.
Definition: dwmac.h:20
int inet_aton(const char *string, struct in_addr *in)
Parse IPv4 address.
Definition: ipv4.c:787
#define DBGC_HD(...)
Definition: compiler.h:507
static int tftm_open(struct interface *xfer, struct uri *uri)
Initiate TFTM download.
Definition: tftp.c:1168
static struct tftp_option tftp_options[]
Recognised TFTP options.
Definition: tftp.c:683
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:180
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:195
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:814
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
Data transfer interface opening.
char data[0]
Definition: tftp.h:66
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:256
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
The common header of all TFTP packets.
Definition: tftp.h:70
struct ib_cm_common common
Definition: ib_mad.h:12
#define TFTP_ERR_ACCESS_DENIED
Access violation.
Definition: tftp.h:27
const char * host
Host name.
Definition: uri.h:77
#define TFTP_PORT
Default TFTP server port.
Definition: tftp.h:15
#define TFTP_ERROR
Error opcode.
Definition: tftp.h:23
void bitmap_set(struct bitmap *bitmap, unsigned int bit)
Set bit in bitmap.
Definition: bitmap.c:94
#define EINVAL_MC_INVALID_MC
Definition: tftp.c:72
A TFTP read request (RRQ) packet.
Definition: tftp.h:38
uint16_t opcode
Definition: tftp.h:58
#define TFTP_OACK
Options acknowledgement opcode.
Definition: tftp.h:24
static int tftp_rx(struct tftp_request *tftp, struct io_buffer *iobuf, struct xfer_metadata *meta)
Receive new data.
Definition: tftp.c:931
void start_timer(struct retry_timer *timer)
Start timer.
Definition: retry.c:94
static int tftp_reopen_mc(struct tftp_request *tftp, struct sockaddr *local)
Reopen TFTP multicast socket.
Definition: tftp.c:254
static int tftp_send_ack(struct tftp_request *tftp)
Transmit ACK.
Definition: tftp.c:401
uint32_t s_addr
Definition: in.h:43
Perform MTFTP recovery on timeout.
Definition: tftp.c:161
static struct profiler tftp_client_profiler __profiler
Client profiler.
Definition: tftp.c:168
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char errmsg[0]
Definition: tftp.h:60
signed long off_t
Definition: stdint.h:8
struct retry_timer timer
Retransmission timer.
Definition: tftp.c:149
struct in_addr sin_addr
IPv4 address.
Definition: in.h:101
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:81
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:118
Bitmaps for multicast downloads.
unsigned int port
Server port.
Definition: tftp.c:122
static int tftp_rx_oack(struct tftp_request *tftp, void *buf, size_t len)
Receive OACK.
Definition: tftp.c:722
#define DBGC2(...)
Definition: compiler.h:522
uint8_t block[3][8]
DES-encrypted blocks.
Definition: mschapv2.h:12
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:595
#define TFTP_ERR_FILE_NOT_FOUND
File not found.
Definition: tftp.h:26
void * data
Start of data.
Definition: iobuf.h:53
#define EINVAL_BLKSIZE
Definition: tftp.c:60
void tftp_set_mtftp_port(unsigned int port)
Set MTFTP multicast port.
Definition: tftp.c:338
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:456
Reference counting.
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define TFTP_DEFAULT_BLKSIZE
Default TFTP data block size.
Definition: tftp.h:16
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static unsigned int bitmap_first_gap(struct bitmap *bitmap)
Get first gap within bitmap.
Definition: bitmap.h:70
static struct interface_operation tftp_socket_operations[]
TFTP socket operations.
Definition: tftp.c:1022
static struct interface_operation tftp_xfer_operations[]
TFTP data transfer interface operations.
Definition: tftp.c:1071
int(* apply)(void)
Apply updated settings.
Definition: settings.h:257
struct errortab tftp_errors [] __errortab
Human-readable error messages.
Definition: tftp.c:176
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:383
A Uniform Resource Identifier.
Definition: uri.h:65
uint32_t blksize
Cipher block size.
Definition: pccrr.h:15
#define DHCP_EB_FEATURE_TFTP
TFTP protocol.
Definition: features.h:42
#define TFTP_DATA
Data block opcode.
Definition: tftp.h:21
struct sockaddr_in sin
Definition: syslog.c:59
struct uri * uri
URI being fetched.
Definition: tftp.c:97
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
uint8_t meta
Metadata flags.
Definition: ena.h:14
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
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:1087
unsigned long tsize
File size.
Definition: tftp.c:116
A URI opener.
Definition: open.h:48
struct sockaddr_tcpip peer
Peer address.
Definition: tftp.c:128
static int bitmap_full(struct bitmap *bitmap)
Check to see if bitmap is full.
Definition: bitmap.h:82
static struct interface_operation tftp_mc_socket_operations[]
TFTP multicast socket operations.
Definition: tftp.c:1031
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:115
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:670
String functions.
#define htons(value)
Definition: byteswap.h:136
Request multicast option.
Definition: tftp.c:159
const char * name
Option name.
Definition: tftp.c:672
struct bofm_section_header done
Definition: bofm_test.c:46
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:64
static size_t tftp_xfer_window(struct tftp_request *tftp)
Check flow control window.
Definition: tftp.c:1045
#define TFTP_RRQ
Read request opcode.
Definition: tftp.h:19
A TFTP acknowledgement (ACK) packet.
Definition: tftp.h:51
size_t filesize
Maximum known length.
Definition: tftp.c:147
#define EINVAL_TSIZE
Definition: tftp.c:63
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:403
A TFTP error (ERROR) packet.
Definition: tftp.h:57
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
String functions.
if(natsemi->flags &NATSEMI_64BIT) return 1
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:297
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:38