2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2006-2007 Robert N. M. Watson
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
44 #include "opt_tcpdebug.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/refcount.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
55 #include <sys/domain.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/protosw.h>
68 #include <net/if_var.h>
69 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_kdtrace.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/in6_pcb.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/scope6_var.h>
85 #include <netinet/tcp_fastopen.h>
87 #include <netinet/tcp.h>
88 #include <netinet/tcp_fsm.h>
89 #include <netinet/tcp_seq.h>
90 #include <netinet/tcp_timer.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet/tcpip.h>
93 #include <netinet/tcp_cc.h>
95 #include <netinet/tcp_pcap.h>
98 #include <netinet/tcp_debug.h>
101 #include <netinet/tcp_offload.h>
105 * TCP protocol interface to socket abstraction.
107 static int tcp_attach(struct socket *);
109 static int tcp_connect(struct tcpcb *, struct sockaddr *,
113 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
116 static void tcp_disconnect(struct tcpcb *);
117 static void tcp_usrclosed(struct tcpcb *);
118 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
121 #define TCPDEBUG0 int ostate = 0
122 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
123 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
124 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
128 #define TCPDEBUG2(req)
132 * TCP attaches to socket via pru_attach(), reserving space,
133 * and an internet control block.
136 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
139 struct tcpcb *tp = NULL;
144 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
147 error = tcp_attach(so);
151 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
152 so->so_linger = TCP_LINGERTIME;
157 TCPDEBUG2(PRU_ATTACH);
158 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
163 * tcp_detach is called when the socket layer loses its final reference
164 * to the socket, be it a file descriptor reference, a reference from TCP,
165 * etc. At this point, there is only one case in which we will keep around
166 * inpcb state: time wait.
168 * This function can probably be re-absorbed back into tcp_usr_detach() now
169 * that there is a single detach path.
172 tcp_detach(struct socket *so, struct inpcb *inp)
176 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
177 INP_WLOCK_ASSERT(inp);
179 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
180 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
184 if (inp->inp_flags & INP_TIMEWAIT) {
186 * There are two cases to handle: one in which the time wait
187 * state is being discarded (INP_DROPPED), and one in which
188 * this connection will remain in timewait. In the former,
189 * it is time to discard all state (except tcptw, which has
190 * already been discarded by the timewait close code, which
191 * should be further up the call stack somewhere). In the
192 * latter case, we detach from the socket, but leave the pcb
193 * present until timewait ends.
195 * XXXRW: Would it be cleaner to free the tcptw here?
197 * Astute question indeed, from twtcp perspective there are
198 * three cases to consider:
200 * #1 tcp_detach is called at tcptw creation time by
201 * tcp_twstart, then do not discard the newly created tcptw
202 * and leave inpcb present until timewait ends
203 * #2 tcp_detach is called at timewait end (or reuse) by
204 * tcp_twclose, then the tcptw has already been discarded
205 * (or reused) and inpcb is freed here
206 * #3 tcp_detach is called() after timewait ends (or reuse)
207 * (e.g. by soclose), then tcptw has already been discarded
208 * (or reused) and inpcb is freed here
210 * In all three cases the tcptw should not be freed here.
212 if (inp->inp_flags & INP_DROPPED) {
213 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
214 "INP_DROPPED && tp != NULL"));
223 * If the connection is not in timewait, we consider two
224 * two conditions: one in which no further processing is
225 * necessary (dropped || embryonic), and one in which TCP is
226 * not yet done, but no longer requires the socket, so the
227 * pcb will persist for the time being.
229 * XXXRW: Does the second case still occur?
231 if (inp->inp_flags & INP_DROPPED ||
232 tp->t_state < TCPS_SYN_SENT) {
244 * pru_detach() detaches the TCP protocol from the socket.
245 * If the protocol state is non-embryonic, then can't
246 * do this directly: have to initiate a pru_disconnect(),
247 * which may finish later; embryonic TCB's can just
251 tcp_usr_detach(struct socket *so)
257 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
258 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
259 INP_INFO_RLOCK(&V_tcbinfo);
263 KASSERT(inp->inp_socket != NULL,
264 ("tcp_usr_detach: inp_socket == NULL"));
267 INP_INFO_RUNLOCK(&V_tcbinfo);
272 * Give the socket an address.
275 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
279 struct tcpcb *tp = NULL;
280 struct sockaddr_in *sinp;
282 sinp = (struct sockaddr_in *)nam;
283 if (nam->sa_len != sizeof (*sinp))
286 * Must check for multicast addresses and disallow binding
289 if (sinp->sin_family == AF_INET &&
290 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
291 return (EAFNOSUPPORT);
295 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
297 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
303 INP_HASH_WLOCK(&V_tcbinfo);
304 error = in_pcbbind(inp, nam, td->td_ucred);
305 INP_HASH_WUNLOCK(&V_tcbinfo);
308 TCP_PROBE2(debug__user, tp, PRU_BIND);
317 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
321 struct tcpcb *tp = NULL;
322 struct sockaddr_in6 *sin6p;
324 sin6p = (struct sockaddr_in6 *)nam;
325 if (nam->sa_len != sizeof (*sin6p))
328 * Must check for multicast addresses and disallow binding
331 if (sin6p->sin6_family == AF_INET6 &&
332 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
333 return (EAFNOSUPPORT);
337 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
339 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
345 INP_HASH_WLOCK(&V_tcbinfo);
346 inp->inp_vflag &= ~INP_IPV4;
347 inp->inp_vflag |= INP_IPV6;
349 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
350 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
351 inp->inp_vflag |= INP_IPV4;
352 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
353 struct sockaddr_in sin;
355 in6_sin6_2_sin(&sin, sin6p);
356 inp->inp_vflag |= INP_IPV4;
357 inp->inp_vflag &= ~INP_IPV6;
358 error = in_pcbbind(inp, (struct sockaddr *)&sin,
360 INP_HASH_WUNLOCK(&V_tcbinfo);
365 error = in6_pcbbind(inp, nam, td->td_ucred);
366 INP_HASH_WUNLOCK(&V_tcbinfo);
369 TCP_PROBE2(debug__user, tp, PRU_BIND);
377 * Prepare to accept connections.
380 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
384 struct tcpcb *tp = NULL;
388 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
390 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
397 error = solisten_proto_check(so);
398 INP_HASH_WLOCK(&V_tcbinfo);
399 if (error == 0 && inp->inp_lport == 0)
400 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
401 INP_HASH_WUNLOCK(&V_tcbinfo);
403 tcp_state_change(tp, TCPS_LISTEN);
404 solisten_proto(so, backlog);
406 if ((so->so_options & SO_NO_OFFLOAD) == 0)
407 tcp_offload_listen_start(tp);
413 if (tp->t_flags & TF_FASTOPEN)
414 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
417 TCPDEBUG2(PRU_LISTEN);
418 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
426 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
430 struct tcpcb *tp = NULL;
434 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
436 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
443 error = solisten_proto_check(so);
444 INP_HASH_WLOCK(&V_tcbinfo);
445 if (error == 0 && inp->inp_lport == 0) {
446 inp->inp_vflag &= ~INP_IPV4;
447 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
448 inp->inp_vflag |= INP_IPV4;
449 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
451 INP_HASH_WUNLOCK(&V_tcbinfo);
453 tcp_state_change(tp, TCPS_LISTEN);
454 solisten_proto(so, backlog);
456 if ((so->so_options & SO_NO_OFFLOAD) == 0)
457 tcp_offload_listen_start(tp);
463 if (tp->t_flags & TF_FASTOPEN)
464 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
467 TCPDEBUG2(PRU_LISTEN);
468 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
476 * Initiate connection to peer.
477 * Create a template for use in transmissions on this connection.
478 * Enter SYN_SENT state, and mark socket as connecting.
479 * Start keep-alive timer, and seed output sequence space.
480 * Send initial segment on connection.
483 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
487 struct tcpcb *tp = NULL;
488 struct sockaddr_in *sinp;
490 sinp = (struct sockaddr_in *)nam;
491 if (nam->sa_len != sizeof (*sinp))
494 * Must disallow TCP ``connections'' to multicast addresses.
496 if (sinp->sin_family == AF_INET
497 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
498 return (EAFNOSUPPORT);
499 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
504 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
506 if (inp->inp_flags & INP_TIMEWAIT) {
510 if (inp->inp_flags & INP_DROPPED) {
511 error = ECONNREFUSED;
516 if ((error = tcp_connect(tp, nam, td)) != 0)
519 if (registered_toedevs > 0 &&
520 (so->so_options & SO_NO_OFFLOAD) == 0 &&
521 (error = tcp_offload_connect(so, nam)) == 0)
524 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
525 error = tp->t_fb->tfb_tcp_output(tp);
527 TCPDEBUG2(PRU_CONNECT);
535 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
539 struct tcpcb *tp = NULL;
540 struct sockaddr_in6 *sin6p;
544 sin6p = (struct sockaddr_in6 *)nam;
545 if (nam->sa_len != sizeof (*sin6p))
548 * Must disallow TCP ``connections'' to multicast addresses.
550 if (sin6p->sin6_family == AF_INET6
551 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
552 return (EAFNOSUPPORT);
555 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
557 if (inp->inp_flags & INP_TIMEWAIT) {
561 if (inp->inp_flags & INP_DROPPED) {
562 error = ECONNREFUSED;
569 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
570 * therefore probably require the hash lock, which isn't held here.
571 * Is this a significant problem?
573 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
574 struct sockaddr_in sin;
576 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
581 in6_sin6_2_sin(&sin, sin6p);
582 inp->inp_vflag |= INP_IPV4;
583 inp->inp_vflag &= ~INP_IPV6;
584 if ((error = prison_remote_ip4(td->td_ucred,
585 &sin.sin_addr)) != 0)
587 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 error = tp->t_fb->tfb_tcp_output(tp);
599 inp->inp_vflag &= ~INP_IPV4;
600 inp->inp_vflag |= INP_IPV6;
601 inp->inp_inc.inc_flags |= INC_ISIPV6;
602 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
604 if ((error = tcp6_connect(tp, nam, td)) != 0)
607 if (registered_toedevs > 0 &&
608 (so->so_options & SO_NO_OFFLOAD) == 0 &&
609 (error = tcp_offload_connect(so, nam)) == 0)
612 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
613 error = tp->t_fb->tfb_tcp_output(tp);
616 TCPDEBUG2(PRU_CONNECT);
617 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
624 * Initiate disconnect from peer.
625 * If connection never passed embryonic stage, just drop;
626 * else if don't need to let data drain, then can just drop anyways,
627 * else have to begin TCP shutdown process: mark socket disconnecting,
628 * drain unread data, state switch to reflect user close, and
629 * send segment (e.g. FIN) to peer. Socket will be really disconnected
630 * when peer sends FIN and acks ours.
632 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
635 tcp_usr_disconnect(struct socket *so)
638 struct tcpcb *tp = NULL;
642 INP_INFO_RLOCK(&V_tcbinfo);
644 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
646 if (inp->inp_flags & INP_TIMEWAIT)
648 if (inp->inp_flags & INP_DROPPED) {
656 TCPDEBUG2(PRU_DISCONNECT);
657 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
659 INP_INFO_RUNLOCK(&V_tcbinfo);
665 * Accept a connection. Essentially all the work is done at higher levels;
666 * just return the address of the peer, storing through addr.
669 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
672 struct inpcb *inp = NULL;
673 struct tcpcb *tp = NULL;
678 if (so->so_state & SS_ISDISCONNECTED)
679 return (ECONNABORTED);
682 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
684 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
685 error = ECONNABORTED;
692 * We inline in_getpeeraddr and COMMON_END here, so that we can
693 * copy the data of interest and defer the malloc until after we
696 port = inp->inp_fport;
697 addr = inp->inp_faddr;
700 TCPDEBUG2(PRU_ACCEPT);
701 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
704 *nam = in_sockaddr(port, &addr);
711 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
713 struct inpcb *inp = NULL;
715 struct tcpcb *tp = NULL;
717 struct in6_addr addr6;
722 if (so->so_state & SS_ISDISCONNECTED)
723 return (ECONNABORTED);
726 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
727 INP_INFO_RLOCK(&V_tcbinfo);
729 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
730 error = ECONNABORTED;
737 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
738 * copy the data of interest and defer the malloc until after we
741 if (inp->inp_vflag & INP_IPV4) {
743 port = inp->inp_fport;
744 addr = inp->inp_faddr;
746 port = inp->inp_fport;
747 addr6 = inp->in6p_faddr;
751 TCPDEBUG2(PRU_ACCEPT);
752 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
754 INP_INFO_RUNLOCK(&V_tcbinfo);
757 *nam = in6_v4mapsin6_sockaddr(port, &addr);
759 *nam = in6_sockaddr(port, &addr6);
766 * Mark the connection as being incapable of further output.
769 tcp_usr_shutdown(struct socket *so)
773 struct tcpcb *tp = NULL;
776 INP_INFO_RLOCK(&V_tcbinfo);
778 KASSERT(inp != NULL, ("inp == NULL"));
780 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
788 if (!(inp->inp_flags & INP_DROPPED))
789 error = tp->t_fb->tfb_tcp_output(tp);
792 TCPDEBUG2(PRU_SHUTDOWN);
793 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
795 INP_INFO_RUNLOCK(&V_tcbinfo);
801 * After a receive, possibly send window update to peer.
804 tcp_usr_rcvd(struct socket *so, int flags)
807 struct tcpcb *tp = NULL;
812 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
814 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
822 * For passively-created TFO connections, don't attempt a window
823 * update while still in SYN_RECEIVED as this may trigger an early
824 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
825 * application response data, or failing that, when the DELACK timer
828 if ((tp->t_flags & TF_FASTOPEN) &&
829 (tp->t_state == TCPS_SYN_RECEIVED))
833 if (tp->t_flags & TF_TOE)
834 tcp_offload_rcvd(tp);
837 tp->t_fb->tfb_tcp_output(tp);
841 TCP_PROBE2(debug__user, tp, PRU_RCVD);
847 * Do a send by putting data in output queue and updating urgent
848 * marker if URG set. Possibly send more data. Unlike the other
849 * pru_*() routines, the mbuf chains are our responsibility. We
850 * must either enqueue them or free them. The other pru_* routines
851 * generally are caller-frees.
854 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
855 struct sockaddr *nam, struct mbuf *control, struct thread *td)
859 struct tcpcb *tp = NULL;
866 * We require the pcbinfo lock if we will close the socket as part of
869 if (flags & PRUS_EOF)
870 INP_INFO_RLOCK(&V_tcbinfo);
872 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
874 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
878 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
879 * for freeing memory.
881 if (m && (flags & PRUS_NOTREADY) == 0)
887 isipv6 = nam && nam->sa_family == AF_INET6;
892 /* TCP doesn't do control messages (rights, creds, etc) */
893 if (control->m_len) {
900 m_freem(control); /* empty control, just free it */
902 if (!(flags & PRUS_OOB)) {
903 sbappendstream(&so->so_snd, m, flags);
904 if (nam && tp->t_state < TCPS_SYN_SENT) {
906 * Do implied connect if not yet connected,
907 * initialize window to default value, and
908 * initialize maxseg using peer's cached MSS.
912 error = tcp6_connect(tp, nam, td);
914 #if defined(INET6) && defined(INET)
918 error = tcp_connect(tp, nam, td);
922 tp->snd_wnd = TTCP_CLIENT_SND_WND;
925 if (flags & PRUS_EOF) {
927 * Close the send side of the connection after
930 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
934 if (!(inp->inp_flags & INP_DROPPED) &&
935 !(flags & PRUS_NOTREADY)) {
936 if (flags & PRUS_MORETOCOME)
937 tp->t_flags |= TF_MORETOCOME;
938 error = tp->t_fb->tfb_tcp_output(tp);
939 if (flags & PRUS_MORETOCOME)
940 tp->t_flags &= ~TF_MORETOCOME;
944 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
946 SOCKBUF_LOCK(&so->so_snd);
947 if (sbspace(&so->so_snd) < -512) {
948 SOCKBUF_UNLOCK(&so->so_snd);
954 * According to RFC961 (Assigned Protocols),
955 * the urgent pointer points to the last octet
956 * of urgent data. We continue, however,
957 * to consider it to indicate the first octet
958 * of data past the urgent section.
959 * Otherwise, snd_up should be one lower.
961 sbappendstream_locked(&so->so_snd, m, flags);
962 SOCKBUF_UNLOCK(&so->so_snd);
963 if (nam && tp->t_state < TCPS_SYN_SENT) {
965 * Do implied connect if not yet connected,
966 * initialize window to default value, and
967 * initialize maxseg using peer's cached MSS.
971 error = tcp6_connect(tp, nam, td);
973 #if defined(INET6) && defined(INET)
977 error = tcp_connect(tp, nam, td);
981 tp->snd_wnd = TTCP_CLIENT_SND_WND;
984 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
985 if (!(flags & PRUS_NOTREADY)) {
986 tp->t_flags |= TF_FORCEDATA;
987 error = tp->t_fb->tfb_tcp_output(tp);
988 tp->t_flags &= ~TF_FORCEDATA;
992 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
993 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
994 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
995 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
997 if (flags & PRUS_EOF)
998 INP_INFO_RUNLOCK(&V_tcbinfo);
1003 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1009 inp = sotoinpcb(so);
1011 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1013 for (int i = 0; i < count; i++)
1015 return (ECONNRESET);
1017 tp = intotcpcb(inp);
1019 SOCKBUF_LOCK(&so->so_snd);
1020 error = sbready(&so->so_snd, m, count);
1021 SOCKBUF_UNLOCK(&so->so_snd);
1023 error = tp->t_fb->tfb_tcp_output(tp);
1030 * Abort the TCP. Drop the connection abruptly.
1033 tcp_usr_abort(struct socket *so)
1036 struct tcpcb *tp = NULL;
1039 inp = sotoinpcb(so);
1040 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1042 INP_INFO_RLOCK(&V_tcbinfo);
1044 KASSERT(inp->inp_socket != NULL,
1045 ("tcp_usr_abort: inp_socket == NULL"));
1048 * If we still have full TCP state, and we're not dropped, drop.
1050 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1051 !(inp->inp_flags & INP_DROPPED)) {
1052 tp = intotcpcb(inp);
1054 tcp_drop(tp, ECONNABORTED);
1055 TCPDEBUG2(PRU_ABORT);
1056 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1058 if (!(inp->inp_flags & INP_DROPPED)) {
1060 so->so_state |= SS_PROTOREF;
1062 inp->inp_flags |= INP_SOCKREF;
1065 INP_INFO_RUNLOCK(&V_tcbinfo);
1069 * TCP socket is closed. Start friendly disconnect.
1072 tcp_usr_close(struct socket *so)
1075 struct tcpcb *tp = NULL;
1078 inp = sotoinpcb(so);
1079 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1081 INP_INFO_RLOCK(&V_tcbinfo);
1083 KASSERT(inp->inp_socket != NULL,
1084 ("tcp_usr_close: inp_socket == NULL"));
1087 * If we still have full TCP state, and we're not dropped, initiate
1090 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1091 !(inp->inp_flags & INP_DROPPED)) {
1092 tp = intotcpcb(inp);
1095 TCPDEBUG2(PRU_CLOSE);
1096 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1098 if (!(inp->inp_flags & INP_DROPPED)) {
1100 so->so_state |= SS_PROTOREF;
1102 inp->inp_flags |= INP_SOCKREF;
1105 INP_INFO_RUNLOCK(&V_tcbinfo);
1109 * Receive out-of-band data.
1112 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1116 struct tcpcb *tp = NULL;
1119 inp = sotoinpcb(so);
1120 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1122 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1126 tp = intotcpcb(inp);
1128 if ((so->so_oobmark == 0 &&
1129 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1130 so->so_options & SO_OOBINLINE ||
1131 tp->t_oobflags & TCPOOB_HADDATA) {
1135 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1136 error = EWOULDBLOCK;
1140 *mtod(m, caddr_t) = tp->t_iobc;
1141 if ((flags & MSG_PEEK) == 0)
1142 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1145 TCPDEBUG2(PRU_RCVOOB);
1146 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1152 struct pr_usrreqs tcp_usrreqs = {
1153 .pru_abort = tcp_usr_abort,
1154 .pru_accept = tcp_usr_accept,
1155 .pru_attach = tcp_usr_attach,
1156 .pru_bind = tcp_usr_bind,
1157 .pru_connect = tcp_usr_connect,
1158 .pru_control = in_control,
1159 .pru_detach = tcp_usr_detach,
1160 .pru_disconnect = tcp_usr_disconnect,
1161 .pru_listen = tcp_usr_listen,
1162 .pru_peeraddr = in_getpeeraddr,
1163 .pru_rcvd = tcp_usr_rcvd,
1164 .pru_rcvoob = tcp_usr_rcvoob,
1165 .pru_send = tcp_usr_send,
1166 .pru_ready = tcp_usr_ready,
1167 .pru_shutdown = tcp_usr_shutdown,
1168 .pru_sockaddr = in_getsockaddr,
1169 .pru_sosetlabel = in_pcbsosetlabel,
1170 .pru_close = tcp_usr_close,
1175 struct pr_usrreqs tcp6_usrreqs = {
1176 .pru_abort = tcp_usr_abort,
1177 .pru_accept = tcp6_usr_accept,
1178 .pru_attach = tcp_usr_attach,
1179 .pru_bind = tcp6_usr_bind,
1180 .pru_connect = tcp6_usr_connect,
1181 .pru_control = in6_control,
1182 .pru_detach = tcp_usr_detach,
1183 .pru_disconnect = tcp_usr_disconnect,
1184 .pru_listen = tcp6_usr_listen,
1185 .pru_peeraddr = in6_mapped_peeraddr,
1186 .pru_rcvd = tcp_usr_rcvd,
1187 .pru_rcvoob = tcp_usr_rcvoob,
1188 .pru_send = tcp_usr_send,
1189 .pru_ready = tcp_usr_ready,
1190 .pru_shutdown = tcp_usr_shutdown,
1191 .pru_sockaddr = in6_mapped_sockaddr,
1192 .pru_sosetlabel = in_pcbsosetlabel,
1193 .pru_close = tcp_usr_close,
1199 * Common subroutine to open a TCP connection to remote host specified
1200 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1201 * port number if needed. Call in_pcbconnect_setup to do the routing and
1202 * to choose a local host address (interface). If there is an existing
1203 * incarnation of the same connection in TIME-WAIT state and if the remote
1204 * host was sending CC options and if the connection duration was < MSL, then
1205 * truncate the previous TIME-WAIT state and proceed.
1206 * Initialize connection parameters and enter SYN-SENT state.
1209 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1211 struct inpcb *inp = tp->t_inpcb, *oinp;
1212 struct socket *so = inp->inp_socket;
1213 struct in_addr laddr;
1217 INP_WLOCK_ASSERT(inp);
1218 INP_HASH_WLOCK(&V_tcbinfo);
1220 if (inp->inp_lport == 0) {
1221 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1227 * Cannot simply call in_pcbconnect, because there might be an
1228 * earlier incarnation of this same connection still in
1229 * TIME_WAIT state, creating an ADDRINUSE error.
1231 laddr = inp->inp_laddr;
1232 lport = inp->inp_lport;
1233 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1234 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1235 if (error && oinp == NULL)
1241 inp->inp_laddr = laddr;
1243 INP_HASH_WUNLOCK(&V_tcbinfo);
1246 * Compute window scaling to request:
1247 * Scale to fit into sweet spot. See tcp_syncache.c.
1248 * XXX: This should move to tcp_output().
1250 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1251 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1252 tp->request_r_scale++;
1255 TCPSTAT_INC(tcps_connattempt);
1256 tcp_state_change(tp, TCPS_SYN_SENT);
1257 tp->iss = tcp_new_isn(tp);
1258 tcp_sendseqinit(tp);
1263 INP_HASH_WUNLOCK(&V_tcbinfo);
1270 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1272 struct inpcb *inp = tp->t_inpcb;
1275 INP_WLOCK_ASSERT(inp);
1276 INP_HASH_WLOCK(&V_tcbinfo);
1278 if (inp->inp_lport == 0) {
1279 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1283 error = in6_pcbconnect(inp, nam, td->td_ucred);
1286 INP_HASH_WUNLOCK(&V_tcbinfo);
1288 /* Compute window scaling to request. */
1289 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1290 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1291 tp->request_r_scale++;
1293 soisconnecting(inp->inp_socket);
1294 TCPSTAT_INC(tcps_connattempt);
1295 tcp_state_change(tp, TCPS_SYN_SENT);
1296 tp->iss = tcp_new_isn(tp);
1297 tcp_sendseqinit(tp);
1302 INP_HASH_WUNLOCK(&V_tcbinfo);
1308 * Export TCP internal state information via a struct tcp_info, based on the
1309 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1310 * (TCP state machine, etc). We export all information using FreeBSD-native
1311 * constants -- for example, the numeric values for tcpi_state will differ
1315 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1318 INP_WLOCK_ASSERT(tp->t_inpcb);
1319 bzero(ti, sizeof(*ti));
1321 ti->tcpi_state = tp->t_state;
1322 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1323 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1324 if (tp->t_flags & TF_SACK_PERMIT)
1325 ti->tcpi_options |= TCPI_OPT_SACK;
1326 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1327 ti->tcpi_options |= TCPI_OPT_WSCALE;
1328 ti->tcpi_snd_wscale = tp->snd_scale;
1329 ti->tcpi_rcv_wscale = tp->rcv_scale;
1332 ti->tcpi_rto = tp->t_rxtcur * tick;
1333 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1334 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1335 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1337 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1338 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1341 * FreeBSD-specific extension fields for tcp_info.
1343 ti->tcpi_rcv_space = tp->rcv_wnd;
1344 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1345 ti->tcpi_snd_wnd = tp->snd_wnd;
1346 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1347 ti->tcpi_snd_nxt = tp->snd_nxt;
1348 ti->tcpi_snd_mss = tp->t_maxseg;
1349 ti->tcpi_rcv_mss = tp->t_maxseg;
1350 if (tp->t_flags & TF_TOE)
1351 ti->tcpi_options |= TCPI_OPT_TOE;
1352 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1353 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1354 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1358 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1359 * socket option arguments. When it re-acquires the lock after the copy, it
1360 * has to revalidate that the connection is still valid for the socket
1363 #define INP_WLOCK_RECHECK(inp) do { \
1365 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1367 return (ECONNRESET); \
1369 tp = intotcpcb(inp); \
1373 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1378 struct tcp_function_block *blk;
1379 struct tcp_function_set fsn;
1382 inp = sotoinpcb(so);
1383 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1385 if (sopt->sopt_level != IPPROTO_TCP) {
1387 if (inp->inp_vflag & INP_IPV6PROTO) {
1389 error = ip6_ctloutput(so, sopt);
1392 #if defined(INET6) && defined(INET)
1398 error = ip_ctloutput(so, sopt);
1403 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1405 return (ECONNRESET);
1407 tp = intotcpcb(inp);
1409 * Protect the TCP option TCP_FUNCTION_BLK so
1410 * that a sub-function can *never* overwrite this.
1412 if ((sopt->sopt_dir == SOPT_SET) &&
1413 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1415 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1419 INP_WLOCK_RECHECK(inp);
1420 if (tp->t_state != TCPS_CLOSED) {
1422 * The user has advanced the state
1423 * past the initial point, we can't
1424 * switch since we are down the road
1425 * and a new set of functions may
1426 * not be compatibile.
1431 blk = find_and_ref_tcp_functions(&fsn);
1436 if (tp->t_fb != blk) {
1437 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1438 refcount_release(&blk->tfb_refcnt);
1443 * Release the old refcnt, the
1444 * lookup acquires a ref on the
1447 if (tp->t_fb->tfb_tcp_fb_fini)
1448 (*tp->t_fb->tfb_tcp_fb_fini)(tp);
1449 refcount_release(&tp->t_fb->tfb_refcnt);
1451 if (tp->t_fb->tfb_tcp_fb_init) {
1452 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1456 if (tp->t_flags & TF_TOE) {
1457 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1463 } else if ((sopt->sopt_dir == SOPT_GET) &&
1464 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1465 strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
1466 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1468 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1471 /* Pass in the INP locked, called must unlock it */
1472 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1476 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1478 int error, opt, optval;
1481 struct cc_algo *algo;
1484 switch (sopt->sopt_dir) {
1486 switch (sopt->sopt_name) {
1487 #ifdef TCP_SIGNATURE
1490 error = sooptcopyin(sopt, &optval, sizeof optval,
1495 INP_WLOCK_RECHECK(inp);
1497 tp->t_flags |= TF_SIGNATURE;
1499 tp->t_flags &= ~TF_SIGNATURE;
1500 goto unlock_and_done;
1501 #endif /* TCP_SIGNATURE */
1506 error = sooptcopyin(sopt, &optval, sizeof optval,
1511 INP_WLOCK_RECHECK(inp);
1512 switch (sopt->sopt_name) {
1520 opt = 0; /* dead code to fool gcc */
1527 tp->t_flags &= ~opt;
1530 if (tp->t_flags & TF_TOE) {
1531 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1540 error = sooptcopyin(sopt, &optval, sizeof optval,
1545 INP_WLOCK_RECHECK(inp);
1547 tp->t_flags |= TF_NOPUSH;
1548 else if (tp->t_flags & TF_NOPUSH) {
1549 tp->t_flags &= ~TF_NOPUSH;
1550 if (TCPS_HAVEESTABLISHED(tp->t_state))
1551 error = tp->t_fb->tfb_tcp_output(tp);
1553 goto unlock_and_done;
1557 error = sooptcopyin(sopt, &optval, sizeof optval,
1562 INP_WLOCK_RECHECK(inp);
1563 if (optval > 0 && optval <= tp->t_maxseg &&
1564 optval + 40 >= V_tcp_minmss)
1565 tp->t_maxseg = optval;
1568 goto unlock_and_done;
1575 case TCP_CONGESTION:
1577 buf = malloc(TCP_CA_NAME_MAX, M_TEMP, M_WAITOK|M_ZERO);
1578 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX, 1);
1584 STAILQ_FOREACH(algo, &cc_list, entries)
1585 if (strncmp(buf, algo->name,
1586 TCP_CA_NAME_MAX) == 0)
1594 INP_WLOCK_RECHECK(inp);
1596 * We hold a write lock over the tcb so it's safe to
1597 * do these things without ordering concerns.
1599 if (CC_ALGO(tp)->cb_destroy != NULL)
1600 CC_ALGO(tp)->cb_destroy(tp->ccv);
1603 * If something goes pear shaped initialising the new
1604 * algo, fall back to newreno (which does not
1605 * require initialisation).
1607 if (algo->cb_init != NULL &&
1608 algo->cb_init(tp->ccv) != 0) {
1609 CC_ALGO(tp) = &newreno_cc_algo;
1611 * The only reason init should fail is
1612 * because of malloc.
1623 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1627 if (ui > (UINT_MAX / hz)) {
1633 INP_WLOCK_RECHECK(inp);
1634 switch (sopt->sopt_name) {
1636 tp->t_keepidle = ui;
1638 * XXX: better check current remaining
1639 * timeout and "merge" it with new value.
1641 if ((tp->t_state > TCPS_LISTEN) &&
1642 (tp->t_state <= TCPS_CLOSING))
1643 tcp_timer_activate(tp, TT_KEEP,
1647 tp->t_keepintvl = ui;
1648 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1649 (TP_MAXIDLE(tp) > 0))
1650 tcp_timer_activate(tp, TT_2MSL,
1654 tp->t_keepinit = ui;
1655 if (tp->t_state == TCPS_SYN_RECEIVED ||
1656 tp->t_state == TCPS_SYN_SENT)
1657 tcp_timer_activate(tp, TT_KEEP,
1661 goto unlock_and_done;
1665 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1669 INP_WLOCK_RECHECK(inp);
1671 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1672 (TP_MAXIDLE(tp) > 0))
1673 tcp_timer_activate(tp, TT_2MSL,
1675 goto unlock_and_done;
1681 error = sooptcopyin(sopt, &optval, sizeof optval,
1686 INP_WLOCK_RECHECK(inp);
1688 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1689 &(tp->t_outpkts) : &(tp->t_inpkts),
1693 goto unlock_and_done;
1699 if (!V_tcp_fastopen_enabled)
1702 error = sooptcopyin(sopt, &optval, sizeof optval,
1707 INP_WLOCK_RECHECK(inp);
1709 tp->t_flags |= TF_FASTOPEN;
1710 if ((tp->t_state == TCPS_LISTEN) &&
1711 (tp->t_tfo_pending == NULL))
1713 tcp_fastopen_alloc_counter();
1715 tp->t_flags &= ~TF_FASTOPEN;
1716 goto unlock_and_done;
1721 error = ENOPROTOOPT;
1727 tp = intotcpcb(inp);
1728 switch (sopt->sopt_name) {
1729 #ifdef TCP_SIGNATURE
1731 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1733 error = sooptcopyout(sopt, &optval, sizeof optval);
1738 optval = tp->t_flags & TF_NODELAY;
1740 error = sooptcopyout(sopt, &optval, sizeof optval);
1743 optval = tp->t_maxseg;
1745 error = sooptcopyout(sopt, &optval, sizeof optval);
1748 optval = tp->t_flags & TF_NOOPT;
1750 error = sooptcopyout(sopt, &optval, sizeof optval);
1753 optval = tp->t_flags & TF_NOPUSH;
1755 error = sooptcopyout(sopt, &optval, sizeof optval);
1758 tcp_fill_info(tp, &ti);
1760 error = sooptcopyout(sopt, &ti, sizeof ti);
1762 case TCP_CONGESTION:
1764 error = sooptcopyout(sopt, CC_ALGO(tp)->name,
1771 switch (sopt->sopt_name) {
1773 ui = tp->t_keepidle / hz;
1776 ui = tp->t_keepintvl / hz;
1779 ui = tp->t_keepinit / hz;
1786 error = sooptcopyout(sopt, &ui, sizeof(ui));
1791 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1792 &(tp->t_outpkts) : &(tp->t_inpkts));
1794 error = sooptcopyout(sopt, &optval, sizeof optval);
1800 optval = tp->t_flags & TF_FASTOPEN;
1802 error = sooptcopyout(sopt, &optval, sizeof optval);
1807 error = ENOPROTOOPT;
1814 #undef INP_WLOCK_RECHECK
1817 * Attach TCP protocol to socket, allocating
1818 * internet protocol control block, tcp control block,
1819 * bufer space, and entering LISTEN state if to accept connections.
1822 tcp_attach(struct socket *so)
1828 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1829 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1833 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1834 so->so_snd.sb_flags |= SB_AUTOSIZE;
1835 INP_INFO_RLOCK(&V_tcbinfo);
1836 error = in_pcballoc(so, &V_tcbinfo);
1838 INP_INFO_RUNLOCK(&V_tcbinfo);
1841 inp = sotoinpcb(so);
1843 if (inp->inp_vflag & INP_IPV6PROTO) {
1844 inp->inp_vflag |= INP_IPV6;
1845 inp->in6p_hops = -1; /* use kernel default */
1849 inp->inp_vflag |= INP_IPV4;
1850 tp = tcp_newtcpcb(inp);
1854 INP_INFO_RUNLOCK(&V_tcbinfo);
1857 tp->t_state = TCPS_CLOSED;
1859 INP_INFO_RUNLOCK(&V_tcbinfo);
1864 * Initiate (or continue) disconnect.
1865 * If embryonic state, just send reset (once).
1866 * If in ``let data drain'' option and linger null, just drop.
1867 * Otherwise (hard), mark socket disconnecting and drop
1868 * current input data; switch states based on user close, and
1869 * send segment to peer (with FIN).
1872 tcp_disconnect(struct tcpcb *tp)
1874 struct inpcb *inp = tp->t_inpcb;
1875 struct socket *so = inp->inp_socket;
1877 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1878 INP_WLOCK_ASSERT(inp);
1881 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1882 * socket is still open.
1884 if (tp->t_state < TCPS_ESTABLISHED) {
1887 ("tcp_disconnect: tcp_close() returned NULL"));
1888 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1889 tp = tcp_drop(tp, 0);
1891 ("tcp_disconnect: tcp_drop() returned NULL"));
1893 soisdisconnecting(so);
1894 sbflush(&so->so_rcv);
1896 if (!(inp->inp_flags & INP_DROPPED))
1897 tp->t_fb->tfb_tcp_output(tp);
1902 * User issued close, and wish to trail through shutdown states:
1903 * if never received SYN, just forget it. If got a SYN from peer,
1904 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1905 * If already got a FIN from peer, then almost done; go to LAST_ACK
1906 * state. In all other cases, have already sent FIN to peer (e.g.
1907 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1908 * for peer to send FIN or not respond to keep-alives, etc.
1909 * We can let the user exit from the close as soon as the FIN is acked.
1912 tcp_usrclosed(struct tcpcb *tp)
1915 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1916 INP_WLOCK_ASSERT(tp->t_inpcb);
1918 switch (tp->t_state) {
1921 tcp_offload_listen_stop(tp);
1923 tcp_state_change(tp, TCPS_CLOSED);
1928 * tcp_close() should never return NULL here as the socket is
1932 ("tcp_usrclosed: tcp_close() returned NULL"));
1936 case TCPS_SYN_RECEIVED:
1937 tp->t_flags |= TF_NEEDFIN;
1940 case TCPS_ESTABLISHED:
1941 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1944 case TCPS_CLOSE_WAIT:
1945 tcp_state_change(tp, TCPS_LAST_ACK);
1948 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1949 soisdisconnected(tp->t_inpcb->inp_socket);
1950 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1951 if (tp->t_state == TCPS_FIN_WAIT_2) {
1954 timeout = (tcp_fast_finwait2_recycle) ?
1955 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1956 tcp_timer_activate(tp, TT_2MSL, timeout);
1963 db_print_indent(int indent)
1967 for (i = 0; i < indent; i++)
1972 db_print_tstate(int t_state)
1977 db_printf("TCPS_CLOSED");
1981 db_printf("TCPS_LISTEN");
1985 db_printf("TCPS_SYN_SENT");
1988 case TCPS_SYN_RECEIVED:
1989 db_printf("TCPS_SYN_RECEIVED");
1992 case TCPS_ESTABLISHED:
1993 db_printf("TCPS_ESTABLISHED");
1996 case TCPS_CLOSE_WAIT:
1997 db_printf("TCPS_CLOSE_WAIT");
2000 case TCPS_FIN_WAIT_1:
2001 db_printf("TCPS_FIN_WAIT_1");
2005 db_printf("TCPS_CLOSING");
2009 db_printf("TCPS_LAST_ACK");
2012 case TCPS_FIN_WAIT_2:
2013 db_printf("TCPS_FIN_WAIT_2");
2016 case TCPS_TIME_WAIT:
2017 db_printf("TCPS_TIME_WAIT");
2021 db_printf("unknown");
2027 db_print_tflags(u_int t_flags)
2032 if (t_flags & TF_ACKNOW) {
2033 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2036 if (t_flags & TF_DELACK) {
2037 db_printf("%sTF_DELACK", comma ? ", " : "");
2040 if (t_flags & TF_NODELAY) {
2041 db_printf("%sTF_NODELAY", comma ? ", " : "");
2044 if (t_flags & TF_NOOPT) {
2045 db_printf("%sTF_NOOPT", comma ? ", " : "");
2048 if (t_flags & TF_SENTFIN) {
2049 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2052 if (t_flags & TF_REQ_SCALE) {
2053 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2056 if (t_flags & TF_RCVD_SCALE) {
2057 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2060 if (t_flags & TF_REQ_TSTMP) {
2061 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2064 if (t_flags & TF_RCVD_TSTMP) {
2065 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2068 if (t_flags & TF_SACK_PERMIT) {
2069 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2072 if (t_flags & TF_NEEDSYN) {
2073 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2076 if (t_flags & TF_NEEDFIN) {
2077 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2080 if (t_flags & TF_NOPUSH) {
2081 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2084 if (t_flags & TF_MORETOCOME) {
2085 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2088 if (t_flags & TF_LQ_OVERFLOW) {
2089 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2092 if (t_flags & TF_LASTIDLE) {
2093 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2096 if (t_flags & TF_RXWIN0SENT) {
2097 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2100 if (t_flags & TF_FASTRECOVERY) {
2101 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2104 if (t_flags & TF_CONGRECOVERY) {
2105 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2108 if (t_flags & TF_WASFRECOVERY) {
2109 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2112 if (t_flags & TF_SIGNATURE) {
2113 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2116 if (t_flags & TF_FORCEDATA) {
2117 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2120 if (t_flags & TF_TSO) {
2121 db_printf("%sTF_TSO", comma ? ", " : "");
2124 if (t_flags & TF_ECN_PERMIT) {
2125 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2128 if (t_flags & TF_FASTOPEN) {
2129 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2135 db_print_toobflags(char t_oobflags)
2140 if (t_oobflags & TCPOOB_HAVEDATA) {
2141 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2144 if (t_oobflags & TCPOOB_HADDATA) {
2145 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2151 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2154 db_print_indent(indent);
2155 db_printf("%s at %p\n", name, tp);
2159 db_print_indent(indent);
2160 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2161 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2163 db_print_indent(indent);
2164 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2165 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2167 db_print_indent(indent);
2168 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2169 &tp->t_timers->tt_delack, tp->t_inpcb);
2171 db_print_indent(indent);
2172 db_printf("t_state: %d (", tp->t_state);
2173 db_print_tstate(tp->t_state);
2176 db_print_indent(indent);
2177 db_printf("t_flags: 0x%x (", tp->t_flags);
2178 db_print_tflags(tp->t_flags);
2181 db_print_indent(indent);
2182 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2183 tp->snd_una, tp->snd_max, tp->snd_nxt);
2185 db_print_indent(indent);
2186 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2187 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2189 db_print_indent(indent);
2190 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2191 tp->iss, tp->irs, tp->rcv_nxt);
2193 db_print_indent(indent);
2194 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2195 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2197 db_print_indent(indent);
2198 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2199 tp->snd_wnd, tp->snd_cwnd);
2201 db_print_indent(indent);
2202 db_printf("snd_ssthresh: %lu snd_recover: "
2203 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2205 db_print_indent(indent);
2206 db_printf("t_rcvtime: %u t_startime: %u\n",
2207 tp->t_rcvtime, tp->t_starttime);
2209 db_print_indent(indent);
2210 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2211 tp->t_rtttime, tp->t_rtseq);
2213 db_print_indent(indent);
2214 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2215 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2217 db_print_indent(indent);
2218 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2219 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2222 db_print_indent(indent);
2223 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2224 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2226 db_print_indent(indent);
2227 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2228 db_print_toobflags(tp->t_oobflags);
2229 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2231 db_print_indent(indent);
2232 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2233 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2235 db_print_indent(indent);
2236 db_printf("ts_recent: %u ts_recent_age: %u\n",
2237 tp->ts_recent, tp->ts_recent_age);
2239 db_print_indent(indent);
2240 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2241 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2243 db_print_indent(indent);
2244 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2245 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2246 tp->snd_recover_prev, tp->t_badrxtwin);
2248 db_print_indent(indent);
2249 db_printf("snd_numholes: %d snd_holes first: %p\n",
2250 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2252 db_print_indent(indent);
2253 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2254 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2256 /* Skip sackblks, sackhint. */
2258 db_print_indent(indent);
2259 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2260 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2263 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2268 db_printf("usage: show tcpcb <addr>\n");
2271 tp = (struct tcpcb *)addr;
2273 db_print_tcpcb(tp, "tcpcb", 0);