Mtftp4Wrq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /** @file
  2. Routines to process Wrq (upload).
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Mtftp4Impl.h"
  7. /**
  8. Build then send a MTFTP data packet for the MTFTP upload session.
  9. @param Instance The MTFTP upload session.
  10. @param BlockNum The block number to send.
  11. @retval EFI_OUT_OF_RESOURCES Failed to build the packet.
  12. @retval EFI_ABORTED The consumer of this child directs to abort the
  13. transmission by return an error through PacketNeeded.
  14. @retval EFI_SUCCESS The data is sent.
  15. **/
  16. EFI_STATUS
  17. Mtftp4WrqSendBlock (
  18. IN OUT MTFTP4_PROTOCOL *Instance,
  19. IN UINT16 BlockNum
  20. )
  21. {
  22. EFI_MTFTP4_PACKET *Packet;
  23. EFI_MTFTP4_TOKEN *Token;
  24. NET_BUF *UdpPacket;
  25. EFI_STATUS Status;
  26. UINT16 DataLen;
  27. UINT8 *DataBuf;
  28. UINT64 Start;
  29. //
  30. // Allocate a buffer to hold the user data
  31. //
  32. UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP4_DATA_HEAD_LEN);
  33. if (UdpPacket == NULL) {
  34. return EFI_OUT_OF_RESOURCES;
  35. }
  36. Packet = (EFI_MTFTP4_PACKET *)NetbufAllocSpace (UdpPacket, MTFTP4_DATA_HEAD_LEN, FALSE);
  37. ASSERT (Packet != NULL);
  38. Packet->Data.OpCode = HTONS (EFI_MTFTP4_OPCODE_DATA);
  39. Packet->Data.Block = HTONS (BlockNum);
  40. //
  41. // Read the block from either the buffer or PacketNeeded callback
  42. //
  43. Token = Instance->Token;
  44. DataLen = Instance->BlkSize;
  45. if (Token->Buffer != NULL) {
  46. Start = MultU64x32 (BlockNum - 1, Instance->BlkSize);
  47. if (Token->BufferSize < Start + Instance->BlkSize) {
  48. DataLen = (UINT16)(Token->BufferSize - Start);
  49. Instance->LastBlock = BlockNum;
  50. Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
  51. }
  52. if (DataLen > 0) {
  53. NetbufAllocSpace (UdpPacket, DataLen, FALSE);
  54. CopyMem (Packet->Data.Data, (UINT8 *)Token->Buffer + Start, DataLen);
  55. }
  56. } else {
  57. //
  58. // Get data from PacketNeeded
  59. //
  60. DataBuf = NULL;
  61. Status = Token->PacketNeeded (
  62. &Instance->Mtftp4,
  63. Token,
  64. &DataLen,
  65. (VOID **)&DataBuf
  66. );
  67. if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) {
  68. if (DataBuf != NULL) {
  69. FreePool (DataBuf);
  70. }
  71. if (UdpPacket != NULL) {
  72. NetbufFree (UdpPacket);
  73. }
  74. Mtftp4SendError (
  75. Instance,
  76. EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
  77. (UINT8 *)"User aborted the transfer"
  78. );
  79. return EFI_ABORTED;
  80. }
  81. if (DataLen < Instance->BlkSize) {
  82. Instance->LastBlock = BlockNum;
  83. Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
  84. }
  85. if (DataLen > 0) {
  86. NetbufAllocSpace (UdpPacket, DataLen, FALSE);
  87. CopyMem (Packet->Data.Data, DataBuf, DataLen);
  88. FreePool (DataBuf);
  89. }
  90. }
  91. return Mtftp4SendPacket (Instance, UdpPacket);
  92. }
  93. /**
  94. Function to handle received ACK packet.
  95. If the ACK number matches the expected block number, and there are more
  96. data pending, send the next block. Otherwise tell the caller that we are done.
  97. @param Instance The MTFTP upload session
  98. @param Packet The MTFTP packet received
  99. @param Len The packet length
  100. @param Completed Return whether the upload has finished.
  101. @retval EFI_SUCCESS The ACK is successfully processed.
  102. @retval EFI_TFTP_ERROR The block number loops back.
  103. @retval Others Failed to transmit the next data packet.
  104. **/
  105. EFI_STATUS
  106. Mtftp4WrqHandleAck (
  107. IN MTFTP4_PROTOCOL *Instance,
  108. IN EFI_MTFTP4_PACKET *Packet,
  109. IN UINT32 Len,
  110. OUT BOOLEAN *Completed
  111. )
  112. {
  113. UINT16 AckNum;
  114. INTN Expected;
  115. UINT64 BlockCounter;
  116. *Completed = FALSE;
  117. AckNum = NTOHS (Packet->Ack.Block[0]);
  118. Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
  119. ASSERT (Expected >= 0);
  120. //
  121. // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput
  122. // restart receive.
  123. //
  124. if (Expected != AckNum) {
  125. return EFI_SUCCESS;
  126. }
  127. //
  128. // Remove the acked block number, if this is the last block number,
  129. // tell the Mtftp4WrqInput to finish the transfer. This is the last
  130. // block number if the block range are empty.
  131. //
  132. Mtftp4RemoveBlockNum (&Instance->Blocks, AckNum, *Completed, &BlockCounter);
  133. Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
  134. if (Expected < 0) {
  135. //
  136. // The block range is empty. It may either because the last
  137. // block has been ACKed, or the sequence number just looped back,
  138. // that is, there is more than 0xffff blocks.
  139. //
  140. if (Instance->LastBlock == AckNum) {
  141. ASSERT (Instance->LastBlock >= 1);
  142. *Completed = TRUE;
  143. return EFI_SUCCESS;
  144. } else {
  145. Mtftp4SendError (
  146. Instance,
  147. EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
  148. (UINT8 *)"Block number rolls back, not supported, try blksize option"
  149. );
  150. return EFI_TFTP_ERROR;
  151. }
  152. }
  153. return Mtftp4WrqSendBlock (Instance, (UINT16)Expected);
  154. }
  155. /**
  156. Check whether the received OACK is valid.
  157. The OACK is valid only if:
  158. 1. It only include options requested by us
  159. 2. It can only include a smaller block size
  160. 3. It can't change the proposed time out value.
  161. 4. Other requirements of the individal MTFTP options as required.
  162. @param Reply The options included in the OACK
  163. @param Request The options we requested
  164. @retval TRUE The options included in OACK is valid.
  165. @retval FALSE The options included in OACK is invalid.
  166. **/
  167. BOOLEAN
  168. Mtftp4WrqOackValid (
  169. IN MTFTP4_OPTION *Reply,
  170. IN MTFTP4_OPTION *Request
  171. )
  172. {
  173. //
  174. // It is invalid for server to return options we don't request
  175. //
  176. if ((Reply->Exist & ~Request->Exist) != 0) {
  177. return FALSE;
  178. }
  179. //
  180. // Server can only specify a smaller block size to be used and
  181. // return the timeout matches that requested.
  182. //
  183. if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0) && (Reply->BlkSize > Request->BlkSize)) ||
  184. (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout)))
  185. {
  186. return FALSE;
  187. }
  188. return TRUE;
  189. }
  190. /**
  191. Function to handle the MTFTP OACK packet.
  192. It parses the packet's options, and update the internal states of the session.
  193. @param Instance The MTFTP session
  194. @param Packet The received OACK packet
  195. @param Len The length of the packet
  196. @param Completed Whether the transmission has completed. NOT used by
  197. this function.
  198. @retval EFI_SUCCESS The OACK process is OK
  199. @retval EFI_TFTP_ERROR Some error occurred, and the session reset.
  200. **/
  201. EFI_STATUS
  202. Mtftp4WrqHandleOack (
  203. IN OUT MTFTP4_PROTOCOL *Instance,
  204. IN EFI_MTFTP4_PACKET *Packet,
  205. IN UINT32 Len,
  206. OUT BOOLEAN *Completed
  207. )
  208. {
  209. MTFTP4_OPTION Reply;
  210. EFI_MTFTP4_PACKET Bogus;
  211. EFI_STATUS Status;
  212. INTN Expected;
  213. *Completed = FALSE;
  214. //
  215. // Ignore the OACK if already started the upload
  216. //
  217. Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
  218. if (Expected != 0) {
  219. return EFI_SUCCESS;
  220. }
  221. //
  222. // Parse and validate the options from server
  223. //
  224. ZeroMem (&Reply, sizeof (MTFTP4_OPTION));
  225. Status = Mtftp4ParseOptionOack (Packet, Len, Instance->Operation, &Reply);
  226. if (EFI_ERROR (Status) || !Mtftp4WrqOackValid (&Reply, &Instance->RequestOption)) {
  227. //
  228. // Don't send a MTFTP error packet when out of resource, it can
  229. // only make it worse.
  230. //
  231. if (Status != EFI_OUT_OF_RESOURCES) {
  232. Mtftp4SendError (
  233. Instance,
  234. EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,
  235. (UINT8 *)"Malformatted OACK packet"
  236. );
  237. }
  238. return EFI_TFTP_ERROR;
  239. }
  240. if (Reply.BlkSize != 0) {
  241. Instance->BlkSize = Reply.BlkSize;
  242. }
  243. if (Reply.Timeout != 0) {
  244. Instance->Timeout = Reply.Timeout;
  245. }
  246. //
  247. // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck,
  248. // which will start the transmission of the first data block.
  249. //
  250. Bogus.Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK);
  251. Bogus.Ack.Block[0] = 0;
  252. Status = Mtftp4WrqHandleAck (
  253. Instance,
  254. &Bogus,
  255. sizeof (EFI_MTFTP4_ACK_HEADER),
  256. Completed
  257. );
  258. return Status;
  259. }
  260. /**
  261. The input process routine for MTFTP upload.
  262. @param UdpPacket The received MTFTP packet.
  263. @param EndPoint The local/remote access point
  264. @param IoStatus The result of the packet receiving
  265. @param Context Opaque parameter for the callback, which is the
  266. MTFTP session.
  267. **/
  268. VOID
  269. EFIAPI
  270. Mtftp4WrqInput (
  271. IN NET_BUF *UdpPacket,
  272. IN UDP_END_POINT *EndPoint,
  273. IN EFI_STATUS IoStatus,
  274. IN VOID *Context
  275. )
  276. {
  277. MTFTP4_PROTOCOL *Instance;
  278. EFI_MTFTP4_PACKET *Packet;
  279. BOOLEAN Completed;
  280. EFI_STATUS Status;
  281. UINT32 Len;
  282. UINT16 Opcode;
  283. Instance = (MTFTP4_PROTOCOL *)Context;
  284. NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE);
  285. Completed = FALSE;
  286. Packet = NULL;
  287. Status = EFI_SUCCESS;
  288. if (EFI_ERROR (IoStatus)) {
  289. Status = IoStatus;
  290. goto ON_EXIT;
  291. }
  292. ASSERT (UdpPacket != NULL);
  293. if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) {
  294. goto ON_EXIT;
  295. }
  296. //
  297. // Client send initial request to server's listening port. Server
  298. // will select a UDP port to communicate with the client.
  299. //
  300. if (EndPoint->RemotePort != Instance->ConnectedPort) {
  301. if (Instance->ConnectedPort != 0) {
  302. goto ON_EXIT;
  303. } else {
  304. Instance->ConnectedPort = EndPoint->RemotePort;
  305. }
  306. }
  307. //
  308. // Copy the MTFTP packet to a continuous buffer if it isn't already so.
  309. //
  310. Len = UdpPacket->TotalSize;
  311. if (UdpPacket->BlockOpNum > 1) {
  312. Packet = AllocatePool (Len);
  313. if (Packet == NULL) {
  314. Status = EFI_OUT_OF_RESOURCES;
  315. goto ON_EXIT;
  316. }
  317. NetbufCopy (UdpPacket, 0, Len, (UINT8 *)Packet);
  318. } else {
  319. Packet = (EFI_MTFTP4_PACKET *)NetbufGetByte (UdpPacket, 0, NULL);
  320. ASSERT (Packet != NULL);
  321. }
  322. Opcode = NTOHS (Packet->OpCode);
  323. //
  324. // Call the user's CheckPacket if provided. Abort the transmission
  325. // if CheckPacket returns an EFI_ERROR code.
  326. //
  327. if ((Instance->Token->CheckPacket != NULL) &&
  328. ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR)))
  329. {
  330. Status = Instance->Token->CheckPacket (
  331. &Instance->Mtftp4,
  332. Instance->Token,
  333. (UINT16)Len,
  334. Packet
  335. );
  336. if (EFI_ERROR (Status)) {
  337. //
  338. // Send an error message to the server to inform it
  339. //
  340. if (Opcode != EFI_MTFTP4_OPCODE_ERROR) {
  341. Mtftp4SendError (
  342. Instance,
  343. EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
  344. (UINT8 *)"User aborted the transfer"
  345. );
  346. }
  347. Status = EFI_ABORTED;
  348. goto ON_EXIT;
  349. }
  350. }
  351. switch (Opcode) {
  352. case EFI_MTFTP4_OPCODE_ACK:
  353. if (Len != MTFTP4_OPCODE_LEN + MTFTP4_BLKNO_LEN) {
  354. goto ON_EXIT;
  355. }
  356. Status = Mtftp4WrqHandleAck (Instance, Packet, Len, &Completed);
  357. break;
  358. case EFI_MTFTP4_OPCODE_OACK:
  359. if (Len <= MTFTP4_OPCODE_LEN) {
  360. goto ON_EXIT;
  361. }
  362. Status = Mtftp4WrqHandleOack (Instance, Packet, Len, &Completed);
  363. break;
  364. case EFI_MTFTP4_OPCODE_ERROR:
  365. Status = EFI_TFTP_ERROR;
  366. break;
  367. default:
  368. break;
  369. }
  370. ON_EXIT:
  371. //
  372. // Free the resources, then if !EFI_ERROR (Status) and not completed,
  373. // restart the receive, otherwise end the session.
  374. //
  375. if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) {
  376. FreePool (Packet);
  377. }
  378. if (UdpPacket != NULL) {
  379. NetbufFree (UdpPacket);
  380. }
  381. if (!EFI_ERROR (Status) && !Completed) {
  382. Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
  383. }
  384. //
  385. // Status may have been updated by UdpIoRecvDatagram
  386. //
  387. if (EFI_ERROR (Status) || Completed) {
  388. Mtftp4CleanOperation (Instance, Status);
  389. }
  390. }
  391. /**
  392. Start the MTFTP session for upload.
  393. It will first init some states, then send the WRQ request packet,
  394. and start receiving the packet.
  395. @param Instance The MTFTP session
  396. @param Operation Redundant parameter, which is always
  397. EFI_MTFTP4_OPCODE_WRQ here.
  398. @retval EFI_SUCCESS The upload process has been started.
  399. @retval Others Failed to start the upload.
  400. **/
  401. EFI_STATUS
  402. Mtftp4WrqStart (
  403. IN MTFTP4_PROTOCOL *Instance,
  404. IN UINT16 Operation
  405. )
  406. {
  407. EFI_STATUS Status;
  408. //
  409. // The valid block number range are [0, 0xffff]. For example:
  410. // the client sends an WRQ request to the server, the server
  411. // ACK with an ACK0 to let client start transfer the first
  412. // packet.
  413. //
  414. Status = Mtftp4InitBlockRange (&Instance->Blocks, 0, 0xffff);
  415. if (EFI_ERROR (Status)) {
  416. return Status;
  417. }
  418. Status = Mtftp4SendRequest (Instance);
  419. if (EFI_ERROR (Status)) {
  420. return Status;
  421. }
  422. return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
  423. }