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