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.28 (Berkeley) 04/10/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) { 229 tp->snd_nxt = tp->iss; 230 if ((tp->t_flags & TF_NOOPT) == 0) { 231 u_short mss; 232 233 opt[0] = TCPOPT_MAXSEG; 234 opt[1] = 4; 235 mss = htons((u_short) tcp_mss(tp, 0)); 236 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss)); 237 optlen = 4; 238 239 if ((tp->t_flags & TF_REQ_SCALE) && 240 ((flags & TH_ACK) == 0 || 241 (tp->t_flags & TF_RCVD_SCALE))) { 242 *((u_long *) (opt + optlen)) = htonl( 243 TCPOPT_NOP << 24 | 244 TCPOPT_WINDOW << 16 | 245 TCPOLEN_WINDOW << 8 | 246 tp->request_r_scale); 247 optlen += 4; 248 } 249 } 250 } 251 252 /* 253 * Send a timestamp and echo-reply if this is a SYN and our side 254 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 255 * and our peer have sent timestamps in our SYN's. 256 */ 257 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 258 (flags & TH_RST) == 0 && 259 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 260 (tp->t_flags & TF_RCVD_TSTMP))) { 261 u_long *lp = (u_long *)(opt + optlen); 262 263 /* Form timestamp option as shown in appendix A of RFC 1323. */ 264 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 265 *lp++ = htonl(tcp_now); 266 *lp = htonl(tp->ts_recent); 267 optlen += TCPOLEN_TSTAMP_APPA; 268 } 269 270 hdrlen += optlen; 271 272 /* 273 * Adjust data length if insertion of options will 274 * bump the packet length beyond the t_maxseg length. 275 */ 276 if (len > tp->t_maxseg - optlen) { 277 len = tp->t_maxseg - optlen; 278 sendalot = 1; 279 } 280 281 282 #ifdef DIAGNOSTIC 283 if (max_linkhdr + hdrlen > MHLEN) 284 panic("tcphdr too big"); 285 #endif 286 287 /* 288 * Grab a header mbuf, attaching a copy of data to 289 * be transmitted, and initialize the header from 290 * the template for sends on this connection. 291 */ 292 if (len) { 293 if (tp->t_force && len == 1) 294 tcpstat.tcps_sndprobe++; 295 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 296 tcpstat.tcps_sndrexmitpack++; 297 tcpstat.tcps_sndrexmitbyte += len; 298 } else { 299 tcpstat.tcps_sndpack++; 300 tcpstat.tcps_sndbyte += len; 301 } 302 #ifdef notyet 303 if ((m = m_copypack(so->so_snd.sb_mb, off, 304 (int)len, max_linkhdr + hdrlen)) == 0) { 305 error = ENOBUFS; 306 goto out; 307 } 308 /* 309 * m_copypack left space for our hdr; use it. 310 */ 311 m->m_len += hdrlen; 312 m->m_data -= hdrlen; 313 #else 314 MGETHDR(m, M_DONTWAIT, MT_HEADER); 315 if (m == NULL) { 316 error = ENOBUFS; 317 goto out; 318 } 319 m->m_data += max_linkhdr; 320 m->m_len = hdrlen; 321 if (len <= MHLEN - hdrlen - max_linkhdr) { 322 m_copydata(so->so_snd.sb_mb, off, (int) len, 323 mtod(m, caddr_t) + hdrlen); 324 m->m_len += len; 325 } else { 326 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 327 if (m->m_next == 0) 328 len = 0; 329 } 330 #endif 331 /* 332 * If we're sending everything we've got, set PUSH. 333 * (This will keep happy those implementations which only 334 * give data to the user when a buffer fills or 335 * a PUSH comes in.) 336 */ 337 if (off + len == so->so_snd.sb_cc) 338 flags |= TH_PUSH; 339 } else { 340 if (tp->t_flags & TF_ACKNOW) 341 tcpstat.tcps_sndacks++; 342 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 343 tcpstat.tcps_sndctrl++; 344 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 345 tcpstat.tcps_sndurg++; 346 else 347 tcpstat.tcps_sndwinup++; 348 349 MGETHDR(m, M_DONTWAIT, MT_HEADER); 350 if (m == NULL) { 351 error = ENOBUFS; 352 goto out; 353 } 354 m->m_data += max_linkhdr; 355 m->m_len = hdrlen; 356 } 357 m->m_pkthdr.rcvif = (struct ifnet *)0; 358 ti = mtod(m, struct tcpiphdr *); 359 if (tp->t_template == 0) 360 panic("tcp_output"); 361 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 362 363 /* 364 * Fill in fields, remembering maximum advertised 365 * window for use in delaying messages about window sizes. 366 * If resending a FIN, be sure not to use a new sequence number. 367 */ 368 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 369 tp->snd_nxt == tp->snd_max) 370 tp->snd_nxt--; 371 ti->ti_seq = htonl(tp->snd_nxt); 372 ti->ti_ack = htonl(tp->rcv_nxt); 373 if (optlen) { 374 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen); 375 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 376 } 377 ti->ti_flags = flags; 378 /* 379 * Calculate receive window. Don't shrink window, 380 * but avoid silly window syndrome. 381 */ 382 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 383 win = 0; 384 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 385 win = (long)TCP_MAXWIN << tp->rcv_scale; 386 if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 387 win = (long)(tp->rcv_adv - tp->rcv_nxt); 388 ti->ti_win = htons((u_short) (win>>tp->rcv_scale)); 389 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 390 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 391 ti->ti_flags |= TH_URG; 392 } else 393 /* 394 * If no urgent pointer to send, then we pull 395 * the urgent pointer to the left edge of the send window 396 * so that it doesn't drift into the send window on sequence 397 * number wraparound. 398 */ 399 tp->snd_up = tp->snd_una; /* drag it along */ 400 401 /* 402 * Put TCP length in extended header, and then 403 * checksum extended header and data. 404 */ 405 if (len + optlen) 406 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + 407 optlen + len)); 408 ti->ti_sum = in_cksum(m, (int)(hdrlen + len)); 409 410 /* 411 * In transmit state, time the transmission and arrange for 412 * the retransmit. In persist state, just set snd_max. 413 */ 414 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 415 tcp_seq startseq = tp->snd_nxt; 416 417 /* 418 * Advance snd_nxt over sequence space of this segment. 419 */ 420 if (flags & (TH_SYN|TH_FIN)) { 421 if (flags & TH_SYN) 422 tp->snd_nxt++; 423 if (flags & TH_FIN) { 424 tp->snd_nxt++; 425 tp->t_flags |= TF_SENTFIN; 426 } 427 } 428 tp->snd_nxt += len; 429 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 430 tp->snd_max = tp->snd_nxt; 431 /* 432 * Time this transmission if not a retransmission and 433 * not currently timing anything. 434 */ 435 if (tp->t_rtt == 0) { 436 tp->t_rtt = 1; 437 tp->t_rtseq = startseq; 438 tcpstat.tcps_segstimed++; 439 } 440 } 441 442 /* 443 * Set retransmit timer if not currently set, 444 * and not doing an ack or a keep-alive probe. 445 * Initial value for retransmit timer is smoothed 446 * round-trip time + 2 * round-trip time variance. 447 * Initialize shift counter which is used for backoff 448 * of retransmit time. 449 */ 450 if (tp->t_timer[TCPT_REXMT] == 0 && 451 tp->snd_nxt != tp->snd_una) { 452 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 453 if (tp->t_timer[TCPT_PERSIST]) { 454 tp->t_timer[TCPT_PERSIST] = 0; 455 tp->t_rxtshift = 0; 456 } 457 } 458 } else 459 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 460 tp->snd_max = tp->snd_nxt + len; 461 462 /* 463 * Trace. 464 */ 465 if (so->so_options & SO_DEBUG) 466 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 467 468 /* 469 * Fill in IP length and desired time to live and 470 * send to IP level. There should be a better way 471 * to handle ttl and tos; we could keep them in 472 * the template, but need a way to checksum without them. 473 */ 474 m->m_pkthdr.len = hdrlen + len; 475 #ifdef TUBA 476 if (tp->t_tuba_pcb) 477 error = tuba_output(m, tp); 478 else 479 #endif 480 { 481 ((struct ip *)ti)->ip_len = m->m_pkthdr.len; 482 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */ 483 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */ 484 #if BSD >= 43 485 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 486 so->so_options & SO_DONTROUTE, 0); 487 #else 488 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route, 489 so->so_options & SO_DONTROUTE); 490 #endif 491 } 492 if (error) { 493 out: 494 if (error == ENOBUFS) { 495 tcp_quench(tp->t_inpcb); 496 return (0); 497 } 498 if ((error == EHOSTUNREACH || error == ENETDOWN) 499 && TCPS_HAVERCVDSYN(tp->t_state)) { 500 tp->t_softerror = error; 501 return (0); 502 } 503 return (error); 504 } 505 tcpstat.tcps_sndtotal++; 506 507 /* 508 * Data sent (as far as we can tell). 509 * If this advertises a larger window than any other segment, 510 * then remember the size of the advertised window. 511 * Any pending ACK has now been sent. 512 */ 513 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 514 tp->rcv_adv = tp->rcv_nxt + win; 515 tp->last_ack_sent = tp->rcv_nxt; 516 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 517 if (sendalot) 518 goto again; 519 return (0); 520 } 521 522 tcp_setpersist(tp) 523 register struct tcpcb *tp; 524 { 525 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 526 527 if (tp->t_timer[TCPT_REXMT]) 528 panic("tcp_output REXMT"); 529 /* 530 * Start/restart persistance timer. 531 */ 532 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 533 t * tcp_backoff[tp->t_rxtshift], 534 TCPTV_PERSMIN, TCPTV_PERSMAX); 535 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 536 tp->t_rxtshift++; 537 } 538