tcp_usrreq.c revision 74018
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34 * $FreeBSD: head/sys/netinet/tcp_usrreq.c 74018 2001-03-09 08:16:40Z jlemon $
35 */
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39#include "opt_tcpdebug.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/mbuf.h>
46#ifdef INET6
47#include <sys/domain.h>
48#endif /* INET6 */
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/protosw.h>
52#include <sys/proc.h>
53#include <sys/jail.h>
54
55#include <net/if.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#ifdef INET6
61#include <netinet/ip6.h>
62#endif
63#include <netinet/in_pcb.h>
64#ifdef INET6
65#include <netinet6/in6_pcb.h>
66#endif
67#include <netinet/in_var.h>
68#include <netinet/ip_var.h>
69#ifdef INET6
70#include <netinet6/ip6_var.h>
71#endif
72#include <netinet/tcp.h>
73#include <netinet/tcp_fsm.h>
74#include <netinet/tcp_seq.h>
75#include <netinet/tcp_timer.h>
76#include <netinet/tcp_var.h>
77#include <netinet/tcpip.h>
78#ifdef TCPDEBUG
79#include <netinet/tcp_debug.h>
80#endif
81
82#ifdef IPSEC
83#include <netinet6/ipsec.h>
84#endif /*IPSEC*/
85
86/*
87 * TCP protocol interface to socket abstraction.
88 */
89extern	char *tcpstates[];	/* XXX ??? */
90
91static int	tcp_attach __P((struct socket *, struct proc *));
92static int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
93				 struct proc *));
94#ifdef INET6
95static int	tcp6_connect __P((struct tcpcb *, struct sockaddr *,
96				 struct proc *));
97#endif /* INET6 */
98static struct tcpcb *
99		tcp_disconnect __P((struct tcpcb *));
100static struct tcpcb *
101		tcp_usrclosed __P((struct tcpcb *));
102
103#ifdef TCPDEBUG
104#define	TCPDEBUG0	int ostate
105#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
106#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
107				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
108#else
109#define	TCPDEBUG0
110#define	TCPDEBUG1()
111#define	TCPDEBUG2(req)
112#endif
113
114/*
115 * TCP attaches to socket via pru_attach(), reserving space,
116 * and an internet control block.
117 */
118static int
119tcp_usr_attach(struct socket *so, int proto, struct proc *p)
120{
121	int s = splnet();
122	int error;
123	struct inpcb *inp = sotoinpcb(so);
124	struct tcpcb *tp = 0;
125	TCPDEBUG0;
126
127	TCPDEBUG1();
128	if (inp) {
129		error = EISCONN;
130		goto out;
131	}
132
133	error = tcp_attach(so, p);
134	if (error)
135		goto out;
136
137	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
138		so->so_linger = TCP_LINGERTIME;
139	tp = sototcpcb(so);
140out:
141	TCPDEBUG2(PRU_ATTACH);
142	splx(s);
143	return error;
144}
145
146/*
147 * pru_detach() detaches the TCP protocol from the socket.
148 * If the protocol state is non-embryonic, then can't
149 * do this directly: have to initiate a pru_disconnect(),
150 * which may finish later; embryonic TCB's can just
151 * be discarded here.
152 */
153static int
154tcp_usr_detach(struct socket *so)
155{
156	int s = splnet();
157	int error = 0;
158	struct inpcb *inp = sotoinpcb(so);
159	struct tcpcb *tp;
160	TCPDEBUG0;
161
162	if (inp == 0) {
163		splx(s);
164		return EINVAL;	/* XXX */
165	}
166	tp = intotcpcb(inp);
167	TCPDEBUG1();
168	tp = tcp_disconnect(tp);
169
170	TCPDEBUG2(PRU_DETACH);
171	splx(s);
172	return error;
173}
174
175#define	COMMON_START()	TCPDEBUG0; \
176			do { \
177				     if (inp == 0) { \
178					     splx(s); \
179					     return EINVAL; \
180				     } \
181				     tp = intotcpcb(inp); \
182				     TCPDEBUG1(); \
183		     } while(0)
184
185#define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
186
187
188/*
189 * Give the socket an address.
190 */
191static int
192tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
193{
194	int s = splnet();
195	int error = 0;
196	struct inpcb *inp = sotoinpcb(so);
197	struct tcpcb *tp;
198	struct sockaddr_in *sinp;
199
200	COMMON_START();
201
202	/*
203	 * Must check for multicast addresses and disallow binding
204	 * to them.
205	 */
206	sinp = (struct sockaddr_in *)nam;
207	if (sinp->sin_family == AF_INET &&
208	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
209		error = EAFNOSUPPORT;
210		goto out;
211	}
212	error = in_pcbbind(inp, nam, p);
213	if (error)
214		goto out;
215	COMMON_END(PRU_BIND);
216
217}
218
219#ifdef INET6
220static int
221tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
222{
223	int s = splnet();
224	int error = 0;
225	struct inpcb *inp = sotoinpcb(so);
226	struct tcpcb *tp;
227	struct sockaddr_in6 *sin6p;
228
229	COMMON_START();
230
231	/*
232	 * Must check for multicast addresses and disallow binding
233	 * to them.
234	 */
235	sin6p = (struct sockaddr_in6 *)nam;
236	if (sin6p->sin6_family == AF_INET6 &&
237	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
238		error = EAFNOSUPPORT;
239		goto out;
240	}
241	inp->inp_vflag &= ~INP_IPV4;
242	inp->inp_vflag |= INP_IPV6;
243	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
244
245		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
246			inp->inp_vflag |= INP_IPV4;
247		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
248			struct sockaddr_in sin;
249
250			in6_sin6_2_sin(&sin, sin6p);
251			inp->inp_vflag |= INP_IPV4;
252			inp->inp_vflag &= ~INP_IPV6;
253			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
254			goto out;
255		}
256	}
257	error = in6_pcbbind(inp, nam, p);
258	if (error)
259		goto out;
260	COMMON_END(PRU_BIND);
261}
262#endif /* INET6 */
263
264/*
265 * Prepare to accept connections.
266 */
267static int
268tcp_usr_listen(struct socket *so, struct proc *p)
269{
270	int s = splnet();
271	int error = 0;
272	struct inpcb *inp = sotoinpcb(so);
273	struct tcpcb *tp;
274
275	COMMON_START();
276	if (inp->inp_lport == 0)
277		error = in_pcbbind(inp, (struct sockaddr *)0, p);
278	if (error == 0)
279		tp->t_state = TCPS_LISTEN;
280	COMMON_END(PRU_LISTEN);
281}
282
283#ifdef INET6
284static int
285tcp6_usr_listen(struct socket *so, struct proc *p)
286{
287	int s = splnet();
288	int error = 0;
289	struct inpcb *inp = sotoinpcb(so);
290	struct tcpcb *tp;
291
292	COMMON_START();
293	if (inp->inp_lport == 0) {
294		inp->inp_vflag &= ~INP_IPV4;
295		if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0)
296			inp->inp_vflag |= INP_IPV4;
297		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
298	}
299	if (error == 0)
300		tp->t_state = TCPS_LISTEN;
301	COMMON_END(PRU_LISTEN);
302}
303#endif /* INET6 */
304
305/*
306 * Initiate connection to peer.
307 * Create a template for use in transmissions on this connection.
308 * Enter SYN_SENT state, and mark socket as connecting.
309 * Start keep-alive timer, and seed output sequence space.
310 * Send initial segment on connection.
311 */
312static int
313tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
314{
315	int s = splnet();
316	int error = 0;
317	struct inpcb *inp = sotoinpcb(so);
318	struct tcpcb *tp;
319	struct sockaddr_in *sinp;
320
321	COMMON_START();
322
323	/*
324	 * Must disallow TCP ``connections'' to multicast addresses.
325	 */
326	sinp = (struct sockaddr_in *)nam;
327	if (sinp->sin_family == AF_INET
328	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
329		error = EAFNOSUPPORT;
330		goto out;
331	}
332
333	if (p && jailed(p->p_ucred))
334		prison_remote_ip(p->p_ucred, 0, &sinp->sin_addr.s_addr);
335
336	if ((error = tcp_connect(tp, nam, p)) != 0)
337		goto out;
338	error = tcp_output(tp);
339	COMMON_END(PRU_CONNECT);
340}
341
342#ifdef INET6
343static int
344tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
345{
346	int s = splnet();
347	int error = 0;
348	struct inpcb *inp = sotoinpcb(so);
349	struct tcpcb *tp;
350	struct sockaddr_in6 *sin6p;
351
352	COMMON_START();
353
354	/*
355	 * Must disallow TCP ``connections'' to multicast addresses.
356	 */
357	sin6p = (struct sockaddr_in6 *)nam;
358	if (sin6p->sin6_family == AF_INET6
359	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
360		error = EAFNOSUPPORT;
361		goto out;
362	}
363
364	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0 &&
365	    IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
366		struct sockaddr_in sin;
367
368		in6_sin6_2_sin(&sin, sin6p);
369		inp->inp_vflag |= INP_IPV4;
370		inp->inp_vflag &= ~INP_IPV6;
371		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
372			goto out;
373		error = tcp_output(tp);
374		goto out;
375	}
376	inp->inp_vflag &= ~INP_IPV4;
377	inp->inp_vflag |= INP_IPV6;
378	if ((error = tcp6_connect(tp, nam, p)) != 0)
379		goto out;
380	error = tcp_output(tp);
381	COMMON_END(PRU_CONNECT);
382}
383#endif /* INET6 */
384
385/*
386 * Initiate disconnect from peer.
387 * If connection never passed embryonic stage, just drop;
388 * else if don't need to let data drain, then can just drop anyways,
389 * else have to begin TCP shutdown process: mark socket disconnecting,
390 * drain unread data, state switch to reflect user close, and
391 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
392 * when peer sends FIN and acks ours.
393 *
394 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
395 */
396static int
397tcp_usr_disconnect(struct socket *so)
398{
399	int s = splnet();
400	int error = 0;
401	struct inpcb *inp = sotoinpcb(so);
402	struct tcpcb *tp;
403
404	COMMON_START();
405	tp = tcp_disconnect(tp);
406	COMMON_END(PRU_DISCONNECT);
407}
408
409/*
410 * Accept a connection.  Essentially all the work is
411 * done at higher levels; just return the address
412 * of the peer, storing through addr.
413 */
414static int
415tcp_usr_accept(struct socket *so, struct sockaddr **nam)
416{
417	int s = splnet();
418	int error = 0;
419	struct inpcb *inp = sotoinpcb(so);
420	struct tcpcb *tp;
421
422	if (so->so_state & SS_ISDISCONNECTED) {
423		error = ECONNABORTED;
424		goto out;
425	}
426	COMMON_START();
427	in_setpeeraddr(so, nam);
428	COMMON_END(PRU_ACCEPT);
429}
430
431#ifdef INET6
432static int
433tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
434{
435	int s = splnet();
436	int error = 0;
437	struct inpcb *inp = sotoinpcb(so);
438	struct tcpcb *tp;
439
440	if (so->so_state & SS_ISDISCONNECTED) {
441		error = ECONNABORTED;
442		goto out;
443	}
444	COMMON_START();
445	in6_mapped_peeraddr(so, nam);
446	COMMON_END(PRU_ACCEPT);
447}
448#endif /* INET6 */
449/*
450 * Mark the connection as being incapable of further output.
451 */
452static int
453tcp_usr_shutdown(struct socket *so)
454{
455	int s = splnet();
456	int error = 0;
457	struct inpcb *inp = sotoinpcb(so);
458	struct tcpcb *tp;
459
460	COMMON_START();
461	socantsendmore(so);
462	tp = tcp_usrclosed(tp);
463	if (tp)
464		error = tcp_output(tp);
465	COMMON_END(PRU_SHUTDOWN);
466}
467
468/*
469 * After a receive, possibly send window update to peer.
470 */
471static int
472tcp_usr_rcvd(struct socket *so, int flags)
473{
474	int s = splnet();
475	int error = 0;
476	struct inpcb *inp = sotoinpcb(so);
477	struct tcpcb *tp;
478
479	COMMON_START();
480	tcp_output(tp);
481	COMMON_END(PRU_RCVD);
482}
483
484/*
485 * Do a send by putting data in output queue and updating urgent
486 * marker if URG set.  Possibly send more data.  Unlike the other
487 * pru_*() routines, the mbuf chains are our responsibility.  We
488 * must either enqueue them or free them.  The other pru_* routines
489 * generally are caller-frees.
490 */
491static int
492tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
493	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
494{
495	int s = splnet();
496	int error = 0;
497	struct inpcb *inp = sotoinpcb(so);
498	struct tcpcb *tp;
499#ifdef INET6
500	int isipv6;
501#endif
502	TCPDEBUG0;
503
504	if (inp == NULL) {
505		/*
506		 * OOPS! we lost a race, the TCP session got reset after
507		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
508		 * network interrupt in the non-splnet() section of sosend().
509		 */
510		if (m)
511			m_freem(m);
512		if (control)
513			m_freem(control);
514		error = ECONNRESET;	/* XXX EPIPE? */
515		tp = NULL;
516		TCPDEBUG1();
517		goto out;
518	}
519#ifdef INET6
520	isipv6 = nam && nam->sa_family == AF_INET6;
521#endif /* INET6 */
522	tp = intotcpcb(inp);
523	TCPDEBUG1();
524	if (control) {
525		/* TCP doesn't do control messages (rights, creds, etc) */
526		if (control->m_len) {
527			m_freem(control);
528			if (m)
529				m_freem(m);
530			error = EINVAL;
531			goto out;
532		}
533		m_freem(control);	/* empty control, just free it */
534	}
535	if(!(flags & PRUS_OOB)) {
536		sbappend(&so->so_snd, m);
537		if (nam && tp->t_state < TCPS_SYN_SENT) {
538			/*
539			 * Do implied connect if not yet connected,
540			 * initialize window to default value, and
541			 * initialize maxseg/maxopd using peer's cached
542			 * MSS.
543			 */
544#ifdef INET6
545			if (isipv6)
546				error = tcp6_connect(tp, nam, p);
547			else
548#endif /* INET6 */
549			error = tcp_connect(tp, nam, p);
550			if (error)
551				goto out;
552			tp->snd_wnd = TTCP_CLIENT_SND_WND;
553			tcp_mss(tp, -1);
554		}
555
556		if (flags & PRUS_EOF) {
557			/*
558			 * Close the send side of the connection after
559			 * the data is sent.
560			 */
561			socantsendmore(so);
562			tp = tcp_usrclosed(tp);
563		}
564		if (tp != NULL) {
565			if (flags & PRUS_MORETOCOME)
566				tp->t_flags |= TF_MORETOCOME;
567			error = tcp_output(tp);
568			if (flags & PRUS_MORETOCOME)
569				tp->t_flags &= ~TF_MORETOCOME;
570		}
571	} else {
572		if (sbspace(&so->so_snd) < -512) {
573			m_freem(m);
574			error = ENOBUFS;
575			goto out;
576		}
577		/*
578		 * According to RFC961 (Assigned Protocols),
579		 * the urgent pointer points to the last octet
580		 * of urgent data.  We continue, however,
581		 * to consider it to indicate the first octet
582		 * of data past the urgent section.
583		 * Otherwise, snd_up should be one lower.
584		 */
585		sbappend(&so->so_snd, m);
586		if (nam && tp->t_state < TCPS_SYN_SENT) {
587			/*
588			 * Do implied connect if not yet connected,
589			 * initialize window to default value, and
590			 * initialize maxseg/maxopd using peer's cached
591			 * MSS.
592			 */
593#ifdef INET6
594			if (isipv6)
595				error = tcp6_connect(tp, nam, p);
596			else
597#endif /* INET6 */
598			error = tcp_connect(tp, nam, p);
599			if (error)
600				goto out;
601			tp->snd_wnd = TTCP_CLIENT_SND_WND;
602			tcp_mss(tp, -1);
603		}
604		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
605		tp->t_force = 1;
606		error = tcp_output(tp);
607		tp->t_force = 0;
608	}
609	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
610		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
611}
612
613/*
614 * Abort the TCP.
615 */
616static int
617tcp_usr_abort(struct socket *so)
618{
619	int s = splnet();
620	int error = 0;
621	struct inpcb *inp = sotoinpcb(so);
622	struct tcpcb *tp;
623
624	COMMON_START();
625	tp = tcp_drop(tp, ECONNABORTED);
626	COMMON_END(PRU_ABORT);
627}
628
629/*
630 * Receive out-of-band data.
631 */
632static int
633tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
634{
635	int s = splnet();
636	int error = 0;
637	struct inpcb *inp = sotoinpcb(so);
638	struct tcpcb *tp;
639
640	COMMON_START();
641	if ((so->so_oobmark == 0 &&
642	     (so->so_state & SS_RCVATMARK) == 0) ||
643	    so->so_options & SO_OOBINLINE ||
644	    tp->t_oobflags & TCPOOB_HADDATA) {
645		error = EINVAL;
646		goto out;
647	}
648	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
649		error = EWOULDBLOCK;
650		goto out;
651	}
652	m->m_len = 1;
653	*mtod(m, caddr_t) = tp->t_iobc;
654	if ((flags & MSG_PEEK) == 0)
655		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
656	COMMON_END(PRU_RCVOOB);
657}
658
659/* xxx - should be const */
660struct pr_usrreqs tcp_usrreqs = {
661	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
662	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
663	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
664	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
665	in_setsockaddr, sosend, soreceive, sopoll
666};
667
668#ifdef INET6
669struct pr_usrreqs tcp6_usrreqs = {
670	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
671	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
672	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
673	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
674	in6_mapped_sockaddr, sosend, soreceive, sopoll
675};
676#endif /* INET6 */
677
678/*
679 * Common subroutine to open a TCP connection to remote host specified
680 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
681 * port number if needed.  Call in_pcbladdr to do the routing and to choose
682 * a local host address (interface).  If there is an existing incarnation
683 * of the same connection in TIME-WAIT state and if the remote host was
684 * sending CC options and if the connection duration was < MSL, then
685 * truncate the previous TIME-WAIT state and proceed.
686 * Initialize connection parameters and enter SYN-SENT state.
687 */
688static int
689tcp_connect(tp, nam, p)
690	register struct tcpcb *tp;
691	struct sockaddr *nam;
692	struct proc *p;
693{
694	struct inpcb *inp = tp->t_inpcb, *oinp;
695	struct socket *so = inp->inp_socket;
696	struct tcpcb *otp;
697	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
698	struct sockaddr_in *ifaddr;
699	struct rmxp_tao *taop;
700	struct rmxp_tao tao_noncached;
701	int error;
702
703	if (inp->inp_lport == 0) {
704		error = in_pcbbind(inp, (struct sockaddr *)0, p);
705		if (error)
706			return error;
707	}
708
709	/*
710	 * Cannot simply call in_pcbconnect, because there might be an
711	 * earlier incarnation of this same connection still in
712	 * TIME_WAIT state, creating an ADDRINUSE error.
713	 */
714	error = in_pcbladdr(inp, nam, &ifaddr);
715	if (error)
716		return error;
717	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
718	    sin->sin_addr, sin->sin_port,
719	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
720						: ifaddr->sin_addr,
721	    inp->inp_lport,  0, NULL);
722	if (oinp) {
723		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
724		otp->t_state == TCPS_TIME_WAIT &&
725		    (ticks - otp->t_starttime) < tcp_msl &&
726		    (otp->t_flags & TF_RCVD_CC))
727			otp = tcp_close(otp);
728		else
729			return EADDRINUSE;
730	}
731	if (inp->inp_laddr.s_addr == INADDR_ANY)
732		inp->inp_laddr = ifaddr->sin_addr;
733	inp->inp_faddr = sin->sin_addr;
734	inp->inp_fport = sin->sin_port;
735	in_pcbrehash(inp);
736
737	tp->t_template = tcp_template(tp);
738	if (tp->t_template == 0) {
739		in_pcbdisconnect(inp);
740		return ENOBUFS;
741	}
742
743	/* Compute window scaling to request.  */
744	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
745	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
746		tp->request_r_scale++;
747
748	soisconnecting(so);
749	tcpstat.tcps_connattempt++;
750	tp->t_state = TCPS_SYN_SENT;
751	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
752	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
753	tcp_sendseqinit(tp);
754
755	/*
756	 * Generate a CC value for this connection and
757	 * check whether CC or CCnew should be used.
758	 */
759	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
760		taop = &tao_noncached;
761		bzero(taop, sizeof(*taop));
762	}
763
764	tp->cc_send = CC_INC(tcp_ccgen);
765	if (taop->tao_ccsent != 0 &&
766	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
767		taop->tao_ccsent = tp->cc_send;
768	} else {
769		taop->tao_ccsent = 0;
770		tp->t_flags |= TF_SENDCCNEW;
771	}
772
773	return 0;
774}
775
776#ifdef INET6
777static int
778tcp6_connect(tp, nam, p)
779	register struct tcpcb *tp;
780	struct sockaddr *nam;
781	struct proc *p;
782{
783	struct inpcb *inp = tp->t_inpcb, *oinp;
784	struct socket *so = inp->inp_socket;
785	struct tcpcb *otp;
786	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
787	struct in6_addr *addr6;
788	struct rmxp_tao *taop;
789	struct rmxp_tao tao_noncached;
790	int error;
791
792	if (inp->inp_lport == 0) {
793		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
794		if (error)
795			return error;
796	}
797
798	/*
799	 * Cannot simply call in_pcbconnect, because there might be an
800	 * earlier incarnation of this same connection still in
801	 * TIME_WAIT state, creating an ADDRINUSE error.
802	 */
803	error = in6_pcbladdr(inp, nam, &addr6);
804	if (error)
805		return error;
806	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
807				  &sin6->sin6_addr, sin6->sin6_port,
808				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
809				  ? addr6
810				  : &inp->in6p_laddr,
811				  inp->inp_lport,  0, NULL);
812	if (oinp) {
813		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
814		    otp->t_state == TCPS_TIME_WAIT &&
815		    (ticks - otp->t_starttime) < tcp_msl &&
816		    (otp->t_flags & TF_RCVD_CC))
817			otp = tcp_close(otp);
818		else
819			return EADDRINUSE;
820	}
821	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
822		inp->in6p_laddr = *addr6;
823	inp->in6p_faddr = sin6->sin6_addr;
824	inp->inp_fport = sin6->sin6_port;
825	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
826		inp->in6p_flowinfo = sin6->sin6_flowinfo;
827	in_pcbrehash(inp);
828
829	tp->t_template = tcp_template(tp);
830	if (tp->t_template == 0) {
831		in6_pcbdisconnect(inp);
832		return ENOBUFS;
833	}
834
835	/* Compute window scaling to request.  */
836	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
837	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
838		tp->request_r_scale++;
839
840	soisconnecting(so);
841	tcpstat.tcps_connattempt++;
842	tp->t_state = TCPS_SYN_SENT;
843	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
844	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
845	tcp_sendseqinit(tp);
846
847	/*
848	 * Generate a CC value for this connection and
849	 * check whether CC or CCnew should be used.
850	 */
851	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
852		taop = &tao_noncached;
853		bzero(taop, sizeof(*taop));
854	}
855
856	tp->cc_send = CC_INC(tcp_ccgen);
857	if (taop->tao_ccsent != 0 &&
858	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
859		taop->tao_ccsent = tp->cc_send;
860	} else {
861		taop->tao_ccsent = 0;
862		tp->t_flags |= TF_SENDCCNEW;
863	}
864
865	return 0;
866}
867#endif /* INET6 */
868
869/*
870 * The new sockopt interface makes it possible for us to block in the
871 * copyin/out step (if we take a page fault).  Taking a page fault at
872 * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
873 * use TSM, there probably isn't any need for this function to run at
874 * splnet() any more.  This needs more examination.)
875 */
876int
877tcp_ctloutput(so, sopt)
878	struct socket *so;
879	struct sockopt *sopt;
880{
881	int	error, opt, optval, s;
882	struct	inpcb *inp;
883	struct	tcpcb *tp;
884
885	error = 0;
886	s = splnet();		/* XXX */
887	inp = sotoinpcb(so);
888	if (inp == NULL) {
889		splx(s);
890		return (ECONNRESET);
891	}
892	if (sopt->sopt_level != IPPROTO_TCP) {
893#ifdef INET6
894		if (INP_CHECK_SOCKAF(so, AF_INET6))
895			error = ip6_ctloutput(so, sopt);
896		else
897#endif /* INET6 */
898		error = ip_ctloutput(so, sopt);
899		splx(s);
900		return (error);
901	}
902	tp = intotcpcb(inp);
903
904	switch (sopt->sopt_dir) {
905	case SOPT_SET:
906		switch (sopt->sopt_name) {
907		case TCP_NODELAY:
908		case TCP_NOOPT:
909			error = sooptcopyin(sopt, &optval, sizeof optval,
910					    sizeof optval);
911			if (error)
912				break;
913
914			switch (sopt->sopt_name) {
915			case TCP_NODELAY:
916				opt = TF_NODELAY;
917				break;
918			case TCP_NOOPT:
919				opt = TF_NOOPT;
920				break;
921			default:
922				opt = 0; /* dead code to fool gcc */
923				break;
924			}
925
926			if (optval)
927				tp->t_flags |= opt;
928			else
929				tp->t_flags &= ~opt;
930			break;
931
932		case TCP_NOPUSH:
933			error = sooptcopyin(sopt, &optval, sizeof optval,
934					    sizeof optval);
935			if (error)
936				break;
937
938			if (optval)
939				tp->t_flags |= TF_NOPUSH;
940			else {
941				tp->t_flags &= ~TF_NOPUSH;
942				error = tcp_output(tp);
943			}
944			break;
945
946		case TCP_MAXSEG:
947			error = sooptcopyin(sopt, &optval, sizeof optval,
948					    sizeof optval);
949			if (error)
950				break;
951
952			if (optval > 0 && optval <= tp->t_maxseg)
953				tp->t_maxseg = optval;
954			else
955				error = EINVAL;
956			break;
957
958		default:
959			error = ENOPROTOOPT;
960			break;
961		}
962		break;
963
964	case SOPT_GET:
965		switch (sopt->sopt_name) {
966		case TCP_NODELAY:
967			optval = tp->t_flags & TF_NODELAY;
968			break;
969		case TCP_MAXSEG:
970			optval = tp->t_maxseg;
971			break;
972		case TCP_NOOPT:
973			optval = tp->t_flags & TF_NOOPT;
974			break;
975		case TCP_NOPUSH:
976			optval = tp->t_flags & TF_NOPUSH;
977			break;
978		default:
979			error = ENOPROTOOPT;
980			break;
981		}
982		if (error == 0)
983			error = sooptcopyout(sopt, &optval, sizeof optval);
984		break;
985	}
986	splx(s);
987	return (error);
988}
989
990/*
991 * tcp_sendspace and tcp_recvspace are the default send and receive window
992 * sizes, respectively.  These are obsolescent (this information should
993 * be set by the route).
994 */
995u_long	tcp_sendspace = 1024*16;
996SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
997    &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
998u_long	tcp_recvspace = 1024*16;
999SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1000    &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1001
1002/*
1003 * Attach TCP protocol to socket, allocating
1004 * internet protocol control block, tcp control block,
1005 * bufer space, and entering LISTEN state if to accept connections.
1006 */
1007static int
1008tcp_attach(so, p)
1009	struct socket *so;
1010	struct proc *p;
1011{
1012	register struct tcpcb *tp;
1013	struct inpcb *inp;
1014	int error;
1015#ifdef INET6
1016	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1017#endif
1018
1019	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1020		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1021		if (error)
1022			return (error);
1023	}
1024	error = in_pcballoc(so, &tcbinfo, p);
1025	if (error)
1026		return (error);
1027	inp = sotoinpcb(so);
1028#ifdef IPSEC
1029	error = ipsec_init_policy(so, &inp->inp_sp);
1030	if (error) {
1031#ifdef INET6
1032		if (isipv6)
1033			in6_pcbdetach(inp);
1034		else
1035#endif
1036		in_pcbdetach(inp);
1037		return (error);
1038	}
1039#endif /*IPSEC*/
1040#ifdef INET6
1041	if (isipv6) {
1042		inp->inp_vflag |= INP_IPV6;
1043		inp->in6p_hops = -1;	/* use kernel default */
1044	}
1045	else
1046#endif
1047	inp->inp_vflag |= INP_IPV4;
1048	tp = tcp_newtcpcb(inp);
1049	if (tp == 0) {
1050		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1051
1052		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1053#ifdef INET6
1054		if (isipv6)
1055			in6_pcbdetach(inp);
1056		else
1057#endif
1058		in_pcbdetach(inp);
1059		so->so_state |= nofd;
1060		return (ENOBUFS);
1061	}
1062	tp->t_state = TCPS_CLOSED;
1063	return (0);
1064}
1065
1066/*
1067 * Initiate (or continue) disconnect.
1068 * If embryonic state, just send reset (once).
1069 * If in ``let data drain'' option and linger null, just drop.
1070 * Otherwise (hard), mark socket disconnecting and drop
1071 * current input data; switch states based on user close, and
1072 * send segment to peer (with FIN).
1073 */
1074static struct tcpcb *
1075tcp_disconnect(tp)
1076	register struct tcpcb *tp;
1077{
1078	struct socket *so = tp->t_inpcb->inp_socket;
1079
1080	if (tp->t_state < TCPS_ESTABLISHED)
1081		tp = tcp_close(tp);
1082	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1083		tp = tcp_drop(tp, 0);
1084	else {
1085		soisdisconnecting(so);
1086		sbflush(&so->so_rcv);
1087		tp = tcp_usrclosed(tp);
1088		if (tp)
1089			(void) tcp_output(tp);
1090	}
1091	return (tp);
1092}
1093
1094/*
1095 * User issued close, and wish to trail through shutdown states:
1096 * if never received SYN, just forget it.  If got a SYN from peer,
1097 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1098 * If already got a FIN from peer, then almost done; go to LAST_ACK
1099 * state.  In all other cases, have already sent FIN to peer (e.g.
1100 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1101 * for peer to send FIN or not respond to keep-alives, etc.
1102 * We can let the user exit from the close as soon as the FIN is acked.
1103 */
1104static struct tcpcb *
1105tcp_usrclosed(tp)
1106	register struct tcpcb *tp;
1107{
1108
1109	switch (tp->t_state) {
1110
1111	case TCPS_CLOSED:
1112	case TCPS_LISTEN:
1113		tp->t_state = TCPS_CLOSED;
1114		tp = tcp_close(tp);
1115		break;
1116
1117	case TCPS_SYN_SENT:
1118	case TCPS_SYN_RECEIVED:
1119		tp->t_flags |= TF_NEEDFIN;
1120		break;
1121
1122	case TCPS_ESTABLISHED:
1123		tp->t_state = TCPS_FIN_WAIT_1;
1124		break;
1125
1126	case TCPS_CLOSE_WAIT:
1127		tp->t_state = TCPS_LAST_ACK;
1128		break;
1129	}
1130	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1131		soisdisconnected(tp->t_inpcb->inp_socket);
1132		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1133		if (tp->t_state == TCPS_FIN_WAIT_2)
1134			callout_reset(tp->tt_2msl, tcp_maxidle,
1135				      tcp_timer_2msl, tp);
1136	}
1137	return (tp);
1138}
1139
1140