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