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