root / slirp / tcp_input.c @ cbeb0857
History | View | Annotate | Download (47.8 kB)
1 | f0cbd3ec | bellard | /*
|
---|---|---|---|
2 | f0cbd3ec | bellard | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
|
3 | f0cbd3ec | bellard | * The Regents of the University of California. All rights reserved.
|
4 | f0cbd3ec | bellard | *
|
5 | f0cbd3ec | bellard | * Redistribution and use in source and binary forms, with or without
|
6 | f0cbd3ec | bellard | * modification, are permitted provided that the following conditions
|
7 | f0cbd3ec | bellard | * are met:
|
8 | f0cbd3ec | bellard | * 1. Redistributions of source code must retain the above copyright
|
9 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer.
|
10 | f0cbd3ec | bellard | * 2. Redistributions in binary form must reproduce the above copyright
|
11 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer in the
|
12 | f0cbd3ec | bellard | * documentation and/or other materials provided with the distribution.
|
13 | f0cbd3ec | bellard | * 3. All advertising materials mentioning features or use of this software
|
14 | f0cbd3ec | bellard | * must display the following acknowledgement:
|
15 | f0cbd3ec | bellard | * This product includes software developed by the University of
|
16 | f0cbd3ec | bellard | * California, Berkeley and its contributors.
|
17 | f0cbd3ec | bellard | * 4. Neither the name of the University nor the names of its contributors
|
18 | f0cbd3ec | bellard | * may be used to endorse or promote products derived from this software
|
19 | f0cbd3ec | bellard | * without specific prior written permission.
|
20 | f0cbd3ec | bellard | *
|
21 | f0cbd3ec | bellard | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
22 | f0cbd3ec | bellard | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23 | f0cbd3ec | bellard | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24 | f0cbd3ec | bellard | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
25 | f0cbd3ec | bellard | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26 | f0cbd3ec | bellard | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
27 | f0cbd3ec | bellard | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
28 | f0cbd3ec | bellard | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
29 | f0cbd3ec | bellard | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
30 | f0cbd3ec | bellard | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
31 | f0cbd3ec | bellard | * SUCH DAMAGE.
|
32 | f0cbd3ec | bellard | *
|
33 | f0cbd3ec | bellard | * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
|
34 | f0cbd3ec | bellard | * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
|
35 | f0cbd3ec | bellard | */
|
36 | f0cbd3ec | bellard | |
37 | f0cbd3ec | bellard | /*
|
38 | f0cbd3ec | bellard | * Changes and additions relating to SLiRP
|
39 | f0cbd3ec | bellard | * Copyright (c) 1995 Danny Gasparovski.
|
40 | f0cbd3ec | bellard | *
|
41 | f0cbd3ec | bellard | * Please read the file COPYRIGHT for the
|
42 | f0cbd3ec | bellard | * terms and conditions of the copyright.
|
43 | f0cbd3ec | bellard | */
|
44 | f0cbd3ec | bellard | |
45 | f0cbd3ec | bellard | #include <slirp.h> |
46 | f0cbd3ec | bellard | #include "ip_icmp.h" |
47 | f0cbd3ec | bellard | |
48 | f0cbd3ec | bellard | struct socket tcb;
|
49 | f0cbd3ec | bellard | |
50 | f0cbd3ec | bellard | int tcprexmtthresh = 3; |
51 | f0cbd3ec | bellard | struct socket *tcp_last_so = &tcb;
|
52 | f0cbd3ec | bellard | |
53 | f0cbd3ec | bellard | tcp_seq tcp_iss; /* tcp initial send seq # */
|
54 | f0cbd3ec | bellard | |
55 | f0cbd3ec | bellard | #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) |
56 | f0cbd3ec | bellard | |
57 | f0cbd3ec | bellard | /* for modulo comparisons of timestamps */
|
58 | f0cbd3ec | bellard | #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) |
59 | f0cbd3ec | bellard | #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) |
60 | f0cbd3ec | bellard | |
61 | f0cbd3ec | bellard | /*
|
62 | f0cbd3ec | bellard | * Insert segment ti into reassembly queue of tcp with
|
63 | f0cbd3ec | bellard | * control block tp. Return TH_FIN if reassembly now includes
|
64 | f0cbd3ec | bellard | * a segment with FIN. The macro form does the common case inline
|
65 | f0cbd3ec | bellard | * (segment is the next to be received on an established connection,
|
66 | f0cbd3ec | bellard | * and the queue is empty), avoiding linkage into and removal
|
67 | f0cbd3ec | bellard | * from the queue and repetition of various conversions.
|
68 | f0cbd3ec | bellard | * Set DELACK for segments received in order, but ack immediately
|
69 | f0cbd3ec | bellard | * when segments are out of order (so fast retransmit can work).
|
70 | f0cbd3ec | bellard | */
|
71 | f0cbd3ec | bellard | #ifdef TCP_ACK_HACK
|
72 | f0cbd3ec | bellard | #define TCP_REASS(tp, ti, m, so, flags) {\
|
73 | f0cbd3ec | bellard | if ((ti)->ti_seq == (tp)->rcv_nxt && \
|
74 | f0cbd3ec | bellard | (tp)->seg_next == (tcpiphdrp_32)(tp) && \ |
75 | f0cbd3ec | bellard | (tp)->t_state == TCPS_ESTABLISHED) {\ |
76 | f0cbd3ec | bellard | if (ti->ti_flags & TH_PUSH) \
|
77 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; \ |
78 | f0cbd3ec | bellard | else \
|
79 | f0cbd3ec | bellard | tp->t_flags |= TF_DELACK; \ |
80 | f0cbd3ec | bellard | (tp)->rcv_nxt += (ti)->ti_len; \ |
81 | f0cbd3ec | bellard | flags = (ti)->ti_flags & TH_FIN; \ |
82 | f0cbd3ec | bellard | tcpstat.tcps_rcvpack++;\ |
83 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyte += (ti)->ti_len;\ |
84 | f0cbd3ec | bellard | if (so->so_emu) { \
|
85 | f0cbd3ec | bellard | if (tcp_emu((so),(m))) sbappend((so), (m)); \
|
86 | f0cbd3ec | bellard | } else \
|
87 | f0cbd3ec | bellard | sbappend((so), (m)); \ |
88 | f0cbd3ec | bellard | /* sorwakeup(so); */ \
|
89 | f0cbd3ec | bellard | } else {\
|
90 | f0cbd3ec | bellard | (flags) = tcp_reass((tp), (ti), (m)); \ |
91 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; \ |
92 | f0cbd3ec | bellard | } \ |
93 | f0cbd3ec | bellard | } |
94 | f0cbd3ec | bellard | #else
|
95 | f0cbd3ec | bellard | #define TCP_REASS(tp, ti, m, so, flags) { \
|
96 | f0cbd3ec | bellard | if ((ti)->ti_seq == (tp)->rcv_nxt && \
|
97 | f0cbd3ec | bellard | (tp)->seg_next == (tcpiphdrp_32)(tp) && \ |
98 | f0cbd3ec | bellard | (tp)->t_state == TCPS_ESTABLISHED) { \ |
99 | f0cbd3ec | bellard | tp->t_flags |= TF_DELACK; \ |
100 | f0cbd3ec | bellard | (tp)->rcv_nxt += (ti)->ti_len; \ |
101 | f0cbd3ec | bellard | flags = (ti)->ti_flags & TH_FIN; \ |
102 | f0cbd3ec | bellard | tcpstat.tcps_rcvpack++;\ |
103 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyte += (ti)->ti_len;\ |
104 | f0cbd3ec | bellard | if (so->so_emu) { \
|
105 | f0cbd3ec | bellard | if (tcp_emu((so),(m))) sbappend(so, (m)); \
|
106 | f0cbd3ec | bellard | } else \
|
107 | f0cbd3ec | bellard | sbappend((so), (m)); \ |
108 | f0cbd3ec | bellard | /* sorwakeup(so); */ \
|
109 | f0cbd3ec | bellard | } else { \
|
110 | f0cbd3ec | bellard | (flags) = tcp_reass((tp), (ti), (m)); \ |
111 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; \ |
112 | f0cbd3ec | bellard | } \ |
113 | f0cbd3ec | bellard | } |
114 | f0cbd3ec | bellard | #endif
|
115 | f0cbd3ec | bellard | |
116 | f0cbd3ec | bellard | int
|
117 | f0cbd3ec | bellard | tcp_reass(tp, ti, m) |
118 | f0cbd3ec | bellard | register struct tcpcb *tp; |
119 | f0cbd3ec | bellard | register struct tcpiphdr *ti; |
120 | f0cbd3ec | bellard | struct mbuf *m;
|
121 | f0cbd3ec | bellard | { |
122 | f0cbd3ec | bellard | register struct tcpiphdr *q; |
123 | f0cbd3ec | bellard | struct socket *so = tp->t_socket;
|
124 | f0cbd3ec | bellard | int flags;
|
125 | f0cbd3ec | bellard | |
126 | f0cbd3ec | bellard | /*
|
127 | f0cbd3ec | bellard | * Call with ti==0 after become established to
|
128 | f0cbd3ec | bellard | * force pre-ESTABLISHED data up to user socket.
|
129 | f0cbd3ec | bellard | */
|
130 | f0cbd3ec | bellard | if (ti == 0) |
131 | f0cbd3ec | bellard | goto present;
|
132 | f0cbd3ec | bellard | |
133 | f0cbd3ec | bellard | /*
|
134 | f0cbd3ec | bellard | * Find a segment which begins after this one does.
|
135 | f0cbd3ec | bellard | */
|
136 | f0cbd3ec | bellard | for (q = (struct tcpiphdr *)tp->seg_next; q != (struct tcpiphdr *)tp; |
137 | f0cbd3ec | bellard | q = (struct tcpiphdr *)q->ti_next)
|
138 | f0cbd3ec | bellard | if (SEQ_GT(q->ti_seq, ti->ti_seq))
|
139 | f0cbd3ec | bellard | break;
|
140 | f0cbd3ec | bellard | |
141 | f0cbd3ec | bellard | /*
|
142 | f0cbd3ec | bellard | * If there is a preceding segment, it may provide some of
|
143 | f0cbd3ec | bellard | * our data already. If so, drop the data from the incoming
|
144 | f0cbd3ec | bellard | * segment. If it provides all of our data, drop us.
|
145 | f0cbd3ec | bellard | */
|
146 | f0cbd3ec | bellard | if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { |
147 | f0cbd3ec | bellard | register int i; |
148 | f0cbd3ec | bellard | q = (struct tcpiphdr *)q->ti_prev;
|
149 | f0cbd3ec | bellard | /* conversion to int (in i) handles seq wraparound */
|
150 | f0cbd3ec | bellard | i = q->ti_seq + q->ti_len - ti->ti_seq; |
151 | f0cbd3ec | bellard | if (i > 0) { |
152 | f0cbd3ec | bellard | if (i >= ti->ti_len) {
|
153 | f0cbd3ec | bellard | tcpstat.tcps_rcvduppack++; |
154 | f0cbd3ec | bellard | tcpstat.tcps_rcvdupbyte += ti->ti_len; |
155 | f0cbd3ec | bellard | m_freem(m); |
156 | f0cbd3ec | bellard | /*
|
157 | f0cbd3ec | bellard | * Try to present any queued data
|
158 | f0cbd3ec | bellard | * at the left window edge to the user.
|
159 | f0cbd3ec | bellard | * This is needed after the 3-WHS
|
160 | f0cbd3ec | bellard | * completes.
|
161 | f0cbd3ec | bellard | */
|
162 | f0cbd3ec | bellard | goto present; /* ??? */ |
163 | f0cbd3ec | bellard | } |
164 | f0cbd3ec | bellard | m_adj(m, i); |
165 | f0cbd3ec | bellard | ti->ti_len -= i; |
166 | f0cbd3ec | bellard | ti->ti_seq += i; |
167 | f0cbd3ec | bellard | } |
168 | f0cbd3ec | bellard | q = (struct tcpiphdr *)(q->ti_next);
|
169 | f0cbd3ec | bellard | } |
170 | f0cbd3ec | bellard | tcpstat.tcps_rcvoopack++; |
171 | f0cbd3ec | bellard | tcpstat.tcps_rcvoobyte += ti->ti_len; |
172 | f0cbd3ec | bellard | REASS_MBUF(ti) = (mbufp_32) m; /* XXX */
|
173 | f0cbd3ec | bellard | |
174 | f0cbd3ec | bellard | /*
|
175 | f0cbd3ec | bellard | * While we overlap succeeding segments trim them or,
|
176 | f0cbd3ec | bellard | * if they are completely covered, dequeue them.
|
177 | f0cbd3ec | bellard | */
|
178 | f0cbd3ec | bellard | while (q != (struct tcpiphdr *)tp) { |
179 | f0cbd3ec | bellard | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; |
180 | f0cbd3ec | bellard | if (i <= 0) |
181 | f0cbd3ec | bellard | break;
|
182 | f0cbd3ec | bellard | if (i < q->ti_len) {
|
183 | f0cbd3ec | bellard | q->ti_seq += i; |
184 | f0cbd3ec | bellard | q->ti_len -= i; |
185 | f0cbd3ec | bellard | m_adj((struct mbuf *) REASS_MBUF(q), i);
|
186 | f0cbd3ec | bellard | break;
|
187 | f0cbd3ec | bellard | } |
188 | f0cbd3ec | bellard | q = (struct tcpiphdr *)q->ti_next;
|
189 | f0cbd3ec | bellard | m = (struct mbuf *) REASS_MBUF((struct tcpiphdr *)q->ti_prev); |
190 | f0cbd3ec | bellard | remque_32((void *)(q->ti_prev));
|
191 | f0cbd3ec | bellard | m_freem(m); |
192 | f0cbd3ec | bellard | } |
193 | f0cbd3ec | bellard | |
194 | f0cbd3ec | bellard | /*
|
195 | f0cbd3ec | bellard | * Stick new segment in its place.
|
196 | f0cbd3ec | bellard | */
|
197 | f0cbd3ec | bellard | insque_32(ti, (void *)(q->ti_prev));
|
198 | f0cbd3ec | bellard | |
199 | f0cbd3ec | bellard | present:
|
200 | f0cbd3ec | bellard | /*
|
201 | f0cbd3ec | bellard | * Present data to user, advancing rcv_nxt through
|
202 | f0cbd3ec | bellard | * completed sequence space.
|
203 | f0cbd3ec | bellard | */
|
204 | f0cbd3ec | bellard | if (!TCPS_HAVEESTABLISHED(tp->t_state))
|
205 | f0cbd3ec | bellard | return (0); |
206 | f0cbd3ec | bellard | ti = (struct tcpiphdr *) tp->seg_next;
|
207 | f0cbd3ec | bellard | if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) |
208 | f0cbd3ec | bellard | return (0); |
209 | f0cbd3ec | bellard | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
|
210 | f0cbd3ec | bellard | return (0); |
211 | f0cbd3ec | bellard | do {
|
212 | f0cbd3ec | bellard | tp->rcv_nxt += ti->ti_len; |
213 | f0cbd3ec | bellard | flags = ti->ti_flags & TH_FIN; |
214 | f0cbd3ec | bellard | remque_32(ti); |
215 | f0cbd3ec | bellard | m = (struct mbuf *) REASS_MBUF(ti); /* XXX */ |
216 | f0cbd3ec | bellard | ti = (struct tcpiphdr *)ti->ti_next;
|
217 | f0cbd3ec | bellard | /* if (so->so_state & SS_FCANTRCVMORE) */
|
218 | f0cbd3ec | bellard | if (so->so_state & SS_FCANTSENDMORE)
|
219 | f0cbd3ec | bellard | m_freem(m); |
220 | f0cbd3ec | bellard | else {
|
221 | f0cbd3ec | bellard | if (so->so_emu) {
|
222 | f0cbd3ec | bellard | if (tcp_emu(so,m)) sbappend(so, m);
|
223 | f0cbd3ec | bellard | } else
|
224 | f0cbd3ec | bellard | sbappend(so, m); |
225 | f0cbd3ec | bellard | } |
226 | f0cbd3ec | bellard | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); |
227 | f0cbd3ec | bellard | /* sorwakeup(so); */
|
228 | f0cbd3ec | bellard | return (flags);
|
229 | f0cbd3ec | bellard | } |
230 | f0cbd3ec | bellard | |
231 | f0cbd3ec | bellard | /*
|
232 | f0cbd3ec | bellard | * TCP input routine, follows pages 65-76 of the
|
233 | f0cbd3ec | bellard | * protocol specification dated September, 1981 very closely.
|
234 | f0cbd3ec | bellard | */
|
235 | f0cbd3ec | bellard | void
|
236 | f0cbd3ec | bellard | tcp_input(m, iphlen, inso) |
237 | f0cbd3ec | bellard | register struct mbuf *m; |
238 | f0cbd3ec | bellard | int iphlen;
|
239 | f0cbd3ec | bellard | struct socket *inso;
|
240 | f0cbd3ec | bellard | { |
241 | f0cbd3ec | bellard | struct ip save_ip, *ip;
|
242 | f0cbd3ec | bellard | register struct tcpiphdr *ti; |
243 | f0cbd3ec | bellard | caddr_t optp = NULL;
|
244 | f0cbd3ec | bellard | int optlen = 0; |
245 | f0cbd3ec | bellard | int len, tlen, off;
|
246 | f0cbd3ec | bellard | register struct tcpcb *tp = 0; |
247 | f0cbd3ec | bellard | register int tiflags; |
248 | f0cbd3ec | bellard | struct socket *so = 0; |
249 | f0cbd3ec | bellard | int todrop, acked, ourfinisacked, needoutput = 0; |
250 | f0cbd3ec | bellard | /* int dropsocket = 0; */
|
251 | f0cbd3ec | bellard | int iss = 0; |
252 | f0cbd3ec | bellard | u_long tiwin; |
253 | f0cbd3ec | bellard | int ret;
|
254 | f0cbd3ec | bellard | /* int ts_present = 0; */
|
255 | f0cbd3ec | bellard | |
256 | f0cbd3ec | bellard | DEBUG_CALL("tcp_input");
|
257 | f0cbd3ec | bellard | DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n",
|
258 | f0cbd3ec | bellard | (long )m, iphlen, (long )inso )); |
259 | f0cbd3ec | bellard | |
260 | f0cbd3ec | bellard | /*
|
261 | f0cbd3ec | bellard | * If called with m == 0, then we're continuing the connect
|
262 | f0cbd3ec | bellard | */
|
263 | f0cbd3ec | bellard | if (m == NULL) { |
264 | f0cbd3ec | bellard | so = inso; |
265 | f0cbd3ec | bellard | |
266 | f0cbd3ec | bellard | /* Re-set a few variables */
|
267 | f0cbd3ec | bellard | tp = sototcpcb(so); |
268 | f0cbd3ec | bellard | m = so->so_m; |
269 | f0cbd3ec | bellard | so->so_m = 0;
|
270 | f0cbd3ec | bellard | ti = so->so_ti; |
271 | f0cbd3ec | bellard | tiwin = ti->ti_win; |
272 | f0cbd3ec | bellard | tiflags = ti->ti_flags; |
273 | f0cbd3ec | bellard | |
274 | f0cbd3ec | bellard | goto cont_conn;
|
275 | f0cbd3ec | bellard | } |
276 | f0cbd3ec | bellard | |
277 | f0cbd3ec | bellard | |
278 | f0cbd3ec | bellard | tcpstat.tcps_rcvtotal++; |
279 | f0cbd3ec | bellard | /*
|
280 | f0cbd3ec | bellard | * Get IP and TCP header together in first mbuf.
|
281 | f0cbd3ec | bellard | * Note: IP leaves IP header in first mbuf.
|
282 | f0cbd3ec | bellard | */
|
283 | f0cbd3ec | bellard | ti = mtod(m, struct tcpiphdr *);
|
284 | f0cbd3ec | bellard | if (iphlen > sizeof(struct ip )) { |
285 | f0cbd3ec | bellard | ip_stripoptions(m, (struct mbuf *)0); |
286 | f0cbd3ec | bellard | iphlen=sizeof(struct ip ); |
287 | f0cbd3ec | bellard | } |
288 | f0cbd3ec | bellard | /* XXX Check if too short */
|
289 | f0cbd3ec | bellard | |
290 | f0cbd3ec | bellard | |
291 | f0cbd3ec | bellard | /*
|
292 | f0cbd3ec | bellard | * Save a copy of the IP header in case we want restore it
|
293 | f0cbd3ec | bellard | * for sending an ICMP error message in response.
|
294 | f0cbd3ec | bellard | */
|
295 | f0cbd3ec | bellard | ip=mtod(m, struct ip *);
|
296 | f0cbd3ec | bellard | save_ip = *ip; |
297 | f0cbd3ec | bellard | save_ip.ip_len+= iphlen; |
298 | f0cbd3ec | bellard | |
299 | f0cbd3ec | bellard | /*
|
300 | f0cbd3ec | bellard | * Checksum extended TCP header and data.
|
301 | f0cbd3ec | bellard | */
|
302 | f0cbd3ec | bellard | tlen = ((struct ip *)ti)->ip_len;
|
303 | f0cbd3ec | bellard | ti->ti_next = ti->ti_prev = 0;
|
304 | f0cbd3ec | bellard | ti->ti_x1 = 0;
|
305 | f0cbd3ec | bellard | ti->ti_len = htons((u_int16_t)tlen); |
306 | f0cbd3ec | bellard | len = sizeof(struct ip ) + tlen; |
307 | f0cbd3ec | bellard | /* keep checksum for ICMP reply
|
308 | f0cbd3ec | bellard | * ti->ti_sum = cksum(m, len);
|
309 | f0cbd3ec | bellard | * if (ti->ti_sum) { */
|
310 | f0cbd3ec | bellard | if(cksum(m, len)) {
|
311 | f0cbd3ec | bellard | tcpstat.tcps_rcvbadsum++; |
312 | f0cbd3ec | bellard | goto drop;
|
313 | f0cbd3ec | bellard | } |
314 | f0cbd3ec | bellard | |
315 | f0cbd3ec | bellard | /*
|
316 | f0cbd3ec | bellard | * Check that TCP offset makes sense,
|
317 | f0cbd3ec | bellard | * pull out TCP options and adjust length. XXX
|
318 | f0cbd3ec | bellard | */
|
319 | f0cbd3ec | bellard | off = ti->ti_off << 2;
|
320 | f0cbd3ec | bellard | if (off < sizeof (struct tcphdr) || off > tlen) { |
321 | f0cbd3ec | bellard | tcpstat.tcps_rcvbadoff++; |
322 | f0cbd3ec | bellard | goto drop;
|
323 | f0cbd3ec | bellard | } |
324 | f0cbd3ec | bellard | tlen -= off; |
325 | f0cbd3ec | bellard | ti->ti_len = tlen; |
326 | f0cbd3ec | bellard | if (off > sizeof (struct tcphdr)) { |
327 | f0cbd3ec | bellard | optlen = off - sizeof (struct tcphdr); |
328 | f0cbd3ec | bellard | optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); |
329 | f0cbd3ec | bellard | |
330 | f0cbd3ec | bellard | /*
|
331 | f0cbd3ec | bellard | * Do quick retrieval of timestamp options ("options
|
332 | f0cbd3ec | bellard | * prediction?"). If timestamp is the only option and it's
|
333 | f0cbd3ec | bellard | * formatted as recommended in RFC 1323 appendix A, we
|
334 | f0cbd3ec | bellard | * quickly get the values now and not bother calling
|
335 | f0cbd3ec | bellard | * tcp_dooptions(), etc.
|
336 | f0cbd3ec | bellard | */
|
337 | f0cbd3ec | bellard | /* if ((optlen == TCPOLEN_TSTAMP_APPA ||
|
338 | f0cbd3ec | bellard | * (optlen > TCPOLEN_TSTAMP_APPA &&
|
339 | f0cbd3ec | bellard | * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
|
340 | f0cbd3ec | bellard | * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
|
341 | f0cbd3ec | bellard | * (ti->ti_flags & TH_SYN) == 0) {
|
342 | f0cbd3ec | bellard | * ts_present = 1;
|
343 | f0cbd3ec | bellard | * ts_val = ntohl(*(u_int32_t *)(optp + 4));
|
344 | f0cbd3ec | bellard | * ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
|
345 | f0cbd3ec | bellard | * optp = NULL; / * we've parsed the options * /
|
346 | f0cbd3ec | bellard | * }
|
347 | f0cbd3ec | bellard | */
|
348 | f0cbd3ec | bellard | } |
349 | f0cbd3ec | bellard | tiflags = ti->ti_flags; |
350 | f0cbd3ec | bellard | |
351 | f0cbd3ec | bellard | /*
|
352 | f0cbd3ec | bellard | * Convert TCP protocol specific fields to host format.
|
353 | f0cbd3ec | bellard | */
|
354 | f0cbd3ec | bellard | NTOHL(ti->ti_seq); |
355 | f0cbd3ec | bellard | NTOHL(ti->ti_ack); |
356 | f0cbd3ec | bellard | NTOHS(ti->ti_win); |
357 | f0cbd3ec | bellard | NTOHS(ti->ti_urp); |
358 | f0cbd3ec | bellard | |
359 | f0cbd3ec | bellard | /*
|
360 | f0cbd3ec | bellard | * Drop TCP, IP headers and TCP options.
|
361 | f0cbd3ec | bellard | */
|
362 | f0cbd3ec | bellard | m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
363 | f0cbd3ec | bellard | m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
364 | f0cbd3ec | bellard | |
365 | f0cbd3ec | bellard | /*
|
366 | f0cbd3ec | bellard | * Locate pcb for segment.
|
367 | f0cbd3ec | bellard | */
|
368 | f0cbd3ec | bellard | findso:
|
369 | f0cbd3ec | bellard | so = tcp_last_so; |
370 | f0cbd3ec | bellard | if (so->so_fport != ti->ti_dport ||
|
371 | f0cbd3ec | bellard | so->so_lport != ti->ti_sport || |
372 | f0cbd3ec | bellard | so->so_laddr.s_addr != ti->ti_src.s_addr || |
373 | f0cbd3ec | bellard | so->so_faddr.s_addr != ti->ti_dst.s_addr) { |
374 | f0cbd3ec | bellard | so = solookup(&tcb, ti->ti_src, ti->ti_sport, |
375 | f0cbd3ec | bellard | ti->ti_dst, ti->ti_dport); |
376 | f0cbd3ec | bellard | if (so)
|
377 | f0cbd3ec | bellard | tcp_last_so = so; |
378 | f0cbd3ec | bellard | ++tcpstat.tcps_socachemiss; |
379 | f0cbd3ec | bellard | } |
380 | f0cbd3ec | bellard | |
381 | f0cbd3ec | bellard | /*
|
382 | f0cbd3ec | bellard | * If the state is CLOSED (i.e., TCB does not exist) then
|
383 | f0cbd3ec | bellard | * all data in the incoming segment is discarded.
|
384 | f0cbd3ec | bellard | * If the TCB exists but is in CLOSED state, it is embryonic,
|
385 | f0cbd3ec | bellard | * but should either do a listen or a connect soon.
|
386 | f0cbd3ec | bellard | *
|
387 | f0cbd3ec | bellard | * state == CLOSED means we've done socreate() but haven't
|
388 | f0cbd3ec | bellard | * attached it to a protocol yet...
|
389 | f0cbd3ec | bellard | *
|
390 | f0cbd3ec | bellard | * XXX If a TCB does not exist, and the TH_SYN flag is
|
391 | f0cbd3ec | bellard | * the only flag set, then create a session, mark it
|
392 | f0cbd3ec | bellard | * as if it was LISTENING, and continue...
|
393 | f0cbd3ec | bellard | */
|
394 | f0cbd3ec | bellard | if (so == 0) { |
395 | f0cbd3ec | bellard | if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
|
396 | f0cbd3ec | bellard | goto dropwithreset;
|
397 | f0cbd3ec | bellard | |
398 | f0cbd3ec | bellard | if ((so = socreate()) == NULL) |
399 | f0cbd3ec | bellard | goto dropwithreset;
|
400 | f0cbd3ec | bellard | if (tcp_attach(so) < 0) { |
401 | f0cbd3ec | bellard | free(so); /* Not sofree (if it failed, it's not insqued) */
|
402 | f0cbd3ec | bellard | goto dropwithreset;
|
403 | f0cbd3ec | bellard | } |
404 | f0cbd3ec | bellard | |
405 | f0cbd3ec | bellard | sbreserve(&so->so_snd, tcp_sndspace); |
406 | f0cbd3ec | bellard | sbreserve(&so->so_rcv, tcp_rcvspace); |
407 | f0cbd3ec | bellard | |
408 | f0cbd3ec | bellard | /* tcp_last_so = so; */ /* XXX ? */ |
409 | f0cbd3ec | bellard | /* tp = sototcpcb(so); */
|
410 | f0cbd3ec | bellard | |
411 | f0cbd3ec | bellard | so->so_laddr = ti->ti_src; |
412 | f0cbd3ec | bellard | so->so_lport = ti->ti_sport; |
413 | f0cbd3ec | bellard | so->so_faddr = ti->ti_dst; |
414 | f0cbd3ec | bellard | so->so_fport = ti->ti_dport; |
415 | f0cbd3ec | bellard | |
416 | f0cbd3ec | bellard | if ((so->so_iptos = tcp_tos(so)) == 0) |
417 | f0cbd3ec | bellard | so->so_iptos = ((struct ip *)ti)->ip_tos;
|
418 | f0cbd3ec | bellard | |
419 | f0cbd3ec | bellard | tp = sototcpcb(so); |
420 | f0cbd3ec | bellard | tp->t_state = TCPS_LISTEN; |
421 | f0cbd3ec | bellard | } |
422 | f0cbd3ec | bellard | |
423 | f0cbd3ec | bellard | /*
|
424 | f0cbd3ec | bellard | * If this is a still-connecting socket, this probably
|
425 | f0cbd3ec | bellard | * a retransmit of the SYN. Whether it's a retransmit SYN
|
426 | f0cbd3ec | bellard | * or something else, we nuke it.
|
427 | f0cbd3ec | bellard | */
|
428 | f0cbd3ec | bellard | if (so->so_state & SS_ISFCONNECTING)
|
429 | f0cbd3ec | bellard | goto drop;
|
430 | f0cbd3ec | bellard | |
431 | f0cbd3ec | bellard | tp = sototcpcb(so); |
432 | f0cbd3ec | bellard | |
433 | f0cbd3ec | bellard | /* XXX Should never fail */
|
434 | f0cbd3ec | bellard | if (tp == 0) |
435 | f0cbd3ec | bellard | goto dropwithreset;
|
436 | f0cbd3ec | bellard | if (tp->t_state == TCPS_CLOSED)
|
437 | f0cbd3ec | bellard | goto drop;
|
438 | f0cbd3ec | bellard | |
439 | f0cbd3ec | bellard | /* Unscale the window into a 32-bit value. */
|
440 | f0cbd3ec | bellard | /* if ((tiflags & TH_SYN) == 0)
|
441 | f0cbd3ec | bellard | * tiwin = ti->ti_win << tp->snd_scale;
|
442 | f0cbd3ec | bellard | * else
|
443 | f0cbd3ec | bellard | */
|
444 | f0cbd3ec | bellard | tiwin = ti->ti_win; |
445 | f0cbd3ec | bellard | |
446 | f0cbd3ec | bellard | /*
|
447 | f0cbd3ec | bellard | * Segment received on connection.
|
448 | f0cbd3ec | bellard | * Reset idle time and keep-alive timer.
|
449 | f0cbd3ec | bellard | */
|
450 | f0cbd3ec | bellard | tp->t_idle = 0;
|
451 | f0cbd3ec | bellard | if (so_options)
|
452 | f0cbd3ec | bellard | tp->t_timer[TCPT_KEEP] = tcp_keepintvl; |
453 | f0cbd3ec | bellard | else
|
454 | f0cbd3ec | bellard | tp->t_timer[TCPT_KEEP] = tcp_keepidle; |
455 | f0cbd3ec | bellard | |
456 | f0cbd3ec | bellard | /*
|
457 | f0cbd3ec | bellard | * Process options if not in LISTEN state,
|
458 | f0cbd3ec | bellard | * else do it below (after getting remote address).
|
459 | f0cbd3ec | bellard | */
|
460 | f0cbd3ec | bellard | if (optp && tp->t_state != TCPS_LISTEN)
|
461 | f0cbd3ec | bellard | tcp_dooptions(tp, (u_char *)optp, optlen, ti); |
462 | f0cbd3ec | bellard | /* , */
|
463 | f0cbd3ec | bellard | /* &ts_present, &ts_val, &ts_ecr); */
|
464 | f0cbd3ec | bellard | |
465 | f0cbd3ec | bellard | /*
|
466 | f0cbd3ec | bellard | * Header prediction: check for the two common cases
|
467 | f0cbd3ec | bellard | * of a uni-directional data xfer. If the packet has
|
468 | f0cbd3ec | bellard | * no control flags, is in-sequence, the window didn't
|
469 | f0cbd3ec | bellard | * change and we're not retransmitting, it's a
|
470 | f0cbd3ec | bellard | * candidate. If the length is zero and the ack moved
|
471 | f0cbd3ec | bellard | * forward, we're the sender side of the xfer. Just
|
472 | f0cbd3ec | bellard | * free the data acked & wake any higher level process
|
473 | f0cbd3ec | bellard | * that was blocked waiting for space. If the length
|
474 | f0cbd3ec | bellard | * is non-zero and the ack didn't move, we're the
|
475 | f0cbd3ec | bellard | * receiver side. If we're getting packets in-order
|
476 | f0cbd3ec | bellard | * (the reassembly queue is empty), add the data to
|
477 | f0cbd3ec | bellard | * the socket buffer and note that we need a delayed ack.
|
478 | f0cbd3ec | bellard | *
|
479 | f0cbd3ec | bellard | * XXX Some of these tests are not needed
|
480 | f0cbd3ec | bellard | * eg: the tiwin == tp->snd_wnd prevents many more
|
481 | f0cbd3ec | bellard | * predictions.. with no *real* advantage..
|
482 | f0cbd3ec | bellard | */
|
483 | f0cbd3ec | bellard | if (tp->t_state == TCPS_ESTABLISHED &&
|
484 | f0cbd3ec | bellard | (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && |
485 | f0cbd3ec | bellard | /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
|
486 | f0cbd3ec | bellard | ti->ti_seq == tp->rcv_nxt && |
487 | f0cbd3ec | bellard | tiwin && tiwin == tp->snd_wnd && |
488 | f0cbd3ec | bellard | tp->snd_nxt == tp->snd_max) { |
489 | f0cbd3ec | bellard | /*
|
490 | f0cbd3ec | bellard | * If last ACK falls within this segment's sequence numbers,
|
491 | f0cbd3ec | bellard | * record the timestamp.
|
492 | f0cbd3ec | bellard | */
|
493 | f0cbd3ec | bellard | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
|
494 | f0cbd3ec | bellard | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
|
495 | f0cbd3ec | bellard | * tp->ts_recent_age = tcp_now;
|
496 | f0cbd3ec | bellard | * tp->ts_recent = ts_val;
|
497 | f0cbd3ec | bellard | * }
|
498 | f0cbd3ec | bellard | */
|
499 | f0cbd3ec | bellard | if (ti->ti_len == 0) { |
500 | f0cbd3ec | bellard | if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
|
501 | f0cbd3ec | bellard | SEQ_LEQ(ti->ti_ack, tp->snd_max) && |
502 | f0cbd3ec | bellard | tp->snd_cwnd >= tp->snd_wnd) { |
503 | f0cbd3ec | bellard | /*
|
504 | f0cbd3ec | bellard | * this is a pure ack for outstanding data.
|
505 | f0cbd3ec | bellard | */
|
506 | f0cbd3ec | bellard | ++tcpstat.tcps_predack; |
507 | f0cbd3ec | bellard | /* if (ts_present)
|
508 | f0cbd3ec | bellard | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
|
509 | f0cbd3ec | bellard | * else
|
510 | f0cbd3ec | bellard | */ if (tp->t_rtt && |
511 | f0cbd3ec | bellard | SEQ_GT(ti->ti_ack, tp->t_rtseq)) |
512 | f0cbd3ec | bellard | tcp_xmit_timer(tp, tp->t_rtt); |
513 | f0cbd3ec | bellard | acked = ti->ti_ack - tp->snd_una; |
514 | f0cbd3ec | bellard | tcpstat.tcps_rcvackpack++; |
515 | f0cbd3ec | bellard | tcpstat.tcps_rcvackbyte += acked; |
516 | f0cbd3ec | bellard | sbdrop(&so->so_snd, acked); |
517 | f0cbd3ec | bellard | tp->snd_una = ti->ti_ack; |
518 | f0cbd3ec | bellard | m_freem(m); |
519 | f0cbd3ec | bellard | |
520 | f0cbd3ec | bellard | /*
|
521 | f0cbd3ec | bellard | * If all outstanding data are acked, stop
|
522 | f0cbd3ec | bellard | * retransmit timer, otherwise restart timer
|
523 | f0cbd3ec | bellard | * using current (possibly backed-off) value.
|
524 | f0cbd3ec | bellard | * If process is waiting for space,
|
525 | f0cbd3ec | bellard | * wakeup/selwakeup/signal. If data
|
526 | f0cbd3ec | bellard | * are ready to send, let tcp_output
|
527 | f0cbd3ec | bellard | * decide between more output or persist.
|
528 | f0cbd3ec | bellard | */
|
529 | f0cbd3ec | bellard | if (tp->snd_una == tp->snd_max)
|
530 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = 0;
|
531 | f0cbd3ec | bellard | else if (tp->t_timer[TCPT_PERSIST] == 0) |
532 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; |
533 | f0cbd3ec | bellard | |
534 | f0cbd3ec | bellard | /*
|
535 | f0cbd3ec | bellard | * There's room in so_snd, sowwakup will read()
|
536 | f0cbd3ec | bellard | * from the socket if we can
|
537 | f0cbd3ec | bellard | */
|
538 | f0cbd3ec | bellard | /* if (so->so_snd.sb_flags & SB_NOTIFY)
|
539 | f0cbd3ec | bellard | * sowwakeup(so);
|
540 | f0cbd3ec | bellard | */
|
541 | f0cbd3ec | bellard | /*
|
542 | f0cbd3ec | bellard | * This is called because sowwakeup might have
|
543 | f0cbd3ec | bellard | * put data into so_snd. Since we don't so sowwakeup,
|
544 | f0cbd3ec | bellard | * we don't need this.. XXX???
|
545 | f0cbd3ec | bellard | */
|
546 | f0cbd3ec | bellard | if (so->so_snd.sb_cc)
|
547 | f0cbd3ec | bellard | (void) tcp_output(tp);
|
548 | f0cbd3ec | bellard | |
549 | f0cbd3ec | bellard | return;
|
550 | f0cbd3ec | bellard | } |
551 | f0cbd3ec | bellard | } else if (ti->ti_ack == tp->snd_una && |
552 | f0cbd3ec | bellard | tp->seg_next == (tcpiphdrp_32)tp && |
553 | f0cbd3ec | bellard | ti->ti_len <= sbspace(&so->so_rcv)) { |
554 | f0cbd3ec | bellard | /*
|
555 | f0cbd3ec | bellard | * this is a pure, in-sequence data packet
|
556 | f0cbd3ec | bellard | * with nothing on the reassembly queue and
|
557 | f0cbd3ec | bellard | * we have enough buffer space to take it.
|
558 | f0cbd3ec | bellard | */
|
559 | f0cbd3ec | bellard | ++tcpstat.tcps_preddat; |
560 | f0cbd3ec | bellard | tp->rcv_nxt += ti->ti_len; |
561 | f0cbd3ec | bellard | tcpstat.tcps_rcvpack++; |
562 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyte += ti->ti_len; |
563 | f0cbd3ec | bellard | /*
|
564 | f0cbd3ec | bellard | * Add data to socket buffer.
|
565 | f0cbd3ec | bellard | */
|
566 | f0cbd3ec | bellard | if (so->so_emu) {
|
567 | f0cbd3ec | bellard | if (tcp_emu(so,m)) sbappend(so, m);
|
568 | f0cbd3ec | bellard | } else
|
569 | f0cbd3ec | bellard | sbappend(so, m); |
570 | f0cbd3ec | bellard | |
571 | f0cbd3ec | bellard | /*
|
572 | f0cbd3ec | bellard | * XXX This is called when data arrives. Later, check
|
573 | f0cbd3ec | bellard | * if we can actually write() to the socket
|
574 | f0cbd3ec | bellard | * XXX Need to check? It's be NON_BLOCKING
|
575 | f0cbd3ec | bellard | */
|
576 | f0cbd3ec | bellard | /* sorwakeup(so); */
|
577 | f0cbd3ec | bellard | |
578 | f0cbd3ec | bellard | /*
|
579 | f0cbd3ec | bellard | * If this is a short packet, then ACK now - with Nagel
|
580 | f0cbd3ec | bellard | * congestion avoidance sender won't send more until
|
581 | f0cbd3ec | bellard | * he gets an ACK.
|
582 | f0cbd3ec | bellard | *
|
583 | 4f552e3b | bellard | * It is better to not delay acks at all to maximize
|
584 | 4f552e3b | bellard | * TCP throughput. See RFC 2581.
|
585 | f0cbd3ec | bellard | */
|
586 | 4f552e3b | bellard | tp->t_flags |= TF_ACKNOW; |
587 | 4f552e3b | bellard | tcp_output(tp); |
588 | f0cbd3ec | bellard | return;
|
589 | f0cbd3ec | bellard | } |
590 | f0cbd3ec | bellard | } /* header prediction */
|
591 | f0cbd3ec | bellard | /*
|
592 | f0cbd3ec | bellard | * Calculate amount of space in receive window,
|
593 | f0cbd3ec | bellard | * and then do TCP input processing.
|
594 | f0cbd3ec | bellard | * Receive window is amount of space in rcv queue,
|
595 | f0cbd3ec | bellard | * but not less than advertised window.
|
596 | f0cbd3ec | bellard | */
|
597 | f0cbd3ec | bellard | { int win;
|
598 | f0cbd3ec | bellard | win = sbspace(&so->so_rcv); |
599 | f0cbd3ec | bellard | if (win < 0) |
600 | f0cbd3ec | bellard | win = 0;
|
601 | f0cbd3ec | bellard | tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
|
602 | f0cbd3ec | bellard | } |
603 | f0cbd3ec | bellard | |
604 | f0cbd3ec | bellard | switch (tp->t_state) {
|
605 | f0cbd3ec | bellard | |
606 | f0cbd3ec | bellard | /*
|
607 | f0cbd3ec | bellard | * If the state is LISTEN then ignore segment if it contains an RST.
|
608 | f0cbd3ec | bellard | * If the segment contains an ACK then it is bad and send a RST.
|
609 | f0cbd3ec | bellard | * If it does not contain a SYN then it is not interesting; drop it.
|
610 | f0cbd3ec | bellard | * Don't bother responding if the destination was a broadcast.
|
611 | f0cbd3ec | bellard | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
|
612 | f0cbd3ec | bellard | * tp->iss, and send a segment:
|
613 | f0cbd3ec | bellard | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
|
614 | f0cbd3ec | bellard | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
|
615 | f0cbd3ec | bellard | * Fill in remote peer address fields if not previously specified.
|
616 | f0cbd3ec | bellard | * Enter SYN_RECEIVED state, and process any other fields of this
|
617 | f0cbd3ec | bellard | * segment in this state.
|
618 | f0cbd3ec | bellard | */
|
619 | f0cbd3ec | bellard | case TCPS_LISTEN: {
|
620 | f0cbd3ec | bellard | |
621 | f0cbd3ec | bellard | if (tiflags & TH_RST)
|
622 | f0cbd3ec | bellard | goto drop;
|
623 | f0cbd3ec | bellard | if (tiflags & TH_ACK)
|
624 | f0cbd3ec | bellard | goto dropwithreset;
|
625 | f0cbd3ec | bellard | if ((tiflags & TH_SYN) == 0) |
626 | f0cbd3ec | bellard | goto drop;
|
627 | f0cbd3ec | bellard | |
628 | f0cbd3ec | bellard | /*
|
629 | f0cbd3ec | bellard | * This has way too many gotos...
|
630 | f0cbd3ec | bellard | * But a bit of spaghetti code never hurt anybody :)
|
631 | f0cbd3ec | bellard | */
|
632 | f0cbd3ec | bellard | |
633 | f0cbd3ec | bellard | /*
|
634 | f0cbd3ec | bellard | * If this is destined for the control address, then flag to
|
635 | f0cbd3ec | bellard | * tcp_ctl once connected, otherwise connect
|
636 | f0cbd3ec | bellard | */
|
637 | f0cbd3ec | bellard | if ((so->so_faddr.s_addr&htonl(0xffffff00)) == special_addr.s_addr) { |
638 | f0cbd3ec | bellard | int lastbyte=ntohl(so->so_faddr.s_addr) & 0xff; |
639 | f0cbd3ec | bellard | if (lastbyte!=CTL_ALIAS && lastbyte!=CTL_DNS) {
|
640 | f0cbd3ec | bellard | #if 0
|
641 | f0cbd3ec | bellard | if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) {
|
642 | f0cbd3ec | bellard | /* Command or exec adress */
|
643 | f0cbd3ec | bellard | so->so_state |= SS_CTL;
|
644 | a3d4af03 | bellard | } else
|
645 | a3d4af03 | bellard | #endif
|
646 | a3d4af03 | bellard | { |
647 | f0cbd3ec | bellard | /* May be an add exec */
|
648 | f0cbd3ec | bellard | struct ex_list *ex_ptr;
|
649 | f0cbd3ec | bellard | for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
|
650 | f0cbd3ec | bellard | if(ex_ptr->ex_fport == so->so_fport &&
|
651 | f0cbd3ec | bellard | lastbyte == ex_ptr->ex_addr) { |
652 | f0cbd3ec | bellard | so->so_state |= SS_CTL; |
653 | f0cbd3ec | bellard | break;
|
654 | f0cbd3ec | bellard | } |
655 | f0cbd3ec | bellard | } |
656 | f0cbd3ec | bellard | } |
657 | f0cbd3ec | bellard | if(so->so_state & SS_CTL) goto cont_input; |
658 | f0cbd3ec | bellard | } |
659 | f0cbd3ec | bellard | /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
|
660 | f0cbd3ec | bellard | } |
661 | f0cbd3ec | bellard | |
662 | f0cbd3ec | bellard | if (so->so_emu & EMU_NOCONNECT) {
|
663 | f0cbd3ec | bellard | so->so_emu &= ~EMU_NOCONNECT; |
664 | f0cbd3ec | bellard | goto cont_input;
|
665 | f0cbd3ec | bellard | } |
666 | f0cbd3ec | bellard | |
667 | 02d2c54c | bellard | if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) { |
668 | f0cbd3ec | bellard | u_char code=ICMP_UNREACH_NET; |
669 | f0cbd3ec | bellard | DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
|
670 | f0cbd3ec | bellard | errno,strerror(errno))); |
671 | f0cbd3ec | bellard | if(errno == ECONNREFUSED) {
|
672 | f0cbd3ec | bellard | /* ACK the SYN, send RST to refuse the connection */
|
673 | f0cbd3ec | bellard | tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0, |
674 | f0cbd3ec | bellard | TH_RST|TH_ACK); |
675 | f0cbd3ec | bellard | } else {
|
676 | f0cbd3ec | bellard | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
677 | f0cbd3ec | bellard | HTONL(ti->ti_seq); /* restore tcp header */
|
678 | f0cbd3ec | bellard | HTONL(ti->ti_ack); |
679 | f0cbd3ec | bellard | HTONS(ti->ti_win); |
680 | f0cbd3ec | bellard | HTONS(ti->ti_urp); |
681 | f0cbd3ec | bellard | m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
682 | f0cbd3ec | bellard | m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
683 | f0cbd3ec | bellard | *ip=save_ip; |
684 | f0cbd3ec | bellard | icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno));
|
685 | f0cbd3ec | bellard | } |
686 | f0cbd3ec | bellard | tp = tcp_close(tp); |
687 | f0cbd3ec | bellard | m_free(m); |
688 | f0cbd3ec | bellard | } else {
|
689 | f0cbd3ec | bellard | /*
|
690 | f0cbd3ec | bellard | * Haven't connected yet, save the current mbuf
|
691 | f0cbd3ec | bellard | * and ti, and return
|
692 | f0cbd3ec | bellard | * XXX Some OS's don't tell us whether the connect()
|
693 | f0cbd3ec | bellard | * succeeded or not. So we must time it out.
|
694 | f0cbd3ec | bellard | */
|
695 | f0cbd3ec | bellard | so->so_m = m; |
696 | f0cbd3ec | bellard | so->so_ti = ti; |
697 | f0cbd3ec | bellard | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; |
698 | f0cbd3ec | bellard | tp->t_state = TCPS_SYN_RECEIVED; |
699 | f0cbd3ec | bellard | } |
700 | f0cbd3ec | bellard | return;
|
701 | f0cbd3ec | bellard | |
702 | f0cbd3ec | bellard | cont_conn:
|
703 | f0cbd3ec | bellard | /* m==NULL
|
704 | f0cbd3ec | bellard | * Check if the connect succeeded
|
705 | f0cbd3ec | bellard | */
|
706 | f0cbd3ec | bellard | if (so->so_state & SS_NOFDREF) {
|
707 | f0cbd3ec | bellard | tp = tcp_close(tp); |
708 | f0cbd3ec | bellard | goto dropwithreset;
|
709 | f0cbd3ec | bellard | } |
710 | f0cbd3ec | bellard | cont_input:
|
711 | f0cbd3ec | bellard | tcp_template(tp); |
712 | f0cbd3ec | bellard | |
713 | f0cbd3ec | bellard | if (optp)
|
714 | f0cbd3ec | bellard | tcp_dooptions(tp, (u_char *)optp, optlen, ti); |
715 | f0cbd3ec | bellard | /* , */
|
716 | f0cbd3ec | bellard | /* &ts_present, &ts_val, &ts_ecr); */
|
717 | f0cbd3ec | bellard | |
718 | f0cbd3ec | bellard | if (iss)
|
719 | f0cbd3ec | bellard | tp->iss = iss; |
720 | f0cbd3ec | bellard | else
|
721 | f0cbd3ec | bellard | tp->iss = tcp_iss; |
722 | f0cbd3ec | bellard | tcp_iss += TCP_ISSINCR/2;
|
723 | f0cbd3ec | bellard | tp->irs = ti->ti_seq; |
724 | f0cbd3ec | bellard | tcp_sendseqinit(tp); |
725 | f0cbd3ec | bellard | tcp_rcvseqinit(tp); |
726 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
727 | f0cbd3ec | bellard | tp->t_state = TCPS_SYN_RECEIVED; |
728 | f0cbd3ec | bellard | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; |
729 | f0cbd3ec | bellard | tcpstat.tcps_accepts++; |
730 | f0cbd3ec | bellard | goto trimthenstep6;
|
731 | f0cbd3ec | bellard | } /* case TCPS_LISTEN */
|
732 | f0cbd3ec | bellard | |
733 | f0cbd3ec | bellard | /*
|
734 | f0cbd3ec | bellard | * If the state is SYN_SENT:
|
735 | f0cbd3ec | bellard | * if seg contains an ACK, but not for our SYN, drop the input.
|
736 | f0cbd3ec | bellard | * if seg contains a RST, then drop the connection.
|
737 | f0cbd3ec | bellard | * if seg does not contain SYN, then drop it.
|
738 | f0cbd3ec | bellard | * Otherwise this is an acceptable SYN segment
|
739 | f0cbd3ec | bellard | * initialize tp->rcv_nxt and tp->irs
|
740 | f0cbd3ec | bellard | * if seg contains ack then advance tp->snd_una
|
741 | f0cbd3ec | bellard | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
|
742 | f0cbd3ec | bellard | * arrange for segment to be acked (eventually)
|
743 | f0cbd3ec | bellard | * continue processing rest of data/controls, beginning with URG
|
744 | f0cbd3ec | bellard | */
|
745 | f0cbd3ec | bellard | case TCPS_SYN_SENT:
|
746 | f0cbd3ec | bellard | if ((tiflags & TH_ACK) &&
|
747 | f0cbd3ec | bellard | (SEQ_LEQ(ti->ti_ack, tp->iss) || |
748 | f0cbd3ec | bellard | SEQ_GT(ti->ti_ack, tp->snd_max))) |
749 | f0cbd3ec | bellard | goto dropwithreset;
|
750 | f0cbd3ec | bellard | |
751 | f0cbd3ec | bellard | if (tiflags & TH_RST) {
|
752 | f0cbd3ec | bellard | if (tiflags & TH_ACK)
|
753 | f0cbd3ec | bellard | tp = tcp_drop(tp,0); /* XXX Check t_softerror! */ |
754 | f0cbd3ec | bellard | goto drop;
|
755 | f0cbd3ec | bellard | } |
756 | f0cbd3ec | bellard | |
757 | f0cbd3ec | bellard | if ((tiflags & TH_SYN) == 0) |
758 | f0cbd3ec | bellard | goto drop;
|
759 | f0cbd3ec | bellard | if (tiflags & TH_ACK) {
|
760 | f0cbd3ec | bellard | tp->snd_una = ti->ti_ack; |
761 | f0cbd3ec | bellard | if (SEQ_LT(tp->snd_nxt, tp->snd_una))
|
762 | f0cbd3ec | bellard | tp->snd_nxt = tp->snd_una; |
763 | f0cbd3ec | bellard | } |
764 | f0cbd3ec | bellard | |
765 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = 0;
|
766 | f0cbd3ec | bellard | tp->irs = ti->ti_seq; |
767 | f0cbd3ec | bellard | tcp_rcvseqinit(tp); |
768 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
769 | f0cbd3ec | bellard | if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
|
770 | f0cbd3ec | bellard | tcpstat.tcps_connects++; |
771 | f0cbd3ec | bellard | soisfconnected(so); |
772 | f0cbd3ec | bellard | tp->t_state = TCPS_ESTABLISHED; |
773 | f0cbd3ec | bellard | |
774 | f0cbd3ec | bellard | /* Do window scaling on this connection? */
|
775 | f0cbd3ec | bellard | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
|
776 | f0cbd3ec | bellard | * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
|
777 | f0cbd3ec | bellard | * tp->snd_scale = tp->requested_s_scale;
|
778 | f0cbd3ec | bellard | * tp->rcv_scale = tp->request_r_scale;
|
779 | f0cbd3ec | bellard | * }
|
780 | f0cbd3ec | bellard | */
|
781 | f0cbd3ec | bellard | (void) tcp_reass(tp, (struct tcpiphdr *)0, |
782 | f0cbd3ec | bellard | (struct mbuf *)0); |
783 | f0cbd3ec | bellard | /*
|
784 | f0cbd3ec | bellard | * if we didn't have to retransmit the SYN,
|
785 | f0cbd3ec | bellard | * use its rtt as our initial srtt & rtt var.
|
786 | f0cbd3ec | bellard | */
|
787 | f0cbd3ec | bellard | if (tp->t_rtt)
|
788 | f0cbd3ec | bellard | tcp_xmit_timer(tp, tp->t_rtt); |
789 | f0cbd3ec | bellard | } else
|
790 | f0cbd3ec | bellard | tp->t_state = TCPS_SYN_RECEIVED; |
791 | f0cbd3ec | bellard | |
792 | f0cbd3ec | bellard | trimthenstep6:
|
793 | f0cbd3ec | bellard | /*
|
794 | f0cbd3ec | bellard | * Advance ti->ti_seq to correspond to first data byte.
|
795 | f0cbd3ec | bellard | * If data, trim to stay within window,
|
796 | f0cbd3ec | bellard | * dropping FIN if necessary.
|
797 | f0cbd3ec | bellard | */
|
798 | f0cbd3ec | bellard | ti->ti_seq++; |
799 | f0cbd3ec | bellard | if (ti->ti_len > tp->rcv_wnd) {
|
800 | f0cbd3ec | bellard | todrop = ti->ti_len - tp->rcv_wnd; |
801 | f0cbd3ec | bellard | m_adj(m, -todrop); |
802 | f0cbd3ec | bellard | ti->ti_len = tp->rcv_wnd; |
803 | f0cbd3ec | bellard | tiflags &= ~TH_FIN; |
804 | f0cbd3ec | bellard | tcpstat.tcps_rcvpackafterwin++; |
805 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyteafterwin += todrop; |
806 | f0cbd3ec | bellard | } |
807 | f0cbd3ec | bellard | tp->snd_wl1 = ti->ti_seq - 1;
|
808 | f0cbd3ec | bellard | tp->rcv_up = ti->ti_seq; |
809 | f0cbd3ec | bellard | goto step6;
|
810 | f0cbd3ec | bellard | } /* switch tp->t_state */
|
811 | f0cbd3ec | bellard | /*
|
812 | f0cbd3ec | bellard | * States other than LISTEN or SYN_SENT.
|
813 | f0cbd3ec | bellard | * First check timestamp, if present.
|
814 | f0cbd3ec | bellard | * Then check that at least some bytes of segment are within
|
815 | f0cbd3ec | bellard | * receive window. If segment begins before rcv_nxt,
|
816 | f0cbd3ec | bellard | * drop leading data (and SYN); if nothing left, just ack.
|
817 | f0cbd3ec | bellard | *
|
818 | f0cbd3ec | bellard | * RFC 1323 PAWS: If we have a timestamp reply on this segment
|
819 | f0cbd3ec | bellard | * and it's less than ts_recent, drop it.
|
820 | f0cbd3ec | bellard | */
|
821 | f0cbd3ec | bellard | /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
|
822 | f0cbd3ec | bellard | * TSTMP_LT(ts_val, tp->ts_recent)) {
|
823 | f0cbd3ec | bellard | *
|
824 | f0cbd3ec | bellard | */ /* Check to see if ts_recent is over 24 days old. */ |
825 | f0cbd3ec | bellard | /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
|
826 | f0cbd3ec | bellard | */ /* |
827 | f0cbd3ec | bellard | * * Invalidate ts_recent. If this segment updates
|
828 | f0cbd3ec | bellard | * * ts_recent, the age will be reset later and ts_recent
|
829 | f0cbd3ec | bellard | * * will get a valid value. If it does not, setting
|
830 | f0cbd3ec | bellard | * * ts_recent to zero will at least satisfy the
|
831 | f0cbd3ec | bellard | * * requirement that zero be placed in the timestamp
|
832 | f0cbd3ec | bellard | * * echo reply when ts_recent isn't valid. The
|
833 | f0cbd3ec | bellard | * * age isn't reset until we get a valid ts_recent
|
834 | f0cbd3ec | bellard | * * because we don't want out-of-order segments to be
|
835 | f0cbd3ec | bellard | * * dropped when ts_recent is old.
|
836 | f0cbd3ec | bellard | * */
|
837 | f0cbd3ec | bellard | /* tp->ts_recent = 0;
|
838 | f0cbd3ec | bellard | * } else {
|
839 | f0cbd3ec | bellard | * tcpstat.tcps_rcvduppack++;
|
840 | f0cbd3ec | bellard | * tcpstat.tcps_rcvdupbyte += ti->ti_len;
|
841 | f0cbd3ec | bellard | * tcpstat.tcps_pawsdrop++;
|
842 | f0cbd3ec | bellard | * goto dropafterack;
|
843 | f0cbd3ec | bellard | * }
|
844 | f0cbd3ec | bellard | * }
|
845 | f0cbd3ec | bellard | */
|
846 | f0cbd3ec | bellard | |
847 | f0cbd3ec | bellard | todrop = tp->rcv_nxt - ti->ti_seq; |
848 | f0cbd3ec | bellard | if (todrop > 0) { |
849 | f0cbd3ec | bellard | if (tiflags & TH_SYN) {
|
850 | f0cbd3ec | bellard | tiflags &= ~TH_SYN; |
851 | f0cbd3ec | bellard | ti->ti_seq++; |
852 | f0cbd3ec | bellard | if (ti->ti_urp > 1) |
853 | f0cbd3ec | bellard | ti->ti_urp--; |
854 | f0cbd3ec | bellard | else
|
855 | f0cbd3ec | bellard | tiflags &= ~TH_URG; |
856 | f0cbd3ec | bellard | todrop--; |
857 | f0cbd3ec | bellard | } |
858 | f0cbd3ec | bellard | /*
|
859 | f0cbd3ec | bellard | * Following if statement from Stevens, vol. 2, p. 960.
|
860 | f0cbd3ec | bellard | */
|
861 | f0cbd3ec | bellard | if (todrop > ti->ti_len
|
862 | f0cbd3ec | bellard | || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
|
863 | f0cbd3ec | bellard | /*
|
864 | f0cbd3ec | bellard | * Any valid FIN must be to the left of the window.
|
865 | f0cbd3ec | bellard | * At this point the FIN must be a duplicate or out
|
866 | f0cbd3ec | bellard | * of sequence; drop it.
|
867 | f0cbd3ec | bellard | */
|
868 | f0cbd3ec | bellard | tiflags &= ~TH_FIN; |
869 | f0cbd3ec | bellard | |
870 | f0cbd3ec | bellard | /*
|
871 | f0cbd3ec | bellard | * Send an ACK to resynchronize and drop any data.
|
872 | f0cbd3ec | bellard | * But keep on processing for RST or ACK.
|
873 | f0cbd3ec | bellard | */
|
874 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
875 | f0cbd3ec | bellard | todrop = ti->ti_len; |
876 | f0cbd3ec | bellard | tcpstat.tcps_rcvduppack++; |
877 | f0cbd3ec | bellard | tcpstat.tcps_rcvdupbyte += todrop; |
878 | f0cbd3ec | bellard | } else {
|
879 | f0cbd3ec | bellard | tcpstat.tcps_rcvpartduppack++; |
880 | f0cbd3ec | bellard | tcpstat.tcps_rcvpartdupbyte += todrop; |
881 | f0cbd3ec | bellard | } |
882 | f0cbd3ec | bellard | m_adj(m, todrop); |
883 | f0cbd3ec | bellard | ti->ti_seq += todrop; |
884 | f0cbd3ec | bellard | ti->ti_len -= todrop; |
885 | f0cbd3ec | bellard | if (ti->ti_urp > todrop)
|
886 | f0cbd3ec | bellard | ti->ti_urp -= todrop; |
887 | f0cbd3ec | bellard | else {
|
888 | f0cbd3ec | bellard | tiflags &= ~TH_URG; |
889 | f0cbd3ec | bellard | ti->ti_urp = 0;
|
890 | f0cbd3ec | bellard | } |
891 | f0cbd3ec | bellard | } |
892 | f0cbd3ec | bellard | /*
|
893 | f0cbd3ec | bellard | * If new data are received on a connection after the
|
894 | f0cbd3ec | bellard | * user processes are gone, then RST the other end.
|
895 | f0cbd3ec | bellard | */
|
896 | f0cbd3ec | bellard | if ((so->so_state & SS_NOFDREF) &&
|
897 | f0cbd3ec | bellard | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { |
898 | f0cbd3ec | bellard | tp = tcp_close(tp); |
899 | f0cbd3ec | bellard | tcpstat.tcps_rcvafterclose++; |
900 | f0cbd3ec | bellard | goto dropwithreset;
|
901 | f0cbd3ec | bellard | } |
902 | f0cbd3ec | bellard | |
903 | f0cbd3ec | bellard | /*
|
904 | f0cbd3ec | bellard | * If segment ends after window, drop trailing data
|
905 | f0cbd3ec | bellard | * (and PUSH and FIN); if nothing left, just ACK.
|
906 | f0cbd3ec | bellard | */
|
907 | f0cbd3ec | bellard | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); |
908 | f0cbd3ec | bellard | if (todrop > 0) { |
909 | f0cbd3ec | bellard | tcpstat.tcps_rcvpackafterwin++; |
910 | f0cbd3ec | bellard | if (todrop >= ti->ti_len) {
|
911 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; |
912 | f0cbd3ec | bellard | /*
|
913 | f0cbd3ec | bellard | * If a new connection request is received
|
914 | f0cbd3ec | bellard | * while in TIME_WAIT, drop the old connection
|
915 | f0cbd3ec | bellard | * and start over if the sequence numbers
|
916 | f0cbd3ec | bellard | * are above the previous ones.
|
917 | f0cbd3ec | bellard | */
|
918 | f0cbd3ec | bellard | if (tiflags & TH_SYN &&
|
919 | f0cbd3ec | bellard | tp->t_state == TCPS_TIME_WAIT && |
920 | f0cbd3ec | bellard | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { |
921 | f0cbd3ec | bellard | iss = tp->rcv_nxt + TCP_ISSINCR; |
922 | f0cbd3ec | bellard | tp = tcp_close(tp); |
923 | f0cbd3ec | bellard | goto findso;
|
924 | f0cbd3ec | bellard | } |
925 | f0cbd3ec | bellard | /*
|
926 | f0cbd3ec | bellard | * If window is closed can only take segments at
|
927 | f0cbd3ec | bellard | * window edge, and have to drop data and PUSH from
|
928 | f0cbd3ec | bellard | * incoming segments. Continue processing, but
|
929 | f0cbd3ec | bellard | * remember to ack. Otherwise, drop segment
|
930 | f0cbd3ec | bellard | * and ack.
|
931 | f0cbd3ec | bellard | */
|
932 | f0cbd3ec | bellard | if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { |
933 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
934 | f0cbd3ec | bellard | tcpstat.tcps_rcvwinprobe++; |
935 | f0cbd3ec | bellard | } else
|
936 | f0cbd3ec | bellard | goto dropafterack;
|
937 | f0cbd3ec | bellard | } else
|
938 | f0cbd3ec | bellard | tcpstat.tcps_rcvbyteafterwin += todrop; |
939 | f0cbd3ec | bellard | m_adj(m, -todrop); |
940 | f0cbd3ec | bellard | ti->ti_len -= todrop; |
941 | f0cbd3ec | bellard | tiflags &= ~(TH_PUSH|TH_FIN); |
942 | f0cbd3ec | bellard | } |
943 | f0cbd3ec | bellard | |
944 | f0cbd3ec | bellard | /*
|
945 | f0cbd3ec | bellard | * If last ACK falls within this segment's sequence numbers,
|
946 | f0cbd3ec | bellard | * record its timestamp.
|
947 | f0cbd3ec | bellard | */
|
948 | f0cbd3ec | bellard | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
|
949 | f0cbd3ec | bellard | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
|
950 | f0cbd3ec | bellard | * ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
|
951 | f0cbd3ec | bellard | * tp->ts_recent_age = tcp_now;
|
952 | f0cbd3ec | bellard | * tp->ts_recent = ts_val;
|
953 | f0cbd3ec | bellard | * }
|
954 | f0cbd3ec | bellard | */
|
955 | f0cbd3ec | bellard | |
956 | f0cbd3ec | bellard | /*
|
957 | f0cbd3ec | bellard | * If the RST bit is set examine the state:
|
958 | f0cbd3ec | bellard | * SYN_RECEIVED STATE:
|
959 | f0cbd3ec | bellard | * If passive open, return to LISTEN state.
|
960 | f0cbd3ec | bellard | * If active open, inform user that connection was refused.
|
961 | f0cbd3ec | bellard | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
|
962 | f0cbd3ec | bellard | * Inform user that connection was reset, and close tcb.
|
963 | f0cbd3ec | bellard | * CLOSING, LAST_ACK, TIME_WAIT STATES
|
964 | f0cbd3ec | bellard | * Close the tcb.
|
965 | f0cbd3ec | bellard | */
|
966 | f0cbd3ec | bellard | if (tiflags&TH_RST) switch (tp->t_state) { |
967 | f0cbd3ec | bellard | |
968 | f0cbd3ec | bellard | case TCPS_SYN_RECEIVED:
|
969 | f0cbd3ec | bellard | /* so->so_error = ECONNREFUSED; */
|
970 | f0cbd3ec | bellard | goto close;
|
971 | f0cbd3ec | bellard | |
972 | f0cbd3ec | bellard | case TCPS_ESTABLISHED:
|
973 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_1:
|
974 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_2:
|
975 | f0cbd3ec | bellard | case TCPS_CLOSE_WAIT:
|
976 | f0cbd3ec | bellard | /* so->so_error = ECONNRESET; */
|
977 | f0cbd3ec | bellard | close:
|
978 | f0cbd3ec | bellard | tp->t_state = TCPS_CLOSED; |
979 | f0cbd3ec | bellard | tcpstat.tcps_drops++; |
980 | f0cbd3ec | bellard | tp = tcp_close(tp); |
981 | f0cbd3ec | bellard | goto drop;
|
982 | f0cbd3ec | bellard | |
983 | f0cbd3ec | bellard | case TCPS_CLOSING:
|
984 | f0cbd3ec | bellard | case TCPS_LAST_ACK:
|
985 | f0cbd3ec | bellard | case TCPS_TIME_WAIT:
|
986 | f0cbd3ec | bellard | tp = tcp_close(tp); |
987 | f0cbd3ec | bellard | goto drop;
|
988 | f0cbd3ec | bellard | } |
989 | f0cbd3ec | bellard | |
990 | f0cbd3ec | bellard | /*
|
991 | f0cbd3ec | bellard | * If a SYN is in the window, then this is an
|
992 | f0cbd3ec | bellard | * error and we send an RST and drop the connection.
|
993 | f0cbd3ec | bellard | */
|
994 | f0cbd3ec | bellard | if (tiflags & TH_SYN) {
|
995 | f0cbd3ec | bellard | tp = tcp_drop(tp,0);
|
996 | f0cbd3ec | bellard | goto dropwithreset;
|
997 | f0cbd3ec | bellard | } |
998 | f0cbd3ec | bellard | |
999 | f0cbd3ec | bellard | /*
|
1000 | f0cbd3ec | bellard | * If the ACK bit is off we drop the segment and return.
|
1001 | f0cbd3ec | bellard | */
|
1002 | f0cbd3ec | bellard | if ((tiflags & TH_ACK) == 0) goto drop; |
1003 | f0cbd3ec | bellard | |
1004 | f0cbd3ec | bellard | /*
|
1005 | f0cbd3ec | bellard | * Ack processing.
|
1006 | f0cbd3ec | bellard | */
|
1007 | f0cbd3ec | bellard | switch (tp->t_state) {
|
1008 | f0cbd3ec | bellard | /*
|
1009 | f0cbd3ec | bellard | * In SYN_RECEIVED state if the ack ACKs our SYN then enter
|
1010 | f0cbd3ec | bellard | * ESTABLISHED state and continue processing, otherwise
|
1011 | f0cbd3ec | bellard | * send an RST. una<=ack<=max
|
1012 | f0cbd3ec | bellard | */
|
1013 | f0cbd3ec | bellard | case TCPS_SYN_RECEIVED:
|
1014 | f0cbd3ec | bellard | |
1015 | f0cbd3ec | bellard | if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
|
1016 | f0cbd3ec | bellard | SEQ_GT(ti->ti_ack, tp->snd_max)) |
1017 | f0cbd3ec | bellard | goto dropwithreset;
|
1018 | f0cbd3ec | bellard | tcpstat.tcps_connects++; |
1019 | f0cbd3ec | bellard | tp->t_state = TCPS_ESTABLISHED; |
1020 | f0cbd3ec | bellard | /*
|
1021 | f0cbd3ec | bellard | * The sent SYN is ack'ed with our sequence number +1
|
1022 | f0cbd3ec | bellard | * The first data byte already in the buffer will get
|
1023 | f0cbd3ec | bellard | * lost if no correction is made. This is only needed for
|
1024 | f0cbd3ec | bellard | * SS_CTL since the buffer is empty otherwise.
|
1025 | f0cbd3ec | bellard | * tp->snd_una++; or:
|
1026 | f0cbd3ec | bellard | */
|
1027 | f0cbd3ec | bellard | tp->snd_una=ti->ti_ack; |
1028 | f0cbd3ec | bellard | if (so->so_state & SS_CTL) {
|
1029 | f0cbd3ec | bellard | /* So tcp_ctl reports the right state */
|
1030 | f0cbd3ec | bellard | ret = tcp_ctl(so); |
1031 | f0cbd3ec | bellard | if (ret == 1) { |
1032 | f0cbd3ec | bellard | soisfconnected(so); |
1033 | f0cbd3ec | bellard | so->so_state &= ~SS_CTL; /* success XXX */
|
1034 | f0cbd3ec | bellard | } else if (ret == 2) { |
1035 | f0cbd3ec | bellard | so->so_state = SS_NOFDREF; /* CTL_CMD */
|
1036 | f0cbd3ec | bellard | } else {
|
1037 | f0cbd3ec | bellard | needoutput = 1;
|
1038 | f0cbd3ec | bellard | tp->t_state = TCPS_FIN_WAIT_1; |
1039 | f0cbd3ec | bellard | } |
1040 | f0cbd3ec | bellard | } else {
|
1041 | f0cbd3ec | bellard | soisfconnected(so); |
1042 | f0cbd3ec | bellard | } |
1043 | f0cbd3ec | bellard | |
1044 | f0cbd3ec | bellard | /* Do window scaling? */
|
1045 | f0cbd3ec | bellard | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
|
1046 | f0cbd3ec | bellard | * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
|
1047 | f0cbd3ec | bellard | * tp->snd_scale = tp->requested_s_scale;
|
1048 | f0cbd3ec | bellard | * tp->rcv_scale = tp->request_r_scale;
|
1049 | f0cbd3ec | bellard | * }
|
1050 | f0cbd3ec | bellard | */
|
1051 | f0cbd3ec | bellard | (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); |
1052 | f0cbd3ec | bellard | tp->snd_wl1 = ti->ti_seq - 1;
|
1053 | f0cbd3ec | bellard | /* Avoid ack processing; snd_una==ti_ack => dup ack */
|
1054 | f0cbd3ec | bellard | goto synrx_to_est;
|
1055 | f0cbd3ec | bellard | /* fall into ... */
|
1056 | f0cbd3ec | bellard | |
1057 | f0cbd3ec | bellard | /*
|
1058 | f0cbd3ec | bellard | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
|
1059 | f0cbd3ec | bellard | * ACKs. If the ack is in the range
|
1060 | f0cbd3ec | bellard | * tp->snd_una < ti->ti_ack <= tp->snd_max
|
1061 | f0cbd3ec | bellard | * then advance tp->snd_una to ti->ti_ack and drop
|
1062 | f0cbd3ec | bellard | * data from the retransmission queue. If this ACK reflects
|
1063 | f0cbd3ec | bellard | * more up to date window information we update our window information.
|
1064 | f0cbd3ec | bellard | */
|
1065 | f0cbd3ec | bellard | case TCPS_ESTABLISHED:
|
1066 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_1:
|
1067 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_2:
|
1068 | f0cbd3ec | bellard | case TCPS_CLOSE_WAIT:
|
1069 | f0cbd3ec | bellard | case TCPS_CLOSING:
|
1070 | f0cbd3ec | bellard | case TCPS_LAST_ACK:
|
1071 | f0cbd3ec | bellard | case TCPS_TIME_WAIT:
|
1072 | f0cbd3ec | bellard | |
1073 | f0cbd3ec | bellard | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
|
1074 | f0cbd3ec | bellard | if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { |
1075 | f0cbd3ec | bellard | tcpstat.tcps_rcvdupack++; |
1076 | f0cbd3ec | bellard | DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n",
|
1077 | f0cbd3ec | bellard | (long )m, (long )so)); |
1078 | f0cbd3ec | bellard | /*
|
1079 | f0cbd3ec | bellard | * If we have outstanding data (other than
|
1080 | f0cbd3ec | bellard | * a window probe), this is a completely
|
1081 | f0cbd3ec | bellard | * duplicate ack (ie, window info didn't
|
1082 | f0cbd3ec | bellard | * change), the ack is the biggest we've
|
1083 | f0cbd3ec | bellard | * seen and we've seen exactly our rexmt
|
1084 | f0cbd3ec | bellard | * threshold of them, assume a packet
|
1085 | f0cbd3ec | bellard | * has been dropped and retransmit it.
|
1086 | f0cbd3ec | bellard | * Kludge snd_nxt & the congestion
|
1087 | f0cbd3ec | bellard | * window so we send only this one
|
1088 | f0cbd3ec | bellard | * packet.
|
1089 | f0cbd3ec | bellard | *
|
1090 | f0cbd3ec | bellard | * We know we're losing at the current
|
1091 | f0cbd3ec | bellard | * window size so do congestion avoidance
|
1092 | f0cbd3ec | bellard | * (set ssthresh to half the current window
|
1093 | f0cbd3ec | bellard | * and pull our congestion window back to
|
1094 | f0cbd3ec | bellard | * the new ssthresh).
|
1095 | f0cbd3ec | bellard | *
|
1096 | f0cbd3ec | bellard | * Dup acks mean that packets have left the
|
1097 | f0cbd3ec | bellard | * network (they're now cached at the receiver)
|
1098 | f0cbd3ec | bellard | * so bump cwnd by the amount in the receiver
|
1099 | f0cbd3ec | bellard | * to keep a constant cwnd packets in the
|
1100 | f0cbd3ec | bellard | * network.
|
1101 | f0cbd3ec | bellard | */
|
1102 | f0cbd3ec | bellard | if (tp->t_timer[TCPT_REXMT] == 0 || |
1103 | f0cbd3ec | bellard | ti->ti_ack != tp->snd_una) |
1104 | f0cbd3ec | bellard | tp->t_dupacks = 0;
|
1105 | f0cbd3ec | bellard | else if (++tp->t_dupacks == tcprexmtthresh) { |
1106 | f0cbd3ec | bellard | tcp_seq onxt = tp->snd_nxt; |
1107 | f0cbd3ec | bellard | u_int win = |
1108 | f0cbd3ec | bellard | min(tp->snd_wnd, tp->snd_cwnd) / 2 /
|
1109 | f0cbd3ec | bellard | tp->t_maxseg; |
1110 | f0cbd3ec | bellard | |
1111 | f0cbd3ec | bellard | if (win < 2) |
1112 | f0cbd3ec | bellard | win = 2;
|
1113 | f0cbd3ec | bellard | tp->snd_ssthresh = win * tp->t_maxseg; |
1114 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = 0;
|
1115 | f0cbd3ec | bellard | tp->t_rtt = 0;
|
1116 | f0cbd3ec | bellard | tp->snd_nxt = ti->ti_ack; |
1117 | f0cbd3ec | bellard | tp->snd_cwnd = tp->t_maxseg; |
1118 | f0cbd3ec | bellard | (void) tcp_output(tp);
|
1119 | f0cbd3ec | bellard | tp->snd_cwnd = tp->snd_ssthresh + |
1120 | f0cbd3ec | bellard | tp->t_maxseg * tp->t_dupacks; |
1121 | f0cbd3ec | bellard | if (SEQ_GT(onxt, tp->snd_nxt))
|
1122 | f0cbd3ec | bellard | tp->snd_nxt = onxt; |
1123 | f0cbd3ec | bellard | goto drop;
|
1124 | f0cbd3ec | bellard | } else if (tp->t_dupacks > tcprexmtthresh) { |
1125 | f0cbd3ec | bellard | tp->snd_cwnd += tp->t_maxseg; |
1126 | f0cbd3ec | bellard | (void) tcp_output(tp);
|
1127 | f0cbd3ec | bellard | goto drop;
|
1128 | f0cbd3ec | bellard | } |
1129 | f0cbd3ec | bellard | } else
|
1130 | f0cbd3ec | bellard | tp->t_dupacks = 0;
|
1131 | f0cbd3ec | bellard | break;
|
1132 | f0cbd3ec | bellard | } |
1133 | f0cbd3ec | bellard | synrx_to_est:
|
1134 | f0cbd3ec | bellard | /*
|
1135 | f0cbd3ec | bellard | * If the congestion window was inflated to account
|
1136 | f0cbd3ec | bellard | * for the other side's cached packets, retract it.
|
1137 | f0cbd3ec | bellard | */
|
1138 | f0cbd3ec | bellard | if (tp->t_dupacks > tcprexmtthresh &&
|
1139 | f0cbd3ec | bellard | tp->snd_cwnd > tp->snd_ssthresh) |
1140 | f0cbd3ec | bellard | tp->snd_cwnd = tp->snd_ssthresh; |
1141 | f0cbd3ec | bellard | tp->t_dupacks = 0;
|
1142 | f0cbd3ec | bellard | if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
|
1143 | f0cbd3ec | bellard | tcpstat.tcps_rcvacktoomuch++; |
1144 | f0cbd3ec | bellard | goto dropafterack;
|
1145 | f0cbd3ec | bellard | } |
1146 | f0cbd3ec | bellard | acked = ti->ti_ack - tp->snd_una; |
1147 | f0cbd3ec | bellard | tcpstat.tcps_rcvackpack++; |
1148 | f0cbd3ec | bellard | tcpstat.tcps_rcvackbyte += acked; |
1149 | f0cbd3ec | bellard | |
1150 | f0cbd3ec | bellard | /*
|
1151 | f0cbd3ec | bellard | * If we have a timestamp reply, update smoothed
|
1152 | f0cbd3ec | bellard | * round trip time. If no timestamp is present but
|
1153 | f0cbd3ec | bellard | * transmit timer is running and timed sequence
|
1154 | f0cbd3ec | bellard | * number was acked, update smoothed round trip time.
|
1155 | f0cbd3ec | bellard | * Since we now have an rtt measurement, cancel the
|
1156 | f0cbd3ec | bellard | * timer backoff (cf., Phil Karn's retransmit alg.).
|
1157 | f0cbd3ec | bellard | * Recompute the initial retransmit timer.
|
1158 | f0cbd3ec | bellard | */
|
1159 | f0cbd3ec | bellard | /* if (ts_present)
|
1160 | f0cbd3ec | bellard | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
|
1161 | f0cbd3ec | bellard | * else
|
1162 | f0cbd3ec | bellard | */
|
1163 | f0cbd3ec | bellard | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
|
1164 | f0cbd3ec | bellard | tcp_xmit_timer(tp,tp->t_rtt); |
1165 | f0cbd3ec | bellard | |
1166 | f0cbd3ec | bellard | /*
|
1167 | f0cbd3ec | bellard | * If all outstanding data is acked, stop retransmit
|
1168 | f0cbd3ec | bellard | * timer and remember to restart (more output or persist).
|
1169 | f0cbd3ec | bellard | * If there is more data to be acked, restart retransmit
|
1170 | f0cbd3ec | bellard | * timer, using current (possibly backed-off) value.
|
1171 | f0cbd3ec | bellard | */
|
1172 | f0cbd3ec | bellard | if (ti->ti_ack == tp->snd_max) {
|
1173 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = 0;
|
1174 | f0cbd3ec | bellard | needoutput = 1;
|
1175 | f0cbd3ec | bellard | } else if (tp->t_timer[TCPT_PERSIST] == 0) |
1176 | f0cbd3ec | bellard | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; |
1177 | f0cbd3ec | bellard | /*
|
1178 | f0cbd3ec | bellard | * When new data is acked, open the congestion window.
|
1179 | f0cbd3ec | bellard | * If the window gives us less than ssthresh packets
|
1180 | f0cbd3ec | bellard | * in flight, open exponentially (maxseg per packet).
|
1181 | f0cbd3ec | bellard | * Otherwise open linearly: maxseg per window
|
1182 | f0cbd3ec | bellard | * (maxseg^2 / cwnd per packet).
|
1183 | f0cbd3ec | bellard | */
|
1184 | f0cbd3ec | bellard | { |
1185 | f0cbd3ec | bellard | register u_int cw = tp->snd_cwnd;
|
1186 | f0cbd3ec | bellard | register u_int incr = tp->t_maxseg;
|
1187 | f0cbd3ec | bellard | |
1188 | f0cbd3ec | bellard | if (cw > tp->snd_ssthresh)
|
1189 | f0cbd3ec | bellard | incr = incr * incr / cw; |
1190 | f0cbd3ec | bellard | tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); |
1191 | f0cbd3ec | bellard | } |
1192 | f0cbd3ec | bellard | if (acked > so->so_snd.sb_cc) {
|
1193 | f0cbd3ec | bellard | tp->snd_wnd -= so->so_snd.sb_cc; |
1194 | f0cbd3ec | bellard | sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
|
1195 | f0cbd3ec | bellard | ourfinisacked = 1;
|
1196 | f0cbd3ec | bellard | } else {
|
1197 | f0cbd3ec | bellard | sbdrop(&so->so_snd, acked); |
1198 | f0cbd3ec | bellard | tp->snd_wnd -= acked; |
1199 | f0cbd3ec | bellard | ourfinisacked = 0;
|
1200 | f0cbd3ec | bellard | } |
1201 | f0cbd3ec | bellard | /*
|
1202 | f0cbd3ec | bellard | * XXX sowwakup is called when data is acked and there's room for
|
1203 | f0cbd3ec | bellard | * for more data... it should read() the socket
|
1204 | f0cbd3ec | bellard | */
|
1205 | f0cbd3ec | bellard | /* if (so->so_snd.sb_flags & SB_NOTIFY)
|
1206 | f0cbd3ec | bellard | * sowwakeup(so);
|
1207 | f0cbd3ec | bellard | */
|
1208 | f0cbd3ec | bellard | tp->snd_una = ti->ti_ack; |
1209 | f0cbd3ec | bellard | if (SEQ_LT(tp->snd_nxt, tp->snd_una))
|
1210 | f0cbd3ec | bellard | tp->snd_nxt = tp->snd_una; |
1211 | f0cbd3ec | bellard | |
1212 | f0cbd3ec | bellard | switch (tp->t_state) {
|
1213 | f0cbd3ec | bellard | |
1214 | f0cbd3ec | bellard | /*
|
1215 | f0cbd3ec | bellard | * In FIN_WAIT_1 STATE in addition to the processing
|
1216 | f0cbd3ec | bellard | * for the ESTABLISHED state if our FIN is now acknowledged
|
1217 | f0cbd3ec | bellard | * then enter FIN_WAIT_2.
|
1218 | f0cbd3ec | bellard | */
|
1219 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_1:
|
1220 | f0cbd3ec | bellard | if (ourfinisacked) {
|
1221 | f0cbd3ec | bellard | /*
|
1222 | f0cbd3ec | bellard | * If we can't receive any more
|
1223 | f0cbd3ec | bellard | * data, then closing user can proceed.
|
1224 | f0cbd3ec | bellard | * Starting the timer is contrary to the
|
1225 | f0cbd3ec | bellard | * specification, but if we don't get a FIN
|
1226 | f0cbd3ec | bellard | * we'll hang forever.
|
1227 | f0cbd3ec | bellard | */
|
1228 | f0cbd3ec | bellard | if (so->so_state & SS_FCANTRCVMORE) {
|
1229 | f0cbd3ec | bellard | soisfdisconnected(so); |
1230 | f0cbd3ec | bellard | tp->t_timer[TCPT_2MSL] = tcp_maxidle; |
1231 | f0cbd3ec | bellard | } |
1232 | f0cbd3ec | bellard | tp->t_state = TCPS_FIN_WAIT_2; |
1233 | f0cbd3ec | bellard | } |
1234 | f0cbd3ec | bellard | break;
|
1235 | f0cbd3ec | bellard | |
1236 | f0cbd3ec | bellard | /*
|
1237 | f0cbd3ec | bellard | * In CLOSING STATE in addition to the processing for
|
1238 | f0cbd3ec | bellard | * the ESTABLISHED state if the ACK acknowledges our FIN
|
1239 | f0cbd3ec | bellard | * then enter the TIME-WAIT state, otherwise ignore
|
1240 | f0cbd3ec | bellard | * the segment.
|
1241 | f0cbd3ec | bellard | */
|
1242 | f0cbd3ec | bellard | case TCPS_CLOSING:
|
1243 | f0cbd3ec | bellard | if (ourfinisacked) {
|
1244 | f0cbd3ec | bellard | tp->t_state = TCPS_TIME_WAIT; |
1245 | f0cbd3ec | bellard | tcp_canceltimers(tp); |
1246 | f0cbd3ec | bellard | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
1247 | f0cbd3ec | bellard | soisfdisconnected(so); |
1248 | f0cbd3ec | bellard | } |
1249 | f0cbd3ec | bellard | break;
|
1250 | f0cbd3ec | bellard | |
1251 | f0cbd3ec | bellard | /*
|
1252 | f0cbd3ec | bellard | * In LAST_ACK, we may still be waiting for data to drain
|
1253 | f0cbd3ec | bellard | * and/or to be acked, as well as for the ack of our FIN.
|
1254 | f0cbd3ec | bellard | * If our FIN is now acknowledged, delete the TCB,
|
1255 | f0cbd3ec | bellard | * enter the closed state and return.
|
1256 | f0cbd3ec | bellard | */
|
1257 | f0cbd3ec | bellard | case TCPS_LAST_ACK:
|
1258 | f0cbd3ec | bellard | if (ourfinisacked) {
|
1259 | f0cbd3ec | bellard | tp = tcp_close(tp); |
1260 | f0cbd3ec | bellard | goto drop;
|
1261 | f0cbd3ec | bellard | } |
1262 | f0cbd3ec | bellard | break;
|
1263 | f0cbd3ec | bellard | |
1264 | f0cbd3ec | bellard | /*
|
1265 | f0cbd3ec | bellard | * In TIME_WAIT state the only thing that should arrive
|
1266 | f0cbd3ec | bellard | * is a retransmission of the remote FIN. Acknowledge
|
1267 | f0cbd3ec | bellard | * it and restart the finack timer.
|
1268 | f0cbd3ec | bellard | */
|
1269 | f0cbd3ec | bellard | case TCPS_TIME_WAIT:
|
1270 | f0cbd3ec | bellard | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
1271 | f0cbd3ec | bellard | goto dropafterack;
|
1272 | f0cbd3ec | bellard | } |
1273 | f0cbd3ec | bellard | } /* switch(tp->t_state) */
|
1274 | f0cbd3ec | bellard | |
1275 | f0cbd3ec | bellard | step6:
|
1276 | f0cbd3ec | bellard | /*
|
1277 | f0cbd3ec | bellard | * Update window information.
|
1278 | f0cbd3ec | bellard | * Don't look at window if no ACK: TAC's send garbage on first SYN.
|
1279 | f0cbd3ec | bellard | */
|
1280 | f0cbd3ec | bellard | if ((tiflags & TH_ACK) &&
|
1281 | f0cbd3ec | bellard | (SEQ_LT(tp->snd_wl1, ti->ti_seq) || |
1282 | f0cbd3ec | bellard | (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
1283 | f0cbd3ec | bellard | (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { |
1284 | f0cbd3ec | bellard | /* keep track of pure window updates */
|
1285 | f0cbd3ec | bellard | if (ti->ti_len == 0 && |
1286 | f0cbd3ec | bellard | tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) |
1287 | f0cbd3ec | bellard | tcpstat.tcps_rcvwinupd++; |
1288 | f0cbd3ec | bellard | tp->snd_wnd = tiwin; |
1289 | f0cbd3ec | bellard | tp->snd_wl1 = ti->ti_seq; |
1290 | f0cbd3ec | bellard | tp->snd_wl2 = ti->ti_ack; |
1291 | f0cbd3ec | bellard | if (tp->snd_wnd > tp->max_sndwnd)
|
1292 | f0cbd3ec | bellard | tp->max_sndwnd = tp->snd_wnd; |
1293 | f0cbd3ec | bellard | needoutput = 1;
|
1294 | f0cbd3ec | bellard | } |
1295 | f0cbd3ec | bellard | |
1296 | f0cbd3ec | bellard | /*
|
1297 | f0cbd3ec | bellard | * Process segments with URG.
|
1298 | f0cbd3ec | bellard | */
|
1299 | f0cbd3ec | bellard | if ((tiflags & TH_URG) && ti->ti_urp &&
|
1300 | f0cbd3ec | bellard | TCPS_HAVERCVDFIN(tp->t_state) == 0) {
|
1301 | f0cbd3ec | bellard | /*
|
1302 | f0cbd3ec | bellard | * This is a kludge, but if we receive and accept
|
1303 | f0cbd3ec | bellard | * random urgent pointers, we'll crash in
|
1304 | f0cbd3ec | bellard | * soreceive. It's hard to imagine someone
|
1305 | f0cbd3ec | bellard | * actually wanting to send this much urgent data.
|
1306 | f0cbd3ec | bellard | */
|
1307 | f0cbd3ec | bellard | if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
|
1308 | f0cbd3ec | bellard | ti->ti_urp = 0;
|
1309 | f0cbd3ec | bellard | tiflags &= ~TH_URG; |
1310 | f0cbd3ec | bellard | goto dodata;
|
1311 | f0cbd3ec | bellard | } |
1312 | f0cbd3ec | bellard | /*
|
1313 | f0cbd3ec | bellard | * If this segment advances the known urgent pointer,
|
1314 | f0cbd3ec | bellard | * then mark the data stream. This should not happen
|
1315 | f0cbd3ec | bellard | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
|
1316 | f0cbd3ec | bellard | * a FIN has been received from the remote side.
|
1317 | f0cbd3ec | bellard | * In these states we ignore the URG.
|
1318 | f0cbd3ec | bellard | *
|
1319 | f0cbd3ec | bellard | * According to RFC961 (Assigned Protocols),
|
1320 | f0cbd3ec | bellard | * the urgent pointer points to the last octet
|
1321 | f0cbd3ec | bellard | * of urgent data. We continue, however,
|
1322 | f0cbd3ec | bellard | * to consider it to indicate the first octet
|
1323 | f0cbd3ec | bellard | * of data past the urgent section as the original
|
1324 | f0cbd3ec | bellard | * spec states (in one of two places).
|
1325 | f0cbd3ec | bellard | */
|
1326 | f0cbd3ec | bellard | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
|
1327 | f0cbd3ec | bellard | tp->rcv_up = ti->ti_seq + ti->ti_urp; |
1328 | f0cbd3ec | bellard | so->so_urgc = so->so_rcv.sb_cc + |
1329 | f0cbd3ec | bellard | (tp->rcv_up - tp->rcv_nxt); /* -1; */
|
1330 | f0cbd3ec | bellard | tp->rcv_up = ti->ti_seq + ti->ti_urp; |
1331 | f0cbd3ec | bellard | |
1332 | f0cbd3ec | bellard | } |
1333 | f0cbd3ec | bellard | } else
|
1334 | f0cbd3ec | bellard | /*
|
1335 | f0cbd3ec | bellard | * If no out of band data is expected,
|
1336 | f0cbd3ec | bellard | * pull receive urgent pointer along
|
1337 | f0cbd3ec | bellard | * with the receive window.
|
1338 | f0cbd3ec | bellard | */
|
1339 | f0cbd3ec | bellard | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
|
1340 | f0cbd3ec | bellard | tp->rcv_up = tp->rcv_nxt; |
1341 | f0cbd3ec | bellard | dodata:
|
1342 | f0cbd3ec | bellard | |
1343 | f0cbd3ec | bellard | /*
|
1344 | f0cbd3ec | bellard | * Process the segment text, merging it into the TCP sequencing queue,
|
1345 | f0cbd3ec | bellard | * and arranging for acknowledgment of receipt if necessary.
|
1346 | f0cbd3ec | bellard | * This process logically involves adjusting tp->rcv_wnd as data
|
1347 | f0cbd3ec | bellard | * is presented to the user (this happens in tcp_usrreq.c,
|
1348 | f0cbd3ec | bellard | * case PRU_RCVD). If a FIN has already been received on this
|
1349 | f0cbd3ec | bellard | * connection then we just ignore the text.
|
1350 | f0cbd3ec | bellard | */
|
1351 | f0cbd3ec | bellard | if ((ti->ti_len || (tiflags&TH_FIN)) &&
|
1352 | f0cbd3ec | bellard | TCPS_HAVERCVDFIN(tp->t_state) == 0) {
|
1353 | f0cbd3ec | bellard | TCP_REASS(tp, ti, m, so, tiflags); |
1354 | f0cbd3ec | bellard | /*
|
1355 | f0cbd3ec | bellard | * Note the amount of data that peer has sent into
|
1356 | f0cbd3ec | bellard | * our window, in order to estimate the sender's
|
1357 | f0cbd3ec | bellard | * buffer size.
|
1358 | f0cbd3ec | bellard | */
|
1359 | f0cbd3ec | bellard | len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt); |
1360 | f0cbd3ec | bellard | } else {
|
1361 | f0cbd3ec | bellard | m_free(m); |
1362 | f0cbd3ec | bellard | tiflags &= ~TH_FIN; |
1363 | f0cbd3ec | bellard | } |
1364 | f0cbd3ec | bellard | |
1365 | f0cbd3ec | bellard | /*
|
1366 | f0cbd3ec | bellard | * If FIN is received ACK the FIN and let the user know
|
1367 | f0cbd3ec | bellard | * that the connection is closing.
|
1368 | f0cbd3ec | bellard | */
|
1369 | f0cbd3ec | bellard | if (tiflags & TH_FIN) {
|
1370 | f0cbd3ec | bellard | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
1371 | f0cbd3ec | bellard | /*
|
1372 | f0cbd3ec | bellard | * If we receive a FIN we can't send more data,
|
1373 | f0cbd3ec | bellard | * set it SS_FDRAIN
|
1374 | f0cbd3ec | bellard | * Shutdown the socket if there is no rx data in the
|
1375 | f0cbd3ec | bellard | * buffer.
|
1376 | f0cbd3ec | bellard | * soread() is called on completion of shutdown() and
|
1377 | f0cbd3ec | bellard | * will got to TCPS_LAST_ACK, and use tcp_output()
|
1378 | f0cbd3ec | bellard | * to send the FIN.
|
1379 | f0cbd3ec | bellard | */
|
1380 | f0cbd3ec | bellard | /* sofcantrcvmore(so); */
|
1381 | f0cbd3ec | bellard | sofwdrain(so); |
1382 | f0cbd3ec | bellard | |
1383 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
1384 | f0cbd3ec | bellard | tp->rcv_nxt++; |
1385 | f0cbd3ec | bellard | } |
1386 | f0cbd3ec | bellard | switch (tp->t_state) {
|
1387 | f0cbd3ec | bellard | |
1388 | f0cbd3ec | bellard | /*
|
1389 | f0cbd3ec | bellard | * In SYN_RECEIVED and ESTABLISHED STATES
|
1390 | f0cbd3ec | bellard | * enter the CLOSE_WAIT state.
|
1391 | f0cbd3ec | bellard | */
|
1392 | f0cbd3ec | bellard | case TCPS_SYN_RECEIVED:
|
1393 | f0cbd3ec | bellard | case TCPS_ESTABLISHED:
|
1394 | f0cbd3ec | bellard | if(so->so_emu == EMU_CTL) /* no shutdown on socket */ |
1395 | f0cbd3ec | bellard | tp->t_state = TCPS_LAST_ACK; |
1396 | f0cbd3ec | bellard | else
|
1397 | f0cbd3ec | bellard | tp->t_state = TCPS_CLOSE_WAIT; |
1398 | f0cbd3ec | bellard | break;
|
1399 | f0cbd3ec | bellard | |
1400 | f0cbd3ec | bellard | /*
|
1401 | f0cbd3ec | bellard | * If still in FIN_WAIT_1 STATE FIN has not been acked so
|
1402 | f0cbd3ec | bellard | * enter the CLOSING state.
|
1403 | f0cbd3ec | bellard | */
|
1404 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_1:
|
1405 | f0cbd3ec | bellard | tp->t_state = TCPS_CLOSING; |
1406 | f0cbd3ec | bellard | break;
|
1407 | f0cbd3ec | bellard | |
1408 | f0cbd3ec | bellard | /*
|
1409 | f0cbd3ec | bellard | * In FIN_WAIT_2 state enter the TIME_WAIT state,
|
1410 | f0cbd3ec | bellard | * starting the time-wait timer, turning off the other
|
1411 | f0cbd3ec | bellard | * standard timers.
|
1412 | f0cbd3ec | bellard | */
|
1413 | f0cbd3ec | bellard | case TCPS_FIN_WAIT_2:
|
1414 | f0cbd3ec | bellard | tp->t_state = TCPS_TIME_WAIT; |
1415 | f0cbd3ec | bellard | tcp_canceltimers(tp); |
1416 | f0cbd3ec | bellard | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
1417 | f0cbd3ec | bellard | soisfdisconnected(so); |
1418 | f0cbd3ec | bellard | break;
|
1419 | f0cbd3ec | bellard | |
1420 | f0cbd3ec | bellard | /*
|
1421 | f0cbd3ec | bellard | * In TIME_WAIT state restart the 2 MSL time_wait timer.
|
1422 | f0cbd3ec | bellard | */
|
1423 | f0cbd3ec | bellard | case TCPS_TIME_WAIT:
|
1424 | f0cbd3ec | bellard | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
1425 | f0cbd3ec | bellard | break;
|
1426 | f0cbd3ec | bellard | } |
1427 | f0cbd3ec | bellard | } |
1428 | f0cbd3ec | bellard | |
1429 | f0cbd3ec | bellard | /*
|
1430 | f0cbd3ec | bellard | * If this is a small packet, then ACK now - with Nagel
|
1431 | f0cbd3ec | bellard | * congestion avoidance sender won't send more until
|
1432 | f0cbd3ec | bellard | * he gets an ACK.
|
1433 | f0cbd3ec | bellard | *
|
1434 | f0cbd3ec | bellard | * See above.
|
1435 | f0cbd3ec | bellard | */
|
1436 | f0cbd3ec | bellard | /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
|
1437 | f0cbd3ec | bellard | */
|
1438 | f0cbd3ec | bellard | /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
|
1439 | f0cbd3ec | bellard | * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
|
1440 | f0cbd3ec | bellard | * ((so->so_iptos & IPTOS_LOWDELAY) &&
|
1441 | f0cbd3ec | bellard | * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
|
1442 | f0cbd3ec | bellard | */
|
1443 | f0cbd3ec | bellard | if (ti->ti_len && (unsigned)ti->ti_len <= 5 && |
1444 | f0cbd3ec | bellard | ((struct tcpiphdr_2 *)ti)->first_char == (char)27) { |
1445 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
1446 | f0cbd3ec | bellard | } |
1447 | f0cbd3ec | bellard | |
1448 | f0cbd3ec | bellard | /*
|
1449 | f0cbd3ec | bellard | * Return any desired output.
|
1450 | f0cbd3ec | bellard | */
|
1451 | f0cbd3ec | bellard | if (needoutput || (tp->t_flags & TF_ACKNOW)) {
|
1452 | f0cbd3ec | bellard | (void) tcp_output(tp);
|
1453 | f0cbd3ec | bellard | } |
1454 | f0cbd3ec | bellard | return;
|
1455 | f0cbd3ec | bellard | |
1456 | f0cbd3ec | bellard | dropafterack:
|
1457 | f0cbd3ec | bellard | /*
|
1458 | f0cbd3ec | bellard | * Generate an ACK dropping incoming segment if it occupies
|
1459 | f0cbd3ec | bellard | * sequence space, where the ACK reflects our state.
|
1460 | f0cbd3ec | bellard | */
|
1461 | f0cbd3ec | bellard | if (tiflags & TH_RST)
|
1462 | f0cbd3ec | bellard | goto drop;
|
1463 | f0cbd3ec | bellard | m_freem(m); |
1464 | f0cbd3ec | bellard | tp->t_flags |= TF_ACKNOW; |
1465 | f0cbd3ec | bellard | (void) tcp_output(tp);
|
1466 | f0cbd3ec | bellard | return;
|
1467 | f0cbd3ec | bellard | |
1468 | f0cbd3ec | bellard | dropwithreset:
|
1469 | f0cbd3ec | bellard | /* reuses m if m!=NULL, m_free() unnecessary */
|
1470 | f0cbd3ec | bellard | if (tiflags & TH_ACK)
|
1471 | f0cbd3ec | bellard | tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
|
1472 | f0cbd3ec | bellard | else {
|
1473 | f0cbd3ec | bellard | if (tiflags & TH_SYN) ti->ti_len++;
|
1474 | f0cbd3ec | bellard | tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
|
1475 | f0cbd3ec | bellard | TH_RST|TH_ACK); |
1476 | f0cbd3ec | bellard | } |
1477 | f0cbd3ec | bellard | |
1478 | f0cbd3ec | bellard | return;
|
1479 | f0cbd3ec | bellard | |
1480 | f0cbd3ec | bellard | drop:
|
1481 | f0cbd3ec | bellard | /*
|
1482 | f0cbd3ec | bellard | * Drop space held by incoming segment and return.
|
1483 | f0cbd3ec | bellard | */
|
1484 | f0cbd3ec | bellard | m_free(m); |
1485 | f0cbd3ec | bellard | |
1486 | f0cbd3ec | bellard | return;
|
1487 | f0cbd3ec | bellard | } |
1488 | f0cbd3ec | bellard | |
1489 | f0cbd3ec | bellard | /* , ts_present, ts_val, ts_ecr) */
|
1490 | f0cbd3ec | bellard | /* int *ts_present;
|
1491 | f0cbd3ec | bellard | * u_int32_t *ts_val, *ts_ecr;
|
1492 | f0cbd3ec | bellard | */
|
1493 | f0cbd3ec | bellard | void
|
1494 | f0cbd3ec | bellard | tcp_dooptions(tp, cp, cnt, ti) |
1495 | f0cbd3ec | bellard | struct tcpcb *tp;
|
1496 | f0cbd3ec | bellard | u_char *cp; |
1497 | f0cbd3ec | bellard | int cnt;
|
1498 | f0cbd3ec | bellard | struct tcpiphdr *ti;
|
1499 | f0cbd3ec | bellard | { |
1500 | f0cbd3ec | bellard | u_int16_t mss; |
1501 | f0cbd3ec | bellard | int opt, optlen;
|
1502 | f0cbd3ec | bellard | |
1503 | f0cbd3ec | bellard | DEBUG_CALL("tcp_dooptions");
|
1504 | f0cbd3ec | bellard | DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt)); |
1505 | f0cbd3ec | bellard | |
1506 | f0cbd3ec | bellard | for (; cnt > 0; cnt -= optlen, cp += optlen) { |
1507 | f0cbd3ec | bellard | opt = cp[0];
|
1508 | f0cbd3ec | bellard | if (opt == TCPOPT_EOL)
|
1509 | f0cbd3ec | bellard | break;
|
1510 | f0cbd3ec | bellard | if (opt == TCPOPT_NOP)
|
1511 | f0cbd3ec | bellard | optlen = 1;
|
1512 | f0cbd3ec | bellard | else {
|
1513 | f0cbd3ec | bellard | optlen = cp[1];
|
1514 | f0cbd3ec | bellard | if (optlen <= 0) |
1515 | f0cbd3ec | bellard | break;
|
1516 | f0cbd3ec | bellard | } |
1517 | f0cbd3ec | bellard | switch (opt) {
|
1518 | f0cbd3ec | bellard | |
1519 | f0cbd3ec | bellard | default:
|
1520 | f0cbd3ec | bellard | continue;
|
1521 | f0cbd3ec | bellard | |
1522 | f0cbd3ec | bellard | case TCPOPT_MAXSEG:
|
1523 | f0cbd3ec | bellard | if (optlen != TCPOLEN_MAXSEG)
|
1524 | f0cbd3ec | bellard | continue;
|
1525 | f0cbd3ec | bellard | if (!(ti->ti_flags & TH_SYN))
|
1526 | f0cbd3ec | bellard | continue;
|
1527 | f0cbd3ec | bellard | memcpy((char *) &mss, (char *) cp + 2, sizeof(mss)); |
1528 | f0cbd3ec | bellard | NTOHS(mss); |
1529 | f0cbd3ec | bellard | (void) tcp_mss(tp, mss); /* sets t_maxseg */ |
1530 | f0cbd3ec | bellard | break;
|
1531 | f0cbd3ec | bellard | |
1532 | f0cbd3ec | bellard | /* case TCPOPT_WINDOW:
|
1533 | f0cbd3ec | bellard | * if (optlen != TCPOLEN_WINDOW)
|
1534 | f0cbd3ec | bellard | * continue;
|
1535 | f0cbd3ec | bellard | * if (!(ti->ti_flags & TH_SYN))
|
1536 | f0cbd3ec | bellard | * continue;
|
1537 | f0cbd3ec | bellard | * tp->t_flags |= TF_RCVD_SCALE;
|
1538 | f0cbd3ec | bellard | * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
|
1539 | f0cbd3ec | bellard | * break;
|
1540 | f0cbd3ec | bellard | */
|
1541 | f0cbd3ec | bellard | /* case TCPOPT_TIMESTAMP:
|
1542 | f0cbd3ec | bellard | * if (optlen != TCPOLEN_TIMESTAMP)
|
1543 | f0cbd3ec | bellard | * continue;
|
1544 | f0cbd3ec | bellard | * *ts_present = 1;
|
1545 | f0cbd3ec | bellard | * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
|
1546 | f0cbd3ec | bellard | * NTOHL(*ts_val);
|
1547 | f0cbd3ec | bellard | * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
|
1548 | f0cbd3ec | bellard | * NTOHL(*ts_ecr);
|
1549 | f0cbd3ec | bellard | *
|
1550 | f0cbd3ec | bellard | */ /* |
1551 | f0cbd3ec | bellard | * * A timestamp received in a SYN makes
|
1552 | f0cbd3ec | bellard | * * it ok to send timestamp requests and replies.
|
1553 | f0cbd3ec | bellard | * */
|
1554 | f0cbd3ec | bellard | /* if (ti->ti_flags & TH_SYN) {
|
1555 | f0cbd3ec | bellard | * tp->t_flags |= TF_RCVD_TSTMP;
|
1556 | f0cbd3ec | bellard | * tp->ts_recent = *ts_val;
|
1557 | f0cbd3ec | bellard | * tp->ts_recent_age = tcp_now;
|
1558 | f0cbd3ec | bellard | * }
|
1559 | f0cbd3ec | bellard | */ break; |
1560 | f0cbd3ec | bellard | } |
1561 | f0cbd3ec | bellard | } |
1562 | f0cbd3ec | bellard | } |
1563 | f0cbd3ec | bellard | |
1564 | f0cbd3ec | bellard | |
1565 | f0cbd3ec | bellard | /*
|
1566 | f0cbd3ec | bellard | * Pull out of band byte out of a segment so
|
1567 | f0cbd3ec | bellard | * it doesn't appear in the user's data queue.
|
1568 | f0cbd3ec | bellard | * It is still reflected in the segment length for
|
1569 | f0cbd3ec | bellard | * sequencing purposes.
|
1570 | f0cbd3ec | bellard | */
|
1571 | f0cbd3ec | bellard | |
1572 | f0cbd3ec | bellard | #ifdef notdef
|
1573 | f0cbd3ec | bellard | |
1574 | f0cbd3ec | bellard | void
|
1575 | f0cbd3ec | bellard | tcp_pulloutofband(so, ti, m) |
1576 | f0cbd3ec | bellard | struct socket *so;
|
1577 | f0cbd3ec | bellard | struct tcpiphdr *ti;
|
1578 | f0cbd3ec | bellard | register struct mbuf *m; |
1579 | f0cbd3ec | bellard | { |
1580 | f0cbd3ec | bellard | int cnt = ti->ti_urp - 1; |
1581 | f0cbd3ec | bellard | |
1582 | f0cbd3ec | bellard | while (cnt >= 0) { |
1583 | f0cbd3ec | bellard | if (m->m_len > cnt) {
|
1584 | f0cbd3ec | bellard | char *cp = mtod(m, caddr_t) + cnt;
|
1585 | f0cbd3ec | bellard | struct tcpcb *tp = sototcpcb(so);
|
1586 | f0cbd3ec | bellard | |
1587 | f0cbd3ec | bellard | tp->t_iobc = *cp; |
1588 | f0cbd3ec | bellard | tp->t_oobflags |= TCPOOB_HAVEDATA; |
1589 | f0cbd3ec | bellard | memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1)); |
1590 | f0cbd3ec | bellard | m->m_len--; |
1591 | f0cbd3ec | bellard | return;
|
1592 | f0cbd3ec | bellard | } |
1593 | f0cbd3ec | bellard | cnt -= m->m_len; |
1594 | f0cbd3ec | bellard | m = m->m_next; /* XXX WRONG! Fix it! */
|
1595 | f0cbd3ec | bellard | if (m == 0) |
1596 | f0cbd3ec | bellard | break;
|
1597 | f0cbd3ec | bellard | } |
1598 | f0cbd3ec | bellard | panic("tcp_pulloutofband");
|
1599 | f0cbd3ec | bellard | } |
1600 | f0cbd3ec | bellard | |
1601 | f0cbd3ec | bellard | #endif /* notdef */ |
1602 | f0cbd3ec | bellard | |
1603 | f0cbd3ec | bellard | /*
|
1604 | f0cbd3ec | bellard | * Collect new round-trip time estimate
|
1605 | f0cbd3ec | bellard | * and update averages and current timeout.
|
1606 | f0cbd3ec | bellard | */
|
1607 | f0cbd3ec | bellard | |
1608 | f0cbd3ec | bellard | void
|
1609 | f0cbd3ec | bellard | tcp_xmit_timer(tp, rtt) |
1610 | f0cbd3ec | bellard | register struct tcpcb *tp; |
1611 | f0cbd3ec | bellard | int rtt;
|
1612 | f0cbd3ec | bellard | { |
1613 | f0cbd3ec | bellard | register short delta; |
1614 | f0cbd3ec | bellard | |
1615 | f0cbd3ec | bellard | DEBUG_CALL("tcp_xmit_timer");
|
1616 | f0cbd3ec | bellard | DEBUG_ARG("tp = %lx", (long)tp); |
1617 | f0cbd3ec | bellard | DEBUG_ARG("rtt = %d", rtt);
|
1618 | f0cbd3ec | bellard | |
1619 | f0cbd3ec | bellard | tcpstat.tcps_rttupdated++; |
1620 | f0cbd3ec | bellard | if (tp->t_srtt != 0) { |
1621 | f0cbd3ec | bellard | /*
|
1622 | f0cbd3ec | bellard | * srtt is stored as fixed point with 3 bits after the
|
1623 | f0cbd3ec | bellard | * binary point (i.e., scaled by 8). The following magic
|
1624 | f0cbd3ec | bellard | * is equivalent to the smoothing algorithm in rfc793 with
|
1625 | f0cbd3ec | bellard | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
|
1626 | f0cbd3ec | bellard | * point). Adjust rtt to origin 0.
|
1627 | f0cbd3ec | bellard | */
|
1628 | f0cbd3ec | bellard | delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
|
1629 | f0cbd3ec | bellard | if ((tp->t_srtt += delta) <= 0) |
1630 | f0cbd3ec | bellard | tp->t_srtt = 1;
|
1631 | f0cbd3ec | bellard | /*
|
1632 | f0cbd3ec | bellard | * We accumulate a smoothed rtt variance (actually, a
|
1633 | f0cbd3ec | bellard | * smoothed mean difference), then set the retransmit
|
1634 | f0cbd3ec | bellard | * timer to smoothed rtt + 4 times the smoothed variance.
|
1635 | f0cbd3ec | bellard | * rttvar is stored as fixed point with 2 bits after the
|
1636 | f0cbd3ec | bellard | * binary point (scaled by 4). The following is
|
1637 | f0cbd3ec | bellard | * equivalent to rfc793 smoothing with an alpha of .75
|
1638 | f0cbd3ec | bellard | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
|
1639 | f0cbd3ec | bellard | * rfc793's wired-in beta.
|
1640 | f0cbd3ec | bellard | */
|
1641 | f0cbd3ec | bellard | if (delta < 0) |
1642 | f0cbd3ec | bellard | delta = -delta; |
1643 | f0cbd3ec | bellard | delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); |
1644 | f0cbd3ec | bellard | if ((tp->t_rttvar += delta) <= 0) |
1645 | f0cbd3ec | bellard | tp->t_rttvar = 1;
|
1646 | f0cbd3ec | bellard | } else {
|
1647 | f0cbd3ec | bellard | /*
|
1648 | f0cbd3ec | bellard | * No rtt measurement yet - use the unsmoothed rtt.
|
1649 | f0cbd3ec | bellard | * Set the variance to half the rtt (so our first
|
1650 | f0cbd3ec | bellard | * retransmit happens at 3*rtt).
|
1651 | f0cbd3ec | bellard | */
|
1652 | f0cbd3ec | bellard | tp->t_srtt = rtt << TCP_RTT_SHIFT; |
1653 | f0cbd3ec | bellard | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
|
1654 | f0cbd3ec | bellard | } |
1655 | f0cbd3ec | bellard | tp->t_rtt = 0;
|
1656 | f0cbd3ec | bellard | tp->t_rxtshift = 0;
|
1657 | f0cbd3ec | bellard | |
1658 | f0cbd3ec | bellard | /*
|
1659 | f0cbd3ec | bellard | * the retransmit should happen at rtt + 4 * rttvar.
|
1660 | f0cbd3ec | bellard | * Because of the way we do the smoothing, srtt and rttvar
|
1661 | f0cbd3ec | bellard | * will each average +1/2 tick of bias. When we compute
|
1662 | f0cbd3ec | bellard | * the retransmit timer, we want 1/2 tick of rounding and
|
1663 | f0cbd3ec | bellard | * 1 extra tick because of +-1/2 tick uncertainty in the
|
1664 | f0cbd3ec | bellard | * firing of the timer. The bias will give us exactly the
|
1665 | f0cbd3ec | bellard | * 1.5 tick we need. But, because the bias is
|
1666 | f0cbd3ec | bellard | * statistical, we have to test that we don't drop below
|
1667 | f0cbd3ec | bellard | * the minimum feasible timer (which is 2 ticks).
|
1668 | f0cbd3ec | bellard | */
|
1669 | f0cbd3ec | bellard | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), |
1670 | f0cbd3ec | bellard | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */ |
1671 | f0cbd3ec | bellard | |
1672 | f0cbd3ec | bellard | /*
|
1673 | f0cbd3ec | bellard | * We received an ack for a packet that wasn't retransmitted;
|
1674 | f0cbd3ec | bellard | * it is probably safe to discard any error indications we've
|
1675 | f0cbd3ec | bellard | * received recently. This isn't quite right, but close enough
|
1676 | f0cbd3ec | bellard | * for now (a route might have failed after we sent a segment,
|
1677 | f0cbd3ec | bellard | * and the return path might not be symmetrical).
|
1678 | f0cbd3ec | bellard | */
|
1679 | f0cbd3ec | bellard | tp->t_softerror = 0;
|
1680 | f0cbd3ec | bellard | } |
1681 | f0cbd3ec | bellard | |
1682 | f0cbd3ec | bellard | /*
|
1683 | f0cbd3ec | bellard | * Determine a reasonable value for maxseg size.
|
1684 | f0cbd3ec | bellard | * If the route is known, check route for mtu.
|
1685 | f0cbd3ec | bellard | * If none, use an mss that can be handled on the outgoing
|
1686 | f0cbd3ec | bellard | * interface without forcing IP to fragment; if bigger than
|
1687 | f0cbd3ec | bellard | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
|
1688 | f0cbd3ec | bellard | * to utilize large mbufs. If no route is found, route has no mtu,
|
1689 | f0cbd3ec | bellard | * or the destination isn't local, use a default, hopefully conservative
|
1690 | f0cbd3ec | bellard | * size (usually 512 or the default IP max size, but no more than the mtu
|
1691 | f0cbd3ec | bellard | * of the interface), as we can't discover anything about intervening
|
1692 | f0cbd3ec | bellard | * gateways or networks. We also initialize the congestion/slow start
|
1693 | f0cbd3ec | bellard | * window to be a single segment if the destination isn't local.
|
1694 | f0cbd3ec | bellard | * While looking at the routing entry, we also initialize other path-dependent
|
1695 | f0cbd3ec | bellard | * parameters from pre-set or cached values in the routing entry.
|
1696 | f0cbd3ec | bellard | */
|
1697 | f0cbd3ec | bellard | |
1698 | f0cbd3ec | bellard | int
|
1699 | f0cbd3ec | bellard | tcp_mss(tp, offer) |
1700 | f0cbd3ec | bellard | register struct tcpcb *tp; |
1701 | f0cbd3ec | bellard | u_int offer; |
1702 | f0cbd3ec | bellard | { |
1703 | f0cbd3ec | bellard | struct socket *so = tp->t_socket;
|
1704 | f0cbd3ec | bellard | int mss;
|
1705 | f0cbd3ec | bellard | |
1706 | f0cbd3ec | bellard | DEBUG_CALL("tcp_mss");
|
1707 | f0cbd3ec | bellard | DEBUG_ARG("tp = %lx", (long)tp); |
1708 | f0cbd3ec | bellard | DEBUG_ARG("offer = %d", offer);
|
1709 | f0cbd3ec | bellard | |
1710 | f0cbd3ec | bellard | mss = min(if_mtu, if_mru) - sizeof(struct tcpiphdr); |
1711 | f0cbd3ec | bellard | if (offer)
|
1712 | f0cbd3ec | bellard | mss = min(mss, offer); |
1713 | f0cbd3ec | bellard | mss = max(mss, 32);
|
1714 | f0cbd3ec | bellard | if (mss < tp->t_maxseg || offer != 0) |
1715 | f0cbd3ec | bellard | tp->t_maxseg = mss; |
1716 | f0cbd3ec | bellard | |
1717 | f0cbd3ec | bellard | tp->snd_cwnd = mss; |
1718 | f0cbd3ec | bellard | |
1719 | f0cbd3ec | bellard | sbreserve(&so->so_snd, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0));
|
1720 | f0cbd3ec | bellard | sbreserve(&so->so_rcv, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0));
|
1721 | f0cbd3ec | bellard | |
1722 | f0cbd3ec | bellard | DEBUG_MISC((dfd, " returning mss = %d\n", mss));
|
1723 | f0cbd3ec | bellard | |
1724 | f0cbd3ec | bellard | return mss;
|
1725 | f0cbd3ec | bellard | } |