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