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