bug fix from shannon
[unix-history] / usr / src / sys / netinet / tcp_output.c
CommitLineData
f1b2fa5b 1/* tcp_output.c 4.20 81/11/29 */
76ee76df
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/mbuf.h"
6#include "../h/socket.h"
d52566dd 7#include "../h/socketvar.h"
0974b45c
BJ
8#include "../net/in.h"
9#include "../net/in_pcb.h"
10#include "../net/in_systm.h"
d52566dd 11#include "../net/ip.h"
eb44bfb2 12#include "../net/ip_var.h"
d52566dd 13#include "../net/tcp.h"
0974b45c 14#define TCPOUTFLAGS
d52566dd 15#include "../net/tcp_fsm.h"
0974b45c
BJ
16#include "../net/tcp_seq.h"
17#include "../net/tcp_timer.h"
18#include "../net/tcp_var.h"
19#include "../net/tcpip.h"
f1b2fa5b 20#include "../errno.h"
76ee76df 21
ea727f86 22/*
a6503abf
BJ
23 * Tcp output routine: figure out what should be sent
24 * and, if nothing, send a null segment anyways if force is nonzero
25 * (e.g. to be sure to send an ACK).
26 *
27 * This routine can be called only after SYNs have been exchanged.
ea727f86 28 */
a6503abf 29tcp_output(tp)
53a5409e 30 register struct tcpcb *tp;
ea727f86 31{
53a5409e 32 register struct socket *so = tp->t_inpcb->inp_socket;
a6503abf
BJ
33 register int len;
34 struct mbuf *m0;
35 int off, flags;
36 register struct mbuf *m;
37 register struct tcpiphdr *ti;
38 int win;
76ee76df 39
a6503abf 40COUNT(TCP_OUTPUT);
76ee76df 41
a6503abf 42 /*
0974b45c
BJ
43 * Determine length of data that can be transmitted,
44 * and flags that will be used.
45 * If there is some data or critical controls (SYN, RST)
46 * to send, then transmit; otherwise, investigate further.
a6503abf
BJ
47 */
48 off = tp->snd_nxt - tp->snd_una;
49 len = MIN(so->so_snd.sb_cc, tp->snd_wnd) - off;
0974b45c
BJ
50 if (len > tp->t_maxseg)
51 len = tp->t_maxseg;
52 flags = tcp_outflags[tp->t_state];
53 if (len || (flags & (TH_SYN|TH_RST)))
a6503abf
BJ
54 goto send;
55
56 /*
0974b45c 57 * See if we owe peer an ACK or have a unacked FIN to send.
a6503abf 58 */
0974b45c 59 if (tp->t_flags & TF_ACKNOW)
a6503abf 60 goto send;
0974b45c
BJ
61 if ((so->so_state & SS_CANTSENDMORE) &&
62 TCPS_OURFINNOTACKED(tp->t_state))
a6503abf 63 goto send;
76ee76df 64
a6503abf
BJ
65 /*
66 * Calculate available window in i, and also amount
67 * of window known to peer (as advertised window less
68 * next expected input.) If this is 35% or more of the
69 * maximum possible window, then want to send a segment to peer.
70 */
0974b45c
BJ
71 win = sbspace(&so->so_rcv);
72 if (win > 0 &&
73 ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
a6503abf
BJ
74 goto send;
75
76 /*
77 * No reason to send a segment, just return.
78 */
f1b2fa5b 79 return (0);
a6503abf
BJ
80
81send:
82 /*
83 * Grab a header mbuf, attaching a copy of data to
84 * be transmitted, and initialize the header from
85 * the template for sends on this connection.
86 */
76ee76df
BJ
87 MGET(m, 0);
88 if (m == 0)
89 return (0);
53a5409e
BJ
90 m->m_off = MMAXOFF - sizeof(struct tcpiphdr);
91 m->m_len = sizeof (struct tcpiphdr);
a6503abf
BJ
92 if (len) {
93 m->m_next = m_copy(so->so_snd.sb_mb, off, len);
94 if (m->m_next == 0)
95 len = 0;
96 }
97 ti = mtod(m, struct tcpiphdr *);
98 if (tp->t_template == 0)
99 panic("tcp_output");
f1b2fa5b 100 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
a6503abf
BJ
101
102 /*
103 * Fill in fields, remembering maximum advertised
104 * window for use in delaying messages about window sizes.
105 */
106 ti->ti_seq = htonl(tp->snd_nxt);
0974b45c
BJ
107 ti->ti_ack = htonl(tp->rcv_nxt);
108 if (tp->t_tcpopt) {
f1b2fa5b 109 m0 = m->m_next;
0974b45c
BJ
110 m->m_next = m_get(0);
111 if (m->m_next == 0) {
112 (void) m_free(m);
f1b2fa5b 113 m_freem(m);
0974b45c
BJ
114 return (0);
115 }
116 m->m_next->m_next = m0;
117 m->m_off = MMINOFF;
118 m->m_len = tp->t_tcpopt->m_len;
119 bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
f1b2fa5b 120 (unsigned)tp->t_tcpopt->m_len);
0974b45c
BJ
121 ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
122 }
123 ti->ti_flags = flags;
a6503abf
BJ
124 win = sbspace(&so->so_rcv);
125 if (win > 0)
f1b2fa5b 126 ti->ti_win = htons((u_short)win);
0974b45c
BJ
127 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
128 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
a6503abf
BJ
129 ti->ti_flags |= TH_URG;
130 } else
131 /*
132 * If no urgent pointer to send, then we pull
133 * the urgent pointer to the left edge of the send window
134 * so that it doesn't drift into the send window on sequence
135 * number wraparound.
136 */
0974b45c
BJ
137 tp->snd_up = tp->snd_una; /* drag it along */
138 /* PUSH */
a6503abf
BJ
139
140 /*
141 * Put TCP length in extended header, and then
142 * checksum extended header and data.
143 */
144 if (len)
145 ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr)));
0974b45c
BJ
146 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len);
147
148 /*
149 * Advance snd_nxt over sequence space of this segment
150 */
151 if (flags & (TH_SYN|TH_FIN))
152 len++;
153 tp->snd_nxt += len;
154
155 /*
156 * Arrange for retransmit and time this transmit if
157 * not already a retransmit and sending either data,
158 * SYN or FIN.
159 */
160 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
161 tp->rxt_seq = tp->snd_nxt - len;
162 tp->rxt_time = 0;
163 tp->rxt_cnt = 0;
164 tp->t_timer[TCPT_REXMT] = 0; /* XXX */
165 }
a6503abf
BJ
166
167 /*
168 * Fill in IP length and desired time to live and
169 * send to IP level.
170 */
171 ((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
172 ((struct ip *)ti)->ip_ttl = TCP_TTL;
0974b45c
BJ
173 if (ip_output(m, tp->t_ipopt) == 0)
174 return (0);
a6503abf
BJ
175
176 /*
177 * Data sent (as far as we can tell).
178 * If this advertises a larger window than any other segment,
179 * then record its sequence to be used in suppressing messages.
0974b45c 180 * Drop send for purpose of ACK requirements.
a6503abf
BJ
181 */
182 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
183 tp->rcv_adv = tp->rcv_nxt + win;
0974b45c
BJ
184 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
185 tp->snd_max = tp->snd_nxt;
186 return (1);
76ee76df 187}