1.\" $OpenBSD: tcp.4,v 1.20 2008/05/09 09:08:47 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: May 9 2008 $ 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.Aq Pa netinet/tcp.h ) , 131to defeat this algorithm. 132.It Cd TCP_MAXSEG 133Set the maximum segment size for this connection. 134The maximum segment size can only be lowered. 135.It Cd TCP_SACK_ENABLE 136Use selective acknowledgements for this connection. 137See 138.Xr options 4 . 139.It Cd TCP_MD5SIG 140Use TCP MD5 signatures per RFC 2385. 141This requires 142.Em Security Associations 143to be set up, which can be done using 144.Xr ipsecctl 8 . 145When a listening socket has 146.Em TCP_MD5SIG 147set, it accepts connections with MD5 signatures only from sources for which a 148.Em Security Association 149is set up. 150Connections without MD5 signatures are only accepted from sources for which no 151.Em Security Association 152is set up. 153The connected socket only has 154.Em TCP_MD5SIG 155set if the connection is protected with MD5 signatures. 156.El 157.Pp 158The option level for the 159.Xr setsockopt 2 160call is the protocol number for 161.Tn TCP , 162available from 163.Xr getprotobyname 3 . 164.Pp 165Options at the 166.Tn IP 167transport level may be used with 168.Tn TCP ; 169see 170.Xr ip 4 171or 172.Xr ip6 4 . 173Incoming connection requests that are source-routed are noted, 174and the reverse source route is used in responding. 175.Sh DIAGNOSTICS 176A socket operation may fail with one of the following errors returned: 177.Bl -tag -width [EADDRNOTAVAIL] 178.It Bq Er EISCONN 179when trying to establish a connection on a socket which 180already has one; 181.It Bq Er ENOBUFS 182when the system runs out of memory for 183an internal data structure; 184.It Bq Er ETIMEDOUT 185when a connection was dropped 186due to excessive retransmissions; 187.It Bq Er ECONNRESET 188when the remote peer 189forces the connection to be closed; 190.It Bq Er ECONNREFUSED 191when the remote 192peer actively refuses connection establishment (usually because 193no process is listening to the port); 194.It Bq Er EADDRINUSE 195when an attempt 196is made to create a socket with a port which has already been 197allocated; 198.It Bq Er EADDRNOTAVAIL 199when an attempt is made to create a 200socket with a network address for which no network interface 201exists. 202.El 203.Sh SEE ALSO 204.Xr tcpbench 1 , 205.Xr getsockopt 2 , 206.Xr socket 2 , 207.Xr inet 4 , 208.Xr inet6 4 , 209.Xr ip 4 , 210.Xr ip6 4 , 211.Xr netintro 4 , 212.Xr ipsecctl 8 , 213.Xr tcpdrop 8 214.Sh HISTORY 215The 216.Nm 217protocol stack appeared in 218.Bx 4.2 . 219