add snd_cwndw to decrease outstanding data after a source quench
[unix-history] / usr / src / sys / netinet / tcp_input.c
CommitLineData
0d115dcc 1/* tcp_input.c 6.7 84/11/01 */
87e78f19 2
20666ad3
JB
3#include "param.h"
4#include "systm.h"
5#include "mbuf.h"
6#include "protosw.h"
7#include "socket.h"
8#include "socketvar.h"
9#include "errno.h"
f4d55810
SL
10
11#include "../net/if.h"
c124e997 12#include "../net/route.h"
f4d55810 13
20666ad3
JB
14#include "in.h"
15#include "in_pcb.h"
16#include "in_systm.h"
17#include "ip.h"
18#include "ip_var.h"
19#include "tcp.h"
20#include "tcp_fsm.h"
21#include "tcp_seq.h"
22#include "tcp_timer.h"
23#include "tcp_var.h"
24#include "tcpip.h"
25#include "tcp_debug.h"
87e78f19 26
22856bb8 27int tcpprintfs = 0;
60b16fa9 28int tcpcksum = 1;
4b935108 29struct tcpiphdr tcp_saveti;
8b5a83bb 30extern tcpnodelack;
87e78f19 31
4b935108 32struct tcpcb *tcp_newtcpcb();
2ff61f9d
BJ
33/*
34 * TCP input routine, follows pages 65-76 of the
35 * protocol specification dated September, 1981 very closely.
36 */
2b4b57cd
BJ
37tcp_input(m0)
38 struct mbuf *m0;
87e78f19 39{
2b4b57cd 40 register struct tcpiphdr *ti;
53a5409e 41 struct inpcb *inp;
2b4b57cd 42 register struct mbuf *m;
8b5a83bb 43 struct mbuf *om = 0;
2b4b57cd 44 int len, tlen, off;
8e65fd66 45 register struct tcpcb *tp = 0;
2b4b57cd 46 register int tiflags;
d52566dd 47 struct socket *so;
f1b2fa5b 48 int todrop, acked;
4b935108 49 short ostate;
ebcadd38 50 struct in_addr laddr;
7aa16f99 51 int dropsocket = 0;
87e78f19
BJ
52
53 /*
4aed14e3
BJ
54 * Get IP and TCP header together in first mbuf.
55 * Note: IP leaves IP header in first mbuf.
87e78f19 56 */
2b4b57cd 57 m = m0;
20790db4 58 ti = mtod(m, struct tcpiphdr *);
4aed14e3 59 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
d63599ac 60 ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
6703c41f
BJ
61 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
62 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
8a13b737 63 tcpstat.tcps_hdrops++;
6703c41f 64 return;
8a13b737
BJ
65 }
66 ti = mtod(m, struct tcpiphdr *);
67 }
87e78f19 68
2b4b57cd 69 /*
4aed14e3 70 * Checksum extended TCP header and data.
2b4b57cd
BJ
71 */
72 tlen = ((struct ip *)ti)->ip_len;
73 len = sizeof (struct ip) + tlen;
60b16fa9 74 if (tcpcksum) {
2b4b57cd
BJ
75 ti->ti_next = ti->ti_prev = 0;
76 ti->ti_x1 = 0;
ac83b17a 77 ti->ti_len = (u_short)tlen;
668cc26d 78 ti->ti_len = htons((u_short)ti->ti_len);
4b6b94ca 79 if (ti->ti_sum = in_cksum(m, len)) {
ee954ff1
SL
80 if (tcpprintfs)
81 printf("tcp sum: src %x\n", ti->ti_src);
2b4b57cd 82 tcpstat.tcps_badsum++;
8a13b737 83 goto drop;
87e78f19
BJ
84 }
85 }
86
87 /*
4aed14e3 88 * Check that TCP offset makes sense,
8b5a83bb 89 * pull out TCP options and adjust length.
87e78f19 90 */
2b4b57cd 91 off = ti->ti_off << 2;
4b6b94ca 92 if (off < sizeof (struct tcphdr) || off > tlen) {
ee954ff1
SL
93 if (tcpprintfs)
94 printf("tcp off: src %x off %d\n", ti->ti_src, off);
2b4b57cd 95 tcpstat.tcps_badoff++;
8a13b737 96 goto drop;
2b4b57cd 97 }
1e977657
BJ
98 tlen -= off;
99 ti->ti_len = tlen;
8b5a83bb
BJ
100 if (off > sizeof (struct tcphdr)) {
101 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
102 tcpstat.tcps_hdrops++;
01b1394e 103 return;
8b5a83bb
BJ
104 }
105 ti = mtod(m, struct tcpiphdr *);
cce93e4b 106 om = m_get(M_DONTWAIT, MT_DATA);
8b5a83bb
BJ
107 if (om == 0)
108 goto drop;
8b5a83bb
BJ
109 om->m_len = off - sizeof (struct tcphdr);
110 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
668cc26d 111 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
8b5a83bb 112 m->m_len -= om->m_len;
668cc26d
SL
113 bcopy(op+om->m_len, op,
114 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
8b5a83bb
BJ
115 }
116 }
2ff61f9d 117 tiflags = ti->ti_flags;
2b4b57cd 118
795e0416 119 /*
1e977657 120 * Drop TCP and IP headers.
795e0416
BJ
121 */
122 off += sizeof (struct ip);
123 m->m_off += off;
124 m->m_len -= off;
125
8a13b737 126 /*
4aed14e3 127 * Convert TCP protocol specific fields to host format.
8a13b737
BJ
128 */
129 ti->ti_seq = ntohl(ti->ti_seq);
130 ti->ti_ack = ntohl(ti->ti_ack);
131 ti->ti_win = ntohs(ti->ti_win);
132 ti->ti_urp = ntohs(ti->ti_urp);
133
2b4b57cd 134 /*
8075bb0e 135 * Locate pcb for segment.
2b4b57cd 136 */
2ff61f9d 137 inp = in_pcblookup
ebcadd38
BJ
138 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
139 INPLOOKUP_WILDCARD);
2ff61f9d
BJ
140
141 /*
142 * If the state is CLOSED (i.e., TCB does not exist) then
4aed14e3 143 * all data in the incoming segment is discarded.
2ff61f9d 144 */
22856bb8 145 if (inp == 0)
8a13b737 146 goto dropwithreset;
2ff61f9d 147 tp = intotcpcb(inp);
22856bb8 148 if (tp == 0)
8a13b737 149 goto dropwithreset;
f1b2fa5b 150 so = inp->inp_socket;
4b935108
BJ
151 if (so->so_options & SO_DEBUG) {
152 ostate = tp->t_state;
153 tcp_saveti = *ti;
154 }
ebf42a75
BJ
155 if (so->so_options & SO_ACCEPTCONN) {
156 so = sonewconn(so);
157 if (so == 0)
158 goto drop;
7aa16f99
SL
159 /*
160 * This is ugly, but ....
161 *
162 * Mark socket as temporary until we're
163 * committed to keeping it. The code at
164 * ``drop'' and ``dropwithreset'' check the
165 * flag dropsocket to see if the temporary
166 * socket created here should be discarded.
167 * We mark the socket as discardable until
168 * we're committed to it below in TCPS_LISTEN.
169 */
170 dropsocket++;
ebf42a75
BJ
171 inp = (struct inpcb *)so->so_pcb;
172 inp->inp_laddr = ti->ti_dst;
173 inp->inp_lport = ti->ti_dport;
174 tp = intotcpcb(inp);
175 tp->t_state = TCPS_LISTEN;
176 }
87e78f19 177
405c9168
BJ
178 /*
179 * Segment received on connection.
180 * Reset idle time and keep-alive timer.
181 */
182 tp->t_idle = 0;
183 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
184
8b5a83bb 185 /*
99578149
MK
186 * Process options if not in LISTEN state,
187 * else do it below (after getting remote address).
8b5a83bb 188 */
99578149
MK
189 if (om && tp->t_state != TCPS_LISTEN) {
190 tcp_dooptions(tp, om, ti);
8b5a83bb
BJ
191 om = 0;
192 }
193
87e78f19 194 /*
8a13b737
BJ
195 * Calculate amount of space in receive window,
196 * and then do TCP input processing.
87e78f19 197 */
8a13b737 198 tp->rcv_wnd = sbspace(&so->so_rcv);
4b6b94ca
BJ
199 if (tp->rcv_wnd < 0)
200 tp->rcv_wnd = 0;
2ff61f9d 201
87e78f19
BJ
202 switch (tp->t_state) {
203
2ff61f9d
BJ
204 /*
205 * If the state is LISTEN then ignore segment if it contains an RST.
206 * If the segment contains an ACK then it is bad and send a RST.
207 * If it does not contain a SYN then it is not interesting; drop it.
8a13b737 208 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
2ff61f9d 209 * tp->iss, and send a segment:
8a13b737 210 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
2ff61f9d
BJ
211 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
212 * Fill in remote peer address fields if not previously specified.
213 * Enter SYN_RECEIVED state, and process any other fields of this
4aed14e3 214 * segment in this state.
2ff61f9d 215 */
8075bb0e 216 case TCPS_LISTEN: {
789d2a39 217 struct mbuf *am;
8075bb0e
BJ
218 register struct sockaddr_in *sin;
219
2ff61f9d
BJ
220 if (tiflags & TH_RST)
221 goto drop;
22856bb8 222 if (tiflags & TH_ACK)
8a13b737 223 goto dropwithreset;
22856bb8 224 if ((tiflags & TH_SYN) == 0)
2ff61f9d 225 goto drop;
789d2a39
SL
226 am = m_get(M_DONTWAIT, MT_SONAME);
227 if (am == NULL)
228 goto drop;
229 am->m_len = sizeof (struct sockaddr_in);
a8d3bf7f 230 sin = mtod(am, struct sockaddr_in *);
8075bb0e
BJ
231 sin->sin_family = AF_INET;
232 sin->sin_addr = ti->ti_src;
233 sin->sin_port = ti->ti_sport;
ebcadd38 234 laddr = inp->inp_laddr;
789d2a39 235 if (inp->inp_laddr.s_addr == INADDR_ANY)
ebcadd38 236 inp->inp_laddr = ti->ti_dst;
a8d3bf7f 237 if (in_pcbconnect(inp, am)) {
ebcadd38 238 inp->inp_laddr = laddr;
5a1f132a 239 (void) m_free(am);
4aed14e3 240 goto drop;
ebcadd38 241 }
5a1f132a 242 (void) m_free(am);
4aed14e3
BJ
243 tp->t_template = tcp_template(tp);
244 if (tp->t_template == 0) {
245 in_pcbdisconnect(inp);
a4f7ea71 246 dropsocket = 0; /* socket is already gone */
ebcadd38 247 inp->inp_laddr = laddr;
93f92b1d 248 tp = 0;
4aed14e3
BJ
249 goto drop;
250 }
99578149
MK
251 if (om) {
252 tcp_dooptions(tp, om, ti);
253 om = 0;
254 }
8a13b737 255 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
2ff61f9d 256 tp->irs = ti->ti_seq;
8a13b737
BJ
257 tcp_sendseqinit(tp);
258 tcp_rcvseqinit(tp);
2ff61f9d 259 tp->t_state = TCPS_SYN_RECEIVED;
4aed14e3 260 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
7aa16f99 261 dropsocket = 0; /* committed to socket */
8a13b737 262 goto trimthenstep6;
8075bb0e 263 }
87e78f19 264
2ff61f9d
BJ
265 /*
266 * If the state is SYN_SENT:
267 * if seg contains an ACK, but not for our SYN, drop the input.
268 * if seg contains a RST, then drop the connection.
269 * if seg does not contain SYN, then drop it.
270 * Otherwise this is an acceptable SYN segment
271 * initialize tp->rcv_nxt and tp->irs
272 * if seg contains ack then advance tp->snd_una
273 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
274 * arrange for segment to be acked (eventually)
275 * continue processing rest of data/controls, beginning with URG
276 */
277 case TCPS_SYN_SENT:
278 if ((tiflags & TH_ACK) &&
22856bb8
BJ
279/* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
280 (SEQ_LT(ti->ti_ack, tp->iss) ||
4b6b94ca 281 SEQ_GT(ti->ti_ack, tp->snd_max)))
8a13b737 282 goto dropwithreset;
2ff61f9d 283 if (tiflags & TH_RST) {
0e3936fa
SL
284 if (tiflags & TH_ACK)
285 tp = tcp_drop(tp, ECONNREFUSED);
2ff61f9d 286 goto drop;
87e78f19 287 }
2ff61f9d
BJ
288 if ((tiflags & TH_SYN) == 0)
289 goto drop;
4b6b94ca 290 tp->snd_una = ti->ti_ack;
b8977237
BJ
291 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
292 tp->snd_nxt = tp->snd_una;
4aed14e3 293 tp->t_timer[TCPT_REXMT] = 0;
2ff61f9d 294 tp->irs = ti->ti_seq;
8a13b737
BJ
295 tcp_rcvseqinit(tp);
296 tp->t_flags |= TF_ACKNOW;
405c9168 297 if (SEQ_GT(tp->snd_una, tp->iss)) {
4aed14e3 298 soisconnected(so);
2ff61f9d 299 tp->t_state = TCPS_ESTABLISHED;
99578149 300 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
405c9168
BJ
301 (void) tcp_reass(tp, (struct tcpiphdr *)0);
302 } else
8a13b737
BJ
303 tp->t_state = TCPS_SYN_RECEIVED;
304 goto trimthenstep6;
305
306trimthenstep6:
307 /*
4b6b94ca 308 * Advance ti->ti_seq to correspond to first data byte.
8a13b737
BJ
309 * If data, trim to stay within window,
310 * dropping FIN if necessary.
311 */
4b6b94ca 312 ti->ti_seq++;
8a13b737
BJ
313 if (ti->ti_len > tp->rcv_wnd) {
314 todrop = ti->ti_len - tp->rcv_wnd;
315 m_adj(m, -todrop);
316 ti->ti_len = tp->rcv_wnd;
317 ti->ti_flags &= ~TH_FIN;
87e78f19 318 }
e832edbc 319 tp->snd_wl1 = ti->ti_seq - 1;
8a13b737 320 goto step6;
2ff61f9d 321 }
87e78f19 322
ed6315dd
MK
323 /*
324 * If data is received on a connection after the
325 * user processes are gone, then RST the other end.
326 */
327 if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
328 ti->ti_len) {
329 tp = tcp_close(tp);
330 goto dropwithreset;
331 }
332
2ff61f9d
BJ
333 /*
334 * States other than LISTEN or SYN_SENT.
335 * First check that at least some bytes of segment are within
336 * receive window.
337 */
338 if (tp->rcv_wnd == 0) {
339 /*
340 * If window is closed can only take segments at
4b6b94ca 341 * window edge, and have to drop data and PUSH from
2ff61f9d
BJ
342 * incoming segments.
343 */
22856bb8 344 if (tp->rcv_nxt != ti->ti_seq)
2ff61f9d 345 goto dropafterack;
8a13b737 346 if (ti->ti_len > 0) {
fd5dc5f0 347 m_adj(m, ti->ti_len);
8a13b737
BJ
348 ti->ti_len = 0;
349 ti->ti_flags &= ~(TH_PUSH|TH_FIN);
87e78f19 350 }
2ff61f9d
BJ
351 } else {
352 /*
4b6b94ca 353 * If segment begins before rcv_nxt, drop leading
2ff61f9d
BJ
354 * data (and SYN); if nothing left, just ack.
355 */
fd5dc5f0
BJ
356 todrop = tp->rcv_nxt - ti->ti_seq;
357 if (todrop > 0) {
8a13b737 358 if (tiflags & TH_SYN) {
22856bb8 359 tiflags &= ~TH_SYN;
fd5dc5f0 360 ti->ti_flags &= ~TH_SYN;
8a13b737
BJ
361 ti->ti_seq++;
362 if (ti->ti_urp > 1)
363 ti->ti_urp--;
364 else
365 tiflags &= ~TH_URG;
366 todrop--;
367 }
1e977657
BJ
368 if (todrop > ti->ti_len ||
369 todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
2ff61f9d
BJ
370 goto dropafterack;
371 m_adj(m, todrop);
372 ti->ti_seq += todrop;
373 ti->ti_len -= todrop;
8a13b737
BJ
374 if (ti->ti_urp > todrop)
375 ti->ti_urp -= todrop;
376 else {
377 tiflags &= ~TH_URG;
fd5dc5f0
BJ
378 ti->ti_flags &= ~TH_URG;
379 ti->ti_urp = 0;
8a13b737 380 }
2ff61f9d
BJ
381 }
382 /*
383 * If segment ends after window, drop trailing data
8a13b737 384 * (and PUSH and FIN); if nothing left, just ACK.
2ff61f9d 385 */
fd5dc5f0
BJ
386 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
387 if (todrop > 0) {
1e977657 388 if (todrop >= ti->ti_len)
2ff61f9d
BJ
389 goto dropafterack;
390 m_adj(m, -todrop);
391 ti->ti_len -= todrop;
8a13b737 392 ti->ti_flags &= ~(TH_PUSH|TH_FIN);
87e78f19 393 }
87e78f19 394 }
87e78f19 395
87e78f19 396 /*
2ff61f9d
BJ
397 * If the RST bit is set examine the state:
398 * SYN_RECEIVED STATE:
399 * If passive open, return to LISTEN state.
400 * If active open, inform user that connection was refused.
401 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
402 * Inform user that connection was reset, and close tcb.
403 * CLOSING, LAST_ACK, TIME_WAIT STATES
404 * Close the tcb.
87e78f19 405 */
2ff61f9d 406 if (tiflags&TH_RST) switch (tp->t_state) {
4b935108 407
2ff61f9d 408 case TCPS_SYN_RECEIVED:
0e3936fa 409 tp = tcp_drop(tp, ECONNREFUSED);
2ff61f9d
BJ
410 goto drop;
411
412 case TCPS_ESTABLISHED:
413 case TCPS_FIN_WAIT_1:
414 case TCPS_FIN_WAIT_2:
415 case TCPS_CLOSE_WAIT:
0e3936fa 416 tp = tcp_drop(tp, ECONNRESET);
2ff61f9d
BJ
417 goto drop;
418
419 case TCPS_CLOSING:
420 case TCPS_LAST_ACK:
421 case TCPS_TIME_WAIT:
0e3936fa 422 tp = tcp_close(tp);
2ff61f9d 423 goto drop;
87e78f19 424 }
87e78f19
BJ
425
426 /*
2ff61f9d
BJ
427 * If a SYN is in the window, then this is an
428 * error and we send an RST and drop the connection.
429 */
430 if (tiflags & TH_SYN) {
0e3936fa 431 tp = tcp_drop(tp, ECONNRESET);
8a13b737 432 goto dropwithreset;
2ff61f9d
BJ
433 }
434
435 /*
436 * If the ACK bit is off we drop the segment and return.
437 */
8a13b737 438 if ((tiflags & TH_ACK) == 0)
2ff61f9d
BJ
439 goto drop;
440
441 /*
442 * Ack processing.
87e78f19 443 */
87e78f19
BJ
444 switch (tp->t_state) {
445
2ff61f9d
BJ
446 /*
447 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
448 * ESTABLISHED state and continue processing, othewise
449 * send an RST.
450 */
451 case TCPS_SYN_RECEIVED:
8a13b737 452 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
4b6b94ca 453 SEQ_GT(ti->ti_ack, tp->snd_max))
8a13b737 454 goto dropwithreset;
4aed14e3 455 tp->snd_una++; /* SYN acked */
b8977237
BJ
456 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
457 tp->snd_nxt = tp->snd_una;
4aed14e3 458 tp->t_timer[TCPT_REXMT] = 0;
8a13b737
BJ
459 soisconnected(so);
460 tp->t_state = TCPS_ESTABLISHED;
99578149 461 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
405c9168 462 (void) tcp_reass(tp, (struct tcpiphdr *)0);
4aed14e3 463 tp->snd_wl1 = ti->ti_seq - 1;
8a13b737 464 /* fall into ... */
87e78f19 465
2ff61f9d
BJ
466 /*
467 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
468 * ACKs. If the ack is in the range
4b6b94ca 469 * tp->snd_una < ti->ti_ack <= tp->snd_max
2ff61f9d
BJ
470 * then advance tp->snd_una to ti->ti_ack and drop
471 * data from the retransmission queue. If this ACK reflects
472 * more up to date window information we update our window information.
473 */
474 case TCPS_ESTABLISHED:
475 case TCPS_FIN_WAIT_1:
476 case TCPS_FIN_WAIT_2:
477 case TCPS_CLOSE_WAIT:
478 case TCPS_CLOSING:
4aed14e3
BJ
479 case TCPS_LAST_ACK:
480 case TCPS_TIME_WAIT:
8a13b737
BJ
481#define ourfinisacked (acked > 0)
482
4aed14e3 483 if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
2ff61f9d 484 break;
22856bb8 485 if (SEQ_GT(ti->ti_ack, tp->snd_max))
2ff61f9d 486 goto dropafterack;
8a13b737 487 acked = ti->ti_ack - tp->snd_una;
dd020fc8
BJ
488
489 /*
490 * If transmit timer is running and timed sequence
491 * number was acked, update smoothed round trip time.
492 */
493 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
494 if (tp->t_srtt == 0)
495 tp->t_srtt = tp->t_rtt;
496 else
497 tp->t_srtt =
498 tcp_alpha * tp->t_srtt +
499 (1 - tcp_alpha) * tp->t_rtt;
dd020fc8
BJ
500 tp->t_rtt = 0;
501 }
502
6703c41f 503 if (ti->ti_ack == tp->snd_max)
4aed14e3 504 tp->t_timer[TCPT_REXMT] = 0;
6703c41f 505 else {
4aed14e3
BJ
506 TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
507 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
22856bb8 508 tp->t_rxtshift = 0;
8a13b737 509 }
6703c41f 510 if (acked > so->so_snd.sb_cc) {
6703c41f 511 tp->snd_wnd -= so->so_snd.sb_cc;
26e96dd8 512 sbdrop(&so->so_snd, so->so_snd.sb_cc);
6703c41f 513 } else {
668cc26d 514 sbdrop(&so->so_snd, acked);
6703c41f
BJ
515 tp->snd_wnd -= acked;
516 acked = 0;
517 }
5744ed2b 518 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
22856bb8 519 sowwakeup(so);
4b6b94ca 520 tp->snd_una = ti->ti_ack;
b8977237
BJ
521 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
522 tp->snd_nxt = tp->snd_una;
405c9168 523
87e78f19
BJ
524 switch (tp->t_state) {
525
2ff61f9d
BJ
526 /*
527 * In FIN_WAIT_1 STATE in addition to the processing
528 * for the ESTABLISHED state if our FIN is now acknowledged
8a13b737 529 * then enter FIN_WAIT_2.
2ff61f9d
BJ
530 */
531 case TCPS_FIN_WAIT_1:
fdae4427
BJ
532 if (ourfinisacked) {
533 /*
534 * If we can't receive any more
535 * data, then closing user can proceed.
536 */
537 if (so->so_state & SS_CANTRCVMORE)
538 soisdisconnected(so);
8a13b737 539 tp->t_state = TCPS_FIN_WAIT_2;
a4f7ea71
MK
540 /*
541 * This is contrary to the specification,
542 * but if we haven't gotten our FIN in
543 * 5 minutes, it's not forthcoming.
544 */
0d115dcc 545 tp->t_timer[TCPT_2MSL] = 5 * 60 * PR_SLOWHZ;
fdae4427 546 }
87e78f19
BJ
547 break;
548
2ff61f9d
BJ
549 /*
550 * In CLOSING STATE in addition to the processing for
551 * the ESTABLISHED state if the ACK acknowledges our FIN
552 * then enter the TIME-WAIT state, otherwise ignore
553 * the segment.
554 */
555 case TCPS_CLOSING:
4aed14e3 556 if (ourfinisacked) {
2ff61f9d 557 tp->t_state = TCPS_TIME_WAIT;
4aed14e3
BJ
558 tcp_canceltimers(tp);
559 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
560 soisdisconnected(so);
561 }
562 break;
87e78f19 563
2ff61f9d 564 /*
8a13b737
BJ
565 * The only thing that can arrive in LAST_ACK state
566 * is an acknowledgment of our FIN. If our FIN is now
567 * acknowledged, delete the TCB, enter the closed state
568 * and return.
2ff61f9d
BJ
569 */
570 case TCPS_LAST_ACK:
0e3936fa
SL
571 if (ourfinisacked)
572 tp = tcp_close(tp);
2ff61f9d 573 goto drop;
87e78f19 574
2ff61f9d
BJ
575 /*
576 * In TIME_WAIT state the only thing that should arrive
577 * is a retransmission of the remote FIN. Acknowledge
578 * it and restart the finack timer.
579 */
580 case TCPS_TIME_WAIT:
405c9168 581 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
2ff61f9d 582 goto dropafterack;
87e78f19 583 }
8a13b737
BJ
584#undef ourfinisacked
585 }
87e78f19 586
2ff61f9d 587step6:
4aed14e3
BJ
588 /*
589 * Update window information.
590 */
22856bb8 591 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
8e65fd66 592 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
22856bb8 593 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
4aed14e3
BJ
594 tp->snd_wnd = ti->ti_win;
595 tp->snd_wl1 = ti->ti_seq;
596 tp->snd_wl2 = ti->ti_ack;
a8d3bf7f 597 if (tp->snd_wnd != 0)
4aed14e3
BJ
598 tp->t_timer[TCPT_PERSIST] = 0;
599 }
4aed14e3 600
2ff61f9d 601 /*
b2db9217 602 * Process segments with URG.
2ff61f9d 603 */
9c811062
BJ
604 if ((tiflags & TH_URG) && ti->ti_urp &&
605 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
f4be5024 606 /*
a5d9c993
SL
607 * This is a kludge, but if we receive accept
608 * random urgent pointers, we'll crash in
609 * soreceive. It's hard to imagine someone
610 * actually wanting to send this much urgent data.
f4be5024 611 */
a4f7ea71 612 if (ti->ti_urp > tp->rcv_wnd + 1) { /* XXX */
f4be5024
SL
613 ti->ti_urp = 0; /* XXX */
614 tiflags &= ~TH_URG; /* XXX */
615 ti->ti_flags &= ~TH_URG; /* XXX */
a5d9c993 616 goto badurp; /* XXX */
f4be5024 617 }
b2db9217
BJ
618 /*
619 * If this segment advances the known urgent pointer,
620 * then mark the data stream. This should not happen
621 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
622 * a FIN has been received from the remote side.
623 * In these states we ignore the URG.
624 */
625 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
626 tp->rcv_up = ti->ti_seq + ti->ti_urp;
627 so->so_oobmark = so->so_rcv.sb_cc +
628 (tp->rcv_up - tp->rcv_nxt) - 1;
629 if (so->so_oobmark == 0)
630 so->so_state |= SS_RCVATMARK;
77a4e3ca 631 sohasoutofband(so);
b2db9217
BJ
632 tp->t_oobflags &= ~TCPOOB_HAVEDATA;
633 }
634 /*
635 * Remove out of band data so doesn't get presented to user.
636 * This can happen independent of advancing the URG pointer,
637 * but if two URG's are pending at once, some out-of-band
638 * data may creep in... ick.
639 */
ebf42a75 640 if (ti->ti_urp <= ti->ti_len)
b2db9217 641 tcp_pulloutofband(so, ti);
5e74df82 642 }
a5d9c993 643badurp: /* XXX */
87e78f19
BJ
644
645 /*
2ff61f9d
BJ
646 * Process the segment text, merging it into the TCP sequencing queue,
647 * and arranging for acknowledgment of receipt if necessary.
648 * This process logically involves adjusting tp->rcv_wnd as data
649 * is presented to the user (this happens in tcp_usrreq.c,
650 * case PRU_RCVD). If a FIN has already been received on this
651 * connection then we just ignore the text.
87e78f19 652 */
e832edbc
BJ
653 if ((ti->ti_len || (tiflags&TH_FIN)) &&
654 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2ff61f9d 655 tiflags = tcp_reass(tp, ti);
8b5a83bb
BJ
656 if (tcpnodelack == 0)
657 tp->t_flags |= TF_DELACK;
658 else
659 tp->t_flags |= TF_ACKNOW;
4aed14e3 660 } else {
2b4b57cd 661 m_freem(m);
e832edbc 662 tiflags &= ~TH_FIN;
4aed14e3 663 }
87e78f19
BJ
664
665 /*
e832edbc
BJ
666 * If FIN is received ACK the FIN and let the user know
667 * that the connection is closing.
87e78f19 668 */
e832edbc 669 if (tiflags & TH_FIN) {
4aed14e3
BJ
670 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
671 socantrcvmore(so);
672 tp->t_flags |= TF_ACKNOW;
673 tp->rcv_nxt++;
674 }
2ff61f9d 675 switch (tp->t_state) {
87e78f19 676
2ff61f9d
BJ
677 /*
678 * In SYN_RECEIVED and ESTABLISHED STATES
679 * enter the CLOSE_WAIT state.
53a5409e 680 */
2ff61f9d
BJ
681 case TCPS_SYN_RECEIVED:
682 case TCPS_ESTABLISHED:
683 tp->t_state = TCPS_CLOSE_WAIT;
684 break;
53a5409e 685
2ff61f9d 686 /*
8a13b737
BJ
687 * If still in FIN_WAIT_1 STATE FIN has not been acked so
688 * enter the CLOSING state.
53a5409e 689 */
2ff61f9d 690 case TCPS_FIN_WAIT_1:
8a13b737 691 tp->t_state = TCPS_CLOSING;
2ff61f9d 692 break;
87e78f19 693
2ff61f9d
BJ
694 /*
695 * In FIN_WAIT_2 state enter the TIME_WAIT state,
696 * starting the time-wait timer, turning off the other
697 * standard timers.
698 */
699 case TCPS_FIN_WAIT_2:
4aed14e3 700 tp->t_state = TCPS_TIME_WAIT;
a6503abf 701 tcp_canceltimers(tp);
405c9168 702 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
4aed14e3 703 soisdisconnected(so);
2ff61f9d
BJ
704 break;
705
53a5409e 706 /*
2ff61f9d 707 * In TIME_WAIT state restart the 2 MSL time_wait timer.
53a5409e 708 */
2ff61f9d 709 case TCPS_TIME_WAIT:
405c9168 710 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
2ff61f9d 711 break;
8a13b737 712 }
87e78f19 713 }
4b935108
BJ
714 if (so->so_options & SO_DEBUG)
715 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
8a13b737
BJ
716
717 /*
718 * Return any desired output.
719 */
668cc26d 720 (void) tcp_output(tp);
2ff61f9d 721 return;
8a13b737 722
2ff61f9d 723dropafterack:
8a13b737 724 /*
1e977657
BJ
725 * Generate an ACK dropping incoming segment if it occupies
726 * sequence space, where the ACK reflects our state.
8a13b737 727 */
1e977657
BJ
728 if ((tiflags&TH_RST) ||
729 tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
8a13b737 730 goto drop;
f3cdd721
BJ
731 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
732 tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
8e65fd66 733 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
4b6b94ca 734 return;
8a13b737
BJ
735
736dropwithreset:
f37c1c84 737 if (om) {
668cc26d 738 (void) m_free(om);
f37c1c84
SL
739 om = 0;
740 }
8a13b737 741 /*
4aed14e3 742 * Generate a RST, dropping incoming segment.
8a13b737
BJ
743 * Make ACK acceptable to originator of segment.
744 */
745 if (tiflags & TH_RST)
746 goto drop;
747 if (tiflags & TH_ACK)
8e65fd66 748 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
8a13b737
BJ
749 else {
750 if (tiflags & TH_SYN)
751 ti->ti_len++;
1e977657
BJ
752 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
753 TH_RST|TH_ACK);
8a13b737 754 }
7aa16f99
SL
755 /* destroy temporarily created socket */
756 if (dropsocket)
757 (void) soabort(so);
4b6b94ca 758 return;
8a13b737 759
2ff61f9d 760drop:
01b1394e
SL
761 if (om)
762 (void) m_free(om);
8a13b737
BJ
763 /*
764 * Drop space held by incoming segment and return.
765 */
f3cdd721
BJ
766 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
767 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
2ff61f9d 768 m_freem(m);
7aa16f99
SL
769 /* destroy temporarily created socket */
770 if (dropsocket)
771 (void) soabort(so);
4b935108 772 return;
2ff61f9d
BJ
773}
774
99578149 775tcp_dooptions(tp, om, ti)
8b5a83bb
BJ
776 struct tcpcb *tp;
777 struct mbuf *om;
99578149 778 struct tcpiphdr *ti;
5e74df82 779{
8b5a83bb
BJ
780 register u_char *cp;
781 int opt, optlen, cnt;
782
783 cp = mtod(om, u_char *);
784 cnt = om->m_len;
785 for (; cnt > 0; cnt -= optlen, cp += optlen) {
786 opt = cp[0];
787 if (opt == TCPOPT_EOL)
788 break;
789 if (opt == TCPOPT_NOP)
790 optlen = 1;
357b20fc 791 else {
8b5a83bb 792 optlen = cp[1];
357b20fc
SL
793 if (optlen <= 0)
794 break;
795 }
8b5a83bb
BJ
796 switch (opt) {
797
798 default:
799 break;
800
801 case TCPOPT_MAXSEG:
802 if (optlen != 4)
803 continue;
99578149
MK
804 if (!(ti->ti_flags & TH_SYN))
805 continue;
8b5a83bb 806 tp->t_maxseg = *(u_short *)(cp + 2);
668cc26d 807 tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
99578149 808 tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
8b5a83bb 809 break;
8b5a83bb 810 }
5e74df82 811 }
668cc26d 812 (void) m_free(om);
5e74df82
BJ
813}
814
b2db9217
BJ
815/*
816 * Pull out of band byte out of a segment so
817 * it doesn't appear in the user's data queue.
818 * It is still reflected in the segment length for
819 * sequencing purposes.
820 */
821tcp_pulloutofband(so, ti)
822 struct socket *so;
823 struct tcpiphdr *ti;
824{
825 register struct mbuf *m;
1acff8ec 826 int cnt = ti->ti_urp - 1;
b2db9217
BJ
827
828 m = dtom(ti);
829 while (cnt >= 0) {
830 if (m->m_len > cnt) {
831 char *cp = mtod(m, caddr_t) + cnt;
832 struct tcpcb *tp = sototcpcb(so);
833
834 tp->t_iobc = *cp;
835 tp->t_oobflags |= TCPOOB_HAVEDATA;
668cc26d 836 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
b2db9217
BJ
837 m->m_len--;
838 return;
839 }
840 cnt -= m->m_len;
841 m = m->m_next;
842 if (m == 0)
843 break;
844 }
845 panic("tcp_pulloutofband");
846}
847
2ff61f9d
BJ
848/*
849 * Insert segment ti into reassembly queue of tcp with
850 * control block tp. Return TH_FIN if reassembly now includes
851 * a segment with FIN.
852 */
f1b2fa5b 853tcp_reass(tp, ti)
2ff61f9d
BJ
854 register struct tcpcb *tp;
855 register struct tcpiphdr *ti;
2ff61f9d
BJ
856{
857 register struct tcpiphdr *q;
8a13b737 858 struct socket *so = tp->t_inpcb->inp_socket;
e832edbc
BJ
859 struct mbuf *m;
860 int flags;
2ff61f9d
BJ
861
862 /*
405c9168
BJ
863 * Call with ti==0 after become established to
864 * force pre-ESTABLISHED data up to user socket.
2ff61f9d 865 */
405c9168 866 if (ti == 0)
2ff61f9d 867 goto present;
87e78f19 868
2ff61f9d
BJ
869 /*
870 * Find a segment which begins after this one does.
871 */
872 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
873 q = (struct tcpiphdr *)q->ti_next)
874 if (SEQ_GT(q->ti_seq, ti->ti_seq))
875 break;
876
877 /*
878 * If there is a preceding segment, it may provide some of
879 * our data already. If so, drop the data from the incoming
880 * segment. If it provides all of our data, drop us.
881 */
882 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
883 register int i;
fd5dc5f0 884 q = (struct tcpiphdr *)q->ti_prev;
2ff61f9d
BJ
885 /* conversion to int (in i) handles seq wraparound */
886 i = q->ti_seq + q->ti_len - ti->ti_seq;
887 if (i > 0) {
2b4b57cd 888 if (i >= ti->ti_len)
2ff61f9d 889 goto drop;
4ab1a5c3 890 m_adj(dtom(ti), i);
2b4b57cd 891 ti->ti_len -= i;
2ff61f9d 892 ti->ti_seq += i;
53a5409e 893 }
2ff61f9d
BJ
894 q = (struct tcpiphdr *)(q->ti_next);
895 }
87e78f19 896
2ff61f9d
BJ
897 /*
898 * While we overlap succeeding segments trim them or,
899 * if they are completely covered, dequeue them.
900 */
fd5dc5f0 901 while (q != (struct tcpiphdr *)tp) {
2ff61f9d 902 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
fd5dc5f0
BJ
903 if (i <= 0)
904 break;
2ff61f9d 905 if (i < q->ti_len) {
fd5dc5f0 906 q->ti_seq += i;
2ff61f9d
BJ
907 q->ti_len -= i;
908 m_adj(dtom(q), i);
909 break;
ac5e71a1 910 }
2ff61f9d 911 q = (struct tcpiphdr *)q->ti_next;
473a17a5 912 m = dtom(q->ti_prev);
2ff61f9d 913 remque(q->ti_prev);
473a17a5 914 m_freem(m);
2ff61f9d 915 }
87e78f19 916
2ff61f9d
BJ
917 /*
918 * Stick new segment in its place.
919 */
920 insque(ti, q->ti_prev);
2ff61f9d 921
2ff61f9d
BJ
922present:
923 /*
4aed14e3
BJ
924 * Present data to user, advancing rcv_nxt through
925 * completed sequence space.
2ff61f9d 926 */
e832edbc 927 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
4aed14e3 928 return (0);
2b4b57cd 929 ti = tp->seg_next;
e832edbc
BJ
930 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
931 return (0);
932 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
933 return (0);
934 do {
4aed14e3
BJ
935 tp->rcv_nxt += ti->ti_len;
936 flags = ti->ti_flags & TH_FIN;
2b4b57cd 937 remque(ti);
e832edbc 938 m = dtom(ti);
2b4b57cd 939 ti = (struct tcpiphdr *)ti->ti_next;
e832edbc 940 if (so->so_state & SS_CANTRCVMORE)
668cc26d 941 m_freem(m);
789d2a39 942 else
e832edbc
BJ
943 sbappend(&so->so_rcv, m);
944 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
945 sorwakeup(so);
2ff61f9d
BJ
946 return (flags);
947drop:
948 m_freem(dtom(ti));
e832edbc 949 return (0);
d52566dd 950}
99578149
MK
951
952/*
953 * Determine a reasonable value for maxseg size.
954 * If the route is known, use one that can be handled
955 * on the given interface without forcing IP to fragment.
956 * If bigger than a page (CLSIZE), round down to nearest pagesize
957 * to utilize pagesize mbufs.
958 * If interface pointer is unavailable, or the destination isn't local,
959 * use a conservative size (512 or the default IP max size),
960 * as we can't discover anything about intervening gateways or networks.
961 *
962 * This is ugly, and doesn't belong at this level, but has to happen somehow.
963 */
964tcp_mss(tp)
965register struct tcpcb *tp;
966{
967 struct route *ro;
968 struct ifnet *ifp;
969 int mss;
970 struct inpcb *inp;
971
972 inp = tp->t_inpcb;
973 ro = &inp->inp_route;
974 if ((ro->ro_rt == (struct rtentry *)0) ||
975 (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
976 /* No route yet, so try to acquire one */
977 if (inp->inp_faddr.s_addr != INADDR_ANY) {
978 ro->ro_dst.sa_family = AF_INET;
979 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
980 inp->inp_faddr;
981 rtalloc(ro);
982 }
983 if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
0d115dcc 984 return (TCP_MSS);
99578149
MK
985 }
986
987 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
988#if (CLBYTES & (CLBYTES - 1)) == 0
989 if (mss > CLBYTES)
990 mss &= ~(CLBYTES-1);
991#else
992 if (mss > CLBYTES)
993 mss = mss / CLBYTES * CLBYTES;
994#endif
995 if (in_localaddr(tp->t_inpcb->inp_faddr))
996 return(mss);
0d115dcc 997 return (MIN(mss, TCP_MSS));
99578149 998}