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