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