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