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