You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #include "tftp.h"
  2. #include "old_tcp.h" /* for struct tcphdr */
  3. #include "errno.h"
  4. #include "etherboot.h"
  5. #include "tftpcore.h"
  6. /** @file */
  7. /**
  8. * await_reply() filter for TFTP packets
  9. *
  10. * @v ptr Pointer to a struct tftp_state
  11. * @v tftp_state::server::sin_addr TFTP server IP address
  12. * @v tftp_state::lport Client UDP port
  13. * @v tftp_state::multicast::sin_addr Multicast IP address, or 0.0.0.0
  14. * @v tftp_state::multicast::sin_port Multicast UDP port, or 0
  15. * @v ip IP header
  16. * @v udp UDP header
  17. * @ret True This is our TFTP packet
  18. * @ret False This is not one of our TFTP packets
  19. *
  20. * Wait for a TFTP packet that is part of the current connection
  21. * (i.e. comes from the TFTP server, has the correct destination port,
  22. * and is addressed either to our IP address and UDP port, or to our
  23. * multicast listening address and UDP port).
  24. *
  25. * Use await_tftp() in code such as
  26. *
  27. * @code
  28. *
  29. * if ( await_reply ( await_tftp, 0, &tftp_state, timeout ) ) {
  30. * ...
  31. * }
  32. *
  33. * @endcode
  34. */
  35. static int await_tftp ( int ival __unused, void *ptr,
  36. unsigned short ptype __unused, struct iphdr *ip,
  37. struct udphdr *udp, struct tcphdr *tcp __unused ) {
  38. struct tftp_state *state = ptr;
  39. /* Must have valid UDP (and, therefore, also IP) headers */
  40. if ( ! udp ) {
  41. DBG2 ( "TFTPCORE: not UDP\n" );
  42. return 0;
  43. }
  44. /* Packet must come from the TFTP server */
  45. if ( ip->src.s_addr != state->server.sin_addr.s_addr ) {
  46. DBG2 ( "TFTPCORE: from %@, not from TFTP server %@\n",
  47. ip->src.s_addr, state->server.sin_addr.s_addr );
  48. return 0;
  49. }
  50. /* Packet may be addressed to our IP address and unicast UDP
  51. * port
  52. */
  53. if ( ( ip->dest.s_addr == arptable[ARP_CLIENT].ipaddr.s_addr ) &&
  54. ( ntohs ( udp->dest ) == state->lport ) ) {
  55. return 1;
  56. }
  57. /* Packet may be addressed to our multicast IP address and UDP
  58. * port, if we have one
  59. */
  60. if ( ( state->multicast.sin_addr.s_addr ) &&
  61. ( ip->dest.s_addr == state->multicast.sin_addr.s_addr ) &&
  62. ( ntohs ( udp->dest ) == state->multicast.sin_port ) ) {
  63. return 1;
  64. }
  65. DBG2 ( "TFTPCORE: to %@:%d, not to %@:%d (or %@:%d)\n",
  66. ip->dest.s_addr, ntohs ( udp->dest ),
  67. arptable[ARP_CLIENT].ipaddr.s_addr, state->lport,
  68. state->multicast.sin_addr.s_addr, state->multicast.sin_port );
  69. return 0;
  70. }
  71. /**
  72. * Retrieve a TFTP packet
  73. *
  74. * @v state TFTP transfer state
  75. * @v tftp_state::server::sin_addr TFTP server IP address
  76. * @v tftp_state::lport Client UDP port
  77. * @v tftp_state::multicast::sin_addr Multicast IP address, or 0.0.0.0
  78. * @v tftp_state::multicast::sin_port Multicast UDP port, or 0
  79. * @v timeout Time to wait for a response
  80. * @ret True Received a non-error response
  81. * @ret False Received error response / no response
  82. * @ret *reply The server's response, if any
  83. * @err #PXENV_STATUS_TFTP_READ_TIMEOUT No response received in time
  84. * @err other As set by tftp_set_errno()
  85. *
  86. * Retrieve the next packet sent by the TFTP server, if any is sent
  87. * within the specified timeout period. The packet is returned via
  88. * #reply. If no packet is received within the timeout period, a NULL
  89. * value will be stored in #reply.
  90. *
  91. * If the response from the server is a TFTP ERROR packet, tftp_get()
  92. * will return False and #errno will be set accordingly.
  93. *
  94. * You can differentiate between "received no response" and "received
  95. * an error response" by checking #reply; if #reply is NULL then no
  96. * response was received.
  97. */
  98. int tftp_get ( struct tftp_state *state, long timeout,
  99. union tftp_any **reply ) {
  100. *reply = NULL;
  101. if ( ! await_reply ( await_tftp, 0, state, timeout ) ) {
  102. errno = PXENV_STATUS_TFTP_READ_TIMEOUT;
  103. return 0;
  104. }
  105. *reply = ( union tftp_any * ) &nic.packet[ETH_HLEN];
  106. DBG ( "TFTPCORE: got reply (type %d)\n",
  107. ntohs ( (*reply)->common.opcode ) );
  108. if ( ntohs ( (*reply)->common.opcode ) == TFTP_ERROR ){
  109. tftp_set_errno ( &(*reply)->error );
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. /**
  115. * Issue a TFTP open request (RRQ)
  116. *
  117. * @v state TFTP transfer state
  118. * @v tftp_state::server::sin_addr TFTP server IP address
  119. * @v tftp_state::server::sin_port TFTP server UDP port, or 0
  120. * @v tftp_state::lport Client UDP port, or 0
  121. * @v tftp_state::multicast::sin_addr Multicast IP address, or 0.0.0.0
  122. * @v tftp_state::multicast::sin_port Multicast UDP port, or 0
  123. * @v tftp_state::blksize Requested blksize, or 0
  124. * @v filename File name
  125. * @v multicast Enable/disable rfc2090 multicast TFTP
  126. * @ret True Received a non-error response
  127. * @ret False Received error response / no response
  128. * @ret tftp_state::server::sin_port TFTP server UDP port
  129. * @ret tftp_state::lport Client UDP port
  130. * @ret tftp_state::blksize Always #TFTP_DEFAULT_BLKSIZE
  131. * @ret *reply The server's response, if any
  132. * @err #PXENV_STATUS_TFTP_OPEN_TIMEOUT TFTP open timed out
  133. * @err other As returned by udp_transmit()
  134. * @err other As set by tftp_set_errno()
  135. *
  136. * Send a TFTP/TFTM/MTFTP RRQ (read request) to a TFTP server, and
  137. * return the server's reply (which may be an OACK, DATA or ERROR
  138. * packet). The server's reply will not be acknowledged, or processed
  139. * in any way.
  140. *
  141. * If tftp_state::server::sin_port is 0, the standard TFTP server port
  142. * (#TFTP_PORT) will be used.
  143. *
  144. * If tftp_state::lport is 0, the standard mechanism of
  145. * using a new, unique port number for each TFTP request will be used.
  146. *
  147. * If tftp_state::multicast::sin_addr is not 0.0.0.0, it (and
  148. * tftp_state::multicast::sin_port) will be used as a multicast
  149. * listening address for replies from the TFTP server.
  150. *
  151. * For the various different types of TFTP server, you should treat
  152. * tftp_state::lport and tftp_state::multicast as follows:
  153. *
  154. * - Standard TFTP server: set tftp_state::lport to 0,
  155. * tftp_state::multicast::sin_addr to 0.0.0.0 and
  156. * tftp_state::multicast::sin_port to 0. tftp_open() will set
  157. * tftp_state::lport to the assigned local UDP port.
  158. *
  159. * - TFTM server: set tftp_state::lport to 0,
  160. * tftp_state::multicast::sin_addr to 0.0.0.0 and
  161. * tftp_state::multicast::sin_port to 0. tftp_open() will set
  162. * tftp_state::lport to the assigned local UDP port. (Your call
  163. * to tftp_process_opts() will then overwrite both
  164. * tftp_state::multicast::sin_addr and
  165. * tftp_state::multicast::sin_port with the values specified in
  166. * the OACK packet.)
  167. *
  168. * - MTFTP server: set tftp_state::multicast::sin_addr to the
  169. * multicast address and both tftp_state::lport and
  170. * tftp_state::multicast::sin_port to the multicast port (both of
  171. * which must be previously known, e.g. provided by a DHCP
  172. * server). tftp_open() will not alter these values.
  173. *
  174. * If tftp_state::blksize is 0, the maximum blocksize
  175. * (#TFTP_MAX_BLKSIZE) will be requested.
  176. *
  177. * On exit, tftp_state::blksize will always contain
  178. * #TFTP_DEFAULT_BLKSIZE, since this is the blocksize value that must
  179. * be assumed until the OACK packet is processed (by a subsequent call
  180. * to tftp_process_opts()).
  181. *
  182. * tftp_state::server::sin_port will be set to the UDP port from which
  183. * the server's response originated. This may or may not be the port
  184. * to which the open request was sent.
  185. *
  186. * The options "blksize" and "tsize" will always be appended to a TFTP
  187. * open request. The option "multicast" will be appended to the
  188. * request if #multicast is True. Servers that do not understand any
  189. * of these options should simply ignore them.
  190. *
  191. * tftp_open() will not automatically join or leave multicast groups;
  192. * the caller is responsible for calling join_group() and
  193. * leave_group() at appropriate times.
  194. *
  195. * If the response from the server is a TFTP ERROR packet, tftp_open()
  196. * will return False and #errno will be set accordingly.
  197. */
  198. int tftp_open ( struct tftp_state *state, const char *filename,
  199. union tftp_any **reply, int multicast ) {
  200. static unsigned short lport = 2000; /* local port */
  201. int fixed_lport;
  202. struct tftp_rrq rrq;
  203. char *p;
  204. unsigned int rrqlen;
  205. int retry;
  206. /* Flush receive queue */
  207. rx_qdrain();
  208. /* Default to blksize of TFTP_MAX_BLKSIZE if none specified */
  209. if ( ! state->blksize )
  210. state->blksize = TFTP_MAX_BLKSIZE;
  211. /* Use default TFTP server port if none specified */
  212. if ( ! state->server.sin_port )
  213. state->server.sin_port = TFTP_PORT;
  214. /* Determine whether or not to use lport */
  215. fixed_lport = state->lport;
  216. /* Set up RRQ */
  217. rrq.opcode = htons ( TFTP_RRQ );
  218. p = rrq.data;
  219. p += sprintf ( p, "%s%coctet%cblksize%c%d%ctsize%c0",
  220. filename, 0, 0, 0, state->blksize, 0, 0 ) + 1;
  221. if ( multicast ) {
  222. p += sprintf ( p, "multicast%c", 0 ) + 1;
  223. }
  224. rrqlen = ( p - ( char * ) &rrq );
  225. /* Set negotiated blksize to default value */
  226. state->blksize = TFTP_DEFAULT_BLKSIZE;
  227. /* Nullify received packet pointer */
  228. *reply = NULL;
  229. /* Transmit RRQ until we get a response */
  230. for ( retry = 0 ; retry < MAX_TFTP_RETRIES ; retry++ ) {
  231. long timeout = rfc2131_sleep_interval ( TIMEOUT, retry );
  232. /* Set client UDP port, if not already fixed */
  233. if ( ! fixed_lport )
  234. state->lport = ++lport;
  235. /* Send the RRQ */
  236. DBG ( "TFTPCORE: requesting %@:%d/%s from port %d\n",
  237. state->server.sin_addr.s_addr, state->server.sin_port,
  238. rrq.data, state->lport );
  239. if ( ! udp_transmit ( state->server.sin_addr.s_addr,
  240. state->lport, state->server.sin_port,
  241. rrqlen, &rrq ) )
  242. return 0;
  243. /* Wait for response */
  244. if ( tftp_get ( state, timeout, reply ) ) {
  245. /* We got a non-error response */
  246. state->server.sin_port
  247. = ntohs ( (*reply)->common.udp.src );
  248. DBG ( "TFTP server is at %@:%d\n",
  249. state->server.sin_addr.s_addr,
  250. state->server.sin_port );
  251. return 1;
  252. }
  253. if ( *reply ) {
  254. /* We got an error response; abort */
  255. return 0;
  256. }
  257. }
  258. DBG ( "TFTPCORE: open request timed out\n" );
  259. errno = PXENV_STATUS_TFTP_OPEN_TIMEOUT;
  260. return 0;
  261. }
  262. /**
  263. * Process a TFTP OACK packet
  264. *
  265. * @v state TFTP transfer state
  266. * @v oack The TFTP OACK packet
  267. * @ret True Options were processed successfully
  268. * @ret False Options were not processed successfully
  269. * @ret tftp_state::blksize Negotiated blksize
  270. * @ret tftp_state::tsize File size (if known), or 0
  271. * @ret tftp_state::multicast::sin_addr Multicast IP address, or 0.0.0.0
  272. * @ret tftp_state::multicast::sin_port Multicast UDP port, or 0
  273. * @ret tftp_state::master Client is master
  274. * @err EINVAL An invalid option value was encountered
  275. *
  276. * Process the options returned by the TFTP server in an rfc2347 OACK
  277. * packet. The options "blksize" (rfc2348), "tsize" (rfc2349) and
  278. * "multicast" (rfc2090) are recognised and processed; any other
  279. * options are silently ignored.
  280. *
  281. * Where an option is not present in the OACK packet, the
  282. * corresponding field(s) in #state will be left unaltered.
  283. *
  284. * Calling tftp_process_opts() does not send an acknowledgement for
  285. * the OACK packet; this is the responsibility of the caller.
  286. *
  287. * @note If the "blksize" option is not present, tftp_state::blksize
  288. * will @b not be implicitly set to #TFTP_DEFAULT_BLKSIZE. However,
  289. * since tftp_open() always sets tftp_state::blksize to
  290. * #TFTP_DEFAULT_BLKSIZE before returning, you probably don't need to
  291. * worry about this.
  292. */
  293. int tftp_process_opts ( struct tftp_state *state, struct tftp_oack *oack ) {
  294. const char *p;
  295. const char *end;
  296. DBG ( "TFTPCORE: processing OACK\n" );
  297. /* End of options */
  298. end = ( ( char * ) &oack->udp ) + ntohs ( oack->udp.len );
  299. /* Only possible error */
  300. errno = EINVAL;
  301. for ( p = oack->data ; p < end ; ) {
  302. if ( strcasecmp ( "blksize", p ) == 0 ) {
  303. p += 8;
  304. state->blksize = strtoul ( p, &p, 10 );
  305. if ( *p ) {
  306. DBG ( "TFTPCORE: garbage \"%s\" "
  307. "after blksize\n", p );
  308. return 0;
  309. }
  310. p++;
  311. DBG ( "TFTPCORE: got blksize %d\n", state->blksize );
  312. } else if ( strcasecmp ( "tsize", p ) == 0 ) {
  313. p += 6;
  314. state->tsize = strtoul ( p, &p, 10 );
  315. if ( *p ) {
  316. DBG ( "TFTPCORE: garbage \"%s\" "
  317. "after tsize\n", p );
  318. return 0;
  319. }
  320. p++;
  321. DBG ( "TFTPCORE: got tsize %d\n", state->tsize );
  322. } else if ( strcasecmp ( "multicast", p ) == 0 ) {
  323. p += 10;
  324. char *e = strchr ( p, ',' );
  325. if ( ( ! e ) || ( e >= end ) ) {
  326. DBG ( "TFTPCORE: malformed multicast field "
  327. "\"%s\"\n", p );
  328. return 0;
  329. }
  330. /* IP address may be missing, in which case we
  331. * should leave state->multicast.sin_addr
  332. * unaltered.
  333. */
  334. if ( e != p ) {
  335. int rc;
  336. *e = '\0';
  337. rc = inet_aton ( p,
  338. &state->multicast.sin_addr );
  339. *e = ',';
  340. if ( ! rc ) {
  341. DBG ( "TFTPCORE: malformed multicast "
  342. "IP address \"%s\"\n", p );
  343. return 0;
  344. }
  345. }
  346. p = e + 1;
  347. /* UDP port may also be missing */
  348. if ( *p != ',' ) {
  349. state->multicast.sin_port
  350. = strtoul ( p, &p, 10 );
  351. if ( *p != ',' ) {
  352. DBG ( "TFTPCORE: garbage \"%s\" "
  353. "after multicast port\n", p );
  354. return 0;
  355. }
  356. }
  357. p++;
  358. /* "Master Client" must always be present */
  359. state->master = strtoul ( p, &p, 10 );
  360. if ( *p ) {
  361. DBG ( "TFTPCORE: garbage \"%s\" "
  362. "after multicast mc\n", p );
  363. return 0;
  364. }
  365. p++;
  366. DBG ( "TFTPCORE: got multicast %@:%d (%s)\n",
  367. state->multicast.sin_addr.s_addr,
  368. state->multicast.sin_port,
  369. ( state->master ? "master" : "not master" ) );
  370. } else {
  371. DBG ( "TFTPCORE: unknown option \"%s\"\n", p );
  372. p += strlen ( p ) + 1; /* skip option name */
  373. p += strlen ( p ) + 1; /* skip option value */
  374. }
  375. }
  376. if ( p > end ) {
  377. DBG ( "TFTPCORE: overran options in OACK\n" );
  378. return 0;
  379. }
  380. return 1;
  381. }
  382. /**
  383. * Acknowledge a TFTP packet
  384. *
  385. * @v state TFTP transfer state
  386. * @v tftp_state::server::sin_addr TFTP server IP address
  387. * @v tftp_state::server::sin_port TFTP server UDP port
  388. * @v tftp_state::lport Client UDP port
  389. * @v tftp_state::block Most recently received block number
  390. * @ret True Acknowledgement packet was sent
  391. * @ret False Acknowledgement packet was not sent
  392. * @err other As returned by udp_transmit()
  393. *
  394. * Send a TFTP ACK packet for the most recently received block.
  395. *
  396. * This sends only a single ACK packet; it does not wait for the
  397. * server's response.
  398. */
  399. int tftp_ack_nowait ( struct tftp_state *state ) {
  400. struct tftp_ack ack;
  401. DBG ( "TFTPCORE: acknowledging data block %d\n", state->block );
  402. ack.opcode = htons ( TFTP_ACK );
  403. ack.block = htons ( state->block );
  404. return udp_transmit ( state->server.sin_addr.s_addr,
  405. state->lport, state->server.sin_port,
  406. sizeof ( ack ), &ack );
  407. }
  408. /**
  409. * Acknowledge a TFTP packet and wait for a response
  410. *
  411. * @v state TFTP transfer state
  412. * @v tftp_state::server::sin_addr TFTP server IP address
  413. * @v tftp_state::server::sin_port TFTP server UDP port
  414. * @v tftp_state::lport Client UDP port
  415. * @v tftp_state::block Most recently received block number
  416. * @ret True Received a non-error response
  417. * @ret False Received error response / no response
  418. * @ret *reply The server's response, if any
  419. * @err #PXENV_STATUS_TFTP_READ_TIMEOUT Timed out waiting for a response
  420. * @err other As returned by tftp_ack_nowait()
  421. * @err other As set by tftp_set_errno()
  422. *
  423. * Send a TFTP ACK packet for the most recently received data block,
  424. * and keep transmitting this ACK until we get a response from the
  425. * server (e.g. a new data block).
  426. *
  427. * If the response is a TFTP DATA packet, no processing is done.
  428. * Specifically, the block number is not checked to ensure that this
  429. * is indeed the next data block in the sequence, nor is
  430. * tftp_state::block updated with the new block number.
  431. *
  432. * If the response from the server is a TFTP ERROR packet, tftp_open()
  433. * will return False and #errno will be set accordingly.
  434. */
  435. int tftp_ack ( struct tftp_state *state, union tftp_any **reply ) {
  436. int retry;
  437. *reply = NULL;
  438. for ( retry = 0 ; retry < MAX_TFTP_RETRIES ; retry++ ) {
  439. long timeout = rfc2131_sleep_interval ( TFTP_REXMT, retry );
  440. /* ACK the last data block */
  441. if ( ! tftp_ack_nowait ( state ) ) {
  442. DBG ( "TFTP: could not send ACK: %m\n" );
  443. return 0;
  444. }
  445. if ( tftp_get ( state, timeout, reply ) ) {
  446. /* We got a non-error response */
  447. return 1;
  448. }
  449. if ( *reply ) {
  450. /* We got an error response */
  451. return 0;
  452. }
  453. }
  454. DBG ( "TFTP: timed out during read\n" );
  455. errno = PXENV_STATUS_TFTP_READ_TIMEOUT;
  456. return 0;
  457. }
  458. /**
  459. * Send a TFTP error
  460. *
  461. * @v state TFTP transfer state
  462. * @v tftp_state::server::sin_addr TFTP server IP address
  463. * @v tftp_state::server::sin_port TFTP server UDP port
  464. * @v tftp_state::lport Client UDP port
  465. * @v errcode TFTP error code
  466. * @v errmsg Descriptive error string, or NULL
  467. * @ret True Error packet was sent
  468. * @ret False Error packet was not sent
  469. *
  470. * Send a TFTP ERROR packet back to the server to terminate the
  471. * transfer.
  472. *
  473. * If #errmsg is NULL, the current error message string as returned by
  474. * strerror(errno) will be used as the error text.
  475. */
  476. int tftp_error ( struct tftp_state *state, int errcode, const char *errmsg ) {
  477. struct tftp_error error;
  478. DBG ( "TFTPCORE: aborting with error %d (%s)\n", errcode, errmsg );
  479. error.opcode = htons ( TFTP_ERROR );
  480. error.errcode = htons ( errcode );
  481. strncpy ( error.errmsg, errmsg ? errmsg : strerror ( errno ),
  482. sizeof ( error.errmsg ) );
  483. return udp_transmit ( state->server.sin_addr.s_addr,
  484. state->lport, state->server.sin_port,
  485. sizeof ( error ), &error );
  486. }
  487. /**
  488. * Interpret a TFTP error
  489. *
  490. * @v error Pointer to a struct tftp_error
  491. *
  492. * Sets #errno based on the error code in a TFTP ERROR packet.
  493. */
  494. void tftp_set_errno ( struct tftp_error *error ) {
  495. static int errmap[] = {
  496. [TFTP_ERR_FILE_NOT_FOUND] = PXENV_STATUS_TFTP_FILE_NOT_FOUND,
  497. [TFTP_ERR_ACCESS_DENIED] = PXENV_STATUS_TFTP_ACCESS_VIOLATION,
  498. [TFTP_ERR_ILLEGAL_OP] = PXENV_STATUS_TFTP_UNKNOWN_OPCODE,
  499. };
  500. unsigned int errcode = ntohs ( error->errcode );
  501. errno = 0;
  502. if ( errcode < ( sizeof(errmap) / sizeof(errmap[0]) ) )
  503. errno = errmap[errcode];
  504. if ( ! errno )
  505. errno = PXENV_STATUS_TFTP_ERROR_OPCODE;
  506. }