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