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