1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University 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 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 67 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $ 68 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.51 2008/09/29 20:52:23 dillon Exp $ 69 */ 70 71 #include "opt_ipsec.h" 72 #include "opt_inet6.h" 73 #include "opt_tcpdebug.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/malloc.h> 79 #include <sys/sysctl.h> 80 #include <sys/globaldata.h> 81 #include <sys/thread.h> 82 83 #include <sys/mbuf.h> 84 #ifdef INET6 85 #include <sys/domain.h> 86 #endif /* INET6 */ 87 #include <sys/socket.h> 88 #include <sys/socketvar.h> 89 #include <sys/protosw.h> 90 91 #include <sys/thread2.h> 92 #include <sys/msgport2.h> 93 94 #include <net/if.h> 95 #include <net/netisr.h> 96 #include <net/route.h> 97 98 #include <net/netmsg2.h> 99 100 #include <netinet/in.h> 101 #include <netinet/in_systm.h> 102 #ifdef INET6 103 #include <netinet/ip6.h> 104 #endif 105 #include <netinet/in_pcb.h> 106 #ifdef INET6 107 #include <netinet6/in6_pcb.h> 108 #endif 109 #include <netinet/in_var.h> 110 #include <netinet/ip_var.h> 111 #ifdef INET6 112 #include <netinet6/ip6_var.h> 113 #endif 114 #include <netinet/tcp.h> 115 #include <netinet/tcp_fsm.h> 116 #include <netinet/tcp_seq.h> 117 #include <netinet/tcp_timer.h> 118 #include <netinet/tcp_var.h> 119 #include <netinet/tcpip.h> 120 #ifdef TCPDEBUG 121 #include <netinet/tcp_debug.h> 122 #endif 123 124 #ifdef IPSEC 125 #include <netinet6/ipsec.h> 126 #endif /*IPSEC*/ 127 128 /* 129 * TCP protocol interface to socket abstraction. 130 */ 131 extern char *tcpstates[]; /* XXX ??? */ 132 133 static int tcp_attach (struct socket *, struct pru_attach_info *); 134 static int tcp_connect (struct tcpcb *, struct sockaddr *, 135 struct thread *); 136 #ifdef INET6 137 static int tcp6_connect (struct tcpcb *, struct sockaddr *, 138 struct thread *); 139 #endif /* INET6 */ 140 static struct tcpcb * 141 tcp_disconnect (struct tcpcb *); 142 static struct tcpcb * 143 tcp_usrclosed (struct tcpcb *); 144 145 #ifdef TCPDEBUG 146 #define TCPDEBUG0 int ostate = 0 147 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 148 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 149 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 150 #else 151 #define TCPDEBUG0 152 #define TCPDEBUG1() 153 #define TCPDEBUG2(req) 154 #endif 155 156 /* 157 * TCP attaches to socket via pru_attach(), reserving space, 158 * and an internet control block. 159 */ 160 static int 161 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai) 162 { 163 int error; 164 struct inpcb *inp; 165 struct tcpcb *tp = 0; 166 TCPDEBUG0; 167 168 crit_enter(); 169 inp = so->so_pcb; 170 TCPDEBUG1(); 171 if (inp) { 172 error = EISCONN; 173 goto out; 174 } 175 176 error = tcp_attach(so, ai); 177 if (error) 178 goto out; 179 180 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 181 so->so_linger = TCP_LINGERTIME; 182 tp = sototcpcb(so); 183 out: 184 TCPDEBUG2(PRU_ATTACH); 185 crit_exit(); 186 return error; 187 } 188 189 /* 190 * pru_detach() detaches the TCP protocol from the socket. 191 * If the protocol state is non-embryonic, then can't 192 * do this directly: have to initiate a pru_disconnect(), 193 * which may finish later; embryonic TCB's can just 194 * be discarded here. 195 */ 196 static int 197 tcp_usr_detach(struct socket *so) 198 { 199 int error = 0; 200 struct inpcb *inp; 201 struct tcpcb *tp; 202 TCPDEBUG0; 203 204 crit_enter(); 205 inp = so->so_pcb; 206 207 /* 208 * If the inp is already detached it may have been due to an async 209 * close. Just return as if no error occured. 210 */ 211 if (inp == NULL) { 212 crit_exit(); 213 return 0; 214 } 215 216 /* 217 * It's possible for the tcpcb (tp) to disconnect from the inp due 218 * to tcp_drop()->tcp_close() being called. This may occur *after* 219 * the detach message has been queued so we may find a NULL tp here. 220 */ 221 if ((tp = intotcpcb(inp)) != NULL) { 222 TCPDEBUG1(); 223 tp = tcp_disconnect(tp); 224 TCPDEBUG2(PRU_DETACH); 225 } 226 crit_exit(); 227 return error; 228 } 229 230 /* 231 * Note: ignore_error is non-zero for certain disconnection races 232 * which we want to silently allow, otherwise close() may return 233 * an unexpected error. 234 */ 235 #define COMMON_START(so, inp, ignore_error) \ 236 TCPDEBUG0; \ 237 \ 238 crit_enter(); \ 239 inp = so->so_pcb; \ 240 do { \ 241 if (inp == NULL) { \ 242 crit_exit(); \ 243 return (ignore_error ? 0 : EINVAL); \ 244 } \ 245 tp = intotcpcb(inp); \ 246 TCPDEBUG1(); \ 247 } while(0) 248 249 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out 250 251 252 /* 253 * Give the socket an address. 254 */ 255 static int 256 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 257 { 258 int error = 0; 259 struct inpcb *inp; 260 struct tcpcb *tp; 261 struct sockaddr_in *sinp; 262 263 COMMON_START(so, inp, 0); 264 265 /* 266 * Must check for multicast addresses and disallow binding 267 * to them. 268 */ 269 sinp = (struct sockaddr_in *)nam; 270 if (sinp->sin_family == AF_INET && 271 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 272 error = EAFNOSUPPORT; 273 goto out; 274 } 275 error = in_pcbbind(inp, nam, td); 276 if (error) 277 goto out; 278 COMMON_END(PRU_BIND); 279 280 } 281 282 #ifdef INET6 283 static int 284 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 285 { 286 int error = 0; 287 struct inpcb *inp; 288 struct tcpcb *tp; 289 struct sockaddr_in6 *sin6p; 290 291 COMMON_START(so, inp, 0); 292 293 /* 294 * Must check for multicast addresses and disallow binding 295 * to them. 296 */ 297 sin6p = (struct sockaddr_in6 *)nam; 298 if (sin6p->sin6_family == AF_INET6 && 299 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 300 error = EAFNOSUPPORT; 301 goto out; 302 } 303 inp->inp_vflag &= ~INP_IPV4; 304 inp->inp_vflag |= INP_IPV6; 305 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 306 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 307 inp->inp_vflag |= INP_IPV4; 308 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 309 struct sockaddr_in sin; 310 311 in6_sin6_2_sin(&sin, sin6p); 312 inp->inp_vflag |= INP_IPV4; 313 inp->inp_vflag &= ~INP_IPV6; 314 error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 315 goto out; 316 } 317 } 318 error = in6_pcbbind(inp, nam, td); 319 if (error) 320 goto out; 321 COMMON_END(PRU_BIND); 322 } 323 #endif /* INET6 */ 324 325 #ifdef SMP 326 struct netmsg_inswildcard { 327 struct netmsg nm_netmsg; 328 struct inpcb *nm_inp; 329 struct inpcbinfo *nm_pcbinfo; 330 }; 331 332 static void 333 in_pcbinswildcardhash_handler(struct netmsg *msg0) 334 { 335 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0; 336 337 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo); 338 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0); 339 } 340 #endif 341 342 /* 343 * Prepare to accept connections. 344 */ 345 static int 346 tcp_usr_listen(struct socket *so, struct thread *td) 347 { 348 int error = 0; 349 struct inpcb *inp; 350 struct tcpcb *tp; 351 #ifdef SMP 352 int cpu; 353 #endif 354 355 COMMON_START(so, inp, 0); 356 if (inp->inp_lport == 0) { 357 error = in_pcbbind(inp, NULL, td); 358 if (error != 0) 359 goto out; 360 } 361 362 tp->t_state = TCPS_LISTEN; 363 tp->tt_msg = NULL; /* Catch any invalid timer usage */ 364 #ifdef SMP 365 /* 366 * We have to set the flag because we can't have other cpus 367 * messing with our inp's flags. 368 */ 369 inp->inp_flags |= INP_WILDCARD_MP; 370 for (cpu = 0; cpu < ncpus2; cpu++) { 371 struct netmsg_inswildcard *msg; 372 373 if (cpu == mycpu->gd_cpuid) { 374 in_pcbinswildcardhash(inp); 375 continue; 376 } 377 378 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG, 379 M_INTWAIT); 380 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0, 381 in_pcbinswildcardhash_handler); 382 msg->nm_inp = inp; 383 msg->nm_pcbinfo = &tcbinfo[cpu]; 384 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg); 385 } 386 #else 387 in_pcbinswildcardhash(inp); 388 #endif 389 COMMON_END(PRU_LISTEN); 390 } 391 392 #ifdef INET6 393 static int 394 tcp6_usr_listen(struct socket *so, struct thread *td) 395 { 396 int error = 0; 397 struct inpcb *inp; 398 struct tcpcb *tp; 399 #ifdef SMP 400 int cpu; 401 #endif 402 403 COMMON_START(so, inp, 0); 404 if (inp->inp_lport == 0) { 405 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) 406 inp->inp_vflag |= INP_IPV4; 407 else 408 inp->inp_vflag &= ~INP_IPV4; 409 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 410 } 411 if (error == 0) 412 tp->t_state = TCPS_LISTEN; 413 #ifdef SMP 414 /* 415 * We have to set the flag because we can't have other cpus 416 * messing with our inp's flags. 417 */ 418 inp->inp_flags |= INP_WILDCARD_MP; 419 for (cpu = 0; cpu < ncpus2; cpu++) { 420 struct netmsg_inswildcard *msg; 421 422 if (cpu == mycpu->gd_cpuid) { 423 in_pcbinswildcardhash(inp); 424 continue; 425 } 426 427 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG, 428 M_INTWAIT); 429 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 0, 430 in_pcbinswildcardhash_handler); 431 msg->nm_inp = inp; 432 msg->nm_pcbinfo = &tcbinfo[cpu]; 433 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg); 434 } 435 #else 436 in_pcbinswildcardhash(inp); 437 #endif 438 COMMON_END(PRU_LISTEN); 439 } 440 #endif /* INET6 */ 441 442 #ifdef SMP 443 static void 444 tcp_output_dispatch(struct netmsg *nmsg) 445 { 446 struct lwkt_msg *msg = &nmsg->nm_lmsg; 447 struct tcpcb *tp = msg->u.ms_resultp; 448 int error; 449 450 error = tcp_output(tp); 451 lwkt_replymsg(msg, error); 452 } 453 #endif 454 455 /* 456 * Initiate connection to peer. 457 * Create a template for use in transmissions on this connection. 458 * Enter SYN_SENT state, and mark socket as connecting. 459 * Start keep-alive timer, and seed output sequence space. 460 * Send initial segment on connection. 461 */ 462 static int 463 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 464 { 465 int error = 0; 466 struct inpcb *inp; 467 struct tcpcb *tp; 468 struct sockaddr_in *sinp; 469 #ifdef SMP 470 lwkt_port_t port; 471 #endif 472 473 COMMON_START(so, inp, 0); 474 475 /* 476 * Must disallow TCP ``connections'' to multicast addresses. 477 */ 478 sinp = (struct sockaddr_in *)nam; 479 if (sinp->sin_family == AF_INET 480 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 481 error = EAFNOSUPPORT; 482 goto out; 483 } 484 485 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) { 486 error = EAFNOSUPPORT; /* IPv6 only jail */ 487 goto out; 488 } 489 490 if ((error = tcp_connect(tp, nam, td)) != 0) 491 goto out; 492 493 #ifdef SMP 494 port = tcp_addrport(inp->inp_faddr.s_addr, inp->inp_fport, 495 inp->inp_laddr.s_addr, inp->inp_lport); 496 if (port != &curthread->td_msgport) { 497 struct netmsg nmsg; 498 struct lwkt_msg *msg; 499 500 netmsg_init(&nmsg, &curthread->td_msgport, 0, 501 tcp_output_dispatch); 502 msg = &nmsg.nm_lmsg; 503 msg->u.ms_resultp = tp; 504 505 error = lwkt_domsg(port, msg, 0); 506 } else 507 #endif 508 error = tcp_output(tp); 509 COMMON_END(PRU_CONNECT); 510 } 511 512 #ifdef INET6 513 static int 514 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 515 { 516 int error = 0; 517 struct inpcb *inp; 518 struct tcpcb *tp; 519 struct sockaddr_in6 *sin6p; 520 521 COMMON_START(so, inp, 0); 522 523 /* 524 * Must disallow TCP ``connections'' to multicast addresses. 525 */ 526 sin6p = (struct sockaddr_in6 *)nam; 527 if (sin6p->sin6_family == AF_INET6 528 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 529 error = EAFNOSUPPORT; 530 goto out; 531 } 532 533 if (!prison_remote_ip(td, nam)) { 534 error = EAFNOSUPPORT; /* IPv4 only jail */ 535 goto out; 536 } 537 538 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 539 struct sockaddr_in sin; 540 541 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 542 error = EINVAL; 543 goto out; 544 } 545 546 in6_sin6_2_sin(&sin, sin6p); 547 inp->inp_vflag |= INP_IPV4; 548 inp->inp_vflag &= ~INP_IPV6; 549 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 550 goto out; 551 error = tcp_output(tp); 552 goto out; 553 } 554 inp->inp_vflag &= ~INP_IPV4; 555 inp->inp_vflag |= INP_IPV6; 556 inp->inp_inc.inc_isipv6 = 1; 557 if ((error = tcp6_connect(tp, nam, td)) != 0) 558 goto out; 559 error = tcp_output(tp); 560 COMMON_END(PRU_CONNECT); 561 } 562 #endif /* INET6 */ 563 564 /* 565 * Initiate disconnect from peer. 566 * If connection never passed embryonic stage, just drop; 567 * else if don't need to let data drain, then can just drop anyways, 568 * else have to begin TCP shutdown process: mark socket disconnecting, 569 * drain unread data, state switch to reflect user close, and 570 * send segment (e.g. FIN) to peer. Socket will be really disconnected 571 * when peer sends FIN and acks ours. 572 * 573 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 574 */ 575 static int 576 tcp_usr_disconnect(struct socket *so) 577 { 578 int error = 0; 579 struct inpcb *inp; 580 struct tcpcb *tp; 581 582 COMMON_START(so, inp, 1); 583 tp = tcp_disconnect(tp); 584 COMMON_END(PRU_DISCONNECT); 585 } 586 587 /* 588 * Accept a connection. Essentially all the work is 589 * done at higher levels; just return the address 590 * of the peer, storing through addr. 591 */ 592 static int 593 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 594 { 595 int error = 0; 596 struct inpcb *inp; 597 struct tcpcb *tp = NULL; 598 TCPDEBUG0; 599 600 crit_enter(); 601 inp = so->so_pcb; 602 if (so->so_state & SS_ISDISCONNECTED) { 603 error = ECONNABORTED; 604 goto out; 605 } 606 if (inp == 0) { 607 crit_exit(); 608 return (EINVAL); 609 } 610 tp = intotcpcb(inp); 611 TCPDEBUG1(); 612 in_setpeeraddr(so, nam); 613 COMMON_END(PRU_ACCEPT); 614 } 615 616 #ifdef INET6 617 static int 618 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 619 { 620 int error = 0; 621 struct inpcb *inp; 622 struct tcpcb *tp = NULL; 623 TCPDEBUG0; 624 625 crit_enter(); 626 inp = so->so_pcb; 627 628 if (so->so_state & SS_ISDISCONNECTED) { 629 error = ECONNABORTED; 630 goto out; 631 } 632 if (inp == 0) { 633 crit_exit(); 634 return (EINVAL); 635 } 636 tp = intotcpcb(inp); 637 TCPDEBUG1(); 638 in6_mapped_peeraddr(so, nam); 639 COMMON_END(PRU_ACCEPT); 640 } 641 #endif /* INET6 */ 642 /* 643 * Mark the connection as being incapable of further output. 644 */ 645 static int 646 tcp_usr_shutdown(struct socket *so) 647 { 648 int error = 0; 649 struct inpcb *inp; 650 struct tcpcb *tp; 651 652 COMMON_START(so, inp, 0); 653 socantsendmore(so); 654 tp = tcp_usrclosed(tp); 655 if (tp) 656 error = tcp_output(tp); 657 COMMON_END(PRU_SHUTDOWN); 658 } 659 660 /* 661 * After a receive, possibly send window update to peer. 662 */ 663 static int 664 tcp_usr_rcvd(struct socket *so, int flags) 665 { 666 int error = 0; 667 struct inpcb *inp; 668 struct tcpcb *tp; 669 670 COMMON_START(so, inp, 0); 671 tcp_output(tp); 672 COMMON_END(PRU_RCVD); 673 } 674 675 /* 676 * Do a send by putting data in output queue and updating urgent 677 * marker if URG set. Possibly send more data. Unlike the other 678 * pru_*() routines, the mbuf chains are our responsibility. We 679 * must either enqueue them or free them. The other pru_* routines 680 * generally are caller-frees. 681 */ 682 static int 683 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 684 struct sockaddr *nam, struct mbuf *control, struct thread *td) 685 { 686 int error = 0; 687 struct inpcb *inp; 688 struct tcpcb *tp; 689 #ifdef INET6 690 int isipv6; 691 #endif 692 TCPDEBUG0; 693 694 crit_enter(); 695 inp = so->so_pcb; 696 697 if (inp == NULL) { 698 /* 699 * OOPS! we lost a race, the TCP session got reset after 700 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 701 * network interrupt in the non-critical section of sosend(). 702 */ 703 if (m) 704 m_freem(m); 705 if (control) 706 m_freem(control); 707 error = ECONNRESET; /* XXX EPIPE? */ 708 tp = NULL; 709 TCPDEBUG1(); 710 goto out; 711 } 712 #ifdef INET6 713 isipv6 = nam && nam->sa_family == AF_INET6; 714 #endif /* INET6 */ 715 tp = intotcpcb(inp); 716 TCPDEBUG1(); 717 if (control) { 718 /* TCP doesn't do control messages (rights, creds, etc) */ 719 if (control->m_len) { 720 m_freem(control); 721 if (m) 722 m_freem(m); 723 error = EINVAL; 724 goto out; 725 } 726 m_freem(control); /* empty control, just free it */ 727 } 728 if(!(flags & PRUS_OOB)) { 729 ssb_appendstream(&so->so_snd, m); 730 if (nam && tp->t_state < TCPS_SYN_SENT) { 731 /* 732 * Do implied connect if not yet connected, 733 * initialize window to default value, and 734 * initialize maxseg/maxopd using peer's cached 735 * MSS. 736 */ 737 #ifdef INET6 738 if (isipv6) 739 error = tcp6_connect(tp, nam, td); 740 else 741 #endif /* INET6 */ 742 error = tcp_connect(tp, nam, td); 743 if (error) 744 goto out; 745 tp->snd_wnd = TTCP_CLIENT_SND_WND; 746 tcp_mss(tp, -1); 747 } 748 749 if (flags & PRUS_EOF) { 750 /* 751 * Close the send side of the connection after 752 * the data is sent. 753 */ 754 socantsendmore(so); 755 tp = tcp_usrclosed(tp); 756 } 757 if (tp != NULL) { 758 if (flags & PRUS_MORETOCOME) 759 tp->t_flags |= TF_MORETOCOME; 760 error = tcp_output(tp); 761 if (flags & PRUS_MORETOCOME) 762 tp->t_flags &= ~TF_MORETOCOME; 763 } 764 } else { 765 if (ssb_space(&so->so_snd) < -512) { 766 m_freem(m); 767 error = ENOBUFS; 768 goto out; 769 } 770 /* 771 * According to RFC961 (Assigned Protocols), 772 * the urgent pointer points to the last octet 773 * of urgent data. We continue, however, 774 * to consider it to indicate the first octet 775 * of data past the urgent section. 776 * Otherwise, snd_up should be one lower. 777 */ 778 ssb_appendstream(&so->so_snd, m); 779 if (nam && tp->t_state < TCPS_SYN_SENT) { 780 /* 781 * Do implied connect if not yet connected, 782 * initialize window to default value, and 783 * initialize maxseg/maxopd using peer's cached 784 * MSS. 785 */ 786 #ifdef INET6 787 if (isipv6) 788 error = tcp6_connect(tp, nam, td); 789 else 790 #endif /* INET6 */ 791 error = tcp_connect(tp, nam, td); 792 if (error) 793 goto out; 794 tp->snd_wnd = TTCP_CLIENT_SND_WND; 795 tcp_mss(tp, -1); 796 } 797 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc; 798 tp->t_flags |= TF_FORCE; 799 error = tcp_output(tp); 800 tp->t_flags &= ~TF_FORCE; 801 } 802 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 803 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 804 } 805 806 /* 807 * Abort the TCP. 808 */ 809 static int 810 tcp_usr_abort(struct socket *so) 811 { 812 int error = 0; 813 struct inpcb *inp; 814 struct tcpcb *tp; 815 816 COMMON_START(so, inp, 1); 817 tp = tcp_drop(tp, ECONNABORTED); 818 COMMON_END(PRU_ABORT); 819 } 820 821 /* 822 * Receive out-of-band data. 823 */ 824 static int 825 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 826 { 827 int error = 0; 828 struct inpcb *inp; 829 struct tcpcb *tp; 830 831 COMMON_START(so, inp, 0); 832 if ((so->so_oobmark == 0 && 833 (so->so_state & SS_RCVATMARK) == 0) || 834 so->so_options & SO_OOBINLINE || 835 tp->t_oobflags & TCPOOB_HADDATA) { 836 error = EINVAL; 837 goto out; 838 } 839 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 840 error = EWOULDBLOCK; 841 goto out; 842 } 843 m->m_len = 1; 844 *mtod(m, caddr_t) = tp->t_iobc; 845 if ((flags & MSG_PEEK) == 0) 846 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 847 COMMON_END(PRU_RCVOOB); 848 } 849 850 /* xxx - should be const */ 851 struct pr_usrreqs tcp_usrreqs = { 852 .pru_abort = tcp_usr_abort, 853 .pru_accept = tcp_usr_accept, 854 .pru_attach = tcp_usr_attach, 855 .pru_bind = tcp_usr_bind, 856 .pru_connect = tcp_usr_connect, 857 .pru_connect2 = pru_connect2_notsupp, 858 .pru_control = in_control, 859 .pru_detach = tcp_usr_detach, 860 .pru_disconnect = tcp_usr_disconnect, 861 .pru_listen = tcp_usr_listen, 862 .pru_peeraddr = in_setpeeraddr, 863 .pru_rcvd = tcp_usr_rcvd, 864 .pru_rcvoob = tcp_usr_rcvoob, 865 .pru_send = tcp_usr_send, 866 .pru_sense = pru_sense_null, 867 .pru_shutdown = tcp_usr_shutdown, 868 .pru_sockaddr = in_setsockaddr, 869 .pru_sosend = sosend, 870 .pru_soreceive = soreceive, 871 .pru_sopoll = sopoll 872 }; 873 874 #ifdef INET6 875 struct pr_usrreqs tcp6_usrreqs = { 876 .pru_abort = tcp_usr_abort, 877 .pru_accept = tcp6_usr_accept, 878 .pru_attach = tcp_usr_attach, 879 .pru_bind = tcp6_usr_bind, 880 .pru_connect = tcp6_usr_connect, 881 .pru_connect2 = pru_connect2_notsupp, 882 .pru_control = in6_control, 883 .pru_detach = tcp_usr_detach, 884 .pru_disconnect = tcp_usr_disconnect, 885 .pru_listen = tcp6_usr_listen, 886 .pru_peeraddr = in6_mapped_peeraddr, 887 .pru_rcvd = tcp_usr_rcvd, 888 .pru_rcvoob = tcp_usr_rcvoob, 889 .pru_send = tcp_usr_send, 890 .pru_sense = pru_sense_null, 891 .pru_shutdown = tcp_usr_shutdown, 892 .pru_sockaddr = in6_mapped_sockaddr, 893 .pru_sosend = sosend, 894 .pru_soreceive = soreceive, 895 .pru_sopoll = sopoll 896 }; 897 #endif /* INET6 */ 898 899 static int 900 tcp_connect_oncpu(struct tcpcb *tp, struct sockaddr_in *sin, 901 struct sockaddr_in *if_sin) 902 { 903 struct inpcb *inp = tp->t_inpcb, *oinp; 904 struct socket *so = inp->inp_socket; 905 struct tcpcb *otp; 906 struct rmxp_tao *taop; 907 struct rmxp_tao tao_noncached; 908 909 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid], 910 sin->sin_addr, sin->sin_port, 911 inp->inp_laddr.s_addr != INADDR_ANY ? 912 inp->inp_laddr : if_sin->sin_addr, 913 inp->inp_lport, 0, NULL); 914 if (oinp != NULL) { 915 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 916 otp->t_state == TCPS_TIME_WAIT && 917 (ticks - otp->t_starttime) < tcp_msl && 918 (otp->t_flags & TF_RCVD_CC)) 919 tcp_close(otp); 920 else 921 return (EADDRINUSE); 922 } 923 if (inp->inp_laddr.s_addr == INADDR_ANY) 924 inp->inp_laddr = if_sin->sin_addr; 925 inp->inp_faddr = sin->sin_addr; 926 inp->inp_fport = sin->sin_port; 927 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid]; 928 in_pcbinsconnhash(inp); 929 930 tcp_create_timermsg(tp); 931 932 /* Compute window scaling to request. */ 933 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 934 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) 935 tp->request_r_scale++; 936 937 soisconnecting(so); 938 tcpstat.tcps_connattempt++; 939 tp->t_state = TCPS_SYN_SENT; 940 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 941 tp->iss = tcp_new_isn(tp); 942 tcp_sendseqinit(tp); 943 944 /* 945 * Generate a CC value for this connection and 946 * check whether CC or CCnew should be used. 947 */ 948 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 949 taop = &tao_noncached; 950 bzero(taop, sizeof *taop); 951 } 952 953 tp->cc_send = CC_INC(tcp_ccgen); 954 if (taop->tao_ccsent != 0 && 955 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 956 taop->tao_ccsent = tp->cc_send; 957 } else { 958 taop->tao_ccsent = 0; 959 tp->t_flags |= TF_SENDCCNEW; 960 } 961 962 return (0); 963 } 964 965 #ifdef SMP 966 967 struct netmsg_tcp_connect { 968 struct netmsg nm_netmsg; 969 struct tcpcb *nm_tp; 970 struct sockaddr_in *nm_sin; 971 struct sockaddr_in *nm_ifsin; 972 }; 973 974 static void 975 tcp_connect_handler(netmsg_t netmsg) 976 { 977 struct netmsg_tcp_connect *msg = (void *)netmsg; 978 int error; 979 980 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_sin, msg->nm_ifsin); 981 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error); 982 } 983 984 #endif 985 986 /* 987 * Common subroutine to open a TCP connection to remote host specified 988 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 989 * port number if needed. Call in_pcbladdr to do the routing and to choose 990 * a local host address (interface). If there is an existing incarnation 991 * of the same connection in TIME-WAIT state and if the remote host was 992 * sending CC options and if the connection duration was < MSL, then 993 * truncate the previous TIME-WAIT state and proceed. 994 * Initialize connection parameters and enter SYN-SENT state. 995 */ 996 static int 997 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 998 { 999 struct inpcb *inp = tp->t_inpcb; 1000 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 1001 struct sockaddr_in *if_sin; 1002 int error; 1003 #ifdef SMP 1004 lwkt_port_t port; 1005 #endif 1006 1007 if (inp->inp_lport == 0) { 1008 error = in_pcbbind(inp, (struct sockaddr *)NULL, td); 1009 if (error) 1010 return (error); 1011 } 1012 1013 /* 1014 * Cannot simply call in_pcbconnect, because there might be an 1015 * earlier incarnation of this same connection still in 1016 * TIME_WAIT state, creating an ADDRINUSE error. 1017 */ 1018 error = in_pcbladdr(inp, nam, &if_sin, td); 1019 if (error) 1020 return (error); 1021 1022 #ifdef SMP 1023 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port, 1024 inp->inp_laddr.s_addr ? 1025 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr, 1026 inp->inp_lport); 1027 1028 if (port != &curthread->td_msgport) { 1029 struct netmsg_tcp_connect msg; 1030 struct route *ro = &inp->inp_route; 1031 1032 /* 1033 * in_pcbladdr() may have allocated a route entry for us 1034 * on the current CPU, but we need a route entry on the 1035 * target CPU, so free it here. 1036 */ 1037 if (ro->ro_rt != NULL) 1038 RTFREE(ro->ro_rt); 1039 bzero(ro, sizeof(*ro)); 1040 1041 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 1042 tcp_connect_handler); 1043 msg.nm_tp = tp; 1044 msg.nm_sin = sin; 1045 msg.nm_ifsin = if_sin; 1046 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 1047 } else 1048 #endif 1049 error = tcp_connect_oncpu(tp, sin, if_sin); 1050 1051 return (error); 1052 } 1053 1054 #ifdef INET6 1055 static int 1056 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1057 { 1058 struct inpcb *inp = tp->t_inpcb, *oinp; 1059 struct socket *so = inp->inp_socket; 1060 struct tcpcb *otp; 1061 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1062 struct in6_addr *addr6; 1063 struct rmxp_tao *taop; 1064 struct rmxp_tao tao_noncached; 1065 int error; 1066 1067 if (inp->inp_lport == 0) { 1068 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 1069 if (error) 1070 return error; 1071 } 1072 1073 /* 1074 * Cannot simply call in_pcbconnect, because there might be an 1075 * earlier incarnation of this same connection still in 1076 * TIME_WAIT state, creating an ADDRINUSE error. 1077 */ 1078 error = in6_pcbladdr(inp, nam, &addr6, td); 1079 if (error) 1080 return error; 1081 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo, 1082 &sin6->sin6_addr, sin6->sin6_port, 1083 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ? 1084 addr6 : &inp->in6p_laddr, 1085 inp->inp_lport, 0, NULL); 1086 if (oinp) { 1087 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 1088 otp->t_state == TCPS_TIME_WAIT && 1089 (ticks - otp->t_starttime) < tcp_msl && 1090 (otp->t_flags & TF_RCVD_CC)) 1091 otp = tcp_close(otp); 1092 else 1093 return (EADDRINUSE); 1094 } 1095 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1096 inp->in6p_laddr = *addr6; 1097 inp->in6p_faddr = sin6->sin6_addr; 1098 inp->inp_fport = sin6->sin6_port; 1099 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0) 1100 inp->in6p_flowinfo = sin6->sin6_flowinfo; 1101 in_pcbinsconnhash(inp); 1102 1103 /* Compute window scaling to request. */ 1104 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1105 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) 1106 tp->request_r_scale++; 1107 1108 soisconnecting(so); 1109 tcpstat.tcps_connattempt++; 1110 tp->t_state = TCPS_SYN_SENT; 1111 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1112 tp->iss = tcp_new_isn(tp); 1113 tcp_sendseqinit(tp); 1114 1115 /* 1116 * Generate a CC value for this connection and 1117 * check whether CC or CCnew should be used. 1118 */ 1119 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 1120 taop = &tao_noncached; 1121 bzero(taop, sizeof *taop); 1122 } 1123 1124 tp->cc_send = CC_INC(tcp_ccgen); 1125 if (taop->tao_ccsent != 0 && 1126 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 1127 taop->tao_ccsent = tp->cc_send; 1128 } else { 1129 taop->tao_ccsent = 0; 1130 tp->t_flags |= TF_SENDCCNEW; 1131 } 1132 1133 return (0); 1134 } 1135 #endif /* INET6 */ 1136 1137 /* 1138 * The new sockopt interface makes it possible for us to block in the 1139 * copyin/out step (if we take a page fault). Taking a page fault while 1140 * in a critical section is probably a Bad Thing. (Since sockets and pcbs 1141 * both now use TSM, there probably isn't any need for this function to 1142 * run in a critical section any more. This needs more examination.) 1143 */ 1144 int 1145 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1146 { 1147 int error, opt, optval; 1148 struct inpcb *inp; 1149 struct tcpcb *tp; 1150 1151 error = 0; 1152 crit_enter(); /* XXX */ 1153 inp = so->so_pcb; 1154 if (inp == NULL) { 1155 crit_exit(); 1156 return (ECONNRESET); 1157 } 1158 if (sopt->sopt_level != IPPROTO_TCP) { 1159 #ifdef INET6 1160 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1161 error = ip6_ctloutput(so, sopt); 1162 else 1163 #endif /* INET6 */ 1164 error = ip_ctloutput(so, sopt); 1165 crit_exit(); 1166 return (error); 1167 } 1168 tp = intotcpcb(inp); 1169 1170 switch (sopt->sopt_dir) { 1171 case SOPT_SET: 1172 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1173 sizeof optval); 1174 if (error) 1175 break; 1176 switch (sopt->sopt_name) { 1177 case TCP_NODELAY: 1178 case TCP_NOOPT: 1179 switch (sopt->sopt_name) { 1180 case TCP_NODELAY: 1181 opt = TF_NODELAY; 1182 break; 1183 case TCP_NOOPT: 1184 opt = TF_NOOPT; 1185 break; 1186 default: 1187 opt = 0; /* dead code to fool gcc */ 1188 break; 1189 } 1190 1191 if (optval) 1192 tp->t_flags |= opt; 1193 else 1194 tp->t_flags &= ~opt; 1195 break; 1196 1197 case TCP_NOPUSH: 1198 if (optval) 1199 tp->t_flags |= TF_NOPUSH; 1200 else { 1201 tp->t_flags &= ~TF_NOPUSH; 1202 error = tcp_output(tp); 1203 } 1204 break; 1205 1206 case TCP_MAXSEG: 1207 if (optval > 0 && optval <= tp->t_maxseg) 1208 tp->t_maxseg = optval; 1209 else 1210 error = EINVAL; 1211 break; 1212 1213 default: 1214 error = ENOPROTOOPT; 1215 break; 1216 } 1217 break; 1218 1219 case SOPT_GET: 1220 switch (sopt->sopt_name) { 1221 case TCP_NODELAY: 1222 optval = tp->t_flags & TF_NODELAY; 1223 break; 1224 case TCP_MAXSEG: 1225 optval = tp->t_maxseg; 1226 break; 1227 case TCP_NOOPT: 1228 optval = tp->t_flags & TF_NOOPT; 1229 break; 1230 case TCP_NOPUSH: 1231 optval = tp->t_flags & TF_NOPUSH; 1232 break; 1233 default: 1234 error = ENOPROTOOPT; 1235 break; 1236 } 1237 if (error == 0) 1238 soopt_from_kbuf(sopt, &optval, sizeof optval); 1239 break; 1240 } 1241 crit_exit(); 1242 return (error); 1243 } 1244 1245 /* 1246 * tcp_sendspace and tcp_recvspace are the default send and receive window 1247 * sizes, respectively. These are obsolescent (this information should 1248 * be set by the route). 1249 * 1250 * Use a default that does not require tcp window scaling to be turned 1251 * on. Individual programs or the administrator can increase the default. 1252 */ 1253 u_long tcp_sendspace = 57344; /* largest multiple of PAGE_SIZE < 64k */ 1254 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1255 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1256 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */ 1257 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1258 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1259 1260 /* 1261 * Attach TCP protocol to socket, allocating 1262 * internet protocol control block, tcp control block, 1263 * bufer space, and entering LISTEN state if to accept connections. 1264 */ 1265 static int 1266 tcp_attach(struct socket *so, struct pru_attach_info *ai) 1267 { 1268 struct tcpcb *tp; 1269 struct inpcb *inp; 1270 int error; 1271 int cpu; 1272 #ifdef INET6 1273 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1274 #endif 1275 1276 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) { 1277 error = soreserve(so, tcp_sendspace, tcp_recvspace, 1278 ai->sb_rlimit); 1279 if (error) 1280 return (error); 1281 } 1282 cpu = mycpu->gd_cpuid; 1283 error = in_pcballoc(so, &tcbinfo[cpu]); 1284 if (error) 1285 return (error); 1286 inp = so->so_pcb; 1287 #ifdef INET6 1288 if (isipv6) { 1289 inp->inp_vflag |= INP_IPV6; 1290 inp->in6p_hops = -1; /* use kernel default */ 1291 } 1292 else 1293 #endif 1294 inp->inp_vflag |= INP_IPV4; 1295 tp = tcp_newtcpcb(inp); 1296 if (tp == 0) { 1297 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1298 1299 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1300 #ifdef INET6 1301 if (isipv6) 1302 in6_pcbdetach(inp); 1303 else 1304 #endif 1305 in_pcbdetach(inp); 1306 so->so_state |= nofd; 1307 return (ENOBUFS); 1308 } 1309 tp->t_state = TCPS_CLOSED; 1310 return (0); 1311 } 1312 1313 /* 1314 * Initiate (or continue) disconnect. 1315 * If embryonic state, just send reset (once). 1316 * If in ``let data drain'' option and linger null, just drop. 1317 * Otherwise (hard), mark socket disconnecting and drop 1318 * current input data; switch states based on user close, and 1319 * send segment to peer (with FIN). 1320 */ 1321 static struct tcpcb * 1322 tcp_disconnect(struct tcpcb *tp) 1323 { 1324 struct socket *so = tp->t_inpcb->inp_socket; 1325 1326 if (tp->t_state < TCPS_ESTABLISHED) 1327 tp = tcp_close(tp); 1328 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1329 tp = tcp_drop(tp, 0); 1330 else { 1331 soisdisconnecting(so); 1332 sbflush(&so->so_rcv.sb); 1333 tp = tcp_usrclosed(tp); 1334 if (tp) 1335 tcp_output(tp); 1336 } 1337 return (tp); 1338 } 1339 1340 /* 1341 * User issued close, and wish to trail through shutdown states: 1342 * if never received SYN, just forget it. If got a SYN from peer, 1343 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1344 * If already got a FIN from peer, then almost done; go to LAST_ACK 1345 * state. In all other cases, have already sent FIN to peer (e.g. 1346 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1347 * for peer to send FIN or not respond to keep-alives, etc. 1348 * We can let the user exit from the close as soon as the FIN is acked. 1349 */ 1350 static struct tcpcb * 1351 tcp_usrclosed(struct tcpcb *tp) 1352 { 1353 1354 switch (tp->t_state) { 1355 1356 case TCPS_CLOSED: 1357 case TCPS_LISTEN: 1358 tp->t_state = TCPS_CLOSED; 1359 tp = tcp_close(tp); 1360 break; 1361 1362 case TCPS_SYN_SENT: 1363 case TCPS_SYN_RECEIVED: 1364 tp->t_flags |= TF_NEEDFIN; 1365 break; 1366 1367 case TCPS_ESTABLISHED: 1368 tp->t_state = TCPS_FIN_WAIT_1; 1369 break; 1370 1371 case TCPS_CLOSE_WAIT: 1372 tp->t_state = TCPS_LAST_ACK; 1373 break; 1374 } 1375 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1376 soisdisconnected(tp->t_inpcb->inp_socket); 1377 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1378 if (tp->t_state == TCPS_FIN_WAIT_2) 1379 callout_reset(tp->tt_2msl, tcp_maxidle, 1380 tcp_timer_2msl, tp); 1381 } 1382 return (tp); 1383 } 1384