tcp_usrreq.c revision 46155
117683Spst/*
217683Spst * Copyright (c) 1982, 1986, 1988, 1993
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that the following conditions
717683Spst * are met:
817683Spst * 1. Redistributions of source code must retain the above copyright
917683Spst *    notice, this list of conditions and the following disclaimer.
1017683Spst * 2. Redistributions in binary form must reproduce the above copyright
1117683Spst *    notice, this list of conditions and the following disclaimer in the
1217683Spst *    documentation and/or other materials provided with the distribution.
1317683Spst * 3. All advertising materials mentioning features or use of this software
1417683Spst *    must display the following acknowledgement:
1517683Spst *	This product includes software developed by the University of
1617683Spst *	California, Berkeley and its contributors.
1717683Spst * 4. Neither the name of the University nor the names of its contributors
1817683Spst *    may be used to endorse or promote products derived from this software
1917683Spst *    without specific prior written permission.
2017683Spst *
2156891Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22172680Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2317683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2417683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25127667Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26127667Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27127667Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28127667Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29127667Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30127667Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31127667Sbms * SUCH DAMAGE.
32127667Sbms *
33127667Sbms *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34127667Sbms *	$Id: tcp_usrreq.c,v 1.41 1999/04/24 18:25:35 ache Exp $
35127667Sbms */
36127667Sbms
37127667Sbms#include "opt_tcpdebug.h"
38127667Sbms
39127667Sbms#include <sys/param.h>
40127667Sbms#include <sys/systm.h>
41127667Sbms#include <sys/kernel.h>
42127667Sbms#include <sys/sysctl.h>
43127667Sbms#include <sys/mbuf.h>
44127667Sbms#include <sys/socket.h>
45127667Sbms#include <sys/socketvar.h>
46127667Sbms#include <sys/protosw.h>
47127667Sbms
48127667Sbms#include <net/if.h>
49127667Sbms#include <net/route.h>
50127667Sbms
51127667Sbms#include <netinet/in.h>
52127667Sbms#include <netinet/in_systm.h>
53127667Sbms#include <netinet/in_pcb.h>
54127667Sbms#include <netinet/in_var.h>
55127667Sbms#include <netinet/ip_var.h>
56127667Sbms#include <netinet/tcp.h>
57127667Sbms#include <netinet/tcp_fsm.h>
58127667Sbms#include <netinet/tcp_seq.h>
59147897Ssam#include <netinet/tcp_timer.h>
60147897Ssam#include <netinet/tcp_var.h>
61147897Ssam#include <netinet/tcpip.h>
62147897Ssam#ifdef TCPDEBUG
6317683Spst#include <netinet/tcp_debug.h>
6417683Spst#endif
6517683Spst
6617683Spst/*
6717683Spst * TCP protocol interface to socket abstraction.
6817683Spst */
6917683Spstextern	char *tcpstates[];	/* XXX ??? */
7056891Sfenner
71147897Ssamstatic int	tcp_attach __P((struct socket *, struct proc *));
7217683Spststatic int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
7317683Spst				 struct proc *));
7417683Spststatic struct tcpcb *
7517683Spst		tcp_disconnect __P((struct tcpcb *));
7617683Spststatic struct tcpcb *
7717683Spst		tcp_usrclosed __P((struct tcpcb *));
7817683Spst
7998533Sfenner#ifdef TCPDEBUG
8098533Sfenner#define	TCPDEBUG0	int ostate
8198533Sfenner#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
8298533Sfenner#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
8398533Sfenner				tcp_trace(TA_USER, ostate, tp, 0, req)
8498533Sfenner#else
8517683Spst#define	TCPDEBUG0
8617683Spst#define	TCPDEBUG1()
8798533Sfenner#define	TCPDEBUG2(req)
8898533Sfenner#endif
8998533Sfenner
9098533Sfenner/*
9198533Sfenner * TCP attaches to socket via pru_attach(), reserving space,
9298533Sfenner * and an internet control block.
9317683Spst */
9475110Sfennerstatic int
9598533Sfennertcp_usr_attach(struct socket *so, int proto, struct proc *p)
9698533Sfenner{
9798533Sfenner	int s = splnet();
9898533Sfenner	int error;
9956891Sfenner	struct inpcb *inp = sotoinpcb(so);
10098533Sfenner	struct tcpcb *tp = 0;
10198533Sfenner	TCPDEBUG0;
10256891Sfenner
10398533Sfenner	TCPDEBUG1();
10456891Sfenner	if (inp) {
10598533Sfenner		error = EISCONN;
10698533Sfenner		goto out;
10798533Sfenner	}
10898533Sfenner
10975110Sfenner	error = tcp_attach(so, p);
11098533Sfenner	if (error)
11198533Sfenner		goto out;
11298533Sfenner
11398533Sfenner	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
11498533Sfenner		so->so_linger = TCP_LINGERTIME;
11598533Sfenner	tp = sototcpcb(so);
116127667Sbmsout:
117127667Sbms	TCPDEBUG2(PRU_ATTACH);
118127667Sbms	splx(s);
119127667Sbms	return error;
120127667Sbms}
121127667Sbms
122127667Sbms/*
123127667Sbms * pru_detach() detaches the TCP protocol from the socket.
124127667Sbms * If the protocol state is non-embryonic, then can't
125127667Sbms * do this directly: have to initiate a pru_disconnect(),
126127667Sbms * which may finish later; embryonic TCB's can just
127127667Sbms * be discarded here.
128147897Ssam */
129147897Ssamstatic int
13017683Spsttcp_usr_detach(struct socket *so)
13117683Spst{
13217683Spst	int s = splnet();
13317683Spst	int error = 0;
13417683Spst	struct inpcb *inp = sotoinpcb(so);
13517683Spst	struct tcpcb *tp;
13617683Spst	TCPDEBUG0;
13717683Spst
13817683Spst	if (inp == 0) {
13917683Spst		splx(s);
140127667Sbms		return EINVAL;	/* XXX */
141127667Sbms	}
142127667Sbms	tp = intotcpcb(inp);
143127667Sbms	TCPDEBUG1();
144127667Sbms	tp = tcp_disconnect(tp);
145127667Sbms
146127667Sbms	TCPDEBUG2(PRU_DETACH);
147127667Sbms	splx(s);
148127667Sbms	return error;
149127667Sbms}
150127667Sbms
151127667Sbms#define	COMMON_START()	TCPDEBUG0; \
152127667Sbms			do { \
153127667Sbms				     if (inp == 0) { \
154127667Sbms					     splx(s); \
155127667Sbms					     return EINVAL; \
156127667Sbms				     } \
157127667Sbms				     tp = intotcpcb(inp); \
158127667Sbms				     TCPDEBUG1(); \
159127667Sbms		     } while(0)
160127667Sbms
161127667Sbms#define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
162127667Sbms
163127667Sbms
164127667Sbms/*
165127667Sbms * Give the socket an address.
166127667Sbms */
167127667Sbmsstatic int
168127667Sbmstcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
169127667Sbms{
170127667Sbms	int s = splnet();
171127667Sbms	int error = 0;
172127667Sbms	struct inpcb *inp = sotoinpcb(so);
173127667Sbms	struct tcpcb *tp;
174127667Sbms	struct sockaddr_in *sinp;
175127667Sbms
176127667Sbms	COMMON_START();
177172680Smlaier
178172680Smlaier	/*
179172680Smlaier	 * Must check for multicast addresses and disallow binding
180172680Smlaier	 * to them.
181147897Ssam	 */
182172680Smlaier	sinp = (struct sockaddr_in *)nam;
183172680Smlaier	if (sinp->sin_family == AF_INET &&
184172680Smlaier	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
185172680Smlaier		error = EAFNOSUPPORT;
186172680Smlaier		goto out;
187147897Ssam	}
188172680Smlaier	error = in_pcbbind(inp, nam, p);
18956891Sfenner	if (error)
19056891Sfenner		goto out;
19117683Spst	COMMON_END(PRU_BIND);
19217683Spst
19356891Sfenner}
19456891Sfenner
19517683Spst/*
19617683Spst * Prepare to accept connections.
19717683Spst */
19817683Spststatic int
19917683Spsttcp_usr_listen(struct socket *so, struct proc *p)
20017683Spst{
20117683Spst	int s = splnet();
20217683Spst	int error = 0;
203127667Sbms	struct inpcb *inp = sotoinpcb(so);
20417683Spst	struct tcpcb *tp;
20517683Spst
20617683Spst	COMMON_START();
20717683Spst	if (inp->inp_lport == 0)
20817683Spst		error = in_pcbbind(inp, (struct sockaddr *)0, p);
20917683Spst	if (error == 0)
21017683Spst		tp->t_state = TCPS_LISTEN;
21117683Spst	COMMON_END(PRU_LISTEN);
21217683Spst}
21317683Spst
21417683Spst/*
21517683Spst * Initiate connection to peer.
21617683Spst * Create a template for use in transmissions on this connection.
21717683Spst * Enter SYN_SENT state, and mark socket as connecting.
21817683Spst * Start keep-alive timer, and seed output sequence space.
21917683Spst * Send initial segment on connection.
22017683Spst */
22117683Spststatic int
22217683Spsttcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
22317683Spst{
22417683Spst	int s = splnet();
22517683Spst	int error = 0;
22617683Spst	struct inpcb *inp = sotoinpcb(so);
22717683Spst	struct tcpcb *tp;
22817683Spst	struct sockaddr_in *sinp;
22917683Spst
23017683Spst	COMMON_START();
23117683Spst
23217683Spst	/*
23317683Spst	 * Must disallow TCP ``connections'' to multicast addresses.
23417683Spst	 */
23517683Spst	sinp = (struct sockaddr_in *)nam;
23617683Spst	if (sinp->sin_family == AF_INET
23717683Spst	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
23817683Spst		error = EAFNOSUPPORT;
23917683Spst		goto out;
24017683Spst	}
24117683Spst
24217683Spst	prison_remote_ip(p, 0, &sinp->sin_addr.s_addr);
24317683Spst
24417683Spst	if ((error = tcp_connect(tp, nam, p)) != 0)
24517683Spst		goto out;
24617683Spst	error = tcp_output(tp);
24717683Spst	COMMON_END(PRU_CONNECT);
24817683Spst}
24917683Spst
25017683Spst/*
25117683Spst * Initiate disconnect from peer.
25217683Spst * If connection never passed embryonic stage, just drop;
25317683Spst * else if don't need to let data drain, then can just drop anyways,
25417683Spst * else have to begin TCP shutdown process: mark socket disconnecting,
25517683Spst * drain unread data, state switch to reflect user close, and
25617683Spst * send segment (e.g. FIN) to peer.  Socket will be really disconnected
25717683Spst * when peer sends FIN and acks ours.
25817683Spst *
25917683Spst * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
26017683Spst */
26117683Spststatic int
26217683Spsttcp_usr_disconnect(struct socket *so)
26317683Spst{
26417683Spst	int s = splnet();
26517683Spst	int error = 0;
26617683Spst	struct inpcb *inp = sotoinpcb(so);
26717683Spst	struct tcpcb *tp;
26817683Spst
26917683Spst	COMMON_START();
27017683Spst	tp = tcp_disconnect(tp);
27117683Spst	COMMON_END(PRU_DISCONNECT);
27217683Spst}
27317683Spst
27417683Spst/*
27517683Spst * Accept a connection.  Essentially all the work is
27617683Spst * done at higher levels; just return the address
27717683Spst * of the peer, storing through addr.
27817683Spst */
27917683Spststatic int
28098533Sfennertcp_usr_accept(struct socket *so, struct sockaddr **nam)
28117683Spst{
28256891Sfenner	int s = splnet();
28356891Sfenner	int error = 0;
28456891Sfenner	struct inpcb *inp = sotoinpcb(so);
28517683Spst	struct tcpcb *tp;
28617683Spst
28717683Spst	COMMON_START();
28817683Spst	in_setpeeraddr(so, nam);
28917683Spst	COMMON_END(PRU_ACCEPT);
29017683Spst}
29117683Spst
29217683Spst/*
29317683Spst * Mark the connection as being incapable of further output.
29417683Spst */
29575110Sfennerstatic int
296146771Ssamtcp_usr_shutdown(struct socket *so)
29775110Sfenner{
298162015Ssam	int s = splnet();
299162015Ssam	int error = 0;
300162015Ssam	struct inpcb *inp = sotoinpcb(so);
301147897Ssam	struct tcpcb *tp;
302127667Sbms
303127667Sbms	COMMON_START();
304127667Sbms	socantsendmore(so);
305172680Smlaier	tp = tcp_usrclosed(tp);
306147897Ssam	if (tp)
307147897Ssam		error = tcp_output(tp);
308127667Sbms	COMMON_END(PRU_SHUTDOWN);
309127667Sbms}
310127667Sbms
311127667Sbms/*
312127667Sbms * After a receive, possibly send window update to peer.
313127667Sbms */
314127667Sbmsstatic int
315127667Sbmstcp_usr_rcvd(struct socket *so, int flags)
31617683Spst{
31775110Sfenner	int s = splnet();
318147897Ssam	int error = 0;
31917683Spst	struct inpcb *inp = sotoinpcb(so);
32017683Spst	struct tcpcb *tp;
32117683Spst
32217683Spst	COMMON_START();
32317683Spst	tcp_output(tp);
32417683Spst	COMMON_END(PRU_RCVD);
325172680Smlaier}
32675110Sfenner
32717683Spst/*
32817683Spst * Do a send by putting data in output queue and updating urgent
32917683Spst * marker if URG set.  Possibly send more data.
33017683Spst */
33117683Spststatic int
33256891Sfennertcp_usr_send(struct socket *so, int flags, struct mbuf *m,
33356891Sfenner	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
334{
335	int s = splnet();
336	int error = 0;
337	struct inpcb *inp = sotoinpcb(so);
338	struct tcpcb *tp;
339
340	COMMON_START();
341	if (control && control->m_len) {
342		m_freem(control); /* XXX shouldn't caller do this??? */
343		if (m)
344			m_freem(m);
345		error = EINVAL;
346		goto out;
347	}
348
349	if(!(flags & PRUS_OOB)) {
350		sbappend(&so->so_snd, m);
351		if (nam && tp->t_state < TCPS_SYN_SENT) {
352			/*
353			 * Do implied connect if not yet connected,
354			 * initialize window to default value, and
355			 * initialize maxseg/maxopd using peer's cached
356			 * MSS.
357			 */
358			error = tcp_connect(tp, nam, p);
359			if (error)
360				goto out;
361			tp->snd_wnd = TTCP_CLIENT_SND_WND;
362			tcp_mss(tp, -1);
363		}
364
365		if (flags & PRUS_EOF) {
366			/*
367			 * Close the send side of the connection after
368			 * the data is sent.
369			 */
370			socantsendmore(so);
371			tp = tcp_usrclosed(tp);
372		}
373		if (tp != NULL) {
374			if (flags & PRUS_MORETOCOME)
375				tp->t_flags |= TF_MORETOCOME;
376			error = tcp_output(tp);
377			if (flags & PRUS_MORETOCOME)
378				tp->t_flags &= ~TF_MORETOCOME;
379		}
380	} else {
381		if (sbspace(&so->so_snd) < -512) {
382			m_freem(m);
383			error = ENOBUFS;
384			goto out;
385		}
386		/*
387		 * According to RFC961 (Assigned Protocols),
388		 * the urgent pointer points to the last octet
389		 * of urgent data.  We continue, however,
390		 * to consider it to indicate the first octet
391		 * of data past the urgent section.
392		 * Otherwise, snd_up should be one lower.
393		 */
394		sbappend(&so->so_snd, m);
395		if (nam && tp->t_state < TCPS_SYN_SENT) {
396			/*
397			 * Do implied connect if not yet connected,
398			 * initialize window to default value, and
399			 * initialize maxseg/maxopd using peer's cached
400			 * MSS.
401			 */
402			error = tcp_connect(tp, nam, p);
403			if (error)
404				goto out;
405			tp->snd_wnd = TTCP_CLIENT_SND_WND;
406			tcp_mss(tp, -1);
407		}
408		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
409		tp->t_force = 1;
410		error = tcp_output(tp);
411		tp->t_force = 0;
412	}
413	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
414		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
415}
416
417/*
418 * Abort the TCP.
419 */
420static int
421tcp_usr_abort(struct socket *so)
422{
423	int s = splnet();
424	int error = 0;
425	struct inpcb *inp = sotoinpcb(so);
426	struct tcpcb *tp;
427
428	COMMON_START();
429	tp = tcp_drop(tp, ECONNABORTED);
430	COMMON_END(PRU_ABORT);
431}
432
433/*
434 * Receive out-of-band data.
435 */
436static int
437tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
438{
439	int s = splnet();
440	int error = 0;
441	struct inpcb *inp = sotoinpcb(so);
442	struct tcpcb *tp;
443
444	COMMON_START();
445	if ((so->so_oobmark == 0 &&
446	     (so->so_state & SS_RCVATMARK) == 0) ||
447	    so->so_options & SO_OOBINLINE ||
448	    tp->t_oobflags & TCPOOB_HADDATA) {
449		error = EINVAL;
450		goto out;
451	}
452	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
453		error = EWOULDBLOCK;
454		goto out;
455	}
456	m->m_len = 1;
457	*mtod(m, caddr_t) = tp->t_iobc;
458	if ((flags & MSG_PEEK) == 0)
459		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
460	COMMON_END(PRU_RCVOOB);
461}
462
463/* xxx - should be const */
464struct pr_usrreqs tcp_usrreqs = {
465	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
466	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
467	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
468	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
469	in_setsockaddr, sosend, soreceive, sopoll
470};
471
472/*
473 * Common subroutine to open a TCP connection to remote host specified
474 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
475 * port number if needed.  Call in_pcbladdr to do the routing and to choose
476 * a local host address (interface).  If there is an existing incarnation
477 * of the same connection in TIME-WAIT state and if the remote host was
478 * sending CC options and if the connection duration was < MSL, then
479 * truncate the previous TIME-WAIT state and proceed.
480 * Initialize connection parameters and enter SYN-SENT state.
481 */
482static int
483tcp_connect(tp, nam, p)
484	register struct tcpcb *tp;
485	struct sockaddr *nam;
486	struct proc *p;
487{
488	struct inpcb *inp = tp->t_inpcb, *oinp;
489	struct socket *so = inp->inp_socket;
490	struct tcpcb *otp;
491	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
492	struct sockaddr_in *ifaddr;
493	struct rmxp_tao *taop;
494	struct rmxp_tao tao_noncached;
495	int error;
496
497	if (inp->inp_lport == 0) {
498		error = in_pcbbind(inp, (struct sockaddr *)0, p);
499		if (error)
500			return error;
501	}
502
503	/*
504	 * Cannot simply call in_pcbconnect, because there might be an
505	 * earlier incarnation of this same connection still in
506	 * TIME_WAIT state, creating an ADDRINUSE error.
507	 */
508	error = in_pcbladdr(inp, nam, &ifaddr);
509	if (error)
510		return error;
511	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
512	    sin->sin_addr, sin->sin_port,
513	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
514						: ifaddr->sin_addr,
515	    inp->inp_lport,  0);
516	if (oinp) {
517		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
518		otp->t_state == TCPS_TIME_WAIT &&
519		    otp->t_duration < TCPTV_MSL &&
520		    (otp->t_flags & TF_RCVD_CC))
521			otp = tcp_close(otp);
522		else
523			return EADDRINUSE;
524	}
525	if (inp->inp_laddr.s_addr == INADDR_ANY)
526		inp->inp_laddr = ifaddr->sin_addr;
527	inp->inp_faddr = sin->sin_addr;
528	inp->inp_fport = sin->sin_port;
529	in_pcbrehash(inp);
530
531	tp->t_template = tcp_template(tp);
532	if (tp->t_template == 0) {
533		in_pcbdisconnect(inp);
534		return ENOBUFS;
535	}
536
537	/* Compute window scaling to request.  */
538	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
539	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
540		tp->request_r_scale++;
541
542	soisconnecting(so);
543	tcpstat.tcps_connattempt++;
544	tp->t_state = TCPS_SYN_SENT;
545	tp->t_timer[TCPT_KEEP] = tcp_keepinit;
546	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
547	tcp_sendseqinit(tp);
548
549	/*
550	 * Generate a CC value for this connection and
551	 * check whether CC or CCnew should be used.
552	 */
553	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
554		taop = &tao_noncached;
555		bzero(taop, sizeof(*taop));
556	}
557
558	tp->cc_send = CC_INC(tcp_ccgen);
559	if (taop->tao_ccsent != 0 &&
560	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
561		taop->tao_ccsent = tp->cc_send;
562	} else {
563		taop->tao_ccsent = 0;
564		tp->t_flags |= TF_SENDCCNEW;
565	}
566
567	return 0;
568}
569
570/*
571 * The new sockopt interface makes it possible for us to block in the
572 * copyin/out step (if we take a page fault).  Taking a page fault at
573 * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
574 * use TSM, there probably isn't any need for this function to run at
575 * splnet() any more.  This needs more examination.)
576 */
577int
578tcp_ctloutput(so, sopt)
579	struct socket *so;
580	struct sockopt *sopt;
581{
582	int	error, opt, optval, s;
583	struct	inpcb *inp;
584	struct	tcpcb *tp;
585
586	error = 0;
587	s = splnet();		/* XXX */
588	inp = sotoinpcb(so);
589	if (inp == NULL) {
590		splx(s);
591		return (ECONNRESET);
592	}
593	if (sopt->sopt_level != IPPROTO_TCP) {
594		error = ip_ctloutput(so, sopt);
595		splx(s);
596		return (error);
597	}
598	tp = intotcpcb(inp);
599
600	switch (sopt->sopt_dir) {
601	case SOPT_SET:
602		switch (sopt->sopt_name) {
603		case TCP_NODELAY:
604		case TCP_NOOPT:
605		case TCP_NOPUSH:
606			error = sooptcopyin(sopt, &optval, sizeof optval,
607					    sizeof optval);
608			if (error)
609				break;
610
611			switch (sopt->sopt_name) {
612			case TCP_NODELAY:
613				opt = TF_NODELAY;
614				break;
615			case TCP_NOOPT:
616				opt = TF_NOOPT;
617				break;
618			case TCP_NOPUSH:
619				opt = TF_NOPUSH;
620				break;
621			default:
622				opt = 0; /* dead code to fool gcc */
623				break;
624			}
625
626			if (optval)
627				tp->t_flags |= opt;
628			else
629				tp->t_flags &= ~opt;
630			break;
631
632		case TCP_MAXSEG:
633			error = sooptcopyin(sopt, &optval, sizeof optval,
634					    sizeof optval);
635			if (error)
636				break;
637
638			if (optval > 0 && optval <= tp->t_maxseg)
639				tp->t_maxseg = optval;
640			else
641				error = EINVAL;
642			break;
643
644		default:
645			error = ENOPROTOOPT;
646			break;
647		}
648		break;
649
650	case SOPT_GET:
651		switch (sopt->sopt_name) {
652		case TCP_NODELAY:
653			optval = tp->t_flags & TF_NODELAY;
654			break;
655		case TCP_MAXSEG:
656			optval = tp->t_maxseg;
657			break;
658		case TCP_NOOPT:
659			optval = tp->t_flags & TF_NOOPT;
660			break;
661		case TCP_NOPUSH:
662			optval = tp->t_flags & TF_NOPUSH;
663			break;
664		default:
665			error = ENOPROTOOPT;
666			break;
667		}
668		if (error == 0)
669			error = sooptcopyout(sopt, &optval, sizeof optval);
670		break;
671	}
672	splx(s);
673	return (error);
674}
675
676/*
677 * tcp_sendspace and tcp_recvspace are the default send and receive window
678 * sizes, respectively.  These are obsolescent (this information should
679 * be set by the route).
680 */
681u_long	tcp_sendspace = 1024*16;
682SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
683	CTLFLAG_RW, &tcp_sendspace , 0, "");
684u_long	tcp_recvspace = 1024*16;
685SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
686	CTLFLAG_RW, &tcp_recvspace , 0, "");
687
688/*
689 * Attach TCP protocol to socket, allocating
690 * internet protocol control block, tcp control block,
691 * bufer space, and entering LISTEN state if to accept connections.
692 */
693static int
694tcp_attach(so, p)
695	struct socket *so;
696	struct proc *p;
697{
698	register struct tcpcb *tp;
699	struct inpcb *inp;
700	int error;
701
702	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
703		error = soreserve(so, tcp_sendspace, tcp_recvspace);
704		if (error)
705			return (error);
706	}
707	error = in_pcballoc(so, &tcbinfo, p);
708	if (error)
709		return (error);
710	inp = sotoinpcb(so);
711	tp = tcp_newtcpcb(inp);
712	if (tp == 0) {
713		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
714
715		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
716		in_pcbdetach(inp);
717		so->so_state |= nofd;
718		return (ENOBUFS);
719	}
720	tp->t_state = TCPS_CLOSED;
721	return (0);
722}
723
724/*
725 * Initiate (or continue) disconnect.
726 * If embryonic state, just send reset (once).
727 * If in ``let data drain'' option and linger null, just drop.
728 * Otherwise (hard), mark socket disconnecting and drop
729 * current input data; switch states based on user close, and
730 * send segment to peer (with FIN).
731 */
732static struct tcpcb *
733tcp_disconnect(tp)
734	register struct tcpcb *tp;
735{
736	struct socket *so = tp->t_inpcb->inp_socket;
737
738	if (tp->t_state < TCPS_ESTABLISHED)
739		tp = tcp_close(tp);
740	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
741		tp = tcp_drop(tp, 0);
742	else {
743		soisdisconnecting(so);
744		sbflush(&so->so_rcv);
745		tp = tcp_usrclosed(tp);
746		if (tp)
747			(void) tcp_output(tp);
748	}
749	return (tp);
750}
751
752/*
753 * User issued close, and wish to trail through shutdown states:
754 * if never received SYN, just forget it.  If got a SYN from peer,
755 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
756 * If already got a FIN from peer, then almost done; go to LAST_ACK
757 * state.  In all other cases, have already sent FIN to peer (e.g.
758 * after PRU_SHUTDOWN), and just have to play tedious game waiting
759 * for peer to send FIN or not respond to keep-alives, etc.
760 * We can let the user exit from the close as soon as the FIN is acked.
761 */
762static struct tcpcb *
763tcp_usrclosed(tp)
764	register struct tcpcb *tp;
765{
766
767	switch (tp->t_state) {
768
769	case TCPS_CLOSED:
770	case TCPS_LISTEN:
771		tp->t_state = TCPS_CLOSED;
772		tp = tcp_close(tp);
773		break;
774
775	case TCPS_SYN_SENT:
776	case TCPS_SYN_RECEIVED:
777		tp->t_flags |= TF_NEEDFIN;
778		break;
779
780	case TCPS_ESTABLISHED:
781		tp->t_state = TCPS_FIN_WAIT_1;
782		break;
783
784	case TCPS_CLOSE_WAIT:
785		tp->t_state = TCPS_LAST_ACK;
786		break;
787	}
788	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
789		soisdisconnected(tp->t_inpcb->inp_socket);
790		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
791		if (tp->t_state == TCPS_FIN_WAIT_2)
792			tp->t_timer[TCPT_2MSL] = tcp_maxidle;
793	}
794	return (tp);
795}
796
797