speed-ups and clean-ups
[unix-history] / usr / src / sys / netinet / tcp_input.c
... / ...
CommitLineData
1/* tcp_input.c 1.85 83/01/04 */
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/mbuf.h"
6#include "../h/protosw.h"
7#include "../h/socket.h"
8#include "../h/socketvar.h"
9#include "../netinet/in.h"
10#include "../net/route.h"
11#include "../netinet/in_pcb.h"
12#include "../netinet/in_systm.h"
13#include "../net/if.h"
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"
23#include <errno.h>
24
25int tcpprintfs = 0;
26int tcpcksum = 1;
27struct tcpiphdr tcp_saveti;
28extern tcpnodelack;
29
30struct tcpcb *tcp_newtcpcb();
31/*
32 * TCP input routine, follows pages 65-76 of the
33 * protocol specification dated September, 1981 very closely.
34 */
35tcp_input(m0)
36 struct mbuf *m0;
37{
38 register struct tcpiphdr *ti;
39 struct inpcb *inp;
40 register struct mbuf *m;
41 struct mbuf *om = 0;
42 int len, tlen, off;
43 register struct tcpcb *tp = 0;
44 register int tiflags;
45 struct socket *so;
46 int todrop, acked;
47 short ostate;
48 struct in_addr laddr;
49
50 /*
51 * Get IP and TCP header together in first mbuf.
52 * Note: IP leaves IP header in first mbuf.
53 */
54 m = m0;
55 ti = mtod(m, struct tcpiphdr *);
56 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
57 ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
58 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
59 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
60 tcpstat.tcps_hdrops++;
61 return;
62 }
63 ti = mtod(m, struct tcpiphdr *);
64 }
65
66 /*
67 * Checksum extended TCP header and data.
68 */
69 tlen = ((struct ip *)ti)->ip_len;
70 len = sizeof (struct ip) + tlen;
71 if (tcpcksum) {
72 ti->ti_next = ti->ti_prev = 0;
73 ti->ti_x1 = 0;
74 ti->ti_len = (u_short)tlen;
75 ti->ti_len = htons((u_short)ti->ti_len);
76 if (ti->ti_sum = in_cksum(m, len)) {
77 tcpstat.tcps_badsum++;
78 if (tcpprintfs)
79 printf("tcp cksum %x\n", ti->ti_sum);
80 goto drop;
81 }
82 }
83
84 /*
85 * Check that TCP offset makes sense,
86 * pull out TCP options and adjust length.
87 */
88 off = ti->ti_off << 2;
89 if (off < sizeof (struct tcphdr) || off > tlen) {
90 tcpstat.tcps_badoff++;
91 goto drop;
92 }
93 tlen -= off;
94 ti->ti_len = tlen;
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, MT_DATA);
102 if (om == 0)
103 goto drop;
104 om->m_len = off - sizeof (struct tcphdr);
105 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
106 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
107 m->m_len -= om->m_len;
108 bcopy(op+om->m_len, op,
109 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
110 }
111 }
112 tiflags = ti->ti_flags;
113
114 /*
115 * Drop TCP and IP headers.
116 */
117 off += sizeof (struct ip);
118 m->m_off += off;
119 m->m_len -= off;
120
121 /*
122 * Convert TCP protocol specific fields to host format.
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
129 /*
130 * Locate pcb for segment.
131 */
132 inp = in_pcblookup
133 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
134 INPLOOKUP_WILDCARD);
135
136 /*
137 * If the state is CLOSED (i.e., TCB does not exist) then
138 * all data in the incoming segment is discarded.
139 */
140 if (inp == 0)
141 goto dropwithreset;
142 tp = intotcpcb(inp);
143 if (tp == 0)
144 goto dropwithreset;
145 so = inp->inp_socket;
146 if (so->so_options & SO_DEBUG) {
147 ostate = tp->t_state;
148 tcp_saveti = *ti;
149 }
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 }
160
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
168 /*
169 * Process options.
170 */
171 if (om) {
172 tcp_dooptions(tp, om);
173 om = 0;
174 }
175
176 /*
177 * Calculate amount of space in receive window,
178 * and then do TCP input processing.
179 */
180 tp->rcv_wnd = sbspace(&so->so_rcv);
181 if (tp->rcv_wnd < 0)
182 tp->rcv_wnd = 0;
183
184 switch (tp->t_state) {
185
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.
190 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
191 * tp->iss, and send a segment:
192 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
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
196 * segment in this state.
197 */
198 case TCPS_LISTEN: {
199 struct mbuf *am;
200 register struct sockaddr_in *sin;
201
202 if (tiflags & TH_RST)
203 goto drop;
204 if (tiflags & TH_ACK)
205 goto dropwithreset;
206 if ((tiflags & TH_SYN) == 0)
207 goto drop;
208 am = m_get(M_DONTWAIT, MT_SONAME);
209 if (am == NULL)
210 goto drop;
211 am->m_len = sizeof (struct sockaddr_in);
212 sin = mtod(am, struct sockaddr_in *);
213 sin->sin_family = AF_INET;
214 sin->sin_addr = ti->ti_src;
215 sin->sin_port = ti->ti_sport;
216 laddr = inp->inp_laddr;
217 if (inp->inp_laddr.s_addr == INADDR_ANY)
218 inp->inp_laddr = ti->ti_dst;
219 if (in_pcbconnect(inp, am)) {
220 inp->inp_laddr = laddr;
221 (void) m_free(am);
222 goto drop;
223 }
224 (void) m_free(am);
225 tp->t_template = tcp_template(tp);
226 if (tp->t_template == 0) {
227 in_pcbdisconnect(inp);
228 inp->inp_laddr = laddr;
229 tp = 0;
230 goto drop;
231 }
232 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
233 tp->irs = ti->ti_seq;
234 tcp_sendseqinit(tp);
235 tcp_rcvseqinit(tp);
236 tp->t_state = TCPS_SYN_RECEIVED;
237 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
238 goto trimthenstep6;
239 }
240
241 /*
242 * If the state is SYN_SENT:
243 * if seg contains an ACK, but not for our SYN, drop the input.
244 * if seg contains a RST, then drop the connection.
245 * if seg does not contain SYN, then drop it.
246 * Otherwise this is an acceptable SYN segment
247 * initialize tp->rcv_nxt and tp->irs
248 * if seg contains ack then advance tp->snd_una
249 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
250 * arrange for segment to be acked (eventually)
251 * continue processing rest of data/controls, beginning with URG
252 */
253 case TCPS_SYN_SENT:
254 if ((tiflags & TH_ACK) &&
255/* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
256 (SEQ_LT(ti->ti_ack, tp->iss) ||
257 SEQ_GT(ti->ti_ack, tp->snd_max)))
258 goto dropwithreset;
259 if (tiflags & TH_RST) {
260 if (tiflags & TH_ACK) {
261 tcp_drop(tp, ECONNREFUSED);
262 tp = 0;
263 }
264 goto drop;
265 }
266 if ((tiflags & TH_SYN) == 0)
267 goto drop;
268 tp->snd_una = ti->ti_ack;
269 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
270 tp->snd_nxt = tp->snd_una;
271 tp->t_timer[TCPT_REXMT] = 0;
272 tp->irs = ti->ti_seq;
273 tcp_rcvseqinit(tp);
274 tp->t_flags |= TF_ACKNOW;
275 if (SEQ_GT(tp->snd_una, tp->iss)) {
276 soisconnected(so);
277 tp->t_state = TCPS_ESTABLISHED;
278 (void) tcp_reass(tp, (struct tcpiphdr *)0);
279 } else
280 tp->t_state = TCPS_SYN_RECEIVED;
281 goto trimthenstep6;
282
283trimthenstep6:
284 /*
285 * Advance ti->ti_seq to correspond to first data byte.
286 * If data, trim to stay within window,
287 * dropping FIN if necessary.
288 */
289 ti->ti_seq++;
290 if (ti->ti_len > tp->rcv_wnd) {
291 todrop = ti->ti_len - tp->rcv_wnd;
292 m_adj(m, -todrop);
293 ti->ti_len = tp->rcv_wnd;
294 ti->ti_flags &= ~TH_FIN;
295 }
296 tp->snd_wl1 = ti->ti_seq - 1;
297 goto step6;
298 }
299
300 /*
301 * States other than LISTEN or SYN_SENT.
302 * First check that at least some bytes of segment are within
303 * receive window.
304 */
305 if (tp->rcv_wnd == 0) {
306 /*
307 * If window is closed can only take segments at
308 * window edge, and have to drop data and PUSH from
309 * incoming segments.
310 */
311 if (tp->rcv_nxt != ti->ti_seq)
312 goto dropafterack;
313 if (ti->ti_len > 0) {
314 m_adj(m, ti->ti_len);
315 ti->ti_len = 0;
316 ti->ti_flags &= ~(TH_PUSH|TH_FIN);
317 }
318 } else {
319 /*
320 * If segment begins before rcv_nxt, drop leading
321 * data (and SYN); if nothing left, just ack.
322 */
323 todrop = tp->rcv_nxt - ti->ti_seq;
324 if (todrop > 0) {
325 if (tiflags & TH_SYN) {
326 tiflags &= ~TH_SYN;
327 ti->ti_flags &= ~TH_SYN;
328 ti->ti_seq++;
329 if (ti->ti_urp > 1)
330 ti->ti_urp--;
331 else
332 tiflags &= ~TH_URG;
333 todrop--;
334 }
335 if (todrop > ti->ti_len ||
336 todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
337 goto dropafterack;
338 m_adj(m, todrop);
339 ti->ti_seq += todrop;
340 ti->ti_len -= todrop;
341 if (ti->ti_urp > todrop)
342 ti->ti_urp -= todrop;
343 else {
344 tiflags &= ~TH_URG;
345 ti->ti_flags &= ~TH_URG;
346 ti->ti_urp = 0;
347 }
348 }
349 /*
350 * If segment ends after window, drop trailing data
351 * (and PUSH and FIN); if nothing left, just ACK.
352 */
353 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
354 if (todrop > 0) {
355 if (todrop >= ti->ti_len)
356 goto dropafterack;
357 m_adj(m, -todrop);
358 ti->ti_len -= todrop;
359 ti->ti_flags &= ~(TH_PUSH|TH_FIN);
360 }
361 }
362
363 /*
364 * If data is received on a connection after the
365 * user processes are gone, then RST the other end.
366 */
367 if ((so->so_state & SS_NOFDREF) && ti->ti_len) {
368 tcp_close(tp);
369 tp = 0;
370 goto dropwithreset;
371 }
372
373 /*
374 * If the RST bit is set examine the state:
375 * SYN_RECEIVED STATE:
376 * If passive open, return to LISTEN state.
377 * If active open, inform user that connection was refused.
378 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
379 * Inform user that connection was reset, and close tcb.
380 * CLOSING, LAST_ACK, TIME_WAIT STATES
381 * Close the tcb.
382 */
383 if (tiflags&TH_RST) switch (tp->t_state) {
384
385 case TCPS_SYN_RECEIVED:
386 tcp_drop(tp, ECONNREFUSED);
387 tp = 0;
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);
395 tp = 0;
396 goto drop;
397
398 case TCPS_CLOSING:
399 case TCPS_LAST_ACK:
400 case TCPS_TIME_WAIT:
401 tcp_close(tp);
402 tp = 0;
403 goto drop;
404 }
405
406 /*
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) {
411 tcp_drop(tp, ECONNRESET);
412 tp = 0;
413 goto dropwithreset;
414 }
415
416 /*
417 * If the ACK bit is off we drop the segment and return.
418 */
419 if ((tiflags & TH_ACK) == 0)
420 goto drop;
421
422 /*
423 * Ack processing.
424 */
425 switch (tp->t_state) {
426
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:
433 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
434 SEQ_GT(ti->ti_ack, tp->snd_max))
435 goto dropwithreset;
436 tp->snd_una++; /* SYN acked */
437 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
438 tp->snd_nxt = tp->snd_una;
439 tp->t_timer[TCPT_REXMT] = 0;
440 soisconnected(so);
441 tp->t_state = TCPS_ESTABLISHED;
442 (void) tcp_reass(tp, (struct tcpiphdr *)0);
443 tp->snd_wl1 = ti->ti_seq - 1;
444 /* fall into ... */
445
446 /*
447 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
448 * ACKs. If the ack is in the range
449 * tp->snd_una < ti->ti_ack <= tp->snd_max
450 * then advance tp->snd_una to ti->ti_ack and drop
451 * data from the retransmission queue. If this ACK reflects
452 * more up to date window information we update our window information.
453 */
454 case TCPS_ESTABLISHED:
455 case TCPS_FIN_WAIT_1:
456 case TCPS_FIN_WAIT_2:
457 case TCPS_CLOSE_WAIT:
458 case TCPS_CLOSING:
459 case TCPS_LAST_ACK:
460 case TCPS_TIME_WAIT:
461#define ourfinisacked (acked > 0)
462
463 if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
464 break;
465 if (SEQ_GT(ti->ti_ack, tp->snd_max))
466 goto dropafterack;
467 acked = ti->ti_ack - tp->snd_una;
468
469 /*
470 * If transmit timer is running and timed sequence
471 * number was acked, update smoothed round trip time.
472 */
473 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
474 if (tp->t_srtt == 0)
475 tp->t_srtt = tp->t_rtt;
476 else
477 tp->t_srtt =
478 tcp_alpha * tp->t_srtt +
479 (1 - tcp_alpha) * tp->t_rtt;
480 tp->t_rtt = 0;
481 }
482
483 if (ti->ti_ack == tp->snd_max)
484 tp->t_timer[TCPT_REXMT] = 0;
485 else {
486 TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
487 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
488 tp->t_rtt = 1;
489 tp->t_rxtshift = 0;
490 }
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 {
495 sbdrop(&so->so_snd, acked);
496 tp->snd_wnd -= acked;
497 acked = 0;
498 }
499 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
500 sowwakeup(so);
501 tp->snd_una = ti->ti_ack;
502 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
503 tp->snd_nxt = tp->snd_una;
504
505 switch (tp->t_state) {
506
507 /*
508 * In FIN_WAIT_1 STATE in addition to the processing
509 * for the ESTABLISHED state if our FIN is now acknowledged
510 * then enter FIN_WAIT_2.
511 */
512 case TCPS_FIN_WAIT_1:
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);
520 tp->t_state = TCPS_FIN_WAIT_2;
521 }
522 break;
523
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:
531 if (ourfinisacked) {
532 tp->t_state = TCPS_TIME_WAIT;
533 tcp_canceltimers(tp);
534 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
535 soisdisconnected(so);
536 }
537 break;
538
539 /*
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.
544 */
545 case TCPS_LAST_ACK:
546 if (ourfinisacked) {
547 tcp_close(tp);
548 tp = 0;
549 }
550 goto drop;
551
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:
558 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
559 goto dropafterack;
560 }
561#undef ourfinisacked
562 }
563
564step6:
565 /*
566 * Update window information.
567 */
568 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
569 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
570 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
571 tp->snd_wnd = ti->ti_win;
572 tp->snd_wl1 = ti->ti_seq;
573 tp->snd_wl2 = ti->ti_ack;
574 if (tp->snd_wnd != 0)
575 tp->t_timer[TCPT_PERSIST] = 0;
576 }
577
578 /*
579 * Process segments with URG.
580 */
581 if ((tiflags & TH_URG) && ti->ti_urp &&
582 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
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;
596 sohasoutofband(so);
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 */
605 if (ti->ti_urp <= ti->ti_len)
606 tcp_pulloutofband(so, ti);
607 }
608
609 /*
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.
616 */
617 if ((ti->ti_len || (tiflags&TH_FIN)) &&
618 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
619 tiflags = tcp_reass(tp, ti);
620 if (tcpnodelack == 0)
621 tp->t_flags |= TF_DELACK;
622 else
623 tp->t_flags |= TF_ACKNOW;
624 } else {
625 m_freem(m);
626 tiflags &= ~TH_FIN;
627 }
628
629 /*
630 * If FIN is received ACK the FIN and let the user know
631 * that the connection is closing.
632 */
633 if (tiflags & TH_FIN) {
634 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
635 socantrcvmore(so);
636 tp->t_flags |= TF_ACKNOW;
637 tp->rcv_nxt++;
638 }
639 switch (tp->t_state) {
640
641 /*
642 * In SYN_RECEIVED and ESTABLISHED STATES
643 * enter the CLOSE_WAIT state.
644 */
645 case TCPS_SYN_RECEIVED:
646 case TCPS_ESTABLISHED:
647 tp->t_state = TCPS_CLOSE_WAIT;
648 break;
649
650 /*
651 * If still in FIN_WAIT_1 STATE FIN has not been acked so
652 * enter the CLOSING state.
653 */
654 case TCPS_FIN_WAIT_1:
655 tp->t_state = TCPS_CLOSING;
656 break;
657
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:
664 tp->t_state = TCPS_TIME_WAIT;
665 tcp_canceltimers(tp);
666 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
667 soisdisconnected(so);
668 break;
669
670 /*
671 * In TIME_WAIT state restart the 2 MSL time_wait timer.
672 */
673 case TCPS_TIME_WAIT:
674 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
675 break;
676 }
677 }
678 if (so->so_options & SO_DEBUG)
679 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
680
681 /*
682 * Return any desired output.
683 */
684 (void) tcp_output(tp);
685 return;
686
687dropafterack:
688 /*
689 * Generate an ACK dropping incoming segment if it occupies
690 * sequence space, where the ACK reflects our state.
691 */
692 if ((tiflags&TH_RST) ||
693 tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
694 goto drop;
695 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
696 tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
697 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
698 return;
699
700dropwithreset:
701 if (om)
702 (void) m_free(om);
703 /*
704 * Generate a RST, dropping incoming segment.
705 * Make ACK acceptable to originator of segment.
706 */
707 if (tiflags & TH_RST)
708 goto drop;
709 if (tiflags & TH_ACK)
710 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
711 else {
712 if (tiflags & TH_SYN)
713 ti->ti_len++;
714 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
715 TH_RST|TH_ACK);
716 }
717 return;
718
719drop:
720 /*
721 * Drop space held by incoming segment and return.
722 */
723 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
724 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
725 m_freem(m);
726 return;
727}
728
729tcp_dooptions(tp, om)
730 struct tcpcb *tp;
731 struct mbuf *om;
732{
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);
755 tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
756 break;
757 }
758 }
759 (void) m_free(om);
760}
761
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;
773 int cnt = ti->ti_urp - 1;
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;
783 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
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
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 */
800tcp_reass(tp, ti)
801 register struct tcpcb *tp;
802 register struct tcpiphdr *ti;
803{
804 register struct tcpiphdr *q;
805 struct socket *so = tp->t_inpcb->inp_socket;
806 struct mbuf *m;
807 int flags;
808
809 /*
810 * Call with ti==0 after become established to
811 * force pre-ESTABLISHED data up to user socket.
812 */
813 if (ti == 0)
814 goto present;
815
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;
831 q = (struct tcpiphdr *)q->ti_prev;
832 /* conversion to int (in i) handles seq wraparound */
833 i = q->ti_seq + q->ti_len - ti->ti_seq;
834 if (i > 0) {
835 if (i >= ti->ti_len)
836 goto drop;
837 m_adj(dtom(ti), i);
838 ti->ti_len -= i;
839 ti->ti_seq += i;
840 }
841 q = (struct tcpiphdr *)(q->ti_next);
842 }
843
844 /*
845 * While we overlap succeeding segments trim them or,
846 * if they are completely covered, dequeue them.
847 */
848 while (q != (struct tcpiphdr *)tp) {
849 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
850 if (i <= 0)
851 break;
852 if (i < q->ti_len) {
853 q->ti_seq += i;
854 q->ti_len -= i;
855 m_adj(dtom(q), i);
856 break;
857 }
858 q = (struct tcpiphdr *)q->ti_next;
859 m = dtom(q->ti_prev);
860 remque(q->ti_prev);
861 m_freem(m);
862 }
863
864 /*
865 * Stick new segment in its place.
866 */
867 insque(ti, q->ti_prev);
868
869present:
870 /*
871 * Present data to user, advancing rcv_nxt through
872 * completed sequence space.
873 */
874 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
875 return (0);
876 ti = tp->seg_next;
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 {
882 tp->rcv_nxt += ti->ti_len;
883 flags = ti->ti_flags & TH_FIN;
884 remque(ti);
885 m = dtom(ti);
886 ti = (struct tcpiphdr *)ti->ti_next;
887 if (so->so_state & SS_CANTRCVMORE)
888 m_freem(m);
889 else
890 sbappend(&so->so_rcv, m);
891 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
892 sorwakeup(so);
893 return (flags);
894drop:
895 m_freem(dtom(ti));
896 return (0);
897}