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