1.\" $OpenBSD: tcp.4,v 1.22 2014/01/21 03:15:46 schwarze 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: January 21 2014 $ 34.Dt TCP 4 35.Os 36.Sh NAME 37.Nm tcp 38.Nd Internet Transmission Control Protocol 39.Sh SYNOPSIS 40.Fd #include <sys/socket.h> 41.Fd #include <netinet/in.h> 42.Ft int 43.Fn socket AF_INET SOCK_STREAM 0 44.Ft int 45.Fn socket AF_INET6 SOCK_STREAM 0 46.Sh DESCRIPTION 47The 48.Tn TCP 49protocol provides a reliable, flow-controlled, two-way 50transmission of data. 51It is a byte-stream protocol used to support the 52.Dv SOCK_STREAM 53abstraction. 54TCP uses the standard 55Internet address format and, in addition, provides a per-host 56collection of 57.Dq port addresses . 58Thus, each address is composed 59of an Internet address specifying the host and network, with 60a specific 61.Tn TCP 62port on the host identifying the peer entity. 63.Pp 64Sockets utilizing the TCP protocol are either 65.Dq active 66or 67.Dq passive . 68Active sockets initiate connections to passive 69sockets. 70By default 71.Tn TCP 72sockets are created active; to create a 73passive socket the 74.Xr listen 2 75system call must be used 76after binding the socket with the 77.Xr bind 2 78system call. 79Only passive sockets may use the 80.Xr accept 2 81call to accept incoming connections. 82Only active sockets may use the 83.Xr connect 2 84call to initiate connections. 85.Pp 86Passive sockets may 87.Dq underspecify 88their location to match 89incoming connection requests from multiple networks. 90This technique, termed 91.Dq wildcard addressing , 92allows a single 93server to provide service to clients on multiple networks. 94To create a socket which listens on all networks, the Internet 95address 96.Dv INADDR_ANY 97must be bound. 98The 99.Tn TCP 100port may still be specified 101at this time; if the port is not specified the system will assign one. 102Once a connection has been established the socket's address is 103fixed by the peer entity's location. 104The address assigned to the socket is the address associated with 105the network interface through which packets are being transmitted 106and received. 107Normally this address corresponds to the peer entity's network. 108.Pp 109.Tn TCP 110supports several socket options which are set with 111.Xr setsockopt 2 112and tested with 113.Xr getsockopt 2 . 114.Bl -ohang 115.It Cd TCP_NODELAY 116Under most circumstances, 117.Tn TCP 118sends data when it is presented; 119when outstanding data has not yet been acknowledged, it gathers 120small amounts of output to be sent in a single packet once 121an acknowledgement is received. 122For a small number of clients, such as window systems 123that send a stream of mouse events which receive no replies, 124this packetization may cause significant delays. 125Therefore, 126.Tn TCP 127provides a boolean option, 128.Dv TCP_NODELAY 129(from 130.In netinet/tcp.h ) , 131to defeat this algorithm. 132.It Cd TCP_NOPUSH 133By convention, the 134.Tn TCP 135sender will set the 136.Dq push 137bit and begin transmission immediately (if permitted) at the 138end of every user call to 139.Xr write 2 140or 141.Xr writev 2 . 142When this option is set to a non-zero value, 143.Tn TCP 144will delay sending any data at all until either the socket 145is closed, the internal send buffer is filled, or this option 146is set to a zero value. 147.It Cd TCP_MAXSEG 148Set the maximum segment size for this connection. 149The maximum segment size can only be lowered. 150.It Cd TCP_SACK_ENABLE 151Use selective acknowledgements for this connection. 152See 153.Xr options 4 . 154.It Cd TCP_MD5SIG 155Use TCP MD5 signatures per RFC 2385. 156This requires 157.Em Security Associations 158to be set up, which can be done using 159.Xr ipsecctl 8 . 160When a listening socket has 161.Em TCP_MD5SIG 162set, it accepts connections with MD5 signatures only from sources for which a 163.Em Security Association 164is set up. 165Connections without MD5 signatures are only accepted from sources for which no 166.Em Security Association 167is set up. 168The connected socket only has 169.Em TCP_MD5SIG 170set if the connection is protected with MD5 signatures. 171.El 172.Pp 173The option level for the 174.Xr setsockopt 2 175call is the protocol number for 176.Tn TCP , 177available from 178.Xr getprotobyname 3 . 179.Pp 180Options at the 181.Tn IP 182transport level may be used with 183.Tn TCP ; 184see 185.Xr ip 4 186or 187.Xr ip6 4 . 188Incoming connection requests that are source-routed are noted, 189and the reverse source route is used in responding. 190.Sh DIAGNOSTICS 191A socket operation may fail with one of the following errors returned: 192.Bl -tag -width [EADDRNOTAVAIL] 193.It Bq Er EISCONN 194when trying to establish a connection on a socket which 195already has one; 196.It Bq Er ENOBUFS 197when the system runs out of memory for 198an internal data structure; 199.It Bq Er ETIMEDOUT 200when a connection was dropped 201due to excessive retransmissions; 202.It Bq Er ECONNRESET 203when the remote peer 204forces the connection to be closed; 205.It Bq Er ECONNREFUSED 206when the remote 207peer actively refuses connection establishment (usually because 208no process is listening to the port); 209.It Bq Er EADDRINUSE 210when an attempt 211is made to create a socket with a port which has already been 212allocated; 213.It Bq Er EADDRNOTAVAIL 214when an attempt is made to create a 215socket with a network address for which no network interface 216exists. 217.El 218.Sh SEE ALSO 219.Xr tcpbench 1 , 220.Xr getsockopt 2 , 221.Xr socket 2 , 222.Xr inet 4 , 223.Xr inet6 4 , 224.Xr ip 4 , 225.Xr ip6 4 , 226.Xr netintro 4 , 227.Xr ipsecctl 8 , 228.Xr tcpdrop 8 229.Sh HISTORY 230The 231.Nm 232protocol stack appeared in 233.Bx 4.2 . 234