summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Library/DxeIpIoLib/DxeIpIoLib.c
blob: 5aaef6d743f55e1117587622f96187a008ce8f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
/** @file
  IpIo Library.

Copyright (c) 2005 - 2009, Intel Corporation.<BR>
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <Uefi.h>

#include <Protocol/Udp4.h>

#include <Library/IpIoLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DpcLib.h>


LIST_ENTRY  mActiveIpIoList = {
  &mActiveIpIoList,
  &mActiveIpIoList
};

EFI_IP4_CONFIG_DATA  mIp4IoDefaultIpConfigData = {
  EFI_IP_PROTO_UDP,
  FALSE,
  TRUE,
  FALSE,
  FALSE,
  FALSE,
  {{0, 0, 0, 0}},
  {{0, 0, 0, 0}},
  0,
  255,
  FALSE,
  FALSE,
  0,
  0
};

EFI_IP6_CONFIG_DATA  mIp6IoDefaultIpConfigData = {
  EFI_IP_PROTO_UDP,
  FALSE,
  TRUE,
  FALSE,
  {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  0,
  255,
  0,
  0,
  0
};

ICMP_ERROR_INFO  mIcmpErrMap[10] = {
  {FALSE, TRUE }, // ICMP_ERR_UNREACH_NET
  {FALSE, TRUE }, // ICMP_ERR_UNREACH_HOST
  {TRUE,  TRUE }, // ICMP_ERR_UNREACH_PROTOCOL
  {TRUE,  TRUE }, // ICMP_ERR_UNREACH_PORT
  {TRUE,  TRUE }, // ICMP_ERR_MSGSIZE
  {FALSE, TRUE }, // ICMP_ERR_UNREACH_SRCFAIL
  {FALSE, TRUE }, // ICMP_ERR_TIMXCEED_INTRANS
  {FALSE, TRUE }, // ICMP_ERR_TIMEXCEED_REASS
  {FALSE, FALSE}, // ICMP_ERR_QUENCH
  {FALSE, TRUE }  // ICMP_ERR_PARAMPROB
};

ICMP_ERROR_INFO  mIcmp6ErrMap[10] = {
  {FALSE, TRUE}, // ICMP6_ERR_UNREACH_NET
  {FALSE, TRUE}, // ICMP6_ERR_UNREACH_HOST
  {TRUE,  TRUE}, // ICMP6_ERR_UNREACH_PROTOCOL
  {TRUE,  TRUE}, // ICMP6_ERR_UNREACH_PORT    
  {TRUE,  TRUE}, // ICMP6_ERR_PACKAGE_TOOBIG
  {FALSE, TRUE}, // ICMP6_ERR_TIMXCEED_HOPLIMIT
  {FALSE, TRUE}, // ICMP6_ERR_TIMXCEED_REASS
  {FALSE, TRUE}, // ICMP6_ERR_PARAMPROB_HEADER    
  {FALSE, TRUE}, // ICMP6_ERR_PARAMPROB_NEXHEADER
  {FALSE, TRUE}  // ICMP6_ERR_PARAMPROB_IPV6OPTION
};


/**
  Notify function for IP transmit token.

  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoTransmitHandlerDpc (
  IN VOID      *Context
  );


/**
  Notify function for IP transmit token.

  @param[in]  Event                 The event signaled.
  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoTransmitHandler (
  IN EFI_EVENT Event,
  IN VOID      *Context
  );


/**
  This function create an IP child ,open the IP protocol, and return the opened
  IP protocol as Interface.

  @param[in]    ControllerHandle   The controller handle.
  @param[in]    ImageHandle        The image handle.
  @param[in]    ChildHandle        Pointer to the buffer to save the IP child handle.
  @param[in]    IpVersion          The version of the IP protocol to use, either
                                   IPv4 or IPv6.
  @param[out]   Interface          Pointer used to get the IP protocol interface.

  @retval       EFI_SUCCESS        The IP child is created and the IP protocol
                                   interface is retrieved.
  @retval       Others             The required operation failed.

**/
EFI_STATUS
IpIoCreateIpChildOpenProtocol (
  IN  EFI_HANDLE  ControllerHandle,
  IN  EFI_HANDLE  ImageHandle,
  IN  EFI_HANDLE  *ChildHandle,
  IN  IP_VERSION  IpVersion,  
  OUT VOID        **Interface
  )
{
  EFI_STATUS  Status;
  EFI_GUID    *ServiceBindingGuid;
  EFI_GUID    *IpProtocolGuid;

  if (IpVersion == IP_VERSION_4) {
    ServiceBindingGuid = &gEfiIp4ServiceBindingProtocolGuid;
    IpProtocolGuid     = &gEfiIp4ProtocolGuid;
  } else if (IpVersion == IP_VERSION_6){
    ServiceBindingGuid = &gEfiIp6ServiceBindingProtocolGuid;
    IpProtocolGuid     = &gEfiIp6ProtocolGuid;
  } else {
    return EFI_UNSUPPORTED;
  }

  //
  // Create an IP child.
  //
  Status = NetLibCreateServiceChild (
             ControllerHandle,
             ImageHandle,
             ServiceBindingGuid,
             ChildHandle
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Open the IP protocol installed on the *ChildHandle.
  //
  Status = gBS->OpenProtocol (
                  *ChildHandle,
                  IpProtocolGuid,
                  Interface,
                  ImageHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    //
    // On failure, destroy the IP child.
    //
    NetLibDestroyServiceChild (
      ControllerHandle,
      ImageHandle,
      ServiceBindingGuid,
      *ChildHandle
      );
  }

  return Status;
}


/**
  This function close the previously openned IP protocol and destroy the IP child.

  @param[in]  ControllerHandle    The controller handle.
  @param[in]  ImageHandle         The image handle.
  @param[in]  ChildHandle         The child handle of the IP child.
  @param[in]  IpVersion           The version of the IP protocol to use, either
                                  IPv4 or IPv6.

  @retval     EFI_SUCCESS         The IP protocol is closed and the relevant IP child
                                  is destroyed.
  @retval     Others              The required operation failed.

**/
EFI_STATUS
IpIoCloseProtocolDestroyIpChild (
  IN EFI_HANDLE  ControllerHandle,
  IN EFI_HANDLE  ImageHandle,
  IN EFI_HANDLE  ChildHandle,
  IN IP_VERSION  IpVersion
  )
{
  EFI_STATUS  Status;
  EFI_GUID    *ServiceBindingGuid;
  EFI_GUID    *IpProtocolGuid;

  if (IpVersion == IP_VERSION_4) {
    ServiceBindingGuid = &gEfiIp4ServiceBindingProtocolGuid;
    IpProtocolGuid     = &gEfiIp4ProtocolGuid;
  } else if (IpVersion == IP_VERSION_6) {
    ServiceBindingGuid = &gEfiIp6ServiceBindingProtocolGuid;
    IpProtocolGuid     = &gEfiIp6ProtocolGuid;
  } else {
    return EFI_UNSUPPORTED;
  }

  //
  // Close the previously openned IP protocol.
  //
  gBS->CloseProtocol (
         ChildHandle,
         IpProtocolGuid,
         ImageHandle,
         ControllerHandle
         );

  //
  // Destroy the IP child.
  //
  Status = NetLibDestroyServiceChild (
             ControllerHandle,
             ImageHandle,
             ServiceBindingGuid,
             ChildHandle
             );

  return Status;
}

/**
  This function handles ICMPv4 packets. It is the worker function of 
  IpIoIcmpHandler.

  @param[in]       IpIo            Pointer to the IP_IO instance.
  @param[in, out]  Pkt             Pointer to the ICMPv4 packet.
  @param[in]       Session         Pointer to the net session of this ICMPv4 packet.

  @retval          EFI_SUCCESS     The ICMPv4 packet is handled successfully.
  @retval          EFI_ABORTED     This type of ICMPv4 packet is not supported.

**/
EFI_STATUS
IpIoIcmpv4Handler (
  IN     IP_IO                *IpIo,
  IN OUT NET_BUF              *Pkt,
  IN     EFI_NET_SESSION_DATA *Session
  )
{
  IP4_ICMP_ERROR_HEAD  *IcmpHdr;
  EFI_IP4_HEADER       *IpHdr;
  ICMP_ERROR           IcmpErr;
  UINT8                *PayLoadHdr;
  UINT8                Type;
  UINT8                Code;
  UINT32               TrimBytes;

  ASSERT (IpIo->IpVersion == IP_VERSION_4);

  IcmpHdr = NET_PROTO_HDR (Pkt, IP4_ICMP_ERROR_HEAD);
  IpHdr   = (EFI_IP4_HEADER *) (&IcmpHdr->IpHead);

  //
  // Check the ICMP packet length.
  //
  if (Pkt->TotalSize < ICMP_ERRLEN (IpHdr)) {

    return EFI_ABORTED;
  }

  Type = IcmpHdr->Head.Type;
  Code = IcmpHdr->Head.Code;

  //
  // Analyze the ICMP Error in this ICMP pkt
  //
  switch (Type) {
  case ICMP_TYPE_UNREACH:
    switch (Code) {
    case ICMP_CODE_UNREACH_NET:
    case ICMP_CODE_UNREACH_HOST:
    case ICMP_CODE_UNREACH_PROTOCOL:
    case ICMP_CODE_UNREACH_PORT:
    case ICMP_CODE_UNREACH_SRCFAIL:
      IcmpErr = (ICMP_ERROR) (ICMP_ERR_UNREACH_NET + Code);

      break;

    case ICMP_CODE_UNREACH_NEEDFRAG:
      IcmpErr = ICMP_ERR_MSGSIZE;

      break;

    case ICMP_CODE_UNREACH_NET_UNKNOWN:
    case ICMP_CODE_UNREACH_NET_PROHIB:
    case ICMP_CODE_UNREACH_TOSNET:
      IcmpErr = ICMP_ERR_UNREACH_NET;

      break;

    case ICMP_CODE_UNREACH_HOST_UNKNOWN:
    case ICMP_CODE_UNREACH_ISOLATED:
    case ICMP_CODE_UNREACH_HOST_PROHIB:
    case ICMP_CODE_UNREACH_TOSHOST:
      IcmpErr = ICMP_ERR_UNREACH_HOST;

      break;

    default:
      return EFI_ABORTED;
    }

    break;

  case ICMP_TYPE_TIMXCEED:
    if (Code > 1) {
      return EFI_ABORTED;
    }

    IcmpErr = (ICMP_ERROR) (Code + ICMP_ERR_TIMXCEED_INTRANS);

    break;

  case ICMP_TYPE_PARAMPROB:
    if (Code > 1) {
      return EFI_ABORTED;
    }

    IcmpErr = ICMP_ERR_PARAMPROB;

    break;

  case ICMP_TYPE_SOURCEQUENCH:
    if (Code != 0) {
      return EFI_ABORTED;
    }

    IcmpErr = ICMP_ERR_QUENCH;

    break;

  default:
    return EFI_ABORTED;
  }

  //
  // Notify user the ICMP pkt only containing payload except
  // IP and ICMP header
  //
  PayLoadHdr = (UINT8 *) ((UINT8 *) IpHdr + EFI_IP4_HEADER_LEN (IpHdr));
  TrimBytes  = (UINT32) (PayLoadHdr - (UINT8 *) IcmpHdr);

  NetbufTrim (Pkt, TrimBytes, TRUE);

  IpIo->PktRcvdNotify (EFI_ICMP_ERROR, IcmpErr, Session, Pkt, IpIo->RcvdContext);

  return EFI_SUCCESS;  
}

/**
  This function handles ICMPv6 packets. It is the worker function of 
  IpIoIcmpHandler.

  @param[in]       IpIo            Pointer to the IP_IO instance.
  @param[in, out]  Pkt             Pointer to the ICMPv6 packet.
  @param[in]       Session         Pointer to the net session of this ICMPv6 packet.

  @retval          EFI_SUCCESS     The ICMPv6 packet is handled successfully.
  @retval          EFI_ABORTED     This type of ICMPv6 packet is not supported.

**/
EFI_STATUS
IpIoIcmpv6Handler (
  IN     IP_IO                *IpIo,
  IN OUT NET_BUF              *Pkt,
  IN     EFI_NET_SESSION_DATA *Session
  )
{
  IP6_ICMP_ERROR_HEAD  *IcmpHdr;
  EFI_IP6_HEADER       *IpHdr;
  ICMP6_ERROR          IcmpErr;
  UINT8                *PayLoadHdr;
  UINT8                Type;
  UINT8                Code;
  UINT8                NextHeader;
  UINT32               TrimBytes;
  BOOLEAN              Flag;

  ASSERT (IpIo->IpVersion == IP_VERSION_6);

  //
  // Check the ICMPv6 packet length.
  //
  if (Pkt->TotalSize < sizeof (IP6_ICMP_ERROR_HEAD)) {

    return EFI_ABORTED;
  }

  IcmpHdr = NET_PROTO_HDR (Pkt, IP6_ICMP_ERROR_HEAD);
  Type    = IcmpHdr->Head.Type;
  Code    = IcmpHdr->Head.Code;

  //
  // Analyze the ICMPv6 Error in this ICMPv6 packet
  // 
  switch (Type) {
  case ICMP_V6_DEST_UNREACHABLE:
    switch (Code) {
    case ICMP_V6_NO_ROUTE_TO_DEST:
    case ICMP_V6_BEYOND_SCOPE:
    case ICMP_V6_ROUTE_REJECTED:
      IcmpErr = ICMP6_ERR_UNREACH_NET;

      break;

    case ICMP_V6_COMM_PROHIBITED:
    case ICMP_V6_ADDR_UNREACHABLE:
    case ICMP_V6_SOURCE_ADDR_FAILED:
      IcmpErr = ICMP6_ERR_UNREACH_HOST;

      break;

    case ICMP_V6_PORT_UNREACHABLE:
      IcmpErr = ICMP6_ERR_UNREACH_PORT;

      break;

     default:
      return EFI_ABORTED;
    }

    break;

  case ICMP_V6_PACKET_TOO_BIG:
    if (Code >= 1) {
      return EFI_ABORTED;
    }

    IcmpErr = ICMP6_ERR_PACKAGE_TOOBIG;

    break;

  case ICMP_V6_TIME_EXCEEDED:
    if (Code > 1) {
      return EFI_ABORTED;
    }

    IcmpErr = (ICMP6_ERROR) (ICMP6_ERR_TIMXCEED_HOPLIMIT + Code);

    break;

  case ICMP_V6_PARAMETER_PROBLEM:
    if (Code > 3) {
      return EFI_ABORTED;
    }

    IcmpErr = (ICMP6_ERROR) (ICMP6_ERR_PARAMPROB_HEADER + Code);

    break;

   default:

     return EFI_ABORTED;
   } 

  //
  // Notify user the ICMPv6 packet only containing payload except
  // IPv6 basic header, extension header and ICMP header
  //

  IpHdr      = (EFI_IP6_HEADER *) (&IcmpHdr->IpHead);
  NextHeader = IpHdr->NextHeader;
  PayLoadHdr = (UINT8 *) ((UINT8 *) IcmpHdr + sizeof (IP6_ICMP_ERROR_HEAD));
  Flag       = TRUE;
  
  do {
    switch (NextHeader) {
    case EFI_IP_PROTO_UDP:
    case EFI_IP_PROTO_TCP:
    case EFI_IP_PROTO_ICMP:
    case IP6_NO_NEXT_HEADER:
      Flag = FALSE;

      break;

    case IP6_HOP_BY_HOP:
    case IP6_DESTINATION:
      //
      // The Hdr Ext Len is 8-bit unsigned integer in 8-octet units, not including
      // the first 8 octets.
      //
      NextHeader = *(PayLoadHdr);
      PayLoadHdr = (UINT8 *) (PayLoadHdr + (*(PayLoadHdr + 1) + 1) * 8);

      break;

    case IP6_FRAGMENT:
      //
      // The Fragment Header Length is 8 octets.
      //
      NextHeader = *(PayLoadHdr);
      PayLoadHdr = (UINT8 *) (PayLoadHdr + 8);

      break;

    default:

      return EFI_ABORTED;
    }
  } while (Flag);

  TrimBytes = (UINT32) (PayLoadHdr - (UINT8 *) IcmpHdr);
  
  NetbufTrim (Pkt, TrimBytes, TRUE);

  IpIo->PktRcvdNotify (EFI_ICMP_ERROR, (ICMP_ERROR) IcmpErr, Session, Pkt, IpIo->RcvdContext);

  return EFI_SUCCESS;
}

/**
  This function handles ICMP packets.

  @param[in]       IpIo            Pointer to the IP_IO instance.
  @param[in, out]  Pkt             Pointer to the ICMP packet.
  @param[in]       Session         Pointer to the net session of this ICMP packet.

  @retval          EFI_SUCCESS     The ICMP packet is handled successfully.
  @retval          EFI_ABORTED     This type of ICMP packet is not supported.
  @retval          EFI_UNSUPPORTED The IP protocol version in IP_IO is not supported.

**/
EFI_STATUS
IpIoIcmpHandler (
  IN     IP_IO                *IpIo,
  IN OUT NET_BUF              *Pkt,
  IN     EFI_NET_SESSION_DATA *Session
  )
{

  if (IpIo->IpVersion == IP_VERSION_4) {

    return IpIoIcmpv4Handler (IpIo, Pkt, Session);

  } else if (IpIo->IpVersion == IP_VERSION_6) {

    return IpIoIcmpv6Handler (IpIo, Pkt, Session);

  } else {

    return EFI_UNSUPPORTED;
  }
}


/**
  Free function for receive token of IP_IO. It is used to
  signal the recycle event to notify IP to recycle the
  data buffer.

  @param[in]  Event                 The event to be signaled.

**/
VOID
IpIoExtFree (
  IN VOID  *Event
  )
{
  gBS->SignalEvent ((EFI_EVENT) Event);
}


/**
  Create a send entry to wrap a packet before sending
  out it through IP.

  @param[in, out]  IpIo                 Pointer to the IP_IO instance.
  @param[in, out]  Pkt                  Pointer to the packet.
  @param[in]       Sender               Pointer to the IP sender.
  @param[in]       Context              Pointer to the context.
  @param[in]       NotifyData           Pointer to the notify data.
  @param[in]       Dest                 Pointer to the destination IP address.
  @param[in]       Override             Pointer to the overriden IP_IO data.

  @return Pointer to the data structure created to wrap the packet. If NULL,
  @return resource limit occurred.

**/
IP_IO_SEND_ENTRY *
IpIoCreateSndEntry (
  IN OUT IP_IO             *IpIo,
  IN OUT NET_BUF           *Pkt,
  IN     VOID              *Sender,
  IN     VOID              *Context    OPTIONAL,
  IN     VOID              *NotifyData OPTIONAL,
  IN     EFI_IP_ADDRESS    *Dest       OPTIONAL,
  IN     IP_IO_OVERRIDE    *Override
  )
{
  IP_IO_SEND_ENTRY          *SndEntry;
  EFI_EVENT                 Event;  
  EFI_STATUS                Status;
  NET_FRAGMENT              *ExtFragment;
  UINT32                    FragmentCount;
  IP_IO_OVERRIDE            *OverrideData;
  IP_IO_IP_TX_DATA          *TxData;
  EFI_IP4_TRANSMIT_DATA     *Ip4TxData;
  EFI_IP6_TRANSMIT_DATA     *Ip6TxData;

  if ((IpIo->IpVersion != IP_VERSION_4) && (IpIo->IpVersion != IP_VERSION_6)) {
    return NULL;
  }

  Event        = NULL;
  TxData       = NULL;
  OverrideData = NULL;

  //
  // Allocate resource for SndEntry
  //
  SndEntry = AllocatePool (sizeof (IP_IO_SEND_ENTRY));
  if (NULL == SndEntry) {
    return NULL;
  }

  Status = gBS->CreateEvent (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  IpIoTransmitHandler,
                  SndEntry,
                  &Event
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  FragmentCount = Pkt->BlockOpNum;

  //
  // Allocate resource for TxData
  //
  TxData = (IP_IO_IP_TX_DATA *) AllocatePool (
    sizeof (IP_IO_IP_TX_DATA) + sizeof (NET_FRAGMENT) * (FragmentCount - 1)
    );

  if (NULL == TxData) {
    goto ON_ERROR;
  }

  //
  // Build a fragment table to contain the fragments in the packet. 
  //
  if (IpIo->IpVersion == IP_VERSION_4) {
    ExtFragment = (NET_FRAGMENT *) TxData->Ip4TxData.FragmentTable;
  } else {
    ExtFragment = (NET_FRAGMENT *) TxData->Ip6TxData.FragmentTable;
  }

  NetbufBuildExt (Pkt, ExtFragment, &FragmentCount);


  //
  // Allocate resource for OverrideData if needed
  //
  if (NULL != Override) {

    OverrideData = AllocateCopyPool (sizeof (IP_IO_OVERRIDE), Override);
    if (NULL == OverrideData) {
      goto ON_ERROR;
    }
  }

  //
  // Set other fields of TxData except the fragment table
  //
  if (IpIo->IpVersion == IP_VERSION_4) {

    Ip4TxData = &TxData->Ip4TxData;

    CopyMem (&Ip4TxData->DestinationAddress, Dest, sizeof (EFI_IPv4_ADDRESS));

    Ip4TxData->OverrideData    = &OverrideData->Ip4OverrideData;
    Ip4TxData->OptionsLength   = 0;
    Ip4TxData->OptionsBuffer   = NULL;
    Ip4TxData->TotalDataLength = Pkt->TotalSize;
    Ip4TxData->FragmentCount   = FragmentCount;

    //
    // Set the fields of SndToken
    //
    SndEntry->SndToken.Ip4Token.Event         = Event;
    SndEntry->SndToken.Ip4Token.Packet.TxData = Ip4TxData;
  } else {

    Ip6TxData = &TxData->Ip6TxData;

    if (Dest != NULL) {
      CopyMem (&Ip6TxData->DestinationAddress, Dest, sizeof (EFI_IPv6_ADDRESS));
    } else {
      ZeroMem (&Ip6TxData->DestinationAddress, sizeof (EFI_IPv6_ADDRESS));
    }

    Ip6TxData->OverrideData  = &OverrideData->Ip6OverrideData;
    Ip6TxData->DataLength    = Pkt->TotalSize;
    Ip6TxData->FragmentCount = FragmentCount;
    Ip6TxData->ExtHdrsLength = 0;
    Ip6TxData->ExtHdrs       = NULL;
    
    //
    // Set the fields of SndToken
    //
    SndEntry->SndToken.Ip6Token.Event         = Event;
    SndEntry->SndToken.Ip6Token.Packet.TxData = Ip6TxData;
  }

  //
  // Set the fields of SndEntry
  //
  SndEntry->IpIo        = IpIo;
  SndEntry->Ip          = Sender;
  SndEntry->Context     = Context;
  SndEntry->NotifyData  = NotifyData;

  SndEntry->Pkt         = Pkt;
  NET_GET_REF (Pkt);

  InsertTailList (&IpIo->PendingSndList, &SndEntry->Entry);

  return SndEntry;

ON_ERROR:

  if (OverrideData != NULL) {
    FreePool (OverrideData);
  }

  if (TxData != NULL) {
    FreePool (TxData);
  }

  if (SndEntry != NULL) {
    FreePool (SndEntry);
  }

  if (Event != NULL) {
    gBS->CloseEvent (Event);
  }

  return NULL;
}


/**
  Destroy the SndEntry.
  
  This function pairs with IpIoCreateSndEntry().

  @param[in]  SndEntry              Pointer to the send entry to be destroyed.

**/
VOID
IpIoDestroySndEntry (
  IN IP_IO_SEND_ENTRY  *SndEntry
  )
{
  EFI_EVENT         Event;
  IP_IO_IP_TX_DATA  *TxData;
  IP_IO_OVERRIDE    *Override;

  if (SndEntry->IpIo->IpVersion == IP_VERSION_4) {
    Event              = SndEntry->SndToken.Ip4Token.Event;
    TxData             = (IP_IO_IP_TX_DATA *) SndEntry->SndToken.Ip4Token.Packet.TxData;
    Override           = (IP_IO_OVERRIDE *) TxData->Ip4TxData.OverrideData;
  } else if (SndEntry->IpIo->IpVersion == IP_VERSION_6) {
    Event              = SndEntry->SndToken.Ip6Token.Event;
    TxData             = (IP_IO_IP_TX_DATA *) SndEntry->SndToken.Ip6Token.Packet.TxData;
    Override           = (IP_IO_OVERRIDE *) TxData->Ip6TxData.OverrideData;
  } else {
    return ;
  }

  gBS->CloseEvent (Event);

  FreePool (TxData);

  if (NULL != Override) {
    FreePool (Override);
  }

  NetbufFree (SndEntry->Pkt);

  RemoveEntryList (&SndEntry->Entry);

  FreePool (SndEntry);
}


/**
  Notify function for IP transmit token.

  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoTransmitHandlerDpc (
  IN VOID      *Context
  )
{
  IP_IO             *IpIo;
  IP_IO_SEND_ENTRY  *SndEntry;
  EFI_STATUS        Status;

  SndEntry  = (IP_IO_SEND_ENTRY *) Context;

  IpIo      = SndEntry->IpIo;

  if (IpIo->IpVersion == IP_VERSION_4) {
    Status = SndEntry->SndToken.Ip4Token.Status;
  } else if (IpIo->IpVersion == IP_VERSION_6){
    Status = SndEntry->SndToken.Ip6Token.Status;
  } else {
    return ;
  }

  if ((IpIo->PktSentNotify != NULL) && (SndEntry->NotifyData != NULL)) {
    IpIo->PktSentNotify (
            Status,
            SndEntry->Context,
            SndEntry->Ip,
            SndEntry->NotifyData
            );
  }

  IpIoDestroySndEntry (SndEntry);
}


/**
  Notify function for IP transmit token.

  @param[in]  Event                 The event signaled.
  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoTransmitHandler (
  IN EFI_EVENT Event,
  IN VOID      *Context
  )
{
  //
  // Request IpIoTransmitHandlerDpc as a DPC at TPL_CALLBACK
  //
  QueueDpc (TPL_CALLBACK, IpIoTransmitHandlerDpc, Context);
}


/**
  The dummy handler for the dummy IP receive token.

  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoDummyHandlerDpc (
  IN VOID      *Context
  )
{
  IP_IO_IP_INFO             *IpInfo;
  EFI_STATUS                 Status;
  EFI_EVENT                  RecycleEvent;

  IpInfo      = (IP_IO_IP_INFO *) Context;

  if ((IpInfo->IpVersion != IP_VERSION_4) && (IpInfo->IpVersion != IP_VERSION_6)) {
    return ;
  }

  RecycleEvent = NULL;

  if (IpInfo->IpVersion == IP_VERSION_4) {
    Status = IpInfo->DummyRcvToken.Ip4Token.Status;

    if (IpInfo->DummyRcvToken.Ip4Token.Packet.RxData != NULL) {
      RecycleEvent = IpInfo->DummyRcvToken.Ip4Token.Packet.RxData->RecycleSignal;
    }
  } else {
    Status = IpInfo->DummyRcvToken.Ip6Token.Status;

    if (IpInfo->DummyRcvToken.Ip6Token.Packet.RxData != NULL) {
      RecycleEvent = IpInfo->DummyRcvToken.Ip6Token.Packet.RxData->RecycleSignal;
    }
  }



  if (EFI_ABORTED == Status) {
    //
    // The reception is actively aborted by the consumer, directly return.
    //
    return;
  } else if (EFI_SUCCESS == Status) {
    //
    // Recycle the RxData.
    //
    ASSERT (RecycleEvent != NULL);

    gBS->SignalEvent (RecycleEvent);
  }

  //
  // Continue the receive.
  //
  if (IpInfo->IpVersion == IP_VERSION_4) {
    ((EFI_IP4_PROTOCOL *) (IpInfo->Ip))->Receive (
                                           (EFI_IP4_PROTOCOL *) (IpInfo->Ip),
                                           &IpInfo->DummyRcvToken.Ip4Token
                                           );
  } else {
    ((EFI_IP6_PROTOCOL *) (IpInfo->Ip))->Receive (
                                           (EFI_IP6_PROTOCOL *) (IpInfo->Ip),
                                           &IpInfo->DummyRcvToken.Ip6Token
                                           );
  }
}


/**
  This function add IpIoDummyHandlerDpc to the end of the DPC queue.

  @param[in]  Event                 The event signaled.
  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoDummyHandler (
  IN EFI_EVENT Event,
  IN VOID      *Context
  )
{
  //
  // Request IpIoDummyHandlerDpc as a DPC at TPL_CALLBACK
  //
  QueueDpc (TPL_CALLBACK, IpIoDummyHandlerDpc, Context);
}


/**
  Notify function for the IP receive token, used to process
  the received IP packets.

  @param[in]  Context               The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoListenHandlerDpc (
  IN VOID      *Context
  )
{
  IP_IO                 *IpIo;
  EFI_STATUS            Status;
  IP_IO_IP_RX_DATA      *RxData;
  VOID                  *Ip;
  EFI_NET_SESSION_DATA  Session;
  NET_BUF               *Pkt;

  IpIo = (IP_IO *) Context;
  Ip   = IpIo->Ip;

  if (IpIo->IpVersion == IP_VERSION_4) {
    Status = IpIo->RcvToken.Ip4Token.Status;
    RxData = (IP_IO_IP_RX_DATA *) IpIo->RcvToken.Ip4Token.Packet.RxData;
  } else if (IpIo->IpVersion == IP_VERSION_6) {
    Status = IpIo->RcvToken.Ip6Token.Status;
    RxData = (IP_IO_IP_RX_DATA *) IpIo->RcvToken.Ip6Token.Packet.RxData;
  } else {
    return;
  }

  if (EFI_ABORTED == Status) {
    //
    // The reception is actively aborted by the consumer, directly return.
    //
    return;
  }

  if (((EFI_SUCCESS != Status) && (EFI_ICMP_ERROR != Status)) || (NULL == RxData)) {
    //
    // @bug Only process the normal packets and the icmp error packets, if RxData is NULL
    // @bug with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although
    // @bug this should be a bug of the low layer (IP).
    //
    goto Resume;
  }

  if (NULL == IpIo->PktRcvdNotify) {
    goto CleanUp;
  }

  if (IpIo->IpVersion == IP_VERSION_4) {
    if ((EFI_IP4 (RxData->Ip4RxData.Header->SourceAddress) != 0) &&
      !Ip4IsUnicast (EFI_NTOHL (((EFI_IP4_RECEIVE_DATA *) RxData)->Header->SourceAddress), 0)) {
    //
    // The source address is not zero and it's not a unicast IP address, discard it.
    //
    goto CleanUp;
  }

  //
  // Create a netbuffer representing IPv4 packet
  //
  Pkt = NetbufFromExt (
          (NET_FRAGMENT *) RxData->Ip4RxData.FragmentTable,
          RxData->Ip4RxData.FragmentCount,
          0,
          0,
          IpIoExtFree,
          RxData->Ip4RxData.RecycleSignal
          );
  if (NULL == Pkt) {
    goto CleanUp;
  }

  //
  // Create a net session
  //
  Session.Source.Addr[0] = EFI_IP4 (RxData->Ip4RxData.Header->SourceAddress);
  Session.Dest.Addr[0]   = EFI_IP4 (RxData->Ip4RxData.Header->DestinationAddress);
  Session.IpHdr.Ip4Hdr   = RxData->Ip4RxData.Header;
  Session.IpVersion      = IP_VERSION_4;
  } else {

    if (!Ip6IsValidUnicast(&RxData->Ip6RxData.Header->SourceAddress)) {
      goto CleanUp;
    }
    
    //
    // Create a netbuffer representing IPv6 packet
    //
    Pkt = NetbufFromExt (
            (NET_FRAGMENT *) RxData->Ip6RxData.FragmentTable,
            RxData->Ip6RxData.FragmentCount,
            0,
            0,
            IpIoExtFree,
            RxData->Ip6RxData.RecycleSignal
            );
    if (NULL == Pkt) {
      goto CleanUp;
    }

    //
    // Create a net session
    //
    CopyMem (
      &Session.Source, 
      &RxData->Ip6RxData.Header->SourceAddress,
      sizeof(EFI_IPv6_ADDRESS)
      );
    CopyMem (
      &Session.Dest, 
      &RxData->Ip6RxData.Header->DestinationAddress, 
      sizeof(EFI_IPv6_ADDRESS)
      );
    Session.IpHdr.Ip6Hdr = RxData->Ip6RxData.Header;
    Session.IpVersion    = IP_VERSION_6;
  } 

  if (EFI_SUCCESS == Status) {

    IpIo->PktRcvdNotify (EFI_SUCCESS, (ICMP_ERROR) 0, &Session, Pkt, IpIo->RcvdContext);
  } else {
    //
    // Status is EFI_ICMP_ERROR
    //
    Status = IpIoIcmpHandler (IpIo, Pkt, &Session);
    if (EFI_ERROR (Status)) {
      NetbufFree (Pkt);
    }
  }

  goto Resume;

CleanUp:

  if (IpIo->IpVersion == IP_VERSION_4){
    gBS->SignalEvent (RxData->Ip4RxData.RecycleSignal);
  } else {
    gBS->SignalEvent (RxData->Ip6RxData.RecycleSignal); 
  }

Resume:

  if (IpIo->IpVersion == IP_VERSION_4){
    ((EFI_IP4_PROTOCOL *) Ip)->Receive (Ip, &(IpIo->RcvToken.Ip4Token));
  } else {
    ((EFI_IP6_PROTOCOL *) Ip)->Receive (Ip, &(IpIo->RcvToken.Ip6Token));
  }
}

/**
  This function add IpIoListenHandlerDpc to the end of the DPC queue.

  @param[in]  Event                The event signaled.
  @param[in]  Context              The context passed in by the event notifier.

**/
VOID
EFIAPI
IpIoListenHandler (
  IN EFI_EVENT Event,
  IN VOID      *Context
  )
{
  //
  // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK
  //
  QueueDpc (TPL_CALLBACK, IpIoListenHandlerDpc, Context);
}


/**
  Create a new IP_IO instance.
  
  This function uses IP4/IP6 service binding protocol in Controller to create
  an IP4/IP6 child (aka IP4/IP6 instance).

  @param[in]  Image             The image handle of the driver or application that
                                consumes IP_IO.
  @param[in]  Controller        The controller handle that has IP4 or IP6 service
                                binding protocol installed.
  @param[in]  IpVersion         The version of the IP protocol to use, either
                                IPv4 or IPv6.                            

  @return Pointer to a newly created IP_IO instance, or NULL if failed.

**/
IP_IO *
EFIAPI
IpIoCreate (
  IN EFI_HANDLE Image,
  IN EFI_HANDLE Controller,
  IN IP_VERSION IpVersion  
  )
{
  EFI_STATUS  Status;
  IP_IO       *IpIo;
  EFI_EVENT   Event;

  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));

  IpIo = AllocateZeroPool (sizeof (IP_IO));
  if (NULL == IpIo) {
    return NULL;
  }

  InitializeListHead (&(IpIo->PendingSndList));
  InitializeListHead (&(IpIo->IpList));
  IpIo->Controller  = Controller;
  IpIo->Image       = Image;
  IpIo->IpVersion   = IpVersion;
  Event             = NULL;

  Status = gBS->CreateEvent (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  IpIoListenHandler,
                  IpIo,
                  &Event
                  );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpIo;
  }

  if (IpVersion == IP_VERSION_4) {
    IpIo->RcvToken.Ip4Token.Event = Event;
  } else {
    IpIo->RcvToken.Ip6Token.Event = Event;
  }

  //
  // Create an IP child and open IP protocol
  //
  Status = IpIoCreateIpChildOpenProtocol (
             Controller,
             Image,
             &IpIo->ChildHandle,
             IpVersion,             
             (VOID **)&(IpIo->Ip)
             );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpIo;
  }

  return IpIo;

