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