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