xref: /csrg-svn/sys/netinet/tcp_input.c (revision 32612)
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.13 (Berkeley) 11/13/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 	/*
542 	 * If new data is received on a connection after the
543 	 * user processes are gone, then RST the other end.
544 	 */
545 	if ((so->so_state & SS_NOFDREF) &&
546 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
547 		tp = tcp_close(tp);
548 		tcpstat.tcps_rcvafterclose++;
549 		goto dropwithreset;
550 	}
551 
552 	if (tp->rcv_wnd == 0) {
553 		/*
554 		 * If window is closed can only take segments at
555 		 * window edge, and have to drop data and PUSH from
556 		 * incoming segments.
557 		 */
558 		if (tp->rcv_nxt != ti->ti_seq) {
559 			tcpstat.tcps_rcvpackafterwin++;
560 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
561 			goto dropafterack;
562 		}
563 		if (ti->ti_len > 0) {
564 			if (ti->ti_len == 1)
565 				tcpstat.tcps_rcvwinprobe++;
566 			else {
567 				tcpstat.tcps_rcvpackafterwin++;
568 				tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
569 			}
570 			m_adj(m, ti->ti_len);
571 			ti->ti_len = 0;
572 			tiflags &= ~(TH_PUSH|TH_FIN);
573 		}
574 	} else {
575 		/*
576 		 * If segment ends after window, drop trailing data
577 		 * (and PUSH and FIN); if nothing left, just ACK.
578 		 */
579 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
580 		if (todrop > 0) {
581 			if (todrop >= ti->ti_len) {
582 				/*
583 				 * If a new connection request is received
584 				 * while in TIME_WAIT, drop the old connection
585 				 * and start over if the sequence numbers
586 				 * are above the previous ones.
587 				 */
588 				if (tiflags & TH_SYN &&
589 				    tp->t_state == TCPS_TIME_WAIT &&
590 				    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
591 					iss = tp->rcv_nxt + TCP_ISSINCR;
592 					(void) tcp_close(tp);
593 					goto findpcb;
594 				}
595 				if (todrop == 1)
596 					tcpstat.tcps_rcvwinprobe++;
597 				else {
598 					tcpstat.tcps_rcvpackafterwin++;
599 					tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
600 				}
601 				goto dropafterack;
602 			}
603 			tcpstat.tcps_rcvpackafterwin++;
604 			tcpstat.tcps_rcvbyteafterwin += todrop;
605 			m_adj(m, -todrop);
606 			ti->ti_len -= todrop;
607 			tiflags &= ~(TH_PUSH|TH_FIN);
608 		}
609 	}
610 
611 #ifdef TCP_COMPAT_42
612 do_rst:
613 #endif
614 	/*
615 	 * If the RST bit is set examine the state:
616 	 *    SYN_RECEIVED STATE:
617 	 *	If passive open, return to LISTEN state.
618 	 *	If active open, inform user that connection was refused.
619 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
620 	 *	Inform user that connection was reset, and close tcb.
621 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
622 	 *	Close the tcb.
623 	 */
624 	if (tiflags&TH_RST) switch (tp->t_state) {
625 
626 	case TCPS_SYN_RECEIVED:
627 		tp = tcp_drop(tp, ECONNREFUSED);
628 		goto drop;
629 
630 	case TCPS_ESTABLISHED:
631 	case TCPS_FIN_WAIT_1:
632 	case TCPS_FIN_WAIT_2:
633 	case TCPS_CLOSE_WAIT:
634 		tp = tcp_drop(tp, ECONNRESET);
635 		goto drop;
636 
637 	case TCPS_CLOSING:
638 	case TCPS_LAST_ACK:
639 	case TCPS_TIME_WAIT:
640 		tp = tcp_close(tp);
641 		goto drop;
642 	}
643 
644 	/*
645 	 * If a SYN is in the window, then this is an
646 	 * error and we send an RST and drop the connection.
647 	 */
648 	if (tiflags & TH_SYN) {
649 		tp = tcp_drop(tp, ECONNRESET);
650 		goto dropwithreset;
651 	}
652 
653 	/*
654 	 * If the ACK bit is off we drop the segment and return.
655 	 */
656 	if ((tiflags & TH_ACK) == 0)
657 		goto drop;
658 
659 	/*
660 	 * Ack processing.
661 	 */
662 	switch (tp->t_state) {
663 
664 	/*
665 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
666 	 * ESTABLISHED state and continue processing, otherwise
667 	 * send an RST.
668 	 */
669 	case TCPS_SYN_RECEIVED:
670 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
671 		    SEQ_GT(ti->ti_ack, tp->snd_max))
672 			goto dropwithreset;
673 		tcpstat.tcps_connects++;
674 		soisconnected(so);
675 		tp->t_state = TCPS_ESTABLISHED;
676 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
677 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
678 		tp->snd_wl1 = ti->ti_seq - 1;
679 		/* fall into ... */
680 
681 	/*
682 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
683 	 * ACKs.  If the ack is in the range
684 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
685 	 * then advance tp->snd_una to ti->ti_ack and drop
686 	 * data from the retransmission queue.  If this ACK reflects
687 	 * more up to date window information we update our window information.
688 	 */
689 	case TCPS_ESTABLISHED:
690 	case TCPS_FIN_WAIT_1:
691 	case TCPS_FIN_WAIT_2:
692 	case TCPS_CLOSE_WAIT:
693 	case TCPS_CLOSING:
694 	case TCPS_LAST_ACK:
695 	case TCPS_TIME_WAIT:
696 
697 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
698 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
699 				tcpstat.tcps_rcvdupack++;
700 				/*
701 				 * If we have outstanding data (not a
702 				 * window probe), this is a completely
703 				 * duplicate ack (ie, window info didn't
704 				 * change), the ack is the biggest we've
705 				 * seen and we've seen exactly our rexmt
706 				 * threshhold of them, assume a packet
707 				 * has been dropped and retransmit it.
708 				 * Kludge snd_nxt & the congestion
709 				 * window so we send only this one
710 				 * packet.  If this packet fills the
711 				 * only hole in the receiver's seq.
712 				 * space, the next real ack will fully
713 				 * open our window.  This means we
714 				 * have to do the usual slow-start to
715 				 * not overwhelm an intermediate gateway
716 				 * with a burst of packets.  Leave
717 				 * here with the congestion window set
718 				 * to allow 2 packets on the next real
719 				 * ack and the exp-to-linear thresh
720 				 * set for half the current window
721 				 * size (since we know we're losing at
722 				 * the current window size).
723 				 */
724 				if (tp->t_timer[TCPT_REXMT] == 0 ||
725 				    ti->ti_ack != tp->snd_una)
726 					tp->t_dupacks = 0;
727 				else if (++tp->t_dupacks == tcprexmtthresh) {
728 					tcp_seq onxt = tp->snd_nxt;
729 					u_int win =
730 					    MIN(tp->snd_wnd, tp->snd_cwnd) / 2 /
731 						tp->t_maxseg;
732 
733 					if (win < 2)
734 						win = 2;
735 					tp->snd_ssthresh = win * tp->t_maxseg;
736 
737 					tp->t_timer[TCPT_REXMT] = 0;
738 					tp->t_rtt = 0;
739 					tp->snd_nxt = ti->ti_ack;
740 					tp->snd_cwnd = tp->t_maxseg;
741 					(void) tcp_output(tp);
742 
743 					if (SEQ_GT(onxt, tp->snd_nxt))
744 						tp->snd_nxt = onxt;
745 					goto drop;
746 				}
747 			} else
748 				tp->t_dupacks = 0;
749 			break;
750 		}
751 		tp->t_dupacks = 0;
752 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
753 			tcpstat.tcps_rcvacktoomuch++;
754 			goto dropafterack;
755 		}
756 		acked = ti->ti_ack - tp->snd_una;
757 		tcpstat.tcps_rcvackpack++;
758 		tcpstat.tcps_rcvackbyte += acked;
759 
760 		/*
761 		 * If transmit timer is running and timed sequence
762 		 * number was acked, update smoothed round trip time.
763 		 * Since we now have an rtt measurement, cancel the
764 		 * timer backoff (cf., Phil Karn's retransmit alg.).
765 		 * Recompute the initial retransmit timer.
766 		 */
767 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
768 			tcpstat.tcps_rttupdated++;
769 			if (tp->t_srtt != 0) {
770 				register short delta;
771 
772 				/*
773 				 * srtt is stored as fixed point with 3 bits
774 				 * after the binary point (i.e., scaled by 8).
775 				 * The following magic is equivalent
776 				 * to the smoothing algorithm in rfc793
777 				 * with an alpha of .875
778 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
779 				 * Adjust t_rtt to origin 0.
780 				 */
781 				tp->t_rtt--;
782 				delta = tp->t_rtt - (tp->t_srtt >> 3);
783 				if ((tp->t_srtt += delta) <= 0)
784 					tp->t_srtt = 1;
785 				/*
786 				 * We accumulate a smoothed rtt variance
787 				 * (actually, a smoothed mean difference),
788 				 * then set the retransmit timer to smoothed
789 				 * rtt + 2 times the smoothed variance.
790 				 * rttvar is stored as fixed point
791 				 * with 2 bits after the binary point
792 				 * (scaled by 4).  The following is equivalent
793 				 * to rfc793 smoothing with an alpha of .75
794 				 * (rttvar = rttvar*3/4 + |delta| / 4).
795 				 * This replaces rfc793's wired-in beta.
796 				 */
797 				if (delta < 0)
798 					delta = -delta;
799 				delta -= (tp->t_rttvar >> 2);
800 				if ((tp->t_rttvar += delta) <= 0)
801 					tp->t_rttvar = 1;
802 			} else {
803 				/*
804 				 * No rtt measurement yet - use the
805 				 * unsmoothed rtt.  Set the variance
806 				 * to half the rtt (so our first
807 				 * retransmit happens at 2*rtt)
808 				 */
809 				tp->t_srtt = tp->t_rtt << 3;
810 				tp->t_rttvar = tp->t_rtt << 1;
811 			}
812 			tp->t_rtt = 0;
813 			tp->t_rxtshift = 0;
814 			TCPT_RANGESET(tp->t_rxtcur,
815 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
816 			    TCPTV_MIN, TCPTV_REXMTMAX);
817 		}
818 
819 		/*
820 		 * If all outstanding data is acked, stop retransmit
821 		 * timer and remember to restart (more output or persist).
822 		 * If there is more data to be acked, restart retransmit
823 		 * timer, using current (possibly backed-off) value.
824 		 */
825 		if (ti->ti_ack == tp->snd_max) {
826 			tp->t_timer[TCPT_REXMT] = 0;
827 			needoutput = 1;
828 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
829 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
830 		/*
831 		 * When new data is acked, open the congestion window.
832 		 * If the window gives us less than ssthresh packets
833 		 * in flight, open exponentially (maxseg per packet).
834 		 * Otherwise open linearly (maxseg per window,
835 		 * or maxseg^2 / cwnd per packet).
836 		 */
837 		{
838 		u_int incr = tp->t_maxseg;
839 
840 		if (tp->snd_cwnd > tp->snd_ssthresh)
841 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
842 
843 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */
844 		}
845 		if (acked > so->so_snd.sb_cc) {
846 			tp->snd_wnd -= so->so_snd.sb_cc;
847 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
848 			ourfinisacked = 1;
849 		} else {
850 			sbdrop(&so->so_snd, acked);
851 			tp->snd_wnd -= acked;
852 			ourfinisacked = 0;
853 		}
854 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
855 			sowwakeup(so);
856 		tp->snd_una = ti->ti_ack;
857 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
858 			tp->snd_nxt = tp->snd_una;
859 
860 		switch (tp->t_state) {
861 
862 		/*
863 		 * In FIN_WAIT_1 STATE in addition to the processing
864 		 * for the ESTABLISHED state if our FIN is now acknowledged
865 		 * then enter FIN_WAIT_2.
866 		 */
867 		case TCPS_FIN_WAIT_1:
868 			if (ourfinisacked) {
869 				/*
870 				 * If we can't receive any more
871 				 * data, then closing user can proceed.
872 				 * Starting the timer is contrary to the
873 				 * specification, but if we don't get a FIN
874 				 * we'll hang forever.
875 				 */
876 				if (so->so_state & SS_CANTRCVMORE) {
877 					soisdisconnected(so);
878 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
879 				}
880 				tp->t_state = TCPS_FIN_WAIT_2;
881 			}
882 			break;
883 
884 	 	/*
885 		 * In CLOSING STATE in addition to the processing for
886 		 * the ESTABLISHED state if the ACK acknowledges our FIN
887 		 * then enter the TIME-WAIT state, otherwise ignore
888 		 * the segment.
889 		 */
890 		case TCPS_CLOSING:
891 			if (ourfinisacked) {
892 				tp->t_state = TCPS_TIME_WAIT;
893 				tcp_canceltimers(tp);
894 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
895 				soisdisconnected(so);
896 			}
897 			break;
898 
899 		/*
900 		 * In LAST_ACK, we may still be waiting for data to drain
901 		 * and/or to be acked, as well as for the ack of our FIN.
902 		 * If our FIN is now acknowledged, delete the TCB,
903 		 * enter the closed state and return.
904 		 */
905 		case TCPS_LAST_ACK:
906 			if (ourfinisacked) {
907 				tp = tcp_close(tp);
908 				goto drop;
909 			}
910 			break;
911 
912 		/*
913 		 * In TIME_WAIT state the only thing that should arrive
914 		 * is a retransmission of the remote FIN.  Acknowledge
915 		 * it and restart the finack timer.
916 		 */
917 		case TCPS_TIME_WAIT:
918 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
919 			goto dropafterack;
920 		}
921 	}
922 
923 step6:
924 	/*
925 	 * Update window information.
926 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
927 	 */
928 	if ((tiflags & TH_ACK) &&
929 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
930 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
931 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
932 		/* keep track of pure window updates */
933 		if (ti->ti_len == 0 &&
934 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
935 			tcpstat.tcps_rcvwinupd++;
936 		tp->snd_wnd = ti->ti_win;
937 		tp->snd_wl1 = ti->ti_seq;
938 		tp->snd_wl2 = ti->ti_ack;
939 		if (tp->snd_wnd > tp->max_sndwnd)
940 			tp->max_sndwnd = tp->snd_wnd;
941 		needoutput = 1;
942 	}
943 
944 	/*
945 	 * Process segments with URG.
946 	 */
947 	if ((tiflags & TH_URG) && ti->ti_urp &&
948 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
949 		/*
950 		 * This is a kludge, but if we receive and accept
951 		 * random urgent pointers, we'll crash in
952 		 * soreceive.  It's hard to imagine someone
953 		 * actually wanting to send this much urgent data.
954 		 */
955 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
956 			ti->ti_urp = 0;			/* XXX */
957 			tiflags &= ~TH_URG;		/* XXX */
958 			goto dodata;			/* XXX */
959 		}
960 		/*
961 		 * If this segment advances the known urgent pointer,
962 		 * then mark the data stream.  This should not happen
963 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
964 		 * a FIN has been received from the remote side.
965 		 * In these states we ignore the URG.
966 		 *
967 		 * According to RFC961 (Assigned Protocols),
968 		 * the urgent pointer points to the last octet
969 		 * of urgent data.  We continue, however,
970 		 * to consider it to indicate the first octet
971 		 * of data past the urgent section
972 		 * as the original spec states.
973 		 */
974 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
975 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
976 			so->so_oobmark = so->so_rcv.sb_cc +
977 			    (tp->rcv_up - tp->rcv_nxt) - 1;
978 			if (so->so_oobmark == 0)
979 				so->so_state |= SS_RCVATMARK;
980 			sohasoutofband(so);
981 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
982 		}
983 		/*
984 		 * Remove out of band data so doesn't get presented to user.
985 		 * This can happen independent of advancing the URG pointer,
986 		 * but if two URG's are pending at once, some out-of-band
987 		 * data may creep in... ick.
988 		 */
989 		if (ti->ti_urp <= ti->ti_len &&
990 		    (so->so_options & SO_OOBINLINE) == 0)
991 			tcp_pulloutofband(so, ti);
992 	} else
993 		/*
994 		 * If no out of band data is expected,
995 		 * pull receive urgent pointer along
996 		 * with the receive window.
997 		 */
998 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
999 			tp->rcv_up = tp->rcv_nxt;
1000 dodata:							/* XXX */
1001 
1002 	/*
1003 	 * Process the segment text, merging it into the TCP sequencing queue,
1004 	 * and arranging for acknowledgment of receipt if necessary.
1005 	 * This process logically involves adjusting tp->rcv_wnd as data
1006 	 * is presented to the user (this happens in tcp_usrreq.c,
1007 	 * case PRU_RCVD).  If a FIN has already been received on this
1008 	 * connection then we just ignore the text.
1009 	 */
1010 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
1011 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1012 		TCP_REASS(tp, ti, m, so, tiflags);
1013 		if (tcpnodelack == 0)
1014 			tp->t_flags |= TF_DELACK;
1015 		else
1016 			tp->t_flags |= TF_ACKNOW;
1017 		/*
1018 		 * Note the amount of data that peer has sent into
1019 		 * our window, in order to estimate the sender's
1020 		 * buffer size.
1021 		 */
1022 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1023 		if (len > tp->max_rcvd)
1024 			tp->max_rcvd = len;
1025 	} else {
1026 		m_freem(m);
1027 		tiflags &= ~TH_FIN;
1028 	}
1029 
1030 	/*
1031 	 * If FIN is received ACK the FIN and let the user know
1032 	 * that the connection is closing.
1033 	 */
1034 	if (tiflags & TH_FIN) {
1035 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1036 			socantrcvmore(so);
1037 			tp->t_flags |= TF_ACKNOW;
1038 			tp->rcv_nxt++;
1039 		}
1040 		switch (tp->t_state) {
1041 
1042 	 	/*
1043 		 * In SYN_RECEIVED and ESTABLISHED STATES
1044 		 * enter the CLOSE_WAIT state.
1045 		 */
1046 		case TCPS_SYN_RECEIVED:
1047 		case TCPS_ESTABLISHED:
1048 			tp->t_state = TCPS_CLOSE_WAIT;
1049 			break;
1050 
1051 	 	/*
1052 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1053 		 * enter the CLOSING state.
1054 		 */
1055 		case TCPS_FIN_WAIT_1:
1056 			tp->t_state = TCPS_CLOSING;
1057 			break;
1058 
1059 	 	/*
1060 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1061 		 * starting the time-wait timer, turning off the other
1062 		 * standard timers.
1063 		 */
1064 		case TCPS_FIN_WAIT_2:
1065 			tp->t_state = TCPS_TIME_WAIT;
1066 			tcp_canceltimers(tp);
1067 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1068 			soisdisconnected(so);
1069 			break;
1070 
1071 		/*
1072 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1073 		 */
1074 		case TCPS_TIME_WAIT:
1075 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1076 			break;
1077 		}
1078 	}
1079 	if (so->so_options & SO_DEBUG)
1080 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1081 
1082 	/*
1083 	 * Return any desired output.
1084 	 */
1085 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1086 		(void) tcp_output(tp);
1087 	return;
1088 
1089 dropafterack:
1090 	/*
1091 	 * Generate an ACK dropping incoming segment if it occupies
1092 	 * sequence space, where the ACK reflects our state.
1093 	 */
1094 	if (tiflags & TH_RST)
1095 		goto drop;
1096 	m_freem(m);
1097 	tp->t_flags |= TF_ACKNOW;
1098 	(void) tcp_output(tp);
1099 	return;
1100 
1101 dropwithreset:
1102 	if (om) {
1103 		(void) m_free(om);
1104 		om = 0;
1105 	}
1106 	/*
1107 	 * Generate a RST, dropping incoming segment.
1108 	 * Make ACK acceptable to originator of segment.
1109 	 * Don't bother to respond if destination was broadcast.
1110 	 */
1111 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
1112 		goto drop;
1113 	if (tiflags & TH_ACK)
1114 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
1115 	else {
1116 		if (tiflags & TH_SYN)
1117 			ti->ti_len++;
1118 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1119 		    TH_RST|TH_ACK);
1120 	}
1121 	/* destroy temporarily created socket */
1122 	if (dropsocket)
1123 		(void) soabort(so);
1124 	return;
1125 
1126 drop:
1127 	if (om)
1128 		(void) m_free(om);
1129 	/*
1130 	 * Drop space held by incoming segment and return.
1131 	 */
1132 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1133 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1134 	m_freem(m);
1135 	/* destroy temporarily created socket */
1136 	if (dropsocket)
1137 		(void) soabort(so);
1138 	return;
1139 }
1140 
1141 tcp_dooptions(tp, om, ti)
1142 	struct tcpcb *tp;
1143 	struct mbuf *om;
1144 	struct tcpiphdr *ti;
1145 {
1146 	register u_char *cp;
1147 	int opt, optlen, cnt;
1148 
1149 	cp = mtod(om, u_char *);
1150 	cnt = om->m_len;
1151 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1152 		opt = cp[0];
1153 		if (opt == TCPOPT_EOL)
1154 			break;
1155 		if (opt == TCPOPT_NOP)
1156 			optlen = 1;
1157 		else {
1158 			optlen = cp[1];
1159 			if (optlen <= 0)
1160 				break;
1161 		}
1162 		switch (opt) {
1163 
1164 		default:
1165 			break;
1166 
1167 		case TCPOPT_MAXSEG:
1168 			if (optlen != 4)
1169 				continue;
1170 			if (!(ti->ti_flags & TH_SYN))
1171 				continue;
1172 			tp->t_maxseg = *(u_short *)(cp + 2);
1173 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
1174 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
1175 			break;
1176 		}
1177 	}
1178 	(void) m_free(om);
1179 }
1180 
1181 /*
1182  * Pull out of band byte out of a segment so
1183  * it doesn't appear in the user's data queue.
1184  * It is still reflected in the segment length for
1185  * sequencing purposes.
1186  */
1187 tcp_pulloutofband(so, ti)
1188 	struct socket *so;
1189 	struct tcpiphdr *ti;
1190 {
1191 	register struct mbuf *m;
1192 	int cnt = ti->ti_urp - 1;
1193 
1194 	m = dtom(ti);
1195 	while (cnt >= 0) {
1196 		if (m->m_len > cnt) {
1197 			char *cp = mtod(m, caddr_t) + cnt;
1198 			struct tcpcb *tp = sototcpcb(so);
1199 
1200 			tp->t_iobc = *cp;
1201 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1202 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1203 			m->m_len--;
1204 			return;
1205 		}
1206 		cnt -= m->m_len;
1207 		m = m->m_next;
1208 		if (m == 0)
1209 			break;
1210 	}
1211 	panic("tcp_pulloutofband");
1212 }
1213 
1214 /*
1215  *  Determine a reasonable value for maxseg size.
1216  *  If the route is known, use one that can be handled
1217  *  on the given interface without forcing IP to fragment.
1218  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
1219  *  to utilize large mbufs.
1220  *  If interface pointer is unavailable, or the destination isn't local,
1221  *  use a conservative size (512 or the default IP max size, but no more
1222  *  than the mtu of the interface through which we route),
1223  *  as we can't discover anything about intervening gateways or networks.
1224  *  We also initialize the congestion/slow start window to be a single
1225  *  segment if the destination isn't local; this information should
1226  *  probably all be saved with the routing entry at the transport level.
1227  *
1228  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
1229  */
1230 tcp_mss(tp)
1231 	register struct tcpcb *tp;
1232 {
1233 	struct route *ro;
1234 	struct ifnet *ifp;
1235 	int mss;
1236 	struct inpcb *inp;
1237 
1238 	inp = tp->t_inpcb;
1239 	ro = &inp->inp_route;
1240 	if ((ro->ro_rt == (struct rtentry *)0) ||
1241 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
1242 		/* No route yet, so try to acquire one */
1243 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1244 			ro->ro_dst.sa_family = AF_INET;
1245 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1246 				inp->inp_faddr;
1247 			rtalloc(ro);
1248 		}
1249 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
1250 			return (TCP_MSS);
1251 	}
1252 
1253 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1254 #if	(MCLBYTES & (MCLBYTES - 1)) == 0
1255 	if (mss > MCLBYTES)
1256 		mss &= ~(MCLBYTES-1);
1257 #else
1258 	if (mss > MCLBYTES)
1259 		mss = mss / MCLBYTES * MCLBYTES;
1260 #endif
1261 	if (in_localaddr(inp->inp_faddr))
1262 		return (mss);
1263 
1264 	mss = MIN(mss, TCP_MSS);
1265 	tp->snd_cwnd = mss;
1266 	return (mss);
1267 }
1268