Actually hook powernow.4 into the build.
[dragonfly.git] / sys / netinet / tcp_usrreq.c
blob8c5f54f57ed616c76750f149410e4f5868fa6dad
1 /*
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
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.
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.
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
67 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
68 * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.51 2008/09/29 20:52:23 dillon Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73 #include "opt_tcpdebug.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/sysctl.h>
80 #include <sys/globaldata.h>
81 #include <sys/thread.h>
83 #include <sys/mbuf.h>
84 #ifdef INET6
85 #include <sys/domain.h>
86 #endif /* INET6 */
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/protosw.h>
91 #include <sys/thread2.h>
92 #include <sys/msgport2.h>
94 #include <net/if.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
98 #include <net/netmsg2.h>
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #ifdef INET6
103 #include <netinet/ip6.h>
104 #endif
105 #include <netinet/in_pcb.h>
106 #ifdef INET6
107 #include <netinet6/in6_pcb.h>
108 #endif
109 #include <netinet/in_var.h>
110 #include <netinet/ip_var.h>
111 #ifdef INET6
112 #include <netinet6/ip6_var.h>
113 #include <netinet6/tcp6_var.h>
114 #endif
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_timer2.h>
120 #include <netinet/tcp_var.h>
121 #include <netinet/tcpip.h>
122 #ifdef TCPDEBUG
123 #include <netinet/tcp_debug.h>
124 #endif
126 #ifdef IPSEC
127 #include <netinet6/ipsec.h>
128 #endif /*IPSEC*/
131 * TCP protocol interface to socket abstraction.
133 extern char *tcpstates[]; /* XXX ??? */
135 static int tcp_attach (struct socket *, struct pru_attach_info *);
136 static int tcp_connect (struct tcpcb *, int flags, struct mbuf *m,
137 struct sockaddr *, struct thread *);
138 #ifdef INET6
139 static int tcp6_connect (struct tcpcb *, int flags, struct mbuf *m,
140 struct sockaddr *, struct thread *);
141 static int tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
142 struct sockaddr_in6 *sin6,
143 struct in6_addr *addr6);
144 #endif /* INET6 */
145 static struct tcpcb *
146 tcp_disconnect (struct tcpcb *);
147 static struct tcpcb *
148 tcp_usrclosed (struct tcpcb *);
150 #ifdef TCPDEBUG
151 #define TCPDEBUG0 int ostate = 0
152 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
153 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
154 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
155 #else
156 #define TCPDEBUG0
157 #define TCPDEBUG1()
158 #define TCPDEBUG2(req)
159 #endif
162 * TCP attaches to socket via pru_attach(), reserving space,
163 * and an internet control block.
165 static int
166 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
168 int error;
169 struct inpcb *inp;
170 struct tcpcb *tp = 0;
171 TCPDEBUG0;
173 crit_enter();
174 inp = so->so_pcb;
175 TCPDEBUG1();
176 if (inp) {
177 error = EISCONN;
178 goto out;
181 error = tcp_attach(so, ai);
182 if (error)
183 goto out;
185 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
186 so->so_linger = TCP_LINGERTIME;
187 tp = sototcpcb(so);
188 out:
189 TCPDEBUG2(PRU_ATTACH);
190 crit_exit();
191 return error;
195 * pru_detach() detaches the TCP protocol from the socket.
196 * If the protocol state is non-embryonic, then can't
197 * do this directly: have to initiate a pru_disconnect(),
198 * which may finish later; embryonic TCB's can just
199 * be discarded here.
201 static int
202 tcp_usr_detach(struct socket *so)
204 int error = 0;
205 struct inpcb *inp;
206 struct tcpcb *tp;
207 TCPDEBUG0;
209 crit_enter();
210 inp = so->so_pcb;
213 * If the inp is already detached it may have been due to an async
214 * close. Just return as if no error occured.
216 if (inp == NULL) {
217 crit_exit();
218 return 0;
222 * It's possible for the tcpcb (tp) to disconnect from the inp due
223 * to tcp_drop()->tcp_close() being called. This may occur *after*
224 * the detach message has been queued so we may find a NULL tp here.
226 if ((tp = intotcpcb(inp)) != NULL) {
227 TCPDEBUG1();
228 tp = tcp_disconnect(tp);
229 TCPDEBUG2(PRU_DETACH);
231 crit_exit();
232 return error;
236 * Note: ignore_error is non-zero for certain disconnection races
237 * which we want to silently allow, otherwise close() may return
238 * an unexpected error.
240 #define COMMON_START(so, inp, ignore_error) \
241 TCPDEBUG0; \
243 crit_enter(); \
244 inp = so->so_pcb; \
245 do { \
246 if (inp == NULL) { \
247 crit_exit(); \
248 return (ignore_error ? 0 : EINVAL); \
250 tp = intotcpcb(inp); \
251 TCPDEBUG1(); \
252 } while(0)
254 #define COMMON_END(req) out: TCPDEBUG2(req); crit_exit(); return error; goto out
258 * Give the socket an address.
260 static int
261 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
263 int error = 0;
264 struct inpcb *inp;
265 struct tcpcb *tp;
266 struct sockaddr_in *sinp;
268 COMMON_START(so, inp, 0);
271 * Must check for multicast addresses and disallow binding
272 * to them.
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;
280 error = in_pcbbind(inp, nam, td);
281 if (error)
282 goto out;
283 COMMON_END(PRU_BIND);
287 #ifdef INET6
288 static int
289 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
291 int error = 0;
292 struct inpcb *inp;
293 struct tcpcb *tp;
294 struct sockaddr_in6 *sin6p;
296 COMMON_START(so, inp, 0);
299 * Must check for multicast addresses and disallow binding
300 * to them.
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;
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;
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;
323 error = in6_pcbbind(inp, nam, td);
324 if (error)
325 goto out;
326 COMMON_END(PRU_BIND);
328 #endif /* INET6 */
330 #ifdef SMP
331 struct netmsg_inswildcard {
332 struct netmsg nm_netmsg;
333 struct inpcb *nm_inp;
334 struct inpcbinfo *nm_pcbinfo;
337 static void
338 in_pcbinswildcardhash_handler(struct netmsg *msg0)
340 struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
342 in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
343 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
345 #endif
348 * Prepare to accept connections.
350 static int
351 tcp_usr_listen(struct socket *so, struct thread *td)
353 int error = 0;
354 struct inpcb *inp;
355 struct tcpcb *tp;
356 #ifdef SMP
357 int cpu;
358 #endif
360 COMMON_START(so, inp, 0);
361 if (inp->inp_lport == 0) {
362 error = in_pcbbind(inp, NULL, td);
363 if (error != 0)
364 goto out;
367 tp->t_state = TCPS_LISTEN;
368 tp->tt_msg = NULL; /* Catch any invalid timer usage */
369 #ifdef SMP
371 * We have to set the flag because we can't have other cpus
372 * messing with our inp's flags.
374 inp->inp_flags |= INP_WILDCARD_MP;
375 for (cpu = 0; cpu < ncpus2; cpu++) {
376 struct netmsg_inswildcard *msg;
378 if (cpu == mycpu->gd_cpuid) {
379 in_pcbinswildcardhash(inp);
380 continue;
383 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
384 M_INTWAIT);
385 netmsg_init(&msg->nm_netmsg, NULL, &netisr_afree_rport,
386 0, in_pcbinswildcardhash_handler);
387 msg->nm_inp = inp;
388 msg->nm_pcbinfo = &tcbinfo[cpu];
389 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
391 #else
392 in_pcbinswildcardhash(inp);
393 #endif
394 COMMON_END(PRU_LISTEN);
397 #ifdef INET6
398 static int
399 tcp6_usr_listen(struct socket *so, struct thread *td)
401 int error = 0;
402 struct inpcb *inp;
403 struct tcpcb *tp;
404 #ifdef SMP
405 int cpu;
406 #endif
408 COMMON_START(so, inp, 0);
409 if (inp->inp_lport == 0) {
410 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
411 inp->inp_vflag |= INP_IPV4;
412 else
413 inp->inp_vflag &= ~INP_IPV4;
414 error = in6_pcbbind(inp, NULL, td);
416 if (error == 0)
417 tp->t_state = TCPS_LISTEN;
418 #ifdef SMP
420 * We have to set the flag because we can't have other cpus
421 * messing with our inp's flags.
423 inp->inp_flags |= INP_WILDCARD_MP;
424 for (cpu = 0; cpu < ncpus2; cpu++) {
425 struct netmsg_inswildcard *msg;
427 if (cpu == mycpu->gd_cpuid) {
428 in_pcbinswildcardhash(inp);
429 continue;
432 msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
433 M_INTWAIT);
434 netmsg_init(&msg->nm_netmsg, NULL, &netisr_afree_rport,
435 0, in_pcbinswildcardhash_handler);
436 msg->nm_inp = inp;
437 msg->nm_pcbinfo = &tcbinfo[cpu];
438 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_netmsg.nm_lmsg);
440 #else
441 in_pcbinswildcardhash(inp);
442 #endif
443 COMMON_END(PRU_LISTEN);
445 #endif /* INET6 */
448 * Initiate connection to peer.
449 * Create a template for use in transmissions on this connection.
450 * Enter SYN_SENT state, and mark socket as connecting.
451 * Start keep-alive timer, and seed output sequence space.
452 * Send initial segment on connection.
454 static int
455 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
457 int error = 0;
458 struct inpcb *inp;
459 struct tcpcb *tp;
460 struct sockaddr_in *sinp;
462 COMMON_START(so, inp, 0);
465 * Must disallow TCP ``connections'' to multicast addresses.
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;
474 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
475 error = EAFNOSUPPORT; /* IPv6 only jail */
476 goto out;
479 if ((error = tcp_connect(tp, 0, NULL, nam, td)) != 0)
480 goto out;
481 COMMON_END(PRU_CONNECT);
484 #ifdef INET6
485 static int
486 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
488 int error = 0;
489 struct inpcb *inp;
490 struct tcpcb *tp;
491 struct sockaddr_in6 *sin6p;
493 COMMON_START(so, inp, 0);
496 * Must disallow TCP ``connections'' to multicast addresses.
498 sin6p = (struct sockaddr_in6 *)nam;
499 if (sin6p->sin6_family == AF_INET6
500 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
501 error = EAFNOSUPPORT;
502 goto out;
505 if (!prison_remote_ip(td, nam)) {
506 error = EAFNOSUPPORT; /* IPv4 only jail */
507 goto out;
510 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
511 struct sockaddr_in sin;
513 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
514 error = EINVAL;
515 goto out;
518 in6_sin6_2_sin(&sin, sin6p);
519 inp->inp_vflag |= INP_IPV4;
520 inp->inp_vflag &= ~INP_IPV6;
521 error = tcp_connect(tp, 0, NULL, (struct sockaddr *)&sin, td);
522 if (error)
523 goto out;
524 goto out;
526 inp->inp_vflag &= ~INP_IPV4;
527 inp->inp_vflag |= INP_IPV6;
528 inp->inp_inc.inc_isipv6 = 1;
529 if ((error = tcp6_connect(tp, 0, NULL, nam, td)) != 0)
530 goto out;
531 error = tcp_output(tp);
532 COMMON_END(PRU_CONNECT);
534 #endif /* INET6 */
537 * Initiate disconnect from peer.
538 * If connection never passed embryonic stage, just drop;
539 * else if don't need to let data drain, then can just drop anyways,
540 * else have to begin TCP shutdown process: mark socket disconnecting,
541 * drain unread data, state switch to reflect user close, and
542 * send segment (e.g. FIN) to peer. Socket will be really disconnected
543 * when peer sends FIN and acks ours.
545 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
547 static int
548 tcp_usr_disconnect(struct socket *so)
550 int error = 0;
551 struct inpcb *inp;
552 struct tcpcb *tp;
554 COMMON_START(so, inp, 1);
555 tp = tcp_disconnect(tp);
556 COMMON_END(PRU_DISCONNECT);
560 * Accept a connection. Essentially all the work is
561 * done at higher levels; just return the address
562 * of the peer, storing through addr.
564 static int
565 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
567 int error = 0;
568 struct inpcb *inp;
569 struct tcpcb *tp = NULL;
570 TCPDEBUG0;
572 crit_enter();
573 inp = so->so_pcb;
574 if (so->so_state & SS_ISDISCONNECTED) {
575 error = ECONNABORTED;
576 goto out;
578 if (inp == 0) {
579 crit_exit();
580 return (EINVAL);
582 tp = intotcpcb(inp);
583 TCPDEBUG1();
584 in_setpeeraddr(so, nam);
585 COMMON_END(PRU_ACCEPT);
588 #ifdef INET6
589 static int
590 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
592 int error = 0;
593 struct inpcb *inp;
594 struct tcpcb *tp = NULL;
595 TCPDEBUG0;
597 crit_enter();
598 inp = so->so_pcb;
600 if (so->so_state & SS_ISDISCONNECTED) {
601 error = ECONNABORTED;
602 goto out;
604 if (inp == 0) {
605 crit_exit();
606 return (EINVAL);
608 tp = intotcpcb(inp);
609 TCPDEBUG1();
610 in6_mapped_peeraddr(so, nam);
611 COMMON_END(PRU_ACCEPT);
613 #endif /* INET6 */
615 * Mark the connection as being incapable of further output.
617 static int
618 tcp_usr_shutdown(struct socket *so)
620 int error = 0;
621 struct inpcb *inp;
622 struct tcpcb *tp;
624 COMMON_START(so, inp, 0);
625 socantsendmore(so);
626 tp = tcp_usrclosed(tp);
627 if (tp)
628 error = tcp_output(tp);
629 COMMON_END(PRU_SHUTDOWN);
633 * After a receive, possibly send window update to peer.
635 static int
636 tcp_usr_rcvd(struct socket *so, int flags)
638 int error = 0;
639 struct inpcb *inp;
640 struct tcpcb *tp;
642 COMMON_START(so, inp, 0);
643 tcp_output(tp);
644 COMMON_END(PRU_RCVD);
648 * Do a send by putting data in output queue and updating urgent
649 * marker if URG set. Possibly send more data. Unlike the other
650 * pru_*() routines, the mbuf chains are our responsibility. We
651 * must either enqueue them or free them. The other pru_* routines
652 * generally are caller-frees.
654 static int
655 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
656 struct sockaddr *nam, struct mbuf *control, struct thread *td)
658 int error = 0;
659 struct inpcb *inp;
660 struct tcpcb *tp;
661 #ifdef INET6
662 int isipv6;
663 #endif
664 TCPDEBUG0;
666 crit_enter();
667 inp = so->so_pcb;
669 if (inp == NULL) {
671 * OOPS! we lost a race, the TCP session got reset after
672 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
673 * network interrupt in the non-critical section of sosend().
675 m_freem(m);
676 if (control)
677 m_freem(control);
678 error = ECONNRESET; /* XXX EPIPE? */
679 tp = NULL;
680 TCPDEBUG1();
681 goto out;
683 #ifdef INET6
684 isipv6 = nam && nam->sa_family == AF_INET6;
685 #endif /* INET6 */
686 tp = intotcpcb(inp);
687 TCPDEBUG1();
688 if (control) {
689 /* TCP doesn't do control messages (rights, creds, etc) */
690 if (control->m_len) {
691 m_freem(control);
692 m_freem(m);
693 error = EINVAL;
694 goto out;
696 m_freem(control); /* empty control, just free it */
700 * Don't let too much OOB data build up
702 if (flags & PRUS_OOB) {
703 if (ssb_space(&so->so_snd) < -512) {
704 m_freem(m);
705 error = ENOBUFS;
706 goto out;
711 * Do implied connect if not yet connected. Any data sent
712 * with the connect is handled by tcp_connect() and friends.
714 * NOTE! PROTOCOL THREAD MAY BE CHANGED BY THE CONNECT!
716 if (nam && tp->t_state < TCPS_SYN_SENT) {
717 #ifdef INET6
718 if (isipv6)
719 error = tcp6_connect(tp, flags, m, nam, td);
720 else
721 #endif /* INET6 */
722 error = tcp_connect(tp, flags, m, nam, td);
723 #if 0
724 /* WTF is this doing here? */
725 tp->snd_wnd = TTCP_CLIENT_SND_WND;
726 tcp_mss(tp, -1);
727 #endif
728 goto out;
732 * Pump the data into the socket.
734 if (m)
735 ssb_appendstream(&so->so_snd, m);
736 if (flags & PRUS_OOB) {
738 * According to RFC961 (Assigned Protocols),
739 * the urgent pointer points to the last octet
740 * of urgent data. We continue, however,
741 * to consider it to indicate the first octet
742 * of data past the urgent section.
743 * Otherwise, snd_up should be one lower.
745 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
746 tp->t_flags |= TF_FORCE;
747 error = tcp_output(tp);
748 tp->t_flags &= ~TF_FORCE;
749 } else {
750 if (flags & PRUS_EOF) {
752 * Close the send side of the connection after
753 * the data is sent.
755 socantsendmore(so);
756 tp = tcp_usrclosed(tp);
758 if (tp != NULL) {
759 if (flags & PRUS_MORETOCOME)
760 tp->t_flags |= TF_MORETOCOME;
761 error = tcp_output(tp);
762 if (flags & PRUS_MORETOCOME)
763 tp->t_flags &= ~TF_MORETOCOME;
766 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
767 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
771 * Abort the TCP.
773 static int
774 tcp_usr_abort(struct socket *so)
776 int error = 0;
777 struct inpcb *inp;
778 struct tcpcb *tp;
780 COMMON_START(so, inp, 1);
781 tp = tcp_drop(tp, ECONNABORTED);
782 COMMON_END(PRU_ABORT);
786 * Receive out-of-band data.
788 static int
789 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
791 int error = 0;
792 struct inpcb *inp;
793 struct tcpcb *tp;
795 COMMON_START(so, inp, 0);
796 if ((so->so_oobmark == 0 &&
797 (so->so_state & SS_RCVATMARK) == 0) ||
798 so->so_options & SO_OOBINLINE ||
799 tp->t_oobflags & TCPOOB_HADDATA) {
800 error = EINVAL;
801 goto out;
803 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
804 error = EWOULDBLOCK;
805 goto out;
807 m->m_len = 1;
808 *mtod(m, caddr_t) = tp->t_iobc;
809 if ((flags & MSG_PEEK) == 0)
810 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
811 COMMON_END(PRU_RCVOOB);
814 /* xxx - should be const */
815 struct pr_usrreqs tcp_usrreqs = {
816 .pru_abort = tcp_usr_abort,
817 .pru_accept = tcp_usr_accept,
818 .pru_attach = tcp_usr_attach,
819 .pru_bind = tcp_usr_bind,
820 .pru_connect = tcp_usr_connect,
821 .pru_connect2 = pru_connect2_notsupp,
822 .pru_control = in_control,
823 .pru_detach = tcp_usr_detach,
824 .pru_disconnect = tcp_usr_disconnect,
825 .pru_listen = tcp_usr_listen,
826 .pru_peeraddr = in_setpeeraddr,
827 .pru_rcvd = tcp_usr_rcvd,
828 .pru_rcvoob = tcp_usr_rcvoob,
829 .pru_send = tcp_usr_send,
830 .pru_sense = pru_sense_null,
831 .pru_shutdown = tcp_usr_shutdown,
832 .pru_sockaddr = in_setsockaddr,
833 .pru_sosend = sosend,
834 .pru_soreceive = soreceive,
835 .pru_sopoll = sopoll
838 #ifdef INET6
839 struct pr_usrreqs tcp6_usrreqs = {
840 .pru_abort = tcp_usr_abort,
841 .pru_accept = tcp6_usr_accept,
842 .pru_attach = tcp_usr_attach,
843 .pru_bind = tcp6_usr_bind,
844 .pru_connect = tcp6_usr_connect,
845 .pru_connect2 = pru_connect2_notsupp,
846 .pru_control = in6_control,
847 .pru_detach = tcp_usr_detach,
848 .pru_disconnect = tcp_usr_disconnect,
849 .pru_listen = tcp6_usr_listen,
850 .pru_peeraddr = in6_mapped_peeraddr,
851 .pru_rcvd = tcp_usr_rcvd,
852 .pru_rcvoob = tcp_usr_rcvoob,
853 .pru_send = tcp_usr_send,
854 .pru_sense = pru_sense_null,
855 .pru_shutdown = tcp_usr_shutdown,
856 .pru_sockaddr = in6_mapped_sockaddr,
857 .pru_sosend = sosend,
858 .pru_soreceive = soreceive,
859 .pru_sopoll = sopoll
861 #endif /* INET6 */
863 static int
864 tcp_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
865 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
867 struct inpcb *inp = tp->t_inpcb, *oinp;
868 struct socket *so = inp->inp_socket;
869 struct route *ro = &inp->inp_route;
871 oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
872 sin->sin_addr, sin->sin_port,
873 inp->inp_laddr.s_addr != INADDR_ANY ?
874 inp->inp_laddr : if_sin->sin_addr,
875 inp->inp_lport, 0, NULL);
876 if (oinp != NULL) {
877 m_freem(m);
878 return (EADDRINUSE);
880 if (inp->inp_laddr.s_addr == INADDR_ANY)
881 inp->inp_laddr = if_sin->sin_addr;
882 inp->inp_faddr = sin->sin_addr;
883 inp->inp_fport = sin->sin_port;
884 inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
885 in_pcbinsconnhash(inp);
888 * We are now on the inpcb's owner CPU, if the cached route was
889 * freed because the rtentry's owner CPU is not the current CPU
890 * (e.g. in tcp_connect()), then we try to reallocate it here with
891 * the hope that a rtentry may be cloned from a RTF_PRCLONING
892 * rtentry.
894 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
895 ro->ro_rt == NULL) {
896 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
897 ro->ro_dst.sa_family = AF_INET;
898 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
899 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr =
900 sin->sin_addr;
901 rtalloc(ro);
905 * Now that no more errors can occur, change the protocol processing
906 * port to the current thread (which is the correct thread).
908 * Create TCP timer message now; we are on the tcpcb's owner
909 * CPU/thread.
911 sosetport(so, &curthread->td_msgport);
912 tcp_create_timermsg(tp, &curthread->td_msgport);
915 * Compute window scaling to request. Use a larger scaling then
916 * needed for the initial receive buffer in case the receive buffer
917 * gets expanded.
919 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
920 tp->request_r_scale = TCP_MIN_WINSHIFT;
921 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
922 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat
924 tp->request_r_scale++;
927 soisconnecting(so);
928 tcpstat.tcps_connattempt++;
929 tp->t_state = TCPS_SYN_SENT;
930 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
931 tp->iss = tcp_new_isn(tp);
932 tcp_sendseqinit(tp);
933 if (m) {
934 ssb_appendstream(&so->so_snd, m);
935 m = NULL;
936 if (flags & PRUS_OOB)
937 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
941 * Close the send side of the connection after
942 * the data is sent if flagged.
944 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
945 socantsendmore(so);
946 tp = tcp_usrclosed(tp);
948 return (tcp_output(tp));
951 #ifdef SMP
953 struct netmsg_tcp_connect {
954 struct netmsg nm_netmsg;
955 struct tcpcb *nm_tp;
956 struct sockaddr_in *nm_sin;
957 struct sockaddr_in *nm_ifsin;
958 int nm_flags;
959 struct mbuf *nm_m;
962 static void
963 tcp_connect_handler(netmsg_t netmsg)
965 struct netmsg_tcp_connect *msg = (void *)netmsg;
966 int error;
968 error = tcp_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
969 msg->nm_sin, msg->nm_ifsin);
970 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
973 struct netmsg_tcp6_connect {
974 struct netmsg nm_netmsg;
975 struct tcpcb *nm_tp;
976 struct sockaddr_in6 *nm_sin6;
977 struct in6_addr *nm_addr6;
978 int nm_flags;
979 struct mbuf *nm_m;
982 #ifdef INET6
983 static void
984 tcp6_connect_handler(netmsg_t netmsg)
986 struct netmsg_tcp6_connect *msg = (void *)netmsg;
987 int error;
989 error = tcp6_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
990 msg->nm_sin6, msg->nm_addr6);
991 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
993 #endif
995 #endif /* SMP */
998 * Common subroutine to open a TCP connection to remote host specified
999 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1000 * port number if needed. Call in_pcbladdr to do the routing and to choose
1001 * a local host address (interface).
1002 * Initialize connection parameters and enter SYN-SENT state.
1004 static int
1005 tcp_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1006 struct sockaddr *nam, struct thread *td)
1008 struct inpcb *inp = tp->t_inpcb;
1009 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1010 struct sockaddr_in *if_sin;
1011 int error;
1012 #ifdef SMP
1013 lwkt_port_t port;
1014 #endif
1017 * Bind if we have to
1019 if (inp->inp_lport == 0) {
1020 error = in_pcbbind(inp, NULL, td);
1021 if (error) {
1022 m_freem(m);
1023 return (error);
1028 * Calculate the correct protocol processing thread. The connect
1029 * operation must run there.
1031 error = in_pcbladdr(inp, nam, &if_sin, td);
1032 if (error) {
1033 m_freem(m);
1034 return (error);
1037 #ifdef SMP
1038 port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1039 inp->inp_laddr.s_addr ?
1040 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
1041 inp->inp_lport);
1043 if (port != &curthread->td_msgport) {
1044 struct netmsg_tcp_connect msg;
1045 struct route *ro = &inp->inp_route;
1048 * in_pcbladdr() may have allocated a route entry for us
1049 * on the current CPU, but we need a route entry on the
1050 * inpcb's owner CPU, so free it here.
1052 if (ro->ro_rt != NULL)
1053 RTFREE(ro->ro_rt);
1054 bzero(ro, sizeof(*ro));
1057 * NOTE: We haven't set so->so_port yet do not pass so
1058 * to netmsg_init() or it will be improperly forwarded.
1060 netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport,
1061 0, tcp_connect_handler);
1062 msg.nm_tp = tp;
1063 msg.nm_sin = sin;
1064 msg.nm_ifsin = if_sin;
1065 msg.nm_flags = flags;
1066 msg.nm_m = m;
1067 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1068 } else {
1069 error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1071 #else
1072 error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1073 #endif
1074 return (error);
1077 #ifdef INET6
1079 static int
1080 tcp6_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1081 struct sockaddr *nam, struct thread *td)
1083 struct inpcb *inp = tp->t_inpcb;
1084 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1085 struct in6_addr *addr6;
1086 #ifdef SMP
1087 lwkt_port_t port;
1088 #endif
1089 int error;
1091 if (inp->inp_lport == 0) {
1092 error = in6_pcbbind(inp, NULL, td);
1093 if (error) {
1094 m_freem(m);
1095 return (error);
1100 * Cannot simply call in_pcbconnect, because there might be an
1101 * earlier incarnation of this same connection still in
1102 * TIME_WAIT state, creating an ADDRINUSE error.
1104 error = in6_pcbladdr(inp, nam, &addr6, td);
1105 if (error) {
1106 m_freem(m);
1107 return (error);
1110 #ifdef SMP
1111 port = tcp6_addrport(); /* XXX hack for now, always cpu0 */
1113 if (port != &curthread->td_msgport) {
1114 struct netmsg_tcp6_connect msg;
1115 struct route *ro = &inp->inp_route;
1118 * in_pcbladdr() may have allocated a route entry for us
1119 * on the current CPU, but we need a route entry on the
1120 * inpcb's owner CPU, so free it here.
1122 if (ro->ro_rt != NULL)
1123 RTFREE(ro->ro_rt);
1124 bzero(ro, sizeof(*ro));
1126 netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport,
1127 0, tcp6_connect_handler);
1128 msg.nm_tp = tp;
1129 msg.nm_sin6 = sin6;
1130 msg.nm_addr6 = addr6;
1131 msg.nm_flags = flags;
1132 msg.nm_m = m;
1133 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1134 } else {
1135 error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1137 #else
1138 error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1139 #endif
1140 return (error);
1143 static int
1144 tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
1145 struct sockaddr_in6 *sin6, struct in6_addr *addr6)
1147 struct inpcb *inp = tp->t_inpcb;
1148 struct socket *so = inp->inp_socket;
1149 struct inpcb *oinp;
1152 * Cannot simply call in_pcbconnect, because there might be an
1153 * earlier incarnation of this same connection still in
1154 * TIME_WAIT state, creating an ADDRINUSE error.
1156 oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1157 &sin6->sin6_addr, sin6->sin6_port,
1158 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1159 addr6 : &inp->in6p_laddr,
1160 inp->inp_lport, 0, NULL);
1161 if (oinp) {
1162 m_freem(m);
1163 return (EADDRINUSE);
1165 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1166 inp->in6p_laddr = *addr6;
1167 inp->in6p_faddr = sin6->sin6_addr;
1168 inp->inp_fport = sin6->sin6_port;
1169 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1170 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1171 in_pcbinsconnhash(inp);
1174 * Now that no more errors can occur, change the protocol processing
1175 * port to the current thread (which is the correct thread).
1177 * Create TCP timer message now; we are on the tcpcb's owner
1178 * CPU/thread.
1180 sosetport(so, &curthread->td_msgport);
1181 tcp_create_timermsg(tp, &curthread->td_msgport);
1183 /* Compute window scaling to request. */
1184 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
1185 tp->request_r_scale = TCP_MIN_WINSHIFT;
1186 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1187 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) {
1188 tp->request_r_scale++;
1191 soisconnecting(so);
1192 tcpstat.tcps_connattempt++;
1193 tp->t_state = TCPS_SYN_SENT;
1194 tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
1195 tp->iss = tcp_new_isn(tp);
1196 tcp_sendseqinit(tp);
1197 if (m) {
1198 ssb_appendstream(&so->so_snd, m);
1199 m = NULL;
1200 if (flags & PRUS_OOB)
1201 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
1205 * Close the send side of the connection after
1206 * the data is sent if flagged.
1208 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
1209 socantsendmore(so);
1210 tp = tcp_usrclosed(tp);
1212 return (tcp_output(tp));
1215 #endif /* INET6 */
1218 * The new sockopt interface makes it possible for us to block in the
1219 * copyin/out step (if we take a page fault). Taking a page fault while
1220 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1221 * both now use TSM, there probably isn't any need for this function to
1222 * run in a critical section any more. This needs more examination.)
1225 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1227 int error, opt, optval;
1228 struct inpcb *inp;
1229 struct tcpcb *tp;
1231 error = 0;
1232 crit_enter(); /* XXX */
1233 inp = so->so_pcb;
1234 if (inp == NULL) {
1235 crit_exit();
1236 return (ECONNRESET);
1238 if (sopt->sopt_level != IPPROTO_TCP) {
1239 #ifdef INET6
1240 if (INP_CHECK_SOCKAF(so, AF_INET6))
1241 error = ip6_ctloutput(so, sopt);
1242 else
1243 #endif /* INET6 */
1244 error = ip_ctloutput(so, sopt);
1245 crit_exit();
1246 return (error);
1248 tp = intotcpcb(inp);
1250 switch (sopt->sopt_dir) {
1251 case SOPT_SET:
1252 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1253 sizeof optval);
1254 if (error)
1255 break;
1256 switch (sopt->sopt_name) {
1257 case TCP_NODELAY:
1258 case TCP_NOOPT:
1259 switch (sopt->sopt_name) {
1260 case TCP_NODELAY:
1261 opt = TF_NODELAY;
1262 break;
1263 case TCP_NOOPT:
1264 opt = TF_NOOPT;
1265 break;
1266 default:
1267 opt = 0; /* dead code to fool gcc */
1268 break;
1271 if (optval)
1272 tp->t_flags |= opt;
1273 else
1274 tp->t_flags &= ~opt;
1275 break;
1277 case TCP_NOPUSH:
1278 if (optval)
1279 tp->t_flags |= TF_NOPUSH;
1280 else {
1281 tp->t_flags &= ~TF_NOPUSH;
1282 error = tcp_output(tp);
1284 break;
1286 case TCP_MAXSEG:
1288 * Must be between 0 and maxseg. If the requested
1289 * maxseg is too small to satisfy the desired minmss,
1290 * pump it up (silently so sysctl modifications of
1291 * minmss do not create unexpected program failures).
1292 * Handle degenerate cases.
1294 if (optval > 0 && optval <= tp->t_maxseg) {
1295 if (optval + 40 < tcp_minmss) {
1296 optval = tcp_minmss - 40;
1297 if (optval < 0)
1298 optval = 1;
1300 tp->t_maxseg = optval;
1301 } else {
1302 error = EINVAL;
1304 break;
1306 default:
1307 error = ENOPROTOOPT;
1308 break;
1310 break;
1312 case SOPT_GET:
1313 switch (sopt->sopt_name) {
1314 case TCP_NODELAY:
1315 optval = tp->t_flags & TF_NODELAY;
1316 break;
1317 case TCP_MAXSEG:
1318 optval = tp->t_maxseg;
1319 break;
1320 case TCP_NOOPT:
1321 optval = tp->t_flags & TF_NOOPT;
1322 break;
1323 case TCP_NOPUSH:
1324 optval = tp->t_flags & TF_NOPUSH;
1325 break;
1326 default:
1327 error = ENOPROTOOPT;
1328 break;
1330 if (error == 0)
1331 soopt_from_kbuf(sopt, &optval, sizeof optval);
1332 break;
1334 crit_exit();
1335 return (error);
1339 * tcp_sendspace and tcp_recvspace are the default send and receive window
1340 * sizes, respectively. These are obsolescent (this information should
1341 * be set by the route).
1343 * Use a default that does not require tcp window scaling to be turned
1344 * on. Individual programs or the administrator can increase the default.
1346 u_long tcp_sendspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1347 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1348 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1349 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1350 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1351 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1354 * Attach TCP protocol to socket, allocating
1355 * internet protocol control block, tcp control block,
1356 * bufer space, and entering LISTEN state if to accept connections.
1358 static int
1359 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1361 struct tcpcb *tp;
1362 struct inpcb *inp;
1363 int error;
1364 int cpu;
1365 #ifdef INET6
1366 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1367 #endif
1369 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1370 error = soreserve(so, tcp_sendspace, tcp_recvspace,
1371 ai->sb_rlimit);
1372 if (error)
1373 return (error);
1375 so->so_rcv.ssb_flags |= SSB_AUTOSIZE;
1376 so->so_snd.ssb_flags |= SSB_AUTOSIZE;
1377 cpu = mycpu->gd_cpuid;
1378 error = in_pcballoc(so, &tcbinfo[cpu]);
1379 if (error)
1380 return (error);
1381 inp = so->so_pcb;
1382 #ifdef INET6
1383 if (isipv6) {
1384 inp->inp_vflag |= INP_IPV6;
1385 inp->in6p_hops = -1; /* use kernel default */
1387 else
1388 #endif
1389 inp->inp_vflag |= INP_IPV4;
1390 tp = tcp_newtcpcb(inp);
1391 if (tp == 0) {
1392 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1394 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1395 #ifdef INET6
1396 if (isipv6)
1397 in6_pcbdetach(inp);
1398 else
1399 #endif
1400 in_pcbdetach(inp);
1401 so->so_state |= nofd;
1402 return (ENOBUFS);
1404 tp->t_state = TCPS_CLOSED;
1405 so->so_port = tcp_soport_attach(so);
1406 return (0);
1410 * Initiate (or continue) disconnect.
1411 * If embryonic state, just send reset (once).
1412 * If in ``let data drain'' option and linger null, just drop.
1413 * Otherwise (hard), mark socket disconnecting and drop
1414 * current input data; switch states based on user close, and
1415 * send segment to peer (with FIN).
1417 static struct tcpcb *
1418 tcp_disconnect(struct tcpcb *tp)
1420 struct socket *so = tp->t_inpcb->inp_socket;
1422 if (tp->t_state < TCPS_ESTABLISHED)
1423 tp = tcp_close(tp);
1424 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1425 tp = tcp_drop(tp, 0);
1426 else {
1427 soisdisconnecting(so);
1428 sbflush(&so->so_rcv.sb);
1429 tp = tcp_usrclosed(tp);
1430 if (tp)
1431 tcp_output(tp);
1433 return (tp);
1437 * User issued close, and wish to trail through shutdown states:
1438 * if never received SYN, just forget it. If got a SYN from peer,
1439 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1440 * If already got a FIN from peer, then almost done; go to LAST_ACK
1441 * state. In all other cases, have already sent FIN to peer (e.g.
1442 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1443 * for peer to send FIN or not respond to keep-alives, etc.
1444 * We can let the user exit from the close as soon as the FIN is acked.
1446 static struct tcpcb *
1447 tcp_usrclosed(struct tcpcb *tp)
1450 switch (tp->t_state) {
1452 case TCPS_CLOSED:
1453 case TCPS_LISTEN:
1454 tp->t_state = TCPS_CLOSED;
1455 tp = tcp_close(tp);
1456 break;
1458 case TCPS_SYN_SENT:
1459 case TCPS_SYN_RECEIVED:
1460 tp->t_flags |= TF_NEEDFIN;
1461 break;
1463 case TCPS_ESTABLISHED:
1464 tp->t_state = TCPS_FIN_WAIT_1;
1465 break;
1467 case TCPS_CLOSE_WAIT:
1468 tp->t_state = TCPS_LAST_ACK;
1469 break;
1471 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1472 soisdisconnected(tp->t_inpcb->inp_socket);
1473 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1474 if (tp->t_state == TCPS_FIN_WAIT_2) {
1475 tcp_callout_reset(tp, tp->tt_2msl, tcp_maxidle,
1476 tcp_timer_2msl);
1479 return (tp);