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/cc.h>
73 #include <netinet/in.h>
74 #include <netinet/in_kdtrace.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip_var.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/in6_pcb.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/scope6_var.h>
86 #include <netinet/tcp_fastopen.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>
94 #include <netinet/tcp_pcap.h>
97 #include <netinet/tcp_debug.h>
100 #include <netinet/tcp_offload.h>
104 * TCP protocol interface to socket abstraction.
106 static int tcp_attach(struct socket *);
108 static int tcp_connect(struct tcpcb *, struct sockaddr *,
112 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
115 static void tcp_disconnect(struct tcpcb *);
116 static void tcp_usrclosed(struct tcpcb *);
117 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
120 #define TCPDEBUG0 int ostate = 0
121 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
122 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
123 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
127 #define TCPDEBUG2(req)
131 * TCP attaches to socket via pru_attach(), reserving space,
132 * and an internet control block.
135 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
138 struct tcpcb *tp = NULL;
143 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
146 error = tcp_attach(so);
150 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
151 so->so_linger = TCP_LINGERTIME;
156 TCPDEBUG2(PRU_ATTACH);
157 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
162 * tcp_detach is called when the socket layer loses its final reference
163 * to the socket, be it a file descriptor reference, a reference from TCP,
164 * etc. At this point, there is only one case in which we will keep around
165 * inpcb state: time wait.
167 * This function can probably be re-absorbed back into tcp_usr_detach() now
168 * that there is a single detach path.
171 tcp_detach(struct socket *so, struct inpcb *inp)
175 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
176 INP_WLOCK_ASSERT(inp);
178 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
179 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
183 if (inp->inp_flags & INP_TIMEWAIT) {
185 * There are two cases to handle: one in which the time wait
186 * state is being discarded (INP_DROPPED), and one in which
187 * this connection will remain in timewait. In the former,
188 * it is time to discard all state (except tcptw, which has
189 * already been discarded by the timewait close code, which
190 * should be further up the call stack somewhere). In the
191 * latter case, we detach from the socket, but leave the pcb
192 * present until timewait ends.
194 * XXXRW: Would it be cleaner to free the tcptw here?
196 * Astute question indeed, from twtcp perspective there are
197 * three cases to consider:
199 * #1 tcp_detach is called at tcptw creation time by
200 * tcp_twstart, then do not discard the newly created tcptw
201 * and leave inpcb present until timewait ends
202 * #2 tcp_detach is called at timewait end (or reuse) by
203 * tcp_twclose, then the tcptw has already been discarded
204 * (or reused) and inpcb is freed here
205 * #3 tcp_detach is called() after timewait ends (or reuse)
206 * (e.g. by soclose), then tcptw has already been discarded
207 * (or reused) and inpcb is freed here
209 * In all three cases the tcptw should not be freed here.
211 if (inp->inp_flags & INP_DROPPED) {
212 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
213 "INP_DROPPED && tp != NULL"));
222 * If the connection is not in timewait, we consider two
223 * two conditions: one in which no further processing is
224 * necessary (dropped || embryonic), and one in which TCP is
225 * not yet done, but no longer requires the socket, so the
226 * pcb will persist for the time being.
228 * XXXRW: Does the second case still occur?
230 if (inp->inp_flags & INP_DROPPED ||
231 tp->t_state < TCPS_SYN_SENT) {
243 * pru_detach() detaches the TCP protocol from the socket.
244 * If the protocol state is non-embryonic, then can't
245 * do this directly: have to initiate a pru_disconnect(),
246 * which may finish later; embryonic TCB's can just
250 tcp_usr_detach(struct socket *so)
256 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
257 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
258 INP_INFO_RLOCK(&V_tcbinfo);
262 KASSERT(inp->inp_socket != NULL,
263 ("tcp_usr_detach: inp_socket == NULL"));
266 INP_INFO_RUNLOCK(&V_tcbinfo);
271 * Give the socket an address.
274 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
278 struct tcpcb *tp = NULL;
279 struct sockaddr_in *sinp;
281 sinp = (struct sockaddr_in *)nam;
282 if (nam->sa_len != sizeof (*sinp))
285 * Must check for multicast addresses and disallow binding
288 if (sinp->sin_family == AF_INET &&
289 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
290 return (EAFNOSUPPORT);
294 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
296 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
302 INP_HASH_WLOCK(&V_tcbinfo);
303 error = in_pcbbind(inp, nam, td->td_ucred);
304 INP_HASH_WUNLOCK(&V_tcbinfo);
307 TCP_PROBE2(debug__user, tp, PRU_BIND);
316 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
320 struct tcpcb *tp = NULL;
321 struct sockaddr_in6 *sin6p;
323 sin6p = (struct sockaddr_in6 *)nam;
324 if (nam->sa_len != sizeof (*sin6p))
327 * Must check for multicast addresses and disallow binding
330 if (sin6p->sin6_family == AF_INET6 &&
331 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
332 return (EAFNOSUPPORT);
336 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
338 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
344 INP_HASH_WLOCK(&V_tcbinfo);
345 inp->inp_vflag &= ~INP_IPV4;
346 inp->inp_vflag |= INP_IPV6;
348 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
349 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
350 inp->inp_vflag |= INP_IPV4;
351 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
352 struct sockaddr_in sin;
354 in6_sin6_2_sin(&sin, sin6p);
355 inp->inp_vflag |= INP_IPV4;
356 inp->inp_vflag &= ~INP_IPV6;
357 error = in_pcbbind(inp, (struct sockaddr *)&sin,
359 INP_HASH_WUNLOCK(&V_tcbinfo);
364 error = in6_pcbbind(inp, nam, td->td_ucred);
365 INP_HASH_WUNLOCK(&V_tcbinfo);
368 TCP_PROBE2(debug__user, tp, PRU_BIND);
376 * Prepare to accept connections.
379 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
383 struct tcpcb *tp = NULL;
387 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
389 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
396 error = solisten_proto_check(so);
397 INP_HASH_WLOCK(&V_tcbinfo);
398 if (error == 0 && inp->inp_lport == 0)
399 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
400 INP_HASH_WUNLOCK(&V_tcbinfo);
402 tcp_state_change(tp, TCPS_LISTEN);
403 solisten_proto(so, backlog);
405 if ((so->so_options & SO_NO_OFFLOAD) == 0)
406 tcp_offload_listen_start(tp);
412 if (tp->t_flags & TF_FASTOPEN)
413 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
416 TCPDEBUG2(PRU_LISTEN);
417 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
425 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
429 struct tcpcb *tp = NULL;
433 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
435 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
442 error = solisten_proto_check(so);
443 INP_HASH_WLOCK(&V_tcbinfo);
444 if (error == 0 && inp->inp_lport == 0) {
445 inp->inp_vflag &= ~INP_IPV4;
446 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
447 inp->inp_vflag |= INP_IPV4;
448 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
450 INP_HASH_WUNLOCK(&V_tcbinfo);
452 tcp_state_change(tp, TCPS_LISTEN);
453 solisten_proto(so, backlog);
455 if ((so->so_options & SO_NO_OFFLOAD) == 0)
456 tcp_offload_listen_start(tp);
462 if (tp->t_flags & TF_FASTOPEN)
463 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
466 TCPDEBUG2(PRU_LISTEN);
467 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
475 * Initiate connection to peer.
476 * Create a template for use in transmissions on this connection.
477 * Enter SYN_SENT state, and mark socket as connecting.
478 * Start keep-alive timer, and seed output sequence space.
479 * Send initial segment on connection.
482 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
486 struct tcpcb *tp = NULL;
487 struct sockaddr_in *sinp;
489 sinp = (struct sockaddr_in *)nam;
490 if (nam->sa_len != sizeof (*sinp))
493 * Must disallow TCP ``connections'' to multicast addresses.
495 if (sinp->sin_family == AF_INET
496 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
497 return (EAFNOSUPPORT);
498 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
503 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
505 if (inp->inp_flags & INP_TIMEWAIT) {
509 if (inp->inp_flags & INP_DROPPED) {
510 error = ECONNREFUSED;
515 if ((error = tcp_connect(tp, nam, td)) != 0)
518 if (registered_toedevs > 0 &&
519 (so->so_options & SO_NO_OFFLOAD) == 0 &&
520 (error = tcp_offload_connect(so, nam)) == 0)
523 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
524 error = tp->t_fb->tfb_tcp_output(tp);
526 TCPDEBUG2(PRU_CONNECT);
534 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
538 struct tcpcb *tp = NULL;
539 struct sockaddr_in6 *sin6p;
543 sin6p = (struct sockaddr_in6 *)nam;
544 if (nam->sa_len != sizeof (*sin6p))
547 * Must disallow TCP ``connections'' to multicast addresses.
549 if (sin6p->sin6_family == AF_INET6
550 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
551 return (EAFNOSUPPORT);
554 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
556 if (inp->inp_flags & INP_TIMEWAIT) {
560 if (inp->inp_flags & INP_DROPPED) {
561 error = ECONNREFUSED;
568 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
569 * therefore probably require the hash lock, which isn't held here.
570 * Is this a significant problem?
572 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
573 struct sockaddr_in sin;
575 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
580 in6_sin6_2_sin(&sin, sin6p);
581 inp->inp_vflag |= INP_IPV4;
582 inp->inp_vflag &= ~INP_IPV6;
583 if ((error = prison_remote_ip4(td->td_ucred,
584 &sin.sin_addr)) != 0)
586 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
589 if (registered_toedevs > 0 &&
590 (so->so_options & SO_NO_OFFLOAD) == 0 &&
591 (error = tcp_offload_connect(so, nam)) == 0)
594 error = tp->t_fb->tfb_tcp_output(tp);
598 inp->inp_vflag &= ~INP_IPV4;
599 inp->inp_vflag |= INP_IPV6;
600 inp->inp_inc.inc_flags |= INC_ISIPV6;
601 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
603 if ((error = tcp6_connect(tp, nam, td)) != 0)
606 if (registered_toedevs > 0 &&
607 (so->so_options & SO_NO_OFFLOAD) == 0 &&
608 (error = tcp_offload_connect(so, nam)) == 0)
611 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
612 error = tp->t_fb->tfb_tcp_output(tp);
615 TCPDEBUG2(PRU_CONNECT);
616 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
623 * Initiate disconnect from peer.
624 * If connection never passed embryonic stage, just drop;
625 * else if don't need to let data drain, then can just drop anyways,
626 * else have to begin TCP shutdown process: mark socket disconnecting,
627 * drain unread data, state switch to reflect user close, and
628 * send segment (e.g. FIN) to peer. Socket will be really disconnected
629 * when peer sends FIN and acks ours.
631 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
634 tcp_usr_disconnect(struct socket *so)
637 struct tcpcb *tp = NULL;
641 INP_INFO_RLOCK(&V_tcbinfo);
643 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
645 if (inp->inp_flags & INP_TIMEWAIT)
647 if (inp->inp_flags & INP_DROPPED) {
655 TCPDEBUG2(PRU_DISCONNECT);
656 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
658 INP_INFO_RUNLOCK(&V_tcbinfo);
664 * Accept a connection. Essentially all the work is done at higher levels;
665 * just return the address of the peer, storing through addr.
668 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
671 struct inpcb *inp = NULL;
672 struct tcpcb *tp = NULL;
677 if (so->so_state & SS_ISDISCONNECTED)
678 return (ECONNABORTED);
681 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
683 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
684 error = ECONNABORTED;
691 * We inline in_getpeeraddr and COMMON_END here, so that we can
692 * copy the data of interest and defer the malloc until after we
695 port = inp->inp_fport;
696 addr = inp->inp_faddr;
699 TCPDEBUG2(PRU_ACCEPT);
700 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
703 *nam = in_sockaddr(port, &addr);
710 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
712 struct inpcb *inp = NULL;
714 struct tcpcb *tp = NULL;
716 struct in6_addr addr6;
721 if (so->so_state & SS_ISDISCONNECTED)
722 return (ECONNABORTED);
725 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
726 INP_INFO_RLOCK(&V_tcbinfo);
728 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
729 error = ECONNABORTED;
736 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
737 * copy the data of interest and defer the malloc until after we
740 if (inp->inp_vflag & INP_IPV4) {
742 port = inp->inp_fport;
743 addr = inp->inp_faddr;
745 port = inp->inp_fport;
746 addr6 = inp->in6p_faddr;
750 TCPDEBUG2(PRU_ACCEPT);
751 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
753 INP_INFO_RUNLOCK(&V_tcbinfo);
756 *nam = in6_v4mapsin6_sockaddr(port, &addr);
758 *nam = in6_sockaddr(port, &addr6);
765 * Mark the connection as being incapable of further output.
768 tcp_usr_shutdown(struct socket *so)
772 struct tcpcb *tp = NULL;
775 INP_INFO_RLOCK(&V_tcbinfo);
777 KASSERT(inp != NULL, ("inp == NULL"));
779 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
787 if (!(inp->inp_flags & INP_DROPPED))
788 error = tp->t_fb->tfb_tcp_output(tp);
791 TCPDEBUG2(PRU_SHUTDOWN);
792 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
794 INP_INFO_RUNLOCK(&V_tcbinfo);
800 * After a receive, possibly send window update to peer.
803 tcp_usr_rcvd(struct socket *so, int flags)
806 struct tcpcb *tp = NULL;
811 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
813 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
821 * For passively-created TFO connections, don't attempt a window
822 * update while still in SYN_RECEIVED as this may trigger an early
823 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
824 * application response data, or failing that, when the DELACK timer
827 if ((tp->t_flags & TF_FASTOPEN) &&
828 (tp->t_state == TCPS_SYN_RECEIVED))
832 if (tp->t_flags & TF_TOE)
833 tcp_offload_rcvd(tp);
836 tp->t_fb->tfb_tcp_output(tp);
840 TCP_PROBE2(debug__user, tp, PRU_RCVD);
846 * Do a send by putting data in output queue and updating urgent
847 * marker if URG set. Possibly send more data. Unlike the other
848 * pru_*() routines, the mbuf chains are our responsibility. We
849 * must either enqueue them or free them. The other pru_* routines
850 * generally are caller-frees.
853 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
854 struct sockaddr *nam, struct mbuf *control, struct thread *td)
858 struct tcpcb *tp = NULL;
865 * We require the pcbinfo lock if we will close the socket as part of
868 if (flags & PRUS_EOF)
869 INP_INFO_RLOCK(&V_tcbinfo);
871 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
873 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
877 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
878 * for freeing memory.
880 if (m && (flags & PRUS_NOTREADY) == 0)
886 isipv6 = nam && nam->sa_family == AF_INET6;
891 /* TCP doesn't do control messages (rights, creds, etc) */
892 if (control->m_len) {
899 m_freem(control); /* empty control, just free it */
901 if (!(flags & PRUS_OOB)) {
902 sbappendstream(&so->so_snd, m, flags);
903 if (nam && tp->t_state < TCPS_SYN_SENT) {
905 * Do implied connect if not yet connected,
906 * initialize window to default value, and
907 * initialize maxseg/maxopd using peer's cached
912 error = tcp6_connect(tp, nam, td);
914 #if defined(INET6) && defined(INET)
918 error = tcp_connect(tp, nam, td);
922 tp->snd_wnd = TTCP_CLIENT_SND_WND;
925 if (flags & PRUS_EOF) {
927 * Close the send side of the connection after
930 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
934 if (!(inp->inp_flags & INP_DROPPED) &&
935 !(flags & PRUS_NOTREADY)) {
936 if (flags & PRUS_MORETOCOME)
937 tp->t_flags |= TF_MORETOCOME;
938 error = tp->t_fb->tfb_tcp_output(tp);
939 if (flags & PRUS_MORETOCOME)
940 tp->t_flags &= ~TF_MORETOCOME;
944 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
946 SOCKBUF_LOCK(&so->so_snd);
947 if (sbspace(&so->so_snd) < -512) {
948 SOCKBUF_UNLOCK(&so->so_snd);
954 * According to RFC961 (Assigned Protocols),
955 * the urgent pointer points to the last octet
956 * of urgent data. We continue, however,
957 * to consider it to indicate the first octet
958 * of data past the urgent section.
959 * Otherwise, snd_up should be one lower.
961 sbappendstream_locked(&so->so_snd, m, flags);
962 SOCKBUF_UNLOCK(&so->so_snd);
963 if (nam && tp->t_state < TCPS_SYN_SENT) {
965 * Do implied connect if not yet connected,
966 * initialize window to default value, and
967 * initialize maxseg/maxopd using peer's cached
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(inp) do { \
1366 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1368 return (ECONNRESET); \
1370 tp = intotcpcb(inp); \
1374 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1379 struct tcp_function_block *blk;
1380 struct tcp_function_set fsn;
1383 inp = sotoinpcb(so);
1384 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1386 if (sopt->sopt_level != IPPROTO_TCP) {
1388 if (inp->inp_vflag & INP_IPV6PROTO) {
1390 error = ip6_ctloutput(so, sopt);
1393 #if defined(INET6) && defined(INET)
1399 error = ip_ctloutput(so, sopt);
1404 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1406 return (ECONNRESET);
1408 tp = intotcpcb(inp);
1410 * Protect the TCP option TCP_FUNCTION_BLK so
1411 * that a sub-function can *never* overwrite this.
1413 if ((sopt->sopt_dir == SOPT_SET) &&
1414 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1416 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1420 INP_WLOCK_RECHECK(inp);
1421 if (tp->t_state != TCPS_CLOSED) {
1423 * The user has advanced the state
1424 * past the initial point, we can't
1425 * switch since we are down the road
1426 * and a new set of functions may
1427 * not be compatibile.
1432 blk = find_and_ref_tcp_functions(&fsn);
1437 if (tp->t_fb != blk) {
1438 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1439 refcount_release(&blk->tfb_refcnt);
1444 * Release the old refcnt, the
1445 * lookup acquires a ref on the
1448 if (tp->t_fb->tfb_tcp_fb_fini)
1449 (*tp->t_fb->tfb_tcp_fb_fini)(tp);
1450 refcount_release(&tp->t_fb->tfb_refcnt);
1452 if (tp->t_fb->tfb_tcp_fb_init) {
1453 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1457 if (tp->t_flags & TF_TOE) {
1458 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1464 } else if ((sopt->sopt_dir == SOPT_GET) &&
1465 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1466 strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
1467 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1469 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1472 /* Pass in the INP locked, called must unlock it */
1473 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1477 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1479 int error, opt, optval;
1482 struct cc_algo *algo;
1483 char buf[TCP_CA_NAME_MAX];
1485 switch (sopt->sopt_dir) {
1487 switch (sopt->sopt_name) {
1488 #ifdef TCP_SIGNATURE
1491 error = sooptcopyin(sopt, &optval, sizeof optval,
1496 INP_WLOCK_RECHECK(inp);
1498 tp->t_flags |= TF_SIGNATURE;
1500 tp->t_flags &= ~TF_SIGNATURE;
1501 goto unlock_and_done;
1502 #endif /* TCP_SIGNATURE */
1507 error = sooptcopyin(sopt, &optval, sizeof optval,
1512 INP_WLOCK_RECHECK(inp);
1513 switch (sopt->sopt_name) {
1521 opt = 0; /* dead code to fool gcc */
1528 tp->t_flags &= ~opt;
1531 if (tp->t_flags & TF_TOE) {
1532 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1541 error = sooptcopyin(sopt, &optval, sizeof optval,
1546 INP_WLOCK_RECHECK(inp);
1548 tp->t_flags |= TF_NOPUSH;
1549 else if (tp->t_flags & TF_NOPUSH) {
1550 tp->t_flags &= ~TF_NOPUSH;
1551 if (TCPS_HAVEESTABLISHED(tp->t_state))
1552 error = tp->t_fb->tfb_tcp_output(tp);
1554 goto unlock_and_done;
1558 error = sooptcopyin(sopt, &optval, sizeof optval,
1563 INP_WLOCK_RECHECK(inp);
1564 if (optval > 0 && optval <= tp->t_maxseg &&
1565 optval + 40 >= V_tcp_minmss)
1566 tp->t_maxseg = optval;
1569 goto unlock_and_done;
1576 case TCP_CONGESTION:
1578 bzero(buf, sizeof(buf));
1579 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1582 INP_WLOCK_RECHECK(inp);
1584 * Return EINVAL if we can't find the requested cc algo.
1588 STAILQ_FOREACH(algo, &cc_list, entries) {
1589 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1591 /* We've found the requested algo. */
1594 * We hold a write lock over the tcb
1595 * so it's safe to do these things
1596 * without ordering concerns.
1598 if (CC_ALGO(tp)->cb_destroy != NULL)
1599 CC_ALGO(tp)->cb_destroy(tp->ccv);
1602 * If something goes pear shaped
1603 * initialising the new algo,
1604 * fall back to newreno (which
1605 * does not require initialisation).
1607 if (algo->cb_init != NULL)
1608 if (algo->cb_init(tp->ccv) > 0) {
1609 CC_ALGO(tp) = &newreno_cc_algo;
1611 * The only reason init
1613 * because of malloc.
1617 break; /* Break the STAILQ_FOREACH. */
1621 goto unlock_and_done;
1627 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1631 if (ui > (UINT_MAX / hz)) {
1637 INP_WLOCK_RECHECK(inp);
1638 switch (sopt->sopt_name) {
1640 tp->t_keepidle = ui;
1642 * XXX: better check current remaining
1643 * timeout and "merge" it with new value.
1645 if ((tp->t_state > TCPS_LISTEN) &&
1646 (tp->t_state <= TCPS_CLOSING))
1647 tcp_timer_activate(tp, TT_KEEP,
1651 tp->t_keepintvl = ui;
1652 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1653 (TP_MAXIDLE(tp) > 0))
1654 tcp_timer_activate(tp, TT_2MSL,
1658 tp->t_keepinit = ui;
1659 if (tp->t_state == TCPS_SYN_RECEIVED ||
1660 tp->t_state == TCPS_SYN_SENT)
1661 tcp_timer_activate(tp, TT_KEEP,
1665 goto unlock_and_done;
1669 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1673 INP_WLOCK_RECHECK(inp);
1675 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1676 (TP_MAXIDLE(tp) > 0))
1677 tcp_timer_activate(tp, TT_2MSL,
1679 goto unlock_and_done;
1685 error = sooptcopyin(sopt, &optval, sizeof optval,
1690 INP_WLOCK_RECHECK(inp);
1692 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1693 &(tp->t_outpkts) : &(tp->t_inpkts),
1697 goto unlock_and_done;
1703 if (!V_tcp_fastopen_enabled)
1706 error = sooptcopyin(sopt, &optval, sizeof optval,
1711 INP_WLOCK_RECHECK(inp);
1713 tp->t_flags |= TF_FASTOPEN;
1714 if ((tp->t_state == TCPS_LISTEN) &&
1715 (tp->t_tfo_pending == NULL))
1717 tcp_fastopen_alloc_counter();
1719 tp->t_flags &= ~TF_FASTOPEN;
1720 goto unlock_and_done;
1725 error = ENOPROTOOPT;
1731 tp = intotcpcb(inp);
1732 switch (sopt->sopt_name) {
1733 #ifdef TCP_SIGNATURE
1735 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1737 error = sooptcopyout(sopt, &optval, sizeof optval);
1742 optval = tp->t_flags & TF_NODELAY;
1744 error = sooptcopyout(sopt, &optval, sizeof optval);
1747 optval = tp->t_maxseg;
1749 error = sooptcopyout(sopt, &optval, sizeof optval);
1752 optval = tp->t_flags & TF_NOOPT;
1754 error = sooptcopyout(sopt, &optval, sizeof optval);
1757 optval = tp->t_flags & TF_NOPUSH;
1759 error = sooptcopyout(sopt, &optval, sizeof optval);
1762 tcp_fill_info(tp, &ti);
1764 error = sooptcopyout(sopt, &ti, sizeof ti);
1766 case TCP_CONGESTION:
1767 bzero(buf, sizeof(buf));
1768 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1770 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1776 switch (sopt->sopt_name) {
1778 ui = tp->t_keepidle / hz;
1781 ui = tp->t_keepintvl / hz;
1784 ui = tp->t_keepinit / hz;
1791 error = sooptcopyout(sopt, &ui, sizeof(ui));
1796 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1797 &(tp->t_outpkts) : &(tp->t_inpkts));
1799 error = sooptcopyout(sopt, &optval, sizeof optval);
1805 optval = tp->t_flags & TF_FASTOPEN;
1807 error = sooptcopyout(sopt, &optval, sizeof optval);
1812 error = ENOPROTOOPT;
1819 #undef INP_WLOCK_RECHECK
1822 * Attach TCP protocol to socket, allocating
1823 * internet protocol control block, tcp control block,
1824 * bufer space, and entering LISTEN state if to accept connections.
1827 tcp_attach(struct socket *so)
1833 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1834 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1838 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1839 so->so_snd.sb_flags |= SB_AUTOSIZE;
1840 INP_INFO_RLOCK(&V_tcbinfo);
1841 error = in_pcballoc(so, &V_tcbinfo);
1843 INP_INFO_RUNLOCK(&V_tcbinfo);
1846 inp = sotoinpcb(so);
1848 if (inp->inp_vflag & INP_IPV6PROTO) {
1849 inp->inp_vflag |= INP_IPV6;
1850 inp->in6p_hops = -1; /* use kernel default */
1854 inp->inp_vflag |= INP_IPV4;
1855 tp = tcp_newtcpcb(inp);
1859 INP_INFO_RUNLOCK(&V_tcbinfo);
1862 tp->t_state = TCPS_CLOSED;
1864 INP_INFO_RUNLOCK(&V_tcbinfo);
1869 * Initiate (or continue) disconnect.
1870 * If embryonic state, just send reset (once).
1871 * If in ``let data drain'' option and linger null, just drop.
1872 * Otherwise (hard), mark socket disconnecting and drop
1873 * current input data; switch states based on user close, and
1874 * send segment to peer (with FIN).
1877 tcp_disconnect(struct tcpcb *tp)
1879 struct inpcb *inp = tp->t_inpcb;
1880 struct socket *so = inp->inp_socket;
1882 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1883 INP_WLOCK_ASSERT(inp);
1886 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1887 * socket is still open.
1889 if (tp->t_state < TCPS_ESTABLISHED) {
1892 ("tcp_disconnect: tcp_close() returned NULL"));
1893 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1894 tp = tcp_drop(tp, 0);
1896 ("tcp_disconnect: tcp_drop() returned NULL"));
1898 soisdisconnecting(so);
1899 sbflush(&so->so_rcv);
1901 if (!(inp->inp_flags & INP_DROPPED))
1902 tp->t_fb->tfb_tcp_output(tp);
1907 * User issued close, and wish to trail through shutdown states:
1908 * if never received SYN, just forget it. If got a SYN from peer,
1909 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1910 * If already got a FIN from peer, then almost done; go to LAST_ACK
1911 * state. In all other cases, have already sent FIN to peer (e.g.
1912 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1913 * for peer to send FIN or not respond to keep-alives, etc.
1914 * We can let the user exit from the close as soon as the FIN is acked.
1917 tcp_usrclosed(struct tcpcb *tp)
1920 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1921 INP_WLOCK_ASSERT(tp->t_inpcb);
1923 switch (tp->t_state) {
1926 tcp_offload_listen_stop(tp);
1928 tcp_state_change(tp, TCPS_CLOSED);
1933 * tcp_close() should never return NULL here as the socket is
1937 ("tcp_usrclosed: tcp_close() returned NULL"));
1941 case TCPS_SYN_RECEIVED:
1942 tp->t_flags |= TF_NEEDFIN;
1945 case TCPS_ESTABLISHED:
1946 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1949 case TCPS_CLOSE_WAIT:
1950 tcp_state_change(tp, TCPS_LAST_ACK);
1953 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1954 soisdisconnected(tp->t_inpcb->inp_socket);
1955 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1956 if (tp->t_state == TCPS_FIN_WAIT_2) {
1959 timeout = (tcp_fast_finwait2_recycle) ?
1960 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1961 tcp_timer_activate(tp, TT_2MSL, timeout);
1968 db_print_indent(int indent)
1972 for (i = 0; i < indent; i++)
1977 db_print_tstate(int t_state)
1982 db_printf("TCPS_CLOSED");
1986 db_printf("TCPS_LISTEN");
1990 db_printf("TCPS_SYN_SENT");
1993 case TCPS_SYN_RECEIVED:
1994 db_printf("TCPS_SYN_RECEIVED");
1997 case TCPS_ESTABLISHED:
1998 db_printf("TCPS_ESTABLISHED");
2001 case TCPS_CLOSE_WAIT:
2002 db_printf("TCPS_CLOSE_WAIT");
2005 case TCPS_FIN_WAIT_1:
2006 db_printf("TCPS_FIN_WAIT_1");
2010 db_printf("TCPS_CLOSING");
2014 db_printf("TCPS_LAST_ACK");
2017 case TCPS_FIN_WAIT_2:
2018 db_printf("TCPS_FIN_WAIT_2");
2021 case TCPS_TIME_WAIT:
2022 db_printf("TCPS_TIME_WAIT");
2026 db_printf("unknown");
2032 db_print_tflags(u_int t_flags)
2037 if (t_flags & TF_ACKNOW) {
2038 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2041 if (t_flags & TF_DELACK) {
2042 db_printf("%sTF_DELACK", comma ? ", " : "");
2045 if (t_flags & TF_NODELAY) {
2046 db_printf("%sTF_NODELAY", comma ? ", " : "");
2049 if (t_flags & TF_NOOPT) {
2050 db_printf("%sTF_NOOPT", comma ? ", " : "");
2053 if (t_flags & TF_SENTFIN) {
2054 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2057 if (t_flags & TF_REQ_SCALE) {
2058 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2061 if (t_flags & TF_RCVD_SCALE) {
2062 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2065 if (t_flags & TF_REQ_TSTMP) {
2066 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2069 if (t_flags & TF_RCVD_TSTMP) {
2070 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2073 if (t_flags & TF_SACK_PERMIT) {
2074 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2077 if (t_flags & TF_NEEDSYN) {
2078 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2081 if (t_flags & TF_NEEDFIN) {
2082 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2085 if (t_flags & TF_NOPUSH) {
2086 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2089 if (t_flags & TF_MORETOCOME) {
2090 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2093 if (t_flags & TF_LQ_OVERFLOW) {
2094 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2097 if (t_flags & TF_LASTIDLE) {
2098 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2101 if (t_flags & TF_RXWIN0SENT) {
2102 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2105 if (t_flags & TF_FASTRECOVERY) {
2106 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2109 if (t_flags & TF_CONGRECOVERY) {
2110 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2113 if (t_flags & TF_WASFRECOVERY) {
2114 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2117 if (t_flags & TF_SIGNATURE) {
2118 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2121 if (t_flags & TF_FORCEDATA) {
2122 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2125 if (t_flags & TF_TSO) {
2126 db_printf("%sTF_TSO", comma ? ", " : "");
2129 if (t_flags & TF_ECN_PERMIT) {
2130 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2133 if (t_flags & TF_FASTOPEN) {
2134 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2140 db_print_toobflags(char t_oobflags)
2145 if (t_oobflags & TCPOOB_HAVEDATA) {
2146 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2149 if (t_oobflags & TCPOOB_HADDATA) {
2150 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2156 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2159 db_print_indent(indent);
2160 db_printf("%s at %p\n", name, tp);
2164 db_print_indent(indent);
2165 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2166 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2168 db_print_indent(indent);
2169 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2170 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2172 db_print_indent(indent);
2173 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2174 &tp->t_timers->tt_delack, tp->t_inpcb);
2176 db_print_indent(indent);
2177 db_printf("t_state: %d (", tp->t_state);
2178 db_print_tstate(tp->t_state);
2181 db_print_indent(indent);
2182 db_printf("t_flags: 0x%x (", tp->t_flags);
2183 db_print_tflags(tp->t_flags);
2186 db_print_indent(indent);
2187 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2188 tp->snd_una, tp->snd_max, tp->snd_nxt);
2190 db_print_indent(indent);
2191 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2192 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2194 db_print_indent(indent);
2195 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2196 tp->iss, tp->irs, tp->rcv_nxt);
2198 db_print_indent(indent);
2199 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2200 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2202 db_print_indent(indent);
2203 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2204 tp->snd_wnd, tp->snd_cwnd);
2206 db_print_indent(indent);
2207 db_printf("snd_ssthresh: %lu snd_recover: "
2208 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2210 db_print_indent(indent);
2211 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2212 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2214 db_print_indent(indent);
2215 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2216 tp->t_rtttime, tp->t_rtseq);
2218 db_print_indent(indent);
2219 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2220 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2222 db_print_indent(indent);
2223 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2224 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2227 db_print_indent(indent);
2228 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2229 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2231 db_print_indent(indent);
2232 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2233 db_print_toobflags(tp->t_oobflags);
2234 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2236 db_print_indent(indent);
2237 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2238 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2240 db_print_indent(indent);
2241 db_printf("ts_recent: %u ts_recent_age: %u\n",
2242 tp->ts_recent, tp->ts_recent_age);
2244 db_print_indent(indent);
2245 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2246 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2248 db_print_indent(indent);
2249 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2250 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2251 tp->snd_recover_prev, tp->t_badrxtwin);
2253 db_print_indent(indent);
2254 db_printf("snd_numholes: %d snd_holes first: %p\n",
2255 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2257 db_print_indent(indent);
2258 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2259 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2261 /* Skip sackblks, sackhint. */
2263 db_print_indent(indent);
2264 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2265 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2268 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2273 db_printf("usage: show tcpcb <addr>\n");
2276 tp = (struct tcpcb *)addr;
2278 db_print_tcpcb(tp, "tcpcb", 0);