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