tftp_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /****************************************************************//**
  2. *
  3. * @file tftp_server.c
  4. *
  5. * @author Logan Gunthorpe <logang@deltatee.com>
  6. * Dirk Ziegelmeier <dziegel@gmx.de>
  7. *
  8. * @brief Trivial File Transfer Protocol (RFC 1350)
  9. *
  10. * Copyright (c) Deltatee Enterprises Ltd. 2013
  11. * All rights reserved.
  12. *
  13. ********************************************************************/
  14. /*
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification,are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  29. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * Author: Logan Gunthorpe <logang@deltatee.com>
  38. * Dirk Ziegelmeier <dziegel@gmx.de>
  39. *
  40. */
  41. /**
  42. * @defgroup tftp TFTP server
  43. * @ingroup apps
  44. *
  45. * This is simple TFTP server for the lwIP raw API.
  46. */
  47. #include "lwip/apps/tftp_server.h"
  48. #if LWIP_UDP
  49. #include "lwip/udp.h"
  50. #include "lwip/timeouts.h"
  51. #include "lwip/debug.h"
  52. #define TFTP_MAX_PAYLOAD_SIZE 512
  53. #define TFTP_HEADER_LENGTH 4
  54. #define TFTP_RRQ 1
  55. #define TFTP_WRQ 2
  56. #define TFTP_DATA 3
  57. #define TFTP_ACK 4
  58. #define TFTP_ERROR 5
  59. enum tftp_error {
  60. TFTP_ERROR_FILE_NOT_FOUND = 1,
  61. TFTP_ERROR_ACCESS_VIOLATION = 2,
  62. TFTP_ERROR_DISK_FULL = 3,
  63. TFTP_ERROR_ILLEGAL_OPERATION = 4,
  64. TFTP_ERROR_UNKNOWN_TRFR_ID = 5,
  65. TFTP_ERROR_FILE_EXISTS = 6,
  66. TFTP_ERROR_NO_SUCH_USER = 7
  67. };
  68. #include <string.h>
  69. struct tftp_state {
  70. const struct tftp_context *ctx;
  71. void *handle;
  72. struct pbuf *last_data;
  73. struct udp_pcb *upcb;
  74. ip_addr_t addr;
  75. u16_t port;
  76. int timer;
  77. int last_pkt;
  78. u16_t blknum;
  79. u8_t retries;
  80. u8_t mode_write;
  81. };
  82. static struct tftp_state tftp_state;
  83. static void tftp_tmr(void* arg);
  84. static void
  85. close_handle(void)
  86. {
  87. tftp_state.port = 0;
  88. ip_addr_set_any(0, &tftp_state.addr);
  89. if(tftp_state.last_data != NULL) {
  90. pbuf_free(tftp_state.last_data);
  91. tftp_state.last_data = NULL;
  92. }
  93. sys_untimeout(tftp_tmr, NULL);
  94. if (tftp_state.handle) {
  95. tftp_state.ctx->close(tftp_state.handle);
  96. tftp_state.handle = NULL;
  97. LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: closing\n"));
  98. }
  99. }
  100. static void
  101. send_error(const ip_addr_t *addr, u16_t port, enum tftp_error code, const char *str)
  102. {
  103. int str_length = strlen(str);
  104. struct pbuf* p;
  105. u16_t* payload;
  106. p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(TFTP_HEADER_LENGTH + str_length + 1), PBUF_RAM);
  107. if(p == NULL) {
  108. return;
  109. }
  110. payload = (u16_t*) p->payload;
  111. payload[0] = PP_HTONS(TFTP_ERROR);
  112. payload[1] = lwip_htons(code);
  113. MEMCPY(&payload[2], str, str_length + 1);
  114. udp_sendto(tftp_state.upcb, p, addr, port);
  115. pbuf_free(p);
  116. }
  117. static void
  118. send_ack(u16_t blknum)
  119. {
  120. struct pbuf* p;
  121. u16_t* payload;
  122. p = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH, PBUF_RAM);
  123. if(p == NULL) {
  124. return;
  125. }
  126. payload = (u16_t*) p->payload;
  127. payload[0] = PP_HTONS(TFTP_ACK);
  128. payload[1] = lwip_htons(blknum);
  129. udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
  130. pbuf_free(p);
  131. }
  132. static void
  133. resend_data(void)
  134. {
  135. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, tftp_state.last_data->len, PBUF_RAM);
  136. if(p == NULL) {
  137. return;
  138. }
  139. if(pbuf_copy(p, tftp_state.last_data) != ERR_OK) {
  140. pbuf_free(p);
  141. return;
  142. }
  143. udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
  144. pbuf_free(p);
  145. }
  146. static void
  147. send_data(void)
  148. {
  149. u16_t *payload;
  150. int ret;
  151. if(tftp_state.last_data != NULL) {
  152. pbuf_free(tftp_state.last_data);
  153. }
  154. tftp_state.last_data = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH + TFTP_MAX_PAYLOAD_SIZE, PBUF_RAM);
  155. if(tftp_state.last_data == NULL) {
  156. return;
  157. }
  158. payload = (u16_t *) tftp_state.last_data->payload;
  159. payload[0] = PP_HTONS(TFTP_DATA);
  160. payload[1] = lwip_htons(tftp_state.blknum);
  161. ret = tftp_state.ctx->read(tftp_state.handle, &payload[2], TFTP_MAX_PAYLOAD_SIZE);
  162. if (ret < 0) {
  163. send_error(&tftp_state.addr, tftp_state.port, TFTP_ERROR_ACCESS_VIOLATION, "Error occured while reading the file.");
  164. close_handle();
  165. return;
  166. }
  167. pbuf_realloc(tftp_state.last_data, (u16_t)(TFTP_HEADER_LENGTH + ret));
  168. resend_data();
  169. }
  170. static void
  171. recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  172. {
  173. u16_t *sbuf = (u16_t *) p->payload;
  174. int opcode;
  175. LWIP_UNUSED_ARG(arg);
  176. LWIP_UNUSED_ARG(upcb);
  177. if (((tftp_state.port != 0) && (port != tftp_state.port)) ||
  178. (!ip_addr_isany_val(tftp_state.addr) && !ip_addr_cmp(&tftp_state.addr, addr))) {
  179. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
  180. pbuf_free(p);
  181. return;
  182. }
  183. opcode = sbuf[0];
  184. tftp_state.last_pkt = tftp_state.timer;
  185. tftp_state.retries = 0;
  186. switch (opcode) {
  187. case PP_HTONS(TFTP_RRQ): /* fall through */
  188. case PP_HTONS(TFTP_WRQ):
  189. {
  190. const char tftp_null = 0;
  191. char filename[TFTP_MAX_FILENAME_LEN + 1] = { 0 };
  192. char mode[TFTP_MAX_MODE_LEN] = { 0 };
  193. u16_t filename_end_offset;
  194. u16_t mode_end_offset;
  195. if(tftp_state.handle != NULL) {
  196. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
  197. break;
  198. }
  199. sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
  200. /* find \0 in pbuf -> end of filename string */
  201. filename_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), 2);
  202. if((u16_t)(filename_end_offset-2) > sizeof(filename)) {
  203. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Filename too long/not NULL terminated");
  204. break;
  205. }
  206. pbuf_copy_partial(p, filename, filename_end_offset-2, 2);
  207. /* find \0 in pbuf -> end of mode string */
  208. mode_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), filename_end_offset+1);
  209. if((u16_t)(mode_end_offset-filename_end_offset) > sizeof(mode)) {
  210. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Mode too long/not NULL terminated");
  211. break;
  212. }
  213. pbuf_copy_partial(p, mode, mode_end_offset-filename_end_offset, filename_end_offset+1);
  214. tftp_state.handle = tftp_state.ctx->open(filename, mode, opcode == PP_HTONS(TFTP_WRQ));
  215. tftp_state.blknum = 1;
  216. if (!tftp_state.handle) {
  217. send_error(addr, port, TFTP_ERROR_FILE_NOT_FOUND, "Unable to open requested file.");
  218. break;
  219. }
  220. LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: %s request from ", (opcode == PP_HTONS(TFTP_WRQ)) ? "write" : "read"));
  221. ip_addr_debug_print(TFTP_DEBUG | LWIP_DBG_STATE, addr);
  222. LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, (" for '%s' mode '%s'\n", filename, mode));
  223. ip_addr_copy(tftp_state.addr, *addr);
  224. tftp_state.port = port;
  225. if (opcode == PP_HTONS(TFTP_WRQ)) {
  226. tftp_state.mode_write = 1;
  227. send_ack(0);
  228. } else {
  229. tftp_state.mode_write = 0;
  230. send_data();
  231. }
  232. break;
  233. }
  234. case PP_HTONS(TFTP_DATA):
  235. {
  236. int ret;
  237. u16_t blknum;
  238. if (tftp_state.handle == NULL) {
  239. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "No connection");
  240. break;
  241. }
  242. if (tftp_state.mode_write != 1) {
  243. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a write connection");
  244. break;
  245. }
  246. blknum = lwip_ntohs(sbuf[1]);
  247. pbuf_header(p, -TFTP_HEADER_LENGTH);
  248. ret = tftp_state.ctx->write(tftp_state.handle, p);
  249. if (ret < 0) {
  250. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "error writing file");
  251. close_handle();
  252. } else {
  253. send_ack(blknum);
  254. }
  255. if (p->tot_len < TFTP_MAX_PAYLOAD_SIZE) {
  256. close_handle();
  257. }
  258. break;
  259. }
  260. case PP_HTONS(TFTP_ACK):
  261. {
  262. u16_t blknum;
  263. int lastpkt;
  264. if (tftp_state.handle == NULL) {
  265. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "No connection");
  266. break;
  267. }
  268. if (tftp_state.mode_write != 0) {
  269. send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a read connection");
  270. break;
  271. }
  272. blknum = lwip_ntohs(sbuf[1]);
  273. if (blknum != tftp_state.blknum) {
  274. send_error(addr, port, TFTP_ERROR_UNKNOWN_TRFR_ID, "Wrong block number");
  275. break;
  276. }
  277. lastpkt = 0;
  278. if (tftp_state.last_data != NULL) {
  279. lastpkt = tftp_state.last_data->tot_len != (TFTP_MAX_PAYLOAD_SIZE + TFTP_HEADER_LENGTH);
  280. }
  281. if (!lastpkt) {
  282. tftp_state.blknum++;
  283. send_data();
  284. } else {
  285. close_handle();
  286. }
  287. break;
  288. }
  289. default:
  290. send_error(addr, port, TFTP_ERROR_ILLEGAL_OPERATION, "Unknown operation");
  291. break;
  292. }
  293. pbuf_free(p);
  294. }
  295. static void
  296. tftp_tmr(void* arg)
  297. {
  298. LWIP_UNUSED_ARG(arg);
  299. tftp_state.timer++;
  300. if (tftp_state.handle == NULL) {
  301. return;
  302. }
  303. sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
  304. if ((tftp_state.timer - tftp_state.last_pkt) > (TFTP_TIMEOUT_MSECS / TFTP_TIMER_MSECS)) {
  305. if ((tftp_state.last_data != NULL) && (tftp_state.retries < TFTP_MAX_RETRIES)) {
  306. LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout, retrying\n"));
  307. resend_data();
  308. tftp_state.retries++;
  309. } else {
  310. LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout\n"));
  311. close_handle();
  312. }
  313. }
  314. }
  315. /** @ingroup tftp
  316. * Initialize TFTP server.
  317. * @param ctx TFTP callback struct
  318. */
  319. err_t
  320. tftp_init(const struct tftp_context *ctx)
  321. {
  322. err_t ret;
  323. struct udp_pcb *pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
  324. if (pcb == NULL) {
  325. return ERR_MEM;
  326. }
  327. ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT);
  328. if (ret != ERR_OK) {
  329. udp_remove(pcb);
  330. return ret;
  331. }
  332. tftp_state.handle = NULL;
  333. tftp_state.port = 0;
  334. tftp_state.ctx = ctx;
  335. tftp_state.timer = 0;
  336. tftp_state.last_data = NULL;
  337. tftp_state.upcb = pcb;
  338. udp_recv(pcb, recv, NULL);
  339. return ERR_OK;
  340. }
  341. #endif /* LWIP_UDP */