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