1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93 34 * $Id: tcp_output.c,v 1.2 1994/08/02 07:49:03 davidg Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/errno.h> 45 46 #include <net/route.h> 47 48 #include <netinet/in.h> 49 #include <netinet/in_systm.h> 50 #include <netinet/ip.h> 51 #include <netinet/in_pcb.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/tcp.h> 54 #define TCPOUTFLAGS 55 #include <netinet/tcp_fsm.h> 56 #include <netinet/tcp_seq.h> 57 #include <netinet/tcp_timer.h> 58 #include <netinet/tcp_var.h> 59 #include <netinet/tcpip.h> 60 #ifdef TCPDEBUG 61 #include <netinet/tcp_debug.h> 62 #endif 63 64 #ifdef notyet 65 extern struct mbuf *m_copypack(); 66 #endif 67 68 69 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */ 70 71 /* 72 * Tcp output routine: figure out what should be sent and send it. 73 */ 74 int 75 tcp_output(tp) 76 register struct tcpcb *tp; 77 { 78 register struct socket *so = tp->t_inpcb->inp_socket; 79 register long len, win; 80 int off, flags, error; 81 register struct mbuf *m; 82 register struct tcpiphdr *ti; 83 u_char opt[MAX_TCPOPTLEN]; 84 unsigned optlen, hdrlen; 85 int idle, sendalot; 86 87 /* 88 * Determine length of data that should be transmitted, 89 * and flags that will be used. 90 * If there is some data or critical controls (SYN, RST) 91 * to send, then transmit; otherwise, investigate further. 92 */ 93 idle = (tp->snd_max == tp->snd_una); 94 if (idle && tp->t_idle >= tp->t_rxtcur) 95 /* 96 * We have been idle for "a while" and no acks are 97 * expected to clock out any data we send -- 98 * slow start to get ack "clock" running again. 99 */ 100 tp->snd_cwnd = tp->t_maxseg; 101 again: 102 sendalot = 0; 103 off = tp->snd_nxt - tp->snd_una; 104 win = min(tp->snd_wnd, tp->snd_cwnd); 105 106 flags = tcp_outflags[tp->t_state]; 107 /* 108 * If in persist timeout with window of 0, send 1 byte. 109 * Otherwise, if window is small but nonzero 110 * and timer expired, we will send what we can 111 * and go to transmit state. 112 */ 113 if (tp->t_force) { 114 if (win == 0) { 115 /* 116 * If we still have some data to send, then 117 * clear the FIN bit. Usually this would 118 * happen below when it realizes that we 119 * aren't sending all the data. However, 120 * if we have exactly 1 byte of unset data, 121 * then it won't clear the FIN bit below, 122 * and if we are in persist state, we wind 123 * up sending the packet without recording 124 * that we sent the FIN bit. 125 * 126 * We can't just blindly clear the FIN bit, 127 * because if we don't have any more data 128 * to send then the probe will be the FIN 129 * itself. 130 */ 131 if (off < so->so_snd.sb_cc) 132 flags &= ~TH_FIN; 133 win = 1; 134 } else { 135 tp->t_timer[TCPT_PERSIST] = 0; 136 tp->t_rxtshift = 0; 137 } 138 } 139 140 len = min(so->so_snd.sb_cc, win) - off; 141 142 if (len < 0) { 143 /* 144 * If FIN has been sent but not acked, 145 * but we haven't been called to retransmit, 146 * len will be -1. Otherwise, window shrank 147 * after we sent into it. If window shrank to 0, 148 * cancel pending retransmit and pull snd_nxt 149 * back to (closed) window. We will enter persist 150 * state below. If the window didn't close completely, 151 * just wait for an ACK. 152 */ 153 len = 0; 154 if (win == 0) { 155 tp->t_timer[TCPT_REXMT] = 0; 156 tp->snd_nxt = tp->snd_una; 157 } 158 } 159 if (len > tp->t_maxseg) { 160 len = tp->t_maxseg; 161 sendalot = 1; 162 } 163 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 164 flags &= ~TH_FIN; 165 166 win = sbspace(&so->so_rcv); 167 168 /* 169 * Sender silly window avoidance. If connection is idle 170 * and can send all data, a maximum segment, 171 * at least a maximum default-size segment do it, 172 * or are forced, do it; otherwise don't bother. 173 * If peer's buffer is tiny, then send 174 * when window is at least half open. 175 * If retransmitting (possibly after persist timer forced us 176 * to send into a small window), then must resend. 177 */ 178 if (len) { 179 if (len == tp->t_maxseg) 180 goto send; 181 if ((idle || tp->t_flags & TF_NODELAY) && 182 len + off >= so->so_snd.sb_cc) 183 goto send; 184 if (tp->t_force) 185 goto send; 186 if (len >= tp->max_sndwnd / 2) 187 goto send; 188 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 189 goto send; 190 } 191 192 /* 193 * Compare available window to amount of window 194 * known to peer (as advertised window less 195 * next expected input). If the difference is at least two 196 * max size segments, or at least 50% of the maximum possible 197 * window, then want to send a window update to peer. 198 */ 199 if (win > 0) { 200 /* 201 * "adv" is the amount we can increase the window, 202 * taking into account that we are limited by 203 * TCP_MAXWIN << tp->rcv_scale. 204 */ 205 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 206 (tp->rcv_adv - tp->rcv_nxt); 207 208 if (adv >= (long) (2 * tp->t_maxseg)) 209 goto send; 210 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 211 goto send; 212 } 213 214 /* 215 * Send if we owe peer an ACK. 216 */ 217 if (tp->t_flags & TF_ACKNOW) 218 goto send; 219 if (flags & (TH_SYN|TH_RST)) 220 goto send; 221 if (SEQ_GT(tp->snd_up, tp->snd_una)) 222 goto send; 223 /* 224 * If our state indicates that FIN should be sent 225 * and we have not yet done so, or we're retransmitting the FIN, 226 * then we need to send. 227 */ 228 if (flags & TH_FIN && 229 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 230 goto send; 231 232 /* 233 * TCP window updates are not reliable, rather a polling protocol 234 * using ``persist'' packets is used to insure receipt of window 235 * updates. The three ``states'' for the output side are: 236 * idle not doing retransmits or persists 237 * persisting to move a small or zero window 238 * (re)transmitting and thereby not persisting 239 * 240 * tp->t_timer[TCPT_PERSIST] 241 * is set when we are in persist state. 242 * tp->t_force 243 * is set when we are called to send a persist packet. 244 * tp->t_timer[TCPT_REXMT] 245 * is set when we are retransmitting 246 * The output side is idle when both timers are zero. 247 * 248 * If send window is too small, there is data to transmit, and no 249 * retransmit or persist is pending, then go to persist state. 250 * If nothing happens soon, send when timer expires: 251 * if window is nonzero, transmit what we can, 252 * otherwise force out a byte. 253 */ 254 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 255 tp->t_timer[TCPT_PERSIST] == 0) { 256 tp->t_rxtshift = 0; 257 tcp_setpersist(tp); 258 } 259 260 /* 261 * No reason to send a segment, just return. 262 */ 263 return (0); 264 265 send: 266 /* 267 * Before ESTABLISHED, force sending of initial options 268 * unless TCP set not to do any options. 269 * NOTE: we assume that the IP/TCP header plus TCP options 270 * always fit in a single mbuf, leaving room for a maximum 271 * link header, i.e. 272 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN 273 */ 274 optlen = 0; 275 hdrlen = sizeof (struct tcpiphdr); 276 if (flags & TH_SYN) { 277 tp->snd_nxt = tp->iss; 278 if ((tp->t_flags & TF_NOOPT) == 0) { 279 u_short mss; 280 281 opt[0] = TCPOPT_MAXSEG; 282 opt[1] = 4; 283 mss = htons((u_short) tcp_mss(tp, 0)); 284 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss)); 285 optlen = 4; 286 287 if ((tp->t_flags & TF_REQ_SCALE) && 288 ((flags & TH_ACK) == 0 || 289 (tp->t_flags & TF_RCVD_SCALE))) { 290 *((u_long *) (opt + optlen)) = htonl( 291 TCPOPT_NOP << 24 | 292 TCPOPT_WINDOW << 16 | 293 TCPOLEN_WINDOW << 8 | 294 tp->request_r_scale); 295 optlen += 4; 296 } 297 } 298 } 299 300 /* 301 * Send a timestamp and echo-reply if this is a SYN and our side 302 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 303 * and our peer have sent timestamps in our SYN's. 304 */ 305 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 306 (flags & TH_RST) == 0 && 307 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 308 (tp->t_flags & TF_RCVD_TSTMP))) { 309 u_long *lp = (u_long *)(opt + optlen); 310 311 /* Form timestamp option as shown in appendix A of RFC 1323. */ 312 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 313 *lp++ = htonl(tcp_now); 314 *lp = htonl(tp->ts_recent); 315 optlen += TCPOLEN_TSTAMP_APPA; 316 } 317 318 hdrlen += optlen; 319 320 /* 321 * Adjust data length if insertion of options will 322 * bump the packet length beyond the t_maxseg length. 323 */ 324 if (len > tp->t_maxseg - optlen) { 325 len = tp->t_maxseg - optlen; 326 sendalot = 1; 327 } 328 329 330 #ifdef DIAGNOSTIC 331 if (max_linkhdr + hdrlen > MHLEN) 332 panic("tcphdr too big"); 333 #endif 334 335 /* 336 * Grab a header mbuf, attaching a copy of data to 337 * be transmitted, and initialize the header from 338 * the template for sends on this connection. 339 */ 340 if (len) { 341 if (tp->t_force && len == 1) 342 tcpstat.tcps_sndprobe++; 343 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 344 tcpstat.tcps_sndrexmitpack++; 345 tcpstat.tcps_sndrexmitbyte += len; 346 } else { 347 tcpstat.tcps_sndpack++; 348 tcpstat.tcps_sndbyte += len; 349 } 350 #ifdef notyet 351 if ((m = m_copypack(so->so_snd.sb_mb, off, 352 (int)len, max_linkhdr + hdrlen)) == 0) { 353 error = ENOBUFS; 354 goto out; 355 } 356 /* 357 * m_copypack left space for our hdr; use it. 358 */ 359 m->m_len += hdrlen; 360 m->m_data -= hdrlen; 361 #else 362 MGETHDR(m, M_DONTWAIT, MT_HEADER); 363 if (m == NULL) { 364 error = ENOBUFS; 365 goto out; 366 } 367 m->m_data += max_linkhdr; 368 m->m_len = hdrlen; 369 if (len <= MHLEN - hdrlen - max_linkhdr) { 370 m_copydata(so->so_snd.sb_mb, off, (int) len, 371 mtod(m, caddr_t) + hdrlen); 372 m->m_len += len; 373 } else { 374 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 375 if (m->m_next == 0) 376 len = 0; 377 } 378 #endif 379 /* 380 * If we're sending everything we've got, set PUSH. 381 * (This will keep happy those implementations which only 382 * give data to the user when a buffer fills or 383 * a PUSH comes in.) 384 */ 385 if (off + len == so->so_snd.sb_cc) 386 flags |= TH_PUSH; 387 } else { 388 if (tp->t_flags & TF_ACKNOW) 389 tcpstat.tcps_sndacks++; 390 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 391 tcpstat.tcps_sndctrl++; 392 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 393 tcpstat.tcps_sndurg++; 394 else 395 tcpstat.tcps_sndwinup++; 396 397 MGETHDR(m, M_DONTWAIT, MT_HEADER); 398 if (m == NULL) { 399 error = ENOBUFS; 400 goto out; 401 } 402 m->m_data += max_linkhdr; 403 m->m_len = hdrlen; 404 } 405 m->m_pkthdr.rcvif = (struct ifnet *)0; 406 ti = mtod(m, struct tcpiphdr *); 407 if (tp->t_template == 0) 408 panic("tcp_output"); 409 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 410 411 /* 412 * Fill in fields, remembering maximum advertised 413 * window for use in delaying messages about window sizes. 414 * If resending a FIN, be sure not to use a new sequence number. 415 */ 416 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 417 tp->snd_nxt == tp->snd_max) 418 tp->snd_nxt--; 419 /* 420 * If we are doing retransmissions, then snd_nxt will 421 * not reflect the first unsent octet. For ACK only 422 * packets, we do not want the sequence number of the 423 * retransmitted packet, we want the sequence number 424 * of the next unsent octet. So, if there is no data 425 * (and no SYN or FIN), use snd_max instead of snd_nxt 426 * when filling in ti_seq. But if we are in persist 427 * state, snd_max might reflect one byte beyond the 428 * right edge of the window, so use snd_nxt in that 429 * case, since we know we aren't doing a retransmission. 430 * (retransmit and persist are mutually exclusive...) 431 */ 432 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST]) 433 ti->ti_seq = htonl(tp->snd_nxt); 434 else 435 ti->ti_seq = htonl(tp->snd_max); 436 ti->ti_ack = htonl(tp->rcv_nxt); 437 if (optlen) { 438 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen); 439 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 440 } 441 ti->ti_flags = flags; 442 /* 443 * Calculate receive window. Don't shrink window, 444 * but avoid silly window syndrome. 445 */ 446 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 447 win = 0; 448 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 449 win = (long)TCP_MAXWIN << tp->rcv_scale; 450 if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 451 win = (long)(tp->rcv_adv - tp->rcv_nxt); 452 ti->ti_win = htons((u_short) (win>>tp->rcv_scale)); 453 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 454 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 455 ti->ti_flags |= TH_URG; 456 } else 457 /* 458 * If no urgent pointer to send, then we pull 459 * the urgent pointer to the left edge of the send window 460 * so that it doesn't drift into the send window on sequence 461 * number wraparound. 462 */ 463 tp->snd_up = tp->snd_una; /* drag it along */ 464 465 /* 466 * Put TCP length in extended header, and then 467 * checksum extended header and data. 468 */ 469 if (len + optlen) 470 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + 471 optlen + len)); 472 ti->ti_sum = in_cksum(m, (int)(hdrlen + len)); 473 474 /* 475 * In transmit state, time the transmission and arrange for 476 * the retransmit. In persist state, just set snd_max. 477 */ 478 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 479 tcp_seq startseq = tp->snd_nxt; 480 481 /* 482 * Advance snd_nxt over sequence space of this segment. 483 */ 484 if (flags & (TH_SYN|TH_FIN)) { 485 if (flags & TH_SYN) 486 tp->snd_nxt++; 487 if (flags & TH_FIN) { 488 tp->snd_nxt++; 489 tp->t_flags |= TF_SENTFIN; 490 } 491 } 492 tp->snd_nxt += len; 493 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 494 tp->snd_max = tp->snd_nxt; 495 /* 496 * Time this transmission if not a retransmission and 497 * not currently timing anything. 498 */ 499 if (tp->t_rtt == 0) { 500 tp->t_rtt = 1; 501 tp->t_rtseq = startseq; 502 tcpstat.tcps_segstimed++; 503 } 504 } 505 506 /* 507 * Set retransmit timer if not currently set, 508 * and not doing an ack or a keep-alive probe. 509 * Initial value for retransmit timer is smoothed 510 * round-trip time + 2 * round-trip time variance. 511 * Initialize shift counter which is used for backoff 512 * of retransmit time. 513 */ 514 if (tp->t_timer[TCPT_REXMT] == 0 && 515 tp->snd_nxt != tp->snd_una) { 516 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 517 if (tp->t_timer[TCPT_PERSIST]) { 518 tp->t_timer[TCPT_PERSIST] = 0; 519 tp->t_rxtshift = 0; 520 } 521 } 522 } else 523 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 524 tp->snd_max = tp->snd_nxt + len; 525 526 #ifdef TCPDEBUG 527 /* 528 * Trace. 529 */ 530 if (so->so_options & SO_DEBUG) 531 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 532 #endif 533 534 /* 535 * Fill in IP length and desired time to live and 536 * send to IP level. There should be a better way 537 * to handle ttl and tos; we could keep them in 538 * the template, but need a way to checksum without them. 539 */ 540 m->m_pkthdr.len = hdrlen + len; 541 #ifdef TUBA 542 if (tp->t_tuba_pcb) 543 error = tuba_output(m, tp); 544 else 545 #endif 546 { 547 ((struct ip *)ti)->ip_len = m->m_pkthdr.len; 548 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */ 549 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */ 550 #if BSD >= 43 551 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 552 so->so_options & SO_DONTROUTE, 0); 553 #else 554 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route, 555 so->so_options & SO_DONTROUTE); 556 #endif 557 } 558 if (error) { 559 out: 560 if (error == ENOBUFS) { 561 tcp_quench(tp->t_inpcb, 0); 562 return (0); 563 } 564 if ((error == EHOSTUNREACH || error == ENETDOWN) 565 && TCPS_HAVERCVDSYN(tp->t_state)) { 566 tp->t_softerror = error; 567 return (0); 568 } 569 return (error); 570 } 571 tcpstat.tcps_sndtotal++; 572 573 /* 574 * Data sent (as far as we can tell). 575 * If this advertises a larger window than any other segment, 576 * then remember the size of the advertised window. 577 * Any pending ACK has now been sent. 578 */ 579 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 580 tp->rcv_adv = tp->rcv_nxt + win; 581 tp->last_ack_sent = tp->rcv_nxt; 582 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 583 if (sendalot) 584 goto again; 585 return (0); 586 } 587 588 void 589 tcp_setpersist(tp) 590 register struct tcpcb *tp; 591 { 592 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 593 594 if (tp->t_timer[TCPT_REXMT]) 595 panic("tcp_output REXMT"); 596 /* 597 * Start/restart persistance timer. 598 */ 599 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 600 t * tcp_backoff[tp->t_rxtshift], 601 TCPTV_PERSMIN, TCPTV_PERSMAX); 602 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 603 tp->t_rxtshift++; 604 } 605