1.\" $OpenBSD: tcp.4,v 1.27 2022/08/11 14:04:08 jmc 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: August 11 2022 $ 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. 162See 163.Xr options 4 . 164.It Cd TCP_MD5SIG 165Use TCP MD5 signatures per RFC 2385. 166This requires 167.Em Security Associations 168to be set up, which can be done using 169.Xr ipsecctl 8 . 170When a listening socket has 171.Em TCP_MD5SIG 172set, it accepts connections with MD5 signatures only from sources for which a 173.Em Security Association 174is set up. 175Connections without MD5 signatures are only accepted from sources for which no 176.Em Security Association 177is set up. 178The connected socket only has 179.Em TCP_MD5SIG 180set if the connection is protected with MD5 signatures. 181.El 182.Pp 183The option level for the 184.Xr setsockopt 2 185call is the protocol number for 186.Tn TCP , 187available from 188.Xr getprotobyname 3 . 189.Pp 190Options at the 191.Tn IP 192transport level may be used with 193.Tn TCP ; 194see 195.Xr ip 4 196or 197.Xr ip6 4 . 198Incoming connection requests that are source-routed are noted, 199and the reverse source route is used in responding. 200.Sh DIAGNOSTICS 201A socket operation may fail with one of the following errors returned: 202.Bl -tag -width [EADDRNOTAVAIL] 203.It Bq Er EISCONN 204when trying to establish a connection on a socket which 205already has one; 206.It Bq Er ENOBUFS 207when the system runs out of memory for 208an internal data structure; 209.It Bq Er ETIMEDOUT 210when a connection was dropped 211due to excessive retransmissions; 212.It Bq Er ECONNRESET 213when the remote peer 214forces the connection to be closed; 215.It Bq Er ECONNREFUSED 216when the remote 217peer actively refuses connection establishment (usually because 218no process is listening to the port); 219.It Bq Er EADDRINUSE 220when an attempt 221is made to create a socket with a port which has already been 222allocated; 223.It Bq Er EADDRNOTAVAIL 224when an attempt is made to create a 225socket with a network address for which no network interface 226exists. 227.El 228.Sh SEE ALSO 229.Xr tcpbench 1 , 230.Xr getsockopt 2 , 231.Xr socket 2 , 232.Xr inet 4 , 233.Xr inet6 4 , 234.Xr ip 4 , 235.Xr ip6 4 , 236.Xr netintro 4 , 237.Xr ipsecctl 8 , 238.Xr tcpdrop 8 239.Sh HISTORY 240The 241.Nm 242protocol stack appeared in 243.Bx 4.2 . 244