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