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