ReleaseIpIo:

  if (Event != NULL) {
    gBS->CloseEvent (Event);
  }

  gBS->FreePool (IpIo);

  return NULL;
}


/**
  Open an IP_IO instance for use.
  
  This function is called after IpIoCreate(). It is used for configuring the IP
  instance and register the callbacks and their context data for sending and
  receiving IP packets.

  @param[in, out]  IpIo               Pointer to an IP_IO instance that needs
                                      to open.
  @param[in]       OpenData           The configuration data and callbacks for
                                      the IP_IO instance.

  @retval          EFI_SUCCESS        The IP_IO instance opened with OpenData
                                      successfully.
  @retval          EFI_ACCESS_DENIED  The IP_IO instance is configured, avoid to 
                                      reopen it.
  @retval          Others             Error condition occurred.

**/
EFI_STATUS
EFIAPI
IpIoOpen (
  IN OUT IP_IO           *IpIo,
  IN     IP_IO_OPEN_DATA *OpenData
  )
{
  EFI_STATUS        Status;
  VOID              *Ip;
  IP_VERSION        IpVersion;

  if (IpIo->IsConfigured) {
    return EFI_ACCESS_DENIED;
  }

  IpVersion = IpIo->IpVersion;

  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));

  Ip = IpIo->Ip;

  //
  // configure ip
  //
  if (IpVersion == IP_VERSION_4){
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Configure (
                                          (EFI_IP4_PROTOCOL *) Ip,
                                          &OpenData->IpConfigData.Ip4CfgData
                                          );
  } else {

    Status = ((EFI_IP6_PROTOCOL *) Ip)->Configure (
                                          (EFI_IP6_PROTOCOL *) Ip, 
                                          &OpenData->IpConfigData.Ip6CfgData
                                          );
  }

  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // @bug To delete the default route entry in this Ip, if it is:
  // @bug (0.0.0.0, 0.0.0.0, 0.0.0.0). Delete this statement if Ip modified
  // @bug its code
  //
  if (IpVersion == IP_VERSION_4){
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Routes (
                                          (EFI_IP4_PROTOCOL *) Ip,
                                          TRUE,
                                          &mZeroIp4Addr,
                                          &mZeroIp4Addr,
                                          &mZeroIp4Addr
                                          );

    if (EFI_ERROR (Status) && (EFI_NOT_FOUND != Status)) {
      return Status;
    }
  }

  IpIo->PktRcvdNotify = OpenData->PktRcvdNotify;
  IpIo->PktSentNotify = OpenData->PktSentNotify;

  IpIo->RcvdContext   = OpenData->RcvdContext;
  IpIo->SndContext    = OpenData->SndContext;

  if (IpVersion == IP_VERSION_4){
    IpIo->Protocol = OpenData->IpConfigData.Ip4CfgData.DefaultProtocol;

    //
    // start to listen incoming packet
    //
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Receive (
                                          (EFI_IP4_PROTOCOL *) Ip,
                                          &(IpIo->RcvToken.Ip4Token)
                                          );
    if (EFI_ERROR (Status)) {
      ((EFI_IP4_PROTOCOL *) Ip)->Configure ((EFI_IP4_PROTOCOL *) Ip, NULL);
      goto ErrorExit;
    }

  } else {

    IpIo->Protocol = OpenData->IpConfigData.Ip6CfgData.DefaultProtocol;
    Status = ((EFI_IP6_PROTOCOL *) Ip)->Receive (
                                          (EFI_IP6_PROTOCOL *) Ip,
                                          &(IpIo->RcvToken.Ip6Token)
                                          );
    if (EFI_ERROR (Status)) {
      ((EFI_IP6_PROTOCOL *) Ip)->Configure ((EFI_IP6_PROTOCOL *) Ip, NULL);
      goto ErrorExit;
    }
  }

  IpIo->IsConfigured = TRUE;
  InsertTailList (&mActiveIpIoList, &IpIo->Entry);

