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