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