tcp_input.c revision 1.63 1 /* $NetBSD: tcp_input.c,v 1.63 1998/08/02 00:35:51 thorpej 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 }
622 } else {
623 /*
624 * Received a SYN.
625 */
626 if (in_hosteq(ti->ti_src, ti->ti_dst) &&
627 ti->ti_sport == ti->ti_dport) {
628 /*
629 * LISTEN socket received a SYN
630 * from itself? This can't possibly
631 * be valid; drop the packet.
632 */
633 tcpstat.tcps_badsyn++;
634 goto drop;
635 }
636 /*
637 * SYN looks ok; create compressed TCP
638 * state for it.
639 */
640 if (so->so_qlen <= so->so_qlimit &&
641 syn_cache_add(so, m, optp, optlen, &opti))
642 m = NULL;
643 }
644 goto drop;
645 }
646 }
647
648 after_listen:
649 #ifdef DIAGNOSTIC
650 /*
651 * Should not happen now that all embryonic connections
652 * are handled with compressed state.
653 */
654 if (tp->t_state == TCPS_LISTEN)
655 panic("tcp_input: TCPS_LISTEN");
656 #endif
657
658 /*
659 * Segment received on connection.
660 * Reset idle time and keep-alive timer.
661 */
662 tp->t_idle = 0;
663 if (TCPS_HAVEESTABLISHED(tp->t_state))
664 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
665
666 /*
667 * Process options.
668 */
669 if (optp)
670 tcp_dooptions(tp, optp, optlen, ti, &opti);
671
672 /*
673 * Header prediction: check for the two common cases
674 * of a uni-directional data xfer. If the packet has
675 * no control flags, is in-sequence, the window didn't
676 * change and we're not retransmitting, it's a
677 * candidate. If the length is zero and the ack moved
678 * forward, we're the sender side of the xfer. Just
679 * free the data acked & wake any higher level process
680 * that was blocked waiting for space. If the length
681 * is non-zero and the ack didn't move, we're the
682 * receiver side. If we're getting packets in-order
683 * (the reassembly queue is empty), add the data to
684 * the socket buffer and note that we need a delayed ack.
685 */
686 if (tp->t_state == TCPS_ESTABLISHED &&
687 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
688 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
689 ti->ti_seq == tp->rcv_nxt &&
690 tiwin && tiwin == tp->snd_wnd &&
691 tp->snd_nxt == tp->snd_max) {
692
693 /*
694 * If last ACK falls within this segment's sequence numbers,
695 * record the timestamp.
696 */
697 if (opti.ts_present &&
698 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
699 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
700 tp->ts_recent_age = tcp_now;
701 tp->ts_recent = opti.ts_val;
702 }
703
704 if (ti->ti_len == 0) {
705 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
706 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
707 tp->snd_cwnd >= tp->snd_wnd &&
708 tp->t_dupacks < tcprexmtthresh) {
709 /*
710 * this is a pure ack for outstanding data.
711 */
712 ++tcpstat.tcps_predack;
713 if (opti.ts_present)
714 tcp_xmit_timer(tp,
715 tcp_now-opti.ts_ecr+1);
716 else if (tp->t_rtt &&
717 SEQ_GT(ti->ti_ack, tp->t_rtseq))
718 tcp_xmit_timer(tp, tp->t_rtt);
719 acked = ti->ti_ack - tp->snd_una;
720 tcpstat.tcps_rcvackpack++;
721 tcpstat.tcps_rcvackbyte += acked;
722 sbdrop(&so->so_snd, acked);
723 tp->snd_una = ti->ti_ack;
724 m_freem(m);
725
726 /*
727 * If all outstanding data are acked, stop
728 * retransmit timer, otherwise restart timer
729 * using current (possibly backed-off) value.
730 * If process is waiting for space,
731 * wakeup/selwakeup/signal. If data
732 * are ready to send, let tcp_output
733 * decide between more output or persist.
734 */
735 if (tp->snd_una == tp->snd_max)
736 TCP_TIMER_DISARM(tp, TCPT_REXMT);
737 else if (TCP_TIMER_ISARMED(tp,
738 TCPT_PERSIST) == 0)
739 TCP_TIMER_ARM(tp, TCPT_REXMT,
740 tp->t_rxtcur);
741
742 sowwakeup(so);
743 if (so->so_snd.sb_cc)
744 (void) tcp_output(tp);
745 return;
746 }
747 } else if (ti->ti_ack == tp->snd_una &&
748 tp->segq.lh_first == NULL &&
749 ti->ti_len <= sbspace(&so->so_rcv)) {
750 /*
751 * this is a pure, in-sequence data packet
752 * with nothing on the reassembly queue and
753 * we have enough buffer space to take it.
754 */
755 ++tcpstat.tcps_preddat;
756 tp->rcv_nxt += ti->ti_len;
757 tcpstat.tcps_rcvpack++;
758 tcpstat.tcps_rcvbyte += ti->ti_len;
759 /*
760 * Drop TCP, IP headers and TCP options then add data
761 * to socket buffer.
762 */
763 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
764 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
765 sbappend(&so->so_rcv, m);
766 sorwakeup(so);
767 TCP_SETUP_ACK(tp, ti);
768 if (tp->t_flags & TF_ACKNOW)
769 (void) tcp_output(tp);
770 return;
771 }
772 }
773
774 /*
775 * Drop TCP, IP headers and TCP options.
776 */
777 hdroptlen = sizeof(struct tcpiphdr) + off - sizeof(struct tcphdr);
778 m->m_data += hdroptlen;
779 m->m_len -= hdroptlen;
780
781 /*
782 * Calculate amount of space in receive window,
783 * and then do TCP input processing.
784 * Receive window is amount of space in rcv queue,
785 * but not less than advertised window.
786 */
787 { int win;
788
789 win = sbspace(&so->so_rcv);
790 if (win < 0)
791 win = 0;
792 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
793 }
794
795 switch (tp->t_state) {
796
797 /*
798 * If the state is SYN_SENT:
799 * if seg contains an ACK, but not for our SYN, drop the input.
800 * if seg contains a RST, then drop the connection.
801 * if seg does not contain SYN, then drop it.
802 * Otherwise this is an acceptable SYN segment
803 * initialize tp->rcv_nxt and tp->irs
804 * if seg contains ack then advance tp->snd_una
805 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
806 * arrange for segment to be acked (eventually)
807 * continue processing rest of data/controls, beginning with URG
808 */
809 case TCPS_SYN_SENT:
810 if ((tiflags & TH_ACK) &&
811 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
812 SEQ_GT(ti->ti_ack, tp->snd_max)))
813 goto dropwithreset;
814 if (tiflags & TH_RST) {
815 if (tiflags & TH_ACK)
816 tp = tcp_drop(tp, ECONNREFUSED);
817 goto drop;
818 }
819 if ((tiflags & TH_SYN) == 0)
820 goto drop;
821 if (tiflags & TH_ACK) {
822 tp->snd_una = ti->ti_ack;
823 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
824 tp->snd_nxt = tp->snd_una;
825 }
826 TCP_TIMER_DISARM(tp, TCPT_REXMT);
827 tp->irs = ti->ti_seq;
828 tcp_rcvseqinit(tp);
829 tp->t_flags |= TF_ACKNOW;
830 tcp_mss_from_peer(tp, opti.maxseg);
831
832 /*
833 * Initialize the initial congestion window. If we
834 * had to retransmit the SYN, we must initialize cwnd
835 * to 1 segment (i.e. the Loss Window).
836 */
837 if (tp->t_flags & TF_SYN_REXMT)
838 tp->snd_cwnd = tp->t_peermss;
839 else
840 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
841 tp->t_peermss);
842
843 tcp_rmx_rtt(tp);
844 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
845 tcpstat.tcps_connects++;
846 soisconnected(so);
847 tcp_established(tp);
848 /* Do window scaling on this connection? */
849 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
850 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
851 tp->snd_scale = tp->requested_s_scale;
852 tp->rcv_scale = tp->request_r_scale;
853 }
854 (void) tcp_reass(tp, (struct tcpiphdr *)0,
855 (struct mbuf *)0);
856 /*
857 * if we didn't have to retransmit the SYN,
858 * use its rtt as our initial srtt & rtt var.
859 */
860 if (tp->t_rtt)
861 tcp_xmit_timer(tp, tp->t_rtt);
862 } else
863 tp->t_state = TCPS_SYN_RECEIVED;
864
865 /*
866 * Advance ti->ti_seq to correspond to first data byte.
867 * If data, trim to stay within window,
868 * dropping FIN if necessary.
869 */
870 ti->ti_seq++;
871 if (ti->ti_len > tp->rcv_wnd) {
872 todrop = ti->ti_len - tp->rcv_wnd;
873 m_adj(m, -todrop);
874 ti->ti_len = tp->rcv_wnd;
875 tiflags &= ~TH_FIN;
876 tcpstat.tcps_rcvpackafterwin++;
877 tcpstat.tcps_rcvbyteafterwin += todrop;
878 }
879 tp->snd_wl1 = ti->ti_seq - 1;
880 tp->rcv_up = ti->ti_seq;
881 goto step6;
882
883 /*
884 * If the state is SYN_RECEIVED:
885 * If seg contains an ACK, but not for our SYN, drop the input
886 * and generate an RST. See page 36, rfc793
887 */
888 case TCPS_SYN_RECEIVED:
889 if ((tiflags & TH_ACK) &&
890 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
891 SEQ_GT(ti->ti_ack, tp->snd_max)))
892 goto dropwithreset;
893 break;
894 }
895
896 /*
897 * States other than LISTEN or SYN_SENT.
898 * First check timestamp, if present.
899 * Then check that at least some bytes of segment are within
900 * receive window. If segment begins before rcv_nxt,
901 * drop leading data (and SYN); if nothing left, just ack.
902 *
903 * RFC 1323 PAWS: If we have a timestamp reply on this segment
904 * and it's less than ts_recent, drop it.
905 */
906 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
907 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
908
909 /* Check to see if ts_recent is over 24 days old. */
910 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
911 /*
912 * Invalidate ts_recent. If this segment updates
913 * ts_recent, the age will be reset later and ts_recent
914 * will get a valid value. If it does not, setting
915 * ts_recent to zero will at least satisfy the
916 * requirement that zero be placed in the timestamp
917 * echo reply when ts_recent isn't valid. The
918 * age isn't reset until we get a valid ts_recent
919 * because we don't want out-of-order segments to be
920 * dropped when ts_recent is old.
921 */
922 tp->ts_recent = 0;
923 } else {
924 tcpstat.tcps_rcvduppack++;
925 tcpstat.tcps_rcvdupbyte += ti->ti_len;
926 tcpstat.tcps_pawsdrop++;
927 goto dropafterack;
928 }
929 }
930
931 todrop = tp->rcv_nxt - ti->ti_seq;
932 if (todrop > 0) {
933 if (tiflags & TH_SYN) {
934 tiflags &= ~TH_SYN;
935 ti->ti_seq++;
936 if (ti->ti_urp > 1)
937 ti->ti_urp--;
938 else {
939 tiflags &= ~TH_URG;
940 ti->ti_urp = 0;
941 }
942 todrop--;
943 }
944 if (todrop > ti->ti_len ||
945 (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
946 /*
947 * Any valid FIN must be to the left of the window.
948 * At this point the FIN must be a duplicate or
949 * out of sequence; drop it.
950 */
951 tiflags &= ~TH_FIN;
952 /*
953 * Send an ACK to resynchronize and drop any data.
954 * But keep on processing for RST or ACK.
955 */
956 tp->t_flags |= TF_ACKNOW;
957 todrop = ti->ti_len;
958 tcpstat.tcps_rcvdupbyte += todrop;
959 tcpstat.tcps_rcvduppack++;
960 } else {
961 tcpstat.tcps_rcvpartduppack++;
962 tcpstat.tcps_rcvpartdupbyte += todrop;
963 }
964 m_adj(m, todrop);
965 ti->ti_seq += todrop;
966 ti->ti_len -= todrop;
967 if (ti->ti_urp > todrop)
968 ti->ti_urp -= todrop;
969 else {
970 tiflags &= ~TH_URG;
971 ti->ti_urp = 0;
972 }
973 }
974
975 /*
976 * If new data are received on a connection after the
977 * user processes are gone, then RST the other end.
978 */
979 if ((so->so_state & SS_NOFDREF) &&
980 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
981 tp = tcp_close(tp);
982 tcpstat.tcps_rcvafterclose++;
983 goto dropwithreset;
984 }
985
986 /*
987 * If segment ends after window, drop trailing data
988 * (and PUSH and FIN); if nothing left, just ACK.
989 */
990 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
991 if (todrop > 0) {
992 tcpstat.tcps_rcvpackafterwin++;
993 if (todrop >= ti->ti_len) {
994 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
995 /*
996 * If a new connection request is received
997 * while in TIME_WAIT, drop the old connection
998 * and start over if the sequence numbers
999 * are above the previous ones.
1000 */
1001 if (tiflags & TH_SYN &&
1002 tp->t_state == TCPS_TIME_WAIT &&
1003 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1004 iss = tcp_new_iss(tp, sizeof(struct tcpcb),
1005 tp->rcv_nxt);
1006 tp = tcp_close(tp);
1007 /*
1008 * We have already advanced the mbuf
1009 * pointers past the IP+TCP headers and
1010 * options. Restore those pointers before
1011 * attempting to use the TCP header again.
1012 */
1013 m->m_data -= hdroptlen;
1014 m->m_len += hdroptlen;
1015 goto findpcb;
1016 }
1017 /*
1018 * If window is closed can only take segments at
1019 * window edge, and have to drop data and PUSH from
1020 * incoming segments. Continue processing, but
1021 * remember to ack. Otherwise, drop segment
1022 * and ack.
1023 */
1024 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1025 tp->t_flags |= TF_ACKNOW;
1026 tcpstat.tcps_rcvwinprobe++;
1027 } else
1028 goto dropafterack;
1029 } else
1030 tcpstat.tcps_rcvbyteafterwin += todrop;
1031 m_adj(m, -todrop);
1032 ti->ti_len -= todrop;
1033 tiflags &= ~(TH_PUSH|TH_FIN);
1034 }
1035
1036 /*
1037 * If last ACK falls within this segment's sequence numbers,
1038 * and the timestamp is newer, record it.
1039 */
1040 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
1041 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
1042 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
1043 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
1044 tp->ts_recent_age = tcp_now;
1045 tp->ts_recent = opti.ts_val;
1046 }
1047
1048 /*
1049 * If the RST bit is set examine the state:
1050 * SYN_RECEIVED STATE:
1051 * If passive open, return to LISTEN state.
1052 * If active open, inform user that connection was refused.
1053 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1054 * Inform user that connection was reset, and close tcb.
1055 * CLOSING, LAST_ACK, TIME_WAIT STATES
1056 * Close the tcb.
1057 */
1058 if (tiflags&TH_RST) switch (tp->t_state) {
1059
1060 case TCPS_SYN_RECEIVED:
1061 so->so_error = ECONNREFUSED;
1062 goto close;
1063
1064 case TCPS_ESTABLISHED:
1065 case TCPS_FIN_WAIT_1:
1066 case TCPS_FIN_WAIT_2:
1067 case TCPS_CLOSE_WAIT:
1068 so->so_error = ECONNRESET;
1069 close:
1070 tp->t_state = TCPS_CLOSED;
1071 tcpstat.tcps_drops++;
1072 tp = tcp_close(tp);
1073 goto drop;
1074
1075 case TCPS_CLOSING:
1076 case TCPS_LAST_ACK:
1077 case TCPS_TIME_WAIT:
1078 tp = tcp_close(tp);
1079 goto drop;
1080 }
1081
1082 /*
1083 * If a SYN is in the window, then this is an
1084 * error and we send an RST and drop the connection.
1085 */
1086 if (tiflags & TH_SYN) {
1087 tp = tcp_drop(tp, ECONNRESET);
1088 goto dropwithreset;
1089 }
1090
1091 /*
1092 * If the ACK bit is off we drop the segment and return.
1093 */
1094 if ((tiflags & TH_ACK) == 0)
1095 goto drop;
1096
1097 /*
1098 * Ack processing.
1099 */
1100 switch (tp->t_state) {
1101
1102 /*
1103 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1104 * ESTABLISHED state and continue processing, otherwise
1105 * send an RST.
1106 */
1107 case TCPS_SYN_RECEIVED:
1108 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1109 SEQ_GT(ti->ti_ack, tp->snd_max))
1110 goto dropwithreset;
1111 tcpstat.tcps_connects++;
1112 soisconnected(so);
1113 tcp_established(tp);
1114 /* Do window scaling? */
1115 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1116 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1117 tp->snd_scale = tp->requested_s_scale;
1118 tp->rcv_scale = tp->request_r_scale;
1119 }
1120 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
1121 tp->snd_wl1 = ti->ti_seq - 1;
1122 /* fall into ... */
1123
1124 /*
1125 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1126 * ACKs. If the ack is in the range
1127 * tp->snd_una < ti->ti_ack <= tp->snd_max
1128 * then advance tp->snd_una to ti->ti_ack and drop
1129 * data from the retransmission queue. If this ACK reflects
1130 * more up to date window information we update our window information.
1131 */
1132 case TCPS_ESTABLISHED:
1133 case TCPS_FIN_WAIT_1:
1134 case TCPS_FIN_WAIT_2:
1135 case TCPS_CLOSE_WAIT:
1136 case TCPS_CLOSING:
1137 case TCPS_LAST_ACK:
1138 case TCPS_TIME_WAIT:
1139
1140 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1141 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1142 tcpstat.tcps_rcvdupack++;
1143 /*
1144 * If we have outstanding data (other than
1145 * a window probe), this is a completely
1146 * duplicate ack (ie, window info didn't
1147 * change), the ack is the biggest we've
1148 * seen and we've seen exactly our rexmt
1149 * threshhold of them, assume a packet
1150 * has been dropped and retransmit it.
1151 * Kludge snd_nxt & the congestion
1152 * window so we send only this one
1153 * packet.
1154 *
1155 * We know we're losing at the current
1156 * window size so do congestion avoidance
1157 * (set ssthresh to half the current window
1158 * and pull our congestion window back to
1159 * the new ssthresh).
1160 *
1161 * Dup acks mean that packets have left the
1162 * network (they're now cached at the receiver)
1163 * so bump cwnd by the amount in the receiver
1164 * to keep a constant cwnd packets in the
1165 * network.
1166 */
1167 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
1168 ti->ti_ack != tp->snd_una)
1169 tp->t_dupacks = 0;
1170 else if (++tp->t_dupacks == tcprexmtthresh) {
1171 tcp_seq onxt = tp->snd_nxt;
1172 u_int win =
1173 min(tp->snd_wnd, tp->snd_cwnd) /
1174 2 / tp->t_segsz;
1175
1176 if (win < 2)
1177 win = 2;
1178 tp->snd_ssthresh = win * tp->t_segsz;
1179 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1180 tp->t_rtt = 0;
1181 tp->snd_nxt = ti->ti_ack;
1182 tp->snd_cwnd = tp->t_segsz;
1183 (void) tcp_output(tp);
1184 tp->snd_cwnd = tp->snd_ssthresh +
1185 tp->t_segsz * tp->t_dupacks;
1186 if (SEQ_GT(onxt, tp->snd_nxt))
1187 tp->snd_nxt = onxt;
1188 goto drop;
1189 } else if (tp->t_dupacks > tcprexmtthresh) {
1190 tp->snd_cwnd += tp->t_segsz;
1191 (void) tcp_output(tp);
1192 goto drop;
1193 }
1194 } else
1195 tp->t_dupacks = 0;
1196 break;
1197 }
1198 /*
1199 * If the congestion window was inflated to account
1200 * for the other side's cached packets, retract it.
1201 */
1202 if (tp->t_dupacks >= tcprexmtthresh &&
1203 tp->snd_cwnd > tp->snd_ssthresh)
1204 tp->snd_cwnd = tp->snd_ssthresh;
1205 tp->t_dupacks = 0;
1206 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1207 tcpstat.tcps_rcvacktoomuch++;
1208 goto dropafterack;
1209 }
1210 acked = ti->ti_ack - tp->snd_una;
1211 tcpstat.tcps_rcvackpack++;
1212 tcpstat.tcps_rcvackbyte += acked;
1213
1214 /*
1215 * If we have a timestamp reply, update smoothed
1216 * round trip time. If no timestamp is present but
1217 * transmit timer is running and timed sequence
1218 * number was acked, update smoothed round trip time.
1219 * Since we now have an rtt measurement, cancel the
1220 * timer backoff (cf., Phil Karn's retransmit alg.).
1221 * Recompute the initial retransmit timer.
1222 */
1223 if (opti.ts_present)
1224 tcp_xmit_timer(tp, tcp_now - opti.ts_ecr + 1);
1225 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1226 tcp_xmit_timer(tp,tp->t_rtt);
1227
1228 /*
1229 * If all outstanding data is acked, stop retransmit
1230 * timer and remember to restart (more output or persist).
1231 * If there is more data to be acked, restart retransmit
1232 * timer, using current (possibly backed-off) value.
1233 */
1234 if (ti->ti_ack == tp->snd_max) {
1235 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1236 needoutput = 1;
1237 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1238 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1239 /*
1240 * When new data is acked, open the congestion window.
1241 * If the window gives us less than ssthresh packets
1242 * in flight, open exponentially (segsz per packet).
1243 * Otherwise open linearly: segsz per window
1244 * (segsz^2 / cwnd per packet), plus a constant
1245 * fraction of a packet (segsz/8) to help larger windows
1246 * open quickly enough.
1247 */
1248 {
1249 register u_int cw = tp->snd_cwnd;
1250 register u_int incr = tp->t_segsz;
1251
1252 if (cw > tp->snd_ssthresh)
1253 incr = incr * incr / cw;
1254 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1255 }
1256 if (acked > so->so_snd.sb_cc) {
1257 tp->snd_wnd -= so->so_snd.sb_cc;
1258 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1259 ourfinisacked = 1;
1260 } else {
1261 sbdrop(&so->so_snd, acked);
1262 tp->snd_wnd -= acked;
1263 ourfinisacked = 0;
1264 }
1265 sowwakeup(so);
1266 tp->snd_una = ti->ti_ack;
1267 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1268 tp->snd_nxt = tp->snd_una;
1269
1270 switch (tp->t_state) {
1271
1272 /*
1273 * In FIN_WAIT_1 STATE in addition to the processing
1274 * for the ESTABLISHED state if our FIN is now acknowledged
1275 * then enter FIN_WAIT_2.
1276 */
1277 case TCPS_FIN_WAIT_1:
1278 if (ourfinisacked) {
1279 /*
1280 * If we can't receive any more
1281 * data, then closing user can proceed.
1282 * Starting the timer is contrary to the
1283 * specification, but if we don't get a FIN
1284 * we'll hang forever.
1285 */
1286 if (so->so_state & SS_CANTRCVMORE) {
1287 soisdisconnected(so);
1288 TCP_TIMER_ARM(tp, TCPT_2MSL,
1289 tcp_maxidle);
1290 }
1291 tp->t_state = TCPS_FIN_WAIT_2;
1292 }
1293 break;
1294
1295 /*
1296 * In CLOSING STATE in addition to the processing for
1297 * the ESTABLISHED state if the ACK acknowledges our FIN
1298 * then enter the TIME-WAIT state, otherwise ignore
1299 * the segment.
1300 */
1301 case TCPS_CLOSING:
1302 if (ourfinisacked) {
1303 tp->t_state = TCPS_TIME_WAIT;
1304 tcp_canceltimers(tp);
1305 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1306 soisdisconnected(so);
1307 }
1308 break;
1309
1310 /*
1311 * In LAST_ACK, we may still be waiting for data to drain
1312 * and/or to be acked, as well as for the ack of our FIN.
1313 * If our FIN is now acknowledged, delete the TCB,
1314 * enter the closed state and return.
1315 */
1316 case TCPS_LAST_ACK:
1317 if (ourfinisacked) {
1318 tp = tcp_close(tp);
1319 goto drop;
1320 }
1321 break;
1322
1323 /*
1324 * In TIME_WAIT state the only thing that should arrive
1325 * is a retransmission of the remote FIN. Acknowledge
1326 * it and restart the finack timer.
1327 */
1328 case TCPS_TIME_WAIT:
1329 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1330 goto dropafterack;
1331 }
1332 }
1333
1334 step6:
1335 /*
1336 * Update window information.
1337 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1338 */
1339 if (((tiflags & TH_ACK) && SEQ_LT(tp->snd_wl1, ti->ti_seq)) ||
1340 (tp->snd_wl1 == ti->ti_seq && SEQ_LT(tp->snd_wl2, ti->ti_ack)) ||
1341 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)) {
1342 /* keep track of pure window updates */
1343 if (ti->ti_len == 0 &&
1344 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1345 tcpstat.tcps_rcvwinupd++;
1346 tp->snd_wnd = tiwin;
1347 tp->snd_wl1 = ti->ti_seq;
1348 tp->snd_wl2 = ti->ti_ack;
1349 if (tp->snd_wnd > tp->max_sndwnd)
1350 tp->max_sndwnd = tp->snd_wnd;
1351 needoutput = 1;
1352 }
1353
1354 /*
1355 * Process segments with URG.
1356 */
1357 if ((tiflags & TH_URG) && ti->ti_urp &&
1358 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1359 /*
1360 * This is a kludge, but if we receive and accept
1361 * random urgent pointers, we'll crash in
1362 * soreceive. It's hard to imagine someone
1363 * actually wanting to send this much urgent data.
1364 */
1365 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1366 ti->ti_urp = 0; /* XXX */
1367 tiflags &= ~TH_URG; /* XXX */
1368 goto dodata; /* XXX */
1369 }
1370 /*
1371 * If this segment advances the known urgent pointer,
1372 * then mark the data stream. This should not happen
1373 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1374 * a FIN has been received from the remote side.
1375 * In these states we ignore the URG.
1376 *
1377 * According to RFC961 (Assigned Protocols),
1378 * the urgent pointer points to the last octet
1379 * of urgent data. We continue, however,
1380 * to consider it to indicate the first octet
1381 * of data past the urgent section as the original
1382 * spec states (in one of two places).
1383 */
1384 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1385 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1386 so->so_oobmark = so->so_rcv.sb_cc +
1387 (tp->rcv_up - tp->rcv_nxt) - 1;
1388 if (so->so_oobmark == 0)
1389 so->so_state |= SS_RCVATMARK;
1390 sohasoutofband(so);
1391 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1392 }
1393 /*
1394 * Remove out of band data so doesn't get presented to user.
1395 * This can happen independent of advancing the URG pointer,
1396 * but if two URG's are pending at once, some out-of-band
1397 * data may creep in... ick.
1398 */
1399 if (ti->ti_urp <= (u_int16_t) ti->ti_len
1400 #ifdef SO_OOBINLINE
1401 && (so->so_options & SO_OOBINLINE) == 0
1402 #endif
1403 )
1404 tcp_pulloutofband(so, ti, m);
1405 } else
1406 /*
1407 * If no out of band data is expected,
1408 * pull receive urgent pointer along
1409 * with the receive window.
1410 */
1411 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1412 tp->rcv_up = tp->rcv_nxt;
1413 dodata: /* XXX */
1414
1415 /*
1416 * Process the segment text, merging it into the TCP sequencing queue,
1417 * and arranging for acknowledgment of receipt if necessary.
1418 * This process logically involves adjusting tp->rcv_wnd as data
1419 * is presented to the user (this happens in tcp_usrreq.c,
1420 * case PRU_RCVD). If a FIN has already been received on this
1421 * connection then we just ignore the text.
1422 */
1423 if ((ti->ti_len || (tiflags & TH_FIN)) &&
1424 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1425 TCP_REASS(tp, ti, m, so, tiflags);
1426 /*
1427 * Note the amount of data that peer has sent into
1428 * our window, in order to estimate the sender's
1429 * buffer size.
1430 */
1431 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1432 } else {
1433 m_freem(m);
1434 tiflags &= ~TH_FIN;
1435 }
1436
1437 /*
1438 * If FIN is received ACK the FIN and let the user know
1439 * that the connection is closing. Ignore a FIN received before
1440 * the connection is fully established.
1441 */
1442 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
1443 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1444 socantrcvmore(so);
1445 tp->t_flags |= TF_ACKNOW;
1446 tp->rcv_nxt++;
1447 }
1448 switch (tp->t_state) {
1449
1450 /*
1451 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
1452 */
1453 case TCPS_ESTABLISHED:
1454 tp->t_state = TCPS_CLOSE_WAIT;
1455 break;
1456
1457 /*
1458 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1459 * enter the CLOSING state.
1460 */
1461 case TCPS_FIN_WAIT_1:
1462 tp->t_state = TCPS_CLOSING;
1463 break;
1464
1465 /*
1466 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1467 * starting the time-wait timer, turning off the other
1468 * standard timers.
1469 */
1470 case TCPS_FIN_WAIT_2:
1471 tp->t_state = TCPS_TIME_WAIT;
1472 tcp_canceltimers(tp);
1473 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1474 soisdisconnected(so);
1475 break;
1476
1477 /*
1478 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1479 */
1480 case TCPS_TIME_WAIT:
1481 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1482 break;
1483 }
1484 }
1485 if (so->so_options & SO_DEBUG)
1486 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1487
1488 /*
1489 * Return any desired output.
1490 */
1491 if (needoutput || (tp->t_flags & TF_ACKNOW))
1492 (void) tcp_output(tp);
1493 return;
1494
1495 badsyn:
1496 /*
1497 * Received a bad SYN. Increment counters and dropwithreset.
1498 */
1499 tcpstat.tcps_badsyn++;
1500 tp = NULL;
1501 goto dropwithreset;
1502
1503 dropafterack:
1504 /*
1505 * Generate an ACK dropping incoming segment if it occupies
1506 * sequence space, where the ACK reflects our state.
1507 */
1508 if (tiflags & TH_RST)
1509 goto drop;
1510 m_freem(m);
1511 tp->t_flags |= TF_ACKNOW;
1512 (void) tcp_output(tp);
1513 return;
1514
1515 dropwithreset:
1516 /*
1517 * Generate a RST, dropping incoming segment.
1518 * Make ACK acceptable to originator of segment.
1519 * Don't bother to respond if destination was broadcast/multicast.
1520 */
1521 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1522 IN_MULTICAST(ti->ti_dst.s_addr))
1523 goto drop;
1524 if (tiflags & TH_ACK)
1525 (void)tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1526 else {
1527 if (tiflags & TH_SYN)
1528 ti->ti_len++;
1529 (void)tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1530 TH_RST|TH_ACK);
1531 }
1532 return;
1533
1534 drop:
1535 /*
1536 * Drop space held by incoming segment and return.
1537 */
1538 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1539 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1540 m_freem(m);
1541 return;
1542 }
1543
1544 void
1545 tcp_dooptions(tp, cp, cnt, ti, oi)
1546 struct tcpcb *tp;
1547 u_char *cp;
1548 int cnt;
1549 struct tcpiphdr *ti;
1550 struct tcp_opt_info *oi;
1551 {
1552 u_int16_t mss;
1553 int opt, optlen;
1554
1555 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1556 opt = cp[0];
1557 if (opt == TCPOPT_EOL)
1558 break;
1559 if (opt == TCPOPT_NOP)
1560 optlen = 1;
1561 else {
1562 optlen = cp[1];
1563 if (optlen <= 0)
1564 break;
1565 }
1566 switch (opt) {
1567
1568 default:
1569 continue;
1570
1571 case TCPOPT_MAXSEG:
1572 if (optlen != TCPOLEN_MAXSEG)
1573 continue;
1574 if (!(ti->ti_flags & TH_SYN))
1575 continue;
1576 bcopy(cp + 2, &mss, sizeof(mss));
1577 oi->maxseg = ntohs(mss);
1578 break;
1579
1580 case TCPOPT_WINDOW:
1581 if (optlen != TCPOLEN_WINDOW)
1582 continue;
1583 if (!(ti->ti_flags & TH_SYN))
1584 continue;
1585 tp->t_flags |= TF_RCVD_SCALE;
1586 tp->requested_s_scale = cp[2];
1587 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
1588 log(LOG_ERR, "TCP: invalid wscale %d from "
1589 "0x%08x, assuming %d\n",
1590 tp->requested_s_scale,
1591 ntohl(ti->ti_src.s_addr),
1592 TCP_MAX_WINSHIFT);
1593 tp->requested_s_scale = TCP_MAX_WINSHIFT;
1594 }
1595 break;
1596
1597 case TCPOPT_TIMESTAMP:
1598 if (optlen != TCPOLEN_TIMESTAMP)
1599 continue;
1600 oi->ts_present = 1;
1601 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
1602 NTOHL(oi->ts_val);
1603 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
1604 NTOHL(oi->ts_ecr);
1605
1606 /*
1607 * A timestamp received in a SYN makes
1608 * it ok to send timestamp requests and replies.
1609 */
1610 if (ti->ti_flags & TH_SYN) {
1611 tp->t_flags |= TF_RCVD_TSTMP;
1612 tp->ts_recent = oi->ts_val;
1613 tp->ts_recent_age = tcp_now;
1614 }
1615 break;
1616 case TCPOPT_SACK_PERMITTED:
1617 if (optlen != TCPOLEN_SACK_PERMITTED)
1618 continue;
1619 if (!(ti->ti_flags & TH_SYN))
1620 continue;
1621 tp->t_flags &= ~TF_CANT_TXSACK;
1622 break;
1623
1624 case TCPOPT_SACK:
1625 if (tp->t_flags & TF_IGNR_RXSACK)
1626 continue;
1627 if (optlen % 8 != 2 || optlen < 10)
1628 continue;
1629 cp += 2;
1630 optlen -= 2;
1631 for (; optlen > 0; cp -= 8, optlen -= 8) {
1632 tcp_seq lwe, rwe;
1633 bcopy((char *)cp, (char *) &lwe, sizeof(lwe));
1634 NTOHL(lwe);
1635 bcopy((char *)cp, (char *) &rwe, sizeof(rwe));
1636 NTOHL(rwe);
1637 /* tcp_mark_sacked(tp, lwe, rwe); */
1638 }
1639 break;
1640 }
1641 }
1642 }
1643
1644 /*
1645 * Pull out of band byte out of a segment so
1646 * it doesn't appear in the user's data queue.
1647 * It is still reflected in the segment length for
1648 * sequencing purposes.
1649 */
1650 void
1651 tcp_pulloutofband(so, ti, m)
1652 struct socket *so;
1653 struct tcpiphdr *ti;
1654 register struct mbuf *m;
1655 {
1656 int cnt = ti->ti_urp - 1;
1657
1658 while (cnt >= 0) {
1659 if (m->m_len > cnt) {
1660 char *cp = mtod(m, caddr_t) + cnt;
1661 struct tcpcb *tp = sototcpcb(so);
1662
1663 tp->t_iobc = *cp;
1664 tp->t_oobflags |= TCPOOB_HAVEDATA;
1665 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1666 m->m_len--;
1667 return;
1668 }
1669 cnt -= m->m_len;
1670 m = m->m_next;
1671 if (m == 0)
1672 break;
1673 }
1674 panic("tcp_pulloutofband");
1675 }
1676
1677 /*
1678 * Collect new round-trip time estimate
1679 * and update averages and current timeout.
1680 */
1681 void
1682 tcp_xmit_timer(tp, rtt)
1683 register struct tcpcb *tp;
1684 short rtt;
1685 {
1686 register short delta;
1687 short rttmin;
1688
1689 tcpstat.tcps_rttupdated++;
1690 --rtt;
1691 if (tp->t_srtt != 0) {
1692 /*
1693 * srtt is stored as fixed point with 3 bits after the
1694 * binary point (i.e., scaled by 8). The following magic
1695 * is equivalent to the smoothing algorithm in rfc793 with
1696 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1697 * point). Adjust rtt to origin 0.
1698 */
1699 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
1700 if ((tp->t_srtt += delta) <= 0)
1701 tp->t_srtt = 1 << 2;
1702 /*
1703 * We accumulate a smoothed rtt variance (actually, a
1704 * smoothed mean difference), then set the retransmit
1705 * timer to smoothed rtt + 4 times the smoothed variance.
1706 * rttvar is stored as fixed point with 2 bits after the
1707 * binary point (scaled by 4). The following is
1708 * equivalent to rfc793 smoothing with an alpha of .75
1709 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1710 * rfc793's wired-in beta.
1711 */
1712 if (delta < 0)
1713 delta = -delta;
1714 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1715 if ((tp->t_rttvar += delta) <= 0)
1716 tp->t_rttvar = 1 << 2;
1717 } else {
1718 /*
1719 * No rtt measurement yet - use the unsmoothed rtt.
1720 * Set the variance to half the rtt (so our first
1721 * retransmit happens at 3*rtt).
1722 */
1723 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
1724 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
1725 }
1726 tp->t_rtt = 0;
1727 tp->t_rxtshift = 0;
1728
1729 /*
1730 * the retransmit should happen at rtt + 4 * rttvar.
1731 * Because of the way we do the smoothing, srtt and rttvar
1732 * will each average +1/2 tick of bias. When we compute
1733 * the retransmit timer, we want 1/2 tick of rounding and
1734 * 1 extra tick because of +-1/2 tick uncertainty in the
1735 * firing of the timer. The bias will give us exactly the
1736 * 1.5 tick we need. But, because the bias is
1737 * statistical, we have to test that we don't drop below
1738 * the minimum feasible timer (which is 2 ticks).
1739 */
1740 if (tp->t_rttmin > rtt + 2)
1741 rttmin = tp->t_rttmin;
1742 else
1743 rttmin = rtt + 2;
1744 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
1745
1746 /*
1747 * We received an ack for a packet that wasn't retransmitted;
1748 * it is probably safe to discard any error indications we've
1749 * received recently. This isn't quite right, but close enough
1750 * for now (a route might have failed after we sent a segment,
1751 * and the return path might not be symmetrical).
1752 */
1753 tp->t_softerror = 0;
1754 }
1755
1756 /*
1757 * TCP compressed state engine. Currently used to hold compressed
1758 * state for SYN_RECEIVED.
1759 */
1760
1761 u_long syn_cache_count;
1762 u_int32_t syn_hash1, syn_hash2;
1763
1764 #define SYN_HASH(sa, sp, dp) \
1765 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
1766 ((u_int32_t)(sp)))^syn_hash2)))
1767
1768 LIST_HEAD(, syn_cache_head) tcp_syn_cache_queue;
1769
1770 #define SYN_CACHE_RM(sc, scp) \
1771 do { \
1772 TAILQ_REMOVE(&(scp)->sch_queue, (sc), sc_queue); \
1773 if (--(scp)->sch_length == 0) \
1774 LIST_REMOVE((scp), sch_headq); \
1775 syn_cache_count--; \
1776 } while (0)
1777
1778 struct pool syn_cache_pool;
1779
1780 void
1781 syn_cache_init()
1782 {
1783 int i;
1784
1785 /* Initialize the hash bucket queues. */
1786 for (i = 0; i < tcp_syn_cache_size; i++)
1787 TAILQ_INIT(&tcp_syn_cache[i].sch_queue);
1788
1789 /* Initialize the active hash bucket cache. */
1790 LIST_INIT(&tcp_syn_cache_queue);
1791
1792 /* Initialize the syn cache pool. */
1793 pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
1794 "synpl", 0, NULL, NULL, M_PCB);
1795 }
1796
1797 void
1798 syn_cache_insert(sc)
1799 struct syn_cache *sc;
1800 {
1801 struct syn_cache_head *scp, *scp2, *sce;
1802 struct syn_cache *sc2;
1803 int s;
1804
1805 /*
1806 * If there are no entries in the hash table, reinitialize
1807 * the hash secrets.
1808 */
1809 if (syn_cache_count == 0) {
1810 struct timeval tv;
1811 microtime(&tv);
1812 syn_hash1 = random() ^ (u_long)≻
1813 syn_hash2 = random() ^ tv.tv_usec;
1814 }
1815
1816 sc->sc_hash = SYN_HASH(&sc->sc_src, sc->sc_sport, sc->sc_dport);
1817 scp = &tcp_syn_cache[sc->sc_hash % tcp_syn_cache_size];
1818
1819 /*
1820 * Make sure that we don't overflow the per-bucket
1821 * limit or the total cache size limit.
1822 */
1823 s = splsoftnet();
1824 if (scp->sch_length >= tcp_syn_bucket_limit) {
1825 tcpstat.tcps_sc_bucketoverflow++;
1826 /*
1827 * The bucket is full. Toss the first (i.e. oldest)
1828 * element in this bucket.
1829 */
1830 sc2 = TAILQ_FIRST(&scp->sch_queue);
1831 SYN_CACHE_RM(sc2, scp);
1832 if (sc2->sc_ipopts)
1833 (void) m_free(sc2->sc_ipopts);
1834 pool_put(&syn_cache_pool, sc2);
1835 } else if (syn_cache_count >= tcp_syn_cache_limit) {
1836 tcpstat.tcps_sc_overflowed++;
1837 /*
1838 * The cache is full. Toss the first (i.e. oldest)
1839 * element in the first non-empty bucket we can find.
1840 */
1841 scp2 = scp;
1842 if (TAILQ_FIRST(&scp2->sch_queue) == NULL) {
1843 sce = &tcp_syn_cache[tcp_syn_cache_size];
1844 for (++scp2; scp2 != scp; scp2++) {
1845 if (scp2 >= sce)
1846 scp2 = &tcp_syn_cache[0];
1847 if (TAILQ_FIRST(&scp2->sch_queue) != NULL)
1848 break;
1849 }
1850 }
1851 sc2 = TAILQ_FIRST(&scp2->sch_queue);
1852 if (sc2 == NULL) {
1853 if (sc->sc_ipopts)
1854 (void) m_free(sc->sc_ipopts);
1855 pool_put(&syn_cache_pool, sc);
1856 return;
1857 }
1858 SYN_CACHE_RM(sc2, scp2);
1859 if (sc2->sc_ipopts)
1860 (void) m_free(sc2->sc_ipopts);
1861 pool_put(&syn_cache_pool, sc2);
1862 }
1863
1864 /* Set entry's timer. */
1865 PRT_SLOW_ARM(sc->sc_timer, tcp_syn_cache_timeo);
1866
1867 /* Put it into the bucket. */
1868 TAILQ_INSERT_TAIL(&scp->sch_queue, sc, sc_queue);
1869 if (++scp->sch_length == 1)
1870 LIST_INSERT_HEAD(&tcp_syn_cache_queue, scp, sch_headq);
1871 syn_cache_count++;
1872
1873 tcpstat.tcps_sc_added++;
1874 splx(s);
1875 }
1876
1877 /*
1878 * Walk down the cache list, looking for expired entries in each bucket.
1879 */
1880 void
1881 syn_cache_timer()
1882 {
1883 struct syn_cache_head *scp, *nscp;
1884 struct syn_cache *sc, *nsc;
1885 int s;
1886
1887 s = splsoftnet();
1888 for (scp = LIST_FIRST(&tcp_syn_cache_queue); scp != NULL; scp = nscp) {
1889 #ifdef DIAGNOSTIC
1890 if (TAILQ_FIRST(&scp->sch_queue) == NULL)
1891 panic("syn_cache_timer: queue inconsistency");
1892 #endif
1893 nscp = LIST_NEXT(scp, sch_headq);
1894 for (sc = TAILQ_FIRST(&scp->sch_queue);
1895 sc != NULL && PRT_SLOW_ISEXPIRED(sc->sc_timer);
1896 sc = nsc) {
1897 nsc = TAILQ_NEXT(sc, sc_queue);
1898 tcpstat.tcps_sc_timed_out++;
1899 SYN_CACHE_RM(sc, scp);
1900 if (sc->sc_ipopts)
1901 (void) m_free(sc->sc_ipopts);
1902 pool_put(&syn_cache_pool, sc);
1903 }
1904 }
1905 splx(s);
1906 }
1907
1908 /*
1909 * Find an entry in the syn cache.
1910 */
1911 struct syn_cache *
1912 syn_cache_lookup(ti, headp)
1913 struct tcpiphdr *ti;
1914 struct syn_cache_head **headp;
1915 {
1916 struct syn_cache *sc;
1917 struct syn_cache_head *scp;
1918 u_int32_t hash;
1919 int s;
1920
1921 hash = SYN_HASH(&ti->ti_src, ti->ti_sport, ti->ti_dport);
1922
1923 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
1924 *headp = scp;
1925 s = splsoftnet();
1926 for (sc = TAILQ_FIRST(&scp->sch_queue); sc != NULL;
1927 sc = TAILQ_NEXT(sc, sc_queue)) {
1928 if (sc->sc_hash != hash)
1929 continue;
1930 if (sc->sc_src.s_addr == ti->ti_src.s_addr &&
1931 sc->sc_sport == ti->ti_sport &&
1932 sc->sc_dport == ti->ti_dport &&
1933 sc->sc_dst.s_addr == ti->ti_dst.s_addr) {
1934 splx(s);
1935 return (sc);
1936 }
1937 }
1938 splx(s);
1939 return (NULL);
1940 }
1941
1942 /*
1943 * This function gets called when we receive an ACK for a
1944 * socket in the LISTEN state. We look up the connection
1945 * in the syn cache, and if its there, we pull it out of
1946 * the cache and turn it into a full-blown connection in
1947 * the SYN-RECEIVED state.
1948 *
1949 * The return values may not be immediately obvious, and their effects
1950 * can be subtle, so here they are:
1951 *
1952 * NULL SYN was not found in cache; caller should drop the
1953 * packet and send an RST.
1954 *
1955 * -1 We were unable to create the new connection, and are
1956 * aborting it. An ACK,RST is being sent to the peer
1957 * (unless we got screwey sequence numbners; see below),
1958 * because the 3-way handshake has been completed. Caller
1959 * should not free the mbuf, since we may be using it. If
1960 * we are not, we will free it.
1961 *
1962 * Otherwise, the return value is a pointer to the new socket
1963 * associated with the connection.
1964 */
1965 struct socket *
1966 syn_cache_get(so, m)
1967 struct socket *so;
1968 struct mbuf *m;
1969 {
1970 struct syn_cache *sc;
1971 struct syn_cache_head *scp;
1972 register struct inpcb *inp;
1973 register struct tcpcb *tp = 0;
1974 register struct tcpiphdr *ti;
1975 struct sockaddr_in *sin;
1976 struct mbuf *am;
1977 long win;
1978 int s;
1979
1980 ti = mtod(m, struct tcpiphdr *);
1981 s = splsoftnet();
1982 if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
1983 splx(s);
1984 return (NULL);
1985 }
1986
1987 win = sbspace(&so->so_rcv);
1988 if (win > TCP_MAXWIN)
1989 win = TCP_MAXWIN;
1990
1991 /*
1992 * Verify the sequence and ack numbers.
1993 */
1994 if ((ti->ti_ack != sc->sc_iss + 1) ||
1995 SEQ_LEQ(ti->ti_seq, sc->sc_irs) ||
1996 SEQ_GT(ti->ti_seq, sc->sc_irs + 1 + win)) {
1997 (void) syn_cache_respond(sc, m, ti, win, 0);
1998 splx(s);
1999 return ((struct socket *)(-1));
2000 }
2001
2002 /* Remove this cache entry */
2003 SYN_CACHE_RM(sc, scp);
2004 splx(s);
2005
2006 /*
2007 * Ok, create the full blown connection, and set things up
2008 * as they would have been set up if we had created the
2009 * connection when the SYN arrived. If we can't create
2010 * the connection, abort it.
2011 */
2012 so = sonewconn(so, SS_ISCONNECTED);
2013 if (so == NULL)
2014 goto resetandabort;
2015
2016 inp = sotoinpcb(so);
2017 inp->inp_laddr = sc->sc_dst;
2018 inp->inp_lport = sc->sc_dport;
2019 in_pcbstate(inp, INP_BOUND);
2020 inp->inp_options = ip_srcroute();
2021 if (inp->inp_options == NULL) {
2022 inp->inp_options = sc->sc_ipopts;
2023 sc->sc_ipopts = NULL;
2024 }
2025
2026 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
2027 if (am == NULL)
2028 goto resetandabort;
2029 am->m_len = sizeof(struct sockaddr_in);
2030 sin = mtod(am, struct sockaddr_in *);
2031 sin->sin_family = AF_INET;
2032 sin->sin_len = sizeof(*sin);
2033 sin->sin_addr = sc->sc_src;
2034 sin->sin_port = sc->sc_sport;
2035 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
2036 if (in_pcbconnect(inp, am)) {
2037 (void) m_free(am);
2038 goto resetandabort;
2039 }
2040 (void) m_free(am);
2041
2042 tp = intotcpcb(inp);
2043 if (sc->sc_request_r_scale != 15) {
2044 tp->requested_s_scale = sc->sc_requested_s_scale;
2045 tp->request_r_scale = sc->sc_request_r_scale;
2046 tp->snd_scale = sc->sc_requested_s_scale;
2047 tp->rcv_scale = sc->sc_request_r_scale;
2048 tp->t_flags |= TF_RCVD_SCALE;
2049 }
2050 if (sc->sc_flags & SCF_TIMESTAMP)
2051 tp->t_flags |= TF_RCVD_TSTMP;
2052
2053 tp->t_template = tcp_template(tp);
2054 if (tp->t_template == 0) {
2055 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
2056 so = NULL;
2057 m_freem(m);
2058 goto abort;
2059 }
2060
2061 tp->iss = sc->sc_iss;
2062 tp->irs = sc->sc_irs;
2063 tcp_sendseqinit(tp);
2064 tcp_rcvseqinit(tp);
2065 tp->t_state = TCPS_SYN_RECEIVED;
2066 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
2067 tcpstat.tcps_accepts++;
2068
2069 /* Initialize tp->t_ourmss before we deal with the peer's! */
2070 tp->t_ourmss = sc->sc_ourmaxseg;
2071 tcp_mss_from_peer(tp, sc->sc_peermaxseg);
2072
2073 /*
2074 * Initialize the initial congestion window. If we
2075 * had to retransmit the SYN,ACK, we must initialize cwnd
2076 * to 1 segment (i.e. the Loss Window).
2077 */
2078 if (sc->sc_flags & SCF_SYNACK_REXMT)
2079 tp->snd_cwnd = tp->t_peermss;
2080 else
2081 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_peermss);
2082
2083 tcp_rmx_rtt(tp);
2084 tp->snd_wl1 = sc->sc_irs;
2085 tp->rcv_up = sc->sc_irs + 1;
2086
2087 /*
2088 * This is what whould have happened in tcp_ouput() when
2089 * the SYN,ACK was sent.
2090 */
2091 tp->snd_up = tp->snd_una;
2092 tp->snd_max = tp->snd_nxt = tp->iss+1;
2093 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
2094 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
2095 tp->rcv_adv = tp->rcv_nxt + win;
2096 tp->last_ack_sent = tp->rcv_nxt;
2097
2098 tcpstat.tcps_sc_completed++;
2099 if (sc->sc_ipopts)
2100 (void) m_free(sc->sc_ipopts);
2101 pool_put(&syn_cache_pool, sc);
2102 return (so);
2103
2104 resetandabort:
2105 (void) tcp_respond(NULL, ti, m, ti->ti_seq+ti->ti_len,
2106 (tcp_seq)0, TH_RST|TH_ACK);
2107 abort:
2108 if (so != NULL)
2109 (void) soabort(so);
2110 if (sc->sc_ipopts)
2111 (void) m_free(sc->sc_ipopts);
2112 pool_put(&syn_cache_pool, sc);
2113 tcpstat.tcps_sc_aborted++;
2114 return ((struct socket *)(-1));
2115 }
2116
2117 /*
2118 * This function is called when we get a RST for a
2119 * non-existant connection, so that we can see if the
2120 * connection is in the syn cache. If it is, zap it.
2121 */
2122
2123 void
2124 syn_cache_reset(ti)
2125 register struct tcpiphdr *ti;
2126 {
2127 struct syn_cache *sc;
2128 struct syn_cache_head *scp;
2129 int s = splsoftnet();
2130
2131 if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
2132 splx(s);
2133 return;
2134 }
2135 if (SEQ_LT(ti->ti_seq,sc->sc_irs) ||
2136 SEQ_GT(ti->ti_seq, sc->sc_irs+1)) {
2137 splx(s);
2138 return;
2139 }
2140 SYN_CACHE_RM(sc, scp);
2141 splx(s);
2142 tcpstat.tcps_sc_reset++;
2143 if (sc->sc_ipopts)
2144 (void) m_free(sc->sc_ipopts);
2145 pool_put(&syn_cache_pool, sc);
2146 }
2147
2148 void
2149 syn_cache_unreach(ip, th)
2150 struct ip *ip;
2151 struct tcphdr *th;
2152 {
2153 struct syn_cache *sc;
2154 struct syn_cache_head *scp;
2155 struct tcpiphdr ti2;
2156 int s;
2157
2158 ti2.ti_src.s_addr = ip->ip_dst.s_addr;
2159 ti2.ti_dst.s_addr = ip->ip_src.s_addr;
2160 ti2.ti_sport = th->th_dport;
2161 ti2.ti_dport = th->th_sport;
2162
2163 s = splsoftnet();
2164 if ((sc = syn_cache_lookup(&ti2, &scp)) == NULL) {
2165 splx(s);
2166 return;
2167 }
2168 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
2169 if (ntohl (th->th_seq) != sc->sc_iss) {
2170 splx(s);
2171 return;
2172 }
2173 SYN_CACHE_RM(sc, scp);
2174 splx(s);
2175 tcpstat.tcps_sc_unreach++;
2176 if (sc->sc_ipopts)
2177 (void) m_free(sc->sc_ipopts);
2178 pool_put(&syn_cache_pool, sc);
2179 }
2180
2181 /*
2182 * Given a LISTEN socket and an inbound SYN request, add
2183 * this to the syn cache, and send back a segment:
2184 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
2185 * to the source.
2186 *
2187 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
2188 * Doing so would require that we hold onto the data and deliver it
2189 * to the application. However, if we are the target of a SYN-flood
2190 * DoS attack, an attacker could send data which would eventually
2191 * consume all available buffer space if it were ACKed. By not ACKing
2192 * the data, we avoid this DoS scenario.
2193 */
2194
2195 int
2196 syn_cache_add(so, m, optp, optlen, oi)
2197 struct socket *so;
2198 struct mbuf *m;
2199 u_char *optp;
2200 int optlen;
2201 struct tcp_opt_info *oi;
2202 {
2203 register struct tcpiphdr *ti;
2204 struct tcpcb tb, *tp;
2205 long win;
2206 struct syn_cache *sc;
2207 struct syn_cache_head *scp;
2208 struct mbuf *ipopts;
2209
2210 tp = sototcpcb(so);
2211 ti = mtod(m, struct tcpiphdr *);
2212
2213 /*
2214 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2215 * in_broadcast() should never return true on a received
2216 * packet with M_BCAST not set.
2217 */
2218 if (m->m_flags & (M_BCAST|M_MCAST) ||
2219 IN_MULTICAST(ti->ti_src.s_addr) ||
2220 IN_MULTICAST(ti->ti_dst.s_addr))
2221 return (0);
2222
2223 /*
2224 * Initialize some local state.
2225 */
2226 win = sbspace(&so->so_rcv);
2227 if (win > TCP_MAXWIN)
2228 win = TCP_MAXWIN;
2229
2230 /*
2231 * Remember the IP options, if any.
2232 */
2233 ipopts = ip_srcroute();
2234
2235 if (optp) {
2236 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
2237 tcp_dooptions(&tb, optp, optlen, ti, oi);
2238 } else
2239 tb.t_flags = 0;
2240
2241 /*
2242 * See if we already have an entry for this connection.
2243 * If we do, resend the SYN,ACK, and remember since the
2244 * initial congestion window must be initialized to 1
2245 * segment when the connection completes.
2246 */
2247 if ((sc = syn_cache_lookup(ti, &scp)) != NULL) {
2248 tcpstat.tcps_sc_dupesyn++;
2249 sc->sc_flags |= SCF_SYNACK_REXMT;
2250
2251 if (ipopts) {
2252 /*
2253 * If we were remembering a previous source route,
2254 * forget it and use the new one we've been given.
2255 */
2256 if (sc->sc_ipopts)
2257 (void) m_free(sc->sc_ipopts);
2258 sc->sc_ipopts = ipopts;
2259 }
2260
2261 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2262 tcpstat.tcps_sndacks++;
2263 tcpstat.tcps_sndtotal++;
2264 }
2265 return (1);
2266 }
2267
2268 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
2269 if (sc == NULL) {
2270 if (ipopts)
2271 (void) m_free(ipopts);
2272 return (0);
2273 }
2274
2275 /*
2276 * Fill in the cache, and put the necessary IP and TCP
2277 * options into the reply.
2278 */
2279 sc->sc_src.s_addr = ti->ti_src.s_addr;
2280 sc->sc_dst.s_addr = ti->ti_dst.s_addr;
2281 sc->sc_sport = ti->ti_sport;
2282 sc->sc_dport = ti->ti_dport;
2283 sc->sc_flags = 0;
2284 sc->sc_ipopts = ipopts;
2285 sc->sc_irs = ti->ti_seq;
2286 sc->sc_iss = tcp_new_iss(sc, sizeof(struct syn_cache), 0);
2287 sc->sc_peermaxseg = oi->maxseg;
2288 sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
2289 m->m_pkthdr.rcvif : NULL);
2290 if (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP))
2291 sc->sc_flags |= SCF_TIMESTAMP;
2292 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2293 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2294 sc->sc_requested_s_scale = tb.requested_s_scale;
2295 sc->sc_request_r_scale = 0;
2296 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
2297 TCP_MAXWIN << sc->sc_request_r_scale <
2298 so->so_rcv.sb_hiwat)
2299 sc->sc_request_r_scale++;
2300 } else {
2301 sc->sc_requested_s_scale = 15;
2302 sc->sc_request_r_scale = 15;
2303 }
2304 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2305 syn_cache_insert(sc);
2306 tcpstat.tcps_sndacks++;
2307 tcpstat.tcps_sndtotal++;
2308 } else {
2309 if (sc->sc_ipopts)
2310 (void) m_free(sc->sc_ipopts);
2311 pool_put(&syn_cache_pool, sc);
2312 tcpstat.tcps_sc_dropped++;
2313 }
2314 return (1);
2315 }
2316
2317 int
2318 syn_cache_respond(sc, m, ti, win, ts)
2319 struct syn_cache *sc;
2320 struct mbuf *m;
2321 register struct tcpiphdr *ti;
2322 long win;
2323 u_long ts;
2324 {
2325 u_int8_t *optp;
2326 int optlen;
2327
2328 /*
2329 * Tack on the TCP options. If there isn't enough trailing
2330 * space for them, move up the fixed header to make space.
2331 */
2332 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
2333 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
2334 if (optlen > M_TRAILINGSPACE(m)) {
2335 if (M_LEADINGSPACE(m) >= optlen) {
2336 m->m_data -= optlen;
2337 m->m_len += optlen;
2338 } else {
2339 struct mbuf *m0 = m;
2340 if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL) {
2341 m_freem(m0);
2342 return (ENOBUFS);
2343 }
2344 MH_ALIGN(m, sizeof(*ti) + optlen);
2345 m->m_next = m0; /* this gets freed below */
2346 }
2347 bcopy((caddr_t)ti, mtod(m, caddr_t), sizeof(*ti));
2348 ti = mtod(m, struct tcpiphdr *);
2349 }
2350
2351 optp = (u_int8_t *)(ti + 1);
2352 optp[0] = TCPOPT_MAXSEG;
2353 optp[1] = 4;
2354 optp[2] = (sc->sc_ourmaxseg >> 8) & 0xff;
2355 optp[3] = sc->sc_ourmaxseg & 0xff;
2356 optlen = 4;
2357
2358 if (sc->sc_request_r_scale != 15) {
2359 *((u_int32_t *)(optp + optlen)) = htonl(TCPOPT_NOP << 24 |
2360 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
2361 sc->sc_request_r_scale);
2362 optlen += 4;
2363 }
2364
2365 if (sc->sc_flags & SCF_TIMESTAMP) {
2366 u_int32_t *lp = (u_int32_t *)(optp + optlen);
2367 /* Form timestamp option as shown in appendix A of RFC 1323. */
2368 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
2369 *lp++ = htonl(tcp_now);
2370 *lp = htonl(ts);
2371 optlen += TCPOLEN_TSTAMP_APPA;
2372 }
2373
2374 /*
2375 * Toss any trailing mbufs. No need to worry about
2376 * m_len and m_pkthdr.len, since tcp_respond() will
2377 * unconditionally set them.
2378 */
2379 if (m->m_next) {
2380 m_freem(m->m_next);
2381 m->m_next = NULL;
2382 }
2383
2384 /*
2385 * Fill in the fields that tcp_respond() will not touch, and
2386 * then send the response.
2387 */
2388 ti->ti_off = (sizeof(struct tcphdr) + optlen) >> 2;
2389 ti->ti_win = htons(win);
2390 return (tcp_respond(NULL, ti, m, sc->sc_irs + 1, sc->sc_iss,
2391 TH_SYN|TH_ACK));
2392 }
2393