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