3 Copyright (c) 2005 - 2007, Intel Corporation
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
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.
26 Get the IP child's current operational data. This can
27 all be used to get the underlying MNP and SNP data.
29 @param This The IP4 protocol instance
30 @param Ip4ModeData The IP4 operation data
31 @param MnpConfigData The MNP configure data
32 @param SnpModeData The SNP operation data
34 @retval EFI_INVALID_PARAMETER The parameter is invalid because This == NULL
35 @retval EFI_SUCCESS The operational parameter is returned.
36 @retval Others Failed to retrieve the IP4 route table.
43 IN CONST EFI_IP4_PROTOCOL
*This
,
44 OUT EFI_IP4_MODE_DATA
*Ip4ModeData
, OPTIONAL
45 OUT EFI_MANAGED_NETWORK_CONFIG_DATA
*MnpConfigData
, OPTIONAL
46 OUT EFI_SIMPLE_NETWORK_MODE
*SnpModeData OPTIONAL
49 IP4_PROTOCOL
*IpInstance
;
51 EFI_IP4_CONFIG_DATA
*Config
;
57 return EFI_INVALID_PARAMETER
;
60 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
61 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
62 IpSb
= IpInstance
->Service
;
64 if (Ip4ModeData
!= NULL
) {
66 // IsStarted is "whether the EfiIp4Configure has been called".
67 // IsConfigured is "whether the station address has been configured"
69 Ip4ModeData
->IsStarted
= (BOOLEAN
)(IpInstance
->State
== IP4_STATE_CONFIGED
);
70 CopyMem (&Ip4ModeData
->ConfigData
, &IpInstance
->ConfigData
, sizeof (Ip4ModeData
->ConfigData
));
71 Ip4ModeData
->IsConfigured
= FALSE
;
73 Ip4ModeData
->GroupCount
= IpInstance
->GroupCount
;
74 Ip4ModeData
->GroupTable
= (EFI_IPv4_ADDRESS
*) IpInstance
->Groups
;
76 Ip4ModeData
->IcmpTypeCount
= 23;
77 Ip4ModeData
->IcmpTypeList
= mIp4SupportedIcmp
;
79 Ip4ModeData
->RouteTable
= NULL
;
80 Ip4ModeData
->RouteCount
= 0;
83 // return the current station address for this IP child. So,
84 // the user can get the default address through this. Some
85 // application wants to know it station address even it is
86 // using the default one, such as a ftp server.
88 if (Ip4ModeData
->IsStarted
) {
89 Config
= &Ip4ModeData
->ConfigData
;
91 Ip
= HTONL (IpInstance
->Interface
->Ip
);
92 NetCopyMem (&Config
->StationAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
94 Ip
= HTONL (IpInstance
->Interface
->SubnetMask
);
95 NetCopyMem (&Config
->SubnetMask
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
97 Ip4ModeData
->IsConfigured
= IpInstance
->Interface
->Configured
;
100 // Build a EFI route table for user from the internal route table.
102 Status
= Ip4BuildEfiRouteTable (IpInstance
);
104 if (EFI_ERROR (Status
)) {
105 NET_RESTORE_TPL (OldTpl
);
109 Ip4ModeData
->RouteTable
= IpInstance
->EfiRouteTable
;
110 Ip4ModeData
->RouteCount
= IpInstance
->EfiRouteCount
;
114 if (MnpConfigData
!= NULL
) {
115 CopyMem (MnpConfigData
, &IpSb
->MnpConfigData
, sizeof (*MnpConfigData
));
118 if (SnpModeData
!= NULL
) {
119 CopyMem (SnpModeData
, &IpSb
->SnpMode
, sizeof (*SnpModeData
));
122 NET_RESTORE_TPL (OldTpl
);
128 Config the MNP parameter used by IP. The IP driver use one MNP
129 child to transmit/receive frames. By default, it configures MNP
130 to receive unicast/multicast/broadcast. And it will enable/disable
131 the promiscous receive according to whether there is IP child
132 enable that or not. If Force isn't false, it will iterate through
133 all the IP children to check whether the promiscuous receive
134 setting has been changed. If it hasn't been changed, it won't
135 reconfigure the MNP. If Force is true, the MNP is configured no
136 matter whether that is changed or not.
138 @param IpSb The IP4 service instance that is to be changed.
139 @param Force Force the configuration or not.
141 @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.
142 @retval Others Configuration failed.
146 Ip4ServiceConfigMnp (
147 IN IP4_SERVICE
*IpSb
,
151 NET_LIST_ENTRY
*Entry
;
152 NET_LIST_ENTRY
*ProtoEntry
;
154 IP4_PROTOCOL
*IpInstance
;
156 BOOLEAN PromiscReceive
;
160 PromiscReceive
= FALSE
;
164 // Iterate through the IP children to check whether promiscuous
165 // receive setting has been changed. Update the interface's receive
168 NET_LIST_FOR_EACH (Entry
, &IpSb
->Interfaces
) {
170 IpIf
= NET_LIST_USER_STRUCT (Entry
, IP4_INTERFACE
, Link
);
171 IpIf
->PromiscRecv
= FALSE
;
173 NET_LIST_FOR_EACH (ProtoEntry
, &IpIf
->IpInstances
) {
174 IpInstance
= NET_LIST_USER_STRUCT (ProtoEntry
, IP4_PROTOCOL
, AddrLink
);
176 if (IpInstance
->ConfigData
.AcceptPromiscuous
) {
177 IpIf
->PromiscRecv
= TRUE
;
178 PromiscReceive
= TRUE
;
184 // If promiscuous receive isn't changed, it isn't necessary to reconfigure.
186 if (PromiscReceive
== IpSb
->MnpConfigData
.EnablePromiscuousReceive
) {
191 IpSb
->MnpConfigData
.EnablePromiscuousReceive
= PromiscReceive
;
194 Status
= IpSb
->Mnp
->Configure (IpSb
->Mnp
, &IpSb
->MnpConfigData
);
197 // recover the original configuration if failed to set the configure.
199 if (EFI_ERROR (Status
) && Reconfig
) {
200 IpSb
->MnpConfigData
.EnablePromiscuousReceive
= (BOOLEAN
) !PromiscReceive
;
208 The event handle for IP4 auto configuration. If IP is asked
209 to reconfigure the default address. The original default
210 interface and route table are removed as the default. If there
211 is active IP children using the default address, the interface
212 will remain valid until all the children have freed their
213 references. If IP is signalled when auto configuration is done,
214 it will configure the default interface and default route table
215 with the configuration information retrieved by IP4_CONFIGURE.
217 @param Context The IP4 service binding instance.
224 Ip4AutoConfigCallBackDpc (
228 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
229 EFI_IP4_IPCONFIG_DATA
*Data
;
230 EFI_IP4_ROUTE_TABLE
*RouteEntry
;
232 IP4_ROUTE_TABLE
*RouteTable
;
238 IpSb
= (IP4_SERVICE
*) Context
;
239 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
241 Ip4Config
= IpSb
->Ip4Config
;
244 // IP is asked to do the reconfiguration. If the default interface
245 // has been configured, release the default interface and route
246 // table, then create a new one. If there are some IP children
247 // using it, the interface won't be physically freed until all the
248 // children have released their reference to it. Also remember to
249 // restart the receive on the default address. IP4 driver only receive
250 // frames on the default address, and when the default interface is
251 // freed, Ip4AcceptFrame won't be informed.
253 if (IpSb
->ActiveEvent
== IpSb
->ReconfigEvent
) {
255 if (IpSb
->DefaultInterface
->Configured
) {
256 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
262 RouteTable
= Ip4CreateRouteTable ();
264 if (RouteTable
== NULL
) {
265 Ip4FreeInterface (IpIf
, NULL
);
269 Ip4CancelReceive (IpSb
->DefaultInterface
);
270 Ip4FreeInterface (IpSb
->DefaultInterface
, NULL
);
271 Ip4FreeRouteTable (IpSb
->DefaultRouteTable
);
273 IpSb
->DefaultInterface
= IpIf
;
274 NetListInsertHead (&IpSb
->Interfaces
, &IpIf
->Link
);
276 IpSb
->DefaultRouteTable
= RouteTable
;
277 Ip4ReceiveFrame (IpIf
, NULL
, Ip4AccpetFrame
, IpSb
);
280 Ip4Config
->Stop (Ip4Config
);
281 Ip4Config
->Start (Ip4Config
, IpSb
->DoneEvent
, IpSb
->ReconfigEvent
);
286 // Get the configure data in two steps: get the length then the data.
290 if (Ip4Config
->GetData (Ip4Config
, &Len
, NULL
) != EFI_BUFFER_TOO_SMALL
) {
294 Data
= NetAllocatePool (Len
);
300 Status
= Ip4Config
->GetData (Ip4Config
, &Len
, Data
);
302 if (EFI_ERROR (Status
)) {
306 IpIf
= IpSb
->DefaultInterface
;
309 // If the default address has been configured don't change it.
310 // This is unlikely to happen if EFI_IP4_CONFIG protocol has
311 // informed us to reconfigure each time it wants to change the
312 // configuration parameters.
314 if (IpIf
->Configured
) {
319 // Set the default interface's address, then add a directed
320 // route for it, that is, the route whose nexthop is zero.
322 Status
= Ip4SetAddress (
324 EFI_NTOHL (Data
->StationAddress
),
325 EFI_NTOHL (Data
->SubnetMask
)
328 if (EFI_ERROR (Status
)) {
333 IpSb
->DefaultRouteTable
,
334 EFI_NTOHL (Data
->StationAddress
),
335 EFI_NTOHL (Data
->SubnetMask
),
340 // Add routes returned by EFI_IP4_CONFIG protocol.
342 for (Index
= 0; Index
< Data
->RouteTableSize
; Index
++) {
343 RouteEntry
= &Data
->RouteTable
[Index
];
346 IpSb
->DefaultRouteTable
,
347 EFI_NTOHL (RouteEntry
->SubnetAddress
),
348 EFI_NTOHL (RouteEntry
->SubnetMask
),
349 EFI_NTOHL (RouteEntry
->GatewayAddress
)
353 IpSb
->State
= IP4_SERVICE_CONFIGED
;
355 Ip4SetVariableData (IpSb
);
363 Ip4AutoConfigCallBack (
371 Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK
375 Event - The event that is signalled.
376 Context - The IP4 service binding instance.
386 IpSb
= (IP4_SERVICE
*) Context
;
387 IpSb
->ActiveEvent
= Event
;
390 // Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK
392 NetLibQueueDpc (TPL_CALLBACK
, Ip4AutoConfigCallBackDpc
, Context
);
397 Start the auto configuration for this IP service instance.
398 It will locates the EFI_IP4_CONFIG_PROTOCOL, then start the
401 @param IpSb The IP4 service instance to configure
403 @retval EFI_SUCCESS The auto configuration is successfull started
404 @retval Others Failed to start auto configuration.
412 EFI_IP4_CONFIG_PROTOCOL
*Ip4Config
;
415 if (IpSb
->State
> IP4_SERVICE_UNSTARTED
) {
420 // Create the DoneEvent and ReconfigEvent to call EFI_IP4_CONFIG
422 Status
= gBS
->CreateEvent (
425 Ip4AutoConfigCallBack
,
430 if (EFI_ERROR (Status
)) {
434 Status
= gBS
->CreateEvent (
437 Ip4AutoConfigCallBack
,
442 if (EFI_ERROR (Status
)) {
443 goto CLOSE_DONE_EVENT
;
447 // Open the EFI_IP4_CONFIG protocol then start auto configure
449 Status
= gBS
->OpenProtocol (
451 &gEfiIp4ConfigProtocolGuid
,
452 (VOID
**) &Ip4Config
,
455 EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
458 if (EFI_ERROR (Status
)) {
459 Status
= EFI_UNSUPPORTED
;
460 goto CLOSE_RECONFIG_EVENT
;
463 Status
= Ip4Config
->Start (Ip4Config
, IpSb
->DoneEvent
, IpSb
->ReconfigEvent
);
465 if (EFI_ERROR (Status
)) {
468 &gEfiIp4ConfigProtocolGuid
,
473 goto CLOSE_RECONFIG_EVENT
;
476 IpSb
->Ip4Config
= Ip4Config
;
477 IpSb
->State
= IP4_SERVICE_STARTED
;
480 CLOSE_RECONFIG_EVENT
:
481 gBS
->CloseEvent (IpSb
->ReconfigEvent
);
482 IpSb
->ReconfigEvent
= NULL
;
485 gBS
->CloseEvent (IpSb
->DoneEvent
);
486 IpSb
->DoneEvent
= NULL
;
493 Intiialize the IP4_PROTOCOL structure to the unconfigured states.
495 @param IpSb The IP4 service instance.
496 @param IpInstance The IP4 child instance.
503 IN IP4_SERVICE
*IpSb
,
504 IN IP4_PROTOCOL
*IpInstance
507 ASSERT ((IpSb
!= NULL
) && (IpInstance
!= NULL
));
509 NetZeroMem (IpInstance
, sizeof (IP4_PROTOCOL
));
511 IpInstance
->Signature
= IP4_PROTOCOL_SIGNATURE
;
512 CopyMem (&IpInstance
->Ip4Proto
, &mEfiIp4ProtocolTemplete
, sizeof (IpInstance
->Ip4Proto
));
513 IpInstance
->State
= IP4_STATE_UNCONFIGED
;
514 IpInstance
->Service
= IpSb
;
516 NetListInit (&IpInstance
->Link
);
517 NetMapInit (&IpInstance
->RxTokens
);
518 NetMapInit (&IpInstance
->TxTokens
);
519 NetListInit (&IpInstance
->Received
);
520 NetListInit (&IpInstance
->Delivered
);
521 NetListInit (&IpInstance
->AddrLink
);
523 NET_RECYCLE_LOCK_INIT (&IpInstance
->RecycleLock
);
528 Configure the IP4 child. If the child is already configured,
529 change the configuration parameter. Otherwise configure it
530 for the first time. The caller should validate the configuration
531 before deliver them to it. It also don't do configure NULL.
533 @param IpInstance The IP4 child to configure.
534 @param Config The configure data.
536 @retval EFI_SUCCESS The IP4 child is successfully configured.
537 @retval EFI_DEVICE_ERROR Failed to free the pending transive or to
538 configure underlying MNP or other errors.
539 @retval EFI_NO_MAPPING The IP4 child is configured to use default
540 address, but the default address hasn't been
541 configured. The IP4 child doesn't need to be
542 reconfigured when default address is configured.
547 IN IP4_PROTOCOL
*IpInstance
,
548 IN EFI_IP4_CONFIG_DATA
*Config
557 IpSb
= IpInstance
->Service
;
560 // User is changing packet filters. It must be stopped
561 // before the station address can be changed.
563 if (IpInstance
->State
== IP4_STATE_CONFIGED
) {
565 // Cancel all the pending transmit/receive from upper layer
567 Status
= Ip4Cancel (IpInstance
, NULL
);
569 if (EFI_ERROR (Status
)) {
570 return EFI_DEVICE_ERROR
;
573 CopyMem (&IpInstance
->ConfigData
, Config
, sizeof (IpInstance
->ConfigData
));
578 // Configure a fresh IP4 protocol instance. Create a route table.
579 // Each IP child has its own route table, which may point to the
580 // default table if it is using default address.
582 Status
= EFI_OUT_OF_RESOURCES
;
583 IpInstance
->RouteTable
= Ip4CreateRouteTable ();
585 if (IpInstance
->RouteTable
== NULL
) {
590 // Set up the interface.
592 NetCopyMem (&Ip
, &Config
->StationAddress
, sizeof (IP4_ADDR
));
593 NetCopyMem (&Netmask
, &Config
->SubnetMask
, sizeof (IP4_ADDR
));
596 Netmask
= NTOHL (Netmask
);
598 if (!Config
->UseDefaultAddress
) {
600 // Find whether there is already an interface with the same
601 // station address. All the instances with the same station
602 // address shares one interface.
604 IpIf
= Ip4FindStationAddress (IpSb
, Ip
, Netmask
);
610 IpIf
= Ip4CreateInterface (IpSb
->Mnp
, IpSb
->Controller
, IpSb
->Image
);
616 Status
= Ip4SetAddress (IpIf
, Ip
, Netmask
);
618 if (EFI_ERROR (Status
)) {
619 Status
= EFI_DEVICE_ERROR
;
620 Ip4FreeInterface (IpIf
, IpInstance
);
624 NetListInsertTail (&IpSb
->Interfaces
, &IpIf
->Link
);
628 // Add a route to this connected network in the route table
630 Ip4AddRoute (IpInstance
->RouteTable
, Ip
, Netmask
, IP4_ALLZERO_ADDRESS
);
634 // Use the default address. If the default configuration hasn't
635 // been started, start it.
637 if (IpSb
->State
== IP4_SERVICE_UNSTARTED
) {
638 Status
= Ip4StartAutoConfig (IpSb
);
640 if (EFI_ERROR (Status
)) {
645 IpIf
= IpSb
->DefaultInterface
;
646 NET_GET_REF (IpSb
->DefaultInterface
);
649 // If default address is used, so is the default route table.
650 // Any route set by the instance has the precedence over the
651 // routes in the default route table. Link the default table
652 // after the instance's table. Routing will search the local
655 NET_GET_REF (IpSb
->DefaultRouteTable
);
656 IpInstance
->RouteTable
->Next
= IpSb
->DefaultRouteTable
;
659 IpInstance
->Interface
= IpIf
;
660 NetListInsertTail (&IpIf
->IpInstances
, &IpInstance
->AddrLink
);
662 CopyMem (&IpInstance
->ConfigData
, Config
, sizeof (IpInstance
->ConfigData
));
663 IpInstance
->State
= IP4_STATE_CONFIGED
;
666 // Although EFI_NO_MAPPING is an error code, the IP child has been
667 // successfully configured and doesn't need reconfiguration when
668 // default address is acquired.
670 if (Config
->UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
671 return EFI_NO_MAPPING
;
677 Ip4FreeRouteTable (IpInstance
->RouteTable
);
678 IpInstance
->RouteTable
= NULL
;
684 Clean up the IP4 child, release all the resources used by it.
686 @param IpInstance The IP4 child to clean up.
688 @retval EFI_SUCCESS The IP4 child is cleaned up
689 @retval EFI_DEVICE_ERROR Some resources failed to be released
694 IN IP4_PROTOCOL
*IpInstance
697 if (EFI_ERROR (Ip4Cancel (IpInstance
, NULL
))) {
698 return EFI_DEVICE_ERROR
;
701 if (EFI_ERROR (Ip4Groups (IpInstance
, FALSE
, NULL
))) {
702 return EFI_DEVICE_ERROR
;
706 // Some packets haven't been recycled. It is because either the
707 // user forgets to recycle the packets, or because the callback
708 // hasn't been called. Just leave it alone.
710 if (!NetListIsEmpty (&IpInstance
->Delivered
)) {
714 if (IpInstance
->Interface
!= NULL
) {
715 NetListRemoveEntry (&IpInstance
->AddrLink
);
716 Ip4FreeInterface (IpInstance
->Interface
, IpInstance
);
717 IpInstance
->Interface
= NULL
;
720 if (IpInstance
->RouteTable
!= NULL
) {
721 if (IpInstance
->RouteTable
->Next
!= NULL
) {
722 Ip4FreeRouteTable (IpInstance
->RouteTable
->Next
);
725 Ip4FreeRouteTable (IpInstance
->RouteTable
);
726 IpInstance
->RouteTable
= NULL
;
729 if (IpInstance
->EfiRouteTable
!= NULL
) {
730 NetFreePool (IpInstance
->EfiRouteTable
);
731 IpInstance
->EfiRouteTable
= NULL
;
732 IpInstance
->EfiRouteCount
= 0;
735 if (IpInstance
->Groups
!= NULL
) {
736 NetFreePool (IpInstance
->Groups
);
737 IpInstance
->Groups
= NULL
;
738 IpInstance
->GroupCount
= 0;
741 NetMapClean (&IpInstance
->TxTokens
);
743 NetMapClean (&IpInstance
->RxTokens
);
750 Validate that Ip/Netmask pair is OK to be used as station
751 address. Only continuous netmasks are supported. and check
752 that StationAddress is a unicast address on the newtwork.
754 @param Ip The IP address to validate
755 @param Netmask The netmaks of the IP
757 @retval TRUE The Ip/Netmask pair is valid
762 Ip4StationAddressValid (
767 IP4_ADDR NetBrdcastMask
;
772 // Only support the station address with 0.0.0.0/0 to enable DHCP client.
774 if (Netmask
== IP4_ALLZERO_ADDRESS
) {
775 return (BOOLEAN
) (Ip
== IP4_ALLZERO_ADDRESS
);
779 // Only support the continuous net masks
781 if ((Len
= NetGetMaskLength (Netmask
)) == IP4_MASK_NUM
) {
786 // Station address can't be class D or class E address
788 if ((Type
= NetGetIpClass (Ip
)) > IP4_ADDR_CLASSC
) {
793 // Station address can't be subnet broadcast/net broadcast address
795 if ((Ip
== (Ip
& Netmask
)) || (Ip
== (Ip
| ~Netmask
))) {
799 NetBrdcastMask
= mIp4AllMasks
[MIN (Len
, Type
<< 3)];
801 if (Ip
== (Ip
| ~NetBrdcastMask
)) {
810 Configure the EFI_IP4_PROTOCOL instance. If IpConfigData is NULL,
811 the instance is cleaned up. If the instance hasn't been configure
812 before, it will be initialized. Otherwise, the filter setting of
813 the instance is updated.
815 @param This The IP4 child to configure
816 @param IpConfigData The configuration to apply. If NULL, clean it up.
818 @retval EFI_INVALID_PARAMETER The parameter is invalid
819 @retval EFI_NO_MAPPING The default address hasn't been configured and the
820 instance wants to use it.
821 @retval EFI_SUCCESS The instance is configured.
828 IN EFI_IP4_PROTOCOL
*This
,
829 IN EFI_IP4_CONFIG_DATA
*IpConfigData OPTIONAL
832 IP4_PROTOCOL
*IpInstance
;
833 EFI_IP4_CONFIG_DATA
*Current
;
841 // First, validate the parameters
844 return EFI_INVALID_PARAMETER
;
847 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
848 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
851 // Validate the configuration first.
853 if (IpConfigData
!= NULL
) {
855 // This implementation doesn't support RawData
857 if (IpConfigData
->RawData
) {
858 Status
= EFI_UNSUPPORTED
;
863 NetCopyMem (&IpAddress
, &IpConfigData
->StationAddress
, sizeof (IP4_ADDR
));
864 NetCopyMem (&SubnetMask
, &IpConfigData
->SubnetMask
, sizeof (IP4_ADDR
));
866 IpAddress
= NTOHL (IpAddress
);
867 SubnetMask
= NTOHL (SubnetMask
);
870 // Check whether the station address is a valid unicast address
872 if (!IpConfigData
->UseDefaultAddress
) {
873 AddrOk
= Ip4StationAddressValid (IpAddress
, SubnetMask
);
876 Status
= EFI_INVALID_PARAMETER
;
882 // User can only update packet filters when already configured.
883 // If it wants to change the station address, it must configure(NULL)
884 // the instance first.
886 if (IpInstance
->State
== IP4_STATE_CONFIGED
) {
887 Current
= &IpInstance
->ConfigData
;
889 if (Current
->UseDefaultAddress
!= IpConfigData
->UseDefaultAddress
) {
890 Status
= EFI_ALREADY_STARTED
;
894 if (!Current
->UseDefaultAddress
&&
895 (!EFI_IP4_EQUAL (&Current
->StationAddress
, &IpConfigData
->StationAddress
) ||
896 !EFI_IP4_EQUAL (&Current
->SubnetMask
, &IpConfigData
->SubnetMask
))) {
897 Status
= EFI_ALREADY_STARTED
;
901 if (Current
->UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
902 return EFI_NO_MAPPING
;
908 // Configure the instance or clean it up.
910 if (IpConfigData
!= NULL
) {
911 Status
= Ip4ConfigProtocol (IpInstance
, IpConfigData
);
913 Status
= Ip4CleanProtocol (IpInstance
);
916 // Don't change the state if it is DESTORY, consider the following
917 // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,
918 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,
919 // the unload fails miserably.
921 if (IpInstance
->State
== IP4_STATE_CONFIGED
) {
922 IpInstance
->State
= IP4_STATE_UNCONFIGED
;
927 // Update the MNP's configure data. Ip4ServiceConfigMnp will check
928 // whether it is necessary to reconfigure the MNP.
930 Ip4ServiceConfigMnp (IpInstance
->Service
, FALSE
);
933 // Update the variable data.
935 Ip4SetVariableData (IpInstance
->Service
);
938 NET_RESTORE_TPL (OldTpl
);
945 Change the IP4 child's multicast setting. The caller
946 should make sure that the parameters is valid.
948 @param IpInstance The IP4 child to change the setting.
949 @param JoinFlag TRUE to join the group, otherwise leave it
950 @param GroupAddress The target group address
952 @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it
953 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
954 @retval EFI_DEVICE_ERROR Failed to set the group configuraton
955 @retval EFI_SUCCESS Successfully updated the group setting.
956 @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.
961 IN IP4_PROTOCOL
*IpInstance
,
963 IN EFI_IPv4_ADDRESS
*GroupAddress OPTIONAL
971 // Add it to the instance's Groups, and join the group by IGMP.
972 // IpInstance->Groups is in network byte order. IGMP operates in
976 NetCopyMem (&Group
, GroupAddress
, sizeof (IP4_ADDR
));
978 for (Index
= 0; Index
< IpInstance
->GroupCount
; Index
++) {
979 if (IpInstance
->Groups
[Index
] == Group
) {
980 return EFI_ALREADY_STARTED
;
984 Members
= Ip4CombineGroups (IpInstance
->Groups
, IpInstance
->GroupCount
, Group
);
986 if (Members
== NULL
) {
987 return EFI_OUT_OF_RESOURCES
;
990 if (EFI_ERROR (Ip4JoinGroup (IpInstance
, NTOHL (Group
)))) {
991 NetFreePool (Members
);
992 return EFI_DEVICE_ERROR
;
995 if (IpInstance
->Groups
!= NULL
) {
996 NetFreePool (IpInstance
->Groups
);
999 IpInstance
->Groups
= Members
;
1000 IpInstance
->GroupCount
++;
1006 // Leave the group. Leave all the groups if GroupAddress is NULL.
1007 // Must iterate from the end to the beginning because the GroupCount
1008 // is decreamented each time an address is removed..
1010 for (Index
= IpInstance
->GroupCount
; Index
> 0 ; Index
--) {
1011 Group
= IpInstance
->Groups
[Index
- 1];
1013 if ((GroupAddress
== NULL
) || EFI_IP4_EQUAL (&Group
, GroupAddress
)) {
1014 if (EFI_ERROR (Ip4LeaveGroup (IpInstance
, NTOHL (Group
)))) {
1015 return EFI_DEVICE_ERROR
;
1018 Ip4RemoveGroupAddr (IpInstance
->Groups
, IpInstance
->GroupCount
, Group
);
1019 IpInstance
->GroupCount
--;
1021 if (IpInstance
->GroupCount
== 0) {
1022 ASSERT (Index
== 1);
1024 NetFreePool (IpInstance
->Groups
);
1025 IpInstance
->Groups
= NULL
;
1028 if (GroupAddress
!= NULL
) {
1034 return ((GroupAddress
!= NULL
) ? EFI_NOT_FOUND
: EFI_SUCCESS
);
1039 Change the IP4 child's multicast setting. If JoinFlag is true,
1040 the child wants to join the group. Otherwise it wants to leave
1041 the group. If JoinFlag is false, and GroupAddress is NULL,
1042 it will leave all the groups which is a member.
1044 @param This The IP4 child to change the setting.
1045 @param JoinFlag TRUE to join the group, otherwise leave it.
1046 @param GroupAddress The target group address
1048 @retval EFI_INVALID_PARAMETER The parameters are invalid
1049 @retval EFI_SUCCESS The group setting has been changed.
1050 @retval Otherwise It failed to change the setting.
1057 IN EFI_IP4_PROTOCOL
*This
,
1058 IN BOOLEAN JoinFlag
,
1059 IN EFI_IPv4_ADDRESS
*GroupAddress OPTIONAL
1062 IP4_PROTOCOL
*IpInstance
;
1067 if ((This
== NULL
) || (JoinFlag
&& (GroupAddress
== NULL
))) {
1068 return EFI_INVALID_PARAMETER
;
1071 if (GroupAddress
!= NULL
) {
1072 NetCopyMem (&McastIp
, GroupAddress
, sizeof (IP4_ADDR
));
1074 if (!IP4_IS_MULTICAST (NTOHL (McastIp
))) {
1075 return EFI_INVALID_PARAMETER
;
1079 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1080 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
1082 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1083 Status
= EFI_NOT_STARTED
;
1087 if (IpInstance
->ConfigData
.UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
1088 Status
= EFI_NO_MAPPING
;
1092 Status
= Ip4Groups (IpInstance
, JoinFlag
, GroupAddress
);
1095 NET_RESTORE_TPL (OldTpl
);
1101 Modify the IP child's route table. Each instance has its own
1104 @param This The IP4 child to modify the route
1105 @param DeleteRoute TRUE to delete the route, otherwise add it
1106 @param SubnetAddress The destination network
1107 @param SubnetMask The destination network's mask
1108 @param GatewayAddress The next hop address.
1110 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1111 @retval EFI_SUCCESS The route table is successfully modified.
1112 @retval Others Failed to modify the route table
1119 IN EFI_IP4_PROTOCOL
*This
,
1120 IN BOOLEAN DeleteRoute
,
1121 IN EFI_IPv4_ADDRESS
*SubnetAddress
,
1122 IN EFI_IPv4_ADDRESS
*SubnetMask
,
1123 IN EFI_IPv4_ADDRESS
*GatewayAddress
1126 IP4_PROTOCOL
*IpInstance
;
1127 IP4_INTERFACE
*IpIf
;
1135 // First, validate the parameters
1137 if ((This
== NULL
) || (SubnetAddress
== NULL
) ||
1138 (SubnetMask
== NULL
) || (GatewayAddress
== NULL
)) {
1139 return EFI_INVALID_PARAMETER
;
1142 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1143 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
1145 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1146 Status
= EFI_NOT_STARTED
;
1150 if (IpInstance
->ConfigData
.UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
1151 Status
= EFI_NO_MAPPING
;
1155 NetCopyMem (&Dest
, SubnetAddress
, sizeof (IP4_ADDR
));
1156 NetCopyMem (&Netmask
, SubnetMask
, sizeof (IP4_ADDR
));
1157 NetCopyMem (&Nexthop
, GatewayAddress
, sizeof (IP4_ADDR
));
1159 Dest
= NTOHL (Dest
);
1160 Netmask
= NTOHL (Netmask
);
1161 Nexthop
= NTOHL (Nexthop
);
1163 IpIf
= IpInstance
->Interface
;
1165 if (!IP4_IS_VALID_NETMASK (Netmask
)) {
1166 Status
= EFI_INVALID_PARAMETER
;
1171 // the gateway address must be a unicast on the connected network if not zero.
1173 if ((Nexthop
!= IP4_ALLZERO_ADDRESS
) &&
1174 (!IP4_NET_EQUAL (Nexthop
, IpIf
->Ip
, IpIf
->SubnetMask
) ||
1175 IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop
, IpIf
)))) {
1177 Status
= EFI_INVALID_PARAMETER
;
1182 Status
= Ip4DelRoute (IpInstance
->RouteTable
, Dest
, Netmask
, Nexthop
);
1184 Status
= Ip4AddRoute (IpInstance
->RouteTable
, Dest
, Netmask
, Nexthop
);
1188 NET_RESTORE_TPL (OldTpl
);
1194 Check whether the user's token or event has already
1195 been enqueue on IP4's list.
1197 @param Map The container of either user's transmit or receive
1199 @param Item Current item to check against
1200 @param Context The Token to check againist.
1202 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
1203 @retval EFI_SUCCESS The current item isn't the same token/event as the
1211 IN NET_MAP_ITEM
*Item
,
1215 EFI_IP4_COMPLETION_TOKEN
*Token
;
1216 EFI_IP4_COMPLETION_TOKEN
*TokenInItem
;
1218 Token
= (EFI_IP4_COMPLETION_TOKEN
*) Context
;
1219 TokenInItem
= (EFI_IP4_COMPLETION_TOKEN
*) Item
->Key
;
1221 if ((Token
== TokenInItem
) || (Token
->Event
== TokenInItem
->Event
)) {
1222 return EFI_ACCESS_DENIED
;
1230 Validate the user's token against current station address.
1232 @param Token User's token to validate
1233 @param IpIf The IP4 child's interface.
1235 @retval EFI_INVALID_PARAMETER Some parameters are invalid
1236 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.
1237 @retval EFI_SUCCESS The token is OK
1243 IN EFI_IP4_COMPLETION_TOKEN
*Token
,
1244 IN IP4_INTERFACE
*IpIf
1247 EFI_IP4_TRANSMIT_DATA
*TxData
;
1248 EFI_IP4_OVERRIDE_DATA
*Override
;
1255 if ((Token
== NULL
) || (Token
->Event
== NULL
) || (Token
->Packet
.TxData
== NULL
)) {
1256 return EFI_INVALID_PARAMETER
;
1259 TxData
= Token
->Packet
.TxData
;
1262 // Check the IP options: no more than 40 bytes and format is OK
1264 if (TxData
->OptionsLength
!= 0) {
1265 if ((TxData
->OptionsLength
> 40) || (TxData
->OptionsBuffer
== NULL
)) {
1266 return EFI_INVALID_PARAMETER
;
1269 if (!Ip4OptionIsValid (TxData
->OptionsBuffer
, TxData
->OptionsLength
, FALSE
)) {
1270 return EFI_INVALID_PARAMETER
;
1275 // Check the fragment table: no empty fragment, and length isn't bogus
1277 if ((TxData
->TotalDataLength
== 0) || (TxData
->FragmentCount
== 0)) {
1278 return EFI_INVALID_PARAMETER
;
1281 Offset
= TxData
->TotalDataLength
;
1283 for (Index
= 0; Index
< TxData
->FragmentCount
; Index
++) {
1284 if ((TxData
->FragmentTable
[Index
].FragmentBuffer
== NULL
) ||
1285 (TxData
->FragmentTable
[Index
].FragmentLength
== 0)) {
1287 return EFI_INVALID_PARAMETER
;
1290 Offset
-= TxData
->FragmentTable
[Index
].FragmentLength
;
1294 return EFI_INVALID_PARAMETER
;
1298 // Check the source and gateway: they must be a valid unicast.
1299 // Gateway must also be on the connected network.
1301 if (TxData
->OverrideData
) {
1302 Override
= TxData
->OverrideData
;
1304 NetCopyMem (&Src
, &Override
->SourceAddress
, sizeof (IP4_ADDR
));
1305 NetCopyMem (&Gateway
, &Override
->GatewayAddress
, sizeof (IP4_ADDR
));
1308 Gateway
= NTOHL (Gateway
);
1310 if ((NetGetIpClass (Src
) > IP4_ADDR_CLASSC
) ||
1311 (Src
== IP4_ALLONE_ADDRESS
) ||
1312 IP4_IS_BROADCAST (Ip4GetNetCast (Src
, IpIf
))) {
1314 return EFI_INVALID_PARAMETER
;
1318 // If gateway isn't zero, it must be a unicast address, and
1319 // on the connected network.
1321 if ((Gateway
!= IP4_ALLZERO_ADDRESS
) &&
1322 ((NetGetIpClass (Gateway
) > IP4_ADDR_CLASSC
) ||
1323 !IP4_NET_EQUAL (Gateway
, IpIf
->Ip
, IpIf
->SubnetMask
) ||
1324 IP4_IS_BROADCAST (Ip4GetNetCast (Gateway
, IpIf
)))) {
1326 return EFI_INVALID_PARAMETER
;
1331 // Check the packet length: Head length and packet length all has a limit
1333 HeadLen
= sizeof (IP4_HEAD
) + ((TxData
->OptionsLength
+ 3) &~0x03);
1335 if ((HeadLen
> IP4_MAX_HEADLEN
) ||
1336 (TxData
->TotalDataLength
+ HeadLen
> IP4_MAX_PACKET_SIZE
)) {
1338 return EFI_BAD_BUFFER_SIZE
;
1346 The callback function for the net buffer which wraps the user's
1347 transmit token. Although it seems this function is pretty simple,
1348 there are some subtle things.
1349 When user requests the IP to transmit a packet by passing it a
1350 token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data
1351 is wrapped in an net buffer. the net buffer's Free function is
1352 set to Ip4FreeTxToken. The Token and token wrap are added to the
1353 IP child's TxToken map. Then the buffer is passed to Ip4Output for
1354 transmission. If something error happened before that, the buffer
1355 is freed, which in turn will free the token wrap. The wrap may
1356 have been added to the TxToken map or not, and the user's event
1357 shouldn't be fired because we are still in the EfiIp4Transmit. If
1358 the buffer has been sent by Ip4Output, it should be removed from
1359 the TxToken map and user's event signaled. The token wrap and buffer
1360 are bound together. Check the comments in Ip4Output for information
1361 about IP fragmentation.
1363 @param Context The token's wrap
1374 IP4_TXTOKEN_WRAP
*Wrap
;
1377 Wrap
= (IP4_TXTOKEN_WRAP
*) Context
;
1380 // Find the token in the instance's map. EfiIp4Transmit put the
1381 // token to the map. If that failed, NetMapFindKey will return NULL.
1383 Item
= NetMapFindKey (&Wrap
->IpInstance
->TxTokens
, Wrap
->Token
);
1386 NetMapRemoveItem (&Wrap
->IpInstance
->TxTokens
, Item
, NULL
);
1390 gBS
->SignalEvent (Wrap
->Token
->Event
);
1393 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
1395 NetLibDispatchDpc ();
1403 The callback function to Ip4Output to update the transmit status.
1405 @param Ip4Instance The Ip4Instance that request the transmit.
1406 @param Packet The user's transmit request
1407 @param IoStatus The result of the transmission
1408 @param Flag Not used during transmission
1409 @param Context The token's wrap.
1417 IP4_PROTOCOL
*Ip4Instance
,
1419 EFI_STATUS IoStatus
,
1424 IP4_TXTOKEN_WRAP
*Wrap
;
1427 // This is the transmission request from upper layer,
1428 // not the IP4 driver itself.
1430 ASSERT (Ip4Instance
!= NULL
);
1433 // The first fragment of the packet has been sent. Update
1434 // the token's status. That is, if fragmented, the transmit's
1435 // status is the first fragment's status. The Wrap will be
1436 // release when all the fragments are release. Check the comments
1437 // in Ip4FreeTxToken and Ip4Output for information.
1439 Wrap
= (IP4_TXTOKEN_WRAP
*) Context
;
1440 Wrap
->Token
->Status
= IoStatus
;
1442 NetbufFree (Wrap
->Packet
);
1447 Transmit the user's data asynchronously. When transmission
1448 completed,the Token's status is updated and its event signalled.
1450 @param This The IP4 child instance
1451 @param Token The user's transmit token, which contains user's
1452 data, the result and an event to signal when
1455 @retval EFI_INVALID_PARAMETER The parameter is invalid.
1456 @retval EFI_NOT_STARTED The IP4 child hasn't been started.
1457 @retval EFI_SUCCESS The user's data has been successfully enqueued
1465 IN EFI_IP4_PROTOCOL
*This
,
1466 IN EFI_IP4_COMPLETION_TOKEN
*Token
1470 IP4_PROTOCOL
*IpInstance
;
1471 IP4_INTERFACE
*IpIf
;
1472 IP4_TXTOKEN_WRAP
*Wrap
;
1473 EFI_IP4_TRANSMIT_DATA
*TxData
;
1474 EFI_IP4_CONFIG_DATA
*Config
;
1475 EFI_IP4_OVERRIDE_DATA
*Override
;
1480 BOOLEAN DontFragment
;
1484 return EFI_INVALID_PARAMETER
;
1487 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1489 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1490 return EFI_NOT_STARTED
;
1493 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
1495 IpSb
= IpInstance
->Service
;
1496 IpIf
= IpInstance
->Interface
;
1497 Config
= &IpInstance
->ConfigData
;
1499 if (Config
->UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
1500 Status
= EFI_NO_MAPPING
;
1505 // make sure that token is properly formated
1507 Status
= Ip4TxTokenValid (Token
, IpIf
);
1509 if (EFI_ERROR (Status
)) {
1514 // Check whether the token or signal already existed.
1516 if (EFI_ERROR (NetMapIterate (&IpInstance
->TxTokens
, Ip4TokenExist
, Token
))) {
1517 Status
= EFI_ACCESS_DENIED
;
1522 // Build the IP header, need to fill in the Tos, TotalLen, Id,
1523 // fragment, Ttl, protocol, Src, and Dst.
1525 TxData
= Token
->Packet
.TxData
;
1527 NetCopyMem (&Head
.Dst
, &TxData
->DestinationAddress
, sizeof (IP4_ADDR
));
1528 Head
.Dst
= NTOHL (Head
.Dst
);
1530 if (TxData
->OverrideData
) {
1531 Override
= TxData
->OverrideData
;
1532 Head
.Protocol
= Override
->Protocol
;
1533 Head
.Tos
= Override
->TypeOfService
;
1534 Head
.Ttl
= Override
->TimeToLive
;
1535 DontFragment
= Override
->DoNotFragment
;
1537 NetCopyMem (&Head
.Src
, &Override
->SourceAddress
, sizeof (IP4_ADDR
));
1538 NetCopyMem (&GateWay
, &Override
->GatewayAddress
, sizeof (IP4_ADDR
));
1540 Head
.Src
= NTOHL (Head
.Src
);
1541 GateWay
= NTOHL (GateWay
);
1543 Head
.Src
= IpIf
->Ip
;
1544 GateWay
= IP4_ALLZERO_ADDRESS
;
1545 Head
.Protocol
= Config
->DefaultProtocol
;
1546 Head
.Tos
= Config
->TypeOfService
;
1547 Head
.Ttl
= Config
->TimeToLive
;
1548 DontFragment
= Config
->DoNotFragment
;
1551 Head
.Fragment
= IP4_HEAD_FRAGMENT_FIELD (DontFragment
, FALSE
, 0);
1552 HeadLen
= sizeof (IP4_HEAD
) + ((TxData
->OptionsLength
+ 3) &~0x03);
1555 // If don't fragment and fragment needed, return error
1557 if (DontFragment
&& (TxData
->TotalDataLength
+ HeadLen
> IpSb
->SnpMode
.MaxPacketSize
)) {
1558 Status
= EFI_BAD_BUFFER_SIZE
;
1563 // OK, it survives all the validation check. Wrap the token in
1564 // a IP4_TXTOKEN_WRAP and the data in a netbuf
1566 Status
= EFI_OUT_OF_RESOURCES
;
1567 Wrap
= NetAllocatePool (sizeof (IP4_TXTOKEN_WRAP
));
1572 Wrap
->IpInstance
= IpInstance
;
1573 Wrap
->Token
= Token
;
1575 Wrap
->Life
= IP4_US_TO_SEC (Config
->TransmitTimeout
);
1576 Wrap
->Packet
= NetbufFromExt (
1577 (NET_FRAGMENT
*) TxData
->FragmentTable
,
1578 TxData
->FragmentCount
,
1585 if (Wrap
->Packet
== NULL
) {
1590 Token
->Status
= EFI_NOT_READY
;
1592 if (EFI_ERROR (NetMapInsertTail (&IpInstance
->TxTokens
, Token
, Wrap
))) {
1594 // NetbufFree will call Ip4FreeTxToken, which in turn will
1595 // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been
1598 NetbufFree (Wrap
->Packet
);
1603 // Mark the packet sent before output it. Mark it not sent again if the
1604 // returned status is not EFI_SUCCESS;
1608 Status
= Ip4Output (
1613 TxData
->OptionsBuffer
,
1614 TxData
->OptionsLength
,
1620 if (EFI_ERROR (Status
)) {
1622 NetbufFree (Wrap
->Packet
);
1626 NET_RESTORE_TPL (OldTpl
);
1632 Receive a packet for the upper layer. If there are packets
1633 pending on the child's receive queue, the receive request
1634 will be fulfilled immediately. Otherwise, the request is
1635 enqueued. When receive request is completed, the status in
1636 the Token is updated and its event is signalled.
1638 @param This The IP4 child to receive packet.
1639 @param Token The user's receive token
1641 @retval EFI_INVALID_PARAMETER The token is invalid.
1642 @retval EFI_NOT_STARTED The IP4 child hasn't been started
1643 @retval EFI_ACCESS_DENIED The token or event is already queued.
1644 @retval EFI_SUCCESS The receive request has been issued.
1651 IN EFI_IP4_PROTOCOL
*This
,
1652 IN EFI_IP4_COMPLETION_TOKEN
*Token
1655 IP4_PROTOCOL
*IpInstance
;
1660 // First validate the parameters
1662 if ((This
== NULL
) || (Token
== NULL
) || (Token
->Event
== NULL
)) {
1663 return EFI_INVALID_PARAMETER
;
1666 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1668 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
1670 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1671 Status
= EFI_NOT_STARTED
;
1676 // Current Udp implementation creates an IP child for each Udp child.
1677 // It initates a asynchronous receive immediately no matter whether
1678 // there is no mapping or not. Disable this for now.
1681 if (Config
->UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
1682 Status
= EFI_NO_MAPPING
;
1688 // Check whether the toke is already on the receive queue.
1690 Status
= NetMapIterate (&IpInstance
->RxTokens
, Ip4TokenExist
, Token
);
1692 if (EFI_ERROR (Status
)) {
1693 Status
= EFI_ACCESS_DENIED
;
1698 // Queue the token then check whether there is pending received packet.
1700 Status
= NetMapInsertTail (&IpInstance
->RxTokens
, Token
, NULL
);
1702 if (EFI_ERROR (Status
)) {
1706 Status
= Ip4InstanceDeliverPacket (IpInstance
);
1709 // Dispatch the DPC queued by the NotifyFunction of this instane's receive
1712 NetLibDispatchDpc ();
1715 NET_RESTORE_TPL (OldTpl
);
1721 Cancel the transmitted but not recycled packet. If a matching
1722 token is found, it will call Ip4CancelPacket to cancel the
1723 packet. Ip4CancelPacket will cancel all the fragments of the
1724 packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP
1725 will be deleted from the Map, and user's event signalled.
1726 Because Ip4CancelPacket and other functions are all called in
1727 line, so, after Ip4CancelPacket returns, the Item has been freed.
1729 @param Map The IP4 child's transmit queue
1730 @param Item The current transmitted packet to test.
1731 @param Context The user's token to cancel.
1733 @retval EFI_SUCCESS Continue to check the next Item.
1734 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
1741 IN NET_MAP_ITEM
*Item
,
1745 EFI_IP4_COMPLETION_TOKEN
*Token
;
1746 IP4_TXTOKEN_WRAP
*Wrap
;
1748 Token
= (EFI_IP4_COMPLETION_TOKEN
*) Context
;
1751 // Return EFI_SUCCESS to check the next item in the map if
1752 // this one doesn't match.
1754 if ((Token
!= NULL
) && (Token
!= Item
->Key
)) {
1758 Wrap
= (IP4_TXTOKEN_WRAP
*) Item
->Value
;
1759 ASSERT (Wrap
!= NULL
);
1762 // Don't access the Item, Wrap and Token's members after this point.
1763 // Item and wrap has been freed. And we no longer own the Token.
1765 Ip4CancelPacket (Wrap
->IpInstance
->Interface
, Wrap
->Packet
, EFI_ABORTED
);
1768 // If only one item is to be cancel, return EFI_ABORTED to stop
1769 // iterating the map any more.
1771 if (Token
!= NULL
) {
1780 Cancel the receive request. This is quiet simple, because
1781 it is only enqueued in our local receive map.
1783 @param Map The IP4 child's receive queue
1784 @param Item Current receive request to cancel.
1785 @param Context The user's token to cancel
1787 @retval EFI_SUCCESS Continue to check the next receive request on the
1789 @retval EFI_ABORTED The user's token (token != NULL) has been
1797 IN NET_MAP_ITEM
*Item
,
1801 EFI_IP4_COMPLETION_TOKEN
*Token
;
1802 EFI_IP4_COMPLETION_TOKEN
*This
;
1804 Token
= (EFI_IP4_COMPLETION_TOKEN
*) Context
;
1807 if ((Token
!= NULL
) && (Token
!= This
)) {
1811 NetMapRemoveItem (Map
, Item
, NULL
);
1813 This
->Status
= EFI_ABORTED
;
1814 This
->Packet
.RxData
= NULL
;
1815 gBS
->SignalEvent (This
->Event
);
1817 if (Token
!= NULL
) {
1826 Cancel the user's receive/transmit request.
1828 @param IpInstance The IP4 child
1829 @param Token The token to cancel. If NULL, all token will be
1832 @retval EFI_SUCCESS The token is cancelled
1833 @retval EFI_NOT_FOUND The token isn't found on either the
1834 transmit/receive queue
1835 @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.
1840 IN IP4_PROTOCOL
*IpInstance
,
1841 IN EFI_IP4_COMPLETION_TOKEN
*Token OPTIONAL
1847 // First check the transmitted packet. Ip4CancelTxTokens returns
1848 // EFI_ABORTED to mean that the token has been cancelled when
1849 // token != NULL. So, return EFI_SUCCESS for this condition.
1851 Status
= NetMapIterate (&IpInstance
->TxTokens
, Ip4CancelTxTokens
, Token
);
1853 if (EFI_ERROR (Status
)) {
1854 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1862 // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT
1863 // for Token!=NULL and it is cancelled.
1865 Status
= NetMapIterate (&IpInstance
->RxTokens
, Ip4CancelRxTokens
, Token
);
1867 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
1870 NetLibDispatchDpc ();
1871 if (EFI_ERROR (Status
)) {
1872 if ((Token
!= NULL
) && (Status
== EFI_ABORTED
)) {
1880 // OK, if the Token is found when Token != NULL, the NetMapIterate
1881 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.
1883 if (Token
!= NULL
) {
1884 return EFI_NOT_FOUND
;
1888 // If Token == NULL, cancel all the tokens. return error if no
1889 // all of them are cancelled.
1891 if (!NetMapIsEmpty (&IpInstance
->TxTokens
) ||
1892 !NetMapIsEmpty (&IpInstance
->RxTokens
)) {
1894 return EFI_DEVICE_ERROR
;
1902 Cancel the queued receive/transmit requests. If Token is NULL,
1903 all the queued requests will be cancelled. It just validate
1904 the parameter then pass them to Ip4Cancel.
1906 @param This The IP4 child to cancel the request
1907 @param Token The token to cancel, if NULL, cancel all.
1909 @retval EFI_INVALID_PARAMETER This is NULL
1910 @retval EFI_NOT_STARTED The IP4 child hasn't been configured.
1911 @retval EFI_NO_MAPPING The IP4 child is configured to use the default,
1912 but the default address hasn't been acquired.
1913 @retval EFI_SUCCESS The Token is cancelled.
1920 IN EFI_IP4_PROTOCOL
*This
,
1921 IN EFI_IP4_COMPLETION_TOKEN
*Token OPTIONAL
1924 IP4_PROTOCOL
*IpInstance
;
1929 return EFI_INVALID_PARAMETER
;
1932 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1934 OldTpl
= NET_RAISE_TPL (NET_TPL_LOCK
);
1936 if (IpInstance
->State
!= IP4_STATE_CONFIGED
) {
1937 Status
= EFI_NOT_STARTED
;
1941 if (IpInstance
->ConfigData
.UseDefaultAddress
&& IP4_NO_MAPPING (IpInstance
)) {
1942 Status
= EFI_NO_MAPPING
;
1946 Status
= Ip4Cancel (IpInstance
, Token
);
1949 NET_RESTORE_TPL (OldTpl
);
1955 Poll the network stack. The EFI network stack is poll based. There
1956 is no interrupt support for the network interface card.
1958 @param This The IP4 child to poll through
1960 @retval EFI_INVALID_PARAMETER The parameter is invalid
1961 @retval EFI_NOT_STARTED The IP4 child hasn't been configured.
1968 IN EFI_IP4_PROTOCOL
*This
1971 IP4_PROTOCOL
*IpInstance
;
1972 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
1975 return EFI_INVALID_PARAMETER
;
1978 IpInstance
= IP4_INSTANCE_FROM_PROTOCOL (This
);
1980 if (IpInstance
->State
== IP4_STATE_UNCONFIGED
) {
1981 return EFI_NOT_STARTED
;
1984 Mnp
= IpInstance
->Service
->Mnp
;
1987 // Don't lock the Poll function to enable the deliver of
1988 // the packet polled up.
1990 return Mnp
->Poll (Mnp
);
1994 mEfiIp4ProtocolTemplete
= {
2007 Decrease the life of the transmitted packets. If it is
2008 decreased to zero, cancel the packet. This function is
2009 called by Ip4packetTimerTicking which time out both the
2010 received-but-not-delivered and transmitted-but-not-recycle
2013 @param Map The IP4 child's transmit map.
2014 @param Item Current transmitted packet
2015 @param Context Not used.
2017 @retval EFI_SUCCESS Always returns EFI_SUCCESS
2021 Ip4SentPacketTicking (
2023 IN NET_MAP_ITEM
*Item
,
2027 IP4_TXTOKEN_WRAP
*Wrap
;
2029 Wrap
= (IP4_TXTOKEN_WRAP
*) Item
->Value
;
2030 ASSERT (Wrap
!= NULL
);
2032 if ((Wrap
->Life
> 0) && (--Wrap
->Life
== 0)) {
2033 Ip4CancelPacket (Wrap
->IpInstance
->Interface
, Wrap
->Packet
, EFI_ABORTED
);
2041 The heart beat timer of IP4 service instance. It times out
2042 all of its IP4 children's received-but-not-delivered and
2043 transmitted-but-not-recycle packets, and provides time input
2044 for its IGMP protocol.
2046 @param Event The IP4 service instance's heart beat timer.
2047 @param Context The IP4 service instance.
2061 IpSb
= (IP4_SERVICE
*) Context
;
2062 NET_CHECK_SIGNATURE (IpSb
, IP4_SERVICE_SIGNATURE
);
2064 Ip4PacketTimerTicking (IpSb
);
2065 Ip4IgmpTicking (IpSb
);