1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
<table class="head">
<tr>
<td class="head-ltitle">TCP(4)</td>
<td class="head-vol">Device Drivers Manual</td>
<td class="head-rtitle">TCP(4)</td>
</tr>
</table>
<div class="manual-text">
<section class="Sh">
<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1>
<p class="Pp"><code class="Nm">tcp</code> — <span class="Nd">Internet
Transmission Control Protocol</span></p>
</section>
<section class="Sh">
<h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<p class="Pp"><code class="In">#include
<<a class="In">sys/socket.h</a>></code>
<br/>
<code class="In">#include <<a class="In">netinet/in.h</a>></code></p>
<p class="Pp"><var class="Ft">int</var>
<br/>
<code class="Fn">socket</code>(<var class="Fa" style="white-space: nowrap;">AF_INET</var>,
<var class="Fa" style="white-space: nowrap;">SOCK_STREAM</var>,
<var class="Fa" style="white-space: nowrap;">0</var>);</p>
<p class="Pp"><var class="Ft">int</var>
<br/>
<code class="Fn">socket</code>(<var class="Fa" style="white-space: nowrap;">AF_INET6</var>,
<var class="Fa" style="white-space: nowrap;">SOCK_STREAM</var>,
<var class="Fa" style="white-space: nowrap;">0</var>);</p>
</section>
<section class="Sh">
<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1>
<p class="Pp">The TCP provides reliable, flow-controlled, two-way transmission
of data. It is a byte-stream protocol used to support the
<code class="Dv">SOCK_STREAM</code> abstraction. TCP uses the standard
Internet address format and, in addition, provides a per-host collection of
“port addresses”. Thus, each address is composed of an
Internet address specifying the host and network, with a specific TCP port
on the host identifying the peer entity.</p>
<p class="Pp">Sockets using TCP are either “active” or
“passive”. Active sockets initiate connections to passive
sockets. By default TCP sockets are created active; to create a passive
socket the <a class="Xr">listen(2)</a> system call must be used after
binding the socket with the <a class="Xr">bind(2)</a> system call. Only
passive sockets may use the <a class="Xr">accept(2)</a> call to accept
incoming connections. Only active sockets may use the
<a class="Xr">connect(2)</a> call to initiate connections.</p>
<p class="Pp">Passive sockets may “underspecify” their location to
match incoming connection requests from multiple networks. This technique,
termed “wildcard addressing”, allows a single server to
provide service to clients on multiple networks. To create a socket which
listens on all networks, the Internet address
<code class="Dv">INADDR_ANY</code> must be bound. The TCP port may still be
specified at this time; if the port is not specified the system will assign
one. Once a connection has been established the socket's address is fixed by
the peer entity's location. The address assigned the socket is the address
associated with the network interface through which packets are being
transmitted and received. Normally this address corresponds to the peer
entity's network.</p>
<p class="Pp">TCP supports a number of socket options which can be set with
<a class="Xr">setsockopt(2)</a> and tested with
<a class="Xr">getsockopt(2)</a>:</p>
<dl class="Bl-tag">
<dt id="TCP_NODELAY"><a class="permalink" href="#TCP_NODELAY"><code class="Dv">TCP_NODELAY</code></a></dt>
<dd>Under most circumstances, TCP sends data when it is presented; when
outstanding data has not yet been acknowledged, it gathers small amounts
of output to be sent in a single packet once an acknowledgment is
received. For a small number of clients, such as window systems that send
a stream of mouse events which receive no replies, this packetization may
cause significant delays. Therefore, TCP provides a boolean option,
<code class="Dv">TCP_NODELAY</code> (from
<code class="In"><<a class="In">netinet/tcp.h</a>></code>, to defeat
this algorithm.</dd>
<dt id="TCP_MAXSEG"><a class="permalink" href="#TCP_MAXSEG"><code class="Dv">TCP_MAXSEG</code></a></dt>
<dd>By default, a sender- and receiver-TCP will negotiate among themselves to
determine the maximum segment size to be used for each connection. The
<code class="Dv">TCP_MAXSEG</code> option allows the user to determine the
result of this negotiation, and to reduce it if desired.</dd>
<dt id="TCP_MD5SIG"><a class="permalink" href="#TCP_MD5SIG"><code class="Dv">TCP_MD5SIG</code></a></dt>
<dd>This option enables the use of MD5 digests (also known as TCP-MD5) on
writes to the specified socket. In the current release, only outgoing
traffic is digested; digests on incoming traffic are not verified. The
current default behavior for the system is to respond to a system
advertising this option with TCP-MD5; this may change.
<p class="Pp">One common use for this in a <span class="Ux">NetBSD</span>
router deployment is to enable based routers to interwork with Cisco
equipment at peering points. Support for this feature conforms to RFC
2385. Only IPv4 (AF_INET) sessions are supported.</p>
<p class="Pp">In order for this option to function correctly, it is
necessary for the administrator to add a tcp-md5 key entry to the
system's security associations database (SADB) using the
<a class="Xr">setkey(8)</a> utility. This entry must have an SPI of
0x1000 and can therefore only be specified on a per-host basis at this
time.</p>
<p class="Pp" id="tcp_signature_compute:">If an SADB entry cannot be found
for the destination, the outgoing traffic will have an invalid digest
option prepended, and the following error message will be visible on the
system console:
<a class="permalink" href="#tcp_signature_compute:"><i class="Em">tcp_signature_compute:
SADB lookup failed for %d.%d.%d.%d</i></a>.</p>
</dd>
<dt id="TCP_KEEPIDLE"><a class="permalink" href="#TCP_KEEPIDLE"><code class="Dv">TCP_KEEPIDLE</code></a></dt>
<dd>TCP probes a connection that has been idle for some amount of time. The
default value for this idle period is 4 hours. The
<code class="Dv">TCP_KEEPIDLE</code> option can be used to affect this
value for a given socket, and specifies the number of seconds of idle time
between keepalive probes. This option takes an <var class="Vt">unsigned
int</var> value, with a value greater than 0.</dd>
<dt id="TCP_KEEPINTVL"><a class="permalink" href="#TCP_KEEPINTVL"><code class="Dv">TCP_KEEPINTVL</code></a></dt>
<dd>When the <code class="Dv">SO_KEEPALIVE</code> option is enabled, TCP
probes a connection that has been idle for some amount of time. If the
remote system does not respond to a keepalive probe, TCP retransmits the
probe after some amount of time. The default value for this retransmit
interval is 150 seconds. The <code class="Dv">TCP_KEEPINTVL</code> option
can be used to affect this value for a given socket, and specifies the
number of seconds to wait before retransmitting a keepalive probe. This
option takes an <var class="Vt">unsigned int</var> value, with a value
greater than 0.</dd>
<dt id="TCP_KEEPCNT"><a class="permalink" href="#TCP_KEEPCNT"><code class="Dv">TCP_KEEPCNT</code></a></dt>
<dd>When the <code class="Dv">SO_KEEPALIVE</code> option is enabled, TCP
probes a connection that has been idle for some amount of time. If the
remote system does not respond to a keepalive probe, TCP retransmits the
probe a certain number of times before a connection is considered to be
broken. The default value for this keepalive probe retransmit limit is 8.
The <code class="Dv">TCP_KEEPCNT</code> option can be used to affect this
value for a given socket, and specifies the maximum number of keepalive
probes to be sent. This option takes an <var class="Vt">unsigned int</var>
value, with a value greater than 0.</dd>
<dt id="TCP_KEEPINIT"><a class="permalink" href="#TCP_KEEPINIT"><code class="Dv">TCP_KEEPINIT</code></a></dt>
<dd>If a TCP connection cannot be established within some amount of time, TCP
will time out the connect attempt. The default value for this initial
connection establishment timeout is 150 seconds. The
<code class="Dv">TCP_KEEPINIT</code> option can be used to affect this
initial timeout period for a given socket, and specifies the number of
seconds to wait before the connect attempt is timed out. For passive
connections, the <code class="Dv">TCP_KEEPINIT</code> option value is
inherited from the listening socket. This option takes an
<var class="Vt">unsigned int</var> value, with a value greater than
0.</dd>
<dt id="TCP_INFO"><a class="permalink" href="#TCP_INFO"><code class="Dv">TCP_INFO</code></a></dt>
<dd>Information about a socket's underlying TCP session may be retrieved by
passing the read-only option <code class="Dv">TPC_INFO</code> to
<a class="Xr">getsockopt(2)</a>. It accepts a single argument: a pointer
to an instance of <var class="Vt">struct tcp_info</var>.
<p class="Pp">This API is subject to change; consult the source to determine
which fields are currently filled out by this option.
<span class="Ux">NetBSD</span> specific additions include send window
size, receive window size, and bandwidth-controlled window space.</p>
</dd>
</dl>
<p class="Pp">The option level for the <a class="Xr">setsockopt(2)</a> call is
the protocol number for TCP, available from
<a class="Xr">getprotobyname(3)</a>.</p>
<p class="Pp">In the historical <span class="Ux">BSD</span> TCP implementation,
if the <code class="Dv">TCP_NODELAY</code> option was set on a passive
socket, the sockets returned by <a class="Xr">accept(2)</a> erroneously did
not have the <code class="Dv">TCP_NODELAY</code> option set; the behavior
was corrected to inherit <code class="Dv">TCP_NODELAY</code> in
<span class="Ux">NetBSD 1.6</span>.</p>
<p class="Pp">Options at the IP network level may be used with TCP; see
<a class="Xr">ip(4)</a> or <a class="Xr">ip6(4)</a>. Incoming connection
requests that are source-routed are noted, and the reverse source route is
used in responding.</p>
<p class="Pp">There are many adjustable parameters that control various aspects
of the <span class="Ux">NetBSD</span> TCP behavior; these parameters are
documented in <a class="Xr">sysctl(7)</a>, and they include:</p>
<ul class="Bl-bullet Bl-compact">
<li>RFC 1323 extensions for high performance</li>
<li>Send/receive buffer sizes</li>
<li>Default maximum segment size (MSS)</li>
<li>SYN cache parameters</li>
<li>Hughes/Touch/Heidemann Congestion Window Monitoring algorithm</li>
<li>Keepalive parameters</li>
<li>newReno algorithm for congestion control</li>
<li>Logging of connection refusals</li>
<li>RST packet rate limits</li>
<li>SACK (Selective Acknowledgment)</li>
<li>ECN (Explicit Congestion Notification)</li>
<li>Congestion window increase methods; the traditional packet counting or RFC
3465 Appropriate Byte Counting</li>
<li>RFC 3390: Increased initial window size</li>
</ul>
</section>
<section class="Sh">
<h1 class="Sh" id="DIAGNOSTICS"><a class="permalink" href="#DIAGNOSTICS">DIAGNOSTICS</a></h1>
<p class="Pp">A socket operation may fail with one of the following errors
returned:</p>
<dl class="Bl-tag">
<dt>[<code class="Er">EISCONN</code>]</dt>
<dd>when trying to establish a connection on a socket which already has
one;</dd>
<dt>[<code class="Er">ENOBUFS</code>]</dt>
<dd>when the system runs out of memory for an internal data structure;</dd>
<dt>[<code class="Er">ETIMEDOUT</code>]</dt>
<dd>when a connection was dropped due to excessive retransmissions;</dd>
<dt>[<code class="Er">ECONNRESET</code>]</dt>
<dd>when the remote peer forces the connection to be closed;</dd>
<dt>[<code class="Er">ECONNREFUSED</code>]</dt>
<dd>when the remote peer actively refuses connection establishment (usually
because no process is listening to the port);</dd>
<dt>[<code class="Er">EADDRINUSE</code>]</dt>
<dd>when an attempt is made to create a socket with a port which has already
been allocated;</dd>
<dt>[<code class="Er">EADDRNOTAVAIL</code>]</dt>
<dd>when an attempt is made to create a socket with a network address for
which no network interface exists.</dd>
</dl>
</section>
<section class="Sh">
<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
ALSO</a></h1>
<p class="Pp"><a class="Xr">getsockopt(2)</a>, <a class="Xr">socket(2)</a>,
<a class="Xr">inet(4)</a>, <a class="Xr">inet6(4)</a>,
<a class="Xr">intro(4)</a>, <a class="Xr">ip(4)</a>,
<a class="Xr">ip6(4)</a>, <a class="Xr">sysctl(7)</a></p>
<p class="Pp"><cite class="Rs"><span class="RsT">Transmission Control
Protocol</span>, <span class="RsR">RFC</span>, <span class="RsN">793</span>,
<span class="RsD">September 1981</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsT">Requirements for Internet Hosts
-- Communication Layers</span>, <span class="RsR">RFC</span>,
<span class="RsN">1122</span>, <span class="RsD">October
1989</span>.</cite></p>
</section>
<section class="Sh">
<h1 class="Sh" id="HISTORY"><a class="permalink" href="#HISTORY">HISTORY</a></h1>
<p class="Pp">The <code class="Nm">tcp</code> protocol stack appeared in
<span class="Ux">4.2BSD</span>.</p>
</section>
</div>
<table class="foot">
<tr>
<td class="foot-date">February 14, 2015</td>
<td class="foot-os">NetBSD 10.1</td>
</tr>
</table>
|