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