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