1 | /* $Id: tcp_output.c 69498 2017-10-28 15:07:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - TCP output.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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 = 0;
|
---|
103 |
|
---|
104 | LogFlowFunc(("ENTER: tcp_output: tp = %R[tcpcb793]\n", tp));
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Determine length of data that should be transmitted,
|
---|
108 | * and flags that will be used.
|
---|
109 | * If there is some data or critical controls (SYN, RST)
|
---|
110 | * to send, then transmit; otherwise, investigate further.
|
---|
111 | */
|
---|
112 | idle = (tp->snd_max == tp->snd_una);
|
---|
113 | if (idle && tp->t_idle >= tp->t_rxtcur)
|
---|
114 | /*
|
---|
115 | * We have been idle for "a while" and no acks are
|
---|
116 | * expected to clock out any data we send --
|
---|
117 | * slow start to get ack "clock" running again.
|
---|
118 | */
|
---|
119 | tp->snd_cwnd = tp->t_maxseg;
|
---|
120 |
|
---|
121 | again:
|
---|
122 | sendalot = 0;
|
---|
123 | off = tp->snd_nxt - tp->snd_una;
|
---|
124 | win = min(tp->snd_wnd, tp->snd_cwnd);
|
---|
125 |
|
---|
126 | flags = tcp_outflags[tp->t_state];
|
---|
127 |
|
---|
128 | Log2((" --- tcp_output flags = 0x%x\n", flags));
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * If in persist timeout with window of 0, send 1 byte.
|
---|
132 | * Otherwise, if window is small but nonzero
|
---|
133 | * and timer expired, we will send what we can
|
---|
134 | * and go to transmit state.
|
---|
135 | */
|
---|
136 | if (tp->t_force)
|
---|
137 | {
|
---|
138 | if (win == 0)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * If we still have some data to send, then
|
---|
142 | * clear the FIN bit. Usually this would
|
---|
143 | * happen below when it realizes that we
|
---|
144 | * aren't sending all the data. However,
|
---|
145 | * if we have exactly 1 byte of unset data,
|
---|
146 | * then it won't clear the FIN bit below,
|
---|
147 | * and if we are in persist state, we wind
|
---|
148 | * up sending the packet without recording
|
---|
149 | * that we sent the FIN bit.
|
---|
150 | *
|
---|
151 | * We can't just blindly clear the FIN bit,
|
---|
152 | * because if we don't have any more data
|
---|
153 | * to send then the probe will be the FIN
|
---|
154 | * itself.
|
---|
155 | */
|
---|
156 | if (off < SBUF_LEN(&so->so_snd))
|
---|
157 | flags &= ~TH_FIN;
|
---|
158 | win = 1;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | tp->t_timer[TCPT_PERSIST] = 0;
|
---|
163 | tp->t_rxtshift = 0;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | len = min(SBUF_LEN(&so->so_snd), win) - off;
|
---|
168 | if (len < 0)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * If FIN has been sent but not acked,
|
---|
172 | * but we haven't been called to retransmit,
|
---|
173 | * len will be -1. Otherwise, window shrank
|
---|
174 | * after we sent into it. If window shrank to 0,
|
---|
175 | * cancel pending retransmit and pull snd_nxt
|
---|
176 | * back to (closed) window. We will enter persist
|
---|
177 | * state below. If the window didn't close completely,
|
---|
178 | * just wait for an ACK.
|
---|
179 | */
|
---|
180 | len = 0;
|
---|
181 | if (win == 0)
|
---|
182 | {
|
---|
183 | tp->t_timer[TCPT_REXMT] = 0;
|
---|
184 | tp->snd_nxt = tp->snd_una;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | if (len > tp->t_maxseg)
|
---|
188 | {
|
---|
189 | len = tp->t_maxseg;
|
---|
190 | sendalot = 1;
|
---|
191 | }
|
---|
192 | if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + SBUF_LEN(&so->so_snd)))
|
---|
193 | flags &= ~TH_FIN;
|
---|
194 |
|
---|
195 | win = sbspace(&so->so_rcv);
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Sender silly window avoidance. If connection is idle
|
---|
199 | * and can send all data, a maximum segment,
|
---|
200 | * at least a maximum default-size segment do it,
|
---|
201 | * or are forced, do it; otherwise don't bother.
|
---|
202 | * If peer's buffer is tiny, then send
|
---|
203 | * when window is at least half open.
|
---|
204 | * If retransmitting (possibly after persist timer forced us
|
---|
205 | * to send into a small window), then must resend.
|
---|
206 | */
|
---|
207 | if (len)
|
---|
208 | {
|
---|
209 | if (len == tp->t_maxseg)
|
---|
210 | goto send;
|
---|
211 | if ((1 || idle || tp->t_flags & TF_NODELAY) &&
|
---|
212 | len + off >= SBUF_LEN(&so->so_snd))
|
---|
213 | goto send;
|
---|
214 | if (tp->t_force)
|
---|
215 | goto send;
|
---|
216 | if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
|
---|
217 | goto send;
|
---|
218 | if (SEQ_LT(tp->snd_nxt, tp->snd_max))
|
---|
219 | goto send;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Compare available window to amount of window
|
---|
224 | * known to peer (as advertised window less
|
---|
225 | * next expected input). If the difference is at least two
|
---|
226 | * max size segments, or at least 50% of the maximum possible
|
---|
227 | * window, then want to send a window update to peer.
|
---|
228 | */
|
---|
229 | if (win > 0)
|
---|
230 | {
|
---|
231 | /*
|
---|
232 | * "adv" is the amount we can increase the window,
|
---|
233 | * taking into account that we are limited by
|
---|
234 | * TCP_MAXWIN << tp->rcv_scale.
|
---|
235 | */
|
---|
236 | long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale);
|
---|
237 | if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
|
---|
238 | adv -= tp->rcv_adv - tp->rcv_nxt;
|
---|
239 |
|
---|
240 | if (adv >= (long) (2 * tp->t_maxseg))
|
---|
241 | goto send;
|
---|
242 | if (2 * adv >= (long) SBUF_SIZE(&so->so_rcv))
|
---|
243 | goto send;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Send if we owe peer an ACK.
|
---|
248 | */
|
---|
249 | if (tp->t_flags & TF_ACKNOW)
|
---|
250 | goto send;
|
---|
251 | if (flags & (TH_SYN|TH_RST))
|
---|
252 | goto send;
|
---|
253 | if (SEQ_GT(tp->snd_up, tp->snd_una))
|
---|
254 | goto send;
|
---|
255 | /*
|
---|
256 | * If our state indicates that FIN should be sent
|
---|
257 | * and we have not yet done so, or we're retransmitting the FIN,
|
---|
258 | * then we need to send.
|
---|
259 | */
|
---|
260 | if ( flags & TH_FIN
|
---|
261 | && ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
|
---|
262 | goto send;
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * TCP window updates are not reliable, rather a polling protocol
|
---|
266 | * using ``persist'' packets is used to insure receipt of window
|
---|
267 | * updates. The three ``states'' for the output side are:
|
---|
268 | * idle not doing retransmits or persists
|
---|
269 | * persisting to move a small or zero window
|
---|
270 | * (re)transmitting and thereby not persisting
|
---|
271 | *
|
---|
272 | * tp->t_timer[TCPT_PERSIST]
|
---|
273 | * is set when we are in persist state.
|
---|
274 | * tp->t_force
|
---|
275 | * is set when we are called to send a persist packet.
|
---|
276 | * tp->t_timer[TCPT_REXMT]
|
---|
277 | * is set when we are retransmitting
|
---|
278 | * The output side is idle when both timers are zero.
|
---|
279 | *
|
---|
280 | * If send window is too small, there is data to transmit, and no
|
---|
281 | * retransmit or persist is pending, then go to persist state.
|
---|
282 | * If nothing happens soon, send when timer expires:
|
---|
283 | * if window is nonzero, transmit what we can,
|
---|
284 | * otherwise force out a byte.
|
---|
285 | */
|
---|
286 | if ( SBUF_LEN(&so->so_snd)
|
---|
287 | && tp->t_timer[TCPT_REXMT] == 0
|
---|
288 | && tp->t_timer[TCPT_PERSIST] == 0)
|
---|
289 | {
|
---|
290 | tp->t_rxtshift = 0;
|
---|
291 | tcp_setpersist(tp);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * No reason to send a segment, just return.
|
---|
296 | */
|
---|
297 | tcpstat.tcps_didnuttin++;
|
---|
298 |
|
---|
299 | LogFlowFuncLeave();
|
---|
300 | return (0);
|
---|
301 |
|
---|
302 | send:
|
---|
303 | LogFlowFunc(("send\n"));
|
---|
304 | /*
|
---|
305 | * Before ESTABLISHED, force sending of initial options
|
---|
306 | * unless TCP set not to do any options.
|
---|
307 | * NOTE: we assume that the IP/TCP header plus TCP options
|
---|
308 | * always fit in a single mbuf, leaving room for a maximum
|
---|
309 | * link header, i.e.
|
---|
310 | * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
|
---|
311 | */
|
---|
312 | optlen = 0;
|
---|
313 | hdrlen = sizeof (struct tcpiphdr);
|
---|
314 | if (flags & TH_SYN)
|
---|
315 | {
|
---|
316 | tp->snd_nxt = tp->iss;
|
---|
317 | if ((tp->t_flags & TF_NOOPT) == 0)
|
---|
318 | {
|
---|
319 | u_int16_t mss;
|
---|
320 |
|
---|
321 | opt[0] = TCPOPT_MAXSEG;
|
---|
322 | opt[1] = 4;
|
---|
323 | mss = RT_H2N_U16((u_int16_t) tcp_mss(pData, tp, 0));
|
---|
324 | memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
|
---|
325 | optlen = 4;
|
---|
326 |
|
---|
327 | #if 0
|
---|
328 | if ( (tp->t_flags & TF_REQ_SCALE)
|
---|
329 | && ( (flags & TH_ACK) == 0
|
---|
330 | || (tp->t_flags & TF_RCVD_SCALE)))
|
---|
331 | {
|
---|
332 | *((u_int32_t *) (opt + optlen)) = RT_H2N_U32( TCPOPT_NOP << 24
|
---|
333 | | TCPOPT_WINDOW << 16
|
---|
334 | | TCPOLEN_WINDOW << 8
|
---|
335 | | tp->request_r_scale);
|
---|
336 | optlen += 4;
|
---|
337 | }
|
---|
338 | #endif
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Send a timestamp and echo-reply if this is a SYN and our side
|
---|
344 | * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
|
---|
345 | * and our peer have sent timestamps in our SYN's.
|
---|
346 | */
|
---|
347 | #if 0
|
---|
348 | if ( (tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP
|
---|
349 | && (flags & TH_RST) == 0
|
---|
350 | && ( (flags & (TH_SYN|TH_ACK)) == TH_SYN
|
---|
351 | || (tp->t_flags & TF_RCVD_TSTMP)))
|
---|
352 | {
|
---|
353 | u_int32_t *lp = (u_int32_t *)(opt + optlen);
|
---|
354 |
|
---|
355 | /* Form timestamp option as shown in appendix A of RFC 1323. */
|
---|
356 | *lp++ = RT_H2N_U32_C(TCPOPT_TSTAMP_HDR);
|
---|
357 | *lp++ = RT_H2N_U32(tcp_now);
|
---|
358 | *lp = RT_H2N_U32(tp->ts_recent);
|
---|
359 | optlen += TCPOLEN_TSTAMP_APPA;
|
---|
360 | }
|
---|
361 | #endif
|
---|
362 | hdrlen += optlen;
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Adjust data length if insertion of options will
|
---|
366 | * bump the packet length beyond the t_maxseg length.
|
---|
367 | */
|
---|
368 | if (len > tp->t_maxseg - optlen)
|
---|
369 | {
|
---|
370 | len = tp->t_maxseg - optlen;
|
---|
371 | sendalot = 1;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /*
|
---|
375 | * Grab a header mbuf, attaching a copy of data to
|
---|
376 | * be transmitted, and initialize the header from
|
---|
377 | * the template for sends on this connection.
|
---|
378 | */
|
---|
379 | if (len)
|
---|
380 | {
|
---|
381 | if (tp->t_force && len == 1)
|
---|
382 | tcpstat.tcps_sndprobe++;
|
---|
383 | else if (SEQ_LT(tp->snd_nxt, tp->snd_max))
|
---|
384 | {
|
---|
385 | tcpstat.tcps_sndrexmitpack++;
|
---|
386 | tcpstat.tcps_sndrexmitbyte += len;
|
---|
387 | }
|
---|
388 | else
|
---|
389 | {
|
---|
390 | tcpstat.tcps_sndpack++;
|
---|
391 | tcpstat.tcps_sndbyte += len;
|
---|
392 | }
|
---|
393 |
|
---|
394 | size = MCLBYTES;
|
---|
395 | if ((len + hdrlen + ETH_HLEN) < MSIZE)
|
---|
396 | size = MCLBYTES;
|
---|
397 | else if ((len + hdrlen + ETH_HLEN) < MCLBYTES)
|
---|
398 | size = MCLBYTES;
|
---|
399 | else if((len + hdrlen + ETH_HLEN) < MJUM9BYTES)
|
---|
400 | size = MJUM9BYTES;
|
---|
401 | else if ((len + hdrlen + ETH_HLEN) < MJUM16BYTES)
|
---|
402 | size = MJUM16BYTES;
|
---|
403 | else
|
---|
404 | AssertMsgFailed(("Unsupported size"));
|
---|
405 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
|
---|
406 | if (m == NULL)
|
---|
407 | {
|
---|
408 | /* error = ENOBUFS; */
|
---|
409 | error = 1;
|
---|
410 | goto out;
|
---|
411 | }
|
---|
412 | m->m_data += if_maxlinkhdr;
|
---|
413 | m->m_pkthdr.header = mtod(m, void *);
|
---|
414 | m->m_len = hdrlen;
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * This will always succeed, since we make sure our mbufs
|
---|
418 | * are big enough to hold one MSS packet + header + ... etc.
|
---|
419 | */
|
---|
420 | #if 0
|
---|
421 | if (len <= MHLEN - hdrlen - max_linkhdr)
|
---|
422 | {
|
---|
423 | #endif
|
---|
424 | sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
|
---|
425 | m->m_len += len;
|
---|
426 | #if 0
|
---|
427 | }
|
---|
428 | else
|
---|
429 | {
|
---|
430 | m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
|
---|
431 | if (m->m_next == 0)
|
---|
432 | len = 0;
|
---|
433 | }
|
---|
434 | #endif
|
---|
435 | /*
|
---|
436 | * If we're sending everything we've got, set PUSH.
|
---|
437 | * (This will keep happy those implementations which only
|
---|
438 | * give data to the user when a buffer fills or
|
---|
439 | * a PUSH comes in.)
|
---|
440 | */
|
---|
441 | if (off + len == (ssize_t)SBUF_LEN(&so->so_snd))
|
---|
442 | flags |= TH_PUSH;
|
---|
443 | }
|
---|
444 | else
|
---|
445 | {
|
---|
446 | bool fUninitializedTemplate = false;
|
---|
447 | if (tp->t_flags & TF_ACKNOW)
|
---|
448 | tcpstat.tcps_sndacks++;
|
---|
449 | else if (flags & (TH_SYN|TH_FIN|TH_RST))
|
---|
450 | tcpstat.tcps_sndctrl++;
|
---|
451 | else if (SEQ_GT(tp->snd_up, tp->snd_una))
|
---|
452 | tcpstat.tcps_sndurg++;
|
---|
453 | else
|
---|
454 | tcpstat.tcps_sndwinup++;
|
---|
455 |
|
---|
456 | if ((hdrlen + ETH_HLEN) < MSIZE)
|
---|
457 | {
|
---|
458 | size = MCLBYTES;
|
---|
459 | }
|
---|
460 | else if ((hdrlen + ETH_HLEN) < MCLBYTES)
|
---|
461 | {
|
---|
462 | size = MCLBYTES;
|
---|
463 | }
|
---|
464 | else if((hdrlen + ETH_HLEN) < MJUM9BYTES)
|
---|
465 | {
|
---|
466 | size = MJUM9BYTES;
|
---|
467 | }
|
---|
468 | else if ((hdrlen + ETH_HLEN) < MJUM16BYTES)
|
---|
469 | {
|
---|
470 | size = MJUM16BYTES;
|
---|
471 | }
|
---|
472 | else
|
---|
473 | {
|
---|
474 | AssertMsgFailed(("Unsupported size"));
|
---|
475 | }
|
---|
476 | m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
|
---|
477 | if (m == NULL)
|
---|
478 | {
|
---|
479 | /* error = ENOBUFS; */
|
---|
480 | error = 1;
|
---|
481 | goto out;
|
---|
482 | }
|
---|
483 | m->m_data += if_maxlinkhdr;
|
---|
484 | m->m_pkthdr.header = mtod(m, void *);
|
---|
485 | m->m_len = hdrlen;
|
---|
486 | /*
|
---|
487 | * Uninitialized TCP template looks very suspicious at this processing state, thus why we have
|
---|
488 | * to workaround the problem till right fix. Warning appears once at release log.
|
---|
489 | */
|
---|
490 | fUninitializedTemplate = RT_BOOL(( tp->t_template.ti_src.s_addr == INADDR_ANY
|
---|
491 | || tp->t_template.ti_dst.s_addr == INADDR_ANY));
|
---|
492 | #ifndef DEBUG_vvl
|
---|
493 | if (fUninitializedTemplate)
|
---|
494 | {
|
---|
495 | static bool fWarn;
|
---|
496 | tcp_template(tp);
|
---|
497 | if(!fWarn)
|
---|
498 | {
|
---|
499 | LogRel(("NAT: TCP: TCP template was created forcely from socket information\n"));
|
---|
500 | fWarn = true;
|
---|
501 | }
|
---|
502 | }
|
---|
503 | #else
|
---|
504 | Assert((!fUninitializedTemplate));
|
---|
505 | #endif
|
---|
506 | }
|
---|
507 |
|
---|
508 | ti = mtod(m, struct tcpiphdr *);
|
---|
509 |
|
---|
510 | memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Fill in fields, remembering maximum advertised
|
---|
514 | * window for use in delaying messages about window sizes.
|
---|
515 | * If resending a FIN, be sure not to use a new sequence number.
|
---|
516 | */
|
---|
517 | if ( flags & TH_FIN
|
---|
518 | && tp->t_flags & TF_SENTFIN
|
---|
519 | && tp->snd_nxt == tp->snd_max)
|
---|
520 | tp->snd_nxt--;
|
---|
521 | /*
|
---|
522 | * If we are doing retransmissions, then snd_nxt will
|
---|
523 | * not reflect the first unsent octet. For ACK only
|
---|
524 | * packets, we do not want the sequence number of the
|
---|
525 | * retransmitted packet, we want the sequence number
|
---|
526 | * of the next unsent octet. So, if there is no data
|
---|
527 | * (and no SYN or FIN), use snd_max instead of snd_nxt
|
---|
528 | * when filling in ti_seq. But if we are in persist
|
---|
529 | * state, snd_max might reflect one byte beyond the
|
---|
530 | * right edge of the window, so use snd_nxt in that
|
---|
531 | * case, since we know we aren't doing a retransmission.
|
---|
532 | * (retransmit and persist are mutually exclusive...)
|
---|
533 | */
|
---|
534 | if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
|
---|
535 | ti->ti_seq = RT_H2N_U32(tp->snd_nxt);
|
---|
536 | else
|
---|
537 | ti->ti_seq = RT_H2N_U32(tp->snd_max);
|
---|
538 | ti->ti_ack = RT_H2N_U32(tp->rcv_nxt);
|
---|
539 | if (optlen)
|
---|
540 | {
|
---|
541 | memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
|
---|
542 | ti->ti_off = (uint8_t)((sizeof (struct tcphdr) + optlen) >> 2);
|
---|
543 | }
|
---|
544 | ti->ti_flags = flags;
|
---|
545 | /*
|
---|
546 | * Calculate receive window. Don't shrink window,
|
---|
547 | * but avoid silly window syndrome.
|
---|
548 | */
|
---|
549 | if (win < (long)(SBUF_SIZE(&so->so_rcv) / 4) && win < (long)tp->t_maxseg)
|
---|
550 | win = 0;
|
---|
551 | if (win > (long)TCP_MAXWIN << tp->rcv_scale)
|
---|
552 | win = (long)TCP_MAXWIN << tp->rcv_scale;
|
---|
553 | if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
|
---|
554 | win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
|
---|
555 | ti->ti_win = RT_H2N_U16((u_int16_t) (win>>tp->rcv_scale));
|
---|
556 |
|
---|
557 | #if 0
|
---|
558 | if (SEQ_GT(tp->snd_up, tp->snd_nxt))
|
---|
559 | {
|
---|
560 | ti->ti_urp = RT_H2N_U16((u_int16_t)(tp->snd_up - tp->snd_nxt));
|
---|
561 | #else
|
---|
562 | if (SEQ_GT(tp->snd_up, tp->snd_una))
|
---|
563 | {
|
---|
564 | ti->ti_urp = RT_H2N_U16((u_int16_t)(tp->snd_up - RT_N2H_U32(ti->ti_seq)));
|
---|
565 | #endif
|
---|
566 | ti->ti_flags |= TH_URG;
|
---|
567 | }
|
---|
568 | else
|
---|
569 | /*
|
---|
570 | * If no urgent pointer to send, then we pull
|
---|
571 | * the urgent pointer to the left edge of the send window
|
---|
572 | * so that it doesn't drift into the send window on sequence
|
---|
573 | * number wraparound.
|
---|
574 | */
|
---|
575 | tp->snd_up = tp->snd_una; /* drag it along */
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Put TCP length in extended header, and then
|
---|
579 | * checksum extended header and data.
|
---|
580 | */
|
---|
581 | if (len + optlen)
|
---|
582 | ti->ti_len = RT_H2N_U16((u_int16_t)(sizeof (struct tcphdr)
|
---|
583 | + optlen + len));
|
---|
584 | ti->ti_sum = cksum(m, (int)(hdrlen + len));
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * In transmit state, time the transmission and arrange for
|
---|
588 | * the retransmit. In persist state, just set snd_max.
|
---|
589 | */
|
---|
590 | if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0)
|
---|
591 | {
|
---|
592 | tcp_seq startseq = tp->snd_nxt;
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * Advance snd_nxt over sequence space of this segment.
|
---|
596 | */
|
---|
597 | if (flags & (TH_SYN|TH_FIN))
|
---|
598 | {
|
---|
599 | if (flags & TH_SYN)
|
---|
600 | tp->snd_nxt++;
|
---|
601 | if (flags & TH_FIN)
|
---|
602 | {
|
---|
603 | tp->snd_nxt++;
|
---|
604 | tp->t_flags |= TF_SENTFIN;
|
---|
605 | }
|
---|
606 | }
|
---|
607 | tp->snd_nxt += len;
|
---|
608 | if (SEQ_GT(tp->snd_nxt, tp->snd_max))
|
---|
609 | {
|
---|
610 | tp->snd_max = tp->snd_nxt;
|
---|
611 | /*
|
---|
612 | * Time this transmission if not a retransmission and
|
---|
613 | * not currently timing anything.
|
---|
614 | */
|
---|
615 | if (tp->t_rtt == 0)
|
---|
616 | {
|
---|
617 | tp->t_rtt = 1;
|
---|
618 | tp->t_rtseq = startseq;
|
---|
619 | tcpstat.tcps_segstimed++;
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | /*
|
---|
624 | * Set retransmit timer if not currently set,
|
---|
625 | * and not doing an ack or a keep-alive probe.
|
---|
626 | * Initial value for retransmit timer is smoothed
|
---|
627 | * round-trip time + 2 * round-trip time variance.
|
---|
628 | * Initialize shift counter which is used for backoff
|
---|
629 | * of retransmit time.
|
---|
630 | */
|
---|
631 | if ( tp->t_timer[TCPT_REXMT] == 0
|
---|
632 | && tp->snd_nxt != tp->snd_una)
|
---|
633 | {
|
---|
634 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
|
---|
635 | if (tp->t_timer[TCPT_PERSIST])
|
---|
636 | {
|
---|
637 | tp->t_timer[TCPT_PERSIST] = 0;
|
---|
638 | tp->t_rxtshift = 0;
|
---|
639 | }
|
---|
640 | }
|
---|
641 | }
|
---|
642 | else
|
---|
643 | if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
|
---|
644 | tp->snd_max = tp->snd_nxt + len;
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Fill in IP length and desired time to live and
|
---|
648 | * send to IP level. There should be a better way
|
---|
649 | * to handle ttl and tos; we could keep them in
|
---|
650 | * the template, but need a way to checksum without them.
|
---|
651 | */
|
---|
652 | M_ASSERTPKTHDR(m);
|
---|
653 | m->m_pkthdr.header = mtod(m, void *);
|
---|
654 | m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
|
---|
655 |
|
---|
656 | {
|
---|
657 | ((struct ip *)ti)->ip_len = m->m_len;
|
---|
658 | ((struct ip *)ti)->ip_ttl = ip_defttl;
|
---|
659 | ((struct ip *)ti)->ip_tos = so->so_iptos;
|
---|
660 |
|
---|
661 | /* #if BSD >= 43 */
|
---|
662 | /* Don't do IP options... */
|
---|
663 | #if 0
|
---|
664 | error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
|
---|
665 | so->so_options & SO_DONTROUTE, 0);
|
---|
666 | #endif
|
---|
667 | error = ip_output(pData, so, m);
|
---|
668 |
|
---|
669 | #if 0
|
---|
670 | /* #else */
|
---|
671 | error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
|
---|
672 | so->so_options & SO_DONTROUTE);
|
---|
673 | /* #endif */
|
---|
674 | #endif
|
---|
675 | }
|
---|
676 | if (error)
|
---|
677 | {
|
---|
678 | out:
|
---|
679 | #if 0
|
---|
680 | if (error == ENOBUFS)
|
---|
681 | {
|
---|
682 | tcp_quench(tp->t_inpcb, 0);
|
---|
683 | return (0);
|
---|
684 | }
|
---|
685 |
|
---|
686 | if ( ( error == EHOSTUNREACH
|
---|
687 | || error == ENETDOWN)
|
---|
688 | && TCPS_HAVERCVDSYN(tp->t_state))
|
---|
689 | {
|
---|
690 | tp->t_softerror = error;
|
---|
691 | return (0);
|
---|
692 | }
|
---|
693 | #endif
|
---|
694 | if (m != NULL)
|
---|
695 | m_freem(pData, m);
|
---|
696 | return (error);
|
---|
697 | }
|
---|
698 | tcpstat.tcps_sndtotal++;
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Data sent (as far as we can tell).
|
---|
702 | * If this advertises a larger window than any other segment,
|
---|
703 | * then remember the size of the advertised window.
|
---|
704 | * Any pending ACK has now been sent.
|
---|
705 | */
|
---|
706 | if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
|
---|
707 | tp->rcv_adv = tp->rcv_nxt + win;
|
---|
708 | tp->last_ack_sent = tp->rcv_nxt;
|
---|
709 | tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
|
---|
710 | if (sendalot)
|
---|
711 | goto again;
|
---|
712 |
|
---|
713 | return (0);
|
---|
714 | }
|
---|
715 |
|
---|
716 | void
|
---|
717 | tcp_setpersist(struct tcpcb *tp)
|
---|
718 | {
|
---|
719 | int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
|
---|
720 |
|
---|
721 | #if 0
|
---|
722 | if (tp->t_timer[TCPT_REXMT])
|
---|
723 | panic("tcp_output REXMT");
|
---|
724 | #endif
|
---|
725 | /*
|
---|
726 | * Start/restart persistence timer.
|
---|
727 | */
|
---|
728 | TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
|
---|
729 | t * tcp_backoff[tp->t_rxtshift],
|
---|
730 | TCPTV_PERSMIN, TCPTV_PERSMAX);
|
---|
731 | if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
|
---|
732 | tp->t_rxtshift++;
|
---|
733 | }
|
---|