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