Udp6Main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /** @file
  2. Contains all EFI_UDP6_PROTOCOL interfaces.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Udp6Impl.h"
  7. EFI_UDP6_PROTOCOL mUdp6Protocol = {
  8. Udp6GetModeData,
  9. Udp6Configure,
  10. Udp6Groups,
  11. Udp6Transmit,
  12. Udp6Receive,
  13. Udp6Cancel,
  14. Udp6Poll
  15. };
  16. /**
  17. This function copies the current operational settings of this EFI UDPv6 Protocol
  18. instance into user-supplied buffers. This function is used optionally to retrieve
  19. the operational mode data of underlying networks or drivers.
  20. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  21. @param[out] Udp6ConfigData The buffer in which the current UDP configuration
  22. data is returned. This parameter is optional and
  23. may be NULL.
  24. @param[out] Ip6ModeData The buffer in which the current EFI IPv6 Protocol
  25. mode data is returned. This parameter is optional
  26. and may be NULL.
  27. @param[out] MnpConfigData The buffer in which the current managed network
  28. configuration data is returned. This parameter is
  29. optional and may be NULL.
  30. @param[out] SnpModeData The buffer in which the simple network mode data
  31. is returned. This parameter is optional and may be NULL.
  32. @retval EFI_SUCCESS The mode data was read.
  33. @retval EFI_NOT_STARTED When Udp6ConfigData is queried, no configuration
  34. data is available because this instance has not
  35. been started.
  36. @retval EFI_INVALID_PARAMETER This is NULL.
  37. **/
  38. EFI_STATUS
  39. EFIAPI
  40. Udp6GetModeData (
  41. IN EFI_UDP6_PROTOCOL *This,
  42. OUT EFI_UDP6_CONFIG_DATA *Udp6ConfigData OPTIONAL,
  43. OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL,
  44. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  45. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  46. )
  47. {
  48. UDP6_INSTANCE_DATA *Instance;
  49. EFI_IP6_PROTOCOL *Ip;
  50. EFI_TPL OldTpl;
  51. EFI_STATUS Status;
  52. if (This == NULL) {
  53. return EFI_INVALID_PARAMETER;
  54. }
  55. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  56. if (!Instance->Configured && (Udp6ConfigData != NULL)) {
  57. return EFI_NOT_STARTED;
  58. }
  59. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  60. if (Udp6ConfigData != NULL) {
  61. //
  62. // Set the Udp6ConfigData.
  63. //
  64. CopyMem (Udp6ConfigData, &Instance->ConfigData, sizeof (EFI_UDP6_CONFIG_DATA));
  65. }
  66. Ip = Instance->IpInfo->Ip.Ip6;
  67. //
  68. // Get the underlying Ip6ModeData, MnpConfigData and SnpModeData.
  69. //
  70. Status = Ip->GetModeData (Ip, Ip6ModeData, MnpConfigData, SnpModeData);
  71. gBS->RestoreTPL (OldTpl);
  72. return Status;
  73. }
  74. /**
  75. This function is used to do the following:
  76. Initialize and start this instance of the EFI UDPv6 Protocol.
  77. Change the filtering rules and operational parameters.
  78. Reset this instance of the EFI UDPv6 Protocol.
  79. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  80. @param[in] UdpConfigData Pointer to the buffer to set the configuration
  81. data. This parameter is optional and may be NULL.
  82. @retval EFI_SUCCESS The configuration settings were set, changed, or
  83. reset successfully.
  84. @retval EFI_NO_MAPPING When the UdpConfigData.UseAnyStationAddress is set
  85. to true and there is no address available for the IP6
  86. driver to bind a source address to this instance.
  87. @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
  88. This is NULL.
  89. UdpConfigData.StationAddress is not a valid
  90. unicast IPv6 address.
  91. UdpConfigData.RemoteAddress is not a valid unicast
  92. IPv6 address if it is not zero.
  93. @retval EFI_ALREADY_STARTED The EFI UDPv6 Protocol instance is already
  94. started/configured and must be stopped/reset
  95. before it can be reconfigured. Only TrafficClass,
  96. HopLimit, ReceiveTimeout, and TransmitTimeout can
  97. be reconfigured without stopping the current
  98. instance of the EFI UDPv6 Protocol.
  99. @retval EFI_ACCESS_DENIED UdpConfigData.AllowDuplicatePort is FALSE and
  100. UdpConfigData.StationPort is already used by another
  101. instance.
  102. @retval EFI_OUT_OF_RESOURCES The EFI UDPv6 Protocol driver cannot allocate
  103. memory for this EFI UDPv6 Protocol instance.
  104. @retval EFI_DEVICE_ERROR An unexpected network or system error occurred, and
  105. this instance was not opened.
  106. **/
  107. EFI_STATUS
  108. EFIAPI
  109. Udp6Configure (
  110. IN EFI_UDP6_PROTOCOL *This,
  111. IN EFI_UDP6_CONFIG_DATA *UdpConfigData OPTIONAL
  112. )
  113. {
  114. EFI_STATUS Status;
  115. UDP6_INSTANCE_DATA *Instance;
  116. UDP6_SERVICE_DATA *Udp6Service;
  117. EFI_TPL OldTpl;
  118. EFI_IPv6_ADDRESS StationAddress;
  119. EFI_IPv6_ADDRESS RemoteAddress;
  120. EFI_IP6_CONFIG_DATA Ip6ConfigData;
  121. EFI_IPv6_ADDRESS LocalAddr;
  122. EFI_IPv6_ADDRESS RemoteAddr;
  123. if (This == NULL) {
  124. return EFI_INVALID_PARAMETER;
  125. }
  126. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  127. if (!Instance->Configured && (UdpConfigData == NULL)) {
  128. return EFI_SUCCESS;
  129. }
  130. Udp6Service = Instance->Udp6Service;
  131. Status = EFI_SUCCESS;
  132. ASSERT (Udp6Service != NULL);
  133. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  134. if (UdpConfigData != NULL) {
  135. IP6_COPY_ADDRESS (&StationAddress, &UdpConfigData->StationAddress);
  136. IP6_COPY_ADDRESS (&RemoteAddress, &UdpConfigData->RemoteAddress);
  137. if ((!NetIp6IsUnspecifiedAddr (&StationAddress) && !NetIp6IsValidUnicast (&StationAddress)) ||
  138. (!NetIp6IsUnspecifiedAddr (&RemoteAddress) && !NetIp6IsValidUnicast (&RemoteAddress))
  139. )
  140. {
  141. //
  142. // If not use default address, and StationAddress is not a valid unicast
  143. // if it is not IPv6 address or RemoteAddress is not a valid unicast IPv6
  144. // address if it is not 0.
  145. //
  146. Status = EFI_INVALID_PARAMETER;
  147. goto ON_EXIT;
  148. }
  149. if (Instance->Configured) {
  150. //
  151. // The instance is already configured, try to do the re-configuration.
  152. //
  153. if (!Udp6IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {
  154. //
  155. // If the new configuration data wants to change some unreconfigurable
  156. // settings, return EFI_ALREADY_STARTED.
  157. //
  158. Status = EFI_ALREADY_STARTED;
  159. goto ON_EXIT;
  160. }
  161. //
  162. // Save the reconfigurable parameters.
  163. //
  164. Instance->ConfigData.TrafficClass = UdpConfigData->TrafficClass;
  165. Instance->ConfigData.HopLimit = UdpConfigData->HopLimit;
  166. Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout;
  167. Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;
  168. } else {
  169. //
  170. // Construct the Ip configuration data from the UdpConfigData.
  171. //
  172. Udp6BuildIp6ConfigData (UdpConfigData, &Ip6ConfigData);
  173. //
  174. // Configure the Ip instance wrapped in the IpInfo.
  175. //
  176. Status = IpIoConfigIp (Instance->IpInfo, &Ip6ConfigData);
  177. if (EFI_ERROR (Status)) {
  178. if (Status == EFI_NO_MAPPING) {
  179. Instance->IsNoMapping = TRUE;
  180. }
  181. goto ON_EXIT;
  182. }
  183. Instance->IsNoMapping = FALSE;
  184. //
  185. // Save the configuration data.
  186. //
  187. CopyMem (
  188. &Instance->ConfigData,
  189. UdpConfigData,
  190. sizeof (EFI_UDP6_CONFIG_DATA)
  191. );
  192. IP6_COPY_ADDRESS (&Instance->ConfigData.StationAddress, &Ip6ConfigData.StationAddress);
  193. //
  194. // Try to allocate the required port resource.
  195. //
  196. Status = Udp6Bind (&Udp6Service->ChildrenList, &Instance->ConfigData);
  197. if (EFI_ERROR (Status)) {
  198. //
  199. // Reset the ip instance if bind fails.
  200. //
  201. IpIoConfigIp (Instance->IpInfo, NULL);
  202. goto ON_EXIT;
  203. }
  204. //
  205. // Pre calculate the checksum for the pseudo head, ignore the UDP length first.
  206. //
  207. IP6_COPY_ADDRESS (&LocalAddr, &Instance->ConfigData.StationAddress);
  208. IP6_COPY_ADDRESS (&RemoteAddr, &Instance->ConfigData.RemoteAddress);
  209. Instance->HeadSum = NetIp6PseudoHeadChecksum (
  210. &LocalAddr,
  211. &RemoteAddr,
  212. EFI_IP_PROTO_UDP,
  213. 0
  214. );
  215. Instance->Configured = TRUE;
  216. }
  217. } else {
  218. //
  219. // UdpConfigData is NULL, reset the instance.
  220. //
  221. Instance->Configured = FALSE;
  222. Instance->IsNoMapping = FALSE;
  223. //
  224. // Reset the Ip instance wrapped in the IpInfo.
  225. //
  226. IpIoConfigIp (Instance->IpInfo, NULL);
  227. //
  228. // Cancel all the user tokens.
  229. //
  230. Instance->Udp6Proto.Cancel (&Instance->Udp6Proto, NULL);
  231. //
  232. // Remove the buffered RxData for this instance.
  233. //
  234. Udp6FlushRcvdDgram (Instance);
  235. ASSERT (IsListEmpty (&Instance->DeliveredDgramQue));
  236. }
  237. ON_EXIT:
  238. gBS->RestoreTPL (OldTpl);
  239. return Status;
  240. }
  241. /**
  242. This function is used to enable and disable the multicast group filtering.
  243. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  244. @param[in] JoinFlag Set to TRUE to join a multicast group. Set to
  245. FALSE to leave one or all multicast groups.
  246. @param[in] MulticastAddress Pointer to multicast group address to join or
  247. leave. This parameter is optional and may be NULL.
  248. @retval EFI_SUCCESS The operation completed successfully.
  249. @retval EFI_NOT_STARTED The EFI UDPv6 Protocol instance has not been
  250. started.
  251. @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
  252. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  253. This is NULL.
  254. JoinFlag is TRUE and MulticastAddress is NULL.
  255. JoinFlag is TRUE and *MulticastAddress is not a
  256. valid multicast address.
  257. @retval EFI_ALREADY_STARTED The group address is already in the group table
  258. (when JoinFlag is TRUE).
  259. @retval EFI_NOT_FOUND The group address is not in the group table (when
  260. JoinFlag is FALSE).
  261. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  262. **/
  263. EFI_STATUS
  264. EFIAPI
  265. Udp6Groups (
  266. IN EFI_UDP6_PROTOCOL *This,
  267. IN BOOLEAN JoinFlag,
  268. IN EFI_IPv6_ADDRESS *MulticastAddress OPTIONAL
  269. )
  270. {
  271. EFI_STATUS Status;
  272. UDP6_INSTANCE_DATA *Instance;
  273. EFI_IP6_PROTOCOL *Ip;
  274. EFI_TPL OldTpl;
  275. EFI_IPv6_ADDRESS *McastIp;
  276. if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) {
  277. return EFI_INVALID_PARAMETER;
  278. }
  279. McastIp = NULL;
  280. if (JoinFlag) {
  281. if (!IP6_IS_MULTICAST (MulticastAddress)) {
  282. return EFI_INVALID_PARAMETER;
  283. }
  284. McastIp = AllocateCopyPool (sizeof (EFI_IPv6_ADDRESS), MulticastAddress);
  285. if (McastIp == NULL) {
  286. return EFI_OUT_OF_RESOURCES;
  287. }
  288. }
  289. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  290. if (!Instance->Configured) {
  291. if (McastIp != NULL) {
  292. FreePool (McastIp);
  293. }
  294. return EFI_NOT_STARTED;
  295. }
  296. Ip = Instance->IpInfo->Ip.Ip6;
  297. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  298. //
  299. // Invoke the Ip instance the Udp6 instance consumes to do the group operation.
  300. //
  301. Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);
  302. if (EFI_ERROR (Status)) {
  303. goto ON_EXIT;
  304. }
  305. //
  306. // Keep a local copy of the configured multicast IPs because IpIo receives
  307. // datagrams from the 0 station address IP instance and then UDP delivers to
  308. // the matched instance. This copy of multicast IPs is used to avoid receive
  309. // the multicast datagrams destinated to multicast IPs the other instances configured.
  310. //
  311. if (JoinFlag) {
  312. Status = NetMapInsertTail (&Instance->McastIps, (VOID *)McastIp, NULL);
  313. } else {
  314. Status = NetMapIterate (&Instance->McastIps, Udp6LeaveGroup, MulticastAddress);
  315. if ((MulticastAddress != NULL) && (Status == EFI_ABORTED)) {
  316. Status = EFI_SUCCESS;
  317. }
  318. }
  319. ON_EXIT:
  320. gBS->RestoreTPL (OldTpl);
  321. if (EFI_ERROR (Status)) {
  322. if (McastIp != NULL) {
  323. FreePool (McastIp);
  324. }
  325. }
  326. return Status;
  327. }
  328. /**
  329. This function places a sending request to this instance of the EFI UDPv6 Protocol,
  330. alongside the transmit data that was filled by the user.
  331. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  332. @param[in] Token Pointer to the completion token that will be
  333. placed into the transmit queue.
  334. @retval EFI_SUCCESS The data was queued for transmission.
  335. @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been
  336. started.
  337. @retval EFI_NO_MAPPING The under-layer IPv6 driver was responsible for
  338. choosing a source address for this instance, but
  339. no source address was available for use.
  340. @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
  341. This is NULL.
  342. Token is NULL. Token.Event is NULL.
  343. Token.Packet.TxData is NULL.
  344. Token.Packet.TxData.FragmentCount is zero.
  345. Token.Packet.TxData.DataLength is not equal to the
  346. sum of fragment lengths.
  347. One or more of the
  348. Token.Packet.TxData.FragmentTable[].FragmentLength
  349. fields is zero.
  350. One or more of the
  351. Token.Packet.TxData.FragmentTable[].FragmentBuffer
  352. fields is NULL. One or more of the
  353. Token.Packet.TxData.UdpSessionData.DestinationAddress
  354. are not valid unicast IPv6
  355. addresses if the UdpSessionData is not NULL.
  356. Token.Packet.TxData.UdpSessionData.
  357. DestinationAddress is NULL
  358. Token.Packet.TxData.UdpSessionData.
  359. DestinationPort
  360. is zero.
  361. Token.Packet.TxData.UdpSessionData is NULL and this
  362. instance's UdpConfigData.RemoteAddress is unspecified.
  363. @retval EFI_ACCESS_DENIED The transmit completion token with the same
  364. Token.Event is already in the transmit queue.
  365. @retval EFI_NOT_READY The completion token could not be queued because
  366. the transmit queue is full.
  367. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
  368. @retval EFI_NOT_FOUND There is no route to the destination network or
  369. address.
  370. @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
  371. packet size. Or, the length of the IP header + UDP
  372. header + data length is greater than MTU if
  373. DoNotFragment is TRUE.
  374. **/
  375. EFI_STATUS
  376. EFIAPI
  377. Udp6Transmit (
  378. IN EFI_UDP6_PROTOCOL *This,
  379. IN EFI_UDP6_COMPLETION_TOKEN *Token
  380. )
  381. {
  382. EFI_STATUS Status;
  383. UDP6_INSTANCE_DATA *Instance;
  384. EFI_TPL OldTpl;
  385. NET_BUF *Packet;
  386. EFI_UDP_HEADER *Udp6Header;
  387. EFI_UDP6_CONFIG_DATA *ConfigData;
  388. EFI_IPv6_ADDRESS Source;
  389. EFI_IPv6_ADDRESS Destination;
  390. EFI_UDP6_TRANSMIT_DATA *TxData;
  391. EFI_UDP6_SESSION_DATA *UdpSessionData;
  392. UDP6_SERVICE_DATA *Udp6Service;
  393. IP_IO_OVERRIDE Override;
  394. UINT16 HeadSum;
  395. EFI_IP_ADDRESS IpDestAddr;
  396. if ((This == NULL) || (Token == NULL)) {
  397. return EFI_INVALID_PARAMETER;
  398. }
  399. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  400. if (!Instance->Configured) {
  401. return EFI_NOT_STARTED;
  402. }
  403. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  404. //
  405. // Validate the Token, if the token is invalid return the error code.
  406. //
  407. Status = Udp6ValidateTxToken (Instance, Token);
  408. if (EFI_ERROR (Status)) {
  409. goto ON_EXIT;
  410. }
  411. if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token)) ||
  412. EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token))
  413. )
  414. {
  415. //
  416. // Try to find a duplicate token in the two token maps, if found, return
  417. // EFI_ACCESS_DENIED.
  418. //
  419. Status = EFI_ACCESS_DENIED;
  420. goto ON_EXIT;
  421. }
  422. TxData = Token->Packet.TxData;
  423. //
  424. // Create a net buffer to hold the user buffer and the udp header.
  425. //
  426. Packet = NetbufFromExt (
  427. (NET_FRAGMENT *)TxData->FragmentTable,
  428. TxData->FragmentCount,
  429. UDP6_HEADER_SIZE,
  430. 0,
  431. Udp6NetVectorExtFree,
  432. NULL
  433. );
  434. if (Packet == NULL) {
  435. Status = EFI_OUT_OF_RESOURCES;
  436. goto ON_EXIT;
  437. }
  438. //
  439. // Store the IpIo in ProtoData.
  440. //
  441. Udp6Service = Instance->Udp6Service;
  442. *((UINTN *)&Packet->ProtoData[0]) = (UINTN)(Udp6Service->IpIo);
  443. Udp6Header = (EFI_UDP_HEADER *)NetbufAllocSpace (Packet, UDP6_HEADER_SIZE, TRUE);
  444. ASSERT (Udp6Header != NULL);
  445. if (Udp6Header == NULL) {
  446. Status = EFI_OUT_OF_RESOURCES;
  447. goto ON_EXIT;
  448. }
  449. ConfigData = &Instance->ConfigData;
  450. //
  451. // Fill the udp header.
  452. //
  453. Udp6Header->SrcPort = HTONS (ConfigData->StationPort);
  454. Udp6Header->DstPort = HTONS (ConfigData->RemotePort);
  455. Udp6Header->Length = HTONS ((UINT16)Packet->TotalSize);
  456. Udp6Header->Checksum = 0;
  457. //
  458. // Set the UDP Header in NET_BUF, this UDP header is for IP6 can fast get the
  459. // Udp header for pseudoHeadCheckSum.
  460. //
  461. Packet->Udp = Udp6Header;
  462. UdpSessionData = TxData->UdpSessionData;
  463. if (UdpSessionData != NULL) {
  464. //
  465. // Set the Destination according to the specified
  466. // UdpSessionData.
  467. //
  468. if (UdpSessionData->DestinationPort != 0) {
  469. Udp6Header->DstPort = HTONS (UdpSessionData->DestinationPort);
  470. }
  471. IP6_COPY_ADDRESS (&Source, &ConfigData->StationAddress);
  472. if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress)) {
  473. IP6_COPY_ADDRESS (&Destination, &UdpSessionData->DestinationAddress);
  474. } else {
  475. IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);
  476. }
  477. //
  478. // Calculate the pseudo head checksum using the overridden parameters.
  479. //
  480. if (!NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress)) {
  481. HeadSum = NetIp6PseudoHeadChecksum (
  482. &Source,
  483. &Destination,
  484. EFI_IP_PROTO_UDP,
  485. 0
  486. );
  487. //
  488. // calculate the checksum.
  489. //
  490. Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);
  491. if (Udp6Header->Checksum == 0) {
  492. //
  493. // If the calculated checksum is 0, fill the Checksum field with all ones.
  494. //
  495. Udp6Header->Checksum = 0xffff;
  496. }
  497. } else {
  498. //
  499. // Set the checksum is zero if the ConfigData->StationAddress is unspecified
  500. // and the Ipv6 will fill the correct value of this checksum.
  501. //
  502. Udp6Header->Checksum = 0;
  503. }
  504. } else {
  505. //
  506. // UdpSessionData is NULL, use the address and port information previously configured.
  507. //
  508. IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);
  509. HeadSum = Instance->HeadSum;
  510. //
  511. // calculate the checksum.
  512. //
  513. Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);
  514. if (Udp6Header->Checksum == 0) {
  515. //
  516. // If the calculated checksum is 0, fill the Checksum field with all ones.
  517. //
  518. Udp6Header->Checksum = 0xffff;
  519. }
  520. }
  521. //
  522. // Fill the IpIo Override data.
  523. //
  524. Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_UDP;
  525. Override.Ip6OverrideData.HopLimit = ConfigData->HopLimit;
  526. Override.Ip6OverrideData.FlowLabel = 0;
  527. //
  528. // Save the token into the TxToken map.
  529. //
  530. Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);
  531. if (EFI_ERROR (Status)) {
  532. goto FREE_PACKET;
  533. }
  534. //
  535. // Send out this datagram through IpIo.
  536. //
  537. if (UdpSessionData != NULL) {
  538. IP6_COPY_ADDRESS (&(IpDestAddr.v6), &Destination);
  539. } else {
  540. ZeroMem (&IpDestAddr.v6, sizeof (EFI_IPv6_ADDRESS));
  541. }
  542. Status = IpIoSend (
  543. Udp6Service->IpIo,
  544. Packet,
  545. Instance->IpInfo,
  546. Instance,
  547. Token,
  548. &IpDestAddr,
  549. &Override
  550. );
  551. if (EFI_ERROR (Status)) {
  552. //
  553. // Remove this token from the TxTokens.
  554. //
  555. Udp6RemoveToken (&Instance->TxTokens, Token);
  556. }
  557. FREE_PACKET:
  558. NetbufFree (Packet);
  559. ON_EXIT:
  560. gBS->RestoreTPL (OldTpl);
  561. return Status;
  562. }
  563. /**
  564. This function places a completion token into the receive packet queue. This function
  565. is always asynchronous.
  566. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  567. @param[in] Token Pointer to a token that is associated with the
  568. receive data descriptor.
  569. @retval EFI_SUCCESS The receive completion token was cached.
  570. @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been
  571. started.
  572. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,
  573. BOOTP, RARP, etc.) is not finished yet.
  574. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  575. This is NULL. Token is NULL. Token.Event is NULL.
  576. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued
  577. due to a lack of system resources (usually
  578. memory).
  579. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  580. The EFI UDPv6 Protocol instance has been reset to
  581. startup defaults.
  582. @retval EFI_ACCESS_DENIED A receive completion token with the same
  583. Token.Event is already in the receive queue.
  584. @retval EFI_NOT_READY The receive request could not be queued because
  585. the receive queue is full.
  586. **/
  587. EFI_STATUS
  588. EFIAPI
  589. Udp6Receive (
  590. IN EFI_UDP6_PROTOCOL *This,
  591. IN EFI_UDP6_COMPLETION_TOKEN *Token
  592. )
  593. {
  594. EFI_STATUS Status;
  595. UDP6_INSTANCE_DATA *Instance;
  596. EFI_TPL OldTpl;
  597. if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
  598. return EFI_INVALID_PARAMETER;
  599. }
  600. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  601. if (!Instance->Configured) {
  602. return EFI_NOT_STARTED;
  603. }
  604. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  605. if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token)) ||
  606. EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token))
  607. )
  608. {
  609. //
  610. // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or
  611. // RxTokens map.
  612. //
  613. Status = EFI_ACCESS_DENIED;
  614. goto ON_EXIT;
  615. }
  616. Token->Packet.RxData = NULL;
  617. //
  618. // Save the token into the RxTokens map.
  619. //
  620. Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);
  621. if (EFI_ERROR (Status)) {
  622. Status = EFI_NOT_READY;
  623. goto ON_EXIT;
  624. }
  625. //
  626. // If there is an icmp error, report it.
  627. //
  628. Udp6ReportIcmpError (Instance);
  629. //
  630. // Try to delivered the received datagrams.
  631. //
  632. Udp6InstanceDeliverDgram (Instance);
  633. //
  634. // Dispatch the DPC queued by the NotifyFunction of Token->Event.
  635. //
  636. DispatchDpc ();
  637. ON_EXIT:
  638. gBS->RestoreTPL (OldTpl);
  639. return Status;
  640. }
  641. /**
  642. This function is used to abort a pending transmit or receive request.
  643. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  644. @param[in] Token Pointer to a token that has been issued by
  645. EFI_UDP6_PROTOCOL.Transmit() or
  646. EFI_UDP6_PROTOCOL.Receive(). This parameter is
  647. optional and may be NULL.
  648. @retval EFI_SUCCESS The asynchronous I/O request was aborted, and
  649. Token.Event was signaled. When Token is NULL, all
  650. pending requests are aborted and their events are
  651. signaled.
  652. @retval EFI_INVALID_PARAMETER This is NULL.
  653. @retval EFI_NOT_STARTED This instance has not been started.
  654. @retval EFI_NO_MAPPING When using the default address, configuration
  655. (DHCP, BOOTP, RARP, etc.) is not finished yet.
  656. @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O
  657. request is not found in the transmit or receive
  658. queue. It is either completed or not issued by
  659. Transmit() or Receive().
  660. **/
  661. EFI_STATUS
  662. EFIAPI
  663. Udp6Cancel (
  664. IN EFI_UDP6_PROTOCOL *This,
  665. IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL
  666. )
  667. {
  668. EFI_STATUS Status;
  669. UDP6_INSTANCE_DATA *Instance;
  670. EFI_TPL OldTpl;
  671. if (This == NULL) {
  672. return EFI_INVALID_PARAMETER;
  673. }
  674. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  675. if (!Instance->Configured) {
  676. return EFI_NOT_STARTED;
  677. }
  678. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  679. //
  680. // Cancel the tokens specified by Token for this instance.
  681. //
  682. Status = Udp6InstanceCancelToken (Instance, Token);
  683. //
  684. // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.
  685. //
  686. DispatchDpc ();
  687. gBS->RestoreTPL (OldTpl);
  688. return Status;
  689. }
  690. /**
  691. This function can be used by network drivers and applications to increase the rate that
  692. data packets are moved between the communications device and the transmit/receive queues.
  693. @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.
  694. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  695. @retval EFI_INVALID_PARAMETER This is NULL.
  696. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  697. @retval EFI_TIMEOUT Data was dropped out of the transmit and/or
  698. receive queue.
  699. **/
  700. EFI_STATUS
  701. EFIAPI
  702. Udp6Poll (
  703. IN EFI_UDP6_PROTOCOL *This
  704. )
  705. {
  706. UDP6_INSTANCE_DATA *Instance;
  707. EFI_IP6_PROTOCOL *Ip;
  708. if (This == NULL) {
  709. return EFI_INVALID_PARAMETER;
  710. }
  711. Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);
  712. Ip = Instance->IpInfo->Ip.Ip6;
  713. //
  714. // Invode the Ip instance consumed by the udp instance to do the poll operation.
  715. //
  716. return Ip->Poll (Ip);
  717. }