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