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