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
|
/** @file
Implement IP4 pesudo interface.
Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
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 "Ip4Impl.h"
//
// Mac address with all zero, used to determine whethter the ARP
// resolve succeeded. Failed ARP requests zero the MAC address buffer.
//
EFI_MAC_ADDRESS mZeroMacAddress;
/**
Callback funtion when frame transmission is finished. It will
call the frame owner's callback function to tell it the result.
@param[in] Context Context which is point to the token.
**/
VOID
EFIAPI
Ip4OnFrameSentDpc (
IN VOID *Context
);
/**
Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK.
@param[in] Event The transmit token's event.
@param[in] Context Context which is point to the token.
**/
VOID
EFIAPI
Ip4OnFrameSent (
IN EFI_EVENT Event,
IN VOID *Context
);
/**
Callback function when ARP request are finished. It will cancelled
all the queued frame if the ARP requests failed. Or transmit them
if the request succeed.
@param[in] Context The context of the callback, a point to the ARP
queue
**/
VOID
EFIAPI
Ip4OnArpResolvedDpc (
IN VOID *Context
);
/**
Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK.
@param Event The Arp request event.
@param Context The context of the callback, a point to the ARP
queue.
**/
VOID
EFIAPI
Ip4OnArpResolved (
IN EFI_EVENT Event,
IN VOID *Context
);
/**
Received a frame from MNP, wrap it in net buffer then deliver
it to IP's input function. The ownship of the packet also
transferred to IP. When Ip is finished with this packet, it
will call NetbufFree to release the packet, NetbufFree will
again call the Ip4RecycleFrame to signal MNP's event and free
the token used.
@param Context Context for the callback.
**/
VOID
EFIAPI
Ip4OnFrameReceivedDpc (
IN VOID *Context
);
/**
Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK.
@param Event The receive event delivered to MNP for receive.
@param Context Context for the callback.
**/
VOID
EFIAPI
Ip4OnFrameReceived (
IN EFI_EVENT Event,
IN VOID *Context
);
/**
Remove all the frames on the ARP queue that pass the FrameToCancel,
that is, either FrameToCancel is NULL or it returns true for the frame.
@param[in] ArpQue ARP frame to remove the frames from.
@param[in] IoStatus The status returned to the cancelled frames'
callback function.
@param[in] FrameToCancel Function to select which frame to cancel.
@param[in] Context Opaque parameter to the FrameToCancel.
**/
VOID
Ip4CancelFrameArp (
IN IP4_ARP_QUE *ArpQue,
IN EFI_STATUS IoStatus,
IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
IN VOID *Context
);
/**
Wrap a transmit request into a newly allocated IP4_LINK_TX_TOKEN.
@param[in] Interface The interface to send out to.
@param[in] IpInstance The IpInstance that transmit the packet. NULL if
the packet is sent by the IP4 driver itself.
@param[in] Packet The packet to transmit
@param[in] CallBack Call back function to execute if transmission
finished.
@param[in] Context Opaque parameter to the call back.
@retval Token The wrapped token if succeed
@retval NULL The wrapped token if NULL
**/
IP4_LINK_TX_TOKEN *
Ip4WrapLinkTxToken (
IN IP4_INTERFACE *Interface,
IN IP4_PROTOCOL *IpInstance OPTIONAL,
IN NET_BUF *Packet,
IN IP4_FRAME_CALLBACK CallBack,
IN VOID *Context
)
{
EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;
EFI_MANAGED_NETWORK_TRANSMIT_DATA *MnpTxData;
IP4_LINK_TX_TOKEN *Token;
EFI_STATUS Status;
UINT32 Count;
Token = AllocatePool (sizeof (IP4_LINK_TX_TOKEN) + \
(Packet->BlockOpNum - 1) * sizeof (EFI_MANAGED_NETWORK_FRAGMENT_DATA));
if (Token == NULL) {
return NULL;
}
Token->Signature = IP4_FRAME_TX_SIGNATURE;
InitializeListHead (&Token->Link);
Token->Interface = Interface;
Token->IpInstance = IpInstance;
Token->CallBack = CallBack;
Token->Packet = Packet;
Token->Context = Context;
CopyMem (&Token->DstMac, &mZeroMacAddress, sizeof (Token->DstMac));
CopyMem (&Token->SrcMac, &Interface->Mac, sizeof (Token->SrcMac));
MnpToken = &(Token->MnpToken);
MnpToken->Status = EFI_NOT_READY;
Status = gBS->CreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
Ip4OnFrameSent,
Token,
&MnpToken->Event
);
if (EFI_ERROR (Status)) {
FreePool (Token);
return NULL;
}
MnpTxData = &Token->MnpTxData;
MnpToken->Packet.TxData = MnpTxData;
MnpTxData->DestinationAddress = &Token->DstMac;
MnpTxData->SourceAddress = &Token->SrcMac;
MnpTxData->ProtocolType = IP4_ETHER_PROTO;
MnpTxData->DataLength = Packet->TotalSize;
MnpTxData->HeaderLength = 0;
Count = Packet->BlockOpNum;
NetbufBuildExt (Packet, (NET_FRAGMENT *) MnpTxData->FragmentTable, &Count);
MnpTxData->FragmentCount = (UINT16)Count;
return Token;
}
/**
Free the link layer transmit token. It will close the event
then free the memory used.
@param[in] Token Token to free
**/
VOID
Ip4FreeLinkTxToken (
IN IP4_LINK_TX_TOKEN *Token
)
{
NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE);
gBS->CloseEvent (Token->MnpToken.Event);
FreePool (Token);
}
/**
Create an IP_ARP_QUE structure to request ARP service.
@param[in] Interface The interface to send ARP from.
@param[in] DestIp The destination IP (host byte order) to request MAC
for
@return Point to newly created IP4_ARP_QUE if succeed, otherwise NULL.
**/
IP4_ARP_QUE *
Ip4CreateArpQue (
IN IP4_INTERFACE *Interface,
IN IP4_ADDR DestIp
)
{
IP4_ARP_QUE *ArpQue;
EFI_STATUS Status;
ArpQue = AllocatePool (sizeof (IP4_ARP_QUE));
if (ArpQue == NULL) {
return NULL;
}
ArpQue->Signature = IP4_FRAME_ARP_SIGNATURE;
InitializeListHead (&ArpQue->Link);
InitializeListHead (&ArpQue->Frames);
ArpQue->Interface = Interface;
Status = gBS->CreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
Ip4OnArpResolved,
ArpQue,
&ArpQue->OnResolved
);
if (EFI_ERROR (Status)) {
FreePool (ArpQue);
return NULL;
}
ArpQue->Ip = DestIp;
CopyMem (&ArpQue->Mac, &mZeroMacAddress, sizeof (ArpQue->Mac));
return ArpQue;
}
/**
Remove all the transmit requests queued on the ARP queue, then free it.
@param[in] ArpQue Arp queue to free
@param[in] IoStatus The transmit status returned to transmit requests'
callback.
**/
VOID
Ip4FreeArpQue (
IN IP4_ARP_QUE *ArpQue,
IN EFI_STATUS IoStatus
)
{
NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE);
//
// Remove all the frame waiting the ARP response
//
Ip4CancelFrameArp (ArpQue, IoStatus, NULL, NULL);
gBS->CloseEvent (ArpQue->OnResolved);
FreePool (ArpQue);
}
/**
Create a link layer receive token to wrap the receive request
@param[in] Interface The interface to receive from
@param[in] IpInstance The instance that request the receive (NULL for IP4
driver itself)
@param[in] CallBack Call back function to execute when finished.
@param[in] Context Opaque parameters to the callback
@return Point to created IP4_LINK_RX_TOKEN if succeed, otherwise NULL.
**/
IP4_LINK_RX_TOKEN *
Ip4CreateLinkRxToken (
IN IP4_INTERFACE *Interface,
IN IP4_PROTOCOL *IpInstance,
IN IP4_FRAME_CALLBACK CallBack,
IN VOID *Context
)
{
EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;
IP4_LINK_RX_TOKEN *Token;
EFI_STATUS Status;
Token = AllocatePool (sizeof (IP4_LINK_RX_TOKEN));
if (Token == NULL) {
return NULL;
}
Token->Signature = IP4_FRAME_RX_SIGNATURE;
Token->Interface = Interface;
Token->IpInstance = IpInstance;
Token->CallBack = CallBack;
Token->Context = Context;
MnpToken = &Token->MnpToken;
MnpToken->Status = EFI_NOT_READY;
Status = gBS->CreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
Ip4OnFrameReceived,
Token,
&MnpToken->Event
);
if (EFI_ERROR (Status)) {
FreePool (Token);
return NULL;
}
MnpToken->Packet.RxData = NULL;
return Token;
}
/**
Free the link layer request token. It will close the event
then free the memory used.
@param[in] Token Request token to free.
**/
VOID
Ip4FreeFrameRxToken (
IN IP4_LINK_RX_TOKEN *Token
)
{
NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE);
gBS->CloseEvent (Token->MnpToken.Event);
FreePool (Token);
}
/**
Remove all the frames on the ARP queue that pass the FrameToCancel,
that is, either FrameToCancel is NULL or it returns true for the frame.
@param[in] ArpQue ARP frame to remove the frames from.
@param[in] IoStatus The status returned to the cancelled frames'
callback function.
@param[in] FrameToCancel Function to select which frame to cancel.
@param[in] Context Opaque parameter to the FrameToCancel.
**/
VOID
Ip4CancelFrameArp (
IN IP4_ARP_QUE *ArpQue,
IN EFI_STATUS IoStatus,
IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
IN VOID *Context
)
{
LIST_ENTRY *Entry;
LIST_ENTRY *Next;
IP4_LINK_TX_TOKEN *Token;
NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {
Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);
if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) {
RemoveEntryList (Entry);
Token->CallBack (Token->IpInstance, Token->Packet, IoStatus, 0, Token->Context);
Ip4FreeLinkTxToken (Token);
}
}
}
/**
Remove all the frames on the interface that pass the FrameToCancel,
either queued on ARP queues or that have already been delivered to
MNP and not yet recycled.
@param[in] Interface Interface to remove the frames from.
@param[in] IoStatus The transmit status returned to the frames'
callback.
@param[in] FrameToCancel Function to select the frame to cancel, NULL to
select all.
@param[in] Context Opaque parameters passed to FrameToCancel.
**/
VOID
Ip4CancelFrames (
IN IP4_INTERFACE *Interface,
IN EFI_STATUS IoStatus,
IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,
IN VOID *Context
)
{
LIST_ENTRY *Entry;
LIST_ENTRY *Next;
IP4_ARP_QUE *ArpQue;
IP4_LINK_TX_TOKEN *Token;
//
// Cancel all the pending frames on ARP requests
//
NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->ArpQues) {
ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link);
Ip4CancelFrameArp (ArpQue, IoStatus, FrameToCancel, Context);
if (IsListEmpty (&ArpQue->Frames)) {
Interface->Arp->Cancel (Interface->Arp, &ArpQue->Ip, ArpQue->OnResolved);
}
}
//
// Cancel all the frames that have been delivered to MNP
// but not yet recycled.
//
NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->SentFrames) {
Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);
if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) {
Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken);
}
}
}
/**
Create an IP4_INTERFACE. Delay the creation of ARP instance until
the interface is configured.
@param[in] Mnp The shared MNP child of this IP4 service binding
instance.
@param[in] Controller The controller this IP4 service binding instance
is installed. Most like the UNDI handle.
@param[in] ImageHandle This driver's image handle.
@return Point to the created IP4_INTERFACE, otherwise NULL.
**/
IP4_INTERFACE *
Ip4CreateInterface (
IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
IN EFI_HANDLE Controller,
IN EFI_HANDLE ImageHandle
)
{
IP4_INTERFACE *Interface;
EFI_SIMPLE_NETWORK_MODE SnpMode;
Interface = AllocatePool (sizeof (IP4_INTERFACE));
if ((Interface == NULL) || (Mnp == NULL)) {
return NULL;
}
Interface->Signature = IP4_INTERFACE_SIGNATURE;
InitializeListHead (&Interface->Link);
Interface->RefCnt = 1;
Interface->Ip = IP4_ALLZERO_ADDRESS;
Interface->SubnetMask = IP4_ALLZERO_ADDRESS;
Interface->Configured = FALSE;
Interface->Controller = Controller;
Interface->Image = ImageHandle;
Interface->Mnp = Mnp;
Interface->Arp = NULL;
Interface->ArpHandle = NULL;
InitializeListHead (&Interface->ArpQues);
InitializeListHead (&Interface->SentFrames);
Interface->RecvRequest = NULL;
//
// Get the interface's Mac address and broadcast mac address from SNP
//
if (EFI_ERROR (Mnp->GetModeData (Mnp, NULL, &SnpMode))) {
FreePool (Interface);
return NULL;
}
CopyMem (&Interface->Mac, &SnpMode.CurrentAddress, sizeof (Interface->Mac));
CopyMem (&Interface->BroadcastMac, &SnpMode.BroadcastAddress, sizeof (Interface->BroadcastMac));
Interface->HwaddrLen = SnpMode.HwAddressSize;
InitializeListHead (&Interface->IpInstances);
Interface->PromiscRecv = FALSE;
return Interface;
}
/**
Set the interface's address, create and configure
the ARP child if necessary.
@param Interface The interface to set the address.
@param IpAddr The interface's IP address.
@param SubnetMask The interface's netmask.
@retval EFI_SUCCESS The interface is configured with Ip/netmask pair,
and a ARP is created for it.
@retval Others Failed to set the interface's address.
**/
EFI_STATUS
Ip4SetAddress (
IN OUT IP4_INTERFACE *Interface,
IN IP4_ADDR IpAddr,
IN IP4_ADDR SubnetMask
)
{
EFI_ARP_CONFIG_DATA ArpConfig;
EFI_STATUS Status;
NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);
//
// Set the ip/netmask, then compute the subnet broadcast
// and network broadcast for easy access. When computing
// nework broadcast, the subnet mask is most like longer
// than the default netmask (not subneted) as defined in
// RFC793. If that isn't the case, we are aggregating the
// networks, use the subnet's mask instead.
//
Interface->Ip = IpAddr;
Interface->SubnetMask = SubnetMask;
Interface->SubnetBrdcast = (IpAddr | ~SubnetMask);
Interface->NetBrdcast = (IpAddr | ~SubnetMask);
//
// Do clean up for Arp child
//
if (Interface->ArpHandle != NULL) {
if (Interface->Arp != NULL) {
gBS->CloseProtocol (
Interface->ArpHandle,
&gEfiArpProtocolGuid,
Interface->Image,
Interface->Controller
);
Interface->Arp = NULL;
}
NetLibDestroyServiceChild (
Interface->Controller,
Interface->Image,
&gEfiArpServiceBindingProtocolGuid,
&Interface->ArpHandle
);
Interface->ArpHandle = NULL;
}
//
// If the address is NOT all zero, create then configure an ARP child.
// Pay attention: DHCP configures its station address as 0.0.0.0/0
//
if (IpAddr != IP4_ALLZERO_ADDRESS) {
Status = NetLibCreateServiceChild (
Interface->Controller,
Interface->Image,
&gEfiArpServiceBindingProtocolGuid,
&Interface->ArpHandle
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gBS->OpenProtocol (
Interface->ArpHandle,
&gEfiArpProtocolGuid,
(VOID **) &Interface->Arp,
Interface->Image,
Interface->Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
IpAddr = HTONL (IpAddr);
ArpConfig.SwAddressType = IP4_ETHER_PROTO;
ArpConfig.SwAddressLength = 4;
ArpConfig.StationAddress = &IpAddr;
ArpConfig.EntryTimeOut = 0;
ArpConfig.RetryCount = 0;
ArpConfig.RetryTimeOut = 0;
Status = Interface->Arp->Configure (Interface->Arp, &ArpConfig);
if (EFI_ERROR (Status)) {
gBS->CloseProtocol (
Interface->ArpHandle,
&gEfiArpProtocolGuid,
Interface->Image,
Interface->Controller
);
goto ON_ERROR;
}
}
Interface->Configured = TRUE;
return EFI_SUCCESS;
ON_ERROR:
NetLibDestroyServiceChild (
Interface->Controller,
Interface->Image,
&gEfiArpServiceBindingProtocolGuid,
&Interface->ArpHandle
);
return Status;
}
/**
Filter function to cancel all the frame related to an IP instance.
@param[in] Frame The transmit request to test whether to cancel
@param[in] Context The context which is the Ip instance that issued
the transmit.
@retval TRUE The frame belongs to this instance and is to be
removed
@retval FALSE The frame doesn't belong to this instance.
**/
BOOLEAN
Ip4CancelInstanceFrame (
IN IP4_LINK_TX_TOKEN *Frame,
IN VOID *Context
)
{
if (Frame->IpInstance == (IP4_PROTOCOL *) Context) {
return TRUE;
}
return FALSE;
}
/**
If there is a pending receive request, cancel it. Don't call
the receive request's callback because this function can be only
called if the instance or driver is tearing itself down. It
doesn't make sense to call it back. But it is necessary to call
the transmit token's callback to give it a chance to free the
packet and update the upper layer's transmit request status, say
that from the UDP.
@param[in] Interface The interface used by the IpInstance
**/
VOID
Ip4CancelReceive (
IN IP4_INTERFACE *Interface
)
{
EFI_TPL OldTpl;
IP4_LINK_RX_TOKEN *Token;
if ((Token = Interface->RecvRequest) != NULL) {
OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
Interface->RecvRequest = NULL;
Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken);
gBS->RestoreTPL (OldTpl);
}
}
/**
Free the interface used by IpInstance. All the IP instance with
the same Ip/Netmask pair share the same interface. It is reference
counted. All the frames haven't been sent will be cancelled.
Because the IpInstance is optional, the caller must remove
IpInstance from the interface's instance list itself.
@param[in] Interface The interface used by the IpInstance.
@param[in] IpInstance The Ip instance that free the interface. NULL if
the Ip driver is releasing the default interface.
@retval EFI_SUCCESS The interface use IpInstance is freed.
**/
EFI_STATUS
Ip4FreeInterface (
IN IP4_INTERFACE *Interface,
IN IP4_PROTOCOL *IpInstance OPTIONAL
)
{
NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);
ASSERT (Interface->RefCnt > 0);
//
// Remove all the pending transmit token related to this IP instance.
//
Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, IpInstance);
if (--Interface->RefCnt > 0) {
return EFI_SUCCESS;
}
//
// Destroy the interface if this is the last IP instance that
// has the address. Remove all the system transmitted packets
// from this interface, cancel the receive request if there is
// one, and destroy the ARP requests.
//
Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, NULL);
Ip4CancelReceive (Interface);
ASSERT (IsListEmpty (&Interface->IpInstances));
ASSERT (IsListEmpty (&Interface->ArpQues));
ASSERT (IsListEmpty (&Interface->SentFrames));
if (Interface->Arp != NULL) {
gBS->CloseProtocol (
Interface->ArpHandle,
&gEfiArpProtocolGuid,
Interface->Image,
Interface->Controller
);
NetLibDestroyServiceChild (
Interface->Controller,
Interface->Image,
&gEfiArpServiceBindingProtocolGuid,
Interface->ArpHandle
);
}
RemoveEntryList (&Interface->Link);
FreePool (Interface);
return EFI_SUCCESS;
}
/**
Callback function when ARP request are finished. It will cancelled
all the queued frame if the ARP requests failed. Or transmit them
if the request succeed.
@param[in] Context The context of the callback, a point to the ARP
queue
**/
VOID
EFIAPI
Ip4OnArpResolvedDpc (
IN VOID *Context
)
{
LIST_ENTRY *Entry;
LIST_ENTRY *Next;
IP4_ARP_QUE *ArpQue;
IP4_INTERFACE *Interface;
IP4_LINK_TX_TOKEN *Token;
EFI_STATUS Status;
ArpQue = (IP4_ARP_QUE *) Context;
NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE);
RemoveEntryList (&ArpQue->Link);
//
// ARP resolve failed for some reason. Release all the frame
// and ARP queue itself. Ip4FreeArpQue will call the frame's
// owner back.
//
if (NET_MAC_EQUAL (&ArpQue->Mac, &mZeroMacAddress, ArpQue->Interface->HwaddrLen)) {
Ip4FreeArpQue (ArpQue, EFI_NO_MAPPING);
return ;
}
//
// ARP resolve succeeded, Transmit all the frame. Release the ARP
// queue. It isn't necessary for us to cache the ARP binding because
// we always check the ARP cache first before transmit.
//
Interface = ArpQue->Interface;
NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {
RemoveEntryList (Entry);
Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);
CopyMem (&Token->DstMac, &ArpQue->Mac, sizeof (Token->DstMac));
//
// Insert the tx token before transmitting it via MNP as the FrameSentDpc
// may be called before Mnp->Transmit returns which will remove this tx
// token from the SentFrames list. Remove it from the list if the returned
// Status of Mnp->Transmit is not EFI_SUCCESS as in this case the
// FrameSentDpc won't be queued.
//
InsertTailList (&Interface->SentFrames, &Token->Link);
Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken);
if (EFI_ERROR (Status)) {
RemoveEntryList (&Token->Link);
Token->CallBack (Token->IpInstance, Token->Packet, Status, 0, Token->Context);
Ip4FreeLinkTxToken (Token);
continue;
}
}
Ip4FreeArpQue (ArpQue, EFI_SUCCESS);
}
/**
Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK.
@param Event The Arp request event.
@param Context The context of the callback, a point to the ARP
queue.
**/
VOID
EFIAPI
Ip4OnArpResolved (
IN EFI_EVENT Event,
IN VOID *Context
)
{
//
// Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK
//
QueueDpc (TPL_CALLBACK, Ip4OnArpResolvedDpc, Context);
}
/**
Callback funtion when frame transmission is finished. It will
call the frame owner's callback function to tell it the result.
@param[in] Context Context which is point to the token.
**/
VOID
EFIAPI
Ip4OnFrameSentDpc (
IN VOID *Context
)
{
IP4_LINK_TX_TOKEN *Token;
Token = (IP4_LINK_TX_TOKEN *) Context;
NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE);
RemoveEntryList (&Token->Link);
Token->CallBack (
Token->IpInstance,
Token->Packet,
Token->MnpToken.Status,
0,
Token->Context
);
Ip4FreeLinkTxToken (Token);
}
/**
Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK.
@param[in] Event The transmit token's event.
@param[in] Context Context which is point to the token.
**/
VOID
EFIAPI
Ip4OnFrameSent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
//
// Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK
//
QueueDpc (TPL_CALLBACK, Ip4OnFrameSentDpc, Context);
}
/**
Send a frame from the interface. If the next hop is broadcast or
multicast address, it is transmitted immediately. If the next hop
is a unicast, it will consult ARP to resolve the NextHop's MAC.
If some error happened, the CallBack won't be called. So, the caller
must test the return value, and take action when there is an error.
@param[in] Interface The interface to send the frame from
@param[in] IpInstance The IP child that request the transmission. NULL
if it is the IP4 driver itself.
@param[in] Packet The packet to transmit.
@param[in] NextHop The immediate destination to transmit the packet
to.
@param[in] CallBack Function to call back when transmit finished.
@param[in] Context Opaque parameter to the call back.
@retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame
@retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop
@retval EFI_SUCCESS The packet is successfully transmitted.
@retval other Other error occurs.
**/
EFI_STATUS
Ip4SendFrame (
IN IP4_INTERFACE *Interface,
IN IP4_PROTOCOL *IpInstance OPTIONAL,
IN NET_BUF *Packet,
IN IP4_ADDR NextHop,
IN IP4_FRAME_CALLBACK CallBack,
IN VOID *Context
)
{
IP4_LINK_TX_TOKEN *Token;
LIST_ENTRY *Entry;
IP4_ARP_QUE *ArpQue;
EFI_ARP_PROTOCOL *Arp;
EFI_STATUS Status;
ASSERT (Interface->Configured);
Token = Ip4WrapLinkTxToken (Interface, IpInstance, Packet, CallBack, Context);
if (Token == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Get the destination MAC address for multicast and broadcasts.
// Don't depend on ARP to solve the address since there maybe no
// ARP at all. Ip4Output has set NextHop to 255.255.255.255 for
// all the broadcasts.
//
if (NextHop == IP4_ALLONE_ADDRESS) {
CopyMem (&Token->DstMac, &Interface->BroadcastMac, sizeof (Token->DstMac));
goto SEND_NOW;
} else if (IP4_IS_MULTICAST (NextHop)) {
Status = Ip4GetMulticastMac (Interface->Mnp, NextHop, &Token->DstMac);
if (EFI_ERROR (Status)) {
goto ON_ERROR;
}
goto SEND_NOW;
}
//
// Can only send out multicast/broadcast if the IP address is zero
//
if ((Arp = Interface->Arp) == NULL) {
Status = EFI_NO_MAPPING;
goto ON_ERROR;
}
//
// First check whether this binding is in the ARP cache.
//
NextHop = HTONL (NextHop);
Status = Arp->Request (Arp, &NextHop, NULL, &Token->DstMac);
if (Status == EFI_SUCCESS) {
goto SEND_NOW;
} else if (Status != EFI_NOT_READY) {
goto ON_ERROR;
}
//
// Have to do asynchronous ARP resolution. First check
// whether there is already a pending request.
//
ArpQue = NULL;
NET_LIST_FOR_EACH (Entry, &Interface->ArpQues) {
ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link);
if (ArpQue->Ip == NextHop) {
break;
}
}
//
// Found a pending ARP request, enqueue the frame then return
//
if (Entry != &Interface->ArpQues) {
InsertTailList (&ArpQue->Frames, &Token->Link);
return EFI_SUCCESS;
}
//
// First frame to NextHop, issue an asynchronous ARP requests
//
ArpQue = Ip4CreateArpQue (Interface, NextHop);
if (ArpQue == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto ON_ERROR;
}
Status = Arp->Request (Arp, &ArpQue->Ip, ArpQue->OnResolved, ArpQue->Mac.Addr);
if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {
Ip4FreeArpQue (ArpQue, EFI_NO_MAPPING);
goto ON_ERROR;
}
InsertHeadList (&ArpQue->Frames, &Token->Link);
InsertHeadList (&Interface->ArpQues, &ArpQue->Link);
return EFI_SUCCESS;
SEND_NOW:
//
// Insert the tx token into the SentFrames list before calling Mnp->Transmit.
// Remove it if the returned status is not EFI_SUCCESS.
//
InsertTailList (&Interface->SentFrames, &Token->Link);
Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken);
if (EFI_ERROR (Status)) {
RemoveEntryList (&Token->Link);
goto ON_ERROR;
}
return EFI_SUCCESS;
ON_ERROR:
Ip4FreeLinkTxToken (Token);
return Status;
}
/**
Call back function when the received packet is freed.
Check Ip4OnFrameReceived for information.
@param Context Context, which is the IP4_LINK_RX_TOKEN.
**/
VOID
EFIAPI
Ip4RecycleFrame (
IN VOID *Context
)
{
IP4_LINK_RX_TOKEN *Frame;
Frame = (IP4_LINK_RX_TOKEN *) Context;
NET_CHECK_SIGNATURE (Frame, IP4_FRAME_RX_SIGNATURE);
gBS->SignalEvent (Frame->MnpToken.Packet.RxData->RecycleEvent);
Ip4FreeFrameRxToken (Frame);
}
/**
Received a frame from MNP, wrap it in net buffer then deliver
it to IP's input function. The ownship of the packet also
transferred to IP. When Ip is finished with this packet, it
will call NetbufFree to release the packet, NetbufFree will
again call the Ip4RecycleFrame to signal MNP's event and free
the token used.
@param Context Context for the callback.
**/
VOID
EFIAPI
Ip4OnFrameReceivedDpc (
IN VOID *Context
)
{
EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;
EFI_MANAGED_NETWORK_RECEIVE_DATA *MnpRxData;
IP4_LINK_RX_TOKEN *Token;
NET_FRAGMENT Netfrag;
NET_BUF *Packet;
UINT32 Flag;
Token = (IP4_LINK_RX_TOKEN *) Context;
NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE);
//
// First clear the interface's receive request in case the
// caller wants to call Ip4ReceiveFrame in the callback.
//
Token->Interface->RecvRequest = NULL;
MnpToken = &Token->MnpToken;
MnpRxData = MnpToken->Packet.RxData;
if (EFI_ERROR (MnpToken->Status) || (MnpRxData == NULL)) {
Token->CallBack (Token->IpInstance, NULL, MnpToken->Status, 0, Token->Context);
Ip4FreeFrameRxToken (Token);
return ;
}
//
// Wrap the frame in a net buffer then deliever it to IP input.
// IP will reassemble the packet, and deliver it to upper layer
//
Netfrag.Len = MnpRxData->DataLength;
Netfrag.Bulk = MnpRxData->PacketData;
Packet = NetbufFromExt (&Netfrag, 1, 0, IP4_MAX_HEADLEN, Ip4RecycleFrame, Token);
if (Packet == NULL) {
gBS->SignalEvent (MnpRxData->RecycleEvent);
Token->CallBack (Token->IpInstance, NULL, EFI_OUT_OF_RESOURCES, 0, Token->Context);
Ip4FreeFrameRxToken (Token);
return ;
}
Flag = (MnpRxData->BroadcastFlag ? IP4_LINK_BROADCAST : 0);
Flag |= (MnpRxData->MulticastFlag ? IP4_LINK_MULTICAST : 0);
Flag |= (MnpRxData->PromiscuousFlag ? IP4_LINK_PROMISC : 0);
Token->CallBack (Token->IpInstance, Packet, EFI_SUCCESS, Flag, Token->Context);
}
/**
Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK.
@param Event The receive event delivered to MNP for receive.
@param Context Context for the callback.
**/
VOID
EFIAPI
Ip4OnFrameReceived (
IN EFI_EVENT Event,
IN VOID *Context
)
{
//
// Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK
//
QueueDpc (TPL_CALLBACK, Ip4OnFrameReceivedDpc, Context);
}
/**
Request to receive the packet from the interface.
@param[in] Interface The interface to receive the frames from.
@param[in] IpInstance The instance that requests the receive. NULL for
the driver itself.
@param[in] CallBack Function to call when receive finished.
@param[in] Context Opaque parameter to the callback.
@retval EFI_ALREADY_STARTED There is already a pending receive request.
@retval EFI_OUT_OF_RESOURCES Failed to allocate resource to receive.
@retval EFI_SUCCESS The recieve request has been started.
@retval other Other error occurs.
**/
EFI_STATUS
Ip4ReceiveFrame (
IN IP4_INTERFACE *Interface,
IN IP4_PROTOCOL *IpInstance OPTIONAL,
IN IP4_FRAME_CALLBACK CallBack,
IN VOID *Context
)
{
IP4_LINK_RX_TOKEN *Token;
EFI_STATUS Status;
NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);
if (Interface->RecvRequest != NULL) {
return EFI_ALREADY_STARTED;
}
Token = Ip4CreateLinkRxToken (Interface, IpInstance, CallBack, Context);
if (Token == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Interface->RecvRequest = Token;
Status = Interface->Mnp->Receive (Interface->Mnp, &Token->MnpToken);
if (EFI_ERROR (Status)) {
Interface->RecvRequest = NULL;
Ip4FreeFrameRxToken (Token);
return Status;
}
return EFI_SUCCESS;
}
|