- Get oskittcp to compile on msvc.
[reactos.git] / reactos / drivers / lib / oskittcp / oskittcp / tcp_input.c
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 * From: @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
34 */
35
36 #ifndef TUBA_INCLUDE
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/errno.h>
45 #include <sys/queue.h>
46 #include <sys/kernel.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_fsm.h>
58 #include <netinet/tcp_seq.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/tcpip.h>
62 #ifdef TCPDEBUG
63 #include <netinet/tcp_debug.h>
64 struct tcpiphdr tcp_saveti;
65 #endif
66
67 int tcprexmtthresh = 3;
68 tcp_seq tcp_iss;
69 tcp_cc tcp_ccgen;
70 struct tcpstat tcpstat;
71 u_long tcp_now;
72 struct inpcbhead tcb;
73 struct inpcbinfo tcbinfo;
74
75 #endif /* TUBA_INCLUDE */
76
77 /*
78 * Insert segment ti into reassembly queue of tcp with
79 * control block tp. Return TH_FIN if reassembly now includes
80 * a segment with FIN. The macro form does the common case inline
81 * (segment is the next to be received on an established connection,
82 * and the queue is empty), avoiding linkage into and removal
83 * from the queue and repetition of various conversions.
84 * Set DELACK for segments received in order, but ack immediately
85 * when segments are out of order (so fast retransmit can work).
86 */
87 #ifdef TCP_ACK_HACK
88 #define TCP_REASS(tp, ti, m, so, flags) { \
89 if ((ti)->ti_seq == (tp)->rcv_nxt && \
90 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
91 (tp)->t_state == TCPS_ESTABLISHED) { \
92 if (ti->ti_flags & TH_PUSH) \
93 tp->t_flags |= TF_ACKNOW; \
94 else \
95 tp->t_flags |= TF_DELACK; \
96 (tp)->rcv_nxt += (ti)->ti_len; \
97 flags = (ti)->ti_flags & TH_FIN; \
98 tcpstat.tcps_rcvpack++;\
99 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
100 sbappend(&(so)->so_rcv, (m)); \
101 sorwakeup(so); \
102 } else { \
103 (flags) = tcp_reass((tp), (ti), (m)); \
104 tp->t_flags |= TF_ACKNOW; \
105 } \
106 }
107 #else
108 #define TCP_REASS(tp, ti, m, so, flags) { \
109 if ((ti)->ti_seq == (tp)->rcv_nxt && \
110 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
111 (tp)->t_state == TCPS_ESTABLISHED) { \
112 tp->t_flags |= TF_DELACK; \
113 (tp)->rcv_nxt += (ti)->ti_len; \
114 flags = (ti)->ti_flags & TH_FIN; \
115 tcpstat.tcps_rcvpack++;\
116 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
117 sbappend(&(so)->so_rcv, (m)); \
118 sorwakeup(so); \
119 } else { \
120 (flags) = tcp_reass((tp), (ti), (m)); \
121 tp->t_flags |= TF_ACKNOW; \
122 } \
123 }
124 #endif
125 #ifndef TUBA_INCLUDE
126
127 int
128 tcp_reass(tp, ti, m)
129 register struct tcpcb *tp;
130 register struct tcpiphdr *ti;
131 struct mbuf *m;
132 {
133 register struct tcpiphdr *q;
134 struct socket *so = tp->t_inpcb->inp_socket;
135 int flags;
136
137 /*
138 * Call with ti==0 after become established to
139 * force pre-ESTABLISHED data up to user socket.
140 */
141 if (ti == 0)
142 goto present;
143
144 /*
145 * Find a segment which begins after this one does.
146 */
147 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
148 q = (struct tcpiphdr *)q->ti_next)
149 if (SEQ_GT(q->ti_seq, ti->ti_seq))
150 break;
151
152 /*
153 * If there is a preceding segment, it may provide some of
154 * our data already. If so, drop the data from the incoming
155 * segment. If it provides all of our data, drop us.
156 */
157 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
158 register int i;
159 q = (struct tcpiphdr *)q->ti_prev;
160 /* conversion to int (in i) handles seq wraparound */
161 i = q->ti_seq + q->ti_len - ti->ti_seq;
162 if (i > 0) {
163 if (i >= ti->ti_len) {
164 tcpstat.tcps_rcvduppack++;
165 tcpstat.tcps_rcvdupbyte += ti->ti_len;
166 m_freem(m);
167 /*
168 * Try to present any queued data
169 * at the left window edge to the user.
170 * This is needed after the 3-WHS
171 * completes.
172 */
173 goto present; /* ??? */
174 }
175 m_adj(m, i);
176 ti->ti_len -= i;
177 ti->ti_seq += i;
178 }
179 q = (struct tcpiphdr *)(q->ti_next);
180 }
181 tcpstat.tcps_rcvoopack++;
182 tcpstat.tcps_rcvoobyte += ti->ti_len;
183 REASS_MBUF(ti) = m; /* XXX */
184
185 /*
186 * While we overlap succeeding segments trim them or,
187 * if they are completely covered, dequeue them.
188 */
189 while (q != (struct tcpiphdr *)tp) {
190 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
191 if (i <= 0)
192 break;
193 if (i < q->ti_len) {
194 q->ti_seq += i;
195 q->ti_len -= i;
196 m_adj(REASS_MBUF(q), i);
197 break;
198 }
199 q = (struct tcpiphdr *)q->ti_next;
200 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
201 remque(q->ti_prev);
202 m_freem(m);
203 }
204
205 /*
206 * Stick new segment in its place.
207 */
208 insque(ti, q->ti_prev);
209
210 present:
211 /*
212 * Present data to user, advancing rcv_nxt through
213 * completed sequence space.
214 */
215 if (!TCPS_HAVEESTABLISHED(tp->t_state))
216 return (0);
217 ti = tp->seg_next;
218 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
219 return (0);
220 do {
221 tp->rcv_nxt += ti->ti_len;
222 OS_DbgPrint(OSK_MID_TRACE,("Added %d to rcv_nxt (result %d)\n",
223 ti->ti_len, tp->rcv_nxt));
224 flags = ti->ti_flags & TH_FIN;
225 remque(ti);
226 m = REASS_MBUF(ti);
227 ti = (struct tcpiphdr *)ti->ti_next;
228 if (so->so_state & SS_CANTRCVMORE)
229 m_freem(m);
230 else
231 sbappend(&so->so_rcv, m);
232 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
233 sorwakeup(so);
234 return (flags);
235 }
236
237 /*
238 * TCP input routine, follows pages 65-76 of the
239 * protocol specification dated September, 1981 very closely.
240 */
241 void
242 tcp_input(m, iphlen)
243 register struct mbuf *m;
244 int iphlen;
245 {
246 register struct tcpiphdr *ti;
247 register struct inpcb *inp;
248 caddr_t optp = NULL;
249 int optlen = 0;
250 int len, tlen, off;
251 register struct tcpcb *tp = 0;
252 register int tiflags;
253 struct socket *so = 0;
254 int todrop, acked, ourfinisacked, needoutput = 0;
255 struct in_addr laddr;
256 int dropsocket = 0;
257 int iss = 0;
258 u_long tiwin;
259 struct tcpopt to; /* options in this segment */
260 struct rmxp_tao *taop; /* pointer to our TAO cache entry */
261 struct rmxp_tao tao_noncached; /* in case there's no cached entry */
262 #ifdef TCPDEBUG
263 short ostate = 0;
264 #endif
265 bzero((char *)&to, sizeof(to));
266
267 tcpstat.tcps_rcvtotal++;
268 /*
269 * Get IP and TCP header together in first mbuf.
270 * Note: IP leaves IP header in first mbuf.
271 */
272 ti = mtod(m, struct tcpiphdr *);
273 if (iphlen > sizeof (struct ip))
274 ip_stripoptions(m, (struct mbuf *)0);
275 if (m->m_len < sizeof (struct tcpiphdr)) {
276 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
277 tcpstat.tcps_rcvshort++;
278 return;
279 }
280 ti = mtod(m, struct tcpiphdr *);
281 }
282
283 /*
284 * Checksum extended TCP header and data.
285 */
286 tlen = ((struct ip *)ti)->ip_len;
287 len = sizeof (struct ip) + tlen;
288 ti->ti_next = ti->ti_prev = 0;
289 ti->ti_x1 = 0;
290 ti->ti_len = (u_short)tlen;
291 HTONS(ti->ti_len);
292 #ifndef __REACTOS__ /* Checksum already done in IPReceive */
293 ti->ti_sum = in_cksum(m, len);
294 if (ti->ti_sum) {
295 printf("TCP: Bad Checksum\n");
296 tcpstat.tcps_rcvbadsum++;
297 goto drop;
298 }
299 #endif
300 #endif /* TUBA_INCLUDE */
301
302 /*
303 * Check that TCP offset makes sense,
304 * pull out TCP options and adjust length. XXX
305 */
306 off = ti->ti_off << 2;
307 if (off < sizeof (struct tcphdr) || off > tlen) {
308 tcpstat.tcps_rcvbadoff++;
309 goto drop;
310 }
311 tlen -= off;
312 ti->ti_len = tlen;
313 if (off > sizeof (struct tcphdr)) {
314 if (m->m_len < sizeof(struct ip) + off) {
315 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
316 tcpstat.tcps_rcvshort++;
317 return;
318 }
319 ti = mtod(m, struct tcpiphdr *);
320 }
321 optlen = off - sizeof (struct tcphdr);
322 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
323 }
324 tiflags = ti->ti_flags;
325
326 /*
327 * Convert TCP protocol specific fields to host format.
328 */
329 NTOHL(ti->ti_seq);
330 NTOHL(ti->ti_ack);
331 NTOHS(ti->ti_win);
332 NTOHS(ti->ti_urp);
333
334 /*
335 * Drop TCP, IP headers and TCP options.
336 */
337 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
338 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
339
340 /*
341 * Locate pcb for segment.
342 */
343 findpcb:
344 /*
345 * First look for an exact match.
346 */
347 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport,
348 ti->ti_dst, ti->ti_dport);
349 /*
350 * ...and if that fails, do a wildcard search.
351 */
352 if (inp == NULL) {
353 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
354 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
355 }
356
357 /*
358 * If the state is CLOSED (i.e., TCB does not exist) then
359 * all data in the incoming segment is discarded.
360 * If the TCB exists but is in CLOSED state, it is embryonic,
361 * but should either do a listen or a connect soon.
362 */
363 if (inp == NULL)
364 goto dropwithreset;
365 tp = intotcpcb(inp);
366 if (tp == 0)
367 goto dropwithreset;
368 if (tp->t_state == TCPS_CLOSED)
369 goto drop;
370
371 /* Unscale the window into a 32-bit value. */
372 if ((tiflags & TH_SYN) == 0)
373 tiwin = ti->ti_win << tp->snd_scale;
374 else
375 tiwin = ti->ti_win;
376
377 so = inp->inp_socket;
378 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
379 #ifdef TCPDEBUG
380 if (so->so_options & SO_DEBUG) {
381 ostate = tp->t_state;
382 tcp_saveti = *ti;
383 }
384 #endif
385 if (so->so_options & SO_ACCEPTCONN) {
386 register struct tcpcb *tp0 = tp;
387 struct socket *so2;
388 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
389 /*
390 * Note: dropwithreset makes sure we don't
391 * send a RST in response to a RST.
392 */
393 if (tiflags & TH_ACK) {
394 tcpstat.tcps_badsyn++;
395 goto dropwithreset;
396 }
397 goto drop;
398 }
399 so2 = sonewconn(so, 0);
400 if (so2 == 0) {
401 unsigned int i, j, qlen;
402
403 static int rnd;
404 static long old_mono_secs;
405 static unsigned int cur_cnt, old_cnt;
406
407 tcpstat.tcps_listendrop++;
408
409 /*
410 * Keep a decaying average of the number
411 * of overruns we've been getting.
412 */
413 if ((i = (mono_time.tv_sec -
414 old_mono_secs)) != 0) {
415 old_mono_secs = mono_time.tv_sec;
416 old_cnt = cur_cnt / i;
417 cur_cnt = 0;
418 }
419
420 so2 = so->so_q0;
421 if (so2 == 0)
422 goto drop;
423
424 /*
425 * If we've been getting a lot of hits,
426 * random drop an incomplete connection
427 * from the queue, otherwise, fall through
428 * so we head-drop from the queue.
429 */
430 qlen = so->so_q0len;
431 if (++cur_cnt > qlen || old_cnt > qlen) {
432 rnd = (314159 * rnd + 66329) & 0xffff;
433 j = ((qlen + 1) * rnd) >> 16;
434
435 while (j-- && so2)
436 so2 = so2->so_q0;
437 }
438 if (so2) {
439 tcp_drop(sototcpcb(so2), ETIMEDOUT);
440 so2 = sonewconn(so, 0);
441 }
442 if (!so2)
443 goto drop;
444 }
445 so = so2;
446 /*
447 * This is ugly, but ....
448 *
449 * Mark socket as temporary until we're
450 * committed to keeping it. The code at
451 * ``drop'' and ``dropwithreset'' check the
452 * flag dropsocket to see if the temporary
453 * socket created here should be discarded.
454 * We mark the socket as discardable until
455 * we're committed to it below in TCPS_LISTEN.
456 */
457 dropsocket++;
458 inp = (struct inpcb *)so->so_pcb;
459 inp->inp_laddr = ti->ti_dst;
460 inp->inp_lport = ti->ti_dport;
461 in_pcbrehash(inp);
462 #if BSD>=43
463 inp->inp_options = ip_srcroute();
464 #endif
465 tp = intotcpcb(inp);
466 tp->t_state = TCPS_LISTEN;
467 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
468
469 /* Compute proper scaling value from buffer space */
470 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
471 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
472 tp->request_r_scale++;
473 }
474 }
475
476 /*
477 * Segment received on connection.
478 * Reset idle time and keep-alive timer.
479 */
480 tp->t_idle = 0;
481 if (TCPS_HAVEESTABLISHED(tp->t_state))
482 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
483
484 /*
485 * Process options if not in LISTEN state,
486 * else do it below (after getting remote address).
487 */
488 if (tp->t_state != TCPS_LISTEN)
489 tcp_dooptions(tp, optp, optlen, ti, &to);
490
491 /*
492 * Header prediction: check for the two common cases
493 * of a uni-directional data xfer. If the packet has
494 * no control flags, is in-sequence, the window didn't
495 * change and we're not retransmitting, it's a
496 * candidate. If the length is zero and the ack moved
497 * forward, we're the sender side of the xfer. Just
498 * free the data acked & wake any higher level process
499 * that was blocked waiting for space. If the length
500 * is non-zero and the ack didn't move, we're the
501 * receiver side. If we're getting packets in-order
502 * (the reassembly queue is empty), add the data to
503 * the socket buffer and note that we need a delayed ack.
504 * Make sure that the hidden state-flags are also off.
505 * Since we check for TCPS_ESTABLISHED above, it can only
506 * be TH_NEEDSYN.
507 */
508 if (tp->t_state == TCPS_ESTABLISHED &&
509 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
510 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
511 ((to.to_flag & TOF_TS) == 0 ||
512 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
513 /*
514 * Using the CC option is compulsory if once started:
515 * the segment is OK if no T/TCP was negotiated or
516 * if the segment has a CC option equal to CCrecv
517 */
518 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
519 ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
520 ti->ti_seq == tp->rcv_nxt &&
521 tiwin && tiwin == tp->snd_wnd &&
522 tp->snd_nxt == tp->snd_max) {
523
524 /*
525 * If last ACK falls within this segment's sequence numbers,
526 * record the timestamp.
527 * NOTE that the test is modified according to the latest
528 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
529 */
530 if ((to.to_flag & TOF_TS) != 0 &&
531 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
532 tp->ts_recent_age = tcp_now;
533 tp->ts_recent = to.to_tsval;
534 }
535
536 if (ti->ti_len == 0) {
537 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
538 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
539 tp->snd_cwnd >= tp->snd_wnd) {
540 /*
541 * this is a pure ack for outstanding data.
542 */
543 ++tcpstat.tcps_predack;
544 if ((to.to_flag & TOF_TS) != 0)
545 tcp_xmit_timer(tp,
546 tcp_now - to.to_tsecr + 1);
547 else if (tp->t_rtt &&
548 SEQ_GT(ti->ti_ack, tp->t_rtseq))
549 tcp_xmit_timer(tp, tp->t_rtt);
550 acked = ti->ti_ack - tp->snd_una;
551 tcpstat.tcps_rcvackpack++;
552 tcpstat.tcps_rcvackbyte += acked;
553 sbdrop(&so->so_snd, acked);
554 tp->snd_una = ti->ti_ack;
555 m_freem(m);
556
557 /*
558 * If all outstanding data are acked, stop
559 * retransmit timer, otherwise restart timer
560 * using current (possibly backed-off) value.
561 * If process is waiting for space,
562 * wakeup/selwakeup/signal. If data
563 * are ready to send, let tcp_output
564 * decide between more output or persist.
565 */
566 if (tp->snd_una == tp->snd_max)
567 tp->t_timer[TCPT_REXMT] = 0;
568 else if (tp->t_timer[TCPT_PERSIST] == 0)
569 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
570
571 if (so->so_snd.sb_flags & SB_NOTIFY)
572 sowwakeup(so);
573 if (so->so_snd.sb_cc)
574 (void) tcp_output(tp);
575 return;
576 }
577 } else if (ti->ti_ack == tp->snd_una &&
578 tp->seg_next == (struct tcpiphdr *)tp &&
579 ti->ti_len <= sbspace(&so->so_rcv)) {
580 /*
581 * this is a pure, in-sequence data packet
582 * with nothing on the reassembly queue and
583 * we have enough buffer space to take it.
584 */
585 ++tcpstat.tcps_preddat;
586 tp->rcv_nxt += ti->ti_len;
587 OS_DbgPrint(OSK_MID_TRACE,("Added %d to rcv_nxt\n", ti->ti_len - sizeof(struct ip)));
588 tcpstat.tcps_rcvpack++;
589 tcpstat.tcps_rcvbyte += ti->ti_len;
590 /*
591 * Add data to socket buffer.
592 */
593 sbappend(&so->so_rcv, m);
594 sorwakeup(so);
595 #ifdef TCP_ACK_HACK
596 /*
597 * If this is a short packet, then ACK now - with Nagel
598 * congestion avoidance sender won't send more until
599 * he gets an ACK.
600 */
601 if (tiflags & TH_PUSH) {
602 tp->t_flags |= TF_ACKNOW;
603 tcp_output(tp);
604 } else {
605 tp->t_flags |= TF_DELACK;
606 }
607 #else
608 tp->t_flags |= TF_DELACK;
609 #endif
610 return;
611 }
612 }
613
614 /*
615 * Calculate amount of space in receive window,
616 * and then do TCP input processing.
617 * Receive window is amount of space in rcv queue,
618 * but not less than advertised window.
619 */
620 { int win;
621
622 win = sbspace(&so->so_rcv);
623 if (win < 0)
624 win = 0;
625 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
626 }
627
628 switch (tp->t_state) {
629
630 /*
631 * If the state is LISTEN then ignore segment if it contains an RST.
632 * If the segment contains an ACK then it is bad and send a RST.
633 * If it does not contain a SYN then it is not interesting; drop it.
634 * Don't bother responding if the destination was a broadcast.
635 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
636 * tp->iss, and send a segment:
637 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
638 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
639 * Fill in remote peer address fields if not previously specified.
640 * Enter SYN_RECEIVED state, and process any other fields of this
641 * segment in this state.
642 */
643 case TCPS_LISTEN: {
644 struct mbuf *am;
645 register struct sockaddr_in *sin;
646
647 if (tiflags & TH_RST)
648 goto drop;
649 if (tiflags & TH_ACK)
650 goto dropwithreset;
651 if ((tiflags & TH_SYN) == 0)
652 goto drop;
653 /*
654 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
655 * in_broadcast() should never return true on a received
656 * packet with M_BCAST not set.
657 */
658 if (m->m_flags & (M_BCAST|M_MCAST) ||
659 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
660 goto drop;
661 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
662 if (am == NULL)
663 goto drop;
664 am->m_len = sizeof (struct sockaddr_in);
665 sin = mtod(am, struct sockaddr_in *);
666 sin->sin_family = AF_INET;
667 sin->sin_len = sizeof(*sin);
668 sin->sin_addr = ti->ti_src;
669 sin->sin_port = ti->ti_sport;
670 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
671 laddr = inp->inp_laddr;
672 if (inp->inp_laddr.s_addr == INADDR_ANY)
673 inp->inp_laddr = ti->ti_dst;
674 if (in_pcbconnect(inp, am)) {
675 inp->inp_laddr = laddr;
676 (void) m_free(am);
677 goto drop;
678 }
679 (void) m_free(am);
680 tp->t_template = tcp_template(tp);
681 if (tp->t_template == 0) {
682 tp = tcp_drop(tp, ENOBUFS);
683 dropsocket = 0; /* socket is already gone */
684 goto drop;
685 }
686 if ((taop = tcp_gettaocache(inp)) == NULL) {
687 taop = &tao_noncached;
688 bzero(taop, sizeof(*taop));
689 }
690 tcp_dooptions(tp, optp, optlen, ti, &to);
691 if (iss)
692 tp->iss = iss;
693 else
694 tp->iss = tcp_iss;
695 tcp_iss += TCP_ISSINCR/2;
696 tp->irs = ti->ti_seq;
697 tcp_sendseqinit(tp);
698 tcp_rcvseqinit(tp);
699 /*
700 * Initialization of the tcpcb for transaction;
701 * set SND.WND = SEG.WND,
702 * initialize CCsend and CCrecv.
703 */
704 tp->snd_wnd = tiwin; /* initial send-window */
705 tp->cc_send = CC_INC(tcp_ccgen);
706 tp->cc_recv = to.to_cc;
707 /*
708 * Perform TAO test on incoming CC (SEG.CC) option, if any.
709 * - compare SEG.CC against cached CC from the same host,
710 * if any.
711 * - if SEG.CC > chached value, SYN must be new and is accepted
712 * immediately: save new CC in the cache, mark the socket
713 * connected, enter ESTABLISHED state, turn on flag to
714 * send a SYN in the next segment.
715 * A virtual advertised window is set in rcv_adv to
716 * initialize SWS prevention. Then enter normal segment
717 * processing: drop SYN, process data and FIN.
718 * - otherwise do a normal 3-way handshake.
719 */
720 if ((to.to_flag & TOF_CC) != 0) {
721 if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
722 taop->tao_cc = to.to_cc;
723 tp->t_state = TCPS_ESTABLISHED;
724
725 /*
726 * If there is a FIN, or if there is data and the
727 * connection is local, then delay SYN,ACK(SYN) in
728 * the hope of piggy-backing it on a response
729 * segment. Otherwise must send ACK now in case
730 * the other side is slow starting.
731 */
732 if ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
733 in_localaddr(inp->inp_faddr)))
734 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
735 else
736 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
737
738 /*
739 * Limit the `virtual advertised window' to TCP_MAXWIN
740 * here. Even if we requested window scaling, it will
741 * become effective only later when our SYN is acked.
742 */
743 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
744 tcpstat.tcps_connects++;
745 soisconnected(so);
746 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
747 dropsocket = 0; /* committed to socket */
748 tcpstat.tcps_accepts++;
749 goto trimthenstep6;
750 }
751 /* else do standard 3-way handshake */
752 } else {
753 /*
754 * No CC option, but maybe CC.NEW:
755 * invalidate cached value.
756 */
757 taop->tao_cc = 0;
758 }
759 /*
760 * TAO test failed or there was no CC option,
761 * do a standard 3-way handshake.
762 */
763 tp->t_flags |= TF_ACKNOW;
764 tp->t_state = TCPS_SYN_RECEIVED;
765 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
766 dropsocket = 0; /* committed to socket */
767 tcpstat.tcps_accepts++;
768 goto trimthenstep6;
769 }
770
771 /*
772 * If the state is SYN_RECEIVED:
773 * do just the ack and RST checks from SYN_SENT state.
774 * If the state is SYN_SENT:
775 * if seg contains an ACK, but not for our SYN, drop the input.
776 * if seg contains a RST, then drop the connection.
777 * if seg does not contain SYN, then drop it.
778 * Otherwise this is an acceptable SYN segment
779 * initialize tp->rcv_nxt and tp->irs
780 * if seg contains ack then advance tp->snd_una
781 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
782 * arrange for segment to be acked (eventually)
783 * continue processing rest of data/controls, beginning with URG
784 */
785 case TCPS_SYN_RECEIVED:
786 case TCPS_SYN_SENT:
787 if ((taop = tcp_gettaocache(inp)) == NULL) {
788 taop = &tao_noncached;
789 bzero(taop, sizeof(*taop));
790 }
791
792 if ((tiflags & TH_ACK) &&
793 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
794 SEQ_GT(ti->ti_ack, tp->snd_max))) {
795 /*
796 * If we have a cached CCsent for the remote host,
797 * hence we haven't just crashed and restarted,
798 * do not send a RST. This may be a retransmission
799 * from the other side after our earlier ACK was lost.
800 * Our new SYN, when it arrives, will serve as the
801 * needed ACK.
802 */
803 if (taop->tao_ccsent != 0)
804 goto drop;
805 else
806 goto dropwithreset;
807 }
808 if (tiflags & TH_RST) {
809 if (tiflags & TH_ACK)
810 tp = tcp_drop(tp, ECONNREFUSED);
811 goto drop;
812 }
813 if (tp->t_state == TCPS_SYN_RECEIVED)
814 break;
815 if ((tiflags & TH_SYN) == 0)
816 goto drop;
817 tp->snd_wnd = ti->ti_win; /* initial send window */
818 tp->cc_recv = to.to_cc; /* foreign CC */
819
820 tp->irs = ti->ti_seq;
821 tcp_rcvseqinit(tp);
822 if (tiflags & TH_ACK) {
823 /*
824 * Our SYN was acked. If segment contains CC.ECHO
825 * option, check it to make sure this segment really
826 * matches our SYN. If not, just drop it as old
827 * duplicate, but send an RST if we're still playing
828 * by the old rules.
829 */
830 if ((to.to_flag & TOF_CCECHO) &&
831 tp->cc_send != to.to_ccecho) {
832 if (taop->tao_ccsent != 0)
833 goto drop;
834 else
835 goto dropwithreset;
836 }
837 tcpstat.tcps_connects++;
838 soisconnected(so);
839 /* Do window scaling on this connection? */
840 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
841 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
842 tp->snd_scale = tp->requested_s_scale;
843 tp->rcv_scale = tp->request_r_scale;
844 }
845 /* Segment is acceptable, update cache if undefined. */
846 if (taop->tao_ccsent == 0)
847 taop->tao_ccsent = to.to_ccecho;
848
849 tp->rcv_adv += tp->rcv_wnd;
850 tp->snd_una++; /* SYN is acked */
851 /*
852 * If there's data, delay ACK; if there's also a FIN
853 * ACKNOW will be turned on later.
854 */
855 if (ti->ti_len != 0)
856 tp->t_flags |= TF_DELACK;
857 else
858 tp->t_flags |= TF_ACKNOW;
859 /*
860 * Received <SYN,ACK> in SYN_SENT[*] state.
861 * Transitions:
862 * SYN_SENT --> ESTABLISHED
863 * SYN_SENT* --> FIN_WAIT_1
864 */
865 if (tp->t_flags & TF_NEEDFIN) {
866 tp->t_state = TCPS_FIN_WAIT_1;
867 tp->t_flags &= ~TF_NEEDFIN;
868 tiflags &= ~TH_SYN;
869 } else {
870 tp->t_state = TCPS_ESTABLISHED;
871 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
872 }
873 } else {
874 /*
875 * Received initial SYN in SYN-SENT[*] state => simul-
876 * taneous open. If segment contains CC option and there is
877 * a cached CC, apply TAO test; if it succeeds, connection is
878 * half-synchronized. Otherwise, do 3-way handshake:
879 * SYN-SENT -> SYN-RECEIVED
880 * SYN-SENT* -> SYN-RECEIVED*
881 * If there was no CC option, clear cached CC value.
882 */
883 tp->t_flags |= TF_ACKNOW;
884 tp->t_timer[TCPT_REXMT] = 0;
885 if (to.to_flag & TOF_CC) {
886 if (taop->tao_cc != 0 &&
887 CC_GT(to.to_cc, taop->tao_cc)) {
888 /*
889 * update cache and make transition:
890 * SYN-SENT -> ESTABLISHED*
891 * SYN-SENT* -> FIN-WAIT-1*
892 */
893 taop->tao_cc = to.to_cc;
894 if (tp->t_flags & TF_NEEDFIN) {
895 tp->t_state = TCPS_FIN_WAIT_1;
896 tp->t_flags &= ~TF_NEEDFIN;
897 } else {
898 tp->t_state = TCPS_ESTABLISHED;
899 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
900 }
901 tp->t_flags |= TF_NEEDSYN;
902 } else
903 tp->t_state = TCPS_SYN_RECEIVED;
904 } else {
905 /* CC.NEW or no option => invalidate cache */
906 taop->tao_cc = 0;
907 tp->t_state = TCPS_SYN_RECEIVED;
908 }
909 }
910
911 trimthenstep6:
912 /*
913 * Advance ti->ti_seq to correspond to first data byte.
914 * If data, trim to stay within window,
915 * dropping FIN if necessary.
916 */
917 ti->ti_seq++;
918 if (ti->ti_len > tp->rcv_wnd) {
919 todrop = ti->ti_len - tp->rcv_wnd;
920 m_adj(m, -todrop);
921 ti->ti_len = tp->rcv_wnd;
922 tiflags &= ~TH_FIN;
923 tcpstat.tcps_rcvpackafterwin++;
924 tcpstat.tcps_rcvbyteafterwin += todrop;
925 }
926 tp->snd_wl1 = ti->ti_seq - 1;
927 tp->rcv_up = ti->ti_seq;
928 /*
929 * Client side of transaction: already sent SYN and data.
930 * If the remote host used T/TCP to validate the SYN,
931 * our data will be ACK'd; if so, enter normal data segment
932 * processing in the middle of step 5, ack processing.
933 * Otherwise, goto step 6.
934 */
935 if (tiflags & TH_ACK)
936 goto process_ACK;
937 goto step6;
938 /*
939 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
940 * if segment contains a SYN and CC [not CC.NEW] option:
941 * if state == TIME_WAIT and connection duration > MSL,
942 * drop packet and send RST;
943 *
944 * if SEG.CC > CCrecv then is new SYN, and can implicitly
945 * ack the FIN (and data) in retransmission queue.
946 * Complete close and delete TCPCB. Then reprocess
947 * segment, hoping to find new TCPCB in LISTEN state;
948 *
949 * else must be old SYN; drop it.
950 * else do normal processing.
951 */
952 case TCPS_LAST_ACK:
953 case TCPS_CLOSING:
954 case TCPS_TIME_WAIT:
955 if ((tiflags & TH_SYN) &&
956 (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
957 if (tp->t_state == TCPS_TIME_WAIT &&
958 tp->t_duration > TCPTV_MSL)
959 goto dropwithreset;
960 if (CC_GT(to.to_cc, tp->cc_recv)) {
961 tp = tcp_close(tp);
962 goto findpcb;
963 }
964 else
965 goto drop;
966 }
967 break; /* continue normal processing */
968 }
969
970 /*
971 * States other than LISTEN or SYN_SENT.
972 * First check timestamp, if present.
973 * Then check the connection count, if present.
974 * Then check that at least some bytes of segment are within
975 * receive window. If segment begins before rcv_nxt,
976 * drop leading data (and SYN); if nothing left, just ack.
977 *
978 * RFC 1323 PAWS: If we have a timestamp reply on this segment
979 * and it's less than ts_recent, drop it.
980 */
981 if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 &&
982 tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) {
983
984 /* Check to see if ts_recent is over 24 days old. */
985 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
986 /*
987 * Invalidate ts_recent. If this segment updates
988 * ts_recent, the age will be reset later and ts_recent
989 * will get a valid value. If it does not, setting
990 * ts_recent to zero will at least satisfy the
991 * requirement that zero be placed in the timestamp
992 * echo reply when ts_recent isn't valid. The
993 * age isn't reset until we get a valid ts_recent
994 * because we don't want out-of-order segments to be
995 * dropped when ts_recent is old.
996 */
997 tp->ts_recent = 0;
998 } else {
999 tcpstat.tcps_rcvduppack++;
1000 tcpstat.tcps_rcvdupbyte += ti->ti_len;
1001 tcpstat.tcps_pawsdrop++;
1002 goto dropafterack;
1003 }
1004 }
1005
1006 /*
1007 * T/TCP mechanism
1008 * If T/TCP was negotiated and the segment doesn't have CC,
1009 * or if it's CC is wrong then drop the segment.
1010 * RST segments do not have to comply with this.
1011 */
1012 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1013 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) &&
1014 (tiflags & TH_RST) == 0)
1015 goto dropafterack;
1016
1017 todrop = tp->rcv_nxt - ti->ti_seq;
1018 if (todrop > 0) {
1019 if (tiflags & TH_SYN) {
1020 tiflags &= ~TH_SYN;
1021 ti->ti_seq++;
1022 if (ti->ti_urp > 1)
1023 ti->ti_urp--;
1024 else
1025 tiflags &= ~TH_URG;
1026 todrop--;
1027 }
1028 /*
1029 * Following if statement from Stevens, vol. 2, p. 960.
1030 */
1031 if (todrop > ti->ti_len
1032 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
1033 /*
1034 * Any valid FIN must be to the left of the window.
1035 * At this point the FIN must be a duplicate or out
1036 * of sequence; drop it.
1037 */
1038 tiflags &= ~TH_FIN;
1039
1040 /*
1041 * Send an ACK to resynchronize and drop any data.
1042 * But keep on processing for RST or ACK.
1043 */
1044 tp->t_flags |= TF_ACKNOW;
1045 todrop = ti->ti_len;
1046 tcpstat.tcps_rcvduppack++;
1047 tcpstat.tcps_rcvdupbyte += todrop;
1048 } else {
1049 tcpstat.tcps_rcvpartduppack++;
1050 tcpstat.tcps_rcvpartdupbyte += todrop;
1051 }
1052 m_adj(m, todrop);
1053 ti->ti_seq += todrop;
1054 ti->ti_len -= todrop;
1055 if (ti->ti_urp > todrop)
1056 ti->ti_urp -= todrop;
1057 else {
1058 tiflags &= ~TH_URG;
1059 ti->ti_urp = 0;
1060 }
1061 }
1062
1063 /*
1064 * If new data are received on a connection after the
1065 * user processes are gone, then RST the other end.
1066 */
1067 if ((so->so_state & SS_NOFDREF) &&
1068 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1069 tp = tcp_close(tp);
1070 tcpstat.tcps_rcvafterclose++;
1071 goto dropwithreset;
1072 }
1073
1074 /*
1075 * If segment ends after window, drop trailing data
1076 * (and PUSH and FIN); if nothing left, just ACK.
1077 */
1078 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1079 if (todrop > 0) {
1080 tcpstat.tcps_rcvpackafterwin++;
1081 if (todrop >= ti->ti_len) {
1082 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1083 /*
1084 * If a new connection request is received
1085 * while in TIME_WAIT, drop the old connection
1086 * and start over if the sequence numbers
1087 * are above the previous ones.
1088 */
1089 if (tiflags & TH_SYN &&
1090 tp->t_state == TCPS_TIME_WAIT &&
1091 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1092 iss = tp->rcv_nxt + TCP_ISSINCR;
1093 tp = tcp_close(tp);
1094 goto findpcb;
1095 }
1096 /*
1097 * If window is closed can only take segments at
1098 * window edge, and have to drop data and PUSH from
1099 * incoming segments. Continue processing, but
1100 * remember to ack. Otherwise, drop segment
1101 * and ack.
1102 */
1103 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1104 tp->t_flags |= TF_ACKNOW;
1105 tcpstat.tcps_rcvwinprobe++;
1106 } else
1107 goto dropafterack;
1108 } else
1109 tcpstat.tcps_rcvbyteafterwin += todrop;
1110 m_adj(m, -todrop);
1111 ti->ti_len -= todrop;
1112 tiflags &= ~(TH_PUSH|TH_FIN);
1113 }
1114
1115 /*
1116 * If last ACK falls within this segment's sequence numbers,
1117 * record its timestamp.
1118 * NOTE that the test is modified according to the latest
1119 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1120 */
1121 if ((to.to_flag & TOF_TS) != 0 &&
1122 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1123 tp->ts_recent_age = tcp_now;
1124 tp->ts_recent = to.to_tsval;
1125 }
1126
1127 /*
1128 * If the RST bit is set examine the state:
1129 * SYN_RECEIVED STATE:
1130 * If passive open, return to LISTEN state.
1131 * If active open, inform user that connection was refused.
1132 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1133 * Inform user that connection was reset, and close tcb.
1134 * CLOSING, LAST_ACK, TIME_WAIT STATES
1135 * Close the tcb.
1136 */
1137 if (tiflags&TH_RST) switch (tp->t_state) {
1138
1139 case TCPS_SYN_RECEIVED:
1140 so->so_error = ECONNREFUSED;
1141 goto close;
1142
1143 case TCPS_ESTABLISHED:
1144 case TCPS_FIN_WAIT_1:
1145 case TCPS_FIN_WAIT_2:
1146 case TCPS_CLOSE_WAIT:
1147 so->so_error = ECONNRESET;
1148 close:
1149 tp->t_state = TCPS_CLOSED;
1150 tcpstat.tcps_drops++;
1151 tp = tcp_close(tp);
1152 goto drop;
1153
1154 case TCPS_CLOSING:
1155 case TCPS_LAST_ACK:
1156 case TCPS_TIME_WAIT:
1157 tp = tcp_close(tp);
1158 goto drop;
1159 }
1160
1161 /*
1162 * If a SYN is in the window, then this is an
1163 * error and we send an RST and drop the connection.
1164 */
1165 if (tiflags & TH_SYN) {
1166 tp = tcp_drop(tp, ECONNRESET);
1167 goto dropwithreset;
1168 }
1169
1170 /*
1171 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1172 * flag is on (half-synchronized state), then queue data for
1173 * later processing; else drop segment and return.
1174 */
1175 if ((tiflags & TH_ACK) == 0) {
1176 if (tp->t_state == TCPS_SYN_RECEIVED ||
1177 (tp->t_flags & TF_NEEDSYN))
1178 goto step6;
1179 else
1180 goto drop;
1181 }
1182
1183 /*
1184 * Ack processing.
1185 */
1186 switch (tp->t_state) {
1187
1188 /*
1189 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1190 * ESTABLISHED state and continue processing, otherwise
1191 * send an RST.
1192 */
1193 case TCPS_SYN_RECEIVED:
1194 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1195 SEQ_GT(ti->ti_ack, tp->snd_max))
1196 goto dropwithreset;
1197
1198 tcpstat.tcps_connects++;
1199 soisconnected(so);
1200 /* Do window scaling? */
1201 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1202 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1203 tp->snd_scale = tp->requested_s_scale;
1204 tp->rcv_scale = tp->request_r_scale;
1205 }
1206 /*
1207 * Upon successful completion of 3-way handshake,
1208 * update cache.CC if it was undefined, pass any queued
1209 * data to the user, and advance state appropriately.
1210 */
1211 if ((taop = tcp_gettaocache(inp)) != NULL &&
1212 taop->tao_cc == 0)
1213 taop->tao_cc = tp->cc_recv;
1214
1215 /*
1216 * Make transitions:
1217 * SYN-RECEIVED -> ESTABLISHED
1218 * SYN-RECEIVED* -> FIN-WAIT-1
1219 */
1220 if (tp->t_flags & TF_NEEDFIN) {
1221 tp->t_state = TCPS_FIN_WAIT_1;
1222 tp->t_flags &= ~TF_NEEDFIN;
1223 } else {
1224 tp->t_state = TCPS_ESTABLISHED;
1225 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
1226 }
1227 /*
1228 * If segment contains data or ACK, will call tcp_reass()
1229 * later; if not, do so now to pass queued data to user.
1230 */
1231 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1232 (void) tcp_reass(tp, (struct tcpiphdr *)0,
1233 (struct mbuf *)0);
1234 tp->snd_wl1 = ti->ti_seq - 1;
1235 /* fall into ... */
1236
1237 /*
1238 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1239 * ACKs. If the ack is in the range
1240 * tp->snd_una < ti->ti_ack <= tp->snd_max
1241 * then advance tp->snd_una to ti->ti_ack and drop
1242 * data from the retransmission queue. If this ACK reflects
1243 * more up to date window information we update our window information.
1244 */
1245 case TCPS_ESTABLISHED:
1246 case TCPS_FIN_WAIT_1:
1247 case TCPS_FIN_WAIT_2:
1248 case TCPS_CLOSE_WAIT:
1249 case TCPS_CLOSING:
1250 case TCPS_LAST_ACK:
1251 case TCPS_TIME_WAIT:
1252
1253 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1254 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1255 tcpstat.tcps_rcvdupack++;
1256 /*
1257 * If we have outstanding data (other than
1258 * a window probe), this is a completely
1259 * duplicate ack (ie, window info didn't
1260 * change), the ack is the biggest we've
1261 * seen and we've seen exactly our rexmt
1262 * threshhold of them, assume a packet
1263 * has been dropped and retransmit it.
1264 * Kludge snd_nxt & the congestion
1265 * window so we send only this one
1266 * packet.
1267 *
1268 * We know we're losing at the current
1269 * window size so do congestion avoidance
1270 * (set ssthresh to half the current window
1271 * and pull our congestion window back to
1272 * the new ssthresh).
1273 *
1274 * Dup acks mean that packets have left the
1275 * network (they're now cached at the receiver)
1276 * so bump cwnd by the amount in the receiver
1277 * to keep a constant cwnd packets in the
1278 * network.
1279 */
1280 if (tp->t_timer[TCPT_REXMT] == 0 ||
1281 ti->ti_ack != tp->snd_una)
1282 tp->t_dupacks = 0;
1283 else if (++tp->t_dupacks == tcprexmtthresh) {
1284 tcp_seq onxt = tp->snd_nxt;
1285 u_int win =
1286 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1287 tp->t_maxseg;
1288
1289 if (win < 2)
1290 win = 2;
1291 tp->snd_ssthresh = win * tp->t_maxseg;
1292 tp->t_timer[TCPT_REXMT] = 0;
1293 tp->t_rtt = 0;
1294 tp->snd_nxt = ti->ti_ack;
1295 tp->snd_cwnd = tp->t_maxseg;
1296 (void) tcp_output(tp);
1297 tp->snd_cwnd = tp->snd_ssthresh +
1298 tp->t_maxseg * tp->t_dupacks;
1299 if (SEQ_GT(onxt, tp->snd_nxt))
1300 tp->snd_nxt = onxt;
1301 goto drop;
1302 } else if (tp->t_dupacks > tcprexmtthresh) {
1303 tp->snd_cwnd += tp->t_maxseg;
1304 (void) tcp_output(tp);
1305 goto drop;
1306 }
1307 } else
1308 tp->t_dupacks = 0;
1309 break;
1310 }
1311 /*
1312 * If the congestion window was inflated to account
1313 * for the other side's cached packets, retract it.
1314 */
1315 if (tp->t_dupacks > tcprexmtthresh &&
1316 tp->snd_cwnd > tp->snd_ssthresh)
1317 tp->snd_cwnd = tp->snd_ssthresh;
1318 tp->t_dupacks = 0;
1319 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1320 tcpstat.tcps_rcvacktoomuch++;
1321 goto dropafterack;
1322 }
1323 /*
1324 * If we reach this point, ACK is not a duplicate,
1325 * i.e., it ACKs something we sent.
1326 */
1327 if (tp->t_flags & TF_NEEDSYN) {
1328 /*
1329 * T/TCP: Connection was half-synchronized, and our
1330 * SYN has been ACK'd (so connection is now fully
1331 * synchronized). Go to non-starred state,
1332 * increment snd_una for ACK of SYN, and check if
1333 * we can do window scaling.
1334 */
1335 tp->t_flags &= ~TF_NEEDSYN;
1336 tp->snd_una++;
1337 /* Do window scaling? */
1338 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1339 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1340 tp->snd_scale = tp->requested_s_scale;
1341 tp->rcv_scale = tp->request_r_scale;
1342 }
1343 }
1344
1345 process_ACK:
1346 acked = ti->ti_ack - tp->snd_una;
1347 tcpstat.tcps_rcvackpack++;
1348 tcpstat.tcps_rcvackbyte += acked;
1349
1350 /*
1351 * If we have a timestamp reply, update smoothed
1352 * round trip time. If no timestamp is present but
1353 * transmit timer is running and timed sequence
1354 * number was acked, update smoothed round trip time.
1355 * Since we now have an rtt measurement, cancel the
1356 * timer backoff (cf., Phil Karn's retransmit alg.).
1357 * Recompute the initial retransmit timer.
1358 */
1359 if (to.to_flag & TOF_TS)
1360 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1361 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1362 tcp_xmit_timer(tp,tp->t_rtt);
1363
1364 /*
1365 * If all outstanding data is acked, stop retransmit
1366 * timer and remember to restart (more output or persist).
1367 * If there is more data to be acked, restart retransmit
1368 * timer, using current (possibly backed-off) value.
1369 */
1370 if (ti->ti_ack == tp->snd_max) {
1371 tp->t_timer[TCPT_REXMT] = 0;
1372 needoutput = 1;
1373 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1374 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1375
1376 /*
1377 * If no data (only SYN) was ACK'd,
1378 * skip rest of ACK processing.
1379 */
1380 if (acked == 0)
1381 goto step6;
1382
1383 /*
1384 * When new data is acked, open the congestion window.
1385 * If the window gives us less than ssthresh packets
1386 * in flight, open exponentially (maxseg per packet).
1387 * Otherwise open linearly: maxseg per window
1388 * (maxseg^2 / cwnd per packet).
1389 */
1390 {
1391 register u_int cw = tp->snd_cwnd;
1392 register u_int incr = tp->t_maxseg;
1393
1394 if (cw > tp->snd_ssthresh)
1395 incr = incr * incr / cw;
1396 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1397 }
1398 if (acked > so->so_snd.sb_cc) {
1399 tp->snd_wnd -= so->so_snd.sb_cc;
1400 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1401 ourfinisacked = 1;
1402 } else {
1403 sbdrop(&so->so_snd, acked);
1404 tp->snd_wnd -= acked;
1405 ourfinisacked = 0;
1406 }
1407 if (so->so_snd.sb_flags & SB_NOTIFY)
1408 sowwakeup(so);
1409 tp->snd_una = ti->ti_ack;
1410 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1411 tp->snd_nxt = tp->snd_una;
1412
1413 switch (tp->t_state) {
1414
1415 /*
1416 * In FIN_WAIT_1 STATE in addition to the processing
1417 * for the ESTABLISHED state if our FIN is now acknowledged
1418 * then enter FIN_WAIT_2.
1419 */
1420 case TCPS_FIN_WAIT_1:
1421 if (ourfinisacked) {
1422 /*
1423 * If we can't receive any more
1424 * data, then closing user can proceed.
1425 * Starting the timer is contrary to the
1426 * specification, but if we don't get a FIN
1427 * we'll hang forever.
1428 */
1429 if (so->so_state & SS_CANTRCVMORE) {
1430 soisdisconnected(so);
1431 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1432 }
1433 tp->t_state = TCPS_FIN_WAIT_2;
1434 }
1435 break;
1436
1437 /*
1438 * In CLOSING STATE in addition to the processing for
1439 * the ESTABLISHED state if the ACK acknowledges our FIN
1440 * then enter the TIME-WAIT state, otherwise ignore
1441 * the segment.
1442 */
1443 case TCPS_CLOSING:
1444 if (ourfinisacked) {
1445 tp->t_state = TCPS_TIME_WAIT;
1446 tcp_canceltimers(tp);
1447 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1448 if (tp->cc_recv != 0 &&
1449 tp->t_duration < TCPTV_MSL)
1450 tp->t_timer[TCPT_2MSL] =
1451 tp->t_rxtcur * TCPTV_TWTRUNC;
1452 else
1453 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1454 soisdisconnected(so);
1455 }
1456 break;
1457
1458 /*
1459 * In LAST_ACK, we may still be waiting for data to drain
1460 * and/or to be acked, as well as for the ack of our FIN.
1461 * If our FIN is now acknowledged, delete the TCB,
1462 * enter the closed state and return.
1463 */
1464 case TCPS_LAST_ACK:
1465 if (ourfinisacked) {
1466 tp = tcp_close(tp);
1467 goto drop;
1468 }
1469 break;
1470
1471 /*
1472 * In TIME_WAIT state the only thing that should arrive
1473 * is a retransmission of the remote FIN. Acknowledge
1474 * it and restart the finack timer.
1475 */
1476 case TCPS_TIME_WAIT:
1477 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1478 goto dropafterack;
1479 }
1480 }
1481
1482 step6:
1483 /*
1484 * Update window information.
1485 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1486 */
1487 if ((tiflags & TH_ACK) &&
1488 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1489 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1490 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1491 /* keep track of pure window updates */
1492 if (ti->ti_len == 0 &&
1493 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1494 tcpstat.tcps_rcvwinupd++;
1495 tp->snd_wnd = tiwin;
1496 tp->snd_wl1 = ti->ti_seq;
1497 tp->snd_wl2 = ti->ti_ack;
1498 if (tp->snd_wnd > tp->max_sndwnd)
1499 tp->max_sndwnd = tp->snd_wnd;
1500 needoutput = 1;
1501 }
1502
1503 /*
1504 * Process segments with URG.
1505 */
1506 if ((tiflags & TH_URG) && ti->ti_urp &&
1507 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1508 /*
1509 * This is a kludge, but if we receive and accept
1510 * random urgent pointers, we'll crash in
1511 * soreceive. It's hard to imagine someone
1512 * actually wanting to send this much urgent data.
1513 */
1514 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1515 ti->ti_urp = 0; /* XXX */
1516 tiflags &= ~TH_URG; /* XXX */
1517 goto dodata; /* XXX */
1518 }
1519 /*
1520 * If this segment advances the known urgent pointer,
1521 * then mark the data stream. This should not happen
1522 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1523 * a FIN has been received from the remote side.
1524 * In these states we ignore the URG.
1525 *
1526 * According to RFC961 (Assigned Protocols),
1527 * the urgent pointer points to the last octet
1528 * of urgent data. We continue, however,
1529 * to consider it to indicate the first octet
1530 * of data past the urgent section as the original
1531 * spec states (in one of two places).
1532 */
1533 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1534 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1535 so->so_oobmark = so->so_rcv.sb_cc +
1536 (tp->rcv_up - tp->rcv_nxt) - 1;
1537 if (so->so_oobmark == 0)
1538 so->so_state |= SS_RCVATMARK;
1539 sohasoutofband(so);
1540 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1541 }
1542 /*
1543 * Remove out of band data so doesn't get presented to user.
1544 * This can happen independent of advancing the URG pointer,
1545 * but if two URG's are pending at once, some out-of-band
1546 * data may creep in... ick.
1547 */
1548 if (ti->ti_urp <= (u_long)ti->ti_len
1549 #ifdef SO_OOBINLINE
1550 && (so->so_options & SO_OOBINLINE) == 0
1551 #endif
1552 )
1553 tcp_pulloutofband(so, ti, m);
1554 } else
1555 /*
1556 * If no out of band data is expected,
1557 * pull receive urgent pointer along
1558 * with the receive window.
1559 */
1560 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1561 tp->rcv_up = tp->rcv_nxt;
1562 dodata: /* XXX */
1563
1564 /*
1565 * Process the segment text, merging it into the TCP sequencing queue,
1566 * and arranging for acknowledgment of receipt if necessary.
1567 * This process logically involves adjusting tp->rcv_wnd as data
1568 * is presented to the user (this happens in tcp_usrreq.c,
1569 * case PRU_RCVD). If a FIN has already been received on this
1570 * connection then we just ignore the text.
1571 */
1572 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1573 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1574 TCP_REASS(tp, ti, m, so, tiflags);
1575 /*
1576 * Note the amount of data that peer has sent into
1577 * our window, in order to estimate the sender's
1578 * buffer size.
1579 */
1580 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1581 } else {
1582 m_freem(m);
1583 tiflags &= ~TH_FIN;
1584 }
1585
1586 /*
1587 * If FIN is received ACK the FIN and let the user know
1588 * that the connection is closing.
1589 */
1590 if (tiflags & TH_FIN) {
1591 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1592 socantrcvmore(so);
1593 /*
1594 * If connection is half-synchronized
1595 * (ie NEEDSYN flag on) then delay ACK,
1596 * so it may be piggybacked when SYN is sent.
1597 * Otherwise, since we received a FIN then no
1598 * more input can be expected, send ACK now.
1599 */
1600 if (tp->t_flags & TF_NEEDSYN)
1601 tp->t_flags |= TF_DELACK;
1602 else
1603 tp->t_flags |= TF_ACKNOW;
1604 tp->rcv_nxt++;
1605 }
1606 switch (tp->t_state) {
1607
1608 /*
1609 * In SYN_RECEIVED and ESTABLISHED STATES
1610 * enter the CLOSE_WAIT state.
1611 */
1612 case TCPS_SYN_RECEIVED:
1613 case TCPS_ESTABLISHED:
1614 tp->t_state = TCPS_CLOSE_WAIT;
1615 break;
1616
1617 /*
1618 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1619 * enter the CLOSING state.
1620 */
1621 case TCPS_FIN_WAIT_1:
1622 tp->t_state = TCPS_CLOSING;
1623 break;
1624
1625 /*
1626 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1627 * starting the time-wait timer, turning off the other
1628 * standard timers.
1629 */
1630 case TCPS_FIN_WAIT_2:
1631 tp->t_state = TCPS_TIME_WAIT;
1632 tcp_canceltimers(tp);
1633 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1634 if (tp->cc_recv != 0 &&
1635 tp->t_duration < TCPTV_MSL) {
1636 tp->t_timer[TCPT_2MSL] =
1637 tp->t_rxtcur * TCPTV_TWTRUNC;
1638 /* For transaction client, force ACK now. */
1639 tp->t_flags |= TF_ACKNOW;
1640 }
1641 else
1642 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1643 soisdisconnected(so);
1644 break;
1645
1646 /*
1647 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1648 */
1649 case TCPS_TIME_WAIT:
1650 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1651 break;
1652 }
1653 }
1654 #ifdef TCPDEBUG
1655 if (so->so_options & SO_DEBUG)
1656 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1657 #endif
1658
1659 /*
1660 * Return any desired output.
1661 */
1662 if (needoutput || (tp->t_flags & TF_ACKNOW))
1663 (void) tcp_output(tp);
1664 return;
1665
1666 dropafterack:
1667 /*
1668 * Generate an ACK dropping incoming segment if it occupies
1669 * sequence space, where the ACK reflects our state.
1670 */
1671 if (tiflags & TH_RST)
1672 goto drop;
1673 #ifdef TCPDEBUG
1674 if (so->so_options & SO_DEBUG)
1675 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1676 #endif
1677 m_freem(m);
1678 tp->t_flags |= TF_ACKNOW;
1679 (void) tcp_output(tp);
1680 return;
1681
1682 dropwithreset:
1683 /*
1684 * Generate a RST, dropping incoming segment.
1685 * Make ACK acceptable to originator of segment.
1686 * Don't bother to respond if destination was broadcast/multicast.
1687 */
1688 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1689 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1690 goto drop;
1691 #ifdef TCPDEBUG
1692 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1693 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1694 #endif
1695 if (tiflags & TH_ACK)
1696 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1697 else {
1698 if (tiflags & TH_SYN)
1699 ti->ti_len++;
1700 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1701 TH_RST|TH_ACK);
1702 }
1703 /* destroy temporarily created socket */
1704 if (dropsocket)
1705 (void) soabort(so);
1706 return;
1707
1708 drop:
1709 /*
1710 * Drop space held by incoming segment and return.
1711 */
1712 #ifdef TCPDEBUG
1713 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1714 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1715 #endif
1716 m_freem(m);
1717 /* destroy temporarily created socket */
1718 if (dropsocket)
1719 (void) soabort(so);
1720 return;
1721 #ifndef TUBA_INCLUDE
1722 }
1723
1724 void
1725 tcp_dooptions(tp, cp, cnt, ti, to)
1726 struct tcpcb *tp;
1727 u_char *cp;
1728 int cnt;
1729 struct tcpiphdr *ti;
1730 struct tcpopt *to;
1731 {
1732 u_short mss = 0;
1733 int opt, optlen;
1734
1735 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1736 opt = cp[0];
1737 if (opt == TCPOPT_EOL)
1738 break;
1739 if (opt == TCPOPT_NOP)
1740 optlen = 1;
1741 else {
1742 optlen = cp[1];
1743 if (optlen <= 0)
1744 break;
1745 }
1746 switch (opt) {
1747
1748 default:
1749 continue;
1750
1751 case TCPOPT_MAXSEG:
1752 if (optlen != TCPOLEN_MAXSEG)
1753 continue;
1754 if (!(ti->ti_flags & TH_SYN))
1755 continue;
1756 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1757 NTOHS(mss);
1758 break;
1759
1760 case TCPOPT_WINDOW:
1761 if (optlen != TCPOLEN_WINDOW)
1762 continue;
1763 if (!(ti->ti_flags & TH_SYN))
1764 continue;
1765 tp->t_flags |= TF_RCVD_SCALE;
1766 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1767 break;
1768
1769 case TCPOPT_TIMESTAMP:
1770 if (optlen != TCPOLEN_TIMESTAMP)
1771 continue;
1772 to->to_flag |= TOF_TS;
1773 bcopy((char *)cp + 2,
1774 (char *)&to->to_tsval, sizeof(to->to_tsval));
1775 NTOHL(to->to_tsval);
1776 bcopy((char *)cp + 6,
1777 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1778 NTOHL(to->to_tsecr);
1779
1780 /*
1781 * A timestamp received in a SYN makes
1782 * it ok to send timestamp requests and replies.
1783 */
1784 if (ti->ti_flags & TH_SYN) {
1785 tp->t_flags |= TF_RCVD_TSTMP;
1786 tp->ts_recent = to->to_tsval;
1787 tp->ts_recent_age = tcp_now;
1788 }
1789 break;
1790 case TCPOPT_CC:
1791 if (optlen != TCPOLEN_CC)
1792 continue;
1793 to->to_flag |= TOF_CC;
1794 bcopy((char *)cp + 2,
1795 (char *)&to->to_cc, sizeof(to->to_cc));
1796 NTOHL(to->to_cc);
1797 /*
1798 * A CC or CC.new option received in a SYN makes
1799 * it ok to send CC in subsequent segments.
1800 */
1801 if (ti->ti_flags & TH_SYN)
1802 tp->t_flags |= TF_RCVD_CC;
1803 break;
1804 case TCPOPT_CCNEW:
1805 if (optlen != TCPOLEN_CC)
1806 continue;
1807 if (!(ti->ti_flags & TH_SYN))
1808 continue;
1809 to->to_flag |= TOF_CCNEW;
1810 bcopy((char *)cp + 2,
1811 (char *)&to->to_cc, sizeof(to->to_cc));
1812 NTOHL(to->to_cc);
1813 /*
1814 * A CC or CC.new option received in a SYN makes
1815 * it ok to send CC in subsequent segments.
1816 */
1817 tp->t_flags |= TF_RCVD_CC;
1818 break;
1819 case TCPOPT_CCECHO:
1820 if (optlen != TCPOLEN_CC)
1821 continue;
1822 if (!(ti->ti_flags & TH_SYN))
1823 continue;
1824 to->to_flag |= TOF_CCECHO;
1825 bcopy((char *)cp + 2,
1826 (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1827 NTOHL(to->to_ccecho);
1828 break;
1829 }
1830 }
1831 if (ti->ti_flags & TH_SYN)
1832 tcp_mss(tp, mss); /* sets t_maxseg */
1833 }
1834
1835 /*
1836 * Pull out of band byte out of a segment so
1837 * it doesn't appear in the user's data queue.
1838 * It is still reflected in the segment length for
1839 * sequencing purposes.
1840 */
1841 void
1842 tcp_pulloutofband(so, ti, m)
1843 struct socket *so;
1844 struct tcpiphdr *ti;
1845 register struct mbuf *m;
1846 {
1847 int cnt = ti->ti_urp - 1;
1848
1849 while (cnt >= 0) {
1850 if (m->m_len > cnt) {
1851 char *cp = mtod(m, caddr_t) + cnt;
1852 struct tcpcb *tp = sototcpcb(so);
1853
1854 tp->t_iobc = *cp;
1855 tp->t_oobflags |= TCPOOB_HAVEDATA;
1856 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1857 m->m_len--;
1858 return;
1859 }
1860 cnt -= m->m_len;
1861 m = m->m_next;
1862 if (m == 0)
1863 break;
1864 }
1865 panic("tcp_pulloutofband");
1866 }
1867
1868 /*
1869 * Collect new round-trip time estimate
1870 * and update averages and current timeout.
1871 */
1872 void
1873 tcp_xmit_timer(tp, rtt)
1874 register struct tcpcb *tp;
1875 short rtt;
1876 {
1877 register short delta;
1878
1879 tcpstat.tcps_rttupdated++;
1880 tp->t_rttupdated++;
1881 if (tp->t_srtt != 0) {
1882 /*
1883 * srtt is stored as fixed point with 3 bits after the
1884 * binary point (i.e., scaled by 8). The following magic
1885 * is equivalent to the smoothing algorithm in rfc793 with
1886 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1887 * point). Adjust rtt to origin 0.
1888 */
1889 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1890 if ((tp->t_srtt += delta) <= 0)
1891 tp->t_srtt = 1;
1892 /*
1893 * We accumulate a smoothed rtt variance (actually, a
1894 * smoothed mean difference), then set the retransmit
1895 * timer to smoothed rtt + 4 times the smoothed variance.
1896 * rttvar is stored as fixed point with 2 bits after the
1897 * binary point (scaled by 4). The following is
1898 * equivalent to rfc793 smoothing with an alpha of .75
1899 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1900 * rfc793's wired-in beta.
1901 */
1902 if (delta < 0)
1903 delta = -delta;
1904 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1905 if ((tp->t_rttvar += delta) <= 0)
1906 tp->t_rttvar = 1;
1907 } else {
1908 /*
1909 * No rtt measurement yet - use the unsmoothed rtt.
1910 * Set the variance to half the rtt (so our first
1911 * retransmit happens at 3*rtt).
1912 */
1913 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1914 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1915 }
1916 tp->t_rtt = 0;
1917 tp->t_rxtshift = 0;
1918
1919 /*
1920 * the retransmit should happen at rtt + 4 * rttvar.
1921 * Because of the way we do the smoothing, srtt and rttvar
1922 * will each average +1/2 tick of bias. When we compute
1923 * the retransmit timer, we want 1/2 tick of rounding and
1924 * 1 extra tick because of +-1/2 tick uncertainty in the
1925 * firing of the timer. The bias will give us exactly the
1926 * 1.5 tick we need. But, because the bias is
1927 * statistical, we have to test that we don't drop below
1928 * the minimum feasible timer (which is 2 ticks).
1929 */
1930 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1931 tp->t_rttmin, TCPTV_REXMTMAX);
1932
1933 /*
1934 * We received an ack for a packet that wasn't retransmitted;
1935 * it is probably safe to discard any error indications we've
1936 * received recently. This isn't quite right, but close enough
1937 * for now (a route might have failed after we sent a segment,
1938 * and the return path might not be symmetrical).
1939 */
1940 tp->t_softerror = 0;
1941 }
1942
1943 /*
1944 * Determine a reasonable value for maxseg size.
1945 * If the route is known, check route for mtu.
1946 * If none, use an mss that can be handled on the outgoing
1947 * interface without forcing IP to fragment; if bigger than
1948 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1949 * to utilize large mbufs. If no route is found, route has no mtu,
1950 * or the destination isn't local, use a default, hopefully conservative
1951 * size (usually 512 or the default IP max size, but no more than the mtu
1952 * of the interface), as we can't discover anything about intervening
1953 * gateways or networks. We also initialize the congestion/slow start
1954 * window to be a single segment if the destination isn't local.
1955 * While looking at the routing entry, we also initialize other path-dependent
1956 * parameters from pre-set or cached values in the routing entry.
1957 *
1958 * Also take into account the space needed for options that we
1959 * send regularly. Make maxseg shorter by that amount to assure
1960 * that we can send maxseg amount of data even when the options
1961 * are present. Store the upper limit of the length of options plus
1962 * data in maxopd.
1963 *
1964 * NOTE that this routine is only called when we process an incoming
1965 * segment, for outgoing segments only tcp_mssopt is called.
1966 *
1967 * In case of T/TCP, we call this routine during implicit connection
1968 * setup as well (offer = -1), to initialize maxseg from the cached
1969 * MSS of our peer.
1970 */
1971 void
1972 tcp_mss(tp, offer)
1973 struct tcpcb *tp;
1974 int offer;
1975 {
1976 register struct rtentry *rt;
1977 struct ifnet *ifp;
1978 register int rtt, mss;
1979 u_long bufsize;
1980 struct inpcb *inp;
1981 struct socket *so;
1982 struct rmxp_tao *taop;
1983 int origoffer = offer;
1984
1985 inp = tp->t_inpcb;
1986 if ((rt = tcp_rtlookup(inp)) == NULL) {
1987 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
1988 return;
1989 }
1990 #ifndef __REACTOS__
1991 ifp = rt->rt_ifp;
1992 #endif
1993 so = inp->inp_socket;
1994
1995 taop = rmx_taop(rt->rt_rmx);
1996 /*
1997 * Offer == -1 means that we didn't receive SYN yet,
1998 * use cached value in that case;
1999 */
2000 if (offer == -1)
2001 offer = taop->tao_mssopt;
2002 /*
2003 * Offer == 0 means that there was no MSS on the SYN segment,
2004 * in this case we use tcp_mssdflt.
2005 */
2006 if (offer == 0)
2007 offer = tcp_mssdflt;
2008 else
2009 /*
2010 * Sanity check: make sure that maxopd will be large
2011 * enough to allow some data on segments even is the
2012 * all the option space is used (40bytes). Otherwise
2013 * funny things may happen in tcp_output.
2014 */
2015 offer = max(offer, 64);
2016 taop->tao_mssopt = offer;
2017
2018 /*
2019 * While we're here, check if there's an initial rtt
2020 * or rttvar. Convert from the route-table units
2021 * to scaled multiples of the slow timeout timer.
2022 */
2023 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2024 /*
2025 * XXX the lock bit for RTT indicates that the value
2026 * is also a minimum value; this is subject to time.
2027 */
2028 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2029 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
2030 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
2031 tcpstat.tcps_usedrtt++;
2032 if (rt->rt_rmx.rmx_rttvar) {
2033 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2034 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
2035 tcpstat.tcps_usedrttvar++;
2036 } else {
2037 /* default variation is +- 1 rtt */
2038 tp->t_rttvar =
2039 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2040 }
2041 TCPT_RANGESET(tp->t_rxtcur,
2042 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2043 tp->t_rttmin, TCPTV_REXMTMAX);
2044 }
2045 /*
2046 * if there's an mtu associated with the route, use it
2047 */
2048 if (rt->rt_rmx.rmx_mtu)
2049 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2050 else
2051 {
2052 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
2053 if (!in_localaddr(inp->inp_faddr))
2054 mss = min(mss, tcp_mssdflt);
2055 }
2056 mss = min(mss, offer);
2057 /*
2058 * maxopd stores the maximum length of data AND options
2059 * in a segment; maxseg is the amount of data in a normal
2060 * segment. We need to store this value (maxopd) apart
2061 * from maxseg, because now every segment carries options
2062 * and thus we normally have somewhat less data in segments.
2063 */
2064 tp->t_maxopd = mss;
2065
2066 /*
2067 * In case of T/TCP, origoffer==-1 indicates, that no segments
2068 * were received yet. In this case we just guess, otherwise
2069 * we do the same as before T/TCP.
2070 */
2071 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2072 (origoffer == -1 ||
2073 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2074 mss -= TCPOLEN_TSTAMP_APPA;
2075 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2076 (origoffer == -1 ||
2077 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2078 mss -= TCPOLEN_CC_APPA;
2079
2080 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2081 if (mss > MCLBYTES)
2082 mss &= ~(MCLBYTES-1);
2083 #else
2084 if (mss > MCLBYTES)
2085 mss = mss / MCLBYTES * MCLBYTES;
2086 #endif
2087 /*
2088 * If there's a pipesize, change the socket buffer
2089 * to that size. Make the socket buffers an integral
2090 * number of mss units; if the mss is larger than
2091 * the socket buffer, decrease the mss.
2092 */
2093 #ifdef RTV_SPIPE
2094 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2095 #endif
2096 bufsize = so->so_snd.sb_hiwat;
2097 if (bufsize < mss)
2098 mss = bufsize;
2099 else {
2100 bufsize = roundup(bufsize, mss);
2101 if (bufsize > sb_max)
2102 bufsize = sb_max;
2103 (void)sbreserve(&so->so_snd, bufsize);
2104 }
2105 tp->t_maxseg = mss;
2106
2107 #ifdef RTV_RPIPE
2108 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2109 #endif
2110 bufsize = so->so_rcv.sb_hiwat;
2111 if (bufsize > mss) {
2112 bufsize = roundup(bufsize, mss);
2113 if (bufsize > sb_max)
2114 bufsize = sb_max;
2115 (void)sbreserve(&so->so_rcv, bufsize);
2116 }
2117 /*
2118 * Don't force slow-start on local network.
2119 */
2120 if (!in_localaddr(inp->inp_faddr))
2121 tp->snd_cwnd = mss;
2122
2123 if (rt->rt_rmx.rmx_ssthresh) {
2124 /*
2125 * There's some sort of gateway or interface
2126 * buffer limit on the path. Use this to set
2127 * the slow start threshhold, but set the
2128 * threshold to no less than 2*mss.
2129 */
2130 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2131 tcpstat.tcps_usedssthresh++;
2132 }
2133 }
2134
2135 /*
2136 * Determine the MSS option to send on an outgoing SYN.
2137 */
2138 int
2139 tcp_mssopt(tp)
2140 struct tcpcb *tp;
2141 {
2142 struct rtentry *rt;
2143
2144 rt = tcp_rtlookup(tp->t_inpcb);
2145 if (rt == NULL)
2146 return tcp_mssdflt;
2147 #ifndef __REACTOS__
2148 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2149 #else
2150 return tcp_mssdflt;
2151 #endif
2152 }
2153 #endif /* TUBA_INCLUDE */