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/kernel.h>
51 #include <sys/sysctl.h>
54 #include <sys/domain.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/protosw.h>
67 #include <net/route.h>
70 #include <netinet/cc.h>
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/in6_pcb.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/scope6_var.h>
83 #include <netinet/tcp_fastopen.h>
85 #include <netinet/tcp_fsm.h>
86 #include <netinet/tcp_seq.h>
87 #include <netinet/tcp_timer.h>
88 #include <netinet/tcp_var.h>
89 #include <netinet/tcpip.h>
91 #include <netinet/tcp_debug.h>
94 #include <netinet/tcp_offload.h>
98 * TCP protocol interface to socket abstraction.
100 static int tcp_attach(struct socket *);
102 static int tcp_connect(struct tcpcb *, struct sockaddr *,
106 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
109 static void tcp_disconnect(struct tcpcb *);
110 static void tcp_usrclosed(struct tcpcb *);
111 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
114 #define TCPDEBUG0 int ostate = 0
115 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
116 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
117 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
121 #define TCPDEBUG2(req)
125 * TCP attaches to socket via pru_attach(), reserving space,
126 * and an internet control block.
129 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
132 struct tcpcb *tp = NULL;
137 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
140 error = tcp_attach(so);
144 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
145 so->so_linger = TCP_LINGERTIME;
150 TCPDEBUG2(PRU_ATTACH);
155 * tcp_detach is called when the socket layer loses its final reference
156 * to the socket, be it a file descriptor reference, a reference from TCP,
157 * etc. At this point, there is only one case in which we will keep around
158 * inpcb state: time wait.
160 * This function can probably be re-absorbed back into tcp_usr_detach() now
161 * that there is a single detach path.
164 tcp_detach(struct socket *so, struct inpcb *inp)
168 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
169 INP_WLOCK_ASSERT(inp);
171 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
172 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
176 if (inp->inp_flags & INP_TIMEWAIT) {
178 * There are two cases to handle: one in which the time wait
179 * state is being discarded (INP_DROPPED), and one in which
180 * this connection will remain in timewait. In the former,
181 * it is time to discard all state (except tcptw, which has
182 * already been discarded by the timewait close code, which
183 * should be further up the call stack somewhere). In the
184 * latter case, we detach from the socket, but leave the pcb
185 * present until timewait ends.
187 * XXXRW: Would it be cleaner to free the tcptw here?
189 * Astute question indeed, from twtcp perspective there are
190 * three cases to consider:
192 * #1 tcp_detach is called at tcptw creation time by
193 * tcp_twstart, then do not discard the newly created tcptw
194 * and leave inpcb present until timewait ends
195 * #2 tcp_detach is called at timewait end (or reuse) by
196 * tcp_twclose, then the tcptw has already been discarded
197 * and inpcb is freed here
198 * #3 tcp_detach is called() after timewait ends (or reuse)
199 * (e.g. by soclose), then tcptw has already been discarded
200 * and inpcb is freed here
202 * In all three cases the tcptw should not be freed here.
204 if (inp->inp_flags & INP_DROPPED) {
205 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
206 "INP_DROPPED && tp != NULL"));
215 * If the connection is not in timewait, we consider two
216 * two conditions: one in which no further processing is
217 * necessary (dropped || embryonic), and one in which TCP is
218 * not yet done, but no longer requires the socket, so the
219 * pcb will persist for the time being.
221 * XXXRW: Does the second case still occur?
223 if (inp->inp_flags & INP_DROPPED ||
224 tp->t_state < TCPS_SYN_SENT) {
236 * pru_detach() detaches the TCP protocol from the socket.
237 * If the protocol state is non-embryonic, then can't
238 * do this directly: have to initiate a pru_disconnect(),
239 * which may finish later; embryonic TCB's can just
243 tcp_usr_detach(struct socket *so)
248 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
249 INP_INFO_WLOCK(&V_tcbinfo);
251 KASSERT(inp->inp_socket != NULL,
252 ("tcp_usr_detach: inp_socket == NULL"));
254 INP_INFO_WUNLOCK(&V_tcbinfo);
259 * Give the socket an address.
262 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
266 struct tcpcb *tp = NULL;
267 struct sockaddr_in *sinp;
269 sinp = (struct sockaddr_in *)nam;
270 if (nam->sa_len != sizeof (*sinp))
273 * Must check for multicast addresses and disallow binding
276 if (sinp->sin_family == AF_INET &&
277 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
278 return (EAFNOSUPPORT);
282 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
284 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
290 INP_HASH_WLOCK(&V_tcbinfo);
291 error = in_pcbbind(inp, nam, td->td_ucred);
292 INP_HASH_WUNLOCK(&V_tcbinfo);
303 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
307 struct tcpcb *tp = NULL;
308 struct sockaddr_in6 *sin6p;
310 sin6p = (struct sockaddr_in6 *)nam;
311 if (nam->sa_len != sizeof (*sin6p))
314 * Must check for multicast addresses and disallow binding
317 if (sin6p->sin6_family == AF_INET6 &&
318 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
319 return (EAFNOSUPPORT);
323 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
325 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
331 INP_HASH_WLOCK(&V_tcbinfo);
332 inp->inp_vflag &= ~INP_IPV4;
333 inp->inp_vflag |= INP_IPV6;
335 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
336 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
337 inp->inp_vflag |= INP_IPV4;
338 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
339 struct sockaddr_in sin;
341 in6_sin6_2_sin(&sin, sin6p);
342 inp->inp_vflag |= INP_IPV4;
343 inp->inp_vflag &= ~INP_IPV6;
344 error = in_pcbbind(inp, (struct sockaddr *)&sin,
346 INP_HASH_WUNLOCK(&V_tcbinfo);
351 error = in6_pcbbind(inp, nam, td->td_ucred);
352 INP_HASH_WUNLOCK(&V_tcbinfo);
362 * Prepare to accept connections.
365 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
369 struct tcpcb *tp = NULL;
373 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
375 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
382 error = solisten_proto_check(so);
383 INP_HASH_WLOCK(&V_tcbinfo);
384 if (error == 0 && inp->inp_lport == 0)
385 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
386 INP_HASH_WUNLOCK(&V_tcbinfo);
388 tcp_state_change(tp, TCPS_LISTEN);
389 solisten_proto(so, backlog);
391 if ((so->so_options & SO_NO_OFFLOAD) == 0)
392 tcp_offload_listen_start(tp);
398 if (tp->t_flags & TF_FASTOPEN)
399 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
402 TCPDEBUG2(PRU_LISTEN);
410 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
414 struct tcpcb *tp = NULL;
418 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
420 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
427 error = solisten_proto_check(so);
428 INP_HASH_WLOCK(&V_tcbinfo);
429 if (error == 0 && inp->inp_lport == 0) {
430 inp->inp_vflag &= ~INP_IPV4;
431 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
432 inp->inp_vflag |= INP_IPV4;
433 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
435 INP_HASH_WUNLOCK(&V_tcbinfo);
437 tcp_state_change(tp, TCPS_LISTEN);
438 solisten_proto(so, backlog);
440 if ((so->so_options & SO_NO_OFFLOAD) == 0)
441 tcp_offload_listen_start(tp);
447 if (tp->t_flags & TF_FASTOPEN)
448 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
451 TCPDEBUG2(PRU_LISTEN);
459 * Initiate connection to peer.
460 * Create a template for use in transmissions on this connection.
461 * Enter SYN_SENT state, and mark socket as connecting.
462 * Start keep-alive timer, and seed output sequence space.
463 * Send initial segment on connection.
466 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
470 struct tcpcb *tp = NULL;
471 struct sockaddr_in *sinp;
473 sinp = (struct sockaddr_in *)nam;
474 if (nam->sa_len != sizeof (*sinp))
477 * Must disallow TCP ``connections'' to multicast addresses.
479 if (sinp->sin_family == AF_INET
480 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
481 return (EAFNOSUPPORT);
482 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
487 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
489 if (inp->inp_flags & INP_TIMEWAIT) {
493 if (inp->inp_flags & INP_DROPPED) {
494 error = ECONNREFUSED;
499 if ((error = tcp_connect(tp, nam, td)) != 0)
502 if (registered_toedevs > 0 &&
503 (so->so_options & SO_NO_OFFLOAD) == 0 &&
504 (error = tcp_offload_connect(so, nam)) == 0)
507 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
508 error = tcp_output(tp);
510 TCPDEBUG2(PRU_CONNECT);
518 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
522 struct tcpcb *tp = NULL;
523 struct sockaddr_in6 *sin6p;
527 sin6p = (struct sockaddr_in6 *)nam;
528 if (nam->sa_len != sizeof (*sin6p))
531 * Must disallow TCP ``connections'' to multicast addresses.
533 if (sin6p->sin6_family == AF_INET6
534 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
535 return (EAFNOSUPPORT);
538 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
540 if (inp->inp_flags & INP_TIMEWAIT) {
544 if (inp->inp_flags & INP_DROPPED) {
545 error = ECONNREFUSED;
552 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
553 * therefore probably require the hash lock, which isn't held here.
554 * Is this a significant problem?
556 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
557 struct sockaddr_in sin;
559 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
564 in6_sin6_2_sin(&sin, sin6p);
565 inp->inp_vflag |= INP_IPV4;
566 inp->inp_vflag &= ~INP_IPV6;
567 if ((error = prison_remote_ip4(td->td_ucred,
568 &sin.sin_addr)) != 0)
570 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
573 if (registered_toedevs > 0 &&
574 (so->so_options & SO_NO_OFFLOAD) == 0 &&
575 (error = tcp_offload_connect(so, nam)) == 0)
578 error = tcp_output(tp);
582 inp->inp_vflag &= ~INP_IPV4;
583 inp->inp_vflag |= INP_IPV6;
584 inp->inp_inc.inc_flags |= INC_ISIPV6;
585 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
587 if ((error = tcp6_connect(tp, nam, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
596 error = tcp_output(tp);
599 TCPDEBUG2(PRU_CONNECT);
606 * Initiate disconnect from peer.
607 * If connection never passed embryonic stage, just drop;
608 * else if don't need to let data drain, then can just drop anyways,
609 * else have to begin TCP shutdown process: mark socket disconnecting,
610 * drain unread data, state switch to reflect user close, and
611 * send segment (e.g. FIN) to peer. Socket will be really disconnected
612 * when peer sends FIN and acks ours.
614 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
617 tcp_usr_disconnect(struct socket *so)
620 struct tcpcb *tp = NULL;
624 INP_INFO_WLOCK(&V_tcbinfo);
626 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
628 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
636 TCPDEBUG2(PRU_DISCONNECT);
638 INP_INFO_WUNLOCK(&V_tcbinfo);
644 * Accept a connection. Essentially all the work is done at higher levels;
645 * just return the address of the peer, storing through addr.
647 * The rationale for acquiring the tcbinfo lock here is somewhat complicated,
648 * and is described in detail in the commit log entry for r175612. Acquiring
649 * it delays an accept(2) racing with sonewconn(), which inserts the socket
650 * before the inpcb address/port fields are initialized. A better fix would
651 * prevent the socket from being placed in the listen queue until all fields
652 * are fully initialized.
655 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
658 struct inpcb *inp = NULL;
659 struct tcpcb *tp = NULL;
664 if (so->so_state & SS_ISDISCONNECTED)
665 return (ECONNABORTED);
668 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
669 INP_INFO_RLOCK(&V_tcbinfo);
671 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
672 error = ECONNABORTED;
679 * We inline in_getpeeraddr and COMMON_END here, so that we can
680 * copy the data of interest and defer the malloc until after we
683 port = inp->inp_fport;
684 addr = inp->inp_faddr;
687 TCPDEBUG2(PRU_ACCEPT);
689 INP_INFO_RUNLOCK(&V_tcbinfo);
691 *nam = in_sockaddr(port, &addr);
698 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
700 struct inpcb *inp = NULL;
702 struct tcpcb *tp = NULL;
704 struct in6_addr addr6;
709 if (so->so_state & SS_ISDISCONNECTED)
710 return (ECONNABORTED);
713 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
714 INP_INFO_RLOCK(&V_tcbinfo);
716 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
717 error = ECONNABORTED;
724 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
725 * copy the data of interest and defer the malloc until after we
728 if (inp->inp_vflag & INP_IPV4) {
730 port = inp->inp_fport;
731 addr = inp->inp_faddr;
733 port = inp->inp_fport;
734 addr6 = inp->in6p_faddr;
738 TCPDEBUG2(PRU_ACCEPT);
740 INP_INFO_RUNLOCK(&V_tcbinfo);
743 *nam = in6_v4mapsin6_sockaddr(port, &addr);
745 *nam = in6_sockaddr(port, &addr6);
752 * Mark the connection as being incapable of further output.
755 tcp_usr_shutdown(struct socket *so)
759 struct tcpcb *tp = NULL;
762 INP_INFO_WLOCK(&V_tcbinfo);
764 KASSERT(inp != NULL, ("inp == NULL"));
766 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
774 if (!(inp->inp_flags & INP_DROPPED))
775 error = tcp_output(tp);
778 TCPDEBUG2(PRU_SHUTDOWN);
780 INP_INFO_WUNLOCK(&V_tcbinfo);
786 * After a receive, possibly send window update to peer.
789 tcp_usr_rcvd(struct socket *so, int flags)
792 struct tcpcb *tp = NULL;
797 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
799 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
807 * For passively-created TFO connections, don't attempt a window
808 * update while still in SYN_RECEIVED as this may trigger an early
809 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
810 * application response data, or failing that, when the DELACK timer
813 if ((tp->t_flags & TF_FASTOPEN) &&
814 (tp->t_state == TCPS_SYN_RECEIVED))
818 if (tp->t_flags & TF_TOE)
819 tcp_offload_rcvd(tp);
831 * Do a send by putting data in output queue and updating urgent
832 * marker if URG set. Possibly send more data. Unlike the other
833 * pru_*() routines, the mbuf chains are our responsibility. We
834 * must either enqueue them or free them. The other pru_* routines
835 * generally are caller-frees.
838 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
839 struct sockaddr *nam, struct mbuf *control, struct thread *td)
843 struct tcpcb *tp = NULL;
850 * We require the pcbinfo lock if we will close the socket as part of
853 if (flags & PRUS_EOF)
854 INP_INFO_WLOCK(&V_tcbinfo);
856 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
858 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
867 isipv6 = nam && nam->sa_family == AF_INET6;
872 /* TCP doesn't do control messages (rights, creds, etc) */
873 if (control->m_len) {
880 m_freem(control); /* empty control, just free it */
882 if (!(flags & PRUS_OOB)) {
883 sbappendstream(&so->so_snd, m);
884 if (nam && tp->t_state < TCPS_SYN_SENT) {
886 * Do implied connect if not yet connected,
887 * initialize window to default value, and
888 * initialize maxseg/maxopd using peer's cached
893 error = tcp6_connect(tp, nam, td);
895 #if defined(INET6) && defined(INET)
899 error = tcp_connect(tp, nam, td);
903 tp->snd_wnd = TTCP_CLIENT_SND_WND;
906 if (flags & PRUS_EOF) {
908 * Close the send side of the connection after
911 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
915 if (!(inp->inp_flags & INP_DROPPED)) {
916 if (flags & PRUS_MORETOCOME)
917 tp->t_flags |= TF_MORETOCOME;
918 error = tcp_output(tp);
919 if (flags & PRUS_MORETOCOME)
920 tp->t_flags &= ~TF_MORETOCOME;
924 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
926 SOCKBUF_LOCK(&so->so_snd);
927 if (sbspace(&so->so_snd) < -512) {
928 SOCKBUF_UNLOCK(&so->so_snd);
934 * According to RFC961 (Assigned Protocols),
935 * the urgent pointer points to the last octet
936 * of urgent data. We continue, however,
937 * to consider it to indicate the first octet
938 * of data past the urgent section.
939 * Otherwise, snd_up should be one lower.
941 sbappendstream_locked(&so->so_snd, m);
942 SOCKBUF_UNLOCK(&so->so_snd);
943 if (nam && tp->t_state < TCPS_SYN_SENT) {
945 * Do implied connect if not yet connected,
946 * initialize window to default value, and
947 * initialize maxseg/maxopd using peer's cached
952 error = tcp6_connect(tp, nam, td);
954 #if defined(INET6) && defined(INET)
958 error = tcp_connect(tp, nam, td);
962 tp->snd_wnd = TTCP_CLIENT_SND_WND;
965 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
966 tp->t_flags |= TF_FORCEDATA;
967 error = tcp_output(tp);
968 tp->t_flags &= ~TF_FORCEDATA;
971 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
972 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
974 if (flags & PRUS_EOF)
975 INP_INFO_WUNLOCK(&V_tcbinfo);
980 * Abort the TCP. Drop the connection abruptly.
983 tcp_usr_abort(struct socket *so)
986 struct tcpcb *tp = NULL;
990 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
992 INP_INFO_WLOCK(&V_tcbinfo);
994 KASSERT(inp->inp_socket != NULL,
995 ("tcp_usr_abort: inp_socket == NULL"));
998 * If we still have full TCP state, and we're not dropped, drop.
1000 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1001 !(inp->inp_flags & INP_DROPPED)) {
1002 tp = intotcpcb(inp);
1004 tcp_drop(tp, ECONNABORTED);
1005 TCPDEBUG2(PRU_ABORT);
1007 if (!(inp->inp_flags & INP_DROPPED)) {
1009 so->so_state |= SS_PROTOREF;
1011 inp->inp_flags |= INP_SOCKREF;
1014 INP_INFO_WUNLOCK(&V_tcbinfo);
1018 * TCP socket is closed. Start friendly disconnect.
1021 tcp_usr_close(struct socket *so)
1024 struct tcpcb *tp = NULL;
1027 inp = sotoinpcb(so);
1028 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1030 INP_INFO_WLOCK(&V_tcbinfo);
1032 KASSERT(inp->inp_socket != NULL,
1033 ("tcp_usr_close: inp_socket == NULL"));
1036 * If we still have full TCP state, and we're not dropped, initiate
1039 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1040 !(inp->inp_flags & INP_DROPPED)) {
1041 tp = intotcpcb(inp);
1044 TCPDEBUG2(PRU_CLOSE);
1046 if (!(inp->inp_flags & INP_DROPPED)) {
1048 so->so_state |= SS_PROTOREF;
1050 inp->inp_flags |= INP_SOCKREF;
1053 INP_INFO_WUNLOCK(&V_tcbinfo);
1057 * Receive out-of-band data.
1060 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1064 struct tcpcb *tp = NULL;
1067 inp = sotoinpcb(so);
1068 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1070 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1074 tp = intotcpcb(inp);
1076 if ((so->so_oobmark == 0 &&
1077 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1078 so->so_options & SO_OOBINLINE ||
1079 tp->t_oobflags & TCPOOB_HADDATA) {
1083 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1084 error = EWOULDBLOCK;
1088 *mtod(m, caddr_t) = tp->t_iobc;
1089 if ((flags & MSG_PEEK) == 0)
1090 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1093 TCPDEBUG2(PRU_RCVOOB);
1099 struct pr_usrreqs tcp_usrreqs = {
1100 .pru_abort = tcp_usr_abort,
1101 .pru_accept = tcp_usr_accept,
1102 .pru_attach = tcp_usr_attach,
1103 .pru_bind = tcp_usr_bind,
1104 .pru_connect = tcp_usr_connect,
1105 .pru_control = in_control,
1106 .pru_detach = tcp_usr_detach,
1107 .pru_disconnect = tcp_usr_disconnect,
1108 .pru_listen = tcp_usr_listen,
1109 .pru_peeraddr = in_getpeeraddr,
1110 .pru_rcvd = tcp_usr_rcvd,
1111 .pru_rcvoob = tcp_usr_rcvoob,
1112 .pru_send = tcp_usr_send,
1113 .pru_shutdown = tcp_usr_shutdown,
1114 .pru_sockaddr = in_getsockaddr,
1115 .pru_sosetlabel = in_pcbsosetlabel,
1116 .pru_close = tcp_usr_close,
1121 struct pr_usrreqs tcp6_usrreqs = {
1122 .pru_abort = tcp_usr_abort,
1123 .pru_accept = tcp6_usr_accept,
1124 .pru_attach = tcp_usr_attach,
1125 .pru_bind = tcp6_usr_bind,
1126 .pru_connect = tcp6_usr_connect,
1127 .pru_control = in6_control,
1128 .pru_detach = tcp_usr_detach,
1129 .pru_disconnect = tcp_usr_disconnect,
1130 .pru_listen = tcp6_usr_listen,
1131 .pru_peeraddr = in6_mapped_peeraddr,
1132 .pru_rcvd = tcp_usr_rcvd,
1133 .pru_rcvoob = tcp_usr_rcvoob,
1134 .pru_send = tcp_usr_send,
1135 .pru_shutdown = tcp_usr_shutdown,
1136 .pru_sockaddr = in6_mapped_sockaddr,
1137 .pru_sosetlabel = in_pcbsosetlabel,
1138 .pru_close = tcp_usr_close,
1144 * Common subroutine to open a TCP connection to remote host specified
1145 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1146 * port number if needed. Call in_pcbconnect_setup to do the routing and
1147 * to choose a local host address (interface). If there is an existing
1148 * incarnation of the same connection in TIME-WAIT state and if the remote
1149 * host was sending CC options and if the connection duration was < MSL, then
1150 * truncate the previous TIME-WAIT state and proceed.
1151 * Initialize connection parameters and enter SYN-SENT state.
1154 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1156 struct inpcb *inp = tp->t_inpcb, *oinp;
1157 struct socket *so = inp->inp_socket;
1158 struct in_addr laddr;
1162 INP_WLOCK_ASSERT(inp);
1163 INP_HASH_WLOCK(&V_tcbinfo);
1165 if (inp->inp_lport == 0) {
1166 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1172 * Cannot simply call in_pcbconnect, because there might be an
1173 * earlier incarnation of this same connection still in
1174 * TIME_WAIT state, creating an ADDRINUSE error.
1176 laddr = inp->inp_laddr;
1177 lport = inp->inp_lport;
1178 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1179 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1180 if (error && oinp == NULL)
1186 inp->inp_laddr = laddr;
1188 INP_HASH_WUNLOCK(&V_tcbinfo);
1191 * Compute window scaling to request:
1192 * Scale to fit into sweet spot. See tcp_syncache.c.
1193 * XXX: This should move to tcp_output().
1195 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1196 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1197 tp->request_r_scale++;
1200 TCPSTAT_INC(tcps_connattempt);
1201 tcp_state_change(tp, TCPS_SYN_SENT);
1202 tp->iss = tcp_new_isn(tp);
1203 tcp_sendseqinit(tp);
1208 INP_HASH_WUNLOCK(&V_tcbinfo);
1215 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1217 struct inpcb *inp = tp->t_inpcb, *oinp;
1218 struct socket *so = inp->inp_socket;
1219 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1220 struct in6_addr addr6;
1223 INP_WLOCK_ASSERT(inp);
1224 INP_HASH_WLOCK(&V_tcbinfo);
1226 if (inp->inp_lport == 0) {
1227 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1233 * Cannot simply call in_pcbconnect, because there might be an
1234 * earlier incarnation of this same connection still in
1235 * TIME_WAIT state, creating an ADDRINUSE error.
1236 * in6_pcbladdr() also handles scope zone IDs.
1238 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1239 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1241 error = in6_pcbladdr(inp, nam, &addr6);
1244 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1245 &sin6->sin6_addr, sin6->sin6_port,
1246 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1249 inp->inp_lport, 0, NULL);
1254 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1255 inp->in6p_laddr = addr6;
1256 inp->in6p_faddr = sin6->sin6_addr;
1257 inp->inp_fport = sin6->sin6_port;
1258 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1259 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1260 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1262 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1264 INP_HASH_WUNLOCK(&V_tcbinfo);
1266 /* Compute window scaling to request. */
1267 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1268 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1269 tp->request_r_scale++;
1272 TCPSTAT_INC(tcps_connattempt);
1273 tcp_state_change(tp, TCPS_SYN_SENT);
1274 tp->iss = tcp_new_isn(tp);
1275 tcp_sendseqinit(tp);
1280 INP_HASH_WUNLOCK(&V_tcbinfo);
1286 * Export TCP internal state information via a struct tcp_info, based on the
1287 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1288 * (TCP state machine, etc). We export all information using FreeBSD-native
1289 * constants -- for example, the numeric values for tcpi_state will differ
1293 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1296 INP_WLOCK_ASSERT(tp->t_inpcb);
1297 bzero(ti, sizeof(*ti));
1299 ti->tcpi_state = tp->t_state;
1300 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1301 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1302 if (tp->t_flags & TF_SACK_PERMIT)
1303 ti->tcpi_options |= TCPI_OPT_SACK;
1304 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1305 ti->tcpi_options |= TCPI_OPT_WSCALE;
1306 ti->tcpi_snd_wscale = tp->snd_scale;
1307 ti->tcpi_rcv_wscale = tp->rcv_scale;
1310 ti->tcpi_rto = tp->t_rxtcur * tick;
1311 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1312 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1313 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1315 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1316 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1319 * FreeBSD-specific extension fields for tcp_info.
1321 ti->tcpi_rcv_space = tp->rcv_wnd;
1322 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1323 ti->tcpi_snd_wnd = tp->snd_wnd;
1324 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1325 ti->tcpi_snd_nxt = tp->snd_nxt;
1326 ti->tcpi_snd_mss = tp->t_maxseg;
1327 ti->tcpi_rcv_mss = tp->t_maxseg;
1328 if (tp->t_flags & TF_TOE)
1329 ti->tcpi_options |= TCPI_OPT_TOE;
1330 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1331 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1332 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1336 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1337 * socket option arguments. When it re-acquires the lock after the copy, it
1338 * has to revalidate that the connection is still valid for the socket
1341 #define INP_WLOCK_RECHECK(inp) do { \
1343 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1345 return (ECONNRESET); \
1347 tp = intotcpcb(inp); \
1351 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1353 int error, opt, optval;
1358 char buf[TCP_CA_NAME_MAX];
1359 struct cc_algo *algo;
1362 inp = sotoinpcb(so);
1363 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1365 if (sopt->sopt_level != IPPROTO_TCP) {
1367 if (inp->inp_vflag & INP_IPV6PROTO) {
1369 error = ip6_ctloutput(so, sopt);
1372 #if defined(INET6) && defined(INET)
1378 error = ip_ctloutput(so, sopt);
1383 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1385 return (ECONNRESET);
1388 switch (sopt->sopt_dir) {
1390 switch (sopt->sopt_name) {
1391 #ifdef TCP_SIGNATURE
1394 error = sooptcopyin(sopt, &optval, sizeof optval,
1399 INP_WLOCK_RECHECK(inp);
1401 tp->t_flags |= TF_SIGNATURE;
1403 tp->t_flags &= ~TF_SIGNATURE;
1404 goto unlock_and_done;
1405 #endif /* TCP_SIGNATURE */
1410 error = sooptcopyin(sopt, &optval, sizeof optval,
1415 INP_WLOCK_RECHECK(inp);
1416 switch (sopt->sopt_name) {
1424 opt = 0; /* dead code to fool gcc */
1431 tp->t_flags &= ~opt;
1434 if (tp->t_flags & TF_TOE) {
1435 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1444 error = sooptcopyin(sopt, &optval, sizeof optval,
1449 INP_WLOCK_RECHECK(inp);
1451 tp->t_flags |= TF_NOPUSH;
1452 else if (tp->t_flags & TF_NOPUSH) {
1453 tp->t_flags &= ~TF_NOPUSH;
1454 if (TCPS_HAVEESTABLISHED(tp->t_state))
1455 error = tcp_output(tp);
1457 goto unlock_and_done;
1461 error = sooptcopyin(sopt, &optval, sizeof optval,
1466 INP_WLOCK_RECHECK(inp);
1467 if (optval > 0 && optval <= tp->t_maxseg &&
1468 optval + 40 >= V_tcp_minmss)
1469 tp->t_maxseg = optval;
1472 goto unlock_and_done;
1479 case TCP_CONGESTION:
1481 bzero(buf, sizeof(buf));
1482 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1485 INP_WLOCK_RECHECK(inp);
1487 * Return EINVAL if we can't find the requested cc algo.
1491 STAILQ_FOREACH(algo, &cc_list, entries) {
1492 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1494 /* We've found the requested algo. */
1497 * We hold a write lock over the tcb
1498 * so it's safe to do these things
1499 * without ordering concerns.
1501 if (CC_ALGO(tp)->cb_destroy != NULL)
1502 CC_ALGO(tp)->cb_destroy(tp->ccv);
1505 * If something goes pear shaped
1506 * initialising the new algo,
1507 * fall back to newreno (which
1508 * does not require initialisation).
1510 if (algo->cb_init != NULL)
1511 if (algo->cb_init(tp->ccv) > 0) {
1512 CC_ALGO(tp) = &newreno_cc_algo;
1514 * The only reason init
1516 * because of malloc.
1520 break; /* Break the STAILQ_FOREACH. */
1524 goto unlock_and_done;
1530 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1534 if (ui > (UINT_MAX / hz)) {
1540 INP_WLOCK_RECHECK(inp);
1541 switch (sopt->sopt_name) {
1543 tp->t_keepidle = ui;
1545 * XXX: better check current remaining
1546 * timeout and "merge" it with new value.
1548 if ((tp->t_state > TCPS_LISTEN) &&
1549 (tp->t_state <= TCPS_CLOSING))
1550 tcp_timer_activate(tp, TT_KEEP,
1554 tp->t_keepintvl = ui;
1555 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1556 (TP_MAXIDLE(tp) > 0))
1557 tcp_timer_activate(tp, TT_2MSL,
1561 tp->t_keepinit = ui;
1562 if (tp->t_state == TCPS_SYN_RECEIVED ||
1563 tp->t_state == TCPS_SYN_SENT)
1564 tcp_timer_activate(tp, TT_KEEP,
1568 goto unlock_and_done;
1572 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1576 INP_WLOCK_RECHECK(inp);
1578 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1579 (TP_MAXIDLE(tp) > 0))
1580 tcp_timer_activate(tp, TT_2MSL,
1582 goto unlock_and_done;
1587 if (!V_tcp_fastopen_enabled)
1590 error = sooptcopyin(sopt, &optval, sizeof optval,
1595 INP_WLOCK_RECHECK(inp);
1597 tp->t_flags |= TF_FASTOPEN;
1598 if ((tp->t_state == TCPS_LISTEN) &&
1599 (tp->t_tfo_pending == NULL))
1601 tcp_fastopen_alloc_counter();
1603 tp->t_flags &= ~TF_FASTOPEN;
1604 goto unlock_and_done;
1609 error = ENOPROTOOPT;
1615 tp = intotcpcb(inp);
1616 switch (sopt->sopt_name) {
1617 #ifdef TCP_SIGNATURE
1619 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1621 error = sooptcopyout(sopt, &optval, sizeof optval);
1626 optval = tp->t_flags & TF_NODELAY;
1628 error = sooptcopyout(sopt, &optval, sizeof optval);
1631 optval = tp->t_maxseg;
1633 error = sooptcopyout(sopt, &optval, sizeof optval);
1636 optval = tp->t_flags & TF_NOOPT;
1638 error = sooptcopyout(sopt, &optval, sizeof optval);
1641 optval = tp->t_flags & TF_NOPUSH;
1643 error = sooptcopyout(sopt, &optval, sizeof optval);
1646 tcp_fill_info(tp, &ti);
1648 error = sooptcopyout(sopt, &ti, sizeof ti);
1650 case TCP_CONGESTION:
1651 bzero(buf, sizeof(buf));
1652 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1654 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1660 switch (sopt->sopt_name) {
1662 ui = tp->t_keepidle / hz;
1665 ui = tp->t_keepintvl / hz;
1668 ui = tp->t_keepinit / hz;
1675 error = sooptcopyout(sopt, &ui, sizeof(ui));
1679 optval = tp->t_flags & TF_FASTOPEN;
1681 error = sooptcopyout(sopt, &optval, sizeof optval);
1686 error = ENOPROTOOPT;
1693 #undef INP_WLOCK_RECHECK
1696 * Attach TCP protocol to socket, allocating
1697 * internet protocol control block, tcp control block,
1698 * bufer space, and entering LISTEN state if to accept connections.
1701 tcp_attach(struct socket *so)
1707 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1708 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1712 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1713 so->so_snd.sb_flags |= SB_AUTOSIZE;
1714 INP_INFO_WLOCK(&V_tcbinfo);
1715 error = in_pcballoc(so, &V_tcbinfo);
1717 INP_INFO_WUNLOCK(&V_tcbinfo);
1720 inp = sotoinpcb(so);
1722 if (inp->inp_vflag & INP_IPV6PROTO) {
1723 inp->inp_vflag |= INP_IPV6;
1724 inp->in6p_hops = -1; /* use kernel default */
1728 inp->inp_vflag |= INP_IPV4;
1729 tp = tcp_newtcpcb(inp);
1733 INP_INFO_WUNLOCK(&V_tcbinfo);
1736 tp->t_state = TCPS_CLOSED;
1738 INP_INFO_WUNLOCK(&V_tcbinfo);
1743 * Initiate (or continue) disconnect.
1744 * If embryonic state, just send reset (once).
1745 * If in ``let data drain'' option and linger null, just drop.
1746 * Otherwise (hard), mark socket disconnecting and drop
1747 * current input data; switch states based on user close, and
1748 * send segment to peer (with FIN).
1751 tcp_disconnect(struct tcpcb *tp)
1753 struct inpcb *inp = tp->t_inpcb;
1754 struct socket *so = inp->inp_socket;
1756 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1757 INP_WLOCK_ASSERT(inp);
1760 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1761 * socket is still open.
1763 if (tp->t_state < TCPS_ESTABLISHED) {
1766 ("tcp_disconnect: tcp_close() returned NULL"));
1767 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1768 tp = tcp_drop(tp, 0);
1770 ("tcp_disconnect: tcp_drop() returned NULL"));
1772 soisdisconnecting(so);
1773 sbflush(&so->so_rcv);
1775 if (!(inp->inp_flags & INP_DROPPED))
1781 * User issued close, and wish to trail through shutdown states:
1782 * if never received SYN, just forget it. If got a SYN from peer,
1783 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1784 * If already got a FIN from peer, then almost done; go to LAST_ACK
1785 * state. In all other cases, have already sent FIN to peer (e.g.
1786 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1787 * for peer to send FIN or not respond to keep-alives, etc.
1788 * We can let the user exit from the close as soon as the FIN is acked.
1791 tcp_usrclosed(struct tcpcb *tp)
1794 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1795 INP_WLOCK_ASSERT(tp->t_inpcb);
1797 switch (tp->t_state) {
1800 tcp_offload_listen_stop(tp);
1802 tcp_state_change(tp, TCPS_CLOSED);
1807 * tcp_close() should never return NULL here as the socket is
1811 ("tcp_usrclosed: tcp_close() returned NULL"));
1815 case TCPS_SYN_RECEIVED:
1816 tp->t_flags |= TF_NEEDFIN;
1819 case TCPS_ESTABLISHED:
1820 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1823 case TCPS_CLOSE_WAIT:
1824 tcp_state_change(tp, TCPS_LAST_ACK);
1827 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1828 soisdisconnected(tp->t_inpcb->inp_socket);
1829 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1830 if (tp->t_state == TCPS_FIN_WAIT_2) {
1833 timeout = (tcp_fast_finwait2_recycle) ?
1834 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1835 tcp_timer_activate(tp, TT_2MSL, timeout);
1842 db_print_indent(int indent)
1846 for (i = 0; i < indent; i++)
1851 db_print_tstate(int t_state)
1856 db_printf("TCPS_CLOSED");
1860 db_printf("TCPS_LISTEN");
1864 db_printf("TCPS_SYN_SENT");
1867 case TCPS_SYN_RECEIVED:
1868 db_printf("TCPS_SYN_RECEIVED");
1871 case TCPS_ESTABLISHED:
1872 db_printf("TCPS_ESTABLISHED");
1875 case TCPS_CLOSE_WAIT:
1876 db_printf("TCPS_CLOSE_WAIT");
1879 case TCPS_FIN_WAIT_1:
1880 db_printf("TCPS_FIN_WAIT_1");
1884 db_printf("TCPS_CLOSING");
1888 db_printf("TCPS_LAST_ACK");
1891 case TCPS_FIN_WAIT_2:
1892 db_printf("TCPS_FIN_WAIT_2");
1895 case TCPS_TIME_WAIT:
1896 db_printf("TCPS_TIME_WAIT");
1900 db_printf("unknown");
1906 db_print_tflags(u_int t_flags)
1911 if (t_flags & TF_ACKNOW) {
1912 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1915 if (t_flags & TF_DELACK) {
1916 db_printf("%sTF_DELACK", comma ? ", " : "");
1919 if (t_flags & TF_NODELAY) {
1920 db_printf("%sTF_NODELAY", comma ? ", " : "");
1923 if (t_flags & TF_NOOPT) {
1924 db_printf("%sTF_NOOPT", comma ? ", " : "");
1927 if (t_flags & TF_SENTFIN) {
1928 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1931 if (t_flags & TF_REQ_SCALE) {
1932 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1935 if (t_flags & TF_RCVD_SCALE) {
1936 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1939 if (t_flags & TF_REQ_TSTMP) {
1940 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1943 if (t_flags & TF_RCVD_TSTMP) {
1944 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1947 if (t_flags & TF_SACK_PERMIT) {
1948 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1951 if (t_flags & TF_NEEDSYN) {
1952 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1955 if (t_flags & TF_NEEDFIN) {
1956 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1959 if (t_flags & TF_NOPUSH) {
1960 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1963 if (t_flags & TF_MORETOCOME) {
1964 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1967 if (t_flags & TF_LQ_OVERFLOW) {
1968 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1971 if (t_flags & TF_LASTIDLE) {
1972 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1975 if (t_flags & TF_RXWIN0SENT) {
1976 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1979 if (t_flags & TF_FASTRECOVERY) {
1980 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1983 if (t_flags & TF_CONGRECOVERY) {
1984 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1987 if (t_flags & TF_WASFRECOVERY) {
1988 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1991 if (t_flags & TF_SIGNATURE) {
1992 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1995 if (t_flags & TF_FORCEDATA) {
1996 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1999 if (t_flags & TF_TSO) {
2000 db_printf("%sTF_TSO", comma ? ", " : "");
2003 if (t_flags & TF_ECN_PERMIT) {
2004 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2007 if (t_flags & TF_FASTOPEN) {
2008 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2014 db_print_toobflags(char t_oobflags)
2019 if (t_oobflags & TCPOOB_HAVEDATA) {
2020 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2023 if (t_oobflags & TCPOOB_HADDATA) {
2024 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2030 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2033 db_print_indent(indent);
2034 db_printf("%s at %p\n", name, tp);
2038 db_print_indent(indent);
2039 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2040 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2042 db_print_indent(indent);
2043 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2044 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2046 db_print_indent(indent);
2047 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2048 &tp->t_timers->tt_delack, tp->t_inpcb);
2050 db_print_indent(indent);
2051 db_printf("t_state: %d (", tp->t_state);
2052 db_print_tstate(tp->t_state);
2055 db_print_indent(indent);
2056 db_printf("t_flags: 0x%x (", tp->t_flags);
2057 db_print_tflags(tp->t_flags);
2060 db_print_indent(indent);
2061 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2062 tp->snd_una, tp->snd_max, tp->snd_nxt);
2064 db_print_indent(indent);
2065 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2066 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2068 db_print_indent(indent);
2069 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2070 tp->iss, tp->irs, tp->rcv_nxt);
2072 db_print_indent(indent);
2073 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2074 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2076 db_print_indent(indent);
2077 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2078 tp->snd_wnd, tp->snd_cwnd);
2080 db_print_indent(indent);
2081 db_printf("snd_ssthresh: %lu snd_recover: "
2082 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2084 db_print_indent(indent);
2085 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2086 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2088 db_print_indent(indent);
2089 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2090 tp->t_rtttime, tp->t_rtseq);
2092 db_print_indent(indent);
2093 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2094 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2096 db_print_indent(indent);
2097 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2098 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2101 db_print_indent(indent);
2102 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2103 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2105 db_print_indent(indent);
2106 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2107 db_print_toobflags(tp->t_oobflags);
2108 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2110 db_print_indent(indent);
2111 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2112 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2114 db_print_indent(indent);
2115 db_printf("ts_recent: %u ts_recent_age: %u\n",
2116 tp->ts_recent, tp->ts_recent_age);
2118 db_print_indent(indent);
2119 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2120 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2122 db_print_indent(indent);
2123 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2124 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2125 tp->snd_recover_prev, tp->t_badrxtwin);
2127 db_print_indent(indent);
2128 db_printf("snd_numholes: %d snd_holes first: %p\n",
2129 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2131 db_print_indent(indent);
2132 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2133 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2135 /* Skip sackblks, sackhint. */
2137 db_print_indent(indent);
2138 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2139 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2142 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2147 db_printf("usage: show tcpcb <addr>\n");
2150 tp = (struct tcpcb *)addr;
2152 db_print_tcpcb(tp, "tcpcb", 0);