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