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