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