tcp_usrreq.c revision 122875
1219820Sjeff/* 2219820Sjeff * Copyright (c) 1982, 1986, 1988, 1993 3219820Sjeff * The Regents of the University of California. All rights reserved. 4219820Sjeff * 5219820Sjeff * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer. 10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer in the 12219820Sjeff * documentation and/or other materials provided with the distribution. 13219820Sjeff * 3. All advertising materials mentioning features or use of this software 14219820Sjeff * must display the following acknowledgement: 15219820Sjeff * This product includes software developed by the University of 16219820Sjeff * California, Berkeley and its contributors. 17219820Sjeff * 4. Neither the name of the University nor the names of its contributors 18219820Sjeff * may be used to endorse or promote products derived from this software 19219820Sjeff * without specific prior written permission. 20219820Sjeff * 21219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31219820Sjeff * SUCH DAMAGE. 32219820Sjeff * 33219820Sjeff * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 34219820Sjeff * $FreeBSD: head/sys/netinet/tcp_usrreq.c 122875 2003-11-18 00:39:07Z rwatson $ 35219820Sjeff */ 36219820Sjeff 37219820Sjeff#include "opt_ipsec.h" 38219820Sjeff#include "opt_inet6.h" 39219820Sjeff#include "opt_tcpdebug.h" 40219820Sjeff 41219820Sjeff#include <sys/param.h> 42219820Sjeff#include <sys/systm.h> 43219820Sjeff#include <sys/malloc.h> 44219820Sjeff#include <sys/kernel.h> 45219820Sjeff#include <sys/sysctl.h> 46219820Sjeff#include <sys/mbuf.h> 47219820Sjeff#ifdef INET6 48219820Sjeff#include <sys/domain.h> 49219820Sjeff#endif /* INET6 */ 50219820Sjeff#include <sys/socket.h> 51219820Sjeff#include <sys/socketvar.h> 52219820Sjeff#include <sys/protosw.h> 53219820Sjeff#include <sys/proc.h> 54219820Sjeff#include <sys/jail.h> 55219820Sjeff 56219820Sjeff#include <net/if.h> 57219820Sjeff#include <net/route.h> 58219820Sjeff 59219820Sjeff#include <netinet/in.h> 60219820Sjeff#include <netinet/in_systm.h> 61219820Sjeff#ifdef INET6 62219820Sjeff#include <netinet/ip6.h> 63219820Sjeff#endif 64219820Sjeff#include <netinet/in_pcb.h> 65219820Sjeff#ifdef INET6 66219820Sjeff#include <netinet6/in6_pcb.h> 67219820Sjeff#endif 68219820Sjeff#include <netinet/in_var.h> 69219820Sjeff#include <netinet/ip_var.h> 70219820Sjeff#ifdef INET6 71219820Sjeff#include <netinet6/ip6_var.h> 72219820Sjeff#endif 73219820Sjeff#include <netinet/tcp.h> 74219820Sjeff#include <netinet/tcp_fsm.h> 75219820Sjeff#include <netinet/tcp_seq.h> 76219820Sjeff#include <netinet/tcp_timer.h> 77219820Sjeff#include <netinet/tcp_var.h> 78219820Sjeff#include <netinet/tcpip.h> 79219820Sjeff#ifdef TCPDEBUG 80219820Sjeff#include <netinet/tcp_debug.h> 81219820Sjeff#endif 82219820Sjeff 83219820Sjeff#ifdef IPSEC 84219820Sjeff#include <netinet6/ipsec.h> 85219820Sjeff#endif /*IPSEC*/ 86219820Sjeff 87219820Sjeff/* 88219820Sjeff * TCP protocol interface to socket abstraction. 89219820Sjeff */ 90219820Sjeffextern char *tcpstates[]; /* XXX ??? */ 91219820Sjeff 92219820Sjeffstatic int tcp_attach(struct socket *, struct thread *td); 93219820Sjeffstatic int tcp_connect(struct tcpcb *, struct sockaddr *, 94219820Sjeff struct thread *td); 95219820Sjeff#ifdef INET6 96219820Sjeffstatic int tcp6_connect(struct tcpcb *, struct sockaddr *, 97219820Sjeff struct thread *td); 98219820Sjeff#endif /* INET6 */ 99219820Sjeffstatic struct tcpcb * 100219820Sjeff tcp_disconnect(struct tcpcb *); 101219820Sjeffstatic struct tcpcb * 102219820Sjeff tcp_usrclosed(struct tcpcb *); 103219820Sjeff 104219820Sjeff#ifdef TCPDEBUG 105219820Sjeff#define TCPDEBUG0 int ostate = 0 106219820Sjeff#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 107219820Sjeff#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 108219820Sjeff tcp_trace(TA_USER, ostate, tp, 0, 0, req) 109219820Sjeff#else 110219820Sjeff#define TCPDEBUG0 111219820Sjeff#define TCPDEBUG1() 112219820Sjeff#define TCPDEBUG2(req) 113219820Sjeff#endif 114219820Sjeff 115219820Sjeff/* 116219820Sjeff * TCP attaches to socket via pru_attach(), reserving space, 117219820Sjeff * and an internet control block. 118219820Sjeff */ 119219820Sjeffstatic int 120219820Sjefftcp_usr_attach(struct socket *so, int proto, struct thread *td) 121219820Sjeff{ 122219820Sjeff int s = splnet(); 123219820Sjeff int error; 124219820Sjeff struct inpcb *inp; 125219820Sjeff struct tcpcb *tp = 0; 126219820Sjeff TCPDEBUG0; 127219820Sjeff 128219820Sjeff INP_INFO_WLOCK(&tcbinfo); 129219820Sjeff TCPDEBUG1(); 130219820Sjeff inp = sotoinpcb(so); 131219820Sjeff if (inp) { 132219820Sjeff error = EISCONN; 133219820Sjeff goto out; 134219820Sjeff } 135219820Sjeff 136219820Sjeff error = tcp_attach(so, td); 137219820Sjeff if (error) 138219820Sjeff goto out; 139219820Sjeff 140219820Sjeff if ((so->so_options & SO_LINGER) && so->so_linger == 0) 141219820Sjeff so->so_linger = TCP_LINGERTIME; 142219820Sjeff 143219820Sjeff inp = sotoinpcb(so); 144219820Sjeff tp = intotcpcb(inp); 145219820Sjeffout: 146219820Sjeff TCPDEBUG2(PRU_ATTACH); 147219820Sjeff INP_INFO_WUNLOCK(&tcbinfo); 148219820Sjeff splx(s); 149219820Sjeff return error; 150219820Sjeff} 151219820Sjeff 152219820Sjeff/* 153219820Sjeff * pru_detach() detaches the TCP protocol from the socket. 154219820Sjeff * If the protocol state is non-embryonic, then can't 155219820Sjeff * do this directly: have to initiate a pru_disconnect(), 156219820Sjeff * which may finish later; embryonic TCB's can just 157219820Sjeff * be discarded here. 158219820Sjeff */ 159219820Sjeffstatic int 160219820Sjefftcp_usr_detach(struct socket *so) 161219820Sjeff{ 162219820Sjeff int s = splnet(); 163219820Sjeff int error = 0; 164219820Sjeff struct inpcb *inp; 165219820Sjeff struct tcpcb *tp; 166219820Sjeff TCPDEBUG0; 167219820Sjeff 168219820Sjeff INP_INFO_WLOCK(&tcbinfo); 169219820Sjeff inp = sotoinpcb(so); 170219820Sjeff if (inp == 0) { 171219820Sjeff INP_INFO_WUNLOCK(&tcbinfo); 172219820Sjeff splx(s); 173219820Sjeff return EINVAL; /* XXX */ 174219820Sjeff } 175219820Sjeff INP_LOCK(inp); 176219820Sjeff tp = intotcpcb(inp); 177219820Sjeff TCPDEBUG1(); 178219820Sjeff tp = tcp_disconnect(tp); 179219820Sjeff 180219820Sjeff TCPDEBUG2(PRU_DETACH); 181219820Sjeff if (tp) 182219820Sjeff INP_UNLOCK(inp); 183219820Sjeff INP_INFO_WUNLOCK(&tcbinfo); 184219820Sjeff splx(s); 185219820Sjeff return error; 186219820Sjeff} 187219820Sjeff 188219820Sjeff#define INI_NOLOCK 0 189219820Sjeff#define INI_READ 1 190219820Sjeff#define INI_WRITE 2 191219820Sjeff 192219820Sjeff#define COMMON_START() \ 193219820Sjeff TCPDEBUG0; \ 194219820Sjeff do { \ 195219820Sjeff if (inirw == INI_READ) \ 196219820Sjeff INP_INFO_RLOCK(&tcbinfo); \ 197219820Sjeff else if (inirw == INI_WRITE) \ 198219820Sjeff INP_INFO_WLOCK(&tcbinfo); \ 199219820Sjeff inp = sotoinpcb(so); \ 200219820Sjeff if (inp == 0) { \ 201219820Sjeff if (inirw == INI_READ) \ 202219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); \ 203219820Sjeff else if (inirw == INI_WRITE) \ 204219820Sjeff INP_INFO_WUNLOCK(&tcbinfo); \ 205219820Sjeff splx(s); \ 206219820Sjeff return EINVAL; \ 207219820Sjeff } \ 208219820Sjeff INP_LOCK(inp); \ 209219820Sjeff if (inirw == INI_READ) \ 210219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); \ 211219820Sjeff tp = intotcpcb(inp); \ 212219820Sjeff TCPDEBUG1(); \ 213219820Sjeff} while(0) 214219820Sjeff 215219820Sjeff#define COMMON_END(req) \ 216219820Sjeffout: TCPDEBUG2(req); \ 217219820Sjeff do { \ 218219820Sjeff if (tp) \ 219219820Sjeff INP_UNLOCK(inp); \ 220219820Sjeff if (inirw == INI_WRITE) \ 221219820Sjeff INP_INFO_WUNLOCK(&tcbinfo); \ 222219820Sjeff splx(s); \ 223219820Sjeff return error; \ 224219820Sjeff goto out; \ 225219820Sjeff} while(0) 226219820Sjeff 227219820Sjeff/* 228219820Sjeff * Give the socket an address. 229219820Sjeff */ 230219820Sjeffstatic int 231219820Sjefftcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 232219820Sjeff{ 233219820Sjeff int s = splnet(); 234219820Sjeff int error = 0; 235219820Sjeff struct inpcb *inp; 236219820Sjeff struct tcpcb *tp; 237219820Sjeff struct sockaddr_in *sinp; 238219820Sjeff const int inirw = INI_WRITE; 239219820Sjeff 240219820Sjeff COMMON_START(); 241219820Sjeff 242219820Sjeff /* 243219820Sjeff * Must check for multicast addresses and disallow binding 244219820Sjeff * to them. 245219820Sjeff */ 246219820Sjeff sinp = (struct sockaddr_in *)nam; 247219820Sjeff if (sinp->sin_family == AF_INET && 248219820Sjeff IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 249219820Sjeff error = EAFNOSUPPORT; 250219820Sjeff goto out; 251219820Sjeff } 252219820Sjeff error = in_pcbbind(inp, nam, td); 253219820Sjeff if (error) 254219820Sjeff goto out; 255219820Sjeff COMMON_END(PRU_BIND); 256219820Sjeff} 257219820Sjeff 258219820Sjeff#ifdef INET6 259219820Sjeffstatic int 260219820Sjefftcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 261219820Sjeff{ 262219820Sjeff int s = splnet(); 263219820Sjeff int error = 0; 264219820Sjeff struct inpcb *inp; 265219820Sjeff struct tcpcb *tp; 266219820Sjeff struct sockaddr_in6 *sin6p; 267219820Sjeff const int inirw = INI_WRITE; 268219820Sjeff 269219820Sjeff COMMON_START(); 270219820Sjeff 271219820Sjeff /* 272219820Sjeff * Must check for multicast addresses and disallow binding 273219820Sjeff * to them. 274219820Sjeff */ 275219820Sjeff sin6p = (struct sockaddr_in6 *)nam; 276219820Sjeff if (sin6p->sin6_family == AF_INET6 && 277219820Sjeff IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 278219820Sjeff error = EAFNOSUPPORT; 279219820Sjeff goto out; 280219820Sjeff } 281219820Sjeff inp->inp_vflag &= ~INP_IPV4; 282219820Sjeff inp->inp_vflag |= INP_IPV6; 283219820Sjeff if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 284219820Sjeff if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 285219820Sjeff inp->inp_vflag |= INP_IPV4; 286219820Sjeff else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 287219820Sjeff struct sockaddr_in sin; 288219820Sjeff 289219820Sjeff in6_sin6_2_sin(&sin, sin6p); 290219820Sjeff inp->inp_vflag |= INP_IPV4; 291219820Sjeff inp->inp_vflag &= ~INP_IPV6; 292219820Sjeff error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 293219820Sjeff goto out; 294219820Sjeff } 295219820Sjeff } 296219820Sjeff error = in6_pcbbind(inp, nam, td); 297219820Sjeff if (error) 298219820Sjeff goto out; 299219820Sjeff COMMON_END(PRU_BIND); 300219820Sjeff} 301219820Sjeff#endif /* INET6 */ 302219820Sjeff 303219820Sjeff/* 304219820Sjeff * Prepare to accept connections. 305219820Sjeff */ 306219820Sjeffstatic int 307219820Sjefftcp_usr_listen(struct socket *so, struct thread *td) 308219820Sjeff{ 309219820Sjeff int s = splnet(); 310219820Sjeff int error = 0; 311219820Sjeff struct inpcb *inp; 312219820Sjeff struct tcpcb *tp; 313219820Sjeff const int inirw = INI_WRITE; 314219820Sjeff 315219820Sjeff COMMON_START(); 316219820Sjeff if (inp->inp_lport == 0) 317219820Sjeff error = in_pcbbind(inp, (struct sockaddr *)0, td); 318219820Sjeff if (error == 0) 319219820Sjeff tp->t_state = TCPS_LISTEN; 320219820Sjeff COMMON_END(PRU_LISTEN); 321219820Sjeff} 322219820Sjeff 323219820Sjeff#ifdef INET6 324219820Sjeffstatic int 325219820Sjefftcp6_usr_listen(struct socket *so, struct thread *td) 326219820Sjeff{ 327219820Sjeff int s = splnet(); 328219820Sjeff int error = 0; 329219820Sjeff struct inpcb *inp; 330219820Sjeff struct tcpcb *tp; 331219820Sjeff const int inirw = INI_WRITE; 332219820Sjeff 333219820Sjeff COMMON_START(); 334219820Sjeff if (inp->inp_lport == 0) { 335219820Sjeff inp->inp_vflag &= ~INP_IPV4; 336219820Sjeff if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 337219820Sjeff inp->inp_vflag |= INP_IPV4; 338219820Sjeff error = in6_pcbbind(inp, (struct sockaddr *)0, td); 339219820Sjeff } 340219820Sjeff if (error == 0) 341219820Sjeff tp->t_state = TCPS_LISTEN; 342219820Sjeff COMMON_END(PRU_LISTEN); 343219820Sjeff} 344219820Sjeff#endif /* INET6 */ 345219820Sjeff 346219820Sjeff/* 347219820Sjeff * Initiate connection to peer. 348219820Sjeff * Create a template for use in transmissions on this connection. 349219820Sjeff * Enter SYN_SENT state, and mark socket as connecting. 350219820Sjeff * Start keep-alive timer, and seed output sequence space. 351219820Sjeff * Send initial segment on connection. 352219820Sjeff */ 353219820Sjeffstatic int 354219820Sjefftcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 355219820Sjeff{ 356219820Sjeff int s = splnet(); 357219820Sjeff int error = 0; 358219820Sjeff struct inpcb *inp; 359219820Sjeff struct tcpcb *tp; 360219820Sjeff struct sockaddr_in *sinp; 361219820Sjeff const int inirw = INI_WRITE; 362219820Sjeff 363219820Sjeff COMMON_START(); 364219820Sjeff 365219820Sjeff /* 366219820Sjeff * Must disallow TCP ``connections'' to multicast addresses. 367219820Sjeff */ 368219820Sjeff sinp = (struct sockaddr_in *)nam; 369219820Sjeff if (sinp->sin_family == AF_INET 370219820Sjeff && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 371219820Sjeff error = EAFNOSUPPORT; 372219820Sjeff goto out; 373219820Sjeff } 374219820Sjeff 375219820Sjeff if (td && jailed(td->td_ucred)) 376219820Sjeff prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 377219820Sjeff 378219820Sjeff if ((error = tcp_connect(tp, nam, td)) != 0) 379219820Sjeff goto out; 380219820Sjeff error = tcp_output(tp); 381219820Sjeff COMMON_END(PRU_CONNECT); 382219820Sjeff} 383219820Sjeff 384219820Sjeff#ifdef INET6 385219820Sjeffstatic int 386219820Sjefftcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 387219820Sjeff{ 388219820Sjeff int s = splnet(); 389219820Sjeff int error = 0; 390219820Sjeff struct inpcb *inp; 391219820Sjeff struct tcpcb *tp; 392219820Sjeff struct sockaddr_in6 *sin6p; 393219820Sjeff const int inirw = INI_WRITE; 394219820Sjeff 395219820Sjeff COMMON_START(); 396219820Sjeff 397219820Sjeff /* 398219820Sjeff * Must disallow TCP ``connections'' to multicast addresses. 399219820Sjeff */ 400219820Sjeff sin6p = (struct sockaddr_in6 *)nam; 401219820Sjeff if (sin6p->sin6_family == AF_INET6 402219820Sjeff && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 403219820Sjeff error = EAFNOSUPPORT; 404219820Sjeff goto out; 405219820Sjeff } 406219820Sjeff 407219820Sjeff if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 408219820Sjeff struct sockaddr_in sin; 409219820Sjeff 410219820Sjeff if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 411219820Sjeff error = EINVAL; 412219820Sjeff goto out; 413219820Sjeff } 414219820Sjeff 415219820Sjeff in6_sin6_2_sin(&sin, sin6p); 416219820Sjeff inp->inp_vflag |= INP_IPV4; 417219820Sjeff inp->inp_vflag &= ~INP_IPV6; 418219820Sjeff if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 419219820Sjeff goto out; 420219820Sjeff error = tcp_output(tp); 421219820Sjeff goto out; 422219820Sjeff } 423219820Sjeff inp->inp_vflag &= ~INP_IPV4; 424219820Sjeff inp->inp_vflag |= INP_IPV6; 425219820Sjeff inp->inp_inc.inc_isipv6 = 1; 426219820Sjeff if ((error = tcp6_connect(tp, nam, td)) != 0) 427219820Sjeff goto out; 428219820Sjeff error = tcp_output(tp); 429219820Sjeff COMMON_END(PRU_CONNECT); 430219820Sjeff} 431219820Sjeff#endif /* INET6 */ 432219820Sjeff 433219820Sjeff/* 434219820Sjeff * Initiate disconnect from peer. 435219820Sjeff * If connection never passed embryonic stage, just drop; 436219820Sjeff * else if don't need to let data drain, then can just drop anyways, 437219820Sjeff * else have to begin TCP shutdown process: mark socket disconnecting, 438219820Sjeff * drain unread data, state switch to reflect user close, and 439219820Sjeff * send segment (e.g. FIN) to peer. Socket will be really disconnected 440219820Sjeff * when peer sends FIN and acks ours. 441219820Sjeff * 442219820Sjeff * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 443219820Sjeff */ 444219820Sjeffstatic int 445219820Sjefftcp_usr_disconnect(struct socket *so) 446219820Sjeff{ 447219820Sjeff int s = splnet(); 448219820Sjeff int error = 0; 449219820Sjeff struct inpcb *inp; 450219820Sjeff struct tcpcb *tp; 451219820Sjeff const int inirw = INI_WRITE; 452219820Sjeff 453219820Sjeff COMMON_START(); 454219820Sjeff tp = tcp_disconnect(tp); 455219820Sjeff COMMON_END(PRU_DISCONNECT); 456219820Sjeff} 457219820Sjeff 458219820Sjeff/* 459219820Sjeff * Accept a connection. Essentially all the work is 460219820Sjeff * done at higher levels; just return the address 461219820Sjeff * of the peer, storing through addr. 462219820Sjeff */ 463219820Sjeffstatic int 464219820Sjefftcp_usr_accept(struct socket *so, struct sockaddr **nam) 465219820Sjeff{ 466219820Sjeff int s; 467219820Sjeff int error = 0; 468219820Sjeff struct inpcb *inp = NULL; 469219820Sjeff struct tcpcb *tp = NULL; 470219820Sjeff struct in_addr addr; 471219820Sjeff in_port_t port = 0; 472219820Sjeff TCPDEBUG0; 473219820Sjeff 474219820Sjeff if (so->so_state & SS_ISDISCONNECTED) { 475219820Sjeff error = ECONNABORTED; 476219820Sjeff goto out; 477219820Sjeff } 478219820Sjeff 479219820Sjeff s = splnet(); 480219820Sjeff INP_INFO_RLOCK(&tcbinfo); 481219820Sjeff inp = sotoinpcb(so); 482219820Sjeff if (!inp) { 483219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); 484219820Sjeff splx(s); 485219820Sjeff return (EINVAL); 486219820Sjeff } 487219820Sjeff INP_LOCK(inp); 488219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); 489219820Sjeff tp = intotcpcb(inp); 490219820Sjeff TCPDEBUG1(); 491219820Sjeff 492219820Sjeff /* 493219820Sjeff * We inline in_setpeeraddr and COMMON_END here, so that we can 494219820Sjeff * copy the data of interest and defer the malloc until after we 495219820Sjeff * release the lock. 496219820Sjeff */ 497219820Sjeff port = inp->inp_fport; 498219820Sjeff addr = inp->inp_faddr; 499219820Sjeff 500219820Sjeffout: TCPDEBUG2(PRU_ACCEPT); 501219820Sjeff if (tp) 502219820Sjeff INP_UNLOCK(inp); 503219820Sjeff splx(s); 504219820Sjeff if (error == 0) 505219820Sjeff *nam = in_sockaddr(port, &addr); 506219820Sjeff return error; 507219820Sjeff} 508219820Sjeff 509219820Sjeff#ifdef INET6 510219820Sjeffstatic int 511219820Sjefftcp6_usr_accept(struct socket *so, struct sockaddr **nam) 512219820Sjeff{ 513219820Sjeff int s; 514219820Sjeff struct inpcb *inp = NULL; 515219820Sjeff int error = 0; 516219820Sjeff struct tcpcb *tp = NULL; 517219820Sjeff struct in_addr addr; 518219820Sjeff struct in6_addr addr6; 519219820Sjeff in_port_t port = 0; 520219820Sjeff int v4 = 0; 521219820Sjeff TCPDEBUG0; 522219820Sjeff 523219820Sjeff if (so->so_state & SS_ISDISCONNECTED) { 524219820Sjeff error = ECONNABORTED; 525219820Sjeff goto out; 526219820Sjeff } 527219820Sjeff 528219820Sjeff s = splnet(); 529219820Sjeff INP_INFO_RLOCK(&tcbinfo); 530219820Sjeff inp = sotoinpcb(so); 531219820Sjeff if (inp == 0) { 532219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); 533219820Sjeff splx(s); 534219820Sjeff return (EINVAL); 535219820Sjeff } 536219820Sjeff INP_LOCK(inp); 537219820Sjeff INP_INFO_RUNLOCK(&tcbinfo); 538219820Sjeff tp = intotcpcb(inp); 539219820Sjeff TCPDEBUG1(); 540219820Sjeff /* 541219820Sjeff * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 542219820Sjeff * copy the data of interest and defer the malloc until after we 543219820Sjeff * release the lock. 544219820Sjeff */ 545219820Sjeff if (inp->inp_vflag & INP_IPV4) { 546219820Sjeff v4 = 1; 547219820Sjeff port = inp->inp_fport; 548219820Sjeff addr = inp->inp_faddr; 549219820Sjeff } else { 550219820Sjeff port = inp->inp_fport; 551219820Sjeff addr6 = inp->in6p_faddr; 552219820Sjeff } 553219820Sjeff 554219820Sjeffout: TCPDEBUG2(PRU_ACCEPT); 555219820Sjeff if (tp) 556219820Sjeff INP_UNLOCK(inp); 557219820Sjeff splx(s); 558219820Sjeff if (error == 0) { 559219820Sjeff if (v4) 560219820Sjeff *nam = in6_v4mapsin6_sockaddr(port, &addr); 561219820Sjeff else 562219820Sjeff *nam = in6_sockaddr(port, &addr6); 563219820Sjeff } 564219820Sjeff return error; 565219820Sjeff} 566219820Sjeff#endif /* INET6 */ 567219820Sjeff 568219820Sjeff/* 569219820Sjeff * This is the wrapper function for in_setsockaddr. We just pass down 570219820Sjeff * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 571219820Sjeff * here because in_setsockaddr will call malloc and can block. 572219820Sjeff */ 573219820Sjeffstatic int 574219820Sjefftcp_sockaddr(struct socket *so, struct sockaddr **nam) 575219820Sjeff{ 576219820Sjeff return (in_setsockaddr(so, nam, &tcbinfo)); 577219820Sjeff} 578219820Sjeff 579219820Sjeff/* 580219820Sjeff * This is the wrapper function for in_setpeeraddr. We just pass down 581219820Sjeff * the pcbinfo for in_setpeeraddr to lock. 582219820Sjeff */ 583219820Sjeffstatic int 584219820Sjefftcp_peeraddr(struct socket *so, struct sockaddr **nam) 585219820Sjeff{ 586219820Sjeff return (in_setpeeraddr(so, nam, &tcbinfo)); 587219820Sjeff} 588219820Sjeff 589219820Sjeff/* 590219820Sjeff * Mark the connection as being incapable of further output. 591219820Sjeff */ 592219820Sjeffstatic int 593219820Sjefftcp_usr_shutdown(struct socket *so) 594219820Sjeff{ 595219820Sjeff int s = splnet(); 596219820Sjeff int error = 0; 597219820Sjeff struct inpcb *inp; 598219820Sjeff struct tcpcb *tp; 599219820Sjeff const int inirw = INI_WRITE; 600219820Sjeff 601219820Sjeff COMMON_START(); 602219820Sjeff socantsendmore(so); 603219820Sjeff tp = tcp_usrclosed(tp); 604219820Sjeff if (tp) 605219820Sjeff error = tcp_output(tp); 606219820Sjeff COMMON_END(PRU_SHUTDOWN); 607} 608 609/* 610 * After a receive, possibly send window update to peer. 611 */ 612static int 613tcp_usr_rcvd(struct socket *so, int flags) 614{ 615 int s = splnet(); 616 int error = 0; 617 struct inpcb *inp; 618 struct tcpcb *tp; 619 const int inirw = INI_READ; 620 621 COMMON_START(); 622 tcp_output(tp); 623 COMMON_END(PRU_RCVD); 624} 625 626/* 627 * Do a send by putting data in output queue and updating urgent 628 * marker if URG set. Possibly send more data. Unlike the other 629 * pru_*() routines, the mbuf chains are our responsibility. We 630 * must either enqueue them or free them. The other pru_* routines 631 * generally are caller-frees. 632 */ 633static int 634tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 635 struct sockaddr *nam, struct mbuf *control, struct thread *td) 636{ 637 int s = splnet(); 638 int error = 0; 639 struct inpcb *inp; 640 struct tcpcb *tp; 641 const int inirw = INI_WRITE; 642#ifdef INET6 643 int isipv6; 644#endif 645 TCPDEBUG0; 646 647 /* 648 * Need write lock here because this function might call 649 * tcp_connect or tcp_usrclosed. 650 * We really want to have to this function upgrade from read lock 651 * to write lock. XXX 652 */ 653 INP_INFO_WLOCK(&tcbinfo); 654 inp = sotoinpcb(so); 655 if (inp == NULL) { 656 /* 657 * OOPS! we lost a race, the TCP session got reset after 658 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 659 * network interrupt in the non-splnet() section of sosend(). 660 */ 661 if (m) 662 m_freem(m); 663 if (control) 664 m_freem(control); 665 error = ECONNRESET; /* XXX EPIPE? */ 666 tp = NULL; 667 TCPDEBUG1(); 668 goto out; 669 } 670 INP_LOCK(inp); 671#ifdef INET6 672 isipv6 = nam && nam->sa_family == AF_INET6; 673#endif /* INET6 */ 674 tp = intotcpcb(inp); 675 TCPDEBUG1(); 676 if (control) { 677 /* TCP doesn't do control messages (rights, creds, etc) */ 678 if (control->m_len) { 679 m_freem(control); 680 if (m) 681 m_freem(m); 682 error = EINVAL; 683 goto out; 684 } 685 m_freem(control); /* empty control, just free it */ 686 } 687 if (!(flags & PRUS_OOB)) { 688 sbappendstream(&so->so_snd, m); 689 if (nam && tp->t_state < TCPS_SYN_SENT) { 690 /* 691 * Do implied connect if not yet connected, 692 * initialize window to default value, and 693 * initialize maxseg/maxopd using peer's cached 694 * MSS. 695 */ 696#ifdef INET6 697 if (isipv6) 698 error = tcp6_connect(tp, nam, td); 699 else 700#endif /* INET6 */ 701 error = tcp_connect(tp, nam, td); 702 if (error) 703 goto out; 704 tp->snd_wnd = TTCP_CLIENT_SND_WND; 705 tcp_mss(tp, -1); 706 } 707 708 if (flags & PRUS_EOF) { 709 /* 710 * Close the send side of the connection after 711 * the data is sent. 712 */ 713 socantsendmore(so); 714 tp = tcp_usrclosed(tp); 715 } 716 if (tp != NULL) { 717 if (flags & PRUS_MORETOCOME) 718 tp->t_flags |= TF_MORETOCOME; 719 error = tcp_output(tp); 720 if (flags & PRUS_MORETOCOME) 721 tp->t_flags &= ~TF_MORETOCOME; 722 } 723 } else { 724 if (sbspace(&so->so_snd) < -512) { 725 m_freem(m); 726 error = ENOBUFS; 727 goto out; 728 } 729 /* 730 * According to RFC961 (Assigned Protocols), 731 * the urgent pointer points to the last octet 732 * of urgent data. We continue, however, 733 * to consider it to indicate the first octet 734 * of data past the urgent section. 735 * Otherwise, snd_up should be one lower. 736 */ 737 sbappendstream(&so->so_snd, m); 738 if (nam && tp->t_state < TCPS_SYN_SENT) { 739 /* 740 * Do implied connect if not yet connected, 741 * initialize window to default value, and 742 * initialize maxseg/maxopd using peer's cached 743 * MSS. 744 */ 745#ifdef INET6 746 if (isipv6) 747 error = tcp6_connect(tp, nam, td); 748 else 749#endif /* INET6 */ 750 error = tcp_connect(tp, nam, td); 751 if (error) 752 goto out; 753 tp->snd_wnd = TTCP_CLIENT_SND_WND; 754 tcp_mss(tp, -1); 755 } 756 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 757 tp->t_force = 1; 758 error = tcp_output(tp); 759 tp->t_force = 0; 760 } 761 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 762 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 763} 764 765/* 766 * Abort the TCP. 767 */ 768static int 769tcp_usr_abort(struct socket *so) 770{ 771 int s = splnet(); 772 int error = 0; 773 struct inpcb *inp; 774 struct tcpcb *tp; 775 const int inirw = INI_WRITE; 776 777 COMMON_START(); 778 tp = tcp_drop(tp, ECONNABORTED); 779 COMMON_END(PRU_ABORT); 780} 781 782/* 783 * Receive out-of-band data. 784 */ 785static int 786tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 787{ 788 int s = splnet(); 789 int error = 0; 790 struct inpcb *inp; 791 struct tcpcb *tp; 792 const int inirw = INI_READ; 793 794 COMMON_START(); 795 if ((so->so_oobmark == 0 && 796 (so->so_state & SS_RCVATMARK) == 0) || 797 so->so_options & SO_OOBINLINE || 798 tp->t_oobflags & TCPOOB_HADDATA) { 799 error = EINVAL; 800 goto out; 801 } 802 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 803 error = EWOULDBLOCK; 804 goto out; 805 } 806 m->m_len = 1; 807 *mtod(m, caddr_t) = tp->t_iobc; 808 if ((flags & MSG_PEEK) == 0) 809 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 810 COMMON_END(PRU_RCVOOB); 811} 812 813/* xxx - should be const */ 814struct pr_usrreqs tcp_usrreqs = { 815 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 816 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 817 tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd, 818 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 819 tcp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 820}; 821 822#ifdef INET6 823struct pr_usrreqs tcp6_usrreqs = { 824 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 825 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 826 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 827 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 828 in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 829}; 830#endif /* INET6 */ 831 832/* 833 * Common subroutine to open a TCP connection to remote host specified 834 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 835 * port number if needed. Call in_pcbconnect_setup to do the routing and 836 * to choose a local host address (interface). If there is an existing 837 * incarnation of the same connection in TIME-WAIT state and if the remote 838 * host was sending CC options and if the connection duration was < MSL, then 839 * truncate the previous TIME-WAIT state and proceed. 840 * Initialize connection parameters and enter SYN-SENT state. 841 */ 842static int 843tcp_connect(tp, nam, td) 844 register struct tcpcb *tp; 845 struct sockaddr *nam; 846 struct thread *td; 847{ 848 struct inpcb *inp = tp->t_inpcb, *oinp; 849 struct socket *so = inp->inp_socket; 850 struct tcptw *otw; 851 struct rmxp_tao *taop; 852 struct rmxp_tao tao_noncached; 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); 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); 872 if (error && oinp == NULL) 873 return error; 874 if (oinp) { 875 if (oinp != inp && 876 (oinp->inp_vflag & INP_TIMEWAIT) && 877 (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl && 878 otw->cc_recv != 0) { 879 inp->inp_faddr = oinp->inp_faddr; 880 inp->inp_fport = oinp->inp_fport; 881 (void) tcp_twclose(otw, 0); 882 } else 883 return EADDRINUSE; 884 } 885 inp->inp_laddr = laddr; 886 in_pcbrehash(inp); 887 888 /* Compute window scaling to request. */ 889 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 890 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 891 tp->request_r_scale++; 892 893 soisconnecting(so); 894 tcpstat.tcps_connattempt++; 895 tp->t_state = TCPS_SYN_SENT; 896 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 897 tp->iss = tcp_new_isn(tp); 898 tp->t_bw_rtseq = tp->iss; 899 tcp_sendseqinit(tp); 900 901 /* 902 * Generate a CC value for this connection and 903 * check whether CC or CCnew should be used. 904 */ 905 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 906 taop = &tao_noncached; 907 bzero(taop, sizeof(*taop)); 908 } 909 910 tp->cc_send = CC_INC(tcp_ccgen); 911 if (taop->tao_ccsent != 0 && 912 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 913 taop->tao_ccsent = tp->cc_send; 914 } else { 915 taop->tao_ccsent = 0; 916 tp->t_flags |= TF_SENDCCNEW; 917 } 918 919 return 0; 920} 921 922#ifdef INET6 923static int 924tcp6_connect(tp, nam, td) 925 register struct tcpcb *tp; 926 struct sockaddr *nam; 927 struct thread *td; 928{ 929 struct inpcb *inp = tp->t_inpcb, *oinp; 930 struct socket *so = inp->inp_socket; 931 struct tcptw *otw; 932 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 933 struct in6_addr *addr6; 934 struct rmxp_tao *taop; 935 struct rmxp_tao tao_noncached; 936 int error; 937 938 if (inp->inp_lport == 0) { 939 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 940 if (error) 941 return error; 942 } 943 944 /* 945 * Cannot simply call in_pcbconnect, because there might be an 946 * earlier incarnation of this same connection still in 947 * TIME_WAIT state, creating an ADDRINUSE error. 948 */ 949 error = in6_pcbladdr(inp, nam, &addr6); 950 if (error) 951 return error; 952 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 953 &sin6->sin6_addr, sin6->sin6_port, 954 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 955 ? addr6 956 : &inp->in6p_laddr, 957 inp->inp_lport, 0, NULL); 958 if (oinp) { 959 if (oinp != inp && 960 (oinp->inp_vflag & INP_TIMEWAIT) && 961 (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl && 962 otw->cc_recv != 0) { 963 inp->inp_faddr = oinp->inp_faddr; 964 inp->inp_fport = oinp->inp_fport; 965 (void) tcp_twclose(otw, 0); 966 } else 967 return EADDRINUSE; 968 } 969 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 970 inp->in6p_laddr = *addr6; 971 inp->in6p_faddr = sin6->sin6_addr; 972 inp->inp_fport = sin6->sin6_port; 973 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0) 974 inp->in6p_flowinfo = sin6->sin6_flowinfo; 975 in_pcbrehash(inp); 976 977 /* Compute window scaling to request. */ 978 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 979 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 980 tp->request_r_scale++; 981 982 soisconnecting(so); 983 tcpstat.tcps_connattempt++; 984 tp->t_state = TCPS_SYN_SENT; 985 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 986 tp->iss = tcp_new_isn(tp); 987 tp->t_bw_rtseq = tp->iss; 988 tcp_sendseqinit(tp); 989 990 /* 991 * Generate a CC value for this connection and 992 * check whether CC or CCnew should be used. 993 */ 994 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 995 taop = &tao_noncached; 996 bzero(taop, sizeof(*taop)); 997 } 998 999 tp->cc_send = CC_INC(tcp_ccgen); 1000 if (taop->tao_ccsent != 0 && 1001 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 1002 taop->tao_ccsent = tp->cc_send; 1003 } else { 1004 taop->tao_ccsent = 0; 1005 tp->t_flags |= TF_SENDCCNEW; 1006 } 1007 1008 return 0; 1009} 1010#endif /* INET6 */ 1011 1012/* 1013 * The new sockopt interface makes it possible for us to block in the 1014 * copyin/out step (if we take a page fault). Taking a page fault at 1015 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1016 * use TSM, there probably isn't any need for this function to run at 1017 * splnet() any more. This needs more examination.) 1018 */ 1019int 1020tcp_ctloutput(so, sopt) 1021 struct socket *so; 1022 struct sockopt *sopt; 1023{ 1024 int error, opt, optval, s; 1025 struct inpcb *inp; 1026 struct tcpcb *tp; 1027 1028 error = 0; 1029 s = splnet(); /* XXX */ 1030 INP_INFO_RLOCK(&tcbinfo); 1031 inp = sotoinpcb(so); 1032 if (inp == NULL) { 1033 INP_INFO_RUNLOCK(&tcbinfo); 1034 splx(s); 1035 return (ECONNRESET); 1036 } 1037 INP_LOCK(inp); 1038 INP_INFO_RUNLOCK(&tcbinfo); 1039 if (sopt->sopt_level != IPPROTO_TCP) { 1040#ifdef INET6 1041 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1042 error = ip6_ctloutput(so, sopt); 1043 else 1044#endif /* INET6 */ 1045 error = ip_ctloutput(so, sopt); 1046 INP_UNLOCK(inp); 1047 splx(s); 1048 return (error); 1049 } 1050 tp = intotcpcb(inp); 1051 1052 switch (sopt->sopt_dir) { 1053 case SOPT_SET: 1054 switch (sopt->sopt_name) { 1055 case TCP_NODELAY: 1056 case TCP_NOOPT: 1057 error = sooptcopyin(sopt, &optval, sizeof optval, 1058 sizeof optval); 1059 if (error) 1060 break; 1061 1062 switch (sopt->sopt_name) { 1063 case TCP_NODELAY: 1064 opt = TF_NODELAY; 1065 break; 1066 case TCP_NOOPT: 1067 opt = TF_NOOPT; 1068 break; 1069 default: 1070 opt = 0; /* dead code to fool gcc */ 1071 break; 1072 } 1073 1074 if (optval) 1075 tp->t_flags |= opt; 1076 else 1077 tp->t_flags &= ~opt; 1078 break; 1079 1080 case TCP_NOPUSH: 1081 error = sooptcopyin(sopt, &optval, sizeof optval, 1082 sizeof optval); 1083 if (error) 1084 break; 1085 1086 if (optval) 1087 tp->t_flags |= TF_NOPUSH; 1088 else { 1089 tp->t_flags &= ~TF_NOPUSH; 1090 error = tcp_output(tp); 1091 } 1092 break; 1093 1094 case TCP_MAXSEG: 1095 error = sooptcopyin(sopt, &optval, sizeof optval, 1096 sizeof optval); 1097 if (error) 1098 break; 1099 1100 if (optval > 0 && optval <= tp->t_maxseg) 1101 tp->t_maxseg = optval; 1102 else 1103 error = EINVAL; 1104 break; 1105 1106 default: 1107 error = ENOPROTOOPT; 1108 break; 1109 } 1110 break; 1111 1112 case SOPT_GET: 1113 switch (sopt->sopt_name) { 1114 case TCP_NODELAY: 1115 optval = tp->t_flags & TF_NODELAY; 1116 break; 1117 case TCP_MAXSEG: 1118 optval = tp->t_maxseg; 1119 break; 1120 case TCP_NOOPT: 1121 optval = tp->t_flags & TF_NOOPT; 1122 break; 1123 case TCP_NOPUSH: 1124 optval = tp->t_flags & TF_NOPUSH; 1125 break; 1126 default: 1127 error = ENOPROTOOPT; 1128 break; 1129 } 1130 if (error == 0) 1131 error = sooptcopyout(sopt, &optval, sizeof optval); 1132 break; 1133 } 1134 INP_UNLOCK(inp); 1135 splx(s); 1136 return (error); 1137} 1138 1139/* 1140 * tcp_sendspace and tcp_recvspace are the default send and receive window 1141 * sizes, respectively. These are obsolescent (this information should 1142 * be set by the route). 1143 */ 1144u_long tcp_sendspace = 1024*32; 1145SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1146 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1147u_long tcp_recvspace = 1024*64; 1148SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1149 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1150 1151/* 1152 * Attach TCP protocol to socket, allocating 1153 * internet protocol control block, tcp control block, 1154 * bufer space, and entering LISTEN state if to accept connections. 1155 */ 1156static int 1157tcp_attach(so, td) 1158 struct socket *so; 1159 struct thread *td; 1160{ 1161 register struct tcpcb *tp; 1162 struct inpcb *inp; 1163 int error; 1164#ifdef INET6 1165 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1166#endif 1167 1168 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1169 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1170 if (error) 1171 return (error); 1172 } 1173 error = in_pcballoc(so, &tcbinfo, td); 1174 if (error) 1175 return (error); 1176 inp = sotoinpcb(so); 1177#ifdef INET6 1178 if (isipv6) { 1179 inp->inp_vflag |= INP_IPV6; 1180 inp->in6p_hops = -1; /* use kernel default */ 1181 } 1182 else 1183#endif 1184 inp->inp_vflag |= INP_IPV4; 1185 tp = tcp_newtcpcb(inp); 1186 if (tp == 0) { 1187 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1188 1189 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1190#ifdef INET6 1191 if (isipv6) 1192 in6_pcbdetach(inp); 1193 else 1194#endif 1195 in_pcbdetach(inp); 1196 so->so_state |= nofd; 1197 return (ENOBUFS); 1198 } 1199 tp->t_state = TCPS_CLOSED; 1200 return (0); 1201} 1202 1203/* 1204 * Initiate (or continue) disconnect. 1205 * If embryonic state, just send reset (once). 1206 * If in ``let data drain'' option and linger null, just drop. 1207 * Otherwise (hard), mark socket disconnecting and drop 1208 * current input data; switch states based on user close, and 1209 * send segment to peer (with FIN). 1210 */ 1211static struct tcpcb * 1212tcp_disconnect(tp) 1213 register struct tcpcb *tp; 1214{ 1215 struct socket *so = tp->t_inpcb->inp_socket; 1216 1217 if (tp->t_state < TCPS_ESTABLISHED) 1218 tp = tcp_close(tp); 1219 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1220 tp = tcp_drop(tp, 0); 1221 else { 1222 soisdisconnecting(so); 1223 sbflush(&so->so_rcv); 1224 tp = tcp_usrclosed(tp); 1225 if (tp) 1226 (void) tcp_output(tp); 1227 } 1228 return (tp); 1229} 1230 1231/* 1232 * User issued close, and wish to trail through shutdown states: 1233 * if never received SYN, just forget it. If got a SYN from peer, 1234 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1235 * If already got a FIN from peer, then almost done; go to LAST_ACK 1236 * state. In all other cases, have already sent FIN to peer (e.g. 1237 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1238 * for peer to send FIN or not respond to keep-alives, etc. 1239 * We can let the user exit from the close as soon as the FIN is acked. 1240 */ 1241static struct tcpcb * 1242tcp_usrclosed(tp) 1243 register struct tcpcb *tp; 1244{ 1245 1246 switch (tp->t_state) { 1247 1248 case TCPS_CLOSED: 1249 case TCPS_LISTEN: 1250 tp->t_state = TCPS_CLOSED; 1251 tp = tcp_close(tp); 1252 break; 1253 1254 case TCPS_SYN_SENT: 1255 case TCPS_SYN_RECEIVED: 1256 tp->t_flags |= TF_NEEDFIN; 1257 break; 1258 1259 case TCPS_ESTABLISHED: 1260 tp->t_state = TCPS_FIN_WAIT_1; 1261 break; 1262 1263 case TCPS_CLOSE_WAIT: 1264 tp->t_state = TCPS_LAST_ACK; 1265 break; 1266 } 1267 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1268 soisdisconnected(tp->t_inpcb->inp_socket); 1269 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1270 if (tp->t_state == TCPS_FIN_WAIT_2) 1271 callout_reset(tp->tt_2msl, tcp_maxidle, 1272 tcp_timer_2msl, tp); 1273 } 1274 return (tp); 1275} 1276 1277