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