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