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