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