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