1 /* $OpenBSD: tcp_output.c,v 1.150 2025/01/01 13:44:22 bluhm Exp $ */ 2 /* $NetBSD: tcp_output.c,v 1.16 1997/06/03 16:17:09 kml Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include "pf.h" 72 #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/if_var.h> 84 #include <net/route.h> 85 #if NPF > 0 86 #include <net/pfvar.h> 87 #endif 88 89 #include <netinet/in.h> 90 #include <netinet/ip.h> 91 #include <netinet/in_pcb.h> 92 #include <netinet/ip_var.h> 93 #include <netinet6/ip6_var.h> 94 #include <netinet/tcp.h> 95 #define TCPOUTFLAGS 96 #include <netinet/tcp_fsm.h> 97 #include <netinet/tcp_seq.h> 98 #include <netinet/tcp_timer.h> 99 #include <netinet/tcp_var.h> 100 #include <netinet/tcp_debug.h> 101 102 #ifdef notyet 103 extern struct mbuf *m_copypack(); 104 #endif 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, rcv_hiwat, 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 doing_sosend, idle, sendalot = 0; 204 int i, sack_rxmit = 0; 205 struct sackhole *p; 206 uint64_t now; 207 #ifdef TCP_SIGNATURE 208 unsigned int sigoff; 209 #endif /* TCP_SIGNATURE */ 210 #ifdef TCP_ECN 211 int needect; 212 #endif 213 int tso; 214 215 if (tp->t_flags & TF_BLOCKOUTPUT) { 216 tp->t_flags |= TF_NEEDOUTPUT; 217 return (0); 218 } else 219 tp->t_flags &= ~TF_NEEDOUTPUT; 220 221 #if defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) 222 if (tp->sack_enable && (tp->t_flags & TF_SIGNATURE)) 223 return (EINVAL); 224 #endif /* defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) */ 225 226 now = tcp_now(); 227 228 mtx_enter(&so->so_snd.sb_mtx); 229 doing_sosend = soissending(so); 230 mtx_leave(&so->so_snd.sb_mtx); 231 232 /* 233 * Determine length of data that should be transmitted, 234 * and flags that will be used. 235 * If there is some data or critical controls (SYN, RST) 236 * to send, then transmit; otherwise, investigate further. 237 */ 238 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 239 if (idle && (now - tp->t_rcvtime) >= tp->t_rxtcur) 240 /* 241 * We have been idle for "a while" and no acks are 242 * expected to clock out any data we send -- 243 * slow start to get ack "clock" running again. 244 */ 245 tp->snd_cwnd = 2 * tp->t_maxseg; 246 247 /* remember 'idle' for next invocation of tcp_output */ 248 if (idle && doing_sosend) { 249 tp->t_flags |= TF_LASTIDLE; 250 idle = 0; 251 } else 252 tp->t_flags &= ~TF_LASTIDLE; 253 254 again: 255 /* 256 * If we've recently taken a timeout, snd_max will be greater than 257 * snd_nxt. There may be SACK information that allows us to avoid 258 * resending already delivered data. Adjust snd_nxt accordingly. 259 */ 260 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 261 tcp_sack_adjust(tp); 262 off = tp->snd_nxt - tp->snd_una; 263 win = ulmin(tp->snd_wnd, tp->snd_cwnd); 264 265 flags = tcp_outflags[tp->t_state]; 266 267 /* 268 * Send any SACK-generated retransmissions. If we're explicitly trying 269 * to send out new data (when sendalot is 1), bypass this function. 270 * If we retransmit in fast recovery mode, decrement snd_cwnd, since 271 * we're replacing a (future) new transmission with a retransmission 272 * now, and we previously incremented snd_cwnd in tcp_input(). 273 */ 274 if (tp->sack_enable && !sendalot) { 275 if (tp->t_dupacks >= tcprexmtthresh && 276 (p = tcp_sack_output(tp))) { 277 off = p->rxmit - tp->snd_una; 278 sack_rxmit = 1; 279 /* Coalesce holes into a single retransmission */ 280 len = min(tp->t_maxseg, p->end - p->rxmit); 281 if (SEQ_LT(tp->snd_una, tp->snd_last)) 282 tp->snd_cwnd -= tp->t_maxseg; 283 } 284 } 285 286 sendalot = 0; 287 tso = 0; 288 /* 289 * If in persist timeout with window of 0, send 1 byte. 290 * Otherwise, if window is small but nonzero 291 * and timer expired, we will send what we can 292 * and go to transmit state. 293 */ 294 if (tp->t_force) { 295 if (win == 0) { 296 /* 297 * If we still have some data to send, then 298 * clear the FIN bit. Usually this would 299 * happen below when it realizes that we 300 * aren't sending all the data. However, 301 * if we have exactly 1 byte of unset data, 302 * then it won't clear the FIN bit below, 303 * and if we are in persist state, we wind 304 * up sending the packet without recording 305 * that we sent the FIN bit. 306 * 307 * We can't just blindly clear the FIN bit, 308 * because if we don't have any more data 309 * to send then the probe will be the FIN 310 * itself. 311 */ 312 if (off < so->so_snd.sb_cc) 313 flags &= ~TH_FIN; 314 win = 1; 315 } else { 316 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 317 tp->t_rxtshift = 0; 318 } 319 } 320 321 if (!sack_rxmit) { 322 len = ulmin(so->so_snd.sb_cc, win) - off; 323 } 324 325 if (len < 0) { 326 /* 327 * If FIN has been sent but not acked, 328 * but we haven't been called to retransmit, 329 * len will be -1. Otherwise, window shrank 330 * after we sent into it. If window shrank to 0, 331 * cancel pending retransmit, pull snd_nxt back 332 * to (closed) window, and set the persist timer 333 * if it isn't already going. If the window didn't 334 * close completely, just wait for an ACK. 335 */ 336 len = 0; 337 if (win == 0) { 338 TCP_TIMER_DISARM(tp, TCPT_REXMT); 339 tp->t_rxtshift = 0; 340 tp->snd_nxt = tp->snd_una; 341 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 342 tcp_setpersist(tp); 343 } 344 } 345 346 /* 347 * Never send more than half a buffer full. This insures that we can 348 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and 349 * therefore acks will never be delayed unless we run out of data to 350 * transmit. 351 */ 352 txmaxseg = ulmin(so->so_snd.sb_hiwat / 2, tp->t_maxseg); 353 354 if (len > txmaxseg) { 355 if (atomic_load_int(&tcp_do_tso) && 356 tp->t_inpcb->inp_options == NULL && 357 tp->t_inpcb->inp_outputopts6 == NULL && 358 #ifdef TCP_SIGNATURE 359 ((tp->t_flags & TF_SIGNATURE) == 0) && 360 #endif 361 len >= 2 * tp->t_maxseg && 362 tp->rcv_numsacks == 0 && sack_rxmit == 0 && 363 !(flags & (TH_SYN|TH_RST|TH_FIN))) { 364 tso = 1; 365 /* avoid small chopped packets */ 366 if (len > (len / tp->t_maxseg) * tp->t_maxseg) { 367 len = (len / tp->t_maxseg) * tp->t_maxseg; 368 sendalot = 1; 369 } 370 } else { 371 len = txmaxseg; 372 sendalot = 1; 373 } 374 } 375 if (off + len < so->so_snd.sb_cc) 376 flags &= ~TH_FIN; 377 378 mtx_enter(&so->so_rcv.sb_mtx); 379 win = sbspace_locked(so, &so->so_rcv); 380 rcv_hiwat = (long) so->so_rcv.sb_hiwat; 381 mtx_leave(&so->so_rcv.sb_mtx); 382 383 /* 384 * Sender silly window avoidance. If connection is idle 385 * and can send all data, a maximum segment, 386 * at least a maximum default-size segment do it, 387 * or are forced, do it; otherwise don't bother. 388 * If peer's buffer is tiny, then send 389 * when window is at least half open. 390 * If retransmitting (possibly after persist timer forced us 391 * to send into a small window), then must resend. 392 */ 393 if (len) { 394 if (len >= txmaxseg) 395 goto send; 396 if ((idle || (tp->t_flags & TF_NODELAY)) && 397 len + off >= so->so_snd.sb_cc && !doing_sosend && 398 (tp->t_flags & TF_NOPUSH) == 0) 399 goto send; 400 if (tp->t_force) 401 goto send; 402 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 403 goto send; 404 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 405 goto send; 406 if (sack_rxmit) 407 goto send; 408 } 409 410 /* 411 * Compare available window to amount of window 412 * known to peer (as advertised window less 413 * next expected input). If the difference is at least two 414 * max size segments, or at least 50% of the maximum possible 415 * window, then want to send a window update to peer. 416 */ 417 if (win > 0) { 418 /* 419 * "adv" is the amount we can increase the window, 420 * taking into account that we are limited by 421 * TCP_MAXWIN << tp->rcv_scale. 422 */ 423 long adv = lmin(win, (long)TCP_MAXWIN << tp->rcv_scale) - 424 (tp->rcv_adv - tp->rcv_nxt); 425 426 if (adv >= (long) (2 * tp->t_maxseg)) 427 goto send; 428 if (2 * adv >= rcv_hiwat) 429 goto send; 430 } 431 432 /* 433 * Send if we owe peer an ACK. 434 */ 435 if (tp->t_flags & TF_ACKNOW) 436 goto send; 437 if (flags & (TH_SYN|TH_RST)) 438 goto send; 439 if (SEQ_GT(tp->snd_up, tp->snd_una)) 440 goto send; 441 /* 442 * If our state indicates that FIN should be sent 443 * and we have not yet done so, or we're retransmitting the FIN, 444 * then we need to send. 445 */ 446 if (flags & TH_FIN && 447 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 448 goto send; 449 /* 450 * In SACK, it is possible for tcp_output to fail to send a segment 451 * after the retransmission timer has been turned off. Make sure 452 * that the retransmission timer is set. 453 */ 454 if (SEQ_GT(tp->snd_max, tp->snd_una) && 455 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 456 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 457 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 458 return (0); 459 } 460 461 /* 462 * TCP window updates are not reliable, rather a polling protocol 463 * using ``persist'' packets is used to insure receipt of window 464 * updates. The three ``states'' for the output side are: 465 * idle not doing retransmits or persists 466 * persisting to move a small or zero window 467 * (re)transmitting and thereby not persisting 468 * 469 * tp->t_timer[TCPT_PERSIST] 470 * is set when we are in persist state. 471 * tp->t_force 472 * is set when we are called to send a persist packet. 473 * tp->t_timer[TCPT_REXMT] 474 * is set when we are retransmitting 475 * The output side is idle when both timers are zero. 476 * 477 * If send window is too small, there is data to transmit, and no 478 * retransmit or persist is pending, then go to persist state. 479 * If nothing happens soon, send when timer expires: 480 * if window is nonzero, transmit what we can, 481 * otherwise force out a byte. 482 */ 483 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 484 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 485 tp->t_rxtshift = 0; 486 tcp_setpersist(tp); 487 } 488 489 /* 490 * No reason to send a segment, just return. 491 */ 492 return (0); 493 494 send: 495 /* 496 * Before ESTABLISHED, force sending of initial options 497 * unless TCP set not to do any options. 498 * NOTE: we assume that the IP/TCP header plus TCP options 499 * always fit in a single mbuf, leaving room for a maximum 500 * link header, i.e. 501 * max_linkhdr + sizeof(network header) + sizeof(struct tcphdr + 502 * optlen <= MHLEN 503 */ 504 optlen = 0; 505 506 switch (tp->pf) { 507 case 0: /*default to PF_INET*/ 508 case PF_INET: 509 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr); 510 break; 511 #ifdef INET6 512 case PF_INET6: 513 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 514 break; 515 #endif /* INET6 */ 516 default: 517 return (EPFNOSUPPORT); 518 } 519 520 if (flags & TH_SYN) { 521 tp->snd_nxt = tp->iss; 522 if ((tp->t_flags & TF_NOOPT) == 0) { 523 u_int16_t mss; 524 525 opt[0] = TCPOPT_MAXSEG; 526 opt[1] = 4; 527 mss = htons((u_int16_t) tcp_mss(tp, 0)); 528 memcpy(opt + 2, &mss, sizeof(mss)); 529 optlen = 4; 530 531 if (flags & TH_ACK) 532 tcp_mss_update(tp); 533 /* 534 * If this is the first SYN of connection (not a SYN 535 * ACK), include SACK_PERMIT_HDR option. If this is a 536 * SYN ACK, include SACK_PERMIT_HDR option if peer has 537 * already done so. 538 */ 539 if (tp->sack_enable && ((flags & TH_ACK) == 0 || 540 (tp->t_flags & TF_SACK_PERMIT))) { 541 *((u_int32_t *) (opt + optlen)) = 542 htonl(TCPOPT_SACK_PERMIT_HDR); 543 optlen += 4; 544 } 545 if ((tp->t_flags & TF_REQ_SCALE) && 546 ((flags & TH_ACK) == 0 || 547 (tp->t_flags & TF_RCVD_SCALE))) { 548 *((u_int32_t *) (opt + optlen)) = htonl( 549 TCPOPT_NOP << 24 | 550 TCPOPT_WINDOW << 16 | 551 TCPOLEN_WINDOW << 8 | 552 tp->request_r_scale); 553 optlen += 4; 554 } 555 } 556 } 557 558 /* 559 * Send a timestamp and echo-reply if this is a SYN and our side 560 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 561 * and our peer have sent timestamps in our SYN's. 562 */ 563 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 564 (flags & TH_RST) == 0 && 565 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 566 (tp->t_flags & TF_RCVD_TSTMP))) { 567 u_int32_t *lp = (u_int32_t *)(opt + optlen); 568 569 /* Form timestamp option as shown in appendix A of RFC 1323. */ 570 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 571 *lp++ = htonl(now + tp->ts_modulate); 572 *lp = htonl(tp->ts_recent); 573 optlen += TCPOLEN_TSTAMP_APPA; 574 } 575 /* Set receive buffer autosizing timestamp. */ 576 if (tp->rfbuf_ts == 0) { 577 tp->rfbuf_ts = now; 578 tp->rfbuf_cnt = 0; 579 } 580 581 #ifdef TCP_SIGNATURE 582 if (tp->t_flags & TF_SIGNATURE) { 583 u_int8_t *bp = (u_int8_t *)(opt + optlen); 584 585 /* Send signature option */ 586 *(bp++) = TCPOPT_SIGNATURE; 587 *(bp++) = TCPOLEN_SIGNATURE; 588 sigoff = optlen + 2; 589 590 { 591 unsigned int i; 592 593 for (i = 0; i < 16; i++) 594 *(bp++) = 0; 595 } 596 597 598 /* Pad options list to the next 32 bit boundary and 599 * terminate it. 600 */ 601 *bp++ = TCPOPT_NOP; 602 *bp++ = TCPOPT_NOP; 603 604 optlen += TCPOLEN_SIGLEN; 605 } 606 #endif /* TCP_SIGNATURE */ 607 608 /* 609 * Send SACKs if necessary. This should be the last option processed. 610 * Only as many SACKs are sent as are permitted by the maximum options 611 * size. No more than three SACKs are sent. 612 */ 613 if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED && 614 (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT && 615 tp->rcv_numsacks) { 616 u_int32_t *lp = (u_int32_t *)(opt + optlen); 617 u_int32_t *olp = lp++; 618 int count = 0; /* actual number of SACKs inserted */ 619 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK; 620 621 tcpstat_inc(tcps_sack_snd_opts); 622 maxsack = min(maxsack, TCP_MAX_SACK); 623 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) { 624 struct sackblk sack = tp->sackblks[i]; 625 if (sack.start == 0 && sack.end == 0) 626 continue; 627 *lp++ = htonl(sack.start); 628 *lp++ = htonl(sack.end); 629 count++; 630 } 631 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2)); 632 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */ 633 } 634 635 #ifdef DIAGNOSTIC 636 if (optlen > MAX_TCPOPTLEN) 637 panic("tcp_output: options too long"); 638 #endif /* DIAGNOSTIC */ 639 640 hdrlen += optlen; 641 642 /* 643 * Adjust data length if insertion of options will 644 * bump the packet length beyond the t_maxopd length. 645 * Clear the FIN bit because we cut off the tail of 646 * the segment. 647 */ 648 if (len > tp->t_maxopd - optlen) { 649 if (tso) { 650 if (len + hdrlen + max_linkhdr > MAXMCLBYTES) { 651 len = MAXMCLBYTES - hdrlen - max_linkhdr; 652 sendalot = 1; 653 } 654 } else { 655 len = tp->t_maxopd - optlen; 656 sendalot = 1; 657 } 658 flags &= ~TH_FIN; 659 } 660 661 #ifdef DIAGNOSTIC 662 if (max_linkhdr + hdrlen > MCLBYTES) 663 panic("tcphdr too big"); 664 #endif 665 666 /* 667 * Grab a header mbuf, attaching a copy of data to 668 * be transmitted, and initialize the header from 669 * the template for sends on this connection. 670 */ 671 if (len) { 672 if (tp->t_force && len == 1) 673 tcpstat_inc(tcps_sndprobe); 674 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 675 tcpstat_pkt(tcps_sndrexmitpack, tcps_sndrexmitbyte, 676 len); 677 tp->t_sndrexmitpack++; 678 } else { 679 tcpstat_pkt(tcps_sndpack, tcps_sndbyte, len); 680 } 681 #ifdef notyet 682 if ((m = m_copypack(so->so_snd.sb_mb, off, 683 (int)len, max_linkhdr + hdrlen)) == 0) { 684 error = ENOBUFS; 685 goto out; 686 } 687 /* 688 * m_copypack left space for our hdr; use it. 689 */ 690 m->m_len += hdrlen; 691 m->m_data -= hdrlen; 692 #else 693 MGETHDR(m, M_DONTWAIT, MT_HEADER); 694 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 695 MCLGET(m, M_DONTWAIT); 696 if ((m->m_flags & M_EXT) == 0) { 697 m_freem(m); 698 m = NULL; 699 } 700 } 701 if (m == NULL) { 702 error = ENOBUFS; 703 goto out; 704 } 705 m->m_data += max_linkhdr; 706 m->m_len = hdrlen; 707 if (len <= m_trailingspace(m)) { 708 m_copydata(so->so_snd.sb_mb, off, (int) len, 709 mtod(m, caddr_t) + hdrlen); 710 m->m_len += len; 711 } else { 712 m->m_next = m_copym(so->so_snd.sb_mb, off, (int) len, 713 M_NOWAIT); 714 if (m->m_next == 0) { 715 (void) m_free(m); 716 error = ENOBUFS; 717 goto out; 718 } 719 } 720 if (so->so_snd.sb_mb->m_flags & M_PKTHDR) 721 m->m_pkthdr.ph_loopcnt = 722 so->so_snd.sb_mb->m_pkthdr.ph_loopcnt; 723 #endif 724 /* 725 * If we're sending everything we've got, set PUSH. 726 * (This will keep happy those implementations which only 727 * give data to the user when a buffer fills or 728 * a PUSH comes in.) 729 */ 730 if (off + len == so->so_snd.sb_cc && !doing_sosend) 731 flags |= TH_PUSH; 732 tp->t_sndtime = now; 733 } else { 734 if (tp->t_flags & TF_ACKNOW) 735 tcpstat_inc(tcps_sndacks); 736 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 737 tcpstat_inc(tcps_sndctrl); 738 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 739 tcpstat_inc(tcps_sndurg); 740 else 741 tcpstat_inc(tcps_sndwinup); 742 743 MGETHDR(m, M_DONTWAIT, MT_HEADER); 744 if (m != NULL && max_linkhdr + hdrlen > MHLEN) { 745 MCLGET(m, M_DONTWAIT); 746 if ((m->m_flags & M_EXT) == 0) { 747 m_freem(m); 748 m = NULL; 749 } 750 } 751 if (m == NULL) { 752 error = ENOBUFS; 753 goto out; 754 } 755 m->m_data += max_linkhdr; 756 m->m_len = hdrlen; 757 } 758 m->m_pkthdr.ph_ifidx = 0; 759 m->m_pkthdr.len = hdrlen + len; 760 761 /* Enable TSO and specify the size of the resulting segments. */ 762 if (tso) { 763 SET(m->m_pkthdr.csum_flags, M_TCP_TSO); 764 m->m_pkthdr.ph_mss = tp->t_maxseg; 765 } 766 767 if (!tp->t_template) 768 panic("tcp_output"); 769 #ifdef DIAGNOSTIC 770 if (tp->t_template->m_len != hdrlen - optlen) 771 panic("tcp_output: template len != hdrlen - optlen"); 772 #endif /* DIAGNOSTIC */ 773 memcpy(mtod(m, caddr_t), mtod(tp->t_template, caddr_t), 774 tp->t_template->m_len); 775 th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len - 776 sizeof(struct tcphdr)); 777 778 /* 779 * Fill in fields, remembering maximum advertised 780 * window for use in delaying messages about window sizes. 781 * If resending a FIN, be sure not to use a new sequence number. 782 */ 783 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) && 784 (tp->snd_nxt == tp->snd_max)) 785 tp->snd_nxt--; 786 /* 787 * If we are doing retransmissions, then snd_nxt will 788 * not reflect the first unsent octet. For ACK only 789 * packets, we do not want the sequence number of the 790 * retransmitted packet, we want the sequence number 791 * of the next unsent octet. So, if there is no data 792 * (and no SYN or FIN), use snd_max instead of snd_nxt 793 * when filling in ti_seq. But if we are in persist 794 * state, snd_max might reflect one byte beyond the 795 * right edge of the window, so use snd_nxt in that 796 * case, since we know we aren't doing a retransmission. 797 * (retransmit and persist are mutually exclusive...) 798 */ 799 if (len || (flags & (TH_SYN|TH_FIN)) || 800 TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) 801 th->th_seq = htonl(tp->snd_nxt); 802 else 803 th->th_seq = htonl(tp->snd_max); 804 805 if (sack_rxmit) { 806 /* 807 * If sendalot was turned on (due to option stuffing), turn it 808 * off. Properly set th_seq field. Advance the ret'x pointer 809 * by len. 810 */ 811 if (sendalot) 812 sendalot = 0; 813 th->th_seq = htonl(p->rxmit); 814 p->rxmit += len; 815 tcpstat_pkt(tcps_sack_rexmits, tcps_sack_rexmit_bytes, len); 816 } 817 818 th->th_ack = htonl(tp->rcv_nxt); 819 if (optlen) { 820 memcpy(th + 1, opt, optlen); 821 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 822 } 823 #ifdef TCP_ECN 824 if (tcp_do_ecn) { 825 /* 826 * if we have received congestion experienced segs, 827 * set ECE bit. 828 */ 829 if (tp->t_flags & TF_RCVD_CE) { 830 flags |= TH_ECE; 831 tcpstat_inc(tcps_ecn_sndece); 832 } 833 if (!(tp->t_flags & TF_DISABLE_ECN)) { 834 /* 835 * if this is a SYN seg, set ECE and CWR. 836 * set only ECE for SYN-ACK if peer supports ECN. 837 */ 838 if ((flags & (TH_SYN|TH_ACK)) == TH_SYN) 839 flags |= (TH_ECE|TH_CWR); 840 else if ((tp->t_flags & TF_ECN_PERMIT) && 841 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) 842 flags |= TH_ECE; 843 } 844 /* 845 * if we have reduced the congestion window, notify 846 * the peer by setting CWR bit. 847 */ 848 if ((tp->t_flags & TF_ECN_PERMIT) && 849 (tp->t_flags & TF_SEND_CWR)) { 850 flags |= TH_CWR; 851 tp->t_flags &= ~TF_SEND_CWR; 852 tcpstat_inc(tcps_ecn_sndcwr); 853 } 854 } 855 #endif 856 th->th_flags = flags; 857 858 /* 859 * Calculate receive window. Don't shrink window, 860 * but avoid silly window syndrome. 861 */ 862 if (win < (rcv_hiwat / 4) && win < (long)tp->t_maxseg) 863 win = 0; 864 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 865 win = (long)TCP_MAXWIN << tp->rcv_scale; 866 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt)) 867 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt); 868 if (flags & TH_RST) 869 win = 0; 870 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); 871 if (th->th_win == 0) 872 tp->t_sndzerowin++; 873 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 874 u_int32_t urp = tp->snd_up - tp->snd_nxt; 875 if (urp > IP_MAXPACKET) 876 urp = IP_MAXPACKET; 877 th->th_urp = htons((u_int16_t)urp); 878 th->th_flags |= TH_URG; 879 } else 880 /* 881 * If no urgent pointer to send, then we pull 882 * the urgent pointer to the left edge of the send window 883 * so that it doesn't drift into the send window on sequence 884 * number wraparound. 885 */ 886 tp->snd_up = tp->snd_una; /* drag it along */ 887 888 #ifdef TCP_SIGNATURE 889 if (tp->t_flags & TF_SIGNATURE) { 890 int iphlen; 891 union sockaddr_union src, dst; 892 struct tdb *tdb; 893 894 bzero(&src, sizeof(union sockaddr_union)); 895 bzero(&dst, sizeof(union sockaddr_union)); 896 897 switch (tp->pf) { 898 case 0: /*default to PF_INET*/ 899 case AF_INET: 900 iphlen = sizeof(struct ip); 901 src.sa.sa_len = sizeof(struct sockaddr_in); 902 src.sa.sa_family = AF_INET; 903 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 904 dst.sa.sa_len = sizeof(struct sockaddr_in); 905 dst.sa.sa_family = AF_INET; 906 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 907 break; 908 #ifdef INET6 909 case AF_INET6: 910 iphlen = sizeof(struct ip6_hdr); 911 src.sa.sa_len = sizeof(struct sockaddr_in6); 912 src.sa.sa_family = AF_INET6; 913 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 914 dst.sa.sa_len = sizeof(struct sockaddr_in6); 915 dst.sa.sa_family = AF_INET6; 916 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 917 break; 918 #endif /* INET6 */ 919 } 920 921 tdb = gettdbbysrcdst(rtable_l2(tp->t_inpcb->inp_rtableid), 922 0, &src, &dst, IPPROTO_TCP); 923 if (tdb == NULL) { 924 m_freem(m); 925 return (EPERM); 926 } 927 928 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0, 929 mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0) { 930 m_freem(m); 931 tdb_unref(tdb); 932 return (EINVAL); 933 } 934 tdb_unref(tdb); 935 } 936 #endif /* TCP_SIGNATURE */ 937 938 /* Defer checksumming until later (ip_output() or hardware) */ 939 m->m_pkthdr.csum_flags |= M_TCP_CSUM_OUT; 940 941 /* 942 * In transmit state, time the transmission and arrange for 943 * the retransmit. In persist state, just set snd_max. 944 */ 945 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 946 tcp_seq startseq = tp->snd_nxt; 947 948 /* 949 * Advance snd_nxt over sequence space of this segment. 950 */ 951 if (flags & (TH_SYN|TH_FIN)) { 952 if (flags & TH_SYN) 953 tp->snd_nxt++; 954 if (flags & TH_FIN) { 955 tp->snd_nxt++; 956 tp->t_flags |= TF_SENTFIN; 957 } 958 } 959 if (tp->sack_enable) { 960 if (sack_rxmit && (p->rxmit != tp->snd_nxt)) { 961 goto timer; 962 } 963 } 964 tp->snd_nxt += len; 965 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 966 tp->snd_max = tp->snd_nxt; 967 /* 968 * Time this transmission if not a retransmission and 969 * not currently timing anything. 970 */ 971 if (tp->t_rtttime == 0) { 972 tp->t_rtttime = now; 973 tp->t_rtseq = startseq; 974 tcpstat_inc(tcps_segstimed); 975 } 976 } 977 978 /* 979 * Set retransmit timer if not currently set, 980 * and not doing an ack or a keep-alive probe. 981 * Initial value for retransmit timer is smoothed 982 * round-trip time + 2 * round-trip time variance. 983 * Initialize shift counter which is used for backoff 984 * of retransmit time. 985 */ 986 timer: 987 if (tp->sack_enable && sack_rxmit && 988 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 989 tp->snd_nxt != tp->snd_max) { 990 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 991 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) { 992 TCP_TIMER_DISARM(tp, TCPT_PERSIST); 993 tp->t_rxtshift = 0; 994 } 995 } 996 997 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 998 tp->snd_nxt != tp->snd_una) { 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 1006 if (len == 0 && so->so_snd.sb_cc && 1007 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 && 1008 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) { 1009 /* 1010 * Avoid a situation where we do not set persist timer 1011 * after a zero window condition. For example: 1012 * 1) A -> B: packet with enough data to fill the window 1013 * 2) B -> A: ACK for #1 + new data (0 window 1014 * advertisement) 1015 * 3) A -> B: ACK for #2, 0 len packet 1016 * 1017 * In this case, A will not activate the persist timer, 1018 * because it chose to send a packet. Unless tcp_output 1019 * is called for some other reason (delayed ack timer, 1020 * another input packet from B, socket syscall), A will 1021 * not send zero window probes. 1022 * 1023 * So, if you send a 0-length packet, but there is data 1024 * in the socket buffer, and neither the rexmt or 1025 * persist timer is already set, then activate the 1026 * persist timer. 1027 */ 1028 tp->t_rxtshift = 0; 1029 tcp_setpersist(tp); 1030 } 1031 } else 1032 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 1033 tp->snd_max = tp->snd_nxt + len; 1034 1035 tcp_update_sndspace(tp); 1036 1037 /* 1038 * Trace. 1039 */ 1040 if (so->so_options & SO_DEBUG) 1041 tcp_trace(TA_OUTPUT, tp->t_state, tp, tp, mtod(m, caddr_t), 0, 1042 len); 1043 1044 /* 1045 * Fill in IP length and desired time to live and 1046 * send to IP level. There should be a better way 1047 * to handle ttl and tos; we could keep them in 1048 * the template, but need a way to checksum without them. 1049 */ 1050 1051 #ifdef TCP_ECN 1052 /* 1053 * if peer is ECN capable, set the ECT bit in the IP header. 1054 * but don't set ECT for a pure ack, a retransmit or a window probe. 1055 */ 1056 needect = 0; 1057 if (tcp_do_ecn && (tp->t_flags & TF_ECN_PERMIT)) { 1058 if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) || 1059 (tp->t_force && len == 1)) { 1060 /* don't set ECT */ 1061 } else { 1062 needect = 1; 1063 tcpstat_inc(tcps_ecn_sndect); 1064 } 1065 } 1066 #endif 1067 1068 /* force routing table */ 1069 m->m_pkthdr.ph_rtableid = tp->t_inpcb->inp_rtableid; 1070 1071 #if NPF > 0 1072 pf_mbuf_link_inpcb(m, tp->t_inpcb); 1073 #endif 1074 1075 switch (tp->pf) { 1076 case 0: /*default to PF_INET*/ 1077 case AF_INET: 1078 { 1079 struct ip *ip; 1080 1081 ip = mtod(m, struct ip *); 1082 ip->ip_len = htons(m->m_pkthdr.len); 1083 packetlen = m->m_pkthdr.len; 1084 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; 1085 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos; 1086 #ifdef TCP_ECN 1087 if (needect) 1088 ip->ip_tos |= IPTOS_ECN_ECT0; 1089 #endif 1090 } 1091 #if NSTOEPLITZ > 0 1092 m->m_pkthdr.ph_flowid = tp->t_inpcb->inp_flowid; 1093 SET(m->m_pkthdr.csum_flags, M_FLOWID); 1094 #endif 1095 error = ip_output(m, tp->t_inpcb->inp_options, 1096 &tp->t_inpcb->inp_route, 1097 (ip_mtudisc ? IP_MTUDISC : 0), NULL, 1098 &tp->t_inpcb->inp_seclevel, 0); 1099 break; 1100 #ifdef INET6 1101 case AF_INET6: 1102 { 1103 struct ip6_hdr *ip6; 1104 1105 ip6 = mtod(m, struct ip6_hdr *); 1106 ip6->ip6_plen = m->m_pkthdr.len - 1107 sizeof(struct ip6_hdr); 1108 packetlen = m->m_pkthdr.len; 1109 ip6->ip6_nxt = IPPROTO_TCP; 1110 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb); 1111 #ifdef TCP_ECN 1112 if (needect) 1113 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1114 #endif 1115 } 1116 error = ip6_output(m, tp->t_inpcb->inp_outputopts6, 1117 &tp->t_inpcb->inp_route, 0, NULL, 1118 &tp->t_inpcb->inp_seclevel); 1119 break; 1120 #endif /* INET6 */ 1121 } 1122 1123 if (error) { 1124 out: 1125 if (error == ENOBUFS) { 1126 /* 1127 * If the interface queue is full, or IP cannot 1128 * get an mbuf, trigger TCP slow start. 1129 */ 1130 tp->snd_cwnd = tp->t_maxseg; 1131 return (0); 1132 } 1133 if (error == EMSGSIZE) { 1134 /* 1135 * ip_output() will have already fixed the route 1136 * for us. tcp_mtudisc() will, as its last action, 1137 * initiate retransmission, so it is important to 1138 * not do so here. 1139 */ 1140 tcp_mtudisc(tp->t_inpcb, -1); 1141 return (0); 1142 } 1143 if ((error == EHOSTUNREACH || error == ENETDOWN) && 1144 TCPS_HAVERCVDSYN(tp->t_state)) { 1145 tp->t_softerror = error; 1146 return (0); 1147 } 1148 1149 /* Restart the delayed ACK timer, if necessary. */ 1150 if (TCP_TIMER_ISARMED(tp, TCPT_DELACK)) 1151 TCP_TIMER_ARM(tp, TCPT_DELACK, tcp_delack_msecs); 1152 1153 return (error); 1154 } 1155 1156 if (packetlen > tp->t_pmtud_mtu_sent) 1157 tp->t_pmtud_mtu_sent = packetlen; 1158 1159 tcpstat_inc(tcps_sndtotal); 1160 if (TCP_TIMER_ISARMED(tp, TCPT_DELACK)) 1161 tcpstat_inc(tcps_delack); 1162 1163 /* 1164 * Data sent (as far as we can tell). 1165 * If this advertises a larger window than any other segment, 1166 * then remember the size of the advertised window. 1167 * Any pending ACK has now been sent. 1168 */ 1169 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 1170 tp->rcv_adv = tp->rcv_nxt + win; 1171 tp->last_ack_sent = tp->rcv_nxt; 1172 tp->t_sndacktime = now; 1173 tp->t_flags &= ~TF_ACKNOW; 1174 TCP_TIMER_DISARM(tp, TCPT_DELACK); 1175 if (sendalot) 1176 goto again; 1177 return (0); 1178 } 1179 1180 void 1181 tcp_setpersist(struct tcpcb *tp) 1182 { 1183 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + TCP_RTT_BASE_SHIFT); 1184 int msec; 1185 1186 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT)) 1187 panic("tcp_output REXMT"); 1188 /* 1189 * Start/restart persistence timer. 1190 */ 1191 if (t < tp->t_rttmin) 1192 t = tp->t_rttmin; 1193 TCPT_RANGESET(msec, t * tcp_backoff[tp->t_rxtshift], 1194 TCPTV_PERSMIN, TCPTV_PERSMAX); 1195 TCP_TIMER_ARM(tp, TCPT_PERSIST, msec); 1196 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1197 tp->t_rxtshift++; 1198 } 1199 1200 int 1201 tcp_chopper(struct mbuf *m0, struct mbuf_list *ml, struct ifnet *ifp, 1202 u_int mss) 1203 { 1204 struct ip *ip = NULL; 1205 #ifdef INET6 1206 struct ip6_hdr *ip6 = NULL; 1207 #endif 1208 struct tcphdr *th; 1209 int firstlen, iphlen, hlen, tlen, off; 1210 int error; 1211 1212 ml_init(ml); 1213 ml_enqueue(ml, m0); 1214 1215 if (mss == 0) { 1216 error = EINVAL; 1217 goto bad; 1218 } 1219 1220 ip = mtod(m0, struct ip *); 1221 switch (ip->ip_v) { 1222 case 4: 1223 iphlen = ip->ip_hl << 2; 1224 if (ISSET(ip->ip_off, htons(IP_OFFMASK | IP_MF)) || 1225 iphlen != sizeof(struct ip) || ip->ip_p != IPPROTO_TCP) { 1226 /* only TCP without fragment or IP option supported */ 1227 error = EPROTOTYPE; 1228 goto bad; 1229 } 1230 break; 1231 #ifdef INET6 1232 case 6: 1233 ip = NULL; 1234 ip6 = mtod(m0, struct ip6_hdr *); 1235 iphlen = sizeof(struct ip6_hdr); 1236 if (ip6->ip6_nxt != IPPROTO_TCP) { 1237 /* only TCP without IPv6 header chain supported */ 1238 error = EPROTOTYPE; 1239 goto bad; 1240 } 1241 break; 1242 #endif 1243 default: 1244 panic("%s: unknown ip version %d", __func__, ip->ip_v); 1245 } 1246 1247 tlen = m0->m_pkthdr.len; 1248 if (tlen < iphlen + sizeof(struct tcphdr)) { 1249 error = ENOPROTOOPT; 1250 goto bad; 1251 } 1252 /* IP and TCP header should be contiguous, this check is paranoia */ 1253 if (m0->m_len < iphlen + sizeof(*th)) { 1254 ml_dequeue(ml); 1255 if ((m0 = m_pullup(m0, iphlen + sizeof(*th))) == NULL) { 1256 error = ENOBUFS; 1257 goto bad; 1258 } 1259 ml_enqueue(ml, m0); 1260 } 1261 th = (struct tcphdr *)(mtod(m0, caddr_t) + iphlen); 1262 hlen = iphlen + (th->th_off << 2); 1263 if (tlen < hlen) { 1264 error = ENOPROTOOPT; 1265 goto bad; 1266 } 1267 firstlen = MIN(tlen - hlen, mss); 1268 1269 CLR(m0->m_pkthdr.csum_flags, M_TCP_TSO); 1270 1271 /* 1272 * Loop through length of payload after first segment, 1273 * make new header and copy data of each part and link onto chain. 1274 */ 1275 for (off = hlen + firstlen; off < tlen; off += mss) { 1276 struct mbuf *m; 1277 struct tcphdr *mhth; 1278 int len; 1279 1280 len = MIN(tlen - off, mss); 1281 1282 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1283 if (m == NULL) { 1284 error = ENOBUFS; 1285 goto bad; 1286 } 1287 ml_enqueue(ml, m); 1288 if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0) 1289 goto bad; 1290 1291 /* IP and TCP header to the end, space for link layer header */ 1292 m->m_len = hlen; 1293 m_align(m, hlen); 1294 1295 /* copy and adjust TCP header */ 1296 mhth = (struct tcphdr *)(mtod(m, caddr_t) + iphlen); 1297 memcpy(mhth, th, hlen - iphlen); 1298 mhth->th_seq = htonl(ntohl(th->th_seq) + (off - hlen)); 1299 if (off + len < tlen) 1300 CLR(mhth->th_flags, TH_PUSH|TH_FIN); 1301 1302 /* add mbuf chain with payload */ 1303 m->m_pkthdr.len = hlen + len; 1304 if ((m->m_next = m_copym(m0, off, len, M_DONTWAIT)) == NULL) { 1305 error = ENOBUFS; 1306 goto bad; 1307 } 1308 1309 /* copy and adjust IP header, calculate checksum */ 1310 SET(m->m_pkthdr.csum_flags, M_TCP_CSUM_OUT); 1311 if (ip) { 1312 struct ip *mhip; 1313 1314 mhip = mtod(m, struct ip *); 1315 *mhip = *ip; 1316 mhip->ip_len = htons(hlen + len); 1317 mhip->ip_id = htons(ip_randomid()); 1318 in_hdr_cksum_out(m, ifp); 1319 in_proto_cksum_out(m, ifp); 1320 } 1321 #ifdef INET6 1322 if (ip6) { 1323 struct ip6_hdr *mhip6; 1324 1325 mhip6 = mtod(m, struct ip6_hdr *); 1326 *mhip6 = *ip6; 1327 mhip6->ip6_plen = htons(hlen - iphlen + len); 1328 in6_proto_cksum_out(m, ifp); 1329 } 1330 #endif 1331 } 1332 1333 /* 1334 * Update first segment by trimming what's been copied out 1335 * and updating header, then send each segment (in order). 1336 */ 1337 if (hlen + firstlen < tlen) { 1338 m_adj(m0, hlen + firstlen - tlen); 1339 CLR(th->th_flags, TH_PUSH|TH_FIN); 1340 } 1341 /* adjust IP header, calculate checksum */ 1342 SET(m0->m_pkthdr.csum_flags, M_TCP_CSUM_OUT); 1343 if (ip) { 1344 ip->ip_len = htons(m0->m_pkthdr.len); 1345 in_hdr_cksum_out(m0, ifp); 1346 in_proto_cksum_out(m0, ifp); 1347 } 1348 #ifdef INET6 1349 if (ip6) { 1350 ip6->ip6_plen = htons(m0->m_pkthdr.len - iphlen); 1351 in6_proto_cksum_out(m0, ifp); 1352 } 1353 #endif 1354 1355 tcpstat_add(tcps_outpkttso, ml_len(ml)); 1356 return 0; 1357 1358 bad: 1359 tcpstat_inc(tcps_outbadtso); 1360 ml_purge(ml); 1361 return error; 1362 } 1363 1364 int 1365 tcp_if_output_tso(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, 1366 struct rtentry *rt, uint32_t ifcap, u_int mtu) 1367 { 1368 struct mbuf_list ml; 1369 int error; 1370 1371 /* caller must fail later or fragment */ 1372 if (!ISSET((*mp)->m_pkthdr.csum_flags, M_TCP_TSO)) 1373 return 0; 1374 if ((*mp)->m_pkthdr.ph_mss > mtu) { 1375 CLR((*mp)->m_pkthdr.csum_flags, M_TCP_TSO); 1376 return 0; 1377 } 1378 1379 /* network interface hardware will do TSO */ 1380 if (in_ifcap_cksum(*mp, ifp, ifcap)) { 1381 if (ISSET(ifcap, IFCAP_TSOv4)) { 1382 in_hdr_cksum_out(*mp, ifp); 1383 in_proto_cksum_out(*mp, ifp); 1384 } 1385 #ifdef INET6 1386 if (ISSET(ifcap, IFCAP_TSOv6)) 1387 in6_proto_cksum_out(*mp, ifp); 1388 #endif 1389 error = ifp->if_output(ifp, *mp, dst, rt); 1390 if (!error) 1391 tcpstat_inc(tcps_outhwtso); 1392 goto done; 1393 } 1394 1395 /* as fallback do TSO in software */ 1396 if ((error = tcp_chopper(*mp, &ml, ifp, (*mp)->m_pkthdr.ph_mss)) || 1397 (error = if_output_ml(ifp, &ml, dst, rt))) 1398 goto done; 1399 tcpstat_inc(tcps_outswtso); 1400 1401 done: 1402 *mp = NULL; 1403 return error; 1404 } 1405