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