Commit | Line | Data |
---|---|---|
77a4e3ca | 1 | /* tcp_output.c 4.45 82/10/05 */ |
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 | /* | |
77a4e3ca | 28 | * Initial options. |
8b5a83bb | 29 | */ |
8b5a83bb | 30 | u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; |
8b5a83bb | 31 | |
ea727f86 | 32 | /* |
4aed14e3 | 33 | * Tcp output routine: figure out what should be sent and send it. |
ea727f86 | 34 | */ |
a6503abf | 35 | tcp_output(tp) |
53a5409e | 36 | register struct tcpcb *tp; |
ea727f86 | 37 | { |
53a5409e | 38 | register struct socket *so = tp->t_inpcb->inp_socket; |
a6503abf BJ |
39 | register int len; |
40 | struct mbuf *m0; | |
8a2f82db | 41 | int off, flags, win, error; |
a6503abf BJ |
42 | register struct mbuf *m; |
43 | register struct tcpiphdr *ti; | |
8b5a83bb BJ |
44 | u_char *opt; |
45 | unsigned optlen = 0; | |
2266a466 | 46 | int sendalot; |
76ee76df | 47 | |
76ee76df | 48 | |
a6503abf | 49 | /* |
8ae6c089 | 50 | * Determine length of data that should be transmitted, |
0974b45c BJ |
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. | |
a6503abf | 54 | */ |
2266a466 BJ |
55 | again: |
56 | sendalot = 0; | |
a6503abf | 57 | off = tp->snd_nxt - tp->snd_una; |
405c9168 | 58 | len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off; |
3232ef09 | 59 | if (len < 0) |
8a2f82db | 60 | return (0); /* ??? */ /* past FIN */ |
2266a466 | 61 | if (len > tp->t_maxseg) { |
0974b45c | 62 | len = tp->t_maxseg; |
2266a466 BJ |
63 | sendalot = 1; |
64 | } | |
8ae6c089 | 65 | |
0974b45c | 66 | flags = tcp_outflags[tp->t_state]; |
275e05b7 | 67 | if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc) |
405c9168 | 68 | flags &= ~TH_FIN; |
8ae6c089 BJ |
69 | if (flags & (TH_SYN|TH_RST|TH_FIN)) |
70 | goto send; | |
71 | if (SEQ_GT(tp->snd_up, tp->snd_una)) | |
a6503abf BJ |
72 | goto send; |
73 | ||
8ae6c089 BJ |
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 | ||
a6503abf | 88 | /* |
3232ef09 | 89 | * Send if we owe peer an ACK. |
a6503abf | 90 | */ |
8b5a83bb BJ |
91 | if (tp->t_flags&TF_ACKNOW) |
92 | goto send; | |
93 | ||
76ee76df | 94 | |
a6503abf BJ |
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 | */ | |
0974b45c BJ |
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)) | |
a6503abf BJ |
104 | goto send; |
105 | ||
2266a466 BJ |
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 | ||
a6503abf BJ |
133 | /* |
134 | * No reason to send a segment, just return. | |
135 | */ | |
f1b2fa5b | 136 | return (0); |
a6503abf BJ |
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 | */ | |
76ee76df BJ |
144 | MGET(m, 0); |
145 | if (m == 0) | |
8a2f82db | 146 | return (ENOBUFS); |
4aed14e3 | 147 | m->m_off = MMAXOFF - sizeof (struct tcpiphdr); |
53a5409e | 148 | m->m_len = sizeof (struct tcpiphdr); |
a6503abf BJ |
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"); | |
f1b2fa5b | 157 | bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); |
a6503abf BJ |
158 | |
159 | /* | |
160 | * Fill in fields, remembering maximum advertised | |
161 | * window for use in delaying messages about window sizes. | |
162 | */ | |
4aed14e3 BJ |
163 | ti->ti_seq = tp->snd_nxt; |
164 | ti->ti_ack = tp->rcv_nxt; | |
165 | #if vax | |
166 | ti->ti_seq = htonl(ti->ti_seq); | |
167 | ti->ti_ack = htonl(ti->ti_ack); | |
168 | #endif | |
8b5a83bb BJ |
169 | /* |
170 | * Before ESTABLISHED, force sending of initial options | |
171 | * unless TCP set to not do any options. | |
172 | */ | |
173 | if (tp->t_state < TCPS_ESTABLISHED) { | |
174 | if (tp->t_flags&TF_NOOPT) | |
175 | goto noopt; | |
176 | opt = tcp_initopt; | |
177 | optlen = sizeof (tcp_initopt); | |
77a4e3ca | 178 | *(u_short *)(opt + 2) = MIN(so->so_rcv.sb_hiwat / 2, 1024); |
8b5a83bb BJ |
179 | #if vax |
180 | *(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2)); | |
181 | #endif | |
182 | } else { | |
183 | if (tp->t_tcpopt == 0) | |
184 | goto noopt; | |
185 | opt = mtod(tp->t_tcpopt, u_char *); | |
186 | optlen = tp->t_tcpopt->m_len; | |
187 | } | |
77a4e3ca | 188 | if (opt) { |
f1b2fa5b | 189 | m0 = m->m_next; |
ef9b4258 | 190 | m->m_next = m_get(M_DONTWAIT); |
0974b45c BJ |
191 | if (m->m_next == 0) { |
192 | (void) m_free(m); | |
8b5a83bb | 193 | m_freem(m0); |
8a2f82db | 194 | return (ENOBUFS); |
0974b45c BJ |
195 | } |
196 | m->m_next->m_next = m0; | |
8b5a83bb | 197 | m0 = m->m_next; |
8b5a83bb | 198 | m0->m_len = optlen; |
668cc26d | 199 | bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); |
8b5a83bb | 200 | opt = (u_char *)(mtod(m0, caddr_t) + optlen); |
8b5a83bb BJ |
201 | while (m0->m_len & 0x3) { |
202 | *opt++ = TCPOPT_EOL; | |
203 | m0->m_len++; | |
204 | } | |
205 | optlen = m0->m_len; | |
206 | ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; | |
0974b45c | 207 | } |
8b5a83bb | 208 | noopt: |
0974b45c | 209 | ti->ti_flags = flags; |
a6503abf | 210 | win = sbspace(&so->so_rcv); |
8ae6c089 BJ |
211 | if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */ |
212 | win = 0; | |
a6503abf | 213 | if (win > 0) |
8ae6c089 | 214 | #if vax |
f1b2fa5b | 215 | ti->ti_win = htons((u_short)win); |
8ae6c089 BJ |
216 | #else |
217 | ti->ti_win = win; | |
218 | #endif | |
0974b45c | 219 | if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { |
5e74df82 BJ |
220 | ti->ti_urp = tp->snd_up - tp->snd_nxt; |
221 | #if vax | |
222 | ti->ti_urp = htons(ti->ti_urp); | |
223 | #endif | |
a6503abf BJ |
224 | ti->ti_flags |= TH_URG; |
225 | } else | |
226 | /* | |
227 | * If no urgent pointer to send, then we pull | |
228 | * the urgent pointer to the left edge of the send window | |
229 | * so that it doesn't drift into the send window on sequence | |
230 | * number wraparound. | |
231 | */ | |
0974b45c | 232 | tp->snd_up = tp->snd_una; /* drag it along */ |
02c1608b BJ |
233 | /* |
234 | * If anything to send and we can send it all, set PUSH. | |
235 | * (This will keep happy those implementations which only | |
236 | * give data to the user when a buffer fills or a PUSH comes in. | |
237 | */ | |
02c1608b BJ |
238 | if (len && off+len == so->so_snd.sb_cc) |
239 | ti->ti_flags |= TH_PUSH; | |
a6503abf BJ |
240 | |
241 | /* | |
242 | * Put TCP length in extended header, and then | |
243 | * checksum extended header and data. | |
244 | */ | |
8b5a83bb BJ |
245 | if (len + optlen) { |
246 | ti->ti_len = sizeof (struct tcphdr) + optlen + len; | |
247 | #if vax | |
248 | ti->ti_len = htons((u_short)ti->ti_len); | |
249 | #endif | |
250 | } | |
668cc26d | 251 | ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); |
0974b45c BJ |
252 | |
253 | /* | |
2266a466 BJ |
254 | * In transmit state, time the transmission and arrange for |
255 | * the retransmit. In persist state, reset persist time for | |
256 | * next persist. | |
0974b45c | 257 | */ |
2266a466 BJ |
258 | if (tp->t_force == 0) { |
259 | /* | |
8931cb5b | 260 | * Advance snd_nxt over sequence space of this segment. |
2266a466 BJ |
261 | */ |
262 | if (flags & (TH_SYN|TH_FIN)) | |
263 | tp->snd_nxt++; | |
264 | tp->snd_nxt += len; | |
e45e6858 BJ |
265 | if (SEQ_GT(tp->snd_nxt, tp->snd_max)) |
266 | tp->snd_max = tp->snd_nxt; | |
405c9168 | 267 | |
2266a466 BJ |
268 | /* |
269 | * Time this transmission if not a retransmission and | |
270 | * not currently timing anything. | |
271 | */ | |
272 | if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) { | |
273 | tp->t_rtt = 1; | |
274 | tp->t_rtseq = tp->snd_nxt - len; | |
275 | } | |
405c9168 | 276 | |
2266a466 BJ |
277 | /* |
278 | * Set retransmit timer if not currently set. | |
279 | * Initial value for retransmit timer to tcp_beta*tp->t_srtt. | |
280 | * Initialize shift counter which is used for exponential | |
281 | * backoff of retransmit time. | |
282 | */ | |
283 | if (tp->t_timer[TCPT_REXMT] == 0 && | |
284 | tp->snd_nxt != tp->snd_una) { | |
285 | TCPT_RANGESET(tp->t_timer[TCPT_REXMT], | |
286 | tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); | |
287 | tp->t_rtt = 0; | |
288 | tp->t_rxtshift = 0; | |
289 | } | |
290 | tp->t_timer[TCPT_PERSIST] = 0; | |
e45e6858 BJ |
291 | } else { |
292 | if (SEQ_GT(tp->snd_una+1, tp->snd_max)) | |
293 | tp->snd_max = tp->snd_una+1; | |
8931cb5b | 294 | } |
a6503abf | 295 | |
f1dd32da BJ |
296 | /* |
297 | * Trace. | |
298 | */ | |
8931cb5b | 299 | if (so->so_options & SO_DEBUG) |
f1dd32da BJ |
300 | tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); |
301 | ||
a6503abf BJ |
302 | /* |
303 | * Fill in IP length and desired time to live and | |
304 | * send to IP level. | |
305 | */ | |
8b5a83bb | 306 | ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; |
a6503abf | 307 | ((struct ip *)ti)->ip_ttl = TCP_TTL; |
8931cb5b BJ |
308 | if (error = ip_output(m, tp->t_ipopt, (so->so_options & SO_DONTROUTE) ? |
309 | &routetoif : &tp->t_inpcb->inp_route, 0)) | |
8a2f82db | 310 | return (error); |
a6503abf BJ |
311 | |
312 | /* | |
313 | * Data sent (as far as we can tell). | |
314 | * If this advertises a larger window than any other segment, | |
4aed14e3 | 315 | * then remember the size of the advertised window. |
0974b45c | 316 | * Drop send for purpose of ACK requirements. |
a6503abf | 317 | */ |
be43ac7f | 318 | if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) |
a6503abf | 319 | tp->rcv_adv = tp->rcv_nxt + win; |
0974b45c | 320 | tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); |
2266a466 BJ |
321 | if (sendalot && tp->t_force == 0) |
322 | goto again; | |
8a2f82db | 323 | return (0); |
76ee76df | 324 | } |
2266a466 BJ |
325 | |
326 | tcp_setpersist(tp) | |
327 | register struct tcpcb *tp; | |
328 | { | |
329 | ||
330 | if (tp->t_timer[TCPT_REXMT]) | |
331 | panic("tcp_output REXMT"); | |
332 | /* | |
333 | * Start/restart persistance timer. | |
334 | */ | |
335 | TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], | |
336 | ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, | |
337 | TCPTV_PERSMIN, TCPTV_MAX); | |
338 | tp->t_rxtshift++; | |
339 | if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) | |
340 | tp->t_rxtshift = 0; | |
341 | } |