tcp_usrreq.c revision 98102
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 98102 2002-06-10 20:05:46Z hsu $ 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 INP_LOCK(inp); 145 tp = intotcpcb(inp); 146out: 147 TCPDEBUG2(PRU_ATTACH); 148 if (tp) 149 INP_UNLOCK(inp); 150 INP_INFO_WUNLOCK(&tcbinfo); 151 splx(s); 152 return error; 153} 154 155/* 156 * pru_detach() detaches the TCP protocol from the socket. 157 * If the protocol state is non-embryonic, then can't 158 * do this directly: have to initiate a pru_disconnect(), 159 * which may finish later; embryonic TCB's can just 160 * be discarded here. 161 */ 162static int 163tcp_usr_detach(struct socket *so) 164{ 165 int s = splnet(); 166 int error = 0; 167 struct inpcb *inp; 168 struct tcpcb *tp; 169 TCPDEBUG0; 170 171 INP_INFO_WLOCK(&tcbinfo); 172 inp = sotoinpcb(so); 173 if (inp == 0) { 174 INP_INFO_WUNLOCK(&tcbinfo); 175 splx(s); 176 return EINVAL; /* XXX */ 177 } 178 INP_LOCK(inp); 179 tp = intotcpcb(inp); 180 TCPDEBUG1(); 181 tp = tcp_disconnect(tp); 182 183 TCPDEBUG2(PRU_DETACH); 184 if (tp) 185 INP_UNLOCK(inp); 186 INP_INFO_WUNLOCK(&tcbinfo); 187 splx(s); 188 return error; 189} 190 191#define INI_NOLOCK 0 192#define INI_READ 1 193#define INI_WRITE 2 194 195#define COMMON_START() \ 196 TCPDEBUG0; \ 197 do { \ 198 if (inirw == INI_READ) \ 199 INP_INFO_RLOCK(&tcbinfo); \ 200 else if (inirw == INI_WRITE) \ 201 INP_INFO_WLOCK(&tcbinfo); \ 202 inp = sotoinpcb(so); \ 203 if (inp == 0) { \ 204 if (inirw == INI_READ) \ 205 INP_INFO_RUNLOCK(&tcbinfo); \ 206 else if (inirw == INI_WRITE) \ 207 INP_INFO_WUNLOCK(&tcbinfo); \ 208 splx(s); \ 209 return EINVAL; \ 210 } \ 211 INP_LOCK(inp); \ 212 if (inirw == INI_READ) \ 213 INP_INFO_RUNLOCK(&tcbinfo); \ 214 tp = intotcpcb(inp); \ 215 TCPDEBUG1(); \ 216} while(0) 217 218#define COMMON_END(req) \ 219out: TCPDEBUG2(req); \ 220 do { \ 221 if (tp) \ 222 INP_UNLOCK(inp); \ 223 if (inirw == INI_WRITE) \ 224 INP_INFO_WUNLOCK(&tcbinfo); \ 225 splx(s); \ 226 return error; \ 227 goto out; \ 228} while(0) 229 230/* 231 * Give the socket an address. 232 */ 233static int 234tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 235{ 236 int s = splnet(); 237 int error = 0; 238 struct inpcb *inp; 239 struct tcpcb *tp; 240 struct sockaddr_in *sinp; 241 const int inirw = INI_READ; 242 243 COMMON_START(); 244 245 /* 246 * Must check for multicast addresses and disallow binding 247 * to them. 248 */ 249 sinp = (struct sockaddr_in *)nam; 250 if (sinp->sin_family == AF_INET && 251 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 252 error = EAFNOSUPPORT; 253 goto out; 254 } 255 error = in_pcbbind(inp, nam, td); 256 if (error) 257 goto out; 258 COMMON_END(PRU_BIND); 259} 260 261#ifdef INET6 262static int 263tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 264{ 265 int s = splnet(); 266 int error = 0; 267 struct inpcb *inp; 268 struct tcpcb *tp; 269 struct sockaddr_in6 *sin6p; 270 const int inirw = INI_READ; 271 272 COMMON_START(); 273 274 /* 275 * Must check for multicast addresses and disallow binding 276 * to them. 277 */ 278 sin6p = (struct sockaddr_in6 *)nam; 279 if (sin6p->sin6_family == AF_INET6 && 280 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 281 error = EAFNOSUPPORT; 282 goto out; 283 } 284 inp->inp_vflag &= ~INP_IPV4; 285 inp->inp_vflag |= INP_IPV6; 286 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 287 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 288 inp->inp_vflag |= INP_IPV4; 289 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 290 struct sockaddr_in sin; 291 292 in6_sin6_2_sin(&sin, sin6p); 293 inp->inp_vflag |= INP_IPV4; 294 inp->inp_vflag &= ~INP_IPV6; 295 error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 296 goto out; 297 } 298 } 299 error = in6_pcbbind(inp, nam, td); 300 if (error) 301 goto out; 302 COMMON_END(PRU_BIND); 303} 304#endif /* INET6 */ 305 306/* 307 * Prepare to accept connections. 308 */ 309static int 310tcp_usr_listen(struct socket *so, struct thread *td) 311{ 312 int s = splnet(); 313 int error = 0; 314 struct inpcb *inp; 315 struct tcpcb *tp; 316 const int inirw = INI_READ; 317 318 COMMON_START(); 319 if (inp->inp_lport == 0) 320 error = in_pcbbind(inp, (struct sockaddr *)0, td); 321 if (error == 0) 322 tp->t_state = TCPS_LISTEN; 323 COMMON_END(PRU_LISTEN); 324} 325 326#ifdef INET6 327static int 328tcp6_usr_listen(struct socket *so, struct thread *td) 329{ 330 int s = splnet(); 331 int error = 0; 332 struct inpcb *inp; 333 struct tcpcb *tp; 334 const int inirw = INI_READ; 335 336 COMMON_START(); 337 if (inp->inp_lport == 0) { 338 inp->inp_vflag &= ~INP_IPV4; 339 if (ip6_mapped_addr_on && 340 (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 (sinp->sin_family == AF_INET 374 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 375 error = EAFNOSUPPORT; 376 goto out; 377 } 378 379 if (td && jailed(td->td_ucred)) 380 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 381 382 if ((error = tcp_connect(tp, nam, td)) != 0) 383 goto out; 384 error = tcp_output(tp); 385 COMMON_END(PRU_CONNECT); 386} 387 388#ifdef INET6 389static int 390tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 391{ 392 int s = splnet(); 393 int error = 0; 394 struct inpcb *inp; 395 struct tcpcb *tp; 396 struct sockaddr_in6 *sin6p; 397 const int inirw = INI_WRITE; 398 399 COMMON_START(); 400 401 /* 402 * Must disallow TCP ``connections'' to multicast addresses. 403 */ 404 sin6p = (struct sockaddr_in6 *)nam; 405 if (sin6p->sin6_family == AF_INET6 406 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 407 error = EAFNOSUPPORT; 408 goto out; 409 } 410 411 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 412 struct sockaddr_in sin; 413 414 if (!ip6_mapped_addr_on || 415 (inp->inp_flags & IN6P_IPV6_V6ONLY)) 416 return(EINVAL); 417 418 in6_sin6_2_sin(&sin, sin6p); 419 inp->inp_vflag |= INP_IPV4; 420 inp->inp_vflag &= ~INP_IPV6; 421 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 422 goto out; 423 error = tcp_output(tp); 424 goto out; 425 } 426 inp->inp_vflag &= ~INP_IPV4; 427 inp->inp_vflag |= INP_IPV6; 428 inp->inp_inc.inc_isipv6 = 1; 429 if ((error = tcp6_connect(tp, nam, td)) != 0) 430 goto out; 431 error = tcp_output(tp); 432 COMMON_END(PRU_CONNECT); 433} 434#endif /* INET6 */ 435 436/* 437 * Initiate disconnect from peer. 438 * If connection never passed embryonic stage, just drop; 439 * else if don't need to let data drain, then can just drop anyways, 440 * else have to begin TCP shutdown process: mark socket disconnecting, 441 * drain unread data, state switch to reflect user close, and 442 * send segment (e.g. FIN) to peer. Socket will be really disconnected 443 * when peer sends FIN and acks ours. 444 * 445 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 446 */ 447static int 448tcp_usr_disconnect(struct socket *so) 449{ 450 int s = splnet(); 451 int error = 0; 452 struct inpcb *inp; 453 struct tcpcb *tp; 454 const int inirw = INI_WRITE; 455 456 COMMON_START(); 457 tp = tcp_disconnect(tp); 458 COMMON_END(PRU_DISCONNECT); 459} 460 461/* 462 * Accept a connection. Essentially all the work is 463 * done at higher levels; just return the address 464 * of the peer, storing through addr. 465 */ 466static int 467tcp_usr_accept(struct socket *so, struct sockaddr **nam) 468{ 469 int s; 470 int error = 0; 471 struct inpcb *inp = NULL; 472 struct tcpcb *tp = NULL; 473 struct sockaddr_in *sin; 474 const int inirw = INI_READ; 475 TCPDEBUG0; 476 477 if (so->so_state & SS_ISDISCONNECTED) { 478 error = ECONNABORTED; 479 goto out; 480 } 481 482 /* 483 * Do the malloc first in case it blocks. 484 */ 485 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 486 M_WAITOK | M_ZERO); 487 sin->sin_family = AF_INET; 488 sin->sin_len = sizeof(*sin); 489 490 s = splnet(); 491 INP_INFO_RLOCK(&tcbinfo); 492 inp = sotoinpcb(so); 493 if (!inp) { 494 INP_INFO_RUNLOCK(&tcbinfo); 495 splx(s); 496 free(sin, M_SONAME); 497 return (EINVAL); 498 } 499 INP_LOCK(inp); 500 INP_INFO_RUNLOCK(&tcbinfo); 501 tp = intotcpcb(inp); 502 TCPDEBUG1(); 503 504 /* 505 * We inline in_setpeeraddr here, because we have already done 506 * the locking and the malloc. 507 */ 508 sin->sin_port = inp->inp_fport; 509 sin->sin_addr = inp->inp_faddr; 510 *nam = (struct sockaddr *)sin; 511 512 COMMON_END(PRU_ACCEPT); 513} 514 515#ifdef INET6 516static int 517tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 518{ 519 int s; 520 struct inpcb *inp = NULL; 521 int error = 0; 522 struct tcpcb *tp = NULL; 523 const int inirw = INI_READ; 524 TCPDEBUG0; 525 526 if (so->so_state & SS_ISDISCONNECTED) { 527 error = ECONNABORTED; 528 goto out; 529 } 530 531 s = splnet(); 532 INP_INFO_RLOCK(&tcbinfo); 533 inp = sotoinpcb(so); 534 if (inp == 0) { 535 INP_INFO_RUNLOCK(&tcbinfo); 536 splx(s); 537 return (EINVAL); 538 } 539 INP_LOCK(inp); 540 INP_INFO_RUNLOCK(&tcbinfo); 541 tp = intotcpcb(inp); 542 TCPDEBUG1(); 543 in6_mapped_peeraddr(so, nam); 544 COMMON_END(PRU_ACCEPT); 545} 546#endif /* INET6 */ 547 548/* 549 * This is the wrapper function for in_setsockaddr. We just pass down 550 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 551 * here because in_setsockaddr will call malloc and can block. 552 */ 553static int 554tcp_sockaddr(struct socket *so, struct sockaddr **nam) 555{ 556 return (in_setsockaddr(so, nam, &tcbinfo)); 557} 558 559/* 560 * This is the wrapper function for in_setpeeraddr. We just pass down 561 * the pcbinfo for in_setpeeraddr to lock. 562 */ 563static int 564tcp_peeraddr(struct socket *so, struct sockaddr **nam) 565{ 566 return (in_setpeeraddr(so, nam, &tcbinfo)); 567} 568 569/* 570 * Mark the connection as being incapable of further output. 571 */ 572static int 573tcp_usr_shutdown(struct socket *so) 574{ 575 int s = splnet(); 576 int error = 0; 577 struct inpcb *inp; 578 struct tcpcb *tp; 579 const int inirw = INI_WRITE; 580 581 COMMON_START(); 582 socantsendmore(so); 583 tp = tcp_usrclosed(tp); 584 if (tp) 585 error = tcp_output(tp); 586 COMMON_END(PRU_SHUTDOWN); 587} 588 589/* 590 * After a receive, possibly send window update to peer. 591 */ 592static int 593tcp_usr_rcvd(struct socket *so, int flags) 594{ 595 int s = splnet(); 596 int error = 0; 597 struct inpcb *inp; 598 struct tcpcb *tp; 599 const int inirw = INI_READ; 600 601 COMMON_START(); 602 tcp_output(tp); 603 COMMON_END(PRU_RCVD); 604} 605 606/* 607 * Do a send by putting data in output queue and updating urgent 608 * marker if URG set. Possibly send more data. Unlike the other 609 * pru_*() routines, the mbuf chains are our responsibility. We 610 * must either enqueue them or free them. The other pru_* routines 611 * generally are caller-frees. 612 */ 613static int 614tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 615 struct sockaddr *nam, struct mbuf *control, struct thread *td) 616{ 617 int s = splnet(); 618 int error = 0; 619 struct inpcb *inp; 620 struct tcpcb *tp; 621 const int inirw = INI_WRITE; 622#ifdef INET6 623 int isipv6; 624#endif 625 TCPDEBUG0; 626 627 /* 628 * Need write lock here because this function might call 629 * tcp_connect or tcp_usrclosed. 630 * We really want to have to this function upgrade from read lock 631 * to write lock. XXX 632 */ 633 INP_INFO_WLOCK(&tcbinfo); 634 inp = sotoinpcb(so); 635 if (inp == NULL) { 636 /* 637 * OOPS! we lost a race, the TCP session got reset after 638 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 639 * network interrupt in the non-splnet() section of sosend(). 640 */ 641 if (m) 642 m_freem(m); 643 if (control) 644 m_freem(control); 645 error = ECONNRESET; /* XXX EPIPE? */ 646 tp = NULL; 647 TCPDEBUG1(); 648 goto out; 649 } 650 INP_LOCK(inp); 651#ifdef INET6 652 isipv6 = nam && nam->sa_family == AF_INET6; 653#endif /* INET6 */ 654 tp = intotcpcb(inp); 655 TCPDEBUG1(); 656 if (control) { 657 /* TCP doesn't do control messages (rights, creds, etc) */ 658 if (control->m_len) { 659 m_freem(control); 660 if (m) 661 m_freem(m); 662 error = EINVAL; 663 goto out; 664 } 665 m_freem(control); /* empty control, just free it */ 666 } 667 if (!(flags & PRUS_OOB)) { 668 sbappend(&so->so_snd, m); 669 if (nam && tp->t_state < TCPS_SYN_SENT) { 670 /* 671 * Do implied connect if not yet connected, 672 * initialize window to default value, and 673 * initialize maxseg/maxopd using peer's cached 674 * MSS. 675 */ 676#ifdef INET6 677 if (isipv6) 678 error = tcp6_connect(tp, nam, td); 679 else 680#endif /* INET6 */ 681 error = tcp_connect(tp, nam, td); 682 if (error) 683 goto out; 684 tp->snd_wnd = TTCP_CLIENT_SND_WND; 685 tcp_mss(tp, -1); 686 } 687 688 if (flags & PRUS_EOF) { 689 /* 690 * Close the send side of the connection after 691 * the data is sent. 692 */ 693 socantsendmore(so); 694 tp = tcp_usrclosed(tp); 695 } 696 if (tp != NULL) { 697 if (flags & PRUS_MORETOCOME) 698 tp->t_flags |= TF_MORETOCOME; 699 error = tcp_output(tp); 700 if (flags & PRUS_MORETOCOME) 701 tp->t_flags &= ~TF_MORETOCOME; 702 } 703 } else { 704 if (sbspace(&so->so_snd) < -512) { 705 m_freem(m); 706 error = ENOBUFS; 707 goto out; 708 } 709 /* 710 * According to RFC961 (Assigned Protocols), 711 * the urgent pointer points to the last octet 712 * of urgent data. We continue, however, 713 * to consider it to indicate the first octet 714 * of data past the urgent section. 715 * Otherwise, snd_up should be one lower. 716 */ 717 sbappend(&so->so_snd, m); 718 if (nam && tp->t_state < TCPS_SYN_SENT) { 719 /* 720 * Do implied connect if not yet connected, 721 * initialize window to default value, and 722 * initialize maxseg/maxopd using peer's cached 723 * MSS. 724 */ 725#ifdef INET6 726 if (isipv6) 727 error = tcp6_connect(tp, nam, td); 728 else 729#endif /* INET6 */ 730 error = tcp_connect(tp, nam, td); 731 if (error) 732 goto out; 733 tp->snd_wnd = TTCP_CLIENT_SND_WND; 734 tcp_mss(tp, -1); 735 } 736 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 737 tp->t_force = 1; 738 error = tcp_output(tp); 739 tp->t_force = 0; 740 } 741 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 742 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 743} 744 745/* 746 * Abort the TCP. 747 */ 748static int 749tcp_usr_abort(struct socket *so) 750{ 751 int s = splnet(); 752 int error = 0; 753 struct inpcb *inp; 754 struct tcpcb *tp; 755 const int inirw = INI_WRITE; 756 757 COMMON_START(); 758 tp = tcp_drop(tp, ECONNABORTED); 759 COMMON_END(PRU_ABORT); 760} 761 762/* 763 * Receive out-of-band data. 764 */ 765static int 766tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 767{ 768 int s = splnet(); 769 int error = 0; 770 struct inpcb *inp; 771 struct tcpcb *tp; 772 const int inirw = INI_READ; 773 774 COMMON_START(); 775 if ((so->so_oobmark == 0 && 776 (so->so_state & SS_RCVATMARK) == 0) || 777 so->so_options & SO_OOBINLINE || 778 tp->t_oobflags & TCPOOB_HADDATA) { 779 error = EINVAL; 780 goto out; 781 } 782 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 783 error = EWOULDBLOCK; 784 goto out; 785 } 786 m->m_len = 1; 787 *mtod(m, caddr_t) = tp->t_iobc; 788 if ((flags & MSG_PEEK) == 0) 789 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 790 COMMON_END(PRU_RCVOOB); 791} 792 793/* xxx - should be const */ 794struct pr_usrreqs tcp_usrreqs = { 795 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 796 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 797 tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd, 798 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 799 tcp_sockaddr, sosend, soreceive, sopoll 800}; 801 802#ifdef INET6 803struct pr_usrreqs tcp6_usrreqs = { 804 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 805 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 806 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 807 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 808 in6_mapped_sockaddr, sosend, soreceive, sopoll 809}; 810#endif /* INET6 */ 811 812/* 813 * Common subroutine to open a TCP connection to remote host specified 814 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 815 * port number if needed. Call in_pcbladdr to do the routing and to choose 816 * a local host address (interface). If there is an existing incarnation 817 * of the same connection in TIME-WAIT state and if the remote host was 818 * sending CC options and if the connection duration was < MSL, then 819 * truncate the previous TIME-WAIT state and proceed. 820 * Initialize connection parameters and enter SYN-SENT state. 821 */ 822static int 823tcp_connect(tp, nam, td) 824 register struct tcpcb *tp; 825 struct sockaddr *nam; 826 struct thread *td; 827{ 828 struct inpcb *inp = tp->t_inpcb, *oinp; 829 struct socket *so = inp->inp_socket; 830 struct tcpcb *otp; 831 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 832 struct sockaddr_in *ifaddr; 833 struct rmxp_tao *taop; 834 struct rmxp_tao tao_noncached; 835 int error; 836 837 if (inp->inp_lport == 0) { 838 error = in_pcbbind(inp, (struct sockaddr *)0, td); 839 if (error) 840 return error; 841 } 842 843 /* 844 * Cannot simply call in_pcbconnect, because there might be an 845 * earlier incarnation of this same connection still in 846 * TIME_WAIT state, creating an ADDRINUSE error. 847 */ 848 error = in_pcbladdr(inp, nam, &ifaddr); 849 if (error) 850 return error; 851 oinp = in_pcblookup_hash(inp->inp_pcbinfo, 852 sin->sin_addr, sin->sin_port, 853 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 854 : ifaddr->sin_addr, 855 inp->inp_lport, 0, NULL); 856 if (oinp) { 857 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 858 otp->t_state == TCPS_TIME_WAIT && 859 (ticks - otp->t_starttime) < tcp_msl && 860 (otp->t_flags & TF_RCVD_CC)) 861 otp = tcp_close(otp); 862 else 863 return EADDRINUSE; 864 } 865 if (inp->inp_laddr.s_addr == INADDR_ANY) 866 inp->inp_laddr = ifaddr->sin_addr; 867 inp->inp_faddr = sin->sin_addr; 868 inp->inp_fport = sin->sin_port; 869 in_pcbrehash(inp); 870 871 /* Compute window scaling to request. */ 872 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 873 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 874 tp->request_r_scale++; 875 876 soisconnecting(so); 877 tcpstat.tcps_connattempt++; 878 tp->t_state = TCPS_SYN_SENT; 879 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 880 tp->iss = tcp_new_isn(tp); 881 tcp_sendseqinit(tp); 882 883 /* 884 * Generate a CC value for this connection and 885 * check whether CC or CCnew should be used. 886 */ 887 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 888 taop = &tao_noncached; 889 bzero(taop, sizeof(*taop)); 890 } 891 892 tp->cc_send = CC_INC(tcp_ccgen); 893 if (taop->tao_ccsent != 0 && 894 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 895 taop->tao_ccsent = tp->cc_send; 896 } else { 897 taop->tao_ccsent = 0; 898 tp->t_flags |= TF_SENDCCNEW; 899 } 900 901 return 0; 902} 903 904#ifdef INET6 905static int 906tcp6_connect(tp, nam, td) 907 register struct tcpcb *tp; 908 struct sockaddr *nam; 909 struct thread *td; 910{ 911 struct inpcb *inp = tp->t_inpcb, *oinp; 912 struct socket *so = inp->inp_socket; 913 struct tcpcb *otp; 914 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 915 struct in6_addr *addr6; 916 struct rmxp_tao *taop; 917 struct rmxp_tao tao_noncached; 918 int error; 919 920 if (inp->inp_lport == 0) { 921 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 922 if (error) 923 return error; 924 } 925 926 /* 927 * Cannot simply call in_pcbconnect, because there might be an 928 * earlier incarnation of this same connection still in 929 * TIME_WAIT state, creating an ADDRINUSE error. 930 */ 931 error = in6_pcbladdr(inp, nam, &addr6); 932 if (error) 933 return error; 934 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 935 &sin6->sin6_addr, sin6->sin6_port, 936 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 937 ? addr6 938 : &inp->in6p_laddr, 939 inp->inp_lport, 0, NULL); 940 if (oinp) { 941 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 942 otp->t_state == TCPS_TIME_WAIT && 943 (ticks - otp->t_starttime) < tcp_msl && 944 (otp->t_flags & TF_RCVD_CC)) 945 otp = tcp_close(otp); 946 else 947 return EADDRINUSE; 948 } 949 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 950 inp->in6p_laddr = *addr6; 951 inp->in6p_faddr = sin6->sin6_addr; 952 inp->inp_fport = sin6->sin6_port; 953 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL) 954 inp->in6p_flowinfo = sin6->sin6_flowinfo; 955 in_pcbrehash(inp); 956 957 /* Compute window scaling to request. */ 958 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 959 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 960 tp->request_r_scale++; 961 962 soisconnecting(so); 963 tcpstat.tcps_connattempt++; 964 tp->t_state = TCPS_SYN_SENT; 965 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 966 tp->iss = tcp_new_isn(tp); 967 tcp_sendseqinit(tp); 968 969 /* 970 * Generate a CC value for this connection and 971 * check whether CC or CCnew should be used. 972 */ 973 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 974 taop = &tao_noncached; 975 bzero(taop, sizeof(*taop)); 976 } 977 978 tp->cc_send = CC_INC(tcp_ccgen); 979 if (taop->tao_ccsent != 0 && 980 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 981 taop->tao_ccsent = tp->cc_send; 982 } else { 983 taop->tao_ccsent = 0; 984 tp->t_flags |= TF_SENDCCNEW; 985 } 986 987 return 0; 988} 989#endif /* INET6 */ 990 991/* 992 * The new sockopt interface makes it possible for us to block in the 993 * copyin/out step (if we take a page fault). Taking a page fault at 994 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 995 * use TSM, there probably isn't any need for this function to run at 996 * splnet() any more. This needs more examination.) 997 */ 998int 999tcp_ctloutput(so, sopt) 1000 struct socket *so; 1001 struct sockopt *sopt; 1002{ 1003 int error, opt, optval, s; 1004 struct inpcb *inp; 1005 struct tcpcb *tp; 1006 1007 error = 0; 1008 s = splnet(); /* XXX */ 1009 INP_INFO_RLOCK(&tcbinfo); 1010 inp = sotoinpcb(so); 1011 if (inp == NULL) { 1012 INP_INFO_RUNLOCK(&tcbinfo); 1013 splx(s); 1014 return (ECONNRESET); 1015 } 1016 INP_LOCK(inp); 1017 INP_INFO_RUNLOCK(&tcbinfo); 1018 if (sopt->sopt_level != IPPROTO_TCP) { 1019#ifdef INET6 1020 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1021 error = ip6_ctloutput(so, sopt); 1022 else 1023#endif /* INET6 */ 1024 error = ip_ctloutput(so, sopt); 1025 INP_UNLOCK(inp); 1026 splx(s); 1027 return (error); 1028 } 1029 tp = intotcpcb(inp); 1030 1031 switch (sopt->sopt_dir) { 1032 case SOPT_SET: 1033 switch (sopt->sopt_name) { 1034 case TCP_NODELAY: 1035 case TCP_NOOPT: 1036 error = sooptcopyin(sopt, &optval, sizeof optval, 1037 sizeof optval); 1038 if (error) 1039 break; 1040 1041 switch (sopt->sopt_name) { 1042 case TCP_NODELAY: 1043 opt = TF_NODELAY; 1044 break; 1045 case TCP_NOOPT: 1046 opt = TF_NOOPT; 1047 break; 1048 default: 1049 opt = 0; /* dead code to fool gcc */ 1050 break; 1051 } 1052 1053 if (optval) 1054 tp->t_flags |= opt; 1055 else 1056 tp->t_flags &= ~opt; 1057 break; 1058 1059 case TCP_NOPUSH: 1060 error = sooptcopyin(sopt, &optval, sizeof optval, 1061 sizeof optval); 1062 if (error) 1063 break; 1064 1065 if (optval) 1066 tp->t_flags |= TF_NOPUSH; 1067 else { 1068 tp->t_flags &= ~TF_NOPUSH; 1069 error = tcp_output(tp); 1070 } 1071 break; 1072 1073 case TCP_MAXSEG: 1074 error = sooptcopyin(sopt, &optval, sizeof optval, 1075 sizeof optval); 1076 if (error) 1077 break; 1078 1079 if (optval > 0 && optval <= tp->t_maxseg) 1080 tp->t_maxseg = optval; 1081 else 1082 error = EINVAL; 1083 break; 1084 1085 default: 1086 error = ENOPROTOOPT; 1087 break; 1088 } 1089 break; 1090 1091 case SOPT_GET: 1092 switch (sopt->sopt_name) { 1093 case TCP_NODELAY: 1094 optval = tp->t_flags & TF_NODELAY; 1095 break; 1096 case TCP_MAXSEG: 1097 optval = tp->t_maxseg; 1098 break; 1099 case TCP_NOOPT: 1100 optval = tp->t_flags & TF_NOOPT; 1101 break; 1102 case TCP_NOPUSH: 1103 optval = tp->t_flags & TF_NOPUSH; 1104 break; 1105 default: 1106 error = ENOPROTOOPT; 1107 break; 1108 } 1109 if (error == 0) 1110 error = sooptcopyout(sopt, &optval, sizeof optval); 1111 break; 1112 } 1113 INP_UNLOCK(inp); 1114 splx(s); 1115 return (error); 1116} 1117 1118/* 1119 * tcp_sendspace and tcp_recvspace are the default send and receive window 1120 * sizes, respectively. These are obsolescent (this information should 1121 * be set by the route). 1122 */ 1123u_long tcp_sendspace = 1024*32; 1124SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1125 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1126u_long tcp_recvspace = 1024*64; 1127SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1128 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1129 1130/* 1131 * Attach TCP protocol to socket, allocating 1132 * internet protocol control block, tcp control block, 1133 * bufer space, and entering LISTEN state if to accept connections. 1134 */ 1135static int 1136tcp_attach(so, td) 1137 struct socket *so; 1138 struct thread *td; 1139{ 1140 register struct tcpcb *tp; 1141 struct inpcb *inp; 1142 int error; 1143#ifdef INET6 1144 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL; 1145#endif 1146 1147 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1148 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1149 if (error) 1150 return (error); 1151 } 1152 error = in_pcballoc(so, &tcbinfo, td); 1153 if (error) 1154 return (error); 1155 inp = sotoinpcb(so); 1156#ifdef INET6 1157 if (isipv6) { 1158 inp->inp_vflag |= INP_IPV6; 1159 inp->in6p_hops = -1; /* use kernel default */ 1160 } 1161 else 1162#endif 1163 inp->inp_vflag |= INP_IPV4; 1164 tp = tcp_newtcpcb(inp); 1165 if (tp == 0) { 1166 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1167 1168 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1169#ifdef INET6 1170 if (isipv6) 1171 in6_pcbdetach(inp); 1172 else 1173#endif 1174 in_pcbdetach(inp); 1175 so->so_state |= nofd; 1176 return (ENOBUFS); 1177 } 1178 tp->t_state = TCPS_CLOSED; 1179 return (0); 1180} 1181 1182/* 1183 * Initiate (or continue) disconnect. 1184 * If embryonic state, just send reset (once). 1185 * If in ``let data drain'' option and linger null, just drop. 1186 * Otherwise (hard), mark socket disconnecting and drop 1187 * current input data; switch states based on user close, and 1188 * send segment to peer (with FIN). 1189 */ 1190static struct tcpcb * 1191tcp_disconnect(tp) 1192 register struct tcpcb *tp; 1193{ 1194 struct socket *so = tp->t_inpcb->inp_socket; 1195 1196 if (tp->t_state < TCPS_ESTABLISHED) 1197 tp = tcp_close(tp); 1198 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1199 tp = tcp_drop(tp, 0); 1200 else { 1201 soisdisconnecting(so); 1202 sbflush(&so->so_rcv); 1203 tp = tcp_usrclosed(tp); 1204 if (tp) 1205 (void) tcp_output(tp); 1206 } 1207 return (tp); 1208} 1209 1210/* 1211 * User issued close, and wish to trail through shutdown states: 1212 * if never received SYN, just forget it. If got a SYN from peer, 1213 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1214 * If already got a FIN from peer, then almost done; go to LAST_ACK 1215 * state. In all other cases, have already sent FIN to peer (e.g. 1216 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1217 * for peer to send FIN or not respond to keep-alives, etc. 1218 * We can let the user exit from the close as soon as the FIN is acked. 1219 */ 1220static struct tcpcb * 1221tcp_usrclosed(tp) 1222 register struct tcpcb *tp; 1223{ 1224 1225 switch (tp->t_state) { 1226 1227 case TCPS_CLOSED: 1228 case TCPS_LISTEN: 1229 tp->t_state = TCPS_CLOSED; 1230 tp = tcp_close(tp); 1231 break; 1232 1233 case TCPS_SYN_SENT: 1234 case TCPS_SYN_RECEIVED: 1235 tp->t_flags |= TF_NEEDFIN; 1236 break; 1237 1238 case TCPS_ESTABLISHED: 1239 tp->t_state = TCPS_FIN_WAIT_1; 1240 break; 1241 1242 case TCPS_CLOSE_WAIT: 1243 tp->t_state = TCPS_LAST_ACK; 1244 break; 1245 } 1246 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1247 soisdisconnected(tp->t_inpcb->inp_socket); 1248 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1249 if (tp->t_state == TCPS_FIN_WAIT_2) 1250 callout_reset(tp->tt_2msl, tcp_maxidle, 1251 tcp_timer_2msl, tp); 1252 } 1253 return (tp); 1254} 1255 1256