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