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