xref: /csrg-svn/sys/netinet/tcp_input.c (revision 25196)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tcp_input.c	6.14 (Berkeley) 10/14/85
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "mbuf.h"
12 #include "protosw.h"
13 #include "socket.h"
14 #include "socketvar.h"
15 #include "errno.h"
16 
17 #include "../net/if.h"
18 #include "../net/route.h"
19 
20 #include "in.h"
21 #include "in_pcb.h"
22 #include "in_systm.h"
23 #include "ip.h"
24 #include "ip_var.h"
25 #include "tcp.h"
26 #include "tcp_fsm.h"
27 #include "tcp_seq.h"
28 #include "tcp_timer.h"
29 #include "tcp_var.h"
30 #include "tcpip.h"
31 #include "tcp_debug.h"
32 
33 int	tcpprintfs = 0;
34 int	tcpcksum = 1;
35 struct	tcpiphdr tcp_saveti;
36 extern	tcpnodelack;
37 
38 struct	tcpcb *tcp_newtcpcb();
39 
40 /*
41  * Insert segment ti into reassembly queue of tcp with
42  * control block tp.  Return TH_FIN if reassembly now includes
43  * a segment with FIN.  The macro form does the common case inline
44  * (segment is the next to be received on an established connection,
45  * and the queue is empty), avoiding linkage into and removal
46  * from the queue and repetition of various conversions.
47  */
48 #define	TCP_REASS(tp, ti, m, so, flags) { \
49 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
50 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
51 	    (tp)->t_state == TCPS_ESTABLISHED) { \
52 		(tp)->rcv_nxt += (ti)->ti_len; \
53 		flags = (ti)->ti_flags & TH_FIN; \
54 		sbappend(&(so)->so_rcv, (m)); \
55 		sorwakeup(so); \
56 	} else \
57 		(flags) = tcp_reass((tp), (ti)); \
58 }
59 
60 tcp_reass(tp, ti)
61 	register struct tcpcb *tp;
62 	register struct tcpiphdr *ti;
63 {
64 	register struct tcpiphdr *q;
65 	struct socket *so = tp->t_inpcb->inp_socket;
66 	struct mbuf *m;
67 	int flags;
68 
69 	/*
70 	 * Call with ti==0 after become established to
71 	 * force pre-ESTABLISHED data up to user socket.
72 	 */
73 	if (ti == 0)
74 		goto present;
75 
76 	/*
77 	 * Find a segment which begins after this one does.
78 	 */
79 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
80 	    q = (struct tcpiphdr *)q->ti_next)
81 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
82 			break;
83 
84 	/*
85 	 * If there is a preceding segment, it may provide some of
86 	 * our data already.  If so, drop the data from the incoming
87 	 * segment.  If it provides all of our data, drop us.
88 	 */
89 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
90 		register int i;
91 		q = (struct tcpiphdr *)q->ti_prev;
92 		/* conversion to int (in i) handles seq wraparound */
93 		i = q->ti_seq + q->ti_len - ti->ti_seq;
94 		if (i > 0) {
95 			if (i >= ti->ti_len)
96 				goto drop;
97 			m_adj(dtom(ti), i);
98 			ti->ti_len -= i;
99 			ti->ti_seq += i;
100 		}
101 		q = (struct tcpiphdr *)(q->ti_next);
102 	}
103 
104 	/*
105 	 * While we overlap succeeding segments trim them or,
106 	 * if they are completely covered, dequeue them.
107 	 */
108 	while (q != (struct tcpiphdr *)tp) {
109 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
110 		if (i <= 0)
111 			break;
112 		if (i < q->ti_len) {
113 			q->ti_seq += i;
114 			q->ti_len -= i;
115 			m_adj(dtom(q), i);
116 			break;
117 		}
118 		q = (struct tcpiphdr *)q->ti_next;
119 		m = dtom(q->ti_prev);
120 		remque(q->ti_prev);
121 		m_freem(m);
122 	}
123 
124 	/*
125 	 * Stick new segment in its place.
126 	 */
127 	insque(ti, q->ti_prev);
128 
129 present:
130 	/*
131 	 * Present data to user, advancing rcv_nxt through
132 	 * completed sequence space.
133 	 */
134 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
135 		return (0);
136 	ti = tp->seg_next;
137 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
138 		return (0);
139 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
140 		return (0);
141 	do {
142 		tp->rcv_nxt += ti->ti_len;
143 		flags = ti->ti_flags & TH_FIN;
144 		remque(ti);
145 		m = dtom(ti);
146 		ti = (struct tcpiphdr *)ti->ti_next;
147 		if (so->so_state & SS_CANTRCVMORE)
148 			m_freem(m);
149 		else
150 			sbappend(&so->so_rcv, m);
151 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
152 	sorwakeup(so);
153 	return (flags);
154 drop:
155 	m_freem(dtom(ti));
156 	return (0);
157 }
158 
159 /*
160  * TCP input routine, follows pages 65-76 of the
161  * protocol specification dated September, 1981 very closely.
162  */
163 tcp_input(m0)
164 	struct mbuf *m0;
165 {
166 	register struct tcpiphdr *ti;
167 	struct inpcb *inp;
168 	register struct mbuf *m;
169 	struct mbuf *om = 0;
170 	int len, tlen, off;
171 	register struct tcpcb *tp = 0;
172 	register int tiflags;
173 	struct socket *so;
174 	int todrop, acked;
175 	short ostate;
176 	struct in_addr laddr;
177 	int dropsocket = 0;
178 
179 	/*
180 	 * Get IP and TCP header together in first mbuf.
181 	 * Note: IP leaves IP header in first mbuf.
182 	 */
183 	m = m0;
184 	ti = mtod(m, struct tcpiphdr *);
185 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
186 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
187 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
188 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
189 			tcpstat.tcps_hdrops++;
190 			return;
191 		}
192 		ti = mtod(m, struct tcpiphdr *);
193 	}
194 
195 	/*
196 	 * Checksum extended TCP header and data.
197 	 */
198 	tlen = ((struct ip *)ti)->ip_len;
199 	len = sizeof (struct ip) + tlen;
200 	if (tcpcksum) {
201 		ti->ti_next = ti->ti_prev = 0;
202 		ti->ti_x1 = 0;
203 		ti->ti_len = (u_short)tlen;
204 		ti->ti_len = htons((u_short)ti->ti_len);
205 		if (ti->ti_sum = in_cksum(m, len)) {
206 			if (tcpprintfs)
207 				printf("tcp sum: src %x\n", ti->ti_src);
208 			tcpstat.tcps_badsum++;
209 			goto drop;
210 		}
211 	}
212 
213 	/*
214 	 * Check that TCP offset makes sense,
215 	 * pull out TCP options and adjust length.
216 	 */
217 	off = ti->ti_off << 2;
218 	if (off < sizeof (struct tcphdr) || off > tlen) {
219 		if (tcpprintfs)
220 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
221 		tcpstat.tcps_badoff++;
222 		goto drop;
223 	}
224 	tlen -= off;
225 	ti->ti_len = tlen;
226 	if (off > sizeof (struct tcphdr)) {
227 		if (m->m_len < sizeof(struct ip) + off) {
228 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
229 				tcpstat.tcps_hdrops++;
230 				return;
231 			}
232 			ti = mtod(m, struct tcpiphdr *);
233 		}
234 		om = m_get(M_DONTWAIT, MT_DATA);
235 		if (om == 0)
236 			goto drop;
237 		om->m_len = off - sizeof (struct tcphdr);
238 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
239 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
240 		  m->m_len -= om->m_len;
241 		  bcopy(op+om->m_len, op,
242 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
243 		}
244 	}
245 	tiflags = ti->ti_flags;
246 
247 	/*
248 	 * Drop TCP and IP headers.
249 	 */
250 	off += sizeof (struct ip);
251 	m->m_off += off;
252 	m->m_len -= off;
253 
254 	/*
255 	 * Convert TCP protocol specific fields to host format.
256 	 */
257 	ti->ti_seq = ntohl(ti->ti_seq);
258 	ti->ti_ack = ntohl(ti->ti_ack);
259 	ti->ti_win = ntohs(ti->ti_win);
260 	ti->ti_urp = ntohs(ti->ti_urp);
261 
262 	/*
263 	 * Locate pcb for segment.
264 	 */
265 	inp = in_pcblookup
266 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
267 		INPLOOKUP_WILDCARD);
268 
269 	/*
270 	 * If the state is CLOSED (i.e., TCB does not exist) then
271 	 * all data in the incoming segment is discarded.
272 	 */
273 	if (inp == 0)
274 		goto dropwithreset;
275 	tp = intotcpcb(inp);
276 	if (tp == 0)
277 		goto dropwithreset;
278 	so = inp->inp_socket;
279 	if (so->so_options & SO_DEBUG) {
280 		ostate = tp->t_state;
281 		tcp_saveti = *ti;
282 	}
283 	if (so->so_options & SO_ACCEPTCONN) {
284 		so = sonewconn(so);
285 		if (so == 0)
286 			goto drop;
287 		/*
288 		 * This is ugly, but ....
289 		 *
290 		 * Mark socket as temporary until we're
291 		 * committed to keeping it.  The code at
292 		 * ``drop'' and ``dropwithreset'' check the
293 		 * flag dropsocket to see if the temporary
294 		 * socket created here should be discarded.
295 		 * We mark the socket as discardable until
296 		 * we're committed to it below in TCPS_LISTEN.
297 		 */
298 		dropsocket++;
299 		inp = (struct inpcb *)so->so_pcb;
300 		inp->inp_laddr = ti->ti_dst;
301 		inp->inp_lport = ti->ti_dport;
302 		inp->inp_options = ip_srcroute();
303 		tp = intotcpcb(inp);
304 		tp->t_state = TCPS_LISTEN;
305 	}
306 
307 	/*
308 	 * Segment received on connection.
309 	 * Reset idle time and keep-alive timer.
310 	 */
311 	tp->t_idle = 0;
312 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
313 
314 	/*
315 	 * Process options if not in LISTEN state,
316 	 * else do it below (after getting remote address).
317 	 */
318 	if (om && tp->t_state != TCPS_LISTEN) {
319 		tcp_dooptions(tp, om, ti);
320 		om = 0;
321 	}
322 
323 	/*
324 	 * Calculate amount of space in receive window,
325 	 * and then do TCP input processing.
326 	 * Receive window is amount of space in rcv queue,
327 	 * but not less than advertised window.
328 	 */
329 	tp->rcv_wnd = sbspace(&so->so_rcv);
330 	if (tp->rcv_wnd < 0)
331 		tp->rcv_wnd = 0;
332 	tp->rcv_wnd = MAX(tp->rcv_wnd, (short)(tp->rcv_adv - tp->rcv_nxt));
333 
334 	switch (tp->t_state) {
335 
336 	/*
337 	 * If the state is LISTEN then ignore segment if it contains an RST.
338 	 * If the segment contains an ACK then it is bad and send a RST.
339 	 * If it does not contain a SYN then it is not interesting; drop it.
340 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
341 	 * tp->iss, and send a segment:
342 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
343 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
344 	 * Fill in remote peer address fields if not previously specified.
345 	 * Enter SYN_RECEIVED state, and process any other fields of this
346 	 * segment in this state.
347 	 */
348 	case TCPS_LISTEN: {
349 		struct mbuf *am;
350 		register struct sockaddr_in *sin;
351 
352 		if (tiflags & TH_RST)
353 			goto drop;
354 		if (tiflags & TH_ACK)
355 			goto dropwithreset;
356 		if ((tiflags & TH_SYN) == 0)
357 			goto drop;
358 		am = m_get(M_DONTWAIT, MT_SONAME);
359 		if (am == NULL)
360 			goto drop;
361 		am->m_len = sizeof (struct sockaddr_in);
362 		sin = mtod(am, struct sockaddr_in *);
363 		sin->sin_family = AF_INET;
364 		sin->sin_addr = ti->ti_src;
365 		sin->sin_port = ti->ti_sport;
366 		laddr = inp->inp_laddr;
367 		if (inp->inp_laddr.s_addr == INADDR_ANY)
368 			inp->inp_laddr = ti->ti_dst;
369 		if (in_pcbconnect(inp, am)) {
370 			inp->inp_laddr = laddr;
371 			(void) m_free(am);
372 			goto drop;
373 		}
374 		(void) m_free(am);
375 		tp->t_template = tcp_template(tp);
376 		if (tp->t_template == 0) {
377 			in_pcbdisconnect(inp);
378 			dropsocket = 0;		/* socket is already gone */
379 			tp = 0;
380 			goto drop;
381 		}
382 		if (om) {
383 			tcp_dooptions(tp, om, ti);
384 			om = 0;
385 		}
386 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
387 		tp->irs = ti->ti_seq;
388 		tcp_sendseqinit(tp);
389 		tcp_rcvseqinit(tp);
390 		tp->t_state = TCPS_SYN_RECEIVED;
391 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
392 		dropsocket = 0;		/* committed to socket */
393 		goto trimthenstep6;
394 		}
395 
396 	/*
397 	 * If the state is SYN_SENT:
398 	 *	if seg contains an ACK, but not for our SYN, drop the input.
399 	 *	if seg contains a RST, then drop the connection.
400 	 *	if seg does not contain SYN, then drop it.
401 	 * Otherwise this is an acceptable SYN segment
402 	 *	initialize tp->rcv_nxt and tp->irs
403 	 *	if seg contains ack then advance tp->snd_una
404 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
405 	 *	arrange for segment to be acked (eventually)
406 	 *	continue processing rest of data/controls, beginning with URG
407 	 */
408 	case TCPS_SYN_SENT:
409 		if ((tiflags & TH_ACK) &&
410 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
411 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
412 			goto dropwithreset;
413 		if (tiflags & TH_RST) {
414 			if (tiflags & TH_ACK)
415 				tp = tcp_drop(tp, ECONNREFUSED);
416 			goto drop;
417 		}
418 		if ((tiflags & TH_SYN) == 0)
419 			goto drop;
420 		tp->snd_una = ti->ti_ack;
421 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
422 			tp->snd_nxt = tp->snd_una;
423 		tp->t_timer[TCPT_REXMT] = 0;
424 		tp->irs = ti->ti_seq;
425 		tcp_rcvseqinit(tp);
426 		tp->t_flags |= TF_ACKNOW;
427 		if (SEQ_GT(tp->snd_una, tp->iss)) {
428 			soisconnected(so);
429 			tp->t_state = TCPS_ESTABLISHED;
430 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
431 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
432 		} else
433 			tp->t_state = TCPS_SYN_RECEIVED;
434 		goto trimthenstep6;
435 
436 trimthenstep6:
437 		/*
438 		 * Advance ti->ti_seq to correspond to first data byte.
439 		 * If data, trim to stay within window,
440 		 * dropping FIN if necessary.
441 		 */
442 		ti->ti_seq++;
443 		if (ti->ti_len > tp->rcv_wnd) {
444 			todrop = ti->ti_len - tp->rcv_wnd;
445 			m_adj(m, -todrop);
446 			ti->ti_len = tp->rcv_wnd;
447 			ti->ti_flags &= ~TH_FIN;
448 		}
449 		tp->snd_wl1 = ti->ti_seq - 1;
450 		goto step6;
451 	}
452 
453 	/*
454 	 * If data is received on a connection after the
455 	 * user processes are gone, then RST the other end.
456 	 */
457 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
458 	    ti->ti_len) {
459 		tp = tcp_close(tp);
460 		goto dropwithreset;
461 	}
462 
463 	/*
464 	 * States other than LISTEN or SYN_SENT.
465 	 * First check that at least some bytes of segment are within
466 	 * receive window.
467 	 */
468 	if (tp->rcv_wnd == 0) {
469 		/*
470 		 * If window is closed can only take segments at
471 		 * window edge, and have to drop data and PUSH from
472 		 * incoming segments.
473 		 */
474 		if (tp->rcv_nxt != ti->ti_seq)
475 			goto dropafterack;
476 		if (ti->ti_len > 0) {
477 			m_adj(m, ti->ti_len);
478 			ti->ti_len = 0;
479 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
480 		}
481 	} else {
482 		/*
483 		 * If segment begins before rcv_nxt, drop leading
484 		 * data (and SYN); if nothing left, just ack.
485 		 */
486 		todrop = tp->rcv_nxt - ti->ti_seq;
487 		if (todrop > 0) {
488 			if (tiflags & TH_SYN) {
489 				tiflags &= ~TH_SYN;
490 				ti->ti_flags &= ~TH_SYN;
491 				ti->ti_seq++;
492 				if (ti->ti_urp > 1)
493 					ti->ti_urp--;
494 				else
495 					tiflags &= ~TH_URG;
496 				todrop--;
497 			}
498 			if (todrop > ti->ti_len ||
499 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
500 				goto dropafterack;
501 			m_adj(m, todrop);
502 			ti->ti_seq += todrop;
503 			ti->ti_len -= todrop;
504 			if (ti->ti_urp > todrop)
505 				ti->ti_urp -= todrop;
506 			else {
507 				tiflags &= ~TH_URG;
508 				ti->ti_flags &= ~TH_URG;
509 				ti->ti_urp = 0;
510 			}
511 		}
512 		/*
513 		 * If segment ends after window, drop trailing data
514 		 * (and PUSH and FIN); if nothing left, just ACK.
515 		 */
516 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
517 		if (todrop > 0) {
518 			if (todrop >= ti->ti_len)
519 				goto dropafterack;
520 			m_adj(m, -todrop);
521 			ti->ti_len -= todrop;
522 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
523 		}
524 	}
525 
526 	/*
527 	 * If the RST bit is set examine the state:
528 	 *    SYN_RECEIVED STATE:
529 	 *	If passive open, return to LISTEN state.
530 	 *	If active open, inform user that connection was refused.
531 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
532 	 *	Inform user that connection was reset, and close tcb.
533 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
534 	 *	Close the tcb.
535 	 */
536 	if (tiflags&TH_RST) switch (tp->t_state) {
537 
538 	case TCPS_SYN_RECEIVED:
539 		tp = tcp_drop(tp, ECONNREFUSED);
540 		goto drop;
541 
542 	case TCPS_ESTABLISHED:
543 	case TCPS_FIN_WAIT_1:
544 	case TCPS_FIN_WAIT_2:
545 	case TCPS_CLOSE_WAIT:
546 		tp = tcp_drop(tp, ECONNRESET);
547 		goto drop;
548 
549 	case TCPS_CLOSING:
550 	case TCPS_LAST_ACK:
551 	case TCPS_TIME_WAIT:
552 		tp = tcp_close(tp);
553 		goto drop;
554 	}
555 
556 	/*
557 	 * If a SYN is in the window, then this is an
558 	 * error and we send an RST and drop the connection.
559 	 */
560 	if (tiflags & TH_SYN) {
561 		tp = tcp_drop(tp, ECONNRESET);
562 		goto dropwithreset;
563 	}
564 
565 	/*
566 	 * If the ACK bit is off we drop the segment and return.
567 	 */
568 	if ((tiflags & TH_ACK) == 0)
569 		goto drop;
570 
571 	/*
572 	 * Ack processing.
573 	 */
574 	switch (tp->t_state) {
575 
576 	/*
577 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
578 	 * ESTABLISHED state and continue processing, othewise
579 	 * send an RST.
580 	 */
581 	case TCPS_SYN_RECEIVED:
582 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
583 		    SEQ_GT(ti->ti_ack, tp->snd_max))
584 			goto dropwithreset;
585 		tp->snd_una++;			/* SYN acked */
586 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
587 			tp->snd_nxt = tp->snd_una;
588 		tp->t_timer[TCPT_REXMT] = 0;
589 		soisconnected(so);
590 		tp->t_state = TCPS_ESTABLISHED;
591 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
592 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
593 		tp->snd_wl1 = ti->ti_seq - 1;
594 		/* fall into ... */
595 
596 	/*
597 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
598 	 * ACKs.  If the ack is in the range
599 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
600 	 * then advance tp->snd_una to ti->ti_ack and drop
601 	 * data from the retransmission queue.  If this ACK reflects
602 	 * more up to date window information we update our window information.
603 	 */
604 	case TCPS_ESTABLISHED:
605 	case TCPS_FIN_WAIT_1:
606 	case TCPS_FIN_WAIT_2:
607 	case TCPS_CLOSE_WAIT:
608 	case TCPS_CLOSING:
609 	case TCPS_LAST_ACK:
610 	case TCPS_TIME_WAIT:
611 #define	ourfinisacked	(acked > 0)
612 
613 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
614 			break;
615 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
616 			goto dropafterack;
617 		acked = ti->ti_ack - tp->snd_una;
618 
619 		/*
620 		 * If transmit timer is running and timed sequence
621 		 * number was acked, update smoothed round trip time.
622 		 */
623 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
624 			if (tp->t_srtt == 0)
625 				tp->t_srtt = tp->t_rtt;
626 			else
627 				tp->t_srtt =
628 				    tcp_alpha * tp->t_srtt +
629 				    (1 - tcp_alpha) * tp->t_rtt;
630 			tp->t_rtt = 0;
631 		}
632 
633 		if (ti->ti_ack == tp->snd_max)
634 			tp->t_timer[TCPT_REXMT] = 0;
635 		else {
636 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
637 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
638 			tp->t_rxtshift = 0;
639 		}
640 		/*
641 		 * When new data is acked, open the congestion window a bit.
642 		 */
643 		if (acked > 0)
644 			tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
645 		if (acked > so->so_snd.sb_cc) {
646 			tp->snd_wnd -= so->so_snd.sb_cc;
647 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
648 		} else {
649 			sbdrop(&so->so_snd, acked);
650 			tp->snd_wnd -= acked;
651 			acked = 0;
652 		}
653 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
654 			sowwakeup(so);
655 		tp->snd_una = ti->ti_ack;
656 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
657 			tp->snd_nxt = tp->snd_una;
658 
659 		switch (tp->t_state) {
660 
661 		/*
662 		 * In FIN_WAIT_1 STATE in addition to the processing
663 		 * for the ESTABLISHED state if our FIN is now acknowledged
664 		 * then enter FIN_WAIT_2.
665 		 */
666 		case TCPS_FIN_WAIT_1:
667 			if (ourfinisacked) {
668 				/*
669 				 * If we can't receive any more
670 				 * data, then closing user can proceed.
671 				 * Starting the timer is contrary to the
672 				 * specification, but if we don't get a FIN
673 				 * we'll hang forever.
674 				 */
675 				if (so->so_state & SS_CANTRCVMORE) {
676 					soisdisconnected(so);
677 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
678 				}
679 				tp->t_state = TCPS_FIN_WAIT_2;
680 			}
681 			break;
682 
683 	 	/*
684 		 * In CLOSING STATE in addition to the processing for
685 		 * the ESTABLISHED state if the ACK acknowledges our FIN
686 		 * then enter the TIME-WAIT state, otherwise ignore
687 		 * the segment.
688 		 */
689 		case TCPS_CLOSING:
690 			if (ourfinisacked) {
691 				tp->t_state = TCPS_TIME_WAIT;
692 				tcp_canceltimers(tp);
693 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
694 				soisdisconnected(so);
695 			}
696 			break;
697 
698 		/*
699 		 * The only thing that can arrive in  LAST_ACK state
700 		 * is an acknowledgment of our FIN.  If our FIN is now
701 		 * acknowledged, delete the TCB, enter the closed state
702 		 * and return.
703 		 */
704 		case TCPS_LAST_ACK:
705 			if (ourfinisacked)
706 				tp = tcp_close(tp);
707 			goto drop;
708 
709 		/*
710 		 * In TIME_WAIT state the only thing that should arrive
711 		 * is a retransmission of the remote FIN.  Acknowledge
712 		 * it and restart the finack timer.
713 		 */
714 		case TCPS_TIME_WAIT:
715 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
716 			goto dropafterack;
717 		}
718 #undef ourfinisacked
719 	}
720 
721 step6:
722 	/*
723 	 * Update window information.
724 	 */
725 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
726 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
727 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
728 		tp->snd_wnd = ti->ti_win;
729 		tp->snd_wl1 = ti->ti_seq;
730 		tp->snd_wl2 = ti->ti_ack;
731 	}
732 
733 	/*
734 	 * Process segments with URG.
735 	 */
736 	if ((tiflags & TH_URG) && ti->ti_urp &&
737 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
738 		/*
739 		 * This is a kludge, but if we receive accept
740 		 * random urgent pointers, we'll crash in
741 		 * soreceive.  It's hard to imagine someone
742 		 * actually wanting to send this much urgent data.
743 		 */
744 		if (ti->ti_urp + (unsigned) so->so_rcv.sb_cc > 32767) {
745 			ti->ti_urp = 0;			/* XXX */
746 			tiflags &= ~TH_URG;		/* XXX */
747 			ti->ti_flags &= ~TH_URG;	/* XXX */
748 			goto badurp;			/* XXX */
749 		}
750 		/*
751 		 * If this segment advances the known urgent pointer,
752 		 * then mark the data stream.  This should not happen
753 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
754 		 * a FIN has been received from the remote side.
755 		 * In these states we ignore the URG.
756 		 */
757 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
758 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
759 			so->so_oobmark = so->so_rcv.sb_cc +
760 			    (tp->rcv_up - tp->rcv_nxt) - 1;
761 			if (so->so_oobmark == 0)
762 				so->so_state |= SS_RCVATMARK;
763 			sohasoutofband(so);
764 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
765 		}
766 		/*
767 		 * Remove out of band data so doesn't get presented to user.
768 		 * This can happen independent of advancing the URG pointer,
769 		 * but if two URG's are pending at once, some out-of-band
770 		 * data may creep in... ick.
771 		 */
772 		if (ti->ti_urp <= ti->ti_len)
773 			tcp_pulloutofband(so, ti);
774 	}
775 badurp:							/* XXX */
776 
777 	/*
778 	 * Process the segment text, merging it into the TCP sequencing queue,
779 	 * and arranging for acknowledgment of receipt if necessary.
780 	 * This process logically involves adjusting tp->rcv_wnd as data
781 	 * is presented to the user (this happens in tcp_usrreq.c,
782 	 * case PRU_RCVD).  If a FIN has already been received on this
783 	 * connection then we just ignore the text.
784 	 */
785 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
786 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
787 		TCP_REASS(tp, ti, m, so, tiflags);
788 		if (tcpnodelack == 0)
789 			tp->t_flags |= TF_DELACK;
790 		else
791 			tp->t_flags |= TF_ACKNOW;
792 	} else {
793 		m_freem(m);
794 		tiflags &= ~TH_FIN;
795 	}
796 
797 	/*
798 	 * If FIN is received ACK the FIN and let the user know
799 	 * that the connection is closing.
800 	 */
801 	if (tiflags & TH_FIN) {
802 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
803 			socantrcvmore(so);
804 			tp->t_flags |= TF_ACKNOW;
805 			tp->rcv_nxt++;
806 		}
807 		switch (tp->t_state) {
808 
809 	 	/*
810 		 * In SYN_RECEIVED and ESTABLISHED STATES
811 		 * enter the CLOSE_WAIT state.
812 		 */
813 		case TCPS_SYN_RECEIVED:
814 		case TCPS_ESTABLISHED:
815 			tp->t_state = TCPS_CLOSE_WAIT;
816 			break;
817 
818 	 	/*
819 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
820 		 * enter the CLOSING state.
821 		 */
822 		case TCPS_FIN_WAIT_1:
823 			tp->t_state = TCPS_CLOSING;
824 			break;
825 
826 	 	/*
827 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
828 		 * starting the time-wait timer, turning off the other
829 		 * standard timers.
830 		 */
831 		case TCPS_FIN_WAIT_2:
832 			tp->t_state = TCPS_TIME_WAIT;
833 			tcp_canceltimers(tp);
834 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
835 			soisdisconnected(so);
836 			break;
837 
838 		/*
839 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
840 		 */
841 		case TCPS_TIME_WAIT:
842 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
843 			break;
844 		}
845 	}
846 	if (so->so_options & SO_DEBUG)
847 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
848 
849 	/*
850 	 * Return any desired output.
851 	 */
852 	(void) tcp_output(tp);
853 	return;
854 
855 dropafterack:
856 	/*
857 	 * Generate an ACK dropping incoming segment if it occupies
858 	 * sequence space, where the ACK reflects our state.
859 	 */
860 	if ((tiflags&TH_RST) ||
861 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
862 		goto drop;
863 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
864 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
865 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
866 	return;
867 
868 dropwithreset:
869 	if (om) {
870 		(void) m_free(om);
871 		om = 0;
872 	}
873 	/*
874 	 * Generate a RST, dropping incoming segment.
875 	 * Make ACK acceptable to originator of segment.
876 	 */
877 	if (tiflags & TH_RST)
878 		goto drop;
879 	if (tiflags & TH_ACK)
880 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
881 	else {
882 		if (tiflags & TH_SYN)
883 			ti->ti_len++;
884 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
885 		    TH_RST|TH_ACK);
886 	}
887 	/* destroy temporarily created socket */
888 	if (dropsocket)
889 		(void) soabort(so);
890 	return;
891 
892 drop:
893 	if (om)
894 		(void) m_free(om);
895 	/*
896 	 * Drop space held by incoming segment and return.
897 	 */
898 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
899 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
900 	m_freem(m);
901 	/* destroy temporarily created socket */
902 	if (dropsocket)
903 		(void) soabort(so);
904 	return;
905 }
906 
907 tcp_dooptions(tp, om, ti)
908 	struct tcpcb *tp;
909 	struct mbuf *om;
910 	struct tcpiphdr *ti;
911 {
912 	register u_char *cp;
913 	int opt, optlen, cnt;
914 
915 	cp = mtod(om, u_char *);
916 	cnt = om->m_len;
917 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
918 		opt = cp[0];
919 		if (opt == TCPOPT_EOL)
920 			break;
921 		if (opt == TCPOPT_NOP)
922 			optlen = 1;
923 		else {
924 			optlen = cp[1];
925 			if (optlen <= 0)
926 				break;
927 		}
928 		switch (opt) {
929 
930 		default:
931 			break;
932 
933 		case TCPOPT_MAXSEG:
934 			if (optlen != 4)
935 				continue;
936 			if (!(ti->ti_flags & TH_SYN))
937 				continue;
938 			tp->t_maxseg = *(u_short *)(cp + 2);
939 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
940 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
941 			break;
942 		}
943 	}
944 	(void) m_free(om);
945 }
946 
947 /*
948  * Pull out of band byte out of a segment so
949  * it doesn't appear in the user's data queue.
950  * It is still reflected in the segment length for
951  * sequencing purposes.
952  */
953 tcp_pulloutofband(so, ti)
954 	struct socket *so;
955 	struct tcpiphdr *ti;
956 {
957 	register struct mbuf *m;
958 	int cnt = ti->ti_urp - 1;
959 
960 	m = dtom(ti);
961 	while (cnt >= 0) {
962 		if (m->m_len > cnt) {
963 			char *cp = mtod(m, caddr_t) + cnt;
964 			struct tcpcb *tp = sototcpcb(so);
965 
966 			tp->t_iobc = *cp;
967 			tp->t_oobflags |= TCPOOB_HAVEDATA;
968 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
969 			m->m_len--;
970 			return;
971 		}
972 		cnt -= m->m_len;
973 		m = m->m_next;
974 		if (m == 0)
975 			break;
976 	}
977 	panic("tcp_pulloutofband");
978 }
979 
980 /*
981  *  Determine a reasonable value for maxseg size.
982  *  If the route is known, use one that can be handled
983  *  on the given interface without forcing IP to fragment.
984  *  If bigger than a page (CLBYTES), round down to nearest pagesize
985  *  to utilize pagesize mbufs.
986  *  If interface pointer is unavailable, or the destination isn't local,
987  *  use a conservative size (512 or the default IP max size, but no more
988  *  than the mtu of the interface through which we route),
989  *  as we can't discover anything about intervening gateways or networks.
990  *
991  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
992  */
993 tcp_mss(tp)
994 	register struct tcpcb *tp;
995 {
996 	struct route *ro;
997 	struct ifnet *ifp;
998 	int mss;
999 	struct inpcb *inp;
1000 
1001 	inp = tp->t_inpcb;
1002 	ro = &inp->inp_route;
1003 	if ((ro->ro_rt == (struct rtentry *)0) ||
1004 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
1005 		/* No route yet, so try to acquire one */
1006 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1007 			ro->ro_dst.sa_family = AF_INET;
1008 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1009 				inp->inp_faddr;
1010 			rtalloc(ro);
1011 		}
1012 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
1013 			return (TCP_MSS);
1014 	}
1015 
1016 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1017 #if	(CLBYTES & (CLBYTES - 1)) == 0
1018 	if (mss > CLBYTES)
1019 		mss &= ~(CLBYTES-1);
1020 #else
1021 	if (mss > CLBYTES)
1022 		mss = mss / CLBYTES * CLBYTES;
1023 #endif
1024 	if (in_localaddr(inp->inp_faddr))
1025 		return (mss);
1026 	return (MIN(mss, TCP_MSS));
1027 }
1028