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