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