tcp_usrreq.c revision 75619
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 75619 2001-04-17 18:08:01Z kris $
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 = 0
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 = NULL;
421	TCPDEBUG0;
422
423	if (so->so_state & SS_ISDISCONNECTED) {
424		error = ECONNABORTED;
425		goto out;
426	}
427	if (inp == 0) {
428		splx(s);
429		return (EINVAL);
430	}
431	tp = intotcpcb(inp);
432	TCPDEBUG1();
433	in_setpeeraddr(so, nam);
434	COMMON_END(PRU_ACCEPT);
435}
436
437#ifdef INET6
438static int
439tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
440{
441	int s = splnet();
442	int error = 0;
443	struct inpcb *inp = sotoinpcb(so);
444	struct tcpcb *tp = NULL;
445	TCPDEBUG0;
446
447	if (so->so_state & SS_ISDISCONNECTED) {
448		error = ECONNABORTED;
449		goto out;
450	}
451	if (inp == 0) {
452		splx(s);
453		return (EINVAL);
454	}
455	tp = intotcpcb(inp);
456	TCPDEBUG1();
457	in6_mapped_peeraddr(so, nam);
458	COMMON_END(PRU_ACCEPT);
459}
460#endif /* INET6 */
461/*
462 * Mark the connection as being incapable of further output.
463 */
464static int
465tcp_usr_shutdown(struct socket *so)
466{
467	int s = splnet();
468	int error = 0;
469	struct inpcb *inp = sotoinpcb(so);
470	struct tcpcb *tp;
471
472	COMMON_START();
473	socantsendmore(so);
474	tp = tcp_usrclosed(tp);
475	if (tp)
476		error = tcp_output(tp);
477	COMMON_END(PRU_SHUTDOWN);
478}
479
480/*
481 * After a receive, possibly send window update to peer.
482 */
483static int
484tcp_usr_rcvd(struct socket *so, int flags)
485{
486	int s = splnet();
487	int error = 0;
488	struct inpcb *inp = sotoinpcb(so);
489	struct tcpcb *tp;
490
491	COMMON_START();
492	tcp_output(tp);
493	COMMON_END(PRU_RCVD);
494}
495
496/*
497 * Do a send by putting data in output queue and updating urgent
498 * marker if URG set.  Possibly send more data.  Unlike the other
499 * pru_*() routines, the mbuf chains are our responsibility.  We
500 * must either enqueue them or free them.  The other pru_* routines
501 * generally are caller-frees.
502 */
503static int
504tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
505	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
506{
507	int s = splnet();
508	int error = 0;
509	struct inpcb *inp = sotoinpcb(so);
510	struct tcpcb *tp;
511#ifdef INET6
512	int isipv6;
513#endif
514	TCPDEBUG0;
515
516	if (inp == NULL) {
517		/*
518		 * OOPS! we lost a race, the TCP session got reset after
519		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
520		 * network interrupt in the non-splnet() section of sosend().
521		 */
522		if (m)
523			m_freem(m);
524		if (control)
525			m_freem(control);
526		error = ECONNRESET;	/* XXX EPIPE? */
527		tp = NULL;
528		TCPDEBUG1();
529		goto out;
530	}
531#ifdef INET6
532	isipv6 = nam && nam->sa_family == AF_INET6;
533#endif /* INET6 */
534	tp = intotcpcb(inp);
535	TCPDEBUG1();
536	if (control) {
537		/* TCP doesn't do control messages (rights, creds, etc) */
538		if (control->m_len) {
539			m_freem(control);
540			if (m)
541				m_freem(m);
542			error = EINVAL;
543			goto out;
544		}
545		m_freem(control);	/* empty control, just free it */
546	}
547	if(!(flags & PRUS_OOB)) {
548		sbappend(&so->so_snd, m);
549		if (nam && tp->t_state < TCPS_SYN_SENT) {
550			/*
551			 * Do implied connect if not yet connected,
552			 * initialize window to default value, and
553			 * initialize maxseg/maxopd using peer's cached
554			 * MSS.
555			 */
556#ifdef INET6
557			if (isipv6)
558				error = tcp6_connect(tp, nam, p);
559			else
560#endif /* INET6 */
561			error = tcp_connect(tp, nam, p);
562			if (error)
563				goto out;
564			tp->snd_wnd = TTCP_CLIENT_SND_WND;
565			tcp_mss(tp, -1);
566		}
567
568		if (flags & PRUS_EOF) {
569			/*
570			 * Close the send side of the connection after
571			 * the data is sent.
572			 */
573			socantsendmore(so);
574			tp = tcp_usrclosed(tp);
575		}
576		if (tp != NULL) {
577			if (flags & PRUS_MORETOCOME)
578				tp->t_flags |= TF_MORETOCOME;
579			error = tcp_output(tp);
580			if (flags & PRUS_MORETOCOME)
581				tp->t_flags &= ~TF_MORETOCOME;
582		}
583	} else {
584		if (sbspace(&so->so_snd) < -512) {
585			m_freem(m);
586			error = ENOBUFS;
587			goto out;
588		}
589		/*
590		 * According to RFC961 (Assigned Protocols),
591		 * the urgent pointer points to the last octet
592		 * of urgent data.  We continue, however,
593		 * to consider it to indicate the first octet
594		 * of data past the urgent section.
595		 * Otherwise, snd_up should be one lower.
596		 */
597		sbappend(&so->so_snd, m);
598		if (nam && tp->t_state < TCPS_SYN_SENT) {
599			/*
600			 * Do implied connect if not yet connected,
601			 * initialize window to default value, and
602			 * initialize maxseg/maxopd using peer's cached
603			 * MSS.
604			 */
605#ifdef INET6
606			if (isipv6)
607				error = tcp6_connect(tp, nam, p);
608			else
609#endif /* INET6 */
610			error = tcp_connect(tp, nam, p);
611			if (error)
612				goto out;
613			tp->snd_wnd = TTCP_CLIENT_SND_WND;
614			tcp_mss(tp, -1);
615		}
616		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
617		tp->t_force = 1;
618		error = tcp_output(tp);
619		tp->t_force = 0;
620	}
621	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
622		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
623}
624
625/*
626 * Abort the TCP.
627 */
628static int
629tcp_usr_abort(struct socket *so)
630{
631	int s = splnet();
632	int error = 0;
633	struct inpcb *inp = sotoinpcb(so);
634	struct tcpcb *tp;
635
636	COMMON_START();
637	tp = tcp_drop(tp, ECONNABORTED);
638	COMMON_END(PRU_ABORT);
639}
640
641/*
642 * Receive out-of-band data.
643 */
644static int
645tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
646{
647	int s = splnet();
648	int error = 0;
649	struct inpcb *inp = sotoinpcb(so);
650	struct tcpcb *tp;
651
652	COMMON_START();
653	if ((so->so_oobmark == 0 &&
654	     (so->so_state & SS_RCVATMARK) == 0) ||
655	    so->so_options & SO_OOBINLINE ||
656	    tp->t_oobflags & TCPOOB_HADDATA) {
657		error = EINVAL;
658		goto out;
659	}
660	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
661		error = EWOULDBLOCK;
662		goto out;
663	}
664	m->m_len = 1;
665	*mtod(m, caddr_t) = tp->t_iobc;
666	if ((flags & MSG_PEEK) == 0)
667		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
668	COMMON_END(PRU_RCVOOB);
669}
670
671/* xxx - should be const */
672struct pr_usrreqs tcp_usrreqs = {
673	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
674	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
675	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
676	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
677	in_setsockaddr, sosend, soreceive, sopoll
678};
679
680#ifdef INET6
681struct pr_usrreqs tcp6_usrreqs = {
682	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
683	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
684	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
685	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
686	in6_mapped_sockaddr, sosend, soreceive, sopoll
687};
688#endif /* INET6 */
689
690/*
691 * Common subroutine to open a TCP connection to remote host specified
692 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
693 * port number if needed.  Call in_pcbladdr to do the routing and to choose
694 * a local host address (interface).  If there is an existing incarnation
695 * of the same connection in TIME-WAIT state and if the remote host was
696 * sending CC options and if the connection duration was < MSL, then
697 * truncate the previous TIME-WAIT state and proceed.
698 * Initialize connection parameters and enter SYN-SENT state.
699 */
700static int
701tcp_connect(tp, nam, p)
702	register struct tcpcb *tp;
703	struct sockaddr *nam;
704	struct proc *p;
705{
706	struct inpcb *inp = tp->t_inpcb, *oinp;
707	struct socket *so = inp->inp_socket;
708	struct tcpcb *otp;
709	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
710	struct sockaddr_in *ifaddr;
711	struct rmxp_tao *taop;
712	struct rmxp_tao tao_noncached;
713	int error;
714
715	if (inp->inp_lport == 0) {
716		error = in_pcbbind(inp, (struct sockaddr *)0, p);
717		if (error)
718			return error;
719	}
720
721	/*
722	 * Cannot simply call in_pcbconnect, because there might be an
723	 * earlier incarnation of this same connection still in
724	 * TIME_WAIT state, creating an ADDRINUSE error.
725	 */
726	error = in_pcbladdr(inp, nam, &ifaddr);
727	if (error)
728		return error;
729	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
730	    sin->sin_addr, sin->sin_port,
731	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
732						: ifaddr->sin_addr,
733	    inp->inp_lport,  0, NULL);
734	if (oinp) {
735		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
736		otp->t_state == TCPS_TIME_WAIT &&
737		    (ticks - otp->t_starttime) < tcp_msl &&
738		    (otp->t_flags & TF_RCVD_CC))
739			otp = tcp_close(otp);
740		else
741			return EADDRINUSE;
742	}
743	if (inp->inp_laddr.s_addr == INADDR_ANY)
744		inp->inp_laddr = ifaddr->sin_addr;
745	inp->inp_faddr = sin->sin_addr;
746	inp->inp_fport = sin->sin_port;
747	in_pcbrehash(inp);
748
749	tp->t_template = tcp_template(tp);
750	if (tp->t_template == 0) {
751		in_pcbdisconnect(inp);
752		return ENOBUFS;
753	}
754
755	/* Compute window scaling to request.  */
756	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
757	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
758		tp->request_r_scale++;
759
760	soisconnecting(so);
761	tcpstat.tcps_connattempt++;
762	tp->t_state = TCPS_SYN_SENT;
763	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
764#ifdef TCP_COMPAT_42
765	tp->iss = tcp_iss;
766	tcp_iss += TCP_ISSINCR/2;
767#else  /* TCP_COMPAT_42 */
768	tp->iss = tcp_rndiss_next();
769#endif /* !TCP_COMPAT_42 */
770	tcp_sendseqinit(tp);
771
772	/*
773	 * Generate a CC value for this connection and
774	 * check whether CC or CCnew should be used.
775	 */
776	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
777		taop = &tao_noncached;
778		bzero(taop, sizeof(*taop));
779	}
780
781	tp->cc_send = CC_INC(tcp_ccgen);
782	if (taop->tao_ccsent != 0 &&
783	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
784		taop->tao_ccsent = tp->cc_send;
785	} else {
786		taop->tao_ccsent = 0;
787		tp->t_flags |= TF_SENDCCNEW;
788	}
789
790	return 0;
791}
792
793#ifdef INET6
794static int
795tcp6_connect(tp, nam, p)
796	register struct tcpcb *tp;
797	struct sockaddr *nam;
798	struct proc *p;
799{
800	struct inpcb *inp = tp->t_inpcb, *oinp;
801	struct socket *so = inp->inp_socket;
802	struct tcpcb *otp;
803	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
804	struct in6_addr *addr6;
805	struct rmxp_tao *taop;
806	struct rmxp_tao tao_noncached;
807	int error;
808
809	if (inp->inp_lport == 0) {
810		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
811		if (error)
812			return error;
813	}
814
815	/*
816	 * Cannot simply call in_pcbconnect, because there might be an
817	 * earlier incarnation of this same connection still in
818	 * TIME_WAIT state, creating an ADDRINUSE error.
819	 */
820	error = in6_pcbladdr(inp, nam, &addr6);
821	if (error)
822		return error;
823	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
824				  &sin6->sin6_addr, sin6->sin6_port,
825				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
826				  ? addr6
827				  : &inp->in6p_laddr,
828				  inp->inp_lport,  0, NULL);
829	if (oinp) {
830		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
831		    otp->t_state == TCPS_TIME_WAIT &&
832		    (ticks - otp->t_starttime) < tcp_msl &&
833		    (otp->t_flags & TF_RCVD_CC))
834			otp = tcp_close(otp);
835		else
836			return EADDRINUSE;
837	}
838	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
839		inp->in6p_laddr = *addr6;
840	inp->in6p_faddr = sin6->sin6_addr;
841	inp->inp_fport = sin6->sin6_port;
842	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
843		inp->in6p_flowinfo = sin6->sin6_flowinfo;
844	in_pcbrehash(inp);
845
846	tp->t_template = tcp_template(tp);
847	if (tp->t_template == 0) {
848		in6_pcbdisconnect(inp);
849		return ENOBUFS;
850	}
851
852	/* Compute window scaling to request.  */
853	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
854	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
855		tp->request_r_scale++;
856
857	soisconnecting(so);
858	tcpstat.tcps_connattempt++;
859	tp->t_state = TCPS_SYN_SENT;
860	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
861#ifdef TCP_COMPAT_42
862	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
863#else
864	tp->iss = tcp_rndiss_next();
865#endif /* TCP_COMPAT_42 */
866	tcp_sendseqinit(tp);
867
868	/*
869	 * Generate a CC value for this connection and
870	 * check whether CC or CCnew should be used.
871	 */
872	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
873		taop = &tao_noncached;
874		bzero(taop, sizeof(*taop));
875	}
876
877	tp->cc_send = CC_INC(tcp_ccgen);
878	if (taop->tao_ccsent != 0 &&
879	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
880		taop->tao_ccsent = tp->cc_send;
881	} else {
882		taop->tao_ccsent = 0;
883		tp->t_flags |= TF_SENDCCNEW;
884	}
885
886	return 0;
887}
888#endif /* INET6 */
889
890/*
891 * The new sockopt interface makes it possible for us to block in the
892 * copyin/out step (if we take a page fault).  Taking a page fault at
893 * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
894 * use TSM, there probably isn't any need for this function to run at
895 * splnet() any more.  This needs more examination.)
896 */
897int
898tcp_ctloutput(so, sopt)
899	struct socket *so;
900	struct sockopt *sopt;
901{
902	int	error, opt, optval, s;
903	struct	inpcb *inp;
904	struct	tcpcb *tp;
905
906	error = 0;
907	s = splnet();		/* XXX */
908	inp = sotoinpcb(so);
909	if (inp == NULL) {
910		splx(s);
911		return (ECONNRESET);
912	}
913	if (sopt->sopt_level != IPPROTO_TCP) {
914#ifdef INET6
915		if (INP_CHECK_SOCKAF(so, AF_INET6))
916			error = ip6_ctloutput(so, sopt);
917		else
918#endif /* INET6 */
919		error = ip_ctloutput(so, sopt);
920		splx(s);
921		return (error);
922	}
923	tp = intotcpcb(inp);
924
925	switch (sopt->sopt_dir) {
926	case SOPT_SET:
927		switch (sopt->sopt_name) {
928		case TCP_NODELAY:
929		case TCP_NOOPT:
930			error = sooptcopyin(sopt, &optval, sizeof optval,
931					    sizeof optval);
932			if (error)
933				break;
934
935			switch (sopt->sopt_name) {
936			case TCP_NODELAY:
937				opt = TF_NODELAY;
938				break;
939			case TCP_NOOPT:
940				opt = TF_NOOPT;
941				break;
942			default:
943				opt = 0; /* dead code to fool gcc */
944				break;
945			}
946
947			if (optval)
948				tp->t_flags |= opt;
949			else
950				tp->t_flags &= ~opt;
951			break;
952
953		case TCP_NOPUSH:
954			error = sooptcopyin(sopt, &optval, sizeof optval,
955					    sizeof optval);
956			if (error)
957				break;
958
959			if (optval)
960				tp->t_flags |= TF_NOPUSH;
961			else {
962				tp->t_flags &= ~TF_NOPUSH;
963				error = tcp_output(tp);
964			}
965			break;
966
967		case TCP_MAXSEG:
968			error = sooptcopyin(sopt, &optval, sizeof optval,
969					    sizeof optval);
970			if (error)
971				break;
972
973			if (optval > 0 && optval <= tp->t_maxseg)
974				tp->t_maxseg = optval;
975			else
976				error = EINVAL;
977			break;
978
979		default:
980			error = ENOPROTOOPT;
981			break;
982		}
983		break;
984
985	case SOPT_GET:
986		switch (sopt->sopt_name) {
987		case TCP_NODELAY:
988			optval = tp->t_flags & TF_NODELAY;
989			break;
990		case TCP_MAXSEG:
991			optval = tp->t_maxseg;
992			break;
993		case TCP_NOOPT:
994			optval = tp->t_flags & TF_NOOPT;
995			break;
996		case TCP_NOPUSH:
997			optval = tp->t_flags & TF_NOPUSH;
998			break;
999		default:
1000			error = ENOPROTOOPT;
1001			break;
1002		}
1003		if (error == 0)
1004			error = sooptcopyout(sopt, &optval, sizeof optval);
1005		break;
1006	}
1007	splx(s);
1008	return (error);
1009}
1010
1011/*
1012 * tcp_sendspace and tcp_recvspace are the default send and receive window
1013 * sizes, respectively.  These are obsolescent (this information should
1014 * be set by the route).
1015 */
1016u_long	tcp_sendspace = 1024*16;
1017SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1018    &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1019u_long	tcp_recvspace = 1024*16;
1020SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1021    &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1022
1023/*
1024 * Attach TCP protocol to socket, allocating
1025 * internet protocol control block, tcp control block,
1026 * bufer space, and entering LISTEN state if to accept connections.
1027 */
1028static int
1029tcp_attach(so, p)
1030	struct socket *so;
1031	struct proc *p;
1032{
1033	register struct tcpcb *tp;
1034	struct inpcb *inp;
1035	int error;
1036#ifdef INET6
1037	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1038#endif
1039
1040	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1041		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1042		if (error)
1043			return (error);
1044	}
1045	error = in_pcballoc(so, &tcbinfo, p);
1046	if (error)
1047		return (error);
1048	inp = sotoinpcb(so);
1049#ifdef IPSEC
1050	error = ipsec_init_policy(so, &inp->inp_sp);
1051	if (error) {
1052#ifdef INET6
1053		if (isipv6)
1054			in6_pcbdetach(inp);
1055		else
1056#endif
1057		in_pcbdetach(inp);
1058		return (error);
1059	}
1060#endif /*IPSEC*/
1061#ifdef INET6
1062	if (isipv6) {
1063		inp->inp_vflag |= INP_IPV6;
1064		inp->in6p_hops = -1;	/* use kernel default */
1065	}
1066	else
1067#endif
1068	inp->inp_vflag |= INP_IPV4;
1069	tp = tcp_newtcpcb(inp);
1070	if (tp == 0) {
1071		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1072
1073		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1074#ifdef INET6
1075		if (isipv6)
1076			in6_pcbdetach(inp);
1077		else
1078#endif
1079		in_pcbdetach(inp);
1080		so->so_state |= nofd;
1081		return (ENOBUFS);
1082	}
1083	tp->t_state = TCPS_CLOSED;
1084	return (0);
1085}
1086
1087/*
1088 * Initiate (or continue) disconnect.
1089 * If embryonic state, just send reset (once).
1090 * If in ``let data drain'' option and linger null, just drop.
1091 * Otherwise (hard), mark socket disconnecting and drop
1092 * current input data; switch states based on user close, and
1093 * send segment to peer (with FIN).
1094 */
1095static struct tcpcb *
1096tcp_disconnect(tp)
1097	register struct tcpcb *tp;
1098{
1099	struct socket *so = tp->t_inpcb->inp_socket;
1100
1101	if (tp->t_state < TCPS_ESTABLISHED)
1102		tp = tcp_close(tp);
1103	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1104		tp = tcp_drop(tp, 0);
1105	else {
1106		soisdisconnecting(so);
1107		sbflush(&so->so_rcv);
1108		tp = tcp_usrclosed(tp);
1109		if (tp)
1110			(void) tcp_output(tp);
1111	}
1112	return (tp);
1113}
1114
1115/*
1116 * User issued close, and wish to trail through shutdown states:
1117 * if never received SYN, just forget it.  If got a SYN from peer,
1118 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1119 * If already got a FIN from peer, then almost done; go to LAST_ACK
1120 * state.  In all other cases, have already sent FIN to peer (e.g.
1121 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1122 * for peer to send FIN or not respond to keep-alives, etc.
1123 * We can let the user exit from the close as soon as the FIN is acked.
1124 */
1125static struct tcpcb *
1126tcp_usrclosed(tp)
1127	register struct tcpcb *tp;
1128{
1129
1130	switch (tp->t_state) {
1131
1132	case TCPS_CLOSED:
1133	case TCPS_LISTEN:
1134		tp->t_state = TCPS_CLOSED;
1135		tp = tcp_close(tp);
1136		break;
1137
1138	case TCPS_SYN_SENT:
1139	case TCPS_SYN_RECEIVED:
1140		tp->t_flags |= TF_NEEDFIN;
1141		break;
1142
1143	case TCPS_ESTABLISHED:
1144		tp->t_state = TCPS_FIN_WAIT_1;
1145		break;
1146
1147	case TCPS_CLOSE_WAIT:
1148		tp->t_state = TCPS_LAST_ACK;
1149		break;
1150	}
1151	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1152		soisdisconnected(tp->t_inpcb->inp_socket);
1153		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1154		if (tp->t_state == TCPS_FIN_WAIT_2)
1155			callout_reset(tp->tt_2msl, tcp_maxidle,
1156				      tcp_timer_2msl, tp);
1157	}
1158	return (tp);
1159}
1160
1161