xref: /csrg-svn/sys/netinet/tcp_input.c (revision 33748)
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.15.1.2 (Berkeley) 03/16/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 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 #if BSD>=43
323 		inp->inp_options = ip_srcroute();
324 #endif
325 		tp = intotcpcb(inp);
326 		tp->t_state = TCPS_LISTEN;
327 	}
328 
329 	/*
330 	 * Segment received on connection.
331 	 * Reset idle time and keep-alive timer.
332 	 */
333 	tp->t_idle = 0;
334 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
335 
336 	/*
337 	 * Process options if not in LISTEN state,
338 	 * else do it below (after getting remote address).
339 	 */
340 	if (om && tp->t_state != TCPS_LISTEN) {
341 		tcp_dooptions(tp, om, ti);
342 		om = 0;
343 	}
344 
345 	/*
346 	 * Calculate amount of space in receive window,
347 	 * and then do TCP input processing.
348 	 * Receive window is amount of space in rcv queue,
349 	 * but not less than advertised window.
350 	 */
351 	{ int win;
352 
353 	win = sbspace(&so->so_rcv);
354 	if (win < 0)
355 		win = 0;
356 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
357 	}
358 
359 	switch (tp->t_state) {
360 
361 	/*
362 	 * If the state is LISTEN then ignore segment if it contains an RST.
363 	 * If the segment contains an ACK then it is bad and send a RST.
364 	 * If it does not contain a SYN then it is not interesting; drop it.
365 	 * Don't bother responding if the destination was a broadcast.
366 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
367 	 * tp->iss, and send a segment:
368 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
369 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
370 	 * Fill in remote peer address fields if not previously specified.
371 	 * Enter SYN_RECEIVED state, and process any other fields of this
372 	 * segment in this state.
373 	 */
374 	case TCPS_LISTEN: {
375 		struct mbuf *am;
376 		register struct sockaddr_in *sin;
377 
378 		if (tiflags & TH_RST)
379 			goto drop;
380 		if (tiflags & TH_ACK)
381 			goto dropwithreset;
382 		if ((tiflags & TH_SYN) == 0)
383 			goto drop;
384 		if (in_broadcast(ti->ti_dst))
385 			goto drop;
386 		am = m_get(M_DONTWAIT, MT_SONAME);
387 		if (am == NULL)
388 			goto drop;
389 		am->m_len = sizeof (struct sockaddr_in);
390 		sin = mtod(am, struct sockaddr_in *);
391 		sin->sin_family = AF_INET;
392 		sin->sin_addr = ti->ti_src;
393 		sin->sin_port = ti->ti_sport;
394 		laddr = inp->inp_laddr;
395 		if (inp->inp_laddr.s_addr == INADDR_ANY)
396 			inp->inp_laddr = ti->ti_dst;
397 		if (in_pcbconnect(inp, am)) {
398 			inp->inp_laddr = laddr;
399 			(void) m_free(am);
400 			goto drop;
401 		}
402 		(void) m_free(am);
403 		tp->t_template = tcp_template(tp);
404 		if (tp->t_template == 0) {
405 			tp = tcp_drop(tp, ENOBUFS);
406 			dropsocket = 0;		/* socket is already gone */
407 			goto drop;
408 		}
409 		if (om) {
410 			tcp_dooptions(tp, om, ti);
411 			om = 0;
412 		}
413 		if (iss)
414 			tp->iss = iss;
415 		else
416 			tp->iss = tcp_iss;
417 		tcp_iss += TCP_ISSINCR/2;
418 		tp->irs = ti->ti_seq;
419 		tcp_sendseqinit(tp);
420 		tcp_rcvseqinit(tp);
421 		tp->t_flags |= TF_ACKNOW;
422 		tp->t_state = TCPS_SYN_RECEIVED;
423 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
424 		dropsocket = 0;		/* committed to socket */
425 		tcpstat.tcps_accepts++;
426 		goto trimthenstep6;
427 		}
428 
429 	/*
430 	 * If the state is SYN_SENT:
431 	 *	if seg contains an ACK, but not for our SYN, drop the input.
432 	 *	if seg contains a RST, then drop the connection.
433 	 *	if seg does not contain SYN, then drop it.
434 	 * Otherwise this is an acceptable SYN segment
435 	 *	initialize tp->rcv_nxt and tp->irs
436 	 *	if seg contains ack then advance tp->snd_una
437 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
438 	 *	arrange for segment to be acked (eventually)
439 	 *	continue processing rest of data/controls, beginning with URG
440 	 */
441 	case TCPS_SYN_SENT:
442 		if ((tiflags & TH_ACK) &&
443 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
444 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
445 			goto dropwithreset;
446 		if (tiflags & TH_RST) {
447 			if (tiflags & TH_ACK)
448 				tp = tcp_drop(tp, ECONNREFUSED);
449 			goto drop;
450 		}
451 		if ((tiflags & TH_SYN) == 0)
452 			goto drop;
453 		if (tiflags & TH_ACK) {
454 			tp->snd_una = ti->ti_ack;
455 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
456 				tp->snd_nxt = tp->snd_una;
457 		}
458 		tp->t_timer[TCPT_REXMT] = 0;
459 		tp->irs = ti->ti_seq;
460 		tcp_rcvseqinit(tp);
461 		tp->t_flags |= TF_ACKNOW;
462 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
463 			tcpstat.tcps_connects++;
464 			soisconnected(so);
465 			tp->t_state = TCPS_ESTABLISHED;
466 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
467 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
468 			/*
469 			 * if we didn't have to retransmit the SYN,
470 			 * use its rtt as our initial srtt & rtt var.
471 			 */
472 			if (tp->t_rtt) {
473 				tp->t_srtt = tp->t_rtt << 3;
474 				tp->t_rttvar = tp->t_rtt << 1;
475 				TCPT_RANGESET(tp->t_rxtcur,
476 				    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
477 				    TCPTV_MIN, TCPTV_REXMTMAX);
478 				tp->t_rtt = 0;
479 			}
480 		} else
481 			tp->t_state = TCPS_SYN_RECEIVED;
482 
483 trimthenstep6:
484 		/*
485 		 * Advance ti->ti_seq to correspond to first data byte.
486 		 * If data, trim to stay within window,
487 		 * dropping FIN if necessary.
488 		 */
489 		ti->ti_seq++;
490 		if (ti->ti_len > tp->rcv_wnd) {
491 			todrop = ti->ti_len - tp->rcv_wnd;
492 #if BSD>=43
493 			m_adj(m, -todrop);
494 #else
495 			/* XXX work around 4.2 m_adj bug */
496 			if (m->m_len) {
497 				m_adj(m, -todrop);
498 			} else {
499 				/* skip tcp/ip header in first mbuf */
500 				m_adj(m->m_next, -todrop);
501 			}
502 #endif
503 			ti->ti_len = tp->rcv_wnd;
504 			tiflags &= ~TH_FIN;
505 			tcpstat.tcps_rcvpackafterwin++;
506 			tcpstat.tcps_rcvbyteafterwin += todrop;
507 		}
508 		tp->snd_wl1 = ti->ti_seq - 1;
509 		tp->rcv_up = ti->ti_seq;
510 		goto step6;
511 	}
512 
513 	/*
514 	 * States other than LISTEN or SYN_SENT.
515 	 * First check that at least some bytes of segment are within
516 	 * receive window.  If segment begins before rcv_nxt,
517 	 * drop leading data (and SYN); if nothing left, just ack.
518 	 */
519 	todrop = tp->rcv_nxt - ti->ti_seq;
520 	if (todrop > 0) {
521 		if (tiflags & TH_SYN) {
522 			tiflags &= ~TH_SYN;
523 			ti->ti_seq++;
524 			if (ti->ti_urp > 1)
525 				ti->ti_urp--;
526 			else
527 				tiflags &= ~TH_URG;
528 			todrop--;
529 		}
530 		if (todrop > ti->ti_len ||
531 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
532 			tcpstat.tcps_rcvduppack++;
533 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
534 			/*
535 			 * If segment is just one to the left of the window,
536 			 * check two special cases:
537 			 * 1. Don't toss RST in response to 4.2-style keepalive.
538 			 * 2. If the only thing to drop is a FIN, we can drop
539 			 *    it, but check the ACK or we will get into FIN
540 			 *    wars if our FINs crossed (both CLOSING).
541 			 * In either case, send ACK to resynchronize,
542 			 * but keep on processing for RST or ACK.
543 			 */
544 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
545 #ifdef TCP_COMPAT_42
546 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
547 #endif
548 			   ) {
549 				todrop = ti->ti_len;
550 				tiflags &= ~TH_FIN;
551 				tp->t_flags |= TF_ACKNOW;
552 			} else
553 				goto dropafterack;
554 		} else {
555 			tcpstat.tcps_rcvpartduppack++;
556 			tcpstat.tcps_rcvpartdupbyte += todrop;
557 		}
558 		m_adj(m, todrop);
559 		ti->ti_seq += todrop;
560 		ti->ti_len -= todrop;
561 		if (ti->ti_urp > todrop)
562 			ti->ti_urp -= todrop;
563 		else {
564 			tiflags &= ~TH_URG;
565 			ti->ti_urp = 0;
566 		}
567 	}
568 
569 	/*
570 	 * If new data are received on a connection after the
571 	 * user processes are gone, then RST the other end.
572 	 */
573 	if ((so->so_state & SS_NOFDREF) &&
574 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
575 		tp = tcp_close(tp);
576 		tcpstat.tcps_rcvafterclose++;
577 		goto dropwithreset;
578 	}
579 
580 	/*
581 	 * If segment ends after window, drop trailing data
582 	 * (and PUSH and FIN); if nothing left, just ACK.
583 	 */
584 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
585 	if (todrop > 0) {
586 		tcpstat.tcps_rcvpackafterwin++;
587 		if (todrop >= ti->ti_len) {
588 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
589 			/*
590 			 * If a new connection request is received
591 			 * while in TIME_WAIT, drop the old connection
592 			 * and start over if the sequence numbers
593 			 * are above the previous ones.
594 			 */
595 			if (tiflags & TH_SYN &&
596 			    tp->t_state == TCPS_TIME_WAIT &&
597 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
598 				iss = tp->rcv_nxt + TCP_ISSINCR;
599 				(void) tcp_close(tp);
600 				goto findpcb;
601 			}
602 			/*
603 			 * If window is closed can only take segments at
604 			 * window edge, and have to drop data and PUSH from
605 			 * incoming segments.  Continue processing, but
606 			 * remember to ack.  Otherwise, drop segment
607 			 * and ack.
608 			 */
609 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
610 				tp->t_flags |= TF_ACKNOW;
611 				tcpstat.tcps_rcvwinprobe++;
612 			} else
613 				goto dropafterack;
614 		} else
615 			tcpstat.tcps_rcvbyteafterwin += todrop;
616 #if BSD>=43
617 		m_adj(m, -todrop);
618 #else
619 		/* XXX work around m_adj bug */
620 		if (m->m_len) {
621 			m_adj(m, -todrop);
622 		} else {
623 			/* skip tcp/ip header in first mbuf */
624 			m_adj(m->m_next, -todrop);
625 		}
626 #endif
627 		ti->ti_len -= todrop;
628 		tiflags &= ~(TH_PUSH|TH_FIN);
629 	}
630 
631 	/*
632 	 * If the RST bit is set examine the state:
633 	 *    SYN_RECEIVED STATE:
634 	 *	If passive open, return to LISTEN state.
635 	 *	If active open, inform user that connection was refused.
636 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
637 	 *	Inform user that connection was reset, and close tcb.
638 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
639 	 *	Close the tcb.
640 	 */
641 	if (tiflags&TH_RST) switch (tp->t_state) {
642 
643 	case TCPS_SYN_RECEIVED:
644 		so->so_error = ECONNREFUSED;
645 		goto close;
646 
647 	case TCPS_ESTABLISHED:
648 	case TCPS_FIN_WAIT_1:
649 	case TCPS_FIN_WAIT_2:
650 	case TCPS_CLOSE_WAIT:
651 		so->so_error = ECONNRESET;
652 	close:
653 		tp->t_state = TCPS_CLOSED;
654 		tcpstat.tcps_drops++;
655 		tp = tcp_close(tp);
656 		goto drop;
657 
658 	case TCPS_CLOSING:
659 	case TCPS_LAST_ACK:
660 	case TCPS_TIME_WAIT:
661 		tp = tcp_close(tp);
662 		goto drop;
663 	}
664 
665 	/*
666 	 * If a SYN is in the window, then this is an
667 	 * error and we send an RST and drop the connection.
668 	 */
669 	if (tiflags & TH_SYN) {
670 		tp = tcp_drop(tp, ECONNRESET);
671 		goto dropwithreset;
672 	}
673 
674 	/*
675 	 * If the ACK bit is off we drop the segment and return.
676 	 */
677 	if ((tiflags & TH_ACK) == 0)
678 		goto drop;
679 
680 	/*
681 	 * Ack processing.
682 	 */
683 	switch (tp->t_state) {
684 
685 	/*
686 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
687 	 * ESTABLISHED state and continue processing, otherwise
688 	 * send an RST.
689 	 */
690 	case TCPS_SYN_RECEIVED:
691 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
692 		    SEQ_GT(ti->ti_ack, tp->snd_max))
693 			goto dropwithreset;
694 		tcpstat.tcps_connects++;
695 		soisconnected(so);
696 		tp->t_state = TCPS_ESTABLISHED;
697 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
698 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
699 		tp->snd_wl1 = ti->ti_seq - 1;
700 		/* fall into ... */
701 
702 	/*
703 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
704 	 * ACKs.  If the ack is in the range
705 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
706 	 * then advance tp->snd_una to ti->ti_ack and drop
707 	 * data from the retransmission queue.  If this ACK reflects
708 	 * more up to date window information we update our window information.
709 	 */
710 	case TCPS_ESTABLISHED:
711 	case TCPS_FIN_WAIT_1:
712 	case TCPS_FIN_WAIT_2:
713 	case TCPS_CLOSE_WAIT:
714 	case TCPS_CLOSING:
715 	case TCPS_LAST_ACK:
716 	case TCPS_TIME_WAIT:
717 
718 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
719 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
720 				tcpstat.tcps_rcvdupack++;
721 				/*
722 				 * If we have outstanding data (not a
723 				 * window probe), this is a completely
724 				 * duplicate ack (ie, window info didn't
725 				 * change), the ack is the biggest we've
726 				 * seen and we've seen exactly our rexmt
727 				 * threshhold of them, assume a packet
728 				 * has been dropped and retransmit it.
729 				 * Kludge snd_nxt & the congestion
730 				 * window so we send only this one
731 				 * packet.  If this packet fills the
732 				 * only hole in the receiver's seq.
733 				 * space, the next real ack will fully
734 				 * open our window.  This means we
735 				 * have to do the usual slow-start to
736 				 * not overwhelm an intermediate gateway
737 				 * with a burst of packets.  Leave
738 				 * here with the congestion window set
739 				 * to allow 2 packets on the next real
740 				 * ack and the exp-to-linear thresh
741 				 * set for half the current window
742 				 * size (since we know we're losing at
743 				 * the current window size).
744 				 */
745 				if (tp->t_timer[TCPT_REXMT] == 0 ||
746 				    ti->ti_ack != tp->snd_una)
747 					tp->t_dupacks = 0;
748 				else if (++tp->t_dupacks == tcprexmtthresh) {
749 					tcp_seq onxt = tp->snd_nxt;
750 					u_int win =
751 					    MIN(tp->snd_wnd, tp->snd_cwnd) / 2 /
752 						tp->t_maxseg;
753 
754 					if (win < 2)
755 						win = 2;
756 					tp->snd_ssthresh = win * tp->t_maxseg;
757 
758 					tp->t_timer[TCPT_REXMT] = 0;
759 					tp->t_rtt = 0;
760 					tp->snd_nxt = ti->ti_ack;
761 					tp->snd_cwnd = tp->t_maxseg;
762 					(void) tcp_output(tp);
763 
764 					if (SEQ_GT(onxt, tp->snd_nxt))
765 						tp->snd_nxt = onxt;
766 					goto drop;
767 				}
768 			} else
769 				tp->t_dupacks = 0;
770 			break;
771 		}
772 		tp->t_dupacks = 0;
773 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
774 			tcpstat.tcps_rcvacktoomuch++;
775 			goto dropafterack;
776 		}
777 		acked = ti->ti_ack - tp->snd_una;
778 		tcpstat.tcps_rcvackpack++;
779 		tcpstat.tcps_rcvackbyte += acked;
780 
781 		/*
782 		 * If transmit timer is running and timed sequence
783 		 * number was acked, update smoothed round trip time.
784 		 * Since we now have an rtt measurement, cancel the
785 		 * timer backoff (cf., Phil Karn's retransmit alg.).
786 		 * Recompute the initial retransmit timer.
787 		 */
788 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
789 			tcpstat.tcps_rttupdated++;
790 			if (tp->t_srtt != 0) {
791 				register short delta;
792 
793 				/*
794 				 * srtt is stored as fixed point with 3 bits
795 				 * after the binary point (i.e., scaled by 8).
796 				 * The following magic is equivalent
797 				 * to the smoothing algorithm in rfc793
798 				 * with an alpha of .875
799 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
800 				 * Adjust t_rtt to origin 0.
801 				 */
802 				delta = tp->t_rtt - 1 - (tp->t_srtt >> 3);
803 				if ((tp->t_srtt += delta) <= 0)
804 					tp->t_srtt = 1;
805 				/*
806 				 * We accumulate a smoothed rtt variance
807 				 * (actually, a smoothed mean difference),
808 				 * then set the retransmit timer to smoothed
809 				 * rtt + 2 times the smoothed variance.
810 				 * rttvar is stored as fixed point
811 				 * with 2 bits after the binary point
812 				 * (scaled by 4).  The following is equivalent
813 				 * to rfc793 smoothing with an alpha of .75
814 				 * (rttvar = rttvar*3/4 + |delta| / 4).
815 				 * This replaces rfc793's wired-in beta.
816 				 */
817 				if (delta < 0)
818 					delta = -delta;
819 				delta -= (tp->t_rttvar >> 2);
820 				if ((tp->t_rttvar += delta) <= 0)
821 					tp->t_rttvar = 1;
822 			} else {
823 				/*
824 				 * No rtt measurement yet - use the
825 				 * unsmoothed rtt.  Set the variance
826 				 * to half the rtt (so our first
827 				 * retransmit happens at 2*rtt)
828 				 */
829 				tp->t_srtt = tp->t_rtt << 3;
830 				tp->t_rttvar = tp->t_rtt << 1;
831 			}
832 			tp->t_rtt = 0;
833 			tp->t_rxtshift = 0;
834 			TCPT_RANGESET(tp->t_rxtcur,
835 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
836 			    TCPTV_MIN, TCPTV_REXMTMAX);
837 		}
838 
839 		/*
840 		 * If all outstanding data is acked, stop retransmit
841 		 * timer and remember to restart (more output or persist).
842 		 * If there is more data to be acked, restart retransmit
843 		 * timer, using current (possibly backed-off) value.
844 		 */
845 		if (ti->ti_ack == tp->snd_max) {
846 			tp->t_timer[TCPT_REXMT] = 0;
847 			needoutput = 1;
848 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
849 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
850 		/*
851 		 * When new data is acked, open the congestion window.
852 		 * If the window gives us less than ssthresh packets
853 		 * in flight, open exponentially (maxseg per packet).
854 		 * Otherwise open linearly (maxseg per window,
855 		 * or maxseg^2 / cwnd per packet).
856 		 */
857 		{
858 		u_int incr = tp->t_maxseg;
859 
860 		if (tp->snd_cwnd > tp->snd_ssthresh)
861 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
862 
863 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, IP_MAXPACKET); /* XXX */
864 		}
865 		if (acked > so->so_snd.sb_cc) {
866 			tp->snd_wnd -= so->so_snd.sb_cc;
867 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
868 			ourfinisacked = 1;
869 		} else {
870 			sbdrop(&so->so_snd, acked);
871 			tp->snd_wnd -= acked;
872 			ourfinisacked = 0;
873 		}
874 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
875 			sowwakeup(so);
876 		tp->snd_una = ti->ti_ack;
877 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
878 			tp->snd_nxt = tp->snd_una;
879 
880 		switch (tp->t_state) {
881 
882 		/*
883 		 * In FIN_WAIT_1 STATE in addition to the processing
884 		 * for the ESTABLISHED state if our FIN is now acknowledged
885 		 * then enter FIN_WAIT_2.
886 		 */
887 		case TCPS_FIN_WAIT_1:
888 			if (ourfinisacked) {
889 				/*
890 				 * If we can't receive any more
891 				 * data, then closing user can proceed.
892 				 * Starting the timer is contrary to the
893 				 * specification, but if we don't get a FIN
894 				 * we'll hang forever.
895 				 */
896 				if (so->so_state & SS_CANTRCVMORE) {
897 					soisdisconnected(so);
898 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
899 				}
900 				tp->t_state = TCPS_FIN_WAIT_2;
901 			}
902 			break;
903 
904 	 	/*
905 		 * In CLOSING STATE in addition to the processing for
906 		 * the ESTABLISHED state if the ACK acknowledges our FIN
907 		 * then enter the TIME-WAIT state, otherwise ignore
908 		 * the segment.
909 		 */
910 		case TCPS_CLOSING:
911 			if (ourfinisacked) {
912 				tp->t_state = TCPS_TIME_WAIT;
913 				tcp_canceltimers(tp);
914 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
915 				soisdisconnected(so);
916 			}
917 			break;
918 
919 		/*
920 		 * In LAST_ACK, we may still be waiting for data to drain
921 		 * and/or to be acked, as well as for the ack of our FIN.
922 		 * If our FIN is now acknowledged, delete the TCB,
923 		 * enter the closed state and return.
924 		 */
925 		case TCPS_LAST_ACK:
926 			if (ourfinisacked) {
927 				tp = tcp_close(tp);
928 				goto drop;
929 			}
930 			break;
931 
932 		/*
933 		 * In TIME_WAIT state the only thing that should arrive
934 		 * is a retransmission of the remote FIN.  Acknowledge
935 		 * it and restart the finack timer.
936 		 */
937 		case TCPS_TIME_WAIT:
938 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
939 			goto dropafterack;
940 		}
941 	}
942 
943 step6:
944 	/*
945 	 * Update window information.
946 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
947 	 */
948 	if ((tiflags & TH_ACK) &&
949 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
950 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
951 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
952 		/* keep track of pure window updates */
953 		if (ti->ti_len == 0 &&
954 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
955 			tcpstat.tcps_rcvwinupd++;
956 		tp->snd_wnd = ti->ti_win;
957 		tp->snd_wl1 = ti->ti_seq;
958 		tp->snd_wl2 = ti->ti_ack;
959 		if (tp->snd_wnd > tp->max_sndwnd)
960 			tp->max_sndwnd = tp->snd_wnd;
961 		needoutput = 1;
962 	}
963 
964 	/*
965 	 * Process segments with URG.
966 	 */
967 	if ((tiflags & TH_URG) && ti->ti_urp &&
968 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
969 		/*
970 		 * This is a kludge, but if we receive and accept
971 		 * random urgent pointers, we'll crash in
972 		 * soreceive.  It's hard to imagine someone
973 		 * actually wanting to send this much urgent data.
974 		 */
975 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
976 			ti->ti_urp = 0;			/* XXX */
977 			tiflags &= ~TH_URG;		/* XXX */
978 			goto dodata;			/* XXX */
979 		}
980 		/*
981 		 * If this segment advances the known urgent pointer,
982 		 * then mark the data stream.  This should not happen
983 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
984 		 * a FIN has been received from the remote side.
985 		 * In these states we ignore the URG.
986 		 *
987 		 * According to RFC961 (Assigned Protocols),
988 		 * the urgent pointer points to the last octet
989 		 * of urgent data.  We continue, however,
990 		 * to consider it to indicate the first octet
991 		 * of data past the urgent section
992 		 * as the original spec states.
993 		 */
994 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
995 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
996 			so->so_oobmark = so->so_rcv.sb_cc +
997 			    (tp->rcv_up - tp->rcv_nxt) - 1;
998 			if (so->so_oobmark == 0)
999 				so->so_state |= SS_RCVATMARK;
1000 			sohasoutofband(so);
1001 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1002 		}
1003 		/*
1004 		 * Remove out of band data so doesn't get presented to user.
1005 		 * This can happen independent of advancing the URG pointer,
1006 		 * but if two URG's are pending at once, some out-of-band
1007 		 * data may creep in... ick.
1008 		 */
1009 		if (ti->ti_urp <= ti->ti_len
1010 #ifdef SO_OOBINLINE
1011 		     && (so->so_options & SO_OOBINLINE) == 0
1012 #endif
1013 							   )
1014 			tcp_pulloutofband(so, ti);
1015 	} else
1016 		/*
1017 		 * If no out of band data is expected,
1018 		 * pull receive urgent pointer along
1019 		 * with the receive window.
1020 		 */
1021 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1022 			tp->rcv_up = tp->rcv_nxt;
1023 dodata:							/* XXX */
1024 
1025 	/*
1026 	 * Process the segment text, merging it into the TCP sequencing queue,
1027 	 * and arranging for acknowledgment of receipt if necessary.
1028 	 * This process logically involves adjusting tp->rcv_wnd as data
1029 	 * is presented to the user (this happens in tcp_usrreq.c,
1030 	 * case PRU_RCVD).  If a FIN has already been received on this
1031 	 * connection then we just ignore the text.
1032 	 */
1033 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
1034 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1035 		TCP_REASS(tp, ti, m, so, tiflags);
1036 		if (tcpnodelack == 0)
1037 			tp->t_flags |= TF_DELACK;
1038 		else
1039 			tp->t_flags |= TF_ACKNOW;
1040 		/*
1041 		 * Note the amount of data that peer has sent into
1042 		 * our window, in order to estimate the sender's
1043 		 * buffer size.
1044 		 */
1045 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1046 		if (len > tp->max_rcvd)
1047 			tp->max_rcvd = len;
1048 	} else {
1049 		m_freem(m);
1050 		tiflags &= ~TH_FIN;
1051 	}
1052 
1053 	/*
1054 	 * If FIN is received ACK the FIN and let the user know
1055 	 * that the connection is closing.
1056 	 */
1057 	if (tiflags & TH_FIN) {
1058 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1059 			socantrcvmore(so);
1060 			tp->t_flags |= TF_ACKNOW;
1061 			tp->rcv_nxt++;
1062 		}
1063 		switch (tp->t_state) {
1064 
1065 	 	/*
1066 		 * In SYN_RECEIVED and ESTABLISHED STATES
1067 		 * enter the CLOSE_WAIT state.
1068 		 */
1069 		case TCPS_SYN_RECEIVED:
1070 		case TCPS_ESTABLISHED:
1071 			tp->t_state = TCPS_CLOSE_WAIT;
1072 			break;
1073 
1074 	 	/*
1075 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1076 		 * enter the CLOSING state.
1077 		 */
1078 		case TCPS_FIN_WAIT_1:
1079 			tp->t_state = TCPS_CLOSING;
1080 			break;
1081 
1082 	 	/*
1083 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1084 		 * starting the time-wait timer, turning off the other
1085 		 * standard timers.
1086 		 */
1087 		case TCPS_FIN_WAIT_2:
1088 			tp->t_state = TCPS_TIME_WAIT;
1089 			tcp_canceltimers(tp);
1090 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1091 			soisdisconnected(so);
1092 			break;
1093 
1094 		/*
1095 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1096 		 */
1097 		case TCPS_TIME_WAIT:
1098 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1099 			break;
1100 		}
1101 	}
1102 	if (so->so_options & SO_DEBUG)
1103 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1104 
1105 	/*
1106 	 * Return any desired output.
1107 	 */
1108 	if (needoutput || (tp->t_flags & TF_ACKNOW))
1109 		(void) tcp_output(tp);
1110 	return;
1111 
1112 dropafterack:
1113 	/*
1114 	 * Generate an ACK dropping incoming segment if it occupies
1115 	 * sequence space, where the ACK reflects our state.
1116 	 */
1117 	if (tiflags & TH_RST)
1118 		goto drop;
1119 	m_freem(m);
1120 	tp->t_flags |= TF_ACKNOW;
1121 	(void) tcp_output(tp);
1122 	return;
1123 
1124 dropwithreset:
1125 	if (om) {
1126 		(void) m_free(om);
1127 		om = 0;
1128 	}
1129 	/*
1130 	 * Generate a RST, dropping incoming segment.
1131 	 * Make ACK acceptable to originator of segment.
1132 	 * Don't bother to respond if destination was broadcast.
1133 	 */
1134 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
1135 		goto drop;
1136 	if (tiflags & TH_ACK)
1137 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
1138 	else {
1139 		if (tiflags & TH_SYN)
1140 			ti->ti_len++;
1141 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1142 		    TH_RST|TH_ACK);
1143 	}
1144 	/* destroy temporarily created socket */
1145 	if (dropsocket)
1146 		(void) soabort(so);
1147 	return;
1148 
1149 drop:
1150 	if (om)
1151 		(void) m_free(om);
1152 	/*
1153 	 * Drop space held by incoming segment and return.
1154 	 */
1155 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1156 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1157 	m_freem(m);
1158 	/* destroy temporarily created socket */
1159 	if (dropsocket)
1160 		(void) soabort(so);
1161 	return;
1162 }
1163 
1164 tcp_dooptions(tp, om, ti)
1165 	struct tcpcb *tp;
1166 	struct mbuf *om;
1167 	struct tcpiphdr *ti;
1168 {
1169 	register u_char *cp;
1170 	int opt, optlen, cnt;
1171 
1172 	cp = mtod(om, u_char *);
1173 	cnt = om->m_len;
1174 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1175 		opt = cp[0];
1176 		if (opt == TCPOPT_EOL)
1177 			break;
1178 		if (opt == TCPOPT_NOP)
1179 			optlen = 1;
1180 		else {
1181 			optlen = cp[1];
1182 			if (optlen <= 0)
1183 				break;
1184 		}
1185 		switch (opt) {
1186 
1187 		default:
1188 			break;
1189 
1190 		case TCPOPT_MAXSEG:
1191 			if (optlen != 4)
1192 				continue;
1193 			if (!(ti->ti_flags & TH_SYN))
1194 				continue;
1195 			tp->t_maxseg = *(u_short *)(cp + 2);
1196 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
1197 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
1198 			break;
1199 		}
1200 	}
1201 	(void) m_free(om);
1202 }
1203 
1204 /*
1205  * Pull out of band byte out of a segment so
1206  * it doesn't appear in the user's data queue.
1207  * It is still reflected in the segment length for
1208  * sequencing purposes.
1209  */
1210 tcp_pulloutofband(so, ti)
1211 	struct socket *so;
1212 	struct tcpiphdr *ti;
1213 {
1214 	register struct mbuf *m;
1215 	int cnt = ti->ti_urp - 1;
1216 
1217 	m = dtom(ti);
1218 	while (cnt >= 0) {
1219 		if (m->m_len > cnt) {
1220 			char *cp = mtod(m, caddr_t) + cnt;
1221 			struct tcpcb *tp = sototcpcb(so);
1222 
1223 			tp->t_iobc = *cp;
1224 			tp->t_oobflags |= TCPOOB_HAVEDATA;
1225 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1226 			m->m_len--;
1227 			return;
1228 		}
1229 		cnt -= m->m_len;
1230 		m = m->m_next;
1231 		if (m == 0)
1232 			break;
1233 	}
1234 	panic("tcp_pulloutofband");
1235 }
1236 
1237 /*
1238  *  Determine a reasonable value for maxseg size.
1239  *  If the route is known, use one that can be handled
1240  *  on the given interface without forcing IP to fragment.
1241  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
1242  *  to utilize large mbufs.
1243  *  If interface pointer is unavailable, or the destination isn't local,
1244  *  use a conservative size (512 or the default IP max size, but no more
1245  *  than the mtu of the interface through which we route),
1246  *  as we can't discover anything about intervening gateways or networks.
1247  *  We also initialize the congestion/slow start window to be a single
1248  *  segment if the destination isn't local; this information should
1249  *  probably all be saved with the routing entry at the transport level.
1250  *
1251  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
1252  */
1253 tcp_mss(tp)
1254 	register struct tcpcb *tp;
1255 {
1256 	struct route *ro;
1257 	struct ifnet *ifp;
1258 	int mss;
1259 	struct inpcb *inp;
1260 
1261 	inp = tp->t_inpcb;
1262 	ro = &inp->inp_route;
1263 	if ((ro->ro_rt == (struct rtentry *)0) ||
1264 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
1265 		/* No route yet, so try to acquire one */
1266 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1267 			ro->ro_dst.sa_family = AF_INET;
1268 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1269 				inp->inp_faddr;
1270 			rtalloc(ro);
1271 		}
1272 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
1273 			return (TCP_MSS);
1274 	}
1275 
1276 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1277 #if	(MCLBYTES & (MCLBYTES - 1)) == 0
1278 	if (mss > MCLBYTES)
1279 		mss &= ~(MCLBYTES-1);
1280 #else
1281 	if (mss > MCLBYTES)
1282 		mss = mss / MCLBYTES * MCLBYTES;
1283 #endif
1284 	if (in_localaddr(inp->inp_faddr))
1285 		return (mss);
1286 
1287 	mss = MIN(mss, TCP_MSS);
1288 	tp->snd_cwnd = mss;
1289 	return (mss);
1290 }
1291 
1292 #if BSD<43
1293 /* XXX this belongs in netinet/in.c */
1294 in_localaddr(in)
1295 	struct in_addr in;
1296 {
1297 	register u_long i = ntohl(in.s_addr);
1298 	register struct ifnet *ifp;
1299 	register struct sockaddr_in *sin;
1300 	register u_long mask;
1301 
1302 	if (IN_CLASSA(i))
1303 		mask = IN_CLASSA_NET;
1304 	else if (IN_CLASSB(i))
1305 		mask = IN_CLASSB_NET;
1306 	else if (IN_CLASSC(i))
1307 		mask = IN_CLASSC_NET;
1308 	else
1309 		return (0);
1310 
1311 	i &= mask;
1312 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
1313 		if (ifp->if_addr.sa_family != AF_INET)
1314 			continue;
1315 		sin = (struct sockaddr_in *)&ifp->if_addr;
1316 		if ((sin->sin_addr.s_addr & mask) == i)
1317 			return (1);
1318 	}
1319 	return (0);
1320 }
1321 #endif
1322