1 /** @file
2   Declaration of external functions shared in TCP driver.
3 
4   Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 
6   SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef _TCP_FUNC_H_
11 #define _TCP_FUNC_H_
12 
13 #include "TcpOption.h"
14 
15 #define TCP_COMP_VAL(Min, Max, Default, Val) \
16   ((((Val) <= (Max)) && ((Val) >= (Min))) ? (Val) : (Default))
17 
18 /**
19   Timeout handler prototype.
20 
21   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
22 
23 **/
24 typedef
25 VOID
26 (*TCP_TIMER_HANDLER) (
27   IN OUT TCP_CB *Tcb
28   );
29 
30 //
31 // Functions in TcpMisc.c
32 //
33 
34 /**
35   Initialize the Tcb locally related members.
36 
37   @param[in, out]  Tcb               Pointer to the TCP_CB of this TCP instance.
38 
39 **/
40 VOID
41 TcpInitTcbLocal (
42   IN OUT TCP_CB *Tcb
43   );
44 
45 /**
46   Initialize the peer related members.
47 
48   @param[in, out]  Tcb    Pointer to the TCP_CB of this TCP instance.
49   @param[in]       Seg    Pointer to the segment that contains the peer's initial information.
50   @param[in]       Opt    Pointer to the options announced by the peer.
51 
52 **/
53 VOID
54 TcpInitTcbPeer (
55   IN OUT TCP_CB     *Tcb,
56   IN     TCP_SEG    *Seg,
57   IN     TCP_OPTION *Opt
58   );
59 
60 /**
61   Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
62 
63   @param[in]  Addr     Pointer to the IP address needs to match.
64   @param[in]  Port     The port number needs to match.
65   @param[in]  Version  IP_VERSION_4 indicates TCP is running on IP4 stack.
66                        IP_VERSION_6 indicates TCP is running on IP6 stack.
67 
68 
69   @retval     TRUE     The Tcb which matches the <Addr Port> pairs exists.
70   @retval     FALSE    Otherwise
71 
72 **/
73 BOOLEAN
74 TcpFindTcbByPeer (
75   IN EFI_IP_ADDRESS  *Addr,
76   IN TCP_PORTNO      Port,
77   IN UINT8           Version
78   );
79 
80 /**
81   Locate the TCP_CB related to the socket pair.
82 
83   @param[in]  LocalPort      The local port number.
84   @param[in]  LocalIp        The local IP address.
85   @param[in]  RemotePort     The remote port number.
86   @param[in]  RemoteIp       The remote IP address.
87   @param[in]  Version        IP_VERSION_4 indicates TCP is running on IP4 stack,
88                              IP_VERSION_6 indicates TCP is running on IP6 stack.
89   @param[in]  Syn            If TRUE, the listen sockets are searched.
90 
91   @return Pointer to the related TCP_CB. If NULL, no match is found.
92 
93 **/
94 TCP_CB *
95 TcpLocateTcb (
96   IN TCP_PORTNO      LocalPort,
97   IN EFI_IP_ADDRESS  *LocalIp,
98   IN TCP_PORTNO      RemotePort,
99   IN EFI_IP_ADDRESS  *RemoteIp,
100   IN UINT8           Version,
101   IN BOOLEAN         Syn
102   );
103 
104 /**
105   Insert a Tcb into the proper queue.
106 
107   @param[in]  Tcb               Pointer to the TCP_CB to be inserted.
108 
109   @retval 0                     The Tcb was inserted successfully.
110   @retval -1                    An error condition occurred.
111 
112 **/
113 INTN
114 TcpInsertTcb (
115   IN TCP_CB *Tcb
116   );
117 
118 /**
119   Clone a TCP_CB from Tcb.
120 
121   @param[in]  Tcb                   Pointer to the TCP_CB to be cloned.
122 
123   @return Pointer to the new cloned TCP_CB. If NULL, an error condition occurred.
124 
125 **/
126 TCP_CB *
127 TcpCloneTcb (
128   IN TCP_CB *Tcb
129   );
130 
131 /**
132   Compute an ISS to be used by a new connection.
133 
134   @return The result ISS.
135 
136 **/
137 TCP_SEQNO
138 TcpGetIss (
139   VOID
140   );
141 
142 /**
143   Get the local mss.
144 
145   @param[in]  Sock        Pointer to the socket to get mss.
146 
147   @return The mss size.
148 
149 **/
150 UINT16
151 TcpGetRcvMss (
152   IN SOCKET  *Sock
153   );
154 
155 /**
156   Set the Tcb's state.
157 
158   @param[in]  Tcb                   Pointer to the TCP_CB of this TCP instance.
159   @param[in]  State                 The state to be set.
160 
161 **/
162 VOID
163 TcpSetState (
164   IN TCP_CB *Tcb,
165   IN UINT8  State
166   );
167 
168 /**
169   Compute the TCP segment's checksum.
170 
171   @param[in]  Nbuf       Pointer to the buffer that contains the TCP segment.
172   @param[in]  HeadSum    The checksum value of the fixed part of pseudo header.
173 
174   @return The checksum value.
175 
176 **/
177 UINT16
178 TcpChecksum (
179   IN NET_BUF *Nbuf,
180   IN UINT16  HeadSum
181   );
182 
183 /**
184   Translate the information from the head of the received TCP
185   segment Nbuf contains, and fill it into a TCP_SEG structure.
186 
187   @param[in]       Tcb           Pointer to the TCP_CB of this TCP instance.
188   @param[in, out]  Nbuf          Pointer to the buffer contains the TCP segment.
189 
190   @return Pointer to the TCP_SEG that contains the translated TCP head information.
191 
192 **/
193 TCP_SEG *
194 TcpFormatNetbuf (
195   IN     TCP_CB  *Tcb,
196   IN OUT NET_BUF *Nbuf
197   );
198 
199 /**
200   Initialize an active connection,
201 
202   @param[in, out]  Tcb          Pointer to the TCP_CB that wants to initiate a
203                                 connection.
204 
205 **/
206 VOID
207 TcpOnAppConnect (
208   IN OUT TCP_CB  *Tcb
209   );
210 
211 /**
212   Application has consumed some data, check whether
213   to send a window update ack or a delayed ack.
214 
215   @param[in]  Tcb        Pointer to the TCP_CB of this TCP instance.
216 
217 **/
218 VOID
219 TcpOnAppConsume (
220   IN TCP_CB *Tcb
221   );
222 
223 /**
224   Initiate the connection close procedure, called when
225   applications want to close the connection.
226 
227   @param[in, out]  Tcb          Pointer to the TCP_CB of this TCP instance.
228 
229 **/
230 VOID
231 TcpOnAppClose (
232   IN OUT TCP_CB *Tcb
233   );
234 
235 /**
236   Check whether the application's newly delivered data can be sent out.
237 
238   @param[in, out]  Tcb          Pointer to the TCP_CB of this TCP instance.
239 
240   @retval 0                     The data has been sent out successfully.
241   @retval -1                    The Tcb is not in a state that data is permitted to
242                                 be sent out.
243 
244 **/
245 INTN
246 TcpOnAppSend (
247   IN OUT TCP_CB *Tcb
248   );
249 
250 /**
251   Abort the connection by sending a reset segment: called
252   when the application wants to abort the connection.
253 
254   @param[in]  Tcb                   Pointer to the TCP_CB of the TCP instance.
255 
256 **/
257 VOID
258 TcpOnAppAbort (
259   IN TCP_CB *Tcb
260   );
261 
262 /**
263   Reset the connection related with Tcb.
264 
265   @param[in]  Tcb         Pointer to the TCP_CB of the connection to be reset.
266 
267 **/
268 VOID
269 TcpResetConnection (
270   IN TCP_CB *Tcb
271   );
272 
273 /**
274   Install the device path protocol on the TCP instance.
275 
276   @param[in]  Sock          Pointer to the socket representing the TCP instance.
277 
278   @retval EFI_SUCCESS           The device path protocol installed.
279   @retval other                 Failed to install the device path protocol.
280 
281 **/
282 EFI_STATUS
283 TcpInstallDevicePath (
284   IN SOCKET *Sock
285   );
286 
287 
288 //
289 // Functions in TcpOutput.c
290 //
291 
292 /**
293   Compute the sequence space left in the old receive window.
294 
295   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance.
296 
297   @return The sequence space left in the old receive window.
298 
299 **/
300 UINT32
301 TcpRcvWinOld (
302   IN TCP_CB *Tcb
303   );
304 
305 /**
306   Compute the current receive window.
307 
308   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance.
309 
310   @return The size of the current receive window, in bytes.
311 
312 **/
313 UINT32
314 TcpRcvWinNow (
315   IN TCP_CB *Tcb
316   );
317 
318 /**
319   Get the maximum SndNxt.
320 
321   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance.
322 
323   @return The sequence number of the maximum SndNxt.
324 
325 **/
326 TCP_SEQNO
327 TcpGetMaxSndNxt (
328   IN TCP_CB *Tcb
329   );
330 
331 /**
332   Compute how much data to send.
333 
334   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance.
335   @param[in]  Force   If TRUE, ignore the sender's SWS avoidance algorithm
336                       and send out data by force.
337 
338   @return The length of the data that can be sent. If 0, no data can be sent.
339 
340 **/
341 UINT32
342 TcpDataToSend (
343   IN TCP_CB *Tcb,
344   IN INTN   Force
345   );
346 
347 /**
348   Retransmit the segment from sequence Seq.
349 
350   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance.
351   @param[in]  Seq     The sequence number of the segment to be retransmitted.
352 
353   @retval 0       The retransmission succeeded.
354   @retval -1      An error condition occurred.
355 
356 **/
357 INTN
358 TcpRetransmit (
359   IN TCP_CB    *Tcb,
360   IN TCP_SEQNO Seq
361   );
362 
363 /**
364   Check whether to send data/SYN/FIN and piggyback an ACK.
365 
366   @param[in, out]  Tcb     Pointer to the TCP_CB of this TCP instance.
367   @param[in]       Force   If TRUE, ignore the sender's SWS avoidance algorithm
368                            and send out data by force.
369 
370   @return The number of bytes sent.
371 
372 **/
373 INTN
374 TcpToSendData (
375   IN OUT TCP_CB *Tcb,
376   IN     INTN   Force
377   );
378 
379 /**
380   Check whether to send an ACK or delayed ACK.
381 
382   @param[in, out]  Tcb     Pointer to the TCP_CB of this TCP instance.
383 
384 **/
385 VOID
386 TcpToSendAck (
387   IN OUT TCP_CB *Tcb
388   );
389 
390 /**
391   Send an ACK immediately.
392 
393   @param[in, out]  Tcb     Pointer to the TCP_CB of this TCP instance.
394 
395 **/
396 VOID
397 TcpSendAck (
398   IN OUT TCP_CB *Tcb
399   );
400 
401 /**
402   Send a zero probe segment. It can be used by keepalive and zero window probe.
403 
404   @param[in, out]  Tcb     Pointer to the TCP_CB of this TCP instance.
405 
406   @retval 0       The zero probe segment was sent out successfully.
407   @retval other   An error condition occurred.
408 
409 **/
410 INTN
411 TcpSendZeroProbe (
412   IN OUT TCP_CB *Tcb
413   );
414 
415 /**
416   Send a RESET segment in response to the segment received.
417 
418   @param[in]  Tcb     Pointer to the TCP_CB of this TCP instance, may be NULL.
419   @param[in]  Head    TCP header of the segment that triggers the reset.
420   @param[in]  Len     Length of the segment that triggers the reset.
421   @param[in]  Local   Local IP address.
422   @param[in]  Remote  Remote peer's IP address.
423   @param[in]  Version IP_VERSION_4 indicates TCP is running on IP4 stack,
424                       IP_VERSION_6 indicates TCP is running on IP6 stack.
425 
426   @retval     0       A reset is sent or no need to send it.
427   @retval     -1      No reset is sent.
428 
429 **/
430 INTN
431 TcpSendReset (
432   IN TCP_CB          *Tcb,
433   IN TCP_HEAD        *Head,
434   IN INT32           Len,
435   IN EFI_IP_ADDRESS  *Local,
436   IN EFI_IP_ADDRESS  *Remote,
437   IN UINT8           Version
438   );
439 
440 /**
441   Verify that the segment is in good shape.
442 
443   @param[in]  Nbuf    Buffer that contains the segment to be checked.
444 
445   @retval     0       The segment is broken.
446   @retval     1       The segment is in good shape.
447 
448 **/
449 INTN
450 TcpVerifySegment (
451   IN NET_BUF *Nbuf
452   );
453 
454 //
455 // Functions from TcpInput.c
456 //
457 
458 /**
459   Process the received ICMP error messages for TCP.
460 
461   @param[in]  Nbuf     Buffer that contains part of the TCP segment without IP header
462                        truncated from the ICMP error packet.
463   @param[in]  IcmpErr  The ICMP error code interpreted from an ICMP error packet.
464   @param[in]  Src      Source address of the ICMP error message.
465   @param[in]  Dst      Destination address of the ICMP error message.
466   @param[in]  Version  IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates
467                        IP6 stack.
468 
469 **/
470 VOID
471 TcpIcmpInput (
472   IN NET_BUF         *Nbuf,
473   IN UINT8           IcmpErr,
474   IN EFI_IP_ADDRESS  *Src,
475   IN EFI_IP_ADDRESS  *Dst,
476   IN UINT8           Version
477   );
478 
479 /**
480   Process the received TCP segments.
481 
482   @param[in]  Nbuf     Buffer that contains received TCP segment without an IP header.
483   @param[in]  Src      Source address of the segment, or the peer's IP address.
484   @param[in]  Dst      Destination address of the segment, or the local end's IP
485                        address.
486   @param[in]  Version  IP_VERSION_4 indicates IP4 stack, IP_VERSION_6 indicates
487                        IP6 stack.
488 
489   @retval 0        The segment processed successfully. It is either accepted or
490                    discarded. But no connection is reset by the segment.
491   @retval -1       A connection is reset by the segment.
492 
493 **/
494 INTN
495 TcpInput (
496   IN NET_BUF         *Nbuf,
497   IN EFI_IP_ADDRESS  *Src,
498   IN EFI_IP_ADDRESS  *Dst,
499   IN UINT8           Version
500   );
501 
502 //
503 // Functions in TcpTimer.c
504 //
505 
506 /**
507   Close the TCP connection.
508 
509   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
510 
511 **/
512 VOID
513 TcpClose (
514   IN OUT TCP_CB *Tcb
515   );
516 
517 /**
518   Heart beat timer handler, queues the DPC at TPL_CALLBACK.
519 
520   @param[in]  Event    Timer event signaled, ignored.
521   @param[in]  Context  Context of the timer event, ignored.
522 
523 **/
524 VOID
525 EFIAPI
526 TcpTicking (
527   IN EFI_EVENT Event,
528   IN VOID      *Context
529   );
530 
531 /**
532   Enable a TCP timer.
533 
534   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
535   @param[in]       Timer    The index of the timer to be enabled.
536   @param[in]       TimeOut  The timeout value of this timer.
537 
538 **/
539 VOID
540 TcpSetTimer (
541   IN OUT TCP_CB *Tcb,
542   IN     UINT16 Timer,
543   IN     UINT32 TimeOut
544   );
545 
546 /**
547   Clear one TCP timer.
548 
549   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
550   @param[in]       Timer    The index of the timer to be cleared.
551 
552 **/
553 VOID
554 TcpClearTimer (
555   IN OUT TCP_CB *Tcb,
556   IN     UINT16 Timer
557   );
558 
559 /**
560   Clear all TCP timers.
561 
562   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
563 
564 **/
565 VOID
566 TcpClearAllTimer (
567   IN OUT TCP_CB *Tcb
568   );
569 
570 /**
571   Enable the window prober timer and set the timeout value.
572 
573   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
574 
575 **/
576 VOID
577 TcpSetProbeTimer (
578   IN OUT TCP_CB *Tcb
579   );
580 
581 /**
582   Enable the keepalive timer and set the timeout value.
583 
584   @param[in, out]  Tcb      Pointer to the TCP_CB of this TCP instance.
585 
586 **/
587 VOID
588 TcpSetKeepaliveTimer (
589   IN OUT TCP_CB *Tcb
590   );
591 
592 //
593 // Functions in TcpIo.c
594 //
595 
596 /**
597   Packet receive callback function provided to IP_IO. Used to call
598   the proper function to handle the packet received by IP.
599 
600   @param[in] Status        Result of the receive request.
601   @param[in] IcmpErr       Valid when Status is EFI_ICMP_ERROR.
602   @param[in] NetSession    The IP session for the received packet.
603   @param[in] Pkt           Packet received.
604   @param[in] Context       The data provided by the user for the received packet when
605                            the callback is registered in IP_IO_OPEN_DATA::RcvdContext.
606                            This is an optional parameter that may be NULL.
607 
608 **/
609 VOID
610 EFIAPI
611 TcpRxCallback (
612   IN EFI_STATUS                       Status,
613   IN UINT8                            IcmpErr,
614   IN EFI_NET_SESSION_DATA             *NetSession,
615   IN NET_BUF                          *Pkt,
616   IN VOID                             *Context    OPTIONAL
617   );
618 
619 /**
620   Send the segment to IP via IpIo function.
621 
622   @param[in]  Tcb                Pointer to the TCP_CB of this TCP instance.
623   @param[in]  Nbuf               Pointer to the TCP segment to be sent.
624   @param[in]  Src                Source address of the TCP segment.
625   @param[in]  Dest               Destination address of the TCP segment.
626   @param[in]  Version            IP_VERSION_4 or IP_VERSION_6
627 
628   @retval 0                      The segment was sent out successfully.
629   @retval -1                     The segment failed to be sent.
630 
631 **/
632 INTN
633 TcpSendIpPacket (
634   IN TCP_CB          *Tcb,
635   IN NET_BUF         *Nbuf,
636   IN EFI_IP_ADDRESS  *Src,
637   IN EFI_IP_ADDRESS  *Dest,
638   IN UINT8           Version
639   );
640 
641 /**
642   Refresh the remote peer's Neighbor Cache State if already exists.
643 
644   @param[in]  Tcb                Pointer to the TCP_CB of this TCP instance.
645   @param[in]  Neighbor           Source address of the TCP segment.
646   @param[in]  Timeout            Time in 100-ns units that this entry will remain
647                                  in the neighbor cache. A value of zero means that
648                                  the entry  is permanent. A value of non-zero means
649                                  that the entry is  dynamic and will be deleted
650                                  after Timeout.
651 
652   @retval EFI_SUCCESS            Successfully updated the neighbor relationship.
653   @retval EFI_NOT_STARTED        The IpIo is not configured.
654   @retval EFI_INVALID_PARAMETER  Any input parameter is invalid.
655   @retval EFI_OUT_OF_RESOURCES   Failed to allocate some resources.
656   @retval EFI_NOT_FOUND          This entry is not in the neighbor table.
657 
658 **/
659 EFI_STATUS
660 Tcp6RefreshNeighbor (
661   IN TCP_CB          *Tcb,
662   IN EFI_IP_ADDRESS  *Neighbor,
663   IN UINT32          Timeout
664   );
665 
666 //
667 // Functions in TcpDispatcher.c
668 //
669 
670 /**
671   The protocol handler provided to the socket layer, used to
672   dispatch the socket level requests by calling the corresponding
673   TCP layer functions.
674 
675   @param[in]  Sock               Pointer to the socket of this TCP instance.
676   @param[in]  Request            The code of this operation request.
677   @param[in]  Data               Pointer to the operation specific data passed in
678                                  together with the operation request. This is an
679                                  optional parameter that may be NULL.
680 
681   @retval EFI_SUCCESS            The socket request completed successfully.
682   @retval other                  The error status returned by the corresponding TCP
683                                  layer function.
684 
685 **/
686 EFI_STATUS
687 TcpDispatcher (
688   IN SOCKET                  *Sock,
689   IN UINT8                   Request,
690   IN VOID                    *Data    OPTIONAL
691   );
692 
693 #endif
694