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