bounds checking
[unix-history] / usr / src / sys / netinet / tcp_output.c
CommitLineData
ee787340 1/* tcp_output.c 4.36 82/03/28 */
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"
f1dd32da 21#include "../net/tcp_debug.h"
ee787340 22#include "../net/route.h"
f1b2fa5b 23#include "../errno.h"
76ee76df 24
4aed14e3 25char *tcpstates[]; /* XXX */
8b5a83bb
BJ
26
27/*
28 * Initial options: indicate max segment length 1/2 of space
29 * allocated for receive; if TCPTRUEOOB is defined, indicate
30 * willingness to do true out-of-band.
31 */
32#ifndef TCPTRUEOOB
33u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
34#else
35u_char tcp_initopt[6] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, TCPOPT_WILLOOB, 2 };
36#endif
37
ea727f86 38/*
4aed14e3 39 * Tcp output routine: figure out what should be sent and send it.
ea727f86 40 */
a6503abf 41tcp_output(tp)
53a5409e 42 register struct tcpcb *tp;
ea727f86 43{
53a5409e 44 register struct socket *so = tp->t_inpcb->inp_socket;
a6503abf
BJ
45 register int len;
46 struct mbuf *m0;
fa627691 47 int off, flags, win;
a6503abf
BJ
48 register struct mbuf *m;
49 register struct tcpiphdr *ti;
8b5a83bb
BJ
50 u_char *opt;
51 unsigned optlen = 0;
76ee76df 52
a6503abf 53COUNT(TCP_OUTPUT);
76ee76df 54
a6503abf 55 /*
8ae6c089 56 * Determine length of data that should be transmitted,
0974b45c
BJ
57 * and flags that will be used.
58 * If there is some data or critical controls (SYN, RST)
59 * to send, then transmit; otherwise, investigate further.
a6503abf
BJ
60 */
61 off = tp->snd_nxt - tp->snd_una;
405c9168 62 len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
3232ef09 63 if (len < 0)
fa627691 64 return (0); /* past FIN */
0974b45c
BJ
65 if (len > tp->t_maxseg)
66 len = tp->t_maxseg;
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
96#ifdef TCPTRUEOOB
97 /*
98 * Send if an out of band data or ack should be transmitted.
99 */
100 if (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK)))
a6503abf 101 goto send;
8b5a83bb 102#endif
76ee76df 103
a6503abf
BJ
104 /*
105 * Calculate available window in i, and also amount
106 * of window known to peer (as advertised window less
107 * next expected input.) If this is 35% or more of the
108 * maximum possible window, then want to send a segment to peer.
109 */
0974b45c
BJ
110 win = sbspace(&so->so_rcv);
111 if (win > 0 &&
112 ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
a6503abf
BJ
113 goto send;
114
115 /*
116 * No reason to send a segment, just return.
117 */
f1b2fa5b 118 return (0);
a6503abf
BJ
119
120send:
121 /*
122 * Grab a header mbuf, attaching a copy of data to
123 * be transmitted, and initialize the header from
124 * the template for sends on this connection.
125 */
76ee76df
BJ
126 MGET(m, 0);
127 if (m == 0)
128 return (0);
4aed14e3 129 m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
53a5409e 130 m->m_len = sizeof (struct tcpiphdr);
a6503abf
BJ
131 if (len) {
132 m->m_next = m_copy(so->so_snd.sb_mb, off, len);
133 if (m->m_next == 0)
134 len = 0;
135 }
136 ti = mtod(m, struct tcpiphdr *);
137 if (tp->t_template == 0)
138 panic("tcp_output");
f1b2fa5b 139 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
a6503abf
BJ
140
141 /*
142 * Fill in fields, remembering maximum advertised
143 * window for use in delaying messages about window sizes.
144 */
4aed14e3
BJ
145 ti->ti_seq = tp->snd_nxt;
146 ti->ti_ack = tp->rcv_nxt;
147#if vax
148 ti->ti_seq = htonl(ti->ti_seq);
149 ti->ti_ack = htonl(ti->ti_ack);
150#endif
8b5a83bb
BJ
151 /*
152 * Before ESTABLISHED, force sending of initial options
153 * unless TCP set to not do any options.
154 */
155 if (tp->t_state < TCPS_ESTABLISHED) {
156 if (tp->t_flags&TF_NOOPT)
157 goto noopt;
158 opt = tcp_initopt;
159 optlen = sizeof (tcp_initopt);
160 *(u_short *)(opt + 2) = so->so_rcv.sb_hiwat / 2;
161#if vax
162 *(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
163#endif
164 } else {
165 if (tp->t_tcpopt == 0)
166 goto noopt;
167 opt = mtod(tp->t_tcpopt, u_char *);
168 optlen = tp->t_tcpopt->m_len;
169 }
170#ifndef TCPTRUEOOB
171 if (opt)
172#else
173 if (opt || (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK)))
174#endif
175 {
f1b2fa5b 176 m0 = m->m_next;
ef9b4258 177 m->m_next = m_get(M_DONTWAIT);
0974b45c
BJ
178 if (m->m_next == 0) {
179 (void) m_free(m);
8b5a83bb 180 m_freem(m0);
0974b45c
BJ
181 return (0);
182 }
183 m->m_next->m_next = m0;
8b5a83bb
BJ
184 m0 = m->m_next;
185 m0->m_off = MMINOFF;
186 m0->m_len = optlen;
668cc26d 187 bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
8b5a83bb
BJ
188 opt = (u_char *)(mtod(m0, caddr_t) + optlen);
189#ifdef TCPTRUEOOB
190 if (tp->t_oobflags&TCPOOB_OWEACK) {
191printf("tp %x send OOBACK for %x\n", tp->t_iobseq);
192 *opt++ = TCPOPT_OOBACK;
193 *opt++ = 3;
194 *opt++ = tp->t_iobseq;
195 m0->m_len += 3;
196 tp->t_oobflags &= ~TCPOOB_OWEACK;
197 /* sender should rexmt oob to force ack repeat */
198 }
199 if (tp->t_oobflags&TCPOOB_NEEDACK) {
200printf("tp %x send OOBDATA seq %x data %x\n", tp->t_oobseq, tp->t_oobc);
201 *opt++ = TCPOPT_OOBDATA;
b2db9217 202 *opt++ = 8;
8b5a83bb
BJ
203 *opt++ = tp->t_oobseq;
204 *opt++ = tp->t_oobc;
b2db9217
BJ
205 *(tcp_seq *)opt = tp->t_oobmark - tp->snd_nxt;
206#ifdef vax
207 *(tcp_seq *)opt = htonl((unsigned)*(tcp_seq *)opt);
208#endif
209 m0->m_len += 8;
8b5a83bb
BJ
210 TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT],
211 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
212 }
213#endif
214 while (m0->m_len & 0x3) {
215 *opt++ = TCPOPT_EOL;
216 m0->m_len++;
217 }
218 optlen = m0->m_len;
219 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
0974b45c 220 }
8b5a83bb 221noopt:
0974b45c 222 ti->ti_flags = flags;
a6503abf 223 win = sbspace(&so->so_rcv);
8ae6c089
BJ
224 if (win < so->so_rcv.sb_hiwat / 4) /* avoid silly window */
225 win = 0;
a6503abf 226 if (win > 0)
8ae6c089 227#if vax
f1b2fa5b 228 ti->ti_win = htons((u_short)win);
8ae6c089
BJ
229#else
230 ti->ti_win = win;
231#endif
0974b45c 232 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
5e74df82
BJ
233 ti->ti_urp = tp->snd_up - tp->snd_nxt;
234#if vax
235 ti->ti_urp = htons(ti->ti_urp);
236#endif
a6503abf
BJ
237 ti->ti_flags |= TH_URG;
238 } else
239 /*
240 * If no urgent pointer to send, then we pull
241 * the urgent pointer to the left edge of the send window
242 * so that it doesn't drift into the send window on sequence
243 * number wraparound.
244 */
0974b45c
BJ
245 tp->snd_up = tp->snd_una; /* drag it along */
246 /* PUSH */
a6503abf
BJ
247
248 /*
249 * Put TCP length in extended header, and then
250 * checksum extended header and data.
251 */
8b5a83bb
BJ
252 if (len + optlen) {
253 ti->ti_len = sizeof (struct tcphdr) + optlen + len;
254#if vax
255 ti->ti_len = htons((u_short)ti->ti_len);
256#endif
257 }
668cc26d 258 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
0974b45c
BJ
259
260 /*
261 * Advance snd_nxt over sequence space of this segment
262 */
263 if (flags & (TH_SYN|TH_FIN))
4aed14e3 264 tp->snd_nxt++;
0974b45c
BJ
265 tp->snd_nxt += len;
266
267 /*
405c9168 268 * If this transmission closes the window,
4aed14e3
BJ
269 * start persistance timer at 2 round trip times
270 * but at least TCPTV_PERSMIN ticks.
0974b45c 271 */
89413846 272 if (TCPS_HAVERCVDSYN(tp->t_state) &&
275e05b7 273 SEQ_GEQ(tp->snd_nxt, tp->snd_una+tp->snd_wnd) &&
4aed14e3
BJ
274 tp->t_timer[TCPT_PERSIST] == 0)
275 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
276 2 * tp->t_srtt, TCPTV_PERSMIN, TCPTV_MAX);
405c9168
BJ
277
278 /*
279 * Time this transmission if not a retransmission and
280 * not currently timing anything.
281 */
282 if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
283 tp->t_rtt = 1;
284 tp->t_rtseq = tp->snd_nxt - len;
285 }
286
287 /*
288 * Set retransmit timer if not currently set.
4aed14e3 289 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
405c9168
BJ
290 * Initialize shift counter which is used for exponential
291 * backoff of retransmit time.
292 */
4aed14e3
BJ
293 if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) {
294 TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
295 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
275e05b7 296 tp->t_rtt = 0;
405c9168 297 tp->t_rxtshift = 0;
0974b45c 298 }
a6503abf 299
f1dd32da
BJ
300 /*
301 * Trace.
302 */
303 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
304 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
305
a6503abf
BJ
306 /*
307 * Fill in IP length and desired time to live and
308 * send to IP level.
309 */
8b5a83bb 310 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
a6503abf 311 ((struct ip *)ti)->ip_ttl = TCP_TTL;
ee787340 312 if (ip_output(m, tp->t_ipopt, 0, 0) == 0)
0974b45c 313 return (0);
a6503abf
BJ
314
315 /*
316 * Data sent (as far as we can tell).
317 * If this advertises a larger window than any other segment,
4aed14e3 318 * then remember the size of the advertised window.
0974b45c 319 * Drop send for purpose of ACK requirements.
a6503abf 320 */
be43ac7f 321 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
a6503abf 322 tp->rcv_adv = tp->rcv_nxt + win;
0974b45c 323 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
4aed14e3
BJ
324 if (SEQ_GT(tp->snd_nxt, tp->snd_max))
325 tp->snd_max = tp->snd_nxt;
0974b45c 326 return (1);
76ee76df 327}