ErrorExit:

  return Status;
}


/**
  Stop an IP_IO instance.
  
  This function is paired with IpIoOpen(). The IP_IO will be unconfigured and all
  the pending send/receive tokens will be canceled.

  @param[in, out]  IpIo            Pointer to the IP_IO instance that needs to stop.

  @retval          EFI_SUCCESS     The IP_IO instance stopped successfully.
  @retval          Others          Error condition occurred.

**/
EFI_STATUS
EFIAPI
IpIoStop (
  IN OUT IP_IO *IpIo
  )
{
  EFI_STATUS        Status;
  VOID              *Ip;
  IP_IO_IP_INFO     *IpInfo;
  IP_VERSION        IpVersion;

  if (!IpIo->IsConfigured) {
    return EFI_SUCCESS;
  }

  IpVersion = IpIo->IpVersion;

  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));

  //
  // Remove the IpIo from the active IpIo list.
  //
  RemoveEntryList (&IpIo->Entry);

  Ip = IpIo->Ip;

  //
  // Configure NULL Ip
  //
  if (IpVersion == IP_VERSION_4) {
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Configure ((EFI_IP4_PROTOCOL *) Ip, NULL);
  } else {
    Status = ((EFI_IP6_PROTOCOL *) Ip)->Configure ((EFI_IP6_PROTOCOL *) Ip, NULL);
  }
  if (EFI_ERROR (Status)) {
    return Status;
  }

  IpIo->IsConfigured = FALSE;

  //
  // Detroy the Ip List used by IpIo
  //

  while (!IsListEmpty (&(IpIo->IpList))) {
    IpInfo = NET_LIST_HEAD (&(IpIo->IpList), IP_IO_IP_INFO, Entry);

    IpIoRemoveIp (IpIo, IpInfo);
  }

  //
  // All pending send tokens should be flushed by reseting the IP instances.
  //
  ASSERT (IsListEmpty (&IpIo->PendingSndList));

  //
  // Close the receive event.
  //
  if (IpVersion == IP_VERSION_4){
    gBS->CloseEvent (IpIo->RcvToken.Ip4Token.Event);
  } else {
    gBS->CloseEvent (IpIo->RcvToken.Ip6Token.Event);
  }

  return EFI_SUCCESS;
}


