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