xref: /original-bsd/sys/netinet/tcp_output.c (revision e59fb703)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tcp_output.c	7.22 (Berkeley) 08/31/90
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "malloc.h"
13 #include "mbuf.h"
14 #include "protosw.h"
15 #include "socket.h"
16 #include "socketvar.h"
17 #include "errno.h"
18 
19 #include "../net/route.h"
20 
21 #include "in.h"
22 #include "in_systm.h"
23 #include "ip.h"
24 #include "in_pcb.h"
25 #include "ip_var.h"
26 #include "tcp.h"
27 #define	TCPOUTFLAGS
28 #include "tcp_fsm.h"
29 #include "tcp_seq.h"
30 #include "tcp_timer.h"
31 #include "tcp_var.h"
32 #include "tcpip.h"
33 #include "tcp_debug.h"
34 
35 #ifdef notyet
36 extern struct mbuf *m_copypack();
37 #endif
38 
39 /*
40  * Initial options.
41  */
42 u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
43 
44 /*
45  * Tcp output routine: figure out what should be sent and send it.
46  */
47 tcp_output(tp)
48 	register struct tcpcb *tp;
49 {
50 	register struct socket *so = tp->t_inpcb->inp_socket;
51 	register long len, win;
52 	int off, flags, error;
53 	register struct mbuf *m;
54 	register struct tcpiphdr *ti;
55 	u_char *opt;
56 	unsigned optlen, hdrlen;
57 	int idle, sendalot;
58 
59 	/*
60 	 * Determine length of data that should be transmitted,
61 	 * and flags that will be used.
62 	 * If there is some data or critical controls (SYN, RST)
63 	 * to send, then transmit; otherwise, investigate further.
64 	 */
65 	idle = (tp->snd_max == tp->snd_una);
66 	if (idle && tp->t_idle >= tp->t_rxtcur)
67 		/*
68 		 * We have been idle for "a while" and no acks are
69 		 * expected to clock out any data we send --
70 		 * slow start to get ack "clock" running again.
71 		 */
72 		tp->snd_cwnd = tp->t_maxseg;
73 again:
74 	sendalot = 0;
75 	off = tp->snd_nxt - tp->snd_una;
76 	win = min(tp->snd_wnd, tp->snd_cwnd);
77 
78 	/*
79 	 * If in persist timeout with window of 0, send 1 byte.
80 	 * Otherwise, if window is small but nonzero
81 	 * and timer expired, we will send what we can
82 	 * and go to transmit state.
83 	 */
84 	if (tp->t_force) {
85 		if (win == 0)
86 			win = 1;
87 		else {
88 			tp->t_timer[TCPT_PERSIST] = 0;
89 			tp->t_rxtshift = 0;
90 		}
91 	}
92 
93 	flags = tcp_outflags[tp->t_state];
94 	len = min(so->so_snd.sb_cc, win) - off;
95 
96 	if (len < 0) {
97 		/*
98 		 * If FIN has been sent but not acked,
99 		 * but we haven't been called to retransmit,
100 		 * len will be -1.  Otherwise, window shrank
101 		 * after we sent into it.  If window shrank to 0,
102 		 * cancel pending retransmit and pull snd_nxt
103 		 * back to (closed) window.  We will enter persist
104 		 * state below.  If the window didn't close completely,
105 		 * just wait for an ACK.
106 		 */
107 		len = 0;
108 		if (win == 0) {
109 			tp->t_timer[TCPT_REXMT] = 0;
110 			tp->snd_nxt = tp->snd_una;
111 		}
112 	}
113 	if (len > tp->t_maxseg) {
114 		len = tp->t_maxseg;
115 		sendalot = 1;
116 	}
117 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
118 		flags &= ~TH_FIN;
119 
120 	win = sbspace(&so->so_rcv);
121 
122 	/*
123 	 * Sender silly window avoidance.  If connection is idle
124 	 * and can send all data, a maximum segment,
125 	 * at least a maximum default-size segment do it,
126 	 * or are forced, do it; otherwise don't bother.
127 	 * If peer's buffer is tiny, then send
128 	 * when window is at least half open.
129 	 * If retransmitting (possibly after persist timer forced us
130 	 * to send into a small window), then must resend.
131 	 */
132 	if (len) {
133 		if (len == tp->t_maxseg)
134 			goto send;
135 		if ((idle || tp->t_flags & TF_NODELAY) &&
136 		    len + off >= so->so_snd.sb_cc)
137 			goto send;
138 		if (tp->t_force)
139 			goto send;
140 		if (len >= tp->max_sndwnd / 2)
141 			goto send;
142 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
143 			goto send;
144 	}
145 
146 	/*
147 	 * Compare available window to amount of window
148 	 * known to peer (as advertised window less
149 	 * next expected input).  If the difference is at least two
150 	 * max size segments, or at least 50% of the maximum possible
151 	 * window, then want to send a window update to peer.
152 	 */
153 	if (win > 0) {
154 		long adv = win - (tp->rcv_adv - tp->rcv_nxt);
155 
156 		if (adv >= (long) (2 * tp->t_maxseg))
157 			goto send;
158 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
159 			goto send;
160 	}
161 
162 	/*
163 	 * Send if we owe peer an ACK.
164 	 */
165 	if (tp->t_flags & TF_ACKNOW)
166 		goto send;
167 	if (flags & (TH_SYN|TH_RST))
168 		goto send;
169 	if (SEQ_GT(tp->snd_up, tp->snd_una))
170 		goto send;
171 	/*
172 	 * If our state indicates that FIN should be sent
173 	 * and we have not yet done so, or we're retransmitting the FIN,
174 	 * then we need to send.
175 	 */
176 	if (flags & TH_FIN &&
177 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
178 		goto send;
179 
180 	/*
181 	 * TCP window updates are not reliable, rather a polling protocol
182 	 * using ``persist'' packets is used to insure receipt of window
183 	 * updates.  The three ``states'' for the output side are:
184 	 *	idle			not doing retransmits or persists
185 	 *	persisting		to move a small or zero window
186 	 *	(re)transmitting	and thereby not persisting
187 	 *
188 	 * tp->t_timer[TCPT_PERSIST]
189 	 *	is set when we are in persist state.
190 	 * tp->t_force
191 	 *	is set when we are called to send a persist packet.
192 	 * tp->t_timer[TCPT_REXMT]
193 	 *	is set when we are retransmitting
194 	 * The output side is idle when both timers are zero.
195 	 *
196 	 * If send window is too small, there is data to transmit, and no
197 	 * retransmit or persist is pending, then go to persist state.
198 	 * If nothing happens soon, send when timer expires:
199 	 * if window is nonzero, transmit what we can,
200 	 * otherwise force out a byte.
201 	 */
202 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
203 	    tp->t_timer[TCPT_PERSIST] == 0) {
204 		tp->t_rxtshift = 0;
205 		tcp_setpersist(tp);
206 	}
207 
208 	/*
209 	 * No reason to send a segment, just return.
210 	 */
211 	return (0);
212 
213 send:
214 	/*
215 	 * Before ESTABLISHED, force sending of initial options
216 	 * unless TCP set not to do any options.
217 	 * NOTE: we assume that the IP/TCP header plus TCP options
218 	 * always fit in a single mbuf, leaving room for a maximum
219 	 * link header, i.e.
220 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
221 	 */
222 	optlen = 0;
223 	hdrlen = sizeof (struct tcpiphdr);
224 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
225 		opt = tcp_initopt;
226 		optlen = sizeof (tcp_initopt);
227 		hdrlen += sizeof (tcp_initopt);
228 		*(u_short *)(opt + 2) = htons((u_short) tcp_mss(tp, 0));
229 #ifdef DIAGNOSTIC
230 	 	if (max_linkhdr + hdrlen > MHLEN)
231 			panic("tcphdr too big");
232 #endif
233 	}
234 
235 	/*
236 	 * Grab a header mbuf, attaching a copy of data to
237 	 * be transmitted, and initialize the header from
238 	 * the template for sends on this connection.
239 	 */
240 	if (len) {
241 		if (tp->t_force && len == 1)
242 			tcpstat.tcps_sndprobe++;
243 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
244 			tcpstat.tcps_sndrexmitpack++;
245 			tcpstat.tcps_sndrexmitbyte += len;
246 		} else {
247 			tcpstat.tcps_sndpack++;
248 			tcpstat.tcps_sndbyte += len;
249 		}
250 #ifdef notyet
251 		if ((m = m_copypack(so->so_snd.sb_mb, off,
252 		    (int)len, max_linkhdr + hdrlen)) == 0) {
253 			error = ENOBUFS;
254 			goto out;
255 		}
256 		/*
257 		 * m_copypack left space for our hdr; use it.
258 		 */
259 		m->m_len += hdrlen;
260 		m->m_data -= hdrlen;
261 #else
262 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
263 		if (m == NULL) {
264 			error = ENOBUFS;
265 			goto out;
266 		}
267 		m->m_data += max_linkhdr;
268 		m->m_len = hdrlen;
269 		if (len <= MHLEN - hdrlen - max_linkhdr) {
270 			m_copydata(so->so_snd.sb_mb, off, (int) len,
271 			    mtod(m, caddr_t) + hdrlen);
272 			m->m_len += len;
273 		} else {
274 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
275 			if (m->m_next == 0)
276 				len = 0;
277 		}
278 #endif
279 		/*
280 		 * If we're sending everything we've got, set PUSH.
281 		 * (This will keep happy those implementations which only
282 		 * give data to the user when a buffer fills or
283 		 * a PUSH comes in.)
284 		 */
285 		if (off + len == so->so_snd.sb_cc)
286 			flags |= TH_PUSH;
287 	} else {
288 		if (tp->t_flags & TF_ACKNOW)
289 			tcpstat.tcps_sndacks++;
290 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
291 			tcpstat.tcps_sndctrl++;
292 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
293 			tcpstat.tcps_sndurg++;
294 		else
295 			tcpstat.tcps_sndwinup++;
296 
297 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
298 		if (m == NULL) {
299 			error = ENOBUFS;
300 			goto out;
301 		}
302 		m->m_data += max_linkhdr;
303 		m->m_len = hdrlen;
304 	}
305 	m->m_pkthdr.rcvif = (struct ifnet *)0;
306 	ti = mtod(m, struct tcpiphdr *);
307 	if (tp->t_template == 0)
308 		panic("tcp_output");
309 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
310 
311 	/*
312 	 * Fill in fields, remembering maximum advertised
313 	 * window for use in delaying messages about window sizes.
314 	 * If resending a FIN, be sure not to use a new sequence number.
315 	 */
316 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
317 	    tp->snd_nxt == tp->snd_max)
318 		tp->snd_nxt--;
319 	ti->ti_seq = htonl(tp->snd_nxt);
320 	ti->ti_ack = htonl(tp->rcv_nxt);
321 	if (optlen) {
322 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
323 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
324 	}
325 	ti->ti_flags = flags;
326 	/*
327 	 * Calculate receive window.  Don't shrink window,
328 	 * but avoid silly window syndrome.
329 	 */
330 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
331 		win = 0;
332 	if (win > TCP_MAXWIN)
333 		win = TCP_MAXWIN;
334 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
335 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
336 	ti->ti_win = htons((u_short)win);
337 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
338 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
339 		ti->ti_flags |= TH_URG;
340 	} else
341 		/*
342 		 * If no urgent pointer to send, then we pull
343 		 * the urgent pointer to the left edge of the send window
344 		 * so that it doesn't drift into the send window on sequence
345 		 * number wraparound.
346 		 */
347 		tp->snd_up = tp->snd_una;		/* drag it along */
348 
349 	/*
350 	 * Put TCP length in extended header, and then
351 	 * checksum extended header and data.
352 	 */
353 	if (len + optlen)
354 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
355 		    optlen + len));
356 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
357 
358 	/*
359 	 * In transmit state, time the transmission and arrange for
360 	 * the retransmit.  In persist state, just set snd_max.
361 	 */
362 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
363 		tcp_seq startseq = tp->snd_nxt;
364 
365 		/*
366 		 * Advance snd_nxt over sequence space of this segment.
367 		 */
368 		if (flags & (TH_SYN|TH_FIN)) {
369 			if (flags & TH_SYN)
370 				tp->snd_nxt++;
371 			if (flags & TH_FIN) {
372 				tp->snd_nxt++;
373 				tp->t_flags |= TF_SENTFIN;
374 			}
375 		}
376 		tp->snd_nxt += len;
377 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
378 			tp->snd_max = tp->snd_nxt;
379 			/*
380 			 * Time this transmission if not a retransmission and
381 			 * not currently timing anything.
382 			 */
383 			if (tp->t_rtt == 0) {
384 				tp->t_rtt = 1;
385 				tp->t_rtseq = startseq;
386 				tcpstat.tcps_segstimed++;
387 			}
388 		}
389 
390 		/*
391 		 * Set retransmit timer if not currently set,
392 		 * and not doing an ack or a keep-alive probe.
393 		 * Initial value for retransmit timer is smoothed
394 		 * round-trip time + 2 * round-trip time variance.
395 		 * Initialize shift counter which is used for backoff
396 		 * of retransmit time.
397 		 */
398 		if (tp->t_timer[TCPT_REXMT] == 0 &&
399 		    tp->snd_nxt != tp->snd_una) {
400 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
401 			if (tp->t_timer[TCPT_PERSIST]) {
402 				tp->t_timer[TCPT_PERSIST] = 0;
403 				tp->t_rxtshift = 0;
404 			}
405 		}
406 	} else
407 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
408 			tp->snd_max = tp->snd_nxt + len;
409 
410 	/*
411 	 * Trace.
412 	 */
413 	if (so->so_options & SO_DEBUG)
414 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
415 
416 	/*
417 	 * Fill in IP length and desired time to live and
418 	 * send to IP level.  There should be a better way
419 	 * to handle ttl and tos; we could keep them in
420 	 * the template, but need a way to checksum without them.
421 	 */
422 	m->m_pkthdr.len = hdrlen + len;
423 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
424 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
425 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
426 #if BSD >= 43
427 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
428 	    so->so_options & SO_DONTROUTE);
429 #else
430 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
431 	    so->so_options & SO_DONTROUTE);
432 #endif
433 	if (error) {
434 out:
435 		if (error == ENOBUFS) {
436 			tcp_quench(tp->t_inpcb);
437 			return (0);
438 		}
439 		if ((error == EHOSTUNREACH || error == ENETDOWN)
440 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
441 			tp->t_softerror = error;
442 			return (0);
443 		}
444 		return (error);
445 	}
446 	tcpstat.tcps_sndtotal++;
447 
448 	/*
449 	 * Data sent (as far as we can tell).
450 	 * If this advertises a larger window than any other segment,
451 	 * then remember the size of the advertised window.
452 	 * Any pending ACK has now been sent.
453 	 */
454 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
455 		tp->rcv_adv = tp->rcv_nxt + win;
456 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
457 	if (sendalot)
458 		goto again;
459 	return (0);
460 }
461 
462 tcp_setpersist(tp)
463 	register struct tcpcb *tp;
464 {
465 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
466 
467 	if (tp->t_timer[TCPT_REXMT])
468 		panic("tcp_output REXMT");
469 	/*
470 	 * Start/restart persistance timer.
471 	 */
472 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
473 	    t * tcp_backoff[tp->t_rxtshift],
474 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
475 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
476 		tp->t_rxtshift++;
477 }
478