TcpFunc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /** @file
  2. Declaration of external functions shared in TCP driver.
  3. Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _TCP_FUNC_H_
  7. #define _TCP_FUNC_H_
  8. #include "TcpOption.h"
  9. #define TCP_COMP_VAL(Min, Max, Default, Val) \
  10. ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
  11. /**
  12. Timeout handler prototype.
  13. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  14. **/
  15. typedef
  16. VOID
  17. (*TCP_TIMER_HANDLER) (
  18. IN OUT TCP_CB *Tcb
  19. );
  20. //
  21. // Functions in TcpMisc.c
  22. //
  23. /**
  24. Initialize the Tcb locally related members.
  25. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  26. **/
  27. VOID
  28. TcpInitTcbLocal (
  29. IN OUT TCP_CB *Tcb
  30. );
  31. /**
  32. Initialize the peer related members.
  33. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  34. @param[in] Seg Pointer to the segment that contains the peer's initial information.
  35. @param[in] Opt Pointer to the options announced by the peer.
  36. **/
  37. VOID
  38. TcpInitTcbPeer (
  39. IN OUT TCP_CB *Tcb,
  40. IN TCP_SEG *Seg,
  41. IN TCP_OPTION *Opt
  42. );
  43. /**
  44. Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
  45. @param[in] Addr Pointer to the IP address needs to match.
  46. @param[in] Port The port number needs to match.
  47. @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack.
  48. IP_VERSION_6 indicates TCP is running on IP6 stack.
  49. @retval TRUE The Tcb which matches the <Addr Port> pairs exists.
  50. @retval FALSE Otherwise
  51. **/
  52. BOOLEAN
  53. TcpFindTcbByPeer (
  54. IN EFI_IP_ADDRESS *Addr,
  55. IN TCP_PORTNO Port,
  56. IN UINT8 Version
  57. );
  58. /**
  59. Locate the TCP_CB related to the socket pair.
  60. @param[in] LocalPort The local port number.
  61. @param[in] LocalIp The local IP address.
  62. @param[in] RemotePort The remote port number.
  63. @param[in] RemoteIp The remote IP address.
  64. @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
  65. IP_VERSION_6 indicates TCP is running on IP6 stack.
  66. @param[in] Syn If TRUE, the listen sockets are searched.
  67. @return Pointer to the related TCP_CB. If NULL, no match is found.
  68. **/
  69. TCP_CB *
  70. TcpLocateTcb (
  71. IN TCP_PORTNO LocalPort,
  72. IN EFI_IP_ADDRESS *LocalIp,
  73. IN TCP_PORTNO RemotePort,
  74. IN EFI_IP_ADDRESS *RemoteIp,
  75. IN UINT8 Version,
  76. IN BOOLEAN Syn
  77. );
  78. /**
  79. Insert a Tcb into the proper queue.
  80. @param[in] Tcb Pointer to the TCP_CB to be inserted.
  81. @retval 0 The Tcb was inserted successfully.
  82. @retval -1 An error condition occurred.
  83. **/
  84. INTN
  85. TcpInsertTcb (
  86. IN TCP_CB *Tcb
  87. );
  88. /**
  89. Clone a TCP_CB from Tcb.
  90. @param[in] Tcb Pointer to the TCP_CB to be cloned.
  91. @return Pointer to the new cloned TCP_CB. If NULL, an error condition occurred.
  92. **/
  93. TCP_CB *
  94. TcpCloneTcb (
  95. IN TCP_CB *Tcb
  96. );
  97. /**
  98. Compute an ISS to be used by a new connection.
  99. @return The result ISS.
  100. **/
  101. TCP_SEQNO
  102. TcpGetIss (
  103. VOID
  104. );
  105. /**
  106. Get the local mss.
  107. @param[in] Sock Pointer to the socket to get mss.
  108. @return The mss size.
  109. **/
  110. UINT16
  111. TcpGetRcvMss (
  112. IN SOCKET *Sock
  113. );
  114. /**
  115. Set the Tcb's state.
  116. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  117. @param[in] State The state to be set.
  118. **/
  119. VOID
  120. TcpSetState (
  121. IN TCP_CB *Tcb,
  122. IN UINT8 State
  123. );
  124. /**
  125. Compute the TCP segment's checksum.
  126. @param[in] Nbuf Pointer to the buffer that contains the TCP segment.
  127. @param[in] HeadSum The checksum value of the fixed part of pseudo header.
  128. @return The checksum value.
  129. **/
  130. UINT16
  131. TcpChecksum (
  132. IN NET_BUF *Nbuf,
  133. IN UINT16 HeadSum
  134. );
  135. /**
  136. Translate the information from the head of the received TCP
  137. segment Nbuf contains, and fill it into a TCP_SEG structure.
  138. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  139. @param[in, out] Nbuf Pointer to the buffer contains the TCP segment.
  140. @return Pointer to the TCP_SEG that contains the translated TCP head information.
  141. **/
  142. TCP_SEG *
  143. TcpFormatNetbuf (
  144. IN TCP_CB *Tcb,
  145. IN OUT NET_BUF *Nbuf
  146. );
  147. /**
  148. Initialize an active connection,
  149. @param[in, out] Tcb Pointer to the TCP_CB that wants to initiate a
  150. connection.
  151. **/
  152. VOID
  153. TcpOnAppConnect (
  154. IN OUT TCP_CB *Tcb
  155. );
  156. /**
  157. Application has consumed some data, check whether
  158. to send a window update ack or a delayed ack.
  159. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  160. **/
  161. VOID
  162. TcpOnAppConsume (
  163. IN TCP_CB *Tcb
  164. );
  165. /**
  166. Initiate the connection close procedure, called when
  167. applications want to close the connection.
  168. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  169. **/
  170. VOID
  171. TcpOnAppClose (
  172. IN OUT TCP_CB *Tcb
  173. );
  174. /**
  175. Check whether the application's newly delivered data can be sent out.
  176. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  177. @retval 0 The data has been sent out successfully.
  178. @retval -1 The Tcb is not in a state that data is permitted to
  179. be sent out.
  180. **/
  181. INTN
  182. TcpOnAppSend (
  183. IN OUT TCP_CB *Tcb
  184. );
  185. /**
  186. Abort the connection by sending a reset segment: called
  187. when the application wants to abort the connection.
  188. @param[in] Tcb Pointer to the TCP_CB of the TCP instance.
  189. **/
  190. VOID
  191. TcpOnAppAbort (
  192. IN TCP_CB *Tcb
  193. );
  194. /**
  195. Reset the connection related with Tcb.
  196. @param[in] Tcb Pointer to the TCP_CB of the connection to be reset.
  197. **/
  198. VOID
  199. TcpResetConnection (
  200. IN TCP_CB *Tcb
  201. );
  202. /**
  203. Install the device path protocol on the TCP instance.
  204. @param[in] Sock Pointer to the socket representing the TCP instance.
  205. @retval EFI_SUCCESS The device path protocol installed.
  206. @retval other Failed to install the device path protocol.
  207. **/
  208. EFI_STATUS
  209. TcpInstallDevicePath (
  210. IN SOCKET *Sock
  211. );
  212. //
  213. // Functions in TcpOutput.c
  214. //
  215. /**
  216. Compute the sequence space left in the old receive window.
  217. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  218. @return The sequence space left in the old receive window.
  219. **/
  220. UINT32
  221. TcpRcvWinOld (
  222. IN TCP_CB *Tcb
  223. );
  224. /**
  225. Compute the current receive window.
  226. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  227. @return The size of the current receive window, in bytes.
  228. **/
  229. UINT32
  230. TcpRcvWinNow (
  231. IN TCP_CB *Tcb
  232. );
  233. /**
  234. Get the maximum SndNxt.
  235. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  236. @return The sequence number of the maximum SndNxt.
  237. **/
  238. TCP_SEQNO
  239. TcpGetMaxSndNxt (
  240. IN TCP_CB *Tcb
  241. );
  242. /**
  243. Compute how much data to send.
  244. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  245. @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
  246. and send out data by force.
  247. @return The length of the data that can be sent. If 0, no data can be sent.
  248. **/
  249. UINT32
  250. TcpDataToSend (
  251. IN TCP_CB *Tcb,
  252. IN INTN Force
  253. );
  254. /**
  255. Retransmit the segment from sequence Seq.
  256. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  257. @param[in] Seq The sequence number of the segment to be retransmitted.
  258. @retval 0 The retransmission succeeded.
  259. @retval -1 An error condition occurred.
  260. **/
  261. INTN
  262. TcpRetransmit (
  263. IN TCP_CB *Tcb,
  264. IN TCP_SEQNO Seq
  265. );
  266. /**
  267. Check whether to send data/SYN/FIN and piggyback an ACK.
  268. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  269. @param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
  270. and send out data by force.
  271. @return The number of bytes sent.
  272. **/
  273. INTN
  274. TcpToSendData (
  275. IN OUT TCP_CB *Tcb,
  276. IN INTN Force
  277. );
  278. /**
  279. Check whether to send an ACK or delayed ACK.
  280. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  281. **/
  282. VOID
  283. TcpToSendAck (
  284. IN OUT TCP_CB *Tcb
  285. );
  286. /**
  287. Send an ACK immediately.
  288. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  289. **/
  290. VOID
  291. TcpSendAck (
  292. IN OUT TCP_CB *Tcb
  293. );
  294. /**
  295. Send a zero probe segment. It can be used by keepalive and zero window probe.
  296. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  297. @retval 0 The zero probe segment was sent out successfully.
  298. @retval other An error condition occurred.
  299. **/
  300. INTN
  301. TcpSendZeroProbe (
  302. IN OUT TCP_CB *Tcb
  303. );
  304. /**
  305. Send a RESET segment in response to the segment received.
  306. @param[in] Tcb Pointer to the TCP_CB of this TCP instance, may be NULL.
  307. @param[in] Head TCP header of the segment that triggers the reset.
  308. @param[in] Len Length of the segment that triggers the reset.
  309. @param[in] Local Local IP address.
  310. @param[in] Remote Remote peer's IP address.
  311. @param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
  312. IP_VERSION_6 indicates TCP is running on IP6 stack.
  313. @retval 0 A reset is sent or no need to send it.
  314. @retval -1 No reset is sent.
  315. **/
  316. INTN
  317. TcpSendReset (
  318. IN TCP_CB *Tcb,
  319. IN TCP_HEAD *Head,
  320. IN INT32 Len,
  321. IN EFI_IP_ADDRESS *Local,
  322. IN EFI_IP_ADDRESS *Remote,
  323. IN UINT8 Version
  324. );
  325. /**
  326. Verify that the segment is in good shape.
  327. @param[in] Nbuf Buffer that contains the segment to be checked.
  328. @retval 0 The segment is broken.
  329. @retval 1 The segment is in good shape.
  330. **/
  331. INTN
  332. TcpVerifySegment (
  333. IN NET_BUF *Nbuf
  334. );
  335. //
  336. // Functions from TcpInput.c
  337. //
  338. /**
  339. Process the received ICMP error messages for TCP.
  340. @param[in] Nbuf Buffer that contains part of the TCP segment without IP header
  341. truncated from the ICMP error packet.
  342. @param[in] IcmpErr The ICMP error code interpreted from an ICMP error packet.
  343. @param[in] Src Source address of the ICMP error message.
  344. @param[in] Dst Destination address of the ICMP error message.
  345. @param[in] Version IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates
  346. IP6 stack.
  347. **/
  348. VOID
  349. TcpIcmpInput (
  350. IN NET_BUF *Nbuf,
  351. IN UINT8 IcmpErr,
  352. IN EFI_IP_ADDRESS *Src,
  353. IN EFI_IP_ADDRESS *Dst,
  354. IN UINT8 Version
  355. );
  356. /**
  357. Process the received TCP segments.
  358. @param[in] Nbuf Buffer that contains received TCP segment without an IP header.
  359. @param[in] Src Source address of the segment, or the peer's IP address.
  360. @param[in] Dst Destination address of the segment, or the local end's IP
  361. address.
  362. @param[in] Version IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates
  363. IP6 stack.
  364. @retval 0 The segment processed successfully. It is either accepted or
  365. discarded. But no connection is reset by the segment.
  366. @retval -1 A connection is reset by the segment.
  367. **/
  368. INTN
  369. TcpInput (
  370. IN NET_BUF *Nbuf,
  371. IN EFI_IP_ADDRESS *Src,
  372. IN EFI_IP_ADDRESS *Dst,
  373. IN UINT8 Version
  374. );
  375. //
  376. // Functions in TcpTimer.c
  377. //
  378. /**
  379. Close the TCP connection.
  380. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  381. **/
  382. VOID
  383. TcpClose (
  384. IN OUT TCP_CB *Tcb
  385. );
  386. /**
  387. Heart beat timer handler, queues the DPC at TPL_CALLBACK.
  388. @param[in] Event Timer event signaled, ignored.
  389. @param[in] Context Context of the timer event, ignored.
  390. **/
  391. VOID
  392. EFIAPI
  393. TcpTicking (
  394. IN EFI_EVENT Event,
  395. IN VOID *Context
  396. );
  397. /**
  398. Enable a TCP timer.
  399. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  400. @param[in] Timer The index of the timer to be enabled.
  401. @param[in] TimeOut The timeout value of this timer.
  402. **/
  403. VOID
  404. TcpSetTimer (
  405. IN OUT TCP_CB *Tcb,
  406. IN UINT16 Timer,
  407. IN UINT32 TimeOut
  408. );
  409. /**
  410. Clear one TCP timer.
  411. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  412. @param[in] Timer The index of the timer to be cleared.
  413. **/
  414. VOID
  415. TcpClearTimer (
  416. IN OUT TCP_CB *Tcb,
  417. IN UINT16 Timer
  418. );
  419. /**
  420. Clear all TCP timers.
  421. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  422. **/
  423. VOID
  424. TcpClearAllTimer (
  425. IN OUT TCP_CB *Tcb
  426. );
  427. /**
  428. Enable the window prober timer and set the timeout value.
  429. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  430. **/
  431. VOID
  432. TcpSetProbeTimer (
  433. IN OUT TCP_CB *Tcb
  434. );
  435. /**
  436. Enable the keepalive timer and set the timeout value.
  437. @param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
  438. **/
  439. VOID
  440. TcpSetKeepaliveTimer (
  441. IN OUT TCP_CB *Tcb
  442. );
  443. //
  444. // Functions in TcpIo.c
  445. //
  446. /**
  447. Packet receive callback function provided to IP_IO. Used to call
  448. the proper function to handle the packet received by IP.
  449. @param[in] Status Result of the receive request.
  450. @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
  451. @param[in] NetSession The IP session for the received packet.
  452. @param[in] Pkt Packet received.
  453. @param[in] Context The data provided by the user for the received packet when
  454. the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
  455. This is an optional parameter that may be NULL.
  456. **/
  457. VOID
  458. EFIAPI
  459. TcpRxCallback (
  460. IN EFI_STATUS Status,
  461. IN UINT8 IcmpErr,
  462. IN EFI_NET_SESSION_DATA *NetSession,
  463. IN NET_BUF *Pkt,
  464. IN VOID *Context OPTIONAL
  465. );
  466. /**
  467. Send the segment to IP via IpIo function.
  468. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  469. @param[in] Nbuf Pointer to the TCP segment to be sent.
  470. @param[in] Src Source address of the TCP segment.
  471. @param[in] Dest Destination address of the TCP segment.
  472. @param[in] Version IP_VERSION_4 or IP_VERSION_6
  473. @retval 0 The segment was sent out successfully.
  474. @retval -1 The segment failed to be sent.
  475. **/
  476. INTN
  477. TcpSendIpPacket (
  478. IN TCP_CB *Tcb,
  479. IN NET_BUF *Nbuf,
  480. IN EFI_IP_ADDRESS *Src,
  481. IN EFI_IP_ADDRESS *Dest,
  482. IN UINT8 Version
  483. );
  484. /**
  485. Refresh the remote peer's Neighbor Cache State if already exists.
  486. @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
  487. @param[in] Neighbor Source address of the TCP segment.
  488. @param[in] Timeout Time in 100-ns units that this entry will remain
  489. in the neighbor cache. A value of zero means that
  490. the entry is permanent. A value of non-zero means
  491. that the entry is dynamic and will be deleted
  492. after Timeout.
  493. @retval EFI_SUCCESS Successfully updated the neighbor relationship.
  494. @retval EFI_NOT_STARTED The IpIo is not configured.
  495. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  496. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
  497. @retval EFI_NOT_FOUND This entry is not in the neighbor table.
  498. **/
  499. EFI_STATUS
  500. Tcp6RefreshNeighbor (
  501. IN TCP_CB *Tcb,
  502. IN EFI_IP_ADDRESS *Neighbor,
  503. IN UINT32 Timeout
  504. );
  505. //
  506. // Functions in TcpDispatcher.c
  507. //
  508. /**
  509. The protocol handler provided to the socket layer, used to
  510. dispatch the socket level requests by calling the corresponding
  511. TCP layer functions.
  512. @param[in] Sock Pointer to the socket of this TCP instance.
  513. @param[in] Request The code of this operation request.
  514. @param[in] Data Pointer to the operation specific data passed in
  515. together with the operation request. This is an
  516. optional parameter that may be NULL.
  517. @retval EFI_SUCCESS The socket request completed successfully.
  518. @retval other The error status returned by the corresponding TCP
  519. layer function.
  520. **/
  521. EFI_STATUS
  522. TcpDispatcher (
  523. IN SOCKET *Sock,
  524. IN UINT8 Request,
  525. IN VOID *Data OPTIONAL
  526. );
  527. #endif