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