xref: /csrg-svn/sys/netinet/tcp_input.c (revision 5357)
1 /*	tcp_input.c	1.46	82/01/07	*/
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;
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 			so->so_state |= SS_CONNAWAITING;
217 			soisconnected(so);
218 			tp->t_state = TCPS_ESTABLISHED;
219 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
220 		} else
221 			tp->t_state = TCPS_SYN_RECEIVED;
222 		goto trimthenstep6;
223 
224 trimthenstep6:
225 		/*
226 		 * Advance ti->ti_seq to correspond to first data byte.
227 		 * If data, trim to stay within window,
228 		 * dropping FIN if necessary.
229 		 */
230 		ti->ti_seq++;
231 		if (ti->ti_len > tp->rcv_wnd) {
232 			todrop = ti->ti_len - tp->rcv_wnd;
233 			m_adj(m, -todrop);
234 			ti->ti_len = tp->rcv_wnd;
235 			ti->ti_flags &= ~TH_FIN;
236 		}
237 		tp->snd_wl1 = ti->ti_seq - 1;
238 		goto step6;
239 	}
240 
241 	/*
242 	 * States other than LISTEN or SYN_SENT.
243 	 * First check that at least some bytes of segment are within
244 	 * receive window.
245 	 */
246 	if (tp->rcv_wnd == 0) {
247 		/*
248 		 * If window is closed can only take segments at
249 		 * window edge, and have to drop data and PUSH from
250 		 * incoming segments.
251 		 */
252 		if (tp->rcv_nxt != ti->ti_seq)
253 			goto dropafterack;
254 		if (ti->ti_len > 0) {
255 			ti->ti_len = 0;
256 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
257 		}
258 	} else {
259 		/*
260 		 * If segment begins before rcv_nxt, drop leading
261 		 * data (and SYN); if nothing left, just ack.
262 		 */
263 		if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) {
264 			todrop = tp->rcv_nxt - ti->ti_seq;
265 			if (tiflags & TH_SYN) {
266 				tiflags &= ~TH_SYN;
267 				ti->ti_seq++;
268 				if (ti->ti_urp > 1)
269 					ti->ti_urp--;
270 				else
271 					tiflags &= ~TH_URG;
272 				todrop--;
273 			}
274 			if (todrop > ti->ti_len)
275 				goto dropafterack;
276 			m_adj(m, todrop);
277 			ti->ti_seq += todrop;
278 			ti->ti_len -= todrop;
279 			if (ti->ti_urp > todrop)
280 				ti->ti_urp -= todrop;
281 			else {
282 				tiflags &= ~TH_URG;
283 				/* ti->ti_flags &= ~TH_URG; */
284 				/* ti->ti_urp = 0; */
285 			}
286 			/* tiflags &= ~TH_SYN; */
287 			/* ti->ti_flags &= ~TH_SYN; */
288 		}
289 		/*
290 		 * If segment ends after window, drop trailing data
291 		 * (and PUSH and FIN); if nothing left, just ACK.
292 		 */
293 		if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) {
294 			todrop =
295 			     ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd);
296 			if (todrop > ti->ti_len)
297 				goto dropafterack;
298 			m_adj(m, -todrop);
299 			ti->ti_len -= todrop;
300 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
301 		}
302 	}
303 
304 	/*
305 	 * If the RST bit is set examine the state:
306 	 *    SYN_RECEIVED STATE:
307 	 *	If passive open, return to LISTEN state.
308 	 *	If active open, inform user that connection was refused.
309 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
310 	 *	Inform user that connection was reset, and close tcb.
311 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
312 	 *	Close the tcb.
313 	 */
314 	if (tiflags&TH_RST) switch (tp->t_state) {
315 
316 	case TCPS_SYN_RECEIVED:
317 		if (inp->inp_socket->so_options & SO_ACCEPTCONN) {
318 			/* a miniature tcp_close, but invisible to user */
319 			(void) m_free(dtom(tp->t_template));
320 			(void) m_free(dtom(tp));
321 			inp->inp_ppcb = 0;
322 			tp = tcp_newtcpcb(inp);
323 			tp->t_state = TCPS_LISTEN;
324 			goto drop;
325 		}
326 		tcp_drop(tp, ECONNREFUSED);
327 		goto drop;
328 
329 	case TCPS_ESTABLISHED:
330 	case TCPS_FIN_WAIT_1:
331 	case TCPS_FIN_WAIT_2:
332 	case TCPS_CLOSE_WAIT:
333 		tcp_drop(tp, ECONNRESET);
334 		goto drop;
335 
336 	case TCPS_CLOSING:
337 	case TCPS_LAST_ACK:
338 	case TCPS_TIME_WAIT:
339 		tcp_close(tp);
340 		goto drop;
341 	}
342 
343 	/*
344 	 * If a SYN is in the window, then this is an
345 	 * error and we send an RST and drop the connection.
346 	 */
347 	if (tiflags & TH_SYN) {
348 		tcp_drop(tp, ECONNRESET);
349 		goto dropwithreset;
350 	}
351 
352 	/*
353 	 * If the ACK bit is off we drop the segment and return.
354 	 */
355 	if ((tiflags & TH_ACK) == 0)
356 		goto drop;
357 
358 	/*
359 	 * Ack processing.
360 	 */
361 	switch (tp->t_state) {
362 
363 	/*
364 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
365 	 * ESTABLISHED state and continue processing, othewise
366 	 * send an RST.
367 	 */
368 	case TCPS_SYN_RECEIVED:
369 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
370 		    SEQ_GT(ti->ti_ack, tp->snd_max))
371 			goto dropwithreset;
372 		tp->snd_una++;			/* SYN acked */
373 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
374 			tp->snd_nxt = tp->snd_una;
375 		tp->t_timer[TCPT_REXMT] = 0;
376 		so->so_state |= SS_CONNAWAITING;
377 		soisconnected(so);
378 		tp->t_state = TCPS_ESTABLISHED;
379 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
380 		tp->snd_wl1 = ti->ti_seq - 1;
381 		/* fall into ... */
382 
383 	/*
384 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
385 	 * ACKs.  If the ack is in the range
386 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
387 	 * then advance tp->snd_una to ti->ti_ack and drop
388 	 * data from the retransmission queue.  If this ACK reflects
389 	 * more up to date window information we update our window information.
390 	 */
391 	case TCPS_ESTABLISHED:
392 	case TCPS_FIN_WAIT_1:
393 	case TCPS_FIN_WAIT_2:
394 	case TCPS_CLOSE_WAIT:
395 	case TCPS_CLOSING:
396 	case TCPS_LAST_ACK:
397 	case TCPS_TIME_WAIT:
398 #define	ourfinisacked	(acked > 0)
399 
400 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
401 			break;
402 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
403 			goto dropafterack;
404 		acked = ti->ti_ack - tp->snd_una;
405 		if (ti->ti_ack == tp->snd_max)
406 			tp->t_timer[TCPT_REXMT] = 0;
407 		else {
408 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
409 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
410 			tp->t_rtt = 0;
411 			tp->t_rxtshift = 0;
412 		}
413 		if (acked > so->so_snd.sb_cc) {
414 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
415 			tp->snd_wnd -= so->so_snd.sb_cc;
416 		} else {
417 			sbdrop(&so->so_snd.sb_cc, acked);
418 			tp->snd_wnd -= acked;
419 			acked = 0;
420 		}
421 		if (so->so_snd.sb_flags & SB_WAIT)
422 			sowwakeup(so);
423 		tp->snd_una = ti->ti_ack;
424 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
425 			tp->snd_nxt = tp->snd_una;
426 
427 		/*
428 		 * If transmit timer is running and timed sequence
429 		 * number was acked, update smoothed round trip time.
430 		 */
431 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
432 			if (tp->t_srtt == 0)
433 				tp->t_srtt = tp->t_rtt;
434 			else
435 				tp->t_srtt =
436 				    tcp_alpha * tp->t_srtt +
437 				    (1 - tcp_alpha) * tp->t_rtt;
438 			tp->t_rtt = 0;
439 		}
440 
441 		switch (tp->t_state) {
442 
443 		/*
444 		 * In FIN_WAIT_1 STATE in addition to the processing
445 		 * for the ESTABLISHED state if our FIN is now acknowledged
446 		 * then enter FIN_WAIT_2.
447 		 */
448 		case TCPS_FIN_WAIT_1:
449 			if (ourfinisacked)
450 				tp->t_state = TCPS_FIN_WAIT_2;
451 			break;
452 
453 	 	/*
454 		 * In CLOSING STATE in addition to the processing for
455 		 * the ESTABLISHED state if the ACK acknowledges our FIN
456 		 * then enter the TIME-WAIT state, otherwise ignore
457 		 * the segment.
458 		 */
459 		case TCPS_CLOSING:
460 			if (ourfinisacked) {
461 				tp->t_state = TCPS_TIME_WAIT;
462 				tcp_canceltimers(tp);
463 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
464 				soisdisconnected(so);
465 			}
466 			break;
467 
468 		/*
469 		 * The only thing that can arrive in  LAST_ACK state
470 		 * is an acknowledgment of our FIN.  If our FIN is now
471 		 * acknowledged, delete the TCB, enter the closed state
472 		 * and return.
473 		 */
474 		case TCPS_LAST_ACK:
475 			if (ourfinisacked)
476 				tcp_close(tp);
477 			goto drop;
478 
479 		/*
480 		 * In TIME_WAIT state the only thing that should arrive
481 		 * is a retransmission of the remote FIN.  Acknowledge
482 		 * it and restart the finack timer.
483 		 */
484 		case TCPS_TIME_WAIT:
485 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
486 			goto dropafterack;
487 		}
488 #undef ourfinisacked
489 	}
490 
491 step6:
492 	/*
493 	 * Update window information.
494 	 */
495 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
496 	    (SEQ_LEQ(tp->snd_wl2, ti->ti_ack) ||
497 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
498 		tp->snd_wnd = ti->ti_win;
499 		tp->snd_wl1 = ti->ti_seq;
500 		tp->snd_wl2 = ti->ti_ack;
501 		if (tp->snd_wnd > 0)
502 			tp->t_timer[TCPT_PERSIST] = 0;
503 	}
504 
505 	/*
506 	 * If an URG bit is set in the segment and is greater than the
507 	 * current known urgent pointer, then signal the user that the
508 	 * remote side has out of band data.  This should not happen
509 	 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since
510 	 * a FIN has been received from the remote side.  In these states
511 	 * we ignore the URG.
512 	 */
513 	if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0)
514 		if (SEQ_GT(ti->ti_urp, tp->rcv_up)) {
515 			tp->rcv_up = ti->ti_urp;
516 #if 0
517 			sohasoutofband(so);		/* XXX */
518 #endif
519 		}
520 
521 	/*
522 	 * Process the segment text, merging it into the TCP sequencing queue,
523 	 * and arranging for acknowledgment of receipt if necessary.
524 	 * This process logically involves adjusting tp->rcv_wnd as data
525 	 * is presented to the user (this happens in tcp_usrreq.c,
526 	 * case PRU_RCVD).  If a FIN has already been received on this
527 	 * connection then we just ignore the text.
528 	 */
529 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
530 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
531 		off += sizeof (struct ip);		/* drop IP header */
532 		m->m_off += off;
533 		m->m_len -= off;
534 		tiflags = tcp_reass(tp, ti);
535 { extern tcpdelack;
536 if (tcpdelack) tp->t_flags |= TF_DELACK; else
537 		tp->t_flags |= TF_ACKNOW;		/* XXX TF_DELACK */
538 }
539 	} else {
540 		m_freem(m);
541 		tiflags &= ~TH_FIN;
542 	}
543 
544 	/*
545 	 * If FIN is received ACK the FIN and let the user know
546 	 * that the connection is closing.
547 	 */
548 	if (tiflags & TH_FIN) {
549 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
550 			socantrcvmore(so);
551 			tp->t_flags |= TF_ACKNOW;
552 			tp->rcv_nxt++;
553 		}
554 		switch (tp->t_state) {
555 
556 	 	/*
557 		 * In SYN_RECEIVED and ESTABLISHED STATES
558 		 * enter the CLOSE_WAIT state.
559 		 */
560 		case TCPS_SYN_RECEIVED:
561 		case TCPS_ESTABLISHED:
562 			tp->t_state = TCPS_CLOSE_WAIT;
563 			break;
564 
565 	 	/*
566 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
567 		 * enter the CLOSING state.
568 		 */
569 		case TCPS_FIN_WAIT_1:
570 			tp->t_state = TCPS_CLOSING;
571 			break;
572 
573 	 	/*
574 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
575 		 * starting the time-wait timer, turning off the other
576 		 * standard timers.
577 		 */
578 		case TCPS_FIN_WAIT_2:
579 			tp->t_state = TCPS_TIME_WAIT;
580 			tcp_canceltimers(tp);
581 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
582 			soisdisconnected(so);
583 			break;
584 
585 		/*
586 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
587 		 */
588 		case TCPS_TIME_WAIT:
589 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
590 			break;
591 		}
592 	}
593 	if (so->so_options & SO_DEBUG)
594 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
595 
596 	/*
597 	 * Return any desired output.
598 	 */
599 	tcp_output(tp);
600 	return;
601 
602 dropafterack:
603 	/*
604 	 * Generate an ACK dropping incoming segment.
605 	 * Make ACK reflect our state.
606 	 */
607 	if (tiflags & TH_RST)
608 		goto drop;
609 	tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
610 	return;
611 
612 dropwithreset:
613 	/*
614 	 * Generate a RST, dropping incoming segment.
615 	 * Make ACK acceptable to originator of segment.
616 	 */
617 	if (tiflags & TH_RST)
618 		goto drop;
619 	if (tiflags & TH_ACK)
620 		tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST);
621 	else {
622 		if (tiflags & TH_SYN)
623 			ti->ti_len++;
624 		tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK);
625 	}
626 	return;
627 
628 drop:
629 	/*
630 	 * Drop space held by incoming segment and return.
631 	 */
632 	m_freem(m);
633 	return;
634 }
635 
636 /*
637  * Insert segment ti into reassembly queue of tcp with
638  * control block tp.  Return TH_FIN if reassembly now includes
639  * a segment with FIN.
640  */
641 tcp_reass(tp, ti)
642 	register struct tcpcb *tp;
643 	register struct tcpiphdr *ti;
644 {
645 	register struct tcpiphdr *q;
646 	struct socket *so = tp->t_inpcb->inp_socket;
647 	struct mbuf *m;
648 	int flags;
649 COUNT(TCP_REASS);
650 
651 	/*
652 	 * Call with ti==0 after become established to
653 	 * force pre-ESTABLISHED data up to user socket.
654 	 */
655 	if (ti == 0)
656 		goto present;
657 
658 	/*
659 	 * Find a segment which begins after this one does.
660 	 */
661 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
662 	    q = (struct tcpiphdr *)q->ti_next)
663 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
664 			break;
665 
666 	/*
667 	 * If there is a preceding segment, it may provide some of
668 	 * our data already.  If so, drop the data from the incoming
669 	 * segment.  If it provides all of our data, drop us.
670 	 */
671 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
672 		register int i;
673 		q = (struct tcpiphdr *)(q->ti_prev);
674 		/* conversion to int (in i) handles seq wraparound */
675 		i = q->ti_seq + q->ti_len - ti->ti_seq;
676 		if (i > 0) {
677 			if (i >= ti->ti_len)
678 				goto drop;
679 			m_adj(dtom(tp), i);
680 			ti->ti_len -= i;
681 			ti->ti_seq += i;
682 		}
683 		q = (struct tcpiphdr *)(q->ti_next);
684 	}
685 
686 	/*
687 	 * While we overlap succeeding segments trim them or,
688 	 * if they are completely covered, dequeue them.
689 	 */
690 	while (q != (struct tcpiphdr *)tp &&
691 	    SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) {
692 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
693 		if (i < q->ti_len) {
694 			q->ti_len -= i;
695 			m_adj(dtom(q), i);
696 			break;
697 		}
698 		q = (struct tcpiphdr *)q->ti_next;
699 		m_freem(dtom(q->ti_prev));
700 		remque(q->ti_prev);
701 	}
702 
703 	/*
704 	 * Stick new segment in its place.
705 	 */
706 	insque(ti, q->ti_prev);
707 
708 present:
709 	/*
710 	 * Present data to user, advancing rcv_nxt through
711 	 * completed sequence space.
712 	 */
713 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
714 		return (0);
715 	ti = tp->seg_next;
716 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
717 		return (0);
718 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
719 		return (0);
720 	do {
721 		tp->rcv_nxt += ti->ti_len;
722 		flags = ti->ti_flags & TH_FIN;
723 		remque(ti);
724 		m = dtom(ti);
725 		ti = (struct tcpiphdr *)ti->ti_next;
726 		if (so->so_state & SS_CANTRCVMORE)
727 			(void) m_freem(m);
728 		else
729 			sbappend(&so->so_rcv, m);
730 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
731 	sorwakeup(so);
732 	return (flags);
733 drop:
734 	m_freem(dtom(ti));
735 	return (0);
736 }
737