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