1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
30 * $FreeBSD: releng/5.3/sys/netinet/tcp_output.c 137082 2004-10-30 20:50:06Z rwatson $
31 */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
36 #include "opt_mac.h"
37 #include "opt_tcpdebug.h"
38 #include "opt_tcp_sack.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mac.h>
46 #include <sys/mbuf.h>
47 #include <sys/mutex.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52
53 #include <net/route.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip_var.h>
60 #ifdef INET6
61 #include <netinet6/in6_pcb.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #endif
65 #include <netinet/tcp.h>
66 #define TCPOUTFLAGS
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcpip.h>
72 #ifdef TCPDEBUG
73 #include <netinet/tcp_debug.h>
74 #endif
75
76 #ifdef IPSEC
77 #include <netinet6/ipsec.h>
78 #endif /*IPSEC*/
79
80 #ifdef FAST_IPSEC
81 #include <netipsec/ipsec.h>
82 #define IPSEC
83 #endif /*FAST_IPSEC*/
84
85 #include <machine/in_cksum.h>
86
87 #ifdef notyet
88 extern struct mbuf *m_copypack();
89 #endif
90
91 int path_mtu_discovery = 1;
92 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
93 &path_mtu_discovery, 1, "Enable Path MTU Discovery");
94
95 int ss_fltsz = 1;
96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
97 &ss_fltsz, 1, "Slow start flight size");
98
99 int ss_fltsz_local = 4;
100 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
101 &ss_fltsz_local, 1, "Slow start flight size for local networks");
102
103 int tcp_do_newreno = 1;
104 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
105 0, "Enable NewReno Algorithms");
106
107 /*
108 * Tcp output routine: figure out what should be sent and send it.
109 */
110 int
111 tcp_output(struct tcpcb *tp)
112 {
113 struct socket *so = tp->t_inpcb->inp_socket;
114 long len, recwin, sendwin;
115 int off, flags, error;
116 #ifdef TCP_SIGNATURE
117 int sigoff = 0;
118 #endif
119 struct mbuf *m;
120 struct ip *ip = NULL;
121 struct ipovly *ipov = NULL;
122 struct tcphdr *th;
123 u_char opt[TCP_MAXOLEN];
124 unsigned ipoptlen, optlen, hdrlen;
125 int idle, sendalot;
126 int i, sack_rxmit;
127 struct sackhole *p;
128 #if 0
129 int maxburst = TCP_MAXBURST;
130 #endif
131 struct rmxp_tao tao;
132 #ifdef INET6
133 struct ip6_hdr *ip6 = NULL;
134 int isipv6;
135
136 bzero(&tao, sizeof(tao));
137 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
138 #endif
139
140 INP_LOCK_ASSERT(tp->t_inpcb);
141
142 /*
143 * Determine length of data that should be transmitted,
144 * and flags that will be used.
145 * If there is some data or critical controls (SYN, RST)
146 * to send, then transmit; otherwise, investigate further.
147 */
148 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
149 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
150 /*
151 * We have been idle for "a while" and no acks are
152 * expected to clock out any data we send --
153 * slow start to get ack "clock" running again.
154 *
155 * Set the slow-start flight size depending on whether
156 * this is a local network or not.
157 */
158 int ss = ss_fltsz;
159 #ifdef INET6
160 if (isipv6) {
161 if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
162 ss = ss_fltsz_local;
163 } else
164 #endif /* INET6 */
165 if (in_localaddr(tp->t_inpcb->inp_faddr))
166 ss = ss_fltsz_local;
167 tp->snd_cwnd = tp->t_maxseg * ss;
168 }
169 tp->t_flags &= ~TF_LASTIDLE;
170 if (idle) {
171 if (tp->t_flags & TF_MORETOCOME) {
172 tp->t_flags |= TF_LASTIDLE;
173 idle = 0;
174 }
175 }
176 again:
177 /*
178 * If we've recently taken a timeout, snd_max will be greater than
179 * snd_nxt. There may be SACK information that allows us to avoid
180 * resending already delivered data. Adjust snd_nxt accordingly.
181 */
182 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
183 tcp_sack_adjust(tp);
184 sendalot = 0;
185 off = tp->snd_nxt - tp->snd_una;
186 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
187 sendwin = min(sendwin, tp->snd_bwnd);
188
189 flags = tcp_outflags[tp->t_state];
190 /*
191 * Send any SACK-generated retransmissions. If we're explicitly trying
192 * to send out new data (when sendalot is 1), bypass this function.
193 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
194 * we're replacing a (future) new transmission with a retransmission
195 * now, and we previously incremented snd_cwnd in tcp_input().
196 */
197 /*
198 * Still in sack recovery , reset rxmit flag to zero.
199 */
200 sack_rxmit = 0;
201 len = 0;
202 p = NULL;
203 if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
204 (p = tcp_sack_output(tp))) {
205 KASSERT(tp->snd_cwnd >= 0,
206 ("%s: CWIN is negative : %ld", __func__, tp->snd_cwnd));
207 /* Do not retransmit SACK segments beyond snd_recover */
208 if (SEQ_GT(p->end, tp->snd_recover)) {
209 /*
210 * (At least) part of sack hole extends beyond
211 * snd_recover. Check to see if we can rexmit data
212 * for this hole.
213 */
214 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
215 /*
216 * Can't rexmit any more data for this hole.
217 * That data will be rexmitted in the next
218 * sack recovery episode, when snd_recover
219 * moves past p->rxmit.
220 */
221 p = NULL;
222 goto after_sack_rexmit;
223 } else
224 /* Can rexmit part of the current hole */
225 len = ((long)ulmin(tp->snd_cwnd,
226 tp->snd_recover - p->rxmit));
227 } else
228 len = ((long)ulmin(tp->snd_cwnd, p->end - p->rxmit));
229 off = p->rxmit - tp->snd_una;
230 KASSERT(off >= 0,("%s: sack block to the left of una : %d",
231 __func__, off));
232 if (len > 0) {
233 sack_rxmit = 1;
234 sendalot = 1;
235 tcpstat.tcps_sack_rexmits++;
236 tcpstat.tcps_sack_rexmit_bytes +=
237 min(len, tp->t_maxseg);
238 }
239 }
240 after_sack_rexmit:
241 /*
242 * Get standard flags, and add SYN or FIN if requested by 'hidden'
243 * state flags.
244 */
245 if (tp->t_flags & TF_NEEDFIN)
246 flags |= TH_FIN;
247 if (tp->t_flags & TF_NEEDSYN)
248 flags |= TH_SYN;
249
250 SOCKBUF_LOCK(&so->so_snd);
251 /*
252 * If in persist timeout with window of 0, send 1 byte.
253 * Otherwise, if window is small but nonzero
254 * and timer expired, we will send what we can
255 * and go to transmit state.
256 */
257 if (tp->t_force) {
258 if (sendwin == 0) {
259 /*
260 * If we still have some data to send, then
261 * clear the FIN bit. Usually this would
262 * happen below when it realizes that we
263 * aren't sending all the data. However,
264 * if we have exactly 1 byte of unsent data,
265 * then it won't clear the FIN bit below,
266 * and if we are in persist state, we wind
267 * up sending the packet without recording
268 * that we sent the FIN bit.
269 *
270 * We can't just blindly clear the FIN bit,
271 * because if we don't have any more data
272 * to send then the probe will be the FIN
273 * itself.
274 */
275 if (off < so->so_snd.sb_cc)
276 flags &= ~TH_FIN;
277 sendwin = 1;
278 } else {
279 callout_stop(tp->tt_persist);
280 tp->t_rxtshift = 0;
281 }
282 }
283
284 /*
285 * If snd_nxt == snd_max and we have transmitted a FIN, the
286 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
287 * a negative length. This can also occur when TCP opens up
288 * its congestion window while receiving additional duplicate
289 * acks after fast-retransmit because TCP will reset snd_nxt
290 * to snd_max after the fast-retransmit.
291 *
292 * In the normal retransmit-FIN-only case, however, snd_nxt will
293 * be set to snd_una, the offset will be 0, and the length may
294 * wind up 0.
295 *
296 * If sack_rxmit is true we are retransmitting from the scoreboard
297 * in which case len is already set.
298 */
299 if (!sack_rxmit)
300 len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
301
302 /*
303 * Lop off SYN bit if it has already been sent. However, if this
304 * is SYN-SENT state and if segment contains data and if we don't
305 * know that foreign host supports TAO, suppress sending segment.
306 */
307 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
308 flags &= ~TH_SYN;
309 off--, len++;
310 if (tcp_do_rfc1644)
311 tcp_hc_gettao(&tp->t_inpcb->inp_inc, &tao);
312 if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
313 tao.tao_ccsent == 0)
314 goto just_return;
315 }
316
317 /*
318 * Be careful not to send data and/or FIN on SYN segments
319 * in cases when no CC option will be sent.
320 * This measure is needed to prevent interoperability problems
321 * with not fully conformant TCP implementations.
322 */
323 if ((flags & TH_SYN) &&
324 ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
325 ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
326 len = 0;
327 flags &= ~TH_FIN;
328 }
329
330 if (len < 0) {
331 /*
332 * If FIN has been sent but not acked,
333 * but we haven't been called to retransmit,
334 * len will be < 0. Otherwise, window shrank
335 * after we sent into it. If window shrank to 0,
336 * cancel pending retransmit, pull snd_nxt back
337 * to (closed) window, and set the persist timer
338 * if it isn't already going. If the window didn't
339 * close completely, just wait for an ACK.
340 */
341 len = 0;
342 if (sendwin == 0) {
343 callout_stop(tp->tt_rexmt);
344 tp->t_rxtshift = 0;
345 tp->snd_nxt = tp->snd_una;
346 if (!callout_active(tp->tt_persist))
347 tcp_setpersist(tp);
348 }
349 }
350
351 /*
352 * len will be >= 0 after this point. Truncate to the maximum
353 * segment length and ensure that FIN is removed if the length
354 * no longer contains the last data byte.
355 */
356 if (len > tp->t_maxseg) {
357 len = tp->t_maxseg;
358 sendalot = 1;
359 }
360 if (sack_rxmit) {
361 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
362 flags &= ~TH_FIN;
363 } else {
364 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
365 flags &= ~TH_FIN;
366 }
367
368 recwin = sbspace(&so->so_rcv);
369
370 /*
371 * Sender silly window avoidance. We transmit under the following
372 * conditions when len is non-zero:
373 *
374 * - We have a full segment
375 * - This is the last buffer in a write()/send() and we are
376 * either idle or running NODELAY
377 * - we've timed out (e.g. persist timer)
378 * - we have more then 1/2 the maximum send window's worth of
379 * data (receiver may be limited the window size)
380 * - we need to retransmit
381 */
382 if (len) {
383 if (len == tp->t_maxseg)
384 goto send;
385 /*
386 * NOTE! on localhost connections an 'ack' from the remote
387 * end may occur synchronously with the output and cause
388 * us to flush a buffer queued with moretocome. XXX
389 *
390 * note: the len + off check is almost certainly unnecessary.
391 */
392 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */
393 (idle || (tp->t_flags & TF_NODELAY)) &&
394 len + off >= so->so_snd.sb_cc &&
395 (tp->t_flags & TF_NOPUSH) == 0) {
396 goto send;
397 }
398 if (tp->t_force) /* typ. timeout case */
399 goto send;
400 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
401 goto send;
402 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */
403 goto send;
404 if (sack_rxmit)
405 goto send;
406 }
407
408 /*
409 * Compare available window to amount of window
410 * known to peer (as advertised window less
411 * next expected input). If the difference is at least two
412 * max size segments, or at least 50% of the maximum possible
413 * window, then want to send a window update to peer.
414 * Skip this if the connection is in T/TCP half-open state.
415 */
416 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
417 /*
418 * "adv" is the amount we can increase the window,
419 * taking into account that we are limited by
420 * TCP_MAXWIN << tp->rcv_scale.
421 */
422 long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
423 (tp->rcv_adv - tp->rcv_nxt);
424
425 if (adv >= (long) (2 * tp->t_maxseg))
426 goto send;
427 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
428 goto send;
429 }
430
431 /*
432 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
433 * is also a catch-all for the retransmit timer timeout case.
434 */
435 if (tp->t_flags & TF_ACKNOW)
436 goto send;
437 if ((flags & TH_RST) ||
438 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
439 goto send;
440 if (SEQ_GT(tp->snd_up, tp->snd_una))
441 goto send;
442 /*
443 * If our state indicates that FIN should be sent
444 * and we have not yet done so, then we need to send.
445 */
446 if (flags & TH_FIN &&
447 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
448 goto send;
449 /*
450 * In SACK, it is possible for tcp_output to fail to send a segment
451 * after the retransmission timer has been turned off. Make sure
452 * that the retransmission timer is set.
453 */
454 if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
455 !callout_active(tp->tt_rexmt) &&
456 !callout_active(tp->tt_persist)) {
457 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
458 tcp_timer_rexmt, tp);
459 goto just_return;
460 }
461 /*
462 * TCP window updates are not reliable, rather a polling protocol
463 * using ``persist'' packets is used to insure receipt of window
464 * updates. The three ``states'' for the output side are:
465 * idle not doing retransmits or persists
466 * persisting to move a small or zero window
467 * (re)transmitting and thereby not persisting
468 *
469 * callout_active(tp->tt_persist)
470 * is true when we are in persist state.
471 * tp->t_force
472 * is set when we are called to send a persist packet.
473 * callout_active(tp->tt_rexmt)
474 * is set when we are retransmitting
475 * The output side is idle when both timers are zero.
476 *
477 * If send window is too small, there is data to transmit, and no
478 * retransmit or persist is pending, then go to persist state.
479 * If nothing happens soon, send when timer expires:
480 * if window is nonzero, transmit what we can,
481 * otherwise force out a byte.
482 */
483 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
484 !callout_active(tp->tt_persist)) {
485 tp->t_rxtshift = 0;
486 tcp_setpersist(tp);
487 }
488
489 /*
490 * No reason to send a segment, just return.
491 */
492 just_return:
493 SOCKBUF_UNLOCK(&so->so_snd);
494 return (0);
495
496 send:
497 SOCKBUF_LOCK_ASSERT(&so->so_snd);
498 /*
499 * Before ESTABLISHED, force sending of initial options
500 * unless TCP set not to do any options.
501 * NOTE: we assume that the IP/TCP header plus TCP options
502 * always fit in a single mbuf, leaving room for a maximum
503 * link header, i.e.
504 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
505 */
506 optlen = 0;
507 #ifdef INET6
508 if (isipv6)
509 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
510 else
511 #endif
512 hdrlen = sizeof (struct tcpiphdr);
513 if (flags & TH_SYN) {
514 tp->snd_nxt = tp->iss;
515 if ((tp->t_flags & TF_NOOPT) == 0) {
516 u_short mss;
517
518 opt[0] = TCPOPT_MAXSEG;
519 opt[1] = TCPOLEN_MAXSEG;
520 mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
521 (void)memcpy(opt + 2, &mss, sizeof(mss));
522 optlen = TCPOLEN_MAXSEG;
523
524 /*
525 * If this is the first SYN of connection (not a SYN
526 * ACK), include SACK_PERMIT_HDR option. If this is a
527 * SYN ACK, include SACK_PERMIT_HDR option if peer has
528 * already done so. This is only for active connect,
529 * since the syncache takes care of the passive connect.
530 */
531 if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
532 (tp->t_flags & TF_SACK_PERMIT))) {
533 *((u_int32_t *) (opt + optlen)) =
534 htonl(TCPOPT_SACK_PERMIT_HDR);
535 optlen += 4;
536 }
537 if ((tp->t_flags & TF_REQ_SCALE) &&
538 ((flags & TH_ACK) == 0 ||
539 (tp->t_flags & TF_RCVD_SCALE))) {
540 *((u_int32_t *)(opt + optlen)) = htonl(
541 TCPOPT_NOP << 24 |
542 TCPOPT_WINDOW << 16 |
543 TCPOLEN_WINDOW << 8 |
544 tp->request_r_scale);
545 optlen += 4;
546 }
547 }
548 }
549
550 /*
551 * Send a timestamp and echo-reply if this is a SYN and our side
552 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
553 * and our peer have sent timestamps in our SYN's.
554 */
555 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
556 (flags & TH_RST) == 0 &&
557 ((flags & TH_ACK) == 0 ||
558 (tp->t_flags & TF_RCVD_TSTMP))) {
559 u_int32_t *lp = (u_int32_t *)(opt + optlen);
560
561 /* Form timestamp option as shown in appendix A of RFC 1323. */
562 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
563 *lp++ = htonl(ticks);
564 *lp = htonl(tp->ts_recent);
565 optlen += TCPOLEN_TSTAMP_APPA;
566 }
567
568 /*
569 * Send SACKs if necessary. This should be the last option processed.
570 * Only as many SACKs are sent as are permitted by the maximum options
571 * size. No more than three SACKs are sent.
572 */
573 if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
574 (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
575 tp->rcv_numsacks) {
576 u_int32_t *lp = (u_int32_t *)(opt + optlen);
577 u_int32_t *olp = lp++;
578 int count = 0; /* actual number of SACKs inserted */
579 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
580
581 tcpstat.tcps_sack_send_blocks++;
582 maxsack = min(maxsack, TCP_MAX_SACK);
583 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
584 struct sackblk sack = tp->sackblks[i];
585 if (sack.start == 0 && sack.end == 0)
586 continue;
587 *lp++ = htonl(sack.start);
588 *lp++ = htonl(sack.end);
589 count++;
590 }
591 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
592 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
593 }
594 /*
595 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
596 * options are allowed (!TF_NOOPT) and it's not a RST.
597 */
598 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
599 (flags & TH_RST) == 0) {
600 switch (flags & (TH_SYN|TH_ACK)) {
601 /*
602 * This is a normal ACK, send CC if we received CC before
603 * from our peer.
604 */
605 case TH_ACK:
606 if (!(tp->t_flags & TF_RCVD_CC))
607 break;
608 /*FALLTHROUGH*/
609
610 /*
611 * We can only get here in T/TCP's SYN_SENT* state, when
612 * we're a sending a non-SYN segment without waiting for
613 * the ACK of our SYN. A check above assures that we only
614 * do this if our peer understands T/TCP.
615 */
616 case 0:
617 opt[optlen++] = TCPOPT_NOP;
618 opt[optlen++] = TCPOPT_NOP;
619 opt[optlen++] = TCPOPT_CC;
620 opt[optlen++] = TCPOLEN_CC;
621 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
622
623 optlen += 4;
624 break;
625
626 /*
627 * This is our initial SYN, check whether we have to use
628 * CC or CC.new.
629 */
630 case TH_SYN:
631 opt[optlen++] = TCPOPT_NOP;
632 opt[optlen++] = TCPOPT_NOP;
633 opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
634 TCPOPT_CCNEW : TCPOPT_CC;
635 opt[optlen++] = TCPOLEN_CC;
636 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
637 optlen += 4;
638 break;
639
640 /*
641 * This is a SYN,ACK; send CC and CC.echo if we received
642 * CC from our peer.
643 */
644 case (TH_SYN|TH_ACK):
645 if (tp->t_flags & TF_RCVD_CC) {
646 opt[optlen++] = TCPOPT_NOP;
647 opt[optlen++] = TCPOPT_NOP;
648 opt[optlen++] = TCPOPT_CC;
649 opt[optlen++] = TCPOLEN_CC;
650 *(u_int32_t *)&opt[optlen] =
651 htonl(tp->cc_send);
652 optlen += 4;
653 opt[optlen++] = TCPOPT_NOP;
654 opt[optlen++] = TCPOPT_NOP;
655 opt[optlen++] = TCPOPT_CCECHO;
656 opt[optlen++] = TCPOLEN_CC;
657 *(u_int32_t *)&opt[optlen] =
658 htonl(tp->cc_recv);
659 optlen += 4;
660 }
661 break;
662 }
663 }
664
665 #ifdef TCP_SIGNATURE
666 #ifdef INET6
667 if (!isipv6)
668 #endif
669 if (tp->t_flags & TF_SIGNATURE) {
670 int i;
671 u_char *bp;
672
673 /* Initialize TCP-MD5 option (RFC2385) */
674 bp = (u_char *)opt + optlen;
675 *bp++ = TCPOPT_SIGNATURE;
676 *bp++ = TCPOLEN_SIGNATURE;
677 sigoff = optlen + 2;
678 for (i = 0; i < TCP_SIGLEN; i++)
679 *bp++ = 0;
680 optlen += TCPOLEN_SIGNATURE;
681
682 /* Terminate options list and maintain 32-bit alignment. */
683 *bp++ = TCPOPT_NOP;
684 *bp++ = TCPOPT_EOL;
685 optlen += 2;
686 }
687 #endif /* TCP_SIGNATURE */
688
689 hdrlen += optlen;
690
691 #ifdef INET6
692 if (isipv6)
693 ipoptlen = ip6_optlen(tp->t_inpcb);
694 else
695 #endif
696 if (tp->t_inpcb->inp_options)
697 ipoptlen = tp->t_inpcb->inp_options->m_len -
698 offsetof(struct ipoption, ipopt_list);
699 else
700 ipoptlen = 0;
701 #ifdef IPSEC
702 ipoptlen += ipsec_hdrsiz_tcp(tp);
703 #endif
704
705 /*
706 * Adjust data length if insertion of options will
707 * bump the packet length beyond the t_maxopd length.
708 * Clear the FIN bit because we cut off the tail of
709 * the segment.
710 */
711 if (len + optlen + ipoptlen > tp->t_maxopd) {
712 /*
713 * If there is still more to send, don't close the connection.
714 */
715 flags &= ~TH_FIN;
716 len = tp->t_maxopd - optlen - ipoptlen;
717 sendalot = 1;
718 }
719
720 /*#ifdef DIAGNOSTIC*/
721 #ifdef INET6
722 if (max_linkhdr + hdrlen > MCLBYTES)
723 #else
724 if (max_linkhdr + hdrlen > MHLEN)
725 #endif
726 panic("tcphdr too big");
727 /*#endif*/
728
729 /*
730 * Grab a header mbuf, attaching a copy of data to
731 * be transmitted, and initialize the header from
732 * the template for sends on this connection.
733 */
734 if (len) {
735 if (tp->t_force && len == 1)
736 tcpstat.tcps_sndprobe++;
737 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
738 tcpstat.tcps_sndrexmitpack++;
739 tcpstat.tcps_sndrexmitbyte += len;
740 } else {
741 tcpstat.tcps_sndpack++;
742 tcpstat.tcps_sndbyte += len;
743 }
744 #ifdef notyet
745 if ((m = m_copypack(so->so_snd.sb_mb, off,
746 (int)len, max_linkhdr + hdrlen)) == 0) {
747 SOCKBUF_UNLOCK(&so->so_snd);
748 error = ENOBUFS;
749 goto out;
750 }
751 /*
752 * m_copypack left space for our hdr; use it.
753 */
754 m->m_len += hdrlen;
755 m->m_data -= hdrlen;
756 #else
757 MGETHDR(m, M_DONTWAIT, MT_HEADER);
758 if (m == NULL) {
759 SOCKBUF_UNLOCK(&so->so_snd);
760 error = ENOBUFS;
761 goto out;
762 }
763 #ifdef INET6
764 if (MHLEN < hdrlen + max_linkhdr) {
765 MCLGET(m, M_DONTWAIT);
766 if ((m->m_flags & M_EXT) == 0) {
767 SOCKBUF_UNLOCK(&so->so_snd);
768 m_freem(m);
769 error = ENOBUFS;
770 goto out;
771 }
772 }
773 #endif
774 m->m_data += max_linkhdr;
775 m->m_len = hdrlen;
776 if (len <= MHLEN - hdrlen - max_linkhdr) {
777 m_copydata(so->so_snd.sb_mb, off, (int) len,
778 mtod(m, caddr_t) + hdrlen);
779 m->m_len += len;
780 } else {
781 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
782 if (m->m_next == 0) {
783 SOCKBUF_UNLOCK(&so->so_snd);
784 (void) m_free(m);
785 error = ENOBUFS;
786 goto out;
787 }
788 }
789 #endif
790 /*
791 * If we're sending everything we've got, set PUSH.
792 * (This will keep happy those implementations which only
793 * give data to the user when a buffer fills or
794 * a PUSH comes in.)
795 */
796 if (off + len == so->so_snd.sb_cc)
797 flags |= TH_PUSH;
798 SOCKBUF_UNLOCK(&so->so_snd);
799 } else {
800 SOCKBUF_UNLOCK(&so->so_snd);
801 if (tp->t_flags & TF_ACKNOW)
802 tcpstat.tcps_sndacks++;
803 else if (flags & (TH_SYN|TH_FIN|TH_RST))
804 tcpstat.tcps_sndctrl++;
805 else if (SEQ_GT(tp->snd_up, tp->snd_una))
806 tcpstat.tcps_sndurg++;
807 else
808 tcpstat.tcps_sndwinup++;
809
810 MGETHDR(m, M_DONTWAIT, MT_HEADER);
811 if (m == NULL) {
812 error = ENOBUFS;
813 goto out;
814 }
815 #ifdef INET6
816 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
817 MHLEN >= hdrlen) {
818 MH_ALIGN(m, hdrlen);
819 } else
820 #endif
821 m->m_data += max_linkhdr;
822 m->m_len = hdrlen;
823 }
824 SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
825 m->m_pkthdr.rcvif = (struct ifnet *)0;
826 #ifdef MAC
827 mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
828 #endif
829 #ifdef INET6
830 if (isipv6) {
831 ip6 = mtod(m, struct ip6_hdr *);
832 th = (struct tcphdr *)(ip6 + 1);
833 tcpip_fillheaders(tp->t_inpcb, ip6, th);
834 } else
835 #endif /* INET6 */
836 {
837 ip = mtod(m, struct ip *);
838 ipov = (struct ipovly *)ip;
839 th = (struct tcphdr *)(ip + 1);
840 tcpip_fillheaders(tp->t_inpcb, ip, th);
841 }
842
843 /*
844 * Fill in fields, remembering maximum advertised
845 * window for use in delaying messages about window sizes.
846 * If resending a FIN, be sure not to use a new sequence number.
847 */
848 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
849 tp->snd_nxt == tp->snd_max)
850 tp->snd_nxt--;
851 /*
852 * If we are doing retransmissions, then snd_nxt will
853 * not reflect the first unsent octet. For ACK only
854 * packets, we do not want the sequence number of the
855 * retransmitted packet, we want the sequence number
856 * of the next unsent octet. So, if there is no data
857 * (and no SYN or FIN), use snd_max instead of snd_nxt
858 * when filling in ti_seq. But if we are in persist
859 * state, snd_max might reflect one byte beyond the
860 * right edge of the window, so use snd_nxt in that
861 * case, since we know we aren't doing a retransmission.
862 * (retransmit and persist are mutually exclusive...)
863 */
864 if (len || (flags & (TH_SYN|TH_FIN))
865 || callout_active(tp->tt_persist))
866 th->th_seq = htonl(tp->snd_nxt);
867 else
868 th->th_seq = htonl(tp->snd_max);
869 if (sack_rxmit) {
870 th->th_seq = htonl(p->rxmit);
871 p->rxmit += len;
872 }
873 th->th_ack = htonl(tp->rcv_nxt);
874 if (optlen) {
875 bcopy(opt, th + 1, optlen);
876 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
877 }
878 th->th_flags = flags;
879 /*
880 * Calculate receive window. Don't shrink window,
881 * but avoid silly window syndrome.
882 */
883 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
884 recwin < (long)tp->t_maxseg)
885 recwin = 0;
886 if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
887 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
888 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
889 recwin = (long)TCP_MAXWIN << tp->rcv_scale;
890 th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
891
892
893 /*
894 * Adjust the RXWIN0SENT flag - indicate that we have advertised
895 * a 0 window. This may cause the remote transmitter to stall. This
896 * flag tells soreceive() to disable delayed acknowledgements when
897 * draining the buffer. This can occur if the receiver is attempting
898 * to read more data then can be buffered prior to transmitting on
899 * the connection.
900 */
901 if (recwin == 0)
902 tp->t_flags |= TF_RXWIN0SENT;
903 else
904 tp->t_flags &= ~TF_RXWIN0SENT;
905 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
906 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
907 th->th_flags |= TH_URG;
908 } else
909 /*
910 * If no urgent pointer to send, then we pull
911 * the urgent pointer to the left edge of the send window
912 * so that it doesn't drift into the send window on sequence
913 * number wraparound.
914 */
915 tp->snd_up = tp->snd_una; /* drag it along */
916
917 #ifdef TCP_SIGNATURE
918 #ifdef INET6
919 if (!isipv6)
920 #endif
921 if (tp->t_flags & TF_SIGNATURE)
922 tcp_signature_compute(m, sizeof(struct ip), len, optlen,
923 (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
924 #endif
925
926 /*
927 * Put TCP length in extended header, and then
928 * checksum extended header and data.
929 */
930 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
931 #ifdef INET6
932 if (isipv6)
933 /*
934 * ip6_plen is not need to be filled now, and will be filled
935 * in ip6_output.
936 */
937 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
938 sizeof(struct tcphdr) + optlen + len);
939 else
940 #endif /* INET6 */
941 {
942 m->m_pkthdr.csum_flags = CSUM_TCP;
943 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
944 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
945 htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
946
947 /* IP version must be set here for ipv4/ipv6 checking later */
948 KASSERT(ip->ip_v == IPVERSION,
949 ("%s: IP version incorrect: %d", __func__, ip->ip_v));
950 }
951
952 /*
953 * In transmit state, time the transmission and arrange for
954 * the retransmit. In persist state, just set snd_max.
955 */
956 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
957 tcp_seq startseq = tp->snd_nxt;
958
959 /*
960 * Advance snd_nxt over sequence space of this segment.
961 */
962 if (flags & (TH_SYN|TH_FIN)) {
963 if (flags & TH_SYN)
964 tp->snd_nxt++;
965 if (flags & TH_FIN) {
966 tp->snd_nxt++;
967 tp->t_flags |= TF_SENTFIN;
968 }
969 }
970 if (tp->sack_enable && sack_rxmit)
971 goto timer;
972 tp->snd_nxt += len;
973 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
974 tp->snd_max = tp->snd_nxt;
975 /*
976 * Time this transmission if not a retransmission and
977 * not currently timing anything.
978 */
979 if (tp->t_rtttime == 0) {
980 tp->t_rtttime = ticks;
981 tp->t_rtseq = startseq;
982 tcpstat.tcps_segstimed++;
983 }
984 }
985
986 /*
987 * Set retransmit timer if not currently set,
988 * and not doing a pure ack or a keep-alive probe.
989 * Initial value for retransmit timer is smoothed
990 * round-trip time + 2 * round-trip time variance.
991 * Initialize shift counter which is used for backoff
992 * of retransmit time.
993 */
994 timer:
995 if (tp->sack_enable && sack_rxmit &&
996 !callout_active(tp->tt_rexmt) &&
997 tp->snd_nxt != tp->snd_max) {
998 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
999 tcp_timer_rexmt, tp);
1000 if (callout_active(tp->tt_persist)) {
1001 callout_stop(tp->tt_persist);
1002 tp->t_rxtshift = 0;
1003 }
1004 }
1005 if (!callout_active(tp->tt_rexmt) &&
1006 tp->snd_nxt != tp->snd_una) {
1007 if (callout_active(tp->tt_persist)) {
1008 callout_stop(tp->tt_persist);
1009 tp->t_rxtshift = 0;
1010 }
1011 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1012 tcp_timer_rexmt, tp);
1013 }
1014 } else {
1015 /*
1016 * Persist case, update snd_max but since we are in
1017 * persist mode (no window) we do not update snd_nxt.
1018 */
1019 int xlen = len;
1020 if (flags & TH_SYN)
1021 ++xlen;
1022 if (flags & TH_FIN) {
1023 ++xlen;
1024 tp->t_flags |= TF_SENTFIN;
1025 }
1026 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1027 tp->snd_max = tp->snd_nxt + len;
1028 }
1029
1030 #ifdef TCPDEBUG
1031 /*
1032 * Trace.
1033 */
1034 if (so->so_options & SO_DEBUG) {
1035 u_short save = 0;
1036 #ifdef INET6
1037 if (!isipv6)
1038 #endif
1039 {
1040 save = ipov->ih_len;
1041 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
1042 }
1043 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
1044 #ifdef INET6
1045 if (!isipv6)
1046 #endif
1047 ipov->ih_len = save;
1048 }
1049 #endif
1050
1051 /*
1052 * Fill in IP length and desired time to live and
1053 * send to IP level. There should be a better way
1054 * to handle ttl and tos; we could keep them in
1055 * the template, but need a way to checksum without them.
1056 */
1057 /*
1058 * m->m_pkthdr.len should have been set before cksum calcuration,
1059 * because in6_cksum() need it.
1060 */
1061 #ifdef INET6
1062 if (isipv6) {
1063 /*
1064 * we separately set hoplimit for every segment, since the
1065 * user might want to change the value via setsockopt.
1066 * Also, desired default hop limit might be changed via
1067 * Neighbor Discovery.
1068 */
1069 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1070
1071 /* TODO: IPv6 IP6TOS_ECT bit on */
1072 error = ip6_output(m,
1073 tp->t_inpcb->in6p_outputopts, NULL,
1074 (so->so_options & SO_DONTROUTE), NULL, NULL,
1075 tp->t_inpcb);
1076 } else
1077 #endif /* INET6 */
1078 {
1079 ip->ip_len = m->m_pkthdr.len;
1080 #ifdef INET6
1081 if (INP_CHECK_SOCKAF(so, AF_INET6))
1082 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1083 #endif /* INET6 */
1084 /*
1085 * If we do path MTU discovery, then we set DF on every packet.
1086 * This might not be the best thing to do according to RFC3390
1087 * Section 2. However the tcp hostcache migitates the problem
1088 * so it affects only the first tcp connection with a host.
1089 */
1090 if (path_mtu_discovery)
1091 ip->ip_off |= IP_DF;
1092
1093 error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1094 (so->so_options & SO_DONTROUTE), 0, tp->t_inpcb);
1095 }
1096 if (error) {
1097
1098 /*
1099 * We know that the packet was lost, so back out the
1100 * sequence number advance, if any.
1101 */
1102 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
1103 /*
1104 * No need to check for TH_FIN here because
1105 * the TF_SENTFIN flag handles that case.
1106 */
1107 if ((flags & TH_SYN) == 0) {
1108 if (sack_rxmit)
1109 p->rxmit -= len;
1110 else
1111 tp->snd_nxt -= len;
1112 }
1113 }
1114
1115 out:
1116 SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */
1117 if (error == ENOBUFS) {
1118 if (!callout_active(tp->tt_rexmt) &&
1119 !callout_active(tp->tt_persist))
1120 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1121 tcp_timer_rexmt, tp);
1122 tcp_quench(tp->t_inpcb, 0);
1123 return (0);
1124 }
1125 if (error == EMSGSIZE) {
1126 /*
1127 * ip_output() will have already fixed the route
1128 * for us. tcp_mtudisc() will, as its last action,
1129 * initiate retransmission, so it is important to
1130 * not do so here.
1131 */
1132 tcp_mtudisc(tp->t_inpcb, 0);
1133 return 0;
1134 }
1135 if ((error == EHOSTUNREACH || error == ENETDOWN)
1136 && TCPS_HAVERCVDSYN(tp->t_state)) {
1137 tp->t_softerror = error;
1138 return (0);
1139 }
1140 return (error);
1141 }
1142 tcpstat.tcps_sndtotal++;
1143
1144 /*
1145 * Data sent (as far as we can tell).
1146 * If this advertises a larger window than any other segment,
1147 * then remember the size of the advertised window.
1148 * Any pending ACK has now been sent.
1149 */
1150 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1151 tp->rcv_adv = tp->rcv_nxt + recwin;
1152 tp->last_ack_sent = tp->rcv_nxt;
1153 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1154 if (callout_active(tp->tt_delack))
1155 callout_stop(tp->tt_delack);
1156 #if 0
1157 /*
1158 * This completely breaks TCP if newreno is turned on. What happens
1159 * is that if delayed-acks are turned on on the receiver, this code
1160 * on the transmitter effectively destroys the TCP window, forcing
1161 * it to four packets (1.5Kx4 = 6K window).
1162 */
1163 if (sendalot && (!tcp_do_newreno || --maxburst))
1164 goto again;
1165 #endif
1166 if (sendalot)
1167 goto again;
1168 return (0);
1169 }
1170
1171 void
1172 tcp_setpersist(tp)
1173 register struct tcpcb *tp;
1174 {
1175 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
1176 int tt;
1177
1178 if (callout_active(tp->tt_rexmt))
1179 panic("tcp_setpersist: retransmit pending");
1180 /*
1181 * Start/restart persistance timer.
1182 */
1183 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1184 TCPTV_PERSMIN, TCPTV_PERSMAX);
1185 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1186 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1187 tp->t_rxtshift++;
1188 }
Cache object: c85f37a35ab0c6de99ca62938b3b61c8
|