1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 331fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 342c37256eSGarrett Wollman * $Id: tcp_usrreq.c,v 1.22 1996/03/11 15:13:37 davidg Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 382ee45d7dSDavid Greenman #include <sys/queue.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40c7a82f90SGarrett Wollman #include <sys/kernel.h> 4198163b98SPoul-Henning Kamp #include <sys/sysctl.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44df8bae1dSRodney W. Grimes #include <sys/socket.h> 45df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 46df8bae1dSRodney W. Grimes #include <sys/protosw.h> 47df8bae1dSRodney W. Grimes #include <sys/errno.h> 48df8bae1dSRodney W. Grimes #include <sys/stat.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 55df8bae1dSRodney W. Grimes #include <netinet/ip.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 57b5e8ce9fSBruce Evans #include <netinet/in_var.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 60df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 61df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 62df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 63df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 65610ee2f9SDavid Greenman #ifdef TCPDEBUG 66df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 67610ee2f9SDavid Greenman #endif 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes /* 70df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 71df8bae1dSRodney W. Grimes */ 72df8bae1dSRodney W. Grimes extern char *tcpstates[]; 73df8bae1dSRodney W. Grimes 740312fbe9SPoul-Henning Kamp static int tcp_attach __P((struct socket *)); 750312fbe9SPoul-Henning Kamp static int tcp_connect __P((struct tcpcb *, struct mbuf *)); 760312fbe9SPoul-Henning Kamp static struct tcpcb * 770312fbe9SPoul-Henning Kamp tcp_disconnect __P((struct tcpcb *)); 780312fbe9SPoul-Henning Kamp static struct tcpcb * 790312fbe9SPoul-Henning Kamp tcp_usrclosed __P((struct tcpcb *)); 802c37256eSGarrett Wollman 812c37256eSGarrett Wollman #ifdef notdef 82df8bae1dSRodney W. Grimes /* 83df8bae1dSRodney W. Grimes * Process a TCP user request for TCP tb. If this is a send request 84df8bae1dSRodney W. Grimes * then m is the mbuf chain of send data. If this is a timer expiration 85df8bae1dSRodney W. Grimes * (called from the software clock routine), then timertype tells which timer. 86df8bae1dSRodney W. Grimes */ 87df8bae1dSRodney W. Grimes /*ARGSUSED*/ 88df8bae1dSRodney W. Grimes int 89df8bae1dSRodney W. Grimes tcp_usrreq(so, req, m, nam, control) 90df8bae1dSRodney W. Grimes struct socket *so; 91df8bae1dSRodney W. Grimes int req; 92df8bae1dSRodney W. Grimes struct mbuf *m, *nam, *control; 93df8bae1dSRodney W. Grimes { 94df8bae1dSRodney W. Grimes register struct inpcb *inp; 9526f9a767SRodney W. Grimes register struct tcpcb *tp = 0; 969ee39fc6SGarrett Wollman struct sockaddr_in *sinp; 97df8bae1dSRodney W. Grimes int s; 98df8bae1dSRodney W. Grimes int error = 0; 99a0292f23SGarrett Wollman #ifdef TCPDEBUG 100df8bae1dSRodney W. Grimes int ostate; 101a0292f23SGarrett Wollman #endif 102df8bae1dSRodney W. Grimes 103df8bae1dSRodney W. Grimes if (req == PRU_CONTROL) 104b6e3d50fSGarrett Wollman return (in_control(so, (u_long)m, (caddr_t)nam, 105df8bae1dSRodney W. Grimes (struct ifnet *)control)); 106df8bae1dSRodney W. Grimes if (control && control->m_len) { 107df8bae1dSRodney W. Grimes m_freem(control); 108df8bae1dSRodney W. Grimes if (m) 109df8bae1dSRodney W. Grimes m_freem(m); 110df8bae1dSRodney W. Grimes return (EINVAL); 111df8bae1dSRodney W. Grimes } 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes s = splnet(); 114df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 115df8bae1dSRodney W. Grimes /* 116df8bae1dSRodney W. Grimes * When a TCP is attached to a socket, then there will be 117df8bae1dSRodney W. Grimes * a (struct inpcb) pointed at by the socket, and this 118df8bae1dSRodney W. Grimes * structure will point at a subsidary (struct tcpcb). 119df8bae1dSRodney W. Grimes */ 120df8bae1dSRodney W. Grimes if (inp == 0 && req != PRU_ATTACH) { 121df8bae1dSRodney W. Grimes splx(s); 122b6e3d50fSGarrett Wollman #if 0 123b6e3d50fSGarrett Wollman /* 124b6e3d50fSGarrett Wollman * The following corrects an mbuf leak under rare 125b6e3d50fSGarrett Wollman * circumstances, but has not been fully tested. 126b6e3d50fSGarrett Wollman */ 127b6e3d50fSGarrett Wollman if (m && req != PRU_SENSE) 128b6e3d50fSGarrett Wollman m_freem(m); 129b6e3d50fSGarrett Wollman #else 130b6e3d50fSGarrett Wollman /* safer version of fix for mbuf leak */ 131b6e3d50fSGarrett Wollman if (m && (req == PRU_SEND || req == PRU_SENDOOB)) 132b6e3d50fSGarrett Wollman m_freem(m); 133b6e3d50fSGarrett Wollman #endif 134df8bae1dSRodney W. Grimes return (EINVAL); /* XXX */ 135df8bae1dSRodney W. Grimes } 136df8bae1dSRodney W. Grimes if (inp) { 137df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 138df8bae1dSRodney W. Grimes /* WHAT IF TP IS 0? */ 139df8bae1dSRodney W. Grimes #ifdef KPROF 140df8bae1dSRodney W. Grimes tcp_acounts[tp->t_state][req]++; 141df8bae1dSRodney W. Grimes #endif 142a0292f23SGarrett Wollman #ifdef TCPDEBUG 143df8bae1dSRodney W. Grimes ostate = tp->t_state; 144df8bae1dSRodney W. Grimes } else 145df8bae1dSRodney W. Grimes ostate = 0; 146a0292f23SGarrett Wollman #else /* TCPDEBUG */ 147a0292f23SGarrett Wollman } 148a0292f23SGarrett Wollman #endif /* TCPDEBUG */ 149a0292f23SGarrett Wollman 150df8bae1dSRodney W. Grimes switch (req) { 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes /* 153df8bae1dSRodney W. Grimes * TCP attaches to socket via PRU_ATTACH, reserving space, 154df8bae1dSRodney W. Grimes * and an internet control block. 155df8bae1dSRodney W. Grimes */ 156df8bae1dSRodney W. Grimes case PRU_ATTACH: 157df8bae1dSRodney W. Grimes if (inp) { 158df8bae1dSRodney W. Grimes error = EISCONN; 159df8bae1dSRodney W. Grimes break; 160df8bae1dSRodney W. Grimes } 161df8bae1dSRodney W. Grimes error = tcp_attach(so); 162df8bae1dSRodney W. Grimes if (error) 163df8bae1dSRodney W. Grimes break; 164df8bae1dSRodney W. Grimes if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1651fdbc7aeSGarrett Wollman so->so_linger = TCP_LINGERTIME * hz; 166df8bae1dSRodney W. Grimes tp = sototcpcb(so); 167df8bae1dSRodney W. Grimes break; 168df8bae1dSRodney W. Grimes 169df8bae1dSRodney W. Grimes /* 170df8bae1dSRodney W. Grimes * PRU_DETACH detaches the TCP protocol from the socket. 171df8bae1dSRodney W. Grimes * If the protocol state is non-embryonic, then can't 172df8bae1dSRodney W. Grimes * do this directly: have to initiate a PRU_DISCONNECT, 173df8bae1dSRodney W. Grimes * which may finish later; embryonic TCB's can just 174df8bae1dSRodney W. Grimes * be discarded here. 175df8bae1dSRodney W. Grimes */ 176df8bae1dSRodney W. Grimes case PRU_DETACH: 177df8bae1dSRodney W. Grimes if (tp->t_state > TCPS_LISTEN) 178df8bae1dSRodney W. Grimes tp = tcp_disconnect(tp); 179df8bae1dSRodney W. Grimes else 180df8bae1dSRodney W. Grimes tp = tcp_close(tp); 181df8bae1dSRodney W. Grimes break; 182df8bae1dSRodney W. Grimes 183df8bae1dSRodney W. Grimes /* 184df8bae1dSRodney W. Grimes * Give the socket an address. 185df8bae1dSRodney W. Grimes */ 186df8bae1dSRodney W. Grimes case PRU_BIND: 1879ee39fc6SGarrett Wollman /* 1889ee39fc6SGarrett Wollman * Must check for multicast addresses and disallow binding 1899ee39fc6SGarrett Wollman * to them. 1909ee39fc6SGarrett Wollman */ 1919ee39fc6SGarrett Wollman sinp = mtod(nam, struct sockaddr_in *); 1929ee39fc6SGarrett Wollman if (sinp->sin_family == AF_INET && 1939ee39fc6SGarrett Wollman IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1949ee39fc6SGarrett Wollman error = EAFNOSUPPORT; 1959ee39fc6SGarrett Wollman break; 1969ee39fc6SGarrett Wollman } 197df8bae1dSRodney W. Grimes error = in_pcbbind(inp, nam); 198df8bae1dSRodney W. Grimes if (error) 199df8bae1dSRodney W. Grimes break; 200df8bae1dSRodney W. Grimes break; 201df8bae1dSRodney W. Grimes 202df8bae1dSRodney W. Grimes /* 203df8bae1dSRodney W. Grimes * Prepare to accept connections. 204df8bae1dSRodney W. Grimes */ 205df8bae1dSRodney W. Grimes case PRU_LISTEN: 206df8bae1dSRodney W. Grimes if (inp->inp_lport == 0) 20715bd2b43SDavid Greenman error = in_pcbbind(inp, NULL); 208df8bae1dSRodney W. Grimes if (error == 0) 209df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 210df8bae1dSRodney W. Grimes break; 211df8bae1dSRodney W. Grimes 212df8bae1dSRodney W. Grimes /* 213df8bae1dSRodney W. Grimes * Initiate connection to peer. 214df8bae1dSRodney W. Grimes * Create a template for use in transmissions on this connection. 215df8bae1dSRodney W. Grimes * Enter SYN_SENT state, and mark socket as connecting. 216df8bae1dSRodney W. Grimes * Start keep-alive timer, and seed output sequence space. 217df8bae1dSRodney W. Grimes * Send initial segment on connection. 218df8bae1dSRodney W. Grimes */ 219df8bae1dSRodney W. Grimes case PRU_CONNECT: 2209ee39fc6SGarrett Wollman /* 2219ee39fc6SGarrett Wollman * Must disallow TCP ``connections'' to multicast addresses. 2229ee39fc6SGarrett Wollman */ 2239ee39fc6SGarrett Wollman sinp = mtod(nam, struct sockaddr_in *); 2249ee39fc6SGarrett Wollman if (sinp->sin_family == AF_INET 2259ee39fc6SGarrett Wollman && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 2269ee39fc6SGarrett Wollman error = EAFNOSUPPORT; 2279ee39fc6SGarrett Wollman break; 2289ee39fc6SGarrett Wollman } 2299ee39fc6SGarrett Wollman 230a0292f23SGarrett Wollman if ((error = tcp_connect(tp, nam)) != 0) 231a0292f23SGarrett Wollman break; 232df8bae1dSRodney W. Grimes error = tcp_output(tp); 233df8bae1dSRodney W. Grimes break; 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes /* 236df8bae1dSRodney W. Grimes * Create a TCP connection between two sockets. 237df8bae1dSRodney W. Grimes */ 238df8bae1dSRodney W. Grimes case PRU_CONNECT2: 239df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 240df8bae1dSRodney W. Grimes break; 241df8bae1dSRodney W. Grimes 242df8bae1dSRodney W. Grimes /* 243df8bae1dSRodney W. Grimes * Initiate disconnect from peer. 244df8bae1dSRodney W. Grimes * If connection never passed embryonic stage, just drop; 245df8bae1dSRodney W. Grimes * else if don't need to let data drain, then can just drop anyways, 246df8bae1dSRodney W. Grimes * else have to begin TCP shutdown process: mark socket disconnecting, 247df8bae1dSRodney W. Grimes * drain unread data, state switch to reflect user close, and 248df8bae1dSRodney W. Grimes * send segment (e.g. FIN) to peer. Socket will be really disconnected 249df8bae1dSRodney W. Grimes * when peer sends FIN and acks ours. 250df8bae1dSRodney W. Grimes * 251df8bae1dSRodney W. Grimes * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 252df8bae1dSRodney W. Grimes */ 253df8bae1dSRodney W. Grimes case PRU_DISCONNECT: 254df8bae1dSRodney W. Grimes tp = tcp_disconnect(tp); 255df8bae1dSRodney W. Grimes break; 256df8bae1dSRodney W. Grimes 257df8bae1dSRodney W. Grimes /* 258df8bae1dSRodney W. Grimes * Accept a connection. Essentially all the work is 259df8bae1dSRodney W. Grimes * done at higher levels; just return the address 260df8bae1dSRodney W. Grimes * of the peer, storing through addr. 261df8bae1dSRodney W. Grimes */ 262df8bae1dSRodney W. Grimes case PRU_ACCEPT: 263df8bae1dSRodney W. Grimes in_setpeeraddr(inp, nam); 264df8bae1dSRodney W. Grimes break; 265df8bae1dSRodney W. Grimes 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Mark the connection as being incapable of further output. 268df8bae1dSRodney W. Grimes */ 269df8bae1dSRodney W. Grimes case PRU_SHUTDOWN: 270df8bae1dSRodney W. Grimes socantsendmore(so); 271df8bae1dSRodney W. Grimes tp = tcp_usrclosed(tp); 272df8bae1dSRodney W. Grimes if (tp) 273df8bae1dSRodney W. Grimes error = tcp_output(tp); 274df8bae1dSRodney W. Grimes break; 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * After a receive, possibly send window update to peer. 278df8bae1dSRodney W. Grimes */ 279df8bae1dSRodney W. Grimes case PRU_RCVD: 280df8bae1dSRodney W. Grimes (void) tcp_output(tp); 281df8bae1dSRodney W. Grimes break; 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes /* 284df8bae1dSRodney W. Grimes * Do a send by putting data in output queue and updating urgent 285df8bae1dSRodney W. Grimes * marker if URG set. Possibly send more data. 286df8bae1dSRodney W. Grimes */ 287a0292f23SGarrett Wollman case PRU_SEND_EOF: 288df8bae1dSRodney W. Grimes case PRU_SEND: 289df8bae1dSRodney W. Grimes sbappend(&so->so_snd, m); 290a0292f23SGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 291a0292f23SGarrett Wollman /* 292a0292f23SGarrett Wollman * Do implied connect if not yet connected, 293a0292f23SGarrett Wollman * initialize window to default value, and 294a0292f23SGarrett Wollman * initialize maxseg/maxopd using peer's cached 295a0292f23SGarrett Wollman * MSS. 296a0292f23SGarrett Wollman */ 297a0292f23SGarrett Wollman error = tcp_connect(tp, nam); 298a0292f23SGarrett Wollman if (error) 299a0292f23SGarrett Wollman break; 300a0292f23SGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 301a0292f23SGarrett Wollman tcp_mss(tp, -1); 302a0292f23SGarrett Wollman } 303a0292f23SGarrett Wollman 304a0292f23SGarrett Wollman if (req == PRU_SEND_EOF) { 305a0292f23SGarrett Wollman /* 306a0292f23SGarrett Wollman * Close the send side of the connection after 307a0292f23SGarrett Wollman * the data is sent. 308a0292f23SGarrett Wollman */ 309a0292f23SGarrett Wollman socantsendmore(so); 310a0292f23SGarrett Wollman tp = tcp_usrclosed(tp); 311a0292f23SGarrett Wollman } 312a0292f23SGarrett Wollman if (tp != NULL) 313df8bae1dSRodney W. Grimes error = tcp_output(tp); 314df8bae1dSRodney W. Grimes break; 315df8bae1dSRodney W. Grimes 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * Abort the TCP. 318df8bae1dSRodney W. Grimes */ 319df8bae1dSRodney W. Grimes case PRU_ABORT: 320df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNABORTED); 321df8bae1dSRodney W. Grimes break; 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes case PRU_SENSE: 324df8bae1dSRodney W. Grimes ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 325df8bae1dSRodney W. Grimes (void) splx(s); 326df8bae1dSRodney W. Grimes return (0); 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes case PRU_RCVOOB: 329df8bae1dSRodney W. Grimes if ((so->so_oobmark == 0 && 330df8bae1dSRodney W. Grimes (so->so_state & SS_RCVATMARK) == 0) || 331df8bae1dSRodney W. Grimes so->so_options & SO_OOBINLINE || 332df8bae1dSRodney W. Grimes tp->t_oobflags & TCPOOB_HADDATA) { 333df8bae1dSRodney W. Grimes error = EINVAL; 334df8bae1dSRodney W. Grimes break; 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 337df8bae1dSRodney W. Grimes error = EWOULDBLOCK; 338df8bae1dSRodney W. Grimes break; 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes m->m_len = 1; 341df8bae1dSRodney W. Grimes *mtod(m, caddr_t) = tp->t_iobc; 342df8bae1dSRodney W. Grimes if (((int)nam & MSG_PEEK) == 0) 343df8bae1dSRodney W. Grimes tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 344df8bae1dSRodney W. Grimes break; 345df8bae1dSRodney W. Grimes 346df8bae1dSRodney W. Grimes case PRU_SENDOOB: 347df8bae1dSRodney W. Grimes if (sbspace(&so->so_snd) < -512) { 348df8bae1dSRodney W. Grimes m_freem(m); 349df8bae1dSRodney W. Grimes error = ENOBUFS; 350df8bae1dSRodney W. Grimes break; 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes /* 353df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 354df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 355df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 356df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 357df8bae1dSRodney W. Grimes * of data past the urgent section. 358df8bae1dSRodney W. Grimes * Otherwise, snd_up should be one lower. 359df8bae1dSRodney W. Grimes */ 360df8bae1dSRodney W. Grimes sbappend(&so->so_snd, m); 361df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 362df8bae1dSRodney W. Grimes tp->t_force = 1; 363df8bae1dSRodney W. Grimes error = tcp_output(tp); 364df8bae1dSRodney W. Grimes tp->t_force = 0; 365df8bae1dSRodney W. Grimes break; 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes case PRU_SOCKADDR: 368df8bae1dSRodney W. Grimes in_setsockaddr(inp, nam); 369df8bae1dSRodney W. Grimes break; 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes case PRU_PEERADDR: 372df8bae1dSRodney W. Grimes in_setpeeraddr(inp, nam); 373df8bae1dSRodney W. Grimes break; 374df8bae1dSRodney W. Grimes 375df8bae1dSRodney W. Grimes /* 376df8bae1dSRodney W. Grimes * TCP slow timer went off; going through this 377df8bae1dSRodney W. Grimes * routine for tracing's sake. 378df8bae1dSRodney W. Grimes */ 379df8bae1dSRodney W. Grimes case PRU_SLOWTIMO: 380df8bae1dSRodney W. Grimes tp = tcp_timers(tp, (int)nam); 381a0292f23SGarrett Wollman #ifdef TCPDEBUG 382df8bae1dSRodney W. Grimes req |= (int)nam << 8; /* for debug's sake */ 383a0292f23SGarrett Wollman #endif 384df8bae1dSRodney W. Grimes break; 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes default: 387df8bae1dSRodney W. Grimes panic("tcp_usrreq"); 388df8bae1dSRodney W. Grimes } 389610ee2f9SDavid Greenman #ifdef TCPDEBUG 390df8bae1dSRodney W. Grimes if (tp && (so->so_options & SO_DEBUG)) 391df8bae1dSRodney W. Grimes tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 392610ee2f9SDavid Greenman #endif 393df8bae1dSRodney W. Grimes splx(s); 394df8bae1dSRodney W. Grimes return (error); 395df8bae1dSRodney W. Grimes } 3962c37256eSGarrett Wollman #endif 3972c37256eSGarrett Wollman 3982c37256eSGarrett Wollman #ifdef TCPDEBUG 3992c37256eSGarrett Wollman #define TCPDEBUG0 int ostate 4002c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 4012c37256eSGarrett Wollman #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) && \ 4022c37256eSGarrett Wollman tcp_trace(TA_USER, ostate, tp, 0, req) 4032c37256eSGarrett Wollman #else 4042c37256eSGarrett Wollman #define TCPDEBUG0 4052c37256eSGarrett Wollman #define TCPDEBUG1() 4062c37256eSGarrett Wollman #define TCPDEBUG2(req) 4072c37256eSGarrett Wollman #endif 4082c37256eSGarrett Wollman 4092c37256eSGarrett Wollman /* 4102c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 4112c37256eSGarrett Wollman * and an internet control block. 4122c37256eSGarrett Wollman */ 4132c37256eSGarrett Wollman static int 4142c37256eSGarrett Wollman tcp_usr_attach(struct socket *so, int proto) 4152c37256eSGarrett Wollman { 4162c37256eSGarrett Wollman int s = splnet(); 4172c37256eSGarrett Wollman int error; 4182c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4192c37256eSGarrett Wollman struct tcpcb *tp = 0; 4202c37256eSGarrett Wollman TCPDEBUG0; 4212c37256eSGarrett Wollman 4222c37256eSGarrett Wollman TCPDEBUG1(); 4232c37256eSGarrett Wollman if (inp) { 4242c37256eSGarrett Wollman error = EISCONN; 4252c37256eSGarrett Wollman goto out; 4262c37256eSGarrett Wollman } 4272c37256eSGarrett Wollman 4282c37256eSGarrett Wollman error = tcp_attach(so); 4292c37256eSGarrett Wollman if (error) 4302c37256eSGarrett Wollman goto out; 4312c37256eSGarrett Wollman 4322c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 4332c37256eSGarrett Wollman so->so_linger = TCP_LINGERTIME * hz; 4342c37256eSGarrett Wollman tp = sototcpcb(so); 4352c37256eSGarrett Wollman out: 4362c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 4372c37256eSGarrett Wollman splx(s); 4382c37256eSGarrett Wollman return error; 4392c37256eSGarrett Wollman } 4402c37256eSGarrett Wollman 4412c37256eSGarrett Wollman /* 4422c37256eSGarrett Wollman * pru_detach() detaches the TCP protocol from the socket. 4432c37256eSGarrett Wollman * If the protocol state is non-embryonic, then can't 4442c37256eSGarrett Wollman * do this directly: have to initiate a pru_disconnect(), 4452c37256eSGarrett Wollman * which may finish later; embryonic TCB's can just 4462c37256eSGarrett Wollman * be discarded here. 4472c37256eSGarrett Wollman */ 4482c37256eSGarrett Wollman static int 4492c37256eSGarrett Wollman tcp_usr_detach(struct socket *so) 4502c37256eSGarrett Wollman { 4512c37256eSGarrett Wollman int s = splnet(); 4522c37256eSGarrett Wollman int error = 0; 4532c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4542c37256eSGarrett Wollman struct tcpcb *tp; 4552c37256eSGarrett Wollman TCPDEBUG0; 4562c37256eSGarrett Wollman 4572c37256eSGarrett Wollman if (inp == 0) { 4582c37256eSGarrett Wollman splx(s); 4592c37256eSGarrett Wollman return EINVAL; /* XXX */ 4602c37256eSGarrett Wollman } 4612c37256eSGarrett Wollman tp = intotcpcb(inp); 4622c37256eSGarrett Wollman TCPDEBUG1(); 4632c37256eSGarrett Wollman if (tp->t_state > TCPS_LISTEN) 4642c37256eSGarrett Wollman tp = tcp_disconnect(tp); 4652c37256eSGarrett Wollman else 4662c37256eSGarrett Wollman tp = tcp_close(tp); 4672c37256eSGarrett Wollman 4682c37256eSGarrett Wollman TCPDEBUG2(PRU_DETACH); 4692c37256eSGarrett Wollman splx(s); 4702c37256eSGarrett Wollman return error; 4712c37256eSGarrett Wollman } 4722c37256eSGarrett Wollman 4732c37256eSGarrett Wollman #define COMMON_START() TCPDEBUG0; \ 4742c37256eSGarrett Wollman do { \ 4752c37256eSGarrett Wollman if (inp == 0) { \ 4762c37256eSGarrett Wollman splx(s); \ 4772c37256eSGarrett Wollman return EINVAL; \ 4782c37256eSGarrett Wollman } \ 4792c37256eSGarrett Wollman tp = intotcpcb(inp); \ 4802c37256eSGarrett Wollman TCPDEBUG1(); \ 4812c37256eSGarrett Wollman } while(0) 4822c37256eSGarrett Wollman 4832c37256eSGarrett Wollman #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out 4842c37256eSGarrett Wollman 4852c37256eSGarrett Wollman 4862c37256eSGarrett Wollman /* 4872c37256eSGarrett Wollman * Give the socket an address. 4882c37256eSGarrett Wollman */ 4892c37256eSGarrett Wollman static int 4902c37256eSGarrett Wollman tcp_usr_bind(struct socket *so, struct mbuf *nam) 4912c37256eSGarrett Wollman { 4922c37256eSGarrett Wollman int s = splnet(); 4932c37256eSGarrett Wollman int error = 0; 4942c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4952c37256eSGarrett Wollman struct tcpcb *tp; 4962c37256eSGarrett Wollman struct sockaddr_in *sinp; 4972c37256eSGarrett Wollman 4982c37256eSGarrett Wollman COMMON_START(); 4992c37256eSGarrett Wollman 5002c37256eSGarrett Wollman /* 5012c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 5022c37256eSGarrett Wollman * to them. 5032c37256eSGarrett Wollman */ 5042c37256eSGarrett Wollman sinp = mtod(nam, struct sockaddr_in *); 5052c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 5062c37256eSGarrett Wollman IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 5072c37256eSGarrett Wollman error = EAFNOSUPPORT; 5082c37256eSGarrett Wollman goto out; 5092c37256eSGarrett Wollman } 5102c37256eSGarrett Wollman error = in_pcbbind(inp, nam); 5112c37256eSGarrett Wollman if (error) 5122c37256eSGarrett Wollman goto out; 5132c37256eSGarrett Wollman COMMON_END(PRU_BIND); 5142c37256eSGarrett Wollman 5152c37256eSGarrett Wollman } 5162c37256eSGarrett Wollman 5172c37256eSGarrett Wollman /* 5182c37256eSGarrett Wollman * Prepare to accept connections. 5192c37256eSGarrett Wollman */ 5202c37256eSGarrett Wollman static int 5212c37256eSGarrett Wollman tcp_usr_listen(struct socket *so) 5222c37256eSGarrett Wollman { 5232c37256eSGarrett Wollman int s = splnet(); 5242c37256eSGarrett Wollman int error = 0; 5252c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 5262c37256eSGarrett Wollman struct tcpcb *tp; 5272c37256eSGarrett Wollman 5282c37256eSGarrett Wollman COMMON_START(); 5292c37256eSGarrett Wollman if (inp->inp_lport == 0) 5302c37256eSGarrett Wollman error = in_pcbbind(inp, NULL); 5312c37256eSGarrett Wollman if (error == 0) 5322c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 5332c37256eSGarrett Wollman COMMON_END(PRU_LISTEN); 5342c37256eSGarrett Wollman } 5352c37256eSGarrett Wollman 5362c37256eSGarrett Wollman /* 5372c37256eSGarrett Wollman * Initiate connection to peer. 5382c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5392c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5402c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5412c37256eSGarrett Wollman * Send initial segment on connection. 5422c37256eSGarrett Wollman */ 5432c37256eSGarrett Wollman static int 5442c37256eSGarrett Wollman tcp_usr_connect(struct socket *so, struct mbuf *nam) 5452c37256eSGarrett Wollman { 5462c37256eSGarrett Wollman int s = splnet(); 5472c37256eSGarrett Wollman int error = 0; 5482c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 5492c37256eSGarrett Wollman struct tcpcb *tp; 5502c37256eSGarrett Wollman struct sockaddr_in *sinp; 5512c37256eSGarrett Wollman 5522c37256eSGarrett Wollman COMMON_START(); 5532c37256eSGarrett Wollman 5542c37256eSGarrett Wollman /* 5552c37256eSGarrett Wollman * Must disallow TCP ``connections'' to multicast addresses. 5562c37256eSGarrett Wollman */ 5572c37256eSGarrett Wollman sinp = mtod(nam, struct sockaddr_in *); 5582c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 5592c37256eSGarrett Wollman && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 5602c37256eSGarrett Wollman error = EAFNOSUPPORT; 5612c37256eSGarrett Wollman goto out; 5622c37256eSGarrett Wollman } 5632c37256eSGarrett Wollman 5642c37256eSGarrett Wollman if ((error = tcp_connect(tp, nam)) != 0) 5652c37256eSGarrett Wollman goto out; 5662c37256eSGarrett Wollman error = tcp_output(tp); 5672c37256eSGarrett Wollman COMMON_END(PRU_CONNECT); 5682c37256eSGarrett Wollman } 5692c37256eSGarrett Wollman 5702c37256eSGarrett Wollman /* 5712c37256eSGarrett Wollman * Initiate disconnect from peer. 5722c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 5732c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 5742c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 5752c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 5762c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 5772c37256eSGarrett Wollman * when peer sends FIN and acks ours. 5782c37256eSGarrett Wollman * 5792c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 5802c37256eSGarrett Wollman */ 5812c37256eSGarrett Wollman static int 5822c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 5832c37256eSGarrett Wollman { 5842c37256eSGarrett Wollman int s = splnet(); 5852c37256eSGarrett Wollman int error = 0; 5862c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 5872c37256eSGarrett Wollman struct tcpcb *tp; 5882c37256eSGarrett Wollman 5892c37256eSGarrett Wollman COMMON_START(); 5902c37256eSGarrett Wollman tp = tcp_disconnect(tp); 5912c37256eSGarrett Wollman COMMON_END(PRU_DISCONNECT); 5922c37256eSGarrett Wollman } 5932c37256eSGarrett Wollman 5942c37256eSGarrett Wollman /* 5952c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 5962c37256eSGarrett Wollman * done at higher levels; just return the address 5972c37256eSGarrett Wollman * of the peer, storing through addr. 5982c37256eSGarrett Wollman */ 5992c37256eSGarrett Wollman static int 6002c37256eSGarrett Wollman tcp_usr_accept(struct socket *so, struct mbuf *nam) 6012c37256eSGarrett Wollman { 6022c37256eSGarrett Wollman int s = splnet(); 6032c37256eSGarrett Wollman int error = 0; 6042c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6052c37256eSGarrett Wollman struct tcpcb *tp; 6062c37256eSGarrett Wollman 6072c37256eSGarrett Wollman COMMON_START(); 6082c37256eSGarrett Wollman in_setpeeraddr(inp, nam); 6092c37256eSGarrett Wollman COMMON_END(PRU_ACCEPT); 6102c37256eSGarrett Wollman } 6112c37256eSGarrett Wollman 6122c37256eSGarrett Wollman /* 6132c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 6142c37256eSGarrett Wollman */ 6152c37256eSGarrett Wollman static int 6162c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 6172c37256eSGarrett Wollman { 6182c37256eSGarrett Wollman int s = splnet(); 6192c37256eSGarrett Wollman int error = 0; 6202c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6212c37256eSGarrett Wollman struct tcpcb *tp; 6222c37256eSGarrett Wollman 6232c37256eSGarrett Wollman COMMON_START(); 6242c37256eSGarrett Wollman socantsendmore(so); 6252c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 6262c37256eSGarrett Wollman if (tp) 6272c37256eSGarrett Wollman error = tcp_output(tp); 6282c37256eSGarrett Wollman COMMON_END(PRU_SHUTDOWN); 6292c37256eSGarrett Wollman } 6302c37256eSGarrett Wollman 6312c37256eSGarrett Wollman /* 6322c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 6332c37256eSGarrett Wollman */ 6342c37256eSGarrett Wollman static int 6352c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 6362c37256eSGarrett Wollman { 6372c37256eSGarrett Wollman int s = splnet(); 6382c37256eSGarrett Wollman int error = 0; 6392c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6402c37256eSGarrett Wollman struct tcpcb *tp; 6412c37256eSGarrett Wollman 6422c37256eSGarrett Wollman COMMON_START(); 6432c37256eSGarrett Wollman tcp_output(tp); 6442c37256eSGarrett Wollman COMMON_END(PRU_RCVD); 6452c37256eSGarrett Wollman } 6462c37256eSGarrett Wollman 6472c37256eSGarrett Wollman /* 6482c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 6492c37256eSGarrett Wollman * marker if URG set. Possibly send more data. 6502c37256eSGarrett Wollman */ 6512c37256eSGarrett Wollman static int 6522c37256eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam, 6532c37256eSGarrett Wollman struct mbuf *control) 6542c37256eSGarrett Wollman { 6552c37256eSGarrett Wollman int s = splnet(); 6562c37256eSGarrett Wollman int error = 0; 6572c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6582c37256eSGarrett Wollman struct tcpcb *tp; 6592c37256eSGarrett Wollman 6602c37256eSGarrett Wollman COMMON_START(); 6612c37256eSGarrett Wollman if (control && control->m_len) { 6622c37256eSGarrett Wollman m_freem(control); /* XXX shouldn't caller do this??? */ 6632c37256eSGarrett Wollman if (m) 6642c37256eSGarrett Wollman m_freem(m); 6652c37256eSGarrett Wollman return EINVAL; 6662c37256eSGarrett Wollman } 6672c37256eSGarrett Wollman 6682c37256eSGarrett Wollman if(!(flags & PRUS_OOB)) { 6692c37256eSGarrett Wollman sbappend(&so->so_snd, m); 6702c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 6712c37256eSGarrett Wollman /* 6722c37256eSGarrett Wollman * Do implied connect if not yet connected, 6732c37256eSGarrett Wollman * initialize window to default value, and 6742c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 6752c37256eSGarrett Wollman * MSS. 6762c37256eSGarrett Wollman */ 6772c37256eSGarrett Wollman error = tcp_connect(tp, nam); 6782c37256eSGarrett Wollman if (error) 6792c37256eSGarrett Wollman goto out; 6802c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 6812c37256eSGarrett Wollman tcp_mss(tp, -1); 6822c37256eSGarrett Wollman } 6832c37256eSGarrett Wollman 6842c37256eSGarrett Wollman if (flags & PRUS_EOF) { 6852c37256eSGarrett Wollman /* 6862c37256eSGarrett Wollman * Close the send side of the connection after 6872c37256eSGarrett Wollman * the data is sent. 6882c37256eSGarrett Wollman */ 6892c37256eSGarrett Wollman socantsendmore(so); 6902c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 6912c37256eSGarrett Wollman } 6922c37256eSGarrett Wollman if (tp != NULL) 6932c37256eSGarrett Wollman error = tcp_output(tp); 6942c37256eSGarrett Wollman } else { 6952c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 6962c37256eSGarrett Wollman m_freem(m); 6972c37256eSGarrett Wollman error = ENOBUFS; 6982c37256eSGarrett Wollman goto out; 6992c37256eSGarrett Wollman } 7002c37256eSGarrett Wollman /* 7012c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 7022c37256eSGarrett Wollman * the urgent pointer points to the last octet 7032c37256eSGarrett Wollman * of urgent data. We continue, however, 7042c37256eSGarrett Wollman * to consider it to indicate the first octet 7052c37256eSGarrett Wollman * of data past the urgent section. 7062c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 7072c37256eSGarrett Wollman */ 7082c37256eSGarrett Wollman sbappend(&so->so_snd, m); 7092c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 7102c37256eSGarrett Wollman tp->t_force = 1; 7112c37256eSGarrett Wollman error = tcp_output(tp); 7122c37256eSGarrett Wollman tp->t_force = 0; 7132c37256eSGarrett Wollman } 7142c37256eSGarrett Wollman COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 7152c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 7162c37256eSGarrett Wollman } 7172c37256eSGarrett Wollman 7182c37256eSGarrett Wollman /* 7192c37256eSGarrett Wollman * Abort the TCP. 7202c37256eSGarrett Wollman */ 7212c37256eSGarrett Wollman static int 7222c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 7232c37256eSGarrett Wollman { 7242c37256eSGarrett Wollman int s = splnet(); 7252c37256eSGarrett Wollman int error = 0; 7262c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 7272c37256eSGarrett Wollman struct tcpcb *tp; 7282c37256eSGarrett Wollman 7292c37256eSGarrett Wollman COMMON_START(); 7302c37256eSGarrett Wollman tp = tcp_drop(tp, ECONNABORTED); 7312c37256eSGarrett Wollman COMMON_END(PRU_ABORT); 7322c37256eSGarrett Wollman } 7332c37256eSGarrett Wollman 7342c37256eSGarrett Wollman /* 7352c37256eSGarrett Wollman * Fill in st_bklsize for fstat() operations on a socket. 7362c37256eSGarrett Wollman */ 7372c37256eSGarrett Wollman static int 7382c37256eSGarrett Wollman tcp_usr_sense(struct socket *so, struct stat *sb) 7392c37256eSGarrett Wollman { 7402c37256eSGarrett Wollman int s = splnet(); 7412c37256eSGarrett Wollman 7422c37256eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat; 7432c37256eSGarrett Wollman splx(s); 7442c37256eSGarrett Wollman return 0; 7452c37256eSGarrett Wollman } 7462c37256eSGarrett Wollman 7472c37256eSGarrett Wollman /* 7482c37256eSGarrett Wollman * Receive out-of-band data. 7492c37256eSGarrett Wollman */ 7502c37256eSGarrett Wollman static int 7512c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 7522c37256eSGarrett Wollman { 7532c37256eSGarrett Wollman int s = splnet(); 7542c37256eSGarrett Wollman int error = 0; 7552c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 7562c37256eSGarrett Wollman struct tcpcb *tp; 7572c37256eSGarrett Wollman 7582c37256eSGarrett Wollman COMMON_START(); 7592c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 7602c37256eSGarrett Wollman (so->so_state & SS_RCVATMARK) == 0) || 7612c37256eSGarrett Wollman so->so_options & SO_OOBINLINE || 7622c37256eSGarrett Wollman tp->t_oobflags & TCPOOB_HADDATA) { 7632c37256eSGarrett Wollman error = EINVAL; 7642c37256eSGarrett Wollman goto out; 7652c37256eSGarrett Wollman } 7662c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 7672c37256eSGarrett Wollman error = EWOULDBLOCK; 7682c37256eSGarrett Wollman goto out; 7692c37256eSGarrett Wollman } 7702c37256eSGarrett Wollman m->m_len = 1; 7712c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 7722c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 7732c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 7742c37256eSGarrett Wollman COMMON_END(PRU_RCVOOB); 7752c37256eSGarrett Wollman } 7762c37256eSGarrett Wollman 7772c37256eSGarrett Wollman static int 7782c37256eSGarrett Wollman tcp_usr_sockaddr(struct socket *so, struct mbuf *nam) 7792c37256eSGarrett Wollman { 7802c37256eSGarrett Wollman int s = splnet(); 7812c37256eSGarrett Wollman int error = 0; 7822c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 7832c37256eSGarrett Wollman struct tcpcb *tp; 7842c37256eSGarrett Wollman 7852c37256eSGarrett Wollman COMMON_START(); 7862c37256eSGarrett Wollman in_setsockaddr(inp, nam); 7872c37256eSGarrett Wollman COMMON_END(PRU_SOCKADDR); 7882c37256eSGarrett Wollman } 7892c37256eSGarrett Wollman 7902c37256eSGarrett Wollman static int 7912c37256eSGarrett Wollman tcp_usr_peeraddr(struct socket *so, struct mbuf *nam) 7922c37256eSGarrett Wollman { 7932c37256eSGarrett Wollman int s = splnet(); 7942c37256eSGarrett Wollman int error = 0; 7952c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 7962c37256eSGarrett Wollman struct tcpcb *tp; 7972c37256eSGarrett Wollman 7982c37256eSGarrett Wollman COMMON_START(); 7992c37256eSGarrett Wollman in_setpeeraddr(inp, nam); 8002c37256eSGarrett Wollman COMMON_END(PRU_PEERADDR); 8012c37256eSGarrett Wollman } 8022c37256eSGarrett Wollman 8032c37256eSGarrett Wollman /* 8042c37256eSGarrett Wollman * XXX - this should just be a call to in_control, but we need to get 8052c37256eSGarrett Wollman * the types worked out. 8062c37256eSGarrett Wollman */ 8072c37256eSGarrett Wollman static int 8082c37256eSGarrett Wollman tcp_usr_control(struct socket *so, int cmd, caddr_t arg, struct ifnet *ifp) 8092c37256eSGarrett Wollman { 8102c37256eSGarrett Wollman return in_control(so, cmd, arg, ifp); 8112c37256eSGarrett Wollman } 8122c37256eSGarrett Wollman 8132c37256eSGarrett Wollman /* xxx - should be const */ 8142c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 8152c37256eSGarrett Wollman tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 8162c37256eSGarrett Wollman tcp_usr_connect, pru_connect2_notsupp, tcp_usr_control, tcp_usr_detach, 8172c37256eSGarrett Wollman tcp_usr_disconnect, tcp_usr_listen, tcp_usr_peeraddr, tcp_usr_rcvd, 8182c37256eSGarrett Wollman tcp_usr_rcvoob, tcp_usr_send, tcp_usr_sense, tcp_usr_shutdown, 8192c37256eSGarrett Wollman tcp_usr_sockaddr 8202c37256eSGarrett Wollman }; 821df8bae1dSRodney W. Grimes 822a0292f23SGarrett Wollman /* 823a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 824a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 825a0292f23SGarrett Wollman * port number if needed. Call in_pcbladdr to do the routing and to choose 826a0292f23SGarrett Wollman * a local host address (interface). If there is an existing incarnation 827a0292f23SGarrett Wollman * of the same connection in TIME-WAIT state and if the remote host was 828a0292f23SGarrett Wollman * sending CC options and if the connection duration was < MSL, then 829a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 830a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 831a0292f23SGarrett Wollman */ 8320312fbe9SPoul-Henning Kamp static int 833a0292f23SGarrett Wollman tcp_connect(tp, nam) 834a0292f23SGarrett Wollman register struct tcpcb *tp; 835a0292f23SGarrett Wollman struct mbuf *nam; 836a0292f23SGarrett Wollman { 837a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 838a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 839a0292f23SGarrett Wollman struct tcpcb *otp; 840a0292f23SGarrett Wollman struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 841a0292f23SGarrett Wollman struct sockaddr_in *ifaddr; 842a0292f23SGarrett Wollman int error; 843a45d2726SAndras Olah struct rmxp_tao *taop; 844a45d2726SAndras Olah struct rmxp_tao tao_noncached; 845a0292f23SGarrett Wollman 846a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 847a0292f23SGarrett Wollman error = in_pcbbind(inp, NULL); 848a0292f23SGarrett Wollman if (error) 849a0292f23SGarrett Wollman return error; 850a0292f23SGarrett Wollman } 851a0292f23SGarrett Wollman 852a0292f23SGarrett Wollman /* 853a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 854a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 855a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 856a0292f23SGarrett Wollman */ 857a0292f23SGarrett Wollman error = in_pcbladdr(inp, nam, &ifaddr); 858d3628763SRodney W. Grimes if (error) 859d3628763SRodney W. Grimes return error; 86015bd2b43SDavid Greenman oinp = in_pcblookup(inp->inp_pcbinfo->listhead, 861a0292f23SGarrett Wollman sin->sin_addr, sin->sin_port, 862a0292f23SGarrett Wollman inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 863a0292f23SGarrett Wollman : ifaddr->sin_addr, 864a0292f23SGarrett Wollman inp->inp_lport, 0); 865a0292f23SGarrett Wollman if (oinp) { 866a0292f23SGarrett Wollman if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 867a0292f23SGarrett Wollman otp->t_state == TCPS_TIME_WAIT && 868a0292f23SGarrett Wollman otp->t_duration < TCPTV_MSL && 869a0292f23SGarrett Wollman (otp->t_flags & TF_RCVD_CC)) 870a0292f23SGarrett Wollman otp = tcp_close(otp); 871a0292f23SGarrett Wollman else 872a0292f23SGarrett Wollman return EADDRINUSE; 873a0292f23SGarrett Wollman } 874a0292f23SGarrett Wollman if (inp->inp_laddr.s_addr == INADDR_ANY) 875a0292f23SGarrett Wollman inp->inp_laddr = ifaddr->sin_addr; 876a0292f23SGarrett Wollman inp->inp_faddr = sin->sin_addr; 877a0292f23SGarrett Wollman inp->inp_fport = sin->sin_port; 87815bd2b43SDavid Greenman in_pcbrehash(inp); 879a0292f23SGarrett Wollman 880a0292f23SGarrett Wollman tp->t_template = tcp_template(tp); 881a0292f23SGarrett Wollman if (tp->t_template == 0) { 882a0292f23SGarrett Wollman in_pcbdisconnect(inp); 883a0292f23SGarrett Wollman return ENOBUFS; 884a0292f23SGarrett Wollman } 885a0292f23SGarrett Wollman 886a0292f23SGarrett Wollman /* Compute window scaling to request. */ 887a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 888a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 889a0292f23SGarrett Wollman tp->request_r_scale++; 890a0292f23SGarrett Wollman 891a0292f23SGarrett Wollman soisconnecting(so); 892a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 893a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 894a0292f23SGarrett Wollman tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 895a0292f23SGarrett Wollman tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 896a0292f23SGarrett Wollman tcp_sendseqinit(tp); 897a45d2726SAndras Olah 898a45d2726SAndras Olah /* 899a45d2726SAndras Olah * Generate a CC value for this connection and 900a45d2726SAndras Olah * check whether CC or CCnew should be used. 901a45d2726SAndras Olah */ 902a45d2726SAndras Olah if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 903a45d2726SAndras Olah taop = &tao_noncached; 904a45d2726SAndras Olah bzero(taop, sizeof(*taop)); 905a45d2726SAndras Olah } 906a45d2726SAndras Olah 907a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 908a45d2726SAndras Olah if (taop->tao_ccsent != 0 && 909a45d2726SAndras Olah CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 910a45d2726SAndras Olah taop->tao_ccsent = tp->cc_send; 911a45d2726SAndras Olah } else { 912a45d2726SAndras Olah taop->tao_ccsent = 0; 913a45d2726SAndras Olah tp->t_flags |= TF_SENDCCNEW; 914a45d2726SAndras Olah } 915a0292f23SGarrett Wollman 916a0292f23SGarrett Wollman return 0; 917a0292f23SGarrett Wollman } 918a0292f23SGarrett Wollman 919df8bae1dSRodney W. Grimes int 920df8bae1dSRodney W. Grimes tcp_ctloutput(op, so, level, optname, mp) 921df8bae1dSRodney W. Grimes int op; 922df8bae1dSRodney W. Grimes struct socket *so; 923df8bae1dSRodney W. Grimes int level, optname; 924df8bae1dSRodney W. Grimes struct mbuf **mp; 925df8bae1dSRodney W. Grimes { 926df8bae1dSRodney W. Grimes int error = 0, s; 927df8bae1dSRodney W. Grimes struct inpcb *inp; 928df8bae1dSRodney W. Grimes register struct tcpcb *tp; 929df8bae1dSRodney W. Grimes register struct mbuf *m; 930df8bae1dSRodney W. Grimes register int i; 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes s = splnet(); 933df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 934df8bae1dSRodney W. Grimes if (inp == NULL) { 935df8bae1dSRodney W. Grimes splx(s); 936df8bae1dSRodney W. Grimes if (op == PRCO_SETOPT && *mp) 937df8bae1dSRodney W. Grimes (void) m_free(*mp); 938df8bae1dSRodney W. Grimes return (ECONNRESET); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes if (level != IPPROTO_TCP) { 941df8bae1dSRodney W. Grimes error = ip_ctloutput(op, so, level, optname, mp); 942df8bae1dSRodney W. Grimes splx(s); 943df8bae1dSRodney W. Grimes return (error); 944df8bae1dSRodney W. Grimes } 945df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 946df8bae1dSRodney W. Grimes 947df8bae1dSRodney W. Grimes switch (op) { 948df8bae1dSRodney W. Grimes 949df8bae1dSRodney W. Grimes case PRCO_SETOPT: 950df8bae1dSRodney W. Grimes m = *mp; 951df8bae1dSRodney W. Grimes switch (optname) { 952df8bae1dSRodney W. Grimes 953df8bae1dSRodney W. Grimes case TCP_NODELAY: 954df8bae1dSRodney W. Grimes if (m == NULL || m->m_len < sizeof (int)) 955df8bae1dSRodney W. Grimes error = EINVAL; 956df8bae1dSRodney W. Grimes else if (*mtod(m, int *)) 957df8bae1dSRodney W. Grimes tp->t_flags |= TF_NODELAY; 958df8bae1dSRodney W. Grimes else 959df8bae1dSRodney W. Grimes tp->t_flags &= ~TF_NODELAY; 960df8bae1dSRodney W. Grimes break; 961df8bae1dSRodney W. Grimes 962df8bae1dSRodney W. Grimes case TCP_MAXSEG: 963df8bae1dSRodney W. Grimes if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg) 964df8bae1dSRodney W. Grimes tp->t_maxseg = i; 965df8bae1dSRodney W. Grimes else 966df8bae1dSRodney W. Grimes error = EINVAL; 967df8bae1dSRodney W. Grimes break; 968df8bae1dSRodney W. Grimes 969a0292f23SGarrett Wollman case TCP_NOOPT: 970a0292f23SGarrett Wollman if (m == NULL || m->m_len < sizeof (int)) 971a0292f23SGarrett Wollman error = EINVAL; 972a0292f23SGarrett Wollman else if (*mtod(m, int *)) 973a0292f23SGarrett Wollman tp->t_flags |= TF_NOOPT; 974a0292f23SGarrett Wollman else 975a0292f23SGarrett Wollman tp->t_flags &= ~TF_NOOPT; 976a0292f23SGarrett Wollman break; 977a0292f23SGarrett Wollman 978a0292f23SGarrett Wollman case TCP_NOPUSH: 979a0292f23SGarrett Wollman if (m == NULL || m->m_len < sizeof (int)) 980a0292f23SGarrett Wollman error = EINVAL; 981a0292f23SGarrett Wollman else if (*mtod(m, int *)) 982a0292f23SGarrett Wollman tp->t_flags |= TF_NOPUSH; 983a0292f23SGarrett Wollman else 984a0292f23SGarrett Wollman tp->t_flags &= ~TF_NOPUSH; 985a0292f23SGarrett Wollman break; 986a0292f23SGarrett Wollman 987df8bae1dSRodney W. Grimes default: 988df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 989df8bae1dSRodney W. Grimes break; 990df8bae1dSRodney W. Grimes } 991df8bae1dSRodney W. Grimes if (m) 992df8bae1dSRodney W. Grimes (void) m_free(m); 993df8bae1dSRodney W. Grimes break; 994df8bae1dSRodney W. Grimes 995df8bae1dSRodney W. Grimes case PRCO_GETOPT: 996df8bae1dSRodney W. Grimes *mp = m = m_get(M_WAIT, MT_SOOPTS); 997df8bae1dSRodney W. Grimes m->m_len = sizeof(int); 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes switch (optname) { 1000df8bae1dSRodney W. Grimes case TCP_NODELAY: 1001df8bae1dSRodney W. Grimes *mtod(m, int *) = tp->t_flags & TF_NODELAY; 1002df8bae1dSRodney W. Grimes break; 1003df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1004df8bae1dSRodney W. Grimes *mtod(m, int *) = tp->t_maxseg; 1005df8bae1dSRodney W. Grimes break; 1006a0292f23SGarrett Wollman case TCP_NOOPT: 1007a0292f23SGarrett Wollman *mtod(m, int *) = tp->t_flags & TF_NOOPT; 1008a0292f23SGarrett Wollman break; 1009a0292f23SGarrett Wollman case TCP_NOPUSH: 1010a0292f23SGarrett Wollman *mtod(m, int *) = tp->t_flags & TF_NOPUSH; 1011a0292f23SGarrett Wollman break; 1012df8bae1dSRodney W. Grimes default: 1013df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1014df8bae1dSRodney W. Grimes break; 1015df8bae1dSRodney W. Grimes } 1016df8bae1dSRodney W. Grimes break; 1017df8bae1dSRodney W. Grimes } 1018df8bae1dSRodney W. Grimes splx(s); 1019df8bae1dSRodney W. Grimes return (error); 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 102226e30fbbSDavid Greenman /* 102326e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 102426e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 102526e30fbbSDavid Greenman * be set by the route). 102626e30fbbSDavid Greenman */ 102726e30fbbSDavid Greenman u_long tcp_sendspace = 1024*16; 102898163b98SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, 102998163b98SPoul-Henning Kamp CTLFLAG_RW, &tcp_sendspace , 0, ""); 103026e30fbbSDavid Greenman u_long tcp_recvspace = 1024*16; 103198163b98SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, 103298163b98SPoul-Henning Kamp CTLFLAG_RW, &tcp_recvspace , 0, ""); 1033df8bae1dSRodney W. Grimes 1034df8bae1dSRodney W. Grimes /* 1035df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1036df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1037df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1038df8bae1dSRodney W. Grimes */ 10390312fbe9SPoul-Henning Kamp static int 1040df8bae1dSRodney W. Grimes tcp_attach(so) 1041df8bae1dSRodney W. Grimes struct socket *so; 1042df8bae1dSRodney W. Grimes { 1043df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1044df8bae1dSRodney W. Grimes struct inpcb *inp; 1045df8bae1dSRodney W. Grimes int error; 1046df8bae1dSRodney W. Grimes 1047df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1048df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1049df8bae1dSRodney W. Grimes if (error) 1050df8bae1dSRodney W. Grimes return (error); 1051df8bae1dSRodney W. Grimes } 105215bd2b43SDavid Greenman error = in_pcballoc(so, &tcbinfo); 1053df8bae1dSRodney W. Grimes if (error) 1054df8bae1dSRodney W. Grimes return (error); 1055df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1056df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1057df8bae1dSRodney W. Grimes if (tp == 0) { 1058df8bae1dSRodney W. Grimes int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1059df8bae1dSRodney W. Grimes 1060df8bae1dSRodney W. Grimes so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1061df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1062df8bae1dSRodney W. Grimes so->so_state |= nofd; 1063df8bae1dSRodney W. Grimes return (ENOBUFS); 1064df8bae1dSRodney W. Grimes } 1065df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1066df8bae1dSRodney W. Grimes return (0); 1067df8bae1dSRodney W. Grimes } 1068df8bae1dSRodney W. Grimes 1069df8bae1dSRodney W. Grimes /* 1070df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1071df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1072df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1073df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1074df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1075df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1076df8bae1dSRodney W. Grimes */ 10770312fbe9SPoul-Henning Kamp static struct tcpcb * 1078df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1079df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1080df8bae1dSRodney W. Grimes { 1081df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 1082df8bae1dSRodney W. Grimes 1083df8bae1dSRodney W. Grimes if (tp->t_state < TCPS_ESTABLISHED) 1084df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1085df8bae1dSRodney W. Grimes else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1086df8bae1dSRodney W. Grimes tp = tcp_drop(tp, 0); 1087df8bae1dSRodney W. Grimes else { 1088df8bae1dSRodney W. Grimes soisdisconnecting(so); 1089df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1090df8bae1dSRodney W. Grimes tp = tcp_usrclosed(tp); 1091df8bae1dSRodney W. Grimes if (tp) 1092df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1093df8bae1dSRodney W. Grimes } 1094df8bae1dSRodney W. Grimes return (tp); 1095df8bae1dSRodney W. Grimes } 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes /* 1098df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1099df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1100df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1101df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1102df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1103df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1104df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1105df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1106df8bae1dSRodney W. Grimes */ 11070312fbe9SPoul-Henning Kamp static struct tcpcb * 1108df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1109df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1110df8bae1dSRodney W. Grimes { 1111df8bae1dSRodney W. Grimes 1112df8bae1dSRodney W. Grimes switch (tp->t_state) { 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1115df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1116df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1117df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1118df8bae1dSRodney W. Grimes break; 1119df8bae1dSRodney W. Grimes 1120a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1121df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1122a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1123a0292f23SGarrett Wollman break; 1124a0292f23SGarrett Wollman 1125df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1126df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1127df8bae1dSRodney W. Grimes break; 1128df8bae1dSRodney W. Grimes 1129df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1130df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1131df8bae1dSRodney W. Grimes break; 1132df8bae1dSRodney W. Grimes } 1133b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1134df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1135b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1136b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 1137b6239c4aSAndras Olah tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1138b6239c4aSAndras Olah } 1139df8bae1dSRodney W. Grimes return (tp); 1140df8bae1dSRodney W. Grimes } 1141a0292f23SGarrett Wollman 1142