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