/**
  Destroy an IP_IO instance.
  
  This function is paired with IpIoCreate(). The IP_IO will be closed first.
  Resource will be freed afterwards. See IpIoCloseProtocolDestroyIpChild().

  @param[in, out]  IpIo         Pointer to the IP_IO instance that needs to be
                                destroyed.

  @retval          EFI_SUCCESS  The IP_IO instance destroyed successfully.
  @retval          Others       Error condition occurred.

**/
EFI_STATUS
EFIAPI
IpIoDestroy (
  IN OUT IP_IO *IpIo
  )
{
  //
  // Stop the IpIo.
  //
  IpIoStop (IpIo);

  //
  // Close the IP protocol and destroy the child.
  //
  IpIoCloseProtocolDestroyIpChild (
    IpIo->Controller,
    IpIo->Image,
    IpIo->ChildHandle,
    IpIo->IpVersion
    );

  gBS->FreePool (IpIo);

  return EFI_SUCCESS;
}


/**
  Send out an IP packet.
  
  This function is called after IpIoOpen(). The data to be sent are wrapped in
  Pkt. The IP instance wrapped in IpIo is used for sending by default but can be
  overriden by Sender. Other sending configs, like source address and gateway
  address etc., are specified in OverrideData.

  @param[in, out]  IpIo                  Pointer to an IP_IO instance used for sending IP
                                         packet.
  @param[in, out]  Pkt                   Pointer to the IP packet to be sent.
  @param[in]       Sender                The IP protocol instance used for sending.
  @param[in]       Context               Optional context data.
  @param[in]       NotifyData            Optional notify data.
  @param[in]       Dest                  The destination IP address to send this packet to.
  @param[in]       OverrideData          The data to override some configuration of the IP
                                         instance used for sending.

  @retval          EFI_SUCCESS           The operation is completed successfully.
  @retval          EFI_NOT_STARTED       The IpIo is not configured.
  @retval          EFI_OUT_OF_RESOURCES  Failed due to resource limit.

**/
EFI_STATUS
EFIAPI
IpIoSend (
  IN OUT IP_IO          *IpIo,
  IN OUT NET_BUF        *Pkt,
  IN     IP_IO_IP_INFO  *Sender        OPTIONAL,
  IN     VOID           *Context       OPTIONAL,
  IN     VOID           *NotifyData    OPTIONAL,
  IN     EFI_IP_ADDRESS *Dest,
  IN     IP_IO_OVERRIDE *OverrideData  OPTIONAL
  )
{
  EFI_STATUS        Status;
  VOID              *Ip;
  IP_IO_SEND_ENTRY  *SndEntry;

  ASSERT ((IpIo->IpVersion != IP_VERSION_4) || (Dest != NULL));

  if (!IpIo->IsConfigured) {
    return EFI_NOT_STARTED;
  }

  Ip = (NULL == Sender) ? IpIo->Ip : Sender->Ip;

  //
  // create a new SndEntry
  //
  SndEntry = IpIoCreateSndEntry (IpIo, Pkt, Ip, Context, NotifyData, Dest, OverrideData);
  if (NULL == SndEntry) {
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // Send this Packet
  //
  if (IpIo->IpVersion == IP_VERSION_4){
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Transmit (
                                          (EFI_IP4_PROTOCOL *) Ip,
                                          &SndEntry->SndToken.Ip4Token
                                          );
  } else {
    Status = ((EFI_IP6_PROTOCOL *) Ip)->Transmit (
                                          (EFI_IP6_PROTOCOL *) Ip,
                                          &SndEntry->SndToken.Ip6Token
                                          );
  }

  if (EFI_ERROR (Status)) {
    IpIoDestroySndEntry (SndEntry);
  }

  return Status;
}


/**
  Cancel the IP transmit token which wraps this Packet.

  @param[in]  IpIo                  Pointer to the IP_IO instance.
  @param[in]  Packet                Pointer to the packet of NET_BUF to cancel.

**/
VOID
EFIAPI
IpIoCancelTxToken (
  IN IP_IO  *IpIo,
  IN VOID   *Packet
  )
{
  LIST_ENTRY        *Node;
  IP_IO_SEND_ENTRY  *SndEntry;
  VOID              *Ip;

  ASSERT ((IpIo != NULL) && (Packet != NULL));

  NET_LIST_FOR_EACH (Node, &IpIo->PendingSndList) {

    SndEntry = NET_LIST_USER_STRUCT (Node, IP_IO_SEND_ENTRY, Entry);

    if (SndEntry->Pkt == Packet) {

      Ip = SndEntry->Ip;

      if (IpIo->IpVersion == IP_VERSION_4) {
        ((EFI_IP4_PROTOCOL *) Ip)->Cancel (
                                     (EFI_IP4_PROTOCOL *) Ip,
                                     &SndEntry->SndToken.Ip4Token
                                     );
      } else {
        ((EFI_IP6_PROTOCOL *) Ip)->Cancel (
                                     (EFI_IP6_PROTOCOL *) Ip,
                                     &SndEntry->SndToken.Ip6Token
                                     );
      }

      break;
    }
  }

}


/**
  Add a new IP instance for sending data.
  
  The function is used to add the IP_IO to the IP_IO sending list. The caller
  can later use IpIoFindSender() to get the IP_IO and call IpIoSend() to send
  data.

  @param[in, out]  IpIo               Pointer to a IP_IO instance to add a new IP
                                      instance for sending purpose.

  @return Pointer to the created IP_IO_IP_INFO structure, NULL if failed.

**/
IP_IO_IP_INFO *
EFIAPI
IpIoAddIp (
  IN OUT IP_IO  *IpIo
  )
{
  EFI_STATUS     Status;
  IP_IO_IP_INFO  *IpInfo;
  EFI_EVENT      Event;

  ASSERT (IpIo != NULL);

  IpInfo = AllocatePool (sizeof (IP_IO_IP_INFO));
  if (IpInfo == NULL) {
    return NULL;
  }

  //
  // Init this IpInfo, set the Addr and SubnetMask to 0 before we configure the IP
  // instance.
  //
  InitializeListHead (&IpInfo->Entry);
  IpInfo->ChildHandle = NULL;
  ZeroMem (&IpInfo->Addr, sizeof (IpInfo->Addr));
  ZeroMem (&IpInfo->PreMask, sizeof (IpInfo->PreMask));

  IpInfo->RefCnt    = 1;
  IpInfo->IpVersion = IpIo->IpVersion;

  //
  // Create the IP instance and open the IP protocol.
  //
  Status = IpIoCreateIpChildOpenProtocol (
             IpIo->Controller,
             IpIo->Image,
             &IpInfo->ChildHandle,
             IpInfo->IpVersion,
             (VOID **) &IpInfo->Ip
             );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpInfo;
  }

  //
  // Create the event for the DummyRcvToken.
  //
  Status = gBS->CreateEvent (
                  EVT_NOTIFY_SIGNAL,
                  TPL_NOTIFY,
                  IpIoDummyHandler,
                  IpInfo,
                  &Event
                  );
  if (EFI_ERROR (Status)) {
    goto ReleaseIpChild;
  }

  if (IpInfo->IpVersion == IP_VERSION_4) {
    IpInfo->DummyRcvToken.Ip4Token.Event = Event;
  } else {
    IpInfo->DummyRcvToken.Ip6Token.Event = Event;
  }

  //
  // Link this IpInfo into the IpIo.
  //
  InsertTailList (&IpIo->IpList, &IpInfo->Entry);

  return IpInfo;

ReleaseIpChild:

  IpIoCloseProtocolDestroyIpChild (
    IpIo->Controller,
    IpIo->Image,
    IpInfo->ChildHandle,
    IpInfo->IpVersion
    );

ReleaseIpInfo:

  gBS->FreePool (IpInfo);

  return NULL;
}


