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