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