/**
  Configure the IP instance of this IpInfo and start the receiving if IpConfigData
  is not NULL.

  @param[in, out]  IpInfo          Pointer to the IP_IO_IP_INFO instance.
  @param[in, out]  IpConfigData    The IP configure data used to configure the IP
                                   instance, if NULL the IP instance is reset. If
                                   UseDefaultAddress is set to TRUE, and the configure
                                   operation succeeds, the default address information
                                   is written back in this IpConfigData.

  @retval          EFI_SUCCESS     The IP instance of this IpInfo is configured successfully
                                   or no need to reconfigure it.
  @retval          Others          Configuration fails.

**/
EFI_STATUS
EFIAPI
IpIoConfigIp (
  IN OUT IP_IO_IP_INFO        *IpInfo,
  IN OUT VOID                 *IpConfigData OPTIONAL
  )
{
  EFI_STATUS         Status;
  VOID               *Ip;
  IP_VERSION         IpVersion;
  EFI_IP4_MODE_DATA  Ip4ModeData;
  EFI_IP6_MODE_DATA  Ip6ModeData;

  ASSERT (IpInfo != NULL);

  if (IpInfo->RefCnt > 1) {
    //
    // This IP instance is shared, don't reconfigure it until it has only one
    // consumer. Currently, only the tcp children cloned from their passive parent
    // will share the same IP. So this cases only happens while IpConfigData is NULL,
    // let the last consumer clean the IP instance.
    //
    return EFI_SUCCESS;
  }

  IpVersion = IpInfo->IpVersion;
  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));

  Ip = IpInfo->Ip;

  if (IpInfo->IpVersion == IP_VERSION_4) {
    Status = ((EFI_IP4_PROTOCOL *) Ip)->Configure ((EFI_IP4_PROTOCOL *) Ip, IpConfigData);
  } else {
    Status = ((EFI_IP6_PROTOCOL *) Ip)->Configure ((EFI_IP6_PROTOCOL *) Ip, IpConfigData);
  }

  if (EFI_ERROR (Status)) {
    goto OnExit;
  }

  if (IpConfigData != NULL) {
    if (IpInfo->IpVersion == IP_VERSION_4){

      if (((EFI_IP4_CONFIG_DATA *) IpConfigData)->UseDefaultAddress) {
        ((EFI_IP4_PROTOCOL *) Ip)->GetModeData (
                                     (EFI_IP4_PROTOCOL *) Ip, 
                                     &Ip4ModeData, 
                                     NULL, 
                                     NULL
                                     );

        ((EFI_IP4_CONFIG_DATA*) IpConfigData)->StationAddress = Ip4ModeData.ConfigData.StationAddress;
        ((EFI_IP4_CONFIG_DATA*) IpConfigData)->SubnetMask     = Ip4ModeData.ConfigData.SubnetMask;
    }

      CopyMem (
        &IpInfo->Addr.Addr, 
        &((EFI_IP4_CONFIG_DATA *) IpConfigData)->StationAddress, 
        sizeof (IP4_ADDR)
        );
      CopyMem (
        &IpInfo->PreMask.SubnetMask, 
        &((EFI_IP4_CONFIG_DATA *) IpConfigData)->SubnetMask,
        sizeof (IP4_ADDR)
        );

      Status = ((EFI_IP4_PROTOCOL *) Ip)->Receive (
                                            (EFI_IP4_PROTOCOL *) Ip,
                                            &IpInfo->DummyRcvToken.Ip4Token
                                            );
    if (EFI_ERROR (Status)) {
        ((EFI_IP4_PROTOCOL*)Ip)->Configure (Ip, NULL);
    }
  } else {

      ((EFI_IP6_PROTOCOL *) Ip)->GetModeData (
                                   (EFI_IP6_PROTOCOL *) Ip,
                                   &Ip6ModeData,
                                   NULL,
                                   NULL
                                   );

      if (Ip6ModeData.IsConfigured) {
        CopyMem (
          &((EFI_IP6_CONFIG_DATA *) IpConfigData)->StationAddress,
          &Ip6ModeData.ConfigData.StationAddress,
          sizeof (EFI_IPv6_ADDRESS)
          );

        if (Ip6ModeData.AddressList != NULL) {
          FreePool (Ip6ModeData.AddressList);
        }

        if (Ip6ModeData.GroupTable != NULL) {
          FreePool (Ip6ModeData.GroupTable);
        }

        if (Ip6ModeData.RouteTable != NULL) {
          FreePool (Ip6ModeData.RouteTable);
        }

        if (Ip6ModeData.NeighborCache != NULL) {
          FreePool (Ip6ModeData.NeighborCache);
        }

        if (Ip6ModeData.PrefixTable != NULL) {
          FreePool (Ip6ModeData.PrefixTable);
        }

        if (Ip6ModeData.IcmpTypeList != NULL) {
          FreePool (Ip6ModeData.IcmpTypeList);
        }

      } else {
        Status = EFI_NO_MAPPING;
        goto OnExit;
      } 

      CopyMem (
        &IpInfo->Addr, 
        &Ip6ModeData.ConfigData.StationAddress, 
        sizeof (EFI_IPv6_ADDRESS)
        );

      Status = ((EFI_IP6_PROTOCOL *) Ip)->Receive (
                                            (EFI_IP6_PROTOCOL *) Ip,
                                            &IpInfo->DummyRcvToken.Ip6Token
                                            );
      if (EFI_ERROR (Status)) {
        ((EFI_IP6_PROTOCOL *) Ip)->Configure ((EFI_IP6_PROTOCOL *) Ip, NULL);
      }
    }  
  } else {
    //
    // The IP instance is reset, set the stored Addr and SubnetMask to zero.
    //
    ZeroMem (&IpInfo->Addr, sizeof (IpInfo->Addr));
    ZeroMem (&IpInfo->PreMask, sizeof (IpInfo->PreMask));
  }

