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