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