]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Ip4Dxe/Ip4Impl.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / Ip4Dxe / Ip4Impl.c
CommitLineData
772db4bb 1/** @file\r
2\r
018432f0 3Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 4SPDX-License-Identifier: BSD-2-Clause-Patent\r
772db4bb 5\r
772db4bb 6**/\r
7\r
8#include "Ip4Impl.h"\r
9\r
d1050b9d 10EFI_IPSEC2_PROTOCOL *mIpSec = NULL;\r
a1503a32 11\r
5405e9a6 12/**\r
13 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
e2851998 14\r
5405e9a6 15 The GetModeData() function returns the current operational mode data for this\r
16 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
17 function is used optionally to retrieve the operational mode data of underlying\r
18 networks or drivers.\r
19\r
3e8c18da 20 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
21 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
22 @param[out] MnpConfigData Pointer to the managed network configuration data structure.\r
23 @param[out] SnpModeData Pointer to the simple network mode data structure.\r
5405e9a6 24\r
25 @retval EFI_SUCCESS The operation completed successfully.\r
26 @retval EFI_INVALID_PARAMETER This is NULL.\r
27 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.\r
772db4bb 28\r
5405e9a6 29**/\r
30EFI_STATUS\r
31EFIAPI\r
32EfiIp4GetModeData (\r
d1050b9d
MK
33 IN CONST EFI_IP4_PROTOCOL *This,\r
34 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,\r
35 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
36 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
5405e9a6 37 );\r
e2851998 38\r
772db4bb 39/**\r
5405e9a6 40 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
e2851998 41\r
5405e9a6 42 The Configure() function is used to set, change, or reset the operational\r
43 parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
44 parameters have been set, no network traffic can be sent or received by this\r
45 instance. Once the parameters have been reset (by calling this function with\r
46 IpConfigData set to NULL), no more traffic can be sent or received until these\r
47 parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
48 and stopped independently of each other by enabling or disabling their receive\r
49 filter settings with the Configure() function.\r
e2851998 50\r
5405e9a6 51 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
52 be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
53 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
54 to retrieve the default IPv4 address if it is not available yet. Clients could\r
55 frequently call GetModeData() to check the status to ensure that the default IPv4\r
56 address is ready.\r
e2851998 57\r
5405e9a6 58 If operational parameters are reset or changed, any pending transmit and receive\r
59 requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
60 and their events will be signaled.\r
61\r
3e8c18da 62 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
63 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.\r
5405e9a6 64\r
65 @retval EFI_SUCCESS The driver instance was successfully opened.\r
66 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
67 RARP, etc.) is not finished yet.\r
68 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
69 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:\r
70 A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
71 not be located when clients choose to use the default IPv4\r
72 address. This EFI IPv4 Protocol implementation does not\r
73 support this requested filter or timeout setting.\r
74 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.\r
75 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the\r
76 IPv4 address or subnet mask can be changed. The interface must\r
77 also be stopped when switching to/from raw packet mode.\r
78 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4\r
79 Protocol driver instance is not opened.\r
772db4bb 80\r
5405e9a6 81**/\r
82EFI_STATUS\r
83EFIAPI\r
84EfiIp4Configure (\r
d1050b9d
MK
85 IN EFI_IP4_PROTOCOL *This,\r
86 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL\r
5405e9a6 87 );\r
e2851998 88\r
5405e9a6 89/**\r
90 Joins and leaves multicast groups.\r
e2851998 91\r
5405e9a6 92 The Groups() function is used to join and leave multicast group sessions. Joining\r
93 a group will enable reception of matching multicast packets. Leaving a group will\r
94 disable the multicast packet reception.\r
e2851998 95\r
5405e9a6 96 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
97\r
3e8c18da 98 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
99 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.\r
100 @param[in] GroupAddress Pointer to the IPv4 multicast address.\r
5405e9a6 101\r
102 @retval EFI_SUCCESS The operation completed successfully.\r
103 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
104 - This is NULL.\r
105 - JoinFlag is TRUE and GroupAddress is NULL.\r
106 - GroupAddress is not NULL and *GroupAddress is\r
107 not a multicast IPv4 address.\r
108 @retval EFI_NOT_STARTED This instance has not been started.\r
109 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
110 RARP, etc.) is not finished yet.\r
111 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.\r
112 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.\r
113 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
114 JoinFlag is TRUE).\r
115 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).\r
116 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
772db4bb 117\r
5405e9a6 118**/\r
119EFI_STATUS\r
120EFIAPI\r
121EfiIp4Groups (\r
d1050b9d
MK
122 IN EFI_IP4_PROTOCOL *This,\r
123 IN BOOLEAN JoinFlag,\r
124 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
5405e9a6 125 );\r
e2851998 126\r
5405e9a6 127/**\r
128 Adds and deletes routing table entries.\r
129\r
130 The Routes() function adds a route to or deletes a route from the routing table.\r
e2851998 131\r
5405e9a6 132 Routes are determined by comparing the SubnetAddress with the destination IPv4\r
133 address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
134 on the same subnet as the configured station address.\r
e2851998 135\r
5405e9a6 136 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
137 The default route matches all destination IPv4 addresses that do not match any\r
138 other routes.\r
e2851998 139\r
5405e9a6 140 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
141 IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
142 nonroute entry will be inserted into the routing table for outgoing packets that\r
143 are addressed to a local subnet (gateway address of 0.0.0.0).\r
e2851998 144\r
5405e9a6 145 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
146 IPv4 Protocol instances that use the default IPv4 address will also have copies\r
147 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
148 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
149 instances. As a result, client modification to the routing table will be lost.\r
150\r
3e8c18da 151 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
152 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to\r
153 FALSE to add this route to the routing table. SubnetAddress\r
154 and SubnetMask are used as the key to each route entry.\r
155 @param[in] SubnetAddress The address of the subnet that needs to be routed.\r
156 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
157 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.\r
5405e9a6 158\r
159 @retval EFI_SUCCESS The operation completed successfully.\r
160 @retval EFI_NOT_STARTED The driver instance has not been started.\r
161 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
162 RARP, etc.) is not finished yet.\r
163 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
164 - This is NULL.\r
165 - SubnetAddress is NULL.\r
166 - SubnetMask is NULL.\r
167 - GatewayAddress is NULL.\r
168 - *SubnetAddress is not a valid subnet address.\r
169 - *SubnetMask is not a valid subnet mask.\r
170 - *GatewayAddress is not a valid unicast IPv4 address.\r
171 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
172 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).\r
173 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when\r
174 DeleteRoute is FALSE).\r
e2851998 175\r
5405e9a6 176**/\r
177EFI_STATUS\r
178EFIAPI\r
179EfiIp4Routes (\r
d1050b9d
MK
180 IN EFI_IP4_PROTOCOL *This,\r
181 IN BOOLEAN DeleteRoute,\r
182 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
183 IN EFI_IPv4_ADDRESS *SubnetMask,\r
184 IN EFI_IPv4_ADDRESS *GatewayAddress\r
5405e9a6 185 );\r
e2851998 186\r
5405e9a6 187/**\r
188 Places outgoing data packets into the transmit queue.\r
189\r
190 The Transmit() function places a sending request in the transmit queue of this\r
191 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
192 errors occur, the event in the token will be signaled and the status is updated.\r
193\r
3e8c18da 194 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
195 @param[in] Token Pointer to the transmit token.\r
5405e9a6 196\r
197 @retval EFI_SUCCESS The data has been queued for transmission.\r
198 @retval EFI_NOT_STARTED This instance has not been started.\r
199 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
200 RARP, etc.) is not finished yet.\r
6c585b52 201 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
5405e9a6 202 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event\r
203 was already in the transmit queue.\r
204 @retval EFI_NOT_READY The completion token could not be queued because the transmit\r
e2851998 205 queue is full.\r
5405e9a6 206 @retval EFI_NOT_FOUND Not route is found to destination address.\r
207 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
208 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too\r
209 short to transmit.\r
210 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is\r
211 greater than MTU (or greater than the maximum packet size if\r
212 Token.Packet.TxData.OverrideData.\r
213 DoNotFragment is TRUE.)\r
214\r
215**/\r
216EFI_STATUS\r
217EFIAPI\r
218EfiIp4Transmit (\r
d1050b9d
MK
219 IN EFI_IP4_PROTOCOL *This,\r
220 IN EFI_IP4_COMPLETION_TOKEN *Token\r
5405e9a6 221 );\r
e2851998 222\r
5405e9a6 223/**\r
224 Places a receiving request into the receiving queue.\r
e2851998 225\r
5405e9a6 226 The Receive() function places a completion token into the receive packet queue.\r
227 This function is always asynchronous.\r
e2851998 228\r
5405e9a6 229 The Token.Event field in the completion token must be filled in by the caller\r
230 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
231 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
232 is signaled.\r
233\r
3e8c18da 234 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
235 @param[in] Token Pointer to a token that is associated with the receive data descriptor.\r
5405e9a6 236\r
237 @retval EFI_SUCCESS The receive completion token was cached.\r
238 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
239 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
240 is not finished yet.\r
241 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
242 - This is NULL.\r
243 - Token is NULL.\r
244 - Token.Event is NULL.\r
245 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
246 resources (usually memory).\r
247 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
248 The EFI IPv4 Protocol instance has been reset to startup defaults.\r
249 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
250 in the receive queue.\r
251 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
252 @retval EFI_ICMP_ERROR An ICMP error packet was received.\r
253\r
254**/\r
255EFI_STATUS\r
256EFIAPI\r
257EfiIp4Receive (\r
d1050b9d
MK
258 IN EFI_IP4_PROTOCOL *This,\r
259 IN EFI_IP4_COMPLETION_TOKEN *Token\r
5405e9a6 260 );\r
e2851998 261\r
5405e9a6 262/**\r
263 Abort an asynchronous transmit or receive request.\r
e2851998 264\r
5405e9a6 265 The Cancel() function is used to abort a pending transmit or receive request.\r
266 If the token is in the transmit or receive request queues, after calling this\r
267 function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
268 be signaled. If the token is not in one of the queues, which usually means the\r
269 asynchronous operation has completed, this function will not signal the token\r
270 and EFI_NOT_FOUND is returned.\r
271\r
3e8c18da 272 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
273 @param[in] Token Pointer to a token that has been issued by\r
274 EFI_IP4_PROTOCOL.Transmit() or\r
275 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
276 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
277 defined in EFI_IP4_PROTOCOL.Transmit().\r
5405e9a6 278\r
279 @retval EFI_SUCCESS The asynchronous I/O request was aborted and\r
280 Token.->Event was signaled. When Token is NULL, all\r
281 pending requests were aborted and their events were signaled.\r
282 @retval EFI_INVALID_PARAMETER This is NULL.\r
283 @retval EFI_NOT_STARTED This instance has not been started.\r
284 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
285 RARP, etc.) is not finished yet.\r
286 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
287 not found in the transmit or receive queue. It has either completed\r
288 or was not issued by Transmit() and Receive().\r
289\r
290**/\r
291EFI_STATUS\r
292EFIAPI\r
293EfiIp4Cancel (\r
d1050b9d
MK
294 IN EFI_IP4_PROTOCOL *This,\r
295 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
5405e9a6 296 );\r
e2851998 297\r
5405e9a6 298/**\r
299 Polls for incoming data packets and processes outgoing data packets.\r
e2851998 300\r
5405e9a6 301 The Poll() function polls for incoming data packets and processes outgoing data\r
302 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
303 function to increase the rate that data packets are moved between the communications\r
304 device and the transmit and receive queues.\r
e2851998 305\r
5405e9a6 306 In some systems the periodic timer event may not poll the underlying communications\r
307 device fast enough to transmit and/or receive all data packets without missing\r
308 incoming packets or dropping outgoing packets. Drivers and applications that are\r
309 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
310 more often.\r
311\r
3e8c18da 312 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
5405e9a6 313\r
314 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
315 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
316 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
317 RARP, etc.) is not finished yet.\r
318 @retval EFI_INVALID_PARAMETER This is NULL.\r
319 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
320 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
321 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
322 Consider increasing the polling rate.\r
323\r
324**/\r
325EFI_STATUS\r
326EFIAPI\r
327EfiIp4Poll (\r
d1050b9d 328 IN EFI_IP4_PROTOCOL *This\r
5405e9a6 329 );\r
330\r
331EFI_IP4_PROTOCOL\r
d1050b9d 332 mEfiIp4ProtocolTemplete = {\r
5405e9a6 333 EfiIp4GetModeData,\r
334 EfiIp4Configure,\r
335 EfiIp4Groups,\r
336 EfiIp4Routes,\r
337 EfiIp4Transmit,\r
338 EfiIp4Receive,\r
339 EfiIp4Cancel,\r
340 EfiIp4Poll\r
341};\r
342\r
343/**\r
344 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.\r
e2851998 345\r
5405e9a6 346 The GetModeData() function returns the current operational mode data for this\r
347 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This\r
348 function is used optionally to retrieve the operational mode data of underlying\r
349 networks or drivers.\r
350\r
3e8c18da 351 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
352 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
353 @param[out] MnpConfigData Pointer to the managed network configuration data structure.\r
354 @param[out] SnpModeData Pointer to the simple network mode data structure.\r
5405e9a6 355\r
356 @retval EFI_SUCCESS The operation completed successfully.\r
357 @retval EFI_INVALID_PARAMETER This is NULL.\r
358 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.\r
772db4bb 359\r
360**/\r
772db4bb 361EFI_STATUS\r
362EFIAPI\r
363EfiIp4GetModeData (\r
d1050b9d
MK
364 IN CONST EFI_IP4_PROTOCOL *This,\r
365 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,\r
366 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
367 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
772db4bb 368 )\r
369{\r
d1050b9d
MK
370 IP4_PROTOCOL *IpInstance;\r
371 IP4_SERVICE *IpSb;\r
372 EFI_IP4_CONFIG_DATA *Config;\r
373 EFI_STATUS Status;\r
374 EFI_TPL OldTpl;\r
375 IP4_ADDR Ip;\r
772db4bb 376\r
377 if (This == NULL) {\r
378 return EFI_INVALID_PARAMETER;\r
379 }\r
380\r
e48e37fc 381 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 382 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
383 IpSb = IpInstance->Service;\r
384\r
385 if (Ip4ModeData != NULL) {\r
386 //\r
387 // IsStarted is "whether the EfiIp4Configure has been called".\r
388 // IsConfigured is "whether the station address has been configured"\r
389 //\r
d1050b9d 390 Ip4ModeData->IsStarted = (BOOLEAN)(IpInstance->State == IP4_STATE_CONFIGED);\r
687a2e5f 391 CopyMem (&Ip4ModeData->ConfigData, &IpInstance->ConfigData, sizeof (Ip4ModeData->ConfigData));\r
d1050b9d 392 Ip4ModeData->IsConfigured = FALSE;\r
772db4bb 393\r
d1050b9d
MK
394 Ip4ModeData->GroupCount = IpInstance->GroupCount;\r
395 Ip4ModeData->GroupTable = (EFI_IPv4_ADDRESS *)IpInstance->Groups;\r
772db4bb 396\r
397 Ip4ModeData->IcmpTypeCount = 23;\r
398 Ip4ModeData->IcmpTypeList = mIp4SupportedIcmp;\r
399\r
d1050b9d
MK
400 Ip4ModeData->RouteTable = NULL;\r
401 Ip4ModeData->RouteCount = 0;\r
772db4bb 402\r
a1503a32 403 Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize;\r
404\r
772db4bb 405 //\r
406 // return the current station address for this IP child. So,\r
407 // the user can get the default address through this. Some\r
408 // application wants to know it station address even it is\r
409 // using the default one, such as a ftp server.\r
410 //\r
411 if (Ip4ModeData->IsStarted) {\r
d1050b9d 412 Config = &Ip4ModeData->ConfigData;\r
772db4bb 413\r
414 Ip = HTONL (IpInstance->Interface->Ip);\r
e48e37fc 415 CopyMem (&Config->StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 416\r
417 Ip = HTONL (IpInstance->Interface->SubnetMask);\r
e48e37fc 418 CopyMem (&Config->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 419\r
420 Ip4ModeData->IsConfigured = IpInstance->Interface->Configured;\r
421\r
422 //\r
423 // Build a EFI route table for user from the internal route table.\r
424 //\r
425 Status = Ip4BuildEfiRouteTable (IpInstance);\r
426\r
427 if (EFI_ERROR (Status)) {\r
e48e37fc 428 gBS->RestoreTPL (OldTpl);\r
772db4bb 429 return Status;\r
430 }\r
431\r
432 Ip4ModeData->RouteTable = IpInstance->EfiRouteTable;\r
433 Ip4ModeData->RouteCount = IpInstance->EfiRouteCount;\r
434 }\r
435 }\r
436\r
dd29f3ed 437 //\r
438 // Get fresh mode data from MNP, since underlying media status may change\r
439 //\r
440 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData);\r
772db4bb 441\r
e48e37fc 442 gBS->RestoreTPL (OldTpl);\r
dd29f3ed 443 return Status;\r
772db4bb 444}\r
445\r
772db4bb 446/**\r
447 Config the MNP parameter used by IP. The IP driver use one MNP\r
448 child to transmit/receive frames. By default, it configures MNP\r
449 to receive unicast/multicast/broadcast. And it will enable/disable\r
450 the promiscous receive according to whether there is IP child\r
5405e9a6 451 enable that or not. If Force is FALSE, it will iterate through\r
772db4bb 452 all the IP children to check whether the promiscuous receive\r
453 setting has been changed. If it hasn't been changed, it won't\r
5405e9a6 454 reconfigure the MNP. If Force is TRUE, the MNP is configured no\r
772db4bb 455 matter whether that is changed or not.\r
456\r
3e8c18da 457 @param[in] IpSb The IP4 service instance that is to be changed.\r
458 @param[in] Force Force the configuration or not.\r
772db4bb 459\r
460 @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.\r
461 @retval Others Configuration failed.\r
462\r
463**/\r
464EFI_STATUS\r
465Ip4ServiceConfigMnp (\r
d1050b9d
MK
466 IN IP4_SERVICE *IpSb,\r
467 IN BOOLEAN Force\r
772db4bb 468 )\r
469{\r
d1050b9d
MK
470 LIST_ENTRY *Entry;\r
471 LIST_ENTRY *ProtoEntry;\r
472 IP4_INTERFACE *IpIf;\r
473 IP4_PROTOCOL *IpInstance;\r
474 BOOLEAN Reconfig;\r
475 BOOLEAN PromiscReceive;\r
476 EFI_STATUS Status;\r
772db4bb 477\r
478 Reconfig = FALSE;\r
479 PromiscReceive = FALSE;\r
480\r
481 if (!Force) {\r
482 //\r
483 // Iterate through the IP children to check whether promiscuous\r
484 // receive setting has been changed. Update the interface's receive\r
485 // filter also.\r
486 //\r
487 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
772db4bb 488 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
489 IpIf->PromiscRecv = FALSE;\r
490\r
491 NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) {\r
492 IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP4_PROTOCOL, AddrLink);\r
493\r
494 if (IpInstance->ConfigData.AcceptPromiscuous) {\r
495 IpIf->PromiscRecv = TRUE;\r
496 PromiscReceive = TRUE;\r
497 }\r
498 }\r
499 }\r
500\r
501 //\r
502 // If promiscuous receive isn't changed, it isn't necessary to reconfigure.\r
503 //\r
504 if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) {\r
505 return EFI_SUCCESS;\r
506 }\r
507\r
d1050b9d 508 Reconfig = TRUE;\r
772db4bb 509 IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive;\r
510 }\r
511\r
512 Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData);\r
513\r
514 //\r
515 // recover the original configuration if failed to set the configure.\r
516 //\r
517 if (EFI_ERROR (Status) && Reconfig) {\r
687a2e5f 518 IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive;\r
772db4bb 519 }\r
520\r
521 return Status;\r
522}\r
523\r
772db4bb 524/**\r
6c585b52 525 Initialize the IP4_PROTOCOL structure to the unconfigured states.\r
772db4bb 526\r
527 @param IpSb The IP4 service instance.\r
528 @param IpInstance The IP4 child instance.\r
529\r
772db4bb 530**/\r
531VOID\r
532Ip4InitProtocol (\r
d1050b9d
MK
533 IN IP4_SERVICE *IpSb,\r
534 IN OUT IP4_PROTOCOL *IpInstance\r
772db4bb 535 )\r
536{\r
537 ASSERT ((IpSb != NULL) && (IpInstance != NULL));\r
538\r
e48e37fc 539 ZeroMem (IpInstance, sizeof (IP4_PROTOCOL));\r
772db4bb 540\r
541 IpInstance->Signature = IP4_PROTOCOL_SIGNATURE;\r
687a2e5f 542 CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto));\r
772db4bb 543 IpInstance->State = IP4_STATE_UNCONFIGED;\r
d1050b9d 544 IpInstance->InDestroy = FALSE;\r
772db4bb 545 IpInstance->Service = IpSb;\r
546\r
e48e37fc 547 InitializeListHead (&IpInstance->Link);\r
d1050b9d
MK
548 NetMapInit (&IpInstance->RxTokens);\r
549 NetMapInit (&IpInstance->TxTokens);\r
e48e37fc 550 InitializeListHead (&IpInstance->Received);\r
551 InitializeListHead (&IpInstance->Delivered);\r
552 InitializeListHead (&IpInstance->AddrLink);\r
772db4bb 553\r
e48e37fc 554 EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);\r
772db4bb 555}\r
556\r
772db4bb 557/**\r
558 Configure the IP4 child. If the child is already configured,\r
559 change the configuration parameter. Otherwise configure it\r
560 for the first time. The caller should validate the configuration\r
561 before deliver them to it. It also don't do configure NULL.\r
562\r
3e8c18da 563 @param[in, out] IpInstance The IP4 child to configure.\r
564 @param[in] Config The configure data.\r
772db4bb 565\r
566 @retval EFI_SUCCESS The IP4 child is successfully configured.\r
567 @retval EFI_DEVICE_ERROR Failed to free the pending transive or to\r
568 configure underlying MNP or other errors.\r
569 @retval EFI_NO_MAPPING The IP4 child is configured to use default\r
570 address, but the default address hasn't been\r
571 configured. The IP4 child doesn't need to be\r
572 reconfigured when default address is configured.\r
5405e9a6 573 @retval EFI_OUT_OF_RESOURCES No more memory space is available.\r
574 @retval other Other error occurs.\r
772db4bb 575\r
576**/\r
577EFI_STATUS\r
578Ip4ConfigProtocol (\r
5405e9a6 579 IN OUT IP4_PROTOCOL *IpInstance,\r
580 IN EFI_IP4_CONFIG_DATA *Config\r
772db4bb 581 )\r
582{\r
583 IP4_SERVICE *IpSb;\r
584 IP4_INTERFACE *IpIf;\r
585 EFI_STATUS Status;\r
586 IP4_ADDR Ip;\r
587 IP4_ADDR Netmask;\r
216f7970 588 EFI_ARP_PROTOCOL *Arp;\r
ad1484c3
JW
589 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;\r
590 EFI_IP4_CONFIG2_POLICY Policy;\r
772db4bb 591\r
592 IpSb = IpInstance->Service;\r
593\r
d1050b9d 594 Ip4Config2 = NULL;\r
ad1484c3 595\r
772db4bb 596 //\r
597 // User is changing packet filters. It must be stopped\r
598 // before the station address can be changed.\r
599 //\r
600 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
601 //\r
602 // Cancel all the pending transmit/receive from upper layer\r
603 //\r
604 Status = Ip4Cancel (IpInstance, NULL);\r
605\r
606 if (EFI_ERROR (Status)) {\r
607 return EFI_DEVICE_ERROR;\r
608 }\r
609\r
687a2e5f 610 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
772db4bb 611 return EFI_SUCCESS;\r
612 }\r
613\r
614 //\r
615 // Configure a fresh IP4 protocol instance. Create a route table.\r
616 // Each IP child has its own route table, which may point to the\r
617 // default table if it is using default address.\r
618 //\r
619 Status = EFI_OUT_OF_RESOURCES;\r
620 IpInstance->RouteTable = Ip4CreateRouteTable ();\r
621\r
622 if (IpInstance->RouteTable == NULL) {\r
623 return Status;\r
624 }\r
625\r
626 //\r
627 // Set up the interface.\r
628 //\r
e48e37fc 629 CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR));\r
630 CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 631\r
632 Ip = NTOHL (Ip);\r
633 Netmask = NTOHL (Netmask);\r
634\r
635 if (!Config->UseDefaultAddress) {\r
636 //\r
637 // Find whether there is already an interface with the same\r
638 // station address. All the instances with the same station\r
639 // address shares one interface.\r
640 //\r
641 IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask);\r
642\r
643 if (IpIf != NULL) {\r
644 NET_GET_REF (IpIf);\r
772db4bb 645 } else {\r
646 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);\r
647\r
648 if (IpIf == NULL) {\r
649 goto ON_ERROR;\r
650 }\r
651\r
652 Status = Ip4SetAddress (IpIf, Ip, Netmask);\r
653\r
654 if (EFI_ERROR (Status)) {\r
655 Status = EFI_DEVICE_ERROR;\r
656 Ip4FreeInterface (IpIf, IpInstance);\r
657 goto ON_ERROR;\r
658 }\r
659\r
e48e37fc 660 InsertTailList (&IpSb->Interfaces, &IpIf->Link);\r
772db4bb 661 }\r
662\r
663 //\r
ecbd055d 664 // Add a route to this connected network in the instance route table.\r
772db4bb 665 //\r
ecbd055d 666 Ip4AddRoute (\r
1737b7e3
JW
667 IpInstance->RouteTable,\r
668 Ip & Netmask,\r
669 Netmask,\r
ecbd055d
JW
670 IP4_ALLZERO_ADDRESS\r
671 );\r
772db4bb 672 } else {\r
673 //\r
7648748e 674 // Use the default address. Check the state.\r
772db4bb 675 //\r
676 if (IpSb->State == IP4_SERVICE_UNSTARTED) {\r
ad1484c3 677 //\r
4a76d9b9 678 // Trigger the EFI_IP4_CONFIG2_PROTOCOL to retrieve the\r
ad1484c3
JW
679 // default IPv4 address if it is not available yet.\r
680 //\r
681 Policy = IpSb->Ip4Config2Instance.Policy;\r
682 if (Policy != Ip4Config2PolicyDhcp) {\r
683 Ip4Config2 = &IpSb->Ip4Config2Instance.Ip4Config2;\r
d1050b9d
MK
684 Policy = Ip4Config2PolicyDhcp;\r
685 Status = Ip4Config2->SetData (\r
686 Ip4Config2,\r
687 Ip4Config2DataTypePolicy,\r
688 sizeof (EFI_IP4_CONFIG2_POLICY),\r
689 &Policy\r
690 );\r
ad1484c3
JW
691 if (EFI_ERROR (Status)) {\r
692 goto ON_ERROR;\r
693 }\r
923473ed 694 }\r
772db4bb 695 }\r
696\r
697 IpIf = IpSb->DefaultInterface;\r
698 NET_GET_REF (IpSb->DefaultInterface);\r
699\r
700 //\r
701 // If default address is used, so is the default route table.\r
702 // Any route set by the instance has the precedence over the\r
703 // routes in the default route table. Link the default table\r
704 // after the instance's table. Routing will search the local\r
705 // table first.\r
706 //\r
707 NET_GET_REF (IpSb->DefaultRouteTable);\r
708 IpInstance->RouteTable->Next = IpSb->DefaultRouteTable;\r
709 }\r
710\r
711 IpInstance->Interface = IpIf;\r
216f7970 712 if (IpIf->Arp != NULL) {\r
d1050b9d 713 Arp = NULL;\r
216f7970 714 Status = gBS->OpenProtocol (\r
715 IpIf->ArpHandle,\r
716 &gEfiArpProtocolGuid,\r
d1050b9d 717 (VOID **)&Arp,\r
216f7970 718 gIp4DriverBinding.DriverBindingHandle,\r
719 IpInstance->Handle,\r
720 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
721 );\r
722 if (EFI_ERROR (Status)) {\r
7155d0a6 723 Ip4FreeInterface (IpIf, IpInstance);\r
7648748e 724 goto ON_ERROR;\r
216f7970 725 }\r
726 }\r
d1050b9d 727\r
e48e37fc 728 InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);\r
772db4bb 729\r
687a2e5f 730 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));\r
d1050b9d 731 IpInstance->State = IP4_STATE_CONFIGED;\r
772db4bb 732\r
733 //\r
734 // Although EFI_NO_MAPPING is an error code, the IP child has been\r
735 // successfully configured and doesn't need reconfiguration when\r
736 // default address is acquired.\r
737 //\r
738 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
739 return EFI_NO_MAPPING;\r
740 }\r
741\r
742 return EFI_SUCCESS;\r
743\r
744ON_ERROR:\r
745 Ip4FreeRouteTable (IpInstance->RouteTable);\r
746 IpInstance->RouteTable = NULL;\r
747 return Status;\r
748}\r
749\r
772db4bb 750/**\r
751 Clean up the IP4 child, release all the resources used by it.\r
752\r
3e8c18da 753 @param[in] IpInstance The IP4 child to clean up.\r
772db4bb 754\r
1f6729ff 755 @retval EFI_SUCCESS The IP4 child is cleaned up.\r
756 @retval EFI_DEVICE_ERROR Some resources failed to be released.\r
772db4bb 757\r
758**/\r
759EFI_STATUS\r
760Ip4CleanProtocol (\r
d1050b9d 761 IN IP4_PROTOCOL *IpInstance\r
772db4bb 762 )\r
763{\r
764 if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) {\r
765 return EFI_DEVICE_ERROR;\r
766 }\r
767\r
768 if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) {\r
769 return EFI_DEVICE_ERROR;\r
770 }\r
771\r
772 //\r
773 // Some packets haven't been recycled. It is because either the\r
774 // user forgets to recycle the packets, or because the callback\r
775 // hasn't been called. Just leave it alone.\r
776 //\r
e48e37fc 777 if (!IsListEmpty (&IpInstance->Delivered)) {\r
772db4bb 778 }\r
779\r
780 if (IpInstance->Interface != NULL) {\r
e48e37fc 781 RemoveEntryList (&IpInstance->AddrLink);\r
216f7970 782 if (IpInstance->Interface->Arp != NULL) {\r
783 gBS->CloseProtocol (\r
784 IpInstance->Interface->ArpHandle,\r
785 &gEfiArpProtocolGuid,\r
786 gIp4DriverBinding.DriverBindingHandle,\r
787 IpInstance->Handle\r
788 );\r
789 }\r
d1050b9d 790\r
772db4bb 791 Ip4FreeInterface (IpInstance->Interface, IpInstance);\r
792 IpInstance->Interface = NULL;\r
793 }\r
794\r
795 if (IpInstance->RouteTable != NULL) {\r
796 if (IpInstance->RouteTable->Next != NULL) {\r
797 Ip4FreeRouteTable (IpInstance->RouteTable->Next);\r
798 }\r
799\r
800 Ip4FreeRouteTable (IpInstance->RouteTable);\r
801 IpInstance->RouteTable = NULL;\r
802 }\r
803\r
804 if (IpInstance->EfiRouteTable != NULL) {\r
766c7483 805 FreePool (IpInstance->EfiRouteTable);\r
772db4bb 806 IpInstance->EfiRouteTable = NULL;\r
807 IpInstance->EfiRouteCount = 0;\r
808 }\r
809\r
810 if (IpInstance->Groups != NULL) {\r
766c7483 811 FreePool (IpInstance->Groups);\r
d1050b9d
MK
812 IpInstance->Groups = NULL;\r
813 IpInstance->GroupCount = 0;\r
772db4bb 814 }\r
815\r
816 NetMapClean (&IpInstance->TxTokens);\r
817\r
818 NetMapClean (&IpInstance->RxTokens);\r
819\r
820 return EFI_SUCCESS;\r
821}\r
822\r
772db4bb 823/**\r
5405e9a6 824 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.\r
e2851998 825\r
5405e9a6 826 The Configure() function is used to set, change, or reset the operational\r
827 parameters and filter settings for this EFI IPv4 Protocol instance. Until these\r
828 parameters have been set, no network traffic can be sent or received by this\r
829 instance. Once the parameters have been reset (by calling this function with\r
830 IpConfigData set to NULL), no more traffic can be sent or received until these\r
831 parameters have been set again. Each EFI IPv4 Protocol instance can be started\r
832 and stopped independently of each other by enabling or disabling their receive\r
833 filter settings with the Configure() function.\r
e2851998 834\r
5405e9a6 835 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will\r
836 be appended as an alias address into the addresses list in the EFI IPv4 Protocol\r
837 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL\r
838 to retrieve the default IPv4 address if it is not available yet. Clients could\r
839 frequently call GetModeData() to check the status to ensure that the default IPv4\r
840 address is ready.\r
e2851998 841\r
5405e9a6 842 If operational parameters are reset or changed, any pending transmit and receive\r
843 requests will be cancelled. Their completion token status will be set to EFI_ABORTED\r
844 and their events will be signaled.\r
845\r
3e8c18da 846 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
847 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.\r
5405e9a6 848\r
849 @retval EFI_SUCCESS The driver instance was successfully opened.\r
850 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
851 RARP, etc.) is not finished yet.\r
852 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
853 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:\r
854 A configuration protocol (DHCP, BOOTP, RARP, etc.) could\r
855 not be located when clients choose to use the default IPv4\r
856 address. This EFI IPv4 Protocol implementation does not\r
857 support this requested filter or timeout setting.\r
858 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.\r
859 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the\r
860 IPv4 address or subnet mask can be changed. The interface must\r
861 also be stopped when switching to/from raw packet mode.\r
862 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4\r
863 Protocol driver instance is not opened.\r
772db4bb 864\r
865**/\r
772db4bb 866EFI_STATUS\r
867EFIAPI\r
868EfiIp4Configure (\r
d1050b9d
MK
869 IN EFI_IP4_PROTOCOL *This,\r
870 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL\r
772db4bb 871 )\r
872{\r
d1050b9d
MK
873 IP4_PROTOCOL *IpInstance;\r
874 EFI_IP4_CONFIG_DATA *Current;\r
875 EFI_TPL OldTpl;\r
876 EFI_STATUS Status;\r
877 BOOLEAN AddrOk;\r
878 IP4_ADDR IpAddress;\r
879 IP4_ADDR SubnetMask;\r
772db4bb 880\r
881 //\r
882 // First, validate the parameters\r
883 //\r
884 if (This == NULL) {\r
885 return EFI_INVALID_PARAMETER;\r
886 }\r
887\r
888 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 889 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 890\r
891 //\r
892 // Validate the configuration first.\r
893 //\r
894 if (IpConfigData != NULL) {\r
e48e37fc 895 CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));\r
896 CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));\r
772db4bb 897\r
898 IpAddress = NTOHL (IpAddress);\r
899 SubnetMask = NTOHL (SubnetMask);\r
900\r
901 //\r
902 // Check whether the station address is a valid unicast address\r
903 //\r
904 if (!IpConfigData->UseDefaultAddress) {\r
905 AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask);\r
906\r
907 if (!AddrOk) {\r
908 Status = EFI_INVALID_PARAMETER;\r
909 goto ON_EXIT;\r
910 }\r
911 }\r
912\r
913 //\r
914 // User can only update packet filters when already configured.\r
915 // If it wants to change the station address, it must configure(NULL)\r
916 // the instance first.\r
917 //\r
918 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
919 Current = &IpInstance->ConfigData;\r
920\r
921 if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) {\r
922 Status = EFI_ALREADY_STARTED;\r
923 goto ON_EXIT;\r
924 }\r
925\r
926 if (!Current->UseDefaultAddress &&\r
d1050b9d
MK
927 (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) ||\r
928 !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask)))\r
929 {\r
772db4bb 930 Status = EFI_ALREADY_STARTED;\r
931 goto ON_EXIT;\r
932 }\r
933\r
934 if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
ae0a0b06 935 Status = EFI_NO_MAPPING;\r
936 goto ON_EXIT;\r
772db4bb 937 }\r
938 }\r
939 }\r
940\r
941 //\r
942 // Configure the instance or clean it up.\r
943 //\r
944 if (IpConfigData != NULL) {\r
945 Status = Ip4ConfigProtocol (IpInstance, IpConfigData);\r
946 } else {\r
947 Status = Ip4CleanProtocol (IpInstance);\r
948\r
949 //\r
4bb89650 950 // Consider the following valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,\r
772db4bb 951 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,\r
952 // the unload fails miserably.\r
953 //\r
954 if (IpInstance->State == IP4_STATE_CONFIGED) {\r
955 IpInstance->State = IP4_STATE_UNCONFIGED;\r
956 }\r
957 }\r
958\r
959 //\r
960 // Update the MNP's configure data. Ip4ServiceConfigMnp will check\r
961 // whether it is necessary to reconfigure the MNP.\r
962 //\r
963 Ip4ServiceConfigMnp (IpInstance->Service, FALSE);\r
964\r
772db4bb 965ON_EXIT:\r
e48e37fc 966 gBS->RestoreTPL (OldTpl);\r
772db4bb 967 return Status;\r
772db4bb 968}\r
969\r
772db4bb 970/**\r
971 Change the IP4 child's multicast setting. The caller\r
972 should make sure that the parameters is valid.\r
973\r
3e8c18da 974 @param[in] IpInstance The IP4 child to change the setting.\r
1f6729ff 975 @param[in] JoinFlag TRUE to join the group, otherwise leave it.\r
976 @param[in] GroupAddress The target group address.\r
772db4bb 977\r
1f6729ff 978 @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it.\r
772db4bb 979 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.\r
6c585b52 980 @retval EFI_DEVICE_ERROR Failed to set the group configuration.\r
772db4bb 981 @retval EFI_SUCCESS Successfully updated the group setting.\r
982 @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.\r
983\r
984**/\r
985EFI_STATUS\r
986Ip4Groups (\r
d1050b9d
MK
987 IN IP4_PROTOCOL *IpInstance,\r
988 IN BOOLEAN JoinFlag,\r
989 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
772db4bb 990 )\r
991{\r
d1050b9d
MK
992 IP4_ADDR *Members;\r
993 IP4_ADDR Group;\r
994 UINT32 Index;\r
772db4bb 995\r
996 //\r
997 // Add it to the instance's Groups, and join the group by IGMP.\r
998 // IpInstance->Groups is in network byte order. IGMP operates in\r
999 // host byte order\r
1000 //\r
1001 if (JoinFlag) {\r
e2851998 1002 //\r
894d038a 1003 // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.\r
1004 //\r
1005 ASSERT (GroupAddress != NULL);\r
e48e37fc 1006 CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1007\r
1008 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
1009 if (IpInstance->Groups[Index] == Group) {\r
1010 return EFI_ALREADY_STARTED;\r
1011 }\r
1012 }\r
1013\r
1014 Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1015\r
1016 if (Members == NULL) {\r
1017 return EFI_OUT_OF_RESOURCES;\r
1018 }\r
1019\r
1020 if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {\r
766c7483 1021 FreePool (Members);\r
772db4bb 1022 return EFI_DEVICE_ERROR;\r
1023 }\r
1024\r
1025 if (IpInstance->Groups != NULL) {\r
766c7483 1026 FreePool (IpInstance->Groups);\r
772db4bb 1027 }\r
1028\r
1029 IpInstance->Groups = Members;\r
1030 IpInstance->GroupCount++;\r
1031\r
1032 return EFI_SUCCESS;\r
1033 }\r
1034\r
1035 //\r
1036 // Leave the group. Leave all the groups if GroupAddress is NULL.\r
1037 // Must iterate from the end to the beginning because the GroupCount\r
6c585b52 1038 // is decremented each time an address is removed..\r
772db4bb 1039 //\r
d1050b9d 1040 for (Index = IpInstance->GroupCount; Index > 0; Index--) {\r
a5f525fc
FS
1041 ASSERT (IpInstance->Groups != NULL);\r
1042 Group = IpInstance->Groups[Index - 1];\r
84b5c78e 1043 if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {\r
772db4bb 1044 if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {\r
1045 return EFI_DEVICE_ERROR;\r
1046 }\r
1047\r
1048 Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group);\r
1049 IpInstance->GroupCount--;\r
1050\r
1051 if (IpInstance->GroupCount == 0) {\r
1052 ASSERT (Index == 1);\r
1053\r
766c7483 1054 FreePool (IpInstance->Groups);\r
772db4bb 1055 IpInstance->Groups = NULL;\r
1056 }\r
1057\r
1058 if (GroupAddress != NULL) {\r
1059 return EFI_SUCCESS;\r
1060 }\r
1061 }\r
1062 }\r
1063\r
1064 return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);\r
1065}\r
1066\r
772db4bb 1067/**\r
5405e9a6 1068 Joins and leaves multicast groups.\r
e2851998 1069\r
5405e9a6 1070 The Groups() function is used to join and leave multicast group sessions. Joining\r
1071 a group will enable reception of matching multicast packets. Leaving a group will\r
1072 disable the multicast packet reception.\r
e2851998 1073\r
5405e9a6 1074 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.\r
1075\r
3e8c18da 1076 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1077 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.\r
1078 @param[in] GroupAddress Pointer to the IPv4 multicast address.\r
5405e9a6 1079\r
1080 @retval EFI_SUCCESS The operation completed successfully.\r
1081 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:\r
1082 - This is NULL.\r
1083 - JoinFlag is TRUE and GroupAddress is NULL.\r
1084 - GroupAddress is not NULL and *GroupAddress is\r
1085 not a multicast IPv4 address.\r
1086 @retval EFI_NOT_STARTED This instance has not been started.\r
1087 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1088 RARP, etc.) is not finished yet.\r
1089 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.\r
1090 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.\r
1091 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
1092 JoinFlag is TRUE).\r
1093 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).\r
1094 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
772db4bb 1095\r
1096**/\r
772db4bb 1097EFI_STATUS\r
1098EFIAPI\r
1099EfiIp4Groups (\r
d1050b9d
MK
1100 IN EFI_IP4_PROTOCOL *This,\r
1101 IN BOOLEAN JoinFlag,\r
1102 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL\r
772db4bb 1103 )\r
1104{\r
d1050b9d
MK
1105 IP4_PROTOCOL *IpInstance;\r
1106 EFI_STATUS Status;\r
1107 EFI_TPL OldTpl;\r
1108 IP4_ADDR McastIp;\r
772db4bb 1109\r
1110 if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) {\r
1111 return EFI_INVALID_PARAMETER;\r
1112 }\r
1113\r
1114 if (GroupAddress != NULL) {\r
e48e37fc 1115 CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR));\r
772db4bb 1116\r
1117 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {\r
1118 return EFI_INVALID_PARAMETER;\r
1119 }\r
1120 }\r
1121\r
1122 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1123 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1124\r
1125 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1126 Status = EFI_NOT_STARTED;\r
1127 goto ON_EXIT;\r
1128 }\r
1129\r
1130 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1131 Status = EFI_NO_MAPPING;\r
1132 goto ON_EXIT;\r
1133 }\r
1134\r
1135 Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress);\r
1136\r
1137ON_EXIT:\r
e48e37fc 1138 gBS->RestoreTPL (OldTpl);\r
772db4bb 1139 return Status;\r
1140}\r
1141\r
772db4bb 1142/**\r
5405e9a6 1143 Adds and deletes routing table entries.\r
1144\r
1145 The Routes() function adds a route to or deletes a route from the routing table.\r
e2851998 1146\r
5405e9a6 1147 Routes are determined by comparing the SubnetAddress with the destination IPv4\r
1148 address arithmetically AND-ed with the SubnetMask. The gateway address must be\r
1149 on the same subnet as the configured station address.\r
e2851998 1150\r
5405e9a6 1151 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
1152 The default route matches all destination IPv4 addresses that do not match any\r
1153 other routes.\r
e2851998 1154\r
5405e9a6 1155 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination\r
1156 IP address if it can be found in the ARP cache or on the local subnet. One automatic\r
1157 nonroute entry will be inserted into the routing table for outgoing packets that\r
1158 are addressed to a local subnet (gateway address of 0.0.0.0).\r
e2851998 1159\r
5405e9a6 1160 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI\r
1161 IPv4 Protocol instances that use the default IPv4 address will also have copies\r
1162 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these\r
1163 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its\r
1164 instances. As a result, client modification to the routing table will be lost.\r
1165\r
3e8c18da 1166 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1167 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to\r
1168 FALSE to add this route to the routing table. SubnetAddress\r
1169 and SubnetMask are used as the key to each route entry.\r
1170 @param[in] SubnetAddress The address of the subnet that needs to be routed.\r
1171 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
1172 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.\r
5405e9a6 1173\r
1174 @retval EFI_SUCCESS The operation completed successfully.\r
1175 @retval EFI_NOT_STARTED The driver instance has not been started.\r
1176 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1177 RARP, etc.) is not finished yet.\r
1178 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1179 - This is NULL.\r
1180 - SubnetAddress is NULL.\r
1181 - SubnetMask is NULL.\r
1182 - GatewayAddress is NULL.\r
1183 - *SubnetAddress is not a valid subnet address.\r
1184 - *SubnetMask is not a valid subnet mask.\r
1185 - *GatewayAddress is not a valid unicast IPv4 address.\r
1186 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
1187 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).\r
1188 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when\r
1189 DeleteRoute is FALSE).\r
e2851998 1190\r
772db4bb 1191**/\r
772db4bb 1192EFI_STATUS\r
1193EFIAPI\r
1194EfiIp4Routes (\r
d1050b9d
MK
1195 IN EFI_IP4_PROTOCOL *This,\r
1196 IN BOOLEAN DeleteRoute,\r
1197 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
1198 IN EFI_IPv4_ADDRESS *SubnetMask,\r
1199 IN EFI_IPv4_ADDRESS *GatewayAddress\r
772db4bb 1200 )\r
1201{\r
d1050b9d
MK
1202 IP4_PROTOCOL *IpInstance;\r
1203 IP4_INTERFACE *IpIf;\r
1204 IP4_ADDR Dest;\r
1205 IP4_ADDR Netmask;\r
1206 IP4_ADDR Nexthop;\r
1207 EFI_STATUS Status;\r
1208 EFI_TPL OldTpl;\r
772db4bb 1209\r
1210 //\r
1211 // First, validate the parameters\r
1212 //\r
1213 if ((This == NULL) || (SubnetAddress == NULL) ||\r
d1050b9d
MK
1214 (SubnetMask == NULL) || (GatewayAddress == NULL))\r
1215 {\r
772db4bb 1216 return EFI_INVALID_PARAMETER;\r
1217 }\r
1218\r
1219 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
e48e37fc 1220 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1221\r
1222 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1223 Status = EFI_NOT_STARTED;\r
1224 goto ON_EXIT;\r
1225 }\r
1226\r
1227 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1228 Status = EFI_NO_MAPPING;\r
1229 goto ON_EXIT;\r
1230 }\r
1231\r
e48e37fc 1232 CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR));\r
1233 CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR));\r
1234 CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1235\r
1236 Dest = NTOHL (Dest);\r
1237 Netmask = NTOHL (Netmask);\r
1238 Nexthop = NTOHL (Nexthop);\r
1239\r
d1050b9d 1240 IpIf = IpInstance->Interface;\r
772db4bb 1241\r
1242 if (!IP4_IS_VALID_NETMASK (Netmask)) {\r
1243 Status = EFI_INVALID_PARAMETER;\r
1244 goto ON_EXIT;\r
1245 }\r
1246\r
1247 //\r
1248 // the gateway address must be a unicast on the connected network if not zero.\r
1249 //\r
1250 if ((Nexthop != IP4_ALLZERO_ADDRESS) &&\r
d1050b9d
MK
1251 (((IpIf->SubnetMask != IP4_ALLONE_ADDRESS) && !IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask)) ||\r
1252 IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf))))\r
1253 {\r
772db4bb 1254 Status = EFI_INVALID_PARAMETER;\r
1255 goto ON_EXIT;\r
1256 }\r
1257\r
1258 if (DeleteRoute) {\r
1259 Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1260 } else {\r
1261 Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);\r
1262 }\r
1263\r
1264ON_EXIT:\r
e48e37fc 1265 gBS->RestoreTPL (OldTpl);\r
772db4bb 1266 return Status;\r
1267}\r
1268\r
772db4bb 1269/**\r
1270 Check whether the user's token or event has already\r
5405e9a6 1271 been enqueued on IP4's list.\r
772db4bb 1272\r
3e8c18da 1273 @param[in] Map The container of either user's transmit or receive\r
1274 token.\r
1f6729ff 1275 @param[in] Item Current item to check against.\r
6c585b52 1276 @param[in] Context The Token to check against.\r
772db4bb 1277\r
1f6729ff 1278 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP.\r
772db4bb 1279 @retval EFI_SUCCESS The current item isn't the same token/event as the\r
1280 context.\r
1281\r
1282**/\r
772db4bb 1283EFI_STATUS\r
e798cd87 1284EFIAPI\r
772db4bb 1285Ip4TokenExist (\r
d1050b9d
MK
1286 IN NET_MAP *Map,\r
1287 IN NET_MAP_ITEM *Item,\r
1288 IN VOID *Context\r
772db4bb 1289 )\r
1290{\r
1291 EFI_IP4_COMPLETION_TOKEN *Token;\r
1292 EFI_IP4_COMPLETION_TOKEN *TokenInItem;\r
1293\r
d1050b9d
MK
1294 Token = (EFI_IP4_COMPLETION_TOKEN *)Context;\r
1295 TokenInItem = (EFI_IP4_COMPLETION_TOKEN *)Item->Key;\r
772db4bb 1296\r
1297 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
1298 return EFI_ACCESS_DENIED;\r
1299 }\r
1300\r
1301 return EFI_SUCCESS;\r
1302}\r
1303\r
772db4bb 1304/**\r
1305 Validate the user's token against current station address.\r
1306\r
216f7970 1307 @param[in] Token User's token to validate.\r
1308 @param[in] IpIf The IP4 child's interface.\r
1309 @param[in] RawData Set to TRUE to send unformatted packets.\r
772db4bb 1310\r
216f7970 1311 @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
772db4bb 1312 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.\r
216f7970 1313 @retval EFI_SUCCESS The token is valid.\r
772db4bb 1314\r
1315**/\r
772db4bb 1316EFI_STATUS\r
1317Ip4TxTokenValid (\r
d1050b9d
MK
1318 IN EFI_IP4_COMPLETION_TOKEN *Token,\r
1319 IN IP4_INTERFACE *IpIf,\r
1320 IN BOOLEAN RawData\r
772db4bb 1321 )\r
1322{\r
d1050b9d
MK
1323 EFI_IP4_TRANSMIT_DATA *TxData;\r
1324 EFI_IP4_OVERRIDE_DATA *Override;\r
1325 IP4_ADDR Src;\r
1326 IP4_ADDR Gateway;\r
1327 UINT32 Offset;\r
1328 UINT32 Index;\r
1329 UINT32 HeadLen;\r
772db4bb 1330\r
1331 if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) {\r
1332 return EFI_INVALID_PARAMETER;\r
1333 }\r
1334\r
1335 TxData = Token->Packet.TxData;\r
1336\r
1337 //\r
216f7970 1338 // Check the fragment table: no empty fragment, and length isn't bogus.\r
772db4bb 1339 //\r
1340 if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {\r
1341 return EFI_INVALID_PARAMETER;\r
1342 }\r
1343\r
1344 Offset = TxData->TotalDataLength;\r
1345\r
216f7970 1346 if (Offset > IP4_MAX_PACKET_SIZE) {\r
1347 return EFI_BAD_BUFFER_SIZE;\r
1348 }\r
1349\r
772db4bb 1350 for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
1351 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
d1050b9d
MK
1352 (TxData->FragmentTable[Index].FragmentLength == 0))\r
1353 {\r
772db4bb 1354 return EFI_INVALID_PARAMETER;\r
1355 }\r
1356\r
1357 Offset -= TxData->FragmentTable[Index].FragmentLength;\r
1358 }\r
1359\r
1360 if (Offset != 0) {\r
1361 return EFI_INVALID_PARAMETER;\r
1362 }\r
1363\r
216f7970 1364 //\r
1365 // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData\r
1366 // is TRUE.\r
1367 //\r
1368 if (RawData) {\r
1369 return EFI_SUCCESS;\r
1370 }\r
1371\r
1372 //\r
1373 // Check the IP options: no more than 40 bytes and format is OK\r
1374 //\r
1375 if (TxData->OptionsLength != 0) {\r
1376 if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {\r
1377 return EFI_INVALID_PARAMETER;\r
1378 }\r
1379\r
1380 if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {\r
1381 return EFI_INVALID_PARAMETER;\r
1382 }\r
1383 }\r
1384\r
772db4bb 1385 //\r
1386 // Check the source and gateway: they must be a valid unicast.\r
1387 // Gateway must also be on the connected network.\r
1388 //\r
e2851998 1389 if (TxData->OverrideData != NULL) {\r
772db4bb 1390 Override = TxData->OverrideData;\r
1391\r
e48e37fc 1392 CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1393 CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
772db4bb 1394\r
1395 Src = NTOHL (Src);\r
1396 Gateway = NTOHL (Gateway);\r
1397\r
1398 if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) ||\r
1399 (Src == IP4_ALLONE_ADDRESS) ||\r
d1050b9d
MK
1400 IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf)))\r
1401 {\r
772db4bb 1402 return EFI_INVALID_PARAMETER;\r
1403 }\r
1404\r
1405 //\r
1406 // If gateway isn't zero, it must be a unicast address, and\r
1407 // on the connected network.\r
1408 //\r
1409 if ((Gateway != IP4_ALLZERO_ADDRESS) &&\r
1410 ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) ||\r
1411 !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) ||\r
d1050b9d
MK
1412 IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf))))\r
1413 {\r
772db4bb 1414 return EFI_INVALID_PARAMETER;\r
1415 }\r
1416 }\r
1417\r
1418 //\r
1419 // Check the packet length: Head length and packet length all has a limit\r
1420 //\r
1421 HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);\r
1422\r
1423 if ((HeadLen > IP4_MAX_HEADLEN) ||\r
d1050b9d
MK
1424 (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE))\r
1425 {\r
772db4bb 1426 return EFI_BAD_BUFFER_SIZE;\r
1427 }\r
1428\r
1429 return EFI_SUCCESS;\r
1430}\r
1431\r
772db4bb 1432/**\r
1433 The callback function for the net buffer which wraps the user's\r
1434 transmit token. Although it seems this function is pretty simple,\r
1435 there are some subtle things.\r
1436 When user requests the IP to transmit a packet by passing it a\r
1437 token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data\r
1438 is wrapped in an net buffer. the net buffer's Free function is\r
1439 set to Ip4FreeTxToken. The Token and token wrap are added to the\r
1440 IP child's TxToken map. Then the buffer is passed to Ip4Output for\r
1441 transmission. If something error happened before that, the buffer\r
1442 is freed, which in turn will free the token wrap. The wrap may\r
1443 have been added to the TxToken map or not, and the user's event\r
1444 shouldn't be fired because we are still in the EfiIp4Transmit. If\r
1445 the buffer has been sent by Ip4Output, it should be removed from\r
1446 the TxToken map and user's event signaled. The token wrap and buffer\r
1447 are bound together. Check the comments in Ip4Output for information\r
1448 about IP fragmentation.\r
1449\r
1f6729ff 1450 @param[in] Context The token's wrap.\r
772db4bb 1451\r
1452**/\r
772db4bb 1453VOID\r
e798cd87 1454EFIAPI\r
772db4bb 1455Ip4FreeTxToken (\r
d1050b9d 1456 IN VOID *Context\r
772db4bb 1457 )\r
1458{\r
d1050b9d
MK
1459 IP4_TXTOKEN_WRAP *Wrap;\r
1460 NET_MAP_ITEM *Item;\r
772db4bb 1461\r
d1050b9d 1462 Wrap = (IP4_TXTOKEN_WRAP *)Context;\r
772db4bb 1463\r
a1503a32 1464 //\r
1465 // Signal IpSecRecycleEvent to inform IPsec free the memory\r
1466 //\r
1467 if (Wrap->IpSecRecycleSignal != NULL) {\r
1468 gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
1469 }\r
1470\r
772db4bb 1471 //\r
1472 // Find the token in the instance's map. EfiIp4Transmit put the\r
1473 // token to the map. If that failed, NetMapFindKey will return NULL.\r
1474 //\r
1475 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);\r
1476\r
1477 if (Item != NULL) {\r
1478 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);\r
1479 }\r
1480\r
1481 if (Wrap->Sent) {\r
1482 gBS->SignalEvent (Wrap->Token->Event);\r
36ee91ca 1483\r
1484 //\r
1485 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
1486 //\r
d8d26fb2 1487 DispatchDpc ();\r
772db4bb 1488 }\r
1489\r
766c7483 1490 FreePool (Wrap);\r
772db4bb 1491}\r
1492\r
772db4bb 1493/**\r
1494 The callback function to Ip4Output to update the transmit status.\r
1495\r
1496 @param Ip4Instance The Ip4Instance that request the transmit.\r
1f6729ff 1497 @param Packet The user's transmit request.\r
1498 @param IoStatus The result of the transmission.\r
1499 @param Flag Not used during transmission.\r
772db4bb 1500 @param Context The token's wrap.\r
1501\r
772db4bb 1502**/\r
772db4bb 1503VOID\r
1504Ip4OnPacketSent (\r
d1050b9d
MK
1505 IP4_PROTOCOL *Ip4Instance,\r
1506 NET_BUF *Packet,\r
1507 EFI_STATUS IoStatus,\r
1508 UINT32 Flag,\r
1509 VOID *Context\r
772db4bb 1510 )\r
1511{\r
d1050b9d 1512 IP4_TXTOKEN_WRAP *Wrap;\r
772db4bb 1513\r
1514 //\r
1515 // This is the transmission request from upper layer,\r
1516 // not the IP4 driver itself.\r
1517 //\r
1518 ASSERT (Ip4Instance != NULL);\r
1519\r
1520 //\r
1521 // The first fragment of the packet has been sent. Update\r
1522 // the token's status. That is, if fragmented, the transmit's\r
1523 // status is the first fragment's status. The Wrap will be\r
1524 // release when all the fragments are release. Check the comments\r
1525 // in Ip4FreeTxToken and Ip4Output for information.\r
1526 //\r
d1050b9d 1527 Wrap = (IP4_TXTOKEN_WRAP *)Context;\r
772db4bb 1528 Wrap->Token->Status = IoStatus;\r
1529\r
1530 NetbufFree (Wrap->Packet);\r
1531}\r
1532\r
772db4bb 1533/**\r
5405e9a6 1534 Places outgoing data packets into the transmit queue.\r
1535\r
1536 The Transmit() function places a sending request in the transmit queue of this\r
1537 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some\r
1538 errors occur, the event in the token will be signaled and the status is updated.\r
1539\r
3e8c18da 1540 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1541 @param[in] Token Pointer to the transmit token.\r
5405e9a6 1542\r
1543 @retval EFI_SUCCESS The data has been queued for transmission.\r
1544 @retval EFI_NOT_STARTED This instance has not been started.\r
1545 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
1546 RARP, etc.) is not finished yet.\r
6c585b52 1547 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
5405e9a6 1548 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event\r
1549 was already in the transmit queue.\r
1550 @retval EFI_NOT_READY The completion token could not be queued because the transmit\r
e2851998 1551 queue is full.\r
5405e9a6 1552 @retval EFI_NOT_FOUND Not route is found to destination address.\r
1553 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
1554 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too\r
1555 short to transmit.\r
1556 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is\r
1557 greater than MTU (or greater than the maximum packet size if\r
1558 Token.Packet.TxData.OverrideData.\r
1f6729ff 1559 DoNotFragment is TRUE).\r
772db4bb 1560\r
1561**/\r
772db4bb 1562EFI_STATUS\r
1563EFIAPI\r
1564EfiIp4Transmit (\r
d1050b9d
MK
1565 IN EFI_IP4_PROTOCOL *This,\r
1566 IN EFI_IP4_COMPLETION_TOKEN *Token\r
772db4bb 1567 )\r
1568{\r
d1050b9d
MK
1569 IP4_SERVICE *IpSb;\r
1570 IP4_PROTOCOL *IpInstance;\r
1571 IP4_INTERFACE *IpIf;\r
1572 IP4_TXTOKEN_WRAP *Wrap;\r
1573 EFI_IP4_TRANSMIT_DATA *TxData;\r
1574 EFI_IP4_CONFIG_DATA *Config;\r
1575 EFI_IP4_OVERRIDE_DATA *Override;\r
1576 IP4_HEAD Head;\r
1577 IP4_ADDR GateWay;\r
1578 EFI_STATUS Status;\r
1579 EFI_TPL OldTpl;\r
1580 BOOLEAN DontFragment;\r
1581 UINT32 HeadLen;\r
1582 UINT8 RawHdrLen;\r
1583 UINT32 OptionsLength;\r
1584 UINT8 *OptionsBuffer;\r
1585 VOID *FirstFragment;\r
772db4bb 1586\r
1587 if (This == NULL) {\r
1588 return EFI_INVALID_PARAMETER;\r
1589 }\r
1590\r
1591 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1592\r
1593 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1594 return EFI_NOT_STARTED;\r
1595 }\r
1596\r
d1050b9d 1597 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1598\r
d1050b9d
MK
1599 IpSb = IpInstance->Service;\r
1600 IpIf = IpInstance->Interface;\r
1601 Config = &IpInstance->ConfigData;\r
772db4bb 1602\r
1603 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
1604 Status = EFI_NO_MAPPING;\r
1605 goto ON_EXIT;\r
1606 }\r
1607\r
1608 //\r
6c585b52 1609 // make sure that token is properly formatted\r
772db4bb 1610 //\r
216f7970 1611 Status = Ip4TxTokenValid (Token, IpIf, Config->RawData);\r
772db4bb 1612\r
1613 if (EFI_ERROR (Status)) {\r
1614 goto ON_EXIT;\r
1615 }\r
1616\r
1617 //\r
1618 // Check whether the token or signal already existed.\r
1619 //\r
1620 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) {\r
1621 Status = EFI_ACCESS_DENIED;\r
1622 goto ON_EXIT;\r
1623 }\r
1624\r
1625 //\r
1626 // Build the IP header, need to fill in the Tos, TotalLen, Id,\r
1627 // fragment, Ttl, protocol, Src, and Dst.\r
1628 //\r
1629 TxData = Token->Packet.TxData;\r
1630\r
216f7970 1631 FirstFragment = NULL;\r
772db4bb 1632\r
216f7970 1633 if (Config->RawData) {\r
1634 //\r
1635 // When RawData is TRUE, first buffer in FragmentTable points to a raw\r
1636 // IPv4 fragment including IPv4 header and options.\r
1637 //\r
1638 FirstFragment = TxData->FragmentTable[0].FragmentBuffer;\r
1639 CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8));\r
772db4bb 1640\r
d1050b9d 1641 RawHdrLen = (UINT8)(RawHdrLen & 0x0f);\r
216f7970 1642 if (RawHdrLen < 5) {\r
098e9f49
FS
1643 Status = EFI_INVALID_PARAMETER;\r
1644 goto ON_EXIT;\r
216f7970 1645 }\r
1646\r
d1050b9d 1647 RawHdrLen = (UINT8)(RawHdrLen << 2);\r
4a76d9b9 1648\r
216f7970 1649 CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN);\r
1650\r
1651 Ip4NtohHead (&Head);\r
1652 HeadLen = 0;\r
1653 DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment);\r
772db4bb 1654\r
216f7970 1655 if (!DontFragment) {\r
098e9f49
FS
1656 Status = EFI_INVALID_PARAMETER;\r
1657 goto ON_EXIT;\r
216f7970 1658 }\r
1659\r
1660 GateWay = IP4_ALLZERO_ADDRESS;\r
1661\r
1662 //\r
1663 // Get IPv4 options from first fragment.\r
1664 //\r
1665 if (RawHdrLen == IP4_MIN_HEADLEN) {\r
1666 OptionsLength = 0;\r
1667 OptionsBuffer = NULL;\r
1668 } else {\r
1669 OptionsLength = RawHdrLen - IP4_MIN_HEADLEN;\r
d1050b9d 1670 OptionsBuffer = (UINT8 *)FirstFragment + IP4_MIN_HEADLEN;\r
216f7970 1671 }\r
1672\r
1673 //\r
1674 // Trim off IPv4 header and options from first fragment.\r
1675 //\r
d1050b9d 1676 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment + RawHdrLen;\r
216f7970 1677 TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen;\r
772db4bb 1678 } else {\r
216f7970 1679 CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));\r
1680 Head.Dst = NTOHL (Head.Dst);\r
772db4bb 1681\r
216f7970 1682 if (TxData->OverrideData != NULL) {\r
1683 Override = TxData->OverrideData;\r
1684 Head.Protocol = Override->Protocol;\r
1685 Head.Tos = Override->TypeOfService;\r
1686 Head.Ttl = Override->TimeToLive;\r
1687 DontFragment = Override->DoNotFragment;\r
1688\r
1689 CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));\r
1690 CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));\r
1691\r
1692 Head.Src = NTOHL (Head.Src);\r
1693 GateWay = NTOHL (GateWay);\r
1694 } else {\r
1695 Head.Src = IpIf->Ip;\r
1696 GateWay = IP4_ALLZERO_ADDRESS;\r
1697 Head.Protocol = Config->DefaultProtocol;\r
1698 Head.Tos = Config->TypeOfService;\r
1699 Head.Ttl = Config->TimeToLive;\r
1700 DontFragment = Config->DoNotFragment;\r
1701 }\r
1702\r
1703 Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);\r
1704 HeadLen = (TxData->OptionsLength + 3) & (~0x03);\r
1705\r
1706 OptionsLength = TxData->OptionsLength;\r
d1050b9d 1707 OptionsBuffer = (UINT8 *)(TxData->OptionsBuffer);\r
216f7970 1708 }\r
772db4bb 1709\r
1710 //\r
1711 // If don't fragment and fragment needed, return error\r
1712 //\r
a1503a32 1713 if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {\r
772db4bb 1714 Status = EFI_BAD_BUFFER_SIZE;\r
1715 goto ON_EXIT;\r
1716 }\r
1717\r
1718 //\r
1719 // OK, it survives all the validation check. Wrap the token in\r
1720 // a IP4_TXTOKEN_WRAP and the data in a netbuf\r
1721 //\r
1722 Status = EFI_OUT_OF_RESOURCES;\r
f6b7393c 1723 Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));\r
772db4bb 1724 if (Wrap == NULL) {\r
1725 goto ON_EXIT;\r
1726 }\r
1727\r
d1050b9d
MK
1728 Wrap->IpInstance = IpInstance;\r
1729 Wrap->Token = Token;\r
1730 Wrap->Sent = FALSE;\r
1731 Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout);\r
1732 Wrap->Packet = NetbufFromExt (\r
1733 (NET_FRAGMENT *)TxData->FragmentTable,\r
1734 TxData->FragmentCount,\r
1735 IP4_MAX_HEADLEN,\r
1736 0,\r
1737 Ip4FreeTxToken,\r
1738 Wrap\r
1739 );\r
772db4bb 1740\r
1741 if (Wrap->Packet == NULL) {\r
766c7483 1742 FreePool (Wrap);\r
772db4bb 1743 goto ON_EXIT;\r
1744 }\r
1745\r
1746 Token->Status = EFI_NOT_READY;\r
1747\r
1748 if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) {\r
1749 //\r
1750 // NetbufFree will call Ip4FreeTxToken, which in turn will\r
1751 // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been\r
1752 // enqueued.\r
1753 //\r
216f7970 1754 if (Config->RawData) {\r
1755 //\r
1756 // Restore pointer of first fragment in RawData mode.\r
1757 //\r
d1050b9d 1758 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;\r
216f7970 1759 }\r
1760\r
772db4bb 1761 NetbufFree (Wrap->Packet);\r
1762 goto ON_EXIT;\r
1763 }\r
1764\r
36ee91ca 1765 //\r
1766 // Mark the packet sent before output it. Mark it not sent again if the\r
1767 // returned status is not EFI_SUCCESS;\r
1768 //\r
1769 Wrap->Sent = TRUE;\r
1770\r
772db4bb 1771 Status = Ip4Output (\r
1772 IpSb,\r
1773 IpInstance,\r
1774 Wrap->Packet,\r
1775 &Head,\r
216f7970 1776 OptionsBuffer,\r
1777 OptionsLength,\r
772db4bb 1778 GateWay,\r
1779 Ip4OnPacketSent,\r
1780 Wrap\r
1781 );\r
1782\r
1783 if (EFI_ERROR (Status)) {\r
36ee91ca 1784 Wrap->Sent = FALSE;\r
216f7970 1785\r
1786 if (Config->RawData) {\r
1787 //\r
1788 // Restore pointer of first fragment in RawData mode.\r
1789 //\r
d1050b9d 1790 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;\r
216f7970 1791 }\r
1792\r
772db4bb 1793 NetbufFree (Wrap->Packet);\r
772db4bb 1794 }\r
1795\r
216f7970 1796 if (Config->RawData) {\r
1797 //\r
1798 // Restore pointer of first fragment in RawData mode.\r
1799 //\r
d1050b9d 1800 TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;\r
216f7970 1801 }\r
1802\r
772db4bb 1803ON_EXIT:\r
e48e37fc 1804 gBS->RestoreTPL (OldTpl);\r
772db4bb 1805 return Status;\r
1806}\r
1807\r
772db4bb 1808/**\r
5405e9a6 1809 Places a receiving request into the receiving queue.\r
e2851998 1810\r
5405e9a6 1811 The Receive() function places a completion token into the receive packet queue.\r
1812 This function is always asynchronous.\r
e2851998 1813\r
5405e9a6 1814 The Token.Event field in the completion token must be filled in by the caller\r
1815 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol\r
1816 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
1817 is signaled.\r
1818\r
3e8c18da 1819 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
1820 @param[in] Token Pointer to a token that is associated with the receive data descriptor.\r
5405e9a6 1821\r
1822 @retval EFI_SUCCESS The receive completion token was cached.\r
1823 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
1824 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)\r
1825 is not finished yet.\r
1826 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
1827 - This is NULL.\r
1828 - Token is NULL.\r
1829 - Token.Event is NULL.\r
1830 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
1831 resources (usually memory).\r
1832 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
1833 The EFI IPv4 Protocol instance has been reset to startup defaults.\r
1834 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already\r
1835 in the receive queue.\r
1836 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
1837 @retval EFI_ICMP_ERROR An ICMP error packet was received.\r
772db4bb 1838\r
1839**/\r
772db4bb 1840EFI_STATUS\r
1841EFIAPI\r
1842EfiIp4Receive (\r
d1050b9d
MK
1843 IN EFI_IP4_PROTOCOL *This,\r
1844 IN EFI_IP4_COMPLETION_TOKEN *Token\r
772db4bb 1845 )\r
1846{\r
d1050b9d
MK
1847 IP4_PROTOCOL *IpInstance;\r
1848 EFI_STATUS Status;\r
1849 EFI_TPL OldTpl;\r
772db4bb 1850\r
1851 //\r
1852 // First validate the parameters\r
1853 //\r
1854 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
1855 return EFI_INVALID_PARAMETER;\r
1856 }\r
1857\r
1858 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
1859\r
e48e37fc 1860 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 1861\r
1862 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1863 Status = EFI_NOT_STARTED;\r
1864 goto ON_EXIT;\r
1865 }\r
1866\r
772db4bb 1867 //\r
1868 // Check whether the toke is already on the receive queue.\r
1869 //\r
1870 Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token);\r
1871\r
1872 if (EFI_ERROR (Status)) {\r
1873 Status = EFI_ACCESS_DENIED;\r
1874 goto ON_EXIT;\r
1875 }\r
1876\r
1877 //\r
1878 // Queue the token then check whether there is pending received packet.\r
1879 //\r
1880 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);\r
1881\r
1882 if (EFI_ERROR (Status)) {\r
1883 goto ON_EXIT;\r
1884 }\r
1885\r
1886 Status = Ip4InstanceDeliverPacket (IpInstance);\r
1887\r
36ee91ca 1888 //\r
1889 // Dispatch the DPC queued by the NotifyFunction of this instane's receive\r
1890 // event.\r
1891 //\r
d8d26fb2 1892 DispatchDpc ();\r
36ee91ca 1893\r
772db4bb 1894ON_EXIT:\r
e48e37fc 1895 gBS->RestoreTPL (OldTpl);\r
772db4bb 1896 return Status;\r
1897}\r
1898\r
772db4bb 1899/**\r
1900 Cancel the transmitted but not recycled packet. If a matching\r
1901 token is found, it will call Ip4CancelPacket to cancel the\r
1902 packet. Ip4CancelPacket will cancel all the fragments of the\r
1903 packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP\r
1904 will be deleted from the Map, and user's event signalled.\r
1905 Because Ip4CancelPacket and other functions are all called in\r
1906 line, so, after Ip4CancelPacket returns, the Item has been freed.\r
1907\r
1f6729ff 1908 @param[in] Map The IP4 child's transmit queue.\r
1909 @param[in] Item The current transmitted packet to test.\r
1910 @param[in] Context The user's token to cancel.\r
772db4bb 1911\r
1912 @retval EFI_SUCCESS Continue to check the next Item.\r
1913 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r
1914\r
1915**/\r
772db4bb 1916EFI_STATUS\r
e798cd87 1917EFIAPI\r
772db4bb 1918Ip4CancelTxTokens (\r
d1050b9d
MK
1919 IN NET_MAP *Map,\r
1920 IN NET_MAP_ITEM *Item,\r
1921 IN VOID *Context\r
772db4bb 1922 )\r
1923{\r
1924 EFI_IP4_COMPLETION_TOKEN *Token;\r
1925 IP4_TXTOKEN_WRAP *Wrap;\r
1926\r
d1050b9d 1927 Token = (EFI_IP4_COMPLETION_TOKEN *)Context;\r
772db4bb 1928\r
1929 //\r
1930 // Return EFI_SUCCESS to check the next item in the map if\r
1931 // this one doesn't match.\r
1932 //\r
1933 if ((Token != NULL) && (Token != Item->Key)) {\r
1934 return EFI_SUCCESS;\r
1935 }\r
1936\r
d1050b9d 1937 Wrap = (IP4_TXTOKEN_WRAP *)Item->Value;\r
772db4bb 1938 ASSERT (Wrap != NULL);\r
1939\r
1940 //\r
1941 // Don't access the Item, Wrap and Token's members after this point.\r
1942 // Item and wrap has been freed. And we no longer own the Token.\r
1943 //\r
1944 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
1945\r
1946 //\r
1947 // If only one item is to be cancel, return EFI_ABORTED to stop\r
1948 // iterating the map any more.\r
1949 //\r
1950 if (Token != NULL) {\r
1951 return EFI_ABORTED;\r
1952 }\r
1953\r
1954 return EFI_SUCCESS;\r
1955}\r
1956\r
772db4bb 1957/**\r
1958 Cancel the receive request. This is quiet simple, because\r
1959 it is only enqueued in our local receive map.\r
1960\r
1f6729ff 1961 @param[in] Map The IP4 child's receive queue.\r
1962 @param[in] Item Current receive request to cancel.\r
1963 @param[in] Context The user's token to cancel.\r
772db4bb 1964\r
1965 @retval EFI_SUCCESS Continue to check the next receive request on the\r
1966 queue.\r
1967 @retval EFI_ABORTED The user's token (token != NULL) has been\r
1968 cancelled.\r
1969\r
1970**/\r
772db4bb 1971EFI_STATUS\r
e798cd87 1972EFIAPI\r
772db4bb 1973Ip4CancelRxTokens (\r
d1050b9d
MK
1974 IN NET_MAP *Map,\r
1975 IN NET_MAP_ITEM *Item,\r
1976 IN VOID *Context\r
772db4bb 1977 )\r
1978{\r
1979 EFI_IP4_COMPLETION_TOKEN *Token;\r
1980 EFI_IP4_COMPLETION_TOKEN *This;\r
1981\r
d1050b9d 1982 Token = (EFI_IP4_COMPLETION_TOKEN *)Context;\r
772db4bb 1983 This = Item->Key;\r
1984\r
1985 if ((Token != NULL) && (Token != This)) {\r
1986 return EFI_SUCCESS;\r
1987 }\r
1988\r
1989 NetMapRemoveItem (Map, Item, NULL);\r
1990\r
1991 This->Status = EFI_ABORTED;\r
1992 This->Packet.RxData = NULL;\r
1993 gBS->SignalEvent (This->Event);\r
1994\r
1995 if (Token != NULL) {\r
1996 return EFI_ABORTED;\r
1997 }\r
1998\r
1999 return EFI_SUCCESS;\r
2000}\r
2001\r
772db4bb 2002/**\r
2003 Cancel the user's receive/transmit request.\r
2004\r
1f6729ff 2005 @param[in] IpInstance The IP4 child.\r
3e8c18da 2006 @param[in] Token The token to cancel. If NULL, all token will be\r
772db4bb 2007 cancelled.\r
2008\r
1f6729ff 2009 @retval EFI_SUCCESS The token is cancelled.\r
772db4bb 2010 @retval EFI_NOT_FOUND The token isn't found on either the\r
1f6729ff 2011 transmit/receive queue.\r
772db4bb 2012 @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.\r
2013\r
2014**/\r
2015EFI_STATUS\r
2016Ip4Cancel (\r
d1050b9d
MK
2017 IN IP4_PROTOCOL *IpInstance,\r
2018 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
772db4bb 2019 )\r
2020{\r
d1050b9d 2021 EFI_STATUS Status;\r
772db4bb 2022\r
2023 //\r
2024 // First check the transmitted packet. Ip4CancelTxTokens returns\r
2025 // EFI_ABORTED to mean that the token has been cancelled when\r
2026 // token != NULL. So, return EFI_SUCCESS for this condition.\r
2027 //\r
2028 Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token);\r
2029\r
2030 if (EFI_ERROR (Status)) {\r
2031 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2032 return EFI_SUCCESS;\r
2033 }\r
2034\r
2035 return Status;\r
2036 }\r
2037\r
2038 //\r
2039 // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT\r
2040 // for Token!=NULL and it is cancelled.\r
2041 //\r
2042 Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token);\r
36ee91ca 2043 //\r
2044 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
2045 // events.\r
2046 //\r
d8d26fb2 2047 DispatchDpc ();\r
772db4bb 2048 if (EFI_ERROR (Status)) {\r
2049 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
2050 return EFI_SUCCESS;\r
2051 }\r
2052\r
2053 return Status;\r
2054 }\r
2055\r
2056 //\r
2057 // OK, if the Token is found when Token != NULL, the NetMapIterate\r
2058 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.\r
2059 //\r
2060 if (Token != NULL) {\r
2061 return EFI_NOT_FOUND;\r
2062 }\r
2063\r
2064 //\r
2065 // If Token == NULL, cancel all the tokens. return error if no\r
2066 // all of them are cancelled.\r
2067 //\r
2068 if (!NetMapIsEmpty (&IpInstance->TxTokens) ||\r
d1050b9d
MK
2069 !NetMapIsEmpty (&IpInstance->RxTokens))\r
2070 {\r
772db4bb 2071 return EFI_DEVICE_ERROR;\r
2072 }\r
2073\r
2074 return EFI_SUCCESS;\r
2075}\r
2076\r
772db4bb 2077/**\r
5405e9a6 2078 Abort an asynchronous transmit or receive request.\r
e2851998 2079\r
5405e9a6 2080 The Cancel() function is used to abort a pending transmit or receive request.\r
2081 If the token is in the transmit or receive request queues, after calling this\r
2082 function, Token->Status will be set to EFI_ABORTED and then Token->Event will\r
2083 be signaled. If the token is not in one of the queues, which usually means the\r
2084 asynchronous operation has completed, this function will not signal the token\r
2085 and EFI_NOT_FOUND is returned.\r
2086\r
3e8c18da 2087 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
2088 @param[in] Token Pointer to a token that has been issued by\r
2089 EFI_IP4_PROTOCOL.Transmit() or\r
2090 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending\r
2091 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is\r
2092 defined in EFI_IP4_PROTOCOL.Transmit().\r
5405e9a6 2093\r
2094 @retval EFI_SUCCESS The asynchronous I/O request was aborted and\r
2095 Token.->Event was signaled. When Token is NULL, all\r
2096 pending requests were aborted and their events were signaled.\r
2097 @retval EFI_INVALID_PARAMETER This is NULL.\r
2098 @retval EFI_NOT_STARTED This instance has not been started.\r
2099 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2100 RARP, etc.) is not finished yet.\r
2101 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
2102 not found in the transmit or receive queue. It has either completed\r
2103 or was not issued by Transmit() and Receive().\r
772db4bb 2104\r
2105**/\r
772db4bb 2106EFI_STATUS\r
2107EFIAPI\r
2108EfiIp4Cancel (\r
d1050b9d
MK
2109 IN EFI_IP4_PROTOCOL *This,\r
2110 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL\r
772db4bb 2111 )\r
2112{\r
d1050b9d
MK
2113 IP4_PROTOCOL *IpInstance;\r
2114 EFI_STATUS Status;\r
2115 EFI_TPL OldTpl;\r
772db4bb 2116\r
2117 if (This == NULL) {\r
2118 return EFI_INVALID_PARAMETER;\r
2119 }\r
2120\r
2121 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2122\r
e48e37fc 2123 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 2124\r
2125 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
2126 Status = EFI_NOT_STARTED;\r
2127 goto ON_EXIT;\r
2128 }\r
2129\r
2130 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {\r
2131 Status = EFI_NO_MAPPING;\r
2132 goto ON_EXIT;\r
2133 }\r
2134\r
2135 Status = Ip4Cancel (IpInstance, Token);\r
2136\r
2137ON_EXIT:\r
e48e37fc 2138 gBS->RestoreTPL (OldTpl);\r
772db4bb 2139 return Status;\r
2140}\r
2141\r
772db4bb 2142/**\r
5405e9a6 2143 Polls for incoming data packets and processes outgoing data packets.\r
e2851998 2144\r
5405e9a6 2145 The Poll() function polls for incoming data packets and processes outgoing data\r
2146 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()\r
2147 function to increase the rate that data packets are moved between the communications\r
2148 device and the transmit and receive queues.\r
e2851998 2149\r
5405e9a6 2150 In some systems the periodic timer event may not poll the underlying communications\r
2151 device fast enough to transmit and/or receive all data packets without missing\r
2152 incoming packets or dropping outgoing packets. Drivers and applications that are\r
2153 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function\r
2154 more often.\r
2155\r
3e8c18da 2156 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.\r
5405e9a6 2157\r
2158 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
2159 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.\r
2160 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
2161 RARP, etc.) is not finished yet.\r
2162 @retval EFI_INVALID_PARAMETER This is NULL.\r
2163 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
2164 @retval EFI_NOT_READY No incoming or outgoing data is processed.\r
2165 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
2166 Consider increasing the polling rate.\r
772db4bb 2167\r
2168**/\r
772db4bb 2169EFI_STATUS\r
2170EFIAPI\r
2171EfiIp4Poll (\r
d1050b9d 2172 IN EFI_IP4_PROTOCOL *This\r
772db4bb 2173 )\r
2174{\r
2175 IP4_PROTOCOL *IpInstance;\r
2176 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
2177\r
2178 if (This == NULL) {\r
2179 return EFI_INVALID_PARAMETER;\r
2180 }\r
2181\r
2182 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);\r
2183\r
2184 if (IpInstance->State == IP4_STATE_UNCONFIGED) {\r
2185 return EFI_NOT_STARTED;\r
2186 }\r
2187\r
2188 Mnp = IpInstance->Service->Mnp;\r
2189\r
2190 //\r
2191 // Don't lock the Poll function to enable the deliver of\r
2192 // the packet polled up.\r
2193 //\r
2194 return Mnp->Poll (Mnp);\r
2195}\r
2196\r
772db4bb 2197/**\r
2198 Decrease the life of the transmitted packets. If it is\r
2199 decreased to zero, cancel the packet. This function is\r
96e1079f 2200 called by Ip4PacketTimerTicking which time out both the\r
772db4bb 2201 received-but-not-delivered and transmitted-but-not-recycle\r
2202 packets.\r
2203\r
3e8c18da 2204 @param[in] Map The IP4 child's transmit map.\r
1f6729ff 2205 @param[in] Item Current transmitted packet.\r
3e8c18da 2206 @param[in] Context Not used.\r
772db4bb 2207\r
1f6729ff 2208 @retval EFI_SUCCESS Always returns EFI_SUCCESS.\r
772db4bb 2209\r
2210**/\r
2211EFI_STATUS\r
e798cd87 2212EFIAPI\r
772db4bb 2213Ip4SentPacketTicking (\r
d1050b9d
MK
2214 IN NET_MAP *Map,\r
2215 IN NET_MAP_ITEM *Item,\r
2216 IN VOID *Context\r
772db4bb 2217 )\r
2218{\r
d1050b9d 2219 IP4_TXTOKEN_WRAP *Wrap;\r
772db4bb 2220\r
d1050b9d 2221 Wrap = (IP4_TXTOKEN_WRAP *)Item->Value;\r
772db4bb 2222 ASSERT (Wrap != NULL);\r
2223\r
2224 if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {\r
2225 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);\r
2226 }\r
2227\r
2228 return EFI_SUCCESS;\r
2229}\r
2230\r
772db4bb 2231/**\r
4a76d9b9
SZ
2232 This heart beat timer of IP4 service instance times out all of its IP4 children's\r
2233 received-but-not-delivered and transmitted-but-not-recycle packets, and provides\r
018432f0 2234 time input for its IGMP protocol.\r
772db4bb 2235\r
3e8c18da 2236 @param[in] Event The IP4 service instance's heart beat timer.\r
2237 @param[in] Context The IP4 service instance.\r
772db4bb 2238\r
2239**/\r
2240VOID\r
2241EFIAPI\r
2242Ip4TimerTicking (\r
d1050b9d
MK
2243 IN EFI_EVENT Event,\r
2244 IN VOID *Context\r
772db4bb 2245 )\r
2246{\r
d1050b9d 2247 IP4_SERVICE *IpSb;\r
018432f0 2248\r
d1050b9d 2249 IpSb = (IP4_SERVICE *)Context;\r
018432f0 2250 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
4a76d9b9 2251\r
018432f0
WF
2252 Ip4PacketTimerTicking (IpSb);\r
2253 Ip4IgmpTicking (IpSb);\r
2254}\r
2255\r
2256/**\r
4a76d9b9
SZ
2257 This dedicated timer is used to poll underlying network media status. In case\r
2258 of cable swap or wireless network switch, a new round auto configuration will\r
2259 be initiated. The timer will signal the IP4 to run DHCP configuration again.\r
2260 IP4 driver will free old IP address related resource, such as route table and\r
2261 Interface, then initiate a DHCP process to acquire new IP, eventually create\r
018432f0
WF
2262 route table for new IP address.\r
2263\r
2264 @param[in] Event The IP4 service instance's heart beat timer.\r
2265 @param[in] Context The IP4 service instance.\r
2266\r
2267**/\r
2268VOID\r
2269EFIAPI\r
2270Ip4TimerReconfigChecking (\r
d1050b9d
MK
2271 IN EFI_EVENT Event,\r
2272 IN VOID *Context\r
018432f0
WF
2273 )\r
2274{\r
d1050b9d
MK
2275 IP4_SERVICE *IpSb;\r
2276 BOOLEAN OldMediaPresent;\r
2277 EFI_STATUS Status;\r
2278 EFI_SIMPLE_NETWORK_MODE SnpModeData;\r
772db4bb 2279\r
d1050b9d 2280 IpSb = (IP4_SERVICE *)Context;\r
772db4bb 2281 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);\r
4a76d9b9 2282\r
2c320007 2283 OldMediaPresent = IpSb->MediaPresent;\r
772db4bb 2284\r
2c320007 2285 //\r
4a76d9b9
SZ
2286 // Get fresh mode data from MNP, since underlying media status may change.\r
2287 // Here, it needs to mention that the MediaPresent can also be checked even if\r
2c320007
JW
2288 // EFI_NOT_STARTED returned while this MNP child driver instance isn't configured.\r
2289 //\r
2290 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &SnpModeData);\r
2291 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
2292 return;\r
2293 }\r
2294\r
2295 IpSb->MediaPresent = SnpModeData.MediaPresent;\r
2296 //\r
2297 // Media transimit Unpresent to Present means new link movement is detected.\r
2298 //\r
7648748e 2299 if (!OldMediaPresent && IpSb->MediaPresent && (IpSb->Ip4Config2Instance.Policy == Ip4Config2PolicyDhcp)) {\r
2c320007
JW
2300 //\r
2301 // Signal the IP4 to run the dhcp configuration again. IP4 driver will free\r
4a76d9b9
SZ
2302 // old IP address related resource, such as route table and Interface, then\r
2303 // initiate a DHCP round to acquire new IP, eventually\r
2c320007
JW
2304 // create route table for new IP address.\r
2305 //\r
2306 if (IpSb->ReconfigEvent != NULL) {\r
2307 Status = gBS->SignalEvent (IpSb->ReconfigEvent);\r
2308 DispatchDpc ();\r
2309 }\r
2310 }\r
772db4bb 2311}\r