1 /* $OpenBSD: tcp_output.c,v 1.101 2013/10/24 11:31:43 mpi 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 "pf.h" 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/kernel.h> 80 81 #include <net/route.h> 82 #include <net/if.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #include <netinet/in_pcb.h> 88 #include <netinet/ip_var.h> 89 #include <netinet/tcp.h> 90 #define TCPOUTFLAGS 91 #include <netinet/tcp_fsm.h> 92 #include <netinet/tcp_seq.h> 93 #include <netinet/tcp_timer.h> 94 #include <netinet/tcp_var.h> 95 #include <netinet/tcpip.h> 96 #include <netinet/tcp_debug.h> 97 98 #ifdef notyet 99 extern struct mbuf *m_copypack(); 100 #endif 101 102 #ifdef TCP_SACK 103 extern int tcprexmtthresh; 104 #endif 105 106 #ifdef TCP_SACK 107 #ifdef TCP_SACK_DEBUG 108 void tcp_print_holes(struct tcpcb *tp); 109 110 void 111 tcp_print_holes(struct tcpcb *tp) 112 { 113 struct sackhole *p = tp->snd_holes; 114 if (p == 0) 115 return; 116 printf("Hole report: start--end dups rxmit\n"); 117 while (p) { 118 printf("%x--%x d %d r %x\n", p->start, p->end, p->dups, 119 p->rxmit); 120 p = p->next; 121 } 122 printf("\n"); 123 } 124 #endif /* TCP_SACK_DEBUG */ 125 126 /* 127 * Returns pointer to a sackhole if there are any pending retransmissions; 128 * NULL otherwise. 129 */ 130 struct sackhole * 131 tcp_sack_output(struct tcpcb *tp) 132 { 133 struct sackhole *p; 134 135 if (!tp->sack_enable) 136 return (NULL); 137 p = tp->snd_holes; 138 while (p) { 139 #ifndef TCP_FACK 140 if (p->dups >= tcprexmtthresh && SEQ_LT(p->rxmit, p->end)) { 141 #else 142 /* In FACK, if p->dups is less than tcprexmtthresh, but 143 * snd_fack advances more than tcprextmtthresh * tp->t_maxseg, 144 * tcp_input() will try fast retransmit. This forces output. 145 */ 146 if ((p->dups >= tcprexmtthresh || 147 tp->t_dupacks == tcprexmtthresh) && 148 SEQ_LT(p->rxmit, p->end)) { 149 #endif /* TCP_FACK */ 150 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */ 151 p = p->next; 152 continue; 153 } 154 #ifdef TCP_SACK_DEBUG 155 if (p) 156 tcp_print_holes(tp); 157 #endif 158 return (p); 159 } 160 p = p->next; 161 } 162 return (NULL); 163 } 164 165 /* 166 * After a timeout, the SACK list may be rebuilt. This SACK information 167 * should be used to avoid retransmitting SACKed data. This function 168 * traverses the SACK list to see if snd_nxt should be moved forward. 169 */ 170 171 void 172 tcp_sack_adjust(struct tcpcb *tp) 173 { 174 struct sackhole *cur = tp->snd_holes; 175 if (cur == NULL) 176 return; /* No holes */ 177 if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack)) 178 return; /* We're already beyond any SACKed blocks */ 179 /* 180 * Two cases for which we want to advance snd_nxt: 181 * i) snd_nxt lies between end of one hole and beginning of another 182 * ii) snd_nxt lies between end of last hole and rcv_lastsack 183 */ 184 while (cur->next) { 185 if (SEQ_LT(tp->snd_nxt, cur->end)) 186 return; 187 if (SEQ_GEQ(tp->snd_nxt, cur->next->start)) 188 cur = cur->next; 189 else { 190 tp->snd_nxt = cur->next->start; 191 return; 192 } 193 } 194 if (SEQ_LT(tp->snd_nxt, cur->end)) 195 return; 196 tp->snd_nxt = tp->rcv_lastsack; 197 return; 198 } 199 #endif /* TCP_SACK */ 200 201 /* 202 * Tcp output routine: figure out what should be sent and send it. 203 */ 204 int 205 tcp_output(struct tcpcb *tp) 206 { 207 struct socket *so = tp->t_inpcb->inp_socket; 208 long len, win, txmaxseg; 209 int off, flags, error; 210 struct mbuf *m; 211 struct tcphdr *th; 212 u_int32_t optbuf[howmany(MAX_TCPOPTLEN, sizeof(u_int32_t))]; 213 u_char *opt = (u_char *)optbuf; 214 unsigned int optlen, hdrlen, packetlen; 215 int idle, sendalot = 0; 216 #ifdef TCP_SACK 217 int i, sack_rxmit = 0; 218 struct sackhole *p; 219 int maxburst = TCP_MAXBURST; 220 #endif 221 #ifdef TCP_SIGNATURE 222 unsigned int sigoff; 223 #endif /* TCP_SIGNATURE */ 224 #ifdef TCP_ECN 225 int needect; 226 #endif 227 228 if (tp->t_flags & TF_BLOCKOUTPUT) { 229 tp->t_flags |= TF_NEEDOUTPUT; 230 return (0); 231 } else 232 tp->t_flags &= ~TF_NEEDOUTPUT; 233 234 #if defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) 235 if (tp->sack_enable && (tp->t_flags & TF_SIGNATURE)) 236 return (EINVAL); 237 #endif /* defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) */ 238 239 /* 240 * Determine length of data that should be transmitted, 241 * and flags that will be used. 242 * If there is some data or critical controls (SYN, RST) 243 * to send, then transmit; otherwise, investigate further. 244 */ 245 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 246 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) 247 /* 248 * We have been idle for "a while" and no acks are 249 * expected to clock out any data we send -- 250 * slow start to get ack "clock" running again. 251 */ 252 tp->snd_cwnd = 2 * tp->t_maxseg; 253 254 /* remember 'idle' for next invocation of tcp_output */ 255 if (idle && soissending(so)) { 256 tp->t_flags |= TF_LASTIDLE; 257 idle = 0; 258 } else 259 tp->t_flags &= ~TF_LASTIDLE; 260 261 again: 262 #ifdef TCP_SACK 263 /* 264 * If we've recently taken a timeout, snd_max will be greater than 265 * snd_nxt. There may be SACK information that allows us to avoid 266 * resending already delivered data. Adjust snd_nxt accordingly. 267 */ 268 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 269 tcp_sack_adjust(tp); 270 #endif 271 off = tp->snd_nxt - tp->snd_una; 272 #if defined(TCP_SACK) && defined(TCP_FACK) 273 /* Normally, sendable data is limited by off < tp->snd_cwnd. 274 * But in FACK, sendable data is limited by snd_awnd < snd_cwnd, 275 * regardless of offset. 276 */ 277 if (tp->sack_enable && (tp->t_dupacks > tcprexmtthresh)) 278 win = tp->snd_wnd; 279 else 280 #endif 281 win = ulmin(tp->snd_wnd, tp->snd_cwnd); 282 283 flags = tcp_outflags[tp->t_state]; 284 285 #ifdef TCP_SACK 286 /* 287 * Send any SACK-generated retransmissions. If we're explicitly trying 288 * to send out new data (when sendalot is 1), bypass this function. 289 * If we retransmit in fast recovery mode, decrement snd_cwnd, since 290 * we're replacing a (future) new transmission with a retransmission 291 * now, and we previously incremented snd_cwnd in tcp_input(). 292 */ 293 if (tp->sack_enable && !sendalot) { 294 if (tp->t_dupacks >= tcprexmtthresh && 295 (p = tcp_sack_output(tp))) { 296 off = p->rxmit - tp->snd_una; 297 sack_rxmit = 1; 298 /* Coalesce holes into a single retransmission */ 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_enable && 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 && !soissending(so) && 415 (tp->t_flags & TF_NOPUSH) == 0) 416 goto send; 417 if (tp->t_force) 418 goto send; 419 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 420 goto send; 421 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 422 goto send; 423 #ifdef TCP_SACK 424 if (sack_rxmit) 425 goto send; 426 #endif 427 } 428 429 /* 430 * Compare available window to amount of window 431 * known to peer (as advertised window less 432 * next expected input). If the difference is at least two 433 * max size segments, or at least 50% of the maximum possible 434 * window, then want to send a window update to peer. 435 */ 436 if (win > 0) { 437 /* 438 * "adv" is the amount we can increase the window, 439 * taking into account that we are limited by 440 * TCP_MAXWIN << tp->rcv_scale. 441 */ 442 long adv = lmin(win, (long)TCP_MAXWIN << tp->rcv_scale) - 443 (tp->rcv_adv - tp->rcv_nxt); 444 445 if (adv >= (long) (2 * tp->t_maxseg)) 446 goto send; 447 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 448 goto send; 449 } 450 451 /* 452 * Send if we owe peer an ACK. 453 */ 454 if (tp->t_flags & TF_ACKNOW) 455 goto send; 456 if (flags & (TH_SYN|TH_RST)) 457 goto send; 458 if (SEQ_GT(tp->snd_up, tp->snd_una)) 459 goto send; 460 /* 461 * If our state indicates that FIN should be sent 462 * and we have not yet done so, or we're retransmitting the FIN, 463 * then we need to send. 464 */ 465 if (flags & TH_FIN && 466 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 467 goto send; 468 #ifdef TCP_SACK 469 /* 470 * In SACK, it is possible for tcp_output to fail to send a segment 471 * after the retransmission timer has been turned off. Make sure 472 * that the retransmission timer is set. 473 */ 474 if (SEQ_GT(tp->snd_max, tp->snd_una) && 475 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 476 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 477 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 478 return (0); 479 } 480 #endif /* TCP_SACK */ 481 482 /* 483 * TCP window updates are not reliable, rather a polling protocol 484 * using ``persist'' packets is used to insure receipt of window 485 * updates. The three ``states'' for the output side are: 486 * idle not doing retransmits or persists 487 * persisting to move a small or zero window 488 * (re)transmitting and thereby not persisting 489 * 490 * tp->t_timer[TCPT_PERSIST] 491 * is set when we are in persist state. 492 * tp->t_force 493 * is set when we are called to send a persist packet. 494 * tp->t_timer[TCPT_REXMT] 495 * is set when we are retransmitting 496 * The output side is idle when both timers are zero. 497 * 498 * If send window is too small, there is data to transmit, and no 499 * retransmit or persist is pending, then go to persist state. 500 * If nothing happens soon, send when timer expires: 501 * if window is nonzero, transmit what we can, 502 * otherwise force out a byte. 503 */ 504 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 505 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 506 tp->t_rxtshift = 0; 507 tcp_setpersist(tp); 508 } 509 510 /* 511 * No reason to send a segment, just return. 512 */ 513 return (0); 514 515 send: 516 /* 517 * Before ESTABLISHED, force sending of initial options 518 * unless TCP set not to do any options. 519 * NOTE: we assume that the IP/TCP header plus TCP options 520 * always fit in a single mbuf, leaving room for a maximum 521 * link header, i.e. 522 * max_linkhdr + sizeof(network header) + sizeof(struct tcphdr + 523 * optlen <= MHLEN 524 */ 525 optlen = 0; 526 527 switch (tp->pf) { 528 case 0: /*default to PF_INET*/ 529 #ifdef INET 530 case PF_INET: 531 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr); 532 break; 533 #endif /* INET */ 534 #ifdef INET6 535 case PF_INET6: 536 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 537 break; 538 #endif /* INET6 */ 539 default: 540 return (EPFNOSUPPORT); 541 } 542 543 if (flags & TH_SYN) { 544 tp->snd_nxt = tp->iss; 545 if ((tp->t_flags & TF_NOOPT) == 0) { 546 u_int16_t mss; 547 548 opt[0] = TCPOPT_MAXSEG; 549 opt[1] = 4; 550 mss = htons((u_int16_t) tcp_mss(tp, 0)); 551 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss)); 552 optlen = 4; 553 554 if (flags & TH_ACK) 555 tcp_mss_update(tp); 556 #ifdef TCP_SACK 557 /* 558 * If this is the first SYN of connection (not a SYN 559 * ACK), include SACK_PERMIT_HDR option. If this is a 560 * SYN ACK, include SACK_PERMIT_HDR option if peer has 561 * already done so. 562 */ 563 if (tp->sack_enable && ((flags & TH_ACK) == 0 || 564 (tp->t_flags & TF_SACK_PERMIT))) { 565 *((u_int32_t *) (opt + optlen)) = 566 htonl(TCPOPT_SACK_PERMIT_HDR); 567 optlen += 4; 568 } 569 #endif 570 571 if ((tp->t_flags & TF_REQ_SCALE) && 572 ((flags & TH_ACK) == 0 || 573 (tp->t_flags & TF_RCVD_SCALE))) { 574 *((u_int32_t *) (opt + optlen)) = htonl( 575 TCPOPT_NOP << 24 | 576 TCPOPT_WINDOW << 16 | 577 TCPOLEN_WINDOW << 8 | 578 tp->request_r_scale); 579 optlen += 4; 580 } 581 } 582 } 583 584 /* 585 * Send a timestamp and echo-reply if this is a SYN and our side 586 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 587 * and our peer have sent timestamps in our SYN's. 588 */ 589 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 590 (flags & TH_RST) == 0 && 591 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 592 (tp->t_flags & TF_RCVD_TSTMP))) { 593 u_int32_t *lp = (u_int32_t *)(opt + optlen); 594 595 /* Form timestamp option as shown in appendix A of RFC 1323. */ 596 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 597 *lp++ = htonl(tcp_now + tp->ts_modulate); 598 *lp = htonl(tp->ts_recent); 599 optlen += TCPOLEN_TSTAMP_APPA; 600 601 /* Set receive buffer autosizing timestamp. */ 602 if (tp->rfbuf_ts == 0) 603 tp->rfbuf_ts = tcp_now; 604 605 } 606 607 #ifdef TCP_SIGNATURE 608 if (tp->t_flags & TF_SIGNATURE) { 609 u_int8_t *bp = (u_int8_t *)(opt + optlen); 610 611 /* Send signature option */ 612 *(bp++) = TCPOPT_SIGNATURE; 613 *(bp++) = TCPOLEN_SIGNATURE; 614 sigoff = optlen + 2; 615 616 { 617 unsigned int i; 618 619 for (i = 0; i < 16; i++) 620 *(bp++) = 0; 621 } 622 623 624 /* Pad options list to the next 32 bit boundary and 625 * terminate it. 626 */ 627 *bp++ = TCPOPT_NOP; 628 *bp++ = TCPOPT_NOP; 629 630 optlen += TCPOLEN_SIGLEN; 631 } 632 #endif /* TCP_SIGNATURE */ 633 634 #ifdef TCP_SACK 635 /* 636 * Send SACKs if necessary. This should be the last option processed. 637 * Only as many SACKs are sent as are permitted by the maximum options 638 * size. No more than three SACKs are sent. 639 */ 640 if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED && 641 (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT && 642 tp->rcv_numsacks) { 643 u_int32_t *lp = (u_int32_t *)(opt + optlen); 644 u_int32_t *olp = lp++; 645 int count = 0; /* actual number of SACKs inserted */ 646 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK; 647 648 tcpstat.tcps_sack_snd_opts++; 649 maxsack = min(maxsack, TCP_MAX_SACK); 650 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) { 651 struct sackblk sack = tp->sackblks[i]; 652 if (sack.start == 0 && sack.end == 0) 653 continue; 654 *lp++ = htonl(sack.start); 655 *lp++ = htonl(sack.end); 656 count++; 657 } 658 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2)); 659 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */ 660 } 661 #endif /* TCP_SACK */ 662 663 #ifdef DIAGNOSTIC 664 if (optlen > MAX_TCPOPTLEN) 665 panic("tcp_output: options too long"); 666 #endif /* DIAGNOSTIC */ 667 668 hdrlen += optlen; 669 670 /* 671 * Adjust data length if insertion of options will 672 * bump the packet length beyond the t_maxopd length. 673 */ 674 if (len > tp->t_maxopd - optlen) { 675 len = tp->t_maxopd - optlen; 676 sendalot = 1; 677 flags &= ~TH_FIN; 678 } 679 680 #ifdef DIAGNOSTIC 681 if (max_linkhdr + hdrlen > MCLBYTES) 682 panic("tcphdr too big"); 683 #endif 684 685 /* 686 * Grab a header mbuf, attaching a copy of data to 687 * be transmitted, and initialize the header from 688 * the template for sends on this connection. 689 */ 690 if (len) { 691 if (tp->t_force && len == 1) 692 tcpstat.tcps_sndprobe++; 693 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 694 tcpstat.tcps_sndrexmitpack++; 695 tcpstat.tcps_sndrexmitbyte += len; 696 } else { 697 tcpstat.tcps_sndpack++; 698 tcpstat.tcps_sndbyte += len; 699 } 700 #ifdef notyet 701 if ((m = m_copypack(so->so_snd.sb_mb, off, 702 (int)len, max_linkhdr + hdrlen)) == 0) { 703 error = ENOBUFS; 704 goto out; 705 } 706 /* 707 * m_copypack left space for our hdr; use it. 708 */ 709 m->m_len += hdrlen; 710 m->m_data -= hdrlen; 711 #else 712 MGETHDR(m, M_DONTWAIT, MT_HEADER); 713 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 714 MCLGET(m, M_DONTWAIT); 715 if ((m->m_flags & M_EXT) == 0) { 716 m_freem(m); 717 m = NULL; 718 } 719 } 720 if (m == NULL) { 721 error = ENOBUFS; 722 goto out; 723 } 724 m->m_data += max_linkhdr; 725 m->m_len = hdrlen; 726 if (len <= M_TRAILINGSPACE(m)) { 727 m_copydata(so->so_snd.sb_mb, off, (int) len, 728 mtod(m, caddr_t) + hdrlen); 729 m->m_len += len; 730 } else { 731 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 732 if (m->m_next == 0) { 733 (void) m_free(m); 734 error = ENOBUFS; 735 goto out; 736 } 737 } 738 #endif 739 /* 740 * If we're sending everything we've got, set PUSH. 741 * (This will keep happy those implementations which only 742 * give data to the user when a buffer fills or 743 * a PUSH comes in.) 744 */ 745 if (off + len == so->so_snd.sb_cc && !soissending(so)) 746 flags |= TH_PUSH; 747 } else { 748 if (tp->t_flags & TF_ACKNOW) 749 tcpstat.tcps_sndacks++; 750 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 751 tcpstat.tcps_sndctrl++; 752 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 753 tcpstat.tcps_sndurg++; 754 else 755 tcpstat.tcps_sndwinup++; 756 757 MGETHDR(m, M_DONTWAIT, MT_HEADER); 758 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 759 MCLGET(m, M_DONTWAIT); 760 if ((m->m_flags & M_EXT) == 0) { 761 m_freem(m); 762 m = NULL; 763 } 764 } 765 if (m == NULL) { 766 error = ENOBUFS; 767 goto out; 768 } 769 m->m_data += max_linkhdr; 770 m->m_len = hdrlen; 771 } 772 m->m_pkthdr.rcvif = (struct ifnet *)0; 773 m->m_pkthdr.len = hdrlen + len; 774 775 if (!tp->t_template) 776 panic("tcp_output"); 777 #ifdef DIAGNOSTIC 778 if (tp->t_template->m_len != hdrlen - optlen) 779 panic("tcp_output: template len != hdrlen - optlen"); 780 #endif /* DIAGNOSTIC */ 781 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), 782 tp->t_template->m_len); 783 th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len - 784 sizeof(struct tcphdr)); 785 786 /* 787 * Fill in fields, remembering maximum advertised 788 * window for use in delaying messages about window sizes. 789 * If resending a FIN, be sure not to use a new sequence number. 790 */ 791 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) && 792 (tp->snd_nxt == tp->snd_max)) 793 tp->snd_nxt--; 794 /* 795 * If we are doing retransmissions, then snd_nxt will 796 * not reflect the first unsent octet. For ACK only 797 * packets, we do not want the sequence number of the 798 * retransmitted packet, we want the sequence number 799 * of the next unsent octet. So, if there is no data 800 * (and no SYN or FIN), use snd_max instead of snd_nxt 801 * when filling in ti_seq. But if we are in persist 802 * state, snd_max might reflect one byte beyond the 803 * right edge of the window, so use snd_nxt in that 804 * case, since we know we aren't doing a retransmission. 805 * (retransmit and persist are mutually exclusive...) 806 */ 807 if (len || (flags & (TH_SYN|TH_FIN)) || TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) 808 th->th_seq = htonl(tp->snd_nxt); 809 else 810 th->th_seq = htonl(tp->snd_max); 811 812 #ifdef TCP_SACK 813 if (sack_rxmit) { 814 /* 815 * If sendalot was turned on (due to option stuffing), turn it 816 * off. Properly set th_seq field. Advance the ret'x pointer 817 * by len. 818 */ 819 if (sendalot) 820 sendalot = 0; 821 th->th_seq = htonl(p->rxmit); 822 p->rxmit += len; 823 #if defined(TCP_SACK) && defined(TCP_FACK) 824 tp->retran_data += len; 825 #endif /* TCP_FACK */ 826 tcpstat.tcps_sack_rexmits++; 827 tcpstat.tcps_sack_rexmit_bytes += len; 828 } 829 #endif /* TCP_SACK */ 830 831 th->th_ack = htonl(tp->rcv_nxt); 832 if (optlen) { 833 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen); 834 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 835 } 836 #ifdef TCP_ECN 837 if (tcp_do_ecn) { 838 /* 839 * if we have received congestion experienced segs, 840 * set ECE bit. 841 */ 842 if (tp->t_flags & TF_RCVD_CE) { 843 flags |= TH_ECE; 844 tcpstat.tcps_ecn_sndece++; 845 } 846 if (!(tp->t_flags & TF_DISABLE_ECN)) { 847 /* 848 * if this is a SYN seg, set ECE and CWR. 849 * set only ECE for SYN-ACK if peer supports ECN. 850 */ 851 if ((flags & (TH_SYN|TH_ACK)) == TH_SYN) 852 flags |= (TH_ECE|TH_CWR); 853 else if ((tp->t_flags & TF_ECN_PERMIT) && 854 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) 855 flags |= TH_ECE; 856 } 857 /* 858 * if we have reduced the congestion window, notify 859 * the peer by setting CWR bit. 860 */ 861 if ((tp->t_flags & TF_ECN_PERMIT) && 862 (tp->t_flags & TF_SEND_CWR)) { 863 flags |= TH_CWR; 864 tp->t_flags &= ~TF_SEND_CWR; 865 tcpstat.tcps_ecn_sndcwr++; 866 } 867 } 868 #endif 869 th->th_flags = flags; 870 871 /* 872 * Calculate receive window. Don't shrink window, 873 * but avoid silly window syndrome. 874 */ 875 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 876 win = 0; 877 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 878 win = (long)TCP_MAXWIN << tp->rcv_scale; 879 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt)) 880 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt); 881 if (flags & TH_RST) 882 win = 0; 883 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); 884 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 885 u_int32_t urp = tp->snd_up - tp->snd_nxt; 886 if (urp > IP_MAXPACKET) 887 urp = IP_MAXPACKET; 888 th->th_urp = htons((u_int16_t)urp); 889 th->th_flags |= TH_URG; 890 } else 891 /* 892 * If no urgent pointer to send, then we pull 893 * the urgent pointer to the left edge of the send window 894 * so that it doesn't drift into the send window on sequence 895 * number wraparound. 896 */ 897 tp->snd_up = tp->snd_una; /* drag it along */ 898 899 #ifdef TCP_SIGNATURE 900 if (tp->t_flags & TF_SIGNATURE) { 901 int iphlen; 902 union sockaddr_union src, dst; 903 struct tdb *tdb; 904 905 bzero(&src, sizeof(union sockaddr_union)); 906 bzero(&dst, sizeof(union sockaddr_union)); 907 908 switch (tp->pf) { 909 case 0: /*default to PF_INET*/ 910 #ifdef INET 911 case AF_INET: 912 iphlen = sizeof(struct ip); 913 src.sa.sa_len = sizeof(struct sockaddr_in); 914 src.sa.sa_family = AF_INET; 915 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 916 dst.sa.sa_len = sizeof(struct sockaddr_in); 917 dst.sa.sa_family = AF_INET; 918 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 919 break; 920 #endif /* INET */ 921 #ifdef INET6 922 case AF_INET6: 923 iphlen = sizeof(struct ip6_hdr); 924 src.sa.sa_len = sizeof(struct sockaddr_in6); 925 src.sa.sa_family = AF_INET6; 926 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 927 dst.sa.sa_len = sizeof(struct sockaddr_in6); 928 dst.sa.sa_family = AF_INET6; 929 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 930 break; 931 #endif /* INET6 */ 932 } 933 934 tdb = gettdbbysrcdst(rtable_l2(tp->t_inpcb->inp_rtableid), 935 0, &src, &dst, IPPROTO_TCP); 936 if (tdb == NULL) 937 return (EPERM); 938 939 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0, 940 mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0) 941 return (EINVAL); 942 } 943 #endif /* TCP_SIGNATURE */ 944 945 /* Defer checksumming until later (ip_output() or hardware) */ 946 m->m_pkthdr.csum_flags |= M_TCP_CSUM_OUT; 947 948 /* 949 * In transmit state, time the transmission and arrange for 950 * the retransmit. In persist state, just set snd_max. 951 */ 952 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 953 tcp_seq startseq = tp->snd_nxt; 954 955 /* 956 * Advance snd_nxt over sequence space of this segment. 957 */ 958 if (flags & (TH_SYN|TH_FIN)) { 959 if (flags & TH_SYN) 960 tp->snd_nxt++; 961 if (flags & TH_FIN) { 962 tp->snd_nxt++; 963 tp->t_flags |= TF_SENTFIN; 964 } 965 } 966 #ifdef TCP_SACK 967 if (tp->sack_enable) { 968 if (sack_rxmit && (p->rxmit != tp->snd_nxt)) { 969 goto timer; 970 } 971 } 972 #endif 973 tp->snd_nxt += len; 974 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 975 tp->snd_max = tp->snd_nxt; 976 /* 977 * Time this transmission if not a retransmission and 978 * not currently timing anything. 979 */ 980 if (tp->t_rtttime == 0) { 981 tp->t_rtttime = tcp_now; 982 tp->t_rtseq = startseq; 983 tcpstat.tcps_segstimed++; 984 } 985 } 986 987 /* 988 * Set retransmit timer if not currently set, 989 * and not doing an ack or a keep-alive probe. 990 * Initial value for retransmit timer is smoothed 991 * round-trip time + 2 * round-trip time variance. 992 * Initialize shift counter which is used for backoff 993 * of retransmit time. 994 */ 995 #ifdef TCP_SACK 996 timer: 997 if (tp->sack_enable && sack_rxmit && 998 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 999 tp->snd_nxt != tp->snd_max) { 1000 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1001 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1002 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1003 tp->t_rxtshift = 0; 1004 } 1005 } 1006 #endif 1007 1008 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1009 tp->snd_nxt != tp->snd_una) { 1010 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1011 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1012 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1013 tp->t_rxtshift = 0; 1014 } 1015 } 1016 } else 1017 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 1018 tp->snd_max = tp->snd_nxt + len; 1019 1020 tcp_update_sndspace(tp); 1021 1022 /* 1023 * Trace. 1024 */ 1025 if (so->so_options & SO_DEBUG) 1026 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, caddr_t), 0, 1027 len); 1028 1029 /* 1030 * Fill in IP length and desired time to live and 1031 * send to IP level. There should be a better way 1032 * to handle ttl and tos; we could keep them in 1033 * the template, but need a way to checksum without them. 1034 */ 1035 1036 #ifdef TCP_ECN 1037 /* 1038 * if peer is ECN capable, set the ECT bit in the IP header. 1039 * but don't set ECT for a pure ack, a retransmit or a window probe. 1040 */ 1041 needect = 0; 1042 if (tcp_do_ecn && (tp->t_flags & TF_ECN_PERMIT)) { 1043 if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) || 1044 (tp->t_force && len == 1)) { 1045 /* don't set ECT */ 1046 } else { 1047 needect = 1; 1048 tcpstat.tcps_ecn_sndect++; 1049 } 1050 } 1051 #endif 1052 1053 /* force routing domain */ 1054 m->m_pkthdr.rdomain = tp->t_inpcb->inp_rtableid; 1055 1056 #if NPF > 0 1057 m->m_pkthdr.pf.inp = tp->t_inpcb; 1058 #endif 1059 1060 switch (tp->pf) { 1061 case 0: /*default to PF_INET*/ 1062 #ifdef INET 1063 case AF_INET: 1064 { 1065 struct ip *ip; 1066 1067 ip = mtod(m, struct ip *); 1068 ip->ip_len = htons(m->m_pkthdr.len); 1069 packetlen = m->m_pkthdr.len; 1070 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; 1071 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos; 1072 #ifdef TCP_ECN 1073 if (needect) 1074 ip->ip_tos |= IPTOS_ECN_ECT0; 1075 #endif 1076 } 1077 error = ip_output(m, tp->t_inpcb->inp_options, 1078 &tp->t_inpcb->inp_route, 1079 (ip_mtudisc ? IP_MTUDISC : 0) | 1080 (so->so_options & SO_DONTROUTE), 1081 (void *)NULL, tp->t_inpcb); 1082 break; 1083 #endif /* INET */ 1084 #ifdef INET6 1085 case AF_INET6: 1086 { 1087 struct ip6_hdr *ip6; 1088 1089 ip6 = mtod(m, struct ip6_hdr *); 1090 ip6->ip6_plen = m->m_pkthdr.len - 1091 sizeof(struct ip6_hdr); 1092 packetlen = m->m_pkthdr.len; 1093 ip6->ip6_nxt = IPPROTO_TCP; 1094 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1095 #ifdef TCP_ECN 1096 if (needect) 1097 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1098 #endif 1099 } 1100 error = ip6_output(m, tp->t_inpcb->inp_outputopts6, 1101 &tp->t_inpcb->inp_route6, 1102 (so->so_options & SO_DONTROUTE), NULL, NULL, 1103 tp->t_inpcb); 1104 break; 1105 #endif /* INET6 */ 1106 } 1107 1108 #if defined(TCP_SACK) && defined(TCP_FACK) 1109 /* Update snd_awnd to reflect the new data that was sent. */ 1110 tp->snd_awnd = tcp_seq_subtract(tp->snd_max, tp->snd_fack) + 1111 tp->retran_data; 1112 #endif /* defined(TCP_SACK) && defined(TCP_FACK) */ 1113 1114 if (error) { 1115 out: 1116 if (error == ENOBUFS) { 1117 /* 1118 * If the interface queue is full, or IP cannot 1119 * get an mbuf, trigger TCP slow start. 1120 */ 1121 tp->snd_cwnd = tp->t_maxseg; 1122 return (0); 1123 } 1124 if (error == EMSGSIZE) { 1125 /* 1126 * ip_output() will have already fixed the route 1127 * for us. tcp_mtudisc() will, as its last action, 1128 * initiate retransmission, so it is important to 1129 * not do so here. 1130 */ 1131 tcp_mtudisc(tp->t_inpcb, -1); 1132 return (0); 1133 } 1134 if (error == EACCES) /* translate pf(4) error for userland */ 1135 error = EHOSTUNREACH; 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