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