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