1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)tcp_output.c 7.19 (Berkeley) 04/03/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "malloc.h" 23 #include "mbuf.h" 24 #include "protosw.h" 25 #include "socket.h" 26 #include "socketvar.h" 27 #include "errno.h" 28 29 #include "../net/route.h" 30 31 #include "in.h" 32 #include "in_systm.h" 33 #include "ip.h" 34 #include "in_pcb.h" 35 #include "ip_var.h" 36 #include "tcp.h" 37 #define TCPOUTFLAGS 38 #include "tcp_fsm.h" 39 #include "tcp_seq.h" 40 #include "tcp_timer.h" 41 #include "tcp_var.h" 42 #include "tcpip.h" 43 #include "tcp_debug.h" 44 45 /* 46 * Initial options. 47 */ 48 u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, }; 49 50 /* 51 * Tcp output routine: figure out what should be sent and send it. 52 */ 53 tcp_output(tp) 54 register struct tcpcb *tp; 55 { 56 register struct socket *so = tp->t_inpcb->inp_socket; 57 register long len, win; 58 struct mbuf *m0; 59 int off, flags, error; 60 register struct mbuf *m; 61 register struct tcpiphdr *ti; 62 u_char *opt; 63 unsigned optlen = 0; 64 int idle, sendalot; 65 66 /* 67 * Determine length of data that should be transmitted, 68 * and flags that will be used. 69 * If there is some data or critical controls (SYN, RST) 70 * to send, then transmit; otherwise, investigate further. 71 */ 72 idle = (tp->snd_max == tp->snd_una); 73 again: 74 sendalot = 0; 75 off = tp->snd_nxt - tp->snd_una; 76 win = min(tp->snd_wnd, tp->snd_cwnd); 77 78 /* 79 * If in persist timeout with window of 0, send 1 byte. 80 * Otherwise, if window is small but nonzero 81 * and timer expired, we will send what we can 82 * and go to transmit state. 83 */ 84 if (tp->t_force) { 85 if (win == 0) 86 win = 1; 87 else { 88 tp->t_timer[TCPT_PERSIST] = 0; 89 tp->t_rxtshift = 0; 90 } 91 } 92 93 len = min(so->so_snd.sb_cc, win) - off; 94 flags = tcp_outflags[tp->t_state]; 95 96 if (len < 0) { 97 /* 98 * If FIN has been sent but not acked, 99 * but we haven't been called to retransmit, 100 * len will be -1. Otherwise, window shrank 101 * after we sent into it. If window shrank to 0, 102 * cancel pending retransmit and pull snd_nxt 103 * back to (closed) window. We will enter persist 104 * state below. If the window didn't close completely, 105 * just wait for an ACK. 106 */ 107 len = 0; 108 if (win == 0) { 109 tp->t_timer[TCPT_REXMT] = 0; 110 tp->snd_nxt = tp->snd_una; 111 } 112 } 113 if (len > tp->t_maxseg) { 114 len = tp->t_maxseg; 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) 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 at least two 168 * max size segments, or at least 50% of the maximum possible 169 * window, then want to send a window update to peer. 170 */ 171 if (win > 0) { 172 int adv = win - (tp->rcv_adv - tp->rcv_nxt); 173 174 if (adv >= 2 * tp->t_maxseg) 175 goto send; 176 if (2 * adv >= so->so_rcv.sb_hiwat) 177 goto send; 178 } 179 180 /* 181 * TCP window updates are not reliable, rather a polling protocol 182 * using ``persist'' packets is used to insure receipt of window 183 * updates. The three ``states'' for the output side are: 184 * idle not doing retransmits or persists 185 * persisting to move a small or zero window 186 * (re)transmitting and thereby not persisting 187 * 188 * tp->t_timer[TCPT_PERSIST] 189 * is set when we are in persist state. 190 * tp->t_force 191 * is set when we are called to send a persist packet. 192 * tp->t_timer[TCPT_REXMT] 193 * is set when we are retransmitting 194 * The output side is idle when both timers are zero. 195 * 196 * If send window is too small, there is data to transmit, and no 197 * retransmit or persist is pending, then go to persist state. 198 * If nothing happens soon, send when timer expires: 199 * if window is nonzero, transmit what we can, 200 * otherwise force out a byte. 201 */ 202 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 203 tp->t_timer[TCPT_PERSIST] == 0) { 204 tp->t_rxtshift = 0; 205 tcp_setpersist(tp); 206 } 207 208 /* 209 * No reason to send a segment, just return. 210 */ 211 return (0); 212 213 send: 214 /* 215 * Grab a header mbuf, attaching a copy of data to 216 * be transmitted, and initialize the header from 217 * the template for sends on this connection. 218 */ 219 MGETHDR(m, M_DONTWAIT, MT_HEADER); 220 if (m == NULL) 221 return (ENOBUFS); 222 m->m_data += max_linkhdr; 223 m->m_len = sizeof (struct tcpiphdr); 224 m->m_pkthdr.rcvif = (struct ifnet *)0; 225 ti = mtod(m, struct tcpiphdr *); 226 if (len) { 227 if (tp->t_force && len == 1) 228 tcpstat.tcps_sndprobe++; 229 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 230 tcpstat.tcps_sndrexmitpack++; 231 tcpstat.tcps_sndrexmitbyte += len; 232 } else { 233 tcpstat.tcps_sndpack++; 234 tcpstat.tcps_sndbyte += len; 235 } 236 if (len <= MHLEN - sizeof (struct tcpiphdr) - max_linkhdr) { 237 m_copydata(so->so_snd.sb_mb, off, (int) len, 238 mtod(m, caddr_t) + sizeof(struct tcpiphdr)); 239 m->m_len += len; 240 } else { 241 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 242 if (m->m_next == 0) 243 len = 0; 244 } 245 } else if (tp->t_flags & TF_ACKNOW) 246 tcpstat.tcps_sndacks++; 247 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 248 tcpstat.tcps_sndctrl++; 249 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 250 tcpstat.tcps_sndurg++; 251 else 252 tcpstat.tcps_sndwinup++; 253 254 if (tp->t_template == 0) 255 panic("tcp_output"); 256 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 257 258 /* 259 * Fill in fields, remembering maximum advertised 260 * window for use in delaying messages about window sizes. 261 * If resending a FIN, be sure not to use a new sequence number. 262 */ 263 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 264 tp->snd_nxt == tp->snd_max) 265 tp->snd_nxt--; 266 ti->ti_seq = htonl(tp->snd_nxt); 267 ti->ti_ack = htonl(tp->rcv_nxt); 268 /* 269 * Before ESTABLISHED, force sending of initial options 270 * unless TCP set to not do any options. 271 */ 272 opt = NULL; 273 if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) { 274 u_short mss; 275 276 mss = min(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 277 if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 278 opt = tcp_initopt; 279 optlen = sizeof (tcp_initopt); 280 *(u_short *)(opt + 2) = htons(mss); 281 } 282 } 283 if (opt) { 284 m0 = m->m_next; 285 m->m_next = m_get(M_DONTWAIT, MT_DATA); 286 if (m->m_next == 0) { 287 (void) m_free(m); 288 m_freem(m0); 289 return (ENOBUFS); 290 } 291 m->m_next->m_next = m0; 292 m0 = m->m_next; 293 m0->m_len = optlen; 294 bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 295 opt = (u_char *)(mtod(m0, caddr_t) + optlen); 296 while (m0->m_len & 0x3) { 297 *opt++ = TCPOPT_EOL; 298 m0->m_len++; 299 } 300 optlen = m0->m_len; 301 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 302 } 303 ti->ti_flags = flags; 304 /* 305 * Calculate receive window. Don't shrink window, 306 * but avoid silly window syndrome. 307 */ 308 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 309 win = 0; 310 if (win > IP_MAXPACKET) 311 win = IP_MAXPACKET; 312 if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 313 win = (long)(tp->rcv_adv - tp->rcv_nxt); 314 ti->ti_win = htons((u_short)win); 315 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 316 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 317 ti->ti_flags |= TH_URG; 318 } else 319 /* 320 * If no urgent pointer to send, then we pull 321 * the urgent pointer to the left edge of the send window 322 * so that it doesn't drift into the send window on sequence 323 * number wraparound. 324 */ 325 tp->snd_up = tp->snd_una; /* drag it along */ 326 /* 327 * If anything to send and we can send it all, set PUSH. 328 * (This will keep happy those implementations which only 329 * give data to the user when a buffer fills or a PUSH comes in.) 330 */ 331 if (len && off+len == so->so_snd.sb_cc) 332 ti->ti_flags |= TH_PUSH; 333 334 /* 335 * Put TCP length in extended header, and then 336 * checksum extended header and data. 337 */ 338 if (len + optlen) 339 ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 340 optlen + len)); 341 ti->ti_sum = in_cksum(m, 342 (int)(sizeof (struct tcpiphdr) + (int)optlen + len)); 343 344 /* 345 * In transmit state, time the transmission and arrange for 346 * the retransmit. In persist state, just set snd_max. 347 */ 348 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 349 tcp_seq startseq = tp->snd_nxt; 350 351 /* 352 * Advance snd_nxt over sequence space of this segment. 353 */ 354 if (flags & TH_SYN) 355 tp->snd_nxt++; 356 if (flags & TH_FIN) { 357 tp->snd_nxt++; 358 tp->t_flags |= TF_SENTFIN; 359 } 360 tp->snd_nxt += len; 361 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 362 tp->snd_max = tp->snd_nxt; 363 /* 364 * Time this transmission if not a retransmission and 365 * not currently timing anything. 366 */ 367 if (tp->t_rtt == 0) { 368 tp->t_rtt = 1; 369 tp->t_rtseq = startseq; 370 tcpstat.tcps_segstimed++; 371 } 372 } 373 374 /* 375 * Set retransmit timer if not currently set, 376 * and not doing an ack or a keep-alive probe. 377 * Initial value for retransmit timer is smoothed 378 * round-trip time + 2 * round-trip time variance. 379 * Initialize shift counter which is used for backoff 380 * of retransmit time. 381 */ 382 if (tp->t_timer[TCPT_REXMT] == 0 && 383 tp->snd_nxt != tp->snd_una) { 384 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 385 if (tp->t_timer[TCPT_PERSIST]) { 386 tp->t_timer[TCPT_PERSIST] = 0; 387 tp->t_rxtshift = 0; 388 } 389 } 390 } else 391 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 392 tp->snd_max = tp->snd_nxt + len; 393 394 /* 395 * Trace. 396 */ 397 if (so->so_options & SO_DEBUG) 398 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 399 400 /* 401 * Fill in IP length and desired time to live and 402 * send to IP level. 403 */ 404 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 405 if (m->m_flags & M_PKTHDR) 406 m->m_pkthdr.len = ((struct ip *)ti)->ip_len; 407 ((struct ip *)ti)->ip_ttl = tcp_ttl; 408 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 409 so->so_options & SO_DONTROUTE); 410 if (error) { 411 if (error == ENOBUFS) { 412 tcp_quench(tp->t_inpcb); 413 return (0); 414 } 415 return (error); 416 } 417 tcpstat.tcps_sndtotal++; 418 419 /* 420 * Data sent (as far as we can tell). 421 * If this advertises a larger window than any other segment, 422 * then remember the size of the advertised window. 423 * Any pending ACK has now been sent. 424 */ 425 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 426 tp->rcv_adv = tp->rcv_nxt + win; 427 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 428 if (sendalot) 429 goto again; 430 return (0); 431 } 432 433 tcp_setpersist(tp) 434 register struct tcpcb *tp; 435 { 436 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 437 438 if (tp->t_timer[TCPT_REXMT]) 439 panic("tcp_output REXMT"); 440 /* 441 * Start/restart persistance timer. 442 */ 443 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 444 t * tcp_backoff[tp->t_rxtshift], 445 TCPTV_PERSMIN, TCPTV_PERSMAX); 446 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 447 tp->t_rxtshift++; 448 } 449