Udp4Main.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /** @file
  2. (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Udp4Impl.h"
  7. EFI_UDP4_PROTOCOL mUdp4Protocol = {
  8. Udp4GetModeData,
  9. Udp4Configure,
  10. Udp4Groups,
  11. Udp4Routes,
  12. Udp4Transmit,
  13. Udp4Receive,
  14. Udp4Cancel,
  15. Udp4Poll
  16. };
  17. /**
  18. Reads the current operational settings.
  19. The GetModeData() function copies the current operational settings of this EFI
  20. UDPv4 Protocol instance into user-supplied buffers. This function is used
  21. optionally to retrieve the operational mode data of underlying networks or
  22. drivers.
  23. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  24. @param[out] Udp4ConfigData Pointer to the buffer to receive the current configuration data.
  25. @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
  26. @param[out] MnpConfigData Pointer to the managed network configuration data structure.
  27. @param[out] SnpModeData Pointer to the simple network mode data structure.
  28. @retval EFI_SUCCESS The mode data was read.
  29. @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is
  30. available because this instance has not been started.
  31. @retval EFI_INVALID_PARAMETER This is NULL.
  32. **/
  33. EFI_STATUS
  34. EFIAPI
  35. Udp4GetModeData (
  36. IN EFI_UDP4_PROTOCOL *This,
  37. OUT EFI_UDP4_CONFIG_DATA *Udp4ConfigData OPTIONAL,
  38. OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
  39. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  40. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  41. )
  42. {
  43. UDP4_INSTANCE_DATA *Instance;
  44. EFI_IP4_PROTOCOL *Ip;
  45. EFI_TPL OldTpl;
  46. EFI_STATUS Status;
  47. if (This == NULL) {
  48. return EFI_INVALID_PARAMETER;
  49. }
  50. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  51. if (!Instance->Configured && (Udp4ConfigData != NULL)) {
  52. return EFI_NOT_STARTED;
  53. }
  54. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  55. if (Udp4ConfigData != NULL) {
  56. //
  57. // Set the Udp4ConfigData.
  58. //
  59. CopyMem (Udp4ConfigData, &Instance->ConfigData, sizeof (*Udp4ConfigData));
  60. }
  61. Ip = Instance->IpInfo->Ip.Ip4;
  62. //
  63. // Get the underlying Ip4ModeData, MnpConfigData and SnpModeData.
  64. //
  65. Status = Ip->GetModeData (Ip, Ip4ModeData, MnpConfigData, SnpModeData);
  66. gBS->RestoreTPL (OldTpl);
  67. return Status;
  68. }
  69. /**
  70. Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4
  71. Protocol.
  72. The Configure() function is used to do the following:
  73. * Initialize and start this instance of the EFI UDPv4 Protocol.
  74. * Change the filtering rules and operational parameters.
  75. * Reset this instance of the EFI UDPv4 Protocol.
  76. Until these parameters are initialized, no network traffic can be sent or
  77. received by this instance. This instance can be also reset by calling Configure()
  78. with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting
  79. queue are flushed and no traffic is allowed through this instance.
  80. With different parameters in UdpConfigData, Configure() can be used to bind
  81. this instance to specified port.
  82. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  83. @param[in] UdpConfigData Pointer to the buffer to receive the current configuration data.
  84. @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.
  85. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
  86. RARP, etc.) is not finished yet.
  87. @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
  88. @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured
  89. and must be stopped/reset before it can be reconfigured.
  90. @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE
  91. and UdpConfigData.StationPort is already used by
  92. other instance.
  93. @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this
  94. EFI UDPv4 Protocol instance.
  95. @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance
  96. was not opened.
  97. **/
  98. EFI_STATUS
  99. EFIAPI
  100. Udp4Configure (
  101. IN EFI_UDP4_PROTOCOL *This,
  102. IN EFI_UDP4_CONFIG_DATA *UdpConfigData OPTIONAL
  103. )
  104. {
  105. EFI_STATUS Status;
  106. UDP4_INSTANCE_DATA *Instance;
  107. UDP4_SERVICE_DATA *Udp4Service;
  108. EFI_TPL OldTpl;
  109. IP4_ADDR StationAddress;
  110. IP4_ADDR SubnetMask;
  111. IP4_ADDR RemoteAddress;
  112. EFI_IP4_CONFIG_DATA Ip4ConfigData;
  113. IP4_ADDR LocalAddr;
  114. IP4_ADDR RemoteAddr;
  115. if (This == NULL) {
  116. return EFI_INVALID_PARAMETER;
  117. }
  118. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  119. if (!Instance->Configured && (UdpConfigData == NULL)) {
  120. return EFI_SUCCESS;
  121. }
  122. Udp4Service = Instance->Udp4Service;
  123. Status = EFI_SUCCESS;
  124. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  125. if (UdpConfigData != NULL) {
  126. CopyMem (&StationAddress, &UdpConfigData->StationAddress, sizeof (IP4_ADDR));
  127. CopyMem (&SubnetMask, &UdpConfigData->SubnetMask, sizeof (IP4_ADDR));
  128. CopyMem (&RemoteAddress, &UdpConfigData->RemoteAddress, sizeof (IP4_ADDR));
  129. StationAddress = NTOHL (StationAddress);
  130. SubnetMask = NTOHL (SubnetMask);
  131. RemoteAddress = NTOHL (RemoteAddress);
  132. if (!UdpConfigData->UseDefaultAddress &&
  133. (!IP4_IS_VALID_NETMASK (SubnetMask) ||
  134. !((StationAddress == 0) || ((SubnetMask != 0) && NetIp4IsUnicast (StationAddress, SubnetMask))) ||
  135. IP4_IS_LOCAL_BROADCAST (RemoteAddress)))
  136. {
  137. //
  138. // Don't use default address, and subnet mask is invalid or StationAddress is not
  139. // a valid unicast IPv4 address or RemoteAddress is not a valid unicast IPv4 address
  140. // if it is not 0.
  141. //
  142. Status = EFI_INVALID_PARAMETER;
  143. goto ON_EXIT;
  144. }
  145. if (Instance->Configured) {
  146. //
  147. // The instance is already configured, try to do the re-configuration.
  148. //
  149. if (!Udp4IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {
  150. //
  151. // If the new configuration data wants to change some unreconfigurable
  152. // settings, return EFI_ALREADY_STARTED.
  153. //
  154. Status = EFI_ALREADY_STARTED;
  155. goto ON_EXIT;
  156. }
  157. //
  158. // Save the reconfigurable parameters.
  159. //
  160. Instance->ConfigData.TypeOfService = UdpConfigData->TypeOfService;
  161. Instance->ConfigData.TimeToLive = UdpConfigData->TimeToLive;
  162. Instance->ConfigData.DoNotFragment = UdpConfigData->DoNotFragment;
  163. Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout;
  164. Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;
  165. } else {
  166. //
  167. // Construct the Ip configuration data from the UdpConfigData.
  168. //
  169. Udp4BuildIp4ConfigData (UdpConfigData, &Ip4ConfigData);
  170. //
  171. // Configure the Ip instance wrapped in the IpInfo.
  172. //
  173. Status = IpIoConfigIp (Instance->IpInfo, &Ip4ConfigData);
  174. if (EFI_ERROR (Status)) {
  175. if (Status == EFI_NO_MAPPING) {
  176. Instance->IsNoMapping = TRUE;
  177. }
  178. goto ON_EXIT;
  179. }
  180. Instance->IsNoMapping = FALSE;
  181. //
  182. // Save the configuration data.
  183. //
  184. CopyMem (&Instance->ConfigData, UdpConfigData, sizeof (Instance->ConfigData));
  185. IP4_COPY_ADDRESS (&Instance->ConfigData.StationAddress, &Ip4ConfigData.StationAddress);
  186. IP4_COPY_ADDRESS (&Instance->ConfigData.SubnetMask, &Ip4ConfigData.SubnetMask);
  187. //
  188. // Try to allocate the required port resource.
  189. //
  190. Status = Udp4Bind (&Udp4Service->ChildrenList, &Instance->ConfigData);
  191. if (EFI_ERROR (Status)) {
  192. //
  193. // Reset the ip instance if bind fails.
  194. //
  195. IpIoConfigIp (Instance->IpInfo, NULL);
  196. goto ON_EXIT;
  197. }
  198. //
  199. // Pre calculate the checksum for the pseudo head, ignore the UDP length first.
  200. //
  201. CopyMem (&LocalAddr, &Instance->ConfigData.StationAddress, sizeof (IP4_ADDR));
  202. CopyMem (&RemoteAddr, &Instance->ConfigData.RemoteAddress, sizeof (IP4_ADDR));
  203. Instance->HeadSum = NetPseudoHeadChecksum (
  204. LocalAddr,
  205. RemoteAddr,
  206. EFI_IP_PROTO_UDP,
  207. 0
  208. );
  209. Instance->Configured = TRUE;
  210. }
  211. } else {
  212. //
  213. // UdpConfigData is NULL, reset the instance.
  214. //
  215. Instance->Configured = FALSE;
  216. Instance->IsNoMapping = FALSE;
  217. //
  218. // Reset the Ip instance wrapped in the IpInfo.
  219. //
  220. IpIoConfigIp (Instance->IpInfo, NULL);
  221. //
  222. // Cancel all the user tokens.
  223. //
  224. Instance->Udp4Proto.Cancel (&Instance->Udp4Proto, NULL);
  225. //
  226. // Remove the buffered RxData for this instance.
  227. //
  228. Udp4FlushRcvdDgram (Instance);
  229. ASSERT (IsListEmpty (&Instance->DeliveredDgramQue));
  230. }
  231. ON_EXIT:
  232. gBS->RestoreTPL (OldTpl);
  233. return Status;
  234. }
  235. /**
  236. Joins and leaves multicast groups.
  237. The Groups() function is used to enable and disable the multicast group
  238. filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all
  239. currently joined groups are left.
  240. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  241. @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one
  242. or all multicast groups.
  243. @param[in] MulticastAddress Pointer to multicast group address to join or leave.
  244. @retval EFI_SUCCESS The operation completed successfully.
  245. @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
  246. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
  247. RARP, etc.) is not finished yet.
  248. @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.
  249. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  250. - This is NULL.
  251. - JoinFlag is TRUE and MulticastAddress is NULL.
  252. - JoinFlag is TRUE and *MulticastAddress is not
  253. a valid multicast address.
  254. @retval EFI_ALREADY_STARTED The group address is already in the group table (when
  255. JoinFlag is TRUE).
  256. @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is
  257. FALSE).
  258. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  259. **/
  260. EFI_STATUS
  261. EFIAPI
  262. Udp4Groups (
  263. IN EFI_UDP4_PROTOCOL *This,
  264. IN BOOLEAN JoinFlag,
  265. IN EFI_IPv4_ADDRESS *MulticastAddress OPTIONAL
  266. )
  267. {
  268. EFI_STATUS Status;
  269. UDP4_INSTANCE_DATA *Instance;
  270. EFI_IP4_PROTOCOL *Ip;
  271. EFI_TPL OldTpl;
  272. IP4_ADDR McastIp;
  273. if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) {
  274. return EFI_INVALID_PARAMETER;
  275. }
  276. McastIp = 0;
  277. if (JoinFlag) {
  278. CopyMem (&McastIp, MulticastAddress, sizeof (IP4_ADDR));
  279. if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {
  280. return EFI_INVALID_PARAMETER;
  281. }
  282. }
  283. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  284. if (Instance->IsNoMapping) {
  285. return EFI_NO_MAPPING;
  286. }
  287. if (!Instance->Configured) {
  288. return EFI_NOT_STARTED;
  289. }
  290. Ip = Instance->IpInfo->Ip.Ip4;
  291. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  292. //
  293. // Invoke the Ip instance the Udp4 instance consumes to do the group operation.
  294. //
  295. Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);
  296. if (EFI_ERROR (Status)) {
  297. goto ON_EXIT;
  298. }
  299. //
  300. // Keep a local copy of the configured multicast IPs because IpIo receives
  301. // datagrams from the 0 station address IP instance and then UDP delivers to
  302. // the matched instance. This copy of multicast IPs is used to avoid receive
  303. // the multicast datagrams destined to multicast IPs the other instances configured.
  304. //
  305. if (JoinFlag) {
  306. NetMapInsertTail (&Instance->McastIps, (VOID *)(UINTN)McastIp, NULL);
  307. } else {
  308. NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress);
  309. }
  310. ON_EXIT:
  311. gBS->RestoreTPL (OldTpl);
  312. return Status;
  313. }
  314. /**
  315. Adds and deletes routing table entries.
  316. The Routes() function adds a route to or deletes a route from the routing table.
  317. Routes are determined by comparing the SubnetAddress with the destination IP
  318. address and arithmetically AND-ing it with the SubnetMask. The gateway address
  319. must be on the same subnet as the configured station address.
  320. The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
  321. The default route matches all destination IP addresses that do not match any
  322. other routes.
  323. A zero GatewayAddress is a nonroute. Packets are sent to the destination IP
  324. address if it can be found in the Address Resolution Protocol (ARP) cache or
  325. on the local subnet. One automatic nonroute entry will be inserted into the
  326. routing table for outgoing packets that are addressed to a local subnet
  327. (gateway address of 0.0.0.0).
  328. Each instance of the EFI UDPv4 Protocol has its own independent routing table.
  329. Instances of the EFI UDPv4 Protocol that use the default IP address will also
  330. have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These
  331. copies will be updated automatically whenever the IP driver reconfigures its
  332. instances; as a result, the previous modification to these copies will be lost.
  333. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  334. @param[in] DeleteRoute Set to TRUE to delete this route from the routing table.
  335. Set to FALSE to add this route to the routing table.
  336. @param[in] SubnetAddress The destination network address that needs to be routed.
  337. @param[in] SubnetMask The subnet mask of SubnetAddress.
  338. @param[in] GatewayAddress The gateway IP address for this route.
  339. @retval EFI_SUCCESS The operation completed successfully.
  340. @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.
  341. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
  342. - RARP, etc.) is not finished yet.
  343. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  344. @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
  345. @retval EFI_NOT_FOUND This route is not in the routing table.
  346. @retval EFI_ACCESS_DENIED The route is already defined in the routing table.
  347. **/
  348. EFI_STATUS
  349. EFIAPI
  350. Udp4Routes (
  351. IN EFI_UDP4_PROTOCOL *This,
  352. IN BOOLEAN DeleteRoute,
  353. IN EFI_IPv4_ADDRESS *SubnetAddress,
  354. IN EFI_IPv4_ADDRESS *SubnetMask,
  355. IN EFI_IPv4_ADDRESS *GatewayAddress
  356. )
  357. {
  358. UDP4_INSTANCE_DATA *Instance;
  359. EFI_IP4_PROTOCOL *Ip;
  360. if (This == NULL) {
  361. return EFI_INVALID_PARAMETER;
  362. }
  363. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  364. if (Instance->IsNoMapping) {
  365. return EFI_NO_MAPPING;
  366. }
  367. if (!Instance->Configured) {
  368. return EFI_NOT_STARTED;
  369. }
  370. Ip = Instance->IpInfo->Ip.Ip4;
  371. //
  372. // Invoke the Ip instance the Udp4 instance consumes to do the actual operation.
  373. //
  374. return Ip->Routes (Ip, DeleteRoute, SubnetAddress, SubnetMask, GatewayAddress);
  375. }
  376. /**
  377. Queues outgoing data packets into the transmit queue.
  378. The Transmit() function places a sending request to this instance of the EFI
  379. UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever
  380. the packet in the token is sent out or some errors occur, the Token.Event will
  381. be signaled and Token.Status is updated. Providing a proper notification function
  382. and context for the event will enable the user to receive the notification and
  383. transmitting status.
  384. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  385. @param[in] Token Pointer to the completion token that will be placed into the
  386. transmit queue.
  387. @retval EFI_SUCCESS The data has been queued for transmission.
  388. @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
  389. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,
  390. RARP, etc.) is not finished yet.
  391. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  392. @retval EFI_ACCESS_DENIED The transmit completion token with the same
  393. Token.Event was already in the transmit queue.
  394. @retval EFI_NOT_READY The completion token could not be queued because the
  395. transmit queue is full.
  396. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
  397. @retval EFI_NOT_FOUND There is no route to the destination network or address.
  398. @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet
  399. size. Or the length of the IP header + UDP header + data
  400. length is greater than MTU if DoNotFragment is TRUE.
  401. **/
  402. EFI_STATUS
  403. EFIAPI
  404. Udp4Transmit (
  405. IN EFI_UDP4_PROTOCOL *This,
  406. IN EFI_UDP4_COMPLETION_TOKEN *Token
  407. )
  408. {
  409. EFI_STATUS Status;
  410. UDP4_INSTANCE_DATA *Instance;
  411. EFI_TPL OldTpl;
  412. NET_BUF *Packet;
  413. EFI_UDP_HEADER *Udp4Header;
  414. EFI_UDP4_CONFIG_DATA *ConfigData;
  415. IP4_ADDR Source;
  416. IP4_ADDR Destination;
  417. EFI_UDP4_TRANSMIT_DATA *TxData;
  418. EFI_UDP4_SESSION_DATA *UdpSessionData;
  419. UDP4_SERVICE_DATA *Udp4Service;
  420. IP_IO_OVERRIDE Override;
  421. UINT16 HeadSum;
  422. EFI_IP_ADDRESS IpDestAddr;
  423. if ((This == NULL) || (Token == NULL)) {
  424. return EFI_INVALID_PARAMETER;
  425. }
  426. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  427. if (Instance->IsNoMapping) {
  428. return EFI_NO_MAPPING;
  429. }
  430. if (!Instance->Configured) {
  431. return EFI_NOT_STARTED;
  432. }
  433. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  434. //
  435. // Validate the Token, if the token is invalid return the error code.
  436. //
  437. Status = Udp4ValidateTxToken (Instance, Token);
  438. if (EFI_ERROR (Status)) {
  439. goto ON_EXIT;
  440. }
  441. if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)) ||
  442. EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token)))
  443. {
  444. //
  445. // Try to find a duplicate token in the two token maps, if found, return
  446. // EFI_ACCESS_DENIED.
  447. //
  448. Status = EFI_ACCESS_DENIED;
  449. goto ON_EXIT;
  450. }
  451. TxData = Token->Packet.TxData;
  452. //
  453. // Create a net buffer to hold the user buffer and the udp header.
  454. //
  455. Packet = NetbufFromExt (
  456. (NET_FRAGMENT *)TxData->FragmentTable,
  457. TxData->FragmentCount,
  458. UDP4_HEADER_SIZE,
  459. 0,
  460. Udp4NetVectorExtFree,
  461. NULL
  462. );
  463. if (Packet == NULL) {
  464. Status = EFI_OUT_OF_RESOURCES;
  465. goto ON_EXIT;
  466. }
  467. //
  468. // Store the IpIo in ProtoData.
  469. //
  470. Udp4Service = Instance->Udp4Service;
  471. *((UINTN *)&Packet->ProtoData[0]) = (UINTN)(Udp4Service->IpIo);
  472. Udp4Header = (EFI_UDP_HEADER *)NetbufAllocSpace (Packet, UDP4_HEADER_SIZE, TRUE);
  473. ASSERT (Udp4Header != NULL);
  474. ConfigData = &Instance->ConfigData;
  475. //
  476. // Fill the udp header.
  477. //
  478. Udp4Header->SrcPort = HTONS (ConfigData->StationPort);
  479. Udp4Header->DstPort = HTONS (ConfigData->RemotePort);
  480. Udp4Header->Length = HTONS ((UINT16)Packet->TotalSize);
  481. Udp4Header->Checksum = 0;
  482. UdpSessionData = TxData->UdpSessionData;
  483. IP4_COPY_ADDRESS (&Override.Ip4OverrideData.SourceAddress, &ConfigData->StationAddress);
  484. if (UdpSessionData != NULL) {
  485. //
  486. // Set the SourceAddress, SrcPort and Destination according to the specified
  487. // UdpSessionData.
  488. //
  489. if (!EFI_IP4_EQUAL (&UdpSessionData->SourceAddress, &mZeroIp4Addr)) {
  490. IP4_COPY_ADDRESS (&Override.Ip4OverrideData.SourceAddress, &UdpSessionData->SourceAddress);
  491. }
  492. if (UdpSessionData->SourcePort != 0) {
  493. Udp4Header->SrcPort = HTONS (UdpSessionData->SourcePort);
  494. }
  495. if (UdpSessionData->DestinationPort != 0) {
  496. Udp4Header->DstPort = HTONS (UdpSessionData->DestinationPort);
  497. }
  498. CopyMem (&Source, &Override.Ip4OverrideData.SourceAddress, sizeof (IP4_ADDR));
  499. CopyMem (&Destination, &UdpSessionData->DestinationAddress, sizeof (IP4_ADDR));
  500. //
  501. // calculate the pseudo head checksum using the overridden parameters.
  502. //
  503. HeadSum = NetPseudoHeadChecksum (
  504. Source,
  505. Destination,
  506. EFI_IP_PROTO_UDP,
  507. 0
  508. );
  509. } else {
  510. //
  511. // UdpSessionData is NULL, use the address and port information previously configured.
  512. //
  513. CopyMem (&Destination, &ConfigData->RemoteAddress, sizeof (IP4_ADDR));
  514. HeadSum = Instance->HeadSum;
  515. }
  516. //
  517. // calculate the checksum.
  518. //
  519. Udp4Header->Checksum = Udp4Checksum (Packet, HeadSum);
  520. if (Udp4Header->Checksum == 0) {
  521. //
  522. // If the calculated checksum is 0, fill the Checksum field with all ones.
  523. //
  524. Udp4Header->Checksum = 0xffff;
  525. }
  526. //
  527. // Fill the IpIo Override data.
  528. //
  529. if (TxData->GatewayAddress != NULL) {
  530. IP4_COPY_ADDRESS (&Override.Ip4OverrideData.GatewayAddress, TxData->GatewayAddress);
  531. } else {
  532. ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
  533. }
  534. Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_UDP;
  535. Override.Ip4OverrideData.TypeOfService = ConfigData->TypeOfService;
  536. Override.Ip4OverrideData.TimeToLive = ConfigData->TimeToLive;
  537. Override.Ip4OverrideData.DoNotFragment = ConfigData->DoNotFragment;
  538. //
  539. // Save the token into the TxToken map.
  540. //
  541. Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);
  542. if (EFI_ERROR (Status)) {
  543. goto FREE_PACKET;
  544. }
  545. //
  546. // Send out this datagram through IpIo.
  547. //
  548. IpDestAddr.Addr[0] = Destination;
  549. Status = IpIoSend (
  550. Udp4Service->IpIo,
  551. Packet,
  552. Instance->IpInfo,
  553. Instance,
  554. Token,
  555. &IpDestAddr,
  556. &Override
  557. );
  558. if (EFI_ERROR (Status)) {
  559. //
  560. // Remove this token from the TxTokens.
  561. //
  562. Udp4RemoveToken (&Instance->TxTokens, Token);
  563. }
  564. FREE_PACKET:
  565. NetbufFree (Packet);
  566. ON_EXIT:
  567. gBS->RestoreTPL (OldTpl);
  568. return Status;
  569. }
  570. /**
  571. Places an asynchronous receive request into the receiving queue.
  572. The Receive() function places a completion token into the receive packet queue.
  573. This function is always asynchronous.
  574. The caller must fill in the Token.Event field in the completion token, and this
  575. field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol
  576. driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
  577. is signaled. Providing a proper notification function and context for the event
  578. will enable the user to receive the notification and receiving status. That
  579. notification function is guaranteed to not be re-entered.
  580. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  581. @param[in] Token Pointer to a token that is associated with
  582. the receive data descriptor.
  583. @retval EFI_SUCCESS The receive completion token was cached.
  584. @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.
  585. @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)
  586. is not finished yet.
  587. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  588. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
  589. resources (usually memory).
  590. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  591. @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in
  592. the receive queue.
  593. @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
  594. **/
  595. EFI_STATUS
  596. EFIAPI
  597. Udp4Receive (
  598. IN EFI_UDP4_PROTOCOL *This,
  599. IN EFI_UDP4_COMPLETION_TOKEN *Token
  600. )
  601. {
  602. EFI_STATUS Status;
  603. UDP4_INSTANCE_DATA *Instance;
  604. EFI_TPL OldTpl;
  605. if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
  606. return EFI_INVALID_PARAMETER;
  607. }
  608. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  609. if (Instance->IsNoMapping) {
  610. return EFI_NO_MAPPING;
  611. }
  612. if (!Instance->Configured) {
  613. return EFI_NOT_STARTED;
  614. }
  615. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  616. if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token)) ||
  617. EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)))
  618. {
  619. //
  620. // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or
  621. // RxTokens map.
  622. //
  623. Status = EFI_ACCESS_DENIED;
  624. goto ON_EXIT;
  625. }
  626. Token->Packet.RxData = NULL;
  627. //
  628. // Save the token into the RxTokens map.
  629. //
  630. Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);
  631. if (EFI_ERROR (Status)) {
  632. Status = EFI_NOT_READY;
  633. goto ON_EXIT;
  634. }
  635. //
  636. // If there is an icmp error, report it.
  637. //
  638. Udp4ReportIcmpError (Instance);
  639. //
  640. // Try to deliver the received datagrams.
  641. //
  642. Udp4InstanceDeliverDgram (Instance);
  643. //
  644. // Dispatch the DPC queued by the NotifyFunction of Token->Event.
  645. //
  646. DispatchDpc ();
  647. ON_EXIT:
  648. gBS->RestoreTPL (OldTpl);
  649. return Status;
  650. }
  651. /**
  652. Aborts an asynchronous transmit or receive request.
  653. The Cancel() function is used to abort a pending transmit or receive request.
  654. If the token is in the transmit or receive request queues, after calling this
  655. function, Token.Status will be set to EFI_ABORTED and then Token.Event will be
  656. signaled. If the token is not in one of the queues, which usually means that
  657. the asynchronous operation has completed, this function will not signal the
  658. token and EFI_NOT_FOUND is returned.
  659. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  660. @param[in] Token Pointer to a token that has been issued by
  661. EFI_UDP4_PROTOCOL.Transmit() or
  662. EFI_UDP4_PROTOCOL.Receive().If NULL, all pending
  663. tokens are aborted.
  664. @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event
  665. was signaled. When Token is NULL, all pending requests are
  666. aborted and their events are signaled.
  667. @retval EFI_INVALID_PARAMETER This is NULL.
  668. @retval EFI_NOT_STARTED This instance has not been started.
  669. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  670. RARP, etc.) is not finished yet.
  671. @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
  672. not found in the transmit or receive queue. It has either completed
  673. or was not issued by Transmit() and Receive().
  674. **/
  675. EFI_STATUS
  676. EFIAPI
  677. Udp4Cancel (
  678. IN EFI_UDP4_PROTOCOL *This,
  679. IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL
  680. )
  681. {
  682. EFI_STATUS Status;
  683. UDP4_INSTANCE_DATA *Instance;
  684. EFI_TPL OldTpl;
  685. if (This == NULL) {
  686. return EFI_INVALID_PARAMETER;
  687. }
  688. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  689. if (Instance->IsNoMapping) {
  690. return EFI_NO_MAPPING;
  691. }
  692. if (!Instance->Configured) {
  693. return EFI_NOT_STARTED;
  694. }
  695. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  696. //
  697. // Cancel the tokens specified by Token for this instance.
  698. //
  699. Status = Udp4InstanceCancelToken (Instance, Token);
  700. //
  701. // Dispatch the DPC queued by the NotifyFunction of the cancelled token's events.
  702. //
  703. DispatchDpc ();
  704. gBS->RestoreTPL (OldTpl);
  705. return Status;
  706. }
  707. /**
  708. Polls for incoming data packets and processes outgoing data packets.
  709. The Poll() function can be used by network drivers and applications to increase
  710. the rate that data packets are moved between the communications device and the
  711. transmit and receive queues.
  712. In some systems, the periodic timer event in the managed network driver may not
  713. poll the underlying communications device fast enough to transmit and/or receive
  714. all data packets without missing incoming packets or dropping outgoing packets.
  715. Drivers and applications that are experiencing packet loss should try calling
  716. the Poll() function more often.
  717. @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.
  718. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  719. @retval EFI_INVALID_PARAMETER This is NULL.
  720. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  721. @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
  722. **/
  723. EFI_STATUS
  724. EFIAPI
  725. Udp4Poll (
  726. IN EFI_UDP4_PROTOCOL *This
  727. )
  728. {
  729. UDP4_INSTANCE_DATA *Instance;
  730. EFI_IP4_PROTOCOL *Ip;
  731. if (This == NULL) {
  732. return EFI_INVALID_PARAMETER;
  733. }
  734. Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);
  735. Ip = Instance->IpInfo->Ip.Ip4;
  736. //
  737. // Invode the Ip instance consumed by the udp instance to do the poll operation.
  738. //
  739. return Ip->Poll (Ip);
  740. }