1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include "qemu/osdep.h"
42#include "slirp.h"
43
44static const u_char tcp_outflags[TCP_NSTATES] = {
45 TH_RST|TH_ACK, 0, TH_SYN, TH_SYN|TH_ACK,
46 TH_ACK, TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
47 TH_FIN|TH_ACK, TH_ACK, TH_ACK,
48};
49
50
51#undef MAX_TCPOPTLEN
52#define MAX_TCPOPTLEN 32
53
54
55
56
57int
58tcp_output(struct tcpcb *tp)
59{
60 register struct socket *so = tp->t_socket;
61 register long len, win;
62 int off, flags, error;
63 register struct mbuf *m;
64 register struct tcpiphdr *ti, tcpiph_save;
65 struct ip *ip;
66 struct ip6 *ip6;
67 u_char opt[MAX_TCPOPTLEN];
68 unsigned optlen, hdrlen;
69 int idle, sendalot;
70
71 DEBUG_CALL("tcp_output");
72 DEBUG_ARG("tp = %p", tp);
73
74
75
76
77
78
79
80 idle = (tp->snd_max == tp->snd_una);
81 if (idle && tp->t_idle >= tp->t_rxtcur)
82
83
84
85
86
87 tp->snd_cwnd = tp->t_maxseg;
88again:
89 sendalot = 0;
90 off = tp->snd_nxt - tp->snd_una;
91 win = MIN(tp->snd_wnd, tp->snd_cwnd);
92
93 flags = tcp_outflags[tp->t_state];
94
95 DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
96
97
98
99
100
101
102
103 if (tp->t_force) {
104 if (win == 0) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 if (off < so->so_snd.sb_cc)
122 flags &= ~TH_FIN;
123 win = 1;
124 } else {
125 tp->t_timer[TCPT_PERSIST] = 0;
126 tp->t_rxtshift = 0;
127 }
128 }
129
130 len = MIN(so->so_snd.sb_cc, win) - off;
131
132 if (len < 0) {
133
134
135
136
137
138
139
140
141
142
143 len = 0;
144 if (win == 0) {
145 tp->t_timer[TCPT_REXMT] = 0;
146 tp->snd_nxt = tp->snd_una;
147 }
148 }
149
150 if (len > tp->t_maxseg) {
151 len = tp->t_maxseg;
152 sendalot = 1;
153 }
154 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
155 flags &= ~TH_FIN;
156
157 win = sbspace(&so->so_rcv);
158
159
160
161
162
163
164
165
166
167
168
169 if (len) {
170 if (len == tp->t_maxseg)
171 goto send;
172 if ((1 || idle || tp->t_flags & TF_NODELAY) &&
173 len + off >= so->so_snd.sb_cc)
174 goto send;
175 if (tp->t_force)
176 goto send;
177 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
178 goto send;
179 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
180 goto send;
181 }
182
183
184
185
186
187
188
189
190 if (win > 0) {
191
192
193
194
195
196 long adv = MIN(win, (long)TCP_MAXWIN << tp->rcv_scale) -
197 (tp->rcv_adv - tp->rcv_nxt);
198
199 if (adv >= (long) (2 * tp->t_maxseg))
200 goto send;
201 if (2 * adv >= (long) so->so_rcv.sb_datalen)
202 goto send;
203 }
204
205
206
207
208 if (tp->t_flags & TF_ACKNOW)
209 goto send;
210 if (flags & (TH_SYN|TH_RST))
211 goto send;
212 if (SEQ_GT(tp->snd_up, tp->snd_una))
213 goto send;
214
215
216
217
218
219 if (flags & TH_FIN &&
220 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
221 goto send;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
246 tp->t_timer[TCPT_PERSIST] == 0) {
247 tp->t_rxtshift = 0;
248 tcp_setpersist(tp);
249 }
250
251
252
253
254 return (0);
255
256send:
257
258
259
260
261
262
263
264
265 optlen = 0;
266 hdrlen = sizeof (struct tcpiphdr);
267 if (flags & TH_SYN) {
268 tp->snd_nxt = tp->iss;
269 if ((tp->t_flags & TF_NOOPT) == 0) {
270 uint16_t mss;
271
272 opt[0] = TCPOPT_MAXSEG;
273 opt[1] = 4;
274 mss = htons((uint16_t) tcp_mss(tp, 0));
275 memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
276 optlen = 4;
277 }
278 }
279
280 hdrlen += optlen;
281
282
283
284
285
286 if (len > tp->t_maxseg - optlen) {
287 len = tp->t_maxseg - optlen;
288 sendalot = 1;
289 }
290
291
292
293
294
295
296 if (len) {
297 m = m_get(so->slirp);
298 if (m == NULL) {
299 error = 1;
300 goto out;
301 }
302 m->m_data += IF_MAXLINKHDR;
303 m->m_len = hdrlen;
304
305 sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
306 m->m_len += len;
307
308
309
310
311
312
313
314 if (off + len == so->so_snd.sb_cc)
315 flags |= TH_PUSH;
316 } else {
317 m = m_get(so->slirp);
318 if (m == NULL) {
319 error = 1;
320 goto out;
321 }
322 m->m_data += IF_MAXLINKHDR;
323 m->m_len = hdrlen;
324 }
325
326 ti = mtod(m, struct tcpiphdr *);
327
328 memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
329
330
331
332
333
334
335 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
336 tp->snd_nxt == tp->snd_max)
337 tp->snd_nxt--;
338
339
340
341
342
343
344
345
346
347
348
349
350
351 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
352 ti->ti_seq = htonl(tp->snd_nxt);
353 else
354 ti->ti_seq = htonl(tp->snd_max);
355 ti->ti_ack = htonl(tp->rcv_nxt);
356 if (optlen) {
357 memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
358 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
359 }
360 ti->ti_flags = flags;
361
362
363
364
365 if (win < (long)(so->so_rcv.sb_datalen / 4) && win < (long)tp->t_maxseg)
366 win = 0;
367 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
368 win = (long)TCP_MAXWIN << tp->rcv_scale;
369 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
370 win = (long)(tp->rcv_adv - tp->rcv_nxt);
371 ti->ti_win = htons((uint16_t) (win>>tp->rcv_scale));
372
373 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
374 ti->ti_urp = htons((uint16_t)(tp->snd_up - ntohl(ti->ti_seq)));
375 ti->ti_flags |= TH_URG;
376 } else
377
378
379
380
381
382
383 tp->snd_up = tp->snd_una;
384
385
386
387
388
389 if (len + optlen)
390 ti->ti_len = htons((uint16_t)(sizeof (struct tcphdr) +
391 optlen + len));
392 ti->ti_sum = cksum(m, (int)(hdrlen + len));
393
394
395
396
397
398 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
399 tcp_seq startseq = tp->snd_nxt;
400
401
402
403
404 if (flags & (TH_SYN|TH_FIN)) {
405 if (flags & TH_SYN)
406 tp->snd_nxt++;
407 if (flags & TH_FIN) {
408 tp->snd_nxt++;
409 tp->t_flags |= TF_SENTFIN;
410 }
411 }
412 tp->snd_nxt += len;
413 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
414 tp->snd_max = tp->snd_nxt;
415
416
417
418
419 if (tp->t_rtt == 0) {
420 tp->t_rtt = 1;
421 tp->t_rtseq = startseq;
422 }
423 }
424
425
426
427
428
429
430
431
432
433 if (tp->t_timer[TCPT_REXMT] == 0 &&
434 tp->snd_nxt != tp->snd_una) {
435 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
436 if (tp->t_timer[TCPT_PERSIST]) {
437 tp->t_timer[TCPT_PERSIST] = 0;
438 tp->t_rxtshift = 0;
439 }
440 }
441 } else
442 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
443 tp->snd_max = tp->snd_nxt + len;
444
445
446
447
448
449
450
451 m->m_len = hdrlen + len;
452 tcpiph_save = *mtod(m, struct tcpiphdr *);
453
454 switch (so->so_ffamily) {
455 case AF_INET:
456 m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
457 - sizeof(struct ip);
458 m->m_len -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
459 - sizeof(struct ip);
460 ip = mtod(m, struct ip *);
461
462 ip->ip_len = m->m_len;
463 ip->ip_dst = tcpiph_save.ti_dst;
464 ip->ip_src = tcpiph_save.ti_src;
465 ip->ip_p = tcpiph_save.ti_pr;
466
467 ip->ip_ttl = IPDEFTTL;
468 ip->ip_tos = so->so_iptos;
469 error = ip_output(so, m);
470 break;
471
472 case AF_INET6:
473 m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
474 - sizeof(struct ip6);
475 m->m_len -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
476 - sizeof(struct ip6);
477 ip6 = mtod(m, struct ip6 *);
478
479 ip6->ip_pl = tcpiph_save.ti_len;
480 ip6->ip_dst = tcpiph_save.ti_dst6;
481 ip6->ip_src = tcpiph_save.ti_src6;
482 ip6->ip_nh = tcpiph_save.ti_nh6;
483
484 error = ip6_output(so, m, 0);
485 break;
486
487 default:
488 g_assert_not_reached();
489 }
490
491 if (error) {
492out:
493 return (error);
494 }
495
496
497
498
499
500
501
502 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
503 tp->rcv_adv = tp->rcv_nxt + win;
504 tp->last_ack_sent = tp->rcv_nxt;
505 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
506 if (sendalot)
507 goto again;
508
509 return (0);
510}
511
512void
513tcp_setpersist(struct tcpcb *tp)
514{
515 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
516
517
518
519
520 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
521 t * tcp_backoff[tp->t_rxtshift],
522 TCPTV_PERSMIN, TCPTV_PERSMAX);
523 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
524 tp->t_rxtshift++;
525}
526