OnExit:

  return Status;
}


/**
  Destroy an IP instance maintained in IpIo->IpList for
  sending purpose.
  
  This function pairs with IpIoAddIp(). The IpInfo is previously created by
  IpIoAddIp(). The IP_IO_IP_INFO::RefCnt is decremented and the IP instance
  will be dstroyed if the RefCnt is zero.

  @param[in]  IpIo                  Pointer to the IP_IO instance.
  @param[in]  IpInfo                Pointer to the IpInfo to be removed.

**/
VOID
EFIAPI
IpIoRemoveIp (
  IN IP_IO            *IpIo,
  IN IP_IO_IP_INFO    *IpInfo
  )
{

  IP_VERSION          IpVersion;

  ASSERT (IpInfo->RefCnt > 0);

  NET_PUT_REF (IpInfo);

  if (IpInfo->RefCnt > 0) {

    return;
  }

  IpVersion = IpIo->IpVersion;

  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));

  RemoveEntryList (&IpInfo->Entry);

  if (IpVersion == IP_VERSION_4){
    ((EFI_IP4_PROTOCOL *) (IpInfo->Ip))->Configure (
                                           (EFI_IP4_PROTOCOL *) (IpInfo->Ip),
                                           NULL
                                           );
    IpIoCloseProtocolDestroyIpChild (
      IpIo->Controller,
      IpIo->Image,
      IpInfo->ChildHandle,
      IP_VERSION_4
      );

    gBS->CloseEvent (IpInfo->DummyRcvToken.Ip4Token.Event);

  } else {

    ((EFI_IP6_PROTOCOL *) (IpInfo->Ip))->Configure (
                                           (EFI_IP6_PROTOCOL *) (IpInfo->Ip),
                                           NULL
                                           );

    IpIoCloseProtocolDestroyIpChild (
      IpIo->Controller,
      IpIo->Image,
      IpInfo->ChildHandle,
      IP_VERSION_6
      );

    gBS->CloseEvent (IpInfo->DummyRcvToken.Ip6Token.Event);
  }

  FreePool (IpInfo);
}


