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