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/cc/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);
528 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
536 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
540 struct tcpcb *tp = NULL;
541 struct sockaddr_in6 *sin6p;
545 sin6p = (struct sockaddr_in6 *)nam;
546 if (nam->sa_len != sizeof (*sin6p))
549 * Must disallow TCP ``connections'' to multicast addresses.
551 if (sin6p->sin6_family == AF_INET6
552 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
553 return (EAFNOSUPPORT);
556 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
558 if (inp->inp_flags & INP_TIMEWAIT) {
562 if (inp->inp_flags & INP_DROPPED) {
563 error = ECONNREFUSED;
570 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
571 * therefore probably require the hash lock, which isn't held here.
572 * Is this a significant problem?
574 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
575 struct sockaddr_in sin;
577 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
582 in6_sin6_2_sin(&sin, sin6p);
583 inp->inp_vflag |= INP_IPV4;
584 inp->inp_vflag &= ~INP_IPV6;
585 if ((error = prison_remote_ip4(td->td_ucred,
586 &sin.sin_addr)) != 0)
588 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
591 if (registered_toedevs > 0 &&
592 (so->so_options & SO_NO_OFFLOAD) == 0 &&
593 (error = tcp_offload_connect(so, nam)) == 0)
596 error = tp->t_fb->tfb_tcp_output(tp);
600 inp->inp_vflag &= ~INP_IPV4;
601 inp->inp_vflag |= INP_IPV6;
602 inp->inp_inc.inc_flags |= INC_ISIPV6;
603 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
605 if ((error = tcp6_connect(tp, nam, td)) != 0)
608 if (registered_toedevs > 0 &&
609 (so->so_options & SO_NO_OFFLOAD) == 0 &&
610 (error = tcp_offload_connect(so, nam)) == 0)
613 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
614 error = tp->t_fb->tfb_tcp_output(tp);
617 TCPDEBUG2(PRU_CONNECT);
618 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
625 * Initiate disconnect from peer.
626 * If connection never passed embryonic stage, just drop;
627 * else if don't need to let data drain, then can just drop anyways,
628 * else have to begin TCP shutdown process: mark socket disconnecting,
629 * drain unread data, state switch to reflect user close, and
630 * send segment (e.g. FIN) to peer. Socket will be really disconnected
631 * when peer sends FIN and acks ours.
633 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
636 tcp_usr_disconnect(struct socket *so)
639 struct tcpcb *tp = NULL;
643 INP_INFO_RLOCK(&V_tcbinfo);
645 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
647 if (inp->inp_flags & INP_TIMEWAIT)
649 if (inp->inp_flags & INP_DROPPED) {
657 TCPDEBUG2(PRU_DISCONNECT);
658 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
660 INP_INFO_RUNLOCK(&V_tcbinfo);
666 * Accept a connection. Essentially all the work is done at higher levels;
667 * just return the address of the peer, storing through addr.
670 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
673 struct inpcb *inp = NULL;
674 struct tcpcb *tp = NULL;
679 if (so->so_state & SS_ISDISCONNECTED)
680 return (ECONNABORTED);
683 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
685 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
686 error = ECONNABORTED;
693 * We inline in_getpeeraddr and COMMON_END here, so that we can
694 * copy the data of interest and defer the malloc until after we
697 port = inp->inp_fport;
698 addr = inp->inp_faddr;
701 TCPDEBUG2(PRU_ACCEPT);
702 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
705 *nam = in_sockaddr(port, &addr);
712 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
714 struct inpcb *inp = NULL;
716 struct tcpcb *tp = NULL;
718 struct in6_addr addr6;
723 if (so->so_state & SS_ISDISCONNECTED)
724 return (ECONNABORTED);
727 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
728 INP_INFO_RLOCK(&V_tcbinfo);
730 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
731 error = ECONNABORTED;
738 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
739 * copy the data of interest and defer the malloc until after we
742 if (inp->inp_vflag & INP_IPV4) {
744 port = inp->inp_fport;
745 addr = inp->inp_faddr;
747 port = inp->inp_fport;
748 addr6 = inp->in6p_faddr;
752 TCPDEBUG2(PRU_ACCEPT);
753 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
755 INP_INFO_RUNLOCK(&V_tcbinfo);
758 *nam = in6_v4mapsin6_sockaddr(port, &addr);
760 *nam = in6_sockaddr(port, &addr6);
767 * Mark the connection as being incapable of further output.
770 tcp_usr_shutdown(struct socket *so)
774 struct tcpcb *tp = NULL;
777 INP_INFO_RLOCK(&V_tcbinfo);
779 KASSERT(inp != NULL, ("inp == NULL"));
781 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
789 if (!(inp->inp_flags & INP_DROPPED))
790 error = tp->t_fb->tfb_tcp_output(tp);
793 TCPDEBUG2(PRU_SHUTDOWN);
794 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
796 INP_INFO_RUNLOCK(&V_tcbinfo);
802 * After a receive, possibly send window update to peer.
805 tcp_usr_rcvd(struct socket *so, int flags)
808 struct tcpcb *tp = NULL;
813 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
815 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
823 * For passively-created TFO connections, don't attempt a window
824 * update while still in SYN_RECEIVED as this may trigger an early
825 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
826 * application response data, or failing that, when the DELACK timer
829 if ((tp->t_flags & TF_FASTOPEN) &&
830 (tp->t_state == TCPS_SYN_RECEIVED))
834 if (tp->t_flags & TF_TOE)
835 tcp_offload_rcvd(tp);
838 tp->t_fb->tfb_tcp_output(tp);
842 TCP_PROBE2(debug__user, tp, PRU_RCVD);
848 * Do a send by putting data in output queue and updating urgent
849 * marker if URG set. Possibly send more data. Unlike the other
850 * pru_*() routines, the mbuf chains are our responsibility. We
851 * must either enqueue them or free them. The other pru_* routines
852 * generally are caller-frees.
855 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
856 struct sockaddr *nam, struct mbuf *control, struct thread *td)
860 struct tcpcb *tp = NULL;
867 * We require the pcbinfo lock if we will close the socket as part of
870 if (flags & PRUS_EOF)
871 INP_INFO_RLOCK(&V_tcbinfo);
873 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
875 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
879 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
880 * for freeing memory.
882 if (m && (flags & PRUS_NOTREADY) == 0)
888 isipv6 = nam && nam->sa_family == AF_INET6;
893 /* TCP doesn't do control messages (rights, creds, etc) */
894 if (control->m_len) {
901 m_freem(control); /* empty control, just free it */
903 if (!(flags & PRUS_OOB)) {
904 sbappendstream(&so->so_snd, m, flags);
905 if (nam && tp->t_state < TCPS_SYN_SENT) {
907 * Do implied connect if not yet connected,
908 * initialize window to default value, and
909 * initialize maxseg using peer's cached MSS.
913 error = tcp6_connect(tp, nam, td);
915 #if defined(INET6) && defined(INET)
919 error = tcp_connect(tp, nam, td);
923 tp->snd_wnd = TTCP_CLIENT_SND_WND;
926 if (flags & PRUS_EOF) {
928 * Close the send side of the connection after
931 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
935 if (!(inp->inp_flags & INP_DROPPED) &&
936 !(flags & PRUS_NOTREADY)) {
937 if (flags & PRUS_MORETOCOME)
938 tp->t_flags |= TF_MORETOCOME;
939 error = tp->t_fb->tfb_tcp_output(tp);
940 if (flags & PRUS_MORETOCOME)
941 tp->t_flags &= ~TF_MORETOCOME;
945 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
947 SOCKBUF_LOCK(&so->so_snd);
948 if (sbspace(&so->so_snd) < -512) {
949 SOCKBUF_UNLOCK(&so->so_snd);
955 * According to RFC961 (Assigned Protocols),
956 * the urgent pointer points to the last octet
957 * of urgent data. We continue, however,
958 * to consider it to indicate the first octet
959 * of data past the urgent section.
960 * Otherwise, snd_up should be one lower.
962 sbappendstream_locked(&so->so_snd, m, flags);
963 SOCKBUF_UNLOCK(&so->so_snd);
964 if (nam && tp->t_state < TCPS_SYN_SENT) {
966 * Do implied connect if not yet connected,
967 * initialize window to default value, and
968 * initialize maxseg using peer's cached MSS.
972 error = tcp6_connect(tp, nam, td);
974 #if defined(INET6) && defined(INET)
978 error = tcp_connect(tp, nam, td);
982 tp->snd_wnd = TTCP_CLIENT_SND_WND;
985 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
986 if (!(flags & PRUS_NOTREADY)) {
987 tp->t_flags |= TF_FORCEDATA;
988 error = tp->t_fb->tfb_tcp_output(tp);
989 tp->t_flags &= ~TF_FORCEDATA;
993 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
994 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
995 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
996 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
998 if (flags & PRUS_EOF)
999 INP_INFO_RUNLOCK(&V_tcbinfo);
1004 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1010 inp = sotoinpcb(so);
1012 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1014 for (int i = 0; i < count; i++)
1016 return (ECONNRESET);
1018 tp = intotcpcb(inp);
1020 SOCKBUF_LOCK(&so->so_snd);
1021 error = sbready(&so->so_snd, m, count);
1022 SOCKBUF_UNLOCK(&so->so_snd);
1024 error = tp->t_fb->tfb_tcp_output(tp);
1031 * Abort the TCP. Drop the connection abruptly.
1034 tcp_usr_abort(struct socket *so)
1037 struct tcpcb *tp = NULL;
1040 inp = sotoinpcb(so);
1041 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1043 INP_INFO_RLOCK(&V_tcbinfo);
1045 KASSERT(inp->inp_socket != NULL,
1046 ("tcp_usr_abort: inp_socket == NULL"));
1049 * If we still have full TCP state, and we're not dropped, drop.
1051 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1052 !(inp->inp_flags & INP_DROPPED)) {
1053 tp = intotcpcb(inp);
1055 tcp_drop(tp, ECONNABORTED);
1056 TCPDEBUG2(PRU_ABORT);
1057 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1059 if (!(inp->inp_flags & INP_DROPPED)) {
1061 so->so_state |= SS_PROTOREF;
1063 inp->inp_flags |= INP_SOCKREF;
1066 INP_INFO_RUNLOCK(&V_tcbinfo);
1070 * TCP socket is closed. Start friendly disconnect.
1073 tcp_usr_close(struct socket *so)
1076 struct tcpcb *tp = NULL;
1079 inp = sotoinpcb(so);
1080 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1082 INP_INFO_RLOCK(&V_tcbinfo);
1084 KASSERT(inp->inp_socket != NULL,
1085 ("tcp_usr_close: inp_socket == NULL"));
1088 * If we still have full TCP state, and we're not dropped, initiate
1091 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1092 !(inp->inp_flags & INP_DROPPED)) {
1093 tp = intotcpcb(inp);
1096 TCPDEBUG2(PRU_CLOSE);
1097 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1099 if (!(inp->inp_flags & INP_DROPPED)) {
1101 so->so_state |= SS_PROTOREF;
1103 inp->inp_flags |= INP_SOCKREF;
1106 INP_INFO_RUNLOCK(&V_tcbinfo);
1110 * Receive out-of-band data.
1113 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1117 struct tcpcb *tp = NULL;
1120 inp = sotoinpcb(so);
1121 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1123 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1127 tp = intotcpcb(inp);
1129 if ((so->so_oobmark == 0 &&
1130 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1131 so->so_options & SO_OOBINLINE ||
1132 tp->t_oobflags & TCPOOB_HADDATA) {
1136 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1137 error = EWOULDBLOCK;
1141 *mtod(m, caddr_t) = tp->t_iobc;
1142 if ((flags & MSG_PEEK) == 0)
1143 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1146 TCPDEBUG2(PRU_RCVOOB);
1147 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1153 struct pr_usrreqs tcp_usrreqs = {
1154 .pru_abort = tcp_usr_abort,
1155 .pru_accept = tcp_usr_accept,
1156 .pru_attach = tcp_usr_attach,
1157 .pru_bind = tcp_usr_bind,
1158 .pru_connect = tcp_usr_connect,
1159 .pru_control = in_control,
1160 .pru_detach = tcp_usr_detach,
1161 .pru_disconnect = tcp_usr_disconnect,
1162 .pru_listen = tcp_usr_listen,
1163 .pru_peeraddr = in_getpeeraddr,
1164 .pru_rcvd = tcp_usr_rcvd,
1165 .pru_rcvoob = tcp_usr_rcvoob,
1166 .pru_send = tcp_usr_send,
1167 .pru_ready = tcp_usr_ready,
1168 .pru_shutdown = tcp_usr_shutdown,
1169 .pru_sockaddr = in_getsockaddr,
1170 .pru_sosetlabel = in_pcbsosetlabel,
1171 .pru_close = tcp_usr_close,
1176 struct pr_usrreqs tcp6_usrreqs = {
1177 .pru_abort = tcp_usr_abort,
1178 .pru_accept = tcp6_usr_accept,
1179 .pru_attach = tcp_usr_attach,
1180 .pru_bind = tcp6_usr_bind,
1181 .pru_connect = tcp6_usr_connect,
1182 .pru_control = in6_control,
1183 .pru_detach = tcp_usr_detach,
1184 .pru_disconnect = tcp_usr_disconnect,
1185 .pru_listen = tcp6_usr_listen,
1186 .pru_peeraddr = in6_mapped_peeraddr,
1187 .pru_rcvd = tcp_usr_rcvd,
1188 .pru_rcvoob = tcp_usr_rcvoob,
1189 .pru_send = tcp_usr_send,
1190 .pru_ready = tcp_usr_ready,
1191 .pru_shutdown = tcp_usr_shutdown,
1192 .pru_sockaddr = in6_mapped_sockaddr,
1193 .pru_sosetlabel = in_pcbsosetlabel,
1194 .pru_close = tcp_usr_close,
1200 * Common subroutine to open a TCP connection to remote host specified
1201 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1202 * port number if needed. Call in_pcbconnect_setup to do the routing and
1203 * to choose a local host address (interface). If there is an existing
1204 * incarnation of the same connection in TIME-WAIT state and if the remote
1205 * host was sending CC options and if the connection duration was < MSL, then
1206 * truncate the previous TIME-WAIT state and proceed.
1207 * Initialize connection parameters and enter SYN-SENT state.
1210 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1212 struct inpcb *inp = tp->t_inpcb, *oinp;
1213 struct socket *so = inp->inp_socket;
1214 struct in_addr laddr;
1218 INP_WLOCK_ASSERT(inp);
1219 INP_HASH_WLOCK(&V_tcbinfo);
1221 if (inp->inp_lport == 0) {
1222 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1228 * Cannot simply call in_pcbconnect, because there might be an
1229 * earlier incarnation of this same connection still in
1230 * TIME_WAIT state, creating an ADDRINUSE error.
1232 laddr = inp->inp_laddr;
1233 lport = inp->inp_lport;
1234 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1235 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1236 if (error && oinp == NULL)
1242 inp->inp_laddr = laddr;
1244 INP_HASH_WUNLOCK(&V_tcbinfo);
1247 * Compute window scaling to request:
1248 * Scale to fit into sweet spot. See tcp_syncache.c.
1249 * XXX: This should move to tcp_output().
1251 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1252 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1253 tp->request_r_scale++;
1256 TCPSTAT_INC(tcps_connattempt);
1257 tcp_state_change(tp, TCPS_SYN_SENT);
1258 tp->iss = tcp_new_isn(tp);
1259 tcp_sendseqinit(tp);
1264 INP_HASH_WUNLOCK(&V_tcbinfo);
1271 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1273 struct inpcb *inp = tp->t_inpcb;
1276 INP_WLOCK_ASSERT(inp);
1277 INP_HASH_WLOCK(&V_tcbinfo);
1279 if (inp->inp_lport == 0) {
1280 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1284 error = in6_pcbconnect(inp, nam, td->td_ucred);
1287 INP_HASH_WUNLOCK(&V_tcbinfo);
1289 /* Compute window scaling to request. */
1290 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1291 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1292 tp->request_r_scale++;
1294 soisconnecting(inp->inp_socket);
1295 TCPSTAT_INC(tcps_connattempt);
1296 tcp_state_change(tp, TCPS_SYN_SENT);
1297 tp->iss = tcp_new_isn(tp);
1298 tcp_sendseqinit(tp);
1303 INP_HASH_WUNLOCK(&V_tcbinfo);
1309 * Export TCP internal state information via a struct tcp_info, based on the
1310 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1311 * (TCP state machine, etc). We export all information using FreeBSD-native
1312 * constants -- for example, the numeric values for tcpi_state will differ
1316 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1319 INP_WLOCK_ASSERT(tp->t_inpcb);
1320 bzero(ti, sizeof(*ti));
1322 ti->tcpi_state = tp->t_state;
1323 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1324 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1325 if (tp->t_flags & TF_SACK_PERMIT)
1326 ti->tcpi_options |= TCPI_OPT_SACK;
1327 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1328 ti->tcpi_options |= TCPI_OPT_WSCALE;
1329 ti->tcpi_snd_wscale = tp->snd_scale;
1330 ti->tcpi_rcv_wscale = tp->rcv_scale;
1333 ti->tcpi_rto = tp->t_rxtcur * tick;
1334 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1335 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1336 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1338 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1339 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1342 * FreeBSD-specific extension fields for tcp_info.
1344 ti->tcpi_rcv_space = tp->rcv_wnd;
1345 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1346 ti->tcpi_snd_wnd = tp->snd_wnd;
1347 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1348 ti->tcpi_snd_nxt = tp->snd_nxt;
1349 ti->tcpi_snd_mss = tp->t_maxseg;
1350 ti->tcpi_rcv_mss = tp->t_maxseg;
1351 if (tp->t_flags & TF_TOE)
1352 ti->tcpi_options |= TCPI_OPT_TOE;
1353 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1354 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1355 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1359 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1360 * socket option arguments. When it re-acquires the lock after the copy, it
1361 * has to revalidate that the connection is still valid for the socket
1364 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1366 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1369 return (ECONNRESET); \
1371 tp = intotcpcb(inp); \
1373 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1376 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1381 struct tcp_function_block *blk;
1382 struct tcp_function_set fsn;
1385 inp = sotoinpcb(so);
1386 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1388 if (sopt->sopt_level != IPPROTO_TCP) {
1390 if (inp->inp_vflag & INP_IPV6PROTO) {
1392 error = ip6_ctloutput(so, sopt);
1395 #if defined(INET6) && defined(INET)
1401 error = ip_ctloutput(so, sopt);
1406 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1408 return (ECONNRESET);
1410 tp = intotcpcb(inp);
1412 * Protect the TCP option TCP_FUNCTION_BLK so
1413 * that a sub-function can *never* overwrite this.
1415 if ((sopt->sopt_dir == SOPT_SET) &&
1416 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1418 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1422 INP_WLOCK_RECHECK(inp);
1423 blk = find_and_ref_tcp_functions(&fsn);
1428 if (tp->t_fb == blk) {
1429 /* You already have this */
1430 refcount_release(&blk->tfb_refcnt);
1434 if (tp->t_state != TCPS_CLOSED) {
1437 * The user has advanced the state
1438 * past the initial point, we may not
1439 * be able to switch.
1441 if (blk->tfb_tcp_handoff_ok != NULL) {
1443 * Does the stack provide a
1444 * query mechanism, if so it may
1445 * still be possible?
1447 error = (*blk->tfb_tcp_handoff_ok)(tp);
1450 refcount_release(&blk->tfb_refcnt);
1455 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1456 refcount_release(&blk->tfb_refcnt);
1461 * Release the old refcnt, the
1462 * lookup acquired a ref on the
1465 if (tp->t_fb->tfb_tcp_fb_fini) {
1467 * Tell the stack to cleanup with 0 i.e.
1468 * the tcb is not going away.
1470 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1472 refcount_release(&tp->t_fb->tfb_refcnt);
1474 if (tp->t_fb->tfb_tcp_fb_init) {
1475 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1478 if (tp->t_flags & TF_TOE) {
1479 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1485 } else if ((sopt->sopt_dir == SOPT_GET) &&
1486 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1487 strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
1488 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1490 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1493 /* Pass in the INP locked, called must unlock it */
1494 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1498 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1500 int error, opt, optval;
1503 struct cc_algo *algo;
1504 char *pbuf, buf[TCP_CA_NAME_MAX];
1508 * For TCP_CCALGOOPT forward the control to CC module, for both
1509 * SOPT_SET and SOPT_GET.
1511 switch (sopt->sopt_name) {
1514 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1515 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1516 sopt->sopt_valsize);
1521 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1522 if (CC_ALGO(tp)->ctl_output != NULL)
1523 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1527 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1528 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1533 switch (sopt->sopt_dir) {
1535 switch (sopt->sopt_name) {
1536 #ifdef TCP_SIGNATURE
1539 error = sooptcopyin(sopt, &optval, sizeof optval,
1544 INP_WLOCK_RECHECK(inp);
1546 tp->t_flags |= TF_SIGNATURE;
1548 tp->t_flags &= ~TF_SIGNATURE;
1549 goto unlock_and_done;
1550 #endif /* TCP_SIGNATURE */
1555 error = sooptcopyin(sopt, &optval, sizeof optval,
1560 INP_WLOCK_RECHECK(inp);
1561 switch (sopt->sopt_name) {
1569 opt = 0; /* dead code to fool gcc */
1576 tp->t_flags &= ~opt;
1579 if (tp->t_flags & TF_TOE) {
1580 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1589 error = sooptcopyin(sopt, &optval, sizeof optval,
1594 INP_WLOCK_RECHECK(inp);
1596 tp->t_flags |= TF_NOPUSH;
1597 else if (tp->t_flags & TF_NOPUSH) {
1598 tp->t_flags &= ~TF_NOPUSH;
1599 if (TCPS_HAVEESTABLISHED(tp->t_state))
1600 error = tp->t_fb->tfb_tcp_output(tp);
1602 goto unlock_and_done;
1606 error = sooptcopyin(sopt, &optval, sizeof optval,
1611 INP_WLOCK_RECHECK(inp);
1612 if (optval > 0 && optval <= tp->t_maxseg &&
1613 optval + 40 >= V_tcp_minmss)
1614 tp->t_maxseg = optval;
1617 goto unlock_and_done;
1624 case TCP_CONGESTION:
1626 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1629 buf[sopt->sopt_valsize] = '\0';
1630 INP_WLOCK_RECHECK(inp);
1632 STAILQ_FOREACH(algo, &cc_list, entries)
1633 if (strncmp(buf, algo->name,
1634 TCP_CA_NAME_MAX) == 0)
1643 * We hold a write lock over the tcb so it's safe to
1644 * do these things without ordering concerns.
1646 if (CC_ALGO(tp)->cb_destroy != NULL)
1647 CC_ALGO(tp)->cb_destroy(tp->ccv);
1650 * If something goes pear shaped initialising the new
1651 * algo, fall back to newreno (which does not
1652 * require initialisation).
1654 if (algo->cb_init != NULL &&
1655 algo->cb_init(tp->ccv) != 0) {
1656 CC_ALGO(tp) = &newreno_cc_algo;
1658 * The only reason init should fail is
1659 * because of malloc.
1670 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1674 if (ui > (UINT_MAX / hz)) {
1680 INP_WLOCK_RECHECK(inp);
1681 switch (sopt->sopt_name) {
1683 tp->t_keepidle = ui;
1685 * XXX: better check current remaining
1686 * timeout and "merge" it with new value.
1688 if ((tp->t_state > TCPS_LISTEN) &&
1689 (tp->t_state <= TCPS_CLOSING))
1690 tcp_timer_activate(tp, TT_KEEP,
1694 tp->t_keepintvl = ui;
1695 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1696 (TP_MAXIDLE(tp) > 0))
1697 tcp_timer_activate(tp, TT_2MSL,
1701 tp->t_keepinit = ui;
1702 if (tp->t_state == TCPS_SYN_RECEIVED ||
1703 tp->t_state == TCPS_SYN_SENT)
1704 tcp_timer_activate(tp, TT_KEEP,
1708 goto unlock_and_done;
1712 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1716 INP_WLOCK_RECHECK(inp);
1718 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1719 (TP_MAXIDLE(tp) > 0))
1720 tcp_timer_activate(tp, TT_2MSL,
1722 goto unlock_and_done;
1728 error = sooptcopyin(sopt, &optval, sizeof optval,
1733 INP_WLOCK_RECHECK(inp);
1735 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1736 &(tp->t_outpkts) : &(tp->t_inpkts),
1740 goto unlock_and_done;
1746 if (!V_tcp_fastopen_enabled)
1749 error = sooptcopyin(sopt, &optval, sizeof optval,
1754 INP_WLOCK_RECHECK(inp);
1756 tp->t_flags |= TF_FASTOPEN;
1757 if ((tp->t_state == TCPS_LISTEN) &&
1758 (tp->t_tfo_pending == NULL))
1760 tcp_fastopen_alloc_counter();
1762 tp->t_flags &= ~TF_FASTOPEN;
1763 goto unlock_and_done;
1768 error = ENOPROTOOPT;
1774 tp = intotcpcb(inp);
1775 switch (sopt->sopt_name) {
1776 #ifdef TCP_SIGNATURE
1778 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1780 error = sooptcopyout(sopt, &optval, sizeof optval);
1785 optval = tp->t_flags & TF_NODELAY;
1787 error = sooptcopyout(sopt, &optval, sizeof optval);
1790 optval = tp->t_maxseg;
1792 error = sooptcopyout(sopt, &optval, sizeof optval);
1795 optval = tp->t_flags & TF_NOOPT;
1797 error = sooptcopyout(sopt, &optval, sizeof optval);
1800 optval = tp->t_flags & TF_NOPUSH;
1802 error = sooptcopyout(sopt, &optval, sizeof optval);
1805 tcp_fill_info(tp, &ti);
1807 error = sooptcopyout(sopt, &ti, sizeof ti);
1809 case TCP_CONGESTION:
1810 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1812 error = sooptcopyout(sopt, buf, len + 1);
1818 switch (sopt->sopt_name) {
1820 ui = tp->t_keepidle / hz;
1823 ui = tp->t_keepintvl / hz;
1826 ui = tp->t_keepinit / hz;
1833 error = sooptcopyout(sopt, &ui, sizeof(ui));
1838 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1839 &(tp->t_outpkts) : &(tp->t_inpkts));
1841 error = sooptcopyout(sopt, &optval, sizeof optval);
1847 optval = tp->t_flags & TF_FASTOPEN;
1849 error = sooptcopyout(sopt, &optval, sizeof optval);
1854 error = ENOPROTOOPT;
1861 #undef INP_WLOCK_RECHECK
1862 #undef INP_WLOCK_RECHECK_CLEANUP
1865 * Attach TCP protocol to socket, allocating
1866 * internet protocol control block, tcp control block,
1867 * bufer space, and entering LISTEN state if to accept connections.
1870 tcp_attach(struct socket *so)
1876 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1877 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1881 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1882 so->so_snd.sb_flags |= SB_AUTOSIZE;
1883 INP_INFO_RLOCK(&V_tcbinfo);
1884 error = in_pcballoc(so, &V_tcbinfo);
1886 INP_INFO_RUNLOCK(&V_tcbinfo);
1889 inp = sotoinpcb(so);
1891 if (inp->inp_vflag & INP_IPV6PROTO) {
1892 inp->inp_vflag |= INP_IPV6;
1893 inp->in6p_hops = -1; /* use kernel default */
1897 inp->inp_vflag |= INP_IPV4;
1898 tp = tcp_newtcpcb(inp);
1902 INP_INFO_RUNLOCK(&V_tcbinfo);
1905 tp->t_state = TCPS_CLOSED;
1907 INP_INFO_RUNLOCK(&V_tcbinfo);
1908 TCPSTATES_INC(TCPS_CLOSED);
1913 * Initiate (or continue) disconnect.
1914 * If embryonic state, just send reset (once).
1915 * If in ``let data drain'' option and linger null, just drop.
1916 * Otherwise (hard), mark socket disconnecting and drop
1917 * current input data; switch states based on user close, and
1918 * send segment to peer (with FIN).
1921 tcp_disconnect(struct tcpcb *tp)
1923 struct inpcb *inp = tp->t_inpcb;
1924 struct socket *so = inp->inp_socket;
1926 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1927 INP_WLOCK_ASSERT(inp);
1930 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1931 * socket is still open.
1933 if (tp->t_state < TCPS_ESTABLISHED) {
1936 ("tcp_disconnect: tcp_close() returned NULL"));
1937 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1938 tp = tcp_drop(tp, 0);
1940 ("tcp_disconnect: tcp_drop() returned NULL"));
1942 soisdisconnecting(so);
1943 sbflush(&so->so_rcv);
1945 if (!(inp->inp_flags & INP_DROPPED))
1946 tp->t_fb->tfb_tcp_output(tp);
1951 * User issued close, and wish to trail through shutdown states:
1952 * if never received SYN, just forget it. If got a SYN from peer,
1953 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1954 * If already got a FIN from peer, then almost done; go to LAST_ACK
1955 * state. In all other cases, have already sent FIN to peer (e.g.
1956 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1957 * for peer to send FIN or not respond to keep-alives, etc.
1958 * We can let the user exit from the close as soon as the FIN is acked.
1961 tcp_usrclosed(struct tcpcb *tp)
1964 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1965 INP_WLOCK_ASSERT(tp->t_inpcb);
1967 switch (tp->t_state) {
1970 tcp_offload_listen_stop(tp);
1972 tcp_state_change(tp, TCPS_CLOSED);
1977 * tcp_close() should never return NULL here as the socket is
1981 ("tcp_usrclosed: tcp_close() returned NULL"));
1985 case TCPS_SYN_RECEIVED:
1986 tp->t_flags |= TF_NEEDFIN;
1989 case TCPS_ESTABLISHED:
1990 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1993 case TCPS_CLOSE_WAIT:
1994 tcp_state_change(tp, TCPS_LAST_ACK);
1997 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1998 soisdisconnected(tp->t_inpcb->inp_socket);
1999 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2000 if (tp->t_state == TCPS_FIN_WAIT_2) {
2003 timeout = (tcp_fast_finwait2_recycle) ?
2004 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2005 tcp_timer_activate(tp, TT_2MSL, timeout);
2012 db_print_indent(int indent)
2016 for (i = 0; i < indent; i++)
2021 db_print_tstate(int t_state)
2026 db_printf("TCPS_CLOSED");
2030 db_printf("TCPS_LISTEN");
2034 db_printf("TCPS_SYN_SENT");
2037 case TCPS_SYN_RECEIVED:
2038 db_printf("TCPS_SYN_RECEIVED");
2041 case TCPS_ESTABLISHED:
2042 db_printf("TCPS_ESTABLISHED");
2045 case TCPS_CLOSE_WAIT:
2046 db_printf("TCPS_CLOSE_WAIT");
2049 case TCPS_FIN_WAIT_1:
2050 db_printf("TCPS_FIN_WAIT_1");
2054 db_printf("TCPS_CLOSING");
2058 db_printf("TCPS_LAST_ACK");
2061 case TCPS_FIN_WAIT_2:
2062 db_printf("TCPS_FIN_WAIT_2");
2065 case TCPS_TIME_WAIT:
2066 db_printf("TCPS_TIME_WAIT");
2070 db_printf("unknown");
2076 db_print_tflags(u_int t_flags)
2081 if (t_flags & TF_ACKNOW) {
2082 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2085 if (t_flags & TF_DELACK) {
2086 db_printf("%sTF_DELACK", comma ? ", " : "");
2089 if (t_flags & TF_NODELAY) {
2090 db_printf("%sTF_NODELAY", comma ? ", " : "");
2093 if (t_flags & TF_NOOPT) {
2094 db_printf("%sTF_NOOPT", comma ? ", " : "");
2097 if (t_flags & TF_SENTFIN) {
2098 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2101 if (t_flags & TF_REQ_SCALE) {
2102 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2105 if (t_flags & TF_RCVD_SCALE) {
2106 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2109 if (t_flags & TF_REQ_TSTMP) {
2110 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2113 if (t_flags & TF_RCVD_TSTMP) {
2114 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2117 if (t_flags & TF_SACK_PERMIT) {
2118 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2121 if (t_flags & TF_NEEDSYN) {
2122 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2125 if (t_flags & TF_NEEDFIN) {
2126 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2129 if (t_flags & TF_NOPUSH) {
2130 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2133 if (t_flags & TF_MORETOCOME) {
2134 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2137 if (t_flags & TF_LQ_OVERFLOW) {
2138 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2141 if (t_flags & TF_LASTIDLE) {
2142 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2145 if (t_flags & TF_RXWIN0SENT) {
2146 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2149 if (t_flags & TF_FASTRECOVERY) {
2150 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2153 if (t_flags & TF_CONGRECOVERY) {
2154 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2157 if (t_flags & TF_WASFRECOVERY) {
2158 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2161 if (t_flags & TF_SIGNATURE) {
2162 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2165 if (t_flags & TF_FORCEDATA) {
2166 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2169 if (t_flags & TF_TSO) {
2170 db_printf("%sTF_TSO", comma ? ", " : "");
2173 if (t_flags & TF_ECN_PERMIT) {
2174 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2177 if (t_flags & TF_FASTOPEN) {
2178 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2184 db_print_toobflags(char t_oobflags)
2189 if (t_oobflags & TCPOOB_HAVEDATA) {
2190 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2193 if (t_oobflags & TCPOOB_HADDATA) {
2194 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2200 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2203 db_print_indent(indent);
2204 db_printf("%s at %p\n", name, tp);
2208 db_print_indent(indent);
2209 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2210 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2212 db_print_indent(indent);
2213 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2214 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2216 db_print_indent(indent);
2217 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2218 &tp->t_timers->tt_delack, tp->t_inpcb);
2220 db_print_indent(indent);
2221 db_printf("t_state: %d (", tp->t_state);
2222 db_print_tstate(tp->t_state);
2225 db_print_indent(indent);
2226 db_printf("t_flags: 0x%x (", tp->t_flags);
2227 db_print_tflags(tp->t_flags);
2230 db_print_indent(indent);
2231 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2232 tp->snd_una, tp->snd_max, tp->snd_nxt);
2234 db_print_indent(indent);
2235 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2236 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2238 db_print_indent(indent);
2239 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2240 tp->iss, tp->irs, tp->rcv_nxt);
2242 db_print_indent(indent);
2243 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2244 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2246 db_print_indent(indent);
2247 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2248 tp->snd_wnd, tp->snd_cwnd);
2250 db_print_indent(indent);
2251 db_printf("snd_ssthresh: %lu snd_recover: "
2252 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2254 db_print_indent(indent);
2255 db_printf("t_rcvtime: %u t_startime: %u\n",
2256 tp->t_rcvtime, tp->t_starttime);
2258 db_print_indent(indent);
2259 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2260 tp->t_rtttime, tp->t_rtseq);
2262 db_print_indent(indent);
2263 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2264 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2266 db_print_indent(indent);
2267 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2268 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2271 db_print_indent(indent);
2272 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2273 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2275 db_print_indent(indent);
2276 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2277 db_print_toobflags(tp->t_oobflags);
2278 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2280 db_print_indent(indent);
2281 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2282 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2284 db_print_indent(indent);
2285 db_printf("ts_recent: %u ts_recent_age: %u\n",
2286 tp->ts_recent, tp->ts_recent_age);
2288 db_print_indent(indent);
2289 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2290 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2292 db_print_indent(indent);
2293 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2294 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2295 tp->snd_recover_prev, tp->t_badrxtwin);
2297 db_print_indent(indent);
2298 db_printf("snd_numholes: %d snd_holes first: %p\n",
2299 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2301 db_print_indent(indent);
2302 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2303 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2305 /* Skip sackblks, sackhint. */
2307 db_print_indent(indent);
2308 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2309 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2312 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2317 db_printf("usage: show tcpcb <addr>\n");
2320 tp = (struct tcpcb *)addr;
2322 db_print_tcpcb(tp, "tcpcb", 0);