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