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