1 /** @file 2 The implementation of the Udp4 protocol. 3 4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> 5 This program and the accompanying materials 6 are licensed and made available under the terms and conditions of the BSD License 7 which accompanies this distribution. The full text of the license may be found at 8 http://opensource.org/licenses/bsd-license.php 9 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 12 13 **/ 14 15 16 #include "Udp4Impl.h" 17 18 UINT16 mUdp4RandomPort; 19 20 /** 21 This function checks and timeouts the I/O datagrams holding by the corresponding 22 service context. 23 24 @param[in] Event The event this function registered to. 25 @param[in] Context The context data registered during the creation of 26 the Event. 27 28 **/ 29 VOID 30 EFIAPI 31 Udp4CheckTimeout ( 32 IN EFI_EVENT Event, 33 IN VOID *Context 34 ); 35 36 /** 37 This function finds the udp instance by the specified <Address, Port> pair. 38 39 @param[in] InstanceList Pointer to the head of the list linking the udp 40 instances. 41 @param[in] Address Pointer to the specified IPv4 address. 42 @param[in] Port The udp port number. 43 44 @retval TRUE The specified <Address, Port> pair is found. 45 @retval FALSE Otherwise. 46 47 **/ 48 BOOLEAN 49 Udp4FindInstanceByPort ( 50 IN LIST_ENTRY *InstanceList, 51 IN EFI_IPv4_ADDRESS *Address, 52 IN UINT16 Port 53 ); 54 55 /** 56 This function is the packet transmitting notify function registered to the IpIo 57 interface. It's called to signal the udp TxToken when IpIo layer completes the 58 transmitting of the udp datagram. 59 60 @param[in] Status The completion status of the output udp datagram. 61 @param[in] Context Pointer to the context data. 62 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending. 63 @param[in] NotifyData Pointer to the notify data. 64 65 **/ 66 VOID 67 EFIAPI 68 Udp4DgramSent ( 69 IN EFI_STATUS Status, 70 IN VOID *Context, 71 IN IP_IO_IP_PROTOCOL Sender, 72 IN VOID *NotifyData 73 ); 74 75 /** 76 This function processes the received datagram passed up by the IpIo layer. 77 78 @param[in] Status The status of this udp datagram. 79 @param[in] IcmpError The IcmpError code, only available when Status is 80 EFI_ICMP_ERROR. 81 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. 82 @param[in] Packet Pointer to the NET_BUF containing the received udp 83 datagram. 84 @param[in] Context Pointer to the context data. 85 86 **/ 87 VOID 88 EFIAPI 89 Udp4DgramRcvd ( 90 IN EFI_STATUS Status, 91 IN UINT8 IcmpError, 92 IN EFI_NET_SESSION_DATA *NetSession, 93 IN NET_BUF *Packet, 94 IN VOID *Context 95 ); 96 97 /** 98 This function cancels the token specified by Arg in the Map. This is a callback 99 used by Udp4InstanceCancelToken(). 100 101 @param[in] Map Pointer to the NET_MAP. 102 @param[in] Item Pointer to the NET_MAP_ITEM. 103 @param[in] Arg Pointer to the token to be cancelled, if NULL, 104 the token specified by Item is cancelled. 105 106 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token 107 is not the same as that in the Item if Arg is not 108 NULL. 109 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is 110 cancelled. 111 112 **/ 113 EFI_STATUS 114 EFIAPI 115 Udp4CancelTokens ( 116 IN NET_MAP *Map, 117 IN NET_MAP_ITEM *Item, 118 IN VOID *Arg OPTIONAL 119 ); 120 121 /** 122 This function matches the received udp datagram with the Instance. 123 124 @param[in] Instance Pointer to the udp instance context data. 125 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted 126 from the received udp datagram. 127 128 @retval TRUE The udp datagram matches the receiving requirments of the 129 udp Instance. 130 @retval FALSE Otherwise. 131 132 **/ 133 BOOLEAN 134 Udp4MatchDgram ( 135 IN UDP4_INSTANCE_DATA *Instance, 136 IN EFI_UDP4_SESSION_DATA *Udp4Session 137 ); 138 139 /** 140 This function removes the Wrap specified by Context and release relevant resources. 141 142 @param[in] Event The Event this notify function registered to. 143 @param[in] Context Pointer to the context data. 144 145 **/ 146 VOID 147 EFIAPI 148 Udp4RecycleRxDataWrap ( 149 IN EFI_EVENT Event, 150 IN VOID *Context 151 ); 152 153 /** 154 This function wraps the Packet and the RxData. 155 156 @param[in] Instance Pointer to the instance context data. 157 @param[in] Packet Pointer to the buffer containing the received 158 datagram. 159 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this 160 datagram. 161 162 @return Pointer to the structure wrapping the RxData and the Packet. 163 164 **/ 165 UDP4_RXDATA_WRAP * 166 Udp4WrapRxData ( 167 IN UDP4_INSTANCE_DATA *Instance, 168 IN NET_BUF *Packet, 169 IN EFI_UDP4_RECEIVE_DATA *RxData 170 ); 171 172 /** 173 This function enqueues the received datagram into the instances' receiving queues. 174 175 @param[in] Udp4Service Pointer to the udp service context data. 176 @param[in] Packet Pointer to the buffer containing the received 177 datagram. 178 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this 179 datagram. 180 181 @return The times this datagram is enqueued. 182 183 **/ 184 UINTN 185 Udp4EnqueueDgram ( 186 IN UDP4_SERVICE_DATA *Udp4Service, 187 IN NET_BUF *Packet, 188 IN EFI_UDP4_RECEIVE_DATA *RxData 189 ); 190 191 /** 192 This function delivers the datagrams enqueued in the instances. 193 194 @param[in] Udp4Service Pointer to the udp service context data. 195 196 **/ 197 VOID 198 Udp4DeliverDgram ( 199 IN UDP4_SERVICE_DATA *Udp4Service 200 ); 201 202 /** 203 This function demultiplexes the received udp datagram to the appropriate instances. 204 205 @param[in] Udp4Service Pointer to the udp service context data. 206 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from 207 the received datagram. 208 @param[in] Packet Pointer to the buffer containing the received udp 209 datagram. 210 211 **/ 212 VOID 213 Udp4Demultiplex ( 214 IN UDP4_SERVICE_DATA *Udp4Service, 215 IN EFI_NET_SESSION_DATA *NetSession, 216 IN NET_BUF *Packet 217 ); 218 219 /** 220 This function handles the received Icmp Error message and demultiplexes it to the 221 instance. 222 223 @param[in] Udp4Service Pointer to the udp service context data. 224 @param[in] IcmpError The icmp error code. 225 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted 226 from the received Icmp Error packet. 227 @param[in] Packet Pointer to the Icmp Error packet. 228 229 **/ 230 VOID 231 Udp4IcmpHandler ( 232 IN UDP4_SERVICE_DATA *Udp4Service, 233 IN UINT8 IcmpError, 234 IN EFI_NET_SESSION_DATA *NetSession, 235 IN NET_BUF *Packet 236 ); 237 238 /** 239 This function builds and sends out a icmp port unreachable message. 240 241 @param[in] IpIo Pointer to the IP_IO instance. 242 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet 243 causes this icmp error message. 244 @param[in] Udp4Header Pointer to the udp header of the datagram causes 245 this icmp error message. 246 247 **/ 248 VOID 249 Udp4SendPortUnreach ( 250 IN IP_IO *IpIo, 251 IN EFI_NET_SESSION_DATA *NetSession, 252 IN VOID *Udp4Header 253 ); 254 255 256 /** 257 Create the Udp service context data. 258 259 @param[in, out] Udp4Service Pointer to the UDP4_SERVICE_DATA. 260 @param[in] ImageHandle The image handle of this udp4 driver. 261 @param[in] ControllerHandle The controller handle this udp4 driver binds on. 262 263 @retval EFI_SUCCESS The udp4 service context data is created and 264 initialized. 265 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory. 266 @retval other Other error occurs. 267 268 **/ 269 EFI_STATUS 270 Udp4CreateService ( 271 IN OUT UDP4_SERVICE_DATA *Udp4Service, 272 IN EFI_HANDLE ImageHandle, 273 IN EFI_HANDLE ControllerHandle 274 ) 275 { 276 EFI_STATUS Status; 277 IP_IO_OPEN_DATA OpenData; 278 EFI_IP4_CONFIG_DATA *Ip4ConfigData; 279 280 ZeroMem (Udp4Service, sizeof (UDP4_SERVICE_DATA)); 281 282 Udp4Service->Signature = UDP4_SERVICE_DATA_SIGNATURE; 283 Udp4Service->ServiceBinding = mUdp4ServiceBinding; 284 Udp4Service->ImageHandle = ImageHandle; 285 Udp4Service->ControllerHandle = ControllerHandle; 286 Udp4Service->ChildrenNumber = 0; 287 288 InitializeListHead (&Udp4Service->ChildrenList); 289 290 // 291 // Create the IpIo for this service context. 292 // 293 Udp4Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle, IP_VERSION_4); 294 if (Udp4Service->IpIo == NULL) { 295 return EFI_OUT_OF_RESOURCES; 296 } 297 298 // 299 // Set the OpenData used to open the IpIo. 300 // 301 Ip4ConfigData = &OpenData.IpConfigData.Ip4CfgData; 302 CopyMem (Ip4ConfigData, &mIp4IoDefaultIpConfigData, sizeof (EFI_IP4_CONFIG_DATA)); 303 Ip4ConfigData->AcceptBroadcast = TRUE; 304 OpenData.RcvdContext = (VOID *) Udp4Service; 305 OpenData.SndContext = NULL; 306 OpenData.PktRcvdNotify = Udp4DgramRcvd; 307 OpenData.PktSentNotify = Udp4DgramSent; 308 309 // 310 // Configure and start the IpIo. 311 // 312 Status = IpIoOpen (Udp4Service->IpIo, &OpenData); 313 if (EFI_ERROR (Status)) { 314 goto ON_ERROR; 315 } 316 317 // 318 // Create the event for Udp timeout checking. 319 // 320 Status = gBS->CreateEvent ( 321 EVT_TIMER | EVT_NOTIFY_SIGNAL, 322 TPL_CALLBACK, 323 Udp4CheckTimeout, 324 Udp4Service, 325 &Udp4Service->TimeoutEvent 326 ); 327 if (EFI_ERROR (Status)) { 328 goto ON_ERROR; 329 } 330 331 // 332 // Start the timeout timer event. 333 // 334 Status = gBS->SetTimer ( 335 Udp4Service->TimeoutEvent, 336 TimerPeriodic, 337 UDP4_TIMEOUT_INTERVAL 338 ); 339 if (EFI_ERROR (Status)) { 340 goto ON_ERROR; 341 } 342 343 return EFI_SUCCESS; 344 345 ON_ERROR: 346 347 if (Udp4Service->TimeoutEvent != NULL) { 348 gBS->CloseEvent (Udp4Service->TimeoutEvent); 349 } 350 351 IpIoDestroy (Udp4Service->IpIo); 352 353 return Status; 354 } 355 356 357 /** 358 Clean the Udp service context data. 359 360 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. 361 362 **/ 363 VOID 364 Udp4CleanService ( 365 IN UDP4_SERVICE_DATA *Udp4Service 366 ) 367 { 368 // 369 // Cancel the TimeoutEvent timer. 370 // 371 gBS->SetTimer (Udp4Service->TimeoutEvent, TimerCancel, 0); 372 373 // 374 // Close the TimeoutEvent timer. 375 // 376 gBS->CloseEvent (Udp4Service->TimeoutEvent); 377 378 // 379 // Destroy the IpIo. 380 // 381 IpIoDestroy (Udp4Service->IpIo); 382 } 383 384 385 /** 386 This function checks and timeouts the I/O datagrams holding by the corresponding 387 service context. 388 389 @param[in] Event The event this function registered to. 390 @param[in] Context The context data registered during the creation of 391 the Event. 392 393 **/ 394 VOID 395 EFIAPI 396 Udp4CheckTimeout ( 397 IN EFI_EVENT Event, 398 IN VOID *Context 399 ) 400 { 401 UDP4_SERVICE_DATA *Udp4Service; 402 LIST_ENTRY *Entry; 403 UDP4_INSTANCE_DATA *Instance; 404 LIST_ENTRY *WrapEntry; 405 LIST_ENTRY *NextEntry; 406 UDP4_RXDATA_WRAP *Wrap; 407 408 Udp4Service = (UDP4_SERVICE_DATA *) Context; 409 NET_CHECK_SIGNATURE (Udp4Service, UDP4_SERVICE_DATA_SIGNATURE); 410 411 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { 412 // 413 // Iterate all the instances belonging to this service context. 414 // 415 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); 416 NET_CHECK_SIGNATURE (Instance, UDP4_INSTANCE_DATA_SIGNATURE); 417 418 if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) { 419 // 420 // Skip this instance if it's not configured or no receive timeout. 421 // 422 continue; 423 } 424 425 NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) { 426 // 427 // Iterate all the rxdatas belonging to this udp instance. 428 // 429 Wrap = NET_LIST_USER_STRUCT (WrapEntry, UDP4_RXDATA_WRAP, Link); 430 431 // 432 // TimeoutTick unit is microsecond, MNP_TIMEOUT_CHECK_INTERVAL unit is 100ns. 433 // 434 if (Wrap->TimeoutTick < (UDP4_TIMEOUT_INTERVAL / 10)) { 435 // 436 // Remove this RxData if it timeouts. 437 // 438 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap); 439 } else { 440 Wrap->TimeoutTick -= (UDP4_TIMEOUT_INTERVAL / 10); 441 } 442 } 443 } 444 } 445 446 447 /** 448 This function intializes the new created udp instance. 449 450 @param[in] Udp4Service Pointer to the UDP4_SERVICE_DATA. 451 @param[in, out] Instance Pointer to the un-initialized UDP4_INSTANCE_DATA. 452 453 **/ 454 VOID 455 Udp4InitInstance ( 456 IN UDP4_SERVICE_DATA *Udp4Service, 457 IN OUT UDP4_INSTANCE_DATA *Instance 458 ) 459 { 460 // 461 // Set the signature. 462 // 463 Instance->Signature = UDP4_INSTANCE_DATA_SIGNATURE; 464 465 // 466 // Init the lists. 467 // 468 InitializeListHead (&Instance->Link); 469 InitializeListHead (&Instance->RcvdDgramQue); 470 InitializeListHead (&Instance->DeliveredDgramQue); 471 472 // 473 // Init the NET_MAPs. 474 // 475 NetMapInit (&Instance->TxTokens); 476 NetMapInit (&Instance->RxTokens); 477 NetMapInit (&Instance->McastIps); 478 479 // 480 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members. 481 // 482 Instance->Udp4Service = Udp4Service; 483 CopyMem (&Instance->Udp4Proto, &mUdp4Protocol, sizeof (Instance->Udp4Proto)); 484 Instance->IcmpError = EFI_SUCCESS; 485 Instance->Configured = FALSE; 486 Instance->IsNoMapping = FALSE; 487 Instance->InDestroy = FALSE; 488 } 489 490 491 /** 492 This function cleans the udp instance. 493 494 @param[in] Instance Pointer to the UDP4_INSTANCE_DATA to clean. 495 496 **/ 497 VOID 498 Udp4CleanInstance ( 499 IN UDP4_INSTANCE_DATA *Instance 500 ) 501 { 502 NetMapClean (&Instance->McastIps); 503 NetMapClean (&Instance->RxTokens); 504 NetMapClean (&Instance->TxTokens); 505 } 506 507 508 /** 509 This function finds the udp instance by the specified <Address, Port> pair. 510 511 @param[in] InstanceList Pointer to the head of the list linking the udp 512 instances. 513 @param[in] Address Pointer to the specified IPv4 address. 514 @param[in] Port The udp port number. 515 516 @retval TRUE The specified <Address, Port> pair is found. 517 @retval FALSE Otherwise. 518 519 **/ 520 BOOLEAN 521 Udp4FindInstanceByPort ( 522 IN LIST_ENTRY *InstanceList, 523 IN EFI_IPv4_ADDRESS *Address, 524 IN UINT16 Port 525 ) 526 { 527 LIST_ENTRY *Entry; 528 UDP4_INSTANCE_DATA *Instance; 529 EFI_UDP4_CONFIG_DATA *ConfigData; 530 531 NET_LIST_FOR_EACH (Entry, InstanceList) { 532 // 533 // Iterate all the udp instances. 534 // 535 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); 536 ConfigData = &Instance->ConfigData; 537 538 if (!Instance->Configured || ConfigData->AcceptAnyPort) { 539 // 540 // If the instance is not configured or the configdata of the instance indicates 541 // this instance accepts any port, skip it. 542 // 543 continue; 544 } 545 546 if (EFI_IP4_EQUAL (&ConfigData->StationAddress, Address) && 547 (ConfigData->StationPort == Port)) { 548 // 549 // if both the address and the port are the same, return TRUE. 550 // 551 return TRUE; 552 } 553 } 554 555 // 556 // return FALSE when matching fails. 557 // 558 return FALSE; 559 } 560 561 562 /** 563 This function tries to bind the udp instance according to the configured port 564 allocation strategy. 565 566 @param[in] InstanceList Pointer to the head of the list linking the udp 567 instances. 568 @param[in, out] ConfigData Pointer to the ConfigData of the instance to be 569 bound. ConfigData->StationPort will be assigned 570 with an available port value on success. 571 572 @retval EFI_SUCCESS The bound operation is completed successfully. 573 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is 574 already used by other instance. 575 @retval EFI_OUT_OF_RESOURCES No available port resources. 576 577 **/ 578 EFI_STATUS 579 Udp4Bind ( 580 IN LIST_ENTRY *InstanceList, 581 IN OUT EFI_UDP4_CONFIG_DATA *ConfigData 582 ) 583 { 584 EFI_IPv4_ADDRESS *StationAddress; 585 UINT16 StartPort; 586 587 if (ConfigData->AcceptAnyPort) { 588 return EFI_SUCCESS; 589 } 590 591 StationAddress = &ConfigData->StationAddress; 592 593 if (ConfigData->StationPort != 0) { 594 595 if (!ConfigData->AllowDuplicatePort && 596 Udp4FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)) { 597 // 598 // Do not allow duplicate port and the port is already used by other instance. 599 // 600 return EFI_ACCESS_DENIED; 601 } 602 } else { 603 // 604 // select a random port for this instance; 605 // 606 607 if (ConfigData->AllowDuplicatePort) { 608 // 609 // Just pick up the random port if the instance allows duplicate port. 610 // 611 ConfigData->StationPort = mUdp4RandomPort; 612 } else { 613 614 StartPort = mUdp4RandomPort; 615 616 while (Udp4FindInstanceByPort(InstanceList, StationAddress, mUdp4RandomPort)) { 617 618 mUdp4RandomPort++; 619 if (mUdp4RandomPort == 0) { 620 mUdp4RandomPort = UDP4_PORT_KNOWN; 621 } 622 623 if (mUdp4RandomPort == StartPort) { 624 // 625 // No available port. 626 // 627 return EFI_OUT_OF_RESOURCES; 628 } 629 } 630 631 ConfigData->StationPort = mUdp4RandomPort; 632 } 633 634 mUdp4RandomPort++; 635 if (mUdp4RandomPort == 0) { 636 mUdp4RandomPort = UDP4_PORT_KNOWN; 637 } 638 } 639 640 return EFI_SUCCESS; 641 } 642 643 644 /** 645 This function is used to check whether the NewConfigData has any un-reconfigurable 646 parameters changed compared to the OldConfigData. 647 648 @param[in] OldConfigData Pointer to the current ConfigData the udp instance 649 uses. 650 @param[in] NewConfigData Pointer to the new ConfigData. 651 652 @retval TRUE The instance is reconfigurable. 653 @retval FALSE Otherwise. 654 655 **/ 656 BOOLEAN 657 Udp4IsReconfigurable ( 658 IN EFI_UDP4_CONFIG_DATA *OldConfigData, 659 IN EFI_UDP4_CONFIG_DATA *NewConfigData 660 ) 661 { 662 if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) || 663 (NewConfigData->AcceptBroadcast != OldConfigData->AcceptBroadcast) || 664 (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) || 665 (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort) 666 ) { 667 // 668 // The receiving filter parameters cannot be changed. 669 // 670 return FALSE; 671 } 672 673 if ((!NewConfigData->AcceptAnyPort) && 674 (NewConfigData->StationPort != OldConfigData->StationPort) 675 ) { 676 // 677 // The port is not changeable. 678 // 679 return FALSE; 680 } 681 682 if (!NewConfigData->AcceptPromiscuous) { 683 684 if (NewConfigData->UseDefaultAddress != OldConfigData->UseDefaultAddress) { 685 // 686 // The NewConfigData differs to the old one on the UseDefaultAddress. 687 // 688 return FALSE; 689 } 690 691 if (!NewConfigData->UseDefaultAddress && 692 (!EFI_IP4_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress) || 693 !EFI_IP4_EQUAL (&NewConfigData->SubnetMask, &OldConfigData->SubnetMask)) 694 ) { 695 // 696 // If the instance doesn't use the default address, and the new address or 697 // new subnet mask is different from the old values. 698 // 699 return FALSE; 700 } 701 } 702 703 if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) { 704 // 705 // The remoteaddress is not the same. 706 // 707 return FALSE; 708 } 709 710 if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &mZeroIp4Addr) && 711 NewConfigData->RemotePort != OldConfigData->RemotePort 712 ) { 713 // 714 // The RemotePort differs if it's designated in the configdata. 715 // 716 return FALSE; 717 } 718 719 // 720 // All checks pass, return TRUE. 721 // 722 return TRUE; 723 } 724 725 726 /** 727 This function builds the Ip4 configdata from the Udp4ConfigData. 728 729 @param[in] Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA. 730 @param[in, out] Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA. 731 732 **/ 733 VOID 734 Udp4BuildIp4ConfigData ( 735 IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData, 736 IN OUT EFI_IP4_CONFIG_DATA *Ip4ConfigData 737 ) 738 { 739 CopyMem (Ip4ConfigData, &mIp4IoDefaultIpConfigData, sizeof (*Ip4ConfigData)); 740 741 Ip4ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP; 742 Ip4ConfigData->AcceptBroadcast = Udp4ConfigData->AcceptBroadcast; 743 Ip4ConfigData->AcceptPromiscuous = Udp4ConfigData->AcceptPromiscuous; 744 Ip4ConfigData->UseDefaultAddress = Udp4ConfigData->UseDefaultAddress; 745 CopyMem (&Ip4ConfigData->StationAddress, &Udp4ConfigData->StationAddress, sizeof (EFI_IPv4_ADDRESS)); 746 CopyMem (&Ip4ConfigData->SubnetMask, &Udp4ConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS)); 747 748 // 749 // use the -1 magic number to disable the receiving process of the ip instance. 750 // 751 Ip4ConfigData->ReceiveTimeout = (UINT32) (-1); 752 } 753 754 755 /** 756 This function validates the TxToken, it returns the error code according to the spec. 757 758 @param[in] Instance Pointer to the udp instance context data. 759 @param[in] TxToken Pointer to the token to be checked. 760 761 @retval EFI_SUCCESS The TxToken is valid. 762 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is 763 NULL. Token is NULL. Token.Event is NULL. 764 Token.Packet.TxData is NULL. 765 Token.Packet.TxData.FragmentCount is zero. 766 Token.Packet.TxData.DataLength is not equal to the 767 sum of fragment lengths. One or more of the 768 Token.Packet.TxData.FragmentTable[]. 769 FragmentLength fields is zero. One or more of the 770 Token.Packet.TxData.FragmentTable[]. 771 FragmentBuffer fields is NULL. 772 Token.Packet.TxData. GatewayAddress is not a 773 unicast IPv4 address if it is not NULL. One or 774 more IPv4 addresses in Token.Packet.TxData. 775 UdpSessionData are not valid unicast IPv4 776 addresses if the UdpSessionData is not NULL. 777 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP 778 packet size. 779 780 **/ 781 EFI_STATUS 782 Udp4ValidateTxToken ( 783 IN UDP4_INSTANCE_DATA *Instance, 784 IN EFI_UDP4_COMPLETION_TOKEN *TxToken 785 ) 786 { 787 EFI_UDP4_TRANSMIT_DATA *TxData; 788 UINT32 Index; 789 UINT32 TotalLen; 790 EFI_UDP4_CONFIG_DATA *ConfigData; 791 EFI_UDP4_SESSION_DATA *UdpSessionData; 792 IP4_ADDR SourceAddress; 793 IP4_ADDR GatewayAddress; 794 795 if (TxToken->Event == NULL) { 796 return EFI_INVALID_PARAMETER; 797 } 798 799 TxData = TxToken->Packet.TxData; 800 801 if ((TxData == NULL) || (TxData->FragmentCount == 0)) { 802 return EFI_INVALID_PARAMETER; 803 } 804 805 TotalLen = 0; 806 for (Index = 0; Index < TxData->FragmentCount; Index++) { 807 808 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) || 809 (TxData->FragmentTable[Index].FragmentLength == 0)) { 810 // 811 // if the FragmentBuffer is NULL or the FragmentLeng is zero. 812 // 813 return EFI_INVALID_PARAMETER; 814 } 815 816 TotalLen += TxData->FragmentTable[Index].FragmentLength; 817 } 818 819 if (TotalLen != TxData->DataLength) { 820 // 821 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the 822 // DataLength. 823 // 824 return EFI_INVALID_PARAMETER; 825 } 826 827 if (TxData->GatewayAddress != NULL) { 828 CopyMem (&GatewayAddress, TxData->GatewayAddress, sizeof (IP4_ADDR)); 829 830 if (!Instance->ConfigData.UseDefaultAddress && 831 (EFI_NTOHL(Instance->ConfigData.SubnetMask) != 0) && 832 !NetIp4IsUnicast (NTOHL (GatewayAddress), EFI_NTOHL(Instance->ConfigData.SubnetMask))) { 833 // 834 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0. 835 // 836 return EFI_INVALID_PARAMETER; 837 } 838 } 839 840 ConfigData = &Instance->ConfigData; 841 UdpSessionData = TxData->UdpSessionData; 842 843 if (UdpSessionData != NULL) { 844 845 CopyMem (&SourceAddress, &UdpSessionData->SourceAddress, sizeof (IP4_ADDR)); 846 847 if ((SourceAddress != 0) && 848 !Instance->ConfigData.UseDefaultAddress && 849 (EFI_NTOHL(Instance->ConfigData.SubnetMask) != 0) && 850 !NetIp4IsUnicast (HTONL (SourceAddress), EFI_NTOHL(Instance->ConfigData.SubnetMask))) { 851 // 852 // Check whether SourceAddress is a valid IPv4 address in case it's not zero. 853 // The configured station address is used if SourceAddress is zero. 854 // 855 return EFI_INVALID_PARAMETER; 856 } 857 858 if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) { 859 // 860 // Ambiguous, no avalaible DestinationPort for this token. 861 // 862 return EFI_INVALID_PARAMETER; 863 } 864 865 if (EFI_IP4_EQUAL (&UdpSessionData->DestinationAddress, &mZeroIp4Addr)) { 866 // 867 // The DestinationAddress specified in the UdpSessionData is 0. 868 // 869 return EFI_INVALID_PARAMETER; 870 } 871 } else if (EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr)) { 872 // 873 // the configured RemoteAddress is all zero, and the user doens't override the 874 // destination address. 875 // 876 return EFI_INVALID_PARAMETER; 877 } 878 879 if (TxData->DataLength > UDP4_MAX_DATA_SIZE) { 880 return EFI_BAD_BUFFER_SIZE; 881 } 882 883 return EFI_SUCCESS; 884 } 885 886 887 /** 888 This function checks whether the specified Token duplicates with the one in the Map. 889 890 @param[in] Map Pointer to the NET_MAP. 891 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to 892 the Token. 893 @param[in] Context Pointer to the Token to be checked. 894 895 @retval EFI_SUCCESS The Token specified by Context differs from the 896 one in the Item. 897 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item. 898 899 **/ 900 EFI_STATUS 901 EFIAPI 902 Udp4TokenExist ( 903 IN NET_MAP *Map, 904 IN NET_MAP_ITEM *Item, 905 IN VOID *Context 906 ) 907 { 908 EFI_UDP4_COMPLETION_TOKEN *Token; 909 EFI_UDP4_COMPLETION_TOKEN *TokenInItem; 910 911 Token = (EFI_UDP4_COMPLETION_TOKEN*) Context; 912 TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key; 913 914 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) { 915 // 916 // The Token duplicates with the TokenInItem in case either the two pointers are the 917 // same or the Events of these two tokens are the same. 918 // 919 return EFI_ACCESS_DENIED; 920 } 921 922 return EFI_SUCCESS; 923 } 924 925 926 /** 927 This function calculates the checksum for the Packet, utilizing the pre-calculated 928 pseudo HeadSum to reduce some overhead. 929 930 @param[in] Packet Pointer to the NET_BUF contains the udp datagram. 931 @param[in] HeadSum Checksum of the pseudo header execpt the length 932 field. 933 934 @retval The 16-bit checksum of this udp datagram. 935 936 **/ 937 UINT16 938 Udp4Checksum ( 939 IN NET_BUF *Packet, 940 IN UINT16 HeadSum 941 ) 942 { 943 UINT16 Checksum; 944 945 Checksum = NetbufChecksum (Packet); 946 Checksum = NetAddChecksum (Checksum, HeadSum); 947 948 Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize)); 949 950 return (UINT16) ~Checksum; 951 } 952 953 954 /** 955 This function removes the specified Token from the TokenMap. 956 957 @param[in, out] TokenMap Pointer to the NET_MAP containing the tokens. 958 @param[in] Token Pointer to the Token to be removed. 959 960 @retval EFI_SUCCESS The specified Token is removed from the TokenMap. 961 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap. 962 963 **/ 964 EFI_STATUS 965 Udp4RemoveToken ( 966 IN OUT NET_MAP *TokenMap, 967 IN EFI_UDP4_COMPLETION_TOKEN *Token 968 ) 969 { 970 NET_MAP_ITEM *Item; 971 972 // 973 // Find the Token first. 974 // 975 Item = NetMapFindKey (TokenMap, (VOID *) Token); 976 977 if (Item != NULL) { 978 // 979 // Remove the token if it's found in the map. 980 // 981 NetMapRemoveItem (TokenMap, Item, NULL); 982 983 return EFI_SUCCESS; 984 } 985 986 return EFI_NOT_FOUND; 987 } 988 989 990 /** 991 This function is the packet transmitting notify function registered to the IpIo 992 interface. It's called to signal the udp TxToken when IpIo layer completes the 993 transmitting of the udp datagram. 994 995 @param[in] Status The completion status of the output udp datagram. 996 @param[in] Context Pointer to the context data. 997 @param[in] Sender Specify a pointer of EFI_IP4_PROTOCOL for sending. 998 @param[in] NotifyData Pointer to the notify data. 999 1000 **/ 1001 VOID 1002 EFIAPI 1003 Udp4DgramSent ( 1004 IN EFI_STATUS Status, 1005 IN VOID *Context, 1006 IN IP_IO_IP_PROTOCOL Sender, 1007 IN VOID *NotifyData 1008 ) 1009 { 1010 UDP4_INSTANCE_DATA *Instance; 1011 EFI_UDP4_COMPLETION_TOKEN *Token; 1012 1013 Instance = (UDP4_INSTANCE_DATA *) Context; 1014 Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData; 1015 1016 if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) { 1017 // 1018 // The token may be cancelled. Only signal it if the remove operation succeeds. 1019 // 1020 Token->Status = Status; 1021 gBS->SignalEvent (Token->Event); 1022 DispatchDpc (); 1023 } 1024 } 1025 1026 1027 /** 1028 This function processes the received datagram passed up by the IpIo layer. 1029 1030 @param[in] Status The status of this udp datagram. 1031 @param[in] IcmpError The IcmpError code, only available when Status is 1032 EFI_ICMP_ERROR. 1033 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA. 1034 @param[in] Packet Pointer to the NET_BUF containing the received udp 1035 datagram. 1036 @param[in] Context Pointer to the context data. 1037 1038 **/ 1039 VOID 1040 EFIAPI 1041 Udp4DgramRcvd ( 1042 IN EFI_STATUS Status, 1043 IN UINT8 IcmpError, 1044 IN EFI_NET_SESSION_DATA *NetSession, 1045 IN NET_BUF *Packet, 1046 IN VOID *Context 1047 ) 1048 { 1049 NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE); 1050 1051 // 1052 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR. 1053 // 1054 if (Status == EFI_SUCCESS) { 1055 // 1056 // Demultiplex the received datagram. 1057 // 1058 Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet); 1059 } else { 1060 // 1061 // Handle the ICMP_ERROR packet. 1062 // 1063 Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet); 1064 } 1065 1066 // 1067 // Dispatch the DPC queued by the NotifyFunction of the rx token's events 1068 // which are signaled with received data. 1069 // 1070 DispatchDpc (); 1071 } 1072 1073 1074 /** 1075 This function removes the multicast group specified by Arg from the Map. 1076 1077 @param[in, out] Map Pointer to the NET_MAP. 1078 @param[in] Item Pointer to the NET_MAP_ITEM. 1079 @param[in] Arg Pointer to the Arg, it's the pointer to a 1080 multicast IPv4 Address. 1081 1082 @retval EFI_SUCCESS The multicast address is removed. 1083 @retval EFI_ABORTED The specified multicast address is removed and the 1084 Arg is not NULL. 1085 1086 **/ 1087 EFI_STATUS 1088 EFIAPI 1089 Udp4LeaveGroup ( 1090 IN OUT NET_MAP *Map, 1091 IN NET_MAP_ITEM *Item, 1092 IN VOID *Arg OPTIONAL 1093 ) 1094 { 1095 EFI_IPv4_ADDRESS *McastIp; 1096 1097 McastIp = Arg; 1098 1099 if ((McastIp != NULL) && (!EFI_IP4_EQUAL (McastIp, &(Item->Key)))) { 1100 // 1101 // McastIp is not NULL and the multicast address contained in the Item 1102 // is not the same as McastIp. 1103 // 1104 return EFI_SUCCESS; 1105 } 1106 1107 // 1108 // Remove this Item. 1109 // 1110 NetMapRemoveItem (Map, Item, NULL); 1111 1112 if (McastIp != NULL) { 1113 // 1114 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration. 1115 // 1116 return EFI_ABORTED; 1117 } 1118 1119 return EFI_SUCCESS; 1120 } 1121 1122 1123 /** 1124 This function cancels the token specified by Arg in the Map. This is a callback 1125 used by Udp4InstanceCancelToken(). 1126 1127 @param[in] Map Pointer to the NET_MAP. 1128 @param[in] Item Pointer to the NET_MAP_ITEM. 1129 @param[in] Arg Pointer to the token to be cancelled, if NULL, 1130 the token specified by Item is cancelled. 1131 1132 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token 1133 is not the same as that in the Item if Arg is not 1134 NULL. 1135 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is 1136 cancelled. 1137 1138 **/ 1139 EFI_STATUS 1140 EFIAPI 1141 Udp4CancelTokens ( 1142 IN NET_MAP *Map, 1143 IN NET_MAP_ITEM *Item, 1144 IN VOID *Arg OPTIONAL 1145 ) 1146 { 1147 EFI_UDP4_COMPLETION_TOKEN *TokenToCancel; 1148 NET_BUF *Packet; 1149 IP_IO *IpIo; 1150 1151 if ((Arg != NULL) && (Item->Key != Arg)) { 1152 return EFI_SUCCESS; 1153 } 1154 1155 if (Item->Value != NULL) { 1156 // 1157 // If the token is a transmit token, the corresponding Packet is recorded in 1158 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken 1159 // will invoke Udp4DgramSent, the token will be signaled and this Item will 1160 // be removed from the Map there. 1161 // 1162 Packet = (NET_BUF *) (Item->Value); 1163 IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0])); 1164 1165 IpIoCancelTxToken (IpIo, Packet); 1166 } else { 1167 // 1168 // The token is a receive token. Abort it and remove it from the Map. 1169 // 1170 TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key; 1171 NetMapRemoveItem (Map, Item, NULL); 1172 1173 TokenToCancel->Status = EFI_ABORTED; 1174 gBS->SignalEvent (TokenToCancel->Event); 1175 } 1176 1177 if (Arg != NULL) { 1178 return EFI_ABORTED; 1179 } 1180 1181 return EFI_SUCCESS; 1182 } 1183 1184 1185 /** 1186 This function removes all the Wrap datas in the RcvdDgramQue. 1187 1188 @param[in] Instance Pointer to the udp instance context data. 1189 1190 **/ 1191 VOID 1192 Udp4FlushRcvdDgram ( 1193 IN UDP4_INSTANCE_DATA *Instance 1194 ) 1195 { 1196 UDP4_RXDATA_WRAP *Wrap; 1197 1198 while (!IsListEmpty (&Instance->RcvdDgramQue)) { 1199 // 1200 // Iterate all the Wraps in the RcvdDgramQue. 1201 // 1202 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link); 1203 1204 // 1205 // The Wrap will be removed from the RcvdDgramQue by this function call. 1206 // 1207 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap); 1208 } 1209 } 1210 1211 1212 1213 /** 1214 Cancel Udp4 tokens from the Udp4 instance. 1215 1216 @param[in] Instance Pointer to the udp instance context data. 1217 @param[in] Token Pointer to the token to be canceled, if NULL, all 1218 tokens in this instance will be cancelled. 1219 1220 @retval EFI_SUCCESS The Token is cancelled. 1221 @retval EFI_NOT_FOUND The Token is not found. 1222 1223 **/ 1224 EFI_STATUS 1225 Udp4InstanceCancelToken ( 1226 IN UDP4_INSTANCE_DATA *Instance, 1227 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL 1228 ) 1229 { 1230 EFI_STATUS Status; 1231 1232 // 1233 // Cancel this token from the TxTokens map. 1234 // 1235 Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token); 1236 1237 if ((Token != NULL) && (Status == EFI_ABORTED)) { 1238 // 1239 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from 1240 // the TxTokens, just return success. 1241 // 1242 return EFI_SUCCESS; 1243 } 1244 1245 // 1246 // Try to cancel this token from the RxTokens map in condition either the Token 1247 // is NULL or the specified Token is not in TxTokens. 1248 // 1249 Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token); 1250 1251 if ((Token != NULL) && (Status == EFI_SUCCESS)) { 1252 // 1253 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the 1254 // TxTokens nor the RxTokens, or say, it's not found. 1255 // 1256 return EFI_NOT_FOUND; 1257 } 1258 1259 ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens)) 1260 && (0 == NetMapGetCount (&Instance->RxTokens)))); 1261 1262 return EFI_SUCCESS; 1263 } 1264 1265 1266 /** 1267 This function matches the received udp datagram with the Instance. 1268 1269 @param[in] Instance Pointer to the udp instance context data. 1270 @param[in] Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted 1271 from the received udp datagram. 1272 1273 @retval TRUE The udp datagram matches the receiving requirments of the 1274 udp Instance. 1275 @retval FALSE Otherwise. 1276 1277 **/ 1278 BOOLEAN 1279 Udp4MatchDgram ( 1280 IN UDP4_INSTANCE_DATA *Instance, 1281 IN EFI_UDP4_SESSION_DATA *Udp4Session 1282 ) 1283 { 1284 EFI_UDP4_CONFIG_DATA *ConfigData; 1285 IP4_ADDR Destination; 1286 1287 ConfigData = &Instance->ConfigData; 1288 1289 if (ConfigData->AcceptPromiscuous) { 1290 // 1291 // Always matches if this instance is in the promiscuous state. 1292 // 1293 return TRUE; 1294 } 1295 1296 if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) || 1297 ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort)) 1298 ) { 1299 // 1300 // The local port or the remote port doesn't match. 1301 // 1302 return FALSE; 1303 } 1304 1305 if (!EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr) && 1306 !EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &Udp4Session->SourceAddress) 1307 ) { 1308 // 1309 // This datagram doesn't come from the instance's specified sender. 1310 // 1311 return FALSE; 1312 } 1313 1314 if (EFI_IP4_EQUAL (&ConfigData->StationAddress, &mZeroIp4Addr) || 1315 EFI_IP4_EQUAL (&Udp4Session->DestinationAddress, &ConfigData->StationAddress) 1316 ) { 1317 // 1318 // The instance is configured to receive datagrams destined to any station IP or 1319 // the destination address of this datagram matches the configured station IP. 1320 // 1321 return TRUE; 1322 } 1323 1324 CopyMem (&Destination, &Udp4Session->DestinationAddress, sizeof (IP4_ADDR)); 1325 1326 if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) { 1327 // 1328 // The instance is configured to receive broadcast and this is a broadcast packet. 1329 // 1330 return TRUE; 1331 } 1332 1333 if (IP4_IS_MULTICAST (NTOHL (Destination)) && 1334 NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination) != NULL 1335 ) { 1336 // 1337 // It's a multicast packet and the multicast address is accepted by this instance. 1338 // 1339 return TRUE; 1340 } 1341 1342 return FALSE; 1343 } 1344 1345 1346 /** 1347 This function removes the Wrap specified by Context and release relevant resources. 1348 1349 @param[in] Event The Event this notify function registered to. 1350 @param[in] Context Pointer to the context data. 1351 1352 **/ 1353 VOID 1354 EFIAPI 1355 Udp4RecycleRxDataWrap ( 1356 IN EFI_EVENT Event, 1357 IN VOID *Context 1358 ) 1359 { 1360 UDP4_RXDATA_WRAP *Wrap; 1361 1362 Wrap = (UDP4_RXDATA_WRAP *) Context; 1363 1364 // 1365 // Remove the Wrap from the list it belongs to. 1366 // 1367 RemoveEntryList (&Wrap->Link); 1368 1369 // 1370 // Free the Packet associated with this Wrap. 1371 // 1372 NetbufFree (Wrap->Packet); 1373 1374 // 1375 // Close the event. 1376 // 1377 gBS->CloseEvent (Wrap->RxData.RecycleSignal); 1378 1379 FreePool (Wrap); 1380 } 1381 1382 1383 /** 1384 This function wraps the Packet and the RxData. 1385 1386 @param[in] Instance Pointer to the instance context data. 1387 @param[in] Packet Pointer to the buffer containing the received 1388 datagram. 1389 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this 1390 datagram. 1391 1392 @return Pointer to the structure wrapping the RxData and the Packet. 1393 1394 **/ 1395 UDP4_RXDATA_WRAP * 1396 Udp4WrapRxData ( 1397 IN UDP4_INSTANCE_DATA *Instance, 1398 IN NET_BUF *Packet, 1399 IN EFI_UDP4_RECEIVE_DATA *RxData 1400 ) 1401 { 1402 EFI_STATUS Status; 1403 UDP4_RXDATA_WRAP *Wrap; 1404 1405 // 1406 // Allocate buffer for the Wrap. 1407 // 1408 Wrap = AllocatePool (sizeof (UDP4_RXDATA_WRAP) + 1409 (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA)); 1410 if (Wrap == NULL) { 1411 return NULL; 1412 } 1413 1414 InitializeListHead (&Wrap->Link); 1415 1416 CopyMem (&Wrap->RxData, RxData, sizeof (Wrap->RxData)); 1417 1418 // 1419 // Create the Recycle event. 1420 // 1421 Status = gBS->CreateEvent ( 1422 EVT_NOTIFY_SIGNAL, 1423 TPL_NOTIFY, 1424 Udp4RecycleRxDataWrap, 1425 Wrap, 1426 &Wrap->RxData.RecycleSignal 1427 ); 1428 if (EFI_ERROR (Status)) { 1429 FreePool (Wrap); 1430 return NULL; 1431 } 1432 1433 Wrap->Packet = Packet; 1434 Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout; 1435 1436 return Wrap; 1437 } 1438 1439 1440 /** 1441 This function enqueues the received datagram into the instances' receiving queues. 1442 1443 @param[in] Udp4Service Pointer to the udp service context data. 1444 @param[in] Packet Pointer to the buffer containing the received 1445 datagram. 1446 @param[in] RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this 1447 datagram. 1448 1449 @return The times this datagram is enqueued. 1450 1451 **/ 1452 UINTN 1453 Udp4EnqueueDgram ( 1454 IN UDP4_SERVICE_DATA *Udp4Service, 1455 IN NET_BUF *Packet, 1456 IN EFI_UDP4_RECEIVE_DATA *RxData 1457 ) 1458 { 1459 LIST_ENTRY *Entry; 1460 UDP4_INSTANCE_DATA *Instance; 1461 UDP4_RXDATA_WRAP *Wrap; 1462 UINTN Enqueued; 1463 1464 Enqueued = 0; 1465 1466 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { 1467 // 1468 // Iterate the instances. 1469 // 1470 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); 1471 1472 if (!Instance->Configured) { 1473 continue; 1474 } 1475 1476 if (Udp4MatchDgram (Instance, &RxData->UdpSession)) { 1477 // 1478 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue. 1479 // 1480 Wrap = Udp4WrapRxData (Instance, Packet, RxData); 1481 if (Wrap == NULL) { 1482 continue; 1483 } 1484 1485 NET_GET_REF (Packet); 1486 1487 InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link); 1488 1489 Enqueued++; 1490 } 1491 } 1492 1493 return Enqueued; 1494 } 1495 1496 1497 /** 1498 This function delivers the received datagrams for the specified instance. 1499 1500 @param[in] Instance Pointer to the instance context data. 1501 1502 **/ 1503 VOID 1504 Udp4InstanceDeliverDgram ( 1505 IN UDP4_INSTANCE_DATA *Instance 1506 ) 1507 { 1508 UDP4_RXDATA_WRAP *Wrap; 1509 EFI_UDP4_COMPLETION_TOKEN *Token; 1510 NET_BUF *Dup; 1511 EFI_UDP4_RECEIVE_DATA *RxData; 1512 EFI_TPL OldTpl; 1513 1514 if (!IsListEmpty (&Instance->RcvdDgramQue) && 1515 !NetMapIsEmpty (&Instance->RxTokens)) { 1516 1517 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link); 1518 1519 if (NET_BUF_SHARED (Wrap->Packet)) { 1520 // 1521 // Duplicate the Packet if it is shared between instances. 1522 // 1523 Dup = NetbufDuplicate (Wrap->Packet, NULL, 0); 1524 if (Dup == NULL) { 1525 return; 1526 } 1527 1528 NetbufFree (Wrap->Packet); 1529 1530 Wrap->Packet = Dup; 1531 } 1532 1533 NetListRemoveHead (&Instance->RcvdDgramQue); 1534 1535 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); 1536 1537 // 1538 // Build the FragmentTable and set the FragmentCount in RxData. 1539 // 1540 RxData = &Wrap->RxData; 1541 RxData->FragmentCount = Wrap->Packet->BlockOpNum; 1542 1543 NetbufBuildExt ( 1544 Wrap->Packet, 1545 (NET_FRAGMENT *) RxData->FragmentTable, 1546 &RxData->FragmentCount 1547 ); 1548 1549 Token->Status = EFI_SUCCESS; 1550 Token->Packet.RxData = &Wrap->RxData; 1551 1552 OldTpl = gBS->RaiseTPL (TPL_NOTIFY); 1553 InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link); 1554 gBS->RestoreTPL (OldTpl); 1555 1556 gBS->SignalEvent (Token->Event); 1557 } 1558 } 1559 1560 1561 /** 1562 This function delivers the datagrams enqueued in the instances. 1563 1564 @param[in] Udp4Service Pointer to the udp service context data. 1565 1566 **/ 1567 VOID 1568 Udp4DeliverDgram ( 1569 IN UDP4_SERVICE_DATA *Udp4Service 1570 ) 1571 { 1572 LIST_ENTRY *Entry; 1573 UDP4_INSTANCE_DATA *Instance; 1574 1575 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { 1576 // 1577 // Iterate the instances. 1578 // 1579 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); 1580 1581 if (!Instance->Configured) { 1582 continue; 1583 } 1584 1585 // 1586 // Deliver the datagrams of this instance. 1587 // 1588 Udp4InstanceDeliverDgram (Instance); 1589 } 1590 } 1591 1592 1593 /** 1594 This function demultiplexes the received udp datagram to the appropriate instances. 1595 1596 @param[in] Udp4Service Pointer to the udp service context data. 1597 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from 1598 the received datagram. 1599 @param[in] Packet Pointer to the buffer containing the received udp 1600 datagram. 1601 1602 **/ 1603 VOID 1604 Udp4Demultiplex ( 1605 IN UDP4_SERVICE_DATA *Udp4Service, 1606 IN EFI_NET_SESSION_DATA *NetSession, 1607 IN NET_BUF *Packet 1608 ) 1609 { 1610 EFI_UDP_HEADER *Udp4Header; 1611 UINT16 HeadSum; 1612 EFI_UDP4_RECEIVE_DATA RxData; 1613 EFI_UDP4_SESSION_DATA *Udp4Session; 1614 UINTN Enqueued; 1615 1616 if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) { 1617 NetbufFree (Packet); 1618 return; 1619 } 1620 1621 // 1622 // Get the datagram header from the packet buffer. 1623 // 1624 Udp4Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); 1625 ASSERT (Udp4Header != NULL); 1626 1627 if (Udp4Header->Checksum != 0) { 1628 // 1629 // check the checksum. 1630 // 1631 HeadSum = NetPseudoHeadChecksum ( 1632 NetSession->Source.Addr[0], 1633 NetSession->Dest.Addr[0], 1634 EFI_IP_PROTO_UDP, 1635 0 1636 ); 1637 1638 if (Udp4Checksum (Packet, HeadSum) != 0) { 1639 // 1640 // Wrong checksum. 1641 // 1642 NetbufFree (Packet); 1643 return; 1644 } 1645 } 1646 1647 Udp4Session = &RxData.UdpSession; 1648 Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort); 1649 Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort); 1650 1651 CopyMem (&Udp4Session->SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS)); 1652 CopyMem (&Udp4Session->DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); 1653 1654 // 1655 // Trim the UDP header. 1656 // 1657 NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE); 1658 1659 RxData.DataLength = (UINT32) Packet->TotalSize; 1660 1661 // 1662 // Try to enqueue this datagram into the instances. 1663 // 1664 Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData); 1665 1666 if (Enqueued == 0) { 1667 // 1668 // Send the port unreachable ICMP packet before we free this NET_BUF 1669 // 1670 Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header); 1671 } 1672 1673 // 1674 // Try to free the packet before deliver it. 1675 // 1676 NetbufFree (Packet); 1677 1678 if (Enqueued > 0) { 1679 // 1680 // Deliver the datagram. 1681 // 1682 Udp4DeliverDgram (Udp4Service); 1683 } 1684 } 1685 1686 1687 /** 1688 This function builds and sends out a icmp port unreachable message. 1689 1690 @param[in] IpIo Pointer to the IP_IO instance. 1691 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet 1692 causes this icmp error message. 1693 @param[in] Udp4Header Pointer to the udp header of the datagram causes 1694 this icmp error message. 1695 1696 **/ 1697 VOID 1698 Udp4SendPortUnreach ( 1699 IN IP_IO *IpIo, 1700 IN EFI_NET_SESSION_DATA *NetSession, 1701 IN VOID *Udp4Header 1702 ) 1703 { 1704 NET_BUF *Packet; 1705 UINT32 Len; 1706 IP4_ICMP_ERROR_HEAD *IcmpErrHdr; 1707 EFI_IP4_HEADER *IpHdr; 1708 UINT8 *Ptr; 1709 IP_IO_OVERRIDE Override; 1710 IP_IO_IP_INFO *IpSender; 1711 1712 IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest); 1713 if (IpSender == NULL) { 1714 // 1715 // No appropriate sender, since we cannot send out the ICMP message through 1716 // the default zero station address IP instance, abort. 1717 // 1718 return; 1719 } 1720 1721 IpHdr = NetSession->IpHdr.Ip4Hdr; 1722 1723 // 1724 // Calculate the required length of the icmp error message. 1725 // 1726 Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) - 1727 sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH; 1728 1729 // 1730 // Allocate buffer for the icmp error message. 1731 // 1732 Packet = NetbufAlloc (Len); 1733 if (Packet == NULL) { 1734 return; 1735 } 1736 1737 // 1738 // Allocate space for the IP4_ICMP_ERROR_HEAD. 1739 // 1740 IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE); 1741 ASSERT (IcmpErrHdr != NULL); 1742 1743 // 1744 // Set the required fields for the icmp port unreachable message. 1745 // 1746 IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH; 1747 IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT; 1748 IcmpErrHdr->Head.Checksum = 0; 1749 IcmpErrHdr->Fourth = 0; 1750 1751 // 1752 // Copy the IP header of the datagram tragged the error. 1753 // 1754 CopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr)); 1755 1756 // 1757 // Copy the UDP header. 1758 // 1759 Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr); 1760 CopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH); 1761 1762 // 1763 // Calculate the checksum. 1764 // 1765 IcmpErrHdr->Head.Checksum = (UINT16) ~(NetbufChecksum (Packet)); 1766 1767 // 1768 // Fill the override data. 1769 // 1770 Override.Ip4OverrideData.DoNotFragment = FALSE; 1771 Override.Ip4OverrideData.TypeOfService = 0; 1772 Override.Ip4OverrideData.TimeToLive = 255; 1773 Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_ICMP; 1774 1775 CopyMem (&Override.Ip4OverrideData.SourceAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); 1776 ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS)); 1777 1778 // 1779 // Send out this icmp packet. 1780 // 1781 IpIoSend (IpIo, Packet, IpSender, NULL, NULL, &NetSession->Source, &Override); 1782 1783 NetbufFree (Packet); 1784 } 1785 1786 1787 /** 1788 This function handles the received Icmp Error message and demultiplexes it to the 1789 instance. 1790 1791 @param[in] Udp4Service Pointer to the udp service context data. 1792 @param[in] IcmpError The icmp error code. 1793 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted 1794 from the received Icmp Error packet. 1795 @param[in] Packet Pointer to the Icmp Error packet. 1796 1797 **/ 1798 VOID 1799 Udp4IcmpHandler ( 1800 IN UDP4_SERVICE_DATA *Udp4Service, 1801 IN UINT8 IcmpError, 1802 IN EFI_NET_SESSION_DATA *NetSession, 1803 IN NET_BUF *Packet 1804 ) 1805 { 1806 EFI_UDP_HEADER *Udp4Header; 1807 EFI_UDP4_SESSION_DATA Udp4Session; 1808 LIST_ENTRY *Entry; 1809 UDP4_INSTANCE_DATA *Instance; 1810 1811 if (Packet->TotalSize < sizeof (EFI_UDP_HEADER)) { 1812 NetbufFree (Packet); 1813 return; 1814 } 1815 1816 Udp4Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL); 1817 ASSERT (Udp4Header != NULL); 1818 1819 CopyMem (&Udp4Session.SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS)); 1820 CopyMem (&Udp4Session.DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS)); 1821 1822 Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort); 1823 Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort); 1824 1825 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) { 1826 // 1827 // Iterate all the instances. 1828 // 1829 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link); 1830 1831 if (!Instance->Configured) { 1832 continue; 1833 } 1834 1835 if (Udp4MatchDgram (Instance, &Udp4Session)) { 1836 // 1837 // Translate the Icmp Error code according to the udp spec. 1838 // 1839 Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, IP_VERSION_4, NULL, NULL); 1840 1841 if (IcmpError > ICMP_ERR_UNREACH_PORT) { 1842 Instance->IcmpError = EFI_ICMP_ERROR; 1843 } 1844 1845 // 1846 // Notify the instance with the received Icmp Error. 1847 // 1848 Udp4ReportIcmpError (Instance); 1849 1850 break; 1851 } 1852 } 1853 1854 NetbufFree (Packet); 1855 } 1856 1857 1858 /** 1859 This function reports the received ICMP error. 1860 1861 @param[in] Instance Pointer to the udp instance context data. 1862 1863 **/ 1864 VOID 1865 Udp4ReportIcmpError ( 1866 IN UDP4_INSTANCE_DATA *Instance 1867 ) 1868 { 1869 EFI_UDP4_COMPLETION_TOKEN *Token; 1870 1871 if (NetMapIsEmpty (&Instance->RxTokens)) { 1872 // 1873 // There are no receive tokens to deliver the ICMP error. 1874 // 1875 return; 1876 } 1877 1878 if (EFI_ERROR (Instance->IcmpError)) { 1879 // 1880 // Try to get a RxToken from the RxTokens map. 1881 // 1882 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL); 1883 1884 if (Token != NULL) { 1885 // 1886 // Report the error through the Token. 1887 // 1888 Token->Status = Instance->IcmpError; 1889 gBS->SignalEvent (Token->Event); 1890 1891 // 1892 // Clear the IcmpError. 1893 // 1894 Instance->IcmpError = EFI_SUCCESS; 1895 } 1896 } 1897 } 1898 1899 1900 /** 1901 This function is a dummy ext-free function for the NET_BUF created for the output 1902 udp datagram. 1903 1904 @param[in] Context Pointer to the context data. 1905 1906 **/ 1907 VOID 1908 EFIAPI 1909 Udp4NetVectorExtFree ( 1910 VOID *Context 1911 ) 1912 { 1913 } 1914 1915