1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
34 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.54.2.14 1999/09/05 08:18:40 peter Exp $
35 */
36
37 #include "opt_tcpdebug.h"
38
39 #ifndef TUBA_INCLUDE
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52
53 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
54
55 #include <net/if.h>
56 #include <net/route.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_seq.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcpip.h>
69 #ifdef TCPDEBUG
70 #include <netinet/tcp_debug.h>
71 static struct tcpiphdr tcp_saveti;
72 #endif
73
74 static int tcprexmtthresh = 3;
75 tcp_seq tcp_iss;
76 tcp_cc tcp_ccgen;
77
78 struct tcpstat tcpstat;
79 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats,
80 CTLFLAG_RD, &tcpstat , tcpstat, "");
81
82 static int log_in_vain = 0;
83 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
84 &log_in_vain, 0, "");
85
86 static int no_local_slowstart = 0;
87 SYSCTL_INT(_net_inet_tcp, OID_AUTO, no_local_slowstart, CTLFLAG_RW,
88 &no_local_slowstart, 0, "");
89
90 u_long tcp_now;
91 struct inpcbhead tcb;
92 struct inpcbinfo tcbinfo;
93
94 static void tcp_dooptions __P((struct tcpcb *,
95 u_char *, int, struct tcpiphdr *, struct tcpopt *));
96 static void tcp_pulloutofband __P((struct socket *,
97 struct tcpiphdr *, struct mbuf *));
98 static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *));
99 static void tcp_xmit_timer __P((struct tcpcb *, int));
100
101 #endif /* TUBA_INCLUDE */
102
103 /*
104 * Insert segment ti into reassembly queue of tcp with
105 * control block tp. Return TH_FIN if reassembly now includes
106 * a segment with FIN. The macro form does the common case inline
107 * (segment is the next to be received on an established connection,
108 * and the queue is empty), avoiding linkage into and removal
109 * from the queue and repetition of various conversions.
110 * Set DELACK for segments received in order, but ack immediately
111 * when segments are out of order (so fast retransmit can work).
112 */
113 #ifdef TCP_ACK_HACK
114 #define TCP_REASS(tp, ti, m, so, flags) { \
115 if ((ti)->ti_seq == (tp)->rcv_nxt && \
116 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
117 (tp)->t_state == TCPS_ESTABLISHED) { \
118 if (ti->ti_flags & TH_PUSH) \
119 tp->t_flags |= TF_ACKNOW; \
120 else \
121 tp->t_flags |= TF_DELACK; \
122 (tp)->rcv_nxt += (ti)->ti_len; \
123 flags = (ti)->ti_flags & TH_FIN; \
124 tcpstat.tcps_rcvpack++;\
125 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
126 sbappend(&(so)->so_rcv, (m)); \
127 sorwakeup(so); \
128 } else { \
129 (flags) = tcp_reass((tp), (ti), (m)); \
130 tp->t_flags |= TF_ACKNOW; \
131 } \
132 }
133 #else
134 #define TCP_REASS(tp, ti, m, so, flags) { \
135 if ((ti)->ti_seq == (tp)->rcv_nxt && \
136 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
137 (tp)->t_state == TCPS_ESTABLISHED) { \
138 tp->t_flags |= TF_DELACK; \
139 (tp)->rcv_nxt += (ti)->ti_len; \
140 flags = (ti)->ti_flags & TH_FIN; \
141 tcpstat.tcps_rcvpack++;\
142 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
143 sbappend(&(so)->so_rcv, (m)); \
144 sorwakeup(so); \
145 } else { \
146 (flags) = tcp_reass((tp), (ti), (m)); \
147 tp->t_flags |= TF_ACKNOW; \
148 } \
149 }
150 #endif
151 #ifndef TUBA_INCLUDE
152
153 static int
154 tcp_reass(tp, ti, m)
155 register struct tcpcb *tp;
156 register struct tcpiphdr *ti;
157 struct mbuf *m;
158 {
159 register struct tcpiphdr *q;
160 struct socket *so = tp->t_inpcb->inp_socket;
161 int flags;
162
163 /*
164 * Call with ti==0 after become established to
165 * force pre-ESTABLISHED data up to user socket.
166 */
167 if (ti == 0)
168 goto present;
169
170 /*
171 * Find a segment which begins after this one does.
172 */
173 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
174 q = (struct tcpiphdr *)q->ti_next)
175 if (SEQ_GT(q->ti_seq, ti->ti_seq))
176 break;
177
178 /*
179 * If there is a preceding segment, it may provide some of
180 * our data already. If so, drop the data from the incoming
181 * segment. If it provides all of our data, drop us.
182 */
183 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
184 register int i;
185 q = (struct tcpiphdr *)q->ti_prev;
186 /* conversion to int (in i) handles seq wraparound */
187 i = q->ti_seq + q->ti_len - ti->ti_seq;
188 if (i > 0) {
189 if (i >= ti->ti_len) {
190 tcpstat.tcps_rcvduppack++;
191 tcpstat.tcps_rcvdupbyte += ti->ti_len;
192 m_freem(m);
193 /*
194 * Try to present any queued data
195 * at the left window edge to the user.
196 * This is needed after the 3-WHS
197 * completes.
198 */
199 goto present; /* ??? */
200 }
201 m_adj(m, i);
202 ti->ti_len -= i;
203 ti->ti_seq += i;
204 }
205 q = (struct tcpiphdr *)(q->ti_next);
206 }
207 tcpstat.tcps_rcvoopack++;
208 tcpstat.tcps_rcvoobyte += ti->ti_len;
209 REASS_MBUF(ti) = m; /* XXX */
210
211 /*
212 * While we overlap succeeding segments trim them or,
213 * if they are completely covered, dequeue them.
214 */
215 while (q != (struct tcpiphdr *)tp) {
216 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
217 if (i <= 0)
218 break;
219 if (i < q->ti_len) {
220 q->ti_seq += i;
221 q->ti_len -= i;
222 m_adj(REASS_MBUF(q), i);
223 break;
224 }
225 q = (struct tcpiphdr *)q->ti_next;
226 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
227 remque(q->ti_prev);
228 m_freem(m);
229 }
230
231 /*
232 * Stick new segment in its place.
233 */
234 insque(ti, q->ti_prev);
235
236 present:
237 /*
238 * Present data to user, advancing rcv_nxt through
239 * completed sequence space.
240 */
241 if (!TCPS_HAVEESTABLISHED(tp->t_state))
242 return (0);
243 ti = tp->seg_next;
244 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
245 return (0);
246 do {
247 tp->rcv_nxt += ti->ti_len;
248 flags = ti->ti_flags & TH_FIN;
249 remque(ti);
250 m = REASS_MBUF(ti);
251 ti = (struct tcpiphdr *)ti->ti_next;
252 if (so->so_state & SS_CANTRCVMORE)
253 m_freem(m);
254 else
255 sbappend(&so->so_rcv, m);
256 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
257 sorwakeup(so);
258 return (flags);
259 }
260
261 /*
262 * TCP input routine, follows pages 65-76 of the
263 * protocol specification dated September, 1981 very closely.
264 */
265 void
266 tcp_input(m, iphlen)
267 register struct mbuf *m;
268 int iphlen;
269 {
270 register struct tcpiphdr *ti;
271 register struct inpcb *inp;
272 u_char *optp = NULL;
273 int optlen = 0;
274 int len, tlen, off;
275 register struct tcpcb *tp = 0;
276 register int tiflags;
277 struct socket *so = 0;
278 int todrop, acked, ourfinisacked, needoutput = 0;
279 struct in_addr laddr;
280 int dropsocket = 0;
281 int iss = 0;
282 u_long tiwin;
283 struct tcpopt to; /* options in this segment */
284 struct rmxp_tao *taop; /* pointer to our TAO cache entry */
285 struct rmxp_tao tao_noncached; /* in case there's no cached entry */
286 #ifdef TCPDEBUG
287 short ostate = 0;
288 #endif
289
290 bzero((char *)&to, sizeof(to));
291
292 tcpstat.tcps_rcvtotal++;
293 /*
294 * Get IP and TCP header together in first mbuf.
295 * Note: IP leaves IP header in first mbuf.
296 */
297 ti = mtod(m, struct tcpiphdr *);
298 if (iphlen > sizeof (struct ip))
299 ip_stripoptions(m, (struct mbuf *)0);
300 if (m->m_len < sizeof (struct tcpiphdr)) {
301 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
302 tcpstat.tcps_rcvshort++;
303 return;
304 }
305 ti = mtod(m, struct tcpiphdr *);
306 }
307
308 /*
309 * Checksum extended TCP header and data.
310 */
311 tlen = ((struct ip *)ti)->ip_len;
312 len = sizeof (struct ip) + tlen;
313 ti->ti_next = ti->ti_prev = 0;
314 ti->ti_x1 = 0;
315 ti->ti_len = (u_short)tlen;
316 HTONS(ti->ti_len);
317 ti->ti_sum = in_cksum(m, len);
318 if (ti->ti_sum) {
319 tcpstat.tcps_rcvbadsum++;
320 goto drop;
321 }
322 #endif /* TUBA_INCLUDE */
323
324 /*
325 * Check that TCP offset makes sense,
326 * pull out TCP options and adjust length. XXX
327 */
328 off = ti->ti_off << 2;
329 if (off < sizeof (struct tcphdr) || off > tlen) {
330 tcpstat.tcps_rcvbadoff++;
331 goto drop;
332 }
333 tlen -= off;
334 ti->ti_len = tlen;
335 if (off > sizeof (struct tcphdr)) {
336 if (m->m_len < sizeof(struct ip) + off) {
337 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
338 tcpstat.tcps_rcvshort++;
339 return;
340 }
341 ti = mtod(m, struct tcpiphdr *);
342 }
343 optlen = off - sizeof (struct tcphdr);
344 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
345 }
346 tiflags = ti->ti_flags;
347
348 /*
349 * Convert TCP protocol specific fields to host format.
350 */
351 NTOHL(ti->ti_seq);
352 NTOHL(ti->ti_ack);
353 NTOHS(ti->ti_win);
354 NTOHS(ti->ti_urp);
355
356 /*
357 * Drop TCP, IP headers and TCP options.
358 */
359 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
360 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
361
362 /*
363 * Locate pcb for segment.
364 */
365 findpcb:
366 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport,
367 ti->ti_dst, ti->ti_dport, 1);
368
369 /*
370 * If the state is CLOSED (i.e., TCB does not exist) then
371 * all data in the incoming segment is discarded.
372 * If the TCB exists but is in CLOSED state, it is embryonic,
373 * but should either do a listen or a connect soon.
374 */
375 if (inp == NULL) {
376 if (log_in_vain && tiflags & TH_SYN) {
377 char buf[4*sizeof "123"];
378
379 strcpy(buf, inet_ntoa(ti->ti_dst));
380 log(LOG_INFO, "Connection attempt to TCP %s:%d"
381 " from %s:%d\n",
382 buf, ntohs(ti->ti_dport),
383 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
384 }
385 goto dropwithreset;
386 }
387 tp = intotcpcb(inp);
388 if (tp == 0)
389 goto dropwithreset;
390 if (tp->t_state == TCPS_CLOSED)
391 goto drop;
392
393 /* Unscale the window into a 32-bit value. */
394 if ((tiflags & TH_SYN) == 0)
395 tiwin = ti->ti_win << tp->snd_scale;
396 else
397 tiwin = ti->ti_win;
398
399 so = inp->inp_socket;
400 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
401 #ifdef TCPDEBUG
402 if (so->so_options & SO_DEBUG) {
403 ostate = tp->t_state;
404 tcp_saveti = *ti;
405 }
406 #endif
407 if (so->so_options & SO_ACCEPTCONN) {
408 register struct tcpcb *tp0 = tp;
409 struct socket *so2;
410 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
411 /*
412 * Note: dropwithreset makes sure we don't
413 * send a RST in response to a RST.
414 */
415 if (tiflags & TH_ACK) {
416 tcpstat.tcps_badsyn++;
417 goto dropwithreset;
418 }
419 goto drop;
420 }
421 so2 = sonewconn(so, 0);
422 if (so2 == 0) {
423 tcpstat.tcps_listendrop++;
424 so2 = sodropablereq(so);
425 if (so2) {
426 tcp_drop(sototcpcb(so2), ETIMEDOUT);
427 so2 = sonewconn(so, 0);
428 }
429 if (!so2)
430 goto drop;
431 }
432 so = so2;
433 /*
434 * This is ugly, but ....
435 *
436 * Mark socket as temporary until we're
437 * committed to keeping it. The code at
438 * ``drop'' and ``dropwithreset'' check the
439 * flag dropsocket to see if the temporary
440 * socket created here should be discarded.
441 * We mark the socket as discardable until
442 * we're committed to it below in TCPS_LISTEN.
443 */
444 dropsocket++;
445 inp = (struct inpcb *)so->so_pcb;
446 inp->inp_laddr = ti->ti_dst;
447 inp->inp_lport = ti->ti_dport;
448 in_pcbrehash(inp);
449 #if BSD>=43
450 inp->inp_options = ip_srcroute();
451 #endif
452 tp = intotcpcb(inp);
453 tp->t_state = TCPS_LISTEN;
454 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
455
456 /* Compute proper scaling value from buffer space */
457 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
458 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
459 tp->request_r_scale++;
460 }
461 }
462
463 /*
464 * Segment received on connection.
465 * Reset idle time and keep-alive timer.
466 */
467 tp->t_idle = 0;
468 if (TCPS_HAVEESTABLISHED(tp->t_state))
469 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
470
471 /*
472 * Process options if not in LISTEN state,
473 * else do it below (after getting remote address).
474 */
475 if (tp->t_state != TCPS_LISTEN)
476 tcp_dooptions(tp, optp, optlen, ti, &to);
477
478 /*
479 * Header prediction: check for the two common cases
480 * of a uni-directional data xfer. If the packet has
481 * no control flags, is in-sequence, the window didn't
482 * change and we're not retransmitting, it's a
483 * candidate. If the length is zero and the ack moved
484 * forward, we're the sender side of the xfer. Just
485 * free the data acked & wake any higher level process
486 * that was blocked waiting for space. If the length
487 * is non-zero and the ack didn't move, we're the
488 * receiver side. If we're getting packets in-order
489 * (the reassembly queue is empty), add the data to
490 * the socket buffer and note that we need a delayed ack.
491 * Make sure that the hidden state-flags are also off.
492 * Since we check for TCPS_ESTABLISHED above, it can only
493 * be TH_NEEDSYN.
494 */
495 if (tp->t_state == TCPS_ESTABLISHED &&
496 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
497 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
498 ((to.to_flag & TOF_TS) == 0 ||
499 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
500 /*
501 * Using the CC option is compulsory if once started:
502 * the segment is OK if no T/TCP was negotiated or
503 * if the segment has a CC option equal to CCrecv
504 */
505 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
506 (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) &&
507 ti->ti_seq == tp->rcv_nxt &&
508 tiwin && tiwin == tp->snd_wnd &&
509 tp->snd_nxt == tp->snd_max) {
510
511 /*
512 * If last ACK falls within this segment's sequence numbers,
513 * record the timestamp.
514 * NOTE that the test is modified according to the latest
515 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
516 */
517 if ((to.to_flag & TOF_TS) != 0 &&
518 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
519 tp->ts_recent_age = tcp_now;
520 tp->ts_recent = to.to_tsval;
521 }
522
523 if (ti->ti_len == 0) {
524 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
525 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
526 tp->snd_cwnd >= tp->snd_wnd &&
527 tp->t_dupacks < tcprexmtthresh) {
528 /*
529 * this is a pure ack for outstanding data.
530 */
531 ++tcpstat.tcps_predack;
532 if ((to.to_flag & TOF_TS) != 0)
533 tcp_xmit_timer(tp,
534 tcp_now - to.to_tsecr + 1);
535 else if (tp->t_rtt &&
536 SEQ_GT(ti->ti_ack, tp->t_rtseq))
537 tcp_xmit_timer(tp, tp->t_rtt);
538 acked = ti->ti_ack - tp->snd_una;
539 tcpstat.tcps_rcvackpack++;
540 tcpstat.tcps_rcvackbyte += acked;
541 sbdrop(&so->so_snd, acked);
542 tp->snd_una = ti->ti_ack;
543 m_freem(m);
544
545 /*
546 * If all outstanding data are acked, stop
547 * retransmit timer, otherwise restart timer
548 * using current (possibly backed-off) value.
549 * If process is waiting for space,
550 * wakeup/selwakeup/signal. If data
551 * are ready to send, let tcp_output
552 * decide between more output or persist.
553 */
554 if (tp->snd_una == tp->snd_max)
555 tp->t_timer[TCPT_REXMT] = 0;
556 else if (tp->t_timer[TCPT_PERSIST] == 0)
557 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
558
559 if (so->so_snd.sb_flags & SB_NOTIFY)
560 sowwakeup(so);
561 if (so->so_snd.sb_cc)
562 (void) tcp_output(tp);
563 return;
564 }
565 } else if (ti->ti_ack == tp->snd_una &&
566 tp->seg_next == (struct tcpiphdr *)tp &&
567 ti->ti_len <= sbspace(&so->so_rcv)) {
568 /*
569 * this is a pure, in-sequence data packet
570 * with nothing on the reassembly queue and
571 * we have enough buffer space to take it.
572 */
573 ++tcpstat.tcps_preddat;
574 tp->rcv_nxt += ti->ti_len;
575 tcpstat.tcps_rcvpack++;
576 tcpstat.tcps_rcvbyte += ti->ti_len;
577 /*
578 * Add data to socket buffer.
579 */
580 sbappend(&so->so_rcv, m);
581 sorwakeup(so);
582 #ifdef TCP_ACK_HACK
583 /*
584 * If this is a short packet, then ACK now - with Nagel
585 * congestion avoidance sender won't send more until
586 * he gets an ACK.
587 */
588 if (tiflags & TH_PUSH) {
589 tp->t_flags |= TF_ACKNOW;
590 tcp_output(tp);
591 } else {
592 tp->t_flags |= TF_DELACK;
593 }
594 #else
595 tp->t_flags |= TF_DELACK;
596 #endif
597 return;
598 }
599 }
600
601 /*
602 * Calculate amount of space in receive window,
603 * and then do TCP input processing.
604 * Receive window is amount of space in rcv queue,
605 * but not less than advertised window.
606 */
607 { int win;
608
609 win = sbspace(&so->so_rcv);
610 if (win < 0)
611 win = 0;
612 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
613 }
614
615 switch (tp->t_state) {
616
617 /*
618 * If the state is LISTEN then ignore segment if it contains an RST.
619 * If the segment contains an ACK then it is bad and send a RST.
620 * If it does not contain a SYN then it is not interesting; drop it.
621 * If it is from this socket, drop it, it must be forged.
622 * Don't bother responding if the destination was a broadcast.
623 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
624 * tp->iss, and send a segment:
625 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
626 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
627 * Fill in remote peer address fields if not previously specified.
628 * Enter SYN_RECEIVED state, and process any other fields of this
629 * segment in this state.
630 */
631 case TCPS_LISTEN: {
632 struct mbuf *am;
633 register struct sockaddr_in *sin;
634
635 if (tiflags & TH_RST)
636 goto drop;
637 if (tiflags & TH_ACK)
638 goto dropwithreset;
639 if ((tiflags & TH_SYN) == 0)
640 goto drop;
641 if ((ti->ti_dport == ti->ti_sport) &&
642 (ti->ti_dst.s_addr == ti->ti_src.s_addr))
643 goto drop;
644 /*
645 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
646 * in_broadcast() should never return true on a received
647 * packet with M_BCAST not set.
648 */
649 if (m->m_flags & (M_BCAST|M_MCAST) ||
650 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
651 goto drop;
652 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
653 if (am == NULL)
654 goto drop;
655 am->m_len = sizeof (struct sockaddr_in);
656 sin = mtod(am, struct sockaddr_in *);
657 sin->sin_family = AF_INET;
658 sin->sin_len = sizeof(*sin);
659 sin->sin_addr = ti->ti_src;
660 sin->sin_port = ti->ti_sport;
661 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
662 laddr = inp->inp_laddr;
663 if (inp->inp_laddr.s_addr == INADDR_ANY)
664 inp->inp_laddr = ti->ti_dst;
665 if (in_pcbconnect(inp, am)) {
666 inp->inp_laddr = laddr;
667 (void) m_free(am);
668 goto drop;
669 }
670 (void) m_free(am);
671 tp->t_template = tcp_template(tp);
672 if (tp->t_template == 0) {
673 tp = tcp_drop(tp, ENOBUFS);
674 dropsocket = 0; /* socket is already gone */
675 goto drop;
676 }
677 if ((taop = tcp_gettaocache(inp)) == NULL) {
678 taop = &tao_noncached;
679 bzero(taop, sizeof(*taop));
680 }
681 tcp_dooptions(tp, optp, optlen, ti, &to);
682 if (iss)
683 tp->iss = iss;
684 else
685 tp->iss = tcp_iss;
686 tcp_iss += TCP_ISSINCR/4;
687 tp->irs = ti->ti_seq;
688 tcp_sendseqinit(tp);
689 tcp_rcvseqinit(tp);
690 /*
691 * Initialization of the tcpcb for transaction;
692 * set SND.WND = SEG.WND,
693 * initialize CCsend and CCrecv.
694 */
695 tp->snd_wnd = tiwin; /* initial send-window */
696 tp->cc_send = CC_INC(tcp_ccgen);
697 tp->cc_recv = to.to_cc;
698 /*
699 * Perform TAO test on incoming CC (SEG.CC) option, if any.
700 * - compare SEG.CC against cached CC from the same host,
701 * if any.
702 * - if SEG.CC > chached value, SYN must be new and is accepted
703 * immediately: save new CC in the cache, mark the socket
704 * connected, enter ESTABLISHED state, turn on flag to
705 * send a SYN in the next segment.
706 * A virtual advertised window is set in rcv_adv to
707 * initialize SWS prevention. Then enter normal segment
708 * processing: drop SYN, process data and FIN.
709 * - otherwise do a normal 3-way handshake.
710 */
711 if ((to.to_flag & TOF_CC) != 0) {
712 if (((tp->t_flags & TF_NOPUSH) != 0) &&
713 taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
714
715 taop->tao_cc = to.to_cc;
716 tp->t_state = TCPS_ESTABLISHED;
717
718 /*
719 * If there is a FIN, or if there is data and the
720 * connection is local, then delay SYN,ACK(SYN) in
721 * the hope of piggy-backing it on a response
722 * segment. Otherwise must send ACK now in case
723 * the other side is slow starting.
724 */
725 if ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
726 in_localaddr(inp->inp_faddr)))
727 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
728 else
729 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
730
731 /*
732 * Limit the `virtual advertised window' to TCP_MAXWIN
733 * here. Even if we requested window scaling, it will
734 * become effective only later when our SYN is acked.
735 */
736 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
737 tcpstat.tcps_connects++;
738 soisconnected(so);
739 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
740 dropsocket = 0; /* committed to socket */
741 tcpstat.tcps_accepts++;
742 goto trimthenstep6;
743 }
744 /* else do standard 3-way handshake */
745 } else {
746 /*
747 * No CC option, but maybe CC.NEW:
748 * invalidate cached value.
749 */
750 taop->tao_cc = 0;
751 }
752 /*
753 * TAO test failed or there was no CC option,
754 * do a standard 3-way handshake.
755 */
756 tp->t_flags |= TF_ACKNOW;
757 tp->t_state = TCPS_SYN_RECEIVED;
758 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
759 dropsocket = 0; /* committed to socket */
760 tcpstat.tcps_accepts++;
761 goto trimthenstep6;
762 }
763
764 /*
765 * If the state is SYN_RECEIVED:
766 * if seg contains SYN/ACK, send a RST.
767 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
768 */
769 case TCPS_SYN_RECEIVED:
770 if (tiflags & TH_ACK) {
771 if (tiflags & TH_SYN) {
772 tcpstat.tcps_badsyn++;
773 goto dropwithreset;
774 }
775 if (SEQ_LEQ(ti->ti_ack, tp->snd_una) ||
776 SEQ_GT(ti->ti_ack, tp->snd_max))
777 goto dropwithreset;
778 }
779 break;
780
781 /*
782 * If the state is SYN_SENT:
783 * if seg contains an ACK, but not for our SYN, drop the input.
784 * if seg contains a RST, then drop the connection.
785 * if seg does not contain SYN, then drop it.
786 * Otherwise this is an acceptable SYN segment
787 * initialize tp->rcv_nxt and tp->irs
788 * if seg contains ack then advance tp->snd_una
789 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
790 * arrange for segment to be acked (eventually)
791 * continue processing rest of data/controls, beginning with URG
792 */
793 case TCPS_SYN_SENT:
794 if ((taop = tcp_gettaocache(inp)) == NULL) {
795 taop = &tao_noncached;
796 bzero(taop, sizeof(*taop));
797 }
798
799 if ((tiflags & TH_ACK) &&
800 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
801 SEQ_GT(ti->ti_ack, tp->snd_max))) {
802 /*
803 * If we have a cached CCsent for the remote host,
804 * hence we haven't just crashed and restarted,
805 * do not send a RST. This may be a retransmission
806 * from the other side after our earlier ACK was lost.
807 * Our new SYN, when it arrives, will serve as the
808 * needed ACK.
809 */
810 if (taop->tao_ccsent != 0)
811 goto drop;
812 else
813 goto dropwithreset;
814 }
815 if (tiflags & TH_RST) {
816 if (tiflags & TH_ACK)
817 tp = tcp_drop(tp, ECONNREFUSED);
818 goto drop;
819 }
820 if ((tiflags & TH_SYN) == 0)
821 goto drop;
822 tp->snd_wnd = ti->ti_win; /* initial send window */
823 tp->cc_recv = to.to_cc; /* foreign CC */
824
825 tp->irs = ti->ti_seq;
826 tcp_rcvseqinit(tp);
827 if (tiflags & TH_ACK) {
828 /*
829 * Our SYN was acked. If segment contains CC.ECHO
830 * option, check it to make sure this segment really
831 * matches our SYN. If not, just drop it as old
832 * duplicate, but send an RST if we're still playing
833 * by the old rules. If no CC.ECHO option, make sure
834 * we don't get fooled into using T/TCP.
835 */
836 if (to.to_flag & TOF_CCECHO) {
837 if (tp->cc_send != to.to_ccecho)
838 if (taop->tao_ccsent != 0)
839 goto drop;
840 else
841 goto dropwithreset;
842 } else
843 tp->t_flags &= ~TF_RCVD_CC;
844 tcpstat.tcps_connects++;
845 soisconnected(so);
846 /* Do window scaling on this connection? */
847 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
848 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
849 tp->snd_scale = tp->requested_s_scale;
850 tp->rcv_scale = tp->request_r_scale;
851 }
852 /* Segment is acceptable, update cache if undefined. */
853 if (taop->tao_ccsent == 0)
854 taop->tao_ccsent = to.to_ccecho;
855
856 tp->rcv_adv += tp->rcv_wnd;
857 tp->snd_una++; /* SYN is acked */
858 /*
859 * If there's data, delay ACK; if there's also a FIN
860 * ACKNOW will be turned on later.
861 */
862 if (ti->ti_len != 0)
863 tp->t_flags |= TF_DELACK;
864 else
865 tp->t_flags |= TF_ACKNOW;
866 /*
867 * Received <SYN,ACK> in SYN_SENT[*] state.
868 * Transitions:
869 * SYN_SENT --> ESTABLISHED
870 * SYN_SENT* --> FIN_WAIT_1
871 */
872 if (tp->t_flags & TF_NEEDFIN) {
873 tp->t_state = TCPS_FIN_WAIT_1;
874 tp->t_flags &= ~TF_NEEDFIN;
875 tiflags &= ~TH_SYN;
876 } else {
877 tp->t_state = TCPS_ESTABLISHED;
878 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
879 }
880 } else {
881 /*
882 * Received initial SYN in SYN-SENT[*] state => simul-
883 * taneous open. If segment contains CC option and there is
884 * a cached CC, apply TAO test; if it succeeds, connection is
885 * half-synchronized. Otherwise, do 3-way handshake:
886 * SYN-SENT -> SYN-RECEIVED
887 * SYN-SENT* -> SYN-RECEIVED*
888 * If there was no CC option, clear cached CC value.
889 */
890 tp->t_flags |= TF_ACKNOW;
891 tp->t_timer[TCPT_REXMT] = 0;
892 if (to.to_flag & TOF_CC) {
893 if (taop->tao_cc != 0 &&
894 CC_GT(to.to_cc, taop->tao_cc)) {
895 /*
896 * update cache and make transition:
897 * SYN-SENT -> ESTABLISHED*
898 * SYN-SENT* -> FIN-WAIT-1*
899 */
900 taop->tao_cc = to.to_cc;
901 if (tp->t_flags & TF_NEEDFIN) {
902 tp->t_state = TCPS_FIN_WAIT_1;
903 tp->t_flags &= ~TF_NEEDFIN;
904 } else {
905 tp->t_state = TCPS_ESTABLISHED;
906 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
907 }
908 tp->t_flags |= TF_NEEDSYN;
909 } else
910 tp->t_state = TCPS_SYN_RECEIVED;
911 } else {
912 /* CC.NEW or no option => invalidate cache */
913 taop->tao_cc = 0;
914 tp->t_state = TCPS_SYN_RECEIVED;
915 }
916 }
917
918 trimthenstep6:
919 /*
920 * Advance ti->ti_seq to correspond to first data byte.
921 * If data, trim to stay within window,
922 * dropping FIN if necessary.
923 */
924 ti->ti_seq++;
925 if (ti->ti_len > tp->rcv_wnd) {
926 todrop = ti->ti_len - tp->rcv_wnd;
927 m_adj(m, -todrop);
928 ti->ti_len = tp->rcv_wnd;
929 tiflags &= ~TH_FIN;
930 tcpstat.tcps_rcvpackafterwin++;
931 tcpstat.tcps_rcvbyteafterwin += todrop;
932 }
933 tp->snd_wl1 = ti->ti_seq - 1;
934 tp->rcv_up = ti->ti_seq;
935 /*
936 * Client side of transaction: already sent SYN and data.
937 * If the remote host used T/TCP to validate the SYN,
938 * our data will be ACK'd; if so, enter normal data segment
939 * processing in the middle of step 5, ack processing.
940 * Otherwise, goto step 6.
941 */
942 if (tiflags & TH_ACK)
943 goto process_ACK;
944 goto step6;
945 /*
946 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
947 * if segment contains a SYN and CC [not CC.NEW] option:
948 * if state == TIME_WAIT and connection duration > MSL,
949 * drop packet and send RST;
950 *
951 * if SEG.CC > CCrecv then is new SYN, and can implicitly
952 * ack the FIN (and data) in retransmission queue.
953 * Complete close and delete TCPCB. Then reprocess
954 * segment, hoping to find new TCPCB in LISTEN state;
955 *
956 * else must be old SYN; drop it.
957 * else do normal processing.
958 */
959 case TCPS_LAST_ACK:
960 case TCPS_CLOSING:
961 case TCPS_TIME_WAIT:
962 if ((tiflags & TH_SYN) &&
963 (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
964 if (tp->t_state == TCPS_TIME_WAIT &&
965 tp->t_duration > TCPTV_MSL)
966 goto dropwithreset;
967 if (CC_GT(to.to_cc, tp->cc_recv)) {
968 tp = tcp_close(tp);
969 goto findpcb;
970 }
971 else
972 goto drop;
973 }
974 break; /* continue normal processing */
975 }
976
977 /*
978 * States other than LISTEN or SYN_SENT.
979 * First check the RST flag and sequence number since reset segments
980 * are exempt from the timestamp and connection count tests. This
981 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
982 * below which allowed reset segments in half the sequence space
983 * to fall though and be processed (which gives forged reset
984 * segments with a random sequence number a 50 percent chance of
985 * killing a connection).
986 * Then check timestamp, if present.
987 * Then check the connection count, if present.
988 * Then check that at least some bytes of segment are within
989 * receive window. If segment begins before rcv_nxt,
990 * drop leading data (and SYN); if nothing left, just ack.
991 *
992 *
993 * If the RST bit is set, check the sequence number to see
994 * if this is a valid reset segment.
995 * RFC 793 page 37:
996 * In all states except SYN-SENT, all reset (RST) segments
997 * are validated by checking their SEQ-fields. A reset is
998 * valid if its sequence number is in the window.
999 * Note: this does not take into account delayed ACKs, so
1000 * we should test against last_ack_sent instead of rcv_nxt.
1001 * Also, it does not make sense to allow reset segments with
1002 * sequence numbers greater than last_ack_sent to be processed
1003 * since these sequence numbers are just the acknowledgement
1004 * numbers in our outgoing packets being echoed back at us,
1005 * and these acknowledgement numbers are monotonically
1006 * increasing.
1007 * If we have multiple segments in flight, the intial reset
1008 * segment sequence numbers will be to the left of last_ack_sent,
1009 * but they will eventually catch up.
1010 * In any case, it never made sense to trim reset segments to
1011 * fit the receive window since RFC 1122 says:
1012 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1013 *
1014 * A TCP SHOULD allow a received RST segment to include data.
1015 *
1016 * DISCUSSION
1017 * It has been suggested that a RST segment could contain
1018 * ASCII text that encoded and explained the cause of the
1019 * RST. No standard has yet been established for such
1020 * data.
1021 *
1022 * If the reset segment passes the sequence number test examine
1023 * the state:
1024 * SYN_RECEIVED STATE:
1025 * If passive open, return to LISTEN state.
1026 * If active open, inform user that connection was refused.
1027 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1028 * Inform user that connection was reset, and close tcb.
1029 * CLOSING, LAST_ACK, TIME_WAIT STATES
1030 * Close the tcb.
1031 * TIME_WAIT state:
1032 * Drop the segment - see Stevens, vol. 2, p. 964 and
1033 * RFC 1337.
1034 */
1035 if (tiflags & TH_RST) {
1036 if (tp->last_ack_sent == ti->ti_seq) {
1037 switch (tp->t_state) {
1038
1039 case TCPS_SYN_RECEIVED:
1040 so->so_error = ECONNREFUSED;
1041 goto close;
1042
1043 case TCPS_ESTABLISHED:
1044 case TCPS_FIN_WAIT_1:
1045 case TCPS_FIN_WAIT_2:
1046 case TCPS_CLOSE_WAIT:
1047 so->so_error = ECONNRESET;
1048 close:
1049 tp->t_state = TCPS_CLOSED;
1050 tcpstat.tcps_drops++;
1051 tp = tcp_close(tp);
1052 break;
1053
1054 case TCPS_CLOSING:
1055 case TCPS_LAST_ACK:
1056 tp = tcp_close(tp);
1057 break;
1058
1059 case TCPS_TIME_WAIT:
1060 break;
1061 }
1062 }
1063 goto drop;
1064 }
1065
1066 /*
1067 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1068 * and it's less than ts_recent, drop it.
1069 */
1070 if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
1071 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1072
1073 /* Check to see if ts_recent is over 24 days old. */
1074 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1075 /*
1076 * Invalidate ts_recent. If this segment updates
1077 * ts_recent, the age will be reset later and ts_recent
1078 * will get a valid value. If it does not, setting
1079 * ts_recent to zero will at least satisfy the
1080 * requirement that zero be placed in the timestamp
1081 * echo reply when ts_recent isn't valid. The
1082 * age isn't reset until we get a valid ts_recent
1083 * because we don't want out-of-order segments to be
1084 * dropped when ts_recent is old.
1085 */
1086 tp->ts_recent = 0;
1087 } else {
1088 tcpstat.tcps_rcvduppack++;
1089 tcpstat.tcps_rcvdupbyte += ti->ti_len;
1090 tcpstat.tcps_pawsdrop++;
1091 goto dropafterack;
1092 }
1093 }
1094
1095 /*
1096 * T/TCP mechanism
1097 * If T/TCP was negotiated and the segment doesn't have CC,
1098 * or if it's CC is wrong then drop the segment.
1099 * RST segments do not have to comply with this.
1100 */
1101 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1102 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1103 goto dropafterack;
1104
1105 /*
1106 * In the SYN-RECEIVED state, validate that the packet belongs to
1107 * this connection before trimming the data to fit the receive
1108 * window. Check the sequence number versus IRS since we know
1109 * the sequence numbers haven't wrapped. This is a partial fix
1110 * for the "LAND" DoS attack.
1111 */
1112 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(ti->ti_seq, tp->irs))
1113 goto dropwithreset;
1114
1115 todrop = tp->rcv_nxt - ti->ti_seq;
1116 if (todrop > 0) {
1117 if (tiflags & TH_SYN) {
1118 tiflags &= ~TH_SYN;
1119 ti->ti_seq++;
1120 if (ti->ti_urp > 1)
1121 ti->ti_urp--;
1122 else
1123 tiflags &= ~TH_URG;
1124 todrop--;
1125 }
1126 /*
1127 * Following if statement from Stevens, vol. 2, p. 960.
1128 */
1129 if (todrop > ti->ti_len
1130 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
1131 /*
1132 * Any valid FIN must be to the left of the window.
1133 * At this point the FIN must be a duplicate or out
1134 * of sequence; drop it.
1135 */
1136 tiflags &= ~TH_FIN;
1137
1138 /*
1139 * Send an ACK to resynchronize and drop any data.
1140 * But keep on processing for RST or ACK.
1141 */
1142 tp->t_flags |= TF_ACKNOW;
1143 todrop = ti->ti_len;
1144 tcpstat.tcps_rcvduppack++;
1145 tcpstat.tcps_rcvdupbyte += todrop;
1146 } else {
1147 tcpstat.tcps_rcvpartduppack++;
1148 tcpstat.tcps_rcvpartdupbyte += todrop;
1149 }
1150 m_adj(m, todrop);
1151 ti->ti_seq += todrop;
1152 ti->ti_len -= todrop;
1153 if (ti->ti_urp > todrop)
1154 ti->ti_urp -= todrop;
1155 else {
1156 tiflags &= ~TH_URG;
1157 ti->ti_urp = 0;
1158 }
1159 }
1160
1161 /*
1162 * If new data are received on a connection after the
1163 * user processes are gone, then RST the other end.
1164 */
1165 if ((so->so_state & SS_NOFDREF) &&
1166 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1167 tp = tcp_close(tp);
1168 tcpstat.tcps_rcvafterclose++;
1169 goto dropwithreset;
1170 }
1171
1172 /*
1173 * If segment ends after window, drop trailing data
1174 * (and PUSH and FIN); if nothing left, just ACK.
1175 */
1176 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1177 if (todrop > 0) {
1178 tcpstat.tcps_rcvpackafterwin++;
1179 if (todrop >= ti->ti_len) {
1180 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1181 /*
1182 * If a new connection request is received
1183 * while in TIME_WAIT, drop the old connection
1184 * and start over if the sequence numbers
1185 * are above the previous ones.
1186 */
1187 if (tiflags & TH_SYN &&
1188 tp->t_state == TCPS_TIME_WAIT &&
1189 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1190 iss = tp->rcv_nxt + TCP_ISSINCR;
1191 tp = tcp_close(tp);
1192 goto findpcb;
1193 }
1194 /*
1195 * If window is closed can only take segments at
1196 * window edge, and have to drop data and PUSH from
1197 * incoming segments. Continue processing, but
1198 * remember to ack. Otherwise, drop segment
1199 * and ack.
1200 */
1201 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1202 tp->t_flags |= TF_ACKNOW;
1203 tcpstat.tcps_rcvwinprobe++;
1204 } else
1205 goto dropafterack;
1206 } else
1207 tcpstat.tcps_rcvbyteafterwin += todrop;
1208 m_adj(m, -todrop);
1209 ti->ti_len -= todrop;
1210 tiflags &= ~(TH_PUSH|TH_FIN);
1211 }
1212
1213 /*
1214 * If last ACK falls within this segment's sequence numbers,
1215 * record its timestamp.
1216 * NOTE that the test is modified according to the latest
1217 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1218 */
1219 if ((to.to_flag & TOF_TS) != 0 &&
1220 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1221 tp->ts_recent_age = tcp_now;
1222 tp->ts_recent = to.to_tsval;
1223 }
1224
1225 /*
1226 * If a SYN is in the window, then this is an
1227 * error and we send an RST and drop the connection.
1228 */
1229 if (tiflags & TH_SYN) {
1230 tp = tcp_drop(tp, ECONNRESET);
1231 goto dropwithreset;
1232 }
1233
1234 /*
1235 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1236 * flag is on (half-synchronized state), then queue data for
1237 * later processing; else drop segment and return.
1238 */
1239 if ((tiflags & TH_ACK) == 0) {
1240 if (tp->t_state == TCPS_SYN_RECEIVED ||
1241 (tp->t_flags & TF_NEEDSYN))
1242 goto step6;
1243 else
1244 goto drop;
1245 }
1246
1247 /*
1248 * Ack processing.
1249 */
1250 switch (tp->t_state) {
1251
1252 /*
1253 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1254 * ESTABLISHED state and continue processing.
1255 * The ACK was checked above.
1256 */
1257 case TCPS_SYN_RECEIVED:
1258
1259 tcpstat.tcps_connects++;
1260 soisconnected(so);
1261 /* Do window scaling? */
1262 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1263 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1264 tp->snd_scale = tp->requested_s_scale;
1265 tp->rcv_scale = tp->request_r_scale;
1266 }
1267 /*
1268 * Upon successful completion of 3-way handshake,
1269 * update cache.CC if it was undefined, pass any queued
1270 * data to the user, and advance state appropriately.
1271 */
1272 if ((taop = tcp_gettaocache(inp)) != NULL &&
1273 taop->tao_cc == 0)
1274 taop->tao_cc = tp->cc_recv;
1275
1276 /*
1277 * Make transitions:
1278 * SYN-RECEIVED -> ESTABLISHED
1279 * SYN-RECEIVED* -> FIN-WAIT-1
1280 */
1281 if (tp->t_flags & TF_NEEDFIN) {
1282 tp->t_state = TCPS_FIN_WAIT_1;
1283 tp->t_flags &= ~TF_NEEDFIN;
1284 } else {
1285 tp->t_state = TCPS_ESTABLISHED;
1286 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
1287 }
1288 /*
1289 * If segment contains data or ACK, will call tcp_reass()
1290 * later; if not, do so now to pass queued data to user.
1291 */
1292 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1293 (void) tcp_reass(tp, (struct tcpiphdr *)0,
1294 (struct mbuf *)0);
1295 tp->snd_wl1 = ti->ti_seq - 1;
1296 /* fall into ... */
1297
1298 /*
1299 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1300 * ACKs. If the ack is in the range
1301 * tp->snd_una < ti->ti_ack <= tp->snd_max
1302 * then advance tp->snd_una to ti->ti_ack and drop
1303 * data from the retransmission queue. If this ACK reflects
1304 * more up to date window information we update our window information.
1305 */
1306 case TCPS_ESTABLISHED:
1307 case TCPS_FIN_WAIT_1:
1308 case TCPS_FIN_WAIT_2:
1309 case TCPS_CLOSE_WAIT:
1310 case TCPS_CLOSING:
1311 case TCPS_LAST_ACK:
1312 case TCPS_TIME_WAIT:
1313
1314 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1315 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1316 tcpstat.tcps_rcvdupack++;
1317 /*
1318 * If we have outstanding data (other than
1319 * a window probe), this is a completely
1320 * duplicate ack (ie, window info didn't
1321 * change), the ack is the biggest we've
1322 * seen and we've seen exactly our rexmt
1323 * threshhold of them, assume a packet
1324 * has been dropped and retransmit it.
1325 * Kludge snd_nxt & the congestion
1326 * window so we send only this one
1327 * packet.
1328 *
1329 * We know we're losing at the current
1330 * window size so do congestion avoidance
1331 * (set ssthresh to half the current window
1332 * and pull our congestion window back to
1333 * the new ssthresh).
1334 *
1335 * Dup acks mean that packets have left the
1336 * network (they're now cached at the receiver)
1337 * so bump cwnd by the amount in the receiver
1338 * to keep a constant cwnd packets in the
1339 * network.
1340 */
1341 if (tp->t_timer[TCPT_REXMT] == 0 ||
1342 ti->ti_ack != tp->snd_una)
1343 tp->t_dupacks = 0;
1344 else if (++tp->t_dupacks == tcprexmtthresh) {
1345 tcp_seq onxt = tp->snd_nxt;
1346 u_int win =
1347 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1348 tp->t_maxseg;
1349
1350 if (win < 2)
1351 win = 2;
1352 tp->snd_ssthresh = win * tp->t_maxseg;
1353 tp->t_timer[TCPT_REXMT] = 0;
1354 tp->t_rtt = 0;
1355 tp->snd_nxt = ti->ti_ack;
1356 tp->snd_cwnd = tp->t_maxseg;
1357 (void) tcp_output(tp);
1358 tp->snd_cwnd = tp->snd_ssthresh +
1359 tp->t_maxseg * tp->t_dupacks;
1360 if (SEQ_GT(onxt, tp->snd_nxt))
1361 tp->snd_nxt = onxt;
1362 goto drop;
1363 } else if (tp->t_dupacks > tcprexmtthresh) {
1364 tp->snd_cwnd += tp->t_maxseg;
1365 (void) tcp_output(tp);
1366 goto drop;
1367 }
1368 } else
1369 tp->t_dupacks = 0;
1370 break;
1371 }
1372 /*
1373 * If the congestion window was inflated to account
1374 * for the other side's cached packets, retract it.
1375 */
1376 if (tp->t_dupacks >= tcprexmtthresh &&
1377 tp->snd_cwnd > tp->snd_ssthresh)
1378 tp->snd_cwnd = tp->snd_ssthresh;
1379 tp->t_dupacks = 0;
1380 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1381 tcpstat.tcps_rcvacktoomuch++;
1382 goto dropafterack;
1383 }
1384 /*
1385 * If we reach this point, ACK is not a duplicate,
1386 * i.e., it ACKs something we sent.
1387 */
1388 if (tp->t_flags & TF_NEEDSYN) {
1389 /*
1390 * T/TCP: Connection was half-synchronized, and our
1391 * SYN has been ACK'd (so connection is now fully
1392 * synchronized). Go to non-starred state,
1393 * increment snd_una for ACK of SYN, and check if
1394 * we can do window scaling.
1395 */
1396 tp->t_flags &= ~TF_NEEDSYN;
1397 tp->snd_una++;
1398 /* Do window scaling? */
1399 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1400 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1401 tp->snd_scale = tp->requested_s_scale;
1402 tp->rcv_scale = tp->request_r_scale;
1403 }
1404 }
1405
1406 process_ACK:
1407 acked = ti->ti_ack - tp->snd_una;
1408 tcpstat.tcps_rcvackpack++;
1409 tcpstat.tcps_rcvackbyte += acked;
1410
1411 /*
1412 * If we have a timestamp reply, update smoothed
1413 * round trip time. If no timestamp is present but
1414 * transmit timer is running and timed sequence
1415 * number was acked, update smoothed round trip time.
1416 * Since we now have an rtt measurement, cancel the
1417 * timer backoff (cf., Phil Karn's retransmit alg.).
1418 * Recompute the initial retransmit timer.
1419 */
1420 if (to.to_flag & TOF_TS)
1421 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1422 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1423 tcp_xmit_timer(tp,tp->t_rtt);
1424
1425 /*
1426 * If all outstanding data is acked, stop retransmit
1427 * timer and remember to restart (more output or persist).
1428 * If there is more data to be acked, restart retransmit
1429 * timer, using current (possibly backed-off) value.
1430 */
1431 if (ti->ti_ack == tp->snd_max) {
1432 tp->t_timer[TCPT_REXMT] = 0;
1433 needoutput = 1;
1434 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1435 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1436
1437 /*
1438 * If no data (only SYN) was ACK'd,
1439 * skip rest of ACK processing.
1440 */
1441 if (acked == 0)
1442 goto step6;
1443
1444 /*
1445 * When new data is acked, open the congestion window.
1446 * If the window gives us less than ssthresh packets
1447 * in flight, open exponentially (maxseg per packet).
1448 * Otherwise open linearly: maxseg per window
1449 * (maxseg^2 / cwnd per packet).
1450 */
1451 {
1452 register u_int cw = tp->snd_cwnd;
1453 register u_int incr = tp->t_maxseg;
1454
1455 if (cw > tp->snd_ssthresh)
1456 incr = incr * incr / cw;
1457 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1458 }
1459 if (acked > so->so_snd.sb_cc) {
1460 tp->snd_wnd -= so->so_snd.sb_cc;
1461 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1462 ourfinisacked = 1;
1463 } else {
1464 sbdrop(&so->so_snd, acked);
1465 tp->snd_wnd -= acked;
1466 ourfinisacked = 0;
1467 }
1468 if (so->so_snd.sb_flags & SB_NOTIFY)
1469 sowwakeup(so);
1470 tp->snd_una = ti->ti_ack;
1471 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1472 tp->snd_nxt = tp->snd_una;
1473
1474 switch (tp->t_state) {
1475
1476 /*
1477 * In FIN_WAIT_1 STATE in addition to the processing
1478 * for the ESTABLISHED state if our FIN is now acknowledged
1479 * then enter FIN_WAIT_2.
1480 */
1481 case TCPS_FIN_WAIT_1:
1482 if (ourfinisacked) {
1483 /*
1484 * If we can't receive any more
1485 * data, then closing user can proceed.
1486 * Starting the timer is contrary to the
1487 * specification, but if we don't get a FIN
1488 * we'll hang forever.
1489 */
1490 if (so->so_state & SS_CANTRCVMORE) {
1491 soisdisconnected(so);
1492 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1493 }
1494 tp->t_state = TCPS_FIN_WAIT_2;
1495 }
1496 break;
1497
1498 /*
1499 * In CLOSING STATE in addition to the processing for
1500 * the ESTABLISHED state if the ACK acknowledges our FIN
1501 * then enter the TIME-WAIT state, otherwise ignore
1502 * the segment.
1503 */
1504 case TCPS_CLOSING:
1505 if (ourfinisacked) {
1506 tp->t_state = TCPS_TIME_WAIT;
1507 tcp_canceltimers(tp);
1508 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1509 if (tp->cc_recv != 0 &&
1510 tp->t_duration < TCPTV_MSL)
1511 tp->t_timer[TCPT_2MSL] =
1512 tp->t_rxtcur * TCPTV_TWTRUNC;
1513 else
1514 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1515 soisdisconnected(so);
1516 }
1517 break;
1518
1519 /*
1520 * In LAST_ACK, we may still be waiting for data to drain
1521 * and/or to be acked, as well as for the ack of our FIN.
1522 * If our FIN is now acknowledged, delete the TCB,
1523 * enter the closed state and return.
1524 */
1525 case TCPS_LAST_ACK:
1526 if (ourfinisacked) {
1527 tp = tcp_close(tp);
1528 goto drop;
1529 }
1530 break;
1531
1532 /*
1533 * In TIME_WAIT state the only thing that should arrive
1534 * is a retransmission of the remote FIN. Acknowledge
1535 * it and restart the finack timer.
1536 */
1537 case TCPS_TIME_WAIT:
1538 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1539 goto dropafterack;
1540 }
1541 }
1542
1543 step6:
1544 /*
1545 * Update window information.
1546 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1547 */
1548 if ((tiflags & TH_ACK) &&
1549 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1550 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1551 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1552 /* keep track of pure window updates */
1553 if (ti->ti_len == 0 &&
1554 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1555 tcpstat.tcps_rcvwinupd++;
1556 tp->snd_wnd = tiwin;
1557 tp->snd_wl1 = ti->ti_seq;
1558 tp->snd_wl2 = ti->ti_ack;
1559 if (tp->snd_wnd > tp->max_sndwnd)
1560 tp->max_sndwnd = tp->snd_wnd;
1561 needoutput = 1;
1562 }
1563
1564 /*
1565 * Process segments with URG.
1566 */
1567 if ((tiflags & TH_URG) && ti->ti_urp &&
1568 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1569 /*
1570 * This is a kludge, but if we receive and accept
1571 * random urgent pointers, we'll crash in
1572 * soreceive. It's hard to imagine someone
1573 * actually wanting to send this much urgent data.
1574 */
1575 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1576 ti->ti_urp = 0; /* XXX */
1577 tiflags &= ~TH_URG; /* XXX */
1578 goto dodata; /* XXX */
1579 }
1580 /*
1581 * If this segment advances the known urgent pointer,
1582 * then mark the data stream. This should not happen
1583 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1584 * a FIN has been received from the remote side.
1585 * In these states we ignore the URG.
1586 *
1587 * According to RFC961 (Assigned Protocols),
1588 * the urgent pointer points to the last octet
1589 * of urgent data. We continue, however,
1590 * to consider it to indicate the first octet
1591 * of data past the urgent section as the original
1592 * spec states (in one of two places).
1593 */
1594 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1595 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1596 so->so_oobmark = so->so_rcv.sb_cc +
1597 (tp->rcv_up - tp->rcv_nxt) - 1;
1598 if (so->so_oobmark == 0)
1599 so->so_state |= SS_RCVATMARK;
1600 sohasoutofband(so);
1601 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1602 }
1603 /*
1604 * Remove out of band data so doesn't get presented to user.
1605 * This can happen independent of advancing the URG pointer,
1606 * but if two URG's are pending at once, some out-of-band
1607 * data may creep in... ick.
1608 */
1609 if (ti->ti_urp <= (u_long)ti->ti_len
1610 #ifdef SO_OOBINLINE
1611 && (so->so_options & SO_OOBINLINE) == 0
1612 #endif
1613 )
1614 tcp_pulloutofband(so, ti, m);
1615 } else
1616 /*
1617 * If no out of band data is expected,
1618 * pull receive urgent pointer along
1619 * with the receive window.
1620 */
1621 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1622 tp->rcv_up = tp->rcv_nxt;
1623 dodata: /* XXX */
1624
1625 /*
1626 * Process the segment text, merging it into the TCP sequencing queue,
1627 * and arranging for acknowledgment of receipt if necessary.
1628 * This process logically involves adjusting tp->rcv_wnd as data
1629 * is presented to the user (this happens in tcp_usrreq.c,
1630 * case PRU_RCVD). If a FIN has already been received on this
1631 * connection then we just ignore the text.
1632 */
1633 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1634 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1635 TCP_REASS(tp, ti, m, so, tiflags);
1636 /*
1637 * Note the amount of data that peer has sent into
1638 * our window, in order to estimate the sender's
1639 * buffer size.
1640 */
1641 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1642 } else {
1643 m_freem(m);
1644 tiflags &= ~TH_FIN;
1645 }
1646
1647 /*
1648 * If FIN is received ACK the FIN and let the user know
1649 * that the connection is closing.
1650 */
1651 if (tiflags & TH_FIN) {
1652 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1653 socantrcvmore(so);
1654 /*
1655 * If connection is half-synchronized
1656 * (ie NEEDSYN flag on) then delay ACK,
1657 * so it may be piggybacked when SYN is sent.
1658 * Otherwise, since we received a FIN then no
1659 * more input can be expected, send ACK now.
1660 */
1661 if (tp->t_flags & TF_NEEDSYN)
1662 tp->t_flags |= TF_DELACK;
1663 else
1664 tp->t_flags |= TF_ACKNOW;
1665 tp->rcv_nxt++;
1666 }
1667 switch (tp->t_state) {
1668
1669 /*
1670 * In SYN_RECEIVED and ESTABLISHED STATES
1671 * enter the CLOSE_WAIT state.
1672 */
1673 case TCPS_SYN_RECEIVED:
1674 case TCPS_ESTABLISHED:
1675 tp->t_state = TCPS_CLOSE_WAIT;
1676 break;
1677
1678 /*
1679 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1680 * enter the CLOSING state.
1681 */
1682 case TCPS_FIN_WAIT_1:
1683 tp->t_state = TCPS_CLOSING;
1684 break;
1685
1686 /*
1687 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1688 * starting the time-wait timer, turning off the other
1689 * standard timers.
1690 */
1691 case TCPS_FIN_WAIT_2:
1692 tp->t_state = TCPS_TIME_WAIT;
1693 tcp_canceltimers(tp);
1694 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1695 if (tp->cc_recv != 0 &&
1696 tp->t_duration < TCPTV_MSL) {
1697 tp->t_timer[TCPT_2MSL] =
1698 tp->t_rxtcur * TCPTV_TWTRUNC;
1699 /* For transaction client, force ACK now. */
1700 tp->t_flags |= TF_ACKNOW;
1701 }
1702 else
1703 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1704 soisdisconnected(so);
1705 break;
1706
1707 /*
1708 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1709 */
1710 case TCPS_TIME_WAIT:
1711 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1712 break;
1713 }
1714 }
1715 #ifdef TCPDEBUG
1716 if (so->so_options & SO_DEBUG)
1717 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1718 #endif
1719
1720 /*
1721 * Return any desired output.
1722 */
1723 if (needoutput || (tp->t_flags & TF_ACKNOW))
1724 (void) tcp_output(tp);
1725 return;
1726
1727 dropafterack:
1728 /*
1729 * Generate an ACK dropping incoming segment if it occupies
1730 * sequence space, where the ACK reflects our state.
1731 *
1732 * We can now skip the test for the RST flag since all
1733 * paths to this code happen after packets containing
1734 * RST have been dropped.
1735 *
1736 * In the SYN-RECEIVED state, don't send an ACK unless the
1737 * segment we received passes the SYN-RECEIVED ACK test.
1738 * If it fails send a RST. This breaks the loop in the
1739 * "LAND" DoS attack, and also prevents an ACK storm
1740 * between two listening ports that have been sent forged
1741 * SYN segments, each with the source address of the other.
1742 */
1743 if (tp->t_state == TCPS_SYN_RECEIVED && (tiflags & TH_ACK) &&
1744 (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1745 SEQ_GT(ti->ti_ack, tp->snd_max)) )
1746 goto dropwithreset;
1747 #ifdef TCPDEBUG
1748 if (so->so_options & SO_DEBUG)
1749 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1750 #endif
1751 m_freem(m);
1752 tp->t_flags |= TF_ACKNOW;
1753 (void) tcp_output(tp);
1754 return;
1755
1756 dropwithreset:
1757 /*
1758 * Generate a RST, dropping incoming segment.
1759 * Make ACK acceptable to originator of segment.
1760 * Don't bother to respond if destination was broadcast/multicast.
1761 */
1762 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1763 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1764 goto drop;
1765 #ifdef TCPDEBUG
1766 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1767 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1768 #endif
1769 if (tiflags & TH_ACK)
1770 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1771 else {
1772 if (tiflags & TH_SYN)
1773 ti->ti_len++;
1774 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1775 TH_RST|TH_ACK);
1776 }
1777 /* destroy temporarily created socket */
1778 if (dropsocket)
1779 (void) soabort(so);
1780 return;
1781
1782 drop:
1783 /*
1784 * Drop space held by incoming segment and return.
1785 */
1786 #ifdef TCPDEBUG
1787 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1788 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1789 #endif
1790 m_freem(m);
1791 /* destroy temporarily created socket */
1792 if (dropsocket)
1793 (void) soabort(so);
1794 return;
1795 #ifndef TUBA_INCLUDE
1796 }
1797
1798 static void
1799 tcp_dooptions(tp, cp, cnt, ti, to)
1800 struct tcpcb *tp;
1801 u_char *cp;
1802 int cnt;
1803 struct tcpiphdr *ti;
1804 struct tcpopt *to;
1805 {
1806 u_short mss = 0;
1807 int opt, optlen;
1808
1809 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1810 opt = cp[0];
1811 if (opt == TCPOPT_EOL)
1812 break;
1813 if (opt == TCPOPT_NOP)
1814 optlen = 1;
1815 else {
1816 optlen = cp[1];
1817 if (optlen <= 0)
1818 break;
1819 }
1820 switch (opt) {
1821
1822 default:
1823 continue;
1824
1825 case TCPOPT_MAXSEG:
1826 if (optlen != TCPOLEN_MAXSEG)
1827 continue;
1828 if (!(ti->ti_flags & TH_SYN))
1829 continue;
1830 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1831 NTOHS(mss);
1832 break;
1833
1834 case TCPOPT_WINDOW:
1835 if (optlen != TCPOLEN_WINDOW)
1836 continue;
1837 if (!(ti->ti_flags & TH_SYN))
1838 continue;
1839 tp->t_flags |= TF_RCVD_SCALE;
1840 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1841 break;
1842
1843 case TCPOPT_TIMESTAMP:
1844 if (optlen != TCPOLEN_TIMESTAMP)
1845 continue;
1846 to->to_flag |= TOF_TS;
1847 bcopy((char *)cp + 2,
1848 (char *)&to->to_tsval, sizeof(to->to_tsval));
1849 NTOHL(to->to_tsval);
1850 bcopy((char *)cp + 6,
1851 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1852 NTOHL(to->to_tsecr);
1853
1854 /*
1855 * A timestamp received in a SYN makes
1856 * it ok to send timestamp requests and replies.
1857 */
1858 if (ti->ti_flags & TH_SYN) {
1859 tp->t_flags |= TF_RCVD_TSTMP;
1860 tp->ts_recent = to->to_tsval;
1861 tp->ts_recent_age = tcp_now;
1862 }
1863 break;
1864 case TCPOPT_CC:
1865 if (optlen != TCPOLEN_CC)
1866 continue;
1867 to->to_flag |= TOF_CC;
1868 bcopy((char *)cp + 2,
1869 (char *)&to->to_cc, sizeof(to->to_cc));
1870 NTOHL(to->to_cc);
1871 /*
1872 * A CC or CC.new option received in a SYN makes
1873 * it ok to send CC in subsequent segments.
1874 */
1875 if (ti->ti_flags & TH_SYN)
1876 tp->t_flags |= TF_RCVD_CC;
1877 break;
1878 case TCPOPT_CCNEW:
1879 if (optlen != TCPOLEN_CC)
1880 continue;
1881 if (!(ti->ti_flags & TH_SYN))
1882 continue;
1883 to->to_flag |= TOF_CCNEW;
1884 bcopy((char *)cp + 2,
1885 (char *)&to->to_cc, sizeof(to->to_cc));
1886 NTOHL(to->to_cc);
1887 /*
1888 * A CC or CC.new option received in a SYN makes
1889 * it ok to send CC in subsequent segments.
1890 */
1891 tp->t_flags |= TF_RCVD_CC;
1892 break;
1893 case TCPOPT_CCECHO:
1894 if (optlen != TCPOLEN_CC)
1895 continue;
1896 if (!(ti->ti_flags & TH_SYN))
1897 continue;
1898 to->to_flag |= TOF_CCECHO;
1899 bcopy((char *)cp + 2,
1900 (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1901 NTOHL(to->to_ccecho);
1902 break;
1903 }
1904 }
1905 if (ti->ti_flags & TH_SYN)
1906 tcp_mss(tp, mss); /* sets t_maxseg */
1907 }
1908
1909 /*
1910 * Pull out of band byte out of a segment so
1911 * it doesn't appear in the user's data queue.
1912 * It is still reflected in the segment length for
1913 * sequencing purposes.
1914 */
1915 static void
1916 tcp_pulloutofband(so, ti, m)
1917 struct socket *so;
1918 struct tcpiphdr *ti;
1919 register struct mbuf *m;
1920 {
1921 int cnt = ti->ti_urp - 1;
1922
1923 while (cnt >= 0) {
1924 if (m->m_len > cnt) {
1925 char *cp = mtod(m, caddr_t) + cnt;
1926 struct tcpcb *tp = sototcpcb(so);
1927
1928 tp->t_iobc = *cp;
1929 tp->t_oobflags |= TCPOOB_HAVEDATA;
1930 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1931 m->m_len--;
1932 return;
1933 }
1934 cnt -= m->m_len;
1935 m = m->m_next;
1936 if (m == 0)
1937 break;
1938 }
1939 panic("tcp_pulloutofband");
1940 }
1941
1942 /*
1943 * Collect new round-trip time estimate
1944 * and update averages and current timeout.
1945 */
1946 static void
1947 tcp_xmit_timer(tp, rtt)
1948 register struct tcpcb *tp;
1949 short rtt;
1950 {
1951 register int delta;
1952
1953 tcpstat.tcps_rttupdated++;
1954 tp->t_rttupdated++;
1955 if (tp->t_srtt != 0) {
1956 /*
1957 * srtt is stored as fixed point with 5 bits after the
1958 * binary point (i.e., scaled by 8). The following magic
1959 * is equivalent to the smoothing algorithm in rfc793 with
1960 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1961 * point). Adjust rtt to origin 0.
1962 */
1963 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
1964 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
1965
1966 if ((tp->t_srtt += delta) <= 0)
1967 tp->t_srtt = 1;
1968
1969 /*
1970 * We accumulate a smoothed rtt variance (actually, a
1971 * smoothed mean difference), then set the retransmit
1972 * timer to smoothed rtt + 4 times the smoothed variance.
1973 * rttvar is stored as fixed point with 4 bits after the
1974 * binary point (scaled by 16). The following is
1975 * equivalent to rfc793 smoothing with an alpha of .75
1976 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1977 * rfc793's wired-in beta.
1978 */
1979 if (delta < 0)
1980 delta = -delta;
1981 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
1982 if ((tp->t_rttvar += delta) <= 0)
1983 tp->t_rttvar = 1;
1984 } else {
1985 /*
1986 * No rtt measurement yet - use the unsmoothed rtt.
1987 * Set the variance to half the rtt (so our first
1988 * retransmit happens at 3*rtt).
1989 */
1990 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1991 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1992 }
1993 tp->t_rtt = 0;
1994 tp->t_rxtshift = 0;
1995
1996 /*
1997 * the retransmit should happen at rtt + 4 * rttvar.
1998 * Because of the way we do the smoothing, srtt and rttvar
1999 * will each average +1/2 tick of bias. When we compute
2000 * the retransmit timer, we want 1/2 tick of rounding and
2001 * 1 extra tick because of +-1/2 tick uncertainty in the
2002 * firing of the timer. The bias will give us exactly the
2003 * 1.5 tick we need. But, because the bias is
2004 * statistical, we have to test that we don't drop below
2005 * the minimum feasible timer (which is 2 ticks).
2006 */
2007 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2008 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2009
2010 /*
2011 * We received an ack for a packet that wasn't retransmitted;
2012 * it is probably safe to discard any error indications we've
2013 * received recently. This isn't quite right, but close enough
2014 * for now (a route might have failed after we sent a segment,
2015 * and the return path might not be symmetrical).
2016 */
2017 tp->t_softerror = 0;
2018 }
2019
2020 /*
2021 * Determine a reasonable value for maxseg size.
2022 * If the route is known, check route for mtu.
2023 * If none, use an mss that can be handled on the outgoing
2024 * interface without forcing IP to fragment; if bigger than
2025 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2026 * to utilize large mbufs. If no route is found, route has no mtu,
2027 * or the destination isn't local, use a default, hopefully conservative
2028 * size (usually 512 or the default IP max size, but no more than the mtu
2029 * of the interface), as we can't discover anything about intervening
2030 * gateways or networks. We also initialize the congestion/slow start
2031 * window to be a single segment if the destination isn't local.
2032 * While looking at the routing entry, we also initialize other path-dependent
2033 * parameters from pre-set or cached values in the routing entry.
2034 *
2035 * Also take into account the space needed for options that we
2036 * send regularly. Make maxseg shorter by that amount to assure
2037 * that we can send maxseg amount of data even when the options
2038 * are present. Store the upper limit of the length of options plus
2039 * data in maxopd.
2040 *
2041 * NOTE that this routine is only called when we process an incoming
2042 * segment, for outgoing segments only tcp_mssopt is called.
2043 *
2044 * In case of T/TCP, we call this routine during implicit connection
2045 * setup as well (offer = -1), to initialize maxseg from the cached
2046 * MSS of our peer.
2047 */
2048 void
2049 tcp_mss(tp, offer)
2050 struct tcpcb *tp;
2051 int offer;
2052 {
2053 register struct rtentry *rt;
2054 struct ifnet *ifp;
2055 register int rtt, mss;
2056 u_long bufsize;
2057 struct inpcb *inp;
2058 struct socket *so;
2059 struct rmxp_tao *taop;
2060 int origoffer = offer;
2061
2062 inp = tp->t_inpcb;
2063 if ((rt = tcp_rtlookup(inp)) == NULL) {
2064 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2065 return;
2066 }
2067 ifp = rt->rt_ifp;
2068 so = inp->inp_socket;
2069
2070 taop = rmx_taop(rt->rt_rmx);
2071 /*
2072 * Offer == -1 means that we didn't receive SYN yet,
2073 * use cached value in that case;
2074 */
2075 if (offer == -1)
2076 offer = taop->tao_mssopt;
2077 /*
2078 * Offer == 0 means that there was no MSS on the SYN segment,
2079 * in this case we use tcp_mssdflt.
2080 */
2081 if (offer == 0)
2082 offer = tcp_mssdflt;
2083 else
2084 /*
2085 * Sanity check: make sure that maxopd will be large
2086 * enough to allow some data on segments even is the
2087 * all the option space is used (40bytes). Otherwise
2088 * funny things may happen in tcp_output.
2089 */
2090 offer = max(offer, 64);
2091 taop->tao_mssopt = offer;
2092
2093 /*
2094 * While we're here, check if there's an initial rtt
2095 * or rttvar. Convert from the route-table units
2096 * to scaled multiples of the slow timeout timer.
2097 */
2098 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2099 /*
2100 * XXX the lock bit for RTT indicates that the value
2101 * is also a minimum value; this is subject to time.
2102 */
2103 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2104 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
2105 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
2106 tcpstat.tcps_usedrtt++;
2107 if (rt->rt_rmx.rmx_rttvar) {
2108 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2109 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
2110 tcpstat.tcps_usedrttvar++;
2111 } else {
2112 /* default variation is +- 1 rtt */
2113 tp->t_rttvar =
2114 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2115 }
2116 TCPT_RANGESET(tp->t_rxtcur,
2117 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2118 tp->t_rttmin, TCPTV_REXMTMAX);
2119 }
2120 /*
2121 * if there's an mtu associated with the route, use it
2122 */
2123 if (rt->rt_rmx.rmx_mtu)
2124 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2125 else
2126 {
2127 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
2128 if (!in_localaddr(inp->inp_faddr))
2129 mss = min(mss, tcp_mssdflt);
2130 }
2131 mss = min(mss, offer);
2132 /*
2133 * maxopd stores the maximum length of data AND options
2134 * in a segment; maxseg is the amount of data in a normal
2135 * segment. We need to store this value (maxopd) apart
2136 * from maxseg, because now every segment carries options
2137 * and thus we normally have somewhat less data in segments.
2138 */
2139 tp->t_maxopd = mss;
2140
2141 /*
2142 * In case of T/TCP, origoffer==-1 indicates, that no segments
2143 * were received yet. In this case we just guess, otherwise
2144 * we do the same as before T/TCP.
2145 */
2146 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2147 (origoffer == -1 ||
2148 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2149 mss -= TCPOLEN_TSTAMP_APPA;
2150 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2151 (origoffer == -1 ||
2152 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2153 mss -= TCPOLEN_CC_APPA;
2154
2155 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2156 if (mss > MCLBYTES)
2157 mss &= ~(MCLBYTES-1);
2158 #else
2159 if (mss > MCLBYTES)
2160 mss = mss / MCLBYTES * MCLBYTES;
2161 #endif
2162 /*
2163 * If there's a pipesize, change the socket buffer
2164 * to that size. Make the socket buffers an integral
2165 * number of mss units; if the mss is larger than
2166 * the socket buffer, decrease the mss.
2167 */
2168 #ifdef RTV_SPIPE
2169 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2170 #endif
2171 bufsize = so->so_snd.sb_hiwat;
2172 if (bufsize < mss)
2173 mss = bufsize;
2174 else {
2175 bufsize = roundup(bufsize, mss);
2176 if (bufsize > sb_max)
2177 bufsize = sb_max;
2178 (void)sbreserve(&so->so_snd, bufsize);
2179 }
2180 tp->t_maxseg = mss;
2181
2182 #ifdef RTV_RPIPE
2183 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2184 #endif
2185 bufsize = so->so_rcv.sb_hiwat;
2186 if (bufsize > mss) {
2187 bufsize = roundup(bufsize, mss);
2188 if (bufsize > sb_max)
2189 bufsize = sb_max;
2190 (void)sbreserve(&so->so_rcv, bufsize);
2191 }
2192 /*
2193 * Don't force slow-start on local network.
2194 * Make this depend on the sysctl variable below
2195 */
2196 if (!no_local_slowstart || !in_localaddr(inp->inp_faddr))
2197 tp->snd_cwnd = mss;
2198
2199 if (rt->rt_rmx.rmx_ssthresh) {
2200 /*
2201 * There's some sort of gateway or interface
2202 * buffer limit on the path. Use this to set
2203 * the slow start threshhold, but set the
2204 * threshold to no less than 2*mss.
2205 */
2206 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2207 tcpstat.tcps_usedssthresh++;
2208 }
2209 }
2210
2211 /*
2212 * Determine the MSS option to send on an outgoing SYN.
2213 */
2214 int
2215 tcp_mssopt(tp)
2216 struct tcpcb *tp;
2217 {
2218 struct rtentry *rt;
2219
2220 rt = tcp_rtlookup(tp->t_inpcb);
2221 if (rt == NULL)
2222 return tcp_mssdflt;
2223
2224 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2225 }
2226 #endif /* TUBA_INCLUDE */
Cache object: 0abc13346ad99d0edf7cf8eda78e2609
|