1 /** @file
2   Tcp function header file.
3 
4 Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>
5 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<BR>
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 #ifndef _TCP4_FUNC_H_
16 #define _TCP4_FUNC_H_
17 
18 //
19 // Declaration of all the functions in TCP
20 // protocol. It is intended to keep tcp.h
21 // clear.
22 //
23 
24 //
25 // Functions in tcp.c
26 //
27 
28 /**
29   Try to find one Tcb whose <Ip, Port> equals to <IN Addr, IN Port>.
30 
31   @param  Addr                  Pointer to the IP address needs to match.
32   @param  Port                  The port number needs to match.
33 
34   @return  The Tcb which matches the <Addr Port> paire exists or not.
35 
36 **/
37 BOOLEAN
38 TcpFindTcbByPeer (
39   IN EFI_IPv4_ADDRESS  *Addr,
40   IN TCP_PORTNO        Port
41   );
42 
43 /**
44   Locate the TCP_CB related to the socket pair.
45 
46   @param  LocalPort             The local port number.
47   @param  LocalIp               The local IP address.
48   @param  RemotePort            The remote port number.
49   @param  RemoteIp              The remote IP address.
50   @param  Syn                   Whether to search the listen sockets, if TRUE, the
51                                 listen sockets are searched.
52 
53   @return  Pointer to the related TCP_CB, if NULL no match is found.
54 
55 **/
56 TCP_CB *
57 TcpLocateTcb (
58   IN TCP_PORTNO  LocalPort,
59   IN UINT32      LocalIp,
60   IN TCP_PORTNO  RemotePort,
61   IN UINT32      RemoteIp,
62   IN BOOLEAN     Syn
63   );
64 
65 /**
66   Insert a Tcb into the proper queue.
67 
68   @param  Tcb                   Pointer to the TCP_CB to be inserted.
69 
70   @retval 0                     The Tcb is inserted successfully.
71   @retval -1                    Error condition occurred.
72 
73 **/
74 INTN
75 TcpInsertTcb (
76   IN TCP_CB *Tcb
77   );
78 
79 /**
80   Clone a TCP_CB from Tcb.
81 
82   @param  Tcb                   Pointer to the TCP_CB to be cloned.
83 
84   @return  Pointer to the new cloned TCP_CB, if NULL error condition occurred.
85 
86 **/
87 TCP_CB *
88 TcpCloneTcb (
89   IN TCP_CB *Tcb
90   );
91 
92 /**
93   Compute an ISS to be used by a new connection.
94 
95   @return  The result ISS.
96 
97 **/
98 TCP_SEQNO
99 TcpGetIss (
100   VOID
101   );
102 
103 /**
104   Initialize the Tcb local related members.
105 
106   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
107 
108 **/
109 VOID
110 TcpInitTcbLocal (
111   IN OUT TCP_CB *Tcb
112   );
113 
114 /**
115   Initialize the peer related members.
116 
117   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
118   @param  Seg                   Pointer to the segment that contains the peer's
119                                 intial info.
120   @param  Opt                   Pointer to the options announced by the peer.
121 
122 **/
123 VOID
124 TcpInitTcbPeer (
125   IN OUT TCP_CB     *Tcb,
126   IN     TCP_SEG    *Seg,
127   IN     TCP_OPTION *Opt
128   );
129 
130 /**
131   Get the local mss.
132 
133   @param  Sock        Pointer to the socket to get mss
134 
135   @return  The mss size.
136 
137 **/
138 UINT16
139 TcpGetRcvMss (
140   IN SOCKET  *Sock
141   );
142 
143 /**
144   Set the Tcb's state.
145 
146   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
147   @param  State                 The state to be set.
148 
149 **/
150 VOID
151 TcpSetState (
152   IN OUT TCP_CB    *Tcb,
153   IN     UINT8     State
154   );
155 
156 //
157 // Functions in Tcp4Output.c
158 //
159 /**
160   Send the segment to IP via IpIo function.
161 
162   @param  Tcb         Pointer to the TCP_CB of this TCP instance.
163   @param  Nbuf        Pointer to the TCP segment to be sent.
164   @param  Src         Source address of the TCP segment.
165   @param  Dest        Destination address of the TCP segment.
166 
167   @retval 0           The segment was sent out successfully.
168   @retval -1          The segment was failed to send.
169 
170 **/
171 INTN
172 TcpSendIpPacket (
173   IN TCP_CB    *Tcb,
174   IN NET_BUF   *Nbuf,
175   IN UINT32    Src,
176   IN UINT32    Dest
177   );
178 
179 /**
180   Check whether to send data/SYN/FIN and piggy back an ACK.
181 
182   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
183   @param  Force   Whether to ignore the sender's SWS avoidance algorithm and send
184                   out data by force.
185 
186   @return The number of bytes sent.
187 
188 **/
189 INTN
190 TcpToSendData (
191   IN OUT TCP_CB *Tcb,
192   IN     INTN Force
193   );
194 
195 /**
196   Check whether to send an ACK or delayed ACK.
197 
198   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
199 
200 **/
201 VOID
202 TcpToSendAck (
203   IN OUT TCP_CB *Tcb
204   );
205 
206 /**
207   Send an ACK immediately.
208 
209   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
210 
211 **/
212 VOID
213 TcpSendAck (
214   IN OUT TCP_CB *Tcb
215   );
216 
217 /**
218   Send a zero probe segment. It can be used by keepalive and zero window probe.
219 
220   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
221 
222   @retval 0       The zero probe segment was sent out successfully.
223   @retval other   Error condition occurred.
224 
225 **/
226 INTN
227 TcpSendZeroProbe (
228   IN OUT TCP_CB *Tcb
229   );
230 
231 /**
232   Process the data and FIN flag, check whether to deliver
233   data to the socket layer.
234 
235   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
236 
237   @retval 0        No error occurred to deliver data.
238   @retval -1       Error condition occurred. Proper response is to reset the
239                    connection.
240 
241 **/
242 INTN
243 TcpDeliverData (
244   IN OUT TCP_CB *Tcb
245   );
246 
247 /**
248   Send a RESET segment in response to the segment received.
249 
250   @param  Tcb     Pointer to the TCP_CB of this TCP instance, may be NULL.
251   @param  Head    TCP header of the segment that triggers the reset.
252   @param  Len     Length of the segment that triggers the reset.
253   @param  Local   Local IP address.
254   @param  Remote  Remote peer's IP address.
255 
256   @retval 0       A reset is sent or no need to send it.
257   @retval -1      No reset is sent.
258 
259 **/
260 INTN
261 TcpSendReset (
262   IN TCP_CB    *Tcb,
263   IN TCP_HEAD  *Head,
264   IN INT32     Len,
265   IN UINT32    Local,
266   IN UINT32    Remote
267   );
268 
269 /**
270   Compute the sequence space left in the old receive window.
271 
272   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
273 
274   @return The sequence space left in the old receive window.
275 
276 **/
277 UINT32
278 TcpRcvWinOld (
279   IN TCP_CB *Tcb
280   );
281 
282 /**
283   Compute the current receive window.
284 
285   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
286 
287   @return The size of the current receive window, in bytes.
288 
289 **/
290 UINT32
291 TcpRcvWinNow (
292   IN TCP_CB *Tcb
293   );
294 
295 /**
296   Retransmit the segment from sequence Seq.
297 
298   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
299   @param  Seq     The sequence number of the segment to be retransmitted.
300 
301   @retval 0       Retransmission succeeded.
302   @retval -1      Error condition occurred.
303 
304 **/
305 INTN
306 TcpRetransmit (
307   IN TCP_CB    *Tcb,
308   IN TCP_SEQNO Seq
309   );
310 
311 /**
312   Compute how much data to send.
313 
314   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
315   @param  Force   Whether to ignore the sender's SWS avoidance algorithm and send
316                   out data by force.
317 
318   @return The length of the data can be sent, if 0, no data can be sent.
319 
320 **/
321 UINT32
322 TcpDataToSend (
323   IN TCP_CB *Tcb,
324   IN INTN   Force
325   );
326 
327 /**
328   Verify that the segment is in good shape.
329 
330   @param  Nbuf    Buffer that contains the segment to be checked.
331 
332   @retval 0       The segment is broken.
333   @retval 1       The segment is in good shape.
334 
335 **/
336 INTN
337 TcpVerifySegment (
338   IN NET_BUF *Nbuf
339   );
340 
341 /**
342   Verify that all the segments in SndQue are in good shape.
343 
344   @param  Head    Pointer to the head node of the SndQue.
345 
346   @retval 0       At least one segment is broken.
347   @retval 1       All segments in the specific queue are in good shape.
348 
349 **/
350 INTN
351 TcpCheckSndQue (
352   IN LIST_ENTRY     *Head
353   );
354 
355 /**
356   Get a segment from the Tcb's SndQue.
357 
358   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
359   @param  Seq     The sequence number of the segment.
360   @param  Len     The maximum length of the segment.
361 
362   @return Pointer to the segment, if NULL some error occurred.
363 
364 **/
365 NET_BUF *
366 TcpGetSegmentSndQue (
367   IN TCP_CB    *Tcb,
368   IN TCP_SEQNO Seq,
369   IN UINT32    Len
370   );
371 
372 /**
373   Get a segment from the Tcb's socket buffer.
374 
375   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
376   @param  Seq     The sequence number of the segment.
377   @param  Len     The maximum length of the segment.
378 
379   @return Pointer to the segment, if NULL some error occurred.
380 
381 **/
382 NET_BUF *
383 TcpGetSegmentSock (
384   IN TCP_CB    *Tcb,
385   IN TCP_SEQNO Seq,
386   IN UINT32    Len
387   );
388 
389 /**
390   Get a segment starting from sequence Seq of a maximum
391   length of Len.
392 
393   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
394   @param  Seq     The sequence number of the segment.
395   @param  Len     The maximum length of the segment.
396 
397   @return Pointer to the segment, if NULL some error occurred.
398 
399 **/
400 NET_BUF *
401 TcpGetSegment (
402   IN TCP_CB    *Tcb,
403   IN TCP_SEQNO Seq,
404   IN UINT32    Len
405   );
406 
407 /**
408   Get the maximum SndNxt.
409 
410   @param  Tcb     Pointer to the TCP_CB of this TCP instance.
411 
412   @return The sequence number of the maximum SndNxt.
413 
414 **/
415 TCP_SEQNO
416 TcpGetMaxSndNxt (
417   IN TCP_CB *Tcb
418   );
419 
420 //
421 // Functions from Tcp4Input.c
422 //
423 /**
424   Process the received ICMP error messages for TCP.
425 
426   @param  Nbuf     Buffer that contains part of the TCP segment without IP header
427                    truncated from the ICMP error packet.
428   @param  IcmpErr  The ICMP error code interpreted from ICMP error packet.
429   @param  Src      Source address of the ICMP error message.
430   @param  Dst      Destination address of the ICMP error message.
431 
432 **/
433 VOID
434 TcpIcmpInput (
435   IN NET_BUF     *Nbuf,
436   IN UINT8       IcmpErr,
437   IN UINT32      Src,
438   IN UINT32      Dst
439   );
440 
441 /**
442   Process the received TCP segments.
443 
444   @param  Nbuf     Buffer that contains received TCP segment without IP header.
445   @param  Src      Source address of the segment, or the peer's IP address.
446   @param  Dst      Destination address of the segment, or the local end's IP
447                    address.
448 
449   @retval 0        Segment is processed successfully. It is either accepted or
450                    discarded. But no connection is reset by the segment.
451   @retval -1       A connection is reset by the segment.
452 
453 **/
454 INTN
455 TcpInput (
456   IN NET_BUF *Nbuf,
457   IN UINT32  Src,
458   IN UINT32  Dst
459   );
460 
461 /**
462   Check whether the sequence number of the incoming segment is acceptable.
463 
464   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
465   @param  Seg      Pointer to the incoming segment.
466 
467   @retval 1       The sequence number is acceptable.
468   @retval 0       The sequence number is not acceptable.
469 
470 **/
471 INTN
472 TcpSeqAcceptable (
473   IN TCP_CB  *Tcb,
474   IN TCP_SEG *Seg
475   );
476 
477 /**
478   NewReno fast recovery, RFC3782.
479 
480   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
481   @param  Seg      Segment that triggers the fast recovery.
482 
483 **/
484 VOID
485 TcpFastRecover (
486   IN OUT TCP_CB  *Tcb,
487   IN     TCP_SEG *Seg
488   );
489 
490 /**
491   NewReno fast loss recovery, RFC3792.
492 
493   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
494   @param  Seg      Segment that triggers the fast loss recovery.
495 
496 **/
497 VOID
498 TcpFastLossRecover (
499   IN OUT TCP_CB  *Tcb,
500   IN     TCP_SEG *Seg
501   );
502 
503 /**
504   Compute the RTT as specified in RFC2988.
505 
506   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
507   @param  Measure  Currently measured RTT in heart beats.
508 
509 **/
510 VOID
511 TcpComputeRtt (
512   IN OUT TCP_CB *Tcb,
513   IN     UINT32 Measure
514   );
515 
516 /**
517   Trim off the data outside the tcb's receive window.
518 
519   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
520   @param  Nbuf     Pointer to the NET_BUF containing the received tcp segment.
521 
522 **/
523 VOID
524 TcpTrimInWnd (
525   IN TCP_CB  *Tcb,
526   IN NET_BUF *Nbuf
527   );
528 
529 /**
530   Store the data into the reassemble queue.
531 
532   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
533   @param  Nbuf     Pointer to the buffer containing the data to be queued.
534 
535 **/
536 VOID
537 TcpQueueData (
538   IN OUT TCP_CB  *Tcb,
539   IN     NET_BUF *Nbuf
540   );
541 
542 /**
543   Ajust the send queue or the retransmit queue.
544 
545   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
546   @param  Ack      The acknowledge seuqence number of the received segment.
547 
548 **/
549 VOID
550 TcpAdjustSndQue (
551   IN TCP_CB    *Tcb,
552   IN TCP_SEQNO Ack
553   );
554 
555 //
556 // Functions from Tcp4Misc.c
557 //
558 /**
559   Compute the TCP segment's checksum.
560 
561   @param  Nbuf                  Pointer to the buffer that contains the TCP
562                                 segment.
563   @param  HeadSum               The checksum value of the fixed part of pseudo
564                                 header.
565 
566   @return  The checksum value.
567 
568 **/
569 UINT16
570 TcpChecksum (
571   IN NET_BUF *Nbuf,
572   IN UINT16  HeadSum
573   );
574 
575 /**
576   Translate the information from the head of the received TCP
577   segment Nbuf contains and fill it into a TCP_SEG structure.
578 
579   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
580   @param  Nbuf                  Pointer to the buffer contains the TCP segment.
581 
582   @return  Pointer to the TCP_SEG that contains the translated TCP head information.
583 
584 **/
585 TCP_SEG *
586 TcpFormatNetbuf (
587   IN     TCP_CB  *Tcb,
588   IN OUT NET_BUF *Nbuf
589   );
590 
591 /**
592   Initialize an active connection.
593 
594   @param  Tcb                   Pointer to the TCP_CB that wants to initiate a
595                                 connection.
596 
597 **/
598 VOID
599 TcpOnAppConnect (
600   IN OUT TCP_CB  *Tcb
601   );
602 
603 /**
604   Application has consumed some data, check whether
605   to send a window updata ack or a delayed ack.
606 
607   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
608 
609 **/
610 VOID
611 TcpOnAppConsume (
612   IN TCP_CB *Tcb
613   );
614 
615 /**
616   Initiate the connection close procedure, called when
617   applications want to close the connection.
618 
619   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
620 
621 **/
622 VOID
623 TcpOnAppClose (
624   IN OUT TCP_CB *Tcb
625   );
626 
627 /**
628   Check whether the application's newly delivered data can be sent out.
629 
630   @param  Tcb                   Pointer to the TCP_CB of this TCP instance.
631 
632   @retval 0                     Whether the data is sent out or is buffered for
633                                 further sending.
634   @retval -1                    The Tcb is not in a state that data is permitted to
635                                 be sent out.
636 
637 **/
638 INTN
639 TcpOnAppSend (
640   IN OUT TCP_CB *Tcb
641   );
642 
643 /**
644   Abort the connection by sending a reset segment, called
645   when the application wants to abort the connection.
646 
647   @param  Tcb                   Pointer to the TCP_CB of the TCP instance.
648 
649 **/
650 VOID
651 TcpOnAppAbort (
652   IN TCP_CB *Tcb
653   );
654 
655 /**
656   Reset the connection related with Tcb.
657 
658   @param  Tcb                   Pointer to the TCP_CB of the connection to be
659                                 reset.
660 
661 **/
662 VOID
663 TcpResetConnection (
664   IN TCP_CB *Tcb
665   );
666 
667 //
668 // Functions in Tcp4Timer.c
669 //
670 /**
671   Close the TCP connection.
672 
673   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
674 
675 **/
676 VOID
677 TcpClose (
678   IN OUT TCP_CB *Tcb
679   );
680 
681 /**
682   Heart beat timer handler, queues the DPC at TPL_CALLBACK.
683 
684   @param  Event    Timer event signaled, ignored.
685   @param  Context  Context of the timer event, ignored.
686 
687 **/
688 VOID
689 EFIAPI
690 TcpTicking (
691   IN EFI_EVENT Event,
692   IN VOID      *Context
693   );
694 
695 /**
696   Enable a TCP timer.
697 
698   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
699   @param  Timer    The index of the timer to be enabled.
700   @param  TimeOut  The timeout value of this timer.
701 
702 **/
703 VOID
704 TcpSetTimer (
705   IN OUT TCP_CB *Tcb,
706   IN     UINT16 Timer,
707   IN     UINT32 TimeOut
708   );
709 
710 /**
711   Clear one TCP timer.
712 
713   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
714   @param  Timer    The index of the timer to be cleared.
715 
716 **/
717 VOID
718 TcpClearTimer (
719   IN OUT TCP_CB *Tcb,
720   IN     UINT16 Timer
721   );
722 
723 /**
724   Clear all TCP timers.
725 
726   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
727 
728 **/
729 VOID
730 TcpClearAllTimer (
731   IN OUT TCP_CB *Tcb
732   );
733 
734 /**
735   Enable the window prober timer and set the timeout value.
736 
737   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
738 
739 **/
740 VOID
741 TcpSetProbeTimer (
742   IN OUT TCP_CB *Tcb
743   );
744 
745 /**
746   Enable the keepalive timer and set the timeout value.
747 
748   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
749 
750 **/
751 VOID
752 TcpSetKeepaliveTimer (
753   IN OUT TCP_CB *Tcb
754   );
755 
756 /**
757   Backoff the RTO.
758 
759   @param  Tcb      Pointer to the TCP_CB of this TCP instance.
760 
761 **/
762 VOID
763 TcpBackoffRto (
764   IN OUT TCP_CB *Tcb
765   );
766 
767 /**
768   Install the device path protocol on the TCP instance.
769 
770   @param  Sock             Pointer to the socket representing the TCP instance.
771 
772   @retval  EFI_SUCCESS     The device path protocol is installed.
773   @retval  other           Failed to install the device path protocol.
774 
775 **/
776 EFI_STATUS
777 TcpInstallDevicePath (
778   IN SOCKET *Sock
779   );
780 
781 #endif
782