xref: /csrg-svn/sys/netinet/tcp_input.c (revision 32098)
1 /*
2  * Copyright (c) 1982, 1986 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	7.11 (Berkeley) 09/04/87
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 int	tcprexmtthresh = 3;
36 struct	tcpiphdr tcp_saveti;
37 extern	tcpnodelack;
38 
39 struct	tcpcb *tcp_newtcpcb();
40 
41 /*
42  * Insert segment ti into reassembly queue of tcp with
43  * control block tp.  Return TH_FIN if reassembly now includes
44  * a segment with FIN.  The macro form does the common case inline
45  * (segment is the next to be received on an established connection,
46  * and the queue is empty), avoiding linkage into and removal
47  * from the queue and repetition of various conversions.
48  */
49 #define	TCP_REASS(tp, ti, m, so, flags) { \
50 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
51 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
52 	    (tp)->t_state == TCPS_ESTABLISHED) { \
53 		(tp)->rcv_nxt += (ti)->ti_len; \
54 		flags = (ti)->ti_flags & TH_FIN; \
55 		tcpstat.tcps_rcvpack++;\
56 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
57 		sbappend(&(so)->so_rcv, (m)); \
58 		sorwakeup(so); \
59 	} else \
60 		(flags) = tcp_reass((tp), (ti)); \
61 }
62 
63 tcp_reass(tp, ti)
64 	register struct tcpcb *tp;
65 	register struct tcpiphdr *ti;
66 {
67 	register struct tcpiphdr *q;
68 	struct socket *so = tp->t_inpcb->inp_socket;
69 	struct mbuf *m;
70 	int flags;
71 
72 	/*
73 	 * Call with ti==0 after become established to
74 	 * force pre-ESTABLISHED data up to user socket.
75 	 */
76 	if (ti == 0)
77 		goto present;
78 
79 	/*
80 	 * Find a segment which begins after this one does.
81 	 */
82 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
83 	    q = (struct tcpiphdr *)q->ti_next)
84 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
85 			break;
86 
87 	/*
88 	 * If there is a preceding segment, it may provide some of
89 	 * our data already.  If so, drop the data from the incoming
90 	 * segment.  If it provides all of our data, drop us.
91 	 */
92 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
93 		register int i;
94 		q = (struct tcpiphdr *)q->ti_prev;
95 		/* conversion to int (in i) handles seq wraparound */
96 		i = q->ti_seq + q->ti_len - ti->ti_seq;
97 		if (i > 0) {
98 			if (i >= ti->ti_len) {
99 				tcpstat.tcps_rcvduppack++;
100 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
101 				goto drop;
102 			}
103 			m_adj(dtom(ti), i);
104 			ti->ti_len -= i;
105 			ti->ti_seq += i;
106 		}
107 		q = (struct tcpiphdr *)(q->ti_next);
108 	}
109 	tcpstat.tcps_rcvoopack++;
110 	tcpstat.tcps_rcvoobyte += ti->ti_len;
111 
112 	/*
113 	 * While we overlap succeeding segments trim them or,
114 	 * if they are completely covered, dequeue them.
115 	 */
116 	while (q != (struct tcpiphdr *)tp) {
117 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
118 		if (i <= 0)
119 			break;
120 		if (i < q->ti_len) {
121 			q->ti_seq += i;
122 			q->ti_len -= i;
123 			m_adj(dtom(q), i);
124 			break;
125 		}
126 		q = (struct tcpiphdr *)q->ti_next;
127 		m = dtom(q->ti_prev);
128 		remque(q->ti_prev);
129 		m_freem(m);
130 	}
131 
132 	/*
133 	 * Stick new segment in its place.
134 	 */
135 	insque(ti, q->ti_prev);
136 
137 present:
138 	/*
139 	 * Present data to user, advancing rcv_nxt through
140 	 * completed sequence space.
141 	 */
142 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
143 		return (0);
144 	ti = tp->seg_next;
145 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
146 		return (0);
147 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
148 		return (0);
149 	do {
150 		tp->rcv_nxt += ti->ti_len;
151 		flags = ti->ti_flags & TH_FIN;
152 		remque(ti);
153 		m = dtom(ti);
154 		ti = (struct tcpiphdr *)ti->ti_next;
155 		if (so->so_state & SS_CANTRCVMORE)
156 			m_freem(m);
157 		else
158 			sbappend(&so->so_rcv, m);
159 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
160 	sorwakeup(so);
161 	return (flags);
162 drop:
163 	m_freem(dtom(ti));
164 	return (0);
165 }
166 
167 /*
168  * TCP input routine, follows pages 65-76 of the
169  * protocol specification dated September, 1981 very closely.
170  */
171 tcp_input(m0)
172 	struct mbuf *m0;
173 {
174 	register struct tcpiphdr *ti;
175 	struct inpcb *inp;
176 	register struct mbuf *m;
177 	struct mbuf *om = 0;
178 	int len, tlen, off;
179 	register struct tcpcb *tp = 0;
180 	register int tiflags;
181 	struct socket *so;
182 	int todrop, acked, ourfinisacked, needoutput = 0;
183 	short ostate;
184 	struct in_addr laddr;
185 	int dropsocket = 0;
186 	int iss = 0;
187 
188 	tcpstat.tcps_rcvtotal++;
189 	/*
190 	 * Get IP and TCP header together in first mbuf.
191 	 * Note: IP leaves IP header in first mbuf.
192 	 */
193 	m = m0;
194 	ti = mtod(m, struct tcpiphdr *);
195 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
196 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
197 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
198 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
199 			tcpstat.tcps_rcvshort++;
200 			return;
201 		}
202 		ti = mtod(m, struct tcpiphdr *);
203 	}
204 
205 	/*
206 	 * Checksum extended TCP header and data.
207 	 */
208 	tlen = ((struct ip *)ti)->ip_len;
209 	len = sizeof (struct ip) + tlen;
210 	if (tcpcksum) {
211 		ti->ti_next = ti->ti_prev = 0;
212 		ti->ti_x1 = 0;
213 		ti->ti_len = (u_short)tlen;
214 		ti->ti_len = htons((u_short)ti->ti_len);
215 		if (ti->ti_sum = in_cksum(m, len)) {
216 			if (tcpprintfs)
217 				printf("tcp sum: src %x\n", ti->ti_src);
218 			tcpstat.tcps_rcvbadsum++;
219 			goto drop;
220 		}
221 	}
222 
223 	/*
224 	 * Check that TCP offset makes sense,
225 	 * pull out TCP options and adjust length.
226 	 */
227 	off = ti->ti_off << 2;
228 	if (off < sizeof (struct tcphdr) || off > tlen) {
229 		if (tcpprintfs)
230 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
231 		tcpstat.tcps_rcvbadoff++;
232 		goto drop;
233 	}
234 	tlen -= off;
235 	ti->ti_len = tlen;
236 	if (off > sizeof (struct tcphdr)) {
237 		if (m->m_len < sizeof(struct ip) + off) {
238 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
239 				tcpstat.tcps_rcvshort++;
240 				return;
241 			}
242 			ti = mtod(m, struct tcpiphdr *);
243 		}
244 		om = m_get(M_DONTWAIT, MT_DATA);
245 		if (om == 0)
246 			goto drop;
247 		om->m_len = off - sizeof (struct tcphdr);
248 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
249 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
250 		  m->m_len -= om->m_len;
251 		  bcopy(op+om->m_len, op,
252 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
253 		}
254 	}
255 	tiflags = ti->ti_flags;
256 
257 	/*
258 	 * Drop TCP and IP headers; TCP options were dropped above.
259 	 */
260 	m->m_off += sizeof(struct tcpiphdr);
261 	m->m_len -= sizeof(struct tcpiphdr);
262 
263 	/*
264 	 * Convert TCP protocol specific fields to host format.
265 	 */
266 	ti->ti_seq = ntohl(ti->ti_seq);
267 	ti->ti_ack = ntohl(ti->ti_ack);
268 	ti->ti_win = ntohs(ti->ti_win);
269 	ti->ti_urp = ntohs(ti->ti_urp);
270 
271 	/*
272 	 * Locate pcb for segment.
273 	 */
274 findpcb:
275 	inp = in_pcblookup
276 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
277 		INPLOOKUP_WILDCARD);
278 
279 	/*
280 	 * If the state is CLOSED (i.e., TCB does not exist) then
281 	 * all data in the incoming segment is discarded.
282 	 * If the TCB exists but is in CLOSED state, it is embryonic,
283 	 * but should either do a listen or a connect soon.
284 	 */
285 	if (inp == 0)
286 		goto dropwithreset;
287 	tp = intotcpcb(inp);
288 	if (tp == 0)
289 		goto dropwithreset;
290 	if (tp->t_state == TCPS_CLOSED)
291 		goto drop;
292 	so = inp->inp_socket;
293 	if (so->so_options & SO_DEBUG) {
294 		ostate = tp->t_state;
295 		tcp_saveti = *ti;
296 	}
297 	if (so->so_options & SO_ACCEPTCONN) {
298 		so = sonewconn(so);
299 		if (so == 0)
300 			goto drop;
301 		/*
302 		 * This is ugly, but ....
303 		 *
304 		 * Mark socket as temporary until we're
305 		 * committed to keeping it.  The code at
306 		 * ``drop'' and ``dropwithreset'' check the
307 		 * flag dropsocket to see if the temporary
308 		 * socket created here should be discarded.
309 		 * We mark the socket as discardable until
310 		 * we're committed to it below in TCPS_LISTEN.
311 		 */
312 		dropsocket++;
313 		inp = (struct inpcb *)so->so_pcb;
314 		inp->inp_laddr = ti->ti_dst;
315 		inp->inp_lport = ti->ti_dport;
316 		inp->inp_options = ip_srcroute();
317 		tp = intotcpcb(inp);
318 		tp->t_state = TCPS_LISTEN;
319 	}
320 
321 	/*
322 	 * Segment received on connection.
323 	 * Reset idle time and keep-alive timer.
324 	 */
325 	tp->t_idle = 0;
326 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
327 
328 	/*
329 	 * Process options if not in LISTEN state,
330 	 * else do it below (after getting remote address).
331 	 */
332 	if (om && tp->t_state != TCPS_LISTEN) {
333 		tcp_dooptions(tp, om, ti);
334 		om = 0;
335 	}
336 
337 	/*
338 	 * Calculate amount of space in receive window,
339 	 * and then do TCP input processing.
340 	 * Receive window is amount of space in rcv queue,
341 	 * but not less than advertised window.
342 	 */
343 	{ int win;
344 
345 	win = sbspace(&so->so_rcv);
346 	if (win < 0)
347 		win = 0;
348 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
349 	}
350 
351 	switch (tp->t_state) {
352 
353 	/*
354 	 * If the state is LISTEN then ignore segment if it contains an RST.
355 	 * If the segment contains an ACK then it is bad and send a RST.
356 	 * If it does not contain a SYN then it is not interesting; drop it.
357 	 * Don't bother responding if the destination was a broadcast.
358 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
359 	 * tp->iss, and send a segment:
360 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
361 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
362 	 * Fill in remote peer address fields if not previously specified.
363 	 * Enter SYN_RECEIVED state, and process any other fields of this
364 	 * segment in this state.
365 	 */
366 	case TCPS_LISTEN: {
367 		struct mbuf *am;
368 		register struct sockaddr_in *sin;
369 
370 		if (tiflags & TH_RST)
371 			goto drop;
372 		if (tiflags & TH_ACK)
373 			goto dropwithreset;
374 		if ((tiflags & TH_SYN) == 0)
375 			goto drop;
376 		if (in_broadcast(ti->ti_dst))
377 			goto drop;
378 		am = m_get(M_DONTWAIT, MT_SONAME);
379 		if (am == NULL)
380 			goto drop;
381 		am->m_len = sizeof (struct sockaddr_in);
382 		sin = mtod(am, struct sockaddr_in *);
383 		sin->sin_family = AF_INET;
384 		sin->sin_addr = ti->ti_src;
385 		sin->sin_port = ti->ti_sport;
386 		laddr = inp->inp_laddr;
387 		if (inp->inp_laddr.s_addr == INADDR_ANY)
388 			inp->inp_laddr = ti->ti_dst;
389 		if (in_pcbconnect(inp, am)) {
390 			inp->inp_laddr = laddr;
391 			(void) m_free(am);
392 			goto drop;
393 		}
394 		(void) m_free(am);
395 		tp->t_template = tcp_template(tp);
396 		if (tp->t_template == 0) {
397 			tp = tcp_drop(tp, ENOBUFS);
398 			dropsocket = 0;		/* socket is already gone */
399 			goto drop;
400 		}
401 		if (om) {
402 			tcp_dooptions(tp, om, ti);
403 			om = 0;
404 		}
405 		if (iss)
406 			tp->iss = iss;
407 		else
408 			tp->iss = tcp_iss;
409 		tcp_iss += TCP_ISSINCR/2;
410 		tp->irs = ti->ti_seq;
411 		tcp_sendseqinit(tp);
412 		tcp_rcvseqinit(tp);
413 		tp->t_flags |= TF_ACKNOW;
414 		tp->t_state = TCPS_SYN_RECEIVED;
415 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
416 		dropsocket = 0;		/* committed to socket */
417 		tcpstat.tcps_accepts++;
418 		goto trimthenstep6;
419 		}
420 
421 	/*
422 	 * If the state is SYN_SENT:
423 	 *	if seg contains an ACK, but not for our SYN, drop the input.
424 	 *	if seg contains a RST, then drop the connection.
425 	 *	if seg does not contain SYN, then drop it.
426 	 * Otherwise this is an acceptable SYN segment
427 	 *	initialize tp->rcv_nxt and tp->irs
428 	 *	if seg contains ack then advance tp->snd_una
429 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
430 	 *	arrange for segment to be acked (eventually)
431 	 *	continue processing rest of data/controls, beginning with URG
432 	 */
433 	case TCPS_SYN_SENT:
434 		if ((tiflags & TH_ACK) &&
435 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
436 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
437 			goto dropwithreset;
438 		if (tiflags & TH_RST) {
439 			if (tiflags & TH_ACK)
440 				tp = tcp_drop(tp, ECONNREFUSED);
441 			goto drop;
442 		}
443 		if ((tiflags & TH_SYN) == 0)
444 			goto drop;
445 		if (tiflags & TH_ACK) {
446 			tp->snd_una = ti->ti_ack;
447 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
448 				tp->snd_nxt = tp->snd_una;
449 		}
450 		tp->t_timer[TCPT_REXMT] = 0;
451 		tp->irs = ti->ti_seq;
452 		tcp_rcvseqinit(tp);
453 		tp->t_flags |= TF_ACKNOW;
454 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
455 			tcpstat.tcps_connects++;
456 			soisconnected(so);
457 			tp->t_state = TCPS_ESTABLISHED;
458 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
459 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
460 			/*
461 			 * if we didn't have to retransmit the SYN,
462 			 * use its rtt as our initial srtt & rtt var.
463 			 */
464 			if (tp->t_rtt) {
465 				tp->t_srtt = tp->t_rtt << 3;
466 				tp->t_rttvar = tp->t_rtt << 1;
467 				TCPT_RANGESET(tp->t_rxtcur,
468 				    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
469 				    TCPTV_MIN, TCPTV_REXMTMAX);
470 				tp->t_rtt = 0;
471 			}
472 		} else
473 			tp->t_state = TCPS_SYN_RECEIVED;
474 
475 trimthenstep6:
476 		/*
477 		 * Advance ti->ti_seq to correspond to first data byte.
478 		 * If data, trim to stay within window,
479 		 * dropping FIN if necessary.
480 		 */
481 		ti->ti_seq++;
482 		if (ti->ti_len > tp->rcv_wnd) {
483 			todrop = ti->ti_len - tp->rcv_wnd;
484 			m_adj(m, -todrop);
485 			ti->ti_len = tp->rcv_wnd;
486 			tiflags &= ~TH_FIN;
487 			tcpstat.tcps_rcvpackafterwin++;
488 			tcpstat.tcps_rcvbyteafterwin += todrop;
489 		}
490 		tp->snd_wl1 = ti->ti_seq - 1;
491 		tp->rcv_up = ti->ti_seq;
492 		goto step6;
493 	}
494 
495 	/*
496 	 * States other than LISTEN or SYN_SENT.
497 	 * First check that at least some bytes of segment are within
498 	 * receive window.  If segment begins before rcv_nxt,
499 	 * drop leading data (and SYN); if nothing left, just ack.
500 	 */
501 	todrop = tp->rcv_nxt - ti->ti_seq;
502 	if (todrop > 0) {
503 		if (tiflags & TH_SYN) {
504 			tiflags &= ~TH_SYN;
505 			ti->ti_seq++;
506 			if (ti->ti_urp > 1)
507 				ti->ti_urp--;
508 			else
509 				tiflags &= ~TH_URG;
510 			todrop--;
511 		}
512 		if (todrop > ti->ti_len ||
513 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
514 #ifdef TCP_COMPAT_42
515 			/*
516 			 * Don't toss RST in response to 4.2-style keepalive.
517 			 */
518 			if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST)
519 				goto do_rst;
520 #endif
521 			tcpstat.tcps_rcvduppack++;
522 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
523 			todrop = ti->ti_len;
524 			tiflags &= ~TH_FIN;
525 			tp->t_flags |= TF_ACKNOW;
526 		} else {
527 			tcpstat.tcps_rcvpartduppack++;
528 			tcpstat.tcps_rcvpartdupbyte += todrop;
529 		}
530 		m_adj(m, todrop);
531 		ti->ti_seq += todrop;
532 		ti->ti_len -= todrop;
533 		if (ti->ti_urp > todrop)
534 			ti->ti_urp -= todrop;
535 		else {
536 			tiflags &= ~TH_URG;
537 			ti->ti_urp = 0;
538 		}
539 	}
540 
541 	if (tp->rcv_wnd == 0) {
542 		/*
543 		 * If window is closed can only take segments at
544 		 * window edge, and have to drop data and PUSH from
545 		 * incoming segments.
546 		 *
547 		 * If new data is received on a connection after the
548 		 * user processes are gone, then RST the other end.
549 		 */
550 		if ((so->so_state & SS_NOFDREF) &&
551 		    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
552 			tp = tcp_close(tp);
553 			tcpstat.tcps_rcvafterclose++;
554 			goto dropwithreset;
555 		}
556 		if (tp->rcv_nxt != ti->ti_seq) {
557 			tcpstat.tcps_rcvpackafterwin++;
558 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
559 			goto dropafterack;
560 		}
561 		if (ti->ti_len > 0) {
562 			if (ti->ti_len == 1)
563 				tcpstat.tcps_rcvwinprobe++;
564 			else {
565 				tcpstat.tcps_rcvpackafterwin++;
566 				tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
567 			}
568 			m_adj(m, ti->ti_len);
569 			ti->ti_len = 0;
570 			tiflags &= ~(TH_PUSH|TH_FIN);
571 		}
572 	} else {
573 		/*
574 		 * If segment ends after window, drop trailing data
575 		 * (and PUSH and FIN); if nothing left, just ACK.
576 		 */
577 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
578 		if (todrop > 0) {
579 			if (todrop >= ti->ti_len) {
580 				/*
581 				 * If a new connection request is received
582 				 * while in TIME_WAIT, drop the old connection
583 				 * and start over if the sequence numbers
584 				 * are above the previous ones.
585 				 */
586 				if (tiflags & TH_SYN &&
587 				    tp->t_state == TCPS_TIME_WAIT &&
588 				    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
589 					iss = tp->rcv_nxt + TCP_ISSINCR;
590 					(void) tcp_close(tp);
591 					goto findpcb;
592 				}
593 				if (todrop == 1)
594 					tcpstat.tcps_rcvwinprobe++;
595 				else {
596 					tcpstat.tcps_rcvpackafterwin++;
597 					tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
598 				}
599 				goto dropafterack;
600 			}
601 			tcpstat.tcps_rcvpackafterwin++;
602 			tcpstat.tcps_rcvbyteafterwin += todrop;
603 			m_adj(m, -todrop);
604 			ti->ti_len -= todrop;
605 			tiflags &= ~(TH_PUSH|TH_FIN);
606 		}
607 	}
608 
609 #ifdef TCP_COMPAT_42
610 do_rst:
611 #endif
612 	/*
613 	 * If the RST bit is set examine the state:
614 	 *    SYN_RECEIVED STATE:
615 	 *	If passive open, return to LISTEN state.
616 	 *	If active open, inform user that connection was refused.
617 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
618 	 *	Inform user that connection was reset, and close tcb.
619 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
620 	 *	Close the tcb.
621 	 */
622 	if (tiflags&TH_RST) switch (tp->t_state) {
623 
624 	case TCPS_SYN_RECEIVED:
625 		tp = tcp_drop(tp, ECONNREFUSED);
626 		goto drop;
627 
628 	case TCPS_ESTABLISHED:
629 	case TCPS_FIN_WAIT_1:
630 	case TCPS_FIN_WAIT_2:
631 	case TCPS_CLOSE_WAIT:
632 		tp = tcp_drop(tp, ECONNRESET);
633 		goto drop;
634 
635 	case TCPS_CLOSING:
636 	case TCPS_LAST_ACK:
637 	case TCPS_TIME_WAIT:
638 		tp = tcp_close(tp);
639 		goto drop;
640 	}
641 
642 	/*
643 	 * If a SYN is in the window, then this is an
644 	 * error and we send an RST and drop the connection.
645 	 */
646 	if (tiflags & TH_SYN) {
647 		tp = tcp_drop(tp, ECONNRESET);
648 		goto dropwithreset;
649 	}
650 
651 	/*
652 	 * If the ACK bit is off we drop the segment and return.
653 	 */
654 	if ((tiflags & TH_ACK) == 0)
655 		goto drop;
656 
657 	/*
658 	 * Ack processing.
659 	 */
660 	switch (tp->t_state) {
661 
662 	/*
663 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
664 	 * ESTABLISHED state and continue processing, otherwise
665 	 * send an RST.
666 	 */
667 	case TCPS_SYN_RECEIVED:
668 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
669 		    SEQ_GT(ti->ti_ack, tp->snd_max))
670 			goto dropwithreset;
671 		tcpstat.tcps_connects++;
672 		soisconnected(so);
673 		tp->t_state = TCPS_ESTABLISHED;
674 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
675 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
676 		tp->snd_wl1 = ti->ti_seq - 1;
677 		/* fall into ... */
678 
679 	/*
680 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
681 	 * ACKs.  If the ack is in the range
682 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
683 	 * then advance tp->snd_una to ti->ti_ack and drop
684 	 * data from the retransmission queue.  If this ACK reflects
685 	 * more up to date window information we update our window information.
686 	 */
687 	case TCPS_ESTABLISHED:
688 	case TCPS_FIN_WAIT_1:
689 	case TCPS_FIN_WAIT_2:
690 	case TCPS_CLOSE_WAIT:
691 	case TCPS_CLOSING:
692 	case TCPS_LAST_ACK:
693 	case TCPS_TIME_WAIT:
694 
695 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
696 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
697 				tcpstat.tcps_rcvdupack++;
698 				/*
699 				 * If we have outstanding data (not a
700 				 * window probe), this is a completely
701 				 * duplicate ack (ie, window info didn't
702 				 * change), the ack is the biggest we've
703 				 * seen and we've seen exactly our rexmt
704 				 * threshhold of them, assume a packet
705 				 * has been dropped and retransmit it.
706 				 * Kludge snd_nxt & the congestion
707 				 * window so we send only this one
708 				 * packet.  Close the congestion
709 				 * window to half its current size
710 				 * to prevent a restart burst if this
711 				 * packet fills all the holes in the
712 				 * receiver's sequence space and she
713 				 * advertises a fully open window on
714 				 * the next real ack.
715 				 */
716 				if (tp->t_timer[TCPT_REXMT] == 0 ||
717 				    ti->ti_ack != tp->snd_una)
718 					tp->t_dupacks = 0;
719 				else if (++tp->t_dupacks == tcprexmtthresh) {
720 					tcp_seq onxt = tp->snd_nxt;
721 					u_short cwnd = tp->snd_cwnd;
722 
723 					tp->t_timer[TCPT_REXMT] = 0;
724 					tp->t_rtt = 0;
725 					tp->snd_nxt = ti->ti_ack;
726 					tp->snd_cwnd = tp->t_maxseg;
727 					(void) tcp_output(tp);
728 
729 					if (cwnd >= 4 * tp->t_maxseg)
730 						tp->snd_cwnd = cwnd / 2;
731 					if (SEQ_GT(onxt, tp->snd_nxt))
732 						tp->snd_nxt = onxt;
733 					goto drop;
734 				}
735 			} else
736 				tp->t_dupacks = 0;
737 			break;
738 		}
739 		tp->t_dupacks = 0;
740 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
741 			tcpstat.tcps_rcvacktoomuch++;
742 			goto dropafterack;
743 		}
744 		acked = ti->ti_ack - tp->snd_una;
745 		tcpstat.tcps_rcvackpack++;
746 		tcpstat.tcps_rcvackbyte += acked;
747 
748 		/*
749 		 * If transmit timer is running and timed sequence
750 		 * number was acked, update smoothed round trip time.
751 		 * Since we now have an rtt measurement, cancel the
752 		 * timer backoff (cf., Phil Karn's retransmit alg.).
753 		 * Recompute the initial retransmit timer.
754 		 */
755 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
756 			tcpstat.tcps_rttupdated++;
757 			if (tp->t_srtt != 0) {
758 				register short delta;
759 
760 				/*
761 				 * srtt is stored as fixed point with 3 bits
762 				 * after the binary point (i.e., scaled by 8).
763 				 * The following magic is equivalent
764 				 * to the smoothing algorithm in rfc793
765 				 * with an alpha of .875
766 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
767 				 * Adjust t_rtt to origin 0.
768 				 */
769 				tp->t_rtt--;
770 				delta = tp->t_rtt - (tp->t_srtt >> 3);
771 				if ((tp->t_srtt += delta) <= 0)
772 					tp->t_srtt = 1;
773 				/*
774 				 * We accumulate a smoothed rtt variance
775 				 * (actually, a smoothed mean difference),
776 				 * then set the retransmit timer to smoothed
777 				 * rtt + 2 times the smoothed variance.
778 				 * rttvar is stored as fixed point
779 				 * with 2 bits after the binary point
780 				 * (scaled by 4).  The following is equivalent
781 				 * to rfc793 smoothing with an alpha of .75
782 				 * (rttvar = rttvar*3/4 + |delta| / 4).
783 				 * This replaces rfc793's wired-in beta.
784 				 */
785 				if (delta < 0)
786 					delta = -delta;
787 				delta -= (tp->t_rttvar >> 2);
788 				if ((tp->t_rttvar += delta) <= 0)
789 					tp->t_rttvar = 1;
790 			} else {
791 				/*
792 				 * No rtt measurement yet - use the
793 				 * unsmoothed rtt.  Set the variance
794 				 * to half the rtt (so our first
795 				 * retransmit happens at 2*rtt)
796 				 */
797 				tp->t_srtt = tp->t_rtt << 3;
798 				tp->t_rttvar = tp->t_rtt << 1;
799 			}
800 			tp->t_rtt = 0;
801 			tp->t_rxtshift = 0;
802 			TCPT_RANGESET(tp->t_rxtcur,
803 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
804 			    TCPTV_MIN, TCPTV_REXMTMAX);
805 		}
806 
807 		/*
808 		 * If all outstanding data is acked, stop retransmit
809 		 * timer and remember to restart (more output or persist).
810 		 * If there is more data to be acked, restart retransmit
811 		 * timer, using current (possibly backed-off) value.
812 		 */
813 		if (ti->ti_ack == tp->snd_max) {
814 			tp->t_timer[TCPT_REXMT] = 0;
815 			needoutput = 1;
816 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
817 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
818 		/*
819 		 * When new data is acked, open the congestion window.
820 		 * If the window gives us less than ssthresh packets
821 		 * in flight, open exponentially (maxseg per packet).
822 		 * Otherwise open linearly (maxseg per window,
823 		 * or maxseg^2 / cwnd per packet).
824 		 */
825 		{
826 		u_int incr = tp->t_maxseg;
827 
828 		if (tp->snd_cwnd > tp->snd_ssthresh)
829 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
830 
831 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */
832 		}
833 		if (acked > so->so_snd.sb_cc) {
834 			tp->snd_wnd -= so->so_snd.sb_cc;
835 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
836 			ourfinisacked = 1;
837 		} else {
838 			sbdrop(&so->so_snd, acked);
839 			tp->snd_wnd -= acked;
840 			ourfinisacked = 0;
841 		}
842 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
843 			sowwakeup(so);
844 		tp->snd_una = ti->ti_ack;
845 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
846 			tp->snd_nxt = tp->snd_una;
847 
848 		switch (tp->t_state) {
849 
850 		/*
851 		 * In FIN_WAIT_1 STATE in addition to the processing
852 		 * for the ESTABLISHED state if our FIN is now acknowledged
853 		 * then enter FIN_WAIT_2.
854 		 */
855 		case TCPS_FIN_WAIT_1:
856 			if (ourfinisacked) {
857 				/*
858 				 * If we can't receive any more
859 				 * data, then closing user can proceed.
860 				 * Starting the timer is contrary to the
861 				 * specification, but if we don't get a FIN
862 				 * we'll hang forever.
863 				 */
864 				if (so->so_state & SS_CANTRCVMORE) {
865 					soisdisconnected(so);
866 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
867 				}
868 				tp->t_state = TCPS_FIN_WAIT_2;
869 			}
870 			break;
871 
872 	 	/*
873 		 * In CLOSING STATE in addition to the processing for
874 		 * the ESTABLISHED state if the ACK acknowledges our FIN
875 		 * then enter the TIME-WAIT state, otherwise ignore
876 		 * the segment.
877 		 */
878 		case TCPS_CLOSING:
879 			if (ourfinisacked) {
880 				tp->t_state = TCPS_TIME_WAIT;
881 				tcp_canceltimers(tp);
882 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
883 				soisdisconnected(so);
884 			}
885 			break;
886 
887 		/*
888 		 * In LAST_ACK, we may still be waiting for data to drain
889 		 * and/or to be acked, as well as for the ack of our FIN.
890 		 * If our FIN is now acknowledged, delete the TCB,
891 		 * enter the closed state and return.
892 		 */
893 		case TCPS_LAST_ACK:
894 			if (ourfinisacked) {
895 				tp = tcp_close(tp);
896 				goto drop;
897 			}
898 			break;
899 
900 		/*
901 		 * In TIME_WAIT state the only thing that should arrive
902 		 * is a retransmission of the remote FIN.  Acknowledge
903 		 * it and restart the finack timer.
904 		 */
905 		case TCPS_TIME_WAIT:
906 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
907 			goto dropafterack;
908 		}
909 	}
910 
911 step6:
912 	/*
913 	 * Update window information.
914 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
915 	 */
916 	if ((tiflags & TH_ACK) &&
917 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
918 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
919 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
920 		/* keep track of pure window updates */
921 		if (ti->ti_len == 0 &&
922 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
923 			tcpstat.tcps_rcvwinupd++;
924 		tp->snd_wnd = ti->ti_win;
925 		tp->snd_wl1 = ti->ti_seq;
926 		tp->snd_wl2 = ti->ti_ack;
927 		if (tp->snd_wnd > tp->max_sndwnd)
928 			tp->max_sndwnd = tp->snd_wnd;
929 		needoutput = 1;
930 	}
931 
932 	/*
933 	 * Process segments with URG.
934 	 */
935 	if ((tiflags & TH_URG) && ti->ti_urp &&
936 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
937 		/*
938 		 * This is a kludge, but if we receive and accept
939 		 * random urgent pointers, we'll crash in
940 		 * soreceive.  It's hard to imagine someone
941 		 * actually wanting to send this much urgent data.
942 		 */
943 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
944 			ti->ti_urp = 0;			/* XXX */
945 			tiflags &= ~TH_URG;		/* XXX */
946 			goto dodata;			/* XXX */
947 		}
948 		/*
949 		 * If this segment advances the known urgent pointer,
950 		 * then mark the data stream.  This should not happen
951 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
952 		 * a FIN has been received from the remote side.
953 		 * In these states we ignore the URG.
954 		 *
955 		 * According to RFC961 (Assigned Protocols),
956 		 * the urgent pointer points to the last octet
957 		 * of urgent data.  We continue, however,
958 		 * to consider it to indicate the first octet
959 		 * of data past the urgent section
960 		 * as the original spec states.
961 		 */
962 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
963 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
964 			so->so_oobmark = so->so_rcv.sb_cc +
965 			    (tp->rcv_up - tp->rcv_nxt) - 1;
966 			if (so->so_oobmark == 0)
967 				so->so_state |= SS_RCVATMARK;
968 			sohasoutofband(so);
969 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
970 		}
971 		/*
972 		 * Remove out of band data so doesn't get presented to user.
973 		 * This can happen independent of advancing the URG pointer,
974 		 * but if two URG's are pending at once, some out-of-band
975 		 * data may creep in... ick.
976 		 */
977 		if (ti->ti_urp <= ti->ti_len &&
978 		    (so->so_options & SO_OOBINLINE) == 0)
979 			tcp_pulloutofband(so, ti);
980 	} else
981 		/*
982 		 * If no out of band data is expected,
983 		 * pull receive urgent pointer along
984 		 * with the receive window.
985 		 */
986 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
987 			tp->rcv_up = tp->rcv_nxt;
988 dodata:							/* XXX */
989 
990 	/*
991 	 * Process the segment text, merging it into the TCP sequencing queue,
992 	 * and arranging for acknowledgment of receipt if necessary.
993 	 * This process logically involves adjusting tp->rcv_wnd as data
994 	 * is presented to the user (this happens in tcp_usrreq.c,
995 	 * case PRU_RCVD).  If a FIN has already been received on this
996 	 * connection then we just ignore the text.
997 	 */
998 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
999 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1000 		TCP_REASS(tp, ti, m, so, tiflags);
1001 		if (tcpnodelack == 0)
1002 			tp->t_flags |= TF_DELACK;
1003 		else
1004 			tp->t_flags |= TF_ACKNOW;
1005 		/*
1006 		 * Note the amount of data that peer has sent into
1007 		 * our window, in order to estimate the sender's
1008 		 * buffer size.
1009 		 */
1010 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1011 		if (len > tp->max_rcvd)
1012 			tp->max_rcvd = len;
1013 	} else {
1014 		m_freem(m);
1015 		tiflags &= ~TH_FIN;
1016 	}
1017 
1018 	/*
1019 	 * If FIN is received ACK the FIN and let the user know
1020 	 * that the connection is closing.
1021 	 */
1022 	if (tiflags & TH_FIN) {
1023 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1024 			socantrcvmore(so);
1025 			tp->t_flags |= TF_ACKNOW;
1026 			tp->rcv_nxt++;
1027 		}
1028 		switch (tp->t_state) {
1029 
1030 	 	/*
1031 		 * In SYN_RECEIVED and ESTABLISHED STATES
1032 		 * enter the CLOSE_WAIT state.
1033 		 */
1034 		case TCPS_SYN_RECEIVED:
1035 		case TCPS_ESTABLISHED:
1036 			tp->t_state = TCPS_CLOSE_WAIT;
1037 			break;
1038 
1039 	 	/*
1040 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1041 		 * enter the CLOSING state.
1042 		 */
1043 		case TCPS_FIN_WAIT_1:
1044 			tp->t_state = TCPS_CLOSING;
1045 			break;
1046 
1047 	 	/*
1048 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1049 		 * starting the time-wait timer, turning off the other
1050 		 * standard timers.
1051 		 */
1052 		case TCPS_FIN_WAIT_2:
1053 			tp->t_state = TCPS_TIME_WAIT;
1054 			tcp_canceltimers(tp);
1055 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1056 			soisdisconnected(so);
1057 			break;
1058 
1059 		/*
1060 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1061 		 */
1062 		case TCPS_TIME_WAIT:
1063 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1064 			break;
1065 		}
1066 	}
1067 	if (so->so_options & SO_DEBUG)
1068 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1069 
1070 	/*
1071 	 * Return any desired output.
1072 	 */
1073 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1074 		(void) tcp_output(tp);
1075 	return;
1076 
1077 dropafterack:
1078 	/*
1079 	 * Generate an ACK dropping incoming segment if it occupies
1080 	 * sequence space, where the ACK reflects our state.
1081 	 */
1082 	if (tiflags & TH_RST)
1083 		goto drop;
1084 	m_freem(m);
1085 	tp->t_flags |= TF_ACKNOW;
1086 	(void) tcp_output(tp);
1087 	return;
1088 
1089 dropwithreset:
1090 	if (om) {
1091 		(void) m_free(om);
1092 		om = 0;
1093 	}
1094 	/*
1095 	 * Generate a RST, dropping incoming segment.
1096 	 * Make ACK acceptable to originator of segment.
1097 	 * Don't bother to respond if destination was broadcast.
1098 	 */
1099 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
1100 		goto drop;
1101 	if (tiflags & TH_ACK)
1102 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
1103 	else {
1104 		if (tiflags & TH_SYN)
1105 			ti->ti_len++;
1106 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1107 		    TH_RST|TH_ACK);
1108 	}
1109 	/* destroy temporarily created socket */
1110 	if (dropsocket)
1111 		(void) soabort(so);
1112 	return;
1113 
1114 drop:
1115 	if (om)
1116 		(void) m_free(om);
1117 	/*
1118 	 * Drop space held by incoming segment and return.
1119 	 */
1120 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1121 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1122 	m_freem(m);
1123 	/* destroy temporarily created socket */
1124 	if (dropsocket)
1125 		(void) soabort(so);
1126 	return;
1127 }
1128 
1129 tcp_dooptions(tp, om, ti)
1130 	struct tcpcb *tp;
1131 	struct mbuf *om;
1132 	struct tcpiphdr *ti;
1133 {
1134 	register u_char *cp;
1135 	int opt, optlen, cnt;
1136 
1137 	cp = mtod(om, u_char *);
1138 	cnt = om->m_len;
1139 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1140 		opt = cp[0];
1141 		if (opt == TCPOPT_EOL)
1142 			break;
1143 		if (opt == TCPOPT_NOP)
1144 			optlen = 1;
1145 		else {
1146 			optlen = cp[1];
1147 			if (optlen <= 0)
1148 				break;
1149 		}
1150 		switch (opt) {
1151 
1152 		default:
1153 			break;
1154 
1155 		case TCPOPT_MAXSEG:
1156 			if (optlen != 4)
1157 				continue;
1158 			if (!(ti->ti_flags & TH_SYN))
1159 				continue;
1160 			tp->t_maxseg = *(u_short *)(cp + 2);
1161 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
1162 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
1163 			break;
1164 		}
1165 	}
1166 	(void) m_free(om);
1167 }
1168 
1169 /*
1170  * Pull out of band byte out of a segment so
1171  * it doesn't appear in the user's data queue.
1172  * It is still reflected in the segment length for
1173  * sequencing purposes.
1174  */
1175 tcp_pulloutofband(so, ti)
1176 	struct socket *so;
1177 	struct tcpiphdr *ti;
1178 {
1179 	register struct mbuf *m;
1180 	int cnt = ti->ti_urp - 1;
1181 
1182 	m = dtom(ti);
1183 	while (cnt >= 0) {
1184 		if (m->m_len > cnt) {
1185 			char *cp = mtod(m, caddr_t) + cnt;
1186 			struct tcpcb *tp = sototcpcb(so);
1187 
1188 			tp->t_iobc = *cp;
1189 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1190 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1191 			m->m_len--;
1192 			return;
1193 		}
1194 		cnt -= m->m_len;
1195 		m = m->m_next;
1196 		if (m == 0)
1197 			break;
1198 	}
1199 	panic("tcp_pulloutofband");
1200 }
1201 
1202 /*
1203  *  Determine a reasonable value for maxseg size.
1204  *  If the route is known, use one that can be handled
1205  *  on the given interface without forcing IP to fragment.
1206  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
1207  *  to utilize large mbufs.
1208  *  If interface pointer is unavailable, or the destination isn't local,
1209  *  use a conservative size (512 or the default IP max size, but no more
1210  *  than the mtu of the interface through which we route),
1211  *  as we can't discover anything about intervening gateways or networks.
1212  *  We also initialize the congestion/slow start window to be a single
1213  *  segment if the destination isn't local; this information should
1214  *  probably all be saved with the routing entry at the transport level.
1215  *
1216  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
1217  */
1218 tcp_mss(tp)
1219 	register struct tcpcb *tp;
1220 {
1221 	struct route *ro;
1222 	struct ifnet *ifp;
1223 	int mss;
1224 	struct inpcb *inp;
1225 
1226 	inp = tp->t_inpcb;
1227 	ro = &inp->inp_route;
1228 	if ((ro->ro_rt == (struct rtentry *)0) ||
1229 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
1230 		/* No route yet, so try to acquire one */
1231 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1232 			ro->ro_dst.sa_family = AF_INET;
1233 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1234 				inp->inp_faddr;
1235 			rtalloc(ro);
1236 		}
1237 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
1238 			return (TCP_MSS);
1239 	}
1240 
1241 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1242 #if	(MCLBYTES & (MCLBYTES - 1)) == 0
1243 	if (mss > MCLBYTES)
1244 		mss &= ~(MCLBYTES-1);
1245 #else
1246 	if (mss > MCLBYTES)
1247 		mss = mss / MCLBYTES * MCLBYTES;
1248 #endif
1249 	if (in_localaddr(inp->inp_faddr))
1250 		return (mss);
1251 
1252 	mss = MIN(mss, TCP_MSS);
1253 	tp->snd_cwnd = mss;
1254 	return (mss);
1255 }
1256