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