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