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