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