tcp_output.c revision 1.41 1 1.41 thorpej /* $NetBSD: tcp_output.c,v 1.41 1998/07/17 22:52:01 thorpej Exp $ */
2 1.28 thorpej
3 1.28 thorpej /*-
4 1.28 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.28 thorpej * All rights reserved.
6 1.28 thorpej *
7 1.28 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.28 thorpej * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
9 1.28 thorpej * Facility, NASA Ames Research Center.
10 1.28 thorpej *
11 1.28 thorpej * Redistribution and use in source and binary forms, with or without
12 1.28 thorpej * modification, are permitted provided that the following conditions
13 1.28 thorpej * are met:
14 1.28 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.28 thorpej * notice, this list of conditions and the following disclaimer.
16 1.28 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.28 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.28 thorpej * documentation and/or other materials provided with the distribution.
19 1.28 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.28 thorpej * must display the following acknowledgement:
21 1.28 thorpej * This product includes software developed by the NetBSD
22 1.28 thorpej * Foundation, Inc. and its contributors.
23 1.28 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.28 thorpej * contributors may be used to endorse or promote products derived
25 1.28 thorpej * from this software without specific prior written permission.
26 1.28 thorpej *
27 1.28 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.28 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.28 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.28 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.28 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.28 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.28 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.28 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.28 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.28 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.28 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.28 thorpej */
39 1.10 cgd
40 1.1 cgd /*
41 1.27 thorpej * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42 1.9 mycroft * The Regents of the University of California. All rights reserved.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.1 cgd * 3. All advertising materials mentioning features or use of this software
53 1.1 cgd * must display the following acknowledgement:
54 1.1 cgd * This product includes software developed by the University of
55 1.1 cgd * California, Berkeley and its contributors.
56 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
57 1.1 cgd * may be used to endorse or promote products derived from this software
58 1.1 cgd * without specific prior written permission.
59 1.1 cgd *
60 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 cgd * SUCH DAMAGE.
71 1.1 cgd *
72 1.27 thorpej * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
73 1.1 cgd */
74 1.1 cgd
75 1.4 mycroft #include <sys/param.h>
76 1.4 mycroft #include <sys/systm.h>
77 1.4 mycroft #include <sys/malloc.h>
78 1.4 mycroft #include <sys/mbuf.h>
79 1.4 mycroft #include <sys/protosw.h>
80 1.4 mycroft #include <sys/socket.h>
81 1.4 mycroft #include <sys/socketvar.h>
82 1.4 mycroft #include <sys/errno.h>
83 1.1 cgd
84 1.17 thorpej #include <net/if.h>
85 1.4 mycroft #include <net/route.h>
86 1.1 cgd
87 1.4 mycroft #include <netinet/in.h>
88 1.4 mycroft #include <netinet/in_systm.h>
89 1.4 mycroft #include <netinet/ip.h>
90 1.4 mycroft #include <netinet/in_pcb.h>
91 1.4 mycroft #include <netinet/ip_var.h>
92 1.4 mycroft #include <netinet/tcp.h>
93 1.1 cgd #define TCPOUTFLAGS
94 1.4 mycroft #include <netinet/tcp_fsm.h>
95 1.4 mycroft #include <netinet/tcp_seq.h>
96 1.4 mycroft #include <netinet/tcp_timer.h>
97 1.4 mycroft #include <netinet/tcp_var.h>
98 1.4 mycroft #include <netinet/tcpip.h>
99 1.4 mycroft #include <netinet/tcp_debug.h>
100 1.14 christos
101 1.1 cgd #ifdef notyet
102 1.1 cgd extern struct mbuf *m_copypack();
103 1.1 cgd #endif
104 1.1 cgd
105 1.9 mycroft #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
106 1.1 cgd
107 1.33 thorpej /*
108 1.36 thorpej * Knob to enable Congestion Window Monitoring, and control the
109 1.36 thorpej * the burst size it allows. Default burst is 4 packets, per
110 1.36 thorpej * the Internet draft.
111 1.33 thorpej */
112 1.33 thorpej int tcp_cwm = 0;
113 1.36 thorpej int tcp_cwm_burstsize = 4;
114 1.33 thorpej
115 1.18 thorpej static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
116 1.18 thorpej static __inline void
117 1.18 thorpej tcp_segsize(tp, txsegsizep, rxsegsizep)
118 1.17 thorpej struct tcpcb *tp;
119 1.18 thorpej int *txsegsizep, *rxsegsizep;
120 1.17 thorpej {
121 1.17 thorpej struct inpcb *inp = tp->t_inpcb;
122 1.17 thorpej struct rtentry *rt;
123 1.17 thorpej struct ifnet *ifp;
124 1.17 thorpej int size;
125 1.17 thorpej
126 1.17 thorpej if ((rt = in_pcbrtentry(inp)) == NULL) {
127 1.17 thorpej size = tcp_mssdflt;
128 1.17 thorpej goto out;
129 1.17 thorpej }
130 1.17 thorpej
131 1.17 thorpej ifp = rt->rt_ifp;
132 1.17 thorpej
133 1.19 kml if (rt->rt_rmx.rmx_mtu != 0)
134 1.17 thorpej size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
135 1.20 kml else if (ip_mtudisc || in_localaddr(inp->inp_faddr) ||
136 1.19 kml ifp->if_flags & IFF_LOOPBACK)
137 1.17 thorpej size = ifp->if_mtu - sizeof(struct tcpiphdr);
138 1.17 thorpej else
139 1.17 thorpej size = tcp_mssdflt;
140 1.31 kml size -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb));
141 1.17 thorpej
142 1.17 thorpej out:
143 1.21 kml *txsegsizep = min(tp->t_peermss, size);
144 1.18 thorpej *rxsegsizep = min(tp->t_ourmss, size);
145 1.21 kml
146 1.21 kml if (*txsegsizep != tp->t_segsz) {
147 1.35 kml /*
148 1.35 kml * If the new segment size is larger, we don't want to
149 1.35 kml * mess up the congestion window, but if it is smaller
150 1.35 kml * we'll have to reduce the congestion window to ensure
151 1.35 kml * that we don't get into trouble with initial windows
152 1.35 kml * and the rest. In any case, if the segment size
153 1.35 kml * has changed, chances are the path has, too, and
154 1.35 kml * our congestion window will be different.
155 1.29 kml */
156 1.35 kml if (*txsegsizep < tp->t_segsz) {
157 1.35 kml tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
158 1.35 kml * *txsegsizep, *txsegsizep);
159 1.35 kml tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
160 1.35 kml * *txsegsizep, *txsegsizep);
161 1.35 kml }
162 1.21 kml tp->t_segsz = *txsegsizep;
163 1.21 kml }
164 1.17 thorpej }
165 1.17 thorpej
166 1.1 cgd /*
167 1.1 cgd * Tcp output routine: figure out what should be sent and send it.
168 1.1 cgd */
169 1.6 mycroft int
170 1.1 cgd tcp_output(tp)
171 1.1 cgd register struct tcpcb *tp;
172 1.1 cgd {
173 1.1 cgd register struct socket *so = tp->t_inpcb->inp_socket;
174 1.1 cgd register long len, win;
175 1.1 cgd int off, flags, error;
176 1.1 cgd register struct mbuf *m;
177 1.1 cgd register struct tcpiphdr *ti;
178 1.9 mycroft u_char opt[MAX_TCPOPTLEN];
179 1.1 cgd unsigned optlen, hdrlen;
180 1.18 thorpej int idle, sendalot, txsegsize, rxsegsize;
181 1.17 thorpej
182 1.18 thorpej tcp_segsize(tp, &txsegsize, &rxsegsize);
183 1.1 cgd
184 1.1 cgd idle = (tp->snd_max == tp->snd_una);
185 1.33 thorpej
186 1.41 thorpej /*
187 1.41 thorpej * Restart Window computation. From draft-floyd-incr-init-win-03:
188 1.41 thorpej *
189 1.41 thorpej * Optionally, a TCP MAY set the restart window to the
190 1.41 thorpej * minimum of the value used for the initial window and
191 1.41 thorpej * the current value of cwnd (in other words, using a
192 1.41 thorpej * larger value for the restart window should never increase
193 1.41 thorpej * the size of cwnd).
194 1.41 thorpej */
195 1.33 thorpej if (tcp_cwm) {
196 1.1 cgd /*
197 1.33 thorpej * Hughes/Touch/Heidemann Congestion Window Monitoring.
198 1.33 thorpej * Count the number of packets currently pending
199 1.33 thorpej * acknowledgement, and limit our congestion window
200 1.37 thorpej * to a pre-determined allowed burst size plus that count.
201 1.33 thorpej * This prevents bursting once all pending packets have
202 1.33 thorpej * been acknowledged (i.e. transmission is idle).
203 1.1 cgd */
204 1.33 thorpej tp->snd_cwnd = min(tp->snd_cwnd,
205 1.36 thorpej (tcp_cwm_burstsize * txsegsize) +
206 1.33 thorpej (tp->snd_nxt - tp->snd_una));
207 1.33 thorpej } else {
208 1.33 thorpej if (idle && tp->t_idle >= tp->t_rxtcur) {
209 1.33 thorpej /*
210 1.33 thorpej * We have been idle for "a while" and no acks are
211 1.33 thorpej * expected to clock out any data we send --
212 1.33 thorpej * slow start to get ack "clock" running again.
213 1.33 thorpej */
214 1.41 thorpej tp->snd_cwnd = min(tp->snd_cwnd,
215 1.41 thorpej TCP_INITIAL_WINDOW(tcp_init_win, txsegsize));
216 1.33 thorpej }
217 1.33 thorpej }
218 1.33 thorpej
219 1.1 cgd again:
220 1.33 thorpej /*
221 1.33 thorpej * Determine length of data that should be transmitted, and
222 1.33 thorpej * flags that should be used. If there is some data or critical
223 1.33 thorpej * controls (SYN, RST) to send, then transmit; otherwise,
224 1.33 thorpej * investigate further.
225 1.33 thorpej */
226 1.1 cgd sendalot = 0;
227 1.1 cgd off = tp->snd_nxt - tp->snd_una;
228 1.1 cgd win = min(tp->snd_wnd, tp->snd_cwnd);
229 1.1 cgd
230 1.9 mycroft flags = tcp_outflags[tp->t_state];
231 1.1 cgd /*
232 1.1 cgd * If in persist timeout with window of 0, send 1 byte.
233 1.1 cgd * Otherwise, if window is small but nonzero
234 1.1 cgd * and timer expired, we will send what we can
235 1.1 cgd * and go to transmit state.
236 1.1 cgd */
237 1.1 cgd if (tp->t_force) {
238 1.9 mycroft if (win == 0) {
239 1.9 mycroft /*
240 1.9 mycroft * If we still have some data to send, then
241 1.9 mycroft * clear the FIN bit. Usually this would
242 1.9 mycroft * happen below when it realizes that we
243 1.9 mycroft * aren't sending all the data. However,
244 1.9 mycroft * if we have exactly 1 byte of unset data,
245 1.9 mycroft * then it won't clear the FIN bit below,
246 1.9 mycroft * and if we are in persist state, we wind
247 1.9 mycroft * up sending the packet without recording
248 1.9 mycroft * that we sent the FIN bit.
249 1.9 mycroft *
250 1.9 mycroft * We can't just blindly clear the FIN bit,
251 1.9 mycroft * because if we don't have any more data
252 1.9 mycroft * to send then the probe will be the FIN
253 1.9 mycroft * itself.
254 1.9 mycroft */
255 1.9 mycroft if (off < so->so_snd.sb_cc)
256 1.9 mycroft flags &= ~TH_FIN;
257 1.1 cgd win = 1;
258 1.9 mycroft } else {
259 1.38 thorpej TCP_TIMER_DISARM(tp, TCPT_PERSIST);
260 1.1 cgd tp->t_rxtshift = 0;
261 1.1 cgd }
262 1.1 cgd }
263 1.1 cgd
264 1.11 mycroft if (win < so->so_snd.sb_cc) {
265 1.11 mycroft len = win - off;
266 1.11 mycroft flags &= ~TH_FIN;
267 1.11 mycroft } else
268 1.11 mycroft len = so->so_snd.sb_cc - off;
269 1.1 cgd
270 1.1 cgd if (len < 0) {
271 1.1 cgd /*
272 1.1 cgd * If FIN has been sent but not acked,
273 1.1 cgd * but we haven't been called to retransmit,
274 1.1 cgd * len will be -1. Otherwise, window shrank
275 1.1 cgd * after we sent into it. If window shrank to 0,
276 1.25 thorpej * cancel pending retransmit, pull snd_nxt back
277 1.25 thorpej * to (closed) window, and set the persist timer
278 1.25 thorpej * if it isn't already going. If the window didn't
279 1.25 thorpej * close completely, just wait for an ACK.
280 1.1 cgd */
281 1.1 cgd len = 0;
282 1.1 cgd if (win == 0) {
283 1.38 thorpej TCP_TIMER_DISARM(tp, TCPT_REXMT);
284 1.25 thorpej tp->t_rxtshift = 0;
285 1.1 cgd tp->snd_nxt = tp->snd_una;
286 1.38 thorpej if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
287 1.25 thorpej tcp_setpersist(tp);
288 1.1 cgd }
289 1.1 cgd }
290 1.18 thorpej if (len > txsegsize) {
291 1.18 thorpej len = txsegsize;
292 1.11 mycroft flags &= ~TH_FIN;
293 1.1 cgd sendalot = 1;
294 1.1 cgd }
295 1.1 cgd
296 1.1 cgd win = sbspace(&so->so_rcv);
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Sender silly window avoidance. If connection is idle
300 1.1 cgd * and can send all data, a maximum segment,
301 1.1 cgd * at least a maximum default-size segment do it,
302 1.1 cgd * or are forced, do it; otherwise don't bother.
303 1.1 cgd * If peer's buffer is tiny, then send
304 1.1 cgd * when window is at least half open.
305 1.1 cgd * If retransmitting (possibly after persist timer forced us
306 1.1 cgd * to send into a small window), then must resend.
307 1.1 cgd */
308 1.1 cgd if (len) {
309 1.18 thorpej if (len == txsegsize)
310 1.1 cgd goto send;
311 1.1 cgd if ((idle || tp->t_flags & TF_NODELAY) &&
312 1.1 cgd len + off >= so->so_snd.sb_cc)
313 1.1 cgd goto send;
314 1.1 cgd if (tp->t_force)
315 1.1 cgd goto send;
316 1.1 cgd if (len >= tp->max_sndwnd / 2)
317 1.1 cgd goto send;
318 1.1 cgd if (SEQ_LT(tp->snd_nxt, tp->snd_max))
319 1.1 cgd goto send;
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd /*
323 1.18 thorpej * Compare available window to amount of window known to peer
324 1.18 thorpej * (as advertised window less next expected input). If the
325 1.18 thorpej * difference is at least twice the size of the largest segment
326 1.18 thorpej * we expect to receive (i.e. two segments) or at least 50% of
327 1.18 thorpej * the maximum possible window, then want to send a window update
328 1.18 thorpej * to peer.
329 1.1 cgd */
330 1.1 cgd if (win > 0) {
331 1.9 mycroft /*
332 1.9 mycroft * "adv" is the amount we can increase the window,
333 1.9 mycroft * taking into account that we are limited by
334 1.9 mycroft * TCP_MAXWIN << tp->rcv_scale.
335 1.9 mycroft */
336 1.9 mycroft long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
337 1.9 mycroft (tp->rcv_adv - tp->rcv_nxt);
338 1.1 cgd
339 1.18 thorpej if (adv >= (long) (2 * rxsegsize))
340 1.1 cgd goto send;
341 1.1 cgd if (2 * adv >= (long) so->so_rcv.sb_hiwat)
342 1.1 cgd goto send;
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd /*
346 1.1 cgd * Send if we owe peer an ACK.
347 1.1 cgd */
348 1.1 cgd if (tp->t_flags & TF_ACKNOW)
349 1.1 cgd goto send;
350 1.1 cgd if (flags & (TH_SYN|TH_RST))
351 1.1 cgd goto send;
352 1.1 cgd if (SEQ_GT(tp->snd_up, tp->snd_una))
353 1.1 cgd goto send;
354 1.1 cgd /*
355 1.1 cgd * If our state indicates that FIN should be sent
356 1.1 cgd * and we have not yet done so, or we're retransmitting the FIN,
357 1.1 cgd * then we need to send.
358 1.1 cgd */
359 1.1 cgd if (flags & TH_FIN &&
360 1.1 cgd ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
361 1.1 cgd goto send;
362 1.1 cgd
363 1.1 cgd /*
364 1.1 cgd * TCP window updates are not reliable, rather a polling protocol
365 1.1 cgd * using ``persist'' packets is used to insure receipt of window
366 1.1 cgd * updates. The three ``states'' for the output side are:
367 1.1 cgd * idle not doing retransmits or persists
368 1.1 cgd * persisting to move a small or zero window
369 1.1 cgd * (re)transmitting and thereby not persisting
370 1.1 cgd *
371 1.1 cgd * tp->t_timer[TCPT_PERSIST]
372 1.1 cgd * is set when we are in persist state.
373 1.1 cgd * tp->t_force
374 1.1 cgd * is set when we are called to send a persist packet.
375 1.1 cgd * tp->t_timer[TCPT_REXMT]
376 1.1 cgd * is set when we are retransmitting
377 1.1 cgd * The output side is idle when both timers are zero.
378 1.1 cgd *
379 1.1 cgd * If send window is too small, there is data to transmit, and no
380 1.1 cgd * retransmit or persist is pending, then go to persist state.
381 1.1 cgd * If nothing happens soon, send when timer expires:
382 1.1 cgd * if window is nonzero, transmit what we can,
383 1.1 cgd * otherwise force out a byte.
384 1.1 cgd */
385 1.38 thorpej if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
386 1.38 thorpej TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
387 1.1 cgd tp->t_rxtshift = 0;
388 1.1 cgd tcp_setpersist(tp);
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd /*
392 1.1 cgd * No reason to send a segment, just return.
393 1.1 cgd */
394 1.1 cgd return (0);
395 1.1 cgd
396 1.1 cgd send:
397 1.1 cgd /*
398 1.1 cgd * Before ESTABLISHED, force sending of initial options
399 1.1 cgd * unless TCP set not to do any options.
400 1.1 cgd * NOTE: we assume that the IP/TCP header plus TCP options
401 1.1 cgd * always fit in a single mbuf, leaving room for a maximum
402 1.1 cgd * link header, i.e.
403 1.1 cgd * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
404 1.1 cgd */
405 1.1 cgd optlen = 0;
406 1.1 cgd hdrlen = sizeof (struct tcpiphdr);
407 1.9 mycroft if (flags & TH_SYN) {
408 1.34 kml struct rtentry *rt = in_pcbrtentry(tp->t_inpcb);
409 1.34 kml
410 1.9 mycroft tp->snd_nxt = tp->iss;
411 1.34 kml tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
412 1.34 kml rt->rt_ifp : NULL);
413 1.9 mycroft if ((tp->t_flags & TF_NOOPT) == 0) {
414 1.9 mycroft opt[0] = TCPOPT_MAXSEG;
415 1.9 mycroft opt[1] = 4;
416 1.17 thorpej opt[2] = (tp->t_ourmss >> 8) & 0xff;
417 1.17 thorpej opt[3] = tp->t_ourmss & 0xff;
418 1.9 mycroft optlen = 4;
419 1.9 mycroft
420 1.9 mycroft if ((tp->t_flags & TF_REQ_SCALE) &&
421 1.9 mycroft ((flags & TH_ACK) == 0 ||
422 1.9 mycroft (tp->t_flags & TF_RCVD_SCALE))) {
423 1.12 cgd *((u_int32_t *) (opt + optlen)) = htonl(
424 1.9 mycroft TCPOPT_NOP << 24 |
425 1.9 mycroft TCPOPT_WINDOW << 16 |
426 1.9 mycroft TCPOLEN_WINDOW << 8 |
427 1.9 mycroft tp->request_r_scale);
428 1.9 mycroft optlen += 4;
429 1.9 mycroft }
430 1.9 mycroft }
431 1.9 mycroft }
432 1.9 mycroft
433 1.9 mycroft /*
434 1.9 mycroft * Send a timestamp and echo-reply if this is a SYN and our side
435 1.9 mycroft * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
436 1.9 mycroft * and our peer have sent timestamps in our SYN's.
437 1.9 mycroft */
438 1.9 mycroft if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
439 1.9 mycroft (flags & TH_RST) == 0 &&
440 1.9 mycroft ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
441 1.9 mycroft (tp->t_flags & TF_RCVD_TSTMP))) {
442 1.13 cgd u_int32_t *lp = (u_int32_t *)(opt + optlen);
443 1.9 mycroft
444 1.9 mycroft /* Form timestamp option as shown in appendix A of RFC 1323. */
445 1.9 mycroft *lp++ = htonl(TCPOPT_TSTAMP_HDR);
446 1.9 mycroft *lp++ = htonl(tcp_now);
447 1.9 mycroft *lp = htonl(tp->ts_recent);
448 1.9 mycroft optlen += TCPOLEN_TSTAMP_APPA;
449 1.9 mycroft }
450 1.9 mycroft
451 1.9 mycroft hdrlen += optlen;
452 1.9 mycroft
453 1.1 cgd #ifdef DIAGNOSTIC
454 1.29 kml if (len > txsegsize)
455 1.29 kml panic("tcp data to be sent is larger than segment");
456 1.9 mycroft if (max_linkhdr + hdrlen > MHLEN)
457 1.9 mycroft panic("tcphdr too big");
458 1.1 cgd #endif
459 1.1 cgd
460 1.1 cgd /*
461 1.1 cgd * Grab a header mbuf, attaching a copy of data to
462 1.1 cgd * be transmitted, and initialize the header from
463 1.1 cgd * the template for sends on this connection.
464 1.1 cgd */
465 1.1 cgd if (len) {
466 1.1 cgd if (tp->t_force && len == 1)
467 1.1 cgd tcpstat.tcps_sndprobe++;
468 1.1 cgd else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
469 1.1 cgd tcpstat.tcps_sndrexmitpack++;
470 1.1 cgd tcpstat.tcps_sndrexmitbyte += len;
471 1.1 cgd } else {
472 1.1 cgd tcpstat.tcps_sndpack++;
473 1.1 cgd tcpstat.tcps_sndbyte += len;
474 1.1 cgd }
475 1.1 cgd #ifdef notyet
476 1.1 cgd if ((m = m_copypack(so->so_snd.sb_mb, off,
477 1.1 cgd (int)len, max_linkhdr + hdrlen)) == 0) {
478 1.1 cgd error = ENOBUFS;
479 1.1 cgd goto out;
480 1.1 cgd }
481 1.1 cgd /*
482 1.1 cgd * m_copypack left space for our hdr; use it.
483 1.1 cgd */
484 1.1 cgd m->m_len += hdrlen;
485 1.1 cgd m->m_data -= hdrlen;
486 1.1 cgd #else
487 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_HEADER);
488 1.1 cgd if (m == NULL) {
489 1.1 cgd error = ENOBUFS;
490 1.1 cgd goto out;
491 1.1 cgd }
492 1.1 cgd m->m_data += max_linkhdr;
493 1.1 cgd m->m_len = hdrlen;
494 1.1 cgd if (len <= MHLEN - hdrlen - max_linkhdr) {
495 1.1 cgd m_copydata(so->so_snd.sb_mb, off, (int) len,
496 1.1 cgd mtod(m, caddr_t) + hdrlen);
497 1.1 cgd m->m_len += len;
498 1.1 cgd } else {
499 1.1 cgd m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
500 1.25 thorpej if (m->m_next == 0) {
501 1.40 sommerfe m_freem(m);
502 1.25 thorpej error = ENOBUFS;
503 1.25 thorpej goto out;
504 1.25 thorpej }
505 1.1 cgd }
506 1.1 cgd #endif
507 1.1 cgd /*
508 1.1 cgd * If we're sending everything we've got, set PUSH.
509 1.1 cgd * (This will keep happy those implementations which only
510 1.1 cgd * give data to the user when a buffer fills or
511 1.1 cgd * a PUSH comes in.)
512 1.1 cgd */
513 1.1 cgd if (off + len == so->so_snd.sb_cc)
514 1.1 cgd flags |= TH_PUSH;
515 1.1 cgd } else {
516 1.1 cgd if (tp->t_flags & TF_ACKNOW)
517 1.1 cgd tcpstat.tcps_sndacks++;
518 1.1 cgd else if (flags & (TH_SYN|TH_FIN|TH_RST))
519 1.1 cgd tcpstat.tcps_sndctrl++;
520 1.1 cgd else if (SEQ_GT(tp->snd_up, tp->snd_una))
521 1.1 cgd tcpstat.tcps_sndurg++;
522 1.1 cgd else
523 1.1 cgd tcpstat.tcps_sndwinup++;
524 1.1 cgd
525 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_HEADER);
526 1.1 cgd if (m == NULL) {
527 1.1 cgd error = ENOBUFS;
528 1.1 cgd goto out;
529 1.1 cgd }
530 1.1 cgd m->m_data += max_linkhdr;
531 1.1 cgd m->m_len = hdrlen;
532 1.1 cgd }
533 1.1 cgd m->m_pkthdr.rcvif = (struct ifnet *)0;
534 1.1 cgd ti = mtod(m, struct tcpiphdr *);
535 1.1 cgd if (tp->t_template == 0)
536 1.1 cgd panic("tcp_output");
537 1.1 cgd bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
538 1.1 cgd
539 1.1 cgd /*
540 1.1 cgd * Fill in fields, remembering maximum advertised
541 1.1 cgd * window for use in delaying messages about window sizes.
542 1.1 cgd * If resending a FIN, be sure not to use a new sequence number.
543 1.1 cgd */
544 1.9 mycroft if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
545 1.1 cgd tp->snd_nxt == tp->snd_max)
546 1.1 cgd tp->snd_nxt--;
547 1.9 mycroft /*
548 1.9 mycroft * If we are doing retransmissions, then snd_nxt will
549 1.9 mycroft * not reflect the first unsent octet. For ACK only
550 1.9 mycroft * packets, we do not want the sequence number of the
551 1.9 mycroft * retransmitted packet, we want the sequence number
552 1.9 mycroft * of the next unsent octet. So, if there is no data
553 1.9 mycroft * (and no SYN or FIN), use snd_max instead of snd_nxt
554 1.9 mycroft * when filling in ti_seq. But if we are in persist
555 1.9 mycroft * state, snd_max might reflect one byte beyond the
556 1.9 mycroft * right edge of the window, so use snd_nxt in that
557 1.9 mycroft * case, since we know we aren't doing a retransmission.
558 1.9 mycroft * (retransmit and persist are mutually exclusive...)
559 1.9 mycroft */
560 1.38 thorpej if (len || (flags & (TH_SYN|TH_FIN)) ||
561 1.38 thorpej TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
562 1.8 mycroft ti->ti_seq = htonl(tp->snd_nxt);
563 1.8 mycroft else
564 1.8 mycroft ti->ti_seq = htonl(tp->snd_max);
565 1.1 cgd ti->ti_ack = htonl(tp->rcv_nxt);
566 1.1 cgd if (optlen) {
567 1.1 cgd bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
568 1.1 cgd ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
569 1.1 cgd }
570 1.1 cgd ti->ti_flags = flags;
571 1.1 cgd /*
572 1.1 cgd * Calculate receive window. Don't shrink window,
573 1.1 cgd * but avoid silly window syndrome.
574 1.1 cgd */
575 1.18 thorpej if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
576 1.1 cgd win = 0;
577 1.9 mycroft if (win > (long)TCP_MAXWIN << tp->rcv_scale)
578 1.9 mycroft win = (long)TCP_MAXWIN << tp->rcv_scale;
579 1.1 cgd if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
580 1.1 cgd win = (long)(tp->rcv_adv - tp->rcv_nxt);
581 1.12 cgd ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
582 1.1 cgd if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
583 1.16 kml u_int32_t urp = tp->snd_up - tp->snd_nxt;
584 1.16 kml if (urp > IP_MAXPACKET)
585 1.16 kml urp = IP_MAXPACKET;
586 1.16 kml ti->ti_urp = htons((u_int16_t)urp);
587 1.1 cgd ti->ti_flags |= TH_URG;
588 1.1 cgd } else
589 1.1 cgd /*
590 1.1 cgd * If no urgent pointer to send, then we pull
591 1.1 cgd * the urgent pointer to the left edge of the send window
592 1.1 cgd * so that it doesn't drift into the send window on sequence
593 1.1 cgd * number wraparound.
594 1.1 cgd */
595 1.1 cgd tp->snd_up = tp->snd_una; /* drag it along */
596 1.1 cgd
597 1.1 cgd /*
598 1.1 cgd * Put TCP length in extended header, and then
599 1.1 cgd * checksum extended header and data.
600 1.1 cgd */
601 1.1 cgd if (len + optlen)
602 1.12 cgd ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
603 1.1 cgd optlen + len));
604 1.1 cgd ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
605 1.1 cgd
606 1.1 cgd /*
607 1.1 cgd * In transmit state, time the transmission and arrange for
608 1.1 cgd * the retransmit. In persist state, just set snd_max.
609 1.1 cgd */
610 1.38 thorpej if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
611 1.1 cgd tcp_seq startseq = tp->snd_nxt;
612 1.1 cgd
613 1.1 cgd /*
614 1.1 cgd * Advance snd_nxt over sequence space of this segment.
615 1.1 cgd */
616 1.1 cgd if (flags & (TH_SYN|TH_FIN)) {
617 1.1 cgd if (flags & TH_SYN)
618 1.1 cgd tp->snd_nxt++;
619 1.1 cgd if (flags & TH_FIN) {
620 1.1 cgd tp->snd_nxt++;
621 1.1 cgd tp->t_flags |= TF_SENTFIN;
622 1.1 cgd }
623 1.1 cgd }
624 1.1 cgd tp->snd_nxt += len;
625 1.1 cgd if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
626 1.1 cgd tp->snd_max = tp->snd_nxt;
627 1.1 cgd /*
628 1.1 cgd * Time this transmission if not a retransmission and
629 1.1 cgd * not currently timing anything.
630 1.1 cgd */
631 1.1 cgd if (tp->t_rtt == 0) {
632 1.1 cgd tp->t_rtt = 1;
633 1.1 cgd tp->t_rtseq = startseq;
634 1.1 cgd tcpstat.tcps_segstimed++;
635 1.1 cgd }
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd /*
639 1.1 cgd * Set retransmit timer if not currently set,
640 1.1 cgd * and not doing an ack or a keep-alive probe.
641 1.1 cgd * Initial value for retransmit timer is smoothed
642 1.1 cgd * round-trip time + 2 * round-trip time variance.
643 1.1 cgd * Initialize shift counter which is used for backoff
644 1.1 cgd * of retransmit time.
645 1.1 cgd */
646 1.38 thorpej if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
647 1.1 cgd tp->snd_nxt != tp->snd_una) {
648 1.38 thorpej TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
649 1.38 thorpej if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
650 1.38 thorpej TCP_TIMER_DISARM(tp, TCPT_PERSIST);
651 1.1 cgd tp->t_rxtshift = 0;
652 1.1 cgd }
653 1.1 cgd }
654 1.1 cgd } else
655 1.1 cgd if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
656 1.1 cgd tp->snd_max = tp->snd_nxt + len;
657 1.1 cgd
658 1.1 cgd /*
659 1.1 cgd * Trace.
660 1.1 cgd */
661 1.1 cgd if (so->so_options & SO_DEBUG)
662 1.1 cgd tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
663 1.1 cgd
664 1.1 cgd /*
665 1.1 cgd * Fill in IP length and desired time to live and
666 1.1 cgd * send to IP level. There should be a better way
667 1.1 cgd * to handle ttl and tos; we could keep them in
668 1.1 cgd * the template, but need a way to checksum without them.
669 1.1 cgd */
670 1.1 cgd m->m_pkthdr.len = hdrlen + len;
671 1.9 mycroft {
672 1.19 kml struct rtentry *rt;
673 1.19 kml
674 1.1 cgd ((struct ip *)ti)->ip_len = m->m_pkthdr.len;
675 1.1 cgd ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */
676 1.1 cgd ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */
677 1.19 kml
678 1.20 kml if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 &&
679 1.19 kml (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
680 1.19 kml ((struct ip *)ti)->ip_off |= IP_DF;
681 1.19 kml
682 1.1 cgd #if BSD >= 43
683 1.1 cgd error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
684 1.9 mycroft so->so_options & SO_DONTROUTE, 0);
685 1.1 cgd #else
686 1.9 mycroft error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
687 1.1 cgd so->so_options & SO_DONTROUTE);
688 1.1 cgd #endif
689 1.9 mycroft }
690 1.1 cgd if (error) {
691 1.1 cgd out:
692 1.1 cgd if (error == ENOBUFS) {
693 1.6 mycroft tcp_quench(tp->t_inpcb, 0);
694 1.1 cgd return (0);
695 1.1 cgd }
696 1.1 cgd if ((error == EHOSTUNREACH || error == ENETDOWN)
697 1.1 cgd && TCPS_HAVERCVDSYN(tp->t_state)) {
698 1.1 cgd tp->t_softerror = error;
699 1.1 cgd return (0);
700 1.1 cgd }
701 1.1 cgd return (error);
702 1.1 cgd }
703 1.1 cgd tcpstat.tcps_sndtotal++;
704 1.23 thorpej if (tp->t_flags & TF_DELACK)
705 1.23 thorpej tcpstat.tcps_delack++;
706 1.1 cgd
707 1.1 cgd /*
708 1.1 cgd * Data sent (as far as we can tell).
709 1.1 cgd * If this advertises a larger window than any other segment,
710 1.1 cgd * then remember the size of the advertised window.
711 1.1 cgd * Any pending ACK has now been sent.
712 1.1 cgd */
713 1.1 cgd if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
714 1.1 cgd tp->rcv_adv = tp->rcv_nxt + win;
715 1.9 mycroft tp->last_ack_sent = tp->rcv_nxt;
716 1.26 thorpej tp->t_flags &= ~TF_ACKNOW;
717 1.26 thorpej TCP_CLEAR_DELACK(tp);
718 1.1 cgd if (sendalot)
719 1.1 cgd goto again;
720 1.1 cgd return (0);
721 1.1 cgd }
722 1.1 cgd
723 1.6 mycroft void
724 1.1 cgd tcp_setpersist(tp)
725 1.1 cgd register struct tcpcb *tp;
726 1.1 cgd {
727 1.22 thorpej register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
728 1.38 thorpej int nticks;
729 1.1 cgd
730 1.38 thorpej if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
731 1.1 cgd panic("tcp_output REXMT");
732 1.1 cgd /*
733 1.1 cgd * Start/restart persistance timer.
734 1.1 cgd */
735 1.30 kml if (t < tp->t_rttmin)
736 1.30 kml t = tp->t_rttmin;
737 1.38 thorpej TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
738 1.1 cgd TCPTV_PERSMIN, TCPTV_PERSMAX);
739 1.38 thorpej TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
740 1.1 cgd if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
741 1.1 cgd tp->t_rxtshift++;
742 1.1 cgd }
743