xref: /original-bsd/sys/netinet/tcp_output.c (revision f0fd5f8a)
1 /*	tcp_output.c	4.49	82/12/14	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/protosw.h"
7 #include "../h/socket.h"
8 #include "../h/socketvar.h"
9 #include "../netinet/in.h"
10 #include "../net/route.h"
11 #include "../netinet/in_pcb.h"
12 #include "../netinet/in_systm.h"
13 #include "../netinet/ip.h"
14 #include "../netinet/ip_var.h"
15 #include "../netinet/tcp.h"
16 #define	TCPOUTFLAGS
17 #include "../netinet/tcp_fsm.h"
18 #include "../netinet/tcp_seq.h"
19 #include "../netinet/tcp_timer.h"
20 #include "../netinet/tcp_var.h"
21 #include "../netinet/tcpip.h"
22 #include "../netinet/tcp_debug.h"
23 #include <errno.h>
24 
25 char *tcpstates[]; /* XXX */
26 
27 /*
28  * Initial options.
29  */
30 u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
31 
32 /*
33  * Tcp output routine: figure out what should be sent and send it.
34  */
35 tcp_output(tp)
36 	register struct tcpcb *tp;
37 {
38 	register struct socket *so = tp->t_inpcb->inp_socket;
39 	register int len;
40 	struct mbuf *m0;
41 	int off, flags, win, error;
42 	register struct mbuf *m;
43 	register struct tcpiphdr *ti;
44 	u_char *opt;
45 	unsigned optlen = 0;
46 	int sendalot;
47 
48 
49 	/*
50 	 * Determine length of data that should be transmitted,
51 	 * and flags that will be used.
52 	 * If there is some data or critical controls (SYN, RST)
53 	 * to send, then transmit; otherwise, investigate further.
54 	 */
55 again:
56 	sendalot = 0;
57 	off = tp->snd_nxt - tp->snd_una;
58 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
59 	if (len < 0)
60 		return (0);	/* ??? */	/* past FIN */
61 	if (len > tp->t_maxseg) {
62 		len = tp->t_maxseg;
63 		sendalot = 1;
64 	}
65 
66 	flags = tcp_outflags[tp->t_state];
67 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
68 		flags &= ~TH_FIN;
69 	if (flags & (TH_SYN|TH_RST|TH_FIN))
70 		goto send;
71 	if (SEQ_GT(tp->snd_up, tp->snd_una))
72 		goto send;
73 
74 	/*
75 	 * Sender silly window avoidance.  If can send all data,
76 	 * a maximum segment, at least 1/4 of window do it,
77 	 * or are forced, do it; otherwise don't bother.
78 	 */
79 	if (len) {
80 		if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc)
81 			goto send;
82 		if (len * 4 >= tp->snd_wnd)		/* a lot */
83 			goto send;
84 		if (tp->t_force)
85 			goto send;
86 	}
87 
88 	/*
89 	 * Send if we owe peer an ACK.
90 	 */
91 	if (tp->t_flags&TF_ACKNOW)
92 		goto send;
93 
94 
95 	/*
96 	 * Calculate available window in i, and also amount
97 	 * of window known to peer (as advertised window less
98 	 * next expected input.)  If this is 35% or more of the
99 	 * maximum possible window, then want to send a segment to peer.
100 	 */
101 	win = sbspace(&so->so_rcv);
102 	if (win > 0 &&
103 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
104 		goto send;
105 
106 	/*
107 	 * TCP window updates are not reliable, rather a polling protocol
108 	 * using ``persist'' packets is used to insure receipt of window
109 	 * updates.  The three ``states'' for the output side are:
110 	 *	idle			not doing retransmits or persists
111 	 *	persisting		to move a zero window
112 	 *	(re)transmitting	and thereby not persisting
113 	 *
114 	 * tp->t_timer[TCPT_PERSIST]
115 	 *	is set when we are in persist state.
116 	 * tp->t_force
117 	 *	is set when we are called to send a persist packet.
118 	 * tp->t_timer[TCPT_REXMT]
119 	 *	is set when we are retransmitting
120 	 * The output side is idle when both timers are zero.
121 	 *
122 	 * If send window is closed, there is data to transmit, and no
123 	 * retransmit or persist is pending, then go to persist state,
124 	 * arranging to force out a byte to get more current window information
125 	 * if nothing happens soon.
126 	 */
127 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
128 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
129 		tp->t_rxtshift = 0;
130 		tcp_setpersist(tp);
131 	}
132 
133 	/*
134 	 * No reason to send a segment, just return.
135 	 */
136 	return (0);
137 
138 send:
139 	/*
140 	 * Grab a header mbuf, attaching a copy of data to
141 	 * be transmitted, and initialize the header from
142 	 * the template for sends on this connection.
143 	 */
144 	MGET(m, M_DONTWAIT, MT_DATA);
145 	if (m == 0)
146 		return (ENOBUFS);
147 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
148 	m->m_len = sizeof (struct tcpiphdr);
149 	if (len) {
150 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
151 		if (m->m_next == 0)
152 			len = 0;
153 	}
154 	ti = mtod(m, struct tcpiphdr *);
155 	if (tp->t_template == 0)
156 		panic("tcp_output");
157 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
158 
159 	/*
160 	 * Fill in fields, remembering maximum advertised
161 	 * window for use in delaying messages about window sizes.
162 	 */
163 	ti->ti_seq = tp->snd_nxt;
164 	ti->ti_ack = tp->rcv_nxt;
165 	ti->ti_seq = htonl(ti->ti_seq);
166 	ti->ti_ack = htonl(ti->ti_ack);
167 	/*
168 	 * Before ESTABLISHED, force sending of initial options
169 	 * unless TCP set to not do any options.
170 	 */
171 	if (tp->t_state < TCPS_ESTABLISHED) {
172 		if (tp->t_flags&TF_NOOPT)
173 			goto noopt;
174 		opt = tcp_initopt;
175 		optlen = sizeof (tcp_initopt);
176 		*(u_short *)(opt + 2) = MIN(so->so_rcv.sb_hiwat / 2, 1024);
177 		*(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
178 	} else {
179 		if (tp->t_tcpopt == 0)
180 			goto noopt;
181 		opt = mtod(tp->t_tcpopt, u_char *);
182 		optlen = tp->t_tcpopt->m_len;
183 	}
184 	if (opt) {
185 		m0 = m->m_next;
186 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
187 		if (m->m_next == 0) {
188 			(void) m_free(m);
189 			m_freem(m0);
190 			return (ENOBUFS);
191 		}
192 		m->m_next->m_next = m0;
193 		m0 = m->m_next;
194 		m0->m_len = optlen;
195 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
196 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
197 		while (m0->m_len & 0x3) {
198 			*opt++ = TCPOPT_EOL;
199 			m0->m_len++;
200 		}
201 		optlen = m0->m_len;
202 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
203 	}
204 noopt:
205 	ti->ti_flags = flags;
206 	win = sbspace(&so->so_rcv);
207 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
208 		win = 0;
209 	if (win > 0)
210 		ti->ti_win = htons((u_short)win);
211 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
212 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
213 		ti->ti_urp = htons(ti->ti_urp);
214 		ti->ti_flags |= TH_URG;
215 	} else
216 		/*
217 		 * If no urgent pointer to send, then we pull
218 		 * the urgent pointer to the left edge of the send window
219 		 * so that it doesn't drift into the send window on sequence
220 		 * number wraparound.
221 		 */
222 		tp->snd_up = tp->snd_una;		/* drag it along */
223 	/*
224 	 * If anything to send and we can send it all, set PUSH.
225 	 * (This will keep happy those implementations which only
226 	 * give data to the user when a buffer fills or a PUSH comes in.
227 	 */
228 	if (len && off+len == so->so_snd.sb_cc)
229 		ti->ti_flags |= TH_PUSH;
230 
231 	/*
232 	 * Put TCP length in extended header, and then
233 	 * checksum extended header and data.
234 	 */
235 	if (len + optlen) {
236 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
237 		ti->ti_len = htons((u_short)ti->ti_len);
238 	}
239 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
240 
241 	/*
242 	 * In transmit state, time the transmission and arrange for
243 	 * the retransmit.  In persist state, reset persist time for
244 	 * next persist.
245 	 */
246 	if (tp->t_force == 0) {
247 		/*
248 		 * Advance snd_nxt over sequence space of this segment.
249 		 */
250 		if (flags & (TH_SYN|TH_FIN))
251 			tp->snd_nxt++;
252 		tp->snd_nxt += len;
253 		if (SEQ_GT(tp->snd_nxt, tp->snd_max))
254 			tp->snd_max = tp->snd_nxt;
255 
256 		/*
257 		 * Time this transmission if not a retransmission and
258 		 * not currently timing anything.
259 		 */
260 		if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
261 			tp->t_rtt = 1;
262 			tp->t_rtseq = tp->snd_nxt - len;
263 		}
264 
265 		/*
266 		 * Set retransmit timer if not currently set.
267 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
268 		 * Initialize shift counter which is used for exponential
269 		 * backoff of retransmit time.
270 		 */
271 		if (tp->t_timer[TCPT_REXMT] == 0 &&
272 		    tp->snd_nxt != tp->snd_una) {
273 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
274 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
275 			tp->t_rtt = 0;
276 			tp->t_rxtshift = 0;
277 		}
278 		tp->t_timer[TCPT_PERSIST] = 0;
279 	} else {
280 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
281 			tp->snd_max = tp->snd_una+1;
282 	}
283 
284 	/*
285 	 * Trace.
286 	 */
287 	if (so->so_options & SO_DEBUG)
288 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
289 
290 	/*
291 	 * Fill in IP length and desired time to live and
292 	 * send to IP level.
293 	 */
294 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
295 	((struct ip *)ti)->ip_ttl = TCP_TTL;
296 	if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ?
297 	    &routetoif : &tp->t_inpcb->inp_route, 0))
298 		return (error);
299 
300 	/*
301 	 * Data sent (as far as we can tell).
302 	 * If this advertises a larger window than any other segment,
303 	 * then remember the size of the advertised window.
304 	 * Drop send for purpose of ACK requirements.
305 	 */
306 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
307 		tp->rcv_adv = tp->rcv_nxt + win;
308 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
309 	if (sendalot && tp->t_force == 0)
310 		goto again;
311 	return (0);
312 }
313 
314 tcp_setpersist(tp)
315 	register struct tcpcb *tp;
316 {
317 
318 	if (tp->t_timer[TCPT_REXMT])
319 		panic("tcp_output REXMT");
320 	/*
321 	 * Start/restart persistance timer.
322 	 */
323 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
324 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
325 	    TCPTV_PERSMIN, TCPTV_MAX);
326 	tp->t_rxtshift++;
327 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
328 		tp->t_rxtshift = 0;
329 }
330