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