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