]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/EfiSocketLib/Udp4.c
ShellPkg: Fix ICC11 build failure.
[mirror_edk2.git] / StdLib / EfiSocketLib / Udp4.c
1 /** @file
2 Implement the UDP4 driver support for the socket layer.
3
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Socket.h"
16
17
18 /**
19 Get the local socket address
20
21 This routine returns the IPv4 address and UDP port number associated
22 with the local socket.
23
24 This routine is called by ::EslSocketGetLocalAddress to determine the
25 network address for the SOCK_DGRAM socket.
26
27 @param [in] pPort Address of an ::ESL_PORT structure.
28
29 @param [out] pSockAddr Network address to receive the local system address
30
31 **/
32 VOID
33 EslUdp4LocalAddressGet (
34 IN ESL_PORT * pPort,
35 OUT struct sockaddr * pSockAddr
36 )
37 {
38 struct sockaddr_in * pLocalAddress;
39 ESL_UDP4_CONTEXT * pUdp4;
40
41 DBG_ENTER ( );
42
43 //
44 // Return the local address
45 //
46 pUdp4 = &pPort->Context.Udp4;
47 pLocalAddress = (struct sockaddr_in *)pSockAddr;
48 pLocalAddress->sin_family = AF_INET;
49 pLocalAddress->sin_port = SwapBytes16 ( pUdp4->ConfigData.StationPort );
50 CopyMem ( &pLocalAddress->sin_addr,
51 &pUdp4->ConfigData.StationAddress.Addr[0],
52 sizeof ( pLocalAddress->sin_addr ));
53
54 DBG_EXIT ( );
55 }
56
57
58 /**
59 Set the local port address.
60
61 This routine sets the local port address.
62
63 This support routine is called by ::EslSocketPortAllocate.
64
65 @param [in] pPort Address of an ESL_PORT structure
66 @param [in] pSockAddr Address of a sockaddr structure that contains the
67 connection point on the local machine. An IPv4 address
68 of INADDR_ANY specifies that the connection is made to
69 all of the network stacks on the platform. Specifying a
70 specific IPv4 address restricts the connection to the
71 network stack supporting that address. Specifying zero
72 for the port causes the network layer to assign a port
73 number from the dynamic range. Specifying a specific
74 port number causes the network layer to use that port.
75
76 @param [in] bBindTest TRUE = run bind testing
77
78 @retval EFI_SUCCESS The operation was successful
79
80 **/
81 EFI_STATUS
82 EslUdp4LocalAddressSet (
83 IN ESL_PORT * pPort,
84 IN CONST struct sockaddr * pSockAddr,
85 IN BOOLEAN bBindTest
86 )
87 {
88 EFI_UDP4_CONFIG_DATA * pConfig;
89 CONST struct sockaddr_in * pIpAddress;
90 CONST UINT8 * pIpv4Address;
91 EFI_STATUS Status;
92
93 DBG_ENTER ( );
94
95 //
96 // Validate the address
97 //
98 pIpAddress = (struct sockaddr_in *)pSockAddr;
99 if ( INADDR_BROADCAST == pIpAddress->sin_addr.s_addr ) {
100 //
101 // The local address must not be the broadcast address
102 //
103 Status = EFI_INVALID_PARAMETER;
104 pPort->pSocket->errno = EADDRNOTAVAIL;
105 }
106 else {
107 //
108 // Set the local address
109 //
110 pIpAddress = (struct sockaddr_in *)pSockAddr;
111 pIpv4Address = (UINT8 *)&pIpAddress->sin_addr.s_addr;
112 pConfig = &pPort->Context.Udp4.ConfigData;
113 pConfig->StationAddress.Addr[0] = pIpv4Address[0];
114 pConfig->StationAddress.Addr[1] = pIpv4Address[1];
115 pConfig->StationAddress.Addr[2] = pIpv4Address[2];
116 pConfig->StationAddress.Addr[3] = pIpv4Address[3];
117
118 //
119 // Determine if the default address is used
120 //
121 pConfig->UseDefaultAddress = (BOOLEAN)( 0 == pIpAddress->sin_addr.s_addr );
122
123 //
124 // Set the subnet mask
125 //
126 if ( pConfig->UseDefaultAddress ) {
127 pConfig->SubnetMask.Addr[0] = 0;
128 pConfig->SubnetMask.Addr[1] = 0;
129 pConfig->SubnetMask.Addr[2] = 0;
130 pConfig->SubnetMask.Addr[3] = 0;
131 }
132 else {
133 pConfig->SubnetMask.Addr[0] = 0xff;
134 pConfig->SubnetMask.Addr[1] = 0xff;
135 pConfig->SubnetMask.Addr[2] = 0xff;
136 pConfig->SubnetMask.Addr[3] = 0xff;
137 }
138
139 //
140 // Validate the IP address
141 //
142 pConfig->StationPort = 0;
143 Status = bBindTest ? EslSocketBindTest ( pPort, EADDRNOTAVAIL )
144 : EFI_SUCCESS;
145 if ( !EFI_ERROR ( Status )) {
146 //
147 // Set the port number
148 //
149 pConfig->StationPort = SwapBytes16 ( pIpAddress->sin_port );
150
151 //
152 // Display the local address
153 //
154 DEBUG (( DEBUG_BIND,
155 "0x%08x: Port, Local UDP4 Address: %d.%d.%d.%d:%d\r\n",
156 pPort,
157 pConfig->StationAddress.Addr[0],
158 pConfig->StationAddress.Addr[1],
159 pConfig->StationAddress.Addr[2],
160 pConfig->StationAddress.Addr[3],
161 pConfig->StationPort ));
162 }
163 }
164
165 //
166 // Return the operation status
167 //
168 DBG_EXIT_STATUS ( Status );
169 return Status;
170 }
171
172
173 /**
174 Free a receive packet
175
176 This routine performs the network specific operations necessary
177 to free a receive packet.
178
179 This routine is called by ::EslSocketPortCloseTxDone to free a
180 receive packet.
181
182 @param [in] pPacket Address of an ::ESL_PACKET structure.
183 @param [in, out] pRxBytes Address of the count of RX bytes
184
185 **/
186 VOID
187 EslUdp4PacketFree (
188 IN ESL_PACKET * pPacket,
189 IN OUT size_t * pRxBytes
190 )
191 {
192 EFI_UDP4_RECEIVE_DATA * pRxData;
193
194 DBG_ENTER ( );
195
196 //
197 // Account for the receive bytes
198 //
199 pRxData = pPacket->Op.Udp4Rx.pRxData;
200 *pRxBytes -= pRxData->DataLength;
201
202 //
203 // Disconnect the buffer from the packet
204 //
205 pPacket->Op.Udp4Rx.pRxData = NULL;
206
207 //
208 // Return the buffer to the UDP4 driver
209 //
210 gBS->SignalEvent ( pRxData->RecycleSignal );
211 DBG_EXIT ( );
212 }
213
214
215 /**
216 Initialize the network specific portions of an ::ESL_PORT structure.
217
218 This routine initializes the network specific portions of an
219 ::ESL_PORT structure for use by the socket.
220
221 This support routine is called by ::EslSocketPortAllocate
222 to connect the socket with the underlying network adapter
223 running the UDPv4 protocol.
224
225 @param [in] pPort Address of an ESL_PORT structure
226 @param [in] DebugFlags Flags for debug messages
227
228 @retval EFI_SUCCESS - Socket successfully created
229
230 **/
231 EFI_STATUS
232 EslUdp4PortAllocate (
233 IN ESL_PORT * pPort,
234 IN UINTN DebugFlags
235 )
236 {
237 EFI_UDP4_CONFIG_DATA * pConfig;
238 ESL_SOCKET * pSocket;
239 EFI_STATUS Status;
240
241 DBG_ENTER ( );
242
243 //
244 // Initialize the port
245 //
246 pSocket = pPort->pSocket;
247 pSocket->TxPacketOffset = OFFSET_OF ( ESL_PACKET, Op.Udp4Tx.TxData );
248 pSocket->TxTokenEventOffset = OFFSET_OF ( ESL_IO_MGMT, Token.Udp4Tx.Event );
249 pSocket->TxTokenOffset = OFFSET_OF ( EFI_UDP4_COMPLETION_TOKEN, Packet.TxData );
250
251 //
252 // Save the cancel, receive and transmit addresses
253 //
254 pPort->pfnConfigure = (PFN_NET_CONFIGURE)pPort->pProtocol.UDPv4->Configure;
255 pPort->pfnRxCancel = (PFN_NET_IO_START)pPort->pProtocol.UDPv4->Cancel;
256 pPort->pfnRxPoll = (PFN_NET_POLL)pPort->pProtocol.UDPv4->Poll;
257 pPort->pfnRxStart = (PFN_NET_IO_START)pPort->pProtocol.UDPv4->Receive;
258 pPort->pfnTxStart = (PFN_NET_IO_START)pPort->pProtocol.UDPv4->Transmit;
259
260 //
261 // Set the configuration flags
262 //
263 pConfig = &pPort->Context.Udp4.ConfigData;
264 pConfig->TimeToLive = 255;
265 pConfig->AcceptAnyPort = FALSE;
266 pConfig->AcceptBroadcast = FALSE;
267 pConfig->AcceptPromiscuous = FALSE;
268 pConfig->AllowDuplicatePort = TRUE;
269 pConfig->DoNotFragment = TRUE;
270 Status = EFI_SUCCESS;
271
272 //
273 // Return the operation status
274 //
275 DBG_EXIT_STATUS ( Status );
276 return Status;
277 }
278
279
280 /**
281 Receive data from a network connection.
282
283 This routine attempts to return buffered data to the caller. The
284 data is removed from the urgent queue if the message flag MSG_OOB
285 is specified, otherwise data is removed from the normal queue.
286 See the \ref ReceiveEngine section.
287
288 This routine is called by ::EslSocketReceive to handle the network
289 specific receive operation to support SOCK_DGRAM sockets.
290
291 @param [in] pPort Address of an ::ESL_PORT structure.
292
293 @param [in] pPacket Address of an ::ESL_PACKET structure.
294
295 @param [in] pbConsumePacket Address of a BOOLEAN indicating if the packet is to be consumed
296
297 @param [in] BufferLength Length of the the buffer
298
299 @param [in] pBuffer Address of a buffer to receive the data.
300
301 @param [in] pDataLength Number of received data bytes in the buffer.
302
303 @param [out] pAddress Network address to receive the remote system address
304
305 @param [out] pSkipBytes Address to receive the number of bytes skipped
306
307 @return Returns the address of the next free byte in the buffer.
308
309 **/
310 UINT8 *
311 EslUdp4Receive (
312 IN ESL_PORT * pPort,
313 IN ESL_PACKET * pPacket,
314 IN BOOLEAN * pbConsumePacket,
315 IN size_t BufferLength,
316 IN UINT8 * pBuffer,
317 OUT size_t * pDataLength,
318 OUT struct sockaddr * pAddress,
319 OUT size_t * pSkipBytes
320 )
321 {
322 size_t DataBytes;
323 struct sockaddr_in * pRemoteAddress;
324 EFI_UDP4_RECEIVE_DATA * pRxData;
325
326 DBG_ENTER ( );
327
328 pRxData = pPacket->Op.Udp4Rx.pRxData;
329 //
330 // Return the remote system address if requested
331 //
332 if ( NULL != pAddress ) {
333 //
334 // Build the remote address
335 //
336 DEBUG (( DEBUG_RX,
337 "Getting packet remote address: %d.%d.%d.%d:%d\r\n",
338 pRxData->UdpSession.SourceAddress.Addr[0],
339 pRxData->UdpSession.SourceAddress.Addr[1],
340 pRxData->UdpSession.SourceAddress.Addr[2],
341 pRxData->UdpSession.SourceAddress.Addr[3],
342 pRxData->UdpSession.SourcePort ));
343 pRemoteAddress = (struct sockaddr_in *)pAddress;
344 CopyMem ( &pRemoteAddress->sin_addr,
345 &pRxData->UdpSession.SourceAddress.Addr[0],
346 sizeof ( pRemoteAddress->sin_addr ));
347 pRemoteAddress->sin_port = SwapBytes16 ( pRxData->UdpSession.SourcePort );
348 }
349
350 //
351 // Copy the received data
352 //
353 pBuffer = EslSocketCopyFragmentedBuffer ( pRxData->FragmentCount,
354 (EFI_IP4_FRAGMENT_DATA *)&pRxData->FragmentTable[0],
355 BufferLength,
356 pBuffer,
357 &DataBytes );
358
359 //
360 // Determine if the data is being read
361 //
362 if ( *pbConsumePacket ) {
363 //
364 // Display for the bytes consumed
365 //
366 DEBUG (( DEBUG_RX,
367 "0x%08x: Port account for 0x%08x bytes\r\n",
368 pPort,
369 DataBytes ));
370
371 //
372 // Account for any discarded data
373 //
374 *pSkipBytes = pRxData->DataLength - DataBytes;
375 }
376
377 //
378 // Return the data length and the buffer address
379 //
380 *pDataLength = DataBytes;
381 DBG_EXIT_HEX ( pBuffer );
382 return pBuffer;
383 }
384
385
386 /**
387 Get the remote socket address
388
389 This routine returns the address of the remote connection point
390 associated with the SOCK_DGRAM socket.
391
392 This routine is called by ::EslSocketGetPeerAddress to detemine
393 the UDPv4 address and port number associated with the network adapter.
394
395 @param [in] pPort Address of an ::ESL_PORT structure.
396
397 @param [out] pAddress Network address to receive the remote system address
398
399 **/
400 VOID
401 EslUdp4RemoteAddressGet (
402 IN ESL_PORT * pPort,
403 OUT struct sockaddr * pAddress
404 )
405 {
406 struct sockaddr_in * pRemoteAddress;
407 ESL_UDP4_CONTEXT * pUdp4;
408
409 DBG_ENTER ( );
410
411 //
412 // Return the remote address
413 //
414 pUdp4 = &pPort->Context.Udp4;
415 pRemoteAddress = (struct sockaddr_in *)pAddress;
416 pRemoteAddress->sin_family = AF_INET;
417 pRemoteAddress->sin_port = SwapBytes16 ( pUdp4->ConfigData.RemotePort );
418 CopyMem ( &pRemoteAddress->sin_addr,
419 &pUdp4->ConfigData.RemoteAddress.Addr[0],
420 sizeof ( pRemoteAddress->sin_addr ));
421
422 DBG_EXIT ( );
423 }
424
425
426 /**
427 Set the remote address
428
429 This routine sets the remote address in the port.
430
431 This routine is called by ::EslSocketConnect to specify the
432 remote network address.
433
434 @param [in] pPort Address of an ::ESL_PORT structure.
435
436 @param [in] pSockAddr Network address of the remote system.
437
438 @param [in] SockAddrLength Length in bytes of the network address.
439
440 @retval EFI_SUCCESS The operation was successful
441
442 **/
443 EFI_STATUS
444 EslUdp4RemoteAddressSet (
445 IN ESL_PORT * pPort,
446 IN CONST struct sockaddr * pSockAddr,
447 IN socklen_t SockAddrLength
448 )
449 {
450 CONST struct sockaddr_in * pRemoteAddress;
451 ESL_UDP4_CONTEXT * pUdp4;
452 EFI_STATUS Status;
453
454 DBG_ENTER ( );
455
456 //
457 // Set the remote address
458 //
459 pUdp4 = &pPort->Context.Udp4;
460 pRemoteAddress = (struct sockaddr_in *)pSockAddr;
461 pUdp4->ConfigData.RemoteAddress.Addr[0] = (UINT8)( pRemoteAddress->sin_addr.s_addr );
462 pUdp4->ConfigData.RemoteAddress.Addr[1] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 8 );
463 pUdp4->ConfigData.RemoteAddress.Addr[2] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 16 );
464 pUdp4->ConfigData.RemoteAddress.Addr[3] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 24 );
465 pUdp4->ConfigData.RemotePort = SwapBytes16 ( pRemoteAddress->sin_port );
466 Status = EFI_SUCCESS;
467
468 //
469 // Return the operation status
470 //
471 DBG_EXIT_STATUS ( Status );
472 return Status;
473 }
474
475
476 /**
477 Process the receive completion
478
479 This routine keeps the UDPv4 driver's buffer and queues it in
480 in FIFO order to the data queue. The UDP4 driver's buffer will
481 be returned by either ::EslUdp4Receive or ::EslSocketPortCloseTxDone.
482 See the \ref ReceiveEngine section.
483
484 This routine is called by the UDPv4 driver when data is
485 received.
486
487 @param [in] Event The receive completion event
488
489 @param [in] pIo Address of an ::ESL_IO_MGMT structure
490
491 **/
492 VOID
493 EslUdp4RxComplete (
494 IN EFI_EVENT Event,
495 IN ESL_IO_MGMT * pIo
496 )
497 {
498 size_t LengthInBytes;
499 ESL_PACKET * pPacket;
500 EFI_UDP4_RECEIVE_DATA * pRxData;
501 EFI_STATUS Status;
502
503 DBG_ENTER ( );
504
505 //
506 // Get the operation status.
507 //
508 Status = pIo->Token.Udp4Rx.Status;
509
510 //
511 // Get the packet length
512 //
513 pRxData = pIo->Token.Udp4Rx.Packet.RxData;
514 LengthInBytes = pRxData->DataLength;
515
516 //
517 // +--------------------+ +-----------------------+
518 // | ESL_IO_MGMT | | Data Buffer |
519 // | | | (Driver owned) |
520 // | +---------------+ +-----------------------+
521 // | | Token | ^
522 // | | Rx Event | |
523 // | | | +-----------------------+
524 // | | RxData --> | EFI_UDP4_RECEIVE_DATA |
525 // +----+---------------+ | (Driver owned) |
526 // +-----------------------+
527 // +--------------------+ ^
528 // | ESL_PACKET | .
529 // | | .
530 // | +---------------+ .
531 // | | pRxData --> NULL .......
532 // +----+---------------+
533 //
534 //
535 // Save the data in the packet
536 //
537 pPacket = pIo->pPacket;
538 pPacket->Op.Udp4Rx.pRxData = pRxData;
539
540 //
541 // Complete this request
542 //
543 EslSocketRxComplete ( pIo, Status, LengthInBytes, FALSE );
544 DBG_EXIT ( );
545 }
546
547
548 /**
549 Determine if the socket is configured.
550
551 This routine uses the flag ESL_SOCKET::bConfigured to determine
552 if the network layer's configuration routine has been called.
553 This routine calls the bind and configuration routines if they
554 were not already called. After the port is configured, the
555 \ref ReceiveEngine is started.
556
557 This routine is called by EslSocketIsConfigured to verify
558 that the socket is configured.
559
560 @param [in] pSocket Address of an ::ESL_SOCKET structure
561
562 @retval EFI_SUCCESS - The port is connected
563 @retval EFI_NOT_STARTED - The port is not connected
564
565 **/
566 EFI_STATUS
567 EslUdp4SocketIsConfigured (
568 IN ESL_SOCKET * pSocket
569 )
570 {
571 EFI_UDP4_CONFIG_DATA * pConfigData;
572 ESL_PORT * pPort;
573 ESL_PORT * pNextPort;
574 ESL_UDP4_CONTEXT * pUdp4;
575 EFI_UDP4_PROTOCOL * pUdp4Protocol;
576 EFI_STATUS Status;
577 struct sockaddr_in LocalAddress;
578
579 DBG_ENTER ( );
580
581 //
582 // Assume success
583 //
584 Status = EFI_SUCCESS;
585
586 //
587 // Configure the port if necessary
588 //
589 if ( !pSocket->bConfigured ) {
590 //
591 // Fill in the port list if necessary
592 //
593 if ( NULL == pSocket->pPortList ) {
594 LocalAddress.sin_len = sizeof ( LocalAddress );
595 LocalAddress.sin_family = AF_INET;
596 LocalAddress.sin_addr.s_addr = 0;
597 LocalAddress.sin_port = 0;
598 Status = EslSocketBind ( &pSocket->SocketProtocol,
599 (struct sockaddr *)&LocalAddress,
600 LocalAddress.sin_len,
601 &pSocket->errno );
602 }
603
604 //
605 // Walk the port list
606 //
607 pPort = pSocket->pPortList;
608 while ( NULL != pPort ) {
609 //
610 // Attempt to configure the port
611 //
612 pNextPort = pPort->pLinkSocket;
613 pUdp4 = &pPort->Context.Udp4;
614 pUdp4Protocol = pPort->pProtocol.UDPv4;
615 pConfigData = &pUdp4->ConfigData;
616 DEBUG (( DEBUG_TX,
617 "0x%08x: pPort Configuring for %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d\r\n",
618 pPort,
619 pConfigData->StationAddress.Addr[0],
620 pConfigData->StationAddress.Addr[1],
621 pConfigData->StationAddress.Addr[2],
622 pConfigData->StationAddress.Addr[3],
623 pConfigData->StationPort,
624 pConfigData->RemoteAddress.Addr[0],
625 pConfigData->RemoteAddress.Addr[1],
626 pConfigData->RemoteAddress.Addr[2],
627 pConfigData->RemoteAddress.Addr[3],
628 pConfigData->RemotePort ));
629 Status = pUdp4Protocol->Configure ( pUdp4Protocol,
630 pConfigData );
631 if ( !EFI_ERROR ( Status )) {
632 //
633 // Update the configuration data
634 //
635 Status = pUdp4Protocol->GetModeData ( pUdp4Protocol,
636 pConfigData,
637 NULL,
638 NULL,
639 NULL );
640 }
641 if ( EFI_ERROR ( Status )) {
642 DEBUG (( DEBUG_LISTEN,
643 "ERROR - Failed to configure the Udp4 port, Status: %r\r\n",
644 Status ));
645 switch ( Status ) {
646 case EFI_ACCESS_DENIED:
647 pSocket->errno = EACCES;
648 break;
649
650 default:
651 case EFI_DEVICE_ERROR:
652 pSocket->errno = EIO;
653 break;
654
655 case EFI_INVALID_PARAMETER:
656 pSocket->errno = EADDRNOTAVAIL;
657 break;
658
659 case EFI_NO_MAPPING:
660 pSocket->errno = EAFNOSUPPORT;
661 break;
662
663 case EFI_OUT_OF_RESOURCES:
664 pSocket->errno = ENOBUFS;
665 break;
666
667 case EFI_UNSUPPORTED:
668 pSocket->errno = EOPNOTSUPP;
669 break;
670 }
671 }
672 else {
673 DEBUG (( DEBUG_TX,
674 "0x%08x: pPort Configured for %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d\r\n",
675 pPort,
676 pConfigData->StationAddress.Addr[0],
677 pConfigData->StationAddress.Addr[1],
678 pConfigData->StationAddress.Addr[2],
679 pConfigData->StationAddress.Addr[3],
680 pConfigData->StationPort,
681 pConfigData->RemoteAddress.Addr[0],
682 pConfigData->RemoteAddress.Addr[1],
683 pConfigData->RemoteAddress.Addr[2],
684 pConfigData->RemoteAddress.Addr[3],
685 pConfigData->RemotePort ));
686 pPort->bConfigured = TRUE;
687
688 //
689 // Start the first read on the port
690 //
691 EslSocketRxStart ( pPort );
692
693 //
694 // The socket is connected
695 //
696 pSocket->State = SOCKET_STATE_CONNECTED;
697 }
698
699 //
700 // Set the next port
701 //
702 pPort = pNextPort;
703 }
704
705 //
706 // Determine the configuration status
707 //
708 if ( NULL != pSocket->pPortList ) {
709 pSocket->bConfigured = TRUE;
710 }
711 }
712
713 //
714 // Determine the socket configuration status
715 //
716 if ( !EFI_ERROR ( Status )) {
717 Status = pSocket->bConfigured ? EFI_SUCCESS : EFI_NOT_STARTED;
718 }
719
720 //
721 // Return the port connected state.
722 //
723 DBG_EXIT_STATUS ( Status );
724 return Status;
725 }
726
727
728 /**
729 Buffer data for transmission over a network connection.
730
731 This routine buffers data for the transmit engine in the normal
732 data queue. When the \ref TransmitEngine has resources, this
733 routine will start the transmission of the next buffer on the
734 network connection.
735
736 This routine is called by ::EslSocketTransmit to buffer
737 data for transmission. The data is copied into a local buffer
738 freeing the application buffer for reuse upon return. When
739 necessary, this routine starts the transmit engine that
740 performs the data transmission on the network connection. The
741 transmit engine transmits the data a packet at a time over the
742 network connection.
743
744 Transmission errors are returned during the next transmission or
745 during the close operation. Only buffering errors are returned
746 during the current transmission attempt.
747
748 @param [in] pSocket Address of an ::ESL_SOCKET structure
749
750 @param [in] Flags Message control flags
751
752 @param [in] BufferLength Length of the the buffer
753
754 @param [in] pBuffer Address of a buffer to receive the data.
755
756 @param [in] pDataLength Number of received data bytes in the buffer.
757
758 @param [in] pAddress Network address of the remote system address
759
760 @param [in] AddressLength Length of the remote network address structure
761
762 @retval EFI_SUCCESS - Socket data successfully buffered
763
764 **/
765 EFI_STATUS
766 EslUdp4TxBuffer (
767 IN ESL_SOCKET * pSocket,
768 IN int Flags,
769 IN size_t BufferLength,
770 IN CONST UINT8 * pBuffer,
771 OUT size_t * pDataLength,
772 IN const struct sockaddr * pAddress,
773 IN socklen_t AddressLength
774 )
775 {
776 ESL_PACKET * pPacket;
777 ESL_PACKET * pPreviousPacket;
778 ESL_PORT * pPort;
779 const struct sockaddr_in * pRemoteAddress;
780 ESL_UDP4_CONTEXT * pUdp4;
781 size_t * pTxBytes;
782 ESL_UDP4_TX_DATA * pTxData;
783 EFI_STATUS Status;
784 EFI_TPL TplPrevious;
785
786 DBG_ENTER ( );
787
788 //
789 // Assume failure
790 //
791 Status = EFI_UNSUPPORTED;
792 pSocket->errno = ENOTCONN;
793 *pDataLength = 0;
794
795 //
796 // Verify that the socket is connected
797 //
798 if ( SOCKET_STATE_CONNECTED == pSocket->State ) {
799 //
800 // Locate the port
801 //
802 pPort = pSocket->pPortList;
803 if ( NULL != pPort ) {
804 //
805 // Determine the queue head
806 //
807 pUdp4 = &pPort->Context.Udp4;
808 pTxBytes = &pSocket->TxBytes;
809
810 //
811 // Verify that there is enough room to buffer another
812 // transmit operation
813 //
814 if ( pSocket->MaxTxBuf > *pTxBytes ) {
815 //
816 // Attempt to allocate the packet
817 //
818 Status = EslSocketPacketAllocate ( &pPacket,
819 sizeof ( pPacket->Op.Udp4Tx )
820 - sizeof ( pPacket->Op.Udp4Tx.Buffer )
821 + BufferLength,
822 0,
823 DEBUG_TX );
824 if ( !EFI_ERROR ( Status )) {
825 //
826 // Initialize the transmit operation
827 //
828 pTxData = &pPacket->Op.Udp4Tx;
829 pTxData->TxData.GatewayAddress = NULL;
830 pTxData->TxData.UdpSessionData = NULL;
831 pTxData->TxData.DataLength = (UINT32) BufferLength;
832 pTxData->TxData.FragmentCount = 1;
833 pTxData->TxData.FragmentTable[0].FragmentLength = (UINT32) BufferLength;
834 pTxData->TxData.FragmentTable[0].FragmentBuffer = &pPacket->Op.Udp4Tx.Buffer[0];
835 pTxData->RetransmitCount = 0;
836
837 //
838 // Set the remote system address if necessary
839 //
840 pTxData->TxData.UdpSessionData = NULL;
841 if ( NULL != pAddress ) {
842 pRemoteAddress = (const struct sockaddr_in *)pAddress;
843 pTxData->Session.SourceAddress.Addr[0] = pUdp4->ConfigData.StationAddress.Addr[0];
844 pTxData->Session.SourceAddress.Addr[1] = pUdp4->ConfigData.StationAddress.Addr[1];
845 pTxData->Session.SourceAddress.Addr[2] = pUdp4->ConfigData.StationAddress.Addr[2];
846 pTxData->Session.SourceAddress.Addr[3] = pUdp4->ConfigData.StationAddress.Addr[3];
847 pTxData->Session.SourcePort = 0;
848 pTxData->Session.DestinationAddress.Addr[0] = (UINT8)pRemoteAddress->sin_addr.s_addr;
849 pTxData->Session.DestinationAddress.Addr[1] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 8 );
850 pTxData->Session.DestinationAddress.Addr[2] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 16 );
851 pTxData->Session.DestinationAddress.Addr[3] = (UINT8)( pRemoteAddress->sin_addr.s_addr >> 24 );
852 pTxData->Session.DestinationPort = SwapBytes16 ( pRemoteAddress->sin_port );
853
854 //
855 // Use the remote system address when sending this packet
856 //
857 pTxData->TxData.UdpSessionData = &pTxData->Session;
858 }
859
860 //
861 // Copy the data into the buffer
862 //
863 CopyMem ( &pPacket->Op.Udp4Tx.Buffer[0],
864 pBuffer,
865 BufferLength );
866
867 //
868 // Synchronize with the socket layer
869 //
870 RAISE_TPL ( TplPrevious, TPL_SOCKETS );
871
872 //
873 // Stop transmission after an error
874 //
875 if ( !EFI_ERROR ( pSocket->TxError )) {
876 //
877 // Display the request
878 //
879 DEBUG (( DEBUG_TX,
880 "Send %d %s bytes from 0x%08x\r\n",
881 BufferLength,
882 pBuffer ));
883
884 //
885 // Queue the data for transmission
886 //
887 pPacket->pNext = NULL;
888 pPreviousPacket = pSocket->pTxPacketListTail;
889 if ( NULL == pPreviousPacket ) {
890 pSocket->pTxPacketListHead = pPacket;
891 }
892 else {
893 pPreviousPacket->pNext = pPacket;
894 }
895 pSocket->pTxPacketListTail = pPacket;
896 DEBUG (( DEBUG_TX,
897 "0x%08x: Packet on transmit list\r\n",
898 pPacket ));
899
900 //
901 // Account for the buffered data
902 //
903 *pTxBytes += BufferLength;
904 *pDataLength = BufferLength;
905
906 //
907 // Start the transmit engine if it is idle
908 //
909 if ( NULL != pPort->pTxFree ) {
910 EslSocketTxStart ( pPort,
911 &pSocket->pTxPacketListHead,
912 &pSocket->pTxPacketListTail,
913 &pPort->pTxActive,
914 &pPort->pTxFree );
915 }
916 }
917 else {
918 //
919 // Previous transmit error
920 // Stop transmission
921 //
922 Status = pSocket->TxError;
923 pSocket->errno = EIO;
924
925 //
926 // Free the packet
927 //
928 EslSocketPacketFree ( pPacket, DEBUG_TX );
929 }
930
931 //
932 // Release the socket layer synchronization
933 //
934 RESTORE_TPL ( TplPrevious );
935 }
936 else {
937 //
938 // Packet allocation failed
939 //
940 pSocket->errno = ENOMEM;
941 }
942 }
943 else {
944 //
945 // Not enough buffer space available
946 //
947 pSocket->errno = EAGAIN;
948 Status = EFI_NOT_READY;
949 }
950 }
951 }
952
953 //
954 // Return the operation status
955 //
956 DBG_EXIT_STATUS ( Status );
957 return Status;
958 }
959
960
961 /**
962 Process the transmit completion
963
964 This routine use ::EslSocketTxComplete to perform the transmit
965 completion processing for data packets.
966
967 This routine is called by the UDPv4 network layer when a data
968 transmit request completes.
969
970 @param [in] Event The normal transmit completion event
971
972 @param [in] pIo Address of an ::ESL_IO_MGMT structure
973
974 **/
975 VOID
976 EslUdp4TxComplete (
977 IN EFI_EVENT Event,
978 IN ESL_IO_MGMT * pIo
979 )
980 {
981 UINT32 LengthInBytes;
982 ESL_PORT * pPort;
983 ESL_PACKET * pPacket;
984 ESL_SOCKET * pSocket;
985 EFI_STATUS Status;
986
987 DBG_ENTER ( );
988
989 //
990 // Locate the active transmit packet
991 //
992 pPacket = pIo->pPacket;
993 pPort = pIo->pPort;
994 pSocket = pPort->pSocket;
995
996 //
997 // Get the transmit length and status
998 //
999 LengthInBytes = pPacket->Op.Udp4Tx.TxData.DataLength;
1000 pSocket->TxBytes -= LengthInBytes;
1001 Status = pIo->Token.Udp4Tx.Status;
1002
1003 //
1004 // Complete the transmit operation
1005 //
1006 EslSocketTxComplete ( pIo,
1007 LengthInBytes,
1008 Status,
1009 "UDP ",
1010 &pSocket->pTxPacketListHead,
1011 &pSocket->pTxPacketListTail,
1012 &pPort->pTxActive,
1013 &pPort->pTxFree );
1014 DBG_EXIT ( );
1015 }
1016
1017
1018 /**
1019 Interface between the socket layer and the network specific
1020 code that supports SOCK_DGRAM sockets over UDPv4.
1021 **/
1022 CONST ESL_PROTOCOL_API cEslUdp4Api = {
1023 "UDPv4",
1024 IPPROTO_UDP,
1025 OFFSET_OF ( ESL_PORT, Context.Udp4.ConfigData ),
1026 OFFSET_OF ( ESL_LAYER, pUdp4List ),
1027 OFFSET_OF ( struct sockaddr_in, sin_zero ),
1028 sizeof ( struct sockaddr_in ),
1029 AF_INET,
1030 sizeof (((ESL_PACKET *)0 )->Op.Udp4Rx ),
1031 sizeof (((ESL_PACKET *)0 )->Op.Udp4Rx ),
1032 OFFSET_OF ( ESL_IO_MGMT, Token.Udp4Rx.Packet.RxData ),
1033 FALSE,
1034 EADDRINUSE,
1035 NULL, // Accept
1036 NULL, // ConnectPoll
1037 NULL, // ConnectStart
1038 EslUdp4SocketIsConfigured,
1039 EslUdp4LocalAddressGet,
1040 EslUdp4LocalAddressSet,
1041 NULL, // Listen
1042 NULL, // OptionGet
1043 NULL, // OptionSet
1044 EslUdp4PacketFree,
1045 EslUdp4PortAllocate,
1046 NULL, // PortClose,
1047 NULL, // PortCloseOp
1048 TRUE,
1049 EslUdp4Receive,
1050 EslUdp4RemoteAddressGet,
1051 EslUdp4RemoteAddressSet,
1052 EslUdp4RxComplete,
1053 NULL, // RxStart
1054 EslUdp4TxBuffer,
1055 EslUdp4TxComplete,
1056 NULL // TxOobComplete
1057 };