xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5419)
1 /*	tcp_input.c	1.48	82/01/17	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/protosw.h"
7 #include "../h/socket.h"
8 #include "../h/socketvar.h"
9 #include "../net/in.h"
10 #include "../net/in_pcb.h"
11 #include "../net/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/ip.h"
14 #include "../net/ip_var.h"
15 #include "../net/tcp.h"
16 #include "../net/tcp_fsm.h"
17 #include "../net/tcp_seq.h"
18 #include "../net/tcp_timer.h"
19 #include "../net/tcp_var.h"
20 #include "../net/tcpip.h"
21 #include "../net/tcp_debug.h"
22 #include "../errno.h"
23 
24 int	tcpprintfs = 0;
25 int	tcpcksum = 1;
26 struct	sockaddr_in tcp_in = { AF_INET };
27 struct	tcpiphdr tcp_saveti;
28 
29 struct	tcpcb *tcp_newtcpcb();
30 /*
31  * TCP input routine, follows pages 65-76 of the
32  * protocol specification dated September, 1981 very closely.
33  */
34 tcp_input(m0)
35 	struct mbuf *m0;
36 {
37 	register struct tcpiphdr *ti;
38 	struct inpcb *inp;
39 	register struct mbuf *m;
40 	int len, tlen, off;
41 	register struct tcpcb *tp = 0;
42 	register int tiflags;
43 	struct socket *so;
44 	int todrop, acked;
45 	short ostate;
46 
47 COUNT(TCP_INPUT);
48 	/*
49 	 * Get IP and TCP header together in first mbuf.
50 	 * Note: IP leaves IP header in first mbuf.
51 	 */
52 	m = m0;
53 	ti = mtod(m, struct tcpiphdr *);
54 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
55 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
56 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
57 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
58 			tcpstat.tcps_hdrops++;
59 			return;
60 		}
61 		ti = mtod(m, struct tcpiphdr *);
62 	}
63 
64 	/*
65 	 * Checksum extended TCP header and data.
66 	 */
67 	tlen = ((struct ip *)ti)->ip_len;
68 	len = sizeof (struct ip) + tlen;
69 	if (tcpcksum) {
70 		ti->ti_next = ti->ti_prev = 0;
71 		ti->ti_x1 = 0;
72 		ti->ti_len = (u_short)tlen;
73 #if vax
74 		ti->ti_len = htons(ti->ti_len);
75 #endif
76 		if (ti->ti_sum = in_cksum(m, len)) {
77 			tcpstat.tcps_badsum++;
78 			printf("tcp cksum %x\n", ti->ti_sum);
79 			goto drop;
80 		}
81 	}
82 
83 	/*
84 	 * Check that TCP offset makes sense,
85 	 * process TCP options and adjust length.
86 	 */
87 	off = ti->ti_off << 2;
88 	if (off < sizeof (struct tcphdr) || off > tlen) {
89 		tcpstat.tcps_badoff++;
90 		goto drop;
91 	}
92 	ti->ti_len = tlen - off;
93 #if 0
94 	if (off > sizeof (struct tcphdr))
95 		tcp_options(ti);
96 #endif
97 	tiflags = ti->ti_flags;
98 
99 #if vax
100 	/*
101 	 * Convert TCP protocol specific fields to host format.
102 	 */
103 	ti->ti_seq = ntohl(ti->ti_seq);
104 	ti->ti_ack = ntohl(ti->ti_ack);
105 	ti->ti_win = ntohs(ti->ti_win);
106 	ti->ti_urp = ntohs(ti->ti_urp);
107 #endif
108 
109 	/*
110 	 * Locate pcb for segment.
111 	 */
112 	inp = in_pcblookup
113 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
114 
115 	/*
116 	 * If the state is CLOSED (i.e., TCB does not exist) then
117 	 * all data in the incoming segment is discarded.
118 	 */
119 	if (inp == 0)
120 		goto dropwithreset;
121 	tp = intotcpcb(inp);
122 	if (tp == 0)
123 		goto dropwithreset;
124 	so = inp->inp_socket;
125 	if (so->so_options & SO_DEBUG) {
126 		ostate = tp->t_state;
127 		tcp_saveti = *ti;
128 	}
129 
130 	/*
131 	 * Segment received on connection.
132 	 * Reset idle time and keep-alive timer.
133 	 */
134 	tp->t_idle = 0;
135 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
136 
137 	/*
138 	 * Calculate amount of space in receive window,
139 	 * and then do TCP input processing.
140 	 */
141 	tp->rcv_wnd = sbspace(&so->so_rcv);
142 	if (tp->rcv_wnd < 0)
143 		tp->rcv_wnd = 0;
144 
145 	switch (tp->t_state) {
146 
147 	/*
148 	 * If the state is LISTEN then ignore segment if it contains an RST.
149 	 * If the segment contains an ACK then it is bad and send a RST.
150 	 * If it does not contain a SYN then it is not interesting; drop it.
151 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
152 	 * tp->iss, and send a segment:
153 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
154 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
155 	 * Fill in remote peer address fields if not previously specified.
156 	 * Enter SYN_RECEIVED state, and process any other fields of this
157 	 * segment in this state.
158 	 */
159 	case TCPS_LISTEN:
160 		if (tiflags & TH_RST)
161 			goto drop;
162 		if (tiflags & TH_ACK)
163 			goto dropwithreset;
164 		if ((tiflags & TH_SYN) == 0)
165 			goto drop;
166 		tcp_in.sin_addr = ti->ti_src;
167 		tcp_in.sin_port = ti->ti_sport;
168 		if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in))
169 			goto drop;
170 		tp->t_template = tcp_template(tp);
171 		if (tp->t_template == 0) {
172 			in_pcbdisconnect(inp);
173 			goto drop;
174 		}
175 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
176 		tp->irs = ti->ti_seq;
177 		tcp_sendseqinit(tp);
178 		tcp_rcvseqinit(tp);
179 		tp->t_state = TCPS_SYN_RECEIVED;
180 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
181 		goto trimthenstep6;
182 
183 	/*
184 	 * If the state is SYN_SENT:
185 	 *	if seg contains an ACK, but not for our SYN, drop the input.
186 	 *	if seg contains a RST, then drop the connection.
187 	 *	if seg does not contain SYN, then drop it.
188 	 * Otherwise this is an acceptable SYN segment
189 	 *	initialize tp->rcv_nxt and tp->irs
190 	 *	if seg contains ack then advance tp->snd_una
191 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
192 	 *	arrange for segment to be acked (eventually)
193 	 *	continue processing rest of data/controls, beginning with URG
194 	 */
195 	case TCPS_SYN_SENT:
196 		if ((tiflags & TH_ACK) &&
197 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
198 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
199 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
200 			goto dropwithreset;
201 		if (tiflags & TH_RST) {
202 			if (tiflags & TH_ACK)
203 				tcp_drop(tp, ECONNREFUSED);
204 			goto drop;
205 		}
206 		if ((tiflags & TH_SYN) == 0)
207 			goto drop;
208 		tp->snd_una = ti->ti_ack;
209 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
210 			tp->snd_nxt = tp->snd_una;
211 		tp->t_timer[TCPT_REXMT] = 0;
212 		tp->irs = ti->ti_seq;
213 		tcp_rcvseqinit(tp);
214 		tp->t_flags |= TF_ACKNOW;
215 		if (SEQ_GT(tp->snd_una, tp->iss)) {
216 			if (so->so_options & SO_ACCEPTCONN)
217 				so->so_state |= SS_CONNAWAITING;
218 			soisconnected(so);
219 			tp->t_state = TCPS_ESTABLISHED;
220 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
221 		} else
222 			tp->t_state = TCPS_SYN_RECEIVED;
223 		goto trimthenstep6;
224 
225 trimthenstep6:
226 		/*
227 		 * Advance ti->ti_seq to correspond to first data byte.
228 		 * If data, trim to stay within window,
229 		 * dropping FIN if necessary.
230 		 */
231 		ti->ti_seq++;
232 		if (ti->ti_len > tp->rcv_wnd) {
233 			todrop = ti->ti_len - tp->rcv_wnd;
234 			m_adj(m, -todrop);
235 			ti->ti_len = tp->rcv_wnd;
236 			ti->ti_flags &= ~TH_FIN;
237 		}
238 		tp->snd_wl1 = ti->ti_seq - 1;
239 		goto step6;
240 	}
241 
242 	/*
243 	 * States other than LISTEN or SYN_SENT.
244 	 * First check that at least some bytes of segment are within
245 	 * receive window.
246 	 */
247 	if (tp->rcv_wnd == 0) {
248 		/*
249 		 * If window is closed can only take segments at
250 		 * window edge, and have to drop data and PUSH from
251 		 * incoming segments.
252 		 */
253 		if (tp->rcv_nxt != ti->ti_seq)
254 			goto dropafterack;
255 		if (ti->ti_len > 0) {
256 			ti->ti_len = 0;
257 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
258 		}
259 	} else {
260 		/*
261 		 * If segment begins before rcv_nxt, drop leading
262 		 * data (and SYN); if nothing left, just ack.
263 		 */
264 		if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) {
265 			todrop = tp->rcv_nxt - ti->ti_seq;
266 			if (tiflags & TH_SYN) {
267 				tiflags &= ~TH_SYN;
268 				ti->ti_seq++;
269 				if (ti->ti_urp > 1)
270 					ti->ti_urp--;
271 				else
272 					tiflags &= ~TH_URG;
273 				todrop--;
274 			}
275 			if (todrop > ti->ti_len)
276 				goto dropafterack;
277 			m_adj(m, todrop);
278 			ti->ti_seq += todrop;
279 			ti->ti_len -= todrop;
280 			if (ti->ti_urp > todrop)
281 				ti->ti_urp -= todrop;
282 			else {
283 				tiflags &= ~TH_URG;
284 				/* ti->ti_flags &= ~TH_URG; */
285 				/* ti->ti_urp = 0; */
286 			}
287 			/* tiflags &= ~TH_SYN; */
288 			/* ti->ti_flags &= ~TH_SYN; */
289 		}
290 		/*
291 		 * If segment ends after window, drop trailing data
292 		 * (and PUSH and FIN); if nothing left, just ACK.
293 		 */
294 		if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) {
295 			todrop =
296 			     ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd);
297 			if (todrop > ti->ti_len)
298 				goto dropafterack;
299 			m_adj(m, -todrop);
300 			ti->ti_len -= todrop;
301 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
302 		}
303 	}
304 
305 	/*
306 	 * If the RST bit is set examine the state:
307 	 *    SYN_RECEIVED STATE:
308 	 *	If passive open, return to LISTEN state.
309 	 *	If active open, inform user that connection was refused.
310 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
311 	 *	Inform user that connection was reset, and close tcb.
312 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
313 	 *	Close the tcb.
314 	 */
315 	if (tiflags&TH_RST) switch (tp->t_state) {
316 
317 	case TCPS_SYN_RECEIVED:
318 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
319 			/* a miniature tcp_close, but invisible to user */
320 			(void) m_free(dtom(tp->t_template));
321 			(void) m_free(dtom(tp));
322 			inp->inp_ppcb = 0;
323 			tp = tcp_newtcpcb(inp);
324 			tp->t_state = TCPS_LISTEN;
325 			goto drop;
326 		}
327 		tcp_drop(tp, ECONNREFUSED);
328 		goto drop;
329 
330 	case TCPS_ESTABLISHED:
331 	case TCPS_FIN_WAIT_1:
332 	case TCPS_FIN_WAIT_2:
333 	case TCPS_CLOSE_WAIT:
334 		tcp_drop(tp, ECONNRESET);
335 		goto drop;
336 
337 	case TCPS_CLOSING:
338 	case TCPS_LAST_ACK:
339 	case TCPS_TIME_WAIT:
340 		tcp_close(tp);
341 		goto drop;
342 	}
343 
344 	/*
345 	 * If a SYN is in the window, then this is an
346 	 * error and we send an RST and drop the connection.
347 	 */
348 	if (tiflags & TH_SYN) {
349 		tcp_drop(tp, ECONNRESET);
350 		goto dropwithreset;
351 	}
352 
353 	/*
354 	 * If the ACK bit is off we drop the segment and return.
355 	 */
356 	if ((tiflags & TH_ACK) == 0)
357 		goto drop;
358 
359 	/*
360 	 * Ack processing.
361 	 */
362 	switch (tp->t_state) {
363 
364 	/*
365 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
366 	 * ESTABLISHED state and continue processing, othewise
367 	 * send an RST.
368 	 */
369 	case TCPS_SYN_RECEIVED:
370 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
371 		    SEQ_GT(ti->ti_ack, tp->snd_max))
372 			goto dropwithreset;
373 		tp->snd_una++;			/* SYN acked */
374 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
375 			tp->snd_nxt = tp->snd_una;
376 		tp->t_timer[TCPT_REXMT] = 0;
377 		if (so->so_options & SO_ACCEPTCONN)
378 			so->so_state |= SS_CONNAWAITING;
379 		soisconnected(so);
380 		tp->t_state = TCPS_ESTABLISHED;
381 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
382 		tp->snd_wl1 = ti->ti_seq - 1;
383 		/* fall into ... */
384 
385 	/*
386 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
387 	 * ACKs.  If the ack is in the range
388 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
389 	 * then advance tp->snd_una to ti->ti_ack and drop
390 	 * data from the retransmission queue.  If this ACK reflects
391 	 * more up to date window information we update our window information.
392 	 */
393 	case TCPS_ESTABLISHED:
394 	case TCPS_FIN_WAIT_1:
395 	case TCPS_FIN_WAIT_2:
396 	case TCPS_CLOSE_WAIT:
397 	case TCPS_CLOSING:
398 	case TCPS_LAST_ACK:
399 	case TCPS_TIME_WAIT:
400 #define	ourfinisacked	(acked > 0)
401 
402 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
403 			break;
404 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
405 			goto dropafterack;
406 		acked = ti->ti_ack - tp->snd_una;
407 		if (ti->ti_ack == tp->snd_max)
408 			tp->t_timer[TCPT_REXMT] = 0;
409 		else {
410 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
411 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
412 			tp->t_rtt = 0;
413 			tp->t_rxtshift = 0;
414 		}
415 		if (acked > so->so_snd.sb_cc) {
416 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
417 			tp->snd_wnd -= so->so_snd.sb_cc;
418 		} else {
419 			sbdrop(&so->so_snd.sb_cc, acked);
420 			tp->snd_wnd -= acked;
421 			acked = 0;
422 		}
423 		if (so->so_snd.sb_flags & SB_WAIT)
424 			sowwakeup(so);
425 		tp->snd_una = ti->ti_ack;
426 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
427 			tp->snd_nxt = tp->snd_una;
428 
429 		/*
430 		 * If transmit timer is running and timed sequence
431 		 * number was acked, update smoothed round trip time.
432 		 */
433 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
434 			if (tp->t_srtt == 0)
435 				tp->t_srtt = tp->t_rtt;
436 			else
437 				tp->t_srtt =
438 				    tcp_alpha * tp->t_srtt +
439 				    (1 - tcp_alpha) * tp->t_rtt;
440 			tp->t_rtt = 0;
441 		}
442 
443 		switch (tp->t_state) {
444 
445 		/*
446 		 * In FIN_WAIT_1 STATE in addition to the processing
447 		 * for the ESTABLISHED state if our FIN is now acknowledged
448 		 * then enter FIN_WAIT_2.
449 		 */
450 		case TCPS_FIN_WAIT_1:
451 			if (ourfinisacked)
452 				tp->t_state = TCPS_FIN_WAIT_2;
453 			break;
454 
455 	 	/*
456 		 * In CLOSING STATE in addition to the processing for
457 		 * the ESTABLISHED state if the ACK acknowledges our FIN
458 		 * then enter the TIME-WAIT state, otherwise ignore
459 		 * the segment.
460 		 */
461 		case TCPS_CLOSING:
462 			if (ourfinisacked) {
463 				tp->t_state = TCPS_TIME_WAIT;
464 				tcp_canceltimers(tp);
465 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
466 				soisdisconnected(so);
467 			}
468 			break;
469 
470 		/*
471 		 * The only thing that can arrive in  LAST_ACK state
472 		 * is an acknowledgment of our FIN.  If our FIN is now
473 		 * acknowledged, delete the TCB, enter the closed state
474 		 * and return.
475 		 */
476 		case TCPS_LAST_ACK:
477 			if (ourfinisacked)
478 				tcp_close(tp);
479 			goto drop;
480 
481 		/*
482 		 * In TIME_WAIT state the only thing that should arrive
483 		 * is a retransmission of the remote FIN.  Acknowledge
484 		 * it and restart the finack timer.
485 		 */
486 		case TCPS_TIME_WAIT:
487 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
488 			goto dropafterack;
489 		}
490 #undef ourfinisacked
491 	}
492 
493 step6:
494 	/*
495 	 * Update window information.
496 	 */
497 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
498 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
499 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
500 /*
501 printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd);
502 */
503 		tp->snd_wnd = ti->ti_win;
504 		tp->snd_wl1 = ti->ti_seq;
505 		tp->snd_wl2 = ti->ti_ack;
506 		if (tp->snd_wnd > 0)
507 			tp->t_timer[TCPT_PERSIST] = 0;
508 	}
509 
510 	/*
511 	 * If an URG bit is set and in the segment and is greater than the
512 	 * current known urgent pointer, then signal the user that the
513 	 * remote side has out of band data.  This should not happen
514 	 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since
515 	 * a FIN has been received from the remote side.  In these states
516 	 * we ignore the URG.
517 	 */
518 	if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0 &&
519 	    ti->ti_urp <= ti->ti_len &&
520 	    SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
521 		tp->rcv_up = ti->ti_seq + ti->ti_urp;
522 		so->so_oobmark = so->so_rcv.sb_cc +
523 		    (tp->rcv_up - tp->rcv_nxt) - 1;
524 		if (so->so_oobmark == 0)
525 			so->so_state |= SS_RCVATMARK;
526 		tcp_pulloutofband(so, ti);
527 		sohasoutofband(so);
528 	}
529 
530 	/*
531 	 * Process the segment text, merging it into the TCP sequencing queue,
532 	 * and arranging for acknowledgment of receipt if necessary.
533 	 * This process logically involves adjusting tp->rcv_wnd as data
534 	 * is presented to the user (this happens in tcp_usrreq.c,
535 	 * case PRU_RCVD).  If a FIN has already been received on this
536 	 * connection then we just ignore the text.
537 	 */
538 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
539 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
540 		off += sizeof (struct ip);		/* drop IP header */
541 		m->m_off += off;
542 		m->m_len -= off;
543 		tiflags = tcp_reass(tp, ti);
544 { extern tcpdelack;
545 if (tcpdelack) tp->t_flags |= TF_DELACK; else
546 		tp->t_flags |= TF_ACKNOW;		/* XXX TF_DELACK */
547 }
548 	} else {
549 		m_freem(m);
550 		tiflags &= ~TH_FIN;
551 	}
552 
553 	/*
554 	 * If FIN is received ACK the FIN and let the user know
555 	 * that the connection is closing.
556 	 */
557 	if (tiflags & TH_FIN) {
558 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
559 			socantrcvmore(so);
560 			tp->t_flags |= TF_ACKNOW;
561 			tp->rcv_nxt++;
562 		}
563 		switch (tp->t_state) {
564 
565 	 	/*
566 		 * In SYN_RECEIVED and ESTABLISHED STATES
567 		 * enter the CLOSE_WAIT state.
568 		 */
569 		case TCPS_SYN_RECEIVED:
570 		case TCPS_ESTABLISHED:
571 			tp->t_state = TCPS_CLOSE_WAIT;
572 			break;
573 
574 	 	/*
575 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
576 		 * enter the CLOSING state.
577 		 */
578 		case TCPS_FIN_WAIT_1:
579 			tp->t_state = TCPS_CLOSING;
580 			break;
581 
582 	 	/*
583 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
584 		 * starting the time-wait timer, turning off the other
585 		 * standard timers.
586 		 */
587 		case TCPS_FIN_WAIT_2:
588 			tp->t_state = TCPS_TIME_WAIT;
589 			tcp_canceltimers(tp);
590 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
591 			soisdisconnected(so);
592 			break;
593 
594 		/*
595 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
596 		 */
597 		case TCPS_TIME_WAIT:
598 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
599 			break;
600 		}
601 	}
602 	if (so->so_options & SO_DEBUG)
603 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
604 
605 	/*
606 	 * Return any desired output.
607 	 */
608 	tcp_output(tp);
609 	return;
610 
611 dropafterack:
612 	/*
613 	 * Generate an ACK dropping incoming segment.
614 	 * Make ACK reflect our state.
615 	 */
616 	if (tiflags & TH_RST)
617 		goto drop;
618 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
619 	return;
620 
621 dropwithreset:
622 	/*
623 	 * Generate a RST, dropping incoming segment.
624 	 * Make ACK acceptable to originator of segment.
625 	 */
626 	if (tiflags & TH_RST)
627 		goto drop;
628 	if (tiflags & TH_ACK)
629 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
630 	else {
631 		if (tiflags & TH_SYN)
632 			ti->ti_len++;
633 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
634 	}
635 	return;
636 
637 drop:
638 	/*
639 	 * Drop space held by incoming segment and return.
640 	 */
641 	m_freem(m);
642 	return;
643 }
644 
645 /*
646  * Pull the character before the urgent pointer into
647  * the TCP control block for presentation as out-of-band data.
648  * We leave ti->ti_len reflecting the out-of-band data,
649  * so that sequencing will continue to work.
650  */
651 tcp_pulloutofband(so, ti)
652 	struct socket *so;
653 	struct tcpiphdr *ti;
654 {
655 	register struct mbuf *m;
656 	int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1;
657 
658 	m = dtom(ti);
659 	while (cnt >= 0) {
660 		if (m->m_len > cnt) {
661 			char *cp = mtod(m, caddr_t) + cnt;
662 			struct tcpcb *tp = sototcpcb(so);
663 
664 			tp->t_oobc = *cp;
665 			tp->t_haveoob = 1;
666 			bcopy(cp+1, cp, m->m_len - cnt - 1);
667 			m->m_len--;
668 			return;
669 		}
670 		cnt -= m->m_len;
671 		m = m->m_next;
672 		if (m == 0)
673 			break;
674 	}
675 	panic("tcp_pulloutofband");
676 }
677 
678 /*
679  * Insert segment ti into reassembly queue of tcp with
680  * control block tp.  Return TH_FIN if reassembly now includes
681  * a segment with FIN.
682  */
683 tcp_reass(tp, ti)
684 	register struct tcpcb *tp;
685 	register struct tcpiphdr *ti;
686 {
687 	register struct tcpiphdr *q;
688 	struct socket *so = tp->t_inpcb->inp_socket;
689 	struct mbuf *m;
690 	int flags;
691 COUNT(TCP_REASS);
692 
693 	/*
694 	 * Call with ti==0 after become established to
695 	 * force pre-ESTABLISHED data up to user socket.
696 	 */
697 	if (ti == 0)
698 		goto present;
699 
700 	/*
701 	 * Find a segment which begins after this one does.
702 	 */
703 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
704 	    q = (struct tcpiphdr *)q->ti_next)
705 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
706 			break;
707 
708 	/*
709 	 * If there is a preceding segment, it may provide some of
710 	 * our data already.  If so, drop the data from the incoming
711 	 * segment.  If it provides all of our data, drop us.
712 	 */
713 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
714 		register int i;
715 		q = (struct tcpiphdr *)(q->ti_prev);
716 		/* conversion to int (in i) handles seq wraparound */
717 		i = q->ti_seq + q->ti_len - ti->ti_seq;
718 		if (i > 0) {
719 			if (i >= ti->ti_len)
720 				goto drop;
721 			m_adj(dtom(tp), i);
722 			ti->ti_len -= i;
723 			ti->ti_seq += i;
724 		}
725 		q = (struct tcpiphdr *)(q->ti_next);
726 	}
727 
728 	/*
729 	 * While we overlap succeeding segments trim them or,
730 	 * if they are completely covered, dequeue them.
731 	 */
732 	while (q != (struct tcpiphdr *)tp &&
733 	    SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) {
734 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
735 		if (i < q->ti_len) {
736 			q->ti_len -= i;
737 			m_adj(dtom(q), i);
738 			break;
739 		}
740 		q = (struct tcpiphdr *)q->ti_next;
741 		m_freem(dtom(q->ti_prev));
742 		remque(q->ti_prev);
743 	}
744 
745 	/*
746 	 * Stick new segment in its place.
747 	 */
748 	insque(ti, q->ti_prev);
749 
750 present:
751 	/*
752 	 * Present data to user, advancing rcv_nxt through
753 	 * completed sequence space.
754 	 */
755 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
756 		return (0);
757 	ti = tp->seg_next;
758 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
759 		return (0);
760 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
761 		return (0);
762 	do {
763 		tp->rcv_nxt += ti->ti_len;
764 		flags = ti->ti_flags & TH_FIN;
765 		remque(ti);
766 		m = dtom(ti);
767 		ti = (struct tcpiphdr *)ti->ti_next;
768 		if (so->so_state & SS_CANTRCVMORE)
769 			(void) m_freem(m);
770 		else
771 			sbappend(&so->so_rcv, m);
772 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
773 	sorwakeup(so);
774 	return (flags);
775 drop:
776 	m_freem(dtom(ti));
777 	return (0);
778 }
779