Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.9
      1  1.1      cgd /*
      2  1.9  mycroft  * Copyright (c) 1982, 1986, 1988, 1993
      3  1.9  mycroft  *	The Regents of the University of California.  All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  *
     33  1.9  mycroft  *	from: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
     34  1.9  mycroft  *	$Id: tcp_usrreq.c,v 1.9 1994/05/13 06:06:49 mycroft Exp $
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.5  mycroft #include <sys/param.h>
     38  1.5  mycroft #include <sys/systm.h>
     39  1.5  mycroft #include <sys/malloc.h>
     40  1.5  mycroft #include <sys/mbuf.h>
     41  1.5  mycroft #include <sys/socket.h>
     42  1.5  mycroft #include <sys/socketvar.h>
     43  1.5  mycroft #include <sys/protosw.h>
     44  1.5  mycroft #include <sys/errno.h>
     45  1.5  mycroft #include <sys/stat.h>
     46  1.1      cgd 
     47  1.5  mycroft #include <net/if.h>
     48  1.5  mycroft #include <net/route.h>
     49  1.1      cgd 
     50  1.5  mycroft #include <netinet/in.h>
     51  1.5  mycroft #include <netinet/in_systm.h>
     52  1.5  mycroft #include <netinet/ip.h>
     53  1.5  mycroft #include <netinet/in_pcb.h>
     54  1.5  mycroft #include <netinet/ip_var.h>
     55  1.5  mycroft #include <netinet/tcp.h>
     56  1.5  mycroft #include <netinet/tcp_fsm.h>
     57  1.5  mycroft #include <netinet/tcp_seq.h>
     58  1.5  mycroft #include <netinet/tcp_timer.h>
     59  1.5  mycroft #include <netinet/tcp_var.h>
     60  1.5  mycroft #include <netinet/tcpip.h>
     61  1.5  mycroft #include <netinet/tcp_debug.h>
     62  1.1      cgd 
     63  1.1      cgd /*
     64  1.1      cgd  * TCP protocol interface to socket abstraction.
     65  1.1      cgd  */
     66  1.1      cgd extern	char *tcpstates[];
     67  1.1      cgd 
     68  1.1      cgd /*
     69  1.1      cgd  * Process a TCP user request for TCP tb.  If this is a send request
     70  1.1      cgd  * then m is the mbuf chain of send data.  If this is a timer expiration
     71  1.1      cgd  * (called from the software clock routine), then timertype tells which timer.
     72  1.1      cgd  */
     73  1.1      cgd /*ARGSUSED*/
     74  1.6  mycroft int
     75  1.1      cgd tcp_usrreq(so, req, m, nam, control)
     76  1.1      cgd 	struct socket *so;
     77  1.1      cgd 	int req;
     78  1.1      cgd 	struct mbuf *m, *nam, *control;
     79  1.1      cgd {
     80  1.1      cgd 	register struct inpcb *inp;
     81  1.1      cgd 	register struct tcpcb *tp;
     82  1.1      cgd 	int s;
     83  1.1      cgd 	int error = 0;
     84  1.1      cgd 	int ostate;
     85  1.1      cgd 
     86  1.1      cgd 	if (req == PRU_CONTROL)
     87  1.1      cgd 		return (in_control(so, (int)m, (caddr_t)nam,
     88  1.1      cgd 			(struct ifnet *)control));
     89  1.1      cgd 	if (control && control->m_len) {
     90  1.1      cgd 		m_freem(control);
     91  1.1      cgd 		if (m)
     92  1.1      cgd 			m_freem(m);
     93  1.1      cgd 		return (EINVAL);
     94  1.1      cgd 	}
     95  1.1      cgd 
     96  1.1      cgd 	s = splnet();
     97  1.1      cgd 	inp = sotoinpcb(so);
     98  1.1      cgd 	/*
     99  1.1      cgd 	 * When a TCP is attached to a socket, then there will be
    100  1.1      cgd 	 * a (struct inpcb) pointed at by the socket, and this
    101  1.1      cgd 	 * structure will point at a subsidary (struct tcpcb).
    102  1.1      cgd 	 */
    103  1.1      cgd 	if (inp == 0 && req != PRU_ATTACH) {
    104  1.1      cgd 		splx(s);
    105  1.1      cgd 		return (EINVAL);		/* XXX */
    106  1.1      cgd 	}
    107  1.1      cgd 	if (inp) {
    108  1.1      cgd 		tp = intotcpcb(inp);
    109  1.1      cgd 		/* WHAT IF TP IS 0? */
    110  1.1      cgd #ifdef KPROF
    111  1.1      cgd 		tcp_acounts[tp->t_state][req]++;
    112  1.1      cgd #endif
    113  1.1      cgd 		ostate = tp->t_state;
    114  1.1      cgd 	} else
    115  1.1      cgd 		ostate = 0;
    116  1.1      cgd 	switch (req) {
    117  1.1      cgd 
    118  1.1      cgd 	/*
    119  1.1      cgd 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    120  1.1      cgd 	 * and an internet control block.
    121  1.1      cgd 	 */
    122  1.1      cgd 	case PRU_ATTACH:
    123  1.1      cgd 		if (inp) {
    124  1.1      cgd 			error = EISCONN;
    125  1.1      cgd 			break;
    126  1.1      cgd 		}
    127  1.1      cgd 		error = tcp_attach(so);
    128  1.1      cgd 		if (error)
    129  1.1      cgd 			break;
    130  1.1      cgd 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    131  1.1      cgd 			so->so_linger = TCP_LINGERTIME;
    132  1.1      cgd 		tp = sototcpcb(so);
    133  1.1      cgd 		break;
    134  1.1      cgd 
    135  1.1      cgd 	/*
    136  1.1      cgd 	 * PRU_DETACH detaches the TCP protocol from the socket.
    137  1.1      cgd 	 * If the protocol state is non-embryonic, then can't
    138  1.1      cgd 	 * do this directly: have to initiate a PRU_DISCONNECT,
    139  1.1      cgd 	 * which may finish later; embryonic TCB's can just
    140  1.1      cgd 	 * be discarded here.
    141  1.1      cgd 	 */
    142  1.1      cgd 	case PRU_DETACH:
    143  1.1      cgd 		if (tp->t_state > TCPS_LISTEN)
    144  1.1      cgd 			tp = tcp_disconnect(tp);
    145  1.1      cgd 		else
    146  1.1      cgd 			tp = tcp_close(tp);
    147  1.1      cgd 		break;
    148  1.1      cgd 
    149  1.1      cgd 	/*
    150  1.1      cgd 	 * Give the socket an address.
    151  1.1      cgd 	 */
    152  1.1      cgd 	case PRU_BIND:
    153  1.1      cgd 		error = in_pcbbind(inp, nam);
    154  1.1      cgd 		if (error)
    155  1.1      cgd 			break;
    156  1.1      cgd 		break;
    157  1.1      cgd 
    158  1.1      cgd 	/*
    159  1.1      cgd 	 * Prepare to accept connections.
    160  1.1      cgd 	 */
    161  1.1      cgd 	case PRU_LISTEN:
    162  1.1      cgd 		if (inp->inp_lport == 0)
    163  1.1      cgd 			error = in_pcbbind(inp, (struct mbuf *)0);
    164  1.1      cgd 		if (error == 0)
    165  1.1      cgd 			tp->t_state = TCPS_LISTEN;
    166  1.1      cgd 		break;
    167  1.1      cgd 
    168  1.1      cgd 	/*
    169  1.1      cgd 	 * Initiate connection to peer.
    170  1.1      cgd 	 * Create a template for use in transmissions on this connection.
    171  1.1      cgd 	 * Enter SYN_SENT state, and mark socket as connecting.
    172  1.1      cgd 	 * Start keep-alive timer, and seed output sequence space.
    173  1.1      cgd 	 * Send initial segment on connection.
    174  1.1      cgd 	 */
    175  1.1      cgd 	case PRU_CONNECT:
    176  1.1      cgd 		if (inp->inp_lport == 0) {
    177  1.1      cgd 			error = in_pcbbind(inp, (struct mbuf *)0);
    178  1.1      cgd 			if (error)
    179  1.1      cgd 				break;
    180  1.1      cgd 		}
    181  1.1      cgd 		error = in_pcbconnect(inp, nam);
    182  1.1      cgd 		if (error)
    183  1.1      cgd 			break;
    184  1.1      cgd 		tp->t_template = tcp_template(tp);
    185  1.1      cgd 		if (tp->t_template == 0) {
    186  1.1      cgd 			in_pcbdisconnect(inp);
    187  1.1      cgd 			error = ENOBUFS;
    188  1.1      cgd 			break;
    189  1.1      cgd 		}
    190  1.9  mycroft 		/* Compute window scaling to request.  */
    191  1.9  mycroft 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    192  1.9  mycroft 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    193  1.9  mycroft 			tp->request_r_scale++;
    194  1.1      cgd 		soisconnecting(so);
    195  1.1      cgd 		tcpstat.tcps_connattempt++;
    196  1.1      cgd 		tp->t_state = TCPS_SYN_SENT;
    197  1.1      cgd 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
    198  1.1      cgd 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
    199  1.1      cgd 		tcp_sendseqinit(tp);
    200  1.1      cgd 		error = tcp_output(tp);
    201  1.1      cgd 		break;
    202  1.1      cgd 
    203  1.1      cgd 	/*
    204  1.1      cgd 	 * Create a TCP connection between two sockets.
    205  1.1      cgd 	 */
    206  1.1      cgd 	case PRU_CONNECT2:
    207  1.1      cgd 		error = EOPNOTSUPP;
    208  1.1      cgd 		break;
    209  1.1      cgd 
    210  1.1      cgd 	/*
    211  1.1      cgd 	 * Initiate disconnect from peer.
    212  1.1      cgd 	 * If connection never passed embryonic stage, just drop;
    213  1.1      cgd 	 * else if don't need to let data drain, then can just drop anyways,
    214  1.1      cgd 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    215  1.1      cgd 	 * drain unread data, state switch to reflect user close, and
    216  1.1      cgd 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    217  1.1      cgd 	 * when peer sends FIN and acks ours.
    218  1.1      cgd 	 *
    219  1.1      cgd 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    220  1.1      cgd 	 */
    221  1.1      cgd 	case PRU_DISCONNECT:
    222  1.1      cgd 		tp = tcp_disconnect(tp);
    223  1.1      cgd 		break;
    224  1.1      cgd 
    225  1.1      cgd 	/*
    226  1.1      cgd 	 * Accept a connection.  Essentially all the work is
    227  1.1      cgd 	 * done at higher levels; just return the address
    228  1.1      cgd 	 * of the peer, storing through addr.
    229  1.1      cgd 	 */
    230  1.8  mycroft 	case PRU_ACCEPT:
    231  1.8  mycroft 		in_setpeeraddr(inp, nam);
    232  1.1      cgd 		break;
    233  1.1      cgd 
    234  1.1      cgd 	/*
    235  1.1      cgd 	 * Mark the connection as being incapable of further output.
    236  1.1      cgd 	 */
    237  1.1      cgd 	case PRU_SHUTDOWN:
    238  1.1      cgd 		socantsendmore(so);
    239  1.1      cgd 		tp = tcp_usrclosed(tp);
    240  1.1      cgd 		if (tp)
    241  1.1      cgd 			error = tcp_output(tp);
    242  1.1      cgd 		break;
    243  1.1      cgd 
    244  1.1      cgd 	/*
    245  1.1      cgd 	 * After a receive, possibly send window update to peer.
    246  1.1      cgd 	 */
    247  1.1      cgd 	case PRU_RCVD:
    248  1.1      cgd 		(void) tcp_output(tp);
    249  1.1      cgd 		break;
    250  1.1      cgd 
    251  1.1      cgd 	/*
    252  1.1      cgd 	 * Do a send by putting data in output queue and updating urgent
    253  1.1      cgd 	 * marker if URG set.  Possibly send more data.
    254  1.1      cgd 	 */
    255  1.1      cgd 	case PRU_SEND:
    256  1.1      cgd 		sbappend(&so->so_snd, m);
    257  1.1      cgd 		error = tcp_output(tp);
    258  1.1      cgd 		break;
    259  1.1      cgd 
    260  1.1      cgd 	/*
    261  1.1      cgd 	 * Abort the TCP.
    262  1.1      cgd 	 */
    263  1.1      cgd 	case PRU_ABORT:
    264  1.1      cgd 		tp = tcp_drop(tp, ECONNABORTED);
    265  1.1      cgd 		break;
    266  1.1      cgd 
    267  1.1      cgd 	case PRU_SENSE:
    268  1.1      cgd 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    269  1.1      cgd 		(void) splx(s);
    270  1.1      cgd 		return (0);
    271  1.1      cgd 
    272  1.1      cgd 	case PRU_RCVOOB:
    273  1.1      cgd 		if ((so->so_oobmark == 0 &&
    274  1.1      cgd 		    (so->so_state & SS_RCVATMARK) == 0) ||
    275  1.1      cgd 		    so->so_options & SO_OOBINLINE ||
    276  1.1      cgd 		    tp->t_oobflags & TCPOOB_HADDATA) {
    277  1.1      cgd 			error = EINVAL;
    278  1.1      cgd 			break;
    279  1.1      cgd 		}
    280  1.1      cgd 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    281  1.1      cgd 			error = EWOULDBLOCK;
    282  1.1      cgd 			break;
    283  1.1      cgd 		}
    284  1.1      cgd 		m->m_len = 1;
    285  1.1      cgd 		*mtod(m, caddr_t) = tp->t_iobc;
    286  1.1      cgd 		if (((int)nam & MSG_PEEK) == 0)
    287  1.1      cgd 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    288  1.1      cgd 		break;
    289  1.1      cgd 
    290  1.1      cgd 	case PRU_SENDOOB:
    291  1.1      cgd 		if (sbspace(&so->so_snd) < -512) {
    292  1.1      cgd 			m_freem(m);
    293  1.1      cgd 			error = ENOBUFS;
    294  1.1      cgd 			break;
    295  1.1      cgd 		}
    296  1.1      cgd 		/*
    297  1.1      cgd 		 * According to RFC961 (Assigned Protocols),
    298  1.1      cgd 		 * the urgent pointer points to the last octet
    299  1.1      cgd 		 * of urgent data.  We continue, however,
    300  1.1      cgd 		 * to consider it to indicate the first octet
    301  1.1      cgd 		 * of data past the urgent section.
    302  1.1      cgd 		 * Otherwise, snd_up should be one lower.
    303  1.1      cgd 		 */
    304  1.1      cgd 		sbappend(&so->so_snd, m);
    305  1.1      cgd 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    306  1.1      cgd 		tp->t_force = 1;
    307  1.1      cgd 		error = tcp_output(tp);
    308  1.1      cgd 		tp->t_force = 0;
    309  1.1      cgd 		break;
    310  1.1      cgd 
    311  1.1      cgd 	case PRU_SOCKADDR:
    312  1.1      cgd 		in_setsockaddr(inp, nam);
    313  1.1      cgd 		break;
    314  1.1      cgd 
    315  1.1      cgd 	case PRU_PEERADDR:
    316  1.1      cgd 		in_setpeeraddr(inp, nam);
    317  1.1      cgd 		break;
    318  1.1      cgd 
    319  1.1      cgd 	/*
    320  1.1      cgd 	 * TCP slow timer went off; going through this
    321  1.1      cgd 	 * routine for tracing's sake.
    322  1.1      cgd 	 */
    323  1.1      cgd 	case PRU_SLOWTIMO:
    324  1.1      cgd 		tp = tcp_timers(tp, (int)nam);
    325  1.1      cgd 		req |= (int)nam << 8;		/* for debug's sake */
    326  1.1      cgd 		break;
    327  1.1      cgd 
    328  1.1      cgd 	default:
    329  1.1      cgd 		panic("tcp_usrreq");
    330  1.1      cgd 	}
    331  1.1      cgd 	if (tp && (so->so_options & SO_DEBUG))
    332  1.1      cgd 		tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req);
    333  1.1      cgd 	splx(s);
    334  1.1      cgd 	return (error);
    335  1.1      cgd }
    336  1.1      cgd 
    337  1.6  mycroft int
    338  1.1      cgd tcp_ctloutput(op, so, level, optname, mp)
    339  1.1      cgd 	int op;
    340  1.1      cgd 	struct socket *so;
    341  1.1      cgd 	int level, optname;
    342  1.1      cgd 	struct mbuf **mp;
    343  1.1      cgd {
    344  1.9  mycroft 	int error = 0, s;
    345  1.9  mycroft 	struct inpcb *inp;
    346  1.9  mycroft 	register struct tcpcb *tp;
    347  1.1      cgd 	register struct mbuf *m;
    348  1.9  mycroft 	register int i;
    349  1.1      cgd 
    350  1.9  mycroft 	s = splnet();
    351  1.9  mycroft 	inp = sotoinpcb(so);
    352  1.9  mycroft 	if (inp == NULL) {
    353  1.9  mycroft 		splx(s);
    354  1.9  mycroft 		if (op == PRCO_SETOPT && *mp)
    355  1.9  mycroft 			(void) m_free(*mp);
    356  1.9  mycroft 		return (ECONNRESET);
    357  1.9  mycroft 	}
    358  1.9  mycroft 	if (level != IPPROTO_TCP) {
    359  1.9  mycroft 		error = ip_ctloutput(op, so, level, optname, mp);
    360  1.9  mycroft 		splx(s);
    361  1.9  mycroft 		return (error);
    362  1.9  mycroft 	}
    363  1.9  mycroft 	tp = intotcpcb(inp);
    364  1.1      cgd 
    365  1.1      cgd 	switch (op) {
    366  1.1      cgd 
    367  1.1      cgd 	case PRCO_SETOPT:
    368  1.1      cgd 		m = *mp;
    369  1.1      cgd 		switch (optname) {
    370  1.1      cgd 
    371  1.1      cgd 		case TCP_NODELAY:
    372  1.1      cgd 			if (m == NULL || m->m_len < sizeof (int))
    373  1.1      cgd 				error = EINVAL;
    374  1.1      cgd 			else if (*mtod(m, int *))
    375  1.1      cgd 				tp->t_flags |= TF_NODELAY;
    376  1.1      cgd 			else
    377  1.1      cgd 				tp->t_flags &= ~TF_NODELAY;
    378  1.1      cgd 			break;
    379  1.1      cgd 
    380  1.9  mycroft 		case TCP_MAXSEG:
    381  1.9  mycroft 			if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
    382  1.9  mycroft 				tp->t_maxseg = i;
    383  1.9  mycroft 			else
    384  1.9  mycroft 				error = EINVAL;
    385  1.9  mycroft 			break;
    386  1.9  mycroft 
    387  1.1      cgd 		default:
    388  1.9  mycroft 			error = ENOPROTOOPT;
    389  1.1      cgd 			break;
    390  1.1      cgd 		}
    391  1.1      cgd 		if (m)
    392  1.1      cgd 			(void) m_free(m);
    393  1.1      cgd 		break;
    394  1.1      cgd 
    395  1.1      cgd 	case PRCO_GETOPT:
    396  1.1      cgd 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    397  1.1      cgd 		m->m_len = sizeof(int);
    398  1.1      cgd 
    399  1.1      cgd 		switch (optname) {
    400  1.1      cgd 		case TCP_NODELAY:
    401  1.1      cgd 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    402  1.1      cgd 			break;
    403  1.1      cgd 		case TCP_MAXSEG:
    404  1.1      cgd 			*mtod(m, int *) = tp->t_maxseg;
    405  1.1      cgd 			break;
    406  1.1      cgd 		default:
    407  1.9  mycroft 			error = ENOPROTOOPT;
    408  1.1      cgd 			break;
    409  1.1      cgd 		}
    410  1.1      cgd 		break;
    411  1.1      cgd 	}
    412  1.9  mycroft 	splx(s);
    413  1.1      cgd 	return (error);
    414  1.1      cgd }
    415  1.1      cgd 
    416  1.4      cgd u_long	tcp_sendspace = 1024*8;
    417  1.4      cgd u_long	tcp_recvspace = 1024*8;
    418  1.1      cgd 
    419  1.1      cgd /*
    420  1.1      cgd  * Attach TCP protocol to socket, allocating
    421  1.1      cgd  * internet protocol control block, tcp control block,
    422  1.1      cgd  * bufer space, and entering LISTEN state if to accept connections.
    423  1.1      cgd  */
    424  1.6  mycroft int
    425  1.1      cgd tcp_attach(so)
    426  1.1      cgd 	struct socket *so;
    427  1.1      cgd {
    428  1.1      cgd 	register struct tcpcb *tp;
    429  1.1      cgd 	struct inpcb *inp;
    430  1.1      cgd 	int error;
    431  1.1      cgd 
    432  1.1      cgd 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    433  1.1      cgd 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    434  1.1      cgd 		if (error)
    435  1.1      cgd 			return (error);
    436  1.1      cgd 	}
    437  1.1      cgd 	error = in_pcballoc(so, &tcb);
    438  1.1      cgd 	if (error)
    439  1.1      cgd 		return (error);
    440  1.1      cgd 	inp = sotoinpcb(so);
    441  1.1      cgd 	tp = tcp_newtcpcb(inp);
    442  1.1      cgd 	if (tp == 0) {
    443  1.1      cgd 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    444  1.1      cgd 
    445  1.1      cgd 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    446  1.1      cgd 		in_pcbdetach(inp);
    447  1.1      cgd 		so->so_state |= nofd;
    448  1.1      cgd 		return (ENOBUFS);
    449  1.1      cgd 	}
    450  1.1      cgd 	tp->t_state = TCPS_CLOSED;
    451  1.1      cgd 	return (0);
    452  1.1      cgd }
    453  1.1      cgd 
    454  1.1      cgd /*
    455  1.1      cgd  * Initiate (or continue) disconnect.
    456  1.1      cgd  * If embryonic state, just send reset (once).
    457  1.1      cgd  * If in ``let data drain'' option and linger null, just drop.
    458  1.1      cgd  * Otherwise (hard), mark socket disconnecting and drop
    459  1.1      cgd  * current input data; switch states based on user close, and
    460  1.1      cgd  * send segment to peer (with FIN).
    461  1.1      cgd  */
    462  1.1      cgd struct tcpcb *
    463  1.1      cgd tcp_disconnect(tp)
    464  1.1      cgd 	register struct tcpcb *tp;
    465  1.1      cgd {
    466  1.1      cgd 	struct socket *so = tp->t_inpcb->inp_socket;
    467  1.1      cgd 
    468  1.1      cgd 	if (tp->t_state < TCPS_ESTABLISHED)
    469  1.1      cgd 		tp = tcp_close(tp);
    470  1.1      cgd 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    471  1.1      cgd 		tp = tcp_drop(tp, 0);
    472  1.1      cgd 	else {
    473  1.1      cgd 		soisdisconnecting(so);
    474  1.1      cgd 		sbflush(&so->so_rcv);
    475  1.1      cgd 		tp = tcp_usrclosed(tp);
    476  1.1      cgd 		if (tp)
    477  1.1      cgd 			(void) tcp_output(tp);
    478  1.1      cgd 	}
    479  1.1      cgd 	return (tp);
    480  1.1      cgd }
    481  1.1      cgd 
    482  1.1      cgd /*
    483  1.1      cgd  * User issued close, and wish to trail through shutdown states:
    484  1.1      cgd  * if never received SYN, just forget it.  If got a SYN from peer,
    485  1.1      cgd  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    486  1.1      cgd  * If already got a FIN from peer, then almost done; go to LAST_ACK
    487  1.1      cgd  * state.  In all other cases, have already sent FIN to peer (e.g.
    488  1.1      cgd  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    489  1.1      cgd  * for peer to send FIN or not respond to keep-alives, etc.
    490  1.1      cgd  * We can let the user exit from the close as soon as the FIN is acked.
    491  1.1      cgd  */
    492  1.1      cgd struct tcpcb *
    493  1.1      cgd tcp_usrclosed(tp)
    494  1.1      cgd 	register struct tcpcb *tp;
    495  1.1      cgd {
    496  1.1      cgd 
    497  1.1      cgd 	switch (tp->t_state) {
    498  1.1      cgd 
    499  1.1      cgd 	case TCPS_CLOSED:
    500  1.1      cgd 	case TCPS_LISTEN:
    501  1.1      cgd 	case TCPS_SYN_SENT:
    502  1.1      cgd 		tp->t_state = TCPS_CLOSED;
    503  1.1      cgd 		tp = tcp_close(tp);
    504  1.1      cgd 		break;
    505  1.1      cgd 
    506  1.1      cgd 	case TCPS_SYN_RECEIVED:
    507  1.1      cgd 	case TCPS_ESTABLISHED:
    508  1.1      cgd 		tp->t_state = TCPS_FIN_WAIT_1;
    509  1.1      cgd 		break;
    510  1.1      cgd 
    511  1.1      cgd 	case TCPS_CLOSE_WAIT:
    512  1.1      cgd 		tp->t_state = TCPS_LAST_ACK;
    513  1.1      cgd 		break;
    514  1.1      cgd 	}
    515  1.1      cgd 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
    516  1.1      cgd 		soisdisconnected(tp->t_inpcb->inp_socket);
    517  1.1      cgd 	return (tp);
    518  1.1      cgd }
    519