tcp_usrreq.c revision 97658
1194869Sjamie/* 2194869Sjamie * Copyright (c) 1982, 1986, 1988, 1993 3194869Sjamie * The Regents of the University of California. All rights reserved. 4194869Sjamie * 5194869Sjamie * Redistribution and use in source and binary forms, with or without 6194869Sjamie * modification, are permitted provided that the following conditions 7194869Sjamie * are met: 8194869Sjamie * 1. Redistributions of source code must retain the above copyright 9194869Sjamie * notice, this list of conditions and the following disclaimer. 10194869Sjamie * 2. Redistributions in binary form must reproduce the above copyright 11194869Sjamie * notice, this list of conditions and the following disclaimer in the 12194869Sjamie * documentation and/or other materials provided with the distribution. 13194869Sjamie * 3. All advertising materials mentioning features or use of this software 14194869Sjamie * must display the following acknowledgement: 15194869Sjamie * This product includes software developed by the University of 16194869Sjamie * California, Berkeley and its contributors. 17194869Sjamie * 4. Neither the name of the University nor the names of its contributors 18194869Sjamie * may be used to endorse or promote products derived from this software 19194869Sjamie * without specific prior written permission. 20194869Sjamie * 21194869Sjamie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22194869Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23194869Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24194869Sjamie * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25194869Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26194869Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27194869Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28194869Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29194869Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30194869Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31194869Sjamie * SUCH DAMAGE. 32194869Sjamie * 33194869Sjamie * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 34194869Sjamie * $FreeBSD: head/sys/netinet/tcp_usrreq.c 97658 2002-05-31 11:52:35Z tanimura $ 35194869Sjamie */ 36194869Sjamie 37194869Sjamie#include "opt_ipsec.h" 38194869Sjamie#include "opt_inet6.h" 39194869Sjamie#include "opt_tcpdebug.h" 40194869Sjamie 41194869Sjamie#include <sys/param.h> 42194869Sjamie#include <sys/systm.h> 43194869Sjamie#include <sys/kernel.h> 44194869Sjamie#include <sys/sysctl.h> 45194869Sjamie#include <sys/mbuf.h> 46194869Sjamie#ifdef INET6 47194869Sjamie#include <sys/domain.h> 48194869Sjamie#endif /* INET6 */ 49194869Sjamie#include <sys/socket.h> 50194869Sjamie#include <sys/socketvar.h> 51194869Sjamie#include <sys/protosw.h> 52194869Sjamie#include <sys/proc.h> 53194869Sjamie#include <sys/jail.h> 54194869Sjamie 55194869Sjamie#include <net/if.h> 56194869Sjamie#include <net/route.h> 57194869Sjamie 58194869Sjamie#include <netinet/in.h> 59194869Sjamie#include <netinet/in_systm.h> 60194869Sjamie#ifdef INET6 61194869Sjamie#include <netinet/ip6.h> 62194869Sjamie#endif 63194869Sjamie#include <netinet/in_pcb.h> 64194869Sjamie#ifdef INET6 65194869Sjamie#include <netinet6/in6_pcb.h> 66194869Sjamie#endif 67194869Sjamie#include <netinet/in_var.h> 68194869Sjamie#include <netinet/ip_var.h> 69194869Sjamie#ifdef INET6 70194869Sjamie#include <netinet6/ip6_var.h> 71194869Sjamie#endif 72194869Sjamie#include <netinet/tcp.h> 73194869Sjamie#include <netinet/tcp_fsm.h> 74194869Sjamie#include <netinet/tcp_seq.h> 75194869Sjamie#include <netinet/tcp_timer.h> 76194869Sjamie#include <netinet/tcp_var.h> 77194869Sjamie#include <netinet/tcpip.h> 78194869Sjamie#ifdef TCPDEBUG 79194869Sjamie#include <netinet/tcp_debug.h> 80194869Sjamie#endif 81194869Sjamie 82194869Sjamie#ifdef IPSEC 83194869Sjamie#include <netinet6/ipsec.h> 84194869Sjamie#endif /*IPSEC*/ 85194869Sjamie 86194869Sjamie/* 87194869Sjamie * TCP protocol interface to socket abstraction. 88194869Sjamie */ 89194869Sjamieextern char *tcpstates[]; /* XXX ??? */ 90194869Sjamie 91194869Sjamiestatic int tcp_attach(struct socket *, struct thread *td); 92194869Sjamiestatic int tcp_connect(struct tcpcb *, struct sockaddr *, 93194869Sjamie struct thread *td); 94194869Sjamie#ifdef INET6 95194869Sjamiestatic int tcp6_connect(struct tcpcb *, struct sockaddr *, 96194869Sjamie struct thread *td); 97194869Sjamie#endif /* INET6 */ 98194869Sjamiestatic struct tcpcb * 99194869Sjamie tcp_disconnect(struct tcpcb *); 100194869Sjamiestatic struct tcpcb * 101194869Sjamie tcp_usrclosed(struct tcpcb *); 102194869Sjamie 103194869Sjamie#ifdef TCPDEBUG 104194869Sjamie#define TCPDEBUG0 int ostate = 0 105194869Sjamie#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 106194869Sjamie#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 107194869Sjamie tcp_trace(TA_USER, ostate, tp, 0, 0, req) 108194869Sjamie#else 109194869Sjamie#define TCPDEBUG0 110194869Sjamie#define TCPDEBUG1() 111194869Sjamie#define TCPDEBUG2(req) 112194869Sjamie#endif 113194869Sjamie 114194869Sjamie/* 115194869Sjamie * TCP attaches to socket via pru_attach(), reserving space, 116194869Sjamie * and an internet control block. 117194869Sjamie */ 118194869Sjamiestatic int 119194869Sjamietcp_usr_attach(struct socket *so, int proto, struct thread *td) 120194869Sjamie{ 121194869Sjamie int s = splnet(); 122194869Sjamie int error; 123194869Sjamie struct inpcb *inp = sotoinpcb(so); 124194869Sjamie struct tcpcb *tp = 0; 125194869Sjamie TCPDEBUG0; 126194869Sjamie 127194869Sjamie TCPDEBUG1(); 128194869Sjamie if (inp) { 129194869Sjamie error = EISCONN; 130194869Sjamie goto out; 131194869Sjamie } 132194869Sjamie 133194869Sjamie error = tcp_attach(so, td); 134194869Sjamie if (error) 135194869Sjamie goto out; 136194869Sjamie 137194869Sjamie if ((so->so_options & SO_LINGER) && so->so_linger == 0) 138194869Sjamie so->so_linger = TCP_LINGERTIME; 139194869Sjamie tp = sototcpcb(so); 140194869Sjamieout: 141194869Sjamie TCPDEBUG2(PRU_ATTACH); 142194869Sjamie splx(s); 143194869Sjamie return error; 144194869Sjamie} 145194869Sjamie 146194869Sjamie/* 147194869Sjamie * pru_detach() detaches the TCP protocol from the socket. 148194869Sjamie * If the protocol state is non-embryonic, then can't 149194869Sjamie * do this directly: have to initiate a pru_disconnect(), 150194869Sjamie * which may finish later; embryonic TCB's can just 151194869Sjamie * be discarded here. 152194869Sjamie */ 153194869Sjamiestatic int 154194869Sjamietcp_usr_detach(struct socket *so) 155194869Sjamie{ 156194869Sjamie int s = splnet(); 157194869Sjamie int error = 0; 158194869Sjamie struct inpcb *inp = sotoinpcb(so); 159194869Sjamie struct tcpcb *tp; 160194869Sjamie TCPDEBUG0; 161194869Sjamie 162194869Sjamie if (inp == 0) { 163194869Sjamie splx(s); 164194869Sjamie return EINVAL; /* XXX */ 165194869Sjamie } 166194869Sjamie tp = intotcpcb(inp); 167194869Sjamie TCPDEBUG1(); 168194869Sjamie tp = tcp_disconnect(tp); 169194869Sjamie 170194869Sjamie TCPDEBUG2(PRU_DETACH); 171194869Sjamie splx(s); 172194869Sjamie return error; 173194869Sjamie} 174194869Sjamie 175194869Sjamie#define COMMON_START() TCPDEBUG0; \ 176194869Sjamie do { \ 177194869Sjamie if (inp == 0) { \ 178194869Sjamie splx(s); \ 179194869Sjamie return EINVAL; \ 180194869Sjamie } \ 181194869Sjamie tp = intotcpcb(inp); \ 182194869Sjamie TCPDEBUG1(); \ 183194869Sjamie } while(0) 184194869Sjamie 185194869Sjamie#define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out 186194869Sjamie 187194869Sjamie 188194869Sjamie/* 189194869Sjamie * Give the socket an address. 190194869Sjamie */ 191194869Sjamiestatic int 192194869Sjamietcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 193194869Sjamie{ 194194869Sjamie int s = splnet(); 195194869Sjamie int error = 0; 196194869Sjamie struct inpcb *inp = sotoinpcb(so); 197194869Sjamie struct tcpcb *tp; 198194869Sjamie struct sockaddr_in *sinp; 199194869Sjamie 200194869Sjamie COMMON_START(); 201194869Sjamie 202194869Sjamie /* 203194869Sjamie * Must check for multicast addresses and disallow binding 204194869Sjamie * to them. 205194869Sjamie */ 206194869Sjamie sinp = (struct sockaddr_in *)nam; 207194869Sjamie if (sinp->sin_family == AF_INET && 208194869Sjamie IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 209194869Sjamie error = EAFNOSUPPORT; 210194869Sjamie goto out; 211194869Sjamie } 212194869Sjamie error = in_pcbbind(inp, nam, td); 213194869Sjamie if (error) 214194869Sjamie goto out; 215194869Sjamie COMMON_END(PRU_BIND); 216194869Sjamie 217194869Sjamie} 218194869Sjamie 219194869Sjamie#ifdef INET6 220194869Sjamiestatic int 221194869Sjamietcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 222194869Sjamie{ 223194869Sjamie int s = splnet(); 224194869Sjamie int error = 0; 225194869Sjamie struct inpcb *inp = sotoinpcb(so); 226194869Sjamie struct tcpcb *tp; 227194869Sjamie struct sockaddr_in6 *sin6p; 228194869Sjamie 229194869Sjamie COMMON_START(); 230194869Sjamie 231194869Sjamie /* 232194869Sjamie * Must check for multicast addresses and disallow binding 233194869Sjamie * to them. 234194869Sjamie */ 235194869Sjamie sin6p = (struct sockaddr_in6 *)nam; 236194869Sjamie if (sin6p->sin6_family == AF_INET6 && 237194869Sjamie IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 238194869Sjamie error = EAFNOSUPPORT; 239194869Sjamie goto out; 240194869Sjamie } 241194869Sjamie inp->inp_vflag &= ~INP_IPV4; 242194869Sjamie inp->inp_vflag |= INP_IPV6; 243194869Sjamie if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 244194869Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 245194869Sjamie inp->inp_vflag |= INP_IPV4; 246194869Sjamie else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 247194869Sjamie struct sockaddr_in sin; 248194869Sjamie 249194869Sjamie in6_sin6_2_sin(&sin, sin6p); 250194869Sjamie inp->inp_vflag |= INP_IPV4; 251194869Sjamie inp->inp_vflag &= ~INP_IPV6; 252194869Sjamie error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 253194869Sjamie goto out; 254194869Sjamie } 255194869Sjamie } 256194869Sjamie error = in6_pcbbind(inp, nam, td); 257194869Sjamie if (error) 258194869Sjamie goto out; 259194869Sjamie COMMON_END(PRU_BIND); 260194869Sjamie} 261194869Sjamie#endif /* INET6 */ 262194869Sjamie 263194869Sjamie/* 264194869Sjamie * Prepare to accept connections. 265194869Sjamie */ 266194869Sjamiestatic int 267194869Sjamietcp_usr_listen(struct socket *so, struct thread *td) 268194869Sjamie{ 269194869Sjamie int s = splnet(); 270194869Sjamie int error = 0; 271194869Sjamie struct inpcb *inp = sotoinpcb(so); 272194869Sjamie struct tcpcb *tp; 273194869Sjamie 274194869Sjamie COMMON_START(); 275194869Sjamie if (inp->inp_lport == 0) 276194869Sjamie error = in_pcbbind(inp, (struct sockaddr *)0, td); 277194869Sjamie if (error == 0) 278194869Sjamie tp->t_state = TCPS_LISTEN; 279194869Sjamie COMMON_END(PRU_LISTEN); 280194869Sjamie} 281194869Sjamie 282194869Sjamie#ifdef INET6 283194869Sjamiestatic int 284194869Sjamietcp6_usr_listen(struct socket *so, struct thread *td) 285194869Sjamie{ 286194869Sjamie int s = splnet(); 287194869Sjamie int error = 0; 288194869Sjamie struct inpcb *inp = sotoinpcb(so); 289194869Sjamie struct tcpcb *tp; 290194869Sjamie 291194869Sjamie COMMON_START(); 292194869Sjamie if (inp->inp_lport == 0) { 293194869Sjamie inp->inp_vflag &= ~INP_IPV4; 294194869Sjamie if (ip6_mapped_addr_on && 295194869Sjamie (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 296194869Sjamie inp->inp_vflag |= INP_IPV4; 297194869Sjamie error = in6_pcbbind(inp, (struct sockaddr *)0, td); 298194869Sjamie } 299194869Sjamie if (error == 0) 300194869Sjamie tp->t_state = TCPS_LISTEN; 301194869Sjamie COMMON_END(PRU_LISTEN); 302194869Sjamie} 303194869Sjamie#endif /* INET6 */ 304194869Sjamie 305194869Sjamie/* 306194869Sjamie * Initiate connection to peer. 307194869Sjamie * Create a template for use in transmissions on this connection. 308194869Sjamie * Enter SYN_SENT state, and mark socket as connecting. 309194869Sjamie * Start keep-alive timer, and seed output sequence space. 310194869Sjamie * Send initial segment on connection. 311194869Sjamie */ 312194869Sjamiestatic int 313194869Sjamietcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 314194869Sjamie{ 315194869Sjamie int s = splnet(); 316194869Sjamie int error = 0; 317194869Sjamie struct inpcb *inp = sotoinpcb(so); 318194869Sjamie struct tcpcb *tp; 319194869Sjamie struct sockaddr_in *sinp; 320194869Sjamie 321194869Sjamie COMMON_START(); 322194869Sjamie 323194869Sjamie /* 324194869Sjamie * Must disallow TCP ``connections'' to multicast addresses. 325194869Sjamie */ 326194869Sjamie sinp = (struct sockaddr_in *)nam; 327194869Sjamie if (sinp->sin_family == AF_INET 328194869Sjamie && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 329194869Sjamie error = EAFNOSUPPORT; 330194869Sjamie goto out; 331194869Sjamie } 332194869Sjamie 333194869Sjamie if (td && jailed(td->td_ucred)) 334194869Sjamie prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 335194869Sjamie 336194869Sjamie if ((error = tcp_connect(tp, nam, td)) != 0) 337194869Sjamie goto out; 338194869Sjamie error = tcp_output(tp); 339194869Sjamie COMMON_END(PRU_CONNECT); 340194869Sjamie} 341194869Sjamie 342194869Sjamie#ifdef INET6 343194869Sjamiestatic int 344194869Sjamietcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 345194869Sjamie{ 346194869Sjamie int s = splnet(); 347194869Sjamie int error = 0; 348194869Sjamie struct inpcb *inp = sotoinpcb(so); 349194869Sjamie struct tcpcb *tp; 350194869Sjamie struct sockaddr_in6 *sin6p; 351194869Sjamie 352194869Sjamie COMMON_START(); 353194869Sjamie 354194869Sjamie /* 355194869Sjamie * Must disallow TCP ``connections'' to multicast addresses. 356194869Sjamie */ 357194869Sjamie sin6p = (struct sockaddr_in6 *)nam; 358194869Sjamie if (sin6p->sin6_family == AF_INET6 359194869Sjamie && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 360194869Sjamie error = EAFNOSUPPORT; 361194869Sjamie goto out; 362194869Sjamie } 363194869Sjamie 364194869Sjamie if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 365194869Sjamie struct sockaddr_in sin; 366194869Sjamie 367194869Sjamie if (!ip6_mapped_addr_on || 368194869Sjamie (inp->inp_flags & IN6P_IPV6_V6ONLY)) 369194869Sjamie return(EINVAL); 370194869Sjamie 371194869Sjamie in6_sin6_2_sin(&sin, sin6p); 372194869Sjamie inp->inp_vflag |= INP_IPV4; 373194869Sjamie inp->inp_vflag &= ~INP_IPV6; 374194869Sjamie if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 375194869Sjamie goto out; 376194869Sjamie error = tcp_output(tp); 377194869Sjamie goto out; 378194869Sjamie } 379194869Sjamie inp->inp_vflag &= ~INP_IPV4; 380194869Sjamie inp->inp_vflag |= INP_IPV6; 381194869Sjamie inp->inp_inc.inc_isipv6 = 1; 382194869Sjamie if ((error = tcp6_connect(tp, nam, td)) != 0) 383194869Sjamie goto out; 384194869Sjamie error = tcp_output(tp); 385194869Sjamie COMMON_END(PRU_CONNECT); 386194869Sjamie} 387194869Sjamie#endif /* INET6 */ 388194869Sjamie 389194869Sjamie/* 390194869Sjamie * Initiate disconnect from peer. 391194869Sjamie * If connection never passed embryonic stage, just drop; 392194869Sjamie * else if don't need to let data drain, then can just drop anyways, 393194869Sjamie * else have to begin TCP shutdown process: mark socket disconnecting, 394194869Sjamie * drain unread data, state switch to reflect user close, and 395194869Sjamie * send segment (e.g. FIN) to peer. Socket will be really disconnected 396194869Sjamie * when peer sends FIN and acks ours. 397194869Sjamie * 398194869Sjamie * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 399194869Sjamie */ 400194869Sjamiestatic int 401194869Sjamietcp_usr_disconnect(struct socket *so) 402194869Sjamie{ 403194869Sjamie int s = splnet(); 404194869Sjamie int error = 0; 405194869Sjamie struct inpcb *inp = sotoinpcb(so); 406194869Sjamie struct tcpcb *tp; 407194869Sjamie 408194869Sjamie COMMON_START(); 409194869Sjamie tp = tcp_disconnect(tp); 410194869Sjamie COMMON_END(PRU_DISCONNECT); 411194869Sjamie} 412194869Sjamie 413194869Sjamie/* 414194869Sjamie * Accept a connection. Essentially all the work is 415194869Sjamie * done at higher levels; just return the address 416194869Sjamie * of the peer, storing through addr. 417194869Sjamie */ 418194869Sjamiestatic int 419194869Sjamietcp_usr_accept(struct socket *so, struct sockaddr **nam) 420194869Sjamie{ 421194869Sjamie int s = splnet(); 422194869Sjamie int error = 0; 423194869Sjamie struct inpcb *inp = sotoinpcb(so); 424194869Sjamie struct tcpcb *tp = NULL; 425194869Sjamie TCPDEBUG0; 426194869Sjamie 427194869Sjamie if (so->so_state & SS_ISDISCONNECTED) { 428194869Sjamie error = ECONNABORTED; 429194869Sjamie goto out; 430194869Sjamie } 431194869Sjamie if (inp == 0) { 432194869Sjamie splx(s); 433194869Sjamie return (EINVAL); 434194869Sjamie } 435194869Sjamie tp = intotcpcb(inp); 436194869Sjamie TCPDEBUG1(); 437194869Sjamie in_setpeeraddr(so, nam); 438194869Sjamie COMMON_END(PRU_ACCEPT); 439194869Sjamie} 440194869Sjamie 441194869Sjamie#ifdef INET6 442194869Sjamiestatic int 443194869Sjamietcp6_usr_accept(struct socket *so, struct sockaddr **nam) 444194869Sjamie{ 445194869Sjamie int s = splnet(); 446194869Sjamie int error = 0; 447194869Sjamie struct inpcb *inp = sotoinpcb(so); 448194869Sjamie struct tcpcb *tp = NULL; 449194869Sjamie TCPDEBUG0; 450194869Sjamie 451194869Sjamie if (so->so_state & SS_ISDISCONNECTED) { 452194869Sjamie error = ECONNABORTED; 453194869Sjamie goto out; 454194869Sjamie } 455194869Sjamie if (inp == 0) { 456194869Sjamie splx(s); 457194869Sjamie return (EINVAL); 458194869Sjamie } 459194869Sjamie tp = intotcpcb(inp); 460194869Sjamie TCPDEBUG1(); 461194869Sjamie in6_mapped_peeraddr(so, nam); 462194869Sjamie COMMON_END(PRU_ACCEPT); 463194869Sjamie} 464194869Sjamie#endif /* INET6 */ 465194869Sjamie/* 466194869Sjamie * Mark the connection as being incapable of further output. 467194869Sjamie */ 468194869Sjamiestatic int 469194869Sjamietcp_usr_shutdown(struct socket *so) 470194869Sjamie{ 471194869Sjamie int s = splnet(); 472194869Sjamie int error = 0; 473194869Sjamie struct inpcb *inp = sotoinpcb(so); 474194869Sjamie struct tcpcb *tp; 475194869Sjamie 476194869Sjamie COMMON_START(); 477194869Sjamie socantsendmore(so); 478194869Sjamie tp = tcp_usrclosed(tp); 479194869Sjamie if (tp) 480194869Sjamie error = tcp_output(tp); 481194869Sjamie COMMON_END(PRU_SHUTDOWN); 482194869Sjamie} 483194869Sjamie 484194869Sjamie/* 485194869Sjamie * After a receive, possibly send window update to peer. 486194869Sjamie */ 487194869Sjamiestatic int 488194869Sjamietcp_usr_rcvd(struct socket *so, int flags) 489194869Sjamie{ 490194869Sjamie int s = splnet(); 491194869Sjamie int error = 0; 492194869Sjamie struct inpcb *inp = sotoinpcb(so); 493194869Sjamie struct tcpcb *tp; 494194869Sjamie 495194869Sjamie COMMON_START(); 496194869Sjamie tcp_output(tp); 497194869Sjamie COMMON_END(PRU_RCVD); 498194869Sjamie} 499194869Sjamie 500194869Sjamie/* 501194869Sjamie * Do a send by putting data in output queue and updating urgent 502194869Sjamie * marker if URG set. Possibly send more data. Unlike the other 503194869Sjamie * pru_*() routines, the mbuf chains are our responsibility. We 504194869Sjamie * must either enqueue them or free them. The other pru_* routines 505194869Sjamie * generally are caller-frees. 506194869Sjamie */ 507194869Sjamiestatic int 508194869Sjamietcp_usr_send(struct socket *so, int flags, struct mbuf *m, 509194869Sjamie struct sockaddr *nam, struct mbuf *control, struct thread *td) 510194869Sjamie{ 511194869Sjamie int s = splnet(); 512194869Sjamie int error = 0; 513194869Sjamie struct inpcb *inp = sotoinpcb(so); 514194869Sjamie struct tcpcb *tp; 515194869Sjamie#ifdef INET6 516194869Sjamie int isipv6; 517194869Sjamie#endif 518194869Sjamie TCPDEBUG0; 519194869Sjamie 520194869Sjamie if (inp == NULL) { 521194869Sjamie /* 522194869Sjamie * OOPS! we lost a race, the TCP session got reset after 523194869Sjamie * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 524194869Sjamie * network interrupt in the non-splnet() section of sosend(). 525194869Sjamie */ 526194869Sjamie if (m) 527194869Sjamie m_freem(m); 528194869Sjamie if (control) 529194869Sjamie m_freem(control); 530194869Sjamie error = ECONNRESET; /* XXX EPIPE? */ 531194869Sjamie tp = NULL; 532194869Sjamie TCPDEBUG1(); 533194869Sjamie goto out; 534194869Sjamie } 535194869Sjamie#ifdef INET6 536194869Sjamie isipv6 = nam && nam->sa_family == AF_INET6; 537194869Sjamie#endif /* INET6 */ 538194869Sjamie tp = intotcpcb(inp); 539194869Sjamie TCPDEBUG1(); 540194869Sjamie if (control) { 541194869Sjamie /* TCP doesn't do control messages (rights, creds, etc) */ 542194869Sjamie if (control->m_len) { 543194869Sjamie m_freem(control); 544194869Sjamie if (m) 545194869Sjamie m_freem(m); 546194869Sjamie error = EINVAL; 547194869Sjamie goto out; 548194869Sjamie } 549194869Sjamie m_freem(control); /* empty control, just free it */ 550194869Sjamie } 551194869Sjamie if(!(flags & PRUS_OOB)) { 552194869Sjamie sbappend(&so->so_snd, m); 553194869Sjamie if (nam && tp->t_state < TCPS_SYN_SENT) { 554194869Sjamie /* 555194869Sjamie * Do implied connect if not yet connected, 556194869Sjamie * initialize window to default value, and 557194869Sjamie * initialize maxseg/maxopd using peer's cached 558194869Sjamie * MSS. 559194869Sjamie */ 560194869Sjamie#ifdef INET6 561194869Sjamie if (isipv6) 562194869Sjamie error = tcp6_connect(tp, nam, td); 563194869Sjamie else 564194869Sjamie#endif /* INET6 */ 565194869Sjamie error = tcp_connect(tp, nam, td); 566194869Sjamie if (error) 567194869Sjamie goto out; 568194869Sjamie tp->snd_wnd = TTCP_CLIENT_SND_WND; 569194869Sjamie tcp_mss(tp, -1); 570194869Sjamie } 571194869Sjamie 572194869Sjamie if (flags & PRUS_EOF) { 573194869Sjamie /* 574194869Sjamie * Close the send side of the connection after 575194869Sjamie * the data is sent. 576194869Sjamie */ 577194869Sjamie socantsendmore(so); 578194869Sjamie tp = tcp_usrclosed(tp); 579194869Sjamie } 580194869Sjamie if (tp != NULL) { 581194869Sjamie if (flags & PRUS_MORETOCOME) 582194869Sjamie tp->t_flags |= TF_MORETOCOME; 583194869Sjamie error = tcp_output(tp); 584194869Sjamie if (flags & PRUS_MORETOCOME) 585194869Sjamie tp->t_flags &= ~TF_MORETOCOME; 586194869Sjamie } 587194869Sjamie } else { 588194869Sjamie if (sbspace(&so->so_snd) < -512) { 589194869Sjamie m_freem(m); 590194869Sjamie error = ENOBUFS; 591194869Sjamie goto out; 592194869Sjamie } 593194869Sjamie /* 594194869Sjamie * According to RFC961 (Assigned Protocols), 595194869Sjamie * the urgent pointer points to the last octet 596194869Sjamie * of urgent data. We continue, however, 597194869Sjamie * to consider it to indicate the first octet 598194869Sjamie * of data past the urgent section. 599194869Sjamie * Otherwise, snd_up should be one lower. 600194869Sjamie */ 601194869Sjamie sbappend(&so->so_snd, m); 602194869Sjamie if (nam && tp->t_state < TCPS_SYN_SENT) { 603194869Sjamie /* 604194869Sjamie * Do implied connect if not yet connected, 605194869Sjamie * initialize window to default value, and 606194869Sjamie * initialize maxseg/maxopd using peer's cached 607194869Sjamie * MSS. 608194869Sjamie */ 609194869Sjamie#ifdef INET6 610194869Sjamie if (isipv6) 611194869Sjamie error = tcp6_connect(tp, nam, td); 612194869Sjamie else 613194869Sjamie#endif /* INET6 */ 614194869Sjamie error = tcp_connect(tp, nam, td); 615194869Sjamie if (error) 616194869Sjamie goto out; 617194869Sjamie tp->snd_wnd = TTCP_CLIENT_SND_WND; 618194869Sjamie tcp_mss(tp, -1); 619194869Sjamie } 620194869Sjamie tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 621194869Sjamie tp->t_force = 1; 622194869Sjamie error = tcp_output(tp); 623194869Sjamie tp->t_force = 0; 624194869Sjamie } 625194869Sjamie COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 626194869Sjamie ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 627194869Sjamie} 628194869Sjamie 629194869Sjamie/* 630194869Sjamie * Abort the TCP. 631194869Sjamie */ 632194869Sjamiestatic int 633194869Sjamietcp_usr_abort(struct socket *so) 634194869Sjamie{ 635194869Sjamie int s = splnet(); 636194869Sjamie int error = 0; 637194869Sjamie struct inpcb *inp = sotoinpcb(so); 638194869Sjamie struct tcpcb *tp; 639194869Sjamie 640194869Sjamie COMMON_START(); 641194869Sjamie tp = tcp_drop(tp, ECONNABORTED); 642194869Sjamie COMMON_END(PRU_ABORT); 643194869Sjamie} 644194869Sjamie 645194869Sjamie/* 646194869Sjamie * Receive out-of-band data. 647194869Sjamie */ 648194869Sjamiestatic int 649194869Sjamietcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 650194869Sjamie{ 651194869Sjamie int s = splnet(); 652194869Sjamie int error = 0; 653194869Sjamie struct inpcb *inp = sotoinpcb(so); 654194869Sjamie struct tcpcb *tp; 655194869Sjamie 656194869Sjamie COMMON_START(); 657194869Sjamie if ((so->so_oobmark == 0 && 658194869Sjamie (so->so_state & SS_RCVATMARK) == 0) || 659194869Sjamie so->so_options & SO_OOBINLINE || 660194869Sjamie tp->t_oobflags & TCPOOB_HADDATA) { 661194869Sjamie error = EINVAL; 662194869Sjamie goto out; 663194869Sjamie } 664194869Sjamie if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 665194869Sjamie error = EWOULDBLOCK; 666194869Sjamie goto out; 667194869Sjamie } 668194869Sjamie m->m_len = 1; 669194869Sjamie *mtod(m, caddr_t) = tp->t_iobc; 670194869Sjamie if ((flags & MSG_PEEK) == 0) 671194869Sjamie tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 672194869Sjamie COMMON_END(PRU_RCVOOB); 673194869Sjamie} 674194869Sjamie 675194869Sjamie/* xxx - should be const */ 676194869Sjamiestruct pr_usrreqs tcp_usrreqs = { 677194869Sjamie tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 678194869Sjamie tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 679194869Sjamie tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd, 680194869Sjamie tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 681194869Sjamie in_setsockaddr, sosend, soreceive, sopoll 682194869Sjamie}; 683194869Sjamie 684194869Sjamie#ifdef INET6 685194869Sjamiestruct pr_usrreqs tcp6_usrreqs = { 686194869Sjamie tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 687194869Sjamie tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 688194869Sjamie tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 689194869Sjamie tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 690194869Sjamie in6_mapped_sockaddr, sosend, soreceive, sopoll 691194869Sjamie}; 692194869Sjamie#endif /* INET6 */ 693194869Sjamie 694194869Sjamie/* 695194869Sjamie * Common subroutine to open a TCP connection to remote host specified 696194869Sjamie * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 697194869Sjamie * port number if needed. Call in_pcbladdr to do the routing and to choose 698194869Sjamie * a local host address (interface). If there is an existing incarnation 699194869Sjamie * of the same connection in TIME-WAIT state and if the remote host was 700194869Sjamie * sending CC options and if the connection duration was < MSL, then 701194869Sjamie * truncate the previous TIME-WAIT state and proceed. 702194869Sjamie * Initialize connection parameters and enter SYN-SENT state. 703194869Sjamie */ 704194869Sjamiestatic int 705194869Sjamietcp_connect(tp, nam, td) 706194869Sjamie register struct tcpcb *tp; 707194869Sjamie struct sockaddr *nam; 708194869Sjamie struct thread *td; 709194869Sjamie{ 710194869Sjamie struct inpcb *inp = tp->t_inpcb, *oinp; 711194869Sjamie struct socket *so = inp->inp_socket; 712194869Sjamie struct tcpcb *otp; 713194869Sjamie struct sockaddr_in *sin = (struct sockaddr_in *)nam; 714194869Sjamie struct sockaddr_in *ifaddr; 715194869Sjamie struct rmxp_tao *taop; 716194869Sjamie struct rmxp_tao tao_noncached; 717194869Sjamie int error; 718194869Sjamie 719194869Sjamie if (inp->inp_lport == 0) { 720194869Sjamie error = in_pcbbind(inp, (struct sockaddr *)0, td); 721194869Sjamie if (error) 722194869Sjamie return error; 723194869Sjamie } 724194869Sjamie 725194869Sjamie /* 726194869Sjamie * Cannot simply call in_pcbconnect, because there might be an 727194869Sjamie * earlier incarnation of this same connection still in 728194869Sjamie * TIME_WAIT state, creating an ADDRINUSE error. 729194869Sjamie */ 730194869Sjamie error = in_pcbladdr(inp, nam, &ifaddr); 731194869Sjamie if (error) 732194869Sjamie return error; 733194869Sjamie oinp = in_pcblookup_hash(inp->inp_pcbinfo, 734194869Sjamie sin->sin_addr, sin->sin_port, 735194869Sjamie inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 736194869Sjamie : ifaddr->sin_addr, 737194869Sjamie inp->inp_lport, 0, NULL); 738194869Sjamie if (oinp) { 739194869Sjamie if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 740194869Sjamie otp->t_state == TCPS_TIME_WAIT && 741194869Sjamie (ticks - otp->t_starttime) < tcp_msl && 742194869Sjamie (otp->t_flags & TF_RCVD_CC)) 743194869Sjamie otp = tcp_close(otp); 744194869Sjamie else 745194869Sjamie return EADDRINUSE; 746194869Sjamie } 747194869Sjamie if (inp->inp_laddr.s_addr == INADDR_ANY) 748194869Sjamie inp->inp_laddr = ifaddr->sin_addr; 749194869Sjamie inp->inp_faddr = sin->sin_addr; 750194869Sjamie inp->inp_fport = sin->sin_port; 751194869Sjamie in_pcbrehash(inp); 752194869Sjamie 753194869Sjamie /* Compute window scaling to request. */ 754194869Sjamie while (tp->request_r_scale < TCP_MAX_WINSHIFT && 755194869Sjamie (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 756194869Sjamie tp->request_r_scale++; 757194869Sjamie 758194869Sjamie soisconnecting(so); 759194869Sjamie tcpstat.tcps_connattempt++; 760194869Sjamie tp->t_state = TCPS_SYN_SENT; 761194869Sjamie callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 762194869Sjamie tp->iss = tcp_new_isn(tp); 763194869Sjamie tcp_sendseqinit(tp); 764194869Sjamie 765194869Sjamie /* 766194869Sjamie * Generate a CC value for this connection and 767194869Sjamie * check whether CC or CCnew should be used. 768194869Sjamie */ 769194869Sjamie if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 770194869Sjamie taop = &tao_noncached; 771194869Sjamie bzero(taop, sizeof(*taop)); 772194869Sjamie } 773194869Sjamie 774194869Sjamie tp->cc_send = CC_INC(tcp_ccgen); 775194869Sjamie if (taop->tao_ccsent != 0 && 776194869Sjamie CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 777194869Sjamie taop->tao_ccsent = tp->cc_send; 778194869Sjamie } else { 779194869Sjamie taop->tao_ccsent = 0; 780194869Sjamie tp->t_flags |= TF_SENDCCNEW; 781194869Sjamie } 782194869Sjamie 783194869Sjamie return 0; 784194869Sjamie} 785194869Sjamie 786194869Sjamie#ifdef INET6 787194869Sjamiestatic int 788194869Sjamietcp6_connect(tp, nam, td) 789194869Sjamie register struct tcpcb *tp; 790194869Sjamie struct sockaddr *nam; 791194869Sjamie struct thread *td; 792194869Sjamie{ 793194869Sjamie struct inpcb *inp = tp->t_inpcb, *oinp; 794194869Sjamie struct socket *so = inp->inp_socket; 795194869Sjamie struct tcpcb *otp; 796194869Sjamie struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 797194869Sjamie struct in6_addr *addr6; 798194869Sjamie struct rmxp_tao *taop; 799194869Sjamie struct rmxp_tao tao_noncached; 800194869Sjamie int error; 801194869Sjamie 802194869Sjamie if (inp->inp_lport == 0) { 803194869Sjamie error = in6_pcbbind(inp, (struct sockaddr *)0, td); 804194869Sjamie if (error) 805194869Sjamie return error; 806194869Sjamie } 807194869Sjamie 808194869Sjamie /* 809194869Sjamie * Cannot simply call in_pcbconnect, because there might be an 810194869Sjamie * earlier incarnation of this same connection still in 811194869Sjamie * TIME_WAIT state, creating an ADDRINUSE error. 812194869Sjamie */ 813194869Sjamie error = in6_pcbladdr(inp, nam, &addr6); 814194869Sjamie if (error) 815194869Sjamie return error; 816194869Sjamie oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 817194869Sjamie &sin6->sin6_addr, sin6->sin6_port, 818194869Sjamie IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 819194869Sjamie ? addr6 820194869Sjamie : &inp->in6p_laddr, 821194869Sjamie inp->inp_lport, 0, NULL); 822194869Sjamie if (oinp) { 823194869Sjamie if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 824194869Sjamie otp->t_state == TCPS_TIME_WAIT && 825194869Sjamie (ticks - otp->t_starttime) < tcp_msl && 826194869Sjamie (otp->t_flags & TF_RCVD_CC)) 827194869Sjamie otp = tcp_close(otp); 828194869Sjamie else 829194869Sjamie return EADDRINUSE; 830194869Sjamie } 831194869Sjamie if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 832194869Sjamie inp->in6p_laddr = *addr6; 833194869Sjamie inp->in6p_faddr = sin6->sin6_addr; 834194869Sjamie inp->inp_fport = sin6->sin6_port; 835194869Sjamie if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL) 836194869Sjamie inp->in6p_flowinfo = sin6->sin6_flowinfo; 837194869Sjamie in_pcbrehash(inp); 838194869Sjamie 839194869Sjamie /* Compute window scaling to request. */ 840194869Sjamie while (tp->request_r_scale < TCP_MAX_WINSHIFT && 841194869Sjamie (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 842194869Sjamie tp->request_r_scale++; 843194869Sjamie 844194869Sjamie soisconnecting(so); 845194869Sjamie tcpstat.tcps_connattempt++; 846194869Sjamie tp->t_state = TCPS_SYN_SENT; 847194869Sjamie callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 848194869Sjamie tp->iss = tcp_new_isn(tp); 849194869Sjamie tcp_sendseqinit(tp); 850194869Sjamie 851194869Sjamie /* 852194869Sjamie * Generate a CC value for this connection and 853194869Sjamie * check whether CC or CCnew should be used. 854194869Sjamie */ 855194869Sjamie if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 856194869Sjamie taop = &tao_noncached; 857194869Sjamie bzero(taop, sizeof(*taop)); 858194869Sjamie } 859194869Sjamie 860194869Sjamie tp->cc_send = CC_INC(tcp_ccgen); 861194869Sjamie if (taop->tao_ccsent != 0 && 862194869Sjamie CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 863194869Sjamie taop->tao_ccsent = tp->cc_send; 864194869Sjamie } else { 865194869Sjamie taop->tao_ccsent = 0; 866194869Sjamie tp->t_flags |= TF_SENDCCNEW; 867194869Sjamie } 868194869Sjamie 869194869Sjamie return 0; 870194869Sjamie} 871194869Sjamie#endif /* INET6 */ 872194869Sjamie 873194869Sjamie/* 874194869Sjamie * The new sockopt interface makes it possible for us to block in the 875194869Sjamie * copyin/out step (if we take a page fault). Taking a page fault at 876194869Sjamie * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 877194869Sjamie * use TSM, there probably isn't any need for this function to run at 878194869Sjamie * splnet() any more. This needs more examination.) 879194869Sjamie */ 880194869Sjamieint 881194869Sjamietcp_ctloutput(so, sopt) 882194869Sjamie struct socket *so; 883194869Sjamie struct sockopt *sopt; 884194869Sjamie{ 885194869Sjamie int error, opt, optval, s; 886194869Sjamie struct inpcb *inp; 887194869Sjamie struct tcpcb *tp; 888194869Sjamie 889194869Sjamie error = 0; 890194869Sjamie s = splnet(); /* XXX */ 891194869Sjamie inp = sotoinpcb(so); 892194869Sjamie if (inp == NULL) { 893194869Sjamie splx(s); 894194869Sjamie return (ECONNRESET); 895194869Sjamie } 896194869Sjamie if (sopt->sopt_level != IPPROTO_TCP) { 897194869Sjamie#ifdef INET6 898194869Sjamie if (INP_CHECK_SOCKAF(so, AF_INET6)) 899194869Sjamie error = ip6_ctloutput(so, sopt); 900194869Sjamie else 901194869Sjamie#endif /* INET6 */ 902194869Sjamie error = ip_ctloutput(so, sopt); 903194869Sjamie splx(s); 904194869Sjamie return (error); 905194869Sjamie } 906194869Sjamie tp = intotcpcb(inp); 907194869Sjamie 908194869Sjamie switch (sopt->sopt_dir) { 909194869Sjamie case SOPT_SET: 910194869Sjamie switch (sopt->sopt_name) { 911194869Sjamie case TCP_NODELAY: 912194869Sjamie case TCP_NOOPT: 913194869Sjamie error = sooptcopyin(sopt, &optval, sizeof optval, 914194869Sjamie sizeof optval); 915194869Sjamie if (error) 916194869Sjamie break; 917194869Sjamie 918194869Sjamie switch (sopt->sopt_name) { 919194869Sjamie case TCP_NODELAY: 920194869Sjamie opt = TF_NODELAY; 921194869Sjamie break; 922194869Sjamie case TCP_NOOPT: 923194869Sjamie opt = TF_NOOPT; 924194869Sjamie break; 925194869Sjamie default: 926194869Sjamie opt = 0; /* dead code to fool gcc */ 927194869Sjamie break; 928194869Sjamie } 929194869Sjamie 930194869Sjamie if (optval) 931194869Sjamie tp->t_flags |= opt; 932194869Sjamie else 933194869Sjamie tp->t_flags &= ~opt; 934194869Sjamie break; 935194869Sjamie 936194869Sjamie case TCP_NOPUSH: 937194869Sjamie error = sooptcopyin(sopt, &optval, sizeof optval, 938194869Sjamie sizeof optval); 939194869Sjamie if (error) 940194869Sjamie break; 941194869Sjamie 942194869Sjamie if (optval) 943194869Sjamie tp->t_flags |= TF_NOPUSH; 944194869Sjamie else { 945194869Sjamie tp->t_flags &= ~TF_NOPUSH; 946194869Sjamie error = tcp_output(tp); 947194869Sjamie } 948194869Sjamie break; 949194869Sjamie 950194869Sjamie case TCP_MAXSEG: 951194869Sjamie error = sooptcopyin(sopt, &optval, sizeof optval, 952194869Sjamie sizeof optval); 953194869Sjamie if (error) 954194869Sjamie break; 955194869Sjamie 956194869Sjamie if (optval > 0 && optval <= tp->t_maxseg) 957194869Sjamie tp->t_maxseg = optval; 958194869Sjamie else 959194869Sjamie error = EINVAL; 960194869Sjamie break; 961194869Sjamie 962194869Sjamie default: 963194869Sjamie error = ENOPROTOOPT; 964194869Sjamie break; 965194869Sjamie } 966194869Sjamie break; 967194869Sjamie 968194869Sjamie case SOPT_GET: 969194869Sjamie switch (sopt->sopt_name) { 970194869Sjamie case TCP_NODELAY: 971194869Sjamie optval = tp->t_flags & TF_NODELAY; 972194869Sjamie break; 973 case TCP_MAXSEG: 974 optval = tp->t_maxseg; 975 break; 976 case TCP_NOOPT: 977 optval = tp->t_flags & TF_NOOPT; 978 break; 979 case TCP_NOPUSH: 980 optval = tp->t_flags & TF_NOPUSH; 981 break; 982 default: 983 error = ENOPROTOOPT; 984 break; 985 } 986 if (error == 0) 987 error = sooptcopyout(sopt, &optval, sizeof optval); 988 break; 989 } 990 splx(s); 991 return (error); 992} 993 994/* 995 * tcp_sendspace and tcp_recvspace are the default send and receive window 996 * sizes, respectively. These are obsolescent (this information should 997 * be set by the route). 998 */ 999u_long tcp_sendspace = 1024*32; 1000SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1001 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1002u_long tcp_recvspace = 1024*64; 1003SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1004 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1005 1006/* 1007 * Attach TCP protocol to socket, allocating 1008 * internet protocol control block, tcp control block, 1009 * bufer space, and entering LISTEN state if to accept connections. 1010 */ 1011static int 1012tcp_attach(so, td) 1013 struct socket *so; 1014 struct thread *td; 1015{ 1016 register struct tcpcb *tp; 1017 struct inpcb *inp; 1018 int error; 1019#ifdef INET6 1020 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL; 1021#endif 1022 1023 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1024 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1025 if (error) 1026 return (error); 1027 } 1028 error = in_pcballoc(so, &tcbinfo, td); 1029 if (error) 1030 return (error); 1031 inp = sotoinpcb(so); 1032#ifdef INET6 1033 if (isipv6) { 1034 inp->inp_vflag |= INP_IPV6; 1035 inp->in6p_hops = -1; /* use kernel default */ 1036 } 1037 else 1038#endif 1039 inp->inp_vflag |= INP_IPV4; 1040 tp = tcp_newtcpcb(inp); 1041 if (tp == 0) { 1042 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1043 1044 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1045#ifdef INET6 1046 if (isipv6) 1047 in6_pcbdetach(inp); 1048 else 1049#endif 1050 in_pcbdetach(inp); 1051 so->so_state |= nofd; 1052 return (ENOBUFS); 1053 } 1054 tp->t_state = TCPS_CLOSED; 1055 return (0); 1056} 1057 1058/* 1059 * Initiate (or continue) disconnect. 1060 * If embryonic state, just send reset (once). 1061 * If in ``let data drain'' option and linger null, just drop. 1062 * Otherwise (hard), mark socket disconnecting and drop 1063 * current input data; switch states based on user close, and 1064 * send segment to peer (with FIN). 1065 */ 1066static struct tcpcb * 1067tcp_disconnect(tp) 1068 register struct tcpcb *tp; 1069{ 1070 struct socket *so = tp->t_inpcb->inp_socket; 1071 1072 if (tp->t_state < TCPS_ESTABLISHED) 1073 tp = tcp_close(tp); 1074 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1075 tp = tcp_drop(tp, 0); 1076 else { 1077 soisdisconnecting(so); 1078 sbflush(&so->so_rcv); 1079 tp = tcp_usrclosed(tp); 1080 if (tp) 1081 (void) tcp_output(tp); 1082 } 1083 return (tp); 1084} 1085 1086/* 1087 * User issued close, and wish to trail through shutdown states: 1088 * if never received SYN, just forget it. If got a SYN from peer, 1089 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1090 * If already got a FIN from peer, then almost done; go to LAST_ACK 1091 * state. In all other cases, have already sent FIN to peer (e.g. 1092 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1093 * for peer to send FIN or not respond to keep-alives, etc. 1094 * We can let the user exit from the close as soon as the FIN is acked. 1095 */ 1096static struct tcpcb * 1097tcp_usrclosed(tp) 1098 register struct tcpcb *tp; 1099{ 1100 1101 switch (tp->t_state) { 1102 1103 case TCPS_CLOSED: 1104 case TCPS_LISTEN: 1105 tp->t_state = TCPS_CLOSED; 1106 tp = tcp_close(tp); 1107 break; 1108 1109 case TCPS_SYN_SENT: 1110 case TCPS_SYN_RECEIVED: 1111 tp->t_flags |= TF_NEEDFIN; 1112 break; 1113 1114 case TCPS_ESTABLISHED: 1115 tp->t_state = TCPS_FIN_WAIT_1; 1116 break; 1117 1118 case TCPS_CLOSE_WAIT: 1119 tp->t_state = TCPS_LAST_ACK; 1120 break; 1121 } 1122 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1123 soisdisconnected(tp->t_inpcb->inp_socket); 1124 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1125 if (tp->t_state == TCPS_FIN_WAIT_2) 1126 callout_reset(tp->tt_2msl, tcp_maxidle, 1127 tcp_timer_2msl, tp); 1128 } 1129 return (tp); 1130} 1131 1132