numerous fixes
[unix-history] / usr / src / sys / netinet / tcp_output.c
CommitLineData
be43ac7f 1/* tcp_output.c 4.23 81/12/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"
0974b45c
BJ
9#include "../net/in.h"
10#include "../net/in_pcb.h"
11#include "../net/in_systm.h"
d52566dd 12#include "../net/ip.h"
eb44bfb2 13#include "../net/ip_var.h"
d52566dd 14#include "../net/tcp.h"
0974b45c 15#define TCPOUTFLAGS
d52566dd 16#include "../net/tcp_fsm.h"
0974b45c
BJ
17#include "../net/tcp_seq.h"
18#include "../net/tcp_timer.h"
19#include "../net/tcp_var.h"
20#include "../net/tcpip.h"
f1b2fa5b 21#include "../errno.h"
76ee76df 22
4aed14e3 23char *tcpstates[]; /* XXX */
ea727f86 24/*
4aed14e3 25 * Tcp output routine: figure out what should be sent and send it.
ea727f86 26 */
a6503abf 27tcp_output(tp)
53a5409e 28 register struct tcpcb *tp;
ea727f86 29{
53a5409e 30 register struct socket *so = tp->t_inpcb->inp_socket;
a6503abf
BJ
31 register int len;
32 struct mbuf *m0;
33 int off, flags;
34 register struct mbuf *m;
35 register struct tcpiphdr *ti;
36 int win;
76ee76df 37
a6503abf 38COUNT(TCP_OUTPUT);
76ee76df 39
a6503abf 40 /*
0974b45c
BJ
41 * Determine length of data that can be transmitted,
42 * and flags that will be used.
43 * If there is some data or critical controls (SYN, RST)
44 * to send, then transmit; otherwise, investigate further.
a6503abf
BJ
45 */
46 off = tp->snd_nxt - tp->snd_una;
405c9168 47 len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
0974b45c
BJ
48 if (len > tp->t_maxseg)
49 len = tp->t_maxseg;
4aed14e3
BJ
50 if (len < 0)
51 len = 0; /* FIN can cause -1 */
0974b45c 52 flags = tcp_outflags[tp->t_state];
405c9168
BJ
53 if (len < so->so_snd.sb_cc)
54 flags &= ~TH_FIN;
0974b45c 55 if (len || (flags & (TH_SYN|TH_RST)))
a6503abf
BJ
56 goto send;
57
58 /*
0974b45c 59 * See if we owe peer an ACK or have a unacked FIN to send.
a6503abf 60 */
0974b45c 61 if (tp->t_flags & TF_ACKNOW)
a6503abf 62 goto send;
0974b45c
BJ
63 if ((so->so_state & SS_CANTSENDMORE) &&
64 TCPS_OURFINNOTACKED(tp->t_state))
a6503abf 65 goto send;
76ee76df 66
a6503abf
BJ
67 /*
68 * Calculate available window in i, and also amount
69 * of window known to peer (as advertised window less
70 * next expected input.) If this is 35% or more of the
71 * maximum possible window, then want to send a segment to peer.
72 */
0974b45c
BJ
73 win = sbspace(&so->so_rcv);
74 if (win > 0 &&
75 ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
a6503abf
BJ
76 goto send;
77
78 /*
79 * No reason to send a segment, just return.
80 */
f1b2fa5b 81 return (0);
a6503abf
BJ
82
83send:
84 /*
85 * Grab a header mbuf, attaching a copy of data to
86 * be transmitted, and initialize the header from
87 * the template for sends on this connection.
88 */
76ee76df
BJ
89 MGET(m, 0);
90 if (m == 0)
91 return (0);
4aed14e3 92 m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
53a5409e 93 m->m_len = sizeof (struct tcpiphdr);
a6503abf
BJ
94 if (len) {
95 m->m_next = m_copy(so->so_snd.sb_mb, off, len);
96 if (m->m_next == 0)
97 len = 0;
98 }
99 ti = mtod(m, struct tcpiphdr *);
100 if (tp->t_template == 0)
101 panic("tcp_output");
f1b2fa5b 102 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
a6503abf
BJ
103
104 /*
105 * Fill in fields, remembering maximum advertised
106 * window for use in delaying messages about window sizes.
107 */
4aed14e3
BJ
108 ti->ti_seq = tp->snd_nxt;
109 ti->ti_ack = tp->rcv_nxt;
110#if vax
111 ti->ti_seq = htonl(ti->ti_seq);
112 ti->ti_ack = htonl(ti->ti_ack);
113#endif
0974b45c 114 if (tp->t_tcpopt) {
f1b2fa5b 115 m0 = m->m_next;
0974b45c
BJ
116 m->m_next = m_get(0);
117 if (m->m_next == 0) {
118 (void) m_free(m);
f1b2fa5b 119 m_freem(m);
0974b45c
BJ
120 return (0);
121 }
122 m->m_next->m_next = m0;
123 m->m_off = MMINOFF;
124 m->m_len = tp->t_tcpopt->m_len;
125 bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
f1b2fa5b 126 (unsigned)tp->t_tcpopt->m_len);
0974b45c
BJ
127 ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
128 }
129 ti->ti_flags = flags;
a6503abf
BJ
130 win = sbspace(&so->so_rcv);
131 if (win > 0)
f1b2fa5b 132 ti->ti_win = htons((u_short)win);
0974b45c
BJ
133 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
134 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
a6503abf
BJ
135 ti->ti_flags |= TH_URG;
136 } else
137 /*
138 * If no urgent pointer to send, then we pull
139 * the urgent pointer to the left edge of the send window
140 * so that it doesn't drift into the send window on sequence
141 * number wraparound.
142 */
0974b45c
BJ
143 tp->snd_up = tp->snd_una; /* drag it along */
144 /* PUSH */
a6503abf
BJ
145
146 /*
147 * Put TCP length in extended header, and then
148 * checksum extended header and data.
149 */
150 if (len)
151 ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr)));
0974b45c
BJ
152 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len);
153
154 /*
155 * Advance snd_nxt over sequence space of this segment
156 */
157 if (flags & (TH_SYN|TH_FIN))
4aed14e3 158 tp->snd_nxt++;
0974b45c
BJ
159 tp->snd_nxt += len;
160
161 /*
405c9168 162 * If this transmission closes the window,
4aed14e3
BJ
163 * start persistance timer at 2 round trip times
164 * but at least TCPTV_PERSMIN ticks.
0974b45c 165 */
4aed14e3
BJ
166 if (SEQ_GT(tp->snd_nxt, tp->snd_una+tp->snd_wnd) &&
167 tp->t_timer[TCPT_PERSIST] == 0)
168 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
169 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
405c9168
BJ
170
171 /*
172 * Time this transmission if not a retransmission and
173 * not currently timing anything.
174 */
175 if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
176 tp->t_rtt = 1;
177 tp->t_rtseq = tp->snd_nxt - len;
178 }
179
180 /*
181 * Set retransmit timer if not currently set.
4aed14e3 182 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
405c9168
BJ
183 * Initialize shift counter which is used for exponential
184 * backoff of retransmit time.
185 */
4aed14e3
BJ
186 if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) {
187 TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
188 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
405c9168 189 tp->t_rxtshift = 0;
0974b45c 190 }
a6503abf
BJ
191
192 /*
193 * Fill in IP length and desired time to live and
194 * send to IP level.
195 */
196 ((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
197 ((struct ip *)ti)->ip_ttl = TCP_TTL;
be43ac7f 198 if (ip_output(m, tp->t_ipopt) == 0)
0974b45c 199 return (0);
a6503abf
BJ
200
201 /*
202 * Data sent (as far as we can tell).
203 * If this advertises a larger window than any other segment,
4aed14e3 204 * then remember the size of the advertised window.
0974b45c 205 * Drop send for purpose of ACK requirements.
a6503abf 206 */
be43ac7f 207 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
a6503abf 208 tp->rcv_adv = tp->rcv_nxt + win;
0974b45c 209 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
4aed14e3
BJ
210 if (SEQ_GT(tp->snd_nxt, tp->snd_max))
211 tp->snd_max = tp->snd_nxt;
0974b45c 212 return (1);
76ee76df 213}