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
|
/** @file
Copyright (c) 2006 - 2007, Intel Corporation
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.
Module Name:
Udp4Io.c
Abstract:
Help functions to access UDP service, it is used by both the DHCP and MTFTP.
**/
#include <PiDxe.h>
#include <Protocol/Udp4.h>
#include <Library/UdpIoLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
STATIC
VOID
EFIAPI
UdpIoOnDgramSent (
IN EFI_EVENT Event,
IN VOID *Context
);
STATIC
VOID
EFIAPI
UdpIoOnDgramRcvd (
IN EFI_EVENT Event,
IN VOID *Context
);
/**
Wrap a transmit request into a UDP_TX_TOKEN.
@param UdpIo The UdpIo port to send packet to
@param Packet The user's packet
@param EndPoint The local and remote access point
@param Gateway The overrided next hop
@param CallBack The function to call when transmission completed.
@param Context The opaque parameter to the call back
@return The wrapped transmission request or NULL if failed to allocate resources.
**/
STATIC
UDP_TX_TOKEN *
UdpIoWrapTx (
IN UDP_IO_PORT *UdpIo,
IN NET_BUF *Packet,
IN UDP_POINTS *EndPoint, OPTIONAL
IN IP4_ADDR Gateway,
IN UDP_IO_CALLBACK CallBack,
IN VOID *Context
)
{
UDP_TX_TOKEN *Token;
EFI_UDP4_COMPLETION_TOKEN *UdpToken;
EFI_UDP4_TRANSMIT_DATA *UdpTxData;
EFI_STATUS Status;
UINT32 Count;
Token = NetAllocatePool (sizeof (UDP_TX_TOKEN) +
sizeof (EFI_UDP4_FRAGMENT_DATA) * (Packet->BlockOpNum - 1));
if (Token == NULL) {
return NULL;
}
Token->Signature = UDP_IO_TX_SIGNATURE;
NetListInit (&Token->Link);
Token->UdpIo = UdpIo;
Token->CallBack = CallBack;
Token->Packet = Packet;
Token->Context = Context;
UdpToken = &(Token->UdpToken);
UdpToken->Status = EFI_NOT_READY;
Status = gBS->CreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
UdpIoOnDgramSent,
Token,
&UdpToken->Event
);
if (EFI_ERROR (Status)) {
NetFreePool (Token);
return NULL;
}
UdpTxData = &Token->UdpTxData;
UdpToken->Packet.TxData = UdpTxData;
UdpTxData->UdpSessionData = NULL;
UdpTxData->GatewayAddress = NULL;
if (EndPoint != NULL) {
EFI_IP4 (Token->UdpSession.SourceAddress) = HTONL (EndPoint->LocalAddr);
EFI_IP4 (Token->UdpSession.DestinationAddress) = HTONL (EndPoint->RemoteAddr);
Token->UdpSession.SourcePort = EndPoint->LocalPort;
Token->UdpSession.DestinationPort = EndPoint->RemotePort;
UdpTxData->UdpSessionData = &Token->UdpSession;
}
if (Gateway != 0) {
EFI_IP4 (Token->Gateway) = HTONL (Gateway);
UdpTxData->GatewayAddress = &Token->Gateway;
}
UdpTxData->DataLength = Packet->TotalSize;
Count = Packet->BlockOpNum;
NetbufBuildExt (Packet, (NET_FRAGMENT *) UdpTxData->FragmentTable, &Count);
UdpTxData->FragmentCount = Count;
return Token;
}
/**
Free a UDP_TX_TOKEN. The event is closed and memory released.
@param Token The UDP_TX_TOKEN to release.
@return None
**/
VOID
UdpIoFreeTxToken (
IN UDP_TX_TOKEN *Token
)
{
gBS->CloseEvent (Token->UdpToken.Event);
NetFreePool (Token);
}
/**
Create a UDP_RX_TOKEN to wrap the request.
@param UdpIo The UdpIo to receive packets from
@param CallBack The function to call when receive finished.
@param Context The opaque parameter to the CallBack
@param HeadLen The head length to reserver for the packet.
@return The Wrapped request or NULL if failed to allocate resources.
**/
UDP_RX_TOKEN *
UdpIoCreateRxToken (
IN UDP_IO_PORT *UdpIo,
IN UDP_IO_CALLBACK CallBack,
IN VOID *Context,
IN UINT32 HeadLen
)
{
UDP_RX_TOKEN *Token;
EFI_STATUS Status;
Token = NetAllocatePool (sizeof (UDP_RX_TOKEN));
if (Token == NULL) {
return NULL;
}
Token->Signature = UDP_IO_RX_SIGNATURE;
Token->UdpIo = UdpIo;
Token->CallBack = CallBack;
Token->Context = Context;
Token->HeadLen = HeadLen;
Token->UdpToken.Status = EFI_NOT_READY;
Token->UdpToken.Packet.RxData = NULL;
Status = gBS->CreateEvent (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
UdpIoOnDgramRcvd,
Token,
&Token->UdpToken.Event
);
if (EFI_ERROR (Status)) {
NetFreePool (Token);
return NULL;
}
return Token;
}
/**
Free a receive request wrap.
@param Token The receive request to release.
@return None
**/
VOID
UdpIoFreeRxToken (
IN UDP_RX_TOKEN *Token
)
{
gBS->CloseEvent (Token->UdpToken.Event);
NetFreePool (Token);
}
/**
Create a UDP IO port to access the UDP service. It will
create and configure a UDP child.
@param Controller The controller that has the UDP service binding
protocol installed.
@param Image The image handle for the driver.
@param Configure The function to configure the created UDP child
@param Context The opaque parameter for the Configure funtion.
@return A point to just created UDP IO port or NULL if failed.
**/
UDP_IO_PORT *
UdpIoCreatePort (
IN EFI_HANDLE Controller,
IN EFI_HANDLE Image,
IN UDP_IO_CONFIG Configure,
IN VOID *Context
)
{
UDP_IO_PORT *UdpIo;
EFI_STATUS Status;
ASSERT (Configure != NULL);
UdpIo = NetAllocatePool (sizeof (UDP_IO_PORT));
if (UdpIo == NULL) {
return NULL;
}
UdpIo->Signature = UDP_IO_SIGNATURE;
NetListInit (&UdpIo->Link);
UdpIo->RefCnt = 1;
UdpIo->Controller = Controller;
UdpIo->Image = Image;
NetListInit (&UdpIo->SentDatagram);
UdpIo->RecvRequest = NULL;
UdpIo->UdpHandle = NULL;
//
// Create a UDP child then open and configure it
//
Status = NetLibCreateServiceChild (
Controller,
Image,
&gEfiUdp4ServiceBindingProtocolGuid,
&UdpIo->UdpHandle
);
if (EFI_ERROR (Status)) {
goto FREE_MEM;
}
Status = gBS->OpenProtocol (
UdpIo->UdpHandle,
&gEfiUdp4ProtocolGuid,
(VOID **) &UdpIo->Udp,
Image,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
goto FREE_CHILD;
}
if (EFI_ERROR (Configure (UdpIo, Context))) {
goto CLOSE_PROTOCOL;
}
Status = UdpIo->Udp->GetModeData (UdpIo->Udp, NULL, NULL, NULL, &UdpIo->SnpMode);
if (EFI_ERROR (Status)) {
goto CLOSE_PROTOCOL;
}
return UdpIo;
CLOSE_PROTOCOL:
gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp4ProtocolGuid, Image, Controller);
FREE_CHILD:
NetLibDestroyServiceChild (
Controller,
Image,
&gEfiUdp4ServiceBindingProtocolGuid,
UdpIo->UdpHandle
);
FREE_MEM:
NetFreePool (UdpIo);
return NULL;
}
/**
Cancel all the sent datagram that pass the selection of ToCancel.
If ToCancel is NULL, all the datagrams are cancelled.
@param UdpIo The UDP IO port to cancel packet
@param IoStatus The IoStatus to return to the packet owners.
@param ToCancel The select funtion to test whether to cancel this
packet or not.
@param Context The opaque parameter to the ToCancel.
@return None
**/
STATIC
VOID
UdpIoCancelDgrams (
IN UDP_IO_PORT *UdpIo,
IN EFI_STATUS IoStatus,
IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL
IN VOID *Context
)
{
NET_LIST_ENTRY *Entry;
NET_LIST_ENTRY *Next;
UDP_TX_TOKEN *Token;
NET_LIST_FOR_EACH_SAFE (Entry, Next, &UdpIo->SentDatagram) {
Token = NET_LIST_USER_STRUCT (Entry, UDP_TX_TOKEN, Link);
if ((ToCancel == NULL) || (ToCancel (Token, Context))) {
NetListRemoveEntry (Entry);
UdpIo->Udp->Cancel (UdpIo->Udp, &Token->UdpToken);
Token->CallBack (Token->Packet, NULL, IoStatus, Token->Context);
UdpIoFreeTxToken (Token);
}
}
}
/**
Free the UDP IO port and all its related resources including
all the transmitted packet.
@param UdpIo The UDP IO port to free.
@retval EFI_SUCCESS The UDP IO port is freed.
**/
EFI_STATUS
UdpIoFreePort (
IN UDP_IO_PORT *UdpIo
)
{
UDP_RX_TOKEN *RxToken;
//
// Cancel all the sent datagram and receive requests. The
// callbacks of transmit requests are executed to allow the
// caller to release the resource. The callback of receive
// request are NOT executed. This is because it is most
// likely that the current user of the UDP IO port is closing
// itself.
//
UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);
if ((RxToken = UdpIo->RecvRequest) != NULL) {
UdpIo->RecvRequest = NULL;
UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);
UdpIoFreeRxToken (RxToken);
}
//
// Close then destory the UDP child
//
gBS->CloseProtocol (
UdpIo->UdpHandle,
&gEfiUdp4ProtocolGuid,
UdpIo->Image,
UdpIo->Controller
);
NetLibDestroyServiceChild (
UdpIo->Controller,
UdpIo->Image,
&gEfiUdp4ServiceBindingProtocolGuid,
UdpIo->UdpHandle
);
if (!IsListEmpty(&UdpIo->Link)) {
NetListRemoveEntry (&UdpIo->Link);
}
NetFreePool (UdpIo);
return EFI_SUCCESS;
}
/**
Clean up the UDP IO port. It will release all the transmitted
datagrams and receive request. It will also configure NULL the
UDP child.
@param UdpIo UDP IO port to clean up.
@return None
**/
VOID
UdpIoCleanPort (
IN UDP_IO_PORT *UdpIo
)
{
UDP_RX_TOKEN *RxToken;
//
// Cancel all the sent datagram and receive requests.
//
UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);
if ((RxToken = UdpIo->RecvRequest) != NULL) {
UdpIo->RecvRequest = NULL;
UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);
UdpIoFreeRxToken (RxToken);
}
UdpIo->Udp->Configure (UdpIo->Udp, NULL);
}
/**
The callback function when the packet is sent by UDP.
It will remove the packet from the local list then call
the packet owner's callback function.
@param Event The event signalled.
@param Context The UDP TX Token.
@return None
**/
STATIC
VOID
EFIAPI
UdpIoOnDgramSent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
UDP_TX_TOKEN *Token;
Token = (UDP_TX_TOKEN *) Context;
ASSERT (Token->Signature == UDP_IO_TX_SIGNATURE);
NetListRemoveEntry (&Token->Link);
Token->CallBack (Token->Packet, NULL, Token->UdpToken.Status, Token->Context);
UdpIoFreeTxToken (Token);
}
/**
Send a packet through the UDP IO port.
@param UdpIo The UDP IO Port to send the packet through
@param Packet The packet to send
@param EndPoint The local and remote access point
@param Gateway The gateway to use
@param CallBack The call back function to call when packet is
transmitted or failed.
@param Context The opque parameter to the CallBack
@retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet
@retval EFI_SUCCESS The packet is successfully delivered to UDP for
transmission.
**/
EFI_STATUS
UdpIoSendDatagram (
IN UDP_IO_PORT *UdpIo,
IN NET_BUF *Packet,
IN UDP_POINTS *EndPoint, OPTIONAL
IN IP4_ADDR Gateway,
IN UDP_IO_CALLBACK CallBack,
IN VOID *Context
)
{
UDP_TX_TOKEN *Token;
EFI_STATUS Status;
Token = UdpIoWrapTx (UdpIo, Packet, EndPoint, Gateway, CallBack, Context);
if (Token == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = UdpIo->Udp->Transmit (UdpIo->Udp, &Token->UdpToken);
if (EFI_ERROR (Status)) {
UdpIoFreeTxToken (Token);
return Status;
}
NetListInsertHead (&UdpIo->SentDatagram, &Token->Link);
return EFI_SUCCESS;
}
/**
The selection function to cancel a single sent datagram.
@param Token The UDP TX token to test againist.
@param Context The context
@return TRUE if the packet is to be cancelled, otherwise FALSE.
**/
STATIC
BOOLEAN
UdpIoCancelSingleDgram (
IN UDP_TX_TOKEN *Token,
IN VOID *Context
)
{
NET_BUF *Packet;
Packet = (NET_BUF *) Context;
if (Token->Packet == Packet) {
return TRUE;
}
return FALSE;
}
/**
Cancel a single sent datagram.
@param UdpIo The UDP IO port to cancel the packet from
@param Packet The packet to cancel
@return None
**/
VOID
UdpIoCancelSentDatagram (
IN UDP_IO_PORT *UdpIo,
IN NET_BUF *Packet
)
{
UdpIoCancelDgrams (UdpIo, EFI_ABORTED, UdpIoCancelSingleDgram, Packet);
}
/**
Recycle the received UDP data.
@param Context The UDP_RX_TOKEN
@return None
**/
STATIC
VOID
UdpIoRecycleDgram (
IN VOID *Context
)
{
UDP_RX_TOKEN *Token;
Token = (UDP_RX_TOKEN *) Context;
gBS->SignalEvent (Token->UdpToken.Packet.RxData->RecycleSignal);
UdpIoFreeRxToken (Token);
}
/**
The event handle for UDP receive request. It will build
a NET_BUF from the recieved UDP data, then deliver it
to the receiver.
@param Event The UDP receive request event
@param Context The UDP RX token.
@return None
**/
STATIC
VOID
EFIAPI
UdpIoOnDgramRcvd (
IN EFI_EVENT Event,
IN VOID *Context
)
{
EFI_UDP4_COMPLETION_TOKEN *UdpToken;
EFI_UDP4_RECEIVE_DATA *UdpRxData;
EFI_UDP4_SESSION_DATA *UdpSession;
UDP_RX_TOKEN *Token;
UDP_POINTS Points;
NET_BUF *Netbuf;
Token = (UDP_RX_TOKEN *) Context;
ASSERT ((Token->Signature == UDP_IO_RX_SIGNATURE) &&
(Token == Token->UdpIo->RecvRequest));
//
// Clear the receive request first in case that the caller
// wants to restart the receive in the callback.
//
Token->UdpIo->RecvRequest = NULL;
UdpToken = &Token->UdpToken;
UdpRxData = UdpToken->Packet.RxData;
if (EFI_ERROR (UdpToken->Status) || (UdpRxData == NULL)) {
Token->CallBack (NULL, NULL, UdpToken->Status, Token->Context);
UdpIoFreeRxToken (Token);
goto ON_EXIT;
}
//
// Build a NET_BUF from the UDP receive data, then deliver it up.
//
Netbuf = NetbufFromExt (
(NET_FRAGMENT *) UdpRxData->FragmentTable,
UdpRxData->FragmentCount,
0,
(UINT32) Token->HeadLen,
UdpIoRecycleDgram,
Token
);
if (Netbuf == NULL) {
gBS->SignalEvent (UdpRxData->RecycleSignal);
Token->CallBack (NULL, NULL, EFI_OUT_OF_RESOURCES, Token->Context);
UdpIoFreeRxToken (Token);
goto ON_EXIT;
}
UdpSession = &UdpRxData->UdpSession;
Points.LocalAddr = EFI_NTOHL (UdpSession->DestinationAddress);
Points.LocalPort = UdpSession->DestinationPort;
Points.RemoteAddr = EFI_NTOHL (UdpSession->SourceAddress);
Points.RemotePort = UdpSession->SourcePort;
Token->CallBack (Netbuf, &Points, EFI_SUCCESS, Token->Context);
ON_EXIT:
return;
}
/**
Issue a receive request to the UDP IO port.
@param UdpIo The UDP IO port to recieve the packet from.
@param CallBack The call back function to execute when receive
finished.
@param Context The opque context to the call back
@param HeadLen The lenght of the application's header
@retval EFI_ALREADY_STARTED There is already a pending receive request. Only
one receive request is supported.
@retval EFI_OUT_OF_RESOURCES Failed to allocate some resource.
@retval EFI_SUCCESS The receive request is issued successfully.
**/
EFI_STATUS
UdpIoRecvDatagram (
IN UDP_IO_PORT *UdpIo,
IN UDP_IO_CALLBACK CallBack,
IN VOID *Context,
IN UINT32 HeadLen
)
{
UDP_RX_TOKEN *Token;
EFI_STATUS Status;
if (UdpIo->RecvRequest != NULL) {
return EFI_ALREADY_STARTED;
}
Token = UdpIoCreateRxToken (UdpIo, CallBack, Context, HeadLen);
if (Token == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = UdpIo->Udp->Receive (UdpIo->Udp, &Token->UdpToken);
if (EFI_ERROR (Status)) {
UdpIoFreeRxToken (Token);
return Status;
}
UdpIo->RecvRequest = Token;
return EFI_SUCCESS;
}
|