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 * 3. 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_ipsec.h"
45 #include "opt_tcpdebug.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
51 #include <sys/refcount.h>
52 #include <sys/kernel.h>
53 #include <sys/sysctl.h>
56 #include <sys/domain.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/protosw.h>
63 #include <sys/syslog.h>
70 #include <net/if_var.h>
71 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_kdtrace.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip_var.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/in6_pcb.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/scope6_var.h>
87 #include <netinet/tcp_fastopen.h>
89 #include <netinet/tcp.h>
90 #include <netinet/tcp_fsm.h>
91 #include <netinet/tcp_seq.h>
92 #include <netinet/tcp_timer.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
97 #include <netinet/tcp_pcap.h>
100 #include <netinet/tcp_debug.h>
103 #include <netinet/tcp_offload.h>
105 #include <netipsec/ipsec_support.h>
108 * TCP protocol interface to socket abstraction.
110 static int tcp_attach(struct socket *);
112 static int tcp_connect(struct tcpcb *, struct sockaddr *,
116 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
119 static void tcp_disconnect(struct tcpcb *);
120 static void tcp_usrclosed(struct tcpcb *);
121 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
124 #define TCPDEBUG0 int ostate = 0
125 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
126 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
127 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
131 #define TCPDEBUG2(req)
135 * TCP attaches to socket via pru_attach(), reserving space,
136 * and an internet control block.
139 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
142 struct tcpcb *tp = NULL;
147 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
150 error = tcp_attach(so);
154 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
155 so->so_linger = TCP_LINGERTIME;
160 TCPDEBUG2(PRU_ATTACH);
161 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
166 * tcp_detach is called when the socket layer loses its final reference
167 * to the socket, be it a file descriptor reference, a reference from TCP,
168 * etc. At this point, there is only one case in which we will keep around
169 * inpcb state: time wait.
171 * This function can probably be re-absorbed back into tcp_usr_detach() now
172 * that there is a single detach path.
175 tcp_detach(struct socket *so, struct inpcb *inp)
179 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
180 INP_WLOCK_ASSERT(inp);
182 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
183 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
187 if (inp->inp_flags & INP_TIMEWAIT) {
189 * There are two cases to handle: one in which the time wait
190 * state is being discarded (INP_DROPPED), and one in which
191 * this connection will remain in timewait. In the former,
192 * it is time to discard all state (except tcptw, which has
193 * already been discarded by the timewait close code, which
194 * should be further up the call stack somewhere). In the
195 * latter case, we detach from the socket, but leave the pcb
196 * present until timewait ends.
198 * XXXRW: Would it be cleaner to free the tcptw here?
200 * Astute question indeed, from twtcp perspective there are
201 * three cases to consider:
203 * #1 tcp_detach is called at tcptw creation time by
204 * tcp_twstart, then do not discard the newly created tcptw
205 * and leave inpcb present until timewait ends
206 * #2 tcp_detach is called at timewait end (or reuse) by
207 * tcp_twclose, then the tcptw has already been discarded
208 * (or reused) and inpcb is freed here
209 * #3 tcp_detach is called() after timewait ends (or reuse)
210 * (e.g. by soclose), then tcptw has already been discarded
211 * (or reused) and inpcb is freed here
213 * In all three cases the tcptw should not be freed here.
215 if (inp->inp_flags & INP_DROPPED) {
217 if (__predict_true(tp == NULL)) {
221 * This case should not happen as in TIMEWAIT
222 * state the inp should not be destroyed before
223 * its tcptw. If INVARIANTS is defined, panic.
226 panic("%s: Panic before an inp double-free: "
227 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
230 log(LOG_ERR, "%s: Avoid an inp double-free: "
231 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
242 * If the connection is not in timewait, we consider two
243 * two conditions: one in which no further processing is
244 * necessary (dropped || embryonic), and one in which TCP is
245 * not yet done, but no longer requires the socket, so the
246 * pcb will persist for the time being.
248 * XXXRW: Does the second case still occur?
250 if (inp->inp_flags & INP_DROPPED ||
251 tp->t_state < TCPS_SYN_SENT) {
263 * pru_detach() detaches the TCP protocol from the socket.
264 * If the protocol state is non-embryonic, then can't
265 * do this directly: have to initiate a pru_disconnect(),
266 * which may finish later; embryonic TCB's can just
270 tcp_usr_detach(struct socket *so)
276 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
277 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
278 INP_INFO_RLOCK(&V_tcbinfo);
282 KASSERT(inp->inp_socket != NULL,
283 ("tcp_usr_detach: inp_socket == NULL"));
286 INP_INFO_RUNLOCK(&V_tcbinfo);
291 * Give the socket an address.
294 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
298 struct tcpcb *tp = NULL;
299 struct sockaddr_in *sinp;
301 sinp = (struct sockaddr_in *)nam;
302 if (nam->sa_len != sizeof (*sinp))
305 * Must check for multicast addresses and disallow binding
308 if (sinp->sin_family == AF_INET &&
309 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
310 return (EAFNOSUPPORT);
314 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
316 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
322 INP_HASH_WLOCK(&V_tcbinfo);
323 error = in_pcbbind(inp, nam, td->td_ucred);
324 INP_HASH_WUNLOCK(&V_tcbinfo);
327 TCP_PROBE2(debug__user, tp, PRU_BIND);
336 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 struct tcpcb *tp = NULL;
341 struct sockaddr_in6 *sin6p;
343 sin6p = (struct sockaddr_in6 *)nam;
344 if (nam->sa_len != sizeof (*sin6p))
347 * Must check for multicast addresses and disallow binding
350 if (sin6p->sin6_family == AF_INET6 &&
351 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
352 return (EAFNOSUPPORT);
356 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
358 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
364 INP_HASH_WLOCK(&V_tcbinfo);
365 inp->inp_vflag &= ~INP_IPV4;
366 inp->inp_vflag |= INP_IPV6;
368 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
369 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
370 inp->inp_vflag |= INP_IPV4;
371 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
372 struct sockaddr_in sin;
374 in6_sin6_2_sin(&sin, sin6p);
375 inp->inp_vflag |= INP_IPV4;
376 inp->inp_vflag &= ~INP_IPV6;
377 error = in_pcbbind(inp, (struct sockaddr *)&sin,
379 INP_HASH_WUNLOCK(&V_tcbinfo);
384 error = in6_pcbbind(inp, nam, td->td_ucred);
385 INP_HASH_WUNLOCK(&V_tcbinfo);
388 TCP_PROBE2(debug__user, tp, PRU_BIND);
396 * Prepare to accept connections.
399 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
403 struct tcpcb *tp = NULL;
407 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
409 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
416 error = solisten_proto_check(so);
417 INP_HASH_WLOCK(&V_tcbinfo);
418 if (error == 0 && inp->inp_lport == 0)
419 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
420 INP_HASH_WUNLOCK(&V_tcbinfo);
422 tcp_state_change(tp, TCPS_LISTEN);
423 solisten_proto(so, backlog);
425 if ((so->so_options & SO_NO_OFFLOAD) == 0)
426 tcp_offload_listen_start(tp);
432 if (IS_FASTOPEN(tp->t_flags))
433 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
436 TCPDEBUG2(PRU_LISTEN);
437 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
445 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
449 struct tcpcb *tp = NULL;
453 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
455 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
462 error = solisten_proto_check(so);
463 INP_HASH_WLOCK(&V_tcbinfo);
464 if (error == 0 && inp->inp_lport == 0) {
465 inp->inp_vflag &= ~INP_IPV4;
466 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
467 inp->inp_vflag |= INP_IPV4;
468 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
470 INP_HASH_WUNLOCK(&V_tcbinfo);
472 tcp_state_change(tp, TCPS_LISTEN);
473 solisten_proto(so, backlog);
475 if ((so->so_options & SO_NO_OFFLOAD) == 0)
476 tcp_offload_listen_start(tp);
482 if (IS_FASTOPEN(tp->t_flags))
483 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
486 TCPDEBUG2(PRU_LISTEN);
487 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
495 * Initiate connection to peer.
496 * Create a template for use in transmissions on this connection.
497 * Enter SYN_SENT state, and mark socket as connecting.
498 * Start keep-alive timer, and seed output sequence space.
499 * Send initial segment on connection.
502 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
506 struct tcpcb *tp = NULL;
507 struct sockaddr_in *sinp;
509 sinp = (struct sockaddr_in *)nam;
510 if (nam->sa_len != sizeof (*sinp))
513 * Must disallow TCP ``connections'' to multicast addresses.
515 if (sinp->sin_family == AF_INET
516 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
517 return (EAFNOSUPPORT);
518 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
523 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
525 if (inp->inp_flags & INP_TIMEWAIT) {
529 if (inp->inp_flags & INP_DROPPED) {
530 error = ECONNREFUSED;
535 if ((error = tcp_connect(tp, nam, td)) != 0)
538 if (registered_toedevs > 0 &&
539 (so->so_options & SO_NO_OFFLOAD) == 0 &&
540 (error = tcp_offload_connect(so, nam)) == 0)
543 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
544 error = tp->t_fb->tfb_tcp_output(tp);
546 TCPDEBUG2(PRU_CONNECT);
547 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
555 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
559 struct tcpcb *tp = NULL;
560 struct sockaddr_in6 *sin6p;
564 sin6p = (struct sockaddr_in6 *)nam;
565 if (nam->sa_len != sizeof (*sin6p))
568 * Must disallow TCP ``connections'' to multicast addresses.
570 if (sin6p->sin6_family == AF_INET6
571 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
572 return (EAFNOSUPPORT);
575 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
577 if (inp->inp_flags & INP_TIMEWAIT) {
581 if (inp->inp_flags & INP_DROPPED) {
582 error = ECONNREFUSED;
589 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
590 * therefore probably require the hash lock, which isn't held here.
591 * Is this a significant problem?
593 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
594 struct sockaddr_in sin;
596 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
600 if ((inp->inp_vflag & INP_IPV4) == 0) {
601 error = EAFNOSUPPORT;
605 in6_sin6_2_sin(&sin, sin6p);
606 inp->inp_vflag |= INP_IPV4;
607 inp->inp_vflag &= ~INP_IPV6;
608 if ((error = prison_remote_ip4(td->td_ucred,
609 &sin.sin_addr)) != 0)
611 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
614 if (registered_toedevs > 0 &&
615 (so->so_options & SO_NO_OFFLOAD) == 0 &&
616 (error = tcp_offload_connect(so, nam)) == 0)
619 error = tp->t_fb->tfb_tcp_output(tp);
622 if ((inp->inp_vflag & INP_IPV6) == 0) {
623 error = EAFNOSUPPORT;
628 inp->inp_vflag &= ~INP_IPV4;
629 inp->inp_vflag |= INP_IPV6;
630 inp->inp_inc.inc_flags |= INC_ISIPV6;
631 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
633 if ((error = tcp6_connect(tp, nam, td)) != 0)
636 if (registered_toedevs > 0 &&
637 (so->so_options & SO_NO_OFFLOAD) == 0 &&
638 (error = tcp_offload_connect(so, nam)) == 0)
641 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
642 error = tp->t_fb->tfb_tcp_output(tp);
645 TCPDEBUG2(PRU_CONNECT);
646 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
653 * Initiate disconnect from peer.
654 * If connection never passed embryonic stage, just drop;
655 * else if don't need to let data drain, then can just drop anyways,
656 * else have to begin TCP shutdown process: mark socket disconnecting,
657 * drain unread data, state switch to reflect user close, and
658 * send segment (e.g. FIN) to peer. Socket will be really disconnected
659 * when peer sends FIN and acks ours.
661 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
664 tcp_usr_disconnect(struct socket *so)
667 struct tcpcb *tp = NULL;
671 INP_INFO_RLOCK(&V_tcbinfo);
673 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
675 if (inp->inp_flags & INP_TIMEWAIT)
677 if (inp->inp_flags & INP_DROPPED) {
685 TCPDEBUG2(PRU_DISCONNECT);
686 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
688 INP_INFO_RUNLOCK(&V_tcbinfo);
694 * Accept a connection. Essentially all the work is done at higher levels;
695 * just return the address of the peer, storing through addr.
698 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
701 struct inpcb *inp = NULL;
702 struct tcpcb *tp = NULL;
707 if (so->so_state & SS_ISDISCONNECTED)
708 return (ECONNABORTED);
711 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
713 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
714 error = ECONNABORTED;
721 * We inline in_getpeeraddr and COMMON_END here, so that we can
722 * copy the data of interest and defer the malloc until after we
725 port = inp->inp_fport;
726 addr = inp->inp_faddr;
729 TCPDEBUG2(PRU_ACCEPT);
730 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
733 *nam = in_sockaddr(port, &addr);
740 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
742 struct inpcb *inp = NULL;
744 struct tcpcb *tp = NULL;
746 struct in6_addr addr6;
751 if (so->so_state & SS_ISDISCONNECTED)
752 return (ECONNABORTED);
755 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
756 INP_INFO_RLOCK(&V_tcbinfo);
758 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
759 error = ECONNABORTED;
766 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
767 * copy the data of interest and defer the malloc until after we
770 if (inp->inp_vflag & INP_IPV4) {
772 port = inp->inp_fport;
773 addr = inp->inp_faddr;
775 port = inp->inp_fport;
776 addr6 = inp->in6p_faddr;
780 TCPDEBUG2(PRU_ACCEPT);
781 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
783 INP_INFO_RUNLOCK(&V_tcbinfo);
786 *nam = in6_v4mapsin6_sockaddr(port, &addr);
788 *nam = in6_sockaddr(port, &addr6);
795 * Mark the connection as being incapable of further output.
798 tcp_usr_shutdown(struct socket *so)
802 struct tcpcb *tp = NULL;
805 INP_INFO_RLOCK(&V_tcbinfo);
807 KASSERT(inp != NULL, ("inp == NULL"));
809 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
817 if (!(inp->inp_flags & INP_DROPPED))
818 error = tp->t_fb->tfb_tcp_output(tp);
821 TCPDEBUG2(PRU_SHUTDOWN);
822 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
824 INP_INFO_RUNLOCK(&V_tcbinfo);
830 * After a receive, possibly send window update to peer.
833 tcp_usr_rcvd(struct socket *so, int flags)
836 struct tcpcb *tp = NULL;
841 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
843 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
851 * For passively-created TFO connections, don't attempt a window
852 * update while still in SYN_RECEIVED as this may trigger an early
853 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
854 * application response data, or failing that, when the DELACK timer
857 if (IS_FASTOPEN(tp->t_flags) &&
858 (tp->t_state == TCPS_SYN_RECEIVED))
862 if (tp->t_flags & TF_TOE)
863 tcp_offload_rcvd(tp);
866 tp->t_fb->tfb_tcp_output(tp);
870 TCP_PROBE2(debug__user, tp, PRU_RCVD);
876 * Do a send by putting data in output queue and updating urgent
877 * marker if URG set. Possibly send more data. Unlike the other
878 * pru_*() routines, the mbuf chains are our responsibility. We
879 * must either enqueue them or free them. The other pru_* routines
880 * generally are caller-frees.
883 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
884 struct sockaddr *nam, struct mbuf *control, struct thread *td)
888 struct tcpcb *tp = NULL;
895 * We require the pcbinfo lock if we will close the socket as part of
898 if (flags & PRUS_EOF)
899 INP_INFO_RLOCK(&V_tcbinfo);
901 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
903 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
907 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
908 * for freeing memory.
910 if (m && (flags & PRUS_NOTREADY) == 0)
916 isipv6 = nam && nam->sa_family == AF_INET6;
921 /* TCP doesn't do control messages (rights, creds, etc) */
922 if (control->m_len) {
929 m_freem(control); /* empty control, just free it */
931 if (!(flags & PRUS_OOB)) {
932 sbappendstream(&so->so_snd, m, flags);
933 if (nam && tp->t_state < TCPS_SYN_SENT) {
935 * Do implied connect if not yet connected,
936 * initialize window to default value, and
937 * initialize maxseg using peer's cached MSS.
941 error = tcp6_connect(tp, nam, td);
943 #if defined(INET6) && defined(INET)
947 error = tcp_connect(tp, nam, td);
951 tp->snd_wnd = TTCP_CLIENT_SND_WND;
954 if (flags & PRUS_EOF) {
956 * Close the send side of the connection after
959 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
963 if (!(inp->inp_flags & INP_DROPPED) &&
964 !(flags & PRUS_NOTREADY)) {
965 if (flags & PRUS_MORETOCOME)
966 tp->t_flags |= TF_MORETOCOME;
967 error = tp->t_fb->tfb_tcp_output(tp);
968 if (flags & PRUS_MORETOCOME)
969 tp->t_flags &= ~TF_MORETOCOME;
973 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
975 SOCKBUF_LOCK(&so->so_snd);
976 if (sbspace(&so->so_snd) < -512) {
977 SOCKBUF_UNLOCK(&so->so_snd);
983 * According to RFC961 (Assigned Protocols),
984 * the urgent pointer points to the last octet
985 * of urgent data. We continue, however,
986 * to consider it to indicate the first octet
987 * of data past the urgent section.
988 * Otherwise, snd_up should be one lower.
990 sbappendstream_locked(&so->so_snd, m, flags);
991 SOCKBUF_UNLOCK(&so->so_snd);
992 if (nam && tp->t_state < TCPS_SYN_SENT) {
994 * Do implied connect if not yet connected,
995 * initialize window to default value, and
996 * initialize maxseg using peer's cached MSS.
1000 error = tcp6_connect(tp, nam, td);
1002 #if defined(INET6) && defined(INET)
1006 error = tcp_connect(tp, nam, td);
1010 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1013 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1014 if (!(flags & PRUS_NOTREADY)) {
1015 tp->t_flags |= TF_FORCEDATA;
1016 error = tp->t_fb->tfb_tcp_output(tp);
1017 tp->t_flags &= ~TF_FORCEDATA;
1021 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1022 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1023 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1024 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1026 if (flags & PRUS_EOF)
1027 INP_INFO_RUNLOCK(&V_tcbinfo);
1032 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1038 inp = sotoinpcb(so);
1040 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1042 for (int i = 0; i < count; i++)
1044 return (ECONNRESET);
1046 tp = intotcpcb(inp);
1048 SOCKBUF_LOCK(&so->so_snd);
1049 error = sbready(&so->so_snd, m, count);
1050 SOCKBUF_UNLOCK(&so->so_snd);
1052 error = tp->t_fb->tfb_tcp_output(tp);
1059 * Abort the TCP. Drop the connection abruptly.
1062 tcp_usr_abort(struct socket *so)
1065 struct tcpcb *tp = NULL;
1068 inp = sotoinpcb(so);
1069 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1071 INP_INFO_RLOCK(&V_tcbinfo);
1073 KASSERT(inp->inp_socket != NULL,
1074 ("tcp_usr_abort: inp_socket == NULL"));
1077 * If we still have full TCP state, and we're not dropped, drop.
1079 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1080 !(inp->inp_flags & INP_DROPPED)) {
1081 tp = intotcpcb(inp);
1083 tcp_drop(tp, ECONNABORTED);
1084 TCPDEBUG2(PRU_ABORT);
1085 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1087 if (!(inp->inp_flags & INP_DROPPED)) {
1089 so->so_state |= SS_PROTOREF;
1091 inp->inp_flags |= INP_SOCKREF;
1094 INP_INFO_RUNLOCK(&V_tcbinfo);
1098 * TCP socket is closed. Start friendly disconnect.
1101 tcp_usr_close(struct socket *so)
1104 struct tcpcb *tp = NULL;
1107 inp = sotoinpcb(so);
1108 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1110 INP_INFO_RLOCK(&V_tcbinfo);
1112 KASSERT(inp->inp_socket != NULL,
1113 ("tcp_usr_close: inp_socket == NULL"));
1116 * If we still have full TCP state, and we're not dropped, initiate
1119 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1120 !(inp->inp_flags & INP_DROPPED)) {
1121 tp = intotcpcb(inp);
1124 TCPDEBUG2(PRU_CLOSE);
1125 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1127 if (!(inp->inp_flags & INP_DROPPED)) {
1129 so->so_state |= SS_PROTOREF;
1131 inp->inp_flags |= INP_SOCKREF;
1134 INP_INFO_RUNLOCK(&V_tcbinfo);
1138 * Receive out-of-band data.
1141 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1145 struct tcpcb *tp = NULL;
1148 inp = sotoinpcb(so);
1149 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1151 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1155 tp = intotcpcb(inp);
1157 if ((so->so_oobmark == 0 &&
1158 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1159 so->so_options & SO_OOBINLINE ||
1160 tp->t_oobflags & TCPOOB_HADDATA) {
1164 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1165 error = EWOULDBLOCK;
1169 *mtod(m, caddr_t) = tp->t_iobc;
1170 if ((flags & MSG_PEEK) == 0)
1171 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1174 TCPDEBUG2(PRU_RCVOOB);
1175 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1181 struct pr_usrreqs tcp_usrreqs = {
1182 .pru_abort = tcp_usr_abort,
1183 .pru_accept = tcp_usr_accept,
1184 .pru_attach = tcp_usr_attach,
1185 .pru_bind = tcp_usr_bind,
1186 .pru_connect = tcp_usr_connect,
1187 .pru_control = in_control,
1188 .pru_detach = tcp_usr_detach,
1189 .pru_disconnect = tcp_usr_disconnect,
1190 .pru_listen = tcp_usr_listen,
1191 .pru_peeraddr = in_getpeeraddr,
1192 .pru_rcvd = tcp_usr_rcvd,
1193 .pru_rcvoob = tcp_usr_rcvoob,
1194 .pru_send = tcp_usr_send,
1195 .pru_ready = tcp_usr_ready,
1196 .pru_shutdown = tcp_usr_shutdown,
1197 .pru_sockaddr = in_getsockaddr,
1198 .pru_sosetlabel = in_pcbsosetlabel,
1199 .pru_close = tcp_usr_close,
1204 struct pr_usrreqs tcp6_usrreqs = {
1205 .pru_abort = tcp_usr_abort,
1206 .pru_accept = tcp6_usr_accept,
1207 .pru_attach = tcp_usr_attach,
1208 .pru_bind = tcp6_usr_bind,
1209 .pru_connect = tcp6_usr_connect,
1210 .pru_control = in6_control,
1211 .pru_detach = tcp_usr_detach,
1212 .pru_disconnect = tcp_usr_disconnect,
1213 .pru_listen = tcp6_usr_listen,
1214 .pru_peeraddr = in6_mapped_peeraddr,
1215 .pru_rcvd = tcp_usr_rcvd,
1216 .pru_rcvoob = tcp_usr_rcvoob,
1217 .pru_send = tcp_usr_send,
1218 .pru_ready = tcp_usr_ready,
1219 .pru_shutdown = tcp_usr_shutdown,
1220 .pru_sockaddr = in6_mapped_sockaddr,
1221 .pru_sosetlabel = in_pcbsosetlabel,
1222 .pru_close = tcp_usr_close,
1228 * Common subroutine to open a TCP connection to remote host specified
1229 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1230 * port number if needed. Call in_pcbconnect_setup to do the routing and
1231 * to choose a local host address (interface). If there is an existing
1232 * incarnation of the same connection in TIME-WAIT state and if the remote
1233 * host was sending CC options and if the connection duration was < MSL, then
1234 * truncate the previous TIME-WAIT state and proceed.
1235 * Initialize connection parameters and enter SYN-SENT state.
1238 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1240 struct inpcb *inp = tp->t_inpcb, *oinp;
1241 struct socket *so = inp->inp_socket;
1242 struct in_addr laddr;
1246 INP_WLOCK_ASSERT(inp);
1247 INP_HASH_WLOCK(&V_tcbinfo);
1249 if (inp->inp_lport == 0) {
1250 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1256 * Cannot simply call in_pcbconnect, because there might be an
1257 * earlier incarnation of this same connection still in
1258 * TIME_WAIT state, creating an ADDRINUSE error.
1260 laddr = inp->inp_laddr;
1261 lport = inp->inp_lport;
1262 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1263 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1264 if (error && oinp == NULL)
1270 inp->inp_laddr = laddr;
1272 INP_HASH_WUNLOCK(&V_tcbinfo);
1275 * Compute window scaling to request:
1276 * Scale to fit into sweet spot. See tcp_syncache.c.
1277 * XXX: This should move to tcp_output().
1279 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1280 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1281 tp->request_r_scale++;
1284 TCPSTAT_INC(tcps_connattempt);
1285 tcp_state_change(tp, TCPS_SYN_SENT);
1286 tp->iss = tcp_new_isn(tp);
1287 tcp_sendseqinit(tp);
1292 INP_HASH_WUNLOCK(&V_tcbinfo);
1299 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1301 struct inpcb *inp = tp->t_inpcb;
1304 INP_WLOCK_ASSERT(inp);
1305 INP_HASH_WLOCK(&V_tcbinfo);
1307 if (inp->inp_lport == 0) {
1308 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1312 error = in6_pcbconnect(inp, nam, td->td_ucred);
1315 INP_HASH_WUNLOCK(&V_tcbinfo);
1317 /* Compute window scaling to request. */
1318 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1319 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1320 tp->request_r_scale++;
1322 soisconnecting(inp->inp_socket);
1323 TCPSTAT_INC(tcps_connattempt);
1324 tcp_state_change(tp, TCPS_SYN_SENT);
1325 tp->iss = tcp_new_isn(tp);
1326 tcp_sendseqinit(tp);
1331 INP_HASH_WUNLOCK(&V_tcbinfo);
1337 * Export TCP internal state information via a struct tcp_info, based on the
1338 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1339 * (TCP state machine, etc). We export all information using FreeBSD-native
1340 * constants -- for example, the numeric values for tcpi_state will differ
1344 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1347 INP_WLOCK_ASSERT(tp->t_inpcb);
1348 bzero(ti, sizeof(*ti));
1350 ti->tcpi_state = tp->t_state;
1351 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1352 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1353 if (tp->t_flags & TF_SACK_PERMIT)
1354 ti->tcpi_options |= TCPI_OPT_SACK;
1355 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1356 ti->tcpi_options |= TCPI_OPT_WSCALE;
1357 ti->tcpi_snd_wscale = tp->snd_scale;
1358 ti->tcpi_rcv_wscale = tp->rcv_scale;
1360 if (tp->t_flags & TF_ECN_PERMIT)
1361 ti->tcpi_options |= TCPI_OPT_ECN;
1363 ti->tcpi_rto = tp->t_rxtcur * tick;
1364 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1365 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1366 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1368 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1369 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1372 * FreeBSD-specific extension fields for tcp_info.
1374 ti->tcpi_rcv_space = tp->rcv_wnd;
1375 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1376 ti->tcpi_snd_wnd = tp->snd_wnd;
1377 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1378 ti->tcpi_snd_nxt = tp->snd_nxt;
1379 ti->tcpi_snd_mss = tp->t_maxseg;
1380 ti->tcpi_rcv_mss = tp->t_maxseg;
1381 if (tp->t_flags & TF_TOE)
1382 ti->tcpi_options |= TCPI_OPT_TOE;
1383 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1384 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1385 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1389 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1390 * socket option arguments. When it re-acquires the lock after the copy, it
1391 * has to revalidate that the connection is still valid for the socket
1394 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1396 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1399 return (ECONNRESET); \
1401 tp = intotcpcb(inp); \
1403 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1406 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1411 struct tcp_function_block *blk;
1412 struct tcp_function_set fsn;
1415 inp = sotoinpcb(so);
1416 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1418 if (sopt->sopt_level != IPPROTO_TCP) {
1420 if (inp->inp_vflag & INP_IPV6PROTO) {
1422 error = ip6_ctloutput(so, sopt);
1425 #if defined(INET6) && defined(INET)
1431 error = ip_ctloutput(so, sopt);
1436 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1438 return (ECONNRESET);
1440 tp = intotcpcb(inp);
1442 * Protect the TCP option TCP_FUNCTION_BLK so
1443 * that a sub-function can *never* overwrite this.
1445 if ((sopt->sopt_dir == SOPT_SET) &&
1446 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1448 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1452 INP_WLOCK_RECHECK(inp);
1453 blk = find_and_ref_tcp_functions(&fsn);
1458 if (tp->t_fb == blk) {
1459 /* You already have this */
1460 refcount_release(&blk->tfb_refcnt);
1464 if (tp->t_state != TCPS_CLOSED) {
1467 * The user has advanced the state
1468 * past the initial point, we may not
1469 * be able to switch.
1471 if (blk->tfb_tcp_handoff_ok != NULL) {
1473 * Does the stack provide a
1474 * query mechanism, if so it may
1475 * still be possible?
1477 error = (*blk->tfb_tcp_handoff_ok)(tp);
1480 refcount_release(&blk->tfb_refcnt);
1485 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1486 refcount_release(&blk->tfb_refcnt);
1491 * Release the old refcnt, the
1492 * lookup acquired a ref on the
1495 if (tp->t_fb->tfb_tcp_fb_fini) {
1497 * Tell the stack to cleanup with 0 i.e.
1498 * the tcb is not going away.
1500 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1502 refcount_release(&tp->t_fb->tfb_refcnt);
1504 if (tp->t_fb->tfb_tcp_fb_init) {
1505 (*tp->t_fb->tfb_tcp_fb_init)(tp);
1508 if (tp->t_flags & TF_TOE) {
1509 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1515 } else if ((sopt->sopt_dir == SOPT_GET) &&
1516 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1517 strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
1518 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1520 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1523 /* Pass in the INP locked, called must unlock it */
1524 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1528 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1530 int error, opt, optval;
1533 struct cc_algo *algo;
1534 char *pbuf, buf[TCP_CA_NAME_MAX];
1538 * For TCP_CCALGOOPT forward the control to CC module, for both
1539 * SOPT_SET and SOPT_GET.
1541 switch (sopt->sopt_name) {
1544 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1545 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1546 sopt->sopt_valsize);
1551 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1552 if (CC_ALGO(tp)->ctl_output != NULL)
1553 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1557 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1558 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1563 switch (sopt->sopt_dir) {
1565 switch (sopt->sopt_name) {
1566 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1568 if (!TCPMD5_ENABLED()) {
1570 return (ENOPROTOOPT);
1572 error = TCPMD5_PCBCTL(inp, sopt);
1575 goto unlock_and_done;
1581 error = sooptcopyin(sopt, &optval, sizeof optval,
1586 INP_WLOCK_RECHECK(inp);
1587 switch (sopt->sopt_name) {
1595 opt = 0; /* dead code to fool gcc */
1602 tp->t_flags &= ~opt;
1605 if (tp->t_flags & TF_TOE) {
1606 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1615 error = sooptcopyin(sopt, &optval, sizeof optval,
1620 INP_WLOCK_RECHECK(inp);
1622 tp->t_flags |= TF_NOPUSH;
1623 else if (tp->t_flags & TF_NOPUSH) {
1624 tp->t_flags &= ~TF_NOPUSH;
1625 if (TCPS_HAVEESTABLISHED(tp->t_state))
1626 error = tp->t_fb->tfb_tcp_output(tp);
1628 goto unlock_and_done;
1632 error = sooptcopyin(sopt, &optval, sizeof optval,
1637 INP_WLOCK_RECHECK(inp);
1638 if (optval > 0 && optval <= tp->t_maxseg &&
1639 optval + 40 >= V_tcp_minmss)
1640 tp->t_maxseg = optval;
1643 goto unlock_and_done;
1650 case TCP_CONGESTION:
1652 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1655 buf[sopt->sopt_valsize] = '\0';
1656 INP_WLOCK_RECHECK(inp);
1658 STAILQ_FOREACH(algo, &cc_list, entries)
1659 if (strncmp(buf, algo->name,
1660 TCP_CA_NAME_MAX) == 0)
1669 * We hold a write lock over the tcb so it's safe to
1670 * do these things without ordering concerns.
1672 if (CC_ALGO(tp)->cb_destroy != NULL)
1673 CC_ALGO(tp)->cb_destroy(tp->ccv);
1676 * If something goes pear shaped initialising the new
1677 * algo, fall back to newreno (which does not
1678 * require initialisation).
1680 if (algo->cb_init != NULL &&
1681 algo->cb_init(tp->ccv) != 0) {
1682 CC_ALGO(tp) = &newreno_cc_algo;
1684 * The only reason init should fail is
1685 * because of malloc.
1696 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1700 if (ui > (UINT_MAX / hz)) {
1706 INP_WLOCK_RECHECK(inp);
1707 switch (sopt->sopt_name) {
1709 tp->t_keepidle = ui;
1711 * XXX: better check current remaining
1712 * timeout and "merge" it with new value.
1714 if ((tp->t_state > TCPS_LISTEN) &&
1715 (tp->t_state <= TCPS_CLOSING))
1716 tcp_timer_activate(tp, TT_KEEP,
1720 tp->t_keepintvl = ui;
1721 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1722 (TP_MAXIDLE(tp) > 0))
1723 tcp_timer_activate(tp, TT_2MSL,
1727 tp->t_keepinit = ui;
1728 if (tp->t_state == TCPS_SYN_RECEIVED ||
1729 tp->t_state == TCPS_SYN_SENT)
1730 tcp_timer_activate(tp, TT_KEEP,
1734 goto unlock_and_done;
1738 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1742 INP_WLOCK_RECHECK(inp);
1744 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1745 (TP_MAXIDLE(tp) > 0))
1746 tcp_timer_activate(tp, TT_2MSL,
1748 goto unlock_and_done;
1754 error = sooptcopyin(sopt, &optval, sizeof optval,
1759 INP_WLOCK_RECHECK(inp);
1761 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1762 &(tp->t_outpkts) : &(tp->t_inpkts),
1766 goto unlock_and_done;
1772 if (!V_tcp_fastopen_enabled)
1775 error = sooptcopyin(sopt, &optval, sizeof optval,
1780 INP_WLOCK_RECHECK(inp);
1782 tp->t_flags |= TF_FASTOPEN;
1783 if ((tp->t_state == TCPS_LISTEN) &&
1784 (tp->t_tfo_pending == NULL))
1786 tcp_fastopen_alloc_counter();
1788 tp->t_flags &= ~TF_FASTOPEN;
1789 goto unlock_and_done;
1794 error = ENOPROTOOPT;
1800 tp = intotcpcb(inp);
1801 switch (sopt->sopt_name) {
1802 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1804 if (!TCPMD5_ENABLED()) {
1806 return (ENOPROTOOPT);
1808 error = TCPMD5_PCBCTL(inp, sopt);
1813 optval = tp->t_flags & TF_NODELAY;
1815 error = sooptcopyout(sopt, &optval, sizeof optval);
1818 optval = tp->t_maxseg;
1820 error = sooptcopyout(sopt, &optval, sizeof optval);
1823 optval = tp->t_flags & TF_NOOPT;
1825 error = sooptcopyout(sopt, &optval, sizeof optval);
1828 optval = tp->t_flags & TF_NOPUSH;
1830 error = sooptcopyout(sopt, &optval, sizeof optval);
1833 tcp_fill_info(tp, &ti);
1835 error = sooptcopyout(sopt, &ti, sizeof ti);
1837 case TCP_CONGESTION:
1838 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1840 error = sooptcopyout(sopt, buf, len + 1);
1846 switch (sopt->sopt_name) {
1848 ui = TP_KEEPIDLE(tp) / hz;
1851 ui = TP_KEEPINTVL(tp) / hz;
1854 ui = TP_KEEPINIT(tp) / hz;
1857 ui = TP_KEEPCNT(tp);
1861 error = sooptcopyout(sopt, &ui, sizeof(ui));
1866 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1867 &(tp->t_outpkts) : &(tp->t_inpkts));
1869 error = sooptcopyout(sopt, &optval, sizeof optval);
1875 optval = tp->t_flags & TF_FASTOPEN;
1877 error = sooptcopyout(sopt, &optval, sizeof optval);
1882 error = ENOPROTOOPT;
1889 #undef INP_WLOCK_RECHECK
1890 #undef INP_WLOCK_RECHECK_CLEANUP
1893 * Attach TCP protocol to socket, allocating
1894 * internet protocol control block, tcp control block,
1895 * bufer space, and entering LISTEN state if to accept connections.
1898 tcp_attach(struct socket *so)
1904 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1905 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1909 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1910 so->so_snd.sb_flags |= SB_AUTOSIZE;
1911 INP_INFO_RLOCK(&V_tcbinfo);
1912 error = in_pcballoc(so, &V_tcbinfo);
1914 INP_INFO_RUNLOCK(&V_tcbinfo);
1917 inp = sotoinpcb(so);
1919 if (inp->inp_vflag & INP_IPV6PROTO) {
1920 inp->inp_vflag |= INP_IPV6;
1921 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1922 inp->inp_vflag |= INP_IPV4;
1923 inp->in6p_hops = -1; /* use kernel default */
1927 inp->inp_vflag |= INP_IPV4;
1928 tp = tcp_newtcpcb(inp);
1932 INP_INFO_RUNLOCK(&V_tcbinfo);
1935 tp->t_state = TCPS_CLOSED;
1937 INP_INFO_RUNLOCK(&V_tcbinfo);
1938 TCPSTATES_INC(TCPS_CLOSED);
1943 * Initiate (or continue) disconnect.
1944 * If embryonic state, just send reset (once).
1945 * If in ``let data drain'' option and linger null, just drop.
1946 * Otherwise (hard), mark socket disconnecting and drop
1947 * current input data; switch states based on user close, and
1948 * send segment to peer (with FIN).
1951 tcp_disconnect(struct tcpcb *tp)
1953 struct inpcb *inp = tp->t_inpcb;
1954 struct socket *so = inp->inp_socket;
1956 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1957 INP_WLOCK_ASSERT(inp);
1960 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1961 * socket is still open.
1963 if (tp->t_state < TCPS_ESTABLISHED) {
1966 ("tcp_disconnect: tcp_close() returned NULL"));
1967 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1968 tp = tcp_drop(tp, 0);
1970 ("tcp_disconnect: tcp_drop() returned NULL"));
1972 soisdisconnecting(so);
1973 sbflush(&so->so_rcv);
1975 if (!(inp->inp_flags & INP_DROPPED))
1976 tp->t_fb->tfb_tcp_output(tp);
1981 * User issued close, and wish to trail through shutdown states:
1982 * if never received SYN, just forget it. If got a SYN from peer,
1983 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1984 * If already got a FIN from peer, then almost done; go to LAST_ACK
1985 * state. In all other cases, have already sent FIN to peer (e.g.
1986 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1987 * for peer to send FIN or not respond to keep-alives, etc.
1988 * We can let the user exit from the close as soon as the FIN is acked.
1991 tcp_usrclosed(struct tcpcb *tp)
1994 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1995 INP_WLOCK_ASSERT(tp->t_inpcb);
1997 switch (tp->t_state) {
2000 tcp_offload_listen_stop(tp);
2002 tcp_state_change(tp, TCPS_CLOSED);
2007 * tcp_close() should never return NULL here as the socket is
2011 ("tcp_usrclosed: tcp_close() returned NULL"));
2015 case TCPS_SYN_RECEIVED:
2016 tp->t_flags |= TF_NEEDFIN;
2019 case TCPS_ESTABLISHED:
2020 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2023 case TCPS_CLOSE_WAIT:
2024 tcp_state_change(tp, TCPS_LAST_ACK);
2027 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2028 soisdisconnected(tp->t_inpcb->inp_socket);
2029 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2030 if (tp->t_state == TCPS_FIN_WAIT_2) {
2033 timeout = (tcp_fast_finwait2_recycle) ?
2034 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2035 tcp_timer_activate(tp, TT_2MSL, timeout);
2042 db_print_indent(int indent)
2046 for (i = 0; i < indent; i++)
2051 db_print_tstate(int t_state)
2056 db_printf("TCPS_CLOSED");
2060 db_printf("TCPS_LISTEN");
2064 db_printf("TCPS_SYN_SENT");
2067 case TCPS_SYN_RECEIVED:
2068 db_printf("TCPS_SYN_RECEIVED");
2071 case TCPS_ESTABLISHED:
2072 db_printf("TCPS_ESTABLISHED");
2075 case TCPS_CLOSE_WAIT:
2076 db_printf("TCPS_CLOSE_WAIT");
2079 case TCPS_FIN_WAIT_1:
2080 db_printf("TCPS_FIN_WAIT_1");
2084 db_printf("TCPS_CLOSING");
2088 db_printf("TCPS_LAST_ACK");
2091 case TCPS_FIN_WAIT_2:
2092 db_printf("TCPS_FIN_WAIT_2");
2095 case TCPS_TIME_WAIT:
2096 db_printf("TCPS_TIME_WAIT");
2100 db_printf("unknown");
2106 db_print_tflags(u_int t_flags)
2111 if (t_flags & TF_ACKNOW) {
2112 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2115 if (t_flags & TF_DELACK) {
2116 db_printf("%sTF_DELACK", comma ? ", " : "");
2119 if (t_flags & TF_NODELAY) {
2120 db_printf("%sTF_NODELAY", comma ? ", " : "");
2123 if (t_flags & TF_NOOPT) {
2124 db_printf("%sTF_NOOPT", comma ? ", " : "");
2127 if (t_flags & TF_SENTFIN) {
2128 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2131 if (t_flags & TF_REQ_SCALE) {
2132 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2135 if (t_flags & TF_RCVD_SCALE) {
2136 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2139 if (t_flags & TF_REQ_TSTMP) {
2140 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2143 if (t_flags & TF_RCVD_TSTMP) {
2144 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2147 if (t_flags & TF_SACK_PERMIT) {
2148 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2151 if (t_flags & TF_NEEDSYN) {
2152 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2155 if (t_flags & TF_NEEDFIN) {
2156 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2159 if (t_flags & TF_NOPUSH) {
2160 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2163 if (t_flags & TF_MORETOCOME) {
2164 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2167 if (t_flags & TF_LQ_OVERFLOW) {
2168 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2171 if (t_flags & TF_LASTIDLE) {
2172 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2175 if (t_flags & TF_RXWIN0SENT) {
2176 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2179 if (t_flags & TF_FASTRECOVERY) {
2180 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2183 if (t_flags & TF_CONGRECOVERY) {
2184 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2187 if (t_flags & TF_WASFRECOVERY) {
2188 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2191 if (t_flags & TF_SIGNATURE) {
2192 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2195 if (t_flags & TF_FORCEDATA) {
2196 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2199 if (t_flags & TF_TSO) {
2200 db_printf("%sTF_TSO", comma ? ", " : "");
2203 if (t_flags & TF_ECN_PERMIT) {
2204 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2207 if (t_flags & TF_FASTOPEN) {
2208 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2214 db_print_toobflags(char t_oobflags)
2219 if (t_oobflags & TCPOOB_HAVEDATA) {
2220 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2223 if (t_oobflags & TCPOOB_HADDATA) {
2224 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2230 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2233 db_print_indent(indent);
2234 db_printf("%s at %p\n", name, tp);
2238 db_print_indent(indent);
2239 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2240 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2242 db_print_indent(indent);
2243 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2244 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2246 db_print_indent(indent);
2247 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2248 &tp->t_timers->tt_delack, tp->t_inpcb);
2250 db_print_indent(indent);
2251 db_printf("t_state: %d (", tp->t_state);
2252 db_print_tstate(tp->t_state);
2255 db_print_indent(indent);
2256 db_printf("t_flags: 0x%x (", tp->t_flags);
2257 db_print_tflags(tp->t_flags);
2260 db_print_indent(indent);
2261 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2262 tp->snd_una, tp->snd_max, tp->snd_nxt);
2264 db_print_indent(indent);
2265 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2266 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2268 db_print_indent(indent);
2269 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2270 tp->iss, tp->irs, tp->rcv_nxt);
2272 db_print_indent(indent);
2273 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2274 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2276 db_print_indent(indent);
2277 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2278 tp->snd_wnd, tp->snd_cwnd);
2280 db_print_indent(indent);
2281 db_printf("snd_ssthresh: %u snd_recover: "
2282 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2284 db_print_indent(indent);
2285 db_printf("t_rcvtime: %u t_startime: %u\n",
2286 tp->t_rcvtime, tp->t_starttime);
2288 db_print_indent(indent);
2289 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2290 tp->t_rtttime, tp->t_rtseq);
2292 db_print_indent(indent);
2293 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2294 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2296 db_print_indent(indent);
2297 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2298 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2301 db_print_indent(indent);
2302 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2303 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2305 db_print_indent(indent);
2306 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2307 db_print_toobflags(tp->t_oobflags);
2308 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2310 db_print_indent(indent);
2311 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2312 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2314 db_print_indent(indent);
2315 db_printf("ts_recent: %u ts_recent_age: %u\n",
2316 tp->ts_recent, tp->ts_recent_age);
2318 db_print_indent(indent);
2319 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2320 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2322 db_print_indent(indent);
2323 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2324 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2325 tp->snd_recover_prev, tp->t_badrxtwin);
2327 db_print_indent(indent);
2328 db_printf("snd_numholes: %d snd_holes first: %p\n",
2329 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2331 db_print_indent(indent);
2332 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2333 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2335 /* Skip sackblks, sackhint. */
2337 db_print_indent(indent);
2338 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2339 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2342 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2347 db_printf("usage: show tcpcb <addr>\n");
2350 tp = (struct tcpcb *)addr;
2352 db_print_tcpcb(tp, "tcpcb", 0);