PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/external/qemu/slirp-android/tftp.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C | 431 lines | 293 code | 108 blank | 30 comment | 51 complexity | 941c47eeb4a64515734d3554fb99e127 MD5 | raw file
  1. /*
  2. * tftp.c - a simple, read-only tftp server for qemu
  3. *
  4. * Copyright (c) 2004 Magnus Damm <damm@opensource.se>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include <slirp.h>
  25. #include "qemu-common.h" // for pstrcpy
  26. struct tftp_session {
  27. int in_use;
  28. unsigned char filename[TFTP_FILENAME_MAX];
  29. uint32_t client_ip;
  30. uint16_t client_port;
  31. int timestamp;
  32. };
  33. static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
  34. const char *tftp_prefix;
  35. static void tftp_session_update(struct tftp_session *spt)
  36. {
  37. spt->timestamp = curtime;
  38. spt->in_use = 1;
  39. }
  40. static void tftp_session_terminate(struct tftp_session *spt)
  41. {
  42. spt->in_use = 0;
  43. }
  44. static int tftp_session_allocate(struct tftp_t *tp)
  45. {
  46. struct tftp_session *spt;
  47. int k;
  48. for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
  49. spt = &tftp_sessions[k];
  50. if (!spt->in_use)
  51. goto found;
  52. /* sessions time out after 5 inactive seconds */
  53. if ((int)(curtime - spt->timestamp) > 5000)
  54. goto found;
  55. }
  56. return -1;
  57. found:
  58. memset(spt, 0, sizeof(*spt));
  59. spt->client_ip = ip_geth(tp->ip.ip_src);
  60. spt->client_port = port_geth(tp->udp.uh_sport);
  61. tftp_session_update(spt);
  62. return k;
  63. }
  64. static int tftp_session_find(struct tftp_t *tp)
  65. {
  66. struct tftp_session *spt;
  67. int k;
  68. for (k = 0; k < TFTP_SESSIONS_MAX; k++) {
  69. spt = &tftp_sessions[k];
  70. if (spt->in_use) {
  71. if (spt->client_ip == ip_geth(tp->ip.ip_src)) {
  72. if (spt->client_port == port_geth(tp->udp.uh_sport)) {
  73. return k;
  74. }
  75. }
  76. }
  77. }
  78. return -1;
  79. }
  80. static int tftp_read_data(struct tftp_session *spt, u_int16_t block_nr,
  81. u_int8_t *buf, int len)
  82. {
  83. int fd;
  84. int bytes_read = 0;
  85. char buffer[1024];
  86. int n;
  87. n = snprintf(buffer, sizeof(buffer), "%s/%s",
  88. tftp_prefix, spt->filename);
  89. if (n >= sizeof(buffer))
  90. return -1;
  91. fd = open(buffer, O_RDONLY | O_BINARY);
  92. if (fd < 0) {
  93. return -1;
  94. }
  95. if (len) {
  96. lseek(fd, block_nr * 512, SEEK_SET);
  97. bytes_read = read(fd, buf, len);
  98. }
  99. close(fd);
  100. return bytes_read;
  101. }
  102. static int tftp_send_oack(struct tftp_session *spt,
  103. const char *key, uint32_t value,
  104. struct tftp_t *recv_tp)
  105. {
  106. SockAddress saddr, daddr;
  107. struct mbuf *m;
  108. struct tftp_t *tp;
  109. int n = 0;
  110. m = m_get();
  111. if (!m)
  112. return -1;
  113. memset(m->m_data, 0, m->m_size);
  114. m->m_data += IF_MAXLINKHDR;
  115. tp = (void *)m->m_data;
  116. m->m_data += sizeof(struct udpiphdr);
  117. tp->tp_op = htons(TFTP_OACK);
  118. n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
  119. key) + 1;
  120. n += snprintf((char *)tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
  121. value) + 1;
  122. sock_address_init_inet( &saddr,
  123. ip_geth(recv_tp->ip.ip_dst),
  124. port_geth(recv_tp->udp.uh_dport) );
  125. sock_address_init_inet( &daddr,
  126. spt->client_ip,
  127. spt->client_port );
  128. m->m_len = sizeof(struct tftp_t) - 514 + n -
  129. sizeof(struct ip) - sizeof(struct udphdr);
  130. udp_output2_(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
  131. return 0;
  132. }
  133. static int tftp_send_error(struct tftp_session *spt,
  134. u_int16_t errorcode, const char *msg,
  135. struct tftp_t *recv_tp)
  136. {
  137. SockAddress saddr, daddr;
  138. struct mbuf *m;
  139. struct tftp_t *tp;
  140. int nobytes;
  141. m = m_get();
  142. if (!m) {
  143. return -1;
  144. }
  145. memset(m->m_data, 0, m->m_size);
  146. m->m_data += IF_MAXLINKHDR;
  147. tp = (void *)m->m_data;
  148. m->m_data += sizeof(struct udpiphdr);
  149. tp->tp_op = htons(TFTP_ERROR);
  150. tp->x.tp_error.tp_error_code = htons(errorcode);
  151. pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
  152. sock_address_init_inet( &saddr,
  153. ip_geth(recv_tp->ip.ip_dst),
  154. port_geth(recv_tp->udp.uh_dport) );
  155. sock_address_init_inet( &daddr,
  156. spt->client_ip,
  157. spt->client_port );
  158. nobytes = 2;
  159. m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
  160. sizeof(struct ip) - sizeof(struct udphdr);
  161. udp_output2_(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
  162. tftp_session_terminate(spt);
  163. return 0;
  164. }
  165. static int tftp_send_data(struct tftp_session *spt,
  166. u_int16_t block_nr,
  167. struct tftp_t *recv_tp)
  168. {
  169. SockAddress saddr, daddr;
  170. struct mbuf *m;
  171. struct tftp_t *tp;
  172. int nobytes;
  173. if (block_nr < 1) {
  174. return -1;
  175. }
  176. m = m_get();
  177. if (!m) {
  178. return -1;
  179. }
  180. memset(m->m_data, 0, m->m_size);
  181. m->m_data += IF_MAXLINKHDR;
  182. tp = (void *)m->m_data;
  183. m->m_data += sizeof(struct udpiphdr);
  184. tp->tp_op = htons(TFTP_DATA);
  185. tp->x.tp_data.tp_block_nr = htons(block_nr);
  186. sock_address_init_inet( &saddr,
  187. ip_geth(recv_tp->ip.ip_dst),
  188. port_geth(recv_tp->udp.uh_dport) );
  189. sock_address_init_inet( &daddr,
  190. spt->client_ip,
  191. spt->client_port );
  192. nobytes = tftp_read_data(spt, block_nr - 1, tp->x.tp_data.tp_buf, 512);
  193. if (nobytes < 0) {
  194. m_free(m);
  195. /* send "file not found" error back */
  196. tftp_send_error(spt, 1, "File not found", tp);
  197. return -1;
  198. }
  199. m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
  200. sizeof(struct ip) - sizeof(struct udphdr);
  201. udp_output2_(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
  202. if (nobytes == 512) {
  203. tftp_session_update(spt);
  204. }
  205. else {
  206. tftp_session_terminate(spt);
  207. }
  208. return 0;
  209. }
  210. static void tftp_handle_rrq(struct tftp_t *tp, int pktlen)
  211. {
  212. struct tftp_session *spt;
  213. int s, k, n;
  214. u_int8_t *src, *dst;
  215. s = tftp_session_allocate(tp);
  216. if (s < 0) {
  217. return;
  218. }
  219. spt = &tftp_sessions[s];
  220. src = tp->x.tp_buf;
  221. dst = spt->filename;
  222. n = pktlen - ((uint8_t *)&tp->x.tp_buf[0] - (uint8_t *)tp);
  223. /* get name */
  224. for (k = 0; k < n; k++) {
  225. if (k < TFTP_FILENAME_MAX) {
  226. dst[k] = src[k];
  227. }
  228. else {
  229. return;
  230. }
  231. if (src[k] == '\0') {
  232. break;
  233. }
  234. }
  235. if (k >= n) {
  236. return;
  237. }
  238. k++;
  239. /* check mode */
  240. if ((n - k) < 6) {
  241. return;
  242. }
  243. if (memcmp(&src[k], "octet\0", 6) != 0) {
  244. tftp_send_error(spt, 4, "Unsupported transfer mode", tp);
  245. return;
  246. }
  247. k += 6; /* skipping octet */
  248. /* do sanity checks on the filename */
  249. if ((spt->filename[0] != '/')
  250. || (spt->filename[strlen((char *)spt->filename) - 1] == '/')
  251. || strstr((char *)spt->filename, "/../")) {
  252. tftp_send_error(spt, 2, "Access violation", tp);
  253. return;
  254. }
  255. /* only allow exported prefixes */
  256. if (!tftp_prefix) {
  257. tftp_send_error(spt, 2, "Access violation", tp);
  258. return;
  259. }
  260. /* check if the file exists */
  261. if (tftp_read_data(spt, 0, spt->filename, 0) < 0) {
  262. tftp_send_error(spt, 1, "File not found", tp);
  263. return;
  264. }
  265. if (src[n - 1] != 0) {
  266. tftp_send_error(spt, 2, "Access violation", tp);
  267. return;
  268. }
  269. while (k < n) {
  270. const char *key, *value;
  271. key = (char *)src + k;
  272. k += strlen(key) + 1;
  273. if (k >= n) {
  274. tftp_send_error(spt, 2, "Access violation", tp);
  275. return;
  276. }
  277. value = (char *)src + k;
  278. k += strlen(value) + 1;
  279. if (strcmp(key, "tsize") == 0) {
  280. int tsize = atoi(value);
  281. struct stat stat_p;
  282. if (tsize == 0 && tftp_prefix) {
  283. char buffer[1024];
  284. int len;
  285. len = snprintf(buffer, sizeof(buffer), "%s/%s",
  286. tftp_prefix, spt->filename);
  287. if (stat(buffer, &stat_p) == 0)
  288. tsize = stat_p.st_size;
  289. else {
  290. tftp_send_error(spt, 1, "File not found", tp);
  291. return;
  292. }
  293. }
  294. tftp_send_oack(spt, "tsize", tsize, tp);
  295. }
  296. }
  297. tftp_send_data(spt, 1, tp);
  298. }
  299. static void tftp_handle_ack(struct tftp_t *tp, int pktlen)
  300. {
  301. int s;
  302. s = tftp_session_find(tp);
  303. if (s < 0) {
  304. return;
  305. }
  306. if (tftp_send_data(&tftp_sessions[s],
  307. ntohs(tp->x.tp_data.tp_block_nr) + 1,
  308. tp) < 0) {
  309. return;
  310. }
  311. }
  312. void tftp_input(struct mbuf *m)
  313. {
  314. struct tftp_t *tp = (struct tftp_t *)m->m_data;
  315. switch(ntohs(tp->tp_op)) {
  316. case TFTP_RRQ:
  317. tftp_handle_rrq(tp, m->m_len);
  318. break;
  319. case TFTP_ACK:
  320. tftp_handle_ack(tp, m->m_len);
  321. break;
  322. }
  323. }