tcp_input.c revision 1.66 1 /* $NetBSD: tcp_input.c,v 1.66 1998/09/19 04:32:51 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
9 * Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
73 */
74
75 /*
76 * TODO list for SYN cache stuff:
77 *
78 * Find room for a "state" field, which is needed to keep a
79 * compressed state for TIME_WAIT TCBs. It's been noted already
80 * that this is fairly important for very high-volume web and
81 * mail servers, which use a large number of short-lived
82 * connections.
83 */
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/malloc.h>
88 #include <sys/mbuf.h>
89 #include <sys/protosw.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #include <sys/errno.h>
93 #include <sys/syslog.h>
94 #include <sys/pool.h>
95
96 #include <net/if.h>
97 #include <net/route.h>
98
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/ip_var.h>
104 #include <netinet/tcp.h>
105 #include <netinet/tcp_fsm.h>
106 #include <netinet/tcp_seq.h>
107 #include <netinet/tcp_timer.h>
108 #include <netinet/tcp_var.h>
109 #include <netinet/tcpip.h>
110 #include <netinet/tcp_debug.h>
111
112 #include <machine/stdarg.h>
113
114 int tcprexmtthresh = 3;
115 struct tcpiphdr tcp_saveti;
116
117 extern u_long sb_max;
118
119 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
120
121 /* for modulo comparisons of timestamps */
122 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
123 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
124
125 /*
126 * Macro to compute ACK transmission behavior. Delay the ACK unless
127 * we have already delayed an ACK (must send an ACK every two segments).
128 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
129 * option is enabled.
130 */
131 #define TCP_SETUP_ACK(tp, ti) \
132 do { \
133 if ((tp)->t_flags & TF_DELACK || \
134 (tcp_ack_on_push && (ti)->ti_flags & TH_PUSH)) \
135 tp->t_flags |= TF_ACKNOW; \
136 else \
137 TCP_SET_DELACK(tp); \
138 } while (0)
139
140 /*
141 * Insert segment ti into reassembly queue of tcp with
142 * control block tp. Return TH_FIN if reassembly now includes
143 * a segment with FIN. The macro form does the common case inline
144 * (segment is the next to be received on an established connection,
145 * and the queue is empty), avoiding linkage into and removal
146 * from the queue and repetition of various conversions.
147 * Set DELACK for segments received in order, but ack immediately
148 * when segments are out of order (so fast retransmit can work).
149 */
150 #define TCP_REASS(tp, ti, m, so, flags) { \
151 if ((ti)->ti_seq == (tp)->rcv_nxt && \
152 (tp)->segq.lh_first == NULL && \
153 (tp)->t_state == TCPS_ESTABLISHED) { \
154 TCP_SETUP_ACK(tp, ti); \
155 (tp)->rcv_nxt += (ti)->ti_len; \
156 flags = (ti)->ti_flags & TH_FIN; \
157 tcpstat.tcps_rcvpack++;\
158 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
159 sbappend(&(so)->so_rcv, (m)); \
160 sorwakeup(so); \
161 } else { \
162 (flags) = tcp_reass((tp), (ti), (m)); \
163 tp->t_flags |= TF_ACKNOW; \
164 } \
165 }
166
167 int
168 tcp_reass(tp, ti, m)
169 register struct tcpcb *tp;
170 register struct tcpiphdr *ti;
171 struct mbuf *m;
172 {
173 register struct ipqent *p, *q, *nq, *tiqe = NULL;
174 struct socket *so = tp->t_inpcb->inp_socket;
175 int pkt_flags;
176 tcp_seq pkt_seq;
177 unsigned pkt_len;
178 u_long rcvpartdupbyte = 0;
179 u_long rcvoobyte;
180
181 /*
182 * Call with ti==0 after become established to
183 * force pre-ESTABLISHED data up to user socket.
184 */
185 if (ti == 0)
186 goto present;
187
188 rcvoobyte = ti->ti_len;
189 /*
190 * Copy these to local variables because the tcpiphdr
191 * gets munged while we are collapsing mbufs.
192 */
193 pkt_seq = ti->ti_seq;
194 pkt_len = ti->ti_len;
195 pkt_flags = ti->ti_flags;
196 /*
197 * Find a segment which begins after this one does.
198 */
199 for (p = NULL, q = tp->segq.lh_first; q != NULL; q = nq) {
200 nq = q->ipqe_q.le_next;
201 /*
202 * If the received segment is just right after this
203 * fragment, merge the two together and then check
204 * for further overlaps.
205 */
206 if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
207 #ifdef TCPREASS_DEBUG
208 printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
209 tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
210 q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
211 #endif
212 pkt_len += q->ipqe_len;
213 pkt_flags |= q->ipqe_flags;
214 pkt_seq = q->ipqe_seq;
215 m_cat(q->ipqe_m, m);
216 m = q->ipqe_m;
217 goto free_ipqe;
218 }
219 /*
220 * If the received segment is completely past this
221 * fragment, we need to go the next fragment.
222 */
223 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
224 p = q;
225 continue;
226 }
227 /*
228 * If the fragment is past the received segment,
229 * it (or any following) can't be concatenated.
230 */
231 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len))
232 break;
233 /*
234 * We've received all the data in this segment before.
235 * mark it as a duplicate and return.
236 */
237 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
238 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
239 tcpstat.tcps_rcvduppack++;
240 tcpstat.tcps_rcvdupbyte += pkt_len;
241 m_freem(m);
242 if (tiqe != NULL)
243 FREE(tiqe, M_IPQ);
244 return (0);
245 }
246 /*
247 * Received segment completely overlaps this fragment
248 * so we drop the fragment (this keeps the temporal
249 * ordering of segments correct).
250 */
251 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
252 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
253 rcvpartdupbyte += q->ipqe_len;
254 m_freem(q->ipqe_m);
255 goto free_ipqe;
256 }
257 /*
258 * RX'ed segment extends past the end of the
259 * fragment. Drop the overlapping bytes. Then
260 * merge the fragment and segment then treat as
261 * a longer received packet.
262 */
263 if (SEQ_LT(q->ipqe_seq, pkt_seq)
264 && SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
265 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
266 #ifdef TCPREASS_DEBUG
267 printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
268 tp, overlap,
269 pkt_seq, pkt_seq + pkt_len, pkt_len);
270 #endif
271 m_adj(m, overlap);
272 rcvpartdupbyte += overlap;
273 m_cat(q->ipqe_m, m);
274 m = q->ipqe_m;
275 pkt_seq = q->ipqe_seq;
276 pkt_len += q->ipqe_len - overlap;
277 rcvoobyte -= overlap;
278 goto free_ipqe;
279 }
280 /*
281 * RX'ed segment extends past the front of the
282 * fragment. Drop the overlapping bytes on the
283 * received packet. The packet will then be
284 * contatentated with this fragment a bit later.
285 */
286 if (SEQ_GT(q->ipqe_seq, pkt_seq)
287 && SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) {
288 int overlap = pkt_seq + pkt_len - q->ipqe_seq;
289 #ifdef TCPREASS_DEBUG
290 printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
291 tp, overlap,
292 pkt_seq, pkt_seq + pkt_len, pkt_len);
293 #endif
294 m_adj(m, -overlap);
295 pkt_len -= overlap;
296 rcvpartdupbyte += overlap;
297 rcvoobyte -= overlap;
298 }
299 /*
300 * If the received segment immediates precedes this
301 * fragment then tack the fragment onto this segment
302 * and reinsert the data.
303 */
304 if (q->ipqe_seq == pkt_seq + pkt_len) {
305 #ifdef TCPREASS_DEBUG
306 printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
307 tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
308 pkt_seq, pkt_seq + pkt_len, pkt_len);
309 #endif
310 pkt_len += q->ipqe_len;
311 pkt_flags |= q->ipqe_flags;
312 m_cat(m, q->ipqe_m);
313 LIST_REMOVE(q, ipqe_q);
314 LIST_REMOVE(q, ipqe_timeq);
315 if (tiqe == NULL) {
316 tiqe = q;
317 } else {
318 FREE(q, M_IPQ);
319 }
320 break;
321 }
322 /*
323 * If the fragment is before the segment, remember it.
324 * When this loop is terminated, p will contain the
325 * pointer to fragment that is right before the received
326 * segment.
327 */
328 if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
329 p = q;
330
331 continue;
332
333 /*
334 * This is a common operation. It also will allow
335 * to save doing a malloc/free in most instances.
336 */
337 free_ipqe:
338 LIST_REMOVE(q, ipqe_q);
339 LIST_REMOVE(q, ipqe_timeq);
340 if (tiqe == NULL) {
341 tiqe = q;
342 } else {
343 FREE(q, M_IPQ);
344 }
345 }
346
347 /*
348 * Allocate a new queue entry since the received segment did not
349 * collapse onto any other out-of-order block; thus we are allocating
350 * a new block. If it had collapsed, tiqe would not be NULL and
351 * we would be reusing it.
352 * XXX If we can't, just drop the packet. XXX
353 */
354 if (tiqe == NULL) {
355 MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_IPQ, M_NOWAIT);
356 if (tiqe == NULL) {
357 tcpstat.tcps_rcvmemdrop++;
358 m_freem(m);
359 return (0);
360 }
361 }
362
363 /*
364 * Update the counters.
365 */
366 tcpstat.tcps_rcvoopack++;
367 tcpstat.tcps_rcvoobyte += rcvoobyte;
368 if (rcvpartdupbyte) {
369 tcpstat.tcps_rcvpartduppack++;
370 tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
371 }
372
373 /*
374 * Insert the new fragment queue entry into both queues.
375 */
376 tiqe->ipqe_m = m;
377 tiqe->ipqe_seq = pkt_seq;
378 tiqe->ipqe_len = pkt_len;
379 tiqe->ipqe_flags = pkt_flags;
380 if (p == NULL) {
381 LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
382 #ifdef TCPREASS_DEBUG
383 if (tiqe->ipqe_seq != tp->rcv_nxt)
384 printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
385 tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
386 #endif
387 } else {
388 LIST_INSERT_AFTER(p, tiqe, ipqe_q);
389 #ifdef TCPREASS_DEBUG
390 printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
391 tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
392 p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
393 #endif
394 }
395
396 LIST_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
397
398 present:
399 /*
400 * Present data to user, advancing rcv_nxt through
401 * completed sequence space.
402 */
403 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
404 return (0);
405 q = tp->segq.lh_first;
406 if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
407 return (0);
408 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
409 return (0);
410
411 tp->rcv_nxt += q->ipqe_len;
412 pkt_flags = q->ipqe_flags & TH_FIN;
413
414 LIST_REMOVE(q, ipqe_q);
415 LIST_REMOVE(q, ipqe_timeq);
416 if (so->so_state & SS_CANTRCVMORE)
417 m_freem(q->ipqe_m);
418 else
419 sbappend(&so->so_rcv, q->ipqe_m);
420 FREE(q, M_IPQ);
421 sorwakeup(so);
422 return (pkt_flags);
423 }
424
425 /*
426 * TCP input routine, follows pages 65-76 of the
427 * protocol specification dated September, 1981 very closely.
428 */
429 void
430 #if __STDC__
431 tcp_input(struct mbuf *m, ...)
432 #else
433 tcp_input(m, va_alist)
434 register struct mbuf *m;
435 #endif
436 {
437 register struct tcpiphdr *ti;
438 register struct inpcb *inp;
439 caddr_t optp = NULL;
440 int optlen = 0;
441 int len, tlen, off, hdroptlen;
442 register struct tcpcb *tp = 0;
443 register int tiflags;
444 struct socket *so = NULL;
445 int todrop, acked, ourfinisacked, needoutput = 0;
446 short ostate = 0;
447 int iss = 0;
448 u_long tiwin;
449 struct tcp_opt_info opti;
450 int iphlen;
451 va_list ap;
452
453 va_start(ap, m);
454 iphlen = va_arg(ap, int);
455 va_end(ap);
456
457 tcpstat.tcps_rcvtotal++;
458
459 opti.ts_present = 0;
460 opti.maxseg = 0;
461
462 /*
463 * Get IP and TCP header together in first mbuf.
464 * Note: IP leaves IP header in first mbuf.
465 */
466 ti = mtod(m, struct tcpiphdr *);
467 if (iphlen > sizeof (struct ip))
468 ip_stripoptions(m, (struct mbuf *)0);
469 if (m->m_len < sizeof (struct tcpiphdr)) {
470 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
471 tcpstat.tcps_rcvshort++;
472 return;
473 }
474 ti = mtod(m, struct tcpiphdr *);
475 }
476
477 /*
478 * Checksum extended TCP header and data.
479 */
480 tlen = ((struct ip *)ti)->ip_len;
481 len = sizeof (struct ip) + tlen;
482 bzero(ti->ti_x1, sizeof ti->ti_x1);
483 ti->ti_len = (u_int16_t)tlen;
484 HTONS(ti->ti_len);
485 if ((ti->ti_sum = in_cksum(m, len)) != 0) {
486 tcpstat.tcps_rcvbadsum++;
487 goto drop;
488 }
489
490 /*
491 * Check that TCP offset makes sense,
492 * pull out TCP options and adjust length. XXX
493 */
494 off = ti->ti_off << 2;
495 if (off < sizeof (struct tcphdr) || off > tlen) {
496 tcpstat.tcps_rcvbadoff++;
497 goto drop;
498 }
499 tlen -= off;
500 ti->ti_len = tlen;
501 if (off > sizeof (struct tcphdr)) {
502 if (m->m_len < sizeof(struct ip) + off) {
503 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
504 tcpstat.tcps_rcvshort++;
505 return;
506 }
507 ti = mtod(m, struct tcpiphdr *);
508 }
509 optlen = off - sizeof (struct tcphdr);
510 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
511 /*
512 * Do quick retrieval of timestamp options ("options
513 * prediction?"). If timestamp is the only option and it's
514 * formatted as recommended in RFC 1323 appendix A, we
515 * quickly get the values now and not bother calling
516 * tcp_dooptions(), etc.
517 */
518 if ((optlen == TCPOLEN_TSTAMP_APPA ||
519 (optlen > TCPOLEN_TSTAMP_APPA &&
520 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
521 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
522 (ti->ti_flags & TH_SYN) == 0) {
523 opti.ts_present = 1;
524 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
525 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
526 optp = NULL; /* we've parsed the options */
527 }
528 }
529 tiflags = ti->ti_flags;
530
531 /*
532 * Convert TCP protocol specific fields to host format.
533 */
534 NTOHL(ti->ti_seq);
535 NTOHL(ti->ti_ack);
536 NTOHS(ti->ti_win);
537 NTOHS(ti->ti_urp);
538
539 /*
540 * Locate pcb for segment.
541 */
542 findpcb:
543 inp = in_pcblookup_connect(&tcbtable, ti->ti_src, ti->ti_sport,
544 ti->ti_dst, ti->ti_dport);
545 if (inp == 0) {
546 ++tcpstat.tcps_pcbhashmiss;
547 inp = in_pcblookup_bind(&tcbtable, ti->ti_dst, ti->ti_dport);
548 if (inp == 0) {
549 ++tcpstat.tcps_noport;
550 goto dropwithreset;
551 }
552 }
553
554 /*
555 * If the state is CLOSED (i.e., TCB does not exist) then
556 * all data in the incoming segment is discarded.
557 * If the TCB exists but is in CLOSED state, it is embryonic,
558 * but should either do a listen or a connect soon.
559 */
560 tp = intotcpcb(inp);
561 if (tp == 0)
562 goto dropwithreset;
563 if (tp->t_state == TCPS_CLOSED)
564 goto drop;
565
566 /* Unscale the window into a 32-bit value. */
567 if ((tiflags & TH_SYN) == 0)
568 tiwin = ti->ti_win << tp->snd_scale;
569 else
570 tiwin = ti->ti_win;
571
572 so = inp->inp_socket;
573 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
574 if (so->so_options & SO_DEBUG) {
575 ostate = tp->t_state;
576 tcp_saveti = *ti;
577 }
578 if (so->so_options & SO_ACCEPTCONN) {
579 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
580 if (tiflags & TH_RST) {
581 syn_cache_reset(ti);
582 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
583 (TH_ACK|TH_SYN)) {
584 /*
585 * Received a SYN,ACK. This should
586 * never happen while we are in
587 * LISTEN. Send an RST.
588 */
589 goto badsyn;
590 } else if (tiflags & TH_ACK) {
591 so = syn_cache_get(so, m);
592 if (so == NULL) {
593 /*
594 * We don't have a SYN for
595 * this ACK; send an RST.
596 */
597 goto badsyn;
598 } else if (so ==
599 (struct socket *)(-1)) {
600 /*
601 * We were unable to create
602 * the connection. If the
603 * 3-way handshake was
604 * completeed, and RST has
605 * been sent to the peer.
606 * Since the mbuf might be
607 * in use for the reply,
608 * do not free it.
609 */
610 m = NULL;
611 } else {
612 /*
613 * We have created a
614 * full-blown connection.
615 */
616 inp = sotoinpcb(so);
617 tp = intotcpcb(inp);
618 tiwin <<= tp->snd_scale;
619 goto after_listen;
620 }
621 } else {
622 /*
623 * None of RST, SYN or ACK was set.
624 * This is an invalid packet for a
625 * TCB in LISTEN state. Send a RST.
626 */
627 goto badsyn;
628 }
629 } else {
630 /*
631 * Received a SYN.
632 */
633 if (in_hosteq(ti->ti_src, ti->ti_dst) &&
634 ti->ti_sport == ti->ti_dport) {
635 /*
636 * LISTEN socket received a SYN
637 * from itself? This can't possibly
638 * be valid; drop the packet.
639 */
640 tcpstat.tcps_badsyn++;
641 goto drop;
642 }
643 /*
644 * SYN looks ok; create compressed TCP
645 * state for it.
646 */
647 if (so->so_qlen <= so->so_qlimit &&
648 syn_cache_add(so, m, optp, optlen, &opti))
649 m = NULL;
650 }
651 goto drop;
652 }
653 }
654
655 after_listen:
656 #ifdef DIAGNOSTIC
657 /*
658 * Should not happen now that all embryonic connections
659 * are handled with compressed state.
660 */
661 if (tp->t_state == TCPS_LISTEN)
662 panic("tcp_input: TCPS_LISTEN");
663 #endif
664
665 /*
666 * Segment received on connection.
667 * Reset idle time and keep-alive timer.
668 */
669 tp->t_idle = 0;
670 if (TCPS_HAVEESTABLISHED(tp->t_state))
671 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
672
673 /*
674 * Process options.
675 */
676 if (optp)
677 tcp_dooptions(tp, optp, optlen, ti, &opti);
678
679 /*
680 * Header prediction: check for the two common cases
681 * of a uni-directional data xfer. If the packet has
682 * no control flags, is in-sequence, the window didn't
683 * change and we're not retransmitting, it's a
684 * candidate. If the length is zero and the ack moved
685 * forward, we're the sender side of the xfer. Just
686 * free the data acked & wake any higher level process
687 * that was blocked waiting for space. If the length
688 * is non-zero and the ack didn't move, we're the
689 * receiver side. If we're getting packets in-order
690 * (the reassembly queue is empty), add the data to
691 * the socket buffer and note that we need a delayed ack.
692 */
693 if (tp->t_state == TCPS_ESTABLISHED &&
694 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
695 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
696 ti->ti_seq == tp->rcv_nxt &&
697 tiwin && tiwin == tp->snd_wnd &&
698 tp->snd_nxt == tp->snd_max) {
699
700 /*
701 * If last ACK falls within this segment's sequence numbers,
702 * record the timestamp.
703 */
704 if (opti.ts_present &&
705 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
706 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
707 tp->ts_recent_age = tcp_now;
708 tp->ts_recent = opti.ts_val;
709 }
710
711 if (ti->ti_len == 0) {
712 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
713 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
714 tp->snd_cwnd >= tp->snd_wnd &&
715 tp->t_dupacks < tcprexmtthresh) {
716 /*
717 * this is a pure ack for outstanding data.
718 */
719 ++tcpstat.tcps_predack;
720 if (opti.ts_present)
721 tcp_xmit_timer(tp,
722 tcp_now-opti.ts_ecr+1);
723 else if (tp->t_rtt &&
724 SEQ_GT(ti->ti_ack, tp->t_rtseq))
725 tcp_xmit_timer(tp, tp->t_rtt);
726 acked = ti->ti_ack - tp->snd_una;
727 tcpstat.tcps_rcvackpack++;
728 tcpstat.tcps_rcvackbyte += acked;
729 sbdrop(&so->so_snd, acked);
730 tp->snd_una = ti->ti_ack;
731 m_freem(m);
732
733 /*
734 * If all outstanding data are acked, stop
735 * retransmit timer, otherwise restart timer
736 * using current (possibly backed-off) value.
737 * If process is waiting for space,
738 * wakeup/selwakeup/signal. If data
739 * are ready to send, let tcp_output
740 * decide between more output or persist.
741 */
742 if (tp->snd_una == tp->snd_max)
743 TCP_TIMER_DISARM(tp, TCPT_REXMT);
744 else if (TCP_TIMER_ISARMED(tp,
745 TCPT_PERSIST) == 0)
746 TCP_TIMER_ARM(tp, TCPT_REXMT,
747 tp->t_rxtcur);
748
749 sowwakeup(so);
750 if (so->so_snd.sb_cc)
751 (void) tcp_output(tp);
752 return;
753 }
754 } else if (ti->ti_ack == tp->snd_una &&
755 tp->segq.lh_first == NULL &&
756 ti->ti_len <= sbspace(&so->so_rcv)) {
757 /*
758 * this is a pure, in-sequence data packet
759 * with nothing on the reassembly queue and
760 * we have enough buffer space to take it.
761 */
762 ++tcpstat.tcps_preddat;
763 tp->rcv_nxt += ti->ti_len;
764 tcpstat.tcps_rcvpack++;
765 tcpstat.tcps_rcvbyte += ti->ti_len;
766 /*
767 * Drop TCP, IP headers and TCP options then add data
768 * to socket buffer.
769 */
770 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
771 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
772 sbappend(&so->so_rcv, m);
773 sorwakeup(so);
774 TCP_SETUP_ACK(tp, ti);
775 if (tp->t_flags & TF_ACKNOW)
776 (void) tcp_output(tp);
777 return;
778 }
779 }
780
781 /*
782 * Drop TCP, IP headers and TCP options.
783 */
784 hdroptlen = sizeof(struct tcpiphdr) + off - sizeof(struct tcphdr);
785 m->m_data += hdroptlen;
786 m->m_len -= hdroptlen;
787
788 /*
789 * Calculate amount of space in receive window,
790 * and then do TCP input processing.
791 * Receive window is amount of space in rcv queue,
792 * but not less than advertised window.
793 */
794 { int win;
795
796 win = sbspace(&so->so_rcv);
797 if (win < 0)
798 win = 0;
799 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
800 }
801
802 switch (tp->t_state) {
803
804 /*
805 * If the state is SYN_SENT:
806 * if seg contains an ACK, but not for our SYN, drop the input.
807 * if seg contains a RST, then drop the connection.
808 * if seg does not contain SYN, then drop it.
809 * Otherwise this is an acceptable SYN segment
810 * initialize tp->rcv_nxt and tp->irs
811 * if seg contains ack then advance tp->snd_una
812 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
813 * arrange for segment to be acked (eventually)
814 * continue processing rest of data/controls, beginning with URG
815 */
816 case TCPS_SYN_SENT:
817 if ((tiflags & TH_ACK) &&
818 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
819 SEQ_GT(ti->ti_ack, tp->snd_max)))
820 goto dropwithreset;
821 if (tiflags & TH_RST) {
822 if (tiflags & TH_ACK)
823 tp = tcp_drop(tp, ECONNREFUSED);
824 goto drop;
825 }
826 if ((tiflags & TH_SYN) == 0)
827 goto drop;
828 if (tiflags & TH_ACK) {
829 tp->snd_una = ti->ti_ack;
830 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
831 tp->snd_nxt = tp->snd_una;
832 }
833 TCP_TIMER_DISARM(tp, TCPT_REXMT);
834 tp->irs = ti->ti_seq;
835 tcp_rcvseqinit(tp);
836 tp->t_flags |= TF_ACKNOW;
837 tcp_mss_from_peer(tp, opti.maxseg);
838
839 /*
840 * Initialize the initial congestion window. If we
841 * had to retransmit the SYN, we must initialize cwnd
842 * to 1 segment (i.e. the Loss Window).
843 */
844 if (tp->t_flags & TF_SYN_REXMT)
845 tp->snd_cwnd = tp->t_peermss;
846 else
847 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
848 tp->t_peermss);
849
850 tcp_rmx_rtt(tp);
851 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
852 tcpstat.tcps_connects++;
853 soisconnected(so);
854 tcp_established(tp);
855 /* Do window scaling on this connection? */
856 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
857 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
858 tp->snd_scale = tp->requested_s_scale;
859 tp->rcv_scale = tp->request_r_scale;
860 }
861 (void) tcp_reass(tp, (struct tcpiphdr *)0,
862 (struct mbuf *)0);
863 /*
864 * if we didn't have to retransmit the SYN,
865 * use its rtt as our initial srtt & rtt var.
866 */
867 if (tp->t_rtt)
868 tcp_xmit_timer(tp, tp->t_rtt);
869 } else
870 tp->t_state = TCPS_SYN_RECEIVED;
871
872 /*
873 * Advance ti->ti_seq to correspond to first data byte.
874 * If data, trim to stay within window,
875 * dropping FIN if necessary.
876 */
877 ti->ti_seq++;
878 if (ti->ti_len > tp->rcv_wnd) {
879 todrop = ti->ti_len - tp->rcv_wnd;
880 m_adj(m, -todrop);
881 ti->ti_len = tp->rcv_wnd;
882 tiflags &= ~TH_FIN;
883 tcpstat.tcps_rcvpackafterwin++;
884 tcpstat.tcps_rcvbyteafterwin += todrop;
885 }
886 tp->snd_wl1 = ti->ti_seq - 1;
887 tp->rcv_up = ti->ti_seq;
888 goto step6;
889
890 /*
891 * If the state is SYN_RECEIVED:
892 * If seg contains an ACK, but not for our SYN, drop the input
893 * and generate an RST. See page 36, rfc793
894 */
895 case TCPS_SYN_RECEIVED:
896 if ((tiflags & TH_ACK) &&
897 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
898 SEQ_GT(ti->ti_ack, tp->snd_max)))
899 goto dropwithreset;
900 break;
901 }
902
903 /*
904 * States other than LISTEN or SYN_SENT.
905 * First check timestamp, if present.
906 * Then check that at least some bytes of segment are within
907 * receive window. If segment begins before rcv_nxt,
908 * drop leading data (and SYN); if nothing left, just ack.
909 *
910 * RFC 1323 PAWS: If we have a timestamp reply on this segment
911 * and it's less than ts_recent, drop it.
912 */
913 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
914 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
915
916 /* Check to see if ts_recent is over 24 days old. */
917 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
918 /*
919 * Invalidate ts_recent. If this segment updates
920 * ts_recent, the age will be reset later and ts_recent
921 * will get a valid value. If it does not, setting
922 * ts_recent to zero will at least satisfy the
923 * requirement that zero be placed in the timestamp
924 * echo reply when ts_recent isn't valid. The
925 * age isn't reset until we get a valid ts_recent
926 * because we don't want out-of-order segments to be
927 * dropped when ts_recent is old.
928 */
929 tp->ts_recent = 0;
930 } else {
931 tcpstat.tcps_rcvduppack++;
932 tcpstat.tcps_rcvdupbyte += ti->ti_len;
933 tcpstat.tcps_pawsdrop++;
934 goto dropafterack;
935 }
936 }
937
938 todrop = tp->rcv_nxt - ti->ti_seq;
939 if (todrop > 0) {
940 if (tiflags & TH_SYN) {
941 tiflags &= ~TH_SYN;
942 ti->ti_seq++;
943 if (ti->ti_urp > 1)
944 ti->ti_urp--;
945 else {
946 tiflags &= ~TH_URG;
947 ti->ti_urp = 0;
948 }
949 todrop--;
950 }
951 if (todrop > ti->ti_len ||
952 (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
953 /*
954 * Any valid FIN must be to the left of the window.
955 * At this point the FIN must be a duplicate or
956 * out of sequence; drop it.
957 */
958 tiflags &= ~TH_FIN;
959 /*
960 * Send an ACK to resynchronize and drop any data.
961 * But keep on processing for RST or ACK.
962 */
963 tp->t_flags |= TF_ACKNOW;
964 todrop = ti->ti_len;
965 tcpstat.tcps_rcvdupbyte += todrop;
966 tcpstat.tcps_rcvduppack++;
967 } else {
968 tcpstat.tcps_rcvpartduppack++;
969 tcpstat.tcps_rcvpartdupbyte += todrop;
970 }
971 m_adj(m, todrop);
972 ti->ti_seq += todrop;
973 ti->ti_len -= todrop;
974 if (ti->ti_urp > todrop)
975 ti->ti_urp -= todrop;
976 else {
977 tiflags &= ~TH_URG;
978 ti->ti_urp = 0;
979 }
980 }
981
982 /*
983 * If new data are received on a connection after the
984 * user processes are gone, then RST the other end.
985 */
986 if ((so->so_state & SS_NOFDREF) &&
987 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
988 tp = tcp_close(tp);
989 tcpstat.tcps_rcvafterclose++;
990 goto dropwithreset;
991 }
992
993 /*
994 * If segment ends after window, drop trailing data
995 * (and PUSH and FIN); if nothing left, just ACK.
996 */
997 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
998 if (todrop > 0) {
999 tcpstat.tcps_rcvpackafterwin++;
1000 if (todrop >= ti->ti_len) {
1001 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1002 /*
1003 * If a new connection request is received
1004 * while in TIME_WAIT, drop the old connection
1005 * and start over if the sequence numbers
1006 * are above the previous ones.
1007 */
1008 if (tiflags & TH_SYN &&
1009 tp->t_state == TCPS_TIME_WAIT &&
1010 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1011 iss = tcp_new_iss(tp, sizeof(struct tcpcb),
1012 tp->rcv_nxt);
1013 tp = tcp_close(tp);
1014 /*
1015 * We have already advanced the mbuf
1016 * pointers past the IP+TCP headers and
1017 * options. Restore those pointers before
1018 * attempting to use the TCP header again.
1019 */
1020 m->m_data -= hdroptlen;
1021 m->m_len += hdroptlen;
1022 goto findpcb;
1023 }
1024 /*
1025 * If window is closed can only take segments at
1026 * window edge, and have to drop data and PUSH from
1027 * incoming segments. Continue processing, but
1028 * remember to ack. Otherwise, drop segment
1029 * and ack.
1030 */
1031 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1032 tp->t_flags |= TF_ACKNOW;
1033 tcpstat.tcps_rcvwinprobe++;
1034 } else
1035 goto dropafterack;
1036 } else
1037 tcpstat.tcps_rcvbyteafterwin += todrop;
1038 m_adj(m, -todrop);
1039 ti->ti_len -= todrop;
1040 tiflags &= ~(TH_PUSH|TH_FIN);
1041 }
1042
1043 /*
1044 * If last ACK falls within this segment's sequence numbers,
1045 * and the timestamp is newer, record it.
1046 */
1047 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
1048 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
1049 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
1050 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
1051 tp->ts_recent_age = tcp_now;
1052 tp->ts_recent = opti.ts_val;
1053 }
1054
1055 /*
1056 * If the RST bit is set examine the state:
1057 * SYN_RECEIVED STATE:
1058 * If passive open, return to LISTEN state.
1059 * If active open, inform user that connection was refused.
1060 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1061 * Inform user that connection was reset, and close tcb.
1062 * CLOSING, LAST_ACK, TIME_WAIT STATES
1063 * Close the tcb.
1064 */
1065 if (tiflags&TH_RST) switch (tp->t_state) {
1066
1067 case TCPS_SYN_RECEIVED:
1068 so->so_error = ECONNREFUSED;
1069 goto close;
1070
1071 case TCPS_ESTABLISHED:
1072 case TCPS_FIN_WAIT_1:
1073 case TCPS_FIN_WAIT_2:
1074 case TCPS_CLOSE_WAIT:
1075 so->so_error = ECONNRESET;
1076 close:
1077 tp->t_state = TCPS_CLOSED;
1078 tcpstat.tcps_drops++;
1079 tp = tcp_close(tp);
1080 goto drop;
1081
1082 case TCPS_CLOSING:
1083 case TCPS_LAST_ACK:
1084 case TCPS_TIME_WAIT:
1085 tp = tcp_close(tp);
1086 goto drop;
1087 }
1088
1089 /*
1090 * If a SYN is in the window, then this is an
1091 * error and we send an RST and drop the connection.
1092 */
1093 if (tiflags & TH_SYN) {
1094 tp = tcp_drop(tp, ECONNRESET);
1095 goto dropwithreset;
1096 }
1097
1098 /*
1099 * If the ACK bit is off we drop the segment and return.
1100 */
1101 if ((tiflags & TH_ACK) == 0)
1102 goto drop;
1103
1104 /*
1105 * Ack processing.
1106 */
1107 switch (tp->t_state) {
1108
1109 /*
1110 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1111 * ESTABLISHED state and continue processing, otherwise
1112 * send an RST.
1113 */
1114 case TCPS_SYN_RECEIVED:
1115 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1116 SEQ_GT(ti->ti_ack, tp->snd_max))
1117 goto dropwithreset;
1118 tcpstat.tcps_connects++;
1119 soisconnected(so);
1120 tcp_established(tp);
1121 /* Do window scaling? */
1122 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1123 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1124 tp->snd_scale = tp->requested_s_scale;
1125 tp->rcv_scale = tp->request_r_scale;
1126 }
1127 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
1128 tp->snd_wl1 = ti->ti_seq - 1;
1129 /* fall into ... */
1130
1131 /*
1132 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1133 * ACKs. If the ack is in the range
1134 * tp->snd_una < ti->ti_ack <= tp->snd_max
1135 * then advance tp->snd_una to ti->ti_ack and drop
1136 * data from the retransmission queue. If this ACK reflects
1137 * more up to date window information we update our window information.
1138 */
1139 case TCPS_ESTABLISHED:
1140 case TCPS_FIN_WAIT_1:
1141 case TCPS_FIN_WAIT_2:
1142 case TCPS_CLOSE_WAIT:
1143 case TCPS_CLOSING:
1144 case TCPS_LAST_ACK:
1145 case TCPS_TIME_WAIT:
1146
1147 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1148 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1149 tcpstat.tcps_rcvdupack++;
1150 /*
1151 * If we have outstanding data (other than
1152 * a window probe), this is a completely
1153 * duplicate ack (ie, window info didn't
1154 * change), the ack is the biggest we've
1155 * seen and we've seen exactly our rexmt
1156 * threshhold of them, assume a packet
1157 * has been dropped and retransmit it.
1158 * Kludge snd_nxt & the congestion
1159 * window so we send only this one
1160 * packet.
1161 *
1162 * We know we're losing at the current
1163 * window size so do congestion avoidance
1164 * (set ssthresh to half the current window
1165 * and pull our congestion window back to
1166 * the new ssthresh).
1167 *
1168 * Dup acks mean that packets have left the
1169 * network (they're now cached at the receiver)
1170 * so bump cwnd by the amount in the receiver
1171 * to keep a constant cwnd packets in the
1172 * network.
1173 */
1174 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
1175 ti->ti_ack != tp->snd_una)
1176 tp->t_dupacks = 0;
1177 else if (++tp->t_dupacks == tcprexmtthresh) {
1178 tcp_seq onxt = tp->snd_nxt;
1179 u_int win =
1180 min(tp->snd_wnd, tp->snd_cwnd) /
1181 2 / tp->t_segsz;
1182
1183 if (win < 2)
1184 win = 2;
1185 tp->snd_ssthresh = win * tp->t_segsz;
1186 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1187 tp->t_rtt = 0;
1188 tp->snd_nxt = ti->ti_ack;
1189 tp->snd_cwnd = tp->t_segsz;
1190 (void) tcp_output(tp);
1191 tp->snd_cwnd = tp->snd_ssthresh +
1192 tp->t_segsz * tp->t_dupacks;
1193 if (SEQ_GT(onxt, tp->snd_nxt))
1194 tp->snd_nxt = onxt;
1195 goto drop;
1196 } else if (tp->t_dupacks > tcprexmtthresh) {
1197 tp->snd_cwnd += tp->t_segsz;
1198 (void) tcp_output(tp);
1199 goto drop;
1200 }
1201 } else
1202 tp->t_dupacks = 0;
1203 break;
1204 }
1205 /*
1206 * If the congestion window was inflated to account
1207 * for the other side's cached packets, retract it.
1208 */
1209 if (tp->t_dupacks >= tcprexmtthresh &&
1210 tp->snd_cwnd > tp->snd_ssthresh)
1211 tp->snd_cwnd = tp->snd_ssthresh;
1212 tp->t_dupacks = 0;
1213 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1214 tcpstat.tcps_rcvacktoomuch++;
1215 goto dropafterack;
1216 }
1217 acked = ti->ti_ack - tp->snd_una;
1218 tcpstat.tcps_rcvackpack++;
1219 tcpstat.tcps_rcvackbyte += acked;
1220
1221 /*
1222 * If we have a timestamp reply, update smoothed
1223 * round trip time. If no timestamp is present but
1224 * transmit timer is running and timed sequence
1225 * number was acked, update smoothed round trip time.
1226 * Since we now have an rtt measurement, cancel the
1227 * timer backoff (cf., Phil Karn's retransmit alg.).
1228 * Recompute the initial retransmit timer.
1229 */
1230 if (opti.ts_present)
1231 tcp_xmit_timer(tp, tcp_now - opti.ts_ecr + 1);
1232 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1233 tcp_xmit_timer(tp,tp->t_rtt);
1234
1235 /*
1236 * If all outstanding data is acked, stop retransmit
1237 * timer and remember to restart (more output or persist).
1238 * If there is more data to be acked, restart retransmit
1239 * timer, using current (possibly backed-off) value.
1240 */
1241 if (ti->ti_ack == tp->snd_max) {
1242 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1243 needoutput = 1;
1244 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1245 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1246 /*
1247 * When new data is acked, open the congestion window.
1248 * If the window gives us less than ssthresh packets
1249 * in flight, open exponentially (segsz per packet).
1250 * Otherwise open linearly: segsz per window
1251 * (segsz^2 / cwnd per packet), plus a constant
1252 * fraction of a packet (segsz/8) to help larger windows
1253 * open quickly enough.
1254 */
1255 {
1256 register u_int cw = tp->snd_cwnd;
1257 register u_int incr = tp->t_segsz;
1258
1259 if (cw > tp->snd_ssthresh)
1260 incr = incr * incr / cw;
1261 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1262 }
1263 if (acked > so->so_snd.sb_cc) {
1264 tp->snd_wnd -= so->so_snd.sb_cc;
1265 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1266 ourfinisacked = 1;
1267 } else {
1268 sbdrop(&so->so_snd, acked);
1269 tp->snd_wnd -= acked;
1270 ourfinisacked = 0;
1271 }
1272 sowwakeup(so);
1273 tp->snd_una = ti->ti_ack;
1274 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1275 tp->snd_nxt = tp->snd_una;
1276
1277 switch (tp->t_state) {
1278
1279 /*
1280 * In FIN_WAIT_1 STATE in addition to the processing
1281 * for the ESTABLISHED state if our FIN is now acknowledged
1282 * then enter FIN_WAIT_2.
1283 */
1284 case TCPS_FIN_WAIT_1:
1285 if (ourfinisacked) {
1286 /*
1287 * If we can't receive any more
1288 * data, then closing user can proceed.
1289 * Starting the timer is contrary to the
1290 * specification, but if we don't get a FIN
1291 * we'll hang forever.
1292 */
1293 if (so->so_state & SS_CANTRCVMORE) {
1294 soisdisconnected(so);
1295 if (tcp_maxidle > 0)
1296 TCP_TIMER_ARM(tp, TCPT_2MSL,
1297 tcp_maxidle);
1298 }
1299 tp->t_state = TCPS_FIN_WAIT_2;
1300 }
1301 break;
1302
1303 /*
1304 * In CLOSING STATE in addition to the processing for
1305 * the ESTABLISHED state if the ACK acknowledges our FIN
1306 * then enter the TIME-WAIT state, otherwise ignore
1307 * the segment.
1308 */
1309 case TCPS_CLOSING:
1310 if (ourfinisacked) {
1311 tp->t_state = TCPS_TIME_WAIT;
1312 tcp_canceltimers(tp);
1313 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1314 soisdisconnected(so);
1315 }
1316 break;
1317
1318 /*
1319 * In LAST_ACK, we may still be waiting for data to drain
1320 * and/or to be acked, as well as for the ack of our FIN.
1321 * If our FIN is now acknowledged, delete the TCB,
1322 * enter the closed state and return.
1323 */
1324 case TCPS_LAST_ACK:
1325 if (ourfinisacked) {
1326 tp = tcp_close(tp);
1327 goto drop;
1328 }
1329 break;
1330
1331 /*
1332 * In TIME_WAIT state the only thing that should arrive
1333 * is a retransmission of the remote FIN. Acknowledge
1334 * it and restart the finack timer.
1335 */
1336 case TCPS_TIME_WAIT:
1337 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1338 goto dropafterack;
1339 }
1340 }
1341
1342 step6:
1343 /*
1344 * Update window information.
1345 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1346 */
1347 if (((tiflags & TH_ACK) && SEQ_LT(tp->snd_wl1, ti->ti_seq)) ||
1348 (tp->snd_wl1 == ti->ti_seq && SEQ_LT(tp->snd_wl2, ti->ti_ack)) ||
1349 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)) {
1350 /* keep track of pure window updates */
1351 if (ti->ti_len == 0 &&
1352 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1353 tcpstat.tcps_rcvwinupd++;
1354 tp->snd_wnd = tiwin;
1355 tp->snd_wl1 = ti->ti_seq;
1356 tp->snd_wl2 = ti->ti_ack;
1357 if (tp->snd_wnd > tp->max_sndwnd)
1358 tp->max_sndwnd = tp->snd_wnd;
1359 needoutput = 1;
1360 }
1361
1362 /*
1363 * Process segments with URG.
1364 */
1365 if ((tiflags & TH_URG) && ti->ti_urp &&
1366 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1367 /*
1368 * This is a kludge, but if we receive and accept
1369 * random urgent pointers, we'll crash in
1370 * soreceive. It's hard to imagine someone
1371 * actually wanting to send this much urgent data.
1372 */
1373 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1374 ti->ti_urp = 0; /* XXX */
1375 tiflags &= ~TH_URG; /* XXX */
1376 goto dodata; /* XXX */
1377 }
1378 /*
1379 * If this segment advances the known urgent pointer,
1380 * then mark the data stream. This should not happen
1381 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1382 * a FIN has been received from the remote side.
1383 * In these states we ignore the URG.
1384 *
1385 * According to RFC961 (Assigned Protocols),
1386 * the urgent pointer points to the last octet
1387 * of urgent data. We continue, however,
1388 * to consider it to indicate the first octet
1389 * of data past the urgent section as the original
1390 * spec states (in one of two places).
1391 */
1392 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1393 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1394 so->so_oobmark = so->so_rcv.sb_cc +
1395 (tp->rcv_up - tp->rcv_nxt) - 1;
1396 if (so->so_oobmark == 0)
1397 so->so_state |= SS_RCVATMARK;
1398 sohasoutofband(so);
1399 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1400 }
1401 /*
1402 * Remove out of band data so doesn't get presented to user.
1403 * This can happen independent of advancing the URG pointer,
1404 * but if two URG's are pending at once, some out-of-band
1405 * data may creep in... ick.
1406 */
1407 if (ti->ti_urp <= (u_int16_t) ti->ti_len
1408 #ifdef SO_OOBINLINE
1409 && (so->so_options & SO_OOBINLINE) == 0
1410 #endif
1411 )
1412 tcp_pulloutofband(so, ti, m);
1413 } else
1414 /*
1415 * If no out of band data is expected,
1416 * pull receive urgent pointer along
1417 * with the receive window.
1418 */
1419 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1420 tp->rcv_up = tp->rcv_nxt;
1421 dodata: /* XXX */
1422
1423 /*
1424 * Process the segment text, merging it into the TCP sequencing queue,
1425 * and arranging for acknowledgment of receipt if necessary.
1426 * This process logically involves adjusting tp->rcv_wnd as data
1427 * is presented to the user (this happens in tcp_usrreq.c,
1428 * case PRU_RCVD). If a FIN has already been received on this
1429 * connection then we just ignore the text.
1430 */
1431 if ((ti->ti_len || (tiflags & TH_FIN)) &&
1432 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1433 TCP_REASS(tp, ti, m, so, tiflags);
1434 /*
1435 * Note the amount of data that peer has sent into
1436 * our window, in order to estimate the sender's
1437 * buffer size.
1438 */
1439 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1440 } else {
1441 m_freem(m);
1442 tiflags &= ~TH_FIN;
1443 }
1444
1445 /*
1446 * If FIN is received ACK the FIN and let the user know
1447 * that the connection is closing. Ignore a FIN received before
1448 * the connection is fully established.
1449 */
1450 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
1451 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1452 socantrcvmore(so);
1453 tp->t_flags |= TF_ACKNOW;
1454 tp->rcv_nxt++;
1455 }
1456 switch (tp->t_state) {
1457
1458 /*
1459 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
1460 */
1461 case TCPS_ESTABLISHED:
1462 tp->t_state = TCPS_CLOSE_WAIT;
1463 break;
1464
1465 /*
1466 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1467 * enter the CLOSING state.
1468 */
1469 case TCPS_FIN_WAIT_1:
1470 tp->t_state = TCPS_CLOSING;
1471 break;
1472
1473 /*
1474 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1475 * starting the time-wait timer, turning off the other
1476 * standard timers.
1477 */
1478 case TCPS_FIN_WAIT_2:
1479 tp->t_state = TCPS_TIME_WAIT;
1480 tcp_canceltimers(tp);
1481 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1482 soisdisconnected(so);
1483 break;
1484
1485 /*
1486 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1487 */
1488 case TCPS_TIME_WAIT:
1489 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1490 break;
1491 }
1492 }
1493 if (so->so_options & SO_DEBUG)
1494 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1495
1496 /*
1497 * Return any desired output.
1498 */
1499 if (needoutput || (tp->t_flags & TF_ACKNOW))
1500 (void) tcp_output(tp);
1501 return;
1502
1503 badsyn:
1504 /*
1505 * Received a bad SYN. Increment counters and dropwithreset.
1506 */
1507 tcpstat.tcps_badsyn++;
1508 tp = NULL;
1509 goto dropwithreset;
1510
1511 dropafterack:
1512 /*
1513 * Generate an ACK dropping incoming segment if it occupies
1514 * sequence space, where the ACK reflects our state.
1515 */
1516 if (tiflags & TH_RST)
1517 goto drop;
1518 m_freem(m);
1519 tp->t_flags |= TF_ACKNOW;
1520 (void) tcp_output(tp);
1521 return;
1522
1523 dropwithreset:
1524 /*
1525 * Generate a RST, dropping incoming segment.
1526 * Make ACK acceptable to originator of segment.
1527 * Don't bother to respond if destination was broadcast/multicast.
1528 */
1529 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1530 IN_MULTICAST(ti->ti_dst.s_addr))
1531 goto drop;
1532 if (tiflags & TH_ACK)
1533 (void)tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1534 else {
1535 if (tiflags & TH_SYN)
1536 ti->ti_len++;
1537 (void)tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1538 TH_RST|TH_ACK);
1539 }
1540 return;
1541
1542 drop:
1543 /*
1544 * Drop space held by incoming segment and return.
1545 */
1546 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1547 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1548 m_freem(m);
1549 return;
1550 }
1551
1552 void
1553 tcp_dooptions(tp, cp, cnt, ti, oi)
1554 struct tcpcb *tp;
1555 u_char *cp;
1556 int cnt;
1557 struct tcpiphdr *ti;
1558 struct tcp_opt_info *oi;
1559 {
1560 u_int16_t mss;
1561 int opt, optlen;
1562
1563 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1564 opt = cp[0];
1565 if (opt == TCPOPT_EOL)
1566 break;
1567 if (opt == TCPOPT_NOP)
1568 optlen = 1;
1569 else {
1570 optlen = cp[1];
1571 if (optlen <= 0)
1572 break;
1573 }
1574 switch (opt) {
1575
1576 default:
1577 continue;
1578
1579 case TCPOPT_MAXSEG:
1580 if (optlen != TCPOLEN_MAXSEG)
1581 continue;
1582 if (!(ti->ti_flags & TH_SYN))
1583 continue;
1584 bcopy(cp + 2, &mss, sizeof(mss));
1585 oi->maxseg = ntohs(mss);
1586 break;
1587
1588 case TCPOPT_WINDOW:
1589 if (optlen != TCPOLEN_WINDOW)
1590 continue;
1591 if (!(ti->ti_flags & TH_SYN))
1592 continue;
1593 tp->t_flags |= TF_RCVD_SCALE;
1594 tp->requested_s_scale = cp[2];
1595 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
1596 log(LOG_ERR, "TCP: invalid wscale %d from "
1597 "0x%08x, assuming %d\n",
1598 tp->requested_s_scale,
1599 ntohl(ti->ti_src.s_addr),
1600 TCP_MAX_WINSHIFT);
1601 tp->requested_s_scale = TCP_MAX_WINSHIFT;
1602 }
1603 break;
1604
1605 case TCPOPT_TIMESTAMP:
1606 if (optlen != TCPOLEN_TIMESTAMP)
1607 continue;
1608 oi->ts_present = 1;
1609 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
1610 NTOHL(oi->ts_val);
1611 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
1612 NTOHL(oi->ts_ecr);
1613
1614 /*
1615 * A timestamp received in a SYN makes
1616 * it ok to send timestamp requests and replies.
1617 */
1618 if (ti->ti_flags & TH_SYN) {
1619 tp->t_flags |= TF_RCVD_TSTMP;
1620 tp->ts_recent = oi->ts_val;
1621 tp->ts_recent_age = tcp_now;
1622 }
1623 break;
1624 case TCPOPT_SACK_PERMITTED:
1625 if (optlen != TCPOLEN_SACK_PERMITTED)
1626 continue;
1627 if (!(ti->ti_flags & TH_SYN))
1628 continue;
1629 tp->t_flags &= ~TF_CANT_TXSACK;
1630 break;
1631
1632 case TCPOPT_SACK:
1633 if (tp->t_flags & TF_IGNR_RXSACK)
1634 continue;
1635 if (optlen % 8 != 2 || optlen < 10)
1636 continue;
1637 cp += 2;
1638 optlen -= 2;
1639 for (; optlen > 0; cp -= 8, optlen -= 8) {
1640 tcp_seq lwe, rwe;
1641 bcopy((char *)cp, (char *) &lwe, sizeof(lwe));
1642 NTOHL(lwe);
1643 bcopy((char *)cp, (char *) &rwe, sizeof(rwe));
1644 NTOHL(rwe);
1645 /* tcp_mark_sacked(tp, lwe, rwe); */
1646 }
1647 break;
1648 }
1649 }
1650 }
1651
1652 /*
1653 * Pull out of band byte out of a segment so
1654 * it doesn't appear in the user's data queue.
1655 * It is still reflected in the segment length for
1656 * sequencing purposes.
1657 */
1658 void
1659 tcp_pulloutofband(so, ti, m)
1660 struct socket *so;
1661 struct tcpiphdr *ti;
1662 register struct mbuf *m;
1663 {
1664 int cnt = ti->ti_urp - 1;
1665
1666 while (cnt >= 0) {
1667 if (m->m_len > cnt) {
1668 char *cp = mtod(m, caddr_t) + cnt;
1669 struct tcpcb *tp = sototcpcb(so);
1670
1671 tp->t_iobc = *cp;
1672 tp->t_oobflags |= TCPOOB_HAVEDATA;
1673 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1674 m->m_len--;
1675 return;
1676 }
1677 cnt -= m->m_len;
1678 m = m->m_next;
1679 if (m == 0)
1680 break;
1681 }
1682 panic("tcp_pulloutofband");
1683 }
1684
1685 /*
1686 * Collect new round-trip time estimate
1687 * and update averages and current timeout.
1688 */
1689 void
1690 tcp_xmit_timer(tp, rtt)
1691 register struct tcpcb *tp;
1692 short rtt;
1693 {
1694 register short delta;
1695 short rttmin;
1696
1697 tcpstat.tcps_rttupdated++;
1698 --rtt;
1699 if (tp->t_srtt != 0) {
1700 /*
1701 * srtt is stored as fixed point with 3 bits after the
1702 * binary point (i.e., scaled by 8). The following magic
1703 * is equivalent to the smoothing algorithm in rfc793 with
1704 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1705 * point). Adjust rtt to origin 0.
1706 */
1707 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
1708 if ((tp->t_srtt += delta) <= 0)
1709 tp->t_srtt = 1 << 2;
1710 /*
1711 * We accumulate a smoothed rtt variance (actually, a
1712 * smoothed mean difference), then set the retransmit
1713 * timer to smoothed rtt + 4 times the smoothed variance.
1714 * rttvar is stored as fixed point with 2 bits after the
1715 * binary point (scaled by 4). The following is
1716 * equivalent to rfc793 smoothing with an alpha of .75
1717 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1718 * rfc793's wired-in beta.
1719 */
1720 if (delta < 0)
1721 delta = -delta;
1722 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1723 if ((tp->t_rttvar += delta) <= 0)
1724 tp->t_rttvar = 1 << 2;
1725 } else {
1726 /*
1727 * No rtt measurement yet - use the unsmoothed rtt.
1728 * Set the variance to half the rtt (so our first
1729 * retransmit happens at 3*rtt).
1730 */
1731 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
1732 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
1733 }
1734 tp->t_rtt = 0;
1735 tp->t_rxtshift = 0;
1736
1737 /*
1738 * the retransmit should happen at rtt + 4 * rttvar.
1739 * Because of the way we do the smoothing, srtt and rttvar
1740 * will each average +1/2 tick of bias. When we compute
1741 * the retransmit timer, we want 1/2 tick of rounding and
1742 * 1 extra tick because of +-1/2 tick uncertainty in the
1743 * firing of the timer. The bias will give us exactly the
1744 * 1.5 tick we need. But, because the bias is
1745 * statistical, we have to test that we don't drop below
1746 * the minimum feasible timer (which is 2 ticks).
1747 */
1748 if (tp->t_rttmin > rtt + 2)
1749 rttmin = tp->t_rttmin;
1750 else
1751 rttmin = rtt + 2;
1752 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
1753
1754 /*
1755 * We received an ack for a packet that wasn't retransmitted;
1756 * it is probably safe to discard any error indications we've
1757 * received recently. This isn't quite right, but close enough
1758 * for now (a route might have failed after we sent a segment,
1759 * and the return path might not be symmetrical).
1760 */
1761 tp->t_softerror = 0;
1762 }
1763
1764 /*
1765 * TCP compressed state engine. Currently used to hold compressed
1766 * state for SYN_RECEIVED.
1767 */
1768
1769 u_long syn_cache_count;
1770 u_int32_t syn_hash1, syn_hash2;
1771
1772 #define SYN_HASH(sa, sp, dp) \
1773 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
1774 ((u_int32_t)(sp)))^syn_hash2)))
1775
1776 LIST_HEAD(, syn_cache_head) tcp_syn_cache_queue;
1777
1778 #define SYN_CACHE_RM(sc, scp) \
1779 do { \
1780 TAILQ_REMOVE(&(scp)->sch_queue, (sc), sc_queue); \
1781 if (--(scp)->sch_length == 0) \
1782 LIST_REMOVE((scp), sch_headq); \
1783 syn_cache_count--; \
1784 } while (0)
1785
1786 struct pool syn_cache_pool;
1787
1788 void
1789 syn_cache_init()
1790 {
1791 int i;
1792
1793 /* Initialize the hash bucket queues. */
1794 for (i = 0; i < tcp_syn_cache_size; i++)
1795 TAILQ_INIT(&tcp_syn_cache[i].sch_queue);
1796
1797 /* Initialize the active hash bucket cache. */
1798 LIST_INIT(&tcp_syn_cache_queue);
1799
1800 /* Initialize the syn cache pool. */
1801 pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
1802 "synpl", 0, NULL, NULL, M_PCB);
1803 }
1804
1805 void
1806 syn_cache_insert(sc)
1807 struct syn_cache *sc;
1808 {
1809 struct syn_cache_head *scp, *scp2, *sce;
1810 struct syn_cache *sc2;
1811 int s;
1812
1813 /*
1814 * If there are no entries in the hash table, reinitialize
1815 * the hash secrets.
1816 */
1817 if (syn_cache_count == 0) {
1818 struct timeval tv;
1819 microtime(&tv);
1820 syn_hash1 = random() ^ (u_long)≻
1821 syn_hash2 = random() ^ tv.tv_usec;
1822 }
1823
1824 sc->sc_hash = SYN_HASH(&sc->sc_src, sc->sc_sport, sc->sc_dport);
1825 scp = &tcp_syn_cache[sc->sc_hash % tcp_syn_cache_size];
1826
1827 /*
1828 * Make sure that we don't overflow the per-bucket
1829 * limit or the total cache size limit.
1830 */
1831 s = splsoftnet();
1832 if (scp->sch_length >= tcp_syn_bucket_limit) {
1833 tcpstat.tcps_sc_bucketoverflow++;
1834 /*
1835 * The bucket is full. Toss the first (i.e. oldest)
1836 * element in this bucket.
1837 */
1838 sc2 = TAILQ_FIRST(&scp->sch_queue);
1839 SYN_CACHE_RM(sc2, scp);
1840 if (sc2->sc_ipopts)
1841 (void) m_free(sc2->sc_ipopts);
1842 pool_put(&syn_cache_pool, sc2);
1843 } else if (syn_cache_count >= tcp_syn_cache_limit) {
1844 tcpstat.tcps_sc_overflowed++;
1845 /*
1846 * The cache is full. Toss the first (i.e. oldest)
1847 * element in the first non-empty bucket we can find.
1848 */
1849 scp2 = scp;
1850 if (TAILQ_FIRST(&scp2->sch_queue) == NULL) {
1851 sce = &tcp_syn_cache[tcp_syn_cache_size];
1852 for (++scp2; scp2 != scp; scp2++) {
1853 if (scp2 >= sce)
1854 scp2 = &tcp_syn_cache[0];
1855 if (TAILQ_FIRST(&scp2->sch_queue) != NULL)
1856 break;
1857 }
1858 }
1859 sc2 = TAILQ_FIRST(&scp2->sch_queue);
1860 if (sc2 == NULL) {
1861 if (sc->sc_ipopts)
1862 (void) m_free(sc->sc_ipopts);
1863 pool_put(&syn_cache_pool, sc);
1864 return;
1865 }
1866 SYN_CACHE_RM(sc2, scp2);
1867 if (sc2->sc_ipopts)
1868 (void) m_free(sc2->sc_ipopts);
1869 pool_put(&syn_cache_pool, sc2);
1870 }
1871
1872 /* Set entry's timer. */
1873 PRT_SLOW_ARM(sc->sc_timer, tcp_syn_cache_timeo);
1874
1875 /* Put it into the bucket. */
1876 TAILQ_INSERT_TAIL(&scp->sch_queue, sc, sc_queue);
1877 if (++scp->sch_length == 1)
1878 LIST_INSERT_HEAD(&tcp_syn_cache_queue, scp, sch_headq);
1879 syn_cache_count++;
1880
1881 tcpstat.tcps_sc_added++;
1882 splx(s);
1883 }
1884
1885 /*
1886 * Walk down the cache list, looking for expired entries in each bucket.
1887 */
1888 void
1889 syn_cache_timer()
1890 {
1891 struct syn_cache_head *scp, *nscp;
1892 struct syn_cache *sc, *nsc;
1893 int s;
1894
1895 s = splsoftnet();
1896 for (scp = LIST_FIRST(&tcp_syn_cache_queue); scp != NULL; scp = nscp) {
1897 #ifdef DIAGNOSTIC
1898 if (TAILQ_FIRST(&scp->sch_queue) == NULL)
1899 panic("syn_cache_timer: queue inconsistency");
1900 #endif
1901 nscp = LIST_NEXT(scp, sch_headq);
1902 for (sc = TAILQ_FIRST(&scp->sch_queue);
1903 sc != NULL && PRT_SLOW_ISEXPIRED(sc->sc_timer);
1904 sc = nsc) {
1905 nsc = TAILQ_NEXT(sc, sc_queue);
1906 tcpstat.tcps_sc_timed_out++;
1907 SYN_CACHE_RM(sc, scp);
1908 if (sc->sc_ipopts)
1909 (void) m_free(sc->sc_ipopts);
1910 pool_put(&syn_cache_pool, sc);
1911 }
1912 }
1913 splx(s);
1914 }
1915
1916 /*
1917 * Find an entry in the syn cache.
1918 */
1919 struct syn_cache *
1920 syn_cache_lookup(ti, headp)
1921 struct tcpiphdr *ti;
1922 struct syn_cache_head **headp;
1923 {
1924 struct syn_cache *sc;
1925 struct syn_cache_head *scp;
1926 u_int32_t hash;
1927 int s;
1928
1929 hash = SYN_HASH(&ti->ti_src, ti->ti_sport, ti->ti_dport);
1930
1931 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
1932 *headp = scp;
1933 s = splsoftnet();
1934 for (sc = TAILQ_FIRST(&scp->sch_queue); sc != NULL;
1935 sc = TAILQ_NEXT(sc, sc_queue)) {
1936 if (sc->sc_hash != hash)
1937 continue;
1938 if (sc->sc_src.s_addr == ti->ti_src.s_addr &&
1939 sc->sc_sport == ti->ti_sport &&
1940 sc->sc_dport == ti->ti_dport &&
1941 sc->sc_dst.s_addr == ti->ti_dst.s_addr) {
1942 splx(s);
1943 return (sc);
1944 }
1945 }
1946 splx(s);
1947 return (NULL);
1948 }
1949
1950 /*
1951 * This function gets called when we receive an ACK for a
1952 * socket in the LISTEN state. We look up the connection
1953 * in the syn cache, and if its there, we pull it out of
1954 * the cache and turn it into a full-blown connection in
1955 * the SYN-RECEIVED state.
1956 *
1957 * The return values may not be immediately obvious, and their effects
1958 * can be subtle, so here they are:
1959 *
1960 * NULL SYN was not found in cache; caller should drop the
1961 * packet and send an RST.
1962 *
1963 * -1 We were unable to create the new connection, and are
1964 * aborting it. An ACK,RST is being sent to the peer
1965 * (unless we got screwey sequence numbners; see below),
1966 * because the 3-way handshake has been completed. Caller
1967 * should not free the mbuf, since we may be using it. If
1968 * we are not, we will free it.
1969 *
1970 * Otherwise, the return value is a pointer to the new socket
1971 * associated with the connection.
1972 */
1973 struct socket *
1974 syn_cache_get(so, m)
1975 struct socket *so;
1976 struct mbuf *m;
1977 {
1978 struct syn_cache *sc;
1979 struct syn_cache_head *scp;
1980 register struct inpcb *inp;
1981 register struct tcpcb *tp = 0;
1982 register struct tcpiphdr *ti;
1983 struct sockaddr_in *sin;
1984 struct mbuf *am;
1985 long win;
1986 int s;
1987
1988 ti = mtod(m, struct tcpiphdr *);
1989 s = splsoftnet();
1990 if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
1991 splx(s);
1992 return (NULL);
1993 }
1994
1995 win = sbspace(&so->so_rcv);
1996 if (win > TCP_MAXWIN)
1997 win = TCP_MAXWIN;
1998
1999 /*
2000 * Verify the sequence and ack numbers.
2001 */
2002 if ((ti->ti_ack != sc->sc_iss + 1) ||
2003 SEQ_LEQ(ti->ti_seq, sc->sc_irs) ||
2004 SEQ_GT(ti->ti_seq, sc->sc_irs + 1 + win)) {
2005 (void) syn_cache_respond(sc, m, ti, win, 0);
2006 splx(s);
2007 return ((struct socket *)(-1));
2008 }
2009
2010 /* Remove this cache entry */
2011 SYN_CACHE_RM(sc, scp);
2012 splx(s);
2013
2014 /*
2015 * Ok, create the full blown connection, and set things up
2016 * as they would have been set up if we had created the
2017 * connection when the SYN arrived. If we can't create
2018 * the connection, abort it.
2019 */
2020 so = sonewconn(so, SS_ISCONNECTED);
2021 if (so == NULL)
2022 goto resetandabort;
2023
2024 inp = sotoinpcb(so);
2025 inp->inp_laddr = sc->sc_dst;
2026 inp->inp_lport = sc->sc_dport;
2027 in_pcbstate(inp, INP_BOUND);
2028 inp->inp_options = ip_srcroute();
2029 if (inp->inp_options == NULL) {
2030 inp->inp_options = sc->sc_ipopts;
2031 sc->sc_ipopts = NULL;
2032 }
2033
2034 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
2035 if (am == NULL)
2036 goto resetandabort;
2037 am->m_len = sizeof(struct sockaddr_in);
2038 sin = mtod(am, struct sockaddr_in *);
2039 sin->sin_family = AF_INET;
2040 sin->sin_len = sizeof(*sin);
2041 sin->sin_addr = sc->sc_src;
2042 sin->sin_port = sc->sc_sport;
2043 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
2044 if (in_pcbconnect(inp, am)) {
2045 (void) m_free(am);
2046 goto resetandabort;
2047 }
2048 (void) m_free(am);
2049
2050 tp = intotcpcb(inp);
2051 if (sc->sc_request_r_scale != 15) {
2052 tp->requested_s_scale = sc->sc_requested_s_scale;
2053 tp->request_r_scale = sc->sc_request_r_scale;
2054 tp->snd_scale = sc->sc_requested_s_scale;
2055 tp->rcv_scale = sc->sc_request_r_scale;
2056 tp->t_flags |= TF_RCVD_SCALE;
2057 }
2058 if (sc->sc_flags & SCF_TIMESTAMP)
2059 tp->t_flags |= TF_RCVD_TSTMP;
2060
2061 tp->t_template = tcp_template(tp);
2062 if (tp->t_template == 0) {
2063 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
2064 so = NULL;
2065 m_freem(m);
2066 goto abort;
2067 }
2068
2069 tp->iss = sc->sc_iss;
2070 tp->irs = sc->sc_irs;
2071 tcp_sendseqinit(tp);
2072 tcp_rcvseqinit(tp);
2073 tp->t_state = TCPS_SYN_RECEIVED;
2074 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
2075 tcpstat.tcps_accepts++;
2076
2077 /* Initialize tp->t_ourmss before we deal with the peer's! */
2078 tp->t_ourmss = sc->sc_ourmaxseg;
2079 tcp_mss_from_peer(tp, sc->sc_peermaxseg);
2080
2081 /*
2082 * Initialize the initial congestion window. If we
2083 * had to retransmit the SYN,ACK, we must initialize cwnd
2084 * to 1 segment (i.e. the Loss Window).
2085 */
2086 if (sc->sc_rexmt_count)
2087 tp->snd_cwnd = tp->t_peermss;
2088 else
2089 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_peermss);
2090
2091 tcp_rmx_rtt(tp);
2092 tp->snd_wl1 = sc->sc_irs;
2093 tp->rcv_up = sc->sc_irs + 1;
2094
2095 /*
2096 * This is what whould have happened in tcp_ouput() when
2097 * the SYN,ACK was sent.
2098 */
2099 tp->snd_up = tp->snd_una;
2100 tp->snd_max = tp->snd_nxt = tp->iss+1;
2101 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
2102 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
2103 tp->rcv_adv = tp->rcv_nxt + win;
2104 tp->last_ack_sent = tp->rcv_nxt;
2105
2106 tcpstat.tcps_sc_completed++;
2107 if (sc->sc_ipopts)
2108 (void) m_free(sc->sc_ipopts);
2109 pool_put(&syn_cache_pool, sc);
2110 return (so);
2111
2112 resetandabort:
2113 (void) tcp_respond(NULL, ti, m, ti->ti_seq+ti->ti_len,
2114 (tcp_seq)0, TH_RST|TH_ACK);
2115 abort:
2116 if (so != NULL)
2117 (void) soabort(so);
2118 if (sc->sc_ipopts)
2119 (void) m_free(sc->sc_ipopts);
2120 pool_put(&syn_cache_pool, sc);
2121 tcpstat.tcps_sc_aborted++;
2122 return ((struct socket *)(-1));
2123 }
2124
2125 /*
2126 * This function is called when we get a RST for a
2127 * non-existant connection, so that we can see if the
2128 * connection is in the syn cache. If it is, zap it.
2129 */
2130
2131 void
2132 syn_cache_reset(ti)
2133 register struct tcpiphdr *ti;
2134 {
2135 struct syn_cache *sc;
2136 struct syn_cache_head *scp;
2137 int s = splsoftnet();
2138
2139 if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
2140 splx(s);
2141 return;
2142 }
2143 if (SEQ_LT(ti->ti_seq,sc->sc_irs) ||
2144 SEQ_GT(ti->ti_seq, sc->sc_irs+1)) {
2145 splx(s);
2146 return;
2147 }
2148 SYN_CACHE_RM(sc, scp);
2149 splx(s);
2150 tcpstat.tcps_sc_reset++;
2151 if (sc->sc_ipopts)
2152 (void) m_free(sc->sc_ipopts);
2153 pool_put(&syn_cache_pool, sc);
2154 }
2155
2156 void
2157 syn_cache_unreach(ip, th)
2158 struct ip *ip;
2159 struct tcphdr *th;
2160 {
2161 struct syn_cache *sc;
2162 struct syn_cache_head *scp;
2163 struct tcpiphdr ti2;
2164 int s;
2165
2166 ti2.ti_src.s_addr = ip->ip_dst.s_addr;
2167 ti2.ti_dst.s_addr = ip->ip_src.s_addr;
2168 ti2.ti_sport = th->th_dport;
2169 ti2.ti_dport = th->th_sport;
2170
2171 s = splsoftnet();
2172 if ((sc = syn_cache_lookup(&ti2, &scp)) == NULL) {
2173 splx(s);
2174 return;
2175 }
2176 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
2177 if (ntohl (th->th_seq) != sc->sc_iss) {
2178 splx(s);
2179 return;
2180 }
2181
2182 /*
2183 * If we've rertransmitted 3 times and this is our second error,
2184 * we remove the entry. Otherwise, we allow it to continue on.
2185 * This prevents us from incorrectly nuking an entry during a
2186 * spurious network outage.
2187 *
2188 * See tcp_notify().
2189 */
2190 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rexmt_count < 3) {
2191 sc->sc_flags |= SCF_UNREACH;
2192 splx(s);
2193 return;
2194 }
2195
2196 SYN_CACHE_RM(sc, scp);
2197 splx(s);
2198 tcpstat.tcps_sc_unreach++;
2199 if (sc->sc_ipopts)
2200 (void) m_free(sc->sc_ipopts);
2201 pool_put(&syn_cache_pool, sc);
2202 }
2203
2204 /*
2205 * Given a LISTEN socket and an inbound SYN request, add
2206 * this to the syn cache, and send back a segment:
2207 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
2208 * to the source.
2209 *
2210 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
2211 * Doing so would require that we hold onto the data and deliver it
2212 * to the application. However, if we are the target of a SYN-flood
2213 * DoS attack, an attacker could send data which would eventually
2214 * consume all available buffer space if it were ACKed. By not ACKing
2215 * the data, we avoid this DoS scenario.
2216 */
2217
2218 int
2219 syn_cache_add(so, m, optp, optlen, oi)
2220 struct socket *so;
2221 struct mbuf *m;
2222 u_char *optp;
2223 int optlen;
2224 struct tcp_opt_info *oi;
2225 {
2226 register struct tcpiphdr *ti;
2227 struct tcpcb tb, *tp;
2228 long win;
2229 struct syn_cache *sc;
2230 struct syn_cache_head *scp;
2231 struct mbuf *ipopts;
2232
2233 tp = sototcpcb(so);
2234 ti = mtod(m, struct tcpiphdr *);
2235
2236 /*
2237 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2238 * in_broadcast() should never return true on a received
2239 * packet with M_BCAST not set.
2240 */
2241 if (m->m_flags & (M_BCAST|M_MCAST) ||
2242 IN_MULTICAST(ti->ti_src.s_addr) ||
2243 IN_MULTICAST(ti->ti_dst.s_addr))
2244 return (0);
2245
2246 /*
2247 * Initialize some local state.
2248 */
2249 win = sbspace(&so->so_rcv);
2250 if (win > TCP_MAXWIN)
2251 win = TCP_MAXWIN;
2252
2253 /*
2254 * Remember the IP options, if any.
2255 */
2256 ipopts = ip_srcroute();
2257
2258 if (optp) {
2259 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
2260 tcp_dooptions(&tb, optp, optlen, ti, oi);
2261 } else
2262 tb.t_flags = 0;
2263
2264 /*
2265 * See if we already have an entry for this connection.
2266 * If we do, resend the SYN,ACK, and remember since the
2267 * initial congestion window must be initialized to 1
2268 * segment when the connection completes.
2269 */
2270 if ((sc = syn_cache_lookup(ti, &scp)) != NULL) {
2271 tcpstat.tcps_sc_dupesyn++;
2272 sc->sc_rexmt_count++;
2273 if (sc->sc_rexmt_count == 0) {
2274 /*
2275 * Eeek! We rolled the counter. Just set it
2276 * to the max value. This shouldn't ever happen,
2277 * but there's no real reason to panic here, since
2278 * the count doesn't have to be very precise.
2279 */
2280 sc->sc_rexmt_count = USHRT_MAX;
2281 }
2282
2283 if (ipopts) {
2284 /*
2285 * If we were remembering a previous source route,
2286 * forget it and use the new one we've been given.
2287 */
2288 if (sc->sc_ipopts)
2289 (void) m_free(sc->sc_ipopts);
2290 sc->sc_ipopts = ipopts;
2291 }
2292
2293 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2294 tcpstat.tcps_sndacks++;
2295 tcpstat.tcps_sndtotal++;
2296 }
2297 return (1);
2298 }
2299
2300 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
2301 if (sc == NULL) {
2302 if (ipopts)
2303 (void) m_free(ipopts);
2304 return (0);
2305 }
2306
2307 /*
2308 * Fill in the cache, and put the necessary IP and TCP
2309 * options into the reply.
2310 */
2311 sc->sc_src.s_addr = ti->ti_src.s_addr;
2312 sc->sc_dst.s_addr = ti->ti_dst.s_addr;
2313 sc->sc_sport = ti->ti_sport;
2314 sc->sc_dport = ti->ti_dport;
2315 sc->sc_flags = 0;
2316 sc->sc_ipopts = ipopts;
2317 sc->sc_irs = ti->ti_seq;
2318 sc->sc_iss = tcp_new_iss(sc, sizeof(struct syn_cache), 0);
2319 sc->sc_peermaxseg = oi->maxseg;
2320 sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
2321 m->m_pkthdr.rcvif : NULL);
2322 if (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP))
2323 sc->sc_flags |= SCF_TIMESTAMP;
2324 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2325 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2326 sc->sc_requested_s_scale = tb.requested_s_scale;
2327 sc->sc_request_r_scale = 0;
2328 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
2329 TCP_MAXWIN << sc->sc_request_r_scale <
2330 so->so_rcv.sb_hiwat)
2331 sc->sc_request_r_scale++;
2332 } else {
2333 sc->sc_requested_s_scale = 15;
2334 sc->sc_request_r_scale = 15;
2335 }
2336 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2337 syn_cache_insert(sc);
2338 tcpstat.tcps_sndacks++;
2339 tcpstat.tcps_sndtotal++;
2340 } else {
2341 if (sc->sc_ipopts)
2342 (void) m_free(sc->sc_ipopts);
2343 pool_put(&syn_cache_pool, sc);
2344 tcpstat.tcps_sc_dropped++;
2345 }
2346 return (1);
2347 }
2348
2349 int
2350 syn_cache_respond(sc, m, ti, win, ts)
2351 struct syn_cache *sc;
2352 struct mbuf *m;
2353 register struct tcpiphdr *ti;
2354 long win;
2355 u_long ts;
2356 {
2357 u_int8_t *optp;
2358 int optlen;
2359
2360 /*
2361 * Tack on the TCP options. If there isn't enough trailing
2362 * space for them, move up the fixed header to make space.
2363 */
2364 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
2365 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
2366 if (optlen > M_TRAILINGSPACE(m)) {
2367 if (M_LEADINGSPACE(m) >= optlen) {
2368 m->m_data -= optlen;
2369 m->m_len += optlen;
2370 } else {
2371 struct mbuf *m0 = m;
2372 if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL) {
2373 m_freem(m0);
2374 return (ENOBUFS);
2375 }
2376 MH_ALIGN(m, sizeof(*ti) + optlen);
2377 m->m_next = m0; /* this gets freed below */
2378 }
2379 bcopy((caddr_t)ti, mtod(m, caddr_t), sizeof(*ti));
2380 ti = mtod(m, struct tcpiphdr *);
2381 }
2382
2383 optp = (u_int8_t *)(ti + 1);
2384 optp[0] = TCPOPT_MAXSEG;
2385 optp[1] = 4;
2386 optp[2] = (sc->sc_ourmaxseg >> 8) & 0xff;
2387 optp[3] = sc->sc_ourmaxseg & 0xff;
2388 optlen = 4;
2389
2390 if (sc->sc_request_r_scale != 15) {
2391 *((u_int32_t *)(optp + optlen)) = htonl(TCPOPT_NOP << 24 |
2392 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
2393 sc->sc_request_r_scale);
2394 optlen += 4;
2395 }
2396
2397 if (sc->sc_flags & SCF_TIMESTAMP) {
2398 u_int32_t *lp = (u_int32_t *)(optp + optlen);
2399 /* Form timestamp option as shown in appendix A of RFC 1323. */
2400 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
2401 *lp++ = htonl(tcp_now);
2402 *lp = htonl(ts);
2403 optlen += TCPOLEN_TSTAMP_APPA;
2404 }
2405
2406 /*
2407 * Toss any trailing mbufs. No need to worry about
2408 * m_len and m_pkthdr.len, since tcp_respond() will
2409 * unconditionally set them.
2410 */
2411 if (m->m_next) {
2412 m_freem(m->m_next);
2413 m->m_next = NULL;
2414 }
2415
2416 /*
2417 * Fill in the fields that tcp_respond() will not touch, and
2418 * then send the response.
2419 */
2420 ti->ti_off = (sizeof(struct tcphdr) + optlen) >> 2;
2421 ti->ti_win = htons(win);
2422 return (tcp_respond(NULL, ti, m, sc->sc_irs + 1, sc->sc_iss,
2423 TH_SYN|TH_ACK));
2424 }
2425