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/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
53 #include <sys/domain.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/protosw.h>
66 #include <net/route.h>
69 #include <netinet/cc.h>
70 #include <netinet/in.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip_var.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/in6_pcb.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/scope6_var.h>
81 #include <netinet/tcp_fsm.h>
82 #include <netinet/tcp_seq.h>
83 #include <netinet/tcp_timer.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/tcpip.h>
87 #include <netinet/tcp_debug.h>
89 #include <netinet/tcp_offload.h>
92 * TCP protocol interface to socket abstraction.
94 static int tcp_attach(struct socket *);
96 static int tcp_connect(struct tcpcb *, struct sockaddr *,
100 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
103 static void tcp_disconnect(struct tcpcb *);
104 static void tcp_usrclosed(struct tcpcb *);
105 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
108 #define TCPDEBUG0 int ostate = 0
109 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
110 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
111 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
115 #define TCPDEBUG2(req)
119 * TCP attaches to socket via pru_attach(), reserving space,
120 * and an internet control block.
123 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
126 struct tcpcb *tp = NULL;
131 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
134 error = tcp_attach(so);
138 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
139 so->so_linger = TCP_LINGERTIME;
144 TCPDEBUG2(PRU_ATTACH);
149 * tcp_detach is called when the socket layer loses its final reference
150 * to the socket, be it a file descriptor reference, a reference from TCP,
151 * etc. At this point, there is only one case in which we will keep around
152 * inpcb state: time wait.
154 * This function can probably be re-absorbed back into tcp_usr_detach() now
155 * that there is a single detach path.
158 tcp_detach(struct socket *so, struct inpcb *inp)
162 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
163 INP_WLOCK_ASSERT(inp);
165 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
166 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
170 if (inp->inp_flags & INP_TIMEWAIT) {
172 * There are two cases to handle: one in which the time wait
173 * state is being discarded (INP_DROPPED), and one in which
174 * this connection will remain in timewait. In the former,
175 * it is time to discard all state (except tcptw, which has
176 * already been discarded by the timewait close code, which
177 * should be further up the call stack somewhere). In the
178 * latter case, we detach from the socket, but leave the pcb
179 * present until timewait ends.
181 * XXXRW: Would it be cleaner to free the tcptw here?
183 if (inp->inp_flags & INP_DROPPED) {
184 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
185 "INP_DROPPED && tp != NULL"));
194 * If the connection is not in timewait, we consider two
195 * two conditions: one in which no further processing is
196 * necessary (dropped || embryonic), and one in which TCP is
197 * not yet done, but no longer requires the socket, so the
198 * pcb will persist for the time being.
200 * XXXRW: Does the second case still occur?
202 if (inp->inp_flags & INP_DROPPED ||
203 tp->t_state < TCPS_SYN_SENT) {
213 * pru_detach() detaches the TCP protocol from the socket.
214 * If the protocol state is non-embryonic, then can't
215 * do this directly: have to initiate a pru_disconnect(),
216 * which may finish later; embryonic TCB's can just
220 tcp_usr_detach(struct socket *so)
225 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
226 INP_INFO_WLOCK(&V_tcbinfo);
228 KASSERT(inp->inp_socket != NULL,
229 ("tcp_usr_detach: inp_socket == NULL"));
231 INP_INFO_WUNLOCK(&V_tcbinfo);
236 * Give the socket an address.
239 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
243 struct tcpcb *tp = NULL;
244 struct sockaddr_in *sinp;
246 sinp = (struct sockaddr_in *)nam;
247 if (nam->sa_len != sizeof (*sinp))
250 * Must check for multicast addresses and disallow binding
253 if (sinp->sin_family == AF_INET &&
254 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
255 return (EAFNOSUPPORT);
259 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
261 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
267 INP_HASH_WLOCK(&V_tcbinfo);
268 error = in_pcbbind(inp, nam, td->td_ucred);
269 INP_HASH_WUNLOCK(&V_tcbinfo);
280 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
284 struct tcpcb *tp = NULL;
285 struct sockaddr_in6 *sin6p;
287 sin6p = (struct sockaddr_in6 *)nam;
288 if (nam->sa_len != sizeof (*sin6p))
291 * Must check for multicast addresses and disallow binding
294 if (sin6p->sin6_family == AF_INET6 &&
295 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
296 return (EAFNOSUPPORT);
300 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
302 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
308 INP_HASH_WLOCK(&V_tcbinfo);
309 inp->inp_vflag &= ~INP_IPV4;
310 inp->inp_vflag |= INP_IPV6;
312 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
313 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
314 inp->inp_vflag |= INP_IPV4;
315 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
316 struct sockaddr_in sin;
318 in6_sin6_2_sin(&sin, sin6p);
319 inp->inp_vflag |= INP_IPV4;
320 inp->inp_vflag &= ~INP_IPV6;
321 error = in_pcbbind(inp, (struct sockaddr *)&sin,
323 INP_HASH_WUNLOCK(&V_tcbinfo);
328 error = in6_pcbbind(inp, nam, td->td_ucred);
329 INP_HASH_WUNLOCK(&V_tcbinfo);
339 * Prepare to accept connections.
342 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
346 struct tcpcb *tp = NULL;
350 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
352 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
359 error = solisten_proto_check(so);
360 INP_HASH_WLOCK(&V_tcbinfo);
361 if (error == 0 && inp->inp_lport == 0)
362 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
363 INP_HASH_WUNLOCK(&V_tcbinfo);
365 tp->t_state = TCPS_LISTEN;
366 solisten_proto(so, backlog);
367 tcp_offload_listen_open(tp);
372 TCPDEBUG2(PRU_LISTEN);
380 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
384 struct tcpcb *tp = NULL;
388 KASSERT(inp != NULL, ("tcp6_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 inp->inp_vflag &= ~INP_IPV4;
401 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
402 inp->inp_vflag |= INP_IPV4;
403 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
405 INP_HASH_WUNLOCK(&V_tcbinfo);
407 tp->t_state = TCPS_LISTEN;
408 solisten_proto(so, backlog);
413 TCPDEBUG2(PRU_LISTEN);
421 * Initiate connection to peer.
422 * Create a template for use in transmissions on this connection.
423 * Enter SYN_SENT state, and mark socket as connecting.
424 * Start keep-alive timer, and seed output sequence space.
425 * Send initial segment on connection.
428 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
432 struct tcpcb *tp = NULL;
433 struct sockaddr_in *sinp;
435 sinp = (struct sockaddr_in *)nam;
436 if (nam->sa_len != sizeof (*sinp))
439 * Must disallow TCP ``connections'' to multicast addresses.
441 if (sinp->sin_family == AF_INET
442 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
443 return (EAFNOSUPPORT);
444 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
449 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
451 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
457 if ((error = tcp_connect(tp, nam, td)) != 0)
459 error = tcp_output_connect(so, nam);
461 TCPDEBUG2(PRU_CONNECT);
469 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
473 struct tcpcb *tp = NULL;
474 struct sockaddr_in6 *sin6p;
478 sin6p = (struct sockaddr_in6 *)nam;
479 if (nam->sa_len != sizeof (*sin6p))
482 * Must disallow TCP ``connections'' to multicast addresses.
484 if (sin6p->sin6_family == AF_INET6
485 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
486 return (EAFNOSUPPORT);
489 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
491 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
499 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
500 * therefore probably require the hash lock, which isn't held here.
501 * Is this a significant problem?
503 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
504 struct sockaddr_in sin;
506 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
511 in6_sin6_2_sin(&sin, sin6p);
512 inp->inp_vflag |= INP_IPV4;
513 inp->inp_vflag &= ~INP_IPV6;
514 if ((error = prison_remote_ip4(td->td_ucred,
515 &sin.sin_addr)) != 0)
517 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
519 error = tcp_output_connect(so, nam);
523 inp->inp_vflag &= ~INP_IPV4;
524 inp->inp_vflag |= INP_IPV6;
525 inp->inp_inc.inc_flags |= INC_ISIPV6;
526 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
528 if ((error = tcp6_connect(tp, nam, td)) != 0)
530 error = tcp_output_connect(so, nam);
533 TCPDEBUG2(PRU_CONNECT);
540 * Initiate disconnect from peer.
541 * If connection never passed embryonic stage, just drop;
542 * else if don't need to let data drain, then can just drop anyways,
543 * else have to begin TCP shutdown process: mark socket disconnecting,
544 * drain unread data, state switch to reflect user close, and
545 * send segment (e.g. FIN) to peer. Socket will be really disconnected
546 * when peer sends FIN and acks ours.
548 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
551 tcp_usr_disconnect(struct socket *so)
554 struct tcpcb *tp = NULL;
558 INP_INFO_WLOCK(&V_tcbinfo);
560 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
562 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
570 TCPDEBUG2(PRU_DISCONNECT);
572 INP_INFO_WUNLOCK(&V_tcbinfo);
578 * Accept a connection. Essentially all the work is done at higher levels;
579 * just return the address of the peer, storing through addr.
581 * The rationale for acquiring the tcbinfo lock here is somewhat complicated,
582 * and is described in detail in the commit log entry for r175612. Acquiring
583 * it delays an accept(2) racing with sonewconn(), which inserts the socket
584 * before the inpcb address/port fields are initialized. A better fix would
585 * prevent the socket from being placed in the listen queue until all fields
586 * are fully initialized.
589 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
592 struct inpcb *inp = NULL;
593 struct tcpcb *tp = NULL;
598 if (so->so_state & SS_ISDISCONNECTED)
599 return (ECONNABORTED);
602 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
603 INP_INFO_RLOCK(&V_tcbinfo);
605 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
606 error = ECONNABORTED;
613 * We inline in_getpeeraddr and COMMON_END here, so that we can
614 * copy the data of interest and defer the malloc until after we
617 port = inp->inp_fport;
618 addr = inp->inp_faddr;
621 TCPDEBUG2(PRU_ACCEPT);
623 INP_INFO_RUNLOCK(&V_tcbinfo);
625 *nam = in_sockaddr(port, &addr);
632 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
634 struct inpcb *inp = NULL;
636 struct tcpcb *tp = NULL;
638 struct in6_addr addr6;
643 if (so->so_state & SS_ISDISCONNECTED)
644 return (ECONNABORTED);
647 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
648 INP_INFO_RLOCK(&V_tcbinfo);
650 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
651 error = ECONNABORTED;
658 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
659 * copy the data of interest and defer the malloc until after we
662 if (inp->inp_vflag & INP_IPV4) {
664 port = inp->inp_fport;
665 addr = inp->inp_faddr;
667 port = inp->inp_fport;
668 addr6 = inp->in6p_faddr;
672 TCPDEBUG2(PRU_ACCEPT);
674 INP_INFO_RUNLOCK(&V_tcbinfo);
677 *nam = in6_v4mapsin6_sockaddr(port, &addr);
679 *nam = in6_sockaddr(port, &addr6);
686 * Mark the connection as being incapable of further output.
689 tcp_usr_shutdown(struct socket *so)
693 struct tcpcb *tp = NULL;
696 INP_INFO_WLOCK(&V_tcbinfo);
698 KASSERT(inp != NULL, ("inp == NULL"));
700 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
708 if (!(inp->inp_flags & INP_DROPPED))
709 error = tcp_output_disconnect(tp);
712 TCPDEBUG2(PRU_SHUTDOWN);
714 INP_INFO_WUNLOCK(&V_tcbinfo);
720 * After a receive, possibly send window update to peer.
723 tcp_usr_rcvd(struct socket *so, int flags)
726 struct tcpcb *tp = NULL;
731 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
733 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
748 * Do a send by putting data in output queue and updating urgent
749 * marker if URG set. Possibly send more data. Unlike the other
750 * pru_*() routines, the mbuf chains are our responsibility. We
751 * must either enqueue them or free them. The other pru_* routines
752 * generally are caller-frees.
755 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
756 struct sockaddr *nam, struct mbuf *control, struct thread *td)
760 struct tcpcb *tp = NULL;
767 * We require the pcbinfo lock if we will close the socket as part of
770 if (flags & PRUS_EOF)
771 INP_INFO_WLOCK(&V_tcbinfo);
773 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
775 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
784 isipv6 = nam && nam->sa_family == AF_INET6;
789 /* TCP doesn't do control messages (rights, creds, etc) */
790 if (control->m_len) {
797 m_freem(control); /* empty control, just free it */
799 if (!(flags & PRUS_OOB)) {
800 sbappendstream(&so->so_snd, m);
801 if (nam && tp->t_state < TCPS_SYN_SENT) {
803 * Do implied connect if not yet connected,
804 * initialize window to default value, and
805 * initialize maxseg/maxopd using peer's cached
810 error = tcp6_connect(tp, nam, td);
812 #if defined(INET6) && defined(INET)
816 error = tcp_connect(tp, nam, td);
820 tp->snd_wnd = TTCP_CLIENT_SND_WND;
823 if (flags & PRUS_EOF) {
825 * Close the send side of the connection after
828 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
832 if (!(inp->inp_flags & INP_DROPPED)) {
833 if (flags & PRUS_MORETOCOME)
834 tp->t_flags |= TF_MORETOCOME;
835 error = tcp_output_send(tp);
836 if (flags & PRUS_MORETOCOME)
837 tp->t_flags &= ~TF_MORETOCOME;
841 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
843 SOCKBUF_LOCK(&so->so_snd);
844 if (sbspace(&so->so_snd) < -512) {
845 SOCKBUF_UNLOCK(&so->so_snd);
851 * According to RFC961 (Assigned Protocols),
852 * the urgent pointer points to the last octet
853 * of urgent data. We continue, however,
854 * to consider it to indicate the first octet
855 * of data past the urgent section.
856 * Otherwise, snd_up should be one lower.
858 sbappendstream_locked(&so->so_snd, m);
859 SOCKBUF_UNLOCK(&so->so_snd);
860 if (nam && tp->t_state < TCPS_SYN_SENT) {
862 * Do implied connect if not yet connected,
863 * initialize window to default value, and
864 * initialize maxseg/maxopd using peer's cached
869 error = tcp6_connect(tp, nam, td);
871 #if defined(INET6) && defined(INET)
875 error = tcp_connect(tp, nam, td);
879 tp->snd_wnd = TTCP_CLIENT_SND_WND;
882 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
883 tp->t_flags |= TF_FORCEDATA;
884 error = tcp_output_send(tp);
885 tp->t_flags &= ~TF_FORCEDATA;
888 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
889 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
891 if (flags & PRUS_EOF)
892 INP_INFO_WUNLOCK(&V_tcbinfo);
897 * Abort the TCP. Drop the connection abruptly.
900 tcp_usr_abort(struct socket *so)
903 struct tcpcb *tp = NULL;
907 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
909 INP_INFO_WLOCK(&V_tcbinfo);
911 KASSERT(inp->inp_socket != NULL,
912 ("tcp_usr_abort: inp_socket == NULL"));
915 * If we still have full TCP state, and we're not dropped, drop.
917 if (!(inp->inp_flags & INP_TIMEWAIT) &&
918 !(inp->inp_flags & INP_DROPPED)) {
921 tcp_drop(tp, ECONNABORTED);
922 TCPDEBUG2(PRU_ABORT);
924 if (!(inp->inp_flags & INP_DROPPED)) {
926 so->so_state |= SS_PROTOREF;
928 inp->inp_flags |= INP_SOCKREF;
931 INP_INFO_WUNLOCK(&V_tcbinfo);
935 * TCP socket is closed. Start friendly disconnect.
938 tcp_usr_close(struct socket *so)
941 struct tcpcb *tp = NULL;
945 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
947 INP_INFO_WLOCK(&V_tcbinfo);
949 KASSERT(inp->inp_socket != NULL,
950 ("tcp_usr_close: inp_socket == NULL"));
953 * If we still have full TCP state, and we're not dropped, initiate
956 if (!(inp->inp_flags & INP_TIMEWAIT) &&
957 !(inp->inp_flags & INP_DROPPED)) {
961 TCPDEBUG2(PRU_CLOSE);
963 if (!(inp->inp_flags & INP_DROPPED)) {
965 so->so_state |= SS_PROTOREF;
967 inp->inp_flags |= INP_SOCKREF;
970 INP_INFO_WUNLOCK(&V_tcbinfo);
974 * Receive out-of-band data.
977 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
981 struct tcpcb *tp = NULL;
985 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
987 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
993 if ((so->so_oobmark == 0 &&
994 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
995 so->so_options & SO_OOBINLINE ||
996 tp->t_oobflags & TCPOOB_HADDATA) {
1000 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1001 error = EWOULDBLOCK;
1005 *mtod(m, caddr_t) = tp->t_iobc;
1006 if ((flags & MSG_PEEK) == 0)
1007 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1010 TCPDEBUG2(PRU_RCVOOB);
1016 struct pr_usrreqs tcp_usrreqs = {
1017 .pru_abort = tcp_usr_abort,
1018 .pru_accept = tcp_usr_accept,
1019 .pru_attach = tcp_usr_attach,
1020 .pru_bind = tcp_usr_bind,
1021 .pru_connect = tcp_usr_connect,
1022 .pru_control = in_control,
1023 .pru_detach = tcp_usr_detach,
1024 .pru_disconnect = tcp_usr_disconnect,
1025 .pru_listen = tcp_usr_listen,
1026 .pru_peeraddr = in_getpeeraddr,
1027 .pru_rcvd = tcp_usr_rcvd,
1028 .pru_rcvoob = tcp_usr_rcvoob,
1029 .pru_send = tcp_usr_send,
1030 .pru_shutdown = tcp_usr_shutdown,
1031 .pru_sockaddr = in_getsockaddr,
1032 .pru_sosetlabel = in_pcbsosetlabel,
1033 .pru_close = tcp_usr_close,
1038 struct pr_usrreqs tcp6_usrreqs = {
1039 .pru_abort = tcp_usr_abort,
1040 .pru_accept = tcp6_usr_accept,
1041 .pru_attach = tcp_usr_attach,
1042 .pru_bind = tcp6_usr_bind,
1043 .pru_connect = tcp6_usr_connect,
1044 .pru_control = in6_control,
1045 .pru_detach = tcp_usr_detach,
1046 .pru_disconnect = tcp_usr_disconnect,
1047 .pru_listen = tcp6_usr_listen,
1048 .pru_peeraddr = in6_mapped_peeraddr,
1049 .pru_rcvd = tcp_usr_rcvd,
1050 .pru_rcvoob = tcp_usr_rcvoob,
1051 .pru_send = tcp_usr_send,
1052 .pru_shutdown = tcp_usr_shutdown,
1053 .pru_sockaddr = in6_mapped_sockaddr,
1054 .pru_sosetlabel = in_pcbsosetlabel,
1055 .pru_close = tcp_usr_close,
1061 * Common subroutine to open a TCP connection to remote host specified
1062 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1063 * port number if needed. Call in_pcbconnect_setup to do the routing and
1064 * to choose a local host address (interface). If there is an existing
1065 * incarnation of the same connection in TIME-WAIT state and if the remote
1066 * host was sending CC options and if the connection duration was < MSL, then
1067 * truncate the previous TIME-WAIT state and proceed.
1068 * Initialize connection parameters and enter SYN-SENT state.
1071 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1073 struct inpcb *inp = tp->t_inpcb, *oinp;
1074 struct socket *so = inp->inp_socket;
1075 struct in_addr laddr;
1079 INP_WLOCK_ASSERT(inp);
1080 INP_HASH_WLOCK(&V_tcbinfo);
1082 if (inp->inp_lport == 0) {
1083 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1089 * Cannot simply call in_pcbconnect, because there might be an
1090 * earlier incarnation of this same connection still in
1091 * TIME_WAIT state, creating an ADDRINUSE error.
1093 laddr = inp->inp_laddr;
1094 lport = inp->inp_lport;
1095 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1096 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1097 if (error && oinp == NULL)
1103 inp->inp_laddr = laddr;
1105 INP_HASH_WUNLOCK(&V_tcbinfo);
1108 * Compute window scaling to request:
1109 * Scale to fit into sweet spot. See tcp_syncache.c.
1110 * XXX: This should move to tcp_output().
1112 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1113 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1114 tp->request_r_scale++;
1117 TCPSTAT_INC(tcps_connattempt);
1118 tp->t_state = TCPS_SYN_SENT;
1119 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1120 tp->iss = tcp_new_isn(tp);
1121 tcp_sendseqinit(tp);
1126 INP_HASH_WUNLOCK(&V_tcbinfo);
1133 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1135 struct inpcb *inp = tp->t_inpcb, *oinp;
1136 struct socket *so = inp->inp_socket;
1137 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1138 struct in6_addr addr6;
1141 INP_WLOCK_ASSERT(inp);
1142 INP_HASH_WLOCK(&V_tcbinfo);
1144 if (inp->inp_lport == 0) {
1145 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1151 * Cannot simply call in_pcbconnect, because there might be an
1152 * earlier incarnation of this same connection still in
1153 * TIME_WAIT state, creating an ADDRINUSE error.
1154 * in6_pcbladdr() also handles scope zone IDs.
1156 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1157 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1159 error = in6_pcbladdr(inp, nam, &addr6);
1162 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1163 &sin6->sin6_addr, sin6->sin6_port,
1164 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1167 inp->inp_lport, 0, NULL);
1172 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1173 inp->in6p_laddr = addr6;
1174 inp->in6p_faddr = sin6->sin6_addr;
1175 inp->inp_fport = sin6->sin6_port;
1176 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1177 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1178 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1180 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1182 INP_HASH_WUNLOCK(&V_tcbinfo);
1184 /* Compute window scaling to request. */
1185 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1186 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1187 tp->request_r_scale++;
1190 TCPSTAT_INC(tcps_connattempt);
1191 tp->t_state = TCPS_SYN_SENT;
1192 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1193 tp->iss = tcp_new_isn(tp);
1194 tcp_sendseqinit(tp);
1199 INP_HASH_WUNLOCK(&V_tcbinfo);
1205 * Export TCP internal state information via a struct tcp_info, based on the
1206 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1207 * (TCP state machine, etc). We export all information using FreeBSD-native
1208 * constants -- for example, the numeric values for tcpi_state will differ
1212 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1215 INP_WLOCK_ASSERT(tp->t_inpcb);
1216 bzero(ti, sizeof(*ti));
1218 ti->tcpi_state = tp->t_state;
1219 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1220 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1221 if (tp->t_flags & TF_SACK_PERMIT)
1222 ti->tcpi_options |= TCPI_OPT_SACK;
1223 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1224 ti->tcpi_options |= TCPI_OPT_WSCALE;
1225 ti->tcpi_snd_wscale = tp->snd_scale;
1226 ti->tcpi_rcv_wscale = tp->rcv_scale;
1229 ti->tcpi_rto = tp->t_rxtcur * tick;
1230 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1231 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1232 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1234 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1235 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1238 * FreeBSD-specific extension fields for tcp_info.
1240 ti->tcpi_rcv_space = tp->rcv_wnd;
1241 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1242 ti->tcpi_snd_wnd = tp->snd_wnd;
1243 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1244 ti->tcpi_snd_nxt = tp->snd_nxt;
1245 ti->tcpi_snd_mss = tp->t_maxseg;
1246 ti->tcpi_rcv_mss = tp->t_maxseg;
1247 if (tp->t_flags & TF_TOE)
1248 ti->tcpi_options |= TCPI_OPT_TOE;
1249 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1250 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1251 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1255 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1256 * socket option arguments. When it re-acquires the lock after the copy, it
1257 * has to revalidate that the connection is still valid for the socket
1260 #define INP_WLOCK_RECHECK(inp) do { \
1262 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1264 return (ECONNRESET); \
1266 tp = intotcpcb(inp); \
1270 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1272 int error, opt, optval;
1276 char buf[TCP_CA_NAME_MAX];
1277 struct cc_algo *algo;
1280 inp = sotoinpcb(so);
1281 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1283 if (sopt->sopt_level != IPPROTO_TCP) {
1285 if (inp->inp_vflag & INP_IPV6PROTO) {
1287 error = ip6_ctloutput(so, sopt);
1290 #if defined(INET6) && defined(INET)
1296 error = ip_ctloutput(so, sopt);
1301 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1303 return (ECONNRESET);
1306 switch (sopt->sopt_dir) {
1308 switch (sopt->sopt_name) {
1309 #ifdef TCP_SIGNATURE
1312 error = sooptcopyin(sopt, &optval, sizeof optval,
1317 INP_WLOCK_RECHECK(inp);
1319 tp->t_flags |= TF_SIGNATURE;
1321 tp->t_flags &= ~TF_SIGNATURE;
1324 #endif /* TCP_SIGNATURE */
1328 error = sooptcopyin(sopt, &optval, sizeof optval,
1333 INP_WLOCK_RECHECK(inp);
1334 switch (sopt->sopt_name) {
1342 opt = 0; /* dead code to fool gcc */
1349 tp->t_flags &= ~opt;
1355 error = sooptcopyin(sopt, &optval, sizeof optval,
1360 INP_WLOCK_RECHECK(inp);
1362 tp->t_flags |= TF_NOPUSH;
1363 else if (tp->t_flags & TF_NOPUSH) {
1364 tp->t_flags &= ~TF_NOPUSH;
1365 if (TCPS_HAVEESTABLISHED(tp->t_state))
1366 error = tcp_output(tp);
1373 error = sooptcopyin(sopt, &optval, sizeof optval,
1378 INP_WLOCK_RECHECK(inp);
1379 if (optval > 0 && optval <= tp->t_maxseg &&
1380 optval + 40 >= V_tcp_minmss)
1381 tp->t_maxseg = optval;
1392 case TCP_CONGESTION:
1394 bzero(buf, sizeof(buf));
1395 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1398 INP_WLOCK_RECHECK(inp);
1400 * Return EINVAL if we can't find the requested cc algo.
1404 STAILQ_FOREACH(algo, &cc_list, entries) {
1405 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1407 /* We've found the requested algo. */
1410 * We hold a write lock over the tcb
1411 * so it's safe to do these things
1412 * without ordering concerns.
1414 if (CC_ALGO(tp)->cb_destroy != NULL)
1415 CC_ALGO(tp)->cb_destroy(tp->ccv);
1418 * If something goes pear shaped
1419 * initialising the new algo,
1420 * fall back to newreno (which
1421 * does not require initialisation).
1423 if (algo->cb_init != NULL)
1424 if (algo->cb_init(tp->ccv) > 0) {
1425 CC_ALGO(tp) = &newreno_cc_algo;
1427 * The only reason init
1429 * because of malloc.
1433 break; /* Break the STAILQ_FOREACH. */
1442 error = ENOPROTOOPT;
1448 tp = intotcpcb(inp);
1449 switch (sopt->sopt_name) {
1450 #ifdef TCP_SIGNATURE
1452 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1454 error = sooptcopyout(sopt, &optval, sizeof optval);
1459 optval = tp->t_flags & TF_NODELAY;
1461 error = sooptcopyout(sopt, &optval, sizeof optval);
1464 optval = tp->t_maxseg;
1466 error = sooptcopyout(sopt, &optval, sizeof optval);
1469 optval = tp->t_flags & TF_NOOPT;
1471 error = sooptcopyout(sopt, &optval, sizeof optval);
1474 optval = tp->t_flags & TF_NOPUSH;
1476 error = sooptcopyout(sopt, &optval, sizeof optval);
1479 tcp_fill_info(tp, &ti);
1481 error = sooptcopyout(sopt, &ti, sizeof ti);
1483 case TCP_CONGESTION:
1484 bzero(buf, sizeof(buf));
1485 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1487 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1491 error = ENOPROTOOPT;
1498 #undef INP_WLOCK_RECHECK
1501 * tcp_sendspace and tcp_recvspace are the default send and receive window
1502 * sizes, respectively. These are obsolescent (this information should
1503 * be set by the route).
1505 u_long tcp_sendspace = 1024*32;
1506 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1507 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1508 u_long tcp_recvspace = 1024*64;
1509 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1510 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1513 * Attach TCP protocol to socket, allocating
1514 * internet protocol control block, tcp control block,
1515 * bufer space, and entering LISTEN state if to accept connections.
1518 tcp_attach(struct socket *so)
1524 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1525 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1529 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1530 so->so_snd.sb_flags |= SB_AUTOSIZE;
1531 INP_INFO_WLOCK(&V_tcbinfo);
1532 error = in_pcballoc(so, &V_tcbinfo);
1534 INP_INFO_WUNLOCK(&V_tcbinfo);
1537 inp = sotoinpcb(so);
1539 if (inp->inp_vflag & INP_IPV6PROTO) {
1540 inp->inp_vflag |= INP_IPV6;
1541 inp->in6p_hops = -1; /* use kernel default */
1545 inp->inp_vflag |= INP_IPV4;
1546 tp = tcp_newtcpcb(inp);
1550 INP_INFO_WUNLOCK(&V_tcbinfo);
1553 tp->t_state = TCPS_CLOSED;
1555 INP_INFO_WUNLOCK(&V_tcbinfo);
1560 * Initiate (or continue) disconnect.
1561 * If embryonic state, just send reset (once).
1562 * If in ``let data drain'' option and linger null, just drop.
1563 * Otherwise (hard), mark socket disconnecting and drop
1564 * current input data; switch states based on user close, and
1565 * send segment to peer (with FIN).
1568 tcp_disconnect(struct tcpcb *tp)
1570 struct inpcb *inp = tp->t_inpcb;
1571 struct socket *so = inp->inp_socket;
1573 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1574 INP_WLOCK_ASSERT(inp);
1577 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1578 * socket is still open.
1580 if (tp->t_state < TCPS_ESTABLISHED) {
1583 ("tcp_disconnect: tcp_close() returned NULL"));
1584 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1585 tp = tcp_drop(tp, 0);
1587 ("tcp_disconnect: tcp_drop() returned NULL"));
1589 soisdisconnecting(so);
1590 sbflush(&so->so_rcv);
1592 if (!(inp->inp_flags & INP_DROPPED))
1593 tcp_output_disconnect(tp);
1598 * User issued close, and wish to trail through shutdown states:
1599 * if never received SYN, just forget it. If got a SYN from peer,
1600 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1601 * If already got a FIN from peer, then almost done; go to LAST_ACK
1602 * state. In all other cases, have already sent FIN to peer (e.g.
1603 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1604 * for peer to send FIN or not respond to keep-alives, etc.
1605 * We can let the user exit from the close as soon as the FIN is acked.
1608 tcp_usrclosed(struct tcpcb *tp)
1611 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1612 INP_WLOCK_ASSERT(tp->t_inpcb);
1614 switch (tp->t_state) {
1616 tcp_offload_listen_close(tp);
1619 tp->t_state = TCPS_CLOSED;
1622 * tcp_close() should never return NULL here as the socket is
1626 ("tcp_usrclosed: tcp_close() returned NULL"));
1630 case TCPS_SYN_RECEIVED:
1631 tp->t_flags |= TF_NEEDFIN;
1634 case TCPS_ESTABLISHED:
1635 tp->t_state = TCPS_FIN_WAIT_1;
1638 case TCPS_CLOSE_WAIT:
1639 tp->t_state = TCPS_LAST_ACK;
1642 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1643 soisdisconnected(tp->t_inpcb->inp_socket);
1644 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1645 if (tp->t_state == TCPS_FIN_WAIT_2) {
1648 timeout = (tcp_fast_finwait2_recycle) ?
1649 tcp_finwait2_timeout : tcp_maxidle;
1650 tcp_timer_activate(tp, TT_2MSL, timeout);
1657 db_print_indent(int indent)
1661 for (i = 0; i < indent; i++)
1666 db_print_tstate(int t_state)
1671 db_printf("TCPS_CLOSED");
1675 db_printf("TCPS_LISTEN");
1679 db_printf("TCPS_SYN_SENT");
1682 case TCPS_SYN_RECEIVED:
1683 db_printf("TCPS_SYN_RECEIVED");
1686 case TCPS_ESTABLISHED:
1687 db_printf("TCPS_ESTABLISHED");
1690 case TCPS_CLOSE_WAIT:
1691 db_printf("TCPS_CLOSE_WAIT");
1694 case TCPS_FIN_WAIT_1:
1695 db_printf("TCPS_FIN_WAIT_1");
1699 db_printf("TCPS_CLOSING");
1703 db_printf("TCPS_LAST_ACK");
1706 case TCPS_FIN_WAIT_2:
1707 db_printf("TCPS_FIN_WAIT_2");
1710 case TCPS_TIME_WAIT:
1711 db_printf("TCPS_TIME_WAIT");
1715 db_printf("unknown");
1721 db_print_tflags(u_int t_flags)
1726 if (t_flags & TF_ACKNOW) {
1727 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1730 if (t_flags & TF_DELACK) {
1731 db_printf("%sTF_DELACK", comma ? ", " : "");
1734 if (t_flags & TF_NODELAY) {
1735 db_printf("%sTF_NODELAY", comma ? ", " : "");
1738 if (t_flags & TF_NOOPT) {
1739 db_printf("%sTF_NOOPT", comma ? ", " : "");
1742 if (t_flags & TF_SENTFIN) {
1743 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1746 if (t_flags & TF_REQ_SCALE) {
1747 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1750 if (t_flags & TF_RCVD_SCALE) {
1751 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1754 if (t_flags & TF_REQ_TSTMP) {
1755 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1758 if (t_flags & TF_RCVD_TSTMP) {
1759 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1762 if (t_flags & TF_SACK_PERMIT) {
1763 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1766 if (t_flags & TF_NEEDSYN) {
1767 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1770 if (t_flags & TF_NEEDFIN) {
1771 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1774 if (t_flags & TF_NOPUSH) {
1775 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1778 if (t_flags & TF_MORETOCOME) {
1779 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1782 if (t_flags & TF_LQ_OVERFLOW) {
1783 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1786 if (t_flags & TF_LASTIDLE) {
1787 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1790 if (t_flags & TF_RXWIN0SENT) {
1791 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1794 if (t_flags & TF_FASTRECOVERY) {
1795 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1798 if (t_flags & TF_CONGRECOVERY) {
1799 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1802 if (t_flags & TF_WASFRECOVERY) {
1803 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1806 if (t_flags & TF_SIGNATURE) {
1807 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1810 if (t_flags & TF_FORCEDATA) {
1811 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1814 if (t_flags & TF_TSO) {
1815 db_printf("%sTF_TSO", comma ? ", " : "");
1818 if (t_flags & TF_ECN_PERMIT) {
1819 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1825 db_print_toobflags(char t_oobflags)
1830 if (t_oobflags & TCPOOB_HAVEDATA) {
1831 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1834 if (t_oobflags & TCPOOB_HADDATA) {
1835 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1841 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1844 db_print_indent(indent);
1845 db_printf("%s at %p\n", name, tp);
1849 db_print_indent(indent);
1850 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1851 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1853 db_print_indent(indent);
1854 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1855 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1857 db_print_indent(indent);
1858 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1859 &tp->t_timers->tt_delack, tp->t_inpcb);
1861 db_print_indent(indent);
1862 db_printf("t_state: %d (", tp->t_state);
1863 db_print_tstate(tp->t_state);
1866 db_print_indent(indent);
1867 db_printf("t_flags: 0x%x (", tp->t_flags);
1868 db_print_tflags(tp->t_flags);
1871 db_print_indent(indent);
1872 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
1873 tp->snd_una, tp->snd_max, tp->snd_nxt);
1875 db_print_indent(indent);
1876 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
1877 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1879 db_print_indent(indent);
1880 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
1881 tp->iss, tp->irs, tp->rcv_nxt);
1883 db_print_indent(indent);
1884 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
1885 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1887 db_print_indent(indent);
1888 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
1889 tp->snd_wnd, tp->snd_cwnd);
1891 db_print_indent(indent);
1892 db_printf("snd_ssthresh: %lu snd_recover: "
1893 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
1895 db_print_indent(indent);
1896 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
1897 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1899 db_print_indent(indent);
1900 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
1901 tp->t_rtttime, tp->t_rtseq);
1903 db_print_indent(indent);
1904 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
1905 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
1907 db_print_indent(indent);
1908 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
1909 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1912 db_print_indent(indent);
1913 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
1914 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1916 db_print_indent(indent);
1917 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1918 db_print_toobflags(tp->t_oobflags);
1919 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
1921 db_print_indent(indent);
1922 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
1923 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1925 db_print_indent(indent);
1926 db_printf("ts_recent: %u ts_recent_age: %u\n",
1927 tp->ts_recent, tp->ts_recent_age);
1929 db_print_indent(indent);
1930 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
1931 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1933 db_print_indent(indent);
1934 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
1935 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
1936 tp->snd_recover_prev, tp->t_badrxtwin);
1938 db_print_indent(indent);
1939 db_printf("snd_numholes: %d snd_holes first: %p\n",
1940 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1942 db_print_indent(indent);
1943 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
1944 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1946 /* Skip sackblks, sackhint. */
1948 db_print_indent(indent);
1949 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
1950 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1953 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1958 db_printf("usage: show tcpcb <addr>\n");
1961 tp = (struct tcpcb *)addr;
1963 db_print_tcpcb(tp, "tcpcb", 0);