1 /* 2 * Copyright (c) 1982 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 6.16 (Berkeley) 03/12/86 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 && off) 74 log(7, "persist offset %d\n", off); 75 if (win == 0) 76 win = 1; 77 else { 78 tp->t_timer[TCPT_PERSIST] = 0; 79 tp->t_rxtshift = 0; 80 } 81 } 82 83 len = MIN(so->so_snd.sb_cc, win) - off; 84 if (len > tp->t_maxseg) { 85 len = tp->t_maxseg; 86 /* 87 * Don't send more than one segment if retransmitting 88 * (or persisting, but then we shouldn't be here). 89 */ 90 if (tp->t_rxtshift == 0) 91 sendalot = 1; 92 } 93 94 flags = tcp_outflags[tp->t_state]; 95 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 96 flags &= ~TH_FIN; 97 98 if (len < 0) { 99 /* 100 * If closing and all data acked, len will be -1; 101 * retransmit FIN if necessary. 102 * Otherwise, window shrank after we sent into it. 103 * If window shrank to 0, cancel pending retransmit 104 * and pull snd_nxt back to (closed) window. 105 * We will enter persist state below. 106 */ 107 if (flags & TH_FIN) 108 len = 0; 109 else if (win == 0) { 110 tp->t_timer[TCPT_REXMT] = 0; 111 tp->snd_nxt = tp->snd_una; 112 len = 0; 113 } else 114 return (0); 115 } 116 win = sbspace(&so->so_rcv); 117 118 /* 119 * Send if we owe peer an ACK. 120 */ 121 if (tp->t_flags & TF_ACKNOW) 122 goto send; 123 if (flags & (TH_SYN|TH_RST|TH_FIN)) 124 goto send; 125 if (SEQ_GT(tp->snd_up, tp->snd_una)) 126 goto send; 127 128 /* 129 * Sender silly window avoidance. If connection is idle 130 * and can send all data, a maximum segment, 131 * at least a maximum default-size segment do it, 132 * or are forced, do it; otherwise don't bother. 133 * If peer's buffer is tiny, then send 134 * when window is at least half open. 135 * If retransmitting (possibly after persist timer forced us 136 * to send into a small window), then must resend. 137 */ 138 if (len) { 139 if (len == tp->t_maxseg || len >= TCP_MSS) /* a lot */ 140 goto send; 141 if ((idle || tp->t_flags & TF_NODELAY) && 142 len + off >= so->so_snd.sb_cc) 143 goto send; 144 if (tp->t_force) 145 goto send; 146 if (len >= tp->max_sndwnd / 2) 147 goto send; 148 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 149 goto send; 150 } 151 152 /* 153 * Compare available window to amount of window 154 * known to peer (as advertised window less 155 * next expected input.) If the difference is 35% or more of the 156 * maximum possible window, then want to send a window update to peer. 157 */ 158 if (win > 0 && 159 ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35)) 160 goto send; 161 162 /* 163 * TCP window updates are not reliable, rather a polling protocol 164 * using ``persist'' packets is used to insure receipt of window 165 * updates. The three ``states'' for the output side are: 166 * idle not doing retransmits or persists 167 * persisting to move a small or zero window 168 * (re)transmitting and thereby not persisting 169 * 170 * tp->t_timer[TCPT_PERSIST] 171 * is set when we are in persist state. 172 * tp->t_force 173 * is set when we are called to send a persist packet. 174 * tp->t_timer[TCPT_REXMT] 175 * is set when we are retransmitting 176 * The output side is idle when both timers are zero. 177 * 178 * If send window is too small, there is data to transmit, and no 179 * retransmit or persist is pending, then go to persist state. 180 * If nothing happens soon, send when timer expires: 181 * if window is nonzero, transmit what we can, 182 * otherwise force out a byte. 183 */ 184 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 185 tp->t_timer[TCPT_PERSIST] == 0) { 186 tp->t_rxtshift = 0; 187 tcp_setpersist(tp); 188 } 189 190 /* 191 * No reason to send a segment, just return. 192 */ 193 return (0); 194 195 send: 196 /* 197 * Grab a header mbuf, attaching a copy of data to 198 * be transmitted, and initialize the header from 199 * the template for sends on this connection. 200 */ 201 MGET(m, M_DONTWAIT, MT_HEADER); 202 if (m == NULL) 203 return (ENOBUFS); 204 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 205 m->m_len = sizeof (struct tcpiphdr); 206 if (len) { 207 m->m_next = m_copy(so->so_snd.sb_mb, off, len); 208 if (m->m_next == 0) 209 len = 0; 210 } 211 ti = mtod(m, struct tcpiphdr *); 212 if (tp->t_template == 0) 213 panic("tcp_output"); 214 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 215 216 /* 217 * Fill in fields, remembering maximum advertised 218 * window for use in delaying messages about window sizes. 219 */ 220 ti->ti_seq = htonl(tp->snd_nxt); 221 ti->ti_ack = htonl(tp->rcv_nxt); 222 /* 223 * Before ESTABLISHED, force sending of initial options 224 * unless TCP set to not do any options. 225 */ 226 opt = NULL; 227 if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) { 228 u_short mss; 229 230 mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp)); 231 if (mss > IP_MSS - sizeof(struct tcpiphdr)) { 232 opt = tcp_initopt; 233 optlen = sizeof (tcp_initopt); 234 *(u_short *)(opt + 2) = htons(mss); 235 } 236 } else if (tp->t_tcpopt) { 237 opt = mtod(tp->t_tcpopt, u_char *); 238 optlen = tp->t_tcpopt->m_len; 239 } 240 if (opt) { 241 m0 = m->m_next; 242 m->m_next = m_get(M_DONTWAIT, MT_DATA); 243 if (m->m_next == 0) { 244 (void) m_free(m); 245 m_freem(m0); 246 return (ENOBUFS); 247 } 248 m->m_next->m_next = m0; 249 m0 = m->m_next; 250 m0->m_len = optlen; 251 bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen); 252 opt = (u_char *)(mtod(m0, caddr_t) + optlen); 253 while (m0->m_len & 0x3) { 254 *opt++ = TCPOPT_EOL; 255 m0->m_len++; 256 } 257 optlen = m0->m_len; 258 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 259 } 260 ti->ti_flags = flags; 261 /* 262 * Calculate receive window. Don't shrink window, 263 * but avoid silly window syndrome. 264 */ 265 if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg) 266 win = 0; 267 if (win < (int)(tp->rcv_adv - tp->rcv_nxt)) 268 win = (int)(tp->rcv_adv - tp->rcv_nxt); 269 ti->ti_win = htons((u_short)win); 270 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 271 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 272 ti->ti_flags |= TH_URG; 273 } else 274 /* 275 * If no urgent pointer to send, then we pull 276 * the urgent pointer to the left edge of the send window 277 * so that it doesn't drift into the send window on sequence 278 * number wraparound. 279 */ 280 tp->snd_up = tp->snd_una; /* drag it along */ 281 /* 282 * If anything to send and we can send it all, set PUSH. 283 * (This will keep happy those implementations which only 284 * give data to the user when a buffer fills or a PUSH comes in.) 285 */ 286 if (len && off+len == so->so_snd.sb_cc) 287 ti->ti_flags |= TH_PUSH; 288 289 /* 290 * Put TCP length in extended header, and then 291 * checksum extended header and data. 292 */ 293 if (len + optlen) 294 ti->ti_len = htons((u_short)(sizeof(struct tcphdr) + 295 optlen + len)); 296 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len); 297 298 /* 299 * In transmit state, time the transmission and arrange for 300 * the retransmit. In persist state, just set snd_max. 301 */ 302 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 303 /* 304 * Advance snd_nxt over sequence space of this segment. 305 */ 306 if (flags & (TH_SYN|TH_FIN)) 307 tp->snd_nxt++; 308 tp->snd_nxt += len; 309 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 310 tp->snd_max = tp->snd_nxt; 311 /* 312 * Time this transmission if not a retransmission and 313 * not currently timing anything. 314 */ 315 if (tp->t_rtt == 0) { 316 tp->t_rtt = 1; 317 tp->t_rtseq = tp->snd_nxt - len; 318 } 319 } 320 321 /* 322 * Set retransmit timer if not currently set, 323 * and not doing an ack or a keep-alive probe. 324 * Initial value for retransmit timer is tcp_beta*tp->t_srtt. 325 * Initialize shift counter which is used for backoff 326 * of retransmit time. 327 */ 328 if (tp->t_timer[TCPT_REXMT] == 0 && 329 tp->snd_nxt != tp->snd_una) { 330 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 331 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 332 tp->t_rxtshift = 0; 333 tp->t_timer[TCPT_PERSIST] = 0; 334 } 335 } else 336 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 337 tp->snd_max = tp->snd_nxt + len; 338 339 /* 340 * Trace. 341 */ 342 if (so->so_options & SO_DEBUG) 343 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 344 345 /* 346 * Fill in IP length and desired time to live and 347 * send to IP level. 348 */ 349 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len; 350 ((struct ip *)ti)->ip_ttl = TCP_TTL; 351 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 352 so->so_options & SO_DONTROUTE); 353 if (error) 354 return (error); 355 356 /* 357 * Data sent (as far as we can tell). 358 * If this advertises a larger window than any other segment, 359 * then remember the size of the advertised window. 360 * Any pending ACK has now been sent. 361 */ 362 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 363 tp->rcv_adv = tp->rcv_nxt + win; 364 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); 365 if (sendalot) 366 goto again; 367 return (0); 368 } 369 370 tcp_setpersist(tp) 371 register struct tcpcb *tp; 372 { 373 374 if (tp->t_timer[TCPT_REXMT]) 375 panic("tcp_output REXMT"); 376 /* 377 * Start/restart persistance timer. 378 */ 379 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 380 ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift, 381 TCPTV_PERSMIN, TCPTV_MAX); 382 tp->t_rxtshift++; 383 if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) 384 tp->t_rxtshift = 0; 385 } 386