1.\" $OpenBSD: tcp.4,v 1.24 2018/05/11 12:02:28 zhuk 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: May 11 2018 $ 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_NODELAY 117Under most circumstances, 118.Tn TCP 119sends data when it is presented; 120when outstanding data has not yet been acknowledged, it gathers 121small amounts of output to be sent in a single packet once 122an acknowledgement is received. 123For a small number of clients, such as window systems 124that send a stream of mouse events which receive no replies, 125this packetization may cause significant delays. 126Therefore, 127.Tn TCP 128provides a boolean option, 129.Dv TCP_NODELAY 130(from 131.In netinet/tcp.h ) , 132to defeat this algorithm. 133.It Cd TCP_NOPUSH 134By convention, the 135.Tn TCP 136sender will set the 137.Dq push 138bit and begin transmission immediately (if permitted) at the 139end of every user call to 140.Xr write 2 141or 142.Xr writev 2 . 143When this option is set to a non-zero value, 144.Tn TCP 145will delay sending any data at all until either the socket 146is closed, the internal send buffer is filled, or this option 147is set to a zero value. 148.It Cd TCP_MAXSEG 149Set the maximum segment size for this connection. 150The maximum segment size can only be lowered. 151.It Cd TCP_SACK_ENABLE 152Use selective acknowledgements for this connection. 153See 154.Xr options 4 . 155.It Cd TCP_MD5SIG 156Use TCP MD5 signatures per RFC 2385. 157This requires 158.Em Security Associations 159to be set up, which can be done using 160.Xr ipsecctl 8 . 161When a listening socket has 162.Em TCP_MD5SIG 163set, it accepts connections with MD5 signatures only from sources for which a 164.Em Security Association 165is set up. 166Connections without MD5 signatures are only accepted from sources for which no 167.Em Security Association 168is set up. 169The connected socket only has 170.Em TCP_MD5SIG 171set if the connection is protected with MD5 signatures. 172.El 173.Pp 174The option level for the 175.Xr setsockopt 2 176call is the protocol number for 177.Tn TCP , 178available from 179.Xr getprotobyname 3 . 180.Pp 181Options at the 182.Tn IP 183transport level may be used with 184.Tn TCP ; 185see 186.Xr ip 4 187or 188.Xr ip6 4 . 189Incoming connection requests that are source-routed are noted, 190and the reverse source route is used in responding. 191.Sh DIAGNOSTICS 192A socket operation may fail with one of the following errors returned: 193.Bl -tag -width [EADDRNOTAVAIL] 194.It Bq Er EISCONN 195when trying to establish a connection on a socket which 196already has one; 197.It Bq Er ENOBUFS 198when the system runs out of memory for 199an internal data structure; 200.It Bq Er ETIMEDOUT 201when a connection was dropped 202due to excessive retransmissions; 203.It Bq Er ECONNRESET 204when the remote peer 205forces the connection to be closed; 206.It Bq Er ECONNREFUSED 207when the remote 208peer actively refuses connection establishment (usually because 209no process is listening to the port); 210.It Bq Er EADDRINUSE 211when an attempt 212is made to create a socket with a port which has already been 213allocated; 214.It Bq Er EADDRNOTAVAIL 215when an attempt is made to create a 216socket with a network address for which no network interface 217exists. 218.El 219.Sh SEE ALSO 220.Xr tcpbench 1 , 221.Xr getsockopt 2 , 222.Xr socket 2 , 223.Xr inet 4 , 224.Xr inet6 4 , 225.Xr ip 4 , 226.Xr ip6 4 , 227.Xr netintro 4 , 228.Xr ipsecctl 8 , 229.Xr tcpdrop 8 230.Sh HISTORY 231The 232.Nm 233protocol stack appeared in 234.Bx 4.2 . 235