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