/**
  Find the first IP protocol maintained in IpIo whose local
  address is the same as Src.
  
  This function is called when the caller needs the IpIo to send data to the
  specified Src. The IpIo was added previously by IpIoAddIp().

  @param[in, out]  IpIo              Pointer to the pointer of the IP_IO instance.
  @param[in]       IpVersion         The version of the IP protocol to use, either
                                     IPv4 or IPv6.
  @param[in]       Src               The local IP address.

  @return Pointer to the IP protocol can be used for sending purpose and its local
          address is the same with Src.

**/
IP_IO_IP_INFO *
EFIAPI
IpIoFindSender (
  IN OUT IP_IO           **IpIo,
  IN     IP_VERSION      IpVersion,
  IN     EFI_IP_ADDRESS  *Src
  )
{
  LIST_ENTRY      *IpIoEntry;
  IP_IO           *IpIoPtr;
  LIST_ENTRY      *IpInfoEntry;
  IP_IO_IP_INFO   *IpInfo;

  ASSERT ((IpVersion == IP_VERSION_4) || (IpVersion == IP_VERSION_6));  

  NET_LIST_FOR_EACH (IpIoEntry, &mActiveIpIoList) {
    IpIoPtr = NET_LIST_USER_STRUCT (IpIoEntry, IP_IO, Entry);

    if (((*IpIo != NULL) && (*IpIo != IpIoPtr)) || (IpIoPtr->IpVersion != IpVersion)) {
      continue;
    }

    NET_LIST_FOR_EACH (IpInfoEntry, &IpIoPtr->IpList) {
      IpInfo = NET_LIST_USER_STRUCT (IpInfoEntry, IP_IO_IP_INFO, Entry);
      if (IpInfo->IpVersion == IP_VERSION_4){

        if (EFI_IP4_EQUAL (&IpInfo->Addr.v4, &Src->v4)) {
          *IpIo = IpIoPtr;
          return IpInfo;
        }

      } else {

        if (EFI_IP6_EQUAL (&IpInfo->Addr.v6, &Src->v6)) {
          *IpIo = IpIoPtr;
          return IpInfo;       
        }
      }      

    }
  }

  //
  // No match.
  //
  return NULL;
}


