xref: /freebsd/share/man/man4/tcp.4 (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.
3.\" Copyright (c) 2010-2011 The FreeBSD Foundation
4.\" All rights reserved.
5.\"
6.\" Portions of this documentation were written at the Centre for Advanced
7.\" Internet Architectures, Swinburne University of Technology, Melbourne,
8.\" Australia by David Hayes under sponsorship from the FreeBSD Foundation.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     From: @(#)tcp.4	8.1 (Berkeley) 6/5/93
35.\" $FreeBSD$
36.\"
37.Dd June 28, 2016
38.Dt TCP 4
39.Os
40.Sh NAME
41.Nm tcp
42.Nd Internet Transmission Control Protocol
43.Sh SYNOPSIS
44.In sys/types.h
45.In sys/socket.h
46.In netinet/in.h
47.In netinet/tcp.h
48.Ft int
49.Fn socket AF_INET SOCK_STREAM 0
50.Sh DESCRIPTION
51The
52.Tn TCP
53protocol provides reliable, flow-controlled, two-way
54transmission of data.
55It is a byte-stream protocol used to
56support the
57.Dv SOCK_STREAM
58abstraction.
59.Tn TCP
60uses the standard
61Internet address format and, in addition, provides a per-host
62collection of
63.Dq "port addresses" .
64Thus, each address is composed
65of an Internet address specifying the host and network,
66with a specific
67.Tn TCP
68port on the host identifying the peer entity.
69.Pp
70Sockets utilizing the
71.Tn TCP
72protocol are either
73.Dq active
74or
75.Dq passive .
76Active sockets initiate connections to passive
77sockets.
78By default,
79.Tn TCP
80sockets are created active; to create a
81passive socket, the
82.Xr listen 2
83system call must be used
84after binding the socket with the
85.Xr bind 2
86system call.
87Only passive sockets may use the
88.Xr accept 2
89call to accept incoming connections.
90Only active sockets may use the
91.Xr connect 2
92call to initiate connections.
93.Pp
94Passive sockets may
95.Dq underspecify
96their location to match
97incoming connection requests from multiple networks.
98This technique, termed
99.Dq "wildcard addressing" ,
100allows a single
101server to provide service to clients on multiple networks.
102To create a socket which listens on all networks, the Internet
103address
104.Dv INADDR_ANY
105must be bound.
106The
107.Tn TCP
108port may still be specified
109at this time; if the port is not specified, the system will assign one.
110Once a connection has been established, the socket's address is
111fixed by the peer entity's location.
112The address assigned to the
113socket is the address associated with the network interface
114through which packets are being transmitted and received.
115Normally, this address corresponds to the peer entity's network.
116.Pp
117.Tn TCP
118supports a number of socket options which can be set with
119.Xr setsockopt 2
120and tested with
121.Xr getsockopt 2 :
122.Bl -tag -width ".Dv TCP_FUNCTION_BLK"
123.It Dv TCP_INFO
124Information about a socket's underlying TCP session may be retrieved
125by passing the read-only option
126.Dv TCP_INFO
127to
128.Xr getsockopt 2 .
129It accepts a single argument: a pointer to an instance of
130.Vt "struct tcp_info" .
131.Pp
132This API is subject to change; consult the source to determine
133which fields are currently filled out by this option.
134.Fx
135specific additions include
136send window size,
137receive window size,
138and
139bandwidth-controlled window space.
140.It Dv TCP_CCALGOOPT
141Set or query congestion control algorithm specific parameters.
142See
143.Xr mod_cc 4
144for details.
145.It Dv TCP_CONGESTION
146Select or query the congestion control algorithm that TCP will use for the
147connection.
148See
149.Xr mod_cc 4
150for details.
151.It Dv TCP_FUNCTION_BLK
152Select or query the set of functions that TCP will use for this connection.
153This allows a user to select an alternate TCP stack.
154The alternate TCP stack must already be loaded in the kernel.
155To list the available TCP stacks, see
156.Va functions_available
157in the
158.Sx MIB Variables
159section further down.
160To list the default TCP stack, see
161.Va functions_default
162in the
163.Sx MIB Variables
164section.
165.It Dv TCP_KEEPINIT
166This
167.Xr setsockopt 2
168option accepts a per-socket timeout argument of
169.Vt "u_int"
170in seconds, for new, non-established
171.Tn TCP
172connections.
173For the global default in milliseconds see
174.Va keepinit
175in the
176.Sx MIB Variables
177section further down.
178.It Dv TCP_KEEPIDLE
179This
180.Xr setsockopt 2
181option accepts an argument of
182.Vt "u_int"
183for the amount of time, in seconds, that the connection must be idle
184before keepalive probes (if enabled) are sent for the connection of this
185socket.
186If set on a listening socket, the value is inherited by the newly created
187socket upon
188.Xr accept 2 .
189For the global default in milliseconds see
190.Va keepidle
191in the
192.Sx MIB Variables
193section further down.
194.It Dv TCP_KEEPINTVL
195This
196.Xr setsockopt 2
197option accepts an argument of
198.Vt "u_int"
199to set the per-socket interval, in seconds, between keepalive probes sent
200to a peer.
201If set on a listening socket, the value is inherited by the newly created
202socket upon
203.Xr accept 2 .
204For the global default in milliseconds see
205.Va keepintvl
206in the
207.Sx MIB Variables
208section further down.
209.It Dv TCP_KEEPCNT
210This
211.Xr setsockopt 2
212option accepts an argument of
213.Vt "u_int"
214and allows a per-socket tuning of the number of probes sent, with no response,
215before the connection will be dropped.
216If set on a listening socket, the value is inherited by the newly created
217socket upon
218.Xr accept 2 .
219For the global default see the
220.Va keepcnt
221in the
222.Sx MIB Variables
223section further down.
224.It Dv TCP_NODELAY
225Under most circumstances,
226.Tn TCP
227sends data when it is presented;
228when outstanding data has not yet been acknowledged, it gathers
229small amounts of output to be sent in a single packet once
230an acknowledgement is received.
231For a small number of clients, such as window systems
232that send a stream of mouse events which receive no replies,
233this packetization may cause significant delays.
234The boolean option
235.Dv TCP_NODELAY
236defeats this algorithm.
237.It Dv TCP_MAXSEG
238By default, a sender- and
239.No receiver- Ns Tn TCP
240will negotiate among themselves to determine the maximum segment size
241to be used for each connection.
242The
243.Dv TCP_MAXSEG
244option allows the user to determine the result of this negotiation,
245and to reduce it if desired.
246.It Dv TCP_NOOPT
247.Tn TCP
248usually sends a number of options in each packet, corresponding to
249various
250.Tn TCP
251extensions which are provided in this implementation.
252The boolean option
253.Dv TCP_NOOPT
254is provided to disable
255.Tn TCP
256option use on a per-connection basis.
257.It Dv TCP_NOPUSH
258By convention, the
259.No sender- Ns Tn TCP
260will set the
261.Dq push
262bit, and begin transmission immediately (if permitted) at the end of
263every user call to
264.Xr write 2
265or
266.Xr writev 2 .
267When this option is set to a non-zero value,
268.Tn TCP
269will delay sending any data at all until either the socket is closed,
270or the internal send buffer is filled.
271.It Dv TCP_MD5SIG
272This option enables the use of MD5 digests (also known as TCP-MD5)
273on writes to the specified socket.
274Outgoing traffic is digested;
275digests on incoming traffic are verified if the
276.Va net.inet.tcp.signature_verify_input
277sysctl is nonzero.
278The current default behavior for the system is to respond to a system
279advertising this option with TCP-MD5; this may change.
280.Pp
281One common use for this in a
282.Fx
283router deployment is to enable
284based routers to interwork with Cisco equipment at peering points.
285Support for this feature conforms to RFC 2385.
286Only IPv4
287.Pq Dv AF_INET
288sessions are supported.
289.Pp
290In order for this option to function correctly, it is necessary for the
291administrator to add a tcp-md5 key entry to the system's security
292associations database (SADB) using the
293.Xr setkey 8
294utility.
295This entry must have an SPI of 0x1000 and can therefore only be specified
296on a per-host basis at this time.
297.Pp
298If an SADB entry cannot be found for the destination, the outgoing traffic
299will have an invalid digest option prepended, and the following error message
300will be visible on the system console:
301.Em "tcp_signature_compute: SADB lookup failed for %d.%d.%d.%d" .
302.El
303.Pp
304The option level for the
305.Xr setsockopt 2
306call is the protocol number for
307.Tn TCP ,
308available from
309.Xr getprotobyname 3 ,
310or
311.Dv IPPROTO_TCP .
312All options are declared in
313.In netinet/tcp.h .
314.Pp
315Options at the
316.Tn IP
317transport level may be used with
318.Tn TCP ;
319see
320.Xr ip 4 .
321Incoming connection requests that are source-routed are noted,
322and the reverse source route is used in responding.
323.Pp
324The default congestion control algorithm for
325.Tn TCP
326is
327.Xr cc_newreno 4 .
328Other congestion control algorithms can be made available using the
329.Xr mod_cc 4
330framework.
331.Ss MIB Variables
332The
333.Tn TCP
334protocol implements a number of variables in the
335.Va net.inet.tcp
336branch of the
337.Xr sysctl 3
338MIB.
339.Bl -tag -width ".Va TCPCTL_DO_RFC1323"
340.It Dv TCPCTL_DO_RFC1323
341.Pq Va rfc1323
342Implement the window scaling and timestamp options of RFC 1323
343(default is true).
344.It Dv TCPCTL_MSSDFLT
345.Pq Va mssdflt
346The default value used for the maximum segment size
347.Pq Dq MSS
348when no advice to the contrary is received from MSS negotiation.
349.It Dv TCPCTL_SENDSPACE
350.Pq Va sendspace
351Maximum
352.Tn TCP
353send window.
354.It Dv TCPCTL_RECVSPACE
355.Pq Va recvspace
356Maximum
357.Tn TCP
358receive window.
359.It Va log_in_vain
360Log any connection attempts to ports where there is not a socket
361accepting connections.
362The value of 1 limits the logging to
363.Tn SYN
364(connection establishment) packets only.
365That of 2 results in any
366.Tn TCP
367packets to closed ports being logged.
368Any value unlisted above disables the logging
369(default is 0, i.e., the logging is disabled).
370.It Va msl
371The Maximum Segment Lifetime, in milliseconds, for a packet.
372.It Va keepinit
373Timeout, in milliseconds, for new, non-established
374.Tn TCP
375connections.
376The default is 75000 msec.
377.It Va keepidle
378Amount of time, in milliseconds, that the connection must be idle
379before keepalive probes (if enabled) are sent.
380The default is 7200000 msec (2 hours).
381.It Va keepintvl
382The interval, in milliseconds, between keepalive probes sent to remote
383machines, when no response is received on a
384.Va keepidle
385probe.
386The default is 75000 msec.
387.It Va keepcnt
388Number of probes sent, with no response, before a connection
389is dropped.
390The default is 8 packets.
391.It Va always_keepalive
392Assume that
393.Dv SO_KEEPALIVE
394is set on all
395.Tn TCP
396connections, the kernel will
397periodically send a packet to the remote host to verify the connection
398is still up.
399.It Va icmp_may_rst
400Certain
401.Tn ICMP
402unreachable messages may abort connections in
403.Tn SYN-SENT
404state.
405.It Va do_tcpdrain
406Flush packets in the
407.Tn TCP
408reassembly queue if the system is low on mbufs.
409.It Va blackhole
410If enabled, disable sending of RST when a connection is attempted
411to a port where there is not a socket accepting connections.
412See
413.Xr blackhole 4 .
414.It Va delayed_ack
415Delay ACK to try and piggyback it onto a data packet.
416.It Va delacktime
417Maximum amount of time, in milliseconds, before a delayed ACK is sent.
418.It Va path_mtu_discovery
419Enable Path MTU Discovery.
420.It Va tcbhashsize
421Size of the
422.Tn TCP
423control-block hash table
424(read-only).
425This may be tuned using the kernel option
426.Dv TCBHASHSIZE
427or by setting
428.Va net.inet.tcp.tcbhashsize
429in the
430.Xr loader 8 .
431.It Va pcbcount
432Number of active process control blocks
433(read-only).
434.It Va syncookies
435Determines whether or not
436.Tn SYN
437cookies should be generated for outbound
438.Tn SYN-ACK
439packets.
440.Tn SYN
441cookies are a great help during
442.Tn SYN
443flood attacks, and are enabled by default.
444(See
445.Xr syncookies 4 . )
446.It Va isn_reseed_interval
447The interval (in seconds) specifying how often the secret data used in
448RFC 1948 initial sequence number calculations should be reseeded.
449By default, this variable is set to zero, indicating that
450no reseeding will occur.
451Reseeding should not be necessary, and will break
452.Dv TIME_WAIT
453recycling for a few minutes.
454.It Va rexmit_min , rexmit_slop
455Adjust the retransmit timer calculation for
456.Tn TCP .
457The slop is
458typically added to the raw calculation to take into account
459occasional variances that the
460.Tn SRTT
461(smoothed round-trip time)
462is unable to accommodate, while the minimum specifies an
463absolute minimum.
464While a number of
465.Tn TCP
466RFCs suggest a 1
467second minimum, these RFCs tend to focus on streaming behavior,
468and fail to deal with the fact that a 1 second minimum has severe
469detrimental effects over lossy interactive connections, such
470as a 802.11b wireless link, and over very fast but lossy
471connections for those cases not covered by the fast retransmit
472code.
473For this reason, we use 200ms of slop and a near-0
474minimum, which gives us an effective minimum of 200ms (similar to
475.Tn Linux ) .
476.It Va initcwnd_segments
477Enable the ability to specify initial congestion window in number of segments.
478The default value is 10 as suggested by RFC 6928.
479Changing the value on fly would not affect connections using congestion window
480from the hostcache.
481Caution:
482This regulates the burst of packets allowed to be sent in the first RTT.
483The value should be relative to the link capacity.
484Start with small values for lower-capacity links.
485Large bursts can cause buffer overruns and packet drops if routers have small
486buffers or the link is experiencing congestion.
487.It Va rfc3042
488Enable the Limited Transmit algorithm as described in RFC 3042.
489It helps avoid timeouts on lossy links and also when the congestion window
490is small, as happens on short transfers.
491.It Va rfc3390
492Enable support for RFC 3390, which allows for a variable-sized
493starting congestion window on new connections, depending on the
494maximum segment size.
495This helps throughput in general, but
496particularly affects short transfers and high-bandwidth large
497propagation-delay connections.
498.It Va sack.enable
499Enable support for RFC 2018, TCP Selective Acknowledgment option,
500which allows the receiver to inform the sender about all successfully
501arrived segments, allowing the sender to retransmit the missing segments
502only.
503.It Va sack.maxholes
504Maximum number of SACK holes per connection.
505Defaults to 128.
506.It Va sack.globalmaxholes
507Maximum number of SACK holes per system, across all connections.
508Defaults to 65536.
509.It Va maxtcptw
510When a TCP connection enters the
511.Dv TIME_WAIT
512state, its associated socket structure is freed, since it is of
513negligible size and use, and a new structure is allocated to contain a
514minimal amount of information necessary for sustaining a connection in
515this state, called the compressed TCP TIME_WAIT state.
516Since this structure is smaller than a socket structure, it can save
517a significant amount of system memory.
518The
519.Va net.inet.tcp.maxtcptw
520MIB variable controls the maximum number of these structures allocated.
521By default, it is initialized to
522.Va kern.ipc.maxsockets
523/ 5.
524.It Va nolocaltimewait
525Suppress creating of compressed TCP TIME_WAIT states for connections in
526which both endpoints are local.
527.It Va fast_finwait2_recycle
528Recycle
529.Tn TCP
530.Dv FIN_WAIT_2
531connections faster when the socket is marked as
532.Dv SBS_CANTRCVMORE
533(no user process has the socket open, data received on
534the socket cannot be read).
535The timeout used here is
536.Va finwait2_timeout .
537.It Va finwait2_timeout
538Timeout to use for fast recycling of
539.Tn TCP
540.Dv FIN_WAIT_2
541connections.
542Defaults to 60 seconds.
543.It Va ecn.enable
544Enable support for TCP Explicit Congestion Notification (ECN).
545ECN allows a TCP sender to reduce the transmission rate in order to
546avoid packet drops.
547Settings:
548.Bl -tag -compact
549.It 0
550Disable ECN.
551.It 1
552Allow incoming connections to request ECN.
553Outgoing connections will request ECN.
554.It 2
555Allow incoming connections to request ECN.
556Outgoing connections will not request ECN.
557.El
558.It Va ecn.maxretries
559Number of retries (SYN or SYN/ACK retransmits) before disabling ECN on a
560specific connection.
561This is needed to help with connection establishment
562when a broken firewall is in the network path.
563.It Va pmtud_blackhole_detection
564Turn on automatic path MTU blackhole detection.
565In case of retransmits OS will
566lower the MSS to check if it's MTU problem.
567If current MSS is greater than
568configured value to try, it will be set to configured value, otherwise,
569MSS will be set to default values
570.Po Va net.inet.tcp.mssdflt
571and
572.Va net.inet.tcp.v6mssdflt
573.Pc .
574.It Va pmtud_blackhole_mss
575MSS to try for IPv4 if PMTU blackhole detection is turned on.
576.It Va v6pmtud_blackhole_mss
577MSS to try for IPv6 if PMTU blackhole detection is turned on.
578.It Va pmtud_blackhole_activated
579Number of times configured values were used in an attempt to downshift.
580.It Va pmtud_blackhole_activated_min_mss
581Number of times default MSS was used in an attempt to downshift.
582.It Va pmtud_blackhole_failed
583Number of connections for which retransmits continued even after MSS
584downshift.
585.It Va functions_available
586List of available TCP function blocks (TCP stacks).
587.It Va functions_default
588The default TCP function block (TCP stack).
589.El
590.Sh ERRORS
591A socket operation may fail with one of the following errors returned:
592.Bl -tag -width Er
593.It Bq Er EISCONN
594when trying to establish a connection on a socket which
595already has one;
596.It Bq Er ENOBUFS
597when the system runs out of memory for
598an internal data structure;
599.It Bq Er ETIMEDOUT
600when a connection was dropped
601due to excessive retransmissions;
602.It Bq Er ECONNRESET
603when the remote peer
604forces the connection to be closed;
605.It Bq Er ECONNREFUSED
606when the remote
607peer actively refuses connection establishment (usually because
608no process is listening to the port);
609.It Bq Er EADDRINUSE
610when an attempt
611is made to create a socket with a port which has already been
612allocated;
613.It Bq Er EADDRNOTAVAIL
614when an attempt is made to create a
615socket with a network address for which no network interface
616exists;
617.It Bq Er EAFNOSUPPORT
618when an attempt is made to bind or connect a socket to a multicast
619address.
620.It Bq Er EINVAL
621when trying to change TCP function blocks at an invalid point in the session;
622.It Bq Er ENOENT
623when trying to use a TCP function block that is not available;
624.El
625.Sh SEE ALSO
626.Xr getsockopt 2 ,
627.Xr socket 2 ,
628.Xr sysctl 3 ,
629.Xr blackhole 4 ,
630.Xr inet 4 ,
631.Xr intro 4 ,
632.Xr ip 4 ,
633.Xr mod_cc 4 ,
634.Xr siftr 4 ,
635.Xr syncache 4 ,
636.Xr setkey 8 ,
637.Xr tcp_functions 9
638.Rs
639.%A "V. Jacobson"
640.%A "R. Braden"
641.%A "D. Borman"
642.%T "TCP Extensions for High Performance"
643.%O "RFC 1323"
644.Re
645.Rs
646.%A "A. Heffernan"
647.%T "Protection of BGP Sessions via the TCP MD5 Signature Option"
648.%O "RFC 2385"
649.Re
650.Rs
651.%A "K. Ramakrishnan"
652.%A "S. Floyd"
653.%A "D. Black"
654.%T "The Addition of Explicit Congestion Notification (ECN) to IP"
655.%O "RFC 3168"
656.Re
657.Sh HISTORY
658The
659.Tn TCP
660protocol appeared in
661.Bx 4.2 .
662The RFC 1323 extensions for window scaling and timestamps were added
663in
664.Bx 4.4 .
665The
666.Dv TCP_INFO
667option was introduced in
668.Tn Linux 2.6
669and is
670.Em subject to change .
671