tcp_input.c revision 1.232 1 /* $NetBSD: tcp_input.c,v 1.232 2005/08/10 13:05:16 yamt Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
34 *
35 * NRL grants permission for redistribution and use in source and binary
36 * forms, with or without modification, of the software and documentation
37 * created at NRL provided that the following conditions are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgements:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * This product includes software developed at the Information
49 * Technology Division, US Naval Research Laboratory.
50 * 4. Neither the name of the NRL nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * The views and conclusions contained in the software and documentation
67 * are those of the authors and should not be interpreted as representing
68 * official policies, either expressed or implied, of the US Naval
69 * Research Laboratory (NRL).
70 */
71
72 /*-
73 * Copyright (c) 1997, 1998, 1999, 2001, 2005 The NetBSD Foundation, Inc.
74 * All rights reserved.
75 *
76 * This code is derived from software contributed to The NetBSD Foundation
77 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
78 * Facility, NASA Ames Research Center.
79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Charles M. Hannum.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the NetBSD
93 * Foundation, Inc. and its contributors.
94 * 4. Neither the name of The NetBSD Foundation nor the names of its
95 * contributors may be used to endorse or promote products derived
96 * from this software without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108 * POSSIBILITY OF SUCH DAMAGE.
109 */
110
111 /*
112 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
113 * The Regents of the University of California. All rights reserved.
114 *
115 * Redistribution and use in source and binary forms, with or without
116 * modification, are permitted provided that the following conditions
117 * are met:
118 * 1. Redistributions of source code must retain the above copyright
119 * notice, this list of conditions and the following disclaimer.
120 * 2. Redistributions in binary form must reproduce the above copyright
121 * notice, this list of conditions and the following disclaimer in the
122 * documentation and/or other materials provided with the distribution.
123 * 3. Neither the name of the University nor the names of its contributors
124 * may be used to endorse or promote products derived from this software
125 * without specific prior written permission.
126 *
127 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
128 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
129 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
130 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
131 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
132 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
133 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
134 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
135 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
136 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
137 * SUCH DAMAGE.
138 *
139 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
140 */
141
142 /*
143 * TODO list for SYN cache stuff:
144 *
145 * Find room for a "state" field, which is needed to keep a
146 * compressed state for TIME_WAIT TCBs. It's been noted already
147 * that this is fairly important for very high-volume web and
148 * mail servers, which use a large number of short-lived
149 * connections.
150 */
151
152 #include <sys/cdefs.h>
153 __KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.232 2005/08/10 13:05:16 yamt Exp $");
154
155 #include "opt_inet.h"
156 #include "opt_ipsec.h"
157 #include "opt_inet_csum.h"
158 #include "opt_tcp_debug.h"
159
160 #include <sys/param.h>
161 #include <sys/systm.h>
162 #include <sys/malloc.h>
163 #include <sys/mbuf.h>
164 #include <sys/protosw.h>
165 #include <sys/socket.h>
166 #include <sys/socketvar.h>
167 #include <sys/errno.h>
168 #include <sys/syslog.h>
169 #include <sys/pool.h>
170 #include <sys/domain.h>
171 #include <sys/kernel.h>
172 #ifdef TCP_SIGNATURE
173 #include <sys/md5.h>
174 #endif
175
176 #include <net/if.h>
177 #include <net/route.h>
178 #include <net/if_types.h>
179
180 #include <netinet/in.h>
181 #include <netinet/in_systm.h>
182 #include <netinet/ip.h>
183 #include <netinet/in_pcb.h>
184 #include <netinet/in_var.h>
185 #include <netinet/ip_var.h>
186
187 #ifdef INET6
188 #ifndef INET
189 #include <netinet/in.h>
190 #endif
191 #include <netinet/ip6.h>
192 #include <netinet6/ip6_var.h>
193 #include <netinet6/in6_pcb.h>
194 #include <netinet6/ip6_var.h>
195 #include <netinet6/in6_var.h>
196 #include <netinet/icmp6.h>
197 #include <netinet6/nd6.h>
198 #endif
199
200 #ifndef INET6
201 /* always need ip6.h for IP6_EXTHDR_GET */
202 #include <netinet/ip6.h>
203 #endif
204
205 #include <netinet/tcp.h>
206 #include <netinet/tcp_fsm.h>
207 #include <netinet/tcp_seq.h>
208 #include <netinet/tcp_timer.h>
209 #include <netinet/tcp_var.h>
210 #include <netinet/tcpip.h>
211 #include <netinet/tcp_debug.h>
212
213 #include <machine/stdarg.h>
214
215 #ifdef IPSEC
216 #include <netinet6/ipsec.h>
217 #include <netkey/key.h>
218 #endif /*IPSEC*/
219 #ifdef INET6
220 #include "faith.h"
221 #if defined(NFAITH) && NFAITH > 0
222 #include <net/if_faith.h>
223 #endif
224 #endif /* IPSEC */
225
226 #ifdef FAST_IPSEC
227 #include <netipsec/ipsec.h>
228 #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
229 #include <netipsec/key.h>
230 #ifdef INET6
231 #include <netipsec/ipsec6.h>
232 #endif
233 #endif /* FAST_IPSEC*/
234
235 int tcprexmtthresh = 3;
236 int tcp_log_refused;
237
238 static int tcp_rst_ppslim_count = 0;
239 static struct timeval tcp_rst_ppslim_last;
240 static int tcp_ackdrop_ppslim_count = 0;
241 static struct timeval tcp_ackdrop_ppslim_last;
242
243 #define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ)
244
245 /* for modulo comparisons of timestamps */
246 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
247 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
248
249 /*
250 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
251 */
252 #ifdef INET6
253 #define ND6_HINT(tp) \
254 do { \
255 if (tp && tp->t_in6pcb && tp->t_family == AF_INET6 && \
256 tp->t_in6pcb->in6p_route.ro_rt) { \
257 nd6_nud_hint(tp->t_in6pcb->in6p_route.ro_rt, NULL, 0); \
258 } \
259 } while (/*CONSTCOND*/ 0)
260 #else
261 #define ND6_HINT(tp)
262 #endif
263
264 /*
265 * Macro to compute ACK transmission behavior. Delay the ACK unless
266 * we have already delayed an ACK (must send an ACK every two segments).
267 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
268 * option is enabled.
269 */
270 #define TCP_SETUP_ACK(tp, th) \
271 do { \
272 if ((tp)->t_flags & TF_DELACK || \
273 (tcp_ack_on_push && (th)->th_flags & TH_PUSH)) \
274 tp->t_flags |= TF_ACKNOW; \
275 else \
276 TCP_SET_DELACK(tp); \
277 } while (/*CONSTCOND*/ 0)
278
279 #define ICMP_CHECK(tp, th, acked) \
280 do { \
281 /* \
282 * If we had a pending ICMP message that \
283 * refers to data that have just been \
284 * acknowledged, disregard the recorded ICMP \
285 * message. \
286 */ \
287 if (((tp)->t_flags & TF_PMTUD_PEND) && \
288 SEQ_GT((th)->th_ack, (tp)->t_pmtud_th_seq)) \
289 (tp)->t_flags &= ~TF_PMTUD_PEND; \
290 \
291 /* \
292 * Keep track of the largest chunk of data \
293 * acknowledged since last PMTU update \
294 */ \
295 if ((tp)->t_pmtud_mss_acked < (acked)) \
296 (tp)->t_pmtud_mss_acked = (acked); \
297 } while (/*CONSTCOND*/ 0)
298
299 /*
300 * Convert TCP protocol fields to host order for easier processing.
301 */
302 #define TCP_FIELDS_TO_HOST(th) \
303 do { \
304 NTOHL((th)->th_seq); \
305 NTOHL((th)->th_ack); \
306 NTOHS((th)->th_win); \
307 NTOHS((th)->th_urp); \
308 } while (/*CONSTCOND*/ 0)
309
310 /*
311 * ... and reverse the above.
312 */
313 #define TCP_FIELDS_TO_NET(th) \
314 do { \
315 HTONL((th)->th_seq); \
316 HTONL((th)->th_ack); \
317 HTONS((th)->th_win); \
318 HTONS((th)->th_urp); \
319 } while (/*CONSTCOND*/ 0)
320
321 #ifdef TCP_CSUM_COUNTERS
322 #include <sys/device.h>
323
324 #if defined(INET)
325 extern struct evcnt tcp_hwcsum_ok;
326 extern struct evcnt tcp_hwcsum_bad;
327 extern struct evcnt tcp_hwcsum_data;
328 extern struct evcnt tcp_swcsum;
329 #endif /* defined(INET) */
330 #if defined(INET6)
331 extern struct evcnt tcp6_hwcsum_ok;
332 extern struct evcnt tcp6_hwcsum_bad;
333 extern struct evcnt tcp6_hwcsum_data;
334 extern struct evcnt tcp6_swcsum;
335 #endif /* defined(INET6) */
336
337 #define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
338
339 #else
340
341 #define TCP_CSUM_COUNTER_INCR(ev) /* nothing */
342
343 #endif /* TCP_CSUM_COUNTERS */
344
345 #ifdef TCP_REASS_COUNTERS
346 #include <sys/device.h>
347
348 extern struct evcnt tcp_reass_;
349 extern struct evcnt tcp_reass_empty;
350 extern struct evcnt tcp_reass_iteration[8];
351 extern struct evcnt tcp_reass_prependfirst;
352 extern struct evcnt tcp_reass_prepend;
353 extern struct evcnt tcp_reass_insert;
354 extern struct evcnt tcp_reass_inserttail;
355 extern struct evcnt tcp_reass_append;
356 extern struct evcnt tcp_reass_appendtail;
357 extern struct evcnt tcp_reass_overlaptail;
358 extern struct evcnt tcp_reass_overlapfront;
359 extern struct evcnt tcp_reass_segdup;
360 extern struct evcnt tcp_reass_fragdup;
361
362 #define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++
363
364 #else
365
366 #define TCP_REASS_COUNTER_INCR(ev) /* nothing */
367
368 #endif /* TCP_REASS_COUNTERS */
369
370 #ifdef INET
371 static void tcp4_log_refused(const struct ip *, const struct tcphdr *);
372 #endif
373 #ifdef INET6
374 static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *);
375 #endif
376
377 #define TRAVERSE(x) while ((x)->m_next) (x) = (x)->m_next
378
379 POOL_INIT(tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl", NULL);
380
381 struct ipqent *
382 tcpipqent_alloc()
383 {
384 struct ipqent *ipqe;
385 int s;
386
387 s = splvm();
388 ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT);
389 splx(s);
390
391 return ipqe;
392 }
393
394 void
395 tcpipqent_free(struct ipqent *ipqe)
396 {
397 int s;
398
399 s = splvm();
400 pool_put(&tcpipqent_pool, ipqe);
401 splx(s);
402 }
403
404 int
405 tcp_reass(struct tcpcb *tp, struct tcphdr *th, struct mbuf *m, int *tlen)
406 {
407 struct ipqent *p, *q, *nq, *tiqe = NULL;
408 struct socket *so = NULL;
409 int pkt_flags;
410 tcp_seq pkt_seq;
411 unsigned pkt_len;
412 u_long rcvpartdupbyte = 0;
413 u_long rcvoobyte;
414 #ifdef TCP_REASS_COUNTERS
415 u_int count = 0;
416 #endif
417
418 if (tp->t_inpcb)
419 so = tp->t_inpcb->inp_socket;
420 #ifdef INET6
421 else if (tp->t_in6pcb)
422 so = tp->t_in6pcb->in6p_socket;
423 #endif
424
425 TCP_REASS_LOCK_CHECK(tp);
426
427 /*
428 * Call with th==0 after become established to
429 * force pre-ESTABLISHED data up to user socket.
430 */
431 if (th == 0)
432 goto present;
433
434 rcvoobyte = *tlen;
435 /*
436 * Copy these to local variables because the tcpiphdr
437 * gets munged while we are collapsing mbufs.
438 */
439 pkt_seq = th->th_seq;
440 pkt_len = *tlen;
441 pkt_flags = th->th_flags;
442
443 TCP_REASS_COUNTER_INCR(&tcp_reass_);
444
445 if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) {
446 /*
447 * When we miss a packet, the vast majority of time we get
448 * packets that follow it in order. So optimize for that.
449 */
450 if (pkt_seq == p->ipqe_seq + p->ipqe_len) {
451 p->ipqe_len += pkt_len;
452 p->ipqe_flags |= pkt_flags;
453 m_cat(p->ipre_mlast, m);
454 TRAVERSE(p->ipre_mlast);
455 m = NULL;
456 tiqe = p;
457 TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq);
458 TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail);
459 goto skip_replacement;
460 }
461 /*
462 * While we're here, if the pkt is completely beyond
463 * anything we have, just insert it at the tail.
464 */
465 if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) {
466 TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail);
467 goto insert_it;
468 }
469 }
470
471 q = TAILQ_FIRST(&tp->segq);
472
473 if (q != NULL) {
474 /*
475 * If this segment immediately precedes the first out-of-order
476 * block, simply slap the segment in front of it and (mostly)
477 * skip the complicated logic.
478 */
479 if (pkt_seq + pkt_len == q->ipqe_seq) {
480 q->ipqe_seq = pkt_seq;
481 q->ipqe_len += pkt_len;
482 q->ipqe_flags |= pkt_flags;
483 m_cat(m, q->ipqe_m);
484 q->ipqe_m = m;
485 q->ipre_mlast = m; /* last mbuf may have changed */
486 TRAVERSE(q->ipre_mlast);
487 tiqe = q;
488 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
489 TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst);
490 goto skip_replacement;
491 }
492 } else {
493 TCP_REASS_COUNTER_INCR(&tcp_reass_empty);
494 }
495
496 /*
497 * Find a segment which begins after this one does.
498 */
499 for (p = NULL; q != NULL; q = nq) {
500 nq = TAILQ_NEXT(q, ipqe_q);
501 #ifdef TCP_REASS_COUNTERS
502 count++;
503 #endif
504 /*
505 * If the received segment is just right after this
506 * fragment, merge the two together and then check
507 * for further overlaps.
508 */
509 if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
510 #ifdef TCPREASS_DEBUG
511 printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
512 tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
513 q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
514 #endif
515 pkt_len += q->ipqe_len;
516 pkt_flags |= q->ipqe_flags;
517 pkt_seq = q->ipqe_seq;
518 m_cat(q->ipre_mlast, m);
519 TRAVERSE(q->ipre_mlast);
520 m = q->ipqe_m;
521 TCP_REASS_COUNTER_INCR(&tcp_reass_append);
522 goto free_ipqe;
523 }
524 /*
525 * If the received segment is completely past this
526 * fragment, we need to go the next fragment.
527 */
528 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
529 p = q;
530 continue;
531 }
532 /*
533 * If the fragment is past the received segment,
534 * it (or any following) can't be concatenated.
535 */
536 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) {
537 TCP_REASS_COUNTER_INCR(&tcp_reass_insert);
538 break;
539 }
540
541 /*
542 * We've received all the data in this segment before.
543 * mark it as a duplicate and return.
544 */
545 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
546 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
547 tcpstat.tcps_rcvduppack++;
548 tcpstat.tcps_rcvdupbyte += pkt_len;
549 tcp_new_dsack(tp, pkt_seq, pkt_len);
550 m_freem(m);
551 if (tiqe != NULL) {
552 tcpipqent_free(tiqe);
553 }
554 TCP_REASS_COUNTER_INCR(&tcp_reass_segdup);
555 return (0);
556 }
557 /*
558 * Received segment completely overlaps this fragment
559 * so we drop the fragment (this keeps the temporal
560 * ordering of segments correct).
561 */
562 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
563 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
564 rcvpartdupbyte += q->ipqe_len;
565 m_freem(q->ipqe_m);
566 TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup);
567 goto free_ipqe;
568 }
569 /*
570 * RX'ed segment extends past the end of the
571 * fragment. Drop the overlapping bytes. Then
572 * merge the fragment and segment then treat as
573 * a longer received packet.
574 */
575 if (SEQ_LT(q->ipqe_seq, pkt_seq) &&
576 SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
577 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
578 #ifdef TCPREASS_DEBUG
579 printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
580 tp, overlap,
581 pkt_seq, pkt_seq + pkt_len, pkt_len);
582 #endif
583 m_adj(m, overlap);
584 rcvpartdupbyte += overlap;
585 m_cat(q->ipre_mlast, m);
586 TRAVERSE(q->ipre_mlast);
587 m = q->ipqe_m;
588 pkt_seq = q->ipqe_seq;
589 pkt_len += q->ipqe_len - overlap;
590 rcvoobyte -= overlap;
591 TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail);
592 goto free_ipqe;
593 }
594 /*
595 * RX'ed segment extends past the front of the
596 * fragment. Drop the overlapping bytes on the
597 * received packet. The packet will then be
598 * contatentated with this fragment a bit later.
599 */
600 if (SEQ_GT(q->ipqe_seq, pkt_seq) &&
601 SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) {
602 int overlap = pkt_seq + pkt_len - q->ipqe_seq;
603 #ifdef TCPREASS_DEBUG
604 printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
605 tp, overlap,
606 pkt_seq, pkt_seq + pkt_len, pkt_len);
607 #endif
608 m_adj(m, -overlap);
609 pkt_len -= overlap;
610 rcvpartdupbyte += overlap;
611 TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront);
612 rcvoobyte -= overlap;
613 }
614 /*
615 * If the received segment immediates precedes this
616 * fragment then tack the fragment onto this segment
617 * and reinsert the data.
618 */
619 if (q->ipqe_seq == pkt_seq + pkt_len) {
620 #ifdef TCPREASS_DEBUG
621 printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
622 tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
623 pkt_seq, pkt_seq + pkt_len, pkt_len);
624 #endif
625 pkt_len += q->ipqe_len;
626 pkt_flags |= q->ipqe_flags;
627 m_cat(m, q->ipqe_m);
628 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
629 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
630 tp->t_segqlen--;
631 KASSERT(tp->t_segqlen >= 0);
632 KASSERT(tp->t_segqlen != 0 ||
633 (TAILQ_EMPTY(&tp->segq) &&
634 TAILQ_EMPTY(&tp->timeq)));
635 if (tiqe == NULL) {
636 tiqe = q;
637 } else {
638 tcpipqent_free(q);
639 }
640 TCP_REASS_COUNTER_INCR(&tcp_reass_prepend);
641 break;
642 }
643 /*
644 * If the fragment is before the segment, remember it.
645 * When this loop is terminated, p will contain the
646 * pointer to fragment that is right before the received
647 * segment.
648 */
649 if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
650 p = q;
651
652 continue;
653
654 /*
655 * This is a common operation. It also will allow
656 * to save doing a malloc/free in most instances.
657 */
658 free_ipqe:
659 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
660 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
661 tp->t_segqlen--;
662 KASSERT(tp->t_segqlen >= 0);
663 KASSERT(tp->t_segqlen != 0 ||
664 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
665 if (tiqe == NULL) {
666 tiqe = q;
667 } else {
668 tcpipqent_free(q);
669 }
670 }
671
672 #ifdef TCP_REASS_COUNTERS
673 if (count > 7)
674 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]);
675 else if (count > 0)
676 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]);
677 #endif
678
679 insert_it:
680
681 /*
682 * Allocate a new queue entry since the received segment did not
683 * collapse onto any other out-of-order block; thus we are allocating
684 * a new block. If it had collapsed, tiqe would not be NULL and
685 * we would be reusing it.
686 * XXX If we can't, just drop the packet. XXX
687 */
688 if (tiqe == NULL) {
689 tiqe = tcpipqent_alloc();
690 if (tiqe == NULL) {
691 tcpstat.tcps_rcvmemdrop++;
692 m_freem(m);
693 return (0);
694 }
695 }
696
697 /*
698 * Update the counters.
699 */
700 tcpstat.tcps_rcvoopack++;
701 tcpstat.tcps_rcvoobyte += rcvoobyte;
702 if (rcvpartdupbyte) {
703 tcpstat.tcps_rcvpartduppack++;
704 tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
705 }
706
707 /*
708 * Insert the new fragment queue entry into both queues.
709 */
710 tiqe->ipqe_m = m;
711 tiqe->ipre_mlast = m;
712 tiqe->ipqe_seq = pkt_seq;
713 tiqe->ipqe_len = pkt_len;
714 tiqe->ipqe_flags = pkt_flags;
715 if (p == NULL) {
716 TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
717 #ifdef TCPREASS_DEBUG
718 if (tiqe->ipqe_seq != tp->rcv_nxt)
719 printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
720 tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
721 #endif
722 } else {
723 TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q);
724 #ifdef TCPREASS_DEBUG
725 printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
726 tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
727 p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
728 #endif
729 }
730 tp->t_segqlen++;
731
732 skip_replacement:
733
734 TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
735
736 present:
737 /*
738 * Present data to user, advancing rcv_nxt through
739 * completed sequence space.
740 */
741 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
742 return (0);
743 q = TAILQ_FIRST(&tp->segq);
744 if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
745 return (0);
746 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
747 return (0);
748
749 tp->rcv_nxt += q->ipqe_len;
750 pkt_flags = q->ipqe_flags & TH_FIN;
751 ND6_HINT(tp);
752
753 TAILQ_REMOVE(&tp->segq, q, ipqe_q);
754 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
755 tp->t_segqlen--;
756 KASSERT(tp->t_segqlen >= 0);
757 KASSERT(tp->t_segqlen != 0 ||
758 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
759 if (so->so_state & SS_CANTRCVMORE)
760 m_freem(q->ipqe_m);
761 else
762 sbappendstream(&so->so_rcv, q->ipqe_m);
763 tcpipqent_free(q);
764 sorwakeup(so);
765 return (pkt_flags);
766 }
767
768 #ifdef INET6
769 int
770 tcp6_input(struct mbuf **mp, int *offp, int proto)
771 {
772 struct mbuf *m = *mp;
773
774 /*
775 * draft-itojun-ipv6-tcp-to-anycast
776 * better place to put this in?
777 */
778 if (m->m_flags & M_ANYCAST6) {
779 struct ip6_hdr *ip6;
780 if (m->m_len < sizeof(struct ip6_hdr)) {
781 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
782 tcpstat.tcps_rcvshort++;
783 return IPPROTO_DONE;
784 }
785 }
786 ip6 = mtod(m, struct ip6_hdr *);
787 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
788 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
789 return IPPROTO_DONE;
790 }
791
792 tcp_input(m, *offp, proto);
793 return IPPROTO_DONE;
794 }
795 #endif
796
797 #ifdef INET
798 static void
799 tcp4_log_refused(const struct ip *ip, const struct tcphdr *th)
800 {
801 char src[4*sizeof "123"];
802 char dst[4*sizeof "123"];
803
804 if (ip) {
805 strlcpy(src, inet_ntoa(ip->ip_src), sizeof(src));
806 strlcpy(dst, inet_ntoa(ip->ip_dst), sizeof(dst));
807 }
808 else {
809 strlcpy(src, "(unknown)", sizeof(src));
810 strlcpy(dst, "(unknown)", sizeof(dst));
811 }
812 log(LOG_INFO,
813 "Connection attempt to TCP %s:%d from %s:%d\n",
814 dst, ntohs(th->th_dport),
815 src, ntohs(th->th_sport));
816 }
817 #endif
818
819 #ifdef INET6
820 static void
821 tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th)
822 {
823 char src[INET6_ADDRSTRLEN];
824 char dst[INET6_ADDRSTRLEN];
825
826 if (ip6) {
827 strlcpy(src, ip6_sprintf(&ip6->ip6_src), sizeof(src));
828 strlcpy(dst, ip6_sprintf(&ip6->ip6_dst), sizeof(dst));
829 }
830 else {
831 strlcpy(src, "(unknown v6)", sizeof(src));
832 strlcpy(dst, "(unknown v6)", sizeof(dst));
833 }
834 log(LOG_INFO,
835 "Connection attempt to TCP [%s]:%d from [%s]:%d\n",
836 dst, ntohs(th->th_dport),
837 src, ntohs(th->th_sport));
838 }
839 #endif
840
841 /*
842 * Checksum extended TCP header and data.
843 */
844 int
845 tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th, int toff,
846 int off, int tlen)
847 {
848
849 /*
850 * XXX it's better to record and check if this mbuf is
851 * already checked.
852 */
853
854 switch (af) {
855 #ifdef INET
856 case AF_INET:
857 switch (m->m_pkthdr.csum_flags &
858 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv4) |
859 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
860 case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD:
861 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad);
862 goto badcsum;
863
864 case M_CSUM_TCPv4|M_CSUM_DATA: {
865 u_int32_t hw_csum = m->m_pkthdr.csum_data;
866
867 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data);
868 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
869 const struct ip *ip =
870 mtod(m, const struct ip *);
871
872 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
873 ip->ip_dst.s_addr,
874 htons(hw_csum + tlen + off + IPPROTO_TCP));
875 }
876 if ((hw_csum ^ 0xffff) != 0)
877 goto badcsum;
878 break;
879 }
880
881 case M_CSUM_TCPv4:
882 /* Checksum was okay. */
883 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok);
884 break;
885
886 default:
887 /*
888 * Must compute it ourselves. Maybe skip checksum
889 * on loopback interfaces.
890 */
891 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
892 IFF_LOOPBACK) ||
893 tcp_do_loopback_cksum)) {
894 TCP_CSUM_COUNTER_INCR(&tcp_swcsum);
895 if (in4_cksum(m, IPPROTO_TCP, toff,
896 tlen + off) != 0)
897 goto badcsum;
898 }
899 break;
900 }
901 break;
902 #endif /* INET4 */
903
904 #ifdef INET6
905 case AF_INET6:
906 switch (m->m_pkthdr.csum_flags &
907 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv6) |
908 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
909 case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD:
910 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad);
911 goto badcsum;
912
913 #if 0 /* notyet */
914 case M_CSUM_TCPv6|M_CSUM_DATA:
915 #endif
916
917 case M_CSUM_TCPv6:
918 /* Checksum was okay. */
919 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok);
920 break;
921
922 default:
923 /*
924 * Must compute it ourselves. Maybe skip checksum
925 * on loopback interfaces.
926 */
927 if (__predict_true((m->m_flags & M_LOOP) == 0 ||
928 tcp_do_loopback_cksum)) {
929 TCP_CSUM_COUNTER_INCR(&tcp6_swcsum);
930 if (in6_cksum(m, IPPROTO_TCP, toff,
931 tlen + off) != 0)
932 goto badcsum;
933 }
934 }
935 break;
936 #endif /* INET6 */
937 }
938
939 return 0;
940
941 badcsum:
942 tcpstat.tcps_rcvbadsum++;
943 return -1;
944 }
945
946 /*
947 * TCP input routine, follows pages 65-76 of the
948 * protocol specification dated September, 1981 very closely.
949 */
950 void
951 tcp_input(struct mbuf *m, ...)
952 {
953 struct tcphdr *th;
954 struct ip *ip;
955 struct inpcb *inp;
956 #ifdef INET6
957 struct ip6_hdr *ip6;
958 struct in6pcb *in6p;
959 #endif
960 u_int8_t *optp = NULL;
961 int optlen = 0;
962 int len, tlen, toff, hdroptlen = 0;
963 struct tcpcb *tp = 0;
964 int tiflags;
965 struct socket *so = NULL;
966 int todrop, dupseg, acked, ourfinisacked, needoutput = 0;
967 #ifdef TCP_DEBUG
968 short ostate = 0;
969 #endif
970 int iss = 0;
971 u_long tiwin;
972 struct tcp_opt_info opti;
973 int off, iphlen;
974 va_list ap;
975 int af; /* af on the wire */
976 struct mbuf *tcp_saveti = NULL;
977 uint32_t ts_rtt;
978
979 MCLAIM(m, &tcp_rx_mowner);
980 va_start(ap, m);
981 toff = va_arg(ap, int);
982 (void)va_arg(ap, int); /* ignore value, advance ap */
983 va_end(ap);
984
985 tcpstat.tcps_rcvtotal++;
986
987 bzero(&opti, sizeof(opti));
988 opti.ts_present = 0;
989 opti.maxseg = 0;
990
991 /*
992 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN.
993 *
994 * TCP is, by definition, unicast, so we reject all
995 * multicast outright.
996 *
997 * Note, there are additional src/dst address checks in
998 * the AF-specific code below.
999 */
1000 if (m->m_flags & (M_BCAST|M_MCAST)) {
1001 /* XXX stat */
1002 goto drop;
1003 }
1004 #ifdef INET6
1005 if (m->m_flags & M_ANYCAST6) {
1006 /* XXX stat */
1007 goto drop;
1008 }
1009 #endif
1010
1011 /*
1012 * Get IP and TCP header.
1013 * Note: IP leaves IP header in first mbuf.
1014 */
1015 ip = mtod(m, struct ip *);
1016 #ifdef INET6
1017 ip6 = NULL;
1018 #endif
1019 switch (ip->ip_v) {
1020 #ifdef INET
1021 case 4:
1022 af = AF_INET;
1023 iphlen = sizeof(struct ip);
1024 ip = mtod(m, struct ip *);
1025 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
1026 sizeof(struct tcphdr));
1027 if (th == NULL) {
1028 tcpstat.tcps_rcvshort++;
1029 return;
1030 }
1031 /* We do the checksum after PCB lookup... */
1032 len = ntohs(ip->ip_len);
1033 tlen = len - toff;
1034 break;
1035 #endif
1036 #ifdef INET6
1037 case 6:
1038 ip = NULL;
1039 iphlen = sizeof(struct ip6_hdr);
1040 af = AF_INET6;
1041 ip6 = mtod(m, struct ip6_hdr *);
1042 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
1043 sizeof(struct tcphdr));
1044 if (th == NULL) {
1045 tcpstat.tcps_rcvshort++;
1046 return;
1047 }
1048
1049 /* Be proactive about malicious use of IPv4 mapped address */
1050 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
1051 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1052 /* XXX stat */
1053 goto drop;
1054 }
1055
1056 /*
1057 * Be proactive about unspecified IPv6 address in source.
1058 * As we use all-zero to indicate unbounded/unconnected pcb,
1059 * unspecified IPv6 address can be used to confuse us.
1060 *
1061 * Note that packets with unspecified IPv6 destination is
1062 * already dropped in ip6_input.
1063 */
1064 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1065 /* XXX stat */
1066 goto drop;
1067 }
1068
1069 /*
1070 * Make sure destination address is not multicast.
1071 * Source address checked in ip6_input().
1072 */
1073 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1074 /* XXX stat */
1075 goto drop;
1076 }
1077
1078 /* We do the checksum after PCB lookup... */
1079 len = m->m_pkthdr.len;
1080 tlen = len - toff;
1081 break;
1082 #endif
1083 default:
1084 m_freem(m);
1085 return;
1086 }
1087
1088 KASSERT(TCP_HDR_ALIGNED_P(th));
1089
1090 /*
1091 * Check that TCP offset makes sense,
1092 * pull out TCP options and adjust length. XXX
1093 */
1094 off = th->th_off << 2;
1095 if (off < sizeof (struct tcphdr) || off > tlen) {
1096 tcpstat.tcps_rcvbadoff++;
1097 goto drop;
1098 }
1099 tlen -= off;
1100
1101 /*
1102 * tcp_input() has been modified to use tlen to mean the TCP data
1103 * length throughout the function. Other functions can use
1104 * m->m_pkthdr.len as the basis for calculating the TCP data length.
1105 * rja
1106 */
1107
1108 if (off > sizeof (struct tcphdr)) {
1109 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, off);
1110 if (th == NULL) {
1111 tcpstat.tcps_rcvshort++;
1112 return;
1113 }
1114 /*
1115 * NOTE: ip/ip6 will not be affected by m_pulldown()
1116 * (as they're before toff) and we don't need to update those.
1117 */
1118 KASSERT(TCP_HDR_ALIGNED_P(th));
1119 optlen = off - sizeof (struct tcphdr);
1120 optp = ((u_int8_t *)th) + sizeof(struct tcphdr);
1121 /*
1122 * Do quick retrieval of timestamp options ("options
1123 * prediction?"). If timestamp is the only option and it's
1124 * formatted as recommended in RFC 1323 appendix A, we
1125 * quickly get the values now and not bother calling
1126 * tcp_dooptions(), etc.
1127 */
1128 if ((optlen == TCPOLEN_TSTAMP_APPA ||
1129 (optlen > TCPOLEN_TSTAMP_APPA &&
1130 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
1131 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
1132 (th->th_flags & TH_SYN) == 0) {
1133 opti.ts_present = 1;
1134 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
1135 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
1136 optp = NULL; /* we've parsed the options */
1137 }
1138 }
1139 tiflags = th->th_flags;
1140
1141 /*
1142 * Locate pcb for segment.
1143 */
1144 findpcb:
1145 inp = NULL;
1146 #ifdef INET6
1147 in6p = NULL;
1148 #endif
1149 switch (af) {
1150 #ifdef INET
1151 case AF_INET:
1152 inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport,
1153 ip->ip_dst, th->th_dport);
1154 if (inp == 0) {
1155 ++tcpstat.tcps_pcbhashmiss;
1156 inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, th->th_dport);
1157 }
1158 #ifdef INET6
1159 if (inp == 0) {
1160 struct in6_addr s, d;
1161
1162 /* mapped addr case */
1163 bzero(&s, sizeof(s));
1164 s.s6_addr16[5] = htons(0xffff);
1165 bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
1166 bzero(&d, sizeof(d));
1167 d.s6_addr16[5] = htons(0xffff);
1168 bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
1169 in6p = in6_pcblookup_connect(&tcbtable, &s,
1170 th->th_sport, &d, th->th_dport, 0);
1171 if (in6p == 0) {
1172 ++tcpstat.tcps_pcbhashmiss;
1173 in6p = in6_pcblookup_bind(&tcbtable, &d,
1174 th->th_dport, 0);
1175 }
1176 }
1177 #endif
1178 #ifndef INET6
1179 if (inp == 0)
1180 #else
1181 if (inp == 0 && in6p == 0)
1182 #endif
1183 {
1184 ++tcpstat.tcps_noport;
1185 if (tcp_log_refused &&
1186 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
1187 tcp4_log_refused(ip, th);
1188 }
1189 TCP_FIELDS_TO_HOST(th);
1190 goto dropwithreset_ratelim;
1191 }
1192 #if defined(IPSEC) || defined(FAST_IPSEC)
1193 if (inp && (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0 &&
1194 ipsec4_in_reject(m, inp)) {
1195 ipsecstat.in_polvio++;
1196 goto drop;
1197 }
1198 #ifdef INET6
1199 else if (in6p &&
1200 (in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 &&
1201 ipsec4_in_reject_so(m, in6p->in6p_socket)) {
1202 ipsecstat.in_polvio++;
1203 goto drop;
1204 }
1205 #endif
1206 #endif /*IPSEC*/
1207 break;
1208 #endif /*INET*/
1209 #ifdef INET6
1210 case AF_INET6:
1211 {
1212 int faith;
1213
1214 #if defined(NFAITH) && NFAITH > 0
1215 faith = faithprefix(&ip6->ip6_dst);
1216 #else
1217 faith = 0;
1218 #endif
1219 in6p = in6_pcblookup_connect(&tcbtable, &ip6->ip6_src,
1220 th->th_sport, &ip6->ip6_dst, th->th_dport, faith);
1221 if (in6p == NULL) {
1222 ++tcpstat.tcps_pcbhashmiss;
1223 in6p = in6_pcblookup_bind(&tcbtable, &ip6->ip6_dst,
1224 th->th_dport, faith);
1225 }
1226 if (in6p == NULL) {
1227 ++tcpstat.tcps_noport;
1228 if (tcp_log_refused &&
1229 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
1230 tcp6_log_refused(ip6, th);
1231 }
1232 TCP_FIELDS_TO_HOST(th);
1233 goto dropwithreset_ratelim;
1234 }
1235 #if defined(IPSEC) || defined(FAST_IPSEC)
1236 if ((in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 &&
1237 ipsec6_in_reject(m, in6p)) {
1238 ipsec6stat.in_polvio++;
1239 goto drop;
1240 }
1241 #endif /*IPSEC*/
1242 break;
1243 }
1244 #endif
1245 }
1246
1247 /*
1248 * If the state is CLOSED (i.e., TCB does not exist) then
1249 * all data in the incoming segment is discarded.
1250 * If the TCB exists but is in CLOSED state, it is embryonic,
1251 * but should either do a listen or a connect soon.
1252 */
1253 tp = NULL;
1254 so = NULL;
1255 if (inp) {
1256 tp = intotcpcb(inp);
1257 so = inp->inp_socket;
1258 }
1259 #ifdef INET6
1260 else if (in6p) {
1261 tp = in6totcpcb(in6p);
1262 so = in6p->in6p_socket;
1263 }
1264 #endif
1265 if (tp == 0) {
1266 TCP_FIELDS_TO_HOST(th);
1267 goto dropwithreset_ratelim;
1268 }
1269 if (tp->t_state == TCPS_CLOSED)
1270 goto drop;
1271
1272 /*
1273 * Checksum extended TCP header and data.
1274 */
1275 if (tcp_input_checksum(af, m, th, toff, off, tlen))
1276 goto badcsum;
1277
1278 TCP_FIELDS_TO_HOST(th);
1279
1280 /* Unscale the window into a 32-bit value. */
1281 if ((tiflags & TH_SYN) == 0)
1282 tiwin = th->th_win << tp->snd_scale;
1283 else
1284 tiwin = th->th_win;
1285
1286 #ifdef INET6
1287 /* save packet options if user wanted */
1288 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) {
1289 if (in6p->in6p_options) {
1290 m_freem(in6p->in6p_options);
1291 in6p->in6p_options = 0;
1292 }
1293 ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m);
1294 }
1295 #endif
1296
1297 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
1298 union syn_cache_sa src;
1299 union syn_cache_sa dst;
1300
1301 bzero(&src, sizeof(src));
1302 bzero(&dst, sizeof(dst));
1303 switch (af) {
1304 #ifdef INET
1305 case AF_INET:
1306 src.sin.sin_len = sizeof(struct sockaddr_in);
1307 src.sin.sin_family = AF_INET;
1308 src.sin.sin_addr = ip->ip_src;
1309 src.sin.sin_port = th->th_sport;
1310
1311 dst.sin.sin_len = sizeof(struct sockaddr_in);
1312 dst.sin.sin_family = AF_INET;
1313 dst.sin.sin_addr = ip->ip_dst;
1314 dst.sin.sin_port = th->th_dport;
1315 break;
1316 #endif
1317 #ifdef INET6
1318 case AF_INET6:
1319 src.sin6.sin6_len = sizeof(struct sockaddr_in6);
1320 src.sin6.sin6_family = AF_INET6;
1321 src.sin6.sin6_addr = ip6->ip6_src;
1322 src.sin6.sin6_port = th->th_sport;
1323
1324 dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
1325 dst.sin6.sin6_family = AF_INET6;
1326 dst.sin6.sin6_addr = ip6->ip6_dst;
1327 dst.sin6.sin6_port = th->th_dport;
1328 break;
1329 #endif /* INET6 */
1330 default:
1331 goto badsyn; /*sanity*/
1332 }
1333
1334 if (so->so_options & SO_DEBUG) {
1335 #ifdef TCP_DEBUG
1336 ostate = tp->t_state;
1337 #endif
1338
1339 tcp_saveti = NULL;
1340 if (iphlen + sizeof(struct tcphdr) > MHLEN)
1341 goto nosave;
1342
1343 if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
1344 tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
1345 if (!tcp_saveti)
1346 goto nosave;
1347 } else {
1348 MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
1349 if (!tcp_saveti)
1350 goto nosave;
1351 MCLAIM(m, &tcp_mowner);
1352 tcp_saveti->m_len = iphlen;
1353 m_copydata(m, 0, iphlen,
1354 mtod(tcp_saveti, caddr_t));
1355 }
1356
1357 if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
1358 m_freem(tcp_saveti);
1359 tcp_saveti = NULL;
1360 } else {
1361 tcp_saveti->m_len += sizeof(struct tcphdr);
1362 bcopy(th, mtod(tcp_saveti, caddr_t) + iphlen,
1363 sizeof(struct tcphdr));
1364 }
1365 nosave:;
1366 }
1367 if (so->so_options & SO_ACCEPTCONN) {
1368 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
1369 if (tiflags & TH_RST) {
1370 syn_cache_reset(&src.sa, &dst.sa, th);
1371 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
1372 (TH_ACK|TH_SYN)) {
1373 /*
1374 * Received a SYN,ACK. This should
1375 * never happen while we are in
1376 * LISTEN. Send an RST.
1377 */
1378 goto badsyn;
1379 } else if (tiflags & TH_ACK) {
1380 so = syn_cache_get(&src.sa, &dst.sa,
1381 th, toff, tlen, so, m);
1382 if (so == NULL) {
1383 /*
1384 * We don't have a SYN for
1385 * this ACK; send an RST.
1386 */
1387 goto badsyn;
1388 } else if (so ==
1389 (struct socket *)(-1)) {
1390 /*
1391 * We were unable to create
1392 * the connection. If the
1393 * 3-way handshake was
1394 * completed, and RST has
1395 * been sent to the peer.
1396 * Since the mbuf might be
1397 * in use for the reply,
1398 * do not free it.
1399 */
1400 m = NULL;
1401 } else {
1402 /*
1403 * We have created a
1404 * full-blown connection.
1405 */
1406 tp = NULL;
1407 inp = NULL;
1408 #ifdef INET6
1409 in6p = NULL;
1410 #endif
1411 switch (so->so_proto->pr_domain->dom_family) {
1412 #ifdef INET
1413 case AF_INET:
1414 inp = sotoinpcb(so);
1415 tp = intotcpcb(inp);
1416 break;
1417 #endif
1418 #ifdef INET6
1419 case AF_INET6:
1420 in6p = sotoin6pcb(so);
1421 tp = in6totcpcb(in6p);
1422 break;
1423 #endif
1424 }
1425 if (tp == NULL)
1426 goto badsyn; /*XXX*/
1427 tiwin <<= tp->snd_scale;
1428 goto after_listen;
1429 }
1430 } else {
1431 /*
1432 * None of RST, SYN or ACK was set.
1433 * This is an invalid packet for a
1434 * TCB in LISTEN state. Send a RST.
1435 */
1436 goto badsyn;
1437 }
1438 } else {
1439 /*
1440 * Received a SYN.
1441 */
1442
1443 #ifdef INET6
1444 /*
1445 * If deprecated address is forbidden, we do
1446 * not accept SYN to deprecated interface
1447 * address to prevent any new inbound
1448 * connection from getting established.
1449 * When we do not accept SYN, we send a TCP
1450 * RST, with deprecated source address (instead
1451 * of dropping it). We compromise it as it is
1452 * much better for peer to send a RST, and
1453 * RST will be the final packet for the
1454 * exchange.
1455 *
1456 * If we do not forbid deprecated addresses, we
1457 * accept the SYN packet. RFC2462 does not
1458 * suggest dropping SYN in this case.
1459 * If we decipher RFC2462 5.5.4, it says like
1460 * this:
1461 * 1. use of deprecated addr with existing
1462 * communication is okay - "SHOULD continue
1463 * to be used"
1464 * 2. use of it with new communication:
1465 * (2a) "SHOULD NOT be used if alternate
1466 * address with sufficient scope is
1467 * available"
1468 * (2b) nothing mentioned otherwise.
1469 * Here we fall into (2b) case as we have no
1470 * choice in our source address selection - we
1471 * must obey the peer.
1472 *
1473 * The wording in RFC2462 is confusing, and
1474 * there are multiple description text for
1475 * deprecated address handling - worse, they
1476 * are not exactly the same. I believe 5.5.4
1477 * is the best one, so we follow 5.5.4.
1478 */
1479 if (af == AF_INET6 && !ip6_use_deprecated) {
1480 struct in6_ifaddr *ia6;
1481 if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif,
1482 &ip6->ip6_dst)) &&
1483 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1484 tp = NULL;
1485 goto dropwithreset;
1486 }
1487 }
1488 #endif
1489
1490 #ifdef IPSEC
1491 switch (af) {
1492 #ifdef INET
1493 case AF_INET:
1494 if (ipsec4_in_reject_so(m, so)) {
1495 ipsecstat.in_polvio++;
1496 tp = NULL;
1497 goto dropwithreset;
1498 }
1499 break;
1500 #endif
1501 #ifdef INET6
1502 case AF_INET6:
1503 if (ipsec6_in_reject_so(m, so)) {
1504 ipsec6stat.in_polvio++;
1505 tp = NULL;
1506 goto dropwithreset;
1507 }
1508 break;
1509 #endif
1510 }
1511 #endif
1512
1513 /*
1514 * LISTEN socket received a SYN
1515 * from itself? This can't possibly
1516 * be valid; drop the packet.
1517 */
1518 if (th->th_sport == th->th_dport) {
1519 int i;
1520
1521 switch (af) {
1522 #ifdef INET
1523 case AF_INET:
1524 i = in_hosteq(ip->ip_src, ip->ip_dst);
1525 break;
1526 #endif
1527 #ifdef INET6
1528 case AF_INET6:
1529 i = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst);
1530 break;
1531 #endif
1532 default:
1533 i = 1;
1534 }
1535 if (i) {
1536 tcpstat.tcps_badsyn++;
1537 goto drop;
1538 }
1539 }
1540
1541 /*
1542 * SYN looks ok; create compressed TCP
1543 * state for it.
1544 */
1545 if (so->so_qlen <= so->so_qlimit &&
1546 syn_cache_add(&src.sa, &dst.sa, th, tlen,
1547 so, m, optp, optlen, &opti))
1548 m = NULL;
1549 }
1550 goto drop;
1551 }
1552 }
1553
1554 after_listen:
1555 #ifdef DIAGNOSTIC
1556 /*
1557 * Should not happen now that all embryonic connections
1558 * are handled with compressed state.
1559 */
1560 if (tp->t_state == TCPS_LISTEN)
1561 panic("tcp_input: TCPS_LISTEN");
1562 #endif
1563
1564 /*
1565 * Segment received on connection.
1566 * Reset idle time and keep-alive timer.
1567 */
1568 tp->t_rcvtime = tcp_now;
1569 if (TCPS_HAVEESTABLISHED(tp->t_state))
1570 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1571
1572 /*
1573 * Process options.
1574 */
1575 #ifdef TCP_SIGNATURE
1576 if (optp || (tp->t_flags & TF_SIGNATURE))
1577 #else
1578 if (optp)
1579 #endif
1580 if (tcp_dooptions(tp, optp, optlen, th, m, toff, &opti) < 0)
1581 goto drop;
1582
1583 if (TCP_SACK_ENABLED(tp)) {
1584 tcp_del_sackholes(tp, th);
1585 }
1586
1587 if (opti.ts_present && opti.ts_ecr) {
1588 /*
1589 * Calculate the RTT from the returned time stamp and the
1590 * connection's time base. If the time stamp is later than
1591 * the current time, or is extremely old, fall back to non-1323
1592 * RTT calculation. Since ts_ecr is unsigned, we can test both
1593 * at the same time.
1594 */
1595 ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1;
1596 if (ts_rtt > TCP_PAWS_IDLE)
1597 ts_rtt = 0;
1598 } else {
1599 ts_rtt = 0;
1600 }
1601
1602 /*
1603 * Header prediction: check for the two common cases
1604 * of a uni-directional data xfer. If the packet has
1605 * no control flags, is in-sequence, the window didn't
1606 * change and we're not retransmitting, it's a
1607 * candidate. If the length is zero and the ack moved
1608 * forward, we're the sender side of the xfer. Just
1609 * free the data acked & wake any higher level process
1610 * that was blocked waiting for space. If the length
1611 * is non-zero and the ack didn't move, we're the
1612 * receiver side. If we're getting packets in-order
1613 * (the reassembly queue is empty), add the data to
1614 * the socket buffer and note that we need a delayed ack.
1615 */
1616 if (tp->t_state == TCPS_ESTABLISHED &&
1617 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1618 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
1619 th->th_seq == tp->rcv_nxt &&
1620 tiwin && tiwin == tp->snd_wnd &&
1621 tp->snd_nxt == tp->snd_max) {
1622
1623 /*
1624 * If last ACK falls within this segment's sequence numbers,
1625 * record the timestamp.
1626 * NOTE:
1627 * 1) That the test incorporates suggestions from the latest
1628 * proposal of the tcplw (at) cray.com list (Braden 1993/04/26).
1629 * 2) That updating only on newer timestamps interferes with
1630 * our earlier PAWS tests, so this check should be solely
1631 * predicated on the sequence space of this segment.
1632 * 3) That we modify the segment boundary check to be
1633 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
1634 * instead of RFC1323's
1635 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
1636 * This modified check allows us to overcome RFC1323's
1637 * limitations as described in Stevens TCP/IP Illustrated
1638 * Vol. 2 p.869. In such cases, we can still calculate the
1639 * RTT correctly when RCV.NXT == Last.ACK.Sent.
1640 */
1641 if (opti.ts_present &&
1642 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1643 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1644 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
1645 tp->ts_recent_age = tcp_now;
1646 tp->ts_recent = opti.ts_val;
1647 }
1648
1649 if (tlen == 0) {
1650 /* Ack prediction. */
1651 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1652 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1653 tp->snd_cwnd >= tp->snd_wnd &&
1654 tp->t_partialacks < 0) {
1655 /*
1656 * this is a pure ack for outstanding data.
1657 */
1658 ++tcpstat.tcps_predack;
1659 if (ts_rtt)
1660 tcp_xmit_timer(tp, ts_rtt);
1661 else if (tp->t_rtttime &&
1662 SEQ_GT(th->th_ack, tp->t_rtseq))
1663 tcp_xmit_timer(tp,
1664 tcp_now - tp->t_rtttime);
1665 acked = th->th_ack - tp->snd_una;
1666 tcpstat.tcps_rcvackpack++;
1667 tcpstat.tcps_rcvackbyte += acked;
1668 ND6_HINT(tp);
1669
1670 if (acked > (tp->t_lastoff - tp->t_inoff))
1671 tp->t_lastm = NULL;
1672 sbdrop(&so->so_snd, acked);
1673 tp->t_lastoff -= acked;
1674
1675 ICMP_CHECK(tp, th, acked);
1676
1677 tp->snd_una = th->th_ack;
1678 tp->snd_fack = tp->snd_una;
1679 if (SEQ_LT(tp->snd_high, tp->snd_una))
1680 tp->snd_high = tp->snd_una;
1681 m_freem(m);
1682
1683 /*
1684 * If all outstanding data are acked, stop
1685 * retransmit timer, otherwise restart timer
1686 * using current (possibly backed-off) value.
1687 * If process is waiting for space,
1688 * wakeup/selwakeup/signal. If data
1689 * are ready to send, let tcp_output
1690 * decide between more output or persist.
1691 */
1692 if (tp->snd_una == tp->snd_max)
1693 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1694 else if (TCP_TIMER_ISARMED(tp,
1695 TCPT_PERSIST) == 0)
1696 TCP_TIMER_ARM(tp, TCPT_REXMT,
1697 tp->t_rxtcur);
1698
1699 sowwakeup(so);
1700 if (so->so_snd.sb_cc)
1701 (void) tcp_output(tp);
1702 if (tcp_saveti)
1703 m_freem(tcp_saveti);
1704 return;
1705 }
1706 } else if (th->th_ack == tp->snd_una &&
1707 TAILQ_FIRST(&tp->segq) == NULL &&
1708 tlen <= sbspace(&so->so_rcv)) {
1709 /*
1710 * this is a pure, in-sequence data packet
1711 * with nothing on the reassembly queue and
1712 * we have enough buffer space to take it.
1713 */
1714 ++tcpstat.tcps_preddat;
1715 tp->rcv_nxt += tlen;
1716 tcpstat.tcps_rcvpack++;
1717 tcpstat.tcps_rcvbyte += tlen;
1718 ND6_HINT(tp);
1719 /*
1720 * Drop TCP, IP headers and TCP options then add data
1721 * to socket buffer.
1722 */
1723 if (so->so_state & SS_CANTRCVMORE)
1724 m_freem(m);
1725 else {
1726 m_adj(m, toff + off);
1727 sbappendstream(&so->so_rcv, m);
1728 }
1729 sorwakeup(so);
1730 TCP_SETUP_ACK(tp, th);
1731 if (tp->t_flags & TF_ACKNOW)
1732 (void) tcp_output(tp);
1733 if (tcp_saveti)
1734 m_freem(tcp_saveti);
1735 return;
1736 }
1737 }
1738
1739 /*
1740 * Compute mbuf offset to TCP data segment.
1741 */
1742 hdroptlen = toff + off;
1743
1744 /*
1745 * Calculate amount of space in receive window,
1746 * and then do TCP input processing.
1747 * Receive window is amount of space in rcv queue,
1748 * but not less than advertised window.
1749 */
1750 { int win;
1751
1752 win = sbspace(&so->so_rcv);
1753 if (win < 0)
1754 win = 0;
1755 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1756 }
1757
1758 switch (tp->t_state) {
1759 case TCPS_LISTEN:
1760 /*
1761 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1762 */
1763 if (m->m_flags & (M_BCAST|M_MCAST))
1764 goto drop;
1765 switch (af) {
1766 #ifdef INET6
1767 case AF_INET6:
1768 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
1769 goto drop;
1770 break;
1771 #endif /* INET6 */
1772 case AF_INET:
1773 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1774 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
1775 goto drop;
1776 break;
1777 }
1778 break;
1779
1780 /*
1781 * If the state is SYN_SENT:
1782 * if seg contains an ACK, but not for our SYN, drop the input.
1783 * if seg contains a RST, then drop the connection.
1784 * if seg does not contain SYN, then drop it.
1785 * Otherwise this is an acceptable SYN segment
1786 * initialize tp->rcv_nxt and tp->irs
1787 * if seg contains ack then advance tp->snd_una
1788 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1789 * arrange for segment to be acked (eventually)
1790 * continue processing rest of data/controls, beginning with URG
1791 */
1792 case TCPS_SYN_SENT:
1793 if ((tiflags & TH_ACK) &&
1794 (SEQ_LEQ(th->th_ack, tp->iss) ||
1795 SEQ_GT(th->th_ack, tp->snd_max)))
1796 goto dropwithreset;
1797 if (tiflags & TH_RST) {
1798 if (tiflags & TH_ACK)
1799 tp = tcp_drop(tp, ECONNREFUSED);
1800 goto drop;
1801 }
1802 if ((tiflags & TH_SYN) == 0)
1803 goto drop;
1804 if (tiflags & TH_ACK) {
1805 tp->snd_una = th->th_ack;
1806 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1807 tp->snd_nxt = tp->snd_una;
1808 if (SEQ_LT(tp->snd_high, tp->snd_una))
1809 tp->snd_high = tp->snd_una;
1810 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1811 }
1812 tp->irs = th->th_seq;
1813 tcp_rcvseqinit(tp);
1814 tp->t_flags |= TF_ACKNOW;
1815 tcp_mss_from_peer(tp, opti.maxseg);
1816
1817 /*
1818 * Initialize the initial congestion window. If we
1819 * had to retransmit the SYN, we must initialize cwnd
1820 * to 1 segment (i.e. the Loss Window).
1821 */
1822 if (tp->t_flags & TF_SYN_REXMT)
1823 tp->snd_cwnd = tp->t_peermss;
1824 else {
1825 int ss = tcp_init_win;
1826 #ifdef INET
1827 if (inp != NULL && in_localaddr(inp->inp_faddr))
1828 ss = tcp_init_win_local;
1829 #endif
1830 #ifdef INET6
1831 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
1832 ss = tcp_init_win_local;
1833 #endif
1834 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
1835 }
1836
1837 tcp_rmx_rtt(tp);
1838 if (tiflags & TH_ACK) {
1839 tcpstat.tcps_connects++;
1840 soisconnected(so);
1841 tcp_established(tp);
1842 /* Do window scaling on this connection? */
1843 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1844 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1845 tp->snd_scale = tp->requested_s_scale;
1846 tp->rcv_scale = tp->request_r_scale;
1847 }
1848 TCP_REASS_LOCK(tp);
1849 (void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
1850 TCP_REASS_UNLOCK(tp);
1851 /*
1852 * if we didn't have to retransmit the SYN,
1853 * use its rtt as our initial srtt & rtt var.
1854 */
1855 if (tp->t_rtttime)
1856 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
1857 } else
1858 tp->t_state = TCPS_SYN_RECEIVED;
1859
1860 /*
1861 * Advance th->th_seq to correspond to first data byte.
1862 * If data, trim to stay within window,
1863 * dropping FIN if necessary.
1864 */
1865 th->th_seq++;
1866 if (tlen > tp->rcv_wnd) {
1867 todrop = tlen - tp->rcv_wnd;
1868 m_adj(m, -todrop);
1869 tlen = tp->rcv_wnd;
1870 tiflags &= ~TH_FIN;
1871 tcpstat.tcps_rcvpackafterwin++;
1872 tcpstat.tcps_rcvbyteafterwin += todrop;
1873 }
1874 tp->snd_wl1 = th->th_seq - 1;
1875 tp->rcv_up = th->th_seq;
1876 goto step6;
1877
1878 /*
1879 * If the state is SYN_RECEIVED:
1880 * If seg contains an ACK, but not for our SYN, drop the input
1881 * and generate an RST. See page 36, rfc793
1882 */
1883 case TCPS_SYN_RECEIVED:
1884 if ((tiflags & TH_ACK) &&
1885 (SEQ_LEQ(th->th_ack, tp->iss) ||
1886 SEQ_GT(th->th_ack, tp->snd_max)))
1887 goto dropwithreset;
1888 break;
1889 }
1890
1891 /*
1892 * States other than LISTEN or SYN_SENT.
1893 * First check timestamp, if present.
1894 * Then check that at least some bytes of segment are within
1895 * receive window. If segment begins before rcv_nxt,
1896 * drop leading data (and SYN); if nothing left, just ack.
1897 *
1898 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1899 * and it's less than ts_recent, drop it.
1900 */
1901 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
1902 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
1903
1904 /* Check to see if ts_recent is over 24 days old. */
1905 if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) {
1906 /*
1907 * Invalidate ts_recent. If this segment updates
1908 * ts_recent, the age will be reset later and ts_recent
1909 * will get a valid value. If it does not, setting
1910 * ts_recent to zero will at least satisfy the
1911 * requirement that zero be placed in the timestamp
1912 * echo reply when ts_recent isn't valid. The
1913 * age isn't reset until we get a valid ts_recent
1914 * because we don't want out-of-order segments to be
1915 * dropped when ts_recent is old.
1916 */
1917 tp->ts_recent = 0;
1918 } else {
1919 tcpstat.tcps_rcvduppack++;
1920 tcpstat.tcps_rcvdupbyte += tlen;
1921 tcpstat.tcps_pawsdrop++;
1922 tcp_new_dsack(tp, th->th_seq, tlen);
1923 goto dropafterack;
1924 }
1925 }
1926
1927 todrop = tp->rcv_nxt - th->th_seq;
1928 dupseg = FALSE;
1929 if (todrop > 0) {
1930 if (tiflags & TH_SYN) {
1931 tiflags &= ~TH_SYN;
1932 th->th_seq++;
1933 if (th->th_urp > 1)
1934 th->th_urp--;
1935 else {
1936 tiflags &= ~TH_URG;
1937 th->th_urp = 0;
1938 }
1939 todrop--;
1940 }
1941 if (todrop > tlen ||
1942 (todrop == tlen && (tiflags & TH_FIN) == 0)) {
1943 /*
1944 * Any valid FIN or RST must be to the left of the
1945 * window. At this point the FIN or RST must be a
1946 * duplicate or out of sequence; drop it.
1947 */
1948 if (tiflags & TH_RST)
1949 goto drop;
1950 tiflags &= ~(TH_FIN|TH_RST);
1951 /*
1952 * Send an ACK to resynchronize and drop any data.
1953 * But keep on processing for RST or ACK.
1954 */
1955 tp->t_flags |= TF_ACKNOW;
1956 todrop = tlen;
1957 dupseg = TRUE;
1958 tcpstat.tcps_rcvdupbyte += todrop;
1959 tcpstat.tcps_rcvduppack++;
1960 } else if ((tiflags & TH_RST) &&
1961 th->th_seq != tp->last_ack_sent) {
1962 /*
1963 * Test for reset before adjusting the sequence
1964 * number for overlapping data.
1965 */
1966 goto dropafterack_ratelim;
1967 } else {
1968 tcpstat.tcps_rcvpartduppack++;
1969 tcpstat.tcps_rcvpartdupbyte += todrop;
1970 }
1971 tcp_new_dsack(tp, th->th_seq, todrop);
1972 hdroptlen += todrop; /*drop from head afterwards*/
1973 th->th_seq += todrop;
1974 tlen -= todrop;
1975 if (th->th_urp > todrop)
1976 th->th_urp -= todrop;
1977 else {
1978 tiflags &= ~TH_URG;
1979 th->th_urp = 0;
1980 }
1981 }
1982
1983 /*
1984 * If new data are received on a connection after the
1985 * user processes are gone, then RST the other end.
1986 */
1987 if ((so->so_state & SS_NOFDREF) &&
1988 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1989 tp = tcp_close(tp);
1990 tcpstat.tcps_rcvafterclose++;
1991 goto dropwithreset;
1992 }
1993
1994 /*
1995 * If segment ends after window, drop trailing data
1996 * (and PUSH and FIN); if nothing left, just ACK.
1997 */
1998 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1999 if (todrop > 0) {
2000 tcpstat.tcps_rcvpackafterwin++;
2001 if (todrop >= tlen) {
2002 /*
2003 * The segment actually starts after the window.
2004 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen
2005 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0
2006 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd
2007 */
2008 tcpstat.tcps_rcvbyteafterwin += tlen;
2009 /*
2010 * If a new connection request is received
2011 * while in TIME_WAIT, drop the old connection
2012 * and start over if the sequence numbers
2013 * are above the previous ones.
2014 *
2015 * NOTE: We will checksum the packet again, and
2016 * so we need to put the header fields back into
2017 * network order!
2018 * XXX This kind of sucks, but we don't expect
2019 * XXX this to happen very often, so maybe it
2020 * XXX doesn't matter so much.
2021 */
2022 if (tiflags & TH_SYN &&
2023 tp->t_state == TCPS_TIME_WAIT &&
2024 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
2025 iss = tcp_new_iss(tp, tp->snd_nxt);
2026 tp = tcp_close(tp);
2027 TCP_FIELDS_TO_NET(th);
2028 goto findpcb;
2029 }
2030 /*
2031 * If window is closed can only take segments at
2032 * window edge, and have to drop data and PUSH from
2033 * incoming segments. Continue processing, but
2034 * remember to ack. Otherwise, drop segment
2035 * and (if not RST) ack.
2036 */
2037 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2038 tp->t_flags |= TF_ACKNOW;
2039 tcpstat.tcps_rcvwinprobe++;
2040 } else
2041 goto dropafterack;
2042 } else
2043 tcpstat.tcps_rcvbyteafterwin += todrop;
2044 m_adj(m, -todrop);
2045 tlen -= todrop;
2046 tiflags &= ~(TH_PUSH|TH_FIN);
2047 }
2048
2049 /*
2050 * If last ACK falls within this segment's sequence numbers,
2051 * and the timestamp is newer, record it.
2052 */
2053 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
2054 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2055 SEQ_LT(tp->last_ack_sent, th->th_seq + tlen +
2056 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
2057 tp->ts_recent_age = tcp_now;
2058 tp->ts_recent = opti.ts_val;
2059 }
2060
2061 /*
2062 * If the RST bit is set examine the state:
2063 * SYN_RECEIVED STATE:
2064 * If passive open, return to LISTEN state.
2065 * If active open, inform user that connection was refused.
2066 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
2067 * Inform user that connection was reset, and close tcb.
2068 * CLOSING, LAST_ACK, TIME_WAIT STATES
2069 * Close the tcb.
2070 */
2071 if (tiflags & TH_RST) {
2072 if (th->th_seq != tp->last_ack_sent)
2073 goto dropafterack_ratelim;
2074
2075 switch (tp->t_state) {
2076 case TCPS_SYN_RECEIVED:
2077 so->so_error = ECONNREFUSED;
2078 goto close;
2079
2080 case TCPS_ESTABLISHED:
2081 case TCPS_FIN_WAIT_1:
2082 case TCPS_FIN_WAIT_2:
2083 case TCPS_CLOSE_WAIT:
2084 so->so_error = ECONNRESET;
2085 close:
2086 tp->t_state = TCPS_CLOSED;
2087 tcpstat.tcps_drops++;
2088 tp = tcp_close(tp);
2089 goto drop;
2090
2091 case TCPS_CLOSING:
2092 case TCPS_LAST_ACK:
2093 case TCPS_TIME_WAIT:
2094 tp = tcp_close(tp);
2095 goto drop;
2096 }
2097 }
2098
2099 /*
2100 * Since we've covered the SYN-SENT and SYN-RECEIVED states above
2101 * we must be in a synchronized state. RFC791 states (under RST
2102 * generation) that any unacceptable segment (an out-of-order SYN
2103 * qualifies) received in a synchronized state must elicit only an
2104 * empty acknowledgment segment ... and the connection remains in
2105 * the same state.
2106 */
2107 if (tiflags & TH_SYN) {
2108 if (tp->rcv_nxt == th->th_seq) {
2109 tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1,
2110 TH_ACK);
2111 if (tcp_saveti)
2112 m_freem(tcp_saveti);
2113 return;
2114 }
2115
2116 goto dropafterack_ratelim;
2117 }
2118
2119 /*
2120 * If the ACK bit is off we drop the segment and return.
2121 */
2122 if ((tiflags & TH_ACK) == 0) {
2123 if (tp->t_flags & TF_ACKNOW)
2124 goto dropafterack;
2125 else
2126 goto drop;
2127 }
2128
2129 /*
2130 * Ack processing.
2131 */
2132 switch (tp->t_state) {
2133
2134 /*
2135 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
2136 * ESTABLISHED state and continue processing, otherwise
2137 * send an RST.
2138 */
2139 case TCPS_SYN_RECEIVED:
2140 if (SEQ_GT(tp->snd_una, th->th_ack) ||
2141 SEQ_GT(th->th_ack, tp->snd_max))
2142 goto dropwithreset;
2143 tcpstat.tcps_connects++;
2144 soisconnected(so);
2145 tcp_established(tp);
2146 /* Do window scaling? */
2147 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2148 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2149 tp->snd_scale = tp->requested_s_scale;
2150 tp->rcv_scale = tp->request_r_scale;
2151 }
2152 TCP_REASS_LOCK(tp);
2153 (void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
2154 TCP_REASS_UNLOCK(tp);
2155 tp->snd_wl1 = th->th_seq - 1;
2156 /* fall into ... */
2157
2158 /*
2159 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2160 * ACKs. If the ack is in the range
2161 * tp->snd_una < th->th_ack <= tp->snd_max
2162 * then advance tp->snd_una to th->th_ack and drop
2163 * data from the retransmission queue. If this ACK reflects
2164 * more up to date window information we update our window information.
2165 */
2166 case TCPS_ESTABLISHED:
2167 case TCPS_FIN_WAIT_1:
2168 case TCPS_FIN_WAIT_2:
2169 case TCPS_CLOSE_WAIT:
2170 case TCPS_CLOSING:
2171 case TCPS_LAST_ACK:
2172 case TCPS_TIME_WAIT:
2173
2174 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2175 if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) {
2176 tcpstat.tcps_rcvdupack++;
2177 /*
2178 * If we have outstanding data (other than
2179 * a window probe), this is a completely
2180 * duplicate ack (ie, window info didn't
2181 * change), the ack is the biggest we've
2182 * seen and we've seen exactly our rexmt
2183 * threshhold of them, assume a packet
2184 * has been dropped and retransmit it.
2185 * Kludge snd_nxt & the congestion
2186 * window so we send only this one
2187 * packet.
2188 *
2189 * We know we're losing at the current
2190 * window size so do congestion avoidance
2191 * (set ssthresh to half the current window
2192 * and pull our congestion window back to
2193 * the new ssthresh).
2194 *
2195 * Dup acks mean that packets have left the
2196 * network (they're now cached at the receiver)
2197 * so bump cwnd by the amount in the receiver
2198 * to keep a constant cwnd packets in the
2199 * network.
2200 *
2201 * If we are using TCP/SACK, then enter
2202 * Fast Recovery if the receiver SACKs
2203 * data that is tcprexmtthresh * MSS
2204 * bytes past the last ACKed segment,
2205 * irrespective of the number of DupAcks.
2206 */
2207 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
2208 th->th_ack != tp->snd_una)
2209 tp->t_dupacks = 0;
2210 else if (tp->t_partialacks < 0 &&
2211 (++tp->t_dupacks == tcprexmtthresh ||
2212 TCP_FACK_FASTRECOV(tp))) {
2213 tcp_seq onxt;
2214 u_int win;
2215
2216 if (tcp_do_newreno &&
2217 SEQ_LT(th->th_ack, tp->snd_high)) {
2218 /*
2219 * False fast retransmit after
2220 * timeout. Do not enter fast
2221 * recovery.
2222 */
2223 tp->t_dupacks = 0;
2224 break;
2225 }
2226
2227 onxt = tp->snd_nxt;
2228 win = min(tp->snd_wnd, tp->snd_cwnd) /
2229 2 / tp->t_segsz;
2230 if (win < 2)
2231 win = 2;
2232 tp->snd_ssthresh = win * tp->t_segsz;
2233 tp->snd_recover = tp->snd_max;
2234 tp->t_partialacks = 0;
2235 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2236 tp->t_rtttime = 0;
2237 if (TCP_SACK_ENABLED(tp)) {
2238 tp->t_dupacks = tcprexmtthresh;
2239 tp->sack_newdata = tp->snd_nxt;
2240 tp->snd_cwnd = tp->t_segsz;
2241 (void) tcp_output(tp);
2242 goto drop;
2243 }
2244 tp->snd_nxt = th->th_ack;
2245 tp->snd_cwnd = tp->t_segsz;
2246 (void) tcp_output(tp);
2247 tp->snd_cwnd = tp->snd_ssthresh +
2248 tp->t_segsz * tp->t_dupacks;
2249 if (SEQ_GT(onxt, tp->snd_nxt))
2250 tp->snd_nxt = onxt;
2251 goto drop;
2252 } else if (tp->t_dupacks > tcprexmtthresh) {
2253 tp->snd_cwnd += tp->t_segsz;
2254 (void) tcp_output(tp);
2255 goto drop;
2256 }
2257 } else {
2258 /*
2259 * If the ack appears to be very old, only
2260 * allow data that is in-sequence. This
2261 * makes it somewhat more difficult to insert
2262 * forged data by guessing sequence numbers.
2263 * Sent an ack to try to update the send
2264 * sequence number on the other side.
2265 */
2266 if (tlen && th->th_seq != tp->rcv_nxt &&
2267 SEQ_LT(th->th_ack,
2268 tp->snd_una - tp->max_sndwnd))
2269 goto dropafterack;
2270 }
2271 break;
2272 }
2273 /*
2274 * If the congestion window was inflated to account
2275 * for the other side's cached packets, retract it.
2276 */
2277 if (TCP_SACK_ENABLED(tp))
2278 tcp_sack_newack(tp, th);
2279 else if (tcp_do_newreno)
2280 tcp_newreno_newack(tp, th);
2281 else
2282 tcp_reno_newack(tp, th);
2283 if (SEQ_GT(th->th_ack, tp->snd_max)) {
2284 tcpstat.tcps_rcvacktoomuch++;
2285 goto dropafterack;
2286 }
2287 acked = th->th_ack - tp->snd_una;
2288 tcpstat.tcps_rcvackpack++;
2289 tcpstat.tcps_rcvackbyte += acked;
2290
2291 /*
2292 * If we have a timestamp reply, update smoothed
2293 * round trip time. If no timestamp is present but
2294 * transmit timer is running and timed sequence
2295 * number was acked, update smoothed round trip time.
2296 * Since we now have an rtt measurement, cancel the
2297 * timer backoff (cf., Phil Karn's retransmit alg.).
2298 * Recompute the initial retransmit timer.
2299 */
2300 if (ts_rtt)
2301 tcp_xmit_timer(tp, ts_rtt);
2302 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
2303 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
2304
2305 /*
2306 * If all outstanding data is acked, stop retransmit
2307 * timer and remember to restart (more output or persist).
2308 * If there is more data to be acked, restart retransmit
2309 * timer, using current (possibly backed-off) value.
2310 */
2311 if (th->th_ack == tp->snd_max) {
2312 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2313 needoutput = 1;
2314 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
2315 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
2316 /*
2317 * When new data is acked, open the congestion window.
2318 * If the window gives us less than ssthresh packets
2319 * in flight, open exponentially (segsz per packet).
2320 * Otherwise open linearly: segsz per window
2321 * (segsz^2 / cwnd per packet).
2322 *
2323 * If we are still in fast recovery (meaning we are using
2324 * NewReno and we have only received partial acks), do not
2325 * inflate the window yet.
2326 */
2327 if (tp->t_partialacks < 0) {
2328 u_int cw = tp->snd_cwnd;
2329 u_int incr = tp->t_segsz;
2330
2331 if (cw >= tp->snd_ssthresh)
2332 incr = incr * incr / cw;
2333 tp->snd_cwnd = min(cw + incr,
2334 TCP_MAXWIN << tp->snd_scale);
2335 }
2336 ND6_HINT(tp);
2337 if (acked > so->so_snd.sb_cc) {
2338 tp->snd_wnd -= so->so_snd.sb_cc;
2339 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
2340 ourfinisacked = 1;
2341 } else {
2342 if (acked > (tp->t_lastoff - tp->t_inoff))
2343 tp->t_lastm = NULL;
2344 sbdrop(&so->so_snd, acked);
2345 tp->t_lastoff -= acked;
2346 tp->snd_wnd -= acked;
2347 ourfinisacked = 0;
2348 }
2349 sowwakeup(so);
2350
2351 ICMP_CHECK(tp, th, acked);
2352
2353 tp->snd_una = th->th_ack;
2354 if (SEQ_GT(tp->snd_una, tp->snd_fack))
2355 tp->snd_fack = tp->snd_una;
2356 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2357 tp->snd_nxt = tp->snd_una;
2358 if (SEQ_LT(tp->snd_high, tp->snd_una))
2359 tp->snd_high = tp->snd_una;
2360
2361 switch (tp->t_state) {
2362
2363 /*
2364 * In FIN_WAIT_1 STATE in addition to the processing
2365 * for the ESTABLISHED state if our FIN is now acknowledged
2366 * then enter FIN_WAIT_2.
2367 */
2368 case TCPS_FIN_WAIT_1:
2369 if (ourfinisacked) {
2370 /*
2371 * If we can't receive any more
2372 * data, then closing user can proceed.
2373 * Starting the timer is contrary to the
2374 * specification, but if we don't get a FIN
2375 * we'll hang forever.
2376 */
2377 if (so->so_state & SS_CANTRCVMORE) {
2378 soisdisconnected(so);
2379 if (tcp_maxidle > 0)
2380 TCP_TIMER_ARM(tp, TCPT_2MSL,
2381 tcp_maxidle);
2382 }
2383 tp->t_state = TCPS_FIN_WAIT_2;
2384 }
2385 break;
2386
2387 /*
2388 * In CLOSING STATE in addition to the processing for
2389 * the ESTABLISHED state if the ACK acknowledges our FIN
2390 * then enter the TIME-WAIT state, otherwise ignore
2391 * the segment.
2392 */
2393 case TCPS_CLOSING:
2394 if (ourfinisacked) {
2395 tp->t_state = TCPS_TIME_WAIT;
2396 tcp_canceltimers(tp);
2397 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2398 soisdisconnected(so);
2399 }
2400 break;
2401
2402 /*
2403 * In LAST_ACK, we may still be waiting for data to drain
2404 * and/or to be acked, as well as for the ack of our FIN.
2405 * If our FIN is now acknowledged, delete the TCB,
2406 * enter the closed state and return.
2407 */
2408 case TCPS_LAST_ACK:
2409 if (ourfinisacked) {
2410 tp = tcp_close(tp);
2411 goto drop;
2412 }
2413 break;
2414
2415 /*
2416 * In TIME_WAIT state the only thing that should arrive
2417 * is a retransmission of the remote FIN. Acknowledge
2418 * it and restart the finack timer.
2419 */
2420 case TCPS_TIME_WAIT:
2421 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2422 goto dropafterack;
2423 }
2424 }
2425
2426 step6:
2427 /*
2428 * Update window information.
2429 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2430 */
2431 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2432 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) ||
2433 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) {
2434 /* keep track of pure window updates */
2435 if (tlen == 0 &&
2436 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2437 tcpstat.tcps_rcvwinupd++;
2438 tp->snd_wnd = tiwin;
2439 tp->snd_wl1 = th->th_seq;
2440 tp->snd_wl2 = th->th_ack;
2441 if (tp->snd_wnd > tp->max_sndwnd)
2442 tp->max_sndwnd = tp->snd_wnd;
2443 needoutput = 1;
2444 }
2445
2446 /*
2447 * Process segments with URG.
2448 */
2449 if ((tiflags & TH_URG) && th->th_urp &&
2450 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2451 /*
2452 * This is a kludge, but if we receive and accept
2453 * random urgent pointers, we'll crash in
2454 * soreceive. It's hard to imagine someone
2455 * actually wanting to send this much urgent data.
2456 */
2457 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2458 th->th_urp = 0; /* XXX */
2459 tiflags &= ~TH_URG; /* XXX */
2460 goto dodata; /* XXX */
2461 }
2462 /*
2463 * If this segment advances the known urgent pointer,
2464 * then mark the data stream. This should not happen
2465 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2466 * a FIN has been received from the remote side.
2467 * In these states we ignore the URG.
2468 *
2469 * According to RFC961 (Assigned Protocols),
2470 * the urgent pointer points to the last octet
2471 * of urgent data. We continue, however,
2472 * to consider it to indicate the first octet
2473 * of data past the urgent section as the original
2474 * spec states (in one of two places).
2475 */
2476 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2477 tp->rcv_up = th->th_seq + th->th_urp;
2478 so->so_oobmark = so->so_rcv.sb_cc +
2479 (tp->rcv_up - tp->rcv_nxt) - 1;
2480 if (so->so_oobmark == 0)
2481 so->so_state |= SS_RCVATMARK;
2482 sohasoutofband(so);
2483 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2484 }
2485 /*
2486 * Remove out of band data so doesn't get presented to user.
2487 * This can happen independent of advancing the URG pointer,
2488 * but if two URG's are pending at once, some out-of-band
2489 * data may creep in... ick.
2490 */
2491 if (th->th_urp <= (u_int16_t) tlen
2492 #ifdef SO_OOBINLINE
2493 && (so->so_options & SO_OOBINLINE) == 0
2494 #endif
2495 )
2496 tcp_pulloutofband(so, th, m, hdroptlen);
2497 } else
2498 /*
2499 * If no out of band data is expected,
2500 * pull receive urgent pointer along
2501 * with the receive window.
2502 */
2503 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2504 tp->rcv_up = tp->rcv_nxt;
2505 dodata: /* XXX */
2506
2507 /*
2508 * Process the segment text, merging it into the TCP sequencing queue,
2509 * and arranging for acknowledgement of receipt if necessary.
2510 * This process logically involves adjusting tp->rcv_wnd as data
2511 * is presented to the user (this happens in tcp_usrreq.c,
2512 * case PRU_RCVD). If a FIN has already been received on this
2513 * connection then we just ignore the text.
2514 */
2515 if ((tlen || (tiflags & TH_FIN)) &&
2516 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2517 /*
2518 * Insert segment ti into reassembly queue of tcp with
2519 * control block tp. Return TH_FIN if reassembly now includes
2520 * a segment with FIN. The macro form does the common case
2521 * inline (segment is the next to be received on an
2522 * established connection, and the queue is empty),
2523 * avoiding linkage into and removal from the queue and
2524 * repetition of various conversions.
2525 * Set DELACK for segments received in order, but ack
2526 * immediately when segments are out of order
2527 * (so fast retransmit can work).
2528 */
2529 /* NOTE: this was TCP_REASS() macro, but used only once */
2530 TCP_REASS_LOCK(tp);
2531 if (th->th_seq == tp->rcv_nxt &&
2532 TAILQ_FIRST(&tp->segq) == NULL &&
2533 tp->t_state == TCPS_ESTABLISHED) {
2534 TCP_SETUP_ACK(tp, th);
2535 tp->rcv_nxt += tlen;
2536 tiflags = th->th_flags & TH_FIN;
2537 tcpstat.tcps_rcvpack++;
2538 tcpstat.tcps_rcvbyte += tlen;
2539 ND6_HINT(tp);
2540 if (so->so_state & SS_CANTRCVMORE)
2541 m_freem(m);
2542 else {
2543 m_adj(m, hdroptlen);
2544 sbappendstream(&(so)->so_rcv, m);
2545 }
2546 sorwakeup(so);
2547 } else {
2548 m_adj(m, hdroptlen);
2549 tiflags = tcp_reass(tp, th, m, &tlen);
2550 tp->t_flags |= TF_ACKNOW;
2551 }
2552 TCP_REASS_UNLOCK(tp);
2553
2554 /*
2555 * Note the amount of data that peer has sent into
2556 * our window, in order to estimate the sender's
2557 * buffer size.
2558 */
2559 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2560 } else {
2561 m_freem(m);
2562 m = NULL;
2563 tiflags &= ~TH_FIN;
2564 }
2565
2566 /*
2567 * If FIN is received ACK the FIN and let the user know
2568 * that the connection is closing. Ignore a FIN received before
2569 * the connection is fully established.
2570 */
2571 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
2572 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2573 socantrcvmore(so);
2574 tp->t_flags |= TF_ACKNOW;
2575 tp->rcv_nxt++;
2576 }
2577 switch (tp->t_state) {
2578
2579 /*
2580 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
2581 */
2582 case TCPS_ESTABLISHED:
2583 tp->t_state = TCPS_CLOSE_WAIT;
2584 break;
2585
2586 /*
2587 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2588 * enter the CLOSING state.
2589 */
2590 case TCPS_FIN_WAIT_1:
2591 tp->t_state = TCPS_CLOSING;
2592 break;
2593
2594 /*
2595 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2596 * starting the time-wait timer, turning off the other
2597 * standard timers.
2598 */
2599 case TCPS_FIN_WAIT_2:
2600 tp->t_state = TCPS_TIME_WAIT;
2601 tcp_canceltimers(tp);
2602 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2603 soisdisconnected(so);
2604 break;
2605
2606 /*
2607 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2608 */
2609 case TCPS_TIME_WAIT:
2610 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2611 break;
2612 }
2613 }
2614 #ifdef TCP_DEBUG
2615 if (so->so_options & SO_DEBUG)
2616 tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
2617 #endif
2618
2619 /*
2620 * Return any desired output.
2621 */
2622 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
2623 (void) tcp_output(tp);
2624 }
2625 if (tcp_saveti)
2626 m_freem(tcp_saveti);
2627 return;
2628
2629 badsyn:
2630 /*
2631 * Received a bad SYN. Increment counters and dropwithreset.
2632 */
2633 tcpstat.tcps_badsyn++;
2634 tp = NULL;
2635 goto dropwithreset;
2636
2637 dropafterack:
2638 /*
2639 * Generate an ACK dropping incoming segment if it occupies
2640 * sequence space, where the ACK reflects our state.
2641 */
2642 if (tiflags & TH_RST)
2643 goto drop;
2644 goto dropafterack2;
2645
2646 dropafterack_ratelim:
2647 /*
2648 * We may want to rate-limit ACKs against SYN/RST attack.
2649 */
2650 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
2651 tcp_ackdrop_ppslim) == 0) {
2652 /* XXX stat */
2653 goto drop;
2654 }
2655 /* ...fall into dropafterack2... */
2656
2657 dropafterack2:
2658 m_freem(m);
2659 tp->t_flags |= TF_ACKNOW;
2660 (void) tcp_output(tp);
2661 if (tcp_saveti)
2662 m_freem(tcp_saveti);
2663 return;
2664
2665 dropwithreset_ratelim:
2666 /*
2667 * We may want to rate-limit RSTs in certain situations,
2668 * particularly if we are sending an RST in response to
2669 * an attempt to connect to or otherwise communicate with
2670 * a port for which we have no socket.
2671 */
2672 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
2673 tcp_rst_ppslim) == 0) {
2674 /* XXX stat */
2675 goto drop;
2676 }
2677 /* ...fall into dropwithreset... */
2678
2679 dropwithreset:
2680 /*
2681 * Generate a RST, dropping incoming segment.
2682 * Make ACK acceptable to originator of segment.
2683 */
2684 if (tiflags & TH_RST)
2685 goto drop;
2686
2687 switch (af) {
2688 #ifdef INET6
2689 case AF_INET6:
2690 /* For following calls to tcp_respond */
2691 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
2692 goto drop;
2693 break;
2694 #endif /* INET6 */
2695 case AF_INET:
2696 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
2697 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2698 goto drop;
2699 }
2700
2701 if (tiflags & TH_ACK)
2702 (void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
2703 else {
2704 if (tiflags & TH_SYN)
2705 tlen++;
2706 (void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0,
2707 TH_RST|TH_ACK);
2708 }
2709 if (tcp_saveti)
2710 m_freem(tcp_saveti);
2711 return;
2712
2713 badcsum:
2714 drop:
2715 /*
2716 * Drop space held by incoming segment and return.
2717 */
2718 if (tp) {
2719 if (tp->t_inpcb)
2720 so = tp->t_inpcb->inp_socket;
2721 #ifdef INET6
2722 else if (tp->t_in6pcb)
2723 so = tp->t_in6pcb->in6p_socket;
2724 #endif
2725 else
2726 so = NULL;
2727 #ifdef TCP_DEBUG
2728 if (so && (so->so_options & SO_DEBUG) != 0)
2729 tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0);
2730 #endif
2731 }
2732 if (tcp_saveti)
2733 m_freem(tcp_saveti);
2734 m_freem(m);
2735 return;
2736 }
2737
2738 #ifdef TCP_SIGNATURE
2739 int
2740 tcp_signature_apply(void *fstate, caddr_t data, u_int len)
2741 {
2742
2743 MD5Update(fstate, (u_char *)data, len);
2744 return (0);
2745 }
2746
2747 struct secasvar *
2748 tcp_signature_getsav(struct mbuf *m, struct tcphdr *th)
2749 {
2750 struct secasvar *sav;
2751 #ifdef FAST_IPSEC
2752 union sockaddr_union dst;
2753 #endif
2754 struct ip *ip;
2755 struct ip6_hdr *ip6;
2756
2757 ip = mtod(m, struct ip *);
2758 switch (ip->ip_v) {
2759 case 4:
2760 ip = mtod(m, struct ip *);
2761 ip6 = NULL;
2762 break;
2763 case 6:
2764 ip = NULL;
2765 ip6 = mtod(m, struct ip6_hdr *);
2766 break;
2767 default:
2768 return (NULL);
2769 }
2770
2771 #ifdef FAST_IPSEC
2772 /* Extract the destination from the IP header in the mbuf. */
2773 bzero(&dst, sizeof(union sockaddr_union));
2774 dst.sa.sa_len = sizeof(struct sockaddr_in);
2775 dst.sa.sa_family = AF_INET;
2776 dst.sin.sin_addr = ip->ip_dst;
2777
2778 /*
2779 * Look up an SADB entry which matches the address of the peer.
2780 */
2781 sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI));
2782 #else
2783 if (ip)
2784 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
2785 (caddr_t)&ip->ip_dst, IPPROTO_TCP,
2786 htonl(TCP_SIG_SPI), 0, 0);
2787 else
2788 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
2789 (caddr_t)&ip6->ip6_dst, IPPROTO_TCP,
2790 htonl(TCP_SIG_SPI), 0, 0);
2791 #endif
2792
2793 return (sav); /* freesav must be performed by caller */
2794 }
2795
2796 int
2797 tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff,
2798 struct secasvar *sav, char *sig)
2799 {
2800 MD5_CTX ctx;
2801 struct ip *ip;
2802 struct ipovly *ipovly;
2803 struct ip6_hdr *ip6;
2804 struct ippseudo ippseudo;
2805 struct ip6_hdr_pseudo ip6pseudo;
2806 struct tcphdr th0;
2807 int l, tcphdrlen;
2808
2809 if (sav == NULL)
2810 return (-1);
2811
2812 tcphdrlen = th->th_off * 4;
2813
2814 switch (mtod(m, struct ip *)->ip_v) {
2815 case 4:
2816 ip = mtod(m, struct ip *);
2817 ip6 = NULL;
2818 break;
2819 case 6:
2820 ip = NULL;
2821 ip6 = mtod(m, struct ip6_hdr *);
2822 break;
2823 default:
2824 return (-1);
2825 }
2826
2827 MD5Init(&ctx);
2828
2829 if (ip) {
2830 memset(&ippseudo, 0, sizeof(ippseudo));
2831 ipovly = (struct ipovly *)ip;
2832 ippseudo.ippseudo_src = ipovly->ih_src;
2833 ippseudo.ippseudo_dst = ipovly->ih_dst;
2834 ippseudo.ippseudo_pad = 0;
2835 ippseudo.ippseudo_p = IPPROTO_TCP;
2836 ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff);
2837 MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo));
2838 } else {
2839 memset(&ip6pseudo, 0, sizeof(ip6pseudo));
2840 ip6pseudo.ip6ph_src = ip6->ip6_src;
2841 in6_clearscope(&ip6pseudo.ip6ph_src);
2842 ip6pseudo.ip6ph_dst = ip6->ip6_dst;
2843 in6_clearscope(&ip6pseudo.ip6ph_dst);
2844 ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff);
2845 ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
2846 MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo));
2847 }
2848
2849 th0 = *th;
2850 th0.th_sum = 0;
2851 MD5Update(&ctx, (char *)&th0, sizeof(th0));
2852
2853 l = m->m_pkthdr.len - thoff - tcphdrlen;
2854 if (l > 0)
2855 m_apply(m, thoff + tcphdrlen,
2856 m->m_pkthdr.len - thoff - tcphdrlen,
2857 tcp_signature_apply, &ctx);
2858
2859 MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2860 MD5Final(sig, &ctx);
2861
2862 return (0);
2863 }
2864 #endif
2865
2866 int
2867 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcphdr *th,
2868 struct mbuf *m, int toff, struct tcp_opt_info *oi)
2869 {
2870 u_int16_t mss;
2871 int opt, optlen = 0;
2872 #ifdef TCP_SIGNATURE
2873 caddr_t sigp = NULL;
2874 char sigbuf[TCP_SIGLEN];
2875 struct secasvar *sav = NULL;
2876 #endif
2877
2878 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) {
2879 opt = cp[0];
2880 if (opt == TCPOPT_EOL)
2881 break;
2882 if (opt == TCPOPT_NOP)
2883 optlen = 1;
2884 else {
2885 if (cnt < 2)
2886 break;
2887 optlen = cp[1];
2888 if (optlen < 2 || optlen > cnt)
2889 break;
2890 }
2891 switch (opt) {
2892
2893 default:
2894 continue;
2895
2896 case TCPOPT_MAXSEG:
2897 if (optlen != TCPOLEN_MAXSEG)
2898 continue;
2899 if (!(th->th_flags & TH_SYN))
2900 continue;
2901 bcopy(cp + 2, &mss, sizeof(mss));
2902 oi->maxseg = ntohs(mss);
2903 break;
2904
2905 case TCPOPT_WINDOW:
2906 if (optlen != TCPOLEN_WINDOW)
2907 continue;
2908 if (!(th->th_flags & TH_SYN))
2909 continue;
2910 tp->t_flags |= TF_RCVD_SCALE;
2911 tp->requested_s_scale = cp[2];
2912 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
2913 #if 0 /*XXX*/
2914 char *p;
2915
2916 if (ip)
2917 p = ntohl(ip->ip_src);
2918 #ifdef INET6
2919 else if (ip6)
2920 p = ip6_sprintf(&ip6->ip6_src);
2921 #endif
2922 else
2923 p = "(unknown)";
2924 log(LOG_ERR, "TCP: invalid wscale %d from %s, "
2925 "assuming %d\n",
2926 tp->requested_s_scale, p,
2927 TCP_MAX_WINSHIFT);
2928 #else
2929 log(LOG_ERR, "TCP: invalid wscale %d, "
2930 "assuming %d\n",
2931 tp->requested_s_scale,
2932 TCP_MAX_WINSHIFT);
2933 #endif
2934 tp->requested_s_scale = TCP_MAX_WINSHIFT;
2935 }
2936 break;
2937
2938 case TCPOPT_TIMESTAMP:
2939 if (optlen != TCPOLEN_TIMESTAMP)
2940 continue;
2941 oi->ts_present = 1;
2942 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
2943 NTOHL(oi->ts_val);
2944 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
2945 NTOHL(oi->ts_ecr);
2946
2947 /*
2948 * A timestamp received in a SYN makes
2949 * it ok to send timestamp requests and replies.
2950 */
2951 if (th->th_flags & TH_SYN) {
2952 tp->t_flags |= TF_RCVD_TSTMP;
2953 tp->ts_recent = oi->ts_val;
2954 tp->ts_recent_age = tcp_now;
2955 }
2956 break;
2957
2958 case TCPOPT_SACK_PERMITTED:
2959 if (optlen != TCPOLEN_SACK_PERMITTED)
2960 continue;
2961 if (!(th->th_flags & TH_SYN))
2962 continue;
2963 if (tcp_do_sack) {
2964 tp->t_flags |= TF_SACK_PERMIT;
2965 tp->t_flags |= TF_WILL_SACK;
2966 }
2967 break;
2968
2969 case TCPOPT_SACK:
2970 tcp_sack_option(tp, th, cp, optlen);
2971 break;
2972 #ifdef TCP_SIGNATURE
2973 case TCPOPT_SIGNATURE:
2974 if (optlen != TCPOLEN_SIGNATURE)
2975 continue;
2976 if (sigp && bcmp(sigp, cp + 2, TCP_SIGLEN))
2977 return (-1);
2978
2979 sigp = sigbuf;
2980 memcpy(sigbuf, cp + 2, TCP_SIGLEN);
2981 memset(cp + 2, 0, TCP_SIGLEN);
2982 tp->t_flags |= TF_SIGNATURE;
2983 break;
2984 #endif
2985 }
2986 }
2987
2988 #ifdef TCP_SIGNATURE
2989 if (tp->t_flags & TF_SIGNATURE) {
2990
2991 sav = tcp_signature_getsav(m, th);
2992
2993 if (sav == NULL && tp->t_state == TCPS_LISTEN)
2994 return (-1);
2995 }
2996
2997 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) {
2998 if (sav == NULL)
2999 return (-1);
3000 #ifdef FAST_IPSEC
3001 KEY_FREESAV(&sav);
3002 #else
3003 key_freesav(sav);
3004 #endif
3005 return (-1);
3006 }
3007
3008 if (sigp) {
3009 char sig[TCP_SIGLEN];
3010
3011 TCP_FIELDS_TO_NET(th);
3012 if (tcp_signature(m, th, toff, sav, sig) < 0) {
3013 TCP_FIELDS_TO_HOST(th);
3014 if (sav == NULL)
3015 return (-1);
3016 #ifdef FAST_IPSEC
3017 KEY_FREESAV(&sav);
3018 #else
3019 key_freesav(sav);
3020 #endif
3021 return (-1);
3022 }
3023 TCP_FIELDS_TO_HOST(th);
3024
3025 if (bcmp(sig, sigp, TCP_SIGLEN)) {
3026 tcpstat.tcps_badsig++;
3027 if (sav == NULL)
3028 return (-1);
3029 #ifdef FAST_IPSEC
3030 KEY_FREESAV(&sav);
3031 #else
3032 key_freesav(sav);
3033 #endif
3034 return (-1);
3035 } else
3036 tcpstat.tcps_goodsig++;
3037
3038 key_sa_recordxfer(sav, m);
3039 #ifdef FAST_IPSEC
3040 KEY_FREESAV(&sav);
3041 #else
3042 key_freesav(sav);
3043 #endif
3044 }
3045 #endif
3046
3047 return (0);
3048 }
3049
3050 /*
3051 * Pull out of band byte out of a segment so
3052 * it doesn't appear in the user's data queue.
3053 * It is still reflected in the segment length for
3054 * sequencing purposes.
3055 */
3056 void
3057 tcp_pulloutofband(struct socket *so, struct tcphdr *th,
3058 struct mbuf *m, int off)
3059 {
3060 int cnt = off + th->th_urp - 1;
3061
3062 while (cnt >= 0) {
3063 if (m->m_len > cnt) {
3064 char *cp = mtod(m, caddr_t) + cnt;
3065 struct tcpcb *tp = sototcpcb(so);
3066
3067 tp->t_iobc = *cp;
3068 tp->t_oobflags |= TCPOOB_HAVEDATA;
3069 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3070 m->m_len--;
3071 return;
3072 }
3073 cnt -= m->m_len;
3074 m = m->m_next;
3075 if (m == 0)
3076 break;
3077 }
3078 panic("tcp_pulloutofband");
3079 }
3080
3081 /*
3082 * Collect new round-trip time estimate
3083 * and update averages and current timeout.
3084 */
3085 void
3086 tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt)
3087 {
3088 int32_t delta;
3089
3090 tcpstat.tcps_rttupdated++;
3091 if (tp->t_srtt != 0) {
3092 /*
3093 * srtt is stored as fixed point with 3 bits after the
3094 * binary point (i.e., scaled by 8). The following magic
3095 * is equivalent to the smoothing algorithm in rfc793 with
3096 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3097 * point). Adjust rtt to origin 0.
3098 */
3099 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
3100 if ((tp->t_srtt += delta) <= 0)
3101 tp->t_srtt = 1 << 2;
3102 /*
3103 * We accumulate a smoothed rtt variance (actually, a
3104 * smoothed mean difference), then set the retransmit
3105 * timer to smoothed rtt + 4 times the smoothed variance.
3106 * rttvar is stored as fixed point with 2 bits after the
3107 * binary point (scaled by 4). The following is
3108 * equivalent to rfc793 smoothing with an alpha of .75
3109 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3110 * rfc793's wired-in beta.
3111 */
3112 if (delta < 0)
3113 delta = -delta;
3114 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
3115 if ((tp->t_rttvar += delta) <= 0)
3116 tp->t_rttvar = 1 << 2;
3117 } else {
3118 /*
3119 * No rtt measurement yet - use the unsmoothed rtt.
3120 * Set the variance to half the rtt (so our first
3121 * retransmit happens at 3*rtt).
3122 */
3123 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
3124 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
3125 }
3126 tp->t_rtttime = 0;
3127 tp->t_rxtshift = 0;
3128
3129 /*
3130 * the retransmit should happen at rtt + 4 * rttvar.
3131 * Because of the way we do the smoothing, srtt and rttvar
3132 * will each average +1/2 tick of bias. When we compute
3133 * the retransmit timer, we want 1/2 tick of rounding and
3134 * 1 extra tick because of +-1/2 tick uncertainty in the
3135 * firing of the timer. The bias will give us exactly the
3136 * 1.5 tick we need. But, because the bias is
3137 * statistical, we have to test that we don't drop below
3138 * the minimum feasible timer (which is 2 ticks).
3139 */
3140 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3141 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3142
3143 /*
3144 * We received an ack for a packet that wasn't retransmitted;
3145 * it is probably safe to discard any error indications we've
3146 * received recently. This isn't quite right, but close enough
3147 * for now (a route might have failed after we sent a segment,
3148 * and the return path might not be symmetrical).
3149 */
3150 tp->t_softerror = 0;
3151 }
3152
3153 void
3154 tcp_reno_newack(struct tcpcb *tp, struct tcphdr *th)
3155 {
3156 if (tp->t_partialacks < 0) {
3157 /*
3158 * We were not in fast recovery. Reset the duplicate ack
3159 * counter.
3160 */
3161 tp->t_dupacks = 0;
3162 } else {
3163 /*
3164 * Clamp the congestion window to the crossover point and
3165 * exit fast recovery.
3166 */
3167 if (tp->snd_cwnd > tp->snd_ssthresh)
3168 tp->snd_cwnd = tp->snd_ssthresh;
3169 tp->t_partialacks = -1;
3170 tp->t_dupacks = 0;
3171 }
3172 }
3173
3174 /*
3175 * Implement the NewReno response to a new ack, checking for partial acks in
3176 * fast recovery.
3177 */
3178 void
3179 tcp_newreno_newack(struct tcpcb *tp, struct tcphdr *th)
3180 {
3181 if (tp->t_partialacks < 0) {
3182 /*
3183 * We were not in fast recovery. Reset the duplicate ack
3184 * counter.
3185 */
3186 tp->t_dupacks = 0;
3187 } else if (SEQ_LT(th->th_ack, tp->snd_recover)) {
3188 /*
3189 * This is a partial ack. Retransmit the first unacknowledged
3190 * segment and deflate the congestion window by the amount of
3191 * acknowledged data. Do not exit fast recovery.
3192 */
3193 tcp_seq onxt = tp->snd_nxt;
3194 u_long ocwnd = tp->snd_cwnd;
3195
3196 /*
3197 * snd_una has not yet been updated and the socket's send
3198 * buffer has not yet drained off the ACK'd data, so we
3199 * have to leave snd_una as it was to get the correct data
3200 * offset in tcp_output().
3201 */
3202 if (++tp->t_partialacks == 1)
3203 TCP_TIMER_DISARM(tp, TCPT_REXMT);
3204 tp->t_rtttime = 0;
3205 tp->snd_nxt = th->th_ack;
3206 /*
3207 * Set snd_cwnd to one segment beyond ACK'd offset. snd_una
3208 * is not yet updated when we're called.
3209 */
3210 tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
3211 (void) tcp_output(tp);
3212 tp->snd_cwnd = ocwnd;
3213 if (SEQ_GT(onxt, tp->snd_nxt))
3214 tp->snd_nxt = onxt;
3215 /*
3216 * Partial window deflation. Relies on fact that tp->snd_una
3217 * not updated yet.
3218 */
3219 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz);
3220 } else {
3221 /*
3222 * Complete ack. Inflate the congestion window to ssthresh
3223 * and exit fast recovery.
3224 *
3225 * Window inflation should have left us with approx.
3226 * snd_ssthresh outstanding data. But in case we
3227 * would be inclined to send a burst, better to do
3228 * it via the slow start mechanism.
3229 */
3230 if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
3231 tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
3232 + tp->t_segsz;
3233 else
3234 tp->snd_cwnd = tp->snd_ssthresh;
3235 tp->t_partialacks = -1;
3236 tp->t_dupacks = 0;
3237 }
3238 }
3239
3240
3241 /*
3242 * TCP compressed state engine. Currently used to hold compressed
3243 * state for SYN_RECEIVED.
3244 */
3245
3246 u_long syn_cache_count;
3247 u_int32_t syn_hash1, syn_hash2;
3248
3249 #define SYN_HASH(sa, sp, dp) \
3250 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
3251 ((u_int32_t)(sp)))^syn_hash2)))
3252 #ifndef INET6
3253 #define SYN_HASHALL(hash, src, dst) \
3254 do { \
3255 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \
3256 ((const struct sockaddr_in *)(src))->sin_port, \
3257 ((const struct sockaddr_in *)(dst))->sin_port); \
3258 } while (/*CONSTCOND*/ 0)
3259 #else
3260 #define SYN_HASH6(sa, sp, dp) \
3261 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
3262 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
3263 & 0x7fffffff)
3264
3265 #define SYN_HASHALL(hash, src, dst) \
3266 do { \
3267 switch ((src)->sa_family) { \
3268 case AF_INET: \
3269 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \
3270 ((const struct sockaddr_in *)(src))->sin_port, \
3271 ((const struct sockaddr_in *)(dst))->sin_port); \
3272 break; \
3273 case AF_INET6: \
3274 hash = SYN_HASH6(&((const struct sockaddr_in6 *)(src))->sin6_addr, \
3275 ((const struct sockaddr_in6 *)(src))->sin6_port, \
3276 ((const struct sockaddr_in6 *)(dst))->sin6_port); \
3277 break; \
3278 default: \
3279 hash = 0; \
3280 } \
3281 } while (/*CONSTCOND*/0)
3282 #endif /* INET6 */
3283
3284 #define SYN_CACHE_RM(sc) \
3285 do { \
3286 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \
3287 (sc), sc_bucketq); \
3288 (sc)->sc_tp = NULL; \
3289 LIST_REMOVE((sc), sc_tpq); \
3290 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \
3291 callout_stop(&(sc)->sc_timer); \
3292 syn_cache_count--; \
3293 } while (/*CONSTCOND*/0)
3294
3295 #define SYN_CACHE_PUT(sc) \
3296 do { \
3297 if ((sc)->sc_ipopts) \
3298 (void) m_free((sc)->sc_ipopts); \
3299 if ((sc)->sc_route4.ro_rt != NULL) \
3300 RTFREE((sc)->sc_route4.ro_rt); \
3301 if (callout_invoking(&(sc)->sc_timer)) \
3302 (sc)->sc_flags |= SCF_DEAD; \
3303 else \
3304 pool_put(&syn_cache_pool, (sc)); \
3305 } while (/*CONSTCOND*/0)
3306
3307 POOL_INIT(syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0, "synpl", NULL);
3308
3309 /*
3310 * We don't estimate RTT with SYNs, so each packet starts with the default
3311 * RTT and each timer step has a fixed timeout value.
3312 */
3313 #define SYN_CACHE_TIMER_ARM(sc) \
3314 do { \
3315 TCPT_RANGESET((sc)->sc_rxtcur, \
3316 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \
3317 TCPTV_REXMTMAX); \
3318 callout_reset(&(sc)->sc_timer, \
3319 (sc)->sc_rxtcur * (hz / PR_SLOWHZ), syn_cache_timer, (sc)); \
3320 } while (/*CONSTCOND*/0)
3321
3322 #define SYN_CACHE_TIMESTAMP(sc) (tcp_now - (sc)->sc_timebase)
3323
3324 void
3325 syn_cache_init(void)
3326 {
3327 int i;
3328
3329 /* Initialize the hash buckets. */
3330 for (i = 0; i < tcp_syn_cache_size; i++)
3331 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
3332 }
3333
3334 void
3335 syn_cache_insert(struct syn_cache *sc, struct tcpcb *tp)
3336 {
3337 struct syn_cache_head *scp;
3338 struct syn_cache *sc2;
3339 int s;
3340
3341 /*
3342 * If there are no entries in the hash table, reinitialize
3343 * the hash secrets.
3344 */
3345 if (syn_cache_count == 0) {
3346 syn_hash1 = arc4random();
3347 syn_hash2 = arc4random();
3348 }
3349
3350 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
3351 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
3352 scp = &tcp_syn_cache[sc->sc_bucketidx];
3353
3354 /*
3355 * Make sure that we don't overflow the per-bucket
3356 * limit or the total cache size limit.
3357 */
3358 s = splsoftnet();
3359 if (scp->sch_length >= tcp_syn_bucket_limit) {
3360 tcpstat.tcps_sc_bucketoverflow++;
3361 /*
3362 * The bucket is full. Toss the oldest element in the
3363 * bucket. This will be the first entry in the bucket.
3364 */
3365 sc2 = TAILQ_FIRST(&scp->sch_bucket);
3366 #ifdef DIAGNOSTIC
3367 /*
3368 * This should never happen; we should always find an
3369 * entry in our bucket.
3370 */
3371 if (sc2 == NULL)
3372 panic("syn_cache_insert: bucketoverflow: impossible");
3373 #endif
3374 SYN_CACHE_RM(sc2);
3375 SYN_CACHE_PUT(sc2);
3376 } else if (syn_cache_count >= tcp_syn_cache_limit) {
3377 struct syn_cache_head *scp2, *sce;
3378
3379 tcpstat.tcps_sc_overflowed++;
3380 /*
3381 * The cache is full. Toss the oldest entry in the
3382 * first non-empty bucket we can find.
3383 *
3384 * XXX We would really like to toss the oldest
3385 * entry in the cache, but we hope that this
3386 * condition doesn't happen very often.
3387 */
3388 scp2 = scp;
3389 if (TAILQ_EMPTY(&scp2->sch_bucket)) {
3390 sce = &tcp_syn_cache[tcp_syn_cache_size];
3391 for (++scp2; scp2 != scp; scp2++) {
3392 if (scp2 >= sce)
3393 scp2 = &tcp_syn_cache[0];
3394 if (! TAILQ_EMPTY(&scp2->sch_bucket))
3395 break;
3396 }
3397 #ifdef DIAGNOSTIC
3398 /*
3399 * This should never happen; we should always find a
3400 * non-empty bucket.
3401 */
3402 if (scp2 == scp)
3403 panic("syn_cache_insert: cacheoverflow: "
3404 "impossible");
3405 #endif
3406 }
3407 sc2 = TAILQ_FIRST(&scp2->sch_bucket);
3408 SYN_CACHE_RM(sc2);
3409 SYN_CACHE_PUT(sc2);
3410 }
3411
3412 /*
3413 * Initialize the entry's timer.
3414 */
3415 sc->sc_rxttot = 0;
3416 sc->sc_rxtshift = 0;
3417 SYN_CACHE_TIMER_ARM(sc);
3418
3419 /* Link it from tcpcb entry */
3420 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
3421
3422 /* Put it into the bucket. */
3423 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
3424 scp->sch_length++;
3425 syn_cache_count++;
3426
3427 tcpstat.tcps_sc_added++;
3428 splx(s);
3429 }
3430
3431 /*
3432 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
3433 * If we have retransmitted an entry the maximum number of times, expire
3434 * that entry.
3435 */
3436 void
3437 syn_cache_timer(void *arg)
3438 {
3439 struct syn_cache *sc = arg;
3440 int s;
3441
3442 s = splsoftnet();
3443 callout_ack(&sc->sc_timer);
3444
3445 if (__predict_false(sc->sc_flags & SCF_DEAD)) {
3446 tcpstat.tcps_sc_delayed_free++;
3447 pool_put(&syn_cache_pool, sc);
3448 splx(s);
3449 return;
3450 }
3451
3452 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
3453 /* Drop it -- too many retransmissions. */
3454 goto dropit;
3455 }
3456
3457 /*
3458 * Compute the total amount of time this entry has
3459 * been on a queue. If this entry has been on longer
3460 * than the keep alive timer would allow, expire it.
3461 */
3462 sc->sc_rxttot += sc->sc_rxtcur;
3463 if (sc->sc_rxttot >= TCPTV_KEEP_INIT)
3464 goto dropit;
3465
3466 tcpstat.tcps_sc_retransmitted++;
3467 (void) syn_cache_respond(sc, NULL);
3468
3469 /* Advance the timer back-off. */
3470 sc->sc_rxtshift++;
3471 SYN_CACHE_TIMER_ARM(sc);
3472
3473 splx(s);
3474 return;
3475
3476 dropit:
3477 tcpstat.tcps_sc_timed_out++;
3478 SYN_CACHE_RM(sc);
3479 SYN_CACHE_PUT(sc);
3480 splx(s);
3481 }
3482
3483 /*
3484 * Remove syn cache created by the specified tcb entry,
3485 * because this does not make sense to keep them
3486 * (if there's no tcb entry, syn cache entry will never be used)
3487 */
3488 void
3489 syn_cache_cleanup(struct tcpcb *tp)
3490 {
3491 struct syn_cache *sc, *nsc;
3492 int s;
3493
3494 s = splsoftnet();
3495
3496 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
3497 nsc = LIST_NEXT(sc, sc_tpq);
3498
3499 #ifdef DIAGNOSTIC
3500 if (sc->sc_tp != tp)
3501 panic("invalid sc_tp in syn_cache_cleanup");
3502 #endif
3503 SYN_CACHE_RM(sc);
3504 SYN_CACHE_PUT(sc);
3505 }
3506 /* just for safety */
3507 LIST_INIT(&tp->t_sc);
3508
3509 splx(s);
3510 }
3511
3512 /*
3513 * Find an entry in the syn cache.
3514 */
3515 struct syn_cache *
3516 syn_cache_lookup(const struct sockaddr *src, const struct sockaddr *dst,
3517 struct syn_cache_head **headp)
3518 {
3519 struct syn_cache *sc;
3520 struct syn_cache_head *scp;
3521 u_int32_t hash;
3522 int s;
3523
3524 SYN_HASHALL(hash, src, dst);
3525
3526 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
3527 *headp = scp;
3528 s = splsoftnet();
3529 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
3530 sc = TAILQ_NEXT(sc, sc_bucketq)) {
3531 if (sc->sc_hash != hash)
3532 continue;
3533 if (!bcmp(&sc->sc_src, src, src->sa_len) &&
3534 !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
3535 splx(s);
3536 return (sc);
3537 }
3538 }
3539 splx(s);
3540 return (NULL);
3541 }
3542
3543 /*
3544 * This function gets called when we receive an ACK for a
3545 * socket in the LISTEN state. We look up the connection
3546 * in the syn cache, and if its there, we pull it out of
3547 * the cache and turn it into a full-blown connection in
3548 * the SYN-RECEIVED state.
3549 *
3550 * The return values may not be immediately obvious, and their effects
3551 * can be subtle, so here they are:
3552 *
3553 * NULL SYN was not found in cache; caller should drop the
3554 * packet and send an RST.
3555 *
3556 * -1 We were unable to create the new connection, and are
3557 * aborting it. An ACK,RST is being sent to the peer
3558 * (unless we got screwey sequence numbners; see below),
3559 * because the 3-way handshake has been completed. Caller
3560 * should not free the mbuf, since we may be using it. If
3561 * we are not, we will free it.
3562 *
3563 * Otherwise, the return value is a pointer to the new socket
3564 * associated with the connection.
3565 */
3566 struct socket *
3567 syn_cache_get(struct sockaddr *src, struct sockaddr *dst,
3568 struct tcphdr *th, unsigned int hlen, unsigned int tlen,
3569 struct socket *so, struct mbuf *m)
3570 {
3571 struct syn_cache *sc;
3572 struct syn_cache_head *scp;
3573 struct inpcb *inp = NULL;
3574 #ifdef INET6
3575 struct in6pcb *in6p = NULL;
3576 #endif
3577 struct tcpcb *tp = 0;
3578 struct mbuf *am;
3579 int s;
3580 struct socket *oso;
3581
3582 s = splsoftnet();
3583 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3584 splx(s);
3585 return (NULL);
3586 }
3587
3588 /*
3589 * Verify the sequence and ack numbers. Try getting the correct
3590 * response again.
3591 */
3592 if ((th->th_ack != sc->sc_iss + 1) ||
3593 SEQ_LEQ(th->th_seq, sc->sc_irs) ||
3594 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
3595 (void) syn_cache_respond(sc, m);
3596 splx(s);
3597 return ((struct socket *)(-1));
3598 }
3599
3600 /* Remove this cache entry */
3601 SYN_CACHE_RM(sc);
3602 splx(s);
3603
3604 /*
3605 * Ok, create the full blown connection, and set things up
3606 * as they would have been set up if we had created the
3607 * connection when the SYN arrived. If we can't create
3608 * the connection, abort it.
3609 */
3610 /*
3611 * inp still has the OLD in_pcb stuff, set the
3612 * v6-related flags on the new guy, too. This is
3613 * done particularly for the case where an AF_INET6
3614 * socket is bound only to a port, and a v4 connection
3615 * comes in on that port.
3616 * we also copy the flowinfo from the original pcb
3617 * to the new one.
3618 */
3619 oso = so;
3620 so = sonewconn(so, SS_ISCONNECTED);
3621 if (so == NULL)
3622 goto resetandabort;
3623
3624 switch (so->so_proto->pr_domain->dom_family) {
3625 #ifdef INET
3626 case AF_INET:
3627 inp = sotoinpcb(so);
3628 break;
3629 #endif
3630 #ifdef INET6
3631 case AF_INET6:
3632 in6p = sotoin6pcb(so);
3633 break;
3634 #endif
3635 }
3636 switch (src->sa_family) {
3637 #ifdef INET
3638 case AF_INET:
3639 if (inp) {
3640 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
3641 inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port;
3642 inp->inp_options = ip_srcroute();
3643 in_pcbstate(inp, INP_BOUND);
3644 if (inp->inp_options == NULL) {
3645 inp->inp_options = sc->sc_ipopts;
3646 sc->sc_ipopts = NULL;
3647 }
3648 }
3649 #ifdef INET6
3650 else if (in6p) {
3651 /* IPv4 packet to AF_INET6 socket */
3652 bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
3653 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
3654 bcopy(&((struct sockaddr_in *)dst)->sin_addr,
3655 &in6p->in6p_laddr.s6_addr32[3],
3656 sizeof(((struct sockaddr_in *)dst)->sin_addr));
3657 in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port;
3658 in6totcpcb(in6p)->t_family = AF_INET;
3659 if (sotoin6pcb(oso)->in6p_flags & IN6P_IPV6_V6ONLY)
3660 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
3661 else
3662 in6p->in6p_flags &= ~IN6P_IPV6_V6ONLY;
3663 in6_pcbstate(in6p, IN6P_BOUND);
3664 }
3665 #endif
3666 break;
3667 #endif
3668 #ifdef INET6
3669 case AF_INET6:
3670 if (in6p) {
3671 in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr;
3672 in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port;
3673 in6_pcbstate(in6p, IN6P_BOUND);
3674 }
3675 break;
3676 #endif
3677 }
3678 #ifdef INET6
3679 if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) {
3680 struct in6pcb *oin6p = sotoin6pcb(oso);
3681 /* inherit socket options from the listening socket */
3682 in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS);
3683 if (in6p->in6p_flags & IN6P_CONTROLOPTS) {
3684 m_freem(in6p->in6p_options);
3685 in6p->in6p_options = 0;
3686 }
3687 ip6_savecontrol(in6p, &in6p->in6p_options,
3688 mtod(m, struct ip6_hdr *), m);
3689 }
3690 #endif
3691
3692 #if defined(IPSEC) || defined(FAST_IPSEC)
3693 /*
3694 * we make a copy of policy, instead of sharing the policy,
3695 * for better behavior in terms of SA lookup and dead SA removal.
3696 */
3697 if (inp) {
3698 /* copy old policy into new socket's */
3699 if (ipsec_copy_pcbpolicy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
3700 printf("tcp_input: could not copy policy\n");
3701 }
3702 #ifdef INET6
3703 else if (in6p) {
3704 /* copy old policy into new socket's */
3705 if (ipsec_copy_pcbpolicy(sotoin6pcb(oso)->in6p_sp,
3706 in6p->in6p_sp))
3707 printf("tcp_input: could not copy policy\n");
3708 }
3709 #endif
3710 #endif
3711
3712 /*
3713 * Give the new socket our cached route reference.
3714 */
3715 if (inp)
3716 inp->inp_route = sc->sc_route4; /* struct assignment */
3717 #ifdef INET6
3718 else
3719 in6p->in6p_route = sc->sc_route6;
3720 #endif
3721 sc->sc_route4.ro_rt = NULL;
3722
3723 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
3724 if (am == NULL)
3725 goto resetandabort;
3726 MCLAIM(am, &tcp_mowner);
3727 am->m_len = src->sa_len;
3728 bcopy(src, mtod(am, caddr_t), src->sa_len);
3729 if (inp) {
3730 if (in_pcbconnect(inp, am)) {
3731 (void) m_free(am);
3732 goto resetandabort;
3733 }
3734 }
3735 #ifdef INET6
3736 else if (in6p) {
3737 if (src->sa_family == AF_INET) {
3738 /* IPv4 packet to AF_INET6 socket */
3739 struct sockaddr_in6 *sin6;
3740 sin6 = mtod(am, struct sockaddr_in6 *);
3741 am->m_len = sizeof(*sin6);
3742 bzero(sin6, sizeof(*sin6));
3743 sin6->sin6_family = AF_INET6;
3744 sin6->sin6_len = sizeof(*sin6);
3745 sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port;
3746 sin6->sin6_addr.s6_addr16[5] = htons(0xffff);
3747 bcopy(&((struct sockaddr_in *)src)->sin_addr,
3748 &sin6->sin6_addr.s6_addr32[3],
3749 sizeof(sin6->sin6_addr.s6_addr32[3]));
3750 }
3751 if (in6_pcbconnect(in6p, am)) {
3752 (void) m_free(am);
3753 goto resetandabort;
3754 }
3755 }
3756 #endif
3757 else {
3758 (void) m_free(am);
3759 goto resetandabort;
3760 }
3761 (void) m_free(am);
3762
3763 if (inp)
3764 tp = intotcpcb(inp);
3765 #ifdef INET6
3766 else if (in6p)
3767 tp = in6totcpcb(in6p);
3768 #endif
3769 else
3770 tp = NULL;
3771 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
3772 if (sc->sc_request_r_scale != 15) {
3773 tp->requested_s_scale = sc->sc_requested_s_scale;
3774 tp->request_r_scale = sc->sc_request_r_scale;
3775 tp->snd_scale = sc->sc_requested_s_scale;
3776 tp->rcv_scale = sc->sc_request_r_scale;
3777 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
3778 }
3779 if (sc->sc_flags & SCF_TIMESTAMP)
3780 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
3781 tp->ts_timebase = sc->sc_timebase;
3782
3783 tp->t_template = tcp_template(tp);
3784 if (tp->t_template == 0) {
3785 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
3786 so = NULL;
3787 m_freem(m);
3788 goto abort;
3789 }
3790
3791 tp->iss = sc->sc_iss;
3792 tp->irs = sc->sc_irs;
3793 tcp_sendseqinit(tp);
3794 tcp_rcvseqinit(tp);
3795 tp->t_state = TCPS_SYN_RECEIVED;
3796 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
3797 tcpstat.tcps_accepts++;
3798
3799 if ((sc->sc_flags & SCF_SACK_PERMIT) && tcp_do_sack)
3800 tp->t_flags |= TF_WILL_SACK;
3801
3802 #ifdef TCP_SIGNATURE
3803 if (sc->sc_flags & SCF_SIGNATURE)
3804 tp->t_flags |= TF_SIGNATURE;
3805 #endif
3806
3807 /* Initialize tp->t_ourmss before we deal with the peer's! */
3808 tp->t_ourmss = sc->sc_ourmaxseg;
3809 tcp_mss_from_peer(tp, sc->sc_peermaxseg);
3810
3811 /*
3812 * Initialize the initial congestion window. If we
3813 * had to retransmit the SYN,ACK, we must initialize cwnd
3814 * to 1 segment (i.e. the Loss Window).
3815 */
3816 if (sc->sc_rxtshift)
3817 tp->snd_cwnd = tp->t_peermss;
3818 else {
3819 int ss = tcp_init_win;
3820 #ifdef INET
3821 if (inp != NULL && in_localaddr(inp->inp_faddr))
3822 ss = tcp_init_win_local;
3823 #endif
3824 #ifdef INET6
3825 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
3826 ss = tcp_init_win_local;
3827 #endif
3828 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
3829 }
3830
3831 tcp_rmx_rtt(tp);
3832 tp->snd_wl1 = sc->sc_irs;
3833 tp->rcv_up = sc->sc_irs + 1;
3834
3835 /*
3836 * This is what whould have happened in tcp_output() when
3837 * the SYN,ACK was sent.
3838 */
3839 tp->snd_up = tp->snd_una;
3840 tp->snd_max = tp->snd_nxt = tp->iss+1;
3841 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
3842 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
3843 tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
3844 tp->last_ack_sent = tp->rcv_nxt;
3845 tp->t_partialacks = -1;
3846 tp->t_dupacks = 0;
3847
3848 tcpstat.tcps_sc_completed++;
3849 SYN_CACHE_PUT(sc);
3850 return (so);
3851
3852 resetandabort:
3853 (void)tcp_respond(NULL, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
3854 abort:
3855 if (so != NULL)
3856 (void) soabort(so);
3857 SYN_CACHE_PUT(sc);
3858 tcpstat.tcps_sc_aborted++;
3859 return ((struct socket *)(-1));
3860 }
3861
3862 /*
3863 * This function is called when we get a RST for a
3864 * non-existent connection, so that we can see if the
3865 * connection is in the syn cache. If it is, zap it.
3866 */
3867
3868 void
3869 syn_cache_reset(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th)
3870 {
3871 struct syn_cache *sc;
3872 struct syn_cache_head *scp;
3873 int s = splsoftnet();
3874
3875 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3876 splx(s);
3877 return;
3878 }
3879 if (SEQ_LT(th->th_seq, sc->sc_irs) ||
3880 SEQ_GT(th->th_seq, sc->sc_irs+1)) {
3881 splx(s);
3882 return;
3883 }
3884 SYN_CACHE_RM(sc);
3885 splx(s);
3886 tcpstat.tcps_sc_reset++;
3887 SYN_CACHE_PUT(sc);
3888 }
3889
3890 void
3891 syn_cache_unreach(const struct sockaddr *src, const struct sockaddr *dst,
3892 struct tcphdr *th)
3893 {
3894 struct syn_cache *sc;
3895 struct syn_cache_head *scp;
3896 int s;
3897
3898 s = splsoftnet();
3899 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3900 splx(s);
3901 return;
3902 }
3903 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
3904 if (ntohl (th->th_seq) != sc->sc_iss) {
3905 splx(s);
3906 return;
3907 }
3908
3909 /*
3910 * If we've retransmitted 3 times and this is our second error,
3911 * we remove the entry. Otherwise, we allow it to continue on.
3912 * This prevents us from incorrectly nuking an entry during a
3913 * spurious network outage.
3914 *
3915 * See tcp_notify().
3916 */
3917 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
3918 sc->sc_flags |= SCF_UNREACH;
3919 splx(s);
3920 return;
3921 }
3922
3923 SYN_CACHE_RM(sc);
3924 splx(s);
3925 tcpstat.tcps_sc_unreach++;
3926 SYN_CACHE_PUT(sc);
3927 }
3928
3929 /*
3930 * Given a LISTEN socket and an inbound SYN request, add
3931 * this to the syn cache, and send back a segment:
3932 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3933 * to the source.
3934 *
3935 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
3936 * Doing so would require that we hold onto the data and deliver it
3937 * to the application. However, if we are the target of a SYN-flood
3938 * DoS attack, an attacker could send data which would eventually
3939 * consume all available buffer space if it were ACKed. By not ACKing
3940 * the data, we avoid this DoS scenario.
3941 */
3942
3943 int
3944 syn_cache_add(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th,
3945 unsigned int hlen, struct socket *so, struct mbuf *m, u_char *optp,
3946 int optlen, struct tcp_opt_info *oi)
3947 {
3948 struct tcpcb tb, *tp;
3949 long win;
3950 struct syn_cache *sc;
3951 struct syn_cache_head *scp;
3952 struct mbuf *ipopts;
3953 struct tcp_opt_info opti;
3954
3955 tp = sototcpcb(so);
3956
3957 bzero(&opti, sizeof(opti));
3958
3959 /*
3960 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
3961 *
3962 * Note this check is performed in tcp_input() very early on.
3963 */
3964
3965 /*
3966 * Initialize some local state.
3967 */
3968 win = sbspace(&so->so_rcv);
3969 if (win > TCP_MAXWIN)
3970 win = TCP_MAXWIN;
3971
3972 switch (src->sa_family) {
3973 #ifdef INET
3974 case AF_INET:
3975 /*
3976 * Remember the IP options, if any.
3977 */
3978 ipopts = ip_srcroute();
3979 break;
3980 #endif
3981 default:
3982 ipopts = NULL;
3983 }
3984
3985 #ifdef TCP_SIGNATURE
3986 if (optp || (tp->t_flags & TF_SIGNATURE))
3987 #else
3988 if (optp)
3989 #endif
3990 {
3991 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
3992 #ifdef TCP_SIGNATURE
3993 tb.t_flags |= (tp->t_flags & TF_SIGNATURE);
3994 #endif
3995 if (tcp_dooptions(&tb, optp, optlen, th, m, m->m_pkthdr.len -
3996 sizeof(struct tcphdr) - optlen - hlen, oi) < 0)
3997 return (0);
3998 } else
3999 tb.t_flags = 0;
4000
4001 /*
4002 * See if we already have an entry for this connection.
4003 * If we do, resend the SYN,ACK. We do not count this
4004 * as a retransmission (XXX though maybe we should).
4005 */
4006 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
4007 tcpstat.tcps_sc_dupesyn++;
4008 if (ipopts) {
4009 /*
4010 * If we were remembering a previous source route,
4011 * forget it and use the new one we've been given.
4012 */
4013 if (sc->sc_ipopts)
4014 (void) m_free(sc->sc_ipopts);
4015 sc->sc_ipopts = ipopts;
4016 }
4017 sc->sc_timestamp = tb.ts_recent;
4018 if (syn_cache_respond(sc, m) == 0) {
4019 tcpstat.tcps_sndacks++;
4020 tcpstat.tcps_sndtotal++;
4021 }
4022 return (1);
4023 }
4024
4025 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
4026 if (sc == NULL) {
4027 if (ipopts)
4028 (void) m_free(ipopts);
4029 return (0);
4030 }
4031
4032 /*
4033 * Fill in the cache, and put the necessary IP and TCP
4034 * options into the reply.
4035 */
4036 bzero(sc, sizeof(struct syn_cache));
4037 callout_init(&sc->sc_timer);
4038 bcopy(src, &sc->sc_src, src->sa_len);
4039 bcopy(dst, &sc->sc_dst, dst->sa_len);
4040 sc->sc_flags = 0;
4041 sc->sc_ipopts = ipopts;
4042 sc->sc_irs = th->th_seq;
4043 switch (src->sa_family) {
4044 #ifdef INET
4045 case AF_INET:
4046 {
4047 struct sockaddr_in *srcin = (void *) src;
4048 struct sockaddr_in *dstin = (void *) dst;
4049
4050 sc->sc_iss = tcp_new_iss1(&dstin->sin_addr,
4051 &srcin->sin_addr, dstin->sin_port,
4052 srcin->sin_port, sizeof(dstin->sin_addr), 0);
4053 break;
4054 }
4055 #endif /* INET */
4056 #ifdef INET6
4057 case AF_INET6:
4058 {
4059 struct sockaddr_in6 *srcin6 = (void *) src;
4060 struct sockaddr_in6 *dstin6 = (void *) dst;
4061
4062 sc->sc_iss = tcp_new_iss1(&dstin6->sin6_addr,
4063 &srcin6->sin6_addr, dstin6->sin6_port,
4064 srcin6->sin6_port, sizeof(dstin6->sin6_addr), 0);
4065 break;
4066 }
4067 #endif /* INET6 */
4068 }
4069 sc->sc_peermaxseg = oi->maxseg;
4070 sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
4071 m->m_pkthdr.rcvif : NULL,
4072 sc->sc_src.sa.sa_family);
4073 sc->sc_win = win;
4074 sc->sc_timebase = tcp_now; /* see tcp_newtcpcb() */
4075 sc->sc_timestamp = tb.ts_recent;
4076 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) ==
4077 (TF_REQ_TSTMP|TF_RCVD_TSTMP))
4078 sc->sc_flags |= SCF_TIMESTAMP;
4079 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
4080 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
4081 sc->sc_requested_s_scale = tb.requested_s_scale;
4082 sc->sc_request_r_scale = 0;
4083 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
4084 TCP_MAXWIN << sc->sc_request_r_scale <
4085 so->so_rcv.sb_hiwat)
4086 sc->sc_request_r_scale++;
4087 } else {
4088 sc->sc_requested_s_scale = 15;
4089 sc->sc_request_r_scale = 15;
4090 }
4091 if ((tb.t_flags & TF_SACK_PERMIT) && tcp_do_sack)
4092 sc->sc_flags |= SCF_SACK_PERMIT;
4093 #ifdef TCP_SIGNATURE
4094 if (tb.t_flags & TF_SIGNATURE)
4095 sc->sc_flags |= SCF_SIGNATURE;
4096 #endif
4097 sc->sc_tp = tp;
4098 if (syn_cache_respond(sc, m) == 0) {
4099 syn_cache_insert(sc, tp);
4100 tcpstat.tcps_sndacks++;
4101 tcpstat.tcps_sndtotal++;
4102 } else {
4103 SYN_CACHE_PUT(sc);
4104 tcpstat.tcps_sc_dropped++;
4105 }
4106 return (1);
4107 }
4108
4109 int
4110 syn_cache_respond(struct syn_cache *sc, struct mbuf *m)
4111 {
4112 struct route *ro;
4113 u_int8_t *optp;
4114 int optlen, error;
4115 u_int16_t tlen;
4116 struct ip *ip = NULL;
4117 #ifdef INET6
4118 struct ip6_hdr *ip6 = NULL;
4119 #endif
4120 struct tcpcb *tp;
4121 struct tcphdr *th;
4122 u_int hlen;
4123 struct socket *so;
4124
4125 switch (sc->sc_src.sa.sa_family) {
4126 case AF_INET:
4127 hlen = sizeof(struct ip);
4128 ro = &sc->sc_route4;
4129 break;
4130 #ifdef INET6
4131 case AF_INET6:
4132 hlen = sizeof(struct ip6_hdr);
4133 ro = (struct route *)&sc->sc_route6;
4134 break;
4135 #endif
4136 default:
4137 if (m)
4138 m_freem(m);
4139 return (EAFNOSUPPORT);
4140 }
4141
4142 /* Compute the size of the TCP options. */
4143 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
4144 ((sc->sc_flags & SCF_SACK_PERMIT) ? (TCPOLEN_SACK_PERMITTED + 2) : 0) +
4145 #ifdef TCP_SIGNATURE
4146 ((sc->sc_flags & SCF_SIGNATURE) ? (TCPOLEN_SIGNATURE + 2) : 0) +
4147 #endif
4148 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
4149
4150 tlen = hlen + sizeof(struct tcphdr) + optlen;
4151
4152 /*
4153 * Create the IP+TCP header from scratch.
4154 */
4155 if (m)
4156 m_freem(m);
4157 #ifdef DIAGNOSTIC
4158 if (max_linkhdr + tlen > MCLBYTES)
4159 return (ENOBUFS);
4160 #endif
4161 MGETHDR(m, M_DONTWAIT, MT_DATA);
4162 if (m && tlen > MHLEN) {
4163 MCLGET(m, M_DONTWAIT);
4164 if ((m->m_flags & M_EXT) == 0) {
4165 m_freem(m);
4166 m = NULL;
4167 }
4168 }
4169 if (m == NULL)
4170 return (ENOBUFS);
4171 MCLAIM(m, &tcp_tx_mowner);
4172
4173 /* Fixup the mbuf. */
4174 m->m_data += max_linkhdr;
4175 m->m_len = m->m_pkthdr.len = tlen;
4176 if (sc->sc_tp) {
4177 tp = sc->sc_tp;
4178 if (tp->t_inpcb)
4179 so = tp->t_inpcb->inp_socket;
4180 #ifdef INET6
4181 else if (tp->t_in6pcb)
4182 so = tp->t_in6pcb->in6p_socket;
4183 #endif
4184 else
4185 so = NULL;
4186 } else
4187 so = NULL;
4188 m->m_pkthdr.rcvif = NULL;
4189 memset(mtod(m, u_char *), 0, tlen);
4190
4191 switch (sc->sc_src.sa.sa_family) {
4192 case AF_INET:
4193 ip = mtod(m, struct ip *);
4194 ip->ip_v = 4;
4195 ip->ip_dst = sc->sc_src.sin.sin_addr;
4196 ip->ip_src = sc->sc_dst.sin.sin_addr;
4197 ip->ip_p = IPPROTO_TCP;
4198 th = (struct tcphdr *)(ip + 1);
4199 th->th_dport = sc->sc_src.sin.sin_port;
4200 th->th_sport = sc->sc_dst.sin.sin_port;
4201 break;
4202 #ifdef INET6
4203 case AF_INET6:
4204 ip6 = mtod(m, struct ip6_hdr *);
4205 ip6->ip6_vfc = IPV6_VERSION;
4206 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
4207 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
4208 ip6->ip6_nxt = IPPROTO_TCP;
4209 /* ip6_plen will be updated in ip6_output() */
4210 th = (struct tcphdr *)(ip6 + 1);
4211 th->th_dport = sc->sc_src.sin6.sin6_port;
4212 th->th_sport = sc->sc_dst.sin6.sin6_port;
4213 break;
4214 #endif
4215 default:
4216 th = NULL;
4217 }
4218
4219 th->th_seq = htonl(sc->sc_iss);
4220 th->th_ack = htonl(sc->sc_irs + 1);
4221 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
4222 th->th_flags = TH_SYN|TH_ACK;
4223 th->th_win = htons(sc->sc_win);
4224 /* th_sum already 0 */
4225 /* th_urp already 0 */
4226
4227 /* Tack on the TCP options. */
4228 optp = (u_int8_t *)(th + 1);
4229 *optp++ = TCPOPT_MAXSEG;
4230 *optp++ = 4;
4231 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
4232 *optp++ = sc->sc_ourmaxseg & 0xff;
4233
4234 if (sc->sc_request_r_scale != 15) {
4235 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
4236 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
4237 sc->sc_request_r_scale);
4238 optp += 4;
4239 }
4240
4241 if (sc->sc_flags & SCF_TIMESTAMP) {
4242 u_int32_t *lp = (u_int32_t *)(optp);
4243 /* Form timestamp option as shown in appendix A of RFC 1323. */
4244 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
4245 *lp++ = htonl(SYN_CACHE_TIMESTAMP(sc));
4246 *lp = htonl(sc->sc_timestamp);
4247 optp += TCPOLEN_TSTAMP_APPA;
4248 }
4249
4250 if (sc->sc_flags & SCF_SACK_PERMIT) {
4251 u_int8_t *p = optp;
4252
4253 /* Let the peer know that we will SACK. */
4254 p[0] = TCPOPT_SACK_PERMITTED;
4255 p[1] = 2;
4256 p[2] = TCPOPT_NOP;
4257 p[3] = TCPOPT_NOP;
4258 optp += 4;
4259 }
4260
4261 #ifdef TCP_SIGNATURE
4262 if (sc->sc_flags & SCF_SIGNATURE) {
4263 struct secasvar *sav;
4264 u_int8_t *sigp;
4265
4266 sav = tcp_signature_getsav(m, th);
4267
4268 if (sav == NULL) {
4269 if (m)
4270 m_freem(m);
4271 return (EPERM);
4272 }
4273
4274 *optp++ = TCPOPT_SIGNATURE;
4275 *optp++ = TCPOLEN_SIGNATURE;
4276 sigp = optp;
4277 bzero(optp, TCP_SIGLEN);
4278 optp += TCP_SIGLEN;
4279 *optp++ = TCPOPT_NOP;
4280 *optp++ = TCPOPT_EOL;
4281
4282 (void)tcp_signature(m, th, hlen, sav, sigp);
4283
4284 key_sa_recordxfer(sav, m);
4285 #ifdef FAST_IPSEC
4286 KEY_FREESAV(&sav);
4287 #else
4288 key_freesav(sav);
4289 #endif
4290 }
4291 #endif
4292
4293 /* Compute the packet's checksum. */
4294 switch (sc->sc_src.sa.sa_family) {
4295 case AF_INET:
4296 ip->ip_len = htons(tlen - hlen);
4297 th->th_sum = 0;
4298 th->th_sum = in4_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4299 break;
4300 #ifdef INET6
4301 case AF_INET6:
4302 ip6->ip6_plen = htons(tlen - hlen);
4303 th->th_sum = 0;
4304 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4305 break;
4306 #endif
4307 }
4308
4309 /*
4310 * Fill in some straggling IP bits. Note the stack expects
4311 * ip_len to be in host order, for convenience.
4312 */
4313 switch (sc->sc_src.sa.sa_family) {
4314 #ifdef INET
4315 case AF_INET:
4316 ip->ip_len = htons(tlen);
4317 ip->ip_ttl = ip_defttl;
4318 /* XXX tos? */
4319 break;
4320 #endif
4321 #ifdef INET6
4322 case AF_INET6:
4323 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
4324 ip6->ip6_vfc |= IPV6_VERSION;
4325 ip6->ip6_plen = htons(tlen - hlen);
4326 /* ip6_hlim will be initialized afterwards */
4327 /* XXX flowlabel? */
4328 break;
4329 #endif
4330 }
4331
4332 /* XXX use IPsec policy on listening socket, on SYN ACK */
4333 tp = sc->sc_tp;
4334
4335 switch (sc->sc_src.sa.sa_family) {
4336 #ifdef INET
4337 case AF_INET:
4338 error = ip_output(m, sc->sc_ipopts, ro,
4339 (ip_mtudisc ? IP_MTUDISC : 0),
4340 (struct ip_moptions *)NULL, so);
4341 break;
4342 #endif
4343 #ifdef INET6
4344 case AF_INET6:
4345 ip6->ip6_hlim = in6_selecthlim(NULL,
4346 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
4347
4348 error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro, 0,
4349 (struct ip6_moptions *)0, so, NULL);
4350 break;
4351 #endif
4352 default:
4353 error = EAFNOSUPPORT;
4354 break;
4355 }
4356 return (error);
4357 }
4358