1 /* $OpenBSD: tcp_output.c,v 1.86 2008/09/03 12:51:39 henning Exp $ */ 2 /* $NetBSD: tcp_output.c,v 1.16 1997/06/03 16:17:09 kml Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/protosw.h> 75 #include <sys/socket.h> 76 #include <sys/socketvar.h> 77 #include <sys/kernel.h> 78 79 #include <net/route.h> 80 #include <net/if.h> 81 82 #include <netinet/in.h> 83 #include <netinet/in_systm.h> 84 #include <netinet/ip.h> 85 #include <netinet/in_pcb.h> 86 #include <netinet/ip_var.h> 87 #include <netinet/tcp.h> 88 #define TCPOUTFLAGS 89 #include <netinet/tcp_fsm.h> 90 #include <netinet/tcp_seq.h> 91 #include <netinet/tcp_timer.h> 92 #include <netinet/tcp_var.h> 93 #include <netinet/tcpip.h> 94 #include <netinet/tcp_debug.h> 95 96 #ifdef INET6 97 #include <netinet6/tcpipv6.h> 98 #include <netinet6/in6_var.h> 99 #endif /* INET6 */ 100 101 #ifdef notyet 102 extern struct mbuf *m_copypack(); 103 #endif 104 105 #ifdef TCP_SACK 106 extern int tcprexmtthresh; 107 #endif 108 109 #ifdef TCP_SACK 110 #ifdef TCP_SACK_DEBUG 111 void tcp_print_holes(struct tcpcb *tp); 112 113 void 114 tcp_print_holes(struct tcpcb *tp) 115 { 116 struct sackhole *p = tp->snd_holes; 117 if (p == 0) 118 return; 119 printf("Hole report: start--end dups rxmit\n"); 120 while (p) { 121 printf("%x--%x d %d r %x\n", p->start, p->end, p->dups, 122 p->rxmit); 123 p = p->next; 124 } 125 printf("\n"); 126 } 127 #endif /* TCP_SACK_DEBUG */ 128 129 /* 130 * Returns pointer to a sackhole if there are any pending retransmissions; 131 * NULL otherwise. 132 */ 133 struct sackhole * 134 tcp_sack_output(struct tcpcb *tp) 135 { 136 struct sackhole *p; 137 138 if (!tp->sack_enable) 139 return (NULL); 140 p = tp->snd_holes; 141 while (p) { 142 #ifndef TCP_FACK 143 if (p->dups >= tcprexmtthresh && SEQ_LT(p->rxmit, p->end)) { 144 #else 145 /* In FACK, if p->dups is less than tcprexmtthresh, but 146 * snd_fack advances more than tcprextmtthresh * tp->t_maxseg, 147 * tcp_input() will try fast retransmit. This forces output. 148 */ 149 if ((p->dups >= tcprexmtthresh || 150 tp->t_dupacks == tcprexmtthresh) && 151 SEQ_LT(p->rxmit, p->end)) { 152 #endif /* TCP_FACK */ 153 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 154 p = p->next; 155 continue; 156 } 157 #ifdef TCP_SACK_DEBUG 158 if (p) 159 tcp_print_holes(tp); 160 #endif 161 return (p); 162 } 163 p = p->next; 164 } 165 return (NULL); 166 } 167 168 /* 169 * After a timeout, the SACK list may be rebuilt. This SACK information 170 * should be used to avoid retransmitting SACKed data. This function 171 * traverses the SACK list to see if snd_nxt should be moved forward. 172 */ 173 174 void 175 tcp_sack_adjust(struct tcpcb *tp) 176 { 177 struct sackhole *cur = tp->snd_holes; 178 if (cur == NULL) 179 return; /* No holes */ 180 if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack)) 181 return; /* We're already beyond any SACKed blocks */ 182 /* 183 * Two cases for which we want to advance snd_nxt: 184 * i) snd_nxt lies between end of one hole and beginning of another 185 * ii) snd_nxt lies between end of last hole and rcv_lastsack 186 */ 187 while (cur->next) { 188 if (SEQ_LT(tp->snd_nxt, cur->end)) 189 return; 190 if (SEQ_GEQ(tp->snd_nxt, cur->next->start)) 191 cur = cur->next; 192 else { 193 tp->snd_nxt = cur->next->start; 194 return; 195 } 196 } 197 if (SEQ_LT(tp->snd_nxt, cur->end)) 198 return; 199 tp->snd_nxt = tp->rcv_lastsack; 200 return; 201 } 202 #endif /* TCP_SACK */ 203 204 /* 205 * Tcp output routine: figure out what should be sent and send it. 206 */ 207 int 208 tcp_output(struct tcpcb *tp) 209 { 210 struct socket *so = tp->t_inpcb->inp_socket; 211 long len, win, txmaxseg; 212 int off, flags, error; 213 struct mbuf *m; 214 struct tcphdr *th; 215 u_char opt[MAX_TCPOPTLEN]; 216 unsigned int optlen, hdrlen, packetlen; 217 int idle, sendalot = 0; 218 #ifdef TCP_SACK 219 int i, sack_rxmit = 0; 220 struct sackhole *p; 221 int maxburst = TCP_MAXBURST; 222 #endif 223 #ifdef TCP_SIGNATURE 224 unsigned int sigoff; 225 #endif /* TCP_SIGNATURE */ 226 #ifdef TCP_ECN 227 int needect; 228 #endif 229 230 #if defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) 231 if (tp->sack_enable && (tp->t_flags & TF_SIGNATURE)) 232 return (EINVAL); 233 #endif /* defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) */ 234 235 /* 236 * Determine length of data that should be transmitted, 237 * and flags that will be used. 238 * If there is some data or critical controls (SYN, RST) 239 * to send, then transmit; otherwise, investigate further. 240 */ 241 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 242 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) 243 /* 244 * We have been idle for "a while" and no acks are 245 * expected to clock out any data we send -- 246 * slow start to get ack "clock" running again. 247 */ 248 tp->snd_cwnd = 2 * tp->t_maxseg; 249 250 /* remember 'idle' for next invocation of tcp_output */ 251 if (idle && soissending(so)) { 252 tp->t_flags |= TF_LASTIDLE; 253 idle = 0; 254 } else 255 tp->t_flags &= ~TF_LASTIDLE; 256 257 again: 258 #ifdef TCP_SACK 259 /* 260 * If we've recently taken a timeout, snd_max will be greater than 261 * snd_nxt. There may be SACK information that allows us to avoid 262 * resending already delivered data. Adjust snd_nxt accordingly. 263 */ 264 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 265 tcp_sack_adjust(tp); 266 #endif 267 off = tp->snd_nxt - tp->snd_una; 268 #if defined(TCP_SACK) && defined(TCP_FACK) 269 /* Normally, sendable data is limited by off < tp->snd_cwnd. 270 * But in FACK, sendable data is limited by snd_awnd < snd_cwnd, 271 * regardless of offset. 272 */ 273 if (tp->sack_enable && (tp->t_dupacks > tcprexmtthresh)) 274 win = tp->snd_wnd; 275 else 276 #endif 277 win = ulmin(tp->snd_wnd, tp->snd_cwnd); 278 279 flags = tcp_outflags[tp->t_state]; 280 281 #ifdef TCP_SACK 282 /* 283 * Send any SACK-generated retransmissions. If we're explicitly trying 284 * to send out new data (when sendalot is 1), bypass this function. 285 * If we retransmit in fast recovery mode, decrement snd_cwnd, since 286 * we're replacing a (future) new transmission with a retransmission 287 * now, and we previously incremented snd_cwnd in tcp_input(). 288 */ 289 if (tp->sack_enable && !sendalot) { 290 if (tp->t_dupacks >= tcprexmtthresh && 291 (p = tcp_sack_output(tp))) { 292 off = p->rxmit - tp->snd_una; 293 sack_rxmit = 1; 294 /* Coalesce holes into a single retransmission */ 295 len = min(tp->t_maxseg, p->end - p->rxmit); 296 #ifndef TCP_FACK 297 /* in FACK, hold snd_cwnd constant during recovery */ 298 if (SEQ_LT(tp->snd_una, tp->snd_last)) 299 tp->snd_cwnd -= tp->t_maxseg; 300 #endif 301 } 302 } 303 #endif /* TCP_SACK */ 304 305 sendalot = 0; 306 /* 307 * If in persist timeout with window of 0, send 1 byte. 308 * Otherwise, if window is small but nonzero 309 * and timer expired, we will send what we can 310 * and go to transmit state. 311 */ 312 if (tp->t_force) { 313 if (win == 0) { 314 /* 315 * If we still have some data to send, then 316 * clear the FIN bit. Usually this would 317 * happen below when it realizes that we 318 * aren't sending all the data. However, 319 * if we have exactly 1 byte of unset data, 320 * then it won't clear the FIN bit below, 321 * and if we are in persist state, we wind 322 * up sending the packet without recording 323 * that we sent the FIN bit. 324 * 325 * We can't just blindly clear the FIN bit, 326 * because if we don't have any more data 327 * to send then the probe will be the FIN 328 * itself. 329 */ 330 if (off < so->so_snd.sb_cc) 331 flags &= ~TH_FIN; 332 win = 1; 333 } else { 334 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 335 tp->t_rxtshift = 0; 336 } 337 } 338 339 #ifdef TCP_SACK 340 if (!sack_rxmit) { 341 #endif 342 len = ulmin(so->so_snd.sb_cc, win) - off; 343 344 #if defined(TCP_SACK) && defined(TCP_FACK) 345 /* 346 * If we're in fast recovery (SEQ_GT(tp->snd_last, tp->snd_una)), and 347 * amount of outstanding data (snd_awnd) is >= snd_cwnd, then 348 * do not send data (like zero window conditions) 349 */ 350 if (tp->sack_enable && len && SEQ_GT(tp->snd_last, tp->snd_una) && 351 (tp->snd_awnd >= tp->snd_cwnd)) 352 len = 0; 353 #endif /* TCP_FACK */ 354 #ifdef TCP_SACK 355 } 356 #endif 357 358 if (len < 0) { 359 /* 360 * If FIN has been sent but not acked, 361 * but we haven't been called to retransmit, 362 * len will be -1. Otherwise, window shrank 363 * after we sent into it. If window shrank to 0, 364 * cancel pending retransmit, pull snd_nxt back 365 * to (closed) window, and set the persist timer 366 * if it isn't already going. If the window didn't 367 * close completely, just wait for an ACK. 368 */ 369 len = 0; 370 if (win == 0) { 371 TCP_TIMER_DISARM(tp, TCPT_REXMT); 372 tp->t_rxtshift = 0; 373 tp->snd_nxt = tp->snd_una; 374 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 375 tcp_setpersist(tp); 376 } 377 } 378 379 /* 380 * Never send more than half a buffer full. This insures that we can 381 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and 382 * therefore acks will never be delayed unless we run out of data to 383 * transmit. 384 */ 385 txmaxseg = ulmin(so->so_snd.sb_hiwat / 2, tp->t_maxseg); 386 387 if (len > txmaxseg) { 388 len = txmaxseg; 389 sendalot = 1; 390 } 391 if (off + len < so->so_snd.sb_cc) 392 flags &= ~TH_FIN; 393 394 win = sbspace(&so->so_rcv); 395 396 /* 397 * Sender silly window avoidance. If connection is idle 398 * and can send all data, a maximum segment, 399 * at least a maximum default-size segment do it, 400 * or are forced, do it; otherwise don't bother. 401 * If peer's buffer is tiny, then send 402 * when window is at least half open. 403 * If retransmitting (possibly after persist timer forced us 404 * to send into a small window), then must resend. 405 */ 406 if (len) { 407 if (len == txmaxseg) 408 goto send; 409 if ((idle || tp->t_flags & TF_NODELAY) && 410 len + off >= so->so_snd.sb_cc && !soissending(so)) 411 goto send; 412 if (tp->t_force) 413 goto send; 414 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 415 goto send; 416 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 417 goto send; 418 #ifdef TCP_SACK 419 if (sack_rxmit) 420 goto send; 421 #endif 422 } 423 424 /* 425 * Compare available window to amount of window 426 * known to peer (as advertised window less 427 * next expected input). If the difference is at least two 428 * max size segments, or at least 50% of the maximum possible 429 * window, then want to send a window update to peer. 430 */ 431 if (win > 0) { 432 /* 433 * "adv" is the amount we can increase the window, 434 * taking into account that we are limited by 435 * TCP_MAXWIN << tp->rcv_scale. 436 */ 437 long adv = lmin(win, (long)TCP_MAXWIN << tp->rcv_scale) - 438 (tp->rcv_adv - tp->rcv_nxt); 439 440 if (adv >= (long) (2 * tp->t_maxseg)) 441 goto send; 442 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 443 goto send; 444 } 445 446 /* 447 * Send if we owe peer an ACK. 448 */ 449 if (tp->t_flags & TF_ACKNOW) 450 goto send; 451 if (flags & (TH_SYN|TH_RST)) 452 goto send; 453 if (SEQ_GT(tp->snd_up, tp->snd_una)) 454 goto send; 455 /* 456 * If our state indicates that FIN should be sent 457 * and we have not yet done so, or we're retransmitting the FIN, 458 * then we need to send. 459 */ 460 if (flags & TH_FIN && 461 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 462 goto send; 463 #ifdef TCP_SACK 464 /* 465 * In SACK, it is possible for tcp_output to fail to send a segment 466 * after the retransmission timer has been turned off. Make sure 467 * that the retransmission timer is set. 468 */ 469 if (SEQ_GT(tp->snd_max, tp->snd_una) && 470 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 471 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 472 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 473 return (0); 474 } 475 #endif /* TCP_SACK */ 476 477 /* 478 * TCP window updates are not reliable, rather a polling protocol 479 * using ``persist'' packets is used to insure receipt of window 480 * updates. The three ``states'' for the output side are: 481 * idle not doing retransmits or persists 482 * persisting to move a small or zero window 483 * (re)transmitting and thereby not persisting 484 * 485 * tp->t_timer[TCPT_PERSIST] 486 * is set when we are in persist state. 487 * tp->t_force 488 * is set when we are called to send a persist packet. 489 * tp->t_timer[TCPT_REXMT] 490 * is set when we are retransmitting 491 * The output side is idle when both timers are zero. 492 * 493 * If send window is too small, there is data to transmit, and no 494 * retransmit or persist is pending, then go to persist state. 495 * If nothing happens soon, send when timer expires: 496 * if window is nonzero, transmit what we can, 497 * otherwise force out a byte. 498 */ 499 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 500 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 501 tp->t_rxtshift = 0; 502 tcp_setpersist(tp); 503 } 504 505 /* 506 * No reason to send a segment, just return. 507 */ 508 return (0); 509 510 send: 511 /* 512 * Before ESTABLISHED, force sending of initial options 513 * unless TCP set not to do any options. 514 * NOTE: we assume that the IP/TCP header plus TCP options 515 * always fit in a single mbuf, leaving room for a maximum 516 * link header, i.e. 517 * max_linkhdr + sizeof(network header) + sizeof(struct tcphdr + 518 * optlen <= MHLEN 519 */ 520 optlen = 0; 521 522 switch (tp->pf) { 523 case 0: /*default to PF_INET*/ 524 #ifdef INET 525 case PF_INET: 526 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr); 527 break; 528 #endif /* INET */ 529 #ifdef INET6 530 case PF_INET6: 531 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 532 break; 533 #endif /* INET6 */ 534 default: 535 return (EPFNOSUPPORT); 536 } 537 538 if (flags & TH_SYN) { 539 tp->snd_nxt = tp->iss; 540 if ((tp->t_flags & TF_NOOPT) == 0) { 541 u_int16_t mss; 542 543 opt[0] = TCPOPT_MAXSEG; 544 opt[1] = 4; 545 mss = htons((u_int16_t) tcp_mss(tp, 0)); 546 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss)); 547 optlen = 4; 548 549 if (flags & TH_ACK) 550 tcp_mss_update(tp); 551 #ifdef TCP_SACK 552 /* 553 * If this is the first SYN of connection (not a SYN 554 * ACK), include SACK_PERMIT_HDR option. If this is a 555 * SYN ACK, include SACK_PERMIT_HDR option if peer has 556 * already done so. 557 */ 558 if (tp->sack_enable && ((flags & TH_ACK) == 0 || 559 (tp->t_flags & TF_SACK_PERMIT))) { 560 *((u_int32_t *) (opt + optlen)) = 561 htonl(TCPOPT_SACK_PERMIT_HDR); 562 optlen += 4; 563 } 564 #endif 565 566 if ((tp->t_flags & TF_REQ_SCALE) && 567 ((flags & TH_ACK) == 0 || 568 (tp->t_flags & TF_RCVD_SCALE))) { 569 *((u_int32_t *) (opt + optlen)) = htonl( 570 TCPOPT_NOP << 24 | 571 TCPOPT_WINDOW << 16 | 572 TCPOLEN_WINDOW << 8 | 573 tp->request_r_scale); 574 optlen += 4; 575 } 576 } 577 } 578 579 /* 580 * Send a timestamp and echo-reply if this is a SYN and our side 581 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 582 * and our peer have sent timestamps in our SYN's. 583 */ 584 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 585 (flags & TH_RST) == 0 && 586 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 587 (tp->t_flags & TF_RCVD_TSTMP))) { 588 u_int32_t *lp = (u_int32_t *)(opt + optlen); 589 590 /* Form timestamp option as shown in appendix A of RFC 1323. */ 591 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 592 *lp++ = htonl(tcp_now + tp->ts_modulate); 593 *lp = htonl(tp->ts_recent); 594 optlen += TCPOLEN_TSTAMP_APPA; 595 } 596 597 #ifdef TCP_SIGNATURE 598 if (tp->t_flags & TF_SIGNATURE) { 599 u_int8_t *bp = (u_int8_t *)(opt + optlen); 600 601 /* Send signature option */ 602 *(bp++) = TCPOPT_SIGNATURE; 603 *(bp++) = TCPOLEN_SIGNATURE; 604 sigoff = optlen + 2; 605 606 { 607 unsigned int i; 608 609 for (i = 0; i < 16; i++) 610 *(bp++) = 0; 611 } 612 613 614 /* Pad options list to the next 32 bit boundary and 615 * terminate it. 616 */ 617 *bp++ = TCPOPT_NOP; 618 *bp++ = TCPOPT_NOP; 619 620 optlen += TCPOLEN_SIGLEN; 621 } 622 #endif /* TCP_SIGNATURE */ 623 624 #ifdef TCP_SACK 625 /* 626 * Send SACKs if necessary. This should be the last option processed. 627 * Only as many SACKs are sent as are permitted by the maximum options 628 * size. No more than three SACKs are sent. 629 */ 630 if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED && 631 (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT && 632 tp->rcv_numsacks) { 633 u_int32_t *lp = (u_int32_t *)(opt + optlen); 634 u_int32_t *olp = lp++; 635 int count = 0; /* actual number of SACKs inserted */ 636 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK; 637 638 tcpstat.tcps_sack_snd_opts++; 639 maxsack = min(maxsack, TCP_MAX_SACK); 640 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) { 641 struct sackblk sack = tp->sackblks[i]; 642 if (sack.start == 0 && sack.end == 0) 643 continue; 644 *lp++ = htonl(sack.start); 645 *lp++ = htonl(sack.end); 646 count++; 647 } 648 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2)); 649 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */ 650 } 651 #endif /* TCP_SACK */ 652 653 #ifdef DIAGNOSTIC 654 if (optlen > MAX_TCPOPTLEN) 655 panic("tcp_output: options too long"); 656 #endif /* DIAGNOSTIC */ 657 658 hdrlen += optlen; 659 660 /* 661 * Adjust data length if insertion of options will 662 * bump the packet length beyond the t_maxopd length. 663 */ 664 if (len > tp->t_maxopd - optlen) { 665 len = tp->t_maxopd - optlen; 666 sendalot = 1; 667 flags &= ~TH_FIN; 668 } 669 670 #ifdef DIAGNOSTIC 671 if (max_linkhdr + hdrlen > MCLBYTES) 672 panic("tcphdr too big"); 673 #endif 674 675 /* 676 * Grab a header mbuf, attaching a copy of data to 677 * be transmitted, and initialize the header from 678 * the template for sends on this connection. 679 */ 680 if (len) { 681 if (tp->t_force && len == 1) 682 tcpstat.tcps_sndprobe++; 683 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 684 tcpstat.tcps_sndrexmitpack++; 685 tcpstat.tcps_sndrexmitbyte += len; 686 } else { 687 tcpstat.tcps_sndpack++; 688 tcpstat.tcps_sndbyte += len; 689 } 690 #ifdef notyet 691 if ((m = m_copypack(so->so_snd.sb_mb, off, 692 (int)len, max_linkhdr + hdrlen)) == 0) { 693 error = ENOBUFS; 694 goto out; 695 } 696 /* 697 * m_copypack left space for our hdr; use it. 698 */ 699 m->m_len += hdrlen; 700 m->m_data -= hdrlen; 701 #else 702 MGETHDR(m, M_DONTWAIT, MT_HEADER); 703 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 704 MCLGET(m, M_DONTWAIT); 705 if ((m->m_flags & M_EXT) == 0) { 706 m_freem(m); 707 m = NULL; 708 } 709 } 710 if (m == NULL) { 711 error = ENOBUFS; 712 goto out; 713 } 714 m->m_data += max_linkhdr; 715 m->m_len = hdrlen; 716 if (len <= M_TRAILINGSPACE(m)) { 717 m_copydata(so->so_snd.sb_mb, off, (int) len, 718 mtod(m, caddr_t) + hdrlen); 719 m->m_len += len; 720 } else { 721 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 722 if (m->m_next == 0) { 723 (void) m_free(m); 724 error = ENOBUFS; 725 goto out; 726 } 727 } 728 #endif 729 /* 730 * If we're sending everything we've got, set PUSH. 731 * (This will keep happy those implementations which only 732 * give data to the user when a buffer fills or 733 * a PUSH comes in.) 734 */ 735 if (off + len == so->so_snd.sb_cc && !soissending(so)) 736 flags |= TH_PUSH; 737 } else { 738 if (tp->t_flags & TF_ACKNOW) 739 tcpstat.tcps_sndacks++; 740 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 741 tcpstat.tcps_sndctrl++; 742 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 743 tcpstat.tcps_sndurg++; 744 else 745 tcpstat.tcps_sndwinup++; 746 747 MGETHDR(m, M_DONTWAIT, MT_HEADER); 748 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 749 MCLGET(m, M_DONTWAIT); 750 if ((m->m_flags & M_EXT) == 0) { 751 m_freem(m); 752 m = NULL; 753 } 754 } 755 if (m == NULL) { 756 error = ENOBUFS; 757 goto out; 758 } 759 m->m_data += max_linkhdr; 760 m->m_len = hdrlen; 761 } 762 m->m_pkthdr.rcvif = (struct ifnet *)0; 763 m->m_pkthdr.len = hdrlen + len; 764 765 if (!tp->t_template) 766 panic("tcp_output"); 767 #ifdef DIAGNOSTIC 768 if (tp->t_template->m_len != hdrlen - optlen) 769 panic("tcp_output: template len != hdrlen - optlen"); 770 #endif /* DIAGNOSTIC */ 771 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), 772 tp->t_template->m_len); 773 th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len - 774 sizeof(struct tcphdr)); 775 776 /* 777 * Fill in fields, remembering maximum advertised 778 * window for use in delaying messages about window sizes. 779 * If resending a FIN, be sure not to use a new sequence number. 780 */ 781 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) && 782 (tp->snd_nxt == tp->snd_max)) 783 tp->snd_nxt--; 784 /* 785 * If we are doing retransmissions, then snd_nxt will 786 * not reflect the first unsent octet. For ACK only 787 * packets, we do not want the sequence number of the 788 * retransmitted packet, we want the sequence number 789 * of the next unsent octet. So, if there is no data 790 * (and no SYN or FIN), use snd_max instead of snd_nxt 791 * when filling in ti_seq. But if we are in persist 792 * state, snd_max might reflect one byte beyond the 793 * right edge of the window, so use snd_nxt in that 794 * case, since we know we aren't doing a retransmission. 795 * (retransmit and persist are mutually exclusive...) 796 */ 797 if (len || (flags & (TH_SYN|TH_FIN)) || TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) 798 th->th_seq = htonl(tp->snd_nxt); 799 else 800 th->th_seq = htonl(tp->snd_max); 801 802 #ifdef TCP_SACK 803 if (sack_rxmit) { 804 /* 805 * If sendalot was turned on (due to option stuffing), turn it 806 * off. Properly set th_seq field. Advance the ret'x pointer 807 * by len. 808 */ 809 if (sendalot) 810 sendalot = 0; 811 th->th_seq = htonl(p->rxmit); 812 p->rxmit += len; 813 #if defined(TCP_SACK) && defined(TCP_FACK) 814 tp->retran_data += len; 815 #endif /* TCP_FACK */ 816 tcpstat.tcps_sack_rexmits++; 817 tcpstat.tcps_sack_rexmit_bytes += len; 818 } 819 #endif /* TCP_SACK */ 820 821 th->th_ack = htonl(tp->rcv_nxt); 822 if (optlen) { 823 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen); 824 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 825 } 826 #ifdef TCP_ECN 827 if (tcp_do_ecn) { 828 /* 829 * if we have received congestion experienced segs, 830 * set ECE bit. 831 */ 832 if (tp->t_flags & TF_RCVD_CE) { 833 flags |= TH_ECE; 834 tcpstat.tcps_ecn_sndece++; 835 } 836 if (!(tp->t_flags & TF_DISABLE_ECN)) { 837 /* 838 * if this is a SYN seg, set ECE and CWR. 839 * set only ECE for SYN-ACK if peer supports ECN. 840 */ 841 if ((flags & (TH_SYN|TH_ACK)) == TH_SYN) 842 flags |= (TH_ECE|TH_CWR); 843 else if ((tp->t_flags & TF_ECN_PERMIT) && 844 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) 845 flags |= TH_ECE; 846 } 847 /* 848 * if we have reduced the congestion window, notify 849 * the peer by setting CWR bit. 850 */ 851 if ((tp->t_flags & TF_ECN_PERMIT) && 852 (tp->t_flags & TF_SEND_CWR)) { 853 flags |= TH_CWR; 854 tp->t_flags &= ~TF_SEND_CWR; 855 tcpstat.tcps_ecn_sndcwr++; 856 } 857 } 858 #endif 859 th->th_flags = flags; 860 861 /* 862 * Calculate receive window. Don't shrink window, 863 * but avoid silly window syndrome. 864 */ 865 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 866 win = 0; 867 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 868 win = (long)TCP_MAXWIN << tp->rcv_scale; 869 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt)) 870 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt); 871 if (flags & TH_RST) 872 win = 0; 873 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); 874 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 875 u_int32_t urp = tp->snd_up - tp->snd_nxt; 876 if (urp > IP_MAXPACKET) 877 urp = IP_MAXPACKET; 878 th->th_urp = htons((u_int16_t)urp); 879 th->th_flags |= TH_URG; 880 } else 881 /* 882 * If no urgent pointer to send, then we pull 883 * the urgent pointer to the left edge of the send window 884 * so that it doesn't drift into the send window on sequence 885 * number wraparound. 886 */ 887 tp->snd_up = tp->snd_una; /* drag it along */ 888 889 #ifdef TCP_SIGNATURE 890 if (tp->t_flags & TF_SIGNATURE) { 891 int iphlen; 892 union sockaddr_union src, dst; 893 struct tdb *tdb; 894 895 bzero(&src, sizeof(union sockaddr_union)); 896 bzero(&dst, sizeof(union sockaddr_union)); 897 898 switch (tp->pf) { 899 case 0: /*default to PF_INET*/ 900 #ifdef INET 901 case AF_INET: 902 iphlen = sizeof(struct ip); 903 src.sa.sa_len = sizeof(struct sockaddr_in); 904 src.sa.sa_family = AF_INET; 905 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 906 dst.sa.sa_len = sizeof(struct sockaddr_in); 907 dst.sa.sa_family = AF_INET; 908 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 909 break; 910 #endif /* INET */ 911 #ifdef INET6 912 case AF_INET6: 913 iphlen = sizeof(struct ip6_hdr); 914 src.sa.sa_len = sizeof(struct sockaddr_in6); 915 src.sa.sa_family = AF_INET6; 916 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 917 dst.sa.sa_len = sizeof(struct sockaddr_in6); 918 dst.sa.sa_family = AF_INET6; 919 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 920 break; 921 #endif /* INET6 */ 922 } 923 924 /* XXX gettdbbysrcdst() should really be called at spltdb(). */ 925 /* XXX this is splsoftnet(), currently they are the same. */ 926 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP); 927 if (tdb == NULL) 928 return (EPERM); 929 930 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0, 931 mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0) 932 return (EINVAL); 933 } 934 #endif /* TCP_SIGNATURE */ 935 936 /* 937 * Put TCP length in extended header, and then 938 * checksum extended header and data. 939 */ 940 switch (tp->pf) { 941 case 0: /*default to PF_INET*/ 942 #ifdef INET 943 case AF_INET: 944 /* Defer checksumming until later (ip_output() or hardware) */ 945 m->m_pkthdr.csum_flags |= M_TCPV4_CSUM_OUT; 946 if (len + optlen) 947 th->th_sum = in_cksum_addword(th->th_sum, 948 htons((u_int16_t)(len + optlen))); 949 break; 950 #endif /* INET */ 951 #ifdef INET6 952 case AF_INET6: 953 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 954 hdrlen - sizeof(struct ip6_hdr) + len); 955 break; 956 #endif /* INET6 */ 957 } 958 959 /* 960 * In transmit state, time the transmission and arrange for 961 * the retransmit. In persist state, just set snd_max. 962 */ 963 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 964 tcp_seq startseq = tp->snd_nxt; 965 966 /* 967 * Advance snd_nxt over sequence space of this segment. 968 */ 969 if (flags & (TH_SYN|TH_FIN)) { 970 if (flags & TH_SYN) 971 tp->snd_nxt++; 972 if (flags & TH_FIN) { 973 tp->snd_nxt++; 974 tp->t_flags |= TF_SENTFIN; 975 } 976 } 977 #ifdef TCP_SACK 978 if (tp->sack_enable) { 979 if (sack_rxmit && (p->rxmit != tp->snd_nxt)) { 980 goto timer; 981 } 982 } 983 #endif 984 tp->snd_nxt += len; 985 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 986 tp->snd_max = tp->snd_nxt; 987 /* 988 * Time this transmission if not a retransmission and 989 * not currently timing anything. 990 */ 991 if (tp->t_rtttime == 0) { 992 tp->t_rtttime = tcp_now; 993 tp->t_rtseq = startseq; 994 tcpstat.tcps_segstimed++; 995 } 996 } 997 998 /* 999 * Set retransmit timer if not currently set, 1000 * and not doing an ack or a keep-alive probe. 1001 * Initial value for retransmit timer is smoothed 1002 * round-trip time + 2 * round-trip time variance. 1003 * Initialize shift counter which is used for backoff 1004 * of retransmit time. 1005 */ 1006 #ifdef TCP_SACK 1007 timer: 1008 if (tp->sack_enable && sack_rxmit && 1009 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1010 tp->snd_nxt != tp->snd_max) { 1011 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1012 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1013 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1014 tp->t_rxtshift = 0; 1015 } 1016 } 1017 #endif 1018 1019 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1020 tp->snd_nxt != tp->snd_una) { 1021 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1022 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1023 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1024 tp->t_rxtshift = 0; 1025 } 1026 } 1027 } else 1028 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 1029 tp->snd_max = tp->snd_nxt + len; 1030 1031 /* 1032 * Trace. 1033 */ 1034 if (so->so_options & SO_DEBUG) 1035 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, caddr_t), 0, 1036 len); 1037 1038 /* 1039 * Fill in IP length and desired time to live and 1040 * send to IP level. There should be a better way 1041 * to handle ttl and tos; we could keep them in 1042 * the template, but need a way to checksum without them. 1043 */ 1044 1045 #ifdef TCP_ECN 1046 /* 1047 * if peer is ECN capable, set the ECT bit in the IP header. 1048 * but don't set ECT for a pure ack, a retransmit or a window probe. 1049 */ 1050 needect = 0; 1051 if (tcp_do_ecn && (tp->t_flags & TF_ECN_PERMIT)) { 1052 if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) || 1053 (tp->t_force && len == 1)) { 1054 /* don't set ECT */ 1055 } else { 1056 needect = 1; 1057 tcpstat.tcps_ecn_sndect++; 1058 } 1059 } 1060 #endif 1061 1062 switch (tp->pf) { 1063 case 0: /*default to PF_INET*/ 1064 #ifdef INET 1065 case AF_INET: 1066 { 1067 struct ip *ip; 1068 1069 ip = mtod(m, struct ip *); 1070 ip->ip_len = htons(m->m_pkthdr.len); 1071 packetlen = m->m_pkthdr.len; 1072 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; 1073 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos; 1074 #ifdef TCP_ECN 1075 if (needect) 1076 ip->ip_tos |= IPTOS_ECN_ECT0; 1077 #endif 1078 } 1079 error = ip_output(m, tp->t_inpcb->inp_options, 1080 &tp->t_inpcb->inp_route, 1081 (ip_mtudisc ? IP_MTUDISC : 0) | 1082 (so->so_options & SO_DONTROUTE), 1083 (void *)NULL, tp->t_inpcb); 1084 break; 1085 #endif /* INET */ 1086 #ifdef INET6 1087 case AF_INET6: 1088 { 1089 struct ip6_hdr *ip6; 1090 1091 ip6 = mtod(m, struct ip6_hdr *); 1092 ip6->ip6_plen = m->m_pkthdr.len - 1093 sizeof(struct ip6_hdr); 1094 packetlen = m->m_pkthdr.len; 1095 ip6->ip6_nxt = IPPROTO_TCP; 1096 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1097 #ifdef TCP_ECN 1098 if (needect) 1099 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1100 #endif 1101 } 1102 error = ip6_output(m, tp->t_inpcb->inp_outputopts6, 1103 &tp->t_inpcb->inp_route6, 1104 (so->so_options & SO_DONTROUTE), NULL, NULL, 1105 tp->t_inpcb); 1106 break; 1107 #endif /* INET6 */ 1108 } 1109 1110 #if defined(TCP_SACK) && defined(TCP_FACK) 1111 /* Update snd_awnd to reflect the new data that was sent. */ 1112 tp->snd_awnd = tcp_seq_subtract(tp->snd_max, tp->snd_fack) + 1113 tp->retran_data; 1114 #endif /* defined(TCP_SACK) && defined(TCP_FACK) */ 1115 1116 if (error) { 1117 out: 1118 if (error == ENOBUFS) { 1119 /* 1120 * If the interface queue is full, or IP cannot 1121 * get an mbuf, trigger TCP slow start. 1122 */ 1123 tp->snd_cwnd = tp->t_maxseg; 1124 return (0); 1125 } 1126 if (error == EMSGSIZE) { 1127 /* 1128 * ip_output() will have already fixed the route 1129 * for us. tcp_mtudisc() will, as its last action, 1130 * initiate retransmission, so it is important to 1131 * not do so here. 1132 */ 1133 tcp_mtudisc(tp->t_inpcb, -1); 1134 return (0); 1135 } 1136 if ((error == EHOSTUNREACH || error == ENETDOWN) && 1137 TCPS_HAVERCVDSYN(tp->t_state)) { 1138 tp->t_softerror = error; 1139 return (0); 1140 } 1141 1142 /* Restart the delayed ACK timer, if necessary. */ 1143 if (tp->t_flags & TF_DELACK) 1144 TCP_RESTART_DELACK(tp); 1145 1146 return (error); 1147 } 1148 1149 if (packetlen > tp->t_pmtud_mtu_sent) 1150 tp->t_pmtud_mtu_sent = packetlen; 1151 1152 tcpstat.tcps_sndtotal++; 1153 if (tp->t_flags & TF_DELACK) 1154 tcpstat.tcps_delack++; 1155 1156 /* 1157 * Data sent (as far as we can tell). 1158 * If this advertises a larger window than any other segment, 1159 * then remember the size of the advertised window. 1160 * Any pending ACK has now been sent. 1161 */ 1162 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 1163 tp->rcv_adv = tp->rcv_nxt + win; 1164 tp->last_ack_sent = tp->rcv_nxt; 1165 tp->t_flags &= ~TF_ACKNOW; 1166 TCP_CLEAR_DELACK(tp); 1167 #if defined(TCP_SACK) 1168 if (sendalot && --maxburst) 1169 #else 1170 if (sendalot) 1171 #endif 1172 goto again; 1173 return (0); 1174 } 1175 1176 void 1177 tcp_setpersist(struct tcpcb *tp) 1178 { 1179 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + TCP_RTT_BASE_SHIFT); 1180 int nticks; 1181 1182 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT)) 1183 panic("tcp_output REXMT"); 1184 /* 1185 * Start/restart persistence timer. 1186 */ 1187 if (t < tp->t_rttmin) 1188 t = tp->t_rttmin; 1189 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift], 1190 TCPTV_PERSMIN, TCPTV_PERSMAX); 1191 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks); 1192 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1193 tp->t_rxtshift++; 1194 } 1195