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