Get rid of a few more bytes with TCPDEBUG.
[unix-history] / sys / netinet / tcp_input.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
38e82238 33 * from: @(#)tcp_input.c 7.25 (Berkeley) 6/30/90
60dbdfb8 34 * $Id: tcp_input.c,v 1.5 1994/01/24 05:12:32 davidg Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "malloc.h"
40#include "mbuf.h"
41#include "protosw.h"
42#include "socket.h"
43#include "socketvar.h"
44#include "errno.h"
45
46#include "../net/if.h"
47#include "../net/route.h"
48
49#include "in.h"
50#include "in_systm.h"
51#include "ip.h"
52#include "in_pcb.h"
53#include "ip_var.h"
54#include "tcp.h"
55#include "tcp_fsm.h"
56#include "tcp_seq.h"
57#include "tcp_timer.h"
58#include "tcp_var.h"
59#include "tcpip.h"
ff1a3ad7 60#ifdef TCPDEBUG
15637ed4 61#include "tcp_debug.h"
ff1a3ad7 62#endif
15637ed4 63
4c45483e
GW
64static void tcp_dooptions(struct tcpcb *, struct mbuf *, struct tcpiphdr *);
65static void tcp_pulloutofband(struct socket *, struct tcpiphdr *, struct mbuf *);
66static void tcp_xmit_timer(struct tcpcb *);
67
15637ed4
RG
68int tcprexmtthresh = 3;
69int tcppredack; /* XXX debugging: times hdr predict ok for acks */
70int tcppreddat; /* XXX # times header prediction ok for data packets */
71int tcppcbcachemiss;
60dbdfb8 72#ifdef TCPDEBUG
15637ed4 73struct tcpiphdr tcp_saveti;
60dbdfb8 74#endif
15637ed4
RG
75struct inpcb *tcp_last_inpcb = &tcb;
76
15637ed4
RG
77
78/*
79 * Insert segment ti into reassembly queue of tcp with
80 * control block tp. Return TH_FIN if reassembly now includes
81 * a segment with FIN. The macro form does the common case inline
82 * (segment is the next to be received on an established connection,
83 * and the queue is empty), avoiding linkage into and removal
84 * from the queue and repetition of various conversions.
85 * Set DELACK for segments received in order, but ack immediately
86 * when segments are out of order (so fast retransmit can work).
87 */
88#define TCP_REASS(tp, ti, m, so, flags) { \
89 if ((ti)->ti_seq == (tp)->rcv_nxt && \
90 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
91 (tp)->t_state == TCPS_ESTABLISHED) { \
92 tp->t_flags |= TF_DELACK; \
93 (tp)->rcv_nxt += (ti)->ti_len; \
94 flags = (ti)->ti_flags & TH_FIN; \
95 tcpstat.tcps_rcvpack++;\
96 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
97 sbappend(&(so)->so_rcv, (m)); \
98 sorwakeup(so); \
99 } else { \
100 (flags) = tcp_reass((tp), (ti), (m)); \
101 tp->t_flags |= TF_ACKNOW; \
102 } \
103}
104
4c45483e 105int
15637ed4
RG
106tcp_reass(tp, ti, m)
107 register struct tcpcb *tp;
108 register struct tcpiphdr *ti;
109 struct mbuf *m;
110{
111 register struct tcpiphdr *q;
112 struct socket *so = tp->t_inpcb->inp_socket;
113 int flags;
114
115 /*
116 * Call with ti==0 after become established to
117 * force pre-ESTABLISHED data up to user socket.
118 */
119 if (ti == 0)
120 goto present;
121
122 /*
123 * Find a segment which begins after this one does.
124 */
125 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
126 q = (struct tcpiphdr *)q->ti_next)
127 if (SEQ_GT(q->ti_seq, ti->ti_seq))
128 break;
129
130 /*
131 * If there is a preceding segment, it may provide some of
132 * our data already. If so, drop the data from the incoming
133 * segment. If it provides all of our data, drop us.
134 */
135 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
136 register int i;
137 q = (struct tcpiphdr *)q->ti_prev;
138 /* conversion to int (in i) handles seq wraparound */
139 i = q->ti_seq + q->ti_len - ti->ti_seq;
140 if (i > 0) {
141 if (i >= ti->ti_len) {
142 tcpstat.tcps_rcvduppack++;
143 tcpstat.tcps_rcvdupbyte += ti->ti_len;
144 m_freem(m);
145 return (0);
146 }
147 m_adj(m, i);
148 ti->ti_len -= i;
149 ti->ti_seq += i;
150 }
151 q = (struct tcpiphdr *)(q->ti_next);
152 }
153 tcpstat.tcps_rcvoopack++;
154 tcpstat.tcps_rcvoobyte += ti->ti_len;
155 REASS_MBUF(ti) = m; /* XXX */
156
157 /*
158 * While we overlap succeeding segments trim them or,
159 * if they are completely covered, dequeue them.
160 */
161 while (q != (struct tcpiphdr *)tp) {
162 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
163 if (i <= 0)
164 break;
165 if (i < q->ti_len) {
166 q->ti_seq += i;
167 q->ti_len -= i;
168 m_adj(REASS_MBUF(q), i);
169 break;
170 }
171 q = (struct tcpiphdr *)q->ti_next;
172 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
173 remque(q->ti_prev);
174 m_freem(m);
175 }
176
177 /*
178 * Stick new segment in its place.
179 */
180 insque(ti, q->ti_prev);
181
182present:
183 /*
184 * Present data to user, advancing rcv_nxt through
185 * completed sequence space.
186 */
187 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
188 return (0);
189 ti = tp->seg_next;
190 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
191 return (0);
192 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
193 return (0);
194 do {
195 tp->rcv_nxt += ti->ti_len;
196 flags = ti->ti_flags & TH_FIN;
197 remque(ti);
198 m = REASS_MBUF(ti);
199 ti = (struct tcpiphdr *)ti->ti_next;
200 if (so->so_state & SS_CANTRCVMORE)
201 m_freem(m);
202 else
203 sbappend(&so->so_rcv, m);
204 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
205 sorwakeup(so);
206 return (flags);
207}
208
209/*
210 * TCP input routine, follows pages 65-76 of the
211 * protocol specification dated September, 1981 very closely.
212 */
4c45483e 213void
15637ed4
RG
214tcp_input(m, iphlen)
215 register struct mbuf *m;
216 int iphlen;
217{
218 register struct tcpiphdr *ti;
219 register struct inpcb *inp;
220 struct mbuf *om = 0;
221 int len, tlen, off;
222 register struct tcpcb *tp = 0;
223 register int tiflags;
4c45483e 224 struct socket *so = 0;
15637ed4 225 int todrop, acked, ourfinisacked, needoutput = 0;
15637ed4
RG
226 struct in_addr laddr;
227 int dropsocket = 0;
228 int iss = 0;
60dbdfb8
DG
229#ifdef TCPDEBUG
230 short ostate = 0;
231#endif
15637ed4
RG
232
233 tcpstat.tcps_rcvtotal++;
234 /*
235 * Get IP and TCP header together in first mbuf.
236 * Note: IP leaves IP header in first mbuf.
237 */
238 ti = mtod(m, struct tcpiphdr *);
239 if (iphlen > sizeof (struct ip))
240 ip_stripoptions(m, (struct mbuf *)0);
241 if (m->m_len < sizeof (struct tcpiphdr)) {
242 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
243 tcpstat.tcps_rcvshort++;
244 return;
245 }
246 ti = mtod(m, struct tcpiphdr *);
247 }
248
249 /*
250 * Checksum extended TCP header and data.
251 */
252 tlen = ((struct ip *)ti)->ip_len;
253 len = sizeof (struct ip) + tlen;
254 ti->ti_next = ti->ti_prev = 0;
255 ti->ti_x1 = 0;
256 ti->ti_len = (u_short)tlen;
257 HTONS(ti->ti_len);
258 if (ti->ti_sum = in_cksum(m, len)) {
259 tcpstat.tcps_rcvbadsum++;
260 goto drop;
261 }
262
263 /*
264 * Check that TCP offset makes sense,
265 * pull out TCP options and adjust length. XXX
266 */
267 off = ti->ti_off << 2;
268 if (off < sizeof (struct tcphdr) || off > tlen) {
269 tcpstat.tcps_rcvbadoff++;
270 goto drop;
271 }
272 tlen -= off;
273 ti->ti_len = tlen;
274 if (off > sizeof (struct tcphdr)) {
275 if (m->m_len < sizeof(struct ip) + off) {
276 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
277 tcpstat.tcps_rcvshort++;
278 return;
279 }
280 ti = mtod(m, struct tcpiphdr *);
281 }
282 om = m_get(M_DONTWAIT, MT_DATA);
283 if (om == 0)
284 goto drop;
285 om->m_len = off - sizeof (struct tcphdr);
286 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
287 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
288 m->m_len -= om->m_len;
289 m->m_pkthdr.len -= om->m_len;
290 bcopy(op+om->m_len, op,
291 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
292 }
293 }
294 tiflags = ti->ti_flags;
295
296 /*
297 * Convert TCP protocol specific fields to host format.
298 */
299 NTOHL(ti->ti_seq);
300 NTOHL(ti->ti_ack);
301 NTOHS(ti->ti_win);
302 NTOHS(ti->ti_urp);
303
304 /*
305 * Locate pcb for segment.
306 */
307findpcb:
308 inp = tcp_last_inpcb;
309 if (inp->inp_lport != ti->ti_dport ||
310 inp->inp_fport != ti->ti_sport ||
311 inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
312 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
313 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
314 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
315 if (inp)
316 tcp_last_inpcb = inp;
317 ++tcppcbcachemiss;
318 }
319
320 /*
321 * If the state is CLOSED (i.e., TCB does not exist) then
322 * all data in the incoming segment is discarded.
323 * If the TCB exists but is in CLOSED state, it is embryonic,
324 * but should either do a listen or a connect soon.
325 */
326 if (inp == 0)
327 goto dropwithreset;
328 tp = intotcpcb(inp);
329 if (tp == 0)
330 goto dropwithreset;
331 if (tp->t_state == TCPS_CLOSED)
332 goto drop;
333 so = inp->inp_socket;
334 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
60dbdfb8 335#ifdef TCPDEBUG
15637ed4
RG
336 if (so->so_options & SO_DEBUG) {
337 ostate = tp->t_state;
338 tcp_saveti = *ti;
339 }
60dbdfb8 340#endif
15637ed4
RG
341 if (so->so_options & SO_ACCEPTCONN) {
342 so = sonewconn(so, 0);
343 if (so == 0)
344 goto drop;
345 /*
346 * This is ugly, but ....
347 *
348 * Mark socket as temporary until we're
349 * committed to keeping it. The code at
350 * ``drop'' and ``dropwithreset'' check the
351 * flag dropsocket to see if the temporary
352 * socket created here should be discarded.
353 * We mark the socket as discardable until
354 * we're committed to it below in TCPS_LISTEN.
355 */
356 dropsocket++;
357 inp = (struct inpcb *)so->so_pcb;
358 inp->inp_laddr = ti->ti_dst;
359 inp->inp_lport = ti->ti_dport;
360#if BSD>=43
361 inp->inp_options = ip_srcroute();
362#endif
363 tp = intotcpcb(inp);
364 tp->t_state = TCPS_LISTEN;
365 }
366 }
367
368 /*
369 * Segment received on connection.
370 * Reset idle time and keep-alive timer.
371 */
372 tp->t_idle = 0;
373 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
374
375 /*
376 * Process options if not in LISTEN state,
377 * else do it below (after getting remote address).
378 */
379 if (om && tp->t_state != TCPS_LISTEN) {
380 tcp_dooptions(tp, om, ti);
381 om = 0;
382 }
383 /*
384 * Header prediction: check for the two common cases
385 * of a uni-directional data xfer. If the packet has
386 * no control flags, is in-sequence, the window didn't
387 * change and we're not retransmitting, it's a
388 * candidate. If the length is zero and the ack moved
389 * forward, we're the sender side of the xfer. Just
390 * free the data acked & wake any higher level process
391 * that was blocked waiting for space. If the length
392 * is non-zero and the ack didn't move, we're the
393 * receiver side. If we're getting packets in-order
394 * (the reassembly queue is empty), add the data to
395 * the socket buffer and note that we need a delayed ack.
396 */
397 if (tp->t_state == TCPS_ESTABLISHED &&
398 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
399 ti->ti_seq == tp->rcv_nxt &&
400 ti->ti_win && ti->ti_win == tp->snd_wnd &&
401 tp->snd_nxt == tp->snd_max) {
402 if (ti->ti_len == 0) {
403 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
404 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
405 tp->snd_cwnd >= tp->snd_wnd) {
406 /*
407 * this is a pure ack for outstanding data.
408 */
409 ++tcppredack;
410 if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq))
411 tcp_xmit_timer(tp);
412 acked = ti->ti_ack - tp->snd_una;
413 tcpstat.tcps_rcvackpack++;
414 tcpstat.tcps_rcvackbyte += acked;
415 sbdrop(&so->so_snd, acked);
416 tp->snd_una = ti->ti_ack;
417 m_freem(m);
418
419 /*
420 * If all outstanding data are acked, stop
421 * retransmit timer, otherwise restart timer
422 * using current (possibly backed-off) value.
423 * If process is waiting for space,
424 * wakeup/selwakeup/signal. If data
425 * are ready to send, let tcp_output
426 * decide between more output or persist.
427 */
428 if (tp->snd_una == tp->snd_max)
429 tp->t_timer[TCPT_REXMT] = 0;
430 else if (tp->t_timer[TCPT_PERSIST] == 0)
431 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
432
433 if (so->so_snd.sb_flags & SB_NOTIFY)
434 sowwakeup(so);
435 if (so->so_snd.sb_cc)
436 (void) tcp_output(tp);
437 return;
438 }
439 } else if (ti->ti_ack == tp->snd_una &&
440 tp->seg_next == (struct tcpiphdr *)tp &&
441 ti->ti_len <= sbspace(&so->so_rcv)) {
442 /*
443 * this is a pure, in-sequence data packet
444 * with nothing on the reassembly queue and
445 * we have enough buffer space to take it.
446 */
447 ++tcppreddat;
448 tp->rcv_nxt += ti->ti_len;
449 tcpstat.tcps_rcvpack++;
450 tcpstat.tcps_rcvbyte += ti->ti_len;
451 /*
452 * Drop TCP and IP headers then add data
453 * to socket buffer
454 */
455 m->m_data += sizeof(struct tcpiphdr);
456 m->m_len -= sizeof(struct tcpiphdr);
457 sbappend(&so->so_rcv, m);
458 sorwakeup(so);
459 tp->t_flags |= TF_DELACK;
460 return;
461 }
462 }
463
464 /*
465 * Drop TCP and IP headers; TCP options were dropped above.
466 */
467 m->m_data += sizeof(struct tcpiphdr);
468 m->m_len -= sizeof(struct tcpiphdr);
469
470 /*
471 * Calculate amount of space in receive window,
472 * and then do TCP input processing.
473 * Receive window is amount of space in rcv queue,
474 * but not less than advertised window.
475 */
476 { int win;
477
478 win = sbspace(&so->so_rcv);
479 if (win < 0)
480 win = 0;
481 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
482 }
483
484 switch (tp->t_state) {
485
486 /*
487 * If the state is LISTEN then ignore segment if it contains an RST.
488 * If the segment contains an ACK then it is bad and send a RST.
489 * If it does not contain a SYN then it is not interesting; drop it.
490 * Don't bother responding if the destination was a broadcast.
491 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
492 * tp->iss, and send a segment:
493 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
494 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
495 * Fill in remote peer address fields if not previously specified.
496 * Enter SYN_RECEIVED state, and process any other fields of this
497 * segment in this state.
498 */
499 case TCPS_LISTEN: {
500 struct mbuf *am;
501 register struct sockaddr_in *sin;
502
503 if (tiflags & TH_RST)
504 goto drop;
505 if (tiflags & TH_ACK)
506 goto dropwithreset;
507 if ((tiflags & TH_SYN) == 0)
508 goto drop;
509 if (m->m_flags & M_BCAST)
510 goto drop;
511 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
512 if (am == NULL)
513 goto drop;
514 am->m_len = sizeof (struct sockaddr_in);
515 sin = mtod(am, struct sockaddr_in *);
516 sin->sin_family = AF_INET;
517 sin->sin_len = sizeof(*sin);
518 sin->sin_addr = ti->ti_src;
519 sin->sin_port = ti->ti_sport;
520 laddr = inp->inp_laddr;
521 if (inp->inp_laddr.s_addr == INADDR_ANY)
522 inp->inp_laddr = ti->ti_dst;
523 if (in_pcbconnect(inp, am)) {
524 inp->inp_laddr = laddr;
525 (void) m_free(am);
526 goto drop;
527 }
528 (void) m_free(am);
529 tp->t_template = tcp_template(tp);
530 if (tp->t_template == 0) {
531 tp = tcp_drop(tp, ENOBUFS);
532 dropsocket = 0; /* socket is already gone */
533 goto drop;
534 }
535 if (om) {
536 tcp_dooptions(tp, om, ti);
537 om = 0;
538 }
539 if (iss)
540 tp->iss = iss;
541 else
542 tp->iss = tcp_iss;
543 tcp_iss += TCP_ISSINCR/2;
544 tp->irs = ti->ti_seq;
545 tcp_sendseqinit(tp);
546 tcp_rcvseqinit(tp);
547 tp->t_flags |= TF_ACKNOW;
548 tp->t_state = TCPS_SYN_RECEIVED;
549 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
550 dropsocket = 0; /* committed to socket */
551 tcpstat.tcps_accepts++;
552 goto trimthenstep6;
553 }
554
555 /*
556 * If the state is SYN_SENT:
557 * if seg contains an ACK, but not for our SYN, drop the input.
558 * if seg contains a RST, then drop the connection.
559 * if seg does not contain SYN, then drop it.
560 * Otherwise this is an acceptable SYN segment
561 * initialize tp->rcv_nxt and tp->irs
562 * if seg contains ack then advance tp->snd_una
563 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
564 * arrange for segment to be acked (eventually)
565 * continue processing rest of data/controls, beginning with URG
566 */
567 case TCPS_SYN_SENT:
568 if ((tiflags & TH_ACK) &&
569 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
570 SEQ_GT(ti->ti_ack, tp->snd_max)))
571 goto dropwithreset;
572 if (tiflags & TH_RST) {
573 if (tiflags & TH_ACK)
574 tp = tcp_drop(tp, ECONNREFUSED);
575 goto drop;
576 }
577 if ((tiflags & TH_SYN) == 0)
578 goto drop;
579 if (tiflags & TH_ACK) {
580 tp->snd_una = ti->ti_ack;
581 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
582 tp->snd_nxt = tp->snd_una;
583 }
584 tp->t_timer[TCPT_REXMT] = 0;
585 tp->irs = ti->ti_seq;
586 tcp_rcvseqinit(tp);
587 tp->t_flags |= TF_ACKNOW;
588 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
589 tcpstat.tcps_connects++;
590 soisconnected(so);
591 tp->t_state = TCPS_ESTABLISHED;
592 (void) tcp_reass(tp, (struct tcpiphdr *)0,
593 (struct mbuf *)0);
594 /*
595 * if we didn't have to retransmit the SYN,
596 * use its rtt as our initial srtt & rtt var.
597 */
598 if (tp->t_rtt)
599 tcp_xmit_timer(tp);
600 } else
601 tp->t_state = TCPS_SYN_RECEIVED;
602
603trimthenstep6:
604 /*
605 * Advance ti->ti_seq to correspond to first data byte.
606 * If data, trim to stay within window,
607 * dropping FIN if necessary.
608 */
609 ti->ti_seq++;
4c45483e 610 if ((u_long)ti->ti_len > (u_long)tp->rcv_wnd) {
15637ed4
RG
611 todrop = ti->ti_len - tp->rcv_wnd;
612 m_adj(m, -todrop);
613 ti->ti_len = tp->rcv_wnd;
614 tiflags &= ~TH_FIN;
615 tcpstat.tcps_rcvpackafterwin++;
616 tcpstat.tcps_rcvbyteafterwin += todrop;
617 }
618 tp->snd_wl1 = ti->ti_seq - 1;
619 tp->rcv_up = ti->ti_seq;
620 goto step6;
621 }
622
623 /*
624 * States other than LISTEN or SYN_SENT.
625 * First check that at least some bytes of segment are within
626 * receive window. If segment begins before rcv_nxt,
627 * drop leading data (and SYN); if nothing left, just ack.
628 */
629 todrop = tp->rcv_nxt - ti->ti_seq;
630 if (todrop > 0) {
631 if (tiflags & TH_SYN) {
632 tiflags &= ~TH_SYN;
633 ti->ti_seq++;
634 if (ti->ti_urp > 1)
635 ti->ti_urp--;
636 else
637 tiflags &= ~TH_URG;
638 todrop--;
639 }
640 if (todrop > ti->ti_len ||
641 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
642 tcpstat.tcps_rcvduppack++;
643 tcpstat.tcps_rcvdupbyte += ti->ti_len;
644 /*
645 * If segment is just one to the left of the window,
646 * check two special cases:
647 * 1. Don't toss RST in response to 4.2-style keepalive.
648 * 2. If the only thing to drop is a FIN, we can drop
649 * it, but check the ACK or we will get into FIN
650 * wars if our FINs crossed (both CLOSING).
651 * In either case, send ACK to resynchronize,
652 * but keep on processing for RST or ACK.
653 */
654 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
655#ifdef TCP_COMPAT_42
656 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
657#endif
658 ) {
659 todrop = ti->ti_len;
660 tiflags &= ~TH_FIN;
661 tp->t_flags |= TF_ACKNOW;
662 } else
663 goto dropafterack;
664 } else {
665 tcpstat.tcps_rcvpartduppack++;
666 tcpstat.tcps_rcvpartdupbyte += todrop;
667 }
668 m_adj(m, todrop);
669 ti->ti_seq += todrop;
670 ti->ti_len -= todrop;
671 if (ti->ti_urp > todrop)
672 ti->ti_urp -= todrop;
673 else {
674 tiflags &= ~TH_URG;
675 ti->ti_urp = 0;
676 }
677 }
678
679 /*
680 * If new data are received on a connection after the
681 * user processes are gone, then RST the other end.
682 */
683 if ((so->so_state & SS_NOFDREF) &&
684 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
685 tp = tcp_close(tp);
686 tcpstat.tcps_rcvafterclose++;
687 goto dropwithreset;
688 }
689
690 /*
691 * If segment ends after window, drop trailing data
692 * (and PUSH and FIN); if nothing left, just ACK.
693 */
694 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
695 if (todrop > 0) {
696 tcpstat.tcps_rcvpackafterwin++;
697 if (todrop >= ti->ti_len) {
698 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
699 /*
700 * If a new connection request is received
701 * while in TIME_WAIT, drop the old connection
702 * and start over if the sequence numbers
703 * are above the previous ones.
704 */
705 if (tiflags & TH_SYN &&
706 tp->t_state == TCPS_TIME_WAIT &&
707 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
708 iss = tp->rcv_nxt + TCP_ISSINCR;
709 tp = tcp_close(tp);
710 goto findpcb;
711 }
712 /*
713 * If window is closed can only take segments at
714 * window edge, and have to drop data and PUSH from
715 * incoming segments. Continue processing, but
716 * remember to ack. Otherwise, drop segment
717 * and ack.
718 */
719 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
720 tp->t_flags |= TF_ACKNOW;
721 tcpstat.tcps_rcvwinprobe++;
722 } else
723 goto dropafterack;
724 } else
725 tcpstat.tcps_rcvbyteafterwin += todrop;
726 m_adj(m, -todrop);
727 ti->ti_len -= todrop;
728 tiflags &= ~(TH_PUSH|TH_FIN);
729 }
730
731 /*
732 * If the RST bit is set examine the state:
733 * SYN_RECEIVED STATE:
734 * If passive open, return to LISTEN state.
735 * If active open, inform user that connection was refused.
736 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
737 * Inform user that connection was reset, and close tcb.
738 * CLOSING, LAST_ACK, TIME_WAIT STATES
739 * Close the tcb.
740 */
741 if (tiflags&TH_RST) switch (tp->t_state) {
742
743 case TCPS_SYN_RECEIVED:
744 so->so_error = ECONNREFUSED;
745 goto close;
746
747 case TCPS_ESTABLISHED:
748 case TCPS_FIN_WAIT_1:
749 case TCPS_FIN_WAIT_2:
750 case TCPS_CLOSE_WAIT:
751 so->so_error = ECONNRESET;
752 close:
753 tp->t_state = TCPS_CLOSED;
754 tcpstat.tcps_drops++;
755 tp = tcp_close(tp);
756 goto drop;
757
758 case TCPS_CLOSING:
759 case TCPS_LAST_ACK:
760 case TCPS_TIME_WAIT:
761 tp = tcp_close(tp);
762 goto drop;
763 }
764
765 /*
766 * If a SYN is in the window, then this is an
767 * error and we send an RST and drop the connection.
768 */
769 if (tiflags & TH_SYN) {
770 tp = tcp_drop(tp, ECONNRESET);
771 goto dropwithreset;
772 }
773
774 /*
775 * If the ACK bit is off we drop the segment and return.
776 */
777 if ((tiflags & TH_ACK) == 0)
778 goto drop;
779
780 /*
781 * Ack processing.
782 */
783 switch (tp->t_state) {
784
785 /*
786 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
787 * ESTABLISHED state and continue processing, otherwise
788 * send an RST.
789 */
790 case TCPS_SYN_RECEIVED:
791 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
792 SEQ_GT(ti->ti_ack, tp->snd_max))
793 goto dropwithreset;
794 tcpstat.tcps_connects++;
795 soisconnected(so);
796 tp->t_state = TCPS_ESTABLISHED;
797 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
798 tp->snd_wl1 = ti->ti_seq - 1;
799 /* fall into ... */
800
801 /*
802 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
803 * ACKs. If the ack is in the range
804 * tp->snd_una < ti->ti_ack <= tp->snd_max
805 * then advance tp->snd_una to ti->ti_ack and drop
806 * data from the retransmission queue. If this ACK reflects
807 * more up to date window information we update our window information.
808 */
809 case TCPS_ESTABLISHED:
810 case TCPS_FIN_WAIT_1:
811 case TCPS_FIN_WAIT_2:
812 case TCPS_CLOSE_WAIT:
813 case TCPS_CLOSING:
814 case TCPS_LAST_ACK:
815 case TCPS_TIME_WAIT:
816
817 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
818 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
819 tcpstat.tcps_rcvdupack++;
820 /*
821 * If we have outstanding data (other than
822 * a window probe), this is a completely
823 * duplicate ack (ie, window info didn't
824 * change), the ack is the biggest we've
825 * seen and we've seen exactly our rexmt
826 * threshhold of them, assume a packet
827 * has been dropped and retransmit it.
828 * Kludge snd_nxt & the congestion
829 * window so we send only this one
830 * packet.
831 *
832 * We know we're losing at the current
833 * window size so do congestion avoidance
834 * (set ssthresh to half the current window
835 * and pull our congestion window back to
836 * the new ssthresh).
837 *
838 * Dup acks mean that packets have left the
839 * network (they're now cached at the receiver)
840 * so bump cwnd by the amount in the receiver
841 * to keep a constant cwnd packets in the
842 * network.
843 */
844 if (tp->t_timer[TCPT_REXMT] == 0 ||
845 ti->ti_ack != tp->snd_una)
846 tp->t_dupacks = 0;
847 else if (++tp->t_dupacks == tcprexmtthresh) {
848 tcp_seq onxt = tp->snd_nxt;
849 u_int win =
850 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
851 tp->t_maxseg;
852
853 if (win < 2)
854 win = 2;
855 tp->snd_ssthresh = win * tp->t_maxseg;
856 tp->t_timer[TCPT_REXMT] = 0;
857 tp->t_rtt = 0;
858 tp->snd_nxt = ti->ti_ack;
859 tp->snd_cwnd = tp->t_maxseg;
860 (void) tcp_output(tp);
861 tp->snd_cwnd = tp->snd_ssthresh +
862 tp->t_maxseg * tp->t_dupacks;
863 if (SEQ_GT(onxt, tp->snd_nxt))
864 tp->snd_nxt = onxt;
865 goto drop;
866 } else if (tp->t_dupacks > tcprexmtthresh) {
867 tp->snd_cwnd += tp->t_maxseg;
868 (void) tcp_output(tp);
869 goto drop;
870 }
871 } else
872 tp->t_dupacks = 0;
873 break;
874 }
875 /*
876 * If the congestion window was inflated to account
877 * for the other side's cached packets, retract it.
878 */
879 if (tp->t_dupacks > tcprexmtthresh &&
880 tp->snd_cwnd > tp->snd_ssthresh)
881 tp->snd_cwnd = tp->snd_ssthresh;
882 tp->t_dupacks = 0;
883 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
884 tcpstat.tcps_rcvacktoomuch++;
885 goto dropafterack;
886 }
887 acked = ti->ti_ack - tp->snd_una;
888 tcpstat.tcps_rcvackpack++;
889 tcpstat.tcps_rcvackbyte += acked;
890
891 /*
892 * If transmit timer is running and timed sequence
893 * number was acked, update smoothed round trip time.
894 * Since we now have an rtt measurement, cancel the
895 * timer backoff (cf., Phil Karn's retransmit alg.).
896 * Recompute the initial retransmit timer.
897 */
898 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
899 tcp_xmit_timer(tp);
900
901 /*
902 * If all outstanding data is acked, stop retransmit
903 * timer and remember to restart (more output or persist).
904 * If there is more data to be acked, restart retransmit
905 * timer, using current (possibly backed-off) value.
906 */
907 if (ti->ti_ack == tp->snd_max) {
908 tp->t_timer[TCPT_REXMT] = 0;
909 needoutput = 1;
910 } else if (tp->t_timer[TCPT_PERSIST] == 0)
911 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
912 /*
913 * When new data is acked, open the congestion window.
914 * If the window gives us less than ssthresh packets
915 * in flight, open exponentially (maxseg per packet).
916 * Otherwise open linearly: maxseg per window
917 * (maxseg^2 / cwnd per packet), plus a constant
918 * fraction of a packet (maxseg/8) to help larger windows
919 * open quickly enough.
920 */
921 {
922 register u_int cw = tp->snd_cwnd;
923 register u_int incr = tp->t_maxseg;
924
925 if (cw > tp->snd_ssthresh)
926 incr = incr * incr / cw + incr / 8;
927 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
928 }
929 if (acked > so->so_snd.sb_cc) {
930 tp->snd_wnd -= so->so_snd.sb_cc;
931 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
932 ourfinisacked = 1;
933 } else {
934 sbdrop(&so->so_snd, acked);
935 tp->snd_wnd -= acked;
936 ourfinisacked = 0;
937 }
938 if (so->so_snd.sb_flags & SB_NOTIFY)
939 sowwakeup(so);
940 tp->snd_una = ti->ti_ack;
941 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
942 tp->snd_nxt = tp->snd_una;
943
944 switch (tp->t_state) {
945
946 /*
947 * In FIN_WAIT_1 STATE in addition to the processing
948 * for the ESTABLISHED state if our FIN is now acknowledged
949 * then enter FIN_WAIT_2.
950 */
951 case TCPS_FIN_WAIT_1:
952 if (ourfinisacked) {
953 /*
954 * If we can't receive any more
955 * data, then closing user can proceed.
956 * Starting the timer is contrary to the
957 * specification, but if we don't get a FIN
958 * we'll hang forever.
959 */
960 if (so->so_state & SS_CANTRCVMORE) {
961 soisdisconnected(so);
962 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
963 }
964 tp->t_state = TCPS_FIN_WAIT_2;
965 }
966 break;
967
968 /*
969 * In CLOSING STATE in addition to the processing for
970 * the ESTABLISHED state if the ACK acknowledges our FIN
971 * then enter the TIME-WAIT state, otherwise ignore
972 * the segment.
973 */
974 case TCPS_CLOSING:
975 if (ourfinisacked) {
976 tp->t_state = TCPS_TIME_WAIT;
977 tcp_canceltimers(tp);
978 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
979 soisdisconnected(so);
980 }
981 break;
982
983 /*
984 * In LAST_ACK, we may still be waiting for data to drain
985 * and/or to be acked, as well as for the ack of our FIN.
986 * If our FIN is now acknowledged, delete the TCB,
987 * enter the closed state and return.
988 */
989 case TCPS_LAST_ACK:
990 if (ourfinisacked) {
991 tp = tcp_close(tp);
992 goto drop;
993 }
994 break;
995
996 /*
997 * In TIME_WAIT state the only thing that should arrive
998 * is a retransmission of the remote FIN. Acknowledge
999 * it and restart the finack timer.
1000 */
1001 case TCPS_TIME_WAIT:
1002 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1003 goto dropafterack;
1004 }
1005 }
1006
1007step6:
1008 /*
1009 * Update window information.
1010 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1011 */
1012 if ((tiflags & TH_ACK) &&
1013 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
1014 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1015 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
1016 /* keep track of pure window updates */
1017 if (ti->ti_len == 0 &&
1018 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
1019 tcpstat.tcps_rcvwinupd++;
1020 tp->snd_wnd = ti->ti_win;
1021 tp->snd_wl1 = ti->ti_seq;
1022 tp->snd_wl2 = ti->ti_ack;
1023 if (tp->snd_wnd > tp->max_sndwnd)
1024 tp->max_sndwnd = tp->snd_wnd;
1025 needoutput = 1;
1026 }
1027
1028 /*
1029 * Process segments with URG.
1030 */
1031 if ((tiflags & TH_URG) && ti->ti_urp &&
1032 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1033 /*
1034 * This is a kludge, but if we receive and accept
1035 * random urgent pointers, we'll crash in
1036 * soreceive. It's hard to imagine someone
1037 * actually wanting to send this much urgent data.
1038 */
1039 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
1040 ti->ti_urp = 0; /* XXX */
1041 tiflags &= ~TH_URG; /* XXX */
1042 goto dodata; /* XXX */
1043 }
1044 /*
1045 * If this segment advances the known urgent pointer,
1046 * then mark the data stream. This should not happen
1047 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1048 * a FIN has been received from the remote side.
1049 * In these states we ignore the URG.
1050 *
1051 * According to RFC961 (Assigned Protocols),
1052 * the urgent pointer points to the last octet
1053 * of urgent data. We continue, however,
1054 * to consider it to indicate the first octet
1055 * of data past the urgent section as the original
1056 * spec states (in one of two places).
1057 */
1058 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1059 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1060 so->so_oobmark = so->so_rcv.sb_cc +
1061 (tp->rcv_up - tp->rcv_nxt) - 1;
1062 if (so->so_oobmark == 0)
1063 so->so_state |= SS_RCVATMARK;
1064 sohasoutofband(so);
1065 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1066 }
1067 /*
1068 * Remove out of band data so doesn't get presented to user.
1069 * This can happen independent of advancing the URG pointer,
1070 * but if two URG's are pending at once, some out-of-band
1071 * data may creep in... ick.
1072 */
4c45483e 1073 if ((u_long)ti->ti_urp <= (u_long)ti->ti_len
15637ed4
RG
1074#ifdef SO_OOBINLINE
1075 && (so->so_options & SO_OOBINLINE) == 0
1076#endif
1077 )
1078 tcp_pulloutofband(so, ti, m);
1079 } else
1080 /*
1081 * If no out of band data is expected,
1082 * pull receive urgent pointer along
1083 * with the receive window.
1084 */
1085 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1086 tp->rcv_up = tp->rcv_nxt;
1087dodata: /* XXX */
1088
1089 /*
1090 * Process the segment text, merging it into the TCP sequencing queue,
1091 * and arranging for acknowledgment of receipt if necessary.
1092 * This process logically involves adjusting tp->rcv_wnd as data
1093 * is presented to the user (this happens in tcp_usrreq.c,
1094 * case PRU_RCVD). If a FIN has already been received on this
1095 * connection then we just ignore the text.
1096 */
1097 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1098 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1099 TCP_REASS(tp, ti, m, so, tiflags);
1100 /*
1101 * Note the amount of data that peer has sent into
1102 * our window, in order to estimate the sender's
1103 * buffer size.
1104 */
1105 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1106 } else {
1107 m_freem(m);
1108 tiflags &= ~TH_FIN;
1109 }
1110
1111 /*
1112 * If FIN is received ACK the FIN and let the user know
1113 * that the connection is closing.
1114 */
1115 if (tiflags & TH_FIN) {
1116 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1117 socantrcvmore(so);
1118 tp->t_flags |= TF_ACKNOW;
1119 tp->rcv_nxt++;
1120 }
1121 switch (tp->t_state) {
1122
1123 /*
1124 * In SYN_RECEIVED and ESTABLISHED STATES
1125 * enter the CLOSE_WAIT state.
1126 */
1127 case TCPS_SYN_RECEIVED:
1128 case TCPS_ESTABLISHED:
1129 tp->t_state = TCPS_CLOSE_WAIT;
1130 break;
1131
1132 /*
1133 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1134 * enter the CLOSING state.
1135 */
1136 case TCPS_FIN_WAIT_1:
1137 tp->t_state = TCPS_CLOSING;
1138 break;
1139
1140 /*
1141 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1142 * starting the time-wait timer, turning off the other
1143 * standard timers.
1144 */
1145 case TCPS_FIN_WAIT_2:
1146 tp->t_state = TCPS_TIME_WAIT;
1147 tcp_canceltimers(tp);
1148 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1149 soisdisconnected(so);
1150 break;
1151
1152 /*
1153 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1154 */
1155 case TCPS_TIME_WAIT:
1156 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1157 break;
1158 }
1159 }
ff1a3ad7 1160#ifdef TCPDEBUG
15637ed4
RG
1161 if (so->so_options & SO_DEBUG)
1162 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
ff1a3ad7 1163#endif
15637ed4
RG
1164
1165 /*
1166 * Return any desired output.
1167 */
1168 if (needoutput || (tp->t_flags & TF_ACKNOW))
1169 (void) tcp_output(tp);
1170 return;
1171
1172dropafterack:
1173 /*
1174 * Generate an ACK dropping incoming segment if it occupies
1175 * sequence space, where the ACK reflects our state.
1176 */
1177 if (tiflags & TH_RST)
1178 goto drop;
1179 m_freem(m);
1180 tp->t_flags |= TF_ACKNOW;
1181 (void) tcp_output(tp);
1182 return;
1183
1184dropwithreset:
1185 if (om) {
1186 (void) m_free(om);
1187 om = 0;
1188 }
1189 /*
1190 * Generate a RST, dropping incoming segment.
1191 * Make ACK acceptable to originator of segment.
1192 * Don't bother to respond if destination was broadcast.
1193 */
1194 if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
1195 goto drop;
1196 if (tiflags & TH_ACK)
1197 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1198 else {
1199 if (tiflags & TH_SYN)
1200 ti->ti_len++;
1201 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1202 TH_RST|TH_ACK);
1203 }
1204 /* destroy temporarily created socket */
1205 if (dropsocket)
1206 (void) soabort(so);
1207 return;
1208
1209drop:
1210 if (om)
1211 (void) m_free(om);
1212 /*
1213 * Drop space held by incoming segment and return.
1214 */
ff1a3ad7 1215#ifdef TCPDEBUG
15637ed4
RG
1216 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1217 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
ff1a3ad7 1218#endif
15637ed4
RG
1219 m_freem(m);
1220 /* destroy temporarily created socket */
1221 if (dropsocket)
1222 (void) soabort(so);
1223 return;
1224}
1225
4c45483e 1226static void
15637ed4
RG
1227tcp_dooptions(tp, om, ti)
1228 struct tcpcb *tp;
1229 struct mbuf *om;
1230 struct tcpiphdr *ti;
1231{
1232 register u_char *cp;
1233 u_short mss;
1234 int opt, optlen, cnt;
1235
1236 cp = mtod(om, u_char *);
1237 cnt = om->m_len;
1238 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1239 opt = cp[0];
1240 if (opt == TCPOPT_EOL)
1241 break;
1242 if (opt == TCPOPT_NOP)
1243 optlen = 1;
1244 else {
1245 optlen = cp[1];
1246 if (optlen <= 0)
1247 break;
1248 }
1249 switch (opt) {
1250
1251 default:
1252 continue;
1253
1254 case TCPOPT_MAXSEG:
1255 if (optlen != 4)
1256 continue;
1257 if (!(ti->ti_flags & TH_SYN))
1258 continue;
1259 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1260 NTOHS(mss);
1261 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1262 break;
1263 }
1264 }
1265 (void) m_free(om);
1266}
1267
1268/*
1269 * Pull out of band byte out of a segment so
1270 * it doesn't appear in the user's data queue.
1271 * It is still reflected in the segment length for
1272 * sequencing purposes.
1273 */
4c45483e 1274static void
15637ed4
RG
1275tcp_pulloutofband(so, ti, m)
1276 struct socket *so;
1277 struct tcpiphdr *ti;
1278 register struct mbuf *m;
1279{
1280 int cnt = ti->ti_urp - 1;
1281
1282 while (cnt >= 0) {
1283 if (m->m_len > cnt) {
1284 char *cp = mtod(m, caddr_t) + cnt;
1285 struct tcpcb *tp = sototcpcb(so);
1286
1287 tp->t_iobc = *cp;
1288 tp->t_oobflags |= TCPOOB_HAVEDATA;
1289 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1290 m->m_len--;
1291 return;
1292 }
1293 cnt -= m->m_len;
1294 m = m->m_next;
1295 if (m == 0)
1296 break;
1297 }
1298 panic("tcp_pulloutofband");
1299}
1300
1301/*
1302 * Collect new round-trip time estimate
1303 * and update averages and current timeout.
1304 */
4c45483e 1305static void
15637ed4
RG
1306tcp_xmit_timer(tp)
1307 register struct tcpcb *tp;
1308{
1309 register short delta;
1310
1311 tcpstat.tcps_rttupdated++;
1312 if (tp->t_srtt != 0) {
1313 /*
1314 * srtt is stored as fixed point with 3 bits after the
1315 * binary point (i.e., scaled by 8). The following magic
1316 * is equivalent to the smoothing algorithm in rfc793 with
1317 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1318 * point). Adjust t_rtt to origin 0.
1319 */
1320 delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1321 if ((tp->t_srtt += delta) <= 0)
1322 tp->t_srtt = 1;
1323 /*
1324 * We accumulate a smoothed rtt variance (actually, a
1325 * smoothed mean difference), then set the retransmit
1326 * timer to smoothed rtt + 4 times the smoothed variance.
1327 * rttvar is stored as fixed point with 2 bits after the
1328 * binary point (scaled by 4). The following is
1329 * equivalent to rfc793 smoothing with an alpha of .75
1330 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1331 * rfc793's wired-in beta.
1332 */
1333 if (delta < 0)
1334 delta = -delta;
1335 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1336 if ((tp->t_rttvar += delta) <= 0)
1337 tp->t_rttvar = 1;
1338 } else {
1339 /*
1340 * No rtt measurement yet - use the unsmoothed rtt.
1341 * Set the variance to half the rtt (so our first
1342 * retransmit happens at 2*rtt)
1343 */
1344 tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT;
1345 tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1);
1346 }
1347 tp->t_rtt = 0;
1348 tp->t_rxtshift = 0;
1349
1350 /*
1351 * the retransmit should happen at rtt + 4 * rttvar.
1352 * Because of the way we do the smoothing, srtt and rttvar
1353 * will each average +1/2 tick of bias. When we compute
1354 * the retransmit timer, we want 1/2 tick of rounding and
1355 * 1 extra tick because of +-1/2 tick uncertainty in the
1356 * firing of the timer. The bias will give us exactly the
1357 * 1.5 tick we need. But, because the bias is
1358 * statistical, we have to test that we don't drop below
1359 * the minimum feasible timer (which is 2 ticks).
1360 */
1361 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1362 tp->t_rttmin, TCPTV_REXMTMAX);
1363
1364 /*
1365 * We received an ack for a packet that wasn't retransmitted;
1366 * it is probably safe to discard any error indications we've
1367 * received recently. This isn't quite right, but close enough
1368 * for now (a route might have failed after we sent a segment,
1369 * and the return path might not be symmetrical).
1370 */
1371 tp->t_softerror = 0;
1372}
1373
1374/*
1375 * Determine a reasonable value for maxseg size.
1376 * If the route is known, check route for mtu.
1377 * If none, use an mss that can be handled on the outgoing
1378 * interface without forcing IP to fragment; if bigger than
1379 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1380 * to utilize large mbufs. If no route is found, route has no mtu,
1381 * or the destination isn't local, use a default, hopefully conservative
1382 * size (usually 512 or the default IP max size, but no more than the mtu
1383 * of the interface), as we can't discover anything about intervening
1384 * gateways or networks. We also initialize the congestion/slow start
1385 * window to be a single segment if the destination isn't local.
1386 * While looking at the routing entry, we also initialize other path-dependent
1387 * parameters from pre-set or cached values in the routing entry.
1388 */
4c45483e 1389int
15637ed4
RG
1390tcp_mss(tp, offer)
1391 register struct tcpcb *tp;
1392 u_short offer;
1393{
1394 struct route *ro;
1395 register struct rtentry *rt;
1396 struct ifnet *ifp;
1397 register int rtt, mss;
1398 u_long bufsize;
1399 struct inpcb *inp;
1400 struct socket *so;
1401 extern int tcp_mssdflt, tcp_rttdflt;
1402
1403 inp = tp->t_inpcb;
1404 ro = &inp->inp_route;
1405
1406 if ((rt = ro->ro_rt) == (struct rtentry *)0) {
1407 /* No route yet, so try to acquire one */
1408 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1409 ro->ro_dst.sa_family = AF_INET;
1410 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1411 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1412 inp->inp_faddr;
1413 rtalloc(ro);
1414 }
1415 if ((rt = ro->ro_rt) == (struct rtentry *)0)
1416 return (tcp_mssdflt);
1417 }
1418 ifp = rt->rt_ifp;
1419 so = inp->inp_socket;
1420
1421#ifdef RTV_MTU /* if route characteristics exist ... */
1422 /*
1423 * While we're here, check if there's an initial rtt
1424 * or rttvar. Convert from the route-table units
1425 * to scaled multiples of the slow timeout timer.
1426 */
1427 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1428 if (rt->rt_rmx.rmx_locks & RTV_MTU)
1429 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1430 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1431 if (rt->rt_rmx.rmx_rttvar)
1432 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1433 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1434 else
1435 /* default variation is +- 1 rtt */
1436 tp->t_rttvar =
1437 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1438 TCPT_RANGESET(tp->t_rxtcur,
1439 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1440 tp->t_rttmin, TCPTV_REXMTMAX);
1441 }
1442 /*
1443 * if there's an mtu associated with the route, use it
1444 */
1445 if (rt->rt_rmx.rmx_mtu)
1446 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1447 else
1448#endif /* RTV_MTU */
1449 {
1450 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1451#if (MCLBYTES & (MCLBYTES - 1)) == 0
1452 if (mss > MCLBYTES)
1453 mss &= ~(MCLBYTES-1);
1454#else
1455 if (mss > MCLBYTES)
1456 mss = mss / MCLBYTES * MCLBYTES;
1457#endif
1458 if (!in_localaddr(inp->inp_faddr))
1459 mss = min(mss, tcp_mssdflt);
1460 }
1461 /*
1462 * The current mss, t_maxseg, is initialized to the default value.
1463 * If we compute a smaller value, reduce the current mss.
1464 * If we compute a larger value, return it for use in sending
1465 * a max seg size option, but don't store it for use
1466 * unless we received an offer at least that large from peer.
1467 * However, do not accept offers under 32 bytes.
1468 */
1469 if (offer)
1470 mss = min(mss, offer);
1471 mss = max(mss, 32); /* sanity */
1472 if (mss < tp->t_maxseg || offer != 0) {
1473 /*
1474 * If there's a pipesize, change the socket buffer
1475 * to that size. Make the socket buffers an integral
1476 * number of mss units; if the mss is larger than
1477 * the socket buffer, decrease the mss.
1478 */
1479#ifdef RTV_SPIPE
1480 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
1481#endif
1482 bufsize = so->so_snd.sb_hiwat;
1483 if (bufsize < mss)
1484 mss = bufsize;
1485 else {
1486 bufsize = min(bufsize, SB_MAX) / mss * mss;
1487 (void) sbreserve(&so->so_snd, bufsize);
1488 }
1489 tp->t_maxseg = mss;
1490
1491#ifdef RTV_RPIPE
1492 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
1493#endif
1494 bufsize = so->so_rcv.sb_hiwat;
1495 if (bufsize > mss) {
1496 bufsize = min(bufsize, SB_MAX) / mss * mss;
1497 (void) sbreserve(&so->so_rcv, bufsize);
1498 }
1499 }
1500 tp->snd_cwnd = mss;
1501
1502#ifdef RTV_SSTHRESH
1503 if (rt->rt_rmx.rmx_ssthresh) {
1504 /*
1505 * There's some sort of gateway or interface
1506 * buffer limit on the path. Use this to set
1507 * the slow start threshhold, but set the
1508 * threshold to no less than 2*mss.
1509 */
1510 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1511 }
1512#endif /* RTV_MTU */
1513 return (mss);
1514}