/**
  Get the ICMP error map information.
  
  The ErrorStatus will be returned. The IsHard and Notify are optional. If they
  are not NULL, this routine will fill them.

  @param[in]   IcmpError             IcmpError Type.
  @param[in]   IpVersion             The version of the IP protocol to use,
                                     either IPv4 or IPv6.
  
  @param[out]  IsHard                Whether it is a hard error.
  @param[out]  Notify                Whether it need to notify SockError.

  @return ICMP Error Status, such as EFI_NETWORK_UNREACHABLE.

**/
EFI_STATUS
EFIAPI
IpIoGetIcmpErrStatus (
  IN  ICMP_ERROR  IcmpError,
  IN  IP_VERSION  IpVersion,
  OUT BOOLEAN     *IsHard  OPTIONAL,
  OUT BOOLEAN     *Notify  OPTIONAL
  )
{
  if (IpVersion == IP_VERSION_4 ) {
    ASSERT ((IcmpError >= ICMP_ERR_UNREACH_NET) && (IcmpError <= ICMP_ERR_PARAMPROB));

    if (IsHard != NULL) {
      *IsHard = mIcmpErrMap[IcmpError].IsHard;
    }

    if (Notify != NULL) {
      *Notify = mIcmpErrMap[IcmpError].Notify;
    }

    switch (IcmpError) {
    case ICMP_ERR_UNREACH_NET:
      return  EFI_NETWORK_UNREACHABLE;

    case ICMP_ERR_TIMXCEED_INTRANS:
    case ICMP_ERR_TIMXCEED_REASS:
    case ICMP_ERR_UNREACH_HOST:
      return  EFI_HOST_UNREACHABLE;

    case ICMP_ERR_UNREACH_PROTOCOL:
      return  EFI_PROTOCOL_UNREACHABLE;

    case ICMP_ERR_UNREACH_PORT:
      return  EFI_PORT_UNREACHABLE;

    case ICMP_ERR_MSGSIZE:
    case ICMP_ERR_UNREACH_SRCFAIL:
    case ICMP_ERR_QUENCH:
    case ICMP_ERR_PARAMPROB:
      return  EFI_ICMP_ERROR;

    default:
      ASSERT (FALSE);
      return EFI_UNSUPPORTED;
    }

  } else if (IpVersion == IP_VERSION_6) {

    ASSERT ((IcmpError >= ICMP6_ERR_UNREACH_NET) && (IcmpError <= ICMP6_ERR_PARAMPROB_IPV6OPTION));

    if (IsHard != NULL) {
      *IsHard = mIcmp6ErrMap[IcmpError].IsHard;
    }

    if (Notify != NULL) {
      *Notify = mIcmp6ErrMap[IcmpError].Notify;
    }

    switch (IcmpError) {
    case ICMP6_ERR_UNREACH_NET:
      return EFI_NETWORK_UNREACHABLE;

    case ICMP6_ERR_UNREACH_HOST:
    case ICMP6_ERR_TIMXCEED_HOPLIMIT:
    case ICMP6_ERR_TIMXCEED_REASS:       
      return EFI_HOST_UNREACHABLE;

    case ICMP6_ERR_UNREACH_PROTOCOL:
      return EFI_PROTOCOL_UNREACHABLE;
      
    case ICMP6_ERR_UNREACH_PORT:
      return EFI_PORT_UNREACHABLE;

    case ICMP6_ERR_PACKAGE_TOOBIG:
    case ICMP6_ERR_PARAMPROB_HEADER:
    case ICMP6_ERR_PARAMPROB_NEXHEADER:
    case ICMP6_ERR_PARAMPROB_IPV6OPTION:
      return EFI_ICMP_ERROR;

    default:
      ASSERT (FALSE);
      return EFI_UNSUPPORTED;
    }

  } else {
    //
    // Should never be here
    //
    ASSERT (FALSE);
    return EFI_UNSUPPORTED;
  }
}


/**
  Refresh the remote peer's Neighbor Cache entries.

  This function is called when the caller needs the IpIo to refresh the existing
  IPv6 neighbor cache entries since the neighbor is considered reachable by the 
  node has recently received a confirmation that packets sent recently to the 
  neighbor were received by its IP layer. 

  @param[in]   IpIo                  Pointer to an IP_IO instance
  @param[in]   Neighbor              The IP address of the neighbor
  @param[in]   Timeout               Time in 100-ns units that this entry will
                                     remain in the neighbor cache. A value of 
                                     zero means that the entry is permanent. 
                                     A value of non-zero means that the entry is 
                                     dynamic and will be deleted after Timeout.

  @retval      EFI_SUCCESS           The operation is completed successfully.
  @retval      EFI_NOT_STARTED       The IpIo is not configured.
  @retval      EFI_INVALID_PARAMETER Neighbor Address is invalid.
  @retval      EFI_NOT_FOUND         The neighbor cache entry is not in the 
                                     neighbor table.  
  @retval      EFI_OUT_OF_RESOURCES  Failed due to resource limit.

**/
EFI_STATUS
IpIoRefreshNeighbor (
  IN IP_IO           *IpIo,
  IN EFI_IP_ADDRESS  *Neighbor,
  IN UINT32          Timeout  
  )
{
  EFI_IP6_PROTOCOL  *Ip;

  if (!IpIo->IsConfigured || IpIo->IpVersion != IP_VERSION_6) {
    return EFI_NOT_STARTED;
  }

  Ip = (EFI_IP6_PROTOCOL *) (IpIo->Ip);

  return Ip->Neighbors (Ip, FALSE, &Neighbor->v6, NULL, Timeout, TRUE);
}