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