|
|
1.1 root 1: /*
2: * Copyright (c) 1982, 1986, 1988, 1990, 1993
3: * The Regents of the University of California. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the University nor the names of its contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: *
29: * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
30: * tcp_output.c,v 1.3 1994/09/15 10:36:55 davidg Exp
31: */
32:
33: /*
34: * Changes and additions relating to SLiRP
35: * Copyright (c) 1995 Danny Gasparovski.
36: *
37: * Please read the file COPYRIGHT for the
38: * terms and conditions of the copyright.
39: */
40:
41: #include <slirp.h>
42:
43: /*
44: * Since this is only used in "stats socket", we give meaning
45: * names instead of the REAL names
46: */
47: char *tcpstates[] = {
48: /* "CLOSED", "LISTEN", "SYN_SENT", "SYN_RCVD", */
49: "REDIRECT", "LISTEN", "SYN_SENT", "SYN_RCVD",
50: "ESTABLISHED", "CLOSE_WAIT", "FIN_WAIT_1", "CLOSING",
51: "LAST_ACK", "FIN_WAIT_2", "TIME_WAIT",
52: };
53:
54: u_char tcp_outflags[TCP_NSTATES] = {
55: TH_RST|TH_ACK, 0, TH_SYN, TH_SYN|TH_ACK,
56: TH_ACK, TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
57: TH_FIN|TH_ACK, TH_ACK, TH_ACK,
58: };
59:
60:
61: #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
62:
63: /*
64: * Tcp output routine: figure out what should be sent and send it.
65: */
1.1.1.2 ! root 66: int tcp_output(register struct tcpcb *tp)
1.1 root 67: {
68: register struct socket *so = tp->t_socket;
69: register long len, win;
70: int off, flags, error;
71: register struct mbuf *m;
72: register struct tcpiphdr *ti;
73: u_char opt[MAX_TCPOPTLEN];
74: unsigned optlen, hdrlen;
75: int idle, sendalot;
76:
77: DEBUG_CALL("tcp_output");
78: DEBUG_ARG("tp = %lx", (long )tp);
79:
80: /*
81: * Determine length of data that should be transmitted,
82: * and flags that will be used.
83: * If there is some data or critical controls (SYN, RST)
84: * to send, then transmit; otherwise, investigate further.
85: */
86: idle = (tp->snd_max == tp->snd_una);
87: if (idle && tp->t_idle >= tp->t_rxtcur)
88: /*
89: * We have been idle for "a while" and no acks are
90: * expected to clock out any data we send --
91: * slow start to get ack "clock" running again.
92: */
93: tp->snd_cwnd = tp->t_maxseg;
94: again:
95: sendalot = 0;
96: off = tp->snd_nxt - tp->snd_una;
97: win = min(tp->snd_wnd, tp->snd_cwnd);
98:
99: flags = tcp_outflags[tp->t_state];
100:
101: DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
102:
103: /*
104: * If in persist timeout with window of 0, send 1 byte.
105: * Otherwise, if window is small but nonzero
106: * and timer expired, we will send what we can
107: * and go to transmit state.
108: */
109: if (tp->t_force) {
110: if (win == 0) {
111: /*
112: * If we still have some data to send, then
113: * clear the FIN bit. Usually this would
114: * happen below when it realizes that we
115: * aren't sending all the data. However,
116: * if we have exactly 1 byte of unset data,
117: * then it won't clear the FIN bit below,
118: * and if we are in persist state, we wind
119: * up sending the packet without recording
120: * that we sent the FIN bit.
121: *
122: * We can't just blindly clear the FIN bit,
123: * because if we don't have any more data
124: * to send then the probe will be the FIN
125: * itself.
126: */
1.1.1.2 ! root 127: if (off < (int)so->so_snd.sb_cc)
1.1 root 128: flags &= ~TH_FIN;
129: win = 1;
130: } else {
131: tp->t_timer[TCPT_PERSIST] = 0;
132: tp->t_rxtshift = 0;
133: }
134: }
135:
136: len = min(so->so_snd.sb_cc, win) - off;
137:
138: if (len < 0) {
139: /*
140: * If FIN has been sent but not acked,
141: * but we haven't been called to retransmit,
142: * len will be -1. Otherwise, window shrank
143: * after we sent into it. If window shrank to 0,
144: * cancel pending retransmit and pull snd_nxt
145: * back to (closed) window. We will enter persist
146: * state below. If the window didn't close completely,
147: * just wait for an ACK.
148: */
149: len = 0;
150: if (win == 0) {
151: tp->t_timer[TCPT_REXMT] = 0;
152: tp->snd_nxt = tp->snd_una;
153: }
154: }
155:
156: if (len > tp->t_maxseg) {
157: len = tp->t_maxseg;
158: sendalot = 1;
159: }
160: if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
161: flags &= ~TH_FIN;
162:
163: win = sbspace(&so->so_rcv);
164:
165: /*
166: * Sender silly window avoidance. If connection is idle
167: * and can send all data, a maximum segment,
168: * at least a maximum default-size segment do it,
169: * or are forced, do it; otherwise don't bother.
170: * If peer's buffer is tiny, then send
171: * when window is at least half open.
172: * If retransmitting (possibly after persist timer forced us
173: * to send into a small window), then must resend.
174: */
175: if (len) {
176: if (len == tp->t_maxseg)
177: goto send;
178: if ((1 || idle || tp->t_flags & TF_NODELAY) &&
179: len + off >= so->so_snd.sb_cc)
180: goto send;
181: if (tp->t_force)
182: goto send;
183: if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
184: goto send;
185: if (SEQ_LT(tp->snd_nxt, tp->snd_max))
186: goto send;
187: }
188:
189: /*
190: * Compare available window to amount of window
191: * known to peer (as advertised window less
192: * next expected input). If the difference is at least two
193: * max size segments, or at least 50% of the maximum possible
194: * window, then want to send a window update to peer.
195: */
196: if (win > 0) {
197: /*
198: * "adv" is the amount we can increase the window,
199: * taking into account that we are limited by
200: * TCP_MAXWIN << tp->rcv_scale.
201: */
1.1.1.2 ! root 202: long adv = min(win, TCP_MAXWIN << tp->rcv_scale) -
1.1 root 203: (tp->rcv_adv - tp->rcv_nxt);
204:
1.1.1.2 ! root 205: if (adv >= (long)(2 * tp->t_maxseg))
1.1 root 206: goto send;
1.1.1.2 ! root 207: if (2 * adv >= (long)so->so_rcv.sb_datalen)
1.1 root 208: goto send;
209: }
210:
211: /*
212: * Send if we owe peer an ACK.
213: */
214: if (tp->t_flags & TF_ACKNOW)
215: goto send;
216: if (flags & (TH_SYN|TH_RST))
217: goto send;
218: if (SEQ_GT(tp->snd_up, tp->snd_una))
219: goto send;
220: /*
221: * If our state indicates that FIN should be sent
222: * and we have not yet done so, or we're retransmitting the FIN,
223: * then we need to send.
224: */
225: if (flags & TH_FIN &&
226: ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
227: goto send;
228:
229: /*
230: * TCP window updates are not reliable, rather a polling protocol
231: * using ``persist'' packets is used to insure receipt of window
232: * updates. The three ``states'' for the output side are:
233: * idle not doing retransmits or persists
234: * persisting to move a small or zero window
235: * (re)transmitting and thereby not persisting
236: *
237: * tp->t_timer[TCPT_PERSIST]
238: * is set when we are in persist state.
239: * tp->t_force
240: * is set when we are called to send a persist packet.
241: * tp->t_timer[TCPT_REXMT]
242: * is set when we are retransmitting
243: * The output side is idle when both timers are zero.
244: *
245: * If send window is too small, there is data to transmit, and no
246: * retransmit or persist is pending, then go to persist state.
247: * If nothing happens soon, send when timer expires:
248: * if window is nonzero, transmit what we can,
249: * otherwise force out a byte.
250: */
251: if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
252: tp->t_timer[TCPT_PERSIST] == 0) {
253: tp->t_rxtshift = 0;
254: tcp_setpersist(tp);
255: }
256:
257: /*
258: * No reason to send a segment, just return.
259: */
260: tcpstat.tcps_didnuttin++;
261:
262: return (0);
263:
264: send:
265: /*
266: * Before ESTABLISHED, force sending of initial options
267: * unless TCP set not to do any options.
268: * NOTE: we assume that the IP/TCP header plus TCP options
269: * always fit in a single mbuf, leaving room for a maximum
270: * link header, i.e.
271: * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
272: */
273: optlen = 0;
274: hdrlen = sizeof (struct tcpiphdr);
275: if (flags & TH_SYN) {
276: tp->snd_nxt = tp->iss;
277: if ((tp->t_flags & TF_NOOPT) == 0) {
278: u_int16_t mss;
279:
280: opt[0] = TCPOPT_MAXSEG;
281: opt[1] = 4;
282: mss = htons((u_int16_t) tcp_mss(tp, 0));
283: memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
284: optlen = 4;
285:
286: /* if ((tp->t_flags & TF_REQ_SCALE) &&
287: * ((flags & TH_ACK) == 0 ||
288: * (tp->t_flags & TF_RCVD_SCALE))) {
289: * *((u_int32_t *) (opt + optlen)) = htonl(
290: * TCPOPT_NOP << 24 |
291: * TCPOPT_WINDOW << 16 |
292: * TCPOLEN_WINDOW << 8 |
293: * tp->request_r_scale);
294: * optlen += 4;
295: * }
296: */
297: }
298: }
299:
300: /*
301: * Send a timestamp and echo-reply if this is a SYN and our side
302: * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
303: * and our peer have sent timestamps in our SYN's.
304: */
305: /* if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
306: * (flags & TH_RST) == 0 &&
307: * ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
308: * (tp->t_flags & TF_RCVD_TSTMP))) {
309: * u_int32_t *lp = (u_int32_t *)(opt + optlen);
310: *
311: * / * Form timestamp option as shown in appendix A of RFC 1323. * /
312: * *lp++ = htonl(TCPOPT_TSTAMP_HDR);
313: * *lp++ = htonl(tcp_now);
314: * *lp = htonl(tp->ts_recent);
315: * optlen += TCPOLEN_TSTAMP_APPA;
316: * }
317: */
318: hdrlen += optlen;
319:
320: /*
321: * Adjust data length if insertion of options will
322: * bump the packet length beyond the t_maxseg length.
323: */
324: if (len > tp->t_maxseg - optlen) {
325: len = tp->t_maxseg - optlen;
326: sendalot = 1;
327: }
328:
329: /*
330: * Grab a header mbuf, attaching a copy of data to
331: * be transmitted, and initialize the header from
332: * the template for sends on this connection.
333: */
334: if (len) {
335: if (tp->t_force && len == 1)
336: tcpstat.tcps_sndprobe++;
337: else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
338: tcpstat.tcps_sndrexmitpack++;
339: tcpstat.tcps_sndrexmitbyte += len;
340: } else {
341: tcpstat.tcps_sndpack++;
342: tcpstat.tcps_sndbyte += len;
343: }
344:
345: m = m_get();
346: if (m == NULL) {
347: /* error = ENOBUFS; */
348: error = 1;
349: goto out;
350: }
351: m->m_data += if_maxlinkhdr;
352: m->m_len = hdrlen;
353:
354: /*
355: * This will always succeed, since we make sure our mbufs
356: * are big enough to hold one MSS packet + header + ... etc.
357: */
358: /* if (len <= MHLEN - hdrlen - max_linkhdr) { */
359:
1.1.1.2 ! root 360: sbcopy(&so->so_snd, off, len, mtod(m, caddr_t) + hdrlen);
1.1 root 361: m->m_len += len;
362:
363: /* } else {
364: * m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
365: * if (m->m_next == 0)
366: * len = 0;
367: * }
368: */
369: /*
370: * If we're sending everything we've got, set PUSH.
371: * (This will keep happy those implementations which only
372: * give data to the user when a buffer fills or
373: * a PUSH comes in.)
374: */
375: if (off + len == so->so_snd.sb_cc)
376: flags |= TH_PUSH;
377: } else {
378: if (tp->t_flags & TF_ACKNOW)
379: tcpstat.tcps_sndacks++;
380: else if (flags & (TH_SYN|TH_FIN|TH_RST))
381: tcpstat.tcps_sndctrl++;
382: else if (SEQ_GT(tp->snd_up, tp->snd_una))
383: tcpstat.tcps_sndurg++;
384: else
385: tcpstat.tcps_sndwinup++;
386:
387: m = m_get();
388: if (m == NULL) {
389: /* error = ENOBUFS; */
390: error = 1;
391: goto out;
392: }
393: m->m_data += if_maxlinkhdr;
394: m->m_len = hdrlen;
395: }
396:
397: ti = mtod(m, struct tcpiphdr *);
398:
399: memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
400:
401: /*
402: * Fill in fields, remembering maximum advertised
403: * window for use in delaying messages about window sizes.
404: * If resending a FIN, be sure not to use a new sequence number.
405: */
406: if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
407: tp->snd_nxt == tp->snd_max)
408: tp->snd_nxt--;
409: /*
410: * If we are doing retransmissions, then snd_nxt will
411: * not reflect the first unsent octet. For ACK only
412: * packets, we do not want the sequence number of the
413: * retransmitted packet, we want the sequence number
414: * of the next unsent octet. So, if there is no data
415: * (and no SYN or FIN), use snd_max instead of snd_nxt
416: * when filling in ti_seq. But if we are in persist
417: * state, snd_max might reflect one byte beyond the
418: * right edge of the window, so use snd_nxt in that
419: * case, since we know we aren't doing a retransmission.
420: * (retransmit and persist are mutually exclusive...)
421: */
422: if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
423: ti->ti_seq = htonl(tp->snd_nxt);
424: else
425: ti->ti_seq = htonl(tp->snd_max);
426: ti->ti_ack = htonl(tp->rcv_nxt);
427: if (optlen) {
428: memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
429: ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
430: }
431: ti->ti_flags = flags;
432: /*
433: * Calculate receive window. Don't shrink window,
434: * but avoid silly window syndrome.
435: */
1.1.1.2 ! root 436: if (win < (so->so_rcv.sb_datalen / 4) && win < tp->t_maxseg)
1.1 root 437: win = 0;
1.1.1.2 ! root 438: if (win > (u_long) (TCP_MAXWIN << tp->rcv_scale))
! 439: win = (u_long) (TCP_MAXWIN << tp->rcv_scale);
! 440: if (win < (tp->rcv_adv - tp->rcv_nxt))
! 441: win = (tp->rcv_adv - tp->rcv_nxt);
1.1 root 442: ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
443:
444: if (SEQ_GT(tp->snd_up, tp->snd_una)) {
445: ti->ti_urp = htons((u_int16_t)(tp->snd_up - ntohl(ti->ti_seq)));
446: #ifdef notdef
447: if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
448: ti->ti_urp = htons((u_int16_t)(tp->snd_up - tp->snd_nxt));
449: #endif
450: ti->ti_flags |= TH_URG;
451: } else
452: /*
453: * If no urgent pointer to send, then we pull
454: * the urgent pointer to the left edge of the send window
455: * so that it doesn't drift into the send window on sequence
456: * number wraparound.
457: */
458: tp->snd_up = tp->snd_una; /* drag it along */
459:
460: /*
461: * Put TCP length in extended header, and then
462: * checksum extended header and data.
463: */
464: if (len + optlen)
465: ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
466: optlen + len));
467: ti->ti_sum = cksum(m, (int)(hdrlen + len));
468:
469: /*
470: * In transmit state, time the transmission and arrange for
471: * the retransmit. In persist state, just set snd_max.
472: */
473: if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
474: tcp_seq startseq = tp->snd_nxt;
475:
476: /*
477: * Advance snd_nxt over sequence space of this segment.
478: */
479: if (flags & (TH_SYN|TH_FIN)) {
480: if (flags & TH_SYN)
481: tp->snd_nxt++;
482: if (flags & TH_FIN) {
483: tp->snd_nxt++;
484: tp->t_flags |= TF_SENTFIN;
485: }
486: }
487: tp->snd_nxt += len;
488: if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
489: tp->snd_max = tp->snd_nxt;
490: /*
491: * Time this transmission if not a retransmission and
492: * not currently timing anything.
493: */
494: if (tp->t_rtt == 0) {
495: tp->t_rtt = 1;
496: tp->t_rtseq = startseq;
497: tcpstat.tcps_segstimed++;
498: }
499: }
500:
501: /*
502: * Set retransmit timer if not currently set,
503: * and not doing an ack or a keep-alive probe.
504: * Initial value for retransmit timer is smoothed
505: * round-trip time + 2 * round-trip time variance.
506: * Initialize shift counter which is used for backoff
507: * of retransmit time.
508: */
509: if (tp->t_timer[TCPT_REXMT] == 0 &&
510: tp->snd_nxt != tp->snd_una) {
511: tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
512: if (tp->t_timer[TCPT_PERSIST]) {
513: tp->t_timer[TCPT_PERSIST] = 0;
514: tp->t_rxtshift = 0;
515: }
516: }
517: } else
518: if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
519: tp->snd_max = tp->snd_nxt + len;
520:
521: /*
522: * Fill in IP length and desired time to live and
523: * send to IP level. There should be a better way
524: * to handle ttl and tos; we could keep them in
525: * the template, but need a way to checksum without them.
526: */
527: m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
528:
529: {
530:
1.1.1.2 ! root 531: ((struct ip *)ti)->ip_len = (u_int16_t) m->m_len;
1.1 root 532:
533: ((struct ip *)ti)->ip_ttl = ip_defttl;
534: ((struct ip *)ti)->ip_tos = so->so_iptos;
535:
536: /* #if BSD >= 43 */
537: /* Don't do IP options... */
538: /* error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
539: * so->so_options & SO_DONTROUTE, 0);
540: */
541: error = ip_output(so, m);
542:
543: /* #else
544: * error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
545: * so->so_options & SO_DONTROUTE);
546: * #endif
547: */
548: }
549: if (error) {
550: out:
551: /* if (error == ENOBUFS) {
552: * tcp_quench(tp->t_inpcb, 0);
553: * return (0);
554: * }
555: */
556: /* if ((error == EHOSTUNREACH || error == ENETDOWN)
557: * && TCPS_HAVERCVDSYN(tp->t_state)) {
558: * tp->t_softerror = error;
559: * return (0);
560: * }
561: */
562: return (error);
563: }
564: tcpstat.tcps_sndtotal++;
565:
566: /*
567: * Data sent (as far as we can tell).
568: * If this advertises a larger window than any other segment,
569: * then remember the size of the advertised window.
570: * Any pending ACK has now been sent.
571: */
572: if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
573: tp->rcv_adv = tp->rcv_nxt + win;
574: tp->last_ack_sent = tp->rcv_nxt;
575: tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
576: if (sendalot)
577: goto again;
578:
579: return (0);
580: }
581:
1.1.1.2 ! root 582: void tcp_setpersist(register struct tcpcb *tp)
1.1 root 583: {
584: int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
585:
586: /* if (tp->t_timer[TCPT_REXMT])
587: * panic("tcp_output REXMT");
588: */
589: /*
590: * Start/restart persistence timer.
591: */
592: TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
593: t * tcp_backoff[tp->t_rxtshift],
594: TCPTV_PERSMIN, TCPTV_PERSMAX);
595: if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
596: tp->t_rxtshift++;
597: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.