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