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