1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)tcp_output.c 7.4 (Berkeley) 03/20/87 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "mbuf.h" 12 #include "protosw.h" 13 #include "socket.h" 14 #include "socketvar.h" 15 #include "errno.h" 16 17 #include "../net/route.h" 18 19 #include "in.h" 20 #include "in_pcb.h" 21 #include "in_systm.h" 22 #include "ip.h" 23 #include "ip_var.h" 24 #include "tcp.h" 25 #define TCPOUTFLAGS 26 #include "tcp_fsm.h" 27 #include "tcp_seq.h" 28 #include "tcp_timer.h" 29 #include "tcp_var.h" 30 #include "tcpip.h" 31 #include "tcp_debug.h" 32 33 /* 34 * Initial options. 35 */ 36 u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 37 38 /* 39 * Tcp output routine: figure out what should be sent and send it. 40 */ 41 tcp_output(tp) 42 register struct tcpcb *tp; 43 { 44 register struct socket *so = tp->t_inpcb->inp_socket; 45 register int len, win; 46 struct mbuf *m0; 47 int off, flags, error; 48 register struct mbuf *m; 49 register struct tcpiphdr *ti; 50 u_char *opt; 51 unsigned optlen = 0; 52 int idle, sendalot; 53 54 /* 55 * Determine length of data that should be transmitted, 56 * and flags that will be used. 57 * If there is some data or critical controls (SYN, RST) 58 * to send, then transmit; otherwise, investigate further. 59 */ 60 idle = (tp->snd_max == tp->snd_una); 61 again: 62 sendalot = 0; 63 off = tp->snd_nxt - tp->snd_una; 64 win = MIN(tp->snd_wnd, tp->snd_cwnd); 65 66 /* 67 * If in persist timeout with window of 0, send 1 byte. 68 * Otherwise, if window is small but nonzero 69 * and timer expired, we will send what we can 70 * and go to transmit state. 71 */ 72 if (tp->t_force) { 73 if (win == 0) 74 win = 1; 75 else { 76 tp->t_timer[TCPT_PERSIST] = 0; 77 tp->t_rxtshift = 0; 78 } 79 } 80 81 len = MIN(so->so_snd.sb_cc, win) - off; 82 flags = tcp_outflags[tp->t_state]; 83 84 if (len < 0) { 85 /* 86 * If FIN has been sent but not acked, 87 * but we haven't been called to retransmit, 88 * len will be -1; transmit if acking, otherwise no need. 89 * Otherwise, window shrank after we sent into it. 90 * If window shrank to 0, cancel pending retransmit 91 * and pull snd_nxt back to (closed) window. 92 * We will enter persist state below. 93 * If the window didn't close completely, 94 * just wait for an ACK. 95 */ 96 if (flags & TH_FIN) { 97 if (tp->t_flags & TF_ACKNOW) 98 len = 0; 99 else 100 return (0); 101 } else if (win == 0) { 102 tp->t_timer[TCPT_REXMT] = 0; 103 tp->snd_nxt = tp->snd_una; 104 len = 0; 105 } else 106 return (0); 107 } 108 if (len > tp->t_maxseg) { 109 len = tp->t_maxseg; 110 /* 111 * Don't send more than one segment if retransmitting 112 * (or persisting, but then we shouldn't be here). 113 */ 114 if (tp->t_rxtshift == 0) 115 sendalot = 1; 116 } 117 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 118 flags &= ~TH_FIN; 119 win = sbspace(&so->so_rcv); 120 121 122 /* 123 * If our state indicates that FIN should be sent 124 * and we have not yet done so, or we're retransmitting the FIN, 125 * then we need to send. 126 */ 127 if (flags & TH_FIN && 128 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 129 goto send; 130 /* 131 * Send if we owe peer an ACK. 132 */ 133 if (tp->t_flags & TF_ACKNOW) 134 goto send; 135 if (flags & (TH_SYN|TH_RST)) 136 goto send; 137 if (SEQ_GT(tp->snd_up, tp->snd_una)) 138 goto send; 139 140 /* 141 * Sender silly window avoidance. If connection is idle 142 * and can send all data, a maximum segment, 143 * at least a maximum default-size segment do it, 144 * or are forced, do it; otherwise don't bother. 145 * If peer's buffer is tiny, then send 146 * when window is at least half open. 147 * If retransmitting (possibly after persist timer forced us 148 * to send into a small window), then must resend. 149 */ 150 if (len) { 151 if (len == tp->t_maxseg || len >= TCP_MSS) /* a lot */ 152 goto send; 153 if ((idle || tp->t_flags & TF_NODELAY) && 154 len + off >= so->so_snd.sb_cc) 155 goto send; 156 if (tp->t_force) 157 goto send; 158 if (len >= tp->max_sndwnd / 2) 159 goto send; 160 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 161 goto send; 162 } 163 164 /* 165 * Compare available window to amount of window 166 * known to peer (as advertised window less 167 * next expected input.) If the difference is 35% or more of the 168 * maximum possible window, then want to send a window update to peer. 169 */ 170 if (win > 0 && 171 ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 172 goto send; 173 174 /* 175 * TCP window updates are not reliable, rather a polling protocol 176 * using ``persist'' packets is used to insure receipt of window 177 * updates. The three ``states'' for the output side are: 178 * idle not doing retransmits or persists 179 * persisting to move a small or zero window 180 * (re)transmitting and thereby not persisting 181 * 182 * tp->t_timer[TCPT_PERSIST] 183 * is set when we are in persist state. 184 * tp->t_force 185 * is set when we are called to send a persist packet. 186 * tp->t_timer[TCPT_REXMT] 187 * is set when we are retransmitting 188 * The output side is idle when both timers are zero. 189 * 190 * If send window is too small, there is data to transmit, and no 191 * retransmit or persist is pending, then go to persist state. 192 * If nothing happens soon, send when timer expires: 193 * if window is nonzero, transmit what we can, 194 * otherwise force out a byte. 195 */ 196 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 197 tp->t_timer[TCPT_PERSIST] == 0) { 198 tp->t_rxtshift = 0; 199 tcp_setpersist(tp); 200 } 201 202 /* 203 * No reason to send a segment, just return. 204 */ 205 return (0); 206 207 send: 208 /* 209 * Grab a header mbuf, attaching a copy of data to 210 * be transmitted, and initialize the header from 211 * the template for sends on this connection. 212 */ 213 MGET(m, M_DONTWAIT, MT_HEADER); 214 if (m == NULL) 215 return (ENOBUFS); 216 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 217 m->m_len = sizeof (struct tcpiphdr); 218 if (len) { 219 if (tp->t_force && len == 1) 220 tcpstat.tcps_sndprobe++; 221 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 222 tcpstat.tcps_sndrexmitpack++; 223 tcpstat.tcps_sndrexmitbyte += len; 224 } else { 225 tcpstat.tcps_sndpack++; 226 tcpstat.tcps_sndbyte += len; 227 } 228 m->m_next = m_copy(so->so_snd.sb_mb, off, len); 229 if (m->m_next == 0) 230 len = 0; 231 } else if (tp->t_flags & TF_ACKNOW) 232 tcpstat.tcps_sndacks++; 233 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 234 tcpstat.tcps_sndctrl++; 235 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 236 tcpstat.tcps_sndurg++; 237 else 238 tcpstat.tcps_sndwinup++; 239 240 ti = mtod(m, struct tcpiphdr *); 241 if (tp->t_template == 0) 242 panic("tcp_output"); 243 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 244 245 /* 246 * Fill in fields, remembering maximum advertised 247 * window for use in delaying messages about window sizes. 248 * If resending a FIN, be sure not to use a new sequence number. 249 */ 250 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && len == 0) 251 tp->snd_nxt--; 252 ti->ti_seq = htonl(tp->snd_nxt); 253 ti->ti_ack = htonl(tp->rcv_nxt); 254 /* 255 * Before ESTABLISHED, force sending of initial options 256 * unless TCP set to not do any options. 257 */ 258 opt = NULL; 259 if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) { 260 u_short mss; 261 262 mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 263 if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 264 opt = tcp_initopt; 265 optlen = sizeof (tcp_initopt); 266 *(u_short *)(opt + 2) = htons(mss); 267 } 268 } else if (tp->t_tcpopt) { 269 opt = mtod(tp->t_tcpopt, u_char *); 270 optlen = tp->t_tcpopt->m_len; 271 } 272 if (opt) { 273 m0 = m->m_next; 274 m->m_next = m_get(M_DONTWAIT, MT_DATA); 275 if (m->m_next == 0) { 276 (void) m_free(m); 277 m_freem(m0); 278 return (ENOBUFS); 279 } 280 m->m_next->m_next = m0; 281 m0 = m->m_next; 282 m0->m_len = optlen; 283 bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 284 opt = (u_char *)(mtod(m0, caddr_t) + optlen); 285 while (m0->m_len & 0x3) { 286 *opt++ = TCPOPT_EOL; 287 m0->m_len++; 288 } 289 optlen = m0->m_len; 290 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 291 } 292 ti->ti_flags = flags; 293 /* 294 * Calculate receive window. Don't shrink window, 295 * but avoid silly window syndrome. 296 */ 297 if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 298 win = 0; 299 if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 300 win = (int)(tp->rcv_adv - tp->rcv_nxt); 301 ti->ti_win = htons((u_short)win); 302 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 303 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 304 ti->ti_flags |= TH_URG; 305 } else 306 /* 307 * If no urgent pointer to send, then we pull 308 * the urgent pointer to the left edge of the send window 309 * so that it doesn't drift into the send window on sequence 310 * number wraparound. 311 */ 312 tp->snd_up = tp->snd_una; /* drag it along */ 313 /* 314 * If anything to send and we can send it all, set PUSH. 315 * (This will keep happy those implementations which only 316 * give data to the user when a buffer fills or a PUSH comes in.) 317 */ 318 if (len && off+len == so->so_snd.sb_cc) 319 ti->ti_flags |= TH_PUSH; 320 321 /* 322 * Put TCP length in extended header, and then 323 * checksum extended header and data. 324 */ 325 if (len + optlen) 326 ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 327 optlen + len)); 328 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 329 330 /* 331 * In transmit state, time the transmission and arrange for 332 * the retransmit. In persist state, just set snd_max. 333 */ 334 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 335 /* 336 * Advance snd_nxt over sequence space of this segment. 337 */ 338 if (flags & TH_SYN) 339 tp->snd_nxt++; 340 if (flags & TH_FIN) { 341 tp->snd_nxt++; 342 tp->t_flags |= TF_SENTFIN; 343 } 344 tp->snd_nxt += len; 345 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 346 tp->snd_max = tp->snd_nxt; 347 /* 348 * Time this transmission if not a retransmission and 349 * not currently timing anything. 350 */ 351 if (tp->t_rtt == 0) { 352 tp->t_rtt = 1; 353 tp->t_rtseq = tp->snd_nxt - len; 354 tcpstat.tcps_segstimed++; 355 } 356 } 357 358 /* 359 * Set retransmit timer if not currently set, 360 * and not doing an ack or a keep-alive probe. 361 * Initial value for retransmit timer is tcp_beta*tp->t_srtt. 362 * Initialize shift counter which is used for backoff 363 * of retransmit time. 364 */ 365 if (tp->t_timer[TCPT_REXMT] == 0 && 366 tp->snd_nxt != tp->snd_una) { 367 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 368 tcp_beta * (tp->t_srtt ? tp->t_srtt : TCPTV_SRTTDFLT), 369 TCPTV_MIN, TCPTV_MAX); 370 tp->t_rxtshift = 0; 371 tp->t_timer[TCPT_PERSIST] = 0; 372 } 373 } else 374 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 375 tp->snd_max = tp->snd_nxt + len; 376 377 /* 378 * Trace. 379 */ 380 if (so->so_options & SO_DEBUG) 381 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 382 383 /* 384 * Fill in IP length and desired time to live and 385 * send to IP level. 386 */ 387 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 388 ((struct ip *)ti)->ip_ttl = TCP_TTL; 389 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 390 so->so_options & SO_DONTROUTE); 391 if (error) 392 return (error); 393 tcpstat.tcps_sndtotal++; 394 395 /* 396 * Data sent (as far as we can tell). 397 * If this advertises a larger window than any other segment, 398 * then remember the size of the advertised window. 399 * Any pending ACK has now been sent. 400 */ 401 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 402 tp->rcv_adv = tp->rcv_nxt + win; 403 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 404 if (sendalot) 405 goto again; 406 return (0); 407 } 408 409 tcp_setpersist(tp) 410 register struct tcpcb *tp; 411 { 412 413 if (tp->t_timer[TCPT_REXMT]) 414 panic("tcp_output REXMT"); 415 /* 416 * Start/restart persistance timer. 417 */ 418 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 419 ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 420 TCPTV_PERSMIN, TCPTV_MAX); 421 tp->t_rxtshift++; 422 if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 423 tp->t_rxtshift = 0; 424 } 425