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