1 /* $OpenBSD: tcp_output.c,v 1.116 2015/12/05 10:52:26 tedu 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 #endif 736 /* 737 * If we're sending everything we've got, set PUSH. 738 * (This will keep happy those implementations which only 739 * give data to the user when a buffer fills or 740 * a PUSH comes in.) 741 */ 742 if (off + len == so->so_snd.sb_cc && !soissending(so)) 743 flags |= TH_PUSH; 744 } else { 745 if (tp->t_flags & TF_ACKNOW) 746 tcpstat.tcps_sndacks++; 747 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 748 tcpstat.tcps_sndctrl++; 749 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 750 tcpstat.tcps_sndurg++; 751 else 752 tcpstat.tcps_sndwinup++; 753 754 MGETHDR(m, M_DONTWAIT, MT_HEADER); 755 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 756 MCLGET(m, M_DONTWAIT); 757 if ((m->m_flags & M_EXT) == 0) { 758 m_freem(m); 759 m = NULL; 760 } 761 } 762 if (m == NULL) { 763 error = ENOBUFS; 764 goto out; 765 } 766 m->m_data += max_linkhdr; 767 m->m_len = hdrlen; 768 } 769 m->m_pkthdr.ph_ifidx = 0; 770 m->m_pkthdr.len = hdrlen + len; 771 772 if (!tp->t_template) 773 panic("tcp_output"); 774 #ifdef DIAGNOSTIC 775 if (tp->t_template->m_len != hdrlen - optlen) 776 panic("tcp_output: template len != hdrlen - optlen"); 777 #endif /* DIAGNOSTIC */ 778 memcpy(mtod(m, caddr_t), mtod(tp->t_template, caddr_t), 779 tp->t_template->m_len); 780 th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len - 781 sizeof(struct tcphdr)); 782 783 /* 784 * Fill in fields, remembering maximum advertised 785 * window for use in delaying messages about window sizes. 786 * If resending a FIN, be sure not to use a new sequence number. 787 */ 788 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) && 789 (tp->snd_nxt == tp->snd_max)) 790 tp->snd_nxt--; 791 /* 792 * If we are doing retransmissions, then snd_nxt will 793 * not reflect the first unsent octet. For ACK only 794 * packets, we do not want the sequence number of the 795 * retransmitted packet, we want the sequence number 796 * of the next unsent octet. So, if there is no data 797 * (and no SYN or FIN), use snd_max instead of snd_nxt 798 * when filling in ti_seq. But if we are in persist 799 * state, snd_max might reflect one byte beyond the 800 * right edge of the window, so use snd_nxt in that 801 * case, since we know we aren't doing a retransmission. 802 * (retransmit and persist are mutually exclusive...) 803 */ 804 if (len || (flags & (TH_SYN|TH_FIN)) || TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) 805 th->th_seq = htonl(tp->snd_nxt); 806 else 807 th->th_seq = htonl(tp->snd_max); 808 809 #ifdef TCP_SACK 810 if (sack_rxmit) { 811 /* 812 * If sendalot was turned on (due to option stuffing), turn it 813 * off. Properly set th_seq field. Advance the ret'x pointer 814 * by len. 815 */ 816 if (sendalot) 817 sendalot = 0; 818 th->th_seq = htonl(p->rxmit); 819 p->rxmit += len; 820 #if defined(TCP_SACK) && defined(TCP_FACK) 821 tp->retran_data += len; 822 #endif /* TCP_FACK */ 823 tcpstat.tcps_sack_rexmits++; 824 tcpstat.tcps_sack_rexmit_bytes += len; 825 } 826 #endif /* TCP_SACK */ 827 828 th->th_ack = htonl(tp->rcv_nxt); 829 if (optlen) { 830 memcpy(th + 1, opt, optlen); 831 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 832 } 833 #ifdef TCP_ECN 834 if (tcp_do_ecn) { 835 /* 836 * if we have received congestion experienced segs, 837 * set ECE bit. 838 */ 839 if (tp->t_flags & TF_RCVD_CE) { 840 flags |= TH_ECE; 841 tcpstat.tcps_ecn_sndece++; 842 } 843 if (!(tp->t_flags & TF_DISABLE_ECN)) { 844 /* 845 * if this is a SYN seg, set ECE and CWR. 846 * set only ECE for SYN-ACK if peer supports ECN. 847 */ 848 if ((flags & (TH_SYN|TH_ACK)) == TH_SYN) 849 flags |= (TH_ECE|TH_CWR); 850 else if ((tp->t_flags & TF_ECN_PERMIT) && 851 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) 852 flags |= TH_ECE; 853 } 854 /* 855 * if we have reduced the congestion window, notify 856 * the peer by setting CWR bit. 857 */ 858 if ((tp->t_flags & TF_ECN_PERMIT) && 859 (tp->t_flags & TF_SEND_CWR)) { 860 flags |= TH_CWR; 861 tp->t_flags &= ~TF_SEND_CWR; 862 tcpstat.tcps_ecn_sndcwr++; 863 } 864 } 865 #endif 866 th->th_flags = flags; 867 868 /* 869 * Calculate receive window. Don't shrink window, 870 * but avoid silly window syndrome. 871 */ 872 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 873 win = 0; 874 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 875 win = (long)TCP_MAXWIN << tp->rcv_scale; 876 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt)) 877 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt); 878 if (flags & TH_RST) 879 win = 0; 880 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); 881 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 882 u_int32_t urp = tp->snd_up - tp->snd_nxt; 883 if (urp > IP_MAXPACKET) 884 urp = IP_MAXPACKET; 885 th->th_urp = htons((u_int16_t)urp); 886 th->th_flags |= TH_URG; 887 } else 888 /* 889 * If no urgent pointer to send, then we pull 890 * the urgent pointer to the left edge of the send window 891 * so that it doesn't drift into the send window on sequence 892 * number wraparound. 893 */ 894 tp->snd_up = tp->snd_una; /* drag it along */ 895 896 #ifdef TCP_SIGNATURE 897 if (tp->t_flags & TF_SIGNATURE) { 898 int iphlen; 899 union sockaddr_union src, dst; 900 struct tdb *tdb; 901 902 bzero(&src, sizeof(union sockaddr_union)); 903 bzero(&dst, sizeof(union sockaddr_union)); 904 905 switch (tp->pf) { 906 case 0: /*default to PF_INET*/ 907 case AF_INET: 908 iphlen = sizeof(struct ip); 909 src.sa.sa_len = sizeof(struct sockaddr_in); 910 src.sa.sa_family = AF_INET; 911 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 912 dst.sa.sa_len = sizeof(struct sockaddr_in); 913 dst.sa.sa_family = AF_INET; 914 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 915 break; 916 #ifdef INET6 917 case AF_INET6: 918 iphlen = sizeof(struct ip6_hdr); 919 src.sa.sa_len = sizeof(struct sockaddr_in6); 920 src.sa.sa_family = AF_INET6; 921 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 922 dst.sa.sa_len = sizeof(struct sockaddr_in6); 923 dst.sa.sa_family = AF_INET6; 924 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 925 break; 926 #endif /* INET6 */ 927 } 928 929 tdb = gettdbbysrcdst(rtable_l2(tp->t_inpcb->inp_rtableid), 930 0, &src, &dst, IPPROTO_TCP); 931 if (tdb == NULL) 932 return (EPERM); 933 934 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0, 935 mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0) 936 return (EINVAL); 937 } 938 #endif /* TCP_SIGNATURE */ 939 940 /* Defer checksumming until later (ip_output() or hardware) */ 941 m->m_pkthdr.csum_flags |= M_TCP_CSUM_OUT; 942 943 /* 944 * In transmit state, time the transmission and arrange for 945 * the retransmit. In persist state, just set snd_max. 946 */ 947 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 948 tcp_seq startseq = tp->snd_nxt; 949 950 /* 951 * Advance snd_nxt over sequence space of this segment. 952 */ 953 if (flags & (TH_SYN|TH_FIN)) { 954 if (flags & TH_SYN) 955 tp->snd_nxt++; 956 if (flags & TH_FIN) { 957 tp->snd_nxt++; 958 tp->t_flags |= TF_SENTFIN; 959 } 960 } 961 #ifdef TCP_SACK 962 if (tp->sack_enable) { 963 if (sack_rxmit && (p->rxmit != tp->snd_nxt)) { 964 goto timer; 965 } 966 } 967 #endif 968 tp->snd_nxt += len; 969 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 970 tp->snd_max = tp->snd_nxt; 971 /* 972 * Time this transmission if not a retransmission and 973 * not currently timing anything. 974 */ 975 if (tp->t_rtttime == 0) { 976 tp->t_rtttime = tcp_now; 977 tp->t_rtseq = startseq; 978 tcpstat.tcps_segstimed++; 979 } 980 } 981 982 /* 983 * Set retransmit timer if not currently set, 984 * and not doing an ack or a keep-alive probe. 985 * Initial value for retransmit timer is smoothed 986 * round-trip time + 2 * round-trip time variance. 987 * Initialize shift counter which is used for backoff 988 * of retransmit time. 989 */ 990 #ifdef TCP_SACK 991 timer: 992 if (tp->sack_enable && sack_rxmit && 993 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 994 tp->snd_nxt != tp->snd_max) { 995 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 996 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 997 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 998 tp->t_rxtshift = 0; 999 } 1000 } 1001 #endif 1002 1003 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1004 tp->snd_nxt != tp->snd_una) { 1005 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1006 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 1007 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 1008 tp->t_rxtshift = 0; 1009 } 1010 } 1011 1012 if (len == 0 && so->so_snd.sb_cc && 1013 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1014 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 1015 /* 1016 * Avoid a situation where we do not set persist timer 1017 * after a zero window condition. For example: 1018 * 1) A -> B: packet with enough data to fill the window 1019 * 2) B -> A: ACK for #1 + new data (0 window 1020 * advertisement) 1021 * 3) A -> B: ACK for #2, 0 len packet 1022 * 1023 * In this case, A will not activate the persist timer, 1024 * because it chose to send a packet. Unless tcp_output 1025 * is called for some other reason (delayed ack timer, 1026 * another input packet from B, socket syscall), A will 1027 * not send zero window probes. 1028 * 1029 * So, if you send a 0-length packet, but there is data 1030 * in the socket buffer, and neither the rexmt or 1031 * persist timer is already set, then activate the 1032 * persist timer. 1033 */ 1034 tp->t_rxtshift = 0; 1035 tcp_setpersist(tp); 1036 } 1037 } else 1038 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 1039 tp->snd_max = tp->snd_nxt + len; 1040 1041 tcp_update_sndspace(tp); 1042 1043 /* 1044 * Trace. 1045 */ 1046 if (so->so_options & SO_DEBUG) 1047 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, caddr_t), 0, 1048 len); 1049 1050 /* 1051 * Fill in IP length and desired time to live and 1052 * send to IP level. There should be a better way 1053 * to handle ttl and tos; we could keep them in 1054 * the template, but need a way to checksum without them. 1055 */ 1056 1057 #ifdef TCP_ECN 1058 /* 1059 * if peer is ECN capable, set the ECT bit in the IP header. 1060 * but don't set ECT for a pure ack, a retransmit or a window probe. 1061 */ 1062 needect = 0; 1063 if (tcp_do_ecn && (tp->t_flags & TF_ECN_PERMIT)) { 1064 if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) || 1065 (tp->t_force && len == 1)) { 1066 /* don't set ECT */ 1067 } else { 1068 needect = 1; 1069 tcpstat.tcps_ecn_sndect++; 1070 } 1071 } 1072 #endif 1073 1074 /* force routing table */ 1075 m->m_pkthdr.ph_rtableid = tp->t_inpcb->inp_rtableid; 1076 1077 #if NPF > 0 1078 m->m_pkthdr.pf.inp = tp->t_inpcb; 1079 #endif 1080 1081 switch (tp->pf) { 1082 case 0: /*default to PF_INET*/ 1083 case AF_INET: 1084 { 1085 struct ip *ip; 1086 1087 ip = mtod(m, struct ip *); 1088 ip->ip_len = htons(m->m_pkthdr.len); 1089 packetlen = m->m_pkthdr.len; 1090 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; 1091 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos; 1092 #ifdef TCP_ECN 1093 if (needect) 1094 ip->ip_tos |= IPTOS_ECN_ECT0; 1095 #endif 1096 } 1097 error = ip_output(m, tp->t_inpcb->inp_options, 1098 &tp->t_inpcb->inp_route, 1099 (ip_mtudisc ? IP_MTUDISC : 0), NULL, tp->t_inpcb, 0); 1100 break; 1101 #ifdef INET6 1102 case AF_INET6: 1103 { 1104 struct ip6_hdr *ip6; 1105 1106 ip6 = mtod(m, struct ip6_hdr *); 1107 ip6->ip6_plen = m->m_pkthdr.len - 1108 sizeof(struct ip6_hdr); 1109 packetlen = m->m_pkthdr.len; 1110 ip6->ip6_nxt = IPPROTO_TCP; 1111 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb); 1112 #ifdef TCP_ECN 1113 if (needect) 1114 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1115 #endif 1116 } 1117 error = ip6_output(m, tp->t_inpcb->inp_outputopts6, 1118 &tp->t_inpcb->inp_route6, 1119 0, NULL, tp->t_inpcb); 1120 break; 1121 #endif /* INET6 */ 1122 } 1123 1124 #if defined(TCP_SACK) && defined(TCP_FACK) 1125 /* Update snd_awnd to reflect the new data that was sent. */ 1126 tp->snd_awnd = tcp_seq_subtract(tp->snd_max, tp->snd_fack) + 1127 tp->retran_data; 1128 #endif /* defined(TCP_SACK) && defined(TCP_FACK) */ 1129 1130 if (error) { 1131 out: 1132 if (error == ENOBUFS) { 1133 /* 1134 * If the interface queue is full, or IP cannot 1135 * get an mbuf, trigger TCP slow start. 1136 */ 1137 tp->snd_cwnd = tp->t_maxseg; 1138 return (0); 1139 } 1140 if (error == EMSGSIZE) { 1141 /* 1142 * ip_output() will have already fixed the route 1143 * for us. tcp_mtudisc() will, as its last action, 1144 * initiate retransmission, so it is important to 1145 * not do so here. 1146 */ 1147 tcp_mtudisc(tp->t_inpcb, -1); 1148 return (0); 1149 } 1150 if (error == EACCES) /* translate pf(4) error for userland */ 1151 error = EHOSTUNREACH; 1152 if ((error == EHOSTUNREACH || error == ENETDOWN) && 1153 TCPS_HAVERCVDSYN(tp->t_state)) { 1154 tp->t_softerror = error; 1155 return (0); 1156 } 1157 1158 /* Restart the delayed ACK timer, if necessary. */ 1159 if (tp->t_flags & TF_DELACK) 1160 TCP_RESTART_DELACK(tp); 1161 1162 return (error); 1163 } 1164 1165 if (packetlen > tp->t_pmtud_mtu_sent) 1166 tp->t_pmtud_mtu_sent = packetlen; 1167 1168 tcpstat.tcps_sndtotal++; 1169 if (tp->t_flags & TF_DELACK) 1170 tcpstat.tcps_delack++; 1171 1172 /* 1173 * Data sent (as far as we can tell). 1174 * If this advertises a larger window than any other segment, 1175 * then remember the size of the advertised window. 1176 * Any pending ACK has now been sent. 1177 */ 1178 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 1179 tp->rcv_adv = tp->rcv_nxt + win; 1180 tp->last_ack_sent = tp->rcv_nxt; 1181 tp->t_flags &= ~TF_ACKNOW; 1182 TCP_CLEAR_DELACK(tp); 1183 #if defined(TCP_SACK) 1184 if (sendalot && --maxburst) 1185 #else 1186 if (sendalot) 1187 #endif 1188 goto again; 1189 return (0); 1190 } 1191 1192 void 1193 tcp_setpersist(struct tcpcb *tp) 1194 { 1195 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + TCP_RTT_BASE_SHIFT); 1196 int nticks; 1197 1198 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT)) 1199 panic("tcp_output REXMT"); 1200 /* 1201 * Start/restart persistence timer. 1202 */ 1203 if (t < tp->t_rttmin) 1204 t = tp->t_rttmin; 1205 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift], 1206 TCPTV_PERSMIN, TCPTV_PERSMAX); 1207 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks); 1208 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1209 tp->t_rxtshift++; 1210 } 1211