tcp_input.c revision 1.268 1 /* $NetBSD: tcp_input.c,v 1.268 2007/07/09 21:11:11 ad 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.268 2007/07/09 21:11:11 ad 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 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, IPL_VM);
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 (char *)&ip6->ip6_dst - (char *)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 ipsec6_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, void *));
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 memcpy(mtod(tcp_saveti, char *) + iphlen, th,
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 #if defined(IPSEC) || defined(FAST_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 /*INET6*/
1549 }
1550 #endif /*IPSEC*/
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, tp->t_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 (tp->t_maxidle > 0)
2370 TCP_TIMER_ARM(tp, TCPT_2MSL,
2371 tp->t_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, void *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 if (ip !=NULL) {
2765 dst.sa.sa_len = sizeof(struct sockaddr_in);
2766 dst.sa.sa_family = AF_INET;
2767 dst.sin.sin_addr = ip->ip_dst;
2768 } else {
2769 dst.sa.sa_len = sizeof(struct sockaddr_in6);
2770 dst.sa.sa_family = AF_INET6;
2771 dst.sin6.sin6_addr = ip6->ip6_dst;
2772 }
2773
2774 /*
2775 * Look up an SADB entry which matches the address of the peer.
2776 */
2777 sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI));
2778 #else
2779 if (ip)
2780 sav = key_allocsa(AF_INET, (void *)&ip->ip_src,
2781 (void *)&ip->ip_dst, IPPROTO_TCP,
2782 htonl(TCP_SIG_SPI), 0, 0);
2783 else
2784 sav = key_allocsa(AF_INET6, (void *)&ip6->ip6_src,
2785 (void *)&ip6->ip6_dst, IPPROTO_TCP,
2786 htonl(TCP_SIG_SPI), 0, 0);
2787 #endif
2788
2789 return (sav); /* freesav must be performed by caller */
2790 }
2791
2792 int
2793 tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff,
2794 struct secasvar *sav, char *sig)
2795 {
2796 MD5_CTX ctx;
2797 struct ip *ip;
2798 struct ipovly *ipovly;
2799 struct ip6_hdr *ip6;
2800 struct ippseudo ippseudo;
2801 struct ip6_hdr_pseudo ip6pseudo;
2802 struct tcphdr th0;
2803 int l, tcphdrlen;
2804
2805 if (sav == NULL)
2806 return (-1);
2807
2808 tcphdrlen = th->th_off * 4;
2809
2810 switch (mtod(m, struct ip *)->ip_v) {
2811 case 4:
2812 ip = mtod(m, struct ip *);
2813 ip6 = NULL;
2814 break;
2815 case 6:
2816 ip = NULL;
2817 ip6 = mtod(m, struct ip6_hdr *);
2818 break;
2819 default:
2820 return (-1);
2821 }
2822
2823 MD5Init(&ctx);
2824
2825 if (ip) {
2826 memset(&ippseudo, 0, sizeof(ippseudo));
2827 ipovly = (struct ipovly *)ip;
2828 ippseudo.ippseudo_src = ipovly->ih_src;
2829 ippseudo.ippseudo_dst = ipovly->ih_dst;
2830 ippseudo.ippseudo_pad = 0;
2831 ippseudo.ippseudo_p = IPPROTO_TCP;
2832 ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff);
2833 MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo));
2834 } else {
2835 memset(&ip6pseudo, 0, sizeof(ip6pseudo));
2836 ip6pseudo.ip6ph_src = ip6->ip6_src;
2837 in6_clearscope(&ip6pseudo.ip6ph_src);
2838 ip6pseudo.ip6ph_dst = ip6->ip6_dst;
2839 in6_clearscope(&ip6pseudo.ip6ph_dst);
2840 ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff);
2841 ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
2842 MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo));
2843 }
2844
2845 th0 = *th;
2846 th0.th_sum = 0;
2847 MD5Update(&ctx, (char *)&th0, sizeof(th0));
2848
2849 l = m->m_pkthdr.len - thoff - tcphdrlen;
2850 if (l > 0)
2851 m_apply(m, thoff + tcphdrlen,
2852 m->m_pkthdr.len - thoff - tcphdrlen,
2853 tcp_signature_apply, &ctx);
2854
2855 MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2856 MD5Final(sig, &ctx);
2857
2858 return (0);
2859 }
2860 #endif
2861
2862 static int
2863 tcp_dooptions(struct tcpcb *tp, const u_char *cp, int cnt,
2864 struct tcphdr *th,
2865 struct mbuf *m, int toff, struct tcp_opt_info *oi)
2866 {
2867 u_int16_t mss;
2868 int opt, optlen = 0;
2869 #ifdef TCP_SIGNATURE
2870 void *sigp = NULL;
2871 char sigbuf[TCP_SIGLEN];
2872 struct secasvar *sav = NULL;
2873 #endif
2874
2875 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) {
2876 opt = cp[0];
2877 if (opt == TCPOPT_EOL)
2878 break;
2879 if (opt == TCPOPT_NOP)
2880 optlen = 1;
2881 else {
2882 if (cnt < 2)
2883 break;
2884 optlen = cp[1];
2885 if (optlen < 2 || optlen > cnt)
2886 break;
2887 }
2888 switch (opt) {
2889
2890 default:
2891 continue;
2892
2893 case TCPOPT_MAXSEG:
2894 if (optlen != TCPOLEN_MAXSEG)
2895 continue;
2896 if (!(th->th_flags & TH_SYN))
2897 continue;
2898 if (TCPS_HAVERCVDSYN(tp->t_state))
2899 continue;
2900 bcopy(cp + 2, &mss, sizeof(mss));
2901 oi->maxseg = ntohs(mss);
2902 break;
2903
2904 case TCPOPT_WINDOW:
2905 if (optlen != TCPOLEN_WINDOW)
2906 continue;
2907 if (!(th->th_flags & TH_SYN))
2908 continue;
2909 if (TCPS_HAVERCVDSYN(tp->t_state))
2910 continue;
2911 tp->t_flags |= TF_RCVD_SCALE;
2912 tp->requested_s_scale = cp[2];
2913 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
2914 #if 0 /*XXX*/
2915 char *p;
2916
2917 if (ip)
2918 p = ntohl(ip->ip_src);
2919 #ifdef INET6
2920 else if (ip6)
2921 p = ip6_sprintf(&ip6->ip6_src);
2922 #endif
2923 else
2924 p = "(unknown)";
2925 log(LOG_ERR, "TCP: invalid wscale %d from %s, "
2926 "assuming %d\n",
2927 tp->requested_s_scale, p,
2928 TCP_MAX_WINSHIFT);
2929 #else
2930 log(LOG_ERR, "TCP: invalid wscale %d, "
2931 "assuming %d\n",
2932 tp->requested_s_scale,
2933 TCP_MAX_WINSHIFT);
2934 #endif
2935 tp->requested_s_scale = TCP_MAX_WINSHIFT;
2936 }
2937 break;
2938
2939 case TCPOPT_TIMESTAMP:
2940 if (optlen != TCPOLEN_TIMESTAMP)
2941 continue;
2942 oi->ts_present = 1;
2943 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
2944 NTOHL(oi->ts_val);
2945 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
2946 NTOHL(oi->ts_ecr);
2947
2948 if (!(th->th_flags & TH_SYN))
2949 continue;
2950 if (TCPS_HAVERCVDSYN(tp->t_state))
2951 continue;
2952 /*
2953 * A timestamp received in a SYN makes
2954 * it ok to send timestamp requests and replies.
2955 */
2956 tp->t_flags |= TF_RCVD_TSTMP;
2957 tp->ts_recent = oi->ts_val;
2958 tp->ts_recent_age = tcp_now;
2959 break;
2960
2961 case TCPOPT_SACK_PERMITTED:
2962 if (optlen != TCPOLEN_SACK_PERMITTED)
2963 continue;
2964 if (!(th->th_flags & TH_SYN))
2965 continue;
2966 if (TCPS_HAVERCVDSYN(tp->t_state))
2967 continue;
2968 if (tcp_do_sack) {
2969 tp->t_flags |= TF_SACK_PERMIT;
2970 tp->t_flags |= TF_WILL_SACK;
2971 }
2972 break;
2973
2974 case TCPOPT_SACK:
2975 tcp_sack_option(tp, th, cp, optlen);
2976 break;
2977 #ifdef TCP_SIGNATURE
2978 case TCPOPT_SIGNATURE:
2979 if (optlen != TCPOLEN_SIGNATURE)
2980 continue;
2981 if (sigp && bcmp(sigp, cp + 2, TCP_SIGLEN))
2982 return (-1);
2983
2984 sigp = sigbuf;
2985 memcpy(sigbuf, cp + 2, TCP_SIGLEN);
2986 tp->t_flags |= TF_SIGNATURE;
2987 break;
2988 #endif
2989 }
2990 }
2991
2992 #ifdef TCP_SIGNATURE
2993 if (tp->t_flags & TF_SIGNATURE) {
2994
2995 sav = tcp_signature_getsav(m, th);
2996
2997 if (sav == NULL && tp->t_state == TCPS_LISTEN)
2998 return (-1);
2999 }
3000
3001 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) {
3002 if (sav == NULL)
3003 return (-1);
3004 #ifdef FAST_IPSEC
3005 KEY_FREESAV(&sav);
3006 #else
3007 key_freesav(sav);
3008 #endif
3009 return (-1);
3010 }
3011
3012 if (sigp) {
3013 char sig[TCP_SIGLEN];
3014
3015 TCP_FIELDS_TO_NET(th);
3016 if (tcp_signature(m, th, toff, sav, sig) < 0) {
3017 TCP_FIELDS_TO_HOST(th);
3018 if (sav == NULL)
3019 return (-1);
3020 #ifdef FAST_IPSEC
3021 KEY_FREESAV(&sav);
3022 #else
3023 key_freesav(sav);
3024 #endif
3025 return (-1);
3026 }
3027 TCP_FIELDS_TO_HOST(th);
3028
3029 if (bcmp(sig, sigp, TCP_SIGLEN)) {
3030 tcpstat.tcps_badsig++;
3031 if (sav == NULL)
3032 return (-1);
3033 #ifdef FAST_IPSEC
3034 KEY_FREESAV(&sav);
3035 #else
3036 key_freesav(sav);
3037 #endif
3038 return (-1);
3039 } else
3040 tcpstat.tcps_goodsig++;
3041
3042 key_sa_recordxfer(sav, m);
3043 #ifdef FAST_IPSEC
3044 KEY_FREESAV(&sav);
3045 #else
3046 key_freesav(sav);
3047 #endif
3048 }
3049 #endif
3050
3051 return (0);
3052 }
3053
3054 /*
3055 * Pull out of band byte out of a segment so
3056 * it doesn't appear in the user's data queue.
3057 * It is still reflected in the segment length for
3058 * sequencing purposes.
3059 */
3060 void
3061 tcp_pulloutofband(struct socket *so, struct tcphdr *th,
3062 struct mbuf *m, int off)
3063 {
3064 int cnt = off + th->th_urp - 1;
3065
3066 while (cnt >= 0) {
3067 if (m->m_len > cnt) {
3068 char *cp = mtod(m, char *) + cnt;
3069 struct tcpcb *tp = sototcpcb(so);
3070
3071 tp->t_iobc = *cp;
3072 tp->t_oobflags |= TCPOOB_HAVEDATA;
3073 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3074 m->m_len--;
3075 return;
3076 }
3077 cnt -= m->m_len;
3078 m = m->m_next;
3079 if (m == 0)
3080 break;
3081 }
3082 panic("tcp_pulloutofband");
3083 }
3084
3085 /*
3086 * Collect new round-trip time estimate
3087 * and update averages and current timeout.
3088 */
3089 void
3090 tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt)
3091 {
3092 int32_t delta;
3093
3094 tcpstat.tcps_rttupdated++;
3095 if (tp->t_srtt != 0) {
3096 /*
3097 * srtt is stored as fixed point with 3 bits after the
3098 * binary point (i.e., scaled by 8). The following magic
3099 * is equivalent to the smoothing algorithm in rfc793 with
3100 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3101 * point). Adjust rtt to origin 0.
3102 */
3103 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
3104 if ((tp->t_srtt += delta) <= 0)
3105 tp->t_srtt = 1 << 2;
3106 /*
3107 * We accumulate a smoothed rtt variance (actually, a
3108 * smoothed mean difference), then set the retransmit
3109 * timer to smoothed rtt + 4 times the smoothed variance.
3110 * rttvar is stored as fixed point with 2 bits after the
3111 * binary point (scaled by 4). The following is
3112 * equivalent to rfc793 smoothing with an alpha of .75
3113 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3114 * rfc793's wired-in beta.
3115 */
3116 if (delta < 0)
3117 delta = -delta;
3118 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
3119 if ((tp->t_rttvar += delta) <= 0)
3120 tp->t_rttvar = 1 << 2;
3121 } else {
3122 /*
3123 * No rtt measurement yet - use the unsmoothed rtt.
3124 * Set the variance to half the rtt (so our first
3125 * retransmit happens at 3*rtt).
3126 */
3127 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
3128 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
3129 }
3130 tp->t_rtttime = 0;
3131 tp->t_rxtshift = 0;
3132
3133 /*
3134 * the retransmit should happen at rtt + 4 * rttvar.
3135 * Because of the way we do the smoothing, srtt and rttvar
3136 * will each average +1/2 tick of bias. When we compute
3137 * the retransmit timer, we want 1/2 tick of rounding and
3138 * 1 extra tick because of +-1/2 tick uncertainty in the
3139 * firing of the timer. The bias will give us exactly the
3140 * 1.5 tick we need. But, because the bias is
3141 * statistical, we have to test that we don't drop below
3142 * the minimum feasible timer (which is 2 ticks).
3143 */
3144 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3145 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3146
3147 /*
3148 * We received an ack for a packet that wasn't retransmitted;
3149 * it is probably safe to discard any error indications we've
3150 * received recently. This isn't quite right, but close enough
3151 * for now (a route might have failed after we sent a segment,
3152 * and the return path might not be symmetrical).
3153 */
3154 tp->t_softerror = 0;
3155 }
3156
3157
3158 /*
3159 * TCP compressed state engine. Currently used to hold compressed
3160 * state for SYN_RECEIVED.
3161 */
3162
3163 u_long syn_cache_count;
3164 u_int32_t syn_hash1, syn_hash2;
3165
3166 #define SYN_HASH(sa, sp, dp) \
3167 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
3168 ((u_int32_t)(sp)))^syn_hash2)))
3169 #ifndef INET6
3170 #define SYN_HASHALL(hash, src, dst) \
3171 do { \
3172 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \
3173 ((const struct sockaddr_in *)(src))->sin_port, \
3174 ((const struct sockaddr_in *)(dst))->sin_port); \
3175 } while (/*CONSTCOND*/ 0)
3176 #else
3177 #define SYN_HASH6(sa, sp, dp) \
3178 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
3179 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
3180 & 0x7fffffff)
3181
3182 #define SYN_HASHALL(hash, src, dst) \
3183 do { \
3184 switch ((src)->sa_family) { \
3185 case AF_INET: \
3186 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \
3187 ((const struct sockaddr_in *)(src))->sin_port, \
3188 ((const struct sockaddr_in *)(dst))->sin_port); \
3189 break; \
3190 case AF_INET6: \
3191 hash = SYN_HASH6(&((const struct sockaddr_in6 *)(src))->sin6_addr, \
3192 ((const struct sockaddr_in6 *)(src))->sin6_port, \
3193 ((const struct sockaddr_in6 *)(dst))->sin6_port); \
3194 break; \
3195 default: \
3196 hash = 0; \
3197 } \
3198 } while (/*CONSTCOND*/0)
3199 #endif /* INET6 */
3200
3201 #define SYN_CACHE_RM(sc) \
3202 do { \
3203 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \
3204 (sc), sc_bucketq); \
3205 (sc)->sc_tp = NULL; \
3206 LIST_REMOVE((sc), sc_tpq); \
3207 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \
3208 callout_stop(&(sc)->sc_timer); \
3209 syn_cache_count--; \
3210 } while (/*CONSTCOND*/0)
3211
3212 #define SYN_CACHE_PUT(sc) \
3213 do { \
3214 if ((sc)->sc_ipopts) \
3215 (void) m_free((sc)->sc_ipopts); \
3216 rtcache_free(&(sc)->sc_route); \
3217 if (callout_invoking(&(sc)->sc_timer)) \
3218 (sc)->sc_flags |= SCF_DEAD; \
3219 else { \
3220 callout_destroy(&sc->sc_timer); \
3221 pool_put(&syn_cache_pool, (sc)); \
3222 } \
3223 } while (/*CONSTCOND*/0)
3224
3225 POOL_INIT(syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0, "synpl", NULL,
3226 IPL_SOFTNET);
3227
3228 /*
3229 * We don't estimate RTT with SYNs, so each packet starts with the default
3230 * RTT and each timer step has a fixed timeout value.
3231 */
3232 #define SYN_CACHE_TIMER_ARM(sc) \
3233 do { \
3234 TCPT_RANGESET((sc)->sc_rxtcur, \
3235 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \
3236 TCPTV_REXMTMAX); \
3237 callout_reset(&(sc)->sc_timer, \
3238 (sc)->sc_rxtcur * (hz / PR_SLOWHZ), syn_cache_timer, (sc)); \
3239 } while (/*CONSTCOND*/0)
3240
3241 #define SYN_CACHE_TIMESTAMP(sc) (tcp_now - (sc)->sc_timebase)
3242
3243 void
3244 syn_cache_init(void)
3245 {
3246 int i;
3247
3248 /* Initialize the hash buckets. */
3249 for (i = 0; i < tcp_syn_cache_size; i++)
3250 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
3251 }
3252
3253 void
3254 syn_cache_insert(struct syn_cache *sc, struct tcpcb *tp)
3255 {
3256 struct syn_cache_head *scp;
3257 struct syn_cache *sc2;
3258 int s;
3259
3260 /*
3261 * If there are no entries in the hash table, reinitialize
3262 * the hash secrets.
3263 */
3264 if (syn_cache_count == 0) {
3265 syn_hash1 = arc4random();
3266 syn_hash2 = arc4random();
3267 }
3268
3269 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
3270 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
3271 scp = &tcp_syn_cache[sc->sc_bucketidx];
3272
3273 /*
3274 * Make sure that we don't overflow the per-bucket
3275 * limit or the total cache size limit.
3276 */
3277 s = splsoftnet();
3278 if (scp->sch_length >= tcp_syn_bucket_limit) {
3279 tcpstat.tcps_sc_bucketoverflow++;
3280 /*
3281 * The bucket is full. Toss the oldest element in the
3282 * bucket. This will be the first entry in the bucket.
3283 */
3284 sc2 = TAILQ_FIRST(&scp->sch_bucket);
3285 #ifdef DIAGNOSTIC
3286 /*
3287 * This should never happen; we should always find an
3288 * entry in our bucket.
3289 */
3290 if (sc2 == NULL)
3291 panic("syn_cache_insert: bucketoverflow: impossible");
3292 #endif
3293 SYN_CACHE_RM(sc2);
3294 SYN_CACHE_PUT(sc2); /* calls pool_put but see spl above */
3295 } else if (syn_cache_count >= tcp_syn_cache_limit) {
3296 struct syn_cache_head *scp2, *sce;
3297
3298 tcpstat.tcps_sc_overflowed++;
3299 /*
3300 * The cache is full. Toss the oldest entry in the
3301 * first non-empty bucket we can find.
3302 *
3303 * XXX We would really like to toss the oldest
3304 * entry in the cache, but we hope that this
3305 * condition doesn't happen very often.
3306 */
3307 scp2 = scp;
3308 if (TAILQ_EMPTY(&scp2->sch_bucket)) {
3309 sce = &tcp_syn_cache[tcp_syn_cache_size];
3310 for (++scp2; scp2 != scp; scp2++) {
3311 if (scp2 >= sce)
3312 scp2 = &tcp_syn_cache[0];
3313 if (! TAILQ_EMPTY(&scp2->sch_bucket))
3314 break;
3315 }
3316 #ifdef DIAGNOSTIC
3317 /*
3318 * This should never happen; we should always find a
3319 * non-empty bucket.
3320 */
3321 if (scp2 == scp)
3322 panic("syn_cache_insert: cacheoverflow: "
3323 "impossible");
3324 #endif
3325 }
3326 sc2 = TAILQ_FIRST(&scp2->sch_bucket);
3327 SYN_CACHE_RM(sc2);
3328 SYN_CACHE_PUT(sc2); /* calls pool_put but see spl above */
3329 }
3330
3331 /*
3332 * Initialize the entry's timer.
3333 */
3334 sc->sc_rxttot = 0;
3335 sc->sc_rxtshift = 0;
3336 SYN_CACHE_TIMER_ARM(sc);
3337
3338 /* Link it from tcpcb entry */
3339 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
3340
3341 /* Put it into the bucket. */
3342 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
3343 scp->sch_length++;
3344 syn_cache_count++;
3345
3346 tcpstat.tcps_sc_added++;
3347 splx(s);
3348 }
3349
3350 /*
3351 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
3352 * If we have retransmitted an entry the maximum number of times, expire
3353 * that entry.
3354 */
3355 void
3356 syn_cache_timer(void *arg)
3357 {
3358 struct syn_cache *sc = arg;
3359 int s;
3360
3361 s = splsoftnet();
3362 callout_ack(&sc->sc_timer);
3363
3364 if (__predict_false(sc->sc_flags & SCF_DEAD)) {
3365 tcpstat.tcps_sc_delayed_free++;
3366 callout_destroy(&sc->sc_timer);
3367 pool_put(&syn_cache_pool, sc);
3368 splx(s);
3369 return;
3370 }
3371
3372 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
3373 /* Drop it -- too many retransmissions. */
3374 goto dropit;
3375 }
3376
3377 /*
3378 * Compute the total amount of time this entry has
3379 * been on a queue. If this entry has been on longer
3380 * than the keep alive timer would allow, expire it.
3381 */
3382 sc->sc_rxttot += sc->sc_rxtcur;
3383 if (sc->sc_rxttot >= tcp_keepinit)
3384 goto dropit;
3385
3386 tcpstat.tcps_sc_retransmitted++;
3387 (void) syn_cache_respond(sc, NULL);
3388
3389 /* Advance the timer back-off. */
3390 sc->sc_rxtshift++;
3391 SYN_CACHE_TIMER_ARM(sc);
3392
3393 splx(s);
3394 return;
3395
3396 dropit:
3397 tcpstat.tcps_sc_timed_out++;
3398 SYN_CACHE_RM(sc);
3399 SYN_CACHE_PUT(sc); /* calls pool_put but see spl above */
3400 splx(s);
3401 }
3402
3403 /*
3404 * Remove syn cache created by the specified tcb entry,
3405 * because this does not make sense to keep them
3406 * (if there's no tcb entry, syn cache entry will never be used)
3407 */
3408 void
3409 syn_cache_cleanup(struct tcpcb *tp)
3410 {
3411 struct syn_cache *sc, *nsc;
3412 int s;
3413
3414 s = splsoftnet();
3415
3416 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
3417 nsc = LIST_NEXT(sc, sc_tpq);
3418
3419 #ifdef DIAGNOSTIC
3420 if (sc->sc_tp != tp)
3421 panic("invalid sc_tp in syn_cache_cleanup");
3422 #endif
3423 SYN_CACHE_RM(sc);
3424 SYN_CACHE_PUT(sc); /* calls pool_put but see spl above */
3425 }
3426 /* just for safety */
3427 LIST_INIT(&tp->t_sc);
3428
3429 splx(s);
3430 }
3431
3432 /*
3433 * Find an entry in the syn cache.
3434 */
3435 struct syn_cache *
3436 syn_cache_lookup(const struct sockaddr *src, const struct sockaddr *dst,
3437 struct syn_cache_head **headp)
3438 {
3439 struct syn_cache *sc;
3440 struct syn_cache_head *scp;
3441 u_int32_t hash;
3442 int s;
3443
3444 SYN_HASHALL(hash, src, dst);
3445
3446 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
3447 *headp = scp;
3448 s = splsoftnet();
3449 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
3450 sc = TAILQ_NEXT(sc, sc_bucketq)) {
3451 if (sc->sc_hash != hash)
3452 continue;
3453 if (!bcmp(&sc->sc_src, src, src->sa_len) &&
3454 !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
3455 splx(s);
3456 return (sc);
3457 }
3458 }
3459 splx(s);
3460 return (NULL);
3461 }
3462
3463 /*
3464 * This function gets called when we receive an ACK for a
3465 * socket in the LISTEN state. We look up the connection
3466 * in the syn cache, and if its there, we pull it out of
3467 * the cache and turn it into a full-blown connection in
3468 * the SYN-RECEIVED state.
3469 *
3470 * The return values may not be immediately obvious, and their effects
3471 * can be subtle, so here they are:
3472 *
3473 * NULL SYN was not found in cache; caller should drop the
3474 * packet and send an RST.
3475 *
3476 * -1 We were unable to create the new connection, and are
3477 * aborting it. An ACK,RST is being sent to the peer
3478 * (unless we got screwey sequence numbners; see below),
3479 * because the 3-way handshake has been completed. Caller
3480 * should not free the mbuf, since we may be using it. If
3481 * we are not, we will free it.
3482 *
3483 * Otherwise, the return value is a pointer to the new socket
3484 * associated with the connection.
3485 */
3486 struct socket *
3487 syn_cache_get(struct sockaddr *src, struct sockaddr *dst,
3488 struct tcphdr *th, unsigned int hlen, unsigned int tlen,
3489 struct socket *so, struct mbuf *m)
3490 {
3491 struct syn_cache *sc;
3492 struct syn_cache_head *scp;
3493 struct inpcb *inp = NULL;
3494 #ifdef INET6
3495 struct in6pcb *in6p = NULL;
3496 #endif
3497 struct tcpcb *tp = 0;
3498 struct mbuf *am;
3499 int s;
3500 struct socket *oso;
3501
3502 s = splsoftnet();
3503 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3504 splx(s);
3505 return (NULL);
3506 }
3507
3508 /*
3509 * Verify the sequence and ack numbers. Try getting the correct
3510 * response again.
3511 */
3512 if ((th->th_ack != sc->sc_iss + 1) ||
3513 SEQ_LEQ(th->th_seq, sc->sc_irs) ||
3514 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
3515 (void) syn_cache_respond(sc, m);
3516 splx(s);
3517 return ((struct socket *)(-1));
3518 }
3519
3520 /* Remove this cache entry */
3521 SYN_CACHE_RM(sc);
3522 splx(s);
3523
3524 /*
3525 * Ok, create the full blown connection, and set things up
3526 * as they would have been set up if we had created the
3527 * connection when the SYN arrived. If we can't create
3528 * the connection, abort it.
3529 */
3530 /*
3531 * inp still has the OLD in_pcb stuff, set the
3532 * v6-related flags on the new guy, too. This is
3533 * done particularly for the case where an AF_INET6
3534 * socket is bound only to a port, and a v4 connection
3535 * comes in on that port.
3536 * we also copy the flowinfo from the original pcb
3537 * to the new one.
3538 */
3539 oso = so;
3540 so = sonewconn(so, SS_ISCONNECTED);
3541 if (so == NULL)
3542 goto resetandabort;
3543
3544 switch (so->so_proto->pr_domain->dom_family) {
3545 #ifdef INET
3546 case AF_INET:
3547 inp = sotoinpcb(so);
3548 break;
3549 #endif
3550 #ifdef INET6
3551 case AF_INET6:
3552 in6p = sotoin6pcb(so);
3553 break;
3554 #endif
3555 }
3556 switch (src->sa_family) {
3557 #ifdef INET
3558 case AF_INET:
3559 if (inp) {
3560 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
3561 inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port;
3562 inp->inp_options = ip_srcroute();
3563 in_pcbstate(inp, INP_BOUND);
3564 if (inp->inp_options == NULL) {
3565 inp->inp_options = sc->sc_ipopts;
3566 sc->sc_ipopts = NULL;
3567 }
3568 }
3569 #ifdef INET6
3570 else if (in6p) {
3571 /* IPv4 packet to AF_INET6 socket */
3572 bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
3573 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
3574 bcopy(&((struct sockaddr_in *)dst)->sin_addr,
3575 &in6p->in6p_laddr.s6_addr32[3],
3576 sizeof(((struct sockaddr_in *)dst)->sin_addr));
3577 in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port;
3578 in6totcpcb(in6p)->t_family = AF_INET;
3579 if (sotoin6pcb(oso)->in6p_flags & IN6P_IPV6_V6ONLY)
3580 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
3581 else
3582 in6p->in6p_flags &= ~IN6P_IPV6_V6ONLY;
3583 in6_pcbstate(in6p, IN6P_BOUND);
3584 }
3585 #endif
3586 break;
3587 #endif
3588 #ifdef INET6
3589 case AF_INET6:
3590 if (in6p) {
3591 in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr;
3592 in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port;
3593 in6_pcbstate(in6p, IN6P_BOUND);
3594 }
3595 break;
3596 #endif
3597 }
3598 #ifdef INET6
3599 if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) {
3600 struct in6pcb *oin6p = sotoin6pcb(oso);
3601 /* inherit socket options from the listening socket */
3602 in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS);
3603 if (in6p->in6p_flags & IN6P_CONTROLOPTS) {
3604 m_freem(in6p->in6p_options);
3605 in6p->in6p_options = 0;
3606 }
3607 ip6_savecontrol(in6p, &in6p->in6p_options,
3608 mtod(m, struct ip6_hdr *), m);
3609 }
3610 #endif
3611
3612 #if defined(IPSEC) || defined(FAST_IPSEC)
3613 /*
3614 * we make a copy of policy, instead of sharing the policy,
3615 * for better behavior in terms of SA lookup and dead SA removal.
3616 */
3617 if (inp) {
3618 /* copy old policy into new socket's */
3619 if (ipsec_copy_pcbpolicy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
3620 printf("tcp_input: could not copy policy\n");
3621 }
3622 #ifdef INET6
3623 else if (in6p) {
3624 /* copy old policy into new socket's */
3625 if (ipsec_copy_pcbpolicy(sotoin6pcb(oso)->in6p_sp,
3626 in6p->in6p_sp))
3627 printf("tcp_input: could not copy policy\n");
3628 }
3629 #endif
3630 #endif
3631
3632 /*
3633 * Give the new socket our cached route reference.
3634 */
3635 if (inp) {
3636 rtcache_copy(&inp->inp_route, &sc->sc_route);
3637 rtcache_free(&sc->sc_route);
3638 }
3639 #ifdef INET6
3640 else {
3641 rtcache_copy(&in6p->in6p_route, &sc->sc_route);
3642 rtcache_free(&sc->sc_route);
3643 }
3644 #endif
3645
3646 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
3647 if (am == NULL)
3648 goto resetandabort;
3649 MCLAIM(am, &tcp_mowner);
3650 am->m_len = src->sa_len;
3651 bcopy(src, mtod(am, void *), src->sa_len);
3652 if (inp) {
3653 if (in_pcbconnect(inp, am, NULL)) {
3654 (void) m_free(am);
3655 goto resetandabort;
3656 }
3657 }
3658 #ifdef INET6
3659 else if (in6p) {
3660 if (src->sa_family == AF_INET) {
3661 /* IPv4 packet to AF_INET6 socket */
3662 struct sockaddr_in6 *sin6;
3663 sin6 = mtod(am, struct sockaddr_in6 *);
3664 am->m_len = sizeof(*sin6);
3665 bzero(sin6, sizeof(*sin6));
3666 sin6->sin6_family = AF_INET6;
3667 sin6->sin6_len = sizeof(*sin6);
3668 sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port;
3669 sin6->sin6_addr.s6_addr16[5] = htons(0xffff);
3670 bcopy(&((struct sockaddr_in *)src)->sin_addr,
3671 &sin6->sin6_addr.s6_addr32[3],
3672 sizeof(sin6->sin6_addr.s6_addr32[3]));
3673 }
3674 if (in6_pcbconnect(in6p, am, NULL)) {
3675 (void) m_free(am);
3676 goto resetandabort;
3677 }
3678 }
3679 #endif
3680 else {
3681 (void) m_free(am);
3682 goto resetandabort;
3683 }
3684 (void) m_free(am);
3685
3686 if (inp)
3687 tp = intotcpcb(inp);
3688 #ifdef INET6
3689 else if (in6p)
3690 tp = in6totcpcb(in6p);
3691 #endif
3692 else
3693 tp = NULL;
3694 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
3695 if (sc->sc_request_r_scale != 15) {
3696 tp->requested_s_scale = sc->sc_requested_s_scale;
3697 tp->request_r_scale = sc->sc_request_r_scale;
3698 tp->snd_scale = sc->sc_requested_s_scale;
3699 tp->rcv_scale = sc->sc_request_r_scale;
3700 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
3701 }
3702 if (sc->sc_flags & SCF_TIMESTAMP)
3703 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
3704 tp->ts_timebase = sc->sc_timebase;
3705
3706 tp->t_template = tcp_template(tp);
3707 if (tp->t_template == 0) {
3708 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
3709 so = NULL;
3710 m_freem(m);
3711 goto abort;
3712 }
3713
3714 tp->iss = sc->sc_iss;
3715 tp->irs = sc->sc_irs;
3716 tcp_sendseqinit(tp);
3717 tcp_rcvseqinit(tp);
3718 tp->t_state = TCPS_SYN_RECEIVED;
3719 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
3720 tcpstat.tcps_accepts++;
3721
3722 if ((sc->sc_flags & SCF_SACK_PERMIT) && tcp_do_sack)
3723 tp->t_flags |= TF_WILL_SACK;
3724
3725 if ((sc->sc_flags & SCF_ECN_PERMIT) && tcp_do_ecn)
3726 tp->t_flags |= TF_ECN_PERMIT;
3727
3728 #ifdef TCP_SIGNATURE
3729 if (sc->sc_flags & SCF_SIGNATURE)
3730 tp->t_flags |= TF_SIGNATURE;
3731 #endif
3732
3733 /* Initialize tp->t_ourmss before we deal with the peer's! */
3734 tp->t_ourmss = sc->sc_ourmaxseg;
3735 tcp_mss_from_peer(tp, sc->sc_peermaxseg);
3736
3737 /*
3738 * Initialize the initial congestion window. If we
3739 * had to retransmit the SYN,ACK, we must initialize cwnd
3740 * to 1 segment (i.e. the Loss Window).
3741 */
3742 if (sc->sc_rxtshift)
3743 tp->snd_cwnd = tp->t_peermss;
3744 else {
3745 int ss = tcp_init_win;
3746 #ifdef INET
3747 if (inp != NULL && in_localaddr(inp->inp_faddr))
3748 ss = tcp_init_win_local;
3749 #endif
3750 #ifdef INET6
3751 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
3752 ss = tcp_init_win_local;
3753 #endif
3754 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
3755 }
3756
3757 tcp_rmx_rtt(tp);
3758 tp->snd_wl1 = sc->sc_irs;
3759 tp->rcv_up = sc->sc_irs + 1;
3760
3761 /*
3762 * This is what whould have happened in tcp_output() when
3763 * the SYN,ACK was sent.
3764 */
3765 tp->snd_up = tp->snd_una;
3766 tp->snd_max = tp->snd_nxt = tp->iss+1;
3767 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
3768 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
3769 tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
3770 tp->last_ack_sent = tp->rcv_nxt;
3771 tp->t_partialacks = -1;
3772 tp->t_dupacks = 0;
3773
3774 tcpstat.tcps_sc_completed++;
3775 s = splsoftnet();
3776 SYN_CACHE_PUT(sc);
3777 splx(s);
3778 return (so);
3779
3780 resetandabort:
3781 (void)tcp_respond(NULL, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
3782 abort:
3783 if (so != NULL)
3784 (void) soabort(so);
3785 s = splsoftnet();
3786 SYN_CACHE_PUT(sc);
3787 splx(s);
3788 tcpstat.tcps_sc_aborted++;
3789 return ((struct socket *)(-1));
3790 }
3791
3792 /*
3793 * This function is called when we get a RST for a
3794 * non-existent connection, so that we can see if the
3795 * connection is in the syn cache. If it is, zap it.
3796 */
3797
3798 void
3799 syn_cache_reset(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th)
3800 {
3801 struct syn_cache *sc;
3802 struct syn_cache_head *scp;
3803 int s = splsoftnet();
3804
3805 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3806 splx(s);
3807 return;
3808 }
3809 if (SEQ_LT(th->th_seq, sc->sc_irs) ||
3810 SEQ_GT(th->th_seq, sc->sc_irs+1)) {
3811 splx(s);
3812 return;
3813 }
3814 SYN_CACHE_RM(sc);
3815 tcpstat.tcps_sc_reset++;
3816 SYN_CACHE_PUT(sc); /* calls pool_put but see spl above */
3817 splx(s);
3818 }
3819
3820 void
3821 syn_cache_unreach(const struct sockaddr *src, const struct sockaddr *dst,
3822 struct tcphdr *th)
3823 {
3824 struct syn_cache *sc;
3825 struct syn_cache_head *scp;
3826 int s;
3827
3828 s = splsoftnet();
3829 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3830 splx(s);
3831 return;
3832 }
3833 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
3834 if (ntohl (th->th_seq) != sc->sc_iss) {
3835 splx(s);
3836 return;
3837 }
3838
3839 /*
3840 * If we've retransmitted 3 times and this is our second error,
3841 * we remove the entry. Otherwise, we allow it to continue on.
3842 * This prevents us from incorrectly nuking an entry during a
3843 * spurious network outage.
3844 *
3845 * See tcp_notify().
3846 */
3847 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
3848 sc->sc_flags |= SCF_UNREACH;
3849 splx(s);
3850 return;
3851 }
3852
3853 SYN_CACHE_RM(sc);
3854 tcpstat.tcps_sc_unreach++;
3855 SYN_CACHE_PUT(sc); /* calls pool_put but see spl above */
3856 splx(s);
3857 }
3858
3859 /*
3860 * Given a LISTEN socket and an inbound SYN request, add
3861 * this to the syn cache, and send back a segment:
3862 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3863 * to the source.
3864 *
3865 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
3866 * Doing so would require that we hold onto the data and deliver it
3867 * to the application. However, if we are the target of a SYN-flood
3868 * DoS attack, an attacker could send data which would eventually
3869 * consume all available buffer space if it were ACKed. By not ACKing
3870 * the data, we avoid this DoS scenario.
3871 */
3872
3873 int
3874 syn_cache_add(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th,
3875 unsigned int hlen, struct socket *so, struct mbuf *m, u_char *optp,
3876 int optlen, struct tcp_opt_info *oi)
3877 {
3878 struct tcpcb tb, *tp;
3879 long win;
3880 struct syn_cache *sc;
3881 struct syn_cache_head *scp;
3882 struct mbuf *ipopts;
3883 struct tcp_opt_info opti;
3884 int s;
3885
3886 tp = sototcpcb(so);
3887
3888 bzero(&opti, sizeof(opti));
3889
3890 /*
3891 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
3892 *
3893 * Note this check is performed in tcp_input() very early on.
3894 */
3895
3896 /*
3897 * Initialize some local state.
3898 */
3899 win = sbspace(&so->so_rcv);
3900 if (win > TCP_MAXWIN)
3901 win = TCP_MAXWIN;
3902
3903 switch (src->sa_family) {
3904 #ifdef INET
3905 case AF_INET:
3906 /*
3907 * Remember the IP options, if any.
3908 */
3909 ipopts = ip_srcroute();
3910 break;
3911 #endif
3912 default:
3913 ipopts = NULL;
3914 }
3915
3916 #ifdef TCP_SIGNATURE
3917 if (optp || (tp->t_flags & TF_SIGNATURE))
3918 #else
3919 if (optp)
3920 #endif
3921 {
3922 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
3923 #ifdef TCP_SIGNATURE
3924 tb.t_flags |= (tp->t_flags & TF_SIGNATURE);
3925 #endif
3926 tb.t_state = TCPS_LISTEN;
3927 if (tcp_dooptions(&tb, optp, optlen, th, m, m->m_pkthdr.len -
3928 sizeof(struct tcphdr) - optlen - hlen, oi) < 0)
3929 return (0);
3930 } else
3931 tb.t_flags = 0;
3932
3933 /*
3934 * See if we already have an entry for this connection.
3935 * If we do, resend the SYN,ACK. We do not count this
3936 * as a retransmission (XXX though maybe we should).
3937 */
3938 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
3939 tcpstat.tcps_sc_dupesyn++;
3940 if (ipopts) {
3941 /*
3942 * If we were remembering a previous source route,
3943 * forget it and use the new one we've been given.
3944 */
3945 if (sc->sc_ipopts)
3946 (void) m_free(sc->sc_ipopts);
3947 sc->sc_ipopts = ipopts;
3948 }
3949 sc->sc_timestamp = tb.ts_recent;
3950 if (syn_cache_respond(sc, m) == 0) {
3951 tcpstat.tcps_sndacks++;
3952 tcpstat.tcps_sndtotal++;
3953 }
3954 return (1);
3955 }
3956
3957 s = splsoftnet();
3958 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
3959 splx(s);
3960 if (sc == NULL) {
3961 if (ipopts)
3962 (void) m_free(ipopts);
3963 return (0);
3964 }
3965
3966 /*
3967 * Fill in the cache, and put the necessary IP and TCP
3968 * options into the reply.
3969 */
3970 bzero(sc, sizeof(struct syn_cache));
3971 callout_init(&sc->sc_timer, 0);
3972 bcopy(src, &sc->sc_src, src->sa_len);
3973 bcopy(dst, &sc->sc_dst, dst->sa_len);
3974 sc->sc_flags = 0;
3975 sc->sc_ipopts = ipopts;
3976 sc->sc_irs = th->th_seq;
3977 switch (src->sa_family) {
3978 #ifdef INET
3979 case AF_INET:
3980 {
3981 struct sockaddr_in *srcin = (void *) src;
3982 struct sockaddr_in *dstin = (void *) dst;
3983
3984 sc->sc_iss = tcp_new_iss1(&dstin->sin_addr,
3985 &srcin->sin_addr, dstin->sin_port,
3986 srcin->sin_port, sizeof(dstin->sin_addr), 0);
3987 break;
3988 }
3989 #endif /* INET */
3990 #ifdef INET6
3991 case AF_INET6:
3992 {
3993 struct sockaddr_in6 *srcin6 = (void *) src;
3994 struct sockaddr_in6 *dstin6 = (void *) dst;
3995
3996 sc->sc_iss = tcp_new_iss1(&dstin6->sin6_addr,
3997 &srcin6->sin6_addr, dstin6->sin6_port,
3998 srcin6->sin6_port, sizeof(dstin6->sin6_addr), 0);
3999 break;
4000 }
4001 #endif /* INET6 */
4002 }
4003 sc->sc_peermaxseg = oi->maxseg;
4004 sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
4005 m->m_pkthdr.rcvif : NULL,
4006 sc->sc_src.sa.sa_family);
4007 sc->sc_win = win;
4008 sc->sc_timebase = tcp_now; /* see tcp_newtcpcb() */
4009 sc->sc_timestamp = tb.ts_recent;
4010 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) ==
4011 (TF_REQ_TSTMP|TF_RCVD_TSTMP))
4012 sc->sc_flags |= SCF_TIMESTAMP;
4013 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
4014 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
4015 sc->sc_requested_s_scale = tb.requested_s_scale;
4016 sc->sc_request_r_scale = 0;
4017 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
4018 TCP_MAXWIN << sc->sc_request_r_scale <
4019 so->so_rcv.sb_hiwat)
4020 sc->sc_request_r_scale++;
4021 } else {
4022 sc->sc_requested_s_scale = 15;
4023 sc->sc_request_r_scale = 15;
4024 }
4025 if ((tb.t_flags & TF_SACK_PERMIT) && tcp_do_sack)
4026 sc->sc_flags |= SCF_SACK_PERMIT;
4027
4028 /*
4029 * ECN setup packet recieved.
4030 */
4031 if ((th->th_flags & (TH_ECE|TH_CWR)) && tcp_do_ecn)
4032 sc->sc_flags |= SCF_ECN_PERMIT;
4033
4034 #ifdef TCP_SIGNATURE
4035 if (tb.t_flags & TF_SIGNATURE)
4036 sc->sc_flags |= SCF_SIGNATURE;
4037 #endif
4038 sc->sc_tp = tp;
4039 if (syn_cache_respond(sc, m) == 0) {
4040 syn_cache_insert(sc, tp);
4041 tcpstat.tcps_sndacks++;
4042 tcpstat.tcps_sndtotal++;
4043 } else {
4044 s = splsoftnet();
4045 SYN_CACHE_PUT(sc);
4046 splx(s);
4047 tcpstat.tcps_sc_dropped++;
4048 }
4049 return (1);
4050 }
4051
4052 int
4053 syn_cache_respond(struct syn_cache *sc, struct mbuf *m)
4054 {
4055 struct route *ro;
4056 u_int8_t *optp;
4057 int optlen, error;
4058 u_int16_t tlen;
4059 struct ip *ip = NULL;
4060 #ifdef INET6
4061 struct ip6_hdr *ip6 = NULL;
4062 #endif
4063 struct tcpcb *tp = NULL;
4064 struct tcphdr *th;
4065 u_int hlen;
4066 struct socket *so;
4067
4068 ro = &sc->sc_route;
4069 switch (sc->sc_src.sa.sa_family) {
4070 case AF_INET:
4071 hlen = sizeof(struct ip);
4072 break;
4073 #ifdef INET6
4074 case AF_INET6:
4075 hlen = sizeof(struct ip6_hdr);
4076 break;
4077 #endif
4078 default:
4079 if (m)
4080 m_freem(m);
4081 return (EAFNOSUPPORT);
4082 }
4083
4084 /* Compute the size of the TCP options. */
4085 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
4086 ((sc->sc_flags & SCF_SACK_PERMIT) ? (TCPOLEN_SACK_PERMITTED + 2) : 0) +
4087 #ifdef TCP_SIGNATURE
4088 ((sc->sc_flags & SCF_SIGNATURE) ? (TCPOLEN_SIGNATURE + 2) : 0) +
4089 #endif
4090 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
4091
4092 tlen = hlen + sizeof(struct tcphdr) + optlen;
4093
4094 /*
4095 * Create the IP+TCP header from scratch.
4096 */
4097 if (m)
4098 m_freem(m);
4099 #ifdef DIAGNOSTIC
4100 if (max_linkhdr + tlen > MCLBYTES)
4101 return (ENOBUFS);
4102 #endif
4103 MGETHDR(m, M_DONTWAIT, MT_DATA);
4104 if (m && tlen > MHLEN) {
4105 MCLGET(m, M_DONTWAIT);
4106 if ((m->m_flags & M_EXT) == 0) {
4107 m_freem(m);
4108 m = NULL;
4109 }
4110 }
4111 if (m == NULL)
4112 return (ENOBUFS);
4113 MCLAIM(m, &tcp_tx_mowner);
4114
4115 /* Fixup the mbuf. */
4116 m->m_data += max_linkhdr;
4117 m->m_len = m->m_pkthdr.len = tlen;
4118 if (sc->sc_tp) {
4119 tp = sc->sc_tp;
4120 if (tp->t_inpcb)
4121 so = tp->t_inpcb->inp_socket;
4122 #ifdef INET6
4123 else if (tp->t_in6pcb)
4124 so = tp->t_in6pcb->in6p_socket;
4125 #endif
4126 else
4127 so = NULL;
4128 } else
4129 so = NULL;
4130 m->m_pkthdr.rcvif = NULL;
4131 memset(mtod(m, u_char *), 0, tlen);
4132
4133 switch (sc->sc_src.sa.sa_family) {
4134 case AF_INET:
4135 ip = mtod(m, struct ip *);
4136 ip->ip_v = 4;
4137 ip->ip_dst = sc->sc_src.sin.sin_addr;
4138 ip->ip_src = sc->sc_dst.sin.sin_addr;
4139 ip->ip_p = IPPROTO_TCP;
4140 th = (struct tcphdr *)(ip + 1);
4141 th->th_dport = sc->sc_src.sin.sin_port;
4142 th->th_sport = sc->sc_dst.sin.sin_port;
4143 break;
4144 #ifdef INET6
4145 case AF_INET6:
4146 ip6 = mtod(m, struct ip6_hdr *);
4147 ip6->ip6_vfc = IPV6_VERSION;
4148 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
4149 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
4150 ip6->ip6_nxt = IPPROTO_TCP;
4151 /* ip6_plen will be updated in ip6_output() */
4152 th = (struct tcphdr *)(ip6 + 1);
4153 th->th_dport = sc->sc_src.sin6.sin6_port;
4154 th->th_sport = sc->sc_dst.sin6.sin6_port;
4155 break;
4156 #endif
4157 default:
4158 th = NULL;
4159 }
4160
4161 th->th_seq = htonl(sc->sc_iss);
4162 th->th_ack = htonl(sc->sc_irs + 1);
4163 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
4164 th->th_flags = TH_SYN|TH_ACK;
4165 th->th_win = htons(sc->sc_win);
4166 /* th_sum already 0 */
4167 /* th_urp already 0 */
4168
4169 /* Tack on the TCP options. */
4170 optp = (u_int8_t *)(th + 1);
4171 *optp++ = TCPOPT_MAXSEG;
4172 *optp++ = 4;
4173 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
4174 *optp++ = sc->sc_ourmaxseg & 0xff;
4175
4176 if (sc->sc_request_r_scale != 15) {
4177 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
4178 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
4179 sc->sc_request_r_scale);
4180 optp += 4;
4181 }
4182
4183 if (sc->sc_flags & SCF_TIMESTAMP) {
4184 u_int32_t *lp = (u_int32_t *)(optp);
4185 /* Form timestamp option as shown in appendix A of RFC 1323. */
4186 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
4187 *lp++ = htonl(SYN_CACHE_TIMESTAMP(sc));
4188 *lp = htonl(sc->sc_timestamp);
4189 optp += TCPOLEN_TSTAMP_APPA;
4190 }
4191
4192 if (sc->sc_flags & SCF_SACK_PERMIT) {
4193 u_int8_t *p = optp;
4194
4195 /* Let the peer know that we will SACK. */
4196 p[0] = TCPOPT_SACK_PERMITTED;
4197 p[1] = 2;
4198 p[2] = TCPOPT_NOP;
4199 p[3] = TCPOPT_NOP;
4200 optp += 4;
4201 }
4202
4203 /*
4204 * Send ECN SYN-ACK setup packet.
4205 * Routes can be asymetric, so, even if we receive a packet
4206 * with ECE and CWR set, we must not assume no one will block
4207 * the ECE packet we are about to send.
4208 */
4209 if ((sc->sc_flags & SCF_ECN_PERMIT) && tp &&
4210 SEQ_GEQ(tp->snd_nxt, tp->snd_max)) {
4211 th->th_flags |= TH_ECE;
4212 tcpstat.tcps_ecn_shs++;
4213
4214 /*
4215 * draft-ietf-tcpm-ecnsyn-00.txt
4216 *
4217 * "[...] a TCP node MAY respond to an ECN-setup
4218 * SYN packet by setting ECT in the responding
4219 * ECN-setup SYN/ACK packet, indicating to routers
4220 * that the SYN/ACK packet is ECN-Capable.
4221 * This allows a congested router along the path
4222 * to mark the packet instead of dropping the
4223 * packet as an indication of congestion."
4224 *
4225 * "[...] There can be a great benefit in setting
4226 * an ECN-capable codepoint in SYN/ACK packets [...]
4227 * Congestion is most likely to occur in
4228 * the server-to-client direction. As a result,
4229 * setting an ECN-capable codepoint in SYN/ACK
4230 * packets can reduce the occurence of three-second
4231 * retransmit timeouts resulting from the drop
4232 * of SYN/ACK packets."
4233 *
4234 * Page 4 and 6, January 2006.
4235 */
4236
4237 switch (sc->sc_src.sa.sa_family) {
4238 #ifdef INET
4239 case AF_INET:
4240 ip->ip_tos |= IPTOS_ECN_ECT0;
4241 break;
4242 #endif
4243 #ifdef INET6
4244 case AF_INET6:
4245 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
4246 break;
4247 #endif
4248 }
4249 tcpstat.tcps_ecn_ect++;
4250 }
4251
4252 #ifdef TCP_SIGNATURE
4253 if (sc->sc_flags & SCF_SIGNATURE) {
4254 struct secasvar *sav;
4255 u_int8_t *sigp;
4256
4257 sav = tcp_signature_getsav(m, th);
4258
4259 if (sav == NULL) {
4260 if (m)
4261 m_freem(m);
4262 return (EPERM);
4263 }
4264
4265 *optp++ = TCPOPT_SIGNATURE;
4266 *optp++ = TCPOLEN_SIGNATURE;
4267 sigp = optp;
4268 bzero(optp, TCP_SIGLEN);
4269 optp += TCP_SIGLEN;
4270 *optp++ = TCPOPT_NOP;
4271 *optp++ = TCPOPT_EOL;
4272
4273 (void)tcp_signature(m, th, hlen, sav, sigp);
4274
4275 key_sa_recordxfer(sav, m);
4276 #ifdef FAST_IPSEC
4277 KEY_FREESAV(&sav);
4278 #else
4279 key_freesav(sav);
4280 #endif
4281 }
4282 #endif
4283
4284 /* Compute the packet's checksum. */
4285 switch (sc->sc_src.sa.sa_family) {
4286 case AF_INET:
4287 ip->ip_len = htons(tlen - hlen);
4288 th->th_sum = 0;
4289 th->th_sum = in4_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4290 break;
4291 #ifdef INET6
4292 case AF_INET6:
4293 ip6->ip6_plen = htons(tlen - hlen);
4294 th->th_sum = 0;
4295 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4296 break;
4297 #endif
4298 }
4299
4300 /*
4301 * Fill in some straggling IP bits. Note the stack expects
4302 * ip_len to be in host order, for convenience.
4303 */
4304 switch (sc->sc_src.sa.sa_family) {
4305 #ifdef INET
4306 case AF_INET:
4307 ip->ip_len = htons(tlen);
4308 ip->ip_ttl = ip_defttl;
4309 /* XXX tos? */
4310 break;
4311 #endif
4312 #ifdef INET6
4313 case AF_INET6:
4314 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
4315 ip6->ip6_vfc |= IPV6_VERSION;
4316 ip6->ip6_plen = htons(tlen - hlen);
4317 /* ip6_hlim will be initialized afterwards */
4318 /* XXX flowlabel? */
4319 break;
4320 #endif
4321 }
4322
4323 /* XXX use IPsec policy on listening socket, on SYN ACK */
4324 tp = sc->sc_tp;
4325
4326 switch (sc->sc_src.sa.sa_family) {
4327 #ifdef INET
4328 case AF_INET:
4329 error = ip_output(m, sc->sc_ipopts, ro,
4330 (ip_mtudisc ? IP_MTUDISC : 0),
4331 (struct ip_moptions *)NULL, so);
4332 break;
4333 #endif
4334 #ifdef INET6
4335 case AF_INET6:
4336 ip6->ip6_hlim = in6_selecthlim(NULL,
4337 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
4338
4339 error = ip6_output(m, NULL /*XXX*/, ro, 0, NULL, so, NULL);
4340 break;
4341 #endif
4342 default:
4343 error = EAFNOSUPPORT;
4344 break;
4345 }
4346 return (error);
4347 }
4348