tcp_usrreq.c revision 53353
1251876Speter/*
2251876Speter * Copyright (c) 1982, 1986, 1988, 1993
3251876Speter *	The Regents of the University of California.  All rights reserved.
4251876Speter *
5251876Speter * Redistribution and use in source and binary forms, with or without
6251876Speter * modification, are permitted provided that the following conditions
7251876Speter * are met:
8251876Speter * 1. Redistributions of source code must retain the above copyright
9251876Speter *    notice, this list of conditions and the following disclaimer.
10251876Speter * 2. Redistributions in binary form must reproduce the above copyright
11251876Speter *    notice, this list of conditions and the following disclaimer in the
12251876Speter *    documentation and/or other materials provided with the distribution.
13251876Speter * 3. All advertising materials mentioning features or use of this software
14251876Speter *    must display the following acknowledgement:
15251876Speter *	This product includes software developed by the University of
16251876Speter *	California, Berkeley and its contributors.
17251876Speter * 4. Neither the name of the University nor the names of its contributors
18251876Speter *    may be used to endorse or promote products derived from this software
19251876Speter *    without specific prior written permission.
20251876Speter *
21251876Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22251876Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23251876Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24251876Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25251876Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26251876Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27251876Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28251876Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29251876Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30251876Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31251876Speter * SUCH DAMAGE.
32251876Speter *
33251876Speter *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34251876Speter * $FreeBSD: head/sys/netinet/tcp_usrreq.c 53353 1999-11-18 08:28:24Z peter $
35251876Speter */
36251876Speter
37253734Speter#include "opt_tcpdebug.h"
38251876Speter
39251876Speter#include <sys/param.h>
40251876Speter#include <sys/systm.h>
41251876Speter#include <sys/kernel.h>
42251876Speter#include <sys/sysctl.h>
43251876Speter#include <sys/mbuf.h>
44251876Speter#include <sys/socket.h>
45251876Speter#include <sys/socketvar.h>
46251876Speter#include <sys/protosw.h>
47251876Speter
48251876Speter#include <net/if.h>
49251876Speter#include <net/route.h>
50251876Speter
51251876Speter#include <netinet/in.h>
52251876Speter#include <netinet/in_systm.h>
53251876Speter#include <netinet/in_pcb.h>
54251876Speter#include <netinet/in_var.h>
55251876Speter#include <netinet/ip_var.h>
56251876Speter#include <netinet/tcp.h>
57251876Speter#include <netinet/tcp_fsm.h>
58251876Speter#include <netinet/tcp_seq.h>
59251876Speter#include <netinet/tcp_timer.h>
60251876Speter#include <netinet/tcp_var.h>
61251876Speter#include <netinet/tcpip.h>
62251876Speter#ifdef TCPDEBUG
63251876Speter#include <netinet/tcp_debug.h>
64251876Speter#endif
65251876Speter
66251876Speter/*
67251876Speter * TCP protocol interface to socket abstraction.
68251876Speter */
69251876Speterextern	char *tcpstates[];	/* XXX ??? */
70251876Speter
71251876Speterstatic int	tcp_attach __P((struct socket *, struct proc *));
72251876Speterstatic int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
73251876Speter				 struct proc *));
74251876Speterstatic struct tcpcb *
75251876Speter		tcp_disconnect __P((struct tcpcb *));
76251876Speterstatic struct tcpcb *
77251876Speter		tcp_usrclosed __P((struct tcpcb *));
78251876Speter
79251876Speter#ifdef TCPDEBUG
80251876Speter#define	TCPDEBUG0	int ostate
81251876Speter#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
82251876Speter#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
83251876Speter				tcp_trace(TA_USER, ostate, tp, 0, req)
84251876Speter#else
85251876Speter#define	TCPDEBUG0
86251876Speter#define	TCPDEBUG1()
87251876Speter#define	TCPDEBUG2(req)
88251876Speter#endif
89251876Speter
90251876Speter/*
91 * TCP attaches to socket via pru_attach(), reserving space,
92 * and an internet control block.
93 */
94static int
95tcp_usr_attach(struct socket *so, int proto, struct proc *p)
96{
97	int s = splnet();
98	int error;
99	struct inpcb *inp = sotoinpcb(so);
100	struct tcpcb *tp = 0;
101	TCPDEBUG0;
102
103	TCPDEBUG1();
104	if (inp) {
105		error = EISCONN;
106		goto out;
107	}
108
109	error = tcp_attach(so, p);
110	if (error)
111		goto out;
112
113	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
114		so->so_linger = TCP_LINGERTIME;
115	tp = sototcpcb(so);
116out:
117	TCPDEBUG2(PRU_ATTACH);
118	splx(s);
119	return error;
120}
121
122/*
123 * pru_detach() detaches the TCP protocol from the socket.
124 * If the protocol state is non-embryonic, then can't
125 * do this directly: have to initiate a pru_disconnect(),
126 * which may finish later; embryonic TCB's can just
127 * be discarded here.
128 */
129static int
130tcp_usr_detach(struct socket *so)
131{
132	int s = splnet();
133	int error = 0;
134	struct inpcb *inp = sotoinpcb(so);
135	struct tcpcb *tp;
136	TCPDEBUG0;
137
138	if (inp == 0) {
139		splx(s);
140		return EINVAL;	/* XXX */
141	}
142	tp = intotcpcb(inp);
143	TCPDEBUG1();
144	tp = tcp_disconnect(tp);
145
146	TCPDEBUG2(PRU_DETACH);
147	splx(s);
148	return error;
149}
150
151#define	COMMON_START()	TCPDEBUG0; \
152			do { \
153				     if (inp == 0) { \
154					     splx(s); \
155					     return EINVAL; \
156				     } \
157				     tp = intotcpcb(inp); \
158				     TCPDEBUG1(); \
159		     } while(0)
160
161#define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
162
163
164/*
165 * Give the socket an address.
166 */
167static int
168tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
169{
170	int s = splnet();
171	int error = 0;
172	struct inpcb *inp = sotoinpcb(so);
173	struct tcpcb *tp;
174	struct sockaddr_in *sinp;
175
176	COMMON_START();
177
178	/*
179	 * Must check for multicast addresses and disallow binding
180	 * to them.
181	 */
182	sinp = (struct sockaddr_in *)nam;
183	if (sinp->sin_family == AF_INET &&
184	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
185		error = EAFNOSUPPORT;
186		goto out;
187	}
188	error = in_pcbbind(inp, nam, p);
189	if (error)
190		goto out;
191	COMMON_END(PRU_BIND);
192
193}
194
195/*
196 * Prepare to accept connections.
197 */
198static int
199tcp_usr_listen(struct socket *so, struct proc *p)
200{
201	int s = splnet();
202	int error = 0;
203	struct inpcb *inp = sotoinpcb(so);
204	struct tcpcb *tp;
205
206	COMMON_START();
207	if (inp->inp_lport == 0)
208		error = in_pcbbind(inp, (struct sockaddr *)0, p);
209	if (error == 0)
210		tp->t_state = TCPS_LISTEN;
211	COMMON_END(PRU_LISTEN);
212}
213
214/*
215 * Initiate connection to peer.
216 * Create a template for use in transmissions on this connection.
217 * Enter SYN_SENT state, and mark socket as connecting.
218 * Start keep-alive timer, and seed output sequence space.
219 * Send initial segment on connection.
220 */
221static int
222tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
223{
224	int s = splnet();
225	int error = 0;
226	struct inpcb *inp = sotoinpcb(so);
227	struct tcpcb *tp;
228	struct sockaddr_in *sinp;
229
230	COMMON_START();
231
232	/*
233	 * Must disallow TCP ``connections'' to multicast addresses.
234	 */
235	sinp = (struct sockaddr_in *)nam;
236	if (sinp->sin_family == AF_INET
237	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
238		error = EAFNOSUPPORT;
239		goto out;
240	}
241
242	prison_remote_ip(p, 0, &sinp->sin_addr.s_addr);
243
244	if ((error = tcp_connect(tp, nam, p)) != 0)
245		goto out;
246	error = tcp_output(tp);
247	COMMON_END(PRU_CONNECT);
248}
249
250/*
251 * Initiate disconnect from peer.
252 * If connection never passed embryonic stage, just drop;
253 * else if don't need to let data drain, then can just drop anyways,
254 * else have to begin TCP shutdown process: mark socket disconnecting,
255 * drain unread data, state switch to reflect user close, and
256 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
257 * when peer sends FIN and acks ours.
258 *
259 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
260 */
261static int
262tcp_usr_disconnect(struct socket *so)
263{
264	int s = splnet();
265	int error = 0;
266	struct inpcb *inp = sotoinpcb(so);
267	struct tcpcb *tp;
268
269	COMMON_START();
270	tp = tcp_disconnect(tp);
271	COMMON_END(PRU_DISCONNECT);
272}
273
274/*
275 * Accept a connection.  Essentially all the work is
276 * done at higher levels; just return the address
277 * of the peer, storing through addr.
278 */
279static int
280tcp_usr_accept(struct socket *so, struct sockaddr **nam)
281{
282	int s = splnet();
283	int error = 0;
284	struct inpcb *inp = sotoinpcb(so);
285	struct tcpcb *tp;
286
287	COMMON_START();
288	in_setpeeraddr(so, nam);
289	COMMON_END(PRU_ACCEPT);
290}
291
292/*
293 * Mark the connection as being incapable of further output.
294 */
295static int
296tcp_usr_shutdown(struct socket *so)
297{
298	int s = splnet();
299	int error = 0;
300	struct inpcb *inp = sotoinpcb(so);
301	struct tcpcb *tp;
302
303	COMMON_START();
304	socantsendmore(so);
305	tp = tcp_usrclosed(tp);
306	if (tp)
307		error = tcp_output(tp);
308	COMMON_END(PRU_SHUTDOWN);
309}
310
311/*
312 * After a receive, possibly send window update to peer.
313 */
314static int
315tcp_usr_rcvd(struct socket *so, int flags)
316{
317	int s = splnet();
318	int error = 0;
319	struct inpcb *inp = sotoinpcb(so);
320	struct tcpcb *tp;
321
322	COMMON_START();
323	tcp_output(tp);
324	COMMON_END(PRU_RCVD);
325}
326
327/*
328 * Do a send by putting data in output queue and updating urgent
329 * marker if URG set.  Possibly send more data.  Unlike the other
330 * pru_*() routines, the mbuf chains are our responsibility.  We
331 * must either enqueue them or free them.  The other pru_* routines
332 * generally are caller-frees.
333 */
334static int
335tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
336	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
337{
338	int s = splnet();
339	int error = 0;
340	struct inpcb *inp = sotoinpcb(so);
341	struct tcpcb *tp;
342	TCPDEBUG0;
343
344	if (inp == NULL) {
345		/*
346		 * OOPS! we lost a race, the TCP session got reset after
347		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
348		 * network interrupt in the non-splnet() section of sosend().
349		 */
350		if (m)
351			m_freem(m);
352		if (control)
353			m_freem(control);
354		error = ECONNRESET;	/* XXX EPIPE? */
355		tp = NULL;
356		TCPDEBUG1();
357		goto out;
358	}
359	tp = intotcpcb(inp);
360	TCPDEBUG1();
361	if (control) {
362		/* TCP doesn't do control messages (rights, creds, etc) */
363		if (control->m_len) {
364			m_freem(control);
365			if (m)
366				m_freem(m);
367			error = EINVAL;
368			goto out;
369		}
370		m_freem(control);	/* empty control, just free it */
371	}
372	if(!(flags & PRUS_OOB)) {
373		sbappend(&so->so_snd, m);
374		if (nam && tp->t_state < TCPS_SYN_SENT) {
375			/*
376			 * Do implied connect if not yet connected,
377			 * initialize window to default value, and
378			 * initialize maxseg/maxopd using peer's cached
379			 * MSS.
380			 */
381			error = tcp_connect(tp, nam, p);
382			if (error)
383				goto out;
384			tp->snd_wnd = TTCP_CLIENT_SND_WND;
385			tcp_mss(tp, -1);
386		}
387
388		if (flags & PRUS_EOF) {
389			/*
390			 * Close the send side of the connection after
391			 * the data is sent.
392			 */
393			socantsendmore(so);
394			tp = tcp_usrclosed(tp);
395		}
396		if (tp != NULL) {
397			if (flags & PRUS_MORETOCOME)
398				tp->t_flags |= TF_MORETOCOME;
399			error = tcp_output(tp);
400			if (flags & PRUS_MORETOCOME)
401				tp->t_flags &= ~TF_MORETOCOME;
402		}
403	} else {
404		if (sbspace(&so->so_snd) < -512) {
405			m_freem(m);
406			error = ENOBUFS;
407			goto out;
408		}
409		/*
410		 * According to RFC961 (Assigned Protocols),
411		 * the urgent pointer points to the last octet
412		 * of urgent data.  We continue, however,
413		 * to consider it to indicate the first octet
414		 * of data past the urgent section.
415		 * Otherwise, snd_up should be one lower.
416		 */
417		sbappend(&so->so_snd, m);
418		if (nam && tp->t_state < TCPS_SYN_SENT) {
419			/*
420			 * Do implied connect if not yet connected,
421			 * initialize window to default value, and
422			 * initialize maxseg/maxopd using peer's cached
423			 * MSS.
424			 */
425			error = tcp_connect(tp, nam, p);
426			if (error)
427				goto out;
428			tp->snd_wnd = TTCP_CLIENT_SND_WND;
429			tcp_mss(tp, -1);
430		}
431		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
432		tp->t_force = 1;
433		error = tcp_output(tp);
434		tp->t_force = 0;
435	}
436	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
437		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
438}
439
440/*
441 * Abort the TCP.
442 */
443static int
444tcp_usr_abort(struct socket *so)
445{
446	int s = splnet();
447	int error = 0;
448	struct inpcb *inp = sotoinpcb(so);
449	struct tcpcb *tp;
450
451	COMMON_START();
452	tp = tcp_drop(tp, ECONNABORTED);
453	COMMON_END(PRU_ABORT);
454}
455
456/*
457 * Receive out-of-band data.
458 */
459static int
460tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
461{
462	int s = splnet();
463	int error = 0;
464	struct inpcb *inp = sotoinpcb(so);
465	struct tcpcb *tp;
466
467	COMMON_START();
468	if ((so->so_oobmark == 0 &&
469	     (so->so_state & SS_RCVATMARK) == 0) ||
470	    so->so_options & SO_OOBINLINE ||
471	    tp->t_oobflags & TCPOOB_HADDATA) {
472		error = EINVAL;
473		goto out;
474	}
475	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
476		error = EWOULDBLOCK;
477		goto out;
478	}
479	m->m_len = 1;
480	*mtod(m, caddr_t) = tp->t_iobc;
481	if ((flags & MSG_PEEK) == 0)
482		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
483	COMMON_END(PRU_RCVOOB);
484}
485
486/* xxx - should be const */
487struct pr_usrreqs tcp_usrreqs = {
488	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
489	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
490	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
491	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
492	in_setsockaddr, sosend, soreceive, sopoll
493};
494
495/*
496 * Common subroutine to open a TCP connection to remote host specified
497 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
498 * port number if needed.  Call in_pcbladdr to do the routing and to choose
499 * a local host address (interface).  If there is an existing incarnation
500 * of the same connection in TIME-WAIT state and if the remote host was
501 * sending CC options and if the connection duration was < MSL, then
502 * truncate the previous TIME-WAIT state and proceed.
503 * Initialize connection parameters and enter SYN-SENT state.
504 */
505static int
506tcp_connect(tp, nam, p)
507	register struct tcpcb *tp;
508	struct sockaddr *nam;
509	struct proc *p;
510{
511	struct inpcb *inp = tp->t_inpcb, *oinp;
512	struct socket *so = inp->inp_socket;
513	struct tcpcb *otp;
514	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
515	struct sockaddr_in *ifaddr;
516	struct rmxp_tao *taop;
517	struct rmxp_tao tao_noncached;
518	int error;
519
520	if (inp->inp_lport == 0) {
521		error = in_pcbbind(inp, (struct sockaddr *)0, p);
522		if (error)
523			return error;
524	}
525
526	/*
527	 * Cannot simply call in_pcbconnect, because there might be an
528	 * earlier incarnation of this same connection still in
529	 * TIME_WAIT state, creating an ADDRINUSE error.
530	 */
531	error = in_pcbladdr(inp, nam, &ifaddr);
532	if (error)
533		return error;
534	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
535	    sin->sin_addr, sin->sin_port,
536	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
537						: ifaddr->sin_addr,
538	    inp->inp_lport,  0);
539	if (oinp) {
540		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
541		otp->t_state == TCPS_TIME_WAIT &&
542		    (ticks - otp->t_starttime) < tcp_msl &&
543		    (otp->t_flags & TF_RCVD_CC))
544			otp = tcp_close(otp);
545		else
546			return EADDRINUSE;
547	}
548	if (inp->inp_laddr.s_addr == INADDR_ANY)
549		inp->inp_laddr = ifaddr->sin_addr;
550	inp->inp_faddr = sin->sin_addr;
551	inp->inp_fport = sin->sin_port;
552	in_pcbrehash(inp);
553
554	tp->t_template = tcp_template(tp);
555	if (tp->t_template == 0) {
556		in_pcbdisconnect(inp);
557		return ENOBUFS;
558	}
559
560	/* Compute window scaling to request.  */
561	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
562	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
563		tp->request_r_scale++;
564
565	soisconnecting(so);
566	tcpstat.tcps_connattempt++;
567	tp->t_state = TCPS_SYN_SENT;
568	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
569	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
570	tcp_sendseqinit(tp);
571
572	/*
573	 * Generate a CC value for this connection and
574	 * check whether CC or CCnew should be used.
575	 */
576	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
577		taop = &tao_noncached;
578		bzero(taop, sizeof(*taop));
579	}
580
581	tp->cc_send = CC_INC(tcp_ccgen);
582	if (taop->tao_ccsent != 0 &&
583	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
584		taop->tao_ccsent = tp->cc_send;
585	} else {
586		taop->tao_ccsent = 0;
587		tp->t_flags |= TF_SENDCCNEW;
588	}
589
590	return 0;
591}
592
593/*
594 * The new sockopt interface makes it possible for us to block in the
595 * copyin/out step (if we take a page fault).  Taking a page fault at
596 * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
597 * use TSM, there probably isn't any need for this function to run at
598 * splnet() any more.  This needs more examination.)
599 */
600int
601tcp_ctloutput(so, sopt)
602	struct socket *so;
603	struct sockopt *sopt;
604{
605	int	error, opt, optval, s;
606	struct	inpcb *inp;
607	struct	tcpcb *tp;
608
609	error = 0;
610	s = splnet();		/* XXX */
611	inp = sotoinpcb(so);
612	if (inp == NULL) {
613		splx(s);
614		return (ECONNRESET);
615	}
616	if (sopt->sopt_level != IPPROTO_TCP) {
617		error = ip_ctloutput(so, sopt);
618		splx(s);
619		return (error);
620	}
621	tp = intotcpcb(inp);
622
623	switch (sopt->sopt_dir) {
624	case SOPT_SET:
625		switch (sopt->sopt_name) {
626		case TCP_NODELAY:
627		case TCP_NOOPT:
628		case TCP_NOPUSH:
629			error = sooptcopyin(sopt, &optval, sizeof optval,
630					    sizeof optval);
631			if (error)
632				break;
633
634			switch (sopt->sopt_name) {
635			case TCP_NODELAY:
636				opt = TF_NODELAY;
637				break;
638			case TCP_NOOPT:
639				opt = TF_NOOPT;
640				break;
641			case TCP_NOPUSH:
642				opt = TF_NOPUSH;
643				break;
644			default:
645				opt = 0; /* dead code to fool gcc */
646				break;
647			}
648
649			if (optval)
650				tp->t_flags |= opt;
651			else
652				tp->t_flags &= ~opt;
653			break;
654
655		case TCP_MAXSEG:
656			error = sooptcopyin(sopt, &optval, sizeof optval,
657					    sizeof optval);
658			if (error)
659				break;
660
661			if (optval > 0 && optval <= tp->t_maxseg)
662				tp->t_maxseg = optval;
663			else
664				error = EINVAL;
665			break;
666
667		default:
668			error = ENOPROTOOPT;
669			break;
670		}
671		break;
672
673	case SOPT_GET:
674		switch (sopt->sopt_name) {
675		case TCP_NODELAY:
676			optval = tp->t_flags & TF_NODELAY;
677			break;
678		case TCP_MAXSEG:
679			optval = tp->t_maxseg;
680			break;
681		case TCP_NOOPT:
682			optval = tp->t_flags & TF_NOOPT;
683			break;
684		case TCP_NOPUSH:
685			optval = tp->t_flags & TF_NOPUSH;
686			break;
687		default:
688			error = ENOPROTOOPT;
689			break;
690		}
691		if (error == 0)
692			error = sooptcopyout(sopt, &optval, sizeof optval);
693		break;
694	}
695	splx(s);
696	return (error);
697}
698
699/*
700 * tcp_sendspace and tcp_recvspace are the default send and receive window
701 * sizes, respectively.  These are obsolescent (this information should
702 * be set by the route).
703 */
704u_long	tcp_sendspace = 1024*16;
705SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
706    &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
707u_long	tcp_recvspace = 1024*16;
708SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
709    &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
710
711/*
712 * Attach TCP protocol to socket, allocating
713 * internet protocol control block, tcp control block,
714 * bufer space, and entering LISTEN state if to accept connections.
715 */
716static int
717tcp_attach(so, p)
718	struct socket *so;
719	struct proc *p;
720{
721	register struct tcpcb *tp;
722	struct inpcb *inp;
723	int error;
724
725	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
726		error = soreserve(so, tcp_sendspace, tcp_recvspace);
727		if (error)
728			return (error);
729	}
730	error = in_pcballoc(so, &tcbinfo, p);
731	if (error)
732		return (error);
733	inp = sotoinpcb(so);
734	tp = tcp_newtcpcb(inp);
735	if (tp == 0) {
736		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
737
738		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
739		in_pcbdetach(inp);
740		so->so_state |= nofd;
741		return (ENOBUFS);
742	}
743	tp->t_state = TCPS_CLOSED;
744	return (0);
745}
746
747/*
748 * Initiate (or continue) disconnect.
749 * If embryonic state, just send reset (once).
750 * If in ``let data drain'' option and linger null, just drop.
751 * Otherwise (hard), mark socket disconnecting and drop
752 * current input data; switch states based on user close, and
753 * send segment to peer (with FIN).
754 */
755static struct tcpcb *
756tcp_disconnect(tp)
757	register struct tcpcb *tp;
758{
759	struct socket *so = tp->t_inpcb->inp_socket;
760
761	if (tp->t_state < TCPS_ESTABLISHED)
762		tp = tcp_close(tp);
763	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
764		tp = tcp_drop(tp, 0);
765	else {
766		soisdisconnecting(so);
767		sbflush(&so->so_rcv);
768		tp = tcp_usrclosed(tp);
769		if (tp)
770			(void) tcp_output(tp);
771	}
772	return (tp);
773}
774
775/*
776 * User issued close, and wish to trail through shutdown states:
777 * if never received SYN, just forget it.  If got a SYN from peer,
778 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
779 * If already got a FIN from peer, then almost done; go to LAST_ACK
780 * state.  In all other cases, have already sent FIN to peer (e.g.
781 * after PRU_SHUTDOWN), and just have to play tedious game waiting
782 * for peer to send FIN or not respond to keep-alives, etc.
783 * We can let the user exit from the close as soon as the FIN is acked.
784 */
785static struct tcpcb *
786tcp_usrclosed(tp)
787	register struct tcpcb *tp;
788{
789
790	switch (tp->t_state) {
791
792	case TCPS_CLOSED:
793	case TCPS_LISTEN:
794		tp->t_state = TCPS_CLOSED;
795		tp = tcp_close(tp);
796		break;
797
798	case TCPS_SYN_SENT:
799	case TCPS_SYN_RECEIVED:
800		tp->t_flags |= TF_NEEDFIN;
801		break;
802
803	case TCPS_ESTABLISHED:
804		tp->t_state = TCPS_FIN_WAIT_1;
805		break;
806
807	case TCPS_CLOSE_WAIT:
808		tp->t_state = TCPS_LAST_ACK;
809		break;
810	}
811	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
812		soisdisconnected(tp->t_inpcb->inp_socket);
813		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
814		if (tp->t_state == TCPS_FIN_WAIT_2)
815			callout_reset(tp->tt_2msl, tcp_maxidle,
816				      tcp_timer_2msl, tp);
817	}
818	return (tp);
819}
820
821