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