xref: /openbsd-src/share/man/man4/tcp.4 (revision ac4b1d577783dc97046b605745e5fc251644d7dd)
1.\"	$OpenBSD: tcp.4,v 1.28 2024/12/01 08:11:14 pascal Exp $
2.\"	$NetBSD: tcp.4,v 1.3 1994/11/30 16:22:35 jtc Exp $
3.\"
4.\" Copyright (c) 1983, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)tcp.4	8.1 (Berkeley) 6/5/93
32.\"
33.Dd $Mdocdate: December 1 2024 $
34.Dt TCP 4
35.Os
36.Sh NAME
37.Nm tcp
38.Nd Internet Transmission Control Protocol
39.Sh SYNOPSIS
40.In sys/socket.h
41.In netinet/in.h
42.In netinet/tcp.h
43.Ft int
44.Fn socket AF_INET SOCK_STREAM 0
45.Ft int
46.Fn socket AF_INET6 SOCK_STREAM 0
47.Sh DESCRIPTION
48The
49.Tn TCP
50protocol provides a reliable, flow-controlled, two-way
51transmission of data.
52It is a byte-stream protocol used to support the
53.Dv SOCK_STREAM
54abstraction.
55TCP uses the standard
56Internet address format and, in addition, provides a per-host
57collection of
58.Dq port addresses .
59Thus, each address is composed
60of an Internet address specifying the host and network, with
61a specific
62.Tn TCP
63port on the host identifying the peer entity.
64.Pp
65Sockets utilizing the TCP protocol are either
66.Dq active
67or
68.Dq passive .
69Active sockets initiate connections to passive
70sockets.
71By default
72.Tn TCP
73sockets are created active; to create a
74passive socket the
75.Xr listen 2
76system call must be used
77after binding the socket with the
78.Xr bind 2
79system call.
80Only passive sockets may use the
81.Xr accept 2
82call to accept incoming connections.
83Only active sockets may use the
84.Xr connect 2
85call to initiate connections.
86.Pp
87Passive sockets may
88.Dq underspecify
89their location to match
90incoming connection requests from multiple networks.
91This technique, termed
92.Dq wildcard addressing ,
93allows a single
94server to provide service to clients on multiple networks.
95To create a socket which listens on all networks, the Internet
96address
97.Dv INADDR_ANY
98must be bound.
99The
100.Tn TCP
101port may still be specified
102at this time; if the port is not specified the system will assign one.
103Once a connection has been established, the socket's address is
104fixed by the peer entity's location.
105The address assigned to the socket is the address associated with
106the network interface through which packets are being transmitted
107and received.
108Normally this address corresponds to the peer entity's network.
109.Pp
110.Tn TCP
111supports several socket options which are set with
112.Xr setsockopt 2
113and tested with
114.Xr getsockopt 2 .
115.Bl -ohang
116.It Cd TCP_INFO
117Retrieve information about a socket's underlying TCP session.
118.Dv TCP_INFO
119is only used with
120.Fn getsockopt .
121The argument is a pointer to an instance of
122.Vt "struct tcp_info"
123(from
124.In netinet/tcp.h ) .
125.It Cd TCP_NODELAY
126Under most circumstances,
127.Tn TCP
128sends data when it is presented;
129when outstanding data has not yet been acknowledged, it gathers
130small amounts of output to be sent in a single packet once
131an acknowledgement is received.
132For a small number of clients, such as window systems
133that send a stream of mouse events which receive no replies,
134this packetization may cause significant delays.
135Therefore,
136.Tn TCP
137provides a boolean option,
138.Dv TCP_NODELAY
139(from
140.In netinet/tcp.h ) ,
141to defeat this algorithm.
142.It Cd TCP_NOPUSH
143By convention, the
144.Tn TCP
145sender will set the
146.Dq push
147bit and begin transmission immediately (if permitted) at the
148end of every user call to
149.Xr write 2
150or
151.Xr writev 2 .
152When this option is set to a non-zero value,
153.Tn TCP
154will delay sending any data at all until either the socket
155is closed, the internal send buffer is filled, or this option
156is set to a zero value.
157.It Cd TCP_MAXSEG
158Set the maximum segment size for this connection.
159The maximum segment size can only be lowered.
160.It Cd TCP_SACK_ENABLE
161Use selective acknowledgements for this connection.
162Additional information about
163segments already received can be transmitted back to the sender,
164thus indicating segments that have been lost and allowing for
165a swifter recovery.
166Both communication endpoints need to support
167.Em SACK .
168The fallback behaviour is NewReno fast recovery phase, which allows
169one lost segment to be recovered per round trip time.
170When more than one segment has been dropped per window, the transmission can
171continue without waiting for a retransmission timeout.
172.It Cd TCP_MD5SIG
173Use TCP MD5 signatures per RFC 2385.
174This requires
175.Em Security Associations
176to be set up, which can be done using
177.Xr ipsecctl 8 .
178When a listening socket has
179.Em TCP_MD5SIG
180set, it accepts connections with MD5 signatures only from sources for which a
181.Em Security Association
182is set up.
183Connections without MD5 signatures are only accepted from sources for which no
184.Em Security Association
185is set up.
186The connected socket only has
187.Em TCP_MD5SIG
188set if the connection is protected with MD5 signatures.
189.El
190.Pp
191The option level for the
192.Xr setsockopt 2
193call is the protocol number for
194.Tn TCP ,
195available from
196.Xr getprotobyname 3 .
197.Pp
198Options at the
199.Tn IP
200transport level may be used with
201.Tn TCP ;
202see
203.Xr ip 4
204or
205.Xr ip6 4 .
206Incoming connection requests that are source-routed are noted,
207and the reverse source route is used in responding.
208.Sh DIAGNOSTICS
209A socket operation may fail with one of the following errors returned:
210.Bl -tag -width [EADDRNOTAVAIL]
211.It Bq Er EISCONN
212when trying to establish a connection on a socket which
213already has one;
214.It Bq Er ENOBUFS
215when the system runs out of memory for
216an internal data structure;
217.It Bq Er ETIMEDOUT
218when a connection was dropped
219due to excessive retransmissions;
220.It Bq Er ECONNRESET
221when the remote peer
222forces the connection to be closed;
223.It Bq Er ECONNREFUSED
224when the remote
225peer actively refuses connection establishment (usually because
226no process is listening to the port);
227.It Bq Er EADDRINUSE
228when an attempt
229is made to create a socket with a port which has already been
230allocated;
231.It Bq Er EADDRNOTAVAIL
232when an attempt is made to create a
233socket with a network address for which no network interface
234exists.
235.El
236.Sh SEE ALSO
237.Xr tcpbench 1 ,
238.Xr getsockopt 2 ,
239.Xr socket 2 ,
240.Xr inet 4 ,
241.Xr inet6 4 ,
242.Xr ip 4 ,
243.Xr ip6 4 ,
244.Xr netintro 4 ,
245.Xr ipsecctl 8 ,
246.Xr tcpdrop 8
247.Sh HISTORY
248The
249.Nm
250protocol stack appeared in
251.Bx 4.2 .
252