1 | /** @file
|
---|
2 | Implementation of I/O interfaces between TCP and IpIoLib.
|
---|
3 |
|
---|
4 | Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include "TcpMain.h"
|
---|
11 |
|
---|
12 | /**
|
---|
13 | Packet receive callback function provided to IP_IO, used to call
|
---|
14 | the proper function to handle the packet received by IP.
|
---|
15 |
|
---|
16 | @param[in] Status Result of the receive request.
|
---|
17 | @param[in] IcmpErr Valid when Status is EFI_ICMP_ERROR.
|
---|
18 | @param[in] NetSession The IP session for the received packet.
|
---|
19 | @param[in] Pkt Packet received.
|
---|
20 | @param[in] Context The data provided by the user for the received packet when
|
---|
21 | the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
|
---|
22 | This is an optional parameter that may be NULL.
|
---|
23 |
|
---|
24 | **/
|
---|
25 | VOID
|
---|
26 | EFIAPI
|
---|
27 | TcpRxCallback (
|
---|
28 | IN EFI_STATUS Status,
|
---|
29 | IN UINT8 IcmpErr,
|
---|
30 | IN EFI_NET_SESSION_DATA *NetSession,
|
---|
31 | IN NET_BUF *Pkt,
|
---|
32 | IN VOID *Context OPTIONAL
|
---|
33 | )
|
---|
34 | {
|
---|
35 | if (EFI_SUCCESS == Status) {
|
---|
36 | TcpInput (Pkt, &NetSession->Source, &NetSession->Dest, NetSession->IpVersion);
|
---|
37 | } else {
|
---|
38 | TcpIcmpInput (
|
---|
39 | Pkt,
|
---|
40 | IcmpErr,
|
---|
41 | &NetSession->Source,
|
---|
42 | &NetSession->Dest,
|
---|
43 | NetSession->IpVersion
|
---|
44 | );
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | Send the segment to IP via IpIo function.
|
---|
50 |
|
---|
51 | @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
|
---|
52 | @param[in] Nbuf Pointer to the TCP segment to be sent.
|
---|
53 | @param[in] Src Source address of the TCP segment.
|
---|
54 | @param[in] Dest Destination address of the TCP segment.
|
---|
55 | @param[in] Version IP_VERSION_4 or IP_VERSION_6
|
---|
56 |
|
---|
57 | @retval 0 The segment was sent out successfully.
|
---|
58 | @retval -1 The segment failed to send.
|
---|
59 |
|
---|
60 | **/
|
---|
61 | INTN
|
---|
62 | TcpSendIpPacket (
|
---|
63 | IN TCP_CB *Tcb,
|
---|
64 | IN NET_BUF *Nbuf,
|
---|
65 | IN EFI_IP_ADDRESS *Src,
|
---|
66 | IN EFI_IP_ADDRESS *Dest,
|
---|
67 | IN UINT8 Version
|
---|
68 | )
|
---|
69 | {
|
---|
70 | EFI_STATUS Status;
|
---|
71 | IP_IO *IpIo;
|
---|
72 | IP_IO_OVERRIDE Override;
|
---|
73 | SOCKET *Sock;
|
---|
74 | VOID *IpSender;
|
---|
75 | TCP_PROTO_DATA *TcpProto;
|
---|
76 |
|
---|
77 | if (NULL == Tcb) {
|
---|
78 | IpIo = NULL;
|
---|
79 | IpSender = IpIoFindSender (&IpIo, Version, Src);
|
---|
80 |
|
---|
81 | if (IpSender == NULL) {
|
---|
82 | DEBUG ((DEBUG_WARN, "TcpSendIpPacket: No appropriate IpSender.\n"));
|
---|
83 | return -1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (Version == IP_VERSION_6) {
|
---|
87 | //
|
---|
88 | // It's tricky here. EFI IPv6 Spec don't allow an instance overriding the
|
---|
89 | // destination address if the dest is already specified through the
|
---|
90 | // configuration data. Here we get the IpIo we need and use the default IP
|
---|
91 | // instance in this IpIo to send the packet. The dest address is configured
|
---|
92 | // to be the unspecified address for the default IP instance.
|
---|
93 | //
|
---|
94 | IpSender = NULL;
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | Sock = Tcb->Sk;
|
---|
98 | TcpProto = (TCP_PROTO_DATA *)Sock->ProtoReserved;
|
---|
99 | IpIo = TcpProto->TcpService->IpIo;
|
---|
100 | IpSender = Tcb->IpInfo;
|
---|
101 |
|
---|
102 | if (Version == IP_VERSION_6) {
|
---|
103 | //
|
---|
104 | // It's IPv6 and this TCP segment belongs to a solid TCB, in such case
|
---|
105 | // the destination address can't be overridden, so reset the Dest to NULL.
|
---|
106 | //
|
---|
107 | if (!Tcb->RemoteIpZero) {
|
---|
108 | Dest = NULL;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | ASSERT (Version == IpIo->IpVersion);
|
---|
114 |
|
---|
115 | if (Version == IP_VERSION_4) {
|
---|
116 | Override.Ip4OverrideData.TypeOfService = 0;
|
---|
117 | Override.Ip4OverrideData.TimeToLive = 255;
|
---|
118 | Override.Ip4OverrideData.DoNotFragment = FALSE;
|
---|
119 | Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_TCP;
|
---|
120 | ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
|
---|
121 | CopyMem (&Override.Ip4OverrideData.SourceAddress, Src, sizeof (EFI_IPv4_ADDRESS));
|
---|
122 | } else {
|
---|
123 | Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_TCP;
|
---|
124 | Override.Ip6OverrideData.HopLimit = 255;
|
---|
125 | Override.Ip6OverrideData.FlowLabel = 0;
|
---|
126 | }
|
---|
127 |
|
---|
128 | Status = IpIoSend (IpIo, Nbuf, IpSender, NULL, NULL, Dest, &Override);
|
---|
129 |
|
---|
130 | if (EFI_ERROR (Status)) {
|
---|
131 | DEBUG ((DEBUG_ERROR, "TcpSendIpPacket: return %r error\n", Status));
|
---|
132 | return -1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | Refresh the remote peer's Neighbor Cache State if already exists.
|
---|
140 |
|
---|
141 | @param[in] Tcb Pointer to the TCP_CB of this TCP instance.
|
---|
142 | @param[in] Neighbor Source address of the TCP segment.
|
---|
143 | @param[in] Timeout Time in 100-ns units that this entry will remain
|
---|
144 | in the neighbor cache. A value of zero means that
|
---|
145 | the entry is permanent. A value of non-zero means
|
---|
146 | that the entry is dynamic and will be deleted
|
---|
147 | after Timeout.
|
---|
148 |
|
---|
149 | @retval EFI_SUCCESS Successfully updated the neighbor relationship.
|
---|
150 | @retval EFI_NOT_STARTED The IpIo is not configured.
|
---|
151 | @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
|
---|
152 | @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
|
---|
153 | @retval EFI_NOT_FOUND This entry is not in the neighbor table.
|
---|
154 |
|
---|
155 | **/
|
---|
156 | EFI_STATUS
|
---|
157 | Tcp6RefreshNeighbor (
|
---|
158 | IN TCP_CB *Tcb,
|
---|
159 | IN EFI_IP_ADDRESS *Neighbor,
|
---|
160 | IN UINT32 Timeout
|
---|
161 | )
|
---|
162 | {
|
---|
163 | IP_IO *IpIo;
|
---|
164 | SOCKET *Sock;
|
---|
165 | TCP_PROTO_DATA *TcpProto;
|
---|
166 |
|
---|
167 | if (NULL == Tcb) {
|
---|
168 | IpIo = NULL;
|
---|
169 | IpIoFindSender (&IpIo, IP_VERSION_6, Neighbor);
|
---|
170 |
|
---|
171 | if (IpIo == NULL) {
|
---|
172 | DEBUG ((DEBUG_WARN, "Tcp6AddNeighbor: No appropriate IpIo.\n"));
|
---|
173 | return EFI_NOT_STARTED;
|
---|
174 | }
|
---|
175 | } else {
|
---|
176 | Sock = Tcb->Sk;
|
---|
177 | TcpProto = (TCP_PROTO_DATA *)Sock->ProtoReserved;
|
---|
178 | IpIo = TcpProto->TcpService->IpIo;
|
---|
179 | }
|
---|
180 |
|
---|
181 | return IpIoRefreshNeighbor (IpIo, Neighbor, Timeout);
|
---|
182 | }
|
---|