tcp_usrreq.c revision 54526
118691Sjoerg/*
218691Sjoerg * Copyright (c) 1982, 1986, 1988, 1993
318691Sjoerg *	The Regents of the University of California.  All rights reserved.
418691Sjoerg *
518691Sjoerg * Redistribution and use in source and binary forms, with or without
618691Sjoerg * modification, are permitted provided that the following conditions
718691Sjoerg * are met:
818691Sjoerg * 1. Redistributions of source code must retain the above copyright
918691Sjoerg *    notice, this list of conditions and the following disclaimer.
1018691Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1118691Sjoerg *    notice, this list of conditions and the following disclaimer in the
1218691Sjoerg *    documentation and/or other materials provided with the distribution.
1318691Sjoerg * 3. All advertising materials mentioning features or use of this software
1418691Sjoerg *    must display the following acknowledgement:
1518691Sjoerg *	This product includes software developed by the University of
1618691Sjoerg *	California, Berkeley and its contributors.
1718691Sjoerg * 4. Neither the name of the University nor the names of its contributors
1818691Sjoerg *    may be used to endorse or promote products derived from this software
1918691Sjoerg *    without specific prior written permission.
2018691Sjoerg *
2118691Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2218691Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2318691Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2418691Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2518691Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2650476Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2767665Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2818691Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29206622Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3018691Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3118691Sjoerg * SUCH DAMAGE.
3268678Ssheldonh *
3368678Ssheldonh *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
3468678Ssheldonh * $FreeBSD: head/sys/netinet/tcp_usrreq.c 54526 1999-12-13 00:39:20Z shin $
3568678Ssheldonh */
3668678Ssheldonh
3768678Ssheldonh#include "opt_tcpdebug.h"
3868678Ssheldonh
3968678Ssheldonh#include <sys/param.h>
4068678Ssheldonh#include <sys/systm.h>
4168678Ssheldonh#include <sys/kernel.h>
4268678Ssheldonh#include <sys/sysctl.h>
4318691Sjoerg#include <sys/mbuf.h>
4418691Sjoerg#include <sys/socket.h>
4518691Sjoerg#include <sys/socketvar.h>
4684306Sru#include <sys/protosw.h>
4784306Sru
4832574Sbde#include <net/if.h>
4918691Sjoerg#include <net/route.h>
5032574Sbde
5118691Sjoerg#include <netinet/in.h>
5232574Sbde#include <netinet/in_systm.h>
5318691Sjoerg#include <netinet/in_pcb.h>
5432574Sbde#include <netinet/in_var.h>
5518691Sjoerg#include <netinet/ip_var.h>
5632574Sbde#include <netinet/tcp.h>
5718691Sjoerg#include <netinet/tcp_fsm.h>
5832574Sbde#include <netinet/tcp_seq.h>
5918691Sjoerg#include <netinet/tcp_timer.h>
6032574Sbde#include <netinet/tcp_var.h>
6118691Sjoerg#include <netinet/tcpip.h>
6232574Sbde#ifdef TCPDEBUG
6318691Sjoerg#include <netinet/tcp_debug.h>
6432574Sbde#endif
6518691Sjoerg
6618691Sjoerg/*
6718691Sjoerg * TCP protocol interface to socket abstraction.
6818691Sjoerg */
6932574Sbdeextern	char *tcpstates[];	/* XXX ??? */
7018691Sjoerg
7171895Srustatic int	tcp_attach __P((struct socket *, struct proc *));
7271895Srustatic int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
7371895Sru				 struct proc *));
7471895Srustatic struct tcpcb *
7567665Sjhb		tcp_disconnect __P((struct tcpcb *));
7671895Srustatic struct tcpcb *
77183425Simp		tcp_usrclosed __P((struct tcpcb *));
78183425Simp
79183425Simp#ifdef TCPDEBUG
80183425Simp#define	TCPDEBUG0	int ostate
81183425Simp#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
82183425Simp#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
83233648Seadler				tcp_trace(TA_USER, ostate, tp, 0, req)
84233648Seadler#else
85183425Simp#define	TCPDEBUG0
8671895Sru#define	TCPDEBUG1()
8767665Sjhb#define	TCPDEBUG2(req)
8818691Sjoerg#endif
8918691Sjoerg
9018691Sjoerg/*
9118691Sjoerg * TCP attaches to socket via pru_attach(), reserving space,
9218691Sjoerg * and an internet control block.
9318691Sjoerg */
94121385Shmpstatic int
95121385Shmptcp_usr_attach(struct socket *so, int proto, struct proc *p)
9618691Sjoerg{
9718691Sjoerg	int s = splnet();
9818691Sjoerg	int error;
9918691Sjoerg	struct inpcb *inp = sotoinpcb(so);
10018691Sjoerg	struct tcpcb *tp = 0;
10118691Sjoerg	TCPDEBUG0;
10218691Sjoerg
10318691Sjoerg	TCPDEBUG1();
10418691Sjoerg	if (inp) {
10540589Sbde		error = EISCONN;
10618691Sjoerg		goto out;
10718691Sjoerg	}
10818691Sjoerg
10918691Sjoerg	error = tcp_attach(so, p);
110121385Shmp	if (error)
111121385Shmp		goto out;
11218691Sjoerg
11318691Sjoerg	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
11418691Sjoerg		so->so_linger = TCP_LINGERTIME;
115235376Sgjb	tp = sototcpcb(so);
11618691Sjoergout:
11718691Sjoerg	TCPDEBUG2(PRU_ATTACH);
11818691Sjoerg	splx(s);
119121385Shmp	return error;
120121385Shmp}
12118691Sjoerg
12218691Sjoerg/*
12318691Sjoerg * pru_detach() detaches the TCP protocol from the socket.
12418691Sjoerg * If the protocol state is non-embryonic, then can't
12518691Sjoerg * do this directly: have to initiate a pru_disconnect(),
12618691Sjoerg * which may finish later; embryonic TCB's can just
12718691Sjoerg * be discarded here.
12818691Sjoerg */
12918691Sjoergstatic int
13018691Sjoergtcp_usr_detach(struct socket *so)
13118691Sjoerg{
13218691Sjoerg	int s = splnet();
13318691Sjoerg	int error = 0;
13470466Sru	struct inpcb *inp = sotoinpcb(so);
13518691Sjoerg	struct tcpcb *tp;
13699231Simp	TCPDEBUG0;
13799231Simp
13818691Sjoerg	if (inp == 0) {
13918691Sjoerg		splx(s);
14018691Sjoerg		return EINVAL;	/* XXX */
14118691Sjoerg	}
14218691Sjoerg	tp = intotcpcb(inp);
14318691Sjoerg	TCPDEBUG1();
14418691Sjoerg	tp = tcp_disconnect(tp);
14518691Sjoerg
14618691Sjoerg	TCPDEBUG2(PRU_DETACH);
14718691Sjoerg	splx(s);
14818691Sjoerg	return error;
14918691Sjoerg}
15018691Sjoerg
15118691Sjoerg#define	COMMON_START()	TCPDEBUG0; \
15218691Sjoerg			do { \
15318691Sjoerg				     if (inp == 0) { \
15418691Sjoerg					     splx(s); \
15518691Sjoerg					     return EINVAL; \
15618691Sjoerg				     } \
15718691Sjoerg				     tp = intotcpcb(inp); \
15818691Sjoerg				     TCPDEBUG1(); \
15918691Sjoerg		     } while(0)
16018691Sjoerg
16118691Sjoerg#define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
16218691Sjoerg
16318691Sjoerg
16418691Sjoerg/*
16518691Sjoerg * Give the socket an address.
16618691Sjoerg */
16718691Sjoergstatic int
16818691Sjoergtcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
16918691Sjoerg{
17018691Sjoerg	int s = splnet();
17118691Sjoerg	int error = 0;
17218691Sjoerg	struct inpcb *inp = sotoinpcb(so);
17318691Sjoerg	struct tcpcb *tp;
17418691Sjoerg	struct sockaddr_in *sinp;
17518691Sjoerg
17618691Sjoerg	COMMON_START();
17718691Sjoerg
17818691Sjoerg	/*
17918691Sjoerg	 * Must check for multicast addresses and disallow binding
18018691Sjoerg	 * to them.
18118691Sjoerg	 */
18218691Sjoerg	sinp = (struct sockaddr_in *)nam;
18318691Sjoerg	if (sinp->sin_family == AF_INET &&
18418691Sjoerg	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
18518691Sjoerg		error = EAFNOSUPPORT;
18618691Sjoerg		goto out;
18718691Sjoerg	}
18818691Sjoerg	error = in_pcbbind(inp, nam, p);
18918691Sjoerg	if (error)
19018691Sjoerg		goto out;
19118691Sjoerg	COMMON_END(PRU_BIND);
19218691Sjoerg
19318691Sjoerg}
19418691Sjoerg
19518691Sjoerg/*
19668678Ssheldonh * Prepare to accept connections.
19718691Sjoerg */
19818691Sjoergstatic int
19918691Sjoergtcp_usr_listen(struct socket *so, struct proc *p)
200121385Shmp{
201121385Shmp	int s = splnet();
202131530Sru	int error = 0;
20318691Sjoerg	struct inpcb *inp = sotoinpcb(so);
20418691Sjoerg	struct tcpcb *tp;
20518691Sjoerg
20692566Sru	COMMON_START();
20792566Sru	if (inp->inp_lport == 0)
20818691Sjoerg		error = in_pcbbind(inp, (struct sockaddr *)0, p);
209121385Shmp	if (error == 0)
210121385Shmp		tp->t_state = TCPS_LISTEN;
21168716Sru	COMMON_END(PRU_LISTEN);
21224091Smpp}
21318691Sjoerg
21418691Sjoerg/*
21518691Sjoerg * Initiate connection to peer.
21618691Sjoerg * Create a template for use in transmissions on this connection.
21718691Sjoerg * Enter SYN_SENT state, and mark socket as connecting.
21818691Sjoerg * Start keep-alive timer, and seed output sequence space.
219121385Shmp * Send initial segment on connection.
220121385Shmp */
22118691Sjoergstatic int
222183425Simptcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
223235693Sgjb{
224235693Sgjb	int s = splnet();
225183425Simp	int error = 0;
22618691Sjoerg	struct inpcb *inp = sotoinpcb(so);
227147647Shmp	struct tcpcb *tp;
22868575Sru	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, NULL);
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	inp->inp_vflag |= INP_IPV4;
735	tp = tcp_newtcpcb(inp);
736	if (tp == 0) {
737		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
738
739		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
740		in_pcbdetach(inp);
741		so->so_state |= nofd;
742		return (ENOBUFS);
743	}
744	tp->t_state = TCPS_CLOSED;
745	return (0);
746}
747
748/*
749 * Initiate (or continue) disconnect.
750 * If embryonic state, just send reset (once).
751 * If in ``let data drain'' option and linger null, just drop.
752 * Otherwise (hard), mark socket disconnecting and drop
753 * current input data; switch states based on user close, and
754 * send segment to peer (with FIN).
755 */
756static struct tcpcb *
757tcp_disconnect(tp)
758	register struct tcpcb *tp;
759{
760	struct socket *so = tp->t_inpcb->inp_socket;
761
762	if (tp->t_state < TCPS_ESTABLISHED)
763		tp = tcp_close(tp);
764	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
765		tp = tcp_drop(tp, 0);
766	else {
767		soisdisconnecting(so);
768		sbflush(&so->so_rcv);
769		tp = tcp_usrclosed(tp);
770		if (tp)
771			(void) tcp_output(tp);
772	}
773	return (tp);
774}
775
776/*
777 * User issued close, and wish to trail through shutdown states:
778 * if never received SYN, just forget it.  If got a SYN from peer,
779 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
780 * If already got a FIN from peer, then almost done; go to LAST_ACK
781 * state.  In all other cases, have already sent FIN to peer (e.g.
782 * after PRU_SHUTDOWN), and just have to play tedious game waiting
783 * for peer to send FIN or not respond to keep-alives, etc.
784 * We can let the user exit from the close as soon as the FIN is acked.
785 */
786static struct tcpcb *
787tcp_usrclosed(tp)
788	register struct tcpcb *tp;
789{
790
791	switch (tp->t_state) {
792
793	case TCPS_CLOSED:
794	case TCPS_LISTEN:
795		tp->t_state = TCPS_CLOSED;
796		tp = tcp_close(tp);
797		break;
798
799	case TCPS_SYN_SENT:
800	case TCPS_SYN_RECEIVED:
801		tp->t_flags |= TF_NEEDFIN;
802		break;
803
804	case TCPS_ESTABLISHED:
805		tp->t_state = TCPS_FIN_WAIT_1;
806		break;
807
808	case TCPS_CLOSE_WAIT:
809		tp->t_state = TCPS_LAST_ACK;
810		break;
811	}
812	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
813		soisdisconnected(tp->t_inpcb->inp_socket);
814		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
815		if (tp->t_state == TCPS_FIN_WAIT_2)
816			callout_reset(tp->tt_2msl, tcp_maxidle,
817				      tcp_timer_2msl, tp);
818	}
819	return (tp);
820}
821
822