TcpMain.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /** @file
  2. Implementation of EFI_TCP4_PROTOCOL and EFI_TCP6_PROTOCOL.
  3. (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
  4. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "TcpMain.h"
  8. /**
  9. Check the integrity of the data buffer.
  10. @param[in] DataLen The total length of the data buffer.
  11. @param[in] FragmentCount The fragment count of the fragment table.
  12. @param[in] FragmentTable Pointer to the fragment table of the data
  13. buffer.
  14. @retval EFI_SUCCESS The integrity check passed.
  15. @retval EFI_INVALID_PARAMETER The integrity check failed.
  16. **/
  17. EFI_STATUS
  18. TcpChkDataBuf (
  19. IN UINT32 DataLen,
  20. IN UINT32 FragmentCount,
  21. IN EFI_TCP4_FRAGMENT_DATA *FragmentTable
  22. )
  23. {
  24. UINT32 Index;
  25. UINT32 Len;
  26. for (Index = 0, Len = 0; Index < FragmentCount; Index++) {
  27. if (FragmentTable[Index].FragmentBuffer == NULL) {
  28. return EFI_INVALID_PARAMETER;
  29. }
  30. Len = Len + FragmentTable[Index].FragmentLength;
  31. }
  32. if (DataLen != Len) {
  33. return EFI_INVALID_PARAMETER;
  34. }
  35. return EFI_SUCCESS;
  36. }
  37. /**
  38. Get the current operational status.
  39. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  40. @param[out] Tcp4State Pointer to the buffer to receive the current TCP
  41. state. Optional parameter that may be NULL.
  42. @param[out] Tcp4ConfigData Pointer to the buffer to receive the current TCP
  43. configuration. Optional parameter that may be NULL.
  44. @param[out] Ip4ModeData Pointer to the buffer to receive the current
  45. IPv4 configuration. Optional parameter that may be NULL.
  46. @param[out] MnpConfigData Pointer to the buffer to receive the current MNP
  47. configuration data indirectly used by the TCPv4
  48. Instance. Optional parameter that may be NULL.
  49. @param[out] SnpModeData Pointer to the buffer to receive the current SNP
  50. configuration data indirectly used by the TCPv4
  51. Instance. Optional parameter that may be NULL.
  52. @retval EFI_SUCCESS The mode data was read.
  53. @retval EFI_NOT_STARTED No configuration data is available because this
  54. instance hasn't been started.
  55. @retval EFI_INVALID_PARAMETER This is NULL.
  56. **/
  57. EFI_STATUS
  58. EFIAPI
  59. Tcp4GetModeData (
  60. IN EFI_TCP4_PROTOCOL *This,
  61. OUT EFI_TCP4_CONNECTION_STATE *Tcp4State OPTIONAL,
  62. OUT EFI_TCP4_CONFIG_DATA *Tcp4ConfigData OPTIONAL,
  63. OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
  64. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  65. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  66. )
  67. {
  68. TCP4_MODE_DATA TcpMode;
  69. SOCKET *Sock;
  70. if (NULL == This) {
  71. return EFI_INVALID_PARAMETER;
  72. }
  73. Sock = SOCK_FROM_THIS (This);
  74. TcpMode.Tcp4State = Tcp4State;
  75. TcpMode.Tcp4ConfigData = Tcp4ConfigData;
  76. TcpMode.Ip4ModeData = Ip4ModeData;
  77. TcpMode.MnpConfigData = MnpConfigData;
  78. TcpMode.SnpModeData = SnpModeData;
  79. return SockGetMode (Sock, &TcpMode);
  80. }
  81. /**
  82. Initialize or brutally reset the operational parameters for
  83. this EFI TCPv4 instance.
  84. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  85. @param[in] TcpConfigData Pointer to the configure data to configure the
  86. instance. Optional parameter that may be NULL.
  87. @retval EFI_SUCCESS The operational settings were set, changed, or
  88. reset successfully.
  89. @retval EFI_NO_MAPPING When using a default address, configuration
  90. (through DHCP, BOOTP, RARP, etc.) is not
  91. finished.
  92. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  93. @retval EFI_ACCESS_DENIED Configuring TCP instance when it is already
  94. configured.
  95. @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
  96. @retval EFI_UNSUPPORTED One or more of the control options are not
  97. supported in the implementation.
  98. @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.
  99. **/
  100. EFI_STATUS
  101. EFIAPI
  102. Tcp4Configure (
  103. IN EFI_TCP4_PROTOCOL *This,
  104. IN EFI_TCP4_CONFIG_DATA *TcpConfigData OPTIONAL
  105. )
  106. {
  107. EFI_TCP4_OPTION *Option;
  108. SOCKET *Sock;
  109. EFI_STATUS Status;
  110. IP4_ADDR Ip;
  111. IP4_ADDR SubnetMask;
  112. if (NULL == This) {
  113. return EFI_INVALID_PARAMETER;
  114. }
  115. //
  116. // Tcp protocol related parameter check will be conducted here
  117. //
  118. if (NULL != TcpConfigData) {
  119. CopyMem (&Ip, &TcpConfigData->AccessPoint.RemoteAddress, sizeof (IP4_ADDR));
  120. if (IP4_IS_LOCAL_BROADCAST (NTOHL (Ip))) {
  121. return EFI_INVALID_PARAMETER;
  122. }
  123. if (TcpConfigData->AccessPoint.ActiveFlag && ((0 == TcpConfigData->AccessPoint.RemotePort) || (Ip == 0))) {
  124. return EFI_INVALID_PARAMETER;
  125. }
  126. if (!TcpConfigData->AccessPoint.UseDefaultAddress) {
  127. CopyMem (&Ip, &TcpConfigData->AccessPoint.StationAddress, sizeof (IP4_ADDR));
  128. CopyMem (&SubnetMask, &TcpConfigData->AccessPoint.SubnetMask, sizeof (IP4_ADDR));
  129. if (!IP4_IS_VALID_NETMASK (NTOHL (SubnetMask)) ||
  130. ((SubnetMask != 0) && !NetIp4IsUnicast (NTOHL (Ip), NTOHL (SubnetMask))))
  131. {
  132. return EFI_INVALID_PARAMETER;
  133. }
  134. }
  135. Option = TcpConfigData->ControlOption;
  136. if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) {
  137. return EFI_UNSUPPORTED;
  138. }
  139. }
  140. Sock = SOCK_FROM_THIS (This);
  141. if (NULL == TcpConfigData) {
  142. return SockFlush (Sock);
  143. }
  144. Status = SockConfigure (Sock, TcpConfigData);
  145. if (EFI_NO_MAPPING == Status) {
  146. Sock->ConfigureState = SO_NO_MAPPING;
  147. }
  148. return Status;
  149. }
  150. /**
  151. Add or delete routing entries.
  152. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  153. @param[in] DeleteRoute If TRUE, delete the specified route from routing
  154. table; if FALSE, add the specified route to
  155. routing table.
  156. @param[in] SubnetAddress The destination network.
  157. @param[in] SubnetMask The subnet mask for the destination network.
  158. @param[in] GatewayAddress The gateway address for this route.
  159. @retval EFI_SUCCESS The operation completed successfully.
  160. @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance has not been
  161. configured.
  162. @retval EFI_NO_MAPPING When using a default address, configuration
  163. (through DHCP, BOOTP, RARP, etc.) is not
  164. finished.
  165. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  166. @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to add the
  167. entry to the routing table.
  168. @retval EFI_NOT_FOUND This route is not in the routing table.
  169. @retval EFI_ACCESS_DENIED This route is already in the routing table.
  170. @retval EFI_UNSUPPORTED The TCP driver does not support this operation.
  171. **/
  172. EFI_STATUS
  173. EFIAPI
  174. Tcp4Routes (
  175. IN EFI_TCP4_PROTOCOL *This,
  176. IN BOOLEAN DeleteRoute,
  177. IN EFI_IPv4_ADDRESS *SubnetAddress,
  178. IN EFI_IPv4_ADDRESS *SubnetMask,
  179. IN EFI_IPv4_ADDRESS *GatewayAddress
  180. )
  181. {
  182. SOCKET *Sock;
  183. TCP4_ROUTE_INFO RouteInfo;
  184. if (NULL == This) {
  185. return EFI_INVALID_PARAMETER;
  186. }
  187. Sock = SOCK_FROM_THIS (This);
  188. RouteInfo.DeleteRoute = DeleteRoute;
  189. RouteInfo.SubnetAddress = SubnetAddress;
  190. RouteInfo.SubnetMask = SubnetMask;
  191. RouteInfo.GatewayAddress = GatewayAddress;
  192. return SockRoute (Sock, &RouteInfo);
  193. }
  194. /**
  195. Initiate a non-blocking TCP connection request for an active TCP instance.
  196. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  197. @param[in] ConnectionToken Pointer to the connection token to return when
  198. the TCP three way handshake finishes.
  199. @retval EFI_SUCCESS The connection request successfully
  200. initiated.
  201. @retval EFI_NOT_STARTED This EFI_TCP4_PROTOCOL instance hasn't been
  202. configured.
  203. @retval EFI_ACCESS_DENIED The instance is not configured as an active one,
  204. or it is not in Tcp4StateClosed state.
  205. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  206. @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to
  207. initiate the active open.
  208. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  209. **/
  210. EFI_STATUS
  211. EFIAPI
  212. Tcp4Connect (
  213. IN EFI_TCP4_PROTOCOL *This,
  214. IN EFI_TCP4_CONNECTION_TOKEN *ConnectionToken
  215. )
  216. {
  217. SOCKET *Sock;
  218. if ((NULL == This) || (NULL == ConnectionToken) || (NULL == ConnectionToken->CompletionToken.Event)) {
  219. return EFI_INVALID_PARAMETER;
  220. }
  221. Sock = SOCK_FROM_THIS (This);
  222. return SockConnect (Sock, ConnectionToken);
  223. }
  224. /**
  225. Listen on the passive instance to accept an incoming connection request.
  226. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  227. @param[in] ListenToken Pointer to the listen token to return when
  228. operation finishes.
  229. @retval EFI_SUCCESS The listen token was queued successfully.
  230. @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
  231. configured.
  232. @retval EFI_ACCESS_DENIED The instance is not a passive one or it is not
  233. in Tcp4StateListen state or a same listen token
  234. has already existed in the listen token queue of
  235. this TCP instance.
  236. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  237. @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish
  238. the operation.
  239. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  240. **/
  241. EFI_STATUS
  242. EFIAPI
  243. Tcp4Accept (
  244. IN EFI_TCP4_PROTOCOL *This,
  245. IN EFI_TCP4_LISTEN_TOKEN *ListenToken
  246. )
  247. {
  248. SOCKET *Sock;
  249. if ((NULL == This) || (NULL == ListenToken) || (NULL == ListenToken->CompletionToken.Event)) {
  250. return EFI_INVALID_PARAMETER;
  251. }
  252. Sock = SOCK_FROM_THIS (This);
  253. return SockAccept (Sock, ListenToken);
  254. }
  255. /**
  256. Queues outgoing data into the transmit queue
  257. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  258. @param[in] Token Pointer to the completion token to queue to the
  259. transmit queue.
  260. @retval EFI_SUCCESS The data has been queued for transmission.
  261. @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
  262. configured.
  263. @retval EFI_NO_MAPPING When using a default address, configuration
  264. (DHCP, BOOTP, RARP, etc.) is not finished yet.
  265. @retval EFI_INVALID_PARAMETER One or more parameters are invalid
  266. @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
  267. * A transmit completion token with the same
  268. Token-> CompletionToken.Event was already in the
  269. transmission queue. * The current instance is in
  270. Tcp4StateClosed state. * The current instance is
  271. a passive one and it is in Tcp4StateListen
  272. state. * User has called Close() to disconnect
  273. this connection.
  274. @retval EFI_NOT_READY The completion token could not be queued because
  275. the transmit queue is full.
  276. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of a
  277. resource shortage.
  278. @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or
  279. address.
  280. **/
  281. EFI_STATUS
  282. EFIAPI
  283. Tcp4Transmit (
  284. IN EFI_TCP4_PROTOCOL *This,
  285. IN EFI_TCP4_IO_TOKEN *Token
  286. )
  287. {
  288. SOCKET *Sock;
  289. EFI_STATUS Status;
  290. if ((NULL == This) ||
  291. (NULL == Token) ||
  292. (NULL == Token->CompletionToken.Event) ||
  293. (NULL == Token->Packet.TxData) ||
  294. (0 == Token->Packet.TxData->FragmentCount) ||
  295. (0 == Token->Packet.TxData->DataLength)
  296. )
  297. {
  298. return EFI_INVALID_PARAMETER;
  299. }
  300. Status = TcpChkDataBuf (
  301. Token->Packet.TxData->DataLength,
  302. Token->Packet.TxData->FragmentCount,
  303. Token->Packet.TxData->FragmentTable
  304. );
  305. if (EFI_ERROR (Status)) {
  306. return Status;
  307. }
  308. Sock = SOCK_FROM_THIS (This);
  309. return SockSend (Sock, Token);
  310. }
  311. /**
  312. Place an asynchronous receive request into the receiving queue.
  313. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  314. @param[in] Token Pointer to a token that is associated with the
  315. receive data descriptor.
  316. @retval EFI_SUCCESS The receive completion token was cached
  317. @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
  318. configured.
  319. @retval EFI_NO_MAPPING When using a default address, configuration
  320. (DHCP, BOOTP, RARP, etc.) is not finished yet.
  321. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  322. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued
  323. due to a lack of system resources.
  324. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  325. @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
  326. * A receive completion token with the same
  327. Token->CompletionToken.Event was already in the
  328. receive queue. * The current instance is in
  329. Tcp4StateClosed state. * The current instance is
  330. a passive one and it is in Tcp4StateListen
  331. state. * User has called Close() to disconnect
  332. this connection.
  333. @retval EFI_CONNECTION_FIN The communication peer has closed the connection,
  334. and there is no any buffered data in the receive
  335. buffer of this instance.
  336. @retval EFI_NOT_READY The receive request could not be queued because
  337. the receive queue is full.
  338. **/
  339. EFI_STATUS
  340. EFIAPI
  341. Tcp4Receive (
  342. IN EFI_TCP4_PROTOCOL *This,
  343. IN EFI_TCP4_IO_TOKEN *Token
  344. )
  345. {
  346. SOCKET *Sock;
  347. EFI_STATUS Status;
  348. if ((NULL == This) ||
  349. (NULL == Token) ||
  350. (NULL == Token->CompletionToken.Event) ||
  351. (NULL == Token->Packet.RxData) ||
  352. (0 == Token->Packet.RxData->FragmentCount) ||
  353. (0 == Token->Packet.RxData->DataLength)
  354. )
  355. {
  356. return EFI_INVALID_PARAMETER;
  357. }
  358. Status = TcpChkDataBuf (
  359. Token->Packet.RxData->DataLength,
  360. Token->Packet.RxData->FragmentCount,
  361. Token->Packet.RxData->FragmentTable
  362. );
  363. if (EFI_ERROR (Status)) {
  364. return Status;
  365. }
  366. Sock = SOCK_FROM_THIS (This);
  367. return SockRcv (Sock, Token);
  368. }
  369. /**
  370. Disconnecting a TCP connection gracefully or reset a TCP connection.
  371. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  372. @param[in] CloseToken Pointer to the close token to return when
  373. operation finishes.
  374. @retval EFI_SUCCESS The operation completed successfully.
  375. @retval EFI_NOT_STARTED The EFI_TCP4_PROTOCOL instance hasn't been
  376. configured.
  377. @retval EFI_ACCESS_DENIED One or more of the following are TRUE: *
  378. Configure() has been called with TcpConfigData
  379. set to NULL, and this function has not returned.
  380. * Previous Close() call on this instance has not
  381. finished.
  382. @retval EFI_INVALID_PARAMETER One ore more parameters are invalid.
  383. @retval EFI_OUT_OF_RESOURCES Could not allocate enough resources to finish the
  384. operation.
  385. @retval EFI_DEVICE_ERROR Any unexpected category error not belonging to those
  386. listed above.
  387. **/
  388. EFI_STATUS
  389. EFIAPI
  390. Tcp4Close (
  391. IN EFI_TCP4_PROTOCOL *This,
  392. IN EFI_TCP4_CLOSE_TOKEN *CloseToken
  393. )
  394. {
  395. SOCKET *Sock;
  396. if ((NULL == This) || (NULL == CloseToken) || (NULL == CloseToken->CompletionToken.Event)) {
  397. return EFI_INVALID_PARAMETER;
  398. }
  399. Sock = SOCK_FROM_THIS (This);
  400. return SockClose (Sock, CloseToken, CloseToken->AbortOnClose);
  401. }
  402. /**
  403. Abort an asynchronous connection, listen, transmission or receive request.
  404. @param This The pointer to the EFI_TCP4_PROTOCOL instance.
  405. @param Token The pointer to a token that has been issued by
  406. EFI_TCP4_PROTOCOL.Connect(),
  407. EFI_TCP4_PROTOCOL.Accept(),
  408. EFI_TCP4_PROTOCOL.Transmit() or
  409. EFI_TCP4_PROTOCOL.Receive(). If NULL, all pending
  410. tokens issued by above four functions will be aborted. Type
  411. EFI_TCP4_COMPLETION_TOKEN is defined in
  412. EFI_TCP4_PROTOCOL.Connect().
  413. @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
  414. is signaled.
  415. @retval EFI_INVALID_PARAMETER This is NULL.
  416. @retval EFI_NOT_STARTED This instance hasn't been configured.
  417. @retval EFI_NO_MAPPING When using the default address, configuration
  418. (DHCP, BOOTP,RARP, etc.) hasn't finished yet.
  419. @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the
  420. transmission or receive queue. It has either
  421. completed or wasn't issued by Transmit() and Receive().
  422. **/
  423. EFI_STATUS
  424. EFIAPI
  425. Tcp4Cancel (
  426. IN EFI_TCP4_PROTOCOL *This,
  427. IN EFI_TCP4_COMPLETION_TOKEN *Token OPTIONAL
  428. )
  429. {
  430. SOCKET *Sock;
  431. if (NULL == This) {
  432. return EFI_INVALID_PARAMETER;
  433. }
  434. Sock = SOCK_FROM_THIS (This);
  435. return SockCancel (Sock, Token);
  436. }
  437. /**
  438. Poll to receive incoming data and transmit outgoing segments.
  439. @param[in] This Pointer to the EFI_TCP4_PROTOCOL instance.
  440. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  441. @retval EFI_INVALID_PARAMETER This is NULL.
  442. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  443. @retval EFI_NOT_READY No incoming or outgoing data was processed.
  444. @retval EFI_TIMEOUT Data was dropped out of the transmission or
  445. receive queue. Consider increasing the polling
  446. rate.
  447. **/
  448. EFI_STATUS
  449. EFIAPI
  450. Tcp4Poll (
  451. IN EFI_TCP4_PROTOCOL *This
  452. )
  453. {
  454. SOCKET *Sock;
  455. EFI_STATUS Status;
  456. if (NULL == This) {
  457. return EFI_INVALID_PARAMETER;
  458. }
  459. Sock = SOCK_FROM_THIS (This);
  460. Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL);
  461. return Status;
  462. }
  463. /**
  464. Get the current operational status.
  465. The GetModeData() function copies the current operational settings of this EFI TCPv6
  466. Protocol instance into user-supplied buffers. This function can also be used to retrieve
  467. the operational setting of underlying drivers such as IPv6, MNP, or SNP.
  468. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  469. @param[out] Tcp6State The buffer in which the current TCP state is
  470. returned. Optional parameter that may be NULL.
  471. @param[out] Tcp6ConfigData The buffer in which the current TCP configuration
  472. is returned. Optional parameter that may be NULL.
  473. @param[out] Ip6ModeData The buffer in which the current IPv6 configuration
  474. data used by the TCP instance is returned.
  475. Optional parameter that may be NULL.
  476. @param[out] MnpConfigData The buffer in which the current MNP configuration
  477. data indirectly used by the TCP instance is returned.
  478. Optional parameter that may be NULL.
  479. @param[out] SnpModeData The buffer in which the current SNP mode data
  480. indirectly used by the TCP instance is returned.
  481. Optional parameter that may be NULL.
  482. @retval EFI_SUCCESS The mode data was read.
  483. @retval EFI_NOT_STARTED No configuration data is available because this instance hasn't
  484. been started.
  485. @retval EFI_INVALID_PARAMETER This is NULL.
  486. **/
  487. EFI_STATUS
  488. EFIAPI
  489. Tcp6GetModeData (
  490. IN EFI_TCP6_PROTOCOL *This,
  491. OUT EFI_TCP6_CONNECTION_STATE *Tcp6State OPTIONAL,
  492. OUT EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL,
  493. OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL,
  494. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  495. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  496. )
  497. {
  498. TCP6_MODE_DATA TcpMode;
  499. SOCKET *Sock;
  500. if (NULL == This) {
  501. return EFI_INVALID_PARAMETER;
  502. }
  503. Sock = SOCK_FROM_THIS (This);
  504. TcpMode.Tcp6State = Tcp6State;
  505. TcpMode.Tcp6ConfigData = Tcp6ConfigData;
  506. TcpMode.Ip6ModeData = Ip6ModeData;
  507. TcpMode.MnpConfigData = MnpConfigData;
  508. TcpMode.SnpModeData = SnpModeData;
  509. return SockGetMode (Sock, &TcpMode);
  510. }
  511. /**
  512. Initialize or brutally reset the operational parameters for this EFI TCPv6 instance.
  513. The Configure() function does the following:
  514. - Initialize this TCP instance, i.e., initialize the communication end settings and
  515. specify active open or passive open for an instance.
  516. - Reset this TCP instance brutally, i.e., cancel all pending asynchronous tokens, flush
  517. transmission and receiving buffer directly without informing the communication peer.
  518. No other TCPv6 Protocol operation except Poll() can be executed by this instance until
  519. it is configured properly. For an active TCP instance, after a proper configuration it
  520. may call Connect() to initiate a three-way handshake. For a passive TCP instance,
  521. its state transits to Tcp6StateListen after configuration, and Accept() may be
  522. called to listen the incoming TCP connection requests. If Tcp6ConfigData is set to NULL,
  523. the instance is reset. The resetting process will be done brutally, the state machine will
  524. be set to Tcp6StateClosed directly, the receive queue and transmit queue will be flushed,
  525. and no traffic is allowed through this instance.
  526. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  527. @param[in] Tcp6ConfigData Pointer to the configure data to configure the instance.
  528. If Tcp6ConfigData is set to NULL, the instance is reset.
  529. @retval EFI_SUCCESS The operational settings were set, changed, or reset
  530. successfully.
  531. @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source
  532. address for this instance, but no source address was available for
  533. use.
  534. @retval EFI_INVALID_PARAMETER One or more of the following conditions are TRUE:
  535. - This is NULL.
  536. - Tcp6ConfigData->AccessPoint.StationAddress is neither zero nor
  537. one of the configured IP addresses in the underlying IPv6 driver.
  538. - Tcp6ConfigData->AccessPoint.RemoteAddress isn't a valid unicast
  539. IPv6 address.
  540. - Tcp6ConfigData->AccessPoint.RemoteAddress is zero or
  541. Tcp6ConfigData->AccessPoint.RemotePort is zero when
  542. Tcp6ConfigData->AccessPoint.ActiveFlag is TRUE.
  543. - A same access point has been configured in other TCP
  544. instance properly.
  545. @retval EFI_ACCESS_DENIED Configuring a TCP instance when it is configured without
  546. calling Configure() with NULL to reset it.
  547. @retval EFI_UNSUPPORTED One or more of the control options are not supported in
  548. the implementation.
  549. @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when
  550. executing Configure().
  551. @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
  552. **/
  553. EFI_STATUS
  554. EFIAPI
  555. Tcp6Configure (
  556. IN EFI_TCP6_PROTOCOL *This,
  557. IN EFI_TCP6_CONFIG_DATA *Tcp6ConfigData OPTIONAL
  558. )
  559. {
  560. EFI_TCP6_OPTION *Option;
  561. SOCKET *Sock;
  562. EFI_STATUS Status;
  563. EFI_IPv6_ADDRESS *Ip;
  564. if (NULL == This) {
  565. return EFI_INVALID_PARAMETER;
  566. }
  567. //
  568. // Tcp protocol related parameter check will be conducted here
  569. //
  570. if (NULL != Tcp6ConfigData) {
  571. Ip = &Tcp6ConfigData->AccessPoint.RemoteAddress;
  572. if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) {
  573. return EFI_INVALID_PARAMETER;
  574. }
  575. if (Tcp6ConfigData->AccessPoint.ActiveFlag &&
  576. ((0 == Tcp6ConfigData->AccessPoint.RemotePort) || NetIp6IsUnspecifiedAddr (Ip))
  577. )
  578. {
  579. return EFI_INVALID_PARAMETER;
  580. }
  581. Ip = &Tcp6ConfigData->AccessPoint.StationAddress;
  582. if (!NetIp6IsUnspecifiedAddr (Ip) && !NetIp6IsValidUnicast (Ip)) {
  583. return EFI_INVALID_PARAMETER;
  584. }
  585. Option = Tcp6ConfigData->ControlOption;
  586. if ((NULL != Option) && (Option->EnableSelectiveAck || Option->EnablePathMtuDiscovery)) {
  587. return EFI_UNSUPPORTED;
  588. }
  589. }
  590. Sock = SOCK_FROM_THIS (This);
  591. if (NULL == Tcp6ConfigData) {
  592. return SockFlush (Sock);
  593. }
  594. Status = SockConfigure (Sock, Tcp6ConfigData);
  595. if (EFI_NO_MAPPING == Status) {
  596. Sock->ConfigureState = SO_NO_MAPPING;
  597. }
  598. return Status;
  599. }
  600. /**
  601. Initiate a nonblocking TCP connection request for an active TCP instance.
  602. The Connect() function will initiate an active open to the remote peer configured
  603. in a current TCP instance if it is configured active. If the connection succeeds or
  604. fails due to any error, the ConnectionToken->CompletionToken.Event will be signaled
  605. and ConnectionToken->CompletionToken.Status will be updated accordingly. This
  606. function can only be called for the TCP instance in the Tcp6StateClosed state. The
  607. instance will transfer into Tcp6StateSynSent if the function returns EFI_SUCCESS.
  608. If a TCP three-way handshake succeeds, its state will become Tcp6StateEstablished.
  609. Otherwise, the state will return to Tcp6StateClosed.
  610. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  611. @param[in] ConnectionToken Pointer to the connection token to return when the TCP three
  612. way handshake finishes.
  613. @retval EFI_SUCCESS The connection request successfully initiated and the state of
  614. this TCP instance has been changed to Tcp6StateSynSent.
  615. @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
  616. @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE:
  617. - This instance is not configured as an active one.
  618. - This instance is not in Tcp6StateClosed state.
  619. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  620. - This is NULL.
  621. - ConnectionToken is NULL.
  622. - ConnectionToken->CompletionToken.Event is NULL.
  623. @retval EFI_OUT_OF_RESOURCES The driver can't allocate enough resources to initiate the active open.
  624. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  625. **/
  626. EFI_STATUS
  627. EFIAPI
  628. Tcp6Connect (
  629. IN EFI_TCP6_PROTOCOL *This,
  630. IN EFI_TCP6_CONNECTION_TOKEN *ConnectionToken
  631. )
  632. {
  633. SOCKET *Sock;
  634. if ((NULL == This) || (NULL == ConnectionToken) || (NULL == ConnectionToken->CompletionToken.Event)) {
  635. return EFI_INVALID_PARAMETER;
  636. }
  637. Sock = SOCK_FROM_THIS (This);
  638. return SockConnect (Sock, ConnectionToken);
  639. }
  640. /**
  641. Listen on the passive instance to accept an incoming connection request. This is a
  642. nonblocking operation.
  643. The Accept() function initiates an asynchronous accept request to wait for an incoming
  644. connection on the passive TCP instance. If a remote peer successfully establishes a
  645. connection with this instance, a new TCP instance will be created and its handle will
  646. be returned in ListenToken->NewChildHandle. The newly created instance is configured
  647. by inheriting the passive instance's configuration and is ready for use upon return.
  648. The new instance is in the Tcp6StateEstablished state.
  649. The ListenToken->CompletionToken.Event will be signaled when a new connection is
  650. accepted, when a user aborts the listen or when a connection is reset.
  651. This function only can be called when a current TCP instance is in Tcp6StateListen state.
  652. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  653. @param[in] ListenToken Pointer to the listen token to return when operation finishes.
  654. @retval EFI_SUCCESS The listen token queued successfully.
  655. @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
  656. @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
  657. - This instance is not a passive instance.
  658. - This instance is not in Tcp6StateListen state.
  659. - The same listen token has already existed in the listen
  660. token queue of this TCP instance.
  661. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  662. - This is NULL.
  663. - ListenToken is NULL.
  664. - ListenToken->CompletionToken.Event is NULL.
  665. @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation.
  666. @retval EFI_DEVICE_ERROR Any unexpected error not belonging to a category listed above.
  667. **/
  668. EFI_STATUS
  669. EFIAPI
  670. Tcp6Accept (
  671. IN EFI_TCP6_PROTOCOL *This,
  672. IN EFI_TCP6_LISTEN_TOKEN *ListenToken
  673. )
  674. {
  675. SOCKET *Sock;
  676. if ((NULL == This) || (NULL == ListenToken) || (NULL == ListenToken->CompletionToken.Event)) {
  677. return EFI_INVALID_PARAMETER;
  678. }
  679. Sock = SOCK_FROM_THIS (This);
  680. return SockAccept (Sock, ListenToken);
  681. }
  682. /**
  683. Queues outgoing data into the transmit queue.
  684. The Transmit() function queues a sending request to this TCP instance along with the
  685. user data. The status of the token is updated and the event in the token will be
  686. signaled once the data is sent out or an error occurs.
  687. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  688. @param[in] Token Pointer to the completion token to queue to the transmit queue.
  689. @retval EFI_SUCCESS The data has been queued for transmission.
  690. @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
  691. @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a
  692. source address for this instance, but no source address was
  693. available for use.
  694. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  695. - This is NULL.
  696. - Token is NULL.
  697. - Token->CompletionToken.Event is NULL.
  698. - Token->Packet.TxData is NULL.
  699. - Token->Packet.FragmentCount is zero.
  700. - Token->Packet.DataLength is not equal to the sum of fragment lengths.
  701. @retval EFI_ACCESS_DENIED One or more of the following conditions are TRUE:
  702. - A transmit completion token with the same Token->
  703. CompletionToken.Event was already in the
  704. transmission queue.
  705. - The current instance is in Tcp6StateClosed state.
  706. - The current instance is a passive one and it is in
  707. Tcp6StateListen state.
  708. - User has called Close() to disconnect this connection.
  709. @retval EFI_NOT_READY The completion token could not be queued because the
  710. transmit queue is full.
  711. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data because of resource
  712. shortage.
  713. @retval EFI_NETWORK_UNREACHABLE There is no route to the destination network or address.
  714. **/
  715. EFI_STATUS
  716. EFIAPI
  717. Tcp6Transmit (
  718. IN EFI_TCP6_PROTOCOL *This,
  719. IN EFI_TCP6_IO_TOKEN *Token
  720. )
  721. {
  722. SOCKET *Sock;
  723. EFI_STATUS Status;
  724. if ((NULL == This) ||
  725. (NULL == Token) ||
  726. (NULL == Token->CompletionToken.Event) ||
  727. (NULL == Token->Packet.TxData) ||
  728. (0 == Token->Packet.TxData->FragmentCount) ||
  729. (0 == Token->Packet.TxData->DataLength)
  730. )
  731. {
  732. return EFI_INVALID_PARAMETER;
  733. }
  734. Status = TcpChkDataBuf (
  735. Token->Packet.TxData->DataLength,
  736. Token->Packet.TxData->FragmentCount,
  737. (EFI_TCP4_FRAGMENT_DATA *)Token->Packet.TxData->FragmentTable
  738. );
  739. if (EFI_ERROR (Status)) {
  740. return Status;
  741. }
  742. Sock = SOCK_FROM_THIS (This);
  743. return SockSend (Sock, Token);
  744. }
  745. /**
  746. Places an asynchronous receive request into the receiving queue.
  747. The Receive() function places a completion token into the receive packet queue. This
  748. function is always asynchronous. The caller must allocate the Token->CompletionToken.Event
  749. and the FragmentBuffer used to receive data. The caller also must fill the DataLength that
  750. represents the whole length of all FragmentBuffer. When the receive operation completes, the
  751. EFI TCPv6 Protocol driver updates the Token->CompletionToken.Status and Token->Packet.RxData
  752. fields, and the Token->CompletionToken.Event is signaled. If data obtained, the data and its length
  753. will be copied into the FragmentTable; at the same time the full length of received data will
  754. be recorded in the DataLength fields. Providing a proper notification function and context
  755. for the event enables the user to receive the notification and receiving status. That
  756. notification function is guaranteed to not be re-entered.
  757. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  758. @param[in] Token Pointer to a token that is associated with the receive data
  759. descriptor.
  760. @retval EFI_SUCCESS The receive completion token was cached.
  761. @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
  762. @retval EFI_NO_MAPPING The underlying IPv6 driver was responsible for choosing a source
  763. address for this instance, but no source address was available for use.
  764. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  765. - This is NULL.
  766. - Token is NULL.
  767. - Token->CompletionToken.Event is NULL.
  768. - Token->Packet.RxData is NULL.
  769. - Token->Packet.RxData->DataLength is 0.
  770. - The Token->Packet.RxData->DataLength is not the
  771. sum of all FragmentBuffer length in FragmentTable.
  772. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of
  773. system resources (usually memory).
  774. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  775. The EFI TCPv6 Protocol instance has been reset to startup defaults.
  776. @retval EFI_ACCESS_DENIED One or more of the following conditions is TRUE:
  777. - A receive completion token with the same Token->CompletionToken.Event
  778. was already in the receive queue.
  779. - The current instance is in Tcp6StateClosed state.
  780. - The current instance is a passive one and it is in
  781. Tcp6StateListen state.
  782. - User has called Close() to disconnect this connection.
  783. @retval EFI_CONNECTION_FIN The communication peer has closed the connection and there is no
  784. buffered data in the receive buffer of this instance.
  785. @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
  786. **/
  787. EFI_STATUS
  788. EFIAPI
  789. Tcp6Receive (
  790. IN EFI_TCP6_PROTOCOL *This,
  791. IN EFI_TCP6_IO_TOKEN *Token
  792. )
  793. {
  794. SOCKET *Sock;
  795. EFI_STATUS Status;
  796. if ((NULL == This) ||
  797. (NULL == Token) ||
  798. (NULL == Token->CompletionToken.Event) ||
  799. (NULL == Token->Packet.RxData) ||
  800. (0 == Token->Packet.RxData->FragmentCount) ||
  801. (0 == Token->Packet.RxData->DataLength)
  802. )
  803. {
  804. return EFI_INVALID_PARAMETER;
  805. }
  806. Status = TcpChkDataBuf (
  807. Token->Packet.RxData->DataLength,
  808. Token->Packet.RxData->FragmentCount,
  809. (EFI_TCP4_FRAGMENT_DATA *)Token->Packet.RxData->FragmentTable
  810. );
  811. if (EFI_ERROR (Status)) {
  812. return Status;
  813. }
  814. Sock = SOCK_FROM_THIS (This);
  815. return SockRcv (Sock, Token);
  816. }
  817. /**
  818. Disconnecting a TCP connection gracefully or reset a TCP connection. This function is a
  819. nonblocking operation.
  820. Initiate an asynchronous close token to the TCP driver. After Close() is called, any buffered
  821. transmission data will be sent by the TCP driver, and the current instance will have a graceful close
  822. working flow described as RFC 793 if AbortOnClose is set to FALSE. Otherwise, a rest packet
  823. will be sent by TCP driver to fast disconnect this connection. When the close operation completes
  824. successfully the TCP instance is in Tcp6StateClosed state, all pending asynchronous
  825. operations are signaled, and any buffers used for TCP network traffic are flushed.
  826. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  827. @param[in] CloseToken Pointer to the close token to return when operation finishes.
  828. @retval EFI_SUCCESS The Close() was called successfully.
  829. @retval EFI_NOT_STARTED This EFI TCPv6 Protocol instance has not been configured.
  830. @retval EFI_ACCESS_DENIED One or more of the following are TRUE:
  831. - CloseToken or CloseToken->CompletionToken.Event is already in use.
  832. - Previous Close() call on this instance has not finished.
  833. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  834. - This is NULL.
  835. - CloseToken is NULL.
  836. - CloseToken->CompletionToken.Event is NULL.
  837. @retval EFI_OUT_OF_RESOURCES Could not allocate enough resource to finish the operation.
  838. @retval EFI_DEVICE_ERROR Any unexpected error not belonging to error categories given above.
  839. **/
  840. EFI_STATUS
  841. EFIAPI
  842. Tcp6Close (
  843. IN EFI_TCP6_PROTOCOL *This,
  844. IN EFI_TCP6_CLOSE_TOKEN *CloseToken
  845. )
  846. {
  847. SOCKET *Sock;
  848. if ((NULL == This) || (NULL == CloseToken) || (NULL == CloseToken->CompletionToken.Event)) {
  849. return EFI_INVALID_PARAMETER;
  850. }
  851. Sock = SOCK_FROM_THIS (This);
  852. return SockClose (Sock, CloseToken, CloseToken->AbortOnClose);
  853. }
  854. /**
  855. Abort an asynchronous connection, listen, transmission or receive request.
  856. The Cancel() function aborts a pending connection, listen, transmit or
  857. receive request.
  858. If Token is not NULL and the token is in the connection, listen, transmission
  859. or receive queue when it is being cancelled, its Token->Status will be set
  860. to EFI_ABORTED and then Token->Event will be signaled.
  861. If the token is not in one of the queues, which usually means that the
  862. asynchronous operation has completed, EFI_NOT_FOUND is returned.
  863. If Token is NULL all asynchronous token issued by Connect(), Accept(),
  864. Transmit() and Receive() will be aborted.
  865. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  866. @param[in] Token Pointer to a token that has been issued by
  867. EFI_TCP6_PROTOCOL.Connect(),
  868. EFI_TCP6_PROTOCOL.Accept(),
  869. EFI_TCP6_PROTOCOL.Transmit() or
  870. EFI_TCP6_PROTOCOL.Receive(). If NULL, all pending
  871. tokens issued by above four functions will be aborted. Type
  872. EFI_TCP6_COMPLETION_TOKEN is defined in
  873. EFI_TCP_PROTOCOL.Connect().
  874. @retval EFI_SUCCESS The asynchronous I/O request is aborted and Token->Event
  875. is signaled.
  876. @retval EFI_INVALID_PARAMETER This is NULL.
  877. @retval EFI_NOT_STARTED This instance hasn't been configured.
  878. @retval EFI_NOT_FOUND The asynchronous I/O request isn't found in the transmission or
  879. receive queue. It has either completed or wasn't issued by
  880. Transmit() and Receive().
  881. **/
  882. EFI_STATUS
  883. EFIAPI
  884. Tcp6Cancel (
  885. IN EFI_TCP6_PROTOCOL *This,
  886. IN EFI_TCP6_COMPLETION_TOKEN *Token OPTIONAL
  887. )
  888. {
  889. SOCKET *Sock;
  890. if (NULL == This) {
  891. return EFI_INVALID_PARAMETER;
  892. }
  893. Sock = SOCK_FROM_THIS (This);
  894. return SockCancel (Sock, Token);
  895. }
  896. /**
  897. Poll to receive incoming data and transmit outgoing segments.
  898. The Poll() function increases the rate that data is moved between the network
  899. and application, and can be called when the TCP instance is created successfully.
  900. Its use is optional.
  901. @param[in] This Pointer to the EFI_TCP6_PROTOCOL instance.
  902. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  903. @retval EFI_INVALID_PARAMETER This is NULL.
  904. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  905. @retval EFI_NOT_READY No incoming or outgoing data is processed.
  906. @retval EFI_TIMEOUT Data was dropped out of the transmission or receive queue.
  907. Consider increasing the polling rate.
  908. **/
  909. EFI_STATUS
  910. EFIAPI
  911. Tcp6Poll (
  912. IN EFI_TCP6_PROTOCOL *This
  913. )
  914. {
  915. SOCKET *Sock;
  916. EFI_STATUS Status;
  917. if (NULL == This) {
  918. return EFI_INVALID_PARAMETER;
  919. }
  920. Sock = SOCK_FROM_THIS (This);
  921. Status = Sock->ProtoHandler (Sock, SOCK_POLL, NULL);
  922. return Status;
  923. }