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